blob: 9a8fad9f4aa7c6727760a58ae9575dd08de0b3c8 [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngrtran.c - transforms the data in a row for PNG readers
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Cosmin Truta70d122a2019-02-03 19:51:18 -05004 * Copyright (c) 2018-2019 Cosmin Truta
Glenn Randers-Pehrson0e06b592018-03-06 13:54:52 -06005 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
Cosmin Trutaa8738932018-07-28 18:47:21 -04006 * Copyright (c) 1996-1997 Andreas Dilger
7 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06008 *
Glenn Randers-Pehrsonbfbf8652009-06-26 21:46:52 -05009 * This code is released under the libpng license.
Glenn Randers-Pehrsonc332bbc2009-06-25 13:43:50 -050010 * For conditions of distribution and use, see the disclaimer
Glenn Randers-Pehrson037023b2009-06-24 10:27:36 -050011 * and license in png.h
Glenn Randers-Pehrson3e61d792009-06-24 09:31:28 -050012 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -060013 * This file contains functions optionally called by an application
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060014 * in order to tell libpng how to handle data when reading a PNG.
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -050015 * Transformations that are used in both reading and writing are
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060016 * in pngtrans.c.
17 */
Guy Schalnat0d580581995-07-20 02:43:20 -050018
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050019#include "pngpriv.h"
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -060020
Richard Townsend7734cda2018-01-25 19:03:04 +000021#ifdef PNG_ARM_NEON_IMPLEMENTATION
Cosmin Truta1ceaa832018-09-04 00:53:38 -040022# if PNG_ARM_NEON_IMPLEMENTATION == 1
23# define PNG_ARM_NEON_INTRINSICS_AVAILABLE
24# if defined(_MSC_VER) && defined(_M_ARM64)
25# include <arm64_neon.h>
26# else
27# include <arm_neon.h>
28# endif
29# endif
Richard Townsend7734cda2018-01-25 19:03:04 +000030#endif
31
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060032#ifdef PNG_READ_SUPPORTED
33
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060034/* Set the action on getting a CRC error for an ancillary or critical chunk. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050035void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -060036png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060037{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050038 png_debug(1, "in png_set_crc_action");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050039
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050040 if (png_ptr == NULL)
41 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050042
43 /* Tell libpng how we react to CRC errors in critical chunks */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060044 switch (crit_action)
45 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050046 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060047 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050048
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050049 case PNG_CRC_WARN_USE: /* Warn/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060050 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
51 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
52 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050053
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050054 case PNG_CRC_QUIET_USE: /* Quiet/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060055 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
56 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
57 PNG_FLAG_CRC_CRITICAL_IGNORE;
58 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050059
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050060 case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050061 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -050062 "Can't discard critical data on CRC error");
John Bowler72d07d32017-07-11 07:50:35 -050063 /* FALLTHROUGH */
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050064 case PNG_CRC_ERROR_QUIT: /* Error/quit */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050065
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060066 case PNG_CRC_DEFAULT:
67 default:
68 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
69 break;
70 }
71
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050072 /* Tell libpng how we react to CRC errors in ancillary chunks */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060073 switch (ancil_action)
74 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050075 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060076 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050077
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050078 case PNG_CRC_WARN_USE: /* Warn/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060079 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
80 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
81 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050082
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050083 case PNG_CRC_QUIET_USE: /* Quiet/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060084 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
85 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
86 PNG_FLAG_CRC_ANCILLARY_NOWARN;
87 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050088
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050089 case PNG_CRC_ERROR_QUIT: /* Error/quit */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060090 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
91 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
92 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050093
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050094 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050095
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060096 case PNG_CRC_DEFAULT:
97 default:
98 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
99 break;
100 }
101}
102
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600103#ifdef PNG_READ_TRANSFORMS_SUPPORTED
104/* Is it OK to set a transformation now? Only if png_start_read_image or
105 * png_read_update_info have not been called. It is not necessary for the IHDR
Glenn Randers-Pehrson6df21522014-11-12 21:41:38 -0600106 * to have been read in all cases; the need_IHDR parameter allows for this
107 * check too.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600108 */
109static int
110png_rtran_ok(png_structrp png_ptr, int need_IHDR)
111{
112 if (png_ptr != NULL)
113 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500114 if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600115 png_app_error(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500116 "invalid after png_start_read_image or png_read_update_info");
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600117
118 else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0)
119 png_app_error(png_ptr, "invalid before the PNG header has been read");
120
121 else
122 {
123 /* Turn on failure to initialize correctly for all transforms. */
124 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
125
126 return 1; /* Ok */
127 }
128 }
129
130 return 0; /* no png_error possible! */
131}
132#endif
133
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500134#ifdef PNG_READ_BACKGROUND_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500135/* Handle alpha and tRNS via a background color */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500136void PNGFAPI
John Bowler5d567862011-12-24 09:12:00 -0600137png_set_background_fixed(png_structrp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500138 png_const_color_16p background_color, int background_gamma_code,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500139 int need_expand, png_fixed_point background_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -0500140{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500141 png_debug(1, "in png_set_background_fixed");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -0500142
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500143 if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500144 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500145
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600146 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
147 {
148 png_warning(png_ptr, "Application must supply a known background gamma");
149 return;
150 }
151
John Bowlerd273ad22011-05-07 21:00:28 -0500152 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
153 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
154 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
155
John Bowlerfcd301d2011-12-28 21:34:27 -0600156 png_ptr->background = *background_color;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500157 png_ptr->background_gamma = background_gamma;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -0500158 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600159 if (need_expand != 0)
John Bowlerd273ad22011-05-07 21:00:28 -0500160 png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
161 else
162 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500163}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500164
165# ifdef PNG_FLOATING_POINT_SUPPORTED
166void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600167png_set_background(png_structrp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500168 png_const_color_16p background_color, int background_gamma_code,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500169 int need_expand, double background_gamma)
170{
171 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
172 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
173}
Glenn Randers-Pehrson1a3fa1e2016-03-09 07:40:32 -0600174# endif /* FLOATING_POINT */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500175#endif /* READ_BACKGROUND */
Guy Schalnat0d580581995-07-20 02:43:20 -0500176
John Bowler8d261262011-06-18 13:37:11 -0500177/* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
178 * one that pngrtran does first (scale) happens. This is necessary to allow the
179 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
180 */
181#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500182void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600183png_set_scale_16(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500184{
Glenn Randers-Pehrsona7428d42011-06-17 19:07:04 -0500185 png_debug(1, "in png_set_scale_16");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500186
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500187 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500188 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500189
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500190 png_ptr->transformations |= PNG_SCALE_16_TO_8;
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500191}
John Bowler8d261262011-06-18 13:37:11 -0500192#endif
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500193
John Bowler8d261262011-06-18 13:37:11 -0500194#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500195/* Chop 16-bit depth files to 8-bit depth */
196void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600197png_set_strip_16(png_structrp png_ptr)
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500198{
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500199 png_debug(1, "in png_set_strip_16");
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500200
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500201 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500202 return;
203
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500204 png_ptr->transformations |= PNG_16_TO_8;
Guy Schalnat0d580581995-07-20 02:43:20 -0500205}
John Bowler8d261262011-06-18 13:37:11 -0500206#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500207
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500208#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500209void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600210png_set_strip_alpha(png_structrp png_ptr)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500211{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500212 png_debug(1, "in png_set_strip_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500213
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500214 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500215 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500216
John Bowler9b872f42011-02-12 09:00:16 -0600217 png_ptr->transformations |= PNG_STRIP_ALPHA;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500218}
219#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500220
John Bowlerf70c7d02011-05-10 22:54:37 -0500221#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
222static png_fixed_point
John Bowler5d567862011-12-24 09:12:00 -0600223translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500224 int is_screen)
John Bowlerf70c7d02011-05-10 22:54:37 -0500225{
226 /* Check for flag values. The main reason for having the old Mac value as a
227 * flag is that it is pretty near impossible to work out what the correct
228 * value is from Apple documentation - a working Mac system is needed to
229 * discover the value!
230 */
231 if (output_gamma == PNG_DEFAULT_sRGB ||
232 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
233 {
234 /* If there is no sRGB support this just sets the gamma to the standard
235 * sRGB value. (This is a side effect of using this function!)
236 */
237# ifdef PNG_READ_sRGB_SUPPORTED
238 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
John Bowlere4413a72013-04-17 21:27:47 -0500239# else
240 PNG_UNUSED(png_ptr)
John Bowlerf70c7d02011-05-10 22:54:37 -0500241# endif
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600242 if (is_screen != 0)
John Bowlerf70c7d02011-05-10 22:54:37 -0500243 output_gamma = PNG_GAMMA_sRGB;
244 else
245 output_gamma = PNG_GAMMA_sRGB_INVERSE;
246 }
247
248 else if (output_gamma == PNG_GAMMA_MAC_18 ||
249 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
250 {
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600251 if (is_screen != 0)
John Bowlerf70c7d02011-05-10 22:54:37 -0500252 output_gamma = PNG_GAMMA_MAC_OLD;
253 else
254 output_gamma = PNG_GAMMA_MAC_INVERSE;
255 }
256
257 return output_gamma;
258}
259
260# ifdef PNG_FLOATING_POINT_SUPPORTED
261static png_fixed_point
John Bowler5d567862011-12-24 09:12:00 -0600262convert_gamma_value(png_structrp png_ptr, double output_gamma)
John Bowlerf70c7d02011-05-10 22:54:37 -0500263{
264 /* The following silently ignores cases where fixed point (times 100,000)
265 * gamma values are passed to the floating point API. This is safe and it
266 * means the fixed point constants work just fine with the floating point
267 * API. The alternative would just lead to undetected errors and spurious
268 * bug reports. Negative values fail inside the _fixed API unless they
269 * correspond to the flag values.
270 */
271 if (output_gamma > 0 && output_gamma < 128)
272 output_gamma *= PNG_FP_1;
273
274 /* This preserves -1 and -2 exactly: */
275 output_gamma = floor(output_gamma + .5);
276
277 if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
278 png_fixed_error(png_ptr, "gamma value");
279
280 return (png_fixed_point)output_gamma;
281}
282# endif
283#endif /* READ_ALPHA_MODE || READ_GAMMA */
284
John Bowlerd273ad22011-05-07 21:00:28 -0500285#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
286void PNGFAPI
John Bowler5d567862011-12-24 09:12:00 -0600287png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500288 png_fixed_point output_gamma)
John Bowlerd273ad22011-05-07 21:00:28 -0500289{
290 int compose = 0;
291 png_fixed_point file_gamma;
292
293 png_debug(1, "in png_set_alpha_mode");
294
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500295 if (png_rtran_ok(png_ptr, 0) == 0)
John Bowlerd273ad22011-05-07 21:00:28 -0500296 return;
297
John Bowlerf70c7d02011-05-10 22:54:37 -0500298 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
John Bowlerd273ad22011-05-07 21:00:28 -0500299
John Bowlerf70c7d02011-05-10 22:54:37 -0500300 /* Validate the value to ensure it is in a reasonable range. The value
John Bowlerd273ad22011-05-07 21:00:28 -0500301 * is expected to be 1 or greater, but this range test allows for some
302 * viewing correction values. The intent is to weed out users of this API
303 * who use the inverse of the gamma value accidentally! Since some of these
John Bowlerf1eafe82016-01-29 18:09:49 -0800304 * values are reasonable this may have to be changed:
305 *
Unknownf23b41d2017-11-03 00:52:06 -0400306 * 1.6.x: changed from 0.07..3 to 0.01..100 (to accommodate the optimal 16-bit
John Bowlerf1eafe82016-01-29 18:09:49 -0800307 * gamma of 36, and its reciprocal.)
John Bowlerd273ad22011-05-07 21:00:28 -0500308 */
John Bowlerf1eafe82016-01-29 18:09:49 -0800309 if (output_gamma < 1000 || output_gamma > 10000000)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500310 png_error(png_ptr, "output gamma out of expected range");
John Bowlerd273ad22011-05-07 21:00:28 -0500311
Glenn Randers-Pehrsonc7822512011-05-07 21:23:43 -0500312 /* The default file gamma is the inverse of the output gamma; the output
John Bowlerd273ad22011-05-07 21:00:28 -0500313 * gamma may be changed below so get the file value first:
314 */
315 file_gamma = png_reciprocal(output_gamma);
316
317 /* There are really 8 possibilities here, composed of any combination
318 * of:
319 *
320 * premultiply the color channels
321 * do not encode non-opaque pixels
322 * encode the alpha as well as the color channels
323 *
324 * The differences disappear if the input/output ('screen') gamma is 1.0,
325 * because then the encoding is a no-op and there is only the choice of
326 * premultiplying the color channels or not.
327 *
328 * png_set_alpha_mode and png_set_background interact because both use
329 * png_compose to do the work. Calling both is only useful when
330 * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
331 * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
332 */
333 switch (mode)
334 {
335 case PNG_ALPHA_PNG: /* default: png standard */
336 /* No compose, but it may be set by png_set_background! */
337 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
338 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
339 break;
340
341 case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
342 compose = 1;
343 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
344 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
345 /* The output is linear: */
346 output_gamma = PNG_FP_1;
347 break;
348
349 case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
350 compose = 1;
351 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
352 png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
353 /* output_gamma records the encoding of opaque pixels! */
354 break;
355
356 case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
357 compose = 1;
358 png_ptr->transformations |= PNG_ENCODE_ALPHA;
359 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
360 break;
361
362 default:
363 png_error(png_ptr, "invalid alpha mode");
364 }
365
366 /* Only set the default gamma if the file gamma has not been set (this has
367 * the side effect that the gamma in a second call to png_set_alpha_mode will
368 * be ignored.)
369 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600370 if (png_ptr->colorspace.gamma == 0)
371 {
372 png_ptr->colorspace.gamma = file_gamma;
373 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
374 }
John Bowlerd273ad22011-05-07 21:00:28 -0500375
376 /* But always set the output gamma: */
377 png_ptr->screen_gamma = output_gamma;
378
Glenn Randers-Pehrsonc7822512011-05-07 21:23:43 -0500379 /* Finally, if pre-multiplying, set the background fields to achieve the
John Bowlerd273ad22011-05-07 21:00:28 -0500380 * desired result.
381 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600382 if (compose != 0)
John Bowlerd273ad22011-05-07 21:00:28 -0500383 {
384 /* And obtain alpha pre-multiplication by composing on black: */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600385 memset(&png_ptr->background, 0, (sizeof png_ptr->background));
386 png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */
John Bowlerd273ad22011-05-07 21:00:28 -0500387 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
388 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
389
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500390 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -0500391 png_error(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500392 "conflicting calls to set alpha mode and background");
John Bowlerd273ad22011-05-07 21:00:28 -0500393
394 png_ptr->transformations |= PNG_COMPOSE;
395 }
John Bowlerd273ad22011-05-07 21:00:28 -0500396}
397
398# ifdef PNG_FLOATING_POINT_SUPPORTED
399void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600400png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
John Bowlerd273ad22011-05-07 21:00:28 -0500401{
John Bowlerf70c7d02011-05-10 22:54:37 -0500402 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500403 output_gamma));
John Bowlerd273ad22011-05-07 21:00:28 -0500404}
405# endif
406#endif
407
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500408#ifdef PNG_READ_QUANTIZE_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500409/* Dither file to 8-bit. Supply a palette, the current number
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600410 * of elements in the palette, the maximum number of elements
411 * allowed, and a histogram if possible. If the current number
Glenn Randers-Pehrsonbbe2be32015-03-07 13:13:11 -0600412 * of colors is greater than the maximum number, the palette will be
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500413 * modified to fit in the maximum number. "full_quantize" indicates
414 * whether we need a quantizing cube set up for RGB images, or if we
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600415 * simply are reducing the number of colors in a paletted image.
416 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600417
418typedef struct png_dsort_struct
Guy Schalnat0d580581995-07-20 02:43:20 -0500419{
John Bowlerbaeb6d12011-11-26 18:21:02 -0600420 struct png_dsort_struct * next;
Guy Schalnat0d580581995-07-20 02:43:20 -0500421 png_byte left;
422 png_byte right;
Guy Schalnat6d764711995-12-19 03:22:19 -0600423} png_dsort;
John Bowlerbaeb6d12011-11-26 18:21:02 -0600424typedef png_dsort * png_dsortp;
425typedef png_dsort * * png_dsortpp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500426
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500427void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600428png_set_quantize(png_structrp png_ptr, png_colorp palette,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500429 int num_palette, int maximum_colors, png_const_uint_16p histogram,
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500430 int full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500431{
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500432 png_debug(1, "in png_set_quantize");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500433
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500434 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500435 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500436
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500437 png_ptr->transformations |= PNG_QUANTIZE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500438
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600439 if (full_quantize == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500440 {
441 int i;
442
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500443 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500444 (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte))));
Guy Schalnat0d580581995-07-20 02:43:20 -0500445 for (i = 0; i < num_palette; i++)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500446 png_ptr->quantize_index[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 }
448
449 if (num_palette > maximum_colors)
450 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 if (histogram != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500453 /* This is easy enough, just throw out the least used colors.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500454 * Perhaps not the best solution, but good enough.
455 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500456
457 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500458
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500459 /* Initialize an array to sort colors */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500460 png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500461 (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte))));
Guy Schalnat0d580581995-07-20 02:43:20 -0500462
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500463 /* Initialize the quantize_sort array */
Guy Schalnat0d580581995-07-20 02:43:20 -0500464 for (i = 0; i < num_palette; i++)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500465 png_ptr->quantize_sort[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500466
Andreas Dilger47a0c421997-05-16 02:46:07 -0500467 /* Find the least used palette entries by starting a
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500468 * bubble sort, and running it until we have sorted
469 * out enough colors. Note that we don't care about
470 * sorting all the colors, just finding which are
471 * least used.
472 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500473
474 for (i = num_palette - 1; i >= maximum_colors; i--)
475 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500476 int done; /* To stop early if the list is pre-sorted */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600477 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500478
479 done = 1;
480 for (j = 0; j < i; j++)
481 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500482 if (histogram[png_ptr->quantize_sort[j]]
483 < histogram[png_ptr->quantize_sort[j + 1]])
Guy Schalnat0d580581995-07-20 02:43:20 -0500484 {
485 png_byte t;
486
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500487 t = png_ptr->quantize_sort[j];
488 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
489 png_ptr->quantize_sort[j + 1] = t;
Guy Schalnat0d580581995-07-20 02:43:20 -0500490 done = 0;
491 }
492 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500493
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600494 if (done != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 break;
496 }
497
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500498 /* Swap the palette around, and set up a table, if necessary */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600499 if (full_quantize != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500501 int j = num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500502
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500503 /* Put all the useful colors within the max, but don't
504 * move the others.
505 */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500506 for (i = 0; i < maximum_colors; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500508 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 {
510 do
511 j--;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500512 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500513
Guy Schalnat0d580581995-07-20 02:43:20 -0500514 palette[i] = palette[j];
515 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600516 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500517 }
518 else
519 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500520 int j = num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500521
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500522 /* Move all the used colors inside the max limit, and
523 * develop a translation table.
524 */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500525 for (i = 0; i < maximum_colors; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500526 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500527 /* Only move the colors we need to */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500528 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500529 {
530 png_color tmp_color;
531
532 do
533 j--;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500534 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
Guy Schalnat0d580581995-07-20 02:43:20 -0500535
536 tmp_color = palette[j];
537 palette[j] = palette[i];
538 palette[i] = tmp_color;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500539 /* Indicate where the color went */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500540 png_ptr->quantize_index[j] = (png_byte)i;
541 png_ptr->quantize_index[i] = (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500542 }
543 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500544
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500545 /* Find closest color for those colors we are not using */
Guy Schalnat0d580581995-07-20 02:43:20 -0500546 for (i = 0; i < num_palette; i++)
547 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500548 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500550 int min_d, k, min_k, d_index;
Guy Schalnat0d580581995-07-20 02:43:20 -0500551
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500552 /* Find the closest color to one we threw out */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500553 d_index = png_ptr->quantize_index[i];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500554 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
555 for (k = 1, min_k = 0; k < maximum_colors; k++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500556 {
557 int d;
558
Andreas Dilger47a0c421997-05-16 02:46:07 -0500559 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500560
561 if (d < min_d)
562 {
563 min_d = d;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 min_k = k;
Guy Schalnat0d580581995-07-20 02:43:20 -0500565 }
566 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500567 /* Point to closest color */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500568 png_ptr->quantize_index[i] = (png_byte)min_k;
Guy Schalnat0d580581995-07-20 02:43:20 -0500569 }
570 }
571 }
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500572 png_free(png_ptr, png_ptr->quantize_sort);
573 png_ptr->quantize_sort = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500574 }
575 else
576 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500577 /* This is much harder to do simply (and quickly). Perhaps
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500578 * we need to go through a median cut routine, but those
579 * don't always behave themselves with only a few colors
580 * as input. So we will just find the closest two colors,
581 * and throw out one of them (chosen somewhat randomly).
582 * [We don't understand this at all, so if someone wants to
583 * work on improving it, be our guest - AED, GRP]
584 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500585 int i;
586 int max_d;
587 int num_new_palette;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500588 png_dsortp t;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600589 png_dsortpp hash;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500590
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500591 t = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500592
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500593 /* Initialize palette index arrays */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500594 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500595 (png_alloc_size_t)((png_uint_32)num_palette *
596 (sizeof (png_byte))));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500597 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500598 (png_alloc_size_t)((png_uint_32)num_palette *
599 (sizeof (png_byte))));
Guy Schalnat0d580581995-07-20 02:43:20 -0500600
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500601 /* Initialize the sort array */
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 for (i = 0; i < num_palette; i++)
603 {
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500604 png_ptr->index_to_palette[i] = (png_byte)i;
605 png_ptr->palette_to_index[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500606 }
607
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500608 hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 *
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600609 (sizeof (png_dsortp))));
Guy Schalnat0d580581995-07-20 02:43:20 -0500610
611 num_new_palette = num_palette;
612
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500613 /* Initial wild guess at how far apart the farthest pixel
614 * pair we will be eliminating will be. Larger
615 * numbers mean more areas will be allocated, Smaller
616 * numbers run the risk of not saving enough data, and
617 * having to do this all over again.
618 *
619 * I have not done extensive checking on this number.
620 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500621 max_d = 96;
622
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600623 while (num_new_palette > maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500624 {
625 for (i = 0; i < num_new_palette - 1; i++)
626 {
627 int j;
628
629 for (j = i + 1; j < num_new_palette; j++)
630 {
631 int d;
632
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600633 d = PNG_COLOR_DIST(palette[i], palette[j]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500634
635 if (d <= max_d)
636 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500637
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500638 t = (png_dsortp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500639 (png_alloc_size_t)(sizeof (png_dsort)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500640
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500641 if (t == NULL)
642 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500643
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 t->next = hash[d];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600645 t->left = (png_byte)i;
646 t->right = (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500647 hash[d] = t;
648 }
649 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500650 if (t == NULL)
651 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 }
653
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500654 if (t != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500655 for (i = 0; i <= max_d; i++)
656 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500657 if (hash[i] != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500658 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600659 png_dsortp p;
Guy Schalnat0d580581995-07-20 02:43:20 -0500660
661 for (p = hash[i]; p; p = p->next)
662 {
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500663 if ((int)png_ptr->index_to_palette[p->left]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600664 < num_new_palette &&
665 (int)png_ptr->index_to_palette[p->right]
666 < num_new_palette)
Guy Schalnat0d580581995-07-20 02:43:20 -0500667 {
668 int j, next_j;
669
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600670 if (num_new_palette & 0x01)
Guy Schalnat0d580581995-07-20 02:43:20 -0500671 {
672 j = p->left;
673 next_j = p->right;
674 }
675 else
676 {
677 j = p->right;
678 next_j = p->left;
679 }
680
681 num_new_palette--;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500682 palette[png_ptr->index_to_palette[j]]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600683 = palette[num_new_palette];
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600684 if (full_quantize == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500685 {
686 int k;
687
688 for (k = 0; k < num_palette; k++)
689 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500690 if (png_ptr->quantize_index[k] ==
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600691 png_ptr->index_to_palette[j])
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500692 png_ptr->quantize_index[k] =
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600693 png_ptr->index_to_palette[next_j];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500694
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500695 if ((int)png_ptr->quantize_index[k] ==
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600696 num_new_palette)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500697 png_ptr->quantize_index[k] =
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600698 png_ptr->index_to_palette[j];
Guy Schalnat0d580581995-07-20 02:43:20 -0500699 }
700 }
701
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500702 png_ptr->index_to_palette[png_ptr->palette_to_index
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600703 [num_new_palette]] = png_ptr->index_to_palette[j];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500704
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500705 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600706 = png_ptr->palette_to_index[num_new_palette];
Guy Schalnat0d580581995-07-20 02:43:20 -0500707
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600708 png_ptr->index_to_palette[j] =
709 (png_byte)num_new_palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500710
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600711 png_ptr->palette_to_index[num_new_palette] =
712 (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500713 }
714 if (num_new_palette <= maximum_colors)
715 break;
716 }
717 if (num_new_palette <= maximum_colors)
718 break;
719 }
720 }
721
722 for (i = 0; i < 769; i++)
723 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500724 if (hash[i] != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500725 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500726 png_dsortp p = hash[i];
Guy Schalnat0d580581995-07-20 02:43:20 -0500727 while (p)
728 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 t = p->next;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600730 png_free(png_ptr, p);
Guy Schalnat0d580581995-07-20 02:43:20 -0500731 p = t;
732 }
733 }
734 hash[i] = 0;
735 }
736 max_d += 96;
737 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600738 png_free(png_ptr, hash);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500739 png_free(png_ptr, png_ptr->palette_to_index);
740 png_free(png_ptr, png_ptr->index_to_palette);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500741 png_ptr->palette_to_index = NULL;
742 png_ptr->index_to_palette = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500743 }
744 num_palette = maximum_colors;
745 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500746 if (png_ptr->palette == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600747 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 png_ptr->palette = palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500749 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600750 png_ptr->num_palette = (png_uint_16)num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500751
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -0600752 if (full_quantize != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -0500753 {
754 int i;
Guy Schalnat6d764711995-12-19 03:22:19 -0600755 png_bytep distance;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500756 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
757 PNG_QUANTIZE_BLUE_BITS;
758 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
759 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
760 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -0400761 size_t num_entries = ((size_t)1 << total_bits);
Guy Schalnat0d580581995-07-20 02:43:20 -0500762
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -0500763 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500764 (png_alloc_size_t)(num_entries * (sizeof (png_byte))));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500765
Glenn Randers-Pehrson051d6cc2017-09-03 15:19:59 -0500766 distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)(num_entries *
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600767 (sizeof (png_byte))));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500768
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600769 memset(distance, 0xff, num_entries * (sizeof (png_byte)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500770
771 for (i = 0; i < num_palette; i++)
772 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500773 int ir, ig, ib;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500774 int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
775 int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
776 int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
Guy Schalnat0d580581995-07-20 02:43:20 -0500777
778 for (ir = 0; ir < num_red; ir++)
779 {
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500780 /* int dr = abs(ir - r); */
781 int dr = ((ir > r) ? ir - r : r - ir);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500782 int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
783 PNG_QUANTIZE_GREEN_BITS));
Guy Schalnat0d580581995-07-20 02:43:20 -0500784
Guy Schalnat0d580581995-07-20 02:43:20 -0500785 for (ig = 0; ig < num_green; ig++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600786 {
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500787 /* int dg = abs(ig - g); */
788 int dg = ((ig > g) ? ig - g : g - ig);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500789 int dt = dr + dg;
790 int dm = ((dr > dg) ? dr : dg);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500791 int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
Guy Schalnat0d580581995-07-20 02:43:20 -0500792
Guy Schalnat0d580581995-07-20 02:43:20 -0500793 for (ib = 0; ib < num_blue; ib++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600794 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500795 int d_index = index_g | ib;
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500796 /* int db = abs(ib - b); */
797 int db = ((ib > b) ? ib - b : b - ib);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500798 int dmax = ((dm > db) ? dm : db);
799 int d = dmax + dt + db;
Guy Schalnat0d580581995-07-20 02:43:20 -0500800
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600801 if (d < (int)distance[d_index])
Guy Schalnat0d580581995-07-20 02:43:20 -0500802 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500803 distance[d_index] = (png_byte)d;
804 png_ptr->palette_lookup[d_index] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500805 }
806 }
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500807 }
808 }
809 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500810
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600811 png_free(png_ptr, distance);
Guy Schalnat0d580581995-07-20 02:43:20 -0500812 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500813}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600814#endif /* READ_QUANTIZE */
Guy Schalnat0d580581995-07-20 02:43:20 -0500815
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500816#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500817void PNGFAPI
John Bowler5d567862011-12-24 09:12:00 -0600818png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500819 png_fixed_point file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500820{
821 png_debug(1, "in png_set_gamma_fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500822
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500823 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500824 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500825
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500826 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
John Bowlerf70c7d02011-05-10 22:54:37 -0500827 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
828 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
829
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500830 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
John Bowlerd273ad22011-05-07 21:00:28 -0500831 * premultiplied alpha support; this actually hides an undocumented feature
832 * of the previous implementation which allowed gamma processing to be
833 * disabled in background handling. There is no evidence (so far) that this
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500834 * was being used; however, png_set_background itself accepted and must still
John Bowlerd273ad22011-05-07 21:00:28 -0500835 * accept '0' for the gamma value it takes, because it isn't always used.
836 *
837 * Since this is an API change (albeit a very minor one that removes an
John Bowlerbaeb6d12011-11-26 18:21:02 -0600838 * undocumented API feature) the following checks were only enabled in
839 * libpng-1.6.0.
John Bowlerd273ad22011-05-07 21:00:28 -0500840 */
841 if (file_gamma <= 0)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500842 png_error(png_ptr, "invalid file gamma in png_set_gamma");
John Bowlerd273ad22011-05-07 21:00:28 -0500843
844 if (scrn_gamma <= 0)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500845 png_error(png_ptr, "invalid screen gamma in png_set_gamma");
John Bowlerd273ad22011-05-07 21:00:28 -0500846
847 /* Set the gamma values unconditionally - this overrides the value in the PNG
848 * file if a gAMA chunk was present. png_set_alpha_mode provides a
849 * different, easier, way to default the file gamma.
850 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600851 png_ptr->colorspace.gamma = file_gamma;
852 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500853 png_ptr->screen_gamma = scrn_gamma;
Guy Schalnat0d580581995-07-20 02:43:20 -0500854}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500855
856# ifdef PNG_FLOATING_POINT_SUPPORTED
857void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600858png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500859{
John Bowlerf70c7d02011-05-10 22:54:37 -0500860 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -0500861 convert_gamma_value(png_ptr, file_gamma));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500862}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600863# endif /* FLOATING_POINT */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500864#endif /* READ_GAMMA */
Guy Schalnat0d580581995-07-20 02:43:20 -0500865
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500866#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -0500867/* Expand paletted images to RGB, expand grayscale images of
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500868 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600869 * to alpha channels.
870 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500871void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600872png_set_expand(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500873{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500874 png_debug(1, "in png_set_expand");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500875
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500876 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500877 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500878
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600879 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Guy Schalnat0d580581995-07-20 02:43:20 -0500880}
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500881
882/* GRR 19990627: the following three functions currently are identical
883 * to png_set_expand(). However, it is entirely reasonable that someone
884 * might wish to expand an indexed image to RGB but *not* expand a single,
885 * fully transparent palette entry to a full alpha channel--perhaps instead
886 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
887 * the transparent color with a particular RGB value, or drop tRNS entirely.
888 * IOW, a future version of the library may make the transformations flag
889 * a bit more fine-grained, with separate bits for each of these three
890 * functions.
891 *
892 * More to the point, these functions make it obvious what libpng will be
893 * doing, whereas "expand" can (and does) mean any number of things.
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600894 *
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -0600895 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
896 * to expand only the sample depth but not to expand the tRNS to alpha
897 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500898 */
899
900/* Expand paletted images to RGB. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500901void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600902png_set_palette_to_rgb(png_structrp png_ptr)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500903{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500904 png_debug(1, "in png_set_palette_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500905
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500906 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500907 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500908
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600909 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500910}
911
912/* Expand grayscale images of less than 8-bit depth to 8 bits. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500913void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600914png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600915{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500916 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500917
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500918 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500919 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500920
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500921 png_ptr->transformations |= PNG_EXPAND;
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600922}
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500923
924/* Expand tRNS chunks to alpha channels. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500925void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600926png_set_tRNS_to_alpha(png_structrp png_ptr)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500927{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500928 png_debug(1, "in png_set_tRNS_to_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500929
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500930 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600931 return;
932
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600933 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500934}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -0600935#endif /* READ_EXPAND */
Guy Schalnat0d580581995-07-20 02:43:20 -0500936
John Bowler4d562962011-02-12 09:01:20 -0600937#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500938/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
John Bowler4d562962011-02-12 09:01:20 -0600939 * it may not work correctly.)
940 */
941void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600942png_set_expand_16(png_structrp png_ptr)
John Bowler4d562962011-02-12 09:01:20 -0600943{
944 png_debug(1, "in png_set_expand_16");
945
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500946 if (png_rtran_ok(png_ptr, 0) == 0)
John Bowler4d562962011-02-12 09:01:20 -0600947 return;
948
949 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
John Bowler4d562962011-02-12 09:01:20 -0600950}
951#endif
952
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500953#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500954void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -0600955png_set_gray_to_rgb(png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500956{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500957 png_debug(1, "in png_set_gray_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500958
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500959 if (png_rtran_ok(png_ptr, 0) == 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600960 return;
961
962 /* Because rgb must be 8 bits or more: */
963 png_set_expand_gray_1_2_4_to_8(png_ptr);
964 png_ptr->transformations |= PNG_GRAY_TO_RGB;
Guy Schalnat0d580581995-07-20 02:43:20 -0500965}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500966#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500967
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500968#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500969void PNGFAPI
John Bowler5d567862011-12-24 09:12:00 -0600970png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600971 png_fixed_point red, png_fixed_point green)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600972{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500973 png_debug(1, "in png_set_rgb_to_gray");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500974
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600975 /* Need the IHDR here because of the check on color_type below. */
976 /* TODO: fix this */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -0500977 if (png_rtran_ok(png_ptr, 1) == 0)
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500978 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500979
Glenn Randers-Pehrson36b246a2014-09-27 08:59:52 -0500980 switch (error_action)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600981 {
John Bowler7875d532011-11-07 22:33:49 -0600982 case PNG_ERROR_ACTION_NONE:
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600983 png_ptr->transformations |= PNG_RGB_TO_GRAY;
984 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500985
John Bowler7875d532011-11-07 22:33:49 -0600986 case PNG_ERROR_ACTION_WARN:
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600987 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
988 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500989
John Bowler7875d532011-11-07 22:33:49 -0600990 case PNG_ERROR_ACTION_ERROR:
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600991 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600992 break;
993
994 default:
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500995 png_error(png_ptr, "invalid error action to rgb_to_gray");
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600996 }
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -0600997
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600998 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500999#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001000 png_ptr->transformations |= PNG_EXPAND;
1001#else
1002 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001003 /* Make this an error in 1.6 because otherwise the application may assume
1004 * that it just worked and get a memory overwrite.
1005 */
1006 png_error(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001007 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001008
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001009 /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001010 }
1011#endif
1012 {
John Bowlercb0b2962011-05-12 21:48:29 -05001013 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001014 {
John Bowlercb0b2962011-05-12 21:48:29 -05001015 png_uint_16 red_int, green_int;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001016
John Bowler736f40f2011-08-25 16:19:44 -05001017 /* NOTE: this calculation does not round, but this behavior is retained
Glenn Randers-Pehrson6df21522014-11-12 21:41:38 -06001018 * for consistency; the inaccuracy is very small. The code here always
John Bowler736f40f2011-08-25 16:19:44 -05001019 * overwrites the coefficients, regardless of whether they have been
1020 * defaulted or set already.
1021 */
John Bowler75156122011-09-09 17:21:44 -05001022 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
1023 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
John Bowlercb0b2962011-05-12 21:48:29 -05001024
1025 png_ptr->rgb_to_gray_red_coeff = red_int;
1026 png_ptr->rgb_to_gray_green_coeff = green_int;
John Bowler736f40f2011-08-25 16:19:44 -05001027 png_ptr->rgb_to_gray_coefficients_set = 1;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001028 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001029
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001030 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001031 {
John Bowlercb0b2962011-05-12 21:48:29 -05001032 if (red >= 0 && green >= 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001033 png_app_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001034 "ignoring out of range rgb_to_gray coefficients");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001035
John Bowler736f40f2011-08-25 16:19:44 -05001036 /* Use the defaults, from the cHRM chunk if set, else the historical
1037 * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
1038 * png_do_rgb_to_gray for more discussion of the values. In this case
1039 * the coefficients are not marked as 'set' and are not overwritten if
1040 * something has already provided a default.
John Bowlercb0b2962011-05-12 21:48:29 -05001041 */
1042 if (png_ptr->rgb_to_gray_red_coeff == 0 &&
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001043 png_ptr->rgb_to_gray_green_coeff == 0)
John Bowlercb0b2962011-05-12 21:48:29 -05001044 {
John Bowler736f40f2011-08-25 16:19:44 -05001045 png_ptr->rgb_to_gray_red_coeff = 6968;
1046 png_ptr->rgb_to_gray_green_coeff = 23434;
1047 /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
John Bowlercb0b2962011-05-12 21:48:29 -05001048 }
1049 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001050 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001051}
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001052
1053#ifdef PNG_FLOATING_POINT_SUPPORTED
1054/* Convert a RGB image to a grayscale of the same width. This allows us,
1055 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1056 */
1057
1058void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001059png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001060 double green)
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001061{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001062 png_set_rgb_to_gray_fixed(png_ptr, error_action,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001063 png_fixed(png_ptr, red, "rgb to gray red coefficient"),
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001064 png_fixed(png_ptr, green, "rgb to gray green coefficient"));
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001065}
1066#endif /* FLOATING POINT */
1067
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001068#endif /* RGB_TO_GRAY */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001069
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001070#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
Glenn Randers-Pehrson33188ac2009-06-16 14:12:35 -05001071 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001072void PNGAPI
John Bowler5d567862011-12-24 09:12:00 -06001073png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001074 read_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001075{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001076 png_debug(1, "in png_set_read_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001077
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001078#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001079 png_ptr->transformations |= PNG_USER_TRANSFORM;
1080 png_ptr->read_user_transform_fn = read_user_transform_fn;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001081#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001082}
1083#endif
1084
John Bowler4a12f4a2011-04-17 18:34:22 -05001085#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001086#ifdef PNG_READ_GAMMA_SUPPORTED
1087/* In the case of gamma transformations only do transformations on images where
1088 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
1089 * slows things down slightly, and also needlessly introduces small errors.
1090 */
1091static int /* PRIVATE */
1092png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
1093{
1094 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1095 * correction as a difference of the overall transform from 1.0
1096 *
1097 * We want to compare the threshold with s*f - 1, if we get
1098 * overflow here it is because of wacky gamma values so we
1099 * turn on processing anyway.
1100 */
1101 png_fixed_point gtest;
1102 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1103 png_gamma_significant(gtest);
1104}
1105#endif
1106
Andreas Dilger47a0c421997-05-16 02:46:07 -05001107/* Initialize everything needed for the read. This includes modifying
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001108 * the palette.
1109 */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001110
Glenn Randers-Pehrson6df21522014-11-12 21:41:38 -06001111/* For the moment 'png_init_palette_transformations' and
John Bowlerd273ad22011-05-07 21:00:28 -05001112 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1113 * The intent is that these two routines should have palette or rgb operations
1114 * extracted from 'png_init_read_transformations'.
1115 */
1116static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001117png_init_palette_transformations(png_structrp png_ptr)
John Bowlerd273ad22011-05-07 21:00:28 -05001118{
1119 /* Called to handle the (input) palette case. In png_do_read_transformations
1120 * the first step is to expand the palette if requested, so this code must
1121 * take care to only make changes that are invariant with respect to the
1122 * palette expansion, or only do them if there is no expansion.
1123 *
1124 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1125 * to 0.)
1126 */
1127 int input_has_alpha = 0;
1128 int input_has_transparency = 0;
1129
1130 if (png_ptr->num_trans > 0)
1131 {
1132 int i;
1133
1134 /* Ignore if all the entries are opaque (unlikely!) */
1135 for (i=0; i<png_ptr->num_trans; ++i)
Glenn Randers-Pehrson492e6712013-08-04 14:03:44 -05001136 {
John Bowlerd273ad22011-05-07 21:00:28 -05001137 if (png_ptr->trans_alpha[i] == 255)
1138 continue;
1139 else if (png_ptr->trans_alpha[i] == 0)
1140 input_has_transparency = 1;
1141 else
Glenn Randers-Pehrson492e6712013-08-04 14:03:44 -05001142 {
1143 input_has_transparency = 1;
John Bowlerd273ad22011-05-07 21:00:28 -05001144 input_has_alpha = 1;
Glenn Randers-Pehrson492e6712013-08-04 14:03:44 -05001145 break;
1146 }
1147 }
John Bowlerd273ad22011-05-07 21:00:28 -05001148 }
1149
1150 /* If no alpha we can optimize. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001151 if (input_has_alpha == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001152 {
1153 /* Any alpha means background and associative alpha processing is
Glenn Randers-Pehrson38f49402013-12-22 15:00:59 -06001154 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
John Bowlerd273ad22011-05-07 21:00:28 -05001155 * and ENCODE_ALPHA are irrelevant.
1156 */
1157 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1158 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1159
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001160 if (input_has_transparency == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001161 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1162 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001163
Cosmin Trutaa627bd22019-04-07 19:50:12 -04001164#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
John Bowlerd273ad22011-05-07 21:00:28 -05001165 /* png_set_background handling - deals with the complexity of whether the
1166 * background color is in the file format or the screen format in the case
1167 * where an 'expand' will happen.
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001168 */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001169
John Bowlerd273ad22011-05-07 21:00:28 -05001170 /* The following code cannot be entered in the alpha pre-multiplication case
1171 * because PNG_BACKGROUND_EXPAND is cancelled below.
1172 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001173 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1174 (png_ptr->transformations & PNG_EXPAND) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05001175 {
John Bowlerd273ad22011-05-07 21:00:28 -05001176 {
1177 png_ptr->background.red =
1178 png_ptr->palette[png_ptr->background.index].red;
1179 png_ptr->background.green =
1180 png_ptr->palette[png_ptr->background.index].green;
1181 png_ptr->background.blue =
1182 png_ptr->palette[png_ptr->background.index].blue;
1183
1184#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Cosmin Truta70d122a2019-02-03 19:51:18 -05001185 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
1186 {
1187 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
1188 {
1189 /* Invert the alpha channel (in tRNS) unless the pixels are
1190 * going to be expanded, in which case leave it for later
1191 */
1192 int i, istop = png_ptr->num_trans;
John Bowlerd273ad22011-05-07 21:00:28 -05001193
Cosmin Truta70d122a2019-02-03 19:51:18 -05001194 for (i = 0; i < istop; i++)
1195 png_ptr->trans_alpha[i] =
1196 (png_byte)(255 - png_ptr->trans_alpha[i]);
1197 }
1198 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001199#endif /* READ_INVERT_ALPHA */
John Bowlerd273ad22011-05-07 21:00:28 -05001200 }
1201 } /* background expand and (therefore) no alpha association. */
Cosmin Trutaa627bd22019-04-07 19:50:12 -04001202#endif /* READ_EXPAND && READ_BACKGROUND */
John Bowlerd273ad22011-05-07 21:00:28 -05001203}
1204
1205static void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001206png_init_rgb_transformations(png_structrp png_ptr)
John Bowlerd273ad22011-05-07 21:00:28 -05001207{
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001208 /* Added to libpng-1.5.4: check the color type to determine whether there
John Bowlerd273ad22011-05-07 21:00:28 -05001209 * is any alpha or transparency in the image and simply cancel the
1210 * background and alpha mode stuff if there isn't.
1211 */
1212 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1213 int input_has_transparency = png_ptr->num_trans > 0;
1214
1215 /* If no alpha we can optimize. */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001216 if (input_has_alpha == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001217 {
1218 /* Any alpha means background and associative alpha processing is
Glenn Randers-Pehrson38f49402013-12-22 15:00:59 -06001219 * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
John Bowlerd273ad22011-05-07 21:00:28 -05001220 * and ENCODE_ALPHA are irrelevant.
1221 */
1222# ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1223 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1224 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1225# endif
1226
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001227 if (input_has_transparency == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001228 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1229 }
1230
1231#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1232 /* png_set_background handling - deals with the complexity of whether the
1233 * background color is in the file format or the screen format in the case
1234 * where an 'expand' will happen.
1235 */
1236
1237 /* The following code cannot be entered in the alpha pre-multiplication case
1238 * because PNG_BACKGROUND_EXPAND is cancelled below.
1239 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001240 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
1241 (png_ptr->transformations & PNG_EXPAND) != 0 &&
1242 (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001243 /* i.e., GRAY or GRAY_ALPHA */
1244 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001245 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001246 /* Expand background and tRNS chunks */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001247 int gray = png_ptr->background.gray;
1248 int trans_gray = png_ptr->trans_color.gray;
1249
Guy Schalnat0d580581995-07-20 02:43:20 -05001250 switch (png_ptr->bit_depth)
1251 {
1252 case 1:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001253 gray *= 0xff;
1254 trans_gray *= 0xff;
Guy Schalnat0d580581995-07-20 02:43:20 -05001255 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001256
Guy Schalnat0d580581995-07-20 02:43:20 -05001257 case 2:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001258 gray *= 0x55;
1259 trans_gray *= 0x55;
Guy Schalnat0d580581995-07-20 02:43:20 -05001260 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001261
Guy Schalnat0d580581995-07-20 02:43:20 -05001262 case 4:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001263 gray *= 0x11;
1264 trans_gray *= 0x11;
Guy Schalnate5a37791996-06-05 15:50:50 -05001265 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001266
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001267 default:
1268
Guy Schalnate5a37791996-06-05 15:50:50 -05001269 case 8:
John Bowler72d07d32017-07-11 07:50:35 -05001270 /* FALLTHROUGH */ /* (Already 8 bits) */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001271
Guy Schalnate5a37791996-06-05 15:50:50 -05001272 case 16:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001273 /* Already a full 16 bits */
Guy Schalnat0d580581995-07-20 02:43:20 -05001274 break;
1275 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001276
1277 png_ptr->background.red = png_ptr->background.green =
1278 png_ptr->background.blue = (png_uint_16)gray;
1279
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001280 if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001281 {
1282 png_ptr->trans_color.red = png_ptr->trans_color.green =
1283 png_ptr->trans_color.blue = (png_uint_16)trans_gray;
1284 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001285 }
John Bowlerd273ad22011-05-07 21:00:28 -05001286 } /* background expand and (therefore) no alpha association. */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001287#endif /* READ_EXPAND && READ_BACKGROUND */
John Bowlerd273ad22011-05-07 21:00:28 -05001288}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001289
John Bowlerd273ad22011-05-07 21:00:28 -05001290void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001291png_init_read_transformations(png_structrp png_ptr)
John Bowlerd273ad22011-05-07 21:00:28 -05001292{
1293 png_debug(1, "in png_init_read_transformations");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001294
John Bowlerd273ad22011-05-07 21:00:28 -05001295 /* This internal function is called from png_read_start_row in pngrutil.c
1296 * and it is called before the 'rowbytes' calculation is done, so the code
1297 * in here can change or update the transformations flags.
1298 *
1299 * First do updates that do not depend on the details of the PNG image data
1300 * being processed.
1301 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001302
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001303#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001304 /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
John Bowlerd273ad22011-05-07 21:00:28 -05001305 * png_set_alpha_mode and this is another source for a default file gamma so
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001306 * the test needs to be performed later - here. In addition prior to 1.5.4
John Bowlerd273ad22011-05-07 21:00:28 -05001307 * the tests were repeated for the PALETTE color type here - this is no
1308 * longer necessary (and doesn't seem to have been necessary before.)
1309 */
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001310 {
John Bowlerd273ad22011-05-07 21:00:28 -05001311 /* The following temporary indicates if overall gamma correction is
1312 * required.
1313 */
1314 int gamma_correction = 0;
1315
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001316 if (png_ptr->colorspace.gamma != 0) /* has been set */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001317 {
John Bowlerd273ad22011-05-07 21:00:28 -05001318 if (png_ptr->screen_gamma != 0) /* screen set too */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001319 gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001320 png_ptr->screen_gamma);
John Bowlerd273ad22011-05-07 21:00:28 -05001321
1322 else
1323 /* Assume the output matches the input; a long time default behavior
1324 * of libpng, although the standard has nothing to say about this.
1325 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001326 png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma);
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001327 }
John Bowlerd273ad22011-05-07 21:00:28 -05001328
1329 else if (png_ptr->screen_gamma != 0)
1330 /* The converse - assume the file matches the screen, note that this
Unknownf23b41d2017-11-03 00:52:06 -04001331 * perhaps undesirable default can (from 1.5.4) be changed by calling
John Bowlerd273ad22011-05-07 21:00:28 -05001332 * png_set_alpha_mode (even if the alpha handling mode isn't required
1333 * or isn't changed from the default.)
1334 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001335 png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma);
John Bowlerd273ad22011-05-07 21:00:28 -05001336
1337 else /* neither are set */
1338 /* Just in case the following prevents any processing - file and screen
1339 * are both assumed to be linear and there is no way to introduce a
1340 * third gamma value other than png_set_background with 'UNIQUE', and,
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001341 * prior to 1.5.4
John Bowlerd273ad22011-05-07 21:00:28 -05001342 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001343 png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1;
1344
1345 /* We have a gamma value now. */
1346 png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
John Bowlerd273ad22011-05-07 21:00:28 -05001347
1348 /* Now turn the gamma transformation on or off as appropriate. Notice
1349 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1350 * composition may independently cause gamma correction because it needs
1351 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1352 * hasn't been specified.) In any case this flag may get turned off in
1353 * the code immediately below if the transform can be handled outside the
1354 * row loop.
1355 */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001356 if (gamma_correction != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001357 png_ptr->transformations |= PNG_GAMMA;
1358
1359 else
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001360 png_ptr->transformations &= ~PNG_GAMMA;
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001361 }
John Bowlerd273ad22011-05-07 21:00:28 -05001362#endif
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001363
John Bowlerd273ad22011-05-07 21:00:28 -05001364 /* Certain transformations have the effect of preventing other
Glenn Randers-Pehrson6df21522014-11-12 21:41:38 -06001365 * transformations that happen afterward in png_do_read_transformations;
John Bowlerd273ad22011-05-07 21:00:28 -05001366 * resolve the interdependencies here. From the code of
1367 * png_do_read_transformations the order is:
1368 *
1369 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1370 * 2) PNG_STRIP_ALPHA (if no compose)
1371 * 3) PNG_RGB_TO_GRAY
1372 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1373 * 5) PNG_COMPOSE
1374 * 6) PNG_GAMMA
1375 * 7) PNG_STRIP_ALPHA (if compose)
1376 * 8) PNG_ENCODE_ALPHA
John Bowler8d261262011-06-18 13:37:11 -05001377 * 9) PNG_SCALE_16_TO_8
1378 * 10) PNG_16_TO_8
1379 * 11) PNG_QUANTIZE (converts to palette)
1380 * 12) PNG_EXPAND_16
1381 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1382 * 14) PNG_INVERT_MONO
John Bowler414d7b52014-02-06 11:32:57 -06001383 * 15) PNG_INVERT_ALPHA
1384 * 16) PNG_SHIFT
1385 * 17) PNG_PACK
1386 * 18) PNG_BGR
1387 * 19) PNG_PACKSWAP
1388 * 20) PNG_FILLER (includes PNG_ADD_ALPHA)
John Bowler8d261262011-06-18 13:37:11 -05001389 * 21) PNG_SWAP_ALPHA
1390 * 22) PNG_SWAP_BYTES
1391 * 23) PNG_USER_TRANSFORM [must be last]
John Bowlerd273ad22011-05-07 21:00:28 -05001392 */
1393#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001394 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1395 (png_ptr->transformations & PNG_COMPOSE) == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001396 {
1397 /* Stripping the alpha channel happens immediately after the 'expand'
1398 * transformations, before all other transformation, so it cancels out
1399 * the alpha handling. It has the side effect negating the effect of
1400 * PNG_EXPAND_tRNS too:
1401 */
1402 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1403 PNG_EXPAND_tRNS);
1404 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1405
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001406 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
John Bowlerd273ad22011-05-07 21:00:28 -05001407 * so transparency information would remain just so long as it wasn't
1408 * expanded. This produces unexpected API changes if the set of things
1409 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1410 * documentation - which says ask for what you want, accept what you
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001411 * get.) This makes the behavior consistent from 1.5.4:
John Bowlerd273ad22011-05-07 21:00:28 -05001412 */
1413 png_ptr->num_trans = 0;
1414 }
1415#endif /* STRIP_ALPHA supported, no COMPOSE */
1416
1417#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1418 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1419 * settings will have no effect.
1420 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001421 if (png_gamma_significant(png_ptr->screen_gamma) == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001422 {
1423 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1424 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1425 }
1426#endif
1427
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001428#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1429 /* Make sure the coefficients for the rgb to gray conversion are set
1430 * appropriately.
1431 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001432 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001433 png_colorspace_set_rgb_coefficients(png_ptr);
1434#endif
1435
1436#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1437#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
John Bowlerd273ad22011-05-07 21:00:28 -05001438 /* Detect gray background and attempt to enable optimization for
1439 * gray --> RGB case.
1440 *
1441 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1442 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1443 * background color might actually be gray yet not be flagged as such.
1444 * This is not a problem for the current code, which uses
1445 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1446 * png_do_gray_to_rgb() transformation.
1447 *
John Bowler9994f252011-05-15 18:52:39 -05001448 * TODO: this code needs to be revised to avoid the complexity and
John Bowlerd273ad22011-05-07 21:00:28 -05001449 * interdependencies. The color type of the background should be recorded in
1450 * png_set_background, along with the bit depth, then the code has a record
1451 * of exactly what color space the background is currently in.
1452 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001453 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001454 {
1455 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
John Bowler736f40f2011-08-25 16:19:44 -05001456 * the file was grayscale the background value is gray.
John Bowlerd273ad22011-05-07 21:00:28 -05001457 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001458 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001459 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1460 }
1461
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001462 else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001463 {
1464 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1465 * so the color is in the color space of the output or png_set_alpha_mode
1466 * was called and the color is black. Ignore RGB_TO_GRAY because that
1467 * happens before GRAY_TO_RGB.
1468 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001469 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001470 {
1471 if (png_ptr->background.red == png_ptr->background.green &&
1472 png_ptr->background.red == png_ptr->background.blue)
1473 {
1474 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1475 png_ptr->background.gray = png_ptr->background.red;
1476 }
1477 }
1478 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001479#endif /* READ_EXPAND && READ_BACKGROUND */
1480#endif /* READ_GRAY_TO_RGB */
John Bowlerd273ad22011-05-07 21:00:28 -05001481
1482 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1483 * can be performed directly on the palette, and some (such as rgb to gray)
1484 * can be optimized inside the palette. This is particularly true of the
1485 * composite (background and alpha) stuff, which can be pretty much all done
1486 * in the palette even if the result is expanded to RGB or gray afterward.
1487 *
1488 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1489 * earlier and the palette stuff is actually handled on the first row. This
1490 * leads to the reported bug that the palette returned by png_get_PLTE is not
1491 * updated.
1492 */
1493 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1494 png_init_palette_transformations(png_ptr);
1495
1496 else
1497 png_init_rgb_transformations(png_ptr);
1498
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001499#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1500 defined(PNG_READ_EXPAND_16_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001501 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
1502 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1503 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1504 png_ptr->bit_depth != 16)
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001505 {
1506 /* TODO: fix this. Because the expand_16 operation is after the compose
1507 * handling the background color must be 8, not 16, bits deep, but the
Glenn Randers-Pehrson0a048922011-05-18 21:44:37 -05001508 * application will supply a 16-bit value so reduce it here.
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001509 *
1510 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1511 * present, so that case is ok (until do_expand_16 is moved.)
Glenn Randers-Pehrsonbe720ed2011-06-15 08:20:37 -05001512 *
1513 * NOTE: this discards the low 16 bits of the user supplied background
1514 * color, but until expand_16 works properly there is no choice!
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001515 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001516# define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
John Bowlerb2bee332011-06-10 23:24:58 -05001517 CHOP(png_ptr->background.red);
1518 CHOP(png_ptr->background.green);
1519 CHOP(png_ptr->background.blue);
1520 CHOP(png_ptr->background.gray);
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001521# undef CHOP
1522 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001523#endif /* READ_BACKGROUND && READ_EXPAND_16 */
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001524
John Bowler18c5cfa2011-11-16 14:26:34 -06001525#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1526 (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1527 defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001528 if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
1529 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1530 (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1531 png_ptr->bit_depth == 16)
John Bowler18c5cfa2011-11-16 14:26:34 -06001532 {
1533 /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
1534 * component this will also happen after PNG_COMPOSE and so the background
1535 * color must be pre-expanded here.
1536 *
1537 * TODO: fix this too.
1538 */
1539 png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
1540 png_ptr->background.green =
1541 (png_uint_16)(png_ptr->background.green * 257);
1542 png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
1543 png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
1544 }
1545#endif
1546
John Bowlerd273ad22011-05-07 21:00:28 -05001547 /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1548 * background support (see the comments in scripts/pnglibconf.dfa), this
1549 * allows pre-multiplication of the alpha channel to be implemented as
1550 * compositing on black. This is probably sub-optimal and has been done in
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001551 * 1.5.4 betas simply to enable external critique and testing (i.e. to
John Bowlerd273ad22011-05-07 21:00:28 -05001552 * implement the new API quickly, without lots of internal changes.)
1553 */
1554
1555#ifdef PNG_READ_GAMMA_SUPPORTED
1556# ifdef PNG_READ_BACKGROUND_SUPPORTED
1557 /* Includes ALPHA_MODE */
1558 png_ptr->background_1 = png_ptr->background;
1559# endif
1560
1561 /* This needs to change - in the palette image case a whole set of tables are
1562 * built when it would be quicker to just calculate the correct value for
1563 * each palette entry directly. Also, the test is too tricky - why check
1564 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1565 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1566 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1567 * the gamma tables will not be built even if composition is required on a
1568 * gamma encoded value.
1569 *
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001570 * In 1.5.4 this is addressed below by an additional check on the individual
John Bowlerd273ad22011-05-07 21:00:28 -05001571 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1572 * tables.
1573 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001574 if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
1575 ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
1576 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1577 png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
1578 ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1579 (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1580 png_gamma_significant(png_ptr->screen_gamma) != 0
Glenn Randers-Pehrson903c64d2011-06-15 11:50:23 -05001581# ifdef PNG_READ_BACKGROUND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001582 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
1583 png_gamma_significant(png_ptr->background_gamma) != 0)
Glenn Randers-Pehrson903c64d2011-06-15 11:50:23 -05001584# endif
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001585 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
1586 png_gamma_significant(png_ptr->screen_gamma) != 0))
Guy Schalnat0d580581995-07-20 02:43:20 -05001587 {
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001588 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
Glenn Randers-Pehrsonffa89242009-12-13 08:14:40 -06001589
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001590#ifdef PNG_READ_BACKGROUND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001591 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05001592 {
John Bowler18c5cfa2011-11-16 14:26:34 -06001593 /* Issue a warning about this combination: because RGB_TO_GRAY is
1594 * optimized to do the gamma transform if present yet do_background has
1595 * to do the same thing if both options are set a
1596 * double-gamma-correction happens. This is true in all versions of
1597 * libpng to date.
1598 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001599 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
John Bowler18c5cfa2011-11-16 14:26:34 -06001600 png_warning(png_ptr,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001601 "libpng does not support gamma+background+rgb_to_gray");
John Bowler18c5cfa2011-11-16 14:26:34 -06001602
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001603 if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001604 {
John Bowlerd273ad22011-05-07 21:00:28 -05001605 /* We don't get to here unless there is a tRNS chunk with non-opaque
1606 * entries - see the checking code at the start of this function.
1607 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001608 png_color back, back_1;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001609 png_colorp palette = png_ptr->palette;
1610 int num_palette = png_ptr->num_palette;
1611 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001612 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1613 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001614
Andreas Dilger47a0c421997-05-16 02:46:07 -05001615 back.red = png_ptr->gamma_table[png_ptr->background.red];
1616 back.green = png_ptr->gamma_table[png_ptr->background.green];
1617 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
Guy Schalnate5a37791996-06-05 15:50:50 -05001618
Andreas Dilger47a0c421997-05-16 02:46:07 -05001619 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1620 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1621 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1622 }
1623 else
1624 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001625 png_fixed_point g, gs;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001626
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001627 switch (png_ptr->background_gamma_type)
Glenn Randers-Pehrson4922b1b1998-03-08 22:55:17 -06001628 {
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001629 case PNG_BACKGROUND_GAMMA_SCREEN:
1630 g = (png_ptr->screen_gamma);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001631 gs = PNG_FP_1;
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001632 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001633
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001634 case PNG_BACKGROUND_GAMMA_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001635 g = png_reciprocal(png_ptr->colorspace.gamma);
1636 gs = png_reciprocal2(png_ptr->colorspace.gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001637 png_ptr->screen_gamma);
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001638 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001639
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001640 case PNG_BACKGROUND_GAMMA_UNIQUE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001641 g = png_reciprocal(png_ptr->background_gamma);
1642 gs = png_reciprocal2(png_ptr->background_gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001643 png_ptr->screen_gamma);
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001644 break;
1645 default:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001646 g = PNG_FP_1; /* back_1 */
1647 gs = PNG_FP_1; /* back */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001648 break;
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001649 }
1650
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001651 if (png_gamma_significant(gs) != 0)
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001652 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001653 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1654 gs);
1655 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1656 gs);
1657 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1658 gs);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001659 }
John Bowlerd273ad22011-05-07 21:00:28 -05001660
1661 else
1662 {
1663 back.red = (png_byte)png_ptr->background.red;
1664 back.green = (png_byte)png_ptr->background.green;
1665 back.blue = (png_byte)png_ptr->background.blue;
1666 }
1667
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001668 if (png_gamma_significant(g) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05001669 {
1670 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001671 g);
John Bowlerd273ad22011-05-07 21:00:28 -05001672 back_1.green = png_gamma_8bit_correct(
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001673 png_ptr->background.green, g);
John Bowlerd273ad22011-05-07 21:00:28 -05001674 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001675 g);
John Bowlerd273ad22011-05-07 21:00:28 -05001676 }
1677
1678 else
1679 {
1680 back_1.red = (png_byte)png_ptr->background.red;
1681 back_1.green = (png_byte)png_ptr->background.green;
1682 back_1.blue = (png_byte)png_ptr->background.blue;
1683 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001684 }
John Bowlerd273ad22011-05-07 21:00:28 -05001685
Guy Schalnate5a37791996-06-05 15:50:50 -05001686 for (i = 0; i < num_palette; i++)
1687 {
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -05001688 if (i < (int)png_ptr->num_trans &&
1689 png_ptr->trans_alpha[i] != 0xff)
Guy Schalnate5a37791996-06-05 15:50:50 -05001690 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001691 if (png_ptr->trans_alpha[i] == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001692 {
1693 palette[i] = back;
1694 }
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001695 else /* if (png_ptr->trans_alpha[i] != 0xff) */
Guy Schalnate5a37791996-06-05 15:50:50 -05001696 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001697 png_byte v, w;
Guy Schalnate5a37791996-06-05 15:50:50 -05001698
1699 v = png_ptr->gamma_to_1[palette[i].red];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001700 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
Guy Schalnate5a37791996-06-05 15:50:50 -05001701 palette[i].red = png_ptr->gamma_from_1[w];
1702
1703 v = png_ptr->gamma_to_1[palette[i].green];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001704 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
Guy Schalnate5a37791996-06-05 15:50:50 -05001705 palette[i].green = png_ptr->gamma_from_1[w];
1706
1707 v = png_ptr->gamma_to_1[palette[i].blue];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001708 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
Guy Schalnate5a37791996-06-05 15:50:50 -05001709 palette[i].blue = png_ptr->gamma_from_1[w];
1710 }
1711 }
1712 else
1713 {
1714 palette[i].red = png_ptr->gamma_table[palette[i].red];
1715 palette[i].green = png_ptr->gamma_table[palette[i].green];
1716 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1717 }
1718 }
John Bowlerd273ad22011-05-07 21:00:28 -05001719
1720 /* Prevent the transformations being done again.
1721 *
Glenn Randers-Pehrson63071ac2011-08-29 16:16:26 -05001722 * NOTE: this is highly dubious; it removes the transformations in
John Bowlerd273ad22011-05-07 21:00:28 -05001723 * place. This seems inconsistent with the general treatment of the
1724 * transformations elsewhere.
Glenn Randers-Pehrson72cbc6e2009-09-20 07:28:43 -05001725 */
John Bowlerd273ad22011-05-07 21:00:28 -05001726 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1727 } /* color_type == PNG_COLOR_TYPE_PALETTE */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001728
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001729 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
John Bowlerd273ad22011-05-07 21:00:28 -05001730 else /* color_type != PNG_COLOR_TYPE_PALETTE */
Guy Schalnat0d580581995-07-20 02:43:20 -05001731 {
John Bowler07772cb2011-10-14 18:19:47 -05001732 int gs_sig, g_sig;
1733 png_fixed_point g = PNG_FP_1; /* Correction to linear */
1734 png_fixed_point gs = PNG_FP_1; /* Correction to screen */
Guy Schalnat0d580581995-07-20 02:43:20 -05001735
1736 switch (png_ptr->background_gamma_type)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001737 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001738 case PNG_BACKGROUND_GAMMA_SCREEN:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001739 g = png_ptr->screen_gamma;
1740 /* gs = PNG_FP_1; */
Guy Schalnat0d580581995-07-20 02:43:20 -05001741 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001742
Guy Schalnat0d580581995-07-20 02:43:20 -05001743 case PNG_BACKGROUND_GAMMA_FILE:
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001744 g = png_reciprocal(png_ptr->colorspace.gamma);
1745 gs = png_reciprocal2(png_ptr->colorspace.gamma,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05001746 png_ptr->screen_gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -05001747 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001748
Guy Schalnat0d580581995-07-20 02:43:20 -05001749 case PNG_BACKGROUND_GAMMA_UNIQUE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001750 g = png_reciprocal(png_ptr->background_gamma);
1751 gs = png_reciprocal2(png_ptr->background_gamma,
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001752 png_ptr->screen_gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -05001753 break;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001754
1755 default:
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -05001756 png_error(png_ptr, "invalid background gamma type");
Guy Schalnat0d580581995-07-20 02:43:20 -05001757 }
1758
John Bowler07772cb2011-10-14 18:19:47 -05001759 g_sig = png_gamma_significant(g);
1760 gs_sig = png_gamma_significant(gs);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001761
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001762 if (g_sig != 0)
John Bowler07772cb2011-10-14 18:19:47 -05001763 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1764 png_ptr->background.gray, g);
1765
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001766 if (gs_sig != 0)
John Bowler07772cb2011-10-14 18:19:47 -05001767 png_ptr->background.gray = png_gamma_correct(png_ptr,
1768 png_ptr->background.gray, gs);
Glenn Randers-Pehrson377657d2002-03-08 01:31:27 -06001769
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001770 if ((png_ptr->background.red != png_ptr->background.green) ||
1771 (png_ptr->background.red != png_ptr->background.blue) ||
1772 (png_ptr->background.red != png_ptr->background.gray))
Guy Schalnat0d580581995-07-20 02:43:20 -05001773 {
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001774 /* RGB or RGBA with color background */
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001775 if (g_sig != 0)
John Bowler07772cb2011-10-14 18:19:47 -05001776 {
1777 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1778 png_ptr->background.red, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001779
John Bowler07772cb2011-10-14 18:19:47 -05001780 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1781 png_ptr->background.green, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001782
John Bowler07772cb2011-10-14 18:19:47 -05001783 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1784 png_ptr->background.blue, g);
1785 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001786
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06001787 if (gs_sig != 0)
John Bowler07772cb2011-10-14 18:19:47 -05001788 {
1789 png_ptr->background.red = png_gamma_correct(png_ptr,
1790 png_ptr->background.red, gs);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001791
John Bowler07772cb2011-10-14 18:19:47 -05001792 png_ptr->background.green = png_gamma_correct(png_ptr,
1793 png_ptr->background.green, gs);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001794
John Bowler07772cb2011-10-14 18:19:47 -05001795 png_ptr->background.blue = png_gamma_correct(png_ptr,
1796 png_ptr->background.blue, gs);
1797 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001798 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001799
Guy Schalnat0d580581995-07-20 02:43:20 -05001800 else
1801 {
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001802 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1803 png_ptr->background_1.red = png_ptr->background_1.green
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001804 = png_ptr->background_1.blue = png_ptr->background_1.gray;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001805
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001806 png_ptr->background.red = png_ptr->background.green
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001807 = png_ptr->background.blue = png_ptr->background.gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05001808 }
John Bowler07772cb2011-10-14 18:19:47 -05001809
1810 /* The background is now in screen gamma: */
1811 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
John Bowlerd273ad22011-05-07 21:00:28 -05001812 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1813 }/* png_ptr->transformations & PNG_BACKGROUND */
1814
Guy Schalnate5a37791996-06-05 15:50:50 -05001815 else
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001816 /* Transformation does not include PNG_BACKGROUND */
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001817#endif /* READ_BACKGROUND */
John Bowler18c5cfa2011-11-16 14:26:34 -06001818 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
1819#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1820 /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1821 && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1822 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1823#endif
1824 )
Guy Schalnate5a37791996-06-05 15:50:50 -05001825 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001826 png_colorp palette = png_ptr->palette;
1827 int num_palette = png_ptr->num_palette;
1828 int i;
Guy Schalnate5a37791996-06-05 15:50:50 -05001829
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001830 /* NOTE: there are other transformations that should probably be in
1831 * here too.
John Bowlerd273ad22011-05-07 21:00:28 -05001832 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001833 for (i = 0; i < num_palette; i++)
1834 {
1835 palette[i].red = png_ptr->gamma_table[palette[i].red];
1836 palette[i].green = png_ptr->gamma_table[palette[i].green];
1837 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1838 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001839
Glenn Randers-Pehrson72cbc6e2009-09-20 07:28:43 -05001840 /* Done the gamma correction. */
1841 png_ptr->transformations &= ~PNG_GAMMA;
John Bowlerd273ad22011-05-07 21:00:28 -05001842 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
Guy Schalnate5a37791996-06-05 15:50:50 -05001843 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001844#ifdef PNG_READ_BACKGROUND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001845 else
1846#endif
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001847#endif /* READ_GAMMA */
John Bowlerd273ad22011-05-07 21:00:28 -05001848
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001849#ifdef PNG_READ_BACKGROUND_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001850 /* No GAMMA transformation (see the hanging else 4 lines above) */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001851 if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
John Bowlerd273ad22011-05-07 21:00:28 -05001852 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
Guy Schalnate5a37791996-06-05 15:50:50 -05001853 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001854 int i;
1855 int istop = (int)png_ptr->num_trans;
Guy Schalnate5a37791996-06-05 15:50:50 -05001856 png_color back;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001857 png_colorp palette = png_ptr->palette;
Guy Schalnate5a37791996-06-05 15:50:50 -05001858
Guy Schalnate5a37791996-06-05 15:50:50 -05001859 back.red = (png_byte)png_ptr->background.red;
1860 back.green = (png_byte)png_ptr->background.green;
1861 back.blue = (png_byte)png_ptr->background.blue;
1862
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001863 for (i = 0; i < istop; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001864 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001865 if (png_ptr->trans_alpha[i] == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001866 {
1867 palette[i] = back;
1868 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001869
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001870 else if (png_ptr->trans_alpha[i] != 0xff)
Guy Schalnate5a37791996-06-05 15:50:50 -05001871 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001872 /* The png_composite() macro is defined in png.h */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001873 png_composite(palette[i].red, palette[i].red,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001874 png_ptr->trans_alpha[i], back.red);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001875
Andreas Dilger47a0c421997-05-16 02:46:07 -05001876 png_composite(palette[i].green, palette[i].green,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001877 png_ptr->trans_alpha[i], back.green);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001878
Andreas Dilger47a0c421997-05-16 02:46:07 -05001879 png_composite(palette[i].blue, palette[i].blue,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001880 png_ptr->trans_alpha[i], back.blue);
Guy Schalnate5a37791996-06-05 15:50:50 -05001881 }
1882 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001883
John Bowlerd273ad22011-05-07 21:00:28 -05001884 png_ptr->transformations &= ~PNG_COMPOSE;
Guy Schalnat0d580581995-07-20 02:43:20 -05001885 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06001886#endif /* READ_BACKGROUND */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001887
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001888#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001889 if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
1890 (png_ptr->transformations & PNG_EXPAND) == 0 &&
John Bowlerd273ad22011-05-07 21:00:28 -05001891 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001892 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001893 int i;
1894 int istop = png_ptr->num_palette;
1895 int shift = 8 - png_ptr->sig_bit.red;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001896
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001897 png_ptr->transformations &= ~PNG_SHIFT;
1898
Unknownf23b41d2017-11-03 00:52:06 -04001899 /* significant bits can be in the range 1 to 7 for a meaningful result, if
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001900 * the number of significant bits is 0 then no shift is done (this is an
1901 * error condition which is silently ignored.)
1902 */
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001903 if (shift > 0 && shift < 8)
1904 for (i=0; i<istop; ++i)
1905 {
1906 int component = png_ptr->palette[i].red;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001907
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001908 component >>= shift;
1909 png_ptr->palette[i].red = (png_byte)component;
1910 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001911
1912 shift = 8 - png_ptr->sig_bit.green;
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001913 if (shift > 0 && shift < 8)
1914 for (i=0; i<istop; ++i)
1915 {
1916 int component = png_ptr->palette[i].green;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001917
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001918 component >>= shift;
1919 png_ptr->palette[i].green = (png_byte)component;
1920 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001921
1922 shift = 8 - png_ptr->sig_bit.blue;
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001923 if (shift > 0 && shift < 8)
1924 for (i=0; i<istop; ++i)
1925 {
1926 int component = png_ptr->palette[i].blue;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001927
Glenn Randers-Pehrson32440202013-08-21 18:01:32 -05001928 component >>= shift;
1929 png_ptr->palette[i].blue = (png_byte)component;
1930 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001931 }
Glenn Randers-Pehrson1a3fa1e2016-03-09 07:40:32 -06001932#endif /* READ_SHIFT */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001933}
1934
Andreas Dilger47a0c421997-05-16 02:46:07 -05001935/* Modify the info structure to reflect the transformations. The
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001936 * info should be updated so a PNG file could be written with it,
1937 * assuming the transformations result in valid PNG data.
1938 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001939void /* PRIVATE */
John Bowler5d567862011-12-24 09:12:00 -06001940png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001941{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001942 png_debug(1, "in png_read_transform_info");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001943
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001944#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001945 if ((png_ptr->transformations & PNG_EXPAND) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001946 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001947 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1948 {
John Bowler9994f252011-05-15 18:52:39 -05001949 /* This check must match what actually happens in
Glenn Randers-Pehrson0e128df2011-05-15 19:09:24 -05001950 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
John Bowler9994f252011-05-15 18:52:39 -05001951 * it is all opaque we must do the same (at present it does not.)
1952 */
1953 if (png_ptr->num_trans > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001954 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001955
Andreas Dilger47a0c421997-05-16 02:46:07 -05001956 else
1957 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001958
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001959 info_ptr->bit_depth = 8;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001960 info_ptr->num_trans = 0;
Glenn Randers-Pehrson67f101e2013-12-14 12:36:28 -06001961
1962 if (png_ptr->palette == NULL)
1963 png_error (png_ptr, "Palette is NULL in indexed image");
Andreas Dilger47a0c421997-05-16 02:46:07 -05001964 }
1965 else
1966 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001967 if (png_ptr->num_trans != 0)
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06001968 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001969 if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001970 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06001971 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001972 if (info_ptr->bit_depth < 8)
1973 info_ptr->bit_depth = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001974
Andreas Dilger47a0c421997-05-16 02:46:07 -05001975 info_ptr->num_trans = 0;
1976 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001977 }
1978#endif
1979
John Bowlerd273ad22011-05-07 21:00:28 -05001980#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1981 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1982 /* The following is almost certainly wrong unless the background value is in
1983 * the screen space!
1984 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05001985 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001986 info_ptr->background = png_ptr->background;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001987#endif
1988
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001989#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001990 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
John Bowlerd273ad22011-05-07 21:00:28 -05001991 * however it seems that the code in png_init_read_transformations, which has
1992 * been called before this from png_read_update_info->png_read_start_row
1993 * sometimes does the gamma transform and cancels the flag.
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001994 *
1995 * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1996 * the screen_gamma value. The following probably results in weirdness if
1997 * the info_ptr is used by the app after the rows have been read.
John Bowlerd273ad22011-05-07 21:00:28 -05001998 */
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06001999 info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06002000#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002001
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002002 if (info_ptr->bit_depth == 16)
2003 {
John Bowler8d261262011-06-18 13:37:11 -05002004# ifdef PNG_READ_16BIT_SUPPORTED
2005# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002006 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
John Bowler8d261262011-06-18 13:37:11 -05002007 info_ptr->bit_depth = 8;
2008# endif
2009
2010# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002011 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
John Bowler8d261262011-06-18 13:37:11 -05002012 info_ptr->bit_depth = 8;
2013# endif
2014
2015# else
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05002016 /* No 16-bit support: force chopping 16-bit input down to 8, in this case
John Bowler8d261262011-06-18 13:37:11 -05002017 * the app program can chose if both APIs are available by setting the
2018 * correct scaling to use.
2019 */
2020# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2021 /* For compatibility with previous versions use the strip method by
2022 * default. This code works because if PNG_SCALE_16_TO_8 is already
2023 * set the code below will do that in preference to the chop.
2024 */
2025 png_ptr->transformations |= PNG_16_TO_8;
2026 info_ptr->bit_depth = 8;
2027# else
2028
Glenn Randers-Pehrson0b26ac52011-11-28 10:38:41 -06002029# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05002030 png_ptr->transformations |= PNG_SCALE_16_TO_8;
2031 info_ptr->bit_depth = 8;
2032# else
2033
2034 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
2035# endif
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05002036# endif
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06002037#endif /* !READ_16BIT */
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002038 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002039
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002040#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002041 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002042 info_ptr->color_type = (png_byte)(info_ptr->color_type |
2043 PNG_COLOR_MASK_COLOR);
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06002044#endif
2045
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002046#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002047 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002048 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2049 ~PNG_COLOR_MASK_COLOR);
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06002050#endif
2051
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05002052#ifdef PNG_READ_QUANTIZE_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002053 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002054 {
2055 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002056 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002057 png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002058 {
2059 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2060 }
2061 }
2062#endif
2063
John Bowler1921e6d2011-05-16 20:57:54 -05002064#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002065 if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
2066 info_ptr->bit_depth == 8 &&
2067 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
John Bowler1921e6d2011-05-16 20:57:54 -05002068 {
2069 info_ptr->bit_depth = 16;
2070 }
2071#endif
2072
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002073#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002074 if ((png_ptr->transformations & PNG_PACK) != 0 &&
2075 (info_ptr->bit_depth < 8))
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002076 info_ptr->bit_depth = 8;
2077#endif
2078
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002079 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002080 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002081
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002082 else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002083 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002084
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002085 else
2086 info_ptr->channels = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002087
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002088#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002089 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05002090 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002091 info_ptr->color_type = (png_byte)(info_ptr->color_type &
2092 ~PNG_COLOR_MASK_ALPHA);
John Bowlerd273ad22011-05-07 21:00:28 -05002093 info_ptr->num_trans = 0;
2094 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002095#endif
2096
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002097 if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002098 info_ptr->channels++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002099
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002100#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002101 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002102 if ((png_ptr->transformations & PNG_FILLER) != 0 &&
2103 (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2104 info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002105 {
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05002106 info_ptr->channels++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002107 /* If adding a true alpha channel not just filler */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002108 if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002109 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06002110 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002111#endif
2112
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002113#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2114defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002115 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002116 {
John Bowler896c3cf2015-10-29 18:16:48 -07002117 if (png_ptr->user_transform_depth != 0)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002118 info_ptr->bit_depth = png_ptr->user_transform_depth;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002119
John Bowler896c3cf2015-10-29 18:16:48 -07002120 if (png_ptr->user_transform_channels != 0)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002121 info_ptr->channels = png_ptr->user_transform_channels;
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002122 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002123#endif
2124
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002125 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2126 info_ptr->bit_depth);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002127
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002128 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05002129
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002130 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
John Bowler9994f252011-05-15 18:52:39 -05002131 * check in png_rowbytes that the user buffer won't get overwritten. Note
2132 * that the field is not always set - if png_read_update_info isn't called
2133 * the application has to either not do any transforms or get the calculation
2134 * right itself.
2135 */
2136 png_ptr->info_rowbytes = info_ptr->rowbytes;
2137
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002138#ifndef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002139 if (png_ptr != NULL)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002140 return;
2141#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002142}
2143
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002144#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002145/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2146 * without changing the actual values. Thus, if you had a row with
2147 * a bit depth of 1, you would end up with bytes that only contained
2148 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2149 * png_do_shift() after this.
2150 */
John Bowler8f1150e2013-12-19 15:33:49 -06002151static void
Guy Schalnat6d764711995-12-19 03:22:19 -06002152png_do_unpack(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05002153{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002154 png_debug(1, "in png_do_unpack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002155
Andreas Dilger47a0c421997-05-16 02:46:07 -05002156 if (row_info->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002157 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002158 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002159 png_uint_32 row_width=row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002160
Guy Schalnat0d580581995-07-20 02:43:20 -05002161 switch (row_info->bit_depth)
2162 {
2163 case 1:
2164 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002165 png_bytep sp = row + (size_t)((row_width - 1) >> 3);
2166 png_bytep dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05002167 png_uint_32 shift = 7U - ((row_width + 7U) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002168 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002169 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002170 *dp = (png_byte)((*sp >> shift) & 0x01);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002171
Guy Schalnat0d580581995-07-20 02:43:20 -05002172 if (shift == 7)
2173 {
2174 shift = 0;
2175 sp--;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002176 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002177
Guy Schalnat0d580581995-07-20 02:43:20 -05002178 else
2179 shift++;
2180
2181 dp--;
2182 }
2183 break;
2184 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002185
Guy Schalnat0d580581995-07-20 02:43:20 -05002186 case 2:
2187 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002188
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002189 png_bytep sp = row + (size_t)((row_width - 1) >> 2);
2190 png_bytep dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05002191 png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002192 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002193 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002194 *dp = (png_byte)((*sp >> shift) & 0x03);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002195
Guy Schalnat0d580581995-07-20 02:43:20 -05002196 if (shift == 6)
2197 {
2198 shift = 0;
2199 sp--;
2200 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002201
Guy Schalnat0d580581995-07-20 02:43:20 -05002202 else
2203 shift += 2;
2204
2205 dp--;
2206 }
2207 break;
2208 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002209
Guy Schalnat0d580581995-07-20 02:43:20 -05002210 case 4:
2211 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002212 png_bytep sp = row + (size_t)((row_width - 1) >> 1);
2213 png_bytep dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson761d8332016-10-02 18:46:35 -05002214 png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002215 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002216 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002217 *dp = (png_byte)((*sp >> shift) & 0x0f);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002218
Guy Schalnat0d580581995-07-20 02:43:20 -05002219 if (shift == 4)
2220 {
2221 shift = 0;
2222 sp--;
2223 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002224
Guy Schalnat0d580581995-07-20 02:43:20 -05002225 else
2226 shift = 4;
2227
2228 dp--;
2229 }
2230 break;
2231 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002232
2233 default:
2234 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002235 }
2236 row_info->bit_depth = 8;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002237 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002238 row_info->rowbytes = row_width * row_info->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -05002239 }
2240}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002241#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002242
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002243#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002244/* Reverse the effects of png_do_shift. This routine merely shifts the
2245 * pixels back to their significant bits values. Thus, if you have
2246 * a row of bit depth 8, but only 5 are significant, this will shift
2247 * the values back to 0 through 31.
2248 */
John Bowler8f1150e2013-12-19 15:33:49 -06002249static void
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05002250png_do_unshift(png_row_infop row_info, png_bytep row,
2251 png_const_color_8p sig_bits)
Guy Schalnat0d580581995-07-20 02:43:20 -05002252{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002253 int color_type;
2254
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002255 png_debug(1, "in png_do_unshift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002256
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002257 /* The palette case has already been handled in the _init routine. */
2258 color_type = row_info->color_type;
2259
2260 if (color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002261 {
2262 int shift[4];
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002263 int channels = 0;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002264 int bit_depth = row_info->bit_depth;
Guy Schalnat0d580581995-07-20 02:43:20 -05002265
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002266 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05002267 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002268 shift[channels++] = bit_depth - sig_bits->red;
2269 shift[channels++] = bit_depth - sig_bits->green;
2270 shift[channels++] = bit_depth - sig_bits->blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05002271 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002272
Guy Schalnat0d580581995-07-20 02:43:20 -05002273 else
2274 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002275 shift[channels++] = bit_depth - sig_bits->gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05002276 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002277
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002278 if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05002279 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002280 shift[channels++] = bit_depth - sig_bits->alpha;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002281 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002282
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002283 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002284 int c, have_shift;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002285
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002286 for (c = have_shift = 0; c < channels; ++c)
2287 {
2288 /* A shift of more than the bit depth is an error condition but it
2289 * gets ignored here.
2290 */
2291 if (shift[c] <= 0 || shift[c] >= bit_depth)
2292 shift[c] = 0;
2293
2294 else
2295 have_shift = 1;
2296 }
2297
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06002298 if (have_shift == 0)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002299 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002300 }
Guy Schalnat0f716451995-11-28 11:22:13 -06002301
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002302 switch (bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -05002303 {
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002304 default:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002305 /* Must be 1bpp gray: should not be here! */
2306 /* NOTREACHED */
Glenn Randers-Pehrsonc36bb792011-02-12 09:49:07 -06002307 break;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002308
Guy Schalnat0d580581995-07-20 02:43:20 -05002309 case 2:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002310 /* Must be 2bpp gray */
2311 /* assert(channels == 1 && shift[0] == 1) */
Guy Schalnat0d580581995-07-20 02:43:20 -05002312 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002313 png_bytep bp = row;
2314 png_bytep bp_end = bp + row_info->rowbytes;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002315
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002316 while (bp < bp_end)
Guy Schalnat0d580581995-07-20 02:43:20 -05002317 {
John Bowler92ef3132011-10-27 19:36:08 -05002318 int b = (*bp >> 1) & 0x55;
2319 *bp++ = (png_byte)b;
Guy Schalnat0d580581995-07-20 02:43:20 -05002320 }
2321 break;
2322 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002323
Guy Schalnat0d580581995-07-20 02:43:20 -05002324 case 4:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002325 /* Must be 4bpp gray */
2326 /* assert(channels == 1) */
Guy Schalnat0d580581995-07-20 02:43:20 -05002327 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002328 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002329 png_bytep bp_end = bp + row_info->rowbytes;
2330 int gray_shift = shift[0];
2331 int mask = 0xf >> gray_shift;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002332
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002333 mask |= mask << 4;
2334
2335 while (bp < bp_end)
Guy Schalnat0d580581995-07-20 02:43:20 -05002336 {
John Bowler92ef3132011-10-27 19:36:08 -05002337 int b = (*bp >> gray_shift) & mask;
2338 *bp++ = (png_byte)b;
Guy Schalnat0d580581995-07-20 02:43:20 -05002339 }
2340 break;
2341 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002342
Guy Schalnat0d580581995-07-20 02:43:20 -05002343 case 8:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002344 /* Single byte components, G, GA, RGB, RGBA */
Guy Schalnat0d580581995-07-20 02:43:20 -05002345 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002346 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002347 png_bytep bp_end = bp + row_info->rowbytes;
2348 int channel = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002349
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002350 while (bp < bp_end)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002351 {
John Bowler92ef3132011-10-27 19:36:08 -05002352 int b = *bp >> shift[channel];
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002353 if (++channel >= channels)
2354 channel = 0;
John Bowler92ef3132011-10-27 19:36:08 -05002355 *bp++ = (png_byte)b;
Guy Schalnat0d580581995-07-20 02:43:20 -05002356 }
2357 break;
2358 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002359
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002360#ifdef PNG_READ_16BIT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002361 case 16:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002362 /* Double byte components, G, GA, RGB, RGBA */
Guy Schalnat0d580581995-07-20 02:43:20 -05002363 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002364 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002365 png_bytep bp_end = bp + row_info->rowbytes;
2366 int channel = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002367
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002368 while (bp < bp_end)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002369 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002370 int value = (bp[0] << 8) + bp[1];
2371
2372 value >>= shift[channel];
2373 if (++channel >= channels)
2374 channel = 0;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002375 *bp++ = (png_byte)(value >> 8);
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05002376 *bp++ = (png_byte)value;
Guy Schalnat0d580581995-07-20 02:43:20 -05002377 }
2378 break;
2379 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002380#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002381 }
2382 }
2383}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002384#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002385
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002386#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002387/* Scale rows of bit depth 16 down to 8 accurately */
John Bowler8f1150e2013-12-19 15:33:49 -06002388static void
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002389png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05002390{
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002391 png_debug(1, "in png_do_scale_16_to_8");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002392
Andreas Dilger47a0c421997-05-16 02:46:07 -05002393 if (row_info->bit_depth == 16)
Guy Schalnat0d580581995-07-20 02:43:20 -05002394 {
John Bowlerb2bee332011-06-10 23:24:58 -05002395 png_bytep sp = row; /* source */
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06002396 png_bytep dp = row; /* destination */
John Bowlerb2bee332011-06-10 23:24:58 -05002397 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002398
John Bowlerb2bee332011-06-10 23:24:58 -05002399 while (sp < ep)
Guy Schalnat0d580581995-07-20 02:43:20 -05002400 {
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05002401 /* The input is an array of 16-bit components, these must be scaled to
2402 * 8 bits each. For a 16-bit value V the required value (from the PNG
John Bowlerb2bee332011-06-10 23:24:58 -05002403 * specification) is:
2404 *
2405 * (V * 255) / 65535
2406 *
2407 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2408 *
2409 * Represent V as the two byte value vhi.vlo. Make a guess that the
2410 * result is the top byte of V, vhi, then the correction to this value
2411 * is:
2412 *
2413 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2414 * = floor(((vlo-vhi) + 128.5) / 257)
2415 *
2416 * This can be approximated using integer arithmetic (and a signed
2417 * shift):
2418 *
2419 * error = (vlo-vhi+128) >> 8;
2420 *
2421 * The approximate differs from the exact answer only when (vlo-vhi) is
2422 * 128; it then gives a correction of +1 when the exact correction is
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05002423 * 0. This gives 128 errors. The exact answer (correct for all 16-bit
John Bowlerb2bee332011-06-10 23:24:58 -05002424 * input values) is:
2425 *
2426 * error = (vlo-vhi+128)*65535 >> 24;
2427 *
2428 * An alternative arithmetic calculation which also gives no errors is:
2429 *
2430 * (V * 255 + 32895) >> 16
2431 */
Glenn Randers-Pehrson141d9e32011-06-13 11:47:00 -05002432
John Bowlerb2bee332011-06-10 23:24:58 -05002433 png_int_32 tmp = *sp++; /* must be signed! */
2434 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
John Bowler74945f22011-06-13 20:50:42 -05002435 *dp++ = (png_byte)tmp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002436 }
John Bowlerb2bee332011-06-10 23:24:58 -05002437
Guy Schalnat0d580581995-07-20 02:43:20 -05002438 row_info->bit_depth = 8;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002439 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002440 row_info->rowbytes = row_info->width * row_info->channels;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002441 }
2442}
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002443#endif
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002444
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002445#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -06002446static void
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002447/* Simply discard the low byte. This was the default behavior prior
2448 * to libpng-1.5.4.
2449 */
2450png_do_chop(png_row_infop row_info, png_bytep row)
2451{
2452 png_debug(1, "in png_do_chop");
2453
2454 if (row_info->bit_depth == 16)
2455 {
2456 png_bytep sp = row; /* source */
Glenn Randers-Pehrsonefc4b692011-11-07 23:31:34 -06002457 png_bytep dp = row; /* destination */
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002458 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2459
2460 while (sp < ep)
2461 {
John Bowler8d261262011-06-18 13:37:11 -05002462 *dp++ = *sp;
2463 sp += 2; /* skip low byte */
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002464 }
2465
2466 row_info->bit_depth = 8;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002467 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002468 row_info->rowbytes = row_info->width * row_info->channels;
2469 }
2470}
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002471#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05002472
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002473#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -06002474static void
Andreas Dilger47a0c421997-05-16 02:46:07 -05002475png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2476{
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002477 png_uint_32 row_width = row_info->width;
2478
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002479 png_debug(1, "in png_do_read_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002480
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002481 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002482 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002483 /* This converts from RGBA to ARGB */
2484 if (row_info->bit_depth == 8)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002485 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002486 png_bytep sp = row + row_info->rowbytes;
2487 png_bytep dp = sp;
2488 png_byte save;
2489 png_uint_32 i;
2490
2491 for (i = 0; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002492 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002493 save = *(--sp);
2494 *(--dp) = *(--sp);
2495 *(--dp) = *(--sp);
2496 *(--dp) = *(--sp);
2497 *(--dp) = save;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002498 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002499 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002500
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002501#ifdef PNG_READ_16BIT_SUPPORTED
2502 /* This converts from RRGGBBAA to AARRGGBB */
2503 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002504 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002505 png_bytep sp = row + row_info->rowbytes;
2506 png_bytep dp = sp;
2507 png_byte save[2];
2508 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002509
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002510 for (i = 0; i < row_width; i++)
2511 {
2512 save[0] = *(--sp);
2513 save[1] = *(--sp);
2514 *(--dp) = *(--sp);
2515 *(--dp) = *(--sp);
2516 *(--dp) = *(--sp);
2517 *(--dp) = *(--sp);
2518 *(--dp) = *(--sp);
2519 *(--dp) = *(--sp);
2520 *(--dp) = save[0];
2521 *(--dp) = save[1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002522 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002523 }
2524#endif
2525 }
2526
2527 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2528 {
2529 /* This converts from GA to AG */
2530 if (row_info->bit_depth == 8)
2531 {
2532 png_bytep sp = row + row_info->rowbytes;
2533 png_bytep dp = sp;
2534 png_byte save;
2535 png_uint_32 i;
2536
2537 for (i = 0; i < row_width; i++)
2538 {
2539 save = *(--sp);
2540 *(--dp) = *(--sp);
2541 *(--dp) = save;
2542 }
2543 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002544
2545#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002546 /* This converts from GGAA to AAGG */
2547 else
2548 {
2549 png_bytep sp = row + row_info->rowbytes;
2550 png_bytep dp = sp;
2551 png_byte save[2];
2552 png_uint_32 i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002553
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002554 for (i = 0; i < row_width; i++)
2555 {
2556 save[0] = *(--sp);
2557 save[1] = *(--sp);
2558 *(--dp) = *(--sp);
2559 *(--dp) = *(--sp);
2560 *(--dp) = save[0];
2561 *(--dp) = save[1];
Andreas Dilger47a0c421997-05-16 02:46:07 -05002562 }
2563 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05002564#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002565 }
2566}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002567#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002568
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002569#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -06002570static void
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002571png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2572{
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002573 png_uint_32 row_width;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002574 png_debug(1, "in png_do_read_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002575
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002576 row_width = row_info->width;
2577 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002578 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002579 if (row_info->bit_depth == 8)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002580 {
2581 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002582 png_bytep sp = row + row_info->rowbytes;
2583 png_bytep dp = sp;
2584 png_uint_32 i;
2585
2586 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002587 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002588 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002589
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002590/* This does nothing:
2591 *(--dp) = *(--sp);
2592 *(--dp) = *(--sp);
2593 *(--dp) = *(--sp);
2594 We can replace it with:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002595*/
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002596 sp-=3;
2597 dp=sp;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002598 }
2599 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002600
2601#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002602 /* This inverts the alpha channel in RRGGBBAA */
2603 else
2604 {
2605 png_bytep sp = row + row_info->rowbytes;
2606 png_bytep dp = sp;
2607 png_uint_32 i;
2608
2609 for (i = 0; i < row_width; i++)
2610 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002611 *(--dp) = (png_byte)(255 - *(--sp));
2612 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002613
2614/* This does nothing:
2615 *(--dp) = *(--sp);
2616 *(--dp) = *(--sp);
2617 *(--dp) = *(--sp);
2618 *(--dp) = *(--sp);
2619 *(--dp) = *(--sp);
2620 *(--dp) = *(--sp);
2621 We can replace it with:
2622*/
2623 sp-=6;
2624 dp=sp;
2625 }
2626 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002627#endif
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002628 }
2629 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2630 {
2631 if (row_info->bit_depth == 8)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002632 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002633 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002634 png_bytep sp = row + row_info->rowbytes;
2635 png_bytep dp = sp;
2636 png_uint_32 i;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002637
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002638 for (i = 0; i < row_width; i++)
2639 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002640 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002641 *(--dp) = *(--sp);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002642 }
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002643 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002644
2645#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002646 else
2647 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002648 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002649 png_bytep sp = row + row_info->rowbytes;
2650 png_bytep dp = sp;
2651 png_uint_32 i;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002652
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002653 for (i = 0; i < row_width; i++)
2654 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002655 *(--dp) = (png_byte)(255 - *(--sp));
2656 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002657/*
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002658 *(--dp) = *(--sp);
2659 *(--dp) = *(--sp);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002660*/
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002661 sp-=2;
2662 dp=sp;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002663 }
2664 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002665#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002666 }
2667}
2668#endif
2669
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002670#ifdef PNG_READ_FILLER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002671/* Add filler channel if we have RGB color */
John Bowler8f1150e2013-12-19 15:33:49 -06002672static void
Guy Schalnat6d764711995-12-19 03:22:19 -06002673png_do_read_filler(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002674 png_uint_32 filler, png_uint_32 flags)
Guy Schalnat0d580581995-07-20 02:43:20 -05002675{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002676 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002677 png_uint_32 row_width = row_info->width;
2678
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002679#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05002680 png_byte hi_filler = (png_byte)(filler>>8);
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002681#endif
Glenn Randers-Pehrson01a0e802015-09-24 22:39:53 -05002682 png_byte lo_filler = (png_byte)filler;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002683
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002684 png_debug(1, "in png_do_read_filler");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002685
Andreas Dilger47a0c421997-05-16 02:46:07 -05002686 if (
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002687 row_info->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05002688 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002689 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002690 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002691 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002692 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002693 /* This changes the data from G to GX */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002694 png_bytep sp = row + (size_t)row_width;
2695 png_bytep dp = sp + (size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002696 for (i = 1; i < row_width; i++)
2697 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002698 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002699 *(--dp) = *(--sp);
2700 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002701 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002702 row_info->channels = 2;
2703 row_info->pixel_depth = 16;
2704 row_info->rowbytes = row_width * 2;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002705 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002706
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002707 else
2708 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002709 /* This changes the data from G to XG */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002710 png_bytep sp = row + (size_t)row_width;
2711 png_bytep dp = sp + (size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002712 for (i = 0; i < row_width; i++)
2713 {
2714 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002715 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002716 }
2717 row_info->channels = 2;
2718 row_info->pixel_depth = 16;
2719 row_info->rowbytes = row_width * 2;
2720 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002721 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002722
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002723#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002724 else if (row_info->bit_depth == 16)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002725 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002726 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002727 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002728 /* This changes the data from GG to GGXX */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002729 png_bytep sp = row + (size_t)row_width * 2;
2730 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002731 for (i = 1; i < row_width; i++)
2732 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002733 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002734 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002735 *(--dp) = *(--sp);
2736 *(--dp) = *(--sp);
2737 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002738 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002739 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002740 row_info->channels = 2;
2741 row_info->pixel_depth = 32;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002742 row_info->rowbytes = row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002743 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002744
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002745 else
2746 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002747 /* This changes the data from GG to XXGG */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002748 png_bytep sp = row + (size_t)row_width * 2;
2749 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002750 for (i = 0; i < row_width; i++)
2751 {
2752 *(--dp) = *(--sp);
2753 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002754 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002755 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002756 }
2757 row_info->channels = 2;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002758 row_info->pixel_depth = 32;
2759 row_info->rowbytes = row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002760 }
2761 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002762#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002763 } /* COLOR_TYPE == GRAY */
2764 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2765 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002766 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002767 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002768 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002769 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002770 /* This changes the data from RGB to RGBX */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002771 png_bytep sp = row + (size_t)row_width * 3;
2772 png_bytep dp = sp + (size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002773 for (i = 1; i < row_width; i++)
2774 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002775 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002776 *(--dp) = *(--sp);
2777 *(--dp) = *(--sp);
2778 *(--dp) = *(--sp);
2779 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002780 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002781 row_info->channels = 4;
2782 row_info->pixel_depth = 32;
2783 row_info->rowbytes = row_width * 4;
2784 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002785
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002786 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002787 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002788 /* This changes the data from RGB to XRGB */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002789 png_bytep sp = row + (size_t)row_width * 3;
2790 png_bytep dp = sp + (size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002791 for (i = 0; i < row_width; i++)
2792 {
2793 *(--dp) = *(--sp);
2794 *(--dp) = *(--sp);
2795 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002796 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002797 }
2798 row_info->channels = 4;
2799 row_info->pixel_depth = 32;
2800 row_info->rowbytes = row_width * 4;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002801 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002802 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002803
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002804#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002805 else if (row_info->bit_depth == 16)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002806 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002807 if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002808 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002809 /* This changes the data from RRGGBB to RRGGBBXX */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002810 png_bytep sp = row + (size_t)row_width * 6;
2811 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002812 for (i = 1; i < row_width; i++)
2813 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002814 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002815 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002816 *(--dp) = *(--sp);
2817 *(--dp) = *(--sp);
2818 *(--dp) = *(--sp);
2819 *(--dp) = *(--sp);
2820 *(--dp) = *(--sp);
2821 *(--dp) = *(--sp);
2822 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002823 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002824 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002825 row_info->channels = 4;
2826 row_info->pixel_depth = 64;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002827 row_info->rowbytes = row_width * 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002828 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002829
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002830 else
2831 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002832 /* This changes the data from RRGGBB to XXRRGGBB */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002833 png_bytep sp = row + (size_t)row_width * 6;
2834 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002835 for (i = 0; i < row_width; i++)
2836 {
2837 *(--dp) = *(--sp);
2838 *(--dp) = *(--sp);
2839 *(--dp) = *(--sp);
2840 *(--dp) = *(--sp);
2841 *(--dp) = *(--sp);
2842 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002843 *(--dp) = lo_filler;
John Bowlercde8cd62014-12-29 16:24:33 -06002844 *(--dp) = hi_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002845 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002846
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002847 row_info->channels = 4;
2848 row_info->pixel_depth = 64;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002849 row_info->rowbytes = row_width * 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002850 }
2851 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002852#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002853 } /* COLOR_TYPE == RGB */
Guy Schalnat0d580581995-07-20 02:43:20 -05002854}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002855#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002856
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002857#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002858/* Expand grayscale files to RGB, with or without alpha */
John Bowler8f1150e2013-12-19 15:33:49 -06002859static void
Guy Schalnat6d764711995-12-19 03:22:19 -06002860png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05002861{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002862 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002863 png_uint_32 row_width = row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06002864
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002865 png_debug(1, "in png_do_gray_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002866
Andreas Dilger47a0c421997-05-16 02:46:07 -05002867 if (row_info->bit_depth >= 8 &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05002868 (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05002869 {
2870 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2871 {
2872 if (row_info->bit_depth == 8)
2873 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002874 /* This changes G to RGB */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002875 png_bytep sp = row + (size_t)row_width - 1;
2876 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002877 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002878 {
2879 *(dp--) = *sp;
2880 *(dp--) = *sp;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002881 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05002882 }
2883 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002884
Guy Schalnat0d580581995-07-20 02:43:20 -05002885 else
2886 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002887 /* This changes GG to RRGGBB */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002888 png_bytep sp = row + (size_t)row_width * 2 - 1;
2889 png_bytep dp = sp + (size_t)row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002890 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002891 {
2892 *(dp--) = *sp;
2893 *(dp--) = *(sp - 1);
2894 *(dp--) = *sp;
2895 *(dp--) = *(sp - 1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002896 *(dp--) = *(sp--);
2897 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05002898 }
2899 }
2900 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002901
Guy Schalnat0d580581995-07-20 02:43:20 -05002902 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2903 {
2904 if (row_info->bit_depth == 8)
2905 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002906 /* This changes GA to RGBA */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002907 png_bytep sp = row + (size_t)row_width * 2 - 1;
2908 png_bytep dp = sp + (size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002909 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002910 {
2911 *(dp--) = *(sp--);
2912 *(dp--) = *sp;
2913 *(dp--) = *sp;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002914 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05002915 }
2916 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002917
Guy Schalnat0d580581995-07-20 02:43:20 -05002918 else
2919 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002920 /* This changes GGAA to RRGGBBAA */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04002921 png_bytep sp = row + (size_t)row_width * 4 - 1;
2922 png_bytep dp = sp + (size_t)row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002923 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002924 {
2925 *(dp--) = *(sp--);
2926 *(dp--) = *(sp--);
2927 *(dp--) = *sp;
2928 *(dp--) = *(sp - 1);
2929 *(dp--) = *sp;
2930 *(dp--) = *(sp - 1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002931 *(dp--) = *(sp--);
2932 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05002933 }
2934 }
2935 }
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002936 row_info->channels = (png_byte)(row_info->channels + 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05002937 row_info->color_type |= PNG_COLOR_MASK_COLOR;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05002938 row_info->pixel_depth = (png_byte)(row_info->channels *
2939 row_info->bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002940 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05002941 }
2942}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002943#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002944
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002945#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002946/* Reduce RGB files to grayscale, with or without alpha
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05002947 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
2948 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
2949 * versions dated 1998 through November 2002 have been archived at
Viktor Szakatsb3ffdc52017-03-31 23:25:57 +00002950 * https://web.archive.org/web/20000816232553/www.inforamp.net/
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05002951 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05002952 * Charles Poynton poynton at poynton.com
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002953 *
2954 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2955 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002956 * which can be expressed with integers as
2957 *
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06002958 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002959 *
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05002960 * Poynton's current link (as of January 2003 through July 2011):
2961 * <http://www.poynton.com/notes/colour_and_gamma/>
2962 * has changed the numbers slightly:
2963 *
2964 * Y = 0.2126*R + 0.7152*G + 0.0722*B
2965 *
2966 * which can be expressed with integers as
2967 *
2968 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
2969 *
John Bowler736f40f2011-08-25 16:19:44 -05002970 * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
2971 * end point chromaticities and the D65 white point. Depending on the
2972 * precision used for the D65 white point this produces a variety of different
2973 * numbers, however if the four decimal place value used in ITU-R Rec 709 is
2974 * used (0.3127,0.3290) the Y calculation would be:
2975 *
2976 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
2977 *
2978 * While this is correct the rounding results in an overflow for white, because
2979 * the sum of the rounded coefficients is 32769, not 32768. Consequently
2980 * libpng uses, instead, the closest non-overflowing approximation:
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05002981 *
2982 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
2983 *
John Bowler736f40f2011-08-25 16:19:44 -05002984 * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
2985 * (including an sRGB chunk) then the chromaticities are used to calculate the
2986 * coefficients. See the chunk handling in pngrutil.c for more information.
Glenn Randers-Pehrson0bc79772011-08-11 09:01:57 -05002987 *
John Bowler736f40f2011-08-25 16:19:44 -05002988 * In all cases the calculation is to be done in a linear colorspace. If no
2989 * gamma information is available to correct the encoding of the original RGB
2990 * values this results in an implicit assumption that the original PNG RGB
2991 * values were linear.
Glenn Randers-Pehrson0bc79772011-08-11 09:01:57 -05002992 *
luz.pazeb91c0e2018-02-09 06:57:29 -05002993 * Other integer coefficients can be used via png_set_rgb_to_gray(). Because
John Bowler736f40f2011-08-25 16:19:44 -05002994 * the API takes just red and green coefficients the blue coefficient is
2995 * calculated to make the sum 32768. This will result in different rounding
2996 * to that used above.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002997 */
John Bowler8f1150e2013-12-19 15:33:49 -06002998static int
John Bowler5d567862011-12-24 09:12:00 -06002999png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003000{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003001 int rgb_error = 0;
3002
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003003 png_debug(1, "in png_do_rgb_to_gray");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003004
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003005 if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3006 (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003007 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04003008 png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3009 png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3010 png_uint_32 bc = 32768 - rc - gc;
3011 png_uint_32 row_width = row_info->width;
3012 int have_alpha = (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003013
John Bowler736f40f2011-08-25 16:19:44 -05003014 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003015 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003016#ifdef PNG_READ_GAMMA_SUPPORTED
John Bowler736f40f2011-08-25 16:19:44 -05003017 /* Notice that gamma to/from 1 are not necessarily inverses (if
3018 * there is an overall gamma correction). Prior to 1.5.5 this code
3019 * checked the linearized values for equality; this doesn't match
3020 * the documentation, the original values must be checked.
3021 */
3022 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3023 {
3024 png_bytep sp = row;
3025 png_bytep dp = row;
3026 png_uint_32 i;
3027
3028 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003029 {
John Bowler736f40f2011-08-25 16:19:44 -05003030 png_byte red = *(sp++);
3031 png_byte green = *(sp++);
3032 png_byte blue = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003033
John Bowler736f40f2011-08-25 16:19:44 -05003034 if (red != green || red != blue)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003035 {
John Bowler736f40f2011-08-25 16:19:44 -05003036 red = png_ptr->gamma_to_1[red];
3037 green = png_ptr->gamma_to_1[green];
3038 blue = png_ptr->gamma_to_1[blue];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003039
John Bowler736f40f2011-08-25 16:19:44 -05003040 rgb_error |= 1;
3041 *(dp++) = png_ptr->gamma_from_1[
3042 (rc*red + gc*green + bc*blue + 16384)>>15];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003043 }
John Bowler736f40f2011-08-25 16:19:44 -05003044
3045 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003046 {
John Bowler736f40f2011-08-25 16:19:44 -05003047 /* If there is no overall correction the table will not be
3048 * set.
3049 */
3050 if (png_ptr->gamma_table != NULL)
3051 red = png_ptr->gamma_table[red];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003052
John Bowler736f40f2011-08-25 16:19:44 -05003053 *(dp++) = red;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003054 }
John Bowler736f40f2011-08-25 16:19:44 -05003055
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003056 if (have_alpha != 0)
John Bowler736f40f2011-08-25 16:19:44 -05003057 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003058 }
3059 }
John Bowler736f40f2011-08-25 16:19:44 -05003060 else
3061#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003062 {
John Bowler736f40f2011-08-25 16:19:44 -05003063 png_bytep sp = row;
3064 png_bytep dp = row;
3065 png_uint_32 i;
3066
3067 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003068 {
John Bowler736f40f2011-08-25 16:19:44 -05003069 png_byte red = *(sp++);
3070 png_byte green = *(sp++);
3071 png_byte blue = *(sp++);
3072
3073 if (red != green || red != blue)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003074 {
John Bowler736f40f2011-08-25 16:19:44 -05003075 rgb_error |= 1;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003076 /* NOTE: this is the historical approach which simply
John Bowler736f40f2011-08-25 16:19:44 -05003077 * truncates the results.
3078 */
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003079 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
John Bowler736f40f2011-08-25 16:19:44 -05003080 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003081
John Bowler736f40f2011-08-25 16:19:44 -05003082 else
3083 *(dp++) = red;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003084
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003085 if (have_alpha != 0)
John Bowler736f40f2011-08-25 16:19:44 -05003086 *(dp++) = *(sp++);
3087 }
3088 }
3089 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003090
John Bowler736f40f2011-08-25 16:19:44 -05003091 else /* RGB bit_depth == 16 */
3092 {
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003093#ifdef PNG_READ_GAMMA_SUPPORTED
John Bowler736f40f2011-08-25 16:19:44 -05003094 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3095 {
3096 png_bytep sp = row;
3097 png_bytep dp = row;
3098 png_uint_32 i;
3099
3100 for (i = 0; i < row_width; i++)
3101 {
3102 png_uint_16 red, green, blue, w;
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06003103 png_byte hi,lo;
John Bowler736f40f2011-08-25 16:19:44 -05003104
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06003105 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3106 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3107 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
John Bowler736f40f2011-08-25 16:19:44 -05003108
3109 if (red == green && red == blue)
3110 {
3111 if (png_ptr->gamma_16_table != NULL)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003112 w = png_ptr->gamma_16_table[(red & 0xff)
3113 >> png_ptr->gamma_shift][red >> 8];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003114
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003115 else
John Bowler736f40f2011-08-25 16:19:44 -05003116 w = red;
3117 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003118
John Bowler736f40f2011-08-25 16:19:44 -05003119 else
3120 {
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05003121 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff)
John Bowler736f40f2011-08-25 16:19:44 -05003122 >> png_ptr->gamma_shift][red>>8];
3123 png_uint_16 green_1 =
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05003124 png_ptr->gamma_16_to_1[(green & 0xff) >>
John Bowler736f40f2011-08-25 16:19:44 -05003125 png_ptr->gamma_shift][green>>8];
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05003126 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff)
John Bowler736f40f2011-08-25 16:19:44 -05003127 >> png_ptr->gamma_shift][blue>>8];
3128 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
3129 + bc*blue_1 + 16384)>>15);
Glenn Randers-Pehrsonf17e6c32015-03-22 19:36:22 -05003130 w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >>
John Bowler736f40f2011-08-25 16:19:44 -05003131 png_ptr->gamma_shift][gray16 >> 8];
3132 rgb_error |= 1;
3133 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003134
John Bowler736f40f2011-08-25 16:19:44 -05003135 *(dp++) = (png_byte)((w>>8) & 0xff);
3136 *(dp++) = (png_byte)(w & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003137
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003138 if (have_alpha != 0)
John Bowler736f40f2011-08-25 16:19:44 -05003139 {
3140 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003141 *(dp++) = *(sp++);
3142 }
3143 }
John Bowler736f40f2011-08-25 16:19:44 -05003144 }
3145 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003146#endif
John Bowler736f40f2011-08-25 16:19:44 -05003147 {
3148 png_bytep sp = row;
3149 png_bytep dp = row;
3150 png_uint_32 i;
3151
3152 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003153 {
John Bowler736f40f2011-08-25 16:19:44 -05003154 png_uint_16 red, green, blue, gray16;
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06003155 png_byte hi,lo;
John Bowler736f40f2011-08-25 16:19:44 -05003156
Glenn Randers-Pehrson8cd78da2015-02-07 09:03:30 -06003157 hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
3158 hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3159 hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
John Bowler736f40f2011-08-25 16:19:44 -05003160
3161 if (red != green || red != blue)
3162 rgb_error |= 1;
3163
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05003164 /* From 1.5.5 in the 16-bit case do the accurate conversion even
John Bowler736f40f2011-08-25 16:19:44 -05003165 * in the 'fast' case - this is because this is where the code
Glenn Randers-Pehrson8b83ff32015-08-13 20:57:18 -05003166 * ends up when handling linear 16-bit data.
John Bowler736f40f2011-08-25 16:19:44 -05003167 */
3168 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3169 15);
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05003170 *(dp++) = (png_byte)((gray16 >> 8) & 0xff);
John Bowler736f40f2011-08-25 16:19:44 -05003171 *(dp++) = (png_byte)(gray16 & 0xff);
3172
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06003173 if (have_alpha != 0)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003174 {
John Bowler736f40f2011-08-25 16:19:44 -05003175 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003176 *(dp++) = *(sp++);
3177 }
3178 }
3179 }
3180 }
John Bowler736f40f2011-08-25 16:19:44 -05003181
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003182 row_info->channels = (png_byte)(row_info->channels - 2);
3183 row_info->color_type = (png_byte)(row_info->color_type &
3184 ~PNG_COLOR_MASK_COLOR);
3185 row_info->pixel_depth = (png_byte)(row_info->channels *
3186 row_info->bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003187 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003188 }
3189 return rgb_error;
3190}
3191#endif
3192
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003193#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
3194 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003195/* Replace any alpha or transparency with the supplied background color.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003196 * "background" is already in the screen gamma, while "background_1" is
3197 * at a gamma of 1.0. Paletted files have already been taken care of.
3198 */
John Bowler8f1150e2013-12-19 15:33:49 -06003199static void
John Bowler5d567862011-12-24 09:12:00 -06003200png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003201{
John Bowler4a12f4a2011-04-17 18:34:22 -05003202#ifdef PNG_READ_GAMMA_SUPPORTED
3203 png_const_bytep gamma_table = png_ptr->gamma_table;
3204 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3205 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3206 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3207 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3208 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3209 int gamma_shift = png_ptr->gamma_shift;
Glenn Randers-Pehrson871b1d02013-03-02 14:58:22 -06003210 int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
John Bowler4a12f4a2011-04-17 18:34:22 -05003211#endif
3212
John Bowlerd273ad22011-05-07 21:00:28 -05003213 png_bytep sp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003214 png_uint_32 i;
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003215 png_uint_32 row_width = row_info->width;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003216 int shift;
Guy Schalnate5a37791996-06-05 15:50:50 -05003217
John Bowlerd273ad22011-05-07 21:00:28 -05003218 png_debug(1, "in png_do_compose");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003219
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003220 switch (row_info->color_type)
Guy Schalnat0d580581995-07-20 02:43:20 -05003221 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003222 case PNG_COLOR_TYPE_GRAY:
Guy Schalnat0d580581995-07-20 02:43:20 -05003223 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003224 switch (row_info->bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -05003225 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003226 case 1:
Guy Schalnat0d580581995-07-20 02:43:20 -05003227 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003228 sp = row;
3229 shift = 7;
3230 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003231 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003232 if ((png_uint_16)((*sp >> shift) & 0x01)
3233 == png_ptr->trans_color.gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05003234 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003235 unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
3236 tmp |=
3237 (unsigned int)(png_ptr->background.gray << shift);
3238 *sp = (png_byte)(tmp & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003239 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003240
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003241 if (shift == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05003242 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003243 shift = 7;
3244 sp++;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003245 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003246
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003247 else
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003248 shift--;
Guy Schalnat0d580581995-07-20 02:43:20 -05003249 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003250 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003251 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003252
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003253 case 2:
Guy Schalnat0d580581995-07-20 02:43:20 -05003254 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003255#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003256 if (gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003257 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003258 sp = row;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003259 shift = 6;
3260 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003261 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003262 if ((png_uint_16)((*sp >> shift) & 0x03)
3263 == png_ptr->trans_color.gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05003264 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003265 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3266 tmp |=
3267 (unsigned int)png_ptr->background.gray << shift;
3268 *sp = (png_byte)(tmp & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003269 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003270
Guy Schalnat0d580581995-07-20 02:43:20 -05003271 else
3272 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003273 unsigned int p = (*sp >> shift) & 0x03;
3274 unsigned int g = (gamma_table [p | (p << 2) |
3275 (p << 4) | (p << 6)] >> 6) & 0x03;
3276 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3277 tmp |= (unsigned int)(g << shift);
3278 *sp = (png_byte)(tmp & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003279 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003280
3281 if (shift == 0)
3282 {
3283 shift = 6;
3284 sp++;
3285 }
3286
3287 else
3288 shift -= 2;
3289 }
3290 }
3291
3292 else
3293#endif
3294 {
3295 sp = row;
3296 shift = 6;
3297 for (i = 0; i < row_width; i++)
3298 {
3299 if ((png_uint_16)((*sp >> shift) & 0x03)
3300 == png_ptr->trans_color.gray)
3301 {
3302 unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
3303 tmp |=
3304 (unsigned int)png_ptr->background.gray << shift;
3305 *sp = (png_byte)(tmp & 0xff);
3306 }
3307
3308 if (shift == 0)
3309 {
3310 shift = 6;
3311 sp++;
3312 }
3313
3314 else
3315 shift -= 2;
3316 }
3317 }
3318 break;
3319 }
3320
3321 case 4:
3322 {
3323#ifdef PNG_READ_GAMMA_SUPPORTED
3324 if (gamma_table != NULL)
3325 {
3326 sp = row;
3327 shift = 4;
3328 for (i = 0; i < row_width; i++)
3329 {
3330 if ((png_uint_16)((*sp >> shift) & 0x0f)
3331 == png_ptr->trans_color.gray)
3332 {
3333 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3334 tmp |=
3335 (unsigned int)(png_ptr->background.gray << shift);
3336 *sp = (png_byte)(tmp & 0xff);
3337 }
3338
3339 else
3340 {
3341 unsigned int p = (*sp >> shift) & 0x0f;
3342 unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
3343 0x0f;
3344 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3345 tmp |= (unsigned int)(g << shift);
3346 *sp = (png_byte)(tmp & 0xff);
3347 }
3348
3349 if (shift == 0)
3350 {
3351 shift = 4;
3352 sp++;
3353 }
3354
3355 else
3356 shift -= 4;
3357 }
3358 }
3359
3360 else
3361#endif
3362 {
3363 sp = row;
3364 shift = 4;
3365 for (i = 0; i < row_width; i++)
3366 {
3367 if ((png_uint_16)((*sp >> shift) & 0x0f)
3368 == png_ptr->trans_color.gray)
3369 {
3370 unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
3371 tmp |=
3372 (unsigned int)(png_ptr->background.gray << shift);
3373 *sp = (png_byte)(tmp & 0xff);
3374 }
3375
3376 if (shift == 0)
3377 {
3378 shift = 4;
3379 sp++;
3380 }
3381
3382 else
3383 shift -= 4;
3384 }
3385 }
3386 break;
3387 }
3388
3389 case 8:
3390 {
3391#ifdef PNG_READ_GAMMA_SUPPORTED
3392 if (gamma_table != NULL)
3393 {
3394 sp = row;
3395 for (i = 0; i < row_width; i++, sp++)
3396 {
3397 if (*sp == png_ptr->trans_color.gray)
3398 *sp = (png_byte)png_ptr->background.gray;
3399
3400 else
3401 *sp = gamma_table[*sp];
Guy Schalnat0d580581995-07-20 02:43:20 -05003402 }
3403 }
3404 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003405#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003406 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003407 sp = row;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003408 for (i = 0; i < row_width; i++, sp++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003409 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003410 if (*sp == png_ptr->trans_color.gray)
3411 *sp = (png_byte)png_ptr->background.gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05003412 }
3413 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003414 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003415 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003416
3417 case 16:
Guy Schalnat0d580581995-07-20 02:43:20 -05003418 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003419#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003420 if (gamma_16 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003421 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003422 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003423 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003424 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003425 png_uint_16 v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003426
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003427 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003428
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003429 if (v == png_ptr->trans_color.gray)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003430 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003431 /* Background is already in screen gamma */
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003432 *sp = (png_byte)((png_ptr->background.gray >> 8)
3433 & 0xff);
3434 *(sp + 1) = (png_byte)(png_ptr->background.gray
3435 & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003436 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003437
Andreas Dilger47a0c421997-05-16 02:46:07 -05003438 else
3439 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003440 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3441 *sp = (png_byte)((v >> 8) & 0xff);
3442 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003443 }
3444 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003445 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003446 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003447#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05003448 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003449 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003450 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003451 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003452 png_uint_16 v;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003453
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003454 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003455
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003456 if (v == png_ptr->trans_color.gray)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003457 {
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05003458 *sp = (png_byte)((png_ptr->background.gray >> 8)
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003459 & 0xff);
3460 *(sp + 1) = (png_byte)(png_ptr->background.gray
3461 & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003462 }
3463 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003464 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003465 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003466 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003467
3468 default:
3469 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003470 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003471 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003472 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05003473
3474 case PNG_COLOR_TYPE_RGB:
3475 {
3476 if (row_info->bit_depth == 8)
3477 {
3478#ifdef PNG_READ_GAMMA_SUPPORTED
3479 if (gamma_table != NULL)
3480 {
3481 sp = row;
3482 for (i = 0; i < row_width; i++, sp += 3)
3483 {
3484 if (*sp == png_ptr->trans_color.red &&
3485 *(sp + 1) == png_ptr->trans_color.green &&
3486 *(sp + 2) == png_ptr->trans_color.blue)
3487 {
3488 *sp = (png_byte)png_ptr->background.red;
3489 *(sp + 1) = (png_byte)png_ptr->background.green;
3490 *(sp + 2) = (png_byte)png_ptr->background.blue;
3491 }
3492
3493 else
3494 {
3495 *sp = gamma_table[*sp];
3496 *(sp + 1) = gamma_table[*(sp + 1)];
3497 *(sp + 2) = gamma_table[*(sp + 2)];
3498 }
3499 }
3500 }
3501 else
3502#endif
3503 {
3504 sp = row;
3505 for (i = 0; i < row_width; i++, sp += 3)
3506 {
3507 if (*sp == png_ptr->trans_color.red &&
3508 *(sp + 1) == png_ptr->trans_color.green &&
3509 *(sp + 2) == png_ptr->trans_color.blue)
3510 {
3511 *sp = (png_byte)png_ptr->background.red;
3512 *(sp + 1) = (png_byte)png_ptr->background.green;
3513 *(sp + 2) = (png_byte)png_ptr->background.blue;
3514 }
3515 }
3516 }
3517 }
3518 else /* if (row_info->bit_depth == 16) */
3519 {
3520#ifdef PNG_READ_GAMMA_SUPPORTED
3521 if (gamma_16 != NULL)
3522 {
3523 sp = row;
3524 for (i = 0; i < row_width; i++, sp += 6)
3525 {
3526 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3527
3528 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3529 + *(sp + 3));
3530
3531 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3532 + *(sp + 5));
3533
3534 if (r == png_ptr->trans_color.red &&
3535 g == png_ptr->trans_color.green &&
3536 b == png_ptr->trans_color.blue)
3537 {
3538 /* Background is already in screen gamma */
3539 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3540 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3541 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3542 & 0xff);
3543 *(sp + 3) = (png_byte)(png_ptr->background.green
3544 & 0xff);
3545 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3546 & 0xff);
3547 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3548 }
3549
3550 else
3551 {
3552 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3553 *sp = (png_byte)((v >> 8) & 0xff);
3554 *(sp + 1) = (png_byte)(v & 0xff);
3555
3556 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3557 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3558 *(sp + 3) = (png_byte)(v & 0xff);
3559
3560 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3561 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3562 *(sp + 5) = (png_byte)(v & 0xff);
3563 }
3564 }
3565 }
3566
3567 else
3568#endif
3569 {
3570 sp = row;
3571 for (i = 0; i < row_width; i++, sp += 6)
3572 {
3573 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3574
3575 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3576 + *(sp + 3));
3577
3578 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3579 + *(sp + 5));
3580
3581 if (r == png_ptr->trans_color.red &&
3582 g == png_ptr->trans_color.green &&
3583 b == png_ptr->trans_color.blue)
3584 {
3585 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3586 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3587 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3588 & 0xff);
3589 *(sp + 3) = (png_byte)(png_ptr->background.green
3590 & 0xff);
3591 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3592 & 0xff);
3593 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3594 }
3595 }
3596 }
3597 }
3598 break;
3599 }
3600
3601 case PNG_COLOR_TYPE_GRAY_ALPHA:
3602 {
3603 if (row_info->bit_depth == 8)
3604 {
3605#ifdef PNG_READ_GAMMA_SUPPORTED
3606 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3607 gamma_table != NULL)
3608 {
3609 sp = row;
3610 for (i = 0; i < row_width; i++, sp += 2)
3611 {
3612 png_uint_16 a = *(sp + 1);
3613
3614 if (a == 0xff)
3615 *sp = gamma_table[*sp];
3616
3617 else if (a == 0)
3618 {
3619 /* Background is already in screen gamma */
3620 *sp = (png_byte)png_ptr->background.gray;
3621 }
3622
3623 else
3624 {
3625 png_byte v, w;
3626
3627 v = gamma_to_1[*sp];
3628 png_composite(w, v, a, png_ptr->background_1.gray);
3629 if (optimize == 0)
3630 w = gamma_from_1[w];
3631 *sp = w;
3632 }
3633 }
3634 }
3635 else
3636#endif
3637 {
3638 sp = row;
3639 for (i = 0; i < row_width; i++, sp += 2)
3640 {
3641 png_byte a = *(sp + 1);
3642
3643 if (a == 0)
3644 *sp = (png_byte)png_ptr->background.gray;
3645
3646 else if (a < 0xff)
3647 png_composite(*sp, *sp, a, png_ptr->background.gray);
3648 }
3649 }
3650 }
3651 else /* if (png_ptr->bit_depth == 16) */
3652 {
3653#ifdef PNG_READ_GAMMA_SUPPORTED
3654 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3655 gamma_16_to_1 != NULL)
3656 {
3657 sp = row;
3658 for (i = 0; i < row_width; i++, sp += 4)
3659 {
3660 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3661 + *(sp + 3));
3662
3663 if (a == (png_uint_16)0xffff)
3664 {
3665 png_uint_16 v;
3666
3667 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3668 *sp = (png_byte)((v >> 8) & 0xff);
3669 *(sp + 1) = (png_byte)(v & 0xff);
3670 }
3671
3672 else if (a == 0)
3673 {
3674 /* Background is already in screen gamma */
3675 *sp = (png_byte)((png_ptr->background.gray >> 8)
3676 & 0xff);
3677 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3678 }
3679
3680 else
3681 {
3682 png_uint_16 g, v, w;
3683
3684 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3685 png_composite_16(v, g, a, png_ptr->background_1.gray);
3686 if (optimize != 0)
3687 w = v;
3688 else
3689 w = gamma_16_from_1[(v & 0xff) >>
3690 gamma_shift][v >> 8];
3691 *sp = (png_byte)((w >> 8) & 0xff);
3692 *(sp + 1) = (png_byte)(w & 0xff);
3693 }
3694 }
3695 }
3696 else
3697#endif
3698 {
3699 sp = row;
3700 for (i = 0; i < row_width; i++, sp += 4)
3701 {
3702 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3703 + *(sp + 3));
3704
3705 if (a == 0)
3706 {
3707 *sp = (png_byte)((png_ptr->background.gray >> 8)
3708 & 0xff);
3709 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3710 }
3711
3712 else if (a < 0xffff)
3713 {
3714 png_uint_16 g, v;
3715
3716 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3717 png_composite_16(v, g, a, png_ptr->background.gray);
3718 *sp = (png_byte)((v >> 8) & 0xff);
3719 *(sp + 1) = (png_byte)(v & 0xff);
3720 }
3721 }
3722 }
3723 }
3724 break;
3725 }
3726
3727 case PNG_COLOR_TYPE_RGB_ALPHA:
3728 {
3729 if (row_info->bit_depth == 8)
3730 {
3731#ifdef PNG_READ_GAMMA_SUPPORTED
3732 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3733 gamma_table != NULL)
3734 {
3735 sp = row;
3736 for (i = 0; i < row_width; i++, sp += 4)
3737 {
3738 png_byte a = *(sp + 3);
3739
3740 if (a == 0xff)
3741 {
3742 *sp = gamma_table[*sp];
3743 *(sp + 1) = gamma_table[*(sp + 1)];
3744 *(sp + 2) = gamma_table[*(sp + 2)];
3745 }
3746
3747 else if (a == 0)
3748 {
3749 /* Background is already in screen gamma */
3750 *sp = (png_byte)png_ptr->background.red;
3751 *(sp + 1) = (png_byte)png_ptr->background.green;
3752 *(sp + 2) = (png_byte)png_ptr->background.blue;
3753 }
3754
3755 else
3756 {
3757 png_byte v, w;
3758
3759 v = gamma_to_1[*sp];
3760 png_composite(w, v, a, png_ptr->background_1.red);
3761 if (optimize == 0) w = gamma_from_1[w];
3762 *sp = w;
3763
3764 v = gamma_to_1[*(sp + 1)];
3765 png_composite(w, v, a, png_ptr->background_1.green);
3766 if (optimize == 0) w = gamma_from_1[w];
3767 *(sp + 1) = w;
3768
3769 v = gamma_to_1[*(sp + 2)];
3770 png_composite(w, v, a, png_ptr->background_1.blue);
3771 if (optimize == 0) w = gamma_from_1[w];
3772 *(sp + 2) = w;
3773 }
3774 }
3775 }
3776 else
3777#endif
3778 {
3779 sp = row;
3780 for (i = 0; i < row_width; i++, sp += 4)
3781 {
3782 png_byte a = *(sp + 3);
3783
3784 if (a == 0)
3785 {
3786 *sp = (png_byte)png_ptr->background.red;
3787 *(sp + 1) = (png_byte)png_ptr->background.green;
3788 *(sp + 2) = (png_byte)png_ptr->background.blue;
3789 }
3790
3791 else if (a < 0xff)
3792 {
3793 png_composite(*sp, *sp, a, png_ptr->background.red);
3794
3795 png_composite(*(sp + 1), *(sp + 1), a,
3796 png_ptr->background.green);
3797
3798 png_composite(*(sp + 2), *(sp + 2), a,
3799 png_ptr->background.blue);
3800 }
3801 }
3802 }
3803 }
3804 else /* if (row_info->bit_depth == 16) */
3805 {
3806#ifdef PNG_READ_GAMMA_SUPPORTED
3807 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3808 gamma_16_to_1 != NULL)
3809 {
3810 sp = row;
3811 for (i = 0; i < row_width; i++, sp += 8)
3812 {
3813 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3814 << 8) + (png_uint_16)(*(sp + 7)));
3815
3816 if (a == (png_uint_16)0xffff)
3817 {
3818 png_uint_16 v;
3819
3820 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3821 *sp = (png_byte)((v >> 8) & 0xff);
3822 *(sp + 1) = (png_byte)(v & 0xff);
3823
3824 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3825 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3826 *(sp + 3) = (png_byte)(v & 0xff);
3827
3828 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3829 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3830 *(sp + 5) = (png_byte)(v & 0xff);
3831 }
3832
3833 else if (a == 0)
3834 {
3835 /* Background is already in screen gamma */
3836 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3837 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3838 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3839 & 0xff);
3840 *(sp + 3) = (png_byte)(png_ptr->background.green
3841 & 0xff);
3842 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3843 & 0xff);
3844 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3845 }
3846
3847 else
3848 {
3849 png_uint_16 v, w;
3850
3851 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3852 png_composite_16(w, v, a, png_ptr->background_1.red);
3853 if (optimize == 0)
3854 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3855 8];
3856 *sp = (png_byte)((w >> 8) & 0xff);
3857 *(sp + 1) = (png_byte)(w & 0xff);
3858
3859 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3860 png_composite_16(w, v, a, png_ptr->background_1.green);
3861 if (optimize == 0)
3862 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3863 8];
3864
3865 *(sp + 2) = (png_byte)((w >> 8) & 0xff);
3866 *(sp + 3) = (png_byte)(w & 0xff);
3867
3868 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3869 png_composite_16(w, v, a, png_ptr->background_1.blue);
3870 if (optimize == 0)
3871 w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3872 8];
3873
3874 *(sp + 4) = (png_byte)((w >> 8) & 0xff);
3875 *(sp + 5) = (png_byte)(w & 0xff);
3876 }
3877 }
3878 }
3879
3880 else
3881#endif
3882 {
3883 sp = row;
3884 for (i = 0; i < row_width; i++, sp += 8)
3885 {
3886 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3887 << 8) + (png_uint_16)(*(sp + 7)));
3888
3889 if (a == 0)
3890 {
3891 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3892 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3893 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3894 & 0xff);
3895 *(sp + 3) = (png_byte)(png_ptr->background.green
3896 & 0xff);
3897 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3898 & 0xff);
3899 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3900 }
3901
3902 else if (a < 0xffff)
3903 {
3904 png_uint_16 v;
3905
3906 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3907 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3908 + *(sp + 3));
3909 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3910 + *(sp + 5));
3911
3912 png_composite_16(v, r, a, png_ptr->background.red);
3913 *sp = (png_byte)((v >> 8) & 0xff);
3914 *(sp + 1) = (png_byte)(v & 0xff);
3915
3916 png_composite_16(v, g, a, png_ptr->background.green);
3917 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3918 *(sp + 3) = (png_byte)(v & 0xff);
3919
3920 png_composite_16(v, b, a, png_ptr->background.blue);
3921 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3922 *(sp + 5) = (png_byte)(v & 0xff);
3923 }
3924 }
3925 }
3926 }
3927 break;
3928 }
3929
3930 default:
3931 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003932 }
3933}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06003934#endif /* READ_BACKGROUND || READ_ALPHA_MODE */
Guy Schalnat0d580581995-07-20 02:43:20 -05003935
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003936#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003937/* Gamma correct the image, avoiding the alpha channel. Make sure
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05003938 * you do this after you deal with the transparency issue on grayscale
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -05003939 * or RGB images. If your bit depth is 8, use gamma_table, if it
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003940 * is 16, use gamma_16_table and gamma_shift. Build these with
3941 * build_gamma_table().
3942 */
John Bowler8f1150e2013-12-19 15:33:49 -06003943static void
John Bowler5d567862011-12-24 09:12:00 -06003944png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003945{
John Bowler4a12f4a2011-04-17 18:34:22 -05003946 png_const_bytep gamma_table = png_ptr->gamma_table;
3947 png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
3948 int gamma_shift = png_ptr->gamma_shift;
3949
Guy Schalnat6d764711995-12-19 03:22:19 -06003950 png_bytep sp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003951 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003952 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06003953
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003954 png_debug(1, "in png_do_gamma");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003955
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003956 if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003957 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
Guy Schalnat0d580581995-07-20 02:43:20 -05003958 {
3959 switch (row_info->color_type)
3960 {
3961 case PNG_COLOR_TYPE_RGB:
3962 {
3963 if (row_info->bit_depth == 8)
3964 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003965 sp = row;
3966 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003967 {
3968 *sp = gamma_table[*sp];
3969 sp++;
3970 *sp = gamma_table[*sp];
3971 sp++;
3972 *sp = gamma_table[*sp];
3973 sp++;
3974 }
3975 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003976
Andreas Dilger47a0c421997-05-16 02:46:07 -05003977 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05003978 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003979 sp = row;
3980 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003981 {
3982 png_uint_16 v;
3983
Andreas Dilger47a0c421997-05-16 02:46:07 -05003984 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003985 *sp = (png_byte)((v >> 8) & 0xff);
3986 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003987 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003988
Andreas Dilger47a0c421997-05-16 02:46:07 -05003989 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003990 *sp = (png_byte)((v >> 8) & 0xff);
3991 *(sp + 1) = (png_byte)(v & 0xff);
3992 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003993
Andreas Dilger47a0c421997-05-16 02:46:07 -05003994 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003995 *sp = (png_byte)((v >> 8) & 0xff);
3996 *(sp + 1) = (png_byte)(v & 0xff);
3997 sp += 2;
3998 }
3999 }
4000 break;
4001 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004002
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004003 case PNG_COLOR_TYPE_RGB_ALPHA:
4004 {
4005 if (row_info->bit_depth == 8)
4006 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004007 sp = row;
4008 for (i = 0; i < row_width; i++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004009 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004010 *sp = gamma_table[*sp];
4011 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004012
Guy Schalnat0d580581995-07-20 02:43:20 -05004013 *sp = gamma_table[*sp];
4014 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004015
Guy Schalnat0d580581995-07-20 02:43:20 -05004016 *sp = gamma_table[*sp];
4017 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004018
Guy Schalnat0d580581995-07-20 02:43:20 -05004019 sp++;
4020 }
4021 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004022
Andreas Dilger47a0c421997-05-16 02:46:07 -05004023 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05004024 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004025 sp = row;
4026 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004027 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004028 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004029 *sp = (png_byte)((v >> 8) & 0xff);
4030 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004031 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004032
Andreas Dilger47a0c421997-05-16 02:46:07 -05004033 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004034 *sp = (png_byte)((v >> 8) & 0xff);
4035 *(sp + 1) = (png_byte)(v & 0xff);
4036 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004037
Andreas Dilger47a0c421997-05-16 02:46:07 -05004038 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004039 *sp = (png_byte)((v >> 8) & 0xff);
4040 *(sp + 1) = (png_byte)(v & 0xff);
4041 sp += 4;
4042 }
4043 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004044 break;
4045 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004046
Guy Schalnat0d580581995-07-20 02:43:20 -05004047 case PNG_COLOR_TYPE_GRAY_ALPHA:
4048 {
4049 if (row_info->bit_depth == 8)
4050 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004051 sp = row;
4052 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004053 {
4054 *sp = gamma_table[*sp];
Andreas Dilger47a0c421997-05-16 02:46:07 -05004055 sp += 2;
Guy Schalnat0d580581995-07-20 02:43:20 -05004056 }
4057 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004058
Andreas Dilger47a0c421997-05-16 02:46:07 -05004059 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05004060 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004061 sp = row;
4062 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004063 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004064 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004065 *sp = (png_byte)((v >> 8) & 0xff);
4066 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004067 sp += 4;
4068 }
4069 }
4070 break;
4071 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004072
Guy Schalnat0d580581995-07-20 02:43:20 -05004073 case PNG_COLOR_TYPE_GRAY:
4074 {
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004075 if (row_info->bit_depth == 2)
4076 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004077 sp = row;
4078 for (i = 0; i < row_width; i += 4)
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004079 {
4080 int a = *sp & 0xc0;
4081 int b = *sp & 0x30;
4082 int c = *sp & 0x0c;
4083 int d = *sp & 0x03;
4084
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004085 *sp = (png_byte)(
4086 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06004087 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
4088 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004089 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004090 sp++;
4091 }
4092 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004093
Andreas Dilger47a0c421997-05-16 02:46:07 -05004094 if (row_info->bit_depth == 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05004095 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004096 sp = row;
4097 for (i = 0; i < row_width; i += 2)
Andreas Dilger47a0c421997-05-16 02:46:07 -05004098 {
4099 int msb = *sp & 0xf0;
4100 int lsb = *sp & 0x0f;
4101
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05004102 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06004103 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
Andreas Dilger47a0c421997-05-16 02:46:07 -05004104 sp++;
4105 }
4106 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004107
Andreas Dilger47a0c421997-05-16 02:46:07 -05004108 else if (row_info->bit_depth == 8)
4109 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004110 sp = row;
4111 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004112 {
4113 *sp = gamma_table[*sp];
4114 sp++;
4115 }
4116 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004117
Guy Schalnat0d580581995-07-20 02:43:20 -05004118 else if (row_info->bit_depth == 16)
4119 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004120 sp = row;
4121 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004122 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004123 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004124 *sp = (png_byte)((v >> 8) & 0xff);
4125 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004126 sp += 2;
4127 }
4128 }
4129 break;
4130 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004131
4132 default:
4133 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004134 }
4135 }
4136}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004137#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004138
John Bowlerd273ad22011-05-07 21:00:28 -05004139#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4140/* Encode the alpha channel to the output gamma (the input channel is always
4141 * linear.) Called only with color types that have an alpha channel. Needs the
4142 * from_1 tables.
4143 */
John Bowler8f1150e2013-12-19 15:33:49 -06004144static void
John Bowler5d567862011-12-24 09:12:00 -06004145png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
John Bowlerd273ad22011-05-07 21:00:28 -05004146{
4147 png_uint_32 row_width = row_info->width;
4148
4149 png_debug(1, "in png_do_encode_alpha");
4150
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004151 if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
John Bowlerd273ad22011-05-07 21:00:28 -05004152 {
4153 if (row_info->bit_depth == 8)
4154 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04004155 png_bytep table = png_ptr->gamma_from_1;
John Bowlerd273ad22011-05-07 21:00:28 -05004156
4157 if (table != NULL)
4158 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04004159 int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
John Bowlerd273ad22011-05-07 21:00:28 -05004160
4161 /* The alpha channel is the last component: */
4162 row += step - 1;
4163
4164 for (; row_width > 0; --row_width, row += step)
4165 *row = table[*row];
4166
4167 return;
4168 }
4169 }
4170
4171 else if (row_info->bit_depth == 16)
4172 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04004173 png_uint_16pp table = png_ptr->gamma_16_from_1;
4174 int gamma_shift = png_ptr->gamma_shift;
John Bowlerd273ad22011-05-07 21:00:28 -05004175
4176 if (table != NULL)
4177 {
Cosmin Trutaceb32772018-08-18 22:47:16 -04004178 int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
John Bowlerd273ad22011-05-07 21:00:28 -05004179
4180 /* The alpha channel is the last component: */
4181 row += step - 2;
4182
4183 for (; row_width > 0; --row_width, row += step)
4184 {
4185 png_uint_16 v;
4186
4187 v = table[*(row + 1) >> gamma_shift][*row];
4188 *row = (png_byte)((v >> 8) & 0xff);
4189 *(row + 1) = (png_byte)(v & 0xff);
4190 }
4191
4192 return;
4193 }
4194 }
4195 }
4196
4197 /* Only get to here if called with a weird row_info; no harm has been done,
4198 * so just issue a warning.
4199 */
4200 png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
4201}
4202#endif
4203
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004204#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -05004205/* Expands a palette row to an RGB or RGBA row depending
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004206 * upon whether you supply trans and num_trans.
4207 */
John Bowler8f1150e2013-12-19 15:33:49 -06004208static void
Richard Townsend7734cda2018-01-25 19:03:04 +00004209png_do_expand_palette(png_structrp png_ptr, png_row_infop row_info,
Cosmin Trutab66ed712018-09-04 00:15:30 -04004210 png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha,
4211 int num_trans)
Guy Schalnat0d580581995-07-20 02:43:20 -05004212{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004213 int shift, value;
Guy Schalnat6d764711995-12-19 03:22:19 -06004214 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004215 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004216 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004217
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004218 png_debug(1, "in png_do_expand_palette");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004219
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004220 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -05004221 {
4222 if (row_info->bit_depth < 8)
4223 {
4224 switch (row_info->bit_depth)
4225 {
4226 case 1:
4227 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004228 sp = row + (size_t)((row_width - 1) >> 3);
4229 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004230 shift = 7 - (int)((row_width + 7) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004231 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004232 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004233 if ((*sp >> shift) & 0x01)
Guy Schalnat0d580581995-07-20 02:43:20 -05004234 *dp = 1;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004235
Guy Schalnat0d580581995-07-20 02:43:20 -05004236 else
4237 *dp = 0;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004238
Guy Schalnat0d580581995-07-20 02:43:20 -05004239 if (shift == 7)
4240 {
4241 shift = 0;
4242 sp--;
4243 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004244
Guy Schalnat0d580581995-07-20 02:43:20 -05004245 else
4246 shift++;
4247
4248 dp--;
4249 }
4250 break;
4251 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004252
Guy Schalnat0d580581995-07-20 02:43:20 -05004253 case 2:
4254 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004255 sp = row + (size_t)((row_width - 1) >> 2);
4256 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004257 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004258 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004259 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004260 value = (*sp >> shift) & 0x03;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004261 *dp = (png_byte)value;
Guy Schalnat0d580581995-07-20 02:43:20 -05004262 if (shift == 6)
4263 {
4264 shift = 0;
4265 sp--;
4266 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004267
Guy Schalnat0d580581995-07-20 02:43:20 -05004268 else
4269 shift += 2;
4270
4271 dp--;
4272 }
4273 break;
4274 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004275
Guy Schalnat0d580581995-07-20 02:43:20 -05004276 case 4:
4277 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004278 sp = row + (size_t)((row_width - 1) >> 1);
4279 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004280 shift = (int)((row_width & 0x01) << 2);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004281 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004282 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004283 value = (*sp >> shift) & 0x0f;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004284 *dp = (png_byte)value;
Guy Schalnat0d580581995-07-20 02:43:20 -05004285 if (shift == 4)
4286 {
4287 shift = 0;
4288 sp--;
4289 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004290
Guy Schalnat0d580581995-07-20 02:43:20 -05004291 else
4292 shift += 4;
4293
4294 dp--;
4295 }
4296 break;
4297 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004298
4299 default:
4300 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004301 }
4302 row_info->bit_depth = 8;
4303 row_info->pixel_depth = 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004304 row_info->rowbytes = row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004305 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004306
John Bowler4e230b02011-01-08 14:49:25 -06004307 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004308 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004309 {
John Bowlercd3b0cc2011-06-14 23:01:07 -05004310 if (num_trans > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004311 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004312 sp = row + (size_t)row_width - 1;
4313 dp = row + ((size_t)row_width << 2) - 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05004314
Richard Townsend7734cda2018-01-25 19:03:04 +00004315 i = 0;
4316#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
Cosmin Trutab66ed712018-09-04 00:15:30 -04004317 if (png_ptr->riffled_palette != NULL)
4318 {
Richard Townsend7734cda2018-01-25 19:03:04 +00004319 /* The RGBA optimization works with png_ptr->bit_depth == 8
Cosmin Trutab66ed712018-09-04 00:15:30 -04004320 * but sometimes row_info->bit_depth has been changed to 8.
4321 * In these cases, the palette hasn't been riffled.
4322 */
Cosmin Truta82ae6232019-03-31 09:44:45 -04004323 i = png_do_expand_palette_rgba8_neon(png_ptr, row_info, row,
Cosmin Trutab66ed712018-09-04 00:15:30 -04004324 &sp, &dp);
Richard Townsend7734cda2018-01-25 19:03:04 +00004325 }
Cosmin Truta70d122a2019-02-03 19:51:18 -05004326#else
4327 PNG_UNUSED(png_ptr)
Richard Townsend7734cda2018-01-25 19:03:04 +00004328#endif
4329
4330 for (; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004331 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05004332 if ((int)(*sp) >= num_trans)
Guy Schalnat0d580581995-07-20 02:43:20 -05004333 *dp-- = 0xff;
4334 else
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05004335 *dp-- = trans_alpha[*sp];
Guy Schalnat0d580581995-07-20 02:43:20 -05004336 *dp-- = palette[*sp].blue;
4337 *dp-- = palette[*sp].green;
4338 *dp-- = palette[*sp].red;
4339 sp--;
4340 }
4341 row_info->bit_depth = 8;
4342 row_info->pixel_depth = 32;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004343 row_info->rowbytes = row_width * 4;
Guy Schalnat0d580581995-07-20 02:43:20 -05004344 row_info->color_type = 6;
4345 row_info->channels = 4;
4346 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004347
Guy Schalnat0d580581995-07-20 02:43:20 -05004348 else
4349 {
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004350 sp = row + (size_t)row_width - 1;
4351 dp = row + (size_t)(row_width * 3) - 1;
Richard Townsend7734cda2018-01-25 19:03:04 +00004352 i = 0;
4353#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
Cosmin Truta82ae6232019-03-31 09:44:45 -04004354 i = png_do_expand_palette_rgb8_neon(png_ptr, row_info, row,
Cosmin Trutab66ed712018-09-04 00:15:30 -04004355 &sp, &dp);
Cosmin Truta70d122a2019-02-03 19:51:18 -05004356#else
4357 PNG_UNUSED(png_ptr)
Richard Townsend7734cda2018-01-25 19:03:04 +00004358#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004359
Richard Townsend7734cda2018-01-25 19:03:04 +00004360 for (; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004361 {
4362 *dp-- = palette[*sp].blue;
4363 *dp-- = palette[*sp].green;
4364 *dp-- = palette[*sp].red;
4365 sp--;
4366 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004367
Guy Schalnat0d580581995-07-20 02:43:20 -05004368 row_info->bit_depth = 8;
4369 row_info->pixel_depth = 24;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004370 row_info->rowbytes = row_width * 3;
Guy Schalnat0d580581995-07-20 02:43:20 -05004371 row_info->color_type = 2;
4372 row_info->channels = 3;
4373 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004374 }
4375 }
4376 }
4377}
4378
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06004379/* If the bit depth < 8, it is expanded to 8. Also, if the already
4380 * expanded transparency value is supplied, an alpha channel is built.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004381 */
John Bowler8f1150e2013-12-19 15:33:49 -06004382static void
Guy Schalnat6d764711995-12-19 03:22:19 -06004383png_do_expand(png_row_infop row_info, png_bytep row,
John Bowlercd3b0cc2011-06-14 23:01:07 -05004384 png_const_color_16p trans_color)
Guy Schalnat0d580581995-07-20 02:43:20 -05004385{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004386 int shift, value;
Guy Schalnat6d764711995-12-19 03:22:19 -06004387 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004388 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004389 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004390
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004391 png_debug(1, "in png_do_expand");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004392
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004393 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05004394 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004395 unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
4396
4397 if (row_info->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004398 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004399 switch (row_info->bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -05004400 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004401 case 1:
Guy Schalnat0d580581995-07-20 02:43:20 -05004402 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004403 gray = (gray & 0x01) * 0xff;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004404 sp = row + (size_t)((row_width - 1) >> 3);
4405 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004406 shift = 7 - (int)((row_width + 7) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004407 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004408 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004409 if ((*sp >> shift) & 0x01)
4410 *dp = 0xff;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004411
Guy Schalnat0d580581995-07-20 02:43:20 -05004412 else
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004413 *dp = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004414
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004415 if (shift == 7)
Guy Schalnat0d580581995-07-20 02:43:20 -05004416 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004417 shift = 0;
4418 sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004419 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004420
Guy Schalnat0d580581995-07-20 02:43:20 -05004421 else
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004422 shift++;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004423
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004424 dp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004425 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004426 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004427 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004428
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004429 case 2:
4430 {
4431 gray = (gray & 0x03) * 0x55;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004432 sp = row + (size_t)((row_width - 1) >> 2);
4433 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004434 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4435 for (i = 0; i < row_width; i++)
4436 {
4437 value = (*sp >> shift) & 0x03;
4438 *dp = (png_byte)(value | (value << 2) | (value << 4) |
4439 (value << 6));
4440 if (shift == 6)
4441 {
4442 shift = 0;
4443 sp--;
4444 }
4445
4446 else
4447 shift += 2;
4448
4449 dp--;
4450 }
4451 break;
4452 }
4453
4454 case 4:
4455 {
4456 gray = (gray & 0x0f) * 0x11;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004457 sp = row + (size_t)((row_width - 1) >> 1);
4458 dp = row + (size_t)row_width - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004459 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
4460 for (i = 0; i < row_width; i++)
4461 {
4462 value = (*sp >> shift) & 0x0f;
4463 *dp = (png_byte)(value | (value << 4));
4464 if (shift == 4)
4465 {
4466 shift = 0;
4467 sp--;
4468 }
4469
4470 else
4471 shift = 4;
4472
4473 dp--;
4474 }
4475 break;
4476 }
4477
4478 default:
4479 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004480 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004481
4482 row_info->bit_depth = 8;
4483 row_info->pixel_depth = 8;
4484 row_info->rowbytes = row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004485 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004486
4487 if (trans_color != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05004488 {
4489 if (row_info->bit_depth == 8)
4490 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004491 gray = gray & 0xff;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004492 sp = row + (size_t)row_width - 1;
4493 dp = row + ((size_t)row_width << 1) - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004494
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004495 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004496 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004497 if ((*sp & 0xffU) == gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05004498 *dp-- = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004499
Guy Schalnat0d580581995-07-20 02:43:20 -05004500 else
4501 *dp-- = 0xff;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004502
Guy Schalnat0d580581995-07-20 02:43:20 -05004503 *dp-- = *sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004504 }
4505 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004506
Guy Schalnat0d580581995-07-20 02:43:20 -05004507 else if (row_info->bit_depth == 16)
4508 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004509 unsigned int gray_high = (gray >> 8) & 0xff;
4510 unsigned int gray_low = gray & 0xff;
Andreas Dilger47a0c421997-05-16 02:46:07 -05004511 sp = row + row_info->rowbytes - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004512 dp = row + (row_info->rowbytes << 1) - 1;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004513 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004514 {
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004515 if ((*(sp - 1) & 0xffU) == gray_high &&
4516 (*(sp) & 0xffU) == gray_low)
Guy Schalnat0d580581995-07-20 02:43:20 -05004517 {
4518 *dp-- = 0;
4519 *dp-- = 0;
4520 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004521
Guy Schalnat0d580581995-07-20 02:43:20 -05004522 else
4523 {
4524 *dp-- = 0xff;
4525 *dp-- = 0xff;
4526 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004527
Guy Schalnat0d580581995-07-20 02:43:20 -05004528 *dp-- = *sp--;
4529 *dp-- = *sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004530 }
4531 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004532
4533 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4534 row_info->channels = 2;
4535 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4536 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4537 row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004538 }
4539 }
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004540 else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4541 trans_color != NULL)
4542 {
4543 if (row_info->bit_depth == 8)
4544 {
4545 png_byte red = (png_byte)(trans_color->red & 0xff);
4546 png_byte green = (png_byte)(trans_color->green & 0xff);
4547 png_byte blue = (png_byte)(trans_color->blue & 0xff);
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004548 sp = row + (size_t)row_info->rowbytes - 1;
4549 dp = row + ((size_t)row_width << 2) - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004550 for (i = 0; i < row_width; i++)
4551 {
4552 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4553 *dp-- = 0;
4554
4555 else
4556 *dp-- = 0xff;
4557
4558 *dp-- = *sp--;
4559 *dp-- = *sp--;
4560 *dp-- = *sp--;
4561 }
4562 }
4563 else if (row_info->bit_depth == 16)
4564 {
4565 png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
4566 png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
4567 png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
4568 png_byte red_low = (png_byte)(trans_color->red & 0xff);
4569 png_byte green_low = (png_byte)(trans_color->green & 0xff);
4570 png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
4571 sp = row + row_info->rowbytes - 1;
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04004572 dp = row + ((size_t)row_width << 3) - 1;
Glenn Randers-Pehrson4b52ef82017-10-31 20:57:23 -05004573 for (i = 0; i < row_width; i++)
4574 {
4575 if (*(sp - 5) == red_high &&
4576 *(sp - 4) == red_low &&
4577 *(sp - 3) == green_high &&
4578 *(sp - 2) == green_low &&
4579 *(sp - 1) == blue_high &&
4580 *(sp ) == blue_low)
4581 {
4582 *dp-- = 0;
4583 *dp-- = 0;
4584 }
4585
4586 else
4587 {
4588 *dp-- = 0xff;
4589 *dp-- = 0xff;
4590 }
4591
4592 *dp-- = *sp--;
4593 *dp-- = *sp--;
4594 *dp-- = *sp--;
4595 *dp-- = *sp--;
4596 *dp-- = *sp--;
4597 *dp-- = *sp--;
4598 }
4599 }
4600 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4601 row_info->channels = 4;
4602 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4603 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4604 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004605}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004606#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004607
John Bowler4d562962011-02-12 09:01:20 -06004608#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson8b8aacd2011-08-25 18:10:50 -05004609/* If the bit depth is 8 and the color type is not a palette type expand the
John Bowler4d562962011-02-12 09:01:20 -06004610 * whole row to 16 bits. Has no effect otherwise.
4611 */
John Bowler8f1150e2013-12-19 15:33:49 -06004612static void
John Bowler4d562962011-02-12 09:01:20 -06004613png_do_expand_16(png_row_infop row_info, png_bytep row)
4614{
4615 if (row_info->bit_depth == 8 &&
4616 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4617 {
4618 /* The row have a sequence of bytes containing [0..255] and we need
4619 * to turn it into another row containing [0..65535], to do this we
4620 * calculate:
4621 *
4622 * (input / 255) * 65535
4623 *
4624 * Which happens to be exactly input * 257 and this can be achieved
4625 * simply by byte replication in place (copying backwards).
4626 */
4627 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4628 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
4629 while (dp > sp)
Viktor Szakats8c50acb2017-03-29 23:54:40 +00004630 {
4631 dp[-2] = dp[-1] = *--sp; dp -= 2;
4632 }
John Bowler4d562962011-02-12 09:01:20 -06004633
4634 row_info->rowbytes *= 2;
4635 row_info->bit_depth = 16;
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05004636 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
John Bowler4d562962011-02-12 09:01:20 -06004637 }
4638}
4639#endif
4640
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004641#ifdef PNG_READ_QUANTIZE_SUPPORTED
John Bowler8f1150e2013-12-19 15:33:49 -06004642static void
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004643png_do_quantize(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05004644 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
Guy Schalnat0d580581995-07-20 02:43:20 -05004645{
Guy Schalnat6d764711995-12-19 03:22:19 -06004646 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004647 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004648 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004649
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004650 png_debug(1, "in png_do_quantize");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004651
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004652 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004653 {
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004654 if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
Guy Schalnat0d580581995-07-20 02:43:20 -05004655 {
4656 int r, g, b, p;
Guy Schalnat0d580581995-07-20 02:43:20 -05004657 sp = row;
4658 dp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004659 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004660 {
4661 r = *sp++;
4662 g = *sp++;
4663 b = *sp++;
4664
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004665 /* This looks real messy, but the compiler will reduce
4666 * it down to a reasonable formula. For example, with
4667 * 5 bits per color, we get:
4668 * p = (((r >> 3) & 0x1f) << 10) |
4669 * (((g >> 3) & 0x1f) << 5) |
4670 * ((b >> 3) & 0x1f);
4671 */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004672 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4673 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4674 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4675 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4676 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4677 (PNG_QUANTIZE_BLUE_BITS)) |
4678 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4679 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
Guy Schalnat0d580581995-07-20 02:43:20 -05004680
4681 *dp++ = palette_lookup[p];
4682 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004683
Guy Schalnat0d580581995-07-20 02:43:20 -05004684 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4685 row_info->channels = 1;
4686 row_info->pixel_depth = row_info->bit_depth;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004687 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004688 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004689
Guy Schalnat0d580581995-07-20 02:43:20 -05004690 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004691 palette_lookup != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05004692 {
4693 int r, g, b, p;
Guy Schalnat0d580581995-07-20 02:43:20 -05004694 sp = row;
4695 dp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004696 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004697 {
4698 r = *sp++;
4699 g = *sp++;
4700 b = *sp++;
4701 sp++;
4702
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004703 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4704 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4705 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4706 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4707 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4708 (PNG_QUANTIZE_BLUE_BITS)) |
4709 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4710 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
Guy Schalnat0d580581995-07-20 02:43:20 -05004711
4712 *dp++ = palette_lookup[p];
4713 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004714
Guy Schalnat0d580581995-07-20 02:43:20 -05004715 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4716 row_info->channels = 1;
4717 row_info->pixel_depth = row_info->bit_depth;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004718 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004719 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004720
Guy Schalnat0d580581995-07-20 02:43:20 -05004721 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004722 quantize_lookup)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004723 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004724 sp = row;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004725
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004726 for (i = 0; i < row_width; i++, sp++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004727 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004728 *sp = quantize_lookup[*sp];
Guy Schalnat0d580581995-07-20 02:43:20 -05004729 }
4730 }
4731 }
4732}
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06004733#endif /* READ_QUANTIZE */
Guy Schalnat0d580581995-07-20 02:43:20 -05004734
John Bowlerc10930a2013-12-19 15:24:06 -06004735/* Transform the row. The order of transformations is significant,
4736 * and is very touchy. If you add a transformation, take care to
4737 * decide how it fits in with the other transformations here.
4738 */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004739void /* PRIVATE */
John Bowlerc10930a2013-12-19 15:24:06 -06004740png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004741{
John Bowlerc10930a2013-12-19 15:24:06 -06004742 png_debug(1, "in png_do_read_transformations");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004743
John Bowlerc10930a2013-12-19 15:24:06 -06004744 if (png_ptr->row_buf == NULL)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004745 {
John Bowlerc10930a2013-12-19 15:24:06 -06004746 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
4747 * error is incredibly rare and incredibly easy to debug without this
4748 * information.
4749 */
4750 png_error(png_ptr, "NULL row buffer");
4751 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004752
John Bowlerc10930a2013-12-19 15:24:06 -06004753 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
4754 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
4755 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
4756 * all transformations, however in practice the ROW_INIT always gets done on
4757 * demand, if necessary.
4758 */
4759 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004760 (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004761 {
4762 /* Application has failed to call either png_read_start_image() or
4763 * png_read_update_info() after setting transforms that expand pixels.
4764 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
4765 */
4766 png_error(png_ptr, "Uninitialized row");
4767 }
4768
4769#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004770 if ((png_ptr->transformations & PNG_EXPAND) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004771 {
4772 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004773 {
Cosmin Trutaa627bd22019-04-07 19:50:12 -04004774#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
4775 if ((png_ptr->num_trans > 0) && (png_ptr->bit_depth == 8))
4776 {
4777 if (png_ptr->riffled_palette == NULL)
4778 {
4779 /* Initialize the accelerated palette expansion. */
4780 png_ptr->riffled_palette =
4781 (png_bytep)png_malloc(png_ptr, 256 * 4);
4782 png_riffle_palette_neon(png_ptr);
4783 }
4784 }
4785#endif
Richard Townsend7734cda2018-01-25 19:03:04 +00004786 png_do_expand_palette(png_ptr, row_info, png_ptr->row_buf + 1,
Cosmin Truta70d122a2019-02-03 19:51:18 -05004787 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004788 }
John Bowlerc10930a2013-12-19 15:24:06 -06004789
4790 else
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004791 {
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004792 if (png_ptr->num_trans != 0 &&
4793 (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004794 png_do_expand(row_info, png_ptr->row_buf + 1,
4795 &(png_ptr->trans_color));
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004796
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004797 else
Glenn Randers-Pehrson0e06b592018-03-06 13:54:52 -06004798 png_do_expand(row_info, png_ptr->row_buf + 1, NULL);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004799 }
4800 }
John Bowlerc10930a2013-12-19 15:24:06 -06004801#endif
4802
4803#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004804 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4805 (png_ptr->transformations & PNG_COMPOSE) == 0 &&
4806 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4807 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
John Bowlerc10930a2013-12-19 15:24:06 -06004808 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsondd706042016-07-15 11:20:46 -05004809 0 /* at_start == false, because SWAP_ALPHA happens later */);
John Bowlerc10930a2013-12-19 15:24:06 -06004810#endif
4811
4812#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004813 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004814 {
4815 int rgb_error =
4816 png_do_rgb_to_gray(png_ptr, row_info,
4817 png_ptr->row_buf + 1);
4818
Glenn Randers-Pehrson05670152014-03-08 12:39:52 -06004819 if (rgb_error != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004820 {
4821 png_ptr->rgb_to_gray_status=1;
4822 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4823 PNG_RGB_TO_GRAY_WARN)
4824 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4825
4826 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4827 PNG_RGB_TO_GRAY_ERR)
4828 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4829 }
4830 }
4831#endif
4832
4833/* From Andreas Dilger e-mail to png-implement, 26 March 1998:
4834 *
4835 * In most cases, the "simple transparency" should be done prior to doing
4836 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
4837 * pixel is transparent. You would also need to make sure that the
4838 * transparency information is upgraded to RGB.
4839 *
4840 * To summarize, the current flow is:
4841 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
4842 * with background "in place" if transparent,
4843 * convert to RGB if necessary
4844 * - Gray + alpha -> composite with gray background and remove alpha bytes,
4845 * convert to RGB if necessary
4846 *
4847 * To support RGB backgrounds for gray images we need:
4848 * - Gray + simple transparency -> convert to RGB + simple transparency,
4849 * compare 3 or 6 bytes and composite with
4850 * background "in place" if transparent
4851 * (3x compare/pixel compared to doing
4852 * composite with gray bkgrnd)
4853 * - Gray + alpha -> convert to RGB + alpha, composite with background and
4854 * remove alpha bytes (3x float
4855 * operations/pixel compared with composite
4856 * on gray background)
4857 *
4858 * Greg's change will do this. The reason it wasn't done before is for
4859 * performance, as this increases the per-pixel operations. If we would check
4860 * in advance if the background was gray or RGB, and position the gray-to-RGB
4861 * transform appropriately, then it would save a lot of work/time.
4862 */
4863
4864#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4865 /* If gray -> RGB, do so now only if background is non-gray; else do later
4866 * for performance reasons
4867 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004868 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4869 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004870 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4871#endif
4872
4873#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4874 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004875 if ((png_ptr->transformations & PNG_COMPOSE) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004876 png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
4877#endif
4878
4879#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004880 if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
John Bowlerc10930a2013-12-19 15:24:06 -06004881#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4882 /* Because RGB_TO_GRAY does the gamma transform. */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004883 (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
John Bowlerc10930a2013-12-19 15:24:06 -06004884#endif
4885#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4886 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4887 /* Because PNG_COMPOSE does the gamma transform if there is something to
4888 * do (if there is an alpha channel or transparency.)
4889 */
John Bowlercde8cd62014-12-29 16:24:33 -06004890 !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
John Bowlerc10930a2013-12-19 15:24:06 -06004891 ((png_ptr->num_trans != 0) ||
Glenn Randers-Pehrsonb963fee2014-11-01 13:18:36 -05004892 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
John Bowlerc10930a2013-12-19 15:24:06 -06004893#endif
4894 /* Because png_init_read_transformations transforms the palette, unless
4895 * RGB_TO_GRAY will do the transform.
4896 */
4897 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
4898 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
4899#endif
4900
4901#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004902 if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4903 (png_ptr->transformations & PNG_COMPOSE) != 0 &&
4904 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4905 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
John Bowlerc10930a2013-12-19 15:24:06 -06004906 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004907 0 /* at_start == false, because SWAP_ALPHA happens later */);
John Bowlerc10930a2013-12-19 15:24:06 -06004908#endif
4909
4910#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004911 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
4912 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004913 png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
4914#endif
4915
4916#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004917 if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004918 png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
4919#endif
4920
4921#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
4922 /* There is no harm in doing both of these because only one has any effect,
4923 * by putting the 'scale' option first if the app asks for scale (either by
4924 * calling the API or in a TRANSFORM flag) this is what happens.
4925 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004926 if ((png_ptr->transformations & PNG_16_TO_8) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004927 png_do_chop(row_info, png_ptr->row_buf + 1);
4928#endif
4929
4930#ifdef PNG_READ_QUANTIZE_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004931 if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004932 {
4933 png_do_quantize(row_info, png_ptr->row_buf + 1,
4934 png_ptr->palette_lookup, png_ptr->quantize_index);
4935
4936 if (row_info->rowbytes == 0)
4937 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
4938 }
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06004939#endif /* READ_QUANTIZE */
John Bowlerc10930a2013-12-19 15:24:06 -06004940
4941#ifdef PNG_READ_EXPAND_16_SUPPORTED
4942 /* Do the expansion now, after all the arithmetic has been done. Notice
4943 * that previous transformations can handle the PNG_EXPAND_16 flag if this
4944 * is efficient (particularly true in the case of gamma correction, where
4945 * better accuracy results faster!)
4946 */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004947 if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004948 png_do_expand_16(row_info, png_ptr->row_buf + 1);
4949#endif
4950
4951#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4952 /* NOTE: moved here in 1.5.4 (from much later in this list.) */
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004953 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4954 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004955 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4956#endif
4957
4958#ifdef PNG_READ_INVERT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004959 if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004960 png_do_invert(row_info, png_ptr->row_buf + 1);
4961#endif
4962
John Bowler414d7b52014-02-06 11:32:57 -06004963#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004964 if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
John Bowler414d7b52014-02-06 11:32:57 -06004965 png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
4966#endif
4967
John Bowlerc10930a2013-12-19 15:24:06 -06004968#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004969 if ((png_ptr->transformations & PNG_SHIFT) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004970 png_do_unshift(row_info, png_ptr->row_buf + 1,
4971 &(png_ptr->shift));
4972#endif
4973
4974#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004975 if ((png_ptr->transformations & PNG_PACK) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004976 png_do_unpack(row_info, png_ptr->row_buf + 1);
4977#endif
4978
4979#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
4980 /* Added at libpng-1.5.10 */
4981 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4982 png_ptr->num_palette_max >= 0)
4983 png_do_check_palette_indexes(png_ptr, row_info);
4984#endif
4985
4986#ifdef PNG_READ_BGR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004987 if ((png_ptr->transformations & PNG_BGR) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004988 png_do_bgr(row_info, png_ptr->row_buf + 1);
4989#endif
4990
4991#ifdef PNG_READ_PACKSWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004992 if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004993 png_do_packswap(row_info, png_ptr->row_buf + 1);
4994#endif
4995
4996#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05004997 if ((png_ptr->transformations & PNG_FILLER) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06004998 png_do_read_filler(row_info, png_ptr->row_buf + 1,
4999 (png_uint_32)png_ptr->filler, png_ptr->flags);
5000#endif
5001
John Bowlerc10930a2013-12-19 15:24:06 -06005002#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05005003 if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06005004 png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
5005#endif
5006
5007#ifdef PNG_READ_16BIT_SUPPORTED
5008#ifdef PNG_READ_SWAP_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05005009 if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06005010 png_do_swap(row_info, png_ptr->row_buf + 1);
5011#endif
5012#endif
5013
5014#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05005015 if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
Glenn Randers-Pehrson70cb8f92014-11-06 20:58:33 -06005016 {
John Bowlerc10930a2013-12-19 15:24:06 -06005017 if (png_ptr->read_user_transform_fn != NULL)
5018 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
5019 (png_ptr, /* png_ptr */
5020 row_info, /* row_info: */
5021 /* png_uint_32 width; width of row */
Cosmin Trutaa74aa9a2018-06-17 22:37:44 -04005022 /* size_t rowbytes; number of bytes in row */
John Bowlerc10930a2013-12-19 15:24:06 -06005023 /* png_byte color_type; color type of pixels */
5024 /* png_byte bit_depth; bit depth of samples */
5025 /* png_byte channels; number of channels (1-4) */
5026 /* png_byte pixel_depth; bits per pixel (depth*channels) */
5027 png_ptr->row_buf + 1); /* start of pixel data for row */
5028#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05005029 if (png_ptr->user_transform_depth != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06005030 row_info->bit_depth = png_ptr->user_transform_depth;
5031
Glenn Randers-Pehrson5d713fe2014-10-31 20:48:55 -05005032 if (png_ptr->user_transform_channels != 0)
John Bowlerc10930a2013-12-19 15:24:06 -06005033 row_info->channels = png_ptr->user_transform_channels;
5034#endif
Glenn Randers-Pehrsonc5370ed2015-03-21 11:54:32 -05005035 row_info->pixel_depth = (png_byte)(row_info->bit_depth *
5036 row_info->channels);
John Bowlerc10930a2013-12-19 15:24:06 -06005037
5038 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
5039 }
5040#endif
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06005041}
John Bowlerc10930a2013-12-19 15:24:06 -06005042
Glenn Randers-Pehrsoncda68df2014-11-06 22:11:39 -06005043#endif /* READ_TRANSFORMS */
5044#endif /* READ */