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