From 6a32226f07028096d4bd913bec728357df2715be Mon Sep 17 00:00:00 2001 From: Bannach Date: Mon, 17 Nov 2025 17:29:25 +0100 Subject: [PATCH] Kommentare --- imageInput.c | 19 +++++++++--------- matrix.c | 56 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/imageInput.c b/imageInput.c index 977367e..3fdbbea 100644 --- a/imageInput.c +++ b/imageInput.c @@ -23,7 +23,7 @@ GrayScaleImageSeries *readImages(const char *path) { FILE* file = fopen(path,"rb"); unsigned int width=0,height=0; - GrayScaleImageSeries *imageSeries = (GrayScaleImageSeries*)malloc(sizeof(GrayScaleImageSeries)); + GrayScaleImageSeries *imageSeries = malloc(sizeof(GrayScaleImageSeries)); if (imageSeries ==NULL) { printf("Error allocating memory for GrayScaleImageSeries\n"); return NULL; @@ -33,33 +33,34 @@ GrayScaleImageSeries *readImages(const char *path) fclose(file); return NULL; } - fread(&imageSeries->count,sizeof(unsigned short),1,file); + fread(&imageSeries->count,sizeof(unsigned short),1,file);//Einlesen der Anzahl an Bildern imageSeries->images = (GrayScaleImage*)malloc(imageSeries->count * sizeof(GrayScaleImage)); - fread(&width,sizeof(unsigned short),1,file); - fread(&height,sizeof(unsigned short),1,file); + fread(&width,sizeof(unsigned short),1,file);//Einlesen der Breite + fread(&height,sizeof(unsigned short),1,file);//Einlesen der Höhe imageSeries->labels = malloc(imageSeries->count * sizeof(unsigned char)); - if(imageSeries->labels == NULL) { + if(imageSeries->labels == NULL) { //Wenn Speicher nicht reservierbar free(imageSeries); fclose(file); return NULL; } for(int i = 0; i < imageSeries->count; i++) { - imageSeries->images[i].width = width; + imageSeries->images[i].width = width; //Bilder Einlesen //Breite und Höhe immer gleich imageSeries->images[i].height = height; - const unsigned int imageSize = height*width; + const unsigned int imageSize = height*width; //Anzahl Pixel insgesamt imageSeries->images[i].buffer = (GrayScalePixelType*) malloc(imageSize*sizeof(GrayScalePixelType)); if(imageSeries->images[i].buffer == NULL) { - free(imageSeries->images); + free(imageSeries->images); //Falls speicher nicht reservierbar 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); + fread(&imageSeries->labels[i],sizeof(unsigned char),1,file);//Einlesen des Labels } fclose(file); diff --git a/matrix.c b/matrix.c index 2c1b1a3..67c4deb 100644 --- a/matrix.c +++ b/matrix.c @@ -19,6 +19,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.rows = rows; matrix.cols = cols; matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols); + if (matrix.buffer == NULL) { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } } return matrix; @@ -72,18 +78,17 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix matrix; - if(matrix1.rows != matrix2.rows ) - { - matrix.buffer=NULL; - matrix.cols = 0; - matrix.rows = 0; - return matrix; - } - if (matrix1.cols < matrix2.cols) { + if (matrix1.cols == 1 && matrix1.rows == matrix2.rows) { //Broadcasting für matrix1 matrix.cols = matrix2.cols; matrix.rows = matrix2.rows; matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); + if (matrix.buffer == NULL) { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } for (int i = 0; i < matrix.rows; i++) { for (int j = 0; j < matrix.cols; j++){ matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols]; @@ -94,11 +99,17 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { return matrix; } - if (matrix1.cols > matrix2.cols) - { + else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows) + { //Broadcasting für matrix2 matrix.cols = matrix1.cols; matrix.rows = matrix1.rows; matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols); + if (matrix.buffer == NULL) { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } for (int i = 0; i < matrix.rows; i++) { for (int j = 0; j < matrix.cols; j++){ matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols]; @@ -108,12 +119,18 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { return matrix; } - else + else if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows) { - + //Falls beide Matrizen die gleichen Dimensionen haben matrix.cols = matrix1.cols; matrix.rows = matrix1.rows; matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + if (matrix.buffer == NULL) { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } 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*matrix2.cols+j]); @@ -121,6 +138,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { } return matrix; } + else { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } } @@ -133,9 +156,15 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { matrix.cols = matrix2.cols; matrix.rows = matrix1.rows; matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + if (matrix.buffer == NULL) { + matrix.buffer=NULL; + matrix.cols = 0; + matrix.rows = 0; + return matrix; + } for (int i = 0; i