From 8ab69dd403a140d136b64579f5625481a54ada0d Mon Sep 17 00:00:00 2001 From: Hofmann Jonas Date: Thu, 20 Nov 2025 08:27:00 +0100 Subject: [PATCH] added function to check for correct header --- imageInput.c | 74 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 20 deletions(-) diff --git a/imageInput.c b/imageInput.c index e11ebec..2a1b62e 100644 --- a/imageInput.c +++ b/imageInput.c @@ -13,6 +13,7 @@ //void setupMemory(); unsigned int readStatusInfo(FILE *source, char *const headerString, unsigned int *const imageCount, unsigned int *const imageWidth, unsigned int *const imageHeight, int const amountToRead); void readImagedata(FILE *source, unsigned char *const imageBuffer, unsigned char *const labelBuffer,int const imageCount ,int const amountToRead); +unsigned int checkHeaderString(char header[]); // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen @@ -24,17 +25,19 @@ GrayScaleImageSeries *readImages(const char *path) FILE *readSource; unsigned int numberOfBytesToRead = 0; const unsigned int amountOfStatusInfoToRead = 1; - char headerString[sizeof("__info2_image_file_format__\0")] = ""; + unsigned int expectedHeader = 0; + char headerString[sizeof(FILE_HEADER_STRING)] = ""; - + /* char curChar = 0; int i = 0; + */ readSource = fopen(path, "rb"); if (readSource != NULL) { - + /* // print entire source char by char do { @@ -48,22 +51,33 @@ GrayScaleImageSeries *readImages(const char *path) i++; } while (curChar != EOF); // print entire source char by char - + */ series = calloc(sizeof(unsigned int) + 3* sizeof(headerString), amountOfStatusInfoToRead); series->images = calloc(2*sizeof(unsigned int) + sizeof(headerString), amountOfStatusInfoToRead); - numberOfBytesToRead = readStatusInfo(readSource, headerString, &(series->count), &(series->images->width), &(series->images->height), amountOfStatusInfoToRead); + numberOfBytesToRead = readStatusInfo(readSource, headerString, &(series->count), &(series->images->width), &(series->images->height), amountOfStatusInfoToRead); + + + expectedHeader = checkHeaderString(headerString); + + if (expectedHeader) + { + series->images->buffer = calloc((series->count) * numberOfBytesToRead, sizeof(GrayScalePixelType)); + series->labels = calloc((series->count), sizeof(&(series->labels))); + //printf("%d\n", series->images[0].width); + // series->images[1].width is not being set + // implement setting several amoundt of status info + // for when several images are bing read + // also adjust amoung of memory allocated for + // staus info based on the number of images to be read + //printf("%d\n", series->images[1].width); + readImagedata(readSource, series->images->buffer, series->labels, series->count, numberOfBytesToRead); + } + else + { + printf("unexpected header!\n"); + } - series->images->buffer = calloc((series->count) * numberOfBytesToRead, sizeof(GrayScalePixelType)); - series->labels = calloc((series->count), sizeof(&(series->labels))); - printf("%d\n", series->images[0].width); - // series->images[1].width is not being set - // implement setting several amoundt of status info - // for when several images are bing read - // also adjust amoung of memory allocated for - // staus info based on the number of images to be read - printf("%d\n", series->images[1].width); - readImagedata(readSource, series->images->buffer, series->labels, series->count, numberOfBytesToRead); fclose(readSource); } @@ -110,17 +124,18 @@ unsigned int readStatusInfo(FILE *source, char *const headerString, unsigned int { unsigned int bytesToRead = 0; + /* int numred = 0; int numred2 = 0; int numred3 = 0; int numred4 = 0; - + */ //unsigned int imageCountBuffer = 0; - numred = fread(headerString, 27, 1, source); - numred2 = fread(imageCount, 2, 1, source); - numred3 = fread(imageWidth, 2, 1, source); - numred4 = fread(imageHeight, 2, 1, source); + /*numred =*/ fread(headerString, 27, 1, source); + /*numred2 =*/ fread(imageCount, 2, 1, source); + /*numred3 =*/ fread(imageWidth, 2, 1, source); + /*numred4 =*/ fread(imageHeight, 2, 1, source); bytesToRead = (*imageWidth) * (*imageHeight); /* @@ -150,3 +165,22 @@ void readImagedata(FILE *source, unsigned char *const imageBuffer, unsigned char } } +unsigned int checkHeaderString(char header[]) +{ + int i = 0; + int notIdenticall = 0; + char expectedHeader[sizeof(FILE_HEADER_STRING)] = FILE_HEADER_STRING; + + for (i = 0; i < sizeof(FILE_HEADER_STRING); i++) + { + if (header[i] != expectedHeader[i]) + { + notIdenticall = 1; + printf("%c != %C", header[i], expectedHeader[i]); + } + + } + + return !notIdenticall; +} +