diff --git a/imageInput.c b/imageInput.c index d8ad4f2..dd3bba6 100644 --- a/imageInput.c +++ b/imageInput.c @@ -10,15 +10,11 @@ static void readPictureData(FILE *file, unsigned short *ptrPicturesCount, unsigned short *ptrPictureWidth, unsigned short *ptrPictureHeight) { - unsigned int header = strlen(FILE_HEADER_STRING); - - 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); - } @@ -27,28 +23,39 @@ GrayScaleImageSeries *readImages(const char *path) { unsigned short picturesCount, pictureWidth, pictureHeight; GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); - char line[26]; + char line[strlen(FILE_HEADER_STRING)+1]; FILE *file = fopen(path,"rb"); + //Prüfen auf erfolgreiches Öffnen der Datei + if (file == NULL){ printf("failed open file\n"); return NULL; } - fread(line, sizeof(line), 1, file); + //Prüfen auf übereinstimmung des headers + + fread(line,1, strlen(FILE_HEADER_STRING), file); + + line[strlen(FILE_HEADER_STRING)] = '\0'; + if(strncmp(line, FILE_HEADER_STRING, strlen(FILE_HEADER_STRING))!=0)return NULL; - fseek(file, 0, SEEK_SET); + //Anzahl, Breite und Höhe der Bilder einlesen + readPictureData(file, &picturesCount, &pictureWidth, &pictureHeight); + //Speicher für lables und images zuornden + series->labels = (unsigned char*)malloc(picturesCount * sizeof(char)); series->images = (GrayScaleImage*)malloc(picturesCount * sizeof(GrayScaleImage)); series->count = picturesCount; + //speicher reservierung für images und Einlesen der Bilddaten und labels for(int i = 0; i < picturesCount; i++){ series->images[i].width = pictureWidth; series->images[i].height = pictureHeight; @@ -74,4 +81,5 @@ void clearSeries(GrayScaleImageSeries *series) free(series->images); free(series->labels); + free(series); } diff --git a/matrix.c b/matrix.c index 2d33b96..04df5e8 100644 --- a/matrix.c +++ b/matrix.c @@ -7,11 +7,11 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { if(rows <= 0 || cols <= 0){ - Matrix matrix = { 0 , 0 , UNDEFINED_MATRIX_VALUE }; + Matrix matrix = { NULL , 0 , UNDEFINED_MATRIX_VALUE }; return matrix; } - Matrix matrix = { rows , cols }; + Matrix matrix = { 0 ,rows , cols }; matrix.buffer = malloc((sizeof(MatrixType)*rows*cols)); @@ -47,6 +47,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { int matrixResultCols; + //abgleichen welche matrix die größere spalten anzahl besitzt if(matrix1.cols <= matrix2.cols){ matrixResultCols = matrix2.cols; } @@ -54,8 +55,10 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) matrixResultCols = matrix1.cols; } + //Matrix für das ergebnis generieren Matrix result = createMatrix(matrix1.rows, matrixResultCols); + //prüfen auf gleiche Reihenanzahl if(matrix1.rows != matrix2.rows){ result.buffer = NULL; @@ -64,6 +67,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return result; } + //Addition bei gleicher spalten Anzahl if(matrix1.cols == matrix2.cols){ for(int i = 0; i < matrix1.rows; i++){ for(int j = 0; j < matrix1.cols; j++){ @@ -72,6 +76,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } } + //Addition wenn matrix1 nur eine Spalte hat if((matrix1.cols ==1 && matrix2.cols != 1)){ for(int i = 0; i < matrix2.rows; i++){ for(int j = 0; j < matrix2.cols; j++){ @@ -80,6 +85,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) } } + //Addition wenn matrix2 nur eine Spalte hat if((matrix2.cols == 1 && matrix1.cols != 1)){ for(int i = 0; i < matrix1.rows; i++){ for(int j = 0; j < matrix1.cols; j++){ @@ -94,7 +100,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { if (matrix1.cols != matrix2.rows) { - Matrix invalid = {0,0, NULL}; + Matrix invalid = {NULL ,0, 0}; return invalid; } diff --git a/matrix.h b/matrix.h index 49c981d..e65f9e6 100644 --- a/matrix.h +++ b/matrix.h @@ -6,9 +6,9 @@ typedef float MatrixType; typedef struct{ + MatrixType *buffer; unsigned int rows; unsigned int cols; - MatrixType *buffer; }Matrix; // TODO Matrixtyp definieren