| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "pngpriv.h" |
| 18 |
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) |
| 19 |
# include <errno.h> |
| 20 |
#endif |
| 21 |
|
| 22 |
#ifdef PNG_READ_SUPPORTED |
| 23 |
|
| 24 |
|
| 25 |
PNG_FUNCTION(png_structp,PNGAPI |
| 26 |
png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr, |
| 27 |
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) |
| 28 |
{ |
| 29 |
#ifndef PNG_USER_MEM_SUPPORTED |
| 30 |
png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 31 |
error_fn, warn_fn, NULL, NULL, NULL); |
| 32 |
#else |
| 33 |
return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, |
| 34 |
warn_fn, NULL, NULL, NULL); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
PNG_FUNCTION(png_structp,PNGAPI |
| 41 |
png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, |
| 42 |
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, |
| 43 |
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) |
| 44 |
{ |
| 45 |
png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, |
| 46 |
error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); |
| 47 |
#endif |
| 48 |
|
| 49 |
if (png_ptr != NULL) |
| 50 |
{ |
| 51 |
png_ptr->mode = PNG_IS_READ_STRUCT; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
# ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 57 |
png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; |
| 58 |
# endif |
| 59 |
|
| 60 |
# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED |
| 61 |
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
# if PNG_RELEASE_BUILD |
| 67 |
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; |
| 68 |
# endif |
| 69 |
# endif |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
png_set_read_fn(png_ptr, NULL, NULL); |
| 76 |
} |
| 77 |
|
| 78 |
return png_ptr; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
void PNGAPI |
| 92 |
png_read_info(png_structrp png_ptr, png_inforp info_ptr) |
| 93 |
{ |
| 94 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 95 |
int keep; |
| 96 |
#endif |
| 97 |
|
| 98 |
png_debug(1, "in png_read_info"); |
| 99 |
|
| 100 |
if (png_ptr == NULL || info_ptr == NULL) |
| 101 |
return; |
| 102 |
|
| 103 |
|
| 104 |
png_read_sig(png_ptr, info_ptr); |
| 105 |
|
| 106 |
for (;;) |
| 107 |
{ |
| 108 |
png_uint_32 length = png_read_chunk_header(png_ptr); |
| 109 |
png_uint_32 chunk_name = png_ptr->chunk_name; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (chunk_name == png_IDAT) |
| 115 |
{ |
| 116 |
if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) |
| 117 |
png_chunk_error(png_ptr, "Missing IHDR before IDAT"); |
| 118 |
|
| 119 |
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 120 |
(png_ptr->mode & PNG_HAVE_PLTE) == 0) |
| 121 |
png_chunk_error(png_ptr, "Missing PLTE before IDAT"); |
| 122 |
|
| 123 |
else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) |
| 124 |
png_chunk_benign_error(png_ptr, "Too many IDATs found"); |
| 125 |
|
| 126 |
png_ptr->mode |= PNG_HAVE_IDAT; |
| 127 |
} |
| 128 |
|
| 129 |
else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) |
| 130 |
{ |
| 131 |
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
| 132 |
png_ptr->mode |= PNG_AFTER_IDAT; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
if (chunk_name == png_IHDR) |
| 139 |
png_handle_IHDR(png_ptr, info_ptr, length); |
| 140 |
|
| 141 |
else if (chunk_name == png_IEND) |
| 142 |
png_handle_IEND(png_ptr, info_ptr, length); |
| 143 |
|
| 144 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 145 |
else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
| 146 |
{ |
| 147 |
png_handle_unknown(png_ptr, info_ptr, length, keep); |
| 148 |
|
| 149 |
if (chunk_name == png_PLTE) |
| 150 |
png_ptr->mode |= PNG_HAVE_PLTE; |
| 151 |
|
| 152 |
else if (chunk_name == png_IDAT) |
| 153 |
{ |
| 154 |
png_ptr->idat_size = 0; |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
#endif |
| 159 |
else if (chunk_name == png_PLTE) |
| 160 |
png_handle_PLTE(png_ptr, info_ptr, length); |
| 161 |
|
| 162 |
else if (chunk_name == png_IDAT) |
| 163 |
{ |
| 164 |
png_ptr->idat_size = length; |
| 165 |
break; |
| 166 |
} |
| 167 |
|
| 168 |
#ifdef PNG_READ_bKGD_SUPPORTED |
| 169 |
else if (chunk_name == png_bKGD) |
| 170 |
png_handle_bKGD(png_ptr, info_ptr, length); |
| 171 |
#endif |
| 172 |
|
| 173 |
#ifdef PNG_READ_cHRM_SUPPORTED |
| 174 |
else if (chunk_name == png_cHRM) |
| 175 |
png_handle_cHRM(png_ptr, info_ptr, length); |
| 176 |
#endif |
| 177 |
|
| 178 |
#ifdef PNG_READ_eXIf_SUPPORTED |
| 179 |
else if (chunk_name == png_eXIf) |
| 180 |
png_handle_eXIf(png_ptr, info_ptr, length); |
| 181 |
#endif |
| 182 |
|
| 183 |
#ifdef PNG_READ_gAMA_SUPPORTED |
| 184 |
else if (chunk_name == png_gAMA) |
| 185 |
png_handle_gAMA(png_ptr, info_ptr, length); |
| 186 |
#endif |
| 187 |
|
| 188 |
#ifdef PNG_READ_hIST_SUPPORTED |
| 189 |
else if (chunk_name == png_hIST) |
| 190 |
png_handle_hIST(png_ptr, info_ptr, length); |
| 191 |
#endif |
| 192 |
|
| 193 |
#ifdef PNG_READ_oFFs_SUPPORTED |
| 194 |
else if (chunk_name == png_oFFs) |
| 195 |
png_handle_oFFs(png_ptr, info_ptr, length); |
| 196 |
#endif |
| 197 |
|
| 198 |
#ifdef PNG_READ_pCAL_SUPPORTED |
| 199 |
else if (chunk_name == png_pCAL) |
| 200 |
png_handle_pCAL(png_ptr, info_ptr, length); |
| 201 |
#endif |
| 202 |
|
| 203 |
#ifdef PNG_READ_sCAL_SUPPORTED |
| 204 |
else if (chunk_name == png_sCAL) |
| 205 |
png_handle_sCAL(png_ptr, info_ptr, length); |
| 206 |
#endif |
| 207 |
|
| 208 |
#ifdef PNG_READ_pHYs_SUPPORTED |
| 209 |
else if (chunk_name == png_pHYs) |
| 210 |
png_handle_pHYs(png_ptr, info_ptr, length); |
| 211 |
#endif |
| 212 |
|
| 213 |
#ifdef PNG_READ_sBIT_SUPPORTED |
| 214 |
else if (chunk_name == png_sBIT) |
| 215 |
png_handle_sBIT(png_ptr, info_ptr, length); |
| 216 |
#endif |
| 217 |
|
| 218 |
#ifdef PNG_READ_sRGB_SUPPORTED |
| 219 |
else if (chunk_name == png_sRGB) |
| 220 |
png_handle_sRGB(png_ptr, info_ptr, length); |
| 221 |
#endif |
| 222 |
|
| 223 |
#ifdef PNG_READ_iCCP_SUPPORTED |
| 224 |
else if (chunk_name == png_iCCP) |
| 225 |
png_handle_iCCP(png_ptr, info_ptr, length); |
| 226 |
#endif |
| 227 |
|
| 228 |
#ifdef PNG_READ_sPLT_SUPPORTED |
| 229 |
else if (chunk_name == png_sPLT) |
| 230 |
png_handle_sPLT(png_ptr, info_ptr, length); |
| 231 |
#endif |
| 232 |
|
| 233 |
#ifdef PNG_READ_tEXt_SUPPORTED |
| 234 |
else if (chunk_name == png_tEXt) |
| 235 |
png_handle_tEXt(png_ptr, info_ptr, length); |
| 236 |
#endif |
| 237 |
|
| 238 |
#ifdef PNG_READ_tIME_SUPPORTED |
| 239 |
else if (chunk_name == png_tIME) |
| 240 |
png_handle_tIME(png_ptr, info_ptr, length); |
| 241 |
#endif |
| 242 |
|
| 243 |
#ifdef PNG_READ_tRNS_SUPPORTED |
| 244 |
else if (chunk_name == png_tRNS) |
| 245 |
png_handle_tRNS(png_ptr, info_ptr, length); |
| 246 |
#endif |
| 247 |
|
| 248 |
#ifdef PNG_READ_zTXt_SUPPORTED |
| 249 |
else if (chunk_name == png_zTXt) |
| 250 |
png_handle_zTXt(png_ptr, info_ptr, length); |
| 251 |
#endif |
| 252 |
|
| 253 |
#ifdef PNG_READ_iTXt_SUPPORTED |
| 254 |
else if (chunk_name == png_iTXt) |
| 255 |
png_handle_iTXt(png_ptr, info_ptr, length); |
| 256 |
#endif |
| 257 |
|
| 258 |
else |
| 259 |
png_handle_unknown(png_ptr, info_ptr, length, |
| 260 |
PNG_HANDLE_CHUNK_AS_DEFAULT); |
| 261 |
} |
| 262 |
} |
| 263 |
#endif |
| 264 |
|
| 265 |
|
| 266 |
void PNGAPI |
| 267 |
png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) |
| 268 |
{ |
| 269 |
png_debug(1, "in png_read_update_info"); |
| 270 |
|
| 271 |
if (png_ptr != NULL) |
| 272 |
{ |
| 273 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 274 |
{ |
| 275 |
png_read_start_row(png_ptr); |
| 276 |
|
| 277 |
# ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 278 |
png_read_transform_info(png_ptr, info_ptr); |
| 279 |
# else |
| 280 |
PNG_UNUSED(info_ptr) |
| 281 |
# endif |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
else |
| 286 |
png_app_error(png_ptr, |
| 287 |
"png_read_update_info/png_start_read_image: duplicate call"); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
void PNGAPI |
| 298 |
png_start_read_image(png_structrp png_ptr) |
| 299 |
{ |
| 300 |
png_debug(1, "in png_start_read_image"); |
| 301 |
|
| 302 |
if (png_ptr != NULL) |
| 303 |
{ |
| 304 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 305 |
png_read_start_row(png_ptr); |
| 306 |
|
| 307 |
|
| 308 |
else |
| 309 |
png_app_error(png_ptr, |
| 310 |
"png_start_read_image/png_read_update_info: duplicate call"); |
| 311 |
} |
| 312 |
} |
| 313 |
#endif |
| 314 |
|
| 315 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 316 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
static void |
| 321 |
png_do_read_intrapixel(png_row_infop row_info, png_bytep row) |
| 322 |
{ |
| 323 |
png_debug(1, "in png_do_read_intrapixel"); |
| 324 |
|
| 325 |
if ( |
| 326 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 327 |
{ |
| 328 |
int bytes_per_pixel; |
| 329 |
png_uint_32 row_width = row_info->width; |
| 330 |
|
| 331 |
if (row_info->bit_depth == 8) |
| 332 |
{ |
| 333 |
png_bytep rp; |
| 334 |
png_uint_32 i; |
| 335 |
|
| 336 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 337 |
bytes_per_pixel = 3; |
| 338 |
|
| 339 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 340 |
bytes_per_pixel = 4; |
| 341 |
|
| 342 |
else |
| 343 |
return; |
| 344 |
|
| 345 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 346 |
{ |
| 347 |
*(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); |
| 348 |
*(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); |
| 349 |
} |
| 350 |
} |
| 351 |
else if (row_info->bit_depth == 16) |
| 352 |
{ |
| 353 |
png_bytep rp; |
| 354 |
png_uint_32 i; |
| 355 |
|
| 356 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 357 |
bytes_per_pixel = 6; |
| 358 |
|
| 359 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 360 |
bytes_per_pixel = 8; |
| 361 |
|
| 362 |
else |
| 363 |
return; |
| 364 |
|
| 365 |
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) |
| 366 |
{ |
| 367 |
png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1); |
| 368 |
png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3); |
| 369 |
png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5); |
| 370 |
png_uint_32 red = (s0 + s1 + 65536) & 0xffff; |
| 371 |
png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; |
| 372 |
*(rp ) = (png_byte)((red >> 8) & 0xff); |
| 373 |
*(rp + 1) = (png_byte)(red & 0xff); |
| 374 |
*(rp + 4) = (png_byte)((blue >> 8) & 0xff); |
| 375 |
*(rp + 5) = (png_byte)(blue & 0xff); |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
#endif |
| 381 |
|
| 382 |
void PNGAPI |
| 383 |
png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) |
| 384 |
{ |
| 385 |
png_row_info row_info; |
| 386 |
|
| 387 |
if (png_ptr == NULL) |
| 388 |
return; |
| 389 |
|
| 390 |
png_debug2(1, "in png_read_row (row %lu, pass %d)", |
| 391 |
(unsigned long)png_ptr->row_number, png_ptr->pass); |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 397 |
png_read_start_row(png_ptr); |
| 398 |
|
| 399 |
|
| 400 |
row_info.width = png_ptr->iwidth; |
| 401 |
row_info.color_type = png_ptr->color_type; |
| 402 |
row_info.bit_depth = png_ptr->bit_depth; |
| 403 |
row_info.channels = png_ptr->channels; |
| 404 |
row_info.pixel_depth = png_ptr->pixel_depth; |
| 405 |
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); |
| 406 |
|
| 407 |
#ifdef PNG_WARNINGS_SUPPORTED |
| 408 |
if (png_ptr->row_number == 0 && png_ptr->pass == 0) |
| 409 |
{ |
| 410 |
|
| 411 |
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) |
| 412 |
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
| 413 |
png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); |
| 414 |
#endif |
| 415 |
|
| 416 |
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) |
| 417 |
if ((png_ptr->transformations & PNG_FILLER) != 0) |
| 418 |
png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); |
| 419 |
#endif |
| 420 |
|
| 421 |
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ |
| 422 |
!defined(PNG_READ_PACKSWAP_SUPPORTED) |
| 423 |
if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
| 424 |
png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); |
| 425 |
#endif |
| 426 |
|
| 427 |
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) |
| 428 |
if ((png_ptr->transformations & PNG_PACK) != 0) |
| 429 |
png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); |
| 430 |
#endif |
| 431 |
|
| 432 |
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) |
| 433 |
if ((png_ptr->transformations & PNG_SHIFT) != 0) |
| 434 |
png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); |
| 435 |
#endif |
| 436 |
|
| 437 |
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) |
| 438 |
if ((png_ptr->transformations & PNG_BGR) != 0) |
| 439 |
png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); |
| 440 |
#endif |
| 441 |
|
| 442 |
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) |
| 443 |
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
| 444 |
png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); |
| 445 |
#endif |
| 446 |
} |
| 447 |
#endif |
| 448 |
|
| 449 |
#ifdef PNG_READ_INTERLACING_SUPPORTED |
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
if (png_ptr->interlaced != 0 && |
| 457 |
(png_ptr->transformations & PNG_INTERLACE) != 0) |
| 458 |
{ |
| 459 |
switch (png_ptr->pass) |
| 460 |
{ |
| 461 |
case 0: |
| 462 |
if (png_ptr->row_number & 0x07) |
| 463 |
{ |
| 464 |
if (dsp_row != NULL) |
| 465 |
png_combine_row(png_ptr, dsp_row, 1); |
| 466 |
png_read_finish_row(png_ptr); |
| 467 |
return; |
| 468 |
} |
| 469 |
break; |
| 470 |
|
| 471 |
case 1: |
| 472 |
if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) |
| 473 |
{ |
| 474 |
if (dsp_row != NULL) |
| 475 |
png_combine_row(png_ptr, dsp_row, 1); |
| 476 |
|
| 477 |
png_read_finish_row(png_ptr); |
| 478 |
return; |
| 479 |
} |
| 480 |
break; |
| 481 |
|
| 482 |
case 2: |
| 483 |
if ((png_ptr->row_number & 0x07) != 4) |
| 484 |
{ |
| 485 |
if (dsp_row != NULL && (png_ptr->row_number & 4)) |
| 486 |
png_combine_row(png_ptr, dsp_row, 1); |
| 487 |
|
| 488 |
png_read_finish_row(png_ptr); |
| 489 |
return; |
| 490 |
} |
| 491 |
break; |
| 492 |
|
| 493 |
case 3: |
| 494 |
if ((png_ptr->row_number & 3) || png_ptr->width < 3) |
| 495 |
{ |
| 496 |
if (dsp_row != NULL) |
| 497 |
png_combine_row(png_ptr, dsp_row, 1); |
| 498 |
|
| 499 |
png_read_finish_row(png_ptr); |
| 500 |
return; |
| 501 |
} |
| 502 |
break; |
| 503 |
|
| 504 |
case 4: |
| 505 |
if ((png_ptr->row_number & 3) != 2) |
| 506 |
{ |
| 507 |
if (dsp_row != NULL && (png_ptr->row_number & 2)) |
| 508 |
png_combine_row(png_ptr, dsp_row, 1); |
| 509 |
|
| 510 |
png_read_finish_row(png_ptr); |
| 511 |
return; |
| 512 |
} |
| 513 |
break; |
| 514 |
|
| 515 |
case 5: |
| 516 |
if ((png_ptr->row_number & 1) || png_ptr->width < 2) |
| 517 |
{ |
| 518 |
if (dsp_row != NULL) |
| 519 |
png_combine_row(png_ptr, dsp_row, 1); |
| 520 |
|
| 521 |
png_read_finish_row(png_ptr); |
| 522 |
return; |
| 523 |
} |
| 524 |
break; |
| 525 |
|
| 526 |
default: |
| 527 |
case 6: |
| 528 |
if ((png_ptr->row_number & 1) == 0) |
| 529 |
{ |
| 530 |
png_read_finish_row(png_ptr); |
| 531 |
return; |
| 532 |
} |
| 533 |
break; |
| 534 |
} |
| 535 |
} |
| 536 |
#endif |
| 537 |
|
| 538 |
if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) |
| 539 |
png_error(png_ptr, "Invalid attempt to read row data"); |
| 540 |
|
| 541 |
|
| 542 |
png_ptr->row_buf[0]=255; |
| 543 |
png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); |
| 544 |
|
| 545 |
if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) |
| 546 |
{ |
| 547 |
if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) |
| 548 |
png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, |
| 549 |
png_ptr->prev_row + 1, png_ptr->row_buf[0]); |
| 550 |
else |
| 551 |
png_error(png_ptr, "bad adaptive filter value"); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); |
| 560 |
|
| 561 |
#ifdef PNG_MNG_FEATURES_SUPPORTED |
| 562 |
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && |
| 563 |
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) |
| 564 |
{ |
| 565 |
|
| 566 |
png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); |
| 567 |
} |
| 568 |
#endif |
| 569 |
|
| 570 |
#ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 571 |
if (png_ptr->transformations |
| 572 |
# ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 573 |
|| png_ptr->num_palette_max >= 0 |
| 574 |
# endif |
| 575 |
) |
| 576 |
png_do_read_transformations(png_ptr, &row_info); |
| 577 |
#endif |
| 578 |
|
| 579 |
|
| 580 |
if (png_ptr->transformed_pixel_depth == 0) |
| 581 |
{ |
| 582 |
png_ptr->transformed_pixel_depth = row_info.pixel_depth; |
| 583 |
if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) |
| 584 |
png_error(png_ptr, "sequential row overflow"); |
| 585 |
} |
| 586 |
|
| 587 |
else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) |
| 588 |
png_error(png_ptr, "internal sequential row size calculation error"); |
| 589 |
|
| 590 |
#ifdef PNG_READ_INTERLACING_SUPPORTED |
| 591 |
|
| 592 |
if (png_ptr->interlaced != 0 && |
| 593 |
(png_ptr->transformations & PNG_INTERLACE) != 0) |
| 594 |
{ |
| 595 |
if (png_ptr->pass < 6) |
| 596 |
png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, |
| 597 |
png_ptr->transformations); |
| 598 |
|
| 599 |
if (dsp_row != NULL) |
| 600 |
png_combine_row(png_ptr, dsp_row, 1); |
| 601 |
|
| 602 |
if (row != NULL) |
| 603 |
png_combine_row(png_ptr, row, 0); |
| 604 |
} |
| 605 |
|
| 606 |
else |
| 607 |
#endif |
| 608 |
{ |
| 609 |
if (row != NULL) |
| 610 |
png_combine_row(png_ptr, row, -1); |
| 611 |
|
| 612 |
if (dsp_row != NULL) |
| 613 |
png_combine_row(png_ptr, dsp_row, -1); |
| 614 |
} |
| 615 |
png_read_finish_row(png_ptr); |
| 616 |
|
| 617 |
if (png_ptr->read_row_fn != NULL) |
| 618 |
(*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); |
| 619 |
|
| 620 |
} |
| 621 |
#endif |
| 622 |
|
| 623 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
void PNGAPI |
| 649 |
png_read_rows(png_structrp png_ptr, png_bytepp row, |
| 650 |
png_bytepp display_row, png_uint_32 num_rows) |
| 651 |
{ |
| 652 |
png_uint_32 i; |
| 653 |
png_bytepp rp; |
| 654 |
png_bytepp dp; |
| 655 |
|
| 656 |
png_debug(1, "in png_read_rows"); |
| 657 |
|
| 658 |
if (png_ptr == NULL) |
| 659 |
return; |
| 660 |
|
| 661 |
rp = row; |
| 662 |
dp = display_row; |
| 663 |
if (rp != NULL && dp != NULL) |
| 664 |
for (i = 0; i < num_rows; i++) |
| 665 |
{ |
| 666 |
png_bytep rptr = *rp++; |
| 667 |
png_bytep dptr = *dp++; |
| 668 |
|
| 669 |
png_read_row(png_ptr, rptr, dptr); |
| 670 |
} |
| 671 |
|
| 672 |
else if (rp != NULL) |
| 673 |
for (i = 0; i < num_rows; i++) |
| 674 |
{ |
| 675 |
png_bytep rptr = *rp; |
| 676 |
png_read_row(png_ptr, rptr, NULL); |
| 677 |
rp++; |
| 678 |
} |
| 679 |
|
| 680 |
else if (dp != NULL) |
| 681 |
for (i = 0; i < num_rows; i++) |
| 682 |
{ |
| 683 |
png_bytep dptr = *dp; |
| 684 |
png_read_row(png_ptr, NULL, dptr); |
| 685 |
dp++; |
| 686 |
} |
| 687 |
} |
| 688 |
#endif |
| 689 |
|
| 690 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
void PNGAPI |
| 704 |
png_read_image(png_structrp png_ptr, png_bytepp image) |
| 705 |
{ |
| 706 |
png_uint_32 i, image_height; |
| 707 |
int pass, j; |
| 708 |
png_bytepp rp; |
| 709 |
|
| 710 |
png_debug(1, "in png_read_image"); |
| 711 |
|
| 712 |
if (png_ptr == NULL) |
| 713 |
return; |
| 714 |
|
| 715 |
#ifdef PNG_READ_INTERLACING_SUPPORTED |
| 716 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 717 |
{ |
| 718 |
pass = png_set_interlace_handling(png_ptr); |
| 719 |
|
| 720 |
png_start_read_image(png_ptr); |
| 721 |
} |
| 722 |
else |
| 723 |
{ |
| 724 |
if (png_ptr->interlaced != 0 && |
| 725 |
(png_ptr->transformations & PNG_INTERLACE) == 0) |
| 726 |
{ |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
png_warning(png_ptr, "Interlace handling should be turned on when " |
| 732 |
"using png_read_image"); |
| 733 |
|
| 734 |
png_ptr->num_rows = png_ptr->height; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
pass = png_set_interlace_handling(png_ptr); |
| 741 |
} |
| 742 |
#else |
| 743 |
if (png_ptr->interlaced) |
| 744 |
png_error(png_ptr, |
| 745 |
"Cannot read interlaced image -- interlace handler disabled"); |
| 746 |
|
| 747 |
pass = 1; |
| 748 |
#endif |
| 749 |
|
| 750 |
image_height=png_ptr->height; |
| 751 |
|
| 752 |
for (j = 0; j < pass; j++) |
| 753 |
{ |
| 754 |
rp = image; |
| 755 |
for (i = 0; i < image_height; i++) |
| 756 |
{ |
| 757 |
png_read_row(png_ptr, *rp, NULL); |
| 758 |
rp++; |
| 759 |
} |
| 760 |
} |
| 761 |
} |
| 762 |
#endif |
| 763 |
|
| 764 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
void PNGAPI |
| 770 |
png_read_end(png_structrp png_ptr, png_inforp info_ptr) |
| 771 |
{ |
| 772 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 773 |
int keep; |
| 774 |
#endif |
| 775 |
|
| 776 |
png_debug(1, "in png_read_end"); |
| 777 |
|
| 778 |
if (png_ptr == NULL) |
| 779 |
return; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 785 |
if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0) |
| 786 |
#endif |
| 787 |
png_read_finish_IDAT(png_ptr); |
| 788 |
|
| 789 |
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 790 |
|
| 791 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && |
| 792 |
png_ptr->num_palette_max >= png_ptr->num_palette) |
| 793 |
png_benign_error(png_ptr, "Read palette index exceeding num_palette"); |
| 794 |
#endif |
| 795 |
|
| 796 |
do |
| 797 |
{ |
| 798 |
png_uint_32 length = png_read_chunk_header(png_ptr); |
| 799 |
png_uint_32 chunk_name = png_ptr->chunk_name; |
| 800 |
|
| 801 |
if (chunk_name != png_IDAT) |
| 802 |
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; |
| 803 |
|
| 804 |
if (chunk_name == png_IEND) |
| 805 |
png_handle_IEND(png_ptr, info_ptr, length); |
| 806 |
|
| 807 |
else if (chunk_name == png_IHDR) |
| 808 |
png_handle_IHDR(png_ptr, info_ptr, length); |
| 809 |
|
| 810 |
else if (info_ptr == NULL) |
| 811 |
png_crc_finish(png_ptr, length); |
| 812 |
|
| 813 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 814 |
else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) |
| 815 |
{ |
| 816 |
if (chunk_name == png_IDAT) |
| 817 |
{ |
| 818 |
if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 819 |
|| (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
| 820 |
png_benign_error(png_ptr, ".Too many IDATs found"); |
| 821 |
} |
| 822 |
png_handle_unknown(png_ptr, info_ptr, length, keep); |
| 823 |
if (chunk_name == png_PLTE) |
| 824 |
png_ptr->mode |= PNG_HAVE_PLTE; |
| 825 |
} |
| 826 |
#endif |
| 827 |
|
| 828 |
else if (chunk_name == png_IDAT) |
| 829 |
{ |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) |
| 837 |
|| (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) |
| 838 |
png_benign_error(png_ptr, "..Too many IDATs found"); |
| 839 |
|
| 840 |
png_crc_finish(png_ptr, length); |
| 841 |
} |
| 842 |
else if (chunk_name == png_PLTE) |
| 843 |
png_handle_PLTE(png_ptr, info_ptr, length); |
| 844 |
|
| 845 |
#ifdef PNG_READ_bKGD_SUPPORTED |
| 846 |
else if (chunk_name == png_bKGD) |
| 847 |
png_handle_bKGD(png_ptr, info_ptr, length); |
| 848 |
#endif |
| 849 |
|
| 850 |
#ifdef PNG_READ_cHRM_SUPPORTED |
| 851 |
else if (chunk_name == png_cHRM) |
| 852 |
png_handle_cHRM(png_ptr, info_ptr, length); |
| 853 |
#endif |
| 854 |
|
| 855 |
#ifdef PNG_READ_eXIf_SUPPORTED |
| 856 |
else if (chunk_name == png_eXIf) |
| 857 |
png_handle_eXIf(png_ptr, info_ptr, length); |
| 858 |
#endif |
| 859 |
|
| 860 |
#ifdef PNG_READ_gAMA_SUPPORTED |
| 861 |
else if (chunk_name == png_gAMA) |
| 862 |
png_handle_gAMA(png_ptr, info_ptr, length); |
| 863 |
#endif |
| 864 |
|
| 865 |
#ifdef PNG_READ_hIST_SUPPORTED |
| 866 |
else if (chunk_name == png_hIST) |
| 867 |
png_handle_hIST(png_ptr, info_ptr, length); |
| 868 |
#endif |
| 869 |
|
| 870 |
#ifdef PNG_READ_oFFs_SUPPORTED |
| 871 |
else if (chunk_name == png_oFFs) |
| 872 |
png_handle_oFFs(png_ptr, info_ptr, length); |
| 873 |
#endif |
| 874 |
|
| 875 |
#ifdef PNG_READ_pCAL_SUPPORTED |
| 876 |
else if (chunk_name == png_pCAL) |
| 877 |
png_handle_pCAL(png_ptr, info_ptr, length); |
| 878 |
#endif |
| 879 |
|
| 880 |
#ifdef PNG_READ_sCAL_SUPPORTED |
| 881 |
else if (chunk_name == png_sCAL) |
| 882 |
png_handle_sCAL(png_ptr, info_ptr, length); |
| 883 |
#endif |
| 884 |
|
| 885 |
#ifdef PNG_READ_pHYs_SUPPORTED |
| 886 |
else if (chunk_name == png_pHYs) |
| 887 |
png_handle_pHYs(png_ptr, info_ptr, length); |
| 888 |
#endif |
| 889 |
|
| 890 |
#ifdef PNG_READ_sBIT_SUPPORTED |
| 891 |
else if (chunk_name == png_sBIT) |
| 892 |
png_handle_sBIT(png_ptr, info_ptr, length); |
| 893 |
#endif |
| 894 |
|
| 895 |
#ifdef PNG_READ_sRGB_SUPPORTED |
| 896 |
else if (chunk_name == png_sRGB) |
| 897 |
png_handle_sRGB(png_ptr, info_ptr, length); |
| 898 |
#endif |
| 899 |
|
| 900 |
#ifdef PNG_READ_iCCP_SUPPORTED |
| 901 |
else if (chunk_name == png_iCCP) |
| 902 |
png_handle_iCCP(png_ptr, info_ptr, length); |
| 903 |
#endif |
| 904 |
|
| 905 |
#ifdef PNG_READ_sPLT_SUPPORTED |
| 906 |
else if (chunk_name == png_sPLT) |
| 907 |
png_handle_sPLT(png_ptr, info_ptr, length); |
| 908 |
#endif |
| 909 |
|
| 910 |
#ifdef PNG_READ_tEXt_SUPPORTED |
| 911 |
else if (chunk_name == png_tEXt) |
| 912 |
png_handle_tEXt(png_ptr, info_ptr, length); |
| 913 |
#endif |
| 914 |
|
| 915 |
#ifdef PNG_READ_tIME_SUPPORTED |
| 916 |
else if (chunk_name == png_tIME) |
| 917 |
png_handle_tIME(png_ptr, info_ptr, length); |
| 918 |
#endif |
| 919 |
|
| 920 |
#ifdef PNG_READ_tRNS_SUPPORTED |
| 921 |
else if (chunk_name == png_tRNS) |
| 922 |
png_handle_tRNS(png_ptr, info_ptr, length); |
| 923 |
#endif |
| 924 |
|
| 925 |
#ifdef PNG_READ_zTXt_SUPPORTED |
| 926 |
else if (chunk_name == png_zTXt) |
| 927 |
png_handle_zTXt(png_ptr, info_ptr, length); |
| 928 |
#endif |
| 929 |
|
| 930 |
#ifdef PNG_READ_iTXt_SUPPORTED |
| 931 |
else if (chunk_name == png_iTXt) |
| 932 |
png_handle_iTXt(png_ptr, info_ptr, length); |
| 933 |
#endif |
| 934 |
|
| 935 |
else |
| 936 |
png_handle_unknown(png_ptr, info_ptr, length, |
| 937 |
PNG_HANDLE_CHUNK_AS_DEFAULT); |
| 938 |
} while ((png_ptr->mode & PNG_HAVE_IEND) == 0); |
| 939 |
} |
| 940 |
#endif |
| 941 |
|
| 942 |
|
| 943 |
static void |
| 944 |
png_read_destroy(png_structrp png_ptr) |
| 945 |
{ |
| 946 |
png_debug(1, "in png_read_destroy"); |
| 947 |
|
| 948 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 949 |
png_destroy_gamma_table(png_ptr); |
| 950 |
#endif |
| 951 |
|
| 952 |
png_free(png_ptr, png_ptr->big_row_buf); |
| 953 |
png_ptr->big_row_buf = NULL; |
| 954 |
png_free(png_ptr, png_ptr->big_prev_row); |
| 955 |
png_ptr->big_prev_row = NULL; |
| 956 |
png_free(png_ptr, png_ptr->read_buffer); |
| 957 |
png_ptr->read_buffer = NULL; |
| 958 |
|
| 959 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 960 |
png_free(png_ptr, png_ptr->palette_lookup); |
| 961 |
png_ptr->palette_lookup = NULL; |
| 962 |
png_free(png_ptr, png_ptr->quantize_index); |
| 963 |
png_ptr->quantize_index = NULL; |
| 964 |
#endif |
| 965 |
|
| 966 |
if ((png_ptr->free_me & PNG_FREE_PLTE) != 0) |
| 967 |
{ |
| 968 |
png_zfree(png_ptr, png_ptr->palette); |
| 969 |
png_ptr->palette = NULL; |
| 970 |
} |
| 971 |
png_ptr->free_me &= ~PNG_FREE_PLTE; |
| 972 |
|
| 973 |
#if defined(PNG_tRNS_SUPPORTED) || \ |
| 974 |
defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 975 |
if ((png_ptr->free_me & PNG_FREE_TRNS) != 0) |
| 976 |
{ |
| 977 |
png_free(png_ptr, png_ptr->trans_alpha); |
| 978 |
png_ptr->trans_alpha = NULL; |
| 979 |
} |
| 980 |
png_ptr->free_me &= ~PNG_FREE_TRNS; |
| 981 |
#endif |
| 982 |
|
| 983 |
inflateEnd(&png_ptr->zstream); |
| 984 |
|
| 985 |
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED |
| 986 |
png_free(png_ptr, png_ptr->save_buffer); |
| 987 |
png_ptr->save_buffer = NULL; |
| 988 |
#endif |
| 989 |
|
| 990 |
#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \ |
| 991 |
defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) |
| 992 |
png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 993 |
png_ptr->unknown_chunk.data = NULL; |
| 994 |
#endif |
| 995 |
|
| 996 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED |
| 997 |
png_free(png_ptr, png_ptr->chunk_list); |
| 998 |
png_ptr->chunk_list = NULL; |
| 999 |
#endif |
| 1000 |
|
| 1001 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && \ |
| 1002 |
defined(PNG_ARM_NEON_IMPLEMENTATION) |
| 1003 |
png_free(png_ptr, png_ptr->riffled_palette); |
| 1004 |
png_ptr->riffled_palette = NULL; |
| 1005 |
#endif |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
} |
| 1012 |
|
| 1013 |
|
| 1014 |
void PNGAPI |
| 1015 |
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, |
| 1016 |
png_infopp end_info_ptr_ptr) |
| 1017 |
{ |
| 1018 |
png_structrp png_ptr = NULL; |
| 1019 |
|
| 1020 |
png_debug(1, "in png_destroy_read_struct"); |
| 1021 |
|
| 1022 |
if (png_ptr_ptr != NULL) |
| 1023 |
png_ptr = *png_ptr_ptr; |
| 1024 |
|
| 1025 |
if (png_ptr == NULL) |
| 1026 |
return; |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
png_destroy_info_struct(png_ptr, end_info_ptr_ptr); |
| 1033 |
png_destroy_info_struct(png_ptr, info_ptr_ptr); |
| 1034 |
|
| 1035 |
*png_ptr_ptr = NULL; |
| 1036 |
png_read_destroy(png_ptr); |
| 1037 |
png_destroy_png_struct(png_ptr); |
| 1038 |
} |
| 1039 |
|
| 1040 |
void PNGAPI |
| 1041 |
png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) |
| 1042 |
{ |
| 1043 |
if (png_ptr == NULL) |
| 1044 |
return; |
| 1045 |
|
| 1046 |
png_ptr->read_row_fn = read_row_fn; |
| 1047 |
} |
| 1048 |
|
| 1049 |
|
| 1050 |
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED |
| 1051 |
#ifdef PNG_INFO_IMAGE_SUPPORTED |
| 1052 |
void PNGAPI |
| 1053 |
png_read_png(png_structrp png_ptr, png_inforp info_ptr, |
| 1054 |
int transforms, voidp params) |
| 1055 |
{ |
| 1056 |
png_debug(1, "in png_read_png"); |
| 1057 |
|
| 1058 |
if (png_ptr == NULL || info_ptr == NULL) |
| 1059 |
return; |
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
png_read_info(png_ptr, info_ptr); |
| 1065 |
if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) |
| 1066 |
png_error(png_ptr, "Image is too high to process with png_read_png()"); |
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
if ((transforms & PNG_TRANSFORM_SCALE_16) != 0) |
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 1082 |
png_set_scale_16(png_ptr); |
| 1083 |
#else |
| 1084 |
png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); |
| 1085 |
#endif |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
if ((transforms & PNG_TRANSFORM_STRIP_16) != 0) |
| 1092 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 1093 |
png_set_strip_16(png_ptr); |
| 1094 |
#else |
| 1095 |
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); |
| 1096 |
#endif |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0) |
| 1102 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 1103 |
png_set_strip_alpha(png_ptr); |
| 1104 |
#else |
| 1105 |
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); |
| 1106 |
#endif |
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
if ((transforms & PNG_TRANSFORM_PACKING) != 0) |
| 1112 |
#ifdef PNG_READ_PACK_SUPPORTED |
| 1113 |
png_set_packing(png_ptr); |
| 1114 |
#else |
| 1115 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); |
| 1116 |
#endif |
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) |
| 1122 |
#ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 1123 |
png_set_packswap(png_ptr); |
| 1124 |
#else |
| 1125 |
png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); |
| 1126 |
#endif |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
if ((transforms & PNG_TRANSFORM_EXPAND) != 0) |
| 1134 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 1135 |
png_set_expand(png_ptr); |
| 1136 |
#else |
| 1137 |
png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); |
| 1138 |
#endif |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) |
| 1146 |
#ifdef PNG_READ_INVERT_SUPPORTED |
| 1147 |
png_set_invert_mono(png_ptr); |
| 1148 |
#else |
| 1149 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); |
| 1150 |
#endif |
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
if ((transforms & PNG_TRANSFORM_SHIFT) != 0) |
| 1157 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
| 1158 |
if ((info_ptr->valid & PNG_INFO_sBIT) != 0) |
| 1159 |
png_set_shift(png_ptr, &info_ptr->sig_bit); |
| 1160 |
#else |
| 1161 |
png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); |
| 1162 |
#endif |
| 1163 |
|
| 1164 |
|
| 1165 |
if ((transforms & PNG_TRANSFORM_BGR) != 0) |
| 1166 |
#ifdef PNG_READ_BGR_SUPPORTED |
| 1167 |
png_set_bgr(png_ptr); |
| 1168 |
#else |
| 1169 |
png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); |
| 1170 |
#endif |
| 1171 |
|
| 1172 |
|
| 1173 |
if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) |
| 1174 |
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
| 1175 |
png_set_swap_alpha(png_ptr); |
| 1176 |
#else |
| 1177 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); |
| 1178 |
#endif |
| 1179 |
|
| 1180 |
|
| 1181 |
if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) |
| 1182 |
#ifdef PNG_READ_SWAP_SUPPORTED |
| 1183 |
png_set_swap(png_ptr); |
| 1184 |
#else |
| 1185 |
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); |
| 1186 |
#endif |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) |
| 1191 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
| 1192 |
png_set_invert_alpha(png_ptr); |
| 1193 |
#else |
| 1194 |
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); |
| 1195 |
#endif |
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0) |
| 1200 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 1201 |
png_set_gray_to_rgb(png_ptr); |
| 1202 |
#else |
| 1203 |
png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); |
| 1204 |
#endif |
| 1205 |
|
| 1206 |
|
| 1207 |
if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0) |
| 1208 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 1209 |
png_set_expand_16(png_ptr); |
| 1210 |
#else |
| 1211 |
png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); |
| 1212 |
#endif |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
(void)png_set_interlace_handling(png_ptr); |
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
png_read_update_info(png_ptr, info_ptr); |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); |
| 1230 |
if (info_ptr->row_pointers == NULL) |
| 1231 |
{ |
| 1232 |
png_uint_32 iptr; |
| 1233 |
|
| 1234 |
info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr, |
| 1235 |
info_ptr->height * (sizeof (png_bytep)))); |
| 1236 |
|
| 1237 |
for (iptr=0; iptr<info_ptr->height; iptr++) |
| 1238 |
info_ptr->row_pointers[iptr] = NULL; |
| 1239 |
|
| 1240 |
info_ptr->free_me |= PNG_FREE_ROWS; |
| 1241 |
|
| 1242 |
for (iptr = 0; iptr < info_ptr->height; iptr++) |
| 1243 |
info_ptr->row_pointers[iptr] = png_voidcast(png_bytep, |
| 1244 |
png_malloc(png_ptr, info_ptr->rowbytes)); |
| 1245 |
} |
| 1246 |
|
| 1247 |
png_read_image(png_ptr, info_ptr->row_pointers); |
| 1248 |
info_ptr->valid |= PNG_INFO_IDAT; |
| 1249 |
|
| 1250 |
|
| 1251 |
png_read_end(png_ptr, info_ptr); |
| 1252 |
|
| 1253 |
PNG_UNUSED(params) |
| 1254 |
} |
| 1255 |
#endif |
| 1256 |
#endif |
| 1257 |
|
| 1258 |
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
# define P_NOTSET 0 |
| 1268 |
# define P_sRGB 1 |
| 1269 |
# define P_LINEAR 2 |
| 1270 |
# define P_FILE 3 |
| 1271 |
# define P_LINEAR8 4 |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
#define PNG_CMAP_NONE 0 |
| 1277 |
#define PNG_CMAP_GA 1 |
| 1278 |
#define PNG_CMAP_TRANS 2 |
| 1279 |
#define PNG_CMAP_RGB 3 |
| 1280 |
#define PNG_CMAP_RGB_ALPHA 4 |
| 1281 |
|
| 1282 |
|
| 1283 |
#define PNG_CMAP_NONE_BACKGROUND 256 |
| 1284 |
#define PNG_CMAP_GA_BACKGROUND 231 |
| 1285 |
#define PNG_CMAP_TRANS_BACKGROUND 254 |
| 1286 |
#define PNG_CMAP_RGB_BACKGROUND 256 |
| 1287 |
#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 |
| 1288 |
|
| 1289 |
typedef struct |
| 1290 |
{ |
| 1291 |
|
| 1292 |
png_imagep image; |
| 1293 |
png_voidp buffer; |
| 1294 |
png_int_32 row_stride; |
| 1295 |
png_voidp colormap; |
| 1296 |
png_const_colorp background; |
| 1297 |
|
| 1298 |
png_voidp local_row; |
| 1299 |
png_voidp first_row; |
| 1300 |
ptrdiff_t row_bytes; |
| 1301 |
int file_encoding; |
| 1302 |
png_fixed_point gamma_to_linear; |
| 1303 |
int colormap_processing; |
| 1304 |
} png_image_read_control; |
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
static int |
| 1312 |
png_image_read_init(png_imagep image) |
| 1313 |
{ |
| 1314 |
if (image->opaque == NULL) |
| 1315 |
{ |
| 1316 |
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, |
| 1317 |
png_safe_error, png_safe_warning); |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
memset(image, 0, (sizeof *image)); |
| 1323 |
image->version = PNG_IMAGE_VERSION; |
| 1324 |
|
| 1325 |
if (png_ptr != NULL) |
| 1326 |
{ |
| 1327 |
png_infop info_ptr = png_create_info_struct(png_ptr); |
| 1328 |
|
| 1329 |
if (info_ptr != NULL) |
| 1330 |
{ |
| 1331 |
png_controlp control = png_voidcast(png_controlp, |
| 1332 |
png_malloc_warn(png_ptr, (sizeof *control))); |
| 1333 |
|
| 1334 |
if (control != NULL) |
| 1335 |
{ |
| 1336 |
memset(control, 0, (sizeof *control)); |
| 1337 |
|
| 1338 |
control->png_ptr = png_ptr; |
| 1339 |
control->info_ptr = info_ptr; |
| 1340 |
control->for_write = 0; |
| 1341 |
|
| 1342 |
image->opaque = control; |
| 1343 |
return 1; |
| 1344 |
} |
| 1345 |
|
| 1346 |
|
| 1347 |
png_destroy_info_struct(png_ptr, &info_ptr); |
| 1348 |
} |
| 1349 |
|
| 1350 |
png_destroy_read_struct(&png_ptr, NULL, NULL); |
| 1351 |
} |
| 1352 |
|
| 1353 |
return png_image_error(image, "png_image_read: out of memory"); |
| 1354 |
} |
| 1355 |
|
| 1356 |
return png_image_error(image, "png_image_read: opaque pointer not NULL"); |
| 1357 |
} |
| 1358 |
|
| 1359 |
|
| 1360 |
static png_uint_32 |
| 1361 |
png_image_format(png_structrp png_ptr) |
| 1362 |
{ |
| 1363 |
png_uint_32 format = 0; |
| 1364 |
|
| 1365 |
if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 1366 |
format |= PNG_FORMAT_FLAG_COLOR; |
| 1367 |
|
| 1368 |
if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 1369 |
format |= PNG_FORMAT_FLAG_ALPHA; |
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
else if (png_ptr->num_trans > 0) |
| 1377 |
format |= PNG_FORMAT_FLAG_ALPHA; |
| 1378 |
|
| 1379 |
if (png_ptr->bit_depth == 16) |
| 1380 |
format |= PNG_FORMAT_FLAG_LINEAR; |
| 1381 |
|
| 1382 |
if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0) |
| 1383 |
format |= PNG_FORMAT_FLAG_COLORMAP; |
| 1384 |
|
| 1385 |
return format; |
| 1386 |
} |
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
|
| 1393 |
static int |
| 1394 |
png_gamma_not_sRGB(png_fixed_point g) |
| 1395 |
{ |
| 1396 |
if (g < PNG_FP_1) |
| 1397 |
{ |
| 1398 |
|
| 1399 |
if (g == 0) |
| 1400 |
return 0; |
| 1401 |
|
| 1402 |
return png_gamma_significant((g * 11 + 2)/5 ); |
| 1403 |
} |
| 1404 |
|
| 1405 |
return 1; |
| 1406 |
} |
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
static int |
| 1413 |
png_image_read_header(png_voidp argument) |
| 1414 |
{ |
| 1415 |
png_imagep image = png_voidcast(png_imagep, argument); |
| 1416 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 1417 |
png_inforp info_ptr = image->opaque->info_ptr; |
| 1418 |
|
| 1419 |
#ifdef PNG_BENIGN_ERRORS_SUPPORTED |
| 1420 |
png_set_benign_errors(png_ptr, 1); |
| 1421 |
#endif |
| 1422 |
png_read_info(png_ptr, info_ptr); |
| 1423 |
|
| 1424 |
|
| 1425 |
image->width = png_ptr->width; |
| 1426 |
image->height = png_ptr->height; |
| 1427 |
|
| 1428 |
{ |
| 1429 |
png_uint_32 format = png_image_format(png_ptr); |
| 1430 |
|
| 1431 |
image->format = format; |
| 1432 |
|
| 1433 |
#ifdef PNG_COLORSPACE_SUPPORTED |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags |
| 1440 |
& (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| |
| 1441 |
PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) |
| 1442 |
image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; |
| 1443 |
#endif |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
{ |
| 1450 |
png_uint_32 cmap_entries; |
| 1451 |
|
| 1452 |
switch (png_ptr->color_type) |
| 1453 |
{ |
| 1454 |
case PNG_COLOR_TYPE_GRAY: |
| 1455 |
cmap_entries = 1U << png_ptr->bit_depth; |
| 1456 |
break; |
| 1457 |
|
| 1458 |
case PNG_COLOR_TYPE_PALETTE: |
| 1459 |
cmap_entries = (png_uint_32)png_ptr->num_palette; |
| 1460 |
break; |
| 1461 |
|
| 1462 |
default: |
| 1463 |
cmap_entries = 256; |
| 1464 |
break; |
| 1465 |
} |
| 1466 |
|
| 1467 |
if (cmap_entries > 256) |
| 1468 |
cmap_entries = 256; |
| 1469 |
|
| 1470 |
image->colormap_entries = cmap_entries; |
| 1471 |
} |
| 1472 |
|
| 1473 |
return 1; |
| 1474 |
} |
| 1475 |
|
| 1476 |
#ifdef PNG_STDIO_SUPPORTED |
| 1477 |
int PNGAPI |
| 1478 |
png_image_begin_read_from_stdio(png_imagep image, FILE* file) |
| 1479 |
{ |
| 1480 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1481 |
{ |
| 1482 |
if (file != NULL) |
| 1483 |
{ |
| 1484 |
if (png_image_read_init(image) != 0) |
| 1485 |
{ |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
image->opaque->png_ptr->io_ptr = file; |
| 1491 |
return png_safe_execute(image, png_image_read_header, image); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
|
| 1495 |
else |
| 1496 |
return png_image_error(image, |
| 1497 |
"png_image_begin_read_from_stdio: invalid argument"); |
| 1498 |
} |
| 1499 |
|
| 1500 |
else if (image != NULL) |
| 1501 |
return png_image_error(image, |
| 1502 |
"png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); |
| 1503 |
|
| 1504 |
return 0; |
| 1505 |
} |
| 1506 |
|
| 1507 |
int PNGAPI |
| 1508 |
png_image_begin_read_from_file(png_imagep image, const char *file_name) |
| 1509 |
{ |
| 1510 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1511 |
{ |
| 1512 |
if (file_name != NULL) |
| 1513 |
{ |
| 1514 |
FILE *fp = fopen(file_name, "rb"); |
| 1515 |
|
| 1516 |
if (fp != NULL) |
| 1517 |
{ |
| 1518 |
if (png_image_read_init(image) != 0) |
| 1519 |
{ |
| 1520 |
image->opaque->png_ptr->io_ptr = fp; |
| 1521 |
image->opaque->owned_file = 1; |
| 1522 |
return png_safe_execute(image, png_image_read_header, image); |
| 1523 |
} |
| 1524 |
|
| 1525 |
|
| 1526 |
(void)fclose(fp); |
| 1527 |
} |
| 1528 |
|
| 1529 |
else |
| 1530 |
return png_image_error(image, strerror(errno)); |
| 1531 |
} |
| 1532 |
|
| 1533 |
else |
| 1534 |
return png_image_error(image, |
| 1535 |
"png_image_begin_read_from_file: invalid argument"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
else if (image != NULL) |
| 1539 |
return png_image_error(image, |
| 1540 |
"png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); |
| 1541 |
|
| 1542 |
return 0; |
| 1543 |
} |
| 1544 |
#endif |
| 1545 |
|
| 1546 |
static void PNGCBAPI |
| 1547 |
png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need) |
| 1548 |
{ |
| 1549 |
if (png_ptr != NULL) |
| 1550 |
{ |
| 1551 |
png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); |
| 1552 |
if (image != NULL) |
| 1553 |
{ |
| 1554 |
png_controlp cp = image->opaque; |
| 1555 |
if (cp != NULL) |
| 1556 |
{ |
| 1557 |
png_const_bytep memory = cp->memory; |
| 1558 |
size_t size = cp->size; |
| 1559 |
|
| 1560 |
if (memory != NULL && size >= need) |
| 1561 |
{ |
| 1562 |
memcpy(out, memory, need); |
| 1563 |
cp->memory = memory + need; |
| 1564 |
cp->size = size - need; |
| 1565 |
return; |
| 1566 |
} |
| 1567 |
|
| 1568 |
png_error(png_ptr, "read beyond end of data"); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
|
| 1572 |
png_error(png_ptr, "invalid memory read"); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
int PNGAPI png_image_begin_read_from_memory(png_imagep image, |
| 1577 |
png_const_voidp memory, size_t size) |
| 1578 |
{ |
| 1579 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 1580 |
{ |
| 1581 |
if (memory != NULL && size > 0) |
| 1582 |
{ |
| 1583 |
if (png_image_read_init(image) != 0) |
| 1584 |
{ |
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
image->opaque->memory = png_voidcast(png_const_bytep, memory); |
| 1590 |
image->opaque->size = size; |
| 1591 |
image->opaque->png_ptr->io_ptr = image; |
| 1592 |
image->opaque->png_ptr->read_data_fn = png_image_memory_read; |
| 1593 |
|
| 1594 |
return png_safe_execute(image, png_image_read_header, image); |
| 1595 |
} |
| 1596 |
} |
| 1597 |
|
| 1598 |
else |
| 1599 |
return png_image_error(image, |
| 1600 |
"png_image_begin_read_from_memory: invalid argument"); |
| 1601 |
} |
| 1602 |
|
| 1603 |
else if (image != NULL) |
| 1604 |
return png_image_error(image, |
| 1605 |
"png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); |
| 1606 |
|
| 1607 |
return 0; |
| 1608 |
} |
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
| 1614 |
static void |
| 1615 |
png_image_skip_unused_chunks(png_structrp png_ptr) |
| 1616 |
{ |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
|
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
{ |
| 1636 |
static const png_byte chunks_to_process[] = { |
| 1637 |
98, 75, 71, 68, '\0', |
| 1638 |
99, 72, 82, 77, '\0', |
| 1639 |
103, 65, 77, 65, '\0', |
| 1640 |
# ifdef PNG_READ_iCCP_SUPPORTED |
| 1641 |
105, 67, 67, 80, '\0', |
| 1642 |
# endif |
| 1643 |
115, 66, 73, 84, '\0', |
| 1644 |
115, 82, 71, 66, '\0', |
| 1645 |
}; |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, |
| 1651 |
NULL, -1); |
| 1652 |
|
| 1653 |
|
| 1654 |
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, |
| 1655 |
chunks_to_process, (int)(sizeof chunks_to_process)/5); |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) |
| 1660 |
#else |
| 1661 |
# define PNG_SKIP_CHUNKS(p) ((void)0) |
| 1662 |
#endif |
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) |
| 1669 |
|
| 1670 |
|
| 1671 |
static void |
| 1672 |
set_file_encoding(png_image_read_control *display) |
| 1673 |
{ |
| 1674 |
png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; |
| 1675 |
if (png_gamma_significant(g) != 0) |
| 1676 |
{ |
| 1677 |
if (png_gamma_not_sRGB(g) != 0) |
| 1678 |
{ |
| 1679 |
display->file_encoding = P_FILE; |
| 1680 |
display->gamma_to_linear = png_reciprocal(g); |
| 1681 |
} |
| 1682 |
|
| 1683 |
else |
| 1684 |
display->file_encoding = P_sRGB; |
| 1685 |
} |
| 1686 |
|
| 1687 |
else |
| 1688 |
display->file_encoding = P_LINEAR8; |
| 1689 |
} |
| 1690 |
|
| 1691 |
static unsigned int |
| 1692 |
decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) |
| 1693 |
{ |
| 1694 |
if (encoding == P_FILE) |
| 1695 |
encoding = display->file_encoding; |
| 1696 |
|
| 1697 |
if (encoding == P_NOTSET) |
| 1698 |
{ |
| 1699 |
set_file_encoding(display); |
| 1700 |
encoding = display->file_encoding; |
| 1701 |
} |
| 1702 |
|
| 1703 |
switch (encoding) |
| 1704 |
{ |
| 1705 |
case P_FILE: |
| 1706 |
value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); |
| 1707 |
break; |
| 1708 |
|
| 1709 |
case P_sRGB: |
| 1710 |
value = png_sRGB_table[value]; |
| 1711 |
break; |
| 1712 |
|
| 1713 |
case P_LINEAR: |
| 1714 |
break; |
| 1715 |
|
| 1716 |
case P_LINEAR8: |
| 1717 |
value *= 257; |
| 1718 |
break; |
| 1719 |
|
| 1720 |
#ifdef __GNUC__ |
| 1721 |
default: |
| 1722 |
png_error(display->image->opaque->png_ptr, |
| 1723 |
"unexpected encoding (internal error)"); |
| 1724 |
#endif |
| 1725 |
} |
| 1726 |
|
| 1727 |
return value; |
| 1728 |
} |
| 1729 |
|
| 1730 |
static png_uint_32 |
| 1731 |
png_colormap_compose(png_image_read_control *display, |
| 1732 |
png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, |
| 1733 |
png_uint_32 background, int encoding) |
| 1734 |
{ |
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); |
| 1741 |
png_uint_32 b = decode_gamma(display, background, encoding); |
| 1742 |
|
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
f = f * alpha + b * (255-alpha); |
| 1747 |
|
| 1748 |
if (encoding == P_LINEAR) |
| 1749 |
{ |
| 1750 |
|
| 1751 |
|
| 1752 |
|
| 1753 |
f *= 257; |
| 1754 |
f += f >> 16; |
| 1755 |
f = (f+32768) >> 16; |
| 1756 |
} |
| 1757 |
|
| 1758 |
else |
| 1759 |
f = PNG_sRGB_FROM_LINEAR(f); |
| 1760 |
|
| 1761 |
return f; |
| 1762 |
} |
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
static void |
| 1768 |
png_create_colormap_entry(png_image_read_control *display, |
| 1769 |
png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, |
| 1770 |
png_uint_32 alpha, int encoding) |
| 1771 |
{ |
| 1772 |
png_imagep image = display->image; |
| 1773 |
int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
| 1774 |
P_LINEAR : P_sRGB; |
| 1775 |
int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && |
| 1776 |
(red != green || green != blue); |
| 1777 |
|
| 1778 |
if (ip > 255) |
| 1779 |
png_error(image->opaque->png_ptr, "color-map index out of range"); |
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
if (encoding == P_FILE) |
| 1785 |
{ |
| 1786 |
if (display->file_encoding == P_NOTSET) |
| 1787 |
set_file_encoding(display); |
| 1788 |
|
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
encoding = display->file_encoding; |
| 1793 |
} |
| 1794 |
|
| 1795 |
if (encoding == P_FILE) |
| 1796 |
{ |
| 1797 |
png_fixed_point g = display->gamma_to_linear; |
| 1798 |
|
| 1799 |
red = png_gamma_16bit_correct(red*257, g); |
| 1800 |
green = png_gamma_16bit_correct(green*257, g); |
| 1801 |
blue = png_gamma_16bit_correct(blue*257, g); |
| 1802 |
|
| 1803 |
if (convert_to_Y != 0 || output_encoding == P_LINEAR) |
| 1804 |
{ |
| 1805 |
alpha *= 257; |
| 1806 |
encoding = P_LINEAR; |
| 1807 |
} |
| 1808 |
|
| 1809 |
else |
| 1810 |
{ |
| 1811 |
red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1812 |
green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1813 |
blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1814 |
encoding = P_sRGB; |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
else if (encoding == P_LINEAR8) |
| 1819 |
{ |
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
red *= 257; |
| 1824 |
green *= 257; |
| 1825 |
blue *= 257; |
| 1826 |
alpha *= 257; |
| 1827 |
encoding = P_LINEAR; |
| 1828 |
} |
| 1829 |
|
| 1830 |
else if (encoding == P_sRGB && |
| 1831 |
(convert_to_Y != 0 || output_encoding == P_LINEAR)) |
| 1832 |
{ |
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
red = png_sRGB_table[red]; |
| 1837 |
green = png_sRGB_table[green]; |
| 1838 |
blue = png_sRGB_table[blue]; |
| 1839 |
alpha *= 257; |
| 1840 |
encoding = P_LINEAR; |
| 1841 |
} |
| 1842 |
|
| 1843 |
|
| 1844 |
if (encoding == P_LINEAR) |
| 1845 |
{ |
| 1846 |
if (convert_to_Y != 0) |
| 1847 |
{ |
| 1848 |
|
| 1849 |
png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + |
| 1850 |
(png_uint_32)2366 * blue; |
| 1851 |
|
| 1852 |
if (output_encoding == P_LINEAR) |
| 1853 |
y = (y + 16384) >> 15; |
| 1854 |
|
| 1855 |
else |
| 1856 |
{ |
| 1857 |
|
| 1858 |
y = (y + 128) >> 8; |
| 1859 |
y *= 255; |
| 1860 |
y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); |
| 1861 |
alpha = PNG_DIV257(alpha); |
| 1862 |
encoding = P_sRGB; |
| 1863 |
} |
| 1864 |
|
| 1865 |
blue = red = green = y; |
| 1866 |
} |
| 1867 |
|
| 1868 |
else if (output_encoding == P_sRGB) |
| 1869 |
{ |
| 1870 |
red = PNG_sRGB_FROM_LINEAR(red * 255); |
| 1871 |
green = PNG_sRGB_FROM_LINEAR(green * 255); |
| 1872 |
blue = PNG_sRGB_FROM_LINEAR(blue * 255); |
| 1873 |
alpha = PNG_DIV257(alpha); |
| 1874 |
encoding = P_sRGB; |
| 1875 |
} |
| 1876 |
} |
| 1877 |
|
| 1878 |
if (encoding != output_encoding) |
| 1879 |
png_error(image->opaque->png_ptr, "bad encoding (internal error)"); |
| 1880 |
|
| 1881 |
|
| 1882 |
{ |
| 1883 |
# ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 1884 |
int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && |
| 1885 |
(image->format & PNG_FORMAT_FLAG_ALPHA) != 0; |
| 1886 |
# else |
| 1887 |
# define afirst 0 |
| 1888 |
# endif |
| 1889 |
# ifdef PNG_FORMAT_BGR_SUPPORTED |
| 1890 |
int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; |
| 1891 |
# else |
| 1892 |
# define bgr 0 |
| 1893 |
# endif |
| 1894 |
|
| 1895 |
if (output_encoding == P_LINEAR) |
| 1896 |
{ |
| 1897 |
png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); |
| 1898 |
|
| 1899 |
entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1900 |
|
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1906 |
{ |
| 1907 |
case 4: |
| 1908 |
entry[afirst ? 0 : 3] = (png_uint_16)alpha; |
| 1909 |
|
| 1910 |
|
| 1911 |
case 3: |
| 1912 |
if (alpha < 65535) |
| 1913 |
{ |
| 1914 |
if (alpha > 0) |
| 1915 |
{ |
| 1916 |
blue = (blue * alpha + 32767U)/65535U; |
| 1917 |
green = (green * alpha + 32767U)/65535U; |
| 1918 |
red = (red * alpha + 32767U)/65535U; |
| 1919 |
} |
| 1920 |
|
| 1921 |
else |
| 1922 |
red = green = blue = 0; |
| 1923 |
} |
| 1924 |
entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; |
| 1925 |
entry[afirst + 1] = (png_uint_16)green; |
| 1926 |
entry[afirst + bgr] = (png_uint_16)red; |
| 1927 |
break; |
| 1928 |
|
| 1929 |
case 2: |
| 1930 |
entry[1 ^ afirst] = (png_uint_16)alpha; |
| 1931 |
|
| 1932 |
|
| 1933 |
case 1: |
| 1934 |
if (alpha < 65535) |
| 1935 |
{ |
| 1936 |
if (alpha > 0) |
| 1937 |
green = (green * alpha + 32767U)/65535U; |
| 1938 |
|
| 1939 |
else |
| 1940 |
green = 0; |
| 1941 |
} |
| 1942 |
entry[afirst] = (png_uint_16)green; |
| 1943 |
break; |
| 1944 |
|
| 1945 |
default: |
| 1946 |
break; |
| 1947 |
} |
| 1948 |
} |
| 1949 |
|
| 1950 |
else |
| 1951 |
{ |
| 1952 |
png_bytep entry = png_voidcast(png_bytep, display->colormap); |
| 1953 |
|
| 1954 |
entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); |
| 1955 |
|
| 1956 |
switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) |
| 1957 |
{ |
| 1958 |
case 4: |
| 1959 |
entry[afirst ? 0 : 3] = (png_byte)alpha; |
| 1960 |
|
| 1961 |
case 3: |
| 1962 |
entry[afirst + (2 ^ bgr)] = (png_byte)blue; |
| 1963 |
entry[afirst + 1] = (png_byte)green; |
| 1964 |
entry[afirst + bgr] = (png_byte)red; |
| 1965 |
break; |
| 1966 |
|
| 1967 |
case 2: |
| 1968 |
entry[1 ^ afirst] = (png_byte)alpha; |
| 1969 |
|
| 1970 |
case 1: |
| 1971 |
entry[afirst] = (png_byte)green; |
| 1972 |
break; |
| 1973 |
|
| 1974 |
default: |
| 1975 |
break; |
| 1976 |
} |
| 1977 |
} |
| 1978 |
|
| 1979 |
# ifdef afirst |
| 1980 |
# undef afirst |
| 1981 |
# endif |
| 1982 |
# ifdef bgr |
| 1983 |
# undef bgr |
| 1984 |
# endif |
| 1985 |
} |
| 1986 |
} |
| 1987 |
|
| 1988 |
static int |
| 1989 |
make_gray_file_colormap(png_image_read_control *display) |
| 1990 |
{ |
| 1991 |
unsigned int i; |
| 1992 |
|
| 1993 |
for (i=0; i<256; ++i) |
| 1994 |
png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); |
| 1995 |
|
| 1996 |
return (int)i; |
| 1997 |
} |
| 1998 |
|
| 1999 |
static int |
| 2000 |
make_gray_colormap(png_image_read_control *display) |
| 2001 |
{ |
| 2002 |
unsigned int i; |
| 2003 |
|
| 2004 |
for (i=0; i<256; ++i) |
| 2005 |
png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); |
| 2006 |
|
| 2007 |
return (int)i; |
| 2008 |
} |
| 2009 |
#define PNG_GRAY_COLORMAP_ENTRIES 256 |
| 2010 |
|
| 2011 |
static int |
| 2012 |
make_ga_colormap(png_image_read_control *display) |
| 2013 |
{ |
| 2014 |
unsigned int i, a; |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
i = 0; |
| 2041 |
while (i < 231) |
| 2042 |
{ |
| 2043 |
unsigned int gray = (i * 256 + 115) / 231; |
| 2044 |
png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); |
| 2045 |
} |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); |
| 2051 |
|
| 2052 |
for (a=1; a<5; ++a) |
| 2053 |
{ |
| 2054 |
unsigned int g; |
| 2055 |
|
| 2056 |
for (g=0; g<6; ++g) |
| 2057 |
png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, |
| 2058 |
P_sRGB); |
| 2059 |
} |
| 2060 |
|
| 2061 |
return (int)i; |
| 2062 |
} |
| 2063 |
|
| 2064 |
#define PNG_GA_COLORMAP_ENTRIES 256 |
| 2065 |
|
| 2066 |
static int |
| 2067 |
make_rgb_colormap(png_image_read_control *display) |
| 2068 |
{ |
| 2069 |
unsigned int i, r; |
| 2070 |
|
| 2071 |
|
| 2072 |
for (i=r=0; r<6; ++r) |
| 2073 |
{ |
| 2074 |
unsigned int g; |
| 2075 |
|
| 2076 |
for (g=0; g<6; ++g) |
| 2077 |
{ |
| 2078 |
unsigned int b; |
| 2079 |
|
| 2080 |
for (b=0; b<6; ++b) |
| 2081 |
png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, |
| 2082 |
P_sRGB); |
| 2083 |
} |
| 2084 |
} |
| 2085 |
|
| 2086 |
return (int)i; |
| 2087 |
} |
| 2088 |
|
| 2089 |
#define PNG_RGB_COLORMAP_ENTRIES 216 |
| 2090 |
|
| 2091 |
|
| 2092 |
#define PNG_RGB_INDEX(r,g,b) \ |
| 2093 |
((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) |
| 2094 |
|
| 2095 |
static int |
| 2096 |
png_image_read_colormap(png_voidp argument) |
| 2097 |
{ |
| 2098 |
png_image_read_control *display = |
| 2099 |
png_voidcast(png_image_read_control*, argument); |
| 2100 |
png_imagep image = display->image; |
| 2101 |
|
| 2102 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 2103 |
png_uint_32 output_format = image->format; |
| 2104 |
int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ? |
| 2105 |
P_LINEAR : P_sRGB; |
| 2106 |
|
| 2107 |
unsigned int cmap_entries; |
| 2108 |
unsigned int output_processing; |
| 2109 |
unsigned int data_encoding = P_NOTSET; |
| 2110 |
|
| 2111 |
|
| 2112 |
|
| 2113 |
|
| 2114 |
unsigned int background_index = 256; |
| 2115 |
png_uint_32 back_r, back_g, back_b; |
| 2116 |
|
| 2117 |
|
| 2118 |
int expand_tRNS = 0; |
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || |
| 2126 |
png_ptr->num_trans > 0) && |
| 2127 |
((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) ) |
| 2128 |
{ |
| 2129 |
if (output_encoding == P_LINEAR) |
| 2130 |
back_b = back_g = back_r = 0; |
| 2131 |
|
| 2132 |
else if (display->background == NULL ) |
| 2133 |
png_error(png_ptr, |
| 2134 |
"background color must be supplied to remove alpha/transparency"); |
| 2135 |
|
| 2136 |
|
| 2137 |
|
| 2138 |
|
| 2139 |
|
| 2140 |
else |
| 2141 |
{ |
| 2142 |
back_g = display->background->green; |
| 2143 |
if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0) |
| 2144 |
{ |
| 2145 |
back_r = display->background->red; |
| 2146 |
back_b = display->background->blue; |
| 2147 |
} |
| 2148 |
else |
| 2149 |
back_b = back_r = back_g; |
| 2150 |
} |
| 2151 |
} |
| 2152 |
|
| 2153 |
else if (output_encoding == P_LINEAR) |
| 2154 |
back_b = back_r = back_g = 65535; |
| 2155 |
|
| 2156 |
else |
| 2157 |
back_b = back_r = back_g = 255; |
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
|
| 2164 |
if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0) |
| 2165 |
{ |
| 2166 |
|
| 2167 |
|
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 2171 |
if (png_ptr->bit_depth == 16 && |
| 2172 |
(image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
| 2173 |
png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR; |
| 2174 |
|
| 2175 |
else |
| 2176 |
png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE; |
| 2177 |
|
| 2178 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 2179 |
} |
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
switch (png_ptr->color_type) |
| 2187 |
{ |
| 2188 |
case PNG_COLOR_TYPE_GRAY: |
| 2189 |
if (png_ptr->bit_depth <= 8) |
| 2190 |
{ |
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
unsigned int step, i, val, trans = 256, back_alpha = 0; |
| 2195 |
|
| 2196 |
cmap_entries = 1U << png_ptr->bit_depth; |
| 2197 |
if (cmap_entries > image->colormap_entries) |
| 2198 |
png_error(png_ptr, "gray[8] color-map: too few entries"); |
| 2199 |
|
| 2200 |
step = 255 / (cmap_entries - 1); |
| 2201 |
output_processing = PNG_CMAP_NONE; |
| 2202 |
|
| 2203 |
|
| 2204 |
|
| 2205 |
|
| 2206 |
if (png_ptr->num_trans > 0) |
| 2207 |
{ |
| 2208 |
trans = png_ptr->trans_color.gray; |
| 2209 |
|
| 2210 |
if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) |
| 2211 |
back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
| 2212 |
} |
| 2213 |
|
| 2214 |
|
| 2215 |
|
| 2216 |
|
| 2217 |
|
| 2218 |
|
| 2219 |
|
| 2220 |
|
| 2221 |
for (i=val=0; i<cmap_entries; ++i, val += step) |
| 2222 |
{ |
| 2223 |
|
| 2224 |
|
| 2225 |
|
| 2226 |
|
| 2227 |
if (i != trans) |
| 2228 |
png_create_colormap_entry(display, i, val, val, val, 255, |
| 2229 |
P_FILE); |
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
else |
| 2240 |
png_create_colormap_entry(display, i, back_r, back_g, back_b, |
| 2241 |
back_alpha, output_encoding); |
| 2242 |
} |
| 2243 |
|
| 2244 |
|
| 2245 |
data_encoding = P_FILE; |
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
if (png_ptr->bit_depth < 8) |
| 2253 |
png_set_packing(png_ptr); |
| 2254 |
} |
| 2255 |
|
| 2256 |
else |
| 2257 |
{ |
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
|
| 2267 |
|
| 2268 |
|
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
data_encoding = P_sRGB; |
| 2275 |
|
| 2276 |
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2277 |
png_error(png_ptr, "gray[16] color-map: too few entries"); |
| 2278 |
|
| 2279 |
cmap_entries = (unsigned int)make_gray_colormap(display); |
| 2280 |
|
| 2281 |
if (png_ptr->num_trans > 0) |
| 2282 |
{ |
| 2283 |
unsigned int back_alpha; |
| 2284 |
|
| 2285 |
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2286 |
back_alpha = 0; |
| 2287 |
|
| 2288 |
else |
| 2289 |
{ |
| 2290 |
if (back_r == back_g && back_g == back_b) |
| 2291 |
{ |
| 2292 |
|
| 2293 |
|
| 2294 |
|
| 2295 |
png_color_16 c; |
| 2296 |
png_uint_32 gray = back_g; |
| 2297 |
|
| 2298 |
if (output_encoding == P_LINEAR) |
| 2299 |
{ |
| 2300 |
gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2301 |
|
| 2302 |
|
| 2303 |
|
| 2304 |
|
| 2305 |
png_create_colormap_entry(display, gray, back_g, back_g, |
| 2306 |
back_g, 65535, P_LINEAR); |
| 2307 |
} |
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
c.index = 0; |
| 2313 |
c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
png_set_background_fixed(png_ptr, &c, |
| 2320 |
PNG_BACKGROUND_GAMMA_SCREEN, 0, |
| 2321 |
0); |
| 2322 |
|
| 2323 |
output_processing = PNG_CMAP_NONE; |
| 2324 |
break; |
| 2325 |
} |
| 2326 |
#ifdef __COVERITY__ |
| 2327 |
|
| 2328 |
|
| 2329 |
|
| 2330 |
back_alpha = 255; |
| 2331 |
#else |
| 2332 |
back_alpha = output_encoding == P_LINEAR ? 65535 : 255; |
| 2333 |
#endif |
| 2334 |
} |
| 2335 |
|
| 2336 |
|
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
expand_tRNS = 1; |
| 2344 |
output_processing = PNG_CMAP_TRANS; |
| 2345 |
background_index = 254; |
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
png_create_colormap_entry(display, 254, back_r, back_g, back_b, |
| 2351 |
back_alpha, output_encoding); |
| 2352 |
} |
| 2353 |
|
| 2354 |
else |
| 2355 |
output_processing = PNG_CMAP_NONE; |
| 2356 |
} |
| 2357 |
break; |
| 2358 |
|
| 2359 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
data_encoding = P_sRGB; |
| 2371 |
|
| 2372 |
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2373 |
{ |
| 2374 |
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2375 |
png_error(png_ptr, "gray+alpha color-map: too few entries"); |
| 2376 |
|
| 2377 |
cmap_entries = (unsigned int)make_ga_colormap(display); |
| 2378 |
|
| 2379 |
background_index = PNG_CMAP_GA_BACKGROUND; |
| 2380 |
output_processing = PNG_CMAP_GA; |
| 2381 |
} |
| 2382 |
|
| 2383 |
else |
| 2384 |
{ |
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || |
| 2402 |
(back_r == back_g && back_g == back_b)) |
| 2403 |
{ |
| 2404 |
|
| 2405 |
png_color_16 c; |
| 2406 |
png_uint_32 gray = back_g; |
| 2407 |
|
| 2408 |
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2409 |
png_error(png_ptr, "gray-alpha color-map: too few entries"); |
| 2410 |
|
| 2411 |
cmap_entries = (unsigned int)make_gray_colormap(display); |
| 2412 |
|
| 2413 |
if (output_encoding == P_LINEAR) |
| 2414 |
{ |
| 2415 |
gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2416 |
|
| 2417 |
|
| 2418 |
png_create_colormap_entry(display, gray, back_g, back_g, |
| 2419 |
back_g, 65535, P_LINEAR); |
| 2420 |
} |
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
c.index = 0; |
| 2426 |
c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2427 |
|
| 2428 |
png_set_background_fixed(png_ptr, &c, |
| 2429 |
PNG_BACKGROUND_GAMMA_SCREEN, 0, |
| 2430 |
0); |
| 2431 |
|
| 2432 |
output_processing = PNG_CMAP_NONE; |
| 2433 |
} |
| 2434 |
|
| 2435 |
else |
| 2436 |
{ |
| 2437 |
png_uint_32 i, a; |
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2443 |
png_error(png_ptr, "ga-alpha color-map: too few entries"); |
| 2444 |
|
| 2445 |
i = 0; |
| 2446 |
while (i < 231) |
| 2447 |
{ |
| 2448 |
png_uint_32 gray = (i * 256 + 115) / 231; |
| 2449 |
png_create_colormap_entry(display, i++, gray, gray, gray, |
| 2450 |
255, P_sRGB); |
| 2451 |
} |
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
background_index = i; |
| 2457 |
png_create_colormap_entry(display, i++, back_r, back_g, back_b, |
| 2458 |
#ifdef __COVERITY__ |
| 2459 |
|
| 2460 |
|
| 2461 |
255U, |
| 2462 |
#else |
| 2463 |
output_encoding == P_LINEAR ? 65535U : 255U, |
| 2464 |
#endif |
| 2465 |
output_encoding); |
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 2471 |
|
| 2472 |
|
| 2473 |
|
| 2474 |
|
| 2475 |
if (output_encoding == P_sRGB) |
| 2476 |
{ |
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
back_r = png_sRGB_table[back_r]; |
| 2482 |
back_g = png_sRGB_table[back_g]; |
| 2483 |
back_b = png_sRGB_table[back_b]; |
| 2484 |
} |
| 2485 |
|
| 2486 |
for (a=1; a<5; ++a) |
| 2487 |
{ |
| 2488 |
unsigned int g; |
| 2489 |
|
| 2490 |
|
| 2491 |
|
| 2492 |
|
| 2493 |
png_uint_32 alpha = 51 * a; |
| 2494 |
png_uint_32 back_rx = (255-alpha) * back_r; |
| 2495 |
png_uint_32 back_gx = (255-alpha) * back_g; |
| 2496 |
png_uint_32 back_bx = (255-alpha) * back_b; |
| 2497 |
|
| 2498 |
for (g=0; g<6; ++g) |
| 2499 |
{ |
| 2500 |
png_uint_32 gray = png_sRGB_table[g*51] * alpha; |
| 2501 |
|
| 2502 |
png_create_colormap_entry(display, i++, |
| 2503 |
PNG_sRGB_FROM_LINEAR(gray + back_rx), |
| 2504 |
PNG_sRGB_FROM_LINEAR(gray + back_gx), |
| 2505 |
PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); |
| 2506 |
} |
| 2507 |
} |
| 2508 |
|
| 2509 |
cmap_entries = i; |
| 2510 |
output_processing = PNG_CMAP_GA; |
| 2511 |
} |
| 2512 |
} |
| 2513 |
break; |
| 2514 |
|
| 2515 |
case PNG_COLOR_TYPE_RGB: |
| 2516 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 2517 |
|
| 2518 |
|
| 2519 |
|
| 2520 |
if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) |
| 2521 |
{ |
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, |
| 2531 |
-1); |
| 2532 |
data_encoding = P_sRGB; |
| 2533 |
|
| 2534 |
|
| 2535 |
|
| 2536 |
|
| 2537 |
if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2538 |
png_ptr->num_trans > 0) && |
| 2539 |
(output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2540 |
{ |
| 2541 |
|
| 2542 |
|
| 2543 |
|
| 2544 |
|
| 2545 |
expand_tRNS = 1; |
| 2546 |
|
| 2547 |
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) |
| 2548 |
png_error(png_ptr, "rgb[ga] color-map: too few entries"); |
| 2549 |
|
| 2550 |
cmap_entries = (unsigned int)make_ga_colormap(display); |
| 2551 |
background_index = PNG_CMAP_GA_BACKGROUND; |
| 2552 |
output_processing = PNG_CMAP_GA; |
| 2553 |
} |
| 2554 |
|
| 2555 |
else |
| 2556 |
{ |
| 2557 |
|
| 2558 |
|
| 2559 |
|
| 2560 |
|
| 2561 |
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) |
| 2562 |
png_error(png_ptr, "rgb[gray] color-map: too few entries"); |
| 2563 |
|
| 2564 |
|
| 2565 |
|
| 2566 |
|
| 2567 |
|
| 2568 |
|
| 2569 |
|
| 2570 |
|
| 2571 |
|
| 2572 |
if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2573 |
png_ptr->num_trans > 0) && |
| 2574 |
png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0) |
| 2575 |
{ |
| 2576 |
cmap_entries = (unsigned int)make_gray_file_colormap(display); |
| 2577 |
data_encoding = P_FILE; |
| 2578 |
} |
| 2579 |
|
| 2580 |
else |
| 2581 |
cmap_entries = (unsigned int)make_gray_colormap(display); |
| 2582 |
|
| 2583 |
|
| 2584 |
|
| 2585 |
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2586 |
png_ptr->num_trans > 0) |
| 2587 |
{ |
| 2588 |
png_color_16 c; |
| 2589 |
png_uint_32 gray = back_g; |
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
if (data_encoding == P_FILE) |
| 2597 |
{ |
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
if (output_encoding == P_sRGB) |
| 2604 |
gray = png_sRGB_table[gray]; |
| 2605 |
|
| 2606 |
gray = PNG_DIV257(png_gamma_16bit_correct(gray, |
| 2607 |
png_ptr->colorspace.gamma)); |
| 2608 |
|
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
png_create_colormap_entry(display, gray, back_g, back_g, |
| 2613 |
back_g, 0, output_encoding); |
| 2614 |
} |
| 2615 |
|
| 2616 |
else if (output_encoding == P_LINEAR) |
| 2617 |
{ |
| 2618 |
gray = PNG_sRGB_FROM_LINEAR(gray * 255); |
| 2619 |
|
| 2620 |
|
| 2621 |
|
| 2622 |
png_create_colormap_entry(display, gray, back_g, back_g, |
| 2623 |
back_g, 0, P_LINEAR); |
| 2624 |
} |
| 2625 |
|
| 2626 |
|
| 2627 |
|
| 2628 |
|
| 2629 |
c.index = 0; |
| 2630 |
c.gray = c.red = c.green = c.blue = (png_uint_16)gray; |
| 2631 |
|
| 2632 |
|
| 2633 |
|
| 2634 |
|
| 2635 |
|
| 2636 |
expand_tRNS = 1; |
| 2637 |
png_set_background_fixed(png_ptr, &c, |
| 2638 |
PNG_BACKGROUND_GAMMA_SCREEN, 0, |
| 2639 |
0); |
| 2640 |
} |
| 2641 |
|
| 2642 |
output_processing = PNG_CMAP_NONE; |
| 2643 |
} |
| 2644 |
} |
| 2645 |
|
| 2646 |
else |
| 2647 |
{ |
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
data_encoding = P_sRGB; |
| 2654 |
|
| 2655 |
|
| 2656 |
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 2657 |
png_ptr->num_trans > 0) |
| 2658 |
{ |
| 2659 |
|
| 2660 |
|
| 2661 |
|
| 2662 |
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 2663 |
{ |
| 2664 |
png_uint_32 r; |
| 2665 |
|
| 2666 |
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2667 |
png_error(png_ptr, "rgb+alpha color-map: too few entries"); |
| 2668 |
|
| 2669 |
cmap_entries = (unsigned int)make_rgb_colormap(display); |
| 2670 |
|
| 2671 |
|
| 2672 |
png_create_colormap_entry(display, cmap_entries, 255, 255, |
| 2673 |
255, 0, P_sRGB); |
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
background_index = cmap_entries++; |
| 2679 |
|
| 2680 |
|
| 2681 |
for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2682 |
{ |
| 2683 |
png_uint_32 g; |
| 2684 |
|
| 2685 |
for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2686 |
{ |
| 2687 |
png_uint_32 b; |
| 2688 |
|
| 2689 |
|
| 2690 |
|
| 2691 |
|
| 2692 |
for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2693 |
png_create_colormap_entry(display, cmap_entries++, |
| 2694 |
r, g, b, 128, P_sRGB); |
| 2695 |
} |
| 2696 |
} |
| 2697 |
|
| 2698 |
expand_tRNS = 1; |
| 2699 |
output_processing = PNG_CMAP_RGB_ALPHA; |
| 2700 |
} |
| 2701 |
|
| 2702 |
else |
| 2703 |
{ |
| 2704 |
|
| 2705 |
|
| 2706 |
|
| 2707 |
|
| 2708 |
|
| 2709 |
|
| 2710 |
|
| 2711 |
unsigned int sample_size = |
| 2712 |
PNG_IMAGE_SAMPLE_SIZE(output_format); |
| 2713 |
png_uint_32 r, g, b; |
| 2714 |
|
| 2715 |
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) |
| 2716 |
png_error(png_ptr, "rgb-alpha color-map: too few entries"); |
| 2717 |
|
| 2718 |
cmap_entries = (unsigned int)make_rgb_colormap(display); |
| 2719 |
|
| 2720 |
png_create_colormap_entry(display, cmap_entries, back_r, |
| 2721 |
back_g, back_b, 0, output_encoding); |
| 2722 |
|
| 2723 |
if (output_encoding == P_LINEAR) |
| 2724 |
{ |
| 2725 |
r = PNG_sRGB_FROM_LINEAR(back_r * 255); |
| 2726 |
g = PNG_sRGB_FROM_LINEAR(back_g * 255); |
| 2727 |
b = PNG_sRGB_FROM_LINEAR(back_b * 255); |
| 2728 |
} |
| 2729 |
|
| 2730 |
else |
| 2731 |
{ |
| 2732 |
r = back_r; |
| 2733 |
g = back_g; |
| 2734 |
b = back_g; |
| 2735 |
} |
| 2736 |
|
| 2737 |
|
| 2738 |
|
| 2739 |
|
| 2740 |
|
| 2741 |
|
| 2742 |
if (memcmp((png_const_bytep)display->colormap + |
| 2743 |
sample_size * cmap_entries, |
| 2744 |
(png_const_bytep)display->colormap + |
| 2745 |
sample_size * PNG_RGB_INDEX(r,g,b), |
| 2746 |
sample_size) != 0) |
| 2747 |
{ |
| 2748 |
|
| 2749 |
background_index = cmap_entries++; |
| 2750 |
|
| 2751 |
|
| 2752 |
|
| 2753 |
|
| 2754 |
for (r=0; r<256; r = (r << 1) | 0x7f) |
| 2755 |
{ |
| 2756 |
for (g=0; g<256; g = (g << 1) | 0x7f) |
| 2757 |
{ |
| 2758 |
|
| 2759 |
|
| 2760 |
|
| 2761 |
for (b=0; b<256; b = (b << 1) | 0x7f) |
| 2762 |
png_create_colormap_entry(display, cmap_entries++, |
| 2763 |
png_colormap_compose(display, r, P_sRGB, 128, |
| 2764 |
back_r, output_encoding), |
| 2765 |
png_colormap_compose(display, g, P_sRGB, 128, |
| 2766 |
back_g, output_encoding), |
| 2767 |
png_colormap_compose(display, b, P_sRGB, 128, |
| 2768 |
back_b, output_encoding), |
| 2769 |
0, output_encoding); |
| 2770 |
} |
| 2771 |
} |
| 2772 |
|
| 2773 |
expand_tRNS = 1; |
| 2774 |
output_processing = PNG_CMAP_RGB_ALPHA; |
| 2775 |
} |
| 2776 |
|
| 2777 |
else |
| 2778 |
{ |
| 2779 |
png_color_16 c; |
| 2780 |
|
| 2781 |
c.index = 0; |
| 2782 |
c.red = (png_uint_16)back_r; |
| 2783 |
c.gray = c.green = (png_uint_16)back_g; |
| 2784 |
c.blue = (png_uint_16)back_b; |
| 2785 |
|
| 2786 |
png_set_background_fixed(png_ptr, &c, |
| 2787 |
PNG_BACKGROUND_GAMMA_SCREEN, 0, |
| 2788 |
0); |
| 2789 |
|
| 2790 |
output_processing = PNG_CMAP_RGB; |
| 2791 |
} |
| 2792 |
} |
| 2793 |
} |
| 2794 |
|
| 2795 |
else |
| 2796 |
{ |
| 2797 |
|
| 2798 |
|
| 2799 |
|
| 2800 |
if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) |
| 2801 |
png_error(png_ptr, "rgb color-map: too few entries"); |
| 2802 |
|
| 2803 |
cmap_entries = (unsigned int)make_rgb_colormap(display); |
| 2804 |
output_processing = PNG_CMAP_RGB; |
| 2805 |
} |
| 2806 |
} |
| 2807 |
break; |
| 2808 |
|
| 2809 |
case PNG_COLOR_TYPE_PALETTE: |
| 2810 |
|
| 2811 |
|
| 2812 |
|
| 2813 |
{ |
| 2814 |
unsigned int num_trans = png_ptr->num_trans; |
| 2815 |
png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; |
| 2816 |
png_const_colorp colormap = png_ptr->palette; |
| 2817 |
int do_background = trans != NULL && |
| 2818 |
(output_format & PNG_FORMAT_FLAG_ALPHA) == 0; |
| 2819 |
unsigned int i; |
| 2820 |
|
| 2821 |
|
| 2822 |
if (trans == NULL) |
| 2823 |
num_trans = 0; |
| 2824 |
|
| 2825 |
output_processing = PNG_CMAP_NONE; |
| 2826 |
data_encoding = P_FILE; |
| 2827 |
cmap_entries = (unsigned int)png_ptr->num_palette; |
| 2828 |
if (cmap_entries > 256) |
| 2829 |
cmap_entries = 256; |
| 2830 |
|
| 2831 |
if (cmap_entries > (unsigned int)image->colormap_entries) |
| 2832 |
png_error(png_ptr, "palette color-map: too few entries"); |
| 2833 |
|
| 2834 |
for (i=0; i < cmap_entries; ++i) |
| 2835 |
{ |
| 2836 |
if (do_background != 0 && i < num_trans && trans[i] < 255) |
| 2837 |
{ |
| 2838 |
if (trans[i] == 0) |
| 2839 |
png_create_colormap_entry(display, i, back_r, back_g, |
| 2840 |
back_b, 0, output_encoding); |
| 2841 |
|
| 2842 |
else |
| 2843 |
{ |
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
png_create_colormap_entry(display, i, |
| 2848 |
png_colormap_compose(display, colormap[i].red, |
| 2849 |
P_FILE, trans[i], back_r, output_encoding), |
| 2850 |
png_colormap_compose(display, colormap[i].green, |
| 2851 |
P_FILE, trans[i], back_g, output_encoding), |
| 2852 |
png_colormap_compose(display, colormap[i].blue, |
| 2853 |
P_FILE, trans[i], back_b, output_encoding), |
| 2854 |
output_encoding == P_LINEAR ? trans[i] * 257U : |
| 2855 |
trans[i], |
| 2856 |
output_encoding); |
| 2857 |
} |
| 2858 |
} |
| 2859 |
|
| 2860 |
else |
| 2861 |
png_create_colormap_entry(display, i, colormap[i].red, |
| 2862 |
colormap[i].green, colormap[i].blue, |
| 2863 |
i < num_trans ? trans[i] : 255U, P_FILE); |
| 2864 |
} |
| 2865 |
|
| 2866 |
|
| 2867 |
|
| 2868 |
|
| 2869 |
if (png_ptr->bit_depth < 8) |
| 2870 |
png_set_packing(png_ptr); |
| 2871 |
} |
| 2872 |
break; |
| 2873 |
|
| 2874 |
default: |
| 2875 |
png_error(png_ptr, "invalid PNG color type"); |
| 2876 |
|
| 2877 |
} |
| 2878 |
|
| 2879 |
|
| 2880 |
if (expand_tRNS != 0 && png_ptr->num_trans > 0 && |
| 2881 |
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) |
| 2882 |
png_set_tRNS_to_alpha(png_ptr); |
| 2883 |
|
| 2884 |
switch (data_encoding) |
| 2885 |
{ |
| 2886 |
case P_sRGB: |
| 2887 |
|
| 2888 |
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); |
| 2889 |
|
| 2890 |
|
| 2891 |
case P_FILE: |
| 2892 |
if (png_ptr->bit_depth > 8) |
| 2893 |
png_set_scale_16(png_ptr); |
| 2894 |
break; |
| 2895 |
|
| 2896 |
#ifdef __GNUC__ |
| 2897 |
default: |
| 2898 |
png_error(png_ptr, "bad data option (internal error)"); |
| 2899 |
#endif |
| 2900 |
} |
| 2901 |
|
| 2902 |
if (cmap_entries > 256 || cmap_entries > image->colormap_entries) |
| 2903 |
png_error(png_ptr, "color map overflow (BAD internal error)"); |
| 2904 |
|
| 2905 |
image->colormap_entries = cmap_entries; |
| 2906 |
|
| 2907 |
|
| 2908 |
switch (output_processing) |
| 2909 |
{ |
| 2910 |
case PNG_CMAP_NONE: |
| 2911 |
if (background_index != PNG_CMAP_NONE_BACKGROUND) |
| 2912 |
goto bad_background; |
| 2913 |
break; |
| 2914 |
|
| 2915 |
case PNG_CMAP_GA: |
| 2916 |
if (background_index != PNG_CMAP_GA_BACKGROUND) |
| 2917 |
goto bad_background; |
| 2918 |
break; |
| 2919 |
|
| 2920 |
case PNG_CMAP_TRANS: |
| 2921 |
if (background_index >= cmap_entries || |
| 2922 |
background_index != PNG_CMAP_TRANS_BACKGROUND) |
| 2923 |
goto bad_background; |
| 2924 |
break; |
| 2925 |
|
| 2926 |
case PNG_CMAP_RGB: |
| 2927 |
if (background_index != PNG_CMAP_RGB_BACKGROUND) |
| 2928 |
goto bad_background; |
| 2929 |
break; |
| 2930 |
|
| 2931 |
case PNG_CMAP_RGB_ALPHA: |
| 2932 |
if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) |
| 2933 |
goto bad_background; |
| 2934 |
break; |
| 2935 |
|
| 2936 |
default: |
| 2937 |
png_error(png_ptr, "bad processing option (internal error)"); |
| 2938 |
|
| 2939 |
bad_background: |
| 2940 |
png_error(png_ptr, "bad background index (internal error)"); |
| 2941 |
} |
| 2942 |
|
| 2943 |
display->colormap_processing = (int)output_processing; |
| 2944 |
|
| 2945 |
return 1; |
| 2946 |
} |
| 2947 |
|
| 2948 |
|
| 2949 |
static int |
| 2950 |
png_image_read_and_map(png_voidp argument) |
| 2951 |
{ |
| 2952 |
png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 2953 |
argument); |
| 2954 |
png_imagep image = display->image; |
| 2955 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 2956 |
int passes; |
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
switch (png_ptr->interlaced) |
| 2963 |
{ |
| 2964 |
case PNG_INTERLACE_NONE: |
| 2965 |
passes = 1; |
| 2966 |
break; |
| 2967 |
|
| 2968 |
case PNG_INTERLACE_ADAM7: |
| 2969 |
passes = PNG_INTERLACE_ADAM7_PASSES; |
| 2970 |
break; |
| 2971 |
|
| 2972 |
default: |
| 2973 |
png_error(png_ptr, "unknown interlace type"); |
| 2974 |
} |
| 2975 |
|
| 2976 |
{ |
| 2977 |
png_uint_32 height = image->height; |
| 2978 |
png_uint_32 width = image->width; |
| 2979 |
int proc = display->colormap_processing; |
| 2980 |
png_bytep first_row = png_voidcast(png_bytep, display->first_row); |
| 2981 |
ptrdiff_t step_row = display->row_bytes; |
| 2982 |
int pass; |
| 2983 |
|
| 2984 |
for (pass = 0; pass < passes; ++pass) |
| 2985 |
{ |
| 2986 |
unsigned int startx, stepx, stepy; |
| 2987 |
png_uint_32 y; |
| 2988 |
|
| 2989 |
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 2990 |
{ |
| 2991 |
|
| 2992 |
if (PNG_PASS_COLS(width, pass) == 0) |
| 2993 |
continue; |
| 2994 |
|
| 2995 |
startx = PNG_PASS_START_COL(pass); |
| 2996 |
stepx = PNG_PASS_COL_OFFSET(pass); |
| 2997 |
y = PNG_PASS_START_ROW(pass); |
| 2998 |
stepy = PNG_PASS_ROW_OFFSET(pass); |
| 2999 |
} |
| 3000 |
|
| 3001 |
else |
| 3002 |
{ |
| 3003 |
y = 0; |
| 3004 |
startx = 0; |
| 3005 |
stepx = stepy = 1; |
| 3006 |
} |
| 3007 |
|
| 3008 |
for (; y<height; y += stepy) |
| 3009 |
{ |
| 3010 |
png_bytep inrow = png_voidcast(png_bytep, display->local_row); |
| 3011 |
png_bytep outrow = first_row + y * step_row; |
| 3012 |
png_const_bytep end_row = outrow + width; |
| 3013 |
|
| 3014 |
|
| 3015 |
png_read_row(png_ptr, inrow, NULL); |
| 3016 |
|
| 3017 |
|
| 3018 |
|
| 3019 |
|
| 3020 |
|
| 3021 |
outrow += startx; |
| 3022 |
switch (proc) |
| 3023 |
{ |
| 3024 |
case PNG_CMAP_GA: |
| 3025 |
for (; outrow < end_row; outrow += stepx) |
| 3026 |
{ |
| 3027 |
|
| 3028 |
unsigned int gray = *inrow++; |
| 3029 |
unsigned int alpha = *inrow++; |
| 3030 |
unsigned int entry; |
| 3031 |
|
| 3032 |
|
| 3033 |
|
| 3034 |
|
| 3035 |
|
| 3036 |
if (alpha > 229) |
| 3037 |
{ |
| 3038 |
entry = (231 * gray + 128) >> 8; |
| 3039 |
} |
| 3040 |
else if (alpha < 26) |
| 3041 |
{ |
| 3042 |
entry = 231; |
| 3043 |
} |
| 3044 |
else |
| 3045 |
{ |
| 3046 |
entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); |
| 3047 |
} |
| 3048 |
|
| 3049 |
*outrow = (png_byte)entry; |
| 3050 |
} |
| 3051 |
break; |
| 3052 |
|
| 3053 |
case PNG_CMAP_TRANS: |
| 3054 |
for (; outrow < end_row; outrow += stepx) |
| 3055 |
{ |
| 3056 |
png_byte gray = *inrow++; |
| 3057 |
png_byte alpha = *inrow++; |
| 3058 |
|
| 3059 |
if (alpha == 0) |
| 3060 |
*outrow = PNG_CMAP_TRANS_BACKGROUND; |
| 3061 |
|
| 3062 |
else if (gray != PNG_CMAP_TRANS_BACKGROUND) |
| 3063 |
*outrow = gray; |
| 3064 |
|
| 3065 |
else |
| 3066 |
*outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); |
| 3067 |
} |
| 3068 |
break; |
| 3069 |
|
| 3070 |
case PNG_CMAP_RGB: |
| 3071 |
for (; outrow < end_row; outrow += stepx) |
| 3072 |
{ |
| 3073 |
*outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); |
| 3074 |
inrow += 3; |
| 3075 |
} |
| 3076 |
break; |
| 3077 |
|
| 3078 |
case PNG_CMAP_RGB_ALPHA: |
| 3079 |
for (; outrow < end_row; outrow += stepx) |
| 3080 |
{ |
| 3081 |
unsigned int alpha = inrow[3]; |
| 3082 |
|
| 3083 |
|
| 3084 |
|
| 3085 |
|
| 3086 |
|
| 3087 |
|
| 3088 |
if (alpha >= 196) |
| 3089 |
*outrow = PNG_RGB_INDEX(inrow[0], inrow[1], |
| 3090 |
inrow[2]); |
| 3091 |
|
| 3092 |
else if (alpha < 64) |
| 3093 |
*outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; |
| 3094 |
|
| 3095 |
else |
| 3096 |
{ |
| 3097 |
|
| 3098 |
|
| 3099 |
|
| 3100 |
|
| 3101 |
|
| 3102 |
|
| 3103 |
unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; |
| 3104 |
|
| 3105 |
|
| 3106 |
|
| 3107 |
|
| 3108 |
|
| 3109 |
|
| 3110 |
|
| 3111 |
|
| 3112 |
|
| 3113 |
|
| 3114 |
if (inrow[0] & 0x80) back_i += 9; |
| 3115 |
if (inrow[0] & 0x40) back_i += 9; |
| 3116 |
if (inrow[0] & 0x80) back_i += 3; |
| 3117 |
if (inrow[0] & 0x40) back_i += 3; |
| 3118 |
if (inrow[0] & 0x80) back_i += 1; |
| 3119 |
if (inrow[0] & 0x40) back_i += 1; |
| 3120 |
|
| 3121 |
*outrow = (png_byte)back_i; |
| 3122 |
} |
| 3123 |
|
| 3124 |
inrow += 4; |
| 3125 |
} |
| 3126 |
break; |
| 3127 |
|
| 3128 |
default: |
| 3129 |
break; |
| 3130 |
} |
| 3131 |
} |
| 3132 |
} |
| 3133 |
} |
| 3134 |
|
| 3135 |
return 1; |
| 3136 |
} |
| 3137 |
|
| 3138 |
static int |
| 3139 |
png_image_read_colormapped(png_voidp argument) |
| 3140 |
{ |
| 3141 |
png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3142 |
argument); |
| 3143 |
png_imagep image = display->image; |
| 3144 |
png_controlp control = image->opaque; |
| 3145 |
png_structrp png_ptr = control->png_ptr; |
| 3146 |
png_inforp info_ptr = control->info_ptr; |
| 3147 |
|
| 3148 |
int passes = 0; |
| 3149 |
|
| 3150 |
PNG_SKIP_CHUNKS(png_ptr); |
| 3151 |
|
| 3152 |
|
| 3153 |
|
| 3154 |
|
| 3155 |
|
| 3156 |
if (display->colormap_processing == PNG_CMAP_NONE) |
| 3157 |
passes = png_set_interlace_handling(png_ptr); |
| 3158 |
|
| 3159 |
png_read_update_info(png_ptr, info_ptr); |
| 3160 |
|
| 3161 |
|
| 3162 |
switch (display->colormap_processing) |
| 3163 |
{ |
| 3164 |
case PNG_CMAP_NONE: |
| 3165 |
|
| 3166 |
|
| 3167 |
|
| 3168 |
if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || |
| 3169 |
info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && |
| 3170 |
info_ptr->bit_depth == 8) |
| 3171 |
break; |
| 3172 |
|
| 3173 |
goto bad_output; |
| 3174 |
|
| 3175 |
case PNG_CMAP_TRANS: |
| 3176 |
case PNG_CMAP_GA: |
| 3177 |
|
| 3178 |
|
| 3179 |
|
| 3180 |
|
| 3181 |
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && |
| 3182 |
info_ptr->bit_depth == 8 && |
| 3183 |
png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3184 |
image->colormap_entries == 256) |
| 3185 |
break; |
| 3186 |
|
| 3187 |
goto bad_output; |
| 3188 |
|
| 3189 |
case PNG_CMAP_RGB: |
| 3190 |
|
| 3191 |
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && |
| 3192 |
info_ptr->bit_depth == 8 && |
| 3193 |
png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3194 |
image->colormap_entries == 216) |
| 3195 |
break; |
| 3196 |
|
| 3197 |
goto bad_output; |
| 3198 |
|
| 3199 |
case PNG_CMAP_RGB_ALPHA: |
| 3200 |
|
| 3201 |
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
| 3202 |
info_ptr->bit_depth == 8 && |
| 3203 |
png_ptr->screen_gamma == PNG_GAMMA_sRGB && |
| 3204 |
image->colormap_entries == 244 ) |
| 3205 |
break; |
| 3206 |
|
| 3207 |
goto bad_output; |
| 3208 |
|
| 3209 |
default: |
| 3210 |
bad_output: |
| 3211 |
png_error(png_ptr, "bad color-map processing (internal error)"); |
| 3212 |
} |
| 3213 |
|
| 3214 |
|
| 3215 |
|
| 3216 |
|
| 3217 |
|
| 3218 |
{ |
| 3219 |
png_voidp first_row = display->buffer; |
| 3220 |
ptrdiff_t row_bytes = display->row_stride; |
| 3221 |
|
| 3222 |
|
| 3223 |
|
| 3224 |
|
| 3225 |
if (row_bytes < 0) |
| 3226 |
{ |
| 3227 |
char *ptr = png_voidcast(char*, first_row); |
| 3228 |
ptr += (image->height-1) * (-row_bytes); |
| 3229 |
first_row = png_voidcast(png_voidp, ptr); |
| 3230 |
} |
| 3231 |
|
| 3232 |
display->first_row = first_row; |
| 3233 |
display->row_bytes = row_bytes; |
| 3234 |
} |
| 3235 |
|
| 3236 |
if (passes == 0) |
| 3237 |
{ |
| 3238 |
int result; |
| 3239 |
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 3240 |
|
| 3241 |
display->local_row = row; |
| 3242 |
result = png_safe_execute(image, png_image_read_and_map, display); |
| 3243 |
display->local_row = NULL; |
| 3244 |
png_free(png_ptr, row); |
| 3245 |
|
| 3246 |
return result; |
| 3247 |
} |
| 3248 |
|
| 3249 |
else |
| 3250 |
{ |
| 3251 |
png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; |
| 3252 |
|
| 3253 |
while (--passes >= 0) |
| 3254 |
{ |
| 3255 |
png_uint_32 y = image->height; |
| 3256 |
png_bytep row = png_voidcast(png_bytep, display->first_row); |
| 3257 |
|
| 3258 |
for (; y > 0; --y) |
| 3259 |
{ |
| 3260 |
png_read_row(png_ptr, row, NULL); |
| 3261 |
row += row_bytes; |
| 3262 |
} |
| 3263 |
} |
| 3264 |
|
| 3265 |
return 1; |
| 3266 |
} |
| 3267 |
} |
| 3268 |
|
| 3269 |
|
| 3270 |
static int |
| 3271 |
png_image_read_composite(png_voidp argument) |
| 3272 |
{ |
| 3273 |
png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3274 |
argument); |
| 3275 |
png_imagep image = display->image; |
| 3276 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 3277 |
int passes; |
| 3278 |
|
| 3279 |
switch (png_ptr->interlaced) |
| 3280 |
{ |
| 3281 |
case PNG_INTERLACE_NONE: |
| 3282 |
passes = 1; |
| 3283 |
break; |
| 3284 |
|
| 3285 |
case PNG_INTERLACE_ADAM7: |
| 3286 |
passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3287 |
break; |
| 3288 |
|
| 3289 |
default: |
| 3290 |
png_error(png_ptr, "unknown interlace type"); |
| 3291 |
} |
| 3292 |
|
| 3293 |
{ |
| 3294 |
png_uint_32 height = image->height; |
| 3295 |
png_uint_32 width = image->width; |
| 3296 |
ptrdiff_t step_row = display->row_bytes; |
| 3297 |
unsigned int channels = |
| 3298 |
(image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; |
| 3299 |
int pass; |
| 3300 |
|
| 3301 |
for (pass = 0; pass < passes; ++pass) |
| 3302 |
{ |
| 3303 |
unsigned int startx, stepx, stepy; |
| 3304 |
png_uint_32 y; |
| 3305 |
|
| 3306 |
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3307 |
{ |
| 3308 |
|
| 3309 |
if (PNG_PASS_COLS(width, pass) == 0) |
| 3310 |
continue; |
| 3311 |
|
| 3312 |
startx = PNG_PASS_START_COL(pass) * channels; |
| 3313 |
stepx = PNG_PASS_COL_OFFSET(pass) * channels; |
| 3314 |
y = PNG_PASS_START_ROW(pass); |
| 3315 |
stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3316 |
} |
| 3317 |
|
| 3318 |
else |
| 3319 |
{ |
| 3320 |
y = 0; |
| 3321 |
startx = 0; |
| 3322 |
stepx = channels; |
| 3323 |
stepy = 1; |
| 3324 |
} |
| 3325 |
|
| 3326 |
for (; y<height; y += stepy) |
| 3327 |
{ |
| 3328 |
png_bytep inrow = png_voidcast(png_bytep, display->local_row); |
| 3329 |
png_bytep outrow; |
| 3330 |
png_const_bytep end_row; |
| 3331 |
|
| 3332 |
|
| 3333 |
png_read_row(png_ptr, inrow, NULL); |
| 3334 |
|
| 3335 |
outrow = png_voidcast(png_bytep, display->first_row); |
| 3336 |
outrow += y * step_row; |
| 3337 |
end_row = outrow + width * channels; |
| 3338 |
|
| 3339 |
|
| 3340 |
outrow += startx; |
| 3341 |
for (; outrow < end_row; outrow += stepx) |
| 3342 |
{ |
| 3343 |
png_byte alpha = inrow[channels]; |
| 3344 |
|
| 3345 |
if (alpha > 0) |
| 3346 |
{ |
| 3347 |
unsigned int c; |
| 3348 |
|
| 3349 |
for (c=0; c<channels; ++c) |
| 3350 |
{ |
| 3351 |
png_uint_32 component = inrow[c]; |
| 3352 |
|
| 3353 |
if (alpha < 255) |
| 3354 |
{ |
| 3355 |
|
| 3356 |
|
| 3357 |
|
| 3358 |
|
| 3359 |
|
| 3360 |
|
| 3361 |
component *= 257*255; |
| 3362 |
component += (255-alpha)*png_sRGB_table[outrow[c]]; |
| 3363 |
|
| 3364 |
|
| 3365 |
|
| 3366 |
|
| 3367 |
|
| 3368 |
component = PNG_sRGB_FROM_LINEAR(component); |
| 3369 |
} |
| 3370 |
|
| 3371 |
outrow[c] = (png_byte)component; |
| 3372 |
} |
| 3373 |
} |
| 3374 |
|
| 3375 |
inrow += channels+1; |
| 3376 |
} |
| 3377 |
} |
| 3378 |
} |
| 3379 |
} |
| 3380 |
|
| 3381 |
return 1; |
| 3382 |
} |
| 3383 |
|
| 3384 |
|
| 3385 |
|
| 3386 |
|
| 3387 |
|
| 3388 |
|
| 3389 |
|
| 3390 |
|
| 3391 |
|
| 3392 |
|
| 3393 |
|
| 3394 |
|
| 3395 |
|
| 3396 |
|
| 3397 |
static int |
| 3398 |
png_image_read_background(png_voidp argument) |
| 3399 |
{ |
| 3400 |
png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3401 |
argument); |
| 3402 |
png_imagep image = display->image; |
| 3403 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 3404 |
png_inforp info_ptr = image->opaque->info_ptr; |
| 3405 |
png_uint_32 height = image->height; |
| 3406 |
png_uint_32 width = image->width; |
| 3407 |
int pass, passes; |
| 3408 |
|
| 3409 |
|
| 3410 |
|
| 3411 |
|
| 3412 |
|
| 3413 |
|
| 3414 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 3415 |
png_error(png_ptr, "lost rgb to gray"); |
| 3416 |
|
| 3417 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 3418 |
png_error(png_ptr, "unexpected compose"); |
| 3419 |
|
| 3420 |
if (png_get_channels(png_ptr, info_ptr) != 2) |
| 3421 |
png_error(png_ptr, "lost/gained channels"); |
| 3422 |
|
| 3423 |
|
| 3424 |
if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && |
| 3425 |
(image->format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3426 |
png_error(png_ptr, "unexpected 8-bit transformation"); |
| 3427 |
|
| 3428 |
switch (png_ptr->interlaced) |
| 3429 |
{ |
| 3430 |
case PNG_INTERLACE_NONE: |
| 3431 |
passes = 1; |
| 3432 |
break; |
| 3433 |
|
| 3434 |
case PNG_INTERLACE_ADAM7: |
| 3435 |
passes = PNG_INTERLACE_ADAM7_PASSES; |
| 3436 |
break; |
| 3437 |
|
| 3438 |
default: |
| 3439 |
png_error(png_ptr, "unknown interlace type"); |
| 3440 |
} |
| 3441 |
|
| 3442 |
|
| 3443 |
|
| 3444 |
|
| 3445 |
|
| 3446 |
|
| 3447 |
switch (info_ptr->bit_depth) |
| 3448 |
{ |
| 3449 |
case 8: |
| 3450 |
|
| 3451 |
|
| 3452 |
|
| 3453 |
|
| 3454 |
|
| 3455 |
{ |
| 3456 |
png_bytep first_row = png_voidcast(png_bytep, display->first_row); |
| 3457 |
ptrdiff_t step_row = display->row_bytes; |
| 3458 |
|
| 3459 |
for (pass = 0; pass < passes; ++pass) |
| 3460 |
{ |
| 3461 |
unsigned int startx, stepx, stepy; |
| 3462 |
png_uint_32 y; |
| 3463 |
|
| 3464 |
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3465 |
{ |
| 3466 |
|
| 3467 |
if (PNG_PASS_COLS(width, pass) == 0) |
| 3468 |
continue; |
| 3469 |
|
| 3470 |
startx = PNG_PASS_START_COL(pass); |
| 3471 |
stepx = PNG_PASS_COL_OFFSET(pass); |
| 3472 |
y = PNG_PASS_START_ROW(pass); |
| 3473 |
stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3474 |
} |
| 3475 |
|
| 3476 |
else |
| 3477 |
{ |
| 3478 |
y = 0; |
| 3479 |
startx = 0; |
| 3480 |
stepx = stepy = 1; |
| 3481 |
} |
| 3482 |
|
| 3483 |
if (display->background == NULL) |
| 3484 |
{ |
| 3485 |
for (; y<height; y += stepy) |
| 3486 |
{ |
| 3487 |
png_bytep inrow = png_voidcast(png_bytep, |
| 3488 |
display->local_row); |
| 3489 |
png_bytep outrow = first_row + y * step_row; |
| 3490 |
png_const_bytep end_row = outrow + width; |
| 3491 |
|
| 3492 |
|
| 3493 |
png_read_row(png_ptr, inrow, NULL); |
| 3494 |
|
| 3495 |
|
| 3496 |
outrow += startx; |
| 3497 |
for (; outrow < end_row; outrow += stepx) |
| 3498 |
{ |
| 3499 |
png_byte alpha = inrow[1]; |
| 3500 |
|
| 3501 |
if (alpha > 0) |
| 3502 |
{ |
| 3503 |
png_uint_32 component = inrow[0]; |
| 3504 |
|
| 3505 |
if (alpha < 255) |
| 3506 |
{ |
| 3507 |
|
| 3508 |
|
| 3509 |
|
| 3510 |
|
| 3511 |
component = png_sRGB_table[component] * alpha; |
| 3512 |
component += png_sRGB_table[outrow[0]] * |
| 3513 |
(255-alpha); |
| 3514 |
component = PNG_sRGB_FROM_LINEAR(component); |
| 3515 |
} |
| 3516 |
|
| 3517 |
outrow[0] = (png_byte)component; |
| 3518 |
} |
| 3519 |
|
| 3520 |
inrow += 2; |
| 3521 |
} |
| 3522 |
} |
| 3523 |
} |
| 3524 |
|
| 3525 |
else |
| 3526 |
{ |
| 3527 |
png_byte background8 = display->background->green; |
| 3528 |
png_uint_16 background = png_sRGB_table[background8]; |
| 3529 |
|
| 3530 |
for (; y<height; y += stepy) |
| 3531 |
{ |
| 3532 |
png_bytep inrow = png_voidcast(png_bytep, |
| 3533 |
display->local_row); |
| 3534 |
png_bytep outrow = first_row + y * step_row; |
| 3535 |
png_const_bytep end_row = outrow + width; |
| 3536 |
|
| 3537 |
|
| 3538 |
png_read_row(png_ptr, inrow, NULL); |
| 3539 |
|
| 3540 |
|
| 3541 |
outrow += startx; |
| 3542 |
for (; outrow < end_row; outrow += stepx) |
| 3543 |
{ |
| 3544 |
png_byte alpha = inrow[1]; |
| 3545 |
|
| 3546 |
if (alpha > 0) |
| 3547 |
{ |
| 3548 |
png_uint_32 component = inrow[0]; |
| 3549 |
|
| 3550 |
if (alpha < 255) |
| 3551 |
{ |
| 3552 |
component = png_sRGB_table[component] * alpha; |
| 3553 |
component += background * (255-alpha); |
| 3554 |
component = PNG_sRGB_FROM_LINEAR(component); |
| 3555 |
} |
| 3556 |
|
| 3557 |
outrow[0] = (png_byte)component; |
| 3558 |
} |
| 3559 |
|
| 3560 |
else |
| 3561 |
outrow[0] = background8; |
| 3562 |
|
| 3563 |
inrow += 2; |
| 3564 |
} |
| 3565 |
} |
| 3566 |
} |
| 3567 |
} |
| 3568 |
} |
| 3569 |
break; |
| 3570 |
|
| 3571 |
case 16: |
| 3572 |
|
| 3573 |
|
| 3574 |
|
| 3575 |
|
| 3576 |
{ |
| 3577 |
png_uint_16p first_row = png_voidcast(png_uint_16p, |
| 3578 |
display->first_row); |
| 3579 |
|
| 3580 |
|
| 3581 |
|
| 3582 |
ptrdiff_t step_row = display->row_bytes / 2; |
| 3583 |
unsigned int preserve_alpha = (image->format & |
| 3584 |
PNG_FORMAT_FLAG_ALPHA) != 0; |
| 3585 |
unsigned int outchannels = 1U+preserve_alpha; |
| 3586 |
int swap_alpha = 0; |
| 3587 |
|
| 3588 |
# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED |
| 3589 |
if (preserve_alpha != 0 && |
| 3590 |
(image->format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 3591 |
swap_alpha = 1; |
| 3592 |
# endif |
| 3593 |
|
| 3594 |
for (pass = 0; pass < passes; ++pass) |
| 3595 |
{ |
| 3596 |
unsigned int startx, stepx, stepy; |
| 3597 |
png_uint_32 y; |
| 3598 |
|
| 3599 |
|
| 3600 |
|
| 3601 |
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) |
| 3602 |
{ |
| 3603 |
|
| 3604 |
if (PNG_PASS_COLS(width, pass) == 0) |
| 3605 |
continue; |
| 3606 |
|
| 3607 |
startx = PNG_PASS_START_COL(pass) * outchannels; |
| 3608 |
stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; |
| 3609 |
y = PNG_PASS_START_ROW(pass); |
| 3610 |
stepy = PNG_PASS_ROW_OFFSET(pass); |
| 3611 |
} |
| 3612 |
|
| 3613 |
else |
| 3614 |
{ |
| 3615 |
y = 0; |
| 3616 |
startx = 0; |
| 3617 |
stepx = outchannels; |
| 3618 |
stepy = 1; |
| 3619 |
} |
| 3620 |
|
| 3621 |
for (; y<height; y += stepy) |
| 3622 |
{ |
| 3623 |
png_const_uint_16p inrow; |
| 3624 |
png_uint_16p outrow = first_row + y*step_row; |
| 3625 |
png_uint_16p end_row = outrow + width * outchannels; |
| 3626 |
|
| 3627 |
|
| 3628 |
png_read_row(png_ptr, png_voidcast(png_bytep, |
| 3629 |
display->local_row), NULL); |
| 3630 |
inrow = png_voidcast(png_const_uint_16p, display->local_row); |
| 3631 |
|
| 3632 |
|
| 3633 |
|
| 3634 |
outrow += startx; |
| 3635 |
for (; outrow < end_row; outrow += stepx) |
| 3636 |
{ |
| 3637 |
png_uint_32 component = inrow[0]; |
| 3638 |
png_uint_16 alpha = inrow[1]; |
| 3639 |
|
| 3640 |
if (alpha > 0) |
| 3641 |
{ |
| 3642 |
if (alpha < 65535) |
| 3643 |
{ |
| 3644 |
component *= alpha; |
| 3645 |
component += 32767; |
| 3646 |
component /= 65535; |
| 3647 |
} |
| 3648 |
} |
| 3649 |
|
| 3650 |
else |
| 3651 |
component = 0; |
| 3652 |
|
| 3653 |
outrow[swap_alpha] = (png_uint_16)component; |
| 3654 |
if (preserve_alpha != 0) |
| 3655 |
outrow[1 ^ swap_alpha] = alpha; |
| 3656 |
|
| 3657 |
inrow += 2; |
| 3658 |
} |
| 3659 |
} |
| 3660 |
} |
| 3661 |
} |
| 3662 |
break; |
| 3663 |
|
| 3664 |
#ifdef __GNUC__ |
| 3665 |
default: |
| 3666 |
png_error(png_ptr, "unexpected bit depth"); |
| 3667 |
#endif |
| 3668 |
} |
| 3669 |
|
| 3670 |
return 1; |
| 3671 |
} |
| 3672 |
|
| 3673 |
|
| 3674 |
static int |
| 3675 |
png_image_read_direct(png_voidp argument) |
| 3676 |
{ |
| 3677 |
png_image_read_control *display = png_voidcast(png_image_read_control*, |
| 3678 |
argument); |
| 3679 |
png_imagep image = display->image; |
| 3680 |
png_structrp png_ptr = image->opaque->png_ptr; |
| 3681 |
png_inforp info_ptr = image->opaque->info_ptr; |
| 3682 |
|
| 3683 |
png_uint_32 format = image->format; |
| 3684 |
int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; |
| 3685 |
int do_local_compose = 0; |
| 3686 |
int do_local_background = 0; |
| 3687 |
int passes = 0; |
| 3688 |
|
| 3689 |
|
| 3690 |
|
| 3691 |
|
| 3692 |
|
| 3693 |
png_set_expand(png_ptr); |
| 3694 |
|
| 3695 |
|
| 3696 |
{ |
| 3697 |
png_uint_32 base_format = png_image_format(png_ptr) & |
| 3698 |
~PNG_FORMAT_FLAG_COLORMAP ; |
| 3699 |
png_uint_32 change = format ^ base_format; |
| 3700 |
png_fixed_point output_gamma; |
| 3701 |
int mode; |
| 3702 |
|
| 3703 |
|
| 3704 |
if ((change & PNG_FORMAT_FLAG_COLOR) != 0) |
| 3705 |
{ |
| 3706 |
|
| 3707 |
if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
| 3708 |
png_set_gray_to_rgb(png_ptr); |
| 3709 |
|
| 3710 |
else |
| 3711 |
{ |
| 3712 |
|
| 3713 |
|
| 3714 |
|
| 3715 |
|
| 3716 |
|
| 3717 |
|
| 3718 |
|
| 3719 |
|
| 3720 |
|
| 3721 |
|
| 3722 |
|
| 3723 |
|
| 3724 |
|
| 3725 |
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3726 |
do_local_background = 1; |
| 3727 |
|
| 3728 |
png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, |
| 3729 |
PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); |
| 3730 |
} |
| 3731 |
|
| 3732 |
change &= ~PNG_FORMAT_FLAG_COLOR; |
| 3733 |
} |
| 3734 |
|
| 3735 |
|
| 3736 |
|
| 3737 |
{ |
| 3738 |
png_fixed_point input_gamma_default; |
| 3739 |
|
| 3740 |
if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 && |
| 3741 |
(image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) |
| 3742 |
input_gamma_default = PNG_GAMMA_LINEAR; |
| 3743 |
else |
| 3744 |
input_gamma_default = PNG_DEFAULT_sRGB; |
| 3745 |
|
| 3746 |
|
| 3747 |
|
| 3748 |
|
| 3749 |
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); |
| 3750 |
} |
| 3751 |
|
| 3752 |
if (linear != 0) |
| 3753 |
{ |
| 3754 |
|
| 3755 |
|
| 3756 |
|
| 3757 |
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3758 |
mode = PNG_ALPHA_STANDARD; |
| 3759 |
|
| 3760 |
else |
| 3761 |
mode = PNG_ALPHA_PNG; |
| 3762 |
|
| 3763 |
output_gamma = PNG_GAMMA_LINEAR; |
| 3764 |
} |
| 3765 |
|
| 3766 |
else |
| 3767 |
{ |
| 3768 |
mode = PNG_ALPHA_PNG; |
| 3769 |
output_gamma = PNG_DEFAULT_sRGB; |
| 3770 |
} |
| 3771 |
|
| 3772 |
if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) |
| 3773 |
{ |
| 3774 |
mode = PNG_ALPHA_OPTIMIZED; |
| 3775 |
change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; |
| 3776 |
} |
| 3777 |
|
| 3778 |
|
| 3779 |
|
| 3780 |
|
| 3781 |
|
| 3782 |
|
| 3783 |
|
| 3784 |
if (do_local_background != 0) |
| 3785 |
{ |
| 3786 |
png_fixed_point gtest; |
| 3787 |
|
| 3788 |
|
| 3789 |
|
| 3790 |
|
| 3791 |
|
| 3792 |
|
| 3793 |
if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, |
| 3794 |
PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0) |
| 3795 |
do_local_background = 0; |
| 3796 |
|
| 3797 |
else if (mode == PNG_ALPHA_STANDARD) |
| 3798 |
{ |
| 3799 |
do_local_background = 2; |
| 3800 |
mode = PNG_ALPHA_PNG; |
| 3801 |
} |
| 3802 |
|
| 3803 |
|
| 3804 |
} |
| 3805 |
|
| 3806 |
|
| 3807 |
if ((change & PNG_FORMAT_FLAG_LINEAR) != 0) |
| 3808 |
{ |
| 3809 |
if (linear != 0 ) |
| 3810 |
png_set_expand_16(png_ptr); |
| 3811 |
|
| 3812 |
else |
| 3813 |
png_set_scale_16(png_ptr); |
| 3814 |
|
| 3815 |
change &= ~PNG_FORMAT_FLAG_LINEAR; |
| 3816 |
} |
| 3817 |
|
| 3818 |
|
| 3819 |
if ((change & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3820 |
{ |
| 3821 |
|
| 3822 |
|
| 3823 |
|
| 3824 |
|
| 3825 |
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3826 |
{ |
| 3827 |
|
| 3828 |
|
| 3829 |
|
| 3830 |
|
| 3831 |
|
| 3832 |
if (do_local_background != 0) |
| 3833 |
do_local_background = 2; |
| 3834 |
|
| 3835 |
|
| 3836 |
else if (linear != 0) |
| 3837 |
png_set_strip_alpha(png_ptr); |
| 3838 |
|
| 3839 |
|
| 3840 |
else if (display->background != NULL) |
| 3841 |
{ |
| 3842 |
png_color_16 c; |
| 3843 |
|
| 3844 |
c.index = 0; |
| 3845 |
c.red = display->background->red; |
| 3846 |
c.green = display->background->green; |
| 3847 |
c.blue = display->background->blue; |
| 3848 |
c.gray = display->background->green; |
| 3849 |
|
| 3850 |
|
| 3851 |
|
| 3852 |
|
| 3853 |
|
| 3854 |
|
| 3855 |
|
| 3856 |
png_set_background_fixed(png_ptr, &c, |
| 3857 |
PNG_BACKGROUND_GAMMA_SCREEN, 0, |
| 3858 |
0); |
| 3859 |
} |
| 3860 |
|
| 3861 |
else |
| 3862 |
{ |
| 3863 |
do_local_compose = 1; |
| 3864 |
|
| 3865 |
|
| 3866 |
|
| 3867 |
|
| 3868 |
|
| 3869 |
mode = PNG_ALPHA_OPTIMIZED; |
| 3870 |
} |
| 3871 |
} |
| 3872 |
|
| 3873 |
else |
| 3874 |
{ |
| 3875 |
|
| 3876 |
|
| 3877 |
|
| 3878 |
|
| 3879 |
|
| 3880 |
png_uint_32 filler; |
| 3881 |
int where; |
| 3882 |
|
| 3883 |
if (linear != 0) |
| 3884 |
filler = 65535; |
| 3885 |
|
| 3886 |
else |
| 3887 |
filler = 255; |
| 3888 |
|
| 3889 |
#ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 3890 |
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 3891 |
{ |
| 3892 |
where = PNG_FILLER_BEFORE; |
| 3893 |
change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3894 |
} |
| 3895 |
|
| 3896 |
else |
| 3897 |
#endif |
| 3898 |
where = PNG_FILLER_AFTER; |
| 3899 |
|
| 3900 |
png_set_add_alpha(png_ptr, filler, where); |
| 3901 |
} |
| 3902 |
|
| 3903 |
|
| 3904 |
change &= ~PNG_FORMAT_FLAG_ALPHA; |
| 3905 |
} |
| 3906 |
|
| 3907 |
|
| 3908 |
|
| 3909 |
|
| 3910 |
|
| 3911 |
png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); |
| 3912 |
|
| 3913 |
# ifdef PNG_FORMAT_BGR_SUPPORTED |
| 3914 |
if ((change & PNG_FORMAT_FLAG_BGR) != 0) |
| 3915 |
{ |
| 3916 |
|
| 3917 |
|
| 3918 |
|
| 3919 |
if ((format & PNG_FORMAT_FLAG_COLOR) != 0) |
| 3920 |
png_set_bgr(png_ptr); |
| 3921 |
|
| 3922 |
else |
| 3923 |
format &= ~PNG_FORMAT_FLAG_BGR; |
| 3924 |
|
| 3925 |
change &= ~PNG_FORMAT_FLAG_BGR; |
| 3926 |
} |
| 3927 |
# endif |
| 3928 |
|
| 3929 |
# ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 3930 |
if ((change & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 3931 |
{ |
| 3932 |
|
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
if ((format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3938 |
{ |
| 3939 |
|
| 3940 |
|
| 3941 |
|
| 3942 |
if (do_local_background != 2) |
| 3943 |
png_set_swap_alpha(png_ptr); |
| 3944 |
} |
| 3945 |
|
| 3946 |
else |
| 3947 |
format &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3948 |
|
| 3949 |
change &= ~PNG_FORMAT_FLAG_AFIRST; |
| 3950 |
} |
| 3951 |
# endif |
| 3952 |
|
| 3953 |
|
| 3954 |
|
| 3955 |
|
| 3956 |
if (linear != 0) |
| 3957 |
{ |
| 3958 |
png_uint_16 le = 0x0001; |
| 3959 |
|
| 3960 |
if ((*(png_const_bytep) & le) != 0) |
| 3961 |
png_set_swap(png_ptr); |
| 3962 |
} |
| 3963 |
|
| 3964 |
|
| 3965 |
if (change != 0) |
| 3966 |
png_error(png_ptr, "png_read_image: unsupported transformation"); |
| 3967 |
} |
| 3968 |
|
| 3969 |
PNG_SKIP_CHUNKS(png_ptr); |
| 3970 |
|
| 3971 |
|
| 3972 |
|
| 3973 |
|
| 3974 |
|
| 3975 |
|
| 3976 |
|
| 3977 |
if (do_local_compose == 0 && do_local_background != 2) |
| 3978 |
passes = png_set_interlace_handling(png_ptr); |
| 3979 |
|
| 3980 |
png_read_update_info(png_ptr, info_ptr); |
| 3981 |
|
| 3982 |
{ |
| 3983 |
png_uint_32 info_format = 0; |
| 3984 |
|
| 3985 |
if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 3986 |
info_format |= PNG_FORMAT_FLAG_COLOR; |
| 3987 |
|
| 3988 |
if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 3989 |
{ |
| 3990 |
|
| 3991 |
if (do_local_compose == 0) |
| 3992 |
{ |
| 3993 |
|
| 3994 |
if (do_local_background != 2 || |
| 3995 |
(format & PNG_FORMAT_FLAG_ALPHA) != 0) |
| 3996 |
info_format |= PNG_FORMAT_FLAG_ALPHA; |
| 3997 |
} |
| 3998 |
} |
| 3999 |
|
| 4000 |
else if (do_local_compose != 0) |
| 4001 |
png_error(png_ptr, "png_image_read: alpha channel lost"); |
| 4002 |
|
| 4003 |
if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) { |
| 4004 |
info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; |
| 4005 |
} |
| 4006 |
|
| 4007 |
if (info_ptr->bit_depth == 16) |
| 4008 |
info_format |= PNG_FORMAT_FLAG_LINEAR; |
| 4009 |
|
| 4010 |
#ifdef PNG_FORMAT_BGR_SUPPORTED |
| 4011 |
if ((png_ptr->transformations & PNG_BGR) != 0) |
| 4012 |
info_format |= PNG_FORMAT_FLAG_BGR; |
| 4013 |
#endif |
| 4014 |
|
| 4015 |
#ifdef PNG_FORMAT_AFIRST_SUPPORTED |
| 4016 |
if (do_local_background == 2) |
| 4017 |
{ |
| 4018 |
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) |
| 4019 |
info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 4020 |
} |
| 4021 |
|
| 4022 |
if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || |
| 4023 |
((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && |
| 4024 |
(png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) |
| 4025 |
{ |
| 4026 |
if (do_local_background == 2) |
| 4027 |
png_error(png_ptr, "unexpected alpha swap transformation"); |
| 4028 |
|
| 4029 |
info_format |= PNG_FORMAT_FLAG_AFIRST; |
| 4030 |
} |
| 4031 |
# endif |
| 4032 |
|
| 4033 |
|
| 4034 |
if (info_format != format) |
| 4035 |
png_error(png_ptr, "png_read_image: invalid transformations"); |
| 4036 |
} |
| 4037 |
|
| 4038 |
|
| 4039 |
|
| 4040 |
|
| 4041 |
|
| 4042 |
|
| 4043 |
{ |
| 4044 |
png_voidp first_row = display->buffer; |
| 4045 |
ptrdiff_t row_bytes = display->row_stride; |
| 4046 |
|
| 4047 |
if (linear != 0) |
| 4048 |
row_bytes *= 2; |
| 4049 |
|
| 4050 |
|
| 4051 |
|
| 4052 |
|
| 4053 |
if (row_bytes < 0) |
| 4054 |
{ |
| 4055 |
char *ptr = png_voidcast(char*, first_row); |
| 4056 |
ptr += (image->height-1) * (-row_bytes); |
| 4057 |
first_row = png_voidcast(png_voidp, ptr); |
| 4058 |
} |
| 4059 |
|
| 4060 |
display->first_row = first_row; |
| 4061 |
display->row_bytes = row_bytes; |
| 4062 |
} |
| 4063 |
|
| 4064 |
if (do_local_compose != 0) |
| 4065 |
{ |
| 4066 |
int result; |
| 4067 |
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 4068 |
|
| 4069 |
display->local_row = row; |
| 4070 |
result = png_safe_execute(image, png_image_read_composite, display); |
| 4071 |
display->local_row = NULL; |
| 4072 |
png_free(png_ptr, row); |
| 4073 |
|
| 4074 |
return result; |
| 4075 |
} |
| 4076 |
|
| 4077 |
else if (do_local_background == 2) |
| 4078 |
{ |
| 4079 |
int result; |
| 4080 |
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); |
| 4081 |
|
| 4082 |
display->local_row = row; |
| 4083 |
result = png_safe_execute(image, png_image_read_background, display); |
| 4084 |
display->local_row = NULL; |
| 4085 |
png_free(png_ptr, row); |
| 4086 |
|
| 4087 |
return result; |
| 4088 |
} |
| 4089 |
|
| 4090 |
else |
| 4091 |
{ |
| 4092 |
png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; |
| 4093 |
|
| 4094 |
while (--passes >= 0) |
| 4095 |
{ |
| 4096 |
png_uint_32 y = image->height; |
| 4097 |
png_bytep row = png_voidcast(png_bytep, display->first_row); |
| 4098 |
|
| 4099 |
for (; y > 0; --y) |
| 4100 |
{ |
| 4101 |
png_read_row(png_ptr, row, NULL); |
| 4102 |
row += row_bytes; |
| 4103 |
} |
| 4104 |
} |
| 4105 |
|
| 4106 |
return 1; |
| 4107 |
} |
| 4108 |
} |
| 4109 |
|
| 4110 |
int PNGAPI |
| 4111 |
png_image_finish_read(png_imagep image, png_const_colorp background, |
| 4112 |
void *buffer, png_int_32 row_stride, void *colormap) |
| 4113 |
{ |
| 4114 |
if (image != NULL && image->version == PNG_IMAGE_VERSION) |
| 4115 |
{ |
| 4116 |
|
| 4117 |
|
| 4118 |
|
| 4119 |
|
| 4120 |
unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format); |
| 4121 |
|
| 4122 |
|
| 4123 |
|
| 4124 |
|
| 4125 |
|
| 4126 |
|
| 4127 |
|
| 4128 |
if (image->width <= 0x7fffffffU/channels) |
| 4129 |
{ |
| 4130 |
png_uint_32 check; |
| 4131 |
png_uint_32 png_row_stride = image->width * channels; |
| 4132 |
|
| 4133 |
if (row_stride == 0) |
| 4134 |
row_stride = (png_int_32)png_row_stride; |
| 4135 |
|
| 4136 |
if (row_stride < 0) |
| 4137 |
check = (png_uint_32)(-row_stride); |
| 4138 |
|
| 4139 |
else |
| 4140 |
check = (png_uint_32)row_stride; |
| 4141 |
|
| 4142 |
|
| 4143 |
|
| 4144 |
|
| 4145 |
|
| 4146 |
if (image->opaque != NULL && buffer != NULL && check >= png_row_stride) |
| 4147 |
{ |
| 4148 |
|
| 4149 |
|
| 4150 |
|
| 4151 |
|
| 4152 |
|
| 4153 |
|
| 4154 |
|
| 4155 |
|
| 4156 |
|
| 4157 |
|
| 4158 |
|
| 4159 |
|
| 4160 |
|
| 4161 |
|
| 4162 |
|
| 4163 |
|
| 4164 |
if (image->height <= |
| 4165 |
0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check) |
| 4166 |
{ |
| 4167 |
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || |
| 4168 |
(image->colormap_entries > 0 && colormap != NULL)) |
| 4169 |
{ |
| 4170 |
int result; |
| 4171 |
png_image_read_control display; |
| 4172 |
|
| 4173 |
memset(&display, 0, (sizeof display)); |
| 4174 |
display.image = image; |
| 4175 |
display.buffer = buffer; |
| 4176 |
display.row_stride = row_stride; |
| 4177 |
display.colormap = colormap; |
| 4178 |
display.background = background; |
| 4179 |
display.local_row = NULL; |
| 4180 |
|
| 4181 |
|
| 4182 |
|
| 4183 |
|
| 4184 |
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) |
| 4185 |
result = |
| 4186 |
png_safe_execute(image, |
| 4187 |
png_image_read_colormap, &display) && |
| 4188 |
png_safe_execute(image, |
| 4189 |
png_image_read_colormapped, &display); |
| 4190 |
|
| 4191 |
else |
| 4192 |
result = |
| 4193 |
png_safe_execute(image, |
| 4194 |
png_image_read_direct, &display); |
| 4195 |
|
| 4196 |
png_image_free(image); |
| 4197 |
return result; |
| 4198 |
} |
| 4199 |
|
| 4200 |
else |
| 4201 |
return png_image_error(image, |
| 4202 |
"png_image_finish_read[color-map]: no color-map"); |
| 4203 |
} |
| 4204 |
|
| 4205 |
else |
| 4206 |
return png_image_error(image, |
| 4207 |
"png_image_finish_read: image too large"); |
| 4208 |
} |
| 4209 |
|
| 4210 |
else |
| 4211 |
return png_image_error(image, |
| 4212 |
"png_image_finish_read: invalid argument"); |
| 4213 |
} |
| 4214 |
|
| 4215 |
else |
| 4216 |
return png_image_error(image, |
| 4217 |
"png_image_finish_read: row_stride too large"); |
| 4218 |
} |
| 4219 |
|
| 4220 |
else if (image != NULL) |
| 4221 |
return png_image_error(image, |
| 4222 |
"png_image_finish_read: damaged PNG_IMAGE_VERSION"); |
| 4223 |
|
| 4224 |
return 0; |
| 4225 |
} |
| 4226 |
|
| 4227 |
#endif |
| 4228 |
#endif |