Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4233a7c40b |
@@ -5,44 +5,9 @@
|
|||||||
|
|
||||||
#define BUFFER_SIZE 100
|
#define BUFFER_SIZE 100
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
#define HEADER_LEN (sizeof(FILE_HEADER_STRING) - 1) //-1, um \0 Character rauszufiltern
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|
||||||
static int check(GrayScaleImage imageData){
|
|
||||||
|
|
||||||
int ok = 0;
|
|
||||||
|
|
||||||
if(imageData.width == 10 && imageData.height == 10 && imageData.buffer <= BUFFER_SIZE)
|
|
||||||
return ok = 1;
|
|
||||||
|
|
||||||
else
|
|
||||||
return ok = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int checkHeader(const GrayScalePixelType *buffer){
|
|
||||||
|
|
||||||
int ok = 0;
|
|
||||||
char header[HEADER_LEN + 1]; //Array mit Platz für Header Länge + \0 Character -> String
|
|
||||||
|
|
||||||
// Jeder Byte in String kopieren
|
|
||||||
for(int i = 0; i < HEADER_LEN; i++){
|
|
||||||
|
|
||||||
header[i] = buffer[i];
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
header[HEADER_LEN] = '\0'; // Nullterminator am Ende anhängen
|
|
||||||
|
|
||||||
if(strcmp(header, FILE_HEADER_STRING) == 0) // wenn beide identische sind gibt cmp 0 zurück
|
|
||||||
return ok = 1;
|
|
||||||
|
|
||||||
else
|
|
||||||
return ok = 0;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,8 +5,7 @@
|
|||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
enum addModes{SAMEDIMENSIONS, COLVEC, ROWVEC};
|
enum addModes{sameDimensions, colVec, rowVec};
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
@@ -17,6 +16,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
|
|
||||||
if(rows == 0 || cols == 0)
|
if(rows == 0 || cols == 0)
|
||||||
{
|
{
|
||||||
|
|
||||||
m.rows = 0;
|
m.rows = 0;
|
||||||
m.cols = 0;
|
m.cols = 0;
|
||||||
m.buffer = NULL;
|
m.buffer = NULL;
|
||||||
@@ -27,6 +27,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
|
|
||||||
if (m.buffer == NULL)
|
if (m.buffer == NULL)
|
||||||
{
|
{
|
||||||
|
|
||||||
m.rows = 0;
|
m.rows = 0;
|
||||||
m.cols = 0;
|
m.cols = 0;
|
||||||
m.buffer = NULL;
|
m.buffer = NULL;
|
||||||
@@ -36,16 +37,14 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (matrix->buffer == NULL)
|
if (matrix->buffer == NULL)
|
||||||
{
|
{
|
||||||
matrix->rows = 0;
|
matrix->rows = 0;
|
||||||
matrix->cols = 0;
|
matrix->cols = 0;
|
||||||
matrix->buffer = NULL;
|
matrix->buffer = NULL;
|
||||||
return;
|
return matrix;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -59,9 +58,9 @@ 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(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -72,7 +71,6 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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)
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
@@ -83,54 +81,55 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int get_add_mode(Matrix matrix1, Matrix matrix2) {
|
||||||
|
|
||||||
static int get_add_mode(Matrix matrix1, Matrix matrix2)
|
int get_add_mode;
|
||||||
{
|
|
||||||
int get_add_mode = -1;
|
|
||||||
|
|
||||||
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
|
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||||
{
|
|
||||||
get_add_mode = SAMEDIMENSIONS;
|
|
||||||
}
|
|
||||||
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows)
|
|
||||||
{
|
|
||||||
get_add_mode = COLVEC;
|
|
||||||
}
|
|
||||||
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows)
|
|
||||||
{
|
|
||||||
get_add_mode = COLVEC;
|
|
||||||
}
|
|
||||||
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols)
|
|
||||||
{
|
|
||||||
get_add_mode = ROWVEC;
|
|
||||||
}
|
|
||||||
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols)
|
|
||||||
{
|
|
||||||
get_add_mode = ROWVEC;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
get_add_mode = sameDimensions;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows){
|
||||||
|
|
||||||
|
get_add_mode = colVec;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows){
|
||||||
|
|
||||||
|
get_add_mode = colVec;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols){
|
||||||
|
|
||||||
|
get_add_mode = rowVec;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols){
|
||||||
|
|
||||||
|
get_add_mode = rowVec;
|
||||||
|
|
||||||
|
}
|
||||||
return get_add_mode;
|
return get_add_mode;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
|
||||||
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
|
{
|
||||||
{
|
|
||||||
Matrix matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
Matrix matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
||||||
{
|
|
||||||
matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||||
}
|
|
||||||
|
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix matrix_erg;
|
Matrix matrix_erg;
|
||||||
|
|
||||||
if(matrix1.cols == 1)
|
if(matrix1.cols == 1)
|
||||||
{
|
{
|
||||||
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
@@ -138,12 +137,12 @@ Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
|||||||
for(int i = 0; i < matrix2.rows; i++)
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0; j < matrix2.cols; j++)
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
{
|
|
||||||
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i * matrix2.cols + j] + matrix2.buffer[i];
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
if(matrix2.cols == 1)
|
if(matrix2.cols == 1)
|
||||||
{
|
{
|
||||||
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
@@ -151,20 +150,18 @@ Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
|||||||
for(int i = 0; i < matrix1.rows; i++)
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0; j < matrix1.cols; j++)
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
{
|
|
||||||
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix1.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
}
|
||||||
{
|
|
||||||
|
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
Matrix matrix_erg;
|
Matrix matrix_erg;
|
||||||
|
|
||||||
if(matrix1.rows == 1)
|
if(matrix1.rows == 1)
|
||||||
{
|
{
|
||||||
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
@@ -172,12 +169,12 @@ Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
|||||||
for(int i = 0; i < matrix2.rows; i++)
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0; j < matrix2.cols; j++)
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
{
|
|
||||||
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[j] + matrix2.buffer[i * matrix2.cols + j];
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i * matrix2.cols + j] + matrix2.buffer[j];
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
if(matrix2.rows == 1)
|
if(matrix2.rows == 1)
|
||||||
{
|
{
|
||||||
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
@@ -185,48 +182,58 @@ Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
|||||||
for(int i = 0; i < matrix1.rows; i++)
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
{
|
{
|
||||||
for(int j = 0; j < matrix1.cols; j++)
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
{
|
|
||||||
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[j] + matrix2.buffer[i * matrix1.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
||||||
{
|
|
||||||
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
|
{
|
||||||
int ok = get_add_mode(matrix1,matrix2);
|
int ok = get_add_mode(matrix1,matrix2);
|
||||||
|
|
||||||
Matrix matrix_erg = createMatrix(0, 0);
|
Matrix matrix_erg = createMatrix(0, 0);
|
||||||
|
|
||||||
|
|
||||||
switch(ok)
|
switch(ok)
|
||||||
{
|
{
|
||||||
case SAMEDIMENSIONS:
|
|
||||||
matrix_erg = addSameDim(matrix1, matrix2);
|
|
||||||
|
case sameDimensions:
|
||||||
|
|
||||||
|
addSameDim(matrix1, matrix2);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case COLVEC:
|
case colVec:
|
||||||
matrix_erg = addColVec(matrix1, matrix2);
|
|
||||||
|
addColVec(matrix1, matrix2);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ROWVEC:
|
case rowVec:
|
||||||
matrix_erg = addRowVec(matrix1, matrix2);
|
|
||||||
|
addRowVec(matrix1, matrix2);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
||||||
{
|
{
|
||||||
int can_multiply = 0;
|
int can_multiply = 0;
|
||||||
|
|
||||||
if(matrix1.cols == matrix2.rows)
|
if(matrix1.cols == matrix2.rows)
|
||||||
{
|
|
||||||
can_multiply = 1;
|
can_multiply = 1;
|
||||||
}
|
|
||||||
|
|
||||||
return can_multiply;
|
return can_multiply;
|
||||||
}
|
}
|
||||||
@@ -241,7 +248,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
if (ok == 1)
|
if (ok == 1)
|
||||||
{
|
{
|
||||||
matrix_erg = createMatrix(erg_rows, erg_cols);
|
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
for (int i = 0; i < erg_rows; i++)
|
for (int i = 0; i < erg_rows; i++)
|
||||||
{
|
{
|
||||||
@@ -251,15 +258,15 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
for (int k = 0; k < matrix1.cols; k++)
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
{
|
{
|
||||||
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
sum += getMatrixAt(matrix1, k, j) * getMatrixAt(matrix2, j, k);
|
||||||
}
|
}
|
||||||
|
|
||||||
setMatrixAt(sum, matrix_erg, i, j);
|
setMatrixAt(sum, matrix_erg, i, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user