| 1 |
|
| #include "imageri.h" |
| 2 |
|
| #include "log.h" |
| 3 |
|
| #include "iolayer.h" |
| 4 |
|
| |
| 5 |
|
| #include <stdlib.h> |
| 6 |
|
| #include <errno.h> |
| 7 |
|
| |
| 8 |
|
| #define IDSTRING_MAX 255 |
| 9 |
|
| |
| 10 |
|
| /* |
| 11 |
|
| =head1 NAME |
| 12 |
|
| |
| 13 |
|
| tga.c - implements reading and writing targa files, uses io layer. |
| 14 |
|
| |
| 15 |
|
| =head1 SYNOPSIS |
| 16 |
|
| |
| 17 |
|
| io_glue *ig = io_new_fd( fd ); |
| 18 |
|
| i_img *im = i_readtga_wiol(ig, -1); // no limit on how much is read |
| 19 |
|
| // or |
| 20 |
|
| io_glue *ig = io_new_fd( fd ); |
| 21 |
|
| return_code = i_writetga_wiol(im, ig); |
| 22 |
|
| |
| 23 |
|
| =head1 DESCRIPTION |
| 24 |
|
| |
| 25 |
|
| tga.c implements the basic functions to read and write portable targa |
| 26 |
|
| files. It uses the iolayer and needs either a seekable source or an |
| 27 |
|
| entire memory mapped buffer. |
| 28 |
|
| |
| 29 |
|
| =head1 FUNCTION REFERENCE |
| 30 |
|
| |
| 31 |
|
| Some of these functions are internal. |
| 32 |
|
| |
| 33 |
|
| =over |
| 34 |
|
| |
| 35 |
|
| =cut |
| 36 |
|
| */ |
| 37 |
|
| |
| 38 |
|
| |
| 39 |
|
| |
| 40 |
|
| |
| 41 |
|
| typedef struct { |
| 42 |
|
| unsigned char idlength; |
| 43 |
|
| char colourmaptype; |
| 44 |
|
| char datatypecode; |
| 45 |
|
| unsigned colourmaporigin; |
| 46 |
|
| unsigned colourmaplength; |
| 47 |
|
| unsigned char colourmapdepth; |
| 48 |
|
| unsigned x_origin; |
| 49 |
|
| unsigned y_origin; |
| 50 |
|
| int width; |
| 51 |
|
| int height; |
| 52 |
|
| char bitsperpixel; |
| 53 |
|
| char imagedescriptor; |
| 54 |
|
| } tga_header; |
| 55 |
|
| |
| 56 |
|
| |
| 57 |
|
| typedef enum { NoInit, Raw, Rle } rle_state; |
| 58 |
|
| |
| 59 |
|
| typedef struct { |
| 60 |
|
| int compressed; |
| 61 |
|
| size_t bytepp; |
| 62 |
|
| rle_state state; |
| 63 |
|
| unsigned char cval[4]; |
| 64 |
|
| int len; |
| 65 |
|
| unsigned char hdr; |
| 66 |
|
| io_glue *ig; |
| 67 |
|
| } tga_source; |
| 68 |
|
| |
| 69 |
|
| |
| 70 |
|
| typedef struct { |
| 71 |
|
| int compressed; |
| 72 |
|
| int bytepp; |
| 73 |
|
| io_glue *ig; |
| 74 |
|
| } tga_dest; |
| 75 |
|
| |
| 76 |
|
| #define TGA_MAX_DIM 0xFFFF |
| 77 |
|
| |
| 78 |
|
| /* |
| 79 |
|
| =item bpp_to_bytes(bpp) |
| 80 |
|
| |
| 81 |
|
| Convert bits per pixel into bytes per pixel |
| 82 |
|
| |
| 83 |
|
| bpp - bits per pixel |
| 84 |
|
| |
| 85 |
|
| =cut |
| 86 |
|
| */ |
| 87 |
|
| |
| 88 |
|
| |
| 89 |
|
| static |
| 90 |
|
| size_t |
| 91 |
65 |
| bpp_to_bytes(unsigned int bpp) { |
| 92 |
65 |
| switch (bpp) { |
| 93 |
0 |
| case 8: |
| 94 |
0 |
| return 1; |
| 95 |
7 |
| case 15: |
| 96 |
|
| case 16: |
| 97 |
7 |
| return 2; |
| 98 |
58 |
| case 24: |
| 99 |
58 |
| return 3; |
| 100 |
0 |
| case 32: |
| 101 |
0 |
| return 4; |
| 102 |
|
| } |
| 103 |
0 |
| return 0; |
| 104 |
|
| } |
| 105 |
|
| |
| 106 |
|
| |
| 107 |
|
| |
| 108 |
|
| /* |
| 109 |
|
| =item bpp_to_channels(bpp) |
| 110 |
|
| |
| 111 |
|
| Convert bits per pixel and the number of attribute bits into channels |
| 112 |
|
| in the image |
| 113 |
|
| |
| 114 |
|
| bpp - bits per pixel |
| 115 |
|
| attr_bit_count - number of attribute bits |
| 116 |
|
| |
| 117 |
|
| =cut |
| 118 |
|
| */ |
| 119 |
|
| |
| 120 |
|
| static |
| 121 |
|
| int |
| 122 |
27 |
| bpp_to_channels(unsigned int bpp, int attr_bit_count) { |
| 123 |
27 |
| switch (bpp) { |
| 124 |
0 |
| case 8: |
| 125 |
0 |
| return 1; |
| 126 |
2 |
| case 16: |
| 127 |
2 |
▶ | if (attr_bit_count == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (attr_bit_count == 1) |
2 |
0 |
2 |
|
| 128 |
2 |
| return 4; |
| 129 |
|
| /* FALLTHROUGH */ |
| 130 |
|
| case 15: |
| 131 |
0 |
| return 3; |
| 132 |
0 |
| case 32: |
| 133 |
0 |
▶ | if (attr_bit_count == 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (attr_bit_count == 8) |
0 |
0 |
0 |
|
| 134 |
0 |
| return 4; |
| 135 |
|
| /* FALLTHROUGH */ |
| 136 |
|
| case 24: |
| 137 |
25 |
| return 3; |
| 138 |
|
| } |
| 139 |
0 |
| return 0; |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
|
| |
| 143 |
|
| |
| 144 |
|
| /* |
| 145 |
|
| * Packing functions - used for (un)packing |
| 146 |
|
| * datastructures into raw bytes. |
| 147 |
|
| */ |
| 148 |
|
| |
| 149 |
|
| |
| 150 |
|
| /* |
| 151 |
|
| =item color_unpack(buf, bytepp, val) |
| 152 |
|
| |
| 153 |
|
| Unpacks bytes into colour structures, for 2 byte type the first byte |
| 154 |
|
| coming from the file will actually be GGGBBBBB, and the second will be |
| 155 |
|
| ARRRRRGG. "A" represents an attribute bit. The 3 byte entry contains |
| 156 |
|
| 1 byte each of blue, green, and red. The 4 byte entry contains 1 byte |
| 157 |
|
| each of blue, green, red, and attribute. |
| 158 |
|
| |
| 159 |
|
| buf - pointer to data |
| 160 |
|
| bytepp - bytes per pixel |
| 161 |
|
| val - pointer to color to store to |
| 162 |
|
| |
| 163 |
|
| =cut |
| 164 |
|
| */ |
| 165 |
|
| |
| 166 |
|
| static |
| 167 |
|
| void |
| 168 |
484024 |
| color_unpack(unsigned char *buf, int bytepp, i_color *val) { |
| 169 |
484024 |
| switch (bytepp) { |
| 170 |
0 |
| case 1: |
| 171 |
0 |
| val->gray.gray_color = buf[0]; |
| 172 |
0 |
| break; |
| 173 |
800 |
| case 2: |
| 174 |
800 |
| val->rgba.r = (buf[1] & 0x7c) << 1; |
| 175 |
800 |
| val->rgba.g = ((buf[1] & 0x03) << 6) | ((buf[0] & 0xe0) >> 2); |
| 176 |
800 |
| val->rgba.b = (buf[0] & 0x1f) << 3; |
| 177 |
800 |
▶ | val->rgba.a = (buf[1] & 0x80) ? 0 : 255; |
Branch: 1 branch
| Branch | True | False | Total |
| val->rgba.a = (buf[1] & 0x80) ? 0 : 255; |
400 |
400 |
800 |
|
| 178 |
800 |
| val->rgba.r |= val->rgba.r >> 5; |
| 179 |
800 |
| val->rgba.g |= val->rgba.g >> 5; |
| 180 |
800 |
| val->rgba.b |= val->rgba.b >> 5; |
| 181 |
800 |
| break; |
| 182 |
483224 |
| case 3: |
| 183 |
483224 |
| val->rgb.b = buf[0]; |
| 184 |
483224 |
| val->rgb.g = buf[1]; |
| 185 |
483224 |
| val->rgb.r = buf[2]; |
| 186 |
483224 |
| break; |
| 187 |
0 |
| case 4: |
| 188 |
0 |
| val->rgba.b = buf[0]; |
| 189 |
0 |
| val->rgba.g = buf[1]; |
| 190 |
0 |
| val->rgba.r = buf[2]; |
| 191 |
0 |
| val->rgba.a = buf[3]; |
| 192 |
0 |
| break; |
| 193 |
|
| } |
| 194 |
484024 |
| } |
| 195 |
|
| |
| 196 |
|
| |
| 197 |
|
| |
| 198 |
|
| /* |
| 199 |
|
| =item color_pack |
| 200 |
|
| |
| 201 |
|
| Packs a colour into an array of bytes, for 2 byte type the first byte |
| 202 |
|
| will be GGGBBBBB, and the second will be ARRRRRGG. "A" represents an |
| 203 |
|
| attribute bit. The 3 byte entry contains 1 byte each of blue, green, |
| 204 |
|
| and red. The 4 byte entry contains 1 byte each of blue, green, red, |
| 205 |
|
| and attribute. |
| 206 |
|
| |
| 207 |
|
| buf - destination buffer |
| 208 |
|
| bitspp - bits per pixel |
| 209 |
|
| val - color to pack |
| 210 |
|
| |
| 211 |
|
| =cut |
| 212 |
|
| */ |
| 213 |
|
| |
| 214 |
|
| static |
| 215 |
|
| void |
| 216 |
519584 |
| color_pack(unsigned char *buf, int bitspp, i_color *val) { |
| 217 |
519584 |
| switch (bitspp) { |
| 218 |
0 |
| case 8: |
| 219 |
0 |
| buf[0] = val->gray.gray_color; |
| 220 |
0 |
| break; |
| 221 |
400 |
| case 16: |
| 222 |
400 |
| buf[0] = (val->rgba.b >> 3); |
| 223 |
400 |
| buf[0] |= (val->rgba.g & 0x38) << 2; |
| 224 |
400 |
| buf[1] = (val->rgba.r & 0xf8)>> 1; |
| 225 |
400 |
| buf[1] |= (val->rgba.g >> 6); |
| 226 |
400 |
▶ | buf[1] |= val->rgba.a > 0x7f ? 0 : 0x80; |
Branch: 1 branch
| Branch | True | False | Total |
| buf[1] |= val->rgba.a > 0x7f ? 0 : 0x80; |
200 |
200 |
400 |
|
| 227 |
400 |
| break; |
| 228 |
45432 |
| case 15: |
| 229 |
45432 |
| buf[0] = (val->rgba.b >> 3); |
| 230 |
45432 |
| buf[0] |= (val->rgba.g & 0x38) << 2; |
| 231 |
45432 |
| buf[1] = (val->rgba.r & 0xf8)>> 1; |
| 232 |
45432 |
| buf[1] |= (val->rgba.g >> 6); |
| 233 |
45432 |
| break; |
| 234 |
473752 |
| case 24: |
| 235 |
473752 |
| buf[0] = val->rgb.b; |
| 236 |
473752 |
| buf[1] = val->rgb.g; |
| 237 |
473752 |
| buf[2] = val->rgb.r; |
| 238 |
473752 |
| break; |
| 239 |
0 |
| case 32: |
| 240 |
0 |
| buf[0] = val->rgba.b; |
| 241 |
0 |
| buf[1] = val->rgba.g; |
| 242 |
0 |
| buf[2] = val->rgba.r; |
| 243 |
0 |
| buf[3] = val->rgba.a; |
| 244 |
0 |
| break; |
| 245 |
|
| } |
| 246 |
519584 |
| } |
| 247 |
|
| |
| 248 |
|
| |
| 249 |
|
| /* |
| 250 |
|
| =item find_repeat |
| 251 |
|
| |
| 252 |
|
| Helper function for rle compressor to find the next triple repeat of the |
| 253 |
|
| same pixel value in buffer. |
| 254 |
|
| |
| 255 |
|
| buf - buffer |
| 256 |
|
| length - number of pixel values in buffer |
| 257 |
|
| bytepp - number of bytes in a pixel value |
| 258 |
|
| |
| 259 |
|
| =cut |
| 260 |
|
| */ |
| 261 |
|
| |
| 262 |
|
| static |
| 263 |
|
| int |
| 264 |
133018 |
| find_repeat(unsigned char *buf, int length, int bytepp) { |
| 265 |
133018 |
| int i = 0; |
| 266 |
|
| |
| 267 |
195879 |
▶ | while(i<length-1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(i<length-1) { |
89384 |
106495 |
195879 |
|
| 268 |
89384 |
▶ | if(memcmp(buf+i*bytepp, buf+(i+1)*bytepp, bytepp) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if(memcmp(buf+i*bytepp, buf+(i+1)*bytepp, bytepp) == 0) { |
30419 |
58965 |
89384 |
|
| 269 |
30419 |
▶ | if (i == length-2) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i == length-2) return -1; |
0 |
30419 |
30419 |
|
| 270 |
30419 |
▶ | if (memcmp(buf+(i+1)*bytepp, buf+(i+2)*bytepp,bytepp) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(buf+(i+1)*bytepp, buf+(i+2)*bytepp,bytepp) == 0) |
26523 |
3896 |
30419 |
|
| 271 |
26523 |
| return i; |
| 272 |
3896 |
| else i++; |
| 273 |
|
| } |
| 274 |
62861 |
| i++; |
| 275 |
|
| } |
| 276 |
106495 |
| return -1; |
| 277 |
|
| } |
| 278 |
|
| |
| 279 |
|
| |
| 280 |
|
| /* |
| 281 |
|
| =item find_span |
| 282 |
|
| |
| 283 |
|
| Helper function for rle compressor to find the length of a span where |
| 284 |
|
| the same pixel value is in the buffer. |
| 285 |
|
| |
| 286 |
|
| buf - buffer |
| 287 |
|
| length - number of pixel values in buffer |
| 288 |
|
| bytepp - number of bytes in a pixel value |
| 289 |
|
| |
| 290 |
|
| =cut |
| 291 |
|
| */ |
| 292 |
|
| |
| 293 |
|
| static |
| 294 |
|
| int |
| 295 |
26523 |
| find_span(unsigned char *buf, int length, int bytepp) { |
| 296 |
26523 |
| int i = 0; |
| 297 |
461761 |
▶ | while(i<length) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(i<length) { |
459189 |
2572 |
461761 |
|
| 298 |
459189 |
▶ | if(memcmp(buf, buf+(i*bytepp), bytepp) != 0) return i; |
Branch: 1 branch
| Branch | True | False | Total |
| if(memcmp(buf, buf+(i*bytepp), bytepp) != 0) return i; |
23951 |
435238 |
459189 |
|
| 299 |
435238 |
| i++; |
| 300 |
|
| } |
| 301 |
2572 |
| return length; |
| 302 |
|
| } |
| 303 |
|
| |
| 304 |
|
| |
| 305 |
|
| /* |
| 306 |
|
| =item tga_header_unpack(header, headbuf) |
| 307 |
|
| |
| 308 |
|
| Unpacks the header structure into from buffer and stores |
| 309 |
|
| in the header structure. |
| 310 |
|
| |
| 311 |
|
| header - header structure |
| 312 |
|
| headbuf - buffer to unpack from |
| 313 |
|
| |
| 314 |
|
| =cut |
| 315 |
|
| */ |
| 316 |
|
| |
| 317 |
|
| static |
| 318 |
|
| void |
| 319 |
72 |
| tga_header_unpack(tga_header *header, unsigned char headbuf[18]) { |
| 320 |
72 |
| header->idlength = headbuf[0]; |
| 321 |
72 |
| header->colourmaptype = headbuf[1]; |
| 322 |
72 |
| header->datatypecode = headbuf[2]; |
| 323 |
72 |
| header->colourmaporigin = (headbuf[4] << 8) + headbuf[3]; |
| 324 |
72 |
| header->colourmaplength = (headbuf[6] << 8) + headbuf[5]; |
| 325 |
72 |
| header->colourmapdepth = headbuf[7]; |
| 326 |
72 |
| header->x_origin = (headbuf[9] << 8) + headbuf[8]; |
| 327 |
72 |
| header->y_origin = (headbuf[11] << 8) + headbuf[10]; |
| 328 |
72 |
| header->width = (headbuf[13] << 8) + headbuf[12]; |
| 329 |
72 |
| header->height = (headbuf[15] << 8) + headbuf[14]; |
| 330 |
72 |
| header->bitsperpixel = headbuf[16]; |
| 331 |
72 |
| header->imagedescriptor = headbuf[17]; |
| 332 |
72 |
| } |
| 333 |
|
| |
| 334 |
|
| |
| 335 |
|
| /* this function should never produce diagnostics to stdout, maybe to the logfile */ |
| 336 |
|
| int |
| 337 |
44 |
| tga_header_verify(unsigned char headbuf[18]) { |
| 338 |
|
| tga_header header; |
| 339 |
44 |
| tga_header_unpack(&header, headbuf); |
| 340 |
44 |
| switch (header.datatypecode) { |
| 341 |
3 |
| default: |
| 342 |
|
| /*printf("bad typecode!\n");*/ |
| 343 |
3 |
| return 0; |
| 344 |
17 |
| case 1: /* Uncompressed, color-mapped images */ |
| 345 |
|
| case 3: /* Uncompressed, grayscale images */ |
| 346 |
|
| case 9: /* Compressed, color-mapped images */ |
| 347 |
|
| case 11: /* Compressed, grayscale images */ |
| 348 |
17 |
▶ | if (header.bitsperpixel != 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (header.bitsperpixel != 8) |
17 |
0 |
17 |
|
| 349 |
17 |
| return 0; |
| 350 |
0 |
| break; |
| 351 |
24 |
| case 0: |
| 352 |
|
| case 2: /* Uncompressed, rgb images */ |
| 353 |
|
| case 10: /* Compressed, rgb images */ |
| 354 |
24 |
▶ | if (header.bitsperpixel != 15 && header.bitsperpixel != 16 |
Branch: 2 branches
| Branch | True | False | Total |
| if (header.bitsperpixel != 15 && header.bitsperpixel != 16 |
24 |
0 |
24 |
| if (header.bitsperpixel != 15 && header.bitsperpixel != 16 |
22 |
2 |
24 |
|
| 355 |
22 |
▶ | && header.bitsperpixel != 24 && header.bitsperpixel != 32) |
Branch: 2 branches
| Branch | True | False | Total |
| && header.bitsperpixel != 24 && header.bitsperpixel != 32) |
7 |
15 |
22 |
| && header.bitsperpixel != 24 && header.bitsperpixel != 32) |
6 |
1 |
7 |
|
| 356 |
6 |
| return 0; |
| 357 |
18 |
| break; |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
18 |
| switch (header.colourmaptype) { |
| 361 |
0 |
| default: |
| 362 |
|
| /*printf("bad colourmaptype!\n");*/ |
| 363 |
0 |
| return 0; |
| 364 |
0 |
| case 1: |
| 365 |
0 |
▶ | if (header.datatypecode != 1 && header.datatypecode != 9) |
Branch: 2 branches
| Branch | True | False | Total |
| if (header.datatypecode != 1 && header.datatypecode != 9) |
0 |
0 |
0 |
| if (header.datatypecode != 1 && header.datatypecode != 9) |
0 |
0 |
0 |
|
| 366 |
0 |
| return 0; /* only get a color map on a color mapped image */ |
| 367 |
|
| case 0: |
| 368 |
18 |
| break; |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
18 |
▶ | switch (header.colourmapdepth) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (header.colourmapdepth) { |
0 |
18 |
18 |
|
| 372 |
0 |
| default: |
| 373 |
0 |
| return 0; |
| 374 |
18 |
| case 0: /* can be 0 if no colour map */ |
| 375 |
|
| case 15: |
| 376 |
|
| case 16: |
| 377 |
|
| case 24: |
| 378 |
|
| case 32: |
| 379 |
18 |
| break; |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
18 |
| return 1; |
| 383 |
|
| } |
| 384 |
|
| |
| 385 |
|
| |
| 386 |
|
| /* |
| 387 |
|
| =item tga_header_pack(header, headbuf) |
| 388 |
|
| |
| 389 |
|
| Packs header structure into buffer for writing. |
| 390 |
|
| |
| 391 |
|
| header - header structure |
| 392 |
|
| headbuf - buffer to pack into |
| 393 |
|
| |
| 394 |
|
| =cut |
| 395 |
|
| */ |
| 396 |
|
| |
| 397 |
|
| static |
| 398 |
|
| void |
| 399 |
24 |
| tga_header_pack(tga_header *header, unsigned char headbuf[18]) { |
| 400 |
24 |
| headbuf[0] = header->idlength; |
| 401 |
24 |
| headbuf[1] = header->colourmaptype; |
| 402 |
24 |
| headbuf[2] = header->datatypecode; |
| 403 |
24 |
| headbuf[3] = header->colourmaporigin & 0xff; |
| 404 |
24 |
| headbuf[4] = header->colourmaporigin >> 8; |
| 405 |
24 |
| headbuf[5] = header->colourmaplength & 0xff; |
| 406 |
24 |
| headbuf[6] = header->colourmaplength >> 8; |
| 407 |
24 |
| headbuf[7] = header->colourmapdepth; |
| 408 |
24 |
| headbuf[8] = header->x_origin & 0xff; |
| 409 |
24 |
| headbuf[9] = header->x_origin >> 8; |
| 410 |
24 |
| headbuf[10] = header->y_origin & 0xff; |
| 411 |
24 |
| headbuf[11] = header->y_origin >> 8; |
| 412 |
24 |
| headbuf[12] = header->width & 0xff; |
| 413 |
24 |
| headbuf[13] = header->width >> 8; |
| 414 |
24 |
| headbuf[14] = header->height & 0xff; |
| 415 |
24 |
| headbuf[15] = header->height >> 8; |
| 416 |
24 |
| headbuf[16] = header->bitsperpixel; |
| 417 |
24 |
| headbuf[17] = header->imagedescriptor; |
| 418 |
24 |
| } |
| 419 |
|
| |
| 420 |
|
| |
| 421 |
|
| /* |
| 422 |
|
| =item tga_source_read(s, buf, pixels) |
| 423 |
|
| |
| 424 |
|
| Reads pixel number of pixels from source s into buffer buf. Takes |
| 425 |
|
| care of decompressing the stream if needed. |
| 426 |
|
| |
| 427 |
|
| s - data source |
| 428 |
|
| buf - destination buffer |
| 429 |
|
| pixels - number of pixels to put into buffer |
| 430 |
|
| |
| 431 |
|
| =cut |
| 432 |
|
| */ |
| 433 |
|
| |
| 434 |
|
| static |
| 435 |
|
| int |
| 436 |
108789 |
| tga_source_read(tga_source *s, unsigned char *buf, size_t pixels) { |
| 437 |
108789 |
| size_t cp = 0; |
| 438 |
|
| size_t j, k; |
| 439 |
108789 |
▶ | if (!s->compressed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->compressed) { |
900 |
107889 |
108789 |
|
| 440 |
900 |
| if ((size_t)i_io_read(s->ig, buf, pixels*s->bytepp) |
| 441 |
900 |
▶ | != pixels*s->bytepp) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| != pixels*s->bytepp) return 0; |
0 |
900 |
900 |
|
| 442 |
900 |
| return 1; |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
478928 |
▶ | while(cp < pixels) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(cp < pixels) { |
263150 |
107889 |
371039 |
|
| 446 |
|
| size_t ml; |
| 447 |
263150 |
▶ | if (s->len == 0) s->state = NoInit; |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->len == 0) s->state = NoInit; |
131575 |
131575 |
263150 |
|
| 448 |
263150 |
| switch (s->state) { |
| 449 |
131575 |
| case NoInit: |
| 450 |
131575 |
▶ | if (i_io_read(s->ig, &s->hdr, 1) != 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(s->ig, &s->hdr, 1) != 1) return 0; |
0 |
131575 |
131575 |
|
| 451 |
|
| |
| 452 |
131575 |
| s->len = (s->hdr &~(1<<7))+1; |
| 453 |
131575 |
▶ | s->state = (s->hdr & (1<<7)) ? Rle : Raw; |
Branch: 1 branch
| Branch | True | False | Total |
| s->state = (s->hdr & (1<<7)) ? Rle : Raw; |
21876 |
109699 |
131575 |
|
| 454 |
|
| { |
| 455 |
|
| /* |
| 456 |
|
| static cnt = 0; |
| 457 |
|
| printf("%04d %s: %d\n", cnt++, s->state==Rle?"RLE":"RAW", s->len); |
| 458 |
|
| */ |
| 459 |
|
| } |
| 460 |
131575 |
▶ | if (s->state == Rle |
Branch: 1 branch
| Branch | True | False | Total |
| if (s->state == Rle |
21876 |
109699 |
131575 |
|
| 461 |
21876 |
▶ | && (size_t)i_io_read(s->ig, s->cval, s->bytepp) != s->bytepp) { |
Branch: 1 branch
| Branch | True | False | Total |
| && (size_t)i_io_read(s->ig, s->cval, s->bytepp) != s->bytepp) { |
0 |
21876 |
21876 |
|
| 462 |
0 |
| return 0; |
| 463 |
|
| } |
| 464 |
|
| |
| 465 |
131575 |
| break; |
| 466 |
21876 |
| case Rle: |
| 467 |
21876 |
| ml = i_min(s->len, pixels-cp); |
| 468 |
1113728 |
▶ | for(k=0; k<ml; k++) for(j=0; j < s->bytepp; j++) |
Branch: 2 branches
| Branch | True | False | Total |
| for(k=0; k<ml; k++) for(j=0; j < s->bytepp; j++) |
796713 |
295139 |
1091852 |
| for(k=0; k<ml; k++) for(j=0; j < s->bytepp; j++) |
295139 |
21876 |
317015 |
|
| 469 |
796713 |
| buf[(cp+k)*s->bytepp+j] = s->cval[j]; |
| 470 |
21876 |
| cp += ml; |
| 471 |
21876 |
| s->len -= ml; |
| 472 |
21876 |
| break; |
| 473 |
109699 |
| case Raw: |
| 474 |
109699 |
| ml = i_min(s->len, pixels-cp); |
| 475 |
109699 |
| if ((size_t)i_io_read(s->ig, buf+cp*s->bytepp, ml*s->bytepp) |
| 476 |
109699 |
▶ | != ml*s->bytepp) { |
Branch: 1 branch
| Branch | True | False | Total |
| != ml*s->bytepp) { |
0 |
109699 |
109699 |
|
| 477 |
0 |
| return 0; |
| 478 |
|
| } |
| 479 |
109699 |
| cp += ml; |
| 480 |
109699 |
| s->len -= ml; |
| 481 |
109699 |
| break; |
| 482 |
|
| } |
| 483 |
|
| } |
| 484 |
107889 |
| return 1; |
| 485 |
|
| } |
| 486 |
|
| |
| 487 |
|
| |
| 488 |
|
| |
| 489 |
|
| |
| 490 |
|
| /* |
| 491 |
|
| =item tga_dest_write(s, buf, pixels) |
| 492 |
|
| |
| 493 |
|
| Writes pixels from buf to destination s. Takes care of compressing if the |
| 494 |
|
| destination is compressed. |
| 495 |
|
| |
| 496 |
|
| s - data destination |
| 497 |
|
| buf - source buffer |
| 498 |
|
| pixels - number of pixels to put write to destination |
| 499 |
|
| |
| 500 |
|
| =cut |
| 501 |
|
| */ |
| 502 |
|
| |
| 503 |
|
| static |
| 504 |
|
| int |
| 505 |
109217 |
| tga_dest_write(tga_dest *s, unsigned char *buf, size_t pixels) { |
| 506 |
109217 |
| size_t cp = 0; |
| 507 |
|
| |
| 508 |
109217 |
▶ | if (!s->compressed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s->compressed) { |
150 |
109067 |
109217 |
|
| 509 |
150 |
| if ((size_t)i_io_write(s->ig, buf, pixels*s->bytepp) |
| 510 |
150 |
▶ | != pixels*s->bytepp) { |
Branch: 1 branch
| Branch | True | False | Total |
| != pixels*s->bytepp) { |
0 |
150 |
150 |
|
| 511 |
0 |
| return 0; |
| 512 |
|
| } |
| 513 |
150 |
| return 1; |
| 514 |
|
| } |
| 515 |
|
| |
| 516 |
135590 |
▶ | while(cp < pixels) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(cp < pixels) { |
133018 |
2572 |
135590 |
|
| 517 |
|
| int tlen; |
| 518 |
133018 |
| int nxtrip = find_repeat(buf+cp*s->bytepp, pixels-cp, s->bytepp); |
| 519 |
133018 |
▶ | tlen = (nxtrip == -1) ? (int)(pixels-cp) : nxtrip; |
Branch: 1 branch
| Branch | True | False | Total |
| tlen = (nxtrip == -1) ? (int)(pixels-cp) : nxtrip; |
106495 |
26523 |
133018 |
|
| 520 |
246723 |
▶ | while(tlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while(tlen) { |
113705 |
133018 |
246723 |
|
| 521 |
113705 |
| unsigned char clen = (tlen>128) ? 128 : tlen; |
| 522 |
113705 |
| clen--; |
| 523 |
113705 |
▶ | if (i_io_write(s->ig, &clen, 1) != 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(s->ig, &clen, 1) != 1) return 0; |
0 |
113705 |
113705 |
|
| 524 |
113705 |
| clen++; |
| 525 |
113705 |
▶ | if (i_io_write(s->ig, buf+cp*s->bytepp, clen*s->bytepp) != clen*s->bytepp) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(s->ig, buf+cp*s->bytepp, clen*s->bytepp) != clen*s->bytepp) return 0; |
0 |
113705 |
113705 |
|
| 526 |
113705 |
| tlen -= clen; |
| 527 |
113705 |
| cp += clen; |
| 528 |
|
| } |
| 529 |
133018 |
▶ | if (cp >= pixels) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp >= pixels) break; |
106495 |
26523 |
133018 |
|
| 530 |
26523 |
| tlen = find_span(buf+cp*s->bytepp, pixels-cp, s->bytepp); |
| 531 |
26523 |
▶ | if (tlen <3) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tlen <3) continue; |
0 |
26523 |
26523 |
|
| 532 |
53996 |
▶ | while (tlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (tlen) { |
27473 |
26523 |
53996 |
|
| 533 |
27473 |
| unsigned char clen = (tlen>128) ? 128 : tlen; |
| 534 |
27473 |
| clen = (clen - 1) | 0x80; |
| 535 |
27473 |
▶ | if (i_io_write(s->ig, &clen, 1) != 1) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(s->ig, &clen, 1) != 1) return 0; |
0 |
27473 |
27473 |
|
| 536 |
27473 |
| clen = (clen & ~0x80) + 1; |
| 537 |
27473 |
▶ | if (i_io_write(s->ig, buf+cp*s->bytepp, s->bytepp) != s->bytepp) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(s->ig, buf+cp*s->bytepp, s->bytepp) != s->bytepp) return 0; |
0 |
27473 |
27473 |
|
| 538 |
27473 |
| tlen -= clen; |
| 539 |
27473 |
| cp += clen; |
| 540 |
|
| } |
| 541 |
|
| } |
| 542 |
109067 |
| return 1; |
| 543 |
|
| } |
| 544 |
|
| |
| 545 |
|
| |
| 546 |
|
| |
| 547 |
|
| |
| 548 |
|
| |
| 549 |
|
| |
| 550 |
|
| /* |
| 551 |
|
| =item tga_palette_read(ig, img, bytepp, colourmaplength) |
| 552 |
|
| |
| 553 |
|
| Reads the colormap from a tga file and stores in the paletted image |
| 554 |
|
| structure. |
| 555 |
|
| |
| 556 |
|
| ig - iolayer data source |
| 557 |
|
| img - image structure |
| 558 |
|
| bytepp - bytes per pixel |
| 559 |
|
| colourmaplength - number of colours in colourmap |
| 560 |
|
| |
| 561 |
|
| =cut |
| 562 |
|
| */ |
| 563 |
|
| |
| 564 |
|
| static |
| 565 |
|
| int |
| 566 |
5 |
| tga_palette_read(io_glue *ig, i_img *img, int bytepp, |
| 567 |
|
| unsigned colourmaporigin, unsigned colourmaplength) { |
| 568 |
|
| unsigned i; |
| 569 |
|
| size_t palbsize; |
| 570 |
|
| unsigned char *palbuf; |
| 571 |
|
| i_color val; |
| 572 |
5 |
| unsigned colourmapusable = colourmaplength; |
| 573 |
|
| |
| 574 |
5 |
▶ | if (colourmaporigin > 255) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (colourmaporigin > 255) { |
0 |
5 |
5 |
|
| 575 |
0 |
| i_push_errorf(0, "colourmaporigin %u not supported", colourmaporigin); |
| 576 |
0 |
| return 0; |
| 577 |
|
| } |
| 578 |
5 |
▶ | if (colourmaplength > 256 |
Branch: 1 branch
| Branch | True | False | Total |
| if (colourmaplength > 256 |
4 |
1 |
5 |
|
| 579 |
4 |
▶ | || colourmaporigin + colourmaplength > 256) { |
Branch: 1 branch
| Branch | True | False | Total |
| || colourmaporigin + colourmaplength > 256) { |
0 |
4 |
4 |
|
| 580 |
1 |
| colourmapusable = 256U - colourmaporigin; |
| 581 |
|
| } |
| 582 |
|
| |
| 583 |
5 |
| palbsize = (size_t)colourmaplength * bytepp; |
| 584 |
5 |
| palbuf = mymalloc(palbsize); |
| 585 |
|
| |
| 586 |
5 |
▶ | if ((size_t)i_io_read(ig, palbuf, palbsize) != palbsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)i_io_read(ig, palbuf, palbsize) != palbsize) { |
2 |
3 |
5 |
|
| 587 |
2 |
| myfree(palbuf); |
| 588 |
2 |
| i_push_error(errno, "could not read targa colormap"); |
| 589 |
2 |
| return 0; |
| 590 |
|
| } |
| 591 |
|
| |
| 592 |
|
| /* populate the palette of the new image */ |
| 593 |
233 |
▶ | for(i=0; i<colourmapusable; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<colourmapusable; i++) { |
230 |
3 |
233 |
|
| 594 |
230 |
| color_unpack(palbuf+i*bytepp, bytepp, &val); |
| 595 |
230 |
▶ | i_addcolors(img, &val, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| i_addcolors(img, &val, 1); |
230 |
0 |
230 |
|
| 596 |
|
| } |
| 597 |
|
| /* discard anything from 256 to colourmaplength. |
| 598 |
|
| previously that would happen from i_addcolors() failing |
| 599 |
|
| */ |
| 600 |
3 |
| myfree(palbuf); |
| 601 |
3 |
| return 1; |
| 602 |
|
| } |
| 603 |
|
| |
| 604 |
|
| |
| 605 |
|
| /* |
| 606 |
|
| =item tga_palette_write(ig, img, bitspp, colourmaplength) |
| 607 |
|
| |
| 608 |
|
| Stores the colormap of an image in the destination ig. |
| 609 |
|
| |
| 610 |
|
| ig - iolayer data source |
| 611 |
|
| img - image structure |
| 612 |
|
| bitspp - bits per pixel in colourmap |
| 613 |
|
| colourmaplength - number of colours in colourmap |
| 614 |
|
| |
| 615 |
|
| =cut |
| 616 |
|
| */ |
| 617 |
|
| |
| 618 |
|
| static |
| 619 |
|
| int |
| 620 |
7 |
| tga_palette_write(io_glue *ig, i_img *img, int bitspp, int colourmaplength) { |
| 621 |
|
| int i; |
| 622 |
7 |
| size_t bytepp = bpp_to_bytes(bitspp); |
| 623 |
7 |
▶ | size_t palbsize = i_colorcount(img)*bytepp; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t palbsize = i_colorcount(img)*bytepp; |
7 |
0 |
7 |
|
| 624 |
7 |
| unsigned char *palbuf = mymalloc(palbsize); |
| 625 |
|
| |
| 626 |
1101 |
▶ | for(i=0; i<colourmaplength; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<colourmaplength; i++) { |
1094 |
7 |
1101 |
|
| 627 |
|
| i_color val; |
| 628 |
1094 |
▶ | i_getcolors(img, i, &val, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| i_getcolors(img, i, &val, 1); |
1094 |
0 |
1094 |
|
| 629 |
1094 |
| color_pack(palbuf+i*bytepp, bitspp, &val); |
| 630 |
|
| } |
| 631 |
|
| |
| 632 |
7 |
▶ | if ((size_t)i_io_write(ig, palbuf, palbsize) != palbsize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)i_io_write(ig, palbuf, palbsize) != palbsize) { |
0 |
7 |
7 |
|
| 633 |
0 |
| i_push_error(errno, "could not write targa colormap"); |
| 634 |
0 |
| return 0; |
| 635 |
|
| } |
| 636 |
7 |
| myfree(palbuf); |
| 637 |
7 |
| return 1; |
| 638 |
|
| } |
| 639 |
|
| |
| 640 |
|
| |
| 641 |
|
| |
| 642 |
|
| /* |
| 643 |
|
| =item i_readtga_wiol(ig, length) |
| 644 |
|
| |
| 645 |
|
| Read in an image from the iolayer data source and return the image structure to it. |
| 646 |
|
| Returns NULL on error. |
| 647 |
|
| |
| 648 |
|
| ig - io_glue object |
| 649 |
|
| length - maximum length to read from data source, before closing it -1 |
| 650 |
|
| signifies no limit. |
| 651 |
|
| |
| 652 |
|
| =cut |
| 653 |
|
| */ |
| 654 |
|
| |
| 655 |
|
| i_img * |
| 656 |
28 |
| i_readtga_wiol(io_glue *ig, int length) { |
| 657 |
28 |
| i_img* img = NULL; |
| 658 |
|
| int x, y; |
| 659 |
|
| int width, height, channels; |
| 660 |
|
| int mapped; |
| 661 |
|
| char idbuf[IDSTRING_MAX]; |
| 662 |
|
| |
| 663 |
|
| tga_source src; |
| 664 |
|
| tga_header header; |
| 665 |
|
| unsigned char headbuf[18]; |
| 666 |
|
| unsigned char *databuf; |
| 667 |
|
| |
| 668 |
28 |
| i_color *linebuf = NULL; |
| 669 |
28 |
| i_clear_error(); |
| 670 |
|
| |
| 671 |
28 |
| mm_log((1,"i_readtga(ig %p, length %d)\n", ig, length)); |
| 672 |
|
| |
| 673 |
28 |
▶ | if (i_io_read(ig, &headbuf, 18) != 18) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, &headbuf, 18) != 18) { |
0 |
28 |
28 |
|
| 674 |
0 |
| i_push_error(errno, "could not read targa header"); |
| 675 |
0 |
| return NULL; |
| 676 |
|
| } |
| 677 |
|
| |
| 678 |
28 |
| tga_header_unpack(&header, headbuf); |
| 679 |
|
| |
| 680 |
28 |
| mm_log((1,"Id length: %d\n",header.idlength)); |
| 681 |
28 |
| mm_log((1,"Colour map type: %d\n",header.colourmaptype)); |
| 682 |
28 |
| mm_log((1,"Image type: %d\n",header.datatypecode)); |
| 683 |
28 |
| mm_log((1,"Colour map offset: %d\n",header.colourmaporigin)); |
| 684 |
28 |
| mm_log((1,"Colour map length: %d\n",header.colourmaplength)); |
| 685 |
28 |
| mm_log((1,"Colour map depth: %d\n",header.colourmapdepth)); |
| 686 |
28 |
| mm_log((1,"X origin: %d\n",header.x_origin)); |
| 687 |
28 |
| mm_log((1,"Y origin: %d\n",header.y_origin)); |
| 688 |
28 |
| mm_log((1,"Width: %d\n",header.width)); |
| 689 |
28 |
| mm_log((1,"Height: %d\n",header.height)); |
| 690 |
28 |
| mm_log((1,"Bits per pixel: %d\n",header.bitsperpixel)); |
| 691 |
28 |
| mm_log((1,"Descriptor: %d\n",header.imagedescriptor)); |
| 692 |
|
| |
| 693 |
28 |
▶ | if (header.idlength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header.idlength) { |
4 |
24 |
28 |
|
| 694 |
|
| /* max of 256, so this is safe */ |
| 695 |
4 |
▶ | if (i_io_read(ig, idbuf, header.idlength) != header.idlength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_read(ig, idbuf, header.idlength) != header.idlength) { |
1 |
3 |
4 |
|
| 696 |
1 |
| i_push_error(errno, "short read on targa idstring"); |
| 697 |
1 |
| return NULL; |
| 698 |
|
| } |
| 699 |
|
| } |
| 700 |
|
| |
| 701 |
27 |
| width = header.width; |
| 702 |
27 |
| height = header.height; |
| 703 |
|
| |
| 704 |
|
| |
| 705 |
|
| /* Set tags here */ |
| 706 |
|
| |
| 707 |
27 |
| switch (header.datatypecode) { |
| 708 |
0 |
| case 0: /* No data in image */ |
| 709 |
0 |
| i_push_error(0, "Targa image contains no image data"); |
| 710 |
0 |
| return NULL; |
| 711 |
|
| break; |
| 712 |
5 |
| case 1: /* Uncompressed, color-mapped images */ |
| 713 |
|
| case 9: /* Compressed, color-mapped images */ |
| 714 |
|
| case 3: /* Uncompressed, grayscale images */ |
| 715 |
|
| case 11: /* Compressed, grayscale images */ |
| 716 |
5 |
▶ | if (header.bitsperpixel != 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header.bitsperpixel != 8) { |
0 |
5 |
5 |
|
| 717 |
0 |
| i_push_error(0, "Targa: mapped/grayscale image's bpp is not 8, unsupported."); |
| 718 |
0 |
| return NULL; |
| 719 |
|
| } |
| 720 |
5 |
| src.bytepp = 1; |
| 721 |
5 |
| break; |
| 722 |
22 |
| case 2: /* Uncompressed, rgb images */ |
| 723 |
|
| case 10: /* Compressed, rgb images */ |
| 724 |
22 |
▶ | if ((src.bytepp = bpp_to_bytes(header.bitsperpixel))) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((src.bytepp = bpp_to_bytes(header.bitsperpixel))) |
22 |
0 |
22 |
|
| 725 |
22 |
| break; |
| 726 |
0 |
| i_push_error(0, "Targa: direct color image's bpp is not 15/16/24/32 - unsupported."); |
| 727 |
0 |
| return NULL; |
| 728 |
|
| break; |
| 729 |
0 |
| case 32: /* Compressed color-mapped, Huffman, Delta and runlength */ |
| 730 |
|
| case 33: /* Compressed color-mapped, Huffman, Delta and runlength */ |
| 731 |
0 |
| i_push_error(0, "Unsupported Targa (Huffman/delta/rle/quadtree) subformat is not supported"); |
| 732 |
0 |
| return NULL; |
| 733 |
|
| break; |
| 734 |
0 |
| default: /* All others which we don't know which might be */ |
| 735 |
0 |
| i_push_error(0, "Unknown targa format"); |
| 736 |
0 |
| return NULL; |
| 737 |
|
| break; |
| 738 |
|
| } |
| 739 |
|
| |
| 740 |
27 |
| src.state = NoInit; |
| 741 |
27 |
| src.len = 0; |
| 742 |
27 |
| src.ig = ig; |
| 743 |
27 |
| src.compressed = !!(header.datatypecode & (1<<3)); |
| 744 |
|
| |
| 745 |
|
| /* Determine number of channels */ |
| 746 |
|
| |
| 747 |
27 |
| mapped = 1; |
| 748 |
27 |
| switch (header.datatypecode) { |
| 749 |
22 |
| case 2: /* Uncompressed, rgb images */ /* FALLTHROUGH */ |
| 750 |
|
| case 10: /* Compressed, rgb images */ /* FALLTHROUGH */ |
| 751 |
22 |
| mapped = 0; |
| 752 |
|
| /* FALLTHROUGH */ |
| 753 |
27 |
| case 1: /* Uncompressed, color-mapped images */ |
| 754 |
|
| case 9: /* Compressed, color-mapped images */ |
| 755 |
27 |
▶ | if ((channels = bpp_to_channels(mapped ? |
Branch: 1 branch
| Branch | True | False | Total |
| if ((channels = bpp_to_channels(mapped ? |
27 |
0 |
27 |
|
| 756 |
5 |
| header.colourmapdepth : |
| 757 |
22 |
| header.bitsperpixel, |
| 758 |
54 |
▶ | header.imagedescriptor & 0xF))) break; |
Branch: 1 branch
| Branch | True | False | Total |
| header.imagedescriptor & 0xF))) break; |
5 |
22 |
27 |
|
| 759 |
0 |
| i_push_error(0, "Targa Image has none of 15/16/24/32 pixel layout"); |
| 760 |
0 |
| return NULL; |
| 761 |
|
| break; |
| 762 |
0 |
| case 3: /* Uncompressed, grayscale images */ /* FALLTHROUGH */ |
| 763 |
|
| case 11: /* Compressed, grayscale images */ |
| 764 |
0 |
| mapped = 0; |
| 765 |
0 |
| channels = 1; |
| 766 |
0 |
| break; |
| 767 |
0 |
| default: |
| 768 |
0 |
| i_push_error(0, "invalid or unsupported datatype code"); |
| 769 |
0 |
| return NULL; |
| 770 |
|
| } |
| 771 |
|
| |
| 772 |
27 |
▶ | if (!i_int_check_image_file_limits(width, height, channels, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!i_int_check_image_file_limits(width, height, channels, |
3 |
24 |
27 |
|
| 773 |
|
| sizeof(i_sample_t))) { |
| 774 |
3 |
| mm_log((1, "i_readtga_wiol: image size exceeds limits\n")); |
| 775 |
3 |
| return NULL; |
| 776 |
|
| } |
| 777 |
|
| |
| 778 |
24 |
| img = mapped ? |
| 779 |
24 |
▶ | i_img_pal_new(width, height, channels, 256) : |
Branch: 1 branch
| Branch | True | False | Total |
| i_img_pal_new(width, height, channels, 256) : |
5 |
19 |
24 |
|
| 780 |
19 |
| i_img_8_new(width, height, channels); |
| 781 |
|
| |
| 782 |
24 |
▶ | if (!img) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!img) { |
0 |
24 |
24 |
|
| 783 |
0 |
| return NULL; |
| 784 |
|
| } |
| 785 |
|
| |
| 786 |
24 |
▶ | if (header.idlength) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (header.idlength) { |
3 |
21 |
24 |
|
| 787 |
3 |
| i_tags_add(&img->tags, "tga_idstring", 0, idbuf, header.idlength, 0); |
| 788 |
|
| } |
| 789 |
|
| |
| 790 |
29 |
| if (mapped && |
| 791 |
5 |
| !tga_palette_read(ig, |
| 792 |
|
| img, |
| 793 |
5 |
| bpp_to_bytes(header.colourmapdepth), |
| 794 |
|
| header.colourmaporigin, |
| 795 |
|
| header.colourmaplength) |
| 796 |
|
| ) { |
| 797 |
|
| /* tga_palette_read() sets a message */ |
| 798 |
2 |
▶ | if (img) i_img_destroy(img); |
Branch: 1 branch
| Branch | True | False | Total |
| if (img) i_img_destroy(img); |
2 |
0 |
2 |
|
| 799 |
2 |
| return NULL; |
| 800 |
|
| } |
| 801 |
|
| |
| 802 |
|
| /* Allocate buffers */ |
| 803 |
|
| /* width is max 0xffff, src.bytepp is max 4, so this is safe */ |
| 804 |
22 |
| databuf = mymalloc(width*src.bytepp); |
| 805 |
|
| /* similarly here */ |
| 806 |
22 |
▶ | if (!mapped) linebuf = mymalloc(width*sizeof(i_color)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!mapped) linebuf = mymalloc(width*sizeof(i_color)); |
19 |
3 |
22 |
|
| 807 |
|
| |
| 808 |
108811 |
▶ | for(y=0; y<height; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=0; y<height; y++) { |
108789 |
22 |
108811 |
|
| 809 |
108789 |
▶ | if (!tga_source_read(&src, databuf, width)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!tga_source_read(&src, databuf, width)) { |
0 |
108789 |
108789 |
|
| 810 |
0 |
| i_push_error(errno, "read for targa data failed"); |
| 811 |
0 |
▶ | if (linebuf) myfree(linebuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (linebuf) myfree(linebuf); |
0 |
0 |
0 |
|
| 812 |
0 |
| myfree(databuf); |
| 813 |
0 |
▶ | if (img) i_img_destroy(img); |
Branch: 1 branch
| Branch | True | False | Total |
| if (img) i_img_destroy(img); |
0 |
0 |
0 |
|
| 814 |
0 |
| return NULL; |
| 815 |
|
| } |
| 816 |
108789 |
▶ | if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin; |
Branch: 3 branches
| Branch | True | False | Total |
| if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin; |
450 |
108339 |
108789 |
| if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin; |
0 |
450 |
450 |
| if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin; |
0 |
0 |
0 |
|
| 817 |
108789 |
▶ | if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf); |
Branch: 3 branches
| Branch | True | False | Total |
| if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf); |
450 |
108339 |
108789 |
| if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf); |
450 |
0 |
450 |
| if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf); |
0 |
450 |
450 |
|
| 818 |
|
| else { |
| 819 |
592133 |
▶ | for(x=0; x<width; x++) color_unpack(databuf+x*src.bytepp, src.bytepp, linebuf+x); |
Branch: 1 branch
| Branch | True | False | Total |
| for(x=0; x<width; x++) color_unpack(databuf+x*src.bytepp, src.bytepp, linebuf+x); |
483794 |
108339 |
592133 |
|
| 820 |
108339 |
▶ | i_plin(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, linebuf); |
Branch: 1 branch
| Branch | True | False | Total |
| i_plin(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, linebuf); |
0 |
108339 |
108339 |
|
| 821 |
|
| } |
| 822 |
|
| } |
| 823 |
22 |
| myfree(databuf); |
| 824 |
22 |
▶ | if (linebuf) myfree(linebuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (linebuf) myfree(linebuf); |
19 |
3 |
22 |
|
| 825 |
|
| |
| 826 |
22 |
| i_tags_add(&img->tags, "i_format", 0, "tga", -1, 0); |
| 827 |
22 |
▶ | i_tags_addn(&img->tags, "tga_bitspp", 0, mapped?header.colourmapdepth:header.bitsperpixel); |
Branch: 1 branch
| Branch | True | False | Total |
| i_tags_addn(&img->tags, "tga_bitspp", 0, mapped?header.colourmapdepth:header.bitsperpixel); |
3 |
19 |
22 |
|
| 828 |
22 |
▶ | if (src.compressed) i_tags_addn(&img->tags, "compressed", 0, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (src.compressed) i_tags_addn(&img->tags, "compressed", 0, 1); |
16 |
6 |
22 |
|
| 829 |
22 |
| return img; |
| 830 |
|
| } |
| 831 |
|
| |
| 832 |
|
| |
| 833 |
|
| |
| 834 |
|
| /* |
| 835 |
|
| =item i_writetga_wiol(img, ig) |
| 836 |
|
| |
| 837 |
|
| Writes an image in targa format. Returns 0 on error. |
| 838 |
|
| |
| 839 |
|
| img - image to store |
| 840 |
|
| ig - io_glue object |
| 841 |
|
| |
| 842 |
|
| =cut |
| 843 |
|
| */ |
| 844 |
|
| |
| 845 |
|
| undef_int |
| 846 |
27 |
| i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen) { |
| 847 |
|
| tga_header header; |
| 848 |
|
| tga_dest dest; |
| 849 |
|
| unsigned char headbuf[18]; |
| 850 |
|
| unsigned int bitspp; |
| 851 |
27 |
| unsigned int attr_bits = 0; |
| 852 |
|
| |
| 853 |
|
| int mapped; |
| 854 |
|
| |
| 855 |
|
| /* parameters */ |
| 856 |
|
| |
| 857 |
|
| /* |
| 858 |
|
| int compress = 1; |
| 859 |
|
| char *idstring = "testing"; |
| 860 |
|
| int wierdpack = 0; |
| 861 |
|
| */ |
| 862 |
|
| |
| 863 |
27 |
| idlen = strlen(idstring); |
| 864 |
27 |
| mapped = img->type == i_palette_type; |
| 865 |
|
| |
| 866 |
27 |
| mm_log((1,"i_writetga_wiol(img %p, ig %p, idstring %p, idlen %ld, wierdpack %d, compress %d)\n", |
| 867 |
|
| img, ig, idstring, (long)idlen, wierdpack, compress)); |
| 868 |
27 |
| mm_log((1, "virtual %d, paletted %d\n", i_img_virtual(img), mapped)); |
| 869 |
27 |
| mm_log((1, "channels %d\n", img->channels)); |
| 870 |
|
| |
| 871 |
27 |
| i_clear_error(); |
| 872 |
|
| |
| 873 |
27 |
▶ | if (img->xsize > TGA_MAX_DIM || img->ysize > TGA_MAX_DIM) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (img->xsize > TGA_MAX_DIM || img->ysize > TGA_MAX_DIM) { |
26 |
1 |
27 |
| if (img->xsize > TGA_MAX_DIM || img->ysize > TGA_MAX_DIM) { |
1 |
25 |
26 |
|
| 874 |
2 |
| i_push_error(0, "image too large for TGA"); |
| 875 |
2 |
| return 0; |
| 876 |
|
| } |
| 877 |
|
| |
| 878 |
25 |
| switch (img->channels) { |
| 879 |
0 |
| case 1: |
| 880 |
0 |
| bitspp = 8; |
| 881 |
0 |
▶ | if (wierdpack) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wierdpack) { |
0 |
0 |
0 |
|
| 882 |
0 |
| mm_log((1,"wierdpack option ignored for 1 channel images\n")); |
| 883 |
0 |
| wierdpack=0; |
| 884 |
|
| } |
| 885 |
0 |
| break; |
| 886 |
1 |
| case 2: |
| 887 |
1 |
| i_push_error(0, "Cannot store 2 channel image in targa format"); |
| 888 |
1 |
| return 0; |
| 889 |
|
| break; |
| 890 |
23 |
| case 3: |
| 891 |
23 |
▶ | bitspp = wierdpack ? 15 : 24; |
Branch: 1 branch
| Branch | True | False | Total |
| bitspp = wierdpack ? 15 : 24; |
4 |
19 |
23 |
|
| 892 |
23 |
| break; |
| 893 |
1 |
| case 4: |
| 894 |
1 |
▶ | bitspp = wierdpack ? 16 : 32; |
Branch: 1 branch
| Branch | True | False | Total |
| bitspp = wierdpack ? 16 : 32; |
1 |
0 |
1 |
|
| 895 |
1 |
▶ | attr_bits = wierdpack ? 1 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| attr_bits = wierdpack ? 1 : 8; |
1 |
0 |
1 |
|
| 896 |
1 |
| break; |
| 897 |
0 |
| default: |
| 898 |
0 |
| i_push_error(0, "Targa only handles 1,3 and 4 channel images."); |
| 899 |
0 |
| return 0; |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
24 |
| header.idlength = idlen; |
| 903 |
24 |
| header.colourmaptype = mapped ? 1 : 0; |
| 904 |
24 |
▶ | header.datatypecode = mapped ? 1 : img->channels == 1 ? 3 : 2; |
Branch: 2 branches
| Branch | True | False | Total |
| header.datatypecode = mapped ? 1 : img->channels == 1 ? 3 : 2; |
17 |
7 |
24 |
| header.datatypecode = mapped ? 1 : img->channels == 1 ? 3 : 2; |
0 |
17 |
17 |
|
| 905 |
24 |
▶ | header.datatypecode += compress ? 8 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| header.datatypecode += compress ? 8 : 0; |
21 |
3 |
24 |
|
| 906 |
24 |
| mm_log((1, "datatypecode %d\n", header.datatypecode)); |
| 907 |
24 |
| header.colourmaporigin = 0; |
| 908 |
24 |
▶ | header.colourmaplength = mapped ? i_colorcount(img) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| header.colourmaplength = mapped ? i_colorcount(img) : 0; |
7 |
17 |
24 |
| header.colourmaplength = mapped ? i_colorcount(img) : 0; |
7 |
0 |
7 |
|
| 909 |
24 |
▶ | header.colourmapdepth = mapped ? bitspp : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| header.colourmapdepth = mapped ? bitspp : 0; |
7 |
17 |
24 |
|
| 910 |
24 |
| header.x_origin = 0; |
| 911 |
24 |
| header.y_origin = 0; |
| 912 |
24 |
| header.width = img->xsize; |
| 913 |
24 |
| header.height = img->ysize; |
| 914 |
24 |
▶ | header.bitsperpixel = mapped ? 8 : bitspp; |
Branch: 1 branch
| Branch | True | False | Total |
| header.bitsperpixel = mapped ? 8 : bitspp; |
17 |
7 |
24 |
|
| 915 |
24 |
| header.imagedescriptor = (1<<5) | attr_bits; /* normal order instead of upside down */ |
| 916 |
|
| |
| 917 |
24 |
| tga_header_pack(&header, headbuf); |
| 918 |
|
| |
| 919 |
24 |
▶ | if (i_io_write(ig, &headbuf, sizeof(headbuf)) != sizeof(headbuf)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_write(ig, &headbuf, sizeof(headbuf)) != sizeof(headbuf)) { |
0 |
24 |
24 |
|
| 920 |
0 |
| i_push_error(errno, "could not write targa header"); |
| 921 |
0 |
| return 0; |
| 922 |
|
| } |
| 923 |
|
| |
| 924 |
24 |
▶ | if (idlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (idlen) { |
2 |
22 |
24 |
|
| 925 |
2 |
▶ | if ((size_t)i_io_write(ig, idstring, idlen) != idlen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)i_io_write(ig, idstring, idlen) != idlen) { |
0 |
2 |
2 |
|
| 926 |
0 |
| i_push_error(errno, "could not write targa idstring"); |
| 927 |
0 |
| return 0; |
| 928 |
|
| } |
| 929 |
|
| } |
| 930 |
|
| |
| 931 |
|
| /* Make this into a constructor? */ |
| 932 |
24 |
| dest.compressed = compress; |
| 933 |
24 |
▶ | dest.bytepp = mapped ? 1 : bpp_to_bytes(bitspp); |
Branch: 1 branch
| Branch | True | False | Total |
| dest.bytepp = mapped ? 1 : bpp_to_bytes(bitspp); |
17 |
7 |
24 |
|
| 934 |
24 |
| dest.ig = ig; |
| 935 |
|
| |
| 936 |
24 |
| mm_log((1, "dest.compressed = %d\n", dest.compressed)); |
| 937 |
24 |
| mm_log((1, "dest.bytepp = %d\n", dest.bytepp)); |
| 938 |
|
| |
| 939 |
24 |
▶ | if (img->type == i_palette_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (img->type == i_palette_type) { |
7 |
17 |
24 |
|
| 940 |
7 |
▶ | if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0; |
7 |
0 |
7 |
| if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0; |
0 |
7 |
7 |
|
| 941 |
|
| |
| 942 |
7 |
▶ | if (!i_img_virtual(img) && !dest.compressed) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!i_img_virtual(img) && !dest.compressed) { |
7 |
0 |
7 |
| if (!i_img_virtual(img) && !dest.compressed) { |
2 |
5 |
7 |
|
| 943 |
|
| /* FIXME: assumes image layout */ |
| 944 |
2 |
▶ | if ((size_t)i_io_write(ig, img->idata, img->bytes) != img->bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((size_t)i_io_write(ig, img->idata, img->bytes) != img->bytes) { |
0 |
2 |
2 |
|
| 945 |
0 |
| i_push_error(errno, "could not write targa image data"); |
| 946 |
0 |
| return 0; |
| 947 |
|
| } |
| 948 |
|
| } else { |
| 949 |
|
| int y; |
| 950 |
5 |
| i_palidx *vals = mymalloc(sizeof(i_palidx)*img->xsize); |
| 951 |
755 |
▶ | for(y=0; y<img->ysize; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=0; y<img->ysize; y++) { |
750 |
5 |
755 |
|
| 952 |
750 |
▶ | i_gpal(img, 0, img->xsize, y, vals); |
Branch: 1 branch
| Branch | True | False | Total |
| i_gpal(img, 0, img->xsize, y, vals); |
750 |
0 |
750 |
|
| 953 |
750 |
| tga_dest_write(&dest, vals, img->xsize); |
| 954 |
|
| } |
| 955 |
5 |
| myfree(vals); |
| 956 |
|
| } |
| 957 |
|
| } else { /* direct type */ |
| 958 |
|
| int x, y; |
| 959 |
17 |
▶ | size_t bytepp = wierdpack ? 2 : bpp_to_bytes(bitspp); |
Branch: 1 branch
| Branch | True | False | Total |
| size_t bytepp = wierdpack ? 2 : bpp_to_bytes(bitspp); |
14 |
3 |
17 |
|
| 960 |
17 |
| size_t lsize = bytepp * img->xsize; |
| 961 |
17 |
| i_color *vals = mymalloc(img->xsize*sizeof(i_color)); |
| 962 |
17 |
| unsigned char *buf = mymalloc(lsize); |
| 963 |
|
| |
| 964 |
108484 |
▶ | for(y=0; y<img->ysize; y++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for(y=0; y<img->ysize; y++) { |
108467 |
17 |
108484 |
|
| 965 |
108467 |
| i_glin(img, 0, img->xsize, y, vals); |
| 966 |
626957 |
▶ | for(x=0; x<img->xsize; x++) color_pack(buf+x*bytepp, bitspp, vals+x); |
Branch: 1 branch
| Branch | True | False | Total |
| for(x=0; x<img->xsize; x++) color_pack(buf+x*bytepp, bitspp, vals+x); |
518490 |
108467 |
626957 |
|
| 967 |
108467 |
| tga_dest_write(&dest, buf, img->xsize); |
| 968 |
|
| } |
| 969 |
17 |
| myfree(buf); |
| 970 |
17 |
| myfree(vals); |
| 971 |
|
| } |
| 972 |
|
| |
| 973 |
24 |
▶ | if (i_io_close(ig)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i_io_close(ig)) |
1 |
23 |
24 |
|
| 974 |
1 |
| return 0; |
| 975 |
|
| |
| 976 |
23 |
| return 1; |
| 977 |
|
| } |
| 978 |
|
| |
| 979 |
|
| /* |
| 980 |
|
| =back |
| 981 |
|
| |
| 982 |
|
| =head1 AUTHOR |
| 983 |
|
| |
| 984 |
|
| Arnar M. Hrafnkelsson <addi@umich.edu> |
| 985 |
|
| |
| 986 |
|
| =head1 SEE ALSO |
| 987 |
|
| |
| 988 |
|
| Imager(3) |
| 989 |
|
| |
| 990 |
|
| =cut |
| 991 |
|
| */ |