9 Commits
Author SHA1 Message Date
urbanjo98912 2f6a3a4391 fix Speicherfreigabe im Fehlerfall 2025-11-02 22:45:32 +01:00
urbanjo98912 e60586a42b fix InputTest 2025-11-02 22:36:55 +01:00
urbanjo98912 7dd22e8319 readImages fix 2025-11-02 22:08:21 +01:00
urbanjo98912 e4d2215465 clearSeries completed 2025-11-02 22:01:01 +01:00
urbanjo98912 c58801f245 readImages completed 2025-11-02 21:11:56 +01:00
urbanjo98912 12fd42ce7d matrix.c & matrix.h completed 2025-10-29 22:01:22 +01:00
urbanjo98912 d479ed4000 Merge branch 'timo' 2025-10-27 15:09:28 +01:00
urbanjo98912 a70da6d7ca delete folder 2025-10-27 15:06:42 +01:00
hertelti100229 695f0c087c Code sichten 2025-10-14 18:43:23 +02:00
6 changed files with 200 additions and 7 deletions

No files matched your search

+102 -1
View File
@@ -8,15 +8,116 @@
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
/* ---------------- Hilfsfunktionen ---------------- */
static int readHeader(FILE *file, unsigned short *count, unsigned short *width, unsigned short *height)
{
unsigned short headerlength = strlen(FILE_HEADER_STRING);
char buffer[headerlength + 1];
if (fread(buffer, 1, headerlength, file) != headerlength)
return 0;
buffer[headerlength] = '\0';
if (strcmp(buffer, FILE_HEADER_STRING) != 0)
return 0;
if (fread(count, sizeof(unsigned short), 1, file) != 1)
return 0;
if (fread(width, sizeof(unsigned short), 1, file) != 1)
return 0;
if (fread(height, sizeof(unsigned short), 1, file) != 1)
return 0;
return 1;
}
static int readSingleImage(FILE *file, GrayScaleImage *image)
{
unsigned int pixelCount = image->width * image->height;
if (fread(image->buffer, sizeof(GrayScalePixelType), pixelCount, file) != pixelCount)
return 0;
return 1;
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen // TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path) GrayScaleImageSeries *readImages(const char *path)
{ {
GrayScaleImageSeries *series = NULL; FILE *file = fopen(path, "rb");
if (!file)
return NULL;
unsigned short count, width, height;
if (!readHeader(file, &count, &width, &height)) {
fclose(file);
return NULL;
}
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
series->count = count;
series->images = malloc(count * sizeof(GrayScaleImage));
series->labels = malloc(count * sizeof(unsigned char));
if (!series || !series->images || !series->labels)
{
free(series->images);
free(series->labels);
free(series);
fclose(file);
return NULL;
}
for (unsigned int i = 0; i < count; i++)
{
series->images[i].width = width;
series->images[i].height = height;
series->images[i].buffer = malloc(width * height * sizeof(GrayScalePixelType));
//malloc prüfen
if (!series->images[i].buffer)
{
clearSeries(series);
fclose(file);
return NULL;
}
//Image einlesen + prüfen
if (!readSingleImage(file, &series->images[i])) {
fclose(file);
clearSeries(series);
return NULL;
}
// label einlesen
if (fread(&series->labels[i], sizeof(unsigned char), 1, file) != 1) {
fclose(file);
clearSeries(series);
return NULL;
}
}
fclose(file);
return series; return series;
} }
// 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)
{ {
if(series)
{
for(unsigned int i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
}
free(series->images);
free(series->labels);
free(series);
}
} }
@@ -54,7 +54,7 @@ void test_readImagesReturnsCorrectImageWidth(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedWidth = 10; const unsigned short expectedWidth = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, 8, expectedWidth, 2, 1); prepareImageFile(path, expectedWidth, 8, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);
@@ -70,7 +70,7 @@ void test_readImagesReturnsCorrectImageHeight(void)
GrayScaleImageSeries *series = NULL; GrayScaleImageSeries *series = NULL;
const unsigned short expectedHeight = 10; const unsigned short expectedHeight = 10;
const char *path = "testFile.info2"; const char *path = "testFile.info2";
prepareImageFile(path, expectedHeight, 8, 2, 1); prepareImageFile(path, 8, expectedHeight, 2, 1);
series = readImages(path); series = readImages(path);
TEST_ASSERT_NOT_NULL(series); TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_NOT_NULL(series->images); TEST_ASSERT_NOT_NULL(series->images);
+83 -1
View File
@@ -6,30 +6,112 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m;
if (rows == 0 || cols == 0)
{
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
m.rows = rows;
m.cols = cols;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix != NULL)
{
if (matrix->buffer != NULL)
{
free(matrix->buffer);
matrix->buffer = NULL;
}
matrix->rows = 0;
matrix->cols = 0;
}
} }
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 (matrix.buffer != NULL)
{
if (rowIdx < matrix.rows || colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
}
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) 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) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
for (int k = 0; k < matrix1.cols; k++)
{
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, result, i, j);
}
}
return result;
} }
+7
View File
@@ -7,6 +7,13 @@ typedef float MatrixType;
// TODO Matrixtyp definieren // TODO Matrixtyp definieren
typedef struct
{
int rows;
int cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);
Submodule I2_Neuronales_Netz deleted from ff67cfdff5.
+6 -2
View File
@@ -1,9 +1,13 @@
#include "input.h" #include "input.h"
#include <string.h>
#include <ctype.h> #include <ctype.h>
#include <string.h>
// TODO: // TODO:
// eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei (words.txt) einliest und als C-String zurückgibt. // eine Funktion implementieren, die ein einzelnes Wort aus einer Textdatei
// (words.txt) einliest und als C-String zurückgibt.
// Bedingungen, die in main.c geprüft werden:
// - existiert die angegebene Datei
// Read words from file and store in 'words' array // Read words from file and store in 'words' array
int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount) int readWords(FILE *file, char words[][MAX_WORD_LEN], unsigned int maxWordCount)