| 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 |
25 |
| i_writebmp_wiol(i_img *im, io_glue *ig) { |
| 80 |
25 |
| dIMCTXim(im); |
| 81 |
25 |
| i_clear_error(); |
| 82 |
|
| |
| 83 |
|
| /* pick a format */ |
| 84 |
25 |
▶ | if (im->type == i_direct_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (im->type == i_direct_type) { |
13 |
12 |
25 |
|
| 85 |
13 |
| return write_24bit_data(ig, im); |
| 86 |
|
| } |
| 87 |
|
| else { |
| 88 |
|
| int pal_size; |
| 89 |
|
| |
| 90 |
|
| /* must be paletted */ |
| 91 |
12 |
▶ | pal_size = i_colorcount(im); |
Branch: 1 branch
| Branch | True | False | Total |
| pal_size = i_colorcount(im); |
12 |
0 |
12 |
|
| 92 |
12 |
▶ | if (pal_size <= 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pal_size <= 2) { |
4 |
8 |
12 |
|
| 93 |
4 |
| return write_1bit_data(ig, im); |
| 94 |
|
| } |
| 95 |
8 |
▶ | else if (pal_size <= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (pal_size <= 16) { |
4 |
4 |
8 |
|
| 96 |
4 |
| return write_4bit_data(ig, im); |
| 97 |
|
| } |
| 98 |
|
| else { |
| 99 |
4 |
| 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 |
104 |
| 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 |
104 |
| dIMCTXio(ig); |
| 122 |
|
| |
| 123 |
104 |
| im_log((aIMCTX, 1, "i_readbmp_wiol(ig %p)\n", ig)); |
| 124 |
|
| |
| 125 |
104 |
| i_clear_error(); |
| 126 |
|
| |
| 127 |
104 |
▶ | if (!read_packed(ig, "CCVvvVVV!V!vvVVVVVV", &b_magic, &m_magic, &filesize, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_packed(ig, "CCVvvVVV!V!vvVVVVVV", &b_magic, &m_magic, &filesize, |
1 |
103 |
104 |
|
| 128 |
|
| &res1, &res2, &offbits, &infohead_size, |
| 129 |
|
| &xsize, &ysize, &planes, |
| 130 |
|
| &bit_count, &compression, &size_image, &xres, &yres, |
| 131 |
|
| &clr_used, &clr_important)) { |
| 132 |
1 |
| i_push_error(0, "file too short to be a BMP file"); |
| 133 |
1 |
| return 0; |
| 134 |
|
| } |
| 135 |
103 |
▶ | if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE |
Branch: 3 branches
| Branch | True | False | Total |
| if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE |
103 |
0 |
103 |
| if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE |
103 |
0 |
103 |
| if (b_magic != 'B' || m_magic != 'M' || infohead_size != INFOHEAD_SIZE |
103 |
0 |
103 |
|
| 136 |
103 |
▶ | || planes != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| || planes != 1) { |
1 |
102 |
103 |
|
| 137 |
1 |
| i_push_error(0, "not a BMP file"); |
| 138 |
1 |
| return 0; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
102 |
| 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 |
102 |
▶ | if (!i_int_check_image_file_limits(xsize, abs((int)ysize), 3, sizeof(i_sample_t))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_int_check_image_file_limits(xsize, abs((int)ysize), 3, sizeof(i_sample_t))) { |
8 |
94 |
102 |
|
| 149 |
8 |
| im_log((aIMCTX, 1, "i_readbmp_wiol: image size exceeds limits\n")); |
| 150 |
8 |
| return NULL; |
| 151 |
|
| } |
| 152 |
|
| |
| 153 |
94 |
| switch (bit_count) { |
| 154 |
16 |
| case 1: |
| 155 |
16 |
| im = read_1bit_bmp(ig, xsize, ysize, clr_used, compression, offbits, |
| 156 |
|
| allow_incomplete); |
| 157 |
16 |
| break; |
| 158 |
|
| |
| 159 |
26 |
| case 4: |
| 160 |
26 |
| im = read_4bit_bmp(ig, xsize, ysize, clr_used, compression, offbits, |
| 161 |
|
| allow_incomplete); |
| 162 |
26 |
| break; |
| 163 |
|
| |
| 164 |
25 |
| case 8: |
| 165 |
25 |
| im = read_8bit_bmp(ig, xsize, ysize, clr_used, compression, offbits, |
| 166 |
|
| allow_incomplete); |
| 167 |
25 |
| break; |
| 168 |
|
| |
| 169 |
26 |
| case 32: |
| 170 |
|
| case 24: |
| 171 |
|
| case 16: |
| 172 |
26 |
| im = read_direct_bmp(ig, xsize, ysize, bit_count, clr_used, compression, |
| 173 |
|
| offbits, allow_incomplete); |
| 174 |
26 |
| break; |
| 175 |
|
| |
| 176 |
1 |
| default: |
| 177 |
1 |
| im_push_errorf(aIMCTX, 0, "unknown bit count for BMP file (%d)", (int)bit_count); |
| 178 |
1 |
| return NULL; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
93 |
▶ | if (im) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (im) { |
58 |
35 |
93 |
|
| 182 |
|
| /* store the resolution */ |
| 183 |
58 |
▶ | if (xres && !yres) |
Branch: 2 branches
| Branch | True | False | Total |
| if (xres && !yres) |
58 |
0 |
58 |
| if (xres && !yres) |
0 |
58 |
58 |
|
| 184 |
0 |
| yres = xres; |
| 185 |
58 |
▶ | else if (yres && !xres) |
Branch: 2 branches
| Branch | True | False | Total |
| else if (yres && !xres) |
58 |
0 |
58 |
| else if (yres && !xres) |
0 |
58 |
58 |
|
| 186 |
0 |
| xres = yres; |
| 187 |
58 |
▶ | if (xres) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (xres) { |
58 |
0 |
58 |
|
| 188 |
58 |
| i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 4); |
| 189 |
58 |
| i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 4); |
| 190 |
|
| } |
| 191 |
58 |
| i_tags_addn(&im->tags, "bmp_compression", 0, compression); |
| 192 |
58 |
| i_tags_addn(&im->tags, "bmp_important_colors", 0, clr_important); |
| 193 |
58 |
| i_tags_addn(&im->tags, "bmp_used_colors", 0, clr_used); |
| 194 |
58 |
| i_tags_addn(&im->tags, "bmp_filesize", 0, filesize); |
| 195 |
58 |
| i_tags_addn(&im->tags, "bmp_bit_count", 0, bit_count); |
| 196 |
58 |
| i_tags_add(&im->tags, "i_format", 0, "bmp", 3, 0); |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
93 |
| 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 |
125199 |
| 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 |
125199 |
| va_start(ap, format); |
| 231 |
|
| |
| 232 |
270697 |
▶ | while (*format) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*format) { |
145505 |
125192 |
270697 |
|
| 233 |
145505 |
| p = va_arg(ap, i_packed_t *); |
| 234 |
|
| |
| 235 |
145505 |
| code = *format++; |
| 236 |
145505 |
| shrieking = *format == '!'; |
| 237 |
145505 |
▶ | if (shrieking) ++format; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shrieking) ++format; |
207 |
145298 |
145505 |
|
| 238 |
|
| |
| 239 |
145505 |
| switch (code) { |
| 240 |
414 |
| case 'v': |
| 241 |
414 |
▶ | if (i_io_read(ig, buf, 2) != 2) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 2) != 2) |
0 |
414 |
414 |
|
| 242 |
0 |
| goto fail; |
| 243 |
414 |
| work = buf[0] + ((i_packed_t)buf[1] << 8); |
| 244 |
414 |
▶ | if (shrieking) |
Branch: 1 branch
| Branch | True | False | Total |
| if (shrieking) |
0 |
414 |
414 |
|
| 245 |
0 |
| *p = (work ^ SIGNBIT16) - SIGNBIT16; |
| 246 |
|
| else |
| 247 |
414 |
| *p = work; |
| 248 |
414 |
| break; |
| 249 |
|
| |
| 250 |
1137 |
| case 'V': |
| 251 |
1137 |
▶ | if (i_io_read(ig, buf, 4) != 4) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 4) != 4) |
1 |
1136 |
1137 |
|
| 252 |
1 |
| goto fail; |
| 253 |
1136 |
| work = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16) + ((i_packed_t)buf[3] << 24); |
| 254 |
1136 |
▶ | if (shrieking) |
Branch: 1 branch
| Branch | True | False | Total |
| if (shrieking) |
206 |
930 |
1136 |
|
| 255 |
206 |
| *p = (work ^ SIGNBIT32) - SIGNBIT32; |
| 256 |
|
| else |
| 257 |
930 |
| *p = work; |
| 258 |
1136 |
| break; |
| 259 |
|
| |
| 260 |
25077 |
| case 'C': |
| 261 |
25077 |
▶ | if (i_io_read(ig, buf, 1) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 1) != 1) |
3 |
25074 |
25077 |
|
| 262 |
3 |
| goto fail; |
| 263 |
25074 |
| *p = buf[0]; |
| 264 |
25074 |
| break; |
| 265 |
|
| |
| 266 |
0 |
| case 'c': |
| 267 |
0 |
▶ | if (i_io_read(ig, buf, 1) != 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 1) != 1) |
0 |
0 |
0 |
|
| 268 |
0 |
| goto fail; |
| 269 |
0 |
| *p = (char)buf[0]; |
| 270 |
0 |
| break; |
| 271 |
|
| |
| 272 |
118877 |
| case '3': /* extension - 24-bit number */ |
| 273 |
118877 |
▶ | if (i_io_read(ig, buf, 3) != 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 3) != 3) |
3 |
118874 |
118877 |
|
| 274 |
3 |
| goto fail; |
| 275 |
118874 |
| *p = buf[0] + (buf[1] << 8) + ((i_packed_t)buf[2] << 16); |
| 276 |
118874 |
| break; |
| 277 |
|
| |
| 278 |
0 |
| default: |
| 279 |
|
| { |
| 280 |
0 |
| dIMCTXio(ig); |
| 281 |
0 |
| im_fatal(aIMCTX, 1, "Unknown read_packed format code 0x%02x", code); |
| 282 |
|
| } |
| 283 |
|
| } |
| 284 |
|
| } |
| 285 |
125192 |
| va_end(ap); |
| 286 |
125192 |
| return 1; |
| 287 |
|
| |
| 288 |
7 |
| fail: |
| 289 |
7 |
| va_end(ap); |
| 290 |
7 |
| 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 |
532 |
| write_packed(io_glue *ig, char *format, ...) { |
| 305 |
|
| unsigned char buf[4]; |
| 306 |
|
| va_list ap; |
| 307 |
|
| int i; |
| 308 |
|
| |
| 309 |
532 |
| va_start(ap, format); |
| 310 |
|
| |
| 311 |
2915 |
▶ | while (*format) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (*format) { |
2390 |
525 |
2915 |
|
| 312 |
2390 |
| i = va_arg(ap, i_upacked_t); |
| 313 |
|
| |
| 314 |
2390 |
| switch (*format) { |
| 315 |
84 |
| case 'v': |
| 316 |
84 |
| buf[0] = i & 255; |
| 317 |
84 |
| buf[1] = i / 256; |
| 318 |
84 |
▶ | if (i_io_write(ig, buf, 2) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, buf, 2) == -1) |
0 |
84 |
84 |
|
| 319 |
0 |
| goto fail; |
| 320 |
84 |
| break; |
| 321 |
|
| |
| 322 |
231 |
| case 'V': |
| 323 |
231 |
| buf[0] = i & 0xFF; |
| 324 |
231 |
| buf[1] = (i >> 8) & 0xFF; |
| 325 |
231 |
| buf[2] = (i >> 16) & 0xFF; |
| 326 |
231 |
| buf[3] = (i >> 24) & 0xFF; |
| 327 |
231 |
▶ | if (i_io_write(ig, buf, 4) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, buf, 4) == -1) |
0 |
231 |
231 |
|
| 328 |
0 |
| goto fail; |
| 329 |
231 |
| break; |
| 330 |
|
| |
| 331 |
2075 |
| case 'C': |
| 332 |
|
| case 'c': |
| 333 |
2075 |
| buf[0] = i & 0xFF; |
| 334 |
2075 |
▶ | if (i_io_write(ig, buf, 1) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, buf, 1) == -1) |
7 |
2068 |
2075 |
|
| 335 |
7 |
| goto fail; |
| 336 |
2068 |
| break; |
| 337 |
|
| |
| 338 |
0 |
| default: |
| 339 |
|
| { |
| 340 |
0 |
| dIMCTXio(ig); |
| 341 |
0 |
| im_fatal(aIMCTX, 1, "Unknown write_packed format code 0x%02x", *format); |
| 342 |
|
| } |
| 343 |
|
| } |
| 344 |
2383 |
| ++format; |
| 345 |
|
| } |
| 346 |
525 |
| va_end(ap); |
| 347 |
|
| |
| 348 |
525 |
| return 1; |
| 349 |
|
| |
| 350 |
7 |
| fail: |
| 351 |
7 |
| va_end(ap); |
| 352 |
7 |
| 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 |
25 |
| 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 |
25 |
| int colors_used = 0; |
| 370 |
25 |
| int offset = FILEHEAD_SIZE + INFOHEAD_SIZE; |
| 371 |
25 |
| dIMCTXim(im); |
| 372 |
|
| |
| 373 |
25 |
▶ | if ((i_img_dim_u)im->xsize > SIGNMAX32 |
Branch: 1 branch
| Branch | True | False | Total |
| if ((i_img_dim_u)im->xsize > SIGNMAX32 |
25 |
0 |
25 |
|
| 374 |
25 |
▶ | || (i_img_dim_u)im->ysize > SIGNMAX32) { |
Branch: 1 branch
| Branch | True | False | Total |
| || (i_img_dim_u)im->ysize > SIGNMAX32) { |
0 |
25 |
25 |
|
| 375 |
0 |
| i_push_error(0, "image too large to write to BMP"); |
| 376 |
0 |
| return 0; |
| 377 |
|
| } |
| 378 |
|
| |
| 379 |
25 |
| got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres); |
| 380 |
25 |
| got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres); |
| 381 |
25 |
▶ | if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_tags_get_int(&im->tags, "i_aspect_only", 0,&aspect_only)) |
25 |
0 |
25 |
|
| 382 |
25 |
| aspect_only = 0; |
| 383 |
25 |
▶ | if (!got_xres) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!got_xres) { |
12 |
13 |
25 |
|
| 384 |
12 |
▶ | if (!got_yres) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!got_yres) |
12 |
0 |
12 |
|
| 385 |
12 |
| xres = yres = 72; |
| 386 |
|
| else |
| 387 |
0 |
| xres = yres; |
| 388 |
|
| } |
| 389 |
|
| else { |
| 390 |
13 |
▶ | if (!got_yres) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!got_yres) |
0 |
13 |
13 |
|
| 391 |
0 |
| yres = xres; |
| 392 |
|
| } |
| 393 |
25 |
▶ | if (xres <= 0 || yres <= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (xres <= 0 || yres <= 0) |
25 |
0 |
25 |
| if (xres <= 0 || yres <= 0) |
0 |
25 |
25 |
|
| 394 |
0 |
| xres = yres = 72; |
| 395 |
25 |
▶ | if (aspect_only) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (aspect_only) { |
0 |
25 |
25 |
|
| 396 |
|
| /* scale so the smaller value is 72 */ |
| 397 |
|
| double ratio; |
| 398 |
0 |
▶ | if (xres < yres) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (xres < yres) { |
0 |
0 |
0 |
|
| 399 |
0 |
| ratio = 72.0 / xres; |
| 400 |
|
| } |
| 401 |
|
| else { |
| 402 |
0 |
| ratio = 72.0 / yres; |
| 403 |
|
| } |
| 404 |
0 |
| xres *= ratio; |
| 405 |
0 |
| yres *= ratio; |
| 406 |
|
| } |
| 407 |
|
| /* now to pels/meter */ |
| 408 |
25 |
| xres *= 100.0/2.54; |
| 409 |
25 |
| yres *= 100.0/2.54; |
| 410 |
|
| |
| 411 |
25 |
▶ | if (im->type == i_palette_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (im->type == i_palette_type) { |
12 |
13 |
25 |
|
| 412 |
12 |
▶ | colors_used = i_colorcount(im); |
Branch: 1 branch
| Branch | True | False | Total |
| colors_used = i_colorcount(im); |
12 |
0 |
12 |
|
| 413 |
12 |
| offset += 4 * colors_used; |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
25 |
▶ | if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_packed(ig, "CCVvvVVVVvvVVVVVV", 'B', 'M', |
4 |
21 |
25 |
|
| 417 |
25 |
| (i_upacked_t)(data_size+offset), |
| 418 |
|
| (i_upacked_t)0, (i_upacked_t)0, (i_upacked_t)offset, |
| 419 |
25 |
| (i_upacked_t)INFOHEAD_SIZE, (i_upacked_t)im->xsize, |
| 420 |
25 |
| (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 |
25 |
| (i_upacked_t)(xres+0.5), (i_upacked_t)(yres+0.5), |
| 424 |
|
| (i_upacked_t)colors_used, (i_upacked_t)colors_used)){ |
| 425 |
4 |
| i_push_error(0, "cannot write bmp header"); |
| 426 |
4 |
| return 0; |
| 427 |
|
| } |
| 428 |
21 |
▶ | if (im->type == i_palette_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (im->type == i_palette_type) { |
9 |
12 |
21 |
|
| 429 |
|
| int i; |
| 430 |
|
| i_color c; |
| 431 |
|
| |
| 432 |
513 |
▶ | for (i = 0; i < colors_used; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < colors_used; ++i) { |
507 |
6 |
513 |
|
| 433 |
507 |
▶ | i_getcolors(im, i, &c, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| i_getcolors(im, i, &c, 1); |
507 |
0 |
507 |
|
| 434 |
507 |
▶ | if (im->channels >= 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (im->channels >= 3) { |
507 |
0 |
507 |
|
| 435 |
507 |
▶ | if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]), |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_packed(ig, "CCCC", (i_upacked_t)(c.channel[2]), |
3 |
504 |
507 |
|
| 436 |
507 |
| (i_upacked_t)(c.channel[1]), |
| 437 |
507 |
| (i_upacked_t)(c.channel[0]), (i_upacked_t)0)) { |
| 438 |
3 |
| i_push_error(0, "cannot write palette entry"); |
| 439 |
3 |
| return 0; |
| 440 |
|
| } |
| 441 |
|
| } |
| 442 |
|
| else { |
| 443 |
0 |
| i_upacked_t v = c.channel[0]; |
| 444 |
0 |
▶ | if (!write_packed(ig, "CCCC", v, v, v, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_packed(ig, "CCCC", v, v, v, 0)) { |
0 |
0 |
0 |
|
| 445 |
0 |
| i_push_error(0, "cannot write palette entry"); |
| 446 |
0 |
| return 0; |
| 447 |
|
| } |
| 448 |
|
| } |
| 449 |
|
| } |
| 450 |
|
| } |
| 451 |
|
| |
| 452 |
18 |
| 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 |
4 |
| 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 |
4 |
| int line_size = (im->xsize+7) / 8; |
| 472 |
|
| int x, y; |
| 473 |
|
| int unpacked_size; |
| 474 |
4 |
| dIMCTXim(im); |
| 475 |
|
| |
| 476 |
|
| /* round up to nearest multiple of four */ |
| 477 |
4 |
| line_size = (line_size + 3) / 4 * 4; |
| 478 |
|
| |
| 479 |
4 |
▶ | if (!write_bmphead(ig, im, 1, line_size * im->ysize)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_bmphead(ig, im, 1, line_size * im->ysize)) |
2 |
2 |
4 |
|
| 480 |
2 |
| return 0; |
| 481 |
|
| |
| 482 |
|
| /* this shouldn't be an issue, but let's be careful */ |
| 483 |
2 |
| unpacked_size = im->xsize + 8; |
| 484 |
2 |
▶ | if (unpacked_size < im->xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unpacked_size < im->xsize) { |
0 |
2 |
2 |
|
| 485 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 486 |
0 |
| return 0; |
| 487 |
|
| } |
| 488 |
2 |
| line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */ |
| 489 |
2 |
| memset(line + im->xsize, 0, 8); |
| 490 |
|
| |
| 491 |
|
| /* size allocated here is always much smaller than xsize, hence |
| 492 |
|
| can't overflow int */ |
| 493 |
2 |
| packed = mymalloc(line_size); /* checked 29jun05 tonyc */ |
| 494 |
2 |
| memset(packed, 0, line_size); |
| 495 |
|
| |
| 496 |
152 |
▶ | for (y = im->ysize-1; y >= 0; --y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = im->ysize-1; y >= 0; --y) { |
151 |
1 |
152 |
|
| 497 |
151 |
▶ | i_gpal(im, 0, im->xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(im, 0, im->xsize, y, line); |
151 |
0 |
151 |
|
| 498 |
151 |
| mask = 0x80; |
| 499 |
151 |
| byte = 0; |
| 500 |
151 |
| out = packed; |
| 501 |
22671 |
▶ | for (x = 0; x < im->xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < im->xsize; ++x) { |
22520 |
151 |
22671 |
|
| 502 |
22520 |
▶ | if (line[x]) |
Branch: 1 branch
| Branch | True | False | Total |
| if (line[x]) |
6962 |
15558 |
22520 |
|
| 503 |
6962 |
| byte |= mask; |
| 504 |
22520 |
▶ | if ((mask >>= 1) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((mask >>= 1) == 0) { |
2702 |
19818 |
22520 |
|
| 505 |
2702 |
| *out++ = byte; |
| 506 |
2702 |
| byte = 0; |
| 507 |
2702 |
| mask = 0x80; |
| 508 |
|
| } |
| 509 |
|
| } |
| 510 |
151 |
▶ | if (mask != 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (mask != 0x80) { |
151 |
0 |
151 |
|
| 511 |
151 |
| *out++ = byte; |
| 512 |
|
| } |
| 513 |
151 |
▶ | if (i_io_write(ig, packed, line_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, packed, line_size) < 0) { |
1 |
150 |
151 |
|
| 514 |
1 |
| myfree(packed); |
| 515 |
1 |
| myfree(line); |
| 516 |
1 |
| i_push_error(0, "writing 1 bit/pixel packed data"); |
| 517 |
1 |
| return 0; |
| 518 |
|
| } |
| 519 |
|
| } |
| 520 |
1 |
| myfree(packed); |
| 521 |
1 |
| myfree(line); |
| 522 |
|
| |
| 523 |
1 |
▶ | if (i_io_close(ig)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_close(ig)) |
0 |
1 |
1 |
|
| 524 |
0 |
| return 0; |
| 525 |
|
| |
| 526 |
1 |
| 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 |
4 |
| write_4bit_data(io_glue *ig, i_img *im) { |
| 540 |
|
| i_palidx *line; |
| 541 |
|
| unsigned char *packed; |
| 542 |
|
| unsigned char *out; |
| 543 |
4 |
| int line_size = (im->xsize+1) / 2; |
| 544 |
|
| int x, y; |
| 545 |
|
| int unpacked_size; |
| 546 |
4 |
| dIMCTXim(im); |
| 547 |
|
| |
| 548 |
|
| /* round up to nearest multiple of four */ |
| 549 |
4 |
| line_size = (line_size + 3) / 4 * 4; |
| 550 |
|
| |
| 551 |
4 |
▶ | if (!write_bmphead(ig, im, 4, line_size * im->ysize)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_bmphead(ig, im, 4, line_size * im->ysize)) |
2 |
2 |
4 |
|
| 552 |
2 |
| return 0; |
| 553 |
|
| |
| 554 |
|
| /* this shouldn't be an issue, but let's be careful */ |
| 555 |
2 |
| unpacked_size = im->xsize + 2; |
| 556 |
2 |
▶ | if (unpacked_size < im->xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unpacked_size < im->xsize) { |
0 |
2 |
2 |
|
| 557 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 558 |
0 |
| return 0; |
| 559 |
|
| } |
| 560 |
2 |
| line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */ |
| 561 |
2 |
| memset(line + im->xsize, 0, 2); |
| 562 |
|
| |
| 563 |
|
| /* size allocated here is always much smaller than xsize, hence |
| 564 |
|
| can't overflow int */ |
| 565 |
2 |
| packed = mymalloc(line_size); /* checked 29jun05 tonyc */ |
| 566 |
2 |
| memset(packed, 0, line_size); |
| 567 |
|
| |
| 568 |
152 |
▶ | for (y = im->ysize-1; y >= 0; --y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = im->ysize-1; y >= 0; --y) { |
151 |
1 |
152 |
|
| 569 |
151 |
▶ | i_gpal(im, 0, im->xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(im, 0, im->xsize, y, line); |
151 |
0 |
151 |
|
| 570 |
151 |
| out = packed; |
| 571 |
11411 |
▶ | for (x = 0; x < im->xsize; x += 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < im->xsize; x += 2) { |
11260 |
151 |
11411 |
|
| 572 |
11260 |
| *out++ = (line[x] << 4) + line[x+1]; |
| 573 |
|
| } |
| 574 |
151 |
▶ | if (i_io_write(ig, packed, line_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, packed, line_size) < 0) { |
1 |
150 |
151 |
|
| 575 |
1 |
| myfree(packed); |
| 576 |
1 |
| myfree(line); |
| 577 |
1 |
| i_push_error(0, "writing 4 bit/pixel packed data"); |
| 578 |
1 |
| return 0; |
| 579 |
|
| } |
| 580 |
|
| } |
| 581 |
1 |
| myfree(packed); |
| 582 |
1 |
| myfree(line); |
| 583 |
|
| |
| 584 |
1 |
▶ | if (i_io_close(ig)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_close(ig)) |
0 |
1 |
1 |
|
| 585 |
0 |
| return 0; |
| 586 |
|
| |
| 587 |
1 |
| 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 |
4 |
| write_8bit_data(io_glue *ig, i_img *im) { |
| 601 |
|
| i_palidx *line; |
| 602 |
4 |
| int line_size = im->xsize; |
| 603 |
|
| int y; |
| 604 |
|
| int unpacked_size; |
| 605 |
4 |
| dIMCTXim(im); |
| 606 |
|
| |
| 607 |
|
| /* round up to nearest multiple of four */ |
| 608 |
4 |
| line_size = (line_size + 3) / 4 * 4; |
| 609 |
|
| |
| 610 |
4 |
▶ | if (!write_bmphead(ig, im, 8, line_size * im->ysize)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_bmphead(ig, im, 8, line_size * im->ysize)) |
2 |
2 |
4 |
|
| 611 |
2 |
| return 0; |
| 612 |
|
| |
| 613 |
|
| /* this shouldn't be an issue, but let's be careful */ |
| 614 |
2 |
| unpacked_size = im->xsize + 4; |
| 615 |
2 |
▶ | if (unpacked_size < im->xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (unpacked_size < im->xsize) { |
0 |
2 |
2 |
|
| 616 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 617 |
0 |
| return 0; |
| 618 |
|
| } |
| 619 |
2 |
| line = mymalloc(unpacked_size); /* checked 29jun05 tonyc */ |
| 620 |
2 |
| memset(line + im->xsize, 0, 4); |
| 621 |
|
| |
| 622 |
152 |
▶ | for (y = im->ysize-1; y >= 0; --y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = im->ysize-1; y >= 0; --y) { |
151 |
1 |
152 |
|
| 623 |
151 |
▶ | i_gpal(im, 0, im->xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(im, 0, im->xsize, y, line); |
151 |
0 |
151 |
|
| 624 |
151 |
▶ | if (i_io_write(ig, line, line_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, line, line_size) < 0) { |
1 |
150 |
151 |
|
| 625 |
1 |
| myfree(line); |
| 626 |
1 |
| i_push_error(0, "writing 8 bit/pixel packed data"); |
| 627 |
1 |
| return 0; |
| 628 |
|
| } |
| 629 |
|
| } |
| 630 |
1 |
| myfree(line); |
| 631 |
|
| |
| 632 |
1 |
▶ | if (i_io_close(ig)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_close(ig)) |
0 |
1 |
1 |
|
| 633 |
0 |
| return 0; |
| 634 |
|
| |
| 635 |
1 |
| 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 |
13 |
| write_24bit_data(io_glue *ig, i_img *im) { |
| 649 |
|
| unsigned char *samples; |
| 650 |
|
| int y; |
| 651 |
13 |
| int line_size = 3 * im->xsize; |
| 652 |
|
| i_color bg; |
| 653 |
13 |
| dIMCTXim(im); |
| 654 |
|
| |
| 655 |
13 |
| i_get_file_background(im, &bg); |
| 656 |
|
| |
| 657 |
|
| /* just in case we implement a direct format with 2bytes/pixel |
| 658 |
|
| (unlikely though) */ |
| 659 |
13 |
▶ | if (line_size / 3 != im->xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (line_size / 3 != im->xsize) { |
0 |
13 |
13 |
|
| 660 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 661 |
0 |
| return 0; |
| 662 |
|
| } |
| 663 |
|
| |
| 664 |
13 |
| line_size = (line_size + 3) / 4 * 4; |
| 665 |
|
| |
| 666 |
13 |
▶ | if (!write_bmphead(ig, im, 24, line_size * im->ysize)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!write_bmphead(ig, im, 24, line_size * im->ysize)) |
1 |
12 |
13 |
|
| 667 |
1 |
| return 0; |
| 668 |
12 |
| samples = mymalloc(4 * im->xsize); |
| 669 |
12 |
| memset(samples, 0, line_size); |
| 670 |
1544 |
▶ | for (y = im->ysize-1; y >= 0; --y) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (y = im->ysize-1; y >= 0; --y) { |
1533 |
11 |
1544 |
|
| 671 |
1533 |
| unsigned char *samplep = samples; |
| 672 |
|
| int x; |
| 673 |
1533 |
| i_gsamp_bg(im, 0, im->xsize, y, samples, 3, &bg); |
| 674 |
239665 |
▶ | for (x = 0; x < im->xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < im->xsize; ++x) { |
238132 |
1533 |
239665 |
|
| 675 |
238132 |
| unsigned char tmp = samplep[2]; |
| 676 |
238132 |
| samplep[2] = samplep[0]; |
| 677 |
238132 |
| samplep[0] = tmp; |
| 678 |
238132 |
| samplep += 3; |
| 679 |
|
| } |
| 680 |
1533 |
▶ | if (i_io_write(ig, samples, line_size) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, samples, line_size) < 0) { |
1 |
1532 |
1533 |
|
| 681 |
1 |
| i_push_error(0, "writing image data"); |
| 682 |
1 |
| myfree(samples); |
| 683 |
1 |
| return 0; |
| 684 |
|
| } |
| 685 |
|
| } |
| 686 |
11 |
| myfree(samples); |
| 687 |
|
| |
| 688 |
11 |
▶ | if (i_io_close(ig)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_close(ig)) |
1 |
10 |
11 |
|
| 689 |
1 |
| return 0; |
| 690 |
|
| |
| 691 |
10 |
| 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 |
59 |
| 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 |
59 |
| dIMCTXio(ig); |
| 709 |
|
| |
| 710 |
6274 |
▶ | for (i = 0; i < count; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; ++i) { |
6218 |
56 |
6274 |
|
| 711 |
6218 |
▶ | if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_packed(ig, "CCCC", &b, &g, &r, &x)) { |
3 |
6215 |
6218 |
|
| 712 |
3 |
| i_push_error(0, "reading BMP palette"); |
| 713 |
3 |
| return 0; |
| 714 |
|
| } |
| 715 |
6215 |
| c.channel[0] = r; |
| 716 |
6215 |
| c.channel[1] = g; |
| 717 |
6215 |
| c.channel[2] = b; |
| 718 |
6215 |
▶ | if (i_addcolors(im, &c, 1) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i_addcolors(im, &c, 1) < 0) { |
6215 |
0 |
6215 |
| if (i_addcolors(im, &c, 1) < 0) { |
0 |
6215 |
6215 |
|
| 719 |
0 |
| i_push_error(0, "out of space in image palette"); |
| 720 |
0 |
| return 0; |
| 721 |
|
| } |
| 722 |
|
| } |
| 723 |
|
| |
| 724 |
56 |
| 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 |
16 |
| 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 |
16 |
| int line_size = (xsize + 7)/8; |
| 744 |
|
| int bit; |
| 745 |
|
| unsigned char *in; |
| 746 |
|
| long base_offset; |
| 747 |
16 |
| dIMCTXio(ig); |
| 748 |
|
| |
| 749 |
16 |
▶ | if (compression != BI_RGB) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compression != BI_RGB) { |
1 |
15 |
16 |
|
| 750 |
1 |
| im_push_errorf(aIMCTX, 0, "unknown 1-bit BMP compression (%d)", compression); |
| 751 |
1 |
| return NULL; |
| 752 |
|
| } |
| 753 |
|
| |
| 754 |
15 |
▶ | if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((i_img_dim)((i_img_dim_u)xsize + 8) < xsize) { /* if there was overflow */ |
0 |
15 |
15 |
|
| 755 |
|
| /* we check with 8 because we allocate that much for the decoded |
| 756 |
|
| line buffer */ |
| 757 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 758 |
0 |
| 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 |
15 |
| line_size = (line_size+3) / 4 * 4; |
| 764 |
|
| |
| 765 |
15 |
▶ | if (ysize > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ysize > 0) { |
14 |
1 |
15 |
|
| 766 |
14 |
| start_y = ysize-1; |
| 767 |
14 |
| lasty = -1; |
| 768 |
14 |
| yinc = -1; |
| 769 |
|
| } |
| 770 |
|
| else { |
| 771 |
|
| /* when ysize is -ve it's a top-down image */ |
| 772 |
1 |
| ysize = -ysize; |
| 773 |
1 |
| start_y = 0; |
| 774 |
1 |
| lasty = ysize; |
| 775 |
1 |
| yinc = 1; |
| 776 |
|
| } |
| 777 |
15 |
| y = start_y; |
| 778 |
15 |
▶ | if (!clr_used) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!clr_used) |
0 |
15 |
15 |
|
| 779 |
0 |
| clr_used = 2; |
| 780 |
15 |
▶ | if (clr_used < 0 || clr_used > 2) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (clr_used < 0 || clr_used > 2) { |
15 |
0 |
15 |
| if (clr_used < 0 || clr_used > 2) { |
1 |
14 |
15 |
|
| 781 |
1 |
| im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used); |
| 782 |
1 |
| return NULL; |
| 783 |
|
| } |
| 784 |
|
| |
| 785 |
14 |
| base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4; |
| 786 |
14 |
▶ | if (offbits < base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits < base_offset) { |
1 |
13 |
14 |
|
| 787 |
1 |
| im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits); |
| 788 |
1 |
| return NULL; |
| 789 |
|
| } |
| 790 |
|
| |
| 791 |
13 |
| im = i_img_pal_new(xsize, ysize, 3, 256); |
| 792 |
13 |
▶ | if (!im) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!im) |
0 |
13 |
13 |
|
| 793 |
0 |
| return NULL; |
| 794 |
13 |
▶ | if (!read_bmp_pal(ig, im, clr_used)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_bmp_pal(ig, im, clr_used)) { |
1 |
12 |
13 |
|
| 795 |
1 |
| i_img_destroy(im); |
| 796 |
1 |
| return NULL; |
| 797 |
|
| } |
| 798 |
|
| |
| 799 |
12 |
▶ | if (offbits > base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits > base_offset) { |
2 |
10 |
12 |
|
| 800 |
|
| /* this will be slow if the offset is large, but that should be |
| 801 |
|
| rare */ |
| 802 |
|
| char buffer; |
| 803 |
8 |
▶ | while (base_offset < offbits) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (base_offset < offbits) { |
7 |
1 |
8 |
|
| 804 |
7 |
▶ | if (i_io_read(ig, &buffer, 1) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, &buffer, 1) != 1) { |
1 |
6 |
7 |
|
| 805 |
1 |
| i_img_destroy(im); |
| 806 |
1 |
| i_push_error(0, "failed skipping to image data offset"); |
| 807 |
1 |
| return NULL; |
| 808 |
|
| } |
| 809 |
6 |
| ++base_offset; |
| 810 |
|
| } |
| 811 |
|
| } |
| 812 |
|
| |
| 813 |
11 |
| i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0); |
| 814 |
|
| |
| 815 |
11 |
| packed = mymalloc(line_size); /* checked 29jun05 tonyc */ |
| 816 |
11 |
| line = mymalloc(xsize+8); /* checked 29jun05 tonyc */ |
| 817 |
336 |
▶ | while (y != lasty) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (y != lasty) { |
328 |
8 |
336 |
|
| 818 |
328 |
▶ | if (i_io_read(ig, packed, line_size) != line_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, line_size) != line_size) { |
3 |
325 |
328 |
|
| 819 |
3 |
| myfree(packed); |
| 820 |
3 |
| myfree(line); |
| 821 |
3 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
2 |
3 |
|
| 822 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 823 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y)); |
| 824 |
1 |
| return im; |
| 825 |
|
| } |
| 826 |
|
| else { |
| 827 |
2 |
| i_push_error(0, "failed reading 1-bit bmp data"); |
| 828 |
2 |
| i_img_destroy(im); |
| 829 |
2 |
| return NULL; |
| 830 |
|
| } |
| 831 |
|
| } |
| 832 |
325 |
| in = packed; |
| 833 |
325 |
| bit = 0x80; |
| 834 |
325 |
| p = line; |
| 835 |
26325 |
▶ | for (x = 0; x < xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < xsize; ++x) { |
26000 |
325 |
26325 |
|
| 836 |
26000 |
| *p++ = (*in & bit) ? 1 : 0; |
| 837 |
26000 |
| bit >>= 1; |
| 838 |
26000 |
▶ | if (!bit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bit) { |
3050 |
22950 |
26000 |
|
| 839 |
3050 |
| ++in; |
| 840 |
3050 |
| bit = 0x80; |
| 841 |
|
| } |
| 842 |
|
| } |
| 843 |
325 |
▶ | i_ppal(im, 0, xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, 0, xsize, y, line); |
325 |
0 |
325 |
|
| 844 |
325 |
| y += yinc; |
| 845 |
|
| } |
| 846 |
|
| |
| 847 |
8 |
| myfree(packed); |
| 848 |
8 |
| myfree(line); |
| 849 |
8 |
| 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 |
26 |
| 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 |
26 |
| int line_size = (xsize + 1)/2; |
| 872 |
|
| unsigned char *in; |
| 873 |
|
| int size, i; |
| 874 |
|
| long base_offset; |
| 875 |
|
| int starty; |
| 876 |
26 |
| 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 |
26 |
| line_size = (line_size+3) / 4 * 4; |
| 881 |
|
| |
| 882 |
26 |
▶ | if (ysize > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ysize > 0) { |
24 |
2 |
26 |
|
| 883 |
24 |
| starty = ysize-1; |
| 884 |
24 |
| lasty = -1; |
| 885 |
24 |
| yinc = -1; |
| 886 |
|
| } |
| 887 |
|
| else { |
| 888 |
|
| /* when ysize is -ve it's a top-down image */ |
| 889 |
2 |
| ysize = -ysize; |
| 890 |
2 |
| starty = 0; |
| 891 |
2 |
| lasty = ysize; |
| 892 |
2 |
| yinc = 1; |
| 893 |
|
| } |
| 894 |
26 |
| y = starty; |
| 895 |
26 |
▶ | if (!clr_used) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!clr_used) |
0 |
26 |
26 |
|
| 896 |
0 |
| clr_used = 16; |
| 897 |
|
| |
| 898 |
26 |
▶ | if (clr_used > 16 || clr_used < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (clr_used > 16 || clr_used < 0) { |
24 |
2 |
26 |
| if (clr_used > 16 || clr_used < 0) { |
0 |
24 |
24 |
|
| 899 |
2 |
| im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used); |
| 900 |
2 |
| return NULL; |
| 901 |
|
| } |
| 902 |
|
| |
| 903 |
24 |
| base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4; |
| 904 |
24 |
▶ | if (offbits < base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits < base_offset) { |
1 |
23 |
24 |
|
| 905 |
1 |
| im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits); |
| 906 |
1 |
| return NULL; |
| 907 |
|
| } |
| 908 |
|
| |
| 909 |
23 |
| im = i_img_pal_new(xsize, ysize, 3, 256); |
| 910 |
23 |
▶ | if (!im) /* error should have been pushed already */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!im) /* error should have been pushed already */ |
0 |
23 |
23 |
|
| 911 |
0 |
| return NULL; |
| 912 |
23 |
▶ | if (!read_bmp_pal(ig, im, clr_used)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_bmp_pal(ig, im, clr_used)) { |
1 |
22 |
23 |
|
| 913 |
1 |
| i_img_destroy(im); |
| 914 |
1 |
| return NULL; |
| 915 |
|
| } |
| 916 |
|
| |
| 917 |
22 |
▶ | if (offbits > base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits > base_offset) { |
2 |
20 |
22 |
|
| 918 |
|
| /* this will be slow if the offset is large, but that should be |
| 919 |
|
| rare */ |
| 920 |
|
| char buffer; |
| 921 |
8 |
▶ | while (base_offset < offbits) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (base_offset < offbits) { |
7 |
1 |
8 |
|
| 922 |
7 |
▶ | if (i_io_read(ig, &buffer, 1) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, &buffer, 1) != 1) { |
1 |
6 |
7 |
|
| 923 |
1 |
| i_img_destroy(im); |
| 924 |
1 |
| i_push_error(0, "failed skipping to image data offset"); |
| 925 |
1 |
| return NULL; |
| 926 |
|
| } |
| 927 |
6 |
| ++base_offset; |
| 928 |
|
| } |
| 929 |
|
| } |
| 930 |
|
| |
| 931 |
21 |
▶ | if (line_size < 260) |
Branch: 1 branch
| Branch | True | False | Total |
| if (line_size < 260) |
21 |
0 |
21 |
|
| 932 |
21 |
| packed = mymalloc(260); /* checked 29jun05 tonyc */ |
| 933 |
|
| else |
| 934 |
0 |
| packed = mymalloc(line_size); /* checked 29jun05 tonyc */ |
| 935 |
|
| /* xsize won't approach MAXINT */ |
| 936 |
21 |
| line = mymalloc(xsize+1); /* checked 29jun05 tonyc */ |
| 937 |
21 |
▶ | if (compression == BI_RGB) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compression == BI_RGB) { |
11 |
10 |
21 |
|
| 938 |
11 |
| i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0); |
| 939 |
331 |
▶ | while (y != lasty) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (y != lasty) { |
323 |
8 |
331 |
|
| 940 |
323 |
▶ | if (i_io_read(ig, packed, line_size) != line_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, line_size) != line_size) { |
3 |
320 |
323 |
|
| 941 |
3 |
| myfree(packed); |
| 942 |
3 |
| myfree(line); |
| 943 |
3 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
2 |
3 |
|
| 944 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 945 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(y - starty)); |
| 946 |
1 |
| return im; |
| 947 |
|
| } |
| 948 |
|
| else { |
| 949 |
2 |
| i_push_error(0, "failed reading 4-bit bmp data"); |
| 950 |
2 |
| i_img_destroy(im); |
| 951 |
2 |
| return NULL; |
| 952 |
|
| } |
| 953 |
|
| } |
| 954 |
320 |
| in = packed; |
| 955 |
320 |
| p = line; |
| 956 |
13270 |
▶ | for (x = 0; x < xsize; x+=2) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < xsize; x+=2) { |
12950 |
320 |
13270 |
|
| 957 |
12950 |
| *p++ = *in >> 4; |
| 958 |
12950 |
| *p++ = *in & 0x0F; |
| 959 |
12950 |
| ++in; |
| 960 |
|
| } |
| 961 |
320 |
▶ | i_ppal(im, 0, xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, 0, xsize, y, line); |
320 |
0 |
320 |
|
| 962 |
320 |
| y += yinc; |
| 963 |
|
| } |
| 964 |
8 |
| myfree(packed); |
| 965 |
8 |
| myfree(line); |
| 966 |
|
| } |
| 967 |
10 |
▶ | else if (compression == BI_RLE4) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (compression == BI_RLE4) { |
9 |
1 |
10 |
|
| 968 |
|
| int read_size; |
| 969 |
|
| int count; |
| 970 |
9 |
| i_img_dim xlimit = (xsize + 1) / 2 * 2; /* rounded up */ |
| 971 |
|
| |
| 972 |
9 |
| i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE4", -1, 0); |
| 973 |
9 |
| x = 0; |
| 974 |
|
| while (1) { |
| 975 |
|
| /* there's always at least 2 bytes in a sequence */ |
| 976 |
3588 |
▶ | if (i_io_read(ig, packed, 2) != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, 2) != 2) { |
2 |
3586 |
3588 |
|
| 977 |
2 |
| myfree(packed); |
| 978 |
2 |
| myfree(line); |
| 979 |
2 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
1 |
2 |
|
| 980 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 981 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(y - starty)); |
| 982 |
1 |
| return im; |
| 983 |
|
| } |
| 984 |
|
| else { |
| 985 |
1 |
| i_push_error(0, "missing data during decompression"); |
| 986 |
1 |
| i_img_destroy(im); |
| 987 |
1 |
| return NULL; |
| 988 |
|
| } |
| 989 |
|
| } |
| 990 |
3586 |
▶ | else if (packed[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (packed[0]) { |
2726 |
860 |
3586 |
|
| 991 |
2726 |
| int count = packed[0]; |
| 992 |
2726 |
▶ | if (x + count > xlimit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x + count > xlimit) { |
1 |
2725 |
2726 |
|
| 993 |
|
| /* this file is corrupt */ |
| 994 |
1 |
| myfree(packed); |
| 995 |
1 |
| myfree(line); |
| 996 |
1 |
| i_push_error(0, "invalid data during decompression"); |
| 997 |
1 |
| 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 |
1 |
| i_img_destroy(im); |
| 1000 |
1 |
| return NULL; |
| 1001 |
|
| } |
| 1002 |
|
| /* fill in the line */ |
| 1003 |
43151 |
▶ | for (i = 0; i < count; i += 2) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i += 2) |
40426 |
2725 |
43151 |
|
| 1004 |
40426 |
| line[i] = packed[1] >> 4; |
| 1005 |
43151 |
▶ | for (i = 1; i < count; i += 2) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < count; i += 2) |
40426 |
2725 |
43151 |
|
| 1006 |
40426 |
| line[i] = packed[1] & 0x0F; |
| 1007 |
2725 |
▶ | i_ppal(im, x, x+count, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, x, x+count, y, line); |
2725 |
0 |
2725 |
|
| 1008 |
2725 |
| x += count; |
| 1009 |
|
| } else { |
| 1010 |
860 |
| switch (packed[1]) { |
| 1011 |
562 |
| case BMPRLE_ENDOFLINE: |
| 1012 |
562 |
| x = 0; |
| 1013 |
562 |
| y += yinc; |
| 1014 |
562 |
| break; |
| 1015 |
|
| |
| 1016 |
3 |
| case BMPRLE_ENDOFBMP: |
| 1017 |
3 |
| myfree(packed); |
| 1018 |
3 |
| myfree(line); |
| 1019 |
3 |
| return im; |
| 1020 |
|
| |
| 1021 |
0 |
| case BMPRLE_DELTA: |
| 1022 |
0 |
▶ | if (i_io_read(ig, packed, 2) != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, 2) != 2) { |
0 |
0 |
0 |
|
| 1023 |
0 |
| myfree(packed); |
| 1024 |
0 |
| myfree(line); |
| 1025 |
0 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
0 |
0 |
0 |
|
| 1026 |
0 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1027 |
0 |
| i_tags_setn(&im->tags, "i_lines_read", abs(y - starty)); |
| 1028 |
0 |
| return im; |
| 1029 |
|
| } |
| 1030 |
|
| else { |
| 1031 |
0 |
| i_push_error(0, "missing data during decompression"); |
| 1032 |
0 |
| i_img_destroy(im); |
| 1033 |
0 |
| return NULL; |
| 1034 |
|
| } |
| 1035 |
|
| } |
| 1036 |
0 |
| x += packed[0]; |
| 1037 |
0 |
| y += yinc * packed[1]; |
| 1038 |
0 |
| break; |
| 1039 |
|
| |
| 1040 |
295 |
| default: |
| 1041 |
295 |
| count = packed[1]; |
| 1042 |
295 |
▶ | if (x + count > xlimit) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x + count > xlimit) { |
1 |
294 |
295 |
|
| 1043 |
|
| /* this file is corrupt */ |
| 1044 |
1 |
| myfree(packed); |
| 1045 |
1 |
| myfree(line); |
| 1046 |
1 |
| i_push_error(0, "invalid data during decompression"); |
| 1047 |
1 |
| 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 |
1 |
| i_img_destroy(im); |
| 1050 |
1 |
| return NULL; |
| 1051 |
|
| } |
| 1052 |
294 |
| size = (count + 1) / 2; |
| 1053 |
294 |
| read_size = (size+1) / 2 * 2; |
| 1054 |
294 |
▶ | if (i_io_read(ig, packed, read_size) != read_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, read_size) != read_size) { |
2 |
292 |
294 |
|
| 1055 |
2 |
| myfree(packed); |
| 1056 |
2 |
| myfree(line); |
| 1057 |
2 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
1 |
2 |
|
| 1058 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1059 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(y - starty)); |
| 1060 |
1 |
| return im; |
| 1061 |
|
| } |
| 1062 |
|
| else { |
| 1063 |
1 |
| i_push_error(0, "missing data during decompression"); |
| 1064 |
1 |
| i_img_destroy(im); |
| 1065 |
1 |
| return NULL; |
| 1066 |
|
| } |
| 1067 |
|
| } |
| 1068 |
1156 |
▶ | for (i = 0; i < size; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < size; ++i) { |
864 |
292 |
1156 |
|
| 1069 |
864 |
| line[0] = packed[i] >> 4; |
| 1070 |
864 |
| line[1] = packed[i] & 0xF; |
| 1071 |
864 |
▶ | i_ppal(im, x, x+2, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, x, x+2, y, line); |
864 |
0 |
864 |
|
| 1072 |
864 |
| x += 2; |
| 1073 |
|
| } |
| 1074 |
292 |
| break; |
| 1075 |
|
| } |
| 1076 |
|
| } |
| 1077 |
|
| } |
| 1078 |
|
| } |
| 1079 |
|
| else { /*if (compression == BI_RLE4) {*/ |
| 1080 |
1 |
| myfree(packed); |
| 1081 |
1 |
| myfree(line); |
| 1082 |
1 |
| im_push_errorf(aIMCTX, 0, "unknown 4-bit BMP compression (%d)", compression); |
| 1083 |
1 |
| i_img_destroy(im); |
| 1084 |
1 |
| return NULL; |
| 1085 |
|
| } |
| 1086 |
|
| |
| 1087 |
8 |
| 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 |
25 |
| 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 |
25 |
| int line_size = xsize; |
| 1106 |
|
| long base_offset; |
| 1107 |
25 |
| dIMCTXio(ig); |
| 1108 |
|
| |
| 1109 |
25 |
| line_size = (line_size+3) / 4 * 4; |
| 1110 |
25 |
▶ | if (line_size < xsize) { /* if it overflowed (unlikely, but check) */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (line_size < xsize) { /* if it overflowed (unlikely, but check) */ |
0 |
25 |
25 |
|
| 1111 |
0 |
| i_push_error(0, "integer overflow during memory allocation"); |
| 1112 |
0 |
| return NULL; |
| 1113 |
|
| } |
| 1114 |
|
| |
| 1115 |
25 |
▶ | if (ysize > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ysize > 0) { |
23 |
2 |
25 |
|
| 1116 |
23 |
| start_y = ysize-1; |
| 1117 |
23 |
| lasty = -1; |
| 1118 |
23 |
| yinc = -1; |
| 1119 |
|
| } |
| 1120 |
|
| else { |
| 1121 |
|
| /* when ysize is -ve it's a top-down image */ |
| 1122 |
2 |
| ysize = -ysize; |
| 1123 |
2 |
| start_y = 0; |
| 1124 |
2 |
| lasty = ysize; |
| 1125 |
2 |
| yinc = 1; |
| 1126 |
|
| } |
| 1127 |
25 |
| y = start_y; |
| 1128 |
25 |
▶ | if (!clr_used) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!clr_used) |
0 |
25 |
25 |
|
| 1129 |
0 |
| clr_used = 256; |
| 1130 |
25 |
▶ | if (clr_used > 256 || clr_used < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (clr_used > 256 || clr_used < 0) { |
24 |
1 |
25 |
| if (clr_used > 256 || clr_used < 0) { |
0 |
24 |
24 |
|
| 1131 |
1 |
| im_push_errorf(aIMCTX, 0, "out of range colors used (%d)", clr_used); |
| 1132 |
1 |
| return NULL; |
| 1133 |
|
| } |
| 1134 |
|
| |
| 1135 |
24 |
| base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE + clr_used * 4; |
| 1136 |
24 |
▶ | if (offbits < base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits < base_offset) { |
1 |
23 |
24 |
|
| 1137 |
1 |
| im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits); |
| 1138 |
1 |
| return NULL; |
| 1139 |
|
| } |
| 1140 |
|
| |
| 1141 |
23 |
| im = i_img_pal_new(xsize, ysize, 3, 256); |
| 1142 |
23 |
▶ | if (!im) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!im) |
0 |
23 |
23 |
|
| 1143 |
0 |
| return NULL; |
| 1144 |
23 |
▶ | if (!read_bmp_pal(ig, im, clr_used)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_bmp_pal(ig, im, clr_used)) { |
1 |
22 |
23 |
|
| 1145 |
1 |
| i_img_destroy(im); |
| 1146 |
1 |
| return NULL; |
| 1147 |
|
| } |
| 1148 |
|
| |
| 1149 |
22 |
▶ | if (offbits > base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits > base_offset) { |
2 |
20 |
22 |
|
| 1150 |
|
| /* this will be slow if the offset is large, but that should be |
| 1151 |
|
| rare */ |
| 1152 |
|
| char buffer; |
| 1153 |
8 |
▶ | while (base_offset < offbits) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (base_offset < offbits) { |
7 |
1 |
8 |
|
| 1154 |
7 |
▶ | if (i_io_read(ig, &buffer, 1) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, &buffer, 1) != 1) { |
1 |
6 |
7 |
|
| 1155 |
1 |
| i_img_destroy(im); |
| 1156 |
1 |
| i_push_error(0, "failed skipping to image data offset"); |
| 1157 |
1 |
| return NULL; |
| 1158 |
|
| } |
| 1159 |
6 |
| ++base_offset; |
| 1160 |
|
| } |
| 1161 |
|
| } |
| 1162 |
|
| |
| 1163 |
21 |
| line = mymalloc(line_size); /* checked 29jun05 tonyc */ |
| 1164 |
21 |
▶ | if (compression == BI_RGB) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compression == BI_RGB) { |
11 |
10 |
21 |
|
| 1165 |
11 |
| i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RGB", -1, 0); |
| 1166 |
328 |
▶ | while (y != lasty) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (y != lasty) { |
320 |
8 |
328 |
|
| 1167 |
320 |
▶ | if (i_io_read(ig, line, line_size) != line_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, line, line_size) != line_size) { |
3 |
317 |
320 |
|
| 1168 |
3 |
| myfree(line); |
| 1169 |
3 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
2 |
3 |
|
| 1170 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1171 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(start_y - y)); |
| 1172 |
1 |
| return im; |
| 1173 |
|
| } |
| 1174 |
|
| else { |
| 1175 |
2 |
| i_push_error(0, "failed reading 8-bit bmp data"); |
| 1176 |
2 |
| i_img_destroy(im); |
| 1177 |
2 |
| return NULL; |
| 1178 |
|
| } |
| 1179 |
|
| } |
| 1180 |
317 |
▶ | i_ppal(im, 0, xsize, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, 0, xsize, y, line); |
317 |
0 |
317 |
|
| 1181 |
317 |
| y += yinc; |
| 1182 |
|
| } |
| 1183 |
8 |
| myfree(line); |
| 1184 |
|
| } |
| 1185 |
10 |
▶ | else if (compression == BI_RLE8) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (compression == BI_RLE8) { |
9 |
1 |
10 |
|
| 1186 |
|
| int read_size; |
| 1187 |
|
| int count; |
| 1188 |
|
| unsigned char packed[2]; |
| 1189 |
|
| |
| 1190 |
9 |
| i_tags_add(&im->tags, "bmp_compression_name", 0, "BI_RLE8", -1, 0); |
| 1191 |
9 |
| x = 0; |
| 1192 |
|
| while (1) { |
| 1193 |
|
| /* there's always at least 2 bytes in a sequence */ |
| 1194 |
3610 |
▶ | if (i_io_read(ig, packed, 2) != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, 2) != 2) { |
2 |
3608 |
3610 |
|
| 1195 |
2 |
| myfree(line); |
| 1196 |
2 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
1 |
2 |
|
| 1197 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1198 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y)); |
| 1199 |
1 |
| return im; |
| 1200 |
|
| } |
| 1201 |
|
| else { |
| 1202 |
1 |
| i_push_error(0, "missing data during decompression"); |
| 1203 |
1 |
| i_img_destroy(im); |
| 1204 |
1 |
| return NULL; |
| 1205 |
|
| } |
| 1206 |
|
| } |
| 1207 |
3608 |
▶ | if (packed[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (packed[0]) { |
2296 |
1312 |
3608 |
|
| 1208 |
2296 |
▶ | if (x + packed[0] > xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x + packed[0] > xsize) { |
1 |
2295 |
2296 |
|
| 1209 |
|
| /* this file isn't incomplete, it's corrupt */ |
| 1210 |
1 |
| myfree(line); |
| 1211 |
1 |
| i_push_error(0, "invalid data during decompression"); |
| 1212 |
1 |
| i_img_destroy(im); |
| 1213 |
1 |
| return NULL; |
| 1214 |
|
| } |
| 1215 |
2295 |
| memset(line, packed[1], packed[0]); |
| 1216 |
2295 |
▶ | i_ppal(im, x, x+packed[0], y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, x, x+packed[0], y, line); |
2295 |
0 |
2295 |
|
| 1217 |
2295 |
| x += packed[0]; |
| 1218 |
|
| } else { |
| 1219 |
1312 |
| switch (packed[1]) { |
| 1220 |
540 |
| case BMPRLE_ENDOFLINE: |
| 1221 |
540 |
| x = 0; |
| 1222 |
540 |
| y += yinc; |
| 1223 |
540 |
| break; |
| 1224 |
|
| |
| 1225 |
3 |
| case BMPRLE_ENDOFBMP: |
| 1226 |
3 |
| myfree(line); |
| 1227 |
3 |
| return im; |
| 1228 |
|
| |
| 1229 |
0 |
| case BMPRLE_DELTA: |
| 1230 |
0 |
▶ | if (i_io_read(ig, packed, 2) != 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, packed, 2) != 2) { |
0 |
0 |
0 |
|
| 1231 |
0 |
| myfree(line); |
| 1232 |
0 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
0 |
0 |
0 |
|
| 1233 |
0 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1234 |
0 |
| i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y)); |
| 1235 |
0 |
| return im; |
| 1236 |
|
| } |
| 1237 |
|
| else { |
| 1238 |
0 |
| i_push_error(0, "missing data during decompression"); |
| 1239 |
0 |
| i_img_destroy(im); |
| 1240 |
0 |
| return NULL; |
| 1241 |
|
| } |
| 1242 |
|
| } |
| 1243 |
0 |
| x += packed[0]; |
| 1244 |
0 |
| y += yinc * packed[1]; |
| 1245 |
0 |
| break; |
| 1246 |
|
| |
| 1247 |
769 |
| default: |
| 1248 |
769 |
| count = packed[1]; |
| 1249 |
769 |
▶ | if (x + count > xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (x + count > xsize) { |
1 |
768 |
769 |
|
| 1250 |
|
| /* runs shouldn't cross a line boundary */ |
| 1251 |
|
| /* this file isn't incomplete, it's corrupt */ |
| 1252 |
1 |
| myfree(line); |
| 1253 |
1 |
| i_push_error(0, "invalid data during decompression"); |
| 1254 |
1 |
| i_img_destroy(im); |
| 1255 |
1 |
| return NULL; |
| 1256 |
|
| } |
| 1257 |
768 |
| read_size = (count+1) / 2 * 2; |
| 1258 |
768 |
▶ | if (i_io_read(ig, line, read_size) != read_size) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, line, read_size) != read_size) { |
2 |
766 |
768 |
|
| 1259 |
2 |
| myfree(line); |
| 1260 |
2 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
1 |
2 |
|
| 1261 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1262 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(start_y-y)); |
| 1263 |
1 |
| return im; |
| 1264 |
|
| } |
| 1265 |
|
| else { |
| 1266 |
1 |
| i_push_error(0, "missing data during decompression"); |
| 1267 |
1 |
| i_img_destroy(im); |
| 1268 |
1 |
| return NULL; |
| 1269 |
|
| } |
| 1270 |
|
| } |
| 1271 |
766 |
▶ | i_ppal(im, x, x+count, y, line); |
Branch: 1 branch
| Branch | True | False | Total |
| i_ppal(im, x, x+count, y, line); |
766 |
0 |
766 |
|
| 1272 |
766 |
| x += count; |
| 1273 |
766 |
| break; |
| 1274 |
|
| } |
| 1275 |
|
| } |
| 1276 |
|
| } |
| 1277 |
|
| } |
| 1278 |
|
| else { |
| 1279 |
1 |
| myfree(line); |
| 1280 |
1 |
| im_push_errorf(aIMCTX, 0, "unknown 8-bit BMP compression (%d)", compression); |
| 1281 |
1 |
| i_img_destroy(im); |
| 1282 |
1 |
| return NULL; |
| 1283 |
|
| } |
| 1284 |
|
| |
| 1285 |
8 |
| 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 |
26 |
| 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 |
26 |
| int pix_size = bit_count / 8; |
| 1344 |
26 |
| int line_size = xsize * pix_size; |
| 1345 |
|
| struct bm_masks masks; |
| 1346 |
26 |
| char unpack_code[2] = ""; |
| 1347 |
|
| int i; |
| 1348 |
|
| int extras; |
| 1349 |
|
| char junk[4]; |
| 1350 |
|
| const char *compression_name; |
| 1351 |
|
| int bytes; |
| 1352 |
26 |
| long base_offset = FILEHEAD_SIZE + INFOHEAD_SIZE; |
| 1353 |
26 |
| dIMCTXio(ig); |
| 1354 |
|
| |
| 1355 |
26 |
| unpack_code[0] = "v3V"[pix_size-2]; |
| 1356 |
26 |
| unpack_code[1] = '\0'; |
| 1357 |
|
| |
| 1358 |
26 |
| line_size = (line_size+3) / 4 * 4; |
| 1359 |
26 |
| extras = line_size - xsize * pix_size; |
| 1360 |
|
| |
| 1361 |
26 |
▶ | if (ysize > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ysize > 0) { |
25 |
1 |
26 |
|
| 1362 |
25 |
| starty = ysize-1; |
| 1363 |
25 |
| lasty = -1; |
| 1364 |
25 |
| yinc = -1; |
| 1365 |
|
| } |
| 1366 |
|
| else { |
| 1367 |
|
| /* when ysize is -ve it's a top-down image */ |
| 1368 |
1 |
| ysize = -ysize; |
| 1369 |
1 |
| starty = 0; |
| 1370 |
1 |
| lasty = ysize; |
| 1371 |
1 |
| yinc = 1; |
| 1372 |
|
| } |
| 1373 |
26 |
| y = starty; |
| 1374 |
26 |
▶ | if (compression == BI_RGB) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (compression == BI_RGB) { |
25 |
1 |
26 |
|
| 1375 |
25 |
| compression_name = "BI_RGB"; |
| 1376 |
25 |
| masks = std_masks[pix_size-2]; |
| 1377 |
|
| |
| 1378 |
|
| /* there's a potential "palette" after the header */ |
| 1379 |
25 |
▶ | for (i = 0; i < clr_used; ++clr_used) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < clr_used; ++clr_used) { |
0 |
25 |
25 |
|
| 1380 |
|
| char buf[4]; |
| 1381 |
0 |
▶ | if (i_io_read(ig, buf, 4) != 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, buf, 4) != 4) { |
0 |
0 |
0 |
|
| 1382 |
0 |
| i_push_error(0, "skipping colors"); |
| 1383 |
0 |
| return 0; |
| 1384 |
|
| } |
| 1385 |
0 |
| base_offset += 4; |
| 1386 |
|
| } |
| 1387 |
|
| } |
| 1388 |
1 |
▶ | else if (compression == BI_BITFIELDS) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (compression == BI_BITFIELDS) { |
0 |
1 |
1 |
|
| 1389 |
|
| int pos; |
| 1390 |
|
| unsigned bits; |
| 1391 |
0 |
| compression_name = "BI_BITFIELDS"; |
| 1392 |
|
| |
| 1393 |
0 |
▶ | for (i = 0; i < 3; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; ++i) { |
0 |
0 |
0 |
|
| 1394 |
|
| i_packed_t rmask; |
| 1395 |
0 |
▶ | if (!read_packed(ig, "V", &rmask)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_packed(ig, "V", &rmask)) { |
0 |
0 |
0 |
|
| 1396 |
0 |
| i_push_error(0, "reading pixel masks"); |
| 1397 |
0 |
| return 0; |
| 1398 |
|
| } |
| 1399 |
0 |
▶ | if (rmask == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rmask == 0) { |
0 |
0 |
0 |
|
| 1400 |
0 |
| im_push_errorf(aIMCTX, 0, "Zero mask for channel %d", i); |
| 1401 |
0 |
| return NULL; |
| 1402 |
|
| } |
| 1403 |
0 |
| masks.masks[i] = rmask; |
| 1404 |
|
| /* work out a shift for the mask */ |
| 1405 |
0 |
| pos = 0; |
| 1406 |
0 |
| bits = masks.masks[i]; |
| 1407 |
0 |
▶ | while (!(bits & 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!(bits & 1)) { |
0 |
0 |
0 |
|
| 1408 |
0 |
| ++pos; |
| 1409 |
0 |
| bits >>= 1; |
| 1410 |
|
| } |
| 1411 |
0 |
| masks.shifts[i] = pos; |
| 1412 |
0 |
| pos = 0; |
| 1413 |
0 |
▶ | while (bits & 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (bits & 1) { |
0 |
0 |
0 |
|
| 1414 |
0 |
| ++pos; |
| 1415 |
0 |
| bits >>= 1; |
| 1416 |
|
| } |
| 1417 |
0 |
| 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 |
0 |
| base_offset += 3 * 4; |
| 1422 |
|
| } |
| 1423 |
|
| else { |
| 1424 |
1 |
| im_push_errorf(aIMCTX, 0, "unknown 24-bit BMP compression (%d)", compression); |
| 1425 |
1 |
| return NULL; |
| 1426 |
|
| } |
| 1427 |
|
| |
| 1428 |
25 |
▶ | if (offbits < base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits < base_offset) { |
1 |
24 |
25 |
|
| 1429 |
1 |
| im_push_errorf(aIMCTX, 0, "image data offset too small (%ld)", offbits); |
| 1430 |
1 |
| return NULL; |
| 1431 |
|
| } |
| 1432 |
|
| |
| 1433 |
24 |
▶ | if (offbits > base_offset) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (offbits > base_offset) { |
2 |
22 |
24 |
|
| 1434 |
|
| /* this will be slow if the offset is large, but that should be |
| 1435 |
|
| rare */ |
| 1436 |
|
| char buffer; |
| 1437 |
8 |
▶ | while (base_offset < offbits) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (base_offset < offbits) { |
7 |
1 |
8 |
|
| 1438 |
7 |
▶ | if (i_io_read(ig, &buffer, 1) != 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, &buffer, 1) != 1) { |
1 |
6 |
7 |
|
| 1439 |
1 |
| i_push_error(0, "failed skipping to image data offset"); |
| 1440 |
1 |
| return NULL; |
| 1441 |
|
| } |
| 1442 |
6 |
| ++base_offset; |
| 1443 |
|
| } |
| 1444 |
|
| } |
| 1445 |
|
| |
| 1446 |
23 |
| im = i_img_8_new(xsize, ysize, 3); |
| 1447 |
23 |
▶ | if (!im) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!im) |
0 |
23 |
23 |
|
| 1448 |
0 |
| return NULL; |
| 1449 |
|
| |
| 1450 |
23 |
| 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 |
23 |
| bytes = sizeof(i_color) * xsize; |
| 1455 |
23 |
▶ | if (bytes / sizeof(i_color) != (size_t)xsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bytes / sizeof(i_color) != (size_t)xsize) { |
0 |
23 |
23 |
|
| 1456 |
0 |
| i_img_destroy(im); |
| 1457 |
0 |
| i_push_error(0, "integer overflow calculating buffer size"); |
| 1458 |
0 |
| return NULL; |
| 1459 |
|
| } |
| 1460 |
23 |
| line = mymalloc(bytes); /* checked 29jun05 tonyc */ |
| 1461 |
1096 |
▶ | while (y != lasty) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (y != lasty) { |
1076 |
20 |
1096 |
|
| 1462 |
1076 |
| p = line; |
| 1463 |
119950 |
▶ | for (x = 0; x < xsize; ++x) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = 0; x < xsize; ++x) { |
118877 |
1073 |
119950 |
|
| 1464 |
|
| i_packed_t pixel; |
| 1465 |
118877 |
▶ | if (!read_packed(ig, unpack_code, &pixel)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!read_packed(ig, unpack_code, &pixel)) { |
3 |
118874 |
118877 |
|
| 1466 |
3 |
| myfree(line); |
| 1467 |
3 |
▶ | if (allow_incomplete) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (allow_incomplete) { |
1 |
2 |
3 |
|
| 1468 |
1 |
| i_tags_setn(&im->tags, "i_incomplete", 1); |
| 1469 |
1 |
| i_tags_setn(&im->tags, "i_lines_read", abs(starty - y)); |
| 1470 |
3 |
| return im; |
| 1471 |
|
| } |
| 1472 |
|
| else { |
| 1473 |
2 |
| i_push_error(0, "failed reading image data"); |
| 1474 |
2 |
| i_img_destroy(im); |
| 1475 |
2 |
| return NULL; |
| 1476 |
|
| } |
| 1477 |
|
| } |
| 1478 |
475496 |
▶ | for (i = 0; i < 3; ++i) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 3; ++i) { |
356622 |
118874 |
475496 |
|
| 1479 |
356622 |
| int sample = (pixel & masks.masks[i]) >> masks.shifts[i]; |
| 1480 |
356622 |
| int bits = masks.bits[i]; |
| 1481 |
356622 |
▶ | if (bits < 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (bits < 8) { |
0 |
356622 |
356622 |
|
| 1482 |
0 |
| sample = (sample * samp_converts[bits-1].mult) >> samp_converts[bits-1].shift; |
| 1483 |
|
| } |
| 1484 |
356622 |
▶ | else if (bits) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (bits) { |
356622 |
0 |
356622 |
|
| 1485 |
356622 |
| sample >>= bits - 8; |
| 1486 |
|
| } |
| 1487 |
356622 |
| p->channel[i] = sample; |
| 1488 |
|
| } |
| 1489 |
118874 |
| ++p; |
| 1490 |
|
| } |
| 1491 |
1073 |
| i_plin(im, 0, xsize, y, line); |
| 1492 |
1073 |
▶ | if (extras) |
Branch: 1 branch
| Branch | True | False | Total |
| if (extras) |
750 |
323 |
1073 |
|
| 1493 |
750 |
| i_io_read(ig, junk, extras); |
| 1494 |
1073 |
| y += yinc; |
| 1495 |
|
| } |
| 1496 |
20 |
| myfree(line); |
| 1497 |
|
| |
| 1498 |
20 |
| return im; |
| 1499 |
|
| } |
| 1500 |
|
| |
| 1501 |
|
| /* |
| 1502 |
|
| =head1 SEE ALSO |
| 1503 |
|
| |
| 1504 |
|
| Imager(3) |
| 1505 |
|
| |
| 1506 |
|
| =head1 AUTHOR |
| 1507 |
|
| |
| 1508 |
|
| Tony Cook <tony@develop-help.com> |
| 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 |
|
| */ |