| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include <string.h> |
| 16 |
#include <assert.h> |
| 17 |
|
| 18 |
#include "pm_c_util.h" |
| 19 |
#include "pnm.h" |
| 20 |
#include "shhopt.h" |
| 21 |
#include "mallocvar.h" |
| 22 |
|
| 23 |
#include "palm.h" |
| 24 |
#include "palmcolormap.h" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
enum PalmCompressionType { |
| 29 |
COMPRESSION_NONE, |
| 30 |
COMPRESSION_RLE, |
| 31 |
COMPRESSION_SCANLINE, |
| 32 |
COMPRESSION_PACKBITS |
| 33 |
}; |
| 34 |
|
| 35 |
struct PalmHeader { |
| 36 |
unsigned short cols; |
| 37 |
unsigned short rows; |
| 38 |
unsigned short bytesPerRow; |
| 39 |
unsigned short flags; |
| 40 |
bool directColor; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
bool hasColormap; |
| 45 |
bool hasTransparency; |
| 46 |
unsigned char pixelSizeCode; |
| 47 |
unsigned int pixelSize; |
| 48 |
unsigned char version; |
| 49 |
unsigned int transparentIndex; |
| 50 |
enum PalmCompressionType compressionType; |
| 51 |
|
| 52 |
unsigned char size; |
| 53 |
unsigned char pixelFormat; |
| 54 |
unsigned short density; |
| 55 |
unsigned long transparentValue; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
struct DirectPixelFormat { |
| 61 |
unsigned int redbits; |
| 62 |
unsigned int greenbits; |
| 63 |
unsigned int bluebits; |
| 64 |
}; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
struct DirectColorInfo { |
| 69 |
struct DirectPixelFormat pixelFormat; |
| 70 |
ColormapEntry transparentColor; |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
struct CmdlineInfo { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
const char * inputFilespec; |
| 81 |
unsigned int verbose; |
| 82 |
unsigned int rendition; |
| 83 |
unsigned int showhist; |
| 84 |
unsigned int transparent; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
static void |
| 89 |
parseCommandLine(int argc, const char ** argv, |
| 90 |
struct CmdlineInfo *cmdlineP) { |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
optEntry * option_def; |
| 96 |
|
| 97 |
|
| 98 |
optStruct3 opt; |
| 99 |
|
| 100 |
unsigned int renditionSpec; |
| 101 |
|
| 102 |
unsigned int option_def_index; |
| 103 |
|
| 104 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 105 |
|
| 106 |
option_def_index = 0; |
| 107 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 108 |
&cmdlineP->verbose, 0); |
| 109 |
OPTENT3(0, "showhist", OPT_FLAG, NULL, |
| 110 |
&cmdlineP->showhist, 0); |
| 111 |
OPTENT3(0, "transparent", OPT_FLAG, NULL, |
| 112 |
&cmdlineP->transparent, 0); |
| 113 |
OPTENT3(0, "rendition", OPT_UINT, &cmdlineP->rendition, |
| 114 |
&renditionSpec, 0); |
| 115 |
|
| 116 |
opt.opt_table = option_def; |
| 117 |
opt.short_allowed = FALSE; |
| 118 |
opt.allowNegNum = FALSE; |
| 119 |
|
| 120 |
pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
if (renditionSpec) { |
| 125 |
if (cmdlineP->rendition < 1) |
| 126 |
pm_error("The -rendition value must be at least 1"); |
| 127 |
} else |
| 128 |
cmdlineP->rendition = 1; |
| 129 |
|
| 130 |
if (cmdlineP->transparent && cmdlineP->showhist) |
| 131 |
pm_error("You can't specify -showhist with -transparent"); |
| 132 |
|
| 133 |
if (argc-1 < 1) |
| 134 |
cmdlineP->inputFilespec = "-"; |
| 135 |
else { |
| 136 |
cmdlineP->inputFilespec = argv[1]; |
| 137 |
if (argc-1 > 1) |
| 138 |
pm_error("Too many arguments (%d). The only non-option " |
| 139 |
"argument is the file name", argc-1); |
| 140 |
} |
| 141 |
free(option_def); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
static xelval * |
| 147 |
createGraymap(unsigned int const ncolors, |
| 148 |
xelval const maxval) { |
| 149 |
int i; |
| 150 |
xelval *map; |
| 151 |
|
| 152 |
MALLOCARRAY_NOFAIL(map, ncolors); |
| 153 |
for (i = 0; i < ncolors; ++i) { |
| 154 |
map[i] = maxval - (i * maxval) / (ncolors - 1); |
| 155 |
} |
| 156 |
return map; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static void |
| 162 |
skipbytes(FILE * const ifP, |
| 163 |
unsigned int const nbytes) { |
| 164 |
|
| 165 |
unsigned char buf[256]; |
| 166 |
unsigned int n; |
| 167 |
size_t bytesRead; |
| 168 |
|
| 169 |
n = nbytes; |
| 170 |
|
| 171 |
while (n > 0) { |
| 172 |
if (n > sizeof(buf)) { |
| 173 |
bytesRead = fread(buf, sizeof(char), sizeof(buf), ifP); |
| 174 |
if (bytesRead != sizeof(buf)) |
| 175 |
pm_error("Error reading Palm file. Short read."); |
| 176 |
n -= sizeof(buf); |
| 177 |
} else { |
| 178 |
bytesRead = fread(buf, sizeof(char), n, ifP); |
| 179 |
if (bytesRead != n) |
| 180 |
pm_error("Error reading Palm file. Short read."); |
| 181 |
n = 0; |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
static void |
| 189 |
interpretCompression(unsigned char const compressionValue, |
| 190 |
enum PalmCompressionType * const compressionTypeP) { |
| 191 |
|
| 192 |
switch (compressionValue) { |
| 193 |
case PALM_COMPRESSION_RLE: |
| 194 |
*compressionTypeP = COMPRESSION_RLE; |
| 195 |
break; |
| 196 |
case PALM_COMPRESSION_SCANLINE: |
| 197 |
*compressionTypeP = COMPRESSION_SCANLINE; |
| 198 |
break; |
| 199 |
case PALM_COMPRESSION_PACKBITS: |
| 200 |
*compressionTypeP = COMPRESSION_PACKBITS; |
| 201 |
break; |
| 202 |
case PALM_COMPRESSION_NONE: |
| 203 |
|
| 204 |
*compressionTypeP = COMPRESSION_NONE; |
| 205 |
break; |
| 206 |
default: |
| 207 |
pm_error("The Palm image header has an unrecognized value for " |
| 208 |
"compression type: 0x%02x", (unsigned)compressionValue); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
static void |
| 215 |
readRestOfHeaderVersion3(FILE * const ifP, |
| 216 |
unsigned int const pixelSize, |
| 217 |
unsigned char * const sizeP, |
| 218 |
unsigned char * const pixelFormatP, |
| 219 |
unsigned char * const compressionTypeP, |
| 220 |
short * const densityP, |
| 221 |
unsigned int * const transparentIndexP, |
| 222 |
long * const transparentValueP, |
| 223 |
long * const nextBitmapOffsetP, |
| 224 |
short * const nextDepthOffsetP) { |
| 225 |
|
| 226 |
unsigned char unused; |
| 227 |
|
| 228 |
pm_readcharu(ifP, sizeP); |
| 229 |
|
| 230 |
if (*sizeP != 0x18) |
| 231 |
pm_message("Strange value for Palm bitmap header size: %u", |
| 232 |
(unsigned)*sizeP); |
| 233 |
|
| 234 |
pm_readcharu(ifP, pixelFormatP); |
| 235 |
if (*pixelFormatP != PALM_FORMAT_INDEXED && |
| 236 |
*pixelFormatP != PALM_FORMAT_565) |
| 237 |
pm_error("Unrecognized pixelformat type: %u", *pixelFormatP); |
| 238 |
|
| 239 |
pm_readcharu(ifP, &unused); |
| 240 |
|
| 241 |
|
| 242 |
pm_readcharu(ifP, compressionTypeP); |
| 243 |
|
| 244 |
pm_readbigshort(ifP, densityP); |
| 245 |
|
| 246 |
if (*densityP != PALM_DENSITY_LOW && |
| 247 |
*densityP != PALM_DENSITY_ONEANDAHALF && |
| 248 |
*densityP != PALM_DENSITY_DOUBLE && |
| 249 |
*densityP != PALM_DENSITY_TRIPLE && |
| 250 |
*densityP != PALM_DENSITY_QUADRUPLE) |
| 251 |
pm_error("Invalid value for -density: %d.", *densityP); |
| 252 |
|
| 253 |
pm_readbiglong(ifP, transparentValueP); |
| 254 |
if (pixelSize < 16) |
| 255 |
*transparentIndexP = *transparentValueP; |
| 256 |
else |
| 257 |
*transparentIndexP = 0; |
| 258 |
|
| 259 |
pm_readbiglong(ifP, nextBitmapOffsetP); |
| 260 |
|
| 261 |
|
| 262 |
*nextDepthOffsetP = 0; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
static void |
| 268 |
readRestOfHeaderOld(FILE * const ifP, |
| 269 |
unsigned char * const sizeP, |
| 270 |
unsigned char * const pixelFormatP, |
| 271 |
unsigned char * const compressionTypeP, |
| 272 |
short * const densityP, |
| 273 |
unsigned int * const transparentIndexP, |
| 274 |
long * const transparentValueP, |
| 275 |
long * const nextBitmapOffsetP, |
| 276 |
short * const nextDepthOffsetP) { |
| 277 |
|
| 278 |
short pad; |
| 279 |
unsigned char transparentIndex; |
| 280 |
|
| 281 |
pm_readbigshort(ifP, nextDepthOffsetP); |
| 282 |
pm_readcharu(ifP, &transparentIndex); |
| 283 |
*transparentIndexP = transparentIndex; |
| 284 |
|
| 285 |
pm_readcharu(ifP,compressionTypeP); |
| 286 |
|
| 287 |
pm_readbigshort(ifP, &pad); |
| 288 |
|
| 289 |
|
| 290 |
*sizeP = 0; |
| 291 |
*pixelFormatP = 0; |
| 292 |
*densityP = 0; |
| 293 |
*transparentValueP = 0; |
| 294 |
*nextBitmapOffsetP = 0; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static void |
| 300 |
interpretHeader(struct PalmHeader * const palmHeaderP, |
| 301 |
short const cols, |
| 302 |
short const rows, |
| 303 |
short const bytesPerRow, |
| 304 |
short const flags, |
| 305 |
unsigned char const pixelSizeCode, |
| 306 |
unsigned int const pixelSize, |
| 307 |
unsigned char const version, |
| 308 |
unsigned char const size, |
| 309 |
unsigned char const pixelFormat, |
| 310 |
short const density, |
| 311 |
long const transparentValue, |
| 312 |
unsigned int const transparentIndex, |
| 313 |
unsigned char const compressionType) { |
| 314 |
|
| 315 |
palmHeaderP->cols = cols; |
| 316 |
palmHeaderP->rows = rows; |
| 317 |
palmHeaderP->bytesPerRow = bytesPerRow; |
| 318 |
palmHeaderP->flags = flags; |
| 319 |
palmHeaderP->hasColormap = !!(flags & PALM_HAS_COLORMAP_FLAG); |
| 320 |
palmHeaderP->hasTransparency = !!(flags & PALM_HAS_TRANSPARENCY_FLAG); |
| 321 |
palmHeaderP->pixelSizeCode = pixelSizeCode; |
| 322 |
palmHeaderP->pixelSize = pixelSize; |
| 323 |
palmHeaderP->version = version; |
| 324 |
palmHeaderP->size = size; |
| 325 |
palmHeaderP->pixelFormat = pixelFormat; |
| 326 |
palmHeaderP->density = density; |
| 327 |
palmHeaderP->transparentValue = transparentValue; |
| 328 |
palmHeaderP->transparentIndex = transparentIndex; |
| 329 |
|
| 330 |
if (palmHeaderP->version == 3 && ((flags & PALM_DIRECT_COLOR_FLAG) && |
| 331 |
(pixelFormat != PALM_FORMAT_565))) |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
pm_error("PALM_DIRECT_COLOR_FLAG is set but pixelFormat is not" |
| 336 |
"PALM_FORMAT_565."); |
| 337 |
|
| 338 |
palmHeaderP->directColor = ((flags & PALM_DIRECT_COLOR_FLAG) || |
| 339 |
palmHeaderP->pixelFormat == PALM_FORMAT_565); |
| 340 |
|
| 341 |
if (flags & PALM_IS_COMPRESSED_FLAG) |
| 342 |
interpretCompression(compressionType, |
| 343 |
&palmHeaderP->compressionType); |
| 344 |
else |
| 345 |
palmHeaderP->compressionType = COMPRESSION_NONE; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
static void |
| 351 |
readHeader(FILE * const ifP, |
| 352 |
unsigned int const requestedRendition, |
| 353 |
struct PalmHeader * const palmHeaderP) { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
bool gotHeader; |
| 360 |
unsigned int currentRendition; |
| 361 |
|
| 362 |
gotHeader = FALSE; |
| 363 |
currentRendition = 1; |
| 364 |
while (!gotHeader) { |
| 365 |
short cols, rows, bytesPerRow, flags, nextDepthOffset, density; |
| 366 |
unsigned char pixelSizeCode, version, compressionType, |
| 367 |
size, pixelFormat; |
| 368 |
long transparentValue, nextBitmapOffset; |
| 369 |
unsigned int pixelSize, transparentIndex; |
| 370 |
|
| 371 |
pm_readbigshort(ifP, &cols); |
| 372 |
pm_readbigshort(ifP, &rows); |
| 373 |
pm_readbigshort(ifP, &bytesPerRow); |
| 374 |
pm_readbigshort(ifP, &flags); |
| 375 |
|
| 376 |
pm_readcharu(ifP, &pixelSizeCode); |
| 377 |
pixelSize = pixelSizeCode == 0 ? 1 : pixelSizeCode; |
| 378 |
if (pixelSizeCode != 0x00 && |
| 379 |
pixelSizeCode != 0x01 && |
| 380 |
pixelSizeCode != 0x02 && |
| 381 |
pixelSizeCode != 0x04 && |
| 382 |
pixelSizeCode != 0x08 && |
| 383 |
pixelSizeCode != 0x10 && |
| 384 |
pixelSizeCode != 0xFF) |
| 385 |
pm_error("Invalid value for bits per pixel: %u.", pixelSizeCode); |
| 386 |
|
| 387 |
if ((bytesPerRow * 8) < (cols * pixelSize)) |
| 388 |
pm_error("%u bytes per row is not valid with %u columns and %u " |
| 389 |
"bits per pixel.", bytesPerRow, cols, pixelSize); |
| 390 |
|
| 391 |
pm_readcharu(ifP, &version); |
| 392 |
if (version > 3) |
| 393 |
pm_error("Unknown encoding version type: %d", version); |
| 394 |
else if (version == 3) |
| 395 |
readRestOfHeaderVersion3(ifP, pixelSize, |
| 396 |
&size, &pixelFormat, &compressionType, |
| 397 |
&density, &transparentIndex, |
| 398 |
&transparentValue, &nextBitmapOffset, |
| 399 |
&nextDepthOffset); |
| 400 |
else |
| 401 |
readRestOfHeaderOld(ifP, |
| 402 |
&size, &pixelFormat, &compressionType, |
| 403 |
&density, &transparentIndex, |
| 404 |
&transparentValue, &nextBitmapOffset, |
| 405 |
&nextDepthOffset); |
| 406 |
|
| 407 |
if (currentRendition < requestedRendition) { |
| 408 |
if (version < 3 && nextDepthOffset == 0 && pixelSizeCode != 0xFF) |
| 409 |
pm_error("Not enough renditions in the input Palm Bitmap " |
| 410 |
"to extract the %dth", requestedRendition); |
| 411 |
if (version == 3 && nextBitmapOffset == 0) |
| 412 |
pm_error("Not enough renditions in the input Palm Bitmap " |
| 413 |
"to extract the %dth", requestedRendition); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
if (version < 3 && pixelSizeCode != 0xFF ) |
| 418 |
skipbytes(ifP, (nextDepthOffset*4)-16); |
| 419 |
else if (version == 3) |
| 420 |
|
| 421 |
skipbytes(ifP, (short) nextBitmapOffset-24); |
| 422 |
if (pixelSizeCode != 0xFF) |
| 423 |
++currentRendition; |
| 424 |
} else if (pixelSizeCode != 0xFF) { |
| 425 |
gotHeader = TRUE; |
| 426 |
|
| 427 |
interpretHeader(palmHeaderP, |
| 428 |
cols, rows, bytesPerRow, flags, pixelSizeCode, |
| 429 |
pixelSize, version, size, pixelFormat, density, |
| 430 |
transparentValue, transparentIndex, |
| 431 |
compressionType); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
static const char * |
| 439 |
yesno(bool const arg) { |
| 440 |
|
| 441 |
if (arg) |
| 442 |
return "YES"; |
| 443 |
else |
| 444 |
return "NO"; |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
static void |
| 450 |
reportPalmHeader(struct PalmHeader const palmHeader, |
| 451 |
struct DirectColorInfo const directColorInfo) { |
| 452 |
|
| 453 |
const char *ctype; |
| 454 |
|
| 455 |
switch (palmHeader.compressionType) { |
| 456 |
case COMPRESSION_RLE: |
| 457 |
ctype = "rle (Palm OS 3.5)"; |
| 458 |
break; |
| 459 |
case COMPRESSION_SCANLINE: |
| 460 |
ctype = "scanline (Palm OS 2.0)"; |
| 461 |
break; |
| 462 |
case COMPRESSION_PACKBITS: |
| 463 |
ctype = "packbits (Palm OS 4.0)"; |
| 464 |
break; |
| 465 |
case COMPRESSION_NONE: |
| 466 |
ctype = "none"; |
| 467 |
break; |
| 468 |
} |
| 469 |
pm_message("Dimensions: %hu columns x %hu rows", |
| 470 |
palmHeader.cols, palmHeader.rows); |
| 471 |
pm_message("Row layout: %hu bytes per row, %u bits per pixel", |
| 472 |
palmHeader.bytesPerRow, palmHeader.pixelSize); |
| 473 |
pm_message("Pixel Size code: %u", (unsigned)palmHeader.pixelSizeCode); |
| 474 |
pm_message("Flags: 0x%04hx", palmHeader.flags); |
| 475 |
pm_message(" Direct Color: %s", yesno(palmHeader.directColor)); |
| 476 |
pm_message(" Colormap: %s", yesno(palmHeader.hasColormap)); |
| 477 |
pm_message(" Transparency: %s", yesno(palmHeader.hasTransparency)); |
| 478 |
pm_message("Version %d", palmHeader.version); |
| 479 |
if (palmHeader.hasTransparency) { |
| 480 |
if (palmHeader.directColor) { |
| 481 |
|
| 482 |
ColormapEntry const color = directColorInfo.transparentColor; |
| 483 |
pm_message("Transparent value: #%02x%02x%02x", |
| 484 |
(unsigned int)((color >> 16) & 0xFF), |
| 485 |
(unsigned int)((color >> 8) & 0xFF), |
| 486 |
(unsigned int)((color >> 0) & 0xFF)); |
| 487 |
} else |
| 488 |
pm_message("Transparent index: %u", palmHeader.transparentIndex); |
| 489 |
} |
| 490 |
pm_message("Compression type: %s", ctype); |
| 491 |
if (palmHeader.version == 3) |
| 492 |
pm_message("Density: %d", palmHeader.density); |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
static void |
| 498 |
determineOutputFormat(struct PalmHeader const palmHeader, |
| 499 |
int * const formatP, |
| 500 |
xelval * const maxvalP) { |
| 501 |
|
| 502 |
if (palmHeader.directColor) { |
| 503 |
*formatP = PPM_TYPE; |
| 504 |
*maxvalP = 255; |
| 505 |
} else if (palmHeader.hasColormap) { |
| 506 |
*formatP = PPM_TYPE; |
| 507 |
*maxvalP = 255; |
| 508 |
} else if (palmHeader.pixelSize == 1) { |
| 509 |
*formatP = PBM_TYPE; |
| 510 |
*maxvalP = 1; |
| 511 |
} else if (palmHeader.pixelSize >= 8) { |
| 512 |
*formatP = PPM_TYPE; |
| 513 |
*maxvalP = pm_bitstomaxval(palmHeader.pixelSize); |
| 514 |
} else { |
| 515 |
*formatP = PGM_TYPE; |
| 516 |
*maxvalP = pm_bitstomaxval(palmHeader.pixelSize); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static void |
| 523 |
readRgbFormat(FILE * const ifP, |
| 524 |
struct DirectPixelFormat * const pixelFormatP) { |
| 525 |
|
| 526 |
unsigned char r, g, b; |
| 527 |
|
| 528 |
pm_readcharu(ifP, &r); |
| 529 |
pm_readcharu(ifP, &g); |
| 530 |
pm_readcharu(ifP, &b); |
| 531 |
|
| 532 |
if (r != 5 || g != 6 || b != 5) |
| 533 |
pm_error("This image has a direct color pixel format of " |
| 534 |
"%u red, %u green, %u blue bits. This program " |
| 535 |
"can handle only 5, 6, 5.", r, g, b); |
| 536 |
else { |
| 537 |
pixelFormatP->redbits = r; |
| 538 |
pixelFormatP->greenbits = g; |
| 539 |
pixelFormatP->bluebits = b; |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
static void |
| 546 |
readDirectTransparentColor(FILE * const ifP, |
| 547 |
ColormapEntry * const colorP) { |
| 548 |
|
| 549 |
unsigned char r, g, b; |
| 550 |
|
| 551 |
pm_readcharu(ifP, &r); |
| 552 |
pm_readcharu(ifP, &g); |
| 553 |
pm_readcharu(ifP, &b); |
| 554 |
|
| 555 |
*colorP = (r << 16) | (g << 8) | (b << 0); |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static void |
| 561 |
readDirectInfoType(FILE * const ifP, |
| 562 |
struct PalmHeader const palmHeader, |
| 563 |
struct DirectColorInfo * const directInfoTypeP) { |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
if ((palmHeader.directColor) && palmHeader.pixelSize != 16) |
| 575 |
pm_error("The image is of the direct color type, but has %u " |
| 576 |
"bits per pixel. The only kind of direct color images " |
| 577 |
"this program understands are 16 bit ones.", |
| 578 |
palmHeader.pixelSize); |
| 579 |
|
| 580 |
if (palmHeader.version == 3) { |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
} else { |
| 585 |
if (palmHeader.directColor) { |
| 586 |
unsigned char padding; |
| 587 |
|
| 588 |
readRgbFormat(ifP, &directInfoTypeP->pixelFormat); |
| 589 |
|
| 590 |
pm_readcharu(ifP, &padding); |
| 591 |
pm_readcharu(ifP, &padding); |
| 592 |
|
| 593 |
readDirectTransparentColor(ifP, |
| 594 |
&directInfoTypeP->transparentColor); |
| 595 |
} else { |
| 596 |
|
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
static void |
| 604 |
readColormap(FILE * const ifP, |
| 605 |
struct PalmHeader const palmHeader, |
| 606 |
Colormap ** const colormapPP) { |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
if (palmHeader.hasColormap) |
| 614 |
*colormapPP = palmcolor_read_colormap(ifP); |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
static void |
| 620 |
getColorInfo(struct PalmHeader const palmHeader, |
| 621 |
struct DirectColorInfo const directInfoType, |
| 622 |
Colormap * const colormapFromImageP, |
| 623 |
Colormap ** const colormapPP, |
| 624 |
unsigned int * const ncolorsP, |
| 625 |
struct DirectColorInfo * const directColorInfoP) { |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
if (palmHeader.version == 3 && palmHeader.directColor) { |
| 635 |
*colormapPP = NULL; |
| 636 |
|
| 637 |
assert(palmHeader.pixelFormat == PALM_FORMAT_565); |
| 638 |
|
| 639 |
directColorInfoP->pixelFormat.redbits = 5; |
| 640 |
directColorInfoP->pixelFormat.greenbits = 6; |
| 641 |
directColorInfoP->pixelFormat.bluebits = 5; |
| 642 |
directColorInfoP->transparentColor = |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
((((palmHeader.transparentValue >> 11) & 0x1F) * 255 / 0x1F) |
| 649 |
<< 16) | |
| 650 |
((((palmHeader.transparentValue >> 5) & 0x3F) * 255 / 0x3F) |
| 651 |
<< 8) | |
| 652 |
((((palmHeader.transparentValue >> 0) & 0x1F) * 255 / 0x1F) |
| 653 |
<< 0); |
| 654 |
} else if (palmHeader.directColor) { |
| 655 |
*colormapPP = NULL; |
| 656 |
*directColorInfoP = directInfoType; |
| 657 |
} else if (palmHeader.hasColormap) |
| 658 |
*colormapPP = colormapFromImageP; |
| 659 |
else if (palmHeader.pixelSize >= 8) { |
| 660 |
Colormap * const colormapP = |
| 661 |
palmcolor_build_default_8bit_colormap(); |
| 662 |
qsort(colormapP->color_entries, colormapP->ncolors, |
| 663 |
sizeof(ColormapEntry), palmcolor_compare_indices); |
| 664 |
*colormapPP = colormapP; |
| 665 |
} else |
| 666 |
*colormapPP = NULL; |
| 667 |
|
| 668 |
*ncolorsP = 1 << palmHeader.pixelSize; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
static void |
| 674 |
doTransparent(FILE * const ofP, |
| 675 |
bool const hasTransparency, |
| 676 |
bool const directColor, |
| 677 |
unsigned char const transparentIndex, |
| 678 |
unsigned char const pixelSize, |
| 679 |
Colormap * const colormapP, |
| 680 |
struct DirectColorInfo const directColorInfo) { |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
if (hasTransparency) { |
| 690 |
if (colormapP) { |
| 691 |
ColormapEntry const searchTarget = transparentIndex << 24; |
| 692 |
ColormapEntry * const foundEntryP = |
| 693 |
bsearch(&searchTarget, |
| 694 |
colormapP->color_entries, |
| 695 |
colormapP->ncolors, |
| 696 |
sizeof(searchTarget), |
| 697 |
palmcolor_compare_indices); |
| 698 |
if (!foundEntryP) |
| 699 |
pm_error("Invalid input; transparent index %u " |
| 700 |
"is not among the %u colors in the image's colormap", |
| 701 |
transparentIndex, colormapP->ncolors); |
| 702 |
|
| 703 |
fprintf(ofP, "#%02x%02x%02x\n", |
| 704 |
(unsigned int) ((*foundEntryP >> 16) & 0xFF), |
| 705 |
(unsigned int) ((*foundEntryP >> 8) & 0xFF), |
| 706 |
(unsigned int) ((*foundEntryP >> 0) & 0xFF)); |
| 707 |
} else if (directColor) { |
| 708 |
ColormapEntry const color = directColorInfo.transparentColor; |
| 709 |
fprintf(ofP, "#%02x%02x%02x\n", |
| 710 |
(unsigned int)((color >> 16) & 0xFF), |
| 711 |
(unsigned int)((color >> 8) & 0xFF), |
| 712 |
(unsigned int)((color >> 0) & 0xFF)); |
| 713 |
} else { |
| 714 |
unsigned int const maxval = pm_bitstomaxval(pixelSize); |
| 715 |
unsigned int const grayval = |
| 716 |
((maxval - transparentIndex) * 256) / maxval; |
| 717 |
fprintf(ofP, "#%02x%02x%02x\n", grayval, grayval, grayval); |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
static void |
| 725 |
createHistogram(unsigned int const ncolors, |
| 726 |
unsigned int ** const seenP) { |
| 727 |
|
| 728 |
unsigned int * seen; |
| 729 |
|
| 730 |
MALLOCARRAY(seen, ncolors); |
| 731 |
if (!seen) |
| 732 |
pm_error("Can't allocate array for keeping track of " |
| 733 |
"how many pixels of each of %u colors are in the image.", |
| 734 |
ncolors); |
| 735 |
|
| 736 |
{ |
| 737 |
|
| 738 |
unsigned int i; |
| 739 |
for (i = 0; i < ncolors; ++i) |
| 740 |
seen[i] = 0; |
| 741 |
} |
| 742 |
*seenP = seen; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
static void |
| 748 |
readScanlineRow(FILE * const ifP, |
| 749 |
unsigned char * const palmrow, |
| 750 |
unsigned char * const lastrow, |
| 751 |
unsigned int const bytesPerRow, |
| 752 |
bool const firstRow) { |
| 753 |
|
| 754 |
unsigned int j; |
| 755 |
|
| 756 |
for (j = 0; j < bytesPerRow; j += 8) { |
| 757 |
unsigned char diffmask; |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
unsigned int byteCount; |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
unsigned int k; |
| 768 |
|
| 769 |
pm_readcharu(ifP, &diffmask); |
| 770 |
byteCount = MIN(bytesPerRow - j, 8); |
| 771 |
|
| 772 |
for (k = 0; k < byteCount; ++k) { |
| 773 |
|
| 774 |
if (firstRow || ((diffmask & (1 << (7 - k))) != 0)) { |
| 775 |
unsigned char inval; |
| 776 |
pm_readcharu(ifP, &inval); |
| 777 |
palmrow[j + k] = inval; |
| 778 |
} else |
| 779 |
palmrow[j + k] = lastrow[j + k]; |
| 780 |
} |
| 781 |
} |
| 782 |
memcpy(lastrow, palmrow, bytesPerRow); |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
static void |
| 788 |
readRleRow(FILE * const ifP, |
| 789 |
unsigned char * const palmrow, |
| 790 |
unsigned int const bytesPerRow) { |
| 791 |
|
| 792 |
unsigned int j; |
| 793 |
|
| 794 |
for (j = 0; j < bytesPerRow; ) { |
| 795 |
unsigned char incount; |
| 796 |
unsigned char inval; |
| 797 |
|
| 798 |
pm_readcharu(ifP, &incount); |
| 799 |
if (incount == 0) |
| 800 |
pm_error("Invalid (zero) count in RLE compression."); |
| 801 |
if (j + incount > bytesPerRow) |
| 802 |
pm_error("Invalid Palm image input. Header says %u bytes " |
| 803 |
"per row after uncompressing from RLE, " |
| 804 |
"but we encountered a row with a run length of %u bytes " |
| 805 |
"that pushes the bytes in the row up to %u bytes " |
| 806 |
"(and we didn't look at the rest of the row)", |
| 807 |
bytesPerRow, incount, j + incount); |
| 808 |
pm_readcharu(ifP, &inval); |
| 809 |
memset(palmrow + j, inval, incount); |
| 810 |
j += incount; |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
static void |
| 817 |
readPackBitsRow16(FILE * const ifP, |
| 818 |
unsigned char * const palmrow, |
| 819 |
unsigned int const bytesPerRow) { |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
unsigned int j; |
| 836 |
|
| 837 |
for (j = 0; j < bytesPerRow; ) { |
| 838 |
unsigned char controlByte; |
| 839 |
unsigned int controlNum; |
| 840 |
|
| 841 |
pm_readcharu(ifP, &controlByte); |
| 842 |
controlNum = (unsigned int)controlByte; |
| 843 |
|
| 844 |
if (controlNum >= 128) { |
| 845 |
|
| 846 |
unsigned int const runlength = (257 - controlNum) * 2; |
| 847 |
|
| 848 |
unsigned short inval; |
| 849 |
|
| 850 |
pm_readlittleshortu(ifP, &inval); |
| 851 |
|
| 852 |
if (j + runlength <= bytesPerRow) { |
| 853 |
unsigned int k; |
| 854 |
for (k = 0; k < runlength; k += 2) |
| 855 |
memcpy(palmrow + j + k, &inval, 2); |
| 856 |
} |
| 857 |
j += runlength; |
| 858 |
} else { |
| 859 |
|
| 860 |
|
| 861 |
unsigned int const nonrunlength = (controlNum + 1) * 2; |
| 862 |
|
| 863 |
unsigned int k; |
| 864 |
|
| 865 |
for (k = 0; (k < nonrunlength) && (j + k <= bytesPerRow); ++k) { |
| 866 |
unsigned char inval; |
| 867 |
pm_readcharu(ifP, &inval); |
| 868 |
palmrow[j + k] = inval; |
| 869 |
} |
| 870 |
j += nonrunlength; |
| 871 |
} |
| 872 |
if (j > bytesPerRow) |
| 873 |
pm_error("Invalid Palm image input. Header says %u bytes " |
| 874 |
"per row after uncompressing from 16-bit Packbits at, " |
| 875 |
"but we counted %u bytes in a row, " |
| 876 |
"before we stopped processing the row", |
| 877 |
bytesPerRow, j); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
static void |
| 884 |
readPackBitsRow(FILE * const ifP, |
| 885 |
unsigned char * const palmrow, |
| 886 |
unsigned int const bytesPerRow) { |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
unsigned int j; |
| 892 |
|
| 893 |
for (j = 0; j < bytesPerRow; ) { |
| 894 |
unsigned char controlByte; |
| 895 |
unsigned int controlNum; |
| 896 |
|
| 897 |
pm_readcharu(ifP, &controlByte); |
| 898 |
controlNum = controlByte; |
| 899 |
|
| 900 |
if (controlNum >= 128) { |
| 901 |
unsigned int const runlength = 257 - controlNum; |
| 902 |
|
| 903 |
unsigned char inval; |
| 904 |
|
| 905 |
pm_readcharu(ifP, &inval); |
| 906 |
if (j + runlength <= bytesPerRow) |
| 907 |
memset(palmrow + j, inval, runlength); |
| 908 |
j += runlength; |
| 909 |
} else { |
| 910 |
unsigned int const nonrunlength = controlNum + 1; |
| 911 |
|
| 912 |
unsigned int k; |
| 913 |
|
| 914 |
for (k = 0; k < nonrunlength && j + k <= bytesPerRow; ++k) { |
| 915 |
unsigned char inval; |
| 916 |
pm_readcharu(ifP, &inval); |
| 917 |
palmrow[j + k] = inval; |
| 918 |
} |
| 919 |
j += nonrunlength; |
| 920 |
} |
| 921 |
if (j > bytesPerRow) |
| 922 |
pm_error("Invalid Palm image input. Header says %u bytes " |
| 923 |
"per row after uncompressing from 8-bit Packbits, " |
| 924 |
"but we counted %u bytes in a row, " |
| 925 |
"before we stopped processing the row", |
| 926 |
bytesPerRow, j); |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
static void |
| 933 |
readUncompressedRow(FILE * const ifP, |
| 934 |
unsigned char * const palmrow, |
| 935 |
unsigned int const bytesPerRow) { |
| 936 |
|
| 937 |
int bytesRead; |
| 938 |
|
| 939 |
bytesRead = fread(palmrow, 1, bytesPerRow, ifP); |
| 940 |
if (bytesRead != bytesPerRow) |
| 941 |
pm_error("Error reading Palm file. Short read."); |
| 942 |
} |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
static void |
| 947 |
readDecompressedRow(FILE * const ifP, |
| 948 |
unsigned char * const palmrow, |
| 949 |
unsigned char * const lastrow, |
| 950 |
enum PalmCompressionType const compressionType, |
| 951 |
unsigned int const bytesPerRow, |
| 952 |
unsigned int const pixelSize, |
| 953 |
bool const firstRow) { |
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
switch (compressionType) { |
| 974 |
case COMPRESSION_RLE: |
| 975 |
readRleRow(ifP, palmrow, bytesPerRow); |
| 976 |
break; |
| 977 |
case COMPRESSION_SCANLINE: |
| 978 |
readScanlineRow(ifP, palmrow, lastrow, bytesPerRow, firstRow); |
| 979 |
break; |
| 980 |
case COMPRESSION_PACKBITS: |
| 981 |
if (pixelSize != 16) |
| 982 |
readPackBitsRow(ifP, palmrow, bytesPerRow); |
| 983 |
else |
| 984 |
readPackBitsRow16(ifP, palmrow, bytesPerRow); |
| 985 |
break; |
| 986 |
case COMPRESSION_NONE: |
| 987 |
readUncompressedRow(ifP, palmrow, bytesPerRow); |
| 988 |
break; |
| 989 |
} |
| 990 |
} |
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
static void |
| 995 |
convertRowToPnmDirect(const unsigned char * const palmrow, |
| 996 |
xel * const xelrow, |
| 997 |
unsigned int const cols, |
| 998 |
xelval const maxval, |
| 999 |
unsigned int * const seen) { |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
const unsigned char *inbyte; |
| 1019 |
unsigned int j; |
| 1020 |
|
| 1021 |
for (inbyte = palmrow, j = 0; j < cols; ++j) { |
| 1022 |
unsigned int inval; |
| 1023 |
inval = *inbyte++ << 8; |
| 1024 |
inval |= *inbyte++; |
| 1025 |
|
| 1026 |
if (seen) |
| 1027 |
++seen[inval]; |
| 1028 |
|
| 1029 |
PPM_ASSIGN(xelrow[j], |
| 1030 |
(((inval >> 11) & 0x1F) * maxval) / 0x1F, |
| 1031 |
(((inval >> 5) & 0x3F) * maxval) / 0x3F, |
| 1032 |
(((inval >> 0) & 0x1F) * maxval) / 0x1F |
| 1033 |
); |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
static void |
| 1040 |
convertRowToPnmNotDirect(const unsigned char * const palmrow, |
| 1041 |
xel * const xelrow, |
| 1042 |
unsigned int const cols, |
| 1043 |
Colormap * const colormapP, |
| 1044 |
xelval * const graymap, |
| 1045 |
unsigned int * const seen, |
| 1046 |
unsigned int const pixelSize) { |
| 1047 |
|
| 1048 |
unsigned int const mask = (1 << pixelSize) - 1; |
| 1049 |
|
| 1050 |
const unsigned char * inbyteP; |
| 1051 |
unsigned int inbit; |
| 1052 |
unsigned int j; |
| 1053 |
|
| 1054 |
assert(pixelSize <= 8); |
| 1055 |
|
| 1056 |
inbit = 8 - pixelSize; |
| 1057 |
inbyteP = &palmrow[0]; |
| 1058 |
for (j = 0; j < cols; ++j) { |
| 1059 |
short const color = (*inbyteP & (mask << inbit)) >> inbit; |
| 1060 |
if (seen) |
| 1061 |
++seen[color]; |
| 1062 |
|
| 1063 |
if (colormapP) { |
| 1064 |
ColormapEntry const searchTarget = color << 24; |
| 1065 |
ColormapEntry * const foundEntryP = |
| 1066 |
bsearch(&searchTarget, |
| 1067 |
colormapP->color_entries, |
| 1068 |
colormapP->ncolors, |
| 1069 |
sizeof(searchTarget), |
| 1070 |
palmcolor_compare_indices); |
| 1071 |
|
| 1072 |
if (!foundEntryP) |
| 1073 |
pm_error("Invalid input. A color index in column %u " |
| 1074 |
"is %u, which is not among the %u colors " |
| 1075 |
"in the colormap", |
| 1076 |
j, color, colormapP->ncolors); |
| 1077 |
|
| 1078 |
PPM_ASSIGN(xelrow[j], |
| 1079 |
(*foundEntryP >> 16) & 0xFF, |
| 1080 |
(*foundEntryP >> 8) & 0xFF, |
| 1081 |
(*foundEntryP >> 0) & 0xFF); |
| 1082 |
} else |
| 1083 |
PNM_ASSIGN1(xelrow[j], graymap[color]); |
| 1084 |
|
| 1085 |
if (!inbit) { |
| 1086 |
++inbyteP; |
| 1087 |
inbit = 8 - pixelSize; |
| 1088 |
} else |
| 1089 |
inbit -= pixelSize; |
| 1090 |
} |
| 1091 |
} |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
static void |
| 1096 |
writePnm(FILE * const ofP, |
| 1097 |
struct PalmHeader const palmHeader, |
| 1098 |
FILE * const ifP, |
| 1099 |
Colormap * const colormapP, |
| 1100 |
xelval * const graymap, |
| 1101 |
unsigned int const nColors, |
| 1102 |
int const format, |
| 1103 |
xelval const maxval, |
| 1104 |
unsigned int ** const seenP) { |
| 1105 |
|
| 1106 |
int const cols = palmHeader.cols; |
| 1107 |
int const rows = palmHeader.rows; |
| 1108 |
|
| 1109 |
unsigned char * palmrow; |
| 1110 |
unsigned char * lastrow; |
| 1111 |
xel * xelrow; |
| 1112 |
unsigned int * seen; |
| 1113 |
unsigned int row; |
| 1114 |
|
| 1115 |
pnm_writepnminit(ofP, cols, rows, maxval, format, 0); |
| 1116 |
xelrow = pnm_allocrow(cols); |
| 1117 |
|
| 1118 |
|
| 1119 |
MALLOCARRAY_NOFAIL(palmrow, palmHeader.bytesPerRow); |
| 1120 |
MALLOCARRAY_NOFAIL(lastrow, palmHeader.bytesPerRow); |
| 1121 |
|
| 1122 |
if (seenP) { |
| 1123 |
createHistogram(nColors, &seen); |
| 1124 |
*seenP = seen; |
| 1125 |
} else |
| 1126 |
seen = NULL; |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
if (palmHeader.compressionType != COMPRESSION_NONE) { |
| 1132 |
if (palmHeader.version < 3) { |
| 1133 |
short compressedDataSize16; |
| 1134 |
pm_readbigshort(ifP, &compressedDataSize16); |
| 1135 |
} else { |
| 1136 |
long compressedDataSize32; |
| 1137 |
pm_readbiglong(ifP, &compressedDataSize32); |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
for (row = 0; row < rows; ++row) { |
| 1142 |
readDecompressedRow(ifP, palmrow, lastrow, |
| 1143 |
palmHeader.compressionType, |
| 1144 |
palmHeader.bytesPerRow, |
| 1145 |
palmHeader.pixelSize, |
| 1146 |
row == 0); |
| 1147 |
|
| 1148 |
if (palmHeader.directColor) { |
| 1149 |
assert(palmHeader.pixelSize == 16); |
| 1150 |
convertRowToPnmDirect(palmrow, xelrow, cols, maxval, seen); |
| 1151 |
} else |
| 1152 |
convertRowToPnmNotDirect(palmrow, xelrow, cols, colormapP, graymap, |
| 1153 |
seen, palmHeader.pixelSize); |
| 1154 |
|
| 1155 |
pnm_writepnmrow(ofP, xelrow, cols, maxval, format, 0); |
| 1156 |
} |
| 1157 |
free(lastrow); |
| 1158 |
free(palmrow); |
| 1159 |
pnm_freerow(xelrow); |
| 1160 |
} |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
static void |
| 1165 |
showHistogram(unsigned int * const seen, |
| 1166 |
Colormap * const colormapP, |
| 1167 |
const xelval * const graymap, |
| 1168 |
unsigned int const ncolors) { |
| 1169 |
|
| 1170 |
unsigned int colorIndex; |
| 1171 |
|
| 1172 |
for (colorIndex = 0; colorIndex < ncolors; ++colorIndex) { |
| 1173 |
if (!colormapP) |
| 1174 |
pm_message("%.3d -> %.3d: %d", |
| 1175 |
colorIndex, graymap[colorIndex], seen[colorIndex]); |
| 1176 |
else { |
| 1177 |
ColormapEntry const searchTarget = colorIndex << 24; |
| 1178 |
ColormapEntry * const foundEntryP = |
| 1179 |
bsearch(&searchTarget, |
| 1180 |
colormapP->color_entries, |
| 1181 |
colormapP->ncolors, |
| 1182 |
sizeof(searchTarget), |
| 1183 |
palmcolor_compare_indices); |
| 1184 |
if (foundEntryP) |
| 1185 |
pm_message("%.3d -> %ld,%ld,%ld: %d", colorIndex, |
| 1186 |
(*foundEntryP >> 16) & 0xFF, |
| 1187 |
(*foundEntryP >> 8) & 0xFF, |
| 1188 |
(*foundEntryP & 0xFF), seen[colorIndex]); |
| 1189 |
} |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
int |
| 1196 |
main(int argc, const char **argv) { |
| 1197 |
|
| 1198 |
struct CmdlineInfo cmdline; |
| 1199 |
|
| 1200 |
FILE * ifP; |
| 1201 |
struct PalmHeader palmHeader; |
| 1202 |
struct DirectColorInfo directInfoType; |
| 1203 |
Colormap * colormapFromImageP; |
| 1204 |
Colormap * colormapP; |
| 1205 |
struct DirectColorInfo directColorInfo; |
| 1206 |
int format; |
| 1207 |
xelval maxval; |
| 1208 |
unsigned int nColors; |
| 1209 |
|
| 1210 |
pm_proginit(&argc, argv); |
| 1211 |
|
| 1212 |
parseCommandLine(argc, argv, &cmdline); |
| 1213 |
|
| 1214 |
ifP = pm_openr(cmdline.inputFilespec); |
| 1215 |
|
| 1216 |
readHeader(ifP, cmdline.rendition, &palmHeader); |
| 1217 |
|
| 1218 |
readDirectInfoType(ifP, palmHeader, &directInfoType); |
| 1219 |
|
| 1220 |
readColormap(ifP, palmHeader, &colormapFromImageP); |
| 1221 |
|
| 1222 |
determineOutputFormat(palmHeader, &format, &maxval); |
| 1223 |
|
| 1224 |
getColorInfo(palmHeader, directInfoType, colormapFromImageP, |
| 1225 |
&colormapP, &nColors, &directColorInfo); |
| 1226 |
|
| 1227 |
if (cmdline.verbose) |
| 1228 |
reportPalmHeader(palmHeader, directColorInfo); |
| 1229 |
|
| 1230 |
if (cmdline.transparent) |
| 1231 |
doTransparent(stdout, |
| 1232 |
palmHeader.hasTransparency, palmHeader.directColor, |
| 1233 |
palmHeader.transparentIndex, |
| 1234 |
palmHeader.pixelSize, colormapP, directColorInfo); |
| 1235 |
else { |
| 1236 |
unsigned int * seen; |
| 1237 |
xelval * graymap; |
| 1238 |
|
| 1239 |
graymap = createGraymap(nColors, maxval); |
| 1240 |
|
| 1241 |
writePnm(stdout, |
| 1242 |
palmHeader, ifP, colormapP, graymap, nColors, format, maxval, |
| 1243 |
cmdline.showhist ? &seen : NULL); |
| 1244 |
|
| 1245 |
if (cmdline.showhist) |
| 1246 |
showHistogram(seen, colormapP, graymap, nColors); |
| 1247 |
|
| 1248 |
free(graymap); |
| 1249 |
} |
| 1250 |
pm_close(ifP); |
| 1251 |
|
| 1252 |
return 0; |
| 1253 |
} |
| 1254 |
|
| 1255 |
|
| 1256 |
|