| 1 |
#include "impng.h" |
| 2 |
#include "png.h" |
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <zlib.h> |
| 6 |
|
| 7 |
/* this is a way to get number of channels from color space |
| 8 |
* Color code to channel number */ |
| 9 |
|
| 10 |
static int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA]; |
| 11 |
|
| 12 |
#define PNG_BYTES_TO_CHECK 4 |
| 13 |
|
| 14 |
static i_img * |
| 15 |
read_direct8(png_structp png_ptr, png_infop info_ptr, int channels, i_img_dim width, i_img_dim height); |
| 16 |
|
| 17 |
static i_img * |
| 18 |
read_direct16(png_structp png_ptr, png_infop info_ptr, int channels, i_img_dim width, i_img_dim height); |
| 19 |
|
| 20 |
static i_img * |
| 21 |
read_paletted(png_structp png_ptr, png_infop info_ptr, int channels, i_img_dim width, i_img_dim height); |
| 22 |
|
| 23 |
static i_img * |
| 24 |
read_bilevel(png_structp png_ptr, png_infop info_ptr, i_img_dim width, i_img_dim height); |
| 25 |
|
| 26 |
static int |
| 27 |
write_direct8(png_structp png_ptr, png_infop info_ptr, i_img *im); |
| 28 |
|
| 29 |
static int |
| 30 |
write_direct16(png_structp png_ptr, png_infop info_ptr, i_img *im); |
| 31 |
|
| 32 |
static int |
| 33 |
write_paletted(png_structp png_ptr, png_infop info_ptr, i_img *im); |
| 34 |
|
| 35 |
static int |
| 36 |
write_bilevel(png_structp png_ptr, png_infop info_ptr, i_img *im); |
| 37 |
|
| 38 |
static void |
| 39 |
get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr, int bit_depth, int color_type); |
| 40 |
|
| 41 |
static int |
| 42 |
set_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr); |
| 43 |
|
| 44 |
static const char * |
| 45 |
get_string2(i_img_tags *tags, const char *name, char *buf, size_t *size); |
| 46 |
|
| 47 |
unsigned |
| 48 |
i_png_lib_version(void) { |
| 49 |
return png_access_version_number(); |
| 50 |
} |
| 51 |
|
| 52 |
static char const * const |
| 53 |
features[] = |
| 54 |
{ |
| 55 |
#ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 56 |
"benign-errors", |
| 57 |
#endif |
| 58 |
#ifdef PNG_READ_SUPPORTED |
| 59 |
"read", |
| 60 |
#endif |
| 61 |
#ifdef PNG_WRITE_SUPPORTED |
| 62 |
"write", |
| 63 |
#endif |
| 64 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 65 |
"mng-features", |
| 66 |
#endif |
| 67 |
#ifdef PNG_CHECK_cHRM_SUPPORTED |
| 68 |
"check-cHRM", |
| 69 |
#endif |
| 70 |
#ifdef PNG_SET_USER_LIMITS_SUPPORTED |
| 71 |
"user-limits", |
| 72 |
#endif |
| 73 |
NULL |
| 74 |
}; |
| 75 |
|
| 76 |
const char * const * |
| 77 |
i_png_features(void) { |
| 78 |
return features; |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
wiol_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { |
| 83 |
io_glue *ig = png_get_io_ptr(png_ptr); |
| 84 |
ssize_t rc = i_io_read(ig, data, length); |
| 85 |
if (rc == -1) |
| 86 |
png_error(png_ptr, "Read error on an iolayer source."); |
| 87 |
else if (rc != (ssize_t)length) |
| 88 |
png_error(png_ptr, "Read incomplete on an iolayer source."); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
wiol_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { |
| 93 |
ssize_t rc; |
| 94 |
io_glue *ig = png_get_io_ptr(png_ptr); |
| 95 |
rc = i_io_write(ig, data, length); |
| 96 |
if (rc != (ssize_t)length) |
| 97 |
png_error(png_ptr, "Write error on an iolayer source."); |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
wiol_flush_data(png_structp png_ptr) { |
| 102 |
io_glue *ig = png_get_io_ptr(png_ptr); |
| 103 |
if (!i_io_flush(ig)) |
| 104 |
png_error(png_ptr, "Error flushing output"); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
error_handler(png_structp png_ptr, png_const_charp msg) { |
| 109 |
mm_log((1, "PNG error: '%s'\n", msg)); |
| 110 |
|
| 111 |
i_push_error(0, msg); |
| 112 |
longjmp(png_jmpbuf(png_ptr), 1); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
|
| 117 |
For writing a warning might have information about an error, so send |
| 118 |
it to the error stack. |
| 119 |
|
| 120 |
*/ |
| 121 |
static void |
| 122 |
write_warn_handler(png_structp png_ptr, png_const_charp msg) { |
| 123 |
(void)png_ptr; /* unused */ |
| 124 |
mm_log((1, "PNG write warning '%s'\n", msg)); |
| 125 |
|
| 126 |
i_push_error(0, msg); |
| 127 |
} |
| 128 |
|
| 129 |
#define PNG_DIM_MAX 0x7fffffffL |
| 130 |
|
| 131 |
undef_int |
| 132 |
i_writepng_wiol(i_img *im, io_glue *ig) { |
| 133 |
png_structp png_ptr; |
| 134 |
png_infop info_ptr = NULL; |
| 135 |
i_img_dim width,height; |
| 136 |
volatile int cspace,channels; |
| 137 |
int bits; |
| 138 |
volatile int is_bilevel = 0; |
| 139 |
int zero_is_white; |
| 140 |
|
| 141 |
mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig)); |
| 142 |
|
| 143 |
i_clear_error(); |
| 144 |
|
| 145 |
if ((size_t)im->xsize > PNG_UINT_31_MAX |
| 146 |
|| (size_t)im->ysize > PNG_UINT_31_MAX) { |
| 147 |
i_push_error(0, "image too large for PNG"); |
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
height = im->ysize; |
| 152 |
width = im->xsize; |
| 153 |
|
| 154 |
/* if we ever have 64-bit i_img_dim |
| 155 |
* the libpng docs state that png_set_user_limits() can be used to |
| 156 |
* override the PNG_USER_*_MAX limits, but as implemented they |
| 157 |
* don't. We check against the theoretical limit of PNG here, and |
| 158 |
* try to override the limits below, in case the libpng |
| 159 |
* implementation ever matches the documentation. |
| 160 |
* |
| 161 |
* https://sourceforge.net/tracker/?func=detail&atid=105624&aid=3314943&group_id=5624 |
| 162 |
* fixed in libpng 1.5.3 |
| 163 |
*/ |
| 164 |
if (width > PNG_DIM_MAX || height > PNG_DIM_MAX) { |
| 165 |
i_push_error(0, "Image too large for PNG"); |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
channels=im->channels; |
| 170 |
|
| 171 |
if (i_img_is_monochrome(im, &zero_is_white)) { |
| 172 |
is_bilevel = 1; |
| 173 |
bits = 1; |
| 174 |
cspace = PNG_COLOR_TYPE_GRAY; |
| 175 |
mm_log((1, "i_writepng: bilevel output\n")); |
| 176 |
} |
| 177 |
else if (im->type == i_palette_type) { |
| 178 |
int colors = i_colorcount(im); |
| 179 |
|
| 180 |
cspace = PNG_COLOR_TYPE_PALETTE; |
| 181 |
bits = 1; |
| 182 |
while ((1 << bits) < colors) { |
| 183 |
bits += bits; |
| 184 |
} |
| 185 |
mm_log((1, "i_writepng: paletted output\n")); |
| 186 |
} |
| 187 |
else { |
| 188 |
switch (channels) { |
| 189 |
case 1: |
| 190 |
cspace = PNG_COLOR_TYPE_GRAY; |
| 191 |
break; |
| 192 |
case 2: |
| 193 |
cspace = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 194 |
break; |
| 195 |
case 3: |
| 196 |
cspace = PNG_COLOR_TYPE_RGB; |
| 197 |
break; |
| 198 |
case 4: |
| 199 |
cspace = PNG_COLOR_TYPE_RGB_ALPHA; |
| 200 |
break; |
| 201 |
default: |
| 202 |
fprintf(stderr, "Internal error, channels = %d\n", channels); |
| 203 |
abort(); |
| 204 |
} |
| 205 |
bits = im->bits > 8 ? 16 : 8; |
| 206 |
mm_log((1, "i_writepng: direct output\n")); |
| 207 |
} |
| 208 |
|
| 209 |
mm_log((1,"i_writepng: cspace=%d, bits=%d\n",cspace, bits)); |
| 210 |
|
| 211 |
/* Create and initialize the png_struct with the desired error handler |
| 212 |
* functions. If you want to use the default stderr and longjump method, |
| 213 |
* you can supply NULL for the last three parameters. We also check that |
| 214 |
* the library version is compatible with the one used at compile time, |
| 215 |
* in case we are using dynamically linked libraries. REQUIRED. |
| 216 |
*/ |
| 217 |
|
| 218 |
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, |
| 219 |
error_handler, write_warn_handler); |
| 220 |
|
| 221 |
if (png_ptr == NULL) return 0; |
| 222 |
|
| 223 |
|
| 224 |
/* Allocate/initialize the image information data. REQUIRED */ |
| 225 |
info_ptr = png_create_info_struct(png_ptr); |
| 226 |
|
| 227 |
if (info_ptr == NULL) { |
| 228 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
/* Set error handling. REQUIRED if you aren't supplying your own |
| 233 |
* error hadnling functions in the png_create_write_struct() call. |
| 234 |
*/ |
| 235 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 236 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 237 |
return(0); |
| 238 |
} |
| 239 |
|
| 240 |
png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data); |
| 241 |
|
| 242 |
/* Set the image information here. Width and height are up to 2^31, |
| 243 |
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on |
| 244 |
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, |
| 245 |
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, |
| 246 |
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or |
| 247 |
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST |
| 248 |
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED |
| 249 |
*/ |
| 250 |
|
| 251 |
/* by default, libpng (not PNG) limits the image size to a maximum |
| 252 |
* 1000000 pixels in each direction, but Imager doesn't. |
| 253 |
* Configure libpng to avoid that limit. |
| 254 |
*/ |
| 255 |
png_set_user_limits(png_ptr, width, height); |
| 256 |
|
| 257 |
png_set_IHDR(png_ptr, info_ptr, width, height, bits, cspace, |
| 258 |
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
| 259 |
|
| 260 |
if (!set_png_tags(im, png_ptr, info_ptr)) { |
| 261 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
if (is_bilevel) { |
| 266 |
if (!write_bilevel(png_ptr, info_ptr, im)) { |
| 267 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 268 |
return 0; |
| 269 |
} |
| 270 |
} |
| 271 |
else if (im->type == i_palette_type) { |
| 272 |
if (!write_paletted(png_ptr, info_ptr, im)) { |
| 273 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 274 |
return 0; |
| 275 |
} |
| 276 |
} |
| 277 |
else if (bits == 16) { |
| 278 |
if (!write_direct16(png_ptr, info_ptr, im)) { |
| 279 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
} |
| 283 |
else { |
| 284 |
if (!write_direct8(png_ptr, info_ptr, im)) { |
| 285 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 286 |
return 0; |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
png_write_end(png_ptr, info_ptr); |
| 291 |
|
| 292 |
png_destroy_write_struct(&png_ptr, &info_ptr); |
| 293 |
|
| 294 |
if (i_io_close(ig)) |
| 295 |
return 0; |
| 296 |
|
| 297 |
return(1); |
| 298 |
} |
| 299 |
|
| 300 |
typedef struct { |
| 301 |
char *warnings; |
| 302 |
} i_png_read_state, *i_png_read_statep; |
| 303 |
|
| 304 |
static void |
| 305 |
read_warn_handler(png_structp, png_const_charp); |
| 306 |
|
| 307 |
static void |
| 308 |
cleanup_read_state(i_png_read_statep); |
| 309 |
|
| 310 |
i_img* |
| 311 |
i_readpng_wiol(io_glue *ig, int flags) { |
| 312 |
i_img *im = NULL; |
| 313 |
png_structp png_ptr; |
| 314 |
png_infop info_ptr; |
| 315 |
png_uint_32 width, height; |
| 316 |
int bit_depth, color_type, interlace_type; |
| 317 |
int channels; |
| 318 |
unsigned int sig_read; |
| 319 |
i_png_read_state rs; |
| 320 |
|
| 321 |
rs.warnings = NULL; |
| 322 |
sig_read = 0; |
| 323 |
|
| 324 |
mm_log((1,"i_readpng_wiol(ig %p)\n", ig)); |
| 325 |
i_clear_error(); |
| 326 |
|
| 327 |
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, &rs, |
| 328 |
error_handler, read_warn_handler); |
| 329 |
if (!png_ptr) { |
| 330 |
i_push_error(0, "Cannot create PNG read structure"); |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data); |
| 334 |
|
| 335 |
#if defined(PNG_BENIGN_ERRORS_SUPPORTED) |
| 336 |
png_set_benign_errors(png_ptr, (flags & IMPNG_READ_IGNORE_BENIGN_ERRORS) ? 1 : 0); |
| 337 |
#elif PNG_LIBPNG_VER >= 10400 |
| 338 |
if (flags & IMPNG_READ_IGNORE_BENIGN_ERRORS) { |
| 339 |
i_push_error(0, "libpng not configured to ignore benign errors"); |
| 340 |
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
| 341 |
return NULL; |
| 342 |
} |
| 343 |
#else |
| 344 |
if (flags & IMPNG_READ_IGNORE_BENIGN_ERRORS) { |
| 345 |
i_push_error(0, "libpng too old to ignore benign errors"); |
| 346 |
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
#endif |
| 350 |
|
| 351 |
info_ptr = png_create_info_struct(png_ptr); |
| 352 |
if (info_ptr == NULL) { |
| 353 |
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
| 354 |
i_push_error(0, "Cannot create PNG info structure"); |
| 355 |
return NULL; |
| 356 |
} |
| 357 |
|
| 358 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 359 |
if (im) i_img_destroy(im); |
| 360 |
mm_log((1,"i_readpng_wiol: error.\n")); |
| 361 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 362 |
cleanup_read_state(&rs); |
| 363 |
return NULL; |
| 364 |
} |
| 365 |
|
| 366 |
/* we do our own limit checks */ |
| 367 |
png_set_user_limits(png_ptr, PNG_DIM_MAX, PNG_DIM_MAX); |
| 368 |
|
| 369 |
png_set_sig_bytes(png_ptr, sig_read); |
| 370 |
png_read_info(png_ptr, info_ptr); |
| 371 |
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL); |
| 372 |
|
| 373 |
mm_log((1, "png_get_IHDR results: width %u, height %u, bit_depth %d, color_type %d, interlace_type %d\n", |
| 374 |
(unsigned)width, (unsigned)height, bit_depth,color_type,interlace_type)); |
| 375 |
|
| 376 |
CC2C[PNG_COLOR_TYPE_GRAY]=1; |
| 377 |
CC2C[PNG_COLOR_TYPE_PALETTE]=3; |
| 378 |
CC2C[PNG_COLOR_TYPE_RGB]=3; |
| 379 |
CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4; |
| 380 |
CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2; |
| 381 |
channels = CC2C[color_type]; |
| 382 |
|
| 383 |
mm_log((1,"i_readpng_wiol: channels %d\n",channels)); |
| 384 |
|
| 385 |
if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) { |
| 386 |
mm_log((1, "i_readpnm: image size exceeds limits\n")); |
| 387 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
if (color_type == PNG_COLOR_TYPE_PALETTE) { |
| 392 |
im = read_paletted(png_ptr, info_ptr, channels, width, height); |
| 393 |
} |
| 394 |
else if (color_type == PNG_COLOR_TYPE_GRAY |
| 395 |
&& bit_depth == 1 |
| 396 |
&& !png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 397 |
im = read_bilevel(png_ptr, info_ptr, width, height); |
| 398 |
} |
| 399 |
else if (bit_depth == 16) { |
| 400 |
im = read_direct16(png_ptr, info_ptr, channels, width, height); |
| 401 |
} |
| 402 |
else { |
| 403 |
im = read_direct8(png_ptr, info_ptr, channels, width, height); |
| 404 |
} |
| 405 |
|
| 406 |
if (im) |
| 407 |
get_png_tags(im, png_ptr, info_ptr, bit_depth, color_type); |
| 408 |
|
| 409 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 410 |
|
| 411 |
if (im) { |
| 412 |
if (rs.warnings) { |
| 413 |
i_tags_set(&im->tags, "png_warnings", rs.warnings, -1); |
| 414 |
} |
| 415 |
} |
| 416 |
cleanup_read_state(&rs); |
| 417 |
|
| 418 |
mm_log((1,"(%p) <- i_readpng_wiol\n", im)); |
| 419 |
|
| 420 |
return im; |
| 421 |
} |
| 422 |
|
| 423 |
static i_img * |
| 424 |
read_direct8(png_structp png_ptr, png_infop info_ptr, int ochannels, |
| 425 |
i_img_dim width, i_img_dim height) { |
| 426 |
i_img * volatile vim = NULL; |
| 427 |
int color_type = png_get_color_type(png_ptr, info_ptr); |
| 428 |
int bit_depth = png_get_bit_depth(png_ptr, info_ptr); |
| 429 |
i_img_dim y; |
| 430 |
int number_passes, pass; |
| 431 |
i_img *im; |
| 432 |
unsigned char *line; |
| 433 |
unsigned char * volatile vline = NULL; |
| 434 |
volatile int vchannels = ochannels; |
| 435 |
|
| 436 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 437 |
if (vim) i_img_destroy(vim); |
| 438 |
if (vline) myfree(vline); |
| 439 |
|
| 440 |
return NULL; |
| 441 |
} |
| 442 |
|
| 443 |
number_passes = png_set_interlace_handling(png_ptr); |
| 444 |
mm_log((1,"number of passes=%d\n",number_passes)); |
| 445 |
|
| 446 |
png_set_strip_16(png_ptr); |
| 447 |
png_set_packing(png_ptr); |
| 448 |
|
| 449 |
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
| 450 |
png_set_expand(png_ptr); |
| 451 |
|
| 452 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 453 |
vchannels++; |
| 454 |
mm_log((1, "image has transparency, adding alpha: channels = %d\n", vchannels)); |
| 455 |
png_set_expand(png_ptr); |
| 456 |
} |
| 457 |
|
| 458 |
png_read_update_info(png_ptr, info_ptr); |
| 459 |
|
| 460 |
im = vim = i_img_8_new(width,height,vchannels); |
| 461 |
if (!im) { |
| 462 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 463 |
return NULL; |
| 464 |
} |
| 465 |
|
| 466 |
line = vline = mymalloc(vchannels * width); |
| 467 |
for (pass = 0; pass < number_passes; pass++) { |
| 468 |
for (y = 0; y < height; y++) { |
| 469 |
if (pass > 0) |
| 470 |
i_gsamp(im, 0, width, y, line, NULL, vchannels); |
| 471 |
png_read_row(png_ptr,(png_bytep)line, NULL); |
| 472 |
i_psamp(im, 0, width, y, line, NULL, vchannels); |
| 473 |
} |
| 474 |
} |
| 475 |
myfree(line); |
| 476 |
vline = NULL; |
| 477 |
|
| 478 |
png_read_end(png_ptr, info_ptr); |
| 479 |
|
| 480 |
return im; |
| 481 |
} |
| 482 |
|
| 483 |
static i_img * |
| 484 |
read_direct16(png_structp png_ptr, png_infop info_ptr, int ochannels, |
| 485 |
i_img_dim width, i_img_dim height) { |
| 486 |
i_img * volatile vim = NULL; |
| 487 |
i_img_dim x, y; |
| 488 |
int number_passes, pass; |
| 489 |
i_img *im; |
| 490 |
unsigned char *line; |
| 491 |
unsigned char * volatile vline = NULL; |
| 492 |
unsigned *bits_line; |
| 493 |
unsigned * volatile vbits_line = NULL; |
| 494 |
size_t row_bytes; |
| 495 |
volatile int vchannels = ochannels; |
| 496 |
|
| 497 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 498 |
if (vim) i_img_destroy(vim); |
| 499 |
if (vline) myfree(vline); |
| 500 |
if (vbits_line) myfree(vbits_line); |
| 501 |
|
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
number_passes = png_set_interlace_handling(png_ptr); |
| 506 |
mm_log((1,"number of passes=%d\n",number_passes)); |
| 507 |
|
| 508 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { |
| 509 |
vchannels++; |
| 510 |
mm_log((1, "image has transparency, adding alpha: channels = %d\n", vchannels)); |
| 511 |
png_set_expand(png_ptr); |
| 512 |
} |
| 513 |
|
| 514 |
png_read_update_info(png_ptr, info_ptr); |
| 515 |
|
| 516 |
im = vim = i_img_16_new(width,height,vchannels); |
| 517 |
if (!im) { |
| 518 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 519 |
return NULL; |
| 520 |
} |
| 521 |
|
| 522 |
row_bytes = png_get_rowbytes(png_ptr, info_ptr); |
| 523 |
line = vline = mymalloc(row_bytes); |
| 524 |
memset(line, 0, row_bytes); |
| 525 |
bits_line = vbits_line = mymalloc(sizeof(unsigned) * width * vchannels); |
| 526 |
for (pass = 0; pass < number_passes; pass++) { |
| 527 |
for (y = 0; y < height; y++) { |
| 528 |
if (pass > 0) { |
| 529 |
i_gsamp_bits(im, 0, width, y, bits_line, NULL, vchannels, 16); |
| 530 |
for (x = 0; x < width * vchannels; ++x) { |
| 531 |
line[x*2] = bits_line[x] >> 8; |
| 532 |
line[x*2+1] = bits_line[x] & 0xff; |
| 533 |
} |
| 534 |
} |
| 535 |
png_read_row(png_ptr,(png_bytep)line, NULL); |
| 536 |
for (x = 0; x < width * vchannels; ++x) |
| 537 |
bits_line[x] = (line[x*2] << 8) + line[x*2+1]; |
| 538 |
i_psamp_bits(im, 0, width, y, bits_line, NULL, vchannels, 16); |
| 539 |
} |
| 540 |
} |
| 541 |
myfree(line); |
| 542 |
myfree(bits_line); |
| 543 |
vline = NULL; |
| 544 |
vbits_line = NULL; |
| 545 |
|
| 546 |
png_read_end(png_ptr, info_ptr); |
| 547 |
|
| 548 |
return im; |
| 549 |
} |
| 550 |
|
| 551 |
static i_img * |
| 552 |
read_bilevel(png_structp png_ptr, png_infop info_ptr, |
| 553 |
i_img_dim width, i_img_dim height) { |
| 554 |
i_img * volatile vim = NULL; |
| 555 |
i_img_dim x, y; |
| 556 |
int number_passes, pass; |
| 557 |
i_img *im; |
| 558 |
unsigned char *line; |
| 559 |
unsigned char * volatile vline = NULL; |
| 560 |
i_color palette[2]; |
| 561 |
|
| 562 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 563 |
if (vim) i_img_destroy(vim); |
| 564 |
if (vline) myfree(vline); |
| 565 |
|
| 566 |
return NULL; |
| 567 |
} |
| 568 |
|
| 569 |
number_passes = png_set_interlace_handling(png_ptr); |
| 570 |
mm_log((1,"number of passes=%d\n",number_passes)); |
| 571 |
|
| 572 |
png_set_packing(png_ptr); |
| 573 |
|
| 574 |
png_set_expand(png_ptr); |
| 575 |
|
| 576 |
png_read_update_info(png_ptr, info_ptr); |
| 577 |
|
| 578 |
im = vim = i_img_pal_new(width, height, 1, 256); |
| 579 |
if (!im) { |
| 580 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 581 |
return NULL; |
| 582 |
} |
| 583 |
|
| 584 |
palette[0].channel[0] = palette[0].channel[1] = palette[0].channel[2] = |
| 585 |
palette[0].channel[3] = 0; |
| 586 |
palette[1].channel[0] = palette[1].channel[1] = palette[1].channel[2] = |
| 587 |
palette[1].channel[3] = 255; |
| 588 |
i_addcolors(im, palette, 2); |
| 589 |
|
| 590 |
line = vline = mymalloc(width); |
| 591 |
memset(line, 0, width); |
| 592 |
for (pass = 0; pass < number_passes; pass++) { |
| 593 |
for (y = 0; y < height; y++) { |
| 594 |
if (pass > 0) { |
| 595 |
i_gpal(im, 0, width, y, line); |
| 596 |
/* expand indexes back to 0/255 */ |
| 597 |
for (x = 0; x < width; ++x) |
| 598 |
line[x] = line[x] ? 255 : 0; |
| 599 |
} |
| 600 |
png_read_row(png_ptr,(png_bytep)line, NULL); |
| 601 |
|
| 602 |
/* back to palette indexes */ |
| 603 |
for (x = 0; x < width; ++x) |
| 604 |
line[x] = line[x] ? 1 : 0; |
| 605 |
i_ppal(im, 0, width, y, line); |
| 606 |
} |
| 607 |
} |
| 608 |
myfree(line); |
| 609 |
vline = NULL; |
| 610 |
|
| 611 |
png_read_end(png_ptr, info_ptr); |
| 612 |
|
| 613 |
return im; |
| 614 |
} |
| 615 |
|
| 616 |
/* FIXME: do we need to unscale palette color values from the |
| 617 |
supplied alphas? */ |
| 618 |
static i_img * |
| 619 |
read_paletted(png_structp png_ptr, png_infop info_ptr, int ochannels, |
| 620 |
i_img_dim width, i_img_dim height) { |
| 621 |
i_img * volatile vim = NULL; |
| 622 |
int color_type = png_get_color_type(png_ptr, info_ptr); |
| 623 |
int bit_depth = png_get_bit_depth(png_ptr, info_ptr); |
| 624 |
i_img_dim y; |
| 625 |
int number_passes, pass; |
| 626 |
i_img *im; |
| 627 |
unsigned char *line; |
| 628 |
unsigned char * volatile vline = NULL; |
| 629 |
int num_palette, i; |
| 630 |
png_colorp png_palette; |
| 631 |
png_bytep png_pal_trans; |
| 632 |
png_color_16p png_color_trans; |
| 633 |
int num_pal_trans; |
| 634 |
volatile int vchannels = ochannels; |
| 635 |
|
| 636 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 637 |
if (vim) i_img_destroy(vim); |
| 638 |
if (vline) myfree(vline); |
| 639 |
|
| 640 |
return NULL; |
| 641 |
} |
| 642 |
|
| 643 |
number_passes = png_set_interlace_handling(png_ptr); |
| 644 |
mm_log((1,"number of passes=%d\n",number_passes)); |
| 645 |
|
| 646 |
png_set_strip_16(png_ptr); |
| 647 |
png_set_packing(png_ptr); |
| 648 |
|
| 649 |
if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
| 650 |
png_set_expand(png_ptr); |
| 651 |
|
| 652 |
if (!png_get_PLTE(png_ptr, info_ptr, &png_palette, &num_palette)) { |
| 653 |
i_push_error(0, "Paletted image with no PLTE chunk"); |
| 654 |
return NULL; |
| 655 |
} |
| 656 |
|
| 657 |
if (png_get_tRNS(png_ptr, info_ptr, &png_pal_trans, &num_pal_trans, |
| 658 |
&png_color_trans)) { |
| 659 |
vchannels++; |
| 660 |
} |
| 661 |
else { |
| 662 |
num_pal_trans = 0; |
| 663 |
} |
| 664 |
|
| 665 |
png_read_update_info(png_ptr, info_ptr); |
| 666 |
|
| 667 |
im = vim = i_img_pal_new(width, height, vchannels, 256); |
| 668 |
if (!im) { |
| 669 |
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
|
| 673 |
for (i = 0; i < num_palette; ++i) { |
| 674 |
i_color c; |
| 675 |
|
| 676 |
c.rgba.r = png_palette[i].red; |
| 677 |
c.rgba.g = png_palette[i].green; |
| 678 |
c.rgba.b = png_palette[i].blue; |
| 679 |
if (i < num_pal_trans) |
| 680 |
c.rgba.a = png_pal_trans[i]; |
| 681 |
else |
| 682 |
c.rgba.a = 255; |
| 683 |
i_addcolors(im, &c, 1); |
| 684 |
} |
| 685 |
|
| 686 |
line = vline = mymalloc(width); |
| 687 |
for (pass = 0; pass < number_passes; pass++) { |
| 688 |
for (y = 0; y < height; y++) { |
| 689 |
if (pass == 0) { |
| 690 |
memset(line, 0, width); |
| 691 |
} |
| 692 |
else { |
| 693 |
i_gpal(im, 0, width, y, line); |
| 694 |
} |
| 695 |
png_read_row(png_ptr,(png_bytep)line, NULL); |
| 696 |
i_ppal(im, 0, width, y, line); |
| 697 |
} |
| 698 |
} |
| 699 |
myfree(line); |
| 700 |
vline = NULL; |
| 701 |
|
| 702 |
png_read_end(png_ptr, info_ptr); |
| 703 |
|
| 704 |
return im; |
| 705 |
} |
| 706 |
|
| 707 |
struct png_text_name { |
| 708 |
const char *keyword; |
| 709 |
const char *tagname; |
| 710 |
}; |
| 711 |
|
| 712 |
static const struct png_text_name |
| 713 |
text_tags[] = { |
| 714 |
{ "Author", "png_author" }, |
| 715 |
{ "Comment", "i_comment" }, |
| 716 |
{ "Copyright", "png_copyright" }, |
| 717 |
{ "Creation Time", "png_creation_time" }, |
| 718 |
{ "Description", "png_description" }, |
| 719 |
{ "Disclaimer", "png_disclaimer" }, |
| 720 |
{ "Software", "png_software" }, |
| 721 |
{ "Source", "png_source" }, |
| 722 |
{ "Title", "png_title" }, |
| 723 |
{ "Warning", "png_warning" } |
| 724 |
}; |
| 725 |
|
| 726 |
static const int text_tags_count = sizeof(text_tags) / sizeof(*text_tags); |
| 727 |
|
| 728 |
static const char * const |
| 729 |
chroma_tags[] = { |
| 730 |
"png_chroma_white_x", |
| 731 |
"png_chroma_white_y", |
| 732 |
"png_chroma_red_x", |
| 733 |
"png_chroma_red_y", |
| 734 |
"png_chroma_green_x", |
| 735 |
"png_chroma_green_y", |
| 736 |
"png_chroma_blue_x", |
| 737 |
"png_chroma_blue_y" |
| 738 |
}; |
| 739 |
|
| 740 |
static const int chroma_tag_count = sizeof(chroma_tags) / sizeof(*chroma_tags); |
| 741 |
|
| 742 |
static void |
| 743 |
get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr, |
| 744 |
int bit_depth, int color_type) { |
| 745 |
png_uint_32 xres, yres; |
| 746 |
int unit_type; |
| 747 |
|
| 748 |
i_tags_set(&im->tags, "i_format", "png", -1); |
| 749 |
if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) { |
| 750 |
mm_log((1,"pHYs (%u, %u) %d\n", (unsigned)xres, (unsigned)yres, unit_type)); |
| 751 |
if (unit_type == PNG_RESOLUTION_METER) { |
| 752 |
i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 5); |
| 753 |
i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 5); |
| 754 |
} |
| 755 |
else { |
| 756 |
i_tags_setn(&im->tags, "i_xres", xres); |
| 757 |
i_tags_setn(&im->tags, "i_yres", yres); |
| 758 |
i_tags_setn(&im->tags, "i_aspect_only", 1); |
| 759 |
} |
| 760 |
} |
| 761 |
{ |
| 762 |
int interlace = png_get_interlace_type(png_ptr, info_ptr); |
| 763 |
|
| 764 |
i_tags_setn(&im->tags, "png_interlace", interlace != PNG_INTERLACE_NONE); |
| 765 |
switch (interlace) { |
| 766 |
case PNG_INTERLACE_NONE: |
| 767 |
i_tags_set(&im->tags, "png_interlace_name", "none", -1); |
| 768 |
break; |
| 769 |
|
| 770 |
case PNG_INTERLACE_ADAM7: |
| 771 |
i_tags_set(&im->tags, "png_interlace_name", "adam7", -1); |
| 772 |
break; |
| 773 |
|
| 774 |
default: |
| 775 |
i_tags_set(&im->tags, "png_interlace_name", "unknown", -1); |
| 776 |
break; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/* the various readers can call png_set_expand(), libpng will make |
| 781 |
it's internal record of bit_depth at least 8 in that case */ |
| 782 |
i_tags_setn(&im->tags, "png_bits", bit_depth); |
| 783 |
|
| 784 |
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sRGB)) { |
| 785 |
int intent; |
| 786 |
if (png_get_sRGB(png_ptr, info_ptr, &intent)) { |
| 787 |
i_tags_setn(&im->tags, "png_srgb_intent", intent); |
| 788 |
} |
| 789 |
} |
| 790 |
else { |
| 791 |
/* Ignore these if there's an sRGB chunk, libpng simulates |
| 792 |
their existence if there's an sRGB chunk, and the PNG spec says |
| 793 |
that these are ignored if the sRGB is present, so ignore them. |
| 794 |
*/ |
| 795 |
double gamma; |
| 796 |
double chroma[8]; |
| 797 |
|
| 798 |
if (png_get_gAMA(png_ptr, info_ptr, &gamma)) { |
| 799 |
i_tags_set_float2(&im->tags, "png_gamma", 0, gamma, 4); |
| 800 |
} |
| 801 |
|
| 802 |
if (png_get_cHRM(png_ptr, info_ptr, chroma+0, chroma+1, |
| 803 |
chroma+2, chroma+3, chroma+4, chroma+5, |
| 804 |
chroma+6, chroma+7)) { |
| 805 |
int i; |
| 806 |
|
| 807 |
for (i = 0; i < chroma_tag_count; ++i) |
| 808 |
i_tags_set_float2(&im->tags, chroma_tags[i], 0, chroma[i], 4); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
{ |
| 813 |
int num_text; |
| 814 |
png_text *text; |
| 815 |
|
| 816 |
if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { |
| 817 |
int i; |
| 818 |
int custom_index = 0; |
| 819 |
for (i = 0; i < num_text; ++i) { |
| 820 |
int j; |
| 821 |
int found = 0; |
| 822 |
int compressed = text[i].compression == PNG_ITXT_COMPRESSION_zTXt |
| 823 |
|| text[i].compression == PNG_TEXT_COMPRESSION_zTXt; |
| 824 |
|
| 825 |
for (j = 0; j < text_tags_count; ++j) { |
| 826 |
if (strcmp(text_tags[j].keyword, text[i].key) == 0) { |
| 827 |
char tag_name[50]; |
| 828 |
i_tags_set(&im->tags, text_tags[j].tagname, text[i].text, -1); |
| 829 |
if (compressed) { |
| 830 |
sprintf(tag_name, "%s_compressed", text_tags[j].tagname); |
| 831 |
i_tags_setn(&im->tags, tag_name, 1); |
| 832 |
} |
| 833 |
found = 1; |
| 834 |
break; |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
if (!found) { |
| 839 |
char tag_name[50]; |
| 840 |
sprintf(tag_name, "png_text%d_key", custom_index); |
| 841 |
i_tags_set(&im->tags, tag_name, text[i].key, -1); |
| 842 |
sprintf(tag_name, "png_text%d_text", custom_index); |
| 843 |
i_tags_set(&im->tags, tag_name, text[i].text, -1); |
| 844 |
sprintf(tag_name, "png_text%d_type", custom_index); |
| 845 |
i_tags_set(&im->tags, tag_name, |
| 846 |
(text[i].compression == PNG_TEXT_COMPRESSION_NONE |
| 847 |
|| text[i].compression == PNG_TEXT_COMPRESSION_zTXt) ? |
| 848 |
"text" : "itxt", -1); |
| 849 |
if (compressed) { |
| 850 |
sprintf(tag_name, "png_text%d_compressed", custom_index); |
| 851 |
i_tags_setn(&im->tags, tag_name, 1); |
| 852 |
} |
| 853 |
++custom_index; |
| 854 |
} |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
|
| 859 |
{ |
| 860 |
png_time *mod_time; |
| 861 |
|
| 862 |
if (png_get_tIME(png_ptr, info_ptr, &mod_time)) { |
| 863 |
char time_formatted[80]; |
| 864 |
|
| 865 |
sprintf(time_formatted, "%d-%02d-%02dT%02d:%02d:%02d", |
| 866 |
mod_time->year, mod_time->month, mod_time->day, |
| 867 |
mod_time->hour, mod_time->minute, mod_time->second); |
| 868 |
i_tags_set(&im->tags, "png_time", time_formatted, -1); |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
{ |
| 873 |
png_color_16 *back; |
| 874 |
i_color c; |
| 875 |
|
| 876 |
if (png_get_bKGD(png_ptr, info_ptr, &back)) { |
| 877 |
switch (color_type) { |
| 878 |
case PNG_COLOR_TYPE_GRAY: |
| 879 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 880 |
{ |
| 881 |
/* lib png stores the raw gray value rather than scaling it |
| 882 |
to 16-bit (or 8), we use 8-bit color for i_background */ |
| 883 |
|
| 884 |
int gray; |
| 885 |
switch (bit_depth) { |
| 886 |
case 16: |
| 887 |
gray = back->gray >> 8; |
| 888 |
break; |
| 889 |
case 8: |
| 890 |
gray = back->gray; |
| 891 |
break; |
| 892 |
case 4: |
| 893 |
gray = 0x11 * back->gray; |
| 894 |
break; |
| 895 |
case 2: |
| 896 |
gray = 0x55 * back->gray; |
| 897 |
break; |
| 898 |
case 1: |
| 899 |
gray = back->gray ? 0xFF : 0; |
| 900 |
break; |
| 901 |
default: |
| 902 |
gray = 0; |
| 903 |
} |
| 904 |
c.rgb.r = c.rgb.g = c.rgb.b = gray; |
| 905 |
break; |
| 906 |
} |
| 907 |
|
| 908 |
case PNG_COLOR_TYPE_RGB: |
| 909 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 910 |
{ |
| 911 |
c.rgb.r = bit_depth == 16 ? (back->red >> 8) : back->red; |
| 912 |
c.rgb.g = bit_depth == 16 ? (back->green >> 8) : back->green; |
| 913 |
c.rgb.b = bit_depth == 16 ? (back->blue >> 8) : back->blue; |
| 914 |
break; |
| 915 |
} |
| 916 |
|
| 917 |
case PNG_COLOR_TYPE_PALETTE: |
| 918 |
c.rgb.r = back->red; |
| 919 |
c.rgb.g = back->green; |
| 920 |
c.rgb.b = back->blue; |
| 921 |
break; |
| 922 |
} |
| 923 |
|
| 924 |
c.rgba.a = 255; |
| 925 |
i_tags_set_color(&im->tags, "i_background", 0, &c); |
| 926 |
} |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
#define GET_STR_BUF_SIZE 40 |
| 931 |
|
| 932 |
static int |
| 933 |
set_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr) { |
| 934 |
double xres, yres; |
| 935 |
int aspect_only, have_res = 1; |
| 936 |
|
| 937 |
if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) { |
| 938 |
if (i_tags_get_float(&im->tags, "i_yres", 0, &yres)) |
| 939 |
; /* nothing to do */ |
| 940 |
else |
| 941 |
yres = xres; |
| 942 |
} |
| 943 |
else { |
| 944 |
if (i_tags_get_float(&im->tags, "i_yres", 0, &yres)) |
| 945 |
xres = yres; |
| 946 |
else |
| 947 |
have_res = 0; |
| 948 |
} |
| 949 |
if (have_res) { |
| 950 |
aspect_only = 0; |
| 951 |
i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only); |
| 952 |
xres /= 0.0254; |
| 953 |
yres /= 0.0254; |
| 954 |
png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5, |
| 955 |
aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER); |
| 956 |
} |
| 957 |
|
| 958 |
{ |
| 959 |
int intent; |
| 960 |
if (i_tags_get_int(&im->tags, "png_srgb_intent", 0, &intent)) { |
| 961 |
if (intent < 0 || intent >= PNG_sRGB_INTENT_LAST) { |
| 962 |
i_push_error(0, "tag png_srgb_intent out of range"); |
| 963 |
return 0; |
| 964 |
} |
| 965 |
png_set_sRGB(png_ptr, info_ptr, intent); |
| 966 |
} |
| 967 |
else { |
| 968 |
double chroma[8], gamma; |
| 969 |
int i; |
| 970 |
int found_chroma_count = 0; |
| 971 |
|
| 972 |
for (i = 0; i < chroma_tag_count; ++i) { |
| 973 |
if (i_tags_get_float(&im->tags, chroma_tags[i], 0, chroma+i)) |
| 974 |
++found_chroma_count; |
| 975 |
} |
| 976 |
|
| 977 |
if (found_chroma_count) { |
| 978 |
if (found_chroma_count != chroma_tag_count) { |
| 979 |
i_push_error(0, "all png_chroma_* tags must be supplied or none"); |
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
png_set_cHRM(png_ptr, info_ptr, chroma[0], chroma[1], chroma[2], |
| 984 |
chroma[3], chroma[4], chroma[5], chroma[6], chroma[7]); |
| 985 |
} |
| 986 |
|
| 987 |
if (i_tags_get_float(&im->tags, "png_gamma", 0, &gamma)) { |
| 988 |
png_set_gAMA(png_ptr, info_ptr, gamma); |
| 989 |
} |
| 990 |
} |
| 991 |
} |
| 992 |
|
| 993 |
{ |
| 994 |
/* png_set_text() is sparsely documented, it isn't indicated whether |
| 995 |
multiple calls add to or replace the lists of texts, and |
| 996 |
whether the text/keyword data is copied or not. |
| 997 |
|
| 998 |
Examining the linpng code reveals that png_set_text() adds to |
| 999 |
the list and that the text is copied. |
| 1000 |
*/ |
| 1001 |
int i; |
| 1002 |
|
| 1003 |
/* do our standard tags */ |
| 1004 |
for (i = 0; i < text_tags_count; ++i) { |
| 1005 |
char buf[GET_STR_BUF_SIZE]; |
| 1006 |
size_t size; |
| 1007 |
const char *data; |
| 1008 |
|
| 1009 |
data = get_string2(&im->tags, text_tags[i].tagname, buf, &size); |
| 1010 |
if (data) { |
| 1011 |
png_text text; |
| 1012 |
int compression = size > 1000; |
| 1013 |
char compress_tag[40]; |
| 1014 |
|
| 1015 |
if (memchr(data, '\0', size)) { |
| 1016 |
i_push_errorf(0, "tag %s may not contain NUL characters", text_tags[i].tagname); |
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
|
| 1020 |
sprintf(compress_tag, "%s_compressed", text_tags[i].tagname); |
| 1021 |
i_tags_get_int(&im->tags, compress_tag, 0, &compression); |
| 1022 |
|
| 1023 |
text.compression = compression ? PNG_TEXT_COMPRESSION_zTXt |
| 1024 |
: PNG_TEXT_COMPRESSION_NONE; |
| 1025 |
text.key = (char *)text_tags[i].keyword; |
| 1026 |
text.text_length = size; |
| 1027 |
text.text = (char *)data; |
| 1028 |
#ifdef PNG_iTXt_SUPPORTED |
| 1029 |
text.itxt_length = 0; |
| 1030 |
text.lang = NULL; |
| 1031 |
text.lang_key = NULL; |
| 1032 |
#endif |
| 1033 |
|
| 1034 |
png_set_text(png_ptr, info_ptr, &text, 1); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
|
| 1038 |
/* for non-standard tags ensure keywords are limited to 1 to 79 |
| 1039 |
characters */ |
| 1040 |
i = 0; |
| 1041 |
while (1) { |
| 1042 |
char tag_name[50]; |
| 1043 |
char key_buf[GET_STR_BUF_SIZE], value_buf[GET_STR_BUF_SIZE]; |
| 1044 |
const char *key, *value; |
| 1045 |
size_t key_size, value_size; |
| 1046 |
|
| 1047 |
sprintf(tag_name, "png_text%d_key", i); |
| 1048 |
key = get_string2(&im->tags, tag_name, key_buf, &key_size); |
| 1049 |
|
| 1050 |
if (key) { |
| 1051 |
size_t k; |
| 1052 |
if (key_size < 1 || key_size > 79) { |
| 1053 |
i_push_errorf(0, "tag %s must be between 1 and 79 characters in length", tag_name); |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
|
| 1057 |
if (key[0] == ' ' || key[key_size-1] == ' ') { |
| 1058 |
i_push_errorf(0, "tag %s may not contain leading or trailing spaces", tag_name); |
| 1059 |
return 0; |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (strstr(key, " ")) { |
| 1063 |
i_push_errorf(0, "tag %s may not contain consecutive spaces", tag_name); |
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
|
| 1067 |
for (k = 0; k < key_size; ++k) { |
| 1068 |
if (key[k] < 32 || ((unsigned char)key[k] > 126 && (unsigned char)key[k] < 161)) { |
| 1069 |
i_push_errorf(0, "tag %s may only contain Latin1 characters 32-126, 161-255", tag_name); |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
sprintf(tag_name, "png_text%d_text", i); |
| 1076 |
value = get_string2(&im->tags, tag_name, value_buf, &value_size); |
| 1077 |
|
| 1078 |
if (value) { |
| 1079 |
if (memchr(value, '\0', value_size)) { |
| 1080 |
i_push_errorf(0, "tag %s may not contain NUL characters", tag_name); |
| 1081 |
return 0; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
if (key && value) { |
| 1086 |
png_text text; |
| 1087 |
int compression = value_size > 1000; |
| 1088 |
|
| 1089 |
sprintf(tag_name, "png_text%d_compressed", i); |
| 1090 |
i_tags_get_int(&im->tags, tag_name, 0, &compression); |
| 1091 |
|
| 1092 |
text.compression = compression ? PNG_TEXT_COMPRESSION_zTXt |
| 1093 |
: PNG_TEXT_COMPRESSION_NONE; |
| 1094 |
text.key = (char *)key; |
| 1095 |
text.text_length = value_size; |
| 1096 |
text.text = (char *)value; |
| 1097 |
#ifdef PNG_iTXt_SUPPORTED |
| 1098 |
text.itxt_length = 0; |
| 1099 |
text.lang = NULL; |
| 1100 |
text.lang_key = NULL; |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
png_set_text(png_ptr, info_ptr, &text, 1); |
| 1104 |
} |
| 1105 |
else if (key) { |
| 1106 |
i_push_errorf(0, "tag png_text%d_key found but not png_text%d_text", i, i); |
| 1107 |
return 0; |
| 1108 |
} |
| 1109 |
else if (value) { |
| 1110 |
i_push_errorf(0, "tag png_text%d_text found but not png_text%d_key", i, i); |
| 1111 |
return 0; |
| 1112 |
} |
| 1113 |
else { |
| 1114 |
break; |
| 1115 |
} |
| 1116 |
++i; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
|
| 1120 |
{ |
| 1121 |
char buf[GET_STR_BUF_SIZE]; |
| 1122 |
size_t time_size; |
| 1123 |
const char *timestr = get_string2(&im->tags, "png_time", buf, &time_size); |
| 1124 |
|
| 1125 |
if (timestr) { |
| 1126 |
int year, month, day, hour, minute, second; |
| 1127 |
png_time mod_time; |
| 1128 |
|
| 1129 |
if (sscanf(timestr, "%d-%d-%dT%d:%d:%d", &year, &month, &day, &hour, &minute, &second) == 6) { |
| 1130 |
/* rough validation */ |
| 1131 |
if (month < 1 || month > 12 |
| 1132 |
|| day < 1 || day > 31 |
| 1133 |
|| hour < 0 || hour > 23 |
| 1134 |
|| minute < 0 || minute > 59 |
| 1135 |
|| second < 0 || second > 60) { |
| 1136 |
i_push_error(0, "invalid date/time for png_time"); |
| 1137 |
return 0; |
| 1138 |
} |
| 1139 |
mod_time.year = year; |
| 1140 |
mod_time.month = month; |
| 1141 |
mod_time.day = day; |
| 1142 |
mod_time.hour = hour; |
| 1143 |
mod_time.minute = minute; |
| 1144 |
mod_time.second = second; |
| 1145 |
|
| 1146 |
png_set_tIME(png_ptr, info_ptr, &mod_time); |
| 1147 |
} |
| 1148 |
else { |
| 1149 |
i_push_error(0, "png_time must be formatted 'y-m-dTh:m:s'"); |
| 1150 |
return 0; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
{ |
| 1156 |
int level; |
| 1157 |
if (i_tags_get_int(&im->tags, "png_compression_level", 0, &level)) { |
| 1158 |
if (level >= Z_NO_COMPRESSION && level <= Z_BEST_COMPRESSION) |
| 1159 |
png_set_compression_level(png_ptr, level); |
| 1160 |
else { |
| 1161 |
i_push_errorf(0, "png_compression_level must be between %d and %d", |
| 1162 |
Z_NO_COMPRESSION, Z_BEST_COMPRESSION); |
| 1163 |
return 0; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
{ |
| 1169 |
/* no bKGD support yet, maybe later |
| 1170 |
it may be simpler to do it in the individual writers |
| 1171 |
*/ |
| 1172 |
} |
| 1173 |
|
| 1174 |
return 1; |
| 1175 |
} |
| 1176 |
|
| 1177 |
static const char * |
| 1178 |
get_string2(i_img_tags *tags, const char *name, char *buf, size_t *size) { |
| 1179 |
int index; |
| 1180 |
|
| 1181 |
if (i_tags_find(tags, name, 0, &index)) { |
| 1182 |
const i_img_tag *entry = tags->tags + index; |
| 1183 |
|
| 1184 |
if (entry->data) { |
| 1185 |
*size = entry->size; |
| 1186 |
|
| 1187 |
return entry->data; |
| 1188 |
} |
| 1189 |
else { |
| 1190 |
*size = sprintf(buf, "%d", entry->idata); |
| 1191 |
|
| 1192 |
return buf; |
| 1193 |
} |
| 1194 |
} |
| 1195 |
return NULL; |
| 1196 |
} |
| 1197 |
|
| 1198 |
static int |
| 1199 |
write_direct8(png_structp png_ptr, png_infop info_ptr, i_img *im) { |
| 1200 |
unsigned char *data, *volatile vdata = NULL; |
| 1201 |
i_img_dim y; |
| 1202 |
|
| 1203 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 1204 |
if (vdata) |
| 1205 |
myfree(vdata); |
| 1206 |
|
| 1207 |
return 0; |
| 1208 |
} |
| 1209 |
|
| 1210 |
png_write_info(png_ptr, info_ptr); |
| 1211 |
|
| 1212 |
vdata = data = mymalloc(im->xsize * im->channels); |
| 1213 |
for (y = 0; y < im->ysize; y++) { |
| 1214 |
i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels); |
| 1215 |
png_write_row(png_ptr, (png_bytep)data); |
| 1216 |
} |
| 1217 |
myfree(data); |
| 1218 |
|
| 1219 |
return 1; |
| 1220 |
} |
| 1221 |
|
| 1222 |
static int |
| 1223 |
write_direct16(png_structp png_ptr, png_infop info_ptr, i_img *im) { |
| 1224 |
unsigned *data, *volatile vdata = NULL; |
| 1225 |
unsigned char *tran_data, * volatile vtran_data = NULL; |
| 1226 |
i_img_dim samples_per_row = im->xsize * im->channels; |
| 1227 |
|
| 1228 |
i_img_dim y; |
| 1229 |
|
| 1230 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 1231 |
if (vdata) |
| 1232 |
myfree(vdata); |
| 1233 |
if (vtran_data) |
| 1234 |
myfree(vtran_data); |
| 1235 |
|
| 1236 |
return 0; |
| 1237 |
} |
| 1238 |
|
| 1239 |
png_write_info(png_ptr, info_ptr); |
| 1240 |
|
| 1241 |
vdata = data = mymalloc(samples_per_row * sizeof(unsigned)); |
| 1242 |
vtran_data = tran_data = mymalloc(samples_per_row * 2); |
| 1243 |
for (y = 0; y < im->ysize; y++) { |
| 1244 |
i_img_dim i; |
| 1245 |
unsigned char *p = tran_data; |
| 1246 |
i_gsamp_bits(im, 0, im->xsize, y, data, NULL, im->channels, 16); |
| 1247 |
for (i = 0; i < samples_per_row; ++i) { |
| 1248 |
p[0] = data[i] >> 8; |
| 1249 |
p[1] = data[i] & 0xff; |
| 1250 |
p += 2; |
| 1251 |
} |
| 1252 |
png_write_row(png_ptr, (png_bytep)tran_data); |
| 1253 |
} |
| 1254 |
myfree(tran_data); |
| 1255 |
myfree(data); |
| 1256 |
|
| 1257 |
return 1; |
| 1258 |
} |
| 1259 |
|
| 1260 |
static int |
| 1261 |
write_paletted(png_structp png_ptr, png_infop info_ptr, i_img *im) { |
| 1262 |
unsigned char *data, *volatile vdata = NULL; |
| 1263 |
i_img_dim y; |
| 1264 |
unsigned char pal_map[256]; |
| 1265 |
png_color pcolors[256]; |
| 1266 |
i_color colors[256]; |
| 1267 |
int count = i_colorcount(im); |
| 1268 |
int i; |
| 1269 |
|
| 1270 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 1271 |
if (vdata) |
| 1272 |
myfree(vdata); |
| 1273 |
|
| 1274 |
return 0; |
| 1275 |
} |
| 1276 |
|
| 1277 |
i_getcolors(im, 0, colors, count); |
| 1278 |
if (im->channels < 3) { |
| 1279 |
/* convert the greyscale palette to color */ |
| 1280 |
int i; |
| 1281 |
for (i = 0; i < count; ++i) { |
| 1282 |
i_color *c = colors + i; |
| 1283 |
c->channel[3] = c->channel[1]; |
| 1284 |
c->channel[2] = c->channel[1] = c->channel[0]; |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
if (i_img_has_alpha(im)) { |
| 1289 |
int i; |
| 1290 |
int pal_index = 0; |
| 1291 |
|
| 1292 |
/* the PNG spec suggests sorting the palette by alpha, but that's |
| 1293 |
unnecessary - all we want to do is move the opaque entries to |
| 1294 |
the end */ |
| 1295 |
for (i = 0; i < count; ++i) { |
| 1296 |
if (colors[i].rgba.a != 255) { |
| 1297 |
pal_map[i] = pal_index++; |
| 1298 |
} |
| 1299 |
} |
| 1300 |
for (i = 0; i < count; ++i) { |
| 1301 |
if (colors[i].rgba.a == 255) { |
| 1302 |
pal_map[i] = pal_index++; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
for (i = 0; i < count; ++i) { |
| 1308 |
int desti = i_img_has_alpha(im) ? pal_map[i] : i; |
| 1309 |
|
| 1310 |
pcolors[desti].red = colors[i].rgb.r; |
| 1311 |
pcolors[desti].green = colors[i].rgb.g; |
| 1312 |
pcolors[desti].blue = colors[i].rgb.b; |
| 1313 |
} |
| 1314 |
|
| 1315 |
png_set_PLTE(png_ptr, info_ptr, pcolors, count); |
| 1316 |
|
| 1317 |
if (i_img_has_alpha(im)) { |
| 1318 |
unsigned char trans[256]; |
| 1319 |
int i; |
| 1320 |
int len = 0; |
| 1321 |
|
| 1322 |
for (i = 0; i < count; ++i) { |
| 1323 |
if (colors[i].rgba.a != 255) { |
| 1324 |
int pmi = pal_map[i]; |
| 1325 |
if (len < pmi+1) { |
| 1326 |
len = pmi + 1; |
| 1327 |
} |
| 1328 |
trans[pmi] = colors[i].rgba.a; |
| 1329 |
} |
| 1330 |
} |
| 1331 |
if (len) { |
| 1332 |
png_set_tRNS(png_ptr, info_ptr, trans, len, NULL); |
| 1333 |
} |
| 1334 |
} |
| 1335 |
|
| 1336 |
png_write_info(png_ptr, info_ptr); |
| 1337 |
|
| 1338 |
png_set_packing(png_ptr); |
| 1339 |
|
| 1340 |
vdata = data = mymalloc(im->xsize); |
| 1341 |
for (y = 0; y < im->ysize; y++) { |
| 1342 |
i_gpal(im, 0, im->xsize, y, data); |
| 1343 |
if (i_img_has_alpha(im)) { |
| 1344 |
i_img_dim x; |
| 1345 |
for (x = 0; x < im->xsize; ++x) |
| 1346 |
data[x] = pal_map[data[x]]; |
| 1347 |
} |
| 1348 |
png_write_row(png_ptr, (png_bytep)data); |
| 1349 |
} |
| 1350 |
myfree(data); |
| 1351 |
|
| 1352 |
return 1; |
| 1353 |
} |
| 1354 |
|
| 1355 |
static int |
| 1356 |
write_bilevel(png_structp png_ptr, png_infop info_ptr, i_img *im) { |
| 1357 |
unsigned char *data, *volatile vdata = NULL; |
| 1358 |
i_img_dim y; |
| 1359 |
|
| 1360 |
if (setjmp(png_jmpbuf(png_ptr))) { |
| 1361 |
if (vdata) |
| 1362 |
myfree(vdata); |
| 1363 |
|
| 1364 |
return 0; |
| 1365 |
} |
| 1366 |
|
| 1367 |
png_write_info(png_ptr, info_ptr); |
| 1368 |
|
| 1369 |
png_set_packing(png_ptr); |
| 1370 |
|
| 1371 |
vdata = data = mymalloc(im->xsize); |
| 1372 |
for (y = 0; y < im->ysize; y++) { |
| 1373 |
i_gsamp(im, 0, im->xsize, y, data, NULL, 1); |
| 1374 |
png_write_row(png_ptr, (png_bytep)data); |
| 1375 |
} |
| 1376 |
myfree(data); |
| 1377 |
|
| 1378 |
return 1; |
| 1379 |
} |
| 1380 |
|
| 1381 |
static void |
| 1382 |
read_warn_handler(png_structp png_ptr, png_const_charp msg) { |
| 1383 |
i_png_read_statep rs = (i_png_read_statep)png_get_error_ptr(png_ptr); |
| 1384 |
char *workp; |
| 1385 |
size_t new_size; |
| 1386 |
|
| 1387 |
mm_log((1, "PNG read warning '%s'\n", msg)); |
| 1388 |
|
| 1389 |
/* in case this is part of an error report */ |
| 1390 |
i_push_error(0, msg); |
| 1391 |
|
| 1392 |
/* and save in the warnings so if we do manage to succeed, we |
| 1393 |
* can save it as a tag |
| 1394 |
*/ |
| 1395 |
new_size = (rs->warnings ? strlen(rs->warnings) : 0) |
| 1396 |
+ 1 /* NUL */ |
| 1397 |
+ strlen(msg) /* new text */ |
| 1398 |
+ 1; /* newline */ |
| 1399 |
workp = myrealloc(rs->warnings, new_size); |
| 1400 |
if (!rs->warnings) |
| 1401 |
*workp = '\0'; |
| 1402 |
strcat(workp, msg); |
| 1403 |
strcat(workp, "\n"); |
| 1404 |
rs->warnings = workp; |
| 1405 |
} |
| 1406 |
|
| 1407 |
static void |
| 1408 |
cleanup_read_state(i_png_read_statep rs) { |
| 1409 |
if (rs->warnings) |
| 1410 |
myfree(rs->warnings); |
| 1411 |
} |