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,
138 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
142 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
146 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits);
152 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
157 i_push_errorf(0, "unknown bit count for BMP file (%d)", bit_count);
162 /* store the resolution */
165 else if (yres && !xres)
168 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
169 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
171 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
172 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
173 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
174 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
175 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
176 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
185 =head1 IMPLEMENTATION FUNCTIONS
187 Internal functions used in the implementation.
191 =item read_packed(ig, format, ...)
193 Reads from the specified "file" the specified sizes. The format codes
194 match those used by perl's pack() function, though only a few are
195 implemented. In all cases the vararg arguement is an int *.
197 Returns non-zero if all of the arguments were read.
202 int read_packed(io_glue *ig, char *format, ...) {
203 unsigned char buf[4];
207 va_start(ap, format);
210 p = va_arg(ap, int *);
214 if (ig->readcb(ig, buf, 2) != 2)
216 *p = buf[0] + (buf[1] << 8);
220 if (ig->readcb(ig, buf, 4) != 4)
222 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
226 if (ig->readcb(ig, buf, 1) != 1)
232 if (ig->readcb(ig, buf, 1) != 1)
237 case '3': /* extension - 24-bit number */
238 if (ig->readcb(ig, buf, 3) != 3)
240 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
244 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
252 =item write_packed(ig, format, ...)
254 Writes packed data to the specified io_glue.
256 Returns non-zero on success.
262 write_packed(io_glue *ig, char *format, ...) {
263 unsigned char buf[4];
267 va_start(ap, format);
270 i = va_arg(ap, unsigned int);
276 if (ig->writecb(ig, buf, 2) == -1)
282 buf[1] = (i >> 8) & 0xFF;
283 buf[2] = (i >> 16) & 0xFF;
284 buf[3] = (i >> 24) & 0xFF;
285 if (ig->writecb(ig, buf, 4) == -1)
292 if (ig->writecb(ig, buf, 1) == -1)
297 m_fatal(1, "Unknown write_packed format code 0x%02x", *format);
307 =item write_bmphead(ig, im, bit_count, data_size)
309 Writes a Windows BMP header to the file.
311 Returns non-zero on success.
317 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
319 int got_xres, got_yres, aspect_only;
321 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
323 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
324 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
325 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
337 if (xres <= 0 || yres <= 0)
340 /* scale so the smaller value is 72 */
351 /* now to pels/meter */
355 if (im->type == i_palette_type) {
356 colors_used = i_colorcount(im);
357 offset += 4 * colors_used;
360 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
361 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
362 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
363 colors_used, colors_used)){
364 i_push_error(0, "cannot write bmp header");
367 if (im->type == i_palette_type) {
371 for (i = 0; i < colors_used; ++i) {
372 i_getcolors(im, i, &c, 1);
373 if (im->channels >= 3) {
374 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
376 i_push_error(0, "cannot write palette entry");
381 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
383 i_push_error(0, "cannot write palette entry");
394 =item write_1bit_data(ig, im)
396 Writes the image data as a 1-bit/pixel image.
398 Returns non-zero on success.
403 write_1bit_data(io_glue *ig, i_img *im) {
405 unsigned char *packed;
409 int line_size = (im->xsize+7) / 8;
412 /* round up to nearest multiple of four */
413 line_size = (line_size + 3) / 4 * 4;
415 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
418 line = mymalloc(im->xsize + 8);
419 memset(line + im->xsize, 0, 8);
421 packed = mymalloc(line_size);
422 memset(packed, 0, line_size);
424 for (y = im->ysize-1; y >= 0; --y) {
425 i_gpal(im, 0, im->xsize, y, line);
429 for (x = 0; x < im->xsize; ++x) {
432 if ((mask >>= 1) == 0) {
441 if (ig->writecb(ig, packed, line_size) < 0) {
444 i_push_error(0, "writing 1 bit/pixel packed data");
457 =item write_4bit_data(ig, im)
459 Writes the image data as a 4-bit/pixel image.
461 Returns non-zero on success.
466 write_4bit_data(io_glue *ig, i_img *im) {
468 unsigned char *packed;
470 int line_size = (im->xsize+1) / 2;
473 /* round up to nearest multiple of four */
474 line_size = (line_size + 3) / 4 * 4;
476 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
479 line = mymalloc(im->xsize + 2);
480 memset(line + im->xsize, 0, 2);
482 packed = mymalloc(line_size);
483 memset(packed, 0, line_size);
485 for (y = im->ysize-1; y >= 0; --y) {
486 i_gpal(im, 0, im->xsize, y, line);
488 for (x = 0; x < im->xsize; x += 2) {
489 *out++ = (line[x] << 4) + line[x+1];
491 if (ig->writecb(ig, packed, line_size) < 0) {
494 i_push_error(0, "writing 4 bit/pixel packed data");
507 =item write_8bit_data(ig, im)
509 Writes the image data as a 8-bit/pixel image.
511 Returns non-zero on success.
516 write_8bit_data(io_glue *ig, i_img *im) {
518 int line_size = im->xsize;
521 /* round up to nearest multiple of four */
522 line_size = (line_size + 3) / 4 * 4;
524 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
527 line = mymalloc(im->xsize + 4);
528 memset(line + im->xsize, 0, 4);
530 for (y = im->ysize-1; y >= 0; --y) {
531 i_gpal(im, 0, im->xsize, y, line);
532 if (ig->writecb(ig, line, line_size) < 0) {
534 i_push_error(0, "writing 8 bit/pixel packed data");
545 static int bgr_chans[] = { 2, 1, 0, };
546 static int grey_chans[] = { 0, 0, 0, };
549 =item write_24bit_data(ig, im)
551 Writes the image data as a 24-bit/pixel image.
553 Returns non-zero on success.
558 write_24bit_data(io_glue *ig, i_img *im) {
560 unsigned char *samples;
562 int line_size = 3 * im->xsize;
564 line_size = (line_size + 3) / 4 * 4;
566 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
568 chans = im->channels >= 3 ? bgr_chans : grey_chans;
569 samples = mymalloc(line_size);
570 memset(samples, 0, line_size);
571 for (y = im->ysize-1; y >= 0; --y) {
572 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
573 if (ig->writecb(ig, samples, line_size) < 0) {
574 i_push_error(0, "writing image data");
587 =item read_bmp_pal(ig, im, count)
589 Reads count palette entries from the file and add them to the image.
591 Returns non-zero on success.
596 read_bmp_pal(io_glue *ig, i_img *im, int count) {
601 for (i = 0; i < count; ++i) {
602 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
603 i_push_error(0, "reading BMP palette");
609 if (i_addcolors(im, &c, 1) < 0) {
610 i_push_error(0, "out of space in image palette");
619 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
621 Reads in the palette and image data for a 1-bit/pixel image.
623 Returns the image or NULL.
628 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
629 int compression, long offbits) {
631 int x, y, lasty, yinc;
633 unsigned char *packed;
634 int line_size = (xsize + 7)/8;
639 if (compression != BI_RGB) {
640 i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
644 line_size = (line_size+3) / 4 * 4;
652 /* when ysize is -ve it's a top-down image */
660 if (clr_used < 0 || clr_used > 2) {
661 i_push_errorf(0, "out of range colors used (%d)", clr_used);
665 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
666 if (offbits < base_offset) {
667 i_push_errorf(0, "image data offset too small (%ld)", offbits);
671 im = i_img_pal_new(xsize, ysize, 3, 256);
674 if (!read_bmp_pal(ig, im, clr_used)) {
679 if (offbits > base_offset) {
680 /* this will be slow if the offset is large, but that should be
683 while (base_offset < offbits) {
684 if (ig->readcb(ig, &buffer, 1) != 1) {
686 i_push_error(0, "failed skipping to image data offset");
693 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
695 packed = mymalloc(line_size);
696 line = mymalloc(xsize+8);
698 if (ig->readcb(ig, packed, line_size) != line_size) {
701 i_push_error(0, "failed reading 1-bit bmp data");
708 for (x = 0; x < xsize; ++x) {
709 *p++ = (*in & bit) ? 1 : 0;
716 i_ppal(im, 0, xsize, y, line);
726 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
728 Reads in the palette and image data for a 4-bit/pixel image.
730 Returns the image or NULL.
732 Hopefully this will be combined with the following function at some
738 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
739 int compression, long offbits) {
741 int x, y, lasty, yinc;
743 unsigned char *packed;
744 int line_size = (xsize + 1)/2;
749 line_size = (line_size+3) / 4 * 4;
757 /* when ysize is -ve it's a top-down image */
766 if (clr_used > 16 || clr_used < 0) {
767 i_push_errorf(0, "out of range colors used (%d)", clr_used);
771 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
772 if (offbits < base_offset) {
773 i_push_errorf(0, "image data offset too small (%ld)", offbits);
777 im = i_img_pal_new(xsize, ysize, 3, 256);
778 if (!im) /* error should have been pushed already */
780 if (!read_bmp_pal(ig, im, clr_used)) {
785 if (offbits > base_offset) {
786 /* this will be slow if the offset is large, but that should be
789 while (base_offset < offbits) {
790 if (ig->readcb(ig, &buffer, 1) != 1) {
792 i_push_error(0, "failed skipping to image data offset");
800 packed = mymalloc(260);
802 packed = mymalloc(line_size);
803 line = mymalloc(xsize+1);
804 if (compression == BI_RGB) {
805 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
807 if (ig->readcb(ig, packed, line_size) != line_size) {
810 i_push_error(0, "failed reading 4-bit bmp data");
816 for (x = 0; x < xsize; x+=2) {
821 i_ppal(im, 0, xsize, y, line);
827 else if (compression == BI_RLE4) {
832 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
835 /* there's always at least 2 bytes in a sequence */
836 if (ig->readcb(ig, packed, 2) != 2) {
839 i_push_error(0, "missing data during decompression");
843 else if (packed[0]) {
844 line[0] = packed[1] >> 4;
845 line[1] = packed[1] & 0x0F;
846 for (i = 0; i < packed[0]; i += 2) {
848 i_ppal(im, x, x+2, y, line);
850 i_ppal(im, x, x+(packed[0]-i), y, line);
855 case BMPRLE_ENDOFLINE:
860 case BMPRLE_ENDOFBMP:
866 if (ig->readcb(ig, packed, 2) != 2) {
869 i_push_error(0, "missing data during decompression");
874 y += yinc * packed[1];
879 size = (count + 1) / 2;
880 read_size = (size+1) / 2 * 2;
881 if (ig->readcb(ig, packed, read_size) != read_size) {
884 i_push_error(0, "missing data during decompression");
885 /*i_img_destroy(im);*/
888 for (i = 0; i < size; ++i) {
889 line[0] = packed[i] >> 4;
890 line[1] = packed[i] & 0xF;
891 i_ppal(im, x, x+2, y, line);
899 else { /*if (compression == BI_RLE4) {*/
902 i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
911 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
913 Reads in the palette and image data for a 8-bit/pixel image.
915 Returns the image or NULL.
920 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
921 int compression, long offbits) {
923 int x, y, lasty, yinc;
925 int line_size = xsize;
929 line_size = (line_size+3) / 4 * 4;
937 /* when ysize is -ve it's a top-down image */
945 if (clr_used > 256 || clr_used < 0) {
946 i_push_errorf(0, "out of range colors used (%d)", clr_used);
950 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
951 if (offbits < base_offset) {
952 i_push_errorf(0, "image data offset too small (%ld)", offbits);
956 im = i_img_pal_new(xsize, ysize, 3, 256);
959 if (!read_bmp_pal(ig, im, clr_used)) {
964 if (offbits > base_offset) {
965 /* this will be slow if the offset is large, but that should be
968 while (base_offset < offbits) {
969 if (ig->readcb(ig, &buffer, 1) != 1) {
971 i_push_error(0, "failed skipping to image data offset");
978 line = mymalloc(line_size);
979 if (compression == BI_RGB) {
980 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
982 if (ig->readcb(ig, line, line_size) != line_size) {
984 i_push_error(0, "failed reading 8-bit bmp data");
988 i_ppal(im, 0, xsize, y, line);
993 else if (compression == BI_RLE8) {
997 unsigned char packed[2];
999 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1002 /* there's always at least 2 bytes in a sequence */
1003 if (ig->readcb(ig, packed, 2) != 2) {
1005 i_push_error(0, "missing data during decompression");
1010 memset(line, packed[1], packed[0]);
1011 i_ppal(im, x, x+packed[0], y, line);
1014 switch (packed[1]) {
1015 case BMPRLE_ENDOFLINE:
1020 case BMPRLE_ENDOFBMP:
1025 if (ig->readcb(ig, packed, 2) != 2) {
1027 i_push_error(0, "missing data during decompression");
1032 y += yinc * packed[1];
1037 read_size = (count+1) / 2 * 2;
1038 if (ig->readcb(ig, line, read_size) != read_size) {
1040 i_push_error(0, "missing data during decompression");
1044 i_ppal(im, x, x+count, y, line);
1053 i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
1065 static struct bm_masks std_masks[] =
1068 { 0770000, 00007700, 00000077, },
1072 { 0xFF0000, 0x00FF00, 0x0000FF, },
1076 { 0xFF0000, 0x00FF00, 0x0000FF, },
1082 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
1084 Skips the palette and reads in the image data for a direct colour image.
1086 Returns the image or NULL.
1091 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1092 int clr_used, int compression, long offbits) {
1094 int x, y, lasty, yinc;
1097 int pix_size = bit_count / 8;
1098 int line_size = xsize * pix_size;
1099 struct bm_masks masks;
1100 char unpack_code[2] = "";
1104 const char *compression_name;
1106 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1108 unpack_code[0] = *("v3V"+pix_size-2);
1109 unpack_code[1] = '\0';
1111 line_size = (line_size+3) / 4 * 4;
1112 extras = line_size - xsize * pix_size;
1120 /* when ysize is -ve it's a top-down image */
1126 if (compression == BI_RGB) {
1127 compression_name = "BI_RGB";
1128 masks = std_masks[pix_size-2];
1130 /* there's a potential "palette" after the header */
1131 for (i = 0; i < clr_used; ++clr_used) {
1133 if (ig->readcb(ig, buf, 4) != 4) {
1134 i_push_error(0, "skipping colors");
1140 else if (compression == BI_BITFIELDS) {
1142 compression_name = "BI_BITFIELDS";
1144 for (i = 0; i < 3; ++i) {
1145 if (!read_packed(ig, "V", masks.masks+i)) {
1146 i_push_error(0, "reading pixel masks");
1149 /* work out a shift for the mask */
1151 bit = masks.masks[i] & -masks.masks[i];
1156 masks.shifts[i] = pos - 8;
1158 base_offset += 4 * 4;
1161 i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
1165 if (offbits > base_offset) {
1166 /* this will be slow if the offset is large, but that should be
1169 while (base_offset < offbits) {
1170 if (ig->readcb(ig, &buffer, 1) != 1) {
1172 i_push_error(0, "failed skipping to image data offset");
1179 im = i_img_empty(NULL, xsize, ysize);
1183 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1185 /* I wasn't able to make this overflow in testing, but better to be
1187 bytes = sizeof(i_color) * xsize;
1188 if (bytes / sizeof(i_color) != xsize) {
1190 i_push_error(0, "integer overflow calculating buffer size");
1193 line = mymalloc(bytes);
1194 while (y != lasty) {
1196 for (x = 0; x < xsize; ++x) {
1198 if (!read_packed(ig, unpack_code, &pixel)) {
1199 i_push_error(0, "failed reading image data");
1204 for (i = 0; i < 3; ++i) {
1205 if (masks.shifts[i] > 0)
1206 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1208 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1212 i_plin(im, 0, xsize, y, line);
1214 ig->readcb(ig, junk, extras);
1229 Tony Cook <tony@develop-help.com>
1233 Cannot save as compressed BMP.
1237 Doesn't handle OS/2 bitmaps.
1239 16-bit/pixel images haven't been tested. (I need an image).
1241 BI_BITFIELDS compression hasn't been tested (I need an image).
1243 The header handling for paletted images needs to be refactored