From c26c5247e3db74b5765106fab035d119a10bb2a0 Mon Sep 17 00:00:00 2001 From: Lukas Dotterweich Date: Sat, 7 Nov 2026 16:49:56 +0100 Subject: [PATCH 1/4] Implement ImageInput --- Startcode/imageInput.c | 94 +++++++++++++++++++++++++++++++++++++----- Startcode/imageInput.h | 2 + 2 files changed, 85 insertions(+), 11 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index bb30de1..d37a006 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -6,17 +6,89 @@ #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" -// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei -// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen -GrayScaleImageSeries *readImages(const char *path) -{ - GrayScaleImageSeries *series = NULL; - - return series; +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; + fread(&number, sizeof(unsigned short), 1, file); + 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; + return 1; +} + +static void readLabels(FILE *file, unsigned char label[], unsigned int count) { + fread(label + count, sizeof(char), 1, file); +} + + +GrayScaleImageSeries *readImages(const char *path) { + FILE *file = fopen(path, "rb"); + + if (!controlFile(file)) { + return NULL; + } + GrayScaleImageSeries *firstSeries = malloc(sizeof(GrayScaleImageSeries)); + firstSeries ->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) { + 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); + } + + return firstSeries; +} + + +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); } -// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt -void clearSeries(GrayScaleImageSeries *series) -{ -} \ No newline at end of file diff --git a/Startcode/imageInput.h b/Startcode/imageInput.h index 656e213..891046a 100644 --- a/Startcode/imageInput.h +++ b/Startcode/imageInput.h @@ -15,9 +15,11 @@ typedef struct GrayScaleImage *images; unsigned char *labels; unsigned int count; + FILE *addrSourceTxt; // um in clearSeries den Text schließen zu können } GrayScaleImageSeries; GrayScaleImageSeries *readImages(const char *path); void clearSeries(GrayScaleImageSeries *series); + #endif From 682415e76ebf2c926e5fa574c51eb78f8de782fd Mon Sep 17 00:00:00 2001 From: Lukas Dotterweich Date: Wed, 11 Nov 2026 21:16:28 +0100 Subject: [PATCH 2/4] Implement ImageInput --- Startcode/imageInput.c | 74 ++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 31 deletions(-) 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); } - From 489bd3dc23ddb4dc2a7d4542a6be370c5b41a6c6 Mon Sep 17 00:00:00 2001 From: Lukas Dotterweich Date: Wed, 11 Nov 2026 21:30:22 +0100 Subject: [PATCH 3/4] Implement ImageInput --- Startcode/imageInput.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index 4548438..50b1b0d 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -25,7 +25,7 @@ static unsigned int readNumber(FILE *file) { } return number; } - +// TODO:alles in innere Funktion äußere Funktion nur fopen / fclose // fillBuffer und readLabel muss immer in einer Schleife zusammen stehen static int fillBuffer(unsigned char buffer[], unsigned int bufferSize, FILE *file) { @@ -50,14 +50,19 @@ GrayScaleImageSeries *readImages(const char *path) { if (!controlFile(file)) { return NULL; } - GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); - series->count = readNumber(file); + unsigned int count = readNumber(file); unsigned int width = readNumber(file); unsigned int height = readNumber(file); - if (width == 0 || height == 0 || series->count == 0 || series == NULL) { + if (width == 0 || height == 0 || count == 0) { return NULL; } // Füllen des structs series + GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); + if (series == NULL) { + return NULL; + } + + series->count = count; series->labels = malloc(series->count * sizeof(unsigned char)); series->images = malloc(series->count * sizeof(GrayScaleImage)); series->addrSourceTxt = file; @@ -66,19 +71,20 @@ GrayScaleImageSeries *readImages(const char *path) { } // 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++) { + for (unsigned int i = 0; i < series->count; i++) { + GrayScaleImage *grayScaleImage = &series->images[i]; grayScaleImage->buffer = calloc(bufferSize, width * height * sizeof(GrayScalePixelType)); + if (grayScaleImage->buffer == NULL) { + return NULL; + } + 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) { + int readBufferFault = fillBuffer(grayScaleImage->buffer, bufferSize, file); + int readLabelFault = readLabels(file, series->labels, i); + if (!readLabelFault || !readBufferFault) { return NULL; } } From 65f486df9d04e141e6b92192d349fc981af8f0aa Mon Sep 17 00:00:00 2001 From: Lukas Dotterweich Date: Thu, 12 Nov 2026 09:54:01 +0100 Subject: [PATCH 4/4] Implement ImageInput --- Startcode/imageInput.c | 25 ++++++++++++++++--------- Startcode/imageInput.h | 1 - 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index 50b1b0d..d1aea53 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -43,13 +43,7 @@ static int readLabels(FILE *file, unsigned char label[], unsigned int count) { } return 1; } - - -GrayScaleImageSeries *readImages(const char *path) { - FILE *file = fopen(path, "rb"); - if (!controlFile(file)) { - return NULL; - } +GrayScaleImageSeries *innerSeries(FILE*file) { unsigned int count = readNumber(file); unsigned int width = readNumber(file); unsigned int height = readNumber(file); @@ -65,7 +59,6 @@ GrayScaleImageSeries *readImages(const char *path) { series->count = count; 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; } @@ -93,6 +86,21 @@ GrayScaleImageSeries *readImages(const char *path) { } +GrayScaleImageSeries *readImages(const char *path) { + FILE *file = fopen(path, "rb"); + if (!controlFile(file)) { + return NULL; + } + GrayScaleImageSeries*address = innerSeries(file); + fclose(file); + if (address == NULL) { + return NULL; + } + return address; +} + + + void clearSeries(GrayScaleImageSeries *series) { if (series == NULL) { return; } if (series->images != NULL) { @@ -107,6 +115,5 @@ void clearSeries(GrayScaleImageSeries *series) { if (series->labels != NULL) { free(series->labels); } - fclose(series->addrSourceTxt); free(series); } diff --git a/Startcode/imageInput.h b/Startcode/imageInput.h index 891046a..23fe996 100644 --- a/Startcode/imageInput.h +++ b/Startcode/imageInput.h @@ -15,7 +15,6 @@ typedef struct GrayScaleImage *images; unsigned char *labels; unsigned int count; - FILE *addrSourceTxt; // um in clearSeries den Text schließen zu können } GrayScaleImageSeries; GrayScaleImageSeries *readImages(const char *path);