VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.45/pngrtran.c@ 107935

Last change on this file since 107935 was 107813, checked in by vboxsync, 3 weeks ago

libpng-1.6.45: Applied and adjusted our libpng changes to 1.6.45. bugref:8515

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette