diff --git a/imageInput.c b/imageInput.c index 8bc6c23..977367e 100644 --- a/imageInput.c +++ b/imageInput.c @@ -21,30 +21,47 @@ int checkHeader(FILE* fileName) //// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen GrayScaleImageSeries *readImages(const char *path) { - FILE* file = fopen(path,"rb"); + FILE* file = fopen(path,"rb"); unsigned int width=0,height=0; - GrayScaleImageSeries *imageSeries = malloc(sizeof(GrayScaleImageSeries)); + GrayScaleImageSeries *imageSeries = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries)); + if (imageSeries ==NULL) { + printf("Error allocating memory for GrayScaleImageSeries\n"); + return NULL; + } imageSeries->count = 0; if(checkHeader(file) == 0) { fclose(file); return NULL; } fread(&imageSeries->count,sizeof(unsigned short),1,file); - imageSeries->images = malloc(imageSeries->count * sizeof(GrayScaleImage)); + imageSeries->images = (GrayScaleImage*)malloc(imageSeries->count * sizeof(GrayScaleImage)); fread(&width,sizeof(unsigned short),1,file); fread(&height,sizeof(unsigned short),1,file); imageSeries->labels = malloc(imageSeries->count * sizeof(unsigned char)); + if(imageSeries->labels == NULL) { + free(imageSeries); + fclose(file); + return NULL; + } + for(int i = 0; i < imageSeries->count; i++) { imageSeries->images[i].width = width; imageSeries->images[i].height = height; const unsigned int imageSize = height*width; - imageSeries->images[i].buffer = malloc(imageSize*sizeof(GrayScalePixelType)); + imageSeries->images[i].buffer = (GrayScalePixelType*) malloc(imageSize*sizeof(GrayScalePixelType)); + if(imageSeries->images[i].buffer == NULL) { + free(imageSeries->images); + free(imageSeries); + fclose(file); + return NULL; + } for (int j=0;jimages[i].buffer[j],sizeof(GrayScalePixelType),1,file); } fread(&imageSeries->labels[i],sizeof(unsigned char),1,file); } + fclose(file); return imageSeries; } @@ -52,6 +69,10 @@ 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) { + for (int i = 0; i < series->count; i++) + { + free(series->images[i].buffer); + } free(series->images); free(series->labels); free(series); diff --git a/matrix.c b/matrix.c index ed849dc..5c7c0f2 100644 --- a/matrix.c +++ b/matrix.c @@ -70,7 +70,18 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - + + //Prüfung ob größe Matrix1 und matrix2 identisch + Matrix matrix; + matrix.cols = matrix1.cols; + matrix.rows = matrix1.rows; + matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + for (int i = 0; i < matrix1.rows; i++) { + for (int j = 0; j < matrix1.cols; j++) { + (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix.cols+j]); + } + } + return matrix; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) diff --git a/neuralNetworkTests.c b/neuralNetworkTests.c index f5aa4be..477e342 100644 --- a/neuralNetworkTests.c +++ b/neuralNetworkTests.c @@ -8,6 +8,7 @@ #define IDENTIFICATION_TAG "__info2_neural_network_file_format__" static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn) { + } void test_loadModelReturnsCorrectNumberOfLayers(void)