2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.2.49 [March 29, 2012]
5 * Copyright (c) 1998-2012 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
20 #define PNG_NO_PEDANTIC_WARNINGS
22 #ifdef PNG_READ_SUPPORTED
24 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
26 png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
28 png_debug(1, "in png_set_crc_action");
33 /* Tell libpng how we react to CRC errors in critical chunks */
36 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
39 case PNG_CRC_WARN_USE: /* Warn/use data */
40 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
41 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
44 case PNG_CRC_QUIET_USE: /* Quiet/use data */
45 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
46 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
47 PNG_FLAG_CRC_CRITICAL_IGNORE;
50 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
52 "Can't discard critical data on CRC error.");
53 case PNG_CRC_ERROR_QUIT: /* Error/quit */
57 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
61 /* Tell libpng how we react to CRC errors in ancillary chunks */
64 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
67 case PNG_CRC_WARN_USE: /* Warn/use data */
68 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
69 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
72 case PNG_CRC_QUIET_USE: /* Quiet/use data */
73 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
74 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
75 PNG_FLAG_CRC_ANCILLARY_NOWARN;
78 case PNG_CRC_ERROR_QUIT: /* Error/quit */
79 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
80 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
83 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
87 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
92 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
93 defined(PNG_FLOATING_POINT_SUPPORTED)
94 /* Handle alpha and tRNS via a background color */
96 png_set_background(png_structp png_ptr,
97 png_color_16p background_color, int background_gamma_code,
98 int need_expand, double background_gamma)
100 png_debug(1, "in png_set_background");
104 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
106 png_warning(png_ptr, "Application must supply a known background gamma");
110 png_ptr->transformations |= PNG_BACKGROUND;
111 png_memcpy(&(png_ptr->background), background_color,
112 png_sizeof(png_color_16));
113 png_ptr->background_gamma = (float)background_gamma;
114 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
115 png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
119 #ifdef PNG_READ_16_TO_8_SUPPORTED
120 /* Strip 16 bit depth files to 8 bit depth */
122 png_set_strip_16(png_structp png_ptr)
124 png_debug(1, "in png_set_strip_16");
128 png_ptr->transformations |= PNG_16_TO_8;
132 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
134 png_set_strip_alpha(png_structp png_ptr)
136 png_debug(1, "in png_set_strip_alpha");
140 png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
144 #ifdef PNG_READ_DITHER_SUPPORTED
145 /* Dither file to 8 bit. Supply a palette, the current number
146 * of elements in the palette, the maximum number of elements
147 * allowed, and a histogram if possible. If the current number
148 * of colors is greater then the maximum number, the palette will be
149 * modified to fit in the maximum number. "full_dither" indicates
150 * whether we need a dithering cube set up for RGB images, or if we
151 * simply are reducing the number of colors in a paletted image.
154 typedef struct png_dsort_struct
156 struct png_dsort_struct FAR * next;
160 typedef png_dsort FAR * png_dsortp;
161 typedef png_dsort FAR * FAR * png_dsortpp;
164 png_set_dither(png_structp png_ptr, png_colorp palette,
165 int num_palette, int maximum_colors, png_uint_16p histogram,
168 png_debug(1, "in png_set_dither");
172 png_ptr->transformations |= PNG_DITHER;
178 png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
179 (png_uint_32)(num_palette * png_sizeof(png_byte)));
180 for (i = 0; i < num_palette; i++)
181 png_ptr->dither_index[i] = (png_byte)i;
184 if (num_palette > maximum_colors)
186 if (histogram != NULL)
188 /* This is easy enough, just throw out the least used colors.
189 * Perhaps not the best solution, but good enough.
194 /* Initialize an array to sort colors */
195 png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
196 (png_uint_32)(num_palette * png_sizeof(png_byte)));
198 /* Initialize the dither_sort array */
199 for (i = 0; i < num_palette; i++)
200 png_ptr->dither_sort[i] = (png_byte)i;
202 /* Find the least used palette entries by starting a
203 * bubble sort, and running it until we have sorted
204 * out enough colors. Note that we don't care about
205 * sorting all the colors, just finding which are
209 for (i = num_palette - 1; i >= maximum_colors; i--)
211 int done; /* To stop early if the list is pre-sorted */
215 for (j = 0; j < i; j++)
217 if (histogram[png_ptr->dither_sort[j]]
218 < histogram[png_ptr->dither_sort[j + 1]])
222 t = png_ptr->dither_sort[j];
223 png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
224 png_ptr->dither_sort[j + 1] = t;
232 /* Swap the palette around, and set up a table, if necessary */
237 /* Put all the useful colors within the max, but don't
240 for (i = 0; i < maximum_colors; i++)
242 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
246 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
247 palette[i] = palette[j];
255 /* Move all the used colors inside the max limit, and
256 * develop a translation table.
258 for (i = 0; i < maximum_colors; i++)
260 /* Only move the colors we need to */
261 if ((int)png_ptr->dither_sort[i] >= maximum_colors)
267 while ((int)png_ptr->dither_sort[j] >= maximum_colors);
269 tmp_color = palette[j];
270 palette[j] = palette[i];
271 palette[i] = tmp_color;
272 /* Indicate where the color went */
273 png_ptr->dither_index[j] = (png_byte)i;
274 png_ptr->dither_index[i] = (png_byte)j;
278 /* Find closest color for those colors we are not using */
279 for (i = 0; i < num_palette; i++)
281 if ((int)png_ptr->dither_index[i] >= maximum_colors)
283 int min_d, k, min_k, d_index;
285 /* Find the closest color to one we threw out */
286 d_index = png_ptr->dither_index[i];
287 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
288 for (k = 1, min_k = 0; k < maximum_colors; k++)
292 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
300 /* Point to closest color */
301 png_ptr->dither_index[i] = (png_byte)min_k;
305 png_free(png_ptr, png_ptr->dither_sort);
306 png_ptr->dither_sort = NULL;
310 /* This is much harder to do simply (and quickly). Perhaps
311 * we need to go through a median cut routine, but those
312 * don't always behave themselves with only a few colors
313 * as input. So we will just find the closest two colors,
314 * and throw out one of them (chosen somewhat randomly).
315 * [We don't understand this at all, so if someone wants to
316 * work on improving it, be our guest - AED, GRP]
326 /* Initialize palette index arrays */
327 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
328 (png_uint_32)(num_palette * png_sizeof(png_byte)));
329 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
330 (png_uint_32)(num_palette * png_sizeof(png_byte)));
332 /* Initialize the sort array */
333 for (i = 0; i < num_palette; i++)
335 png_ptr->index_to_palette[i] = (png_byte)i;
336 png_ptr->palette_to_index[i] = (png_byte)i;
339 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
340 png_sizeof(png_dsortp)));
342 num_new_palette = num_palette;
344 /* Initial wild guess at how far apart the farthest pixel
345 * pair we will be eliminating will be. Larger
346 * numbers mean more areas will be allocated, Smaller
347 * numbers run the risk of not saving enough data, and
348 * having to do this all over again.
350 * I have not done extensive checking on this number.
354 while (num_new_palette > maximum_colors)
356 for (i = 0; i < num_new_palette - 1; i++)
360 for (j = i + 1; j < num_new_palette; j++)
364 d = PNG_COLOR_DIST(palette[i], palette[j]);
369 t = (png_dsortp)png_malloc_warn(png_ptr,
370 (png_uint_32)(png_sizeof(png_dsort)));
374 t->left = (png_byte)i;
375 t->right = (png_byte)j;
384 for (i = 0; i <= max_d; i++)
390 for (p = hash[i]; p; p = p->next)
392 if ((int)png_ptr->index_to_palette[p->left]
394 (int)png_ptr->index_to_palette[p->right]
399 if (num_new_palette & 0x01)
411 palette[png_ptr->index_to_palette[j]]
412 = palette[num_new_palette];
417 for (k = 0; k < num_palette; k++)
419 if (png_ptr->dither_index[k] ==
420 png_ptr->index_to_palette[j])
421 png_ptr->dither_index[k] =
422 png_ptr->index_to_palette[next_j];
423 if ((int)png_ptr->dither_index[k] ==
425 png_ptr->dither_index[k] =
426 png_ptr->index_to_palette[j];
430 png_ptr->index_to_palette[png_ptr->palette_to_index
431 [num_new_palette]] = png_ptr->index_to_palette[j];
432 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
433 = png_ptr->palette_to_index[num_new_palette];
435 png_ptr->index_to_palette[j] =
436 (png_byte)num_new_palette;
437 png_ptr->palette_to_index[num_new_palette] =
440 if (num_new_palette <= maximum_colors)
443 if (num_new_palette <= maximum_colors)
448 for (i = 0; i < 769; i++)
452 png_dsortp p = hash[i];
456 png_free(png_ptr, p);
464 png_free(png_ptr, hash);
465 png_free(png_ptr, png_ptr->palette_to_index);
466 png_free(png_ptr, png_ptr->index_to_palette);
467 png_ptr->palette_to_index = NULL;
468 png_ptr->index_to_palette = NULL;
470 num_palette = maximum_colors;
472 if (png_ptr->palette == NULL)
474 png_ptr->palette = palette;
476 png_ptr->num_palette = (png_uint_16)num_palette;
482 int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
483 PNG_DITHER_BLUE_BITS;
484 int num_red = (1 << PNG_DITHER_RED_BITS);
485 int num_green = (1 << PNG_DITHER_GREEN_BITS);
486 int num_blue = (1 << PNG_DITHER_BLUE_BITS);
487 png_size_t num_entries = ((png_size_t)1 << total_bits);
489 png_ptr->palette_lookup = (png_bytep )png_calloc(png_ptr,
490 (png_uint_32)(num_entries * png_sizeof(png_byte)));
492 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
493 png_sizeof(png_byte)));
494 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
496 for (i = 0; i < num_palette; i++)
499 int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
500 int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
501 int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
503 for (ir = 0; ir < num_red; ir++)
505 /* int dr = abs(ir - r); */
506 int dr = ((ir > r) ? ir - r : r - ir);
507 int index_r = (ir << (PNG_DITHER_BLUE_BITS +
508 PNG_DITHER_GREEN_BITS));
510 for (ig = 0; ig < num_green; ig++)
512 /* int dg = abs(ig - g); */
513 int dg = ((ig > g) ? ig - g : g - ig);
515 int dm = ((dr > dg) ? dr : dg);
516 int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
518 for (ib = 0; ib < num_blue; ib++)
520 int d_index = index_g | ib;
521 /* int db = abs(ib - b); */
522 int db = ((ib > b) ? ib - b : b - ib);
523 int dmax = ((dm > db) ? dm : db);
524 int d = dmax + dt + db;
526 if (d < (int)distance[d_index])
528 distance[d_index] = (png_byte)d;
529 png_ptr->palette_lookup[d_index] = (png_byte)i;
536 png_free(png_ptr, distance);
541 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
542 /* Transform the image from the file_gamma to the screen_gamma. We
543 * only do transformations on images where the file_gamma and screen_gamma
544 * are not close reciprocals, otherwise it slows things down slightly, and
545 * also needlessly introduces small errors.
547 * We will turn off gamma transformation later if no semitransparent entries
548 * are present in the tRNS array for palette images. We can't do it here
549 * because we don't necessarily have the tRNS chunk yet.
552 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
554 png_debug(1, "in png_set_gamma");
559 if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
560 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
561 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
562 png_ptr->transformations |= PNG_GAMMA;
563 png_ptr->gamma = (float)file_gamma;
564 png_ptr->screen_gamma = (float)scrn_gamma;
568 #ifdef PNG_READ_EXPAND_SUPPORTED
569 /* Expand paletted images to RGB, expand grayscale images of
570 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
574 png_set_expand(png_structp png_ptr)
576 png_debug(1, "in png_set_expand");
581 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
582 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
585 /* GRR 19990627: the following three functions currently are identical
586 * to png_set_expand(). However, it is entirely reasonable that someone
587 * might wish to expand an indexed image to RGB but *not* expand a single,
588 * fully transparent palette entry to a full alpha channel--perhaps instead
589 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
590 * the transparent color with a particular RGB value, or drop tRNS entirely.
591 * IOW, a future version of the library may make the transformations flag
592 * a bit more fine-grained, with separate bits for each of these three
595 * More to the point, these functions make it obvious what libpng will be
596 * doing, whereas "expand" can (and does) mean any number of things.
598 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
599 * to expand only the sample depth but not to expand the tRNS to alpha
600 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
603 /* Expand paletted images to RGB. */
605 png_set_palette_to_rgb(png_structp png_ptr)
607 png_debug(1, "in png_set_palette_to_rgb");
612 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
613 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
617 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
619 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
621 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
626 png_ptr->transformations |= PNG_EXPAND;
627 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
631 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
632 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
633 /* Deprecated as of libpng-1.2.9 */
635 png_set_gray_1_2_4_to_8(png_structp png_ptr)
637 png_debug(1, "in png_set_gray_1_2_4_to_8");
642 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
647 /* Expand tRNS chunks to alpha channels. */
649 png_set_tRNS_to_alpha(png_structp png_ptr)
651 png_debug(1, "in png_set_tRNS_to_alpha");
653 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
654 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
656 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
658 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
660 png_set_gray_to_rgb(png_structp png_ptr)
662 png_debug(1, "in png_set_gray_to_rgb");
664 png_ptr->transformations |= PNG_GRAY_TO_RGB;
665 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
669 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
670 #ifdef PNG_FLOATING_POINT_SUPPORTED
671 /* Convert a RGB image to a grayscale of the same width. This allows us,
672 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
676 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
679 int red_fixed, green_fixed;
682 if (red > 21474.83647 || red < -21474.83648 ||
683 green > 21474.83647 || green < -21474.83648)
685 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
691 red_fixed = (int)((float)red*100000.0 + 0.5);
692 green_fixed = (int)((float)green*100000.0 + 0.5);
694 png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
699 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
700 png_fixed_point red, png_fixed_point green)
702 png_debug(1, "in png_set_rgb_to_gray");
709 case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
712 case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
715 case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
717 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
718 #ifdef PNG_READ_EXPAND_SUPPORTED
719 png_ptr->transformations |= PNG_EXPAND;
723 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
724 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
728 png_uint_16 red_int, green_int;
729 if (red < 0 || green < 0)
731 red_int = 6968; /* .212671 * 32768 + .5 */
732 green_int = 23434; /* .715160 * 32768 + .5 */
734 else if (red + green < 100000L)
736 red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
737 green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
741 png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
745 png_ptr->rgb_to_gray_red_coeff = red_int;
746 png_ptr->rgb_to_gray_green_coeff = green_int;
747 png_ptr->rgb_to_gray_blue_coeff =
748 (png_uint_16)(32768 - red_int - green_int);
753 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
754 defined(PNG_LEGACY_SUPPORTED) || \
755 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
757 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
758 read_user_transform_fn)
760 png_debug(1, "in png_set_read_user_transform_fn");
765 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
766 png_ptr->transformations |= PNG_USER_TRANSFORM;
767 png_ptr->read_user_transform_fn = read_user_transform_fn;
769 #ifdef PNG_LEGACY_SUPPORTED
770 if (read_user_transform_fn)
772 "This version of libpng does not support user transforms");
777 /* Initialize everything needed for the read. This includes modifying
781 png_init_read_transformations(png_structp png_ptr)
783 png_debug(1, "in png_init_read_transformations");
785 #ifdef PNG_USELESS_TESTS_SUPPORTED
789 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
790 defined(PNG_READ_SHIFT_SUPPORTED) || \
791 defined(PNG_READ_GAMMA_SUPPORTED)
792 int color_type = png_ptr->color_type;
795 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
797 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
798 /* Detect gray background and attempt to enable optimization
799 * for gray --> RGB case
801 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
802 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
803 * background color might actually be gray yet not be flagged as such.
804 * This is not a problem for the current code, which uses
805 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
806 * png_do_gray_to_rgb() transformation.
808 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
809 !(color_type & PNG_COLOR_MASK_COLOR))
811 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
812 } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
813 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
814 (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
815 png_ptr->background.red == png_ptr->background.green &&
816 png_ptr->background.red == png_ptr->background.blue)
818 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
819 png_ptr->background.gray = png_ptr->background.red;
823 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
824 (png_ptr->transformations & PNG_EXPAND))
826 if (!(color_type & PNG_COLOR_MASK_COLOR)) /* i.e., GRAY or GRAY_ALPHA */
828 /* Expand background and tRNS chunks */
829 switch (png_ptr->bit_depth)
832 png_ptr->background.gray *= (png_uint_16)0xff;
833 png_ptr->background.red = png_ptr->background.green
834 = png_ptr->background.blue = png_ptr->background.gray;
835 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
837 png_ptr->trans_values.gray *= (png_uint_16)0xff;
838 png_ptr->trans_values.red = png_ptr->trans_values.green
839 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
844 png_ptr->background.gray *= (png_uint_16)0x55;
845 png_ptr->background.red = png_ptr->background.green
846 = png_ptr->background.blue = png_ptr->background.gray;
847 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
849 png_ptr->trans_values.gray *= (png_uint_16)0x55;
850 png_ptr->trans_values.red = png_ptr->trans_values.green
851 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
856 png_ptr->background.gray *= (png_uint_16)0x11;
857 png_ptr->background.red = png_ptr->background.green
858 = png_ptr->background.blue = png_ptr->background.gray;
859 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
861 png_ptr->trans_values.gray *= (png_uint_16)0x11;
862 png_ptr->trans_values.red = png_ptr->trans_values.green
863 = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
870 png_ptr->background.red = png_ptr->background.green
871 = png_ptr->background.blue = png_ptr->background.gray;
875 else if (color_type == PNG_COLOR_TYPE_PALETTE)
877 png_ptr->background.red =
878 png_ptr->palette[png_ptr->background.index].red;
879 png_ptr->background.green =
880 png_ptr->palette[png_ptr->background.index].green;
881 png_ptr->background.blue =
882 png_ptr->palette[png_ptr->background.index].blue;
884 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
885 if (png_ptr->transformations & PNG_INVERT_ALPHA)
887 #ifdef PNG_READ_EXPAND_SUPPORTED
888 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
891 /* Invert the alpha channel (in tRNS) unless the pixels are
892 * going to be expanded, in which case leave it for later
895 istop=(int)png_ptr->num_trans;
896 for (i=0; i<istop; i++)
897 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
906 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
907 png_ptr->background_1 = png_ptr->background;
909 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
911 if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
912 && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
913 < PNG_GAMMA_THRESHOLD))
917 for (i=0; i<png_ptr->num_trans; i++)
919 if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
920 k=1; /* Partial transparency is present */
923 png_ptr->transformations &= ~PNG_GAMMA;
926 if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
927 png_ptr->gamma != 0.0)
929 png_build_gamma_table(png_ptr);
931 #ifdef PNG_READ_BACKGROUND_SUPPORTED
932 if (png_ptr->transformations & PNG_BACKGROUND)
934 if (color_type == PNG_COLOR_TYPE_PALETTE)
936 /* Could skip if no transparency */
937 png_color back, back_1;
938 png_colorp palette = png_ptr->palette;
939 int num_palette = png_ptr->num_palette;
941 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
943 back.red = png_ptr->gamma_table[png_ptr->background.red];
944 back.green = png_ptr->gamma_table[png_ptr->background.green];
945 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
947 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
948 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
949 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
955 switch (png_ptr->background_gamma_type)
957 case PNG_BACKGROUND_GAMMA_SCREEN:
958 g = (png_ptr->screen_gamma);
962 case PNG_BACKGROUND_GAMMA_FILE:
963 g = 1.0 / (png_ptr->gamma);
964 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
967 case PNG_BACKGROUND_GAMMA_UNIQUE:
968 g = 1.0 / (png_ptr->background_gamma);
969 gs = 1.0 / (png_ptr->background_gamma *
970 png_ptr->screen_gamma);
973 g = 1.0; /* back_1 */
977 if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
979 back.red = (png_byte)png_ptr->background.red;
980 back.green = (png_byte)png_ptr->background.green;
981 back.blue = (png_byte)png_ptr->background.blue;
985 back.red = (png_byte)(pow(
986 (double)png_ptr->background.red/255, gs) * 255.0 + .5);
987 back.green = (png_byte)(pow(
988 (double)png_ptr->background.green/255, gs) * 255.0
990 back.blue = (png_byte)(pow(
991 (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
994 back_1.red = (png_byte)(pow(
995 (double)png_ptr->background.red/255, g) * 255.0 + .5);
996 back_1.green = (png_byte)(pow(
997 (double)png_ptr->background.green/255, g) * 255.0 + .5);
998 back_1.blue = (png_byte)(pow(
999 (double)png_ptr->background.blue/255, g) * 255.0 + .5);
1001 for (i = 0; i < num_palette; i++)
1003 if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
1005 if (png_ptr->trans[i] == 0)
1009 else /* if (png_ptr->trans[i] != 0xff) */
1013 v = png_ptr->gamma_to_1[palette[i].red];
1014 png_composite(w, v, png_ptr->trans[i], back_1.red);
1015 palette[i].red = png_ptr->gamma_from_1[w];
1017 v = png_ptr->gamma_to_1[palette[i].green];
1018 png_composite(w, v, png_ptr->trans[i], back_1.green);
1019 palette[i].green = png_ptr->gamma_from_1[w];
1021 v = png_ptr->gamma_to_1[palette[i].blue];
1022 png_composite(w, v, png_ptr->trans[i], back_1.blue);
1023 palette[i].blue = png_ptr->gamma_from_1[w];
1028 palette[i].red = png_ptr->gamma_table[palette[i].red];
1029 palette[i].green = png_ptr->gamma_table[palette[i].green];
1030 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1033 /* Prevent the transformations being done again, and make sure
1034 * that the now spurious alpha channel is stripped - the code
1035 * has just reduced background composition and gamma correction
1036 * to a simple alpha channel strip.
1038 png_ptr->transformations &= ~PNG_BACKGROUND;
1039 png_ptr->transformations &= ~PNG_GAMMA;
1040 png_ptr->transformations |= PNG_STRIP_ALPHA;
1042 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1044 /* color_type != PNG_COLOR_TYPE_PALETTE */
1046 double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
1050 switch (png_ptr->background_gamma_type)
1052 case PNG_BACKGROUND_GAMMA_SCREEN:
1053 g = (png_ptr->screen_gamma);
1057 case PNG_BACKGROUND_GAMMA_FILE:
1058 g = 1.0 / (png_ptr->gamma);
1059 gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
1062 case PNG_BACKGROUND_GAMMA_UNIQUE:
1063 g = 1.0 / (png_ptr->background_gamma);
1064 gs = 1.0 / (png_ptr->background_gamma *
1065 png_ptr->screen_gamma);
1069 png_ptr->background_1.gray = (png_uint_16)(pow(
1070 (double)png_ptr->background.gray / m, g) * m + .5);
1071 png_ptr->background.gray = (png_uint_16)(pow(
1072 (double)png_ptr->background.gray / m, gs) * m + .5);
1074 if ((png_ptr->background.red != png_ptr->background.green) ||
1075 (png_ptr->background.red != png_ptr->background.blue) ||
1076 (png_ptr->background.red != png_ptr->background.gray))
1078 /* RGB or RGBA with color background */
1079 png_ptr->background_1.red = (png_uint_16)(pow(
1080 (double)png_ptr->background.red / m, g) * m + .5);
1081 png_ptr->background_1.green = (png_uint_16)(pow(
1082 (double)png_ptr->background.green / m, g) * m + .5);
1083 png_ptr->background_1.blue = (png_uint_16)(pow(
1084 (double)png_ptr->background.blue / m, g) * m + .5);
1085 png_ptr->background.red = (png_uint_16)(pow(
1086 (double)png_ptr->background.red / m, gs) * m + .5);
1087 png_ptr->background.green = (png_uint_16)(pow(
1088 (double)png_ptr->background.green / m, gs) * m + .5);
1089 png_ptr->background.blue = (png_uint_16)(pow(
1090 (double)png_ptr->background.blue / m, gs) * m + .5);
1094 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1095 png_ptr->background_1.red = png_ptr->background_1.green
1096 = png_ptr->background_1.blue = png_ptr->background_1.gray;
1097 png_ptr->background.red = png_ptr->background.green
1098 = png_ptr->background.blue = png_ptr->background.gray;
1103 /* Transformation does not include PNG_BACKGROUND */
1104 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1105 if (color_type == PNG_COLOR_TYPE_PALETTE)
1107 png_colorp palette = png_ptr->palette;
1108 int num_palette = png_ptr->num_palette;
1111 for (i = 0; i < num_palette; i++)
1113 palette[i].red = png_ptr->gamma_table[palette[i].red];
1114 palette[i].green = png_ptr->gamma_table[palette[i].green];
1115 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1118 /* Done the gamma correction. */
1119 png_ptr->transformations &= ~PNG_GAMMA;
1122 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1125 #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1126 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1127 /* No GAMMA transformation */
1128 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1129 (color_type == PNG_COLOR_TYPE_PALETTE))
1132 int istop = (int)png_ptr->num_trans;
1134 png_colorp palette = png_ptr->palette;
1136 back.red = (png_byte)png_ptr->background.red;
1137 back.green = (png_byte)png_ptr->background.green;
1138 back.blue = (png_byte)png_ptr->background.blue;
1140 for (i = 0; i < istop; i++)
1142 if (png_ptr->trans[i] == 0)
1146 else if (png_ptr->trans[i] != 0xff)
1148 /* The png_composite() macro is defined in png.h */
1149 png_composite(palette[i].red, palette[i].red,
1150 png_ptr->trans[i], back.red);
1151 png_composite(palette[i].green, palette[i].green,
1152 png_ptr->trans[i], back.green);
1153 png_composite(palette[i].blue, palette[i].blue,
1154 png_ptr->trans[i], back.blue);
1158 /* Handled alpha, still need to strip the channel. */
1159 png_ptr->transformations &= ~PNG_BACKGROUND;
1160 png_ptr->transformations |= PNG_STRIP_ALPHA;
1162 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1164 #ifdef PNG_READ_SHIFT_SUPPORTED
1165 if ((png_ptr->transformations & PNG_SHIFT) &&
1166 !(png_ptr->transformations & PNG_EXPAND) &&
1167 (color_type == PNG_COLOR_TYPE_PALETTE))
1170 png_uint_16 istop = png_ptr->num_palette;
1171 int sr = 8 - png_ptr->sig_bit.red;
1172 int sg = 8 - png_ptr->sig_bit.green;
1173 int sb = 8 - png_ptr->sig_bit.blue;
1175 if (sr < 0 || sr > 8)
1177 if (sg < 0 || sg > 8)
1179 if (sb < 0 || sb > 8)
1181 for (i = 0; i < istop; i++)
1183 png_ptr->palette[i].red >>= sr;
1184 png_ptr->palette[i].green >>= sg;
1185 png_ptr->palette[i].blue >>= sb;
1188 png_ptr->transformations &= ~PNG_SHIFT;
1190 #endif /* PNG_READ_SHIFT_SUPPORTED */
1192 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1193 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1199 /* Modify the info structure to reflect the transformations. The
1200 * info should be updated so a PNG file could be written with it,
1201 * assuming the transformations result in valid PNG data.
1204 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1206 png_debug(1, "in png_read_transform_info");
1208 #ifdef PNG_READ_EXPAND_SUPPORTED
1209 if (png_ptr->transformations & PNG_EXPAND)
1211 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1213 if (png_ptr->num_trans)
1214 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1216 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1217 info_ptr->bit_depth = 8;
1218 info_ptr->num_trans = 0;
1222 if (png_ptr->num_trans)
1224 if (png_ptr->transformations & PNG_EXPAND_tRNS)
1225 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1227 if (info_ptr->bit_depth < 8)
1228 info_ptr->bit_depth = 8;
1229 info_ptr->num_trans = 0;
1234 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1235 if (png_ptr->transformations & PNG_BACKGROUND)
1237 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1238 info_ptr->num_trans = 0;
1239 info_ptr->background = png_ptr->background;
1243 #ifdef PNG_READ_GAMMA_SUPPORTED
1244 if (png_ptr->transformations & PNG_GAMMA)
1246 #ifdef PNG_FLOATING_POINT_SUPPORTED
1247 info_ptr->gamma = png_ptr->gamma;
1249 #ifdef PNG_FIXED_POINT_SUPPORTED
1250 info_ptr->int_gamma = png_ptr->int_gamma;
1255 #ifdef PNG_READ_16_TO_8_SUPPORTED
1256 if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1257 info_ptr->bit_depth = 8;
1260 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1261 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1262 info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1265 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1266 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1267 info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1270 #ifdef PNG_READ_DITHER_SUPPORTED
1271 if (png_ptr->transformations & PNG_DITHER)
1273 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1274 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1275 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1277 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1282 #ifdef PNG_READ_PACK_SUPPORTED
1283 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1284 info_ptr->bit_depth = 8;
1287 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1288 info_ptr->channels = 1;
1289 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1290 info_ptr->channels = 3;
1292 info_ptr->channels = 1;
1294 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1295 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1296 info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1299 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1300 info_ptr->channels++;
1302 #ifdef PNG_READ_FILLER_SUPPORTED
1303 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1304 if ((png_ptr->transformations & PNG_FILLER) &&
1305 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1306 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1308 info_ptr->channels++;
1309 /* If adding a true alpha channel not just filler */
1311 if (png_ptr->transformations & PNG_ADD_ALPHA)
1312 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1317 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1318 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1319 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1321 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1322 info_ptr->bit_depth = png_ptr->user_transform_depth;
1323 if (info_ptr->channels < png_ptr->user_transform_channels)
1324 info_ptr->channels = png_ptr->user_transform_channels;
1328 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1329 info_ptr->bit_depth);
1331 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1333 #ifndef PNG_READ_EXPAND_SUPPORTED
1339 /* Transform the row. The order of transformations is significant,
1340 * and is very touchy. If you add a transformation, take care to
1341 * decide how it fits in with the other transformations here.
1344 png_do_read_transformations(png_structp png_ptr)
1346 png_debug(1, "in png_do_read_transformations");
1348 if (png_ptr->row_buf == NULL)
1350 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1353 png_snprintf2(msg, 50,
1354 "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1356 png_error(png_ptr, msg);
1358 png_error(png_ptr, "NULL row buffer");
1361 #ifdef PNG_WARN_UNINITIALIZED_ROW
1362 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1363 /* Application has failed to call either png_read_start_image()
1364 * or png_read_update_info() after setting transforms that expand
1365 * pixels. This check added to libpng-1.2.19
1367 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1368 png_error(png_ptr, "Uninitialized row");
1370 png_warning(png_ptr, "Uninitialized row");
1374 #ifdef PNG_READ_EXPAND_SUPPORTED
1375 if (png_ptr->transformations & PNG_EXPAND)
1377 if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1379 png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1380 png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
1384 if (png_ptr->num_trans &&
1385 (png_ptr->transformations & PNG_EXPAND_tRNS))
1386 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1387 &(png_ptr->trans_values));
1389 png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1395 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1396 if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
1397 png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1398 PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
1401 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1402 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1405 png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
1406 png_ptr->row_buf + 1);
1409 png_ptr->rgb_to_gray_status=1;
1410 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1411 PNG_RGB_TO_GRAY_WARN)
1412 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1413 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1414 PNG_RGB_TO_GRAY_ERR)
1415 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1420 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1422 * In most cases, the "simple transparency" should be done prior to doing
1423 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1424 * pixel is transparent. You would also need to make sure that the
1425 * transparency information is upgraded to RGB.
1427 * To summarize, the current flow is:
1428 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1429 * with background "in place" if transparent,
1430 * convert to RGB if necessary
1431 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1432 * convert to RGB if necessary
1434 * To support RGB backgrounds for gray images we need:
1435 * - Gray + simple transparency -> convert to RGB + simple transparency,
1436 * compare 3 or 6 bytes and composite with
1437 * background "in place" if transparent
1438 * (3x compare/pixel compared to doing
1439 * composite with gray bkgrnd)
1440 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1441 * remove alpha bytes (3x float
1442 * operations/pixel compared with composite
1443 * on gray background)
1445 * Greg's change will do this. The reason it wasn't done before is for
1446 * performance, as this increases the per-pixel operations. If we would check
1447 * in advance if the background was gray or RGB, and position the gray-to-RGB
1448 * transform appropriately, then it would save a lot of work/time.
1451 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1452 /* If gray -> RGB, do so now only if background is non-gray; else do later
1453 * for performance reasons
1455 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1456 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1457 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1460 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1461 if ((png_ptr->transformations & PNG_BACKGROUND) &&
1462 ((png_ptr->num_trans != 0 ) ||
1463 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1464 png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1465 &(png_ptr->trans_values), &(png_ptr->background)
1466 #ifdef PNG_READ_GAMMA_SUPPORTED
1467 , &(png_ptr->background_1),
1468 png_ptr->gamma_table, png_ptr->gamma_from_1,
1469 png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1470 png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1471 png_ptr->gamma_shift
1476 #ifdef PNG_READ_GAMMA_SUPPORTED
1477 if ((png_ptr->transformations & PNG_GAMMA) &&
1478 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1479 !((png_ptr->transformations & PNG_BACKGROUND) &&
1480 ((png_ptr->num_trans != 0) ||
1481 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1483 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1484 png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1485 png_ptr->gamma_table, png_ptr->gamma_16_table,
1486 png_ptr->gamma_shift);
1489 #ifdef PNG_READ_16_TO_8_SUPPORTED
1490 if (png_ptr->transformations & PNG_16_TO_8)
1491 png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1494 #ifdef PNG_READ_DITHER_SUPPORTED
1495 if (png_ptr->transformations & PNG_DITHER)
1497 png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
1498 png_ptr->palette_lookup, png_ptr->dither_index);
1499 if (png_ptr->row_info.rowbytes == (png_uint_32)0)
1500 png_error(png_ptr, "png_do_dither returned rowbytes=0");
1504 #ifdef PNG_READ_INVERT_SUPPORTED
1505 if (png_ptr->transformations & PNG_INVERT_MONO)
1506 png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1509 #ifdef PNG_READ_SHIFT_SUPPORTED
1510 if (png_ptr->transformations & PNG_SHIFT)
1511 png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1515 #ifdef PNG_READ_PACK_SUPPORTED
1516 if (png_ptr->transformations & PNG_PACK)
1517 png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1520 #ifdef PNG_READ_BGR_SUPPORTED
1521 if (png_ptr->transformations & PNG_BGR)
1522 png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1525 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1526 if (png_ptr->transformations & PNG_PACKSWAP)
1527 png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1530 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1531 /* If gray -> RGB, do so now only if we did not do so above */
1532 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1533 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1534 png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1537 #ifdef PNG_READ_FILLER_SUPPORTED
1538 if (png_ptr->transformations & PNG_FILLER)
1539 png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1540 (png_uint_32)png_ptr->filler, png_ptr->flags);
1543 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1544 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1545 png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1548 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1549 if (png_ptr->transformations & PNG_SWAP_ALPHA)
1550 png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1553 #ifdef PNG_READ_SWAP_SUPPORTED
1554 if (png_ptr->transformations & PNG_SWAP_BYTES)
1555 png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1558 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1559 if (png_ptr->transformations & PNG_USER_TRANSFORM)
1561 if (png_ptr->read_user_transform_fn != NULL)
1562 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1563 (png_ptr, /* png_ptr */
1564 &(png_ptr->row_info), /* row_info: */
1565 /* png_uint_32 width; width of row */
1566 /* png_uint_32 rowbytes; number of bytes in row */
1567 /* png_byte color_type; color type of pixels */
1568 /* png_byte bit_depth; bit depth of samples */
1569 /* png_byte channels; number of channels (1-4) */
1570 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1571 png_ptr->row_buf + 1); /* start of pixel data for row */
1572 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1573 if (png_ptr->user_transform_depth)
1574 png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1575 if (png_ptr->user_transform_channels)
1576 png_ptr->row_info.channels = png_ptr->user_transform_channels;
1578 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1579 png_ptr->row_info.channels);
1580 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1581 png_ptr->row_info.width);
1587 #ifdef PNG_READ_PACK_SUPPORTED
1588 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1589 * without changing the actual values. Thus, if you had a row with
1590 * a bit depth of 1, you would end up with bytes that only contained
1591 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1592 * png_do_shift() after this.
1595 png_do_unpack(png_row_infop row_info, png_bytep row)
1597 png_debug(1, "in png_do_unpack");
1599 #ifdef PNG_USELESS_TESTS_SUPPORTED
1600 if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
1602 if (row_info->bit_depth < 8)
1606 png_uint_32 row_width=row_info->width;
1608 switch (row_info->bit_depth)
1612 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1613 png_bytep dp = row + (png_size_t)row_width - 1;
1614 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1615 for (i = 0; i < row_width; i++)
1617 *dp = (png_byte)((*sp >> shift) & 0x01);
1634 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1635 png_bytep dp = row + (png_size_t)row_width - 1;
1636 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1637 for (i = 0; i < row_width; i++)
1639 *dp = (png_byte)((*sp >> shift) & 0x03);
1655 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1656 png_bytep dp = row + (png_size_t)row_width - 1;
1657 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1658 for (i = 0; i < row_width; i++)
1660 *dp = (png_byte)((*sp >> shift) & 0x0f);
1674 row_info->bit_depth = 8;
1675 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1676 row_info->rowbytes = row_width * row_info->channels;
1681 #ifdef PNG_READ_SHIFT_SUPPORTED
1682 /* Reverse the effects of png_do_shift. This routine merely shifts the
1683 * pixels back to their significant bits values. Thus, if you have
1684 * a row of bit depth 8, but only 5 are significant, this will shift
1685 * the values back to 0 through 31.
1688 png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
1690 png_debug(1, "in png_do_unshift");
1693 #ifdef PNG_USELESS_TESTS_SUPPORTED
1694 row != NULL && row_info != NULL && sig_bits != NULL &&
1696 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1701 png_uint_16 value = 0;
1702 png_uint_32 row_width = row_info->width;
1704 if (row_info->color_type & PNG_COLOR_MASK_COLOR)
1706 shift[channels++] = row_info->bit_depth - sig_bits->red;
1707 shift[channels++] = row_info->bit_depth - sig_bits->green;
1708 shift[channels++] = row_info->bit_depth - sig_bits->blue;
1712 shift[channels++] = row_info->bit_depth - sig_bits->gray;
1714 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
1716 shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1719 for (c = 0; c < channels; c++)
1730 switch (row_info->bit_depth)
1736 png_uint_32 istop = row_info->rowbytes;
1738 for (bp = row, i = 0; i < istop; i++)
1750 png_uint_32 istop = row_info->rowbytes;
1751 png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1752 (png_byte)((int)0xf >> shift[0]));
1754 for (i = 0; i < istop; i++)
1766 png_uint_32 istop = row_width * channels;
1768 for (i = 0; i < istop; i++)
1770 *bp++ >>= shift[i%channels];
1779 png_uint_32 istop = channels * row_width;
1781 for (i = 0; i < istop; i++)
1783 value = (png_uint_16)((*bp << 8) + *(bp + 1));
1784 value >>= shift[i%channels];
1785 *bp++ = (png_byte)(value >> 8);
1786 *bp++ = (png_byte)(value & 0xff);
1795 #ifdef PNG_READ_16_TO_8_SUPPORTED
1796 /* Chop rows of bit depth 16 down to 8 */
1798 png_do_chop(png_row_infop row_info, png_bytep row)
1800 png_debug(1, "in png_do_chop");
1802 #ifdef PNG_USELESS_TESTS_SUPPORTED
1803 if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
1805 if (row_info->bit_depth == 16)
1811 png_uint_32 istop = row_info->width * row_info->channels;
1813 for (i = 0; i<istop; i++, sp += 2, dp++)
1815 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1816 /* This does a more accurate scaling of the 16-bit color
1817 * value, rather than a simple low-byte truncation.
1819 * What the ideal calculation should be:
1820 * *dp = (((((png_uint_32)(*sp) << 8) |
1821 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1822 * / (png_uint_32)65535L;
1824 * GRR: no, I think this is what it really should be:
1825 * *dp = (((((png_uint_32)(*sp) << 8) |
1826 * (png_uint_32)(*(sp + 1))) + 128L)
1827 * / (png_uint_32)257L;
1829 * GRR: here's the exact calculation with shifts:
1830 * temp = (((png_uint_32)(*sp) << 8) |
1831 * (png_uint_32)(*(sp + 1))) + 128L;
1832 * *dp = (temp - (temp >> 8)) >> 8;
1834 * Approximate calculation with shift/add instead of multiply/divide:
1835 * *dp = ((((png_uint_32)(*sp) << 8) |
1836 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1838 * What we actually do to avoid extra shifting and conversion:
1841 *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
1843 /* Simply discard the low order byte */
1847 row_info->bit_depth = 8;
1848 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1849 row_info->rowbytes = row_info->width * row_info->channels;
1854 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1856 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1858 png_debug(1, "in png_do_read_swap_alpha");
1860 #ifdef PNG_USELESS_TESTS_SUPPORTED
1861 if (row != NULL && row_info != NULL)
1864 png_uint_32 row_width = row_info->width;
1865 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1867 /* This converts from RGBA to ARGB */
1868 if (row_info->bit_depth == 8)
1870 png_bytep sp = row + row_info->rowbytes;
1875 for (i = 0; i < row_width; i++)
1884 /* This converts from RRGGBBAA to AARRGGBB */
1887 png_bytep sp = row + row_info->rowbytes;
1892 for (i = 0; i < row_width; i++)
1907 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1909 /* This converts from GA to AG */
1910 if (row_info->bit_depth == 8)
1912 png_bytep sp = row + row_info->rowbytes;
1917 for (i = 0; i < row_width; i++)
1924 /* This converts from GGAA to AAGG */
1927 png_bytep sp = row + row_info->rowbytes;
1932 for (i = 0; i < row_width; i++)
1947 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1949 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
1951 png_debug(1, "in png_do_read_invert_alpha");
1953 #ifdef PNG_USELESS_TESTS_SUPPORTED
1954 if (row != NULL && row_info != NULL)
1957 png_uint_32 row_width = row_info->width;
1958 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1960 /* This inverts the alpha channel in RGBA */
1961 if (row_info->bit_depth == 8)
1963 png_bytep sp = row + row_info->rowbytes;
1967 for (i = 0; i < row_width; i++)
1969 *(--dp) = (png_byte)(255 - *(--sp));
1971 /* This does nothing:
1975 We can replace it with:
1981 /* This inverts the alpha channel in RRGGBBAA */
1984 png_bytep sp = row + row_info->rowbytes;
1988 for (i = 0; i < row_width; i++)
1990 *(--dp) = (png_byte)(255 - *(--sp));
1991 *(--dp) = (png_byte)(255 - *(--sp));
1993 /* This does nothing:
2000 We can replace it with:
2007 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2009 /* This inverts the alpha channel in GA */
2010 if (row_info->bit_depth == 8)
2012 png_bytep sp = row + row_info->rowbytes;
2016 for (i = 0; i < row_width; i++)
2018 *(--dp) = (png_byte)(255 - *(--sp));
2022 /* This inverts the alpha channel in GGAA */
2025 png_bytep sp = row + row_info->rowbytes;
2029 for (i = 0; i < row_width; i++)
2031 *(--dp) = (png_byte)(255 - *(--sp));
2032 *(--dp) = (png_byte)(255 - *(--sp));
2046 #ifdef PNG_READ_FILLER_SUPPORTED
2047 /* Add filler channel if we have RGB color */
2049 png_do_read_filler(png_row_infop row_info, png_bytep row,
2050 png_uint_32 filler, png_uint_32 flags)
2053 png_uint_32 row_width = row_info->width;
2055 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2056 png_byte lo_filler = (png_byte)(filler & 0xff);
2058 png_debug(1, "in png_do_read_filler");
2061 #ifdef PNG_USELESS_TESTS_SUPPORTED
2062 row != NULL && row_info != NULL &&
2064 row_info->color_type == PNG_COLOR_TYPE_GRAY)
2066 if (row_info->bit_depth == 8)
2068 /* This changes the data from G to GX */
2069 if (flags & PNG_FLAG_FILLER_AFTER)
2071 png_bytep sp = row + (png_size_t)row_width;
2072 png_bytep dp = sp + (png_size_t)row_width;
2073 for (i = 1; i < row_width; i++)
2075 *(--dp) = lo_filler;
2078 *(--dp) = lo_filler;
2079 row_info->channels = 2;
2080 row_info->pixel_depth = 16;
2081 row_info->rowbytes = row_width * 2;
2083 /* This changes the data from G to XG */
2086 png_bytep sp = row + (png_size_t)row_width;
2087 png_bytep dp = sp + (png_size_t)row_width;
2088 for (i = 0; i < row_width; i++)
2091 *(--dp) = lo_filler;
2093 row_info->channels = 2;
2094 row_info->pixel_depth = 16;
2095 row_info->rowbytes = row_width * 2;
2098 else if (row_info->bit_depth == 16)
2100 /* This changes the data from GG to GGXX */
2101 if (flags & PNG_FLAG_FILLER_AFTER)
2103 png_bytep sp = row + (png_size_t)row_width * 2;
2104 png_bytep dp = sp + (png_size_t)row_width * 2;
2105 for (i = 1; i < row_width; i++)
2107 *(--dp) = hi_filler;
2108 *(--dp) = lo_filler;
2112 *(--dp) = hi_filler;
2113 *(--dp) = lo_filler;
2114 row_info->channels = 2;
2115 row_info->pixel_depth = 32;
2116 row_info->rowbytes = row_width * 4;
2118 /* This changes the data from GG to XXGG */
2121 png_bytep sp = row + (png_size_t)row_width * 2;
2122 png_bytep dp = sp + (png_size_t)row_width * 2;
2123 for (i = 0; i < row_width; i++)
2127 *(--dp) = hi_filler;
2128 *(--dp) = lo_filler;
2130 row_info->channels = 2;
2131 row_info->pixel_depth = 32;
2132 row_info->rowbytes = row_width * 4;
2135 } /* COLOR_TYPE == GRAY */
2136 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2138 if (row_info->bit_depth == 8)
2140 /* This changes the data from RGB to RGBX */
2141 if (flags & PNG_FLAG_FILLER_AFTER)
2143 png_bytep sp = row + (png_size_t)row_width * 3;
2144 png_bytep dp = sp + (png_size_t)row_width;
2145 for (i = 1; i < row_width; i++)
2147 *(--dp) = lo_filler;
2152 *(--dp) = lo_filler;
2153 row_info->channels = 4;
2154 row_info->pixel_depth = 32;
2155 row_info->rowbytes = row_width * 4;
2157 /* This changes the data from RGB to XRGB */
2160 png_bytep sp = row + (png_size_t)row_width * 3;
2161 png_bytep dp = sp + (png_size_t)row_width;
2162 for (i = 0; i < row_width; i++)
2167 *(--dp) = lo_filler;
2169 row_info->channels = 4;
2170 row_info->pixel_depth = 32;
2171 row_info->rowbytes = row_width * 4;
2174 else if (row_info->bit_depth == 16)
2176 /* This changes the data from RRGGBB to RRGGBBXX */
2177 if (flags & PNG_FLAG_FILLER_AFTER)
2179 png_bytep sp = row + (png_size_t)row_width * 6;
2180 png_bytep dp = sp + (png_size_t)row_width * 2;
2181 for (i = 1; i < row_width; i++)
2183 *(--dp) = hi_filler;
2184 *(--dp) = lo_filler;
2192 *(--dp) = hi_filler;
2193 *(--dp) = lo_filler;
2194 row_info->channels = 4;
2195 row_info->pixel_depth = 64;
2196 row_info->rowbytes = row_width * 8;
2198 /* This changes the data from RRGGBB to XXRRGGBB */
2201 png_bytep sp = row + (png_size_t)row_width * 6;
2202 png_bytep dp = sp + (png_size_t)row_width * 2;
2203 for (i = 0; i < row_width; i++)
2211 *(--dp) = hi_filler;
2212 *(--dp) = lo_filler;
2214 row_info->channels = 4;
2215 row_info->pixel_depth = 64;
2216 row_info->rowbytes = row_width * 8;
2219 } /* COLOR_TYPE == RGB */
2223 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2224 /* Expand grayscale files to RGB, with or without alpha */
2226 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2229 png_uint_32 row_width = row_info->width;
2231 png_debug(1, "in png_do_gray_to_rgb");
2233 if (row_info->bit_depth >= 8 &&
2234 #ifdef PNG_USELESS_TESTS_SUPPORTED
2235 row != NULL && row_info != NULL &&
2237 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2239 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2241 if (row_info->bit_depth == 8)
2243 png_bytep sp = row + (png_size_t)row_width - 1;
2244 png_bytep dp = sp + (png_size_t)row_width * 2;
2245 for (i = 0; i < row_width; i++)
2254 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2255 png_bytep dp = sp + (png_size_t)row_width * 4;
2256 for (i = 0; i < row_width; i++)
2259 *(dp--) = *(sp - 1);
2261 *(dp--) = *(sp - 1);
2267 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2269 if (row_info->bit_depth == 8)
2271 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2272 png_bytep dp = sp + (png_size_t)row_width * 2;
2273 for (i = 0; i < row_width; i++)
2283 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2284 png_bytep dp = sp + (png_size_t)row_width * 4;
2285 for (i = 0; i < row_width; i++)
2290 *(dp--) = *(sp - 1);
2292 *(dp--) = *(sp - 1);
2298 row_info->channels += (png_byte)2;
2299 row_info->color_type |= PNG_COLOR_MASK_COLOR;
2300 row_info->pixel_depth = (png_byte)(row_info->channels *
2301 row_info->bit_depth);
2302 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2307 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2308 /* Reduce RGB files to grayscale, with or without alpha
2309 * using the equation given in Poynton's ColorFAQ at
2310 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2312 * <http://www.poynton.com/notes/colour_and_gamma/>
2313 * Charles Poynton poynton at poynton.com
2315 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2317 * We approximate this with
2319 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2321 * which can be expressed with integers as
2323 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2325 * The calculation is to be done in a linear colorspace.
2327 * Other integer coefficents can be used via png_set_rgb_to_gray().
2330 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2335 png_uint_32 row_width = row_info->width;
2338 png_debug(1, "in png_do_rgb_to_gray");
2341 #ifdef PNG_USELESS_TESTS_SUPPORTED
2342 row != NULL && row_info != NULL &&
2344 (row_info->color_type & PNG_COLOR_MASK_COLOR))
2346 png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2347 png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2348 png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2350 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2352 if (row_info->bit_depth == 8)
2354 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2355 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2360 for (i = 0; i < row_width; i++)
2362 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2363 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2364 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2365 if (red != green || red != blue)
2368 *(dp++) = png_ptr->gamma_from_1[
2369 (rc*red + gc*green + bc*blue)>>15];
2372 *(dp++) = *(sp - 1);
2380 for (i = 0; i < row_width; i++)
2382 png_byte red = *(sp++);
2383 png_byte green = *(sp++);
2384 png_byte blue = *(sp++);
2385 if (red != green || red != blue)
2388 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2391 *(dp++) = *(sp - 1);
2396 else /* RGB bit_depth == 16 */
2398 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2399 if (png_ptr->gamma_16_to_1 != NULL &&
2400 png_ptr->gamma_16_from_1 != NULL)
2404 for (i = 0; i < row_width; i++)
2406 png_uint_16 red, green, blue, w;
2408 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2409 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2410 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2412 if (red == green && red == blue)
2416 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2417 png_ptr->gamma_shift][red>>8];
2418 png_uint_16 green_1 =
2419 png_ptr->gamma_16_to_1[(green&0xff) >>
2420 png_ptr->gamma_shift][green>>8];
2421 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2422 png_ptr->gamma_shift][blue>>8];
2423 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
2425 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2426 png_ptr->gamma_shift][gray16 >> 8];
2430 *(dp++) = (png_byte)((w>>8) & 0xff);
2431 *(dp++) = (png_byte)(w & 0xff);
2439 for (i = 0; i < row_width; i++)
2441 png_uint_16 red, green, blue, gray16;
2443 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2444 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2445 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2447 if (red != green || red != blue)
2449 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2450 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2451 *(dp++) = (png_byte)(gray16 & 0xff);
2456 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2458 if (row_info->bit_depth == 8)
2460 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2461 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2465 for (i = 0; i < row_width; i++)
2467 png_byte red = png_ptr->gamma_to_1[*(sp++)];
2468 png_byte green = png_ptr->gamma_to_1[*(sp++)];
2469 png_byte blue = png_ptr->gamma_to_1[*(sp++)];
2470 if (red != green || red != blue)
2472 *(dp++) = png_ptr->gamma_from_1
2473 [(rc*red + gc*green + bc*blue)>>15];
2474 *(dp++) = *(sp++); /* alpha */
2482 for (i = 0; i < row_width; i++)
2484 png_byte red = *(sp++);
2485 png_byte green = *(sp++);
2486 png_byte blue = *(sp++);
2487 if (red != green || red != blue)
2489 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2490 *(dp++) = *(sp++); /* alpha */
2494 else /* RGBA bit_depth == 16 */
2496 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2497 if (png_ptr->gamma_16_to_1 != NULL &&
2498 png_ptr->gamma_16_from_1 != NULL)
2502 for (i = 0; i < row_width; i++)
2504 png_uint_16 red, green, blue, w;
2506 red = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2507 green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2508 blue = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2510 if (red == green && red == blue)
2514 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff) >>
2515 png_ptr->gamma_shift][red>>8];
2516 png_uint_16 green_1 =
2517 png_ptr->gamma_16_to_1[(green&0xff) >>
2518 png_ptr->gamma_shift][green>>8];
2519 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff) >>
2520 png_ptr->gamma_shift][blue>>8];
2521 png_uint_16 gray16 = (png_uint_16)((rc * red_1
2522 + gc * green_1 + bc * blue_1)>>15);
2523 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2524 png_ptr->gamma_shift][gray16 >> 8];
2528 *(dp++) = (png_byte)((w>>8) & 0xff);
2529 *(dp++) = (png_byte)(w & 0xff);
2530 *(dp++) = *(sp++); /* alpha */
2539 for (i = 0; i < row_width; i++)
2541 png_uint_16 red, green, blue, gray16;
2542 red = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2543 green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2544 blue = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2545 if (red != green || red != blue)
2547 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2548 *(dp++) = (png_byte)((gray16>>8) & 0xff);
2549 *(dp++) = (png_byte)(gray16 & 0xff);
2550 *(dp++) = *(sp++); /* alpha */
2556 row_info->channels -= (png_byte)2;
2557 row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
2558 row_info->pixel_depth = (png_byte)(row_info->channels *
2559 row_info->bit_depth);
2560 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2566 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2567 * large of png_color. This lets grayscale images be treated as
2568 * paletted. Most useful for gamma correction and simplification
2572 png_build_grayscale_palette(int bit_depth, png_colorp palette)
2579 png_debug(1, "in png_do_build_grayscale_palette");
2581 if (palette == NULL)
2612 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2614 palette[i].red = (png_byte)v;
2615 palette[i].green = (png_byte)v;
2616 palette[i].blue = (png_byte)v;
2620 /* This function is currently unused. Do we really need it? */
2621 #if defined(PNG_READ_DITHER_SUPPORTED) && \
2622 defined(PNG_CORRECT_PALETTE_SUPPORTED)
2624 png_correct_palette(png_structp png_ptr, png_colorp palette,
2627 png_debug(1, "in png_correct_palette");
2629 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
2630 defined(PNG_READ_GAMMA_SUPPORTED) && \
2631 defined(PNG_FLOATING_POINT_SUPPORTED)
2632 if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
2634 png_color back, back_1;
2636 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
2638 back.red = png_ptr->gamma_table[png_ptr->background.red];
2639 back.green = png_ptr->gamma_table[png_ptr->background.green];
2640 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
2642 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
2643 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
2644 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
2650 g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
2652 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN
2653 || fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
2655 back.red = png_ptr->background.red;
2656 back.green = png_ptr->background.green;
2657 back.blue = png_ptr->background.blue;
2662 (png_byte)(pow((double)png_ptr->background.red/255, g) *
2665 (png_byte)(pow((double)png_ptr->background.green/255, g) *
2668 (png_byte)(pow((double)png_ptr->background.blue/255, g) *
2672 g = 1.0 / png_ptr->background_gamma;
2675 (png_byte)(pow((double)png_ptr->background.red/255, g) *
2678 (png_byte)(pow((double)png_ptr->background.green/255, g) *
2681 (png_byte)(pow((double)png_ptr->background.blue/255, g) *
2685 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2689 for (i = 0; i < (png_uint_32)num_palette; i++)
2691 if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
2695 else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
2699 v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
2700 png_composite(w, v, png_ptr->trans[i], back_1.red);
2701 palette[i].red = png_ptr->gamma_from_1[w];
2703 v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
2704 png_composite(w, v, png_ptr->trans[i], back_1.green);
2705 palette[i].green = png_ptr->gamma_from_1[w];
2707 v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
2708 png_composite(w, v, png_ptr->trans[i], back_1.blue);
2709 palette[i].blue = png_ptr->gamma_from_1[w];
2713 palette[i].red = png_ptr->gamma_table[palette[i].red];
2714 palette[i].green = png_ptr->gamma_table[palette[i].green];
2715 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2723 for (i = 0; i < num_palette; i++)
2725 if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
2731 palette[i].red = png_ptr->gamma_table[palette[i].red];
2732 palette[i].green = png_ptr->gamma_table[palette[i].green];
2733 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2740 #ifdef PNG_READ_GAMMA_SUPPORTED
2741 if (png_ptr->transformations & PNG_GAMMA)
2745 for (i = 0; i < num_palette; i++)
2747 palette[i].red = png_ptr->gamma_table[palette[i].red];
2748 palette[i].green = png_ptr->gamma_table[palette[i].green];
2749 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
2752 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2756 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2757 if (png_ptr->transformations & PNG_BACKGROUND)
2759 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2763 back.red = (png_byte)png_ptr->background.red;
2764 back.green = (png_byte)png_ptr->background.green;
2765 back.blue = (png_byte)png_ptr->background.blue;
2767 for (i = 0; i < (int)png_ptr->num_trans; i++)
2769 if (png_ptr->trans[i] == 0)
2771 palette[i].red = back.red;
2772 palette[i].green = back.green;
2773 palette[i].blue = back.blue;
2775 else if (png_ptr->trans[i] != 0xff)
2777 png_composite(palette[i].red, png_ptr->palette[i].red,
2778 png_ptr->trans[i], back.red);
2779 png_composite(palette[i].green, png_ptr->palette[i].green,
2780 png_ptr->trans[i], back.green);
2781 png_composite(palette[i].blue, png_ptr->palette[i].blue,
2782 png_ptr->trans[i], back.blue);
2786 else /* Assume grayscale palette (what else could it be?) */
2790 for (i = 0; i < num_palette; i++)
2792 if (i == (png_byte)png_ptr->trans_values.gray)
2794 palette[i].red = (png_byte)png_ptr->background.red;
2795 palette[i].green = (png_byte)png_ptr->background.green;
2796 palette[i].blue = (png_byte)png_ptr->background.blue;
2805 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2806 /* Replace any alpha or transparency with the supplied background color.
2807 * "background" is already in the screen gamma, while "background_1" is
2808 * at a gamma of 1.0. Paletted files have already been taken care of.
2811 png_do_background(png_row_infop row_info, png_bytep row,
2812 png_color_16p trans_values, png_color_16p background
2813 #ifdef PNG_READ_GAMMA_SUPPORTED
2814 , png_color_16p background_1,
2815 png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
2816 png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
2817 png_uint_16pp gamma_16_to_1, int gamma_shift
2823 png_uint_32 row_width=row_info->width;
2826 png_debug(1, "in png_do_background");
2828 if (background != NULL &&
2829 #ifdef PNG_USELESS_TESTS_SUPPORTED
2830 row != NULL && row_info != NULL &&
2832 (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2833 (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
2835 switch (row_info->color_type)
2837 case PNG_COLOR_TYPE_GRAY:
2839 switch (row_info->bit_depth)
2845 for (i = 0; i < row_width; i++)
2847 if ((png_uint_16)((*sp >> shift) & 0x01)
2848 == trans_values->gray)
2850 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2851 *sp |= (png_byte)(background->gray << shift);
2866 #ifdef PNG_READ_GAMMA_SUPPORTED
2867 if (gamma_table != NULL)
2871 for (i = 0; i < row_width; i++)
2873 if ((png_uint_16)((*sp >> shift) & 0x03)
2874 == trans_values->gray)
2876 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2877 *sp |= (png_byte)(background->gray << shift);
2881 png_byte p = (png_byte)((*sp >> shift) & 0x03);
2882 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2883 (p << 4) | (p << 6)] >> 6) & 0x03);
2884 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2885 *sp |= (png_byte)(g << shift);
2901 for (i = 0; i < row_width; i++)
2903 if ((png_uint_16)((*sp >> shift) & 0x03)
2904 == trans_values->gray)
2906 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2907 *sp |= (png_byte)(background->gray << shift);
2923 #ifdef PNG_READ_GAMMA_SUPPORTED
2924 if (gamma_table != NULL)
2928 for (i = 0; i < row_width; i++)
2930 if ((png_uint_16)((*sp >> shift) & 0x0f)
2931 == trans_values->gray)
2933 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2934 *sp |= (png_byte)(background->gray << shift);
2938 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2939 png_byte g = (png_byte)((gamma_table[p |
2940 (p << 4)] >> 4) & 0x0f);
2941 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2942 *sp |= (png_byte)(g << shift);
2958 for (i = 0; i < row_width; i++)
2960 if ((png_uint_16)((*sp >> shift) & 0x0f)
2961 == trans_values->gray)
2963 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2964 *sp |= (png_byte)(background->gray << shift);
2980 #ifdef PNG_READ_GAMMA_SUPPORTED
2981 if (gamma_table != NULL)
2984 for (i = 0; i < row_width; i++, sp++)
2986 if (*sp == trans_values->gray)
2988 *sp = (png_byte)background->gray;
2992 *sp = gamma_table[*sp];
3000 for (i = 0; i < row_width; i++, sp++)
3002 if (*sp == trans_values->gray)
3004 *sp = (png_byte)background->gray;
3013 #ifdef PNG_READ_GAMMA_SUPPORTED
3014 if (gamma_16 != NULL)
3017 for (i = 0; i < row_width; i++, sp += 2)
3021 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3022 if (v == trans_values->gray)
3024 /* Background is already in screen gamma */
3025 *sp = (png_byte)((background->gray >> 8) & 0xff);
3026 *(sp + 1) = (png_byte)(background->gray & 0xff);
3030 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3031 *sp = (png_byte)((v >> 8) & 0xff);
3032 *(sp + 1) = (png_byte)(v & 0xff);
3040 for (i = 0; i < row_width; i++, sp += 2)
3044 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3045 if (v == trans_values->gray)
3047 *sp = (png_byte)((background->gray >> 8) & 0xff);
3048 *(sp + 1) = (png_byte)(background->gray & 0xff);
3058 case PNG_COLOR_TYPE_RGB:
3060 if (row_info->bit_depth == 8)
3062 #ifdef PNG_READ_GAMMA_SUPPORTED
3063 if (gamma_table != NULL)
3066 for (i = 0; i < row_width; i++, sp += 3)
3068 if (*sp == trans_values->red &&
3069 *(sp + 1) == trans_values->green &&
3070 *(sp + 2) == trans_values->blue)
3072 *sp = (png_byte)background->red;
3073 *(sp + 1) = (png_byte)background->green;
3074 *(sp + 2) = (png_byte)background->blue;
3078 *sp = gamma_table[*sp];
3079 *(sp + 1) = gamma_table[*(sp + 1)];
3080 *(sp + 2) = gamma_table[*(sp + 2)];
3088 for (i = 0; i < row_width; i++, sp += 3)
3090 if (*sp == trans_values->red &&
3091 *(sp + 1) == trans_values->green &&
3092 *(sp + 2) == trans_values->blue)
3094 *sp = (png_byte)background->red;
3095 *(sp + 1) = (png_byte)background->green;
3096 *(sp + 2) = (png_byte)background->blue;
3101 else /* if (row_info->bit_depth == 16) */
3103 #ifdef PNG_READ_GAMMA_SUPPORTED
3104 if (gamma_16 != NULL)
3107 for (i = 0; i < row_width; i++, sp += 6)
3109 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3110 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3111 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
3112 if (r == trans_values->red && g == trans_values->green &&
3113 b == trans_values->blue)
3115 /* Background is already in screen gamma */
3116 *sp = (png_byte)((background->red >> 8) & 0xff);
3117 *(sp + 1) = (png_byte)(background->red & 0xff);
3118 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3119 *(sp + 3) = (png_byte)(background->green & 0xff);
3120 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3121 *(sp + 5) = (png_byte)(background->blue & 0xff);
3125 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3126 *sp = (png_byte)((v >> 8) & 0xff);
3127 *(sp + 1) = (png_byte)(v & 0xff);
3128 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3129 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3130 *(sp + 3) = (png_byte)(v & 0xff);
3131 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3132 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3133 *(sp + 5) = (png_byte)(v & 0xff);
3141 for (i = 0; i < row_width; i++, sp += 6)
3143 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
3144 png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3145 png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
3147 if (r == trans_values->red && g == trans_values->green &&
3148 b == trans_values->blue)
3150 *sp = (png_byte)((background->red >> 8) & 0xff);
3151 *(sp + 1) = (png_byte)(background->red & 0xff);
3152 *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3153 *(sp + 3) = (png_byte)(background->green & 0xff);
3154 *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3155 *(sp + 5) = (png_byte)(background->blue & 0xff);
3163 case PNG_COLOR_TYPE_GRAY_ALPHA:
3165 if (row_info->bit_depth == 8)
3167 #ifdef PNG_READ_GAMMA_SUPPORTED
3168 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3169 gamma_table != NULL)
3173 for (i = 0; i < row_width; i++, sp += 2, dp++)
3175 png_uint_16 a = *(sp + 1);
3179 *dp = gamma_table[*sp];
3183 /* Background is already in screen gamma */
3184 *dp = (png_byte)background->gray;
3190 v = gamma_to_1[*sp];
3191 png_composite(w, v, a, background_1->gray);
3192 *dp = gamma_from_1[w];
3201 for (i = 0; i < row_width; i++, sp += 2, dp++)
3203 png_byte a = *(sp + 1);
3209 #ifdef PNG_READ_GAMMA_SUPPORTED
3212 *dp = (png_byte)background->gray;
3216 png_composite(*dp, *sp, a, background_1->gray);
3219 *dp = (png_byte)background->gray;
3224 else /* if (png_ptr->bit_depth == 16) */
3226 #ifdef PNG_READ_GAMMA_SUPPORTED
3227 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3228 gamma_16_to_1 != NULL)
3232 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3234 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3236 if (a == (png_uint_16)0xffff)
3240 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3241 *dp = (png_byte)((v >> 8) & 0xff);
3242 *(dp + 1) = (png_byte)(v & 0xff);
3244 #ifdef PNG_READ_GAMMA_SUPPORTED
3250 /* Background is already in screen gamma */
3251 *dp = (png_byte)((background->gray >> 8) & 0xff);
3252 *(dp + 1) = (png_byte)(background->gray & 0xff);
3254 #ifdef PNG_READ_GAMMA_SUPPORTED
3257 png_uint_16 g, v, w;
3259 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3260 png_composite_16(v, g, a, background_1->gray);
3261 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3262 *dp = (png_byte)((w >> 8) & 0xff);
3263 *(dp + 1) = (png_byte)(w & 0xff);
3273 for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3275 png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
3276 if (a == (png_uint_16)0xffff)
3278 png_memcpy(dp, sp, 2);
3280 #ifdef PNG_READ_GAMMA_SUPPORTED
3286 *dp = (png_byte)((background->gray >> 8) & 0xff);
3287 *(dp + 1) = (png_byte)(background->gray & 0xff);
3289 #ifdef PNG_READ_GAMMA_SUPPORTED
3294 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3295 png_composite_16(v, g, a, background_1->gray);
3296 *dp = (png_byte)((v >> 8) & 0xff);
3297 *(dp + 1) = (png_byte)(v & 0xff);
3306 case PNG_COLOR_TYPE_RGB_ALPHA:
3308 if (row_info->bit_depth == 8)
3310 #ifdef PNG_READ_GAMMA_SUPPORTED
3311 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3312 gamma_table != NULL)
3316 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3318 png_byte a = *(sp + 3);
3322 *dp = gamma_table[*sp];
3323 *(dp + 1) = gamma_table[*(sp + 1)];
3324 *(dp + 2) = gamma_table[*(sp + 2)];
3328 /* Background is already in screen gamma */
3329 *dp = (png_byte)background->red;
3330 *(dp + 1) = (png_byte)background->green;
3331 *(dp + 2) = (png_byte)background->blue;
3337 v = gamma_to_1[*sp];
3338 png_composite(w, v, a, background_1->red);
3339 *dp = gamma_from_1[w];
3340 v = gamma_to_1[*(sp + 1)];
3341 png_composite(w, v, a, background_1->green);
3342 *(dp + 1) = gamma_from_1[w];
3343 v = gamma_to_1[*(sp + 2)];
3344 png_composite(w, v, a, background_1->blue);
3345 *(dp + 2) = gamma_from_1[w];
3354 for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3356 png_byte a = *(sp + 3);
3361 *(dp + 1) = *(sp + 1);
3362 *(dp + 2) = *(sp + 2);
3366 *dp = (png_byte)background->red;
3367 *(dp + 1) = (png_byte)background->green;
3368 *(dp + 2) = (png_byte)background->blue;
3372 png_composite(*dp, *sp, a, background->red);
3373 png_composite(*(dp + 1), *(sp + 1), a,
3375 png_composite(*(dp + 2), *(sp + 2), a,
3381 else /* if (row_info->bit_depth == 16) */
3383 #ifdef PNG_READ_GAMMA_SUPPORTED
3384 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3385 gamma_16_to_1 != NULL)
3389 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3391 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3392 << 8) + (png_uint_16)(*(sp + 7)));
3393 if (a == (png_uint_16)0xffff)
3397 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3398 *dp = (png_byte)((v >> 8) & 0xff);
3399 *(dp + 1) = (png_byte)(v & 0xff);
3400 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3401 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3402 *(dp + 3) = (png_byte)(v & 0xff);
3403 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3404 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3405 *(dp + 5) = (png_byte)(v & 0xff);
3409 /* Background is already in screen gamma */
3410 *dp = (png_byte)((background->red >> 8) & 0xff);
3411 *(dp + 1) = (png_byte)(background->red & 0xff);
3412 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3413 *(dp + 3) = (png_byte)(background->green & 0xff);
3414 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3415 *(dp + 5) = (png_byte)(background->blue & 0xff);
3419 png_uint_16 v, w, x;
3421 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3422 png_composite_16(w, v, a, background_1->red);
3423 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3424 *dp = (png_byte)((x >> 8) & 0xff);
3425 *(dp + 1) = (png_byte)(x & 0xff);
3426 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3427 png_composite_16(w, v, a, background_1->green);
3428 x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3429 *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3430 *(dp + 3) = (png_byte)(x & 0xff);
3431 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3432 png_composite_16(w, v, a, background_1->blue);
3433 x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
3434 *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3435 *(dp + 5) = (png_byte)(x & 0xff);
3444 for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3446 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3447 << 8) + (png_uint_16)(*(sp + 7)));
3448 if (a == (png_uint_16)0xffff)
3450 png_memcpy(dp, sp, 6);
3454 *dp = (png_byte)((background->red >> 8) & 0xff);
3455 *(dp + 1) = (png_byte)(background->red & 0xff);
3456 *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3457 *(dp + 3) = (png_byte)(background->green & 0xff);
3458 *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3459 *(dp + 5) = (png_byte)(background->blue & 0xff);
3465 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3466 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3468 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3471 png_composite_16(v, r, a, background->red);
3472 *dp = (png_byte)((v >> 8) & 0xff);
3473 *(dp + 1) = (png_byte)(v & 0xff);
3474 png_composite_16(v, g, a, background->green);
3475 *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3476 *(dp + 3) = (png_byte)(v & 0xff);
3477 png_composite_16(v, b, a, background->blue);
3478 *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3479 *(dp + 5) = (png_byte)(v & 0xff);
3488 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
3490 row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
3491 row_info->channels--;
3492 row_info->pixel_depth = (png_byte)(row_info->channels *
3493 row_info->bit_depth);
3494 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3500 #ifdef PNG_READ_GAMMA_SUPPORTED
3501 /* Gamma correct the image, avoiding the alpha channel. Make sure
3502 * you do this after you deal with the transparency issue on grayscale
3503 * or RGB images. If your bit depth is 8, use gamma_table, if it
3504 * is 16, use gamma_16_table and gamma_shift. Build these with
3505 * build_gamma_table().
3508 png_do_gamma(png_row_infop row_info, png_bytep row,
3509 png_bytep gamma_table, png_uint_16pp gamma_16_table,
3514 png_uint_32 row_width=row_info->width;
3516 png_debug(1, "in png_do_gamma");
3519 #ifdef PNG_USELESS_TESTS_SUPPORTED
3520 row != NULL && row_info != NULL &&
3522 ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3523 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3525 switch (row_info->color_type)
3527 case PNG_COLOR_TYPE_RGB:
3529 if (row_info->bit_depth == 8)
3532 for (i = 0; i < row_width; i++)
3534 *sp = gamma_table[*sp];
3536 *sp = gamma_table[*sp];
3538 *sp = gamma_table[*sp];
3542 else /* if (row_info->bit_depth == 16) */
3545 for (i = 0; i < row_width; i++)
3549 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3550 *sp = (png_byte)((v >> 8) & 0xff);
3551 *(sp + 1) = (png_byte)(v & 0xff);
3553 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3554 *sp = (png_byte)((v >> 8) & 0xff);
3555 *(sp + 1) = (png_byte)(v & 0xff);
3557 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3558 *sp = (png_byte)((v >> 8) & 0xff);
3559 *(sp + 1) = (png_byte)(v & 0xff);
3566 case PNG_COLOR_TYPE_RGB_ALPHA:
3568 if (row_info->bit_depth == 8)
3571 for (i = 0; i < row_width; i++)
3573 *sp = gamma_table[*sp];
3575 *sp = gamma_table[*sp];
3577 *sp = gamma_table[*sp];
3582 else /* if (row_info->bit_depth == 16) */
3585 for (i = 0; i < row_width; i++)
3587 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3588 *sp = (png_byte)((v >> 8) & 0xff);
3589 *(sp + 1) = (png_byte)(v & 0xff);
3591 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3592 *sp = (png_byte)((v >> 8) & 0xff);
3593 *(sp + 1) = (png_byte)(v & 0xff);
3595 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3596 *sp = (png_byte)((v >> 8) & 0xff);
3597 *(sp + 1) = (png_byte)(v & 0xff);
3604 case PNG_COLOR_TYPE_GRAY_ALPHA:
3606 if (row_info->bit_depth == 8)
3609 for (i = 0; i < row_width; i++)
3611 *sp = gamma_table[*sp];
3615 else /* if (row_info->bit_depth == 16) */
3618 for (i = 0; i < row_width; i++)
3620 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3621 *sp = (png_byte)((v >> 8) & 0xff);
3622 *(sp + 1) = (png_byte)(v & 0xff);
3629 case PNG_COLOR_TYPE_GRAY:
3631 if (row_info->bit_depth == 2)
3634 for (i = 0; i < row_width; i += 4)
3642 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
3643 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3644 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3645 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3650 if (row_info->bit_depth == 4)
3653 for (i = 0; i < row_width; i += 2)
3655 int msb = *sp & 0xf0;
3656 int lsb = *sp & 0x0f;
3658 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3659 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3664 else if (row_info->bit_depth == 8)
3667 for (i = 0; i < row_width; i++)
3669 *sp = gamma_table[*sp];
3674 else if (row_info->bit_depth == 16)
3677 for (i = 0; i < row_width; i++)
3679 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3680 *sp = (png_byte)((v >> 8) & 0xff);
3681 *(sp + 1) = (png_byte)(v & 0xff);
3692 #ifdef PNG_READ_EXPAND_SUPPORTED
3693 /* Expands a palette row to an RGB or RGBA row depending
3694 * upon whether you supply trans and num_trans.
3697 png_do_expand_palette(png_row_infop row_info, png_bytep row,
3698 png_colorp palette, png_bytep trans, int num_trans)
3703 png_uint_32 row_width=row_info->width;
3705 png_debug(1, "in png_do_expand_palette");
3708 #ifdef PNG_USELESS_TESTS_SUPPORTED
3709 row != NULL && row_info != NULL &&
3711 row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3713 if (row_info->bit_depth < 8)
3715 switch (row_info->bit_depth)
3719 sp = row + (png_size_t)((row_width - 1) >> 3);
3720 dp = row + (png_size_t)row_width - 1;
3721 shift = 7 - (int)((row_width + 7) & 0x07);
3722 for (i = 0; i < row_width; i++)
3724 if ((*sp >> shift) & 0x01)
3743 sp = row + (png_size_t)((row_width - 1) >> 2);
3744 dp = row + (png_size_t)row_width - 1;
3745 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3746 for (i = 0; i < row_width; i++)
3748 value = (*sp >> shift) & 0x03;
3749 *dp = (png_byte)value;
3765 sp = row + (png_size_t)((row_width - 1) >> 1);
3766 dp = row + (png_size_t)row_width - 1;
3767 shift = (int)((row_width & 0x01) << 2);
3768 for (i = 0; i < row_width; i++)
3770 value = (*sp >> shift) & 0x0f;
3771 *dp = (png_byte)value;
3785 row_info->bit_depth = 8;
3786 row_info->pixel_depth = 8;
3787 row_info->rowbytes = row_width;
3789 switch (row_info->bit_depth)
3795 sp = row + (png_size_t)row_width - 1;
3796 dp = row + (png_size_t)(row_width << 2) - 1;
3798 for (i = 0; i < row_width; i++)
3800 if ((int)(*sp) >= num_trans)
3804 *dp-- = palette[*sp].blue;
3805 *dp-- = palette[*sp].green;
3806 *dp-- = palette[*sp].red;
3809 row_info->bit_depth = 8;
3810 row_info->pixel_depth = 32;
3811 row_info->rowbytes = row_width * 4;
3812 row_info->color_type = 6;
3813 row_info->channels = 4;
3817 sp = row + (png_size_t)row_width - 1;
3818 dp = row + (png_size_t)(row_width * 3) - 1;
3820 for (i = 0; i < row_width; i++)
3822 *dp-- = palette[*sp].blue;
3823 *dp-- = palette[*sp].green;
3824 *dp-- = palette[*sp].red;
3828 row_info->bit_depth = 8;
3829 row_info->pixel_depth = 24;
3830 row_info->rowbytes = row_width * 3;
3831 row_info->color_type = 2;
3832 row_info->channels = 3;
3840 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3841 * expanded transparency value is supplied, an alpha channel is built.
3844 png_do_expand(png_row_infop row_info, png_bytep row,
3845 png_color_16p trans_value)
3850 png_uint_32 row_width=row_info->width;
3852 png_debug(1, "in png_do_expand");
3854 #ifdef PNG_USELESS_TESTS_SUPPORTED
3855 if (row != NULL && row_info != NULL)
3858 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3860 png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3862 if (row_info->bit_depth < 8)
3864 switch (row_info->bit_depth)
3868 gray = (png_uint_16)((gray&0x01)*0xff);
3869 sp = row + (png_size_t)((row_width - 1) >> 3);
3870 dp = row + (png_size_t)row_width - 1;
3871 shift = 7 - (int)((row_width + 7) & 0x07);
3872 for (i = 0; i < row_width; i++)
3874 if ((*sp >> shift) & 0x01)
3893 gray = (png_uint_16)((gray&0x03)*0x55);
3894 sp = row + (png_size_t)((row_width - 1) >> 2);
3895 dp = row + (png_size_t)row_width - 1;
3896 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3897 for (i = 0; i < row_width; i++)
3899 value = (*sp >> shift) & 0x03;
3900 *dp = (png_byte)(value | (value << 2) | (value << 4) |
3917 gray = (png_uint_16)((gray&0x0f)*0x11);
3918 sp = row + (png_size_t)((row_width - 1) >> 1);
3919 dp = row + (png_size_t)row_width - 1;
3920 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3921 for (i = 0; i < row_width; i++)
3923 value = (*sp >> shift) & 0x0f;
3924 *dp = (png_byte)(value | (value << 4));
3939 row_info->bit_depth = 8;
3940 row_info->pixel_depth = 8;
3941 row_info->rowbytes = row_width;
3944 if (trans_value != NULL)
3946 if (row_info->bit_depth == 8)
3949 sp = row + (png_size_t)row_width - 1;
3950 dp = row + (png_size_t)(row_width << 1) - 1;
3951 for (i = 0; i < row_width; i++)
3961 else if (row_info->bit_depth == 16)
3963 png_byte gray_high = (gray >> 8) & 0xff;
3964 png_byte gray_low = gray & 0xff;
3965 sp = row + row_info->rowbytes - 1;
3966 dp = row + (row_info->rowbytes << 1) - 1;
3967 for (i = 0; i < row_width; i++)
3969 if (*(sp - 1) == gray_high && *(sp) == gray_low)
3984 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
3985 row_info->channels = 2;
3986 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3987 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3991 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
3993 if (row_info->bit_depth == 8)
3995 png_byte red = trans_value->red & 0xff;
3996 png_byte green = trans_value->green & 0xff;
3997 png_byte blue = trans_value->blue & 0xff;
3998 sp = row + (png_size_t)row_info->rowbytes - 1;
3999 dp = row + (png_size_t)(row_width << 2) - 1;
4000 for (i = 0; i < row_width; i++)
4002 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4011 else if (row_info->bit_depth == 16)
4013 png_byte red_high = (trans_value->red >> 8) & 0xff;
4014 png_byte green_high = (trans_value->green >> 8) & 0xff;
4015 png_byte blue_high = (trans_value->blue >> 8) & 0xff;
4016 png_byte red_low = trans_value->red & 0xff;
4017 png_byte green_low = trans_value->green & 0xff;
4018 png_byte blue_low = trans_value->blue & 0xff;
4019 sp = row + row_info->rowbytes - 1;
4020 dp = row + (png_size_t)(row_width << 3) - 1;
4021 for (i = 0; i < row_width; i++)
4023 if (*(sp - 5) == red_high &&
4024 *(sp - 4) == red_low &&
4025 *(sp - 3) == green_high &&
4026 *(sp - 2) == green_low &&
4027 *(sp - 1) == blue_high &&
4046 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4047 row_info->channels = 4;
4048 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4049 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4055 #ifdef PNG_READ_DITHER_SUPPORTED
4057 png_do_dither(png_row_infop row_info, png_bytep row,
4058 png_bytep palette_lookup, png_bytep dither_lookup)
4062 png_uint_32 row_width=row_info->width;
4064 png_debug(1, "in png_do_dither");
4066 #ifdef PNG_USELESS_TESTS_SUPPORTED
4067 if (row != NULL && row_info != NULL)
4070 if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4071 palette_lookup && row_info->bit_depth == 8)
4076 for (i = 0; i < row_width; i++)
4082 /* This looks real messy, but the compiler will reduce
4083 * it down to a reasonable formula. For example, with
4084 * 5 bits per color, we get:
4085 * p = (((r >> 3) & 0x1f) << 10) |
4086 * (((g >> 3) & 0x1f) << 5) |
4087 * ((b >> 3) & 0x1f);
4089 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
4090 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
4091 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
4092 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
4093 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
4094 (PNG_DITHER_BLUE_BITS)) |
4095 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
4096 ((1 << PNG_DITHER_BLUE_BITS) - 1));
4098 *dp++ = palette_lookup[p];
4100 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4101 row_info->channels = 1;
4102 row_info->pixel_depth = row_info->bit_depth;
4103 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4105 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4106 palette_lookup != NULL && row_info->bit_depth == 8)
4111 for (i = 0; i < row_width; i++)
4118 p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
4119 ((1 << PNG_DITHER_RED_BITS) - 1)) <<
4120 (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
4121 (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
4122 ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
4123 (PNG_DITHER_BLUE_BITS)) |
4124 ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
4125 ((1 << PNG_DITHER_BLUE_BITS) - 1));
4127 *dp++ = palette_lookup[p];
4129 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4130 row_info->channels = 1;
4131 row_info->pixel_depth = row_info->bit_depth;
4132 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4134 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4135 dither_lookup && row_info->bit_depth == 8)
4138 for (i = 0; i < row_width; i++, sp++)
4140 *sp = dither_lookup[*sp];
4147 #ifdef PNG_FLOATING_POINT_SUPPORTED
4148 #ifdef PNG_READ_GAMMA_SUPPORTED
4149 static PNG_CONST int png_gamma_shift[] =
4150 {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
4152 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
4153 * tables, we don't make a full table if we are reducing to 8-bit in
4154 * the future. Note also how the gamma_16 tables are segmented so that
4155 * we don't need to allocate > 64K chunks for a full 16-bit table.
4157 * See the PNG extensions document for an integer algorithm for creating
4158 * the gamma tables. Maybe we will implement that here someday.
4160 * We should only reach this point if
4162 * the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
4163 * or the application has provided a file_gamma)
4167 * the screen_gamma is known
4170 * RGB_to_gray transformation is being performed
4175 * the screen_gamma is different from the reciprocal of the
4176 * file_gamma by more than the specified threshold
4180 * a background color has been specified and the file_gamma
4181 * and screen_gamma are not 1.0, within the specified threshold.
4186 png_build_gamma_table(png_structp png_ptr)
4188 png_debug(1, "in png_build_gamma_table");
4190 if (png_ptr->bit_depth <= 8)
4195 if (png_ptr->screen_gamma > .000001)
4196 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4201 png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
4204 for (i = 0; i < 256; i++)
4206 png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
4210 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4211 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4212 if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
4215 g = 1.0 / (png_ptr->gamma);
4217 png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
4220 for (i = 0; i < 256; i++)
4222 png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
4227 png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
4230 if (png_ptr->screen_gamma > 0.000001)
4231 g = 1.0 / png_ptr->screen_gamma;
4234 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4236 for (i = 0; i < 256; i++)
4238 png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
4243 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4248 int i, j, shift, num;
4252 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
4254 sig_bit = (int)png_ptr->sig_bit.red;
4256 if ((int)png_ptr->sig_bit.green > sig_bit)
4257 sig_bit = png_ptr->sig_bit.green;
4259 if ((int)png_ptr->sig_bit.blue > sig_bit)
4260 sig_bit = png_ptr->sig_bit.blue;
4264 sig_bit = (int)png_ptr->sig_bit.gray;
4268 shift = 16 - sig_bit;
4273 if (png_ptr->transformations & PNG_16_TO_8)
4275 if (shift < (16 - PNG_MAX_GAMMA_8))
4276 shift = (16 - PNG_MAX_GAMMA_8);
4285 png_ptr->gamma_shift = (png_byte)shift;
4287 num = (1 << (8 - shift));
4289 if (png_ptr->screen_gamma > .000001)
4290 g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
4294 png_ptr->gamma_16_table = (png_uint_16pp)png_calloc(png_ptr,
4295 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4297 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
4300 png_uint_32 last, max;
4302 for (i = 0; i < num; i++)
4304 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4305 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4310 for (i = 0; i < 256; i++)
4312 fout = ((double)i + 0.5) / 256.0;
4314 max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
4317 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4318 [(int)(last >> (8 - shift))] = (png_uint_16)(
4319 (png_uint_16)i | ((png_uint_16)i << 8));
4323 while (last < ((png_uint_32)num << 8))
4325 png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
4326 [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
4332 for (i = 0; i < num; i++)
4334 png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4335 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4337 ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
4339 for (j = 0; j < 256; j++)
4341 png_ptr->gamma_16_table[i][j] =
4342 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4343 65535.0, g) * 65535.0 + .5);
4348 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4349 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4350 if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
4353 g = 1.0 / (png_ptr->gamma);
4355 png_ptr->gamma_16_to_1 = (png_uint_16pp)png_calloc(png_ptr,
4356 (png_uint_32)(num * png_sizeof(png_uint_16p )));
4358 for (i = 0; i < num; i++)
4360 png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
4361 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4363 ig = (((png_uint_32)i *
4364 (png_uint_32)png_gamma_shift[shift]) >> 4);
4365 for (j = 0; j < 256; j++)
4367 png_ptr->gamma_16_to_1[i][j] =
4368 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4369 65535.0, g) * 65535.0 + .5);
4373 if (png_ptr->screen_gamma > 0.000001)
4374 g = 1.0 / png_ptr->screen_gamma;
4377 g = png_ptr->gamma; /* Probably doing rgb_to_gray */
4379 png_ptr->gamma_16_from_1 = (png_uint_16pp)png_calloc(png_ptr,
4380 (png_uint_32)(num * png_sizeof(png_uint_16p)));
4382 for (i = 0; i < num; i++)
4384 png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
4385 (png_uint_32)(256 * png_sizeof(png_uint_16)));
4387 ig = (((png_uint_32)i *
4388 (png_uint_32)png_gamma_shift[shift]) >> 4);
4390 for (j = 0; j < 256; j++)
4392 png_ptr->gamma_16_from_1[i][j] =
4393 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
4394 65535.0, g) * 65535.0 + .5);
4398 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4402 /* To do: install integer version of png_build_gamma_table here */
4405 #ifdef PNG_MNG_FEATURES_SUPPORTED
4406 /* Undoes intrapixel differencing */
4408 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4410 png_debug(1, "in png_do_read_intrapixel");
4413 #ifdef PNG_USELESS_TESTS_SUPPORTED
4414 row != NULL && row_info != NULL &&
4416 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4418 int bytes_per_pixel;
4419 png_uint_32 row_width = row_info->width;
4420 if (row_info->bit_depth == 8)
4425 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4426 bytes_per_pixel = 3;
4428 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4429 bytes_per_pixel = 4;
4434 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4436 *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
4437 *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
4440 else if (row_info->bit_depth == 16)
4445 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4446 bytes_per_pixel = 6;
4448 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4449 bytes_per_pixel = 8;
4454 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4456 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4457 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4458 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
4459 png_uint_32 red = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4460 png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4461 *(rp ) = (png_byte)((red >> 8) & 0xff);
4462 *(rp+1) = (png_byte)(red & 0xff);
4463 *(rp+4) = (png_byte)((blue >> 8) & 0xff);
4464 *(rp+5) = (png_byte)(blue & 0xff);
4469 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4470 #endif /* PNG_READ_SUPPORTED */