VirtualBox

source: vbox/trunk/src/libs/libpng-1.6.43/pngrtran.c@ 106945

Last change on this file since 106945 was 105469, checked in by vboxsync, 4 months ago

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

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