include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / libs / png / pngrtran.c
blob1526123e025c1402a1d71b292dac1dc8f71b54f7
2 /* pngrtran.c - transforms the data in a row for PNG readers
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.
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
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.
19 #include "pngpriv.h"
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
32 #ifdef PNG_READ_SUPPORTED
34 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
35 void PNGAPI
36 png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
38 png_debug(1, "in png_set_crc_action");
40 if (png_ptr == NULL)
41 return;
43 /* Tell libpng how we react to CRC errors in critical chunks */
44 switch (crit_action)
46 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
47 break;
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;
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;
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 */
66 case PNG_CRC_DEFAULT:
67 default:
68 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
69 break;
72 /* Tell libpng how we react to CRC errors in ancillary chunks */
73 switch (ancil_action)
75 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
76 break;
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;
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;
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;
94 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
96 case PNG_CRC_DEFAULT:
97 default:
98 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
99 break;
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.
109 static int
110 png_rtran_ok(png_structrp png_ptr, int need_IHDR)
112 if (png_ptr != NULL)
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");
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");
121 else
123 /* Turn on failure to initialize correctly for all transforms. */
124 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
126 return 1; /* Ok */
130 return 0; /* no png_error possible! */
132 #endif
134 #ifdef PNG_READ_BACKGROUND_SUPPORTED
135 /* Handle alpha and tRNS via a background color */
136 void PNGFAPI
137 png_set_background_fixed(png_structrp png_ptr,
138 png_const_color_16p background_color, int background_gamma_code,
139 int need_expand, png_fixed_point background_gamma)
141 png_debug(1, "in png_set_background_fixed");
143 if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
144 return;
146 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
148 png_warning(png_ptr, "Application must supply a known background gamma");
149 return;
152 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
153 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
154 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
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;
165 # ifdef PNG_FLOATING_POINT_SUPPORTED
166 void PNGAPI
167 png_set_background(png_structrp png_ptr,
168 png_const_color_16p background_color, int background_gamma_code,
169 int need_expand, double background_gamma)
171 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
172 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
174 # endif /* FLOATING_POINT */
175 #endif /* READ_BACKGROUND */
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.
181 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
182 void PNGAPI
183 png_set_scale_16(png_structrp png_ptr)
185 png_debug(1, "in png_set_scale_16");
187 if (png_rtran_ok(png_ptr, 0) == 0)
188 return;
190 png_ptr->transformations |= PNG_SCALE_16_TO_8;
192 #endif
194 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
195 /* Chop 16-bit depth files to 8-bit depth */
196 void PNGAPI
197 png_set_strip_16(png_structrp png_ptr)
199 png_debug(1, "in png_set_strip_16");
201 if (png_rtran_ok(png_ptr, 0) == 0)
202 return;
204 png_ptr->transformations |= PNG_16_TO_8;
206 #endif
208 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
209 void PNGAPI
210 png_set_strip_alpha(png_structrp png_ptr)
212 png_debug(1, "in png_set_strip_alpha");
214 if (png_rtran_ok(png_ptr, 0) == 0)
215 return;
217 png_ptr->transformations |= PNG_STRIP_ALPHA;
219 #endif
221 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
222 static png_fixed_point
223 translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma,
224 int is_screen)
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!
231 if (output_gamma == PNG_DEFAULT_sRGB ||
232 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
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!)
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;
248 else if (output_gamma == PNG_GAMMA_MAC_18 ||
249 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
251 if (is_screen != 0)
252 output_gamma = PNG_GAMMA_MAC_OLD;
253 else
254 output_gamma = PNG_GAMMA_MAC_INVERSE;
257 return output_gamma;
260 # ifdef PNG_FLOATING_POINT_SUPPORTED
261 static png_fixed_point
262 convert_gamma_value(png_structrp png_ptr, double output_gamma)
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.
271 if (output_gamma > 0 && output_gamma < 128)
272 output_gamma *= PNG_FP_1;
274 /* This preserves -1 and -2 exactly: */
275 output_gamma = floor(output_gamma + .5);
277 if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
278 png_fixed_error(png_ptr, "gamma value");
280 return (png_fixed_point)output_gamma;
282 # endif
283 #endif /* READ_ALPHA_MODE || READ_GAMMA */
285 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
286 void PNGFAPI
287 png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
288 png_fixed_point output_gamma)
290 int compose = 0;
291 png_fixed_point file_gamma;
293 png_debug(1, "in png_set_alpha_mode_fixed");
295 if (png_rtran_ok(png_ptr, 0) == 0)
296 return;
298 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
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!
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.
308 if (output_gamma < 1000 || output_gamma > 10000000)
309 png_error(png_ptr, "output gamma out of expected range");
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:
314 file_gamma = png_reciprocal(output_gamma);
316 /* There are really 8 possibilities here, composed of any combination
317 * of:
319 * premultiply the color channels
320 * do not encode non-opaque pixels
321 * encode the alpha as well as the color channels
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.
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.
332 switch (mode)
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;
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;
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;
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;
361 default:
362 png_error(png_ptr, "invalid alpha mode");
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.)
369 if (png_ptr->colorspace.gamma == 0)
371 png_ptr->colorspace.gamma = file_gamma;
372 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
375 /* But always set the output gamma: */
376 png_ptr->screen_gamma = output_gamma;
378 /* Finally, if pre-multiplying, set the background fields to achieve the
379 * desired result.
381 if (compose != 0)
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;
389 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
390 png_error(png_ptr,
391 "conflicting calls to set alpha mode and background");
393 png_ptr->transformations |= PNG_COMPOSE;
397 # ifdef PNG_FLOATING_POINT_SUPPORTED
398 void PNGAPI
399 png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
401 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
402 output_gamma));
404 # endif
405 #endif
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.
417 typedef struct png_dsort_struct
419 struct png_dsort_struct * next;
420 png_byte left;
421 png_byte right;
422 } png_dsort;
423 typedef png_dsort * png_dsortp;
424 typedef png_dsort * * png_dsortpp;
426 void PNGAPI
427 png_set_quantize(png_structrp png_ptr, png_colorp palette,
428 int num_palette, int maximum_colors, png_const_uint_16p histogram,
429 int full_quantize)
431 png_debug(1, "in png_set_quantize");
433 if (png_rtran_ok(png_ptr, 0) == 0)
434 return;
436 png_ptr->transformations |= PNG_QUANTIZE;
438 if (full_quantize == 0)
440 int i;
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;
448 if (num_palette > maximum_colors)
450 if (histogram != NULL)
452 /* This is easy enough, just throw out the least used colors.
453 * Perhaps not the best solution, but good enough.
456 int i;
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);
462 /* Initialize the quantize_sort array */
463 for (i = 0; i < num_palette; i++)
464 png_ptr->quantize_sort[i] = (png_byte)i;
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.
473 for (i = num_palette - 1; i >= maximum_colors; i--)
475 int done; /* To stop early if the list is pre-sorted */
476 int j;
478 done = 1;
479 for (j = 0; j < i; j++)
481 if (histogram[png_ptr->quantize_sort[j]]
482 < histogram[png_ptr->quantize_sort[j + 1]])
484 png_byte t;
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;
493 if (done != 0)
494 break;
497 /* Swap the palette around, and set up a table, if necessary */
498 if (full_quantize != 0)
500 int j = num_palette;
502 /* Put all the useful colors within the max, but don't
503 * move the others.
505 for (i = 0; i < maximum_colors; i++)
507 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
510 j--;
511 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
513 palette[i] = palette[j];
517 else
519 int j = num_palette;
521 /* Move all the used colors inside the max limit, and
522 * develop a translation table.
524 for (i = 0; i < maximum_colors; i++)
526 /* Only move the colors we need to */
527 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
529 png_color tmp_color;
532 j--;
533 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
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;
544 /* Find closest color for those colors we are not using */
545 for (i = 0; i < num_palette; i++)
547 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
549 int min_d, k, min_k, d_index;
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++)
556 int d;
558 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
560 if (d < min_d)
562 min_d = d;
563 min_k = k;
566 /* Point to closest color */
567 png_ptr->quantize_index[i] = (png_byte)min_k;
571 png_free(png_ptr, png_ptr->quantize_sort);
572 png_ptr->quantize_sort = NULL;
574 else
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]
584 int i;
585 int max_d;
586 int num_new_palette;
587 png_dsortp t;
588 png_dsortpp hash;
590 t = NULL;
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);
598 /* Initialize the sort array */
599 for (i = 0; i < num_palette; i++)
601 png_ptr->index_to_palette[i] = (png_byte)i;
602 png_ptr->palette_to_index[i] = (png_byte)i;
605 hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 *
606 (sizeof (png_dsortp))));
608 num_new_palette = num_palette;
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.
616 * I have not done extensive checking on this number.
618 max_d = 96;
620 while (num_new_palette > maximum_colors)
622 for (i = 0; i < num_new_palette - 1; i++)
624 int j;
626 for (j = i + 1; j < num_new_palette; j++)
628 int d;
630 d = PNG_COLOR_DIST(palette[i], palette[j]);
632 if (d <= max_d)
635 t = (png_dsortp)png_malloc_warn(png_ptr,
636 (png_alloc_size_t)(sizeof (png_dsort)));
638 if (t == NULL)
639 break;
641 t->next = hash[d];
642 t->left = (png_byte)i;
643 t->right = (png_byte)j;
644 hash[d] = t;
647 if (t == NULL)
648 break;
651 if (t != NULL)
652 for (i = 0; i <= max_d; i++)
654 if (hash[i] != NULL)
656 png_dsortp p;
658 for (p = hash[i]; p; p = p->next)
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)
665 int j, next_j;
667 if (num_new_palette & 0x01)
669 j = p->left;
670 next_j = p->right;
672 else
674 j = p->right;
675 next_j = p->left;
678 num_new_palette--;
679 palette[png_ptr->index_to_palette[j]]
680 = palette[num_new_palette];
681 if (full_quantize == 0)
683 int k;
685 for (k = 0; k < num_palette; k++)
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];
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];
699 png_ptr->index_to_palette[png_ptr->palette_to_index
700 [num_new_palette]] = png_ptr->index_to_palette[j];
702 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
703 = png_ptr->palette_to_index[num_new_palette];
705 png_ptr->index_to_palette[j] =
706 (png_byte)num_new_palette;
708 png_ptr->palette_to_index[num_new_palette] =
709 (png_byte)j;
711 if (num_new_palette <= maximum_colors)
712 break;
714 if (num_new_palette <= maximum_colors)
715 break;
719 for (i = 0; i < 769; i++)
721 if (hash[i] != NULL)
723 png_dsortp p = hash[i];
724 while (p)
726 t = p->next;
727 png_free(png_ptr, p);
728 p = t;
731 hash[i] = 0;
733 max_d += 96;
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;
741 num_palette = maximum_colors;
743 if (png_ptr->palette == NULL)
745 png_ptr->palette = palette;
747 png_ptr->num_palette = (png_uint_16)num_palette;
749 if (full_quantize != 0)
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);
760 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
761 (png_alloc_size_t)(num_entries));
763 distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)num_entries);
765 memset(distance, 0xff, num_entries);
767 for (i = 0; i < num_palette; i++)
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));
774 for (ir = 0; ir < num_red; ir++)
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));
781 for (ig = 0; ig < num_green; ig++)
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);
789 for (ib = 0; ib < num_blue; ib++)
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;
797 if (d < (int)distance[d_index])
799 distance[d_index] = (png_byte)d;
800 png_ptr->palette_lookup[d_index] = (png_byte)i;
807 png_free(png_ptr, distance);
810 #endif /* READ_QUANTIZE */
812 #ifdef PNG_READ_GAMMA_SUPPORTED
813 void PNGFAPI
814 png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
815 png_fixed_point file_gamma)
817 png_debug(1, "in png_set_gamma_fixed");
819 if (png_rtran_ok(png_ptr, 0) == 0)
820 return;
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*/);
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.
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.
837 if (file_gamma <= 0)
838 png_error(png_ptr, "invalid file gamma in png_set_gamma");
840 if (scrn_gamma <= 0)
841 png_error(png_ptr, "invalid screen gamma in png_set_gamma");
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.
847 png_ptr->colorspace.gamma = file_gamma;
848 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
849 png_ptr->screen_gamma = scrn_gamma;
852 # ifdef PNG_FLOATING_POINT_SUPPORTED
853 void PNGAPI
854 png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
856 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
857 convert_gamma_value(png_ptr, file_gamma));
859 # endif /* FLOATING_POINT */
860 #endif /* READ_GAMMA */
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.
867 void PNGAPI
868 png_set_expand(png_structrp png_ptr)
870 png_debug(1, "in png_set_expand");
872 if (png_rtran_ok(png_ptr, 0) == 0)
873 return;
875 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
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.
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.
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().
896 /* Expand paletted images to RGB. */
897 void PNGAPI
898 png_set_palette_to_rgb(png_structrp png_ptr)
900 png_debug(1, "in png_set_palette_to_rgb");
902 if (png_rtran_ok(png_ptr, 0) == 0)
903 return;
905 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
908 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
909 void PNGAPI
910 png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
912 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
914 if (png_rtran_ok(png_ptr, 0) == 0)
915 return;
917 png_ptr->transformations |= PNG_EXPAND;
920 /* Expand tRNS chunks to alpha channels. */
921 void PNGAPI
922 png_set_tRNS_to_alpha(png_structrp png_ptr)
924 png_debug(1, "in png_set_tRNS_to_alpha");
926 if (png_rtran_ok(png_ptr, 0) == 0)
927 return;
929 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
931 #endif /* READ_EXPAND */
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.)
937 void PNGAPI
938 png_set_expand_16(png_structrp png_ptr)
940 png_debug(1, "in png_set_expand_16");
942 if (png_rtran_ok(png_ptr, 0) == 0)
943 return;
945 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
947 #endif
949 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
950 void PNGAPI
951 png_set_gray_to_rgb(png_structrp png_ptr)
953 png_debug(1, "in png_set_gray_to_rgb");
955 if (png_rtran_ok(png_ptr, 0) == 0)
956 return;
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;
962 #endif
964 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
965 void PNGFAPI
966 png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
967 png_fixed_point red, png_fixed_point green)
969 png_debug(1, "in png_set_rgb_to_gray_fixed");
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;
976 switch (error_action)
978 case PNG_ERROR_ACTION_NONE:
979 png_ptr->transformations |= PNG_RGB_TO_GRAY;
980 break;
982 case PNG_ERROR_ACTION_WARN:
983 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
984 break;
986 case PNG_ERROR_ACTION_ERROR:
987 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
988 break;
990 default:
991 png_error(png_ptr, "invalid error action to rgb_to_gray");
994 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
995 #ifdef PNG_READ_EXPAND_SUPPORTED
996 png_ptr->transformations |= PNG_EXPAND;
997 #else
999 /* Make this an error in 1.6 because otherwise the application may assume
1000 * that it just worked and get a memory overwrite.
1002 png_error(png_ptr,
1003 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
1005 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
1007 #endif
1009 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
1011 png_uint_16 red_int, green_int;
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.
1018 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
1019 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
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;
1026 else
1028 if (red >= 0 && green >= 0)
1029 png_app_warning(png_ptr,
1030 "ignoring out of range rgb_to_gray coefficients");
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.
1038 if (png_ptr->rgb_to_gray_red_coeff == 0 &&
1039 png_ptr->rgb_to_gray_green_coeff == 0)
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; */
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.
1054 void PNGAPI
1055 png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red,
1056 double green)
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"));
1062 #endif /* FLOATING POINT */
1064 #endif /* RGB_TO_GRAY */
1066 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
1067 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1068 void PNGAPI
1069 png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
1070 read_user_transform_fn)
1072 png_debug(1, "in png_set_read_user_transform_fn");
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
1079 #endif
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.
1087 static int /* PRIVATE */
1088 png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
1090 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1091 * correction as a difference of the overall transform from 1.0
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.
1097 png_fixed_point gtest;
1098 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1099 png_gamma_significant(gtest);
1101 #endif
1103 /* Initialize everything needed for the read. This includes modifying
1104 * the palette.
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'.
1112 static void /* PRIVATE */
1113 png_init_palette_transformations(png_structrp png_ptr)
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.
1120 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1121 * to 0.)
1123 int input_has_alpha = 0;
1124 int input_has_transparency = 0;
1126 if (png_ptr->num_trans > 0)
1128 int i;
1130 /* Ignore if all the entries are opaque (unlikely!) */
1131 for (i=0; i<png_ptr->num_trans; ++i)
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
1139 input_has_transparency = 1;
1140 input_has_alpha = 1;
1141 break;
1146 /* If no alpha we can optimize. */
1147 if (input_has_alpha == 0)
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.
1153 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1154 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1156 if (input_has_transparency == 0)
1157 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
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.
1166 /* The following code cannot be entered in the alpha pre-multiplication case
1167 * because PNG_BACKGROUND_EXPAND is cancelled below.
1169 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1170 (png_ptr->transformations & PNG_EXPAND) != 0)
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;
1180 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1181 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
1183 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1185 /* Invert the alpha channel (in tRNS) unless the pixels are
1186 * going to be expanded, in which case leave it for later
1188 int i, istop = png_ptr->num_trans;
1190 for (i = 0; i < istop; i++)
1191 png_ptr->trans_alpha[i] =
1192 (png_byte)(255 - png_ptr->trans_alpha[i]);
1195 #endif /* READ_INVERT_ALPHA */
1197 } /* background expand and (therefore) no alpha association. */
1198 #endif /* READ_EXPAND && READ_BACKGROUND */
1201 static void /* PRIVATE */
1202 png_init_rgb_transformations(png_structrp png_ptr)
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.
1208 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1209 int input_has_transparency = png_ptr->num_trans > 0;
1211 /* If no alpha we can optimize. */
1212 if (input_has_alpha == 0)
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.
1218 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1219 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1220 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1221 # endif
1223 if (input_has_transparency == 0)
1224 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
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.
1233 /* The following code cannot be entered in the alpha pre-multiplication case
1234 * because PNG_BACKGROUND_EXPAND is cancelled below.
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 */
1242 /* Expand background and tRNS chunks */
1243 int gray = png_ptr->background.gray;
1244 int trans_gray = png_ptr->trans_color.gray;
1246 switch (png_ptr->bit_depth)
1248 case 1:
1249 gray *= 0xff;
1250 trans_gray *= 0xff;
1251 break;
1253 case 2:
1254 gray *= 0x55;
1255 trans_gray *= 0x55;
1256 break;
1258 case 4:
1259 gray *= 0x11;
1260 trans_gray *= 0x11;
1261 break;
1263 default:
1265 case 8:
1266 /* FALLTHROUGH */ /* (Already 8 bits) */
1268 case 16:
1269 /* Already a full 16 bits */
1270 break;
1273 png_ptr->background.red = png_ptr->background.green =
1274 png_ptr->background.blue = (png_uint_16)gray;
1276 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1278 png_ptr->trans_color.red = png_ptr->trans_color.green =
1279 png_ptr->trans_color.blue = (png_uint_16)trans_gray;
1282 } /* background expand and (therefore) no alpha association. */
1283 #endif /* READ_EXPAND && READ_BACKGROUND */
1286 void /* PRIVATE */
1287 png_init_read_transformations(png_structrp png_ptr)
1289 png_debug(1, "in png_init_read_transformations");
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.
1295 * First do updates that do not depend on the details of the PNG image data
1296 * being processed.
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.)
1307 /* The following temporary indicates if overall gamma correction is
1308 * required.
1310 int gamma_correction = 0;
1312 if (png_ptr->colorspace.gamma != 0) /* has been set */
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);
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.
1322 png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma);
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.)
1331 png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma);
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
1339 png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1;
1341 /* We have a gamma value now. */
1342 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
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.
1352 if (gamma_correction != 0)
1353 png_ptr->transformations |= PNG_GAMMA;
1355 else
1356 png_ptr->transformations &= ~PNG_GAMMA;
1358 #endif
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:
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]
1389 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1390 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1391 (png_ptr->transformations & PNG_COMPOSE) == 0)
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:
1398 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1399 PNG_EXPAND_tRNS);
1400 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
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:
1409 png_ptr->num_trans = 0;
1411 #endif /* STRIP_ALPHA supported, no COMPOSE */
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.
1417 if (png_gamma_significant(png_ptr->screen_gamma) == 0)
1419 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1420 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1422 #endif
1424 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1425 /* Make sure the coefficients for the rgb to gray conversion are set
1426 * appropriately.
1428 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1429 png_colorspace_set_rgb_coefficients(png_ptr);
1430 #endif
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.
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.
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.
1449 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
1451 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1452 * the file was grayscale the background value is gray.
1454 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1455 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1458 else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
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.
1465 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
1467 if (png_ptr->background.red == png_ptr->background.green &&
1468 png_ptr->background.red == png_ptr->background.blue)
1470 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1471 png_ptr->background.gray = png_ptr->background.red;
1475 #endif /* READ_EXPAND && READ_BACKGROUND */
1476 #endif /* READ_GRAY_TO_RGB */
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.
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.
1489 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1490 png_init_palette_transformations(png_ptr);
1492 else
1493 png_init_rgb_transformations(png_ptr);
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)
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.
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.)
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!
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
1519 #endif /* READ_BACKGROUND && READ_EXPAND_16 */
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)
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.
1533 * TODO: fix this too.
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);
1541 #endif
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.)
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
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.
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.
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))
1584 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1586 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1587 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
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.
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");
1599 if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
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.
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)
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];
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];
1619 else
1621 png_fixed_point g, gs;
1623 switch (png_ptr->background_gamma_type)
1625 case PNG_BACKGROUND_GAMMA_SCREEN:
1626 g = (png_ptr->screen_gamma);
1627 gs = PNG_FP_1;
1628 break;
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;
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;
1647 if (png_gamma_significant(gs) != 0)
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);
1657 else
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;
1664 if (png_gamma_significant(g) != 0)
1666 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
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,
1674 else
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;
1682 for (i = 0; i < num_palette; i++)
1684 if (i < (int)png_ptr->num_trans &&
1685 png_ptr->trans_alpha[i] != 0xff)
1687 if (png_ptr->trans_alpha[i] == 0)
1689 palette[i] = back;
1691 else /* if (png_ptr->trans_alpha[i] != 0xff) */
1693 png_byte v, w;
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];
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];
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];
1708 else
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];
1716 /* Prevent the transformations being done again.
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.
1722 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1723 } /* color_type == PNG_COLOR_TYPE_PALETTE */
1725 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1726 else /* color_type != PNG_COLOR_TYPE_PALETTE */
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 */
1732 switch (png_ptr->background_gamma_type)
1734 case PNG_BACKGROUND_GAMMA_SCREEN:
1735 g = png_ptr->screen_gamma;
1736 /* gs = PNG_FP_1; */
1737 break;
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;
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;
1751 default:
1752 png_error(png_ptr, "invalid background gamma type");
1755 g_sig = png_gamma_significant(g);
1756 gs_sig = png_gamma_significant(gs);
1758 if (g_sig != 0)
1759 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1760 png_ptr->background.gray, g);
1762 if (gs_sig != 0)
1763 png_ptr->background.gray = png_gamma_correct(png_ptr,
1764 png_ptr->background.gray, gs);
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))
1770 /* RGB or RGBA with color background */
1771 if (g_sig != 0)
1773 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1774 png_ptr->background.red, g);
1776 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1777 png_ptr->background.green, g);
1779 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1780 png_ptr->background.blue, g);
1783 if (gs_sig != 0)
1785 png_ptr->background.red = png_gamma_correct(png_ptr,
1786 png_ptr->background.red, gs);
1788 png_ptr->background.green = png_gamma_correct(png_ptr,
1789 png_ptr->background.green, gs);
1791 png_ptr->background.blue = png_gamma_correct(png_ptr,
1792 png_ptr->background.blue, gs);
1796 else
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;
1802 png_ptr->background.red = png_ptr->background.green
1803 = png_ptr->background.blue = png_ptr->background.gray;
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 */
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
1822 png_colorp palette = png_ptr->palette;
1823 int num_palette = png_ptr->num_palette;
1824 int i;
1826 /* NOTE: there are other transformations that should probably be in
1827 * here too.
1829 for (i = 0; i < num_palette; i++)
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];
1836 /* Done the gamma correction. */
1837 png_ptr->transformations &= ~PNG_GAMMA;
1838 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1840 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1841 else
1842 #endif
1843 #endif /* READ_GAMMA */
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))
1850 int i;
1851 int istop = (int)png_ptr->num_trans;
1852 png_color back;
1853 png_colorp palette = png_ptr->palette;
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;
1859 for (i = 0; i < istop; i++)
1861 if (png_ptr->trans_alpha[i] == 0)
1863 palette[i] = back;
1866 else if (png_ptr->trans_alpha[i] != 0xff)
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);
1872 png_composite(palette[i].green, palette[i].green,
1873 png_ptr->trans_alpha[i], back.green);
1875 png_composite(palette[i].blue, palette[i].blue,
1876 png_ptr->trans_alpha[i], back.blue);
1880 png_ptr->transformations &= ~PNG_COMPOSE;
1882 #endif /* READ_BACKGROUND */
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))
1889 int i;
1890 int istop = png_ptr->num_palette;
1891 int shift = 8 - png_ptr->sig_bit.red;
1893 png_ptr->transformations &= ~PNG_SHIFT;
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.)
1899 if (shift > 0 && shift < 8)
1900 for (i=0; i<istop; ++i)
1902 int component = png_ptr->palette[i].red;
1904 component >>= shift;
1905 png_ptr->palette[i].red = (png_byte)component;
1908 shift = 8 - png_ptr->sig_bit.green;
1909 if (shift > 0 && shift < 8)
1910 for (i=0; i<istop; ++i)
1912 int component = png_ptr->palette[i].green;
1914 component >>= shift;
1915 png_ptr->palette[i].green = (png_byte)component;
1918 shift = 8 - png_ptr->sig_bit.blue;
1919 if (shift > 0 && shift < 8)
1920 for (i=0; i<istop; ++i)
1922 int component = png_ptr->palette[i].blue;
1924 component >>= shift;
1925 png_ptr->palette[i].blue = (png_byte)component;
1928 #endif /* READ_SHIFT */
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.
1935 void /* PRIVATE */
1936 png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
1938 png_debug(1, "in png_read_transform_info");
1940 #ifdef PNG_READ_EXPAND_SUPPORTED
1941 if ((png_ptr->transformations & PNG_EXPAND) != 0)
1943 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
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.)
1949 if (png_ptr->num_trans > 0)
1950 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1952 else
1953 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1955 info_ptr->bit_depth = 8;
1956 info_ptr->num_trans = 0;
1958 if (png_ptr->palette == NULL)
1959 png_error (png_ptr, "Palette is NULL in indexed image");
1961 else
1963 if (png_ptr->num_trans != 0)
1965 if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
1966 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1968 if (info_ptr->bit_depth < 8)
1969 info_ptr->bit_depth = 8;
1971 info_ptr->num_trans = 0;
1974 #endif
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!
1981 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1982 info_ptr->background = png_ptr->background;
1983 #endif
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.
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.
1995 info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
1996 #endif
1998 if (info_ptr->bit_depth == 16)
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
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
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.
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.
2021 png_ptr->transformations |= PNG_16_TO_8;
2022 info_ptr->bit_depth = 8;
2023 # else
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
2030 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
2031 # endif
2032 # endif
2033 #endif /* !READ_16BIT */
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
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
2048 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2049 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
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)
2055 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2058 #endif
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)
2065 info_ptr->bit_depth = 16;
2067 #endif
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
2075 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2076 info_ptr->channels = 1;
2078 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
2079 info_ptr->channels = 3;
2081 else
2082 info_ptr->channels = 1;
2084 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2085 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
2087 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2088 ~PNG_COLOR_MASK_ALPHA);
2089 info_ptr->num_trans = 0;
2091 #endif
2093 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
2094 info_ptr->channels++;
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))
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;
2107 #endif
2109 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2110 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2111 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
2113 if (png_ptr->user_transform_depth != 0)
2114 info_ptr->bit_depth = png_ptr->user_transform_depth;
2116 if (png_ptr->user_transform_channels != 0)
2117 info_ptr->channels = png_ptr->user_transform_channels;
2119 #endif
2121 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2122 info_ptr->bit_depth);
2124 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
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.
2132 png_ptr->info_rowbytes = info_ptr->rowbytes;
2134 #ifndef PNG_READ_EXPAND_SUPPORTED
2135 if (png_ptr != NULL)
2136 return;
2137 #endif
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.
2147 static void
2148 png_do_unpack(png_row_infop row_info, png_bytep row)
2150 png_debug(1, "in png_do_unpack");
2152 if (row_info->bit_depth < 8)
2154 png_uint_32 i;
2155 png_uint_32 row_width=row_info->width;
2157 switch (row_info->bit_depth)
2159 case 1:
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++)
2166 *dp = (png_byte)((*sp >> shift) & 0x01);
2168 if (shift == 7)
2170 shift = 0;
2171 sp--;
2174 else
2175 shift++;
2177 dp--;
2179 break;
2182 case 2:
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++)
2190 *dp = (png_byte)((*sp >> shift) & 0x03);
2192 if (shift == 6)
2194 shift = 0;
2195 sp--;
2198 else
2199 shift += 2;
2201 dp--;
2203 break;
2206 case 4:
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++)
2213 *dp = (png_byte)((*sp >> shift) & 0x0f);
2215 if (shift == 4)
2217 shift = 0;
2218 sp--;
2221 else
2222 shift = 4;
2224 dp--;
2226 break;
2229 default:
2230 break;
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;
2237 #endif
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.
2245 static void
2246 png_do_unshift(png_row_infop row_info, png_bytep row,
2247 png_const_color_8p sig_bits)
2249 int color_type;
2251 png_debug(1, "in png_do_unshift");
2253 /* The palette case has already been handled in the _init routine. */
2254 color_type = row_info->color_type;
2256 if (color_type != PNG_COLOR_TYPE_PALETTE)
2258 int shift[4];
2259 int channels = 0;
2260 int bit_depth = row_info->bit_depth;
2262 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
2264 shift[channels++] = bit_depth - sig_bits->red;
2265 shift[channels++] = bit_depth - sig_bits->green;
2266 shift[channels++] = bit_depth - sig_bits->blue;
2269 else
2271 shift[channels++] = bit_depth - sig_bits->gray;
2274 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
2276 shift[channels++] = bit_depth - sig_bits->alpha;
2280 int c, have_shift;
2282 for (c = have_shift = 0; c < channels; ++c)
2284 /* A shift of more than the bit depth is an error condition but it
2285 * gets ignored here.
2287 if (shift[c] <= 0 || shift[c] >= bit_depth)
2288 shift[c] = 0;
2290 else
2291 have_shift = 1;
2294 if (have_shift == 0)
2295 return;
2298 switch (bit_depth)
2300 default:
2301 /* Must be 1bpp gray: should not be here! */
2302 /* NOTREACHED */
2303 break;
2305 case 2:
2306 /* Must be 2bpp gray */
2307 /* assert(channels == 1 && shift[0] == 1) */
2309 png_bytep bp = row;
2310 png_bytep bp_end = bp + row_info->rowbytes;
2312 while (bp < bp_end)
2314 int b = (*bp >> 1) & 0x55;
2315 *bp++ = (png_byte)b;
2317 break;
2320 case 4:
2321 /* Must be 4bpp gray */
2322 /* assert(channels == 1) */
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;
2329 mask |= mask << 4;
2331 while (bp < bp_end)
2333 int b = (*bp >> gray_shift) & mask;
2334 *bp++ = (png_byte)b;
2336 break;
2339 case 8:
2340 /* Single byte components, G, GA, RGB, RGBA */
2342 png_bytep bp = row;
2343 png_bytep bp_end = bp + row_info->rowbytes;
2344 int channel = 0;
2346 while (bp < bp_end)
2348 int b = *bp >> shift[channel];
2349 if (++channel >= channels)
2350 channel = 0;
2351 *bp++ = (png_byte)b;
2353 break;
2356 #ifdef PNG_READ_16BIT_SUPPORTED
2357 case 16:
2358 /* Double byte components, G, GA, RGB, RGBA */
2360 png_bytep bp = row;
2361 png_bytep bp_end = bp + row_info->rowbytes;
2362 int channel = 0;
2364 while (bp < bp_end)
2366 int value = (bp[0] << 8) + bp[1];
2368 value >>= shift[channel];
2369 if (++channel >= channels)
2370 channel = 0;
2371 *bp++ = (png_byte)(value >> 8);
2372 *bp++ = (png_byte)value;
2374 break;
2376 #endif
2380 #endif
2382 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2383 /* Scale rows of bit depth 16 down to 8 accurately */
2384 static void
2385 png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
2387 png_debug(1, "in png_do_scale_16_to_8");
2389 if (row_info->bit_depth == 16)
2391 png_bytep sp = row; /* source */
2392 png_bytep dp = row; /* destination */
2393 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2395 while (sp < ep)
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:
2401 * (V * 255) / 65535
2403 * This reduces to round(V / 257), or floor((V + 128.5)/257)
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:
2409 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2410 * = floor(((vlo-vhi) + 128.5) / 257)
2412 * This can be approximated using integer arithmetic (and a signed
2413 * shift):
2415 * error = (vlo-vhi+128) >> 8;
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:
2422 * error = (vlo-vhi+128)*65535 >> 24;
2424 * An alternative arithmetic calculation which also gives no errors is:
2426 * (V * 255 + 32895) >> 16
2429 png_int_32 tmp = *sp++; /* must be signed! */
2430 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
2431 *dp++ = (png_byte)tmp;
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;
2439 #endif
2441 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2442 static void
2443 /* Simply discard the low byte. This was the default behavior prior
2444 * to libpng-1.5.4.
2446 png_do_chop(png_row_infop row_info, png_bytep row)
2448 png_debug(1, "in png_do_chop");
2450 if (row_info->bit_depth == 16)
2452 png_bytep sp = row; /* source */
2453 png_bytep dp = row; /* destination */
2454 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2456 while (sp < ep)
2458 *dp++ = *sp;
2459 sp += 2; /* skip low byte */
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;
2467 #endif
2469 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2470 static void
2471 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2473 png_uint_32 row_width = row_info->width;
2475 png_debug(1, "in png_do_read_swap_alpha");
2477 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2479 /* This converts from RGBA to ARGB */
2480 if (row_info->bit_depth == 8)
2482 png_bytep sp = row + row_info->rowbytes;
2483 png_bytep dp = sp;
2484 png_byte save;
2485 png_uint_32 i;
2487 for (i = 0; i < row_width; i++)
2489 save = *(--sp);
2490 *(--dp) = *(--sp);
2491 *(--dp) = *(--sp);
2492 *(--dp) = *(--sp);
2493 *(--dp) = save;
2497 #ifdef PNG_READ_16BIT_SUPPORTED
2498 /* This converts from RRGGBBAA to AARRGGBB */
2499 else
2501 png_bytep sp = row + row_info->rowbytes;
2502 png_bytep dp = sp;
2503 png_byte save[2];
2504 png_uint_32 i;
2506 for (i = 0; i < row_width; i++)
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];
2520 #endif
2523 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2525 /* This converts from GA to AG */
2526 if (row_info->bit_depth == 8)
2528 png_bytep sp = row + row_info->rowbytes;
2529 png_bytep dp = sp;
2530 png_byte save;
2531 png_uint_32 i;
2533 for (i = 0; i < row_width; i++)
2535 save = *(--sp);
2536 *(--dp) = *(--sp);
2537 *(--dp) = save;
2541 #ifdef PNG_READ_16BIT_SUPPORTED
2542 /* This converts from GGAA to AAGG */
2543 else
2545 png_bytep sp = row + row_info->rowbytes;
2546 png_bytep dp = sp;
2547 png_byte save[2];
2548 png_uint_32 i;
2550 for (i = 0; i < row_width; i++)
2552 save[0] = *(--sp);
2553 save[1] = *(--sp);
2554 *(--dp) = *(--sp);
2555 *(--dp) = *(--sp);
2556 *(--dp) = save[0];
2557 *(--dp) = save[1];
2560 #endif
2563 #endif
2565 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2566 static void
2567 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2569 png_uint_32 row_width;
2570 png_debug(1, "in png_do_read_invert_alpha");
2572 row_width = row_info->width;
2573 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2575 if (row_info->bit_depth == 8)
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;
2582 for (i = 0; i < row_width; i++)
2584 *(--dp) = (png_byte)(255 - *(--sp));
2586 /* This does nothing:
2587 *(--dp) = *(--sp);
2588 *(--dp) = *(--sp);
2589 *(--dp) = *(--sp);
2590 We can replace it with:
2592 sp-=3;
2593 dp=sp;
2597 #ifdef PNG_READ_16BIT_SUPPORTED
2598 /* This inverts the alpha channel in RRGGBBAA */
2599 else
2601 png_bytep sp = row + row_info->rowbytes;
2602 png_bytep dp = sp;
2603 png_uint_32 i;
2605 for (i = 0; i < row_width; i++)
2607 *(--dp) = (png_byte)(255 - *(--sp));
2608 *(--dp) = (png_byte)(255 - *(--sp));
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:
2619 sp-=6;
2620 dp=sp;
2623 #endif
2625 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2627 if (row_info->bit_depth == 8)
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;
2634 for (i = 0; i < row_width; i++)
2636 *(--dp) = (png_byte)(255 - *(--sp));
2637 *(--dp) = *(--sp);
2641 #ifdef PNG_READ_16BIT_SUPPORTED
2642 else
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;
2649 for (i = 0; i < row_width; i++)
2651 *(--dp) = (png_byte)(255 - *(--sp));
2652 *(--dp) = (png_byte)(255 - *(--sp));
2654 *(--dp) = *(--sp);
2655 *(--dp) = *(--sp);
2657 sp-=2;
2658 dp=sp;
2661 #endif
2664 #endif
2666 #ifdef PNG_READ_FILLER_SUPPORTED
2667 /* Add filler channel if we have RGB color */
2668 static void
2669 png_do_read_filler(png_row_infop row_info, png_bytep row,
2670 png_uint_32 filler, png_uint_32 flags)
2672 png_uint_32 i;
2673 png_uint_32 row_width = row_info->width;
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;
2680 png_debug(1, "in png_do_read_filler");
2682 if (
2683 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2685 if (row_info->bit_depth == 8)
2687 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
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++)
2694 *(--dp) = lo_filler;
2695 *(--dp) = *(--sp);
2697 *(--dp) = lo_filler;
2698 row_info->channels = 2;
2699 row_info->pixel_depth = 16;
2700 row_info->rowbytes = row_width * 2;
2703 else
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++)
2710 *(--dp) = *(--sp);
2711 *(--dp) = lo_filler;
2713 row_info->channels = 2;
2714 row_info->pixel_depth = 16;
2715 row_info->rowbytes = row_width * 2;
2719 #ifdef PNG_READ_16BIT_SUPPORTED
2720 else if (row_info->bit_depth == 16)
2722 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
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++)
2729 *(--dp) = lo_filler;
2730 *(--dp) = hi_filler;
2731 *(--dp) = *(--sp);
2732 *(--dp) = *(--sp);
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;
2741 else
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++)
2748 *(--dp) = *(--sp);
2749 *(--dp) = *(--sp);
2750 *(--dp) = lo_filler;
2751 *(--dp) = hi_filler;
2753 row_info->channels = 2;
2754 row_info->pixel_depth = 32;
2755 row_info->rowbytes = row_width * 4;
2758 #endif
2759 } /* COLOR_TYPE == GRAY */
2760 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2762 if (row_info->bit_depth == 8)
2764 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
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++)
2771 *(--dp) = lo_filler;
2772 *(--dp) = *(--sp);
2773 *(--dp) = *(--sp);
2774 *(--dp) = *(--sp);
2776 *(--dp) = lo_filler;
2777 row_info->channels = 4;
2778 row_info->pixel_depth = 32;
2779 row_info->rowbytes = row_width * 4;
2782 else
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++)
2789 *(--dp) = *(--sp);
2790 *(--dp) = *(--sp);
2791 *(--dp) = *(--sp);
2792 *(--dp) = lo_filler;
2794 row_info->channels = 4;
2795 row_info->pixel_depth = 32;
2796 row_info->rowbytes = row_width * 4;
2800 #ifdef PNG_READ_16BIT_SUPPORTED
2801 else if (row_info->bit_depth == 16)
2803 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
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++)
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);
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;
2826 else
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++)
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;
2843 row_info->channels = 4;
2844 row_info->pixel_depth = 64;
2845 row_info->rowbytes = row_width * 8;
2848 #endif
2849 } /* COLOR_TYPE == RGB */
2851 #endif
2853 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2854 /* Expand grayscale files to RGB, with or without alpha */
2855 static void
2856 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2858 png_uint_32 i;
2859 png_uint_32 row_width = row_info->width;
2861 png_debug(1, "in png_do_gray_to_rgb");
2863 if (row_info->bit_depth >= 8 &&
2864 (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
2866 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2868 if (row_info->bit_depth == 8)
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++)
2875 *(dp--) = *sp;
2876 *(dp--) = *sp;
2877 *(dp--) = *(sp--);
2881 else
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++)
2888 *(dp--) = *sp;
2889 *(dp--) = *(sp - 1);
2890 *(dp--) = *sp;
2891 *(dp--) = *(sp - 1);
2892 *(dp--) = *(sp--);
2893 *(dp--) = *(sp--);
2898 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2900 if (row_info->bit_depth == 8)
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++)
2907 *(dp--) = *(sp--);
2908 *(dp--) = *sp;
2909 *(dp--) = *sp;
2910 *(dp--) = *(sp--);
2914 else
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++)
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--);
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);
2939 #endif
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
2950 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2952 * which can be expressed with integers as
2954 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
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:
2960 * Y = 0.2126*R + 0.7152*G + 0.0722*B
2962 * which can be expressed with integers as
2964 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
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:
2972 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
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:
2978 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
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.
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.
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.
2994 static int
2995 png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
2997 int rgb_error = 0;
2999 png_debug(1, "in png_do_rgb_to_gray");
3001 if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3002 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
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;
3010 if (row_info->bit_depth == 8)
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.
3018 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3020 png_bytep sp = row;
3021 png_bytep dp = row;
3022 png_uint_32 i;
3024 for (i = 0; i < row_width; i++)
3026 png_byte red = *(sp++);
3027 png_byte green = *(sp++);
3028 png_byte blue = *(sp++);
3030 if (red != green || red != blue)
3032 red = png_ptr->gamma_to_1[red];
3033 green = png_ptr->gamma_to_1[green];
3034 blue = png_ptr->gamma_to_1[blue];
3036 rgb_error |= 1;
3037 *(dp++) = png_ptr->gamma_from_1[
3038 (rc*red + gc*green + bc*blue + 16384)>>15];
3041 else
3043 /* If there is no overall correction the table will not be
3044 * set.
3046 if (png_ptr->gamma_table != NULL)
3047 red = png_ptr->gamma_table[red];
3049 *(dp++) = red;
3052 if (have_alpha != 0)
3053 *(dp++) = *(sp++);
3056 else
3057 #endif
3059 png_bytep sp = row;
3060 png_bytep dp = row;
3061 png_uint_32 i;
3063 for (i = 0; i < row_width; i++)
3065 png_byte red = *(sp++);
3066 png_byte green = *(sp++);
3067 png_byte blue = *(sp++);
3069 if (red != green || red != blue)
3071 rgb_error |= 1;
3072 /* NOTE: this is the historical approach which simply
3073 * truncates the results.
3075 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3078 else
3079 *(dp++) = red;
3081 if (have_alpha != 0)
3082 *(dp++) = *(sp++);
3087 else /* RGB bit_depth == 16 */
3089 #ifdef PNG_READ_GAMMA_SUPPORTED
3090 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3092 png_bytep sp = row;
3093 png_bytep dp = row;
3094 png_uint_32 i;
3096 for (i = 0; i < row_width; i++)
3098 png_uint_16 red, green, blue, w;
3099 png_byte hi,lo;
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));
3105 if (red == green && red == blue)
3107 if (png_ptr->gamma_16_table != NULL)
3108 w = png_ptr->gamma_16_table[(red & 0xff)
3109 >> png_ptr->gamma_shift][red >> 8];
3111 else
3112 w = red;
3115 else
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;
3131 *(dp++) = (png_byte)((w>>8) & 0xff);
3132 *(dp++) = (png_byte)(w & 0xff);
3134 if (have_alpha != 0)
3136 *(dp++) = *(sp++);
3137 *(dp++) = *(sp++);
3141 else
3142 #endif
3144 png_bytep sp = row;
3145 png_bytep dp = row;
3146 png_uint_32 i;
3148 for (i = 0; i < row_width; i++)
3150 png_uint_16 red, green, blue, gray16;
3151 png_byte hi,lo;
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));
3157 if (red != green || red != blue)
3158 rgb_error |= 1;
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.
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);
3169 if (have_alpha != 0)
3171 *(dp++) = *(sp++);
3172 *(dp++) = *(sp++);
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);
3185 return rgb_error;
3187 #endif
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.
3195 static void
3196 png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
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
3209 png_bytep sp;
3210 png_uint_32 i;
3211 png_uint_32 row_width = row_info->width;
3212 int shift;
3214 png_debug(1, "in png_do_compose");
3216 switch (row_info->color_type)
3218 case PNG_COLOR_TYPE_GRAY:
3220 switch (row_info->bit_depth)
3222 case 1:
3224 sp = row;
3225 shift = 7;
3226 for (i = 0; i < row_width; i++)
3228 if ((png_uint_16)((*sp >> shift) & 0x01)
3229 == png_ptr->trans_color.gray)
3231 unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
3232 tmp |=
3233 (unsigned int)(png_ptr->background.gray << shift);
3234 *sp = (png_byte)(tmp & 0xff);
3237 if (shift == 0)
3239 shift = 7;
3240 sp++;
3243 else
3244 shift--;
3246 break;
3249 case 2:
3251 #ifdef PNG_READ_GAMMA_SUPPORTED
3252 if (gamma_table != NULL)
3254 sp = row;
3255 shift = 6;
3256 for (i = 0; i < row_width; i++)
3258 if ((png_uint_16)((*sp >> shift) & 0x03)
3259 == png_ptr->trans_color.gray)
3261 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3262 tmp |=
3263 (unsigned int)png_ptr->background.gray << shift;
3264 *sp = (png_byte)(tmp & 0xff);
3267 else
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);
3277 if (shift == 0)
3279 shift = 6;
3280 sp++;
3283 else
3284 shift -= 2;
3288 else
3289 #endif
3291 sp = row;
3292 shift = 6;
3293 for (i = 0; i < row_width; i++)
3295 if ((png_uint_16)((*sp >> shift) & 0x03)
3296 == png_ptr->trans_color.gray)
3298 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3299 tmp |=
3300 (unsigned int)png_ptr->background.gray << shift;
3301 *sp = (png_byte)(tmp & 0xff);
3304 if (shift == 0)
3306 shift = 6;
3307 sp++;
3310 else
3311 shift -= 2;
3314 break;
3317 case 4:
3319 #ifdef PNG_READ_GAMMA_SUPPORTED
3320 if (gamma_table != NULL)
3322 sp = row;
3323 shift = 4;
3324 for (i = 0; i < row_width; i++)
3326 if ((png_uint_16)((*sp >> shift) & 0x0f)
3327 == png_ptr->trans_color.gray)
3329 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3330 tmp |=
3331 (unsigned int)(png_ptr->background.gray << shift);
3332 *sp = (png_byte)(tmp & 0xff);
3335 else
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);
3345 if (shift == 0)
3347 shift = 4;
3348 sp++;
3351 else
3352 shift -= 4;
3356 else
3357 #endif
3359 sp = row;
3360 shift = 4;
3361 for (i = 0; i < row_width; i++)
3363 if ((png_uint_16)((*sp >> shift) & 0x0f)
3364 == png_ptr->trans_color.gray)
3366 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3367 tmp |=
3368 (unsigned int)(png_ptr->background.gray << shift);
3369 *sp = (png_byte)(tmp & 0xff);
3372 if (shift == 0)
3374 shift = 4;
3375 sp++;
3378 else
3379 shift -= 4;
3382 break;
3385 case 8:
3387 #ifdef PNG_READ_GAMMA_SUPPORTED
3388 if (gamma_table != NULL)
3390 sp = row;
3391 for (i = 0; i < row_width; i++, sp++)
3393 if (*sp == png_ptr->trans_color.gray)
3394 *sp = (png_byte)png_ptr->background.gray;
3396 else
3397 *sp = gamma_table[*sp];
3400 else
3401 #endif
3403 sp = row;
3404 for (i = 0; i < row_width; i++, sp++)
3406 if (*sp == png_ptr->trans_color.gray)
3407 *sp = (png_byte)png_ptr->background.gray;
3410 break;
3413 case 16:
3415 #ifdef PNG_READ_GAMMA_SUPPORTED
3416 if (gamma_16 != NULL)
3418 sp = row;
3419 for (i = 0; i < row_width; i++, sp += 2)
3421 png_uint_16 v;
3423 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3425 if (v == png_ptr->trans_color.gray)
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);
3434 else
3436 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3437 *sp = (png_byte)((v >> 8) & 0xff);
3438 *(sp + 1) = (png_byte)(v & 0xff);
3442 else
3443 #endif
3445 sp = row;
3446 for (i = 0; i < row_width; i++, sp += 2)
3448 png_uint_16 v;
3450 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3452 if (v == png_ptr->trans_color.gray)
3454 *sp = (png_byte)((png_ptr->background.gray >> 8)
3455 & 0xff);
3456 *(sp + 1) = (png_byte)(png_ptr->background.gray
3457 & 0xff);
3461 break;
3464 default:
3465 break;
3467 break;
3470 case PNG_COLOR_TYPE_RGB:
3472 if (row_info->bit_depth == 8)
3474 #ifdef PNG_READ_GAMMA_SUPPORTED
3475 if (gamma_table != NULL)
3477 sp = row;
3478 for (i = 0; i < row_width; i++, sp += 3)
3480 if (*sp == png_ptr->trans_color.red &&
3481 *(sp + 1) == png_ptr->trans_color.green &&
3482 *(sp + 2) == png_ptr->trans_color.blue)
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;
3489 else
3491 *sp = gamma_table[*sp];
3492 *(sp + 1) = gamma_table[*(sp + 1)];
3493 *(sp + 2) = gamma_table[*(sp + 2)];
3497 else
3498 #endif
3500 sp = row;
3501 for (i = 0; i < row_width; i++, sp += 3)
3503 if (*sp == png_ptr->trans_color.red &&
3504 *(sp + 1) == png_ptr->trans_color.green &&
3505 *(sp + 2) == png_ptr->trans_color.blue)
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;
3514 else /* if (row_info->bit_depth == 16) */
3516 #ifdef PNG_READ_GAMMA_SUPPORTED
3517 if (gamma_16 != NULL)
3519 sp = row;
3520 for (i = 0; i < row_width; i++, sp += 6)
3522 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3524 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3525 + *(sp + 3));
3527 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3528 + *(sp + 5));
3530 if (r == png_ptr->trans_color.red &&
3531 g == png_ptr->trans_color.green &&
3532 b == png_ptr->trans_color.blue)
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);
3546 else
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);
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);
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);
3563 else
3564 #endif
3566 sp = row;
3567 for (i = 0; i < row_width; i++, sp += 6)
3569 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3571 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3572 + *(sp + 3));
3574 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3575 + *(sp + 5));
3577 if (r == png_ptr->trans_color.red &&
3578 g == png_ptr->trans_color.green &&
3579 b == png_ptr->trans_color.blue)
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);
3594 break;
3597 case PNG_COLOR_TYPE_GRAY_ALPHA:
3599 if (row_info->bit_depth == 8)
3601 #ifdef PNG_READ_GAMMA_SUPPORTED
3602 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3603 gamma_table != NULL)
3605 sp = row;
3606 for (i = 0; i < row_width; i++, sp += 2)
3608 png_uint_16 a = *(sp + 1);
3610 if (a == 0xff)
3611 *sp = gamma_table[*sp];
3613 else if (a == 0)
3615 /* Background is already in screen gamma */
3616 *sp = (png_byte)png_ptr->background.gray;
3619 else
3621 png_byte v, w;
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;
3631 else
3632 #endif
3634 sp = row;
3635 for (i = 0; i < row_width; i++, sp += 2)
3637 png_byte a = *(sp + 1);
3639 if (a == 0)
3640 *sp = (png_byte)png_ptr->background.gray;
3642 else if (a < 0xff)
3643 png_composite(*sp, *sp, a, png_ptr->background.gray);
3647 else /* if (png_ptr->bit_depth == 16) */
3649 #ifdef PNG_READ_GAMMA_SUPPORTED
3650 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3651 gamma_16_to_1 != NULL)
3653 sp = row;
3654 for (i = 0; i < row_width; i++, sp += 4)
3656 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3657 + *(sp + 3));
3659 if (a == (png_uint_16)0xffff)
3661 png_uint_16 v;
3663 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3664 *sp = (png_byte)((v >> 8) & 0xff);
3665 *(sp + 1) = (png_byte)(v & 0xff);
3668 else if (a == 0)
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);
3676 else
3678 png_uint_16 g, v, w;
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);
3692 else
3693 #endif
3695 sp = row;
3696 for (i = 0; i < row_width; i++, sp += 4)
3698 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3699 + *(sp + 3));
3701 if (a == 0)
3703 *sp = (png_byte)((png_ptr->background.gray >> 8)
3704 & 0xff);
3705 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3708 else if (a < 0xffff)
3710 png_uint_16 g, v;
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);
3720 break;
3723 case PNG_COLOR_TYPE_RGB_ALPHA:
3725 if (row_info->bit_depth == 8)
3727 #ifdef PNG_READ_GAMMA_SUPPORTED
3728 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3729 gamma_table != NULL)
3731 sp = row;
3732 for (i = 0; i < row_width; i++, sp += 4)
3734 png_byte a = *(sp + 3);
3736 if (a == 0xff)
3738 *sp = gamma_table[*sp];
3739 *(sp + 1) = gamma_table[*(sp + 1)];
3740 *(sp + 2) = gamma_table[*(sp + 2)];
3743 else if (a == 0)
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;
3751 else
3753 png_byte v, w;
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;
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;
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;
3772 else
3773 #endif
3775 sp = row;
3776 for (i = 0; i < row_width; i++, sp += 4)
3778 png_byte a = *(sp + 3);
3780 if (a == 0)
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;
3787 else if (a < 0xff)
3789 png_composite(*sp, *sp, a, png_ptr->background.red);
3791 png_composite(*(sp + 1), *(sp + 1), a,
3792 png_ptr->background.green);
3794 png_composite(*(sp + 2), *(sp + 2), a,
3795 png_ptr->background.blue);
3800 else /* if (row_info->bit_depth == 16) */
3802 #ifdef PNG_READ_GAMMA_SUPPORTED
3803 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3804 gamma_16_to_1 != NULL)
3806 sp = row;
3807 for (i = 0; i < row_width; i++, sp += 8)
3809 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3810 << 8) + (png_uint_16)(*(sp + 7)));
3812 if (a == (png_uint_16)0xffff)
3814 png_uint_16 v;
3816 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3817 *sp = (png_byte)((v >> 8) & 0xff);
3818 *(sp + 1) = (png_byte)(v & 0xff);
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);
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);
3829 else if (a == 0)
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);
3843 else
3845 png_uint_16 v, w;
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 >>
3852 *sp = (png_byte)((w >> 8) & 0xff);
3853 *(sp + 1) = (png_byte)(w & 0xff);
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 >>
3861 *(sp + 2) = (png_byte)((w >> 8) & 0xff);
3862 *(sp + 3) = (png_byte)(w & 0xff);
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 >>
3870 *(sp + 4) = (png_byte)((w >> 8) & 0xff);
3871 *(sp + 5) = (png_byte)(w & 0xff);
3876 else
3877 #endif
3879 sp = row;
3880 for (i = 0; i < row_width; i++, sp += 8)
3882 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3883 << 8) + (png_uint_16)(*(sp + 7)));
3885 if (a == 0)
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);
3898 else if (a < 0xffff)
3900 png_uint_16 v;
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));
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);
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);
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);
3923 break;
3926 default:
3927 break;
3930 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
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().
3939 static void
3940 png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
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;
3946 png_bytep sp;
3947 png_uint_32 i;
3948 png_uint_32 row_width=row_info->width;
3950 png_debug(1, "in png_do_gamma");
3952 if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3953 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3955 switch (row_info->color_type)
3957 case PNG_COLOR_TYPE_RGB:
3959 if (row_info->bit_depth == 8)
3961 sp = row;
3962 for (i = 0; i < row_width; i++)
3964 *sp = gamma_table[*sp];
3965 sp++;
3966 *sp = gamma_table[*sp];
3967 sp++;
3968 *sp = gamma_table[*sp];
3969 sp++;
3973 else /* if (row_info->bit_depth == 16) */
3975 sp = row;
3976 for (i = 0; i < row_width; i++)
3978 png_uint_16 v;
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;
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;
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;
3996 break;
3999 case PNG_COLOR_TYPE_RGB_ALPHA:
4001 if (row_info->bit_depth == 8)
4003 sp = row;
4004 for (i = 0; i < row_width; i++)
4006 *sp = gamma_table[*sp];
4007 sp++;
4009 *sp = gamma_table[*sp];
4010 sp++;
4012 *sp = gamma_table[*sp];
4013 sp++;
4015 sp++;
4019 else /* if (row_info->bit_depth == 16) */
4021 sp = row;
4022 for (i = 0; i < row_width; i++)
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;
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;
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;
4040 break;
4043 case PNG_COLOR_TYPE_GRAY_ALPHA:
4045 if (row_info->bit_depth == 8)
4047 sp = row;
4048 for (i = 0; i < row_width; i++)
4050 *sp = gamma_table[*sp];
4051 sp += 2;
4055 else /* if (row_info->bit_depth == 16) */
4057 sp = row;
4058 for (i = 0; i < row_width; i++)
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;
4066 break;
4069 case PNG_COLOR_TYPE_GRAY:
4071 if (row_info->bit_depth == 2)
4073 sp = row;
4074 for (i = 0; i < row_width; i += 4)
4076 int a = *sp & 0xc0;
4077 int b = *sp & 0x30;
4078 int c = *sp & 0x0c;
4079 int d = *sp & 0x03;
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++;
4090 if (row_info->bit_depth == 4)
4092 sp = row;
4093 for (i = 0; i < row_width; i += 2)
4095 int msb = *sp & 0xf0;
4096 int lsb = *sp & 0x0f;
4098 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
4099 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
4100 sp++;
4104 else if (row_info->bit_depth == 8)
4106 sp = row;
4107 for (i = 0; i < row_width; i++)
4109 *sp = gamma_table[*sp];
4110 sp++;
4114 else if (row_info->bit_depth == 16)
4116 sp = row;
4117 for (i = 0; i < row_width; i++)
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;
4125 break;
4128 default:
4129 break;
4133 #endif
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.
4140 static void
4141 png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
4143 png_uint_32 row_width = row_info->width;
4145 png_debug(1, "in png_do_encode_alpha");
4147 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4149 if (row_info->bit_depth == 8)
4151 png_bytep table = png_ptr->gamma_from_1;
4153 if (table != NULL)
4155 int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
4157 /* The alpha channel is the last component: */
4158 row += step - 1;
4160 for (; row_width > 0; --row_width, row += step)
4161 *row = table[*row];
4163 return;
4167 else if (row_info->bit_depth == 16)
4169 png_uint_16pp table = png_ptr->gamma_16_from_1;
4170 int gamma_shift = png_ptr->gamma_shift;
4172 if (table != NULL)
4174 int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
4176 /* The alpha channel is the last component: */
4177 row += step - 2;
4179 for (; row_width > 0; --row_width, row += step)
4181 png_uint_16 v;
4183 v = table[*(row + 1) >> gamma_shift][*row];
4184 *row = (png_byte)((v >> 8) & 0xff);
4185 *(row + 1) = (png_byte)(v & 0xff);
4188 return;
4193 /* Only get to here if called with a weird row_info; no harm has been done,
4194 * so just issue a warning.
4196 png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
4198 #endif
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.
4204 static void
4205 png_do_expand_palette(png_structrp png_ptr, png_row_infop row_info,
4206 png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha,
4207 int num_trans)
4209 int shift, value;
4210 png_bytep sp, dp;
4211 png_uint_32 i;
4212 png_uint_32 row_width=row_info->width;
4214 png_debug(1, "in png_do_expand_palette");
4216 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4218 if (row_info->bit_depth < 8)
4220 switch (row_info->bit_depth)
4222 case 1:
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++)
4229 if ((*sp >> shift) & 0x01)
4230 *dp = 1;
4232 else
4233 *dp = 0;
4235 if (shift == 7)
4237 shift = 0;
4238 sp--;
4241 else
4242 shift++;
4244 dp--;
4246 break;
4249 case 2:
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++)
4256 value = (*sp >> shift) & 0x03;
4257 *dp = (png_byte)value;
4258 if (shift == 6)
4260 shift = 0;
4261 sp--;
4264 else
4265 shift += 2;
4267 dp--;
4269 break;
4272 case 4:
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++)
4279 value = (*sp >> shift) & 0x0f;
4280 *dp = (png_byte)value;
4281 if (shift == 4)
4283 shift = 0;
4284 sp--;
4287 else
4288 shift += 4;
4290 dp--;
4292 break;
4295 default:
4296 break;
4298 row_info->bit_depth = 8;
4299 row_info->pixel_depth = 8;
4300 row_info->rowbytes = row_width;
4303 if (row_info->bit_depth == 8)
4306 if (num_trans > 0)
4308 sp = row + (size_t)row_width - 1;
4309 dp = row + ((size_t)row_width << 2) - 1;
4311 i = 0;
4312 #ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
4313 if (png_ptr->riffled_palette != NULL)
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.
4319 i = png_do_expand_palette_rgba8_neon(png_ptr, row_info, row,
4320 &sp, &dp);
4322 #else
4323 PNG_UNUSED(png_ptr)
4324 #endif
4326 for (; i < row_width; i++)
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--;
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;
4344 else
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
4356 for (; i < row_width; i++)
4358 *dp-- = palette[*sp].blue;
4359 *dp-- = palette[*sp].green;
4360 *dp-- = palette[*sp].red;
4361 sp--;
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;
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.
4378 static void
4379 png_do_expand(png_row_infop row_info, png_bytep row,
4380 png_const_color_16p trans_color)
4382 int shift, value;
4383 png_bytep sp, dp;
4384 png_uint_32 i;
4385 png_uint_32 row_width=row_info->width;
4387 png_debug(1, "in png_do_expand");
4389 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
4391 unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
4393 if (row_info->bit_depth < 8)
4395 switch (row_info->bit_depth)
4397 case 1:
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++)
4405 if ((*sp >> shift) & 0x01)
4406 *dp = 0xff;
4408 else
4409 *dp = 0;
4411 if (shift == 7)
4413 shift = 0;
4414 sp--;
4417 else
4418 shift++;
4420 dp--;
4422 break;
4425 case 2:
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++)
4433 value = (*sp >> shift) & 0x03;
4434 *dp = (png_byte)(value | (value << 2) | (value << 4) |
4435 (value << 6));
4436 if (shift == 6)
4438 shift = 0;
4439 sp--;
4442 else
4443 shift += 2;
4445 dp--;
4447 break;
4450 case 4:
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++)
4458 value = (*sp >> shift) & 0x0f;
4459 *dp = (png_byte)(value | (value << 4));
4460 if (shift == 4)
4462 shift = 0;
4463 sp--;
4466 else
4467 shift = 4;
4469 dp--;
4471 break;
4474 default:
4475 break;
4478 row_info->bit_depth = 8;
4479 row_info->pixel_depth = 8;
4480 row_info->rowbytes = row_width;
4483 if (trans_color != NULL)
4485 if (row_info->bit_depth == 8)
4487 gray = gray & 0xff;
4488 sp = row + (size_t)row_width - 1;
4489 dp = row + ((size_t)row_width << 1) - 1;
4491 for (i = 0; i < row_width; i++)
4493 if ((*sp & 0xffU) == gray)
4494 *dp-- = 0;
4496 else
4497 *dp-- = 0xff;
4499 *dp-- = *sp--;
4503 else if (row_info->bit_depth == 16)
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++)
4511 if ((*(sp - 1) & 0xffU) == gray_high &&
4512 (*(sp) & 0xffU) == gray_low)
4514 *dp-- = 0;
4515 *dp-- = 0;
4518 else
4520 *dp-- = 0xff;
4521 *dp-- = 0xff;
4524 *dp-- = *sp--;
4525 *dp-- = *sp--;
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);
4536 else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4537 trans_color != NULL)
4539 if (row_info->bit_depth == 8)
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++)
4548 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4549 *dp-- = 0;
4551 else
4552 *dp-- = 0xff;
4554 *dp-- = *sp--;
4555 *dp-- = *sp--;
4556 *dp-- = *sp--;
4559 else if (row_info->bit_depth == 16)
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++)
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)
4578 *dp-- = 0;
4579 *dp-- = 0;
4582 else
4584 *dp-- = 0xff;
4585 *dp-- = 0xff;
4588 *dp-- = *sp--;
4589 *dp-- = *sp--;
4590 *dp-- = *sp--;
4591 *dp-- = *sp--;
4592 *dp-- = *sp--;
4593 *dp-- = *sp--;
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);
4602 #endif
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.
4608 static void
4609 png_do_expand_16(png_row_infop row_info, png_bytep row)
4611 if (row_info->bit_depth == 8 &&
4612 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
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:
4618 * (input / 255) * 65535
4620 * Which happens to be exactly input * 257 and this can be achieved
4621 * simply by byte replication in place (copying backwards).
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)
4627 dp[-2] = dp[-1] = *--sp; dp -= 2;
4630 row_info->rowbytes *= 2;
4631 row_info->bit_depth = 16;
4632 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4635 #endif
4637 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4638 static void
4639 png_do_quantize(png_row_infop row_info, png_bytep row,
4640 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4642 png_bytep sp, dp;
4643 png_uint_32 i;
4644 png_uint_32 row_width=row_info->width;
4646 png_debug(1, "in png_do_quantize");
4648 if (row_info->bit_depth == 8)
4650 if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
4652 int r, g, b, p;
4653 sp = row;
4654 dp = row;
4655 for (i = 0; i < row_width; i++)
4657 r = *sp++;
4658 g = *sp++;
4659 b = *sp++;
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);
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));
4677 *dp++ = palette_lookup[p];
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);
4686 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4687 palette_lookup != NULL)
4689 int r, g, b, p;
4690 sp = row;
4691 dp = row;
4692 for (i = 0; i < row_width; i++)
4694 r = *sp++;
4695 g = *sp++;
4696 b = *sp++;
4697 sp++;
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));
4708 *dp++ = palette_lookup[p];
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);
4717 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4718 quantize_lookup)
4720 sp = row;
4722 for (i = 0; i < row_width; i++, sp++)
4724 *sp = quantize_lookup[*sp];
4729 #endif /* READ_QUANTIZE */
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.
4735 void /* PRIVATE */
4736 png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
4738 png_debug(1, "in png_do_read_transformations");
4740 if (png_ptr->row_buf == NULL)
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.
4746 png_error(png_ptr, "NULL row buffer");
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.
4755 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
4756 (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
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).
4762 png_error(png_ptr, "Uninitialized row");
4765 #ifdef PNG_READ_EXPAND_SUPPORTED
4766 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4768 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4770 #ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
4771 if ((png_ptr->num_trans > 0) && (png_ptr->bit_depth == 8))
4773 if (png_ptr->riffled_palette == NULL)
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);
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);
4786 else
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));
4793 else
4794 png_do_expand(row_info, png_ptr->row_buf + 1, NULL);
4797 #endif
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
4808 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4809 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
4811 int rgb_error =
4812 png_do_rgb_to_gray(png_ptr, row_info,
4813 png_ptr->row_buf + 1);
4815 if (rgb_error != 0)
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");
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");
4827 #endif
4829 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
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.
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
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)
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.
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
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
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
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.)
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.
4893 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
4894 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
4895 #endif
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
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
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
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.
4922 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
4923 png_do_chop(row_info, png_ptr->row_buf + 1);
4924 #endif
4926 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4927 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
4929 png_do_quantize(row_info, png_ptr->row_buf + 1,
4930 png_ptr->palette_lookup, png_ptr->quantize_index);
4932 if (row_info->rowbytes == 0)
4933 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
4935 #endif /* READ_QUANTIZE */
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!)
4943 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4944 png_do_expand_16(row_info, png_ptr->row_buf + 1);
4945 #endif
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
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
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
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
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
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
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
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
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
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
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
5010 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
5011 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
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;
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);
5034 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
5036 #endif
5039 #endif /* READ_TRANSFORMS */
5040 #endif /* READ */