| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef PNMTOPNG_WARNING_LEVEL |
| 17 |
# define PNMTOPNG_WARNING_LEVEL 0 |
| 18 |
#endif |
| 19 |
|
| 20 |
|
| 21 |
#include <assert.h> |
| 22 |
#include <math.h> |
| 23 |
#include <float.h> |
| 24 |
#include <png.h> |
| 25 |
#include <zlib.h> |
| 26 |
|
| 27 |
|
| 28 |
#include "pm_c_util.h" |
| 29 |
#include "mallocvar.h" |
| 30 |
#include "nstring.h" |
| 31 |
#include "shhopt.h" |
| 32 |
#include "pam.h" |
| 33 |
#include "pngx.h" |
| 34 |
|
| 35 |
enum AlphaHandling {ALPHA_NONE, ALPHA_ONLY, ALPHA_MIX, ALPHA_IN}; |
| 36 |
|
| 37 |
typedef struct { |
| 38 |
bool needCorrection; |
| 39 |
double gamma; |
| 40 |
} GammaCorrection; |
| 41 |
|
| 42 |
struct CmdlineInfo { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
const char * inputFileName; |
| 47 |
unsigned int verbose; |
| 48 |
enum AlphaHandling alpha; |
| 49 |
const char * background; |
| 50 |
unsigned int gammaSpec; |
| 51 |
float gamma; |
| 52 |
const char * text; |
| 53 |
unsigned int time; |
| 54 |
unsigned int byrow; |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
static bool verbose; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static void |
| 63 |
parseCommandLine(int argc, |
| 64 |
const char ** argv, |
| 65 |
struct CmdlineInfo * cmdlineP ) { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
optEntry * option_def; |
| 77 |
optStruct3 opt; |
| 78 |
|
| 79 |
unsigned int option_def_index; |
| 80 |
|
| 81 |
unsigned int alphaSpec, alphapamSpec, mixSpec, |
| 82 |
backgroundSpec, textSpec; |
| 83 |
|
| 84 |
MALLOCARRAY(option_def, 100); |
| 85 |
|
| 86 |
option_def_index = 0; |
| 87 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 88 |
&cmdlineP->verbose, 0); |
| 89 |
OPTENT3(0, "alpha", OPT_FLAG, NULL, |
| 90 |
&alphaSpec, 0); |
| 91 |
OPTENT3(0, "alphapam", OPT_FLAG, NULL, |
| 92 |
&alphapamSpec, 0); |
| 93 |
OPTENT3(0, "mix", OPT_FLAG, NULL, |
| 94 |
&mixSpec, 0); |
| 95 |
OPTENT3(0, "background", OPT_STRING, &cmdlineP->background, |
| 96 |
&backgroundSpec, 0); |
| 97 |
OPTENT3(0, "gamma", OPT_FLOAT, &cmdlineP->gamma, |
| 98 |
&cmdlineP->gammaSpec, 0); |
| 99 |
OPTENT3(0, "text", OPT_STRING, &cmdlineP->text, |
| 100 |
&textSpec, 0); |
| 101 |
OPTENT3(0, "time", OPT_FLAG, NULL, |
| 102 |
&cmdlineP->time, 0); |
| 103 |
OPTENT3(0, "byrow", OPT_FLAG, NULL, |
| 104 |
&cmdlineP->byrow, 0); |
| 105 |
|
| 106 |
opt.opt_table = option_def; |
| 107 |
opt.short_allowed = FALSE; |
| 108 |
opt.allowNegNum = FALSE; |
| 109 |
|
| 110 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (alphaSpec + mixSpec + alphapamSpec > 1) |
| 115 |
pm_error("You cannot specify more than one of -alpha -alphapam -mix"); |
| 116 |
else if (alphaSpec) |
| 117 |
cmdlineP->alpha = ALPHA_ONLY; |
| 118 |
else if (mixSpec) |
| 119 |
cmdlineP->alpha = ALPHA_MIX; |
| 120 |
else if (alphapamSpec) |
| 121 |
cmdlineP->alpha = ALPHA_IN; |
| 122 |
else |
| 123 |
cmdlineP->alpha = ALPHA_NONE; |
| 124 |
|
| 125 |
if (backgroundSpec && !mixSpec) |
| 126 |
pm_error("-background is useless without -mix"); |
| 127 |
|
| 128 |
if (!backgroundSpec) |
| 129 |
cmdlineP->background = NULL; |
| 130 |
|
| 131 |
if (!textSpec) |
| 132 |
cmdlineP->text = NULL; |
| 133 |
|
| 134 |
if (argc-1 < 1) |
| 135 |
cmdlineP->inputFileName = "-"; |
| 136 |
else if (argc-1 == 1) |
| 137 |
cmdlineP->inputFileName = argv[1]; |
| 138 |
else |
| 139 |
pm_error("Program takes at most one argument: input file name. " |
| 140 |
"you specified %d", argc-1); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
typedef struct { |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
png_uint_16 r; |
| 153 |
png_uint_16 g; |
| 154 |
png_uint_16 b; |
| 155 |
} pngcolor; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
static pngcolor |
| 160 |
pngcolorFrom16(png_color_16 const arg) { |
| 161 |
|
| 162 |
pngcolor retval; |
| 163 |
|
| 164 |
retval.r = arg.red; |
| 165 |
retval.g = arg.green; |
| 166 |
retval.b = arg.blue; |
| 167 |
|
| 168 |
return retval; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
static pngcolor |
| 174 |
pngcolorFromByte(png_color const arg) { |
| 175 |
|
| 176 |
pngcolor retval; |
| 177 |
|
| 178 |
retval.r = arg.red; |
| 179 |
retval.g = arg.green; |
| 180 |
retval.b = arg.blue; |
| 181 |
|
| 182 |
return retval; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
static bool |
| 188 |
pngColorEqual(pngcolor const comparand, |
| 189 |
pngcolor const comparator) { |
| 190 |
|
| 191 |
return (comparand.r == comparator.r |
| 192 |
&& comparand.g == comparator.g |
| 193 |
&& comparand.b == comparator.b); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
static png_uint_16 |
| 199 |
gammaCorrect(png_uint_16 const uncorrected, |
| 200 |
GammaCorrection const gamma, |
| 201 |
png_uint_16 const maxval) { |
| 202 |
|
| 203 |
if (gamma.needCorrection) { |
| 204 |
double const uncorrectedN = (double) uncorrected / maxval; |
| 205 |
return (png_uint_16) |
| 206 |
ROUNDU(pow(uncorrectedN, (1.0 / gamma.gamma)) * maxval); |
| 207 |
} else |
| 208 |
return uncorrected; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static pngcolor |
| 214 |
gammaCorrectColor(pngcolor const color, |
| 215 |
GammaCorrection const gamma, |
| 216 |
png_uint_16 const maxval) { |
| 217 |
|
| 218 |
pngcolor retval; |
| 219 |
|
| 220 |
retval.r = gammaCorrect(color.r, gamma, maxval); |
| 221 |
|
| 222 |
return retval; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static unsigned int |
| 228 |
computePngLineSize(struct pngx * const pngxP) { |
| 229 |
|
| 230 |
unsigned int const bytesPerSample = |
| 231 |
pngx_bitDepth(pngxP) == 16 ? 2 : 1; |
| 232 |
|
| 233 |
unsigned int samplesPerPixel; |
| 234 |
|
| 235 |
switch (pngx_colorType(pngxP)) { |
| 236 |
case PNG_COLOR_TYPE_GRAY_ALPHA: samplesPerPixel = 2; break; |
| 237 |
case PNG_COLOR_TYPE_RGB: samplesPerPixel = 3; break; |
| 238 |
case PNG_COLOR_TYPE_RGB_ALPHA: samplesPerPixel = 4; break; |
| 239 |
default: samplesPerPixel = 1; |
| 240 |
} |
| 241 |
|
| 242 |
if (UINT_MAX / bytesPerSample / samplesPerPixel < pngx_imageWidth(pngxP)) |
| 243 |
pm_error("Width %u of PNG is uncomputably large", |
| 244 |
pngx_imageWidth(pngxP)); |
| 245 |
|
| 246 |
return pngx_imageWidth(pngxP) * bytesPerSample * samplesPerPixel; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
static void |
| 252 |
allocPngRaster(struct pngx * const pngxP, |
| 253 |
png_byte *** const pngImageP) { |
| 254 |
|
| 255 |
unsigned int const lineSize = computePngLineSize(pngxP); |
| 256 |
|
| 257 |
png_byte ** pngImage; |
| 258 |
unsigned int row; |
| 259 |
|
| 260 |
MALLOCARRAY(pngImage, pngx_imageHeight(pngxP)); |
| 261 |
|
| 262 |
if (pngImage == NULL) |
| 263 |
pm_error("couldn't allocate index space for %u PNG raster rows. " |
| 264 |
"Try -byrow, which needs only 1 row of buffer space. ", |
| 265 |
pngx_imageHeight(pngxP)); |
| 266 |
|
| 267 |
for (row = 0; row < pngx_imageHeight(pngxP); ++row) { |
| 268 |
MALLOCARRAY(pngImage[row], lineSize); |
| 269 |
if (pngImage[row] == NULL) |
| 270 |
pm_error("couldn't allocate space for %uth row of PNG raster. " |
| 271 |
"Try -byrow, which needs only 1 row of buffer space. ", |
| 272 |
row); |
| 273 |
} |
| 274 |
*pngImageP = pngImage; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
static void |
| 280 |
freePngRaster(png_byte ** const pngRaster, |
| 281 |
struct pngx * const pngxP) { |
| 282 |
|
| 283 |
unsigned int row; |
| 284 |
|
| 285 |
for (row = 0; row < pngx_imageHeight(pngxP); ++row) |
| 286 |
free(pngRaster[row]); |
| 287 |
|
| 288 |
free(pngRaster); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
typedef struct { |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
struct pngx * pngxP; |
| 298 |
png_byte ** pngRaster; |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
png_byte * rowBuf; |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
unsigned int nextRowNum; |
| 311 |
|
| 312 |
|
| 313 |
} Reader; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static Reader * |
| 318 |
reader_createAllAtOnce(struct pngx * const pngxP, |
| 319 |
FILE * const ifP) { |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
Reader * readerP; |
| 331 |
|
| 332 |
MALLOCVAR_NOFAIL(readerP); |
| 333 |
|
| 334 |
readerP->pngxP = pngxP; |
| 335 |
|
| 336 |
allocPngRaster(pngxP, &readerP->pngRaster); |
| 337 |
|
| 338 |
readerP->rowBuf = NULL; |
| 339 |
|
| 340 |
pngx_readImage(pngxP, readerP->pngRaster); |
| 341 |
|
| 342 |
readerP->nextRowNum = 0; |
| 343 |
|
| 344 |
return readerP; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
static Reader * |
| 350 |
reader_createRowByRow(struct pngx * const pngxP, |
| 351 |
FILE * const ifP) { |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
Reader * readerP; |
| 360 |
|
| 361 |
MALLOCVAR_NOFAIL(readerP); |
| 362 |
|
| 363 |
readerP->pngxP = pngxP; |
| 364 |
|
| 365 |
readerP->pngRaster = NULL; |
| 366 |
|
| 367 |
MALLOCARRAY(readerP->rowBuf, computePngLineSize(pngxP)); |
| 368 |
|
| 369 |
if (!readerP->rowBuf) |
| 370 |
pm_error("Could not allocate %u bytes for a PNG row buffer", |
| 371 |
computePngLineSize(pngxP)); |
| 372 |
|
| 373 |
readerP->nextRowNum = 0; |
| 374 |
|
| 375 |
if (pngx_interlaceType(pngxP) != PNG_INTERLACE_NONE) |
| 376 |
pm_message("WARNING: this is an interlaced PNG. The PAM output " |
| 377 |
"will be interlaced. To get proper output, " |
| 378 |
"don't use -byrow"); |
| 379 |
|
| 380 |
return readerP; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
static void |
| 386 |
reader_destroy(Reader * const readerP) { |
| 387 |
|
| 388 |
if (readerP->pngRaster) |
| 389 |
freePngRaster(readerP->pngRaster, readerP->pngxP); |
| 390 |
|
| 391 |
if (readerP->rowBuf) |
| 392 |
free(readerP->rowBuf); |
| 393 |
|
| 394 |
free(readerP); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static png_byte * |
| 400 |
reader_read(Reader * const readerP) { |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
png_byte * retval; |
| 408 |
|
| 409 |
if (readerP->pngRaster) { |
| 410 |
if (readerP->nextRowNum >= pngx_imageHeight(readerP->pngxP)) |
| 411 |
retval = NULL; |
| 412 |
else |
| 413 |
retval = readerP->pngRaster[readerP->nextRowNum]; |
| 414 |
} else { |
| 415 |
pngx_readRow(readerP->pngxP, readerP->rowBuf, NULL); |
| 416 |
retval = readerP->rowBuf; |
| 417 |
} |
| 418 |
|
| 419 |
++readerP->nextRowNum; |
| 420 |
|
| 421 |
return retval; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
static png_uint_16 |
| 427 |
getPngVal(const png_byte ** const pp, |
| 428 |
int const bitDepth) { |
| 429 |
|
| 430 |
png_uint_16 c; |
| 431 |
|
| 432 |
if (bitDepth == 16) |
| 433 |
c = *(*pp)++ << 8; |
| 434 |
else |
| 435 |
c = 0; |
| 436 |
|
| 437 |
c |= *(*pp)++; |
| 438 |
|
| 439 |
return c; |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
static bool |
| 445 |
isGrayscale(pngcolor const color) { |
| 446 |
|
| 447 |
return color.r == color.g && color.r == color.b; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
static sample |
| 453 |
alphaMix(png_uint_16 const foreground, |
| 454 |
png_uint_16 const background, |
| 455 |
png_uint_16 const alpha, |
| 456 |
sample const maxval) { |
| 457 |
|
| 458 |
double const opacity = (double)alpha / maxval; |
| 459 |
double const transparency = 1.0 - opacity; |
| 460 |
|
| 461 |
return ROUNDU(foreground * opacity + background * transparency); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
static void |
| 467 |
setTuple(const struct pam * const pamP, |
| 468 |
tuple const tuple, |
| 469 |
pngcolor const foreground, |
| 470 |
pngcolor const background, |
| 471 |
enum AlphaHandling const alphaHandling, |
| 472 |
const struct pngx * const pngxP, |
| 473 |
png_uint_16 const alpha) { |
| 474 |
|
| 475 |
if (alphaHandling == ALPHA_ONLY) |
| 476 |
tuple[0] = alpha; |
| 477 |
else if (alphaHandling == ALPHA_NONE || |
| 478 |
(alphaHandling == ALPHA_MIX && alpha == pngxP->maxval)) { |
| 479 |
if (pamP->depth < 3) |
| 480 |
tuple[0] = foreground.r; |
| 481 |
else { |
| 482 |
tuple[PAM_RED_PLANE] = foreground.r; |
| 483 |
tuple[PAM_GRN_PLANE] = foreground.g; |
| 484 |
tuple[PAM_BLU_PLANE] = foreground.b; |
| 485 |
} |
| 486 |
} else if (alphaHandling == ALPHA_IN) { |
| 487 |
if (pamP->depth < 4) { |
| 488 |
tuple[0] = foreground.r; |
| 489 |
tuple[PAM_GRAY_TRN_PLANE] = alpha; |
| 490 |
} else { |
| 491 |
tuple[PAM_RED_PLANE] = foreground.r; |
| 492 |
tuple[PAM_GRN_PLANE] = foreground.g; |
| 493 |
tuple[PAM_BLU_PLANE] = foreground.b; |
| 494 |
tuple[PAM_TRN_PLANE] = alpha; |
| 495 |
} |
| 496 |
} else { |
| 497 |
assert(alphaHandling == ALPHA_MIX); |
| 498 |
|
| 499 |
if (pamP->depth < 3) |
| 500 |
tuple[0] = |
| 501 |
alphaMix(foreground.r, background.r, alpha, pngxP->maxval); |
| 502 |
else { |
| 503 |
tuple[PAM_RED_PLANE] = |
| 504 |
alphaMix(foreground.r, background.r, alpha, pngxP->maxval); |
| 505 |
tuple[PAM_GRN_PLANE] = |
| 506 |
alphaMix(foreground.g, background.g, alpha, pngxP->maxval); |
| 507 |
tuple[PAM_BLU_PLANE] = |
| 508 |
alphaMix(foreground.b, background.b, alpha, pngxP->maxval); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
static bool |
| 516 |
isColor(png_color const c) { |
| 517 |
|
| 518 |
return c.red != c.green || c.green != c.blue; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
static void |
| 524 |
saveText(struct pngx * const pngxP, |
| 525 |
FILE * const tfP) { |
| 526 |
|
| 527 |
struct pngx_text const text = pngx_text(pngxP); |
| 528 |
|
| 529 |
unsigned int i; |
| 530 |
|
| 531 |
for (i = 0 ; i < text.size; ++i) { |
| 532 |
unsigned int j; |
| 533 |
j = 0; |
| 534 |
|
| 535 |
while (text.line[i].key[j] != '\0' && |
| 536 |
text.line[i].key[j] != ' ') |
| 537 |
++j; |
| 538 |
|
| 539 |
if (text.line[i].key[j] != ' ') { |
| 540 |
fprintf(tfP, "%s", text.line[i].key); |
| 541 |
for (j = strlen (text.line[i].key); j < 15; ++j) |
| 542 |
putc(' ', tfP); |
| 543 |
} else { |
| 544 |
fprintf(tfP, "\"%s\"", text.line[i].key); |
| 545 |
for (j = strlen (text.line[i].key); j < 13; ++j) |
| 546 |
putc(' ', tfP); |
| 547 |
} |
| 548 |
putc(' ', tfP); |
| 549 |
|
| 550 |
for (j = 0; j < text.line[i].text_length; ++j) { |
| 551 |
putc(text.line[i].text[j], tfP); |
| 552 |
if (text.line[i].text[j] == '\n') { |
| 553 |
unsigned int k; |
| 554 |
for (k = 0; k < 16; ++k) |
| 555 |
putc(' ', tfP); |
| 556 |
} |
| 557 |
} |
| 558 |
putc('\n', tfP); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
static void |
| 565 |
showTime(struct pngx * const pngxP) { |
| 566 |
|
| 567 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tIME)) { |
| 568 |
png_time const modTime = pngx_time(pngxP); |
| 569 |
|
| 570 |
static const char * const month[] = { |
| 571 |
"", "January", "February", "March", "April", "May", "June", |
| 572 |
"July", "August", "September", "October", "November", "December" |
| 573 |
}; |
| 574 |
|
| 575 |
if (modTime.month < 1 || modTime.month >= ARRAY_SIZE(month)) { |
| 576 |
pm_message("tIME chunk in PNG input is invalid; " |
| 577 |
"modification time of image is unknown. " |
| 578 |
"The month value, which should be in the range " |
| 579 |
"1-12, is %u", modTime.month); |
| 580 |
} else { |
| 581 |
pm_message("modification time: %02d %s %d %02d:%02d:%02d", |
| 582 |
modTime.day, |
| 583 |
month[modTime.month], |
| 584 |
modTime.year, |
| 585 |
modTime.hour, |
| 586 |
modTime.minute, |
| 587 |
modTime.second); |
| 588 |
} |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
static void |
| 595 |
dumpTypeAndFilter(struct pngx * const pngxP) { |
| 596 |
|
| 597 |
const char * typeString; |
| 598 |
const char * filterString; |
| 599 |
|
| 600 |
switch (pngx_colorType(pngxP)) { |
| 601 |
case PNG_COLOR_TYPE_GRAY: |
| 602 |
typeString = "gray"; |
| 603 |
break; |
| 604 |
|
| 605 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 606 |
typeString = "gray+alpha"; |
| 607 |
break; |
| 608 |
|
| 609 |
case PNG_COLOR_TYPE_PALETTE: |
| 610 |
typeString = "palette"; |
| 611 |
break; |
| 612 |
|
| 613 |
case PNG_COLOR_TYPE_RGB: |
| 614 |
typeString = "truecolor"; |
| 615 |
break; |
| 616 |
|
| 617 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 618 |
typeString = "truecolor+alpha"; |
| 619 |
break; |
| 620 |
} |
| 621 |
|
| 622 |
switch (pngx_filterType(pngxP)) { |
| 623 |
case PNG_FILTER_TYPE_BASE: |
| 624 |
pm_asprintf(&filterString, "base filter"); |
| 625 |
break; |
| 626 |
default: |
| 627 |
pm_asprintf(&filterString, "unknown filter type %d", |
| 628 |
pngx_filterType(pngxP)); |
| 629 |
} |
| 630 |
|
| 631 |
pm_message("%s, %s, %s", |
| 632 |
typeString, |
| 633 |
pngx_interlaceType(pngxP) ? |
| 634 |
"Adam7 interlaced" : "not interlaced", |
| 635 |
filterString); |
| 636 |
|
| 637 |
pm_strfree(filterString); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
static void |
| 643 |
dumpPngInfo(struct pngx * const pngxP) { |
| 644 |
|
| 645 |
pm_message("reading a %u x %u image, %u bit%s", |
| 646 |
pngx_imageWidth(pngxP), |
| 647 |
pngx_imageHeight(pngxP), |
| 648 |
pngx_bitDepth(pngxP), |
| 649 |
pngx_bitDepth(pngxP) > 1 ? "s" : ""); |
| 650 |
|
| 651 |
dumpTypeAndFilter(pngxP); |
| 652 |
|
| 653 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD)) { |
| 654 |
png_color_16 const background = pngx_bkgd(pngxP); |
| 655 |
|
| 656 |
pm_message("background {index, gray, red, green, blue} = " |
| 657 |
"{%d, %d, %d, %d, %d}", |
| 658 |
background.index, |
| 659 |
background.gray, |
| 660 |
background.red, |
| 661 |
background.green, |
| 662 |
background.blue); |
| 663 |
} |
| 664 |
|
| 665 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) { |
| 666 |
struct pngx_trns const trns = pngx_trns(pngxP); |
| 667 |
|
| 668 |
pm_message("tRNS chunk (transparency):"); |
| 669 |
pm_message(" %u palette entries", trns.numTrans); |
| 670 |
pm_message(" transparent color = (%u,%u,%u)", |
| 671 |
trns.transColor.red, |
| 672 |
trns.transColor.green, |
| 673 |
trns.transColor.blue); |
| 674 |
} else |
| 675 |
pm_message("tRNS chunk (transparency): not present"); |
| 676 |
|
| 677 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_gAMA)) |
| 678 |
pm_message("gAMA chunk (image gamma): gamma = %4.2f", |
| 679 |
pngx_gama(pngxP)); |
| 680 |
else |
| 681 |
pm_message("gAMA chunk (image gamma): not present"); |
| 682 |
|
| 683 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT)) |
| 684 |
pm_message("sBIT chunk: present"); |
| 685 |
else |
| 686 |
pm_message("sBIT chunk: not present"); |
| 687 |
|
| 688 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_cHRM)) |
| 689 |
pm_message("cHRM chunk: present"); |
| 690 |
else |
| 691 |
pm_message("cHRM chunk: not present"); |
| 692 |
|
| 693 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_PLTE)) |
| 694 |
pm_message("PLTE chunk: %d entries", pngx_plte(pngxP).size); |
| 695 |
else |
| 696 |
pm_message("PLTE chunk: not present"); |
| 697 |
|
| 698 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD)) |
| 699 |
pm_message("bKGD chunk: present"); |
| 700 |
else |
| 701 |
pm_message("bKGD chunk: not present"); |
| 702 |
|
| 703 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_hIST)) |
| 704 |
pm_message("hIST chunk: present"); |
| 705 |
else |
| 706 |
pm_message("hIST chunk: not present"); |
| 707 |
|
| 708 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_pHYs)) |
| 709 |
pm_message("pHYs chunk: present"); |
| 710 |
else |
| 711 |
pm_message("pHYs chunk: not present"); |
| 712 |
|
| 713 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_oFFs)) |
| 714 |
pm_message("oFFs chunk: present"); |
| 715 |
else |
| 716 |
pm_message("oFFs chunk: not present"); |
| 717 |
|
| 718 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tIME)) |
| 719 |
pm_message("tIME chunk: present"); |
| 720 |
else |
| 721 |
pm_message("tIME chunk: not present"); |
| 722 |
|
| 723 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_pCAL)) |
| 724 |
pm_message("pCAL chunk: present"); |
| 725 |
else |
| 726 |
pm_message("pCAL chunk: not present"); |
| 727 |
|
| 728 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_sRGB)) |
| 729 |
pm_message("sRGB chunk: present"); |
| 730 |
else |
| 731 |
pm_message("sRGB chunk: not present"); |
| 732 |
} |
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
static const png_color_16 |
| 737 |
transColor(struct pngx * const pngxP) { |
| 738 |
|
| 739 |
struct pngx_trns const trans = pngx_trns(pngxP); |
| 740 |
|
| 741 |
assert(pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)); |
| 742 |
|
| 743 |
return trans.transColor; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
static bool |
| 749 |
isTransparentColor(pngcolor const color, |
| 750 |
struct pngx * const pngxP, |
| 751 |
GammaCorrection const gamma) { |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
bool retval; |
| 761 |
|
| 762 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) { |
| 763 |
png_color_16 const transColor16 = transColor(pngxP); |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
switch (pngx_colorType(pngxP)) { |
| 780 |
case PNG_COLOR_TYPE_GRAY: |
| 781 |
retval = color.r == gammaCorrect(transColor16.gray, gamma, |
| 782 |
pngxP->maxval); |
| 783 |
break; |
| 784 |
default: { |
| 785 |
pngcolor const transColor = pngcolorFrom16(transColor16); |
| 786 |
retval = pngColorEqual(color, |
| 787 |
gammaCorrectColor(transColor, gamma, |
| 788 |
pngxP->maxval)); |
| 789 |
} |
| 790 |
} |
| 791 |
} else |
| 792 |
retval = FALSE; |
| 793 |
|
| 794 |
return retval; |
| 795 |
} |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
static void |
| 800 |
setupGammaCorrection(struct pngx * const pngxP, |
| 801 |
bool const screenGammaIsKnown, |
| 802 |
float const screenGamma, |
| 803 |
GammaCorrection * const gammaCorrectionP) { |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
if (!screenGammaIsKnown) |
| 825 |
gammaCorrectionP->needCorrection = false; |
| 826 |
else { |
| 827 |
float imageGamma; |
| 828 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_gAMA)) |
| 829 |
imageGamma = pngx_gama(pngxP); |
| 830 |
else { |
| 831 |
if (verbose) |
| 832 |
pm_message("PNG doesn't specify image gamma. Assuming 1.0"); |
| 833 |
imageGamma = 1.0; |
| 834 |
} |
| 835 |
|
| 836 |
if (fabs(screenGamma * imageGamma - 1.0) < .01) { |
| 837 |
gammaCorrectionP->needCorrection = false; |
| 838 |
if (verbose) |
| 839 |
pm_message("image gamma %4.2f matches " |
| 840 |
"screen gamma %4.2f. No conversion.", |
| 841 |
imageGamma, screenGamma); |
| 842 |
} else { |
| 843 |
pngx_setGamma(pngxP, screenGamma, imageGamma); |
| 844 |
|
| 845 |
gammaCorrectionP->needCorrection = true; |
| 846 |
gammaCorrectionP->gamma = imageGamma * screenGamma; |
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
pngx_removeChunk(pngxP, PNG_INFO_sBIT); |
| 851 |
if (verbose) |
| 852 |
pm_message("image gamma is %4.2f, " |
| 853 |
"display gamma is %4.2f; " |
| 854 |
"combined gamma is %4.2f", |
| 855 |
imageGamma, screenGamma, gammaCorrectionP->gamma); |
| 856 |
} |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
static bool |
| 863 |
paletteHasPartialTransparency(struct pngx * const pngxP) { |
| 864 |
|
| 865 |
bool retval; |
| 866 |
|
| 867 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) { |
| 868 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) { |
| 869 |
struct pngx_trns const trans = pngx_trns(pngxP); |
| 870 |
|
| 871 |
bool foundPartial; |
| 872 |
unsigned int i; |
| 873 |
|
| 874 |
for (i = 0, foundPartial = FALSE; |
| 875 |
i < trans.numTrans && !foundPartial; |
| 876 |
++i) { |
| 877 |
if (trans.trans[i] != 0 && trans.trans[i] != pngxP->maxval) { |
| 878 |
foundPartial = TRUE; |
| 879 |
} |
| 880 |
} |
| 881 |
retval = foundPartial; |
| 882 |
} else |
| 883 |
retval = FALSE; |
| 884 |
} else |
| 885 |
retval = FALSE; |
| 886 |
|
| 887 |
return retval; |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
static void |
| 893 |
getComponentSbitFg(struct pngx * const pngxP, |
| 894 |
png_byte * const fgSbitP, |
| 895 |
bool * const notUniformP) { |
| 896 |
|
| 897 |
png_color_8 const sigBit = pngx_sbit(pngxP); |
| 898 |
|
| 899 |
assert(pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT)); |
| 900 |
|
| 901 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB || |
| 902 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA || |
| 903 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) { |
| 904 |
|
| 905 |
if (sigBit.red == sigBit.blue && |
| 906 |
sigBit.red == sigBit.green) { |
| 907 |
*notUniformP = false; |
| 908 |
*fgSbitP = sigBit.red; |
| 909 |
} else |
| 910 |
*notUniformP = true; |
| 911 |
} else { |
| 912 |
|
| 913 |
*notUniformP = false; |
| 914 |
*fgSbitP = sigBit.gray; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
static void |
| 921 |
getComponentSbit(struct pngx * const pngxP, |
| 922 |
enum AlphaHandling const alphaHandling, |
| 923 |
png_byte * const componentSbitP, |
| 924 |
bool * const notUniformP) { |
| 925 |
|
| 926 |
assert(pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT)); |
| 927 |
|
| 928 |
switch (alphaHandling) { |
| 929 |
|
| 930 |
case ALPHA_ONLY: { |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
*notUniformP = false; |
| 935 |
*componentSbitP = pngx_sbit(pngxP).alpha; |
| 936 |
} break; |
| 937 |
case ALPHA_NONE: |
| 938 |
case ALPHA_MIX: |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
getComponentSbitFg(pngxP, componentSbitP, notUniformP); |
| 943 |
break; |
| 944 |
case ALPHA_IN: { |
| 945 |
|
| 946 |
bool fgNotUniform; |
| 947 |
png_byte fgSbit; |
| 948 |
|
| 949 |
getComponentSbitFg(pngxP, &fgSbit, &fgNotUniform); |
| 950 |
|
| 951 |
if (fgNotUniform) |
| 952 |
*notUniformP = true; |
| 953 |
else { |
| 954 |
if (fgSbit == pngx_sbit(pngxP).alpha) { |
| 955 |
*notUniformP = false; |
| 956 |
*componentSbitP = fgSbit; |
| 957 |
} else |
| 958 |
*notUniformP = true; |
| 959 |
} |
| 960 |
} break; |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
static void |
| 967 |
shiftPalette(struct pngx * const pngxP, |
| 968 |
unsigned int const shift) { |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
if (shift > 7) |
| 974 |
pm_error("Invalid PNG: paletted image can't have " |
| 975 |
"more than 8 significant bits per component, " |
| 976 |
"but sBIT chunk says %u bits", |
| 977 |
shift); |
| 978 |
else { |
| 979 |
struct pngx_plte const palette = pngx_plte(pngxP); |
| 980 |
|
| 981 |
unsigned int i; |
| 982 |
|
| 983 |
for (i = 0; i < palette.size; ++i) { |
| 984 |
palette.palette[i].red >>= (8 - shift); |
| 985 |
palette.palette[i].green >>= (8 - shift); |
| 986 |
palette.palette[i].blue >>= (8 - shift); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
static void |
| 994 |
computeMaxvalFromSbit(struct pngx * const pngxP, |
| 995 |
enum AlphaHandling const alphaHandling, |
| 996 |
png_uint_16 * const maxvalP, |
| 997 |
bool * const succeededP, |
| 998 |
int * const errorLevelP) { |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
bool notUniform; |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
png_byte componentSigBit; |
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
getComponentSbit(pngxP, alphaHandling, &componentSigBit, ¬Uniform); |
| 1020 |
|
| 1021 |
if (notUniform) { |
| 1022 |
pm_message("This program cannot handle " |
| 1023 |
"different bit depths for color channels"); |
| 1024 |
pm_message("writing file with %u bit resolution", |
| 1025 |
pngx_bitDepth(pngxP)); |
| 1026 |
*succeededP = false; |
| 1027 |
*errorLevelP = PNMTOPNG_WARNING_LEVEL; |
| 1028 |
} else if (componentSigBit > 15) { |
| 1029 |
pm_message("Invalid PNG: says %u significant bits for a component; " |
| 1030 |
"max possible is 16. Ignoring sBIT chunk.", |
| 1031 |
componentSigBit); |
| 1032 |
*succeededP = false; |
| 1033 |
*errorLevelP = PNMTOPNG_WARNING_LEVEL; |
| 1034 |
} else { |
| 1035 |
if (alphaHandling == ALPHA_MIX && |
| 1036 |
(pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA || |
| 1037 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 1038 |
paletteHasPartialTransparency(pngxP))) |
| 1039 |
*succeededP = false; |
| 1040 |
else { |
| 1041 |
if (componentSigBit < pngx_bitDepth(pngxP)) { |
| 1042 |
pm_message("Image has fewer significant bits, " |
| 1043 |
"writing file with %u bits", componentSigBit); |
| 1044 |
*maxvalP = (1l << componentSigBit) - 1; |
| 1045 |
*succeededP = true; |
| 1046 |
|
| 1047 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) |
| 1048 |
shiftPalette(pngxP, componentSigBit); |
| 1049 |
else |
| 1050 |
pngx_setShift(pngxP, pngx_sbit(pngxP)); |
| 1051 |
} else |
| 1052 |
*succeededP = false; |
| 1053 |
} |
| 1054 |
} |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
static void |
| 1060 |
setupSignificantBits(struct pngx * const pngxP, |
| 1061 |
enum AlphaHandling const alphaHandling, |
| 1062 |
int * const errorLevelP) { |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
bool gotItFromSbit; |
| 1073 |
|
| 1074 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_sBIT)) |
| 1075 |
computeMaxvalFromSbit(pngxP, alphaHandling, |
| 1076 |
&pngxP->maxval, &gotItFromSbit, errorLevelP); |
| 1077 |
else |
| 1078 |
gotItFromSbit = false; |
| 1079 |
|
| 1080 |
if (!gotItFromSbit) { |
| 1081 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) { |
| 1082 |
if (alphaHandling == ALPHA_ONLY) { |
| 1083 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY || |
| 1084 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB) |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
pngxP->maxval = 1; |
| 1089 |
else if (paletteHasPartialTransparency(pngxP)) |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
pngxP->maxval = 255; |
| 1094 |
else |
| 1095 |
|
| 1096 |
pngxP->maxval = 1; |
| 1097 |
} else |
| 1098 |
|
| 1099 |
pngxP->maxval = 255; |
| 1100 |
} else { |
| 1101 |
pngxP->maxval = (1l << pngx_bitDepth(pngxP)) - 1; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
static bool |
| 1109 |
imageHasColor(struct pngx * const pngxP) { |
| 1110 |
|
| 1111 |
bool retval; |
| 1112 |
|
| 1113 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY || |
| 1114 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 1115 |
|
| 1116 |
retval = FALSE; |
| 1117 |
else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_PALETTE) { |
| 1118 |
struct pngx_plte const palette = pngx_plte(pngxP); |
| 1119 |
|
| 1120 |
bool foundColor; |
| 1121 |
unsigned int i; |
| 1122 |
|
| 1123 |
for (i = 0, foundColor = FALSE; |
| 1124 |
i < palette.size && !foundColor; |
| 1125 |
++i) { |
| 1126 |
if (isColor(palette.palette[i])) |
| 1127 |
foundColor = TRUE; |
| 1128 |
} |
| 1129 |
retval = foundColor; |
| 1130 |
} else |
| 1131 |
retval = TRUE; |
| 1132 |
|
| 1133 |
return retval; |
| 1134 |
} |
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
static void |
| 1139 |
determineOutputType(struct pngx * const pngxP, |
| 1140 |
enum AlphaHandling const alphaHandling, |
| 1141 |
pngcolor const bgColor, |
| 1142 |
xelval const maxval, |
| 1143 |
int * const formatP, |
| 1144 |
unsigned int * const depthP, |
| 1145 |
char * const tupleType) { |
| 1146 |
|
| 1147 |
if (alphaHandling == ALPHA_ONLY) { |
| 1148 |
|
| 1149 |
*depthP = 1; |
| 1150 |
*formatP = maxval > 1 ? PGM_FORMAT : PBM_FORMAT; |
| 1151 |
} else { |
| 1152 |
|
| 1153 |
bool const outputIsColor = |
| 1154 |
imageHasColor(pngxP) || !isGrayscale(bgColor); |
| 1155 |
|
| 1156 |
if (alphaHandling == ALPHA_IN) { |
| 1157 |
*formatP = PAM_FORMAT; |
| 1158 |
if (outputIsColor) { |
| 1159 |
*depthP = 4; |
| 1160 |
strcpy(tupleType, "RGB_ALPHA"); |
| 1161 |
} else { |
| 1162 |
*depthP = 2; |
| 1163 |
strcpy(tupleType, "GRAYSCALE_ALPHA"); |
| 1164 |
} |
| 1165 |
} else { |
| 1166 |
if (outputIsColor) { |
| 1167 |
*formatP = PPM_FORMAT; |
| 1168 |
*depthP = 3; |
| 1169 |
} else { |
| 1170 |
*depthP = 1; |
| 1171 |
*formatP = maxval > 1 ? PGM_FORMAT : PBM_FORMAT; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
static void |
| 1180 |
getBackgroundColor(struct pngx * const pngxP, |
| 1181 |
const char * const requestedColor, |
| 1182 |
GammaCorrection const gamma, |
| 1183 |
xelval const maxval, |
| 1184 |
pngcolor * const bgColorP) { |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
if (requestedColor) { |
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
pixel const backcolor = ppm_parsecolor(requestedColor, maxval); |
| 1197 |
|
| 1198 |
bgColorP->r = PPM_GETR(backcolor); |
| 1199 |
bgColorP->g = PPM_GETG(backcolor); |
| 1200 |
bgColorP->b = PPM_GETB(backcolor); |
| 1201 |
|
| 1202 |
} else if (pngx_chunkIsPresent(pngxP, PNG_INFO_bKGD)) { |
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
png_color_16 const background = pngx_bkgd(pngxP); |
| 1207 |
switch (pngx_colorType(pngxP)) { |
| 1208 |
case PNG_COLOR_TYPE_GRAY: |
| 1209 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 1210 |
bgColorP->r = bgColorP->g = bgColorP->b = |
| 1211 |
gammaCorrect(background.gray, gamma, pngxP->maxval); |
| 1212 |
break; |
| 1213 |
case PNG_COLOR_TYPE_PALETTE: { |
| 1214 |
struct pngx_plte const palette = pngx_plte(pngxP); |
| 1215 |
png_color const rawBgcolor = |
| 1216 |
palette.palette[background.index]; |
| 1217 |
*bgColorP = gammaCorrectColor(pngcolorFromByte(rawBgcolor), |
| 1218 |
gamma, pngxP->maxval); |
| 1219 |
} |
| 1220 |
break; |
| 1221 |
case PNG_COLOR_TYPE_RGB: |
| 1222 |
case PNG_COLOR_TYPE_RGB_ALPHA: { |
| 1223 |
png_color_16 const rawBgcolor = background; |
| 1224 |
|
| 1225 |
*bgColorP = gammaCorrectColor(pngcolorFrom16(rawBgcolor), |
| 1226 |
gamma, pngxP->maxval); |
| 1227 |
} |
| 1228 |
break; |
| 1229 |
} |
| 1230 |
} else |
| 1231 |
|
| 1232 |
bgColorP->r = bgColorP->g = bgColorP->b = maxval; |
| 1233 |
} |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
static void |
| 1238 |
warnNonsquarePixels(struct pngx * const pngxP, |
| 1239 |
int * const errorLevelP) { |
| 1240 |
|
| 1241 |
if (pngx_pixelAspectRatioIsKnown(pngxP)) { |
| 1242 |
float const r = pngx_pixelAspectRatio(pngxP); |
| 1243 |
|
| 1244 |
if (r != 1.0) { |
| 1245 |
const char * const baseMsg = "warning - non-square pixels"; |
| 1246 |
|
| 1247 |
if (pm_have_float_format()) |
| 1248 |
pm_message("%s; to fix do a 'pamscale -%cscale %g'", |
| 1249 |
baseMsg, |
| 1250 |
r < 1.0 ? 'x' : 'y', |
| 1251 |
r < 1.0 ? 1.0 / r : r); |
| 1252 |
else |
| 1253 |
pm_message("%s", baseMsg); |
| 1254 |
|
| 1255 |
*errorLevelP = PNMTOPNG_WARNING_LEVEL; |
| 1256 |
} |
| 1257 |
} |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
static png_uint_16 |
| 1263 |
paletteAlpha(struct pngx * const pngxP, |
| 1264 |
png_uint_16 const index, |
| 1265 |
sample const maxval) { |
| 1266 |
|
| 1267 |
png_uint_16 retval; |
| 1268 |
|
| 1269 |
if (pngx_chunkIsPresent(pngxP, PNG_INFO_tRNS)) { |
| 1270 |
struct pngx_trns const trans = pngx_trns(pngxP); |
| 1271 |
|
| 1272 |
if (index < trans.numTrans) |
| 1273 |
retval = trans.trans[index]; |
| 1274 |
else |
| 1275 |
retval = maxval; |
| 1276 |
} else |
| 1277 |
retval = maxval; |
| 1278 |
|
| 1279 |
return retval; |
| 1280 |
} |
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
#define GET_PNG_VAL(p) getPngVal(&(p), pngx_bitDepth(pngxP)) |
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
static void |
| 1289 |
makeTupleRow(const struct pam * const pamP, |
| 1290 |
const tuple * const tuplerow, |
| 1291 |
struct pngx * const pngxP, |
| 1292 |
const png_byte * const pngRasterRow, |
| 1293 |
pngcolor const bgColor, |
| 1294 |
enum AlphaHandling const alphaHandling, |
| 1295 |
GammaCorrection const gamma) { |
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
const png_byte * pngPixelP; |
| 1307 |
unsigned int col; |
| 1308 |
|
| 1309 |
pngPixelP = &pngRasterRow[0]; |
| 1310 |
for (col = 0; col < pngx_imageWidth(pngxP); ++col) { |
| 1311 |
switch (pngx_colorType(pngxP)) { |
| 1312 |
case PNG_COLOR_TYPE_GRAY: { |
| 1313 |
pngcolor fgColor; |
| 1314 |
fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP); |
| 1315 |
setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling, |
| 1316 |
pngxP, |
| 1317 |
isTransparentColor(fgColor, pngxP, gamma) ? |
| 1318 |
0 : pngxP->maxval); |
| 1319 |
} |
| 1320 |
break; |
| 1321 |
|
| 1322 |
case PNG_COLOR_TYPE_GRAY_ALPHA: { |
| 1323 |
pngcolor fgColor; |
| 1324 |
png_uint_16 alpha; |
| 1325 |
|
| 1326 |
fgColor.r = fgColor.g = fgColor.b = GET_PNG_VAL(pngPixelP); |
| 1327 |
alpha = GET_PNG_VAL(pngPixelP); |
| 1328 |
setTuple(pamP, tuplerow[col], fgColor, bgColor, |
| 1329 |
alphaHandling, pngxP, alpha); |
| 1330 |
} |
| 1331 |
break; |
| 1332 |
|
| 1333 |
case PNG_COLOR_TYPE_PALETTE: { |
| 1334 |
png_uint_16 const index = GET_PNG_VAL(pngPixelP); |
| 1335 |
struct pngx_plte const palette = pngx_plte(pngxP); |
| 1336 |
png_color const paletteColor = palette.palette[index]; |
| 1337 |
|
| 1338 |
pngcolor fgColor; |
| 1339 |
|
| 1340 |
fgColor.r = paletteColor.red; |
| 1341 |
fgColor.g = paletteColor.green; |
| 1342 |
fgColor.b = paletteColor.blue; |
| 1343 |
|
| 1344 |
setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling, |
| 1345 |
pngxP, paletteAlpha(pngxP, index, pngxP->maxval)); |
| 1346 |
} |
| 1347 |
break; |
| 1348 |
|
| 1349 |
case PNG_COLOR_TYPE_RGB: { |
| 1350 |
pngcolor fgColor; |
| 1351 |
|
| 1352 |
fgColor.r = GET_PNG_VAL(pngPixelP); |
| 1353 |
fgColor.g = GET_PNG_VAL(pngPixelP); |
| 1354 |
fgColor.b = GET_PNG_VAL(pngPixelP); |
| 1355 |
setTuple(pamP, tuplerow[col], fgColor, bgColor, alphaHandling, |
| 1356 |
pngxP, |
| 1357 |
isTransparentColor(fgColor, pngxP, gamma) ? |
| 1358 |
0 : pngxP->maxval); |
| 1359 |
} |
| 1360 |
break; |
| 1361 |
|
| 1362 |
case PNG_COLOR_TYPE_RGB_ALPHA: { |
| 1363 |
pngcolor fgColor; |
| 1364 |
png_uint_16 alpha; |
| 1365 |
|
| 1366 |
fgColor.r = GET_PNG_VAL(pngPixelP); |
| 1367 |
fgColor.g = GET_PNG_VAL(pngPixelP); |
| 1368 |
fgColor.b = GET_PNG_VAL(pngPixelP); |
| 1369 |
alpha = GET_PNG_VAL(pngPixelP); |
| 1370 |
setTuple(pamP, tuplerow[col], fgColor, bgColor, |
| 1371 |
alphaHandling, pngxP, alpha); |
| 1372 |
} |
| 1373 |
break; |
| 1374 |
|
| 1375 |
default: |
| 1376 |
pm_error("unknown PNG color type: %d", |
| 1377 |
pngx_colorType(pngxP)); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
static void |
| 1385 |
reportOutputFormat(const struct pam * const pamP) { |
| 1386 |
|
| 1387 |
switch (pamP->format) { |
| 1388 |
|
| 1389 |
case PBM_FORMAT: |
| 1390 |
pm_message("Writing a PBM file"); |
| 1391 |
break; |
| 1392 |
case PGM_FORMAT: |
| 1393 |
pm_message("Writing a PGM file with maxval %lu", pamP->maxval); |
| 1394 |
break; |
| 1395 |
case PPM_FORMAT: |
| 1396 |
pm_message("Writing a PPM file with maxval %lu", pamP->maxval); |
| 1397 |
break; |
| 1398 |
case PAM_FORMAT: |
| 1399 |
pm_message("Writing a PAM file with tuple type %s, maxval %lu", |
| 1400 |
pamP->tuple_type, pamP->maxval); |
| 1401 |
break; |
| 1402 |
default: |
| 1403 |
assert(false); |
| 1404 |
} |
| 1405 |
} |
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
static void |
| 1410 |
writeNetpbm(struct pam * const pamP, |
| 1411 |
struct pngx * const pngxP, |
| 1412 |
Reader * const rasterReaderP, |
| 1413 |
pngcolor const bgColor, |
| 1414 |
enum AlphaHandling const alphaHandling, |
| 1415 |
GammaCorrection const gamma) { |
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
tuple * tuplerow; |
| 1428 |
unsigned int row; |
| 1429 |
|
| 1430 |
if (verbose) |
| 1431 |
reportOutputFormat(pamP); |
| 1432 |
|
| 1433 |
pnm_writepaminit(pamP); |
| 1434 |
|
| 1435 |
tuplerow = pnm_allocpamrow(pamP); |
| 1436 |
|
| 1437 |
for (row = 0; row < pngx_imageHeight(pngxP); ++row) { |
| 1438 |
png_byte * const pngRow = reader_read(rasterReaderP); |
| 1439 |
|
| 1440 |
assert(pngRow); |
| 1441 |
|
| 1442 |
makeTupleRow(pamP, tuplerow, pngxP, pngRow, bgColor, |
| 1443 |
alphaHandling, gamma); |
| 1444 |
|
| 1445 |
pnm_writepamrow(pamP, tuplerow); |
| 1446 |
} |
| 1447 |
pnm_freepamrow(tuplerow); |
| 1448 |
} |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
static void |
| 1453 |
convertpng(FILE * const ifP, |
| 1454 |
FILE * const tfP, |
| 1455 |
struct CmdlineInfo const cmdline, |
| 1456 |
int * const errorLevelP) { |
| 1457 |
|
| 1458 |
Reader * rasterReaderP; |
| 1459 |
pngcolor bgColor; |
| 1460 |
GammaCorrection gamma; |
| 1461 |
struct pam pam; |
| 1462 |
struct pngx * pngxP; |
| 1463 |
|
| 1464 |
*errorLevelP = 0; |
| 1465 |
|
| 1466 |
pngx_create(&pngxP, PNGX_READ, NULL); |
| 1467 |
|
| 1468 |
pngx_readStart(pngxP, ifP); |
| 1469 |
|
| 1470 |
if (verbose) |
| 1471 |
dumpPngInfo(pngxP); |
| 1472 |
|
| 1473 |
if (cmdline.time) |
| 1474 |
showTime(pngxP); |
| 1475 |
if (tfP) |
| 1476 |
saveText(pngxP, tfP); |
| 1477 |
|
| 1478 |
warnNonsquarePixels(pngxP, errorLevelP); |
| 1479 |
|
| 1480 |
setupGammaCorrection(pngxP, cmdline.gammaSpec, cmdline.gamma, &gamma); |
| 1481 |
|
| 1482 |
setupSignificantBits(pngxP, cmdline.alpha, errorLevelP); |
| 1483 |
|
| 1484 |
getBackgroundColor(pngxP, cmdline.background, gamma, pngxP->maxval, |
| 1485 |
&bgColor); |
| 1486 |
|
| 1487 |
pam.size = sizeof(pam); |
| 1488 |
pam.len = PAM_STRUCT_SIZE(tuple_type); |
| 1489 |
pam.file = stdout; |
| 1490 |
pam.plainformat = 0; |
| 1491 |
pam.height = pngx_imageHeight(pngxP); |
| 1492 |
pam.width = pngx_imageWidth(pngxP); |
| 1493 |
pam.maxval = pngxP->maxval; |
| 1494 |
|
| 1495 |
determineOutputType(pngxP, cmdline.alpha, bgColor, pngxP->maxval, |
| 1496 |
&pam.format, &pam.depth, pam.tuple_type); |
| 1497 |
|
| 1498 |
rasterReaderP = cmdline.byrow ? |
| 1499 |
reader_createRowByRow(pngxP, ifP) : reader_createAllAtOnce(pngxP, ifP); |
| 1500 |
|
| 1501 |
writeNetpbm(&pam, pngxP, rasterReaderP, bgColor, |
| 1502 |
cmdline.alpha, gamma); |
| 1503 |
|
| 1504 |
reader_destroy(rasterReaderP); |
| 1505 |
|
| 1506 |
pngx_readEnd(pngxP); |
| 1507 |
|
| 1508 |
fflush(stdout); |
| 1509 |
|
| 1510 |
pngx_destroy(pngxP); |
| 1511 |
} |
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
int |
| 1516 |
main(int argc, const char *argv[]) { |
| 1517 |
|
| 1518 |
struct CmdlineInfo cmdline; |
| 1519 |
FILE * ifP; |
| 1520 |
FILE * tfP; |
| 1521 |
int errorLevel; |
| 1522 |
|
| 1523 |
pm_proginit(&argc, argv); |
| 1524 |
|
| 1525 |
parseCommandLine(argc, argv, &cmdline); |
| 1526 |
|
| 1527 |
verbose = cmdline.verbose; |
| 1528 |
|
| 1529 |
ifP = pm_openr(cmdline.inputFileName); |
| 1530 |
|
| 1531 |
if (cmdline.text) |
| 1532 |
tfP = pm_openw(cmdline.text); |
| 1533 |
else |
| 1534 |
tfP = NULL; |
| 1535 |
|
| 1536 |
convertpng(ifP, tfP, cmdline, &errorLevel); |
| 1537 |
|
| 1538 |
if (tfP) |
| 1539 |
pm_close(tfP); |
| 1540 |
|
| 1541 |
pm_close(ifP); |
| 1542 |
pm_close(stdout); |
| 1543 |
|
| 1544 |
return errorLevel; |
| 1545 |
} |
| 1546 |
|
| 1547 |
|
| 1548 |
|