clearSeries und Kommentare ergänzt

This commit is contained in:
Laila Mueller 2025-11-13 10:48:09 +01:00
parent c8b14cefae
commit 6d162b313c
2 changed files with 34 additions and 8 deletions

View File

@ -195,4 +195,29 @@ GrayScaleImageSeries *readImages(const char *path)
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt // TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series) void clearSeries(GrayScaleImageSeries *series)
{ {
//prüfen, ob series überhaupt bereinigt werden muss
if(series == NULL){
return;
}
//images freigeben und NULL setzen -> jeden Index der Images durchgehen und buffer freigeben
if(series->images != NULL){
for(unsigned int i = 0; i < series->count; i++){
if (series->images[i].buffer != NULL){
free(series->images[i].buffer);
series->images[i].buffer = NULL;
}
}
free(series->images); // wenn alle images bereinigt sind, den Zeiger Images selbst bereinigen
series->images = NULL;
}
//labels freigeben und NULL setzen
if(series->labels != NULL){
free(series->labels);
series->labels = NULL;
}
//series freigeben
free(series);
} }

View File

@ -10,12 +10,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
if(matrix.rows == 0 || matrix.cols == 0){ if(matrix.rows == 0 || matrix.cols == 0){ // prüfen, ob ungültige Dimensionen übergeben wurden, wenn ja, "error matrix" erstellen
matrix.rows = 0; matrix.rows = 0;
matrix.cols = 0; matrix.cols = 0;
matrix.buffer = NULL; matrix.buffer = NULL;
} }
else { else { // für gültige Dimensionen: Speicher allokieren
matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType)); matrix.buffer = (float *)malloc(rows * cols * sizeof(MatrixType));
} }
@ -36,19 +36,19 @@ void clearMatrix(Matrix *matrix)
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return; return;
} }
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // value an vorgebenener Stelle setzen
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ if(rowIdx >= matrix.rows || colIdx >= matrix.cols){ // Gültigkeit der Dimensionen prüfen
return 0; return 0;
} }
MatrixType value; MatrixType value;
value = matrix.buffer[rowIdx * matrix.cols + colIdx]; value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // value an vorgegebener Stelle auslesen
return value; return value;
} }
@ -73,13 +73,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
if (matrix1.cols != matrix2.rows){ if (matrix1.cols != matrix2.rows){ // prüfen, ob Matrizen multipliziert werden dürfen
Matrix errorMatrix = createMatrix(0, 0); Matrix errorMatrix = createMatrix(0, 0);
errorMatrix.buffer = NULL; errorMatrix.buffer = NULL;
return errorMatrix; return errorMatrix;
} }
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols); // Ergebnis-Matrix erstellen
// Algorithmus für Multiplikation
for( size_t i = 0; i < matrix1.rows; i++){ for( size_t i = 0; i < matrix1.rows; i++){
for(size_t j = 0; j < matrix2.cols; j++){ for(size_t j = 0; j < matrix2.cols; j++){
MatrixType sum = 0; MatrixType sum = 0;