HolyFuckItsAlive #13

Merged
jastornig merged 105 commits from Delta-Error-Test into main 2023-09-23 22:27:54 +02:00
4 changed files with 39 additions and 13 deletions
Showing only changes of commit 2c768550f3 - Show all commits

46
image.c
View file

@ -6,34 +6,58 @@
#include "image.h" #include "image.h"
#include "matrix.h" #include "matrix.h"
int endian_swap(int input) {
return ((input >> 24) & 0xff) | // move byte 3 to byte 0
((input << 8) & 0xff0000) | // move byte 1 to byte 2
((input >> 8) & 0xff00) | // move byte 2 to byte 1
((input << 24) & 0xff000000); // byte 0 to byte 3
}
int validate_files(FILE* image_file, FILE* label_file) {
// read magic number from files
int magic_number_label, magic_number_images;
fread(&magic_number_label, 4, 1, label_file);
fread(&magic_number_images, 4, 1, image_file);
// compare magic numbers with pre-defined value
if(endian_swap(magic_number_label) != 2049 || endian_swap(magic_number_images) != 2051) {
return 0;
}
return 1;
}
Image** import_images(char* image_file_string, char* label_file_string, int number_of_images) { Image** import_images(char* image_file_string, char* label_file_string, int number_of_images) {
// create file pointer for the image and label data // create file pointer for the image and label data
FILE* image_file = fopen(image_file_string, 'r'); FILE* image_file = fopen(image_file_string, "r");
FILE* label_file = fopen(label_file_string, 'r'); FILE* label_file = fopen(label_file_string, "r");
// check if the file could be opened // check if the file could be opened
if(image_file == NULL || label_file == NULL) { if(image_file == NULL || label_file == NULL) {
printf("ERROR: File could not be opened! (import_images)"); printf("ERROR: File could not be opened! (import_images)");
exit(1);
} }
do { // check magic number of the files
ch = fgetc(label_file); if(validate_files(image_file, label_file)) {
printf("%c", ch); printf("ERROR: File validation failed! (validate_files)");
exit(1);
}
// Checking if character is not EOF.
// If it is EOF stop reading. // Jakob Section
} while (ch != EOF);
// allocate memory for the storage of images // allocate memory for the storage of images
// Image** images = malloc(sizeof(Image) * number_of_images); Image** images = malloc(sizeof(Image) * number_of_images);
fclose(image_file); fclose(image_file);
fclose(label_file); fclose(label_file);
} }
void img_print (Img* img) { void img_print (Image* img) {
//print the image //print the image
matrix_print(img->pixel_values); matrix_print(img->pixel_values);
@ -42,6 +66,6 @@ void img_print (Img* img) {
printf("%d", img->image_label); printf("%d", img->image_label);
} }
void img_free (Img* img) { void img_free (Image* img) {
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "matrix.h"
typedef struct { typedef struct {
Matrix* pixel_values; Matrix* pixel_values;
int image_label; int image_label;

2
main.c
View file

@ -5,6 +5,6 @@
#include "image.h" #include "image.h"
int main() { int main() {
Image** images = import_images("train-images.idx3-ubyte", "train-labels.idx3-ubyte", 20); Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", 20);
} }

View file

@ -265,5 +265,5 @@ Matrix* matrix_flatten(Matrix* matrix, int axis) {
else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j]; else if (axis == 1) result_matrix->numbers[0][i * matrix->columns + j] = matrix->numbers[i][j];
} }
} }
return mat; return result_matrix;
} }