7 bmp.c - read and write windows BMP files
14 if (!i_writebmp_wiol(im, ig)) {
21 Reads and writes Windows BMP files.
28 #define FILEHEAD_SIZE 14
29 #define INFOHEAD_SIZE 40
33 #define BI_BITFIELDS 3
34 #define BMPRLE_ENDOFLINE 0
35 #define BMPRLE_ENDOFBMP 1
36 #define BMPRLE_DELTA 2
38 static int read_packed(io_glue *ig, char *format, ...);
39 static int write_packed(io_glue *ig, char *format, ...);
40 static int write_bmphead(io_glue *ig, i_img *im, int bit_count,
42 static int write_1bit_data(io_glue *ig, i_img *im);
43 static int write_4bit_data(io_glue *ig, i_img *im);
44 static int write_8bit_data(io_glue *ig, i_img *im);
45 static int write_24bit_data(io_glue *ig, i_img *im);
46 static int read_bmp_pal(io_glue *ig, i_img *im, int count);
47 static i_img *read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
48 int compression, long offbits, int allow_incomplete);
49 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
50 int compression, long offbits, int allow_incomplete);
51 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
52 int compression, long offbits, int allow_incomplete);
53 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
54 int bit_count, int clr_used, int compression,
55 long offbits, int allow_incomplete);
58 =item i_writebmp_wiol(im, io_glue)
60 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
61 formats depending on the image.
63 Never compresses the image.
68 i_writebmp_wiol(i_img *im, io_glue *ig) {
69 io_glue_commit_types(ig);
73 if (im->type == i_direct_type) {
74 return write_24bit_data(ig, im);
79 /* must be paletted */
80 pal_size = i_colorcount(im);
82 return write_1bit_data(ig, im);
84 else if (pal_size <= 16) {
85 return write_4bit_data(ig, im);
88 return write_8bit_data(ig, im);
94 =item i_readbmp_wiol(ig)
96 Reads a Windows format bitmap from the given file.
98 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
99 BI_BITFIELDS images too, but I need a test image.
105 i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
106 int b_magic, m_magic, filesize, res1, res2, infohead_size;
107 int xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
108 int clr_used, clr_important, offbits;
111 mm_log((1, "i_readbmp_wiol(ig %p)\n", ig));
113 io_glue_commit_types(ig);
116 if (!read_packed(ig, "CCVvvVVVVvvVVVVVV", &b_magic, &m_magic, &filesize,
117 &res1, &res2, &offbits, &infohead_size,
118 &xsize, &ysize, &planes,
119 &bit_count, &compression, &size_image, &xres, &yres,
120 &clr_used, &clr_important)) {
121 i_push_error(0, "file too short to be a BMP file");
124 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
126 i_push_error(0, "not a BMP file");
130 mm_log((1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
131 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
132 "clr_important %d\n", filesize, offbits, xsize, ysize, planes,
133 bit_count, compression, size_image, xres, yres, clr_used,
136 if (!i_int_check_image_file_limits(xsize, abs(ysize), 3, sizeof(i_sample_t))) {
137 mm_log((1, "i_readbmp_wiol: image size exceeds limits\n"));
143 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
148 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
153 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
160 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
161 offbits, allow_incomplete);
165 i_push_errorf(0, "unknown bit count for BMP file (%d)", bit_count);
170 /* store the resolution */
173 else if (yres && !xres)
176 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
177 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
179 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
180 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
181 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
182 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
183 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
184 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
193 =head1 IMPLEMENTATION FUNCTIONS
195 Internal functions used in the implementation.
199 =item read_packed(ig, format, ...)
201 Reads from the specified "file" the specified sizes. The format codes
202 match those used by perl's pack() function, though only a few are
203 implemented. In all cases the vararg arguement is an int *.
205 Returns non-zero if all of the arguments were read.
210 int read_packed(io_glue *ig, char *format, ...) {
211 unsigned char buf[4];
215 va_start(ap, format);
218 p = va_arg(ap, int *);
222 if (ig->readcb(ig, buf, 2) != 2)
224 *p = buf[0] + (buf[1] << 8);
228 if (ig->readcb(ig, buf, 4) != 4)
230 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
234 if (ig->readcb(ig, buf, 1) != 1)
240 if (ig->readcb(ig, buf, 1) != 1)
245 case '3': /* extension - 24-bit number */
246 if (ig->readcb(ig, buf, 3) != 3)
248 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
252 i_fatal(1, "Unknown read_packed format code 0x%02x", *format);
260 =item write_packed(ig, format, ...)
262 Writes packed data to the specified io_glue.
264 Returns non-zero on success.
270 write_packed(io_glue *ig, char *format, ...) {
271 unsigned char buf[4];
275 va_start(ap, format);
278 i = va_arg(ap, unsigned int);
284 if (ig->writecb(ig, buf, 2) == -1)
290 buf[1] = (i >> 8) & 0xFF;
291 buf[2] = (i >> 16) & 0xFF;
292 buf[3] = (i >> 24) & 0xFF;
293 if (ig->writecb(ig, buf, 4) == -1)
300 if (ig->writecb(ig, buf, 1) == -1)
305 i_fatal(1, "Unknown write_packed format code 0x%02x", *format);
315 =item write_bmphead(ig, im, bit_count, data_size)
317 Writes a Windows BMP header to the file.
319 Returns non-zero on success.
325 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
327 int got_xres, got_yres, aspect_only;
329 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
331 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
332 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
333 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
345 if (xres <= 0 || yres <= 0)
348 /* scale so the smaller value is 72 */
359 /* now to pels/meter */
363 if (im->type == i_palette_type) {
364 colors_used = i_colorcount(im);
365 offset += 4 * colors_used;
368 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
369 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
370 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
371 colors_used, colors_used)){
372 i_push_error(0, "cannot write bmp header");
375 if (im->type == i_palette_type) {
379 for (i = 0; i < colors_used; ++i) {
380 i_getcolors(im, i, &c, 1);
381 if (im->channels >= 3) {
382 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
384 i_push_error(0, "cannot write palette entry");
389 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
391 i_push_error(0, "cannot write palette entry");
402 =item write_1bit_data(ig, im)
404 Writes the image data as a 1-bit/pixel image.
406 Returns non-zero on success.
411 write_1bit_data(io_glue *ig, i_img *im) {
413 unsigned char *packed;
417 int line_size = (im->xsize+7) / 8;
421 /* round up to nearest multiple of four */
422 line_size = (line_size + 3) / 4 * 4;
424 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
427 /* this shouldn't be an issue, but let's be careful */
428 unpacked_size = im->xsize + 8;
429 if (unpacked_size < im->xsize) {
430 i_push_error(0, "integer overflow during memory allocation");
433 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
434 memset(line + im->xsize, 0, 8);
436 /* size allocated here is always much smaller than xsize, hence
437 can't overflow int */
438 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
439 memset(packed, 0, line_size);
441 for (y = im->ysize-1; y >= 0; --y) {
442 i_gpal(im, 0, im->xsize, y, line);
446 for (x = 0; x < im->xsize; ++x) {
449 if ((mask >>= 1) == 0) {
458 if (ig->writecb(ig, packed, line_size) < 0) {
461 i_push_error(0, "writing 1 bit/pixel packed data");
474 =item write_4bit_data(ig, im)
476 Writes the image data as a 4-bit/pixel image.
478 Returns non-zero on success.
483 write_4bit_data(io_glue *ig, i_img *im) {
485 unsigned char *packed;
487 int line_size = (im->xsize+1) / 2;
491 /* round up to nearest multiple of four */
492 line_size = (line_size + 3) / 4 * 4;
494 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
497 /* this shouldn't be an issue, but let's be careful */
498 unpacked_size = im->xsize + 2;
499 if (unpacked_size < im->xsize) {
500 i_push_error(0, "integer overflow during memory allocation");
503 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
504 memset(line + im->xsize, 0, 2);
506 /* size allocated here is always much smaller than xsize, hence
507 can't overflow int */
508 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
509 memset(packed, 0, line_size);
511 for (y = im->ysize-1; y >= 0; --y) {
512 i_gpal(im, 0, im->xsize, y, line);
514 for (x = 0; x < im->xsize; x += 2) {
515 *out++ = (line[x] << 4) + line[x+1];
517 if (ig->writecb(ig, packed, line_size) < 0) {
520 i_push_error(0, "writing 4 bit/pixel packed data");
533 =item write_8bit_data(ig, im)
535 Writes the image data as a 8-bit/pixel image.
537 Returns non-zero on success.
542 write_8bit_data(io_glue *ig, i_img *im) {
544 int line_size = im->xsize;
548 /* round up to nearest multiple of four */
549 line_size = (line_size + 3) / 4 * 4;
551 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
554 /* this shouldn't be an issue, but let's be careful */
555 unpacked_size = im->xsize + 4;
556 if (unpacked_size < im->xsize) {
557 i_push_error(0, "integer overflow during memory allocation");
560 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
561 memset(line + im->xsize, 0, 4);
563 for (y = im->ysize-1; y >= 0; --y) {
564 i_gpal(im, 0, im->xsize, y, line);
565 if (ig->writecb(ig, line, line_size) < 0) {
567 i_push_error(0, "writing 8 bit/pixel packed data");
578 static int bgr_chans[] = { 2, 1, 0, };
579 static int grey_chans[] = { 0, 0, 0, };
582 =item write_24bit_data(ig, im)
584 Writes the image data as a 24-bit/pixel image.
586 Returns non-zero on success.
591 write_24bit_data(io_glue *ig, i_img *im) {
593 unsigned char *samples;
595 int line_size = 3 * im->xsize;
597 /* just in case we implement a direct format with 2bytes/pixel
599 if (line_size / 3 != im->xsize) {
600 i_push_error(0, "integer overflow during memory allocation");
604 line_size = (line_size + 3) / 4 * 4;
606 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
608 chans = im->channels >= 3 ? bgr_chans : grey_chans;
609 samples = mymalloc(line_size); /* checked 29jun05 tonyc */
610 memset(samples, 0, line_size);
611 for (y = im->ysize-1; y >= 0; --y) {
612 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
613 if (ig->writecb(ig, samples, line_size) < 0) {
614 i_push_error(0, "writing image data");
627 =item read_bmp_pal(ig, im, count)
629 Reads count palette entries from the file and add them to the image.
631 Returns non-zero on success.
636 read_bmp_pal(io_glue *ig, i_img *im, int count) {
641 for (i = 0; i < count; ++i) {
642 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
643 i_push_error(0, "reading BMP palette");
649 if (i_addcolors(im, &c, 1) < 0) {
650 i_push_error(0, "out of space in image palette");
659 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
661 Reads in the palette and image data for a 1-bit/pixel image.
663 Returns the image or NULL.
668 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
669 int compression, long offbits, int allow_incomplete) {
671 int x, y, lasty, yinc, start_y;
673 unsigned char *packed;
674 int line_size = (xsize + 7)/8;
679 if (compression != BI_RGB) {
680 i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
684 if (xsize + 8 < xsize) { /* if there was overflow */
685 /* we check with 8 because we allocate that much for the decoded
687 i_push_error(0, "integer overflow during memory allocation");
691 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
692 adjustments below won't make it overflow */
693 line_size = (line_size+3) / 4 * 4;
701 /* when ysize is -ve it's a top-down image */
710 if (clr_used < 0 || clr_used > 2) {
711 i_push_errorf(0, "out of range colors used (%d)", clr_used);
715 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
716 if (offbits < base_offset) {
717 i_push_errorf(0, "image data offset too small (%ld)", offbits);
721 im = i_img_pal_new(xsize, ysize, 3, 256);
724 if (!read_bmp_pal(ig, im, clr_used)) {
729 if (offbits > base_offset) {
730 /* this will be slow if the offset is large, but that should be
733 while (base_offset < offbits) {
734 if (ig->readcb(ig, &buffer, 1) != 1) {
736 i_push_error(0, "failed skipping to image data offset");
743 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
745 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
746 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
748 if (ig->readcb(ig, packed, line_size) != line_size) {
751 if (allow_incomplete) {
752 i_tags_setn(&im->tags, "i_incomplete", 1);
753 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
757 i_push_error(0, "failed reading 1-bit bmp data");
765 for (x = 0; x < xsize; ++x) {
766 *p++ = (*in & bit) ? 1 : 0;
773 i_ppal(im, 0, xsize, y, line);
783 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
785 Reads in the palette and image data for a 4-bit/pixel image.
787 Returns the image or NULL.
789 Hopefully this will be combined with the following function at some
795 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
796 int compression, long offbits, int allow_incomplete) {
798 int x, y, lasty, yinc;
800 unsigned char *packed;
801 int line_size = (xsize + 1)/2;
807 /* line_size is going to be smaller than xsize in most cases (and
808 when it's not, xsize is itself small), and hence not overflow */
809 line_size = (line_size+3) / 4 * 4;
817 /* when ysize is -ve it's a top-down image */
827 if (clr_used > 16 || clr_used < 0) {
828 i_push_errorf(0, "out of range colors used (%d)", clr_used);
832 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
833 if (offbits < base_offset) {
834 i_push_errorf(0, "image data offset too small (%ld)", offbits);
838 im = i_img_pal_new(xsize, ysize, 3, 256);
839 if (!im) /* error should have been pushed already */
841 if (!read_bmp_pal(ig, im, clr_used)) {
846 if (offbits > base_offset) {
847 /* this will be slow if the offset is large, but that should be
850 while (base_offset < offbits) {
851 if (ig->readcb(ig, &buffer, 1) != 1) {
853 i_push_error(0, "failed skipping to image data offset");
861 packed = mymalloc(260); /* checked 29jun05 tonyc */
863 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
864 /* xsize won't approach MAXINT */
865 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
866 if (compression == BI_RGB) {
867 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
869 if (ig->readcb(ig, packed, line_size) != line_size) {
872 if (allow_incomplete) {
873 i_tags_setn(&im->tags, "i_incomplete", 1);
874 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
878 i_push_error(0, "failed reading 4-bit bmp data");
885 for (x = 0; x < xsize; x+=2) {
890 i_ppal(im, 0, xsize, y, line);
896 else if (compression == BI_RLE4) {
900 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
903 /* there's always at least 2 bytes in a sequence */
904 if (ig->readcb(ig, packed, 2) != 2) {
907 if (allow_incomplete) {
908 i_tags_setn(&im->tags, "i_incomplete", 1);
909 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
913 i_push_error(0, "missing data during decompression");
918 else if (packed[0]) {
919 if (x + packed[0] > xsize) {
920 /* this file is corrupt */
922 i_push_error(0, "invalid data during decompression");
926 line[0] = packed[1] >> 4;
927 line[1] = packed[1] & 0x0F;
928 for (i = 0; i < packed[0]; i += 2) {
930 i_ppal(im, x, x+2, y, line);
932 i_ppal(im, x, x+(packed[0]-i), y, line);
937 case BMPRLE_ENDOFLINE:
942 case BMPRLE_ENDOFBMP:
948 if (ig->readcb(ig, packed, 2) != 2) {
951 if (allow_incomplete) {
952 i_tags_setn(&im->tags, "i_incomplete", 1);
953 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
957 i_push_error(0, "missing data during decompression");
963 y += yinc * packed[1];
968 if (x + count > xsize) {
969 /* this file is corrupt */
971 i_push_error(0, "invalid data during decompression");
975 size = (count + 1) / 2;
976 read_size = (size+1) / 2 * 2;
977 if (ig->readcb(ig, packed, read_size) != read_size) {
980 if (allow_incomplete) {
981 i_tags_setn(&im->tags, "i_incomplete", 1);
982 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
986 i_push_error(0, "missing data during decompression");
991 for (i = 0; i < size; ++i) {
992 line[0] = packed[i] >> 4;
993 line[1] = packed[i] & 0xF;
994 i_ppal(im, x, x+2, y, line);
1002 else { /*if (compression == BI_RLE4) {*/
1005 i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
1014 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1016 Reads in the palette and image data for a 8-bit/pixel image.
1018 Returns the image or NULL.
1023 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1024 int compression, long offbits, int allow_incomplete) {
1026 int x, y, lasty, yinc, start_y;
1028 int line_size = xsize;
1031 line_size = (line_size+3) / 4 * 4;
1032 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1033 i_push_error(0, "integer overflow during memory allocation");
1043 /* when ysize is -ve it's a top-down image */
1052 if (clr_used > 256 || clr_used < 0) {
1053 i_push_errorf(0, "out of range colors used (%d)", clr_used);
1057 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1058 if (offbits < base_offset) {
1059 i_push_errorf(0, "image data offset too small (%ld)", offbits);
1063 im = i_img_pal_new(xsize, ysize, 3, 256);
1066 if (!read_bmp_pal(ig, im, clr_used)) {
1071 if (offbits > base_offset) {
1072 /* this will be slow if the offset is large, but that should be
1075 while (base_offset < offbits) {
1076 if (ig->readcb(ig, &buffer, 1) != 1) {
1078 i_push_error(0, "failed skipping to image data offset");
1085 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1086 if (compression == BI_RGB) {
1087 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1088 while (y != lasty) {
1089 if (ig->readcb(ig, line, line_size) != line_size) {
1091 if (allow_incomplete) {
1092 i_tags_setn(&im->tags, "i_incomplete", 1);
1093 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1097 i_push_error(0, "failed reading 8-bit bmp data");
1102 i_ppal(im, 0, xsize, y, line);
1107 else if (compression == BI_RLE8) {
1110 unsigned char packed[2];
1112 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1115 /* there's always at least 2 bytes in a sequence */
1116 if (ig->readcb(ig, packed, 2) != 2) {
1118 if (allow_incomplete) {
1119 i_tags_setn(&im->tags, "i_incomplete", 1);
1120 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1124 i_push_error(0, "missing data during decompression");
1130 if (x + packed[0] > xsize) {
1131 /* this file isn't incomplete, it's corrupt */
1133 i_push_error(0, "invalid data during decompression");
1137 memset(line, packed[1], packed[0]);
1138 i_ppal(im, x, x+packed[0], y, line);
1141 switch (packed[1]) {
1142 case BMPRLE_ENDOFLINE:
1147 case BMPRLE_ENDOFBMP:
1152 if (ig->readcb(ig, packed, 2) != 2) {
1154 if (allow_incomplete) {
1155 i_tags_setn(&im->tags, "i_incomplete", 1);
1156 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1160 i_push_error(0, "missing data during decompression");
1166 y += yinc * packed[1];
1171 if (x + count > xsize) {
1172 /* runs shouldn't cross a line boundary */
1173 /* this file isn't incomplete, it's corrupt */
1175 i_push_error(0, "invalid data during decompression");
1179 read_size = (count+1) / 2 * 2;
1180 if (ig->readcb(ig, line, read_size) != read_size) {
1182 if (allow_incomplete) {
1183 i_tags_setn(&im->tags, "i_incomplete", 1);
1184 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1188 i_push_error(0, "missing data during decompression");
1193 i_ppal(im, x, x+count, y, line);
1202 i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
1214 static struct bm_masks std_masks[] =
1217 { 0770000, 00007700, 00000077, },
1221 { 0xFF0000, 0x00FF00, 0x0000FF, },
1225 { 0xFF0000, 0x00FF00, 0x0000FF, },
1231 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1233 Skips the palette and reads in the image data for a direct colour image.
1235 Returns the image or NULL.
1240 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1241 int clr_used, int compression, long offbits,
1242 int allow_incomplete) {
1244 int x, y, starty, lasty, yinc;
1246 int pix_size = bit_count / 8;
1247 int line_size = xsize * pix_size;
1248 struct bm_masks masks;
1249 char unpack_code[2] = "";
1253 const char *compression_name;
1255 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1257 unpack_code[0] = *("v3V"+pix_size-2);
1258 unpack_code[1] = '\0';
1260 line_size = (line_size+3) / 4 * 4;
1261 extras = line_size - xsize * pix_size;
1269 /* when ysize is -ve it's a top-down image */
1276 if (compression == BI_RGB) {
1277 compression_name = "BI_RGB";
1278 masks = std_masks[pix_size-2];
1280 /* there's a potential "palette" after the header */
1281 for (i = 0; i < clr_used; ++clr_used) {
1283 if (ig->readcb(ig, buf, 4) != 4) {
1284 i_push_error(0, "skipping colors");
1290 else if (compression == BI_BITFIELDS) {
1292 compression_name = "BI_BITFIELDS";
1294 for (i = 0; i < 3; ++i) {
1295 if (!read_packed(ig, "V", masks.masks+i)) {
1296 i_push_error(0, "reading pixel masks");
1299 /* work out a shift for the mask */
1301 bit = masks.masks[i] & -masks.masks[i];
1306 masks.shifts[i] = pos - 8;
1308 base_offset += 4 * 4;
1311 i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
1315 if (offbits < base_offset) {
1316 i_push_errorf(0, "image data offset too small (%ld)", offbits);
1320 if (offbits > base_offset) {
1321 /* this will be slow if the offset is large, but that should be
1324 while (base_offset < offbits) {
1325 if (ig->readcb(ig, &buffer, 1) != 1) {
1326 i_push_error(0, "failed skipping to image data offset");
1333 im = i_img_empty(NULL, xsize, ysize);
1337 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1339 /* I wasn't able to make this overflow in testing, but better to be
1341 bytes = sizeof(i_color) * xsize;
1342 if (bytes / sizeof(i_color) != xsize) {
1344 i_push_error(0, "integer overflow calculating buffer size");
1347 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1348 while (y != lasty) {
1350 for (x = 0; x < xsize; ++x) {
1352 if (!read_packed(ig, unpack_code, &pixel)) {
1354 if (allow_incomplete) {
1355 i_tags_setn(&im->tags, "i_incomplete", 1);
1356 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1360 i_push_error(0, "failed reading image data");
1365 for (i = 0; i < 3; ++i) {
1366 if (masks.shifts[i] > 0)
1367 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1369 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1373 i_plin(im, 0, xsize, y, line);
1375 ig->readcb(ig, junk, extras);
1390 Tony Cook <tony@develop-help.com>
1394 Cannot save as compressed BMP.
1398 Doesn't handle OS/2 bitmaps.
1400 16-bit/pixel images haven't been tested. (I need an image).
1402 BI_BITFIELDS compression hasn't been tested (I need an image).
1404 The header handling for paletted images needs to be refactored