Debian Codegraph @

bmp.c

raw
1 #define IMAGER_NO_CONTEXT
2 #include <stdarg.h>
3 #include "imageri.h"
4
5 /*
6 =head1 NAME
7
8 bmp.c - read and write windows BMP files
9
10 =head1 SYNOPSIS
11
12 i_img *im;
13 io_glue *ig;
14
15 if (!i_writebmp_wiol(im, ig)) {
16 ... error ...
17 }
18 im = i_readbmp(ig);
19
20 =head1 DESCRIPTION
21
22 Reads and writes Windows BMP files.
23
24 =over
25
26 =cut
27 */
28
29 #define FILEHEAD_SIZE 14
30 #define INFOHEAD_SIZE 40
31 #define BI_RGB 0
32 #define BI_RLE8 1
33 #define BI_RLE4 2
34 #define BI_BITFIELDS 3
35 #define BMPRLE_ENDOFLINE 0
36 #define BMPRLE_ENDOFBMP 1
37 #define BMPRLE_DELTA 2
38
39 #define SIGNBIT32 ((i_upacked_t)1U << 31)
40 #define SIGNBIT16 ((i_upacked_t)1U << 15)
41
42 #define SIGNMAX32 ((1UL << 31) - 1)
43
44 static int read_packed(io_glue *ig, char *format, ...);
45 static int write_packed(io_glue *ig, char *format, ...);
46 static int write_bmphead(io_glue *ig, i_img *im, int bit_count,
47 int data_size);
48 static int write_1bit_data(io_glue *ig, i_img *im);
49 static int write_4bit_data(io_glue *ig, i_img *im);
50 static int write_8bit_data(io_glue *ig, i_img *im);
51 static int write_24bit_data(io_glue *ig, i_img *im);
52 static int read_bmp_pal(io_glue *ig, i_img *im, int count);
53 static i_img *read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
54 int compression, long offbits, int allow_incomplete);
55 static i_img *read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
56 int compression, long offbits, int allow_incomplete);
57 static i_img *read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
58 int compression, long offbits, int allow_incomplete);
59 static i_img *read_direct_bmp(io_glue *ig, int xsize, int ysize,
60 int bit_count, int clr_used, int compression,
61 long offbits, int allow_incomplete);
62
63 /* used for the read_packed() and write_packed() functions, an integer
64 * type */
65 typedef long i_packed_t;
66 typedef unsigned long i_upacked_t;
67
68 /*
69 =item i_writebmp_wiol(im, io_glue)
70
71 Writes the image as a BMP file. Uses 1-bit, 4-bit, 8-bit or 24-bit
72 formats depending on the image.
73
74 Never compresses the image.
75
76 =cut
77 */
78 int
79 i_writebmp_wiol(i_img *im, io_glue *ig) {
80 dIMCTXim(im);
81 i_clear_error();
82
83 /* pick a format */
84 if (im->type == i_direct_type) {
85 return write_24bit_data(ig, im);
86 }
87 else {
88 int pal_size;
89
90 /* must be paletted */
91 pal_size = i_colorcount(im);
92 if (pal_size <= 2) {
93 return write_1bit_data(ig, im);
94 }
95 else if (pal_size <= 16) {
96 return write_4bit_data(ig, im);
97 }
98 else {
99 return write_8bit_data(ig, im);
100 }
101 }
102 }
103
104 /*
105 =item i_readbmp_wiol(ig)
106
107 Reads a Windows format bitmap from the given file.
108
109 Handles BI_RLE4 and BI_RLE8 compressed images. Attempts to handle
110 BI_BITFIELDS images too, but I need a test image.
111
112 =cut
113 */
114
115 i_img *
116 i_readbmp_wiol(io_glue *ig, int allow_incomplete) {
117 i_packed_t b_magic, m_magic, filesize, res1, res2, infohead_size;
118 i_packed_t xsize, ysize, planes, bit_count, compression, size_image, xres, yres;
119 i_packed_t clr_used, clr_important, offbits;
120 i_img *im;
121 dIMCTXio(ig);
122
123 im_log((aIMCTX, 1, "i_readbmp_wiol(ig %p)\n", ig));
124
125 i_clear_error();
126
127 if (!read_packed(ig, "CCVvvVVV!V!vvVVVVVV", &b_magic, &m_magic, &filesize,
128 &res1, &res2, &offbits, &infohead_size,
129 &xsize, &ysize, &planes,
130 &bit_count, &compression, &size_image, &xres, &yres,
131 &clr_used, &clr_important)) {
132 i_push_error(0, "file too short to be a BMP file");
133 return 0;
134 }
135 if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE
136 || planes != 1) {
137 i_push_error(0, "not a BMP file");
138 return 0;
139 }
140
141 im_log((aIMCTX, 1, " bmp header: filesize %d offbits %d xsize %d ysize %d planes %d "
142 "bit_count %d compression %d size %d xres %d yres %d clr_used %d "
143 "clr_important %d\n", (int)filesize, (int)offbits, (int)xsize,
144 (int)ysize, (int)planes, (int)bit_count, (int)compression,
145 (int)size_image, (int)xres, (int)yres, (int)clr_used,
146 (int)clr_important));
147
148 if (!i_int_check_image_file_limits(xsize, abs((int)ysize), 3, sizeof(i_sample_t))) {
149 im_log((aIMCTX, 1, "i_readbmp_wiol: image size exceeds limits\n"));
150 return NULL;
151 }
152
153 switch (bit_count) {
154 case 1:
155 im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
156 allow_incomplete);
157 break;
158
159 case 4:
160 im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
161 allow_incomplete);
162 break;
163
164 case 8:
165 im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits,
166 allow_incomplete);
167 break;
168
169 case 32:
170 case 24:
171 case 16:
172 im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression,
173 offbits, allow_incomplete);
174 break;
175
176 default:
177 im_push_errorf(aIMCTX, 0, "unknown bit count for BMP file (%d)", (int)bit_count);
178 return NULL;
179 }
180
181 if (im) {
182 /* store the resolution */
183 if (xres && !yres)
184 yres = xres;
185 else if (yres && !xres)
186 xres = yres;
187 if (xres) {
188 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4);
189 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4);
190 }
191 i_tags_addn(&im->tags, "bmp_compression", 0, compression);
192 i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important);
193 i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used);
194 i_tags_addn(&im->tags, "bmp_filesize", 0, filesize);
195 i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count);
196 i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0);
197 }
198
199 return im;
200 }
201
202 /*
203 =back
204
205 =head1 IMPLEMENTATION FUNCTIONS
206
207 Internal functions used in the implementation.
208
209 =over
210
211 =item read_packed(ig, format, ...)
212
213 Reads from the specified "file" the specified sizes. The format codes
214 match those used by perl's pack() function, though only a few are
215 implemented. In all cases the vararg arguement is an int *.
216
217 Returns non-zero if all of the arguments were read.
218
219 =cut
220 */
221 static int
222 read_packed(io_glue *ig, char *format, ...) {
223 unsigned char buf[4];
224 va_list ap;
225 i_packed_t *p;
226 i_packed_t work;
227 int code;
228 int shrieking; /* format code has a ! flag */
229
230 va_start(ap, format);
231
232 while (*format) {
233 p = va_arg(ap, i_packed_t *);
234
235 code = *format++;
236 shrieking = *format == '!';
237 if (shrieking) ++format;
238
239 switch (code) {
240 case 'v':
241 if (i_io_read(ig, buf, 2) != 2)
242 goto fail;
243 work = buf[0] + ((i_packed_t)buf[1] << 8);
244 if (shrieking)
245 *p = (work ^ SIGNBIT16) - SIGNBIT16;
246 else
247 *p = work;
248 break;
249
250 case 'V':
251 if (i_io_read(ig, buf, 4) != 4)
252 goto fail;
253 work = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16) + ((i_packed_t)buf[3] << 24);
254 if (shrieking)
255 *p = (work ^ SIGNBIT32) - SIGNBIT32;
256 else
257 *p = work;
258 break;
259
260 case 'C':
261 if (i_io_read(ig, buf, 1) != 1)
262 goto fail;
263 *p = buf[0];
264 break;
265
266 case 'c':
267 if (i_io_read(ig, buf, 1) != 1)
268 goto fail;
269 *p = (char)buf[0];
270 break;
271
272 case '3': /* extension - 24-bit number */
273 if (i_io_read(ig, buf, 3) != 3)
274 goto fail;
275 *p = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16);
276 break;
277
278 default:
279 {
280 dIMCTXio(ig);
281 im_fatal(aIMCTX, 1, "Unknown read_packed format code 0x%02x", code);
282 }
283 }
284 }
285 va_end(ap);
286 return 1;
287
288 fail:
289 va_end(ap);
290 return 0;
291 }
292
293 /*
294 =item write_packed(ig, format, ...)
295
296 Writes packed data to the specified io_glue.
297
298 Returns non-zero on success.
299
300 =cut
301 */
302
303 static int
304 write_packed(io_glue *ig, char *format, ...) {
305 unsigned char buf[4];
306 va_list ap;
307 int i;
308
309 va_start(ap, format);
310
311 while (*format) {
312 i = va_arg(ap, i_upacked_t);
313
314 switch (*format) {
315 case 'v':
316 buf[0] = i & 255;
317 buf[1] = i / 256;
318 if (i_io_write(ig, buf, 2) == -1)
319 goto fail;
320 break;
321
322 case 'V':
323 buf[0] = i & 0xFF;
324 buf[1] = (i >> 8) & 0xFF;
325 buf[2] = (i >> 16) & 0xFF;
326 buf[3] = (i >> 24) & 0xFF;
327 if (i_io_write(ig, buf, 4) == -1)
328 goto fail;
329 break;
330
331 case 'C':
332 case 'c':
333 buf[0] = i & 0xFF;
334 if (i_io_write(ig, buf, 1) == -1)
335 goto fail;
336 break;
337
338 default:
339 {
340 dIMCTXio(ig);
341 im_fatal(aIMCTX, 1, "Unknown write_packed format code 0x%02x", *format);
342 }
343 }
344 ++format;
345 }
346 va_end(ap);
347
348 return 1;
349
350 fail:
351 va_end(ap);
352 return 0;
353 }
354
355 /*
356 =item write_bmphead(ig, im, bit_count, data_size)
357
358 Writes a Windows BMP header to the file.
359
360 Returns non-zero on success.
361
362 =cut
363 */
364
365 static
366 int write_bmphead(io_glue *ig, i_img *im, int bit_count, int data_size) {
367 double xres, yres;
368 int got_xres, got_yres, aspect_only;
369 int colors_used = 0;
370 int offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
371 dIMCTXim(im);
372
373 if ((i_img_dim_u)im->xsize > SIGNMAX32
374 || (i_img_dim_u)im->ysize > SIGNMAX32) {
375 i_push_error(0, "image too large to write to BMP");
376 return 0;
377 }
378
379 got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
380 got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);
381 if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only))
382 aspect_only = 0;
383 if (!got_xres) {
384 if (!got_yres)
385 xres = yres = 72;
386 else
387 xres = yres;
388 }
389 else {
390 if (!got_yres)
391 yres = xres;
392 }
393 if (xres <= 0 || yres <= 0)
394 xres = yres = 72;
395 if (aspect_only) {
396 /* scale so the smaller value is 72 */
397 double ratio;
398 if (xres < yres) {
399 ratio = 72.0 / xres;
400 }
401 else {
402 ratio = 72.0 / yres;
403 }
404 xres *= ratio;
405 yres *= ratio;
406 }
407 /* now to pels/meter */
408 xres *= 100.0/2.54;
409 yres *= 100.0/2.54;
410
411 if (im->type == i_palette_type) {
412 colors_used = i_colorcount(im);
413 offset += 4 * colors_used;
414 }
415
416 if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M',
417 (i_upacked_t)(data_size+offset),
418 (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset,
419 (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize,
420 (i_upacked_t)im->ysize, (i_upacked_t)1,
421 (i_upacked_t)bit_count, (i_upacked_t)BI_RGB,
422 (i_upacked_t)data_size,
423 (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5),
424 (i_upacked_t)colors_used, (i_upacked_t)colors_used)){
425 i_push_error(0, "cannot write bmp header");
426 return 0;
427 }
428 if (im->type == i_palette_type) {
429 int i;
430 i_color c;
431
432 for (i = 0; i < colors_used; ++i) {
433 i_getcolors(im, i, &c, 1);
434 if (im->channels >= 3) {
435 if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]),
436 (i_upacked_t)(c.channel[1]),
437 (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) {
438 i_push_error(0, "cannot write palette entry");
439 return 0;
440 }
441 }
442 else {
443 i_upacked_t v = c.channel[0];
444 if (!write_packed(ig, "CCCC", v, v, v, 0)) {
445 i_push_error(0, "cannot write palette entry");
446 return 0;
447 }
448 }
449 }
450 }
451
452 return 1;
453 }
454
455 /*
456 =item write_1bit_data(ig, im)
457
458 Writes the image data as a 1-bit/pixel image.
459
460 Returns non-zero on success.
461
462 =cut
463 */
464 static int
465 write_1bit_data(io_glue *ig, i_img *im) {
466 i_palidx *line;
467 unsigned char *packed;
468 int byte;
469 int mask;
470 unsigned char *out;
471 int line_size = (im->xsize+7) / 8;
472 int x, y;
473 int unpacked_size;
474 dIMCTXim(im);
475
476 /* round up to nearest multiple of four */
477 line_size = (line_size + 3) / 4 * 4;
478
479 if (!write_bmphead(ig, im, 1, line_size * im->ysize))
480 return 0;
481
482 /* this shouldn't be an issue, but let's be careful */
483 unpacked_size = im->xsize + 8;
484 if (unpacked_size < im->xsize) {
485 i_push_error(0, "integer overflow during memory allocation");
486 return 0;
487 }
488 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
489 memset(line + im->xsize, 0, 8);
490
491 /* size allocated here is always much smaller than xsize, hence
492 can't overflow int */
493 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
494 memset(packed, 0, line_size);
495
496 for (y = im->ysize-1; y >= 0; --y) {
497 i_gpal(im, 0, im->xsize, y, line);
498 mask = 0x80;
499 byte = 0;
500 out = packed;
501 for (x = 0; x < im->xsize; ++x) {
502 if (line[x])
503 byte |= mask;
504 if ((mask >>= 1) == 0) {
505 *out++ = byte;
506 byte = 0;
507 mask = 0x80;
508 }
509 }
510 if (mask != 0x80) {
511 *out++ = byte;
512 }
513 if (i_io_write(ig, packed, line_size) < 0) {
514 myfree(packed);
515 myfree(line);
516 i_push_error(0, "writing 1 bit/pixel packed data");
517 return 0;
518 }
519 }
520 myfree(packed);
521 myfree(line);
522
523 if (i_io_close(ig))
524 return 0;
525
526 return 1;
527 }
528
529 /*
530 =item write_4bit_data(ig, im)
531
532 Writes the image data as a 4-bit/pixel image.
533
534 Returns non-zero on success.
535
536 =cut
537 */
538 static int
539 write_4bit_data(io_glue *ig, i_img *im) {
540 i_palidx *line;
541 unsigned char *packed;
542 unsigned char *out;
543 int line_size = (im->xsize+1) / 2;
544 int x, y;
545 int unpacked_size;
546 dIMCTXim(im);
547
548 /* round up to nearest multiple of four */
549 line_size = (line_size + 3) / 4 * 4;
550
551 if (!write_bmphead(ig, im, 4, line_size * im->ysize))
552 return 0;
553
554 /* this shouldn't be an issue, but let's be careful */
555 unpacked_size = im->xsize + 2;
556 if (unpacked_size < im->xsize) {
557 i_push_error(0, "integer overflow during memory allocation");
558 return 0;
559 }
560 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
561 memset(line + im->xsize, 0, 2);
562
563 /* size allocated here is always much smaller than xsize, hence
564 can't overflow int */
565 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
566 memset(packed, 0, line_size);
567
568 for (y = im->ysize-1; y >= 0; --y) {
569 i_gpal(im, 0, im->xsize, y, line);
570 out = packed;
571 for (x = 0; x < im->xsize; x += 2) {
572 *out++ = (line[x] << 4) + line[x+1];
573 }
574 if (i_io_write(ig, packed, line_size) < 0) {
575 myfree(packed);
576 myfree(line);
577 i_push_error(0, "writing 4 bit/pixel packed data");
578 return 0;
579 }
580 }
581 myfree(packed);
582 myfree(line);
583
584 if (i_io_close(ig))
585 return 0;
586
587 return 1;
588 }
589
590 /*
591 =item write_8bit_data(ig, im)
592
593 Writes the image data as a 8-bit/pixel image.
594
595 Returns non-zero on success.
596
597 =cut
598 */
599 static int
600 write_8bit_data(io_glue *ig, i_img *im) {
601 i_palidx *line;
602 int line_size = im->xsize;
603 int y;
604 int unpacked_size;
605 dIMCTXim(im);
606
607 /* round up to nearest multiple of four */
608 line_size = (line_size + 3) / 4 * 4;
609
610 if (!write_bmphead(ig, im, 8, line_size * im->ysize))
611 return 0;
612
613 /* this shouldn't be an issue, but let's be careful */
614 unpacked_size = im->xsize + 4;
615 if (unpacked_size < im->xsize) {
616 i_push_error(0, "integer overflow during memory allocation");
617 return 0;
618 }
619 line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */
620 memset(line + im->xsize, 0, 4);
621
622 for (y = im->ysize-1; y >= 0; --y) {
623 i_gpal(im, 0, im->xsize, y, line);
624 if (i_io_write(ig, line, line_size) < 0) {
625 myfree(line);
626 i_push_error(0, "writing 8 bit/pixel packed data");
627 return 0;
628 }
629 }
630 myfree(line);
631
632 if (i_io_close(ig))
633 return 0;
634
635 return 1;
636 }
637
638 /*
639 =item write_24bit_data(ig, im)
640
641 Writes the image data as a 24-bit/pixel image.
642
643 Returns non-zero on success.
644
645 =cut
646 */
647 static int
648 write_24bit_data(io_glue *ig, i_img *im) {
649 unsigned char *samples;
650 int y;
651 int line_size = 3 * im->xsize;
652 i_color bg;
653 dIMCTXim(im);
654
655 i_get_file_background(im, &bg);
656
657 /* just in case we implement a direct format with 2bytes/pixel
658 (unlikely though) */
659 if (line_size / 3 != im->xsize) {
660 i_push_error(0, "integer overflow during memory allocation");
661 return 0;
662 }
663
664 line_size = (line_size + 3) / 4 * 4;
665
666 if (!write_bmphead(ig, im, 24, line_size * im->ysize))
667 return 0;
668 samples = mymalloc(4 * im->xsize);
669 memset(samples, 0, line_size);
670 for (y = im->ysize-1; y >= 0; --y) {
671 unsigned char *samplep = samples;
672 int x;
673 i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg);
674 for (x = 0; x < im->xsize; ++x) {
675 unsigned char tmp = samplep[2];
676 samplep[2] = samplep[0];
677 samplep[0] = tmp;
678 samplep += 3;
679 }
680 if (i_io_write(ig, samples, line_size) < 0) {
681 i_push_error(0, "writing image data");
682 myfree(samples);
683 return 0;
684 }
685 }
686 myfree(samples);
687
688 if (i_io_close(ig))
689 return 0;
690
691 return 1;
692 }
693
694 /*
695 =item read_bmp_pal(ig, im, count)
696
697 Reads count palette entries from the file and add them to the image.
698
699 Returns non-zero on success.
700
701 =cut
702 */
703 static int
704 read_bmp_pal(io_glue *ig, i_img *im, int count) {
705 int i;
706 i_packed_t r, g, b, x;
707 i_color c;
708 dIMCTXio(ig);
709
710 for (i = 0; i < count; ++i) {
711 if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) {
712 i_push_error(0, "reading BMP palette");
713 return 0;
714 }
715 c.channel[0] = r;
716 c.channel[1] = g;
717 c.channel[2] = b;
718 if (i_addcolors(im, &c, 1) < 0) {
719 i_push_error(0, "out of space in image palette");
720 return 0;
721 }
722 }
723
724 return 1;
725 }
726
727 /*
728 =item read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits)
729
730 Reads in the palette and image data for a 1-bit/pixel image.
731
732 Returns the image or NULL.
733
734 =cut
735 */
736 static i_img *
737 read_1bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
738 int compression, long offbits, int allow_incomplete) {
739 i_img *im;
740 int x, y, lasty, yinc, start_y;
741 i_palidx *line, *p;
742 unsigned char *packed;
743 int line_size = (xsize + 7)/8;
744 int bit;
745 unsigned char *in;
746 long base_offset;
747 dIMCTXio(ig);
748
749 if (compression != BI_RGB) {
750 im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression);
751 return NULL;
752 }
753
754 if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */
755 /* we check with 8 because we allocate that much for the decoded
756 line buffer */
757 i_push_error(0, "integer overflow during memory allocation");
758 return NULL;
759 }
760
761 /* if xsize+7 is ok then (xsize+7)/8 will be and the minor
762 adjustments below won't make it overflow */
763 line_size = (line_size+3) / 4 * 4;
764
765 if (ysize > 0) {
766 start_y = ysize-1;
767 lasty = -1;
768 yinc = -1;
769 }
770 else {
771 /* when ysize is -ve it's a top-down image */
772 ysize = -ysize;
773 start_y = 0;
774 lasty = ysize;
775 yinc = 1;
776 }
777 y = start_y;
778 if (!clr_used)
779 clr_used = 2;
780 if (clr_used < 0 || clr_used > 2) {
781 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
782 return NULL;
783 }
784
785 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
786 if (offbits < base_offset) {
787 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
788 return NULL;
789 }
790
791 im = i_img_pal_new(xsize, ysize, 3, 256);
792 if (!im)
793 return NULL;
794 if (!read_bmp_pal(ig, im, clr_used)) {
795 i_img_destroy(im);
796 return NULL;
797 }
798
799 if (offbits > base_offset) {
800 /* this will be slow if the offset is large, but that should be
801 rare */
802 char buffer;
803 while (base_offset < offbits) {
804 if (i_io_read(ig, &buffer, 1) != 1) {
805 i_img_destroy(im);
806 i_push_error(0, "failed skipping to image data offset");
807 return NULL;
808 }
809 ++base_offset;
810 }
811 }
812
813 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
814
815 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
816 line = mymalloc(xsize+8); /* checked 29jun05 tonyc */
817 while (y != lasty) {
818 if (i_io_read(ig, packed, line_size) != line_size) {
819 myfree(packed);
820 myfree(line);
821 if (allow_incomplete) {
822 i_tags_setn(&im->tags, "i_incomplete", 1);
823 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
824 return im;
825 }
826 else {
827 i_push_error(0, "failed reading 1-bit bmp data");
828 i_img_destroy(im);
829 return NULL;
830 }
831 }
832 in = packed;
833 bit = 0x80;
834 p = line;
835 for (x = 0; x < xsize; ++x) {
836 *p++ = (*in & bit) ? 1 : 0;
837 bit >>= 1;
838 if (!bit) {
839 ++in;
840 bit = 0x80;
841 }
842 }
843 i_ppal(im, 0, xsize, y, line);
844 y += yinc;
845 }
846
847 myfree(packed);
848 myfree(line);
849 return im;
850 }
851
852 /*
853 =item read_4bit_bmp(ig, xsize, ysize, clr_used, compression)
854
855 Reads in the palette and image data for a 4-bit/pixel image.
856
857 Returns the image or NULL.
858
859 Hopefully this will be combined with the following function at some
860 point.
861
862 =cut
863 */
864 static i_img *
865 read_4bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
866 int compression, long offbits, int allow_incomplete) {
867 i_img *im;
868 int x, y, lasty, yinc;
869 i_palidx *line, *p;
870 unsigned char *packed;
871 int line_size = (xsize + 1)/2;
872 unsigned char *in;
873 int size, i;
874 long base_offset;
875 int starty;
876 dIMCTXio(ig);
877
878 /* line_size is going to be smaller than xsize in most cases (and
879 when it's not, xsize is itself small), and hence not overflow */
880 line_size = (line_size+3) / 4 * 4;
881
882 if (ysize > 0) {
883 starty = ysize-1;
884 lasty = -1;
885 yinc = -1;
886 }
887 else {
888 /* when ysize is -ve it's a top-down image */
889 ysize = -ysize;
890 starty = 0;
891 lasty = ysize;
892 yinc = 1;
893 }
894 y = starty;
895 if (!clr_used)
896 clr_used = 16;
897
898 if (clr_used > 16 || clr_used < 0) {
899 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
900 return NULL;
901 }
902
903 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
904 if (offbits < base_offset) {
905 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
906 return NULL;
907 }
908
909 im = i_img_pal_new(xsize, ysize, 3, 256);
910 if (!im) /* error should have been pushed already */
911 return NULL;
912 if (!read_bmp_pal(ig, im, clr_used)) {
913 i_img_destroy(im);
914 return NULL;
915 }
916
917 if (offbits > base_offset) {
918 /* this will be slow if the offset is large, but that should be
919 rare */
920 char buffer;
921 while (base_offset < offbits) {
922 if (i_io_read(ig, &buffer, 1) != 1) {
923 i_img_destroy(im);
924 i_push_error(0, "failed skipping to image data offset");
925 return NULL;
926 }
927 ++base_offset;
928 }
929 }
930
931 if (line_size < 260)
932 packed = mymalloc(260); /* checked 29jun05 tonyc */
933 else
934 packed = mymalloc(line_size); /* checked 29jun05 tonyc */
935 /* xsize won't approach MAXINT */
936 line = mymalloc(xsize+1); /* checked 29jun05 tonyc */
937 if (compression == BI_RGB) {
938 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
939 while (y != lasty) {
940 if (i_io_read(ig, packed, line_size) != line_size) {
941 myfree(packed);
942 myfree(line);
943 if (allow_incomplete) {
944 i_tags_setn(&im->tags, "i_incomplete", 1);
945 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
946 return im;
947 }
948 else {
949 i_push_error(0, "failed reading 4-bit bmp data");
950 i_img_destroy(im);
951 return NULL;
952 }
953 }
954 in = packed;
955 p = line;
956 for (x = 0; x < xsize; x+=2) {
957 *p++ = *in >> 4;
958 *p++ = *in & 0x0F;
959 ++in;
960 }
961 i_ppal(im, 0, xsize, y, line);
962 y += yinc;
963 }
964 myfree(packed);
965 myfree(line);
966 }
967 else if (compression == BI_RLE4) {
968 int read_size;
969 int count;
970 i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */
971
972 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0);
973 x = 0;
974 while (1) {
975 /* there's always at least 2 bytes in a sequence */
976 if (i_io_read(ig, packed, 2) != 2) {
977 myfree(packed);
978 myfree(line);
979 if (allow_incomplete) {
980 i_tags_setn(&im->tags, "i_incomplete", 1);
981 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
982 return im;
983 }
984 else {
985 i_push_error(0, "missing data during decompression");
986 i_img_destroy(im);
987 return NULL;
988 }
989 }
990 else if (packed[0]) {
991 int count = packed[0];
992 if (x + count > xlimit) {
993 /* this file is corrupt */
994 myfree(packed);
995 myfree(line);
996 i_push_error(0, "invalid data during decompression");
997 im_log((aIMCTX, 1, "read 4-bit: scanline overflow x %d + count %d vs xlimit %d (y %d)\n",
998 (int)x, count, (int)xlimit, (int)y));
999 i_img_destroy(im);
1000 return NULL;
1001 }
1002 /* fill in the line */
1003 for (i = 0; i < count; i += 2)
1004 line[i] = packed[1] >> 4;
1005 for (i = 1; i < count; i += 2)
1006 line[i] = packed[1] & 0x0F;
1007 i_ppal(im, x, x+count, y, line);
1008 x += count;
1009 } else {
1010 switch (packed[1]) {
1011 case BMPRLE_ENDOFLINE:
1012 x = 0;
1013 y += yinc;
1014 break;
1015
1016 case BMPRLE_ENDOFBMP:
1017 myfree(packed);
1018 myfree(line);
1019 return im;
1020
1021 case BMPRLE_DELTA:
1022 if (i_io_read(ig, packed, 2) != 2) {
1023 myfree(packed);
1024 myfree(line);
1025 if (allow_incomplete) {
1026 i_tags_setn(&im->tags, "i_incomplete", 1);
1027 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1028 return im;
1029 }
1030 else {
1031 i_push_error(0, "missing data during decompression");
1032 i_img_destroy(im);
1033 return NULL;
1034 }
1035 }
1036 x += packed[0];
1037 y += yinc * packed[1];
1038 break;
1039
1040 default:
1041 count = packed[1];
1042 if (x + count > xlimit) {
1043 /* this file is corrupt */
1044 myfree(packed);
1045 myfree(line);
1046 i_push_error(0, "invalid data during decompression");
1047 im_log((aIMCTX, 1, "read 4-bit: scanline overflow (unpacked) x %d + count %d vs xlimit %d (y %d)\n",
1048 (int)x, count, (int)xlimit, (int)y));
1049 i_img_destroy(im);
1050 return NULL;
1051 }
1052 size = (count + 1) / 2;
1053 read_size = (size+1) / 2 * 2;
1054 if (i_io_read(ig, packed, read_size) != read_size) {
1055 myfree(packed);
1056 myfree(line);
1057 if (allow_incomplete) {
1058 i_tags_setn(&im->tags, "i_incomplete", 1);
1059 i_tags_setn(&im->tags, "i_lines_read", abs(y - starty));
1060 return im;
1061 }
1062 else {
1063 i_push_error(0, "missing data during decompression");
1064 i_img_destroy(im);
1065 return NULL;
1066 }
1067 }
1068 for (i = 0; i < size; ++i) {
1069 line[0] = packed[i] >> 4;
1070 line[1] = packed[i] & 0xF;
1071 i_ppal(im, x, x+2, y, line);
1072 x += 2;
1073 }
1074 break;
1075 }
1076 }
1077 }
1078 }
1079 else { /*if (compression == BI_RLE4) {*/
1080 myfree(packed);
1081 myfree(line);
1082 im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression);
1083 i_img_destroy(im);
1084 return NULL;
1085 }
1086
1087 return im;
1088 }
1089
1090 /*
1091 =item read_8bit_bmp(ig, xsize, ysize, clr_used, compression, allow_incomplete)
1092
1093 Reads in the palette and image data for a 8-bit/pixel image.
1094
1095 Returns the image or NULL.
1096
1097 =cut
1098 */
1099 static i_img *
1100 read_8bit_bmp(io_glue *ig, int xsize, int ysize, int clr_used,
1101 int compression, long offbits, int allow_incomplete) {
1102 i_img *im;
1103 int x, y, lasty, yinc, start_y;
1104 i_palidx *line;
1105 int line_size = xsize;
1106 long base_offset;
1107 dIMCTXio(ig);
1108
1109 line_size = (line_size+3) / 4 * 4;
1110 if (line_size < xsize) { /* if it overflowed (unlikely, but check) */
1111 i_push_error(0, "integer overflow during memory allocation");
1112 return NULL;
1113 }
1114
1115 if (ysize > 0) {
1116 start_y = ysize-1;
1117 lasty = -1;
1118 yinc = -1;
1119 }
1120 else {
1121 /* when ysize is -ve it's a top-down image */
1122 ysize = -ysize;
1123 start_y = 0;
1124 lasty = ysize;
1125 yinc = 1;
1126 }
1127 y = start_y;
1128 if (!clr_used)
1129 clr_used = 256;
1130 if (clr_used > 256 || clr_used < 0) {
1131 im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used);
1132 return NULL;
1133 }
1134
1135 base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4;
1136 if (offbits < base_offset) {
1137 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1138 return NULL;
1139 }
1140
1141 im = i_img_pal_new(xsize, ysize, 3, 256);
1142 if (!im)
1143 return NULL;
1144 if (!read_bmp_pal(ig, im, clr_used)) {
1145 i_img_destroy(im);
1146 return NULL;
1147 }
1148
1149 if (offbits > base_offset) {
1150 /* this will be slow if the offset is large, but that should be
1151 rare */
1152 char buffer;
1153 while (base_offset < offbits) {
1154 if (i_io_read(ig, &buffer, 1) != 1) {
1155 i_img_destroy(im);
1156 i_push_error(0, "failed skipping to image data offset");
1157 return NULL;
1158 }
1159 ++base_offset;
1160 }
1161 }
1162
1163 line = mymalloc(line_size); /* checked 29jun05 tonyc */
1164 if (compression == BI_RGB) {
1165 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0);
1166 while (y != lasty) {
1167 if (i_io_read(ig, line, line_size) != line_size) {
1168 myfree(line);
1169 if (allow_incomplete) {
1170 i_tags_setn(&im->tags, "i_incomplete", 1);
1171 i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y));
1172 return im;
1173 }
1174 else {
1175 i_push_error(0, "failed reading 8-bit bmp data");
1176 i_img_destroy(im);
1177 return NULL;
1178 }
1179 }
1180 i_ppal(im, 0, xsize, y, line);
1181 y += yinc;
1182 }
1183 myfree(line);
1184 }
1185 else if (compression == BI_RLE8) {
1186 int read_size;
1187 int count;
1188 unsigned char packed[2];
1189
1190 i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0);
1191 x = 0;
1192 while (1) {
1193 /* there's always at least 2 bytes in a sequence */
1194 if (i_io_read(ig, packed, 2) != 2) {
1195 myfree(line);
1196 if (allow_incomplete) {
1197 i_tags_setn(&im->tags, "i_incomplete", 1);
1198 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1199 return im;
1200 }
1201 else {
1202 i_push_error(0, "missing data during decompression");
1203 i_img_destroy(im);
1204 return NULL;
1205 }
1206 }
1207 if (packed[0]) {
1208 if (x + packed[0] > xsize) {
1209 /* this file isn't incomplete, it's corrupt */
1210 myfree(line);
1211 i_push_error(0, "invalid data during decompression");
1212 i_img_destroy(im);
1213 return NULL;
1214 }
1215 memset(line, packed[1], packed[0]);
1216 i_ppal(im, x, x+packed[0], y, line);
1217 x += packed[0];
1218 } else {
1219 switch (packed[1]) {
1220 case BMPRLE_ENDOFLINE:
1221 x = 0;
1222 y += yinc;
1223 break;
1224
1225 case BMPRLE_ENDOFBMP:
1226 myfree(line);
1227 return im;
1228
1229 case BMPRLE_DELTA:
1230 if (i_io_read(ig, packed, 2) != 2) {
1231 myfree(line);
1232 if (allow_incomplete) {
1233 i_tags_setn(&im->tags, "i_incomplete", 1);
1234 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1235 return im;
1236 }
1237 else {
1238 i_push_error(0, "missing data during decompression");
1239 i_img_destroy(im);
1240 return NULL;
1241 }
1242 }
1243 x += packed[0];
1244 y += yinc * packed[1];
1245 break;
1246
1247 default:
1248 count = packed[1];
1249 if (x + count > xsize) {
1250 /* runs shouldn't cross a line boundary */
1251 /* this file isn't incomplete, it's corrupt */
1252 myfree(line);
1253 i_push_error(0, "invalid data during decompression");
1254 i_img_destroy(im);
1255 return NULL;
1256 }
1257 read_size = (count+1) / 2 * 2;
1258 if (i_io_read(ig, line, read_size) != read_size) {
1259 myfree(line);
1260 if (allow_incomplete) {
1261 i_tags_setn(&im->tags, "i_incomplete", 1);
1262 i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y));
1263 return im;
1264 }
1265 else {
1266 i_push_error(0, "missing data during decompression");
1267 i_img_destroy(im);
1268 return NULL;
1269 }
1270 }
1271 i_ppal(im, x, x+count, y, line);
1272 x += count;
1273 break;
1274 }
1275 }
1276 }
1277 }
1278 else {
1279 myfree(line);
1280 im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression);
1281 i_img_destroy(im);
1282 return NULL;
1283 }
1284
1285 return im;
1286 }
1287
1288 struct bm_masks {
1289 unsigned masks[3];
1290 int shifts[3];
1291 int bits[3];
1292 };
1293 static struct bm_masks std_masks[] =
1294 {
1295 { /* 16-bit */
1296 { 0076000, 00001740, 00000037, },
1297 { 10, 5, 0, },
1298 { 5, 5, 5, }
1299 },
1300 { /* 24-bit */
1301 { 0xFF0000, 0x00FF00, 0x0000FF, },
1302 { 16, 8, 0, },
1303 { 8, 8, 8, },
1304 },
1305 { /* 32-bit */
1306 { 0xFF0000, 0x00FF00, 0x0000FF, },
1307 { 16, 8, 0, },
1308 { 8, 8, 8, },
1309 },
1310 };
1311
1312 /* multiplier and shift for converting from N bits to 8 bits */
1313 struct bm_sampconverts {
1314 int mult;
1315 int shift;
1316 };
1317 static struct bm_sampconverts samp_converts[] = {
1318 { 0xff, 0 }, /* 1 bit samples */
1319 { 0x55, 0 },
1320 { 0111, 1 },
1321 { 0x11, 0 },
1322 { 0x21, 2 },
1323 { 0x41, 4 },
1324 { 0x81, 6 } /* 7 bit samples */
1325 };
1326
1327 /*
1328 =item read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, allow_incomplete)
1329
1330 Skips the palette and reads in the image data for a direct colour image.
1331
1332 Returns the image or NULL.
1333
1334 =cut
1335 */
1336 static i_img *
1337 read_direct_bmp(io_glue *ig, int xsize, int ysize, int bit_count,
1338 int clr_used, int compression, long offbits,
1339 int allow_incomplete) {
1340 i_img *im;
1341 int x, y, starty, lasty, yinc;
1342 i_color *line, *p;
1343 int pix_size = bit_count / 8;
1344 int line_size = xsize * pix_size;
1345 struct bm_masks masks;
1346 char unpack_code[2] = "";
1347 int i;
1348 int extras;
1349 char junk[4];
1350 const char *compression_name;
1351 int bytes;
1352 long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE;
1353 dIMCTXio(ig);
1354
1355 unpack_code[0] = "v3V"[pix_size-2];
1356 unpack_code[1] = '\0';
1357
1358 line_size = (line_size+3) / 4 * 4;
1359 extras = line_size - xsize * pix_size;
1360
1361 if (ysize > 0) {
1362 starty = ysize-1;
1363 lasty = -1;
1364 yinc = -1;
1365 }
1366 else {
1367 /* when ysize is -ve it's a top-down image */
1368 ysize = -ysize;
1369 starty = 0;
1370 lasty = ysize;
1371 yinc = 1;
1372 }
1373 y = starty;
1374 if (compression == BI_RGB) {
1375 compression_name = "BI_RGB";
1376 masks = std_masks[pix_size-2];
1377
1378 /* there's a potential "palette" after the header */
1379 for (i = 0; i < clr_used; ++clr_used) {
1380 char buf[4];
1381 if (i_io_read(ig, buf, 4) != 4) {
1382 i_push_error(0, "skipping colors");
1383 return 0;
1384 }
1385 base_offset += 4;
1386 }
1387 }
1388 else if (compression == BI_BITFIELDS) {
1389 int pos;
1390 unsigned bits;
1391 compression_name = "BI_BITFIELDS";
1392
1393 for (i = 0; i < 3; ++i) {
1394 i_packed_t rmask;
1395 if (!read_packed(ig, "V", &rmask)) {
1396 i_push_error(0, "reading pixel masks");
1397 return 0;
1398 }
1399 if (rmask == 0) {
1400 im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i);
1401 return NULL;
1402 }
1403 masks.masks[i] = rmask;
1404 /* work out a shift for the mask */
1405 pos = 0;
1406 bits = masks.masks[i];
1407 while (!(bits & 1)) {
1408 ++pos;
1409 bits >>= 1;
1410 }
1411 masks.shifts[i] = pos;
1412 pos = 0;
1413 while (bits & 1) {
1414 ++pos;
1415 bits >>= 1;
1416 }
1417 masks.bits[i] = pos;
1418 /*fprintf(stderr, "%d: mask %08x shift %d bits %d\n", i, masks.masks[i], masks.shifts[i], masks.bits[i]);*/
1419 }
1420 /* account for the masks */
1421 base_offset += 3 * 4;
1422 }
1423 else {
1424 im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression);
1425 return NULL;
1426 }
1427
1428 if (offbits < base_offset) {
1429 im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits);
1430 return NULL;
1431 }
1432
1433 if (offbits > base_offset) {
1434 /* this will be slow if the offset is large, but that should be
1435 rare */
1436 char buffer;
1437 while (base_offset < offbits) {
1438 if (i_io_read(ig, &buffer, 1) != 1) {
1439 i_push_error(0, "failed skipping to image data offset");
1440 return NULL;
1441 }
1442 ++base_offset;
1443 }
1444 }
1445
1446 im = i_img_empty(NULL, xsize, ysize);
1447 if (!im)
1448 return NULL;
1449
1450 i_tags_add(&im->tags, "bmp_compression_name", 0, compression_name, -1, 0);
1451
1452 /* I wasn't able to make this overflow in testing, but better to be
1453 safe */
1454 bytes = sizeof(i_color) * xsize;
1455 if (bytes / sizeof(i_color) != (size_t)xsize) {
1456 i_img_destroy(im);
1457 i_push_error(0, "integer overflow calculating buffer size");
1458 return NULL;
1459 }
1460 line = mymalloc(bytes); /* checked 29jun05 tonyc */
1461 while (y != lasty) {
1462 p = line;
1463 for (x = 0; x < xsize; ++x) {
1464 i_packed_t pixel;
1465 if (!read_packed(ig, unpack_code, &pixel)) {
1466 myfree(line);
1467 if (allow_incomplete) {
1468 i_tags_setn(&im->tags, "i_incomplete", 1);
1469 i_tags_setn(&im->tags, "i_lines_read", abs(starty - y));
1470 return im;
1471 }
1472 else {
1473 i_push_error(0, "failed reading image data");
1474 i_img_destroy(im);
1475 return NULL;
1476 }
1477 }
1478 for (i = 0; i < 3; ++i) {
1479 int sample = (pixel & masks.masks[i]) >> masks.shifts[i];
1480 int bits = masks.bits[i];
1481 if (bits < 8) {
1482 sample = (sample * samp_converts[bits-1].mult) >> samp_converts[bits-1].shift;
1483 }
1484 else if (bits) {
1485 sample >>= bits - 8;
1486 }
1487 p->channel[i] = sample;
1488 }
1489 ++p;
1490 }
1491 i_plin(im, 0, xsize, y, line);
1492 if (extras)
1493 i_io_read(ig, junk, extras);
1494 y += yinc;
1495 }
1496 myfree(line);
1497
1498 return im;
1499 }
1500
1501 /*
1502 =head1 SEE ALSO
1503
1504 Imager(3)
1505
1506 =head1 AUTHOR
1507
1508 Tony Cook <[email protected]>
1509
1510 =head1 RESTRICTIONS
1511
1512 Cannot save as compressed BMP.
1513
1514 =head1 BUGS
1515
1516 Doesn't handle OS/2 bitmaps.
1517
1518 16-bit/pixel images haven't been tested. (I need an image).
1519
1520 BI_BITFIELDS compression hasn't been tested (I need an image).
1521
1522 The header handling for paletted images needs to be refactored
1523
1524 =cut
1525 */