From f52de5e6563962c0e461ea4402c3cf5374409b76 Mon Sep 17 00:00:00 2001 From: Niko Rost Date: Mon, 24 Nov 2025 15:16:30 +0100 Subject: [PATCH] Got a compiled Version that runs all the Test. Still 4 Test Failures present --- imageInput.c | 75 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/imageInput.c b/imageInput.c index f882b67..691a01e 100644 --- a/imageInput.c +++ b/imageInput.c @@ -31,31 +31,36 @@ int checkString (FILE *file){ //Checks if String at the start of File equals exp } void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height); -void allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height); +int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height); // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen + + GrayScaleImageSeries *readImages(const char *path) { FILE* file = fopen(path, "rb"); - if(!(checkString(file))) + if(!(checkString(file))){ fclose(file); return NULL; + } // TODO open binary file from file name unsigned int width, height; //uninitialised Variables, will be set by setparamters - GrayscaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series + GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file - if(!allocateMemory(series, width, height)){ + if(!allocateMemory(series, series->count, width, height)){ clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary + fclose(file); + return NULL; } //Getting image data - for(int i = 0; i < series.count ; i++){ //Iterating for every image - for(int j = 0; j < series.count; j++){ //Iterating for every pixel + for(unsigned int i = 0; i < series->count ; i++){ //Iterating for every image + for(unsigned int j = 0; j < width * height; j++){ //Iterating for every pixel fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file); } } @@ -66,19 +71,19 @@ 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) { - printf("ClearSeries BP1\n"); - for(int i = series->count - 1; i >= 0; i--) + if (!series) + return; + + //Freeing Pixelbuffer for every image + for(unsigned int i = 0; i < series->count; i++) { - free(series->images+series->count*sizeof(GrayScaleImage)*i);//Muss Falsch sein - free(series->labels+series->count*sizeof(unsigned char)*i); + free(series->images[i].buffer); } - printf("ClearSeries BP2\n"); - series->images = NULL; - series->labels = NULL; + free(series->images); //Freeing Images array - free(series); + free(series->labels); //Freeing Labels + free(series); //Freeing Main Structure - series = NULL; } void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters @@ -86,6 +91,13 @@ void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, un fseek(file, 27, SEEK_SET); fread(buffer, 1, 3, file); + if(fread(buffer, 1, 3, file) != 3){ + *imageCount = 0; + *width = 0; + *height = 0; + + + return;} //Stops if file is too short and clean up *imageCount = (int) buffer[0]; @@ -97,14 +109,33 @@ void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, un // TODO read from file and write in 2d arry, write label in labels array, repeat for imageCount -> done in readImages } -// change this to createMatrix? -void allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) { - series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array - series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image - for (int i = 0; i < imageCount; i++) { - series->images[i].buffer = (unsigned char *) malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series +int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) { + series->count = imageCount; //counts number of images in series + + series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array + if (series->images == NULL) + return 0; + + + series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image + if (series->labels == NULL){ + return 0;} + + for (int i = 0; i < imageCount; i++) { //Reserving Memory for every Images Pixelbuffer and setting width and height + series->images[i].width = width; + series->images[i].height = height; + + series->images[i].buffer = malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series + if (series->images[i].buffer == NULL){ + for (int j = 0; j < i; j++){ + free(series->images[j].buffer); + } + free(series->images); + free(series->labels); + return 0; + } } - + return 1; } \ No newline at end of file