From 7a5b1847f5be528a27551342a543bfa24afcacf0 Mon Sep 17 00:00:00 2001 From: pvtrx Date: Thu, 27 Nov 2025 15:12:34 +0100 Subject: [PATCH] fioxed errors that failed the first 4 tests - imageInput now done --- imageInput.c | 98 +++++++++++++++++++++++++++++++++----------------- imageInput.h | 5 ++- testFile.info2 | 1 - 3 files changed, 69 insertions(+), 35 deletions(-) delete mode 100644 testFile.info2 diff --git a/imageInput.c b/imageInput.c index fc222d3..93d2615 100644 --- a/imageInput.c +++ b/imageInput.c @@ -3,49 +3,71 @@ #include #include "imageInput.h" -#define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" +#define FILE_HEADER_LENGTH (sizeof(FILE_HEADER_STRING) - 1U) -// 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 +// Reads an entire image series file and returns a heap-allocated descriptor. GrayScaleImageSeries *readImages(const char *path) { - GrayScaleImageSeries *series = NULL; - FILE *file = fopen(path, "rb"); - - if(file != NULL) + if(file == NULL) { - if(checkFileHeader(file)) - { - unsigned int numberOfImages = readDimension(file); - unsigned short int width = readDimension(file); - unsigned short int height = readDimension(file); - series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); - series->images = (GrayScaleImage *)malloc(numberOfImages * sizeof(GrayScaleImage)); - series->labels = (unsigned char *)malloc(numberOfImages * sizeof(unsigned char)); - series->count = numberOfImages; - for(unsigned int i = 0; i < numberOfImages; i++) - { - series->images[i] = readImage(file); - series->labels[i] = readLabel(file); - } - fclose(file); - return series; - } + return NULL; } + + if(!checkFileHeader(file)) + { + fclose(file); + return NULL; + } + + unsigned int numberOfImages = readDimension(file); + unsigned short width = readDimension(file); + unsigned short height = readDimension(file); + + GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries)); + if(series == NULL) + { + fclose(file); + return NULL; + } + + series->images = (GrayScaleImage *)calloc(numberOfImages, sizeof(GrayScaleImage)); + series->labels = (unsigned char *)calloc(numberOfImages, sizeof(unsigned char)); + series->count = numberOfImages; + + if(series->images == NULL || series->labels == NULL) + { + clearSeries(series); + fclose(file); + return NULL; + } + + for(unsigned int i = 0; i < numberOfImages; i++) + { + series->images[i] = readImage(file, width, height); + series->labels[i] = readLabel(file); + } + + fclose(file); return series; } +// Validates and consumes the file header tag that identifies the custom format. bool checkFileHeader(FILE *file) { - char buffer[BUFFER_SIZE] = {0}; - fread(buffer, sizeof(char), BUFFER_SIZE, file); - return strcmp(buffer, FILE_HEADER_STRING) == 0; + char buffer[FILE_HEADER_LENGTH + 1] = {0}; + size_t bytesRead = fread(buffer, sizeof(char), FILE_HEADER_LENGTH, file); + if(bytesRead != FILE_HEADER_LENGTH) + { + return false; + } + + return strncmp(buffer, FILE_HEADER_STRING, FILE_HEADER_LENGTH) == 0; } +// Reads a single 16-bit dimension (width, height or count of Images) from the file. unsigned short int readDimension(FILE *file) { unsigned short int dimension = 0; @@ -53,14 +75,24 @@ unsigned short int readDimension(FILE *file) return dimension; } -GrayScaleImage readImage(FILE *file) +// Reads a single grayscale image of the provided dimensions from the file. +GrayScaleImage readImage(FILE *file, unsigned short width, unsigned short height) { - GrayScaleImage image; - image.buffer = (GrayScalePixelType *)malloc(image.width * image.height * sizeof(GrayScalePixelType)); - fread(image.buffer, sizeof(GrayScalePixelType), image.width * image.height, file); + GrayScaleImage image = {0}; + image.width = width; + image.height = height; + + unsigned int pixelCount = (unsigned int)width * (unsigned int)height; + image.buffer = (GrayScalePixelType *)malloc(pixelCount * sizeof(GrayScalePixelType)); + if(image.buffer != NULL) + { + fread(image.buffer, sizeof(GrayScalePixelType), pixelCount, file); + } + return image; } +// Reads one byte label that belongs to the preceding image. unsigned char readLabel(FILE *file) { unsigned char label = 0; @@ -68,7 +100,7 @@ unsigned char readLabel(FILE *file) return label; } -// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt +// Releases all heap allocations associated with a GrayScaleImageSeries. void clearSeries(GrayScaleImageSeries *series) { if (series != NULL) diff --git a/imageInput.h b/imageInput.h index 8a861bc..d371566 100644 --- a/imageInput.h +++ b/imageInput.h @@ -1,6 +1,9 @@ #ifndef IMAGEINPUT_H #define IMAGEINPUT_H +#include +#include + typedef unsigned char GrayScalePixelType; typedef struct @@ -22,7 +25,7 @@ GrayScaleImageSeries *readImages(const char *path); void clearSeries(GrayScaleImageSeries *series); bool checkFileHeader(FILE *file); unsigned short int readDimension(FILE *file); -GrayScaleImage readImage(FILE *file); +GrayScaleImage readImage(FILE *file, unsigned short width, unsigned short height); unsigned char readLabel(FILE *file); #endif diff --git a/testFile.info2 b/testFile.info2 deleted file mode 100644 index 1ea3b4c..0000000 --- a/testFile.info2 +++ /dev/null @@ -1 +0,0 @@ -some_tag \ No newline at end of file