| 1 |
/*============================================================================= |
| 2 |
pamtopng |
| 3 |
=============================================================================== |
| 4 |
Read a Netpbm image and produce a PNG (Portable Network Graphics) image. |
| 5 |
=============================================================================*/ |
| 6 |
|
| 7 |
/* |
| 8 |
This Netpbm program pamtopng was derived in 2015 from the Netpbm program |
| 9 |
Pnmtopng. This was a nearly complete rewrite with the following goals: |
| 10 |
|
| 11 |
- Add ability to create a PNG alpha channel from the alpha channel in a |
| 12 |
PAM (format P7) file. |
| 13 |
|
| 14 |
- Simplify the 20 year old pnmtopng code. Because of the many, many features |
| 15 |
that program implements and its need for backward compatibility, the code |
| 16 |
had become rather complex. This program is roughly 1/3 the size of |
| 17 |
pnmtopng.c that it replaces. |
| 18 |
|
| 19 |
- In 1995, bandwid was limited and therefore filesize had to be kept |
| 20 |
small. The original program tried to optimize for that by applying |
| 21 |
many "clever tricks". Today that isn't an issue anymore, so gone |
| 22 |
are filters, palettes, etc. Also, image conversions were removed, |
| 23 |
because those should be done with other NetPBM tools. |
| 24 |
|
| 25 |
- Add ability to create iTXt (international language) chunks. |
| 26 |
*/ |
| 27 |
|
| 28 |
#include <assert.h> |
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <time.h> |
| 32 |
#include <png.h> |
| 33 |
|
| 34 |
#include "pm_c_util.h" |
| 35 |
#include "mallocvar.h" |
| 36 |
#include "nstring.h" |
| 37 |
#include "shhopt.h" |
| 38 |
#include "pam.h" |
| 39 |
#include "pngx.h" |
| 40 |
#include "pngtxt.h" |
| 41 |
|
| 42 |
|
| 43 |
/* global variable */ |
| 44 |
static bool verbose; |
| 45 |
|
| 46 |
|
| 47 |
struct CmdlineInfo { |
| 48 |
const char * inputFileName; |
| 49 |
unsigned int verbose; |
| 50 |
unsigned int interlace; |
| 51 |
unsigned int transparentSpec; |
| 52 |
const char * transparent; |
| 53 |
unsigned int chromaSpec; |
| 54 |
struct pngx_chroma chroma; |
| 55 |
unsigned int gammaSpec; |
| 56 |
float gamma; |
| 57 |
unsigned int srgbintentSpec; |
| 58 |
pngx_srgbIntent srgbintent; |
| 59 |
unsigned int textSpec; |
| 60 |
const char * text; |
| 61 |
unsigned int ztxtSpec; |
| 62 |
const char * ztxt; |
| 63 |
unsigned int itxtSpec; |
| 64 |
const char * itxt; |
| 65 |
unsigned int backgroundSpec; |
| 66 |
const char * background; |
| 67 |
unsigned int timeSpec; |
| 68 |
time_t time; |
| 69 |
}; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
static void |
| 74 |
parseChromaOpt(const char * const chromaOpt, |
| 75 |
struct pngx_chroma * const chromaP) { |
| 76 |
|
| 77 |
int count; |
| 78 |
|
| 79 |
count = sscanf(chromaOpt, "%f %f %f %f %f %f %f %f", |
| 80 |
&chromaP->wx, &chromaP->wy, |
| 81 |
&chromaP->rx, &chromaP->ry, |
| 82 |
&chromaP->gx, &chromaP->gy, |
| 83 |
&chromaP->bx, &chromaP->by); |
| 84 |
|
| 85 |
if (count != 8) |
| 86 |
pm_error("Invalid syntax for the -chroma option value '%s'. " |
| 87 |
"Should be 8 floating point numbers: " |
| 88 |
"x and y for each of white, red, green, and blue", |
| 89 |
chromaOpt); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
static void |
| 95 |
parseSrgbintentOpt(const char * const srgbintentOpt, |
| 96 |
pngx_srgbIntent * const srgbintentP) { |
| 97 |
|
| 98 |
if (streq(srgbintentOpt, "perceptual")) |
| 99 |
*srgbintentP = PNGX_PERCEPTUAL; |
| 100 |
else if (streq(srgbintentOpt, "relativecolorimetric")) |
| 101 |
*srgbintentP = PNGX_RELATIVE_COLORIMETRIC; |
| 102 |
else if (streq(srgbintentOpt, "saturation")) |
| 103 |
*srgbintentP = PNGX_SATURATION; |
| 104 |
else if (streq(srgbintentOpt, "absolutecolorimetric")) |
| 105 |
*srgbintentP = PNGX_ABSOLUTE_COLORIMETRIC; |
| 106 |
else |
| 107 |
pm_error("Unrecognized sRGB intent value '%s'. We understand " |
| 108 |
"only 'perceptual', 'relativecolorimetric', " |
| 109 |
"'saturation', and 'absolutecolorimetric'", |
| 110 |
srgbintentOpt); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static void |
| 116 |
parseTimeOpt(const char * const timeOpt, |
| 117 |
time_t * const timeP) { |
| 118 |
|
| 119 |
struct tm brokenTime; |
| 120 |
int year; |
| 121 |
int month; |
| 122 |
int count; |
| 123 |
|
| 124 |
count = sscanf(timeOpt, "%d-%d-%d %d:%d:%d", |
| 125 |
&year, |
| 126 |
&month, |
| 127 |
&brokenTime.tm_mday, |
| 128 |
&brokenTime.tm_hour, |
| 129 |
&brokenTime.tm_min, |
| 130 |
&brokenTime.tm_sec); |
| 131 |
|
| 132 |
if (count != 6) |
| 133 |
pm_error("Invalid value for -time '%s'. It should have " |
| 134 |
"the form [yy]yy-mm-dd hh:mm:ss.", timeOpt); |
| 135 |
|
| 136 |
if (year < 0) |
| 137 |
pm_error("Year is negative in -time value '%s'", timeOpt); |
| 138 |
if (year > 9999) |
| 139 |
pm_error("Year is more than 4 digits in -time value '%s'", |
| 140 |
timeOpt); |
| 141 |
if (month < 0) |
| 142 |
pm_error("Month is negative in -time value '%s'", timeOpt); |
| 143 |
if (month > 12) |
| 144 |
pm_error("Month is >12 in -time value '%s'", timeOpt); |
| 145 |
if (brokenTime.tm_mday < 0) |
| 146 |
pm_error("Day of month is negative in -time value '%s'", |
| 147 |
timeOpt); |
| 148 |
if (brokenTime.tm_mday > 31) |
| 149 |
pm_error("Day of month is >31 in -time value '%s'", timeOpt); |
| 150 |
if (brokenTime.tm_hour < 0) |
| 151 |
pm_error("Hour is negative in -time value '%s'", timeOpt); |
| 152 |
if (brokenTime.tm_hour > 23) |
| 153 |
pm_error("Hour is >23 in -time value '%s'", timeOpt); |
| 154 |
if (brokenTime.tm_min < 0) |
| 155 |
pm_error("Minute is negative in -time value '%s'", timeOpt); |
| 156 |
if (brokenTime.tm_min > 59) |
| 157 |
pm_error("Minute is >59 in -time value '%s'", timeOpt); |
| 158 |
if (brokenTime.tm_sec < 0) |
| 159 |
pm_error("Second is negative in -time value '%s'", timeOpt); |
| 160 |
if (brokenTime.tm_sec > 59) |
| 161 |
pm_error("Second is >59 in -time value '%s'", timeOpt); |
| 162 |
|
| 163 |
brokenTime.tm_mon = month - 1; |
| 164 |
if (year >= 1900) |
| 165 |
brokenTime.tm_year = year - 1900; |
| 166 |
else |
| 167 |
brokenTime.tm_year = year; |
| 168 |
|
| 169 |
/* Note that mktime() considers brokeTime to be in local time. |
| 170 |
This is what we want, since we got it from a user. User should |
| 171 |
set his local time zone to UTC if he wants absolute time. |
| 172 |
*/ |
| 173 |
*timeP = mktime(&brokenTime); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static void |
| 179 |
parseCommandLine (int argc, |
| 180 |
const char ** argv, |
| 181 |
struct CmdlineInfo * const cmdlineP) { |
| 182 |
|
| 183 |
optEntry * option_def; |
| 184 |
optStruct3 opt; |
| 185 |
unsigned int option_def_index = 0; /* incremented by OPTENT3 */ |
| 186 |
|
| 187 |
const char * srgbintent; |
| 188 |
const char * chroma; |
| 189 |
const char * time; |
| 190 |
|
| 191 |
MALLOCARRAY(option_def, 100); |
| 192 |
|
| 193 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 194 |
&cmdlineP->verbose, 0); |
| 195 |
OPTENT3(0, "interlace", OPT_FLAG, NULL, |
| 196 |
&cmdlineP->interlace, 0); |
| 197 |
OPTENT3(0, "transparent", OPT_STRING, &cmdlineP->transparent, |
| 198 |
&cmdlineP->transparentSpec, 0); |
| 199 |
OPTENT3(0, "chroma", OPT_STRING, &chroma, |
| 200 |
&cmdlineP->chromaSpec, 0); |
| 201 |
OPTENT3(0, "gamma", OPT_FLOAT, &cmdlineP->gamma, |
| 202 |
&cmdlineP->gammaSpec, 0); |
| 203 |
OPTENT3(0, "srgbintent", OPT_STRING, &srgbintent, |
| 204 |
&cmdlineP->srgbintentSpec, 0); |
| 205 |
OPTENT3(0, "text", OPT_STRING, &cmdlineP->text, |
| 206 |
&cmdlineP->textSpec, 0); |
| 207 |
OPTENT3(0, "ztxt", OPT_STRING, &cmdlineP->ztxt, |
| 208 |
&cmdlineP->ztxtSpec, 0); |
| 209 |
OPTENT3(0, "itxt", OPT_STRING, &cmdlineP->itxt, |
| 210 |
&cmdlineP->itxtSpec, 0); |
| 211 |
OPTENT3(0, "background", OPT_STRING, &cmdlineP->background, |
| 212 |
&cmdlineP->backgroundSpec, 0); |
| 213 |
OPTENT3(0, "time", OPT_STRING, &time, |
| 214 |
&cmdlineP->timeSpec, 0); |
| 215 |
|
| 216 |
opt.opt_table = option_def; |
| 217 |
opt.short_allowed = false; /* we have no short (old-fashioned) options */ |
| 218 |
opt.allowNegNum = false; /* we have no parms that are negative numbers */ |
| 219 |
|
| 220 |
/* uses and sets argc, argv, and some of *cmdlineP and others */ |
| 221 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 222 |
|
| 223 |
if (cmdlineP->chromaSpec) |
| 224 |
parseChromaOpt(chroma, &cmdlineP->chroma); |
| 225 |
|
| 226 |
if (cmdlineP->srgbintentSpec) |
| 227 |
parseSrgbintentOpt(srgbintent, &cmdlineP->srgbintent); |
| 228 |
|
| 229 |
if (cmdlineP->timeSpec) |
| 230 |
parseTimeOpt(time, &cmdlineP->time); |
| 231 |
|
| 232 |
/* get the input-file or stdin pipe */ |
| 233 |
if (argc-1 < 1) |
| 234 |
cmdlineP->inputFileName = "-"; |
| 235 |
else if (argc-1 == 1) |
| 236 |
cmdlineP->inputFileName = argv[1]; |
| 237 |
else |
| 238 |
pm_error("Program takes at most one argument: input file name."); |
| 239 |
|
| 240 |
free(option_def); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
static png_byte |
| 246 |
colorTypeFromInputType(const struct pam * const pamP) { |
| 247 |
/*---------------------------------------------------------------------------- |
| 248 |
Analyze the Netpbm image for color-type and bit-depth |
| 249 |
-----------------------------------------------------------------------------*/ |
| 250 |
png_byte retval; |
| 251 |
|
| 252 |
if (pamP->depth < 1 || pamP->depth > 4) |
| 253 |
pm_error ("Number of color planes must be between 1 and 4 inclusive"); |
| 254 |
|
| 255 |
if (pamP->maxval != 1 && pamP->maxval != 3 && pamP->maxval != 15 && |
| 256 |
pamP->maxval != 255 && pamP->maxval != 65535) |
| 257 |
pm_error("The maxval of the input image is %u; " |
| 258 |
"it must be 1, 3, 15, 255 or 65535", (unsigned)pamP->maxval); |
| 259 |
|
| 260 |
if (strneq(pamP->tuple_type, "RGB_ALPHA", 9)) { |
| 261 |
if (pamP->depth == 4) |
| 262 |
retval = PNG_COLOR_TYPE_RGB_ALPHA; |
| 263 |
else |
| 264 |
pm_error("Input tuple type is RGB_ALPHA, " |
| 265 |
"but number of planes is %u instead of 4", |
| 266 |
pamP->depth); |
| 267 |
} else if (strneq(pamP->tuple_type, "RGB", 3)) { |
| 268 |
if (pamP->depth == 3) |
| 269 |
retval = PNG_COLOR_TYPE_RGB; |
| 270 |
else |
| 271 |
pm_error("Input tuple type is RGB, " |
| 272 |
"but number of planes is %u instead of 3", |
| 273 |
pamP->depth); |
| 274 |
} else if (strneq(pamP->tuple_type, "GRAYSCALE_ALPHA", 15)) { |
| 275 |
if (pamP->depth == 2) |
| 276 |
retval = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 277 |
else |
| 278 |
pm_error("Input tuple type is GRAYSCALE_ALPHA, " |
| 279 |
"but number of planes is %u instead of 2", |
| 280 |
pamP->depth); |
| 281 |
} else if (strneq(pamP->tuple_type, "GRAYSCALE", 9)) { |
| 282 |
if (pamP->depth == 1) |
| 283 |
retval = PNG_COLOR_TYPE_GRAY; |
| 284 |
else |
| 285 |
pm_error("Input tuple type is GRAYSCALE, " |
| 286 |
"but number of planes is %u instead of 1", |
| 287 |
pamP->depth); |
| 288 |
} else if (strneq(pamP->tuple_type, "BLACKANDWHITE_ALPHA", 19)) { |
| 289 |
if (pamP->depth != 2) |
| 290 |
pm_error("Input tuple type is BLACKANDWHITE_ALPHA, " |
| 291 |
"but number of planes is %u instead of 2", |
| 292 |
pamP->depth); |
| 293 |
if (pamP->maxval != 1) |
| 294 |
pm_error("Input tuple type is BLACKANDWHITE_ALPHA, " |
| 295 |
"but maxval is %u instead of 1", (unsigned)pamP->maxval); |
| 296 |
|
| 297 |
retval = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 298 |
} else if (strneq(pamP->tuple_type, "BLACKANDWHITE", 13)) { |
| 299 |
if (pamP->depth != 1) |
| 300 |
pm_error("Input tuple type is BLACKANDWHITE, " |
| 301 |
"but number of planes is %u instead of 1", |
| 302 |
pamP->depth); |
| 303 |
if (pamP->maxval != 1) |
| 304 |
pm_error("Input tuple type is BLACKANDWHITE, " |
| 305 |
"but maxval is %u instead of 1", (unsigned)pamP->maxval); |
| 306 |
|
| 307 |
retval = PNG_COLOR_TYPE_GRAY; |
| 308 |
} else |
| 309 |
pm_error("Unrecognized tuple type: '%s'", pamP->tuple_type); |
| 310 |
|
| 311 |
return retval; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
/***************************************************************************** |
| 317 |
* Subroutines that create all the (ancillary) chunks |
| 318 |
*****************************************************************************/ |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
static png_color_16 |
| 323 |
parseAndScaleColor(const char * const colorString, |
| 324 |
xelval const pngMaxval) { |
| 325 |
|
| 326 |
png_color_16 pngColor; |
| 327 |
|
| 328 |
if (colorString) { |
| 329 |
xel const inputColor = ppm_parsecolor(colorString, PNM_OVERALLMAXVAL); |
| 330 |
|
| 331 |
xel scaledColor; |
| 332 |
|
| 333 |
/* Scale the color down to the PNG bit depth */ |
| 334 |
PPM_DEPTH(scaledColor, inputColor, PNM_OVERALLMAXVAL, pngMaxval); |
| 335 |
|
| 336 |
pngColor.red = PPM_GETR(scaledColor); |
| 337 |
pngColor.green = PPM_GETG(scaledColor); |
| 338 |
pngColor.blue = PPM_GETB(scaledColor); |
| 339 |
pngColor.gray = PNM_GET1(scaledColor); |
| 340 |
} |
| 341 |
|
| 342 |
return pngColor; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
static png_color_8 |
| 348 |
sigBitsFmImgType(unsigned int const pnmBitDepth, |
| 349 |
int const pngColorType) { |
| 350 |
/*---------------------------------------------------------------------------- |
| 351 |
A representation used in PNG of color resolutions in an original image. |
| 352 |
-----------------------------------------------------------------------------*/ |
| 353 |
png_color_8 retval; |
| 354 |
|
| 355 |
/* Initial values */ |
| 356 |
if (pnmBitDepth < 8) { |
| 357 |
switch (pngColorType) { |
| 358 |
case PNG_COLOR_TYPE_RGB: |
| 359 |
retval.red = pnmBitDepth; |
| 360 |
retval.green = pnmBitDepth; |
| 361 |
retval.blue = pnmBitDepth; |
| 362 |
retval.gray = 0; |
| 363 |
retval.alpha = 0; |
| 364 |
break; |
| 365 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 366 |
retval.red = pnmBitDepth; |
| 367 |
retval.green = pnmBitDepth; |
| 368 |
retval.blue = pnmBitDepth; |
| 369 |
retval.gray = 0; |
| 370 |
retval.alpha = pnmBitDepth; |
| 371 |
break; |
| 372 |
case PNG_COLOR_TYPE_GRAY: |
| 373 |
/* PNG can (so presumably will) use original bit depth */ |
| 374 |
retval.red = 0; |
| 375 |
retval.green = 0; |
| 376 |
retval.blue = 0; |
| 377 |
retval.gray = 0; |
| 378 |
retval.alpha = 0; |
| 379 |
break; |
| 380 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 381 |
retval.red = 0; |
| 382 |
retval.green = 0; |
| 383 |
retval.blue = 0; |
| 384 |
retval.gray = pnmBitDepth; |
| 385 |
retval.alpha = pnmBitDepth; |
| 386 |
break; |
| 387 |
default: |
| 388 |
assert(false); |
| 389 |
} |
| 390 |
} else { |
| 391 |
/* PNG can (so presumably will) use original bit depth */ |
| 392 |
retval.red = 0; |
| 393 |
retval.green = 0; |
| 394 |
retval.blue = 0; |
| 395 |
retval.gray = 0; |
| 396 |
retval.alpha = 0; |
| 397 |
} |
| 398 |
return retval; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
static void |
| 404 |
doTrnsChunk(const struct pam * const pamP, |
| 405 |
struct pngx * const pngxP, |
| 406 |
const char * const trans) { |
| 407 |
|
| 408 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 409 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA) |
| 410 |
pm_error("Both alpha channel and transparency chunk not allowed."); |
| 411 |
else { |
| 412 |
xelval const pngMaxval = pm_bitstomaxval(pngx_bitDepth(pngxP)); |
| 413 |
png_color_16 const pngColor = parseAndScaleColor(trans, pngMaxval); |
| 414 |
/* Transparent color from text format scaled from 16-bit to |
| 415 |
maxval. |
| 416 |
*/ |
| 417 |
|
| 418 |
pngx_setTrnsValue(pngxP, pngColor); |
| 419 |
|
| 420 |
if (verbose) { |
| 421 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY) { |
| 422 |
pm_message("writing tRNS chunk with color {gray} = {%u}", |
| 423 |
pngColor.gray ); |
| 424 |
} else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB) { |
| 425 |
pm_message("writing tRNS chunk with color " |
| 426 |
"{red, green, blue} = {%u, %u, %u}", |
| 427 |
pngColor.red, pngColor.green, pngColor.blue); |
| 428 |
} |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static void |
| 436 |
doChrmChunk(struct pngx * const pngxP, |
| 437 |
struct pngx_chroma const chroma) { |
| 438 |
|
| 439 |
pngx_setChrm(pngxP, chroma); |
| 440 |
|
| 441 |
if (verbose) { |
| 442 |
pm_message("writing cHRM chunk { wx, wy, rx, ry, gx, gy, bx, by } = " |
| 443 |
"{ %4.2f, %4.2f, %4.2f, %4.2f, " |
| 444 |
"%4.2f, %4.2f, %4.2f, %4.2f }", |
| 445 |
chroma.wx, chroma.wy, |
| 446 |
chroma.rx, chroma.ry, |
| 447 |
chroma.gx, chroma.gy, |
| 448 |
chroma.bx, chroma.by); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
static void |
| 455 |
doGamaChunk(struct pngx * const pngxP, |
| 456 |
float const gamma) { |
| 457 |
|
| 458 |
pngx_setGama(pngxP, gamma); |
| 459 |
|
| 460 |
if (verbose) { |
| 461 |
pm_message("writing gAMA chunk with image gamma value %4.2f", gamma); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
static void |
| 468 |
doSbitChunk(const struct pam * const pamP, |
| 469 |
struct pngx * const pngxP, |
| 470 |
png_color_8 const sigBits) { |
| 471 |
|
| 472 |
if (sigBits.red + sigBits.green + sigBits.blue + |
| 473 |
sigBits.gray + sigBits.alpha > 0) { |
| 474 |
pngx_setSbit(pngxP, sigBits); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
static void |
| 481 |
doSrgbChunk(struct pngx * const pngxP, |
| 482 |
pngx_srgbIntent const srgbIntent) { |
| 483 |
|
| 484 |
pngx_setSrgb(pngxP, srgbIntent); |
| 485 |
|
| 486 |
if (verbose) { |
| 487 |
pm_message("writing sRGB chunk with intent value %s", |
| 488 |
pngx_srgbIntentDesc(srgbIntent)); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
static void |
| 495 |
doTextChunkSet(struct pngx * const pngxP, |
| 496 |
const char * const textFileName) { |
| 497 |
|
| 498 |
bool const ztxt = true; |
| 499 |
bool const itxt = false; |
| 500 |
|
| 501 |
FILE * tfP; |
| 502 |
|
| 503 |
tfP = pm_openr(textFileName); |
| 504 |
|
| 505 |
pngtxt_addChunk(pngxP, tfP, ztxt, itxt, verbose); |
| 506 |
|
| 507 |
pm_close(tfP); |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
static void |
| 513 |
doZtxtChunkSet(struct pngx * const pngxP, |
| 514 |
const char * const textFileName) { |
| 515 |
|
| 516 |
bool const ztxt = true; |
| 517 |
bool const itxt = false; |
| 518 |
|
| 519 |
FILE * tfP; |
| 520 |
|
| 521 |
tfP = pm_openr(textFileName); |
| 522 |
|
| 523 |
pngtxt_addChunk(pngxP, tfP, ztxt, itxt, verbose); |
| 524 |
|
| 525 |
pm_close(tfP); |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
static void |
| 531 |
doItxtChunkSet(struct pngx * const pngxP, |
| 532 |
const char * const textFileName) { |
| 533 |
|
| 534 |
bool const ztxt = true; |
| 535 |
bool const itxt = true; |
| 536 |
|
| 537 |
FILE * tfP; |
| 538 |
|
| 539 |
tfP = pm_openr(textFileName); |
| 540 |
|
| 541 |
pngtxt_addChunk(pngxP, tfP, ztxt, itxt, verbose); |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
static void |
| 547 |
doBkgdChunk (const struct pam * const pamP, |
| 548 |
struct pngx * const pngxP, |
| 549 |
const char * const colorName) |
| 550 |
{ |
| 551 |
xelval const pngMaxval = pm_bitstomaxval(pngx_bitDepth(pngxP)); |
| 552 |
|
| 553 |
png_color_16 const pngColor = parseAndScaleColor(colorName, pngMaxval); |
| 554 |
/* Background color from text format, scaled from 16-bit to maxval */ |
| 555 |
|
| 556 |
pngx_setBkgdRgb(pngxP, pngColor); |
| 557 |
|
| 558 |
if (verbose) { |
| 559 |
if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY || |
| 560 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 561 |
pm_message("writing bKGD chunk with gray level = %u", |
| 562 |
pngColor.gray); |
| 563 |
} else if (pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB || |
| 564 |
pngx_colorType(pngxP) == PNG_COLOR_TYPE_RGB_ALPHA) { |
| 565 |
pm_message("writing bKGD chunk with color {red, green, blue} = " |
| 566 |
"{%u, %u, %u}", |
| 567 |
pngColor.red, pngColor.green, pngColor.blue); |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
static void |
| 575 |
doTimeChunk(struct pngx * const pngxP, |
| 576 |
time_t const time) { |
| 577 |
|
| 578 |
pngx_setTime(pngxP, time); |
| 579 |
|
| 580 |
if (verbose) { |
| 581 |
struct tm * const brokenTimeP = gmtime(&time); |
| 582 |
|
| 583 |
char buffer[100]; |
| 584 |
|
| 585 |
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", brokenTimeP); |
| 586 |
|
| 587 |
pm_message("Writing tIME chunk specifying datetime %s", buffer); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
static void |
| 594 |
convertRow(const struct pam * const pamP, |
| 595 |
const tuple * const tuplerow, |
| 596 |
png_byte * const pngRow, |
| 597 |
unsigned int const bitDepth) { |
| 598 |
|
| 599 |
unsigned int col; |
| 600 |
|
| 601 |
/* An image row consists of columns x planes like gray or rgb(a) x 8 or 16 |
| 602 |
bits. |
| 603 |
*/ |
| 604 |
for (col = 0; col < pamP->width; ++col) { |
| 605 |
unsigned int plane; |
| 606 |
for (plane = 0; plane < pamP->depth; ++plane) { |
| 607 |
if (bitDepth > 8) { |
| 608 |
/* Copy 2 bytes = 16 bits for one pixel */ |
| 609 |
pngRow[2 * (pamP->depth * col + plane)] = |
| 610 |
(tuplerow[col][plane] >> 8) & 0xff ; |
| 611 |
pngRow[2 * (pamP->depth * col + plane) + 1] = |
| 612 |
tuplerow[col][plane] & 0xff ; |
| 613 |
} else { |
| 614 |
/* Copy 1 byte for one pixel. Later, a packing of 2, 4 or 8 |
| 615 |
pixels into a single byte can still happen. |
| 616 |
*/ |
| 617 |
pngRow[pamP->depth * col + plane] = tuplerow[col][plane]; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
static void |
| 626 |
writeRasterRowByRow(const struct pam * const pamP, |
| 627 |
struct pngx * const pngxP, |
| 628 |
int const bitDepth) { |
| 629 |
|
| 630 |
unsigned int const rowSz = |
| 631 |
pamP->width * pamP->depth * (MAX(1, bitDepth/8)); |
| 632 |
|
| 633 |
tuple * tupleRow; |
| 634 |
png_byte * pngRow; |
| 635 |
unsigned int row; |
| 636 |
|
| 637 |
/* We process row-by-row and do not read the complete image into memory */ |
| 638 |
|
| 639 |
tupleRow = pnm_allocpamrow(pamP); |
| 640 |
|
| 641 |
MALLOCARRAY(pngRow, rowSz); |
| 642 |
|
| 643 |
if (pngRow == NULL) |
| 644 |
pm_error("Unable to allocate space for PNG pixel row for " |
| 645 |
"%u columns", pamP->width); |
| 646 |
else { |
| 647 |
for (row = 0; row < pamP->height; ++row) { |
| 648 |
pnm_readpamrow(pamP, tupleRow); |
| 649 |
|
| 650 |
convertRow(pamP, tupleRow, pngRow, bitDepth); |
| 651 |
|
| 652 |
pngx_writeRow(pngxP, pngRow); |
| 653 |
} |
| 654 |
free(pngRow); |
| 655 |
} |
| 656 |
pnm_freepamrow(tupleRow); |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
static png_bytep |
| 662 |
mallocPngImage(unsigned int const rowSize, |
| 663 |
unsigned int const height) { |
| 664 |
|
| 665 |
png_bytep pngImage; |
| 666 |
|
| 667 |
if (UINT_MAX / rowSize < height) |
| 668 |
pm_error("Image is uncomputably large at %u rows of %u bytes", |
| 669 |
height, rowSize); |
| 670 |
|
| 671 |
MALLOCARRAY(pngImage, height * rowSize); |
| 672 |
|
| 673 |
if (!pngImage) |
| 674 |
pm_error("could not allocate %u bytes for a PNG image buffer", |
| 675 |
height * rowSize); |
| 676 |
|
| 677 |
return pngImage; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
static unsigned int |
| 683 |
pngLineSize(struct pngx * const pngxP) { |
| 684 |
|
| 685 |
unsigned int const bytesPerSample = pngx_bitDepth(pngxP) == 16 ? 2 : 1; |
| 686 |
|
| 687 |
unsigned int samplesPerPixel; |
| 688 |
|
| 689 |
switch (pngx_colorType(pngxP)) { |
| 690 |
case PNG_COLOR_TYPE_GRAY: |
| 691 |
samplesPerPixel = 1; |
| 692 |
break; |
| 693 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 694 |
samplesPerPixel = 2; |
| 695 |
break; |
| 696 |
case PNG_COLOR_TYPE_RGB: |
| 697 |
samplesPerPixel = 3; |
| 698 |
break; |
| 699 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 700 |
samplesPerPixel = 4; |
| 701 |
break; |
| 702 |
default: |
| 703 |
assert(false); |
| 704 |
} |
| 705 |
|
| 706 |
if (UINT_MAX / bytesPerSample / samplesPerPixel < pngx_imageWidth(pngxP)) { |
| 707 |
pm_error("pngcopy: width %u of PNG is uncomputably large\n", |
| 708 |
pngx_imageWidth(pngxP)); |
| 709 |
} |
| 710 |
|
| 711 |
return pngx_imageWidth(pngxP) * bytesPerSample * samplesPerPixel; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
static void |
| 717 |
writeRasterWholeImg(struct pam * const pamP, |
| 718 |
struct pngx * const pngxP, |
| 719 |
unsigned int const bitDepth) { |
| 720 |
|
| 721 |
unsigned int const pngRowSize = pngLineSize(pngxP); |
| 722 |
|
| 723 |
tuple * tupleRow; |
| 724 |
png_bytep pngImage; |
| 725 |
/* A one-dimensional malloc'ed array of all pixels in image */ |
| 726 |
png_bytep * pngRowP; |
| 727 |
/* A malloc'ed array of row pointers into pngImage[] */ |
| 728 |
unsigned int row; |
| 729 |
|
| 730 |
tupleRow = pnm_allocpamrow(pamP); |
| 731 |
|
| 732 |
pngImage = mallocPngImage(pngRowSize, pamP->height); |
| 733 |
|
| 734 |
MALLOCARRAY(pngRowP, pamP->height); |
| 735 |
|
| 736 |
if (!pngRowP) |
| 737 |
pm_error("Failed to allocate an array for %u PNG row pointers", |
| 738 |
pamP->height); |
| 739 |
|
| 740 |
for (row = 0; row < pamP->height; ++row) { |
| 741 |
png_bytep const thisPngRowP = &pngImage[row * pngRowSize]; |
| 742 |
|
| 743 |
pnm_readpamrow(pamP, tupleRow); |
| 744 |
|
| 745 |
convertRow(pamP, tupleRow, thisPngRowP, bitDepth); |
| 746 |
|
| 747 |
pngRowP[row] = thisPngRowP; |
| 748 |
} |
| 749 |
|
| 750 |
pngx_writeImage(pngxP, pngRowP); |
| 751 |
|
| 752 |
free(pngRowP); |
| 753 |
free(pngImage); |
| 754 |
pnm_freepamrow(tupleRow); |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
static void |
| 760 |
reportInputFormat(const struct pam * const pamP) { |
| 761 |
|
| 762 |
const char * formatDesc; |
| 763 |
|
| 764 |
if (pamP->format == PBM_FORMAT || pamP->format == RPBM_FORMAT) |
| 765 |
formatDesc = "PBM"; |
| 766 |
else if (pamP->format == PGM_FORMAT || pamP->format == RPGM_FORMAT) |
| 767 |
formatDesc = "PGM"; |
| 768 |
else if (pamP->format == PPM_FORMAT || pamP->format == RPPM_FORMAT) |
| 769 |
formatDesc = "PPM"; |
| 770 |
else if (pamP->format == PAM_FORMAT) |
| 771 |
formatDesc = "PAM"; |
| 772 |
else |
| 773 |
formatDesc = NULL; |
| 774 |
|
| 775 |
if (formatDesc) |
| 776 |
pm_message("Input format = %s", formatDesc); |
| 777 |
else |
| 778 |
pm_message("Unrecognized input format, format code = 0x%x", |
| 779 |
pamP->format); |
| 780 |
|
| 781 |
pm_message("Input tuple type = '%s'", pamP->tuple_type); |
| 782 |
pm_message("Input depth = %u", pamP->depth); |
| 783 |
pm_message("Input maxval = %u", (unsigned int) pamP->maxval); |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
static unsigned int |
| 789 |
pngBitDepth(unsigned int const pnmBitDepth, |
| 790 |
int const pngColorType) { |
| 791 |
|
| 792 |
unsigned int retval; |
| 793 |
|
| 794 |
if ((pngColorType == PNG_COLOR_TYPE_RGB || |
| 795 |
pngColorType == PNG_COLOR_TYPE_RGB_ALPHA || |
| 796 |
pngColorType == PNG_COLOR_TYPE_GRAY_ALPHA) && |
| 797 |
pnmBitDepth < 8) { |
| 798 |
|
| 799 |
retval = 8; |
| 800 |
} else |
| 801 |
retval = pnmBitDepth; |
| 802 |
|
| 803 |
return retval; |
| 804 |
} |
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
static void |
| 809 |
addAncillaryChunks(struct pam * const pamP, |
| 810 |
struct pngx * const pngxP, |
| 811 |
struct CmdlineInfo const cmdline, |
| 812 |
png_color_8 const sigBits) { |
| 813 |
/*---------------------------------------------------------------------------- |
| 814 |
Where requested, add ancillary chunks. |
| 815 |
-----------------------------------------------------------------------------*/ |
| 816 |
if (cmdline.transparentSpec) |
| 817 |
doTrnsChunk(pamP, pngxP,cmdline.transparent); |
| 818 |
|
| 819 |
if (cmdline.chromaSpec) |
| 820 |
doChrmChunk(pngxP, cmdline.chroma); |
| 821 |
|
| 822 |
if (cmdline.gammaSpec) |
| 823 |
doGamaChunk(pngxP, cmdline.gamma); |
| 824 |
|
| 825 |
/* no iccp */ |
| 826 |
|
| 827 |
doSbitChunk(pamP, pngxP, sigBits); |
| 828 |
|
| 829 |
if (cmdline.srgbintentSpec) |
| 830 |
doSrgbChunk(pngxP, cmdline.srgbintent); |
| 831 |
|
| 832 |
if (cmdline.textSpec) |
| 833 |
doTextChunkSet(pngxP, cmdline.text); |
| 834 |
|
| 835 |
if (cmdline.ztxtSpec) |
| 836 |
doZtxtChunkSet(pngxP, cmdline.ztxt); |
| 837 |
|
| 838 |
if (cmdline.itxtSpec) |
| 839 |
doItxtChunkSet(pngxP, cmdline.itxt); |
| 840 |
|
| 841 |
if (cmdline.backgroundSpec) |
| 842 |
doBkgdChunk(pamP, pngxP, cmdline.background); |
| 843 |
|
| 844 |
/* no hist */ |
| 845 |
|
| 846 |
/* no phys */ |
| 847 |
|
| 848 |
/* no splt */ |
| 849 |
|
| 850 |
if (cmdline.timeSpec) |
| 851 |
doTimeChunk(pngxP, cmdline.time); |
| 852 |
|
| 853 |
/* Write the ancillary chunks to PNG image */ |
| 854 |
pngx_writeInfo(pngxP); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
static void |
| 860 |
setShift(struct pngx * const pngxP, |
| 861 |
png_color_8 const sigBits) { |
| 862 |
|
| 863 |
if (sigBits.red + sigBits.green + sigBits.blue + |
| 864 |
sigBits.gray + sigBits.alpha > 0) { |
| 865 |
|
| 866 |
/* Move the 1, 2, 4 bits to most significant bits */ |
| 867 |
pngx_setShift(pngxP, sigBits); |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static void |
| 874 |
doIhdrChunk(struct pngx * const pngxP, |
| 875 |
unsigned int const width, |
| 876 |
unsigned int const height, |
| 877 |
unsigned int const pnmBitDepth, |
| 878 |
int const pngColorType, |
| 879 |
bool const interlace) { |
| 880 |
|
| 881 |
int const interlaceMethod = |
| 882 |
interlace ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE; |
| 883 |
|
| 884 |
pngx_setIhdr(pngxP, width, height, |
| 885 |
pngBitDepth(pnmBitDepth, pngColorType), pngColorType, |
| 886 |
interlaceMethod, PNG_COMPRESSION_TYPE_BASE, |
| 887 |
PNG_FILTER_TYPE_BASE); |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
static void |
| 893 |
pamtopng(FILE * const ifP, |
| 894 |
FILE * const ofP, |
| 895 |
struct CmdlineInfo const cmdline) { |
| 896 |
|
| 897 |
unsigned int pnmBitDepth; |
| 898 |
int pngColorType; |
| 899 |
struct pngx * pngxP; |
| 900 |
png_color_8 sigBits; |
| 901 |
struct pam pam; |
| 902 |
|
| 903 |
pnm_readpaminit(ifP, &pam, PAM_STRUCT_SIZE(tuple_type)); |
| 904 |
|
| 905 |
if (verbose) |
| 906 |
reportInputFormat(&pam); |
| 907 |
|
| 908 |
pnmBitDepth = pm_maxvaltobits(pam.maxval); |
| 909 |
|
| 910 |
pngColorType = colorTypeFromInputType(&pam); |
| 911 |
|
| 912 |
pngx_create(&pngxP, PNGX_WRITE, NULL); |
| 913 |
|
| 914 |
png_init_io(pngxP->png_ptr, ofP); |
| 915 |
|
| 916 |
doIhdrChunk(pngxP, pam.width, pam.height, |
| 917 |
pnmBitDepth, pngColorType, cmdline.interlace > 0); |
| 918 |
|
| 919 |
sigBits = sigBitsFmImgType(pnmBitDepth, pngColorType); |
| 920 |
|
| 921 |
addAncillaryChunks(&pam, pngxP, cmdline, sigBits); |
| 922 |
|
| 923 |
setShift(pngxP, sigBits); |
| 924 |
|
| 925 |
if ((pngColorType == PNG_COLOR_TYPE_GRAY) && (pnmBitDepth < 8)) { |
| 926 |
/* Pack multiple pixels in a byte */ |
| 927 |
pngx_setPacking(pngxP); |
| 928 |
} |
| 929 |
|
| 930 |
if (cmdline.interlace) { |
| 931 |
/* Libpng will expect us to provide pixels in interlaced sequence |
| 932 |
if we write row-by-row, and that is much too difficult, so we |
| 933 |
do whole-image-at-once and let Libpng do the work. |
| 934 |
*/ |
| 935 |
writeRasterWholeImg(&pam, pngxP, pnmBitDepth); |
| 936 |
} else { |
| 937 |
/* We save memory by going row-by-row */ |
| 938 |
writeRasterRowByRow(&pam, pngxP, pnmBitDepth); |
| 939 |
} |
| 940 |
|
| 941 |
pngx_writeEnd(pngxP); |
| 942 |
pngx_destroy(pngxP); |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
int |
| 948 |
main(int argc, |
| 949 |
const char ** argv) { |
| 950 |
|
| 951 |
FILE * ifP; |
| 952 |
struct CmdlineInfo cmdline; |
| 953 |
|
| 954 |
pm_proginit(&argc, argv); |
| 955 |
|
| 956 |
parseCommandLine(argc, argv, &cmdline); |
| 957 |
|
| 958 |
verbose = cmdline.verbose; |
| 959 |
|
| 960 |
ifP = pm_openr(cmdline.inputFileName); |
| 961 |
|
| 962 |
pamtopng(ifP, stdout, cmdline); |
| 963 |
|
| 964 |
pm_close(ifP); |
| 965 |
|
| 966 |
return 0; |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
/* Derived from pnmtorast.c by Jef Poskanzer and pamrgbatopng.c by Bryan |
| 972 |
** Henderson <[email protected]> and probably some other sources |
| 973 |
** |
| 974 |
** Copyright (C) 1995-1998 by Alexander Lehmann <[email protected]> |
| 975 |
** and Willem van Schaik <[email protected]> |
| 976 |
** Copyright (C) 1999,2001 by Greg Roelofs <[email protected]> |
| 977 |
** Copyright (C) 2015 by Willem van Schaik <[email protected]> |
| 978 |
** |
| 979 |
** Permission to use, copy, modify, and distribute this software and its |
| 980 |
** documentation for any purpose and without fee is hereby granted, provided |
| 981 |
** that the above copyright notice appear in all copies and that both that |
| 982 |
** copyright notice and this permission notice appear in supporting |
| 983 |
** documentation. This software is provided "as is" without express or |
| 984 |
** implied warranty. |
| 985 |
*/ |