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);
294 =item write_packed(ig, format, ...)
296 Writes packed data to the specified io_glue.
298 Returns non-zero on success.
304 write_packed(io_glue *ig, char *format, ...) {
305 unsigned char buf[4];
309 va_start(ap, format);
312 i = va_arg(ap, i_upacked_t);
318 if (i_io_write(ig, buf, 2) == -1)
324 buf[1] = (i >> 8) & 0xFF;
325 buf[2] = (i >> 16) & 0xFF;
326 buf[3] = (i >> 24) & 0xFF;
327 if (i_io_write(ig, buf, 4) == -1)
334 if (i_io_write(ig, buf, 1) == -1)
341 im_fatal(aIMCTX, 1, "Unknown write_packed format code 0x%02x", *format);
356 =item write_bmphead(ig, im, bit_count, data_size)
358 Writes a Windows BMP header to the file.
360 Returns non-zero on success.
366 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
368 int got_xres, got_yres, aspect_only;
370 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
373 if (im->xsize > SIGNMAX32 || im->ysize > SIGNMAX32) {
374 i_push_error(0, "image too large to write to BMP");
378 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
379 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
380 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
392 if (xres <= 0 || yres <= 0)
395 /* scale so the smaller value is 72 */
406 /* now to pels/meter */
410 if (im->type == i_palette_type) {
411 colors_used = i_colorcount(im);
412 offset += 4 * colors_used;
415 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M',
416 (i_upacked_t)(data_size+offset),
417 (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset,
418 (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize,
419 (i_upacked_t)im->ysize, (i_upacked_t)1,
420 (i_upacked_t)bit_count, (i_upacked_t)BI_RGB,
421 (i_upacked_t)data_size,
422 (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5),
423 (i_upacked_t)colors_used, (i_upacked_t)colors_used)){
424 i_push_error(0, "cannot write bmp header");
427 if (im->type == i_palette_type) {
431 for (i = 0; i < colors_used; ++i) {
432 i_getcolors(im, i, &c, 1);
433 if (im->channels >= 3) {
434 if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]),
435 (i_upacked_t)(c.channel[1]),
436 (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) {
437 i_push_error(0, "cannot write palette entry");
442 i_upacked_t v = c.channel[0];
443 if (!write_packed(ig, "CCCC", v, v, v, 0)) {
444 i_push_error(0, "cannot write palette entry");
455 =item write_1bit_data(ig, im)
457 Writes the image data as a 1-bit/pixel image.
459 Returns non-zero on success.
464 write_1bit_data(io_glue *ig, i_img *im) {
466 unsigned char *packed;
470 int line_size = (im->xsize+7) / 8;
475 /* round up to nearest multiple of four */
476 line_size = (line_size + 3) / 4 * 4;
478 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
481 /* this shouldn't be an issue, but let's be careful */
482 unpacked_size = im->xsize + 8;
483 if (unpacked_size < im->xsize) {
484 i_push_error(0, "integer overflow during memory allocation");
487 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
488 memset(line + im->xsize, 0, 8);
490 /* size allocated here is always much smaller than xsize, hence
491 can't overflow int */
492 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
493 memset(packed, 0, line_size);
495 for (y = im->ysize-1; y >= 0; --y) {
496 i_gpal(im, 0, im->xsize, y, line);
500 for (x = 0; x < im->xsize; ++x) {
503 if ((mask >>= 1) == 0) {
512 if (i_io_write(ig, packed, line_size) < 0) {
515 i_push_error(0, "writing 1 bit/pixel packed data");
529 =item write_4bit_data(ig, im)
531 Writes the image data as a 4-bit/pixel image.
533 Returns non-zero on success.
538 write_4bit_data(io_glue *ig, i_img *im) {
540 unsigned char *packed;
542 int line_size = (im->xsize+1) / 2;
547 /* round up to nearest multiple of four */
548 line_size = (line_size + 3) / 4 * 4;
550 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
553 /* this shouldn't be an issue, but let's be careful */
554 unpacked_size = im->xsize + 2;
555 if (unpacked_size < im->xsize) {
556 i_push_error(0, "integer overflow during memory allocation");
559 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
560 memset(line + im->xsize, 0, 2);
562 /* size allocated here is always much smaller than xsize, hence
563 can't overflow int */
564 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
565 memset(packed, 0, line_size);
567 for (y = im->ysize-1; y >= 0; --y) {
568 i_gpal(im, 0, im->xsize, y, line);
570 for (x = 0; x < im->xsize; x += 2) {
571 *out++ = (line[x] << 4) + line[x+1];
573 if (i_io_write(ig, packed, line_size) < 0) {
576 i_push_error(0, "writing 4 bit/pixel packed data");
590 =item write_8bit_data(ig, im)
592 Writes the image data as a 8-bit/pixel image.
594 Returns non-zero on success.
599 write_8bit_data(io_glue *ig, i_img *im) {
601 int line_size = im->xsize;
606 /* round up to nearest multiple of four */
607 line_size = (line_size + 3) / 4 * 4;
609 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
612 /* this shouldn't be an issue, but let's be careful */
613 unpacked_size = im->xsize + 4;
614 if (unpacked_size < im->xsize) {
615 i_push_error(0, "integer overflow during memory allocation");
618 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
619 memset(line + im->xsize, 0, 4);
621 for (y = im->ysize-1; y >= 0; --y) {
622 i_gpal(im, 0, im->xsize, y, line);
623 if (i_io_write(ig, line, line_size) < 0) {
625 i_push_error(0, "writing 8 bit/pixel packed data");
638 =item write_24bit_data(ig, im)
640 Writes the image data as a 24-bit/pixel image.
642 Returns non-zero on success.
647 write_24bit_data(io_glue *ig, i_img *im) {
648 unsigned char *samples;
650 int line_size = 3 * im->xsize;
654 i_get_file_background(im, &bg);
656 /* just in case we implement a direct format with 2bytes/pixel
658 if (line_size / 3 != im->xsize) {
659 i_push_error(0, "integer overflow during memory allocation");
663 line_size = (line_size + 3) / 4 * 4;
665 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
667 samples = mymalloc(4 * im->xsize);
668 memset(samples, 0, line_size);
669 for (y = im->ysize-1; y >= 0; --y) {
670 unsigned char *samplep = samples;
672 i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg);
673 for (x = 0; x < im->xsize; ++x) {
674 unsigned char tmp = samplep[2];
675 samplep[2] = samplep[0];
679 if (i_io_write(ig, samples, line_size) < 0) {
680 i_push_error(0, "writing image data");
694 =item read_bmp_pal(ig, im, count)
696 Reads count palette entries from the file and add them to the image.
698 Returns non-zero on success.
703 read_bmp_pal(io_glue *ig, i_img *im, int count) {
705 i_packed_t r, g, b, x;
709 for (i = 0; i < count; ++i) {
710 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
711 i_push_error(0, "reading BMP palette");
717 if (i_addcolors(im, &c, 1) < 0) {
718 i_push_error(0, "out of space in image palette");
727 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
729 Reads in the palette and image data for a 1-bit/pixel image.
731 Returns the image or NULL.
736 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
737 int compression, long offbits, int allow_incomplete) {
739 int x, y, lasty, yinc, start_y;
741 unsigned char *packed;
742 int line_size = (xsize + 7)/8;
748 if (compression != BI_RGB) {
749 im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression);
753 if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */
754 /* we check with 8 because we allocate that much for the decoded
756 i_push_error(0, "integer overflow during memory allocation");
760 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
761 adjustments below won't make it overflow */
762 line_size = (line_size+3) / 4 * 4;
770 /* when ysize is -ve it's a top-down image */
779 if (clr_used < 0 || clr_used > 2) {
780 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
784 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
785 if (offbits < base_offset) {
786 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
790 im = i_img_pal_new(xsize, ysize, 3, 256);
793 if (!read_bmp_pal(ig, im, clr_used)) {
798 if (offbits > base_offset) {
799 /* this will be slow if the offset is large, but that should be
802 while (base_offset < offbits) {
803 if (i_io_read(ig, &buffer, 1) != 1) {
805 i_push_error(0, "failed skipping to image data offset");
812 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
814 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
815 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
817 if (i_io_read(ig, packed, line_size) != line_size) {
820 if (allow_incomplete) {
821 i_tags_setn(&im->tags, "i_incomplete", 1);
822 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
826 i_push_error(0, "failed reading 1-bit bmp data");
834 for (x = 0; x < xsize; ++x) {
835 *p++ = (*in & bit) ? 1 : 0;
842 i_ppal(im, 0, xsize, y, line);
852 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
854 Reads in the palette and image data for a 4-bit/pixel image.
856 Returns the image or NULL.
858 Hopefully this will be combined with the following function at some
864 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
865 int compression, long offbits, int allow_incomplete) {
867 int x, y, lasty, yinc;
869 unsigned char *packed;
870 int line_size = (xsize + 1)/2;
877 /* line_size is going to be smaller than xsize in most cases (and
878 when it's not, xsize is itself small), and hence not overflow */
879 line_size = (line_size+3) / 4 * 4;
887 /* when ysize is -ve it's a top-down image */
897 if (clr_used > 16 || clr_used < 0) {
898 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
902 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
903 if (offbits < base_offset) {
904 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
908 im = i_img_pal_new(xsize, ysize, 3, 256);
909 if (!im) /* error should have been pushed already */
911 if (!read_bmp_pal(ig, im, clr_used)) {
916 if (offbits > base_offset) {
917 /* this will be slow if the offset is large, but that should be
920 while (base_offset < offbits) {
921 if (i_io_read(ig, &buffer, 1) != 1) {
923 i_push_error(0, "failed skipping to image data offset");
931 packed = mymalloc(260); /* checked 29jun05 tonyc */
933 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
934 /* xsize won't approach MAXINT */
935 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
936 if (compression == BI_RGB) {
937 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
939 if (i_io_read(ig, packed, line_size) != line_size) {
942 if (allow_incomplete) {
943 i_tags_setn(&im->tags, "i_incomplete", 1);
944 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
948 i_push_error(0, "failed reading 4-bit bmp data");
955 for (x = 0; x < xsize; x+=2) {
960 i_ppal(im, 0, xsize, y, line);
966 else if (compression == BI_RLE4) {
969 i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */
971 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
974 /* there's always at least 2 bytes in a sequence */
975 if (i_io_read(ig, packed, 2) != 2) {
978 if (allow_incomplete) {
979 i_tags_setn(&im->tags, "i_incomplete", 1);
980 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
984 i_push_error(0, "missing data during decompression");
989 else if (packed[0]) {
990 int count = packed[0];
991 if (x + count > xlimit) {
992 /* this file is corrupt */
995 i_push_error(0, "invalid data during decompression");
996 im_log((aIMCTX, 1, "read 4-bit: scanline overflow x %d + count %d vs xlimit %d (y %d)\n",
997 (int)x, count, (int)xlimit, (int)y));
1001 /* fill in the line */
1002 for (i = 0; i < count; i += 2)
1003 line[i] = packed[1] >> 4;
1004 for (i = 1; i < count; i += 2)
1005 line[i] = packed[1] & 0x0F;
1006 i_ppal(im, x, x+count, y, line);
1009 switch (packed[1]) {
1010 case BMPRLE_ENDOFLINE:
1015 case BMPRLE_ENDOFBMP:
1021 if (i_io_read(ig, packed, 2) != 2) {
1024 if (allow_incomplete) {
1025 i_tags_setn(&im->tags, "i_incomplete", 1);
1026 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1030 i_push_error(0, "missing data during decompression");
1036 y += yinc * packed[1];
1041 if (x + count > xlimit) {
1042 /* this file is corrupt */
1045 i_push_error(0, "invalid data during decompression");
1046 im_log((aIMCTX, 1, "read 4-bit: scanline overflow (unpacked) x %d + count %d vs xlimit %d (y %d)\n",
1047 (int)x, count, (int)xlimit, (int)y));
1051 size = (count + 1) / 2;
1052 read_size = (size+1) / 2 * 2;
1053 if (i_io_read(ig, packed, read_size) != read_size) {
1056 if (allow_incomplete) {
1057 i_tags_setn(&im->tags, "i_incomplete", 1);
1058 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1062 i_push_error(0, "missing data during decompression");
1067 for (i = 0; i < size; ++i) {
1068 line[0] = packed[i] >> 4;
1069 line[1] = packed[i] & 0xF;
1070 i_ppal(im, x, x+2, y, line);
1078 else { /*if (compression == BI_RLE4) {*/
1081 im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression);
1090 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1092 Reads in the palette and image data for a 8-bit/pixel image.
1094 Returns the image or NULL.
1099 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1100 int compression, long offbits, int allow_incomplete) {
1102 int x, y, lasty, yinc, start_y;
1104 int line_size = xsize;
1108 line_size = (line_size+3) / 4 * 4;
1109 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1110 i_push_error(0, "integer overflow during memory allocation");
1120 /* when ysize is -ve it's a top-down image */
1129 if (clr_used > 256 || clr_used < 0) {
1130 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
1134 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1135 if (offbits < base_offset) {
1136 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1140 im = i_img_pal_new(xsize, ysize, 3, 256);
1143 if (!read_bmp_pal(ig, im, clr_used)) {
1148 if (offbits > base_offset) {
1149 /* this will be slow if the offset is large, but that should be
1152 while (base_offset < offbits) {
1153 if (i_io_read(ig, &buffer, 1) != 1) {
1155 i_push_error(0, "failed skipping to image data offset");
1162 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1163 if (compression == BI_RGB) {
1164 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1165 while (y != lasty) {
1166 if (i_io_read(ig, line, line_size) != line_size) {
1168 if (allow_incomplete) {
1169 i_tags_setn(&im->tags, "i_incomplete", 1);
1170 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1174 i_push_error(0, "failed reading 8-bit bmp data");
1179 i_ppal(im, 0, xsize, y, line);
1184 else if (compression == BI_RLE8) {
1187 unsigned char packed[2];
1189 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1192 /* there's always at least 2 bytes in a sequence */
1193 if (i_io_read(ig, packed, 2) != 2) {
1195 if (allow_incomplete) {
1196 i_tags_setn(&im->tags, "i_incomplete", 1);
1197 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1201 i_push_error(0, "missing data during decompression");
1207 if (x + packed[0] > xsize) {
1208 /* this file isn't incomplete, it's corrupt */
1210 i_push_error(0, "invalid data during decompression");
1214 memset(line, packed[1], packed[0]);
1215 i_ppal(im, x, x+packed[0], y, line);
1218 switch (packed[1]) {
1219 case BMPRLE_ENDOFLINE:
1224 case BMPRLE_ENDOFBMP:
1229 if (i_io_read(ig, packed, 2) != 2) {
1231 if (allow_incomplete) {
1232 i_tags_setn(&im->tags, "i_incomplete", 1);
1233 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1237 i_push_error(0, "missing data during decompression");
1243 y += yinc * packed[1];
1248 if (x + count > xsize) {
1249 /* runs shouldn't cross a line boundary */
1250 /* this file isn't incomplete, it's corrupt */
1252 i_push_error(0, "invalid data during decompression");
1256 read_size = (count+1) / 2 * 2;
1257 if (i_io_read(ig, line, read_size) != read_size) {
1259 if (allow_incomplete) {
1260 i_tags_setn(&im->tags, "i_incomplete", 1);
1261 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1265 i_push_error(0, "missing data during decompression");
1270 i_ppal(im, x, x+count, y, line);
1279 im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression);
1292 static struct bm_masks std_masks[] =
1295 { 0076000, 00001740, 00000037, },
1300 { 0xFF0000, 0x00FF00, 0x0000FF, },
1305 { 0xFF0000, 0x00FF00, 0x0000FF, },
1311 /* multiplier and shift for converting from N bits to 8 bits */
1312 struct bm_sampconverts {
1316 static struct bm_sampconverts samp_converts[] = {
1317 { 0xff, 0 }, /* 1 bit samples */
1323 { 0x81, 6 } /* 7 bit samples */
1327 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1329 Skips the palette and reads in the image data for a direct colour image.
1331 Returns the image or NULL.
1336 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1337 int clr_used, int compression, long offbits,
1338 int allow_incomplete) {
1340 int x, y, starty, lasty, yinc;
1342 int pix_size = bit_count / 8;
1343 int line_size = xsize * pix_size;
1344 struct bm_masks masks;
1345 char unpack_code[2] = "";
1349 const char *compression_name;
1351 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1354 unpack_code[0] = *("v3V"+pix_size-2);
1355 unpack_code[1] = '\0';
1357 line_size = (line_size+3) / 4 * 4;
1358 extras = line_size - xsize * pix_size;
1366 /* when ysize is -ve it's a top-down image */
1373 if (compression == BI_RGB) {
1374 compression_name = "BI_RGB";
1375 masks = std_masks[pix_size-2];
1377 /* there's a potential "palette" after the header */
1378 for (i = 0; i < clr_used; ++clr_used) {
1380 if (i_io_read(ig, buf, 4) != 4) {
1381 i_push_error(0, "skipping colors");
1387 else if (compression == BI_BITFIELDS) {
1390 compression_name = "BI_BITFIELDS";
1392 for (i = 0; i < 3; ++i) {
1394 if (!read_packed(ig, "V", &rmask)) {
1395 i_push_error(0, "reading pixel masks");
1399 im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i);
1402 masks.masks[i] = rmask;
1403 /* work out a shift for the mask */
1405 bits = masks.masks[i];
1406 while (!(bits & 1)) {
1410 masks.shifts[i] = pos;
1416 masks.bits[i] = pos;
1417 /*fprintf(stderr, "%d: mask %08x shift %d bits %d\n", i, masks.masks[i], masks.shifts[i], masks.bits[i]);*/
1419 /* account for the masks */
1420 base_offset += 3 * 4;
1423 im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression);
1427 if (offbits < base_offset) {
1428 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1432 if (offbits > base_offset) {
1433 /* this will be slow if the offset is large, but that should be
1436 while (base_offset < offbits) {
1437 if (i_io_read(ig, &buffer, 1) != 1) {
1438 i_push_error(0, "failed skipping to image data offset");
1445 im = i_img_empty(NULL, xsize, ysize);
1449 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1451 /* I wasn't able to make this overflow in testing, but better to be
1453 bytes = sizeof(i_color) * xsize;
1454 if (bytes / sizeof(i_color) != xsize) {
1456 i_push_error(0, "integer overflow calculating buffer size");
1459 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1460 while (y != lasty) {
1462 for (x = 0; x < xsize; ++x) {
1464 if (!read_packed(ig, unpack_code, &pixel)) {
1466 if (allow_incomplete) {
1467 i_tags_setn(&im->tags, "i_incomplete", 1);
1468 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1472 i_push_error(0, "failed reading image data");
1477 for (i = 0; i < 3; ++i) {
1478 int sample = (pixel & masks.masks[i]) >> masks.shifts[i];
1479 int bits = masks.bits[i];
1481 sample = (sample * samp_converts[bits-1].mult) >> samp_converts[bits-1].shift;
1484 sample >>= bits - 8;
1486 p->channel[i] = sample;
1490 i_plin(im, 0, xsize, y, line);
1492 i_io_read(ig, junk, extras);
1507 Tony Cook <tony@develop-help.com>
1511 Cannot save as compressed BMP.
1515 Doesn't handle OS/2 bitmaps.
1517 16-bit/pixel images haven't been tested. (I need an image).
1519 BI_BITFIELDS compression hasn't been tested (I need an image).
1521 The header handling for paletted images needs to be refactored