minor tidy up
[AROS.git] / workbench / libs / png / pngrtran.c
blobeefba2ed7fe2e7df1b114bfc1e2d2b9d0c382e5c
2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.6.24 [August 4, 2016]
5 * Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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_READ_SUPPORTED
23 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
24 void PNGAPI
25 png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
27 png_debug(1, "in png_set_crc_action");
29 if (png_ptr == NULL)
30 return;
32 /* Tell libpng how we react to CRC errors in critical chunks */
33 switch (crit_action)
35 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
36 break;
38 case PNG_CRC_WARN_USE: /* Warn/use data */
39 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
40 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
41 break;
43 case PNG_CRC_QUIET_USE: /* Quiet/use data */
44 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
45 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
46 PNG_FLAG_CRC_CRITICAL_IGNORE;
47 break;
49 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
50 png_warning(png_ptr,
51 "Can't discard critical data on CRC error");
52 case PNG_CRC_ERROR_QUIT: /* Error/quit */
54 case PNG_CRC_DEFAULT:
55 default:
56 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
57 break;
60 /* Tell libpng how we react to CRC errors in ancillary chunks */
61 switch (ancil_action)
63 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
64 break;
66 case PNG_CRC_WARN_USE: /* Warn/use data */
67 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
68 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
69 break;
71 case PNG_CRC_QUIET_USE: /* Quiet/use data */
72 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
73 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
74 PNG_FLAG_CRC_ANCILLARY_NOWARN;
75 break;
77 case PNG_CRC_ERROR_QUIT: /* Error/quit */
78 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
79 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
80 break;
82 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
84 case PNG_CRC_DEFAULT:
85 default:
86 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
87 break;
91 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
92 /* Is it OK to set a transformation now? Only if png_start_read_image or
93 * png_read_update_info have not been called. It is not necessary for the IHDR
94 * to have been read in all cases; the need_IHDR parameter allows for this
95 * check too.
97 static int
98 png_rtran_ok(png_structrp png_ptr, int need_IHDR)
100 if (png_ptr != NULL)
102 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0)
103 png_app_error(png_ptr,
104 "invalid after png_start_read_image or png_read_update_info");
106 else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0)
107 png_app_error(png_ptr, "invalid before the PNG header has been read");
109 else
111 /* Turn on failure to initialize correctly for all transforms. */
112 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
114 return 1; /* Ok */
118 return 0; /* no png_error possible! */
120 #endif
122 #ifdef PNG_READ_BACKGROUND_SUPPORTED
123 /* Handle alpha and tRNS via a background color */
124 void PNGFAPI
125 png_set_background_fixed(png_structrp png_ptr,
126 png_const_color_16p background_color, int background_gamma_code,
127 int need_expand, png_fixed_point background_gamma)
129 png_debug(1, "in png_set_background_fixed");
131 if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
132 return;
134 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
136 png_warning(png_ptr, "Application must supply a known background gamma");
137 return;
140 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
141 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
142 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
144 png_ptr->background = *background_color;
145 png_ptr->background_gamma = background_gamma;
146 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
147 if (need_expand != 0)
148 png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
149 else
150 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
153 # ifdef PNG_FLOATING_POINT_SUPPORTED
154 void PNGAPI
155 png_set_background(png_structrp png_ptr,
156 png_const_color_16p background_color, int background_gamma_code,
157 int need_expand, double background_gamma)
159 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
160 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
162 # endif /* FLOATING_POINT */
163 #endif /* READ_BACKGROUND */
165 /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
166 * one that pngrtran does first (scale) happens. This is necessary to allow the
167 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
169 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
170 void PNGAPI
171 png_set_scale_16(png_structrp png_ptr)
173 png_debug(1, "in png_set_scale_16");
175 if (png_rtran_ok(png_ptr, 0) == 0)
176 return;
178 png_ptr->transformations |= PNG_SCALE_16_TO_8;
180 #endif
182 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
183 /* Chop 16-bit depth files to 8-bit depth */
184 void PNGAPI
185 png_set_strip_16(png_structrp png_ptr)
187 png_debug(1, "in png_set_strip_16");
189 if (png_rtran_ok(png_ptr, 0) == 0)
190 return;
192 png_ptr->transformations |= PNG_16_TO_8;
194 #endif
196 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
197 void PNGAPI
198 png_set_strip_alpha(png_structrp png_ptr)
200 png_debug(1, "in png_set_strip_alpha");
202 if (png_rtran_ok(png_ptr, 0) == 0)
203 return;
205 png_ptr->transformations |= PNG_STRIP_ALPHA;
207 #endif
209 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
210 static png_fixed_point
211 translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma,
212 int is_screen)
214 /* Check for flag values. The main reason for having the old Mac value as a
215 * flag is that it is pretty near impossible to work out what the correct
216 * value is from Apple documentation - a working Mac system is needed to
217 * discover the value!
219 if (output_gamma == PNG_DEFAULT_sRGB ||
220 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
222 /* If there is no sRGB support this just sets the gamma to the standard
223 * sRGB value. (This is a side effect of using this function!)
225 # ifdef PNG_READ_sRGB_SUPPORTED
226 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
227 # else
228 PNG_UNUSED(png_ptr)
229 # endif
230 if (is_screen != 0)
231 output_gamma = PNG_GAMMA_sRGB;
232 else
233 output_gamma = PNG_GAMMA_sRGB_INVERSE;
236 else if (output_gamma == PNG_GAMMA_MAC_18 ||
237 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
239 if (is_screen != 0)
240 output_gamma = PNG_GAMMA_MAC_OLD;
241 else
242 output_gamma = PNG_GAMMA_MAC_INVERSE;
245 return output_gamma;
248 # ifdef PNG_FLOATING_POINT_SUPPORTED
249 static png_fixed_point
250 convert_gamma_value(png_structrp png_ptr, double output_gamma)
252 /* The following silently ignores cases where fixed point (times 100,000)
253 * gamma values are passed to the floating point API. This is safe and it
254 * means the fixed point constants work just fine with the floating point
255 * API. The alternative would just lead to undetected errors and spurious
256 * bug reports. Negative values fail inside the _fixed API unless they
257 * correspond to the flag values.
259 if (output_gamma > 0 && output_gamma < 128)
260 output_gamma *= PNG_FP_1;
262 /* This preserves -1 and -2 exactly: */
263 output_gamma = floor(output_gamma + .5);
265 if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
266 png_fixed_error(png_ptr, "gamma value");
268 return (png_fixed_point)output_gamma;
270 # endif
271 #endif /* READ_ALPHA_MODE || READ_GAMMA */
273 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
274 void PNGFAPI
275 png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
276 png_fixed_point output_gamma)
278 int compose = 0;
279 png_fixed_point file_gamma;
281 png_debug(1, "in png_set_alpha_mode");
283 if (png_rtran_ok(png_ptr, 0) == 0)
284 return;
286 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
288 /* Validate the value to ensure it is in a reasonable range. The value
289 * is expected to be 1 or greater, but this range test allows for some
290 * viewing correction values. The intent is to weed out users of this API
291 * who use the inverse of the gamma value accidentally! Since some of these
292 * values are reasonable this may have to be changed:
294 * 1.6.x: changed from 0.07..3 to 0.01..100 (to accomodate the optimal 16-bit
295 * gamma of 36, and its reciprocal.)
297 if (output_gamma < 1000 || output_gamma > 10000000)
298 png_error(png_ptr, "output gamma out of expected range");
300 /* The default file gamma is the inverse of the output gamma; the output
301 * gamma may be changed below so get the file value first:
303 file_gamma = png_reciprocal(output_gamma);
305 /* There are really 8 possibilities here, composed of any combination
306 * of:
308 * premultiply the color channels
309 * do not encode non-opaque pixels
310 * encode the alpha as well as the color channels
312 * The differences disappear if the input/output ('screen') gamma is 1.0,
313 * because then the encoding is a no-op and there is only the choice of
314 * premultiplying the color channels or not.
316 * png_set_alpha_mode and png_set_background interact because both use
317 * png_compose to do the work. Calling both is only useful when
318 * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
319 * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
321 switch (mode)
323 case PNG_ALPHA_PNG: /* default: png standard */
324 /* No compose, but it may be set by png_set_background! */
325 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
326 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
327 break;
329 case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
330 compose = 1;
331 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
332 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
333 /* The output is linear: */
334 output_gamma = PNG_FP_1;
335 break;
337 case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
338 compose = 1;
339 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
340 png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
341 /* output_gamma records the encoding of opaque pixels! */
342 break;
344 case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
345 compose = 1;
346 png_ptr->transformations |= PNG_ENCODE_ALPHA;
347 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
348 break;
350 default:
351 png_error(png_ptr, "invalid alpha mode");
354 /* Only set the default gamma if the file gamma has not been set (this has
355 * the side effect that the gamma in a second call to png_set_alpha_mode will
356 * be ignored.)
358 if (png_ptr->colorspace.gamma == 0)
360 png_ptr->colorspace.gamma = file_gamma;
361 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
364 /* But always set the output gamma: */
365 png_ptr->screen_gamma = output_gamma;
367 /* Finally, if pre-multiplying, set the background fields to achieve the
368 * desired result.
370 if (compose != 0)
372 /* And obtain alpha pre-multiplication by composing on black: */
373 memset(&png_ptr->background, 0, (sizeof png_ptr->background));
374 png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */
375 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
376 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
378 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
379 png_error(png_ptr,
380 "conflicting calls to set alpha mode and background");
382 png_ptr->transformations |= PNG_COMPOSE;
386 # ifdef PNG_FLOATING_POINT_SUPPORTED
387 void PNGAPI
388 png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
390 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
391 output_gamma));
393 # endif
394 #endif
396 #ifdef PNG_READ_QUANTIZE_SUPPORTED
397 /* Dither file to 8-bit. Supply a palette, the current number
398 * of elements in the palette, the maximum number of elements
399 * allowed, and a histogram if possible. If the current number
400 * of colors is greater than the maximum number, the palette will be
401 * modified to fit in the maximum number. "full_quantize" indicates
402 * whether we need a quantizing cube set up for RGB images, or if we
403 * simply are reducing the number of colors in a paletted image.
406 typedef struct png_dsort_struct
408 struct png_dsort_struct * next;
409 png_byte left;
410 png_byte right;
411 } png_dsort;
412 typedef png_dsort * png_dsortp;
413 typedef png_dsort * * png_dsortpp;
415 void PNGAPI
416 png_set_quantize(png_structrp png_ptr, png_colorp palette,
417 int num_palette, int maximum_colors, png_const_uint_16p histogram,
418 int full_quantize)
420 png_debug(1, "in png_set_quantize");
422 if (png_rtran_ok(png_ptr, 0) == 0)
423 return;
425 png_ptr->transformations |= PNG_QUANTIZE;
427 if (full_quantize == 0)
429 int i;
431 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
432 (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
433 for (i = 0; i < num_palette; i++)
434 png_ptr->quantize_index[i] = (png_byte)i;
437 if (num_palette > maximum_colors)
439 if (histogram != NULL)
441 /* This is easy enough, just throw out the least used colors.
442 * Perhaps not the best solution, but good enough.
445 int i;
447 /* Initialize an array to sort colors */
448 png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
449 (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
451 /* Initialize the quantize_sort array */
452 for (i = 0; i < num_palette; i++)
453 png_ptr->quantize_sort[i] = (png_byte)i;
455 /* Find the least used palette entries by starting a
456 * bubble sort, and running it until we have sorted
457 * out enough colors. Note that we don't care about
458 * sorting all the colors, just finding which are
459 * least used.
462 for (i = num_palette - 1; i >= maximum_colors; i--)
464 int done; /* To stop early if the list is pre-sorted */
465 int j;
467 done = 1;
468 for (j = 0; j < i; j++)
470 if (histogram[png_ptr->quantize_sort[j]]
471 < histogram[png_ptr->quantize_sort[j + 1]])
473 png_byte t;
475 t = png_ptr->quantize_sort[j];
476 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
477 png_ptr->quantize_sort[j + 1] = t;
478 done = 0;
482 if (done != 0)
483 break;
486 /* Swap the palette around, and set up a table, if necessary */
487 if (full_quantize != 0)
489 int j = num_palette;
491 /* Put all the useful colors within the max, but don't
492 * move the others.
494 for (i = 0; i < maximum_colors; i++)
496 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
499 j--;
500 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
502 palette[i] = palette[j];
506 else
508 int j = num_palette;
510 /* Move all the used colors inside the max limit, and
511 * develop a translation table.
513 for (i = 0; i < maximum_colors; i++)
515 /* Only move the colors we need to */
516 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
518 png_color tmp_color;
521 j--;
522 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
524 tmp_color = palette[j];
525 palette[j] = palette[i];
526 palette[i] = tmp_color;
527 /* Indicate where the color went */
528 png_ptr->quantize_index[j] = (png_byte)i;
529 png_ptr->quantize_index[i] = (png_byte)j;
533 /* Find closest color for those colors we are not using */
534 for (i = 0; i < num_palette; i++)
536 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
538 int min_d, k, min_k, d_index;
540 /* Find the closest color to one we threw out */
541 d_index = png_ptr->quantize_index[i];
542 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
543 for (k = 1, min_k = 0; k < maximum_colors; k++)
545 int d;
547 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
549 if (d < min_d)
551 min_d = d;
552 min_k = k;
555 /* Point to closest color */
556 png_ptr->quantize_index[i] = (png_byte)min_k;
560 png_free(png_ptr, png_ptr->quantize_sort);
561 png_ptr->quantize_sort = NULL;
563 else
565 /* This is much harder to do simply (and quickly). Perhaps
566 * we need to go through a median cut routine, but those
567 * don't always behave themselves with only a few colors
568 * as input. So we will just find the closest two colors,
569 * and throw out one of them (chosen somewhat randomly).
570 * [We don't understand this at all, so if someone wants to
571 * work on improving it, be our guest - AED, GRP]
573 int i;
574 int max_d;
575 int num_new_palette;
576 png_dsortp t;
577 png_dsortpp hash;
579 t = NULL;
581 /* Initialize palette index arrays */
582 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
583 (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
584 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
585 (png_uint_32)((png_uint_32)num_palette * (sizeof (png_byte))));
587 /* Initialize the sort array */
588 for (i = 0; i < num_palette; i++)
590 png_ptr->index_to_palette[i] = (png_byte)i;
591 png_ptr->palette_to_index[i] = (png_byte)i;
594 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
595 (sizeof (png_dsortp))));
597 num_new_palette = num_palette;
599 /* Initial wild guess at how far apart the farthest pixel
600 * pair we will be eliminating will be. Larger
601 * numbers mean more areas will be allocated, Smaller
602 * numbers run the risk of not saving enough data, and
603 * having to do this all over again.
605 * I have not done extensive checking on this number.
607 max_d = 96;
609 while (num_new_palette > maximum_colors)
611 for (i = 0; i < num_new_palette - 1; i++)
613 int j;
615 for (j = i + 1; j < num_new_palette; j++)
617 int d;
619 d = PNG_COLOR_DIST(palette[i], palette[j]);
621 if (d <= max_d)
624 t = (png_dsortp)png_malloc_warn(png_ptr,
625 (png_uint_32)(sizeof (png_dsort)));
627 if (t == NULL)
628 break;
630 t->next = hash[d];
631 t->left = (png_byte)i;
632 t->right = (png_byte)j;
633 hash[d] = t;
636 if (t == NULL)
637 break;
640 if (t != NULL)
641 for (i = 0; i <= max_d; i++)
643 if (hash[i] != NULL)
645 png_dsortp p;
647 for (p = hash[i]; p; p = p->next)
649 if ((int)png_ptr->index_to_palette[p->left]
650 < num_new_palette &&
651 (int)png_ptr->index_to_palette[p->right]
652 < num_new_palette)
654 int j, next_j;
656 if (num_new_palette & 0x01)
658 j = p->left;
659 next_j = p->right;
661 else
663 j = p->right;
664 next_j = p->left;
667 num_new_palette--;
668 palette[png_ptr->index_to_palette[j]]
669 = palette[num_new_palette];
670 if (full_quantize == 0)
672 int k;
674 for (k = 0; k < num_palette; k++)
676 if (png_ptr->quantize_index[k] ==
677 png_ptr->index_to_palette[j])
678 png_ptr->quantize_index[k] =
679 png_ptr->index_to_palette[next_j];
681 if ((int)png_ptr->quantize_index[k] ==
682 num_new_palette)
683 png_ptr->quantize_index[k] =
684 png_ptr->index_to_palette[j];
688 png_ptr->index_to_palette[png_ptr->palette_to_index
689 [num_new_palette]] = png_ptr->index_to_palette[j];
691 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
692 = png_ptr->palette_to_index[num_new_palette];
694 png_ptr->index_to_palette[j] =
695 (png_byte)num_new_palette;
697 png_ptr->palette_to_index[num_new_palette] =
698 (png_byte)j;
700 if (num_new_palette <= maximum_colors)
701 break;
703 if (num_new_palette <= maximum_colors)
704 break;
708 for (i = 0; i < 769; i++)
710 if (hash[i] != NULL)
712 png_dsortp p = hash[i];
713 while (p)
715 t = p->next;
716 png_free(png_ptr, p);
717 p = t;
720 hash[i] = 0;
722 max_d += 96;
724 png_free(png_ptr, hash);
725 png_free(png_ptr, png_ptr->palette_to_index);
726 png_free(png_ptr, png_ptr->index_to_palette);
727 png_ptr->palette_to_index = NULL;
728 png_ptr->index_to_palette = NULL;
730 num_palette = maximum_colors;
732 if (png_ptr->palette == NULL)
734 png_ptr->palette = palette;
736 png_ptr->num_palette = (png_uint_16)num_palette;
738 if (full_quantize != 0)
740 int i;
741 png_bytep distance;
742 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
743 PNG_QUANTIZE_BLUE_BITS;
744 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
745 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
746 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
747 png_size_t num_entries = ((png_size_t)1 << total_bits);
749 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
750 (png_uint_32)(num_entries * (sizeof (png_byte))));
752 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
753 (sizeof (png_byte))));
755 memset(distance, 0xff, num_entries * (sizeof (png_byte)));
757 for (i = 0; i < num_palette; i++)
759 int ir, ig, ib;
760 int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
761 int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
762 int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
764 for (ir = 0; ir < num_red; ir++)
766 /* int dr = abs(ir - r); */
767 int dr = ((ir > r) ? ir - r : r - ir);
768 int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
769 PNG_QUANTIZE_GREEN_BITS));
771 for (ig = 0; ig < num_green; ig++)
773 /* int dg = abs(ig - g); */
774 int dg = ((ig > g) ? ig - g : g - ig);
775 int dt = dr + dg;
776 int dm = ((dr > dg) ? dr : dg);
777 int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
779 for (ib = 0; ib < num_blue; ib++)
781 int d_index = index_g | ib;
782 /* int db = abs(ib - b); */
783 int db = ((ib > b) ? ib - b : b - ib);
784 int dmax = ((dm > db) ? dm : db);
785 int d = dmax + dt + db;
787 if (d < (int)distance[d_index])
789 distance[d_index] = (png_byte)d;
790 png_ptr->palette_lookup[d_index] = (png_byte)i;
797 png_free(png_ptr, distance);
800 #endif /* READ_QUANTIZE */
802 #ifdef PNG_READ_GAMMA_SUPPORTED
803 void PNGFAPI
804 png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
805 png_fixed_point file_gamma)
807 png_debug(1, "in png_set_gamma_fixed");
809 if (png_rtran_ok(png_ptr, 0) == 0)
810 return;
812 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
813 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
814 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
816 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
817 * premultiplied alpha support; this actually hides an undocumented feature
818 * of the previous implementation which allowed gamma processing to be
819 * disabled in background handling. There is no evidence (so far) that this
820 * was being used; however, png_set_background itself accepted and must still
821 * accept '0' for the gamma value it takes, because it isn't always used.
823 * Since this is an API change (albeit a very minor one that removes an
824 * undocumented API feature) the following checks were only enabled in
825 * libpng-1.6.0.
827 if (file_gamma <= 0)
828 png_error(png_ptr, "invalid file gamma in png_set_gamma");
830 if (scrn_gamma <= 0)
831 png_error(png_ptr, "invalid screen gamma in png_set_gamma");
833 /* Set the gamma values unconditionally - this overrides the value in the PNG
834 * file if a gAMA chunk was present. png_set_alpha_mode provides a
835 * different, easier, way to default the file gamma.
837 png_ptr->colorspace.gamma = file_gamma;
838 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
839 png_ptr->screen_gamma = scrn_gamma;
842 # ifdef PNG_FLOATING_POINT_SUPPORTED
843 void PNGAPI
844 png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
846 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
847 convert_gamma_value(png_ptr, file_gamma));
849 # endif /* FLOATING_POINT */
850 #endif /* READ_GAMMA */
852 #ifdef PNG_READ_EXPAND_SUPPORTED
853 /* Expand paletted images to RGB, expand grayscale images of
854 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
855 * to alpha channels.
857 void PNGAPI
858 png_set_expand(png_structrp png_ptr)
860 png_debug(1, "in png_set_expand");
862 if (png_rtran_ok(png_ptr, 0) == 0)
863 return;
865 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
868 /* GRR 19990627: the following three functions currently are identical
869 * to png_set_expand(). However, it is entirely reasonable that someone
870 * might wish to expand an indexed image to RGB but *not* expand a single,
871 * fully transparent palette entry to a full alpha channel--perhaps instead
872 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
873 * the transparent color with a particular RGB value, or drop tRNS entirely.
874 * IOW, a future version of the library may make the transformations flag
875 * a bit more fine-grained, with separate bits for each of these three
876 * functions.
878 * More to the point, these functions make it obvious what libpng will be
879 * doing, whereas "expand" can (and does) mean any number of things.
881 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
882 * to expand only the sample depth but not to expand the tRNS to alpha
883 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
886 /* Expand paletted images to RGB. */
887 void PNGAPI
888 png_set_palette_to_rgb(png_structrp png_ptr)
890 png_debug(1, "in png_set_palette_to_rgb");
892 if (png_rtran_ok(png_ptr, 0) == 0)
893 return;
895 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
898 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
899 void PNGAPI
900 png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
902 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
904 if (png_rtran_ok(png_ptr, 0) == 0)
905 return;
907 png_ptr->transformations |= PNG_EXPAND;
910 #ifdef __AROS__
911 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
912 /* Deprecated as of libpng-1.2.9 */
913 void PNGAPI
914 png_set_gray_1_2_4_to_8(png_structp png_ptr)
916 png_debug(1, "in png_set_gray_1_2_4_to_8");
918 if (png_ptr == NULL)
919 return;
921 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
923 #endif /* __AROS__ */
925 /* Expand tRNS chunks to alpha channels. */
926 void PNGAPI
927 png_set_tRNS_to_alpha(png_structrp png_ptr)
929 png_debug(1, "in png_set_tRNS_to_alpha");
931 if (png_rtran_ok(png_ptr, 0) == 0)
932 return;
934 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
936 #endif /* READ_EXPAND */
938 #ifdef PNG_READ_EXPAND_16_SUPPORTED
939 /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
940 * it may not work correctly.)
942 void PNGAPI
943 png_set_expand_16(png_structrp png_ptr)
945 png_debug(1, "in png_set_expand_16");
947 if (png_rtran_ok(png_ptr, 0) == 0)
948 return;
950 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
952 #endif
954 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
955 void PNGAPI
956 png_set_gray_to_rgb(png_structrp png_ptr)
958 png_debug(1, "in png_set_gray_to_rgb");
960 if (png_rtran_ok(png_ptr, 0) == 0)
961 return;
963 /* Because rgb must be 8 bits or more: */
964 png_set_expand_gray_1_2_4_to_8(png_ptr);
965 png_ptr->transformations |= PNG_GRAY_TO_RGB;
967 #endif
969 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
970 void PNGFAPI
971 png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
972 png_fixed_point red, png_fixed_point green)
974 png_debug(1, "in png_set_rgb_to_gray");
976 /* Need the IHDR here because of the check on color_type below. */
977 /* TODO: fix this */
978 if (png_rtran_ok(png_ptr, 1) == 0)
979 return;
981 switch (error_action)
983 case PNG_ERROR_ACTION_NONE:
984 png_ptr->transformations |= PNG_RGB_TO_GRAY;
985 break;
987 case PNG_ERROR_ACTION_WARN:
988 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
989 break;
991 case PNG_ERROR_ACTION_ERROR:
992 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
993 break;
995 default:
996 png_error(png_ptr, "invalid error action to rgb_to_gray");
999 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1000 #ifdef PNG_READ_EXPAND_SUPPORTED
1001 png_ptr->transformations |= PNG_EXPAND;
1002 #else
1004 /* Make this an error in 1.6 because otherwise the application may assume
1005 * that it just worked and get a memory overwrite.
1007 png_error(png_ptr,
1008 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
1010 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
1012 #endif
1014 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
1016 png_uint_16 red_int, green_int;
1018 /* NOTE: this calculation does not round, but this behavior is retained
1019 * for consistency; the inaccuracy is very small. The code here always
1020 * overwrites the coefficients, regardless of whether they have been
1021 * defaulted or set already.
1023 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
1024 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
1026 png_ptr->rgb_to_gray_red_coeff = red_int;
1027 png_ptr->rgb_to_gray_green_coeff = green_int;
1028 png_ptr->rgb_to_gray_coefficients_set = 1;
1031 else
1033 if (red >= 0 && green >= 0)
1034 png_app_warning(png_ptr,
1035 "ignoring out of range rgb_to_gray coefficients");
1037 /* Use the defaults, from the cHRM chunk if set, else the historical
1038 * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
1039 * png_do_rgb_to_gray for more discussion of the values. In this case
1040 * the coefficients are not marked as 'set' and are not overwritten if
1041 * something has already provided a default.
1043 if (png_ptr->rgb_to_gray_red_coeff == 0 &&
1044 png_ptr->rgb_to_gray_green_coeff == 0)
1046 png_ptr->rgb_to_gray_red_coeff = 6968;
1047 png_ptr->rgb_to_gray_green_coeff = 23434;
1048 /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
1054 #ifdef PNG_FLOATING_POINT_SUPPORTED
1055 /* Convert a RGB image to a grayscale of the same width. This allows us,
1056 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1059 void PNGAPI
1060 png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red,
1061 double green)
1063 png_set_rgb_to_gray_fixed(png_ptr, error_action,
1064 png_fixed(png_ptr, red, "rgb to gray red coefficient"),
1065 png_fixed(png_ptr, green, "rgb to gray green coefficient"));
1067 #endif /* FLOATING POINT */
1069 #endif /* RGB_TO_GRAY */
1071 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
1072 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1073 void PNGAPI
1074 png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
1075 read_user_transform_fn)
1077 png_debug(1, "in png_set_read_user_transform_fn");
1079 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1080 png_ptr->transformations |= PNG_USER_TRANSFORM;
1081 png_ptr->read_user_transform_fn = read_user_transform_fn;
1082 #endif
1084 #endif
1086 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1087 #ifdef PNG_READ_GAMMA_SUPPORTED
1088 /* In the case of gamma transformations only do transformations on images where
1089 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
1090 * slows things down slightly, and also needlessly introduces small errors.
1092 static int /* PRIVATE */
1093 png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
1095 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1096 * correction as a difference of the overall transform from 1.0
1098 * We want to compare the threshold with s*f - 1, if we get
1099 * overflow here it is because of wacky gamma values so we
1100 * turn on processing anyway.
1102 png_fixed_point gtest;
1103 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1104 png_gamma_significant(gtest);
1106 #endif
1108 /* Initialize everything needed for the read. This includes modifying
1109 * the palette.
1112 /* For the moment 'png_init_palette_transformations' and
1113 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1114 * The intent is that these two routines should have palette or rgb operations
1115 * extracted from 'png_init_read_transformations'.
1117 static void /* PRIVATE */
1118 png_init_palette_transformations(png_structrp png_ptr)
1120 /* Called to handle the (input) palette case. In png_do_read_transformations
1121 * the first step is to expand the palette if requested, so this code must
1122 * take care to only make changes that are invariant with respect to the
1123 * palette expansion, or only do them if there is no expansion.
1125 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1126 * to 0.)
1128 int input_has_alpha = 0;
1129 int input_has_transparency = 0;
1131 if (png_ptr->num_trans > 0)
1133 int i;
1135 /* Ignore if all the entries are opaque (unlikely!) */
1136 for (i=0; i<png_ptr->num_trans; ++i)
1138 if (png_ptr->trans_alpha[i] == 255)
1139 continue;
1140 else if (png_ptr->trans_alpha[i] == 0)
1141 input_has_transparency = 1;
1142 else
1144 input_has_transparency = 1;
1145 input_has_alpha = 1;
1146 break;
1151 /* If no alpha we can optimize. */
1152 if (input_has_alpha == 0)
1154 /* Any alpha means background and associative alpha processing is
1155 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
1156 * and ENCODE_ALPHA are irrelevant.
1158 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1159 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1161 if (input_has_transparency == 0)
1162 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1165 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1166 /* png_set_background handling - deals with the complexity of whether the
1167 * background color is in the file format or the screen format in the case
1168 * where an 'expand' will happen.
1171 /* The following code cannot be entered in the alpha pre-multiplication case
1172 * because PNG_BACKGROUND_EXPAND is cancelled below.
1174 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1175 (png_ptr->transformations & PNG_EXPAND) != 0)
1178 png_ptr->background.red =
1179 png_ptr->palette[png_ptr->background.index].red;
1180 png_ptr->background.green =
1181 png_ptr->palette[png_ptr->background.index].green;
1182 png_ptr->background.blue =
1183 png_ptr->palette[png_ptr->background.index].blue;
1185 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1186 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
1188 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1190 /* Invert the alpha channel (in tRNS) unless the pixels are
1191 * going to be expanded, in which case leave it for later
1193 int i, istop = png_ptr->num_trans;
1195 for (i=0; i<istop; i++)
1196 png_ptr->trans_alpha[i] = (png_byte)(255 -
1197 png_ptr->trans_alpha[i]);
1200 #endif /* READ_INVERT_ALPHA */
1202 } /* background expand and (therefore) no alpha association. */
1203 #endif /* READ_EXPAND && READ_BACKGROUND */
1206 static void /* PRIVATE */
1207 png_init_rgb_transformations(png_structrp png_ptr)
1209 /* Added to libpng-1.5.4: check the color type to determine whether there
1210 * is any alpha or transparency in the image and simply cancel the
1211 * background and alpha mode stuff if there isn't.
1213 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1214 int input_has_transparency = png_ptr->num_trans > 0;
1216 /* If no alpha we can optimize. */
1217 if (input_has_alpha == 0)
1219 /* Any alpha means background and associative alpha processing is
1220 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
1221 * and ENCODE_ALPHA are irrelevant.
1223 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1224 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1225 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1226 # endif
1228 if (input_has_transparency == 0)
1229 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1232 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1233 /* png_set_background handling - deals with the complexity of whether the
1234 * background color is in the file format or the screen format in the case
1235 * where an 'expand' will happen.
1238 /* The following code cannot be entered in the alpha pre-multiplication case
1239 * because PNG_BACKGROUND_EXPAND is cancelled below.
1241 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1242 (png_ptr->transformations & PNG_EXPAND) != 0 &&
1243 (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1244 /* i.e., GRAY or GRAY_ALPHA */
1247 /* Expand background and tRNS chunks */
1248 int gray = png_ptr->background.gray;
1249 int trans_gray = png_ptr->trans_color.gray;
1251 switch (png_ptr->bit_depth)
1253 case 1:
1254 gray *= 0xff;
1255 trans_gray *= 0xff;
1256 break;
1258 case 2:
1259 gray *= 0x55;
1260 trans_gray *= 0x55;
1261 break;
1263 case 4:
1264 gray *= 0x11;
1265 trans_gray *= 0x11;
1266 break;
1268 default:
1270 case 8:
1271 /* FALL THROUGH (Already 8 bits) */
1273 case 16:
1274 /* Already a full 16 bits */
1275 break;
1278 png_ptr->background.red = png_ptr->background.green =
1279 png_ptr->background.blue = (png_uint_16)gray;
1281 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1283 png_ptr->trans_color.red = png_ptr->trans_color.green =
1284 png_ptr->trans_color.blue = (png_uint_16)trans_gray;
1287 } /* background expand and (therefore) no alpha association. */
1288 #endif /* READ_EXPAND && READ_BACKGROUND */
1291 void /* PRIVATE */
1292 png_init_read_transformations(png_structrp png_ptr)
1294 png_debug(1, "in png_init_read_transformations");
1296 /* This internal function is called from png_read_start_row in pngrutil.c
1297 * and it is called before the 'rowbytes' calculation is done, so the code
1298 * in here can change or update the transformations flags.
1300 * First do updates that do not depend on the details of the PNG image data
1301 * being processed.
1304 #ifdef PNG_READ_GAMMA_SUPPORTED
1305 /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
1306 * png_set_alpha_mode and this is another source for a default file gamma so
1307 * the test needs to be performed later - here. In addition prior to 1.5.4
1308 * the tests were repeated for the PALETTE color type here - this is no
1309 * longer necessary (and doesn't seem to have been necessary before.)
1312 /* The following temporary indicates if overall gamma correction is
1313 * required.
1315 int gamma_correction = 0;
1317 if (png_ptr->colorspace.gamma != 0) /* has been set */
1319 if (png_ptr->screen_gamma != 0) /* screen set too */
1320 gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma,
1321 png_ptr->screen_gamma);
1323 else
1324 /* Assume the output matches the input; a long time default behavior
1325 * of libpng, although the standard has nothing to say about this.
1327 png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma);
1330 else if (png_ptr->screen_gamma != 0)
1331 /* The converse - assume the file matches the screen, note that this
1332 * perhaps undesireable default can (from 1.5.4) be changed by calling
1333 * png_set_alpha_mode (even if the alpha handling mode isn't required
1334 * or isn't changed from the default.)
1336 png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma);
1338 else /* neither are set */
1339 /* Just in case the following prevents any processing - file and screen
1340 * are both assumed to be linear and there is no way to introduce a
1341 * third gamma value other than png_set_background with 'UNIQUE', and,
1342 * prior to 1.5.4
1344 png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1;
1346 /* We have a gamma value now. */
1347 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
1349 /* Now turn the gamma transformation on or off as appropriate. Notice
1350 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1351 * composition may independently cause gamma correction because it needs
1352 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1353 * hasn't been specified.) In any case this flag may get turned off in
1354 * the code immediately below if the transform can be handled outside the
1355 * row loop.
1357 if (gamma_correction != 0)
1358 png_ptr->transformations |= PNG_GAMMA;
1360 else
1361 png_ptr->transformations &= ~PNG_GAMMA;
1363 #endif
1365 /* Certain transformations have the effect of preventing other
1366 * transformations that happen afterward in png_do_read_transformations;
1367 * resolve the interdependencies here. From the code of
1368 * png_do_read_transformations the order is:
1370 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1371 * 2) PNG_STRIP_ALPHA (if no compose)
1372 * 3) PNG_RGB_TO_GRAY
1373 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1374 * 5) PNG_COMPOSE
1375 * 6) PNG_GAMMA
1376 * 7) PNG_STRIP_ALPHA (if compose)
1377 * 8) PNG_ENCODE_ALPHA
1378 * 9) PNG_SCALE_16_TO_8
1379 * 10) PNG_16_TO_8
1380 * 11) PNG_QUANTIZE (converts to palette)
1381 * 12) PNG_EXPAND_16
1382 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1383 * 14) PNG_INVERT_MONO
1384 * 15) PNG_INVERT_ALPHA
1385 * 16) PNG_SHIFT
1386 * 17) PNG_PACK
1387 * 18) PNG_BGR
1388 * 19) PNG_PACKSWAP
1389 * 20) PNG_FILLER (includes PNG_ADD_ALPHA)
1390 * 21) PNG_SWAP_ALPHA
1391 * 22) PNG_SWAP_BYTES
1392 * 23) PNG_USER_TRANSFORM [must be last]
1394 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1395 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1396 (png_ptr->transformations & PNG_COMPOSE) == 0)
1398 /* Stripping the alpha channel happens immediately after the 'expand'
1399 * transformations, before all other transformation, so it cancels out
1400 * the alpha handling. It has the side effect negating the effect of
1401 * PNG_EXPAND_tRNS too:
1403 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1404 PNG_EXPAND_tRNS);
1405 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1407 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
1408 * so transparency information would remain just so long as it wasn't
1409 * expanded. This produces unexpected API changes if the set of things
1410 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1411 * documentation - which says ask for what you want, accept what you
1412 * get.) This makes the behavior consistent from 1.5.4:
1414 png_ptr->num_trans = 0;
1416 #endif /* STRIP_ALPHA supported, no COMPOSE */
1418 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1419 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1420 * settings will have no effect.
1422 if (png_gamma_significant(png_ptr->screen_gamma) == 0)
1424 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1425 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1427 #endif
1429 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1430 /* Make sure the coefficients for the rgb to gray conversion are set
1431 * appropriately.
1433 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1434 png_colorspace_set_rgb_coefficients(png_ptr);
1435 #endif
1437 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1438 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1439 /* Detect gray background and attempt to enable optimization for
1440 * gray --> RGB case.
1442 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1443 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1444 * background color might actually be gray yet not be flagged as such.
1445 * This is not a problem for the current code, which uses
1446 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1447 * png_do_gray_to_rgb() transformation.
1449 * TODO: this code needs to be revised to avoid the complexity and
1450 * interdependencies. The color type of the background should be recorded in
1451 * png_set_background, along with the bit depth, then the code has a record
1452 * of exactly what color space the background is currently in.
1454 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
1456 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1457 * the file was grayscale the background value is gray.
1459 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1460 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1463 else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1465 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1466 * so the color is in the color space of the output or png_set_alpha_mode
1467 * was called and the color is black. Ignore RGB_TO_GRAY because that
1468 * happens before GRAY_TO_RGB.
1470 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
1472 if (png_ptr->background.red == png_ptr->background.green &&
1473 png_ptr->background.red == png_ptr->background.blue)
1475 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1476 png_ptr->background.gray = png_ptr->background.red;
1480 #endif /* READ_EXPAND && READ_BACKGROUND */
1481 #endif /* READ_GRAY_TO_RGB */
1483 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1484 * can be performed directly on the palette, and some (such as rgb to gray)
1485 * can be optimized inside the palette. This is particularly true of the
1486 * composite (background and alpha) stuff, which can be pretty much all done
1487 * in the palette even if the result is expanded to RGB or gray afterward.
1489 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1490 * earlier and the palette stuff is actually handled on the first row. This
1491 * leads to the reported bug that the palette returned by png_get_PLTE is not
1492 * updated.
1494 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1495 png_init_palette_transformations(png_ptr);
1497 else
1498 png_init_rgb_transformations(png_ptr);
1500 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1501 defined(PNG_READ_EXPAND_16_SUPPORTED)
1502 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
1503 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1504 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1505 png_ptr->bit_depth != 16)
1507 /* TODO: fix this. Because the expand_16 operation is after the compose
1508 * handling the background color must be 8, not 16, bits deep, but the
1509 * application will supply a 16-bit value so reduce it here.
1511 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1512 * present, so that case is ok (until do_expand_16 is moved.)
1514 * NOTE: this discards the low 16 bits of the user supplied background
1515 * color, but until expand_16 works properly there is no choice!
1517 # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
1518 CHOP(png_ptr->background.red);
1519 CHOP(png_ptr->background.green);
1520 CHOP(png_ptr->background.blue);
1521 CHOP(png_ptr->background.gray);
1522 # undef CHOP
1524 #endif /* READ_BACKGROUND && READ_EXPAND_16 */
1526 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1527 (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1528 defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
1529 if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
1530 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1531 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1532 png_ptr->bit_depth == 16)
1534 /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
1535 * component this will also happen after PNG_COMPOSE and so the background
1536 * color must be pre-expanded here.
1538 * TODO: fix this too.
1540 png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
1541 png_ptr->background.green =
1542 (png_uint_16)(png_ptr->background.green * 257);
1543 png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
1544 png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
1546 #endif
1548 /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1549 * background support (see the comments in scripts/pnglibconf.dfa), this
1550 * allows pre-multiplication of the alpha channel to be implemented as
1551 * compositing on black. This is probably sub-optimal and has been done in
1552 * 1.5.4 betas simply to enable external critique and testing (i.e. to
1553 * implement the new API quickly, without lots of internal changes.)
1556 #ifdef PNG_READ_GAMMA_SUPPORTED
1557 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1558 /* Includes ALPHA_MODE */
1559 png_ptr->background_1 = png_ptr->background;
1560 # endif
1562 /* This needs to change - in the palette image case a whole set of tables are
1563 * built when it would be quicker to just calculate the correct value for
1564 * each palette entry directly. Also, the test is too tricky - why check
1565 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1566 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1567 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1568 * the gamma tables will not be built even if composition is required on a
1569 * gamma encoded value.
1571 * In 1.5.4 this is addressed below by an additional check on the individual
1572 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1573 * tables.
1575 if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
1576 ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
1577 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1578 png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
1579 ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1580 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1581 png_gamma_significant(png_ptr->screen_gamma) != 0
1582 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1583 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
1584 png_gamma_significant(png_ptr->background_gamma) != 0)
1585 # endif
1586 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
1587 png_gamma_significant(png_ptr->screen_gamma) != 0))
1589 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1591 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1592 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1594 /* Issue a warning about this combination: because RGB_TO_GRAY is
1595 * optimized to do the gamma transform if present yet do_background has
1596 * to do the same thing if both options are set a
1597 * double-gamma-correction happens. This is true in all versions of
1598 * libpng to date.
1600 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1601 png_warning(png_ptr,
1602 "libpng does not support gamma+background+rgb_to_gray");
1604 if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
1606 /* We don't get to here unless there is a tRNS chunk with non-opaque
1607 * entries - see the checking code at the start of this function.
1609 png_color back, back_1;
1610 png_colorp palette = png_ptr->palette;
1611 int num_palette = png_ptr->num_palette;
1612 int i;
1613 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1616 back.red = png_ptr->gamma_table[png_ptr->background.red];
1617 back.green = png_ptr->gamma_table[png_ptr->background.green];
1618 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1620 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1621 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1622 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1624 else
1626 png_fixed_point g, gs;
1628 switch (png_ptr->background_gamma_type)
1630 case PNG_BACKGROUND_GAMMA_SCREEN:
1631 g = (png_ptr->screen_gamma);
1632 gs = PNG_FP_1;
1633 break;
1635 case PNG_BACKGROUND_GAMMA_FILE:
1636 g = png_reciprocal(png_ptr->colorspace.gamma);
1637 gs = png_reciprocal2(png_ptr->colorspace.gamma,
1638 png_ptr->screen_gamma);
1639 break;
1641 case PNG_BACKGROUND_GAMMA_UNIQUE:
1642 g = png_reciprocal(png_ptr->background_gamma);
1643 gs = png_reciprocal2(png_ptr->background_gamma,
1644 png_ptr->screen_gamma);
1645 break;
1646 default:
1647 g = PNG_FP_1; /* back_1 */
1648 gs = PNG_FP_1; /* back */
1649 break;
1652 if (png_gamma_significant(gs) != 0)
1654 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1655 gs);
1656 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1657 gs);
1658 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1659 gs);
1662 else
1664 back.red = (png_byte)png_ptr->background.red;
1665 back.green = (png_byte)png_ptr->background.green;
1666 back.blue = (png_byte)png_ptr->background.blue;
1669 if (png_gamma_significant(g) != 0)
1671 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1673 back_1.green = png_gamma_8bit_correct(
1674 png_ptr->background.green, g);
1675 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1679 else
1681 back_1.red = (png_byte)png_ptr->background.red;
1682 back_1.green = (png_byte)png_ptr->background.green;
1683 back_1.blue = (png_byte)png_ptr->background.blue;
1687 for (i = 0; i < num_palette; i++)
1689 if (i < (int)png_ptr->num_trans &&
1690 png_ptr->trans_alpha[i] != 0xff)
1692 if (png_ptr->trans_alpha[i] == 0)
1694 palette[i] = back;
1696 else /* if (png_ptr->trans_alpha[i] != 0xff) */
1698 png_byte v, w;
1700 v = png_ptr->gamma_to_1[palette[i].red];
1701 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1702 palette[i].red = png_ptr->gamma_from_1[w];
1704 v = png_ptr->gamma_to_1[palette[i].green];
1705 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1706 palette[i].green = png_ptr->gamma_from_1[w];
1708 v = png_ptr->gamma_to_1[palette[i].blue];
1709 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1710 palette[i].blue = png_ptr->gamma_from_1[w];
1713 else
1715 palette[i].red = png_ptr->gamma_table[palette[i].red];
1716 palette[i].green = png_ptr->gamma_table[palette[i].green];
1717 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1721 /* Prevent the transformations being done again.
1723 * NOTE: this is highly dubious; it removes the transformations in
1724 * place. This seems inconsistent with the general treatment of the
1725 * transformations elsewhere.
1727 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1728 } /* color_type == PNG_COLOR_TYPE_PALETTE */
1730 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1731 else /* color_type != PNG_COLOR_TYPE_PALETTE */
1733 int gs_sig, g_sig;
1734 png_fixed_point g = PNG_FP_1; /* Correction to linear */
1735 png_fixed_point gs = PNG_FP_1; /* Correction to screen */
1737 switch (png_ptr->background_gamma_type)
1739 case PNG_BACKGROUND_GAMMA_SCREEN:
1740 g = png_ptr->screen_gamma;
1741 /* gs = PNG_FP_1; */
1742 break;
1744 case PNG_BACKGROUND_GAMMA_FILE:
1745 g = png_reciprocal(png_ptr->colorspace.gamma);
1746 gs = png_reciprocal2(png_ptr->colorspace.gamma,
1747 png_ptr->screen_gamma);
1748 break;
1750 case PNG_BACKGROUND_GAMMA_UNIQUE:
1751 g = png_reciprocal(png_ptr->background_gamma);
1752 gs = png_reciprocal2(png_ptr->background_gamma,
1753 png_ptr->screen_gamma);
1754 break;
1756 default:
1757 png_error(png_ptr, "invalid background gamma type");
1760 g_sig = png_gamma_significant(g);
1761 gs_sig = png_gamma_significant(gs);
1763 if (g_sig != 0)
1764 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1765 png_ptr->background.gray, g);
1767 if (gs_sig != 0)
1768 png_ptr->background.gray = png_gamma_correct(png_ptr,
1769 png_ptr->background.gray, gs);
1771 if ((png_ptr->background.red != png_ptr->background.green) ||
1772 (png_ptr->background.red != png_ptr->background.blue) ||
1773 (png_ptr->background.red != png_ptr->background.gray))
1775 /* RGB or RGBA with color background */
1776 if (g_sig != 0)
1778 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1779 png_ptr->background.red, g);
1781 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1782 png_ptr->background.green, g);
1784 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1785 png_ptr->background.blue, g);
1788 if (gs_sig != 0)
1790 png_ptr->background.red = png_gamma_correct(png_ptr,
1791 png_ptr->background.red, gs);
1793 png_ptr->background.green = png_gamma_correct(png_ptr,
1794 png_ptr->background.green, gs);
1796 png_ptr->background.blue = png_gamma_correct(png_ptr,
1797 png_ptr->background.blue, gs);
1801 else
1803 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1804 png_ptr->background_1.red = png_ptr->background_1.green
1805 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1807 png_ptr->background.red = png_ptr->background.green
1808 = png_ptr->background.blue = png_ptr->background.gray;
1811 /* The background is now in screen gamma: */
1812 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
1813 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1814 }/* png_ptr->transformations & PNG_BACKGROUND */
1816 else
1817 /* Transformation does not include PNG_BACKGROUND */
1818 #endif /* READ_BACKGROUND */
1819 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
1820 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1821 /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1822 && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1823 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1824 #endif
1827 png_colorp palette = png_ptr->palette;
1828 int num_palette = png_ptr->num_palette;
1829 int i;
1831 /* NOTE: there are other transformations that should probably be in
1832 * here too.
1834 for (i = 0; i < num_palette; i++)
1836 palette[i].red = png_ptr->gamma_table[palette[i].red];
1837 palette[i].green = png_ptr->gamma_table[palette[i].green];
1838 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1841 /* Done the gamma correction. */
1842 png_ptr->transformations &= ~PNG_GAMMA;
1843 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1845 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1846 else
1847 #endif
1848 #endif /* READ_GAMMA */
1850 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1851 /* No GAMMA transformation (see the hanging else 4 lines above) */
1852 if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1853 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1855 int i;
1856 int istop = (int)png_ptr->num_trans;
1857 png_color back;
1858 png_colorp palette = png_ptr->palette;
1860 back.red = (png_byte)png_ptr->background.red;
1861 back.green = (png_byte)png_ptr->background.green;
1862 back.blue = (png_byte)png_ptr->background.blue;
1864 for (i = 0; i < istop; i++)
1866 if (png_ptr->trans_alpha[i] == 0)
1868 palette[i] = back;
1871 else if (png_ptr->trans_alpha[i] != 0xff)
1873 /* The png_composite() macro is defined in png.h */
1874 png_composite(palette[i].red, palette[i].red,
1875 png_ptr->trans_alpha[i], back.red);
1877 png_composite(palette[i].green, palette[i].green,
1878 png_ptr->trans_alpha[i], back.green);
1880 png_composite(palette[i].blue, palette[i].blue,
1881 png_ptr->trans_alpha[i], back.blue);
1885 png_ptr->transformations &= ~PNG_COMPOSE;
1887 #endif /* READ_BACKGROUND */
1889 #ifdef PNG_READ_SHIFT_SUPPORTED
1890 if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
1891 (png_ptr->transformations & PNG_EXPAND) == 0 &&
1892 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1894 int i;
1895 int istop = png_ptr->num_palette;
1896 int shift = 8 - png_ptr->sig_bit.red;
1898 png_ptr->transformations &= ~PNG_SHIFT;
1900 /* significant bits can be in the range 1 to 7 for a meaninful result, if
1901 * the number of significant bits is 0 then no shift is done (this is an
1902 * error condition which is silently ignored.)
1904 if (shift > 0 && shift < 8)
1905 for (i=0; i<istop; ++i)
1907 int component = png_ptr->palette[i].red;
1909 component >>= shift;
1910 png_ptr->palette[i].red = (png_byte)component;
1913 shift = 8 - png_ptr->sig_bit.green;
1914 if (shift > 0 && shift < 8)
1915 for (i=0; i<istop; ++i)
1917 int component = png_ptr->palette[i].green;
1919 component >>= shift;
1920 png_ptr->palette[i].green = (png_byte)component;
1923 shift = 8 - png_ptr->sig_bit.blue;
1924 if (shift > 0 && shift < 8)
1925 for (i=0; i<istop; ++i)
1927 int component = png_ptr->palette[i].blue;
1929 component >>= shift;
1930 png_ptr->palette[i].blue = (png_byte)component;
1933 #endif /* READ_SHIFT */
1936 /* Modify the info structure to reflect the transformations. The
1937 * info should be updated so a PNG file could be written with it,
1938 * assuming the transformations result in valid PNG data.
1940 void /* PRIVATE */
1941 png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
1943 png_debug(1, "in png_read_transform_info");
1945 #ifdef PNG_READ_EXPAND_SUPPORTED
1946 if ((png_ptr->transformations & PNG_EXPAND) != 0)
1948 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1950 /* This check must match what actually happens in
1951 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
1952 * it is all opaque we must do the same (at present it does not.)
1954 if (png_ptr->num_trans > 0)
1955 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1957 else
1958 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1960 info_ptr->bit_depth = 8;
1961 info_ptr->num_trans = 0;
1963 if (png_ptr->palette == NULL)
1964 png_error (png_ptr, "Palette is NULL in indexed image");
1966 else
1968 if (png_ptr->num_trans != 0)
1970 if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
1971 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1973 if (info_ptr->bit_depth < 8)
1974 info_ptr->bit_depth = 8;
1976 info_ptr->num_trans = 0;
1979 #endif
1981 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1982 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1983 /* The following is almost certainly wrong unless the background value is in
1984 * the screen space!
1986 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1987 info_ptr->background = png_ptr->background;
1988 #endif
1990 #ifdef PNG_READ_GAMMA_SUPPORTED
1991 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
1992 * however it seems that the code in png_init_read_transformations, which has
1993 * been called before this from png_read_update_info->png_read_start_row
1994 * sometimes does the gamma transform and cancels the flag.
1996 * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1997 * the screen_gamma value. The following probably results in weirdness if
1998 * the info_ptr is used by the app after the rows have been read.
2000 info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
2001 #endif
2003 if (info_ptr->bit_depth == 16)
2005 # ifdef PNG_READ_16BIT_SUPPORTED
2006 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2007 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
2008 info_ptr->bit_depth = 8;
2009 # endif
2011 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2012 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
2013 info_ptr->bit_depth = 8;
2014 # endif
2016 # else
2017 /* No 16-bit support: force chopping 16-bit input down to 8, in this case
2018 * the app program can chose if both APIs are available by setting the
2019 * correct scaling to use.
2021 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2022 /* For compatibility with previous versions use the strip method by
2023 * default. This code works because if PNG_SCALE_16_TO_8 is already
2024 * set the code below will do that in preference to the chop.
2026 png_ptr->transformations |= PNG_16_TO_8;
2027 info_ptr->bit_depth = 8;
2028 # else
2030 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2031 png_ptr->transformations |= PNG_SCALE_16_TO_8;
2032 info_ptr->bit_depth = 8;
2033 # else
2035 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
2036 # endif
2037 # endif
2038 #endif /* !READ_16BIT */
2041 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2042 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
2043 info_ptr->color_type = (png_byte)(info_ptr->color_type |
2044 PNG_COLOR_MASK_COLOR);
2045 #endif
2047 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2048 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
2049 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2050 ~PNG_COLOR_MASK_COLOR);
2051 #endif
2053 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2054 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
2056 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
2057 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
2058 png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8)
2060 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2063 #endif
2065 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2066 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
2067 info_ptr->bit_depth == 8 &&
2068 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
2070 info_ptr->bit_depth = 16;
2072 #endif
2074 #ifdef PNG_READ_PACK_SUPPORTED
2075 if ((png_ptr->transformations & PNG_PACK) != 0 &&
2076 (info_ptr->bit_depth < 8))
2077 info_ptr->bit_depth = 8;
2078 #endif
2080 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2081 info_ptr->channels = 1;
2083 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
2084 info_ptr->channels = 3;
2086 else
2087 info_ptr->channels = 1;
2089 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2090 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
2092 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2093 ~PNG_COLOR_MASK_ALPHA);
2094 info_ptr->num_trans = 0;
2096 #endif
2098 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
2099 info_ptr->channels++;
2101 #ifdef PNG_READ_FILLER_SUPPORTED
2102 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
2103 if ((png_ptr->transformations & PNG_FILLER) != 0 &&
2104 (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2105 info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
2107 info_ptr->channels++;
2108 /* If adding a true alpha channel not just filler */
2109 if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
2110 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2112 #endif
2114 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2115 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2116 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
2118 if (png_ptr->user_transform_depth != 0)
2119 info_ptr->bit_depth = png_ptr->user_transform_depth;
2121 if (png_ptr->user_transform_channels != 0)
2122 info_ptr->channels = png_ptr->user_transform_channels;
2124 #endif
2126 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2127 info_ptr->bit_depth);
2129 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
2131 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
2132 * check in png_rowbytes that the user buffer won't get overwritten. Note
2133 * that the field is not always set - if png_read_update_info isn't called
2134 * the application has to either not do any transforms or get the calculation
2135 * right itself.
2137 png_ptr->info_rowbytes = info_ptr->rowbytes;
2139 #ifndef PNG_READ_EXPAND_SUPPORTED
2140 if (png_ptr != NULL)
2141 return;
2142 #endif
2145 #ifdef PNG_READ_PACK_SUPPORTED
2146 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2147 * without changing the actual values. Thus, if you had a row with
2148 * a bit depth of 1, you would end up with bytes that only contained
2149 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2150 * png_do_shift() after this.
2152 static void
2153 png_do_unpack(png_row_infop row_info, png_bytep row)
2155 png_debug(1, "in png_do_unpack");
2157 if (row_info->bit_depth < 8)
2159 png_uint_32 i;
2160 png_uint_32 row_width=row_info->width;
2162 switch (row_info->bit_depth)
2164 case 1:
2166 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
2167 png_bytep dp = row + (png_size_t)row_width - 1;
2168 png_uint_32 shift = 7U - ((row_width + 7U) & 0x07);
2169 for (i = 0; i < row_width; i++)
2171 *dp = (png_byte)((*sp >> shift) & 0x01);
2173 if (shift == 7)
2175 shift = 0;
2176 sp--;
2179 else
2180 shift++;
2182 dp--;
2184 break;
2187 case 2:
2190 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
2191 png_bytep dp = row + (png_size_t)row_width - 1;
2192 png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1);
2193 for (i = 0; i < row_width; i++)
2195 *dp = (png_byte)((*sp >> shift) & 0x03);
2197 if (shift == 6)
2199 shift = 0;
2200 sp--;
2203 else
2204 shift += 2;
2206 dp--;
2208 break;
2211 case 4:
2213 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
2214 png_bytep dp = row + (png_size_t)row_width - 1;
2215 png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2);
2216 for (i = 0; i < row_width; i++)
2218 *dp = (png_byte)((*sp >> shift) & 0x0f);
2220 if (shift == 4)
2222 shift = 0;
2223 sp--;
2226 else
2227 shift = 4;
2229 dp--;
2231 break;
2234 default:
2235 break;
2237 row_info->bit_depth = 8;
2238 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2239 row_info->rowbytes = row_width * row_info->channels;
2242 #endif
2244 #ifdef PNG_READ_SHIFT_SUPPORTED
2245 /* Reverse the effects of png_do_shift. This routine merely shifts the
2246 * pixels back to their significant bits values. Thus, if you have
2247 * a row of bit depth 8, but only 5 are significant, this will shift
2248 * the values back to 0 through 31.
2250 static void
2251 png_do_unshift(png_row_infop row_info, png_bytep row,
2252 png_const_color_8p sig_bits)
2254 int color_type;
2256 png_debug(1, "in png_do_unshift");
2258 /* The palette case has already been handled in the _init routine. */
2259 color_type = row_info->color_type;
2261 if (color_type != PNG_COLOR_TYPE_PALETTE)
2263 int shift[4];
2264 int channels = 0;
2265 int bit_depth = row_info->bit_depth;
2267 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
2269 shift[channels++] = bit_depth - sig_bits->red;
2270 shift[channels++] = bit_depth - sig_bits->green;
2271 shift[channels++] = bit_depth - sig_bits->blue;
2274 else
2276 shift[channels++] = bit_depth - sig_bits->gray;
2279 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
2281 shift[channels++] = bit_depth - sig_bits->alpha;
2285 int c, have_shift;
2287 for (c = have_shift = 0; c < channels; ++c)
2289 /* A shift of more than the bit depth is an error condition but it
2290 * gets ignored here.
2292 if (shift[c] <= 0 || shift[c] >= bit_depth)
2293 shift[c] = 0;
2295 else
2296 have_shift = 1;
2299 if (have_shift == 0)
2300 return;
2303 switch (bit_depth)
2305 default:
2306 /* Must be 1bpp gray: should not be here! */
2307 /* NOTREACHED */
2308 break;
2310 case 2:
2311 /* Must be 2bpp gray */
2312 /* assert(channels == 1 && shift[0] == 1) */
2314 png_bytep bp = row;
2315 png_bytep bp_end = bp + row_info->rowbytes;
2317 while (bp < bp_end)
2319 int b = (*bp >> 1) & 0x55;
2320 *bp++ = (png_byte)b;
2322 break;
2325 case 4:
2326 /* Must be 4bpp gray */
2327 /* assert(channels == 1) */
2329 png_bytep bp = row;
2330 png_bytep bp_end = bp + row_info->rowbytes;
2331 int gray_shift = shift[0];
2332 int mask = 0xf >> gray_shift;
2334 mask |= mask << 4;
2336 while (bp < bp_end)
2338 int b = (*bp >> gray_shift) & mask;
2339 *bp++ = (png_byte)b;
2341 break;
2344 case 8:
2345 /* Single byte components, G, GA, RGB, RGBA */
2347 png_bytep bp = row;
2348 png_bytep bp_end = bp + row_info->rowbytes;
2349 int channel = 0;
2351 while (bp < bp_end)
2353 int b = *bp >> shift[channel];
2354 if (++channel >= channels)
2355 channel = 0;
2356 *bp++ = (png_byte)b;
2358 break;
2361 #ifdef PNG_READ_16BIT_SUPPORTED
2362 case 16:
2363 /* Double byte components, G, GA, RGB, RGBA */
2365 png_bytep bp = row;
2366 png_bytep bp_end = bp + row_info->rowbytes;
2367 int channel = 0;
2369 while (bp < bp_end)
2371 int value = (bp[0] << 8) + bp[1];
2373 value >>= shift[channel];
2374 if (++channel >= channels)
2375 channel = 0;
2376 *bp++ = (png_byte)(value >> 8);
2377 *bp++ = (png_byte)value;
2379 break;
2381 #endif
2385 #endif
2387 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2388 /* Scale rows of bit depth 16 down to 8 accurately */
2389 static void
2390 png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
2392 png_debug(1, "in png_do_scale_16_to_8");
2394 if (row_info->bit_depth == 16)
2396 png_bytep sp = row; /* source */
2397 png_bytep dp = row; /* destination */
2398 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2400 while (sp < ep)
2402 /* The input is an array of 16-bit components, these must be scaled to
2403 * 8 bits each. For a 16-bit value V the required value (from the PNG
2404 * specification) is:
2406 * (V * 255) / 65535
2408 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2410 * Represent V as the two byte value vhi.vlo. Make a guess that the
2411 * result is the top byte of V, vhi, then the correction to this value
2412 * is:
2414 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2415 * = floor(((vlo-vhi) + 128.5) / 257)
2417 * This can be approximated using integer arithmetic (and a signed
2418 * shift):
2420 * error = (vlo-vhi+128) >> 8;
2422 * The approximate differs from the exact answer only when (vlo-vhi) is
2423 * 128; it then gives a correction of +1 when the exact correction is
2424 * 0. This gives 128 errors. The exact answer (correct for all 16-bit
2425 * input values) is:
2427 * error = (vlo-vhi+128)*65535 >> 24;
2429 * An alternative arithmetic calculation which also gives no errors is:
2431 * (V * 255 + 32895) >> 16
2434 png_int_32 tmp = *sp++; /* must be signed! */
2435 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
2436 *dp++ = (png_byte)tmp;
2439 row_info->bit_depth = 8;
2440 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2441 row_info->rowbytes = row_info->width * row_info->channels;
2444 #endif
2446 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2447 static void
2448 /* Simply discard the low byte. This was the default behavior prior
2449 * to libpng-1.5.4.
2451 png_do_chop(png_row_infop row_info, png_bytep row)
2453 png_debug(1, "in png_do_chop");
2455 if (row_info->bit_depth == 16)
2457 png_bytep sp = row; /* source */
2458 png_bytep dp = row; /* destination */
2459 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2461 while (sp < ep)
2463 *dp++ = *sp;
2464 sp += 2; /* skip low byte */
2467 row_info->bit_depth = 8;
2468 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2469 row_info->rowbytes = row_info->width * row_info->channels;
2472 #endif
2474 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2475 static void
2476 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2478 png_debug(1, "in png_do_read_swap_alpha");
2481 png_uint_32 row_width = row_info->width;
2482 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2484 /* This converts from RGBA to ARGB */
2485 if (row_info->bit_depth == 8)
2487 png_bytep sp = row + row_info->rowbytes;
2488 png_bytep dp = sp;
2489 png_byte save;
2490 png_uint_32 i;
2492 for (i = 0; i < row_width; i++)
2494 save = *(--sp);
2495 *(--dp) = *(--sp);
2496 *(--dp) = *(--sp);
2497 *(--dp) = *(--sp);
2498 *(--dp) = save;
2502 #ifdef PNG_READ_16BIT_SUPPORTED
2503 /* This converts from RRGGBBAA to AARRGGBB */
2504 else
2506 png_bytep sp = row + row_info->rowbytes;
2507 png_bytep dp = sp;
2508 png_byte save[2];
2509 png_uint_32 i;
2511 for (i = 0; i < row_width; i++)
2513 save[0] = *(--sp);
2514 save[1] = *(--sp);
2515 *(--dp) = *(--sp);
2516 *(--dp) = *(--sp);
2517 *(--dp) = *(--sp);
2518 *(--dp) = *(--sp);
2519 *(--dp) = *(--sp);
2520 *(--dp) = *(--sp);
2521 *(--dp) = save[0];
2522 *(--dp) = save[1];
2525 #endif
2528 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2530 /* This converts from GA to AG */
2531 if (row_info->bit_depth == 8)
2533 png_bytep sp = row + row_info->rowbytes;
2534 png_bytep dp = sp;
2535 png_byte save;
2536 png_uint_32 i;
2538 for (i = 0; i < row_width; i++)
2540 save = *(--sp);
2541 *(--dp) = *(--sp);
2542 *(--dp) = save;
2546 #ifdef PNG_READ_16BIT_SUPPORTED
2547 /* This converts from GGAA to AAGG */
2548 else
2550 png_bytep sp = row + row_info->rowbytes;
2551 png_bytep dp = sp;
2552 png_byte save[2];
2553 png_uint_32 i;
2555 for (i = 0; i < row_width; i++)
2557 save[0] = *(--sp);
2558 save[1] = *(--sp);
2559 *(--dp) = *(--sp);
2560 *(--dp) = *(--sp);
2561 *(--dp) = save[0];
2562 *(--dp) = save[1];
2565 #endif
2569 #endif
2571 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2572 static void
2573 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2575 png_uint_32 row_width;
2576 png_debug(1, "in png_do_read_invert_alpha");
2578 row_width = row_info->width;
2579 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2581 if (row_info->bit_depth == 8)
2583 /* This inverts the alpha channel in RGBA */
2584 png_bytep sp = row + row_info->rowbytes;
2585 png_bytep dp = sp;
2586 png_uint_32 i;
2588 for (i = 0; i < row_width; i++)
2590 *(--dp) = (png_byte)(255 - *(--sp));
2592 /* This does nothing:
2593 *(--dp) = *(--sp);
2594 *(--dp) = *(--sp);
2595 *(--dp) = *(--sp);
2596 We can replace it with:
2598 sp-=3;
2599 dp=sp;
2603 #ifdef PNG_READ_16BIT_SUPPORTED
2604 /* This inverts the alpha channel in RRGGBBAA */
2605 else
2607 png_bytep sp = row + row_info->rowbytes;
2608 png_bytep dp = sp;
2609 png_uint_32 i;
2611 for (i = 0; i < row_width; i++)
2613 *(--dp) = (png_byte)(255 - *(--sp));
2614 *(--dp) = (png_byte)(255 - *(--sp));
2616 /* This does nothing:
2617 *(--dp) = *(--sp);
2618 *(--dp) = *(--sp);
2619 *(--dp) = *(--sp);
2620 *(--dp) = *(--sp);
2621 *(--dp) = *(--sp);
2622 *(--dp) = *(--sp);
2623 We can replace it with:
2625 sp-=6;
2626 dp=sp;
2629 #endif
2631 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2633 if (row_info->bit_depth == 8)
2635 /* This inverts the alpha channel in GA */
2636 png_bytep sp = row + row_info->rowbytes;
2637 png_bytep dp = sp;
2638 png_uint_32 i;
2640 for (i = 0; i < row_width; i++)
2642 *(--dp) = (png_byte)(255 - *(--sp));
2643 *(--dp) = *(--sp);
2647 #ifdef PNG_READ_16BIT_SUPPORTED
2648 else
2650 /* This inverts the alpha channel in GGAA */
2651 png_bytep sp = row + row_info->rowbytes;
2652 png_bytep dp = sp;
2653 png_uint_32 i;
2655 for (i = 0; i < row_width; i++)
2657 *(--dp) = (png_byte)(255 - *(--sp));
2658 *(--dp) = (png_byte)(255 - *(--sp));
2660 *(--dp) = *(--sp);
2661 *(--dp) = *(--sp);
2663 sp-=2;
2664 dp=sp;
2667 #endif
2670 #endif
2672 #ifdef PNG_READ_FILLER_SUPPORTED
2673 /* Add filler channel if we have RGB color */
2674 static void
2675 png_do_read_filler(png_row_infop row_info, png_bytep row,
2676 png_uint_32 filler, png_uint_32 flags)
2678 png_uint_32 i;
2679 png_uint_32 row_width = row_info->width;
2681 #ifdef PNG_READ_16BIT_SUPPORTED
2682 png_byte hi_filler = (png_byte)(filler>>8);
2683 #endif
2684 png_byte lo_filler = (png_byte)filler;
2686 png_debug(1, "in png_do_read_filler");
2688 if (
2689 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2691 if (row_info->bit_depth == 8)
2693 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2695 /* This changes the data from G to GX */
2696 png_bytep sp = row + (png_size_t)row_width;
2697 png_bytep dp = sp + (png_size_t)row_width;
2698 for (i = 1; i < row_width; i++)
2700 *(--dp) = lo_filler;
2701 *(--dp) = *(--sp);
2703 *(--dp) = lo_filler;
2704 row_info->channels = 2;
2705 row_info->pixel_depth = 16;
2706 row_info->rowbytes = row_width * 2;
2709 else
2711 /* This changes the data from G to XG */
2712 png_bytep sp = row + (png_size_t)row_width;
2713 png_bytep dp = sp + (png_size_t)row_width;
2714 for (i = 0; i < row_width; i++)
2716 *(--dp) = *(--sp);
2717 *(--dp) = lo_filler;
2719 row_info->channels = 2;
2720 row_info->pixel_depth = 16;
2721 row_info->rowbytes = row_width * 2;
2725 #ifdef PNG_READ_16BIT_SUPPORTED
2726 else if (row_info->bit_depth == 16)
2728 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2730 /* This changes the data from GG to GGXX */
2731 png_bytep sp = row + (png_size_t)row_width * 2;
2732 png_bytep dp = sp + (png_size_t)row_width * 2;
2733 for (i = 1; i < row_width; i++)
2735 *(--dp) = lo_filler;
2736 *(--dp) = hi_filler;
2737 *(--dp) = *(--sp);
2738 *(--dp) = *(--sp);
2740 *(--dp) = lo_filler;
2741 *(--dp) = hi_filler;
2742 row_info->channels = 2;
2743 row_info->pixel_depth = 32;
2744 row_info->rowbytes = row_width * 4;
2747 else
2749 /* This changes the data from GG to XXGG */
2750 png_bytep sp = row + (png_size_t)row_width * 2;
2751 png_bytep dp = sp + (png_size_t)row_width * 2;
2752 for (i = 0; i < row_width; i++)
2754 *(--dp) = *(--sp);
2755 *(--dp) = *(--sp);
2756 *(--dp) = lo_filler;
2757 *(--dp) = hi_filler;
2759 row_info->channels = 2;
2760 row_info->pixel_depth = 32;
2761 row_info->rowbytes = row_width * 4;
2764 #endif
2765 } /* COLOR_TYPE == GRAY */
2766 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2768 if (row_info->bit_depth == 8)
2770 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2772 /* This changes the data from RGB to RGBX */
2773 png_bytep sp = row + (png_size_t)row_width * 3;
2774 png_bytep dp = sp + (png_size_t)row_width;
2775 for (i = 1; i < row_width; i++)
2777 *(--dp) = lo_filler;
2778 *(--dp) = *(--sp);
2779 *(--dp) = *(--sp);
2780 *(--dp) = *(--sp);
2782 *(--dp) = lo_filler;
2783 row_info->channels = 4;
2784 row_info->pixel_depth = 32;
2785 row_info->rowbytes = row_width * 4;
2788 else
2790 /* This changes the data from RGB to XRGB */
2791 png_bytep sp = row + (png_size_t)row_width * 3;
2792 png_bytep dp = sp + (png_size_t)row_width;
2793 for (i = 0; i < row_width; i++)
2795 *(--dp) = *(--sp);
2796 *(--dp) = *(--sp);
2797 *(--dp) = *(--sp);
2798 *(--dp) = lo_filler;
2800 row_info->channels = 4;
2801 row_info->pixel_depth = 32;
2802 row_info->rowbytes = row_width * 4;
2806 #ifdef PNG_READ_16BIT_SUPPORTED
2807 else if (row_info->bit_depth == 16)
2809 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2811 /* This changes the data from RRGGBB to RRGGBBXX */
2812 png_bytep sp = row + (png_size_t)row_width * 6;
2813 png_bytep dp = sp + (png_size_t)row_width * 2;
2814 for (i = 1; i < row_width; i++)
2816 *(--dp) = lo_filler;
2817 *(--dp) = hi_filler;
2818 *(--dp) = *(--sp);
2819 *(--dp) = *(--sp);
2820 *(--dp) = *(--sp);
2821 *(--dp) = *(--sp);
2822 *(--dp) = *(--sp);
2823 *(--dp) = *(--sp);
2825 *(--dp) = lo_filler;
2826 *(--dp) = hi_filler;
2827 row_info->channels = 4;
2828 row_info->pixel_depth = 64;
2829 row_info->rowbytes = row_width * 8;
2832 else
2834 /* This changes the data from RRGGBB to XXRRGGBB */
2835 png_bytep sp = row + (png_size_t)row_width * 6;
2836 png_bytep dp = sp + (png_size_t)row_width * 2;
2837 for (i = 0; i < row_width; i++)
2839 *(--dp) = *(--sp);
2840 *(--dp) = *(--sp);
2841 *(--dp) = *(--sp);
2842 *(--dp) = *(--sp);
2843 *(--dp) = *(--sp);
2844 *(--dp) = *(--sp);
2845 *(--dp) = lo_filler;
2846 *(--dp) = hi_filler;
2849 row_info->channels = 4;
2850 row_info->pixel_depth = 64;
2851 row_info->rowbytes = row_width * 8;
2854 #endif
2855 } /* COLOR_TYPE == RGB */
2857 #endif
2859 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2860 /* Expand grayscale files to RGB, with or without alpha */
2861 static void
2862 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2864 png_uint_32 i;
2865 png_uint_32 row_width = row_info->width;
2867 png_debug(1, "in png_do_gray_to_rgb");
2869 if (row_info->bit_depth >= 8 &&
2870 (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
2872 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2874 if (row_info->bit_depth == 8)
2876 /* This changes G to RGB */
2877 png_bytep sp = row + (png_size_t)row_width - 1;
2878 png_bytep dp = sp + (png_size_t)row_width * 2;
2879 for (i = 0; i < row_width; i++)
2881 *(dp--) = *sp;
2882 *(dp--) = *sp;
2883 *(dp--) = *(sp--);
2887 else
2889 /* This changes GG to RRGGBB */
2890 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2891 png_bytep dp = sp + (png_size_t)row_width * 4;
2892 for (i = 0; i < row_width; i++)
2894 *(dp--) = *sp;
2895 *(dp--) = *(sp - 1);
2896 *(dp--) = *sp;
2897 *(dp--) = *(sp - 1);
2898 *(dp--) = *(sp--);
2899 *(dp--) = *(sp--);
2904 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2906 if (row_info->bit_depth == 8)
2908 /* This changes GA to RGBA */
2909 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2910 png_bytep dp = sp + (png_size_t)row_width * 2;
2911 for (i = 0; i < row_width; i++)
2913 *(dp--) = *(sp--);
2914 *(dp--) = *sp;
2915 *(dp--) = *sp;
2916 *(dp--) = *(sp--);
2920 else
2922 /* This changes GGAA to RRGGBBAA */
2923 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2924 png_bytep dp = sp + (png_size_t)row_width * 4;
2925 for (i = 0; i < row_width; i++)
2927 *(dp--) = *(sp--);
2928 *(dp--) = *(sp--);
2929 *(dp--) = *sp;
2930 *(dp--) = *(sp - 1);
2931 *(dp--) = *sp;
2932 *(dp--) = *(sp - 1);
2933 *(dp--) = *(sp--);
2934 *(dp--) = *(sp--);
2938 row_info->channels = (png_byte)(row_info->channels + 2);
2939 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2940 row_info->pixel_depth = (png_byte)(row_info->channels *
2941 row_info->bit_depth);
2942 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2945 #endif
2947 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2948 /* Reduce RGB files to grayscale, with or without alpha
2949 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
2950 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
2951 * versions dated 1998 through November 2002 have been archived at
2952 * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
2953 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
2954 * Charles Poynton poynton at poynton.com
2956 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2958 * which can be expressed with integers as
2960 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2962 * Poynton's current link (as of January 2003 through July 2011):
2963 * <http://www.poynton.com/notes/colour_and_gamma/>
2964 * has changed the numbers slightly:
2966 * Y = 0.2126*R + 0.7152*G + 0.0722*B
2968 * which can be expressed with integers as
2970 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
2972 * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
2973 * end point chromaticities and the D65 white point. Depending on the
2974 * precision used for the D65 white point this produces a variety of different
2975 * numbers, however if the four decimal place value used in ITU-R Rec 709 is
2976 * used (0.3127,0.3290) the Y calculation would be:
2978 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
2980 * While this is correct the rounding results in an overflow for white, because
2981 * the sum of the rounded coefficients is 32769, not 32768. Consequently
2982 * libpng uses, instead, the closest non-overflowing approximation:
2984 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
2986 * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
2987 * (including an sRGB chunk) then the chromaticities are used to calculate the
2988 * coefficients. See the chunk handling in pngrutil.c for more information.
2990 * In all cases the calculation is to be done in a linear colorspace. If no
2991 * gamma information is available to correct the encoding of the original RGB
2992 * values this results in an implicit assumption that the original PNG RGB
2993 * values were linear.
2995 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
2996 * the API takes just red and green coefficients the blue coefficient is
2997 * calculated to make the sum 32768. This will result in different rounding
2998 * to that used above.
3000 static int
3001 png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
3004 int rgb_error = 0;
3006 png_debug(1, "in png_do_rgb_to_gray");
3008 if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3009 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
3011 PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3012 PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3013 PNG_CONST png_uint_32 bc = 32768 - rc - gc;
3014 PNG_CONST png_uint_32 row_width = row_info->width;
3015 PNG_CONST int have_alpha =
3016 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
3018 if (row_info->bit_depth == 8)
3020 #ifdef PNG_READ_GAMMA_SUPPORTED
3021 /* Notice that gamma to/from 1 are not necessarily inverses (if
3022 * there is an overall gamma correction). Prior to 1.5.5 this code
3023 * checked the linearized values for equality; this doesn't match
3024 * the documentation, the original values must be checked.
3026 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3028 png_bytep sp = row;
3029 png_bytep dp = row;
3030 png_uint_32 i;
3032 for (i = 0; i < row_width; i++)
3034 png_byte red = *(sp++);
3035 png_byte green = *(sp++);
3036 png_byte blue = *(sp++);
3038 if (red != green || red != blue)
3040 red = png_ptr->gamma_to_1[red];
3041 green = png_ptr->gamma_to_1[green];
3042 blue = png_ptr->gamma_to_1[blue];
3044 rgb_error |= 1;
3045 *(dp++) = png_ptr->gamma_from_1[
3046 (rc*red + gc*green + bc*blue + 16384)>>15];
3049 else
3051 /* If there is no overall correction the table will not be
3052 * set.
3054 if (png_ptr->gamma_table != NULL)
3055 red = png_ptr->gamma_table[red];
3057 *(dp++) = red;
3060 if (have_alpha != 0)
3061 *(dp++) = *(sp++);
3064 else
3065 #endif
3067 png_bytep sp = row;
3068 png_bytep dp = row;
3069 png_uint_32 i;
3071 for (i = 0; i < row_width; i++)
3073 png_byte red = *(sp++);
3074 png_byte green = *(sp++);
3075 png_byte blue = *(sp++);
3077 if (red != green || red != blue)
3079 rgb_error |= 1;
3080 /* NOTE: this is the historical approach which simply
3081 * truncates the results.
3083 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3086 else
3087 *(dp++) = red;
3089 if (have_alpha != 0)
3090 *(dp++) = *(sp++);
3095 else /* RGB bit_depth == 16 */
3097 #ifdef PNG_READ_GAMMA_SUPPORTED
3098 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3100 png_bytep sp = row;
3101 png_bytep dp = row;
3102 png_uint_32 i;
3104 for (i = 0; i < row_width; i++)
3106 png_uint_16 red, green, blue, w;
3107 png_byte hi,lo;
3109 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3110 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3111 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
3113 if (red == green && red == blue)
3115 if (png_ptr->gamma_16_table != NULL)
3116 w = png_ptr->gamma_16_table[(red & 0xff)
3117 >> png_ptr->gamma_shift][red >> 8];
3119 else
3120 w = red;
3123 else
3125 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff)
3126 >> png_ptr->gamma_shift][red>>8];
3127 png_uint_16 green_1 =
3128 png_ptr->gamma_16_to_1[(green & 0xff) >>
3129 png_ptr->gamma_shift][green>>8];
3130 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff)
3131 >> png_ptr->gamma_shift][blue>>8];
3132 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
3133 + bc*blue_1 + 16384)>>15);
3134 w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >>
3135 png_ptr->gamma_shift][gray16 >> 8];
3136 rgb_error |= 1;
3139 *(dp++) = (png_byte)((w>>8) & 0xff);
3140 *(dp++) = (png_byte)(w & 0xff);
3142 if (have_alpha != 0)
3144 *(dp++) = *(sp++);
3145 *(dp++) = *(sp++);
3149 else
3150 #endif
3152 png_bytep sp = row;
3153 png_bytep dp = row;
3154 png_uint_32 i;
3156 for (i = 0; i < row_width; i++)
3158 png_uint_16 red, green, blue, gray16;
3159 png_byte hi,lo;
3161 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3162 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3163 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
3165 if (red != green || red != blue)
3166 rgb_error |= 1;
3168 /* From 1.5.5 in the 16-bit case do the accurate conversion even
3169 * in the 'fast' case - this is because this is where the code
3170 * ends up when handling linear 16-bit data.
3172 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3173 15);
3174 *(dp++) = (png_byte)((gray16 >> 8) & 0xff);
3175 *(dp++) = (png_byte)(gray16 & 0xff);
3177 if (have_alpha != 0)
3179 *(dp++) = *(sp++);
3180 *(dp++) = *(sp++);
3186 row_info->channels = (png_byte)(row_info->channels - 2);
3187 row_info->color_type = (png_byte)(row_info->color_type &
3188 ~PNG_COLOR_MASK_COLOR);
3189 row_info->pixel_depth = (png_byte)(row_info->channels *
3190 row_info->bit_depth);
3191 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3193 return rgb_error;
3195 #endif
3197 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
3198 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
3199 /* Replace any alpha or transparency with the supplied background color.
3200 * "background" is already in the screen gamma, while "background_1" is
3201 * at a gamma of 1.0. Paletted files have already been taken care of.
3203 static void
3204 png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
3206 #ifdef PNG_READ_GAMMA_SUPPORTED
3207 png_const_bytep gamma_table = png_ptr->gamma_table;
3208 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3209 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3210 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3211 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3212 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3213 int gamma_shift = png_ptr->gamma_shift;
3214 int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
3215 #endif
3217 png_bytep sp;
3218 png_uint_32 i;
3219 png_uint_32 row_width = row_info->width;
3220 int shift;
3222 png_debug(1, "in png_do_compose");
3225 switch (row_info->color_type)
3227 case PNG_COLOR_TYPE_GRAY:
3229 switch (row_info->bit_depth)
3231 case 1:
3233 sp = row;
3234 shift = 7;
3235 for (i = 0; i < row_width; i++)
3237 if ((png_uint_16)((*sp >> shift) & 0x01)
3238 == png_ptr->trans_color.gray)
3240 unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
3241 tmp |=
3242 (unsigned int)(png_ptr->background.gray << shift);
3243 *sp = (png_byte)(tmp & 0xff);
3246 if (shift == 0)
3248 shift = 7;
3249 sp++;
3252 else
3253 shift--;
3255 break;
3258 case 2:
3260 #ifdef PNG_READ_GAMMA_SUPPORTED
3261 if (gamma_table != NULL)
3263 sp = row;
3264 shift = 6;
3265 for (i = 0; i < row_width; i++)
3267 if ((png_uint_16)((*sp >> shift) & 0x03)
3268 == png_ptr->trans_color.gray)
3270 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3271 tmp |=
3272 (unsigned int)png_ptr->background.gray << shift;
3273 *sp = (png_byte)(tmp & 0xff);
3276 else
3278 unsigned int p = (*sp >> shift) & 0x03;
3279 unsigned int g = (gamma_table [p | (p << 2) |
3280 (p << 4) | (p << 6)] >> 6) & 0x03;
3281 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3282 tmp |= (unsigned int)(g << shift);
3283 *sp = (png_byte)(tmp & 0xff);
3286 if (shift == 0)
3288 shift = 6;
3289 sp++;
3292 else
3293 shift -= 2;
3297 else
3298 #endif
3300 sp = row;
3301 shift = 6;
3302 for (i = 0; i < row_width; i++)
3304 if ((png_uint_16)((*sp >> shift) & 0x03)
3305 == png_ptr->trans_color.gray)
3307 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3308 tmp |=
3309 (unsigned int)png_ptr->background.gray << shift;
3310 *sp = (png_byte)(tmp & 0xff);
3313 if (shift == 0)
3315 shift = 6;
3316 sp++;
3319 else
3320 shift -= 2;
3323 break;
3326 case 4:
3328 #ifdef PNG_READ_GAMMA_SUPPORTED
3329 if (gamma_table != NULL)
3331 sp = row;
3332 shift = 4;
3333 for (i = 0; i < row_width; i++)
3335 if ((png_uint_16)((*sp >> shift) & 0x0f)
3336 == png_ptr->trans_color.gray)
3338 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3339 tmp |=
3340 (unsigned int)(png_ptr->background.gray << shift);
3341 *sp = (png_byte)(tmp & 0xff);
3344 else
3346 unsigned int p = (*sp >> shift) & 0x0f;
3347 unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
3348 0x0f;
3349 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3350 tmp |= (unsigned int)(g << shift);
3351 *sp = (png_byte)(tmp & 0xff);
3354 if (shift == 0)
3356 shift = 4;
3357 sp++;
3360 else
3361 shift -= 4;
3365 else
3366 #endif
3368 sp = row;
3369 shift = 4;
3370 for (i = 0; i < row_width; i++)
3372 if ((png_uint_16)((*sp >> shift) & 0x0f)
3373 == png_ptr->trans_color.gray)
3375 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3376 tmp |=
3377 (unsigned int)(png_ptr->background.gray << shift);
3378 *sp = (png_byte)(tmp & 0xff);
3381 if (shift == 0)
3383 shift = 4;
3384 sp++;
3387 else
3388 shift -= 4;
3391 break;
3394 case 8:
3396 #ifdef PNG_READ_GAMMA_SUPPORTED
3397 if (gamma_table != NULL)
3399 sp = row;
3400 for (i = 0; i < row_width; i++, sp++)
3402 if (*sp == png_ptr->trans_color.gray)
3403 *sp = (png_byte)png_ptr->background.gray;
3405 else
3406 *sp = gamma_table[*sp];
3409 else
3410 #endif
3412 sp = row;
3413 for (i = 0; i < row_width; i++, sp++)
3415 if (*sp == png_ptr->trans_color.gray)
3416 *sp = (png_byte)png_ptr->background.gray;
3419 break;
3422 case 16:
3424 #ifdef PNG_READ_GAMMA_SUPPORTED
3425 if (gamma_16 != NULL)
3427 sp = row;
3428 for (i = 0; i < row_width; i++, sp += 2)
3430 png_uint_16 v;
3432 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3434 if (v == png_ptr->trans_color.gray)
3436 /* Background is already in screen gamma */
3437 *sp = (png_byte)((png_ptr->background.gray >> 8)
3438 & 0xff);
3439 *(sp + 1) = (png_byte)(png_ptr->background.gray
3440 & 0xff);
3443 else
3445 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3446 *sp = (png_byte)((v >> 8) & 0xff);
3447 *(sp + 1) = (png_byte)(v & 0xff);
3451 else
3452 #endif
3454 sp = row;
3455 for (i = 0; i < row_width; i++, sp += 2)
3457 png_uint_16 v;
3459 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3461 if (v == png_ptr->trans_color.gray)
3463 *sp = (png_byte)((png_ptr->background.gray >> 8)
3464 & 0xff);
3465 *(sp + 1) = (png_byte)(png_ptr->background.gray
3466 & 0xff);
3470 break;
3473 default:
3474 break;
3476 break;
3479 case PNG_COLOR_TYPE_RGB:
3481 if (row_info->bit_depth == 8)
3483 #ifdef PNG_READ_GAMMA_SUPPORTED
3484 if (gamma_table != NULL)
3486 sp = row;
3487 for (i = 0; i < row_width; i++, sp += 3)
3489 if (*sp == png_ptr->trans_color.red &&
3490 *(sp + 1) == png_ptr->trans_color.green &&
3491 *(sp + 2) == png_ptr->trans_color.blue)
3493 *sp = (png_byte)png_ptr->background.red;
3494 *(sp + 1) = (png_byte)png_ptr->background.green;
3495 *(sp + 2) = (png_byte)png_ptr->background.blue;
3498 else
3500 *sp = gamma_table[*sp];
3501 *(sp + 1) = gamma_table[*(sp + 1)];
3502 *(sp + 2) = gamma_table[*(sp + 2)];
3506 else
3507 #endif
3509 sp = row;
3510 for (i = 0; i < row_width; i++, sp += 3)
3512 if (*sp == png_ptr->trans_color.red &&
3513 *(sp + 1) == png_ptr->trans_color.green &&
3514 *(sp + 2) == png_ptr->trans_color.blue)
3516 *sp = (png_byte)png_ptr->background.red;
3517 *(sp + 1) = (png_byte)png_ptr->background.green;
3518 *(sp + 2) = (png_byte)png_ptr->background.blue;
3523 else /* if (row_info->bit_depth == 16) */
3525 #ifdef PNG_READ_GAMMA_SUPPORTED
3526 if (gamma_16 != NULL)
3528 sp = row;
3529 for (i = 0; i < row_width; i++, sp += 6)
3531 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3533 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3534 + *(sp + 3));
3536 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3537 + *(sp + 5));
3539 if (r == png_ptr->trans_color.red &&
3540 g == png_ptr->trans_color.green &&
3541 b == png_ptr->trans_color.blue)
3543 /* Background is already in screen gamma */
3544 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3545 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3546 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3547 & 0xff);
3548 *(sp + 3) = (png_byte)(png_ptr->background.green
3549 & 0xff);
3550 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3551 & 0xff);
3552 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3555 else
3557 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3558 *sp = (png_byte)((v >> 8) & 0xff);
3559 *(sp + 1) = (png_byte)(v & 0xff);
3561 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3562 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3563 *(sp + 3) = (png_byte)(v & 0xff);
3565 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3566 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3567 *(sp + 5) = (png_byte)(v & 0xff);
3572 else
3573 #endif
3575 sp = row;
3576 for (i = 0; i < row_width; i++, sp += 6)
3578 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3580 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3581 + *(sp + 3));
3583 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3584 + *(sp + 5));
3586 if (r == png_ptr->trans_color.red &&
3587 g == png_ptr->trans_color.green &&
3588 b == png_ptr->trans_color.blue)
3590 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3591 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3592 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3593 & 0xff);
3594 *(sp + 3) = (png_byte)(png_ptr->background.green
3595 & 0xff);
3596 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3597 & 0xff);
3598 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3603 break;
3606 case PNG_COLOR_TYPE_GRAY_ALPHA:
3608 if (row_info->bit_depth == 8)
3610 #ifdef PNG_READ_GAMMA_SUPPORTED
3611 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3612 gamma_table != NULL)
3614 sp = row;
3615 for (i = 0; i < row_width; i++, sp += 2)
3617 png_uint_16 a = *(sp + 1);
3619 if (a == 0xff)
3620 *sp = gamma_table[*sp];
3622 else if (a == 0)
3624 /* Background is already in screen gamma */
3625 *sp = (png_byte)png_ptr->background.gray;
3628 else
3630 png_byte v, w;
3632 v = gamma_to_1[*sp];
3633 png_composite(w, v, a, png_ptr->background_1.gray);
3634 if (optimize == 0)
3635 w = gamma_from_1[w];
3636 *sp = w;
3640 else
3641 #endif
3643 sp = row;
3644 for (i = 0; i < row_width; i++, sp += 2)
3646 png_byte a = *(sp + 1);
3648 if (a == 0)
3649 *sp = (png_byte)png_ptr->background.gray;
3651 else if (a < 0xff)
3652 png_composite(*sp, *sp, a, png_ptr->background.gray);
3656 else /* if (png_ptr->bit_depth == 16) */
3658 #ifdef PNG_READ_GAMMA_SUPPORTED
3659 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3660 gamma_16_to_1 != NULL)
3662 sp = row;
3663 for (i = 0; i < row_width; i++, sp += 4)
3665 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3666 + *(sp + 3));
3668 if (a == (png_uint_16)0xffff)
3670 png_uint_16 v;
3672 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3673 *sp = (png_byte)((v >> 8) & 0xff);
3674 *(sp + 1) = (png_byte)(v & 0xff);
3677 else if (a == 0)
3679 /* Background is already in screen gamma */
3680 *sp = (png_byte)((png_ptr->background.gray >> 8)
3681 & 0xff);
3682 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3685 else
3687 png_uint_16 g, v, w;
3689 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3690 png_composite_16(v, g, a, png_ptr->background_1.gray);
3691 if (optimize != 0)
3692 w = v;
3693 else
3694 w = gamma_16_from_1[(v & 0xff) >>
3695 gamma_shift][v >> 8];
3696 *sp = (png_byte)((w >> 8) & 0xff);
3697 *(sp + 1) = (png_byte)(w & 0xff);
3701 else
3702 #endif
3704 sp = row;
3705 for (i = 0; i < row_width; i++, sp += 4)
3707 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3708 + *(sp + 3));
3710 if (a == 0)
3712 *sp = (png_byte)((png_ptr->background.gray >> 8)
3713 & 0xff);
3714 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3717 else if (a < 0xffff)
3719 png_uint_16 g, v;
3721 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3722 png_composite_16(v, g, a, png_ptr->background.gray);
3723 *sp = (png_byte)((v >> 8) & 0xff);
3724 *(sp + 1) = (png_byte)(v & 0xff);
3729 break;
3732 case PNG_COLOR_TYPE_RGB_ALPHA:
3734 if (row_info->bit_depth == 8)
3736 #ifdef PNG_READ_GAMMA_SUPPORTED
3737 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3738 gamma_table != NULL)
3740 sp = row;
3741 for (i = 0; i < row_width; i++, sp += 4)
3743 png_byte a = *(sp + 3);
3745 if (a == 0xff)
3747 *sp = gamma_table[*sp];
3748 *(sp + 1) = gamma_table[*(sp + 1)];
3749 *(sp + 2) = gamma_table[*(sp + 2)];
3752 else if (a == 0)
3754 /* Background is already in screen gamma */
3755 *sp = (png_byte)png_ptr->background.red;
3756 *(sp + 1) = (png_byte)png_ptr->background.green;
3757 *(sp + 2) = (png_byte)png_ptr->background.blue;
3760 else
3762 png_byte v, w;
3764 v = gamma_to_1[*sp];
3765 png_composite(w, v, a, png_ptr->background_1.red);
3766 if (optimize == 0) w = gamma_from_1[w];
3767 *sp = w;
3769 v = gamma_to_1[*(sp + 1)];
3770 png_composite(w, v, a, png_ptr->background_1.green);
3771 if (optimize == 0) w = gamma_from_1[w];
3772 *(sp + 1) = w;
3774 v = gamma_to_1[*(sp + 2)];
3775 png_composite(w, v, a, png_ptr->background_1.blue);
3776 if (optimize == 0) w = gamma_from_1[w];
3777 *(sp + 2) = w;
3781 else
3782 #endif
3784 sp = row;
3785 for (i = 0; i < row_width; i++, sp += 4)
3787 png_byte a = *(sp + 3);
3789 if (a == 0)
3791 *sp = (png_byte)png_ptr->background.red;
3792 *(sp + 1) = (png_byte)png_ptr->background.green;
3793 *(sp + 2) = (png_byte)png_ptr->background.blue;
3796 else if (a < 0xff)
3798 png_composite(*sp, *sp, a, png_ptr->background.red);
3800 png_composite(*(sp + 1), *(sp + 1), a,
3801 png_ptr->background.green);
3803 png_composite(*(sp + 2), *(sp + 2), a,
3804 png_ptr->background.blue);
3809 else /* if (row_info->bit_depth == 16) */
3811 #ifdef PNG_READ_GAMMA_SUPPORTED
3812 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3813 gamma_16_to_1 != NULL)
3815 sp = row;
3816 for (i = 0; i < row_width; i++, sp += 8)
3818 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3819 << 8) + (png_uint_16)(*(sp + 7)));
3821 if (a == (png_uint_16)0xffff)
3823 png_uint_16 v;
3825 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3826 *sp = (png_byte)((v >> 8) & 0xff);
3827 *(sp + 1) = (png_byte)(v & 0xff);
3829 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3830 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3831 *(sp + 3) = (png_byte)(v & 0xff);
3833 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3834 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3835 *(sp + 5) = (png_byte)(v & 0xff);
3838 else if (a == 0)
3840 /* Background is already in screen gamma */
3841 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3842 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3843 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3844 & 0xff);
3845 *(sp + 3) = (png_byte)(png_ptr->background.green
3846 & 0xff);
3847 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3848 & 0xff);
3849 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3852 else
3854 png_uint_16 v, w;
3856 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3857 png_composite_16(w, v, a, png_ptr->background_1.red);
3858 if (optimize == 0)
3859 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3861 *sp = (png_byte)((w >> 8) & 0xff);
3862 *(sp + 1) = (png_byte)(w & 0xff);
3864 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3865 png_composite_16(w, v, a, png_ptr->background_1.green);
3866 if (optimize == 0)
3867 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3870 *(sp + 2) = (png_byte)((w >> 8) & 0xff);
3871 *(sp + 3) = (png_byte)(w & 0xff);
3873 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3874 png_composite_16(w, v, a, png_ptr->background_1.blue);
3875 if (optimize == 0)
3876 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3879 *(sp + 4) = (png_byte)((w >> 8) & 0xff);
3880 *(sp + 5) = (png_byte)(w & 0xff);
3885 else
3886 #endif
3888 sp = row;
3889 for (i = 0; i < row_width; i++, sp += 8)
3891 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3892 << 8) + (png_uint_16)(*(sp + 7)));
3894 if (a == 0)
3896 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3897 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3898 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3899 & 0xff);
3900 *(sp + 3) = (png_byte)(png_ptr->background.green
3901 & 0xff);
3902 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3903 & 0xff);
3904 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3907 else if (a < 0xffff)
3909 png_uint_16 v;
3911 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3912 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3913 + *(sp + 3));
3914 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3915 + *(sp + 5));
3917 png_composite_16(v, r, a, png_ptr->background.red);
3918 *sp = (png_byte)((v >> 8) & 0xff);
3919 *(sp + 1) = (png_byte)(v & 0xff);
3921 png_composite_16(v, g, a, png_ptr->background.green);
3922 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3923 *(sp + 3) = (png_byte)(v & 0xff);
3925 png_composite_16(v, b, a, png_ptr->background.blue);
3926 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3927 *(sp + 5) = (png_byte)(v & 0xff);
3932 break;
3935 default:
3936 break;
3940 #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
3942 #ifdef PNG_READ_GAMMA_SUPPORTED
3943 /* Gamma correct the image, avoiding the alpha channel. Make sure
3944 * you do this after you deal with the transparency issue on grayscale
3945 * or RGB images. If your bit depth is 8, use gamma_table, if it
3946 * is 16, use gamma_16_table and gamma_shift. Build these with
3947 * build_gamma_table().
3949 static void
3950 png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
3952 png_const_bytep gamma_table = png_ptr->gamma_table;
3953 png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
3954 int gamma_shift = png_ptr->gamma_shift;
3956 png_bytep sp;
3957 png_uint_32 i;
3958 png_uint_32 row_width=row_info->width;
3960 png_debug(1, "in png_do_gamma");
3962 if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3963 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3965 switch (row_info->color_type)
3967 case PNG_COLOR_TYPE_RGB:
3969 if (row_info->bit_depth == 8)
3971 sp = row;
3972 for (i = 0; i < row_width; i++)
3974 *sp = gamma_table[*sp];
3975 sp++;
3976 *sp = gamma_table[*sp];
3977 sp++;
3978 *sp = gamma_table[*sp];
3979 sp++;
3983 else /* if (row_info->bit_depth == 16) */
3985 sp = row;
3986 for (i = 0; i < row_width; i++)
3988 png_uint_16 v;
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;
3995 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3996 *sp = (png_byte)((v >> 8) & 0xff);
3997 *(sp + 1) = (png_byte)(v & 0xff);
3998 sp += 2;
4000 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4001 *sp = (png_byte)((v >> 8) & 0xff);
4002 *(sp + 1) = (png_byte)(v & 0xff);
4003 sp += 2;
4006 break;
4009 case PNG_COLOR_TYPE_RGB_ALPHA:
4011 if (row_info->bit_depth == 8)
4013 sp = row;
4014 for (i = 0; i < row_width; i++)
4016 *sp = gamma_table[*sp];
4017 sp++;
4019 *sp = gamma_table[*sp];
4020 sp++;
4022 *sp = gamma_table[*sp];
4023 sp++;
4025 sp++;
4029 else /* if (row_info->bit_depth == 16) */
4031 sp = row;
4032 for (i = 0; i < row_width; i++)
4034 png_uint_16 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 += 2;
4039 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4040 *sp = (png_byte)((v >> 8) & 0xff);
4041 *(sp + 1) = (png_byte)(v & 0xff);
4042 sp += 2;
4044 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4045 *sp = (png_byte)((v >> 8) & 0xff);
4046 *(sp + 1) = (png_byte)(v & 0xff);
4047 sp += 4;
4050 break;
4053 case PNG_COLOR_TYPE_GRAY_ALPHA:
4055 if (row_info->bit_depth == 8)
4057 sp = row;
4058 for (i = 0; i < row_width; i++)
4060 *sp = gamma_table[*sp];
4061 sp += 2;
4065 else /* if (row_info->bit_depth == 16) */
4067 sp = row;
4068 for (i = 0; i < row_width; i++)
4070 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4071 *sp = (png_byte)((v >> 8) & 0xff);
4072 *(sp + 1) = (png_byte)(v & 0xff);
4073 sp += 4;
4076 break;
4079 case PNG_COLOR_TYPE_GRAY:
4081 if (row_info->bit_depth == 2)
4083 sp = row;
4084 for (i = 0; i < row_width; i += 4)
4086 int a = *sp & 0xc0;
4087 int b = *sp & 0x30;
4088 int c = *sp & 0x0c;
4089 int d = *sp & 0x03;
4091 *sp = (png_byte)(
4092 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
4093 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
4094 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
4095 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
4096 sp++;
4100 if (row_info->bit_depth == 4)
4102 sp = row;
4103 for (i = 0; i < row_width; i += 2)
4105 int msb = *sp & 0xf0;
4106 int lsb = *sp & 0x0f;
4108 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
4109 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
4110 sp++;
4114 else if (row_info->bit_depth == 8)
4116 sp = row;
4117 for (i = 0; i < row_width; i++)
4119 *sp = gamma_table[*sp];
4120 sp++;
4124 else if (row_info->bit_depth == 16)
4126 sp = row;
4127 for (i = 0; i < row_width; i++)
4129 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4130 *sp = (png_byte)((v >> 8) & 0xff);
4131 *(sp + 1) = (png_byte)(v & 0xff);
4132 sp += 2;
4135 break;
4138 default:
4139 break;
4143 #endif
4145 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4146 /* Encode the alpha channel to the output gamma (the input channel is always
4147 * linear.) Called only with color types that have an alpha channel. Needs the
4148 * from_1 tables.
4150 static void
4151 png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
4153 png_uint_32 row_width = row_info->width;
4155 png_debug(1, "in png_do_encode_alpha");
4157 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4159 if (row_info->bit_depth == 8)
4161 PNG_CONST png_bytep table = png_ptr->gamma_from_1;
4163 if (table != NULL)
4165 PNG_CONST int step =
4166 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
4168 /* The alpha channel is the last component: */
4169 row += step - 1;
4171 for (; row_width > 0; --row_width, row += step)
4172 *row = table[*row];
4174 return;
4178 else if (row_info->bit_depth == 16)
4180 PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1;
4181 PNG_CONST int gamma_shift = png_ptr->gamma_shift;
4183 if (table != NULL)
4185 PNG_CONST int step =
4186 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
4188 /* The alpha channel is the last component: */
4189 row += step - 2;
4191 for (; row_width > 0; --row_width, row += step)
4193 png_uint_16 v;
4195 v = table[*(row + 1) >> gamma_shift][*row];
4196 *row = (png_byte)((v >> 8) & 0xff);
4197 *(row + 1) = (png_byte)(v & 0xff);
4200 return;
4205 /* Only get to here if called with a weird row_info; no harm has been done,
4206 * so just issue a warning.
4208 png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
4210 #endif
4212 #ifdef PNG_READ_EXPAND_SUPPORTED
4213 /* Expands a palette row to an RGB or RGBA row depending
4214 * upon whether you supply trans and num_trans.
4216 static void
4217 png_do_expand_palette(png_row_infop row_info, png_bytep row,
4218 png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
4220 int shift, value;
4221 png_bytep sp, dp;
4222 png_uint_32 i;
4223 png_uint_32 row_width=row_info->width;
4225 png_debug(1, "in png_do_expand_palette");
4227 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4229 if (row_info->bit_depth < 8)
4231 switch (row_info->bit_depth)
4233 case 1:
4235 sp = row + (png_size_t)((row_width - 1) >> 3);
4236 dp = row + (png_size_t)row_width - 1;
4237 shift = 7 - (int)((row_width + 7) & 0x07);
4238 for (i = 0; i < row_width; i++)
4240 if ((*sp >> shift) & 0x01)
4241 *dp = 1;
4243 else
4244 *dp = 0;
4246 if (shift == 7)
4248 shift = 0;
4249 sp--;
4252 else
4253 shift++;
4255 dp--;
4257 break;
4260 case 2:
4262 sp = row + (png_size_t)((row_width - 1) >> 2);
4263 dp = row + (png_size_t)row_width - 1;
4264 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4265 for (i = 0; i < row_width; i++)
4267 value = (*sp >> shift) & 0x03;
4268 *dp = (png_byte)value;
4269 if (shift == 6)
4271 shift = 0;
4272 sp--;
4275 else
4276 shift += 2;
4278 dp--;
4280 break;
4283 case 4:
4285 sp = row + (png_size_t)((row_width - 1) >> 1);
4286 dp = row + (png_size_t)row_width - 1;
4287 shift = (int)((row_width & 0x01) << 2);
4288 for (i = 0; i < row_width; i++)
4290 value = (*sp >> shift) & 0x0f;
4291 *dp = (png_byte)value;
4292 if (shift == 4)
4294 shift = 0;
4295 sp--;
4298 else
4299 shift += 4;
4301 dp--;
4303 break;
4306 default:
4307 break;
4309 row_info->bit_depth = 8;
4310 row_info->pixel_depth = 8;
4311 row_info->rowbytes = row_width;
4314 if (row_info->bit_depth == 8)
4317 if (num_trans > 0)
4319 sp = row + (png_size_t)row_width - 1;
4320 dp = row + (png_size_t)(row_width << 2) - 1;
4322 for (i = 0; i < row_width; i++)
4324 if ((int)(*sp) >= num_trans)
4325 *dp-- = 0xff;
4327 else
4328 *dp-- = trans_alpha[*sp];
4330 *dp-- = palette[*sp].blue;
4331 *dp-- = palette[*sp].green;
4332 *dp-- = palette[*sp].red;
4333 sp--;
4335 row_info->bit_depth = 8;
4336 row_info->pixel_depth = 32;
4337 row_info->rowbytes = row_width * 4;
4338 row_info->color_type = 6;
4339 row_info->channels = 4;
4342 else
4344 sp = row + (png_size_t)row_width - 1;
4345 dp = row + (png_size_t)(row_width * 3) - 1;
4347 for (i = 0; i < row_width; i++)
4349 *dp-- = palette[*sp].blue;
4350 *dp-- = palette[*sp].green;
4351 *dp-- = palette[*sp].red;
4352 sp--;
4355 row_info->bit_depth = 8;
4356 row_info->pixel_depth = 24;
4357 row_info->rowbytes = row_width * 3;
4358 row_info->color_type = 2;
4359 row_info->channels = 3;
4366 /* If the bit depth < 8, it is expanded to 8. Also, if the already
4367 * expanded transparency value is supplied, an alpha channel is built.
4369 static void
4370 png_do_expand(png_row_infop row_info, png_bytep row,
4371 png_const_color_16p trans_color)
4373 int shift, value;
4374 png_bytep sp, dp;
4375 png_uint_32 i;
4376 png_uint_32 row_width=row_info->width;
4378 png_debug(1, "in png_do_expand");
4381 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
4383 unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
4385 if (row_info->bit_depth < 8)
4387 switch (row_info->bit_depth)
4389 case 1:
4391 gray = (gray & 0x01) * 0xff;
4392 sp = row + (png_size_t)((row_width - 1) >> 3);
4393 dp = row + (png_size_t)row_width - 1;
4394 shift = 7 - (int)((row_width + 7) & 0x07);
4395 for (i = 0; i < row_width; i++)
4397 if ((*sp >> shift) & 0x01)
4398 *dp = 0xff;
4400 else
4401 *dp = 0;
4403 if (shift == 7)
4405 shift = 0;
4406 sp--;
4409 else
4410 shift++;
4412 dp--;
4414 break;
4417 case 2:
4419 gray = (gray & 0x03) * 0x55;
4420 sp = row + (png_size_t)((row_width - 1) >> 2);
4421 dp = row + (png_size_t)row_width - 1;
4422 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4423 for (i = 0; i < row_width; i++)
4425 value = (*sp >> shift) & 0x03;
4426 *dp = (png_byte)(value | (value << 2) | (value << 4) |
4427 (value << 6));
4428 if (shift == 6)
4430 shift = 0;
4431 sp--;
4434 else
4435 shift += 2;
4437 dp--;
4439 break;
4442 case 4:
4444 gray = (gray & 0x0f) * 0x11;
4445 sp = row + (png_size_t)((row_width - 1) >> 1);
4446 dp = row + (png_size_t)row_width - 1;
4447 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
4448 for (i = 0; i < row_width; i++)
4450 value = (*sp >> shift) & 0x0f;
4451 *dp = (png_byte)(value | (value << 4));
4452 if (shift == 4)
4454 shift = 0;
4455 sp--;
4458 else
4459 shift = 4;
4461 dp--;
4463 break;
4466 default:
4467 break;
4470 row_info->bit_depth = 8;
4471 row_info->pixel_depth = 8;
4472 row_info->rowbytes = row_width;
4475 if (trans_color != NULL)
4477 if (row_info->bit_depth == 8)
4479 gray = gray & 0xff;
4480 sp = row + (png_size_t)row_width - 1;
4481 dp = row + (png_size_t)(row_width << 1) - 1;
4483 for (i = 0; i < row_width; i++)
4485 if ((*sp & 0xffU) == gray)
4486 *dp-- = 0;
4488 else
4489 *dp-- = 0xff;
4491 *dp-- = *sp--;
4495 else if (row_info->bit_depth == 16)
4497 unsigned int gray_high = (gray >> 8) & 0xff;
4498 unsigned int gray_low = gray & 0xff;
4499 sp = row + row_info->rowbytes - 1;
4500 dp = row + (row_info->rowbytes << 1) - 1;
4501 for (i = 0; i < row_width; i++)
4503 if ((*(sp - 1) & 0xffU) == gray_high &&
4504 (*(sp) & 0xffU) == gray_low)
4506 *dp-- = 0;
4507 *dp-- = 0;
4510 else
4512 *dp-- = 0xff;
4513 *dp-- = 0xff;
4516 *dp-- = *sp--;
4517 *dp-- = *sp--;
4521 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4522 row_info->channels = 2;
4523 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4524 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4525 row_width);
4528 else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4529 trans_color != NULL)
4531 if (row_info->bit_depth == 8)
4533 png_byte red = (png_byte)(trans_color->red & 0xff);
4534 png_byte green = (png_byte)(trans_color->green & 0xff);
4535 png_byte blue = (png_byte)(trans_color->blue & 0xff);
4536 sp = row + (png_size_t)row_info->rowbytes - 1;
4537 dp = row + (png_size_t)(row_width << 2) - 1;
4538 for (i = 0; i < row_width; i++)
4540 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4541 *dp-- = 0;
4543 else
4544 *dp-- = 0xff;
4546 *dp-- = *sp--;
4547 *dp-- = *sp--;
4548 *dp-- = *sp--;
4551 else if (row_info->bit_depth == 16)
4553 png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
4554 png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
4555 png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
4556 png_byte red_low = (png_byte)(trans_color->red & 0xff);
4557 png_byte green_low = (png_byte)(trans_color->green & 0xff);
4558 png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
4559 sp = row + row_info->rowbytes - 1;
4560 dp = row + (png_size_t)(row_width << 3) - 1;
4561 for (i = 0; i < row_width; i++)
4563 if (*(sp - 5) == red_high &&
4564 *(sp - 4) == red_low &&
4565 *(sp - 3) == green_high &&
4566 *(sp - 2) == green_low &&
4567 *(sp - 1) == blue_high &&
4568 *(sp ) == blue_low)
4570 *dp-- = 0;
4571 *dp-- = 0;
4574 else
4576 *dp-- = 0xff;
4577 *dp-- = 0xff;
4580 *dp-- = *sp--;
4581 *dp-- = *sp--;
4582 *dp-- = *sp--;
4583 *dp-- = *sp--;
4584 *dp-- = *sp--;
4585 *dp-- = *sp--;
4588 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4589 row_info->channels = 4;
4590 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4591 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4595 #endif
4597 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4598 /* If the bit depth is 8 and the color type is not a palette type expand the
4599 * whole row to 16 bits. Has no effect otherwise.
4601 static void
4602 png_do_expand_16(png_row_infop row_info, png_bytep row)
4604 if (row_info->bit_depth == 8 &&
4605 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4607 /* The row have a sequence of bytes containing [0..255] and we need
4608 * to turn it into another row containing [0..65535], to do this we
4609 * calculate:
4611 * (input / 255) * 65535
4613 * Which happens to be exactly input * 257 and this can be achieved
4614 * simply by byte replication in place (copying backwards).
4616 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4617 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
4618 while (dp > sp)
4619 dp[-2] = dp[-1] = *--sp, dp -= 2;
4621 row_info->rowbytes *= 2;
4622 row_info->bit_depth = 16;
4623 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4626 #endif
4628 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4629 static void
4630 png_do_quantize(png_row_infop row_info, png_bytep row,
4631 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4633 png_bytep sp, dp;
4634 png_uint_32 i;
4635 png_uint_32 row_width=row_info->width;
4637 png_debug(1, "in png_do_quantize");
4639 if (row_info->bit_depth == 8)
4641 if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
4643 int r, g, b, p;
4644 sp = row;
4645 dp = row;
4646 for (i = 0; i < row_width; i++)
4648 r = *sp++;
4649 g = *sp++;
4650 b = *sp++;
4652 /* This looks real messy, but the compiler will reduce
4653 * it down to a reasonable formula. For example, with
4654 * 5 bits per color, we get:
4655 * p = (((r >> 3) & 0x1f) << 10) |
4656 * (((g >> 3) & 0x1f) << 5) |
4657 * ((b >> 3) & 0x1f);
4659 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4660 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4661 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4662 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4663 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4664 (PNG_QUANTIZE_BLUE_BITS)) |
4665 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4666 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4668 *dp++ = palette_lookup[p];
4671 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4672 row_info->channels = 1;
4673 row_info->pixel_depth = row_info->bit_depth;
4674 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4677 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4678 palette_lookup != NULL)
4680 int r, g, b, p;
4681 sp = row;
4682 dp = row;
4683 for (i = 0; i < row_width; i++)
4685 r = *sp++;
4686 g = *sp++;
4687 b = *sp++;
4688 sp++;
4690 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4691 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4692 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4693 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4694 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4695 (PNG_QUANTIZE_BLUE_BITS)) |
4696 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4697 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4699 *dp++ = palette_lookup[p];
4702 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4703 row_info->channels = 1;
4704 row_info->pixel_depth = row_info->bit_depth;
4705 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4708 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4709 quantize_lookup)
4711 sp = row;
4713 for (i = 0; i < row_width; i++, sp++)
4715 *sp = quantize_lookup[*sp];
4720 #endif /* READ_QUANTIZE */
4722 /* Transform the row. The order of transformations is significant,
4723 * and is very touchy. If you add a transformation, take care to
4724 * decide how it fits in with the other transformations here.
4726 void /* PRIVATE */
4727 png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
4729 png_debug(1, "in png_do_read_transformations");
4731 if (png_ptr->row_buf == NULL)
4733 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
4734 * error is incredibly rare and incredibly easy to debug without this
4735 * information.
4737 png_error(png_ptr, "NULL row buffer");
4740 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
4741 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
4742 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
4743 * all transformations, however in practice the ROW_INIT always gets done on
4744 * demand, if necessary.
4746 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
4747 (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
4749 /* Application has failed to call either png_read_start_image() or
4750 * png_read_update_info() after setting transforms that expand pixels.
4751 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
4753 png_error(png_ptr, "Uninitialized row");
4756 #ifdef PNG_READ_EXPAND_SUPPORTED
4757 if ((png_ptr->transformations & PNG_EXPAND) != 0)
4759 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4761 png_do_expand_palette(row_info, png_ptr->row_buf + 1,
4762 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
4765 else
4767 if (png_ptr->num_trans != 0 &&
4768 (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
4769 png_do_expand(row_info, png_ptr->row_buf + 1,
4770 &(png_ptr->trans_color));
4772 else
4773 png_do_expand(row_info, png_ptr->row_buf + 1,
4774 NULL);
4777 #endif
4779 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4780 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4781 (png_ptr->transformations & PNG_COMPOSE) == 0 &&
4782 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4783 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4784 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4785 0 /* at_start == false, because SWAP_ALPHA happens later */);
4786 #endif
4788 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4789 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
4791 int rgb_error =
4792 png_do_rgb_to_gray(png_ptr, row_info,
4793 png_ptr->row_buf + 1);
4795 if (rgb_error != 0)
4797 png_ptr->rgb_to_gray_status=1;
4798 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4799 PNG_RGB_TO_GRAY_WARN)
4800 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4802 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4803 PNG_RGB_TO_GRAY_ERR)
4804 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4807 #endif
4809 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
4811 * In most cases, the "simple transparency" should be done prior to doing
4812 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
4813 * pixel is transparent. You would also need to make sure that the
4814 * transparency information is upgraded to RGB.
4816 * To summarize, the current flow is:
4817 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
4818 * with background "in place" if transparent,
4819 * convert to RGB if necessary
4820 * - Gray + alpha -> composite with gray background and remove alpha bytes,
4821 * convert to RGB if necessary
4823 * To support RGB backgrounds for gray images we need:
4824 * - Gray + simple transparency -> convert to RGB + simple transparency,
4825 * compare 3 or 6 bytes and composite with
4826 * background "in place" if transparent
4827 * (3x compare/pixel compared to doing
4828 * composite with gray bkgrnd)
4829 * - Gray + alpha -> convert to RGB + alpha, composite with background and
4830 * remove alpha bytes (3x float
4831 * operations/pixel compared with composite
4832 * on gray background)
4834 * Greg's change will do this. The reason it wasn't done before is for
4835 * performance, as this increases the per-pixel operations. If we would check
4836 * in advance if the background was gray or RGB, and position the gray-to-RGB
4837 * transform appropriately, then it would save a lot of work/time.
4840 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4841 /* If gray -> RGB, do so now only if background is non-gray; else do later
4842 * for performance reasons
4844 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4845 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
4846 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4847 #endif
4849 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4850 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4851 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
4852 png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
4853 #endif
4855 #ifdef PNG_READ_GAMMA_SUPPORTED
4856 if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
4857 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4858 /* Because RGB_TO_GRAY does the gamma transform. */
4859 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
4860 #endif
4861 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4862 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4863 /* Because PNG_COMPOSE does the gamma transform if there is something to
4864 * do (if there is an alpha channel or transparency.)
4866 !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
4867 ((png_ptr->num_trans != 0) ||
4868 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
4869 #endif
4870 /* Because png_init_read_transformations transforms the palette, unless
4871 * RGB_TO_GRAY will do the transform.
4873 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
4874 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
4875 #endif
4877 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4878 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4879 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
4880 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4881 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4882 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4883 0 /* at_start == false, because SWAP_ALPHA happens later */);
4884 #endif
4886 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4887 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
4888 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4889 png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
4890 #endif
4892 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
4893 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
4894 png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
4895 #endif
4897 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
4898 /* There is no harm in doing both of these because only one has any effect,
4899 * by putting the 'scale' option first if the app asks for scale (either by
4900 * calling the API or in a TRANSFORM flag) this is what happens.
4902 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
4903 png_do_chop(row_info, png_ptr->row_buf + 1);
4904 #endif
4906 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4907 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
4909 png_do_quantize(row_info, png_ptr->row_buf + 1,
4910 png_ptr->palette_lookup, png_ptr->quantize_index);
4912 if (row_info->rowbytes == 0)
4913 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
4915 #endif /* READ_QUANTIZE */
4917 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4918 /* Do the expansion now, after all the arithmetic has been done. Notice
4919 * that previous transformations can handle the PNG_EXPAND_16 flag if this
4920 * is efficient (particularly true in the case of gamma correction, where
4921 * better accuracy results faster!)
4923 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4924 png_do_expand_16(row_info, png_ptr->row_buf + 1);
4925 #endif
4927 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4928 /* NOTE: moved here in 1.5.4 (from much later in this list.) */
4929 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4930 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
4931 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4932 #endif
4934 #ifdef PNG_READ_INVERT_SUPPORTED
4935 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
4936 png_do_invert(row_info, png_ptr->row_buf + 1);
4937 #endif
4939 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
4940 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
4941 png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
4942 #endif
4944 #ifdef PNG_READ_SHIFT_SUPPORTED
4945 if ((png_ptr->transformations & PNG_SHIFT) != 0)
4946 png_do_unshift(row_info, png_ptr->row_buf + 1,
4947 &(png_ptr->shift));
4948 #endif
4950 #ifdef PNG_READ_PACK_SUPPORTED
4951 if ((png_ptr->transformations & PNG_PACK) != 0)
4952 png_do_unpack(row_info, png_ptr->row_buf + 1);
4953 #endif
4955 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
4956 /* Added at libpng-1.5.10 */
4957 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4958 png_ptr->num_palette_max >= 0)
4959 png_do_check_palette_indexes(png_ptr, row_info);
4960 #endif
4962 #ifdef PNG_READ_BGR_SUPPORTED
4963 if ((png_ptr->transformations & PNG_BGR) != 0)
4964 png_do_bgr(row_info, png_ptr->row_buf + 1);
4965 #endif
4967 #ifdef PNG_READ_PACKSWAP_SUPPORTED
4968 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
4969 png_do_packswap(row_info, png_ptr->row_buf + 1);
4970 #endif
4972 #ifdef PNG_READ_FILLER_SUPPORTED
4973 if ((png_ptr->transformations & PNG_FILLER) != 0)
4974 png_do_read_filler(row_info, png_ptr->row_buf + 1,
4975 (png_uint_32)png_ptr->filler, png_ptr->flags);
4976 #endif
4978 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
4979 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
4980 png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
4981 #endif
4983 #ifdef PNG_READ_16BIT_SUPPORTED
4984 #ifdef PNG_READ_SWAP_SUPPORTED
4985 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
4986 png_do_swap(row_info, png_ptr->row_buf + 1);
4987 #endif
4988 #endif
4990 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
4991 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4993 if (png_ptr->read_user_transform_fn != NULL)
4994 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
4995 (png_ptr, /* png_ptr */
4996 row_info, /* row_info: */
4997 /* png_uint_32 width; width of row */
4998 /* png_size_t rowbytes; number of bytes in row */
4999 /* png_byte color_type; color type of pixels */
5000 /* png_byte bit_depth; bit depth of samples */
5001 /* png_byte channels; number of channels (1-4) */
5002 /* png_byte pixel_depth; bits per pixel (depth*channels) */
5003 png_ptr->row_buf + 1); /* start of pixel data for row */
5004 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
5005 if (png_ptr->user_transform_depth != 0)
5006 row_info->bit_depth = png_ptr->user_transform_depth;
5008 if (png_ptr->user_transform_channels != 0)
5009 row_info->channels = png_ptr->user_transform_channels;
5010 #endif
5011 row_info->pixel_depth = (png_byte)(row_info->bit_depth *
5012 row_info->channels);
5014 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
5016 #endif
5019 #endif /* READ_TRANSFORMS */
5020 #endif /* READ */