From 6d9aa3709a22c691e1c9a0125b7933b515de103b Mon Sep 17 00:00:00 2001 From: Markus Schreiber Date: Wed, 11 Nov 2026 07:12:26 +0100 Subject: [PATCH 1/3] Matrix Funktionen implementiert und getestet --- Startcode/matrix.c | 76 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 0e4ce6e..33ae86d 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -44,24 +44,92 @@ void clearMatrix(Matrix *matrix) matrix->cols = 0; + } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - +if (matrix.buffer == NULL || + rowIdx >= matrix.rows || + colIdx >= matrix.cols) { + return; + } + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + + } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (matrix.buffer == NULL || + rowIdx >= matrix.rows || + colIdx >= matrix.cols) { + return UNDEFINED_MATRIX_VALUE; + } + + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + + if (matrix1.rows != matrix2.rows || + (matrix1.cols != matrix2.cols && + matrix1.cols != 1 && + matrix2.cols != 1)) { + return createMatrix(0, 0); + } + + // Ungültige Eingabematrizen abfangen + if (matrix1.buffer == NULL || matrix2.buffer == NULL) { + return createMatrix(0, 0); + } + + // Größere Spaltenanzahl bestimmen + unsigned int cols = matrix1.cols > matrix2.cols + ? matrix1.cols : matrix2.cols; + + Matrix result = createMatrix(matrix1.rows, cols); + + if (result.buffer == NULL) { + return result; + } + + for (unsigned int i = 0; i < result.rows; i++) { + for (unsigned int j = 0; j < result.cols; j++) { + + // Bei einer Spalte immer Spalte 0 verwenden + unsigned int j1 = matrix1.cols == 1 ? 0 : j; + unsigned int j2 = matrix2.cols == 1 ? 0 : j; + + result.buffer[i * result.cols + j] = + matrix1.buffer[i * matrix1.cols + j1] + + matrix2.buffer[i * matrix2.cols + j2]; + } + } + + return result; + } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - +if (matrix1.cols != matrix2.rows) { + return createMatrix(0, 0); + } + + // Ergebnismatrix erstellen + Matrix result = createMatrix(matrix1.rows, matrix2.cols); + + // Matrizen multiplizieren + for (unsigned int rowIdx = 0; rowIdx < matrix1.rows; rowIdx++) { + for (unsigned int colIdx = 0; colIdx < matrix2.cols; colIdx++) { + for (unsigned int k = 0; k < matrix1.cols; k++) { + result.buffer[rowIdx * result.cols + colIdx] += + matrix1.buffer[rowIdx * matrix1.cols + k] * + matrix2.buffer[k * matrix2.cols + colIdx]; +} +} +} +return result; } \ No newline at end of file From 03d707108abb17fd676912d49a957540132d74a2 Mon Sep 17 00:00:00 2001 From: Markus Schreiber Date: Wed, 11 Nov 2026 07:48:49 +0100 Subject: [PATCH 2/3] ImageInput Funktionen implementiert --- Startcode/imageInput.c | 111 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 3 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index bb30de1..bd8f15a 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -6,14 +6,119 @@ #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" + // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen +static int checkFileHeader(FILE *file) +{ + char buffer[BUFFER_SIZE] = {0}; + + size_t length = strlen(FILE_HEADER_STRING); + + if (length >= BUFFER_SIZE) + { + return 0; + } + + if (fread(buffer, 1, length, file) != length) + { + return 0; + } + + return strcmp(buffer, FILE_HEADER_STRING) == 0; +} GrayScaleImageSeries *readImages(const char *path) { - GrayScaleImageSeries *series = NULL; - - return series; + +FILE *file = fopen(path, "rb"); + +if (file == NULL) { + return NULL; +} +if (!checkFileHeader(file)) +{ + fclose(file); + return NULL; +} +unsigned short numberOfImages; +unsigned short width; +unsigned short height; +if (fread(&numberOfImages, sizeof(unsigned short), 1, file) != 1 || + fread(&width, sizeof(unsigned short), 1, file) != 1 || + fread(&height, sizeof(unsigned short), 1, file) != 1) +{ + fclose(file); + return NULL; +} +if (numberOfImages == 0 || width == 0 || height == 0) +{ + fclose(file); + return NULL; +} +GrayScaleImageSeries *series = + calloc(1, sizeof(GrayScaleImageSeries)); + + +if (series == NULL) +{ + fclose(file); + return NULL; +} + +series->count = numberOfImages; +series->images = + calloc(numberOfImages, sizeof(GrayScaleImage)); + +series->labels = + calloc(numberOfImages, sizeof(unsigned char)); + if (series->images == NULL || series->labels == NULL) +{ + clearSeries(series); + fclose(file); + return NULL; +} +for (unsigned int i = 0; i < numberOfImages; i++) +{ + GrayScaleImage *image = &series->images[i]; + + image->width = width; + image->height = height; + + image->buffer = malloc( + (size_t)width * height * sizeof(GrayScalePixelType) + ); + + if (image->buffer == NULL) + { + clearSeries(series); + fclose(file); + return NULL; + } + + if (fread(image->buffer, + sizeof(GrayScalePixelType), + (size_t)width * height, + file) != (size_t)width * height) + { + clearSeries(series); + fclose(file); + return NULL; + } + + if (fread(&series->labels[i], + sizeof(unsigned char), + 1, + file) != 1) + { + clearSeries(series); + fclose(file); + return NULL; + } +} +fclose(file); +return series; + } // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt From 24d78d14caee8b819e2f7f9942a9460c7eaa9d72 Mon Sep 17 00:00:00 2001 From: Markus Schreiber Date: Wed, 11 Nov 2026 07:59:20 +0100 Subject: [PATCH 3/3] Revert "ImageInput Funktionen implementiert" This reverts commit 03d707108abb17fd676912d49a957540132d74a2. --- Startcode/imageInput.c | 111 ++--------------------------------------- 1 file changed, 3 insertions(+), 108 deletions(-) diff --git a/Startcode/imageInput.c b/Startcode/imageInput.c index bd8f15a..bb30de1 100644 --- a/Startcode/imageInput.c +++ b/Startcode/imageInput.c @@ -6,119 +6,14 @@ #define BUFFER_SIZE 100 #define FILE_HEADER_STRING "__info2_image_file_format__" - // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen -static int checkFileHeader(FILE *file) -{ - char buffer[BUFFER_SIZE] = {0}; - - size_t length = strlen(FILE_HEADER_STRING); - - if (length >= BUFFER_SIZE) - { - return 0; - } - - if (fread(buffer, 1, length, file) != length) - { - return 0; - } - - return strcmp(buffer, FILE_HEADER_STRING) == 0; -} GrayScaleImageSeries *readImages(const char *path) { - -FILE *file = fopen(path, "rb"); - -if (file == NULL) { - return NULL; -} -if (!checkFileHeader(file)) -{ - fclose(file); - return NULL; -} -unsigned short numberOfImages; -unsigned short width; -unsigned short height; -if (fread(&numberOfImages, sizeof(unsigned short), 1, file) != 1 || - fread(&width, sizeof(unsigned short), 1, file) != 1 || - fread(&height, sizeof(unsigned short), 1, file) != 1) -{ - fclose(file); - return NULL; -} -if (numberOfImages == 0 || width == 0 || height == 0) -{ - fclose(file); - return NULL; -} -GrayScaleImageSeries *series = - calloc(1, sizeof(GrayScaleImageSeries)); - - -if (series == NULL) -{ - fclose(file); - return NULL; -} - -series->count = numberOfImages; -series->images = - calloc(numberOfImages, sizeof(GrayScaleImage)); - -series->labels = - calloc(numberOfImages, sizeof(unsigned char)); - if (series->images == NULL || series->labels == NULL) -{ - clearSeries(series); - fclose(file); - return NULL; -} -for (unsigned int i = 0; i < numberOfImages; i++) -{ - GrayScaleImage *image = &series->images[i]; - - image->width = width; - image->height = height; - - image->buffer = malloc( - (size_t)width * height * sizeof(GrayScalePixelType) - ); - - if (image->buffer == NULL) - { - clearSeries(series); - fclose(file); - return NULL; - } - - if (fread(image->buffer, - sizeof(GrayScalePixelType), - (size_t)width * height, - file) != (size_t)width * height) - { - clearSeries(series); - fclose(file); - return NULL; - } - - if (fread(&series->labels[i], - sizeof(unsigned char), - 1, - file) != 1) - { - clearSeries(series); - fclose(file); - return NULL; - } -} -fclose(file); -return series; - + GrayScaleImageSeries *series = NULL; + + return series; } // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt