From 4d1908ed27fce836f6a8e34ee5336b202475dd9f Mon Sep 17 00:00:00 2001 From: = Date: Wed, 26 Nov 2025 21:44:58 +0100 Subject: [PATCH 1/2] added outline for imageInput.c --- imageInput.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 97 insertions(+), 1 deletion(-) diff --git a/imageInput.c b/imageInput.c index bb30de1..5520c28 100644 --- a/imageInput.c +++ b/imageInput.c @@ -6,12 +6,99 @@ #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 + +/// @brief Gets the next char value from specified and opened file +/// @param openedFile stream FILE, from which to read +/// @param iterations how many chars are being taken from +static int getCharValueFromFile(FILE* openedFile, int iterations) { + int addToFile = 0; + + if (openedFile == NULL) return 0; + + for (int i = 0; i < iterations; i++) { + int tmp = fgetc(openedFile); + //If the File ends, the method returns a '0' + if (openedFile == EOF) return addToFile; + + addToFile += tmp; + } + + return addToFile; +} // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen GrayScaleImageSeries *readImages(const char *path) { GrayScaleImageSeries *series = NULL; + + FILE* openFile; + + openFile = fopen(path, "rb"); + + // file Could not be opened/does not exist + if (openFile != NULL) { + + char* actualFileTag = malloc(strlen(FILE_HEADER_STRING) * sizeof(char)); + int numberOfImages = 0; + int width = 0; + int height = 0; + + for (int i = 0; i < strlen(FILE_HEADER_STRING); i++) { + actualFileTag += fgetc(openFile); + } + + // checks if the files are equal: strcmp should return '0' --> convert it to '1' for 'true' + int fileTagEqual = !strcmp(actualFileTag, FILE_HEADER_STRING); + + //we only need the fileTag to verify its an image for our series. + free(actualFileTag); + actualFileTag = NULL; + + + if (fileTagEqual) { + numberOfImages = getCharValueFromFile(openFile, 4); + + // no Images in series -> No image-series + if (numberOfImages == 0) { + fclose(openFile); + return series; + } + + width = getCharValueFromFile(openFile, 4); + height = getCharValueFromFile(openFile, 4); + + // no height/width --> impossible file + if(height == 0 || width == 0) { + fclose(openFile); + return series; + } + + //all the starting parameters are set --> the images can be read + GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage)); + unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char)); + + series->count = 0; + series->images = images; + series->labels = labels; + + for (int i = 0; i < numberOfImages; i++) { + + for (int j = 0; j < width * height; j++) { + //this needs to be reworked + images[i].buffer = malloc(width * height); + + images[i].height = height; + images[i].width = width; + images[i].buffer[j] = fgetc(openFile); + } + series->labels[i] = fgetc(openFile); + series->count++; + } + } + + + } + fclose(openFile); return series; } @@ -19,4 +106,13 @@ GrayScaleImageSeries *readImages(const char *path) // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt void clearSeries(GrayScaleImageSeries *series) { + for (int i = 0; i < series->count; i++) { + for (int j = 0; j < series->images->height * series->images->width; j++) { + free(series->images[i].buffer[j]); + } + } + free(series->images); + series->images = NULL; + free(series->labels); + series->labels = NULL; } \ No newline at end of file From 419296af9ee8aef9b53c1d85a752e92e60aa70f9 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 26 Nov 2025 21:55:17 +0100 Subject: [PATCH 2/2] last short fix for the day in imageInput.c --- imageInput.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/imageInput.c b/imageInput.c index 5520c28..2174990 100644 --- a/imageInput.c +++ b/imageInput.c @@ -9,7 +9,7 @@ /// @brief Gets the next char value from specified and opened file /// @param openedFile stream FILE, from which to read -/// @param iterations how many chars are being taken from +/// @param iterations how many chars are being taken from (1 char equals 2 Hexdecimals equals 8bit) static int getCharValueFromFile(FILE* openedFile, int iterations) { int addToFile = 0; @@ -56,7 +56,7 @@ GrayScaleImageSeries *readImages(const char *path) if (fileTagEqual) { - numberOfImages = getCharValueFromFile(openFile, 4); + numberOfImages = getCharValueFromFile(openFile, 2); // no Images in series -> No image-series if (numberOfImages == 0) { @@ -64,8 +64,8 @@ GrayScaleImageSeries *readImages(const char *path) return series; } - width = getCharValueFromFile(openFile, 4); - height = getCharValueFromFile(openFile, 4); + width = getCharValueFromFile(openFile, 2); + height = getCharValueFromFile(openFile, 2); // no height/width --> impossible file if(height == 0 || width == 0) { @@ -73,7 +73,7 @@ GrayScaleImageSeries *readImages(const char *path) return series; } - //all the starting parameters are set --> the images can be read + //all the starting parameters are set --> the images can be read and stored GrayScaleImage* images = malloc(numberOfImages * sizeof(GrayScaleImage)); unsigned char* labels = malloc(numberOfImages * sizeof(unsigned char)); @@ -82,24 +82,22 @@ GrayScaleImageSeries *readImages(const char *path) series->labels = labels; for (int i = 0; i < numberOfImages; i++) { - for (int j = 0; j < width * height; j++) { - //this needs to be reworked + // allocating the actual matrix image for image images[i].buffer = malloc(width * height); images[i].height = height; images[i].width = width; images[i].buffer[j] = fgetc(openFile); } + //rest of the values that only affect the image itself series->labels[i] = fgetc(openFile); series->count++; } } - - } + fclose(openFile); - return series; } @@ -107,10 +105,9 @@ GrayScaleImageSeries *readImages(const char *path) void clearSeries(GrayScaleImageSeries *series) { for (int i = 0; i < series->count; i++) { - for (int j = 0; j < series->images->height * series->images->width; j++) { - free(series->images[i].buffer[j]); - } + free(series->images[i].buffer); } + free(series->images); series->images = NULL; free(series->labels);