From 39563aec232fadb5d4b5dd2b2dc3c59d8655647c Mon Sep 17 00:00:00 2001 From: Ben Skuppin Date: Wed, 26 Nov 2025 00:01:50 +0100 Subject: [PATCH] Imageinput bis auf einen Test fertig --- imageInput.c | 60 ++++++++++++++++++++++++++++++---------------------- imageInput.h | 5 ----- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/imageInput.c b/imageInput.c index a581da5..4d0ec1c 100644 --- a/imageInput.c +++ b/imageInput.c @@ -8,23 +8,17 @@ // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei -static void readPictureData(const char *path,const short ptrPicturesCount, const short ptrPictureWidth, const short ptrPictureHeigth) +static void readPictureData(FILE *file, unsigned short *ptrPicturesCount, unsigned short *ptrPictureWidth, unsigned short *ptrPictureHeight) { unsigned int header = strlen(FILE_HEADER_STRING); - FILE *file = fopen(path,"rb"); - if (file == NULL){ - perror("failed open file"); - return; - } - fseek(file, header, SEEK_SET); fread(ptrPicturesCount, sizeof(unsigned short), 1, file); fread(ptrPictureWidth, sizeof(unsigned short), 1, file); fread(ptrPictureHeight, sizeof(unsigned short), 1, file); - fclose(file); + } @@ -32,31 +26,47 @@ static void readPictureData(const char *path,const short ptrPicturesCount, const GrayScaleImageSeries *readImages(const char *path) { unsigned short picturesCount, pictureWidth, pictureHeight; - Matrix labels = creatmatrix(1,picturesCount); - Matrix images = creatmatrix(1,picturesCount); + GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));; + + FILE *file = fopen(path,"rb"); + + if (file == NULL){ + printf("failed open file\n"); + + return NULL; + } + readPictureData(file, &picturesCount, &pictureWidth, &pictureHeight); + + series->labels = (unsigned char*)malloc(picturesCount * sizeof(char)); + series->images = (GrayScaleImage*)malloc(picturesCount * sizeof(GrayScaleImage)); + series->count = picturesCount; + + for(int i = 0; i < picturesCount; i++){ + series->images[i].width = pictureWidth; + series->images[i].height = pictureHeight; + series->images[i].buffer = (unsigned char*)malloc(pictureWidth * pictureHeight*sizeof(unsigned char)); + + fread(series->images[i].buffer, sizeof(unsigned char), pictureWidth * pictureHeight, file); + + fread(&series->labels[i], sizeof(unsigned char), 1, file); + + } + + fclose(file); - GrayScaleImageSeries *series = NULL; return series; } // 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++){ + free(series->images[i].buffer); + } + + free(series->images); + free(series->labels); } - - MatrixChar createMatrix(unsigned int rows, unsigned int cols) -{ - if(rows <= 0 || cols <= 0){ - Matrix matrix = { 0 , 0 , UNDEFINED_MATRIX_VALUE }; - return matrix; - } - - MatrixChar matrix = { rows , cols }; - - matrix.buffer = malloc((sizeof(char)*rows*cols)); - - return matrix; -} \ No newline at end of file diff --git a/imageInput.h b/imageInput.h index 00872ec..1f37857 100644 --- a/imageInput.h +++ b/imageInput.h @@ -17,11 +17,6 @@ typedef struct unsigned int count; } GrayScaleImageSeries; -typedef struct{ - unsigned int rows; - unsigned int cols; - MatrixType *buffer; -}MatrixChar; GrayScaleImageSeries *readImages(const char *path); void clearSeries(GrayScaleImageSeries *series);