diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index d37a006..4548438 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -19,63 +19,76 @@ static int controlFile(FILE *file) { static unsigned int readNumber(FILE *file) { unsigned int number = 0; - fread(&number, sizeof(unsigned short), 1, file); + 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) { - int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file); - if (readcount == 0) - return 0; + unsigned int readcount = fread(buffer, sizeof(GrayScalePixelType), bufferSize, file); + if (readcount == 0) { + return 0; + } return 1; } -static void readLabels(FILE *file, unsigned char label[], unsigned int count) { - fread(label + count, sizeof(char), 1, file); +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 *firstSeries = malloc(sizeof(GrayScaleImageSeries)); - firstSeries ->count = readNumber(file); + GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); + series->count = readNumber(file); unsigned int width = readNumber(file); unsigned int height = readNumber(file); - - firstSeries ->labels = malloc(firstSeries ->count*sizeof(unsigned char)); - firstSeries ->images = malloc(firstSeries ->count*sizeof(GrayScaleImage)); - firstSeries ->addrSourceTxt = file; - if (firstSeries ->labels == NULL || firstSeries ->images == NULL) { + if (width == 0 || height == 0 || series->count == 0 || series == NULL) { return NULL; } - int fault = 1; - unsigned int const bufferSize = width * height; - for (unsigned int i = 0; i < firstSeries ->count && fault; i++) - { - GrayScaleImage *grayScaleImage = &firstSeries ->images[i]; - grayScaleImage ->buffer = malloc(width * height*sizeof(GrayScalePixelType)); - grayScaleImage ->width = width; - grayScaleImage -> height = height; - if (width == 0 || height == 0 || firstSeries ->count == 0) { // prüfen ob eingabe richtig ist - return NULL; - } - - fault = fillBuffer(grayScaleImage ->buffer, bufferSize, file); - readLabels(file, firstSeries ->labels, i); + // 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; } - return firstSeries; + // 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 == NULL) { return; } if (series->images != NULL) { for (unsigned int i = 0; i < series->count; i++) { const GrayScaleImage image = series->images[i]; @@ -91,4 +104,3 @@ void clearSeries(GrayScaleImageSeries *series) { fclose(series->addrSourceTxt); free(series); } -