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");
439 =item write_4bit_data(ig, im)
441 Writes the image data as a 4-bit/pixel image.
443 Returns non-zero on success.
448 write_4bit_data(io_glue *ig, i_img *im) {
450 unsigned char *packed;
452 int line_size = (im->xsize+1) / 2;
455 /* round up to nearest multiple of four */
456 line_size = (line_size + 3) / 4 * 4;
458 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
461 line = mymalloc(im->xsize + 2);
462 memset(line + im->xsize, 0, 2);
464 packed = mymalloc(line_size);
465 memset(packed, 0, line_size);
467 for (y = im->ysize-1; y >= 0; --y) {
468 i_gpal(im, 0, im->xsize, y, line);
470 for (x = 0; x < im->xsize; x += 2) {
471 *out++ = (line[x] << 4) + line[x+1];
473 if (ig->writecb(ig, packed, line_size) < 0) {
476 i_push_error(0, "writing 4 bit/pixel packed data");
487 =item write_8bit_data(ig, im)
489 Writes the image data as a 8-bit/pixel image.
491 Returns non-zero on success.
496 write_8bit_data(io_glue *ig, i_img *im) {
498 int line_size = im->xsize;
501 /* round up to nearest multiple of four */
502 line_size = (line_size + 3) / 4 * 4;
504 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
507 line = mymalloc(im->xsize + 4);
508 memset(line + im->xsize, 0, 4);
510 for (y = im->ysize-1; y >= 0; --y) {
511 i_gpal(im, 0, im->xsize, y, line);
512 if (ig->writecb(ig, line, line_size) < 0) {
514 i_push_error(0, "writing 8 bit/pixel packed data");
523 static int bgr_chans[] = { 2, 1, 0, };
524 static int grey_chans[] = { 0, 0, 0, };
527 =item write_24bit_data(ig, im)
529 Writes the image data as a 24-bit/pixel image.
531 Returns non-zero on success.
536 write_24bit_data(io_glue *ig, i_img *im) {
538 unsigned char *samples;
540 int line_size = 3 * im->xsize;
542 line_size = (line_size + 3) / 4 * 4;
544 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
546 chans = im->channels >= 3 ? bgr_chans : grey_chans;
547 samples = mymalloc(line_size);
548 for (y = im->ysize-1; y >= 0; --y) {
549 i_gsamp(im, 0, im->xsize, y, samples, chans, 3);
550 if (ig->writecb(ig, samples, line_size) < 0) {
551 i_push_error(0, "writing image data");
562 =item read_bmp_pal(ig, im, count)
564 Reads count palette entries from the file and add them to the image.
566 Returns non-zero on success.
571 read_bmp_pal(io_glue *ig, i_img *im, int count) {
576 for (i = 0; i < count; ++i) {
577 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
578 i_push_error(0, "reading BMP palette");
584 if (i_addcolors(im, &c, 1) < 0)
592 =item read_1bit_bmp(ig, xsize, ysize, clr_used)
594 Reads in the palette and image data for a 1-bit/pixel image.
596 Returns the image or NULL.
601 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used) {
603 int x, y, lasty, yinc;
605 unsigned char *packed;
606 int line_size = (xsize + 7)/8;
610 line_size = (line_size+3) / 4 * 4;
618 /* when ysize is -ve it's a top-down image */
624 im = i_img_pal_new(xsize, ysize, 3, 256);
627 if (!read_bmp_pal(ig, im, clr_used)) {
632 packed = mymalloc(line_size);
633 line = mymalloc(xsize+8);
635 if (ig->readcb(ig, packed, line_size) != line_size) {
638 i_push_error(0, "reading 1-bit bmp data");
645 for (x = 0; x < xsize; ++x) {
646 *p++ = (*in & bit) ? 1 : 0;
653 i_ppal(im, 0, xsize, y, line);
663 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
665 Reads in the palette and image data for a 4-bit/pixel image.
667 Returns the image or NULL.
669 Hopefully this will be combined with the following function at some
675 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
678 int x, y, lasty, yinc;
680 unsigned char *packed;
681 int line_size = (xsize + 1)/2;
685 line_size = (line_size+3) / 4 * 4;
693 /* when ysize is -ve it's a top-down image */
699 im = i_img_pal_new(xsize, ysize, 3, 256);
702 if (!read_bmp_pal(ig, im, clr_used)) {
708 packed = mymalloc(260);
710 packed = mymalloc(line_size);
711 line = mymalloc(xsize+1);
712 if (compression == BI_RGB) {
714 if (ig->readcb(ig, packed, line_size) != line_size) {
717 i_push_error(0, "reading 4-bit bmp data");
723 for (x = 0; x < xsize; x+=2) {
728 i_ppal(im, 0, xsize, y, line);
734 else if (compression == BI_RLE4) {
741 /* there's always at least 2 bytes in a sequence */
742 if (ig->readcb(ig, packed, 2) != 2) {
745 i_push_error(0, "missing data during decompression");
749 else if (packed[0]) {
750 line[0] = packed[1] >> 4;
751 line[1] = packed[1] & 0x0F;
752 for (i = 0; i < packed[0]; i += 2) {
754 i_ppal(im, x, x+2, y, line);
756 i_ppal(im, x, x+(packed[0]-i), y, line);
761 case BMPRLE_ENDOFLINE:
766 case BMPRLE_ENDOFBMP:
772 if (ig->readcb(ig, packed, 2) != 2) {
775 i_push_error(0, "missing data during decompression");
780 y += yinc * packed[1];
785 size = (count + 1) / 2;
786 read_size = (size+1) / 2 * 2;
787 if (ig->readcb(ig, packed, read_size) != read_size) {
790 i_push_error(0, "missing data during decompression");
791 /*i_img_destroy(im);*/
794 for (i = 0; i < size; ++i) {
795 line[0] = packed[i] >> 4;
796 line[1] = packed[i] & 0xF;
797 i_ppal(im, x, x+2, y, line);
805 else { /*if (compression == BI_RLE4) {*/
808 i_push_error(0, "bad compression for 4-bit image");
817 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression)
819 Reads in the palette and image data for a 8-bit/pixel image.
821 Returns the image or NULL.
826 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
829 int x, y, lasty, yinc;
831 int line_size = xsize;
834 line_size = (line_size+3) / 4 * 4;
842 /* when ysize is -ve it's a top-down image */
848 im = i_img_pal_new(xsize, ysize, 3, 256);
851 if (!read_bmp_pal(ig, im, clr_used)) {
856 line = mymalloc(line_size);
857 if (compression == BI_RGB) {
859 if (ig->readcb(ig, line, line_size) != line_size) {
861 i_push_error(0, "reading 8-bit bmp data");
865 i_ppal(im, 0, xsize, y, line);
870 else if (compression == BI_RLE8) {
874 unsigned char packed[2];
878 /* there's always at least 2 bytes in a sequence */
879 if (ig->readcb(ig, packed, 2) != 2) {
881 i_push_error(0, "missing data during decompression");
886 memset(line, packed[1], packed[0]);
887 i_ppal(im, x, x+packed[0], y, line);
891 case BMPRLE_ENDOFLINE:
896 case BMPRLE_ENDOFBMP:
901 if (ig->readcb(ig, packed, 2) != 2) {
903 i_push_error(0, "missing data during decompression");
908 y += yinc * packed[1];
913 read_size = (count+1) / 2 * 2;
914 if (ig->readcb(ig, line, read_size) != read_size) {
916 i_push_error(0, "missing data during decompression");
920 i_ppal(im, x, x+count, y, line);
929 i_push_errorf(0, "unknown 8-bit BMP compression %d", compression);
941 static struct bm_masks std_masks[] =
944 { 0770000, 00007700, 00000077, },
948 { 0xFF0000, 0x00FF00, 0x0000FF, },
952 { 0xFF0000, 0x00FF00, 0x0000FF, },
958 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression)
960 Skips the palette and reads in the image data for a direct colour image.
962 Returns the image or NULL.
967 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
968 int clr_used, int compression) {
970 int x, y, lasty, yinc;
973 int pix_size = bit_count / 8;
974 int line_size = xsize * pix_size;
975 struct bm_masks masks;
976 char unpack_code[2] = "";
981 unpack_code[0] = *("v3V"+pix_size-2);
982 unpack_code[1] = '\0';
984 line_size = (line_size+3) / 4 * 4;
985 extras = line_size - xsize * pix_size;
993 /* when ysize is -ve it's a top-down image */
999 if (compression == BI_RGB) {
1000 masks = std_masks[pix_size-2];
1002 /* there's a potential "palette" after the header */
1003 for (i = 0; i < clr_used; ++clr_used) {
1005 if (ig->readcb(ig, buf, 4) != 4) {
1006 i_push_error(0, "skipping colors");
1011 else if (compression == BI_BITFIELDS) {
1013 for (i = 0; i < 3; ++i) {
1014 if (!read_packed(ig, "V", masks.masks+i)) {
1015 i_push_error(0, "reading pixel masks");
1018 /* work out a shift for the mask */
1020 bit = masks.masks[i] & -masks.masks[i];
1025 masks.shifts[i] = pos - 8;
1029 im = i_img_empty(NULL, xsize, ysize);
1031 line = mymalloc(sizeof(i_color) * xsize);
1032 while (y != lasty) {
1034 for (x = 0; x < xsize; ++x) {
1036 if (!read_packed(ig, unpack_code, &pixel)) {
1037 i_push_error(0, "reading image data");
1042 for (i = 0; i < 3; ++i) {
1043 if (masks.shifts[i] > 0)
1044 p->channel[i] = (pixel & masks.masks[i]) >> masks.shifts[i];
1046 p->channel[i] = (pixel & masks.masks[i]) << -masks.shifts[i];
1050 i_plin(im, 0, xsize, y, line);
1052 ig->readcb(ig, junk, extras);
1067 Tony Cook <tony@develop-help.com>
1071 Cannot save as compressed BMP.
1075 Doesn't handle OS/2 bitmaps.
1077 16-bit/pixel images haven't been tested. (I need an image).
1079 BI_BITFIELDS compression hasn't been tested (I need an image).