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,
49 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
51 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
53 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
54 int bit_count, int clr_used, int compression);
57 =item i_writebmp_wiol(im, io_glue)
59 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
60 formats depending on the image.
62 Never compresses the image.
67 i_writebmp_wiol(i_img *im, io_glue *ig) {
68 io_glue_commit_types(ig);
72 if (im->type == i_direct_type) {
73 return write_24bit_data(ig, im);
78 /* must be paletted */
79 pal_size = i_colorcount(im);
81 return write_1bit_data(ig, im);
83 else if (pal_size <= 16) {
84 return write_4bit_data(ig, im);
87 return write_8bit_data(ig, im);
93 =item i_readbmp_wiol(ig)
95 Reads a Windows format bitmap from the given file.
97 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
98 BI_BITFIELDS images too, but I need a test image.
104 i_readbmp_wiol(io_glue *ig) {
105 int b_magic, m_magic, filesize, res1, res2, infohead_size;
106 int xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
107 int clr_used, clr_important, offbits;
110 mm_log((1, "i_readbmp_wiol(ig %p)\n", ig));
112 io_glue_commit_types(ig);
115 if (!read_packed(ig, "CCVvvVVVVvvVVVVVV", &b_magic, &m_magic, &filesize,
116 &res1, &res2, &offbits, &infohead_size,
117 &xsize, &ysize, &planes,
118 &bit_count, &compression, &size_image, &xres, &yres,
119 &clr_used, &clr_important)) {
120 i_push_error(0, "file too short");
123 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
125 i_push_error(0, "not a BMP file");
129 mm_log((1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
130 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
131 "clr_important %d\n", filesize, offbits, xsize, ysize, planes,
132 bit_count, compression, size_image, xres, yres, clr_used,
137 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression);
141 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression);
145 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression);
151 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression);
155 i_push_errorf(0, "unknown bit count for BMP file (%d)", bit_count);
160 /* store the resolution */
163 else if (yres && !xres)
166 i_tags_set_float(&im->tags, "i_xres", 0, xres * 0.0254);
167 i_tags_set_float(&im->tags, "i_yres", 0, yres * 0.0254);
169 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
170 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
171 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
172 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
173 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
174 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
183 =head1 IMPLEMENTATION FUNCTIONS
185 Internal functions used in the implementation.
189 =item read_packed(ig, format, ...)
191 Reads from the specified "file" the specified sizes. The format codes
192 match those used by perl's pack() function, though only a few are
193 implemented. In all cases the vararg arguement is an int *.
195 Returns non-zero if all of the arguments were read.
200 int read_packed(io_glue *ig, char *format, ...) {
201 unsigned char buf[4];
205 va_start(ap, format);
208 p = va_arg(ap, int *);
212 if (ig->readcb(ig, buf, 2) != 2)
214 *p = buf[0] + (buf[1] << 8);
218 if (ig->readcb(ig, buf, 4) != 4)
220 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
224 if (ig->readcb(ig, buf, 1) != 1)
230 if (ig->readcb(ig, buf, 1) != 1)
235 case '3': /* extension - 24-bit number */
236 if (ig->readcb(ig, buf, 3) != 3)
238 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
242 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
250 =item write_packed(ig, format, ...)
252 Writes packed data to the specified io_glue.
254 Returns non-zero on success.
260 write_packed(io_glue *ig, char *format, ...) {
261 unsigned char buf[4];
265 va_start(ap, format);
268 i = va_arg(ap, unsigned int);
274 if (ig->writecb(ig, buf, 2) == -1)
280 buf[1] = (i >> 8) & 0xFF;
281 buf[2] = (i >> 16) & 0xFF;
282 buf[3] = (i >> 24) & 0xFF;
283 if (ig->writecb(ig, buf, 4) == -1)
290 if (ig->writecb(ig, buf, 1) == -1)
295 m_fatal(1, "Unknown write_packed format code 0x%02x", *format);
305 =item write_bmphead(ig, im, bit_count, data_size)
307 Writes a Windows BMP header to the file.
309 Returns non-zero on success.
315 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
317 int got_xres, got_yres, aspect_only;
319 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
321 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
322 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
323 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
335 if (xres <= 0 || yres <= 0)
338 /* scale so the smaller value is 72 */
349 /* now to pels/meter */
353 if (im->type == i_palette_type) {
354 colors_used = i_colorcount(im);
355 offset += 4 * colors_used;
358 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
359 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
360 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
361 colors_used, colors_used)){
362 i_push_error(0, "cannot write bmp header");
365 if (im->type == i_palette_type) {
369 for (i = 0; i < colors_used; ++i) {
370 i_getcolors(im, i, &c, 1);
371 if (im->channels >= 3) {
372 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
374 i_push_error(0, "cannot write palette entry");
379 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
381 i_push_error(0, "cannot write palette entry");
392 =item write_1bit_data(ig, im)
394 Writes the image data as a 1-bit/pixel image.
396 Returns non-zero on success.
401 write_1bit_data(io_glue *ig, i_img *im) {
403 unsigned char *packed;
407 int line_size = (im->xsize+7) / 8;
410 /* round up to nearest multiple of four */
411 line_size = (line_size + 3) / 4 * 4;
413 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
416 line = mymalloc(im->xsize + 8);
417 memset(line + im->xsize, 0, 8);
419 packed = mymalloc(line_size);
420 memset(packed, 0, line_size);
422 for (y = im->ysize-1; y >= 0; --y) {
423 i_gpal(im, 0, im->xsize, y, line);
427 for (x = 0; x < im->xsize; ++x) {
430 if ((mask >>= 1) == 0) {
439 if (ig->writecb(ig, packed, line_size) < 0) {
442 i_push_error(0, "writing 1 bit/pixel packed data");
455 =item write_4bit_data(ig, im)
457 Writes the image data as a 4-bit/pixel image.
459 Returns non-zero on success.
464 write_4bit_data(io_glue *ig, i_img *im) {
466 unsigned char *packed;
468 int line_size = (im->xsize+1) / 2;
471 /* round up to nearest multiple of four */
472 line_size = (line_size + 3) / 4 * 4;
474 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
477 line = mymalloc(im->xsize + 2);
478 memset(line + im->xsize, 0, 2);
480 packed = mymalloc(line_size);
481 memset(packed, 0, line_size);
483 for (y = im->ysize-1; y >= 0; --y) {
484 i_gpal(im, 0, im->xsize, y, line);
486 for (x = 0; x < im->xsize; x += 2) {
487 *out++ = (line[x] << 4) + line[x+1];
489 if (ig->writecb(ig, packed, line_size) < 0) {
492 i_push_error(0, "writing 4 bit/pixel packed data");
505 =item write_8bit_data(ig, im)
507 Writes the image data as a 8-bit/pixel image.
509 Returns non-zero on success.
514 write_8bit_data(io_glue *ig, i_img *im) {
516 int line_size = im->xsize;
519 /* round up to nearest multiple of four */
520 line_size = (line_size + 3) / 4 * 4;
522 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
525 line = mymalloc(im->xsize + 4);
526 memset(line + im->xsize, 0, 4);
528 for (y = im->ysize-1; y >= 0; --y) {
529 i_gpal(im, 0, im->xsize, y, line);
530 if (ig->writecb(ig, line, line_size) < 0) {
532 i_push_error(0, "writing 8 bit/pixel packed data");
543 static int bgr_chans[] = { 2, 1, 0, };
544 static int grey_chans[] = { 0, 0, 0, };
547 =item write_24bit_data(ig, im)
549 Writes the image data as a 24-bit/pixel image.
551 Returns non-zero on success.
556 write_24bit_data(io_glue *ig, i_img *im) {
558 unsigned char *samples;
560 int line_size = 3 * im->xsize;
562 line_size = (line_size + 3) / 4 * 4;
564 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
566 chans = im->channels >= 3 ? bgr_chans : grey_chans;
567 samples = mymalloc(line_size);
568 memset(samples, 0, line_size);
569 for (y = im->ysize-1; y >= 0; --y) {
570 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
571 if (ig->writecb(ig, samples, line_size) < 0) {
572 i_push_error(0, "writing image data");
585 =item read_bmp_pal(ig, im, count)
587 Reads count palette entries from the file and add them to the image.
589 Returns non-zero on success.
594 read_bmp_pal(io_glue *ig, i_img *im, int count) {
599 for (i = 0; i < count; ++i) {
600 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
601 i_push_error(0, "reading BMP palette");
607 if (i_addcolors(im, &c, 1) < 0) {
608 i_push_error(0, "out of space in image palette");
617 =item read_1bit_bmp(ig, xsize, ysize, clr_used)
619 Reads in the palette and image data for a 1-bit/pixel image.
621 Returns the image or NULL.
626 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
629 int x, y, lasty, yinc;
631 unsigned char *packed;
632 int line_size = (xsize + 7)/8;
636 if (compression != BI_RGB) {
637 i_push_errorf(0, "unknown 1-bit BMP compression (%d)", compression);
641 line_size = (line_size+3) / 4 * 4;
649 /* when ysize is -ve it's a top-down image */
657 if (clr_used < 0 || clr_used > 2) {
658 i_push_errorf(0, "out of range colors used (%d)", clr_used);
661 im = i_img_pal_new(xsize, ysize, 3, 256);
664 if (!read_bmp_pal(ig, im, clr_used)) {
669 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
671 packed = mymalloc(line_size);
672 line = mymalloc(xsize+8);
674 if (ig->readcb(ig, packed, line_size) != line_size) {
677 i_push_error(0, "failed reading 1-bit bmp data");
684 for (x = 0; x < xsize; ++x) {
685 *p++ = (*in & bit) ? 1 : 0;
692 i_ppal(im, 0, xsize, y, line);
702 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
704 Reads in the palette and image data for a 4-bit/pixel image.
706 Returns the image or NULL.
708 Hopefully this will be combined with the following function at some
714 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
717 int x, y, lasty, yinc;
719 unsigned char *packed;
720 int line_size = (xsize + 1)/2;
724 line_size = (line_size+3) / 4 * 4;
732 /* when ysize is -ve it's a top-down image */
741 if (clr_used > 16 || clr_used < 0) {
742 i_push_errorf(0, "out of range colors used (%d)", clr_used);
746 im = i_img_pal_new(xsize, ysize, 3, 256);
747 if (!im) /* error should have been pushed already */
749 if (!read_bmp_pal(ig, im, clr_used)) {
755 packed = mymalloc(260);
757 packed = mymalloc(line_size);
758 line = mymalloc(xsize+1);
759 if (compression == BI_RGB) {
760 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
762 if (ig->readcb(ig, packed, line_size) != line_size) {
765 i_push_error(0, "failed reading 4-bit bmp data");
771 for (x = 0; x < xsize; x+=2) {
776 i_ppal(im, 0, xsize, y, line);
782 else if (compression == BI_RLE4) {
787 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
790 /* there's always at least 2 bytes in a sequence */
791 if (ig->readcb(ig, packed, 2) != 2) {
794 i_push_error(0, "missing data during decompression");
798 else if (packed[0]) {
799 line[0] = packed[1] >> 4;
800 line[1] = packed[1] & 0x0F;
801 for (i = 0; i < packed[0]; i += 2) {
803 i_ppal(im, x, x+2, y, line);
805 i_ppal(im, x, x+(packed[0]-i), y, line);
810 case BMPRLE_ENDOFLINE:
815 case BMPRLE_ENDOFBMP:
821 if (ig->readcb(ig, packed, 2) != 2) {
824 i_push_error(0, "missing data during decompression");
829 y += yinc * packed[1];
834 size = (count + 1) / 2;
835 read_size = (size+1) / 2 * 2;
836 if (ig->readcb(ig, packed, read_size) != read_size) {
839 i_push_error(0, "missing data during decompression");
840 /*i_img_destroy(im);*/
843 for (i = 0; i < size; ++i) {
844 line[0] = packed[i] >> 4;
845 line[1] = packed[i] & 0xF;
846 i_ppal(im, x, x+2, y, line);
854 else { /*if (compression == BI_RLE4) {*/
857 i_push_errorf(0, "unknown 4-bit BMP compression (%d)", compression);
866 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
868 Reads in the palette and image data for a 8-bit/pixel image.
870 Returns the image or NULL.
875 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
878 int x, y, lasty, yinc;
880 int line_size = xsize;
883 line_size = (line_size+3) / 4 * 4;
891 /* when ysize is -ve it's a top-down image */
899 if (clr_used > 256 || clr_used < 0) {
900 i_push_errorf(0, "out of range colors used (%d)", clr_used);
904 im = i_img_pal_new(xsize, ysize, 3, 256);
907 if (!read_bmp_pal(ig, im, clr_used)) {
912 line = mymalloc(line_size);
913 if (compression == BI_RGB) {
914 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
916 if (ig->readcb(ig, line, line_size) != line_size) {
918 i_push_error(0, "failed reading 8-bit bmp data");
922 i_ppal(im, 0, xsize, y, line);
927 else if (compression == BI_RLE8) {
931 unsigned char packed[2];
933 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
936 /* there's always at least 2 bytes in a sequence */
937 if (ig->readcb(ig, packed, 2) != 2) {
939 i_push_error(0, "missing data during decompression");
944 memset(line, packed[1], packed[0]);
945 i_ppal(im, x, x+packed[0], y, line);
949 case BMPRLE_ENDOFLINE:
954 case BMPRLE_ENDOFBMP:
959 if (ig->readcb(ig, packed, 2) != 2) {
961 i_push_error(0, "missing data during decompression");
966 y += yinc * packed[1];
971 read_size = (count+1) / 2 * 2;
972 if (ig->readcb(ig, line, read_size) != read_size) {
974 i_push_error(0, "missing data during decompression");
978 i_ppal(im, x, x+count, y, line);
987 i_push_errorf(0, "unknown 8-bit BMP compression (%d)", compression);
999 static struct bm_masks std_masks[] =
1002 { 0770000, 00007700, 00000077, },
1006 { 0xFF0000, 0x00FF00, 0x0000FF, },
1010 { 0xFF0000, 0x00FF00, 0x0000FF, },
1016 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
1018 Skips the palette and reads in the image data for a direct colour image.
1020 Returns the image or NULL.
1025 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1026 int clr_used, int compression) {
1028 int x, y, lasty, yinc;
1031 int pix_size = bit_count / 8;
1032 int line_size = xsize * pix_size;
1033 struct bm_masks masks;
1034 char unpack_code[2] = "";
1038 const char *compression_name;
1041 unpack_code[0] = *("v3V"+pix_size-2);
1042 unpack_code[1] = '\0';
1044 line_size = (line_size+3) / 4 * 4;
1045 extras = line_size - xsize * pix_size;
1053 /* when ysize is -ve it's a top-down image */
1059 if (compression == BI_RGB) {
1060 compression_name = "BI_RGB";
1061 masks = std_masks[pix_size-2];
1063 /* there's a potential "palette" after the header */
1064 for (i = 0; i < clr_used; ++clr_used) {
1066 if (ig->readcb(ig, buf, 4) != 4) {
1067 i_push_error(0, "skipping colors");
1072 else if (compression == BI_BITFIELDS) {
1074 compression_name = "BI_BITFIELDS";
1076 for (i = 0; i < 3; ++i) {
1077 if (!read_packed(ig, "V", masks.masks+i)) {
1078 i_push_error(0, "reading pixel masks");
1081 /* work out a shift for the mask */
1083 bit = masks.masks[i] & -masks.masks[i];
1088 masks.shifts[i] = pos - 8;
1092 i_push_errorf(0, "unknown 24-bit BMP compression (%d)", compression);
1096 im = i_img_empty(NULL, xsize, ysize);
1100 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1102 /* I wasn't able to make this overflow in testing, but better to be
1104 bytes = sizeof(i_color) * xsize;
1105 if (bytes / sizeof(i_color) != xsize) {
1107 i_push_error(0, "integer overflow calculating buffer size");
1110 line = mymalloc(bytes);
1111 while (y != lasty) {
1113 for (x = 0; x < xsize; ++x) {
1115 if (!read_packed(ig, unpack_code, &pixel)) {
1116 i_push_error(0, "failed reading image data");
1121 for (i = 0; i < 3; ++i) {
1122 if (masks.shifts[i] > 0)
1123 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1125 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1129 i_plin(im, 0, xsize, y, line);
1131 ig->readcb(ig, junk, extras);
1146 Tony Cook <tony@develop-help.com>
1150 Cannot save as compressed BMP.
1154 Doesn't handle OS/2 bitmaps.
1156 16-bit/pixel images haven't been tested. (I need an image).
1158 BI_BITFIELDS compression hasn't been tested (I need an image).