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