-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmploader.h
69 lines (52 loc) · 1.37 KB
/
bmploader.h
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
#ifndef _BMPLOADER
#define _BMPLOADER
#include <stdio.h>
#include <stdlib.h>
typedef struct fileheader {
char id[2];
unsigned int size;
char reserved1[2];
char reserved2[2];
unsigned int offset;
} fileheader;
struct header {
fileheader filehdr;
} header;
void _failnow() {
fprintf(stderr,"invalid file type");
exit(0);
}
FILE* openfile(char* filename) {
FILE* fileptr=fopen(filename,"rb");
if (fileptr==NULL) {
_failnow();
}
return fileptr;
}
void closefile(FILE* fileptr) {
fclose(fileptr);
}
void readfileheader(FILE* fileptr) {
fread(&header.filehdr.id, 2, 1, fileptr);
fread(&header.filehdr.size, 4, 1, fileptr);
fread(&header.filehdr.reserved1, 2, 1, fileptr);
fread(&header.filehdr.reserved2, 2, 1, fileptr);
fread(&header.filehdr.offset, 4, 1, fileptr);
if (!(header.filehdr.id[0] == 'B' && header.filehdr.id[1] == 'M')) {
_failnow();
}
}
void* loadbmp(char *filename) {
FILE* fileptr=openfile(filename);
// read bmp header
readfileheader(fileptr);
long int dibsize = header.filehdr.size - sizeof(fileheader);
void* datablock = calloc(1,dibsize);
fread(datablock,dibsize,1,fileptr);
closefile(fileptr);
return datablock;
}
void freebmp(void* bitmap) {
free(bitmap);
}
#endif