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 /* store the resolution */
150 else if (yres && !xres)
153 i_tags_set_float(&im->tags, "i_xres", 0, xres * 0.0254);
154 i_tags_set_float(&im->tags, "i_yres", 0, yres * 0.0254);
156 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
157 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
165 =head1 IMPLEMENTATION FUNCTIONS
167 Internal functions used in the implementation.
171 =item read_packed(ig, format, ...)
173 Reads from the specified "file" the specified sizes. The format codes
174 match those used by perl's pack() function, though only a few are
175 implemented. In all cases the vararg arguement is an int *.
177 Returns non-zero if all of the arguments were read.
182 int read_packed(io_glue *ig, char *format, ...) {
183 unsigned char buf[4];
187 va_start(ap, format);
190 p = va_arg(ap, int *);
194 if (ig->readcb(ig, buf, 2) == -1)
196 *p = buf[0] + (buf[1] << 8);
200 if (ig->readcb(ig, buf, 4) == -1)
202 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16) + (buf[3] << 24);
206 if (ig->readcb(ig, buf, 1) == -1)
212 if (ig->readcb(ig, buf, 1) == -1)
217 case '3': /* extension - 24-bit number */
218 if (ig->readcb(ig, buf, 3) == -1)
220 *p = buf[0] + (buf[1] << 8) + (buf[2] << 16);
224 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
232 =item write_packed(ig, format, ...)
234 Writes packed data to the specified io_glue.
236 Returns non-zero on success.
242 write_packed(io_glue *ig, char *format, ...) {
243 unsigned char buf[4];
247 va_start(ap, format);
250 i = va_arg(ap, unsigned int);
256 if (ig->writecb(ig, buf, 2) == -1)
262 buf[1] = (i >> 8) & 0xFF;
263 buf[2] = (i >> 16) & 0xFF;
264 buf[3] = (i >> 24) & 0xFF;
265 if (ig->writecb(ig, buf, 4) == -1)
272 if (ig->writecb(ig, buf, 1) == -1)
277 m_fatal(1, "Unknown read_packed format code 0x%02x", *format);
287 =item write_bmphead(ig, im, bit_count, data_size)
289 Writes a Windows BMP header to the file.
291 Returns non-zero on success.
297 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
299 int got_xres, got_yres, aspect_only;
301 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
303 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
304 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
305 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
317 if (xres <= 0 || yres <= 0)
320 /* scale so the smaller value is 72 */
331 /* now to pels/meter */
335 if (im->type == i_palette_type) {
336 colors_used = i_colorcount(im);
337 offset += 4 * colors_used;
340 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', data_size+offset,
341 0, 0, offset, INFOHEAD_SIZE, im->xsize, im->ysize, 1,
342 bit_count, BI_RGB, 0, (int)(xres+0.5), (int)(yres+0.5),
343 colors_used, colors_used)){
344 i_push_error(0, "cannot write bmp header");
347 if (im->type == i_palette_type) {
351 for (i = 0; i < colors_used; ++i) {
352 i_getcolors(im, i, &c, 1);
353 if (im->channels >= 3) {
354 if (!write_packed(ig, "CCCC", c.channel[2], c.channel[1],
356 i_push_error(0, "cannot write palette entry");
361 if (!write_packed(ig, "CCCC", c.channel[0], c.channel[0],
363 i_push_error(0, "cannot write palette entry");
374 =item write_1bit_data(ig, im)
376 Writes the image data as a 1-bit/pixel image.
378 Returns non-zero on success.
383 write_1bit_data(io_glue *ig, i_img *im) {
385 unsigned char *packed;
389 int line_size = (im->xsize+7) / 8;
392 /* round up to nearest multiple of four */
393 line_size = (line_size + 3) / 4 * 4;
395 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
398 line = mymalloc(im->xsize + 8);
399 memset(line + im->xsize, 0, 8);
401 packed = mymalloc(line_size);
402 memset(packed, 0, line_size);
404 for (y = im->ysize-1; y >= 0; --y) {
405 i_gpal(im, 0, im->xsize, y, line);
409 for (x = 0; x < im->xsize; ++x) {
412 if ((mask >>= 1) == 0) {
421 if (ig->writecb(ig, packed, line_size) < 0) {
424 i_push_error(0, "writing 1 bit/pixel packed data");
435 =item write_4bit_data(ig, im)
437 Writes the image data as a 4-bit/pixel image.
439 Returns non-zero on success.
444 write_4bit_data(io_glue *ig, i_img *im) {
446 unsigned char *packed;
448 int line_size = (im->xsize+1) / 2;
451 /* round up to nearest multiple of four */
452 line_size = (line_size + 3) / 4 * 4;
454 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
457 line = mymalloc(im->xsize + 2);
458 memset(line + im->xsize, 0, 2);
460 packed = mymalloc(line_size);
461 memset(packed, 0, line_size);
463 for (y = im->ysize-1; y >= 0; --y) {
464 i_gpal(im, 0, im->xsize, y, line);
466 for (x = 0; x < im->xsize; x += 2) {
467 *out++ = (line[x] << 4) + line[x+1];
469 if (ig->writecb(ig, packed, line_size) < 0) {
472 i_push_error(0, "writing 4 bit/pixel packed data");
483 =item write_8bit_data(ig, im)
485 Writes the image data as a 8-bit/pixel image.
487 Returns non-zero on success.
492 write_8bit_data(io_glue *ig, i_img *im) {
494 int line_size = im->xsize;
497 /* round up to nearest multiple of four */
498 line_size = (line_size + 3) / 4 * 4;
500 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
503 line = mymalloc(im->xsize + 4);
504 memset(line + im->xsize, 0, 4);
506 for (y = im->ysize-1; y >= 0; --y) {
507 i_gpal(im, 0, im->xsize, y, line);
508 if (ig->writecb(ig, line, line_size) < 0) {
510 i_push_error(0, "writing 8 bit/pixel packed data");
519 static int bgr_chans[] = { 2, 1, 0, };
520 static int grey_chans[] = { 0, 0, 0, };
523 =item write_24bit_data(ig, im)
525 Writes the image data as a 24-bit/pixel image.
527 Returns non-zero on success.
532 write_24bit_data(io_glue *ig, i_img *im) {
534 unsigned char *samples;
536 int line_size = 3 * im->xsize;
538 line_size = (line_size + 3) / 4 * 4;
540 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
542 chans = im->channels >= 3 ? bgr_chans : grey_chans;
543 samples = mymalloc(line_size);
544 for (y = im->ysize-1; y >= 0; --y) {
545 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
546 if (ig->writecb(ig, samples, line_size) < 0) {
547 i_push_error(0, "writing image data");
558 =item read_bmp_pal(ig, im, count)
560 Reads count palette entries from the file and add them to the image.
562 Returns non-zero on success.
567 read_bmp_pal(io_glue *ig, i_img *im, int count) {
572 for (i = 0; i < count; ++i) {
573 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
574 i_push_error(0, "reading BMP palette");
580 if (i_addcolors(im, &c, 1) < 0)
588 =item read_1bit_bmp(ig, xsize, ysize, clr_used)
590 Reads in the palette and image data for a 1-bit/pixel image.
592 Returns the image or NULL.
597 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used) {
599 int x, y, lasty, yinc;
601 unsigned char *packed;
602 int line_size = (xsize + 7)/8;
606 line_size = (line_size+3) / 4 * 4;
614 /* when ysize is -ve it's a top-down image */
620 im = i_img_pal_new(xsize, ysize, 3, 256);
623 if (!read_bmp_pal(ig, im, clr_used)) {
628 packed = mymalloc(line_size);
629 line = mymalloc(xsize+8);
631 if (ig->readcb(ig, packed, line_size) != line_size) {
634 i_push_error(0, "reading 1-bit bmp data");
641 for (x = 0; x < xsize; ++x) {
642 *p++ = (*in & bit) ? 1 : 0;
649 i_ppal(im, 0, xsize, y, line);
657 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
659 Reads in the palette and image data for a 4-bit/pixel image.
661 Returns the image or NULL.
663 Hopefully this will be combined with the following function at some
669 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
672 int x, y, lasty, yinc;
674 unsigned char *packed;
675 int line_size = (xsize + 1)/2;
679 line_size = (line_size+3) / 4 * 4;
687 /* when ysize is -ve it's a top-down image */
693 im = i_img_pal_new(xsize, ysize, 3, 256);
696 if (!read_bmp_pal(ig, im, clr_used)) {
702 packed = mymalloc(260);
704 packed = mymalloc(line_size);
705 line = mymalloc(xsize+1);
706 if (compression == BI_RGB) {
708 if (ig->readcb(ig, packed, line_size) != line_size) {
711 i_push_error(0, "reading 4-bit bmp data");
717 for (x = 0; x < xsize; x+=2) {
722 i_ppal(im, 0, xsize, y, line);
726 else if (compression == BI_RLE4) {
733 /* there's always at least 2 bytes in a sequence */
734 if (ig->readcb(ig, packed, 2) != 2) {
737 i_push_error(0, "missing data during decompression");
741 else if (packed[0]) {
742 line[0] = packed[1] >> 4;
743 line[1] = packed[1] & 0x0F;
744 for (i = 0; i < packed[0]; i += 2) {
746 i_ppal(im, x, x+2, y, line);
748 i_ppal(im, x, x+(packed[0]-i), y, line);
753 case BMPRLE_ENDOFLINE:
758 case BMPRLE_ENDOFBMP:
764 if (ig->readcb(ig, packed, 2) != 2) {
767 i_push_error(0, "missing data during decompression");
772 y += yinc * packed[1];
777 size = (count + 1) / 2;
778 read_size = (size+1) / 2 * 2;
779 if (ig->readcb(ig, packed, read_size) != read_size) {
782 i_push_error(0, "missing data during decompression");
783 /*i_img_destroy(im);*/
786 for (i = 0; i < size; ++i) {
787 line[0] = packed[i] >> 4;
788 line[1] = packed[i] & 0xF;
789 i_ppal(im, x, x+2, y, line);
797 else { /*if (compression == BI_RLE4) {*/
800 i_push_error(0, "bad compression for 4-bit image");
809 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
811 Reads in the palette and image data for a 8-bit/pixel image.
813 Returns the image or NULL.
818 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
821 int x, y, lasty, yinc;
823 int line_size = xsize;
826 line_size = (line_size+3) / 4 * 4;
834 /* when ysize is -ve it's a top-down image */
840 im = i_img_pal_new(xsize, ysize, 3, 256);
843 if (!read_bmp_pal(ig, im, clr_used)) {
848 line = mymalloc(line_size);
849 if (compression == BI_RGB) {
851 if (ig->readcb(ig, line, line_size) != line_size) {
853 i_push_error(0, "reading 8-bit bmp data");
857 i_ppal(im, 0, xsize, y, line);
861 else if (compression == BI_RLE8) {
865 unsigned char packed[2];
869 /* there's always at least 2 bytes in a sequence */
870 if (ig->readcb(ig, packed, 2) != 2) {
872 i_push_error(0, "missing data during decompression");
877 memset(line, packed[1], packed[0]);
878 i_ppal(im, x, x+packed[0], y, line);
882 case BMPRLE_ENDOFLINE:
887 case BMPRLE_ENDOFBMP:
892 if (ig->readcb(ig, packed, 2) != 2) {
894 i_push_error(0, "missing data during decompression");
899 y += yinc * packed[1];
904 read_size = (count+1) / 2 * 2;
905 if (ig->readcb(ig, line, read_size) != read_size) {
907 i_push_error(0, "missing data during decompression");
911 i_ppal(im, x, x+count, y, line);
920 i_push_errorf(0, "unknown 8-bit BMP compression %d", compression);
932 static struct bm_masks std_masks[] =
935 { 0770000, 00007700, 00000077, },
939 { 0xFF0000, 0x00FF00, 0x0000FF, },
943 { 0xFF0000, 0x00FF00, 0x0000FF, },
949 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
951 Skips the palette and reads in the image data for a direct colour image.
953 Returns the image or NULL.
958 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
959 int clr_used, int compression) {
961 int x, y, lasty, yinc;
964 int pix_size = bit_count / 8;
965 int line_size = xsize * pix_size;
966 struct bm_masks masks;
967 char unpack_code[2] = "";
972 unpack_code[0] = *("v3V"+pix_size-2);
973 unpack_code[1] = '\0';
975 line_size = (line_size+3) / 4 * 4;
976 extras = line_size - xsize * pix_size;
984 /* when ysize is -ve it's a top-down image */
990 line = mymalloc(line_size);
991 if (compression == BI_RGB) {
992 masks = std_masks[pix_size-2];
994 /* there's a potential "palette" after the header */
995 for (i = 0; i < clr_used; ++clr_used) {
997 if (ig->readcb(ig, buf, 4) != 4) {
998 i_push_error(0, "skipping colors");
1003 else if (compression == BI_BITFIELDS) {
1005 for (i = 0; i < 3; ++i) {
1006 if (!read_packed(ig, "V", masks.masks+i)) {
1007 i_push_error(0, "reading pixel masks");
1010 /* work out a shift for the mask */
1012 bit = masks.masks[i] & -masks.masks[i];
1017 masks.shifts[i] = pos - 8;
1021 im = i_img_empty(NULL, xsize, ysize);
1023 line = mymalloc(sizeof(i_color) * xsize);
1024 while (y != lasty) {
1026 for (x = 0; x < xsize; ++x) {
1028 if (!read_packed(ig, unpack_code, &pixel)) {
1029 i_push_error(0, "reading image data");
1034 for (i = 0; i < 3; ++i) {
1035 if (masks.shifts[i] > 0)
1036 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1038 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1042 i_plin(im, 0, xsize, y, line);
1044 ig->readcb(ig, junk, extras);
1059 Tony Cook <tony@develop-help.com>
1063 Cannot save as compressed BMP.
1067 Doesn't handle OS/2 bitmaps.
1069 16-bit/pixel images haven't been tested. (I need an image).
1071 BI_BITFIELDS compression hasn't been tested (I need an image).