12 aims to decode *all* valid bitmap formats, although some of the
13 flavours couldn't be verified due to lack of suitable test-files.
14 the following flavours are supported:
16 Bit/Pix Orientation Compression Tested?
21 4 top->bottom RLE4 yes, but not with displacement
24 8 top->bottom RLE8 yes, but not with displacement
27 16 top->bottom BITMASK no
28 16 bottom->top BITMASK no
29 24 top->bottom n/a yes
30 24 bottom->top n/a yes
33 32 top->bottom BITMASK no
34 32 bottom->top BITMASK no
36 OS/2 1.x bmp files are recognised as well, but testing was very limited.
38 verifying was done with a number of test files, generated by
39 different tools. nevertheless, the tests were in no way exhaustive
40 enough to guarantee bug-free decoding. caveat emptor!
49 s |= ((short)Bgetc(b)) << 8;
60 l |= ((long)Bgetc(b)) << 8;
61 l |= ((long)Bgetc(b)) << 16;
62 l |= ((long)Bgetc(b)) << 24;
67 /* get highest bit set */
72 for(i = 32; i; i--, x <<= 1)
78 /* Load a 1-Bit encoded BMP file (uncompressed) */
80 load_1T(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
82 long ix, iy, i = 0, step_up = 0, padded_width = ((width + 31) / 32) * 32;
85 if(height > 0) { /* bottom-up */
86 i = (height - 1) * width;
91 for(iy = height; iy; iy--, i += step_up)
92 for(ix = 0, n = 0; ix < padded_width; ix++, n--) {
98 buf[i] = clut[val & 0x80 ? 1 : 0];
106 /* Load a 4-Bit encoded BMP file (uncompressed) */
108 load_4T(Biobuf* b, long width, long height, Rgb* buf, Rgb* clut)
110 long ix, iy, i = 0, step_up = 0, skip = (4 - (((width % 8) + 1) / 2)) & 3;
113 if(height > 0) { /* bottom-up */
114 i = (height - 1) * width;
115 step_up = -2 * width;
119 for(iy = height; iy; iy--, i += step_up) {
120 for(ix = 0; ix < width; ) {
121 valH = valL = Bgetc(b) & 0xff;
138 /* Load a 4-Bit encoded BMP file (RLE4-compressed) */
140 load_4C(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
142 long ix, iy = height -1;
143 uint val, valS, skip;
149 val = (uint)Bgetc(b);
152 valS = (uint)Bgetc(b);
153 p = &buf[ix + iy * width];
155 *p = clut[0xf & (valS >> 4)];
159 *p = clut[0xf & valS];
166 /* Special modes... */
169 case 0: /* End-Of-Line detected */
173 case 1: /* End-Of-Picture detected -->> abort */
177 case 2: /* Position change detected */
178 val = (uint)Bgetc(b);
180 val = (uint)Bgetc(b);
184 default:/* Transparent data sequence detected */
185 p = &buf[ix + iy * width];
186 if((1 == (val & 3)) || (2 == (val & 3)))
192 valS = (uint)Bgetc(b);
193 *p = clut[0xf & (valS >> 4)];
197 *p = clut[0xf & valS];
213 /* Load a 8-Bit encoded BMP file (uncompressed) */
215 load_8T(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
217 long ix, iy, i = 0, step_up = 0, skip = (4 - (width % 4)) & 3;
219 if(height > 0) { /* bottom-up */
220 i = (height - 1) * width;
221 step_up = -2 * width;
225 for(iy = height; iy; iy--, i += step_up) {
226 for(ix = 0; ix < width; ix++, i++)
227 buf[i] = clut[Bgetc(b) & 0xff];
233 /* Load a 8-Bit encoded BMP file (RLE8-compressed) */
235 load_8C(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
237 long ix, iy = height -1;
248 p = &buf[ix + iy * width];
255 /* Special modes... */
258 case 0: /* End-Of-Line detected */
262 case 1: /* End-Of-Picture detected */
266 case 2: /* Position change detected */
272 default: /* Transparent (not compressed) sequence detected */
273 p = &buf[ix + iy * width];
286 /* Align data stream */
296 /* Load a 16-Bit encoded BMP file (uncompressed) */
298 load_16(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
301 long ix, iy, i = 0, step_up = 0;
303 if(height > 0) { /* bottom-up */
304 i = (height - 1) * width;
305 step_up = -2 * width;
310 unsigned mask_blue = (unsigned)clut[0].blue +
311 ((unsigned)clut[0].green << 8);
312 unsigned mask_green = (unsigned)clut[1].blue +
313 ((unsigned)clut[1].green << 8);
314 unsigned mask_red = (unsigned)clut[2].blue +
315 ((unsigned)clut[2].green << 8);
316 int shft_blue = msb((ulong)mask_blue) - 8;
317 int shft_green = msb((ulong)mask_green) - 8;
318 int shft_red = msb((ulong)mask_red) - 8;
320 for(iy = height; iy; iy--, i += step_up)
321 for(ix = 0; ix < width; ix++, i++) {
323 Bread(b, c, sizeof(c));
324 val = (unsigned)c[0] + ((unsigned)c[1] << 8);
328 buf[i].blue = (uchar)((val & mask_blue) >> shft_blue);
330 buf[i].blue = (uchar)((val & mask_blue) << -shft_blue);
332 buf[i].green = (uchar)((val & mask_green) >> shft_green);
334 buf[i].green = (uchar)((val & mask_green) << -shft_green);
336 buf[i].red = (uchar)((val & mask_red) >> shft_red);
338 buf[i].red = (uchar)((val & mask_red) << -shft_red);
341 for(iy = height; iy; iy--, i += step_up)
342 for(ix = 0; ix < width; ix++, i++) {
343 Bread(b, c, sizeof(c));
344 buf[i].blue = (uchar)((c[0] << 3) & 0xf8);
345 buf[i].green = (uchar)(((((unsigned)c[1] << 6) +
346 (((unsigned)c[0]) >> 2))) & 0xf8);
347 buf[i].red = (uchar)((c[1] << 1) & 0xf8);
352 /* Load a 24-Bit encoded BMP file (uncompressed) */
354 load_24T(Biobuf* b, long width, long height, Rgb* buf)
356 long ix, iy, i = 0, step_up = 0, skip = (4 - ((width * 3) % 4)) & 3;
358 if(height > 0) { /* bottom-up */
359 i = (height - 1) * width;
360 step_up = -2 * width;
364 for(iy = height; iy; iy--, i += step_up) {
365 for(ix = 0; ix < width; ix++, i++) {
367 buf[i].blue = Bgetc(b);
368 buf[i].green = Bgetc(b);
369 buf[i].red = Bgetc(b);
376 /* Load a 32-Bit encoded BMP file (uncompressed) */
378 load_32(Biobuf *b, long width, long height, Rgb* buf, Rgb* clut)
381 long ix, iy, i = 0, step_up = 0;
383 if(height > 0) { /* bottom-up */
384 i = (height - 1) * width;
385 step_up = -2 * width;
390 ulong mask_blue = (ulong)clut[0].blue +
391 ((ulong)clut[0].green << 8) +
392 ((ulong)clut[0].red << 16) +
393 ((ulong)clut[0].alpha << 24);
394 ulong mask_green = (ulong)clut[1].blue +
395 ((ulong)clut[1].green << 8) +
396 ((ulong)clut[1].red << 16) +
397 ((ulong)clut[1].alpha << 24);
398 ulong mask_red = (ulong)clut[2].blue +
399 ((ulong)clut[2].green << 8) +
400 ((ulong)clut[2].red << 16) +
401 ((ulong)clut[2].alpha << 24);
402 int shft_blue = msb(mask_blue) - 8;
403 int shft_green = msb(mask_green) - 8;
404 int shft_red = msb(mask_red) - 8;
406 for(iy = height; iy; iy--, i += step_up)
407 for(ix = 0; ix < width; ix++, i++) {
409 Bread(b, c, sizeof(c));
410 val = (ulong)c[0] + ((ulong)c[1] << 8) +
411 ((ulong)c[2] << 16) + ((ulong)c[1] << 24);
415 buf[i].blue = (uchar)((val & mask_blue) >> shft_blue);
417 buf[i].blue = (uchar)((val & mask_blue) << -shft_blue);
419 buf[i].green = (uchar)((val & mask_green) >> shft_green);
421 buf[i].green = (uchar)((val & mask_green) << -shft_green);
423 buf[i].red = (uchar)((val & mask_red) >> shft_red);
425 buf[i].red = (uchar)((val & mask_red) << -shft_red);
428 for(iy = height; iy; iy--, i += step_up)
429 for(ix = 0; ix < width; ix++, i++) {
430 Bread(b, c, nelem(c));
440 ReadBMP(Biobuf *b, int *width, int *height)
442 int colours, num_coltab = 0;
449 if(bmfh.type != 0x4d42) /* signature must be 'BM' */
450 sysfatal("bad magic number, not a BMP file");
453 bmfh.reserved1 = r16(b);
454 bmfh.reserved2 = r16(b);
455 bmfh.offbits = r32(b);
457 memset(&bmih, 0, sizeof(bmih));
460 if(bmih.size == 0x0c) { /* OS/2 1.x version */
462 bmih.height = r16(b);
463 bmih.planes = r16(b);
465 bmih.compression = BMP_RGB;
466 } else { /* Windows */
468 bmih.height = r32(b);
469 bmih.planes = r16(b);
471 bmih.compression = r32(b);
472 bmih.imagesize = r32(b);
475 bmih.colours = r32(b);
476 bmih.impcolours = r32(b);
480 /* load colour table */
482 num_coltab = (int)bmih.impcolours;
484 num_coltab = 1 << bmih.bpp;
485 } else if(bmih.compression == BMP_BITFIELDS &&
486 (bmih.bpp == 16 || bmih.bpp == 32))
492 Bseek(b, bmih.size + Filehdrsz, 0);
494 for(i = 0; i < num_coltab; i++) {
495 clut[i].blue = (uchar)Bgetc(b);
496 clut[i].green = (uchar)Bgetc(b);
497 clut[i].red = (uchar)Bgetc(b);
498 clut[i].alpha = (uchar)Bgetc(b);
503 *height = bmih.height;
506 Bseek(b, bmfh.offbits, 0);
508 if ((buf = calloc(sizeof(Rgb), *width * abs(*height))) == nil)
509 sysfatal("no memory");
513 load_1T(b, *width, *height, buf, clut);
516 if(bmih.compression == BMP_RLE4)
517 load_4C(b, *width, *height, buf, clut);
519 load_4T(b, *width, *height, buf, clut);
522 if(bmih.compression == BMP_RLE8)
523 load_8C(b, *width, *height, buf, clut);
525 load_8T(b, *width, *height, buf, clut);
528 load_16(b, *width, *height, buf,
529 bmih.compression == BMP_BITFIELDS ? clut : nil);
532 load_24T(b, *width, *height, buf);
535 load_32(b, *width, *height, buf,
536 bmih.compression == BMP_BITFIELDS ? clut : nil);
543 Breadbmp(Biobuf *bp, int colourspace)
545 Rawimage *a, **array;
546 int c, width, height;
557 if (colourspace != CRGB) {
558 errstr(ebuf, sizeof ebuf); /* throw it away */
559 werrstr("ReadRGB: unknown colour space %d", colourspace);
563 if ((bmp = ReadBMP(bp, &width, &height)) == nil)
566 if ((a = calloc(sizeof(Rawimage), 1)) == nil)
569 for (c = 0; c < 3; c++)
570 if ((a->chans[c] = calloc(width, height)) == nil)
573 if ((array = calloc(sizeof(Rawimage *), 2)) == nil)
580 a->chanlen = width * height;
581 a->r = Rect(0, 0, width, height);
584 e = s + width * height;
600 for (c = 0; c < 3; c++)
614 readbmp(int fd, int colorspace)
619 if (Binit(&b, fd, OREAD) < 0)
621 a = Breadbmp(&b, colorspace);