Compare commits

...
8 Commits
3 changed files with 121 additions and 11 deletions

No files matched your search

+39 -1
View File
@@ -7,16 +7,54 @@
#define FILE_HEADER_STRING "__info2_image_file_format__"
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
GrayScaleImage *readSingleImage(size_t widht, size_t height) {
}
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
GrayScaleImageSeries *readImages(const char *path)
{
GrayScaleImageSeries *series = NULL;
unsigned short NumberOfImages;
unsigned short PictureWidth;
unsigned short PictureHeight;
char* verifyWord;
size_t bufferCount = 0;
FILE *file = fopen("mnist_test.info2", "rb"); // opening the file
bufferCount = fread(&verifyWord, 27, 1, file);
if (bufferCount != 1 || verifyWord != FILE_HEADER_STRING) {
strerror("Error: wrong file or unable to read the file!");
// return error code;
}
bufferCount = 0;
bufferCount = fread(&NumberOfImages, 2, 1, file);
if (bufferCount != 1) {
strerror("Error: failed to read number of pictures");
// return error code
}
bufferCount = 0;
bufferCount = fread(&PictureWidth, 2, 1, file);
if (bufferCount != 1) {
strerror("Error: failed to read the picture width!");
// return error code
}
bufferCount = 0;
bufferCount = fread(&PictureHeight, 2, 1, file);
if (bufferCount != 1) {
strerror("Error: failed to read the picture height!");
// return error code
}
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
}
}
+76 -10
View File
@@ -1,35 +1,101 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
int getMatrixIndex(unsigned int cols, unsigned int rowIdx, unsigned int colIdx)
{
return rowIdx * cols + colIdx;
}
void clearMatrix(Matrix *matrix)
Matrix createMatrix(const unsigned int rows, const unsigned int cols)
{
Matrix matrix = {.rows = rows, .cols = cols};
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
return matrix;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)] = value;
}
void clearMatrix(Matrix *matrix)
{
for (unsigned int i = 0; i < matrix->rows; i++)
{
for (unsigned int j = 0; j < matrix->cols; j++)
{
setMatrixAt(UNDEFINED_MATRIX_VALUE, *matrix, i, j);
}
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
return matrix.buffer[getMatrixIndex(matrix.cols, rowIdx, colIdx)];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{
fprintf(stderr, "Fehler: Dimensionen stimmen nicht überein (%ux%u und %ux%u)\n",
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
}
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
// check, ob genügend Speicher vorhanden
if (result.buffer == NULL) {
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
}
for (unsigned int i = 0; i < matrix1.rows; i++)
{
for (unsigned int j = 0; j < matrix1.cols; j++)
{
unsigned int index = getMatrixIndex(matrix1.cols, i, j);
result.buffer[index] = matrix1.buffer[index] + matrix2.buffer[index];
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.rows)
{
printf("Fehler: Spalten von Matrix 1 (%u) entsprechen nicht Zeilen von Matrix 2 (%u)\n",
matrix1.cols, matrix2.rows);
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
}
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
// check, ob genügend Speicher vorhanden
if (result.buffer == NULL) {
return (Matrix){ .rows = 0, .cols = 0, .buffer = NULL };
}
for (unsigned int i = 0; i < matrix1.rows; i++)
{
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
for (unsigned int k = 0; k < matrix1.cols; k++)
{
MatrixType val1 = matrix1.buffer[i * matrix1.cols + k];
MatrixType val2 = matrix2.buffer[k * matrix2.cols + j];
sum += val1 * val2;
}
result.buffer[i * result.cols + j] = sum;
}
}
return result;
}
+6
View File
@@ -6,7 +6,13 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
// Flat Storage: Nur ein Pointer, sorgt für zusammenhängendes array, kein verstreuter Speicher -> bessere Performance
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType* buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);