Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -12,14 +12,20 @@ static FILE* openImageFile(const char* path) {
|
||||
FILE* imageFile = NULL;
|
||||
imageFile = fopen(path, "rb");
|
||||
|
||||
if (imageFile == NULL) {
|
||||
// Fehler beim Öffnen
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Prüfe, ob die Datei mit dem richtigen String beginnt
|
||||
char *fileHeaderString = (char*)malloc(BUFFER_SIZE);
|
||||
fread(fileHeaderString, 1, (BUFFER_SIZE - 1), imageFile);
|
||||
fread(fileHeaderString, 1, strlen(FILE_HEADER_STRING) + 1, imageFile);
|
||||
|
||||
if (strncmp(fileHeaderString, FILE_HEADER_STRING, 27)) {
|
||||
// Datei hat nicht das korrekte Format
|
||||
// Speicherplatz freigeben
|
||||
free(fileHeaderString);
|
||||
fclose(imageFile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -30,13 +36,16 @@ static FILE* openImageFile(const char* path) {
|
||||
}
|
||||
|
||||
static int getInformationOfImages(FILE* imageFile, int dimensionsOfImages[], GrayScaleImageSeries *series) {
|
||||
// Speicher für die drei Zahlen + Nullterminator (je Zahl 2 Byte)
|
||||
char bytestream[7];
|
||||
bytestream[6] = '\0';
|
||||
// Speicher für die drei Zahlen (je Zahl 2 Byte)
|
||||
unsigned char bytestream[6];
|
||||
|
||||
// Daten einlesen
|
||||
int itemsRead = fread(bytestream, 2, 3, imageFile);
|
||||
|
||||
//for (size_t i = 0; i < (itemsRead * 2); i++) {
|
||||
// printf("%02X ", bytestream[i]);
|
||||
//}
|
||||
|
||||
if (itemsRead != 3) {
|
||||
// Nicht genügend Daten gelesen
|
||||
return -1;
|
||||
@@ -54,6 +63,42 @@ static int getInformationOfImages(FILE* imageFile, int dimensionsOfImages[], Gra
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int getGrayScalePixelOfImage(FILE* imageFile, GrayScaleImage* image) {
|
||||
// SPeicher für Buffer allokieren
|
||||
image->buffer = malloc(image->width * image->height);
|
||||
|
||||
size_t expectedItems = (size_t)image->width * image->height; // Anzahl der Pixel/Bytes
|
||||
|
||||
// Pixelwerte aus Datei auslesen
|
||||
int itemsRead = fread(image->buffer, sizeof(GrayScalePixelType), expectedItems, imageFile);
|
||||
|
||||
if (itemsRead != expectedItems) {
|
||||
free(image->buffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char getLabelOfImage(FILE* imageFile, GrayScaleImage* image) {
|
||||
// Array um Label zu speichern
|
||||
unsigned char *bytestream = malloc(1);
|
||||
|
||||
// Label aus Datei auslesen
|
||||
int itemsRead = fread(bytestream, 1, 1, imageFile);
|
||||
|
||||
if (itemsRead != 1) {
|
||||
free(bytestream);
|
||||
return -1;
|
||||
}
|
||||
|
||||
unsigned char label = bytestream[0];
|
||||
|
||||
free(bytestream);
|
||||
|
||||
return label;
|
||||
}
|
||||
|
||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||
GrayScaleImageSeries *readImages(const char *path)
|
||||
{
|
||||
@@ -62,7 +107,7 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
FILE* imageFile = openImageFile(path);
|
||||
|
||||
if (imageFile == NULL) {
|
||||
printf("Unbekannter Fehler");
|
||||
printf("Unbekannter Fehler, imageFile ist Null");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -75,20 +120,79 @@ GrayScaleImageSeries *readImages(const char *path)
|
||||
// 2) Breite der Bilder --> width
|
||||
// 3) Höhe der Bilder --> height
|
||||
int dimensionsOfImages[2];
|
||||
getInformationOfImages(imageFile, dimensionsOfImages, series);
|
||||
int erfolg = getInformationOfImages(imageFile, dimensionsOfImages, series);
|
||||
|
||||
if (erfolg == -1) {
|
||||
printf("Unbekannter Fehler bei getInformationOfImages.");
|
||||
fclose(imageFile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Speicher für die Labels vorbereiten, wird später Bild für Bild befüllt
|
||||
series->labels = malloc(series->count * sizeof(unsigned char));
|
||||
// labels = unsigned char-Array mit der Größe wie AnzahlBilder
|
||||
|
||||
// Springe in der Datei an die Stelle nach den drei Zahlen
|
||||
if (series->labels == NULL) {
|
||||
printf("Fehler: Speicherzuweisung für labels fehlgeschlagen.\n");
|
||||
free(series);
|
||||
fclose(imageFile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Speicher für das Array von GrayScaleImage-Strukturen vorbereiten
|
||||
series->images = malloc(series->count * sizeof(GrayScaleImage));
|
||||
if (series->images == NULL) {
|
||||
printf("Fehler: Speicherzuweisung für images fehlgeschlagen.\n");
|
||||
free(series->labels); // Zuerst labels freigeben
|
||||
free(series);
|
||||
fclose(imageFile);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (int i = 0; i < series->count; i++) {
|
||||
// image initialisieren
|
||||
GrayScaleImage *image = &series->images[i];
|
||||
// image = (GrayScaleImage *)malloc(sizeof(GrayScaleImage));
|
||||
|
||||
image->width = dimensionsOfImages[0];
|
||||
image->height = dimensionsOfImages[1];
|
||||
image->buffer = NULL;
|
||||
|
||||
// Graustufe-Pixel lesen und im image speichern
|
||||
if(getGrayScalePixelOfImage(imageFile, image) != 0) {
|
||||
printf("Fehler: Konnte Pixel des Bildes %d nicht lesen.\n", i + 1);
|
||||
fclose(imageFile);
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// label lesen und in der series an entsprechender Stelle speichern
|
||||
int labelResult = getLabelOfImage(imageFile, image);
|
||||
if (labelResult == -1) {
|
||||
printf("Fehler: Konnte Label des Bildes %d nicht lesen.\n", i);
|
||||
fclose(imageFile);
|
||||
clearSeries(series);
|
||||
return NULL;
|
||||
}
|
||||
series->labels[i] = (unsigned char)labelResult;
|
||||
|
||||
if (series->images == NULL) {
|
||||
printf("Fehler bei malloc für images");
|
||||
free(series->labels); // Labels freigeben
|
||||
free(series); // Serie freigeben
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// für jedes Bild gemäß GreyScaleImageSeries.count:
|
||||
// GreyScaleImage.width = width
|
||||
// GreyScaleImage.height = height
|
||||
// GreyScaleImageSeries.buffer = Pixelwerte
|
||||
// labels = Label des Bilds
|
||||
// series.labels[i] = (unsigned char)(i % 256);
|
||||
// Springe in der Datei an die Stelle nach dem i. Bild
|
||||
|
||||
// GrayScaleImageSeries.labels = labels (mit strcpy)
|
||||
fclose(imageFile);
|
||||
|
||||
return series;
|
||||
}
|
||||
|
||||
@@ -17,34 +17,75 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
return matrix;
|
||||
}
|
||||
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
if (matrix->data != NULL) {
|
||||
free(matrix->data);
|
||||
free(matrix->data);
|
||||
matrix->data = 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(rowIdx >= matrix->rows || colIdx >= matrix->cols){
|
||||
return;
|
||||
}
|
||||
matrix->data[rowIdx * matrix->cols + colIdx] = value;
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(rowIdx >= matrix.rows || colIdx >= matrix.cols){
|
||||
return 0;
|
||||
}
|
||||
MatrixType value;
|
||||
value = matrix.data[rowIdx * matrix.cols + colIdx];
|
||||
return value;
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
}
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { // Matrixen können nur addiert werden, sofern sie jeweils die gleiche Anzahl Spalten und Zeilen haben
|
||||
Matrix errorMatrix = createMatrix(0, 0);
|
||||
errorMatrix.data = NULL;
|
||||
return errorMatrix;
|
||||
}
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
||||
MatrixType sum = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
|
||||
setMatrixAt(sum, &result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
if (matrix1.cols != matrix2.rows){
|
||||
Matrix errorMatrix = createMatrix(0, 0);
|
||||
errorMatrix.data = NULL;
|
||||
return errorMatrix;
|
||||
}
|
||||
Matrix matrix3 = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
for( size_t i = 0; i < matrix1.rows; i++){
|
||||
for(size_t j = 0; j < matrix2.cols; j++){
|
||||
MatrixType sum = 0;
|
||||
for(size_t k = 0; k < matrix1.cols; k++){
|
||||
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||
}
|
||||
setMatrixAt(sum, &matrix3, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return matrix3;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
typedef struct{
|
||||
size_t rows;
|
||||
size_t cols;
|
||||
float* data;
|
||||
float* buffer;
|
||||
} Matrix;
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user