-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_utils.c
More file actions
55 lines (49 loc) · 1.28 KB
/
Copy pathtrain_utils.c
File metadata and controls
55 lines (49 loc) · 1.28 KB
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
#include "stdlib.h"
#include "train_utils.h"
uint32_t swap(uint32_t x) {
return ((x>>24)&0xff) |
((x>>8)&0xff00) |
((x<<8)&0xff0000) |
((x<<24)&0xff000000);
}
void swapInPlace(uint32_t* arr, int size){
for(int i=0; i<size; i++){
*(arr+i) = swap(*(arr+i));
}
return;
}
void readImgHeader(uint32_t* head, FILE* ptr){
if(fread(head, 4, 4, ptr) != 4){
printf("Error reading the Image header\n");
fclose(ptr); exit(1);
}
swapInPlace(head, 4);
printf("-------------------------\n");
printf("magic number: %d\n", head[0]);
printf("number of images: %d\n", head[1]);
printf("number of rows: %d\n", head[2]);
printf("number of columns: %d\n", head[3]);
printf("-------------------------\n");
return;
}
void readLabelHeader(uint32_t* head, FILE* ptr){
if(fread(head, 4, 2, ptr) != 2){
printf("Error reading the Label header\n");
fclose(ptr); exit(1);
}
swapInPlace(head, 2);
printf("-------------------------\n");
printf("magic number: %d\n", head[0]);
printf("number of images: %d\n", head[1]);
printf("-------------------------\n");
return;
}
int readNextImage(uint8_t* img, int img_size, FILE* ptr){
if(fread(img, 1, img_size, ptr) != img_size){
if(feof(ptr))
return 0;
printf("Error reading images\n");
return 0;
}
return 1;
}