2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.2.51 [February 6, 2014]
5 * Copyright (c) 1998-2014 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)
921 k
=1; /* Partial transparency is present */
926 png_ptr
->transformations
&= ~PNG_GAMMA
;
929 if ((png_ptr
->transformations
& (PNG_GAMMA
| PNG_RGB_TO_GRAY
)) &&
930 png_ptr
->gamma
!= 0.0)
932 png_build_gamma_table(png_ptr
);
934 #ifdef PNG_READ_BACKGROUND_SUPPORTED
935 if (png_ptr
->transformations
& PNG_BACKGROUND
)
937 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
939 /* Could skip if no transparency */
940 png_color back
, back_1
;
941 png_colorp palette
= png_ptr
->palette
;
942 int num_palette
= png_ptr
->num_palette
;
944 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_FILE
)
946 back
.red
= png_ptr
->gamma_table
[png_ptr
->background
.red
];
947 back
.green
= png_ptr
->gamma_table
[png_ptr
->background
.green
];
948 back
.blue
= png_ptr
->gamma_table
[png_ptr
->background
.blue
];
950 back_1
.red
= png_ptr
->gamma_to_1
[png_ptr
->background
.red
];
951 back_1
.green
= png_ptr
->gamma_to_1
[png_ptr
->background
.green
];
952 back_1
.blue
= png_ptr
->gamma_to_1
[png_ptr
->background
.blue
];
958 switch (png_ptr
->background_gamma_type
)
960 case PNG_BACKGROUND_GAMMA_SCREEN
:
961 g
= (png_ptr
->screen_gamma
);
965 case PNG_BACKGROUND_GAMMA_FILE
:
966 g
= 1.0 / (png_ptr
->gamma
);
967 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
970 case PNG_BACKGROUND_GAMMA_UNIQUE
:
971 g
= 1.0 / (png_ptr
->background_gamma
);
972 gs
= 1.0 / (png_ptr
->background_gamma
*
973 png_ptr
->screen_gamma
);
976 g
= 1.0; /* back_1 */
980 if ( fabs(gs
- 1.0) < PNG_GAMMA_THRESHOLD
)
982 back
.red
= (png_byte
)png_ptr
->background
.red
;
983 back
.green
= (png_byte
)png_ptr
->background
.green
;
984 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
988 back
.red
= (png_byte
)(pow(
989 (double)png_ptr
->background
.red
/255, gs
) * 255.0 + .5);
990 back
.green
= (png_byte
)(pow(
991 (double)png_ptr
->background
.green
/255, gs
) * 255.0
993 back
.blue
= (png_byte
)(pow(
994 (double)png_ptr
->background
.blue
/255, gs
) * 255.0 + .5);
997 back_1
.red
= (png_byte
)(pow(
998 (double)png_ptr
->background
.red
/255, g
) * 255.0 + .5);
999 back_1
.green
= (png_byte
)(pow(
1000 (double)png_ptr
->background
.green
/255, g
) * 255.0 + .5);
1001 back_1
.blue
= (png_byte
)(pow(
1002 (double)png_ptr
->background
.blue
/255, g
) * 255.0 + .5);
1004 for (i
= 0; i
< num_palette
; i
++)
1006 if (i
< (int)png_ptr
->num_trans
&& png_ptr
->trans
[i
] != 0xff)
1008 if (png_ptr
->trans
[i
] == 0)
1012 else /* if (png_ptr->trans[i] != 0xff) */
1016 v
= png_ptr
->gamma_to_1
[palette
[i
].red
];
1017 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.red
);
1018 palette
[i
].red
= png_ptr
->gamma_from_1
[w
];
1020 v
= png_ptr
->gamma_to_1
[palette
[i
].green
];
1021 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.green
);
1022 palette
[i
].green
= png_ptr
->gamma_from_1
[w
];
1024 v
= png_ptr
->gamma_to_1
[palette
[i
].blue
];
1025 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.blue
);
1026 palette
[i
].blue
= png_ptr
->gamma_from_1
[w
];
1031 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1032 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1033 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1036 /* Prevent the transformations being done again, and make sure
1037 * that the now spurious alpha channel is stripped - the code
1038 * has just reduced background composition and gamma correction
1039 * to a simple alpha channel strip.
1041 png_ptr
->transformations
&= ~PNG_BACKGROUND
;
1042 png_ptr
->transformations
&= ~PNG_GAMMA
;
1043 png_ptr
->transformations
|= PNG_STRIP_ALPHA
;
1045 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1047 /* color_type != PNG_COLOR_TYPE_PALETTE */
1049 double m
= (double)(((png_uint_32
)1 << png_ptr
->bit_depth
) - 1);
1053 switch (png_ptr
->background_gamma_type
)
1055 case PNG_BACKGROUND_GAMMA_SCREEN
:
1056 g
= (png_ptr
->screen_gamma
);
1060 case PNG_BACKGROUND_GAMMA_FILE
:
1061 g
= 1.0 / (png_ptr
->gamma
);
1062 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
1065 case PNG_BACKGROUND_GAMMA_UNIQUE
:
1066 g
= 1.0 / (png_ptr
->background_gamma
);
1067 gs
= 1.0 / (png_ptr
->background_gamma
*
1068 png_ptr
->screen_gamma
);
1072 png_ptr
->background_1
.gray
= (png_uint_16
)(pow(
1073 (double)png_ptr
->background
.gray
/ m
, g
) * m
+ .5);
1074 png_ptr
->background
.gray
= (png_uint_16
)(pow(
1075 (double)png_ptr
->background
.gray
/ m
, gs
) * m
+ .5);
1077 if ((png_ptr
->background
.red
!= png_ptr
->background
.green
) ||
1078 (png_ptr
->background
.red
!= png_ptr
->background
.blue
) ||
1079 (png_ptr
->background
.red
!= png_ptr
->background
.gray
))
1081 /* RGB or RGBA with color background */
1082 png_ptr
->background_1
.red
= (png_uint_16
)(pow(
1083 (double)png_ptr
->background
.red
/ m
, g
) * m
+ .5);
1084 png_ptr
->background_1
.green
= (png_uint_16
)(pow(
1085 (double)png_ptr
->background
.green
/ m
, g
) * m
+ .5);
1086 png_ptr
->background_1
.blue
= (png_uint_16
)(pow(
1087 (double)png_ptr
->background
.blue
/ m
, g
) * m
+ .5);
1088 png_ptr
->background
.red
= (png_uint_16
)(pow(
1089 (double)png_ptr
->background
.red
/ m
, gs
) * m
+ .5);
1090 png_ptr
->background
.green
= (png_uint_16
)(pow(
1091 (double)png_ptr
->background
.green
/ m
, gs
) * m
+ .5);
1092 png_ptr
->background
.blue
= (png_uint_16
)(pow(
1093 (double)png_ptr
->background
.blue
/ m
, gs
) * m
+ .5);
1097 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1098 png_ptr
->background_1
.red
= png_ptr
->background_1
.green
1099 = png_ptr
->background_1
.blue
= png_ptr
->background_1
.gray
;
1100 png_ptr
->background
.red
= png_ptr
->background
.green
1101 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
1106 /* Transformation does not include PNG_BACKGROUND */
1107 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1108 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
1110 png_colorp palette
= png_ptr
->palette
;
1111 int num_palette
= png_ptr
->num_palette
;
1114 for (i
= 0; i
< num_palette
; i
++)
1116 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1117 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1118 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1121 /* Done the gamma correction. */
1122 png_ptr
->transformations
&= ~PNG_GAMMA
;
1125 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1128 #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1129 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1130 /* No GAMMA transformation */
1131 if ((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1132 (color_type
== PNG_COLOR_TYPE_PALETTE
))
1135 int istop
= (int)png_ptr
->num_trans
;
1137 png_colorp palette
= png_ptr
->palette
;
1139 back
.red
= (png_byte
)png_ptr
->background
.red
;
1140 back
.green
= (png_byte
)png_ptr
->background
.green
;
1141 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
1143 for (i
= 0; i
< istop
; i
++)
1145 if (png_ptr
->trans
[i
] == 0)
1149 else if (png_ptr
->trans
[i
] != 0xff)
1151 /* The png_composite() macro is defined in png.h */
1152 png_composite(palette
[i
].red
, palette
[i
].red
,
1153 png_ptr
->trans
[i
], back
.red
);
1154 png_composite(palette
[i
].green
, palette
[i
].green
,
1155 png_ptr
->trans
[i
], back
.green
);
1156 png_composite(palette
[i
].blue
, palette
[i
].blue
,
1157 png_ptr
->trans
[i
], back
.blue
);
1161 /* Handled alpha, still need to strip the channel. */
1162 png_ptr
->transformations
&= ~PNG_BACKGROUND
;
1163 png_ptr
->transformations
|= PNG_STRIP_ALPHA
;
1165 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1167 #ifdef PNG_READ_SHIFT_SUPPORTED
1168 if ((png_ptr
->transformations
& PNG_SHIFT
) &&
1169 !(png_ptr
->transformations
& PNG_EXPAND
) &&
1170 (color_type
== PNG_COLOR_TYPE_PALETTE
))
1173 png_uint_16 istop
= png_ptr
->num_palette
;
1174 int sr
= 8 - png_ptr
->sig_bit
.red
;
1175 int sg
= 8 - png_ptr
->sig_bit
.green
;
1176 int sb
= 8 - png_ptr
->sig_bit
.blue
;
1178 if (sr
< 0 || sr
> 8)
1180 if (sg
< 0 || sg
> 8)
1182 if (sb
< 0 || sb
> 8)
1184 for (i
= 0; i
< istop
; i
++)
1186 png_ptr
->palette
[i
].red
>>= sr
;
1187 png_ptr
->palette
[i
].green
>>= sg
;
1188 png_ptr
->palette
[i
].blue
>>= sb
;
1191 png_ptr
->transformations
&= ~PNG_SHIFT
;
1193 #endif /* PNG_READ_SHIFT_SUPPORTED */
1195 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1196 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1202 /* Modify the info structure to reflect the transformations. The
1203 * info should be updated so a PNG file could be written with it,
1204 * assuming the transformations result in valid PNG data.
1207 png_read_transform_info(png_structp png_ptr
, png_infop info_ptr
)
1209 png_debug(1, "in png_read_transform_info");
1211 #ifdef PNG_READ_EXPAND_SUPPORTED
1212 if (png_ptr
->transformations
& PNG_EXPAND
)
1214 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1216 if (png_ptr
->num_trans
)
1217 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
1219 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB
;
1220 info_ptr
->bit_depth
= 8;
1221 info_ptr
->num_trans
= 0;
1225 if (png_ptr
->num_trans
)
1227 if (png_ptr
->transformations
& PNG_EXPAND_tRNS
)
1228 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
1230 if (info_ptr
->bit_depth
< 8)
1231 info_ptr
->bit_depth
= 8;
1232 info_ptr
->num_trans
= 0;
1237 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1238 if (png_ptr
->transformations
& PNG_BACKGROUND
)
1240 info_ptr
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
1241 info_ptr
->num_trans
= 0;
1242 info_ptr
->background
= png_ptr
->background
;
1246 #ifdef PNG_READ_GAMMA_SUPPORTED
1247 if (png_ptr
->transformations
& PNG_GAMMA
)
1249 #ifdef PNG_FLOATING_POINT_SUPPORTED
1250 info_ptr
->gamma
= png_ptr
->gamma
;
1252 #ifdef PNG_FIXED_POINT_SUPPORTED
1253 info_ptr
->int_gamma
= png_ptr
->int_gamma
;
1258 #ifdef PNG_READ_16_TO_8_SUPPORTED
1259 if ((png_ptr
->transformations
& PNG_16_TO_8
) && (info_ptr
->bit_depth
== 16))
1260 info_ptr
->bit_depth
= 8;
1263 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1264 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
1265 info_ptr
->color_type
|= PNG_COLOR_MASK_COLOR
;
1268 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1269 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1270 info_ptr
->color_type
&= ~PNG_COLOR_MASK_COLOR
;
1273 #ifdef PNG_READ_DITHER_SUPPORTED
1274 if (png_ptr
->transformations
& PNG_DITHER
)
1276 if (((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
1277 (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)) &&
1278 png_ptr
->palette_lookup
&& info_ptr
->bit_depth
== 8)
1280 info_ptr
->color_type
= PNG_COLOR_TYPE_PALETTE
;
1285 #ifdef PNG_READ_PACK_SUPPORTED
1286 if ((png_ptr
->transformations
& PNG_PACK
) && (info_ptr
->bit_depth
< 8))
1287 info_ptr
->bit_depth
= 8;
1290 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1291 info_ptr
->channels
= 1;
1292 else if (info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
1293 info_ptr
->channels
= 3;
1295 info_ptr
->channels
= 1;
1297 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1298 if (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
)
1299 info_ptr
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
1302 if (info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)
1303 info_ptr
->channels
++;
1305 #ifdef PNG_READ_FILLER_SUPPORTED
1306 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1307 if ((png_ptr
->transformations
& PNG_FILLER
) &&
1308 ((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
1309 (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)))
1311 info_ptr
->channels
++;
1312 /* If adding a true alpha channel not just filler */
1314 if (png_ptr
->transformations
& PNG_ADD_ALPHA
)
1315 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
1320 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1321 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1322 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
1324 if (info_ptr
->bit_depth
< png_ptr
->user_transform_depth
)
1325 info_ptr
->bit_depth
= png_ptr
->user_transform_depth
;
1326 if (info_ptr
->channels
< png_ptr
->user_transform_channels
)
1327 info_ptr
->channels
= png_ptr
->user_transform_channels
;
1331 info_ptr
->pixel_depth
= (png_byte
)(info_ptr
->channels
*
1332 info_ptr
->bit_depth
);
1334 info_ptr
->rowbytes
= PNG_ROWBYTES(info_ptr
->pixel_depth
, info_ptr
->width
);
1336 #ifndef PNG_READ_EXPAND_SUPPORTED
1342 /* Transform the row. The order of transformations is significant,
1343 * and is very touchy. If you add a transformation, take care to
1344 * decide how it fits in with the other transformations here.
1347 png_do_read_transformations(png_structp png_ptr
)
1349 png_debug(1, "in png_do_read_transformations");
1351 if (png_ptr
->row_buf
== NULL
)
1353 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1356 png_snprintf2(msg
, 50,
1357 "NULL row buffer for row %ld, pass %d", (long)png_ptr
->row_number
,
1359 png_error(png_ptr
, msg
);
1361 png_error(png_ptr
, "NULL row buffer");
1364 #ifdef PNG_WARN_UNINITIALIZED_ROW
1365 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
1366 /* Application has failed to call either png_read_start_image()
1367 * or png_read_update_info() after setting transforms that expand
1368 * pixels. This check added to libpng-1.2.19
1370 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1371 png_error(png_ptr
, "Uninitialized row");
1373 png_warning(png_ptr
, "Uninitialized row");
1377 #ifdef PNG_READ_EXPAND_SUPPORTED
1378 if (png_ptr
->transformations
& PNG_EXPAND
)
1380 if (png_ptr
->row_info
.color_type
== PNG_COLOR_TYPE_PALETTE
)
1382 png_do_expand_palette(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1383 png_ptr
->palette
, png_ptr
->trans
, png_ptr
->num_trans
);
1387 if (png_ptr
->num_trans
&&
1388 (png_ptr
->transformations
& PNG_EXPAND_tRNS
))
1389 png_do_expand(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1390 &(png_ptr
->trans_values
));
1392 png_do_expand(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1398 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1399 if (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
)
1400 png_do_strip_filler(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1401 PNG_FLAG_FILLER_AFTER
| (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
));
1404 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1405 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1408 png_do_rgb_to_gray(png_ptr
, &(png_ptr
->row_info
),
1409 png_ptr
->row_buf
+ 1);
1412 png_ptr
->rgb_to_gray_status
=1;
1413 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
1414 PNG_RGB_TO_GRAY_WARN
)
1415 png_warning(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
1416 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
1417 PNG_RGB_TO_GRAY_ERR
)
1418 png_error(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
1423 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1425 * In most cases, the "simple transparency" should be done prior to doing
1426 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1427 * pixel is transparent. You would also need to make sure that the
1428 * transparency information is upgraded to RGB.
1430 * To summarize, the current flow is:
1431 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1432 * with background "in place" if transparent,
1433 * convert to RGB if necessary
1434 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1435 * convert to RGB if necessary
1437 * To support RGB backgrounds for gray images we need:
1438 * - Gray + simple transparency -> convert to RGB + simple transparency,
1439 * compare 3 or 6 bytes and composite with
1440 * background "in place" if transparent
1441 * (3x compare/pixel compared to doing
1442 * composite with gray bkgrnd)
1443 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1444 * remove alpha bytes (3x float
1445 * operations/pixel compared with composite
1446 * on gray background)
1448 * Greg's change will do this. The reason it wasn't done before is for
1449 * performance, as this increases the per-pixel operations. If we would check
1450 * in advance if the background was gray or RGB, and position the gray-to-RGB
1451 * transform appropriately, then it would save a lot of work/time.
1454 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1455 /* If gray -> RGB, do so now only if background is non-gray; else do later
1456 * for performance reasons
1458 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
1459 !(png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
1460 png_do_gray_to_rgb(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1463 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1464 if ((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1465 ((png_ptr
->num_trans
!= 0 ) ||
1466 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)))
1467 png_do_background(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1468 &(png_ptr
->trans_values
), &(png_ptr
->background
)
1469 #ifdef PNG_READ_GAMMA_SUPPORTED
1470 , &(png_ptr
->background_1
),
1471 png_ptr
->gamma_table
, png_ptr
->gamma_from_1
,
1472 png_ptr
->gamma_to_1
, png_ptr
->gamma_16_table
,
1473 png_ptr
->gamma_16_from_1
, png_ptr
->gamma_16_to_1
,
1474 png_ptr
->gamma_shift
1479 #ifdef PNG_READ_GAMMA_SUPPORTED
1480 if ((png_ptr
->transformations
& PNG_GAMMA
) &&
1481 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1482 !((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1483 ((png_ptr
->num_trans
!= 0) ||
1484 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
))) &&
1486 (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
))
1487 png_do_gamma(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1488 png_ptr
->gamma_table
, png_ptr
->gamma_16_table
,
1489 png_ptr
->gamma_shift
);
1492 #ifdef PNG_READ_16_TO_8_SUPPORTED
1493 if (png_ptr
->transformations
& PNG_16_TO_8
)
1494 png_do_chop(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1497 #ifdef PNG_READ_DITHER_SUPPORTED
1498 if (png_ptr
->transformations
& PNG_DITHER
)
1500 png_do_dither((png_row_infop
)&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1501 png_ptr
->palette_lookup
, png_ptr
->dither_index
);
1502 if (png_ptr
->row_info
.rowbytes
== (png_uint_32
)0)
1503 png_error(png_ptr
, "png_do_dither returned rowbytes=0");
1507 #ifdef PNG_READ_INVERT_SUPPORTED
1508 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
1509 png_do_invert(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1512 #ifdef PNG_READ_SHIFT_SUPPORTED
1513 if (png_ptr
->transformations
& PNG_SHIFT
)
1514 png_do_unshift(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1518 #ifdef PNG_READ_PACK_SUPPORTED
1519 if (png_ptr
->transformations
& PNG_PACK
)
1520 png_do_unpack(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1523 #ifdef PNG_READ_BGR_SUPPORTED
1524 if (png_ptr
->transformations
& PNG_BGR
)
1525 png_do_bgr(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1528 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1529 if (png_ptr
->transformations
& PNG_PACKSWAP
)
1530 png_do_packswap(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1533 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1534 /* If gray -> RGB, do so now only if we did not do so above */
1535 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
1536 (png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
1537 png_do_gray_to_rgb(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1540 #ifdef PNG_READ_FILLER_SUPPORTED
1541 if (png_ptr
->transformations
& PNG_FILLER
)
1542 png_do_read_filler(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1543 (png_uint_32
)png_ptr
->filler
, png_ptr
->flags
);
1546 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1547 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
1548 png_do_read_invert_alpha(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1551 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1552 if (png_ptr
->transformations
& PNG_SWAP_ALPHA
)
1553 png_do_read_swap_alpha(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1556 #ifdef PNG_READ_SWAP_SUPPORTED
1557 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
1558 png_do_swap(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1561 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1562 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
1564 if (png_ptr
->read_user_transform_fn
!= NULL
)
1565 (*(png_ptr
->read_user_transform_fn
)) /* User read transform function */
1566 (png_ptr
, /* png_ptr */
1567 &(png_ptr
->row_info
), /* row_info: */
1568 /* png_uint_32 width; width of row */
1569 /* png_uint_32 rowbytes; number of bytes in row */
1570 /* png_byte color_type; color type of pixels */
1571 /* png_byte bit_depth; bit depth of samples */
1572 /* png_byte channels; number of channels (1-4) */
1573 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1574 png_ptr
->row_buf
+ 1); /* start of pixel data for row */
1575 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1576 if (png_ptr
->user_transform_depth
)
1577 png_ptr
->row_info
.bit_depth
= png_ptr
->user_transform_depth
;
1578 if (png_ptr
->user_transform_channels
)
1579 png_ptr
->row_info
.channels
= png_ptr
->user_transform_channels
;
1581 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
1582 png_ptr
->row_info
.channels
);
1583 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
1584 png_ptr
->row_info
.width
);
1590 #ifdef PNG_READ_PACK_SUPPORTED
1591 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1592 * without changing the actual values. Thus, if you had a row with
1593 * a bit depth of 1, you would end up with bytes that only contained
1594 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1595 * png_do_shift() after this.
1598 png_do_unpack(png_row_infop row_info
, png_bytep row
)
1600 png_debug(1, "in png_do_unpack");
1602 #ifdef PNG_USELESS_TESTS_SUPPORTED
1603 if (row
!= NULL
&& row_info
!= NULL
&& row_info
->bit_depth
< 8)
1605 if (row_info
->bit_depth
< 8)
1609 png_uint_32 row_width
=row_info
->width
;
1611 switch (row_info
->bit_depth
)
1615 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
1616 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1617 png_uint_32 shift
= 7 - (int)((row_width
+ 7) & 0x07);
1618 for (i
= 0; i
< row_width
; i
++)
1620 *dp
= (png_byte
)((*sp
>> shift
) & 0x01);
1637 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
1638 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1639 png_uint_32 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
1640 for (i
= 0; i
< row_width
; i
++)
1642 *dp
= (png_byte
)((*sp
>> shift
) & 0x03);
1658 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
1659 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1660 png_uint_32 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
1661 for (i
= 0; i
< row_width
; i
++)
1663 *dp
= (png_byte
)((*sp
>> shift
) & 0x0f);
1677 row_info
->bit_depth
= 8;
1678 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
1679 row_info
->rowbytes
= row_width
* row_info
->channels
;
1684 #ifdef PNG_READ_SHIFT_SUPPORTED
1685 /* Reverse the effects of png_do_shift. This routine merely shifts the
1686 * pixels back to their significant bits values. Thus, if you have
1687 * a row of bit depth 8, but only 5 are significant, this will shift
1688 * the values back to 0 through 31.
1691 png_do_unshift(png_row_infop row_info
, png_bytep row
, png_color_8p sig_bits
)
1693 png_debug(1, "in png_do_unshift");
1696 #ifdef PNG_USELESS_TESTS_SUPPORTED
1697 row
!= NULL
&& row_info
!= NULL
&& sig_bits
!= NULL
&&
1699 row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
1704 png_uint_16 value
= 0;
1705 png_uint_32 row_width
= row_info
->width
;
1707 if (row_info
->color_type
& PNG_COLOR_MASK_COLOR
)
1709 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->red
;
1710 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->green
;
1711 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->blue
;
1715 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->gray
;
1717 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
1719 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->alpha
;
1722 for (c
= 0; c
< channels
; c
++)
1733 switch (row_info
->bit_depth
)
1739 png_uint_32 istop
= row_info
->rowbytes
;
1741 for (bp
= row
, i
= 0; i
< istop
; i
++)
1753 png_uint_32 istop
= row_info
->rowbytes
;
1754 png_byte mask
= (png_byte
)((((int)0xf0 >> shift
[0]) & (int)0xf0) |
1755 (png_byte
)((int)0xf >> shift
[0]));
1757 for (i
= 0; i
< istop
; i
++)
1769 png_uint_32 istop
= row_width
* channels
;
1771 for (i
= 0; i
< istop
; i
++)
1773 *bp
++ >>= shift
[i
%channels
];
1782 png_uint_32 istop
= channels
* row_width
;
1784 for (i
= 0; i
< istop
; i
++)
1786 value
= (png_uint_16
)((*bp
<< 8) + *(bp
+ 1));
1787 value
>>= shift
[i
%channels
];
1788 *bp
++ = (png_byte
)(value
>> 8);
1789 *bp
++ = (png_byte
)(value
& 0xff);
1798 #ifdef PNG_READ_16_TO_8_SUPPORTED
1799 /* Chop rows of bit depth 16 down to 8 */
1801 png_do_chop(png_row_infop row_info
, png_bytep row
)
1803 png_debug(1, "in png_do_chop");
1805 #ifdef PNG_USELESS_TESTS_SUPPORTED
1806 if (row
!= NULL
&& row_info
!= NULL
&& row_info
->bit_depth
== 16)
1808 if (row_info
->bit_depth
== 16)
1814 png_uint_32 istop
= row_info
->width
* row_info
->channels
;
1816 for (i
= 0; i
<istop
; i
++, sp
+= 2, dp
++)
1818 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1819 /* This does a more accurate scaling of the 16-bit color
1820 * value, rather than a simple low-byte truncation.
1822 * What the ideal calculation should be:
1823 * *dp = (((((png_uint_32)(*sp) << 8) |
1824 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1825 * / (png_uint_32)65535L;
1827 * GRR: no, I think this is what it really should be:
1828 * *dp = (((((png_uint_32)(*sp) << 8) |
1829 * (png_uint_32)(*(sp + 1))) + 128L)
1830 * / (png_uint_32)257L;
1832 * GRR: here's the exact calculation with shifts:
1833 * temp = (((png_uint_32)(*sp) << 8) |
1834 * (png_uint_32)(*(sp + 1))) + 128L;
1835 * *dp = (temp - (temp >> 8)) >> 8;
1837 * Approximate calculation with shift/add instead of multiply/divide:
1838 * *dp = ((((png_uint_32)(*sp) << 8) |
1839 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1841 * What we actually do to avoid extra shifting and conversion:
1844 *dp
= *sp
+ ((((int)(*(sp
+ 1)) - *sp
) > 128) ? 1 : 0);
1846 /* Simply discard the low order byte */
1850 row_info
->bit_depth
= 8;
1851 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
1852 row_info
->rowbytes
= row_info
->width
* row_info
->channels
;
1857 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1859 png_do_read_swap_alpha(png_row_infop row_info
, png_bytep row
)
1861 png_debug(1, "in png_do_read_swap_alpha");
1863 #ifdef PNG_USELESS_TESTS_SUPPORTED
1864 if (row
!= NULL
&& row_info
!= NULL
)
1867 png_uint_32 row_width
= row_info
->width
;
1868 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
1870 /* This converts from RGBA to ARGB */
1871 if (row_info
->bit_depth
== 8)
1873 png_bytep sp
= row
+ row_info
->rowbytes
;
1878 for (i
= 0; i
< row_width
; i
++)
1887 /* This converts from RRGGBBAA to AARRGGBB */
1890 png_bytep sp
= row
+ row_info
->rowbytes
;
1895 for (i
= 0; i
< row_width
; i
++)
1910 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
1912 /* This converts from GA to AG */
1913 if (row_info
->bit_depth
== 8)
1915 png_bytep sp
= row
+ row_info
->rowbytes
;
1920 for (i
= 0; i
< row_width
; i
++)
1927 /* This converts from GGAA to AAGG */
1930 png_bytep sp
= row
+ row_info
->rowbytes
;
1935 for (i
= 0; i
< row_width
; i
++)
1950 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1952 png_do_read_invert_alpha(png_row_infop row_info
, png_bytep row
)
1954 png_debug(1, "in png_do_read_invert_alpha");
1956 #ifdef PNG_USELESS_TESTS_SUPPORTED
1957 if (row
!= NULL
&& row_info
!= NULL
)
1960 png_uint_32 row_width
= row_info
->width
;
1961 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
1963 /* This inverts the alpha channel in RGBA */
1964 if (row_info
->bit_depth
== 8)
1966 png_bytep sp
= row
+ row_info
->rowbytes
;
1970 for (i
= 0; i
< row_width
; i
++)
1972 *(--dp
) = (png_byte
)(255 - *(--sp
));
1974 /* This does nothing:
1978 We can replace it with:
1984 /* This inverts the alpha channel in RRGGBBAA */
1987 png_bytep sp
= row
+ row_info
->rowbytes
;
1991 for (i
= 0; i
< row_width
; i
++)
1993 *(--dp
) = (png_byte
)(255 - *(--sp
));
1994 *(--dp
) = (png_byte
)(255 - *(--sp
));
1996 /* This does nothing:
2003 We can replace it with:
2010 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2012 /* This inverts the alpha channel in GA */
2013 if (row_info
->bit_depth
== 8)
2015 png_bytep sp
= row
+ row_info
->rowbytes
;
2019 for (i
= 0; i
< row_width
; i
++)
2021 *(--dp
) = (png_byte
)(255 - *(--sp
));
2025 /* This inverts the alpha channel in GGAA */
2028 png_bytep sp
= row
+ row_info
->rowbytes
;
2032 for (i
= 0; i
< row_width
; i
++)
2034 *(--dp
) = (png_byte
)(255 - *(--sp
));
2035 *(--dp
) = (png_byte
)(255 - *(--sp
));
2049 #ifdef PNG_READ_FILLER_SUPPORTED
2050 /* Add filler channel if we have RGB color */
2052 png_do_read_filler(png_row_infop row_info
, png_bytep row
,
2053 png_uint_32 filler
, png_uint_32 flags
)
2056 png_uint_32 row_width
= row_info
->width
;
2058 png_byte hi_filler
= (png_byte
)((filler
>>8) & 0xff);
2059 png_byte lo_filler
= (png_byte
)(filler
& 0xff);
2061 png_debug(1, "in png_do_read_filler");
2064 #ifdef PNG_USELESS_TESTS_SUPPORTED
2065 row
!= NULL
&& row_info
!= NULL
&&
2067 row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
2069 if (row_info
->bit_depth
== 8)
2071 /* This changes the data from G to GX */
2072 if (flags
& PNG_FLAG_FILLER_AFTER
)
2074 png_bytep sp
= row
+ (png_size_t
)row_width
;
2075 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2076 for (i
= 1; i
< row_width
; i
++)
2078 *(--dp
) = lo_filler
;
2081 *(--dp
) = lo_filler
;
2082 row_info
->channels
= 2;
2083 row_info
->pixel_depth
= 16;
2084 row_info
->rowbytes
= row_width
* 2;
2086 /* This changes the data from G to XG */
2089 png_bytep sp
= row
+ (png_size_t
)row_width
;
2090 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2091 for (i
= 0; i
< row_width
; i
++)
2094 *(--dp
) = lo_filler
;
2096 row_info
->channels
= 2;
2097 row_info
->pixel_depth
= 16;
2098 row_info
->rowbytes
= row_width
* 2;
2101 else if (row_info
->bit_depth
== 16)
2103 /* This changes the data from GG to GGXX */
2104 if (flags
& PNG_FLAG_FILLER_AFTER
)
2106 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
2107 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2108 for (i
= 1; i
< row_width
; i
++)
2110 *(--dp
) = hi_filler
;
2111 *(--dp
) = lo_filler
;
2115 *(--dp
) = hi_filler
;
2116 *(--dp
) = lo_filler
;
2117 row_info
->channels
= 2;
2118 row_info
->pixel_depth
= 32;
2119 row_info
->rowbytes
= row_width
* 4;
2121 /* This changes the data from GG to XXGG */
2124 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
2125 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2126 for (i
= 0; i
< row_width
; i
++)
2130 *(--dp
) = hi_filler
;
2131 *(--dp
) = lo_filler
;
2133 row_info
->channels
= 2;
2134 row_info
->pixel_depth
= 32;
2135 row_info
->rowbytes
= row_width
* 4;
2138 } /* COLOR_TYPE == GRAY */
2139 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
2141 if (row_info
->bit_depth
== 8)
2143 /* This changes the data from RGB to RGBX */
2144 if (flags
& PNG_FLAG_FILLER_AFTER
)
2146 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
2147 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2148 for (i
= 1; i
< row_width
; i
++)
2150 *(--dp
) = lo_filler
;
2155 *(--dp
) = lo_filler
;
2156 row_info
->channels
= 4;
2157 row_info
->pixel_depth
= 32;
2158 row_info
->rowbytes
= row_width
* 4;
2160 /* This changes the data from RGB to XRGB */
2163 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
2164 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2165 for (i
= 0; i
< row_width
; i
++)
2170 *(--dp
) = lo_filler
;
2172 row_info
->channels
= 4;
2173 row_info
->pixel_depth
= 32;
2174 row_info
->rowbytes
= row_width
* 4;
2177 else if (row_info
->bit_depth
== 16)
2179 /* This changes the data from RRGGBB to RRGGBBXX */
2180 if (flags
& PNG_FLAG_FILLER_AFTER
)
2182 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
2183 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2184 for (i
= 1; i
< row_width
; i
++)
2186 *(--dp
) = hi_filler
;
2187 *(--dp
) = lo_filler
;
2195 *(--dp
) = hi_filler
;
2196 *(--dp
) = lo_filler
;
2197 row_info
->channels
= 4;
2198 row_info
->pixel_depth
= 64;
2199 row_info
->rowbytes
= row_width
* 8;
2201 /* This changes the data from RRGGBB to XXRRGGBB */
2204 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
2205 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2206 for (i
= 0; i
< row_width
; i
++)
2214 *(--dp
) = hi_filler
;
2215 *(--dp
) = lo_filler
;
2217 row_info
->channels
= 4;
2218 row_info
->pixel_depth
= 64;
2219 row_info
->rowbytes
= row_width
* 8;
2222 } /* COLOR_TYPE == RGB */
2226 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2227 /* Expand grayscale files to RGB, with or without alpha */
2229 png_do_gray_to_rgb(png_row_infop row_info
, png_bytep row
)
2232 png_uint_32 row_width
= row_info
->width
;
2234 png_debug(1, "in png_do_gray_to_rgb");
2236 if (row_info
->bit_depth
>= 8 &&
2237 #ifdef PNG_USELESS_TESTS_SUPPORTED
2238 row
!= NULL
&& row_info
!= NULL
&&
2240 !(row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
2242 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
2244 if (row_info
->bit_depth
== 8)
2246 png_bytep sp
= row
+ (png_size_t
)row_width
- 1;
2247 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2248 for (i
= 0; i
< row_width
; i
++)
2257 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
2258 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
2259 for (i
= 0; i
< row_width
; i
++)
2262 *(dp
--) = *(sp
- 1);
2264 *(dp
--) = *(sp
- 1);
2270 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2272 if (row_info
->bit_depth
== 8)
2274 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
2275 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2276 for (i
= 0; i
< row_width
; i
++)
2286 png_bytep sp
= row
+ (png_size_t
)row_width
* 4 - 1;
2287 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
2288 for (i
= 0; i
< row_width
; i
++)
2293 *(dp
--) = *(sp
- 1);
2295 *(dp
--) = *(sp
- 1);
2301 row_info
->channels
+= (png_byte
)2;
2302 row_info
->color_type
|= PNG_COLOR_MASK_COLOR
;
2303 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
2304 row_info
->bit_depth
);
2305 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
2310 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2311 /* Reduce RGB files to grayscale, with or without alpha
2312 * using the equation given in Poynton's ColorFAQ at
2313 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2315 * <http://www.poynton.com/notes/colour_and_gamma/>
2316 * Charles Poynton poynton at poynton.com
2318 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2320 * We approximate this with
2322 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2324 * which can be expressed with integers as
2326 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2328 * The calculation is to be done in a linear colorspace.
2330 * Other integer coefficents can be used via png_set_rgb_to_gray().
2333 png_do_rgb_to_gray(png_structp png_ptr
, png_row_infop row_info
, png_bytep row
)
2338 png_uint_32 row_width
= row_info
->width
;
2341 png_debug(1, "in png_do_rgb_to_gray");
2344 #ifdef PNG_USELESS_TESTS_SUPPORTED
2345 row
!= NULL
&& row_info
!= NULL
&&
2347 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
2349 png_uint_32 rc
= png_ptr
->rgb_to_gray_red_coeff
;
2350 png_uint_32 gc
= png_ptr
->rgb_to_gray_green_coeff
;
2351 png_uint_32 bc
= png_ptr
->rgb_to_gray_blue_coeff
;
2353 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
2355 if (row_info
->bit_depth
== 8)
2357 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2358 if (png_ptr
->gamma_from_1
!= NULL
&& png_ptr
->gamma_to_1
!= NULL
)
2363 for (i
= 0; i
< row_width
; i
++)
2365 png_byte red
= png_ptr
->gamma_to_1
[*(sp
++)];
2366 png_byte green
= png_ptr
->gamma_to_1
[*(sp
++)];
2367 png_byte blue
= png_ptr
->gamma_to_1
[*(sp
++)];
2368 if (red
!= green
|| red
!= blue
)
2371 *(dp
++) = png_ptr
->gamma_from_1
[
2372 (rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2375 *(dp
++) = *(sp
- 1);
2383 for (i
= 0; i
< row_width
; i
++)
2385 png_byte red
= *(sp
++);
2386 png_byte green
= *(sp
++);
2387 png_byte blue
= *(sp
++);
2388 if (red
!= green
|| red
!= blue
)
2391 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2394 *(dp
++) = *(sp
- 1);
2399 else /* RGB bit_depth == 16 */
2401 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2402 if (png_ptr
->gamma_16_to_1
!= NULL
&&
2403 png_ptr
->gamma_16_from_1
!= NULL
)
2407 for (i
= 0; i
< row_width
; i
++)
2409 png_uint_16 red
, green
, blue
, w
;
2411 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2412 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2413 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2415 if (red
== green
&& red
== blue
)
2419 png_uint_16 red_1
= png_ptr
->gamma_16_to_1
[(red
&0xff) >>
2420 png_ptr
->gamma_shift
][red
>>8];
2421 png_uint_16 green_1
=
2422 png_ptr
->gamma_16_to_1
[(green
&0xff) >>
2423 png_ptr
->gamma_shift
][green
>>8];
2424 png_uint_16 blue_1
= png_ptr
->gamma_16_to_1
[(blue
&0xff) >>
2425 png_ptr
->gamma_shift
][blue
>>8];
2426 png_uint_16 gray16
= (png_uint_16
)((rc
*red_1
+ gc
*green_1
2428 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
2429 png_ptr
->gamma_shift
][gray16
>> 8];
2433 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2434 *(dp
++) = (png_byte
)(w
& 0xff);
2442 for (i
= 0; i
< row_width
; i
++)
2444 png_uint_16 red
, green
, blue
, gray16
;
2446 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2447 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2448 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2450 if (red
!= green
|| red
!= blue
)
2452 gray16
= (png_uint_16
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2453 *(dp
++) = (png_byte
)((gray16
>>8) & 0xff);
2454 *(dp
++) = (png_byte
)(gray16
& 0xff);
2459 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
2461 if (row_info
->bit_depth
== 8)
2463 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2464 if (png_ptr
->gamma_from_1
!= NULL
&& png_ptr
->gamma_to_1
!= NULL
)
2468 for (i
= 0; i
< row_width
; i
++)
2470 png_byte red
= png_ptr
->gamma_to_1
[*(sp
++)];
2471 png_byte green
= png_ptr
->gamma_to_1
[*(sp
++)];
2472 png_byte blue
= png_ptr
->gamma_to_1
[*(sp
++)];
2473 if (red
!= green
|| red
!= blue
)
2475 *(dp
++) = png_ptr
->gamma_from_1
2476 [(rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2477 *(dp
++) = *(sp
++); /* alpha */
2485 for (i
= 0; i
< row_width
; i
++)
2487 png_byte red
= *(sp
++);
2488 png_byte green
= *(sp
++);
2489 png_byte blue
= *(sp
++);
2490 if (red
!= green
|| red
!= blue
)
2492 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2493 *(dp
++) = *(sp
++); /* alpha */
2497 else /* RGBA bit_depth == 16 */
2499 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2500 if (png_ptr
->gamma_16_to_1
!= NULL
&&
2501 png_ptr
->gamma_16_from_1
!= NULL
)
2505 for (i
= 0; i
< row_width
; i
++)
2507 png_uint_16 red
, green
, blue
, w
;
2509 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2510 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2511 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2513 if (red
== green
&& red
== blue
)
2517 png_uint_16 red_1
= png_ptr
->gamma_16_to_1
[(red
&0xff) >>
2518 png_ptr
->gamma_shift
][red
>>8];
2519 png_uint_16 green_1
=
2520 png_ptr
->gamma_16_to_1
[(green
&0xff) >>
2521 png_ptr
->gamma_shift
][green
>>8];
2522 png_uint_16 blue_1
= png_ptr
->gamma_16_to_1
[(blue
&0xff) >>
2523 png_ptr
->gamma_shift
][blue
>>8];
2524 png_uint_16 gray16
= (png_uint_16
)((rc
* red_1
2525 + gc
* green_1
+ bc
* blue_1
)>>15);
2526 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
2527 png_ptr
->gamma_shift
][gray16
>> 8];
2531 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2532 *(dp
++) = (png_byte
)(w
& 0xff);
2533 *(dp
++) = *(sp
++); /* alpha */
2542 for (i
= 0; i
< row_width
; i
++)
2544 png_uint_16 red
, green
, blue
, gray16
;
2545 red
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2546 green
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2547 blue
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2548 if (red
!= green
|| red
!= blue
)
2550 gray16
= (png_uint_16
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2551 *(dp
++) = (png_byte
)((gray16
>>8) & 0xff);
2552 *(dp
++) = (png_byte
)(gray16
& 0xff);
2553 *(dp
++) = *(sp
++); /* alpha */
2559 row_info
->channels
-= (png_byte
)2;
2560 row_info
->color_type
&= ~PNG_COLOR_MASK_COLOR
;
2561 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
2562 row_info
->bit_depth
);
2563 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
2569 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2570 * large of png_color. This lets grayscale images be treated as
2571 * paletted. Most useful for gamma correction and simplification
2575 png_build_grayscale_palette(int bit_depth
, png_colorp palette
)
2582 png_debug(1, "in png_do_build_grayscale_palette");
2584 if (palette
== NULL
)
2615 for (i
= 0, v
= 0; i
< num_palette
; i
++, v
+= color_inc
)
2617 palette
[i
].red
= (png_byte
)v
;
2618 palette
[i
].green
= (png_byte
)v
;
2619 palette
[i
].blue
= (png_byte
)v
;
2623 /* This function is currently unused. Do we really need it? */
2624 #if defined(PNG_READ_DITHER_SUPPORTED) && \
2625 defined(PNG_CORRECT_PALETTE_SUPPORTED)
2627 png_correct_palette(png_structp png_ptr
, png_colorp palette
,
2630 png_debug(1, "in png_correct_palette");
2632 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
2633 defined(PNG_READ_GAMMA_SUPPORTED) && \
2634 defined(PNG_FLOATING_POINT_SUPPORTED)
2635 if (png_ptr
->transformations
& (PNG_GAMMA
| PNG_BACKGROUND
))
2637 png_color back
, back_1
;
2639 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_FILE
)
2641 back
.red
= png_ptr
->gamma_table
[png_ptr
->background
.red
];
2642 back
.green
= png_ptr
->gamma_table
[png_ptr
->background
.green
];
2643 back
.blue
= png_ptr
->gamma_table
[png_ptr
->background
.blue
];
2645 back_1
.red
= png_ptr
->gamma_to_1
[png_ptr
->background
.red
];
2646 back_1
.green
= png_ptr
->gamma_to_1
[png_ptr
->background
.green
];
2647 back_1
.blue
= png_ptr
->gamma_to_1
[png_ptr
->background
.blue
];
2653 g
= 1.0 / (png_ptr
->background_gamma
* png_ptr
->screen_gamma
);
2655 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_SCREEN
2656 || fabs(g
- 1.0) < PNG_GAMMA_THRESHOLD
)
2658 back
.red
= png_ptr
->background
.red
;
2659 back
.green
= png_ptr
->background
.green
;
2660 back
.blue
= png_ptr
->background
.blue
;
2665 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2668 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2671 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2675 g
= 1.0 / png_ptr
->background_gamma
;
2678 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2681 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2684 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2688 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2692 for (i
= 0; i
< (png_uint_32
)num_palette
; i
++)
2694 if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] == 0)
2698 else if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] != 0xff)
2702 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].red
];
2703 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.red
);
2704 palette
[i
].red
= png_ptr
->gamma_from_1
[w
];
2706 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].green
];
2707 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.green
);
2708 palette
[i
].green
= png_ptr
->gamma_from_1
[w
];
2710 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].blue
];
2711 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.blue
);
2712 palette
[i
].blue
= png_ptr
->gamma_from_1
[w
];
2716 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2717 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2718 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2726 for (i
= 0; i
< num_palette
; i
++)
2728 if (palette
[i
].red
== (png_byte
)png_ptr
->trans_values
.gray
)
2734 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2735 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2736 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2743 #ifdef PNG_READ_GAMMA_SUPPORTED
2744 if (png_ptr
->transformations
& PNG_GAMMA
)
2748 for (i
= 0; i
< num_palette
; i
++)
2750 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2751 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2752 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2755 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2759 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2760 if (png_ptr
->transformations
& PNG_BACKGROUND
)
2762 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2766 back
.red
= (png_byte
)png_ptr
->background
.red
;
2767 back
.green
= (png_byte
)png_ptr
->background
.green
;
2768 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
2770 for (i
= 0; i
< (int)png_ptr
->num_trans
; i
++)
2772 if (png_ptr
->trans
[i
] == 0)
2774 palette
[i
].red
= back
.red
;
2775 palette
[i
].green
= back
.green
;
2776 palette
[i
].blue
= back
.blue
;
2778 else if (png_ptr
->trans
[i
] != 0xff)
2780 png_composite(palette
[i
].red
, png_ptr
->palette
[i
].red
,
2781 png_ptr
->trans
[i
], back
.red
);
2782 png_composite(palette
[i
].green
, png_ptr
->palette
[i
].green
,
2783 png_ptr
->trans
[i
], back
.green
);
2784 png_composite(palette
[i
].blue
, png_ptr
->palette
[i
].blue
,
2785 png_ptr
->trans
[i
], back
.blue
);
2789 else /* Assume grayscale palette (what else could it be?) */
2793 for (i
= 0; i
< num_palette
; i
++)
2795 if (i
== (png_byte
)png_ptr
->trans_values
.gray
)
2797 palette
[i
].red
= (png_byte
)png_ptr
->background
.red
;
2798 palette
[i
].green
= (png_byte
)png_ptr
->background
.green
;
2799 palette
[i
].blue
= (png_byte
)png_ptr
->background
.blue
;
2808 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2809 /* Replace any alpha or transparency with the supplied background color.
2810 * "background" is already in the screen gamma, while "background_1" is
2811 * at a gamma of 1.0. Paletted files have already been taken care of.
2814 png_do_background(png_row_infop row_info
, png_bytep row
,
2815 png_color_16p trans_values
, png_color_16p background
2816 #ifdef PNG_READ_GAMMA_SUPPORTED
2817 , png_color_16p background_1
,
2818 png_bytep gamma_table
, png_bytep gamma_from_1
, png_bytep gamma_to_1
,
2819 png_uint_16pp gamma_16
, png_uint_16pp gamma_16_from_1
,
2820 png_uint_16pp gamma_16_to_1
, int gamma_shift
2826 png_uint_32 row_width
=row_info
->width
;
2829 png_debug(1, "in png_do_background");
2831 if (background
!= NULL
&&
2832 #ifdef PNG_USELESS_TESTS_SUPPORTED
2833 row
!= NULL
&& row_info
!= NULL
&&
2835 (!(row_info
->color_type
& PNG_COLOR_MASK_ALPHA
) ||
2836 (row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
&& trans_values
)))
2838 switch (row_info
->color_type
)
2840 case PNG_COLOR_TYPE_GRAY
:
2842 switch (row_info
->bit_depth
)
2848 for (i
= 0; i
< row_width
; i
++)
2850 if ((png_uint_16
)((*sp
>> shift
) & 0x01)
2851 == trans_values
->gray
)
2853 *sp
&= (png_byte
)((0x7f7f >> (7 - shift
)) & 0xff);
2854 *sp
|= (png_byte
)(background
->gray
<< shift
);
2869 #ifdef PNG_READ_GAMMA_SUPPORTED
2870 if (gamma_table
!= NULL
)
2874 for (i
= 0; i
< row_width
; i
++)
2876 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
2877 == trans_values
->gray
)
2879 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2880 *sp
|= (png_byte
)(background
->gray
<< shift
);
2884 png_byte p
= (png_byte
)((*sp
>> shift
) & 0x03);
2885 png_byte g
= (png_byte
)((gamma_table
[p
| (p
<< 2) |
2886 (p
<< 4) | (p
<< 6)] >> 6) & 0x03);
2887 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2888 *sp
|= (png_byte
)(g
<< shift
);
2904 for (i
= 0; i
< row_width
; i
++)
2906 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
2907 == trans_values
->gray
)
2909 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2910 *sp
|= (png_byte
)(background
->gray
<< shift
);
2926 #ifdef PNG_READ_GAMMA_SUPPORTED
2927 if (gamma_table
!= NULL
)
2931 for (i
= 0; i
< row_width
; i
++)
2933 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
2934 == trans_values
->gray
)
2936 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2937 *sp
|= (png_byte
)(background
->gray
<< shift
);
2941 png_byte p
= (png_byte
)((*sp
>> shift
) & 0x0f);
2942 png_byte g
= (png_byte
)((gamma_table
[p
|
2943 (p
<< 4)] >> 4) & 0x0f);
2944 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2945 *sp
|= (png_byte
)(g
<< shift
);
2961 for (i
= 0; i
< row_width
; i
++)
2963 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
2964 == trans_values
->gray
)
2966 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2967 *sp
|= (png_byte
)(background
->gray
<< shift
);
2983 #ifdef PNG_READ_GAMMA_SUPPORTED
2984 if (gamma_table
!= NULL
)
2987 for (i
= 0; i
< row_width
; i
++, sp
++)
2989 if (*sp
== trans_values
->gray
)
2991 *sp
= (png_byte
)background
->gray
;
2995 *sp
= gamma_table
[*sp
];
3003 for (i
= 0; i
< row_width
; i
++, sp
++)
3005 if (*sp
== trans_values
->gray
)
3007 *sp
= (png_byte
)background
->gray
;
3016 #ifdef PNG_READ_GAMMA_SUPPORTED
3017 if (gamma_16
!= NULL
)
3020 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3024 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3025 if (v
== trans_values
->gray
)
3027 /* Background is already in screen gamma */
3028 *sp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3029 *(sp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3033 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3034 *sp
= (png_byte
)((v
>> 8) & 0xff);
3035 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3043 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3047 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3048 if (v
== trans_values
->gray
)
3050 *sp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3051 *(sp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3061 case PNG_COLOR_TYPE_RGB
:
3063 if (row_info
->bit_depth
== 8)
3065 #ifdef PNG_READ_GAMMA_SUPPORTED
3066 if (gamma_table
!= NULL
)
3069 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3071 if (*sp
== trans_values
->red
&&
3072 *(sp
+ 1) == trans_values
->green
&&
3073 *(sp
+ 2) == trans_values
->blue
)
3075 *sp
= (png_byte
)background
->red
;
3076 *(sp
+ 1) = (png_byte
)background
->green
;
3077 *(sp
+ 2) = (png_byte
)background
->blue
;
3081 *sp
= gamma_table
[*sp
];
3082 *(sp
+ 1) = gamma_table
[*(sp
+ 1)];
3083 *(sp
+ 2) = gamma_table
[*(sp
+ 2)];
3091 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3093 if (*sp
== trans_values
->red
&&
3094 *(sp
+ 1) == trans_values
->green
&&
3095 *(sp
+ 2) == trans_values
->blue
)
3097 *sp
= (png_byte
)background
->red
;
3098 *(sp
+ 1) = (png_byte
)background
->green
;
3099 *(sp
+ 2) = (png_byte
)background
->blue
;
3104 else /* if (row_info->bit_depth == 16) */
3106 #ifdef PNG_READ_GAMMA_SUPPORTED
3107 if (gamma_16
!= NULL
)
3110 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3112 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3113 png_uint_16 g
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3114 png_uint_16 b
= (png_uint_16
)(((*(sp
+4)) << 8) + *(sp
+5));
3115 if (r
== trans_values
->red
&& g
== trans_values
->green
&&
3116 b
== trans_values
->blue
)
3118 /* Background is already in screen gamma */
3119 *sp
= (png_byte
)((background
->red
>> 8) & 0xff);
3120 *(sp
+ 1) = (png_byte
)(background
->red
& 0xff);
3121 *(sp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3122 *(sp
+ 3) = (png_byte
)(background
->green
& 0xff);
3123 *(sp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3124 *(sp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3128 png_uint_16 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3129 *sp
= (png_byte
)((v
>> 8) & 0xff);
3130 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3131 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3132 *(sp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3133 *(sp
+ 3) = (png_byte
)(v
& 0xff);
3134 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3135 *(sp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3136 *(sp
+ 5) = (png_byte
)(v
& 0xff);
3144 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3146 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+1));
3147 png_uint_16 g
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3148 png_uint_16 b
= (png_uint_16
)(((*(sp
+4)) << 8) + *(sp
+5));
3150 if (r
== trans_values
->red
&& g
== trans_values
->green
&&
3151 b
== trans_values
->blue
)
3153 *sp
= (png_byte
)((background
->red
>> 8) & 0xff);
3154 *(sp
+ 1) = (png_byte
)(background
->red
& 0xff);
3155 *(sp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3156 *(sp
+ 3) = (png_byte
)(background
->green
& 0xff);
3157 *(sp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3158 *(sp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3166 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3168 if (row_info
->bit_depth
== 8)
3170 #ifdef PNG_READ_GAMMA_SUPPORTED
3171 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
3172 gamma_table
!= NULL
)
3176 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3178 png_uint_16 a
= *(sp
+ 1);
3182 *dp
= gamma_table
[*sp
];
3186 /* Background is already in screen gamma */
3187 *dp
= (png_byte
)background
->gray
;
3193 v
= gamma_to_1
[*sp
];
3194 png_composite(w
, v
, a
, background_1
->gray
);
3195 *dp
= gamma_from_1
[w
];
3204 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3206 png_byte a
= *(sp
+ 1);
3212 #ifdef PNG_READ_GAMMA_SUPPORTED
3215 *dp
= (png_byte
)background
->gray
;
3219 png_composite(*dp
, *sp
, a
, background_1
->gray
);
3222 *dp
= (png_byte
)background
->gray
;
3227 else /* if (png_ptr->bit_depth == 16) */
3229 #ifdef PNG_READ_GAMMA_SUPPORTED
3230 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
3231 gamma_16_to_1
!= NULL
)
3235 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 2)
3237 png_uint_16 a
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3239 if (a
== (png_uint_16
)0xffff)
3243 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3244 *dp
= (png_byte
)((v
>> 8) & 0xff);
3245 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3247 #ifdef PNG_READ_GAMMA_SUPPORTED
3253 /* Background is already in screen gamma */
3254 *dp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3255 *(dp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3257 #ifdef PNG_READ_GAMMA_SUPPORTED
3260 png_uint_16 g
, v
, w
;
3262 g
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
3263 png_composite_16(v
, g
, a
, background_1
->gray
);
3264 w
= gamma_16_from_1
[(v
&0xff) >> gamma_shift
][v
>> 8];
3265 *dp
= (png_byte
)((w
>> 8) & 0xff);
3266 *(dp
+ 1) = (png_byte
)(w
& 0xff);
3276 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 2)
3278 png_uint_16 a
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3279 if (a
== (png_uint_16
)0xffff)
3281 png_memcpy(dp
, sp
, 2);
3283 #ifdef PNG_READ_GAMMA_SUPPORTED
3289 *dp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3290 *(dp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3292 #ifdef PNG_READ_GAMMA_SUPPORTED
3297 g
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3298 png_composite_16(v
, g
, a
, background_1
->gray
);
3299 *dp
= (png_byte
)((v
>> 8) & 0xff);
3300 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3309 case PNG_COLOR_TYPE_RGB_ALPHA
:
3311 if (row_info
->bit_depth
== 8)
3313 #ifdef PNG_READ_GAMMA_SUPPORTED
3314 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
3315 gamma_table
!= NULL
)
3319 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3321 png_byte a
= *(sp
+ 3);
3325 *dp
= gamma_table
[*sp
];
3326 *(dp
+ 1) = gamma_table
[*(sp
+ 1)];
3327 *(dp
+ 2) = gamma_table
[*(sp
+ 2)];
3331 /* Background is already in screen gamma */
3332 *dp
= (png_byte
)background
->red
;
3333 *(dp
+ 1) = (png_byte
)background
->green
;
3334 *(dp
+ 2) = (png_byte
)background
->blue
;
3340 v
= gamma_to_1
[*sp
];
3341 png_composite(w
, v
, a
, background_1
->red
);
3342 *dp
= gamma_from_1
[w
];
3343 v
= gamma_to_1
[*(sp
+ 1)];
3344 png_composite(w
, v
, a
, background_1
->green
);
3345 *(dp
+ 1) = gamma_from_1
[w
];
3346 v
= gamma_to_1
[*(sp
+ 2)];
3347 png_composite(w
, v
, a
, background_1
->blue
);
3348 *(dp
+ 2) = gamma_from_1
[w
];
3357 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3359 png_byte a
= *(sp
+ 3);
3364 *(dp
+ 1) = *(sp
+ 1);
3365 *(dp
+ 2) = *(sp
+ 2);
3369 *dp
= (png_byte
)background
->red
;
3370 *(dp
+ 1) = (png_byte
)background
->green
;
3371 *(dp
+ 2) = (png_byte
)background
->blue
;
3375 png_composite(*dp
, *sp
, a
, background
->red
);
3376 png_composite(*(dp
+ 1), *(sp
+ 1), a
,
3378 png_composite(*(dp
+ 2), *(sp
+ 2), a
,
3384 else /* if (row_info->bit_depth == 16) */
3386 #ifdef PNG_READ_GAMMA_SUPPORTED
3387 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
3388 gamma_16_to_1
!= NULL
)
3392 for (i
= 0; i
< row_width
; i
++, sp
+= 8, dp
+= 6)
3394 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
3395 << 8) + (png_uint_16
)(*(sp
+ 7)));
3396 if (a
== (png_uint_16
)0xffff)
3400 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3401 *dp
= (png_byte
)((v
>> 8) & 0xff);
3402 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3403 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3404 *(dp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3405 *(dp
+ 3) = (png_byte
)(v
& 0xff);
3406 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3407 *(dp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3408 *(dp
+ 5) = (png_byte
)(v
& 0xff);
3412 /* Background is already in screen gamma */
3413 *dp
= (png_byte
)((background
->red
>> 8) & 0xff);
3414 *(dp
+ 1) = (png_byte
)(background
->red
& 0xff);
3415 *(dp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3416 *(dp
+ 3) = (png_byte
)(background
->green
& 0xff);
3417 *(dp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3418 *(dp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3422 png_uint_16 v
, w
, x
;
3424 v
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
3425 png_composite_16(w
, v
, a
, background_1
->red
);
3426 x
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>> 8];
3427 *dp
= (png_byte
)((x
>> 8) & 0xff);
3428 *(dp
+ 1) = (png_byte
)(x
& 0xff);
3429 v
= gamma_16_to_1
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3430 png_composite_16(w
, v
, a
, background_1
->green
);
3431 x
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>> 8];
3432 *(dp
+ 2) = (png_byte
)((x
>> 8) & 0xff);
3433 *(dp
+ 3) = (png_byte
)(x
& 0xff);
3434 v
= gamma_16_to_1
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3435 png_composite_16(w
, v
, a
, background_1
->blue
);
3436 x
= gamma_16_from_1
[(w
& 0xff) >> gamma_shift
][w
>> 8];
3437 *(dp
+ 4) = (png_byte
)((x
>> 8) & 0xff);
3438 *(dp
+ 5) = (png_byte
)(x
& 0xff);
3447 for (i
= 0; i
< row_width
; i
++, sp
+= 8, dp
+= 6)
3449 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
3450 << 8) + (png_uint_16
)(*(sp
+ 7)));
3451 if (a
== (png_uint_16
)0xffff)
3453 png_memcpy(dp
, sp
, 6);
3457 *dp
= (png_byte
)((background
->red
>> 8) & 0xff);
3458 *(dp
+ 1) = (png_byte
)(background
->red
& 0xff);
3459 *(dp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3460 *(dp
+ 3) = (png_byte
)(background
->green
& 0xff);
3461 *(dp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3462 *(dp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3468 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3469 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
3471 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
3474 png_composite_16(v
, r
, a
, background
->red
);
3475 *dp
= (png_byte
)((v
>> 8) & 0xff);
3476 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3477 png_composite_16(v
, g
, a
, background
->green
);
3478 *(dp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3479 *(dp
+ 3) = (png_byte
)(v
& 0xff);
3480 png_composite_16(v
, b
, a
, background
->blue
);
3481 *(dp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3482 *(dp
+ 5) = (png_byte
)(v
& 0xff);
3491 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
3493 row_info
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
3494 row_info
->channels
--;
3495 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
3496 row_info
->bit_depth
);
3497 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
3503 #ifdef PNG_READ_GAMMA_SUPPORTED
3504 /* Gamma correct the image, avoiding the alpha channel. Make sure
3505 * you do this after you deal with the transparency issue on grayscale
3506 * or RGB images. If your bit depth is 8, use gamma_table, if it
3507 * is 16, use gamma_16_table and gamma_shift. Build these with
3508 * build_gamma_table().
3511 png_do_gamma(png_row_infop row_info
, png_bytep row
,
3512 png_bytep gamma_table
, png_uint_16pp gamma_16_table
,
3517 png_uint_32 row_width
=row_info
->width
;
3519 png_debug(1, "in png_do_gamma");
3522 #ifdef PNG_USELESS_TESTS_SUPPORTED
3523 row
!= NULL
&& row_info
!= NULL
&&
3525 ((row_info
->bit_depth
<= 8 && gamma_table
!= NULL
) ||
3526 (row_info
->bit_depth
== 16 && gamma_16_table
!= NULL
)))
3528 switch (row_info
->color_type
)
3530 case PNG_COLOR_TYPE_RGB
:
3532 if (row_info
->bit_depth
== 8)
3535 for (i
= 0; i
< row_width
; i
++)
3537 *sp
= gamma_table
[*sp
];
3539 *sp
= gamma_table
[*sp
];
3541 *sp
= gamma_table
[*sp
];
3545 else /* if (row_info->bit_depth == 16) */
3548 for (i
= 0; i
< row_width
; i
++)
3552 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3553 *sp
= (png_byte
)((v
>> 8) & 0xff);
3554 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3556 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3557 *sp
= (png_byte
)((v
>> 8) & 0xff);
3558 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3560 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3561 *sp
= (png_byte
)((v
>> 8) & 0xff);
3562 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3569 case PNG_COLOR_TYPE_RGB_ALPHA
:
3571 if (row_info
->bit_depth
== 8)
3574 for (i
= 0; i
< row_width
; i
++)
3576 *sp
= gamma_table
[*sp
];
3578 *sp
= gamma_table
[*sp
];
3580 *sp
= gamma_table
[*sp
];
3585 else /* if (row_info->bit_depth == 16) */
3588 for (i
= 0; i
< row_width
; i
++)
3590 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3591 *sp
= (png_byte
)((v
>> 8) & 0xff);
3592 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3594 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3595 *sp
= (png_byte
)((v
>> 8) & 0xff);
3596 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3598 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3599 *sp
= (png_byte
)((v
>> 8) & 0xff);
3600 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3607 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3609 if (row_info
->bit_depth
== 8)
3612 for (i
= 0; i
< row_width
; i
++)
3614 *sp
= gamma_table
[*sp
];
3618 else /* if (row_info->bit_depth == 16) */
3621 for (i
= 0; i
< row_width
; i
++)
3623 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3624 *sp
= (png_byte
)((v
>> 8) & 0xff);
3625 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3632 case PNG_COLOR_TYPE_GRAY
:
3634 if (row_info
->bit_depth
== 2)
3637 for (i
= 0; i
< row_width
; i
+= 4)
3645 ((((int)gamma_table
[a
|(a
>>2)|(a
>>4)|(a
>>6)]) ) & 0xc0)|
3646 ((((int)gamma_table
[(b
<<2)|b
|(b
>>2)|(b
>>4)])>>2) & 0x30)|
3647 ((((int)gamma_table
[(c
<<4)|(c
<<2)|c
|(c
>>2)])>>4) & 0x0c)|
3648 ((((int)gamma_table
[(d
<<6)|(d
<<4)|(d
<<2)|d
])>>6) ));
3653 if (row_info
->bit_depth
== 4)
3656 for (i
= 0; i
< row_width
; i
+= 2)
3658 int msb
= *sp
& 0xf0;
3659 int lsb
= *sp
& 0x0f;
3661 *sp
= (png_byte
)((((int)gamma_table
[msb
| (msb
>> 4)]) & 0xf0)
3662 | (((int)gamma_table
[(lsb
<< 4) | lsb
]) >> 4));
3667 else if (row_info
->bit_depth
== 8)
3670 for (i
= 0; i
< row_width
; i
++)
3672 *sp
= gamma_table
[*sp
];
3677 else if (row_info
->bit_depth
== 16)
3680 for (i
= 0; i
< row_width
; i
++)
3682 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3683 *sp
= (png_byte
)((v
>> 8) & 0xff);
3684 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3695 #ifdef PNG_READ_EXPAND_SUPPORTED
3696 /* Expands a palette row to an RGB or RGBA row depending
3697 * upon whether you supply trans and num_trans.
3700 png_do_expand_palette(png_row_infop row_info
, png_bytep row
,
3701 png_colorp palette
, png_bytep trans
, int num_trans
)
3706 png_uint_32 row_width
=row_info
->width
;
3708 png_debug(1, "in png_do_expand_palette");
3711 #ifdef PNG_USELESS_TESTS_SUPPORTED
3712 row
!= NULL
&& row_info
!= NULL
&&
3714 row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
)
3716 if (row_info
->bit_depth
< 8)
3718 switch (row_info
->bit_depth
)
3722 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
3723 dp
= row
+ (png_size_t
)row_width
- 1;
3724 shift
= 7 - (int)((row_width
+ 7) & 0x07);
3725 for (i
= 0; i
< row_width
; i
++)
3727 if ((*sp
>> shift
) & 0x01)
3746 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
3747 dp
= row
+ (png_size_t
)row_width
- 1;
3748 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
3749 for (i
= 0; i
< row_width
; i
++)
3751 value
= (*sp
>> shift
) & 0x03;
3752 *dp
= (png_byte
)value
;
3768 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
3769 dp
= row
+ (png_size_t
)row_width
- 1;
3770 shift
= (int)((row_width
& 0x01) << 2);
3771 for (i
= 0; i
< row_width
; i
++)
3773 value
= (*sp
>> shift
) & 0x0f;
3774 *dp
= (png_byte
)value
;
3788 row_info
->bit_depth
= 8;
3789 row_info
->pixel_depth
= 8;
3790 row_info
->rowbytes
= row_width
;
3792 switch (row_info
->bit_depth
)
3798 sp
= row
+ (png_size_t
)row_width
- 1;
3799 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
3801 for (i
= 0; i
< row_width
; i
++)
3803 if ((int)(*sp
) >= num_trans
)
3807 *dp
-- = palette
[*sp
].blue
;
3808 *dp
-- = palette
[*sp
].green
;
3809 *dp
-- = palette
[*sp
].red
;
3812 row_info
->bit_depth
= 8;
3813 row_info
->pixel_depth
= 32;
3814 row_info
->rowbytes
= row_width
* 4;
3815 row_info
->color_type
= 6;
3816 row_info
->channels
= 4;
3820 sp
= row
+ (png_size_t
)row_width
- 1;
3821 dp
= row
+ (png_size_t
)(row_width
* 3) - 1;
3823 for (i
= 0; i
< row_width
; i
++)
3825 *dp
-- = palette
[*sp
].blue
;
3826 *dp
-- = palette
[*sp
].green
;
3827 *dp
-- = palette
[*sp
].red
;
3831 row_info
->bit_depth
= 8;
3832 row_info
->pixel_depth
= 24;
3833 row_info
->rowbytes
= row_width
* 3;
3834 row_info
->color_type
= 2;
3835 row_info
->channels
= 3;
3843 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3844 * expanded transparency value is supplied, an alpha channel is built.
3847 png_do_expand(png_row_infop row_info
, png_bytep row
,
3848 png_color_16p trans_value
)
3853 png_uint_32 row_width
=row_info
->width
;
3855 png_debug(1, "in png_do_expand");
3857 #ifdef PNG_USELESS_TESTS_SUPPORTED
3858 if (row
!= NULL
&& row_info
!= NULL
)
3861 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
3863 png_uint_16 gray
= (png_uint_16
)(trans_value
? trans_value
->gray
: 0);
3865 if (row_info
->bit_depth
< 8)
3867 switch (row_info
->bit_depth
)
3871 gray
= (png_uint_16
)((gray
&0x01)*0xff);
3872 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
3873 dp
= row
+ (png_size_t
)row_width
- 1;
3874 shift
= 7 - (int)((row_width
+ 7) & 0x07);
3875 for (i
= 0; i
< row_width
; i
++)
3877 if ((*sp
>> shift
) & 0x01)
3896 gray
= (png_uint_16
)((gray
&0x03)*0x55);
3897 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
3898 dp
= row
+ (png_size_t
)row_width
- 1;
3899 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
3900 for (i
= 0; i
< row_width
; i
++)
3902 value
= (*sp
>> shift
) & 0x03;
3903 *dp
= (png_byte
)(value
| (value
<< 2) | (value
<< 4) |
3920 gray
= (png_uint_16
)((gray
&0x0f)*0x11);
3921 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
3922 dp
= row
+ (png_size_t
)row_width
- 1;
3923 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
3924 for (i
= 0; i
< row_width
; i
++)
3926 value
= (*sp
>> shift
) & 0x0f;
3927 *dp
= (png_byte
)(value
| (value
<< 4));
3942 row_info
->bit_depth
= 8;
3943 row_info
->pixel_depth
= 8;
3944 row_info
->rowbytes
= row_width
;
3947 if (trans_value
!= NULL
)
3949 if (row_info
->bit_depth
== 8)
3952 sp
= row
+ (png_size_t
)row_width
- 1;
3953 dp
= row
+ (png_size_t
)(row_width
<< 1) - 1;
3954 for (i
= 0; i
< row_width
; i
++)
3964 else if (row_info
->bit_depth
== 16)
3966 png_byte gray_high
= (gray
>> 8) & 0xff;
3967 png_byte gray_low
= gray
& 0xff;
3968 sp
= row
+ row_info
->rowbytes
- 1;
3969 dp
= row
+ (row_info
->rowbytes
<< 1) - 1;
3970 for (i
= 0; i
< row_width
; i
++)
3972 if (*(sp
- 1) == gray_high
&& *(sp
) == gray_low
)
3987 row_info
->color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
3988 row_info
->channels
= 2;
3989 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 1);
3990 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
3994 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&& trans_value
)
3996 if (row_info
->bit_depth
== 8)
3998 png_byte red
= trans_value
->red
& 0xff;
3999 png_byte green
= trans_value
->green
& 0xff;
4000 png_byte blue
= trans_value
->blue
& 0xff;
4001 sp
= row
+ (png_size_t
)row_info
->rowbytes
- 1;
4002 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
4003 for (i
= 0; i
< row_width
; i
++)
4005 if (*(sp
- 2) == red
&& *(sp
- 1) == green
&& *(sp
) == blue
)
4014 else if (row_info
->bit_depth
== 16)
4016 png_byte red_high
= (trans_value
->red
>> 8) & 0xff;
4017 png_byte green_high
= (trans_value
->green
>> 8) & 0xff;
4018 png_byte blue_high
= (trans_value
->blue
>> 8) & 0xff;
4019 png_byte red_low
= trans_value
->red
& 0xff;
4020 png_byte green_low
= trans_value
->green
& 0xff;
4021 png_byte blue_low
= trans_value
->blue
& 0xff;
4022 sp
= row
+ row_info
->rowbytes
- 1;
4023 dp
= row
+ (png_size_t
)(row_width
<< 3) - 1;
4024 for (i
= 0; i
< row_width
; i
++)
4026 if (*(sp
- 5) == red_high
&&
4027 *(sp
- 4) == red_low
&&
4028 *(sp
- 3) == green_high
&&
4029 *(sp
- 2) == green_low
&&
4030 *(sp
- 1) == blue_high
&&
4049 row_info
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
4050 row_info
->channels
= 4;
4051 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 2);
4052 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4058 #ifdef PNG_READ_DITHER_SUPPORTED
4060 png_do_dither(png_row_infop row_info
, png_bytep row
,
4061 png_bytep palette_lookup
, png_bytep dither_lookup
)
4065 png_uint_32 row_width
=row_info
->width
;
4067 png_debug(1, "in png_do_dither");
4069 #ifdef PNG_USELESS_TESTS_SUPPORTED
4070 if (row
!= NULL
&& row_info
!= NULL
)
4073 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&&
4074 palette_lookup
&& row_info
->bit_depth
== 8)
4079 for (i
= 0; i
< row_width
; i
++)
4085 /* This looks real messy, but the compiler will reduce
4086 * it down to a reasonable formula. For example, with
4087 * 5 bits per color, we get:
4088 * p = (((r >> 3) & 0x1f) << 10) |
4089 * (((g >> 3) & 0x1f) << 5) |
4090 * ((b >> 3) & 0x1f);
4092 p
= (((r
>> (8 - PNG_DITHER_RED_BITS
)) &
4093 ((1 << PNG_DITHER_RED_BITS
) - 1)) <<
4094 (PNG_DITHER_GREEN_BITS
+ PNG_DITHER_BLUE_BITS
)) |
4095 (((g
>> (8 - PNG_DITHER_GREEN_BITS
)) &
4096 ((1 << PNG_DITHER_GREEN_BITS
) - 1)) <<
4097 (PNG_DITHER_BLUE_BITS
)) |
4098 ((b
>> (8 - PNG_DITHER_BLUE_BITS
)) &
4099 ((1 << PNG_DITHER_BLUE_BITS
) - 1));
4101 *dp
++ = palette_lookup
[p
];
4103 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
4104 row_info
->channels
= 1;
4105 row_info
->pixel_depth
= row_info
->bit_depth
;
4106 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4108 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
4109 palette_lookup
!= NULL
&& row_info
->bit_depth
== 8)
4114 for (i
= 0; i
< row_width
; i
++)
4121 p
= (((r
>> (8 - PNG_DITHER_RED_BITS
)) &
4122 ((1 << PNG_DITHER_RED_BITS
) - 1)) <<
4123 (PNG_DITHER_GREEN_BITS
+ PNG_DITHER_BLUE_BITS
)) |
4124 (((g
>> (8 - PNG_DITHER_GREEN_BITS
)) &
4125 ((1 << PNG_DITHER_GREEN_BITS
) - 1)) <<
4126 (PNG_DITHER_BLUE_BITS
)) |
4127 ((b
>> (8 - PNG_DITHER_BLUE_BITS
)) &
4128 ((1 << PNG_DITHER_BLUE_BITS
) - 1));
4130 *dp
++ = palette_lookup
[p
];
4132 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
4133 row_info
->channels
= 1;
4134 row_info
->pixel_depth
= row_info
->bit_depth
;
4135 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4137 else if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
4138 dither_lookup
&& row_info
->bit_depth
== 8)
4141 for (i
= 0; i
< row_width
; i
++, sp
++)
4143 *sp
= dither_lookup
[*sp
];
4150 #ifdef PNG_FLOATING_POINT_SUPPORTED
4151 #ifdef PNG_READ_GAMMA_SUPPORTED
4152 static PNG_CONST
int png_gamma_shift
[] =
4153 {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
4155 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
4156 * tables, we don't make a full table if we are reducing to 8-bit in
4157 * the future. Note also how the gamma_16 tables are segmented so that
4158 * we don't need to allocate > 64K chunks for a full 16-bit table.
4160 * See the PNG extensions document for an integer algorithm for creating
4161 * the gamma tables. Maybe we will implement that here someday.
4163 * We should only reach this point if
4165 * the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
4166 * or the application has provided a file_gamma)
4170 * the screen_gamma is known
4173 * RGB_to_gray transformation is being performed
4178 * the screen_gamma is different from the reciprocal of the
4179 * file_gamma by more than the specified threshold
4183 * a background color has been specified and the file_gamma
4184 * and screen_gamma are not 1.0, within the specified threshold.
4189 png_build_gamma_table(png_structp png_ptr
)
4191 png_debug(1, "in png_build_gamma_table");
4193 if (png_ptr
->bit_depth
<= 8)
4198 if (png_ptr
->screen_gamma
> .000001)
4199 g
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
4204 png_ptr
->gamma_table
= (png_bytep
)png_malloc(png_ptr
,
4207 for (i
= 0; i
< 256; i
++)
4209 png_ptr
->gamma_table
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4213 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4214 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4215 if (png_ptr
->transformations
& ((PNG_BACKGROUND
) | PNG_RGB_TO_GRAY
))
4218 g
= 1.0 / (png_ptr
->gamma
);
4220 png_ptr
->gamma_to_1
= (png_bytep
)png_malloc(png_ptr
,
4223 for (i
= 0; i
< 256; i
++)
4225 png_ptr
->gamma_to_1
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4230 png_ptr
->gamma_from_1
= (png_bytep
)png_malloc(png_ptr
,
4233 if (png_ptr
->screen_gamma
> 0.000001)
4234 g
= 1.0 / png_ptr
->screen_gamma
;
4237 g
= png_ptr
->gamma
; /* Probably doing rgb_to_gray */
4239 for (i
= 0; i
< 256; i
++)
4241 png_ptr
->gamma_from_1
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4246 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4251 int i
, j
, shift
, num
;
4255 if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
4257 sig_bit
= (int)png_ptr
->sig_bit
.red
;
4259 if ((int)png_ptr
->sig_bit
.green
> sig_bit
)
4260 sig_bit
= png_ptr
->sig_bit
.green
;
4262 if ((int)png_ptr
->sig_bit
.blue
> sig_bit
)
4263 sig_bit
= png_ptr
->sig_bit
.blue
;
4267 sig_bit
= (int)png_ptr
->sig_bit
.gray
;
4271 shift
= 16 - sig_bit
;
4276 if (png_ptr
->transformations
& PNG_16_TO_8
)
4278 if (shift
< (16 - PNG_MAX_GAMMA_8
))
4279 shift
= (16 - PNG_MAX_GAMMA_8
);
4288 png_ptr
->gamma_shift
= (png_byte
)shift
;
4290 num
= (1 << (8 - shift
));
4292 if (png_ptr
->screen_gamma
> .000001)
4293 g
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
4297 png_ptr
->gamma_16_table
= (png_uint_16pp
)png_calloc(png_ptr
,
4298 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4300 if (png_ptr
->transformations
& (PNG_16_TO_8
| PNG_BACKGROUND
))
4303 png_uint_32 last
, max
;
4305 for (i
= 0; i
< num
; i
++)
4307 png_ptr
->gamma_16_table
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4308 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4313 for (i
= 0; i
< 256; i
++)
4315 fout
= ((double)i
+ 0.5) / 256.0;
4317 max
= (png_uint_32
)(fin
* (double)((png_uint_32
)num
<< 8));
4320 png_ptr
->gamma_16_table
[(int)(last
& (0xff >> shift
))]
4321 [(int)(last
>> (8 - shift
))] = (png_uint_16
)(
4322 (png_uint_16
)i
| ((png_uint_16
)i
<< 8));
4326 while (last
< ((png_uint_32
)num
<< 8))
4328 png_ptr
->gamma_16_table
[(int)(last
& (0xff >> shift
))]
4329 [(int)(last
>> (8 - shift
))] = (png_uint_16
)65535L;
4335 for (i
= 0; i
< num
; i
++)
4337 png_ptr
->gamma_16_table
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4338 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4340 ig
= (((png_uint_32
)i
* (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4342 for (j
= 0; j
< 256; j
++)
4344 png_ptr
->gamma_16_table
[i
][j
] =
4345 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4346 65535.0, g
) * 65535.0 + .5);
4351 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4352 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4353 if (png_ptr
->transformations
& (PNG_BACKGROUND
| PNG_RGB_TO_GRAY
))
4356 g
= 1.0 / (png_ptr
->gamma
);
4358 png_ptr
->gamma_16_to_1
= (png_uint_16pp
)png_calloc(png_ptr
,
4359 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4361 for (i
= 0; i
< num
; i
++)
4363 png_ptr
->gamma_16_to_1
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4364 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4366 ig
= (((png_uint_32
)i
*
4367 (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4368 for (j
= 0; j
< 256; j
++)
4370 png_ptr
->gamma_16_to_1
[i
][j
] =
4371 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4372 65535.0, g
) * 65535.0 + .5);
4376 if (png_ptr
->screen_gamma
> 0.000001)
4377 g
= 1.0 / png_ptr
->screen_gamma
;
4380 g
= png_ptr
->gamma
; /* Probably doing rgb_to_gray */
4382 png_ptr
->gamma_16_from_1
= (png_uint_16pp
)png_calloc(png_ptr
,
4383 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4385 for (i
= 0; i
< num
; i
++)
4387 png_ptr
->gamma_16_from_1
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4388 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4390 ig
= (((png_uint_32
)i
*
4391 (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4393 for (j
= 0; j
< 256; j
++)
4395 png_ptr
->gamma_16_from_1
[i
][j
] =
4396 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4397 65535.0, g
) * 65535.0 + .5);
4401 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4405 /* To do: install integer version of png_build_gamma_table here */
4408 #ifdef PNG_MNG_FEATURES_SUPPORTED
4409 /* Undoes intrapixel differencing */
4411 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
4413 png_debug(1, "in png_do_read_intrapixel");
4416 #ifdef PNG_USELESS_TESTS_SUPPORTED
4417 row
!= NULL
&& row_info
!= NULL
&&
4419 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
4421 int bytes_per_pixel
;
4422 png_uint_32 row_width
= row_info
->width
;
4423 if (row_info
->bit_depth
== 8)
4428 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
4429 bytes_per_pixel
= 3;
4431 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4432 bytes_per_pixel
= 4;
4437 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
4439 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+1))&0xff);
4440 *(rp
+2) = (png_byte
)((256 + *(rp
+2) + *(rp
+1))&0xff);
4443 else if (row_info
->bit_depth
== 16)
4448 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
4449 bytes_per_pixel
= 6;
4451 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4452 bytes_per_pixel
= 8;
4457 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
4459 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
4460 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
4461 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
4462 png_uint_32 red
= (png_uint_32
)((s0
+ s1
+ 65536L) & 0xffffL
);
4463 png_uint_32 blue
= (png_uint_32
)((s2
+ s1
+ 65536L) & 0xffffL
);
4464 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
4465 *(rp
+1) = (png_byte
)(red
& 0xff);
4466 *(rp
+4) = (png_byte
)((blue
>> 8) & 0xff);
4467 *(rp
+5) = (png_byte
)(blue
& 0xff);
4472 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4473 #endif /* PNG_READ_SUPPORTED */