| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#define _DEFAULT_SOURCE 1 |
| 16 |
#define _BSD_SOURCE 1 |
| 17 |
#define _XOPEN_SOURCE 500 |
| 18 |
|
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "pm_c_util.h" |
| 22 |
#include "ppm.h" |
| 23 |
#include "tga.h" |
| 24 |
#include "shhopt.h" |
| 25 |
#include "nstring.h" |
| 26 |
|
| 27 |
#define MAXCOLORS 16384 |
| 28 |
|
| 29 |
static int mapped, rlencoded; |
| 30 |
|
| 31 |
static pixel ColorMap[MAXCOLORS]; |
| 32 |
static gray AlphaMap[MAXCOLORS]; |
| 33 |
|
| 34 |
struct cmdlineInfo { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
const char *input_filename; |
| 39 |
unsigned int headerdump; |
| 40 |
const char *alpha_filename; |
| 41 |
unsigned int alpha_stdout; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
parseCommandLine(int argc, char ** argv, |
| 48 |
struct cmdlineInfo *cmdlineP) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
optEntry *option_def = malloc(100*sizeof(optEntry)); |
| 56 |
|
| 57 |
|
| 58 |
optStruct3 opt; |
| 59 |
|
| 60 |
unsigned int option_def_index; |
| 61 |
|
| 62 |
unsigned int alpha_spec; |
| 63 |
|
| 64 |
option_def_index = 0; |
| 65 |
OPTENT3(0, "headerdump", OPT_FLAG, NULL, &cmdlineP->headerdump, 0); |
| 66 |
OPTENT3(0, "debug", OPT_FLAG, NULL, &cmdlineP->headerdump, 0); |
| 67 |
OPTENT3(0, "alphaout", OPT_STRING, &cmdlineP->alpha_filename, |
| 68 |
&alpha_spec, 0); |
| 69 |
|
| 70 |
opt.opt_table = option_def; |
| 71 |
opt.short_allowed = FALSE; |
| 72 |
opt.allowNegNum = FALSE; |
| 73 |
|
| 74 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 75 |
|
| 76 |
|
| 77 |
if (argc - 1 == 0) |
| 78 |
cmdlineP->input_filename = "-"; |
| 79 |
else if (argc - 1 == 1) |
| 80 |
cmdlineP->input_filename = strdup(argv[1]); |
| 81 |
else |
| 82 |
pm_error("Too many arguments. The only argument accepted " |
| 83 |
"is the input file specification"); |
| 84 |
|
| 85 |
if (alpha_spec && |
| 86 |
streq(cmdlineP->alpha_filename, "-")) |
| 87 |
cmdlineP->alpha_stdout = 1; |
| 88 |
else |
| 89 |
cmdlineP->alpha_stdout = 0; |
| 90 |
|
| 91 |
if (!alpha_spec) |
| 92 |
cmdlineP->alpha_filename = NULL; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
static unsigned char |
| 98 |
getbyte(FILE * const ifP) { |
| 99 |
|
| 100 |
unsigned char c; |
| 101 |
|
| 102 |
if ( fread( (char*) &c, 1, 1, ifP ) != 1 ) |
| 103 |
pm_error( "EOF / read error" ); |
| 104 |
|
| 105 |
return c; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
static int RLE_count = 0, RLE_flag = 0; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static void |
| 115 |
handleRun(FILE * const ifP, |
| 116 |
bool * const repeatP) { |
| 117 |
|
| 118 |
if (rlencoded) { |
| 119 |
if (RLE_count == 0) { |
| 120 |
|
| 121 |
unsigned char i; |
| 122 |
i = getbyte(ifP); |
| 123 |
RLE_flag = (i & 0x80); |
| 124 |
if (RLE_flag == 0) { |
| 125 |
|
| 126 |
RLE_count = i + 1; |
| 127 |
} else { |
| 128 |
|
| 129 |
RLE_count = i - 127; |
| 130 |
} |
| 131 |
|
| 132 |
--RLE_count; |
| 133 |
*repeatP = false; |
| 134 |
} else { |
| 135 |
|
| 136 |
--RLE_count; |
| 137 |
if (RLE_flag != 0) { |
| 138 |
|
| 139 |
*repeatP = true; |
| 140 |
} else |
| 141 |
*repeatP = false; |
| 142 |
} |
| 143 |
} else |
| 144 |
*repeatP = false; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
static void |
| 150 |
getPixel(FILE * const ifP, |
| 151 |
pixel * const dest, |
| 152 |
int const size, |
| 153 |
gray * const alphaP) { |
| 154 |
|
| 155 |
static pixval red, grn, blu; |
| 156 |
static pixval alpha; |
| 157 |
static unsigned int l; |
| 158 |
unsigned char j, k; |
| 159 |
bool repeat; |
| 160 |
|
| 161 |
|
| 162 |
handleRun(ifP, &repeat); |
| 163 |
|
| 164 |
if (repeat) { |
| 165 |
|
| 166 |
} else { |
| 167 |
|
| 168 |
switch (size) { |
| 169 |
case 8: |
| 170 |
red = grn = blu = l = getbyte(ifP); |
| 171 |
alpha = 0; |
| 172 |
break; |
| 173 |
|
| 174 |
case 16: |
| 175 |
case 15: |
| 176 |
j = getbyte(ifP); |
| 177 |
k = getbyte(ifP); |
| 178 |
l = ((unsigned int)k << 8) + j; |
| 179 |
red = (k & 0x7C) >> 2; |
| 180 |
grn = ((k & 0x03) << 3) + ((j & 0xE0) >> 5); |
| 181 |
blu = j & 0x1F; |
| 182 |
alpha = 0; |
| 183 |
break; |
| 184 |
|
| 185 |
case 32: |
| 186 |
case 24: |
| 187 |
blu = getbyte(ifP); |
| 188 |
grn = getbyte(ifP); |
| 189 |
red = getbyte(ifP); |
| 190 |
if (size == 32) |
| 191 |
alpha = getbyte(ifP); |
| 192 |
else |
| 193 |
alpha = 0; |
| 194 |
l = 0; |
| 195 |
break; |
| 196 |
|
| 197 |
default: |
| 198 |
pm_error("unknown pixel size (#2) - %d", size); |
| 199 |
} |
| 200 |
} |
| 201 |
if (mapped) { |
| 202 |
*dest = ColorMap[l]; |
| 203 |
*alphaP = AlphaMap[l]; |
| 204 |
} else { |
| 205 |
PPM_ASSIGN(*dest, red, grn, blu); |
| 206 |
*alphaP = alpha; |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static void |
| 213 |
readtga(FILE * const ifP, struct ImageHeader * tgaP) { |
| 214 |
|
| 215 |
unsigned char flags; |
| 216 |
ImageIDField junk; |
| 217 |
|
| 218 |
tgaP->IdLength = getbyte( ifP ); |
| 219 |
tgaP->CoMapType = getbyte( ifP ); |
| 220 |
tgaP->ImgType = getbyte( ifP ); |
| 221 |
tgaP->Index_lo = getbyte( ifP ); |
| 222 |
tgaP->Index_hi = getbyte( ifP ); |
| 223 |
tgaP->Length_lo = getbyte( ifP ); |
| 224 |
tgaP->Length_hi = getbyte( ifP ); |
| 225 |
tgaP->CoSize = getbyte( ifP ); |
| 226 |
tgaP->X_org_lo = getbyte( ifP ); |
| 227 |
tgaP->X_org_hi = getbyte( ifP ); |
| 228 |
tgaP->Y_org_lo = getbyte( ifP ); |
| 229 |
tgaP->Y_org_hi = getbyte( ifP ); |
| 230 |
tgaP->Width_lo = getbyte( ifP ); |
| 231 |
tgaP->Width_hi = getbyte( ifP ); |
| 232 |
tgaP->Height_lo = getbyte( ifP ); |
| 233 |
tgaP->Height_hi = getbyte( ifP ); |
| 234 |
tgaP->PixelSize = getbyte( ifP ); |
| 235 |
flags = getbyte( ifP ); |
| 236 |
tgaP->AttBits = flags & 0xf; |
| 237 |
tgaP->Rsrvd = ( flags & 0x10 ) >> 4; |
| 238 |
tgaP->OrgBit = ( flags & 0x20 ) >> 5; |
| 239 |
tgaP->IntrLve = ( flags & 0xc0 ) >> 6; |
| 240 |
|
| 241 |
if ( tgaP->IdLength != 0 ) |
| 242 |
fread( junk, 1, (int) tgaP->IdLength, ifP ); |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static void |
| 248 |
get_map_entry(FILE * const ifP, pixel * Value, int Size, gray * Alpha) { |
| 249 |
|
| 250 |
unsigned char j, k, r, g, b, a; |
| 251 |
|
| 252 |
|
| 253 |
switch ( Size ) |
| 254 |
{ |
| 255 |
case 8: |
| 256 |
r = g = b = getbyte( ifP ); |
| 257 |
a = 0; |
| 258 |
break; |
| 259 |
|
| 260 |
case 16: |
| 261 |
case 15: |
| 262 |
j = getbyte( ifP ); |
| 263 |
k = getbyte( ifP ); |
| 264 |
r = ( k & 0x7C ) >> 2; |
| 265 |
g = ( ( k & 0x03 ) << 3 ) + ( ( j & 0xE0 ) >> 5 ); |
| 266 |
b = j & 0x1F; |
| 267 |
a = 0; |
| 268 |
break; |
| 269 |
|
| 270 |
case 32: |
| 271 |
case 24: |
| 272 |
b = getbyte( ifP ); |
| 273 |
g = getbyte( ifP ); |
| 274 |
r = getbyte( ifP ); |
| 275 |
if ( Size == 32 ) |
| 276 |
a = getbyte( ifP ); |
| 277 |
else |
| 278 |
a = 0; |
| 279 |
break; |
| 280 |
|
| 281 |
default: |
| 282 |
pm_error( "unknown colormap pixel size (#2) - %d", Size ); |
| 283 |
} |
| 284 |
PPM_ASSIGN( *Value, r, g, b ); |
| 285 |
*Alpha = a; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static void |
| 291 |
dumpHeader(struct ImageHeader const tga_head) { |
| 292 |
const char * imgTypeName; |
| 293 |
switch(tga_head.ImgType) { |
| 294 |
case TGA_Map: imgTypeName = "TGA_Map"; break; |
| 295 |
case TGA_RGB: imgTypeName = "TGA_RGB"; break; |
| 296 |
case TGA_Mono: imgTypeName = "TGA_Mono"; break; |
| 297 |
case TGA_RLEMap: imgTypeName = "TGA_RLEMap"; break; |
| 298 |
case TGA_RLERGB: imgTypeName = "TGA_RLERGB"; break; |
| 299 |
case TGA_RLEMono: imgTypeName = "TGA_RLEMono"; break; |
| 300 |
case TGA_CompMap: imgTypeName = "TGA_CompMap"; break; |
| 301 |
case TGA_CompMap4: imgTypeName = "TGA_CompMap4"; break; |
| 302 |
default: imgTypeName = "unknown"; |
| 303 |
} |
| 304 |
pm_message( "IdLength = %d", (int) tga_head.IdLength ); |
| 305 |
pm_message( "CoMapType = %d", (int) tga_head.CoMapType ); |
| 306 |
pm_message( "ImgType = %d (%s)", (int) tga_head.ImgType, imgTypeName ); |
| 307 |
pm_message( "Index_lo = %d", (int) tga_head.Index_lo ); |
| 308 |
pm_message( "Index_hi = %d", (int) tga_head.Index_hi ); |
| 309 |
pm_message( "Length_lo = %d", (int) tga_head.Length_lo ); |
| 310 |
pm_message( "Length_hi = %d", (int) tga_head.Length_hi ); |
| 311 |
pm_message( "CoSize = %d", (int) tga_head.CoSize ); |
| 312 |
pm_message( "X_org_lo = %d", (int) tga_head.X_org_lo ); |
| 313 |
pm_message( "X_org_hi = %d", (int) tga_head.X_org_hi ); |
| 314 |
pm_message( "Y_org_lo = %d", (int) tga_head.Y_org_lo ); |
| 315 |
pm_message( "Y_org_hi = %d", (int) tga_head.Y_org_hi ); |
| 316 |
pm_message( "Width_lo = %d", (int) tga_head.Width_lo ); |
| 317 |
pm_message( "Width_hi = %d", (int) tga_head.Width_hi ); |
| 318 |
pm_message( "Height_lo = %d", (int) tga_head.Height_lo ); |
| 319 |
pm_message( "Height_hi = %d", (int) tga_head.Height_hi ); |
| 320 |
pm_message( "PixelSize = %d", (int) tga_head.PixelSize ); |
| 321 |
pm_message( "AttBits = %d", (int) tga_head.AttBits ); |
| 322 |
pm_message( "Rsrvd = %d", (int) tga_head.Rsrvd ); |
| 323 |
pm_message( "OrgBit = %d", (int) tga_head.OrgBit ); |
| 324 |
pm_message( "IntrLve = %d", (int) tga_head.IntrLve ); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
int |
| 330 |
main(int argc, char * argv[]) { |
| 331 |
|
| 332 |
struct cmdlineInfo cmdline; |
| 333 |
struct ImageHeader tga_head; |
| 334 |
FILE* ifP; |
| 335 |
FILE *imageout_file, *alpha_file; |
| 336 |
int rows, cols, row, realrow, truerow, baserow; |
| 337 |
int maxval; |
| 338 |
pixel** pixels; |
| 339 |
gray** alpha; |
| 340 |
|
| 341 |
ppm_init(&argc, argv); |
| 342 |
|
| 343 |
parseCommandLine(argc, argv, &cmdline); |
| 344 |
|
| 345 |
ifP = pm_openr(cmdline.input_filename); |
| 346 |
|
| 347 |
if (cmdline.alpha_stdout) |
| 348 |
alpha_file = stdout; |
| 349 |
else if (cmdline.alpha_filename == NULL) |
| 350 |
alpha_file = NULL; |
| 351 |
else |
| 352 |
alpha_file = pm_openw(cmdline.alpha_filename); |
| 353 |
|
| 354 |
if (cmdline.alpha_stdout) |
| 355 |
imageout_file = NULL; |
| 356 |
else |
| 357 |
imageout_file = stdout; |
| 358 |
|
| 359 |
|
| 360 |
readtga(ifP, &tga_head); |
| 361 |
|
| 362 |
if (cmdline.headerdump) |
| 363 |
dumpHeader(tga_head); |
| 364 |
|
| 365 |
rows = ((int) tga_head.Height_lo) + ((int) tga_head.Height_hi) * 256; |
| 366 |
cols = ((int) tga_head.Width_lo) + ((int) tga_head.Width_hi) * 256; |
| 367 |
|
| 368 |
switch (tga_head.ImgType) { |
| 369 |
case TGA_Map: |
| 370 |
case TGA_RGB: |
| 371 |
case TGA_Mono: |
| 372 |
case TGA_RLEMap: |
| 373 |
case TGA_RLERGB: |
| 374 |
case TGA_RLEMono: |
| 375 |
break; |
| 376 |
case TGA_CompMap: |
| 377 |
case TGA_CompMap4: |
| 378 |
pm_error("Targa image type %d (compressed color-mapped data). " |
| 379 |
"Cannot handle this format.", tga_head.ImgType); |
| 380 |
break; |
| 381 |
default: |
| 382 |
pm_error("unknown Targa image type %d", tga_head.ImgType); |
| 383 |
} |
| 384 |
|
| 385 |
if (tga_head.ImgType == TGA_Map || |
| 386 |
tga_head.ImgType == TGA_RLEMap || |
| 387 |
tga_head.ImgType == TGA_CompMap || |
| 388 |
tga_head.ImgType == TGA_CompMap4) |
| 389 |
{ |
| 390 |
if (tga_head.CoMapType != 1) |
| 391 |
pm_error( |
| 392 |
"mapped image (type %d) with color map type != 1", |
| 393 |
tga_head.ImgType ); |
| 394 |
mapped = true; |
| 395 |
|
| 396 |
switch (tga_head.CoSize) { |
| 397 |
case 8: |
| 398 |
case 24: |
| 399 |
case 32: |
| 400 |
maxval = 255; |
| 401 |
break; |
| 402 |
|
| 403 |
case 15: |
| 404 |
case 16: |
| 405 |
maxval = 31; |
| 406 |
break; |
| 407 |
|
| 408 |
default: |
| 409 |
pm_error( |
| 410 |
"unknown colormap pixel size - %d", tga_head.CoSize ); |
| 411 |
} |
| 412 |
} else { |
| 413 |
|
| 414 |
mapped = false; |
| 415 |
switch ( tga_head.PixelSize ) { |
| 416 |
case 8: |
| 417 |
case 24: |
| 418 |
case 32: |
| 419 |
maxval = 255; |
| 420 |
break; |
| 421 |
|
| 422 |
case 15: |
| 423 |
case 16: |
| 424 |
maxval = 31; |
| 425 |
break; |
| 426 |
|
| 427 |
default: |
| 428 |
pm_error("unknown pixel size - %d", tga_head.PixelSize); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
if ( tga_head.CoMapType != 0 ) { |
| 434 |
unsigned int i; |
| 435 |
unsigned int temp1, temp2; |
| 436 |
|
| 437 |
temp1 = tga_head.Index_lo + tga_head.Index_hi * 256; |
| 438 |
temp2 = tga_head.Length_lo + tga_head.Length_hi * 256; |
| 439 |
if ((temp1 + temp2 + 1) >= MAXCOLORS) |
| 440 |
pm_error("too many colors - %d", (temp1 + temp2 + 1)); |
| 441 |
for (i = temp1; i < (temp1 + temp2); ++i) |
| 442 |
get_map_entry(ifP, &ColorMap[i], (int) tga_head.CoSize, |
| 443 |
&AlphaMap[i]); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
if (tga_head.ImgType == TGA_RLEMap || |
| 448 |
tga_head.ImgType == TGA_RLERGB || |
| 449 |
tga_head.ImgType == TGA_RLEMono) |
| 450 |
rlencoded = 1; |
| 451 |
else |
| 452 |
rlencoded = 0; |
| 453 |
|
| 454 |
|
| 455 |
pixels = ppm_allocarray( cols, rows ); |
| 456 |
alpha = pgm_allocarray( cols, rows ); |
| 457 |
truerow = 0; |
| 458 |
baserow = 0; |
| 459 |
for (row = 0; row < rows; ++row) { |
| 460 |
unsigned int col; |
| 461 |
|
| 462 |
realrow = truerow; |
| 463 |
if (tga_head.OrgBit == 0) |
| 464 |
realrow = rows - realrow - 1; |
| 465 |
|
| 466 |
for (col = 0; col < cols; ++col) |
| 467 |
getPixel(ifP, &(pixels[realrow][col]), (int) tga_head.PixelSize, |
| 468 |
&(alpha[realrow][col])); |
| 469 |
if (tga_head.IntrLve == TGA_IL_Four) |
| 470 |
truerow += 4; |
| 471 |
else if (tga_head.IntrLve == TGA_IL_Two) |
| 472 |
truerow += 2; |
| 473 |
else |
| 474 |
++truerow; |
| 475 |
if (truerow >= rows) |
| 476 |
truerow = ++baserow; |
| 477 |
} |
| 478 |
pm_close(ifP); |
| 479 |
|
| 480 |
if (imageout_file) |
| 481 |
ppm_writeppm(imageout_file, pixels, cols, rows, (pixval) maxval, 0); |
| 482 |
if (alpha_file) |
| 483 |
pgm_writepgm(alpha_file, alpha, cols, rows, (pixval) maxval, 0); |
| 484 |
if (imageout_file) |
| 485 |
pm_close(imageout_file); |
| 486 |
if (alpha_file) |
| 487 |
pm_close(alpha_file); |
| 488 |
|
| 489 |
return 0; |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|