forked from freudenreichan/info2Praktikum-NeuronalesNetz
107 lines
3.2 KiB
C
107 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "imageInput.h"
|
|
|
|
#define BUFFER_SIZE 100
|
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
|
|
|
|
|
static int controlFile(FILE *file) {
|
|
if (file == NULL) {
|
|
return 0;
|
|
}
|
|
char buffer[BUFFER_SIZE];
|
|
fread(buffer, sizeof(char), strlen(FILE_HEADER_STRING), file);
|
|
buffer[strlen(FILE_HEADER_STRING)] = '\0';
|
|
return !strcmp(FILE_HEADER_STRING, buffer);
|
|
}
|
|
|
|
static unsigned int readNumber(FILE *file) {
|
|
unsigned int number = 0;
|
|
unsigned int readcount = fread(&number, sizeof(unsigned short), 1, file);
|
|
if (readcount != 1) {
|
|
return 0;
|
|
}
|
|
return number;
|
|
}
|
|
|
|
|
|
// fillBuffer und readLabel muss immer in einer Schleife zusammen stehen
|
|
static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) {
|
|
unsigned int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file);
|
|
if (readcount == 0) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int readLabels(FILE *file, unsigned char label[], unsigned int count) {
|
|
unsigned int readlabels = fread(label + count, sizeof(char), 1, file);
|
|
if (readlabels == 0) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
|
|
GrayScaleImageSeries *readImages(const char *path) {
|
|
FILE *file = fopen(path, "rb");
|
|
if (!controlFile(file)) {
|
|
return NULL;
|
|
}
|
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
|
series->count = readNumber(file);
|
|
unsigned int width = readNumber(file);
|
|
unsigned int height = readNumber(file);
|
|
if (width == 0 || height == 0 || series->count == 0 || series == NULL) {
|
|
return NULL;
|
|
}
|
|
// Füllen des structs series
|
|
series->labels = malloc(series->count * sizeof(unsigned char));
|
|
series->images = malloc(series->count * sizeof(GrayScaleImage));
|
|
series->addrSourceTxt = file;
|
|
if (series->labels == NULL || series->images == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
// struct grayScaleImage füllen als Teil eines Arrays innerhalb series
|
|
int readBufferFault = 1;
|
|
int readLabelFault = 1;
|
|
unsigned int const bufferSize = width * height;
|
|
for (unsigned int i = 0; i < series
|
|
->count && readBufferFault && readLabelFault; i++) {
|
|
GrayScaleImage *grayScaleImage = &series->images[i];
|
|
grayScaleImage->buffer = calloc(bufferSize, width * height * sizeof(GrayScalePixelType));
|
|
grayScaleImage->width = width;
|
|
grayScaleImage->height = height;
|
|
readBufferFault = fillBuffer(grayScaleImage->buffer, bufferSize, file);// frage an tobi kann ich readbufferfault auch schöner initialisieren
|
|
readLabelFault = readLabels(file, series->labels, i);
|
|
|
|
if (grayScaleImage->buffer == NULL) {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
return series;
|
|
}
|
|
|
|
|
|
void clearSeries(GrayScaleImageSeries *series) {
|
|
if (series == NULL) { return; }
|
|
if (series->images != NULL) {
|
|
for (unsigned int i = 0; i < series->count; i++) {
|
|
const GrayScaleImage image = series->images[i];
|
|
if (image.buffer != NULL) {
|
|
free(image.buffer);
|
|
}
|
|
}
|
|
free(series->images);
|
|
}
|
|
if (series->labels != NULL) {
|
|
free(series->labels);
|
|
}
|
|
fclose(series->addrSourceTxt);
|
|
free(series);
|
|
}
|