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 #define SIGNBIT32 ((i_upacked_t)1U << 31)
39 #define SIGNBIT16 ((i_upacked_t)1U << 15)
41 #define SIGNMAX32 ((1UL << 31) - 1)
43 static int read_packed(io_glue *ig, char *format, ...);
44 static int write_packed(io_glue *ig, char *format, ...);
45 static int write_bmphead(io_glue *ig, i_img *im, int bit_count,
47 static int write_1bit_data(io_glue *ig, i_img *im);
48 static int write_4bit_data(io_glue *ig, i_img *im);
49 static int write_8bit_data(io_glue *ig, i_img *im);
50 static int write_24bit_data(io_glue *ig, i_img *im);
51 static int read_bmp_pal(io_glue *ig, i_img *im, int count);
52 static i_img *read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
53 int compression, long offbits, int allow_incomplete);
54 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
55 int compression, long offbits, int allow_incomplete);
56 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
57 int compression, long offbits, int allow_incomplete);
58 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
59 int bit_count, int clr_used, int compression,
60 long offbits, int allow_incomplete);
62 /* used for the read_packed() and write_packed() functions, an integer
64 typedef long i_packed_t;
65 typedef unsigned long i_upacked_t;
68 =item i_writebmp_wiol(im, io_glue)
70 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
71 formats depending on the image.
73 Never compresses the image.
78 i_writebmp_wiol(i_img *im, io_glue *ig) {
79 io_glue_commit_types(ig);
83 if (im->type == i_direct_type) {
84 return write_24bit_data(ig, im);
89 /* must be paletted */
90 pal_size = i_colorcount(im);
92 return write_1bit_data(ig, im);
94 else if (pal_size <= 16) {
95 return write_4bit_data(ig, im);
98 return write_8bit_data(ig, im);
104 =item i_readbmp_wiol(ig)
106 Reads a Windows format bitmap from the given file.
108 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
109 BI_BITFIELDS images too, but I need a test image.
115 i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
116 i_packed_t b_magic, m_magic, filesize, res1, res2, infohead_size;
117 i_packed_t xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
118 i_packed_t clr_used, clr_important, offbits;
121 mm_log((1, "i_readbmp_wiol(ig %p)\n", ig));
123 io_glue_commit_types(ig);
126 if (!read_packed(ig, "CCVvvVVV!V!vvVVVVVV", &b_magic, &m_magic, &filesize,
127 &res1, &res2, &offbits, &infohead_size,
128 &xsize, &ysize, &planes,
129 &bit_count, &compression, &size_image, &xres, &yres,
130 &clr_used, &clr_important)) {
131 i_push_error(0, "file too short to be a BMP file");
134 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
136 i_push_error(0, "not a BMP file");
140 mm_log((1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
141 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
142 "clr_important %d\n", (int)filesize, (int)offbits, (int)xsize,
143 (int)ysize, (int)planes, (int)bit_count, (int)compression,
144 (int)size_image, (int)xres, (int)yres, (int)clr_used,
145 (int)clr_important));
147 if (!i_int_check_image_file_limits(xsize, abs(ysize), 3, sizeof(i_sample_t))) {
148 mm_log((1, "i_readbmp_wiol: image size exceeds limits\n"));
154 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
159 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
164 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
171 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
172 offbits, allow_incomplete);
176 i_push_errorf(0, "unknown bit count for BMP file (%d)", (int)bit_count);
181 /* store the resolution */
184 else if (yres && !xres)
187 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
188 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
190 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
191 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
192 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
193 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
194 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
195 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
204 =head1 IMPLEMENTATION FUNCTIONS
206 Internal functions used in the implementation.
210 =item read_packed(ig, format, ...)
212 Reads from the specified "file" the specified sizes. The format codes
213 match those used by perl's pack() function, though only a few are
214 implemented. In all cases the vararg arguement is an int *.
216 Returns non-zero if all of the arguments were read.
221 read_packed(io_glue *ig, char *format, ...) {
222 unsigned char buf[4];
227 int shrieking; /* format code has a ! flag */
229 va_start(ap, format);
232 p = va_arg(ap, i_packed_t *);
235 shrieking = *format == '!';
236 if (shrieking) ++format;
240 if (ig->readcb(ig, buf, 2) != 2)
242 work = buf[0] + ((i_packed_t)buf[1] << 8);
244 *p = (work ^ SIGNBIT16) - SIGNBIT16;
250 if (ig->readcb(ig, buf, 4) != 4)
252 work = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16) + ((i_packed_t)buf[3] << 24);
254 *p = (work ^ SIGNBIT32) - SIGNBIT32;
260 if (ig->readcb(ig, buf, 1) != 1)
266 if (ig->readcb(ig, buf, 1) != 1)
271 case '3': /* extension - 24-bit number */
272 if (ig->readcb(ig, buf, 3) != 3)
274 *p = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16);
278 i_fatal(1, "Unknown read_packed format code 0x%02x", code);
285 =item write_packed(ig, format, ...)
287 Writes packed data to the specified io_glue.
289 Returns non-zero on success.
295 write_packed(io_glue *ig, char *format, ...) {
296 unsigned char buf[4];
300 va_start(ap, format);
303 i = va_arg(ap, i_upacked_t);
309 if (ig->writecb(ig, buf, 2) == -1)
315 buf[1] = (i >> 8) & 0xFF;
316 buf[2] = (i >> 16) & 0xFF;
317 buf[3] = (i >> 24) & 0xFF;
318 if (ig->writecb(ig, buf, 4) == -1)
325 if (ig->writecb(ig, buf, 1) == -1)
330 i_fatal(1, "Unknown write_packed format code 0x%02x", *format);
340 =item write_bmphead(ig, im, bit_count, data_size)
342 Writes a Windows BMP header to the file.
344 Returns non-zero on success.
350 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
352 int got_xres, got_yres, aspect_only;
354 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
356 if (im->xsize > SIGNMAX32 || im->ysize > SIGNMAX32) {
357 i_push_error(0, "image too large to write to BMP");
361 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
362 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
363 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
375 if (xres <= 0 || yres <= 0)
378 /* scale so the smaller value is 72 */
389 /* now to pels/meter */
393 if (im->type == i_palette_type) {
394 colors_used = i_colorcount(im);
395 offset += 4 * colors_used;
398 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M',
399 (i_upacked_t)(data_size+offset),
400 (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset,
401 (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize,
402 (i_upacked_t)im->ysize, (i_upacked_t)1,
403 (i_upacked_t)bit_count, (i_upacked_t)BI_RGB,
404 (i_upacked_t)data_size,
405 (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5),
406 (i_upacked_t)colors_used, (i_upacked_t)colors_used)){
407 i_push_error(0, "cannot write bmp header");
410 if (im->type == i_palette_type) {
414 for (i = 0; i < colors_used; ++i) {
415 i_getcolors(im, i, &c, 1);
416 if (im->channels >= 3) {
417 if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]),
418 (i_upacked_t)(c.channel[1]),
419 (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) {
420 i_push_error(0, "cannot write palette entry");
425 i_upacked_t v = c.channel[0];
426 if (!write_packed(ig, "CCCC", v, v, v, 0)) {
427 i_push_error(0, "cannot write palette entry");
438 =item write_1bit_data(ig, im)
440 Writes the image data as a 1-bit/pixel image.
442 Returns non-zero on success.
447 write_1bit_data(io_glue *ig, i_img *im) {
449 unsigned char *packed;
453 int line_size = (im->xsize+7) / 8;
457 /* round up to nearest multiple of four */
458 line_size = (line_size + 3) / 4 * 4;
460 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
463 /* this shouldn't be an issue, but let's be careful */
464 unpacked_size = im->xsize + 8;
465 if (unpacked_size < im->xsize) {
466 i_push_error(0, "integer overflow during memory allocation");
469 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
470 memset(line + im->xsize, 0, 8);
472 /* size allocated here is always much smaller than xsize, hence
473 can't overflow int */
474 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
475 memset(packed, 0, line_size);
477 for (y = im->ysize-1; y >= 0; --y) {
478 i_gpal(im, 0, im->xsize, y, line);
482 for (x = 0; x < im->xsize; ++x) {
485 if ((mask >>= 1) == 0) {
494 if (ig->writecb(ig, packed, line_size) < 0) {
497 i_push_error(0, "writing 1 bit/pixel packed data");
510 =item write_4bit_data(ig, im)
512 Writes the image data as a 4-bit/pixel image.
514 Returns non-zero on success.
519 write_4bit_data(io_glue *ig, i_img *im) {
521 unsigned char *packed;
523 int line_size = (im->xsize+1) / 2;
527 /* round up to nearest multiple of four */
528 line_size = (line_size + 3) / 4 * 4;
530 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
533 /* this shouldn't be an issue, but let's be careful */
534 unpacked_size = im->xsize + 2;
535 if (unpacked_size < im->xsize) {
536 i_push_error(0, "integer overflow during memory allocation");
539 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
540 memset(line + im->xsize, 0, 2);
542 /* size allocated here is always much smaller than xsize, hence
543 can't overflow int */
544 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
545 memset(packed, 0, line_size);
547 for (y = im->ysize-1; y >= 0; --y) {
548 i_gpal(im, 0, im->xsize, y, line);
550 for (x = 0; x < im->xsize; x += 2) {
551 *out++ = (line[x] << 4) + line[x+1];
553 if (ig->writecb(ig, packed, line_size) < 0) {
556 i_push_error(0, "writing 4 bit/pixel packed data");
569 =item write_8bit_data(ig, im)
571 Writes the image data as a 8-bit/pixel image.
573 Returns non-zero on success.
578 write_8bit_data(io_glue *ig, i_img *im) {
580 int line_size = im->xsize;
584 /* round up to nearest multiple of four */
585 line_size = (line_size + 3) / 4 * 4;
587 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
590 /* this shouldn't be an issue, but let's be careful */
591 unpacked_size = im->xsize + 4;
592 if (unpacked_size < im->xsize) {
593 i_push_error(0, "integer overflow during memory allocation");
596 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
597 memset(line + im->xsize, 0, 4);
599 for (y = im->ysize-1; y >= 0; --y) {
600 i_gpal(im, 0, im->xsize, y, line);
601 if (ig->writecb(ig, line, line_size) < 0) {
603 i_push_error(0, "writing 8 bit/pixel packed data");
615 =item write_24bit_data(ig, im)
617 Writes the image data as a 24-bit/pixel image.
619 Returns non-zero on success.
624 write_24bit_data(io_glue *ig, i_img *im) {
625 unsigned char *samples;
627 int line_size = 3 * im->xsize;
630 i_get_file_background(im, &bg);
632 /* just in case we implement a direct format with 2bytes/pixel
634 if (line_size / 3 != im->xsize) {
635 i_push_error(0, "integer overflow during memory allocation");
639 line_size = (line_size + 3) / 4 * 4;
641 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
643 samples = mymalloc(4 * im->xsize);
644 memset(samples, 0, line_size);
645 for (y = im->ysize-1; y >= 0; --y) {
646 unsigned char *samplep = samples;
648 i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg);
649 for (x = 0; x < im->xsize; ++x) {
650 unsigned char tmp = samplep[2];
651 samplep[2] = samplep[0];
655 if (ig->writecb(ig, samples, line_size) < 0) {
656 i_push_error(0, "writing image data");
669 =item read_bmp_pal(ig, im, count)
671 Reads count palette entries from the file and add them to the image.
673 Returns non-zero on success.
678 read_bmp_pal(io_glue *ig, i_img *im, int count) {
680 i_packed_t r, g, b, x;
683 for (i = 0; i < count; ++i) {
684 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
685 i_push_error(0, "reading BMP palette");
691 if (i_addcolors(im, &c, 1) < 0) {
692 i_push_error(0, "out of space in image palette");
701 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
703 Reads in the palette and image data for a 1-bit/pixel image.
705 Returns the image or NULL.
710 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
711 int compression, long offbits, int allow_incomplete) {
713 int x, y, lasty, yinc, start_y;
715 unsigned char *packed;
716 int line_size = (xsize + 7)/8;
721 if (compression != BI_RGB) {
722 i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
726 if (xsize + 8 < xsize) { /* if there was overflow */
727 /* we check with 8 because we allocate that much for the decoded
729 i_push_error(0, "integer overflow during memory allocation");
733 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
734 adjustments below won't make it overflow */
735 line_size = (line_size+3) / 4 * 4;
743 /* when ysize is -ve it's a top-down image */
752 if (clr_used < 0 || clr_used > 2) {
753 i_push_errorf(0, "out of range colors used (%d)", clr_used);
757 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
758 if (offbits < base_offset) {
759 i_push_errorf(0, "image data offset too small (%ld)", offbits);
763 im = i_img_pal_new(xsize, ysize, 3, 256);
766 if (!read_bmp_pal(ig, im, clr_used)) {
771 if (offbits > base_offset) {
772 /* this will be slow if the offset is large, but that should be
775 while (base_offset < offbits) {
776 if (ig->readcb(ig, &buffer, 1) != 1) {
778 i_push_error(0, "failed skipping to image data offset");
785 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
787 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
788 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
790 if (ig->readcb(ig, packed, line_size) != line_size) {
793 if (allow_incomplete) {
794 i_tags_setn(&im->tags, "i_incomplete", 1);
795 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
799 i_push_error(0, "failed reading 1-bit bmp data");
807 for (x = 0; x < xsize; ++x) {
808 *p++ = (*in & bit) ? 1 : 0;
815 i_ppal(im, 0, xsize, y, line);
825 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
827 Reads in the palette and image data for a 4-bit/pixel image.
829 Returns the image or NULL.
831 Hopefully this will be combined with the following function at some
837 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
838 int compression, long offbits, int allow_incomplete) {
840 int x, y, lasty, yinc;
842 unsigned char *packed;
843 int line_size = (xsize + 1)/2;
849 /* line_size is going to be smaller than xsize in most cases (and
850 when it's not, xsize is itself small), and hence not overflow */
851 line_size = (line_size+3) / 4 * 4;
859 /* when ysize is -ve it's a top-down image */
869 if (clr_used > 16 || clr_used < 0) {
870 i_push_errorf(0, "out of range colors used (%d)", clr_used);
874 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
875 if (offbits < base_offset) {
876 i_push_errorf(0, "image data offset too small (%ld)", offbits);
880 im = i_img_pal_new(xsize, ysize, 3, 256);
881 if (!im) /* error should have been pushed already */
883 if (!read_bmp_pal(ig, im, clr_used)) {
888 if (offbits > base_offset) {
889 /* this will be slow if the offset is large, but that should be
892 while (base_offset < offbits) {
893 if (ig->readcb(ig, &buffer, 1) != 1) {
895 i_push_error(0, "failed skipping to image data offset");
903 packed = mymalloc(260); /* checked 29jun05 tonyc */
905 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
906 /* xsize won't approach MAXINT */
907 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
908 if (compression == BI_RGB) {
909 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
911 if (ig->readcb(ig, packed, line_size) != line_size) {
914 if (allow_incomplete) {
915 i_tags_setn(&im->tags, "i_incomplete", 1);
916 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
920 i_push_error(0, "failed reading 4-bit bmp data");
927 for (x = 0; x < xsize; x+=2) {
932 i_ppal(im, 0, xsize, y, line);
938 else if (compression == BI_RLE4) {
942 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
945 /* there's always at least 2 bytes in a sequence */
946 if (ig->readcb(ig, packed, 2) != 2) {
949 if (allow_incomplete) {
950 i_tags_setn(&im->tags, "i_incomplete", 1);
951 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
955 i_push_error(0, "missing data during decompression");
960 else if (packed[0]) {
961 if (x + packed[0] > xsize) {
962 /* this file is corrupt */
965 i_push_error(0, "invalid data during decompression");
969 line[0] = packed[1] >> 4;
970 line[1] = packed[1] & 0x0F;
971 for (i = 0; i < packed[0]; i += 2) {
973 i_ppal(im, x, x+2, y, line);
975 i_ppal(im, x, x+(packed[0]-i), y, line);
980 case BMPRLE_ENDOFLINE:
985 case BMPRLE_ENDOFBMP:
991 if (ig->readcb(ig, packed, 2) != 2) {
994 if (allow_incomplete) {
995 i_tags_setn(&im->tags, "i_incomplete", 1);
996 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1000 i_push_error(0, "missing data during decompression");
1006 y += yinc * packed[1];
1011 if (x + count > xsize) {
1012 /* this file is corrupt */
1015 i_push_error(0, "invalid data during decompression");
1019 size = (count + 1) / 2;
1020 read_size = (size+1) / 2 * 2;
1021 if (ig->readcb(ig, packed, read_size) != read_size) {
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");
1035 for (i = 0; i < size; ++i) {
1036 line[0] = packed[i] >> 4;
1037 line[1] = packed[i] & 0xF;
1038 i_ppal(im, x, x+2, y, line);
1046 else { /*if (compression == BI_RLE4) {*/
1049 i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
1058 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1060 Reads in the palette and image data for a 8-bit/pixel image.
1062 Returns the image or NULL.
1067 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1068 int compression, long offbits, int allow_incomplete) {
1070 int x, y, lasty, yinc, start_y;
1072 int line_size = xsize;
1075 line_size = (line_size+3) / 4 * 4;
1076 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1077 i_push_error(0, "integer overflow during memory allocation");
1087 /* when ysize is -ve it's a top-down image */
1096 if (clr_used > 256 || clr_used < 0) {
1097 i_push_errorf(0, "out of range colors used (%d)", clr_used);
1101 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1102 if (offbits < base_offset) {
1103 i_push_errorf(0, "image data offset too small (%ld)", offbits);
1107 im = i_img_pal_new(xsize, ysize, 3, 256);
1110 if (!read_bmp_pal(ig, im, clr_used)) {
1115 if (offbits > base_offset) {
1116 /* this will be slow if the offset is large, but that should be
1119 while (base_offset < offbits) {
1120 if (ig->readcb(ig, &buffer, 1) != 1) {
1122 i_push_error(0, "failed skipping to image data offset");
1129 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1130 if (compression == BI_RGB) {
1131 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1132 while (y != lasty) {
1133 if (ig->readcb(ig, line, line_size) != line_size) {
1135 if (allow_incomplete) {
1136 i_tags_setn(&im->tags, "i_incomplete", 1);
1137 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1141 i_push_error(0, "failed reading 8-bit bmp data");
1146 i_ppal(im, 0, xsize, y, line);
1151 else if (compression == BI_RLE8) {
1154 unsigned char packed[2];
1156 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1159 /* there's always at least 2 bytes in a sequence */
1160 if (ig->readcb(ig, packed, 2) != 2) {
1162 if (allow_incomplete) {
1163 i_tags_setn(&im->tags, "i_incomplete", 1);
1164 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1168 i_push_error(0, "missing data during decompression");
1174 if (x + packed[0] > xsize) {
1175 /* this file isn't incomplete, it's corrupt */
1177 i_push_error(0, "invalid data during decompression");
1181 memset(line, packed[1], packed[0]);
1182 i_ppal(im, x, x+packed[0], y, line);
1185 switch (packed[1]) {
1186 case BMPRLE_ENDOFLINE:
1191 case BMPRLE_ENDOFBMP:
1196 if (ig->readcb(ig, packed, 2) != 2) {
1198 if (allow_incomplete) {
1199 i_tags_setn(&im->tags, "i_incomplete", 1);
1200 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1204 i_push_error(0, "missing data during decompression");
1210 y += yinc * packed[1];
1215 if (x + count > xsize) {
1216 /* runs shouldn't cross a line boundary */
1217 /* this file isn't incomplete, it's corrupt */
1219 i_push_error(0, "invalid data during decompression");
1223 read_size = (count+1) / 2 * 2;
1224 if (ig->readcb(ig, line, read_size) != read_size) {
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");
1237 i_ppal(im, x, x+count, y, line);
1246 i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
1258 static struct bm_masks std_masks[] =
1261 { 0770000, 00007700, 00000077, },
1265 { 0xFF0000, 0x00FF00, 0x0000FF, },
1269 { 0xFF0000, 0x00FF00, 0x0000FF, },
1275 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1277 Skips the palette and reads in the image data for a direct colour image.
1279 Returns the image or NULL.
1284 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1285 int clr_used, int compression, long offbits,
1286 int allow_incomplete) {
1288 int x, y, starty, lasty, yinc;
1290 int pix_size = bit_count / 8;
1291 int line_size = xsize * pix_size;
1292 struct bm_masks masks;
1293 char unpack_code[2] = "";
1297 const char *compression_name;
1299 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1301 unpack_code[0] = *("v3V"+pix_size-2);
1302 unpack_code[1] = '\0';
1304 line_size = (line_size+3) / 4 * 4;
1305 extras = line_size - xsize * pix_size;
1313 /* when ysize is -ve it's a top-down image */
1320 if (compression == BI_RGB) {
1321 compression_name = "BI_RGB";
1322 masks = std_masks[pix_size-2];
1324 /* there's a potential "palette" after the header */
1325 for (i = 0; i < clr_used; ++clr_used) {
1327 if (ig->readcb(ig, buf, 4) != 4) {
1328 i_push_error(0, "skipping colors");
1334 else if (compression == BI_BITFIELDS) {
1336 compression_name = "BI_BITFIELDS";
1338 for (i = 0; i < 3; ++i) {
1340 if (!read_packed(ig, "V", &rmask)) {
1341 i_push_error(0, "reading pixel masks");
1344 masks.masks[i] = rmask;
1345 /* work out a shift for the mask */
1347 bit = masks.masks[i] & -masks.masks[i];
1352 masks.shifts[i] = pos - 8;
1354 base_offset += 4 * 4;
1357 i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
1361 if (offbits < base_offset) {
1362 i_push_errorf(0, "image data offset too small (%ld)", offbits);
1366 if (offbits > base_offset) {
1367 /* this will be slow if the offset is large, but that should be
1370 while (base_offset < offbits) {
1371 if (ig->readcb(ig, &buffer, 1) != 1) {
1372 i_push_error(0, "failed skipping to image data offset");
1379 im = i_img_empty(NULL, xsize, ysize);
1383 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1385 /* I wasn't able to make this overflow in testing, but better to be
1387 bytes = sizeof(i_color) * xsize;
1388 if (bytes / sizeof(i_color) != xsize) {
1390 i_push_error(0, "integer overflow calculating buffer size");
1393 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1394 while (y != lasty) {
1396 for (x = 0; x < xsize; ++x) {
1398 if (!read_packed(ig, unpack_code, &pixel)) {
1400 if (allow_incomplete) {
1401 i_tags_setn(&im->tags, "i_incomplete", 1);
1402 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1406 i_push_error(0, "failed reading image data");
1411 for (i = 0; i < 3; ++i) {
1412 if (masks.shifts[i] > 0)
1413 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1415 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1419 i_plin(im, 0, xsize, y, line);
1421 ig->readcb(ig, junk, extras);
1436 Tony Cook <tony@develop-help.com>
1440 Cannot save as compressed BMP.
1444 Doesn't handle OS/2 bitmaps.
1446 16-bit/pixel images haven't been tested. (I need an image).
1448 BI_BITFIELDS compression hasn't been tested (I need an image).
1450 The header handling for paletted images needs to be refactored