Compare commits

..

No commits in common. "main" and "matrixMultiply" have entirely different histories.

4 changed files with 19 additions and 229 deletions

View File

@ -6,127 +6,17 @@
#define BUFFER_SIZE 100
#define FILE_HEADER_STRING "__info2_image_file_format__"
int checkString (FILE *file){ //Checks if String at the start of File equals expected Format (0 for wrong 1 for right)
if (file == NULL){ //returns 0 for empty file
return 0;
}
char expectedString[] = FILE_HEADER_STRING;
char actualString[BUFFER_SIZE];
rewind(file);
size_t bytesRead = fread(actualString, 1,27 , file); //Stores Bytes of start of File to actualString
if (bytesRead != 27){
return 0;
} //Returns 0 if File is to short
actualString[27] = '\0';
if (strcmp(actualString, expectedString) != 0){
return 0;
}
else{
return 1;
}
}
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height);
static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height);
// 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
GrayScaleImageSeries *readImages(const char *path)
{
FILE* file = fopen(path, "rb");
if(!(checkString(file))){
fclose(file);
return NULL;
}
unsigned int width, height; //uninitialised Variables, will be set by setparamters
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries)); //Reserving memory for series
setParameters(file, &(series->count), &width, &height); //setting parameters taken from image file
if(!allocateMemory(series, series->count, width, height)){
clearSeries(series); //If Memory couldnt be correctly allocated a clearing is necessary
fclose(file);
return NULL;
}
//Getting image data
for(unsigned int i = 0; i < series->count ; i++){ //Iterating for every image
for(unsigned int j = 0; j < width * height; j++){ //Iterating for every pixel
fread(&(series->images[i].buffer[j]), sizeof(GrayScalePixelType), 1, file);
}
fread(&(series->labels[i]), sizeof(unsigned char), 1, file); // TODO Added writing in labels array
}
fclose(file);
GrayScaleImageSeries *series = NULL;
return series;
}
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
void clearSeries(GrayScaleImageSeries *series)
{
if (!series)
return;
//Freeing Pixelbuffer for every image
for(unsigned int i = 0; i < series->count; i++)
{
free(series->images[i].buffer);
}
free(series->images); //Freeing Images array
free(series->labels); //Freeing Labels
free(series); //Freeing Main Structure
}
static void setParameters(FILE* file, unsigned int* imageCount, unsigned int* width, unsigned int* height) { // sets the parameters
unsigned short buffer[3];
fseek(file, 27, SEEK_SET);
if(fread(buffer, sizeof(unsigned short), 3, file) != 3){ // TODO changed some stuff here, used UNSIGNED SHORT instead of UNSIGNED CHAR!!
*imageCount = 0;
*width = 0;
*height = 0;
return;
} //Stops if file is too short and clean up
*imageCount = (unsigned int) buffer[0];
*width = (unsigned int) buffer[1];
*height = (unsigned int) buffer[2];
}
static int allocateMemory(GrayScaleImageSeries *series, const int imageCount, const int width, const int height) {
series->count = imageCount; //counts number of images in series
series->images = malloc (sizeof(GrayScaleImage) * imageCount); //Reserving Memory for Images Array
if (series->images == NULL)
return 0;
series->labels = malloc (series->count * sizeof(unsigned char)); //One Label for every Image
if (series->labels == NULL){
return 0;}
for (int i = 0; i < imageCount; i++) { //Reserving Memory for every Images Pixelbuffer and setting width and height
series->images[i].width = width;
series->images[i].height = height;
series->images[i].buffer = malloc(width * height * sizeof(unsigned char)); // allocates memory for every image in the series
if (series->images[i].buffer == NULL){
for (int j = 0; j < i; j++){
free(series->images[j].buffer);
}
free(series->images);
free(series->labels);
return 0;
}
}
return 1;
}

View File

@ -39,16 +39,12 @@ static void prepareImageFile(const char *path, unsigned short int width, unsigne
void test_readImagesReturnsCorrectNumberOfImages(void)
{
GrayScaleImageSeries *series = NULL;
const unsigned short expectedNumberOfImages = 2;
const char *path = "testFile.info2";
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
series = readImages(path);
TEST_ASSERT_NOT_NULL(series);
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
clearSeries(series);
remove(path);
}

118
matrix.c
View File

@ -1,134 +1,42 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
typedef struct Matrix {
unsigned int rows;
unsigned int cols;
MatrixType* buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix newMatrix;
if (rows == 0 || cols == 0) {
newMatrix.rows = 0;
newMatrix.cols = 0;
newMatrix.buffer = NULL;
return newMatrix;
}
newMatrix.rows = rows;
newMatrix.cols = cols;
newMatrix.buffer = calloc(rows*cols, sizeof(MatrixType));
return newMatrix;
}
void clearMatrix(Matrix *matrix)
{
free(matrix->buffer);
matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (rowIdx >= matrix.rows || colIdx >= matrix.cols){
printf("Index out of bounds\n"); //Error Message because Index Input exceeds Matrix
}
else{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; //Writes Value of value variable in the selected place in Matrix
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (rowIdx >= matrix.rows || colIdx >= matrix.cols){
printf("Index out of bounds\n");
return 0;
}
else{
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx]; //Stores value of selected place of Matrix in value variable
return value;
}
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//If rows and cols of both Matrixes are the same the normal addition starts
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols){
Matrix MatrixErgebnis = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++){
for (unsigned int j = 0; j < matrix1.cols; j++){
//Adding Matrix Elements of same row and col index together and store in new Matrix
MatrixErgebnis.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix1.cols + j];
}
}
return MatrixErgebnis;
}
//if matrix1 is a Vektor it adds elements to first col of matrix2
else if (matrix1.cols == 1 && matrix1.rows == matrix2.rows){
Matrix MatrixErgebnis = createMatrix(matrix2.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix2.rows; i++) {
for (unsigned int j = 0; j < matrix2.cols; j++) {
//Copy Matrix2 to ErgebnisMatrix
MatrixErgebnis.buffer[i * matrix2.cols + j] = getMatrixAt(matrix2, i, j);
//Adding Elements of first col of Matrix 1 (Vector) to all Elements of MatrixErgebnis
MatrixErgebnis.buffer[i * matrix2.cols + j] += matrix1.buffer[i];
}
}
return MatrixErgebnis;
}
//if matrix2 is a Vektor it adds elements to first col of matrix1
else if (matrix2.cols == 1 && matrix1.rows == matrix2.rows){
Matrix MatrixErgebnis = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix1.cols; j++) {
//Copy Matrix1 to ErgebnisMatrix
MatrixErgebnis.buffer[i * matrix1.cols + j] = getMatrixAt(matrix1, i, j);
//Adding Elements of first col of Matrix 2 (Vector) to first col of MatrixErgebnis
MatrixErgebnis.buffer[i * matrix1.cols + j] += matrix2.buffer[i];
}
}
return MatrixErgebnis;
}
else{
printf("Matrix dimensions do not match\n"); //Error Message if dimensions of Input Matrixes are not identical
Matrix MatrixErgebnis = createMatrix(0, 0);
clearMatrix(&MatrixErgebnis);
return MatrixErgebnis;
}
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
if (matrix1.cols != matrix2.rows) {
return result;
}
result.rows = matrix1.rows;
result.cols = matrix2.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
for(int i = 0; i < result.rows; i++) {
for(int j = 0; j < result.cols; j++) {
MatrixType value = 0;
for(int k = 0; k < matrix1.cols; k++) {
value += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
}
result.buffer[i * result.cols + j] = value;
}
}
return result;
}

View File

@ -7,11 +7,7 @@ typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct Matrix{
unsigned int rows;
unsigned int cols;
MatrixType* buffer;
}Matrix;
typedef struct Matrix;