2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.2.45 [July 7, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
13 * This file contains functions optionally called by an application
14 * in order to tell libpng how to handle data when reading a PNG.
15 * Transformations that are used in both reading and writing are
20 #define PNG_NO_PEDANTIC_WARNINGS
22 #ifdef PNG_READ_SUPPORTED
24 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
26 png_set_crc_action(png_structp png_ptr
, int crit_action
, int ancil_action
)
28 png_debug(1, "in png_set_crc_action");
33 /* Tell libpng how we react to CRC errors in critical chunks */
36 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
39 case PNG_CRC_WARN_USE
: /* Warn/use data */
40 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
41 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
;
44 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
45 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
46 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
|
47 PNG_FLAG_CRC_CRITICAL_IGNORE
;
50 case PNG_CRC_WARN_DISCARD
: /* Not a valid action for critical data */
52 "Can't discard critical data on CRC error.");
53 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
57 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
61 /* Tell libpng how we react to CRC errors in ancillary chunks */
64 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
67 case PNG_CRC_WARN_USE
: /* Warn/use data */
68 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
69 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
;
72 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
73 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
74 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
|
75 PNG_FLAG_CRC_ANCILLARY_NOWARN
;
78 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
79 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
80 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_NOWARN
;
83 case PNG_CRC_WARN_DISCARD
: /* Warn/discard data */
87 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
92 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
93 defined(PNG_FLOATING_POINT_SUPPORTED)
94 /* Handle alpha and tRNS via a background color */
96 png_set_background(png_structp png_ptr
,
97 png_color_16p background_color
, int background_gamma_code
,
98 int need_expand
, double background_gamma
)
100 png_debug(1, "in png_set_background");
104 if (background_gamma_code
== PNG_BACKGROUND_GAMMA_UNKNOWN
)
106 png_warning(png_ptr
, "Application must supply a known background gamma");
110 png_ptr
->transformations
|= PNG_BACKGROUND
;
111 png_memcpy(&(png_ptr
->background
), background_color
,
112 png_sizeof(png_color_16
));
113 png_ptr
->background_gamma
= (float)background_gamma
;
114 png_ptr
->background_gamma_type
= (png_byte
)(background_gamma_code
);
115 png_ptr
->transformations
|= (need_expand
? PNG_BACKGROUND_EXPAND
: 0);
119 #ifdef PNG_READ_16_TO_8_SUPPORTED
120 /* Strip 16 bit depth files to 8 bit depth */
122 png_set_strip_16(png_structp png_ptr
)
124 png_debug(1, "in png_set_strip_16");
128 png_ptr
->transformations
|= PNG_16_TO_8
;
132 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
134 png_set_strip_alpha(png_structp png_ptr
)
136 png_debug(1, "in png_set_strip_alpha");
140 png_ptr
->flags
|= PNG_FLAG_STRIP_ALPHA
;
144 #ifdef PNG_READ_DITHER_SUPPORTED
145 /* Dither file to 8 bit. Supply a palette, the current number
146 * of elements in the palette, the maximum number of elements
147 * allowed, and a histogram if possible. If the current number
148 * of colors is greater then the maximum number, the palette will be
149 * modified to fit in the maximum number. "full_dither" indicates
150 * whether we need a dithering cube set up for RGB images, or if we
151 * simply are reducing the number of colors in a paletted image.
154 typedef struct png_dsort_struct
156 struct png_dsort_struct FAR
* next
;
160 typedef png_dsort FAR
* png_dsortp
;
161 typedef png_dsort FAR
* FAR
* png_dsortpp
;
164 png_set_dither(png_structp png_ptr
, png_colorp palette
,
165 int num_palette
, int maximum_colors
, png_uint_16p histogram
,
168 png_debug(1, "in png_set_dither");
172 png_ptr
->transformations
|= PNG_DITHER
;
178 png_ptr
->dither_index
= (png_bytep
)png_malloc(png_ptr
,
179 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
180 for (i
= 0; i
< num_palette
; i
++)
181 png_ptr
->dither_index
[i
] = (png_byte
)i
;
184 if (num_palette
> maximum_colors
)
186 if (histogram
!= NULL
)
188 /* This is easy enough, just throw out the least used colors.
189 * Perhaps not the best solution, but good enough.
194 /* Initialize an array to sort colors */
195 png_ptr
->dither_sort
= (png_bytep
)png_malloc(png_ptr
,
196 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
198 /* Initialize the dither_sort array */
199 for (i
= 0; i
< num_palette
; i
++)
200 png_ptr
->dither_sort
[i
] = (png_byte
)i
;
202 /* Find the least used palette entries by starting a
203 * bubble sort, and running it until we have sorted
204 * out enough colors. Note that we don't care about
205 * sorting all the colors, just finding which are
209 for (i
= num_palette
- 1; i
>= maximum_colors
; i
--)
211 int done
; /* To stop early if the list is pre-sorted */
215 for (j
= 0; j
< i
; j
++)
217 if (histogram
[png_ptr
->dither_sort
[j
]]
218 < histogram
[png_ptr
->dither_sort
[j
+ 1]])
222 t
= png_ptr
->dither_sort
[j
];
223 png_ptr
->dither_sort
[j
] = png_ptr
->dither_sort
[j
+ 1];
224 png_ptr
->dither_sort
[j
+ 1] = t
;
232 /* Swap the palette around, and set up a table, if necessary */
237 /* Put all the useful colors within the max, but don't
240 for (i
= 0; i
< maximum_colors
; i
++)
242 if ((int)png_ptr
->dither_sort
[i
] >= maximum_colors
)
246 while ((int)png_ptr
->dither_sort
[j
] >= maximum_colors
);
247 palette
[i
] = palette
[j
];
255 /* Move all the used colors inside the max limit, and
256 * develop a translation table.
258 for (i
= 0; i
< maximum_colors
; i
++)
260 /* Only move the colors we need to */
261 if ((int)png_ptr
->dither_sort
[i
] >= maximum_colors
)
267 while ((int)png_ptr
->dither_sort
[j
] >= maximum_colors
);
269 tmp_color
= palette
[j
];
270 palette
[j
] = palette
[i
];
271 palette
[i
] = tmp_color
;
272 /* Indicate where the color went */
273 png_ptr
->dither_index
[j
] = (png_byte
)i
;
274 png_ptr
->dither_index
[i
] = (png_byte
)j
;
278 /* Find closest color for those colors we are not using */
279 for (i
= 0; i
< num_palette
; i
++)
281 if ((int)png_ptr
->dither_index
[i
] >= maximum_colors
)
283 int min_d
, k
, min_k
, d_index
;
285 /* Find the closest color to one we threw out */
286 d_index
= png_ptr
->dither_index
[i
];
287 min_d
= PNG_COLOR_DIST(palette
[d_index
], palette
[0]);
288 for (k
= 1, min_k
= 0; k
< maximum_colors
; k
++)
292 d
= PNG_COLOR_DIST(palette
[d_index
], palette
[k
]);
300 /* Point to closest color */
301 png_ptr
->dither_index
[i
] = (png_byte
)min_k
;
305 png_free(png_ptr
, png_ptr
->dither_sort
);
306 png_ptr
->dither_sort
= NULL
;
310 /* This is much harder to do simply (and quickly). Perhaps
311 * we need to go through a median cut routine, but those
312 * don't always behave themselves with only a few colors
313 * as input. So we will just find the closest two colors,
314 * and throw out one of them (chosen somewhat randomly).
315 * [We don't understand this at all, so if someone wants to
316 * work on improving it, be our guest - AED, GRP]
326 /* Initialize palette index arrays */
327 png_ptr
->index_to_palette
= (png_bytep
)png_malloc(png_ptr
,
328 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
329 png_ptr
->palette_to_index
= (png_bytep
)png_malloc(png_ptr
,
330 (png_uint_32
)(num_palette
* png_sizeof(png_byte
)));
332 /* Initialize the sort array */
333 for (i
= 0; i
< num_palette
; i
++)
335 png_ptr
->index_to_palette
[i
] = (png_byte
)i
;
336 png_ptr
->palette_to_index
[i
] = (png_byte
)i
;
339 hash
= (png_dsortpp
)png_calloc(png_ptr
, (png_uint_32
)(769 *
340 png_sizeof(png_dsortp
)));
342 num_new_palette
= num_palette
;
344 /* Initial wild guess at how far apart the farthest pixel
345 * pair we will be eliminating will be. Larger
346 * numbers mean more areas will be allocated, Smaller
347 * numbers run the risk of not saving enough data, and
348 * having to do this all over again.
350 * I have not done extensive checking on this number.
354 while (num_new_palette
> maximum_colors
)
356 for (i
= 0; i
< num_new_palette
- 1; i
++)
360 for (j
= i
+ 1; j
< num_new_palette
; j
++)
364 d
= PNG_COLOR_DIST(palette
[i
], palette
[j
]);
369 t
= (png_dsortp
)png_malloc_warn(png_ptr
,
370 (png_uint_32
)(png_sizeof(png_dsort
)));
374 t
->left
= (png_byte
)i
;
375 t
->right
= (png_byte
)j
;
384 for (i
= 0; i
<= max_d
; i
++)
390 for (p
= hash
[i
]; p
; p
= p
->next
)
392 if ((int)png_ptr
->index_to_palette
[p
->left
]
394 (int)png_ptr
->index_to_palette
[p
->right
]
399 if (num_new_palette
& 0x01)
411 palette
[png_ptr
->index_to_palette
[j
]]
412 = palette
[num_new_palette
];
417 for (k
= 0; k
< num_palette
; k
++)
419 if (png_ptr
->dither_index
[k
] ==
420 png_ptr
->index_to_palette
[j
])
421 png_ptr
->dither_index
[k
] =
422 png_ptr
->index_to_palette
[next_j
];
423 if ((int)png_ptr
->dither_index
[k
] ==
425 png_ptr
->dither_index
[k
] =
426 png_ptr
->index_to_palette
[j
];
430 png_ptr
->index_to_palette
[png_ptr
->palette_to_index
431 [num_new_palette
]] = png_ptr
->index_to_palette
[j
];
432 png_ptr
->palette_to_index
[png_ptr
->index_to_palette
[j
]]
433 = png_ptr
->palette_to_index
[num_new_palette
];
435 png_ptr
->index_to_palette
[j
] =
436 (png_byte
)num_new_palette
;
437 png_ptr
->palette_to_index
[num_new_palette
] =
440 if (num_new_palette
<= maximum_colors
)
443 if (num_new_palette
<= maximum_colors
)
448 for (i
= 0; i
< 769; i
++)
452 png_dsortp p
= hash
[i
];
456 png_free(png_ptr
, p
);
464 png_free(png_ptr
, hash
);
465 png_free(png_ptr
, png_ptr
->palette_to_index
);
466 png_free(png_ptr
, png_ptr
->index_to_palette
);
467 png_ptr
->palette_to_index
= NULL
;
468 png_ptr
->index_to_palette
= NULL
;
470 num_palette
= maximum_colors
;
472 if (png_ptr
->palette
== NULL
)
474 png_ptr
->palette
= palette
;
476 png_ptr
->num_palette
= (png_uint_16
)num_palette
;
482 int total_bits
= PNG_DITHER_RED_BITS
+ PNG_DITHER_GREEN_BITS
+
483 PNG_DITHER_BLUE_BITS
;
484 int num_red
= (1 << PNG_DITHER_RED_BITS
);
485 int num_green
= (1 << PNG_DITHER_GREEN_BITS
);
486 int num_blue
= (1 << PNG_DITHER_BLUE_BITS
);
487 png_size_t num_entries
= ((png_size_t
)1 << total_bits
);
489 png_ptr
->palette_lookup
= (png_bytep
)png_calloc(png_ptr
,
490 (png_uint_32
)(num_entries
* png_sizeof(png_byte
)));
492 distance
= (png_bytep
)png_malloc(png_ptr
, (png_uint_32
)(num_entries
*
493 png_sizeof(png_byte
)));
494 png_memset(distance
, 0xff, num_entries
* png_sizeof(png_byte
));
496 for (i
= 0; i
< num_palette
; i
++)
499 int r
= (palette
[i
].red
>> (8 - PNG_DITHER_RED_BITS
));
500 int g
= (palette
[i
].green
>> (8 - PNG_DITHER_GREEN_BITS
));
501 int b
= (palette
[i
].blue
>> (8 - PNG_DITHER_BLUE_BITS
));
503 for (ir
= 0; ir
< num_red
; ir
++)
505 /* int dr = abs(ir - r); */
506 int dr
= ((ir
> r
) ? ir
- r
: r
- ir
);
507 int index_r
= (ir
<< (PNG_DITHER_BLUE_BITS
+
508 PNG_DITHER_GREEN_BITS
));
510 for (ig
= 0; ig
< num_green
; ig
++)
512 /* int dg = abs(ig - g); */
513 int dg
= ((ig
> g
) ? ig
- g
: g
- ig
);
515 int dm
= ((dr
> dg
) ? dr
: dg
);
516 int index_g
= index_r
| (ig
<< PNG_DITHER_BLUE_BITS
);
518 for (ib
= 0; ib
< num_blue
; ib
++)
520 int d_index
= index_g
| ib
;
521 /* int db = abs(ib - b); */
522 int db
= ((ib
> b
) ? ib
- b
: b
- ib
);
523 int dmax
= ((dm
> db
) ? dm
: db
);
524 int d
= dmax
+ dt
+ db
;
526 if (d
< (int)distance
[d_index
])
528 distance
[d_index
] = (png_byte
)d
;
529 png_ptr
->palette_lookup
[d_index
] = (png_byte
)i
;
536 png_free(png_ptr
, distance
);
541 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
542 /* Transform the image from the file_gamma to the screen_gamma. We
543 * only do transformations on images where the file_gamma and screen_gamma
544 * are not close reciprocals, otherwise it slows things down slightly, and
545 * also needlessly introduces small errors.
547 * We will turn off gamma transformation later if no semitransparent entries
548 * are present in the tRNS array for palette images. We can't do it here
549 * because we don't necessarily have the tRNS chunk yet.
552 png_set_gamma(png_structp png_ptr
, double scrn_gamma
, double file_gamma
)
554 png_debug(1, "in png_set_gamma");
559 if ((fabs(scrn_gamma
* file_gamma
- 1.0) > PNG_GAMMA_THRESHOLD
) ||
560 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) ||
561 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
))
562 png_ptr
->transformations
|= PNG_GAMMA
;
563 png_ptr
->gamma
= (float)file_gamma
;
564 png_ptr
->screen_gamma
= (float)scrn_gamma
;
568 #ifdef PNG_READ_EXPAND_SUPPORTED
569 /* Expand paletted images to RGB, expand grayscale images of
570 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
574 png_set_expand(png_structp png_ptr
)
576 png_debug(1, "in png_set_expand");
581 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
582 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
585 /* GRR 19990627: the following three functions currently are identical
586 * to png_set_expand(). However, it is entirely reasonable that someone
587 * might wish to expand an indexed image to RGB but *not* expand a single,
588 * fully transparent palette entry to a full alpha channel--perhaps instead
589 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
590 * the transparent color with a particular RGB value, or drop tRNS entirely.
591 * IOW, a future version of the library may make the transformations flag
592 * a bit more fine-grained, with separate bits for each of these three
595 * More to the point, these functions make it obvious what libpng will be
596 * doing, whereas "expand" can (and does) mean any number of things.
598 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
599 * to expand only the sample depth but not to expand the tRNS to alpha
600 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
603 /* Expand paletted images to RGB. */
605 png_set_palette_to_rgb(png_structp png_ptr
)
607 png_debug(1, "in png_set_palette_to_rgb");
612 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
613 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
617 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
619 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr
)
621 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
626 png_ptr
->transformations
|= PNG_EXPAND
;
627 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
631 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
632 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
633 /* Deprecated as of libpng-1.2.9 */
635 png_set_gray_1_2_4_to_8(png_structp png_ptr
)
637 png_debug(1, "in png_set_gray_1_2_4_to_8");
642 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
647 /* Expand tRNS chunks to alpha channels. */
649 png_set_tRNS_to_alpha(png_structp png_ptr
)
651 png_debug(1, "in png_set_tRNS_to_alpha");
653 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
654 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
656 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
658 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
660 png_set_gray_to_rgb(png_structp png_ptr
)
662 png_debug(1, "in png_set_gray_to_rgb");
664 png_ptr
->transformations
|= PNG_GRAY_TO_RGB
;
665 png_ptr
->flags
&= ~PNG_FLAG_ROW_INIT
;
669 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
670 #ifdef PNG_FLOATING_POINT_SUPPORTED
671 /* Convert a RGB image to a grayscale of the same width. This allows us,
672 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
676 png_set_rgb_to_gray(png_structp png_ptr
, int error_action
, double red
,
679 int red_fixed
, green_fixed
;
682 if (red
> 21474.83647 || red
< -21474.83648 ||
683 green
> 21474.83647 || green
< -21474.83648)
685 png_warning(png_ptr
, "ignoring out of range rgb_to_gray coefficients");
691 red_fixed
= (int)((float)red
*100000.0 + 0.5);
692 green_fixed
= (int)((float)green
*100000.0 + 0.5);
694 png_set_rgb_to_gray_fixed(png_ptr
, error_action
, red_fixed
, green_fixed
);
699 png_set_rgb_to_gray_fixed(png_structp png_ptr
, int error_action
,
700 png_fixed_point red
, png_fixed_point green
)
702 png_debug(1, "in png_set_rgb_to_gray");
709 case 1: png_ptr
->transformations
|= PNG_RGB_TO_GRAY
;
712 case 2: png_ptr
->transformations
|= PNG_RGB_TO_GRAY_WARN
;
715 case 3: png_ptr
->transformations
|= PNG_RGB_TO_GRAY_ERR
;
717 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
718 #ifdef PNG_READ_EXPAND_SUPPORTED
719 png_ptr
->transformations
|= PNG_EXPAND
;
723 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
724 png_ptr
->transformations
&= ~PNG_RGB_TO_GRAY
;
728 png_uint_16 red_int
, green_int
;
729 if (red
< 0 || green
< 0)
731 red_int
= 6968; /* .212671 * 32768 + .5 */
732 green_int
= 23434; /* .715160 * 32768 + .5 */
734 else if (red
+ green
< 100000L)
736 red_int
= (png_uint_16
)(((png_uint_32
)red
*32768L)/100000L);
737 green_int
= (png_uint_16
)(((png_uint_32
)green
*32768L)/100000L);
741 png_warning(png_ptr
, "ignoring out of range rgb_to_gray coefficients");
745 png_ptr
->rgb_to_gray_red_coeff
= red_int
;
746 png_ptr
->rgb_to_gray_green_coeff
= green_int
;
747 png_ptr
->rgb_to_gray_blue_coeff
=
748 (png_uint_16
)(32768 - red_int
- green_int
);
753 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
754 defined(PNG_LEGACY_SUPPORTED) || \
755 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
757 png_set_read_user_transform_fn(png_structp png_ptr
, png_user_transform_ptr
758 read_user_transform_fn
)
760 png_debug(1, "in png_set_read_user_transform_fn");
765 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
766 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
767 png_ptr
->read_user_transform_fn
= read_user_transform_fn
;
769 #ifdef PNG_LEGACY_SUPPORTED
770 if (read_user_transform_fn
)
772 "This version of libpng does not support user transforms");
777 /* Initialize everything needed for the read. This includes modifying
781 png_init_read_transformations(png_structp png_ptr
)
783 png_debug(1, "in png_init_read_transformations");
785 #ifdef PNG_USELESS_TESTS_SUPPORTED
789 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
790 defined(PNG_READ_SHIFT_SUPPORTED) || \
791 defined(PNG_READ_GAMMA_SUPPORTED)
792 int color_type
= png_ptr
->color_type
;
795 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
797 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
798 /* Detect gray background and attempt to enable optimization
799 * for gray --> RGB case
801 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
802 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
803 * background color might actually be gray yet not be flagged as such.
804 * This is not a problem for the current code, which uses
805 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
806 * png_do_gray_to_rgb() transformation.
808 if ((png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
809 !(color_type
& PNG_COLOR_MASK_COLOR
))
811 png_ptr
->mode
|= PNG_BACKGROUND_IS_GRAY
;
812 } else if ((png_ptr
->transformations
& PNG_BACKGROUND
) &&
813 !(png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
814 (png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
815 png_ptr
->background
.red
== png_ptr
->background
.green
&&
816 png_ptr
->background
.red
== png_ptr
->background
.blue
)
818 png_ptr
->mode
|= PNG_BACKGROUND_IS_GRAY
;
819 png_ptr
->background
.gray
= png_ptr
->background
.red
;
823 if ((png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
824 (png_ptr
->transformations
& PNG_EXPAND
))
826 if (!(color_type
& PNG_COLOR_MASK_COLOR
)) /* i.e., GRAY or GRAY_ALPHA */
828 /* Expand background and tRNS chunks */
829 switch (png_ptr
->bit_depth
)
832 png_ptr
->background
.gray
*= (png_uint_16
)0xff;
833 png_ptr
->background
.red
= png_ptr
->background
.green
834 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
835 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
837 png_ptr
->trans_values
.gray
*= (png_uint_16
)0xff;
838 png_ptr
->trans_values
.red
= png_ptr
->trans_values
.green
839 = png_ptr
->trans_values
.blue
= png_ptr
->trans_values
.gray
;
844 png_ptr
->background
.gray
*= (png_uint_16
)0x55;
845 png_ptr
->background
.red
= png_ptr
->background
.green
846 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
847 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
849 png_ptr
->trans_values
.gray
*= (png_uint_16
)0x55;
850 png_ptr
->trans_values
.red
= png_ptr
->trans_values
.green
851 = png_ptr
->trans_values
.blue
= png_ptr
->trans_values
.gray
;
856 png_ptr
->background
.gray
*= (png_uint_16
)0x11;
857 png_ptr
->background
.red
= png_ptr
->background
.green
858 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
859 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
861 png_ptr
->trans_values
.gray
*= (png_uint_16
)0x11;
862 png_ptr
->trans_values
.red
= png_ptr
->trans_values
.green
863 = png_ptr
->trans_values
.blue
= png_ptr
->trans_values
.gray
;
870 png_ptr
->background
.red
= png_ptr
->background
.green
871 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
875 else if (color_type
== PNG_COLOR_TYPE_PALETTE
)
877 png_ptr
->background
.red
=
878 png_ptr
->palette
[png_ptr
->background
.index
].red
;
879 png_ptr
->background
.green
=
880 png_ptr
->palette
[png_ptr
->background
.index
].green
;
881 png_ptr
->background
.blue
=
882 png_ptr
->palette
[png_ptr
->background
.index
].blue
;
884 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
885 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
887 #ifdef PNG_READ_EXPAND_SUPPORTED
888 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
891 /* Invert the alpha channel (in tRNS) unless the pixels are
892 * going to be expanded, in which case leave it for later
895 istop
=(int)png_ptr
->num_trans
;
896 for (i
=0; i
<istop
; i
++)
897 png_ptr
->trans
[i
] = (png_byte
)(255 - png_ptr
->trans
[i
]);
906 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
907 png_ptr
->background_1
= png_ptr
->background
;
909 #if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
911 if ((color_type
== PNG_COLOR_TYPE_PALETTE
&& png_ptr
->num_trans
!= 0)
912 && (fabs(png_ptr
->screen_gamma
* png_ptr
->gamma
- 1.0)
913 < PNG_GAMMA_THRESHOLD
))
917 for (i
=0; i
<png_ptr
->num_trans
; i
++)
919 if (png_ptr
->trans
[i
] != 0 && png_ptr
->trans
[i
] != 0xff)
920 k
=1; /* Partial transparency is present */
923 png_ptr
->transformations
&= ~PNG_GAMMA
;
926 if ((png_ptr
->transformations
& (PNG_GAMMA
| PNG_RGB_TO_GRAY
)) &&
927 png_ptr
->gamma
!= 0.0)
929 png_build_gamma_table(png_ptr
);
931 #ifdef PNG_READ_BACKGROUND_SUPPORTED
932 if (png_ptr
->transformations
& PNG_BACKGROUND
)
934 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
936 /* Could skip if no transparency */
937 png_color back
, back_1
;
938 png_colorp palette
= png_ptr
->palette
;
939 int num_palette
= png_ptr
->num_palette
;
941 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_FILE
)
943 back
.red
= png_ptr
->gamma_table
[png_ptr
->background
.red
];
944 back
.green
= png_ptr
->gamma_table
[png_ptr
->background
.green
];
945 back
.blue
= png_ptr
->gamma_table
[png_ptr
->background
.blue
];
947 back_1
.red
= png_ptr
->gamma_to_1
[png_ptr
->background
.red
];
948 back_1
.green
= png_ptr
->gamma_to_1
[png_ptr
->background
.green
];
949 back_1
.blue
= png_ptr
->gamma_to_1
[png_ptr
->background
.blue
];
955 switch (png_ptr
->background_gamma_type
)
957 case PNG_BACKGROUND_GAMMA_SCREEN
:
958 g
= (png_ptr
->screen_gamma
);
962 case PNG_BACKGROUND_GAMMA_FILE
:
963 g
= 1.0 / (png_ptr
->gamma
);
964 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
967 case PNG_BACKGROUND_GAMMA_UNIQUE
:
968 g
= 1.0 / (png_ptr
->background_gamma
);
969 gs
= 1.0 / (png_ptr
->background_gamma
*
970 png_ptr
->screen_gamma
);
973 g
= 1.0; /* back_1 */
977 if ( fabs(gs
- 1.0) < PNG_GAMMA_THRESHOLD
)
979 back
.red
= (png_byte
)png_ptr
->background
.red
;
980 back
.green
= (png_byte
)png_ptr
->background
.green
;
981 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
985 back
.red
= (png_byte
)(pow(
986 (double)png_ptr
->background
.red
/255, gs
) * 255.0 + .5);
987 back
.green
= (png_byte
)(pow(
988 (double)png_ptr
->background
.green
/255, gs
) * 255.0
990 back
.blue
= (png_byte
)(pow(
991 (double)png_ptr
->background
.blue
/255, gs
) * 255.0 + .5);
994 back_1
.red
= (png_byte
)(pow(
995 (double)png_ptr
->background
.red
/255, g
) * 255.0 + .5);
996 back_1
.green
= (png_byte
)(pow(
997 (double)png_ptr
->background
.green
/255, g
) * 255.0 + .5);
998 back_1
.blue
= (png_byte
)(pow(
999 (double)png_ptr
->background
.blue
/255, g
) * 255.0 + .5);
1001 for (i
= 0; i
< num_palette
; i
++)
1003 if (i
< (int)png_ptr
->num_trans
&& png_ptr
->trans
[i
] != 0xff)
1005 if (png_ptr
->trans
[i
] == 0)
1009 else /* if (png_ptr->trans[i] != 0xff) */
1013 v
= png_ptr
->gamma_to_1
[palette
[i
].red
];
1014 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.red
);
1015 palette
[i
].red
= png_ptr
->gamma_from_1
[w
];
1017 v
= png_ptr
->gamma_to_1
[palette
[i
].green
];
1018 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.green
);
1019 palette
[i
].green
= png_ptr
->gamma_from_1
[w
];
1021 v
= png_ptr
->gamma_to_1
[palette
[i
].blue
];
1022 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.blue
);
1023 palette
[i
].blue
= png_ptr
->gamma_from_1
[w
];
1028 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1029 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1030 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1033 /* Prevent the transformations being done again, and make sure
1034 * that the now spurious alpha channel is stripped - the code
1035 * has just reduced background composition and gamma correction
1036 * to a simple alpha channel strip.
1038 png_ptr
->transformations
&= ~PNG_BACKGROUND
;
1039 png_ptr
->transformations
&= ~PNG_GAMMA
;
1040 png_ptr
->transformations
|= PNG_STRIP_ALPHA
;
1042 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1044 /* color_type != PNG_COLOR_TYPE_PALETTE */
1046 double m
= (double)(((png_uint_32
)1 << png_ptr
->bit_depth
) - 1);
1050 switch (png_ptr
->background_gamma_type
)
1052 case PNG_BACKGROUND_GAMMA_SCREEN
:
1053 g
= (png_ptr
->screen_gamma
);
1057 case PNG_BACKGROUND_GAMMA_FILE
:
1058 g
= 1.0 / (png_ptr
->gamma
);
1059 gs
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
1062 case PNG_BACKGROUND_GAMMA_UNIQUE
:
1063 g
= 1.0 / (png_ptr
->background_gamma
);
1064 gs
= 1.0 / (png_ptr
->background_gamma
*
1065 png_ptr
->screen_gamma
);
1069 png_ptr
->background_1
.gray
= (png_uint_16
)(pow(
1070 (double)png_ptr
->background
.gray
/ m
, g
) * m
+ .5);
1071 png_ptr
->background
.gray
= (png_uint_16
)(pow(
1072 (double)png_ptr
->background
.gray
/ m
, gs
) * m
+ .5);
1074 if ((png_ptr
->background
.red
!= png_ptr
->background
.green
) ||
1075 (png_ptr
->background
.red
!= png_ptr
->background
.blue
) ||
1076 (png_ptr
->background
.red
!= png_ptr
->background
.gray
))
1078 /* RGB or RGBA with color background */
1079 png_ptr
->background_1
.red
= (png_uint_16
)(pow(
1080 (double)png_ptr
->background
.red
/ m
, g
) * m
+ .5);
1081 png_ptr
->background_1
.green
= (png_uint_16
)(pow(
1082 (double)png_ptr
->background
.green
/ m
, g
) * m
+ .5);
1083 png_ptr
->background_1
.blue
= (png_uint_16
)(pow(
1084 (double)png_ptr
->background
.blue
/ m
, g
) * m
+ .5);
1085 png_ptr
->background
.red
= (png_uint_16
)(pow(
1086 (double)png_ptr
->background
.red
/ m
, gs
) * m
+ .5);
1087 png_ptr
->background
.green
= (png_uint_16
)(pow(
1088 (double)png_ptr
->background
.green
/ m
, gs
) * m
+ .5);
1089 png_ptr
->background
.blue
= (png_uint_16
)(pow(
1090 (double)png_ptr
->background
.blue
/ m
, gs
) * m
+ .5);
1094 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1095 png_ptr
->background_1
.red
= png_ptr
->background_1
.green
1096 = png_ptr
->background_1
.blue
= png_ptr
->background_1
.gray
;
1097 png_ptr
->background
.red
= png_ptr
->background
.green
1098 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
1103 /* Transformation does not include PNG_BACKGROUND */
1104 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1105 if (color_type
== PNG_COLOR_TYPE_PALETTE
)
1107 png_colorp palette
= png_ptr
->palette
;
1108 int num_palette
= png_ptr
->num_palette
;
1111 for (i
= 0; i
< num_palette
; i
++)
1113 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1114 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1115 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1118 /* Done the gamma correction. */
1119 png_ptr
->transformations
&= ~PNG_GAMMA
;
1122 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1125 #endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
1126 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1127 /* No GAMMA transformation */
1128 if ((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1129 (color_type
== PNG_COLOR_TYPE_PALETTE
))
1132 int istop
= (int)png_ptr
->num_trans
;
1134 png_colorp palette
= png_ptr
->palette
;
1136 back
.red
= (png_byte
)png_ptr
->background
.red
;
1137 back
.green
= (png_byte
)png_ptr
->background
.green
;
1138 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
1140 for (i
= 0; i
< istop
; i
++)
1142 if (png_ptr
->trans
[i
] == 0)
1146 else if (png_ptr
->trans
[i
] != 0xff)
1148 /* The png_composite() macro is defined in png.h */
1149 png_composite(palette
[i
].red
, palette
[i
].red
,
1150 png_ptr
->trans
[i
], back
.red
);
1151 png_composite(palette
[i
].green
, palette
[i
].green
,
1152 png_ptr
->trans
[i
], back
.green
);
1153 png_composite(palette
[i
].blue
, palette
[i
].blue
,
1154 png_ptr
->trans
[i
], back
.blue
);
1158 /* Handled alpha, still need to strip the channel. */
1159 png_ptr
->transformations
&= ~PNG_BACKGROUND
;
1160 png_ptr
->transformations
|= PNG_STRIP_ALPHA
;
1162 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1164 #ifdef PNG_READ_SHIFT_SUPPORTED
1165 if ((png_ptr
->transformations
& PNG_SHIFT
) &&
1166 (color_type
== PNG_COLOR_TYPE_PALETTE
))
1169 png_uint_16 istop
= png_ptr
->num_palette
;
1170 int sr
= 8 - png_ptr
->sig_bit
.red
;
1171 int sg
= 8 - png_ptr
->sig_bit
.green
;
1172 int sb
= 8 - png_ptr
->sig_bit
.blue
;
1174 if (sr
< 0 || sr
> 8)
1176 if (sg
< 0 || sg
> 8)
1178 if (sb
< 0 || sb
> 8)
1180 for (i
= 0; i
< istop
; i
++)
1182 png_ptr
->palette
[i
].red
>>= sr
;
1183 png_ptr
->palette
[i
].green
>>= sg
;
1184 png_ptr
->palette
[i
].blue
>>= sb
;
1187 #endif /* PNG_READ_SHIFT_SUPPORTED */
1189 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1190 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1196 /* Modify the info structure to reflect the transformations. The
1197 * info should be updated so a PNG file could be written with it,
1198 * assuming the transformations result in valid PNG data.
1201 png_read_transform_info(png_structp png_ptr
, png_infop info_ptr
)
1203 png_debug(1, "in png_read_transform_info");
1205 #ifdef PNG_READ_EXPAND_SUPPORTED
1206 if (png_ptr
->transformations
& PNG_EXPAND
)
1208 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1210 if (png_ptr
->num_trans
)
1211 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
1213 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB
;
1214 info_ptr
->bit_depth
= 8;
1215 info_ptr
->num_trans
= 0;
1219 if (png_ptr
->num_trans
)
1221 if (png_ptr
->transformations
& PNG_EXPAND_tRNS
)
1222 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
1224 if (info_ptr
->bit_depth
< 8)
1225 info_ptr
->bit_depth
= 8;
1226 info_ptr
->num_trans
= 0;
1231 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1232 if (png_ptr
->transformations
& PNG_BACKGROUND
)
1234 info_ptr
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
1235 info_ptr
->num_trans
= 0;
1236 info_ptr
->background
= png_ptr
->background
;
1240 #ifdef PNG_READ_GAMMA_SUPPORTED
1241 if (png_ptr
->transformations
& PNG_GAMMA
)
1243 #ifdef PNG_FLOATING_POINT_SUPPORTED
1244 info_ptr
->gamma
= png_ptr
->gamma
;
1246 #ifdef PNG_FIXED_POINT_SUPPORTED
1247 info_ptr
->int_gamma
= png_ptr
->int_gamma
;
1252 #ifdef PNG_READ_16_TO_8_SUPPORTED
1253 if ((png_ptr
->transformations
& PNG_16_TO_8
) && (info_ptr
->bit_depth
== 16))
1254 info_ptr
->bit_depth
= 8;
1257 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1258 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
1259 info_ptr
->color_type
|= PNG_COLOR_MASK_COLOR
;
1262 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1263 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1264 info_ptr
->color_type
&= ~PNG_COLOR_MASK_COLOR
;
1267 #ifdef PNG_READ_DITHER_SUPPORTED
1268 if (png_ptr
->transformations
& PNG_DITHER
)
1270 if (((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
1271 (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)) &&
1272 png_ptr
->palette_lookup
&& info_ptr
->bit_depth
== 8)
1274 info_ptr
->color_type
= PNG_COLOR_TYPE_PALETTE
;
1279 #ifdef PNG_READ_PACK_SUPPORTED
1280 if ((png_ptr
->transformations
& PNG_PACK
) && (info_ptr
->bit_depth
< 8))
1281 info_ptr
->bit_depth
= 8;
1284 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1285 info_ptr
->channels
= 1;
1286 else if (info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
1287 info_ptr
->channels
= 3;
1289 info_ptr
->channels
= 1;
1291 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1292 if (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
)
1293 info_ptr
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
1296 if (info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)
1297 info_ptr
->channels
++;
1299 #ifdef PNG_READ_FILLER_SUPPORTED
1300 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
1301 if ((png_ptr
->transformations
& PNG_FILLER
) &&
1302 ((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
1303 (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)))
1305 info_ptr
->channels
++;
1306 /* If adding a true alpha channel not just filler */
1308 if (png_ptr
->transformations
& PNG_ADD_ALPHA
)
1309 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
1314 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1315 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1316 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
1318 if (info_ptr
->bit_depth
< png_ptr
->user_transform_depth
)
1319 info_ptr
->bit_depth
= png_ptr
->user_transform_depth
;
1320 if (info_ptr
->channels
< png_ptr
->user_transform_channels
)
1321 info_ptr
->channels
= png_ptr
->user_transform_channels
;
1325 info_ptr
->pixel_depth
= (png_byte
)(info_ptr
->channels
*
1326 info_ptr
->bit_depth
);
1328 info_ptr
->rowbytes
= PNG_ROWBYTES(info_ptr
->pixel_depth
, info_ptr
->width
);
1330 #ifndef PNG_READ_EXPAND_SUPPORTED
1336 /* Transform the row. The order of transformations is significant,
1337 * and is very touchy. If you add a transformation, take care to
1338 * decide how it fits in with the other transformations here.
1341 png_do_read_transformations(png_structp png_ptr
)
1343 png_debug(1, "in png_do_read_transformations");
1345 if (png_ptr
->row_buf
== NULL
)
1347 #if defined(PNG_STDIO_SUPPORTED) && !defined(_WIN32_WCE)
1350 png_snprintf2(msg
, 50,
1351 "NULL row buffer for row %ld, pass %d", (long)png_ptr
->row_number
,
1353 png_error(png_ptr
, msg
);
1355 png_error(png_ptr
, "NULL row buffer");
1358 #ifdef PNG_WARN_UNINITIALIZED_ROW
1359 if (!(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
1360 /* Application has failed to call either png_read_start_image()
1361 * or png_read_update_info() after setting transforms that expand
1362 * pixels. This check added to libpng-1.2.19
1364 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1365 png_error(png_ptr
, "Uninitialized row");
1367 png_warning(png_ptr
, "Uninitialized row");
1371 #ifdef PNG_READ_EXPAND_SUPPORTED
1372 if (png_ptr
->transformations
& PNG_EXPAND
)
1374 if (png_ptr
->row_info
.color_type
== PNG_COLOR_TYPE_PALETTE
)
1376 png_do_expand_palette(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1377 png_ptr
->palette
, png_ptr
->trans
, png_ptr
->num_trans
);
1381 if (png_ptr
->num_trans
&&
1382 (png_ptr
->transformations
& PNG_EXPAND_tRNS
))
1383 png_do_expand(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1384 &(png_ptr
->trans_values
));
1386 png_do_expand(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1392 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1393 if (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
)
1394 png_do_strip_filler(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1395 PNG_FLAG_FILLER_AFTER
| (png_ptr
->flags
& PNG_FLAG_STRIP_ALPHA
));
1398 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1399 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1402 png_do_rgb_to_gray(png_ptr
, &(png_ptr
->row_info
),
1403 png_ptr
->row_buf
+ 1);
1406 png_ptr
->rgb_to_gray_status
=1;
1407 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
1408 PNG_RGB_TO_GRAY_WARN
)
1409 png_warning(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
1410 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
1411 PNG_RGB_TO_GRAY_ERR
)
1412 png_error(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
1417 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1419 * In most cases, the "simple transparency" should be done prior to doing
1420 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
1421 * pixel is transparent. You would also need to make sure that the
1422 * transparency information is upgraded to RGB.
1424 * To summarize, the current flow is:
1425 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1426 * with background "in place" if transparent,
1427 * convert to RGB if necessary
1428 * - Gray + alpha -> composite with gray background and remove alpha bytes,
1429 * convert to RGB if necessary
1431 * To support RGB backgrounds for gray images we need:
1432 * - Gray + simple transparency -> convert to RGB + simple transparency,
1433 * compare 3 or 6 bytes and composite with
1434 * background "in place" if transparent
1435 * (3x compare/pixel compared to doing
1436 * composite with gray bkgrnd)
1437 * - Gray + alpha -> convert to RGB + alpha, composite with background and
1438 * remove alpha bytes (3x float
1439 * operations/pixel compared with composite
1440 * on gray background)
1442 * Greg's change will do this. The reason it wasn't done before is for
1443 * performance, as this increases the per-pixel operations. If we would check
1444 * in advance if the background was gray or RGB, and position the gray-to-RGB
1445 * transform appropriately, then it would save a lot of work/time.
1448 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1449 /* If gray -> RGB, do so now only if background is non-gray; else do later
1450 * for performance reasons
1452 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
1453 !(png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
1454 png_do_gray_to_rgb(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1457 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1458 if ((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1459 ((png_ptr
->num_trans
!= 0 ) ||
1460 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)))
1461 png_do_background(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1462 &(png_ptr
->trans_values
), &(png_ptr
->background
)
1463 #ifdef PNG_READ_GAMMA_SUPPORTED
1464 , &(png_ptr
->background_1
),
1465 png_ptr
->gamma_table
, png_ptr
->gamma_from_1
,
1466 png_ptr
->gamma_to_1
, png_ptr
->gamma_16_table
,
1467 png_ptr
->gamma_16_from_1
, png_ptr
->gamma_16_to_1
,
1468 png_ptr
->gamma_shift
1473 #ifdef PNG_READ_GAMMA_SUPPORTED
1474 if ((png_ptr
->transformations
& PNG_GAMMA
) &&
1475 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1476 !((png_ptr
->transformations
& PNG_BACKGROUND
) &&
1477 ((png_ptr
->num_trans
!= 0) ||
1478 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
))) &&
1480 (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
))
1481 png_do_gamma(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1482 png_ptr
->gamma_table
, png_ptr
->gamma_16_table
,
1483 png_ptr
->gamma_shift
);
1486 #ifdef PNG_READ_16_TO_8_SUPPORTED
1487 if (png_ptr
->transformations
& PNG_16_TO_8
)
1488 png_do_chop(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1491 #ifdef PNG_READ_DITHER_SUPPORTED
1492 if (png_ptr
->transformations
& PNG_DITHER
)
1494 png_do_dither((png_row_infop
)&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1495 png_ptr
->palette_lookup
, png_ptr
->dither_index
);
1496 if (png_ptr
->row_info
.rowbytes
== (png_uint_32
)0)
1497 png_error(png_ptr
, "png_do_dither returned rowbytes=0");
1501 #ifdef PNG_READ_INVERT_SUPPORTED
1502 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
1503 png_do_invert(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1506 #ifdef PNG_READ_SHIFT_SUPPORTED
1507 if (png_ptr
->transformations
& PNG_SHIFT
)
1508 png_do_unshift(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1512 #ifdef PNG_READ_PACK_SUPPORTED
1513 if (png_ptr
->transformations
& PNG_PACK
)
1514 png_do_unpack(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1517 #ifdef PNG_READ_BGR_SUPPORTED
1518 if (png_ptr
->transformations
& PNG_BGR
)
1519 png_do_bgr(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1522 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1523 if (png_ptr
->transformations
& PNG_PACKSWAP
)
1524 png_do_packswap(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1527 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1528 /* If gray -> RGB, do so now only if we did not do so above */
1529 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
1530 (png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
1531 png_do_gray_to_rgb(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1534 #ifdef PNG_READ_FILLER_SUPPORTED
1535 if (png_ptr
->transformations
& PNG_FILLER
)
1536 png_do_read_filler(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1,
1537 (png_uint_32
)png_ptr
->filler
, png_ptr
->flags
);
1540 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1541 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
1542 png_do_read_invert_alpha(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1545 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1546 if (png_ptr
->transformations
& PNG_SWAP_ALPHA
)
1547 png_do_read_swap_alpha(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1550 #ifdef PNG_READ_SWAP_SUPPORTED
1551 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
1552 png_do_swap(&(png_ptr
->row_info
), png_ptr
->row_buf
+ 1);
1555 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1556 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
1558 if (png_ptr
->read_user_transform_fn
!= NULL
)
1559 (*(png_ptr
->read_user_transform_fn
)) /* User read transform function */
1560 (png_ptr
, /* png_ptr */
1561 &(png_ptr
->row_info
), /* row_info: */
1562 /* png_uint_32 width; width of row */
1563 /* png_uint_32 rowbytes; number of bytes in row */
1564 /* png_byte color_type; color type of pixels */
1565 /* png_byte bit_depth; bit depth of samples */
1566 /* png_byte channels; number of channels (1-4) */
1567 /* png_byte pixel_depth; bits per pixel (depth*channels) */
1568 png_ptr
->row_buf
+ 1); /* start of pixel data for row */
1569 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1570 if (png_ptr
->user_transform_depth
)
1571 png_ptr
->row_info
.bit_depth
= png_ptr
->user_transform_depth
;
1572 if (png_ptr
->user_transform_channels
)
1573 png_ptr
->row_info
.channels
= png_ptr
->user_transform_channels
;
1575 png_ptr
->row_info
.pixel_depth
= (png_byte
)(png_ptr
->row_info
.bit_depth
*
1576 png_ptr
->row_info
.channels
);
1577 png_ptr
->row_info
.rowbytes
= PNG_ROWBYTES(png_ptr
->row_info
.pixel_depth
,
1578 png_ptr
->row_info
.width
);
1584 #ifdef PNG_READ_PACK_SUPPORTED
1585 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1586 * without changing the actual values. Thus, if you had a row with
1587 * a bit depth of 1, you would end up with bytes that only contained
1588 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
1589 * png_do_shift() after this.
1592 png_do_unpack(png_row_infop row_info
, png_bytep row
)
1594 png_debug(1, "in png_do_unpack");
1596 #ifdef PNG_USELESS_TESTS_SUPPORTED
1597 if (row
!= NULL
&& row_info
!= NULL
&& row_info
->bit_depth
< 8)
1599 if (row_info
->bit_depth
< 8)
1603 png_uint_32 row_width
=row_info
->width
;
1605 switch (row_info
->bit_depth
)
1609 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
1610 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1611 png_uint_32 shift
= 7 - (int)((row_width
+ 7) & 0x07);
1612 for (i
= 0; i
< row_width
; i
++)
1614 *dp
= (png_byte
)((*sp
>> shift
) & 0x01);
1631 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
1632 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1633 png_uint_32 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
1634 for (i
= 0; i
< row_width
; i
++)
1636 *dp
= (png_byte
)((*sp
>> shift
) & 0x03);
1652 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
1653 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
1654 png_uint_32 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
1655 for (i
= 0; i
< row_width
; i
++)
1657 *dp
= (png_byte
)((*sp
>> shift
) & 0x0f);
1671 row_info
->bit_depth
= 8;
1672 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
1673 row_info
->rowbytes
= row_width
* row_info
->channels
;
1678 #ifdef PNG_READ_SHIFT_SUPPORTED
1679 /* Reverse the effects of png_do_shift. This routine merely shifts the
1680 * pixels back to their significant bits values. Thus, if you have
1681 * a row of bit depth 8, but only 5 are significant, this will shift
1682 * the values back to 0 through 31.
1685 png_do_unshift(png_row_infop row_info
, png_bytep row
, png_color_8p sig_bits
)
1687 png_debug(1, "in png_do_unshift");
1690 #ifdef PNG_USELESS_TESTS_SUPPORTED
1691 row
!= NULL
&& row_info
!= NULL
&& sig_bits
!= NULL
&&
1693 row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
1698 png_uint_16 value
= 0;
1699 png_uint_32 row_width
= row_info
->width
;
1701 if (row_info
->color_type
& PNG_COLOR_MASK_COLOR
)
1703 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->red
;
1704 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->green
;
1705 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->blue
;
1709 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->gray
;
1711 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
1713 shift
[channels
++] = row_info
->bit_depth
- sig_bits
->alpha
;
1716 for (c
= 0; c
< channels
; c
++)
1727 switch (row_info
->bit_depth
)
1733 png_uint_32 istop
= row_info
->rowbytes
;
1735 for (bp
= row
, i
= 0; i
< istop
; i
++)
1747 png_uint_32 istop
= row_info
->rowbytes
;
1748 png_byte mask
= (png_byte
)((((int)0xf0 >> shift
[0]) & (int)0xf0) |
1749 (png_byte
)((int)0xf >> shift
[0]));
1751 for (i
= 0; i
< istop
; i
++)
1763 png_uint_32 istop
= row_width
* channels
;
1765 for (i
= 0; i
< istop
; i
++)
1767 *bp
++ >>= shift
[i
%channels
];
1776 png_uint_32 istop
= channels
* row_width
;
1778 for (i
= 0; i
< istop
; i
++)
1780 value
= (png_uint_16
)((*bp
<< 8) + *(bp
+ 1));
1781 value
>>= shift
[i
%channels
];
1782 *bp
++ = (png_byte
)(value
>> 8);
1783 *bp
++ = (png_byte
)(value
& 0xff);
1792 #ifdef PNG_READ_16_TO_8_SUPPORTED
1793 /* Chop rows of bit depth 16 down to 8 */
1795 png_do_chop(png_row_infop row_info
, png_bytep row
)
1797 png_debug(1, "in png_do_chop");
1799 #ifdef PNG_USELESS_TESTS_SUPPORTED
1800 if (row
!= NULL
&& row_info
!= NULL
&& row_info
->bit_depth
== 16)
1802 if (row_info
->bit_depth
== 16)
1808 png_uint_32 istop
= row_info
->width
* row_info
->channels
;
1810 for (i
= 0; i
<istop
; i
++, sp
+= 2, dp
++)
1812 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1813 /* This does a more accurate scaling of the 16-bit color
1814 * value, rather than a simple low-byte truncation.
1816 * What the ideal calculation should be:
1817 * *dp = (((((png_uint_32)(*sp) << 8) |
1818 * (png_uint_32)(*(sp + 1))) * 255 + 127)
1819 * / (png_uint_32)65535L;
1821 * GRR: no, I think this is what it really should be:
1822 * *dp = (((((png_uint_32)(*sp) << 8) |
1823 * (png_uint_32)(*(sp + 1))) + 128L)
1824 * / (png_uint_32)257L;
1826 * GRR: here's the exact calculation with shifts:
1827 * temp = (((png_uint_32)(*sp) << 8) |
1828 * (png_uint_32)(*(sp + 1))) + 128L;
1829 * *dp = (temp - (temp >> 8)) >> 8;
1831 * Approximate calculation with shift/add instead of multiply/divide:
1832 * *dp = ((((png_uint_32)(*sp) << 8) |
1833 * (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1835 * What we actually do to avoid extra shifting and conversion:
1838 *dp
= *sp
+ ((((int)(*(sp
+ 1)) - *sp
) > 128) ? 1 : 0);
1840 /* Simply discard the low order byte */
1844 row_info
->bit_depth
= 8;
1845 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
1846 row_info
->rowbytes
= row_info
->width
* row_info
->channels
;
1851 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1853 png_do_read_swap_alpha(png_row_infop row_info
, png_bytep row
)
1855 png_debug(1, "in png_do_read_swap_alpha");
1857 #ifdef PNG_USELESS_TESTS_SUPPORTED
1858 if (row
!= NULL
&& row_info
!= NULL
)
1861 png_uint_32 row_width
= row_info
->width
;
1862 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
1864 /* This converts from RGBA to ARGB */
1865 if (row_info
->bit_depth
== 8)
1867 png_bytep sp
= row
+ row_info
->rowbytes
;
1872 for (i
= 0; i
< row_width
; i
++)
1881 /* This converts from RRGGBBAA to AARRGGBB */
1884 png_bytep sp
= row
+ row_info
->rowbytes
;
1889 for (i
= 0; i
< row_width
; i
++)
1904 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
1906 /* This converts from GA to AG */
1907 if (row_info
->bit_depth
== 8)
1909 png_bytep sp
= row
+ row_info
->rowbytes
;
1914 for (i
= 0; i
< row_width
; i
++)
1921 /* This converts from GGAA to AAGG */
1924 png_bytep sp
= row
+ row_info
->rowbytes
;
1929 for (i
= 0; i
< row_width
; i
++)
1944 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1946 png_do_read_invert_alpha(png_row_infop row_info
, png_bytep row
)
1948 png_debug(1, "in png_do_read_invert_alpha");
1950 #ifdef PNG_USELESS_TESTS_SUPPORTED
1951 if (row
!= NULL
&& row_info
!= NULL
)
1954 png_uint_32 row_width
= row_info
->width
;
1955 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
1957 /* This inverts the alpha channel in RGBA */
1958 if (row_info
->bit_depth
== 8)
1960 png_bytep sp
= row
+ row_info
->rowbytes
;
1964 for (i
= 0; i
< row_width
; i
++)
1966 *(--dp
) = (png_byte
)(255 - *(--sp
));
1968 /* This does nothing:
1972 We can replace it with:
1978 /* This inverts the alpha channel in RRGGBBAA */
1981 png_bytep sp
= row
+ row_info
->rowbytes
;
1985 for (i
= 0; i
< row_width
; i
++)
1987 *(--dp
) = (png_byte
)(255 - *(--sp
));
1988 *(--dp
) = (png_byte
)(255 - *(--sp
));
1990 /* This does nothing:
1997 We can replace it with:
2004 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2006 /* This inverts the alpha channel in GA */
2007 if (row_info
->bit_depth
== 8)
2009 png_bytep sp
= row
+ row_info
->rowbytes
;
2013 for (i
= 0; i
< row_width
; i
++)
2015 *(--dp
) = (png_byte
)(255 - *(--sp
));
2019 /* This inverts the alpha channel in GGAA */
2022 png_bytep sp
= row
+ row_info
->rowbytes
;
2026 for (i
= 0; i
< row_width
; i
++)
2028 *(--dp
) = (png_byte
)(255 - *(--sp
));
2029 *(--dp
) = (png_byte
)(255 - *(--sp
));
2043 #ifdef PNG_READ_FILLER_SUPPORTED
2044 /* Add filler channel if we have RGB color */
2046 png_do_read_filler(png_row_infop row_info
, png_bytep row
,
2047 png_uint_32 filler
, png_uint_32 flags
)
2050 png_uint_32 row_width
= row_info
->width
;
2052 png_byte hi_filler
= (png_byte
)((filler
>>8) & 0xff);
2053 png_byte lo_filler
= (png_byte
)(filler
& 0xff);
2055 png_debug(1, "in png_do_read_filler");
2058 #ifdef PNG_USELESS_TESTS_SUPPORTED
2059 row
!= NULL
&& row_info
!= NULL
&&
2061 row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
2063 if (row_info
->bit_depth
== 8)
2065 /* This changes the data from G to GX */
2066 if (flags
& PNG_FLAG_FILLER_AFTER
)
2068 png_bytep sp
= row
+ (png_size_t
)row_width
;
2069 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2070 for (i
= 1; i
< row_width
; i
++)
2072 *(--dp
) = lo_filler
;
2075 *(--dp
) = lo_filler
;
2076 row_info
->channels
= 2;
2077 row_info
->pixel_depth
= 16;
2078 row_info
->rowbytes
= row_width
* 2;
2080 /* This changes the data from G to XG */
2083 png_bytep sp
= row
+ (png_size_t
)row_width
;
2084 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2085 for (i
= 0; i
< row_width
; i
++)
2088 *(--dp
) = lo_filler
;
2090 row_info
->channels
= 2;
2091 row_info
->pixel_depth
= 16;
2092 row_info
->rowbytes
= row_width
* 2;
2095 else if (row_info
->bit_depth
== 16)
2097 /* This changes the data from GG to GGXX */
2098 if (flags
& PNG_FLAG_FILLER_AFTER
)
2100 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
2101 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2102 for (i
= 1; i
< row_width
; i
++)
2104 *(--dp
) = hi_filler
;
2105 *(--dp
) = lo_filler
;
2109 *(--dp
) = hi_filler
;
2110 *(--dp
) = lo_filler
;
2111 row_info
->channels
= 2;
2112 row_info
->pixel_depth
= 32;
2113 row_info
->rowbytes
= row_width
* 4;
2115 /* This changes the data from GG to XXGG */
2118 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
2119 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2120 for (i
= 0; i
< row_width
; i
++)
2124 *(--dp
) = hi_filler
;
2125 *(--dp
) = lo_filler
;
2127 row_info
->channels
= 2;
2128 row_info
->pixel_depth
= 32;
2129 row_info
->rowbytes
= row_width
* 4;
2132 } /* COLOR_TYPE == GRAY */
2133 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
2135 if (row_info
->bit_depth
== 8)
2137 /* This changes the data from RGB to RGBX */
2138 if (flags
& PNG_FLAG_FILLER_AFTER
)
2140 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
2141 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2142 for (i
= 1; i
< row_width
; i
++)
2144 *(--dp
) = lo_filler
;
2149 *(--dp
) = lo_filler
;
2150 row_info
->channels
= 4;
2151 row_info
->pixel_depth
= 32;
2152 row_info
->rowbytes
= row_width
* 4;
2154 /* This changes the data from RGB to XRGB */
2157 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
2158 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2159 for (i
= 0; i
< row_width
; i
++)
2164 *(--dp
) = lo_filler
;
2166 row_info
->channels
= 4;
2167 row_info
->pixel_depth
= 32;
2168 row_info
->rowbytes
= row_width
* 4;
2171 else if (row_info
->bit_depth
== 16)
2173 /* This changes the data from RRGGBB to RRGGBBXX */
2174 if (flags
& PNG_FLAG_FILLER_AFTER
)
2176 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
2177 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2178 for (i
= 1; i
< row_width
; i
++)
2180 *(--dp
) = hi_filler
;
2181 *(--dp
) = lo_filler
;
2189 *(--dp
) = hi_filler
;
2190 *(--dp
) = lo_filler
;
2191 row_info
->channels
= 4;
2192 row_info
->pixel_depth
= 64;
2193 row_info
->rowbytes
= row_width
* 8;
2195 /* This changes the data from RRGGBB to XXRRGGBB */
2198 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
2199 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2200 for (i
= 0; i
< row_width
; i
++)
2208 *(--dp
) = hi_filler
;
2209 *(--dp
) = lo_filler
;
2211 row_info
->channels
= 4;
2212 row_info
->pixel_depth
= 64;
2213 row_info
->rowbytes
= row_width
* 8;
2216 } /* COLOR_TYPE == RGB */
2220 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2221 /* Expand grayscale files to RGB, with or without alpha */
2223 png_do_gray_to_rgb(png_row_infop row_info
, png_bytep row
)
2226 png_uint_32 row_width
= row_info
->width
;
2228 png_debug(1, "in png_do_gray_to_rgb");
2230 if (row_info
->bit_depth
>= 8 &&
2231 #ifdef PNG_USELESS_TESTS_SUPPORTED
2232 row
!= NULL
&& row_info
!= NULL
&&
2234 !(row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
2236 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
2238 if (row_info
->bit_depth
== 8)
2240 png_bytep sp
= row
+ (png_size_t
)row_width
- 1;
2241 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2242 for (i
= 0; i
< row_width
; i
++)
2251 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
2252 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
2253 for (i
= 0; i
< row_width
; i
++)
2256 *(dp
--) = *(sp
- 1);
2258 *(dp
--) = *(sp
- 1);
2264 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2266 if (row_info
->bit_depth
== 8)
2268 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
2269 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
2270 for (i
= 0; i
< row_width
; i
++)
2280 png_bytep sp
= row
+ (png_size_t
)row_width
* 4 - 1;
2281 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
2282 for (i
= 0; i
< row_width
; i
++)
2287 *(dp
--) = *(sp
- 1);
2289 *(dp
--) = *(sp
- 1);
2295 row_info
->channels
+= (png_byte
)2;
2296 row_info
->color_type
|= PNG_COLOR_MASK_COLOR
;
2297 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
2298 row_info
->bit_depth
);
2299 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
2304 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2305 /* Reduce RGB files to grayscale, with or without alpha
2306 * using the equation given in Poynton's ColorFAQ at
2307 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008)
2309 * <http://www.poynton.com/notes/colour_and_gamma/>
2310 * Charles Poynton poynton at poynton.com
2312 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2314 * We approximate this with
2316 * Y = 0.21268 * R + 0.7151 * G + 0.07217 * B
2318 * which can be expressed with integers as
2320 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
2322 * The calculation is to be done in a linear colorspace.
2324 * Other integer coefficents can be used via png_set_rgb_to_gray().
2327 png_do_rgb_to_gray(png_structp png_ptr
, png_row_infop row_info
, png_bytep row
)
2332 png_uint_32 row_width
= row_info
->width
;
2335 png_debug(1, "in png_do_rgb_to_gray");
2338 #ifdef PNG_USELESS_TESTS_SUPPORTED
2339 row
!= NULL
&& row_info
!= NULL
&&
2341 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
2343 png_uint_32 rc
= png_ptr
->rgb_to_gray_red_coeff
;
2344 png_uint_32 gc
= png_ptr
->rgb_to_gray_green_coeff
;
2345 png_uint_32 bc
= png_ptr
->rgb_to_gray_blue_coeff
;
2347 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
2349 if (row_info
->bit_depth
== 8)
2351 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2352 if (png_ptr
->gamma_from_1
!= NULL
&& png_ptr
->gamma_to_1
!= NULL
)
2357 for (i
= 0; i
< row_width
; i
++)
2359 png_byte red
= png_ptr
->gamma_to_1
[*(sp
++)];
2360 png_byte green
= png_ptr
->gamma_to_1
[*(sp
++)];
2361 png_byte blue
= png_ptr
->gamma_to_1
[*(sp
++)];
2362 if (red
!= green
|| red
!= blue
)
2365 *(dp
++) = png_ptr
->gamma_from_1
[
2366 (rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2369 *(dp
++) = *(sp
- 1);
2377 for (i
= 0; i
< row_width
; i
++)
2379 png_byte red
= *(sp
++);
2380 png_byte green
= *(sp
++);
2381 png_byte blue
= *(sp
++);
2382 if (red
!= green
|| red
!= blue
)
2385 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2388 *(dp
++) = *(sp
- 1);
2393 else /* RGB bit_depth == 16 */
2395 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2396 if (png_ptr
->gamma_16_to_1
!= NULL
&&
2397 png_ptr
->gamma_16_from_1
!= NULL
)
2401 for (i
= 0; i
< row_width
; i
++)
2403 png_uint_16 red
, green
, blue
, w
;
2405 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2406 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2407 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2409 if (red
== green
&& red
== blue
)
2413 png_uint_16 red_1
= png_ptr
->gamma_16_to_1
[(red
&0xff) >>
2414 png_ptr
->gamma_shift
][red
>>8];
2415 png_uint_16 green_1
=
2416 png_ptr
->gamma_16_to_1
[(green
&0xff) >>
2417 png_ptr
->gamma_shift
][green
>>8];
2418 png_uint_16 blue_1
= png_ptr
->gamma_16_to_1
[(blue
&0xff) >>
2419 png_ptr
->gamma_shift
][blue
>>8];
2420 png_uint_16 gray16
= (png_uint_16
)((rc
*red_1
+ gc
*green_1
2422 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
2423 png_ptr
->gamma_shift
][gray16
>> 8];
2427 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2428 *(dp
++) = (png_byte
)(w
& 0xff);
2436 for (i
= 0; i
< row_width
; i
++)
2438 png_uint_16 red
, green
, blue
, gray16
;
2440 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2441 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2442 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2444 if (red
!= green
|| red
!= blue
)
2446 gray16
= (png_uint_16
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2447 *(dp
++) = (png_byte
)((gray16
>>8) & 0xff);
2448 *(dp
++) = (png_byte
)(gray16
& 0xff);
2453 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
2455 if (row_info
->bit_depth
== 8)
2457 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2458 if (png_ptr
->gamma_from_1
!= NULL
&& png_ptr
->gamma_to_1
!= NULL
)
2462 for (i
= 0; i
< row_width
; i
++)
2464 png_byte red
= png_ptr
->gamma_to_1
[*(sp
++)];
2465 png_byte green
= png_ptr
->gamma_to_1
[*(sp
++)];
2466 png_byte blue
= png_ptr
->gamma_to_1
[*(sp
++)];
2467 if (red
!= green
|| red
!= blue
)
2469 *(dp
++) = png_ptr
->gamma_from_1
2470 [(rc
*red
+ gc
*green
+ bc
*blue
)>>15];
2471 *(dp
++) = *(sp
++); /* alpha */
2479 for (i
= 0; i
< row_width
; i
++)
2481 png_byte red
= *(sp
++);
2482 png_byte green
= *(sp
++);
2483 png_byte blue
= *(sp
++);
2484 if (red
!= green
|| red
!= blue
)
2486 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2487 *(dp
++) = *(sp
++); /* alpha */
2491 else /* RGBA bit_depth == 16 */
2493 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2494 if (png_ptr
->gamma_16_to_1
!= NULL
&&
2495 png_ptr
->gamma_16_from_1
!= NULL
)
2499 for (i
= 0; i
< row_width
; i
++)
2501 png_uint_16 red
, green
, blue
, w
;
2503 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2504 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2505 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+1)); sp
+=2;
2507 if (red
== green
&& red
== blue
)
2511 png_uint_16 red_1
= png_ptr
->gamma_16_to_1
[(red
&0xff) >>
2512 png_ptr
->gamma_shift
][red
>>8];
2513 png_uint_16 green_1
=
2514 png_ptr
->gamma_16_to_1
[(green
&0xff) >>
2515 png_ptr
->gamma_shift
][green
>>8];
2516 png_uint_16 blue_1
= png_ptr
->gamma_16_to_1
[(blue
&0xff) >>
2517 png_ptr
->gamma_shift
][blue
>>8];
2518 png_uint_16 gray16
= (png_uint_16
)((rc
* red_1
2519 + gc
* green_1
+ bc
* blue_1
)>>15);
2520 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
2521 png_ptr
->gamma_shift
][gray16
>> 8];
2525 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
2526 *(dp
++) = (png_byte
)(w
& 0xff);
2527 *(dp
++) = *(sp
++); /* alpha */
2536 for (i
= 0; i
< row_width
; i
++)
2538 png_uint_16 red
, green
, blue
, gray16
;
2539 red
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2540 green
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2541 blue
= (png_uint_16
)((*(sp
)<<8) | *(sp
+1)); sp
+=2;
2542 if (red
!= green
|| red
!= blue
)
2544 gray16
= (png_uint_16
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
2545 *(dp
++) = (png_byte
)((gray16
>>8) & 0xff);
2546 *(dp
++) = (png_byte
)(gray16
& 0xff);
2547 *(dp
++) = *(sp
++); /* alpha */
2553 row_info
->channels
-= (png_byte
)2;
2554 row_info
->color_type
&= ~PNG_COLOR_MASK_COLOR
;
2555 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
2556 row_info
->bit_depth
);
2557 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
2563 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
2564 * large of png_color. This lets grayscale images be treated as
2565 * paletted. Most useful for gamma correction and simplification
2569 png_build_grayscale_palette(int bit_depth
, png_colorp palette
)
2576 png_debug(1, "in png_do_build_grayscale_palette");
2578 if (palette
== NULL
)
2609 for (i
= 0, v
= 0; i
< num_palette
; i
++, v
+= color_inc
)
2611 palette
[i
].red
= (png_byte
)v
;
2612 palette
[i
].green
= (png_byte
)v
;
2613 palette
[i
].blue
= (png_byte
)v
;
2617 /* This function is currently unused. Do we really need it? */
2618 #if defined(PNG_READ_DITHER_SUPPORTED) && \
2619 defined(PNG_CORRECT_PALETTE_SUPPORTED)
2621 png_correct_palette(png_structp png_ptr
, png_colorp palette
,
2624 png_debug(1, "in png_correct_palette");
2626 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
2627 defined(PNG_READ_GAMMA_SUPPORTED) && \
2628 defined(PNG_FLOATING_POINT_SUPPORTED)
2629 if (png_ptr
->transformations
& (PNG_GAMMA
| PNG_BACKGROUND
))
2631 png_color back
, back_1
;
2633 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_FILE
)
2635 back
.red
= png_ptr
->gamma_table
[png_ptr
->background
.red
];
2636 back
.green
= png_ptr
->gamma_table
[png_ptr
->background
.green
];
2637 back
.blue
= png_ptr
->gamma_table
[png_ptr
->background
.blue
];
2639 back_1
.red
= png_ptr
->gamma_to_1
[png_ptr
->background
.red
];
2640 back_1
.green
= png_ptr
->gamma_to_1
[png_ptr
->background
.green
];
2641 back_1
.blue
= png_ptr
->gamma_to_1
[png_ptr
->background
.blue
];
2647 g
= 1.0 / (png_ptr
->background_gamma
* png_ptr
->screen_gamma
);
2649 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_SCREEN
2650 || fabs(g
- 1.0) < PNG_GAMMA_THRESHOLD
)
2652 back
.red
= png_ptr
->background
.red
;
2653 back
.green
= png_ptr
->background
.green
;
2654 back
.blue
= png_ptr
->background
.blue
;
2659 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2662 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2665 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2669 g
= 1.0 / png_ptr
->background_gamma
;
2672 (png_byte
)(pow((double)png_ptr
->background
.red
/255, g
) *
2675 (png_byte
)(pow((double)png_ptr
->background
.green
/255, g
) *
2678 (png_byte
)(pow((double)png_ptr
->background
.blue
/255, g
) *
2682 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2686 for (i
= 0; i
< (png_uint_32
)num_palette
; i
++)
2688 if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] == 0)
2692 else if (i
< png_ptr
->num_trans
&& png_ptr
->trans
[i
] != 0xff)
2696 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].red
];
2697 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.red
);
2698 palette
[i
].red
= png_ptr
->gamma_from_1
[w
];
2700 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].green
];
2701 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.green
);
2702 palette
[i
].green
= png_ptr
->gamma_from_1
[w
];
2704 v
= png_ptr
->gamma_to_1
[png_ptr
->palette
[i
].blue
];
2705 png_composite(w
, v
, png_ptr
->trans
[i
], back_1
.blue
);
2706 palette
[i
].blue
= png_ptr
->gamma_from_1
[w
];
2710 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2711 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2712 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2720 for (i
= 0; i
< num_palette
; i
++)
2722 if (palette
[i
].red
== (png_byte
)png_ptr
->trans_values
.gray
)
2728 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2729 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2730 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2737 #ifdef PNG_READ_GAMMA_SUPPORTED
2738 if (png_ptr
->transformations
& PNG_GAMMA
)
2742 for (i
= 0; i
< num_palette
; i
++)
2744 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
2745 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
2746 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
2749 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2753 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2754 if (png_ptr
->transformations
& PNG_BACKGROUND
)
2756 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2760 back
.red
= (png_byte
)png_ptr
->background
.red
;
2761 back
.green
= (png_byte
)png_ptr
->background
.green
;
2762 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
2764 for (i
= 0; i
< (int)png_ptr
->num_trans
; i
++)
2766 if (png_ptr
->trans
[i
] == 0)
2768 palette
[i
].red
= back
.red
;
2769 palette
[i
].green
= back
.green
;
2770 palette
[i
].blue
= back
.blue
;
2772 else if (png_ptr
->trans
[i
] != 0xff)
2774 png_composite(palette
[i
].red
, png_ptr
->palette
[i
].red
,
2775 png_ptr
->trans
[i
], back
.red
);
2776 png_composite(palette
[i
].green
, png_ptr
->palette
[i
].green
,
2777 png_ptr
->trans
[i
], back
.green
);
2778 png_composite(palette
[i
].blue
, png_ptr
->palette
[i
].blue
,
2779 png_ptr
->trans
[i
], back
.blue
);
2783 else /* Assume grayscale palette (what else could it be?) */
2787 for (i
= 0; i
< num_palette
; i
++)
2789 if (i
== (png_byte
)png_ptr
->trans_values
.gray
)
2791 palette
[i
].red
= (png_byte
)png_ptr
->background
.red
;
2792 palette
[i
].green
= (png_byte
)png_ptr
->background
.green
;
2793 palette
[i
].blue
= (png_byte
)png_ptr
->background
.blue
;
2802 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2803 /* Replace any alpha or transparency with the supplied background color.
2804 * "background" is already in the screen gamma, while "background_1" is
2805 * at a gamma of 1.0. Paletted files have already been taken care of.
2808 png_do_background(png_row_infop row_info
, png_bytep row
,
2809 png_color_16p trans_values
, png_color_16p background
2810 #ifdef PNG_READ_GAMMA_SUPPORTED
2811 , png_color_16p background_1
,
2812 png_bytep gamma_table
, png_bytep gamma_from_1
, png_bytep gamma_to_1
,
2813 png_uint_16pp gamma_16
, png_uint_16pp gamma_16_from_1
,
2814 png_uint_16pp gamma_16_to_1
, int gamma_shift
2820 png_uint_32 row_width
=row_info
->width
;
2823 png_debug(1, "in png_do_background");
2825 if (background
!= NULL
&&
2826 #ifdef PNG_USELESS_TESTS_SUPPORTED
2827 row
!= NULL
&& row_info
!= NULL
&&
2829 (!(row_info
->color_type
& PNG_COLOR_MASK_ALPHA
) ||
2830 (row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
&& trans_values
)))
2832 switch (row_info
->color_type
)
2834 case PNG_COLOR_TYPE_GRAY
:
2836 switch (row_info
->bit_depth
)
2842 for (i
= 0; i
< row_width
; i
++)
2844 if ((png_uint_16
)((*sp
>> shift
) & 0x01)
2845 == trans_values
->gray
)
2847 *sp
&= (png_byte
)((0x7f7f >> (7 - shift
)) & 0xff);
2848 *sp
|= (png_byte
)(background
->gray
<< shift
);
2863 #ifdef PNG_READ_GAMMA_SUPPORTED
2864 if (gamma_table
!= NULL
)
2868 for (i
= 0; i
< row_width
; i
++)
2870 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
2871 == trans_values
->gray
)
2873 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2874 *sp
|= (png_byte
)(background
->gray
<< shift
);
2878 png_byte p
= (png_byte
)((*sp
>> shift
) & 0x03);
2879 png_byte g
= (png_byte
)((gamma_table
[p
| (p
<< 2) |
2880 (p
<< 4) | (p
<< 6)] >> 6) & 0x03);
2881 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2882 *sp
|= (png_byte
)(g
<< shift
);
2898 for (i
= 0; i
< row_width
; i
++)
2900 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
2901 == trans_values
->gray
)
2903 *sp
&= (png_byte
)((0x3f3f >> (6 - shift
)) & 0xff);
2904 *sp
|= (png_byte
)(background
->gray
<< shift
);
2920 #ifdef PNG_READ_GAMMA_SUPPORTED
2921 if (gamma_table
!= NULL
)
2925 for (i
= 0; i
< row_width
; i
++)
2927 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
2928 == trans_values
->gray
)
2930 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2931 *sp
|= (png_byte
)(background
->gray
<< shift
);
2935 png_byte p
= (png_byte
)((*sp
>> shift
) & 0x0f);
2936 png_byte g
= (png_byte
)((gamma_table
[p
|
2937 (p
<< 4)] >> 4) & 0x0f);
2938 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2939 *sp
|= (png_byte
)(g
<< shift
);
2955 for (i
= 0; i
< row_width
; i
++)
2957 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
2958 == trans_values
->gray
)
2960 *sp
&= (png_byte
)((0xf0f >> (4 - shift
)) & 0xff);
2961 *sp
|= (png_byte
)(background
->gray
<< shift
);
2977 #ifdef PNG_READ_GAMMA_SUPPORTED
2978 if (gamma_table
!= NULL
)
2981 for (i
= 0; i
< row_width
; i
++, sp
++)
2983 if (*sp
== trans_values
->gray
)
2985 *sp
= (png_byte
)background
->gray
;
2989 *sp
= gamma_table
[*sp
];
2997 for (i
= 0; i
< row_width
; i
++, sp
++)
2999 if (*sp
== trans_values
->gray
)
3001 *sp
= (png_byte
)background
->gray
;
3010 #ifdef PNG_READ_GAMMA_SUPPORTED
3011 if (gamma_16
!= NULL
)
3014 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3018 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3019 if (v
== trans_values
->gray
)
3021 /* Background is already in screen gamma */
3022 *sp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3023 *(sp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3027 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3028 *sp
= (png_byte
)((v
>> 8) & 0xff);
3029 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3037 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3041 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3042 if (v
== trans_values
->gray
)
3044 *sp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3045 *(sp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3055 case PNG_COLOR_TYPE_RGB
:
3057 if (row_info
->bit_depth
== 8)
3059 #ifdef PNG_READ_GAMMA_SUPPORTED
3060 if (gamma_table
!= NULL
)
3063 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3065 if (*sp
== trans_values
->red
&&
3066 *(sp
+ 1) == trans_values
->green
&&
3067 *(sp
+ 2) == trans_values
->blue
)
3069 *sp
= (png_byte
)background
->red
;
3070 *(sp
+ 1) = (png_byte
)background
->green
;
3071 *(sp
+ 2) = (png_byte
)background
->blue
;
3075 *sp
= gamma_table
[*sp
];
3076 *(sp
+ 1) = gamma_table
[*(sp
+ 1)];
3077 *(sp
+ 2) = gamma_table
[*(sp
+ 2)];
3085 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3087 if (*sp
== trans_values
->red
&&
3088 *(sp
+ 1) == trans_values
->green
&&
3089 *(sp
+ 2) == trans_values
->blue
)
3091 *sp
= (png_byte
)background
->red
;
3092 *(sp
+ 1) = (png_byte
)background
->green
;
3093 *(sp
+ 2) = (png_byte
)background
->blue
;
3098 else /* if (row_info->bit_depth == 16) */
3100 #ifdef PNG_READ_GAMMA_SUPPORTED
3101 if (gamma_16
!= NULL
)
3104 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3106 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3107 png_uint_16 g
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3108 png_uint_16 b
= (png_uint_16
)(((*(sp
+4)) << 8) + *(sp
+5));
3109 if (r
== trans_values
->red
&& g
== trans_values
->green
&&
3110 b
== trans_values
->blue
)
3112 /* Background is already in screen gamma */
3113 *sp
= (png_byte
)((background
->red
>> 8) & 0xff);
3114 *(sp
+ 1) = (png_byte
)(background
->red
& 0xff);
3115 *(sp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3116 *(sp
+ 3) = (png_byte
)(background
->green
& 0xff);
3117 *(sp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3118 *(sp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3122 png_uint_16 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3123 *sp
= (png_byte
)((v
>> 8) & 0xff);
3124 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3125 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3126 *(sp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3127 *(sp
+ 3) = (png_byte
)(v
& 0xff);
3128 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3129 *(sp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3130 *(sp
+ 5) = (png_byte
)(v
& 0xff);
3138 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3140 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+1));
3141 png_uint_16 g
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3142 png_uint_16 b
= (png_uint_16
)(((*(sp
+4)) << 8) + *(sp
+5));
3144 if (r
== trans_values
->red
&& g
== trans_values
->green
&&
3145 b
== trans_values
->blue
)
3147 *sp
= (png_byte
)((background
->red
>> 8) & 0xff);
3148 *(sp
+ 1) = (png_byte
)(background
->red
& 0xff);
3149 *(sp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3150 *(sp
+ 3) = (png_byte
)(background
->green
& 0xff);
3151 *(sp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3152 *(sp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3160 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3162 if (row_info
->bit_depth
== 8)
3164 #ifdef PNG_READ_GAMMA_SUPPORTED
3165 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
3166 gamma_table
!= NULL
)
3170 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3172 png_uint_16 a
= *(sp
+ 1);
3176 *dp
= gamma_table
[*sp
];
3180 /* Background is already in screen gamma */
3181 *dp
= (png_byte
)background
->gray
;
3187 v
= gamma_to_1
[*sp
];
3188 png_composite(w
, v
, a
, background_1
->gray
);
3189 *dp
= gamma_from_1
[w
];
3198 for (i
= 0; i
< row_width
; i
++, sp
+= 2, dp
++)
3200 png_byte a
= *(sp
+ 1);
3206 #ifdef PNG_READ_GAMMA_SUPPORTED
3209 *dp
= (png_byte
)background
->gray
;
3213 png_composite(*dp
, *sp
, a
, background_1
->gray
);
3216 *dp
= (png_byte
)background
->gray
;
3221 else /* if (png_ptr->bit_depth == 16) */
3223 #ifdef PNG_READ_GAMMA_SUPPORTED
3224 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
3225 gamma_16_to_1
!= NULL
)
3229 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 2)
3231 png_uint_16 a
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3233 if (a
== (png_uint_16
)0xffff)
3237 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3238 *dp
= (png_byte
)((v
>> 8) & 0xff);
3239 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3241 #ifdef PNG_READ_GAMMA_SUPPORTED
3247 /* Background is already in screen gamma */
3248 *dp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3249 *(dp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3251 #ifdef PNG_READ_GAMMA_SUPPORTED
3254 png_uint_16 g
, v
, w
;
3256 g
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
3257 png_composite_16(v
, g
, a
, background_1
->gray
);
3258 w
= gamma_16_from_1
[(v
&0xff) >> gamma_shift
][v
>> 8];
3259 *dp
= (png_byte
)((w
>> 8) & 0xff);
3260 *(dp
+ 1) = (png_byte
)(w
& 0xff);
3270 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 2)
3272 png_uint_16 a
= (png_uint_16
)(((*(sp
+2)) << 8) + *(sp
+3));
3273 if (a
== (png_uint_16
)0xffff)
3275 png_memcpy(dp
, sp
, 2);
3277 #ifdef PNG_READ_GAMMA_SUPPORTED
3283 *dp
= (png_byte
)((background
->gray
>> 8) & 0xff);
3284 *(dp
+ 1) = (png_byte
)(background
->gray
& 0xff);
3286 #ifdef PNG_READ_GAMMA_SUPPORTED
3291 g
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3292 png_composite_16(v
, g
, a
, background_1
->gray
);
3293 *dp
= (png_byte
)((v
>> 8) & 0xff);
3294 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3303 case PNG_COLOR_TYPE_RGB_ALPHA
:
3305 if (row_info
->bit_depth
== 8)
3307 #ifdef PNG_READ_GAMMA_SUPPORTED
3308 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
3309 gamma_table
!= NULL
)
3313 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3315 png_byte a
= *(sp
+ 3);
3319 *dp
= gamma_table
[*sp
];
3320 *(dp
+ 1) = gamma_table
[*(sp
+ 1)];
3321 *(dp
+ 2) = gamma_table
[*(sp
+ 2)];
3325 /* Background is already in screen gamma */
3326 *dp
= (png_byte
)background
->red
;
3327 *(dp
+ 1) = (png_byte
)background
->green
;
3328 *(dp
+ 2) = (png_byte
)background
->blue
;
3334 v
= gamma_to_1
[*sp
];
3335 png_composite(w
, v
, a
, background_1
->red
);
3336 *dp
= gamma_from_1
[w
];
3337 v
= gamma_to_1
[*(sp
+ 1)];
3338 png_composite(w
, v
, a
, background_1
->green
);
3339 *(dp
+ 1) = gamma_from_1
[w
];
3340 v
= gamma_to_1
[*(sp
+ 2)];
3341 png_composite(w
, v
, a
, background_1
->blue
);
3342 *(dp
+ 2) = gamma_from_1
[w
];
3351 for (i
= 0; i
< row_width
; i
++, sp
+= 4, dp
+= 3)
3353 png_byte a
= *(sp
+ 3);
3358 *(dp
+ 1) = *(sp
+ 1);
3359 *(dp
+ 2) = *(sp
+ 2);
3363 *dp
= (png_byte
)background
->red
;
3364 *(dp
+ 1) = (png_byte
)background
->green
;
3365 *(dp
+ 2) = (png_byte
)background
->blue
;
3369 png_composite(*dp
, *sp
, a
, background
->red
);
3370 png_composite(*(dp
+ 1), *(sp
+ 1), a
,
3372 png_composite(*(dp
+ 2), *(sp
+ 2), a
,
3378 else /* if (row_info->bit_depth == 16) */
3380 #ifdef PNG_READ_GAMMA_SUPPORTED
3381 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
3382 gamma_16_to_1
!= NULL
)
3386 for (i
= 0; i
< row_width
; i
++, sp
+= 8, dp
+= 6)
3388 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
3389 << 8) + (png_uint_16
)(*(sp
+ 7)));
3390 if (a
== (png_uint_16
)0xffff)
3394 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3395 *dp
= (png_byte
)((v
>> 8) & 0xff);
3396 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3397 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3398 *(dp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3399 *(dp
+ 3) = (png_byte
)(v
& 0xff);
3400 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3401 *(dp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3402 *(dp
+ 5) = (png_byte
)(v
& 0xff);
3406 /* Background is already in screen gamma */
3407 *dp
= (png_byte
)((background
->red
>> 8) & 0xff);
3408 *(dp
+ 1) = (png_byte
)(background
->red
& 0xff);
3409 *(dp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3410 *(dp
+ 3) = (png_byte
)(background
->green
& 0xff);
3411 *(dp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3412 *(dp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3416 png_uint_16 v
, w
, x
;
3418 v
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
3419 png_composite_16(w
, v
, a
, background_1
->red
);
3420 x
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>> 8];
3421 *dp
= (png_byte
)((x
>> 8) & 0xff);
3422 *(dp
+ 1) = (png_byte
)(x
& 0xff);
3423 v
= gamma_16_to_1
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3424 png_composite_16(w
, v
, a
, background_1
->green
);
3425 x
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>> 8];
3426 *(dp
+ 2) = (png_byte
)((x
>> 8) & 0xff);
3427 *(dp
+ 3) = (png_byte
)(x
& 0xff);
3428 v
= gamma_16_to_1
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3429 png_composite_16(w
, v
, a
, background_1
->blue
);
3430 x
= gamma_16_from_1
[(w
& 0xff) >> gamma_shift
][w
>> 8];
3431 *(dp
+ 4) = (png_byte
)((x
>> 8) & 0xff);
3432 *(dp
+ 5) = (png_byte
)(x
& 0xff);
3441 for (i
= 0; i
< row_width
; i
++, sp
+= 8, dp
+= 6)
3443 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
3444 << 8) + (png_uint_16
)(*(sp
+ 7)));
3445 if (a
== (png_uint_16
)0xffff)
3447 png_memcpy(dp
, sp
, 6);
3451 *dp
= (png_byte
)((background
->red
>> 8) & 0xff);
3452 *(dp
+ 1) = (png_byte
)(background
->red
& 0xff);
3453 *(dp
+ 2) = (png_byte
)((background
->green
>> 8) & 0xff);
3454 *(dp
+ 3) = (png_byte
)(background
->green
& 0xff);
3455 *(dp
+ 4) = (png_byte
)((background
->blue
>> 8) & 0xff);
3456 *(dp
+ 5) = (png_byte
)(background
->blue
& 0xff);
3462 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3463 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
3465 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
3468 png_composite_16(v
, r
, a
, background
->red
);
3469 *dp
= (png_byte
)((v
>> 8) & 0xff);
3470 *(dp
+ 1) = (png_byte
)(v
& 0xff);
3471 png_composite_16(v
, g
, a
, background
->green
);
3472 *(dp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3473 *(dp
+ 3) = (png_byte
)(v
& 0xff);
3474 png_composite_16(v
, b
, a
, background
->blue
);
3475 *(dp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3476 *(dp
+ 5) = (png_byte
)(v
& 0xff);
3485 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
3487 row_info
->color_type
&= ~PNG_COLOR_MASK_ALPHA
;
3488 row_info
->channels
--;
3489 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
3490 row_info
->bit_depth
);
3491 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
3497 #ifdef PNG_READ_GAMMA_SUPPORTED
3498 /* Gamma correct the image, avoiding the alpha channel. Make sure
3499 * you do this after you deal with the transparency issue on grayscale
3500 * or RGB images. If your bit depth is 8, use gamma_table, if it
3501 * is 16, use gamma_16_table and gamma_shift. Build these with
3502 * build_gamma_table().
3505 png_do_gamma(png_row_infop row_info
, png_bytep row
,
3506 png_bytep gamma_table
, png_uint_16pp gamma_16_table
,
3511 png_uint_32 row_width
=row_info
->width
;
3513 png_debug(1, "in png_do_gamma");
3516 #ifdef PNG_USELESS_TESTS_SUPPORTED
3517 row
!= NULL
&& row_info
!= NULL
&&
3519 ((row_info
->bit_depth
<= 8 && gamma_table
!= NULL
) ||
3520 (row_info
->bit_depth
== 16 && gamma_16_table
!= NULL
)))
3522 switch (row_info
->color_type
)
3524 case PNG_COLOR_TYPE_RGB
:
3526 if (row_info
->bit_depth
== 8)
3529 for (i
= 0; i
< row_width
; i
++)
3531 *sp
= gamma_table
[*sp
];
3533 *sp
= gamma_table
[*sp
];
3535 *sp
= gamma_table
[*sp
];
3539 else /* if (row_info->bit_depth == 16) */
3542 for (i
= 0; i
< row_width
; i
++)
3546 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3547 *sp
= (png_byte
)((v
>> 8) & 0xff);
3548 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3550 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3551 *sp
= (png_byte
)((v
>> 8) & 0xff);
3552 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3554 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3555 *sp
= (png_byte
)((v
>> 8) & 0xff);
3556 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3563 case PNG_COLOR_TYPE_RGB_ALPHA
:
3565 if (row_info
->bit_depth
== 8)
3568 for (i
= 0; i
< row_width
; i
++)
3570 *sp
= gamma_table
[*sp
];
3572 *sp
= gamma_table
[*sp
];
3574 *sp
= gamma_table
[*sp
];
3579 else /* if (row_info->bit_depth == 16) */
3582 for (i
= 0; i
< row_width
; i
++)
3584 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3585 *sp
= (png_byte
)((v
>> 8) & 0xff);
3586 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3588 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3589 *sp
= (png_byte
)((v
>> 8) & 0xff);
3590 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3592 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3593 *sp
= (png_byte
)((v
>> 8) & 0xff);
3594 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3601 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3603 if (row_info
->bit_depth
== 8)
3606 for (i
= 0; i
< row_width
; i
++)
3608 *sp
= gamma_table
[*sp
];
3612 else /* if (row_info->bit_depth == 16) */
3615 for (i
= 0; i
< row_width
; i
++)
3617 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3618 *sp
= (png_byte
)((v
>> 8) & 0xff);
3619 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3626 case PNG_COLOR_TYPE_GRAY
:
3628 if (row_info
->bit_depth
== 2)
3631 for (i
= 0; i
< row_width
; i
+= 4)
3639 ((((int)gamma_table
[a
|(a
>>2)|(a
>>4)|(a
>>6)]) ) & 0xc0)|
3640 ((((int)gamma_table
[(b
<<2)|b
|(b
>>2)|(b
>>4)])>>2) & 0x30)|
3641 ((((int)gamma_table
[(c
<<4)|(c
<<2)|c
|(c
>>2)])>>4) & 0x0c)|
3642 ((((int)gamma_table
[(d
<<6)|(d
<<4)|(d
<<2)|d
])>>6) ));
3647 if (row_info
->bit_depth
== 4)
3650 for (i
= 0; i
< row_width
; i
+= 2)
3652 int msb
= *sp
& 0xf0;
3653 int lsb
= *sp
& 0x0f;
3655 *sp
= (png_byte
)((((int)gamma_table
[msb
| (msb
>> 4)]) & 0xf0)
3656 | (((int)gamma_table
[(lsb
<< 4) | lsb
]) >> 4));
3661 else if (row_info
->bit_depth
== 8)
3664 for (i
= 0; i
< row_width
; i
++)
3666 *sp
= gamma_table
[*sp
];
3671 else if (row_info
->bit_depth
== 16)
3674 for (i
= 0; i
< row_width
; i
++)
3676 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
3677 *sp
= (png_byte
)((v
>> 8) & 0xff);
3678 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3689 #ifdef PNG_READ_EXPAND_SUPPORTED
3690 /* Expands a palette row to an RGB or RGBA row depending
3691 * upon whether you supply trans and num_trans.
3694 png_do_expand_palette(png_row_infop row_info
, png_bytep row
,
3695 png_colorp palette
, png_bytep trans
, int num_trans
)
3700 png_uint_32 row_width
=row_info
->width
;
3702 png_debug(1, "in png_do_expand_palette");
3705 #ifdef PNG_USELESS_TESTS_SUPPORTED
3706 row
!= NULL
&& row_info
!= NULL
&&
3708 row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
)
3710 if (row_info
->bit_depth
< 8)
3712 switch (row_info
->bit_depth
)
3716 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
3717 dp
= row
+ (png_size_t
)row_width
- 1;
3718 shift
= 7 - (int)((row_width
+ 7) & 0x07);
3719 for (i
= 0; i
< row_width
; i
++)
3721 if ((*sp
>> shift
) & 0x01)
3740 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
3741 dp
= row
+ (png_size_t
)row_width
- 1;
3742 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
3743 for (i
= 0; i
< row_width
; i
++)
3745 value
= (*sp
>> shift
) & 0x03;
3746 *dp
= (png_byte
)value
;
3762 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
3763 dp
= row
+ (png_size_t
)row_width
- 1;
3764 shift
= (int)((row_width
& 0x01) << 2);
3765 for (i
= 0; i
< row_width
; i
++)
3767 value
= (*sp
>> shift
) & 0x0f;
3768 *dp
= (png_byte
)value
;
3782 row_info
->bit_depth
= 8;
3783 row_info
->pixel_depth
= 8;
3784 row_info
->rowbytes
= row_width
;
3786 switch (row_info
->bit_depth
)
3792 sp
= row
+ (png_size_t
)row_width
- 1;
3793 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
3795 for (i
= 0; i
< row_width
; i
++)
3797 if ((int)(*sp
) >= num_trans
)
3801 *dp
-- = palette
[*sp
].blue
;
3802 *dp
-- = palette
[*sp
].green
;
3803 *dp
-- = palette
[*sp
].red
;
3806 row_info
->bit_depth
= 8;
3807 row_info
->pixel_depth
= 32;
3808 row_info
->rowbytes
= row_width
* 4;
3809 row_info
->color_type
= 6;
3810 row_info
->channels
= 4;
3814 sp
= row
+ (png_size_t
)row_width
- 1;
3815 dp
= row
+ (png_size_t
)(row_width
* 3) - 1;
3817 for (i
= 0; i
< row_width
; i
++)
3819 *dp
-- = palette
[*sp
].blue
;
3820 *dp
-- = palette
[*sp
].green
;
3821 *dp
-- = palette
[*sp
].red
;
3825 row_info
->bit_depth
= 8;
3826 row_info
->pixel_depth
= 24;
3827 row_info
->rowbytes
= row_width
* 3;
3828 row_info
->color_type
= 2;
3829 row_info
->channels
= 3;
3837 /* If the bit depth < 8, it is expanded to 8. Also, if the already
3838 * expanded transparency value is supplied, an alpha channel is built.
3841 png_do_expand(png_row_infop row_info
, png_bytep row
,
3842 png_color_16p trans_value
)
3847 png_uint_32 row_width
=row_info
->width
;
3849 png_debug(1, "in png_do_expand");
3851 #ifdef PNG_USELESS_TESTS_SUPPORTED
3852 if (row
!= NULL
&& row_info
!= NULL
)
3855 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
3857 png_uint_16 gray
= (png_uint_16
)(trans_value
? trans_value
->gray
: 0);
3859 if (row_info
->bit_depth
< 8)
3861 switch (row_info
->bit_depth
)
3865 gray
= (png_uint_16
)((gray
&0x01)*0xff);
3866 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
3867 dp
= row
+ (png_size_t
)row_width
- 1;
3868 shift
= 7 - (int)((row_width
+ 7) & 0x07);
3869 for (i
= 0; i
< row_width
; i
++)
3871 if ((*sp
>> shift
) & 0x01)
3890 gray
= (png_uint_16
)((gray
&0x03)*0x55);
3891 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
3892 dp
= row
+ (png_size_t
)row_width
- 1;
3893 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
3894 for (i
= 0; i
< row_width
; i
++)
3896 value
= (*sp
>> shift
) & 0x03;
3897 *dp
= (png_byte
)(value
| (value
<< 2) | (value
<< 4) |
3914 gray
= (png_uint_16
)((gray
&0x0f)*0x11);
3915 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
3916 dp
= row
+ (png_size_t
)row_width
- 1;
3917 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
3918 for (i
= 0; i
< row_width
; i
++)
3920 value
= (*sp
>> shift
) & 0x0f;
3921 *dp
= (png_byte
)(value
| (value
<< 4));
3936 row_info
->bit_depth
= 8;
3937 row_info
->pixel_depth
= 8;
3938 row_info
->rowbytes
= row_width
;
3941 if (trans_value
!= NULL
)
3943 if (row_info
->bit_depth
== 8)
3946 sp
= row
+ (png_size_t
)row_width
- 1;
3947 dp
= row
+ (png_size_t
)(row_width
<< 1) - 1;
3948 for (i
= 0; i
< row_width
; i
++)
3958 else if (row_info
->bit_depth
== 16)
3960 png_byte gray_high
= (gray
>> 8) & 0xff;
3961 png_byte gray_low
= gray
& 0xff;
3962 sp
= row
+ row_info
->rowbytes
- 1;
3963 dp
= row
+ (row_info
->rowbytes
<< 1) - 1;
3964 for (i
= 0; i
< row_width
; i
++)
3966 if (*(sp
- 1) == gray_high
&& *(sp
) == gray_low
)
3981 row_info
->color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
3982 row_info
->channels
= 2;
3983 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 1);
3984 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
3988 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&& trans_value
)
3990 if (row_info
->bit_depth
== 8)
3992 png_byte red
= trans_value
->red
& 0xff;
3993 png_byte green
= trans_value
->green
& 0xff;
3994 png_byte blue
= trans_value
->blue
& 0xff;
3995 sp
= row
+ (png_size_t
)row_info
->rowbytes
- 1;
3996 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
3997 for (i
= 0; i
< row_width
; i
++)
3999 if (*(sp
- 2) == red
&& *(sp
- 1) == green
&& *(sp
) == blue
)
4008 else if (row_info
->bit_depth
== 16)
4010 png_byte red_high
= (trans_value
->red
>> 8) & 0xff;
4011 png_byte green_high
= (trans_value
->green
>> 8) & 0xff;
4012 png_byte blue_high
= (trans_value
->blue
>> 8) & 0xff;
4013 png_byte red_low
= trans_value
->red
& 0xff;
4014 png_byte green_low
= trans_value
->green
& 0xff;
4015 png_byte blue_low
= trans_value
->blue
& 0xff;
4016 sp
= row
+ row_info
->rowbytes
- 1;
4017 dp
= row
+ (png_size_t
)(row_width
<< 3) - 1;
4018 for (i
= 0; i
< row_width
; i
++)
4020 if (*(sp
- 5) == red_high
&&
4021 *(sp
- 4) == red_low
&&
4022 *(sp
- 3) == green_high
&&
4023 *(sp
- 2) == green_low
&&
4024 *(sp
- 1) == blue_high
&&
4043 row_info
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
4044 row_info
->channels
= 4;
4045 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 2);
4046 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4052 #ifdef PNG_READ_DITHER_SUPPORTED
4054 png_do_dither(png_row_infop row_info
, png_bytep row
,
4055 png_bytep palette_lookup
, png_bytep dither_lookup
)
4059 png_uint_32 row_width
=row_info
->width
;
4061 png_debug(1, "in png_do_dither");
4063 #ifdef PNG_USELESS_TESTS_SUPPORTED
4064 if (row
!= NULL
&& row_info
!= NULL
)
4067 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&&
4068 palette_lookup
&& row_info
->bit_depth
== 8)
4073 for (i
= 0; i
< row_width
; i
++)
4079 /* This looks real messy, but the compiler will reduce
4080 * it down to a reasonable formula. For example, with
4081 * 5 bits per color, we get:
4082 * p = (((r >> 3) & 0x1f) << 10) |
4083 * (((g >> 3) & 0x1f) << 5) |
4084 * ((b >> 3) & 0x1f);
4086 p
= (((r
>> (8 - PNG_DITHER_RED_BITS
)) &
4087 ((1 << PNG_DITHER_RED_BITS
) - 1)) <<
4088 (PNG_DITHER_GREEN_BITS
+ PNG_DITHER_BLUE_BITS
)) |
4089 (((g
>> (8 - PNG_DITHER_GREEN_BITS
)) &
4090 ((1 << PNG_DITHER_GREEN_BITS
) - 1)) <<
4091 (PNG_DITHER_BLUE_BITS
)) |
4092 ((b
>> (8 - PNG_DITHER_BLUE_BITS
)) &
4093 ((1 << PNG_DITHER_BLUE_BITS
) - 1));
4095 *dp
++ = palette_lookup
[p
];
4097 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
4098 row_info
->channels
= 1;
4099 row_info
->pixel_depth
= row_info
->bit_depth
;
4100 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4102 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
4103 palette_lookup
!= NULL
&& row_info
->bit_depth
== 8)
4108 for (i
= 0; i
< row_width
; i
++)
4115 p
= (((r
>> (8 - PNG_DITHER_RED_BITS
)) &
4116 ((1 << PNG_DITHER_RED_BITS
) - 1)) <<
4117 (PNG_DITHER_GREEN_BITS
+ PNG_DITHER_BLUE_BITS
)) |
4118 (((g
>> (8 - PNG_DITHER_GREEN_BITS
)) &
4119 ((1 << PNG_DITHER_GREEN_BITS
) - 1)) <<
4120 (PNG_DITHER_BLUE_BITS
)) |
4121 ((b
>> (8 - PNG_DITHER_BLUE_BITS
)) &
4122 ((1 << PNG_DITHER_BLUE_BITS
) - 1));
4124 *dp
++ = palette_lookup
[p
];
4126 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
4127 row_info
->channels
= 1;
4128 row_info
->pixel_depth
= row_info
->bit_depth
;
4129 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4131 else if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
4132 dither_lookup
&& row_info
->bit_depth
== 8)
4135 for (i
= 0; i
< row_width
; i
++, sp
++)
4137 *sp
= dither_lookup
[*sp
];
4144 #ifdef PNG_FLOATING_POINT_SUPPORTED
4145 #ifdef PNG_READ_GAMMA_SUPPORTED
4146 static PNG_CONST
int png_gamma_shift
[] =
4147 {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
4149 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
4150 * tables, we don't make a full table if we are reducing to 8-bit in
4151 * the future. Note also how the gamma_16 tables are segmented so that
4152 * we don't need to allocate > 64K chunks for a full 16-bit table.
4154 * See the PNG extensions document for an integer algorithm for creating
4155 * the gamma tables. Maybe we will implement that here someday.
4157 * We should only reach this point if
4159 * the file_gamma is known (i.e., the gAMA or sRGB chunk is present,
4160 * or the application has provided a file_gamma)
4164 * the screen_gamma is known
4167 * RGB_to_gray transformation is being performed
4172 * the screen_gamma is different from the reciprocal of the
4173 * file_gamma by more than the specified threshold
4177 * a background color has been specified and the file_gamma
4178 * and screen_gamma are not 1.0, within the specified threshold.
4183 png_build_gamma_table(png_structp png_ptr
)
4185 png_debug(1, "in png_build_gamma_table");
4187 if (png_ptr
->bit_depth
<= 8)
4192 if (png_ptr
->screen_gamma
> .000001)
4193 g
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
4198 png_ptr
->gamma_table
= (png_bytep
)png_malloc(png_ptr
,
4201 for (i
= 0; i
< 256; i
++)
4203 png_ptr
->gamma_table
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4207 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4208 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4209 if (png_ptr
->transformations
& ((PNG_BACKGROUND
) | PNG_RGB_TO_GRAY
))
4212 g
= 1.0 / (png_ptr
->gamma
);
4214 png_ptr
->gamma_to_1
= (png_bytep
)png_malloc(png_ptr
,
4217 for (i
= 0; i
< 256; i
++)
4219 png_ptr
->gamma_to_1
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4224 png_ptr
->gamma_from_1
= (png_bytep
)png_malloc(png_ptr
,
4227 if (png_ptr
->screen_gamma
> 0.000001)
4228 g
= 1.0 / png_ptr
->screen_gamma
;
4231 g
= png_ptr
->gamma
; /* Probably doing rgb_to_gray */
4233 for (i
= 0; i
< 256; i
++)
4235 png_ptr
->gamma_from_1
[i
] = (png_byte
)(pow((double)i
/ 255.0,
4240 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4245 int i
, j
, shift
, num
;
4249 if (png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
4251 sig_bit
= (int)png_ptr
->sig_bit
.red
;
4253 if ((int)png_ptr
->sig_bit
.green
> sig_bit
)
4254 sig_bit
= png_ptr
->sig_bit
.green
;
4256 if ((int)png_ptr
->sig_bit
.blue
> sig_bit
)
4257 sig_bit
= png_ptr
->sig_bit
.blue
;
4261 sig_bit
= (int)png_ptr
->sig_bit
.gray
;
4265 shift
= 16 - sig_bit
;
4270 if (png_ptr
->transformations
& PNG_16_TO_8
)
4272 if (shift
< (16 - PNG_MAX_GAMMA_8
))
4273 shift
= (16 - PNG_MAX_GAMMA_8
);
4282 png_ptr
->gamma_shift
= (png_byte
)shift
;
4284 num
= (1 << (8 - shift
));
4286 if (png_ptr
->screen_gamma
> .000001)
4287 g
= 1.0 / (png_ptr
->gamma
* png_ptr
->screen_gamma
);
4291 png_ptr
->gamma_16_table
= (png_uint_16pp
)png_calloc(png_ptr
,
4292 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4294 if (png_ptr
->transformations
& (PNG_16_TO_8
| PNG_BACKGROUND
))
4297 png_uint_32 last
, max
;
4299 for (i
= 0; i
< num
; i
++)
4301 png_ptr
->gamma_16_table
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4302 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4307 for (i
= 0; i
< 256; i
++)
4309 fout
= ((double)i
+ 0.5) / 256.0;
4311 max
= (png_uint_32
)(fin
* (double)((png_uint_32
)num
<< 8));
4314 png_ptr
->gamma_16_table
[(int)(last
& (0xff >> shift
))]
4315 [(int)(last
>> (8 - shift
))] = (png_uint_16
)(
4316 (png_uint_16
)i
| ((png_uint_16
)i
<< 8));
4320 while (last
< ((png_uint_32
)num
<< 8))
4322 png_ptr
->gamma_16_table
[(int)(last
& (0xff >> shift
))]
4323 [(int)(last
>> (8 - shift
))] = (png_uint_16
)65535L;
4329 for (i
= 0; i
< num
; i
++)
4331 png_ptr
->gamma_16_table
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4332 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4334 ig
= (((png_uint_32
)i
* (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4336 for (j
= 0; j
< 256; j
++)
4338 png_ptr
->gamma_16_table
[i
][j
] =
4339 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4340 65535.0, g
) * 65535.0 + .5);
4345 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
4346 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
4347 if (png_ptr
->transformations
& (PNG_BACKGROUND
| PNG_RGB_TO_GRAY
))
4350 g
= 1.0 / (png_ptr
->gamma
);
4352 png_ptr
->gamma_16_to_1
= (png_uint_16pp
)png_calloc(png_ptr
,
4353 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4355 for (i
= 0; i
< num
; i
++)
4357 png_ptr
->gamma_16_to_1
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4358 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4360 ig
= (((png_uint_32
)i
*
4361 (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4362 for (j
= 0; j
< 256; j
++)
4364 png_ptr
->gamma_16_to_1
[i
][j
] =
4365 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4366 65535.0, g
) * 65535.0 + .5);
4370 if (png_ptr
->screen_gamma
> 0.000001)
4371 g
= 1.0 / png_ptr
->screen_gamma
;
4374 g
= png_ptr
->gamma
; /* Probably doing rgb_to_gray */
4376 png_ptr
->gamma_16_from_1
= (png_uint_16pp
)png_calloc(png_ptr
,
4377 (png_uint_32
)(num
* png_sizeof(png_uint_16p
)));
4379 for (i
= 0; i
< num
; i
++)
4381 png_ptr
->gamma_16_from_1
[i
] = (png_uint_16p
)png_malloc(png_ptr
,
4382 (png_uint_32
)(256 * png_sizeof(png_uint_16
)));
4384 ig
= (((png_uint_32
)i
*
4385 (png_uint_32
)png_gamma_shift
[shift
]) >> 4);
4387 for (j
= 0; j
< 256; j
++)
4389 png_ptr
->gamma_16_from_1
[i
][j
] =
4390 (png_uint_16
)(pow((double)(ig
+ ((png_uint_32
)j
<< 8)) /
4391 65535.0, g
) * 65535.0 + .5);
4395 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
4399 /* To do: install integer version of png_build_gamma_table here */
4402 #ifdef PNG_MNG_FEATURES_SUPPORTED
4403 /* Undoes intrapixel differencing */
4405 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
4407 png_debug(1, "in png_do_read_intrapixel");
4410 #ifdef PNG_USELESS_TESTS_SUPPORTED
4411 row
!= NULL
&& row_info
!= NULL
&&
4413 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
4415 int bytes_per_pixel
;
4416 png_uint_32 row_width
= row_info
->width
;
4417 if (row_info
->bit_depth
== 8)
4422 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
4423 bytes_per_pixel
= 3;
4425 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4426 bytes_per_pixel
= 4;
4431 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
4433 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+1))&0xff);
4434 *(rp
+2) = (png_byte
)((256 + *(rp
+2) + *(rp
+1))&0xff);
4437 else if (row_info
->bit_depth
== 16)
4442 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
4443 bytes_per_pixel
= 6;
4445 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
4446 bytes_per_pixel
= 8;
4451 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
4453 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
4454 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
4455 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
4456 png_uint_32 red
= (png_uint_32
)((s0
+ s1
+ 65536L) & 0xffffL
);
4457 png_uint_32 blue
= (png_uint_32
)((s2
+ s1
+ 65536L) & 0xffffL
);
4458 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
4459 *(rp
+1) = (png_byte
)(red
& 0xff);
4460 *(rp
+4) = (png_byte
)((blue
>> 8) & 0xff);
4461 *(rp
+5) = (png_byte
)(blue
& 0xff);
4466 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4467 #endif /* PNG_READ_SUPPORTED */