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,
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, dummy, infohead_size;
106 int xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
107 int clr_used, clr_important, offbits;
110 io_glue_commit_types(ig);
113 if (!read_packed(ig, "CCVvvVVVVvvVVVVVV", &b_magic, &m_magic, &filesize,
114 &dummy, &dummy, &offbits, &infohead_size,
115 &xsize, &ysize, &planes,
116 &bit_count, &compression, &size_image, &xres, &yres,
117 &clr_used, &clr_important)) {
118 i_push_error(0, "file too short");
121 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
123 i_push_error(0, "not a BMP file");
129 im = read_1bit_bmp(ig, xsize, ysize, clr_used);
133 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression);
137 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression);
143 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression);
147 i_push_errorf(0, "unknown bit count for BMP file (%d)", bit_count);
151 /* store the resolution */
154 else if (yres && !xres)
157 i_tags_set_float(&im->tags, "i_xres", 0, xres * 0.0254);
158 i_tags_set_float(&im->tags, "i_yres", 0, yres * 0.0254);
160 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
161 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
169 =head1 IMPLEMENTATION FUNCTIONS
171 Internal functions used in the implementation.
175 =item read_packed(ig, format, ...)
177 Reads from the specified "file" the specified sizes. The format codes
178 match those used by perl's pack() function, though only a few are
179 implemented. In all cases the vararg arguement is an int *.
181 Returns non-zero if all of the arguments were read.
186 int read_packed(io_glue *ig, char *format, ...) {
187 unsigned char buf[4];
191 va_start(ap, format);
194 p = va_arg(ap, int *);
198 if (ig->readcb(ig, buf, 2) == -1)
200 *p = buf[0] + (buf[1] << 8);
204 if (ig->readcb(ig, buf, 4) == -1)
206 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
210 if (ig->readcb(ig, buf, 1) == -1)
216 if (ig->readcb(ig, buf, 1) == -1)
221 case '3': /* extension - 24-bit number */
222 if (ig->readcb(ig, buf, 3) == -1)
224 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
228 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
236 =item write_packed(ig, format, ...)
238 Writes packed data to the specified io_glue.
240 Returns non-zero on success.
246 write_packed(io_glue *ig, char *format, ...) {
247 unsigned char buf[4];
251 va_start(ap, format);
254 i = va_arg(ap, unsigned int);
260 if (ig->writecb(ig, buf, 2) == -1)
266 buf[1] = (i >> 8) & 0xFF;
267 buf[2] = (i >> 16) & 0xFF;
268 buf[3] = (i >> 24) & 0xFF;
269 if (ig->writecb(ig, buf, 4) == -1)
276 if (ig->writecb(ig, buf, 1) == -1)
281 m_fatal(1, "Unknown write_packed format code 0x%02x", *format);
291 =item write_bmphead(ig, im, bit_count, data_size)
293 Writes a Windows BMP header to the file.
295 Returns non-zero on success.
301 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
303 int got_xres, got_yres, aspect_only;
305 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
307 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
308 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
309 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
321 if (xres <= 0 || yres <= 0)
324 /* scale so the smaller value is 72 */
335 /* now to pels/meter */
339 if (im->type == i_palette_type) {
340 colors_used = i_colorcount(im);
341 offset += 4 * colors_used;
344 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
345 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
346 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
347 colors_used, colors_used)){
348 i_push_error(0, "cannot write bmp header");
351 if (im->type == i_palette_type) {
355 for (i = 0; i < colors_used; ++i) {
356 i_getcolors(im, i, &c, 1);
357 if (im->channels >= 3) {
358 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
360 i_push_error(0, "cannot write palette entry");
365 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
367 i_push_error(0, "cannot write palette entry");
378 =item write_1bit_data(ig, im)
380 Writes the image data as a 1-bit/pixel image.
382 Returns non-zero on success.
387 write_1bit_data(io_glue *ig, i_img *im) {
389 unsigned char *packed;
393 int line_size = (im->xsize+7) / 8;
396 /* round up to nearest multiple of four */
397 line_size = (line_size + 3) / 4 * 4;
399 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
402 line = mymalloc(im->xsize + 8);
403 memset(line + im->xsize, 0, 8);
405 packed = mymalloc(line_size);
406 memset(packed, 0, line_size);
408 for (y = im->ysize-1; y >= 0; --y) {
409 i_gpal(im, 0, im->xsize, y, line);
413 for (x = 0; x < im->xsize; ++x) {
416 if ((mask >>= 1) == 0) {
425 if (ig->writecb(ig, packed, line_size) < 0) {
428 i_push_error(0, "writing 1 bit/pixel packed data");
441 =item write_4bit_data(ig, im)
443 Writes the image data as a 4-bit/pixel image.
445 Returns non-zero on success.
450 write_4bit_data(io_glue *ig, i_img *im) {
452 unsigned char *packed;
454 int line_size = (im->xsize+1) / 2;
457 /* round up to nearest multiple of four */
458 line_size = (line_size + 3) / 4 * 4;
460 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
463 line = mymalloc(im->xsize + 2);
464 memset(line + im->xsize, 0, 2);
466 packed = mymalloc(line_size);
467 memset(packed, 0, line_size);
469 for (y = im->ysize-1; y >= 0; --y) {
470 i_gpal(im, 0, im->xsize, y, line);
472 for (x = 0; x < im->xsize; x += 2) {
473 *out++ = (line[x] << 4) + line[x+1];
475 if (ig->writecb(ig, packed, line_size) < 0) {
478 i_push_error(0, "writing 4 bit/pixel packed data");
491 =item write_8bit_data(ig, im)
493 Writes the image data as a 8-bit/pixel image.
495 Returns non-zero on success.
500 write_8bit_data(io_glue *ig, i_img *im) {
502 int line_size = im->xsize;
505 /* round up to nearest multiple of four */
506 line_size = (line_size + 3) / 4 * 4;
508 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
511 line = mymalloc(im->xsize + 4);
512 memset(line + im->xsize, 0, 4);
514 for (y = im->ysize-1; y >= 0; --y) {
515 i_gpal(im, 0, im->xsize, y, line);
516 if (ig->writecb(ig, line, line_size) < 0) {
518 i_push_error(0, "writing 8 bit/pixel packed data");
529 static int bgr_chans[] = { 2, 1, 0, };
530 static int grey_chans[] = { 0, 0, 0, };
533 =item write_24bit_data(ig, im)
535 Writes the image data as a 24-bit/pixel image.
537 Returns non-zero on success.
542 write_24bit_data(io_glue *ig, i_img *im) {
544 unsigned char *samples;
546 int line_size = 3 * im->xsize;
548 line_size = (line_size + 3) / 4 * 4;
550 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
552 chans = im->channels >= 3 ? bgr_chans : grey_chans;
553 samples = mymalloc(line_size);
554 memset(samples, 0, line_size);
555 for (y = im->ysize-1; y >= 0; --y) {
556 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
557 if (ig->writecb(ig, samples, line_size) < 0) {
558 i_push_error(0, "writing image data");
571 =item read_bmp_pal(ig, im, count)
573 Reads count palette entries from the file and add them to the image.
575 Returns non-zero on success.
580 read_bmp_pal(io_glue *ig, i_img *im, int count) {
585 for (i = 0; i < count; ++i) {
586 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
587 i_push_error(0, "reading BMP palette");
593 if (i_addcolors(im, &c, 1) < 0)
601 =item read_1bit_bmp(ig, xsize, ysize, clr_used)
603 Reads in the palette and image data for a 1-bit/pixel image.
605 Returns the image or NULL.
610 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used) {
612 int x, y, lasty, yinc;
614 unsigned char *packed;
615 int line_size = (xsize + 7)/8;
619 line_size = (line_size+3) / 4 * 4;
627 /* when ysize is -ve it's a top-down image */
633 im = i_img_pal_new(xsize, ysize, 3, 256);
636 if (!read_bmp_pal(ig, im, clr_used)) {
641 packed = mymalloc(line_size);
642 line = mymalloc(xsize+8);
644 if (ig->readcb(ig, packed, line_size) != line_size) {
647 i_push_error(0, "reading 1-bit bmp data");
654 for (x = 0; x < xsize; ++x) {
655 *p++ = (*in & bit) ? 1 : 0;
662 i_ppal(im, 0, xsize, y, line);
672 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
674 Reads in the palette and image data for a 4-bit/pixel image.
676 Returns the image or NULL.
678 Hopefully this will be combined with the following function at some
684 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
687 int x, y, lasty, yinc;
689 unsigned char *packed;
690 int line_size = (xsize + 1)/2;
694 line_size = (line_size+3) / 4 * 4;
702 /* when ysize is -ve it's a top-down image */
708 im = i_img_pal_new(xsize, ysize, 3, 256);
711 if (!read_bmp_pal(ig, im, clr_used)) {
717 packed = mymalloc(260);
719 packed = mymalloc(line_size);
720 line = mymalloc(xsize+1);
721 if (compression == BI_RGB) {
723 if (ig->readcb(ig, packed, line_size) != line_size) {
726 i_push_error(0, "reading 4-bit bmp data");
732 for (x = 0; x < xsize; x+=2) {
737 i_ppal(im, 0, xsize, y, line);
743 else if (compression == BI_RLE4) {
750 /* there's always at least 2 bytes in a sequence */
751 if (ig->readcb(ig, packed, 2) != 2) {
754 i_push_error(0, "missing data during decompression");
758 else if (packed[0]) {
759 line[0] = packed[1] >> 4;
760 line[1] = packed[1] & 0x0F;
761 for (i = 0; i < packed[0]; i += 2) {
763 i_ppal(im, x, x+2, y, line);
765 i_ppal(im, x, x+(packed[0]-i), y, line);
770 case BMPRLE_ENDOFLINE:
775 case BMPRLE_ENDOFBMP:
781 if (ig->readcb(ig, packed, 2) != 2) {
784 i_push_error(0, "missing data during decompression");
789 y += yinc * packed[1];
794 size = (count + 1) / 2;
795 read_size = (size+1) / 2 * 2;
796 if (ig->readcb(ig, packed, read_size) != read_size) {
799 i_push_error(0, "missing data during decompression");
800 /*i_img_destroy(im);*/
803 for (i = 0; i < size; ++i) {
804 line[0] = packed[i] >> 4;
805 line[1] = packed[i] & 0xF;
806 i_ppal(im, x, x+2, y, line);
814 else { /*if (compression == BI_RLE4) {*/
817 i_push_error(0, "bad compression for 4-bit image");
826 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
828 Reads in the palette and image data for a 8-bit/pixel image.
830 Returns the image or NULL.
835 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
838 int x, y, lasty, yinc;
840 int line_size = xsize;
843 line_size = (line_size+3) / 4 * 4;
851 /* when ysize is -ve it's a top-down image */
857 im = i_img_pal_new(xsize, ysize, 3, 256);
860 if (!read_bmp_pal(ig, im, clr_used)) {
865 line = mymalloc(line_size);
866 if (compression == BI_RGB) {
868 if (ig->readcb(ig, line, line_size) != line_size) {
870 i_push_error(0, "reading 8-bit bmp data");
874 i_ppal(im, 0, xsize, y, line);
879 else if (compression == BI_RLE8) {
883 unsigned char packed[2];
887 /* there's always at least 2 bytes in a sequence */
888 if (ig->readcb(ig, packed, 2) != 2) {
890 i_push_error(0, "missing data during decompression");
895 memset(line, packed[1], packed[0]);
896 i_ppal(im, x, x+packed[0], y, line);
900 case BMPRLE_ENDOFLINE:
905 case BMPRLE_ENDOFBMP:
910 if (ig->readcb(ig, packed, 2) != 2) {
912 i_push_error(0, "missing data during decompression");
917 y += yinc * packed[1];
922 read_size = (count+1) / 2 * 2;
923 if (ig->readcb(ig, line, read_size) != read_size) {
925 i_push_error(0, "missing data during decompression");
929 i_ppal(im, x, x+count, y, line);
938 i_push_errorf(0, "unknown 8-bit BMP compression %d", compression);
950 static struct bm_masks std_masks[] =
953 { 0770000, 00007700, 00000077, },
957 { 0xFF0000, 0x00FF00, 0x0000FF, },
961 { 0xFF0000, 0x00FF00, 0x0000FF, },
967 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
969 Skips the palette and reads in the image data for a direct colour image.
971 Returns the image or NULL.
976 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
977 int clr_used, int compression) {
979 int x, y, lasty, yinc;
982 int pix_size = bit_count / 8;
983 int line_size = xsize * pix_size;
984 struct bm_masks masks;
985 char unpack_code[2] = "";
990 unpack_code[0] = *("v3V"+pix_size-2);
991 unpack_code[1] = '\0';
993 line_size = (line_size+3) / 4 * 4;
994 extras = line_size - xsize * pix_size;
1002 /* when ysize is -ve it's a top-down image */
1008 if (compression == BI_RGB) {
1009 masks = std_masks[pix_size-2];
1011 /* there's a potential "palette" after the header */
1012 for (i = 0; i < clr_used; ++clr_used) {
1014 if (ig->readcb(ig, buf, 4) != 4) {
1015 i_push_error(0, "skipping colors");
1020 else if (compression == BI_BITFIELDS) {
1022 for (i = 0; i < 3; ++i) {
1023 if (!read_packed(ig, "V", masks.masks+i)) {
1024 i_push_error(0, "reading pixel masks");
1027 /* work out a shift for the mask */
1029 bit = masks.masks[i] & -masks.masks[i];
1034 masks.shifts[i] = pos - 8;
1038 im = i_img_empty(NULL, xsize, ysize);
1040 line = mymalloc(sizeof(i_color) * xsize);
1041 while (y != lasty) {
1043 for (x = 0; x < xsize; ++x) {
1045 if (!read_packed(ig, unpack_code, &pixel)) {
1046 i_push_error(0, "reading image data");
1051 for (i = 0; i < 3; ++i) {
1052 if (masks.shifts[i] > 0)
1053 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1055 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1059 i_plin(im, 0, xsize, y, line);
1061 ig->readcb(ig, junk, extras);
1076 Tony Cook <tony@develop-help.com>
1080 Cannot save as compressed BMP.
1084 Doesn't handle OS/2 bitmaps.
1086 16-bit/pixel images haven't been tested. (I need an image).
1088 BI_BITFIELDS compression hasn't been tested (I need an image).