summaryrefslogtreecommitdiff
path: root/OASIS/c/src/ekd_cv.c
blob: 1f9afad6fddbdbb31a0f2ccbf282ba17c27a9199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "ekd_cv.h"

void read_bmp(char* filename, uint8_t** data) {
    bmp_header_t header;
    bmp_info_t info;

    FILE* file = fopen(filename, "rb");
    if (file == NULL) {
        printf("Unable to open file.\n");
        return;
    }

    fread(&header.signature, sizeof(uint16_t), 1, file);
    fread(&header.file_size, sizeof(uint32_t), 1, file);
    fread(&header.reserved, sizeof(uint32_t), 1, file);
    fread(&header.data_offset, sizeof(uint32_t), 1, file);

    fread(&info.size, sizeof(uint32_t), 1, file);
    fread(&info.width, sizeof(uint32_t), 1, file);
    fread(&info.height, sizeof(uint32_t), 1, file);
    fread(&info.planes, sizeof(uint16_t), 1, file);
    fread(&info.bit_depth, sizeof(uint16_t), 1, file);
    fread(&info.compression, sizeof(uint32_t), 1, file);
    fread(&info.image_size, sizeof(uint32_t), 1, file);
    fread(&info.x_pixels_per_meter, sizeof(uint32_t), 1, file);
    fread(&info.y_pixels_per_meter, sizeof(uint32_t), 1, file);
    fread(&info.colours_used, sizeof(uint32_t), 1, file);
    fread(&info.important_colours, sizeof(uint32_t), 1, file);

    uint8_t* raw_data = (uint8_t*) malloc(info.width * info.height * sizeof(uint8_t));

    fseek(file, header.data_offset, SEEK_SET);
    fread(raw_data, sizeof(uint8_t), info.width * info.height, file);

    fclose(file);

    for (uint8_t i=0; i<info.height; i++) {
        for (uint8_t j=0; j < info.width; j++) data[i][j] = raw_data[(i * info.width) + j];
    }

    free(raw_data);
}

void read_dir(char* dneg_path, char* dpos_path, char** file_name_arr) {
    DIR* dneg_dir = opendir(dneg_path);
    DIR* dpos_dir = opendir(dpos_path);
    struct dirent *entry;
    uint8_t item_id = 0; 

    while ((entry = readdir(dneg_dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        strcat(file_name_arr[item_id], entry->d_name);
        item_id++;
    }

    while ((entry = readdir(dpos_dir)) != NULL) {
        if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
            continue;
        }
        strcat(file_name_arr[item_id], entry->d_name);
        item_id++;
    }
 
    closedir(dneg_dir);
    closedir(dpos_dir);
}

void flatten_image(uint8_t** image, float* image_flattened) {
    for (uint8_t i=0; i<IMAGE_HEIGHT; i++) {
        for (uint8_t j=0; j<IMAGE_WIDTH; j++) image_flattened[(i*IMAGE_WIDTH)+j] = (float)image[i][j];
    }
}

void shuffle_data_labels(float** data, int32_t* labels, uint8_t num_images) {
    srand(time(NULL));  // initialize random seed
    for (int i = num_images - 1; i > 0; i--) {
        // Generate a random index between 0 and i (inclusive)
        int j = rand() % (i + 1);

        // Swap the rows of data_flatten and labels at indices i and j
        float* temp_data = data[i];
        data[i] = data[j];
        data[j] = temp_data;
        int32_t temp_label = labels[i];
        labels[i] = labels[j];
        labels[j] = temp_label;
    }
}