1 #define IMAGER_NO_CONTEXT
8 bmp.c - read and write windows BMP files
15 if (!i_writebmp_wiol(im, ig)) {
22 Reads and writes Windows BMP files.
29 #define FILEHEAD_SIZE 14
30 #define INFOHEAD_SIZE 40
34 #define BI_BITFIELDS 3
35 #define BMPRLE_ENDOFLINE 0
36 #define BMPRLE_ENDOFBMP 1
37 #define BMPRLE_DELTA 2
39 #define SIGNBIT32 ((i_upacked_t)1U << 31)
40 #define SIGNBIT16 ((i_upacked_t)1U << 15)
42 #define SIGNMAX32 ((1UL << 31) - 1)
44 static int read_packed(io_glue *ig, char *format, ...);
45 static int write_packed(io_glue *ig, char *format, ...);
46 static int write_bmphead(io_glue *ig, i_img *im, int bit_count,
48 static int write_1bit_data(io_glue *ig, i_img *im);
49 static int write_4bit_data(io_glue *ig, i_img *im);
50 static int write_8bit_data(io_glue *ig, i_img *im);
51 static int write_24bit_data(io_glue *ig, i_img *im);
52 static int read_bmp_pal(io_glue *ig, i_img *im, int count);
53 static i_img *read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
54 int compression, long offbits, int allow_incomplete);
55 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
56 int compression, long offbits, int allow_incomplete);
57 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
58 int compression, long offbits, int allow_incomplete);
59 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
60 int bit_count, int clr_used, int compression,
61 long offbits, int allow_incomplete);
63 /* used for the read_packed() and write_packed() functions, an integer
65 typedef long i_packed_t;
66 typedef unsigned long i_upacked_t;
69 =item i_writebmp_wiol(im, io_glue)
71 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
72 formats depending on the image.
74 Never compresses the image.
79 i_writebmp_wiol(i_img *im, io_glue *ig) {
84 if (im->type == i_direct_type) {
85 return write_24bit_data(ig, im);
90 /* must be paletted */
91 pal_size = i_colorcount(im);
93 return write_1bit_data(ig, im);
95 else if (pal_size <= 16) {
96 return write_4bit_data(ig, im);
99 return write_8bit_data(ig, im);
105 =item i_readbmp_wiol(ig)
107 Reads a Windows format bitmap from the given file.
109 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
110 BI_BITFIELDS images too, but I need a test image.
116 i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
117 i_packed_t b_magic, m_magic, filesize, res1, res2, infohead_size;
118 i_packed_t xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
119 i_packed_t clr_used, clr_important, offbits;
123 im_log((aIMCTX, 1, "i_readbmp_wiol(ig %p)\n", ig));
127 if (!read_packed(ig, "CCVvvVVV!V!vvVVVVVV", &b_magic, &m_magic, &filesize,
128 &res1, &res2, &offbits, &infohead_size,
129 &xsize, &ysize, &planes,
130 &bit_count, &compression, &size_image, &xres, &yres,
131 &clr_used, &clr_important)) {
132 i_push_error(0, "file too short to be a BMP file");
135 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
137 i_push_error(0, "not a BMP file");
141 im_log((aIMCTX, 1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
142 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
143 "clr_important %d\n", (int)filesize, (int)offbits, (int)xsize,
144 (int)ysize, (int)planes, (int)bit_count, (int)compression,
145 (int)size_image, (int)xres, (int)yres, (int)clr_used,
146 (int)clr_important));
148 if (!i_int_check_image_file_limits(xsize, abs(ysize), 3, sizeof(i_sample_t))) {
149 im_log((aIMCTX, 1, "i_readbmp_wiol: image size exceeds limits\n"));
155 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
160 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
165 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
172 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
173 offbits, allow_incomplete);
177 im_push_errorf(aIMCTX, 0, "unknown bit count for BMP file (%d)", (int)bit_count);
182 /* store the resolution */
185 else if (yres && !xres)
188 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
189 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
191 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
192 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
193 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
194 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
195 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
196 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
205 =head1 IMPLEMENTATION FUNCTIONS
207 Internal functions used in the implementation.
211 =item read_packed(ig, format, ...)
213 Reads from the specified "file" the specified sizes. The format codes
214 match those used by perl's pack() function, though only a few are
215 implemented. In all cases the vararg arguement is an int *.
217 Returns non-zero if all of the arguments were read.
222 read_packed(io_glue *ig, char *format, ...) {
223 unsigned char buf[4];
228 int shrieking; /* format code has a ! flag */
230 va_start(ap, format);
233 p = va_arg(ap, i_packed_t *);
236 shrieking = *format == '!';
237 if (shrieking) ++format;
241 if (i_io_read(ig, buf, 2) != 2)
243 work = buf[0] + ((i_packed_t)buf[1] << 8);
245 *p = (work ^ SIGNBIT16) - SIGNBIT16;
251 if (i_io_read(ig, buf, 4) != 4)
253 work = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16) + ((i_packed_t)buf[3] << 24);
255 *p = (work ^ SIGNBIT32) - SIGNBIT32;
261 if (i_io_read(ig, buf, 1) != 1)
267 if (i_io_read(ig, buf, 1) != 1)
272 case '3': /* extension - 24-bit number */
273 if (i_io_read(ig, buf, 3) != 3)
275 *p = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16);
281 im_fatal(aIMCTX, 1, "Unknown read_packed format code 0x%02x", code);
289 =item write_packed(ig, format, ...)
291 Writes packed data to the specified io_glue.
293 Returns non-zero on success.
299 write_packed(io_glue *ig, char *format, ...) {
300 unsigned char buf[4];
304 va_start(ap, format);
307 i = va_arg(ap, i_upacked_t);
313 if (i_io_write(ig, buf, 2) == -1)
319 buf[1] = (i >> 8) & 0xFF;
320 buf[2] = (i >> 16) & 0xFF;
321 buf[3] = (i >> 24) & 0xFF;
322 if (i_io_write(ig, buf, 4) == -1)
329 if (i_io_write(ig, buf, 1) == -1)
336 im_fatal(aIMCTX, 1, "Unknown write_packed format code 0x%02x", *format);
351 =item write_bmphead(ig, im, bit_count, data_size)
353 Writes a Windows BMP header to the file.
355 Returns non-zero on success.
361 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
363 int got_xres, got_yres, aspect_only;
365 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
368 if (im->xsize > SIGNMAX32 || im->ysize > SIGNMAX32) {
369 i_push_error(0, "image too large to write to BMP");
373 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
374 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
375 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
387 if (xres <= 0 || yres <= 0)
390 /* scale so the smaller value is 72 */
401 /* now to pels/meter */
405 if (im->type == i_palette_type) {
406 colors_used = i_colorcount(im);
407 offset += 4 * colors_used;
410 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M',
411 (i_upacked_t)(data_size+offset),
412 (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset,
413 (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize,
414 (i_upacked_t)im->ysize, (i_upacked_t)1,
415 (i_upacked_t)bit_count, (i_upacked_t)BI_RGB,
416 (i_upacked_t)data_size,
417 (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5),
418 (i_upacked_t)colors_used, (i_upacked_t)colors_used)){
419 i_push_error(0, "cannot write bmp header");
422 if (im->type == i_palette_type) {
426 for (i = 0; i < colors_used; ++i) {
427 i_getcolors(im, i, &c, 1);
428 if (im->channels >= 3) {
429 if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]),
430 (i_upacked_t)(c.channel[1]),
431 (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) {
432 i_push_error(0, "cannot write palette entry");
437 i_upacked_t v = c.channel[0];
438 if (!write_packed(ig, "CCCC", v, v, v, 0)) {
439 i_push_error(0, "cannot write palette entry");
450 =item write_1bit_data(ig, im)
452 Writes the image data as a 1-bit/pixel image.
454 Returns non-zero on success.
459 write_1bit_data(io_glue *ig, i_img *im) {
461 unsigned char *packed;
465 int line_size = (im->xsize+7) / 8;
470 /* round up to nearest multiple of four */
471 line_size = (line_size + 3) / 4 * 4;
473 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
476 /* this shouldn't be an issue, but let's be careful */
477 unpacked_size = im->xsize + 8;
478 if (unpacked_size < im->xsize) {
479 i_push_error(0, "integer overflow during memory allocation");
482 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
483 memset(line + im->xsize, 0, 8);
485 /* size allocated here is always much smaller than xsize, hence
486 can't overflow int */
487 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
488 memset(packed, 0, line_size);
490 for (y = im->ysize-1; y >= 0; --y) {
491 i_gpal(im, 0, im->xsize, y, line);
495 for (x = 0; x < im->xsize; ++x) {
498 if ((mask >>= 1) == 0) {
507 if (i_io_write(ig, packed, line_size) < 0) {
510 i_push_error(0, "writing 1 bit/pixel packed data");
524 =item write_4bit_data(ig, im)
526 Writes the image data as a 4-bit/pixel image.
528 Returns non-zero on success.
533 write_4bit_data(io_glue *ig, i_img *im) {
535 unsigned char *packed;
537 int line_size = (im->xsize+1) / 2;
542 /* round up to nearest multiple of four */
543 line_size = (line_size + 3) / 4 * 4;
545 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
548 /* this shouldn't be an issue, but let's be careful */
549 unpacked_size = im->xsize + 2;
550 if (unpacked_size < im->xsize) {
551 i_push_error(0, "integer overflow during memory allocation");
554 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
555 memset(line + im->xsize, 0, 2);
557 /* size allocated here is always much smaller than xsize, hence
558 can't overflow int */
559 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
560 memset(packed, 0, line_size);
562 for (y = im->ysize-1; y >= 0; --y) {
563 i_gpal(im, 0, im->xsize, y, line);
565 for (x = 0; x < im->xsize; x += 2) {
566 *out++ = (line[x] << 4) + line[x+1];
568 if (i_io_write(ig, packed, line_size) < 0) {
571 i_push_error(0, "writing 4 bit/pixel packed data");
585 =item write_8bit_data(ig, im)
587 Writes the image data as a 8-bit/pixel image.
589 Returns non-zero on success.
594 write_8bit_data(io_glue *ig, i_img *im) {
596 int line_size = im->xsize;
601 /* round up to nearest multiple of four */
602 line_size = (line_size + 3) / 4 * 4;
604 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
607 /* this shouldn't be an issue, but let's be careful */
608 unpacked_size = im->xsize + 4;
609 if (unpacked_size < im->xsize) {
610 i_push_error(0, "integer overflow during memory allocation");
613 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
614 memset(line + im->xsize, 0, 4);
616 for (y = im->ysize-1; y >= 0; --y) {
617 i_gpal(im, 0, im->xsize, y, line);
618 if (i_io_write(ig, line, line_size) < 0) {
620 i_push_error(0, "writing 8 bit/pixel packed data");
633 =item write_24bit_data(ig, im)
635 Writes the image data as a 24-bit/pixel image.
637 Returns non-zero on success.
642 write_24bit_data(io_glue *ig, i_img *im) {
643 unsigned char *samples;
645 int line_size = 3 * im->xsize;
649 i_get_file_background(im, &bg);
651 /* just in case we implement a direct format with 2bytes/pixel
653 if (line_size / 3 != im->xsize) {
654 i_push_error(0, "integer overflow during memory allocation");
658 line_size = (line_size + 3) / 4 * 4;
660 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
662 samples = mymalloc(4 * im->xsize);
663 memset(samples, 0, line_size);
664 for (y = im->ysize-1; y >= 0; --y) {
665 unsigned char *samplep = samples;
667 i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg);
668 for (x = 0; x < im->xsize; ++x) {
669 unsigned char tmp = samplep[2];
670 samplep[2] = samplep[0];
674 if (i_io_write(ig, samples, line_size) < 0) {
675 i_push_error(0, "writing image data");
689 =item read_bmp_pal(ig, im, count)
691 Reads count palette entries from the file and add them to the image.
693 Returns non-zero on success.
698 read_bmp_pal(io_glue *ig, i_img *im, int count) {
700 i_packed_t r, g, b, x;
704 for (i = 0; i < count; ++i) {
705 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
706 i_push_error(0, "reading BMP palette");
712 if (i_addcolors(im, &c, 1) < 0) {
713 i_push_error(0, "out of space in image palette");
722 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
724 Reads in the palette and image data for a 1-bit/pixel image.
726 Returns the image or NULL.
731 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
732 int compression, long offbits, int allow_incomplete) {
734 int x, y, lasty, yinc, start_y;
736 unsigned char *packed;
737 int line_size = (xsize + 7)/8;
743 if (compression != BI_RGB) {
744 im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression);
748 if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */
749 /* we check with 8 because we allocate that much for the decoded
751 i_push_error(0, "integer overflow during memory allocation");
755 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
756 adjustments below won't make it overflow */
757 line_size = (line_size+3) / 4 * 4;
765 /* when ysize is -ve it's a top-down image */
774 if (clr_used < 0 || clr_used > 2) {
775 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
779 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
780 if (offbits < base_offset) {
781 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
785 im = i_img_pal_new(xsize, ysize, 3, 256);
788 if (!read_bmp_pal(ig, im, clr_used)) {
793 if (offbits > base_offset) {
794 /* this will be slow if the offset is large, but that should be
797 while (base_offset < offbits) {
798 if (i_io_read(ig, &buffer, 1) != 1) {
800 i_push_error(0, "failed skipping to image data offset");
807 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
809 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
810 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
812 if (i_io_read(ig, packed, line_size) != line_size) {
815 if (allow_incomplete) {
816 i_tags_setn(&im->tags, "i_incomplete", 1);
817 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
821 i_push_error(0, "failed reading 1-bit bmp data");
829 for (x = 0; x < xsize; ++x) {
830 *p++ = (*in & bit) ? 1 : 0;
837 i_ppal(im, 0, xsize, y, line);
847 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
849 Reads in the palette and image data for a 4-bit/pixel image.
851 Returns the image or NULL.
853 Hopefully this will be combined with the following function at some
859 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
860 int compression, long offbits, int allow_incomplete) {
862 int x, y, lasty, yinc;
864 unsigned char *packed;
865 int line_size = (xsize + 1)/2;
872 /* line_size is going to be smaller than xsize in most cases (and
873 when it's not, xsize is itself small), and hence not overflow */
874 line_size = (line_size+3) / 4 * 4;
882 /* when ysize is -ve it's a top-down image */
892 if (clr_used > 16 || clr_used < 0) {
893 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
897 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
898 if (offbits < base_offset) {
899 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
903 im = i_img_pal_new(xsize, ysize, 3, 256);
904 if (!im) /* error should have been pushed already */
906 if (!read_bmp_pal(ig, im, clr_used)) {
911 if (offbits > base_offset) {
912 /* this will be slow if the offset is large, but that should be
915 while (base_offset < offbits) {
916 if (i_io_read(ig, &buffer, 1) != 1) {
918 i_push_error(0, "failed skipping to image data offset");
926 packed = mymalloc(260); /* checked 29jun05 tonyc */
928 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
929 /* xsize won't approach MAXINT */
930 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
931 if (compression == BI_RGB) {
932 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
934 if (i_io_read(ig, packed, line_size) != line_size) {
937 if (allow_incomplete) {
938 i_tags_setn(&im->tags, "i_incomplete", 1);
939 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
943 i_push_error(0, "failed reading 4-bit bmp data");
950 for (x = 0; x < xsize; x+=2) {
955 i_ppal(im, 0, xsize, y, line);
961 else if (compression == BI_RLE4) {
964 i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */
966 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
969 /* there's always at least 2 bytes in a sequence */
970 if (i_io_read(ig, packed, 2) != 2) {
973 if (allow_incomplete) {
974 i_tags_setn(&im->tags, "i_incomplete", 1);
975 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
979 i_push_error(0, "missing data during decompression");
984 else if (packed[0]) {
985 int count = packed[0];
986 if (x + count > xlimit) {
987 /* this file is corrupt */
990 i_push_error(0, "invalid data during decompression");
991 im_log((aIMCTX, 1, "read 4-bit: scanline overflow x %d + count %d vs xlimit %d (y %d)\n",
992 (int)x, count, (int)xlimit, (int)y));
996 /* fill in the line */
997 for (i = 0; i < count; i += 2)
998 line[i] = packed[1] >> 4;
999 for (i = 1; i < count; i += 2)
1000 line[i] = packed[1] & 0x0F;
1001 i_ppal(im, x, x+count, y, line);
1004 switch (packed[1]) {
1005 case BMPRLE_ENDOFLINE:
1010 case BMPRLE_ENDOFBMP:
1016 if (i_io_read(ig, packed, 2) != 2) {
1019 if (allow_incomplete) {
1020 i_tags_setn(&im->tags, "i_incomplete", 1);
1021 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1025 i_push_error(0, "missing data during decompression");
1031 y += yinc * packed[1];
1036 if (x + count > xlimit) {
1037 /* this file is corrupt */
1040 i_push_error(0, "invalid data during decompression");
1041 im_log((aIMCTX, 1, "read 4-bit: scanline overflow (unpacked) x %d + count %d vs xlimit %d (y %d)\n",
1042 (int)x, count, (int)xlimit, (int)y));
1046 size = (count + 1) / 2;
1047 read_size = (size+1) / 2 * 2;
1048 if (i_io_read(ig, packed, read_size) != read_size) {
1051 if (allow_incomplete) {
1052 i_tags_setn(&im->tags, "i_incomplete", 1);
1053 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1057 i_push_error(0, "missing data during decompression");
1062 for (i = 0; i < size; ++i) {
1063 line[0] = packed[i] >> 4;
1064 line[1] = packed[i] & 0xF;
1065 i_ppal(im, x, x+2, y, line);
1073 else { /*if (compression == BI_RLE4) {*/
1076 im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression);
1085 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1087 Reads in the palette and image data for a 8-bit/pixel image.
1089 Returns the image or NULL.
1094 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1095 int compression, long offbits, int allow_incomplete) {
1097 int x, y, lasty, yinc, start_y;
1099 int line_size = xsize;
1103 line_size = (line_size+3) / 4 * 4;
1104 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1105 i_push_error(0, "integer overflow during memory allocation");
1115 /* when ysize is -ve it's a top-down image */
1124 if (clr_used > 256 || clr_used < 0) {
1125 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
1129 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1130 if (offbits < base_offset) {
1131 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1135 im = i_img_pal_new(xsize, ysize, 3, 256);
1138 if (!read_bmp_pal(ig, im, clr_used)) {
1143 if (offbits > base_offset) {
1144 /* this will be slow if the offset is large, but that should be
1147 while (base_offset < offbits) {
1148 if (i_io_read(ig, &buffer, 1) != 1) {
1150 i_push_error(0, "failed skipping to image data offset");
1157 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1158 if (compression == BI_RGB) {
1159 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1160 while (y != lasty) {
1161 if (i_io_read(ig, line, line_size) != line_size) {
1163 if (allow_incomplete) {
1164 i_tags_setn(&im->tags, "i_incomplete", 1);
1165 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1169 i_push_error(0, "failed reading 8-bit bmp data");
1174 i_ppal(im, 0, xsize, y, line);
1179 else if (compression == BI_RLE8) {
1182 unsigned char packed[2];
1184 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1187 /* there's always at least 2 bytes in a sequence */
1188 if (i_io_read(ig, packed, 2) != 2) {
1190 if (allow_incomplete) {
1191 i_tags_setn(&im->tags, "i_incomplete", 1);
1192 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1196 i_push_error(0, "missing data during decompression");
1202 if (x + packed[0] > xsize) {
1203 /* this file isn't incomplete, it's corrupt */
1205 i_push_error(0, "invalid data during decompression");
1209 memset(line, packed[1], packed[0]);
1210 i_ppal(im, x, x+packed[0], y, line);
1213 switch (packed[1]) {
1214 case BMPRLE_ENDOFLINE:
1219 case BMPRLE_ENDOFBMP:
1224 if (i_io_read(ig, packed, 2) != 2) {
1226 if (allow_incomplete) {
1227 i_tags_setn(&im->tags, "i_incomplete", 1);
1228 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1232 i_push_error(0, "missing data during decompression");
1238 y += yinc * packed[1];
1243 if (x + count > xsize) {
1244 /* runs shouldn't cross a line boundary */
1245 /* this file isn't incomplete, it's corrupt */
1247 i_push_error(0, "invalid data during decompression");
1251 read_size = (count+1) / 2 * 2;
1252 if (i_io_read(ig, line, read_size) != read_size) {
1254 if (allow_incomplete) {
1255 i_tags_setn(&im->tags, "i_incomplete", 1);
1256 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1260 i_push_error(0, "missing data during decompression");
1265 i_ppal(im, x, x+count, y, line);
1274 im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression);
1287 static struct bm_masks std_masks[] =
1290 { 0076000, 00001740, 00000037, },
1295 { 0xFF0000, 0x00FF00, 0x0000FF, },
1300 { 0xFF0000, 0x00FF00, 0x0000FF, },
1306 /* multiplier and shift for converting from N bits to 8 bits */
1307 struct bm_sampconverts {
1311 static struct bm_sampconverts samp_converts[] = {
1312 { 0xff, 0 }, /* 1 bit samples */
1318 { 0x81, 6 } /* 7 bit samples */
1322 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1324 Skips the palette and reads in the image data for a direct colour image.
1326 Returns the image or NULL.
1331 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1332 int clr_used, int compression, long offbits,
1333 int allow_incomplete) {
1335 int x, y, starty, lasty, yinc;
1337 int pix_size = bit_count / 8;
1338 int line_size = xsize * pix_size;
1339 struct bm_masks masks;
1340 char unpack_code[2] = "";
1344 const char *compression_name;
1346 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1349 unpack_code[0] = *("v3V"+pix_size-2);
1350 unpack_code[1] = '\0';
1352 line_size = (line_size+3) / 4 * 4;
1353 extras = line_size - xsize * pix_size;
1361 /* when ysize is -ve it's a top-down image */
1368 if (compression == BI_RGB) {
1369 compression_name = "BI_RGB";
1370 masks = std_masks[pix_size-2];
1372 /* there's a potential "palette" after the header */
1373 for (i = 0; i < clr_used; ++clr_used) {
1375 if (i_io_read(ig, buf, 4) != 4) {
1376 i_push_error(0, "skipping colors");
1382 else if (compression == BI_BITFIELDS) {
1385 compression_name = "BI_BITFIELDS";
1387 for (i = 0; i < 3; ++i) {
1389 if (!read_packed(ig, "V", &rmask)) {
1390 i_push_error(0, "reading pixel masks");
1394 im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i);
1397 masks.masks[i] = rmask;
1398 /* work out a shift for the mask */
1400 bits = masks.masks[i];
1401 while (!(bits & 1)) {
1405 masks.shifts[i] = pos;
1411 masks.bits[i] = pos;
1412 /*fprintf(stderr, "%d: mask %08x shift %d bits %d\n", i, masks.masks[i], masks.shifts[i], masks.bits[i]);*/
1414 /* account for the masks */
1415 base_offset += 3 * 4;
1418 im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression);
1422 if (offbits < base_offset) {
1423 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1427 if (offbits > base_offset) {
1428 /* this will be slow if the offset is large, but that should be
1431 while (base_offset < offbits) {
1432 if (i_io_read(ig, &buffer, 1) != 1) {
1433 i_push_error(0, "failed skipping to image data offset");
1440 im = i_img_empty(NULL, xsize, ysize);
1444 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1446 /* I wasn't able to make this overflow in testing, but better to be
1448 bytes = sizeof(i_color) * xsize;
1449 if (bytes / sizeof(i_color) != xsize) {
1451 i_push_error(0, "integer overflow calculating buffer size");
1454 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1455 while (y != lasty) {
1457 for (x = 0; x < xsize; ++x) {
1459 if (!read_packed(ig, unpack_code, &pixel)) {
1461 if (allow_incomplete) {
1462 i_tags_setn(&im->tags, "i_incomplete", 1);
1463 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1467 i_push_error(0, "failed reading image data");
1472 for (i = 0; i < 3; ++i) {
1473 int sample = (pixel & masks.masks[i]) >> masks.shifts[i];
1474 int bits = masks.bits[i];
1476 sample = (sample * samp_converts[bits-1].mult) >> samp_converts[bits-1].shift;
1479 sample >>= bits - 8;
1481 p->channel[i] = sample;
1485 i_plin(im, 0, xsize, y, line);
1487 i_io_read(ig, junk, extras);
1502 Tony Cook <tony@develop-help.com>
1506 Cannot save as compressed BMP.
1510 Doesn't handle OS/2 bitmaps.
1512 16-bit/pixel images haven't been tested. (I need an image).
1514 BI_BITFIELDS compression hasn't been tested (I need an image).
1516 The header handling for paletted images needs to be refactored