Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbmp.cpp
More file actions
399 lines (391 loc) · 14.3 KB
/
Copy pathbmp.cpp
File metadata and controls
399 lines (391 loc) · 14.3 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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#include "bmp.h"
uint32_t *bmp::decode(char *data, int32_t &width, int32_t &height)
{
char *orig=data;
if(io::readUInt16(data)!=0x4D42)
return 0;
fs_t size=io::readUInt32(data);
data+=4;
fs_t pixelDataPos=io::readUInt32(data);
fs_t headerSize=io::readUInt32(data);
width=io::readUInt32(data);
height=io::readUInt32(data);
bool upsideDown=height>0;
if(!upsideDown)
height*=-1;
uint16_t colorPlaneCount=io::readUInt16(data);
uint16_t bitsPerPixel=io::readUInt16(data);
uint32_t compressionMethod=io::readUInt32(data);
uint32_t imageSize=io::readUInt32(data);
int32_t horResInPixelsPerMeter=io::readUInt32(data);
int32_t verResInPixelsPerMeter=io::readUInt32(data);
uint32_t paletteColors=io::readUInt32(data);
data+=4;
uint32_t rowSize=(uint32_t)floor((double)(bitsPerPixel*width+31)/32)*4;
// If bitsPerPixel==16: actual masks; shift values stored in *Shift16.
uint32_t redShift=compressionMethod==BMP_COMPRESSION_BITFIELDS?(bitsPerPixel==16?io::readUInt32(data):((uint32_t)ceil((double)log2((double)io::readUInt32(data)))/8))-1:2; // Get bit offset out of this number
uint32_t greenShift=compressionMethod==BMP_COMPRESSION_BITFIELDS?(bitsPerPixel==16?io::readUInt32(data):((uint32_t)ceil((double)log2((double)io::readUInt32(data)))/8))-1:1;
uint32_t blueShift=compressionMethod==BMP_COMPRESSION_BITFIELDS?(bitsPerPixel==16?io::readUInt32(data):((uint32_t)ceil((double)log2((double)io::readUInt32(data)))/8))-1:0;
uint32_t redShift16,greenShift16,blueShift16;
uint8_t redMax16,greenMax16,blueMax16;
if(bitsPerPixel==16)
{
redShift16=(uint32_t)ceil((double)log2((double)redShift));
greenShift16=(uint32_t)ceil((double)log2((double)greenShift));
blueShift16=(uint32_t)ceil((double)log2((double)blueShift));
if(redShift16>greenShift16)
{
if(redShift16>blueShift16)
{
if(greenShift16>blueShift16)
{
// redShift16>greenShift16>blueShift16
redShift16=greenShift16;
greenShift16=blueShift16;
blueShift16=0;
}
else
{
// redShift16>blueShift16>greenShift16
redShift16=blueShift16;
blueShift16=greenShift16;
greenShift16=0;
}
}
else
{
// blueShift16>redShift16>greenShift16
blueShift16=redShift16;
redShift16=greenShift16;
greenShift16=0;
}
}
else if(blueShift16>greenShift16)
{
// blueShift16>greenShift16>redShift16
blueShift16=greenShift16;
greenShift16=redShift16;
redShift16=0;
}
else
{
if(redShift16>blueShift16)
{
// greenShift16>redShift16>blueShift16
greenShift16=redShift16;
redShift16=blueShift16;
blueShift16=0;
}
else
{
// greenShift16>blueShift16>redShift16
greenShift16=blueShift16;
blueShift16=redShift16;
redShift16=0;
}
}
redMax16=(uint32_t)redShift>>redShift16;
greenMax16=(uint32_t)greenShift>>greenShift16;
blueMax16=(uint32_t)blueShift>>blueShift16;
}
uint32_t *out=(uint32_t*)malloc(width*height*4);
uint32_t *colorTable=0;
if(bitsPerPixel<16)
{
if(paletteColors==0)
paletteColors=(uint32_t)pow(2.0,bitsPerPixel);
// Color table mandatory if bitsPerPixel<16
colorTable=(uint32_t*)malloc(4*paletteColors);
memcpy(colorTable,data,4*paletteColors);
}
data=orig+pixelDataPos;
if(compressionMethod==BMP_COMPRESSION_RGB||compressionMethod==BMP_COMPRESSION_BITFIELDS)
{
if(bitsPerPixel==1)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
out[(height-y-1)*width+x]=0xFF000000|colorTable[((uint8_t)data[(uint32_t)(y*rowSize+(uint32_t)floor((float)x/8))]&(1<<(7-(x%8))))>0];
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
out[y*width+x]=0xFF000000|colorTable[((uint8_t)data[(uint32_t)(y*rowSize+(uint32_t)floor((float)x/8))]&(1<<(7-(x%8))))>0];
}
}
}
}
else if(bitsPerPixel==4)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
out[(height-y-1)*width+x]=0xFF000000|colorTable[(((uint8_t)data[(uint32_t)(y*rowSize+(uint32_t)floor((float)x/2))]&(0xF<<(((x+1)%2)*4))))>>(((x+1)%2)*4)];
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
out[y*width+x]=0xFF000000|colorTable[(((uint8_t)data[(uint32_t)(y*rowSize+(uint32_t)floor((float)x/2))]&(0xF<<(((x+1)%2)*4))))>>(((x+1)%2)*4)];
}
}
}
}
else if(bitsPerPixel==8)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint8_t pixel=(uint8_t)data[y*rowSize+x];
out[(height-y-1)*width+x]=0xFF000000|colorTable[pixel];
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t pixel=(uint8_t)data[y*width+x];
out[y*width+x]=0xFF000000|colorTable[pixel];
}
}
}
}
else if(bitsPerPixel==16)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+2*x; // Byte at which this pixel starts
uint32_t pixel=(uint8_t)data[offset]|((uint32_t)((uint8_t)data[offset+1])<<8);
uint8_t r=(uint8_t)(((double)((pixel&redShift)>>redShift16)/redMax16)*255);
uint8_t g=(uint8_t)(((double)((pixel&greenShift)>>greenShift16)/greenMax16)*255);
uint8_t b=(uint8_t)(((double)((pixel&blueShift)>>blueShift16)/blueMax16)*255);
out[(height-y-1)*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+2*x; // Byte at which this pixel starts
uint32_t pixel=(uint8_t)data[offset]|((uint32_t)((uint8_t)data[offset+1])<<8);
uint8_t r=(uint8_t)(((double)((pixel&redShift)>>redShift16)/redMax16)*255);
uint8_t g=(uint8_t)(((double)((pixel&greenShift)>>greenShift16)/greenMax16)*255);
uint8_t b=(uint8_t)(((double)((pixel&blueShift)>>blueShift16)/blueMax16)*255);
out[y*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
}
else if(bitsPerPixel==24)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+3*x; // Byte at which this pixel starts
uint32_t b=(uint8_t)data[offset+blueShift];
uint32_t g=(uint8_t)data[offset+greenShift];
uint32_t r=(uint8_t)data[offset+redShift];
out[(height-y-1)*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+3*x; // Byte at which this pixel starts
uint32_t b=(uint8_t)data[offset+blueShift];
uint32_t g=(uint8_t)data[offset+greenShift];
uint32_t r=(uint8_t)data[offset+redShift];
out[y*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
}
else if(bitsPerPixel==32)
{
if(upsideDown)
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+4*x; // Byte at which this pixel starts
uint32_t b=(uint8_t)data[offset+blueShift];
uint32_t g=(uint8_t)data[offset+greenShift];
uint32_t r=(uint8_t)data[offset+redShift];
out[y*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
else
{
for(int32_t y=0;y<height;y++)
{
for(int32_t x=0;x<width;x++)
{
uint32_t offset=y*rowSize+4*x; // Byte at which this pixel starts
uint32_t b=(uint8_t)data[offset+blueShift];
uint32_t g=(uint8_t)data[offset+greenShift];
uint32_t r=(uint8_t)data[offset+redShift];
out[(height-y-1)*width+x]=0xFF000000|(r<<16)|(g<<8)|b;
}
}
}
}
free(colorTable);
return out;
}
else if(compressionMethod==BMP_COMPRESSION_RLE8)
{
// Change BMP_COMPRESSION_RLE4, too!
uint32_t y=upsideDown?height-1:0;
uint32_t x=0;
uint8_t nextByte;
while(true)
{
nextByte=(uint8_t)*(data++);
if(nextByte>0)
{
// Encoded mode
// First byte: number of pixels
// Second byte: indexed color
uint32_t color=0xFF000000|colorTable[(uint8_t)*(data++)];
for(uint8_t pos=0;pos<nextByte;pos++)
out[y*width+x++]=color;
}
else
{
uint8_t secondByte=(uint8_t)*(data++);
if(secondByte>0x2)
{
// Absolute mode
for(uint8_t pos=0;pos<secondByte;pos++)
out[y*width+(x++)]=0xFF000000|colorTable[(uint8_t)*(data++)];
data+=(data-orig)%2; // Run must be word-aligned.
}
else
{
// Special code
if(secondByte==0)
{
// End of line
if(upsideDown)
y--;
else
y++;
x=0;
}
else if(secondByte==1)
break; // End of bitmap
else // secondByte==2
{
// Delta
x+=(uint8_t)*(data++);
y+=(uint8_t)*(data++);
}
}
}
}
free(colorTable);
return out;
}
else if(compressionMethod==BMP_COMPRESSION_RLE4)
{
// Change BMP_COMPRESSION_RLE8, too!
uint32_t y=upsideDown?height-1:0;
uint32_t x=0;
uint8_t nextByte;
while(true)
{
nextByte=(uint8_t)*(data++);
if(nextByte>0)
{
// Encoded mode
// First byte: number of pixels
// Second byte: indexed colors (2)!
uint32_t secondByte=(uint8_t)*(data++);
uint32_t colorA=0xFF000000|colorTable[(secondByte&0xF0)>>4];
uint32_t colorB=0xFF000000|colorTable[secondByte&0xF];
for(uint8_t pos=0;pos<nextByte;pos++)
out[y*width+x++]=(pos%2)>0?colorB:colorA;
}
else
{
uint8_t secondByte=(uint8_t)*(data++);
if(secondByte>0x2)
{
// Absolute mode
uint8_t pixel;
bool second;
for(uint8_t pos=0;pos<secondByte;pos++)
{
if(!(second=(pos%2)>0))
pixel=(uint8_t)*(data++);
out[y*width+(x++)]=0xFF000000|colorTable[(second?(pixel&0xF):((pixel&0xF0)>>4))];
}
data+=(data-orig)%2; // Run must be word-aligned.
}
else
{
// Special code
if(secondByte==0)
{
// End of line
if(upsideDown)
y--;
else
y++;
x=0;
}
else if(secondByte==1)
break; // End of bitmap
else // secondByte==2
{
// Delta
x+=(uint8_t)*(data++);
y+=(uint8_t)*(data++);
}
}
}
}
free(colorTable);
return out;
}
free(colorTable);
return 0;
}
You can’t perform that action at this time.
