blob: 737970011f27dd5c75fd1245eb10da1d012f79e6 [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 *
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05004 * Last changed in libpng 1.5.6 [(PENDING RELEASE)]
Glenn Randers-Pehrson64b863c2011-01-04 09:57:06 -06005 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
Glenn Randers-Pehrsond4366722000-06-04 14:29:29 -05006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 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
Glenn Randers-Pehrsonc3cd22b2010-03-08 21:10:25 -060021#ifdef PNG_READ_SUPPORTED
22
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060023/* Set the action on getting a CRC error for an ancillary or critical chunk. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -050024void PNGAPI
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060025png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
26{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -050027 png_debug(1, "in png_set_crc_action");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050028
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050029 if (png_ptr == NULL)
30 return;
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050031
32 /* Tell libpng how we react to CRC errors in critical chunks */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060033 switch (crit_action)
34 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050035 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060036 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050037
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050038 case PNG_CRC_WARN_USE: /* Warn/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060039 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
40 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
41 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050042
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050043 case PNG_CRC_QUIET_USE: /* Quiet/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060044 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
45 png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
46 PNG_FLAG_CRC_CRITICAL_IGNORE;
47 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_DISCARD: /* Not a valid action for critical data */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -050050 png_warning(png_ptr,
51 "Can't discard critical data on CRC error");
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050052 case PNG_CRC_ERROR_QUIT: /* Error/quit */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050053
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060054 case PNG_CRC_DEFAULT:
55 default:
56 png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
57 break;
58 }
59
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -050060 /* Tell libpng how we react to CRC errors in ancillary chunks */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060061 switch (ancil_action)
62 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050063 case PNG_CRC_NO_CHANGE: /* Leave setting as is */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060064 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050065
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050066 case PNG_CRC_WARN_USE: /* Warn/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060067 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
68 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
69 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050070
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050071 case PNG_CRC_QUIET_USE: /* Quiet/use data */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060072 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
73 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
74 PNG_FLAG_CRC_ANCILLARY_NOWARN;
75 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050076
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050077 case PNG_CRC_ERROR_QUIT: /* Error/quit */
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060078 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
79 png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
80 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050081
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050082 case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -050083
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060084 case PNG_CRC_DEFAULT:
85 default:
86 png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
87 break;
88 }
89}
90
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050091#ifdef PNG_READ_BACKGROUND_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -050092/* Handle alpha and tRNS via a background color */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050093void PNGFAPI
94png_set_background_fixed(png_structp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -050095 png_const_color_16p background_color, int background_gamma_code,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050096 int need_expand, png_fixed_point background_gamma)
Guy Schalnat0d580581995-07-20 02:43:20 -050097{
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -050098 png_debug(1, "in png_set_background_fixed");
Glenn Randers-Pehrson821b7102010-06-24 16:16:32 -050099
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500100 if (png_ptr == NULL)
101 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500102
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600103 if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
104 {
105 png_warning(png_ptr, "Application must supply a known background gamma");
106 return;
107 }
108
John Bowlerd273ad22011-05-07 21:00:28 -0500109 png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
110 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
111 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
112
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500113 png_memcpy(&(png_ptr->background), background_color,
114 png_sizeof(png_color_16));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500115 png_ptr->background_gamma = background_gamma;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600116 png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
John Bowlerd273ad22011-05-07 21:00:28 -0500117 if (need_expand)
118 png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
119 else
120 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
Guy Schalnat0d580581995-07-20 02:43:20 -0500121}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500122
123# ifdef PNG_FLOATING_POINT_SUPPORTED
124void PNGAPI
125png_set_background(png_structp png_ptr,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500126 png_const_color_16p background_color, int background_gamma_code,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500127 int need_expand, double background_gamma)
128{
129 png_set_background_fixed(png_ptr, background_color, background_gamma_code,
130 need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
131}
132# endif /* FLOATING_POINT */
133#endif /* READ_BACKGROUND */
Guy Schalnat0d580581995-07-20 02:43:20 -0500134
John Bowler8d261262011-06-18 13:37:11 -0500135/* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
136 * one that pngrtran does first (scale) happens. This is necessary to allow the
137 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
138 */
139#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500140void PNGAPI
Glenn Randers-Pehrsona7428d42011-06-17 19:07:04 -0500141png_set_scale_16(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500142{
Glenn Randers-Pehrsona7428d42011-06-17 19:07:04 -0500143 png_debug(1, "in png_set_scale_16");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500144
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500145 if (png_ptr == NULL)
146 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500147
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500148 png_ptr->transformations |= PNG_SCALE_16_TO_8;
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500149}
John Bowler8d261262011-06-18 13:37:11 -0500150#endif
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500151
John Bowler8d261262011-06-18 13:37:11 -0500152#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500153/* Chop 16-bit depth files to 8-bit depth */
154void PNGAPI
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500155png_set_strip_16(png_structp png_ptr)
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500156{
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500157 png_debug(1, "in png_set_strip_16");
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -0500158
159 if (png_ptr == NULL)
160 return;
161
Glenn Randers-Pehrsonab63dd02011-06-17 20:04:17 -0500162 png_ptr->transformations |= PNG_16_TO_8;
Guy Schalnat0d580581995-07-20 02:43:20 -0500163}
John Bowler8d261262011-06-18 13:37:11 -0500164#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500165
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500166#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500167void PNGAPI
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168png_set_strip_alpha(png_structp png_ptr)
169{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500170 png_debug(1, "in png_set_strip_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500171
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500172 if (png_ptr == NULL)
173 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500174
John Bowler9b872f42011-02-12 09:00:16 -0600175 png_ptr->transformations |= PNG_STRIP_ALPHA;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500176}
177#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500178
John Bowlerf70c7d02011-05-10 22:54:37 -0500179#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
180static png_fixed_point
181translate_gamma_flags(png_structp png_ptr, png_fixed_point output_gamma,
182 int is_screen)
183{
184 /* Check for flag values. The main reason for having the old Mac value as a
185 * flag is that it is pretty near impossible to work out what the correct
186 * value is from Apple documentation - a working Mac system is needed to
187 * discover the value!
188 */
189 if (output_gamma == PNG_DEFAULT_sRGB ||
190 output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
191 {
192 /* If there is no sRGB support this just sets the gamma to the standard
193 * sRGB value. (This is a side effect of using this function!)
194 */
195# ifdef PNG_READ_sRGB_SUPPORTED
196 png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
197# endif
198 if (is_screen)
199 output_gamma = PNG_GAMMA_sRGB;
200 else
201 output_gamma = PNG_GAMMA_sRGB_INVERSE;
202 }
203
204 else if (output_gamma == PNG_GAMMA_MAC_18 ||
205 output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
206 {
207 if (is_screen)
208 output_gamma = PNG_GAMMA_MAC_OLD;
209 else
210 output_gamma = PNG_GAMMA_MAC_INVERSE;
211 }
212
213 return output_gamma;
214}
215
216# ifdef PNG_FLOATING_POINT_SUPPORTED
217static png_fixed_point
218convert_gamma_value(png_structp png_ptr, double output_gamma)
219{
220 /* The following silently ignores cases where fixed point (times 100,000)
221 * gamma values are passed to the floating point API. This is safe and it
222 * means the fixed point constants work just fine with the floating point
223 * API. The alternative would just lead to undetected errors and spurious
224 * bug reports. Negative values fail inside the _fixed API unless they
225 * correspond to the flag values.
226 */
227 if (output_gamma > 0 && output_gamma < 128)
228 output_gamma *= PNG_FP_1;
229
230 /* This preserves -1 and -2 exactly: */
231 output_gamma = floor(output_gamma + .5);
232
233 if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
234 png_fixed_error(png_ptr, "gamma value");
235
236 return (png_fixed_point)output_gamma;
237}
238# endif
239#endif /* READ_ALPHA_MODE || READ_GAMMA */
240
John Bowlerd273ad22011-05-07 21:00:28 -0500241#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
242void PNGFAPI
243png_set_alpha_mode_fixed(png_structp png_ptr, int mode,
244 png_fixed_point output_gamma)
245{
246 int compose = 0;
247 png_fixed_point file_gamma;
248
249 png_debug(1, "in png_set_alpha_mode");
250
251 if (png_ptr == NULL)
252 return;
253
John Bowlerf70c7d02011-05-10 22:54:37 -0500254 output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
John Bowlerd273ad22011-05-07 21:00:28 -0500255
John Bowlerf70c7d02011-05-10 22:54:37 -0500256 /* Validate the value to ensure it is in a reasonable range. The value
John Bowlerd273ad22011-05-07 21:00:28 -0500257 * is expected to be 1 or greater, but this range test allows for some
258 * viewing correction values. The intent is to weed out users of this API
259 * who use the inverse of the gamma value accidentally! Since some of these
260 * values are reasonable this may have to be changed.
261 */
John Bowlerf70c7d02011-05-10 22:54:37 -0500262 if (output_gamma < 70000 || output_gamma > 300000)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500263 png_error(png_ptr, "output gamma out of expected range");
John Bowlerd273ad22011-05-07 21:00:28 -0500264
Glenn Randers-Pehrsonc7822512011-05-07 21:23:43 -0500265 /* The default file gamma is the inverse of the output gamma; the output
John Bowlerd273ad22011-05-07 21:00:28 -0500266 * gamma may be changed below so get the file value first:
267 */
268 file_gamma = png_reciprocal(output_gamma);
269
270 /* There are really 8 possibilities here, composed of any combination
271 * of:
272 *
273 * premultiply the color channels
274 * do not encode non-opaque pixels
275 * encode the alpha as well as the color channels
276 *
277 * The differences disappear if the input/output ('screen') gamma is 1.0,
278 * because then the encoding is a no-op and there is only the choice of
279 * premultiplying the color channels or not.
280 *
281 * png_set_alpha_mode and png_set_background interact because both use
282 * png_compose to do the work. Calling both is only useful when
283 * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
284 * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
285 */
286 switch (mode)
287 {
288 case PNG_ALPHA_PNG: /* default: png standard */
289 /* No compose, but it may be set by png_set_background! */
290 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
291 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
292 break;
293
294 case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
295 compose = 1;
296 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
297 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
298 /* The output is linear: */
299 output_gamma = PNG_FP_1;
300 break;
301
302 case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
303 compose = 1;
304 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
305 png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
306 /* output_gamma records the encoding of opaque pixels! */
307 break;
308
309 case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
310 compose = 1;
311 png_ptr->transformations |= PNG_ENCODE_ALPHA;
312 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
313 break;
314
315 default:
316 png_error(png_ptr, "invalid alpha mode");
317 }
318
319 /* Only set the default gamma if the file gamma has not been set (this has
320 * the side effect that the gamma in a second call to png_set_alpha_mode will
321 * be ignored.)
322 */
323 if (png_ptr->gamma == 0)
324 png_ptr->gamma = file_gamma;
325
326 /* But always set the output gamma: */
327 png_ptr->screen_gamma = output_gamma;
328
Glenn Randers-Pehrsonc7822512011-05-07 21:23:43 -0500329 /* Finally, if pre-multiplying, set the background fields to achieve the
John Bowlerd273ad22011-05-07 21:00:28 -0500330 * desired result.
331 */
332 if (compose)
333 {
334 /* And obtain alpha pre-multiplication by composing on black: */
335 png_memset(&png_ptr->background, 0, sizeof png_ptr->background);
336 png_ptr->background_gamma = png_ptr->gamma; /* just in case */
337 png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
338 png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
339
340 if (png_ptr->transformations & PNG_COMPOSE)
341 png_error(png_ptr,
342 "conflicting calls to set alpha mode and background");
343
344 png_ptr->transformations |= PNG_COMPOSE;
345 }
346
347 /* New API, make sure apps call the correct initializers: */
348 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
349}
350
351# ifdef PNG_FLOATING_POINT_SUPPORTED
352void PNGAPI
353png_set_alpha_mode(png_structp png_ptr, int mode, double output_gamma)
354{
John Bowlerf70c7d02011-05-10 22:54:37 -0500355 png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
356 output_gamma));
John Bowlerd273ad22011-05-07 21:00:28 -0500357}
358# endif
359#endif
360
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500361#ifdef PNG_READ_QUANTIZE_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500362/* Dither file to 8-bit. Supply a palette, the current number
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600363 * of elements in the palette, the maximum number of elements
364 * allowed, and a histogram if possible. If the current number
365 * of colors is greater then the maximum number, the palette will be
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500366 * modified to fit in the maximum number. "full_quantize" indicates
367 * whether we need a quantizing cube set up for RGB images, or if we
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600368 * simply are reducing the number of colors in a paletted image.
369 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600370
371typedef struct png_dsort_struct
Guy Schalnat0d580581995-07-20 02:43:20 -0500372{
Guy Schalnat6d764711995-12-19 03:22:19 -0600373 struct png_dsort_struct FAR * next;
Guy Schalnat0d580581995-07-20 02:43:20 -0500374 png_byte left;
375 png_byte right;
Guy Schalnat6d764711995-12-19 03:22:19 -0600376} png_dsort;
377typedef png_dsort FAR * png_dsortp;
378typedef png_dsort FAR * FAR * png_dsortpp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500379
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500380void PNGAPI
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500381png_set_quantize(png_structp png_ptr, png_colorp palette,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -0500382 int num_palette, int maximum_colors, png_const_uint_16p histogram,
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500383 int full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500384{
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500385 png_debug(1, "in png_set_quantize");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500386
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500387 if (png_ptr == NULL)
388 return;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500389
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500390 png_ptr->transformations |= PNG_QUANTIZE;
Guy Schalnat0d580581995-07-20 02:43:20 -0500391
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500392 if (!full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500393 {
394 int i;
395
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500396 png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600397 (png_uint_32)(num_palette * png_sizeof(png_byte)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500398 for (i = 0; i < num_palette; i++)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500399 png_ptr->quantize_index[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 }
401
402 if (num_palette > maximum_colors)
403 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500404 if (histogram != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500406 /* This is easy enough, just throw out the least used colors.
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500407 * Perhaps not the best solution, but good enough.
408 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500409
410 int i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500411
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500412 /* Initialize an array to sort colors */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500413 png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600414 (png_uint_32)(num_palette * png_sizeof(png_byte)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500415
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500416 /* Initialize the quantize_sort array */
Guy Schalnat0d580581995-07-20 02:43:20 -0500417 for (i = 0; i < num_palette; i++)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500418 png_ptr->quantize_sort[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500419
Andreas Dilger47a0c421997-05-16 02:46:07 -0500420 /* Find the least used palette entries by starting a
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500421 * bubble sort, and running it until we have sorted
422 * out enough colors. Note that we don't care about
423 * sorting all the colors, just finding which are
424 * least used.
425 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500426
427 for (i = num_palette - 1; i >= maximum_colors; i--)
428 {
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -0500429 int done; /* To stop early if the list is pre-sorted */
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600430 int j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500431
432 done = 1;
433 for (j = 0; j < i; j++)
434 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500435 if (histogram[png_ptr->quantize_sort[j]]
436 < histogram[png_ptr->quantize_sort[j + 1]])
Guy Schalnat0d580581995-07-20 02:43:20 -0500437 {
438 png_byte t;
439
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500440 t = png_ptr->quantize_sort[j];
441 png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
442 png_ptr->quantize_sort[j + 1] = t;
Guy Schalnat0d580581995-07-20 02:43:20 -0500443 done = 0;
444 }
445 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500446
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 if (done)
448 break;
449 }
450
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500451 /* Swap the palette around, and set up a table, if necessary */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500452 if (full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500453 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500454 int j = num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500455
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500456 /* Put all the useful colors within the max, but don't
457 * move the others.
458 */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500459 for (i = 0; i < maximum_colors; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500460 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500461 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500462 {
463 do
464 j--;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500465 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500466
Guy Schalnat0d580581995-07-20 02:43:20 -0500467 palette[i] = palette[j];
468 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600469 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500470 }
471 else
472 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500473 int j = num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500474
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500475 /* Move all the used colors inside the max limit, and
476 * develop a translation table.
477 */
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500478 for (i = 0; i < maximum_colors; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500479 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500480 /* Only move the colors we need to */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500481 if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500482 {
483 png_color tmp_color;
484
485 do
486 j--;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500487 while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
Guy Schalnat0d580581995-07-20 02:43:20 -0500488
489 tmp_color = palette[j];
490 palette[j] = palette[i];
491 palette[i] = tmp_color;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500492 /* Indicate where the color went */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500493 png_ptr->quantize_index[j] = (png_byte)i;
494 png_ptr->quantize_index[i] = (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 }
496 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500497
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500498 /* Find closest color for those colors we are not using */
Guy Schalnat0d580581995-07-20 02:43:20 -0500499 for (i = 0; i < num_palette; i++)
500 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500501 if ((int)png_ptr->quantize_index[i] >= maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500502 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 int min_d, k, min_k, d_index;
Guy Schalnat0d580581995-07-20 02:43:20 -0500504
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500505 /* Find the closest color to one we threw out */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500506 d_index = png_ptr->quantize_index[i];
Andreas Dilger47a0c421997-05-16 02:46:07 -0500507 min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
508 for (k = 1, min_k = 0; k < maximum_colors; k++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 {
510 int d;
511
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512 d = PNG_COLOR_DIST(palette[d_index], palette[k]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500513
514 if (d < min_d)
515 {
516 min_d = d;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500517 min_k = k;
Guy Schalnat0d580581995-07-20 02:43:20 -0500518 }
519 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500520 /* Point to closest color */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500521 png_ptr->quantize_index[i] = (png_byte)min_k;
Guy Schalnat0d580581995-07-20 02:43:20 -0500522 }
523 }
524 }
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500525 png_free(png_ptr, png_ptr->quantize_sort);
526 png_ptr->quantize_sort = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 }
528 else
529 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500530 /* This is much harder to do simply (and quickly). Perhaps
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500531 * we need to go through a median cut routine, but those
532 * don't always behave themselves with only a few colors
533 * as input. So we will just find the closest two colors,
534 * and throw out one of them (chosen somewhat randomly).
535 * [We don't understand this at all, so if someone wants to
536 * work on improving it, be our guest - AED, GRP]
537 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500538 int i;
539 int max_d;
540 int num_new_palette;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500541 png_dsortp t;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600542 png_dsortpp hash;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500543
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500544 t = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500545
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500546 /* Initialize palette index arrays */
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500547 png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600548 (png_uint_32)(num_palette * png_sizeof(png_byte)));
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500549 png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600550 (png_uint_32)(num_palette * png_sizeof(png_byte)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500551
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500552 /* Initialize the sort array */
Guy Schalnat0d580581995-07-20 02:43:20 -0500553 for (i = 0; i < num_palette; i++)
554 {
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500555 png_ptr->index_to_palette[i] = (png_byte)i;
556 png_ptr->palette_to_index[i] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500557 }
558
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -0600559 hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600560 png_sizeof(png_dsortp)));
Guy Schalnat0d580581995-07-20 02:43:20 -0500561
562 num_new_palette = num_palette;
563
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500564 /* Initial wild guess at how far apart the farthest pixel
565 * pair we will be eliminating will be. Larger
566 * numbers mean more areas will be allocated, Smaller
567 * numbers run the risk of not saving enough data, and
568 * having to do this all over again.
569 *
570 * I have not done extensive checking on this number.
571 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500572 max_d = 96;
573
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600574 while (num_new_palette > maximum_colors)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 {
576 for (i = 0; i < num_new_palette - 1; i++)
577 {
578 int j;
579
580 for (j = i + 1; j < num_new_palette; j++)
581 {
582 int d;
583
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600584 d = PNG_COLOR_DIST(palette[i], palette[j]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500585
586 if (d <= max_d)
587 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500589 t = (png_dsortp)png_malloc_warn(png_ptr,
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500590 (png_uint_32)(png_sizeof(png_dsort)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500591
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500592 if (t == NULL)
593 break;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500594
Guy Schalnat0d580581995-07-20 02:43:20 -0500595 t->next = hash[d];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600596 t->left = (png_byte)i;
597 t->right = (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 hash[d] = t;
599 }
600 }
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500601 if (t == NULL)
602 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500603 }
604
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500605 if (t != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500606 for (i = 0; i <= max_d; i++)
607 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500608 if (hash[i] != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500609 {
Guy Schalnat6d764711995-12-19 03:22:19 -0600610 png_dsortp p;
Guy Schalnat0d580581995-07-20 02:43:20 -0500611
612 for (p = hash[i]; p; p = p->next)
613 {
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500614 if ((int)png_ptr->index_to_palette[p->left]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600615 < num_new_palette &&
616 (int)png_ptr->index_to_palette[p->right]
617 < num_new_palette)
Guy Schalnat0d580581995-07-20 02:43:20 -0500618 {
619 int j, next_j;
620
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600621 if (num_new_palette & 0x01)
Guy Schalnat0d580581995-07-20 02:43:20 -0500622 {
623 j = p->left;
624 next_j = p->right;
625 }
626 else
627 {
628 j = p->right;
629 next_j = p->left;
630 }
631
632 num_new_palette--;
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500633 palette[png_ptr->index_to_palette[j]]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600634 = palette[num_new_palette];
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500635 if (!full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500636 {
637 int k;
638
639 for (k = 0; k < num_palette; k++)
640 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500641 if (png_ptr->quantize_index[k] ==
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600642 png_ptr->index_to_palette[j])
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500643 png_ptr->quantize_index[k] =
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600644 png_ptr->index_to_palette[next_j];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500645
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500646 if ((int)png_ptr->quantize_index[k] ==
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600647 num_new_palette)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500648 png_ptr->quantize_index[k] =
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600649 png_ptr->index_to_palette[j];
Guy Schalnat0d580581995-07-20 02:43:20 -0500650 }
651 }
652
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500653 png_ptr->index_to_palette[png_ptr->palette_to_index
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600654 [num_new_palette]] = png_ptr->index_to_palette[j];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500655
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500656 png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600657 = png_ptr->palette_to_index[num_new_palette];
Guy Schalnat0d580581995-07-20 02:43:20 -0500658
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600659 png_ptr->index_to_palette[j] =
660 (png_byte)num_new_palette;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500661
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -0600662 png_ptr->palette_to_index[num_new_palette] =
663 (png_byte)j;
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 }
665 if (num_new_palette <= maximum_colors)
666 break;
667 }
668 if (num_new_palette <= maximum_colors)
669 break;
670 }
671 }
672
673 for (i = 0; i < 769; i++)
674 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500675 if (hash[i] != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500676 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500677 png_dsortp p = hash[i];
Guy Schalnat0d580581995-07-20 02:43:20 -0500678 while (p)
679 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500680 t = p->next;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600681 png_free(png_ptr, p);
Guy Schalnat0d580581995-07-20 02:43:20 -0500682 p = t;
683 }
684 }
685 hash[i] = 0;
686 }
687 max_d += 96;
688 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600689 png_free(png_ptr, hash);
Glenn Randers-Pehrson07748d12002-05-25 11:12:10 -0500690 png_free(png_ptr, png_ptr->palette_to_index);
691 png_free(png_ptr, png_ptr->index_to_palette);
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -0500692 png_ptr->palette_to_index = NULL;
693 png_ptr->index_to_palette = NULL;
Guy Schalnat0d580581995-07-20 02:43:20 -0500694 }
695 num_palette = maximum_colors;
696 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500697 if (png_ptr->palette == NULL)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600698 {
Guy Schalnat0d580581995-07-20 02:43:20 -0500699 png_ptr->palette = palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500700 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600701 png_ptr->num_palette = (png_uint_16)num_palette;
Guy Schalnat0d580581995-07-20 02:43:20 -0500702
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500703 if (full_quantize)
Guy Schalnat0d580581995-07-20 02:43:20 -0500704 {
705 int i;
Guy Schalnat6d764711995-12-19 03:22:19 -0600706 png_bytep distance;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500707 int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
708 PNG_QUANTIZE_BLUE_BITS;
709 int num_red = (1 << PNG_QUANTIZE_RED_BITS);
710 int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
711 int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500712 png_size_t num_entries = ((png_size_t)1 << total_bits);
Guy Schalnat0d580581995-07-20 02:43:20 -0500713
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -0500714 png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600715 (png_uint_32)(num_entries * png_sizeof(png_byte)));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500716
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500717 distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600718 png_sizeof(png_byte)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500719
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500720 png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500721
722 for (i = 0; i < num_palette; i++)
723 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500724 int ir, ig, ib;
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500725 int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
726 int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
727 int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
Guy Schalnat0d580581995-07-20 02:43:20 -0500728
729 for (ir = 0; ir < num_red; ir++)
730 {
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500731 /* int dr = abs(ir - r); */
732 int dr = ((ir > r) ? ir - r : r - ir);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500733 int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
734 PNG_QUANTIZE_GREEN_BITS));
Guy Schalnat0d580581995-07-20 02:43:20 -0500735
Guy Schalnat0d580581995-07-20 02:43:20 -0500736 for (ig = 0; ig < num_green; ig++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600737 {
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500738 /* int dg = abs(ig - g); */
739 int dg = ((ig > g) ? ig - g : g - ig);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500740 int dt = dr + dg;
741 int dm = ((dr > dg) ? dr : dg);
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500742 int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
Guy Schalnat0d580581995-07-20 02:43:20 -0500743
Guy Schalnat0d580581995-07-20 02:43:20 -0500744 for (ib = 0; ib < num_blue; ib++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -0600745 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500746 int d_index = index_g | ib;
Glenn Randers-Pehrsond029a752004-08-09 21:50:32 -0500747 /* int db = abs(ib - b); */
748 int db = ((ib > b) ? ib - b : b - ib);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500749 int dmax = ((dm > db) ? dm : db);
750 int d = dmax + dt + db;
Guy Schalnat0d580581995-07-20 02:43:20 -0500751
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600752 if (d < (int)distance[d_index])
Guy Schalnat0d580581995-07-20 02:43:20 -0500753 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500754 distance[d_index] = (png_byte)d;
755 png_ptr->palette_lookup[d_index] = (png_byte)i;
Guy Schalnat0d580581995-07-20 02:43:20 -0500756 }
757 }
Glenn Randers-Pehrson82ae3832001-04-20 10:32:10 -0500758 }
759 }
760 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500761
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600762 png_free(png_ptr, distance);
Guy Schalnat0d580581995-07-20 02:43:20 -0500763 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500764}
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -0500765#endif /* PNG_READ_QUANTIZE_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -0500766
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500767#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500768void PNGFAPI
769png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
770 png_fixed_point file_gamma)
771{
772 png_debug(1, "in png_set_gamma_fixed");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500773
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500774 if (png_ptr == NULL)
775 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500776
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500777 /* New in libpng-1.5.4 - reserve particular negative values as flags. */
John Bowlerf70c7d02011-05-10 22:54:37 -0500778 scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
779 file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
780
John Bowlerd273ad22011-05-07 21:00:28 -0500781#if PNG_LIBPNG_VER >= 10600
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -0500782 /* Checking the gamma values for being >0 was added in 1.5.4 along with the
John Bowlerd273ad22011-05-07 21:00:28 -0500783 * premultiplied alpha support; this actually hides an undocumented feature
784 * of the previous implementation which allowed gamma processing to be
785 * disabled in background handling. There is no evidence (so far) that this
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500786 * was being used; however, png_set_background itself accepted and must still
John Bowlerd273ad22011-05-07 21:00:28 -0500787 * accept '0' for the gamma value it takes, because it isn't always used.
788 *
789 * Since this is an API change (albeit a very minor one that removes an
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500790 * undocumented API feature) it will not be made until libpng-1.6.0.
John Bowlerd273ad22011-05-07 21:00:28 -0500791 */
792 if (file_gamma <= 0)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500793 png_error(png_ptr, "invalid file gamma in png_set_gamma");
John Bowlerd273ad22011-05-07 21:00:28 -0500794
795 if (scrn_gamma <= 0)
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500796 png_error(png_ptr, "invalid screen gamma in png_set_gamma");
John Bowlerd273ad22011-05-07 21:00:28 -0500797#endif
798
799 /* Set the gamma values unconditionally - this overrides the value in the PNG
800 * file if a gAMA chunk was present. png_set_alpha_mode provides a
801 * different, easier, way to default the file gamma.
802 */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500803 png_ptr->gamma = file_gamma;
804 png_ptr->screen_gamma = scrn_gamma;
Guy Schalnat0d580581995-07-20 02:43:20 -0500805}
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500806
807# ifdef PNG_FLOATING_POINT_SUPPORTED
808void PNGAPI
809png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
810{
John Bowlerf70c7d02011-05-10 22:54:37 -0500811 png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
812 convert_gamma_value(png_ptr, file_gamma));
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500813}
814# endif /* FLOATING_POINT_SUPPORTED */
815#endif /* READ_GAMMA */
Guy Schalnat0d580581995-07-20 02:43:20 -0500816
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500817#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -0500818/* Expand paletted images to RGB, expand grayscale images of
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500819 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600820 * to alpha channels.
821 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500822void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600823png_set_expand(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500824{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500825 png_debug(1, "in png_set_expand");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500826
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500827 if (png_ptr == NULL)
828 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500829
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600830 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500831 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Guy Schalnat0d580581995-07-20 02:43:20 -0500832}
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500833
834/* GRR 19990627: the following three functions currently are identical
835 * to png_set_expand(). However, it is entirely reasonable that someone
836 * might wish to expand an indexed image to RGB but *not* expand a single,
837 * fully transparent palette entry to a full alpha channel--perhaps instead
838 * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
839 * the transparent color with a particular RGB value, or drop tRNS entirely.
840 * IOW, a future version of the library may make the transformations flag
841 * a bit more fine-grained, with separate bits for each of these three
842 * functions.
843 *
844 * More to the point, these functions make it obvious what libpng will be
845 * doing, whereas "expand" can (and does) mean any number of things.
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600846 *
Glenn Randers-Pehrson17ca3402009-11-09 06:51:16 -0600847 * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
848 * to expand only the sample depth but not to expand the tRNS to alpha
849 * and its name was changed to png_set_expand_gray_1_2_4_to_8().
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500850 */
851
852/* Expand paletted images to RGB. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500853void PNGAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500854png_set_palette_to_rgb(png_structp png_ptr)
855{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500856 png_debug(1, "in png_set_palette_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500857
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500858 if (png_ptr == NULL)
859 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500860
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600861 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500862 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500863}
864
865/* Expand grayscale images of less than 8-bit depth to 8 bits. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500866void PNGAPI
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600867png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
868{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500869 png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500870
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500871 if (png_ptr == NULL)
872 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500873
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500874 png_ptr->transformations |= PNG_EXPAND;
875 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600876}
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500877
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500878
879
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500880/* Expand tRNS chunks to alpha channels. */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500881void PNGAPI
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500882png_set_tRNS_to_alpha(png_structp png_ptr)
883{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500884 png_debug(1, "in png_set_tRNS_to_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500885
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -0600886 png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500887 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500888}
889#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
Guy Schalnat0d580581995-07-20 02:43:20 -0500890
John Bowler4d562962011-02-12 09:01:20 -0600891#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson55fbff32011-05-17 06:49:32 -0500892/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
John Bowler4d562962011-02-12 09:01:20 -0600893 * it may not work correctly.)
894 */
895void PNGAPI
896png_set_expand_16(png_structp png_ptr)
897{
898 png_debug(1, "in png_set_expand_16");
899
900 if (png_ptr == NULL)
901 return;
902
903 png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
John Bowler4d562962011-02-12 09:01:20 -0600904 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
John Bowlerd273ad22011-05-07 21:00:28 -0500905
906 /* New API, make sure apps call the correct initializers: */
907 png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
John Bowler4d562962011-02-12 09:01:20 -0600908}
909#endif
910
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500911#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -0500912void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -0600913png_set_gray_to_rgb(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500914{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500915 png_debug(1, "in png_set_gray_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500916
John Bowlerf21a0d02011-01-23 23:55:19 -0600917 if (png_ptr != NULL)
918 {
919 /* Because rgb must be 8 bits or more: */
920 png_set_expand_gray_1_2_4_to_8(png_ptr);
921 png_ptr->transformations |= PNG_GRAY_TO_RGB;
922 png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
923 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500924}
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500925#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500926
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500927#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -0500928void PNGFAPI
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600929png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600930 png_fixed_point red, png_fixed_point green)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600931{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -0500932 png_debug(1, "in png_set_rgb_to_gray");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -0500933
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500934 if (png_ptr == NULL)
935 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -0500936
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600937 switch(error_action)
938 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600939 case 1:
940 png_ptr->transformations |= PNG_RGB_TO_GRAY;
941 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500942
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600943 case 2:
944 png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
945 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -0500946
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -0600947 case 3:
948 png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -0600949 break;
950
951 default:
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -0500952 png_error(png_ptr, "invalid error action to rgb_to_gray");
953 break;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600954 }
955 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -0500956#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600957 png_ptr->transformations |= PNG_EXPAND;
958#else
959 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -0500960 png_warning(png_ptr,
961 "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500962
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600963 png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
964 }
965#endif
966 {
John Bowlercb0b2962011-05-12 21:48:29 -0500967 if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600968 {
John Bowlercb0b2962011-05-12 21:48:29 -0500969 png_uint_16 red_int, green_int;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500970
John Bowler736f40f2011-08-25 16:19:44 -0500971 /* NOTE: this calculation does not round, but this behavior is retained
972 * for consistency, the inaccuracy is very small. The code here always
973 * overwrites the coefficients, regardless of whether they have been
974 * defaulted or set already.
975 */
John Bowler75156122011-09-09 17:21:44 -0500976 red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
977 green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
John Bowlercb0b2962011-05-12 21:48:29 -0500978
979 png_ptr->rgb_to_gray_red_coeff = red_int;
980 png_ptr->rgb_to_gray_green_coeff = green_int;
John Bowler736f40f2011-08-25 16:19:44 -0500981 png_ptr->rgb_to_gray_coefficients_set = 1;
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600982 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500983
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -0600984 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600985 {
John Bowlercb0b2962011-05-12 21:48:29 -0500986 if (red >= 0 && green >= 0)
987 png_warning(png_ptr,
988 "ignoring out of range rgb_to_gray coefficients");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -0500989
John Bowler736f40f2011-08-25 16:19:44 -0500990 /* Use the defaults, from the cHRM chunk if set, else the historical
991 * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See
992 * png_do_rgb_to_gray for more discussion of the values. In this case
993 * the coefficients are not marked as 'set' and are not overwritten if
994 * something has already provided a default.
John Bowlercb0b2962011-05-12 21:48:29 -0500995 */
996 if (png_ptr->rgb_to_gray_red_coeff == 0 &&
John Bowler736f40f2011-08-25 16:19:44 -0500997 png_ptr->rgb_to_gray_green_coeff == 0)
John Bowlercb0b2962011-05-12 21:48:29 -0500998 {
John Bowler736f40f2011-08-25 16:19:44 -0500999 png_ptr->rgb_to_gray_red_coeff = 6968;
1000 png_ptr->rgb_to_gray_green_coeff = 23434;
1001 /* png_ptr->rgb_to_gray_blue_coeff = 2366; */
John Bowlercb0b2962011-05-12 21:48:29 -05001002 }
1003 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001004 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001005}
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001006
1007#ifdef PNG_FLOATING_POINT_SUPPORTED
1008/* Convert a RGB image to a grayscale of the same width. This allows us,
1009 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1010 */
1011
1012void PNGAPI
1013png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001014 double green)
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001015{
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001016 if (png_ptr == NULL)
1017 return;
1018
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001019 png_set_rgb_to_gray_fixed(png_ptr, error_action,
1020 png_fixed(png_ptr, red, "rgb to gray red coefficient"),
1021 png_fixed(png_ptr, green, "rgb to gray green coefficient"));
Glenn Randers-Pehrsona272d8f2010-06-25 21:45:31 -05001022}
1023#endif /* FLOATING POINT */
1024
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001025#endif
1026
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001027#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
Glenn Randers-Pehrson33188ac2009-06-16 14:12:35 -05001028 defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001029void PNGAPI
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001030png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001031 read_user_transform_fn)
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001032{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001033 png_debug(1, "in png_set_read_user_transform_fn");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001034
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001035 if (png_ptr == NULL)
1036 return;
Glenn Randers-Pehrsonb3ce3652009-08-15 21:47:03 -05001037
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001038#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001039 png_ptr->transformations |= PNG_USER_TRANSFORM;
1040 png_ptr->read_user_transform_fn = read_user_transform_fn;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001041#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001042}
1043#endif
1044
John Bowler4a12f4a2011-04-17 18:34:22 -05001045#ifdef PNG_READ_TRANSFORMS_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001046#ifdef PNG_READ_GAMMA_SUPPORTED
1047/* In the case of gamma transformations only do transformations on images where
1048 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
1049 * slows things down slightly, and also needlessly introduces small errors.
1050 */
1051static int /* PRIVATE */
1052png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
1053{
1054 /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
1055 * correction as a difference of the overall transform from 1.0
1056 *
1057 * We want to compare the threshold with s*f - 1, if we get
1058 * overflow here it is because of wacky gamma values so we
1059 * turn on processing anyway.
1060 */
1061 png_fixed_point gtest;
1062 return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1063 png_gamma_significant(gtest);
1064}
1065#endif
1066
Andreas Dilger47a0c421997-05-16 02:46:07 -05001067/* Initialize everything needed for the read. This includes modifying
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001068 * the palette.
1069 */
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001070
John Bowlerd273ad22011-05-07 21:00:28 -05001071/*For the moment 'png_init_palette_transformations' and
1072 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
1073 * The intent is that these two routines should have palette or rgb operations
1074 * extracted from 'png_init_read_transformations'.
1075 */
1076static void /* PRIVATE */
1077png_init_palette_transformations(png_structp png_ptr)
1078{
1079 /* Called to handle the (input) palette case. In png_do_read_transformations
1080 * the first step is to expand the palette if requested, so this code must
1081 * take care to only make changes that are invariant with respect to the
1082 * palette expansion, or only do them if there is no expansion.
1083 *
1084 * STRIP_ALPHA has already been handled in the caller (by setting num_trans
1085 * to 0.)
1086 */
1087 int input_has_alpha = 0;
1088 int input_has_transparency = 0;
1089
1090 if (png_ptr->num_trans > 0)
1091 {
1092 int i;
1093
1094 /* Ignore if all the entries are opaque (unlikely!) */
1095 for (i=0; i<png_ptr->num_trans; ++i)
1096 if (png_ptr->trans_alpha[i] == 255)
1097 continue;
1098 else if (png_ptr->trans_alpha[i] == 0)
1099 input_has_transparency = 1;
1100 else
1101 input_has_alpha = 1;
1102 }
1103
1104 /* If no alpha we can optimize. */
1105 if (!input_has_alpha)
1106 {
1107 /* Any alpha means background and associative alpha processing is
1108 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1109 * and ENCODE_ALPHA are irrelevant.
1110 */
1111 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1112 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1113
1114 if (!input_has_transparency)
1115 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1116 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001117
Guy Schalnate5a37791996-06-05 15:50:50 -05001118#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
John Bowlerd273ad22011-05-07 21:00:28 -05001119 /* png_set_background handling - deals with the complexity of whether the
1120 * background color is in the file format or the screen format in the case
1121 * where an 'expand' will happen.
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001122 */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001123
John Bowlerd273ad22011-05-07 21:00:28 -05001124 /* The following code cannot be entered in the alpha pre-multiplication case
1125 * because PNG_BACKGROUND_EXPAND is cancelled below.
1126 */
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001127 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1128 (png_ptr->transformations & PNG_EXPAND))
Guy Schalnat0d580581995-07-20 02:43:20 -05001129 {
John Bowlerd273ad22011-05-07 21:00:28 -05001130 {
1131 png_ptr->background.red =
1132 png_ptr->palette[png_ptr->background.index].red;
1133 png_ptr->background.green =
1134 png_ptr->palette[png_ptr->background.index].green;
1135 png_ptr->background.blue =
1136 png_ptr->palette[png_ptr->background.index].blue;
1137
1138#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1139 if (png_ptr->transformations & PNG_INVERT_ALPHA)
1140 {
1141 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1142 {
1143 /* Invert the alpha channel (in tRNS) unless the pixels are
1144 * going to be expanded, in which case leave it for later
1145 */
1146 int i, istop = png_ptr->num_trans;
1147
1148 for (i=0; i<istop; i++)
1149 png_ptr->trans_alpha[i] = (png_byte)(255 -
1150 png_ptr->trans_alpha[i]);
1151 }
1152 }
1153#endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
1154 }
1155 } /* background expand and (therefore) no alpha association. */
1156#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1157}
1158
1159static void /* PRIVATE */
1160png_init_rgb_transformations(png_structp png_ptr)
1161{
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001162 /* Added to libpng-1.5.4: check the color type to determine whether there
John Bowlerd273ad22011-05-07 21:00:28 -05001163 * is any alpha or transparency in the image and simply cancel the
1164 * background and alpha mode stuff if there isn't.
1165 */
1166 int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1167 int input_has_transparency = png_ptr->num_trans > 0;
1168
1169 /* If no alpha we can optimize. */
1170 if (!input_has_alpha)
1171 {
1172 /* Any alpha means background and associative alpha processing is
1173 * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
1174 * and ENCODE_ALPHA are irrelevant.
1175 */
1176# ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1177 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1178 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1179# endif
1180
1181 if (!input_has_transparency)
1182 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1183 }
1184
1185#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1186 /* png_set_background handling - deals with the complexity of whether the
1187 * background color is in the file format or the screen format in the case
1188 * where an 'expand' will happen.
1189 */
1190
1191 /* The following code cannot be entered in the alpha pre-multiplication case
1192 * because PNG_BACKGROUND_EXPAND is cancelled below.
1193 */
1194 if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1195 (png_ptr->transformations & PNG_EXPAND) &&
1196 !(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
1197 /* i.e., GRAY or GRAY_ALPHA */
1198 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001199 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001200 /* Expand background and tRNS chunks */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001201 int gray = png_ptr->background.gray;
1202 int trans_gray = png_ptr->trans_color.gray;
1203
Guy Schalnat0d580581995-07-20 02:43:20 -05001204 switch (png_ptr->bit_depth)
1205 {
1206 case 1:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001207 gray *= 0xff;
1208 trans_gray *= 0xff;
Guy Schalnat0d580581995-07-20 02:43:20 -05001209 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001210
Guy Schalnat0d580581995-07-20 02:43:20 -05001211 case 2:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001212 gray *= 0x55;
1213 trans_gray *= 0x55;
Guy Schalnat0d580581995-07-20 02:43:20 -05001214 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001215
Guy Schalnat0d580581995-07-20 02:43:20 -05001216 case 4:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001217 gray *= 0x11;
1218 trans_gray *= 0x11;
Guy Schalnate5a37791996-06-05 15:50:50 -05001219 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001220
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001221 default:
1222
Guy Schalnate5a37791996-06-05 15:50:50 -05001223 case 8:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001224 /* Already 8 bits, fall through */
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001225
Guy Schalnate5a37791996-06-05 15:50:50 -05001226 case 16:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001227 /* Already a full 16 bits */
Guy Schalnat0d580581995-07-20 02:43:20 -05001228 break;
1229 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001230
1231 png_ptr->background.red = png_ptr->background.green =
1232 png_ptr->background.blue = (png_uint_16)gray;
1233
1234 if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1235 {
1236 png_ptr->trans_color.red = png_ptr->trans_color.green =
1237 png_ptr->trans_color.blue = (png_uint_16)trans_gray;
1238 }
Guy Schalnat0d580581995-07-20 02:43:20 -05001239 }
John Bowlerd273ad22011-05-07 21:00:28 -05001240 } /* background expand and (therefore) no alpha association. */
1241#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
1242}
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001243
John Bowlerd273ad22011-05-07 21:00:28 -05001244void /* PRIVATE */
1245png_init_read_transformations(png_structp png_ptr)
1246{
1247 png_debug(1, "in png_init_read_transformations");
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001248
John Bowlerd273ad22011-05-07 21:00:28 -05001249 /* This internal function is called from png_read_start_row in pngrutil.c
1250 * and it is called before the 'rowbytes' calculation is done, so the code
1251 * in here can change or update the transformations flags.
1252 *
1253 * First do updates that do not depend on the details of the PNG image data
1254 * being processed.
1255 */
Guy Schalnat0d580581995-07-20 02:43:20 -05001256
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001257#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001258 /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
John Bowlerd273ad22011-05-07 21:00:28 -05001259 * png_set_alpha_mode and this is another source for a default file gamma so
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001260 * the test needs to be performed later - here. In addition prior to 1.5.4
John Bowlerd273ad22011-05-07 21:00:28 -05001261 * the tests were repeated for the PALETTE color type here - this is no
1262 * longer necessary (and doesn't seem to have been necessary before.)
1263 */
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001264 {
John Bowlerd273ad22011-05-07 21:00:28 -05001265 /* The following temporary indicates if overall gamma correction is
1266 * required.
1267 */
1268 int gamma_correction = 0;
1269
1270 if (png_ptr->gamma != 0) /* has been set */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001271 {
John Bowlerd273ad22011-05-07 21:00:28 -05001272 if (png_ptr->screen_gamma != 0) /* screen set too */
1273 gamma_correction = png_gamma_threshold(png_ptr->gamma,
1274 png_ptr->screen_gamma);
1275
1276 else
1277 /* Assume the output matches the input; a long time default behavior
1278 * of libpng, although the standard has nothing to say about this.
1279 */
1280 png_ptr->screen_gamma = png_reciprocal(png_ptr->gamma);
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001281 }
John Bowlerd273ad22011-05-07 21:00:28 -05001282
1283 else if (png_ptr->screen_gamma != 0)
1284 /* The converse - assume the file matches the screen, note that this
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001285 * perhaps undesireable default can (from 1.5.4) be changed by calling
John Bowlerd273ad22011-05-07 21:00:28 -05001286 * png_set_alpha_mode (even if the alpha handling mode isn't required
1287 * or isn't changed from the default.)
1288 */
1289 png_ptr->gamma = png_reciprocal(png_ptr->screen_gamma);
1290
1291 else /* neither are set */
1292 /* Just in case the following prevents any processing - file and screen
1293 * are both assumed to be linear and there is no way to introduce a
1294 * third gamma value other than png_set_background with 'UNIQUE', and,
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001295 * prior to 1.5.4
John Bowlerd273ad22011-05-07 21:00:28 -05001296 */
1297 png_ptr->screen_gamma = png_ptr->gamma = PNG_FP_1;
1298
1299 /* Now turn the gamma transformation on or off as appropriate. Notice
1300 * that PNG_GAMMA just refers to the file->screen correction. Alpha
1301 * composition may independently cause gamma correction because it needs
1302 * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1303 * hasn't been specified.) In any case this flag may get turned off in
1304 * the code immediately below if the transform can be handled outside the
1305 * row loop.
1306 */
1307 if (gamma_correction)
1308 png_ptr->transformations |= PNG_GAMMA;
1309
1310 else
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001311 png_ptr->transformations &= ~PNG_GAMMA;
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001312 }
John Bowlerd273ad22011-05-07 21:00:28 -05001313#endif
Glenn Randers-Pehrsonc6de22d2002-02-23 18:55:25 -06001314
John Bowlerd273ad22011-05-07 21:00:28 -05001315 /* Certain transformations have the effect of preventing other
1316 * transformations that happen afterward in png_do_read_transformations,
1317 * resolve the interdependencies here. From the code of
1318 * png_do_read_transformations the order is:
1319 *
1320 * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1321 * 2) PNG_STRIP_ALPHA (if no compose)
1322 * 3) PNG_RGB_TO_GRAY
1323 * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1324 * 5) PNG_COMPOSE
1325 * 6) PNG_GAMMA
1326 * 7) PNG_STRIP_ALPHA (if compose)
1327 * 8) PNG_ENCODE_ALPHA
John Bowler8d261262011-06-18 13:37:11 -05001328 * 9) PNG_SCALE_16_TO_8
1329 * 10) PNG_16_TO_8
1330 * 11) PNG_QUANTIZE (converts to palette)
1331 * 12) PNG_EXPAND_16
1332 * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1333 * 14) PNG_INVERT_MONO
1334 * 15) PNG_SHIFT
1335 * 16) PNG_PACK
1336 * 17) PNG_BGR
1337 * 18) PNG_PACKSWAP
1338 * 19) PNG_FILLER (includes PNG_ADD_ALPHA)
1339 * 20) PNG_INVERT_ALPHA
1340 * 21) PNG_SWAP_ALPHA
1341 * 22) PNG_SWAP_BYTES
1342 * 23) PNG_USER_TRANSFORM [must be last]
John Bowlerd273ad22011-05-07 21:00:28 -05001343 */
1344#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1345 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
1346 !(png_ptr->transformations & PNG_COMPOSE))
1347 {
1348 /* Stripping the alpha channel happens immediately after the 'expand'
1349 * transformations, before all other transformation, so it cancels out
1350 * the alpha handling. It has the side effect negating the effect of
1351 * PNG_EXPAND_tRNS too:
1352 */
1353 png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1354 PNG_EXPAND_tRNS);
1355 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1356
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001357 /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
John Bowlerd273ad22011-05-07 21:00:28 -05001358 * so transparency information would remain just so long as it wasn't
1359 * expanded. This produces unexpected API changes if the set of things
1360 * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1361 * documentation - which says ask for what you want, accept what you
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001362 * get.) This makes the behavior consistent from 1.5.4:
John Bowlerd273ad22011-05-07 21:00:28 -05001363 */
1364 png_ptr->num_trans = 0;
1365 }
1366#endif /* STRIP_ALPHA supported, no COMPOSE */
1367
1368#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1369 /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1370 * settings will have no effect.
1371 */
1372 if (!png_gamma_significant(png_ptr->screen_gamma))
1373 {
1374 png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1375 png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1376 }
1377#endif
1378
1379#if defined(PNG_READ_EXPAND_SUPPORTED) && \
1380 defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1381 defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1382 /* Detect gray background and attempt to enable optimization for
1383 * gray --> RGB case.
1384 *
1385 * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1386 * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1387 * background color might actually be gray yet not be flagged as such.
1388 * This is not a problem for the current code, which uses
1389 * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1390 * png_do_gray_to_rgb() transformation.
1391 *
John Bowler9994f252011-05-15 18:52:39 -05001392 * TODO: this code needs to be revised to avoid the complexity and
John Bowlerd273ad22011-05-07 21:00:28 -05001393 * interdependencies. The color type of the background should be recorded in
1394 * png_set_background, along with the bit depth, then the code has a record
1395 * of exactly what color space the background is currently in.
1396 */
1397 if (png_ptr->transformations & PNG_BACKGROUND_EXPAND)
1398 {
1399 /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
John Bowler736f40f2011-08-25 16:19:44 -05001400 * the file was grayscale the background value is gray.
John Bowlerd273ad22011-05-07 21:00:28 -05001401 */
1402 if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
1403 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1404 }
1405
1406 else if (png_ptr->transformations & PNG_COMPOSE)
1407 {
1408 /* PNG_COMPOSE: png_set_background was called with need_expand false,
1409 * so the color is in the color space of the output or png_set_alpha_mode
1410 * was called and the color is black. Ignore RGB_TO_GRAY because that
1411 * happens before GRAY_TO_RGB.
1412 */
1413 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1414 {
1415 if (png_ptr->background.red == png_ptr->background.green &&
1416 png_ptr->background.red == png_ptr->background.blue)
1417 {
1418 png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1419 png_ptr->background.gray = png_ptr->background.red;
1420 }
1421 }
1422 }
1423#endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED (etc) */
1424
1425 /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1426 * can be performed directly on the palette, and some (such as rgb to gray)
1427 * can be optimized inside the palette. This is particularly true of the
1428 * composite (background and alpha) stuff, which can be pretty much all done
1429 * in the palette even if the result is expanded to RGB or gray afterward.
1430 *
1431 * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1432 * earlier and the palette stuff is actually handled on the first row. This
1433 * leads to the reported bug that the palette returned by png_get_PLTE is not
1434 * updated.
1435 */
1436 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1437 png_init_palette_transformations(png_ptr);
1438
1439 else
1440 png_init_rgb_transformations(png_ptr);
1441
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001442#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1443 defined(PNG_READ_EXPAND_16_SUPPORTED)
1444 if ((png_ptr->transformations & PNG_EXPAND_16) &&
1445 (png_ptr->transformations & PNG_COMPOSE) &&
1446 !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1447 png_ptr->bit_depth != 16)
1448 {
1449 /* TODO: fix this. Because the expand_16 operation is after the compose
1450 * handling the background color must be 8, not 16, bits deep, but the
Glenn Randers-Pehrson0a048922011-05-18 21:44:37 -05001451 * application will supply a 16-bit value so reduce it here.
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001452 *
1453 * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1454 * present, so that case is ok (until do_expand_16 is moved.)
Glenn Randers-Pehrsonbe720ed2011-06-15 08:20:37 -05001455 *
1456 * NOTE: this discards the low 16 bits of the user supplied background
1457 * color, but until expand_16 works properly there is no choice!
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001458 */
John Bowlerb2bee332011-06-10 23:24:58 -05001459# define CHOP(x) (x)=((png_uint_16)(((png_uint_32)(x)*255+32895) >> 16))
1460 CHOP(png_ptr->background.red);
1461 CHOP(png_ptr->background.green);
1462 CHOP(png_ptr->background.blue);
1463 CHOP(png_ptr->background.gray);
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001464# undef CHOP
1465 }
Glenn Randers-Pehrson2232baa2011-06-14 06:59:46 -05001466#endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
John Bowlerdb0ed3e2011-05-18 18:51:24 -05001467
John Bowlerd273ad22011-05-07 21:00:28 -05001468 /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1469 * background support (see the comments in scripts/pnglibconf.dfa), this
1470 * allows pre-multiplication of the alpha channel to be implemented as
1471 * compositing on black. This is probably sub-optimal and has been done in
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001472 * 1.5.4 betas simply to enable external critique and testing (i.e. to
John Bowlerd273ad22011-05-07 21:00:28 -05001473 * implement the new API quickly, without lots of internal changes.)
1474 */
1475
1476#ifdef PNG_READ_GAMMA_SUPPORTED
1477# ifdef PNG_READ_BACKGROUND_SUPPORTED
1478 /* Includes ALPHA_MODE */
1479 png_ptr->background_1 = png_ptr->background;
1480# endif
1481
1482 /* This needs to change - in the palette image case a whole set of tables are
1483 * built when it would be quicker to just calculate the correct value for
1484 * each palette entry directly. Also, the test is too tricky - why check
1485 * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1486 * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1487 * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1488 * the gamma tables will not be built even if composition is required on a
1489 * gamma encoded value.
1490 *
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001491 * In 1.5.4 this is addressed below by an additional check on the individual
John Bowlerd273ad22011-05-07 21:00:28 -05001492 * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1493 * tables.
1494 */
1495 if ((png_ptr->transformations & PNG_GAMMA)
1496 || ((png_ptr->transformations & PNG_RGB_TO_GRAY)
1497 && (png_gamma_significant(png_ptr->gamma) ||
1498 png_gamma_significant(png_ptr->screen_gamma)))
1499 || ((png_ptr->transformations & PNG_COMPOSE)
1500 && (png_gamma_significant(png_ptr->gamma)
1501 || png_gamma_significant(png_ptr->screen_gamma)
Glenn Randers-Pehrson903c64d2011-06-15 11:50:23 -05001502# ifdef PNG_READ_BACKGROUND_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001503 || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE
Glenn Randers-Pehrson903c64d2011-06-15 11:50:23 -05001504 && png_gamma_significant(png_ptr->background_gamma))
1505# endif
1506 )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA)
John Bowlerd273ad22011-05-07 21:00:28 -05001507 && png_gamma_significant(png_ptr->screen_gamma))
1508 )
Guy Schalnat0d580581995-07-20 02:43:20 -05001509 {
Glenn Randers-Pehrsonf81b50b2009-12-29 16:50:15 -06001510 png_build_gamma_table(png_ptr, png_ptr->bit_depth);
Glenn Randers-Pehrsonffa89242009-12-13 08:14:40 -06001511
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001512#ifdef PNG_READ_BACKGROUND_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001513 if (png_ptr->transformations & PNG_COMPOSE)
Guy Schalnat0d580581995-07-20 02:43:20 -05001514 {
John Bowlerd273ad22011-05-07 21:00:28 -05001515 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001516 {
John Bowlerd273ad22011-05-07 21:00:28 -05001517 /* We don't get to here unless there is a tRNS chunk with non-opaque
1518 * entries - see the checking code at the start of this function.
1519 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001520 png_color back, back_1;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001521 png_colorp palette = png_ptr->palette;
1522 int num_palette = png_ptr->num_palette;
1523 int i;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001524 if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1525 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001526
Andreas Dilger47a0c421997-05-16 02:46:07 -05001527 back.red = png_ptr->gamma_table[png_ptr->background.red];
1528 back.green = png_ptr->gamma_table[png_ptr->background.green];
1529 back.blue = png_ptr->gamma_table[png_ptr->background.blue];
Guy Schalnate5a37791996-06-05 15:50:50 -05001530
Andreas Dilger47a0c421997-05-16 02:46:07 -05001531 back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1532 back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1533 back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1534 }
1535 else
1536 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001537 png_fixed_point g, gs;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001538
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001539 switch (png_ptr->background_gamma_type)
Glenn Randers-Pehrson4922b1b1998-03-08 22:55:17 -06001540 {
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001541 case PNG_BACKGROUND_GAMMA_SCREEN:
1542 g = (png_ptr->screen_gamma);
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001543 gs = PNG_FP_1;
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001544 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001545
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001546 case PNG_BACKGROUND_GAMMA_FILE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001547 g = png_reciprocal(png_ptr->gamma);
1548 gs = png_reciprocal2(png_ptr->gamma,
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001549 png_ptr->screen_gamma);
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001550 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001551
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001552 case PNG_BACKGROUND_GAMMA_UNIQUE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001553 g = png_reciprocal(png_ptr->background_gamma);
1554 gs = png_reciprocal2(png_ptr->background_gamma,
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001555 png_ptr->screen_gamma);
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001556 break;
1557 default:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001558 g = PNG_FP_1; /* back_1 */
1559 gs = PNG_FP_1; /* back */
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001560 break;
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001561 }
1562
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05001563 if (png_gamma_significant(gs))
Glenn Randers-Pehrson8f8fb6a1998-03-09 23:02:06 -06001564 {
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001565 back.red = png_gamma_8bit_correct(png_ptr->background.red,
1566 gs);
1567 back.green = png_gamma_8bit_correct(png_ptr->background.green,
1568 gs);
1569 back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1570 gs);
Andreas Dilger47a0c421997-05-16 02:46:07 -05001571 }
John Bowlerd273ad22011-05-07 21:00:28 -05001572
1573 else
1574 {
1575 back.red = (png_byte)png_ptr->background.red;
1576 back.green = (png_byte)png_ptr->background.green;
1577 back.blue = (png_byte)png_ptr->background.blue;
1578 }
1579
1580 if (png_gamma_significant(g))
1581 {
1582 back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1583 g);
1584 back_1.green = png_gamma_8bit_correct(
1585 png_ptr->background.green, g);
1586 back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1587 g);
1588 }
1589
1590 else
1591 {
1592 back_1.red = (png_byte)png_ptr->background.red;
1593 back_1.green = (png_byte)png_ptr->background.green;
1594 back_1.blue = (png_byte)png_ptr->background.blue;
1595 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001596 }
John Bowlerd273ad22011-05-07 21:00:28 -05001597
Guy Schalnate5a37791996-06-05 15:50:50 -05001598 for (i = 0; i < num_palette; i++)
1599 {
Glenn Randers-Pehrsona3137512010-08-18 20:25:36 -05001600 if (i < (int)png_ptr->num_trans &&
1601 png_ptr->trans_alpha[i] != 0xff)
Guy Schalnate5a37791996-06-05 15:50:50 -05001602 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001603 if (png_ptr->trans_alpha[i] == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001604 {
1605 palette[i] = back;
1606 }
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001607 else /* if (png_ptr->trans_alpha[i] != 0xff) */
Guy Schalnate5a37791996-06-05 15:50:50 -05001608 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001609 png_byte v, w;
Guy Schalnate5a37791996-06-05 15:50:50 -05001610
1611 v = png_ptr->gamma_to_1[palette[i].red];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001612 png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
Guy Schalnate5a37791996-06-05 15:50:50 -05001613 palette[i].red = png_ptr->gamma_from_1[w];
1614
1615 v = png_ptr->gamma_to_1[palette[i].green];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001616 png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
Guy Schalnate5a37791996-06-05 15:50:50 -05001617 palette[i].green = png_ptr->gamma_from_1[w];
1618
1619 v = png_ptr->gamma_to_1[palette[i].blue];
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001620 png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
Guy Schalnate5a37791996-06-05 15:50:50 -05001621 palette[i].blue = png_ptr->gamma_from_1[w];
1622 }
1623 }
1624 else
1625 {
1626 palette[i].red = png_ptr->gamma_table[palette[i].red];
1627 palette[i].green = png_ptr->gamma_table[palette[i].green];
1628 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1629 }
1630 }
John Bowlerd273ad22011-05-07 21:00:28 -05001631
1632 /* Prevent the transformations being done again.
1633 *
Glenn Randers-Pehrson63071ac2011-08-29 16:16:26 -05001634 * NOTE: this is highly dubious; it removes the transformations in
John Bowlerd273ad22011-05-07 21:00:28 -05001635 * place. This seems inconsistent with the general treatment of the
1636 * transformations elsewhere.
Glenn Randers-Pehrson72cbc6e2009-09-20 07:28:43 -05001637 */
John Bowlerd273ad22011-05-07 21:00:28 -05001638 png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1639 } /* color_type == PNG_COLOR_TYPE_PALETTE */
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001640
Glenn Randers-Pehrsond1e8c862002-06-20 06:54:34 -05001641 /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
John Bowlerd273ad22011-05-07 21:00:28 -05001642 else /* color_type != PNG_COLOR_TYPE_PALETTE */
Guy Schalnat0d580581995-07-20 02:43:20 -05001643 {
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001644 png_fixed_point g = PNG_FP_1;
1645 png_fixed_point gs = PNG_FP_1;
Guy Schalnat0d580581995-07-20 02:43:20 -05001646
1647 switch (png_ptr->background_gamma_type)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001648 {
Guy Schalnat0d580581995-07-20 02:43:20 -05001649 case PNG_BACKGROUND_GAMMA_SCREEN:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001650 g = png_ptr->screen_gamma;
1651 /* gs = PNG_FP_1; */
Guy Schalnat0d580581995-07-20 02:43:20 -05001652 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001653
Guy Schalnat0d580581995-07-20 02:43:20 -05001654 case PNG_BACKGROUND_GAMMA_FILE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001655 g = png_reciprocal(png_ptr->gamma);
1656 gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -05001657 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001658
Guy Schalnat0d580581995-07-20 02:43:20 -05001659 case PNG_BACKGROUND_GAMMA_UNIQUE:
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001660 g = png_reciprocal(png_ptr->background_gamma);
1661 gs = png_reciprocal2(png_ptr->background_gamma,
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001662 png_ptr->screen_gamma);
Guy Schalnat0d580581995-07-20 02:43:20 -05001663 break;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06001664
1665 default:
Glenn Randers-Pehrsone9eeb742011-05-08 19:38:42 -05001666 png_error(png_ptr, "invalid background gamma type");
Guy Schalnat0d580581995-07-20 02:43:20 -05001667 }
1668
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001669 png_ptr->background_1.gray = png_gamma_correct(png_ptr,
Glenn Randers-Pehrson29034c52010-07-29 17:58:49 -05001670 png_ptr->background.gray, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001671
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001672 png_ptr->background.gray = png_gamma_correct(png_ptr,
1673 png_ptr->background.gray, gs);
Glenn Randers-Pehrson377657d2002-03-08 01:31:27 -06001674
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001675 if ((png_ptr->background.red != png_ptr->background.green) ||
1676 (png_ptr->background.red != png_ptr->background.blue) ||
1677 (png_ptr->background.red != png_ptr->background.gray))
Guy Schalnat0d580581995-07-20 02:43:20 -05001678 {
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001679 /* RGB or RGBA with color background */
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001680 png_ptr->background_1.red = png_gamma_correct(png_ptr,
1681 png_ptr->background.red, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001682
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001683 png_ptr->background_1.green = png_gamma_correct(png_ptr,
1684 png_ptr->background.green, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001685
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001686 png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1687 png_ptr->background.blue, g);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001688
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001689 png_ptr->background.red = png_gamma_correct(png_ptr,
1690 png_ptr->background.red, gs);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001691
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001692 png_ptr->background.green = png_gamma_correct(png_ptr,
1693 png_ptr->background.green, gs);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001694
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001695 png_ptr->background.blue = png_gamma_correct(png_ptr,
1696 png_ptr->background.blue, gs);
Guy Schalnat0d580581995-07-20 02:43:20 -05001697 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001698
Guy Schalnat0d580581995-07-20 02:43:20 -05001699 else
1700 {
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001701 /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1702 png_ptr->background_1.red = png_ptr->background_1.green
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001703 = png_ptr->background_1.blue = png_ptr->background_1.gray;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001704
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06001705 png_ptr->background.red = png_ptr->background.green
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001706 = png_ptr->background.blue = png_ptr->background.gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05001707 }
John Bowlerd273ad22011-05-07 21:00:28 -05001708 } /* color_type != PNG_COLOR_TYPE_PALETTE */
1709 }/* png_ptr->transformations & PNG_BACKGROUND */
1710
Guy Schalnate5a37791996-06-05 15:50:50 -05001711 else
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05001712 /* Transformation does not include PNG_BACKGROUND */
Glenn Randers-Pehrson5a0be342001-10-18 20:55:13 -05001713#endif /* PNG_READ_BACKGROUND_SUPPORTED */
John Bowlerd273ad22011-05-07 21:00:28 -05001714 if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnate5a37791996-06-05 15:50:50 -05001715 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001716 png_colorp palette = png_ptr->palette;
1717 int num_palette = png_ptr->num_palette;
1718 int i;
Guy Schalnate5a37791996-06-05 15:50:50 -05001719
John Bowlerd273ad22011-05-07 21:00:28 -05001720 /*NOTE: there are other transformations that should probably be in here
1721 * too.
1722 */
Guy Schalnate5a37791996-06-05 15:50:50 -05001723 for (i = 0; i < num_palette; i++)
1724 {
1725 palette[i].red = png_ptr->gamma_table[palette[i].red];
1726 palette[i].green = png_ptr->gamma_table[palette[i].green];
1727 palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1728 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001729
Glenn Randers-Pehrson72cbc6e2009-09-20 07:28:43 -05001730 /* Done the gamma correction. */
1731 png_ptr->transformations &= ~PNG_GAMMA;
John Bowlerd273ad22011-05-07 21:00:28 -05001732 } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
Guy Schalnate5a37791996-06-05 15:50:50 -05001733 }
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001734#ifdef PNG_READ_BACKGROUND_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -05001735 else
1736#endif
Glenn Randers-Pehrson31aee0d2010-07-29 17:39:14 -05001737#endif /* PNG_READ_GAMMA_SUPPORTED */
John Bowlerd273ad22011-05-07 21:00:28 -05001738
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001739#ifdef PNG_READ_BACKGROUND_SUPPORTED
John Bowlerd273ad22011-05-07 21:00:28 -05001740 /* No GAMMA transformation (see the hanging else 4 lines above) */
1741 if ((png_ptr->transformations & PNG_COMPOSE) &&
1742 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
Guy Schalnate5a37791996-06-05 15:50:50 -05001743 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001744 int i;
1745 int istop = (int)png_ptr->num_trans;
Guy Schalnate5a37791996-06-05 15:50:50 -05001746 png_color back;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001747 png_colorp palette = png_ptr->palette;
Guy Schalnate5a37791996-06-05 15:50:50 -05001748
Guy Schalnate5a37791996-06-05 15:50:50 -05001749 back.red = (png_byte)png_ptr->background.red;
1750 back.green = (png_byte)png_ptr->background.green;
1751 back.blue = (png_byte)png_ptr->background.blue;
1752
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001753 for (i = 0; i < istop; i++)
Guy Schalnate5a37791996-06-05 15:50:50 -05001754 {
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001755 if (png_ptr->trans_alpha[i] == 0)
Guy Schalnate5a37791996-06-05 15:50:50 -05001756 {
1757 palette[i] = back;
1758 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001759
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05001760 else if (png_ptr->trans_alpha[i] != 0xff)
Guy Schalnate5a37791996-06-05 15:50:50 -05001761 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001762 /* The png_composite() macro is defined in png.h */
Andreas Dilger47a0c421997-05-16 02:46:07 -05001763 png_composite(palette[i].red, palette[i].red,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001764 png_ptr->trans_alpha[i], back.red);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001765
Andreas Dilger47a0c421997-05-16 02:46:07 -05001766 png_composite(palette[i].green, palette[i].green,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001767 png_ptr->trans_alpha[i], back.green);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001768
Andreas Dilger47a0c421997-05-16 02:46:07 -05001769 png_composite(palette[i].blue, palette[i].blue,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001770 png_ptr->trans_alpha[i], back.blue);
Guy Schalnate5a37791996-06-05 15:50:50 -05001771 }
1772 }
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001773
John Bowlerd273ad22011-05-07 21:00:28 -05001774 png_ptr->transformations &= ~PNG_COMPOSE;
Guy Schalnat0d580581995-07-20 02:43:20 -05001775 }
Glenn Randers-Pehrson5a0be342001-10-18 20:55:13 -05001776#endif /* PNG_READ_BACKGROUND_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001777
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001778#ifdef PNG_READ_SHIFT_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001779 if ((png_ptr->transformations & PNG_SHIFT) &&
John Bowlerd273ad22011-05-07 21:00:28 -05001780 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001781 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001782 int i;
1783 int istop = png_ptr->num_palette;
1784 int shift = 8 - png_ptr->sig_bit.red;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001785
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001786 /* significant bits can be in the range 1 to 7 for a meaninful result, if
1787 * the number of significant bits is 0 then no shift is done (this is an
1788 * error condition which is silently ignored.)
1789 */
1790 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001791 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001792 int component = png_ptr->palette[i].red;
1793
1794 component >>= shift;
1795 png_ptr->palette[i].red = (png_byte)component;
1796 }
1797
1798 shift = 8 - png_ptr->sig_bit.green;
1799 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
1800 {
1801 int component = png_ptr->palette[i].green;
1802
1803 component >>= shift;
1804 png_ptr->palette[i].green = (png_byte)component;
1805 }
1806
1807 shift = 8 - png_ptr->sig_bit.blue;
1808 if (shift > 0 && shift < 8) for (i=0; i<istop; ++i)
1809 {
1810 int component = png_ptr->palette[i].blue;
1811
1812 component >>= shift;
1813 png_ptr->palette[i].blue = (png_byte)component;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001814 }
1815 }
Glenn Randers-Pehrson5a0be342001-10-18 20:55:13 -05001816#endif /* PNG_READ_SHIFT_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001817}
1818
Andreas Dilger47a0c421997-05-16 02:46:07 -05001819/* Modify the info structure to reflect the transformations. The
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06001820 * info should be updated so a PNG file could be written with it,
1821 * assuming the transformations result in valid PNG data.
1822 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05001823void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06001824png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001825{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05001826 png_debug(1, "in png_read_transform_info");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05001827
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001828#ifdef PNG_READ_EXPAND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05001829 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001830 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05001831 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1832 {
John Bowler9994f252011-05-15 18:52:39 -05001833 /* This check must match what actually happens in
Glenn Randers-Pehrson0e128df2011-05-15 19:09:24 -05001834 * png_do_expand_palette; if it ever checks the tRNS chunk to see if
John Bowler9994f252011-05-15 18:52:39 -05001835 * it is all opaque we must do the same (at present it does not.)
1836 */
1837 if (png_ptr->num_trans > 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001838 info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001839
Andreas Dilger47a0c421997-05-16 02:46:07 -05001840 else
1841 info_ptr->color_type = PNG_COLOR_TYPE_RGB;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001842
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001843 info_ptr->bit_depth = 8;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001844 info_ptr->num_trans = 0;
1845 }
1846 else
1847 {
1848 if (png_ptr->num_trans)
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06001849 {
1850 if (png_ptr->transformations & PNG_EXPAND_tRNS)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001851 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06001852 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001853 if (info_ptr->bit_depth < 8)
1854 info_ptr->bit_depth = 8;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001855
Andreas Dilger47a0c421997-05-16 02:46:07 -05001856 info_ptr->num_trans = 0;
1857 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001858 }
1859#endif
1860
John Bowlerd273ad22011-05-07 21:00:28 -05001861#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1862 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1863 /* The following is almost certainly wrong unless the background value is in
1864 * the screen space!
1865 */
1866 if (png_ptr->transformations & PNG_COMPOSE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001867 info_ptr->background = png_ptr->background;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001868#endif
1869
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001870#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05001871 /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
John Bowlerd273ad22011-05-07 21:00:28 -05001872 * however it seems that the code in png_init_read_transformations, which has
1873 * been called before this from png_read_update_info->png_read_start_row
1874 * sometimes does the gamma transform and cancels the flag.
1875 */
1876 info_ptr->gamma = png_ptr->gamma;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001877#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001878
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05001879 if (info_ptr->bit_depth == 16)
1880 {
John Bowler8d261262011-06-18 13:37:11 -05001881# ifdef PNG_READ_16BIT_SUPPORTED
1882# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1883 if (png_ptr->transformations & PNG_SCALE_16_TO_8)
1884 info_ptr->bit_depth = 8;
1885# endif
1886
1887# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1888 if (png_ptr->transformations & PNG_16_TO_8)
1889 info_ptr->bit_depth = 8;
1890# endif
1891
1892# else
1893 /* No 16 bit support: force chopping 16-bit input down to 8, in this case
1894 * the app program can chose if both APIs are available by setting the
1895 * correct scaling to use.
1896 */
1897# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1898 /* For compatibility with previous versions use the strip method by
1899 * default. This code works because if PNG_SCALE_16_TO_8 is already
1900 * set the code below will do that in preference to the chop.
1901 */
1902 png_ptr->transformations |= PNG_16_TO_8;
1903 info_ptr->bit_depth = 8;
1904# else
1905
1906# if PNG_READ_SCALE_16_TO_8_SUPPORTED
1907 png_ptr->transformations |= PNG_SCALE_16_TO_8;
1908 info_ptr->bit_depth = 8;
1909# else
1910
1911 CONFIGURATION ERROR: you must enable at least one 16 to 8 method
1912# endif
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05001913# endif
John Bowler8d261262011-06-18 13:37:11 -05001914#endif /* !READ_16BIT_SUPPORTED */
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05001915 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001916
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001917#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001918 if (png_ptr->transformations & PNG_GRAY_TO_RGB)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001919 info_ptr->color_type = (png_byte)(info_ptr->color_type |
1920 PNG_COLOR_MASK_COLOR);
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001921#endif
1922
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001923#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001924 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001925 info_ptr->color_type = (png_byte)(info_ptr->color_type &
1926 ~PNG_COLOR_MASK_COLOR);
Glenn Randers-Pehrsonddfebd32006-02-22 09:19:25 -06001927#endif
1928
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05001929#ifdef PNG_READ_QUANTIZE_SUPPORTED
1930 if (png_ptr->transformations & PNG_QUANTIZE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001931 {
1932 if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001933 (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1934 png_ptr->palette_lookup && info_ptr->bit_depth == 8)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001935 {
1936 info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1937 }
1938 }
1939#endif
1940
John Bowler1921e6d2011-05-16 20:57:54 -05001941#ifdef PNG_READ_EXPAND_16_SUPPORTED
1942 if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
1943 info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
1944 {
1945 info_ptr->bit_depth = 16;
1946 }
1947#endif
1948
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001949#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001950 if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001951 info_ptr->bit_depth = 8;
1952#endif
1953
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06001954 if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001955 info_ptr->channels = 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001956
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001957 else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1958 info_ptr->channels = 3;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001959
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001960 else
1961 info_ptr->channels = 1;
Andreas Dilger47a0c421997-05-16 02:46:07 -05001962
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001963#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -06001964 if (png_ptr->transformations & PNG_STRIP_ALPHA)
John Bowlerd273ad22011-05-07 21:00:28 -05001965 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05001966 info_ptr->color_type = (png_byte)(info_ptr->color_type &
1967 ~PNG_COLOR_MASK_ALPHA);
John Bowlerd273ad22011-05-07 21:00:28 -05001968 info_ptr->num_trans = 0;
1969 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001970#endif
1971
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001972 if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1973 info_ptr->channels++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001974
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05001975#ifdef PNG_READ_FILLER_SUPPORTED
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001976 /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001977 if ((png_ptr->transformations & PNG_FILLER) &&
1978 ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1979 (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001980 {
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05001981 info_ptr->channels++;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05001982 /* If adding a true alpha channel not just filler */
Glenn Randers-Pehrson67864af2004-08-28 23:30:07 -05001983 if (png_ptr->transformations & PNG_ADD_ALPHA)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001984 info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
Glenn Randers-Pehrson5e5c1e12000-11-10 12:26:19 -06001985 }
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001986#endif
1987
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001988#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1989defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05001990 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001991 {
1992 if (info_ptr->bit_depth < png_ptr->user_transform_depth)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001993 info_ptr->bit_depth = png_ptr->user_transform_depth;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05001994
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001995 if (info_ptr->channels < png_ptr->user_transform_channels)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001996 info_ptr->channels = png_ptr->user_transform_channels;
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06001997 }
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001998#endif
1999
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002000 info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002001 info_ptr->bit_depth);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05002002
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002003 info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
Glenn Randers-Pehrsonbcfd15d1999-10-01 14:22:25 -05002004
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002005 /* Adding in 1.5.4: cache the above value in png_struct so that we can later
John Bowler9994f252011-05-15 18:52:39 -05002006 * check in png_rowbytes that the user buffer won't get overwritten. Note
2007 * that the field is not always set - if png_read_update_info isn't called
2008 * the application has to either not do any transforms or get the calculation
2009 * right itself.
2010 */
2011 png_ptr->info_rowbytes = info_ptr->rowbytes;
2012
Glenn Randers-Pehrsonb2aca212009-09-23 11:32:37 -05002013#ifndef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002014 if (png_ptr)
Glenn Randers-Pehrson104622b2000-05-29 08:58:03 -05002015 return;
2016#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002017}
2018
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002019/* Transform the row. The order of transformations is significant,
2020 * and is very touchy. If you add a transformation, take care to
2021 * decide how it fits in with the other transformations here.
2022 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002023void /* PRIVATE */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002024png_do_read_transformations(png_structp png_ptr, png_row_infop row_info)
Guy Schalnat0d580581995-07-20 02:43:20 -05002025{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002026 png_debug(1, "in png_do_read_transformations");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002027
Andreas Dilger47a0c421997-05-16 02:46:07 -05002028 if (png_ptr->row_buf == NULL)
2029 {
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002030 /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
John Bowler88b77cc2011-05-05 06:49:55 -05002031 * error is incredibly rare and incredibly easy to debug without this
2032 * information.
2033 */
Glenn Randers-Pehrson70e3f541998-01-03 22:40:55 -06002034 png_error(png_ptr, "NULL row buffer");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002035 }
John Bowlerd273ad22011-05-07 21:00:28 -05002036
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002037 /* The following is debugging; prior to 1.5.4 the code was never compiled in;
2038 * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
John Bowlerd273ad22011-05-07 21:00:28 -05002039 * PNG_WARN_UNINITIALIZED_ROW removed. In 1.5 the new flag is set only for
2040 * selected new APIs to ensure that there is no API change.
2041 */
2042 if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
2043 !(png_ptr->flags & PNG_FLAG_ROW_INIT))
2044 {
2045 /* Application has failed to call either png_read_start_image() or
2046 * png_read_update_info() after setting transforms that expand pixels.
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002047 * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002048 */
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002049 png_error(png_ptr, "Uninitialized row");
John Bowlerd273ad22011-05-07 21:00:28 -05002050 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06002051
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002052#ifdef PNG_READ_EXPAND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002053 if (png_ptr->transformations & PNG_EXPAND)
Guy Schalnat0d580581995-07-20 02:43:20 -05002054 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002055 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002056 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002057 png_do_expand_palette(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002058 png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002059 }
Glenn Randers-Pehrsonc36bb792011-02-12 09:49:07 -06002060
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002061 else
Andreas Dilger47a0c421997-05-16 02:46:07 -05002062 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002063 if (png_ptr->num_trans &&
2064 (png_ptr->transformations & PNG_EXPAND_tRNS))
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002065 png_do_expand(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002066 &(png_ptr->trans_color));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002067
John Bowler4d562962011-02-12 09:01:20 -06002068 else
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002069 png_do_expand(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002070 NULL);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002071 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002072 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05002073#endif
2074
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002075#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
John Bowler9b872f42011-02-12 09:00:16 -06002076 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
John Bowlerd273ad22011-05-07 21:00:28 -05002077 !(png_ptr->transformations & PNG_COMPOSE) &&
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002078 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2079 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2080 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrsond2795b72011-04-16 19:41:23 -05002081 0 /* at_start == false, because SWAP_ALPHA happens later */);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002082#endif
2083
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002084#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002085 if (png_ptr->transformations & PNG_RGB_TO_GRAY)
2086 {
2087 int rgb_error =
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002088 png_do_rgb_to_gray(png_ptr, row_info,
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002089 png_ptr->row_buf + 1);
2090
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002091 if (rgb_error)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002092 {
2093 png_ptr->rgb_to_gray_status=1;
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06002094 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002095 PNG_RGB_TO_GRAY_WARN)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002096 png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002097
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002098 if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
2099 PNG_RGB_TO_GRAY_ERR)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002100 png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
2101 }
2102 }
2103#endif
2104
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002105/* From Andreas Dilger e-mail to png-implement, 26 March 1998:
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -05002106 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002107 * In most cases, the "simple transparency" should be done prior to doing
2108 * gray-to-RGB, or you will have to test 3x as many bytes to check if a
2109 * pixel is transparent. You would also need to make sure that the
2110 * transparency information is upgraded to RGB.
Glenn Randers-Pehrsond60c8862009-06-15 21:56:14 -05002111 *
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002112 * To summarize, the current flow is:
2113 * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
2114 * with background "in place" if transparent,
2115 * convert to RGB if necessary
2116 * - Gray + alpha -> composite with gray background and remove alpha bytes,
2117 * convert to RGB if necessary
2118 *
2119 * To support RGB backgrounds for gray images we need:
2120 * - Gray + simple transparency -> convert to RGB + simple transparency,
2121 * compare 3 or 6 bytes and composite with
2122 * background "in place" if transparent
2123 * (3x compare/pixel compared to doing
2124 * composite with gray bkgrnd)
2125 * - Gray + alpha -> convert to RGB + alpha, composite with background and
2126 * remove alpha bytes (3x float
2127 * operations/pixel compared with composite
2128 * on gray background)
2129 *
2130 * Greg's change will do this. The reason it wasn't done before is for
2131 * performance, as this increases the per-pixel operations. If we would check
2132 * in advance if the background was gray or RGB, and position the gray-to-RGB
2133 * transform appropriately, then it would save a lot of work/time.
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002134 */
2135
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002136#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002137 /* If gray -> RGB, do so now only if background is non-gray; else do later
2138 * for performance reasons
2139 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002140 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -06002141 !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002142 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002143#endif
2144
John Bowlerd273ad22011-05-07 21:00:28 -05002145#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
2146 (defined PNG_READ_ALPHA_MODE_SUPPORTED)
2147 if (png_ptr->transformations & PNG_COMPOSE)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002148 png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002149#endif
2150
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002151#ifdef PNG_READ_GAMMA_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002152 if ((png_ptr->transformations & PNG_GAMMA) &&
John Bowlerd273ad22011-05-07 21:00:28 -05002153#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
2154 (defined PNG_READ_ALPHA_MODE_SUPPORTED)
2155 !((png_ptr->transformations & PNG_COMPOSE) &&
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002156 ((png_ptr->num_trans != 0) ||
2157 (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05002158#endif
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002159 (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002160 png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002161#endif
2162
John Bowlerd273ad22011-05-07 21:00:28 -05002163#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2164 if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
2165 (png_ptr->transformations & PNG_COMPOSE) &&
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002166 (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2167 row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
2168 png_do_strip_channel(row_info, png_ptr->row_buf + 1,
John Bowlerd273ad22011-05-07 21:00:28 -05002169 0 /* at_start == false, because SWAP_ALPHA happens later */);
2170#endif
2171
2172#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
2173 if ((png_ptr->transformations & PNG_ENCODE_ALPHA) &&
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002174 (row_info->color_type & PNG_COLOR_MASK_ALPHA))
2175 png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
John Bowlerd273ad22011-05-07 21:00:28 -05002176#endif
2177
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002178#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrsonaee83b42011-06-18 00:19:54 -05002179 if (png_ptr->transformations & PNG_SCALE_16_TO_8)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002180 png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002181#endif
John Bowler8d261262011-06-18 13:37:11 -05002182
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002183#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
John Bowler8d261262011-06-18 13:37:11 -05002184 /* There is no harm in doing both of these because only one has any effect,
2185 * by putting the 'scale' option first if the app asks for scale (either by
2186 * calling the API or in a TRANSFORM flag) this is what happens.
2187 */
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002188 if (png_ptr->transformations & PNG_16_TO_8)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002189 png_do_chop(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrson7a59e1e2009-12-11 07:57:36 -06002190#endif
2191
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05002192#ifdef PNG_READ_QUANTIZE_SUPPORTED
2193 if (png_ptr->transformations & PNG_QUANTIZE)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002194 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002195 png_do_quantize(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05002196 png_ptr->palette_lookup, png_ptr->quantize_index);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002197
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002198 if (row_info->rowbytes == 0)
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05002199 png_error(png_ptr, "png_do_quantize returned rowbytes=0");
Andreas Dilger47a0c421997-05-16 02:46:07 -05002200 }
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05002201#endif /* PNG_READ_QUANTIZE_SUPPORTED */
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002202
John Bowler4d562962011-02-12 09:01:20 -06002203#ifdef PNG_READ_EXPAND_16_SUPPORTED
2204 /* Do the expansion now, after all the arithmetic has been done. Notice
2205 * that previous transformations can handle the PNG_EXPAND_16 flag if this
2206 * is efficient (particularly true in the case of gamma correction, where
2207 * better accuracy results faster!)
2208 */
2209 if (png_ptr->transformations & PNG_EXPAND_16)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002210 png_do_expand_16(row_info, png_ptr->row_buf + 1);
John Bowler4d562962011-02-12 09:01:20 -06002211#endif
John Bowlerd273ad22011-05-07 21:00:28 -05002212
2213#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrsonef217b72011-06-15 12:58:27 -05002214 /*NOTE: moved here in 1.5.4 (from much later in this list.) */
John Bowlerd273ad22011-05-07 21:00:28 -05002215 if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2216 (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002217 png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
John Bowlerd273ad22011-05-07 21:00:28 -05002218#endif
John Bowler4d562962011-02-12 09:01:20 -06002219
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002220#ifdef PNG_READ_INVERT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002221 if (png_ptr->transformations & PNG_INVERT_MONO)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002222 png_do_invert(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002223#endif
2224
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002225#ifdef PNG_READ_SHIFT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002226 if (png_ptr->transformations & PNG_SHIFT)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002227 png_do_unshift(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002228 &(png_ptr->shift));
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002229#endif
2230
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002231#ifdef PNG_READ_PACK_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002232 if (png_ptr->transformations & PNG_PACK)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002233 png_do_unpack(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002234#endif
2235
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002236#ifdef PNG_READ_BGR_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002237 if (png_ptr->transformations & PNG_BGR)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002238 png_do_bgr(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002239#endif
2240
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002241#ifdef PNG_READ_PACKSWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002242 if (png_ptr->transformations & PNG_PACKSWAP)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002243 png_do_packswap(row_info, png_ptr->row_buf + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002244#endif
2245
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002246#ifdef PNG_READ_FILLER_SUPPORTED
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002247 if (png_ptr->transformations & PNG_FILLER)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002248 png_do_read_filler(row_info, png_ptr->row_buf + 1,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002249 (png_uint_32)png_ptr->filler, png_ptr->flags);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002250#endif
2251
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002252#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002253 if (png_ptr->transformations & PNG_INVERT_ALPHA)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002254 png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002255#endif
2256
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002257#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002258 if (png_ptr->transformations & PNG_SWAP_ALPHA)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002259 png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002260#endif
2261
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002262#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002263#ifdef PNG_READ_SWAP_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002264 if (png_ptr->transformations & PNG_SWAP_BYTES)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002265 png_do_swap(row_info, png_ptr->row_buf + 1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002266#endif
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002267#endif
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06002268
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002269#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06002270 if (png_ptr->transformations & PNG_USER_TRANSFORM)
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002271 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002272 if (png_ptr->read_user_transform_fn != NULL)
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05002273 (*(png_ptr->read_user_transform_fn)) /* User read transform function */
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002274 (png_ptr, /* png_ptr */
2275 row_info, /* row_info: */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002276 /* png_uint_32 width; width of row */
Glenn Randers-Pehrsonbcb3aac2010-09-10 22:05:27 -05002277 /* png_size_t rowbytes; number of bytes in row */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002278 /* png_byte color_type; color type of pixels */
2279 /* png_byte bit_depth; bit depth of samples */
2280 /* png_byte channels; number of channels (1-4) */
2281 /* png_byte pixel_depth; bits per pixel (depth*channels) */
2282 png_ptr->row_buf + 1); /* start of pixel data for row */
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002283#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002284 if (png_ptr->user_transform_depth)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002285 row_info->bit_depth = png_ptr->user_transform_depth;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002286
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002287 if (png_ptr->user_transform_channels)
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002288 row_info->channels = png_ptr->user_transform_channels;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05002289#endif
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002290 row_info->pixel_depth = (png_byte)(row_info->bit_depth *
2291 row_info->channels);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002292
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002293 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002294 }
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06002295#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002296}
2297
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002298#ifdef PNG_READ_PACK_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002299/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
2300 * without changing the actual values. Thus, if you had a row with
2301 * a bit depth of 1, you would end up with bytes that only contained
2302 * the numbers 0 or 1. If you would rather they contain 0 and 255, use
2303 * png_do_shift() after this.
2304 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002305void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002306png_do_unpack(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05002307{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002308 png_debug(1, "in png_do_unpack");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002309
Andreas Dilger47a0c421997-05-16 02:46:07 -05002310 if (row_info->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002311 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002312 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002313 png_uint_32 row_width=row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002314
Guy Schalnat0d580581995-07-20 02:43:20 -05002315 switch (row_info->bit_depth)
2316 {
2317 case 1:
2318 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002319 png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
2320 png_bytep dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002321 png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002322 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002323 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002324 *dp = (png_byte)((*sp >> shift) & 0x01);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002325
Guy Schalnat0d580581995-07-20 02:43:20 -05002326 if (shift == 7)
2327 {
2328 shift = 0;
2329 sp--;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002330 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002331
Guy Schalnat0d580581995-07-20 02:43:20 -05002332 else
2333 shift++;
2334
2335 dp--;
2336 }
2337 break;
2338 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002339
Guy Schalnat0d580581995-07-20 02:43:20 -05002340 case 2:
2341 {
Guy Schalnat0d580581995-07-20 02:43:20 -05002342
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002343 png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
2344 png_bytep dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002345 png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002346 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002347 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002348 *dp = (png_byte)((*sp >> shift) & 0x03);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002349
Guy Schalnat0d580581995-07-20 02:43:20 -05002350 if (shift == 6)
2351 {
2352 shift = 0;
2353 sp--;
2354 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002355
Guy Schalnat0d580581995-07-20 02:43:20 -05002356 else
2357 shift += 2;
2358
2359 dp--;
2360 }
2361 break;
2362 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002363
Guy Schalnat0d580581995-07-20 02:43:20 -05002364 case 4:
2365 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002366 png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
2367 png_bytep dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002368 png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002369 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05002370 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002371 *dp = (png_byte)((*sp >> shift) & 0x0f);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002372
Guy Schalnat0d580581995-07-20 02:43:20 -05002373 if (shift == 4)
2374 {
2375 shift = 0;
2376 sp--;
2377 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002378
Guy Schalnat0d580581995-07-20 02:43:20 -05002379 else
2380 shift = 4;
2381
2382 dp--;
2383 }
2384 break;
2385 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002386
2387 default:
2388 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05002389 }
2390 row_info->bit_depth = 8;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002391 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002392 row_info->rowbytes = row_width * row_info->channels;
Guy Schalnat0d580581995-07-20 02:43:20 -05002393 }
2394}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002395#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002396
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002397#ifdef PNG_READ_SHIFT_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06002398/* Reverse the effects of png_do_shift. This routine merely shifts the
2399 * pixels back to their significant bits values. Thus, if you have
2400 * a row of bit depth 8, but only 5 are significant, this will shift
2401 * the values back to 0 through 31.
2402 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002403void /* PRIVATE */
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05002404png_do_unshift(png_row_infop row_info, png_bytep row,
2405 png_const_color_8p sig_bits)
Guy Schalnat0d580581995-07-20 02:43:20 -05002406{
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002407 int color_type;
2408
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002409 png_debug(1, "in png_do_unshift");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002410
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002411 /* The palette case has already been handled in the _init routine. */
2412 color_type = row_info->color_type;
2413
2414 if (color_type != PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -05002415 {
2416 int shift[4];
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002417 int channels = 0;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002418 int bit_depth = row_info->bit_depth;
Guy Schalnat0d580581995-07-20 02:43:20 -05002419
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002420 if (color_type & PNG_COLOR_MASK_COLOR)
Guy Schalnat0d580581995-07-20 02:43:20 -05002421 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002422 shift[channels++] = bit_depth - sig_bits->red;
2423 shift[channels++] = bit_depth - sig_bits->green;
2424 shift[channels++] = bit_depth - sig_bits->blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05002425 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002426
Guy Schalnat0d580581995-07-20 02:43:20 -05002427 else
2428 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002429 shift[channels++] = bit_depth - sig_bits->gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05002430 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002431
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002432 if (color_type & PNG_COLOR_MASK_ALPHA)
Guy Schalnat0d580581995-07-20 02:43:20 -05002433 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002434 shift[channels++] = bit_depth - sig_bits->alpha;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002435 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002436
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002437 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002438 int c, have_shift;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002439
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002440 for (c = have_shift = 0; c < channels; ++c)
2441 {
2442 /* A shift of more than the bit depth is an error condition but it
2443 * gets ignored here.
2444 */
2445 if (shift[c] <= 0 || shift[c] >= bit_depth)
2446 shift[c] = 0;
2447
2448 else
2449 have_shift = 1;
2450 }
2451
2452 if (!have_shift)
2453 return;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002454 }
Guy Schalnat0f716451995-11-28 11:22:13 -06002455
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002456 switch (bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -05002457 {
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002458 default:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002459 /* Must be 1bpp gray: should not be here! */
2460 /* NOTREACHED */
Glenn Randers-Pehrsonc36bb792011-02-12 09:49:07 -06002461 break;
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06002462
Guy Schalnat0d580581995-07-20 02:43:20 -05002463 case 2:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002464 /* Must be 2bpp gray */
2465 /* assert(channels == 1 && shift[0] == 1) */
Guy Schalnat0d580581995-07-20 02:43:20 -05002466 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002467 png_bytep bp = row;
2468 png_bytep bp_end = bp + row_info->rowbytes;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002469
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002470 while (bp < bp_end)
Guy Schalnat0d580581995-07-20 02:43:20 -05002471 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002472 int byte = (*bp >> 1) & 0x55;
2473 *bp++ = (png_byte)byte;
Guy Schalnat0d580581995-07-20 02:43:20 -05002474 }
2475 break;
2476 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002477
Guy Schalnat0d580581995-07-20 02:43:20 -05002478 case 4:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002479 /* Must be 4bpp gray */
2480 /* assert(channels == 1) */
Guy Schalnat0d580581995-07-20 02:43:20 -05002481 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -05002482 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002483 png_bytep bp_end = bp + row_info->rowbytes;
2484 int gray_shift = shift[0];
2485 int mask = 0xf >> gray_shift;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002486
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002487 mask |= mask << 4;
2488
2489 while (bp < bp_end)
Guy Schalnat0d580581995-07-20 02:43:20 -05002490 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002491 int byte = (*bp >> gray_shift) & mask;
2492 *bp++ = (png_byte)byte;
Guy Schalnat0d580581995-07-20 02:43:20 -05002493 }
2494 break;
2495 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002496
Guy Schalnat0d580581995-07-20 02:43:20 -05002497 case 8:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002498 /* Single byte components, G, GA, RGB, RGBA */
Guy Schalnat0d580581995-07-20 02:43:20 -05002499 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002500 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002501 png_bytep bp_end = bp + row_info->rowbytes;
2502 int channel = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002503
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002504 while (bp < bp_end)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002505 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002506 int byte = *bp >> shift[channel];
2507 if (++channel >= channels)
2508 channel = 0;
2509 *bp++ = (png_byte)byte;
Guy Schalnat0d580581995-07-20 02:43:20 -05002510 }
2511 break;
2512 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05002513
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002514#ifdef PNG_READ_16BIT_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05002515 case 16:
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002516 /* Double byte components, G, GA, RGB, RGBA */
Guy Schalnat0d580581995-07-20 02:43:20 -05002517 {
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002518 png_bytep bp = row;
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002519 png_bytep bp_end = bp + row_info->rowbytes;
2520 int channel = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05002521
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002522 while (bp < bp_end)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002523 {
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05002524 int value = (bp[0] << 8) + bp[1];
2525
2526 value >>= shift[channel];
2527 if (++channel >= channels)
2528 channel = 0;
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002529 *bp++ = (png_byte)(value >> 8);
2530 *bp++ = (png_byte)(value & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05002531 }
2532 break;
2533 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002534#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002535 }
2536 }
2537}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002538#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002539
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002540#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002541/* Scale rows of bit depth 16 down to 8 accurately */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002542void /* PRIVATE */
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002543png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05002544{
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002545 png_debug(1, "in png_do_scale_16_to_8");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002546
Andreas Dilger47a0c421997-05-16 02:46:07 -05002547 if (row_info->bit_depth == 16)
Guy Schalnat0d580581995-07-20 02:43:20 -05002548 {
John Bowlerb2bee332011-06-10 23:24:58 -05002549 png_bytep sp = row; /* source */
2550 png_bytep dp = row; /* destinaton */
2551 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002552
John Bowlerb2bee332011-06-10 23:24:58 -05002553 while (sp < ep)
Guy Schalnat0d580581995-07-20 02:43:20 -05002554 {
John Bowlerb2bee332011-06-10 23:24:58 -05002555 /* The input is an array of 16 bit components, these must be scaled to
2556 * 8 bits each. For a 16 bit value V the required value (from the PNG
2557 * specification) is:
2558 *
2559 * (V * 255) / 65535
2560 *
2561 * This reduces to round(V / 257), or floor((V + 128.5)/257)
2562 *
2563 * Represent V as the two byte value vhi.vlo. Make a guess that the
2564 * result is the top byte of V, vhi, then the correction to this value
2565 * is:
2566 *
2567 * error = floor(((V-vhi.vhi) + 128.5) / 257)
2568 * = floor(((vlo-vhi) + 128.5) / 257)
2569 *
2570 * This can be approximated using integer arithmetic (and a signed
2571 * shift):
2572 *
2573 * error = (vlo-vhi+128) >> 8;
2574 *
2575 * The approximate differs from the exact answer only when (vlo-vhi) is
2576 * 128; it then gives a correction of +1 when the exact correction is
2577 * 0. This gives 128 errors. The exact answer (correct for all 16 bit
2578 * input values) is:
2579 *
2580 * error = (vlo-vhi+128)*65535 >> 24;
2581 *
2582 * An alternative arithmetic calculation which also gives no errors is:
2583 *
2584 * (V * 255 + 32895) >> 16
2585 */
Glenn Randers-Pehrson141d9e32011-06-13 11:47:00 -05002586
John Bowlerb2bee332011-06-10 23:24:58 -05002587 png_int_32 tmp = *sp++; /* must be signed! */
2588 tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
John Bowler74945f22011-06-13 20:50:42 -05002589 *dp++ = (png_byte)tmp;
Guy Schalnat0d580581995-07-20 02:43:20 -05002590 }
John Bowlerb2bee332011-06-10 23:24:58 -05002591
Guy Schalnat0d580581995-07-20 02:43:20 -05002592 row_info->bit_depth = 8;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06002593 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -06002594 row_info->rowbytes = row_info->width * row_info->channels;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002595 }
2596}
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002597#endif
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002598
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002599#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002600void /* PRIVATE */
2601/* Simply discard the low byte. This was the default behavior prior
2602 * to libpng-1.5.4.
2603 */
2604png_do_chop(png_row_infop row_info, png_bytep row)
2605{
2606 png_debug(1, "in png_do_chop");
2607
2608 if (row_info->bit_depth == 16)
2609 {
2610 png_bytep sp = row; /* source */
2611 png_bytep dp = row; /* destinaton */
2612 png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2613
2614 while (sp < ep)
2615 {
John Bowler8d261262011-06-18 13:37:11 -05002616 *dp++ = *sp;
2617 sp += 2; /* skip low byte */
Glenn Randers-Pehrson413138a2011-06-13 22:07:37 -05002618 }
2619
2620 row_info->bit_depth = 8;
2621 row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2622 row_info->rowbytes = row_info->width * row_info->channels;
2623 }
2624}
Glenn Randers-Pehrsone6a80602011-06-17 22:28:23 -05002625#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05002626
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002627#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002628void /* PRIVATE */
Andreas Dilger47a0c421997-05-16 02:46:07 -05002629png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
2630{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002631 png_debug(1, "in png_do_read_swap_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002632
Andreas Dilger47a0c421997-05-16 02:46:07 -05002633 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002634 png_uint_32 row_width = row_info->width;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002635 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2636 {
2637 /* This converts from RGBA to ARGB */
2638 if (row_info->bit_depth == 8)
2639 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002640 png_bytep sp = row + row_info->rowbytes;
2641 png_bytep dp = sp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002642 png_byte save;
2643 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002644
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002645 for (i = 0; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002646 {
2647 save = *(--sp);
2648 *(--dp) = *(--sp);
2649 *(--dp) = *(--sp);
2650 *(--dp) = *(--sp);
2651 *(--dp) = save;
2652 }
2653 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002654
2655#ifdef PNG_READ_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002656 /* This converts from RRGGBBAA to AARRGGBB */
2657 else
2658 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002659 png_bytep sp = row + row_info->rowbytes;
2660 png_bytep dp = sp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002661 png_byte save[2];
2662 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002663
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002664 for (i = 0; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002665 {
2666 save[0] = *(--sp);
2667 save[1] = *(--sp);
2668 *(--dp) = *(--sp);
2669 *(--dp) = *(--sp);
2670 *(--dp) = *(--sp);
2671 *(--dp) = *(--sp);
2672 *(--dp) = *(--sp);
2673 *(--dp) = *(--sp);
2674 *(--dp) = save[0];
2675 *(--dp) = save[1];
2676 }
2677 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002678#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05002679 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002680
Andreas Dilger47a0c421997-05-16 02:46:07 -05002681 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2682 {
2683 /* This converts from GA to AG */
2684 if (row_info->bit_depth == 8)
2685 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002686 png_bytep sp = row + row_info->rowbytes;
2687 png_bytep dp = sp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002688 png_byte save;
2689 png_uint_32 i;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002690
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002691 for (i = 0; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002692 {
2693 save = *(--sp);
2694 *(--dp) = *(--sp);
2695 *(--dp) = save;
2696 }
2697 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002698
2699#ifdef PNG_READ_16BIT_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002700 /* This converts from GGAA to AAGG */
2701 else
2702 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002703 png_bytep sp = row + row_info->rowbytes;
2704 png_bytep dp = sp;
Andreas Dilger47a0c421997-05-16 02:46:07 -05002705 png_byte save[2];
2706 png_uint_32 i;
2707
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002708 for (i = 0; i < row_width; i++)
Andreas Dilger47a0c421997-05-16 02:46:07 -05002709 {
2710 save[0] = *(--sp);
2711 save[1] = *(--sp);
2712 *(--dp) = *(--sp);
2713 *(--dp) = *(--sp);
2714 *(--dp) = save[0];
2715 *(--dp) = save[1];
2716 }
2717 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002718#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05002719 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002720 }
2721}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002722#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05002723
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002724#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002725void /* PRIVATE */
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002726png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2727{
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002728 png_uint_32 row_width;
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002729 png_debug(1, "in png_do_read_invert_alpha");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002730
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002731 row_width = row_info->width;
2732 if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002733 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002734 if (row_info->bit_depth == 8)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002735 {
2736 /* This inverts the alpha channel in RGBA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002737 png_bytep sp = row + row_info->rowbytes;
2738 png_bytep dp = sp;
2739 png_uint_32 i;
2740
2741 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002742 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002743 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002744
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002745/* This does nothing:
2746 *(--dp) = *(--sp);
2747 *(--dp) = *(--sp);
2748 *(--dp) = *(--sp);
2749 We can replace it with:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002750*/
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002751 sp-=3;
2752 dp=sp;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002753 }
2754 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002755
2756#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002757 /* This inverts the alpha channel in RRGGBBAA */
2758 else
2759 {
2760 png_bytep sp = row + row_info->rowbytes;
2761 png_bytep dp = sp;
2762 png_uint_32 i;
2763
2764 for (i = 0; i < row_width; i++)
2765 {
2766 *(--dp) = (png_byte)(255 - *(--sp));
2767 *(--dp) = (png_byte)(255 - *(--sp));
2768
2769/* This does nothing:
2770 *(--dp) = *(--sp);
2771 *(--dp) = *(--sp);
2772 *(--dp) = *(--sp);
2773 *(--dp) = *(--sp);
2774 *(--dp) = *(--sp);
2775 *(--dp) = *(--sp);
2776 We can replace it with:
2777*/
2778 sp-=6;
2779 dp=sp;
2780 }
2781 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002782#endif
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002783 }
2784 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2785 {
2786 if (row_info->bit_depth == 8)
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002787 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002788 /* This inverts the alpha channel in GA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002789 png_bytep sp = row + row_info->rowbytes;
2790 png_bytep dp = sp;
2791 png_uint_32 i;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002792
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002793 for (i = 0; i < row_width; i++)
2794 {
2795 *(--dp) = (png_byte)(255 - *(--sp));
2796 *(--dp) = *(--sp);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002797 }
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002798 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002799
2800#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002801 else
2802 {
Glenn Randers-Pehrsoncbe52d81998-02-28 07:00:24 -06002803 /* This inverts the alpha channel in GGAA */
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002804 png_bytep sp = row + row_info->rowbytes;
2805 png_bytep dp = sp;
2806 png_uint_32 i;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002807
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002808 for (i = 0; i < row_width; i++)
2809 {
2810 *(--dp) = (png_byte)(255 - *(--sp));
2811 *(--dp) = (png_byte)(255 - *(--sp));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002812/*
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002813 *(--dp) = *(--sp);
2814 *(--dp) = *(--sp);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06002815*/
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002816 sp-=2;
2817 dp=sp;
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002818 }
2819 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002820#endif
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06002821 }
2822}
2823#endif
2824
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05002825#ifdef PNG_READ_FILLER_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05002826/* Add filler channel if we have RGB color */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05002827void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06002828png_do_read_filler(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002829 png_uint_32 filler, png_uint_32 flags)
Guy Schalnat0d580581995-07-20 02:43:20 -05002830{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002831 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002832 png_uint_32 row_width = row_info->width;
2833
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002834#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -05002835 png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
Glenn Randers-Pehrson4554a542010-08-27 10:48:45 -05002836#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002837 png_byte lo_filler = (png_byte)(filler & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05002838
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05002839 png_debug(1, "in png_do_read_filler");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05002840
Andreas Dilger47a0c421997-05-16 02:46:07 -05002841 if (
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002842 row_info->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05002843 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002844 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05002845 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002846 if (flags & PNG_FLAG_FILLER_AFTER)
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002847 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002848 /* This changes the data from G to GX */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002849 png_bytep sp = row + (png_size_t)row_width;
2850 png_bytep dp = sp + (png_size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002851 for (i = 1; i < row_width; i++)
2852 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002853 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002854 *(--dp) = *(--sp);
2855 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002856 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002857 row_info->channels = 2;
2858 row_info->pixel_depth = 16;
2859 row_info->rowbytes = row_width * 2;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002860 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002861
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002862 else
2863 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002864 /* This changes the data from G to XG */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002865 png_bytep sp = row + (png_size_t)row_width;
2866 png_bytep dp = sp + (png_size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002867 for (i = 0; i < row_width; i++)
2868 {
2869 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002870 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002871 }
2872 row_info->channels = 2;
2873 row_info->pixel_depth = 16;
2874 row_info->rowbytes = row_width * 2;
2875 }
Guy Schalnat0d580581995-07-20 02:43:20 -05002876 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002877
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002878#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002879 else if (row_info->bit_depth == 16)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002880 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002881 if (flags & PNG_FLAG_FILLER_AFTER)
2882 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002883 /* This changes the data from GG to GGXX */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002884 png_bytep sp = row + (png_size_t)row_width * 2;
2885 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002886 for (i = 1; i < row_width; i++)
2887 {
2888 *(--dp) = hi_filler;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002889 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002890 *(--dp) = *(--sp);
2891 *(--dp) = *(--sp);
2892 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002893 *(--dp) = hi_filler;
2894 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002895 row_info->channels = 2;
2896 row_info->pixel_depth = 32;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002897 row_info->rowbytes = row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002898 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002899
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002900 else
2901 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002902 /* This changes the data from GG to XXGG */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002903 png_bytep sp = row + (png_size_t)row_width * 2;
2904 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002905 for (i = 0; i < row_width; i++)
2906 {
2907 *(--dp) = *(--sp);
2908 *(--dp) = *(--sp);
2909 *(--dp) = hi_filler;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002910 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002911 }
2912 row_info->channels = 2;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002913 row_info->pixel_depth = 32;
2914 row_info->rowbytes = row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002915 }
2916 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002917#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002918 } /* COLOR_TYPE == GRAY */
2919 else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2920 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002921 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002922 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002923 if (flags & PNG_FLAG_FILLER_AFTER)
2924 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002925 /* This changes the data from RGB to RGBX */
Glenn Randers-Pehrsondff799e2004-08-07 21:42:49 -05002926 png_bytep sp = row + (png_size_t)row_width * 3;
2927 png_bytep dp = sp + (png_size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002928 for (i = 1; i < row_width; i++)
2929 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002930 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002931 *(--dp) = *(--sp);
2932 *(--dp) = *(--sp);
2933 *(--dp) = *(--sp);
2934 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002935 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002936 row_info->channels = 4;
2937 row_info->pixel_depth = 32;
2938 row_info->rowbytes = row_width * 4;
2939 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002940
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002941 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002942 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002943 /* This changes the data from RGB to XRGB */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05002944 png_bytep sp = row + (png_size_t)row_width * 3;
2945 png_bytep dp = sp + (png_size_t)row_width;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002946 for (i = 0; i < row_width; i++)
2947 {
2948 *(--dp) = *(--sp);
2949 *(--dp) = *(--sp);
2950 *(--dp) = *(--sp);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002951 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002952 }
2953 row_info->channels = 4;
2954 row_info->pixel_depth = 32;
2955 row_info->rowbytes = row_width * 4;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002956 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05002957 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002958
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05002959#ifdef PNG_READ_16BIT_SUPPORTED
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05002960 else if (row_info->bit_depth == 16)
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002961 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002962 if (flags & PNG_FLAG_FILLER_AFTER)
2963 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002964 /* This changes the data from RRGGBB to RRGGBBXX */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002965 png_bytep sp = row + (png_size_t)row_width * 6;
2966 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002967 for (i = 1; i < row_width; i++)
2968 {
2969 *(--dp) = hi_filler;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002970 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002971 *(--dp) = *(--sp);
2972 *(--dp) = *(--sp);
2973 *(--dp) = *(--sp);
2974 *(--dp) = *(--sp);
2975 *(--dp) = *(--sp);
2976 *(--dp) = *(--sp);
2977 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002978 *(--dp) = hi_filler;
2979 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002980 row_info->channels = 4;
2981 row_info->pixel_depth = 64;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05002982 row_info->rowbytes = row_width * 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002983 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05002984
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002985 else
2986 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06002987 /* This changes the data from RRGGBB to XXRRGGBB */
Glenn Randers-Pehrson5b5dcf82004-07-17 22:45:44 -05002988 png_bytep sp = row + (png_size_t)row_width * 6;
2989 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05002990 for (i = 0; i < row_width; i++)
2991 {
2992 *(--dp) = *(--sp);
2993 *(--dp) = *(--sp);
2994 *(--dp) = *(--sp);
2995 *(--dp) = *(--sp);
2996 *(--dp) = *(--sp);
2997 *(--dp) = *(--sp);
2998 *(--dp) = hi_filler;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06002999 *(--dp) = lo_filler;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003000 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003001
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003002 row_info->channels = 4;
3003 row_info->pixel_depth = 64;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05003004 row_info->rowbytes = row_width * 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003005 }
3006 }
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05003007#endif
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003008 } /* COLOR_TYPE == RGB */
Guy Schalnat0d580581995-07-20 02:43:20 -05003009}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003010#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003011
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003012#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003013/* Expand grayscale files to RGB, with or without alpha */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003014void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06003015png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
Guy Schalnat0d580581995-07-20 02:43:20 -05003016{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003017 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003018 png_uint_32 row_width = row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06003019
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003020 png_debug(1, "in png_do_gray_to_rgb");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003021
Andreas Dilger47a0c421997-05-16 02:46:07 -05003022 if (row_info->bit_depth >= 8 &&
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003023 !(row_info->color_type & PNG_COLOR_MASK_COLOR))
Guy Schalnat0d580581995-07-20 02:43:20 -05003024 {
3025 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3026 {
3027 if (row_info->bit_depth == 8)
3028 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003029 /* This changes G to RGB */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003030 png_bytep sp = row + (png_size_t)row_width - 1;
3031 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003032 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003033 {
3034 *(dp--) = *sp;
3035 *(dp--) = *sp;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05003036 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05003037 }
3038 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003039
Guy Schalnat0d580581995-07-20 02:43:20 -05003040 else
3041 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003042 /* This changes GG to RRGGBB */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003043 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
3044 png_bytep dp = sp + (png_size_t)row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003045 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003046 {
3047 *(dp--) = *sp;
3048 *(dp--) = *(sp - 1);
3049 *(dp--) = *sp;
3050 *(dp--) = *(sp - 1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05003051 *(dp--) = *(sp--);
3052 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05003053 }
3054 }
3055 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003056
Guy Schalnat0d580581995-07-20 02:43:20 -05003057 else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3058 {
3059 if (row_info->bit_depth == 8)
3060 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003061 /* This changes GA to RGBA */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003062 png_bytep sp = row + (png_size_t)row_width * 2 - 1;
3063 png_bytep dp = sp + (png_size_t)row_width * 2;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003064 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003065 {
3066 *(dp--) = *(sp--);
3067 *(dp--) = *sp;
3068 *(dp--) = *sp;
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05003069 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05003070 }
3071 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003072
Guy Schalnat0d580581995-07-20 02:43:20 -05003073 else
3074 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003075 /* This changes GGAA to RRGGBBAA */
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003076 png_bytep sp = row + (png_size_t)row_width * 4 - 1;
3077 png_bytep dp = sp + (png_size_t)row_width * 4;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003078 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003079 {
3080 *(dp--) = *(sp--);
3081 *(dp--) = *(sp--);
3082 *(dp--) = *sp;
3083 *(dp--) = *(sp - 1);
3084 *(dp--) = *sp;
3085 *(dp--) = *(sp - 1);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05003086 *(dp--) = *(sp--);
3087 *(dp--) = *(sp--);
Guy Schalnat0d580581995-07-20 02:43:20 -05003088 }
3089 }
3090 }
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003091 row_info->channels = (png_byte)(row_info->channels + 2);
Guy Schalnat0d580581995-07-20 02:43:20 -05003092 row_info->color_type |= PNG_COLOR_MASK_COLOR;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003093 row_info->pixel_depth = (png_byte)(row_info->channels *
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003094 row_info->bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003095 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05003096 }
3097}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003098#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003099
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003100#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05003101/* Reduce RGB files to grayscale, with or without alpha
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05003102 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
3103 * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
3104 * versions dated 1998 through November 2002 have been archived at
3105 * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
3106 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
Glenn Randers-Pehrson895a9c92008-07-25 08:51:18 -05003107 * Charles Poynton poynton at poynton.com
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003108 *
3109 * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
3110 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003111 * which can be expressed with integers as
3112 *
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06003113 * Y = (6969 * R + 23434 * G + 2365 * B)/32768
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003114 *
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05003115 * Poynton's current link (as of January 2003 through July 2011):
3116 * <http://www.poynton.com/notes/colour_and_gamma/>
3117 * has changed the numbers slightly:
3118 *
3119 * Y = 0.2126*R + 0.7152*G + 0.0722*B
3120 *
3121 * which can be expressed with integers as
3122 *
3123 * Y = (6966 * R + 23436 * G + 2366 * B)/32768
3124 *
John Bowler736f40f2011-08-25 16:19:44 -05003125 * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
3126 * end point chromaticities and the D65 white point. Depending on the
3127 * precision used for the D65 white point this produces a variety of different
3128 * numbers, however if the four decimal place value used in ITU-R Rec 709 is
3129 * used (0.3127,0.3290) the Y calculation would be:
3130 *
3131 * Y = (6968 * R + 23435 * G + 2366 * B)/32768
3132 *
3133 * While this is correct the rounding results in an overflow for white, because
3134 * the sum of the rounded coefficients is 32769, not 32768. Consequently
3135 * libpng uses, instead, the closest non-overflowing approximation:
Glenn Randers-Pehrson99ffac02011-07-29 12:22:38 -05003136 *
3137 * Y = (6968 * R + 23434 * G + 2366 * B)/32768
3138 *
John Bowler736f40f2011-08-25 16:19:44 -05003139 * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
3140 * (including an sRGB chunk) then the chromaticities are used to calculate the
3141 * coefficients. See the chunk handling in pngrutil.c for more information.
Glenn Randers-Pehrson0bc79772011-08-11 09:01:57 -05003142 *
John Bowler736f40f2011-08-25 16:19:44 -05003143 * In all cases the calculation is to be done in a linear colorspace. If no
3144 * gamma information is available to correct the encoding of the original RGB
3145 * values this results in an implicit assumption that the original PNG RGB
3146 * values were linear.
Glenn Randers-Pehrson0bc79772011-08-11 09:01:57 -05003147 *
John Bowler736f40f2011-08-25 16:19:44 -05003148 * Other integer coefficents can be used via png_set_rgb_to_gray(). Because
3149 * the API takes just red and green coefficients the blue coefficient is
3150 * calculated to make the sum 32768. This will result in different rounding
3151 * to that used above.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003152 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003153int /* PRIVATE */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003154png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
3155
3156{
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003157 int rgb_error = 0;
3158
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003159 png_debug(1, "in png_do_rgb_to_gray");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003160
John Bowlera96117f2011-01-08 14:41:25 -06003161 if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003162 (row_info->color_type & PNG_COLOR_MASK_COLOR))
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003163 {
John Bowler736f40f2011-08-25 16:19:44 -05003164 PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3165 PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3166 PNG_CONST png_uint_32 bc = 32768 - rc - gc;
3167 PNG_CONST png_uint_32 row_width = row_info->width;
3168 PNG_CONST int have_alpha =
3169 (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003170
John Bowler736f40f2011-08-25 16:19:44 -05003171 if (row_info->bit_depth == 8)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003172 {
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003173#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
John Bowler736f40f2011-08-25 16:19:44 -05003174 /* Notice that gamma to/from 1 are not necessarily inverses (if
3175 * there is an overall gamma correction). Prior to 1.5.5 this code
3176 * checked the linearized values for equality; this doesn't match
3177 * the documentation, the original values must be checked.
3178 */
3179 if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3180 {
3181 png_bytep sp = row;
3182 png_bytep dp = row;
3183 png_uint_32 i;
3184
3185 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003186 {
John Bowler736f40f2011-08-25 16:19:44 -05003187 png_byte red = *(sp++);
3188 png_byte green = *(sp++);
3189 png_byte blue = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003190
John Bowler736f40f2011-08-25 16:19:44 -05003191 if (red != green || red != blue)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003192 {
John Bowler736f40f2011-08-25 16:19:44 -05003193 red = png_ptr->gamma_to_1[red];
3194 green = png_ptr->gamma_to_1[green];
3195 blue = png_ptr->gamma_to_1[blue];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003196
John Bowler736f40f2011-08-25 16:19:44 -05003197 rgb_error |= 1;
3198 *(dp++) = png_ptr->gamma_from_1[
3199 (rc*red + gc*green + bc*blue + 16384)>>15];
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003200 }
John Bowler736f40f2011-08-25 16:19:44 -05003201
3202 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003203 {
John Bowler736f40f2011-08-25 16:19:44 -05003204 /* If there is no overall correction the table will not be
3205 * set.
3206 */
3207 if (png_ptr->gamma_table != NULL)
3208 red = png_ptr->gamma_table[red];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003209
John Bowler736f40f2011-08-25 16:19:44 -05003210 *(dp++) = red;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003211 }
John Bowler736f40f2011-08-25 16:19:44 -05003212
3213 if (have_alpha)
3214 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003215 }
3216 }
John Bowler736f40f2011-08-25 16:19:44 -05003217 else
3218#endif
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003219 {
John Bowler736f40f2011-08-25 16:19:44 -05003220 png_bytep sp = row;
3221 png_bytep dp = row;
3222 png_uint_32 i;
3223
3224 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003225 {
John Bowler736f40f2011-08-25 16:19:44 -05003226 png_byte red = *(sp++);
3227 png_byte green = *(sp++);
3228 png_byte blue = *(sp++);
3229
3230 if (red != green || red != blue)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003231 {
John Bowler736f40f2011-08-25 16:19:44 -05003232 rgb_error |= 1;
3233 /*NOTE: this is the historical approach which simply
3234 * truncates the results.
3235 */
3236 *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3237 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003238
John Bowler736f40f2011-08-25 16:19:44 -05003239 else
3240 *(dp++) = red;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003241
John Bowler736f40f2011-08-25 16:19:44 -05003242 if (have_alpha)
3243 *(dp++) = *(sp++);
3244 }
3245 }
3246 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003247
John Bowler736f40f2011-08-25 16:19:44 -05003248 else /* RGB bit_depth == 16 */
3249 {
3250#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
3251 if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3252 {
3253 png_bytep sp = row;
3254 png_bytep dp = row;
3255 png_uint_32 i;
3256
3257 for (i = 0; i < row_width; i++)
3258 {
3259 png_uint_16 red, green, blue, w;
3260
3261 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3262 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3263 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3264
3265 if (red == green && red == blue)
3266 {
3267 if (png_ptr->gamma_16_table != NULL)
3268 w = png_ptr->gamma_16_table[(red&0xff)
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003269 >> png_ptr->gamma_shift][red>>8];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003270
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003271 else
John Bowler736f40f2011-08-25 16:19:44 -05003272 w = red;
3273 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003274
John Bowler736f40f2011-08-25 16:19:44 -05003275 else
3276 {
3277 png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red&0xff)
3278 >> png_ptr->gamma_shift][red>>8];
3279 png_uint_16 green_1 =
3280 png_ptr->gamma_16_to_1[(green&0xff) >>
3281 png_ptr->gamma_shift][green>>8];
3282 png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue&0xff)
3283 >> png_ptr->gamma_shift][blue>>8];
3284 png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
3285 + bc*blue_1 + 16384)>>15);
3286 w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
3287 png_ptr->gamma_shift][gray16 >> 8];
3288 rgb_error |= 1;
3289 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003290
John Bowler736f40f2011-08-25 16:19:44 -05003291 *(dp++) = (png_byte)((w>>8) & 0xff);
3292 *(dp++) = (png_byte)(w & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003293
John Bowler736f40f2011-08-25 16:19:44 -05003294 if (have_alpha)
3295 {
3296 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003297 *(dp++) = *(sp++);
3298 }
3299 }
John Bowler736f40f2011-08-25 16:19:44 -05003300 }
3301 else
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003302#endif
John Bowler736f40f2011-08-25 16:19:44 -05003303 {
3304 png_bytep sp = row;
3305 png_bytep dp = row;
3306 png_uint_32 i;
3307
3308 for (i = 0; i < row_width; i++)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003309 {
John Bowler736f40f2011-08-25 16:19:44 -05003310 png_uint_16 red, green, blue, gray16;
3311
3312 red = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3313 green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3314 blue = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
3315
3316 if (red != green || red != blue)
3317 rgb_error |= 1;
3318
3319 /* From 1.5.5 in the 16 bit case do the accurate convertion even
3320 * in the 'fast' case - this is because this is where the code
3321 * ends up when handling linear 16 bit data.
3322 */
3323 gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3324 15);
3325 *(dp++) = (png_byte)((gray16>>8) & 0xff);
3326 *(dp++) = (png_byte)(gray16 & 0xff);
3327
3328 if (have_alpha)
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003329 {
John Bowler736f40f2011-08-25 16:19:44 -05003330 *(dp++) = *(sp++);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003331 *(dp++) = *(sp++);
3332 }
3333 }
3334 }
3335 }
John Bowler736f40f2011-08-25 16:19:44 -05003336
Glenn Randers-Pehrsonbb5cb142011-09-22 12:41:58 -05003337 row_info->channels = (png_byte)(row_info->channels - 2);
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05003338 row_info->color_type = (png_byte)(row_info->color_type &
3339 ~PNG_COLOR_MASK_COLOR);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003340 row_info->pixel_depth = (png_byte)(row_info->channels *
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003341 row_info->bit_depth);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05003342 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003343 }
3344 return rgb_error;
3345}
3346#endif
John Bowler4a12f4a2011-04-17 18:34:22 -05003347#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06003348
John Bowler4a12f4a2011-04-17 18:34:22 -05003349#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003350/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
3351 * large of png_color. This lets grayscale images be treated as
3352 * paletted. Most useful for gamma correction and simplification
John Bowler4a12f4a2011-04-17 18:34:22 -05003353 * of code. This API is not used internally.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003354 */
Glenn Randers-Pehrson73d57cb2002-03-25 18:49:08 -06003355void PNGAPI
Guy Schalnat6d764711995-12-19 03:22:19 -06003356png_build_grayscale_palette(int bit_depth, png_colorp palette)
Guy Schalnat0d580581995-07-20 02:43:20 -05003357{
3358 int num_palette;
3359 int color_inc;
3360 int i;
3361 int v;
3362
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05003363 png_debug(1, "in png_do_build_grayscale_palette");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003364
Andreas Dilger47a0c421997-05-16 02:46:07 -05003365 if (palette == NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003366 return;
3367
3368 switch (bit_depth)
3369 {
3370 case 1:
3371 num_palette = 2;
3372 color_inc = 0xff;
3373 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003374
Guy Schalnat0d580581995-07-20 02:43:20 -05003375 case 2:
3376 num_palette = 4;
3377 color_inc = 0x55;
3378 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003379
Guy Schalnat0d580581995-07-20 02:43:20 -05003380 case 4:
3381 num_palette = 16;
3382 color_inc = 0x11;
3383 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003384
Guy Schalnat0d580581995-07-20 02:43:20 -05003385 case 8:
3386 num_palette = 256;
3387 color_inc = 1;
3388 break;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003389
Guy Schalnat0d580581995-07-20 02:43:20 -05003390 default:
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003391 num_palette = 0;
Guy Schalnat69b14481996-01-10 02:56:49 -06003392 color_inc = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05003393 break;
3394 }
3395
3396 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
3397 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003398 palette[i].red = (png_byte)v;
3399 palette[i].green = (png_byte)v;
3400 palette[i].blue = (png_byte)v;
Guy Schalnat0d580581995-07-20 02:43:20 -05003401 }
3402}
John Bowler4a12f4a2011-04-17 18:34:22 -05003403#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003404
Guy Schalnat0d580581995-07-20 02:43:20 -05003405
John Bowler4a12f4a2011-04-17 18:34:22 -05003406#ifdef PNG_READ_TRANSFORMS_SUPPORTED
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003407#ifdef PNG_READ_BACKGROUND_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003408/* Replace any alpha or transparency with the supplied background color.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003409 * "background" is already in the screen gamma, while "background_1" is
3410 * at a gamma of 1.0. Paletted files have already been taken care of.
3411 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05003412void /* PRIVATE */
John Bowlerd273ad22011-05-07 21:00:28 -05003413png_do_compose(png_row_infop row_info, png_bytep row, png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05003414{
John Bowler4a12f4a2011-04-17 18:34:22 -05003415#ifdef PNG_READ_GAMMA_SUPPORTED
3416 png_const_bytep gamma_table = png_ptr->gamma_table;
3417 png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3418 png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3419 png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3420 png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3421 png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3422 int gamma_shift = png_ptr->gamma_shift;
3423#endif
3424
John Bowlerd273ad22011-05-07 21:00:28 -05003425 png_bytep sp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003426 png_uint_32 i;
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003427 png_uint_32 row_width = row_info->width;
John Bowlerd273ad22011-05-07 21:00:28 -05003428 int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003429 int shift;
Guy Schalnate5a37791996-06-05 15:50:50 -05003430
John Bowlerd273ad22011-05-07 21:00:28 -05003431 png_debug(1, "in png_do_compose");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05003432
Guy Schalnat0d580581995-07-20 02:43:20 -05003433 {
3434 switch (row_info->color_type)
3435 {
3436 case PNG_COLOR_TYPE_GRAY:
3437 {
3438 switch (row_info->bit_depth)
3439 {
3440 case 1:
3441 {
Guy Schalnat0d580581995-07-20 02:43:20 -05003442 sp = row;
3443 shift = 7;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003444 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003445 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003446 if ((png_uint_16)((*sp >> shift) & 0x01)
John Bowler4a12f4a2011-04-17 18:34:22 -05003447 == png_ptr->trans_color.gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05003448 {
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003449 *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
John Bowler4a12f4a2011-04-17 18:34:22 -05003450 *sp |= (png_byte)(png_ptr->background.gray << shift);
Guy Schalnat0d580581995-07-20 02:43:20 -05003451 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003452
Guy Schalnat0d580581995-07-20 02:43:20 -05003453 if (!shift)
3454 {
3455 shift = 7;
3456 sp++;
3457 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003458
Guy Schalnat0d580581995-07-20 02:43:20 -05003459 else
3460 shift--;
3461 }
3462 break;
3463 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003464
Guy Schalnat0d580581995-07-20 02:43:20 -05003465 case 2:
3466 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003467#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003468 if (gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003469 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003470 sp = row;
3471 shift = 6;
3472 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003473 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003474 if ((png_uint_16)((*sp >> shift) & 0x03)
John Bowler4a12f4a2011-04-17 18:34:22 -05003475 == png_ptr->trans_color.gray)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003476 {
3477 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
John Bowler4a12f4a2011-04-17 18:34:22 -05003478 *sp |= (png_byte)(png_ptr->background.gray << shift);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003479 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003480
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003481 else
3482 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003483 png_byte p = (png_byte)((*sp >> shift) & 0x03);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003484 png_byte g = (png_byte)((gamma_table [p | (p << 2) |
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003485 (p << 4) | (p << 6)] >> 6) & 0x03);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003486 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3487 *sp |= (png_byte)(g << shift);
3488 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003489
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003490 if (!shift)
3491 {
3492 shift = 6;
3493 sp++;
3494 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003495
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003496 else
3497 shift -= 2;
Guy Schalnat0d580581995-07-20 02:43:20 -05003498 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003499 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003500
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003501 else
3502#endif
3503 {
3504 sp = row;
3505 shift = 6;
3506 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003507 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003508 if ((png_uint_16)((*sp >> shift) & 0x03)
John Bowler4a12f4a2011-04-17 18:34:22 -05003509 == png_ptr->trans_color.gray)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003510 {
3511 *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
John Bowler4a12f4a2011-04-17 18:34:22 -05003512 *sp |= (png_byte)(png_ptr->background.gray << shift);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003513 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003514
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003515 if (!shift)
3516 {
3517 shift = 6;
3518 sp++;
3519 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003520
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003521 else
3522 shift -= 2;
Guy Schalnat0d580581995-07-20 02:43:20 -05003523 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003524 }
3525 break;
3526 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003527
Guy Schalnat0d580581995-07-20 02:43:20 -05003528 case 4:
3529 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003530#ifdef PNG_READ_GAMMA_SUPPORTED
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003531 if (gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003532 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003533 sp = row;
3534 shift = 4;
3535 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003536 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003537 if ((png_uint_16)((*sp >> shift) & 0x0f)
John Bowler4a12f4a2011-04-17 18:34:22 -05003538 == png_ptr->trans_color.gray)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003539 {
3540 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
John Bowler4a12f4a2011-04-17 18:34:22 -05003541 *sp |= (png_byte)(png_ptr->background.gray << shift);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003542 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003543
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003544 else
3545 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003546 png_byte p = (png_byte)((*sp >> shift) & 0x0f);
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003547 png_byte g = (png_byte)((gamma_table[p |
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003548 (p << 4)] >> 4) & 0x0f);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003549 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3550 *sp |= (png_byte)(g << shift);
3551 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003552
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003553 if (!shift)
3554 {
3555 shift = 4;
3556 sp++;
3557 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003558
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003559 else
3560 shift -= 4;
Guy Schalnat0d580581995-07-20 02:43:20 -05003561 }
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003562 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003563
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003564 else
3565#endif
3566 {
3567 sp = row;
3568 shift = 4;
3569 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003570 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06003571 if ((png_uint_16)((*sp >> shift) & 0x0f)
John Bowler4a12f4a2011-04-17 18:34:22 -05003572 == png_ptr->trans_color.gray)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003573 {
3574 *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
John Bowler4a12f4a2011-04-17 18:34:22 -05003575 *sp |= (png_byte)(png_ptr->background.gray << shift);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003576 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003577
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003578 if (!shift)
3579 {
3580 shift = 4;
3581 sp++;
3582 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003583
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05003584 else
3585 shift -= 4;
Guy Schalnat0d580581995-07-20 02:43:20 -05003586 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003587 }
3588 break;
3589 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003590
Guy Schalnat0d580581995-07-20 02:43:20 -05003591 case 8:
3592 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003593#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003594 if (gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003595 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003596 sp = row;
3597 for (i = 0; i < row_width; i++, sp++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003598 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003599 if (*sp == png_ptr->trans_color.gray)
3600 *sp = (png_byte)png_ptr->background.gray;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003601
Guy Schalnat0d580581995-07-20 02:43:20 -05003602 else
Guy Schalnat0d580581995-07-20 02:43:20 -05003603 *sp = gamma_table[*sp];
Guy Schalnat0d580581995-07-20 02:43:20 -05003604 }
3605 }
3606 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003607#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003608 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003609 sp = row;
3610 for (i = 0; i < row_width; i++, sp++)
Guy Schalnat0d580581995-07-20 02:43:20 -05003611 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003612 if (*sp == png_ptr->trans_color.gray)
3613 *sp = (png_byte)png_ptr->background.gray;
Guy Schalnat0d580581995-07-20 02:43:20 -05003614 }
3615 }
3616 break;
3617 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003618
Guy Schalnat0d580581995-07-20 02:43:20 -05003619 case 16:
3620 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003621#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003622 if (gamma_16 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003623 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003624 sp = row;
3625 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003626 {
3627 png_uint_16 v;
3628
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003629 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003630
John Bowler4a12f4a2011-04-17 18:34:22 -05003631 if (v == png_ptr->trans_color.gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05003632 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003633 /* Background is already in screen gamma */
John Bowler4a12f4a2011-04-17 18:34:22 -05003634 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3635 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003636 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003637
Guy Schalnat0d580581995-07-20 02:43:20 -05003638 else
3639 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003640 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003641 *sp = (png_byte)((v >> 8) & 0xff);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06003642 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003643 }
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003644 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003645 }
3646 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003647#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003648 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003649 sp = row;
3650 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003651 {
3652 png_uint_16 v;
3653
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003654 v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003655
John Bowler4a12f4a2011-04-17 18:34:22 -05003656 if (v == png_ptr->trans_color.gray)
Guy Schalnat0d580581995-07-20 02:43:20 -05003657 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003658 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3659 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003660 }
3661 }
3662 }
3663 break;
3664 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06003665
3666 default:
3667 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05003668 }
3669 break;
3670 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003671
Guy Schalnat0d580581995-07-20 02:43:20 -05003672 case PNG_COLOR_TYPE_RGB:
3673 {
3674 if (row_info->bit_depth == 8)
3675 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003676#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003677 if (gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003678 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003679 sp = row;
3680 for (i = 0; i < row_width; i++, sp += 3)
Guy Schalnat0d580581995-07-20 02:43:20 -05003681 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003682 if (*sp == png_ptr->trans_color.red &&
3683 *(sp + 1) == png_ptr->trans_color.green &&
3684 *(sp + 2) == png_ptr->trans_color.blue)
Guy Schalnat0d580581995-07-20 02:43:20 -05003685 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003686 *sp = (png_byte)png_ptr->background.red;
3687 *(sp + 1) = (png_byte)png_ptr->background.green;
3688 *(sp + 2) = (png_byte)png_ptr->background.blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05003689 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003690
Guy Schalnat0d580581995-07-20 02:43:20 -05003691 else
3692 {
3693 *sp = gamma_table[*sp];
3694 *(sp + 1) = gamma_table[*(sp + 1)];
3695 *(sp + 2) = gamma_table[*(sp + 2)];
3696 }
3697 }
3698 }
3699 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003700#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003701 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003702 sp = row;
3703 for (i = 0; i < row_width; i++, sp += 3)
Guy Schalnat0d580581995-07-20 02:43:20 -05003704 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003705 if (*sp == png_ptr->trans_color.red &&
3706 *(sp + 1) == png_ptr->trans_color.green &&
3707 *(sp + 2) == png_ptr->trans_color.blue)
Guy Schalnat0d580581995-07-20 02:43:20 -05003708 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003709 *sp = (png_byte)png_ptr->background.red;
3710 *(sp + 1) = (png_byte)png_ptr->background.green;
3711 *(sp + 2) = (png_byte)png_ptr->background.blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05003712 }
3713 }
3714 }
3715 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003716 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05003717 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003718#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003719 if (gamma_16 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003720 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003721 sp = row;
3722 for (i = 0; i < row_width; i++, sp += 6)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003723 {
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003724 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003725
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003726 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3727 + *(sp + 3));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003728
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003729 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3730 + *(sp + 5));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003731
John Bowler4a12f4a2011-04-17 18:34:22 -05003732 if (r == png_ptr->trans_color.red &&
3733 g == png_ptr->trans_color.green &&
3734 b == png_ptr->trans_color.blue)
Guy Schalnat0d580581995-07-20 02:43:20 -05003735 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003736 /* Background is already in screen gamma */
John Bowler4a12f4a2011-04-17 18:34:22 -05003737 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3738 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3739 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
3740 *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
3741 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
3742 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003743 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003744
Guy Schalnat0d580581995-07-20 02:43:20 -05003745 else
3746 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003747 png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003748 *sp = (png_byte)((v >> 8) & 0xff);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06003749 *(sp + 1) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003750
Andreas Dilger47a0c421997-05-16 02:46:07 -05003751 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003752 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3753 *(sp + 3) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003754
Andreas Dilger47a0c421997-05-16 02:46:07 -05003755 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003756 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3757 *(sp + 5) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05003758 }
3759 }
3760 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003761
Guy Schalnat0d580581995-07-20 02:43:20 -05003762 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003763#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003764 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003765 sp = row;
3766 for (i = 0; i < row_width; i++, sp += 6)
Guy Schalnat0d580581995-07-20 02:43:20 -05003767 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003768 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003769
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003770 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3771 + *(sp + 3));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003772
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003773 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3774 + *(sp + 5));
Guy Schalnat0d580581995-07-20 02:43:20 -05003775
John Bowler4a12f4a2011-04-17 18:34:22 -05003776 if (r == png_ptr->trans_color.red &&
3777 g == png_ptr->trans_color.green &&
3778 b == png_ptr->trans_color.blue)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003779 {
John Bowler4a12f4a2011-04-17 18:34:22 -05003780 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3781 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3782 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
3783 *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
3784 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
3785 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003786 }
3787 }
3788 }
3789 }
3790 break;
3791 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003792
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06003793 case PNG_COLOR_TYPE_GRAY_ALPHA:
Guy Schalnat0d580581995-07-20 02:43:20 -05003794 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003795 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05003796 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003797#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003798 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3799 gamma_table != NULL)
3800 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003801 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003802 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003803 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003804 png_uint_16 a = *(sp + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003805
Andreas Dilger47a0c421997-05-16 02:46:07 -05003806 if (a == 0xff)
John Bowlerd273ad22011-05-07 21:00:28 -05003807 *sp = gamma_table[*sp];
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003808
Andreas Dilger47a0c421997-05-16 02:46:07 -05003809 else if (a == 0)
3810 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003811 /* Background is already in screen gamma */
John Bowlerd273ad22011-05-07 21:00:28 -05003812 *sp = (png_byte)png_ptr->background.gray;
Andreas Dilger47a0c421997-05-16 02:46:07 -05003813 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003814
Andreas Dilger47a0c421997-05-16 02:46:07 -05003815 else
3816 {
3817 png_byte v, w;
Guy Schalnat0d580581995-07-20 02:43:20 -05003818
Andreas Dilger47a0c421997-05-16 02:46:07 -05003819 v = gamma_to_1[*sp];
John Bowler4a12f4a2011-04-17 18:34:22 -05003820 png_composite(w, v, a, png_ptr->background_1.gray);
John Bowlerd273ad22011-05-07 21:00:28 -05003821 if (!optimize)
3822 w = gamma_from_1[w];
3823 *sp = w;
Guy Schalnat0d580581995-07-20 02:43:20 -05003824 }
3825 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003826 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003827 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003828#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -05003829 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003830 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003831 for (i = 0; i < row_width; i++, sp += 2)
Guy Schalnat0d580581995-07-20 02:43:20 -05003832 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003833 png_byte a = *(sp + 1);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003834
John Bowlerd273ad22011-05-07 21:00:28 -05003835 if (a == 0)
3836 *sp = (png_byte)png_ptr->background.gray;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06003837
John Bowlerd273ad22011-05-07 21:00:28 -05003838 else if (a < 0xff)
3839 png_composite(*sp, *sp, a, png_ptr->background_1.gray);
Guy Schalnat0d580581995-07-20 02:43:20 -05003840 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003841 }
3842 }
3843 else /* if (png_ptr->bit_depth == 16) */
3844 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003845#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003846 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3847 gamma_16_to_1 != NULL)
3848 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003849 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003850 for (i = 0; i < row_width; i++, sp += 4)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003851 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003852 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3853 + *(sp + 3));
Andreas Dilger47a0c421997-05-16 02:46:07 -05003854
Andreas Dilger47a0c421997-05-16 02:46:07 -05003855 if (a == (png_uint_16)0xffff)
3856 {
3857 png_uint_16 v;
3858
3859 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
John Bowlerd273ad22011-05-07 21:00:28 -05003860 *sp = (png_byte)((v >> 8) & 0xff);
3861 *(sp + 1) = (png_byte)(v & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003862 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003863
Andreas Dilger47a0c421997-05-16 02:46:07 -05003864 else if (a == 0)
3865 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003866 /* Background is already in screen gamma */
John Bowlerd273ad22011-05-07 21:00:28 -05003867 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3868 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003869 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003870
Andreas Dilger47a0c421997-05-16 02:46:07 -05003871 else
3872 {
3873 png_uint_16 g, v, w;
3874
3875 g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
John Bowler4a12f4a2011-04-17 18:34:22 -05003876 png_composite_16(v, g, a, png_ptr->background_1.gray);
John Bowlerd273ad22011-05-07 21:00:28 -05003877 if (optimize)
3878 w = v;
3879 else
3880 w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3881 *sp = (png_byte)((w >> 8) & 0xff);
3882 *(sp + 1) = (png_byte)(w & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003883 }
3884 }
3885 }
3886 else
3887#endif
3888 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003889 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003890 for (i = 0; i < row_width; i++, sp += 4)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003891 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06003892 png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3893 + *(sp + 3));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003894
John Bowlerd273ad22011-05-07 21:00:28 -05003895 if (a == 0)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003896 {
John Bowlerd273ad22011-05-07 21:00:28 -05003897 *sp = (png_byte)((png_ptr->background.gray >> 8) & 0xff);
3898 *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003899 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003900
John Bowlerd273ad22011-05-07 21:00:28 -05003901 else if (a < 0xffff)
Andreas Dilger47a0c421997-05-16 02:46:07 -05003902 {
3903 png_uint_16 g, v;
3904
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05003905 g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
John Bowler4a12f4a2011-04-17 18:34:22 -05003906 png_composite_16(v, g, a, png_ptr->background_1.gray);
John Bowlerd273ad22011-05-07 21:00:28 -05003907 *sp = (png_byte)((v >> 8) & 0xff);
3908 *(sp + 1) = (png_byte)(v & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05003909 }
3910 }
Guy Schalnat0d580581995-07-20 02:43:20 -05003911 }
3912 }
3913 break;
3914 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003915
Guy Schalnat0d580581995-07-20 02:43:20 -05003916 case PNG_COLOR_TYPE_RGB_ALPHA:
3917 {
3918 if (row_info->bit_depth == 8)
3919 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003920#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003921 if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3922 gamma_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003923 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003924 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003925 for (i = 0; i < row_width; i++, sp += 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05003926 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003927 png_byte a = *(sp + 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05003928
Guy Schalnat0d580581995-07-20 02:43:20 -05003929 if (a == 0xff)
3930 {
John Bowlerd273ad22011-05-07 21:00:28 -05003931 *sp = gamma_table[*sp];
3932 *(sp + 1) = gamma_table[*(sp + 1)];
3933 *(sp + 2) = gamma_table[*(sp + 2)];
Guy Schalnat0d580581995-07-20 02:43:20 -05003934 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003935
Guy Schalnat0d580581995-07-20 02:43:20 -05003936 else if (a == 0)
3937 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05003938 /* Background is already in screen gamma */
John Bowlerd273ad22011-05-07 21:00:28 -05003939 *sp = (png_byte)png_ptr->background.red;
3940 *(sp + 1) = (png_byte)png_ptr->background.green;
3941 *(sp + 2) = (png_byte)png_ptr->background.blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05003942 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003943
Guy Schalnat0d580581995-07-20 02:43:20 -05003944 else
3945 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05003946 png_byte v, w;
Guy Schalnat0d580581995-07-20 02:43:20 -05003947
3948 v = gamma_to_1[*sp];
John Bowler4a12f4a2011-04-17 18:34:22 -05003949 png_composite(w, v, a, png_ptr->background_1.red);
John Bowlerd273ad22011-05-07 21:00:28 -05003950 if (!optimize) w = gamma_from_1[w];
3951 *sp = w;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003952
Guy Schalnat0d580581995-07-20 02:43:20 -05003953 v = gamma_to_1[*(sp + 1)];
John Bowler4a12f4a2011-04-17 18:34:22 -05003954 png_composite(w, v, a, png_ptr->background_1.green);
John Bowlerd273ad22011-05-07 21:00:28 -05003955 if (!optimize) w = gamma_from_1[w];
3956 *(sp + 1) = w;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003957
Guy Schalnat0d580581995-07-20 02:43:20 -05003958 v = gamma_to_1[*(sp + 2)];
John Bowler4a12f4a2011-04-17 18:34:22 -05003959 png_composite(w, v, a, png_ptr->background_1.blue);
John Bowlerd273ad22011-05-07 21:00:28 -05003960 if (!optimize) w = gamma_from_1[w];
3961 *(sp + 2) = w;
Guy Schalnat0d580581995-07-20 02:43:20 -05003962 }
3963 }
3964 }
3965 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05003966#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05003967 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003968 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05003969 for (i = 0; i < row_width; i++, sp += 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05003970 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05003971 png_byte a = *(sp + 3);
Guy Schalnat0d580581995-07-20 02:43:20 -05003972
John Bowlerd273ad22011-05-07 21:00:28 -05003973 if (a == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05003974 {
John Bowlerd273ad22011-05-07 21:00:28 -05003975 *sp = (png_byte)png_ptr->background.red;
3976 *(sp + 1) = (png_byte)png_ptr->background.green;
3977 *(sp + 2) = (png_byte)png_ptr->background.blue;
Guy Schalnat0d580581995-07-20 02:43:20 -05003978 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003979
John Bowlerd273ad22011-05-07 21:00:28 -05003980 else if (a < 0xff)
Guy Schalnat0d580581995-07-20 02:43:20 -05003981 {
John Bowlerd273ad22011-05-07 21:00:28 -05003982 png_composite(*sp, *sp, a, png_ptr->background.red);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003983
John Bowlerd273ad22011-05-07 21:00:28 -05003984 png_composite(*(sp + 1), *(sp + 1), a,
John Bowler4a12f4a2011-04-17 18:34:22 -05003985 png_ptr->background.green);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05003986
John Bowlerd273ad22011-05-07 21:00:28 -05003987 png_composite(*(sp + 2), *(sp + 2), a,
John Bowler4a12f4a2011-04-17 18:34:22 -05003988 png_ptr->background.blue);
Guy Schalnat0d580581995-07-20 02:43:20 -05003989 }
3990 }
3991 }
3992 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05003993 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05003994 {
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05003995#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05003996 if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3997 gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05003998 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05003999 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05004000 for (i = 0; i < row_width; i++, sp += 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004001 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004002 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
4003 << 8) + (png_uint_16)(*(sp + 7)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004004
Guy Schalnat0d580581995-07-20 02:43:20 -05004005 if (a == (png_uint_16)0xffff)
4006 {
4007 png_uint_16 v;
4008
Andreas Dilger47a0c421997-05-16 02:46:07 -05004009 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
John Bowlerd273ad22011-05-07 21:00:28 -05004010 *sp = (png_byte)((v >> 8) & 0xff);
4011 *(sp + 1) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004012
Andreas Dilger47a0c421997-05-16 02:46:07 -05004013 v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
John Bowlerd273ad22011-05-07 21:00:28 -05004014 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
4015 *(sp + 3) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004016
Andreas Dilger47a0c421997-05-16 02:46:07 -05004017 v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
John Bowlerd273ad22011-05-07 21:00:28 -05004018 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
4019 *(sp + 5) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004020 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004021
Guy Schalnat0d580581995-07-20 02:43:20 -05004022 else if (a == 0)
4023 {
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004024 /* Background is already in screen gamma */
John Bowlerd273ad22011-05-07 21:00:28 -05004025 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
4026 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
4027 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
4028 *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
4029 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
4030 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004031 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004032
Guy Schalnat0d580581995-07-20 02:43:20 -05004033 else
4034 {
John Bowlerd273ad22011-05-07 21:00:28 -05004035 png_uint_16 v, w;
Guy Schalnat0d580581995-07-20 02:43:20 -05004036
Andreas Dilger47a0c421997-05-16 02:46:07 -05004037 v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
John Bowler4a12f4a2011-04-17 18:34:22 -05004038 png_composite_16(w, v, a, png_ptr->background_1.red);
John Bowlerd273ad22011-05-07 21:00:28 -05004039 if (!optimize)
4040 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
4041 *sp = (png_byte)((w >> 8) & 0xff);
4042 *(sp + 1) = (png_byte)(w & 0xff);
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004043
Andreas Dilger47a0c421997-05-16 02:46:07 -05004044 v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
John Bowler4a12f4a2011-04-17 18:34:22 -05004045 png_composite_16(w, v, a, png_ptr->background_1.green);
John Bowlerd273ad22011-05-07 21:00:28 -05004046 if (!optimize)
4047 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004048
John Bowlerd273ad22011-05-07 21:00:28 -05004049 *(sp + 2) = (png_byte)((w >> 8) & 0xff);
4050 *(sp + 3) = (png_byte)(w & 0xff);
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004051
Andreas Dilger47a0c421997-05-16 02:46:07 -05004052 v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
John Bowler4a12f4a2011-04-17 18:34:22 -05004053 png_composite_16(w, v, a, png_ptr->background_1.blue);
John Bowlerd273ad22011-05-07 21:00:28 -05004054 if (!optimize)
4055 w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004056
John Bowlerd273ad22011-05-07 21:00:28 -05004057 *(sp + 4) = (png_byte)((w >> 8) & 0xff);
4058 *(sp + 5) = (png_byte)(w & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004059 }
4060 }
4061 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004062
Guy Schalnat0d580581995-07-20 02:43:20 -05004063 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004064#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004065 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004066 sp = row;
John Bowlerd273ad22011-05-07 21:00:28 -05004067 for (i = 0; i < row_width; i++, sp += 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004068 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004069 png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004070 << 8) + (png_uint_16)(*(sp + 7)));
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004071
John Bowlerd273ad22011-05-07 21:00:28 -05004072 if (a == 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004073 {
John Bowlerd273ad22011-05-07 21:00:28 -05004074 *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
4075 *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
4076 *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) & 0xff);
4077 *(sp + 3) = (png_byte)(png_ptr->background.green & 0xff);
4078 *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) & 0xff);
4079 *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004080 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004081
John Bowlerd273ad22011-05-07 21:00:28 -05004082 else if (a < 0xffff)
Guy Schalnat0d580581995-07-20 02:43:20 -05004083 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004084 png_uint_16 v;
Guy Schalnat0d580581995-07-20 02:43:20 -05004085
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05004086 png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
4087 png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
4088 + *(sp + 3));
4089 png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
4090 + *(sp + 5));
Andreas Dilger47a0c421997-05-16 02:46:07 -05004091
John Bowler4a12f4a2011-04-17 18:34:22 -05004092 png_composite_16(v, r, a, png_ptr->background.red);
John Bowlerd273ad22011-05-07 21:00:28 -05004093 *sp = (png_byte)((v >> 8) & 0xff);
4094 *(sp + 1) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004095
John Bowler4a12f4a2011-04-17 18:34:22 -05004096 png_composite_16(v, g, a, png_ptr->background.green);
John Bowlerd273ad22011-05-07 21:00:28 -05004097 *(sp + 2) = (png_byte)((v >> 8) & 0xff);
4098 *(sp + 3) = (png_byte)(v & 0xff);
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004099
John Bowler4a12f4a2011-04-17 18:34:22 -05004100 png_composite_16(v, b, a, png_ptr->background.blue);
John Bowlerd273ad22011-05-07 21:00:28 -05004101 *(sp + 4) = (png_byte)((v >> 8) & 0xff);
4102 *(sp + 5) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004103 }
4104 }
4105 }
4106 }
4107 break;
4108 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004109
4110 default:
4111 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004112 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004113 }
4114}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004115#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004116
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004117#ifdef PNG_READ_GAMMA_SUPPORTED
Andreas Dilger47a0c421997-05-16 02:46:07 -05004118/* Gamma correct the image, avoiding the alpha channel. Make sure
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -05004119 * you do this after you deal with the transparency issue on grayscale
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -05004120 * or RGB images. If your bit depth is 8, use gamma_table, if it
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004121 * is 16, use gamma_16_table and gamma_shift. Build these with
4122 * build_gamma_table().
4123 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004124void /* PRIVATE */
John Bowler4a12f4a2011-04-17 18:34:22 -05004125png_do_gamma(png_row_infop row_info, png_bytep row, png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05004126{
John Bowler4a12f4a2011-04-17 18:34:22 -05004127 png_const_bytep gamma_table = png_ptr->gamma_table;
4128 png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
4129 int gamma_shift = png_ptr->gamma_shift;
4130
Guy Schalnat6d764711995-12-19 03:22:19 -06004131 png_bytep sp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004132 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004133 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004134
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004135 png_debug(1, "in png_do_gamma");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004136
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004137 if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004138 (row_info->bit_depth == 16 && gamma_16_table != NULL)))
Guy Schalnat0d580581995-07-20 02:43:20 -05004139 {
4140 switch (row_info->color_type)
4141 {
4142 case PNG_COLOR_TYPE_RGB:
4143 {
4144 if (row_info->bit_depth == 8)
4145 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004146 sp = row;
4147 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004148 {
4149 *sp = gamma_table[*sp];
4150 sp++;
4151 *sp = gamma_table[*sp];
4152 sp++;
4153 *sp = gamma_table[*sp];
4154 sp++;
4155 }
4156 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004157
Andreas Dilger47a0c421997-05-16 02:46:07 -05004158 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05004159 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004160 sp = row;
4161 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004162 {
4163 png_uint_16 v;
4164
Andreas Dilger47a0c421997-05-16 02:46:07 -05004165 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004166 *sp = (png_byte)((v >> 8) & 0xff);
4167 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004168 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004169
Andreas Dilger47a0c421997-05-16 02:46:07 -05004170 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004171 *sp = (png_byte)((v >> 8) & 0xff);
4172 *(sp + 1) = (png_byte)(v & 0xff);
4173 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004174
Andreas Dilger47a0c421997-05-16 02:46:07 -05004175 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004176 *sp = (png_byte)((v >> 8) & 0xff);
4177 *(sp + 1) = (png_byte)(v & 0xff);
4178 sp += 2;
4179 }
4180 }
4181 break;
4182 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004183
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004184 case PNG_COLOR_TYPE_RGB_ALPHA:
4185 {
4186 if (row_info->bit_depth == 8)
4187 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004188 sp = row;
4189 for (i = 0; i < row_width; i++)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004190 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004191 *sp = gamma_table[*sp];
4192 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004193
Guy Schalnat0d580581995-07-20 02:43:20 -05004194 *sp = gamma_table[*sp];
4195 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004196
Guy Schalnat0d580581995-07-20 02:43:20 -05004197 *sp = gamma_table[*sp];
4198 sp++;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004199
Guy Schalnat0d580581995-07-20 02:43:20 -05004200 sp++;
4201 }
4202 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004203
Andreas Dilger47a0c421997-05-16 02:46:07 -05004204 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05004205 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004206 sp = row;
4207 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004208 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004209 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004210 *sp = (png_byte)((v >> 8) & 0xff);
4211 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004212 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004213
Andreas Dilger47a0c421997-05-16 02:46:07 -05004214 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004215 *sp = (png_byte)((v >> 8) & 0xff);
4216 *(sp + 1) = (png_byte)(v & 0xff);
4217 sp += 2;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004218
Andreas Dilger47a0c421997-05-16 02:46:07 -05004219 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004220 *sp = (png_byte)((v >> 8) & 0xff);
4221 *(sp + 1) = (png_byte)(v & 0xff);
4222 sp += 4;
4223 }
4224 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004225 break;
4226 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004227
Guy Schalnat0d580581995-07-20 02:43:20 -05004228 case PNG_COLOR_TYPE_GRAY_ALPHA:
4229 {
4230 if (row_info->bit_depth == 8)
4231 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004232 sp = row;
4233 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004234 {
4235 *sp = gamma_table[*sp];
Andreas Dilger47a0c421997-05-16 02:46:07 -05004236 sp += 2;
Guy Schalnat0d580581995-07-20 02:43:20 -05004237 }
4238 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004239
Andreas Dilger47a0c421997-05-16 02:46:07 -05004240 else /* if (row_info->bit_depth == 16) */
Guy Schalnat0d580581995-07-20 02:43:20 -05004241 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004242 sp = row;
4243 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004244 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004245 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004246 *sp = (png_byte)((v >> 8) & 0xff);
4247 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004248 sp += 4;
4249 }
4250 }
4251 break;
4252 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004253
Guy Schalnat0d580581995-07-20 02:43:20 -05004254 case PNG_COLOR_TYPE_GRAY:
4255 {
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004256 if (row_info->bit_depth == 2)
4257 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004258 sp = row;
4259 for (i = 0; i < row_width; i += 4)
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004260 {
4261 int a = *sp & 0xc0;
4262 int b = *sp & 0x30;
4263 int c = *sp & 0x0c;
4264 int d = *sp & 0x03;
4265
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05004266 *sp = (png_byte)(
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06004267 ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
4268 ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
4269 ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
4270 ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06004271 sp++;
4272 }
4273 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004274
Andreas Dilger47a0c421997-05-16 02:46:07 -05004275 if (row_info->bit_depth == 4)
Guy Schalnat0d580581995-07-20 02:43:20 -05004276 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004277 sp = row;
4278 for (i = 0; i < row_width; i += 2)
Andreas Dilger47a0c421997-05-16 02:46:07 -05004279 {
4280 int msb = *sp & 0xf0;
4281 int lsb = *sp & 0x0f;
4282
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -05004283 *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
Glenn Randers-Pehrsone3f3c4e2010-02-07 18:08:50 -06004284 | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
Andreas Dilger47a0c421997-05-16 02:46:07 -05004285 sp++;
4286 }
4287 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004288
Andreas Dilger47a0c421997-05-16 02:46:07 -05004289 else if (row_info->bit_depth == 8)
4290 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004291 sp = row;
4292 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004293 {
4294 *sp = gamma_table[*sp];
4295 sp++;
4296 }
4297 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004298
Guy Schalnat0d580581995-07-20 02:43:20 -05004299 else if (row_info->bit_depth == 16)
4300 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004301 sp = row;
4302 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004303 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05004304 png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004305 *sp = (png_byte)((v >> 8) & 0xff);
4306 *(sp + 1) = (png_byte)(v & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004307 sp += 2;
4308 }
4309 }
4310 break;
4311 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004312
4313 default:
4314 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004315 }
4316 }
4317}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004318#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004319
John Bowlerd273ad22011-05-07 21:00:28 -05004320#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4321/* Encode the alpha channel to the output gamma (the input channel is always
4322 * linear.) Called only with color types that have an alpha channel. Needs the
4323 * from_1 tables.
4324 */
4325void /* PRIVATE */
4326png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structp png_ptr)
4327{
4328 png_uint_32 row_width = row_info->width;
4329
4330 png_debug(1, "in png_do_encode_alpha");
4331
4332 if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
4333 {
4334 if (row_info->bit_depth == 8)
4335 {
4336 PNG_CONST png_bytep table = png_ptr->gamma_from_1;
4337
4338 if (table != NULL)
4339 {
4340 PNG_CONST int step =
4341 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
4342
4343 /* The alpha channel is the last component: */
4344 row += step - 1;
4345
4346 for (; row_width > 0; --row_width, row += step)
4347 *row = table[*row];
4348
4349 return;
4350 }
4351 }
4352
4353 else if (row_info->bit_depth == 16)
4354 {
4355 PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1;
4356 PNG_CONST int gamma_shift = png_ptr->gamma_shift;
4357
4358 if (table != NULL)
4359 {
4360 PNG_CONST int step =
4361 (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
4362
4363 /* The alpha channel is the last component: */
4364 row += step - 2;
4365
4366 for (; row_width > 0; --row_width, row += step)
4367 {
4368 png_uint_16 v;
4369
4370 v = table[*(row + 1) >> gamma_shift][*row];
4371 *row = (png_byte)((v >> 8) & 0xff);
4372 *(row + 1) = (png_byte)(v & 0xff);
4373 }
4374
4375 return;
4376 }
4377 }
4378 }
4379
4380 /* Only get to here if called with a weird row_info; no harm has been done,
4381 * so just issue a warning.
4382 */
4383 png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
4384}
4385#endif
4386
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004387#ifdef PNG_READ_EXPAND_SUPPORTED
Glenn Randers-Pehrson352ca6b1999-09-18 15:49:20 -05004388/* Expands a palette row to an RGB or RGBA row depending
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004389 * upon whether you supply trans and num_trans.
4390 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004391void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06004392png_do_expand_palette(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05004393 png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
Guy Schalnat0d580581995-07-20 02:43:20 -05004394{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004395 int shift, value;
Guy Schalnat6d764711995-12-19 03:22:19 -06004396 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004397 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004398 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004399
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004400 png_debug(1, "in png_do_expand_palette");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004401
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004402 if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
Guy Schalnat0d580581995-07-20 02:43:20 -05004403 {
4404 if (row_info->bit_depth < 8)
4405 {
4406 switch (row_info->bit_depth)
4407 {
4408 case 1:
4409 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004410 sp = row + (png_size_t)((row_width - 1) >> 3);
4411 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004412 shift = 7 - (int)((row_width + 7) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004413 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004414 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004415 if ((*sp >> shift) & 0x01)
Guy Schalnat0d580581995-07-20 02:43:20 -05004416 *dp = 1;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004417
Guy Schalnat0d580581995-07-20 02:43:20 -05004418 else
4419 *dp = 0;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004420
Guy Schalnat0d580581995-07-20 02:43:20 -05004421 if (shift == 7)
4422 {
4423 shift = 0;
4424 sp--;
4425 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004426
Guy Schalnat0d580581995-07-20 02:43:20 -05004427 else
4428 shift++;
4429
4430 dp--;
4431 }
4432 break;
4433 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004434
Guy Schalnat0d580581995-07-20 02:43:20 -05004435 case 2:
4436 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004437 sp = row + (png_size_t)((row_width - 1) >> 2);
4438 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004439 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004440 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004441 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004442 value = (*sp >> shift) & 0x03;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004443 *dp = (png_byte)value;
Guy Schalnat0d580581995-07-20 02:43:20 -05004444 if (shift == 6)
4445 {
4446 shift = 0;
4447 sp--;
4448 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004449
Guy Schalnat0d580581995-07-20 02:43:20 -05004450 else
4451 shift += 2;
4452
4453 dp--;
4454 }
4455 break;
4456 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004457
Guy Schalnat0d580581995-07-20 02:43:20 -05004458 case 4:
4459 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004460 sp = row + (png_size_t)((row_width - 1) >> 1);
4461 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004462 shift = (int)((row_width & 0x01) << 2);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004463 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004464 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004465 value = (*sp >> shift) & 0x0f;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004466 *dp = (png_byte)value;
Guy Schalnat0d580581995-07-20 02:43:20 -05004467 if (shift == 4)
4468 {
4469 shift = 0;
4470 sp--;
4471 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004472
Guy Schalnat0d580581995-07-20 02:43:20 -05004473 else
4474 shift += 4;
4475
4476 dp--;
4477 }
4478 break;
4479 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004480
4481 default:
4482 break;
Guy Schalnat0d580581995-07-20 02:43:20 -05004483 }
4484 row_info->bit_depth = 8;
4485 row_info->pixel_depth = 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004486 row_info->rowbytes = row_width;
Guy Schalnat0d580581995-07-20 02:43:20 -05004487 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004488
John Bowler4e230b02011-01-08 14:49:25 -06004489 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004490 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004491 {
John Bowlercd3b0cc2011-06-14 23:01:07 -05004492 if (num_trans > 0)
Guy Schalnat0d580581995-07-20 02:43:20 -05004493 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004494 sp = row + (png_size_t)row_width - 1;
4495 dp = row + (png_size_t)(row_width << 2) - 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05004496
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004497 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004498 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05004499 if ((int)(*sp) >= num_trans)
Guy Schalnat0d580581995-07-20 02:43:20 -05004500 *dp-- = 0xff;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004501
Guy Schalnat0d580581995-07-20 02:43:20 -05004502 else
Glenn Randers-Pehrson6abea752009-08-08 16:52:06 -05004503 *dp-- = trans_alpha[*sp];
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004504
Guy Schalnat0d580581995-07-20 02:43:20 -05004505 *dp-- = palette[*sp].blue;
4506 *dp-- = palette[*sp].green;
4507 *dp-- = palette[*sp].red;
4508 sp--;
4509 }
4510 row_info->bit_depth = 8;
4511 row_info->pixel_depth = 32;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004512 row_info->rowbytes = row_width * 4;
Guy Schalnat0d580581995-07-20 02:43:20 -05004513 row_info->color_type = 6;
4514 row_info->channels = 4;
4515 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004516
Guy Schalnat0d580581995-07-20 02:43:20 -05004517 else
4518 {
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004519 sp = row + (png_size_t)row_width - 1;
4520 dp = row + (png_size_t)(row_width * 3) - 1;
Guy Schalnat0d580581995-07-20 02:43:20 -05004521
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004522 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004523 {
4524 *dp-- = palette[*sp].blue;
4525 *dp-- = palette[*sp].green;
4526 *dp-- = palette[*sp].red;
4527 sp--;
4528 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004529
Guy Schalnat0d580581995-07-20 02:43:20 -05004530 row_info->bit_depth = 8;
4531 row_info->pixel_depth = 24;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004532 row_info->rowbytes = row_width * 3;
Guy Schalnat0d580581995-07-20 02:43:20 -05004533 row_info->color_type = 2;
4534 row_info->channels = 3;
4535 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004536 }
4537 }
4538 }
4539}
4540
Glenn Randers-Pehrson1ce08362006-03-08 23:35:59 -06004541/* If the bit depth < 8, it is expanded to 8. Also, if the already
4542 * expanded transparency value is supplied, an alpha channel is built.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06004543 */
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004544void /* PRIVATE */
Guy Schalnat6d764711995-12-19 03:22:19 -06004545png_do_expand(png_row_infop row_info, png_bytep row,
John Bowlercd3b0cc2011-06-14 23:01:07 -05004546 png_const_color_16p trans_color)
Guy Schalnat0d580581995-07-20 02:43:20 -05004547{
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004548 int shift, value;
Guy Schalnat6d764711995-12-19 03:22:19 -06004549 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004550 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004551 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004552
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004553 png_debug(1, "in png_do_expand");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004554
Guy Schalnat0d580581995-07-20 02:43:20 -05004555 {
Andreas Dilger47a0c421997-05-16 02:46:07 -05004556 if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
Guy Schalnat0d580581995-07-20 02:43:20 -05004557 {
John Bowlercd3b0cc2011-06-14 23:01:07 -05004558 png_uint_16 gray = (png_uint_16)(trans_color ? trans_color->gray : 0);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004559
4560 if (row_info->bit_depth < 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004561 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004562 switch (row_info->bit_depth)
Guy Schalnat0d580581995-07-20 02:43:20 -05004563 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004564 case 1:
4565 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004566 gray = (png_uint_16)((gray & 0x01) * 0xff);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004567 sp = row + (png_size_t)((row_width - 1) >> 3);
4568 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004569 shift = 7 - (int)((row_width + 7) & 0x07);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004570 for (i = 0; i < row_width; i++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004571 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004572 if ((*sp >> shift) & 0x01)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004573 *dp = 0xff;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004574
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004575 else
4576 *dp = 0;
Glenn Randers-Pehrsonf1eb9182010-02-08 15:36:41 -06004577
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004578 if (shift == 7)
4579 {
4580 shift = 0;
4581 sp--;
4582 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004583
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004584 else
4585 shift++;
4586
4587 dp--;
4588 }
4589 break;
4590 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004591
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004592 case 2:
4593 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004594 gray = (png_uint_16)((gray & 0x03) * 0x55);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004595 sp = row + (png_size_t)((row_width - 1) >> 2);
4596 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004597 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004598 for (i = 0; i < row_width; i++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004599 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004600 value = (*sp >> shift) & 0x03;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004601 *dp = (png_byte)(value | (value << 2) | (value << 4) |
4602 (value << 6));
4603 if (shift == 6)
4604 {
4605 shift = 0;
4606 sp--;
4607 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004608
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004609 else
4610 shift += 2;
4611
4612 dp--;
4613 }
4614 break;
4615 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004616
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004617 case 4:
4618 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004619 gray = (png_uint_16)((gray & 0x0f) * 0x11);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004620 sp = row + (png_size_t)((row_width - 1) >> 1);
4621 dp = row + (png_size_t)row_width - 1;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004622 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004623 for (i = 0; i < row_width; i++)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004624 {
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06004625 value = (*sp >> shift) & 0x0f;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004626 *dp = (png_byte)(value | (value << 4));
4627 if (shift == 4)
4628 {
4629 shift = 0;
4630 sp--;
4631 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004632
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004633 else
4634 shift = 4;
4635
4636 dp--;
4637 }
4638 break;
4639 }
Glenn Randers-Pehrsonb3edc732010-11-21 14:06:41 -06004640
4641 default:
4642 break;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004643 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004644
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004645 row_info->bit_depth = 8;
4646 row_info->pixel_depth = 8;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004647 row_info->rowbytes = row_width;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004648 }
4649
John Bowlercd3b0cc2011-06-14 23:01:07 -05004650 if (trans_color != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004651 {
4652 if (row_info->bit_depth == 8)
4653 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004654 gray = gray & 0xff;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004655 sp = row + (png_size_t)row_width - 1;
4656 dp = row + (png_size_t)(row_width << 1) - 1;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004657
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004658 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004659 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004660 if (*sp == gray)
4661 *dp-- = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004662
Guy Schalnat0d580581995-07-20 02:43:20 -05004663 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004664 *dp-- = 0xff;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004665
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004666 *dp-- = *sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004667 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004668 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004669
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004670 else if (row_info->bit_depth == 16)
Guy Schalnat0d580581995-07-20 02:43:20 -05004671 {
Glenn Randers-Pehrson67439c42010-08-19 07:01:09 -05004672 png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
4673 png_byte gray_low = (png_byte)(gray & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004674 sp = row + row_info->rowbytes - 1;
4675 dp = row + (row_info->rowbytes << 1) - 1;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004676 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004677 {
Glenn Randers-Pehrson79134c62009-02-14 10:32:18 -06004678 if (*(sp - 1) == gray_high && *(sp) == gray_low)
Guy Schalnat0d580581995-07-20 02:43:20 -05004679 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004680 *dp-- = 0;
4681 *dp-- = 0;
Guy Schalnat0d580581995-07-20 02:43:20 -05004682 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004683
Guy Schalnat0d580581995-07-20 02:43:20 -05004684 else
Guy Schalnat0d580581995-07-20 02:43:20 -05004685 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004686 *dp-- = 0xff;
4687 *dp-- = 0xff;
Guy Schalnat0d580581995-07-20 02:43:20 -05004688 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004689
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004690 *dp-- = *sp--;
4691 *dp-- = *sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004692 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004693 }
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004694
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06004695 row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4696 row_info->channels = 2;
4697 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
Glenn Randers-Pehrson272489d2004-08-04 06:34:52 -05004698 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4699 row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004700 }
Guy Schalnat0d580581995-07-20 02:43:20 -05004701 }
John Bowlercd3b0cc2011-06-14 23:01:07 -05004702 else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color)
Guy Schalnat0d580581995-07-20 02:43:20 -05004703 {
4704 if (row_info->bit_depth == 8)
4705 {
John Bowlercd3b0cc2011-06-14 23:01:07 -05004706 png_byte red = (png_byte)(trans_color->red & 0xff);
4707 png_byte green = (png_byte)(trans_color->green & 0xff);
4708 png_byte blue = (png_byte)(trans_color->blue & 0xff);
Guy Schalnat0d580581995-07-20 02:43:20 -05004709 sp = row + (png_size_t)row_info->rowbytes - 1;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004710 dp = row + (png_size_t)(row_width << 2) - 1;
4711 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004712 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004713 if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
Guy Schalnat0d580581995-07-20 02:43:20 -05004714 *dp-- = 0;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004715
Guy Schalnat0d580581995-07-20 02:43:20 -05004716 else
4717 *dp-- = 0xff;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004718
Guy Schalnat0d580581995-07-20 02:43:20 -05004719 *dp-- = *sp--;
4720 *dp-- = *sp--;
4721 *dp-- = *sp--;
4722 }
4723 }
4724 else if (row_info->bit_depth == 16)
4725 {
John Bowlercd3b0cc2011-06-14 23:01:07 -05004726 png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
4727 png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
4728 png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
4729 png_byte red_low = (png_byte)(trans_color->red & 0xff);
4730 png_byte green_low = (png_byte)(trans_color->green & 0xff);
4731 png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -05004732 sp = row + row_info->rowbytes - 1;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004733 dp = row + (png_size_t)(row_width << 3) - 1;
4734 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004735 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004736 if (*(sp - 5) == red_high &&
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004737 *(sp - 4) == red_low &&
4738 *(sp - 3) == green_high &&
4739 *(sp - 2) == green_low &&
4740 *(sp - 1) == blue_high &&
4741 *(sp ) == blue_low)
Guy Schalnat0d580581995-07-20 02:43:20 -05004742 {
4743 *dp-- = 0;
4744 *dp-- = 0;
4745 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004746
Guy Schalnat0d580581995-07-20 02:43:20 -05004747 else
4748 {
4749 *dp-- = 0xff;
4750 *dp-- = 0xff;
4751 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004752
Guy Schalnat0d580581995-07-20 02:43:20 -05004753 *dp-- = *sp--;
4754 *dp-- = *sp--;
4755 *dp-- = *sp--;
4756 *dp-- = *sp--;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004757 *dp-- = *sp--;
Guy Schalnat0d580581995-07-20 02:43:20 -05004758 *dp-- = *sp--;
4759 }
4760 }
4761 row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4762 row_info->channels = 4;
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004763 row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004764 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004765 }
4766 }
4767}
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004768#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05004769
John Bowler4d562962011-02-12 09:01:20 -06004770#ifdef PNG_READ_EXPAND_16_SUPPORTED
Glenn Randers-Pehrson8b8aacd2011-08-25 18:10:50 -05004771/* If the bit depth is 8 and the color type is not a palette type expand the
John Bowler4d562962011-02-12 09:01:20 -06004772 * whole row to 16 bits. Has no effect otherwise.
4773 */
4774void /* PRIVATE */
4775png_do_expand_16(png_row_infop row_info, png_bytep row)
4776{
4777 if (row_info->bit_depth == 8 &&
4778 row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4779 {
4780 /* The row have a sequence of bytes containing [0..255] and we need
4781 * to turn it into another row containing [0..65535], to do this we
4782 * calculate:
4783 *
4784 * (input / 255) * 65535
4785 *
4786 * Which happens to be exactly input * 257 and this can be achieved
4787 * simply by byte replication in place (copying backwards).
4788 */
4789 png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4790 png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
4791 while (dp > sp)
4792 dp[-2] = dp[-1] = *--sp, dp -= 2;
4793
4794 row_info->rowbytes *= 2;
4795 row_info->bit_depth = 16;
4796 row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4797 }
4798}
4799#endif
4800
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004801#ifdef PNG_READ_QUANTIZE_SUPPORTED
Glenn Randers-Pehrson75294572000-05-06 14:09:57 -05004802void /* PRIVATE */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004803png_do_quantize(png_row_infop row_info, png_bytep row,
Glenn Randers-Pehrsone600c512010-08-18 07:25:46 -05004804 png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
Guy Schalnat0d580581995-07-20 02:43:20 -05004805{
Guy Schalnat6d764711995-12-19 03:22:19 -06004806 png_bytep sp, dp;
Guy Schalnat51f0eb41995-09-26 05:22:39 -05004807 png_uint_32 i;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004808 png_uint_32 row_width=row_info->width;
Guy Schalnat6d764711995-12-19 03:22:19 -06004809
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004810 png_debug(1, "in png_do_quantize");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004811
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004812 if (row_info->bit_depth == 8)
Guy Schalnat0d580581995-07-20 02:43:20 -05004813 {
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004814 if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
Guy Schalnat0d580581995-07-20 02:43:20 -05004815 {
4816 int r, g, b, p;
Guy Schalnat0d580581995-07-20 02:43:20 -05004817 sp = row;
4818 dp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004819 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004820 {
4821 r = *sp++;
4822 g = *sp++;
4823 b = *sp++;
4824
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004825 /* This looks real messy, but the compiler will reduce
4826 * it down to a reasonable formula. For example, with
4827 * 5 bits per color, we get:
4828 * p = (((r >> 3) & 0x1f) << 10) |
4829 * (((g >> 3) & 0x1f) << 5) |
4830 * ((b >> 3) & 0x1f);
4831 */
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004832 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4833 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4834 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4835 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4836 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4837 (PNG_QUANTIZE_BLUE_BITS)) |
4838 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4839 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
Guy Schalnat0d580581995-07-20 02:43:20 -05004840
4841 *dp++ = palette_lookup[p];
4842 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004843
Guy Schalnat0d580581995-07-20 02:43:20 -05004844 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4845 row_info->channels = 1;
4846 row_info->pixel_depth = row_info->bit_depth;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004847 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004848 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004849
Guy Schalnat0d580581995-07-20 02:43:20 -05004850 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004851 palette_lookup != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05004852 {
4853 int r, g, b, p;
Guy Schalnat0d580581995-07-20 02:43:20 -05004854 sp = row;
4855 dp = row;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004856 for (i = 0; i < row_width; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004857 {
4858 r = *sp++;
4859 g = *sp++;
4860 b = *sp++;
4861 sp++;
4862
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004863 p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4864 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4865 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4866 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4867 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4868 (PNG_QUANTIZE_BLUE_BITS)) |
4869 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4870 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
Guy Schalnat0d580581995-07-20 02:43:20 -05004871
4872 *dp++ = palette_lookup[p];
4873 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004874
Guy Schalnat0d580581995-07-20 02:43:20 -05004875 row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4876 row_info->channels = 1;
4877 row_info->pixel_depth = row_info->bit_depth;
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004878 row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
Guy Schalnat0d580581995-07-20 02:43:20 -05004879 }
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004880
Guy Schalnat0d580581995-07-20 02:43:20 -05004881 else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
Glenn Randers-Pehrson925d23b2010-08-26 21:43:18 -05004882 quantize_lookup)
Guy Schalnatb2e01bd1996-01-26 01:38:47 -06004883 {
Guy Schalnat0d580581995-07-20 02:43:20 -05004884 sp = row;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004885
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05004886 for (i = 0; i < row_width; i++, sp++)
Guy Schalnat0d580581995-07-20 02:43:20 -05004887 {
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004888 *sp = quantize_lookup[*sp];
Guy Schalnat0d580581995-07-20 02:43:20 -05004889 }
4890 }
4891 }
4892}
Glenn Randers-Pehrson3cd7cff2010-04-16 19:27:08 -05004893#endif /* PNG_READ_QUANTIZE_SUPPORTED */
John Bowler4a12f4a2011-04-17 18:34:22 -05004894#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
Guy Schalnat0d580581995-07-20 02:43:20 -05004895
Glenn Randers-Pehrsone26c0952009-09-23 11:22:08 -05004896#ifdef PNG_MNG_FEATURES_SUPPORTED
Glenn Randers-Pehrson4bb4d012009-05-20 12:45:29 -05004897/* Undoes intrapixel differencing */
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004898void /* PRIVATE */
4899png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4900{
Glenn Randers-Pehrson51650b82008-08-05 07:44:42 -05004901 png_debug(1, "in png_do_read_intrapixel");
Glenn Randers-Pehrsonda009802009-08-15 13:25:47 -05004902
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004903 if (
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004904 (row_info->color_type & PNG_COLOR_MASK_COLOR))
4905 {
4906 int bytes_per_pixel;
4907 png_uint_32 row_width = row_info->width;
Glenn Randers-Pehrsonf24daf22010-05-06 09:44:04 -05004908
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004909 if (row_info->bit_depth == 8)
4910 {
4911 png_bytep rp;
4912 png_uint_32 i;
4913
4914 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4915 bytes_per_pixel = 3;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004916
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004917 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4918 bytes_per_pixel = 4;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004919
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004920 else
4921 return;
4922
4923 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4924 {
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004925 *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
4926 *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004927 }
4928 }
4929 else if (row_info->bit_depth == 16)
4930 {
4931 png_bytep rp;
4932 png_uint_32 i;
4933
4934 if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4935 bytes_per_pixel = 6;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004936
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004937 else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4938 bytes_per_pixel = 8;
Glenn Randers-Pehrson glennrp@comcast.netb1c0d332009-05-15 20:39:34 -05004939
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004940 else
4941 return;
4942
4943 for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4944 {
Glenn Randers-Pehrson145f5c82008-07-10 09:13:13 -05004945 png_uint_32 s0 = (*(rp ) << 8) | *(rp + 1);
4946 png_uint_32 s1 = (*(rp + 2) << 8) | *(rp + 3);
4947 png_uint_32 s2 = (*(rp + 4) << 8) | *(rp + 5);
John Bowler75156122011-09-09 17:21:44 -05004948 png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
4949 png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
Glenn Randers-Pehrsonbc363ec2010-10-12 21:17:00 -05004950 *(rp ) = (png_byte)((red >> 8) & 0xff);
Glenn Randers-Pehrson3ea36752010-03-03 09:35:31 -06004951 *(rp + 1) = (png_byte)(red & 0xff);
4952 *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
4953 *(rp + 5) = (png_byte)(blue & 0xff);
Glenn Randers-Pehrson2ad31ae2000-12-15 08:54:42 -06004954 }
4955 }
4956 }
4957}
4958#endif /* PNG_MNG_FEATURES_SUPPORTED */
Glenn Randers-Pehrson9c3ab682006-02-20 22:09:05 -06004959#endif /* PNG_READ_SUPPORTED */