7 bmp.c - read and write windows BMP files
14 if (!i_writebmp_wiol(im, ig)) {
21 Reads and writes Windows BMP files.
28 #define FILEHEAD_SIZE 14
29 #define INFOHEAD_SIZE 40
33 #define BI_BITFIELDS 3
34 #define BMPRLE_ENDOFLINE 0
35 #define BMPRLE_ENDOFBMP 1
36 #define BMPRLE_DELTA 2
38 static int read_packed(io_glue *ig, char *format, ...);
39 static int write_packed(io_glue *ig, char *format, ...);
40 static int write_bmphead(io_glue *ig, i_img *im, int bit_count,
42 static int write_1bit_data(io_glue *ig, i_img *im);
43 static int write_4bit_data(io_glue *ig, i_img *im);
44 static int write_8bit_data(io_glue *ig, i_img *im);
45 static int write_24bit_data(io_glue *ig, i_img *im);
46 static int read_bmp_pal(io_glue *ig, i_img *im, int count);
47 static i_img *read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
48 int compression, long offbits);
49 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
50 int compression, long offbits);
51 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
52 int compression, long offbits);
53 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
54 int bit_count, int clr_used, int compression,
58 =item i_writebmp_wiol(im, io_glue)
60 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
61 formats depending on the image.
63 Never compresses the image.
68 i_writebmp_wiol(i_img *im, io_glue *ig) {
69 io_glue_commit_types(ig);
73 if (im->type == i_direct_type) {
74 return write_24bit_data(ig, im);
79 /* must be paletted */
80 pal_size = i_colorcount(im);
82 return write_1bit_data(ig, im);
84 else if (pal_size <= 16) {
85 return write_4bit_data(ig, im);
88 return write_8bit_data(ig, im);
94 =item i_readbmp_wiol(ig)
96 Reads a Windows format bitmap from the given file.
98 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
99 BI_BITFIELDS images too, but I need a test image.
105 i_readbmp_wiol(io_glue *ig) {
106 int b_magic, m_magic, filesize, res1, res2, infohead_size;
107 int xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
108 int clr_used, clr_important, offbits;
111 mm_log((1, "i_readbmp_wiol(ig %p)\n", ig));
113 io_glue_commit_types(ig);
116 if (!read_packed(ig, "CCVvvVVVVvvVVVVVV", &b_magic, &m_magic, &filesize,
117 &res1, &res2, &offbits, &infohead_size,
118 &xsize, &ysize, &planes,
119 &bit_count, &compression, &size_image, &xres, &yres,
120 &clr_used, &clr_important)) {
121 i_push_error(0, "file too short");
124 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
126 i_push_error(0, "not a BMP file");
130 mm_log((1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
131 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
132 "clr_important %d\n", filesize, offbits, xsize, ysize, planes,
133 bit_count, compression, size_image, xres, yres, clr_used,
136 if (!i_int_check_image_file_limits(xsize, ysize, 3, sizeof(i_sample_t))) {
137 mm_log((1, "i_readbmp_wiol: image size exceeds limits\n"));
143 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
147 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
151 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
157 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
162 i_push_errorf(0, "unknown bit count for BMP file (%d)", bit_count);
167 /* store the resolution */
170 else if (yres && !xres)
173 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
174 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
176 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
177 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
178 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
179 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
180 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
181 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
190 =head1 IMPLEMENTATION FUNCTIONS
192 Internal functions used in the implementation.
196 =item read_packed(ig, format, ...)
198 Reads from the specified "file" the specified sizes. The format codes
199 match those used by perl's pack() function, though only a few are
200 implemented. In all cases the vararg arguement is an int *.
202 Returns non-zero if all of the arguments were read.
207 int read_packed(io_glue *ig, char *format, ...) {
208 unsigned char buf[4];
212 va_start(ap, format);
215 p = va_arg(ap, int *);
219 if (ig->readcb(ig, buf, 2) != 2)
221 *p = buf[0] + (buf[1] << 8);
225 if (ig->readcb(ig, buf, 4) != 4)
227 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
231 if (ig->readcb(ig, buf, 1) != 1)
237 if (ig->readcb(ig, buf, 1) != 1)
242 case '3': /* extension - 24-bit number */
243 if (ig->readcb(ig, buf, 3) != 3)
245 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
249 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
257 =item write_packed(ig, format, ...)
259 Writes packed data to the specified io_glue.
261 Returns non-zero on success.
267 write_packed(io_glue *ig, char *format, ...) {
268 unsigned char buf[4];
272 va_start(ap, format);
275 i = va_arg(ap, unsigned int);
281 if (ig->writecb(ig, buf, 2) == -1)
287 buf[1] = (i >> 8) & 0xFF;
288 buf[2] = (i >> 16) & 0xFF;
289 buf[3] = (i >> 24) & 0xFF;
290 if (ig->writecb(ig, buf, 4) == -1)
297 if (ig->writecb(ig, buf, 1) == -1)
302 m_fatal(1, "Unknown write_packed format code 0x%02x", *format);
312 =item write_bmphead(ig, im, bit_count, data_size)
314 Writes a Windows BMP header to the file.
316 Returns non-zero on success.
322 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
324 int got_xres, got_yres, aspect_only;
326 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
328 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
329 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
330 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
342 if (xres <= 0 || yres <= 0)
345 /* scale so the smaller value is 72 */
356 /* now to pels/meter */
360 if (im->type == i_palette_type) {
361 colors_used = i_colorcount(im);
362 offset += 4 * colors_used;
365 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
366 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
367 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
368 colors_used, colors_used)){
369 i_push_error(0, "cannot write bmp header");
372 if (im->type == i_palette_type) {
376 for (i = 0; i < colors_used; ++i) {
377 i_getcolors(im, i, &c, 1);
378 if (im->channels >= 3) {
379 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
381 i_push_error(0, "cannot write palette entry");
386 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
388 i_push_error(0, "cannot write palette entry");
399 =item write_1bit_data(ig, im)
401 Writes the image data as a 1-bit/pixel image.
403 Returns non-zero on success.
408 write_1bit_data(io_glue *ig, i_img *im) {
410 unsigned char *packed;
414 int line_size = (im->xsize+7) / 8;
417 /* round up to nearest multiple of four */
418 line_size = (line_size + 3) / 4 * 4;
420 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
423 line = mymalloc(im->xsize + 8);
424 memset(line + im->xsize, 0, 8);
426 packed = mymalloc(line_size);
427 memset(packed, 0, line_size);
429 for (y = im->ysize-1; y >= 0; --y) {
430 i_gpal(im, 0, im->xsize, y, line);
434 for (x = 0; x < im->xsize; ++x) {
437 if ((mask >>= 1) == 0) {
446 if (ig->writecb(ig, packed, line_size) < 0) {
449 i_push_error(0, "writing 1 bit/pixel packed data");
462 =item write_4bit_data(ig, im)
464 Writes the image data as a 4-bit/pixel image.
466 Returns non-zero on success.
471 write_4bit_data(io_glue *ig, i_img *im) {
473 unsigned char *packed;
475 int line_size = (im->xsize+1) / 2;
478 /* round up to nearest multiple of four */
479 line_size = (line_size + 3) / 4 * 4;
481 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
484 line = mymalloc(im->xsize + 2);
485 memset(line + im->xsize, 0, 2);
487 packed = mymalloc(line_size);
488 memset(packed, 0, line_size);
490 for (y = im->ysize-1; y >= 0; --y) {
491 i_gpal(im, 0, im->xsize, y, line);
493 for (x = 0; x < im->xsize; x += 2) {
494 *out++ = (line[x] << 4) + line[x+1];
496 if (ig->writecb(ig, packed, line_size) < 0) {
499 i_push_error(0, "writing 4 bit/pixel packed data");
512 =item write_8bit_data(ig, im)
514 Writes the image data as a 8-bit/pixel image.
516 Returns non-zero on success.
521 write_8bit_data(io_glue *ig, i_img *im) {
523 int line_size = im->xsize;
526 /* round up to nearest multiple of four */
527 line_size = (line_size + 3) / 4 * 4;
529 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
532 line = mymalloc(im->xsize + 4);
533 memset(line + im->xsize, 0, 4);
535 for (y = im->ysize-1; y >= 0; --y) {
536 i_gpal(im, 0, im->xsize, y, line);
537 if (ig->writecb(ig, line, line_size) < 0) {
539 i_push_error(0, "writing 8 bit/pixel packed data");
550 static int bgr_chans[] = { 2, 1, 0, };
551 static int grey_chans[] = { 0, 0, 0, };
554 =item write_24bit_data(ig, im)
556 Writes the image data as a 24-bit/pixel image.
558 Returns non-zero on success.
563 write_24bit_data(io_glue *ig, i_img *im) {
565 unsigned char *samples;
567 int line_size = 3 * im->xsize;
569 line_size = (line_size + 3) / 4 * 4;
571 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
573 chans = im->channels >= 3 ? bgr_chans : grey_chans;
574 samples = mymalloc(line_size);
575 memset(samples, 0, line_size);
576 for (y = im->ysize-1; y >= 0; --y) {
577 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
578 if (ig->writecb(ig, samples, line_size) < 0) {
579 i_push_error(0, "writing image data");
592 =item read_bmp_pal(ig, im, count)
594 Reads count palette entries from the file and add them to the image.
596 Returns non-zero on success.
601 read_bmp_pal(io_glue *ig, i_img *im, int count) {
606 for (i = 0; i < count; ++i) {
607 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
608 i_push_error(0, "reading BMP palette");
614 if (i_addcolors(im, &c, 1) < 0) {
615 i_push_error(0, "out of space in image palette");
624 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
626 Reads in the palette and image data for a 1-bit/pixel image.
628 Returns the image or NULL.
633 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
634 int compression, long offbits) {
636 int x, y, lasty, yinc;
638 unsigned char *packed;
639 int line_size = (xsize + 7)/8;
644 if (compression != BI_RGB) {
645 i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
649 line_size = (line_size+3) / 4 * 4;
657 /* when ysize is -ve it's a top-down image */
665 if (clr_used < 0 || clr_used > 2) {
666 i_push_errorf(0, "out of range colors used (%d)", clr_used);
670 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
671 if (offbits < base_offset) {
672 i_push_errorf(0, "image data offset too small (%ld)", offbits);
676 im = i_img_pal_new(xsize, ysize, 3, 256);
679 if (!read_bmp_pal(ig, im, clr_used)) {
684 if (offbits > base_offset) {
685 /* this will be slow if the offset is large, but that should be
688 while (base_offset < offbits) {
689 if (ig->readcb(ig, &buffer, 1) != 1) {
691 i_push_error(0, "failed skipping to image data offset");
698 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
700 packed = mymalloc(line_size);
701 line = mymalloc(xsize+8);
703 if (ig->readcb(ig, packed, line_size) != line_size) {
706 i_push_error(0, "failed reading 1-bit bmp data");
713 for (x = 0; x < xsize; ++x) {
714 *p++ = (*in & bit) ? 1 : 0;
721 i_ppal(im, 0, xsize, y, line);
731 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
733 Reads in the palette and image data for a 4-bit/pixel image.
735 Returns the image or NULL.
737 Hopefully this will be combined with the following function at some
743 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
744 int compression, long offbits) {
746 int x, y, lasty, yinc;
748 unsigned char *packed;
749 int line_size = (xsize + 1)/2;
754 line_size = (line_size+3) / 4 * 4;
762 /* when ysize is -ve it's a top-down image */
771 if (clr_used > 16 || clr_used < 0) {
772 i_push_errorf(0, "out of range colors used (%d)", clr_used);
776 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
777 if (offbits < base_offset) {
778 i_push_errorf(0, "image data offset too small (%ld)", offbits);
782 im = i_img_pal_new(xsize, ysize, 3, 256);
783 if (!im) /* error should have been pushed already */
785 if (!read_bmp_pal(ig, im, clr_used)) {
790 if (offbits > base_offset) {
791 /* this will be slow if the offset is large, but that should be
794 while (base_offset < offbits) {
795 if (ig->readcb(ig, &buffer, 1) != 1) {
797 i_push_error(0, "failed skipping to image data offset");
805 packed = mymalloc(260);
807 packed = mymalloc(line_size);
808 line = mymalloc(xsize+1);
809 if (compression == BI_RGB) {
810 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
812 if (ig->readcb(ig, packed, line_size) != line_size) {
815 i_push_error(0, "failed reading 4-bit bmp data");
821 for (x = 0; x < xsize; x+=2) {
826 i_ppal(im, 0, xsize, y, line);
832 else if (compression == BI_RLE4) {
836 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
839 /* there's always at least 2 bytes in a sequence */
840 if (ig->readcb(ig, packed, 2) != 2) {
843 i_push_error(0, "missing data during decompression");
847 else if (packed[0]) {
848 line[0] = packed[1] >> 4;
849 line[1] = packed[1] & 0x0F;
850 for (i = 0; i < packed[0]; i += 2) {
852 i_ppal(im, x, x+2, y, line);
854 i_ppal(im, x, x+(packed[0]-i), y, line);
859 case BMPRLE_ENDOFLINE:
864 case BMPRLE_ENDOFBMP:
870 if (ig->readcb(ig, packed, 2) != 2) {
873 i_push_error(0, "missing data during decompression");
878 y += yinc * packed[1];
883 size = (count + 1) / 2;
884 read_size = (size+1) / 2 * 2;
885 if (ig->readcb(ig, packed, read_size) != read_size) {
888 i_push_error(0, "missing data during decompression");
889 /*i_img_destroy(im);*/
892 for (i = 0; i < size; ++i) {
893 line[0] = packed[i] >> 4;
894 line[1] = packed[i] & 0xF;
895 i_ppal(im, x, x+2, y, line);
903 else { /*if (compression == BI_RLE4) {*/
906 i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
915 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
917 Reads in the palette and image data for a 8-bit/pixel image.
919 Returns the image or NULL.
924 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
925 int compression, long offbits) {
927 int x, y, lasty, yinc;
929 int line_size = xsize;
932 line_size = (line_size+3) / 4 * 4;
940 /* when ysize is -ve it's a top-down image */
948 if (clr_used > 256 || clr_used < 0) {
949 i_push_errorf(0, "out of range colors used (%d)", clr_used);
953 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
954 if (offbits < base_offset) {
955 i_push_errorf(0, "image data offset too small (%ld)", offbits);
959 im = i_img_pal_new(xsize, ysize, 3, 256);
962 if (!read_bmp_pal(ig, im, clr_used)) {
967 if (offbits > base_offset) {
968 /* this will be slow if the offset is large, but that should be
971 while (base_offset < offbits) {
972 if (ig->readcb(ig, &buffer, 1) != 1) {
974 i_push_error(0, "failed skipping to image data offset");
981 line = mymalloc(line_size);
982 if (compression == BI_RGB) {
983 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
985 if (ig->readcb(ig, line, line_size) != line_size) {
987 i_push_error(0, "failed reading 8-bit bmp data");
991 i_ppal(im, 0, xsize, y, line);
996 else if (compression == BI_RLE8) {
999 unsigned char packed[2];
1001 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1004 /* there's always at least 2 bytes in a sequence */
1005 if (ig->readcb(ig, packed, 2) != 2) {
1007 i_push_error(0, "missing data during decompression");
1012 memset(line, packed[1], packed[0]);
1013 i_ppal(im, x, x+packed[0], y, line);
1016 switch (packed[1]) {
1017 case BMPRLE_ENDOFLINE:
1022 case BMPRLE_ENDOFBMP:
1027 if (ig->readcb(ig, packed, 2) != 2) {
1029 i_push_error(0, "missing data during decompression");
1034 y += yinc * packed[1];
1039 read_size = (count+1) / 2 * 2;
1040 if (ig->readcb(ig, line, read_size) != read_size) {
1042 i_push_error(0, "missing data during decompression");
1046 i_ppal(im, x, x+count, y, line);
1055 i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
1067 static struct bm_masks std_masks[] =
1070 { 0770000, 00007700, 00000077, },
1074 { 0xFF0000, 0x00FF00, 0x0000FF, },
1078 { 0xFF0000, 0x00FF00, 0x0000FF, },
1084 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
1086 Skips the palette and reads in the image data for a direct colour image.
1088 Returns the image or NULL.
1093 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1094 int clr_used, int compression, long offbits) {
1096 int x, y, lasty, yinc;
1098 int pix_size = bit_count / 8;
1099 int line_size = xsize * pix_size;
1100 struct bm_masks masks;
1101 char unpack_code[2] = "";
1105 const char *compression_name;
1107 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1109 unpack_code[0] = *("v3V"+pix_size-2);
1110 unpack_code[1] = '\0';
1112 line_size = (line_size+3) / 4 * 4;
1113 extras = line_size - xsize * pix_size;
1121 /* when ysize is -ve it's a top-down image */
1127 if (compression == BI_RGB) {
1128 compression_name = "BI_RGB";
1129 masks = std_masks[pix_size-2];
1131 /* there's a potential "palette" after the header */
1132 for (i = 0; i < clr_used; ++clr_used) {
1134 if (ig->readcb(ig, buf, 4) != 4) {
1135 i_push_error(0, "skipping colors");
1141 else if (compression == BI_BITFIELDS) {
1143 compression_name = "BI_BITFIELDS";
1145 for (i = 0; i < 3; ++i) {
1146 if (!read_packed(ig, "V", masks.masks+i)) {
1147 i_push_error(0, "reading pixel masks");
1150 /* work out a shift for the mask */
1152 bit = masks.masks[i] & -masks.masks[i];
1157 masks.shifts[i] = pos - 8;
1159 base_offset += 4 * 4;
1162 i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
1166 if (offbits > base_offset) {
1167 /* this will be slow if the offset is large, but that should be
1170 while (base_offset < offbits) {
1171 if (ig->readcb(ig, &buffer, 1) != 1) {
1173 i_push_error(0, "failed skipping to image data offset");
1180 im = i_img_empty(NULL, xsize, ysize);
1184 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1186 /* I wasn't able to make this overflow in testing, but better to be
1188 bytes = sizeof(i_color) * xsize;
1189 if (bytes / sizeof(i_color) != xsize) {
1191 i_push_error(0, "integer overflow calculating buffer size");
1194 line = mymalloc(bytes);
1195 while (y != lasty) {
1197 for (x = 0; x < xsize; ++x) {
1199 if (!read_packed(ig, unpack_code, &pixel)) {
1200 i_push_error(0, "failed reading image data");
1205 for (i = 0; i < 3; ++i) {
1206 if (masks.shifts[i] > 0)
1207 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1209 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1213 i_plin(im, 0, xsize, y, line);
1215 ig->readcb(ig, junk, extras);
1230 Tony Cook <tony@develop-help.com>
1234 Cannot save as compressed BMP.
1238 Doesn't handle OS/2 bitmaps.
1240 16-bit/pixel images haven't been tested. (I need an image).
1242 BI_BITFIELDS compression hasn't been tested (I need an image).
1244 The header handling for paletted images needs to be refactored