2 /* pngrtran.c - transforms the data in a row for PNG readers
4 * Last changed in libpng 1.6.2 [April 25, 2013]
5 * Copyright (c) 1998-2013 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
21 #ifdef PNG_READ_SUPPORTED
23 /* Set the action on getting a CRC error for an ancillary or critical chunk. */
25 png_set_crc_action(png_structrp png_ptr
, int crit_action
, int ancil_action
)
27 png_debug(1, "in png_set_crc_action");
32 /* Tell libpng how we react to CRC errors in critical chunks */
35 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
38 case PNG_CRC_WARN_USE
: /* Warn/use data */
39 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
40 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
;
43 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
44 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
45 png_ptr
->flags
|= PNG_FLAG_CRC_CRITICAL_USE
|
46 PNG_FLAG_CRC_CRITICAL_IGNORE
;
49 case PNG_CRC_WARN_DISCARD
: /* Not a valid action for critical data */
51 "Can't discard critical data on CRC error");
52 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
56 png_ptr
->flags
&= ~PNG_FLAG_CRC_CRITICAL_MASK
;
60 /* Tell libpng how we react to CRC errors in ancillary chunks */
63 case PNG_CRC_NO_CHANGE
: /* Leave setting as is */
66 case PNG_CRC_WARN_USE
: /* Warn/use data */
67 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
68 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
;
71 case PNG_CRC_QUIET_USE
: /* Quiet/use data */
72 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
73 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_USE
|
74 PNG_FLAG_CRC_ANCILLARY_NOWARN
;
77 case PNG_CRC_ERROR_QUIT
: /* Error/quit */
78 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
79 png_ptr
->flags
|= PNG_FLAG_CRC_ANCILLARY_NOWARN
;
82 case PNG_CRC_WARN_DISCARD
: /* Warn/discard data */
86 png_ptr
->flags
&= ~PNG_FLAG_CRC_ANCILLARY_MASK
;
91 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
92 /* Is it OK to set a transformation now? Only if png_start_read_image or
93 * png_read_update_info have not been called. It is not necessary for the IHDR
94 * to have been read in all cases, the parameter allows for this check too.
97 png_rtran_ok(png_structrp png_ptr
, int need_IHDR
)
101 if (png_ptr
->flags
& PNG_FLAG_ROW_INIT
)
102 png_app_error(png_ptr
,
103 "invalid after png_start_read_image or png_read_update_info");
105 else if (need_IHDR
&& (png_ptr
->mode
& PNG_HAVE_IHDR
) == 0)
106 png_app_error(png_ptr
, "invalid before the PNG header has been read");
110 /* Turn on failure to initialize correctly for all transforms. */
111 png_ptr
->flags
|= PNG_FLAG_DETECT_UNINITIALIZED
;
117 return 0; /* no png_error possible! */
121 #ifdef PNG_READ_BACKGROUND_SUPPORTED
122 /* Handle alpha and tRNS via a background color */
124 png_set_background_fixed(png_structrp png_ptr
,
125 png_const_color_16p background_color
, int background_gamma_code
,
126 int need_expand
, png_fixed_point background_gamma
)
128 png_debug(1, "in png_set_background_fixed");
130 if (!png_rtran_ok(png_ptr
, 0) || background_color
== NULL
)
133 if (background_gamma_code
== PNG_BACKGROUND_GAMMA_UNKNOWN
)
135 png_warning(png_ptr
, "Application must supply a known background gamma");
139 png_ptr
->transformations
|= PNG_COMPOSE
| PNG_STRIP_ALPHA
;
140 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
141 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
143 png_ptr
->background
= *background_color
;
144 png_ptr
->background_gamma
= background_gamma
;
145 png_ptr
->background_gamma_type
= (png_byte
)(background_gamma_code
);
147 png_ptr
->transformations
|= PNG_BACKGROUND_EXPAND
;
149 png_ptr
->transformations
&= ~PNG_BACKGROUND_EXPAND
;
152 # ifdef PNG_FLOATING_POINT_SUPPORTED
154 png_set_background(png_structrp png_ptr
,
155 png_const_color_16p background_color
, int background_gamma_code
,
156 int need_expand
, double background_gamma
)
158 png_set_background_fixed(png_ptr
, background_color
, background_gamma_code
,
159 need_expand
, png_fixed(png_ptr
, background_gamma
, "png_set_background"));
161 # endif /* FLOATING_POINT */
162 #endif /* READ_BACKGROUND */
164 /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
165 * one that pngrtran does first (scale) happens. This is necessary to allow the
166 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
168 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
170 png_set_scale_16(png_structrp png_ptr
)
172 png_debug(1, "in png_set_scale_16");
174 if (!png_rtran_ok(png_ptr
, 0))
177 png_ptr
->transformations
|= PNG_SCALE_16_TO_8
;
181 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
182 /* Chop 16-bit depth files to 8-bit depth */
184 png_set_strip_16(png_structrp png_ptr
)
186 png_debug(1, "in png_set_strip_16");
188 if (!png_rtran_ok(png_ptr
, 0))
191 png_ptr
->transformations
|= PNG_16_TO_8
;
195 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
197 png_set_strip_alpha(png_structrp png_ptr
)
199 png_debug(1, "in png_set_strip_alpha");
201 if (!png_rtran_ok(png_ptr
, 0))
204 png_ptr
->transformations
|= PNG_STRIP_ALPHA
;
208 #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
209 static png_fixed_point
210 translate_gamma_flags(png_structrp png_ptr
, png_fixed_point output_gamma
,
213 /* Check for flag values. The main reason for having the old Mac value as a
214 * flag is that it is pretty near impossible to work out what the correct
215 * value is from Apple documentation - a working Mac system is needed to
216 * discover the value!
218 if (output_gamma
== PNG_DEFAULT_sRGB
||
219 output_gamma
== PNG_FP_1
/ PNG_DEFAULT_sRGB
)
221 /* If there is no sRGB support this just sets the gamma to the standard
222 * sRGB value. (This is a side effect of using this function!)
224 # ifdef PNG_READ_sRGB_SUPPORTED
225 png_ptr
->flags
|= PNG_FLAG_ASSUME_sRGB
;
230 output_gamma
= PNG_GAMMA_sRGB
;
232 output_gamma
= PNG_GAMMA_sRGB_INVERSE
;
235 else if (output_gamma
== PNG_GAMMA_MAC_18
||
236 output_gamma
== PNG_FP_1
/ PNG_GAMMA_MAC_18
)
239 output_gamma
= PNG_GAMMA_MAC_OLD
;
241 output_gamma
= PNG_GAMMA_MAC_INVERSE
;
247 # ifdef PNG_FLOATING_POINT_SUPPORTED
248 static png_fixed_point
249 convert_gamma_value(png_structrp png_ptr
, double output_gamma
)
251 /* The following silently ignores cases where fixed point (times 100,000)
252 * gamma values are passed to the floating point API. This is safe and it
253 * means the fixed point constants work just fine with the floating point
254 * API. The alternative would just lead to undetected errors and spurious
255 * bug reports. Negative values fail inside the _fixed API unless they
256 * correspond to the flag values.
258 if (output_gamma
> 0 && output_gamma
< 128)
259 output_gamma
*= PNG_FP_1
;
261 /* This preserves -1 and -2 exactly: */
262 output_gamma
= floor(output_gamma
+ .5);
264 if (output_gamma
> PNG_FP_MAX
|| output_gamma
< PNG_FP_MIN
)
265 png_fixed_error(png_ptr
, "gamma value");
267 return (png_fixed_point
)output_gamma
;
270 #endif /* READ_ALPHA_MODE || READ_GAMMA */
272 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
274 png_set_alpha_mode_fixed(png_structrp png_ptr
, int mode
,
275 png_fixed_point output_gamma
)
278 png_fixed_point file_gamma
;
280 png_debug(1, "in png_set_alpha_mode");
282 if (!png_rtran_ok(png_ptr
, 0))
285 output_gamma
= translate_gamma_flags(png_ptr
, output_gamma
, 1/*screen*/);
287 /* Validate the value to ensure it is in a reasonable range. The value
288 * is expected to be 1 or greater, but this range test allows for some
289 * viewing correction values. The intent is to weed out users of this API
290 * who use the inverse of the gamma value accidentally! Since some of these
291 * values are reasonable this may have to be changed.
293 if (output_gamma
< 70000 || output_gamma
> 300000)
294 png_error(png_ptr
, "output gamma out of expected range");
296 /* The default file gamma is the inverse of the output gamma; the output
297 * gamma may be changed below so get the file value first:
299 file_gamma
= png_reciprocal(output_gamma
);
301 /* There are really 8 possibilities here, composed of any combination
304 * premultiply the color channels
305 * do not encode non-opaque pixels
306 * encode the alpha as well as the color channels
308 * The differences disappear if the input/output ('screen') gamma is 1.0,
309 * because then the encoding is a no-op and there is only the choice of
310 * premultiplying the color channels or not.
312 * png_set_alpha_mode and png_set_background interact because both use
313 * png_compose to do the work. Calling both is only useful when
314 * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
315 * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
319 case PNG_ALPHA_PNG
: /* default: png standard */
320 /* No compose, but it may be set by png_set_background! */
321 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
322 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
325 case PNG_ALPHA_ASSOCIATED
: /* color channels premultiplied */
327 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
328 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
329 /* The output is linear: */
330 output_gamma
= PNG_FP_1
;
333 case PNG_ALPHA_OPTIMIZED
: /* associated, non-opaque pixels linear */
335 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
336 png_ptr
->flags
|= PNG_FLAG_OPTIMIZE_ALPHA
;
337 /* output_gamma records the encoding of opaque pixels! */
340 case PNG_ALPHA_BROKEN
: /* associated, non-linear, alpha encoded */
342 png_ptr
->transformations
|= PNG_ENCODE_ALPHA
;
343 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
347 png_error(png_ptr
, "invalid alpha mode");
350 /* Only set the default gamma if the file gamma has not been set (this has
351 * the side effect that the gamma in a second call to png_set_alpha_mode will
354 if (png_ptr
->colorspace
.gamma
== 0)
356 png_ptr
->colorspace
.gamma
= file_gamma
;
357 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
360 /* But always set the output gamma: */
361 png_ptr
->screen_gamma
= output_gamma
;
363 /* Finally, if pre-multiplying, set the background fields to achieve the
368 /* And obtain alpha pre-multiplication by composing on black: */
369 memset(&png_ptr
->background
, 0, (sizeof png_ptr
->background
));
370 png_ptr
->background_gamma
= png_ptr
->colorspace
.gamma
; /* just in case */
371 png_ptr
->background_gamma_type
= PNG_BACKGROUND_GAMMA_FILE
;
372 png_ptr
->transformations
&= ~PNG_BACKGROUND_EXPAND
;
374 if (png_ptr
->transformations
& PNG_COMPOSE
)
376 "conflicting calls to set alpha mode and background");
378 png_ptr
->transformations
|= PNG_COMPOSE
;
382 # ifdef PNG_FLOATING_POINT_SUPPORTED
384 png_set_alpha_mode(png_structrp png_ptr
, int mode
, double output_gamma
)
386 png_set_alpha_mode_fixed(png_ptr
, mode
, convert_gamma_value(png_ptr
,
392 #ifdef PNG_READ_QUANTIZE_SUPPORTED
393 /* Dither file to 8-bit. Supply a palette, the current number
394 * of elements in the palette, the maximum number of elements
395 * allowed, and a histogram if possible. If the current number
396 * of colors is greater then the maximum number, the palette will be
397 * modified to fit in the maximum number. "full_quantize" indicates
398 * whether we need a quantizing cube set up for RGB images, or if we
399 * simply are reducing the number of colors in a paletted image.
402 typedef struct png_dsort_struct
404 struct png_dsort_struct
* next
;
408 typedef png_dsort
* png_dsortp
;
409 typedef png_dsort
* * png_dsortpp
;
412 png_set_quantize(png_structrp png_ptr
, png_colorp palette
,
413 int num_palette
, int maximum_colors
, png_const_uint_16p histogram
,
416 png_debug(1, "in png_set_quantize");
418 if (!png_rtran_ok(png_ptr
, 0))
421 png_ptr
->transformations
|= PNG_QUANTIZE
;
427 png_ptr
->quantize_index
= (png_bytep
)png_malloc(png_ptr
,
428 (png_uint_32
)(num_palette
* (sizeof (png_byte
))));
429 for (i
= 0; i
< num_palette
; i
++)
430 png_ptr
->quantize_index
[i
] = (png_byte
)i
;
433 if (num_palette
> maximum_colors
)
435 if (histogram
!= NULL
)
437 /* This is easy enough, just throw out the least used colors.
438 * Perhaps not the best solution, but good enough.
443 /* Initialize an array to sort colors */
444 png_ptr
->quantize_sort
= (png_bytep
)png_malloc(png_ptr
,
445 (png_uint_32
)(num_palette
* (sizeof (png_byte
))));
447 /* Initialize the quantize_sort array */
448 for (i
= 0; i
< num_palette
; i
++)
449 png_ptr
->quantize_sort
[i
] = (png_byte
)i
;
451 /* Find the least used palette entries by starting a
452 * bubble sort, and running it until we have sorted
453 * out enough colors. Note that we don't care about
454 * sorting all the colors, just finding which are
458 for (i
= num_palette
- 1; i
>= maximum_colors
; i
--)
460 int done
; /* To stop early if the list is pre-sorted */
464 for (j
= 0; j
< i
; j
++)
466 if (histogram
[png_ptr
->quantize_sort
[j
]]
467 < histogram
[png_ptr
->quantize_sort
[j
+ 1]])
471 t
= png_ptr
->quantize_sort
[j
];
472 png_ptr
->quantize_sort
[j
] = png_ptr
->quantize_sort
[j
+ 1];
473 png_ptr
->quantize_sort
[j
+ 1] = t
;
482 /* Swap the palette around, and set up a table, if necessary */
487 /* Put all the useful colors within the max, but don't
490 for (i
= 0; i
< maximum_colors
; i
++)
492 if ((int)png_ptr
->quantize_sort
[i
] >= maximum_colors
)
496 while ((int)png_ptr
->quantize_sort
[j
] >= maximum_colors
);
498 palette
[i
] = palette
[j
];
506 /* Move all the used colors inside the max limit, and
507 * develop a translation table.
509 for (i
= 0; i
< maximum_colors
; i
++)
511 /* Only move the colors we need to */
512 if ((int)png_ptr
->quantize_sort
[i
] >= maximum_colors
)
518 while ((int)png_ptr
->quantize_sort
[j
] >= maximum_colors
);
520 tmp_color
= palette
[j
];
521 palette
[j
] = palette
[i
];
522 palette
[i
] = tmp_color
;
523 /* Indicate where the color went */
524 png_ptr
->quantize_index
[j
] = (png_byte
)i
;
525 png_ptr
->quantize_index
[i
] = (png_byte
)j
;
529 /* Find closest color for those colors we are not using */
530 for (i
= 0; i
< num_palette
; i
++)
532 if ((int)png_ptr
->quantize_index
[i
] >= maximum_colors
)
534 int min_d
, k
, min_k
, d_index
;
536 /* Find the closest color to one we threw out */
537 d_index
= png_ptr
->quantize_index
[i
];
538 min_d
= PNG_COLOR_DIST(palette
[d_index
], palette
[0]);
539 for (k
= 1, min_k
= 0; k
< maximum_colors
; k
++)
543 d
= PNG_COLOR_DIST(palette
[d_index
], palette
[k
]);
551 /* Point to closest color */
552 png_ptr
->quantize_index
[i
] = (png_byte
)min_k
;
556 png_free(png_ptr
, png_ptr
->quantize_sort
);
557 png_ptr
->quantize_sort
= NULL
;
561 /* This is much harder to do simply (and quickly). Perhaps
562 * we need to go through a median cut routine, but those
563 * don't always behave themselves with only a few colors
564 * as input. So we will just find the closest two colors,
565 * and throw out one of them (chosen somewhat randomly).
566 * [We don't understand this at all, so if someone wants to
567 * work on improving it, be our guest - AED, GRP]
577 /* Initialize palette index arrays */
578 png_ptr
->index_to_palette
= (png_bytep
)png_malloc(png_ptr
,
579 (png_uint_32
)(num_palette
* (sizeof (png_byte
))));
580 png_ptr
->palette_to_index
= (png_bytep
)png_malloc(png_ptr
,
581 (png_uint_32
)(num_palette
* (sizeof (png_byte
))));
583 /* Initialize the sort array */
584 for (i
= 0; i
< num_palette
; i
++)
586 png_ptr
->index_to_palette
[i
] = (png_byte
)i
;
587 png_ptr
->palette_to_index
[i
] = (png_byte
)i
;
590 hash
= (png_dsortpp
)png_calloc(png_ptr
, (png_uint_32
)(769 *
591 (sizeof (png_dsortp
))));
593 num_new_palette
= num_palette
;
595 /* Initial wild guess at how far apart the farthest pixel
596 * pair we will be eliminating will be. Larger
597 * numbers mean more areas will be allocated, Smaller
598 * numbers run the risk of not saving enough data, and
599 * having to do this all over again.
601 * I have not done extensive checking on this number.
605 while (num_new_palette
> maximum_colors
)
607 for (i
= 0; i
< num_new_palette
- 1; i
++)
611 for (j
= i
+ 1; j
< num_new_palette
; j
++)
615 d
= PNG_COLOR_DIST(palette
[i
], palette
[j
]);
620 t
= (png_dsortp
)png_malloc_warn(png_ptr
,
621 (png_uint_32
)(sizeof (png_dsort
)));
627 t
->left
= (png_byte
)i
;
628 t
->right
= (png_byte
)j
;
637 for (i
= 0; i
<= max_d
; i
++)
643 for (p
= hash
[i
]; p
; p
= p
->next
)
645 if ((int)png_ptr
->index_to_palette
[p
->left
]
647 (int)png_ptr
->index_to_palette
[p
->right
]
652 if (num_new_palette
& 0x01)
664 palette
[png_ptr
->index_to_palette
[j
]]
665 = palette
[num_new_palette
];
670 for (k
= 0; k
< num_palette
; k
++)
672 if (png_ptr
->quantize_index
[k
] ==
673 png_ptr
->index_to_palette
[j
])
674 png_ptr
->quantize_index
[k
] =
675 png_ptr
->index_to_palette
[next_j
];
677 if ((int)png_ptr
->quantize_index
[k
] ==
679 png_ptr
->quantize_index
[k
] =
680 png_ptr
->index_to_palette
[j
];
684 png_ptr
->index_to_palette
[png_ptr
->palette_to_index
685 [num_new_palette
]] = png_ptr
->index_to_palette
[j
];
687 png_ptr
->palette_to_index
[png_ptr
->index_to_palette
[j
]]
688 = png_ptr
->palette_to_index
[num_new_palette
];
690 png_ptr
->index_to_palette
[j
] =
691 (png_byte
)num_new_palette
;
693 png_ptr
->palette_to_index
[num_new_palette
] =
696 if (num_new_palette
<= maximum_colors
)
699 if (num_new_palette
<= maximum_colors
)
704 for (i
= 0; i
< 769; i
++)
708 png_dsortp p
= hash
[i
];
712 png_free(png_ptr
, p
);
720 png_free(png_ptr
, hash
);
721 png_free(png_ptr
, png_ptr
->palette_to_index
);
722 png_free(png_ptr
, png_ptr
->index_to_palette
);
723 png_ptr
->palette_to_index
= NULL
;
724 png_ptr
->index_to_palette
= NULL
;
726 num_palette
= maximum_colors
;
728 if (png_ptr
->palette
== NULL
)
730 png_ptr
->palette
= palette
;
732 png_ptr
->num_palette
= (png_uint_16
)num_palette
;
738 int total_bits
= PNG_QUANTIZE_RED_BITS
+ PNG_QUANTIZE_GREEN_BITS
+
739 PNG_QUANTIZE_BLUE_BITS
;
740 int num_red
= (1 << PNG_QUANTIZE_RED_BITS
);
741 int num_green
= (1 << PNG_QUANTIZE_GREEN_BITS
);
742 int num_blue
= (1 << PNG_QUANTIZE_BLUE_BITS
);
743 png_size_t num_entries
= ((png_size_t
)1 << total_bits
);
745 png_ptr
->palette_lookup
= (png_bytep
)png_calloc(png_ptr
,
746 (png_uint_32
)(num_entries
* (sizeof (png_byte
))));
748 distance
= (png_bytep
)png_malloc(png_ptr
, (png_uint_32
)(num_entries
*
749 (sizeof (png_byte
))));
751 memset(distance
, 0xff, num_entries
* (sizeof (png_byte
)));
753 for (i
= 0; i
< num_palette
; i
++)
756 int r
= (palette
[i
].red
>> (8 - PNG_QUANTIZE_RED_BITS
));
757 int g
= (palette
[i
].green
>> (8 - PNG_QUANTIZE_GREEN_BITS
));
758 int b
= (palette
[i
].blue
>> (8 - PNG_QUANTIZE_BLUE_BITS
));
760 for (ir
= 0; ir
< num_red
; ir
++)
762 /* int dr = abs(ir - r); */
763 int dr
= ((ir
> r
) ? ir
- r
: r
- ir
);
764 int index_r
= (ir
<< (PNG_QUANTIZE_BLUE_BITS
+
765 PNG_QUANTIZE_GREEN_BITS
));
767 for (ig
= 0; ig
< num_green
; ig
++)
769 /* int dg = abs(ig - g); */
770 int dg
= ((ig
> g
) ? ig
- g
: g
- ig
);
772 int dm
= ((dr
> dg
) ? dr
: dg
);
773 int index_g
= index_r
| (ig
<< PNG_QUANTIZE_BLUE_BITS
);
775 for (ib
= 0; ib
< num_blue
; ib
++)
777 int d_index
= index_g
| ib
;
778 /* int db = abs(ib - b); */
779 int db
= ((ib
> b
) ? ib
- b
: b
- ib
);
780 int dmax
= ((dm
> db
) ? dm
: db
);
781 int d
= dmax
+ dt
+ db
;
783 if (d
< (int)distance
[d_index
])
785 distance
[d_index
] = (png_byte
)d
;
786 png_ptr
->palette_lookup
[d_index
] = (png_byte
)i
;
793 png_free(png_ptr
, distance
);
796 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
798 #ifdef PNG_READ_GAMMA_SUPPORTED
800 png_set_gamma_fixed(png_structrp png_ptr
, png_fixed_point scrn_gamma
,
801 png_fixed_point file_gamma
)
803 png_debug(1, "in png_set_gamma_fixed");
805 if (!png_rtran_ok(png_ptr
, 0))
808 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
809 scrn_gamma
= translate_gamma_flags(png_ptr
, scrn_gamma
, 1/*screen*/);
810 file_gamma
= translate_gamma_flags(png_ptr
, file_gamma
, 0/*file*/);
812 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
813 * premultiplied alpha support; this actually hides an undocumented feature
814 * of the previous implementation which allowed gamma processing to be
815 * disabled in background handling. There is no evidence (so far) that this
816 * was being used; however, png_set_background itself accepted and must still
817 * accept '0' for the gamma value it takes, because it isn't always used.
819 * Since this is an API change (albeit a very minor one that removes an
820 * undocumented API feature) the following checks were only enabled in
824 png_error(png_ptr
, "invalid file gamma in png_set_gamma");
827 png_error(png_ptr
, "invalid screen gamma in png_set_gamma");
829 /* Set the gamma values unconditionally - this overrides the value in the PNG
830 * file if a gAMA chunk was present. png_set_alpha_mode provides a
831 * different, easier, way to default the file gamma.
833 png_ptr
->colorspace
.gamma
= file_gamma
;
834 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
835 png_ptr
->screen_gamma
= scrn_gamma
;
838 # ifdef PNG_FLOATING_POINT_SUPPORTED
840 png_set_gamma(png_structrp png_ptr
, double scrn_gamma
, double file_gamma
)
842 png_set_gamma_fixed(png_ptr
, convert_gamma_value(png_ptr
, scrn_gamma
),
843 convert_gamma_value(png_ptr
, file_gamma
));
845 # endif /* FLOATING_POINT_SUPPORTED */
846 #endif /* READ_GAMMA */
848 #ifdef PNG_READ_EXPAND_SUPPORTED
849 /* Expand paletted images to RGB, expand grayscale images of
850 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
854 png_set_expand(png_structrp png_ptr
)
856 png_debug(1, "in png_set_expand");
858 if (!png_rtran_ok(png_ptr
, 0))
861 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
864 /* GRR 19990627: the following three functions currently are identical
865 * to png_set_expand(). However, it is entirely reasonable that someone
866 * might wish to expand an indexed image to RGB but *not* expand a single,
867 * fully transparent palette entry to a full alpha channel--perhaps instead
868 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
869 * the transparent color with a particular RGB value, or drop tRNS entirely.
870 * IOW, a future version of the library may make the transformations flag
871 * a bit more fine-grained, with separate bits for each of these three
874 * More to the point, these functions make it obvious what libpng will be
875 * doing, whereas "expand" can (and does) mean any number of things.
877 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
878 * to expand only the sample depth but not to expand the tRNS to alpha
879 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
882 /* Expand paletted images to RGB. */
884 png_set_palette_to_rgb(png_structrp png_ptr
)
886 png_debug(1, "in png_set_palette_to_rgb");
888 if (!png_rtran_ok(png_ptr
, 0))
891 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
894 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
896 png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr
)
898 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
900 if (!png_rtran_ok(png_ptr
, 0))
903 png_ptr
->transformations
|= PNG_EXPAND
;
907 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
908 /* Deprecated as of libpng-1.2.9 */
910 png_set_gray_1_2_4_to_8(png_structp png_ptr
)
912 png_debug(1, "in png_set_gray_1_2_4_to_8");
917 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
919 #endif /* __AROS__ */
921 /* Expand tRNS chunks to alpha channels. */
923 png_set_tRNS_to_alpha(png_structrp png_ptr
)
925 png_debug(1, "in png_set_tRNS_to_alpha");
927 if (!png_rtran_ok(png_ptr
, 0))
930 png_ptr
->transformations
|= (PNG_EXPAND
| PNG_EXPAND_tRNS
);
932 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
934 #ifdef PNG_READ_EXPAND_16_SUPPORTED
935 /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
936 * it may not work correctly.)
939 png_set_expand_16(png_structrp png_ptr
)
941 png_debug(1, "in png_set_expand_16");
943 if (!png_rtran_ok(png_ptr
, 0))
946 png_ptr
->transformations
|= (PNG_EXPAND_16
| PNG_EXPAND
| PNG_EXPAND_tRNS
);
950 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
952 png_set_gray_to_rgb(png_structrp png_ptr
)
954 png_debug(1, "in png_set_gray_to_rgb");
956 if (!png_rtran_ok(png_ptr
, 0))
959 /* Because rgb must be 8 bits or more: */
960 png_set_expand_gray_1_2_4_to_8(png_ptr
);
961 png_ptr
->transformations
|= PNG_GRAY_TO_RGB
;
965 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
967 png_set_rgb_to_gray_fixed(png_structrp png_ptr
, int error_action
,
968 png_fixed_point red
, png_fixed_point green
)
970 png_debug(1, "in png_set_rgb_to_gray");
972 /* Need the IHDR here because of the check on color_type below. */
974 if (!png_rtran_ok(png_ptr
, 1))
979 case PNG_ERROR_ACTION_NONE
:
980 png_ptr
->transformations
|= PNG_RGB_TO_GRAY
;
983 case PNG_ERROR_ACTION_WARN
:
984 png_ptr
->transformations
|= PNG_RGB_TO_GRAY_WARN
;
987 case PNG_ERROR_ACTION_ERROR
:
988 png_ptr
->transformations
|= PNG_RGB_TO_GRAY_ERR
;
992 png_error(png_ptr
, "invalid error action to rgb_to_gray");
996 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
997 #ifdef PNG_READ_EXPAND_SUPPORTED
998 png_ptr
->transformations
|= PNG_EXPAND
;
1001 /* Make this an error in 1.6 because otherwise the application may assume
1002 * that it just worked and get a memory overwrite.
1005 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
1007 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
1011 if (red
>= 0 && green
>= 0 && red
+ green
<= PNG_FP_1
)
1013 png_uint_16 red_int
, green_int
;
1015 /* NOTE: this calculation does not round, but this behavior is retained
1016 * for consistency, the inaccuracy is very small. The code here always
1017 * overwrites the coefficients, regardless of whether they have been
1018 * defaulted or set already.
1020 red_int
= (png_uint_16
)(((png_uint_32
)red
*32768)/100000);
1021 green_int
= (png_uint_16
)(((png_uint_32
)green
*32768)/100000);
1023 png_ptr
->rgb_to_gray_red_coeff
= red_int
;
1024 png_ptr
->rgb_to_gray_green_coeff
= green_int
;
1025 png_ptr
->rgb_to_gray_coefficients_set
= 1;
1030 if (red
>= 0 && green
>= 0)
1031 png_app_warning(png_ptr
,
1032 "ignoring out of range rgb_to_gray coefficients");
1034 /* Use the defaults, from the cHRM chunk if set, else the historical
1035 * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
1036 * png_do_rgb_to_gray for more discussion of the values. In this case
1037 * the coefficients are not marked as 'set' and are not overwritten if
1038 * something has already provided a default.
1040 if (png_ptr
->rgb_to_gray_red_coeff
== 0 &&
1041 png_ptr
->rgb_to_gray_green_coeff
== 0)
1043 png_ptr
->rgb_to_gray_red_coeff
= 6968;
1044 png_ptr
->rgb_to_gray_green_coeff
= 23434;
1045 /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
1051 #ifdef PNG_FLOATING_POINT_SUPPORTED
1052 /* Convert a RGB image to a grayscale of the same width. This allows us,
1053 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1057 png_set_rgb_to_gray(png_structrp png_ptr
, int error_action
, double red
,
1060 png_set_rgb_to_gray_fixed(png_ptr
, error_action
,
1061 png_fixed(png_ptr
, red
, "rgb to gray red coefficient"),
1062 png_fixed(png_ptr
, green
, "rgb to gray green coefficient"));
1064 #endif /* FLOATING POINT */
1066 #endif /* RGB_TO_GRAY */
1068 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
1069 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1071 png_set_read_user_transform_fn(png_structrp png_ptr
, png_user_transform_ptr
1072 read_user_transform_fn
)
1074 png_debug(1, "in png_set_read_user_transform_fn");
1076 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1077 png_ptr
->transformations
|= PNG_USER_TRANSFORM
;
1078 png_ptr
->read_user_transform_fn
= read_user_transform_fn
;
1083 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
1084 #ifdef PNG_READ_GAMMA_SUPPORTED
1085 /* In the case of gamma transformations only do transformations on images where
1086 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
1087 * slows things down slightly, and also needlessly introduces small errors.
1089 static int /* PRIVATE */
1090 png_gamma_threshold(png_fixed_point screen_gamma
, png_fixed_point file_gamma
)
1092 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1093 * correction as a difference of the overall transform from 1.0
1095 * We want to compare the threshold with s*f - 1, if we get
1096 * overflow here it is because of wacky gamma values so we
1097 * turn on processing anyway.
1099 png_fixed_point gtest
;
1100 return !png_muldiv(>est
, screen_gamma
, file_gamma
, PNG_FP_1
) ||
1101 png_gamma_significant(gtest
);
1105 /* Initialize everything needed for the read. This includes modifying
1109 /*For the moment 'png_init_palette_transformations' and
1110 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1111 * The intent is that these two routines should have palette or rgb operations
1112 * extracted from 'png_init_read_transformations'.
1114 static void /* PRIVATE */
1115 png_init_palette_transformations(png_structrp png_ptr
)
1117 /* Called to handle the (input) palette case. In png_do_read_transformations
1118 * the first step is to expand the palette if requested, so this code must
1119 * take care to only make changes that are invariant with respect to the
1120 * palette expansion, or only do them if there is no expansion.
1122 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1125 int input_has_alpha
= 0;
1126 int input_has_transparency
= 0;
1128 if (png_ptr
->num_trans
> 0)
1132 /* Ignore if all the entries are opaque (unlikely!) */
1133 for (i
=0; i
<png_ptr
->num_trans
; ++i
)
1134 if (png_ptr
->trans_alpha
[i
] == 255)
1136 else if (png_ptr
->trans_alpha
[i
] == 0)
1137 input_has_transparency
= 1;
1139 input_has_alpha
= 1;
1142 /* If no alpha we can optimize. */
1143 if (!input_has_alpha
)
1145 /* Any alpha means background and associative alpha processing is
1146 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1147 * and ENCODE_ALPHA are irrelevant.
1149 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
1150 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
1152 if (!input_has_transparency
)
1153 png_ptr
->transformations
&= ~(PNG_COMPOSE
| PNG_BACKGROUND_EXPAND
);
1156 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1157 /* png_set_background handling - deals with the complexity of whether the
1158 * background color is in the file format or the screen format in the case
1159 * where an 'expand' will happen.
1162 /* The following code cannot be entered in the alpha pre-multiplication case
1163 * because PNG_BACKGROUND_EXPAND is cancelled below.
1165 if ((png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
1166 (png_ptr
->transformations
& PNG_EXPAND
))
1169 png_ptr
->background
.red
=
1170 png_ptr
->palette
[png_ptr
->background
.index
].red
;
1171 png_ptr
->background
.green
=
1172 png_ptr
->palette
[png_ptr
->background
.index
].green
;
1173 png_ptr
->background
.blue
=
1174 png_ptr
->palette
[png_ptr
->background
.index
].blue
;
1176 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1177 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
1179 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
1181 /* Invert the alpha channel (in tRNS) unless the pixels are
1182 * going to be expanded, in which case leave it for later
1184 int i
, istop
= png_ptr
->num_trans
;
1186 for (i
=0; i
<istop
; i
++)
1187 png_ptr
->trans_alpha
[i
] = (png_byte
)(255 -
1188 png_ptr
->trans_alpha
[i
]);
1191 #endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
1193 } /* background expand and (therefore) no alpha association. */
1194 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1197 static void /* PRIVATE */
1198 png_init_rgb_transformations(png_structrp png_ptr
)
1200 /* Added to libpng-1.5.4: check the color type to determine whether there
1201 * is any alpha or transparency in the image and simply cancel the
1202 * background and alpha mode stuff if there isn't.
1204 int input_has_alpha
= (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0;
1205 int input_has_transparency
= png_ptr
->num_trans
> 0;
1207 /* If no alpha we can optimize. */
1208 if (!input_has_alpha
)
1210 /* Any alpha means background and associative alpha processing is
1211 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1212 * and ENCODE_ALPHA are irrelevant.
1214 # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1215 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
1216 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
1219 if (!input_has_transparency
)
1220 png_ptr
->transformations
&= ~(PNG_COMPOSE
| PNG_BACKGROUND_EXPAND
);
1223 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1224 /* png_set_background handling - deals with the complexity of whether the
1225 * background color is in the file format or the screen format in the case
1226 * where an 'expand' will happen.
1229 /* The following code cannot be entered in the alpha pre-multiplication case
1230 * because PNG_BACKGROUND_EXPAND is cancelled below.
1232 if ((png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
1233 (png_ptr
->transformations
& PNG_EXPAND
) &&
1234 !(png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
))
1235 /* i.e., GRAY or GRAY_ALPHA */
1238 /* Expand background and tRNS chunks */
1239 int gray
= png_ptr
->background
.gray
;
1240 int trans_gray
= png_ptr
->trans_color
.gray
;
1242 switch (png_ptr
->bit_depth
)
1262 /* FALL THROUGH (Already 8 bits) */
1265 /* Already a full 16 bits */
1269 png_ptr
->background
.red
= png_ptr
->background
.green
=
1270 png_ptr
->background
.blue
= (png_uint_16
)gray
;
1272 if (!(png_ptr
->transformations
& PNG_EXPAND_tRNS
))
1274 png_ptr
->trans_color
.red
= png_ptr
->trans_color
.green
=
1275 png_ptr
->trans_color
.blue
= (png_uint_16
)trans_gray
;
1278 } /* background expand and (therefore) no alpha association. */
1279 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1283 png_init_read_transformations(png_structrp png_ptr
)
1285 png_debug(1, "in png_init_read_transformations");
1287 /* This internal function is called from png_read_start_row in pngrutil.c
1288 * and it is called before the 'rowbytes' calculation is done, so the code
1289 * in here can change or update the transformations flags.
1291 * First do updates that do not depend on the details of the PNG image data
1295 #ifdef PNG_READ_GAMMA_SUPPORTED
1296 /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
1297 * png_set_alpha_mode and this is another source for a default file gamma so
1298 * the test needs to be performed later - here. In addition prior to 1.5.4
1299 * the tests were repeated for the PALETTE color type here - this is no
1300 * longer necessary (and doesn't seem to have been necessary before.)
1303 /* The following temporary indicates if overall gamma correction is
1306 int gamma_correction
= 0;
1308 if (png_ptr
->colorspace
.gamma
!= 0) /* has been set */
1310 if (png_ptr
->screen_gamma
!= 0) /* screen set too */
1311 gamma_correction
= png_gamma_threshold(png_ptr
->colorspace
.gamma
,
1312 png_ptr
->screen_gamma
);
1315 /* Assume the output matches the input; a long time default behavior
1316 * of libpng, although the standard has nothing to say about this.
1318 png_ptr
->screen_gamma
= png_reciprocal(png_ptr
->colorspace
.gamma
);
1321 else if (png_ptr
->screen_gamma
!= 0)
1322 /* The converse - assume the file matches the screen, note that this
1323 * perhaps undesireable default can (from 1.5.4) be changed by calling
1324 * png_set_alpha_mode (even if the alpha handling mode isn't required
1325 * or isn't changed from the default.)
1327 png_ptr
->colorspace
.gamma
= png_reciprocal(png_ptr
->screen_gamma
);
1329 else /* neither are set */
1330 /* Just in case the following prevents any processing - file and screen
1331 * are both assumed to be linear and there is no way to introduce a
1332 * third gamma value other than png_set_background with 'UNIQUE', and,
1335 png_ptr
->screen_gamma
= png_ptr
->colorspace
.gamma
= PNG_FP_1
;
1337 /* We have a gamma value now. */
1338 png_ptr
->colorspace
.flags
|= PNG_COLORSPACE_HAVE_GAMMA
;
1340 /* Now turn the gamma transformation on or off as appropriate. Notice
1341 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1342 * composition may independently cause gamma correction because it needs
1343 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1344 * hasn't been specified.) In any case this flag may get turned off in
1345 * the code immediately below if the transform can be handled outside the
1348 if (gamma_correction
)
1349 png_ptr
->transformations
|= PNG_GAMMA
;
1352 png_ptr
->transformations
&= ~PNG_GAMMA
;
1356 /* Certain transformations have the effect of preventing other
1357 * transformations that happen afterward in png_do_read_transformations,
1358 * resolve the interdependencies here. From the code of
1359 * png_do_read_transformations the order is:
1361 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1362 * 2) PNG_STRIP_ALPHA (if no compose)
1363 * 3) PNG_RGB_TO_GRAY
1364 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1367 * 7) PNG_STRIP_ALPHA (if compose)
1368 * 8) PNG_ENCODE_ALPHA
1369 * 9) PNG_SCALE_16_TO_8
1371 * 11) PNG_QUANTIZE (converts to palette)
1373 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1374 * 14) PNG_INVERT_MONO
1379 * 19) PNG_FILLER (includes PNG_ADD_ALPHA)
1380 * 20) PNG_INVERT_ALPHA
1381 * 21) PNG_SWAP_ALPHA
1382 * 22) PNG_SWAP_BYTES
1383 * 23) PNG_USER_TRANSFORM [must be last]
1385 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1386 if ((png_ptr
->transformations
& PNG_STRIP_ALPHA
) &&
1387 !(png_ptr
->transformations
& PNG_COMPOSE
))
1389 /* Stripping the alpha channel happens immediately after the 'expand'
1390 * transformations, before all other transformation, so it cancels out
1391 * the alpha handling. It has the side effect negating the effect of
1392 * PNG_EXPAND_tRNS too:
1394 png_ptr
->transformations
&= ~(PNG_BACKGROUND_EXPAND
| PNG_ENCODE_ALPHA
|
1396 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
1398 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
1399 * so transparency information would remain just so long as it wasn't
1400 * expanded. This produces unexpected API changes if the set of things
1401 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1402 * documentation - which says ask for what you want, accept what you
1403 * get.) This makes the behavior consistent from 1.5.4:
1405 png_ptr
->num_trans
= 0;
1407 #endif /* STRIP_ALPHA supported, no COMPOSE */
1409 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1410 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1411 * settings will have no effect.
1413 if (!png_gamma_significant(png_ptr
->screen_gamma
))
1415 png_ptr
->transformations
&= ~PNG_ENCODE_ALPHA
;
1416 png_ptr
->flags
&= ~PNG_FLAG_OPTIMIZE_ALPHA
;
1420 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1421 /* Make sure the coefficients for the rgb to gray conversion are set
1424 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1425 png_colorspace_set_rgb_coefficients(png_ptr
);
1428 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1429 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1430 /* Detect gray background and attempt to enable optimization for
1431 * gray --> RGB case.
1433 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1434 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1435 * background color might actually be gray yet not be flagged as such.
1436 * This is not a problem for the current code, which uses
1437 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1438 * png_do_gray_to_rgb() transformation.
1440 * TODO: this code needs to be revised to avoid the complexity and
1441 * interdependencies. The color type of the background should be recorded in
1442 * png_set_background, along with the bit depth, then the code has a record
1443 * of exactly what color space the background is currently in.
1445 if (png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
)
1447 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1448 * the file was grayscale the background value is gray.
1450 if (!(png_ptr
->color_type
& PNG_COLOR_MASK_COLOR
))
1451 png_ptr
->mode
|= PNG_BACKGROUND_IS_GRAY
;
1454 else if (png_ptr
->transformations
& PNG_COMPOSE
)
1456 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1457 * so the color is in the color space of the output or png_set_alpha_mode
1458 * was called and the color is black. Ignore RGB_TO_GRAY because that
1459 * happens before GRAY_TO_RGB.
1461 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
1463 if (png_ptr
->background
.red
== png_ptr
->background
.green
&&
1464 png_ptr
->background
.red
== png_ptr
->background
.blue
)
1466 png_ptr
->mode
|= PNG_BACKGROUND_IS_GRAY
;
1467 png_ptr
->background
.gray
= png_ptr
->background
.red
;
1471 #endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1472 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
1474 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1475 * can be performed directly on the palette, and some (such as rgb to gray)
1476 * can be optimized inside the palette. This is particularly true of the
1477 * composite (background and alpha) stuff, which can be pretty much all done
1478 * in the palette even if the result is expanded to RGB or gray afterward.
1480 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1481 * earlier and the palette stuff is actually handled on the first row. This
1482 * leads to the reported bug that the palette returned by png_get_PLTE is not
1485 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1486 png_init_palette_transformations(png_ptr
);
1489 png_init_rgb_transformations(png_ptr
);
1491 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1492 defined(PNG_READ_EXPAND_16_SUPPORTED)
1493 if ((png_ptr
->transformations
& PNG_EXPAND_16
) &&
1494 (png_ptr
->transformations
& PNG_COMPOSE
) &&
1495 !(png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
1496 png_ptr
->bit_depth
!= 16)
1498 /* TODO: fix this. Because the expand_16 operation is after the compose
1499 * handling the background color must be 8, not 16, bits deep, but the
1500 * application will supply a 16-bit value so reduce it here.
1502 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1503 * present, so that case is ok (until do_expand_16 is moved.)
1505 * NOTE: this discards the low 16 bits of the user supplied background
1506 * color, but until expand_16 works properly there is no choice!
1508 # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
1509 CHOP(png_ptr
->background
.red
);
1510 CHOP(png_ptr
->background
.green
);
1511 CHOP(png_ptr
->background
.blue
);
1512 CHOP(png_ptr
->background
.gray
);
1515 #endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
1517 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1518 (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1519 defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
1520 if ((png_ptr
->transformations
& (PNG_16_TO_8
|PNG_SCALE_16_TO_8
)) &&
1521 (png_ptr
->transformations
& PNG_COMPOSE
) &&
1522 !(png_ptr
->transformations
& PNG_BACKGROUND_EXPAND
) &&
1523 png_ptr
->bit_depth
== 16)
1525 /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
1526 * component this will also happen after PNG_COMPOSE and so the background
1527 * color must be pre-expanded here.
1529 * TODO: fix this too.
1531 png_ptr
->background
.red
= (png_uint_16
)(png_ptr
->background
.red
* 257);
1532 png_ptr
->background
.green
=
1533 (png_uint_16
)(png_ptr
->background
.green
* 257);
1534 png_ptr
->background
.blue
= (png_uint_16
)(png_ptr
->background
.blue
* 257);
1535 png_ptr
->background
.gray
= (png_uint_16
)(png_ptr
->background
.gray
* 257);
1539 /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1540 * background support (see the comments in scripts/pnglibconf.dfa), this
1541 * allows pre-multiplication of the alpha channel to be implemented as
1542 * compositing on black. This is probably sub-optimal and has been done in
1543 * 1.5.4 betas simply to enable external critique and testing (i.e. to
1544 * implement the new API quickly, without lots of internal changes.)
1547 #ifdef PNG_READ_GAMMA_SUPPORTED
1548 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1549 /* Includes ALPHA_MODE */
1550 png_ptr
->background_1
= png_ptr
->background
;
1553 /* This needs to change - in the palette image case a whole set of tables are
1554 * built when it would be quicker to just calculate the correct value for
1555 * each palette entry directly. Also, the test is too tricky - why check
1556 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1557 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1558 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1559 * the gamma tables will not be built even if composition is required on a
1560 * gamma encoded value.
1562 * In 1.5.4 this is addressed below by an additional check on the individual
1563 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1566 if ((png_ptr
->transformations
& PNG_GAMMA
)
1567 || ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1568 && (png_gamma_significant(png_ptr
->colorspace
.gamma
) ||
1569 png_gamma_significant(png_ptr
->screen_gamma
)))
1570 || ((png_ptr
->transformations
& PNG_COMPOSE
)
1571 && (png_gamma_significant(png_ptr
->colorspace
.gamma
)
1572 || png_gamma_significant(png_ptr
->screen_gamma
)
1573 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1574 || (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_UNIQUE
1575 && png_gamma_significant(png_ptr
->background_gamma
))
1577 )) || ((png_ptr
->transformations
& PNG_ENCODE_ALPHA
)
1578 && png_gamma_significant(png_ptr
->screen_gamma
))
1581 png_build_gamma_table(png_ptr
, png_ptr
->bit_depth
);
1583 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1584 if (png_ptr
->transformations
& PNG_COMPOSE
)
1586 /* Issue a warning about this combination: because RGB_TO_GRAY is
1587 * optimized to do the gamma transform if present yet do_background has
1588 * to do the same thing if both options are set a
1589 * double-gamma-correction happens. This is true in all versions of
1592 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
1593 png_warning(png_ptr
,
1594 "libpng does not support gamma+background+rgb_to_gray");
1596 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1598 /* We don't get to here unless there is a tRNS chunk with non-opaque
1599 * entries - see the checking code at the start of this function.
1601 png_color back
, back_1
;
1602 png_colorp palette
= png_ptr
->palette
;
1603 int num_palette
= png_ptr
->num_palette
;
1605 if (png_ptr
->background_gamma_type
== PNG_BACKGROUND_GAMMA_FILE
)
1608 back
.red
= png_ptr
->gamma_table
[png_ptr
->background
.red
];
1609 back
.green
= png_ptr
->gamma_table
[png_ptr
->background
.green
];
1610 back
.blue
= png_ptr
->gamma_table
[png_ptr
->background
.blue
];
1612 back_1
.red
= png_ptr
->gamma_to_1
[png_ptr
->background
.red
];
1613 back_1
.green
= png_ptr
->gamma_to_1
[png_ptr
->background
.green
];
1614 back_1
.blue
= png_ptr
->gamma_to_1
[png_ptr
->background
.blue
];
1618 png_fixed_point g
, gs
;
1620 switch (png_ptr
->background_gamma_type
)
1622 case PNG_BACKGROUND_GAMMA_SCREEN
:
1623 g
= (png_ptr
->screen_gamma
);
1627 case PNG_BACKGROUND_GAMMA_FILE
:
1628 g
= png_reciprocal(png_ptr
->colorspace
.gamma
);
1629 gs
= png_reciprocal2(png_ptr
->colorspace
.gamma
,
1630 png_ptr
->screen_gamma
);
1633 case PNG_BACKGROUND_GAMMA_UNIQUE
:
1634 g
= png_reciprocal(png_ptr
->background_gamma
);
1635 gs
= png_reciprocal2(png_ptr
->background_gamma
,
1636 png_ptr
->screen_gamma
);
1639 g
= PNG_FP_1
; /* back_1 */
1640 gs
= PNG_FP_1
; /* back */
1644 if (png_gamma_significant(gs
))
1646 back
.red
= png_gamma_8bit_correct(png_ptr
->background
.red
,
1648 back
.green
= png_gamma_8bit_correct(png_ptr
->background
.green
,
1650 back
.blue
= png_gamma_8bit_correct(png_ptr
->background
.blue
,
1656 back
.red
= (png_byte
)png_ptr
->background
.red
;
1657 back
.green
= (png_byte
)png_ptr
->background
.green
;
1658 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
1661 if (png_gamma_significant(g
))
1663 back_1
.red
= png_gamma_8bit_correct(png_ptr
->background
.red
,
1665 back_1
.green
= png_gamma_8bit_correct(
1666 png_ptr
->background
.green
, g
);
1667 back_1
.blue
= png_gamma_8bit_correct(png_ptr
->background
.blue
,
1673 back_1
.red
= (png_byte
)png_ptr
->background
.red
;
1674 back_1
.green
= (png_byte
)png_ptr
->background
.green
;
1675 back_1
.blue
= (png_byte
)png_ptr
->background
.blue
;
1679 for (i
= 0; i
< num_palette
; i
++)
1681 if (i
< (int)png_ptr
->num_trans
&&
1682 png_ptr
->trans_alpha
[i
] != 0xff)
1684 if (png_ptr
->trans_alpha
[i
] == 0)
1688 else /* if (png_ptr->trans_alpha[i] != 0xff) */
1692 v
= png_ptr
->gamma_to_1
[palette
[i
].red
];
1693 png_composite(w
, v
, png_ptr
->trans_alpha
[i
], back_1
.red
);
1694 palette
[i
].red
= png_ptr
->gamma_from_1
[w
];
1696 v
= png_ptr
->gamma_to_1
[palette
[i
].green
];
1697 png_composite(w
, v
, png_ptr
->trans_alpha
[i
], back_1
.green
);
1698 palette
[i
].green
= png_ptr
->gamma_from_1
[w
];
1700 v
= png_ptr
->gamma_to_1
[palette
[i
].blue
];
1701 png_composite(w
, v
, png_ptr
->trans_alpha
[i
], back_1
.blue
);
1702 palette
[i
].blue
= png_ptr
->gamma_from_1
[w
];
1707 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1708 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1709 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1713 /* Prevent the transformations being done again.
1715 * NOTE: this is highly dubious; it removes the transformations in
1716 * place. This seems inconsistent with the general treatment of the
1717 * transformations elsewhere.
1719 png_ptr
->transformations
&= ~(PNG_COMPOSE
| PNG_GAMMA
);
1720 } /* color_type == PNG_COLOR_TYPE_PALETTE */
1722 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1723 else /* color_type != PNG_COLOR_TYPE_PALETTE */
1726 png_fixed_point g
= PNG_FP_1
; /* Correction to linear */
1727 png_fixed_point gs
= PNG_FP_1
; /* Correction to screen */
1729 switch (png_ptr
->background_gamma_type
)
1731 case PNG_BACKGROUND_GAMMA_SCREEN
:
1732 g
= png_ptr
->screen_gamma
;
1733 /* gs = PNG_FP_1; */
1736 case PNG_BACKGROUND_GAMMA_FILE
:
1737 g
= png_reciprocal(png_ptr
->colorspace
.gamma
);
1738 gs
= png_reciprocal2(png_ptr
->colorspace
.gamma
,
1739 png_ptr
->screen_gamma
);
1742 case PNG_BACKGROUND_GAMMA_UNIQUE
:
1743 g
= png_reciprocal(png_ptr
->background_gamma
);
1744 gs
= png_reciprocal2(png_ptr
->background_gamma
,
1745 png_ptr
->screen_gamma
);
1749 png_error(png_ptr
, "invalid background gamma type");
1752 g_sig
= png_gamma_significant(g
);
1753 gs_sig
= png_gamma_significant(gs
);
1756 png_ptr
->background_1
.gray
= png_gamma_correct(png_ptr
,
1757 png_ptr
->background
.gray
, g
);
1760 png_ptr
->background
.gray
= png_gamma_correct(png_ptr
,
1761 png_ptr
->background
.gray
, gs
);
1763 if ((png_ptr
->background
.red
!= png_ptr
->background
.green
) ||
1764 (png_ptr
->background
.red
!= png_ptr
->background
.blue
) ||
1765 (png_ptr
->background
.red
!= png_ptr
->background
.gray
))
1767 /* RGB or RGBA with color background */
1770 png_ptr
->background_1
.red
= png_gamma_correct(png_ptr
,
1771 png_ptr
->background
.red
, g
);
1773 png_ptr
->background_1
.green
= png_gamma_correct(png_ptr
,
1774 png_ptr
->background
.green
, g
);
1776 png_ptr
->background_1
.blue
= png_gamma_correct(png_ptr
,
1777 png_ptr
->background
.blue
, g
);
1782 png_ptr
->background
.red
= png_gamma_correct(png_ptr
,
1783 png_ptr
->background
.red
, gs
);
1785 png_ptr
->background
.green
= png_gamma_correct(png_ptr
,
1786 png_ptr
->background
.green
, gs
);
1788 png_ptr
->background
.blue
= png_gamma_correct(png_ptr
,
1789 png_ptr
->background
.blue
, gs
);
1795 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1796 png_ptr
->background_1
.red
= png_ptr
->background_1
.green
1797 = png_ptr
->background_1
.blue
= png_ptr
->background_1
.gray
;
1799 png_ptr
->background
.red
= png_ptr
->background
.green
1800 = png_ptr
->background
.blue
= png_ptr
->background
.gray
;
1803 /* The background is now in screen gamma: */
1804 png_ptr
->background_gamma_type
= PNG_BACKGROUND_GAMMA_SCREEN
;
1805 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1806 }/* png_ptr->transformations & PNG_BACKGROUND */
1809 /* Transformation does not include PNG_BACKGROUND */
1810 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1811 if (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
1812 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1813 /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1814 && ((png_ptr
->transformations
& PNG_EXPAND
) == 0 ||
1815 (png_ptr
->transformations
& PNG_RGB_TO_GRAY
) == 0)
1819 png_colorp palette
= png_ptr
->palette
;
1820 int num_palette
= png_ptr
->num_palette
;
1823 /* NOTE: there are other transformations that should probably be in
1826 for (i
= 0; i
< num_palette
; i
++)
1828 palette
[i
].red
= png_ptr
->gamma_table
[palette
[i
].red
];
1829 palette
[i
].green
= png_ptr
->gamma_table
[palette
[i
].green
];
1830 palette
[i
].blue
= png_ptr
->gamma_table
[palette
[i
].blue
];
1833 /* Done the gamma correction. */
1834 png_ptr
->transformations
&= ~PNG_GAMMA
;
1835 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1837 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1840 #endif /* PNG_READ_GAMMA_SUPPORTED */
1842 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1843 /* No GAMMA transformation (see the hanging else 4 lines above) */
1844 if ((png_ptr
->transformations
& PNG_COMPOSE
) &&
1845 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
))
1848 int istop
= (int)png_ptr
->num_trans
;
1850 png_colorp palette
= png_ptr
->palette
;
1852 back
.red
= (png_byte
)png_ptr
->background
.red
;
1853 back
.green
= (png_byte
)png_ptr
->background
.green
;
1854 back
.blue
= (png_byte
)png_ptr
->background
.blue
;
1856 for (i
= 0; i
< istop
; i
++)
1858 if (png_ptr
->trans_alpha
[i
] == 0)
1863 else if (png_ptr
->trans_alpha
[i
] != 0xff)
1865 /* The png_composite() macro is defined in png.h */
1866 png_composite(palette
[i
].red
, palette
[i
].red
,
1867 png_ptr
->trans_alpha
[i
], back
.red
);
1869 png_composite(palette
[i
].green
, palette
[i
].green
,
1870 png_ptr
->trans_alpha
[i
], back
.green
);
1872 png_composite(palette
[i
].blue
, palette
[i
].blue
,
1873 png_ptr
->trans_alpha
[i
], back
.blue
);
1877 png_ptr
->transformations
&= ~PNG_COMPOSE
;
1879 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1881 #ifdef PNG_READ_SHIFT_SUPPORTED
1882 if ((png_ptr
->transformations
& PNG_SHIFT
) &&
1883 !(png_ptr
->transformations
& PNG_EXPAND
) &&
1884 (png_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
))
1887 int istop
= png_ptr
->num_palette
;
1888 int shift
= 8 - png_ptr
->sig_bit
.red
;
1890 png_ptr
->transformations
&= ~PNG_SHIFT
;
1892 /* significant bits can be in the range 1 to 7 for a meaninful result, if
1893 * the number of significant bits is 0 then no shift is done (this is an
1894 * error condition which is silently ignored.)
1896 if (shift
> 0 && shift
< 8) for (i
=0; i
<istop
; ++i
)
1898 int component
= png_ptr
->palette
[i
].red
;
1900 component
>>= shift
;
1901 png_ptr
->palette
[i
].red
= (png_byte
)component
;
1904 shift
= 8 - png_ptr
->sig_bit
.green
;
1905 if (shift
> 0 && shift
< 8) for (i
=0; i
<istop
; ++i
)
1907 int component
= png_ptr
->palette
[i
].green
;
1909 component
>>= shift
;
1910 png_ptr
->palette
[i
].green
= (png_byte
)component
;
1913 shift
= 8 - png_ptr
->sig_bit
.blue
;
1914 if (shift
> 0 && shift
< 8) for (i
=0; i
<istop
; ++i
)
1916 int component
= png_ptr
->palette
[i
].blue
;
1918 component
>>= shift
;
1919 png_ptr
->palette
[i
].blue
= (png_byte
)component
;
1922 #endif /* PNG_READ_SHIFT_SUPPORTED */
1925 /* Modify the info structure to reflect the transformations. The
1926 * info should be updated so a PNG file could be written with it,
1927 * assuming the transformations result in valid PNG data.
1930 png_read_transform_info(png_structrp png_ptr
, png_inforp info_ptr
)
1932 png_debug(1, "in png_read_transform_info");
1934 #ifdef PNG_READ_EXPAND_SUPPORTED
1935 if (png_ptr
->transformations
& PNG_EXPAND
)
1937 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
1939 /* This check must match what actually happens in
1940 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
1941 * it is all opaque we must do the same (at present it does not.)
1943 if (png_ptr
->num_trans
> 0)
1944 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
1947 info_ptr
->color_type
= PNG_COLOR_TYPE_RGB
;
1949 info_ptr
->bit_depth
= 8;
1950 info_ptr
->num_trans
= 0;
1954 if (png_ptr
->num_trans
)
1956 if (png_ptr
->transformations
& PNG_EXPAND_tRNS
)
1957 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
1959 if (info_ptr
->bit_depth
< 8)
1960 info_ptr
->bit_depth
= 8;
1962 info_ptr
->num_trans
= 0;
1967 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1968 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1969 /* The following is almost certainly wrong unless the background value is in
1972 if (png_ptr
->transformations
& PNG_COMPOSE
)
1973 info_ptr
->background
= png_ptr
->background
;
1976 #ifdef PNG_READ_GAMMA_SUPPORTED
1977 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
1978 * however it seems that the code in png_init_read_transformations, which has
1979 * been called before this from png_read_update_info->png_read_start_row
1980 * sometimes does the gamma transform and cancels the flag.
1982 * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1983 * the screen_gamma value. The following probably results in weirdness if
1984 * the info_ptr is used by the app after the rows have been read.
1986 info_ptr
->colorspace
.gamma
= png_ptr
->colorspace
.gamma
;
1989 if (info_ptr
->bit_depth
== 16)
1991 # ifdef PNG_READ_16BIT_SUPPORTED
1992 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1993 if (png_ptr
->transformations
& PNG_SCALE_16_TO_8
)
1994 info_ptr
->bit_depth
= 8;
1997 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1998 if (png_ptr
->transformations
& PNG_16_TO_8
)
1999 info_ptr
->bit_depth
= 8;
2003 /* No 16 bit support: force chopping 16-bit input down to 8, in this case
2004 * the app program can chose if both APIs are available by setting the
2005 * correct scaling to use.
2007 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2008 /* For compatibility with previous versions use the strip method by
2009 * default. This code works because if PNG_SCALE_16_TO_8 is already
2010 * set the code below will do that in preference to the chop.
2012 png_ptr
->transformations
|= PNG_16_TO_8
;
2013 info_ptr
->bit_depth
= 8;
2016 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2017 png_ptr
->transformations
|= PNG_SCALE_16_TO_8
;
2018 info_ptr
->bit_depth
= 8;
2021 CONFIGURATION ERROR
: you must enable at least one
16 to
8 method
2024 #endif /* !READ_16BIT_SUPPORTED */
2027 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2028 if (png_ptr
->transformations
& PNG_GRAY_TO_RGB
)
2029 info_ptr
->color_type
= (png_byte
)(info_ptr
->color_type
|
2030 PNG_COLOR_MASK_COLOR
);
2033 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2034 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
2035 info_ptr
->color_type
= (png_byte
)(info_ptr
->color_type
&
2036 ~PNG_COLOR_MASK_COLOR
);
2039 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2040 if (png_ptr
->transformations
& PNG_QUANTIZE
)
2042 if (((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
2043 (info_ptr
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)) &&
2044 png_ptr
->palette_lookup
&& info_ptr
->bit_depth
== 8)
2046 info_ptr
->color_type
= PNG_COLOR_TYPE_PALETTE
;
2051 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2052 if (png_ptr
->transformations
& PNG_EXPAND_16
&& info_ptr
->bit_depth
== 8 &&
2053 info_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
2055 info_ptr
->bit_depth
= 16;
2059 #ifdef PNG_READ_PACK_SUPPORTED
2060 if ((png_ptr
->transformations
& PNG_PACK
) && (info_ptr
->bit_depth
< 8))
2061 info_ptr
->bit_depth
= 8;
2064 if (info_ptr
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2065 info_ptr
->channels
= 1;
2067 else if (info_ptr
->color_type
& PNG_COLOR_MASK_COLOR
)
2068 info_ptr
->channels
= 3;
2071 info_ptr
->channels
= 1;
2073 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2074 if (png_ptr
->transformations
& PNG_STRIP_ALPHA
)
2076 info_ptr
->color_type
= (png_byte
)(info_ptr
->color_type
&
2077 ~PNG_COLOR_MASK_ALPHA
);
2078 info_ptr
->num_trans
= 0;
2082 if (info_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
)
2083 info_ptr
->channels
++;
2085 #ifdef PNG_READ_FILLER_SUPPORTED
2086 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
2087 if ((png_ptr
->transformations
& PNG_FILLER
) &&
2088 ((info_ptr
->color_type
== PNG_COLOR_TYPE_RGB
) ||
2089 (info_ptr
->color_type
== PNG_COLOR_TYPE_GRAY
)))
2091 info_ptr
->channels
++;
2092 /* If adding a true alpha channel not just filler */
2093 if (png_ptr
->transformations
& PNG_ADD_ALPHA
)
2094 info_ptr
->color_type
|= PNG_COLOR_MASK_ALPHA
;
2098 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2099 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2100 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
2102 if (info_ptr
->bit_depth
< png_ptr
->user_transform_depth
)
2103 info_ptr
->bit_depth
= png_ptr
->user_transform_depth
;
2105 if (info_ptr
->channels
< png_ptr
->user_transform_channels
)
2106 info_ptr
->channels
= png_ptr
->user_transform_channels
;
2110 info_ptr
->pixel_depth
= (png_byte
)(info_ptr
->channels
*
2111 info_ptr
->bit_depth
);
2113 info_ptr
->rowbytes
= PNG_ROWBYTES(info_ptr
->pixel_depth
, info_ptr
->width
);
2115 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
2116 * check in png_rowbytes that the user buffer won't get overwritten. Note
2117 * that the field is not always set - if png_read_update_info isn't called
2118 * the application has to either not do any transforms or get the calculation
2121 png_ptr
->info_rowbytes
= info_ptr
->rowbytes
;
2123 #ifndef PNG_READ_EXPAND_SUPPORTED
2129 /* Transform the row. The order of transformations is significant,
2130 * and is very touchy. If you add a transformation, take care to
2131 * decide how it fits in with the other transformations here.
2134 png_do_read_transformations(png_structrp png_ptr
, png_row_infop row_info
)
2136 png_debug(1, "in png_do_read_transformations");
2138 if (png_ptr
->row_buf
== NULL
)
2140 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
2141 * error is incredibly rare and incredibly easy to debug without this
2144 png_error(png_ptr
, "NULL row buffer");
2147 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
2148 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
2149 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
2150 * all transformations, however in practice the ROW_INIT always gets done on
2151 * demand, if necessary.
2153 if ((png_ptr
->flags
& PNG_FLAG_DETECT_UNINITIALIZED
) != 0 &&
2154 !(png_ptr
->flags
& PNG_FLAG_ROW_INIT
))
2156 /* Application has failed to call either png_read_start_image() or
2157 * png_read_update_info() after setting transforms that expand pixels.
2158 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
2160 png_error(png_ptr
, "Uninitialized row");
2163 #ifdef PNG_READ_EXPAND_SUPPORTED
2164 if (png_ptr
->transformations
& PNG_EXPAND
)
2166 if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
)
2168 png_do_expand_palette(row_info
, png_ptr
->row_buf
+ 1,
2169 png_ptr
->palette
, png_ptr
->trans_alpha
, png_ptr
->num_trans
);
2174 if (png_ptr
->num_trans
&&
2175 (png_ptr
->transformations
& PNG_EXPAND_tRNS
))
2176 png_do_expand(row_info
, png_ptr
->row_buf
+ 1,
2177 &(png_ptr
->trans_color
));
2180 png_do_expand(row_info
, png_ptr
->row_buf
+ 1,
2186 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2187 if ((png_ptr
->transformations
& PNG_STRIP_ALPHA
) &&
2188 !(png_ptr
->transformations
& PNG_COMPOSE
) &&
2189 (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2190 row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
2191 png_do_strip_channel(row_info
, png_ptr
->row_buf
+ 1,
2192 0 /* at_start == false, because SWAP_ALPHA happens later */);
2195 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2196 if (png_ptr
->transformations
& PNG_RGB_TO_GRAY
)
2199 png_do_rgb_to_gray(png_ptr
, row_info
,
2200 png_ptr
->row_buf
+ 1);
2204 png_ptr
->rgb_to_gray_status
=1;
2205 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
2206 PNG_RGB_TO_GRAY_WARN
)
2207 png_warning(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
2209 if ((png_ptr
->transformations
& PNG_RGB_TO_GRAY
) ==
2210 PNG_RGB_TO_GRAY_ERR
)
2211 png_error(png_ptr
, "png_do_rgb_to_gray found nongray pixel");
2216 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
2218 * In most cases, the "simple transparency" should be done prior to doing
2219 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
2220 * pixel is transparent. You would also need to make sure that the
2221 * transparency information is upgraded to RGB.
2223 * To summarize, the current flow is:
2224 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
2225 * with background "in place" if transparent,
2226 * convert to RGB if necessary
2227 * - Gray + alpha -> composite with gray background and remove alpha bytes,
2228 * convert to RGB if necessary
2230 * To support RGB backgrounds for gray images we need:
2231 * - Gray + simple transparency -> convert to RGB + simple transparency,
2232 * compare 3 or 6 bytes and composite with
2233 * background "in place" if transparent
2234 * (3x compare/pixel compared to doing
2235 * composite with gray bkgrnd)
2236 * - Gray + alpha -> convert to RGB + alpha, composite with background and
2237 * remove alpha bytes (3x float
2238 * operations/pixel compared with composite
2239 * on gray background)
2241 * Greg's change will do this. The reason it wasn't done before is for
2242 * performance, as this increases the per-pixel operations. If we would check
2243 * in advance if the background was gray or RGB, and position the gray-to-RGB
2244 * transform appropriately, then it would save a lot of work/time.
2247 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2248 /* If gray -> RGB, do so now only if background is non-gray; else do later
2249 * for performance reasons
2251 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
2252 !(png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
2253 png_do_gray_to_rgb(row_info
, png_ptr
->row_buf
+ 1);
2256 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2257 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
2258 if (png_ptr
->transformations
& PNG_COMPOSE
)
2259 png_do_compose(row_info
, png_ptr
->row_buf
+ 1, png_ptr
);
2262 #ifdef PNG_READ_GAMMA_SUPPORTED
2263 if ((png_ptr
->transformations
& PNG_GAMMA
) &&
2264 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2265 /* Because RGB_TO_GRAY does the gamma transform. */
2266 !(png_ptr
->transformations
& PNG_RGB_TO_GRAY
) &&
2268 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2269 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
2270 /* Because PNG_COMPOSE does the gamma transform if there is something to
2271 * do (if there is an alpha channel or transparency.)
2273 !((png_ptr
->transformations
& PNG_COMPOSE
) &&
2274 ((png_ptr
->num_trans
!= 0) ||
2275 (png_ptr
->color_type
& PNG_COLOR_MASK_ALPHA
))) &&
2277 /* Because png_init_read_transformations transforms the palette, unless
2278 * RGB_TO_GRAY will do the transform.
2280 (png_ptr
->color_type
!= PNG_COLOR_TYPE_PALETTE
))
2281 png_do_gamma(row_info
, png_ptr
->row_buf
+ 1, png_ptr
);
2284 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2285 if ((png_ptr
->transformations
& PNG_STRIP_ALPHA
) &&
2286 (png_ptr
->transformations
& PNG_COMPOSE
) &&
2287 (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
||
2288 row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
))
2289 png_do_strip_channel(row_info
, png_ptr
->row_buf
+ 1,
2290 0 /* at_start == false, because SWAP_ALPHA happens later */);
2293 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
2294 if ((png_ptr
->transformations
& PNG_ENCODE_ALPHA
) &&
2295 (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
))
2296 png_do_encode_alpha(row_info
, png_ptr
->row_buf
+ 1, png_ptr
);
2299 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2300 if (png_ptr
->transformations
& PNG_SCALE_16_TO_8
)
2301 png_do_scale_16_to_8(row_info
, png_ptr
->row_buf
+ 1);
2304 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2305 /* There is no harm in doing both of these because only one has any effect,
2306 * by putting the 'scale' option first if the app asks for scale (either by
2307 * calling the API or in a TRANSFORM flag) this is what happens.
2309 if (png_ptr
->transformations
& PNG_16_TO_8
)
2310 png_do_chop(row_info
, png_ptr
->row_buf
+ 1);
2313 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2314 if (png_ptr
->transformations
& PNG_QUANTIZE
)
2316 png_do_quantize(row_info
, png_ptr
->row_buf
+ 1,
2317 png_ptr
->palette_lookup
, png_ptr
->quantize_index
);
2319 if (row_info
->rowbytes
== 0)
2320 png_error(png_ptr
, "png_do_quantize returned rowbytes=0");
2322 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
2324 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2325 /* Do the expansion now, after all the arithmetic has been done. Notice
2326 * that previous transformations can handle the PNG_EXPAND_16 flag if this
2327 * is efficient (particularly true in the case of gamma correction, where
2328 * better accuracy results faster!)
2330 if (png_ptr
->transformations
& PNG_EXPAND_16
)
2331 png_do_expand_16(row_info
, png_ptr
->row_buf
+ 1);
2334 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2335 /* NOTE: moved here in 1.5.4 (from much later in this list.) */
2336 if ((png_ptr
->transformations
& PNG_GRAY_TO_RGB
) &&
2337 (png_ptr
->mode
& PNG_BACKGROUND_IS_GRAY
))
2338 png_do_gray_to_rgb(row_info
, png_ptr
->row_buf
+ 1);
2341 #ifdef PNG_READ_INVERT_SUPPORTED
2342 if (png_ptr
->transformations
& PNG_INVERT_MONO
)
2343 png_do_invert(row_info
, png_ptr
->row_buf
+ 1);
2346 #ifdef PNG_READ_SHIFT_SUPPORTED
2347 if (png_ptr
->transformations
& PNG_SHIFT
)
2348 png_do_unshift(row_info
, png_ptr
->row_buf
+ 1,
2352 #ifdef PNG_READ_PACK_SUPPORTED
2353 if (png_ptr
->transformations
& PNG_PACK
)
2354 png_do_unpack(row_info
, png_ptr
->row_buf
+ 1);
2357 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
2358 /* Added at libpng-1.5.10 */
2359 if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
2360 png_ptr
->num_palette_max
>= 0)
2361 png_do_check_palette_indexes(png_ptr
, row_info
);
2364 #ifdef PNG_READ_BGR_SUPPORTED
2365 if (png_ptr
->transformations
& PNG_BGR
)
2366 png_do_bgr(row_info
, png_ptr
->row_buf
+ 1);
2369 #ifdef PNG_READ_PACKSWAP_SUPPORTED
2370 if (png_ptr
->transformations
& PNG_PACKSWAP
)
2371 png_do_packswap(row_info
, png_ptr
->row_buf
+ 1);
2374 #ifdef PNG_READ_FILLER_SUPPORTED
2375 if (png_ptr
->transformations
& PNG_FILLER
)
2376 png_do_read_filler(row_info
, png_ptr
->row_buf
+ 1,
2377 (png_uint_32
)png_ptr
->filler
, png_ptr
->flags
);
2380 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2381 if (png_ptr
->transformations
& PNG_INVERT_ALPHA
)
2382 png_do_read_invert_alpha(row_info
, png_ptr
->row_buf
+ 1);
2385 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2386 if (png_ptr
->transformations
& PNG_SWAP_ALPHA
)
2387 png_do_read_swap_alpha(row_info
, png_ptr
->row_buf
+ 1);
2390 #ifdef PNG_READ_16BIT_SUPPORTED
2391 #ifdef PNG_READ_SWAP_SUPPORTED
2392 if (png_ptr
->transformations
& PNG_SWAP_BYTES
)
2393 png_do_swap(row_info
, png_ptr
->row_buf
+ 1);
2397 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
2398 if (png_ptr
->transformations
& PNG_USER_TRANSFORM
)
2400 if (png_ptr
->read_user_transform_fn
!= NULL
)
2401 (*(png_ptr
->read_user_transform_fn
)) /* User read transform function */
2402 (png_ptr
, /* png_ptr */
2403 row_info
, /* row_info: */
2404 /* png_uint_32 width; width of row */
2405 /* png_size_t rowbytes; number of bytes in row */
2406 /* png_byte color_type; color type of pixels */
2407 /* png_byte bit_depth; bit depth of samples */
2408 /* png_byte channels; number of channels (1-4) */
2409 /* png_byte pixel_depth; bits per pixel (depth*channels) */
2410 png_ptr
->row_buf
+ 1); /* start of pixel data for row */
2411 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
2412 if (png_ptr
->user_transform_depth
)
2413 row_info
->bit_depth
= png_ptr
->user_transform_depth
;
2415 if (png_ptr
->user_transform_channels
)
2416 row_info
->channels
= png_ptr
->user_transform_channels
;
2418 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
*
2419 row_info
->channels
);
2421 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_info
->width
);
2426 #ifdef PNG_READ_PACK_SUPPORTED
2427 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2428 * without changing the actual values. Thus, if you had a row with
2429 * a bit depth of 1, you would end up with bytes that only contained
2430 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2431 * png_do_shift() after this.
2434 png_do_unpack(png_row_infop row_info
, png_bytep row
)
2436 png_debug(1, "in png_do_unpack");
2438 if (row_info
->bit_depth
< 8)
2441 png_uint_32 row_width
=row_info
->width
;
2443 switch (row_info
->bit_depth
)
2447 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
2448 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
2449 png_uint_32 shift
= 7 - (int)((row_width
+ 7) & 0x07);
2450 for (i
= 0; i
< row_width
; i
++)
2452 *dp
= (png_byte
)((*sp
>> shift
) & 0x01);
2471 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
2472 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
2473 png_uint_32 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
2474 for (i
= 0; i
< row_width
; i
++)
2476 *dp
= (png_byte
)((*sp
>> shift
) & 0x03);
2494 png_bytep sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
2495 png_bytep dp
= row
+ (png_size_t
)row_width
- 1;
2496 png_uint_32 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
2497 for (i
= 0; i
< row_width
; i
++)
2499 *dp
= (png_byte
)((*sp
>> shift
) & 0x0f);
2518 row_info
->bit_depth
= 8;
2519 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
2520 row_info
->rowbytes
= row_width
* row_info
->channels
;
2525 #ifdef PNG_READ_SHIFT_SUPPORTED
2526 /* Reverse the effects of png_do_shift. This routine merely shifts the
2527 * pixels back to their significant bits values. Thus, if you have
2528 * a row of bit depth 8, but only 5 are significant, this will shift
2529 * the values back to 0 through 31.
2532 png_do_unshift(png_row_infop row_info
, png_bytep row
,
2533 png_const_color_8p sig_bits
)
2537 png_debug(1, "in png_do_unshift");
2539 /* The palette case has already been handled in the _init routine. */
2540 color_type
= row_info
->color_type
;
2542 if (color_type
!= PNG_COLOR_TYPE_PALETTE
)
2546 int bit_depth
= row_info
->bit_depth
;
2548 if (color_type
& PNG_COLOR_MASK_COLOR
)
2550 shift
[channels
++] = bit_depth
- sig_bits
->red
;
2551 shift
[channels
++] = bit_depth
- sig_bits
->green
;
2552 shift
[channels
++] = bit_depth
- sig_bits
->blue
;
2557 shift
[channels
++] = bit_depth
- sig_bits
->gray
;
2560 if (color_type
& PNG_COLOR_MASK_ALPHA
)
2562 shift
[channels
++] = bit_depth
- sig_bits
->alpha
;
2568 for (c
= have_shift
= 0; c
< channels
; ++c
)
2570 /* A shift of more than the bit depth is an error condition but it
2571 * gets ignored here.
2573 if (shift
[c
] <= 0 || shift
[c
] >= bit_depth
)
2587 /* Must be 1bpp gray: should not be here! */
2592 /* Must be 2bpp gray */
2593 /* assert(channels == 1 && shift[0] == 1) */
2596 png_bytep bp_end
= bp
+ row_info
->rowbytes
;
2600 int b
= (*bp
>> 1) & 0x55;
2601 *bp
++ = (png_byte
)b
;
2607 /* Must be 4bpp gray */
2608 /* assert(channels == 1) */
2611 png_bytep bp_end
= bp
+ row_info
->rowbytes
;
2612 int gray_shift
= shift
[0];
2613 int mask
= 0xf >> gray_shift
;
2619 int b
= (*bp
>> gray_shift
) & mask
;
2620 *bp
++ = (png_byte
)b
;
2626 /* Single byte components, G, GA, RGB, RGBA */
2629 png_bytep bp_end
= bp
+ row_info
->rowbytes
;
2634 int b
= *bp
>> shift
[channel
];
2635 if (++channel
>= channels
)
2637 *bp
++ = (png_byte
)b
;
2642 #ifdef PNG_READ_16BIT_SUPPORTED
2644 /* Double byte components, G, GA, RGB, RGBA */
2647 png_bytep bp_end
= bp
+ row_info
->rowbytes
;
2652 int value
= (bp
[0] << 8) + bp
[1];
2654 value
>>= shift
[channel
];
2655 if (++channel
>= channels
)
2657 *bp
++ = (png_byte
)(value
>> 8);
2658 *bp
++ = (png_byte
)(value
& 0xff);
2668 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2669 /* Scale rows of bit depth 16 down to 8 accurately */
2671 png_do_scale_16_to_8(png_row_infop row_info
, png_bytep row
)
2673 png_debug(1, "in png_do_scale_16_to_8");
2675 if (row_info
->bit_depth
== 16)
2677 png_bytep sp
= row
; /* source */
2678 png_bytep dp
= row
; /* destination */
2679 png_bytep ep
= sp
+ row_info
->rowbytes
; /* end+1 */
2683 /* The input is an array of 16 bit components, these must be scaled to
2684 * 8 bits each. For a 16 bit value V the required value (from the PNG
2685 * specification) is:
2689 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2691 * Represent V as the two byte value vhi.vlo. Make a guess that the
2692 * result is the top byte of V, vhi, then the correction to this value
2695 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2696 * = floor(((vlo-vhi) + 128.5) / 257)
2698 * This can be approximated using integer arithmetic (and a signed
2701 * error = (vlo-vhi+128) >> 8;
2703 * The approximate differs from the exact answer only when (vlo-vhi) is
2704 * 128; it then gives a correction of +1 when the exact correction is
2705 * 0. This gives 128 errors. The exact answer (correct for all 16 bit
2708 * error = (vlo-vhi+128)*65535 >> 24;
2710 * An alternative arithmetic calculation which also gives no errors is:
2712 * (V * 255 + 32895) >> 16
2715 png_int_32 tmp
= *sp
++; /* must be signed! */
2716 tmp
+= (((int)*sp
++ - tmp
+ 128) * 65535) >> 24;
2717 *dp
++ = (png_byte
)tmp
;
2720 row_info
->bit_depth
= 8;
2721 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
2722 row_info
->rowbytes
= row_info
->width
* row_info
->channels
;
2727 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2729 /* Simply discard the low byte. This was the default behavior prior
2732 png_do_chop(png_row_infop row_info
, png_bytep row
)
2734 png_debug(1, "in png_do_chop");
2736 if (row_info
->bit_depth
== 16)
2738 png_bytep sp
= row
; /* source */
2739 png_bytep dp
= row
; /* destination */
2740 png_bytep ep
= sp
+ row_info
->rowbytes
; /* end+1 */
2745 sp
+= 2; /* skip low byte */
2748 row_info
->bit_depth
= 8;
2749 row_info
->pixel_depth
= (png_byte
)(8 * row_info
->channels
);
2750 row_info
->rowbytes
= row_info
->width
* row_info
->channels
;
2755 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2757 png_do_read_swap_alpha(png_row_infop row_info
, png_bytep row
)
2759 png_debug(1, "in png_do_read_swap_alpha");
2762 png_uint_32 row_width
= row_info
->width
;
2763 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
2765 /* This converts from RGBA to ARGB */
2766 if (row_info
->bit_depth
== 8)
2768 png_bytep sp
= row
+ row_info
->rowbytes
;
2773 for (i
= 0; i
< row_width
; i
++)
2783 #ifdef PNG_READ_16BIT_SUPPORTED
2784 /* This converts from RRGGBBAA to AARRGGBB */
2787 png_bytep sp
= row
+ row_info
->rowbytes
;
2792 for (i
= 0; i
< row_width
; i
++)
2809 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2811 /* This converts from GA to AG */
2812 if (row_info
->bit_depth
== 8)
2814 png_bytep sp
= row
+ row_info
->rowbytes
;
2819 for (i
= 0; i
< row_width
; i
++)
2827 #ifdef PNG_READ_16BIT_SUPPORTED
2828 /* This converts from GGAA to AAGG */
2831 png_bytep sp
= row
+ row_info
->rowbytes
;
2836 for (i
= 0; i
< row_width
; i
++)
2852 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2854 png_do_read_invert_alpha(png_row_infop row_info
, png_bytep row
)
2856 png_uint_32 row_width
;
2857 png_debug(1, "in png_do_read_invert_alpha");
2859 row_width
= row_info
->width
;
2860 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
2862 if (row_info
->bit_depth
== 8)
2864 /* This inverts the alpha channel in RGBA */
2865 png_bytep sp
= row
+ row_info
->rowbytes
;
2869 for (i
= 0; i
< row_width
; i
++)
2871 *(--dp
) = (png_byte
)(255 - *(--sp
));
2873 /* This does nothing:
2877 We can replace it with:
2884 #ifdef PNG_READ_16BIT_SUPPORTED
2885 /* This inverts the alpha channel in RRGGBBAA */
2888 png_bytep sp
= row
+ row_info
->rowbytes
;
2892 for (i
= 0; i
< row_width
; i
++)
2894 *(--dp
) = (png_byte
)(255 - *(--sp
));
2895 *(--dp
) = (png_byte
)(255 - *(--sp
));
2897 /* This does nothing:
2904 We can replace it with:
2912 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
2914 if (row_info
->bit_depth
== 8)
2916 /* This inverts the alpha channel in GA */
2917 png_bytep sp
= row
+ row_info
->rowbytes
;
2921 for (i
= 0; i
< row_width
; i
++)
2923 *(--dp
) = (png_byte
)(255 - *(--sp
));
2928 #ifdef PNG_READ_16BIT_SUPPORTED
2931 /* This inverts the alpha channel in GGAA */
2932 png_bytep sp
= row
+ row_info
->rowbytes
;
2936 for (i
= 0; i
< row_width
; i
++)
2938 *(--dp
) = (png_byte
)(255 - *(--sp
));
2939 *(--dp
) = (png_byte
)(255 - *(--sp
));
2953 #ifdef PNG_READ_FILLER_SUPPORTED
2954 /* Add filler channel if we have RGB color */
2956 png_do_read_filler(png_row_infop row_info
, png_bytep row
,
2957 png_uint_32 filler
, png_uint_32 flags
)
2960 png_uint_32 row_width
= row_info
->width
;
2962 #ifdef PNG_READ_16BIT_SUPPORTED
2963 png_byte hi_filler
= (png_byte
)((filler
>>8) & 0xff);
2965 png_byte lo_filler
= (png_byte
)(filler
& 0xff);
2967 png_debug(1, "in png_do_read_filler");
2970 row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
2972 if (row_info
->bit_depth
== 8)
2974 if (flags
& PNG_FLAG_FILLER_AFTER
)
2976 /* This changes the data from G to GX */
2977 png_bytep sp
= row
+ (png_size_t
)row_width
;
2978 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2979 for (i
= 1; i
< row_width
; i
++)
2981 *(--dp
) = lo_filler
;
2984 *(--dp
) = lo_filler
;
2985 row_info
->channels
= 2;
2986 row_info
->pixel_depth
= 16;
2987 row_info
->rowbytes
= row_width
* 2;
2992 /* This changes the data from G to XG */
2993 png_bytep sp
= row
+ (png_size_t
)row_width
;
2994 png_bytep dp
= sp
+ (png_size_t
)row_width
;
2995 for (i
= 0; i
< row_width
; i
++)
2998 *(--dp
) = lo_filler
;
3000 row_info
->channels
= 2;
3001 row_info
->pixel_depth
= 16;
3002 row_info
->rowbytes
= row_width
* 2;
3006 #ifdef PNG_READ_16BIT_SUPPORTED
3007 else if (row_info
->bit_depth
== 16)
3009 if (flags
& PNG_FLAG_FILLER_AFTER
)
3011 /* This changes the data from GG to GGXX */
3012 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
3013 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3014 for (i
= 1; i
< row_width
; i
++)
3016 *(--dp
) = hi_filler
;
3017 *(--dp
) = lo_filler
;
3021 *(--dp
) = hi_filler
;
3022 *(--dp
) = lo_filler
;
3023 row_info
->channels
= 2;
3024 row_info
->pixel_depth
= 32;
3025 row_info
->rowbytes
= row_width
* 4;
3030 /* This changes the data from GG to XXGG */
3031 png_bytep sp
= row
+ (png_size_t
)row_width
* 2;
3032 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3033 for (i
= 0; i
< row_width
; i
++)
3037 *(--dp
) = hi_filler
;
3038 *(--dp
) = lo_filler
;
3040 row_info
->channels
= 2;
3041 row_info
->pixel_depth
= 32;
3042 row_info
->rowbytes
= row_width
* 4;
3046 } /* COLOR_TYPE == GRAY */
3047 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
3049 if (row_info
->bit_depth
== 8)
3051 if (flags
& PNG_FLAG_FILLER_AFTER
)
3053 /* This changes the data from RGB to RGBX */
3054 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
3055 png_bytep dp
= sp
+ (png_size_t
)row_width
;
3056 for (i
= 1; i
< row_width
; i
++)
3058 *(--dp
) = lo_filler
;
3063 *(--dp
) = lo_filler
;
3064 row_info
->channels
= 4;
3065 row_info
->pixel_depth
= 32;
3066 row_info
->rowbytes
= row_width
* 4;
3071 /* This changes the data from RGB to XRGB */
3072 png_bytep sp
= row
+ (png_size_t
)row_width
* 3;
3073 png_bytep dp
= sp
+ (png_size_t
)row_width
;
3074 for (i
= 0; i
< row_width
; i
++)
3079 *(--dp
) = lo_filler
;
3081 row_info
->channels
= 4;
3082 row_info
->pixel_depth
= 32;
3083 row_info
->rowbytes
= row_width
* 4;
3087 #ifdef PNG_READ_16BIT_SUPPORTED
3088 else if (row_info
->bit_depth
== 16)
3090 if (flags
& PNG_FLAG_FILLER_AFTER
)
3092 /* This changes the data from RRGGBB to RRGGBBXX */
3093 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
3094 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3095 for (i
= 1; i
< row_width
; i
++)
3097 *(--dp
) = hi_filler
;
3098 *(--dp
) = lo_filler
;
3106 *(--dp
) = hi_filler
;
3107 *(--dp
) = lo_filler
;
3108 row_info
->channels
= 4;
3109 row_info
->pixel_depth
= 64;
3110 row_info
->rowbytes
= row_width
* 8;
3115 /* This changes the data from RRGGBB to XXRRGGBB */
3116 png_bytep sp
= row
+ (png_size_t
)row_width
* 6;
3117 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3118 for (i
= 0; i
< row_width
; i
++)
3126 *(--dp
) = hi_filler
;
3127 *(--dp
) = lo_filler
;
3130 row_info
->channels
= 4;
3131 row_info
->pixel_depth
= 64;
3132 row_info
->rowbytes
= row_width
* 8;
3136 } /* COLOR_TYPE == RGB */
3140 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
3141 /* Expand grayscale files to RGB, with or without alpha */
3143 png_do_gray_to_rgb(png_row_infop row_info
, png_bytep row
)
3146 png_uint_32 row_width
= row_info
->width
;
3148 png_debug(1, "in png_do_gray_to_rgb");
3150 if (row_info
->bit_depth
>= 8 &&
3151 !(row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
3153 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
3155 if (row_info
->bit_depth
== 8)
3157 /* This changes G to RGB */
3158 png_bytep sp
= row
+ (png_size_t
)row_width
- 1;
3159 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3160 for (i
= 0; i
< row_width
; i
++)
3170 /* This changes GG to RRGGBB */
3171 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
3172 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
3173 for (i
= 0; i
< row_width
; i
++)
3176 *(dp
--) = *(sp
- 1);
3178 *(dp
--) = *(sp
- 1);
3185 else if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
3187 if (row_info
->bit_depth
== 8)
3189 /* This changes GA to RGBA */
3190 png_bytep sp
= row
+ (png_size_t
)row_width
* 2 - 1;
3191 png_bytep dp
= sp
+ (png_size_t
)row_width
* 2;
3192 for (i
= 0; i
< row_width
; i
++)
3203 /* This changes GGAA to RRGGBBAA */
3204 png_bytep sp
= row
+ (png_size_t
)row_width
* 4 - 1;
3205 png_bytep dp
= sp
+ (png_size_t
)row_width
* 4;
3206 for (i
= 0; i
< row_width
; i
++)
3211 *(dp
--) = *(sp
- 1);
3213 *(dp
--) = *(sp
- 1);
3219 row_info
->channels
= (png_byte
)(row_info
->channels
+ 2);
3220 row_info
->color_type
|= PNG_COLOR_MASK_COLOR
;
3221 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
3222 row_info
->bit_depth
);
3223 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
3228 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
3229 /* Reduce RGB files to grayscale, with or without alpha
3230 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
3231 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
3232 * versions dated 1998 through November 2002 have been archived at
3233 * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
3234 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
3235 * Charles Poynton poynton at poynton.com
3237 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
3239 * which can be expressed with integers as
3241 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
3243 * Poynton's current link (as of January 2003 through July 2011):
3244 * <http://www.poynton.com/notes/colour_and_gamma/>
3245 * has changed the numbers slightly:
3247 * Y = 0.2126*R + 0.7152*G + 0.0722*B
3249 * which can be expressed with integers as
3251 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
3253 * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
3254 * end point chromaticities and the D65 white point. Depending on the
3255 * precision used for the D65 white point this produces a variety of different
3256 * numbers, however if the four decimal place value used in ITU-R Rec 709 is
3257 * used (0.3127,0.3290) the Y calculation would be:
3259 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
3261 * While this is correct the rounding results in an overflow for white, because
3262 * the sum of the rounded coefficients is 32769, not 32768. Consequently
3263 * libpng uses, instead, the closest non-overflowing approximation:
3265 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
3267 * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
3268 * (including an sRGB chunk) then the chromaticities are used to calculate the
3269 * coefficients. See the chunk handling in pngrutil.c for more information.
3271 * In all cases the calculation is to be done in a linear colorspace. If no
3272 * gamma information is available to correct the encoding of the original RGB
3273 * values this results in an implicit assumption that the original PNG RGB
3274 * values were linear.
3276 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
3277 * the API takes just red and green coefficients the blue coefficient is
3278 * calculated to make the sum 32768. This will result in different rounding
3279 * to that used above.
3282 png_do_rgb_to_gray(png_structrp png_ptr
, png_row_infop row_info
, png_bytep row
)
3287 png_debug(1, "in png_do_rgb_to_gray");
3289 if (!(row_info
->color_type
& PNG_COLOR_MASK_PALETTE
) &&
3290 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
3292 PNG_CONST png_uint_32 rc
= png_ptr
->rgb_to_gray_red_coeff
;
3293 PNG_CONST png_uint_32 gc
= png_ptr
->rgb_to_gray_green_coeff
;
3294 PNG_CONST png_uint_32 bc
= 32768 - rc
- gc
;
3295 PNG_CONST png_uint_32 row_width
= row_info
->width
;
3296 PNG_CONST
int have_alpha
=
3297 (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
) != 0;
3299 if (row_info
->bit_depth
== 8)
3301 #ifdef PNG_READ_GAMMA_SUPPORTED
3302 /* Notice that gamma to/from 1 are not necessarily inverses (if
3303 * there is an overall gamma correction). Prior to 1.5.5 this code
3304 * checked the linearized values for equality; this doesn't match
3305 * the documentation, the original values must be checked.
3307 if (png_ptr
->gamma_from_1
!= NULL
&& png_ptr
->gamma_to_1
!= NULL
)
3313 for (i
= 0; i
< row_width
; i
++)
3315 png_byte red
= *(sp
++);
3316 png_byte green
= *(sp
++);
3317 png_byte blue
= *(sp
++);
3319 if (red
!= green
|| red
!= blue
)
3321 red
= png_ptr
->gamma_to_1
[red
];
3322 green
= png_ptr
->gamma_to_1
[green
];
3323 blue
= png_ptr
->gamma_to_1
[blue
];
3326 *(dp
++) = png_ptr
->gamma_from_1
[
3327 (rc
*red
+ gc
*green
+ bc
*blue
+ 16384)>>15];
3332 /* If there is no overall correction the table will not be
3335 if (png_ptr
->gamma_table
!= NULL
)
3336 red
= png_ptr
->gamma_table
[red
];
3352 for (i
= 0; i
< row_width
; i
++)
3354 png_byte red
= *(sp
++);
3355 png_byte green
= *(sp
++);
3356 png_byte blue
= *(sp
++);
3358 if (red
!= green
|| red
!= blue
)
3361 /* NOTE: this is the historical approach which simply
3362 * truncates the results.
3364 *(dp
++) = (png_byte
)((rc
*red
+ gc
*green
+ bc
*blue
)>>15);
3376 else /* RGB bit_depth == 16 */
3378 #ifdef PNG_READ_GAMMA_SUPPORTED
3379 if (png_ptr
->gamma_16_to_1
!= NULL
&& png_ptr
->gamma_16_from_1
!= NULL
)
3385 for (i
= 0; i
< row_width
; i
++)
3387 png_uint_16 red
, green
, blue
, w
;
3389 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3390 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3391 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3393 if (red
== green
&& red
== blue
)
3395 if (png_ptr
->gamma_16_table
!= NULL
)
3396 w
= png_ptr
->gamma_16_table
[(red
&0xff)
3397 >> png_ptr
->gamma_shift
][red
>>8];
3405 png_uint_16 red_1
= png_ptr
->gamma_16_to_1
[(red
&0xff)
3406 >> png_ptr
->gamma_shift
][red
>>8];
3407 png_uint_16 green_1
=
3408 png_ptr
->gamma_16_to_1
[(green
&0xff) >>
3409 png_ptr
->gamma_shift
][green
>>8];
3410 png_uint_16 blue_1
= png_ptr
->gamma_16_to_1
[(blue
&0xff)
3411 >> png_ptr
->gamma_shift
][blue
>>8];
3412 png_uint_16 gray16
= (png_uint_16
)((rc
*red_1
+ gc
*green_1
3413 + bc
*blue_1
+ 16384)>>15);
3414 w
= png_ptr
->gamma_16_from_1
[(gray16
&0xff) >>
3415 png_ptr
->gamma_shift
][gray16
>> 8];
3419 *(dp
++) = (png_byte
)((w
>>8) & 0xff);
3420 *(dp
++) = (png_byte
)(w
& 0xff);
3436 for (i
= 0; i
< row_width
; i
++)
3438 png_uint_16 red
, green
, blue
, gray16
;
3440 red
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3441 green
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3442 blue
= (png_uint_16
)(((*(sp
))<<8) | *(sp
+ 1)); sp
+= 2;
3444 if (red
!= green
|| red
!= blue
)
3447 /* From 1.5.5 in the 16 bit case do the accurate conversion even
3448 * in the 'fast' case - this is because this is where the code
3449 * ends up when handling linear 16 bit data.
3451 gray16
= (png_uint_16
)((rc
*red
+ gc
*green
+ bc
*blue
+ 16384) >>
3453 *(dp
++) = (png_byte
)((gray16
>>8) & 0xff);
3454 *(dp
++) = (png_byte
)(gray16
& 0xff);
3465 row_info
->channels
= (png_byte
)(row_info
->channels
- 2);
3466 row_info
->color_type
= (png_byte
)(row_info
->color_type
&
3467 ~PNG_COLOR_MASK_COLOR
);
3468 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
*
3469 row_info
->bit_depth
);
3470 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
3475 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
3477 #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
3478 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
3479 * large of png_color. This lets grayscale images be treated as
3480 * paletted. Most useful for gamma correction and simplification
3481 * of code. This API is not used internally.
3484 png_build_grayscale_palette(int bit_depth
, png_colorp palette
)
3491 png_debug(1, "in png_do_build_grayscale_palette");
3493 if (palette
== NULL
)
3524 for (i
= 0, v
= 0; i
< num_palette
; i
++, v
+= color_inc
)
3526 palette
[i
].red
= (png_byte
)v
;
3527 palette
[i
].green
= (png_byte
)v
;
3528 palette
[i
].blue
= (png_byte
)v
;
3534 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
3535 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
3536 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
3537 /* Replace any alpha or transparency with the supplied background color.
3538 * "background" is already in the screen gamma, while "background_1" is
3539 * at a gamma of 1.0. Paletted files have already been taken care of.
3542 png_do_compose(png_row_infop row_info
, png_bytep row
, png_structrp png_ptr
)
3544 #ifdef PNG_READ_GAMMA_SUPPORTED
3545 png_const_bytep gamma_table
= png_ptr
->gamma_table
;
3546 png_const_bytep gamma_from_1
= png_ptr
->gamma_from_1
;
3547 png_const_bytep gamma_to_1
= png_ptr
->gamma_to_1
;
3548 png_const_uint_16pp gamma_16
= png_ptr
->gamma_16_table
;
3549 png_const_uint_16pp gamma_16_from_1
= png_ptr
->gamma_16_from_1
;
3550 png_const_uint_16pp gamma_16_to_1
= png_ptr
->gamma_16_to_1
;
3551 int gamma_shift
= png_ptr
->gamma_shift
;
3552 int optimize
= (png_ptr
->flags
& PNG_FLAG_OPTIMIZE_ALPHA
) != 0;
3557 png_uint_32 row_width
= row_info
->width
;
3560 png_debug(1, "in png_do_compose");
3563 switch (row_info
->color_type
)
3565 case PNG_COLOR_TYPE_GRAY
:
3567 switch (row_info
->bit_depth
)
3573 for (i
= 0; i
< row_width
; i
++)
3575 if ((png_uint_16
)((*sp
>> shift
) & 0x01)
3576 == png_ptr
->trans_color
.gray
)
3578 unsigned int tmp
= *sp
& (0x7f7f >> (7 - shift
));
3579 tmp
|= png_ptr
->background
.gray
<< shift
;
3580 *sp
= (png_byte
)(tmp
& 0xff);
3597 #ifdef PNG_READ_GAMMA_SUPPORTED
3598 if (gamma_table
!= NULL
)
3602 for (i
= 0; i
< row_width
; i
++)
3604 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
3605 == png_ptr
->trans_color
.gray
)
3607 unsigned int tmp
= *sp
& (0x3f3f >> (6 - shift
));
3608 tmp
|= png_ptr
->background
.gray
<< shift
;
3609 *sp
= (png_byte
)(tmp
& 0xff);
3614 unsigned int p
= (*sp
>> shift
) & 0x03;
3615 unsigned int g
= (gamma_table
[p
| (p
<< 2) |
3616 (p
<< 4) | (p
<< 6)] >> 6) & 0x03;
3617 unsigned int tmp
= *sp
& (0x3f3f >> (6 - shift
));
3619 *sp
= (png_byte
)(tmp
& 0xff);
3638 for (i
= 0; i
< row_width
; i
++)
3640 if ((png_uint_16
)((*sp
>> shift
) & 0x03)
3641 == png_ptr
->trans_color
.gray
)
3643 unsigned int tmp
= *sp
& (0x3f3f >> (6 - shift
));
3644 tmp
|= png_ptr
->background
.gray
<< shift
;
3645 *sp
= (png_byte
)(tmp
& 0xff);
3663 #ifdef PNG_READ_GAMMA_SUPPORTED
3664 if (gamma_table
!= NULL
)
3668 for (i
= 0; i
< row_width
; i
++)
3670 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
3671 == png_ptr
->trans_color
.gray
)
3673 unsigned int tmp
= *sp
& (0xf0f >> (4 - shift
));
3674 tmp
|= png_ptr
->background
.gray
<< shift
;
3675 *sp
= (png_byte
)(tmp
& 0xff);
3680 unsigned int p
= (*sp
>> shift
) & 0x0f;
3681 unsigned int g
= (gamma_table
[p
| (p
<< 4)] >> 4) &
3683 unsigned int tmp
= *sp
& (0xf0f >> (4 - shift
));
3685 *sp
= (png_byte
)(tmp
& 0xff);
3704 for (i
= 0; i
< row_width
; i
++)
3706 if ((png_uint_16
)((*sp
>> shift
) & 0x0f)
3707 == png_ptr
->trans_color
.gray
)
3709 unsigned int tmp
= *sp
& (0xf0f >> (4 - shift
));
3710 tmp
|= png_ptr
->background
.gray
<< shift
;
3711 *sp
= (png_byte
)(tmp
& 0xff);
3729 #ifdef PNG_READ_GAMMA_SUPPORTED
3730 if (gamma_table
!= NULL
)
3733 for (i
= 0; i
< row_width
; i
++, sp
++)
3735 if (*sp
== png_ptr
->trans_color
.gray
)
3736 *sp
= (png_byte
)png_ptr
->background
.gray
;
3739 *sp
= gamma_table
[*sp
];
3746 for (i
= 0; i
< row_width
; i
++, sp
++)
3748 if (*sp
== png_ptr
->trans_color
.gray
)
3749 *sp
= (png_byte
)png_ptr
->background
.gray
;
3757 #ifdef PNG_READ_GAMMA_SUPPORTED
3758 if (gamma_16
!= NULL
)
3761 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3765 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3767 if (v
== png_ptr
->trans_color
.gray
)
3769 /* Background is already in screen gamma */
3770 *sp
= (png_byte
)((png_ptr
->background
.gray
>> 8)
3772 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.gray
3778 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3779 *sp
= (png_byte
)((v
>> 8) & 0xff);
3780 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3788 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3792 v
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3794 if (v
== png_ptr
->trans_color
.gray
)
3796 *sp
= (png_byte
)((png_ptr
->background
.gray
>> 8)
3798 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.gray
3812 case PNG_COLOR_TYPE_RGB
:
3814 if (row_info
->bit_depth
== 8)
3816 #ifdef PNG_READ_GAMMA_SUPPORTED
3817 if (gamma_table
!= NULL
)
3820 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3822 if (*sp
== png_ptr
->trans_color
.red
&&
3823 *(sp
+ 1) == png_ptr
->trans_color
.green
&&
3824 *(sp
+ 2) == png_ptr
->trans_color
.blue
)
3826 *sp
= (png_byte
)png_ptr
->background
.red
;
3827 *(sp
+ 1) = (png_byte
)png_ptr
->background
.green
;
3828 *(sp
+ 2) = (png_byte
)png_ptr
->background
.blue
;
3833 *sp
= gamma_table
[*sp
];
3834 *(sp
+ 1) = gamma_table
[*(sp
+ 1)];
3835 *(sp
+ 2) = gamma_table
[*(sp
+ 2)];
3843 for (i
= 0; i
< row_width
; i
++, sp
+= 3)
3845 if (*sp
== png_ptr
->trans_color
.red
&&
3846 *(sp
+ 1) == png_ptr
->trans_color
.green
&&
3847 *(sp
+ 2) == png_ptr
->trans_color
.blue
)
3849 *sp
= (png_byte
)png_ptr
->background
.red
;
3850 *(sp
+ 1) = (png_byte
)png_ptr
->background
.green
;
3851 *(sp
+ 2) = (png_byte
)png_ptr
->background
.blue
;
3856 else /* if (row_info->bit_depth == 16) */
3858 #ifdef PNG_READ_GAMMA_SUPPORTED
3859 if (gamma_16
!= NULL
)
3862 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3864 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3866 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
3869 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
3872 if (r
== png_ptr
->trans_color
.red
&&
3873 g
== png_ptr
->trans_color
.green
&&
3874 b
== png_ptr
->trans_color
.blue
)
3876 /* Background is already in screen gamma */
3877 *sp
= (png_byte
)((png_ptr
->background
.red
>> 8) & 0xff);
3878 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.red
& 0xff);
3879 *(sp
+ 2) = (png_byte
)((png_ptr
->background
.green
>> 8)
3881 *(sp
+ 3) = (png_byte
)(png_ptr
->background
.green
3883 *(sp
+ 4) = (png_byte
)((png_ptr
->background
.blue
>> 8)
3885 *(sp
+ 5) = (png_byte
)(png_ptr
->background
.blue
& 0xff);
3890 png_uint_16 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
3891 *sp
= (png_byte
)((v
>> 8) & 0xff);
3892 *(sp
+ 1) = (png_byte
)(v
& 0xff);
3894 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
3895 *(sp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
3896 *(sp
+ 3) = (png_byte
)(v
& 0xff);
3898 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
3899 *(sp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
3900 *(sp
+ 5) = (png_byte
)(v
& 0xff);
3909 for (i
= 0; i
< row_width
; i
++, sp
+= 6)
3911 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
3913 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
3916 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
3919 if (r
== png_ptr
->trans_color
.red
&&
3920 g
== png_ptr
->trans_color
.green
&&
3921 b
== png_ptr
->trans_color
.blue
)
3923 *sp
= (png_byte
)((png_ptr
->background
.red
>> 8) & 0xff);
3924 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.red
& 0xff);
3925 *(sp
+ 2) = (png_byte
)((png_ptr
->background
.green
>> 8)
3927 *(sp
+ 3) = (png_byte
)(png_ptr
->background
.green
3929 *(sp
+ 4) = (png_byte
)((png_ptr
->background
.blue
>> 8)
3931 *(sp
+ 5) = (png_byte
)(png_ptr
->background
.blue
& 0xff);
3939 case PNG_COLOR_TYPE_GRAY_ALPHA
:
3941 if (row_info
->bit_depth
== 8)
3943 #ifdef PNG_READ_GAMMA_SUPPORTED
3944 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
3945 gamma_table
!= NULL
)
3948 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3950 png_uint_16 a
= *(sp
+ 1);
3953 *sp
= gamma_table
[*sp
];
3957 /* Background is already in screen gamma */
3958 *sp
= (png_byte
)png_ptr
->background
.gray
;
3965 v
= gamma_to_1
[*sp
];
3966 png_composite(w
, v
, a
, png_ptr
->background_1
.gray
);
3968 w
= gamma_from_1
[w
];
3977 for (i
= 0; i
< row_width
; i
++, sp
+= 2)
3979 png_byte a
= *(sp
+ 1);
3982 *sp
= (png_byte
)png_ptr
->background
.gray
;
3985 png_composite(*sp
, *sp
, a
, png_ptr
->background
.gray
);
3989 else /* if (png_ptr->bit_depth == 16) */
3991 #ifdef PNG_READ_GAMMA_SUPPORTED
3992 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
3993 gamma_16_to_1
!= NULL
)
3996 for (i
= 0; i
< row_width
; i
++, sp
+= 4)
3998 png_uint_16 a
= (png_uint_16
)(((*(sp
+ 2)) << 8)
4001 if (a
== (png_uint_16
)0xffff)
4005 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
4006 *sp
= (png_byte
)((v
>> 8) & 0xff);
4007 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4012 /* Background is already in screen gamma */
4013 *sp
= (png_byte
)((png_ptr
->background
.gray
>> 8)
4015 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.gray
& 0xff);
4020 png_uint_16 g
, v
, w
;
4022 g
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
4023 png_composite_16(v
, g
, a
, png_ptr
->background_1
.gray
);
4027 w
= gamma_16_from_1
[(v
&0xff) >> gamma_shift
][v
>> 8];
4028 *sp
= (png_byte
)((w
>> 8) & 0xff);
4029 *(sp
+ 1) = (png_byte
)(w
& 0xff);
4037 for (i
= 0; i
< row_width
; i
++, sp
+= 4)
4039 png_uint_16 a
= (png_uint_16
)(((*(sp
+ 2)) << 8)
4044 *sp
= (png_byte
)((png_ptr
->background
.gray
>> 8)
4046 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.gray
& 0xff);
4049 else if (a
< 0xffff)
4053 g
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
4054 png_composite_16(v
, g
, a
, png_ptr
->background
.gray
);
4055 *sp
= (png_byte
)((v
>> 8) & 0xff);
4056 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4064 case PNG_COLOR_TYPE_RGB_ALPHA
:
4066 if (row_info
->bit_depth
== 8)
4068 #ifdef PNG_READ_GAMMA_SUPPORTED
4069 if (gamma_to_1
!= NULL
&& gamma_from_1
!= NULL
&&
4070 gamma_table
!= NULL
)
4073 for (i
= 0; i
< row_width
; i
++, sp
+= 4)
4075 png_byte a
= *(sp
+ 3);
4079 *sp
= gamma_table
[*sp
];
4080 *(sp
+ 1) = gamma_table
[*(sp
+ 1)];
4081 *(sp
+ 2) = gamma_table
[*(sp
+ 2)];
4086 /* Background is already in screen gamma */
4087 *sp
= (png_byte
)png_ptr
->background
.red
;
4088 *(sp
+ 1) = (png_byte
)png_ptr
->background
.green
;
4089 *(sp
+ 2) = (png_byte
)png_ptr
->background
.blue
;
4096 v
= gamma_to_1
[*sp
];
4097 png_composite(w
, v
, a
, png_ptr
->background_1
.red
);
4098 if (!optimize
) w
= gamma_from_1
[w
];
4101 v
= gamma_to_1
[*(sp
+ 1)];
4102 png_composite(w
, v
, a
, png_ptr
->background_1
.green
);
4103 if (!optimize
) w
= gamma_from_1
[w
];
4106 v
= gamma_to_1
[*(sp
+ 2)];
4107 png_composite(w
, v
, a
, png_ptr
->background_1
.blue
);
4108 if (!optimize
) w
= gamma_from_1
[w
];
4117 for (i
= 0; i
< row_width
; i
++, sp
+= 4)
4119 png_byte a
= *(sp
+ 3);
4123 *sp
= (png_byte
)png_ptr
->background
.red
;
4124 *(sp
+ 1) = (png_byte
)png_ptr
->background
.green
;
4125 *(sp
+ 2) = (png_byte
)png_ptr
->background
.blue
;
4130 png_composite(*sp
, *sp
, a
, png_ptr
->background
.red
);
4132 png_composite(*(sp
+ 1), *(sp
+ 1), a
,
4133 png_ptr
->background
.green
);
4135 png_composite(*(sp
+ 2), *(sp
+ 2), a
,
4136 png_ptr
->background
.blue
);
4141 else /* if (row_info->bit_depth == 16) */
4143 #ifdef PNG_READ_GAMMA_SUPPORTED
4144 if (gamma_16
!= NULL
&& gamma_16_from_1
!= NULL
&&
4145 gamma_16_to_1
!= NULL
)
4148 for (i
= 0; i
< row_width
; i
++, sp
+= 8)
4150 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
4151 << 8) + (png_uint_16
)(*(sp
+ 7)));
4153 if (a
== (png_uint_16
)0xffff)
4157 v
= gamma_16
[*(sp
+ 1) >> gamma_shift
][*sp
];
4158 *sp
= (png_byte
)((v
>> 8) & 0xff);
4159 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4161 v
= gamma_16
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
4162 *(sp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
4163 *(sp
+ 3) = (png_byte
)(v
& 0xff);
4165 v
= gamma_16
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
4166 *(sp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
4167 *(sp
+ 5) = (png_byte
)(v
& 0xff);
4172 /* Background is already in screen gamma */
4173 *sp
= (png_byte
)((png_ptr
->background
.red
>> 8) & 0xff);
4174 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.red
& 0xff);
4175 *(sp
+ 2) = (png_byte
)((png_ptr
->background
.green
>> 8)
4177 *(sp
+ 3) = (png_byte
)(png_ptr
->background
.green
4179 *(sp
+ 4) = (png_byte
)((png_ptr
->background
.blue
>> 8)
4181 *(sp
+ 5) = (png_byte
)(png_ptr
->background
.blue
& 0xff);
4188 v
= gamma_16_to_1
[*(sp
+ 1) >> gamma_shift
][*sp
];
4189 png_composite_16(w
, v
, a
, png_ptr
->background_1
.red
);
4191 w
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>>
4193 *sp
= (png_byte
)((w
>> 8) & 0xff);
4194 *(sp
+ 1) = (png_byte
)(w
& 0xff);
4196 v
= gamma_16_to_1
[*(sp
+ 3) >> gamma_shift
][*(sp
+ 2)];
4197 png_composite_16(w
, v
, a
, png_ptr
->background_1
.green
);
4199 w
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>>
4202 *(sp
+ 2) = (png_byte
)((w
>> 8) & 0xff);
4203 *(sp
+ 3) = (png_byte
)(w
& 0xff);
4205 v
= gamma_16_to_1
[*(sp
+ 5) >> gamma_shift
][*(sp
+ 4)];
4206 png_composite_16(w
, v
, a
, png_ptr
->background_1
.blue
);
4208 w
= gamma_16_from_1
[((w
&0xff) >> gamma_shift
)][w
>>
4211 *(sp
+ 4) = (png_byte
)((w
>> 8) & 0xff);
4212 *(sp
+ 5) = (png_byte
)(w
& 0xff);
4221 for (i
= 0; i
< row_width
; i
++, sp
+= 8)
4223 png_uint_16 a
= (png_uint_16
)(((png_uint_16
)(*(sp
+ 6))
4224 << 8) + (png_uint_16
)(*(sp
+ 7)));
4228 *sp
= (png_byte
)((png_ptr
->background
.red
>> 8) & 0xff);
4229 *(sp
+ 1) = (png_byte
)(png_ptr
->background
.red
& 0xff);
4230 *(sp
+ 2) = (png_byte
)((png_ptr
->background
.green
>> 8)
4232 *(sp
+ 3) = (png_byte
)(png_ptr
->background
.green
4234 *(sp
+ 4) = (png_byte
)((png_ptr
->background
.blue
>> 8)
4236 *(sp
+ 5) = (png_byte
)(png_ptr
->background
.blue
& 0xff);
4239 else if (a
< 0xffff)
4243 png_uint_16 r
= (png_uint_16
)(((*sp
) << 8) + *(sp
+ 1));
4244 png_uint_16 g
= (png_uint_16
)(((*(sp
+ 2)) << 8)
4246 png_uint_16 b
= (png_uint_16
)(((*(sp
+ 4)) << 8)
4249 png_composite_16(v
, r
, a
, png_ptr
->background
.red
);
4250 *sp
= (png_byte
)((v
>> 8) & 0xff);
4251 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4253 png_composite_16(v
, g
, a
, png_ptr
->background
.green
);
4254 *(sp
+ 2) = (png_byte
)((v
>> 8) & 0xff);
4255 *(sp
+ 3) = (png_byte
)(v
& 0xff);
4257 png_composite_16(v
, b
, a
, png_ptr
->background
.blue
);
4258 *(sp
+ 4) = (png_byte
)((v
>> 8) & 0xff);
4259 *(sp
+ 5) = (png_byte
)(v
& 0xff);
4272 #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */
4274 #ifdef PNG_READ_GAMMA_SUPPORTED
4275 /* Gamma correct the image, avoiding the alpha channel. Make sure
4276 * you do this after you deal with the transparency issue on grayscale
4277 * or RGB images. If your bit depth is 8, use gamma_table, if it
4278 * is 16, use gamma_16_table and gamma_shift. Build these with
4279 * build_gamma_table().
4282 png_do_gamma(png_row_infop row_info
, png_bytep row
, png_structrp png_ptr
)
4284 png_const_bytep gamma_table
= png_ptr
->gamma_table
;
4285 png_const_uint_16pp gamma_16_table
= png_ptr
->gamma_16_table
;
4286 int gamma_shift
= png_ptr
->gamma_shift
;
4290 png_uint_32 row_width
=row_info
->width
;
4292 png_debug(1, "in png_do_gamma");
4294 if (((row_info
->bit_depth
<= 8 && gamma_table
!= NULL
) ||
4295 (row_info
->bit_depth
== 16 && gamma_16_table
!= NULL
)))
4297 switch (row_info
->color_type
)
4299 case PNG_COLOR_TYPE_RGB
:
4301 if (row_info
->bit_depth
== 8)
4304 for (i
= 0; i
< row_width
; i
++)
4306 *sp
= gamma_table
[*sp
];
4308 *sp
= gamma_table
[*sp
];
4310 *sp
= gamma_table
[*sp
];
4315 else /* if (row_info->bit_depth == 16) */
4318 for (i
= 0; i
< row_width
; i
++)
4322 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4323 *sp
= (png_byte
)((v
>> 8) & 0xff);
4324 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4327 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4328 *sp
= (png_byte
)((v
>> 8) & 0xff);
4329 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4332 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4333 *sp
= (png_byte
)((v
>> 8) & 0xff);
4334 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4341 case PNG_COLOR_TYPE_RGB_ALPHA
:
4343 if (row_info
->bit_depth
== 8)
4346 for (i
= 0; i
< row_width
; i
++)
4348 *sp
= gamma_table
[*sp
];
4351 *sp
= gamma_table
[*sp
];
4354 *sp
= gamma_table
[*sp
];
4361 else /* if (row_info->bit_depth == 16) */
4364 for (i
= 0; i
< row_width
; i
++)
4366 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4367 *sp
= (png_byte
)((v
>> 8) & 0xff);
4368 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4371 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4372 *sp
= (png_byte
)((v
>> 8) & 0xff);
4373 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4376 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4377 *sp
= (png_byte
)((v
>> 8) & 0xff);
4378 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4385 case PNG_COLOR_TYPE_GRAY_ALPHA
:
4387 if (row_info
->bit_depth
== 8)
4390 for (i
= 0; i
< row_width
; i
++)
4392 *sp
= gamma_table
[*sp
];
4397 else /* if (row_info->bit_depth == 16) */
4400 for (i
= 0; i
< row_width
; i
++)
4402 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4403 *sp
= (png_byte
)((v
>> 8) & 0xff);
4404 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4411 case PNG_COLOR_TYPE_GRAY
:
4413 if (row_info
->bit_depth
== 2)
4416 for (i
= 0; i
< row_width
; i
+= 4)
4424 ((((int)gamma_table
[a
|(a
>>2)|(a
>>4)|(a
>>6)]) ) & 0xc0)|
4425 ((((int)gamma_table
[(b
<<2)|b
|(b
>>2)|(b
>>4)])>>2) & 0x30)|
4426 ((((int)gamma_table
[(c
<<4)|(c
<<2)|c
|(c
>>2)])>>4) & 0x0c)|
4427 ((((int)gamma_table
[(d
<<6)|(d
<<4)|(d
<<2)|d
])>>6) ));
4432 if (row_info
->bit_depth
== 4)
4435 for (i
= 0; i
< row_width
; i
+= 2)
4437 int msb
= *sp
& 0xf0;
4438 int lsb
= *sp
& 0x0f;
4440 *sp
= (png_byte
)((((int)gamma_table
[msb
| (msb
>> 4)]) & 0xf0)
4441 | (((int)gamma_table
[(lsb
<< 4) | lsb
]) >> 4));
4446 else if (row_info
->bit_depth
== 8)
4449 for (i
= 0; i
< row_width
; i
++)
4451 *sp
= gamma_table
[*sp
];
4456 else if (row_info
->bit_depth
== 16)
4459 for (i
= 0; i
< row_width
; i
++)
4461 png_uint_16 v
= gamma_16_table
[*(sp
+ 1) >> gamma_shift
][*sp
];
4462 *sp
= (png_byte
)((v
>> 8) & 0xff);
4463 *(sp
+ 1) = (png_byte
)(v
& 0xff);
4477 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4478 /* Encode the alpha channel to the output gamma (the input channel is always
4479 * linear.) Called only with color types that have an alpha channel. Needs the
4483 png_do_encode_alpha(png_row_infop row_info
, png_bytep row
, png_structrp png_ptr
)
4485 png_uint_32 row_width
= row_info
->width
;
4487 png_debug(1, "in png_do_encode_alpha");
4489 if (row_info
->color_type
& PNG_COLOR_MASK_ALPHA
)
4491 if (row_info
->bit_depth
== 8)
4493 PNG_CONST png_bytep table
= png_ptr
->gamma_from_1
;
4497 PNG_CONST
int step
=
4498 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
) ? 4 : 2;
4500 /* The alpha channel is the last component: */
4503 for (; row_width
> 0; --row_width
, row
+= step
)
4510 else if (row_info
->bit_depth
== 16)
4512 PNG_CONST png_uint_16pp table
= png_ptr
->gamma_16_from_1
;
4513 PNG_CONST
int gamma_shift
= png_ptr
->gamma_shift
;
4517 PNG_CONST
int step
=
4518 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
) ? 8 : 4;
4520 /* The alpha channel is the last component: */
4523 for (; row_width
> 0; --row_width
, row
+= step
)
4527 v
= table
[*(row
+ 1) >> gamma_shift
][*row
];
4528 *row
= (png_byte
)((v
>> 8) & 0xff);
4529 *(row
+ 1) = (png_byte
)(v
& 0xff);
4537 /* Only get to here if called with a weird row_info; no harm has been done,
4538 * so just issue a warning.
4540 png_warning(png_ptr
, "png_do_encode_alpha: unexpected call");
4544 #ifdef PNG_READ_EXPAND_SUPPORTED
4545 /* Expands a palette row to an RGB or RGBA row depending
4546 * upon whether you supply trans and num_trans.
4549 png_do_expand_palette(png_row_infop row_info
, png_bytep row
,
4550 png_const_colorp palette
, png_const_bytep trans_alpha
, int num_trans
)
4555 png_uint_32 row_width
=row_info
->width
;
4557 png_debug(1, "in png_do_expand_palette");
4559 if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
)
4561 if (row_info
->bit_depth
< 8)
4563 switch (row_info
->bit_depth
)
4567 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
4568 dp
= row
+ (png_size_t
)row_width
- 1;
4569 shift
= 7 - (int)((row_width
+ 7) & 0x07);
4570 for (i
= 0; i
< row_width
; i
++)
4572 if ((*sp
>> shift
) & 0x01)
4594 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
4595 dp
= row
+ (png_size_t
)row_width
- 1;
4596 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
4597 for (i
= 0; i
< row_width
; i
++)
4599 value
= (*sp
>> shift
) & 0x03;
4600 *dp
= (png_byte
)value
;
4617 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
4618 dp
= row
+ (png_size_t
)row_width
- 1;
4619 shift
= (int)((row_width
& 0x01) << 2);
4620 for (i
= 0; i
< row_width
; i
++)
4622 value
= (*sp
>> shift
) & 0x0f;
4623 *dp
= (png_byte
)value
;
4641 row_info
->bit_depth
= 8;
4642 row_info
->pixel_depth
= 8;
4643 row_info
->rowbytes
= row_width
;
4646 if (row_info
->bit_depth
== 8)
4651 sp
= row
+ (png_size_t
)row_width
- 1;
4652 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
4654 for (i
= 0; i
< row_width
; i
++)
4656 if ((int)(*sp
) >= num_trans
)
4660 *dp
-- = trans_alpha
[*sp
];
4662 *dp
-- = palette
[*sp
].blue
;
4663 *dp
-- = palette
[*sp
].green
;
4664 *dp
-- = palette
[*sp
].red
;
4667 row_info
->bit_depth
= 8;
4668 row_info
->pixel_depth
= 32;
4669 row_info
->rowbytes
= row_width
* 4;
4670 row_info
->color_type
= 6;
4671 row_info
->channels
= 4;
4676 sp
= row
+ (png_size_t
)row_width
- 1;
4677 dp
= row
+ (png_size_t
)(row_width
* 3) - 1;
4679 for (i
= 0; i
< row_width
; i
++)
4681 *dp
-- = palette
[*sp
].blue
;
4682 *dp
-- = palette
[*sp
].green
;
4683 *dp
-- = palette
[*sp
].red
;
4687 row_info
->bit_depth
= 8;
4688 row_info
->pixel_depth
= 24;
4689 row_info
->rowbytes
= row_width
* 3;
4690 row_info
->color_type
= 2;
4691 row_info
->channels
= 3;
4698 /* If the bit depth < 8, it is expanded to 8. Also, if the already
4699 * expanded transparency value is supplied, an alpha channel is built.
4702 png_do_expand(png_row_infop row_info
, png_bytep row
,
4703 png_const_color_16p trans_color
)
4708 png_uint_32 row_width
=row_info
->width
;
4710 png_debug(1, "in png_do_expand");
4713 if (row_info
->color_type
== PNG_COLOR_TYPE_GRAY
)
4715 unsigned int gray
= trans_color
? trans_color
->gray
: 0;
4717 if (row_info
->bit_depth
< 8)
4719 switch (row_info
->bit_depth
)
4723 gray
= (gray
& 0x01) * 0xff;
4724 sp
= row
+ (png_size_t
)((row_width
- 1) >> 3);
4725 dp
= row
+ (png_size_t
)row_width
- 1;
4726 shift
= 7 - (int)((row_width
+ 7) & 0x07);
4727 for (i
= 0; i
< row_width
; i
++)
4729 if ((*sp
>> shift
) & 0x01)
4751 gray
= (gray
& 0x03) * 0x55;
4752 sp
= row
+ (png_size_t
)((row_width
- 1) >> 2);
4753 dp
= row
+ (png_size_t
)row_width
- 1;
4754 shift
= (int)((3 - ((row_width
+ 3) & 0x03)) << 1);
4755 for (i
= 0; i
< row_width
; i
++)
4757 value
= (*sp
>> shift
) & 0x03;
4758 *dp
= (png_byte
)(value
| (value
<< 2) | (value
<< 4) |
4776 gray
= (gray
& 0x0f) * 0x11;
4777 sp
= row
+ (png_size_t
)((row_width
- 1) >> 1);
4778 dp
= row
+ (png_size_t
)row_width
- 1;
4779 shift
= (int)((1 - ((row_width
+ 1) & 0x01)) << 2);
4780 for (i
= 0; i
< row_width
; i
++)
4782 value
= (*sp
>> shift
) & 0x0f;
4783 *dp
= (png_byte
)(value
| (value
<< 4));
4802 row_info
->bit_depth
= 8;
4803 row_info
->pixel_depth
= 8;
4804 row_info
->rowbytes
= row_width
;
4807 if (trans_color
!= NULL
)
4809 if (row_info
->bit_depth
== 8)
4812 sp
= row
+ (png_size_t
)row_width
- 1;
4813 dp
= row
+ (png_size_t
)(row_width
<< 1) - 1;
4815 for (i
= 0; i
< row_width
; i
++)
4827 else if (row_info
->bit_depth
== 16)
4829 unsigned int gray_high
= (gray
>> 8) & 0xff;
4830 unsigned int gray_low
= gray
& 0xff;
4831 sp
= row
+ row_info
->rowbytes
- 1;
4832 dp
= row
+ (row_info
->rowbytes
<< 1) - 1;
4833 for (i
= 0; i
< row_width
; i
++)
4835 if (*(sp
- 1) == gray_high
&& *(sp
) == gray_low
)
4852 row_info
->color_type
= PNG_COLOR_TYPE_GRAY_ALPHA
;
4853 row_info
->channels
= 2;
4854 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 1);
4855 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
,
4859 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&& trans_color
)
4861 if (row_info
->bit_depth
== 8)
4863 png_byte red
= (png_byte
)(trans_color
->red
& 0xff);
4864 png_byte green
= (png_byte
)(trans_color
->green
& 0xff);
4865 png_byte blue
= (png_byte
)(trans_color
->blue
& 0xff);
4866 sp
= row
+ (png_size_t
)row_info
->rowbytes
- 1;
4867 dp
= row
+ (png_size_t
)(row_width
<< 2) - 1;
4868 for (i
= 0; i
< row_width
; i
++)
4870 if (*(sp
- 2) == red
&& *(sp
- 1) == green
&& *(sp
) == blue
)
4881 else if (row_info
->bit_depth
== 16)
4883 png_byte red_high
= (png_byte
)((trans_color
->red
>> 8) & 0xff);
4884 png_byte green_high
= (png_byte
)((trans_color
->green
>> 8) & 0xff);
4885 png_byte blue_high
= (png_byte
)((trans_color
->blue
>> 8) & 0xff);
4886 png_byte red_low
= (png_byte
)(trans_color
->red
& 0xff);
4887 png_byte green_low
= (png_byte
)(trans_color
->green
& 0xff);
4888 png_byte blue_low
= (png_byte
)(trans_color
->blue
& 0xff);
4889 sp
= row
+ row_info
->rowbytes
- 1;
4890 dp
= row
+ (png_size_t
)(row_width
<< 3) - 1;
4891 for (i
= 0; i
< row_width
; i
++)
4893 if (*(sp
- 5) == red_high
&&
4894 *(sp
- 4) == red_low
&&
4895 *(sp
- 3) == green_high
&&
4896 *(sp
- 2) == green_low
&&
4897 *(sp
- 1) == blue_high
&&
4918 row_info
->color_type
= PNG_COLOR_TYPE_RGB_ALPHA
;
4919 row_info
->channels
= 4;
4920 row_info
->pixel_depth
= (png_byte
)(row_info
->bit_depth
<< 2);
4921 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
4927 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4928 /* If the bit depth is 8 and the color type is not a palette type expand the
4929 * whole row to 16 bits. Has no effect otherwise.
4932 png_do_expand_16(png_row_infop row_info
, png_bytep row
)
4934 if (row_info
->bit_depth
== 8 &&
4935 row_info
->color_type
!= PNG_COLOR_TYPE_PALETTE
)
4937 /* The row have a sequence of bytes containing [0..255] and we need
4938 * to turn it into another row containing [0..65535], to do this we
4941 * (input / 255) * 65535
4943 * Which happens to be exactly input * 257 and this can be achieved
4944 * simply by byte replication in place (copying backwards).
4946 png_byte
*sp
= row
+ row_info
->rowbytes
; /* source, last byte + 1 */
4947 png_byte
*dp
= sp
+ row_info
->rowbytes
; /* destination, end + 1 */
4949 dp
[-2] = dp
[-1] = *--sp
, dp
-= 2;
4951 row_info
->rowbytes
*= 2;
4952 row_info
->bit_depth
= 16;
4953 row_info
->pixel_depth
= (png_byte
)(row_info
->channels
* 16);
4958 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4960 png_do_quantize(png_row_infop row_info
, png_bytep row
,
4961 png_const_bytep palette_lookup
, png_const_bytep quantize_lookup
)
4965 png_uint_32 row_width
=row_info
->width
;
4967 png_debug(1, "in png_do_quantize");
4969 if (row_info
->bit_depth
== 8)
4971 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
&& palette_lookup
)
4976 for (i
= 0; i
< row_width
; i
++)
4982 /* This looks real messy, but the compiler will reduce
4983 * it down to a reasonable formula. For example, with
4984 * 5 bits per color, we get:
4985 * p = (((r >> 3) & 0x1f) << 10) |
4986 * (((g >> 3) & 0x1f) << 5) |
4987 * ((b >> 3) & 0x1f);
4989 p
= (((r
>> (8 - PNG_QUANTIZE_RED_BITS
)) &
4990 ((1 << PNG_QUANTIZE_RED_BITS
) - 1)) <<
4991 (PNG_QUANTIZE_GREEN_BITS
+ PNG_QUANTIZE_BLUE_BITS
)) |
4992 (((g
>> (8 - PNG_QUANTIZE_GREEN_BITS
)) &
4993 ((1 << PNG_QUANTIZE_GREEN_BITS
) - 1)) <<
4994 (PNG_QUANTIZE_BLUE_BITS
)) |
4995 ((b
>> (8 - PNG_QUANTIZE_BLUE_BITS
)) &
4996 ((1 << PNG_QUANTIZE_BLUE_BITS
) - 1));
4998 *dp
++ = palette_lookup
[p
];
5001 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
5002 row_info
->channels
= 1;
5003 row_info
->pixel_depth
= row_info
->bit_depth
;
5004 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
5007 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
&&
5008 palette_lookup
!= NULL
)
5013 for (i
= 0; i
< row_width
; i
++)
5020 p
= (((r
>> (8 - PNG_QUANTIZE_RED_BITS
)) &
5021 ((1 << PNG_QUANTIZE_RED_BITS
) - 1)) <<
5022 (PNG_QUANTIZE_GREEN_BITS
+ PNG_QUANTIZE_BLUE_BITS
)) |
5023 (((g
>> (8 - PNG_QUANTIZE_GREEN_BITS
)) &
5024 ((1 << PNG_QUANTIZE_GREEN_BITS
) - 1)) <<
5025 (PNG_QUANTIZE_BLUE_BITS
)) |
5026 ((b
>> (8 - PNG_QUANTIZE_BLUE_BITS
)) &
5027 ((1 << PNG_QUANTIZE_BLUE_BITS
) - 1));
5029 *dp
++ = palette_lookup
[p
];
5032 row_info
->color_type
= PNG_COLOR_TYPE_PALETTE
;
5033 row_info
->channels
= 1;
5034 row_info
->pixel_depth
= row_info
->bit_depth
;
5035 row_info
->rowbytes
= PNG_ROWBYTES(row_info
->pixel_depth
, row_width
);
5038 else if (row_info
->color_type
== PNG_COLOR_TYPE_PALETTE
&&
5043 for (i
= 0; i
< row_width
; i
++, sp
++)
5045 *sp
= quantize_lookup
[*sp
];
5050 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
5051 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
5053 #ifdef PNG_MNG_FEATURES_SUPPORTED
5054 /* Undoes intrapixel differencing */
5056 png_do_read_intrapixel(png_row_infop row_info
, png_bytep row
)
5058 png_debug(1, "in png_do_read_intrapixel");
5061 (row_info
->color_type
& PNG_COLOR_MASK_COLOR
))
5063 int bytes_per_pixel
;
5064 png_uint_32 row_width
= row_info
->width
;
5066 if (row_info
->bit_depth
== 8)
5071 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
5072 bytes_per_pixel
= 3;
5074 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
5075 bytes_per_pixel
= 4;
5080 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
5082 *(rp
) = (png_byte
)((256 + *rp
+ *(rp
+ 1)) & 0xff);
5083 *(rp
+2) = (png_byte
)((256 + *(rp
+ 2) + *(rp
+ 1)) & 0xff);
5086 else if (row_info
->bit_depth
== 16)
5091 if (row_info
->color_type
== PNG_COLOR_TYPE_RGB
)
5092 bytes_per_pixel
= 6;
5094 else if (row_info
->color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)
5095 bytes_per_pixel
= 8;
5100 for (i
= 0, rp
= row
; i
< row_width
; i
++, rp
+= bytes_per_pixel
)
5102 png_uint_32 s0
= (*(rp
) << 8) | *(rp
+ 1);
5103 png_uint_32 s1
= (*(rp
+ 2) << 8) | *(rp
+ 3);
5104 png_uint_32 s2
= (*(rp
+ 4) << 8) | *(rp
+ 5);
5105 png_uint_32 red
= (s0
+ s1
+ 65536) & 0xffff;
5106 png_uint_32 blue
= (s2
+ s1
+ 65536) & 0xffff;
5107 *(rp
) = (png_byte
)((red
>> 8) & 0xff);
5108 *(rp
+ 1) = (png_byte
)(red
& 0xff);
5109 *(rp
+ 4) = (png_byte
)((blue
>> 8) & 0xff);
5110 *(rp
+ 5) = (png_byte
)(blue
& 0xff);
5115 #endif /* PNG_MNG_FEATURES_SUPPORTED */
5116 #endif /* PNG_READ_SUPPORTED */