| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include "cdjpeg.h" |
| 21 |
|
| 22 |
#ifdef TARGA_SUPPORTED |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#ifdef HAVE_UNSIGNED_CHAR |
| 28 |
typedef unsigned char U_CHAR; |
| 29 |
#define UCH(x) ((int) (x)) |
| 30 |
#else |
| 31 |
#ifdef CHAR_IS_UNSIGNED |
| 32 |
typedef char U_CHAR; |
| 33 |
#define UCH(x) ((int) (x)) |
| 34 |
#else |
| 35 |
typedef char U_CHAR; |
| 36 |
#define UCH(x) ((int) (x) & 0xFF) |
| 37 |
#endif |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
#define ReadOK(file,buffer,len) (JFREAD(file,buffer,len) == ((size_t) (len))) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
typedef struct _tga_source_struct * tga_source_ptr; |
| 47 |
|
| 48 |
typedef struct _tga_source_struct { |
| 49 |
struct cjpeg_source_struct pub; |
| 50 |
|
| 51 |
j_compress_ptr cinfo; |
| 52 |
|
| 53 |
JSAMPARRAY colormap; |
| 54 |
|
| 55 |
jvirt_sarray_ptr whole_image; |
| 56 |
JDIMENSION current_row; |
| 57 |
|
| 58 |
|
| 59 |
JMETHOD(void, read_pixel, (tga_source_ptr sinfo)); |
| 60 |
|
| 61 |
|
| 62 |
U_CHAR tga_pixel[4]; |
| 63 |
|
| 64 |
int pixel_size; |
| 65 |
|
| 66 |
|
| 67 |
int block_count; |
| 68 |
int dup_pixel_count; |
| 69 |
|
| 70 |
|
| 71 |
JMETHOD(JDIMENSION, get_pixel_rows, (j_compress_ptr cinfo, |
| 72 |
cjpeg_source_ptr sinfo)); |
| 73 |
} tga_source_struct; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static const UINT8 c5to8bits[32] = { |
| 79 |
0, 8, 16, 25, 33, 41, 49, 58, |
| 80 |
66, 74, 82, 90, 99, 107, 115, 123, |
| 81 |
132, 140, 148, 156, 165, 173, 181, 189, |
| 82 |
197, 206, 214, 222, 230, 239, 247, 255 |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
LOCAL(int) |
| 88 |
read_byte (tga_source_ptr sinfo) |
| 89 |
|
| 90 |
{ |
| 91 |
register FILE *infile = sinfo->pub.input_file; |
| 92 |
register int c; |
| 93 |
|
| 94 |
if ((c = getc(infile)) == EOF) |
| 95 |
ERREXIT(sinfo->cinfo, JERR_INPUT_EOF); |
| 96 |
return c; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
LOCAL(void) |
| 101 |
read_colormap (tga_source_ptr sinfo, int cmaplen, int mapentrysize) |
| 102 |
|
| 103 |
{ |
| 104 |
int i; |
| 105 |
|
| 106 |
|
| 107 |
if (mapentrysize != 24) |
| 108 |
ERREXIT(sinfo->cinfo, JERR_TGA_BADCMAP); |
| 109 |
|
| 110 |
for (i = 0; i < cmaplen; i++) { |
| 111 |
sinfo->colormap[2][i] = (JSAMPLE) read_byte(sinfo); |
| 112 |
sinfo->colormap[1][i] = (JSAMPLE) read_byte(sinfo); |
| 113 |
sinfo->colormap[0][i] = (JSAMPLE) read_byte(sinfo); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
METHODDEF(void) |
| 123 |
read_non_rle_pixel (tga_source_ptr sinfo) |
| 124 |
|
| 125 |
{ |
| 126 |
register FILE *infile = sinfo->pub.input_file; |
| 127 |
register int i; |
| 128 |
|
| 129 |
for (i = 0; i < sinfo->pixel_size; i++) { |
| 130 |
sinfo->tga_pixel[i] = (U_CHAR) getc(infile); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
METHODDEF(void) |
| 136 |
read_rle_pixel (tga_source_ptr sinfo) |
| 137 |
|
| 138 |
{ |
| 139 |
register FILE *infile = sinfo->pub.input_file; |
| 140 |
register int i; |
| 141 |
|
| 142 |
|
| 143 |
if (sinfo->dup_pixel_count > 0) { |
| 144 |
sinfo->dup_pixel_count--; |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
if (--sinfo->block_count < 0) { |
| 150 |
i = read_byte(sinfo); |
| 151 |
if (i & 0x80) { |
| 152 |
sinfo->dup_pixel_count = i & 0x7F; |
| 153 |
sinfo->block_count = 0; |
| 154 |
} else { |
| 155 |
sinfo->block_count = i & 0x7F; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
for (i = 0; i < sinfo->pixel_size; i++) { |
| 161 |
sinfo->tga_pixel[i] = (U_CHAR) getc(infile); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
METHODDEF(JDIMENSION) |
| 174 |
get_8bit_gray_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 175 |
|
| 176 |
{ |
| 177 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 178 |
register JSAMPROW ptr; |
| 179 |
register JDIMENSION col; |
| 180 |
|
| 181 |
ptr = source->pub.buffer[0]; |
| 182 |
for (col = cinfo->image_width; col > 0; col--) { |
| 183 |
(*source->read_pixel) (source); |
| 184 |
*ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]); |
| 185 |
} |
| 186 |
return 1; |
| 187 |
} |
| 188 |
|
| 189 |
METHODDEF(JDIMENSION) |
| 190 |
get_8bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 191 |
|
| 192 |
{ |
| 193 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 194 |
register int t; |
| 195 |
register JSAMPROW ptr; |
| 196 |
register JDIMENSION col; |
| 197 |
register JSAMPARRAY colormap = source->colormap; |
| 198 |
|
| 199 |
ptr = source->pub.buffer[0]; |
| 200 |
for (col = cinfo->image_width; col > 0; col--) { |
| 201 |
(*source->read_pixel) (source); |
| 202 |
t = UCH(source->tga_pixel[0]); |
| 203 |
*ptr++ = colormap[0][t]; |
| 204 |
*ptr++ = colormap[1][t]; |
| 205 |
*ptr++ = colormap[2][t]; |
| 206 |
} |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
METHODDEF(JDIMENSION) |
| 211 |
get_16bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 212 |
|
| 213 |
{ |
| 214 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 215 |
register int t; |
| 216 |
register JSAMPROW ptr; |
| 217 |
register JDIMENSION col; |
| 218 |
|
| 219 |
ptr = source->pub.buffer[0]; |
| 220 |
for (col = cinfo->image_width; col > 0; col--) { |
| 221 |
(*source->read_pixel) (source); |
| 222 |
t = UCH(source->tga_pixel[0]); |
| 223 |
t += UCH(source->tga_pixel[1]) << 8; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
ptr[2] = (JSAMPLE) c5to8bits[t & 0x1F]; |
| 229 |
t >>= 5; |
| 230 |
ptr[1] = (JSAMPLE) c5to8bits[t & 0x1F]; |
| 231 |
t >>= 5; |
| 232 |
ptr[0] = (JSAMPLE) c5to8bits[t & 0x1F]; |
| 233 |
ptr += 3; |
| 234 |
} |
| 235 |
return 1; |
| 236 |
} |
| 237 |
|
| 238 |
METHODDEF(JDIMENSION) |
| 239 |
get_24bit_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 240 |
|
| 241 |
{ |
| 242 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 243 |
register JSAMPROW ptr; |
| 244 |
register JDIMENSION col; |
| 245 |
|
| 246 |
ptr = source->pub.buffer[0]; |
| 247 |
for (col = cinfo->image_width; col > 0; col--) { |
| 248 |
(*source->read_pixel) (source); |
| 249 |
*ptr++ = (JSAMPLE) UCH(source->tga_pixel[2]); |
| 250 |
*ptr++ = (JSAMPLE) UCH(source->tga_pixel[1]); |
| 251 |
*ptr++ = (JSAMPLE) UCH(source->tga_pixel[0]); |
| 252 |
} |
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
#define get_32bit_row get_24bit_row |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
METHODDEF(JDIMENSION) |
| 273 |
get_memory_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 274 |
{ |
| 275 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 276 |
JDIMENSION source_row; |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
source_row = cinfo->image_height - source->current_row - 1; |
| 282 |
|
| 283 |
|
| 284 |
source->pub.buffer = (*cinfo->mem->access_virt_sarray) |
| 285 |
((j_common_ptr) cinfo, source->whole_image, |
| 286 |
source_row, (JDIMENSION) 1, FALSE); |
| 287 |
|
| 288 |
source->current_row++; |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
METHODDEF(JDIMENSION) |
| 300 |
preload_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 301 |
{ |
| 302 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 303 |
JDIMENSION row; |
| 304 |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 305 |
|
| 306 |
|
| 307 |
for (row = 0; row < cinfo->image_height; row++) { |
| 308 |
if (progress != NULL) { |
| 309 |
progress->pub.pass_counter = (long) row; |
| 310 |
progress->pub.pass_limit = (long) cinfo->image_height; |
| 311 |
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo); |
| 312 |
} |
| 313 |
source->pub.buffer = (*cinfo->mem->access_virt_sarray) |
| 314 |
((j_common_ptr) cinfo, source->whole_image, row, (JDIMENSION) 1, TRUE); |
| 315 |
(*source->get_pixel_rows) (cinfo, sinfo); |
| 316 |
} |
| 317 |
if (progress != NULL) |
| 318 |
progress->completed_extra_passes++; |
| 319 |
|
| 320 |
|
| 321 |
source->pub.get_pixel_rows = get_memory_row; |
| 322 |
source->current_row = 0; |
| 323 |
|
| 324 |
return get_memory_row(cinfo, sinfo); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
METHODDEF(void) |
| 333 |
start_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 334 |
{ |
| 335 |
tga_source_ptr source = (tga_source_ptr) sinfo; |
| 336 |
U_CHAR targaheader[18]; |
| 337 |
int idlen, cmaptype, subtype, flags, interlace_type, components; |
| 338 |
unsigned int width, height, maplen; |
| 339 |
boolean is_bottom_up; |
| 340 |
|
| 341 |
#define GET_2B(offset) ((unsigned int) UCH(targaheader[offset]) + \ |
| 342 |
(((unsigned int) UCH(targaheader[offset+1])) << 8)) |
| 343 |
|
| 344 |
if (! ReadOK(source->pub.input_file, targaheader, 18)) |
| 345 |
ERREXIT(cinfo, JERR_INPUT_EOF); |
| 346 |
|
| 347 |
|
| 348 |
if (targaheader[16] == 15) |
| 349 |
targaheader[16] = 16; |
| 350 |
|
| 351 |
idlen = UCH(targaheader[0]); |
| 352 |
cmaptype = UCH(targaheader[1]); |
| 353 |
subtype = UCH(targaheader[2]); |
| 354 |
maplen = GET_2B(5); |
| 355 |
width = GET_2B(12); |
| 356 |
height = GET_2B(14); |
| 357 |
source->pixel_size = UCH(targaheader[16]) >> 3; |
| 358 |
flags = UCH(targaheader[17]); |
| 359 |
|
| 360 |
is_bottom_up = ((flags & 0x20) == 0); |
| 361 |
interlace_type = flags >> 6; |
| 362 |
|
| 363 |
if (cmaptype > 1 || |
| 364 |
source->pixel_size < 1 || source->pixel_size > 4 || |
| 365 |
(UCH(targaheader[16]) & 7) != 0 || |
| 366 |
interlace_type != 0) |
| 367 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 368 |
|
| 369 |
if (subtype > 8) { |
| 370 |
|
| 371 |
source->read_pixel = read_rle_pixel; |
| 372 |
source->block_count = source->dup_pixel_count = 0; |
| 373 |
subtype -= 8; |
| 374 |
} else { |
| 375 |
|
| 376 |
source->read_pixel = read_non_rle_pixel; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
components = 3; |
| 381 |
cinfo->in_color_space = JCS_RGB; |
| 382 |
|
| 383 |
switch (subtype) { |
| 384 |
case 1: |
| 385 |
if (source->pixel_size == 1 && cmaptype == 1) |
| 386 |
source->get_pixel_rows = get_8bit_row; |
| 387 |
else |
| 388 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 389 |
TRACEMS2(cinfo, 1, JTRC_TGA_MAPPED, width, height); |
| 390 |
break; |
| 391 |
case 2: |
| 392 |
switch (source->pixel_size) { |
| 393 |
case 2: |
| 394 |
source->get_pixel_rows = get_16bit_row; |
| 395 |
break; |
| 396 |
case 3: |
| 397 |
source->get_pixel_rows = get_24bit_row; |
| 398 |
break; |
| 399 |
case 4: |
| 400 |
source->get_pixel_rows = get_32bit_row; |
| 401 |
break; |
| 402 |
default: |
| 403 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 404 |
break; |
| 405 |
} |
| 406 |
TRACEMS2(cinfo, 1, JTRC_TGA, width, height); |
| 407 |
break; |
| 408 |
case 3: |
| 409 |
components = 1; |
| 410 |
cinfo->in_color_space = JCS_GRAYSCALE; |
| 411 |
if (source->pixel_size == 1) |
| 412 |
source->get_pixel_rows = get_8bit_gray_row; |
| 413 |
else |
| 414 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 415 |
TRACEMS2(cinfo, 1, JTRC_TGA_GRAY, width, height); |
| 416 |
break; |
| 417 |
default: |
| 418 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 419 |
break; |
| 420 |
} |
| 421 |
|
| 422 |
if (is_bottom_up) { |
| 423 |
|
| 424 |
source->whole_image = (*cinfo->mem->request_virt_sarray) |
| 425 |
((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE, |
| 426 |
(JDIMENSION) width * components, (JDIMENSION) height, (JDIMENSION) 1); |
| 427 |
if (cinfo->progress != NULL) { |
| 428 |
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress; |
| 429 |
progress->total_extra_passes++; |
| 430 |
} |
| 431 |
|
| 432 |
source->pub.buffer_height = 1; |
| 433 |
source->pub.get_pixel_rows = preload_image; |
| 434 |
} else { |
| 435 |
|
| 436 |
source->whole_image = NULL; |
| 437 |
source->pub.buffer = (*cinfo->mem->alloc_sarray) |
| 438 |
((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 439 |
(JDIMENSION) width * components, (JDIMENSION) 1); |
| 440 |
source->pub.buffer_height = 1; |
| 441 |
source->pub.get_pixel_rows = source->get_pixel_rows; |
| 442 |
} |
| 443 |
|
| 444 |
while (idlen--) |
| 445 |
(void) read_byte(source); |
| 446 |
|
| 447 |
if (maplen > 0) { |
| 448 |
if (maplen > 256 || GET_2B(3) != 0) |
| 449 |
ERREXIT(cinfo, JERR_TGA_BADCMAP); |
| 450 |
|
| 451 |
source->colormap = (*cinfo->mem->alloc_sarray) |
| 452 |
((j_common_ptr) cinfo, JPOOL_IMAGE, (JDIMENSION) maplen, (JDIMENSION) 3); |
| 453 |
|
| 454 |
read_colormap(source, (int) maplen, UCH(targaheader[7])); |
| 455 |
} else { |
| 456 |
if (cmaptype) |
| 457 |
ERREXIT(cinfo, JERR_TGA_BADPARMS); |
| 458 |
source->colormap = NULL; |
| 459 |
} |
| 460 |
|
| 461 |
cinfo->input_components = components; |
| 462 |
cinfo->data_precision = 8; |
| 463 |
cinfo->image_width = width; |
| 464 |
cinfo->image_height = height; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
METHODDEF(void) |
| 473 |
finish_input_tga (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 474 |
{ |
| 475 |
|
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
GLOBAL(cjpeg_source_ptr) |
| 484 |
jinit_read_targa (j_compress_ptr cinfo) |
| 485 |
{ |
| 486 |
tga_source_ptr source; |
| 487 |
|
| 488 |
|
| 489 |
source = (tga_source_ptr) |
| 490 |
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 491 |
SIZEOF(tga_source_struct)); |
| 492 |
source->cinfo = cinfo; |
| 493 |
|
| 494 |
source->pub.start_input = start_input_tga; |
| 495 |
source->pub.finish_input = finish_input_tga; |
| 496 |
|
| 497 |
return (cjpeg_source_ptr) source; |
| 498 |
} |
| 499 |
|
| 500 |
#endif |