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);
347 =item write_bmphead(ig, im, bit_count, data_size)
349 Writes a Windows BMP header to the file.
351 Returns non-zero on success.
357 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
359 int got_xres, got_yres, aspect_only;
361 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
364 if (im->xsize > SIGNMAX32 || im->ysize > SIGNMAX32) {
365 i_push_error(0, "image too large to write to BMP");
369 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
370 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
371 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
383 if (xres <= 0 || yres <= 0)
386 /* scale so the smaller value is 72 */
397 /* now to pels/meter */
401 if (im->type == i_palette_type) {
402 colors_used = i_colorcount(im);
403 offset += 4 * colors_used;
406 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M',
407 (i_upacked_t)(data_size+offset),
408 (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset,
409 (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize,
410 (i_upacked_t)im->ysize, (i_upacked_t)1,
411 (i_upacked_t)bit_count, (i_upacked_t)BI_RGB,
412 (i_upacked_t)data_size,
413 (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5),
414 (i_upacked_t)colors_used, (i_upacked_t)colors_used)){
415 i_push_error(0, "cannot write bmp header");
418 if (im->type == i_palette_type) {
422 for (i = 0; i < colors_used; ++i) {
423 i_getcolors(im, i, &c, 1);
424 if (im->channels >= 3) {
425 if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]),
426 (i_upacked_t)(c.channel[1]),
427 (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) {
428 i_push_error(0, "cannot write palette entry");
433 i_upacked_t v = c.channel[0];
434 if (!write_packed(ig, "CCCC", v, v, v, 0)) {
435 i_push_error(0, "cannot write palette entry");
446 =item write_1bit_data(ig, im)
448 Writes the image data as a 1-bit/pixel image.
450 Returns non-zero on success.
455 write_1bit_data(io_glue *ig, i_img *im) {
457 unsigned char *packed;
461 int line_size = (im->xsize+7) / 8;
466 /* round up to nearest multiple of four */
467 line_size = (line_size + 3) / 4 * 4;
469 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
472 /* this shouldn't be an issue, but let's be careful */
473 unpacked_size = im->xsize + 8;
474 if (unpacked_size < im->xsize) {
475 i_push_error(0, "integer overflow during memory allocation");
478 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
479 memset(line + im->xsize, 0, 8);
481 /* size allocated here is always much smaller than xsize, hence
482 can't overflow int */
483 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
484 memset(packed, 0, line_size);
486 for (y = im->ysize-1; y >= 0; --y) {
487 i_gpal(im, 0, im->xsize, y, line);
491 for (x = 0; x < im->xsize; ++x) {
494 if ((mask >>= 1) == 0) {
503 if (i_io_write(ig, packed, line_size) < 0) {
506 i_push_error(0, "writing 1 bit/pixel packed data");
520 =item write_4bit_data(ig, im)
522 Writes the image data as a 4-bit/pixel image.
524 Returns non-zero on success.
529 write_4bit_data(io_glue *ig, i_img *im) {
531 unsigned char *packed;
533 int line_size = (im->xsize+1) / 2;
538 /* round up to nearest multiple of four */
539 line_size = (line_size + 3) / 4 * 4;
541 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
544 /* this shouldn't be an issue, but let's be careful */
545 unpacked_size = im->xsize + 2;
546 if (unpacked_size < im->xsize) {
547 i_push_error(0, "integer overflow during memory allocation");
550 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
551 memset(line + im->xsize, 0, 2);
553 /* size allocated here is always much smaller than xsize, hence
554 can't overflow int */
555 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
556 memset(packed, 0, line_size);
558 for (y = im->ysize-1; y >= 0; --y) {
559 i_gpal(im, 0, im->xsize, y, line);
561 for (x = 0; x < im->xsize; x += 2) {
562 *out++ = (line[x] << 4) + line[x+1];
564 if (i_io_write(ig, packed, line_size) < 0) {
567 i_push_error(0, "writing 4 bit/pixel packed data");
581 =item write_8bit_data(ig, im)
583 Writes the image data as a 8-bit/pixel image.
585 Returns non-zero on success.
590 write_8bit_data(io_glue *ig, i_img *im) {
592 int line_size = im->xsize;
597 /* round up to nearest multiple of four */
598 line_size = (line_size + 3) / 4 * 4;
600 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
603 /* this shouldn't be an issue, but let's be careful */
604 unpacked_size = im->xsize + 4;
605 if (unpacked_size < im->xsize) {
606 i_push_error(0, "integer overflow during memory allocation");
609 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
610 memset(line + im->xsize, 0, 4);
612 for (y = im->ysize-1; y >= 0; --y) {
613 i_gpal(im, 0, im->xsize, y, line);
614 if (i_io_write(ig, line, line_size) < 0) {
616 i_push_error(0, "writing 8 bit/pixel packed data");
629 =item write_24bit_data(ig, im)
631 Writes the image data as a 24-bit/pixel image.
633 Returns non-zero on success.
638 write_24bit_data(io_glue *ig, i_img *im) {
639 unsigned char *samples;
641 int line_size = 3 * im->xsize;
645 i_get_file_background(im, &bg);
647 /* just in case we implement a direct format with 2bytes/pixel
649 if (line_size / 3 != im->xsize) {
650 i_push_error(0, "integer overflow during memory allocation");
654 line_size = (line_size + 3) / 4 * 4;
656 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
658 samples = mymalloc(4 * im->xsize);
659 memset(samples, 0, line_size);
660 for (y = im->ysize-1; y >= 0; --y) {
661 unsigned char *samplep = samples;
663 i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg);
664 for (x = 0; x < im->xsize; ++x) {
665 unsigned char tmp = samplep[2];
666 samplep[2] = samplep[0];
670 if (i_io_write(ig, samples, line_size) < 0) {
671 i_push_error(0, "writing image data");
685 =item read_bmp_pal(ig, im, count)
687 Reads count palette entries from the file and add them to the image.
689 Returns non-zero on success.
694 read_bmp_pal(io_glue *ig, i_img *im, int count) {
696 i_packed_t r, g, b, x;
700 for (i = 0; i < count; ++i) {
701 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
702 i_push_error(0, "reading BMP palette");
708 if (i_addcolors(im, &c, 1) < 0) {
709 i_push_error(0, "out of space in image palette");
718 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
720 Reads in the palette and image data for a 1-bit/pixel image.
722 Returns the image or NULL.
727 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
728 int compression, long offbits, int allow_incomplete) {
730 int x, y, lasty, yinc, start_y;
732 unsigned char *packed;
733 int line_size = (xsize + 7)/8;
739 if (compression != BI_RGB) {
740 im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression);
744 if (xsize + 8 < xsize) { /* if there was overflow */
745 /* we check with 8 because we allocate that much for the decoded
747 i_push_error(0, "integer overflow during memory allocation");
751 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
752 adjustments below won't make it overflow */
753 line_size = (line_size+3) / 4 * 4;
761 /* when ysize is -ve it's a top-down image */
770 if (clr_used < 0 || clr_used > 2) {
771 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
775 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
776 if (offbits < base_offset) {
777 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
781 im = i_img_pal_new(xsize, ysize, 3, 256);
784 if (!read_bmp_pal(ig, im, clr_used)) {
789 if (offbits > base_offset) {
790 /* this will be slow if the offset is large, but that should be
793 while (base_offset < offbits) {
794 if (i_io_read(ig, &buffer, 1) != 1) {
796 i_push_error(0, "failed skipping to image data offset");
803 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
805 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
806 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
808 if (i_io_read(ig, packed, line_size) != line_size) {
811 if (allow_incomplete) {
812 i_tags_setn(&im->tags, "i_incomplete", 1);
813 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
817 i_push_error(0, "failed reading 1-bit bmp data");
825 for (x = 0; x < xsize; ++x) {
826 *p++ = (*in & bit) ? 1 : 0;
833 i_ppal(im, 0, xsize, y, line);
843 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
845 Reads in the palette and image data for a 4-bit/pixel image.
847 Returns the image or NULL.
849 Hopefully this will be combined with the following function at some
855 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
856 int compression, long offbits, int allow_incomplete) {
858 int x, y, lasty, yinc;
860 unsigned char *packed;
861 int line_size = (xsize + 1)/2;
868 /* line_size is going to be smaller than xsize in most cases (and
869 when it's not, xsize is itself small), and hence not overflow */
870 line_size = (line_size+3) / 4 * 4;
878 /* when ysize is -ve it's a top-down image */
888 if (clr_used > 16 || clr_used < 0) {
889 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
893 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
894 if (offbits < base_offset) {
895 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
899 im = i_img_pal_new(xsize, ysize, 3, 256);
900 if (!im) /* error should have been pushed already */
902 if (!read_bmp_pal(ig, im, clr_used)) {
907 if (offbits > base_offset) {
908 /* this will be slow if the offset is large, but that should be
911 while (base_offset < offbits) {
912 if (i_io_read(ig, &buffer, 1) != 1) {
914 i_push_error(0, "failed skipping to image data offset");
922 packed = mymalloc(260); /* checked 29jun05 tonyc */
924 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
925 /* xsize won't approach MAXINT */
926 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
927 if (compression == BI_RGB) {
928 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
930 if (i_io_read(ig, packed, line_size) != line_size) {
933 if (allow_incomplete) {
934 i_tags_setn(&im->tags, "i_incomplete", 1);
935 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
939 i_push_error(0, "failed reading 4-bit bmp data");
946 for (x = 0; x < xsize; x+=2) {
951 i_ppal(im, 0, xsize, y, line);
957 else if (compression == BI_RLE4) {
960 i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */
962 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
965 /* there's always at least 2 bytes in a sequence */
966 if (i_io_read(ig, packed, 2) != 2) {
969 if (allow_incomplete) {
970 i_tags_setn(&im->tags, "i_incomplete", 1);
971 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
975 i_push_error(0, "missing data during decompression");
980 else if (packed[0]) {
981 int count = packed[0];
982 if (x + count > xlimit) {
983 /* this file is corrupt */
986 i_push_error(0, "invalid data during decompression");
987 im_log((aIMCTX, 1, "read 4-bit: scanline overflow x %d + count %d vs xlimit %d (y %d)\n",
988 (int)x, count, (int)xlimit, (int)y));
992 /* fill in the line */
993 for (i = 0; i < count; i += 2)
994 line[i] = packed[1] >> 4;
995 for (i = 1; i < count; i += 2)
996 line[i] = packed[1] & 0x0F;
997 i_ppal(im, x, x+count, y, line);
1000 switch (packed[1]) {
1001 case BMPRLE_ENDOFLINE:
1006 case BMPRLE_ENDOFBMP:
1012 if (i_io_read(ig, packed, 2) != 2) {
1015 if (allow_incomplete) {
1016 i_tags_setn(&im->tags, "i_incomplete", 1);
1017 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1021 i_push_error(0, "missing data during decompression");
1027 y += yinc * packed[1];
1032 if (x + count > xlimit) {
1033 /* this file is corrupt */
1036 i_push_error(0, "invalid data during decompression");
1037 im_log((aIMCTX, 1, "read 4-bit: scanline overflow (unpacked) x %d + count %d vs xlimit %d (y %d)\n",
1038 (int)x, count, (int)xlimit, (int)y));
1042 size = (count + 1) / 2;
1043 read_size = (size+1) / 2 * 2;
1044 if (i_io_read(ig, packed, read_size) != read_size) {
1047 if (allow_incomplete) {
1048 i_tags_setn(&im->tags, "i_incomplete", 1);
1049 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1053 i_push_error(0, "missing data during decompression");
1058 for (i = 0; i < size; ++i) {
1059 line[0] = packed[i] >> 4;
1060 line[1] = packed[i] & 0xF;
1061 i_ppal(im, x, x+2, y, line);
1069 else { /*if (compression == BI_RLE4) {*/
1072 im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression);
1081 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1083 Reads in the palette and image data for a 8-bit/pixel image.
1085 Returns the image or NULL.
1090 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1091 int compression, long offbits, int allow_incomplete) {
1093 int x, y, lasty, yinc, start_y;
1095 int line_size = xsize;
1099 line_size = (line_size+3) / 4 * 4;
1100 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1101 i_push_error(0, "integer overflow during memory allocation");
1111 /* when ysize is -ve it's a top-down image */
1120 if (clr_used > 256 || clr_used < 0) {
1121 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
1125 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1126 if (offbits < base_offset) {
1127 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1131 im = i_img_pal_new(xsize, ysize, 3, 256);
1134 if (!read_bmp_pal(ig, im, clr_used)) {
1139 if (offbits > base_offset) {
1140 /* this will be slow if the offset is large, but that should be
1143 while (base_offset < offbits) {
1144 if (i_io_read(ig, &buffer, 1) != 1) {
1146 i_push_error(0, "failed skipping to image data offset");
1153 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1154 if (compression == BI_RGB) {
1155 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1156 while (y != lasty) {
1157 if (i_io_read(ig, line, line_size) != line_size) {
1159 if (allow_incomplete) {
1160 i_tags_setn(&im->tags, "i_incomplete", 1);
1161 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1165 i_push_error(0, "failed reading 8-bit bmp data");
1170 i_ppal(im, 0, xsize, y, line);
1175 else if (compression == BI_RLE8) {
1178 unsigned char packed[2];
1180 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1183 /* there's always at least 2 bytes in a sequence */
1184 if (i_io_read(ig, packed, 2) != 2) {
1186 if (allow_incomplete) {
1187 i_tags_setn(&im->tags, "i_incomplete", 1);
1188 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1192 i_push_error(0, "missing data during decompression");
1198 if (x + packed[0] > xsize) {
1199 /* this file isn't incomplete, it's corrupt */
1201 i_push_error(0, "invalid data during decompression");
1205 memset(line, packed[1], packed[0]);
1206 i_ppal(im, x, x+packed[0], y, line);
1209 switch (packed[1]) {
1210 case BMPRLE_ENDOFLINE:
1215 case BMPRLE_ENDOFBMP:
1220 if (i_io_read(ig, packed, 2) != 2) {
1222 if (allow_incomplete) {
1223 i_tags_setn(&im->tags, "i_incomplete", 1);
1224 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1228 i_push_error(0, "missing data during decompression");
1234 y += yinc * packed[1];
1239 if (x + count > xsize) {
1240 /* runs shouldn't cross a line boundary */
1241 /* this file isn't incomplete, it's corrupt */
1243 i_push_error(0, "invalid data during decompression");
1247 read_size = (count+1) / 2 * 2;
1248 if (i_io_read(ig, line, read_size) != read_size) {
1250 if (allow_incomplete) {
1251 i_tags_setn(&im->tags, "i_incomplete", 1);
1252 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1256 i_push_error(0, "missing data during decompression");
1261 i_ppal(im, x, x+count, y, line);
1270 im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression);
1283 static struct bm_masks std_masks[] =
1286 { 0076000, 00001740, 00000037, },
1291 { 0xFF0000, 0x00FF00, 0x0000FF, },
1296 { 0xFF0000, 0x00FF00, 0x0000FF, },
1302 /* multiplier and shift for converting from N bits to 8 bits */
1303 struct bm_sampconverts {
1307 static struct bm_sampconverts samp_converts[] = {
1308 { 0xff, 0 }, /* 1 bit samples */
1314 { 0x81, 6 } /* 7 bit samples */
1318 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1320 Skips the palette and reads in the image data for a direct colour image.
1322 Returns the image or NULL.
1327 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1328 int clr_used, int compression, long offbits,
1329 int allow_incomplete) {
1331 int x, y, starty, lasty, yinc;
1333 int pix_size = bit_count / 8;
1334 int line_size = xsize * pix_size;
1335 struct bm_masks masks;
1336 char unpack_code[2] = "";
1340 const char *compression_name;
1342 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1345 unpack_code[0] = *("v3V"+pix_size-2);
1346 unpack_code[1] = '\0';
1348 line_size = (line_size+3) / 4 * 4;
1349 extras = line_size - xsize * pix_size;
1357 /* when ysize is -ve it's a top-down image */
1364 if (compression == BI_RGB) {
1365 compression_name = "BI_RGB";
1366 masks = std_masks[pix_size-2];
1368 /* there's a potential "palette" after the header */
1369 for (i = 0; i < clr_used; ++clr_used) {
1371 if (i_io_read(ig, buf, 4) != 4) {
1372 i_push_error(0, "skipping colors");
1378 else if (compression == BI_BITFIELDS) {
1381 compression_name = "BI_BITFIELDS";
1383 for (i = 0; i < 3; ++i) {
1385 if (!read_packed(ig, "V", &rmask)) {
1386 i_push_error(0, "reading pixel masks");
1390 im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i);
1393 masks.masks[i] = rmask;
1394 /* work out a shift for the mask */
1396 bits = masks.masks[i];
1397 while (!(bits & 1)) {
1401 masks.shifts[i] = pos;
1407 masks.bits[i] = pos;
1408 /*fprintf(stderr, "%d: mask %08x shift %d bits %d\n", i, masks.masks[i], masks.shifts[i], masks.bits[i]);*/
1410 /* account for the masks */
1411 base_offset += 3 * 4;
1414 im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression);
1418 if (offbits < base_offset) {
1419 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1423 if (offbits > base_offset) {
1424 /* this will be slow if the offset is large, but that should be
1427 while (base_offset < offbits) {
1428 if (i_io_read(ig, &buffer, 1) != 1) {
1429 i_push_error(0, "failed skipping to image data offset");
1436 im = i_img_empty(NULL, xsize, ysize);
1440 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1442 /* I wasn't able to make this overflow in testing, but better to be
1444 bytes = sizeof(i_color) * xsize;
1445 if (bytes / sizeof(i_color) != xsize) {
1447 i_push_error(0, "integer overflow calculating buffer size");
1450 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1451 while (y != lasty) {
1453 for (x = 0; x < xsize; ++x) {
1455 if (!read_packed(ig, unpack_code, &pixel)) {
1457 if (allow_incomplete) {
1458 i_tags_setn(&im->tags, "i_incomplete", 1);
1459 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1463 i_push_error(0, "failed reading image data");
1468 for (i = 0; i < 3; ++i) {
1469 int sample = (pixel & masks.masks[i]) >> masks.shifts[i];
1470 int bits = masks.bits[i];
1472 sample = (sample * samp_converts[bits-1].mult) >> samp_converts[bits-1].shift;
1475 sample >>= bits - 8;
1477 p->channel[i] = sample;
1481 i_plin(im, 0, xsize, y, line);
1483 i_io_read(ig, junk, extras);
1498 Tony Cook <tony@develop-help.com>
1502 Cannot save as compressed BMP.
1506 Doesn't handle OS/2 bitmaps.
1508 16-bit/pixel images haven't been tested. (I need an image).
1510 BI_BITFIELDS compression hasn't been tested (I need an image).
1512 The header handling for paletted images needs to be refactored