| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "pngpriv.h" |
| 20 |
|
| 21 |
#ifdef PNG_ARM_NEON_IMPLEMENTATION |
| 22 |
# if PNG_ARM_NEON_IMPLEMENTATION == 1 |
| 23 |
# define PNG_ARM_NEON_INTRINSICS_AVAILABLE |
| 24 |
# if defined(_MSC_VER) && !defined(__clang__) && defined(_M_ARM64) |
| 25 |
# include <arm64_neon.h> |
| 26 |
# else |
| 27 |
# include <arm_neon.h> |
| 28 |
# endif |
| 29 |
# endif |
| 30 |
#endif |
| 31 |
|
| 32 |
#ifdef PNG_READ_SUPPORTED |
| 33 |
|
| 34 |
|
| 35 |
void PNGAPI |
| 36 |
png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) |
| 37 |
{ |
| 38 |
png_debug(1, "in png_set_crc_action"); |
| 39 |
|
| 40 |
if (png_ptr == NULL) |
| 41 |
return; |
| 42 |
|
| 43 |
|
| 44 |
switch (crit_action) |
| 45 |
{ |
| 46 |
case PNG_CRC_NO_CHANGE: |
| 47 |
break; |
| 48 |
|
| 49 |
case PNG_CRC_WARN_USE: |
| 50 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 51 |
png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; |
| 52 |
break; |
| 53 |
|
| 54 |
case PNG_CRC_QUIET_USE: |
| 55 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 56 |
png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | |
| 57 |
PNG_FLAG_CRC_CRITICAL_IGNORE; |
| 58 |
break; |
| 59 |
|
| 60 |
case PNG_CRC_WARN_DISCARD: |
| 61 |
png_warning(png_ptr, |
| 62 |
"Can't discard critical data on CRC error"); |
| 63 |
|
| 64 |
case PNG_CRC_ERROR_QUIT: |
| 65 |
|
| 66 |
case PNG_CRC_DEFAULT: |
| 67 |
default: |
| 68 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
| 69 |
break; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
switch (ancil_action) |
| 74 |
{ |
| 75 |
case PNG_CRC_NO_CHANGE: |
| 76 |
break; |
| 77 |
|
| 78 |
case PNG_CRC_WARN_USE: |
| 79 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 80 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; |
| 81 |
break; |
| 82 |
|
| 83 |
case PNG_CRC_QUIET_USE: |
| 84 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 85 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | |
| 86 |
PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 87 |
break; |
| 88 |
|
| 89 |
case PNG_CRC_ERROR_QUIT: |
| 90 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 91 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; |
| 92 |
break; |
| 93 |
|
| 94 |
case PNG_CRC_WARN_DISCARD: |
| 95 |
|
| 96 |
case PNG_CRC_DEFAULT: |
| 97 |
default: |
| 98 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
#ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static int |
| 110 |
png_rtran_ok(png_structrp png_ptr, int need_IHDR) |
| 111 |
{ |
| 112 |
if (png_ptr != NULL) |
| 113 |
{ |
| 114 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) |
| 115 |
png_app_error(png_ptr, |
| 116 |
"invalid after png_start_read_image or png_read_update_info"); |
| 117 |
|
| 118 |
else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) |
| 119 |
png_app_error(png_ptr, "invalid before the PNG header has been read"); |
| 120 |
|
| 121 |
else |
| 122 |
{ |
| 123 |
|
| 124 |
png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; |
| 125 |
|
| 126 |
return 1; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 135 |
|
| 136 |
void PNGFAPI |
| 137 |
png_set_background_fixed(png_structrp png_ptr, |
| 138 |
png_const_color_16p background_color, int background_gamma_code, |
| 139 |
int need_expand, png_fixed_point background_gamma) |
| 140 |
{ |
| 141 |
png_debug(1, "in png_set_background_fixed"); |
| 142 |
|
| 143 |
if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL) |
| 144 |
return; |
| 145 |
|
| 146 |
if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) |
| 147 |
{ |
| 148 |
png_warning(png_ptr, "Application must supply a known background gamma"); |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; |
| 153 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 154 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 155 |
|
| 156 |
png_ptr->background = *background_color; |
| 157 |
png_ptr->background_gamma = background_gamma; |
| 158 |
png_ptr->background_gamma_type = (png_byte)(background_gamma_code); |
| 159 |
if (need_expand != 0) |
| 160 |
png_ptr->transformations |= PNG_BACKGROUND_EXPAND; |
| 161 |
else |
| 162 |
png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
| 163 |
} |
| 164 |
|
| 165 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
| 166 |
void PNGAPI |
| 167 |
png_set_background(png_structrp png_ptr, |
| 168 |
png_const_color_16p background_color, int background_gamma_code, |
| 169 |
int need_expand, double background_gamma) |
| 170 |
{ |
| 171 |
png_set_background_fixed(png_ptr, background_color, background_gamma_code, |
| 172 |
need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); |
| 173 |
} |
| 174 |
# endif |
| 175 |
#endif |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 182 |
void PNGAPI |
| 183 |
png_set_scale_16(png_structrp png_ptr) |
| 184 |
{ |
| 185 |
png_debug(1, "in png_set_scale_16"); |
| 186 |
|
| 187 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 188 |
return; |
| 189 |
|
| 190 |
png_ptr->transformations |= PNG_SCALE_16_TO_8; |
| 191 |
} |
| 192 |
#endif |
| 193 |
|
| 194 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 195 |
|
| 196 |
void PNGAPI |
| 197 |
png_set_strip_16(png_structrp png_ptr) |
| 198 |
{ |
| 199 |
png_debug(1, "in png_set_strip_16"); |
| 200 |
|
| 201 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 202 |
return; |
| 203 |
|
| 204 |
png_ptr->transformations |= PNG_16_TO_8; |
| 205 |
} |
| 206 |
#endif |
| 207 |
|
| 208 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 209 |
void PNGAPI |
| 210 |
png_set_strip_alpha(png_structrp png_ptr) |
| 211 |
{ |
| 212 |
png_debug(1, "in png_set_strip_alpha"); |
| 213 |
|
| 214 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 215 |
return; |
| 216 |
|
| 217 |
png_ptr->transformations |= PNG_STRIP_ALPHA; |
| 218 |
} |
| 219 |
#endif |
| 220 |
|
| 221 |
#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) |
| 222 |
static png_fixed_point |
| 223 |
translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, |
| 224 |
int is_screen) |
| 225 |
{ |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (output_gamma == PNG_DEFAULT_sRGB || |
| 232 |
output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) |
| 233 |
{ |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
# ifdef PNG_READ_sRGB_SUPPORTED |
| 238 |
png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; |
| 239 |
# else |
| 240 |
PNG_UNUSED(png_ptr) |
| 241 |
# endif |
| 242 |
if (is_screen != 0) |
| 243 |
output_gamma = PNG_GAMMA_sRGB; |
| 244 |
else |
| 245 |
output_gamma = PNG_GAMMA_sRGB_INVERSE; |
| 246 |
} |
| 247 |
|
| 248 |
else if (output_gamma == PNG_GAMMA_MAC_18 || |
| 249 |
output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) |
| 250 |
{ |
| 251 |
if (is_screen != 0) |
| 252 |
output_gamma = PNG_GAMMA_MAC_OLD; |
| 253 |
else |
| 254 |
output_gamma = PNG_GAMMA_MAC_INVERSE; |
| 255 |
} |
| 256 |
|
| 257 |
return output_gamma; |
| 258 |
} |
| 259 |
|
| 260 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
| 261 |
static png_fixed_point |
| 262 |
convert_gamma_value(png_structrp png_ptr, double output_gamma) |
| 263 |
{ |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
if (output_gamma > 0 && output_gamma < 128) |
| 272 |
output_gamma *= PNG_FP_1; |
| 273 |
|
| 274 |
|
| 275 |
output_gamma = floor(output_gamma + .5); |
| 276 |
|
| 277 |
if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN) |
| 278 |
png_fixed_error(png_ptr, "gamma value"); |
| 279 |
|
| 280 |
return (png_fixed_point)output_gamma; |
| 281 |
} |
| 282 |
# endif |
| 283 |
#endif |
| 284 |
|
| 285 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 286 |
void PNGFAPI |
| 287 |
png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, |
| 288 |
png_fixed_point output_gamma) |
| 289 |
{ |
| 290 |
int compose = 0; |
| 291 |
png_fixed_point file_gamma; |
| 292 |
|
| 293 |
png_debug(1, "in png_set_alpha_mode_fixed"); |
| 294 |
|
| 295 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 296 |
return; |
| 297 |
|
| 298 |
output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1); |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (output_gamma < 1000 || output_gamma > 10000000) |
| 309 |
png_error(png_ptr, "output gamma out of expected range"); |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
file_gamma = png_reciprocal(output_gamma); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
switch (mode) |
| 333 |
{ |
| 334 |
case PNG_ALPHA_PNG: |
| 335 |
|
| 336 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 337 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 338 |
break; |
| 339 |
|
| 340 |
case PNG_ALPHA_ASSOCIATED: |
| 341 |
compose = 1; |
| 342 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 343 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 344 |
|
| 345 |
output_gamma = PNG_FP_1; |
| 346 |
break; |
| 347 |
|
| 348 |
case PNG_ALPHA_OPTIMIZED: |
| 349 |
compose = 1; |
| 350 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 351 |
png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA; |
| 352 |
|
| 353 |
break; |
| 354 |
|
| 355 |
case PNG_ALPHA_BROKEN: |
| 356 |
compose = 1; |
| 357 |
png_ptr->transformations |= PNG_ENCODE_ALPHA; |
| 358 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 359 |
break; |
| 360 |
|
| 361 |
default: |
| 362 |
png_error(png_ptr, "invalid alpha mode"); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
if (png_ptr->colorspace.gamma == 0) |
| 370 |
{ |
| 371 |
png_ptr->colorspace.gamma = file_gamma; |
| 372 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
png_ptr->screen_gamma = output_gamma; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
if (compose != 0) |
| 382 |
{ |
| 383 |
|
| 384 |
memset(&png_ptr->background, 0, (sizeof png_ptr->background)); |
| 385 |
png_ptr->background_gamma = png_ptr->colorspace.gamma; |
| 386 |
png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; |
| 387 |
png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
| 388 |
|
| 389 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 390 |
png_error(png_ptr, |
| 391 |
"conflicting calls to set alpha mode and background"); |
| 392 |
|
| 393 |
png_ptr->transformations |= PNG_COMPOSE; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
| 398 |
void PNGAPI |
| 399 |
png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) |
| 400 |
{ |
| 401 |
png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, |
| 402 |
output_gamma)); |
| 403 |
} |
| 404 |
# endif |
| 405 |
#endif |
| 406 |
|
| 407 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
typedef struct png_dsort_struct |
| 418 |
{ |
| 419 |
struct png_dsort_struct * next; |
| 420 |
png_byte left; |
| 421 |
png_byte right; |
| 422 |
} png_dsort; |
| 423 |
typedef png_dsort * png_dsortp; |
| 424 |
typedef png_dsort * * png_dsortpp; |
| 425 |
|
| 426 |
void PNGAPI |
| 427 |
png_set_quantize(png_structrp png_ptr, png_colorp palette, |
| 428 |
int num_palette, int maximum_colors, png_const_uint_16p histogram, |
| 429 |
int full_quantize) |
| 430 |
{ |
| 431 |
png_debug(1, "in png_set_quantize"); |
| 432 |
|
| 433 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 434 |
return; |
| 435 |
|
| 436 |
png_ptr->transformations |= PNG_QUANTIZE; |
| 437 |
|
| 438 |
if (full_quantize == 0) |
| 439 |
{ |
| 440 |
int i; |
| 441 |
|
| 442 |
png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, |
| 443 |
(png_alloc_size_t)num_palette); |
| 444 |
for (i = 0; i < num_palette; i++) |
| 445 |
png_ptr->quantize_index[i] = (png_byte)i; |
| 446 |
} |
| 447 |
|
| 448 |
if (num_palette > maximum_colors) |
| 449 |
{ |
| 450 |
if (histogram != NULL) |
| 451 |
{ |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
int i; |
| 457 |
|
| 458 |
|
| 459 |
png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, |
| 460 |
(png_alloc_size_t)num_palette); |
| 461 |
|
| 462 |
|
| 463 |
for (i = 0; i < num_palette; i++) |
| 464 |
png_ptr->quantize_sort[i] = (png_byte)i; |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
for (i = num_palette - 1; i >= maximum_colors; i--) |
| 474 |
{ |
| 475 |
int done; |
| 476 |
int j; |
| 477 |
|
| 478 |
done = 1; |
| 479 |
for (j = 0; j < i; j++) |
| 480 |
{ |
| 481 |
if (histogram[png_ptr->quantize_sort[j]] |
| 482 |
< histogram[png_ptr->quantize_sort[j + 1]]) |
| 483 |
{ |
| 484 |
png_byte t; |
| 485 |
|
| 486 |
t = png_ptr->quantize_sort[j]; |
| 487 |
png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; |
| 488 |
png_ptr->quantize_sort[j + 1] = t; |
| 489 |
done = 0; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
if (done != 0) |
| 494 |
break; |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
if (full_quantize != 0) |
| 499 |
{ |
| 500 |
int j = num_palette; |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
for (i = 0; i < maximum_colors; i++) |
| 506 |
{ |
| 507 |
if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
| 508 |
{ |
| 509 |
do |
| 510 |
j--; |
| 511 |
while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
| 512 |
|
| 513 |
palette[i] = palette[j]; |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
else |
| 518 |
{ |
| 519 |
int j = num_palette; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
for (i = 0; i < maximum_colors; i++) |
| 525 |
{ |
| 526 |
|
| 527 |
if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
| 528 |
{ |
| 529 |
png_color tmp_color; |
| 530 |
|
| 531 |
do |
| 532 |
j--; |
| 533 |
while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
| 534 |
|
| 535 |
tmp_color = palette[j]; |
| 536 |
palette[j] = palette[i]; |
| 537 |
palette[i] = tmp_color; |
| 538 |
|
| 539 |
png_ptr->quantize_index[j] = (png_byte)i; |
| 540 |
png_ptr->quantize_index[i] = (png_byte)j; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
for (i = 0; i < num_palette; i++) |
| 546 |
{ |
| 547 |
if ((int)png_ptr->quantize_index[i] >= maximum_colors) |
| 548 |
{ |
| 549 |
int min_d, k, min_k, d_index; |
| 550 |
|
| 551 |
|
| 552 |
d_index = png_ptr->quantize_index[i]; |
| 553 |
min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); |
| 554 |
for (k = 1, min_k = 0; k < maximum_colors; k++) |
| 555 |
{ |
| 556 |
int d; |
| 557 |
|
| 558 |
d = PNG_COLOR_DIST(palette[d_index], palette[k]); |
| 559 |
|
| 560 |
if (d < min_d) |
| 561 |
{ |
| 562 |
min_d = d; |
| 563 |
min_k = k; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
png_ptr->quantize_index[i] = (png_byte)min_k; |
| 568 |
} |
| 569 |
} |
| 570 |
} |
| 571 |
png_free(png_ptr, png_ptr->quantize_sort); |
| 572 |
png_ptr->quantize_sort = NULL; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
int i; |
| 585 |
int max_d; |
| 586 |
int num_new_palette; |
| 587 |
png_dsortp t; |
| 588 |
png_dsortpp hash; |
| 589 |
|
| 590 |
t = NULL; |
| 591 |
|
| 592 |
|
| 593 |
png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, |
| 594 |
(png_alloc_size_t)num_palette); |
| 595 |
png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, |
| 596 |
(png_alloc_size_t)num_palette); |
| 597 |
|
| 598 |
|
| 599 |
for (i = 0; i < num_palette; i++) |
| 600 |
{ |
| 601 |
png_ptr->index_to_palette[i] = (png_byte)i; |
| 602 |
png_ptr->palette_to_index[i] = (png_byte)i; |
| 603 |
} |
| 604 |
|
| 605 |
hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 * |
| 606 |
(sizeof (png_dsortp)))); |
| 607 |
|
| 608 |
num_new_palette = num_palette; |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
max_d = 96; |
| 619 |
|
| 620 |
while (num_new_palette > maximum_colors) |
| 621 |
{ |
| 622 |
for (i = 0; i < num_new_palette - 1; i++) |
| 623 |
{ |
| 624 |
int j; |
| 625 |
|
| 626 |
for (j = i + 1; j < num_new_palette; j++) |
| 627 |
{ |
| 628 |
int d; |
| 629 |
|
| 630 |
d = PNG_COLOR_DIST(palette[i], palette[j]); |
| 631 |
|
| 632 |
if (d <= max_d) |
| 633 |
{ |
| 634 |
|
| 635 |
t = (png_dsortp)png_malloc_warn(png_ptr, |
| 636 |
(png_alloc_size_t)(sizeof (png_dsort))); |
| 637 |
|
| 638 |
if (t == NULL) |
| 639 |
break; |
| 640 |
|
| 641 |
t->next = hash[d]; |
| 642 |
t->left = (png_byte)i; |
| 643 |
t->right = (png_byte)j; |
| 644 |
hash[d] = t; |
| 645 |
} |
| 646 |
} |
| 647 |
if (t == NULL) |
| 648 |
break; |
| 649 |
} |
| 650 |
|
| 651 |
if (t != NULL) |
| 652 |
for (i = 0; i <= max_d; i++) |
| 653 |
{ |
| 654 |
if (hash[i] != NULL) |
| 655 |
{ |
| 656 |
png_dsortp p; |
| 657 |
|
| 658 |
for (p = hash[i]; p; p = p->next) |
| 659 |
{ |
| 660 |
if ((int)png_ptr->index_to_palette[p->left] |
| 661 |
< num_new_palette && |
| 662 |
(int)png_ptr->index_to_palette[p->right] |
| 663 |
< num_new_palette) |
| 664 |
{ |
| 665 |
int j, next_j; |
| 666 |
|
| 667 |
if (num_new_palette & 0x01) |
| 668 |
{ |
| 669 |
j = p->left; |
| 670 |
next_j = p->right; |
| 671 |
} |
| 672 |
else |
| 673 |
{ |
| 674 |
j = p->right; |
| 675 |
next_j = p->left; |
| 676 |
} |
| 677 |
|
| 678 |
num_new_palette--; |
| 679 |
palette[png_ptr->index_to_palette[j]] |
| 680 |
= palette[num_new_palette]; |
| 681 |
if (full_quantize == 0) |
| 682 |
{ |
| 683 |
int k; |
| 684 |
|
| 685 |
for (k = 0; k < num_palette; k++) |
| 686 |
{ |
| 687 |
if (png_ptr->quantize_index[k] == |
| 688 |
png_ptr->index_to_palette[j]) |
| 689 |
png_ptr->quantize_index[k] = |
| 690 |
png_ptr->index_to_palette[next_j]; |
| 691 |
|
| 692 |
if ((int)png_ptr->quantize_index[k] == |
| 693 |
num_new_palette) |
| 694 |
png_ptr->quantize_index[k] = |
| 695 |
png_ptr->index_to_palette[j]; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
png_ptr->index_to_palette[png_ptr->palette_to_index |
| 700 |
[num_new_palette]] = png_ptr->index_to_palette[j]; |
| 701 |
|
| 702 |
png_ptr->palette_to_index[png_ptr->index_to_palette[j]] |
| 703 |
= png_ptr->palette_to_index[num_new_palette]; |
| 704 |
|
| 705 |
png_ptr->index_to_palette[j] = |
| 706 |
(png_byte)num_new_palette; |
| 707 |
|
| 708 |
png_ptr->palette_to_index[num_new_palette] = |
| 709 |
(png_byte)j; |
| 710 |
} |
| 711 |
if (num_new_palette <= maximum_colors) |
| 712 |
break; |
| 713 |
} |
| 714 |
if (num_new_palette <= maximum_colors) |
| 715 |
break; |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
for (i = 0; i < 769; i++) |
| 720 |
{ |
| 721 |
if (hash[i] != NULL) |
| 722 |
{ |
| 723 |
png_dsortp p = hash[i]; |
| 724 |
while (p) |
| 725 |
{ |
| 726 |
t = p->next; |
| 727 |
png_free(png_ptr, p); |
| 728 |
p = t; |
| 729 |
} |
| 730 |
} |
| 731 |
hash[i] = 0; |
| 732 |
} |
| 733 |
max_d += 96; |
| 734 |
} |
| 735 |
png_free(png_ptr, hash); |
| 736 |
png_free(png_ptr, png_ptr->palette_to_index); |
| 737 |
png_free(png_ptr, png_ptr->index_to_palette); |
| 738 |
png_ptr->palette_to_index = NULL; |
| 739 |
png_ptr->index_to_palette = NULL; |
| 740 |
} |
| 741 |
num_palette = maximum_colors; |
| 742 |
} |
| 743 |
if (png_ptr->palette == NULL) |
| 744 |
{ |
| 745 |
png_ptr->palette = palette; |
| 746 |
} |
| 747 |
png_ptr->num_palette = (png_uint_16)num_palette; |
| 748 |
|
| 749 |
if (full_quantize != 0) |
| 750 |
{ |
| 751 |
int i; |
| 752 |
png_bytep distance; |
| 753 |
int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + |
| 754 |
PNG_QUANTIZE_BLUE_BITS; |
| 755 |
int num_red = (1 << PNG_QUANTIZE_RED_BITS); |
| 756 |
int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); |
| 757 |
int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); |
| 758 |
size_t num_entries = ((size_t)1 << total_bits); |
| 759 |
|
| 760 |
png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, |
| 761 |
(png_alloc_size_t)(num_entries)); |
| 762 |
|
| 763 |
distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)num_entries); |
| 764 |
|
| 765 |
memset(distance, 0xff, num_entries); |
| 766 |
|
| 767 |
for (i = 0; i < num_palette; i++) |
| 768 |
{ |
| 769 |
int ir, ig, ib; |
| 770 |
int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS)); |
| 771 |
int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS)); |
| 772 |
int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS)); |
| 773 |
|
| 774 |
for (ir = 0; ir < num_red; ir++) |
| 775 |
{ |
| 776 |
|
| 777 |
int dr = ((ir > r) ? ir - r : r - ir); |
| 778 |
int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS + |
| 779 |
PNG_QUANTIZE_GREEN_BITS)); |
| 780 |
|
| 781 |
for (ig = 0; ig < num_green; ig++) |
| 782 |
{ |
| 783 |
|
| 784 |
int dg = ((ig > g) ? ig - g : g - ig); |
| 785 |
int dt = dr + dg; |
| 786 |
int dm = ((dr > dg) ? dr : dg); |
| 787 |
int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS); |
| 788 |
|
| 789 |
for (ib = 0; ib < num_blue; ib++) |
| 790 |
{ |
| 791 |
int d_index = index_g | ib; |
| 792 |
|
| 793 |
int db = ((ib > b) ? ib - b : b - ib); |
| 794 |
int dmax = ((dm > db) ? dm : db); |
| 795 |
int d = dmax + dt + db; |
| 796 |
|
| 797 |
if (d < (int)distance[d_index]) |
| 798 |
{ |
| 799 |
distance[d_index] = (png_byte)d; |
| 800 |
png_ptr->palette_lookup[d_index] = (png_byte)i; |
| 801 |
} |
| 802 |
} |
| 803 |
} |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
png_free(png_ptr, distance); |
| 808 |
} |
| 809 |
} |
| 810 |
#endif |
| 811 |
|
| 812 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 813 |
void PNGFAPI |
| 814 |
png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, |
| 815 |
png_fixed_point file_gamma) |
| 816 |
{ |
| 817 |
png_debug(1, "in png_set_gamma_fixed"); |
| 818 |
|
| 819 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 820 |
return; |
| 821 |
|
| 822 |
|
| 823 |
scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1); |
| 824 |
file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0); |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
if (file_gamma <= 0) |
| 838 |
png_error(png_ptr, "invalid file gamma in png_set_gamma"); |
| 839 |
|
| 840 |
if (scrn_gamma <= 0) |
| 841 |
png_error(png_ptr, "invalid screen gamma in png_set_gamma"); |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
png_ptr->colorspace.gamma = file_gamma; |
| 848 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 849 |
png_ptr->screen_gamma = scrn_gamma; |
| 850 |
} |
| 851 |
|
| 852 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
| 853 |
void PNGAPI |
| 854 |
png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) |
| 855 |
{ |
| 856 |
png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), |
| 857 |
convert_gamma_value(png_ptr, file_gamma)); |
| 858 |
} |
| 859 |
# endif |
| 860 |
#endif |
| 861 |
|
| 862 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
void PNGAPI |
| 868 |
png_set_expand(png_structrp png_ptr) |
| 869 |
{ |
| 870 |
png_debug(1, "in png_set_expand"); |
| 871 |
|
| 872 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 873 |
return; |
| 874 |
|
| 875 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
void PNGAPI |
| 898 |
png_set_palette_to_rgb(png_structrp png_ptr) |
| 899 |
{ |
| 900 |
png_debug(1, "in png_set_palette_to_rgb"); |
| 901 |
|
| 902 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 903 |
return; |
| 904 |
|
| 905 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
void PNGAPI |
| 910 |
png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) |
| 911 |
{ |
| 912 |
png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); |
| 913 |
|
| 914 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 915 |
return; |
| 916 |
|
| 917 |
png_ptr->transformations |= PNG_EXPAND; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
void PNGAPI |
| 922 |
png_set_tRNS_to_alpha(png_structrp png_ptr) |
| 923 |
{ |
| 924 |
png_debug(1, "in png_set_tRNS_to_alpha"); |
| 925 |
|
| 926 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 927 |
return; |
| 928 |
|
| 929 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
| 930 |
} |
| 931 |
#endif |
| 932 |
|
| 933 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
void PNGAPI |
| 938 |
png_set_expand_16(png_structrp png_ptr) |
| 939 |
{ |
| 940 |
png_debug(1, "in png_set_expand_16"); |
| 941 |
|
| 942 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 943 |
return; |
| 944 |
|
| 945 |
png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); |
| 946 |
} |
| 947 |
#endif |
| 948 |
|
| 949 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 950 |
void PNGAPI |
| 951 |
png_set_gray_to_rgb(png_structrp png_ptr) |
| 952 |
{ |
| 953 |
png_debug(1, "in png_set_gray_to_rgb"); |
| 954 |
|
| 955 |
if (png_rtran_ok(png_ptr, 0) == 0) |
| 956 |
return; |
| 957 |
|
| 958 |
|
| 959 |
png_set_expand_gray_1_2_4_to_8(png_ptr); |
| 960 |
png_ptr->transformations |= PNG_GRAY_TO_RGB; |
| 961 |
} |
| 962 |
#endif |
| 963 |
|
| 964 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 965 |
void PNGFAPI |
| 966 |
png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, |
| 967 |
png_fixed_point red, png_fixed_point green) |
| 968 |
{ |
| 969 |
png_debug(1, "in png_set_rgb_to_gray_fixed"); |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
if (png_rtran_ok(png_ptr, 1) == 0) |
| 974 |
return; |
| 975 |
|
| 976 |
switch (error_action) |
| 977 |
{ |
| 978 |
case PNG_ERROR_ACTION_NONE: |
| 979 |
png_ptr->transformations |= PNG_RGB_TO_GRAY; |
| 980 |
break; |
| 981 |
|
| 982 |
case PNG_ERROR_ACTION_WARN: |
| 983 |
png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; |
| 984 |
break; |
| 985 |
|
| 986 |
case PNG_ERROR_ACTION_ERROR: |
| 987 |
png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; |
| 988 |
break; |
| 989 |
|
| 990 |
default: |
| 991 |
png_error(png_ptr, "invalid error action to rgb_to_gray"); |
| 992 |
} |
| 993 |
|
| 994 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 995 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 996 |
png_ptr->transformations |= PNG_EXPAND; |
| 997 |
#else |
| 998 |
{ |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
png_error(png_ptr, |
| 1003 |
"Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); |
| 1004 |
|
| 1005 |
|
| 1006 |
} |
| 1007 |
#endif |
| 1008 |
{ |
| 1009 |
if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) |
| 1010 |
{ |
| 1011 |
png_uint_16 red_int, green_int; |
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); |
| 1019 |
green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); |
| 1020 |
|
| 1021 |
png_ptr->rgb_to_gray_red_coeff = red_int; |
| 1022 |
png_ptr->rgb_to_gray_green_coeff = green_int; |
| 1023 |
png_ptr->rgb_to_gray_coefficients_set = 1; |
| 1024 |
} |
| 1025 |
|
| 1026 |
else |
| 1027 |
{ |
| 1028 |
if (red >= 0 && green >= 0) |
| 1029 |
png_app_warning(png_ptr, |
| 1030 |
"ignoring out of range rgb_to_gray coefficients"); |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
if (png_ptr->rgb_to_gray_red_coeff == 0 && |
| 1039 |
png_ptr->rgb_to_gray_green_coeff == 0) |
| 1040 |
{ |
| 1041 |
png_ptr->rgb_to_gray_red_coeff = 6968; |
| 1042 |
png_ptr->rgb_to_gray_green_coeff = 23434; |
| 1043 |
|
| 1044 |
} |
| 1045 |
} |
| 1046 |
} |
| 1047 |
} |
| 1048 |
|
| 1049 |
#ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
void PNGAPI |
| 1055 |
png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, |
| 1056 |
double green) |
| 1057 |
{ |
| 1058 |
png_set_rgb_to_gray_fixed(png_ptr, error_action, |
| 1059 |
png_fixed(png_ptr, red, "rgb to gray red coefficient"), |
| 1060 |
png_fixed(png_ptr, green, "rgb to gray green coefficient")); |
| 1061 |
} |
| 1062 |
#endif |
| 1063 |
|
| 1064 |
#endif |
| 1065 |
|
| 1066 |
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ |
| 1067 |
defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
| 1068 |
void PNGAPI |
| 1069 |
png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr |
| 1070 |
read_user_transform_fn) |
| 1071 |
{ |
| 1072 |
png_debug(1, "in png_set_read_user_transform_fn"); |
| 1073 |
|
| 1074 |
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
| 1075 |
png_ptr->transformations |= PNG_USER_TRANSFORM; |
| 1076 |
png_ptr->read_user_transform_fn = read_user_transform_fn; |
| 1077 |
#endif |
| 1078 |
} |
| 1079 |
#endif |
| 1080 |
|
| 1081 |
#ifdef PNG_READ_TRANSFORMS_SUPPORTED |
| 1082 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
static int |
| 1088 |
png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) |
| 1089 |
{ |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
png_fixed_point gtest; |
| 1098 |
return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) || |
| 1099 |
png_gamma_significant(gtest); |
| 1100 |
} |
| 1101 |
#endif |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
static void |
| 1113 |
png_init_palette_transformations(png_structrp png_ptr) |
| 1114 |
{ |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
int input_has_alpha = 0; |
| 1124 |
int input_has_transparency = 0; |
| 1125 |
|
| 1126 |
if (png_ptr->num_trans > 0) |
| 1127 |
{ |
| 1128 |
int i; |
| 1129 |
|
| 1130 |
|
| 1131 |
for (i=0; i<png_ptr->num_trans; ++i) |
| 1132 |
{ |
| 1133 |
if (png_ptr->trans_alpha[i] == 255) |
| 1134 |
continue; |
| 1135 |
else if (png_ptr->trans_alpha[i] == 0) |
| 1136 |
input_has_transparency = 1; |
| 1137 |
else |
| 1138 |
{ |
| 1139 |
input_has_transparency = 1; |
| 1140 |
input_has_alpha = 1; |
| 1141 |
break; |
| 1142 |
} |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
if (input_has_alpha == 0) |
| 1148 |
{ |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1154 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1155 |
|
| 1156 |
if (input_has_transparency == 0) |
| 1157 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
| 1158 |
} |
| 1159 |
|
| 1160 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
| 1170 |
(png_ptr->transformations & PNG_EXPAND) != 0) |
| 1171 |
{ |
| 1172 |
{ |
| 1173 |
png_ptr->background.red = |
| 1174 |
png_ptr->palette[png_ptr->background.index].red; |
| 1175 |
png_ptr->background.green = |
| 1176 |
png_ptr->palette[png_ptr->background.index].green; |
| 1177 |
png_ptr->background.blue = |
| 1178 |
png_ptr->palette[png_ptr->background.index].blue; |
| 1179 |
|
| 1180 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
| 1181 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
| 1182 |
{ |
| 1183 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
| 1184 |
{ |
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
int i, istop = png_ptr->num_trans; |
| 1189 |
|
| 1190 |
for (i = 0; i < istop; i++) |
| 1191 |
png_ptr->trans_alpha[i] = |
| 1192 |
(png_byte)(255 - png_ptr->trans_alpha[i]); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
#endif |
| 1196 |
} |
| 1197 |
} |
| 1198 |
#endif |
| 1199 |
} |
| 1200 |
|
| 1201 |
static void |
| 1202 |
png_init_rgb_transformations(png_structrp png_ptr) |
| 1203 |
{ |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
| 1209 |
int input_has_transparency = png_ptr->num_trans > 0; |
| 1210 |
|
| 1211 |
|
| 1212 |
if (input_has_alpha == 0) |
| 1213 |
{ |
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
# ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 1219 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1220 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1221 |
# endif |
| 1222 |
|
| 1223 |
if (input_has_transparency == 0) |
| 1224 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
| 1225 |
} |
| 1226 |
|
| 1227 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
| 1237 |
(png_ptr->transformations & PNG_EXPAND) != 0 && |
| 1238 |
(png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
| 1239 |
|
| 1240 |
{ |
| 1241 |
{ |
| 1242 |
|
| 1243 |
int gray = png_ptr->background.gray; |
| 1244 |
int trans_gray = png_ptr->trans_color.gray; |
| 1245 |
|
| 1246 |
switch (png_ptr->bit_depth) |
| 1247 |
{ |
| 1248 |
case 1: |
| 1249 |
gray *= 0xff; |
| 1250 |
trans_gray *= 0xff; |
| 1251 |
break; |
| 1252 |
|
| 1253 |
case 2: |
| 1254 |
gray *= 0x55; |
| 1255 |
trans_gray *= 0x55; |
| 1256 |
break; |
| 1257 |
|
| 1258 |
case 4: |
| 1259 |
gray *= 0x11; |
| 1260 |
trans_gray *= 0x11; |
| 1261 |
break; |
| 1262 |
|
| 1263 |
default: |
| 1264 |
|
| 1265 |
case 8: |
| 1266 |
|
| 1267 |
|
| 1268 |
case 16: |
| 1269 |
|
| 1270 |
break; |
| 1271 |
} |
| 1272 |
|
| 1273 |
png_ptr->background.red = png_ptr->background.green = |
| 1274 |
png_ptr->background.blue = (png_uint_16)gray; |
| 1275 |
|
| 1276 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
| 1277 |
{ |
| 1278 |
png_ptr->trans_color.red = png_ptr->trans_color.green = |
| 1279 |
png_ptr->trans_color.blue = (png_uint_16)trans_gray; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
} |
| 1283 |
#endif |
| 1284 |
} |
| 1285 |
|
| 1286 |
void |
| 1287 |
png_init_read_transformations(png_structrp png_ptr) |
| 1288 |
{ |
| 1289 |
png_debug(1, "in png_init_read_transformations"); |
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
{ |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
int gamma_correction = 0; |
| 1311 |
|
| 1312 |
if (png_ptr->colorspace.gamma != 0) |
| 1313 |
{ |
| 1314 |
if (png_ptr->screen_gamma != 0) |
| 1315 |
gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, |
| 1316 |
png_ptr->screen_gamma); |
| 1317 |
|
| 1318 |
else |
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); |
| 1323 |
} |
| 1324 |
|
| 1325 |
else if (png_ptr->screen_gamma != 0) |
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); |
| 1332 |
|
| 1333 |
else |
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; |
| 1340 |
|
| 1341 |
|
| 1342 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
if (gamma_correction != 0) |
| 1353 |
png_ptr->transformations |= PNG_GAMMA; |
| 1354 |
|
| 1355 |
else |
| 1356 |
png_ptr->transformations &= ~PNG_GAMMA; |
| 1357 |
} |
| 1358 |
#endif |
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 1390 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 1391 |
(png_ptr->transformations & PNG_COMPOSE) == 0) |
| 1392 |
{ |
| 1393 |
|
| 1394 |
|
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | |
| 1399 |
PNG_EXPAND_tRNS); |
| 1400 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
png_ptr->num_trans = 0; |
| 1410 |
} |
| 1411 |
#endif |
| 1412 |
|
| 1413 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
if (png_gamma_significant(png_ptr->screen_gamma) == 0) |
| 1418 |
{ |
| 1419 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
| 1420 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
| 1421 |
} |
| 1422 |
#endif |
| 1423 |
|
| 1424 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
| 1429 |
png_colorspace_set_rgb_coefficients(png_ptr); |
| 1430 |
#endif |
| 1431 |
|
| 1432 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 1433 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0) |
| 1450 |
{ |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
| 1455 |
png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
| 1456 |
} |
| 1457 |
|
| 1458 |
else if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 1459 |
{ |
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
| 1466 |
{ |
| 1467 |
if (png_ptr->background.red == png_ptr->background.green && |
| 1468 |
png_ptr->background.red == png_ptr->background.blue) |
| 1469 |
{ |
| 1470 |
png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
| 1471 |
png_ptr->background.gray = png_ptr->background.red; |
| 1472 |
} |
| 1473 |
} |
| 1474 |
} |
| 1475 |
#endif |
| 1476 |
#endif |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1490 |
png_init_palette_transformations(png_ptr); |
| 1491 |
|
| 1492 |
else |
| 1493 |
png_init_rgb_transformations(png_ptr); |
| 1494 |
|
| 1495 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 1496 |
defined(PNG_READ_EXPAND_16_SUPPORTED) |
| 1497 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
| 1498 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1499 |
(png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
| 1500 |
png_ptr->bit_depth != 16) |
| 1501 |
{ |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
# define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) |
| 1513 |
CHOP(png_ptr->background.red); |
| 1514 |
CHOP(png_ptr->background.green); |
| 1515 |
CHOP(png_ptr->background.blue); |
| 1516 |
CHOP(png_ptr->background.gray); |
| 1517 |
# undef CHOP |
| 1518 |
} |
| 1519 |
#endif |
| 1520 |
|
| 1521 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
| 1522 |
(defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ |
| 1523 |
defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) |
| 1524 |
if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 && |
| 1525 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1526 |
(png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
| 1527 |
png_ptr->bit_depth == 16) |
| 1528 |
{ |
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); |
| 1536 |
png_ptr->background.green = |
| 1537 |
(png_uint_16)(png_ptr->background.green * 257); |
| 1538 |
png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257); |
| 1539 |
png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257); |
| 1540 |
} |
| 1541 |
#endif |
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 1552 |
# ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1553 |
|
| 1554 |
png_ptr->background_1 = png_ptr->background; |
| 1555 |
# endif |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
if ((png_ptr->transformations & PNG_GAMMA) != 0 || |
| 1571 |
((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 && |
| 1572 |
(png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
| 1573 |
png_gamma_significant(png_ptr->screen_gamma) != 0)) || |
| 1574 |
((png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1575 |
(png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
| 1576 |
png_gamma_significant(png_ptr->screen_gamma) != 0 |
| 1577 |
# ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1578 |
|| (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE && |
| 1579 |
png_gamma_significant(png_ptr->background_gamma) != 0) |
| 1580 |
# endif |
| 1581 |
)) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
| 1582 |
png_gamma_significant(png_ptr->screen_gamma) != 0)) |
| 1583 |
{ |
| 1584 |
png_build_gamma_table(png_ptr, png_ptr->bit_depth); |
| 1585 |
|
| 1586 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1587 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 1588 |
{ |
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
| 1596 |
png_warning(png_ptr, |
| 1597 |
"libpng does not support gamma+background+rgb_to_gray"); |
| 1598 |
|
| 1599 |
if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0) |
| 1600 |
{ |
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
png_color back, back_1; |
| 1605 |
png_colorp palette = png_ptr->palette; |
| 1606 |
int num_palette = png_ptr->num_palette; |
| 1607 |
int i; |
| 1608 |
if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
| 1609 |
{ |
| 1610 |
|
| 1611 |
back.red = png_ptr->gamma_table[png_ptr->background.red]; |
| 1612 |
back.green = png_ptr->gamma_table[png_ptr->background.green]; |
| 1613 |
back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
| 1614 |
|
| 1615 |
back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
| 1616 |
back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
| 1617 |
back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
| 1618 |
} |
| 1619 |
else |
| 1620 |
{ |
| 1621 |
png_fixed_point g, gs; |
| 1622 |
|
| 1623 |
switch (png_ptr->background_gamma_type) |
| 1624 |
{ |
| 1625 |
case PNG_BACKGROUND_GAMMA_SCREEN: |
| 1626 |
g = (png_ptr->screen_gamma); |
| 1627 |
gs = PNG_FP_1; |
| 1628 |
break; |
| 1629 |
|
| 1630 |
case PNG_BACKGROUND_GAMMA_FILE: |
| 1631 |
g = png_reciprocal(png_ptr->colorspace.gamma); |
| 1632 |
gs = png_reciprocal2(png_ptr->colorspace.gamma, |
| 1633 |
png_ptr->screen_gamma); |
| 1634 |
break; |
| 1635 |
|
| 1636 |
case PNG_BACKGROUND_GAMMA_UNIQUE: |
| 1637 |
g = png_reciprocal(png_ptr->background_gamma); |
| 1638 |
gs = png_reciprocal2(png_ptr->background_gamma, |
| 1639 |
png_ptr->screen_gamma); |
| 1640 |
break; |
| 1641 |
default: |
| 1642 |
g = PNG_FP_1; |
| 1643 |
gs = PNG_FP_1; |
| 1644 |
break; |
| 1645 |
} |
| 1646 |
|
| 1647 |
if (png_gamma_significant(gs) != 0) |
| 1648 |
{ |
| 1649 |
back.red = png_gamma_8bit_correct(png_ptr->background.red, |
| 1650 |
gs); |
| 1651 |
back.green = png_gamma_8bit_correct(png_ptr->background.green, |
| 1652 |
gs); |
| 1653 |
back.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
| 1654 |
gs); |
| 1655 |
} |
| 1656 |
|
| 1657 |
else |
| 1658 |
{ |
| 1659 |
back.red = (png_byte)png_ptr->background.red; |
| 1660 |
back.green = (png_byte)png_ptr->background.green; |
| 1661 |
back.blue = (png_byte)png_ptr->background.blue; |
| 1662 |
} |
| 1663 |
|
| 1664 |
if (png_gamma_significant(g) != 0) |
| 1665 |
{ |
| 1666 |
back_1.red = png_gamma_8bit_correct(png_ptr->background.red, |
| 1667 |
g); |
| 1668 |
back_1.green = png_gamma_8bit_correct( |
| 1669 |
png_ptr->background.green, g); |
| 1670 |
back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
| 1671 |
g); |
| 1672 |
} |
| 1673 |
|
| 1674 |
else |
| 1675 |
{ |
| 1676 |
back_1.red = (png_byte)png_ptr->background.red; |
| 1677 |
back_1.green = (png_byte)png_ptr->background.green; |
| 1678 |
back_1.blue = (png_byte)png_ptr->background.blue; |
| 1679 |
} |
| 1680 |
} |
| 1681 |
|
| 1682 |
for (i = 0; i < num_palette; i++) |
| 1683 |
{ |
| 1684 |
if (i < (int)png_ptr->num_trans && |
| 1685 |
png_ptr->trans_alpha[i] != 0xff) |
| 1686 |
{ |
| 1687 |
if (png_ptr->trans_alpha[i] == 0) |
| 1688 |
{ |
| 1689 |
palette[i] = back; |
| 1690 |
} |
| 1691 |
else |
| 1692 |
{ |
| 1693 |
png_byte v, w; |
| 1694 |
|
| 1695 |
v = png_ptr->gamma_to_1[palette[i].red]; |
| 1696 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.red); |
| 1697 |
palette[i].red = png_ptr->gamma_from_1[w]; |
| 1698 |
|
| 1699 |
v = png_ptr->gamma_to_1[palette[i].green]; |
| 1700 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.green); |
| 1701 |
palette[i].green = png_ptr->gamma_from_1[w]; |
| 1702 |
|
| 1703 |
v = png_ptr->gamma_to_1[palette[i].blue]; |
| 1704 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue); |
| 1705 |
palette[i].blue = png_ptr->gamma_from_1[w]; |
| 1706 |
} |
| 1707 |
} |
| 1708 |
else |
| 1709 |
{ |
| 1710 |
palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 1711 |
palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 1712 |
palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 1713 |
} |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA); |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
else |
| 1727 |
{ |
| 1728 |
int gs_sig, g_sig; |
| 1729 |
png_fixed_point g = PNG_FP_1; |
| 1730 |
png_fixed_point gs = PNG_FP_1; |
| 1731 |
|
| 1732 |
switch (png_ptr->background_gamma_type) |
| 1733 |
{ |
| 1734 |
case PNG_BACKGROUND_GAMMA_SCREEN: |
| 1735 |
g = png_ptr->screen_gamma; |
| 1736 |
|
| 1737 |
break; |
| 1738 |
|
| 1739 |
case PNG_BACKGROUND_GAMMA_FILE: |
| 1740 |
g = png_reciprocal(png_ptr->colorspace.gamma); |
| 1741 |
gs = png_reciprocal2(png_ptr->colorspace.gamma, |
| 1742 |
png_ptr->screen_gamma); |
| 1743 |
break; |
| 1744 |
|
| 1745 |
case PNG_BACKGROUND_GAMMA_UNIQUE: |
| 1746 |
g = png_reciprocal(png_ptr->background_gamma); |
| 1747 |
gs = png_reciprocal2(png_ptr->background_gamma, |
| 1748 |
png_ptr->screen_gamma); |
| 1749 |
break; |
| 1750 |
|
| 1751 |
default: |
| 1752 |
png_error(png_ptr, "invalid background gamma type"); |
| 1753 |
} |
| 1754 |
|
| 1755 |
g_sig = png_gamma_significant(g); |
| 1756 |
gs_sig = png_gamma_significant(gs); |
| 1757 |
|
| 1758 |
if (g_sig != 0) |
| 1759 |
png_ptr->background_1.gray = png_gamma_correct(png_ptr, |
| 1760 |
png_ptr->background.gray, g); |
| 1761 |
|
| 1762 |
if (gs_sig != 0) |
| 1763 |
png_ptr->background.gray = png_gamma_correct(png_ptr, |
| 1764 |
png_ptr->background.gray, gs); |
| 1765 |
|
| 1766 |
if ((png_ptr->background.red != png_ptr->background.green) || |
| 1767 |
(png_ptr->background.red != png_ptr->background.blue) || |
| 1768 |
(png_ptr->background.red != png_ptr->background.gray)) |
| 1769 |
{ |
| 1770 |
|
| 1771 |
if (g_sig != 0) |
| 1772 |
{ |
| 1773 |
png_ptr->background_1.red = png_gamma_correct(png_ptr, |
| 1774 |
png_ptr->background.red, g); |
| 1775 |
|
| 1776 |
png_ptr->background_1.green = png_gamma_correct(png_ptr, |
| 1777 |
png_ptr->background.green, g); |
| 1778 |
|
| 1779 |
png_ptr->background_1.blue = png_gamma_correct(png_ptr, |
| 1780 |
png_ptr->background.blue, g); |
| 1781 |
} |
| 1782 |
|
| 1783 |
if (gs_sig != 0) |
| 1784 |
{ |
| 1785 |
png_ptr->background.red = png_gamma_correct(png_ptr, |
| 1786 |
png_ptr->background.red, gs); |
| 1787 |
|
| 1788 |
png_ptr->background.green = png_gamma_correct(png_ptr, |
| 1789 |
png_ptr->background.green, gs); |
| 1790 |
|
| 1791 |
png_ptr->background.blue = png_gamma_correct(png_ptr, |
| 1792 |
png_ptr->background.blue, gs); |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
else |
| 1797 |
{ |
| 1798 |
|
| 1799 |
png_ptr->background_1.red = png_ptr->background_1.green |
| 1800 |
= png_ptr->background_1.blue = png_ptr->background_1.gray; |
| 1801 |
|
| 1802 |
png_ptr->background.red = png_ptr->background.green |
| 1803 |
= png_ptr->background.blue = png_ptr->background.gray; |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; |
| 1808 |
} |
| 1809 |
} |
| 1810 |
|
| 1811 |
else |
| 1812 |
|
| 1813 |
#endif |
| 1814 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE |
| 1815 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 1816 |
|
| 1817 |
&& ((png_ptr->transformations & PNG_EXPAND) == 0 || |
| 1818 |
(png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
| 1819 |
#endif |
| 1820 |
) |
| 1821 |
{ |
| 1822 |
png_colorp palette = png_ptr->palette; |
| 1823 |
int num_palette = png_ptr->num_palette; |
| 1824 |
int i; |
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
for (i = 0; i < num_palette; i++) |
| 1830 |
{ |
| 1831 |
palette[i].red = png_ptr->gamma_table[palette[i].red]; |
| 1832 |
palette[i].green = png_ptr->gamma_table[palette[i].green]; |
| 1833 |
palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
| 1834 |
} |
| 1835 |
|
| 1836 |
|
| 1837 |
png_ptr->transformations &= ~PNG_GAMMA; |
| 1838 |
} |
| 1839 |
} |
| 1840 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1841 |
else |
| 1842 |
#endif |
| 1843 |
#endif |
| 1844 |
|
| 1845 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
| 1846 |
|
| 1847 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 1848 |
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
| 1849 |
{ |
| 1850 |
int i; |
| 1851 |
int istop = (int)png_ptr->num_trans; |
| 1852 |
png_color back; |
| 1853 |
png_colorp palette = png_ptr->palette; |
| 1854 |
|
| 1855 |
back.red = (png_byte)png_ptr->background.red; |
| 1856 |
back.green = (png_byte)png_ptr->background.green; |
| 1857 |
back.blue = (png_byte)png_ptr->background.blue; |
| 1858 |
|
| 1859 |
for (i = 0; i < istop; i++) |
| 1860 |
{ |
| 1861 |
if (png_ptr->trans_alpha[i] == 0) |
| 1862 |
{ |
| 1863 |
palette[i] = back; |
| 1864 |
} |
| 1865 |
|
| 1866 |
else if (png_ptr->trans_alpha[i] != 0xff) |
| 1867 |
{ |
| 1868 |
|
| 1869 |
png_composite(palette[i].red, palette[i].red, |
| 1870 |
png_ptr->trans_alpha[i], back.red); |
| 1871 |
|
| 1872 |
png_composite(palette[i].green, palette[i].green, |
| 1873 |
png_ptr->trans_alpha[i], back.green); |
| 1874 |
|
| 1875 |
png_composite(palette[i].blue, palette[i].blue, |
| 1876 |
png_ptr->trans_alpha[i], back.blue); |
| 1877 |
} |
| 1878 |
} |
| 1879 |
|
| 1880 |
png_ptr->transformations &= ~PNG_COMPOSE; |
| 1881 |
} |
| 1882 |
#endif |
| 1883 |
|
| 1884 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
| 1885 |
if ((png_ptr->transformations & PNG_SHIFT) != 0 && |
| 1886 |
(png_ptr->transformations & PNG_EXPAND) == 0 && |
| 1887 |
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
| 1888 |
{ |
| 1889 |
int i; |
| 1890 |
int istop = png_ptr->num_palette; |
| 1891 |
int shift = 8 - png_ptr->sig_bit.red; |
| 1892 |
|
| 1893 |
png_ptr->transformations &= ~PNG_SHIFT; |
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
|
| 1899 |
if (shift > 0 && shift < 8) |
| 1900 |
for (i=0; i<istop; ++i) |
| 1901 |
{ |
| 1902 |
int component = png_ptr->palette[i].red; |
| 1903 |
|
| 1904 |
component >>= shift; |
| 1905 |
png_ptr->palette[i].red = (png_byte)component; |
| 1906 |
} |
| 1907 |
|
| 1908 |
shift = 8 - png_ptr->sig_bit.green; |
| 1909 |
if (shift > 0 && shift < 8) |
| 1910 |
for (i=0; i<istop; ++i) |
| 1911 |
{ |
| 1912 |
int component = png_ptr->palette[i].green; |
| 1913 |
|
| 1914 |
component >>= shift; |
| 1915 |
png_ptr->palette[i].green = (png_byte)component; |
| 1916 |
} |
| 1917 |
|
| 1918 |
shift = 8 - png_ptr->sig_bit.blue; |
| 1919 |
if (shift > 0 && shift < 8) |
| 1920 |
for (i=0; i<istop; ++i) |
| 1921 |
{ |
| 1922 |
int component = png_ptr->palette[i].blue; |
| 1923 |
|
| 1924 |
component >>= shift; |
| 1925 |
png_ptr->palette[i].blue = (png_byte)component; |
| 1926 |
} |
| 1927 |
} |
| 1928 |
#endif |
| 1929 |
} |
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
void |
| 1936 |
png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) |
| 1937 |
{ |
| 1938 |
png_debug(1, "in png_read_transform_info"); |
| 1939 |
|
| 1940 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 1941 |
if ((png_ptr->transformations & PNG_EXPAND) != 0) |
| 1942 |
{ |
| 1943 |
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 1944 |
{ |
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
if (png_ptr->num_trans > 0) |
| 1950 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 1951 |
|
| 1952 |
else |
| 1953 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
| 1954 |
|
| 1955 |
info_ptr->bit_depth = 8; |
| 1956 |
info_ptr->num_trans = 0; |
| 1957 |
|
| 1958 |
if (png_ptr->palette == NULL) |
| 1959 |
png_error (png_ptr, "Palette is NULL in indexed image"); |
| 1960 |
} |
| 1961 |
else |
| 1962 |
{ |
| 1963 |
if (png_ptr->num_trans != 0) |
| 1964 |
{ |
| 1965 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
| 1966 |
info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 1967 |
} |
| 1968 |
if (info_ptr->bit_depth < 8) |
| 1969 |
info_ptr->bit_depth = 8; |
| 1970 |
|
| 1971 |
info_ptr->num_trans = 0; |
| 1972 |
} |
| 1973 |
} |
| 1974 |
#endif |
| 1975 |
|
| 1976 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 1977 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 1982 |
info_ptr->background = png_ptr->background; |
| 1983 |
#endif |
| 1984 |
|
| 1985 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; |
| 1996 |
#endif |
| 1997 |
|
| 1998 |
if (info_ptr->bit_depth == 16) |
| 1999 |
{ |
| 2000 |
# ifdef PNG_READ_16BIT_SUPPORTED |
| 2001 |
# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 2002 |
if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
| 2003 |
info_ptr->bit_depth = 8; |
| 2004 |
# endif |
| 2005 |
|
| 2006 |
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 2007 |
if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
| 2008 |
info_ptr->bit_depth = 8; |
| 2009 |
# endif |
| 2010 |
|
| 2011 |
# else |
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
png_ptr->transformations |= PNG_16_TO_8; |
| 2022 |
info_ptr->bit_depth = 8; |
| 2023 |
# else |
| 2024 |
|
| 2025 |
# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 2026 |
png_ptr->transformations |= PNG_SCALE_16_TO_8; |
| 2027 |
info_ptr->bit_depth = 8; |
| 2028 |
# else |
| 2029 |
|
| 2030 |
CONFIGURATION ERROR: you must enable at least one 16 to 8 method |
| 2031 |
# endif |
| 2032 |
# endif |
| 2033 |
#endif |
| 2034 |
} |
| 2035 |
|
| 2036 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 2037 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
| 2038 |
info_ptr->color_type = (png_byte)(info_ptr->color_type | |
| 2039 |
PNG_COLOR_MASK_COLOR); |
| 2040 |
#endif |
| 2041 |
|
| 2042 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 2043 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
| 2044 |
info_ptr->color_type = (png_byte)(info_ptr->color_type & |
| 2045 |
~PNG_COLOR_MASK_COLOR); |
| 2046 |
#endif |
| 2047 |
|
| 2048 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 2049 |
if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
| 2050 |
{ |
| 2051 |
if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
| 2052 |
(info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && |
| 2053 |
png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8) |
| 2054 |
{ |
| 2055 |
info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
| 2056 |
} |
| 2057 |
} |
| 2058 |
#endif |
| 2059 |
|
| 2060 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 2061 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
| 2062 |
info_ptr->bit_depth == 8 && |
| 2063 |
info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
| 2064 |
{ |
| 2065 |
info_ptr->bit_depth = 16; |
| 2066 |
} |
| 2067 |
#endif |
| 2068 |
|
| 2069 |
#ifdef PNG_READ_PACK_SUPPORTED |
| 2070 |
if ((png_ptr->transformations & PNG_PACK) != 0 && |
| 2071 |
(info_ptr->bit_depth < 8)) |
| 2072 |
info_ptr->bit_depth = 8; |
| 2073 |
#endif |
| 2074 |
|
| 2075 |
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
| 2076 |
info_ptr->channels = 1; |
| 2077 |
|
| 2078 |
else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 2079 |
info_ptr->channels = 3; |
| 2080 |
|
| 2081 |
else |
| 2082 |
info_ptr->channels = 1; |
| 2083 |
|
| 2084 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 2085 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0) |
| 2086 |
{ |
| 2087 |
info_ptr->color_type = (png_byte)(info_ptr->color_type & |
| 2088 |
~PNG_COLOR_MASK_ALPHA); |
| 2089 |
info_ptr->num_trans = 0; |
| 2090 |
} |
| 2091 |
#endif |
| 2092 |
|
| 2093 |
if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 2094 |
info_ptr->channels++; |
| 2095 |
|
| 2096 |
#ifdef PNG_READ_FILLER_SUPPORTED |
| 2097 |
|
| 2098 |
if ((png_ptr->transformations & PNG_FILLER) != 0 && |
| 2099 |
(info_ptr->color_type == PNG_COLOR_TYPE_RGB || |
| 2100 |
info_ptr->color_type == PNG_COLOR_TYPE_GRAY)) |
| 2101 |
{ |
| 2102 |
info_ptr->channels++; |
| 2103 |
|
| 2104 |
if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0) |
| 2105 |
info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
| 2106 |
} |
| 2107 |
#endif |
| 2108 |
|
| 2109 |
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ |
| 2110 |
defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
| 2111 |
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
| 2112 |
{ |
| 2113 |
if (png_ptr->user_transform_depth != 0) |
| 2114 |
info_ptr->bit_depth = png_ptr->user_transform_depth; |
| 2115 |
|
| 2116 |
if (png_ptr->user_transform_channels != 0) |
| 2117 |
info_ptr->channels = png_ptr->user_transform_channels; |
| 2118 |
} |
| 2119 |
#endif |
| 2120 |
|
| 2121 |
info_ptr->pixel_depth = (png_byte)(info_ptr->channels * |
| 2122 |
info_ptr->bit_depth); |
| 2123 |
|
| 2124 |
info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); |
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
png_ptr->info_rowbytes = info_ptr->rowbytes; |
| 2133 |
|
| 2134 |
#ifndef PNG_READ_EXPAND_SUPPORTED |
| 2135 |
if (png_ptr != NULL) |
| 2136 |
return; |
| 2137 |
#endif |
| 2138 |
} |
| 2139 |
|
| 2140 |
#ifdef PNG_READ_PACK_SUPPORTED |
| 2141 |
|
| 2142 |
|
| 2143 |
|
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
static void |
| 2148 |
png_do_unpack(png_row_infop row_info, png_bytep row) |
| 2149 |
{ |
| 2150 |
png_debug(1, "in png_do_unpack"); |
| 2151 |
|
| 2152 |
if (row_info->bit_depth < 8) |
| 2153 |
{ |
| 2154 |
png_uint_32 i; |
| 2155 |
png_uint_32 row_width=row_info->width; |
| 2156 |
|
| 2157 |
switch (row_info->bit_depth) |
| 2158 |
{ |
| 2159 |
case 1: |
| 2160 |
{ |
| 2161 |
png_bytep sp = row + (size_t)((row_width - 1) >> 3); |
| 2162 |
png_bytep dp = row + (size_t)row_width - 1; |
| 2163 |
png_uint_32 shift = 7U - ((row_width + 7U) & 0x07); |
| 2164 |
for (i = 0; i < row_width; i++) |
| 2165 |
{ |
| 2166 |
*dp = (png_byte)((*sp >> shift) & 0x01); |
| 2167 |
|
| 2168 |
if (shift == 7) |
| 2169 |
{ |
| 2170 |
shift = 0; |
| 2171 |
sp--; |
| 2172 |
} |
| 2173 |
|
| 2174 |
else |
| 2175 |
shift++; |
| 2176 |
|
| 2177 |
dp--; |
| 2178 |
} |
| 2179 |
break; |
| 2180 |
} |
| 2181 |
|
| 2182 |
case 2: |
| 2183 |
{ |
| 2184 |
|
| 2185 |
png_bytep sp = row + (size_t)((row_width - 1) >> 2); |
| 2186 |
png_bytep dp = row + (size_t)row_width - 1; |
| 2187 |
png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1); |
| 2188 |
for (i = 0; i < row_width; i++) |
| 2189 |
{ |
| 2190 |
*dp = (png_byte)((*sp >> shift) & 0x03); |
| 2191 |
|
| 2192 |
if (shift == 6) |
| 2193 |
{ |
| 2194 |
shift = 0; |
| 2195 |
sp--; |
| 2196 |
} |
| 2197 |
|
| 2198 |
else |
| 2199 |
shift += 2; |
| 2200 |
|
| 2201 |
dp--; |
| 2202 |
} |
| 2203 |
break; |
| 2204 |
} |
| 2205 |
|
| 2206 |
case 4: |
| 2207 |
{ |
| 2208 |
png_bytep sp = row + (size_t)((row_width - 1) >> 1); |
| 2209 |
png_bytep dp = row + (size_t)row_width - 1; |
| 2210 |
png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2); |
| 2211 |
for (i = 0; i < row_width; i++) |
| 2212 |
{ |
| 2213 |
*dp = (png_byte)((*sp >> shift) & 0x0f); |
| 2214 |
|
| 2215 |
if (shift == 4) |
| 2216 |
{ |
| 2217 |
shift = 0; |
| 2218 |
sp--; |
| 2219 |
} |
| 2220 |
|
| 2221 |
else |
| 2222 |
shift = 4; |
| 2223 |
|
| 2224 |
dp--; |
| 2225 |
} |
| 2226 |
break; |
| 2227 |
} |
| 2228 |
|
| 2229 |
default: |
| 2230 |
break; |
| 2231 |
} |
| 2232 |
row_info->bit_depth = 8; |
| 2233 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2234 |
row_info->rowbytes = row_width * row_info->channels; |
| 2235 |
} |
| 2236 |
} |
| 2237 |
#endif |
| 2238 |
|
| 2239 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
static void |
| 2246 |
png_do_unshift(png_row_infop row_info, png_bytep row, |
| 2247 |
png_const_color_8p sig_bits) |
| 2248 |
{ |
| 2249 |
int color_type; |
| 2250 |
|
| 2251 |
png_debug(1, "in png_do_unshift"); |
| 2252 |
|
| 2253 |
|
| 2254 |
color_type = row_info->color_type; |
| 2255 |
|
| 2256 |
if (color_type != PNG_COLOR_TYPE_PALETTE) |
| 2257 |
{ |
| 2258 |
int shift[4]; |
| 2259 |
int channels = 0; |
| 2260 |
int bit_depth = row_info->bit_depth; |
| 2261 |
|
| 2262 |
if ((color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 2263 |
{ |
| 2264 |
shift[channels++] = bit_depth - sig_bits->red; |
| 2265 |
shift[channels++] = bit_depth - sig_bits->green; |
| 2266 |
shift[channels++] = bit_depth - sig_bits->blue; |
| 2267 |
} |
| 2268 |
|
| 2269 |
else |
| 2270 |
{ |
| 2271 |
shift[channels++] = bit_depth - sig_bits->gray; |
| 2272 |
} |
| 2273 |
|
| 2274 |
if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 2275 |
{ |
| 2276 |
shift[channels++] = bit_depth - sig_bits->alpha; |
| 2277 |
} |
| 2278 |
|
| 2279 |
{ |
| 2280 |
int c, have_shift; |
| 2281 |
|
| 2282 |
for (c = have_shift = 0; c < channels; ++c) |
| 2283 |
{ |
| 2284 |
|
| 2285 |
|
| 2286 |
|
| 2287 |
if (shift[c] <= 0 || shift[c] >= bit_depth) |
| 2288 |
shift[c] = 0; |
| 2289 |
|
| 2290 |
else |
| 2291 |
have_shift = 1; |
| 2292 |
} |
| 2293 |
|
| 2294 |
if (have_shift == 0) |
| 2295 |
return; |
| 2296 |
} |
| 2297 |
|
| 2298 |
switch (bit_depth) |
| 2299 |
{ |
| 2300 |
default: |
| 2301 |
|
| 2302 |
|
| 2303 |
break; |
| 2304 |
|
| 2305 |
case 2: |
| 2306 |
|
| 2307 |
|
| 2308 |
{ |
| 2309 |
png_bytep bp = row; |
| 2310 |
png_bytep bp_end = bp + row_info->rowbytes; |
| 2311 |
|
| 2312 |
while (bp < bp_end) |
| 2313 |
{ |
| 2314 |
int b = (*bp >> 1) & 0x55; |
| 2315 |
*bp++ = (png_byte)b; |
| 2316 |
} |
| 2317 |
break; |
| 2318 |
} |
| 2319 |
|
| 2320 |
case 4: |
| 2321 |
|
| 2322 |
|
| 2323 |
{ |
| 2324 |
png_bytep bp = row; |
| 2325 |
png_bytep bp_end = bp + row_info->rowbytes; |
| 2326 |
int gray_shift = shift[0]; |
| 2327 |
int mask = 0xf >> gray_shift; |
| 2328 |
|
| 2329 |
mask |= mask << 4; |
| 2330 |
|
| 2331 |
while (bp < bp_end) |
| 2332 |
{ |
| 2333 |
int b = (*bp >> gray_shift) & mask; |
| 2334 |
*bp++ = (png_byte)b; |
| 2335 |
} |
| 2336 |
break; |
| 2337 |
} |
| 2338 |
|
| 2339 |
case 8: |
| 2340 |
|
| 2341 |
{ |
| 2342 |
png_bytep bp = row; |
| 2343 |
png_bytep bp_end = bp + row_info->rowbytes; |
| 2344 |
int channel = 0; |
| 2345 |
|
| 2346 |
while (bp < bp_end) |
| 2347 |
{ |
| 2348 |
int b = *bp >> shift[channel]; |
| 2349 |
if (++channel >= channels) |
| 2350 |
channel = 0; |
| 2351 |
*bp++ = (png_byte)b; |
| 2352 |
} |
| 2353 |
break; |
| 2354 |
} |
| 2355 |
|
| 2356 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2357 |
case 16: |
| 2358 |
|
| 2359 |
{ |
| 2360 |
png_bytep bp = row; |
| 2361 |
png_bytep bp_end = bp + row_info->rowbytes; |
| 2362 |
int channel = 0; |
| 2363 |
|
| 2364 |
while (bp < bp_end) |
| 2365 |
{ |
| 2366 |
int value = (bp[0] << 8) + bp[1]; |
| 2367 |
|
| 2368 |
value >>= shift[channel]; |
| 2369 |
if (++channel >= channels) |
| 2370 |
channel = 0; |
| 2371 |
*bp++ = (png_byte)(value >> 8); |
| 2372 |
*bp++ = (png_byte)value; |
| 2373 |
} |
| 2374 |
break; |
| 2375 |
} |
| 2376 |
#endif |
| 2377 |
} |
| 2378 |
} |
| 2379 |
} |
| 2380 |
#endif |
| 2381 |
|
| 2382 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 2383 |
|
| 2384 |
static void |
| 2385 |
png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) |
| 2386 |
{ |
| 2387 |
png_debug(1, "in png_do_scale_16_to_8"); |
| 2388 |
|
| 2389 |
if (row_info->bit_depth == 16) |
| 2390 |
{ |
| 2391 |
png_bytep sp = row; |
| 2392 |
png_bytep dp = row; |
| 2393 |
png_bytep ep = sp + row_info->rowbytes; |
| 2394 |
|
| 2395 |
while (sp < ep) |
| 2396 |
{ |
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
|
| 2414 |
|
| 2415 |
|
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
|
| 2429 |
png_int_32 tmp = *sp++; |
| 2430 |
tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; |
| 2431 |
*dp++ = (png_byte)tmp; |
| 2432 |
} |
| 2433 |
|
| 2434 |
row_info->bit_depth = 8; |
| 2435 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2436 |
row_info->rowbytes = row_info->width * row_info->channels; |
| 2437 |
} |
| 2438 |
} |
| 2439 |
#endif |
| 2440 |
|
| 2441 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 2442 |
static void |
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
png_do_chop(png_row_infop row_info, png_bytep row) |
| 2447 |
{ |
| 2448 |
png_debug(1, "in png_do_chop"); |
| 2449 |
|
| 2450 |
if (row_info->bit_depth == 16) |
| 2451 |
{ |
| 2452 |
png_bytep sp = row; |
| 2453 |
png_bytep dp = row; |
| 2454 |
png_bytep ep = sp + row_info->rowbytes; |
| 2455 |
|
| 2456 |
while (sp < ep) |
| 2457 |
{ |
| 2458 |
*dp++ = *sp; |
| 2459 |
sp += 2; |
| 2460 |
} |
| 2461 |
|
| 2462 |
row_info->bit_depth = 8; |
| 2463 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
| 2464 |
row_info->rowbytes = row_info->width * row_info->channels; |
| 2465 |
} |
| 2466 |
} |
| 2467 |
#endif |
| 2468 |
|
| 2469 |
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
| 2470 |
static void |
| 2471 |
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) |
| 2472 |
{ |
| 2473 |
png_uint_32 row_width = row_info->width; |
| 2474 |
|
| 2475 |
png_debug(1, "in png_do_read_swap_alpha"); |
| 2476 |
|
| 2477 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2478 |
{ |
| 2479 |
|
| 2480 |
if (row_info->bit_depth == 8) |
| 2481 |
{ |
| 2482 |
png_bytep sp = row + row_info->rowbytes; |
| 2483 |
png_bytep dp = sp; |
| 2484 |
png_byte save; |
| 2485 |
png_uint_32 i; |
| 2486 |
|
| 2487 |
for (i = 0; i < row_width; i++) |
| 2488 |
{ |
| 2489 |
save = *(--sp); |
| 2490 |
*(--dp) = *(--sp); |
| 2491 |
*(--dp) = *(--sp); |
| 2492 |
*(--dp) = *(--sp); |
| 2493 |
*(--dp) = save; |
| 2494 |
} |
| 2495 |
} |
| 2496 |
|
| 2497 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2498 |
|
| 2499 |
else |
| 2500 |
{ |
| 2501 |
png_bytep sp = row + row_info->rowbytes; |
| 2502 |
png_bytep dp = sp; |
| 2503 |
png_byte save[2]; |
| 2504 |
png_uint_32 i; |
| 2505 |
|
| 2506 |
for (i = 0; i < row_width; i++) |
| 2507 |
{ |
| 2508 |
save[0] = *(--sp); |
| 2509 |
save[1] = *(--sp); |
| 2510 |
*(--dp) = *(--sp); |
| 2511 |
*(--dp) = *(--sp); |
| 2512 |
*(--dp) = *(--sp); |
| 2513 |
*(--dp) = *(--sp); |
| 2514 |
*(--dp) = *(--sp); |
| 2515 |
*(--dp) = *(--sp); |
| 2516 |
*(--dp) = save[0]; |
| 2517 |
*(--dp) = save[1]; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
#endif |
| 2521 |
} |
| 2522 |
|
| 2523 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2524 |
{ |
| 2525 |
|
| 2526 |
if (row_info->bit_depth == 8) |
| 2527 |
{ |
| 2528 |
png_bytep sp = row + row_info->rowbytes; |
| 2529 |
png_bytep dp = sp; |
| 2530 |
png_byte save; |
| 2531 |
png_uint_32 i; |
| 2532 |
|
| 2533 |
for (i = 0; i < row_width; i++) |
| 2534 |
{ |
| 2535 |
save = *(--sp); |
| 2536 |
*(--dp) = *(--sp); |
| 2537 |
*(--dp) = save; |
| 2538 |
} |
| 2539 |
} |
| 2540 |
|
| 2541 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2542 |
|
| 2543 |
else |
| 2544 |
{ |
| 2545 |
png_bytep sp = row + row_info->rowbytes; |
| 2546 |
png_bytep dp = sp; |
| 2547 |
png_byte save[2]; |
| 2548 |
png_uint_32 i; |
| 2549 |
|
| 2550 |
for (i = 0; i < row_width; i++) |
| 2551 |
{ |
| 2552 |
save[0] = *(--sp); |
| 2553 |
save[1] = *(--sp); |
| 2554 |
*(--dp) = *(--sp); |
| 2555 |
*(--dp) = *(--sp); |
| 2556 |
*(--dp) = save[0]; |
| 2557 |
*(--dp) = save[1]; |
| 2558 |
} |
| 2559 |
} |
| 2560 |
#endif |
| 2561 |
} |
| 2562 |
} |
| 2563 |
#endif |
| 2564 |
|
| 2565 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
| 2566 |
static void |
| 2567 |
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) |
| 2568 |
{ |
| 2569 |
png_uint_32 row_width; |
| 2570 |
png_debug(1, "in png_do_read_invert_alpha"); |
| 2571 |
|
| 2572 |
row_width = row_info->width; |
| 2573 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
| 2574 |
{ |
| 2575 |
if (row_info->bit_depth == 8) |
| 2576 |
{ |
| 2577 |
|
| 2578 |
png_bytep sp = row + row_info->rowbytes; |
| 2579 |
png_bytep dp = sp; |
| 2580 |
png_uint_32 i; |
| 2581 |
|
| 2582 |
for (i = 0; i < row_width; i++) |
| 2583 |
{ |
| 2584 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
sp-=3; |
| 2593 |
dp=sp; |
| 2594 |
} |
| 2595 |
} |
| 2596 |
|
| 2597 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2598 |
|
| 2599 |
else |
| 2600 |
{ |
| 2601 |
png_bytep sp = row + row_info->rowbytes; |
| 2602 |
png_bytep dp = sp; |
| 2603 |
png_uint_32 i; |
| 2604 |
|
| 2605 |
for (i = 0; i < row_width; i++) |
| 2606 |
{ |
| 2607 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2608 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2609 |
|
| 2610 |
|
| 2611 |
|
| 2612 |
|
| 2613 |
|
| 2614 |
|
| 2615 |
|
| 2616 |
|
| 2617 |
|
| 2618 |
|
| 2619 |
sp-=6; |
| 2620 |
dp=sp; |
| 2621 |
} |
| 2622 |
} |
| 2623 |
#endif |
| 2624 |
} |
| 2625 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2626 |
{ |
| 2627 |
if (row_info->bit_depth == 8) |
| 2628 |
{ |
| 2629 |
|
| 2630 |
png_bytep sp = row + row_info->rowbytes; |
| 2631 |
png_bytep dp = sp; |
| 2632 |
png_uint_32 i; |
| 2633 |
|
| 2634 |
for (i = 0; i < row_width; i++) |
| 2635 |
{ |
| 2636 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2637 |
*(--dp) = *(--sp); |
| 2638 |
} |
| 2639 |
} |
| 2640 |
|
| 2641 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2642 |
else |
| 2643 |
{ |
| 2644 |
|
| 2645 |
png_bytep sp = row + row_info->rowbytes; |
| 2646 |
png_bytep dp = sp; |
| 2647 |
png_uint_32 i; |
| 2648 |
|
| 2649 |
for (i = 0; i < row_width; i++) |
| 2650 |
{ |
| 2651 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2652 |
*(--dp) = (png_byte)(255 - *(--sp)); |
| 2653 |
|
| 2654 |
|
| 2655 |
|
| 2656 |
|
| 2657 |
sp-=2; |
| 2658 |
dp=sp; |
| 2659 |
} |
| 2660 |
} |
| 2661 |
#endif |
| 2662 |
} |
| 2663 |
} |
| 2664 |
#endif |
| 2665 |
|
| 2666 |
#ifdef PNG_READ_FILLER_SUPPORTED |
| 2667 |
|
| 2668 |
static void |
| 2669 |
png_do_read_filler(png_row_infop row_info, png_bytep row, |
| 2670 |
png_uint_32 filler, png_uint_32 flags) |
| 2671 |
{ |
| 2672 |
png_uint_32 i; |
| 2673 |
png_uint_32 row_width = row_info->width; |
| 2674 |
|
| 2675 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2676 |
png_byte hi_filler = (png_byte)(filler>>8); |
| 2677 |
#endif |
| 2678 |
png_byte lo_filler = (png_byte)filler; |
| 2679 |
|
| 2680 |
png_debug(1, "in png_do_read_filler"); |
| 2681 |
|
| 2682 |
if ( |
| 2683 |
row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2684 |
{ |
| 2685 |
if (row_info->bit_depth == 8) |
| 2686 |
{ |
| 2687 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
| 2688 |
{ |
| 2689 |
|
| 2690 |
png_bytep sp = row + (size_t)row_width; |
| 2691 |
png_bytep dp = sp + (size_t)row_width; |
| 2692 |
for (i = 1; i < row_width; i++) |
| 2693 |
{ |
| 2694 |
*(--dp) = lo_filler; |
| 2695 |
*(--dp) = *(--sp); |
| 2696 |
} |
| 2697 |
*(--dp) = lo_filler; |
| 2698 |
row_info->channels = 2; |
| 2699 |
row_info->pixel_depth = 16; |
| 2700 |
row_info->rowbytes = row_width * 2; |
| 2701 |
} |
| 2702 |
|
| 2703 |
else |
| 2704 |
{ |
| 2705 |
|
| 2706 |
png_bytep sp = row + (size_t)row_width; |
| 2707 |
png_bytep dp = sp + (size_t)row_width; |
| 2708 |
for (i = 0; i < row_width; i++) |
| 2709 |
{ |
| 2710 |
*(--dp) = *(--sp); |
| 2711 |
*(--dp) = lo_filler; |
| 2712 |
} |
| 2713 |
row_info->channels = 2; |
| 2714 |
row_info->pixel_depth = 16; |
| 2715 |
row_info->rowbytes = row_width * 2; |
| 2716 |
} |
| 2717 |
} |
| 2718 |
|
| 2719 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2720 |
else if (row_info->bit_depth == 16) |
| 2721 |
{ |
| 2722 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
| 2723 |
{ |
| 2724 |
|
| 2725 |
png_bytep sp = row + (size_t)row_width * 2; |
| 2726 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2727 |
for (i = 1; i < row_width; i++) |
| 2728 |
{ |
| 2729 |
*(--dp) = lo_filler; |
| 2730 |
*(--dp) = hi_filler; |
| 2731 |
*(--dp) = *(--sp); |
| 2732 |
*(--dp) = *(--sp); |
| 2733 |
} |
| 2734 |
*(--dp) = lo_filler; |
| 2735 |
*(--dp) = hi_filler; |
| 2736 |
row_info->channels = 2; |
| 2737 |
row_info->pixel_depth = 32; |
| 2738 |
row_info->rowbytes = row_width * 4; |
| 2739 |
} |
| 2740 |
|
| 2741 |
else |
| 2742 |
{ |
| 2743 |
|
| 2744 |
png_bytep sp = row + (size_t)row_width * 2; |
| 2745 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2746 |
for (i = 0; i < row_width; i++) |
| 2747 |
{ |
| 2748 |
*(--dp) = *(--sp); |
| 2749 |
*(--dp) = *(--sp); |
| 2750 |
*(--dp) = lo_filler; |
| 2751 |
*(--dp) = hi_filler; |
| 2752 |
} |
| 2753 |
row_info->channels = 2; |
| 2754 |
row_info->pixel_depth = 32; |
| 2755 |
row_info->rowbytes = row_width * 4; |
| 2756 |
} |
| 2757 |
} |
| 2758 |
#endif |
| 2759 |
} |
| 2760 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
| 2761 |
{ |
| 2762 |
if (row_info->bit_depth == 8) |
| 2763 |
{ |
| 2764 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
| 2765 |
{ |
| 2766 |
|
| 2767 |
png_bytep sp = row + (size_t)row_width * 3; |
| 2768 |
png_bytep dp = sp + (size_t)row_width; |
| 2769 |
for (i = 1; i < row_width; i++) |
| 2770 |
{ |
| 2771 |
*(--dp) = lo_filler; |
| 2772 |
*(--dp) = *(--sp); |
| 2773 |
*(--dp) = *(--sp); |
| 2774 |
*(--dp) = *(--sp); |
| 2775 |
} |
| 2776 |
*(--dp) = lo_filler; |
| 2777 |
row_info->channels = 4; |
| 2778 |
row_info->pixel_depth = 32; |
| 2779 |
row_info->rowbytes = row_width * 4; |
| 2780 |
} |
| 2781 |
|
| 2782 |
else |
| 2783 |
{ |
| 2784 |
|
| 2785 |
png_bytep sp = row + (size_t)row_width * 3; |
| 2786 |
png_bytep dp = sp + (size_t)row_width; |
| 2787 |
for (i = 0; i < row_width; i++) |
| 2788 |
{ |
| 2789 |
*(--dp) = *(--sp); |
| 2790 |
*(--dp) = *(--sp); |
| 2791 |
*(--dp) = *(--sp); |
| 2792 |
*(--dp) = lo_filler; |
| 2793 |
} |
| 2794 |
row_info->channels = 4; |
| 2795 |
row_info->pixel_depth = 32; |
| 2796 |
row_info->rowbytes = row_width * 4; |
| 2797 |
} |
| 2798 |
} |
| 2799 |
|
| 2800 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 2801 |
else if (row_info->bit_depth == 16) |
| 2802 |
{ |
| 2803 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
| 2804 |
{ |
| 2805 |
|
| 2806 |
png_bytep sp = row + (size_t)row_width * 6; |
| 2807 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2808 |
for (i = 1; i < row_width; i++) |
| 2809 |
{ |
| 2810 |
*(--dp) = lo_filler; |
| 2811 |
*(--dp) = hi_filler; |
| 2812 |
*(--dp) = *(--sp); |
| 2813 |
*(--dp) = *(--sp); |
| 2814 |
*(--dp) = *(--sp); |
| 2815 |
*(--dp) = *(--sp); |
| 2816 |
*(--dp) = *(--sp); |
| 2817 |
*(--dp) = *(--sp); |
| 2818 |
} |
| 2819 |
*(--dp) = lo_filler; |
| 2820 |
*(--dp) = hi_filler; |
| 2821 |
row_info->channels = 4; |
| 2822 |
row_info->pixel_depth = 64; |
| 2823 |
row_info->rowbytes = row_width * 8; |
| 2824 |
} |
| 2825 |
|
| 2826 |
else |
| 2827 |
{ |
| 2828 |
|
| 2829 |
png_bytep sp = row + (size_t)row_width * 6; |
| 2830 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2831 |
for (i = 0; i < row_width; i++) |
| 2832 |
{ |
| 2833 |
*(--dp) = *(--sp); |
| 2834 |
*(--dp) = *(--sp); |
| 2835 |
*(--dp) = *(--sp); |
| 2836 |
*(--dp) = *(--sp); |
| 2837 |
*(--dp) = *(--sp); |
| 2838 |
*(--dp) = *(--sp); |
| 2839 |
*(--dp) = lo_filler; |
| 2840 |
*(--dp) = hi_filler; |
| 2841 |
} |
| 2842 |
|
| 2843 |
row_info->channels = 4; |
| 2844 |
row_info->pixel_depth = 64; |
| 2845 |
row_info->rowbytes = row_width * 8; |
| 2846 |
} |
| 2847 |
} |
| 2848 |
#endif |
| 2849 |
} |
| 2850 |
} |
| 2851 |
#endif |
| 2852 |
|
| 2853 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 2854 |
|
| 2855 |
static void |
| 2856 |
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) |
| 2857 |
{ |
| 2858 |
png_uint_32 i; |
| 2859 |
png_uint_32 row_width = row_info->width; |
| 2860 |
|
| 2861 |
png_debug(1, "in png_do_gray_to_rgb"); |
| 2862 |
|
| 2863 |
if (row_info->bit_depth >= 8 && |
| 2864 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) == 0) |
| 2865 |
{ |
| 2866 |
if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 2867 |
{ |
| 2868 |
if (row_info->bit_depth == 8) |
| 2869 |
{ |
| 2870 |
|
| 2871 |
png_bytep sp = row + (size_t)row_width - 1; |
| 2872 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2873 |
for (i = 0; i < row_width; i++) |
| 2874 |
{ |
| 2875 |
*(dp--) = *sp; |
| 2876 |
*(dp--) = *sp; |
| 2877 |
*(dp--) = *(sp--); |
| 2878 |
} |
| 2879 |
} |
| 2880 |
|
| 2881 |
else |
| 2882 |
{ |
| 2883 |
|
| 2884 |
png_bytep sp = row + (size_t)row_width * 2 - 1; |
| 2885 |
png_bytep dp = sp + (size_t)row_width * 4; |
| 2886 |
for (i = 0; i < row_width; i++) |
| 2887 |
{ |
| 2888 |
*(dp--) = *sp; |
| 2889 |
*(dp--) = *(sp - 1); |
| 2890 |
*(dp--) = *sp; |
| 2891 |
*(dp--) = *(sp - 1); |
| 2892 |
*(dp--) = *(sp--); |
| 2893 |
*(dp--) = *(sp--); |
| 2894 |
} |
| 2895 |
} |
| 2896 |
} |
| 2897 |
|
| 2898 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
| 2899 |
{ |
| 2900 |
if (row_info->bit_depth == 8) |
| 2901 |
{ |
| 2902 |
|
| 2903 |
png_bytep sp = row + (size_t)row_width * 2 - 1; |
| 2904 |
png_bytep dp = sp + (size_t)row_width * 2; |
| 2905 |
for (i = 0; i < row_width; i++) |
| 2906 |
{ |
| 2907 |
*(dp--) = *(sp--); |
| 2908 |
*(dp--) = *sp; |
| 2909 |
*(dp--) = *sp; |
| 2910 |
*(dp--) = *(sp--); |
| 2911 |
} |
| 2912 |
} |
| 2913 |
|
| 2914 |
else |
| 2915 |
{ |
| 2916 |
|
| 2917 |
png_bytep sp = row + (size_t)row_width * 4 - 1; |
| 2918 |
png_bytep dp = sp + (size_t)row_width * 4; |
| 2919 |
for (i = 0; i < row_width; i++) |
| 2920 |
{ |
| 2921 |
*(dp--) = *(sp--); |
| 2922 |
*(dp--) = *(sp--); |
| 2923 |
*(dp--) = *sp; |
| 2924 |
*(dp--) = *(sp - 1); |
| 2925 |
*(dp--) = *sp; |
| 2926 |
*(dp--) = *(sp - 1); |
| 2927 |
*(dp--) = *(sp--); |
| 2928 |
*(dp--) = *(sp--); |
| 2929 |
} |
| 2930 |
} |
| 2931 |
} |
| 2932 |
row_info->channels = (png_byte)(row_info->channels + 2); |
| 2933 |
row_info->color_type |= PNG_COLOR_MASK_COLOR; |
| 2934 |
row_info->pixel_depth = (png_byte)(row_info->channels * |
| 2935 |
row_info->bit_depth); |
| 2936 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
| 2937 |
} |
| 2938 |
} |
| 2939 |
#endif |
| 2940 |
|
| 2941 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 2942 |
|
| 2943 |
|
| 2944 |
|
| 2945 |
|
| 2946 |
|
| 2947 |
|
| 2948 |
|
| 2949 |
|
| 2950 |
|
| 2951 |
|
| 2952 |
|
| 2953 |
|
| 2954 |
|
| 2955 |
|
| 2956 |
|
| 2957 |
|
| 2958 |
|
| 2959 |
|
| 2960 |
|
| 2961 |
|
| 2962 |
|
| 2963 |
|
| 2964 |
|
| 2965 |
|
| 2966 |
|
| 2967 |
|
| 2968 |
|
| 2969 |
|
| 2970 |
|
| 2971 |
|
| 2972 |
|
| 2973 |
|
| 2974 |
|
| 2975 |
|
| 2976 |
|
| 2977 |
|
| 2978 |
|
| 2979 |
|
| 2980 |
|
| 2981 |
|
| 2982 |
|
| 2983 |
|
| 2984 |
|
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
static int |
| 2995 |
png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) |
| 2996 |
{ |
| 2997 |
int rgb_error = 0; |
| 2998 |
|
| 2999 |
png_debug(1, "in png_do_rgb_to_gray"); |
| 3000 |
|
| 3001 |
if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 && |
| 3002 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
| 3003 |
{ |
| 3004 |
png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; |
| 3005 |
png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; |
| 3006 |
png_uint_32 bc = 32768 - rc - gc; |
| 3007 |
png_uint_32 row_width = row_info->width; |
| 3008 |
int have_alpha = (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
| 3009 |
|
| 3010 |
if (row_info->bit_depth == 8) |
| 3011 |
{ |
| 3012 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3013 |
|
| 3014 |
|
| 3015 |
|
| 3016 |
|
| 3017 |
|
| 3018 |
if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
| 3019 |
{ |
| 3020 |
png_bytep sp = row; |
| 3021 |
png_bytep dp = row; |
| 3022 |
png_uint_32 i; |
| 3023 |
|
| 3024 |
for (i = 0; i < row_width; i++) |
| 3025 |
{ |
| 3026 |
png_byte red = *(sp++); |
| 3027 |
png_byte green = *(sp++); |
| 3028 |
png_byte blue = *(sp++); |
| 3029 |
|
| 3030 |
if (red != green || red != blue) |
| 3031 |
{ |
| 3032 |
red = png_ptr->gamma_to_1[red]; |
| 3033 |
green = png_ptr->gamma_to_1[green]; |
| 3034 |
blue = png_ptr->gamma_to_1[blue]; |
| 3035 |
|
| 3036 |
rgb_error |= 1; |
| 3037 |
*(dp++) = png_ptr->gamma_from_1[ |
| 3038 |
(rc*red + gc*green + bc*blue + 16384)>>15]; |
| 3039 |
} |
| 3040 |
|
| 3041 |
else |
| 3042 |
{ |
| 3043 |
|
| 3044 |
|
| 3045 |
|
| 3046 |
if (png_ptr->gamma_table != NULL) |
| 3047 |
red = png_ptr->gamma_table[red]; |
| 3048 |
|
| 3049 |
*(dp++) = red; |
| 3050 |
} |
| 3051 |
|
| 3052 |
if (have_alpha != 0) |
| 3053 |
*(dp++) = *(sp++); |
| 3054 |
} |
| 3055 |
} |
| 3056 |
else |
| 3057 |
#endif |
| 3058 |
{ |
| 3059 |
png_bytep sp = row; |
| 3060 |
png_bytep dp = row; |
| 3061 |
png_uint_32 i; |
| 3062 |
|
| 3063 |
for (i = 0; i < row_width; i++) |
| 3064 |
{ |
| 3065 |
png_byte red = *(sp++); |
| 3066 |
png_byte green = *(sp++); |
| 3067 |
png_byte blue = *(sp++); |
| 3068 |
|
| 3069 |
if (red != green || red != blue) |
| 3070 |
{ |
| 3071 |
rgb_error |= 1; |
| 3072 |
|
| 3073 |
|
| 3074 |
|
| 3075 |
*(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); |
| 3076 |
} |
| 3077 |
|
| 3078 |
else |
| 3079 |
*(dp++) = red; |
| 3080 |
|
| 3081 |
if (have_alpha != 0) |
| 3082 |
*(dp++) = *(sp++); |
| 3083 |
} |
| 3084 |
} |
| 3085 |
} |
| 3086 |
|
| 3087 |
else |
| 3088 |
{ |
| 3089 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3090 |
if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) |
| 3091 |
{ |
| 3092 |
png_bytep sp = row; |
| 3093 |
png_bytep dp = row; |
| 3094 |
png_uint_32 i; |
| 3095 |
|
| 3096 |
for (i = 0; i < row_width; i++) |
| 3097 |
{ |
| 3098 |
png_uint_16 red, green, blue, w; |
| 3099 |
png_byte hi,lo; |
| 3100 |
|
| 3101 |
hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
| 3102 |
hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
| 3103 |
hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
| 3104 |
|
| 3105 |
if (red == green && red == blue) |
| 3106 |
{ |
| 3107 |
if (png_ptr->gamma_16_table != NULL) |
| 3108 |
w = png_ptr->gamma_16_table[(red & 0xff) |
| 3109 |
>> png_ptr->gamma_shift][red >> 8]; |
| 3110 |
|
| 3111 |
else |
| 3112 |
w = red; |
| 3113 |
} |
| 3114 |
|
| 3115 |
else |
| 3116 |
{ |
| 3117 |
png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff) |
| 3118 |
>> png_ptr->gamma_shift][red>>8]; |
| 3119 |
png_uint_16 green_1 = |
| 3120 |
png_ptr->gamma_16_to_1[(green & 0xff) >> |
| 3121 |
png_ptr->gamma_shift][green>>8]; |
| 3122 |
png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff) |
| 3123 |
>> png_ptr->gamma_shift][blue>>8]; |
| 3124 |
png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 |
| 3125 |
+ bc*blue_1 + 16384)>>15); |
| 3126 |
w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >> |
| 3127 |
png_ptr->gamma_shift][gray16 >> 8]; |
| 3128 |
rgb_error |= 1; |
| 3129 |
} |
| 3130 |
|
| 3131 |
*(dp++) = (png_byte)((w>>8) & 0xff); |
| 3132 |
*(dp++) = (png_byte)(w & 0xff); |
| 3133 |
|
| 3134 |
if (have_alpha != 0) |
| 3135 |
{ |
| 3136 |
*(dp++) = *(sp++); |
| 3137 |
*(dp++) = *(sp++); |
| 3138 |
} |
| 3139 |
} |
| 3140 |
} |
| 3141 |
else |
| 3142 |
#endif |
| 3143 |
{ |
| 3144 |
png_bytep sp = row; |
| 3145 |
png_bytep dp = row; |
| 3146 |
png_uint_32 i; |
| 3147 |
|
| 3148 |
for (i = 0; i < row_width; i++) |
| 3149 |
{ |
| 3150 |
png_uint_16 red, green, blue, gray16; |
| 3151 |
png_byte hi,lo; |
| 3152 |
|
| 3153 |
hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
| 3154 |
hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
| 3155 |
hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
| 3156 |
|
| 3157 |
if (red != green || red != blue) |
| 3158 |
rgb_error |= 1; |
| 3159 |
|
| 3160 |
|
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> |
| 3165 |
15); |
| 3166 |
*(dp++) = (png_byte)((gray16 >> 8) & 0xff); |
| 3167 |
*(dp++) = (png_byte)(gray16 & 0xff); |
| 3168 |
|
| 3169 |
if (have_alpha != 0) |
| 3170 |
{ |
| 3171 |
*(dp++) = *(sp++); |
| 3172 |
*(dp++) = *(sp++); |
| 3173 |
} |
| 3174 |
} |
| 3175 |
} |
| 3176 |
} |
| 3177 |
|
| 3178 |
row_info->channels = (png_byte)(row_info->channels - 2); |
| 3179 |
row_info->color_type = (png_byte)(row_info->color_type & |
| 3180 |
~PNG_COLOR_MASK_COLOR); |
| 3181 |
row_info->pixel_depth = (png_byte)(row_info->channels * |
| 3182 |
row_info->bit_depth); |
| 3183 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
| 3184 |
} |
| 3185 |
return rgb_error; |
| 3186 |
} |
| 3187 |
#endif |
| 3188 |
|
| 3189 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 3190 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 3191 |
|
| 3192 |
|
| 3193 |
|
| 3194 |
|
| 3195 |
static void |
| 3196 |
png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
| 3197 |
{ |
| 3198 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3199 |
png_const_bytep gamma_table = png_ptr->gamma_table; |
| 3200 |
png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; |
| 3201 |
png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; |
| 3202 |
png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; |
| 3203 |
png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; |
| 3204 |
png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; |
| 3205 |
int gamma_shift = png_ptr->gamma_shift; |
| 3206 |
int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; |
| 3207 |
#endif |
| 3208 |
|
| 3209 |
png_bytep sp; |
| 3210 |
png_uint_32 i; |
| 3211 |
png_uint_32 row_width = row_info->width; |
| 3212 |
int shift; |
| 3213 |
|
| 3214 |
png_debug(1, "in png_do_compose"); |
| 3215 |
|
| 3216 |
switch (row_info->color_type) |
| 3217 |
{ |
| 3218 |
case PNG_COLOR_TYPE_GRAY: |
| 3219 |
{ |
| 3220 |
switch (row_info->bit_depth) |
| 3221 |
{ |
| 3222 |
case 1: |
| 3223 |
{ |
| 3224 |
sp = row; |
| 3225 |
shift = 7; |
| 3226 |
for (i = 0; i < row_width; i++) |
| 3227 |
{ |
| 3228 |
if ((png_uint_16)((*sp >> shift) & 0x01) |
| 3229 |
== png_ptr->trans_color.gray) |
| 3230 |
{ |
| 3231 |
unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); |
| 3232 |
tmp |= |
| 3233 |
(unsigned int)(png_ptr->background.gray << shift); |
| 3234 |
*sp = (png_byte)(tmp & 0xff); |
| 3235 |
} |
| 3236 |
|
| 3237 |
if (shift == 0) |
| 3238 |
{ |
| 3239 |
shift = 7; |
| 3240 |
sp++; |
| 3241 |
} |
| 3242 |
|
| 3243 |
else |
| 3244 |
shift--; |
| 3245 |
} |
| 3246 |
break; |
| 3247 |
} |
| 3248 |
|
| 3249 |
case 2: |
| 3250 |
{ |
| 3251 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3252 |
if (gamma_table != NULL) |
| 3253 |
{ |
| 3254 |
sp = row; |
| 3255 |
shift = 6; |
| 3256 |
for (i = 0; i < row_width; i++) |
| 3257 |
{ |
| 3258 |
if ((png_uint_16)((*sp >> shift) & 0x03) |
| 3259 |
== png_ptr->trans_color.gray) |
| 3260 |
{ |
| 3261 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
| 3262 |
tmp |= |
| 3263 |
(unsigned int)png_ptr->background.gray << shift; |
| 3264 |
*sp = (png_byte)(tmp & 0xff); |
| 3265 |
} |
| 3266 |
|
| 3267 |
else |
| 3268 |
{ |
| 3269 |
unsigned int p = (*sp >> shift) & 0x03; |
| 3270 |
unsigned int g = (gamma_table [p | (p << 2) | |
| 3271 |
(p << 4) | (p << 6)] >> 6) & 0x03; |
| 3272 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
| 3273 |
tmp |= (unsigned int)(g << shift); |
| 3274 |
*sp = (png_byte)(tmp & 0xff); |
| 3275 |
} |
| 3276 |
|
| 3277 |
if (shift == 0) |
| 3278 |
{ |
| 3279 |
shift = 6; |
| 3280 |
sp++; |
| 3281 |
} |
| 3282 |
|
| 3283 |
else |
| 3284 |
shift -= 2; |
| 3285 |
} |
| 3286 |
} |
| 3287 |
|
| 3288 |
else |
| 3289 |
#endif |
| 3290 |
{ |
| 3291 |
sp = row; |
| 3292 |
shift = 6; |
| 3293 |
for (i = 0; i < row_width; i++) |
| 3294 |
{ |
| 3295 |
if ((png_uint_16)((*sp >> shift) & 0x03) |
| 3296 |
== png_ptr->trans_color.gray) |
| 3297 |
{ |
| 3298 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
| 3299 |
tmp |= |
| 3300 |
(unsigned int)png_ptr->background.gray << shift; |
| 3301 |
*sp = (png_byte)(tmp & 0xff); |
| 3302 |
} |
| 3303 |
|
| 3304 |
if (shift == 0) |
| 3305 |
{ |
| 3306 |
shift = 6; |
| 3307 |
sp++; |
| 3308 |
} |
| 3309 |
|
| 3310 |
else |
| 3311 |
shift -= 2; |
| 3312 |
} |
| 3313 |
} |
| 3314 |
break; |
| 3315 |
} |
| 3316 |
|
| 3317 |
case 4: |
| 3318 |
{ |
| 3319 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3320 |
if (gamma_table != NULL) |
| 3321 |
{ |
| 3322 |
sp = row; |
| 3323 |
shift = 4; |
| 3324 |
for (i = 0; i < row_width; i++) |
| 3325 |
{ |
| 3326 |
if ((png_uint_16)((*sp >> shift) & 0x0f) |
| 3327 |
== png_ptr->trans_color.gray) |
| 3328 |
{ |
| 3329 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
| 3330 |
tmp |= |
| 3331 |
(unsigned int)(png_ptr->background.gray << shift); |
| 3332 |
*sp = (png_byte)(tmp & 0xff); |
| 3333 |
} |
| 3334 |
|
| 3335 |
else |
| 3336 |
{ |
| 3337 |
unsigned int p = (*sp >> shift) & 0x0f; |
| 3338 |
unsigned int g = (gamma_table[p | (p << 4)] >> 4) & |
| 3339 |
0x0f; |
| 3340 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
| 3341 |
tmp |= (unsigned int)(g << shift); |
| 3342 |
*sp = (png_byte)(tmp & 0xff); |
| 3343 |
} |
| 3344 |
|
| 3345 |
if (shift == 0) |
| 3346 |
{ |
| 3347 |
shift = 4; |
| 3348 |
sp++; |
| 3349 |
} |
| 3350 |
|
| 3351 |
else |
| 3352 |
shift -= 4; |
| 3353 |
} |
| 3354 |
} |
| 3355 |
|
| 3356 |
else |
| 3357 |
#endif |
| 3358 |
{ |
| 3359 |
sp = row; |
| 3360 |
shift = 4; |
| 3361 |
for (i = 0; i < row_width; i++) |
| 3362 |
{ |
| 3363 |
if ((png_uint_16)((*sp >> shift) & 0x0f) |
| 3364 |
== png_ptr->trans_color.gray) |
| 3365 |
{ |
| 3366 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
| 3367 |
tmp |= |
| 3368 |
(unsigned int)(png_ptr->background.gray << shift); |
| 3369 |
*sp = (png_byte)(tmp & 0xff); |
| 3370 |
} |
| 3371 |
|
| 3372 |
if (shift == 0) |
| 3373 |
{ |
| 3374 |
shift = 4; |
| 3375 |
sp++; |
| 3376 |
} |
| 3377 |
|
| 3378 |
else |
| 3379 |
shift -= 4; |
| 3380 |
} |
| 3381 |
} |
| 3382 |
break; |
| 3383 |
} |
| 3384 |
|
| 3385 |
case 8: |
| 3386 |
{ |
| 3387 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3388 |
if (gamma_table != NULL) |
| 3389 |
{ |
| 3390 |
sp = row; |
| 3391 |
for (i = 0; i < row_width; i++, sp++) |
| 3392 |
{ |
| 3393 |
if (*sp == png_ptr->trans_color.gray) |
| 3394 |
*sp = (png_byte)png_ptr->background.gray; |
| 3395 |
|
| 3396 |
else |
| 3397 |
*sp = gamma_table[*sp]; |
| 3398 |
} |
| 3399 |
} |
| 3400 |
else |
| 3401 |
#endif |
| 3402 |
{ |
| 3403 |
sp = row; |
| 3404 |
for (i = 0; i < row_width; i++, sp++) |
| 3405 |
{ |
| 3406 |
if (*sp == png_ptr->trans_color.gray) |
| 3407 |
*sp = (png_byte)png_ptr->background.gray; |
| 3408 |
} |
| 3409 |
} |
| 3410 |
break; |
| 3411 |
} |
| 3412 |
|
| 3413 |
case 16: |
| 3414 |
{ |
| 3415 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3416 |
if (gamma_16 != NULL) |
| 3417 |
{ |
| 3418 |
sp = row; |
| 3419 |
for (i = 0; i < row_width; i++, sp += 2) |
| 3420 |
{ |
| 3421 |
png_uint_16 v; |
| 3422 |
|
| 3423 |
v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3424 |
|
| 3425 |
if (v == png_ptr->trans_color.gray) |
| 3426 |
{ |
| 3427 |
|
| 3428 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3429 |
& 0xff); |
| 3430 |
*(sp + 1) = (png_byte)(png_ptr->background.gray |
| 3431 |
& 0xff); |
| 3432 |
} |
| 3433 |
|
| 3434 |
else |
| 3435 |
{ |
| 3436 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3437 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3438 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3439 |
} |
| 3440 |
} |
| 3441 |
} |
| 3442 |
else |
| 3443 |
#endif |
| 3444 |
{ |
| 3445 |
sp = row; |
| 3446 |
for (i = 0; i < row_width; i++, sp += 2) |
| 3447 |
{ |
| 3448 |
png_uint_16 v; |
| 3449 |
|
| 3450 |
v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3451 |
|
| 3452 |
if (v == png_ptr->trans_color.gray) |
| 3453 |
{ |
| 3454 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3455 |
& 0xff); |
| 3456 |
*(sp + 1) = (png_byte)(png_ptr->background.gray |
| 3457 |
& 0xff); |
| 3458 |
} |
| 3459 |
} |
| 3460 |
} |
| 3461 |
break; |
| 3462 |
} |
| 3463 |
|
| 3464 |
default: |
| 3465 |
break; |
| 3466 |
} |
| 3467 |
break; |
| 3468 |
} |
| 3469 |
|
| 3470 |
case PNG_COLOR_TYPE_RGB: |
| 3471 |
{ |
| 3472 |
if (row_info->bit_depth == 8) |
| 3473 |
{ |
| 3474 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3475 |
if (gamma_table != NULL) |
| 3476 |
{ |
| 3477 |
sp = row; |
| 3478 |
for (i = 0; i < row_width; i++, sp += 3) |
| 3479 |
{ |
| 3480 |
if (*sp == png_ptr->trans_color.red && |
| 3481 |
*(sp + 1) == png_ptr->trans_color.green && |
| 3482 |
*(sp + 2) == png_ptr->trans_color.blue) |
| 3483 |
{ |
| 3484 |
*sp = (png_byte)png_ptr->background.red; |
| 3485 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
| 3486 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
| 3487 |
} |
| 3488 |
|
| 3489 |
else |
| 3490 |
{ |
| 3491 |
*sp = gamma_table[*sp]; |
| 3492 |
*(sp + 1) = gamma_table[*(sp + 1)]; |
| 3493 |
*(sp + 2) = gamma_table[*(sp + 2)]; |
| 3494 |
} |
| 3495 |
} |
| 3496 |
} |
| 3497 |
else |
| 3498 |
#endif |
| 3499 |
{ |
| 3500 |
sp = row; |
| 3501 |
for (i = 0; i < row_width; i++, sp += 3) |
| 3502 |
{ |
| 3503 |
if (*sp == png_ptr->trans_color.red && |
| 3504 |
*(sp + 1) == png_ptr->trans_color.green && |
| 3505 |
*(sp + 2) == png_ptr->trans_color.blue) |
| 3506 |
{ |
| 3507 |
*sp = (png_byte)png_ptr->background.red; |
| 3508 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
| 3509 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
| 3510 |
} |
| 3511 |
} |
| 3512 |
} |
| 3513 |
} |
| 3514 |
else |
| 3515 |
{ |
| 3516 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3517 |
if (gamma_16 != NULL) |
| 3518 |
{ |
| 3519 |
sp = row; |
| 3520 |
for (i = 0; i < row_width; i++, sp += 6) |
| 3521 |
{ |
| 3522 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3523 |
|
| 3524 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3525 |
+ *(sp + 3)); |
| 3526 |
|
| 3527 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3528 |
+ *(sp + 5)); |
| 3529 |
|
| 3530 |
if (r == png_ptr->trans_color.red && |
| 3531 |
g == png_ptr->trans_color.green && |
| 3532 |
b == png_ptr->trans_color.blue) |
| 3533 |
{ |
| 3534 |
|
| 3535 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3536 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3537 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3538 |
& 0xff); |
| 3539 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
| 3540 |
& 0xff); |
| 3541 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3542 |
& 0xff); |
| 3543 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
| 3544 |
} |
| 3545 |
|
| 3546 |
else |
| 3547 |
{ |
| 3548 |
png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3549 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3550 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3551 |
|
| 3552 |
v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
| 3553 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3554 |
*(sp + 3) = (png_byte)(v & 0xff); |
| 3555 |
|
| 3556 |
v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
| 3557 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3558 |
*(sp + 5) = (png_byte)(v & 0xff); |
| 3559 |
} |
| 3560 |
} |
| 3561 |
} |
| 3562 |
|
| 3563 |
else |
| 3564 |
#endif |
| 3565 |
{ |
| 3566 |
sp = row; |
| 3567 |
for (i = 0; i < row_width; i++, sp += 6) |
| 3568 |
{ |
| 3569 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3570 |
|
| 3571 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3572 |
+ *(sp + 3)); |
| 3573 |
|
| 3574 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3575 |
+ *(sp + 5)); |
| 3576 |
|
| 3577 |
if (r == png_ptr->trans_color.red && |
| 3578 |
g == png_ptr->trans_color.green && |
| 3579 |
b == png_ptr->trans_color.blue) |
| 3580 |
{ |
| 3581 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3582 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3583 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3584 |
& 0xff); |
| 3585 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
| 3586 |
& 0xff); |
| 3587 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3588 |
& 0xff); |
| 3589 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
| 3590 |
} |
| 3591 |
} |
| 3592 |
} |
| 3593 |
} |
| 3594 |
break; |
| 3595 |
} |
| 3596 |
|
| 3597 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 3598 |
{ |
| 3599 |
if (row_info->bit_depth == 8) |
| 3600 |
{ |
| 3601 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3602 |
if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3603 |
gamma_table != NULL) |
| 3604 |
{ |
| 3605 |
sp = row; |
| 3606 |
for (i = 0; i < row_width; i++, sp += 2) |
| 3607 |
{ |
| 3608 |
png_uint_16 a = *(sp + 1); |
| 3609 |
|
| 3610 |
if (a == 0xff) |
| 3611 |
*sp = gamma_table[*sp]; |
| 3612 |
|
| 3613 |
else if (a == 0) |
| 3614 |
{ |
| 3615 |
|
| 3616 |
*sp = (png_byte)png_ptr->background.gray; |
| 3617 |
} |
| 3618 |
|
| 3619 |
else |
| 3620 |
{ |
| 3621 |
png_byte v, w; |
| 3622 |
|
| 3623 |
v = gamma_to_1[*sp]; |
| 3624 |
png_composite(w, v, a, png_ptr->background_1.gray); |
| 3625 |
if (optimize == 0) |
| 3626 |
w = gamma_from_1[w]; |
| 3627 |
*sp = w; |
| 3628 |
} |
| 3629 |
} |
| 3630 |
} |
| 3631 |
else |
| 3632 |
#endif |
| 3633 |
{ |
| 3634 |
sp = row; |
| 3635 |
for (i = 0; i < row_width; i++, sp += 2) |
| 3636 |
{ |
| 3637 |
png_byte a = *(sp + 1); |
| 3638 |
|
| 3639 |
if (a == 0) |
| 3640 |
*sp = (png_byte)png_ptr->background.gray; |
| 3641 |
|
| 3642 |
else if (a < 0xff) |
| 3643 |
png_composite(*sp, *sp, a, png_ptr->background.gray); |
| 3644 |
} |
| 3645 |
} |
| 3646 |
} |
| 3647 |
else |
| 3648 |
{ |
| 3649 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3650 |
if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3651 |
gamma_16_to_1 != NULL) |
| 3652 |
{ |
| 3653 |
sp = row; |
| 3654 |
for (i = 0; i < row_width; i++, sp += 4) |
| 3655 |
{ |
| 3656 |
png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
| 3657 |
+ *(sp + 3)); |
| 3658 |
|
| 3659 |
if (a == (png_uint_16)0xffff) |
| 3660 |
{ |
| 3661 |
png_uint_16 v; |
| 3662 |
|
| 3663 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3664 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3665 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3666 |
} |
| 3667 |
|
| 3668 |
else if (a == 0) |
| 3669 |
{ |
| 3670 |
|
| 3671 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3672 |
& 0xff); |
| 3673 |
*(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
| 3674 |
} |
| 3675 |
|
| 3676 |
else |
| 3677 |
{ |
| 3678 |
png_uint_16 g, v, w; |
| 3679 |
|
| 3680 |
g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
| 3681 |
png_composite_16(v, g, a, png_ptr->background_1.gray); |
| 3682 |
if (optimize != 0) |
| 3683 |
w = v; |
| 3684 |
else |
| 3685 |
w = gamma_16_from_1[(v & 0xff) >> |
| 3686 |
gamma_shift][v >> 8]; |
| 3687 |
*sp = (png_byte)((w >> 8) & 0xff); |
| 3688 |
*(sp + 1) = (png_byte)(w & 0xff); |
| 3689 |
} |
| 3690 |
} |
| 3691 |
} |
| 3692 |
else |
| 3693 |
#endif |
| 3694 |
{ |
| 3695 |
sp = row; |
| 3696 |
for (i = 0; i < row_width; i++, sp += 4) |
| 3697 |
{ |
| 3698 |
png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
| 3699 |
+ *(sp + 3)); |
| 3700 |
|
| 3701 |
if (a == 0) |
| 3702 |
{ |
| 3703 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
| 3704 |
& 0xff); |
| 3705 |
*(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
| 3706 |
} |
| 3707 |
|
| 3708 |
else if (a < 0xffff) |
| 3709 |
{ |
| 3710 |
png_uint_16 g, v; |
| 3711 |
|
| 3712 |
g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3713 |
png_composite_16(v, g, a, png_ptr->background.gray); |
| 3714 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3715 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3716 |
} |
| 3717 |
} |
| 3718 |
} |
| 3719 |
} |
| 3720 |
break; |
| 3721 |
} |
| 3722 |
|
| 3723 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 3724 |
{ |
| 3725 |
if (row_info->bit_depth == 8) |
| 3726 |
{ |
| 3727 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3728 |
if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
| 3729 |
gamma_table != NULL) |
| 3730 |
{ |
| 3731 |
sp = row; |
| 3732 |
for (i = 0; i < row_width; i++, sp += 4) |
| 3733 |
{ |
| 3734 |
png_byte a = *(sp + 3); |
| 3735 |
|
| 3736 |
if (a == 0xff) |
| 3737 |
{ |
| 3738 |
*sp = gamma_table[*sp]; |
| 3739 |
*(sp + 1) = gamma_table[*(sp + 1)]; |
| 3740 |
*(sp + 2) = gamma_table[*(sp + 2)]; |
| 3741 |
} |
| 3742 |
|
| 3743 |
else if (a == 0) |
| 3744 |
{ |
| 3745 |
|
| 3746 |
*sp = (png_byte)png_ptr->background.red; |
| 3747 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
| 3748 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
| 3749 |
} |
| 3750 |
|
| 3751 |
else |
| 3752 |
{ |
| 3753 |
png_byte v, w; |
| 3754 |
|
| 3755 |
v = gamma_to_1[*sp]; |
| 3756 |
png_composite(w, v, a, png_ptr->background_1.red); |
| 3757 |
if (optimize == 0) w = gamma_from_1[w]; |
| 3758 |
*sp = w; |
| 3759 |
|
| 3760 |
v = gamma_to_1[*(sp + 1)]; |
| 3761 |
png_composite(w, v, a, png_ptr->background_1.green); |
| 3762 |
if (optimize == 0) w = gamma_from_1[w]; |
| 3763 |
*(sp + 1) = w; |
| 3764 |
|
| 3765 |
v = gamma_to_1[*(sp + 2)]; |
| 3766 |
png_composite(w, v, a, png_ptr->background_1.blue); |
| 3767 |
if (optimize == 0) w = gamma_from_1[w]; |
| 3768 |
*(sp + 2) = w; |
| 3769 |
} |
| 3770 |
} |
| 3771 |
} |
| 3772 |
else |
| 3773 |
#endif |
| 3774 |
{ |
| 3775 |
sp = row; |
| 3776 |
for (i = 0; i < row_width; i++, sp += 4) |
| 3777 |
{ |
| 3778 |
png_byte a = *(sp + 3); |
| 3779 |
|
| 3780 |
if (a == 0) |
| 3781 |
{ |
| 3782 |
*sp = (png_byte)png_ptr->background.red; |
| 3783 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
| 3784 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
| 3785 |
} |
| 3786 |
|
| 3787 |
else if (a < 0xff) |
| 3788 |
{ |
| 3789 |
png_composite(*sp, *sp, a, png_ptr->background.red); |
| 3790 |
|
| 3791 |
png_composite(*(sp + 1), *(sp + 1), a, |
| 3792 |
png_ptr->background.green); |
| 3793 |
|
| 3794 |
png_composite(*(sp + 2), *(sp + 2), a, |
| 3795 |
png_ptr->background.blue); |
| 3796 |
} |
| 3797 |
} |
| 3798 |
} |
| 3799 |
} |
| 3800 |
else |
| 3801 |
{ |
| 3802 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3803 |
if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
| 3804 |
gamma_16_to_1 != NULL) |
| 3805 |
{ |
| 3806 |
sp = row; |
| 3807 |
for (i = 0; i < row_width; i++, sp += 8) |
| 3808 |
{ |
| 3809 |
png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3810 |
<< 8) + (png_uint_16)(*(sp + 7))); |
| 3811 |
|
| 3812 |
if (a == (png_uint_16)0xffff) |
| 3813 |
{ |
| 3814 |
png_uint_16 v; |
| 3815 |
|
| 3816 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
| 3817 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3818 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3819 |
|
| 3820 |
v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
| 3821 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3822 |
*(sp + 3) = (png_byte)(v & 0xff); |
| 3823 |
|
| 3824 |
v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
| 3825 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3826 |
*(sp + 5) = (png_byte)(v & 0xff); |
| 3827 |
} |
| 3828 |
|
| 3829 |
else if (a == 0) |
| 3830 |
{ |
| 3831 |
|
| 3832 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3833 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3834 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3835 |
& 0xff); |
| 3836 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
| 3837 |
& 0xff); |
| 3838 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3839 |
& 0xff); |
| 3840 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
| 3841 |
} |
| 3842 |
|
| 3843 |
else |
| 3844 |
{ |
| 3845 |
png_uint_16 v, w; |
| 3846 |
|
| 3847 |
v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
| 3848 |
png_composite_16(w, v, a, png_ptr->background_1.red); |
| 3849 |
if (optimize == 0) |
| 3850 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
| 3851 |
8]; |
| 3852 |
*sp = (png_byte)((w >> 8) & 0xff); |
| 3853 |
*(sp + 1) = (png_byte)(w & 0xff); |
| 3854 |
|
| 3855 |
v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
| 3856 |
png_composite_16(w, v, a, png_ptr->background_1.green); |
| 3857 |
if (optimize == 0) |
| 3858 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
| 3859 |
8]; |
| 3860 |
|
| 3861 |
*(sp + 2) = (png_byte)((w >> 8) & 0xff); |
| 3862 |
*(sp + 3) = (png_byte)(w & 0xff); |
| 3863 |
|
| 3864 |
v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
| 3865 |
png_composite_16(w, v, a, png_ptr->background_1.blue); |
| 3866 |
if (optimize == 0) |
| 3867 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
| 3868 |
8]; |
| 3869 |
|
| 3870 |
*(sp + 4) = (png_byte)((w >> 8) & 0xff); |
| 3871 |
*(sp + 5) = (png_byte)(w & 0xff); |
| 3872 |
} |
| 3873 |
} |
| 3874 |
} |
| 3875 |
|
| 3876 |
else |
| 3877 |
#endif |
| 3878 |
{ |
| 3879 |
sp = row; |
| 3880 |
for (i = 0; i < row_width; i++, sp += 8) |
| 3881 |
{ |
| 3882 |
png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
| 3883 |
<< 8) + (png_uint_16)(*(sp + 7))); |
| 3884 |
|
| 3885 |
if (a == 0) |
| 3886 |
{ |
| 3887 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
| 3888 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
| 3889 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
| 3890 |
& 0xff); |
| 3891 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
| 3892 |
& 0xff); |
| 3893 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
| 3894 |
& 0xff); |
| 3895 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
| 3896 |
} |
| 3897 |
|
| 3898 |
else if (a < 0xffff) |
| 3899 |
{ |
| 3900 |
png_uint_16 v; |
| 3901 |
|
| 3902 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
| 3903 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
| 3904 |
+ *(sp + 3)); |
| 3905 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
| 3906 |
+ *(sp + 5)); |
| 3907 |
|
| 3908 |
png_composite_16(v, r, a, png_ptr->background.red); |
| 3909 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3910 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3911 |
|
| 3912 |
png_composite_16(v, g, a, png_ptr->background.green); |
| 3913 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
| 3914 |
*(sp + 3) = (png_byte)(v & 0xff); |
| 3915 |
|
| 3916 |
png_composite_16(v, b, a, png_ptr->background.blue); |
| 3917 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
| 3918 |
*(sp + 5) = (png_byte)(v & 0xff); |
| 3919 |
} |
| 3920 |
} |
| 3921 |
} |
| 3922 |
} |
| 3923 |
break; |
| 3924 |
} |
| 3925 |
|
| 3926 |
default: |
| 3927 |
break; |
| 3928 |
} |
| 3929 |
} |
| 3930 |
#endif |
| 3931 |
|
| 3932 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 3933 |
|
| 3934 |
|
| 3935 |
|
| 3936 |
|
| 3937 |
|
| 3938 |
|
| 3939 |
static void |
| 3940 |
png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
| 3941 |
{ |
| 3942 |
png_const_bytep gamma_table = png_ptr->gamma_table; |
| 3943 |
png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; |
| 3944 |
int gamma_shift = png_ptr->gamma_shift; |
| 3945 |
|
| 3946 |
png_bytep sp; |
| 3947 |
png_uint_32 i; |
| 3948 |
png_uint_32 row_width=row_info->width; |
| 3949 |
|
| 3950 |
png_debug(1, "in png_do_gamma"); |
| 3951 |
|
| 3952 |
if (((row_info->bit_depth <= 8 && gamma_table != NULL) || |
| 3953 |
(row_info->bit_depth == 16 && gamma_16_table != NULL))) |
| 3954 |
{ |
| 3955 |
switch (row_info->color_type) |
| 3956 |
{ |
| 3957 |
case PNG_COLOR_TYPE_RGB: |
| 3958 |
{ |
| 3959 |
if (row_info->bit_depth == 8) |
| 3960 |
{ |
| 3961 |
sp = row; |
| 3962 |
for (i = 0; i < row_width; i++) |
| 3963 |
{ |
| 3964 |
*sp = gamma_table[*sp]; |
| 3965 |
sp++; |
| 3966 |
*sp = gamma_table[*sp]; |
| 3967 |
sp++; |
| 3968 |
*sp = gamma_table[*sp]; |
| 3969 |
sp++; |
| 3970 |
} |
| 3971 |
} |
| 3972 |
|
| 3973 |
else |
| 3974 |
{ |
| 3975 |
sp = row; |
| 3976 |
for (i = 0; i < row_width; i++) |
| 3977 |
{ |
| 3978 |
png_uint_16 v; |
| 3979 |
|
| 3980 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3981 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3982 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3983 |
sp += 2; |
| 3984 |
|
| 3985 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3986 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3987 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3988 |
sp += 2; |
| 3989 |
|
| 3990 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 3991 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 3992 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 3993 |
sp += 2; |
| 3994 |
} |
| 3995 |
} |
| 3996 |
break; |
| 3997 |
} |
| 3998 |
|
| 3999 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
| 4000 |
{ |
| 4001 |
if (row_info->bit_depth == 8) |
| 4002 |
{ |
| 4003 |
sp = row; |
| 4004 |
for (i = 0; i < row_width; i++) |
| 4005 |
{ |
| 4006 |
*sp = gamma_table[*sp]; |
| 4007 |
sp++; |
| 4008 |
|
| 4009 |
*sp = gamma_table[*sp]; |
| 4010 |
sp++; |
| 4011 |
|
| 4012 |
*sp = gamma_table[*sp]; |
| 4013 |
sp++; |
| 4014 |
|
| 4015 |
sp++; |
| 4016 |
} |
| 4017 |
} |
| 4018 |
|
| 4019 |
else |
| 4020 |
{ |
| 4021 |
sp = row; |
| 4022 |
for (i = 0; i < row_width; i++) |
| 4023 |
{ |
| 4024 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4025 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 4026 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 4027 |
sp += 2; |
| 4028 |
|
| 4029 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4030 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 4031 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 4032 |
sp += 2; |
| 4033 |
|
| 4034 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4035 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 4036 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 4037 |
sp += 4; |
| 4038 |
} |
| 4039 |
} |
| 4040 |
break; |
| 4041 |
} |
| 4042 |
|
| 4043 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
| 4044 |
{ |
| 4045 |
if (row_info->bit_depth == 8) |
| 4046 |
{ |
| 4047 |
sp = row; |
| 4048 |
for (i = 0; i < row_width; i++) |
| 4049 |
{ |
| 4050 |
*sp = gamma_table[*sp]; |
| 4051 |
sp += 2; |
| 4052 |
} |
| 4053 |
} |
| 4054 |
|
| 4055 |
else |
| 4056 |
{ |
| 4057 |
sp = row; |
| 4058 |
for (i = 0; i < row_width; i++) |
| 4059 |
{ |
| 4060 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4061 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 4062 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 4063 |
sp += 4; |
| 4064 |
} |
| 4065 |
} |
| 4066 |
break; |
| 4067 |
} |
| 4068 |
|
| 4069 |
case PNG_COLOR_TYPE_GRAY: |
| 4070 |
{ |
| 4071 |
if (row_info->bit_depth == 2) |
| 4072 |
{ |
| 4073 |
sp = row; |
| 4074 |
for (i = 0; i < row_width; i += 4) |
| 4075 |
{ |
| 4076 |
int a = *sp & 0xc0; |
| 4077 |
int b = *sp & 0x30; |
| 4078 |
int c = *sp & 0x0c; |
| 4079 |
int d = *sp & 0x03; |
| 4080 |
|
| 4081 |
*sp = (png_byte)( |
| 4082 |
((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| |
| 4083 |
((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| |
| 4084 |
((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| |
| 4085 |
((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); |
| 4086 |
sp++; |
| 4087 |
} |
| 4088 |
} |
| 4089 |
|
| 4090 |
if (row_info->bit_depth == 4) |
| 4091 |
{ |
| 4092 |
sp = row; |
| 4093 |
for (i = 0; i < row_width; i += 2) |
| 4094 |
{ |
| 4095 |
int msb = *sp & 0xf0; |
| 4096 |
int lsb = *sp & 0x0f; |
| 4097 |
|
| 4098 |
*sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
| 4099 |
| (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); |
| 4100 |
sp++; |
| 4101 |
} |
| 4102 |
} |
| 4103 |
|
| 4104 |
else if (row_info->bit_depth == 8) |
| 4105 |
{ |
| 4106 |
sp = row; |
| 4107 |
for (i = 0; i < row_width; i++) |
| 4108 |
{ |
| 4109 |
*sp = gamma_table[*sp]; |
| 4110 |
sp++; |
| 4111 |
} |
| 4112 |
} |
| 4113 |
|
| 4114 |
else if (row_info->bit_depth == 16) |
| 4115 |
{ |
| 4116 |
sp = row; |
| 4117 |
for (i = 0; i < row_width; i++) |
| 4118 |
{ |
| 4119 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
| 4120 |
*sp = (png_byte)((v >> 8) & 0xff); |
| 4121 |
*(sp + 1) = (png_byte)(v & 0xff); |
| 4122 |
sp += 2; |
| 4123 |
} |
| 4124 |
} |
| 4125 |
break; |
| 4126 |
} |
| 4127 |
|
| 4128 |
default: |
| 4129 |
break; |
| 4130 |
} |
| 4131 |
} |
| 4132 |
} |
| 4133 |
#endif |
| 4134 |
|
| 4135 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 4136 |
|
| 4137 |
|
| 4138 |
|
| 4139 |
|
| 4140 |
static void |
| 4141 |
png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
| 4142 |
{ |
| 4143 |
png_uint_32 row_width = row_info->width; |
| 4144 |
|
| 4145 |
png_debug(1, "in png_do_encode_alpha"); |
| 4146 |
|
| 4147 |
if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 4148 |
{ |
| 4149 |
if (row_info->bit_depth == 8) |
| 4150 |
{ |
| 4151 |
png_bytep table = png_ptr->gamma_from_1; |
| 4152 |
|
| 4153 |
if (table != NULL) |
| 4154 |
{ |
| 4155 |
int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; |
| 4156 |
|
| 4157 |
|
| 4158 |
row += step - 1; |
| 4159 |
|
| 4160 |
for (; row_width > 0; --row_width, row += step) |
| 4161 |
*row = table[*row]; |
| 4162 |
|
| 4163 |
return; |
| 4164 |
} |
| 4165 |
} |
| 4166 |
|
| 4167 |
else if (row_info->bit_depth == 16) |
| 4168 |
{ |
| 4169 |
png_uint_16pp table = png_ptr->gamma_16_from_1; |
| 4170 |
int gamma_shift = png_ptr->gamma_shift; |
| 4171 |
|
| 4172 |
if (table != NULL) |
| 4173 |
{ |
| 4174 |
int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4; |
| 4175 |
|
| 4176 |
|
| 4177 |
row += step - 2; |
| 4178 |
|
| 4179 |
for (; row_width > 0; --row_width, row += step) |
| 4180 |
{ |
| 4181 |
png_uint_16 v; |
| 4182 |
|
| 4183 |
v = table[*(row + 1) >> gamma_shift][*row]; |
| 4184 |
*row = (png_byte)((v >> 8) & 0xff); |
| 4185 |
*(row + 1) = (png_byte)(v & 0xff); |
| 4186 |
} |
| 4187 |
|
| 4188 |
return; |
| 4189 |
} |
| 4190 |
} |
| 4191 |
} |
| 4192 |
|
| 4193 |
|
| 4194 |
|
| 4195 |
|
| 4196 |
png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); |
| 4197 |
} |
| 4198 |
#endif |
| 4199 |
|
| 4200 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 4201 |
|
| 4202 |
|
| 4203 |
|
| 4204 |
static void |
| 4205 |
png_do_expand_palette(png_structrp png_ptr, png_row_infop row_info, |
| 4206 |
png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha, |
| 4207 |
int num_trans) |
| 4208 |
{ |
| 4209 |
int shift, value; |
| 4210 |
png_bytep sp, dp; |
| 4211 |
png_uint_32 i; |
| 4212 |
png_uint_32 row_width=row_info->width; |
| 4213 |
|
| 4214 |
png_debug(1, "in png_do_expand_palette"); |
| 4215 |
|
| 4216 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
| 4217 |
{ |
| 4218 |
if (row_info->bit_depth < 8) |
| 4219 |
{ |
| 4220 |
switch (row_info->bit_depth) |
| 4221 |
{ |
| 4222 |
case 1: |
| 4223 |
{ |
| 4224 |
sp = row + (size_t)((row_width - 1) >> 3); |
| 4225 |
dp = row + (size_t)row_width - 1; |
| 4226 |
shift = 7 - (int)((row_width + 7) & 0x07); |
| 4227 |
for (i = 0; i < row_width; i++) |
| 4228 |
{ |
| 4229 |
if ((*sp >> shift) & 0x01) |
| 4230 |
*dp = 1; |
| 4231 |
|
| 4232 |
else |
| 4233 |
*dp = 0; |
| 4234 |
|
| 4235 |
if (shift == 7) |
| 4236 |
{ |
| 4237 |
shift = 0; |
| 4238 |
sp--; |
| 4239 |
} |
| 4240 |
|
| 4241 |
else |
| 4242 |
shift++; |
| 4243 |
|
| 4244 |
dp--; |
| 4245 |
} |
| 4246 |
break; |
| 4247 |
} |
| 4248 |
|
| 4249 |
case 2: |
| 4250 |
{ |
| 4251 |
sp = row + (size_t)((row_width - 1) >> 2); |
| 4252 |
dp = row + (size_t)row_width - 1; |
| 4253 |
shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
| 4254 |
for (i = 0; i < row_width; i++) |
| 4255 |
{ |
| 4256 |
value = (*sp >> shift) & 0x03; |
| 4257 |
*dp = (png_byte)value; |
| 4258 |
if (shift == 6) |
| 4259 |
{ |
| 4260 |
shift = 0; |
| 4261 |
sp--; |
| 4262 |
} |
| 4263 |
|
| 4264 |
else |
| 4265 |
shift += 2; |
| 4266 |
|
| 4267 |
dp--; |
| 4268 |
} |
| 4269 |
break; |
| 4270 |
} |
| 4271 |
|
| 4272 |
case 4: |
| 4273 |
{ |
| 4274 |
sp = row + (size_t)((row_width - 1) >> 1); |
| 4275 |
dp = row + (size_t)row_width - 1; |
| 4276 |
shift = (int)((row_width & 0x01) << 2); |
| 4277 |
for (i = 0; i < row_width; i++) |
| 4278 |
{ |
| 4279 |
value = (*sp >> shift) & 0x0f; |
| 4280 |
*dp = (png_byte)value; |
| 4281 |
if (shift == 4) |
| 4282 |
{ |
| 4283 |
shift = 0; |
| 4284 |
sp--; |
| 4285 |
} |
| 4286 |
|
| 4287 |
else |
| 4288 |
shift += 4; |
| 4289 |
|
| 4290 |
dp--; |
| 4291 |
} |
| 4292 |
break; |
| 4293 |
} |
| 4294 |
|
| 4295 |
default: |
| 4296 |
break; |
| 4297 |
} |
| 4298 |
row_info->bit_depth = 8; |
| 4299 |
row_info->pixel_depth = 8; |
| 4300 |
row_info->rowbytes = row_width; |
| 4301 |
} |
| 4302 |
|
| 4303 |
if (row_info->bit_depth == 8) |
| 4304 |
{ |
| 4305 |
{ |
| 4306 |
if (num_trans > 0) |
| 4307 |
{ |
| 4308 |
sp = row + (size_t)row_width - 1; |
| 4309 |
dp = row + ((size_t)row_width << 2) - 1; |
| 4310 |
|
| 4311 |
i = 0; |
| 4312 |
#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE |
| 4313 |
if (png_ptr->riffled_palette != NULL) |
| 4314 |
{ |
| 4315 |
|
| 4316 |
|
| 4317 |
|
| 4318 |
|
| 4319 |
i = png_do_expand_palette_rgba8_neon(png_ptr, row_info, row, |
| 4320 |
&sp, &dp); |
| 4321 |
} |
| 4322 |
#else |
| 4323 |
PNG_UNUSED(png_ptr) |
| 4324 |
#endif |
| 4325 |
|
| 4326 |
for (; i < row_width; i++) |
| 4327 |
{ |
| 4328 |
if ((int)(*sp) >= num_trans) |
| 4329 |
*dp-- = 0xff; |
| 4330 |
else |
| 4331 |
*dp-- = trans_alpha[*sp]; |
| 4332 |
*dp-- = palette[*sp].blue; |
| 4333 |
*dp-- = palette[*sp].green; |
| 4334 |
*dp-- = palette[*sp].red; |
| 4335 |
sp--; |
| 4336 |
} |
| 4337 |
row_info->bit_depth = 8; |
| 4338 |
row_info->pixel_depth = 32; |
| 4339 |
row_info->rowbytes = row_width * 4; |
| 4340 |
row_info->color_type = 6; |
| 4341 |
row_info->channels = 4; |
| 4342 |
} |
| 4343 |
|
| 4344 |
else |
| 4345 |
{ |
| 4346 |
sp = row + (size_t)row_width - 1; |
| 4347 |
dp = row + (size_t)(row_width * 3) - 1; |
| 4348 |
i = 0; |
| 4349 |
#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE |
| 4350 |
i = png_do_expand_palette_rgb8_neon(png_ptr, row_info, row, |
| 4351 |
&sp, &dp); |
| 4352 |
#else |
| 4353 |
PNG_UNUSED(png_ptr) |
| 4354 |
#endif |
| 4355 |
|
| 4356 |
for (; i < row_width; i++) |
| 4357 |
{ |
| 4358 |
*dp-- = palette[*sp].blue; |
| 4359 |
*dp-- = palette[*sp].green; |
| 4360 |
*dp-- = palette[*sp].red; |
| 4361 |
sp--; |
| 4362 |
} |
| 4363 |
|
| 4364 |
row_info->bit_depth = 8; |
| 4365 |
row_info->pixel_depth = 24; |
| 4366 |
row_info->rowbytes = row_width * 3; |
| 4367 |
row_info->color_type = 2; |
| 4368 |
row_info->channels = 3; |
| 4369 |
} |
| 4370 |
} |
| 4371 |
} |
| 4372 |
} |
| 4373 |
} |
| 4374 |
|
| 4375 |
|
| 4376 |
|
| 4377 |
|
| 4378 |
static void |
| 4379 |
png_do_expand(png_row_infop row_info, png_bytep row, |
| 4380 |
png_const_color_16p trans_color) |
| 4381 |
{ |
| 4382 |
int shift, value; |
| 4383 |
png_bytep sp, dp; |
| 4384 |
png_uint_32 i; |
| 4385 |
png_uint_32 row_width=row_info->width; |
| 4386 |
|
| 4387 |
png_debug(1, "in png_do_expand"); |
| 4388 |
|
| 4389 |
if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
| 4390 |
{ |
| 4391 |
unsigned int gray = trans_color != NULL ? trans_color->gray : 0; |
| 4392 |
|
| 4393 |
if (row_info->bit_depth < 8) |
| 4394 |
{ |
| 4395 |
switch (row_info->bit_depth) |
| 4396 |
{ |
| 4397 |
case 1: |
| 4398 |
{ |
| 4399 |
gray = (gray & 0x01) * 0xff; |
| 4400 |
sp = row + (size_t)((row_width - 1) >> 3); |
| 4401 |
dp = row + (size_t)row_width - 1; |
| 4402 |
shift = 7 - (int)((row_width + 7) & 0x07); |
| 4403 |
for (i = 0; i < row_width; i++) |
| 4404 |
{ |
| 4405 |
if ((*sp >> shift) & 0x01) |
| 4406 |
*dp = 0xff; |
| 4407 |
|
| 4408 |
else |
| 4409 |
*dp = 0; |
| 4410 |
|
| 4411 |
if (shift == 7) |
| 4412 |
{ |
| 4413 |
shift = 0; |
| 4414 |
sp--; |
| 4415 |
} |
| 4416 |
|
| 4417 |
else |
| 4418 |
shift++; |
| 4419 |
|
| 4420 |
dp--; |
| 4421 |
} |
| 4422 |
break; |
| 4423 |
} |
| 4424 |
|
| 4425 |
case 2: |
| 4426 |
{ |
| 4427 |
gray = (gray & 0x03) * 0x55; |
| 4428 |
sp = row + (size_t)((row_width - 1) >> 2); |
| 4429 |
dp = row + (size_t)row_width - 1; |
| 4430 |
shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
| 4431 |
for (i = 0; i < row_width; i++) |
| 4432 |
{ |
| 4433 |
value = (*sp >> shift) & 0x03; |
| 4434 |
*dp = (png_byte)(value | (value << 2) | (value << 4) | |
| 4435 |
(value << 6)); |
| 4436 |
if (shift == 6) |
| 4437 |
{ |
| 4438 |
shift = 0; |
| 4439 |
sp--; |
| 4440 |
} |
| 4441 |
|
| 4442 |
else |
| 4443 |
shift += 2; |
| 4444 |
|
| 4445 |
dp--; |
| 4446 |
} |
| 4447 |
break; |
| 4448 |
} |
| 4449 |
|
| 4450 |
case 4: |
| 4451 |
{ |
| 4452 |
gray = (gray & 0x0f) * 0x11; |
| 4453 |
sp = row + (size_t)((row_width - 1) >> 1); |
| 4454 |
dp = row + (size_t)row_width - 1; |
| 4455 |
shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
| 4456 |
for (i = 0; i < row_width; i++) |
| 4457 |
{ |
| 4458 |
value = (*sp >> shift) & 0x0f; |
| 4459 |
*dp = (png_byte)(value | (value << 4)); |
| 4460 |
if (shift == 4) |
| 4461 |
{ |
| 4462 |
shift = 0; |
| 4463 |
sp--; |
| 4464 |
} |
| 4465 |
|
| 4466 |
else |
| 4467 |
shift = 4; |
| 4468 |
|
| 4469 |
dp--; |
| 4470 |
} |
| 4471 |
break; |
| 4472 |
} |
| 4473 |
|
| 4474 |
default: |
| 4475 |
break; |
| 4476 |
} |
| 4477 |
|
| 4478 |
row_info->bit_depth = 8; |
| 4479 |
row_info->pixel_depth = 8; |
| 4480 |
row_info->rowbytes = row_width; |
| 4481 |
} |
| 4482 |
|
| 4483 |
if (trans_color != NULL) |
| 4484 |
{ |
| 4485 |
if (row_info->bit_depth == 8) |
| 4486 |
{ |
| 4487 |
gray = gray & 0xff; |
| 4488 |
sp = row + (size_t)row_width - 1; |
| 4489 |
dp = row + ((size_t)row_width << 1) - 1; |
| 4490 |
|
| 4491 |
for (i = 0; i < row_width; i++) |
| 4492 |
{ |
| 4493 |
if ((*sp & 0xffU) == gray) |
| 4494 |
*dp-- = 0; |
| 4495 |
|
| 4496 |
else |
| 4497 |
*dp-- = 0xff; |
| 4498 |
|
| 4499 |
*dp-- = *sp--; |
| 4500 |
} |
| 4501 |
} |
| 4502 |
|
| 4503 |
else if (row_info->bit_depth == 16) |
| 4504 |
{ |
| 4505 |
unsigned int gray_high = (gray >> 8) & 0xff; |
| 4506 |
unsigned int gray_low = gray & 0xff; |
| 4507 |
sp = row + row_info->rowbytes - 1; |
| 4508 |
dp = row + (row_info->rowbytes << 1) - 1; |
| 4509 |
for (i = 0; i < row_width; i++) |
| 4510 |
{ |
| 4511 |
if ((*(sp - 1) & 0xffU) == gray_high && |
| 4512 |
(*(sp) & 0xffU) == gray_low) |
| 4513 |
{ |
| 4514 |
*dp-- = 0; |
| 4515 |
*dp-- = 0; |
| 4516 |
} |
| 4517 |
|
| 4518 |
else |
| 4519 |
{ |
| 4520 |
*dp-- = 0xff; |
| 4521 |
*dp-- = 0xff; |
| 4522 |
} |
| 4523 |
|
| 4524 |
*dp-- = *sp--; |
| 4525 |
*dp-- = *sp--; |
| 4526 |
} |
| 4527 |
} |
| 4528 |
|
| 4529 |
row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; |
| 4530 |
row_info->channels = 2; |
| 4531 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); |
| 4532 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
| 4533 |
row_width); |
| 4534 |
} |
| 4535 |
} |
| 4536 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB && |
| 4537 |
trans_color != NULL) |
| 4538 |
{ |
| 4539 |
if (row_info->bit_depth == 8) |
| 4540 |
{ |
| 4541 |
png_byte red = (png_byte)(trans_color->red & 0xff); |
| 4542 |
png_byte green = (png_byte)(trans_color->green & 0xff); |
| 4543 |
png_byte blue = (png_byte)(trans_color->blue & 0xff); |
| 4544 |
sp = row + (size_t)row_info->rowbytes - 1; |
| 4545 |
dp = row + ((size_t)row_width << 2) - 1; |
| 4546 |
for (i = 0; i < row_width; i++) |
| 4547 |
{ |
| 4548 |
if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) |
| 4549 |
*dp-- = 0; |
| 4550 |
|
| 4551 |
else |
| 4552 |
*dp-- = 0xff; |
| 4553 |
|
| 4554 |
*dp-- = *sp--; |
| 4555 |
*dp-- = *sp--; |
| 4556 |
*dp-- = *sp--; |
| 4557 |
} |
| 4558 |
} |
| 4559 |
else if (row_info->bit_depth == 16) |
| 4560 |
{ |
| 4561 |
png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff); |
| 4562 |
png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff); |
| 4563 |
png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff); |
| 4564 |
png_byte red_low = (png_byte)(trans_color->red & 0xff); |
| 4565 |
png_byte green_low = (png_byte)(trans_color->green & 0xff); |
| 4566 |
png_byte blue_low = (png_byte)(trans_color->blue & 0xff); |
| 4567 |
sp = row + row_info->rowbytes - 1; |
| 4568 |
dp = row + ((size_t)row_width << 3) - 1; |
| 4569 |
for (i = 0; i < row_width; i++) |
| 4570 |
{ |
| 4571 |
if (*(sp - 5) == red_high && |
| 4572 |
*(sp - 4) == red_low && |
| 4573 |
*(sp - 3) == green_high && |
| 4574 |
*(sp - 2) == green_low && |
| 4575 |
*(sp - 1) == blue_high && |
| 4576 |
*(sp ) == blue_low) |
| 4577 |
{ |
| 4578 |
*dp-- = 0; |
| 4579 |
*dp-- = 0; |
| 4580 |
} |
| 4581 |
|
| 4582 |
else |
| 4583 |
{ |
| 4584 |
*dp-- = 0xff; |
| 4585 |
*dp-- = 0xff; |
| 4586 |
} |
| 4587 |
|
| 4588 |
*dp-- = *sp--; |
| 4589 |
*dp-- = *sp--; |
| 4590 |
*dp-- = *sp--; |
| 4591 |
*dp-- = *sp--; |
| 4592 |
*dp-- = *sp--; |
| 4593 |
*dp-- = *sp--; |
| 4594 |
} |
| 4595 |
} |
| 4596 |
row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
| 4597 |
row_info->channels = 4; |
| 4598 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); |
| 4599 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
| 4600 |
} |
| 4601 |
} |
| 4602 |
#endif |
| 4603 |
|
| 4604 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4605 |
|
| 4606 |
|
| 4607 |
|
| 4608 |
static void |
| 4609 |
png_do_expand_16(png_row_infop row_info, png_bytep row) |
| 4610 |
{ |
| 4611 |
if (row_info->bit_depth == 8 && |
| 4612 |
row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
| 4613 |
{ |
| 4614 |
|
| 4615 |
|
| 4616 |
|
| 4617 |
|
| 4618 |
|
| 4619 |
|
| 4620 |
|
| 4621 |
|
| 4622 |
|
| 4623 |
png_byte *sp = row + row_info->rowbytes; |
| 4624 |
png_byte *dp = sp + row_info->rowbytes; |
| 4625 |
while (dp > sp) |
| 4626 |
{ |
| 4627 |
dp[-2] = dp[-1] = *--sp; dp -= 2; |
| 4628 |
} |
| 4629 |
|
| 4630 |
row_info->rowbytes *= 2; |
| 4631 |
row_info->bit_depth = 16; |
| 4632 |
row_info->pixel_depth = (png_byte)(row_info->channels * 16); |
| 4633 |
} |
| 4634 |
} |
| 4635 |
#endif |
| 4636 |
|
| 4637 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 4638 |
static void |
| 4639 |
png_do_quantize(png_row_infop row_info, png_bytep row, |
| 4640 |
png_const_bytep palette_lookup, png_const_bytep quantize_lookup) |
| 4641 |
{ |
| 4642 |
png_bytep sp, dp; |
| 4643 |
png_uint_32 i; |
| 4644 |
png_uint_32 row_width=row_info->width; |
| 4645 |
|
| 4646 |
png_debug(1, "in png_do_quantize"); |
| 4647 |
|
| 4648 |
if (row_info->bit_depth == 8) |
| 4649 |
{ |
| 4650 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup) |
| 4651 |
{ |
| 4652 |
int r, g, b, p; |
| 4653 |
sp = row; |
| 4654 |
dp = row; |
| 4655 |
for (i = 0; i < row_width; i++) |
| 4656 |
{ |
| 4657 |
r = *sp++; |
| 4658 |
g = *sp++; |
| 4659 |
b = *sp++; |
| 4660 |
|
| 4661 |
|
| 4662 |
|
| 4663 |
|
| 4664 |
|
| 4665 |
|
| 4666 |
|
| 4667 |
|
| 4668 |
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
| 4669 |
((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
| 4670 |
(PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
| 4671 |
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
| 4672 |
((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
| 4673 |
(PNG_QUANTIZE_BLUE_BITS)) | |
| 4674 |
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
| 4675 |
((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
| 4676 |
|
| 4677 |
*dp++ = palette_lookup[p]; |
| 4678 |
} |
| 4679 |
|
| 4680 |
row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 4681 |
row_info->channels = 1; |
| 4682 |
row_info->pixel_depth = row_info->bit_depth; |
| 4683 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
| 4684 |
} |
| 4685 |
|
| 4686 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
| 4687 |
palette_lookup != NULL) |
| 4688 |
{ |
| 4689 |
int r, g, b, p; |
| 4690 |
sp = row; |
| 4691 |
dp = row; |
| 4692 |
for (i = 0; i < row_width; i++) |
| 4693 |
{ |
| 4694 |
r = *sp++; |
| 4695 |
g = *sp++; |
| 4696 |
b = *sp++; |
| 4697 |
sp++; |
| 4698 |
|
| 4699 |
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
| 4700 |
((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
| 4701 |
(PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
| 4702 |
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
| 4703 |
((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
| 4704 |
(PNG_QUANTIZE_BLUE_BITS)) | |
| 4705 |
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
| 4706 |
((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
| 4707 |
|
| 4708 |
*dp++ = palette_lookup[p]; |
| 4709 |
} |
| 4710 |
|
| 4711 |
row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
| 4712 |
row_info->channels = 1; |
| 4713 |
row_info->pixel_depth = row_info->bit_depth; |
| 4714 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
| 4715 |
} |
| 4716 |
|
| 4717 |
else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
| 4718 |
quantize_lookup) |
| 4719 |
{ |
| 4720 |
sp = row; |
| 4721 |
|
| 4722 |
for (i = 0; i < row_width; i++, sp++) |
| 4723 |
{ |
| 4724 |
*sp = quantize_lookup[*sp]; |
| 4725 |
} |
| 4726 |
} |
| 4727 |
} |
| 4728 |
} |
| 4729 |
#endif |
| 4730 |
|
| 4731 |
|
| 4732 |
|
| 4733 |
|
| 4734 |
|
| 4735 |
void |
| 4736 |
png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) |
| 4737 |
{ |
| 4738 |
png_debug(1, "in png_do_read_transformations"); |
| 4739 |
|
| 4740 |
if (png_ptr->row_buf == NULL) |
| 4741 |
{ |
| 4742 |
|
| 4743 |
|
| 4744 |
|
| 4745 |
|
| 4746 |
png_error(png_ptr, "NULL row buffer"); |
| 4747 |
} |
| 4748 |
|
| 4749 |
|
| 4750 |
|
| 4751 |
|
| 4752 |
|
| 4753 |
|
| 4754 |
|
| 4755 |
if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && |
| 4756 |
(png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
| 4757 |
{ |
| 4758 |
|
| 4759 |
|
| 4760 |
|
| 4761 |
|
| 4762 |
png_error(png_ptr, "Uninitialized row"); |
| 4763 |
} |
| 4764 |
|
| 4765 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
| 4766 |
if ((png_ptr->transformations & PNG_EXPAND) != 0) |
| 4767 |
{ |
| 4768 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
| 4769 |
{ |
| 4770 |
#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE |
| 4771 |
if ((png_ptr->num_trans > 0) && (png_ptr->bit_depth == 8)) |
| 4772 |
{ |
| 4773 |
if (png_ptr->riffled_palette == NULL) |
| 4774 |
{ |
| 4775 |
|
| 4776 |
png_ptr->riffled_palette = |
| 4777 |
(png_bytep)png_malloc(png_ptr, 256 * 4); |
| 4778 |
png_riffle_palette_neon(png_ptr); |
| 4779 |
} |
| 4780 |
} |
| 4781 |
#endif |
| 4782 |
png_do_expand_palette(png_ptr, row_info, png_ptr->row_buf + 1, |
| 4783 |
png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); |
| 4784 |
} |
| 4785 |
|
| 4786 |
else |
| 4787 |
{ |
| 4788 |
if (png_ptr->num_trans != 0 && |
| 4789 |
(png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
| 4790 |
png_do_expand(row_info, png_ptr->row_buf + 1, |
| 4791 |
&(png_ptr->trans_color)); |
| 4792 |
|
| 4793 |
else |
| 4794 |
png_do_expand(row_info, png_ptr->row_buf + 1, NULL); |
| 4795 |
} |
| 4796 |
} |
| 4797 |
#endif |
| 4798 |
|
| 4799 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 4800 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 4801 |
(png_ptr->transformations & PNG_COMPOSE) == 0 && |
| 4802 |
(row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 4803 |
row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 4804 |
png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
| 4805 |
0 ); |
| 4806 |
#endif |
| 4807 |
|
| 4808 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 4809 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
| 4810 |
{ |
| 4811 |
int rgb_error = |
| 4812 |
png_do_rgb_to_gray(png_ptr, row_info, |
| 4813 |
png_ptr->row_buf + 1); |
| 4814 |
|
| 4815 |
if (rgb_error != 0) |
| 4816 |
{ |
| 4817 |
png_ptr->rgb_to_gray_status=1; |
| 4818 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
| 4819 |
PNG_RGB_TO_GRAY_WARN) |
| 4820 |
png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 4821 |
|
| 4822 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
| 4823 |
PNG_RGB_TO_GRAY_ERR) |
| 4824 |
png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
| 4825 |
} |
| 4826 |
} |
| 4827 |
#endif |
| 4828 |
|
| 4829 |
|
| 4830 |
|
| 4831 |
|
| 4832 |
|
| 4833 |
|
| 4834 |
|
| 4835 |
|
| 4836 |
|
| 4837 |
|
| 4838 |
|
| 4839 |
|
| 4840 |
|
| 4841 |
|
| 4842 |
|
| 4843 |
|
| 4844 |
|
| 4845 |
|
| 4846 |
|
| 4847 |
|
| 4848 |
|
| 4849 |
|
| 4850 |
|
| 4851 |
|
| 4852 |
|
| 4853 |
|
| 4854 |
|
| 4855 |
|
| 4856 |
|
| 4857 |
|
| 4858 |
|
| 4859 |
|
| 4860 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 4861 |
|
| 4862 |
|
| 4863 |
|
| 4864 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
| 4865 |
(png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0) |
| 4866 |
png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
| 4867 |
#endif |
| 4868 |
|
| 4869 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 4870 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 4871 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
| 4872 |
png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4873 |
#endif |
| 4874 |
|
| 4875 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
| 4876 |
if ((png_ptr->transformations & PNG_GAMMA) != 0 && |
| 4877 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
| 4878 |
|
| 4879 |
(png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 && |
| 4880 |
#endif |
| 4881 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
| 4882 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
| 4883 |
|
| 4884 |
|
| 4885 |
|
| 4886 |
!((png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 4887 |
((png_ptr->num_trans != 0) || |
| 4888 |
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) && |
| 4889 |
#endif |
| 4890 |
|
| 4891 |
|
| 4892 |
|
| 4893 |
(png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) |
| 4894 |
png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4895 |
#endif |
| 4896 |
|
| 4897 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
| 4898 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
| 4899 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
| 4900 |
(row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
| 4901 |
row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
| 4902 |
png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
| 4903 |
0 ); |
| 4904 |
#endif |
| 4905 |
|
| 4906 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
| 4907 |
if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
| 4908 |
(row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
| 4909 |
png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); |
| 4910 |
#endif |
| 4911 |
|
| 4912 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
| 4913 |
if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
| 4914 |
png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); |
| 4915 |
#endif |
| 4916 |
|
| 4917 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
| 4918 |
|
| 4919 |
|
| 4920 |
|
| 4921 |
|
| 4922 |
if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
| 4923 |
png_do_chop(row_info, png_ptr->row_buf + 1); |
| 4924 |
#endif |
| 4925 |
|
| 4926 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
| 4927 |
if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
| 4928 |
{ |
| 4929 |
png_do_quantize(row_info, png_ptr->row_buf + 1, |
| 4930 |
png_ptr->palette_lookup, png_ptr->quantize_index); |
| 4931 |
|
| 4932 |
if (row_info->rowbytes == 0) |
| 4933 |
png_error(png_ptr, "png_do_quantize returned rowbytes=0"); |
| 4934 |
} |
| 4935 |
#endif |
| 4936 |
|
| 4937 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
| 4938 |
|
| 4939 |
|
| 4940 |
|
| 4941 |
|
| 4942 |
|
| 4943 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0) |
| 4944 |
png_do_expand_16(row_info, png_ptr->row_buf + 1); |
| 4945 |
#endif |
| 4946 |
|
| 4947 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
| 4948 |
|
| 4949 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
| 4950 |
(png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0) |
| 4951 |
png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
| 4952 |
#endif |
| 4953 |
|
| 4954 |
#ifdef PNG_READ_INVERT_SUPPORTED |
| 4955 |
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
| 4956 |
png_do_invert(row_info, png_ptr->row_buf + 1); |
| 4957 |
#endif |
| 4958 |
|
| 4959 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
| 4960 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
| 4961 |
png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); |
| 4962 |
#endif |
| 4963 |
|
| 4964 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
| 4965 |
if ((png_ptr->transformations & PNG_SHIFT) != 0) |
| 4966 |
png_do_unshift(row_info, png_ptr->row_buf + 1, |
| 4967 |
&(png_ptr->shift)); |
| 4968 |
#endif |
| 4969 |
|
| 4970 |
#ifdef PNG_READ_PACK_SUPPORTED |
| 4971 |
if ((png_ptr->transformations & PNG_PACK) != 0) |
| 4972 |
png_do_unpack(row_info, png_ptr->row_buf + 1); |
| 4973 |
#endif |
| 4974 |
|
| 4975 |
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
| 4976 |
|
| 4977 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
| 4978 |
png_ptr->num_palette_max >= 0) |
| 4979 |
png_do_check_palette_indexes(png_ptr, row_info); |
| 4980 |
#endif |
| 4981 |
|
| 4982 |
#ifdef PNG_READ_BGR_SUPPORTED |
| 4983 |
if ((png_ptr->transformations & PNG_BGR) != 0) |
| 4984 |
png_do_bgr(row_info, png_ptr->row_buf + 1); |
| 4985 |
#endif |
| 4986 |
|
| 4987 |
#ifdef PNG_READ_PACKSWAP_SUPPORTED |
| 4988 |
if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
| 4989 |
png_do_packswap(row_info, png_ptr->row_buf + 1); |
| 4990 |
#endif |
| 4991 |
|
| 4992 |
#ifdef PNG_READ_FILLER_SUPPORTED |
| 4993 |
if ((png_ptr->transformations & PNG_FILLER) != 0) |
| 4994 |
png_do_read_filler(row_info, png_ptr->row_buf + 1, |
| 4995 |
(png_uint_32)png_ptr->filler, png_ptr->flags); |
| 4996 |
#endif |
| 4997 |
|
| 4998 |
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
| 4999 |
if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) |
| 5000 |
png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); |
| 5001 |
#endif |
| 5002 |
|
| 5003 |
#ifdef PNG_READ_16BIT_SUPPORTED |
| 5004 |
#ifdef PNG_READ_SWAP_SUPPORTED |
| 5005 |
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
| 5006 |
png_do_swap(row_info, png_ptr->row_buf + 1); |
| 5007 |
#endif |
| 5008 |
#endif |
| 5009 |
|
| 5010 |
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
| 5011 |
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
| 5012 |
{ |
| 5013 |
if (png_ptr->read_user_transform_fn != NULL) |
| 5014 |
(*(png_ptr->read_user_transform_fn)) |
| 5015 |
(png_ptr, |
| 5016 |
row_info, |
| 5017 |
|
| 5018 |
|
| 5019 |
|
| 5020 |
|
| 5021 |
|
| 5022 |
|
| 5023 |
png_ptr->row_buf + 1); |
| 5024 |
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED |
| 5025 |
if (png_ptr->user_transform_depth != 0) |
| 5026 |
row_info->bit_depth = png_ptr->user_transform_depth; |
| 5027 |
|
| 5028 |
if (png_ptr->user_transform_channels != 0) |
| 5029 |
row_info->channels = png_ptr->user_transform_channels; |
| 5030 |
#endif |
| 5031 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth * |
| 5032 |
row_info->channels); |
| 5033 |
|
| 5034 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); |
| 5035 |
} |
| 5036 |
#endif |
| 5037 |
} |
| 5038 |
|
| 5039 |
#endif |
| 5040 |
#endif |