diff --git a/CMakeLists.txt b/CMakeLists.txt index 1688010..847c636 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,5 +3,5 @@ project(c_net C) set(CMAKE_C_STANDARD 11) -add_executable(c_net main.c matrix.c) +add_executable(c_net main.c matrix.c image.c) target_link_libraries(c_net m) diff --git a/image.c b/image.c index e7d8862..ceb5d19 100644 --- a/image.c +++ b/image.c @@ -1,25 +1,62 @@ -#pragma once - -#include #include +#include #include "image.h" #include "matrix.h" -Img** import_images(char* image_file_string, char* label_file_string, int number_of_images) { +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) { // create file pointer for the image and label data - FILE* image_file = fopen(image_file_string, 'r'); - FILE* label_file = fopen(label_file_string, 'r'); + FILE* image_file = fopen(image_file_string, "r"); + FILE* label_file = fopen(label_file_string, "r"); // check if the file could be opened if(image_file == NULL || label_file == NULL) { - printf("ERROR: File could not be opened! ()"); + printf("ERROR: File could not be opened! (import_images)"); + exit(1); } + // check magic number of the files + if(validate_files(image_file, label_file)) { + printf("ERROR: File validation failed! (validate_files)"); + exit(1); + } + + + // Jakob Section + + + // allocate memory for the storage of images + Image** images = malloc(sizeof(Image) * number_of_images); + + fclose(image_file); + fclose(label_file); } -void img_print (Img* img) { +void img_print (Image* img) { + //print the image matrix_print(img->pixel_values); //print the number of the image diff --git a/image.h b/image.h index ddd20d5..b80b4d9 100644 --- a/image.h +++ b/image.h @@ -1,10 +1,13 @@ #pragma once +#include "matrix.h" + +#include "matrix.h" typedef struct { Matrix* pixel_values; int image_label; } Image; -Img** import_images(char* image_file_string, char* label_file_string, int number_of_images); -void img_print (Img* img); -void img_free (Img* img); \ No newline at end of file +Image** import_images(char* image_file_string, char* label_file_string, int number_of_images); +void img_print (Image* image); +void img_free (Image* image); \ No newline at end of file diff --git a/main.c b/main.c index 5fdc7bf..82750fe 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,10 @@ #include +#include + #include "matrix.h" -#include +#include "image.h" int main() { + Image** images = import_images("../data/train-images.idx3-ubyte", "../data/train-labels.idx1-ubyte", 20); } \ No newline at end of file