| 1 |
/* pamtotga.c - read a portable pixmap and produce a TrueVision Targa file |
| 2 |
** |
| 3 |
** Copyright (C) 1989, 1991 by Mark Shand and Jef Poskanzer |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
*/ |
| 12 |
|
| 13 |
#define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */ |
| 14 |
#define _BSD_SOURCE /* Make sure string.h contains strdup() */ |
| 15 |
#define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */ |
| 16 |
|
| 17 |
#include <assert.h> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "pm_c_util.h" |
| 21 |
#include "pam.h" |
| 22 |
#include "pammap.h" |
| 23 |
#include "shhopt.h" |
| 24 |
#include "mallocvar.h" |
| 25 |
#include "nstring.h" |
| 26 |
#include "tga.h" |
| 27 |
|
| 28 |
/* Max number of colors allowed for colormapped output. */ |
| 29 |
#define MAXCOLORS 256 |
| 30 |
|
| 31 |
struct CmdlineInfo { |
| 32 |
/* All the information the user supplied in the command line, |
| 33 |
in a form easy for the program to use. |
| 34 |
*/ |
| 35 |
const char * inputFileName; |
| 36 |
const char * outName; |
| 37 |
enum TGAbaseImageType imgType; |
| 38 |
enum TGAmapType mapType; |
| 39 |
bool defaultFormat; |
| 40 |
unsigned int norle; |
| 41 |
unsigned int verbose; |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
parseCommandLine(int argc, const char ** argv, |
| 48 |
struct CmdlineInfo * const cmdlineP) { |
| 49 |
/*---------------------------------------------------------------------------- |
| 50 |
Parse the program arguments (given by argc and argv) into a form |
| 51 |
the program can deal with more easily -- a cmdline_info structure. |
| 52 |
If the syntax is invalid, issue a message and exit the program via |
| 53 |
pm_error(). |
| 54 |
|
| 55 |
Note that the file spec array we return is stored in the storage that |
| 56 |
was passed to us as the argv array. |
| 57 |
-----------------------------------------------------------------------------*/ |
| 58 |
optStruct3 opt; /* set by OPTENT3 */ |
| 59 |
optEntry *option_def = malloc(100*sizeof(optEntry)); |
| 60 |
unsigned int option_def_index; |
| 61 |
|
| 62 |
unsigned int outNameSpec; |
| 63 |
unsigned int cmap, cmap16, mono, rgb; |
| 64 |
|
| 65 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 66 |
OPTENT3(0, "name", OPT_STRING, |
| 67 |
&cmdlineP->outName, &outNameSpec, 0); |
| 68 |
OPTENT3(0, "cmap", OPT_FLAG, |
| 69 |
NULL, &cmap, 0); |
| 70 |
OPTENT3(0, "cmap16", OPT_FLAG, |
| 71 |
NULL, &cmap16, 0); |
| 72 |
OPTENT3(0, "mono", OPT_FLAG, |
| 73 |
NULL, &mono, 0); |
| 74 |
OPTENT3(0, "rgb", OPT_FLAG, |
| 75 |
NULL, &rgb, 0); |
| 76 |
OPTENT3(0, "norle", OPT_FLAG, |
| 77 |
NULL, &cmdlineP->norle, 0); |
| 78 |
OPTENT3(0, "verbose", OPT_FLAG, |
| 79 |
NULL, &cmdlineP->verbose, 0); |
| 80 |
|
| 81 |
opt.opt_table = option_def; |
| 82 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 83 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 84 |
|
| 85 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 86 |
/* Uses and sets argc, argv, and some of *cmdline_p and others. */ |
| 87 |
|
| 88 |
if (cmap + cmap16 + mono + rgb > 1) |
| 89 |
pm_error("You may specify only one of -cmap, -cmap16, " |
| 90 |
"-mono, and -rgb."); |
| 91 |
|
| 92 |
if (cmap + cmap16 + mono + rgb == 0) |
| 93 |
cmdlineP->defaultFormat = TRUE; |
| 94 |
else { |
| 95 |
cmdlineP->defaultFormat = FALSE; |
| 96 |
|
| 97 |
if (cmap) { |
| 98 |
cmdlineP->imgType = TGA_MAP_TYPE; |
| 99 |
cmdlineP->mapType = TGA_MAPTYPE_LONG; |
| 100 |
} else if (cmap16) { |
| 101 |
cmdlineP->imgType = TGA_MAP_TYPE; |
| 102 |
cmdlineP->mapType = TGA_MAPTYPE_SHORT; |
| 103 |
} else if (mono) |
| 104 |
cmdlineP->imgType = TGA_MONO_TYPE; |
| 105 |
else if (rgb) |
| 106 |
cmdlineP->imgType = TGA_RGB_TYPE; |
| 107 |
} |
| 108 |
|
| 109 |
if (!outNameSpec) |
| 110 |
cmdlineP->outName = NULL; |
| 111 |
|
| 112 |
if (argc-1 == 0) |
| 113 |
cmdlineP->inputFileName = "-"; |
| 114 |
else if (argc-1 != 1) |
| 115 |
pm_error("Program takes zero or one argument (filename). You " |
| 116 |
"specified %d", argc-1); |
| 117 |
else |
| 118 |
cmdlineP->inputFileName = argv[1]; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static void |
| 125 |
putPixel(struct pam * const pamP, |
| 126 |
tuple const tuple, |
| 127 |
enum TGAbaseImageType const imgType, |
| 128 |
bool const withAlpha, |
| 129 |
tuplehash const cht) { |
| 130 |
/*---------------------------------------------------------------------------- |
| 131 |
Write a single pixel of the TGA raster to Standard Output. The |
| 132 |
pixel is to have color 'tuple'. The raster has format 'imgType'. |
| 133 |
The color palette from which the specified color is to be drawn, if |
| 134 |
'imgType' indicates use of a color palette, is 'cht'. |
| 135 |
-----------------------------------------------------------------------------*/ |
| 136 |
if (imgType == TGA_MAP_TYPE) { |
| 137 |
int retval; |
| 138 |
int found; |
| 139 |
|
| 140 |
pnm_lookuptuple(pamP, cht, tuple, &found, &retval); |
| 141 |
if (!found) |
| 142 |
pm_error("Internal error: color not found in map that was " |
| 143 |
"generated from all the colors in the image"); |
| 144 |
putchar(retval); |
| 145 |
} else { |
| 146 |
if (imgType == TGA_RGB_TYPE && pamP->depth < 3) { |
| 147 |
/* Make RGB pixel out of a single input plane */ |
| 148 |
unsigned int plane; |
| 149 |
|
| 150 |
for (plane = 0; plane < 3; ++plane) |
| 151 |
putchar(pnm_scalesample(tuple[0], |
| 152 |
pamP->maxval, TGA_MAXVAL)); |
| 153 |
} else if (imgType == TGA_MONO_TYPE) |
| 154 |
putchar(pnm_scalesample(tuple[0], |
| 155 |
pamP->maxval, TGA_MAXVAL)); |
| 156 |
else { |
| 157 |
putchar(pnm_scalesample(tuple[PAM_BLU_PLANE], |
| 158 |
pamP->maxval, TGA_MAXVAL)); |
| 159 |
putchar(pnm_scalesample(tuple[PAM_GRN_PLANE], |
| 160 |
pamP->maxval, TGA_MAXVAL)); |
| 161 |
putchar(pnm_scalesample(tuple[PAM_RED_PLANE], |
| 162 |
pamP->maxval, TGA_MAXVAL)); |
| 163 |
if (withAlpha) |
| 164 |
putchar(pnm_scalesample(tuple[PAM_TRN_PLANE], |
| 165 |
pamP->maxval, TGA_MAXVAL)); |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
static void |
| 173 |
putMapEntry(struct pam * const pamP, |
| 174 |
tuple const value, |
| 175 |
int const size) { |
| 176 |
|
| 177 |
if (size == 8) |
| 178 |
putchar(pnm_scalesample(value[0], |
| 179 |
pamP->maxval, TGA_MAXVAL)); |
| 180 |
else if (size == 15 || size == 16) { |
| 181 |
tuple const tuple31 = pnm_allocpamtuple(pamP); |
| 182 |
|
| 183 |
assert(pamP->depth >= 3); |
| 184 |
|
| 185 |
pnm_scaletuple(pamP, tuple31, value, 31); |
| 186 |
{ |
| 187 |
unsigned int const trn = |
| 188 |
size == 16 && tuple31[PAM_TRN_PLANE] > 0 ? 1 : 0; |
| 189 |
|
| 190 |
unsigned int const mapentry = |
| 191 |
tuple31[PAM_BLU_PLANE] << 0 | |
| 192 |
tuple31[PAM_GRN_PLANE] << 5 | |
| 193 |
tuple31[PAM_RED_PLANE] << 10 | |
| 194 |
trn << 15; |
| 195 |
|
| 196 |
/* Note little-endian byte swapping */ |
| 197 |
putchar(mapentry % 256); |
| 198 |
putchar(mapentry / 256); |
| 199 |
} |
| 200 |
pnm_freepamtuple(tuple31); |
| 201 |
} else { |
| 202 |
assert(size == 24 || size == 32); |
| 203 |
|
| 204 |
assert(pamP->depth >= 3); |
| 205 |
|
| 206 |
putchar(pnm_scalesample(value[PAM_BLU_PLANE], |
| 207 |
pamP->maxval, TGA_MAXVAL)); |
| 208 |
putchar(pnm_scalesample(value[PAM_GRN_PLANE], |
| 209 |
pamP->maxval, TGA_MAXVAL)); |
| 210 |
putchar(pnm_scalesample(value[PAM_RED_PLANE], |
| 211 |
pamP->maxval, TGA_MAXVAL)); |
| 212 |
if (size == 32) |
| 213 |
putchar(pnm_scalesample(value[PAM_TRN_PLANE], |
| 214 |
pamP->maxval, TGA_MAXVAL)); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
static void |
| 221 |
computeRunlengths(struct pam * const pamP, |
| 222 |
tuple * const tuplerow, |
| 223 |
int * const runlength) { |
| 224 |
|
| 225 |
int col, start; |
| 226 |
|
| 227 |
/* Initialize all run lengths to 0. (This is just an error check.) */ |
| 228 |
for (col = 0; col < pamP->width; ++col) |
| 229 |
runlength[col] = 0; |
| 230 |
|
| 231 |
/* Find runs of identical pixels. */ |
| 232 |
for ( col = 0; col < pamP->width; ) { |
| 233 |
start = col; |
| 234 |
do { |
| 235 |
++col; |
| 236 |
} while ( col < pamP->width && |
| 237 |
col - start < 128 && |
| 238 |
pnm_tupleequal(pamP, tuplerow[col], tuplerow[start])); |
| 239 |
runlength[start] = col - start; |
| 240 |
} |
| 241 |
|
| 242 |
/* Now look for runs of length-1 runs, and turn them into negative runs. */ |
| 243 |
for (col = 0; col < pamP->width; ) { |
| 244 |
if (runlength[col] == 1) { |
| 245 |
start = col; |
| 246 |
while (col < pamP->width && |
| 247 |
col - start < 128 && |
| 248 |
runlength[col] == 1 ) { |
| 249 |
runlength[col] = 0; |
| 250 |
++col; |
| 251 |
} |
| 252 |
runlength[start] = - ( col - start ); |
| 253 |
} else |
| 254 |
col += runlength[col]; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
static void |
| 261 |
computeOutName(struct CmdlineInfo const cmdline, |
| 262 |
const char ** const outNameP) { |
| 263 |
|
| 264 |
char * workarea; |
| 265 |
|
| 266 |
if (cmdline.outName) |
| 267 |
workarea = strdup(cmdline.outName); |
| 268 |
else if (streq(cmdline.inputFileName, "-")) |
| 269 |
workarea = NULL; |
| 270 |
else { |
| 271 |
char * cp; |
| 272 |
workarea = strdup(cmdline.inputFileName); |
| 273 |
cp = strchr(workarea, '.'); |
| 274 |
if (cp != NULL) |
| 275 |
*cp = '\0'; /* remove extension */ |
| 276 |
} |
| 277 |
|
| 278 |
if (workarea == NULL) |
| 279 |
*outNameP = NULL; |
| 280 |
else { |
| 281 |
/* Truncate the name to fit TGA specs */ |
| 282 |
if (strlen(workarea) > IMAGEIDFIELDMAXSIZE) |
| 283 |
workarea[IMAGEIDFIELDMAXSIZE] = '\0'; |
| 284 |
*outNameP = workarea; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
static void |
| 291 |
validateTupleType(struct pam * const pamP) { |
| 292 |
|
| 293 |
if (streq(pamP->tuple_type, "RGB_ALPHA")) { |
| 294 |
if (pamP->depth < 4) |
| 295 |
pm_error("Invalid depth for tuple type RGB_ALPHA. " |
| 296 |
"Should have at least 4 planes, but has %d.", |
| 297 |
pamP->depth); |
| 298 |
} else if (streq(pamP->tuple_type, "RGB")) { |
| 299 |
if (pamP->depth < 3) |
| 300 |
pm_error("Invalid depth for tuple type RGB. " |
| 301 |
"Should have at least 3 planes, but has %d.", |
| 302 |
pamP->depth); |
| 303 |
} else if (streq(pamP->tuple_type, "GRAYSCALE")) { |
| 304 |
} else if (streq(pamP->tuple_type, "BLACKANDWHITE")) { |
| 305 |
} else |
| 306 |
pm_error("Invalid type of input. PAM tuple type is '%s'. " |
| 307 |
"This programs understands only RGB_ALPHA, RGB, GRAYSCALE, " |
| 308 |
"and BLACKANDWHITE.", pamP->tuple_type); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
static void |
| 314 |
computeImageType_cht(struct pam * const pamP, |
| 315 |
struct CmdlineInfo const cmdline, |
| 316 |
tuple ** const tuples, |
| 317 |
enum TGAbaseImageType * const baseImgTypeP, |
| 318 |
enum TGAmapType * const mapTypeP, |
| 319 |
bool * const withAlphaP, |
| 320 |
tupletable * const chvP, |
| 321 |
tuplehash * const chtP, |
| 322 |
int * const ncolorsP) { |
| 323 |
|
| 324 |
unsigned int ncolors; |
| 325 |
enum TGAbaseImageType baseImgType; |
| 326 |
enum TGAmapType mapType; |
| 327 |
bool withAlpha; |
| 328 |
|
| 329 |
validateTupleType(pamP); |
| 330 |
|
| 331 |
if (cmdline.defaultFormat) { |
| 332 |
/* default the image type */ |
| 333 |
if (streq(pamP->tuple_type, "RGB_ALPHA")) { |
| 334 |
baseImgType = TGA_RGB_TYPE; |
| 335 |
*chvP = NULL; |
| 336 |
withAlpha = true; |
| 337 |
} else if (streq(pamP->tuple_type, "RGB")) { |
| 338 |
pm_message("computing colormap..."); |
| 339 |
*chvP = |
| 340 |
pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors); |
| 341 |
if (*chvP == NULL) { |
| 342 |
pm_message("Too many colors for colormapped TGA. Doing RGB."); |
| 343 |
baseImgType = TGA_RGB_TYPE; |
| 344 |
} else { |
| 345 |
baseImgType = TGA_MAP_TYPE; |
| 346 |
mapType = TGA_MAPTYPE_LONG; |
| 347 |
} |
| 348 |
withAlpha = false; |
| 349 |
} else { |
| 350 |
baseImgType = TGA_MONO_TYPE; |
| 351 |
*chvP = NULL; |
| 352 |
withAlpha = false; |
| 353 |
} |
| 354 |
} else { |
| 355 |
withAlpha = (streq(pamP->tuple_type, "RGB_ALPHA")); |
| 356 |
|
| 357 |
baseImgType = cmdline.imgType; |
| 358 |
|
| 359 |
if (baseImgType == TGA_MAP_TYPE) { |
| 360 |
mapType = cmdline.mapType; |
| 361 |
|
| 362 |
if (withAlpha) |
| 363 |
pm_error("Can't do a colormap because image has transparency " |
| 364 |
"information"); |
| 365 |
pm_message("computing colormap..."); |
| 366 |
*chvP = |
| 367 |
pnm_computetuplefreqtable(pamP, tuples, MAXCOLORS, &ncolors); |
| 368 |
if (*chvP == NULL) |
| 369 |
pm_error("Too many colors for colormapped TGA. " |
| 370 |
"Use 'pnmquant %d' to reduce the number of colors.", |
| 371 |
MAXCOLORS); |
| 372 |
} else |
| 373 |
*chvP = NULL; |
| 374 |
} |
| 375 |
|
| 376 |
if (baseImgType == TGA_MAP_TYPE) { |
| 377 |
pm_message("%u colors found.", ncolors); |
| 378 |
/* Make a hash table for fast color lookup. */ |
| 379 |
*chtP = pnm_computetupletablehash(pamP, *chvP, ncolors); |
| 380 |
} else |
| 381 |
*chtP = NULL; |
| 382 |
|
| 383 |
*baseImgTypeP = baseImgType; |
| 384 |
*mapTypeP = mapType; |
| 385 |
*withAlphaP = withAlpha; |
| 386 |
*ncolorsP = ncolors; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
static void |
| 392 |
computeTgaHeader(struct pam * const pamP, |
| 393 |
enum TGAbaseImageType const baseImgType, |
| 394 |
enum TGAmapType const mapType, |
| 395 |
bool const withAlpha, |
| 396 |
bool const rle, |
| 397 |
int const ncolors, |
| 398 |
unsigned char const orgBit, |
| 399 |
const char * const id, |
| 400 |
struct ImageHeader * const tgaHeaderP) { |
| 401 |
|
| 402 |
if (rle) { |
| 403 |
switch (baseImgType ) { |
| 404 |
case TGA_MONO_TYPE: tgaHeaderP->ImgType = TGA_RLEMono; break; |
| 405 |
case TGA_MAP_TYPE: tgaHeaderP->ImgType = TGA_RLEMap; break; |
| 406 |
case TGA_RGB_TYPE: tgaHeaderP->ImgType = TGA_RLERGB; break; |
| 407 |
} |
| 408 |
} else { |
| 409 |
switch(baseImgType) { |
| 410 |
case TGA_MONO_TYPE: tgaHeaderP->ImgType = TGA_Mono; break; |
| 411 |
case TGA_MAP_TYPE: tgaHeaderP->ImgType = TGA_Map; break; |
| 412 |
case TGA_RGB_TYPE: tgaHeaderP->ImgType = TGA_RGB; break; |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
if (id) { |
| 417 |
tgaHeaderP->IdLength = strlen(id); |
| 418 |
tgaHeaderP->Id = strdup(id); |
| 419 |
} else |
| 420 |
tgaHeaderP->IdLength = 0; |
| 421 |
tgaHeaderP->Index_lo = 0; |
| 422 |
tgaHeaderP->Index_hi = 0; |
| 423 |
if (baseImgType == TGA_MAP_TYPE) { |
| 424 |
tgaHeaderP->CoMapType = 1; |
| 425 |
tgaHeaderP->Length_lo = ncolors % 256; |
| 426 |
tgaHeaderP->Length_hi = ncolors / 256; |
| 427 |
if (pamP->depth < 3) |
| 428 |
tgaHeaderP->CoSize = 8; |
| 429 |
else { |
| 430 |
switch (mapType) { |
| 431 |
case TGA_MAPTYPE_SHORT: |
| 432 |
tgaHeaderP->CoSize = withAlpha ? 16 : 15; |
| 433 |
break; |
| 434 |
case TGA_MAPTYPE_LONG: |
| 435 |
tgaHeaderP->CoSize = withAlpha ? 32 : 24; |
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
} else { |
| 440 |
tgaHeaderP->CoMapType = 0; |
| 441 |
tgaHeaderP->Length_lo = 0; |
| 442 |
tgaHeaderP->Length_hi = 0; |
| 443 |
tgaHeaderP->CoSize = 0; |
| 444 |
} |
| 445 |
switch (baseImgType) { |
| 446 |
case TGA_MAP_TYPE: |
| 447 |
tgaHeaderP->PixelSize = 8; |
| 448 |
break; |
| 449 |
case TGA_RGB_TYPE: |
| 450 |
tgaHeaderP->PixelSize = 8 * (withAlpha ? 4: 3); |
| 451 |
break; |
| 452 |
case TGA_MONO_TYPE: |
| 453 |
tgaHeaderP->PixelSize = 8; |
| 454 |
} |
| 455 |
tgaHeaderP->X_org_lo = tgaHeaderP->X_org_hi = 0; |
| 456 |
tgaHeaderP->Y_org_lo = tgaHeaderP->Y_org_hi = 0; |
| 457 |
tgaHeaderP->Width_lo = pamP->width % 256; |
| 458 |
tgaHeaderP->Width_hi = pamP->width / 256; |
| 459 |
tgaHeaderP->Height_lo = pamP->height % 256; |
| 460 |
tgaHeaderP->Height_hi = pamP->height / 256; |
| 461 |
tgaHeaderP->AttBits = 0; |
| 462 |
tgaHeaderP->Rsrvd = 0; |
| 463 |
tgaHeaderP->IntrLve = 0; |
| 464 |
tgaHeaderP->OrgBit = orgBit; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
static void |
| 470 |
reportTgaHeader(struct ImageHeader const tgaHeader) { |
| 471 |
|
| 472 |
switch (tgaHeader.ImgType) { |
| 473 |
case TGA_RLEMono: |
| 474 |
pm_message("Generating monochrome, run-length encoded"); |
| 475 |
break; |
| 476 |
case TGA_RLEMap: |
| 477 |
pm_message("Generating colormapped, run-length encoded"); |
| 478 |
pm_message("%u bits per colormap entry", tgaHeader.CoSize); |
| 479 |
break; |
| 480 |
case TGA_RLERGB: |
| 481 |
pm_message("Generating RGB truecolor, run-length encoded"); |
| 482 |
break; |
| 483 |
case TGA_Mono: |
| 484 |
pm_message("Generating monochrome, uncompressed"); |
| 485 |
break; |
| 486 |
case TGA_Map: |
| 487 |
pm_message("Generating colormapped, uncompressed"); |
| 488 |
pm_message("%u bits per colormap entry", tgaHeader.CoSize); |
| 489 |
break; |
| 490 |
case TGA_RGB: |
| 491 |
pm_message("Generating RGB truecolor, uncompressed"); |
| 492 |
break; |
| 493 |
} |
| 494 |
pm_message("%u bits per pixel", tgaHeader.PixelSize); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
static void |
| 500 |
writeTgaHeader(struct ImageHeader const tgaHeader) { |
| 501 |
|
| 502 |
unsigned char flags; |
| 503 |
|
| 504 |
putchar(tgaHeader.IdLength); |
| 505 |
putchar(tgaHeader.CoMapType); |
| 506 |
putchar(tgaHeader.ImgType); |
| 507 |
putchar(tgaHeader.Index_lo); |
| 508 |
putchar(tgaHeader.Index_hi); |
| 509 |
putchar(tgaHeader.Length_lo); |
| 510 |
putchar(tgaHeader.Length_hi); |
| 511 |
putchar(tgaHeader.CoSize); |
| 512 |
putchar(tgaHeader.X_org_lo); |
| 513 |
putchar(tgaHeader.X_org_hi); |
| 514 |
putchar(tgaHeader.Y_org_lo); |
| 515 |
putchar(tgaHeader.Y_org_hi); |
| 516 |
putchar(tgaHeader.Width_lo); |
| 517 |
putchar(tgaHeader.Width_hi); |
| 518 |
putchar(tgaHeader.Height_lo); |
| 519 |
putchar(tgaHeader.Height_hi); |
| 520 |
putchar(tgaHeader.PixelSize); |
| 521 |
flags = (tgaHeader.AttBits & 0xf) | |
| 522 |
((tgaHeader.Rsrvd & 0x1) << 4) | |
| 523 |
((tgaHeader.OrgBit & 0x1) << 5) | |
| 524 |
((tgaHeader.OrgBit & 0x3) << 6); |
| 525 |
putchar(flags); |
| 526 |
|
| 527 |
if (tgaHeader.IdLength > 0) |
| 528 |
fwrite(tgaHeader.Id, 1, (int) tgaHeader.IdLength, stdout); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
static void |
| 534 |
releaseTgaHeader(struct ImageHeader const tgaHeader) { |
| 535 |
|
| 536 |
if (tgaHeader.IdLength > 0) |
| 537 |
pm_strfree(tgaHeader.Id); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
static void |
| 543 |
writeTgaRaster(struct pam * const pamP, |
| 544 |
tuple ** const tuples, |
| 545 |
tuplehash const cht, |
| 546 |
enum TGAbaseImageType const imgType, |
| 547 |
bool const withAlpha, |
| 548 |
bool const rle, |
| 549 |
unsigned char const orgBit) { |
| 550 |
|
| 551 |
int* runlength; /* malloc'ed */ |
| 552 |
int row; |
| 553 |
|
| 554 |
if (rle) |
| 555 |
MALLOCARRAY(runlength, pamP->width); |
| 556 |
|
| 557 |
for (row = 0; row < pamP->height; ++row) { |
| 558 |
int realrow; |
| 559 |
realrow = (orgBit != 0) ? row : pamP->height - row - 1; |
| 560 |
if (rle) { |
| 561 |
int col; |
| 562 |
computeRunlengths(pamP, tuples[realrow], runlength); |
| 563 |
for (col = 0; col < pamP->width; ) { |
| 564 |
if (runlength[col] > 0) { |
| 565 |
putchar(0x80 + runlength[col] - 1); |
| 566 |
putPixel(pamP, tuples[realrow][col], imgType, withAlpha, |
| 567 |
cht); |
| 568 |
col += runlength[col]; |
| 569 |
} else if (runlength[col] < 0) { |
| 570 |
int i; |
| 571 |
putchar(-runlength[col] - 1); |
| 572 |
for (i = 0; i < -runlength[col]; ++i) |
| 573 |
putPixel(pamP, tuples[realrow][col+i], |
| 574 |
imgType, withAlpha, cht); |
| 575 |
col += -runlength[col]; |
| 576 |
} else |
| 577 |
pm_error("Internal error: zero run length"); |
| 578 |
} |
| 579 |
} else { |
| 580 |
int col; |
| 581 |
for (col = 0; col < pamP->width; ++col) |
| 582 |
putPixel(pamP, tuples[realrow][col], imgType, withAlpha, cht); |
| 583 |
} |
| 584 |
} |
| 585 |
if (rle) |
| 586 |
free(runlength); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
int |
| 592 |
main(int argc, const char **argv) { |
| 593 |
|
| 594 |
struct CmdlineInfo cmdline; |
| 595 |
FILE * ifP; |
| 596 |
tuple ** tuples; |
| 597 |
struct pam pam; |
| 598 |
int colorCt; |
| 599 |
tupletable chv; |
| 600 |
tuplehash cht; |
| 601 |
struct ImageHeader tgaHeader; |
| 602 |
enum TGAbaseImageType baseImgType; |
| 603 |
enum TGAmapType mapType; |
| 604 |
bool withAlpha; |
| 605 |
const char * outName; |
| 606 |
|
| 607 |
pm_proginit(&argc, argv); |
| 608 |
|
| 609 |
parseCommandLine(argc, argv, &cmdline); |
| 610 |
|
| 611 |
ifP = pm_openr(cmdline.inputFileName); |
| 612 |
|
| 613 |
computeOutName(cmdline, &outName); |
| 614 |
|
| 615 |
tuples = pnm_readpam(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 616 |
pm_close(ifP); |
| 617 |
|
| 618 |
computeImageType_cht(&pam, cmdline, tuples, |
| 619 |
&baseImgType, &mapType, |
| 620 |
&withAlpha, &chv, &cht, &colorCt); |
| 621 |
|
| 622 |
/* Do the Targa header */ |
| 623 |
computeTgaHeader(&pam, baseImgType, mapType, withAlpha, !cmdline.norle, |
| 624 |
colorCt, 0, outName, &tgaHeader); |
| 625 |
|
| 626 |
if (cmdline.verbose) |
| 627 |
reportTgaHeader(tgaHeader); |
| 628 |
|
| 629 |
writeTgaHeader(tgaHeader); |
| 630 |
|
| 631 |
if (baseImgType == TGA_MAP_TYPE) { |
| 632 |
/* Write out the Targa colormap. */ |
| 633 |
unsigned int i; |
| 634 |
for (i = 0; i < colorCt; ++i) |
| 635 |
putMapEntry(&pam, chv[i]->tuple, tgaHeader.CoSize); |
| 636 |
} |
| 637 |
|
| 638 |
writeTgaRaster(&pam, tuples, cht, baseImgType, withAlpha, |
| 639 |
!cmdline.norle, 0); |
| 640 |
|
| 641 |
if (cht) |
| 642 |
pnm_destroytuplehash(cht); |
| 643 |
if (chv) |
| 644 |
pnm_freetupletable(&pam, chv); |
| 645 |
|
| 646 |
releaseTgaHeader(tgaHeader); |
| 647 |
pm_strfree(outName); |
| 648 |
pnm_freepamarray(tuples, &pam); |
| 649 |
|
| 650 |
return 0; |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|