From 65da22f5646cddce4cd0e359efffcaeac50bfc21 Mon Sep 17 00:00:00 2001 From: Niko Rost Date: Mon, 24 Nov 2025 14:32:40 +0100 Subject: [PATCH] Started working on ImageInput, didnt finish and doesnt compile yet --- imageInput.c | 61 +++++++++++++++++++++++++---------------------- imageInputTests.c | 7 ++++++ 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/imageInput.c b/imageInput.c index 138e826..f882b67 100644 --- a/imageInput.c +++ b/imageInput.c @@ -31,45 +31,48 @@ 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 *s, const int imageCount, const int width, const int height); +void 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"); - // TODO open binary file from file name - GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); - GrayScaleImage *image = malloc(sizeof(GrayScaleImage)); - series->images = image; - 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 - unsigned int* imageCount = &(series->count); // sets a pointer for int variable count in struct - printf("%d", *imageCount); - unsigned int* width = &(series->images->width); - //printf("%d", *width); - setParameters(file, imageCount, width, &(series->images->height)); - printf("BP3\n"); - allocateMemory(series, *imageCount, series->images->width, series->images->height); - printf("BP4\n"); - //allocateMemoryLabels(); - for(int i = (series->images->width * series->images->height) - 1; i >= 0; i--) - { - fread(&(series->images->buffer[i]), sizeof(GrayScalePixelType), 1, file); + 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)){ + clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary + } + + + + //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 + fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file); + } } fclose(file); - printf("BP5\n"); return series; } // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt void clearSeries(GrayScaleImageSeries *series) { - for(size_t i = series->count - 1; i >= 0; i--) + printf("ClearSeries BP1\n"); + for(int i = series->count - 1; i >= 0; i--) { - free(series->images+series->count*sizeof(GrayScaleImage)*i); + free(series->images+series->count*sizeof(GrayScaleImage)*i);//Muss Falsch sein free(series->labels+series->count*sizeof(unsigned char)*i); } + printf("ClearSeries BP2\n"); series->images = NULL; series->labels = NULL; @@ -78,7 +81,7 @@ void clearSeries(GrayScaleImageSeries *series) series = NULL; } void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters - printf("BP1"); + char buffer[3]; fseek(file, 27, SEEK_SET); @@ -90,18 +93,18 @@ void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, un *width = (int) buffer[1]; *height = (int) buffer[2]; - printf("BP2\n"); - printf("%d\n", *imageCount); -printf("%d\n", *width); -printf("%d\n", *height); // TODO allocate memory for labels array (in imageInput.h) -> Done in allocate Memory // 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* s, const int imageCount, const int width, const int height) { +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++) { - s->images[i].buffer = (unsigned char *) malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series - s->labels = malloc(width * height * sizeof(unsigned char)); + series->images[i].buffer = (unsigned char *) malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series + } + } \ No newline at end of file diff --git a/imageInputTests.c b/imageInputTests.c index 03240ab..e8cebdb 100644 --- a/imageInputTests.c +++ b/imageInputTests.c @@ -39,14 +39,20 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne void test_readImagesReturnsCorrectNumberOfImages(void) { GrayScaleImageSeries *series = NULL; + const unsigned short expectedNumberOfImages = 2; const char *path = "testFile.info2"; prepareImageFile(path, 8, 8, expectedNumberOfImages, 1); + series = readImages(path); + TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count); + clearSeries(series); + printf("Test 1 BP5\n"); remove(path); + printf("Test 1 BP6\n"); } void test_readImagesReturnsCorrectImageWidth(void) @@ -133,6 +139,7 @@ int main() printf("\n============================\nImage input tests\n============================\n"); RUN_TEST(test_readImagesReturnsCorrectNumberOfImages); + printf("BP Check1 complete"); RUN_TEST(test_readImagesReturnsCorrectImageWidth); RUN_TEST(test_readImagesReturnsCorrectImageHeight); RUN_TEST(test_readImagesReturnsCorrectLabels);