-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmc_sdl_bmp.h
221 lines (176 loc) · 4.58 KB
/
mc_sdl_bmp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
extern int _open(const char *pathname, int flags, mode_t mode);
extern off_t _lseek(int fd, off_t offset, int whence);
extern int _close(int fd);
extern ssize_t _read(int fd, void *buf, size_t count);
extern void mc_sleep(void);
typedef uint32_t DWORD;
typedef uint16_t WORD;
typedef int32_t LONG;
typedef int32_t FXPT2DOT30;
typedef struct tagCIEXYZ {
FXPT2DOT30 ciexyzX;
FXPT2DOT30 ciexyzY;
FXPT2DOT30 ciexyzZ;
} CIEXYZ;
typedef struct tagICEXYZTRIPLE {
CIEXYZ ciexyzRed;
CIEXYZ ciexyzGreen;
CIEXYZ ciexyzBlue;
} CIEXYZTRIPLE;
typedef struct {
DWORD bV4Size;
LONG bV4Width;
LONG bV4Height;
WORD bV4Planes;
WORD bV4BitCount;
DWORD bV4V4Compression;
DWORD bV4SizeImage;
LONG bV4XPelsPerMeter;
LONG bV4YPelsPerMeter;
DWORD bV4ClrUsed;
DWORD bV4ClrImportant;
DWORD bV4RedMask;
DWORD bV4GreenMask;
DWORD bV4BlueMask;
DWORD bV4AlphaMask;
DWORD bV4CSType;
CIEXYZTRIPLE bV4Endpoints;
DWORD bV4GammaRed;
DWORD bV4GammaGreen;
DWORD bV4GammaBlue;
} BITMAPV4HEADER;
SDL_Surface *SDL_LoadBMP(const char *path) {
int f = _open(path, 0, O_RDONLY);
if (f < 0) {
printf("error opening file\n");
return 0;
}
uint16_t magic;
if (_read(f, &magic, sizeof(magic)) != sizeof(magic)) {
printf("couldn't read enough for magic\n");
return 0;
}
// magic should be 0x4D42
uint32_t size;
if (_read(f, &size, sizeof(size)) != sizeof(size)) {
printf("couldn't read enough for size\n");
return 0;
}
uint32_t reserved;
if (_read(f, &reserved, sizeof(reserved)) != sizeof(reserved)) {
printf("couldn't read enough for reserved\n");
return 0;
}
uint32_t offset;
if (_read(f, &offset, sizeof(offset)) != sizeof(offset)) {
printf("couldn't read enough for offset\n");
return 0;
}
uint32_t infohdrsize;
if (_read(f, &infohdrsize, sizeof(infohdrsize)) != sizeof(infohdrsize)) {
printf("couldn't read enough for infohdrsize\n");
return 0;
}
if (infohdrsize != 108) {
printf("wrong infohdrsize %d\n", infohdrsize);
return 0;
}
BITMAPV4HEADER bmp_hdr;
void *ptr = ((char*)&bmp_hdr) + 4;
if (_read(f, ptr, 104) != 104) {
printf("Couldn't read enough for bmp_hdr\n");
return 0;
}
mc_sleep();
int num_bytes;
if (bmp_hdr.bV4BitCount == 4) {
num_bytes = (bmp_hdr.bV4Width * bmp_hdr.bV4Height) / 2;
} else if (bmp_hdr.bV4BitCount == 1) {
num_bytes = (bmp_hdr.bV4Width * bmp_hdr.bV4Height) / 8;
}
uint8_t *buf = malloc(num_bytes);
if (!buf) {
printf("couldn't allocate buffer\n");
return 0;
}
if (_lseek(f, offset, SEEK_SET) < 0) {
printf("lseek error\n");
return 0;
}
if (_read(f, buf, num_bytes) != num_bytes) {
printf("couldn't read pixel data\n");
return 0;
}
if (_close(f) < 0) {
printf("couldn't close file\n");
return 0;
}
mc_sleep();
uint8_t *pixels = malloc(bmp_hdr.bV4Width * bmp_hdr.bV4Height);
if (pixels == 0) {
free(buf);
printf("couldn't allocate pixel buffer\n");
return 0;
}
if (bmp_hdr.bV4BitCount == 4) {
for (int y = 0; y < bmp_hdr.bV4Height; ++y) {
mc_sleep();
for (int x = 0; x < bmp_hdr.bV4Width; ++x) {
int offset = (x + bmp_hdr.bV4Width * y) / 2;
int need_lo = x % 2;
uint8_t *dst = &pixels[(bmp_hdr.bV4Height - 1 - y) * bmp_hdr.bV4Width + x];
uint8_t src = buf[offset];
if (need_lo) {
*dst = src & 0xF;
} else {
*dst = src >> 4;
}
}
}
} else if (bmp_hdr.bV4BitCount == 1) {
for (int y = 0; y < bmp_hdr.bV4Height; ++y) {
mc_sleep();
for (int x = 0; x < bmp_hdr.bV4Width; ++x) {
int offset = (x + bmp_hdr.bV4Width * y) / 8;
int need_lo = x % 8;
uint8_t *dst = &pixels[(bmp_hdr.bV4Height - 1 - y) * bmp_hdr.bV4Width + x];
uint8_t src = buf[offset];
*dst = ((src >> (7 - need_lo)) & 0x1);
}
}
} else {
printf("invalid bit count\n");
return 0;
}
free(buf);
mc_sleep();
SDL_Surface *surf = (SDL_Surface*)SDL_malloc(sizeof(SDL_Surface));
if (surf == 0) {
free(buf);
SDL_free(pixels);
return 0;
}
SDL_PixelFormat *format = (SDL_PixelFormat*)SDL_malloc(sizeof(SDL_PixelFormat));
if (format == 0) {
free(buf);
SDL_free(pixels);
SDL_free(surf);
return 0;
}
format->BitsPerPixel = 8;
format->BytesPerPixel = 1;
surf->w = bmp_hdr.bV4Width;
surf->h = bmp_hdr.bV4Height;
surf->clip_rect.x = 0;
surf->clip_rect.y = 0;
surf->clip_rect.w = surf->w;
surf->clip_rect.h = surf->h;
surf->locked = 0;
surf->pixels = pixels;
surf->format = format;
surf->pitch = surf->w * surf->format->BytesPerPixel;
return surf;
}