author | azvegint |
Wed, 10 Aug 2016 02:50:58 +0300 | |
changeset 40436 | 94ae0ede53a5 |
parent 35692 | ee66ced7896c |
child 43321 | 2b8d397c03a4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 6 |
* published by the Free Software Foundation. Oracle designates this |
2 | 7 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 8 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 9 |
* |
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
14 |
* accompanied this code). |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License version |
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
19 |
* |
|
5506 | 20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
21 |
* or visit www.oracle.com if you need additional information or have any |
|
22 |
* questions. |
|
2 | 23 |
*/ |
24 |
||
25 |
/* pngrtran.c - transforms the data in a row for PNG readers |
|
26 |
* |
|
27 |
* This file is available under and governed by the GNU General Public |
|
28 |
* License version 2 only, as published by the Free Software Foundation. |
|
29 |
* However, the following notice accompanied the original version of this |
|
30 |
* file and, per its terms, should not be removed: |
|
31 |
* |
|
40436
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
32 |
* Last changed in libpng 1.6.22 [May 26, 2016] |
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
33 |
* Copyright (c) 1998-2002,2004,2006-2016 Glenn Randers-Pehrson |
2 | 34 |
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
35 |
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
|
36 |
* |
|
10576 | 37 |
* This code is released under the libpng license. |
38 |
* For conditions of distribution and use, see the disclaimer |
|
39 |
* and license in png.h |
|
40 |
* |
|
2 | 41 |
* This file contains functions optionally called by an application |
42 |
* in order to tell libpng how to handle data when reading a PNG. |
|
43 |
* Transformations that are used in both reading and writing are |
|
44 |
* in pngtrans.c. |
|
45 |
*/ |
|
46 |
||
10576 | 47 |
#include "pngpriv.h" |
48 |
||
49 |
#ifdef PNG_READ_SUPPORTED |
|
2 | 50 |
|
51 |
/* Set the action on getting a CRC error for an ancillary or critical chunk. */ |
|
52 |
void PNGAPI |
|
29913 | 53 |
png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) |
2 | 54 |
{ |
10576 | 55 |
png_debug(1, "in png_set_crc_action"); |
56 |
||
57 |
if (png_ptr == NULL) |
|
58 |
return; |
|
59 |
||
2 | 60 |
/* Tell libpng how we react to CRC errors in critical chunks */ |
61 |
switch (crit_action) |
|
62 |
{ |
|
10576 | 63 |
case PNG_CRC_NO_CHANGE: /* Leave setting as is */ |
2 | 64 |
break; |
10576 | 65 |
|
66 |
case PNG_CRC_WARN_USE: /* Warn/use data */ |
|
2 | 67 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
68 |
png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; |
|
69 |
break; |
|
10576 | 70 |
|
71 |
case PNG_CRC_QUIET_USE: /* Quiet/use data */ |
|
2 | 72 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
73 |
png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | |
|
74 |
PNG_FLAG_CRC_CRITICAL_IGNORE; |
|
75 |
break; |
|
10576 | 76 |
|
77 |
case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ |
|
78 |
png_warning(png_ptr, |
|
79 |
"Can't discard critical data on CRC error"); |
|
80 |
case PNG_CRC_ERROR_QUIT: /* Error/quit */ |
|
81 |
||
2 | 82 |
case PNG_CRC_DEFAULT: |
83 |
default: |
|
84 |
png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; |
|
85 |
break; |
|
86 |
} |
|
87 |
||
10576 | 88 |
/* Tell libpng how we react to CRC errors in ancillary chunks */ |
2 | 89 |
switch (ancil_action) |
90 |
{ |
|
10576 | 91 |
case PNG_CRC_NO_CHANGE: /* Leave setting as is */ |
2 | 92 |
break; |
10576 | 93 |
|
94 |
case PNG_CRC_WARN_USE: /* Warn/use data */ |
|
2 | 95 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
96 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; |
|
97 |
break; |
|
10576 | 98 |
|
99 |
case PNG_CRC_QUIET_USE: /* Quiet/use data */ |
|
2 | 100 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
101 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | |
|
102 |
PNG_FLAG_CRC_ANCILLARY_NOWARN; |
|
103 |
break; |
|
10576 | 104 |
|
105 |
case PNG_CRC_ERROR_QUIT: /* Error/quit */ |
|
2 | 106 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
107 |
png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; |
|
108 |
break; |
|
10576 | 109 |
|
110 |
case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ |
|
111 |
||
2 | 112 |
case PNG_CRC_DEFAULT: |
113 |
default: |
|
114 |
png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; |
|
115 |
break; |
|
116 |
} |
|
117 |
} |
|
118 |
||
29913 | 119 |
#ifdef PNG_READ_TRANSFORMS_SUPPORTED |
120 |
/* Is it OK to set a transformation now? Only if png_start_read_image or |
|
121 |
* png_read_update_info have not been called. It is not necessary for the IHDR |
|
122 |
* to have been read in all cases; the need_IHDR parameter allows for this |
|
123 |
* check too. |
|
124 |
*/ |
|
125 |
static int |
|
126 |
png_rtran_ok(png_structrp png_ptr, int need_IHDR) |
|
127 |
{ |
|
128 |
if (png_ptr != NULL) |
|
129 |
{ |
|
130 |
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) |
|
131 |
png_app_error(png_ptr, |
|
132 |
"invalid after png_start_read_image or png_read_update_info"); |
|
133 |
||
134 |
else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) |
|
135 |
png_app_error(png_ptr, "invalid before the PNG header has been read"); |
|
136 |
||
137 |
else |
|
138 |
{ |
|
139 |
/* Turn on failure to initialize correctly for all transforms. */ |
|
140 |
png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; |
|
141 |
||
142 |
return 1; /* Ok */ |
|
143 |
} |
|
144 |
} |
|
145 |
||
146 |
return 0; /* no png_error possible! */ |
|
147 |
} |
|
148 |
#endif |
|
149 |
||
10576 | 150 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
151 |
/* Handle alpha and tRNS via a background color */ |
|
152 |
void PNGFAPI |
|
29913 | 153 |
png_set_background_fixed(png_structrp png_ptr, |
10576 | 154 |
png_const_color_16p background_color, int background_gamma_code, |
155 |
int need_expand, png_fixed_point background_gamma) |
|
2 | 156 |
{ |
10576 | 157 |
png_debug(1, "in png_set_background_fixed"); |
158 |
||
29913 | 159 |
if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL) |
10576 | 160 |
return; |
161 |
||
2 | 162 |
if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) |
163 |
{ |
|
164 |
png_warning(png_ptr, "Application must supply a known background gamma"); |
|
165 |
return; |
|
166 |
} |
|
167 |
||
10576 | 168 |
png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; |
169 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
170 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
171 |
||
29913 | 172 |
png_ptr->background = *background_color; |
10576 | 173 |
png_ptr->background_gamma = background_gamma; |
2 | 174 |
png_ptr->background_gamma_type = (png_byte)(background_gamma_code); |
29913 | 175 |
if (need_expand != 0) |
10576 | 176 |
png_ptr->transformations |= PNG_BACKGROUND_EXPAND; |
177 |
else |
|
178 |
png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
|
179 |
} |
|
180 |
||
181 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
|
182 |
void PNGAPI |
|
29913 | 183 |
png_set_background(png_structrp png_ptr, |
10576 | 184 |
png_const_color_16p background_color, int background_gamma_code, |
185 |
int need_expand, double background_gamma) |
|
186 |
{ |
|
187 |
png_set_background_fixed(png_ptr, background_color, background_gamma_code, |
|
188 |
need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); |
|
189 |
} |
|
40436
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
190 |
# endif /* FLOATING_POINT */ |
10576 | 191 |
#endif /* READ_BACKGROUND */ |
192 |
||
193 |
/* Scale 16-bit depth files to 8-bit depth. If both of these are set then the |
|
194 |
* one that pngrtran does first (scale) happens. This is necessary to allow the |
|
195 |
* TRANSFORM and API behavior to be somewhat consistent, and it's simpler. |
|
196 |
*/ |
|
197 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
|
198 |
void PNGAPI |
|
29913 | 199 |
png_set_scale_16(png_structrp png_ptr) |
10576 | 200 |
{ |
201 |
png_debug(1, "in png_set_scale_16"); |
|
202 |
||
29913 | 203 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 204 |
return; |
205 |
||
206 |
png_ptr->transformations |= PNG_SCALE_16_TO_8; |
|
2 | 207 |
} |
208 |
#endif |
|
209 |
||
10576 | 210 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
211 |
/* Chop 16-bit depth files to 8-bit depth */ |
|
2 | 212 |
void PNGAPI |
29913 | 213 |
png_set_strip_16(png_structrp png_ptr) |
2 | 214 |
{ |
10576 | 215 |
png_debug(1, "in png_set_strip_16"); |
216 |
||
29913 | 217 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 218 |
return; |
219 |
||
2 | 220 |
png_ptr->transformations |= PNG_16_TO_8; |
221 |
} |
|
222 |
#endif |
|
223 |
||
10576 | 224 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
2 | 225 |
void PNGAPI |
29913 | 226 |
png_set_strip_alpha(png_structrp png_ptr) |
2 | 227 |
{ |
10576 | 228 |
png_debug(1, "in png_set_strip_alpha"); |
229 |
||
29913 | 230 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 231 |
return; |
232 |
||
233 |
png_ptr->transformations |= PNG_STRIP_ALPHA; |
|
2 | 234 |
} |
235 |
#endif |
|
236 |
||
10576 | 237 |
#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) |
238 |
static png_fixed_point |
|
29913 | 239 |
translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, |
10576 | 240 |
int is_screen) |
241 |
{ |
|
242 |
/* Check for flag values. The main reason for having the old Mac value as a |
|
243 |
* flag is that it is pretty near impossible to work out what the correct |
|
244 |
* value is from Apple documentation - a working Mac system is needed to |
|
245 |
* discover the value! |
|
246 |
*/ |
|
247 |
if (output_gamma == PNG_DEFAULT_sRGB || |
|
248 |
output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) |
|
249 |
{ |
|
250 |
/* If there is no sRGB support this just sets the gamma to the standard |
|
251 |
* sRGB value. (This is a side effect of using this function!) |
|
252 |
*/ |
|
253 |
# ifdef PNG_READ_sRGB_SUPPORTED |
|
254 |
png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; |
|
29913 | 255 |
# else |
256 |
PNG_UNUSED(png_ptr) |
|
10576 | 257 |
# endif |
29913 | 258 |
if (is_screen != 0) |
10576 | 259 |
output_gamma = PNG_GAMMA_sRGB; |
260 |
else |
|
261 |
output_gamma = PNG_GAMMA_sRGB_INVERSE; |
|
262 |
} |
|
263 |
||
264 |
else if (output_gamma == PNG_GAMMA_MAC_18 || |
|
265 |
output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) |
|
266 |
{ |
|
29913 | 267 |
if (is_screen != 0) |
10576 | 268 |
output_gamma = PNG_GAMMA_MAC_OLD; |
269 |
else |
|
270 |
output_gamma = PNG_GAMMA_MAC_INVERSE; |
|
271 |
} |
|
272 |
||
273 |
return output_gamma; |
|
274 |
} |
|
275 |
||
276 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
|
277 |
static png_fixed_point |
|
29913 | 278 |
convert_gamma_value(png_structrp png_ptr, double output_gamma) |
10576 | 279 |
{ |
280 |
/* The following silently ignores cases where fixed point (times 100,000) |
|
281 |
* gamma values are passed to the floating point API. This is safe and it |
|
282 |
* means the fixed point constants work just fine with the floating point |
|
283 |
* API. The alternative would just lead to undetected errors and spurious |
|
284 |
* bug reports. Negative values fail inside the _fixed API unless they |
|
285 |
* correspond to the flag values. |
|
286 |
*/ |
|
287 |
if (output_gamma > 0 && output_gamma < 128) |
|
288 |
output_gamma *= PNG_FP_1; |
|
289 |
||
290 |
/* This preserves -1 and -2 exactly: */ |
|
291 |
output_gamma = floor(output_gamma + .5); |
|
292 |
||
293 |
if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN) |
|
294 |
png_fixed_error(png_ptr, "gamma value"); |
|
295 |
||
296 |
return (png_fixed_point)output_gamma; |
|
297 |
} |
|
298 |
# endif |
|
299 |
#endif /* READ_ALPHA_MODE || READ_GAMMA */ |
|
300 |
||
301 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
|
302 |
void PNGFAPI |
|
29913 | 303 |
png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, |
10576 | 304 |
png_fixed_point output_gamma) |
305 |
{ |
|
306 |
int compose = 0; |
|
307 |
png_fixed_point file_gamma; |
|
308 |
||
309 |
png_debug(1, "in png_set_alpha_mode"); |
|
310 |
||
29913 | 311 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 312 |
return; |
313 |
||
314 |
output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); |
|
315 |
||
316 |
/* Validate the value to ensure it is in a reasonable range. The value |
|
317 |
* is expected to be 1 or greater, but this range test allows for some |
|
318 |
* viewing correction values. The intent is to weed out users of this API |
|
319 |
* who use the inverse of the gamma value accidentally! Since some of these |
|
40436
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
320 |
* values are reasonable this may have to be changed: |
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
321 |
* |
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
322 |
* 1.6.x: changed from 0.07..3 to 0.01..100 (to accomodate the optimal 16-bit |
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
323 |
* gamma of 36, and its reciprocal.) |
10576 | 324 |
*/ |
40436
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
325 |
if (output_gamma < 1000 || output_gamma > 10000000) |
10576 | 326 |
png_error(png_ptr, "output gamma out of expected range"); |
327 |
||
328 |
/* The default file gamma is the inverse of the output gamma; the output |
|
329 |
* gamma may be changed below so get the file value first: |
|
330 |
*/ |
|
331 |
file_gamma = png_reciprocal(output_gamma); |
|
332 |
||
333 |
/* There are really 8 possibilities here, composed of any combination |
|
334 |
* of: |
|
335 |
* |
|
336 |
* premultiply the color channels |
|
337 |
* do not encode non-opaque pixels |
|
338 |
* encode the alpha as well as the color channels |
|
339 |
* |
|
340 |
* The differences disappear if the input/output ('screen') gamma is 1.0, |
|
341 |
* because then the encoding is a no-op and there is only the choice of |
|
342 |
* premultiplying the color channels or not. |
|
343 |
* |
|
344 |
* png_set_alpha_mode and png_set_background interact because both use |
|
345 |
* png_compose to do the work. Calling both is only useful when |
|
346 |
* png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along |
|
347 |
* with a default gamma value. Otherwise PNG_COMPOSE must not be set. |
|
348 |
*/ |
|
349 |
switch (mode) |
|
350 |
{ |
|
351 |
case PNG_ALPHA_PNG: /* default: png standard */ |
|
352 |
/* No compose, but it may be set by png_set_background! */ |
|
353 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
354 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
355 |
break; |
|
356 |
||
357 |
case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */ |
|
358 |
compose = 1; |
|
359 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
360 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
361 |
/* The output is linear: */ |
|
362 |
output_gamma = PNG_FP_1; |
|
363 |
break; |
|
364 |
||
365 |
case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */ |
|
366 |
compose = 1; |
|
367 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
368 |
png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA; |
|
369 |
/* output_gamma records the encoding of opaque pixels! */ |
|
370 |
break; |
|
371 |
||
372 |
case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */ |
|
373 |
compose = 1; |
|
374 |
png_ptr->transformations |= PNG_ENCODE_ALPHA; |
|
375 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
376 |
break; |
|
377 |
||
378 |
default: |
|
379 |
png_error(png_ptr, "invalid alpha mode"); |
|
380 |
} |
|
381 |
||
382 |
/* Only set the default gamma if the file gamma has not been set (this has |
|
383 |
* the side effect that the gamma in a second call to png_set_alpha_mode will |
|
384 |
* be ignored.) |
|
385 |
*/ |
|
29913 | 386 |
if (png_ptr->colorspace.gamma == 0) |
387 |
{ |
|
388 |
png_ptr->colorspace.gamma = file_gamma; |
|
389 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
|
390 |
} |
|
10576 | 391 |
|
392 |
/* But always set the output gamma: */ |
|
393 |
png_ptr->screen_gamma = output_gamma; |
|
394 |
||
395 |
/* Finally, if pre-multiplying, set the background fields to achieve the |
|
396 |
* desired result. |
|
397 |
*/ |
|
29913 | 398 |
if (compose != 0) |
10576 | 399 |
{ |
400 |
/* And obtain alpha pre-multiplication by composing on black: */ |
|
29913 | 401 |
memset(&png_ptr->background, 0, (sizeof png_ptr->background)); |
402 |
png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ |
|
10576 | 403 |
png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; |
404 |
png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; |
|
405 |
||
29913 | 406 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
10576 | 407 |
png_error(png_ptr, |
408 |
"conflicting calls to set alpha mode and background"); |
|
409 |
||
410 |
png_ptr->transformations |= PNG_COMPOSE; |
|
411 |
} |
|
412 |
} |
|
413 |
||
414 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
|
415 |
void PNGAPI |
|
29913 | 416 |
png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) |
10576 | 417 |
{ |
418 |
png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, |
|
419 |
output_gamma)); |
|
420 |
} |
|
421 |
# endif |
|
422 |
#endif |
|
423 |
||
424 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
|
425 |
/* Dither file to 8-bit. Supply a palette, the current number |
|
2 | 426 |
* of elements in the palette, the maximum number of elements |
427 |
* allowed, and a histogram if possible. If the current number |
|
35296 | 428 |
* of colors is greater than the maximum number, the palette will be |
10576 | 429 |
* modified to fit in the maximum number. "full_quantize" indicates |
430 |
* whether we need a quantizing cube set up for RGB images, or if we |
|
2 | 431 |
* simply are reducing the number of colors in a paletted image. |
432 |
*/ |
|
433 |
||
434 |
typedef struct png_dsort_struct |
|
435 |
{ |
|
29913 | 436 |
struct png_dsort_struct * next; |
2 | 437 |
png_byte left; |
438 |
png_byte right; |
|
439 |
} png_dsort; |
|
29913 | 440 |
typedef png_dsort * png_dsortp; |
441 |
typedef png_dsort * * png_dsortpp; |
|
2 | 442 |
|
443 |
void PNGAPI |
|
29913 | 444 |
png_set_quantize(png_structrp png_ptr, png_colorp palette, |
10576 | 445 |
int num_palette, int maximum_colors, png_const_uint_16p histogram, |
446 |
int full_quantize) |
|
2 | 447 |
{ |
10576 | 448 |
png_debug(1, "in png_set_quantize"); |
449 |
||
29913 | 450 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 451 |
return; |
452 |
||
453 |
png_ptr->transformations |= PNG_QUANTIZE; |
|
454 |
||
29913 | 455 |
if (full_quantize == 0) |
2 | 456 |
{ |
457 |
int i; |
|
458 |
||
10576 | 459 |
png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, |
29913 | 460 |
(png_uint_32)(num_palette * (sizeof (png_byte)))); |
2 | 461 |
for (i = 0; i < num_palette; i++) |
10576 | 462 |
png_ptr->quantize_index[i] = (png_byte)i; |
2 | 463 |
} |
464 |
||
465 |
if (num_palette > maximum_colors) |
|
466 |
{ |
|
467 |
if (histogram != NULL) |
|
468 |
{ |
|
469 |
/* This is easy enough, just throw out the least used colors. |
|
10576 | 470 |
* Perhaps not the best solution, but good enough. |
471 |
*/ |
|
2 | 472 |
|
473 |
int i; |
|
474 |
||
10576 | 475 |
/* Initialize an array to sort colors */ |
476 |
png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, |
|
29913 | 477 |
(png_uint_32)(num_palette * (sizeof (png_byte)))); |
10576 | 478 |
|
479 |
/* Initialize the quantize_sort array */ |
|
2 | 480 |
for (i = 0; i < num_palette; i++) |
10576 | 481 |
png_ptr->quantize_sort[i] = (png_byte)i; |
2 | 482 |
|
483 |
/* Find the least used palette entries by starting a |
|
10576 | 484 |
* bubble sort, and running it until we have sorted |
485 |
* out enough colors. Note that we don't care about |
|
486 |
* sorting all the colors, just finding which are |
|
487 |
* least used. |
|
488 |
*/ |
|
2 | 489 |
|
490 |
for (i = num_palette - 1; i >= maximum_colors; i--) |
|
491 |
{ |
|
10576 | 492 |
int done; /* To stop early if the list is pre-sorted */ |
2 | 493 |
int j; |
494 |
||
495 |
done = 1; |
|
496 |
for (j = 0; j < i; j++) |
|
497 |
{ |
|
10576 | 498 |
if (histogram[png_ptr->quantize_sort[j]] |
499 |
< histogram[png_ptr->quantize_sort[j + 1]]) |
|
2 | 500 |
{ |
501 |
png_byte t; |
|
502 |
||
10576 | 503 |
t = png_ptr->quantize_sort[j]; |
504 |
png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; |
|
505 |
png_ptr->quantize_sort[j + 1] = t; |
|
2 | 506 |
done = 0; |
507 |
} |
|
508 |
} |
|
10576 | 509 |
|
29913 | 510 |
if (done != 0) |
2 | 511 |
break; |
512 |
} |
|
513 |
||
10576 | 514 |
/* Swap the palette around, and set up a table, if necessary */ |
29913 | 515 |
if (full_quantize != 0) |
2 | 516 |
{ |
517 |
int j = num_palette; |
|
518 |
||
10576 | 519 |
/* Put all the useful colors within the max, but don't |
520 |
* move the others. |
|
521 |
*/ |
|
2 | 522 |
for (i = 0; i < maximum_colors; i++) |
523 |
{ |
|
10576 | 524 |
if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
2 | 525 |
{ |
526 |
do |
|
527 |
j--; |
|
10576 | 528 |
while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
529 |
||
2 | 530 |
palette[i] = palette[j]; |
531 |
} |
|
532 |
} |
|
533 |
} |
|
534 |
else |
|
535 |
{ |
|
536 |
int j = num_palette; |
|
537 |
||
10576 | 538 |
/* Move all the used colors inside the max limit, and |
539 |
* develop a translation table. |
|
540 |
*/ |
|
2 | 541 |
for (i = 0; i < maximum_colors; i++) |
542 |
{ |
|
10576 | 543 |
/* Only move the colors we need to */ |
544 |
if ((int)png_ptr->quantize_sort[i] >= maximum_colors) |
|
2 | 545 |
{ |
546 |
png_color tmp_color; |
|
547 |
||
548 |
do |
|
549 |
j--; |
|
10576 | 550 |
while ((int)png_ptr->quantize_sort[j] >= maximum_colors); |
2 | 551 |
|
552 |
tmp_color = palette[j]; |
|
553 |
palette[j] = palette[i]; |
|
554 |
palette[i] = tmp_color; |
|
10576 | 555 |
/* Indicate where the color went */ |
556 |
png_ptr->quantize_index[j] = (png_byte)i; |
|
557 |
png_ptr->quantize_index[i] = (png_byte)j; |
|
2 | 558 |
} |
559 |
} |
|
560 |
||
10576 | 561 |
/* Find closest color for those colors we are not using */ |
2 | 562 |
for (i = 0; i < num_palette; i++) |
563 |
{ |
|
10576 | 564 |
if ((int)png_ptr->quantize_index[i] >= maximum_colors) |
2 | 565 |
{ |
566 |
int min_d, k, min_k, d_index; |
|
567 |
||
10576 | 568 |
/* Find the closest color to one we threw out */ |
569 |
d_index = png_ptr->quantize_index[i]; |
|
2 | 570 |
min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); |
571 |
for (k = 1, min_k = 0; k < maximum_colors; k++) |
|
572 |
{ |
|
573 |
int d; |
|
574 |
||
575 |
d = PNG_COLOR_DIST(palette[d_index], palette[k]); |
|
576 |
||
577 |
if (d < min_d) |
|
578 |
{ |
|
579 |
min_d = d; |
|
580 |
min_k = k; |
|
581 |
} |
|
582 |
} |
|
10576 | 583 |
/* Point to closest color */ |
584 |
png_ptr->quantize_index[i] = (png_byte)min_k; |
|
2 | 585 |
} |
586 |
} |
|
587 |
} |
|
10576 | 588 |
png_free(png_ptr, png_ptr->quantize_sort); |
589 |
png_ptr->quantize_sort = NULL; |
|
2 | 590 |
} |
591 |
else |
|
592 |
{ |
|
593 |
/* This is much harder to do simply (and quickly). Perhaps |
|
10576 | 594 |
* we need to go through a median cut routine, but those |
595 |
* don't always behave themselves with only a few colors |
|
596 |
* as input. So we will just find the closest two colors, |
|
597 |
* and throw out one of them (chosen somewhat randomly). |
|
598 |
* [We don't understand this at all, so if someone wants to |
|
599 |
* work on improving it, be our guest - AED, GRP] |
|
600 |
*/ |
|
2 | 601 |
int i; |
602 |
int max_d; |
|
603 |
int num_new_palette; |
|
604 |
png_dsortp t; |
|
605 |
png_dsortpp hash; |
|
606 |
||
10576 | 607 |
t = NULL; |
608 |
||
609 |
/* Initialize palette index arrays */ |
|
2 | 610 |
png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, |
29913 | 611 |
(png_uint_32)(num_palette * (sizeof (png_byte)))); |
2 | 612 |
png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, |
29913 | 613 |
(png_uint_32)(num_palette * (sizeof (png_byte)))); |
10576 | 614 |
|
615 |
/* Initialize the sort array */ |
|
2 | 616 |
for (i = 0; i < num_palette; i++) |
617 |
{ |
|
618 |
png_ptr->index_to_palette[i] = (png_byte)i; |
|
619 |
png_ptr->palette_to_index[i] = (png_byte)i; |
|
620 |
} |
|
621 |
||
10576 | 622 |
hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 * |
29913 | 623 |
(sizeof (png_dsortp)))); |
2 | 624 |
|
625 |
num_new_palette = num_palette; |
|
626 |
||
10576 | 627 |
/* Initial wild guess at how far apart the farthest pixel |
628 |
* pair we will be eliminating will be. Larger |
|
629 |
* numbers mean more areas will be allocated, Smaller |
|
630 |
* numbers run the risk of not saving enough data, and |
|
631 |
* having to do this all over again. |
|
632 |
* |
|
633 |
* I have not done extensive checking on this number. |
|
634 |
*/ |
|
2 | 635 |
max_d = 96; |
636 |
||
637 |
while (num_new_palette > maximum_colors) |
|
638 |
{ |
|
639 |
for (i = 0; i < num_new_palette - 1; i++) |
|
640 |
{ |
|
641 |
int j; |
|
642 |
||
643 |
for (j = i + 1; j < num_new_palette; j++) |
|
644 |
{ |
|
645 |
int d; |
|
646 |
||
647 |
d = PNG_COLOR_DIST(palette[i], palette[j]); |
|
648 |
||
649 |
if (d <= max_d) |
|
650 |
{ |
|
651 |
||
652 |
t = (png_dsortp)png_malloc_warn(png_ptr, |
|
29913 | 653 |
(png_uint_32)(sizeof (png_dsort))); |
10576 | 654 |
|
2 | 655 |
if (t == NULL) |
656 |
break; |
|
10576 | 657 |
|
2 | 658 |
t->next = hash[d]; |
659 |
t->left = (png_byte)i; |
|
660 |
t->right = (png_byte)j; |
|
661 |
hash[d] = t; |
|
662 |
} |
|
663 |
} |
|
664 |
if (t == NULL) |
|
665 |
break; |
|
666 |
} |
|
667 |
||
668 |
if (t != NULL) |
|
669 |
for (i = 0; i <= max_d; i++) |
|
670 |
{ |
|
671 |
if (hash[i] != NULL) |
|
672 |
{ |
|
673 |
png_dsortp p; |
|
674 |
||
675 |
for (p = hash[i]; p; p = p->next) |
|
676 |
{ |
|
677 |
if ((int)png_ptr->index_to_palette[p->left] |
|
10576 | 678 |
< num_new_palette && |
679 |
(int)png_ptr->index_to_palette[p->right] |
|
680 |
< num_new_palette) |
|
2 | 681 |
{ |
682 |
int j, next_j; |
|
683 |
||
684 |
if (num_new_palette & 0x01) |
|
685 |
{ |
|
686 |
j = p->left; |
|
687 |
next_j = p->right; |
|
688 |
} |
|
689 |
else |
|
690 |
{ |
|
691 |
j = p->right; |
|
692 |
next_j = p->left; |
|
693 |
} |
|
694 |
||
695 |
num_new_palette--; |
|
696 |
palette[png_ptr->index_to_palette[j]] |
|
10576 | 697 |
= palette[num_new_palette]; |
29913 | 698 |
if (full_quantize == 0) |
2 | 699 |
{ |
700 |
int k; |
|
701 |
||
702 |
for (k = 0; k < num_palette; k++) |
|
703 |
{ |
|
10576 | 704 |
if (png_ptr->quantize_index[k] == |
705 |
png_ptr->index_to_palette[j]) |
|
706 |
png_ptr->quantize_index[k] = |
|
707 |
png_ptr->index_to_palette[next_j]; |
|
708 |
||
709 |
if ((int)png_ptr->quantize_index[k] == |
|
710 |
num_new_palette) |
|
711 |
png_ptr->quantize_index[k] = |
|
712 |
png_ptr->index_to_palette[j]; |
|
2 | 713 |
} |
714 |
} |
|
715 |
||
716 |
png_ptr->index_to_palette[png_ptr->palette_to_index |
|
10576 | 717 |
[num_new_palette]] = png_ptr->index_to_palette[j]; |
718 |
||
2 | 719 |
png_ptr->palette_to_index[png_ptr->index_to_palette[j]] |
10576 | 720 |
= png_ptr->palette_to_index[num_new_palette]; |
721 |
||
722 |
png_ptr->index_to_palette[j] = |
|
723 |
(png_byte)num_new_palette; |
|
724 |
||
725 |
png_ptr->palette_to_index[num_new_palette] = |
|
726 |
(png_byte)j; |
|
2 | 727 |
} |
728 |
if (num_new_palette <= maximum_colors) |
|
729 |
break; |
|
730 |
} |
|
731 |
if (num_new_palette <= maximum_colors) |
|
732 |
break; |
|
733 |
} |
|
734 |
} |
|
735 |
||
736 |
for (i = 0; i < 769; i++) |
|
737 |
{ |
|
738 |
if (hash[i] != NULL) |
|
739 |
{ |
|
740 |
png_dsortp p = hash[i]; |
|
741 |
while (p) |
|
742 |
{ |
|
743 |
t = p->next; |
|
744 |
png_free(png_ptr, p); |
|
745 |
p = t; |
|
746 |
} |
|
747 |
} |
|
748 |
hash[i] = 0; |
|
749 |
} |
|
750 |
max_d += 96; |
|
751 |
} |
|
752 |
png_free(png_ptr, hash); |
|
753 |
png_free(png_ptr, png_ptr->palette_to_index); |
|
754 |
png_free(png_ptr, png_ptr->index_to_palette); |
|
10576 | 755 |
png_ptr->palette_to_index = NULL; |
756 |
png_ptr->index_to_palette = NULL; |
|
2 | 757 |
} |
758 |
num_palette = maximum_colors; |
|
759 |
} |
|
760 |
if (png_ptr->palette == NULL) |
|
761 |
{ |
|
762 |
png_ptr->palette = palette; |
|
763 |
} |
|
764 |
png_ptr->num_palette = (png_uint_16)num_palette; |
|
765 |
||
29913 | 766 |
if (full_quantize != 0) |
2 | 767 |
{ |
768 |
int i; |
|
769 |
png_bytep distance; |
|
10576 | 770 |
int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + |
771 |
PNG_QUANTIZE_BLUE_BITS; |
|
772 |
int num_red = (1 << PNG_QUANTIZE_RED_BITS); |
|
773 |
int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); |
|
774 |
int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); |
|
2 | 775 |
png_size_t num_entries = ((png_size_t)1 << total_bits); |
776 |
||
10576 | 777 |
png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, |
29913 | 778 |
(png_uint_32)(num_entries * (sizeof (png_byte)))); |
2 | 779 |
|
780 |
distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries * |
|
29913 | 781 |
(sizeof (png_byte)))); |
782 |
||
783 |
memset(distance, 0xff, num_entries * (sizeof (png_byte))); |
|
2 | 784 |
|
785 |
for (i = 0; i < num_palette; i++) |
|
786 |
{ |
|
787 |
int ir, ig, ib; |
|
10576 | 788 |
int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS)); |
789 |
int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS)); |
|
790 |
int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS)); |
|
2 | 791 |
|
792 |
for (ir = 0; ir < num_red; ir++) |
|
793 |
{ |
|
794 |
/* int dr = abs(ir - r); */ |
|
795 |
int dr = ((ir > r) ? ir - r : r - ir); |
|
10576 | 796 |
int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS + |
797 |
PNG_QUANTIZE_GREEN_BITS)); |
|
2 | 798 |
|
799 |
for (ig = 0; ig < num_green; ig++) |
|
800 |
{ |
|
801 |
/* int dg = abs(ig - g); */ |
|
802 |
int dg = ((ig > g) ? ig - g : g - ig); |
|
803 |
int dt = dr + dg; |
|
804 |
int dm = ((dr > dg) ? dr : dg); |
|
10576 | 805 |
int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS); |
2 | 806 |
|
807 |
for (ib = 0; ib < num_blue; ib++) |
|
808 |
{ |
|
809 |
int d_index = index_g | ib; |
|
810 |
/* int db = abs(ib - b); */ |
|
811 |
int db = ((ib > b) ? ib - b : b - ib); |
|
812 |
int dmax = ((dm > db) ? dm : db); |
|
813 |
int d = dmax + dt + db; |
|
814 |
||
815 |
if (d < (int)distance[d_index]) |
|
816 |
{ |
|
817 |
distance[d_index] = (png_byte)d; |
|
818 |
png_ptr->palette_lookup[d_index] = (png_byte)i; |
|
819 |
} |
|
820 |
} |
|
821 |
} |
|
822 |
} |
|
823 |
} |
|
824 |
||
825 |
png_free(png_ptr, distance); |
|
826 |
} |
|
827 |
} |
|
29913 | 828 |
#endif /* READ_QUANTIZE */ |
10576 | 829 |
|
830 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
831 |
void PNGFAPI |
|
29913 | 832 |
png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, |
10576 | 833 |
png_fixed_point file_gamma) |
834 |
{ |
|
835 |
png_debug(1, "in png_set_gamma_fixed"); |
|
836 |
||
29913 | 837 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 838 |
return; |
839 |
||
840 |
/* New in libpng-1.5.4 - reserve particular negative values as flags. */ |
|
841 |
scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); |
|
842 |
file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); |
|
843 |
||
844 |
/* Checking the gamma values for being >0 was added in 1.5.4 along with the |
|
845 |
* premultiplied alpha support; this actually hides an undocumented feature |
|
846 |
* of the previous implementation which allowed gamma processing to be |
|
847 |
* disabled in background handling. There is no evidence (so far) that this |
|
848 |
* was being used; however, png_set_background itself accepted and must still |
|
849 |
* accept '0' for the gamma value it takes, because it isn't always used. |
|
850 |
* |
|
851 |
* Since this is an API change (albeit a very minor one that removes an |
|
29913 | 852 |
* undocumented API feature) the following checks were only enabled in |
853 |
* libpng-1.6.0. |
|
10576 | 854 |
*/ |
855 |
if (file_gamma <= 0) |
|
856 |
png_error(png_ptr, "invalid file gamma in png_set_gamma"); |
|
857 |
||
858 |
if (scrn_gamma <= 0) |
|
859 |
png_error(png_ptr, "invalid screen gamma in png_set_gamma"); |
|
2 | 860 |
|
10576 | 861 |
/* Set the gamma values unconditionally - this overrides the value in the PNG |
862 |
* file if a gAMA chunk was present. png_set_alpha_mode provides a |
|
863 |
* different, easier, way to default the file gamma. |
|
864 |
*/ |
|
29913 | 865 |
png_ptr->colorspace.gamma = file_gamma; |
866 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
|
10576 | 867 |
png_ptr->screen_gamma = scrn_gamma; |
868 |
} |
|
869 |
||
870 |
# ifdef PNG_FLOATING_POINT_SUPPORTED |
|
2 | 871 |
void PNGAPI |
29913 | 872 |
png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) |
2 | 873 |
{ |
10576 | 874 |
png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), |
875 |
convert_gamma_value(png_ptr, file_gamma)); |
|
2 | 876 |
} |
29913 | 877 |
# endif /* FLOATING_POINT */ |
10576 | 878 |
#endif /* READ_GAMMA */ |
879 |
||
880 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
|
2 | 881 |
/* Expand paletted images to RGB, expand grayscale images of |
882 |
* less than 8-bit depth to 8-bit depth, and expand tRNS chunks |
|
883 |
* to alpha channels. |
|
884 |
*/ |
|
885 |
void PNGAPI |
|
29913 | 886 |
png_set_expand(png_structrp png_ptr) |
2 | 887 |
{ |
10576 | 888 |
png_debug(1, "in png_set_expand"); |
889 |
||
29913 | 890 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 891 |
return; |
892 |
||
2 | 893 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
894 |
} |
|
895 |
||
896 |
/* GRR 19990627: the following three functions currently are identical |
|
897 |
* to png_set_expand(). However, it is entirely reasonable that someone |
|
898 |
* might wish to expand an indexed image to RGB but *not* expand a single, |
|
899 |
* fully transparent palette entry to a full alpha channel--perhaps instead |
|
900 |
* convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace |
|
901 |
* the transparent color with a particular RGB value, or drop tRNS entirely. |
|
902 |
* IOW, a future version of the library may make the transformations flag |
|
903 |
* a bit more fine-grained, with separate bits for each of these three |
|
904 |
* functions. |
|
905 |
* |
|
906 |
* More to the point, these functions make it obvious what libpng will be |
|
907 |
* doing, whereas "expand" can (and does) mean any number of things. |
|
908 |
* |
|
10576 | 909 |
* GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified |
910 |
* to expand only the sample depth but not to expand the tRNS to alpha |
|
911 |
* and its name was changed to png_set_expand_gray_1_2_4_to_8(). |
|
2 | 912 |
*/ |
913 |
||
914 |
/* Expand paletted images to RGB. */ |
|
915 |
void PNGAPI |
|
29913 | 916 |
png_set_palette_to_rgb(png_structrp png_ptr) |
2 | 917 |
{ |
10576 | 918 |
png_debug(1, "in png_set_palette_to_rgb"); |
919 |
||
29913 | 920 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 921 |
return; |
922 |
||
2 | 923 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
924 |
} |
|
925 |
||
926 |
/* Expand grayscale images of less than 8-bit depth to 8 bits. */ |
|
927 |
void PNGAPI |
|
29913 | 928 |
png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) |
2 | 929 |
{ |
10576 | 930 |
png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); |
931 |
||
29913 | 932 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 933 |
return; |
934 |
||
2 | 935 |
png_ptr->transformations |= PNG_EXPAND; |
936 |
} |
|
10576 | 937 |
|
2 | 938 |
/* Expand tRNS chunks to alpha channels. */ |
939 |
void PNGAPI |
|
29913 | 940 |
png_set_tRNS_to_alpha(png_structrp png_ptr) |
2 | 941 |
{ |
10576 | 942 |
png_debug(1, "in png_set_tRNS_to_alpha"); |
943 |
||
29913 | 944 |
if (png_rtran_ok(png_ptr, 0) == 0) |
945 |
return; |
|
946 |
||
2 | 947 |
png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); |
948 |
} |
|
29913 | 949 |
#endif /* READ_EXPAND */ |
2 | 950 |
|
10576 | 951 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
952 |
/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise |
|
953 |
* it may not work correctly.) |
|
954 |
*/ |
|
955 |
void PNGAPI |
|
29913 | 956 |
png_set_expand_16(png_structrp png_ptr) |
10576 | 957 |
{ |
958 |
png_debug(1, "in png_set_expand_16"); |
|
959 |
||
29913 | 960 |
if (png_rtran_ok(png_ptr, 0) == 0) |
10576 | 961 |
return; |
962 |
||
963 |
png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); |
|
964 |
} |
|
965 |
#endif |
|
966 |
||
967 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
|
2 | 968 |
void PNGAPI |
29913 | 969 |
png_set_gray_to_rgb(png_structrp png_ptr) |
2 | 970 |
{ |
10576 | 971 |
png_debug(1, "in png_set_gray_to_rgb"); |
972 |
||
29913 | 973 |
if (png_rtran_ok(png_ptr, 0) == 0) |
974 |
return; |
|
975 |
||
976 |
/* Because rgb must be 8 bits or more: */ |
|
977 |
png_set_expand_gray_1_2_4_to_8(png_ptr); |
|
978 |
png_ptr->transformations |= PNG_GRAY_TO_RGB; |
|
2 | 979 |
} |
980 |
#endif |
|
981 |
||
10576 | 982 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
983 |
void PNGFAPI |
|
29913 | 984 |
png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, |
10576 | 985 |
png_fixed_point red, png_fixed_point green) |
986 |
{ |
|
987 |
png_debug(1, "in png_set_rgb_to_gray"); |
|
988 |
||
29913 | 989 |
/* Need the IHDR here because of the check on color_type below. */ |
990 |
/* TODO: fix this */ |
|
991 |
if (png_rtran_ok(png_ptr, 1) == 0) |
|
10576 | 992 |
return; |
993 |
||
29913 | 994 |
switch (error_action) |
10576 | 995 |
{ |
29913 | 996 |
case PNG_ERROR_ACTION_NONE: |
10576 | 997 |
png_ptr->transformations |= PNG_RGB_TO_GRAY; |
998 |
break; |
|
999 |
||
29913 | 1000 |
case PNG_ERROR_ACTION_WARN: |
10576 | 1001 |
png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; |
1002 |
break; |
|
1003 |
||
29913 | 1004 |
case PNG_ERROR_ACTION_ERROR: |
10576 | 1005 |
png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; |
1006 |
break; |
|
1007 |
||
1008 |
default: |
|
1009 |
png_error(png_ptr, "invalid error action to rgb_to_gray"); |
|
1010 |
} |
|
29913 | 1011 |
|
10576 | 1012 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
1013 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
|
1014 |
png_ptr->transformations |= PNG_EXPAND; |
|
1015 |
#else |
|
1016 |
{ |
|
29913 | 1017 |
/* Make this an error in 1.6 because otherwise the application may assume |
1018 |
* that it just worked and get a memory overwrite. |
|
1019 |
*/ |
|
1020 |
png_error(png_ptr, |
|
10576 | 1021 |
"Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); |
1022 |
||
29913 | 1023 |
/* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ |
10576 | 1024 |
} |
1025 |
#endif |
|
1026 |
{ |
|
1027 |
if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) |
|
1028 |
{ |
|
1029 |
png_uint_16 red_int, green_int; |
|
1030 |
||
29913 | 1031 |
/* NOTE: this calculation does not round, but this behavior is retained |
1032 |
* for consistency; the inaccuracy is very small. The code here always |
|
1033 |
* overwrites the coefficients, regardless of whether they have been |
|
1034 |
* defaulted or set already. |
|
1035 |
*/ |
|
1036 |
red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); |
|
1037 |
green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); |
|
10576 | 1038 |
|
1039 |
png_ptr->rgb_to_gray_red_coeff = red_int; |
|
1040 |
png_ptr->rgb_to_gray_green_coeff = green_int; |
|
29913 | 1041 |
png_ptr->rgb_to_gray_coefficients_set = 1; |
10576 | 1042 |
} |
1043 |
||
1044 |
else |
|
1045 |
{ |
|
1046 |
if (red >= 0 && green >= 0) |
|
29913 | 1047 |
png_app_warning(png_ptr, |
10576 | 1048 |
"ignoring out of range rgb_to_gray coefficients"); |
1049 |
||
29913 | 1050 |
/* Use the defaults, from the cHRM chunk if set, else the historical |
1051 |
* values which are close to the sRGB/HDTV/ITU-Rec 709 values. See |
|
1052 |
* png_do_rgb_to_gray for more discussion of the values. In this case |
|
1053 |
* the coefficients are not marked as 'set' and are not overwritten if |
|
1054 |
* something has already provided a default. |
|
10576 | 1055 |
*/ |
1056 |
if (png_ptr->rgb_to_gray_red_coeff == 0 && |
|
29913 | 1057 |
png_ptr->rgb_to_gray_green_coeff == 0) |
10576 | 1058 |
{ |
29913 | 1059 |
png_ptr->rgb_to_gray_red_coeff = 6968; |
1060 |
png_ptr->rgb_to_gray_green_coeff = 23434; |
|
1061 |
/* png_ptr->rgb_to_gray_blue_coeff = 2366; */ |
|
10576 | 1062 |
} |
1063 |
} |
|
1064 |
} |
|
1065 |
} |
|
1066 |
||
1067 |
#ifdef PNG_FLOATING_POINT_SUPPORTED |
|
2 | 1068 |
/* Convert a RGB image to a grayscale of the same width. This allows us, |
1069 |
* for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. |
|
1070 |
*/ |
|
1071 |
||
1072 |
void PNGAPI |
|
29913 | 1073 |
png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, |
2 | 1074 |
double green) |
1075 |
{ |
|
10576 | 1076 |
png_set_rgb_to_gray_fixed(png_ptr, error_action, |
1077 |
png_fixed(png_ptr, red, "rgb to gray red coefficient"), |
|
1078 |
png_fixed(png_ptr, green, "rgb to gray green coefficient")); |
|
2 | 1079 |
} |
10576 | 1080 |
#endif /* FLOATING POINT */ |
1081 |
||
29913 | 1082 |
#endif /* RGB_TO_GRAY */ |
2 | 1083 |
|
1084 |
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ |
|
10576 | 1085 |
defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) |
2 | 1086 |
void PNGAPI |
29913 | 1087 |
png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr |
10576 | 1088 |
read_user_transform_fn) |
2 | 1089 |
{ |
10576 | 1090 |
png_debug(1, "in png_set_read_user_transform_fn"); |
1091 |
||
1092 |
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
|
2 | 1093 |
png_ptr->transformations |= PNG_USER_TRANSFORM; |
1094 |
png_ptr->read_user_transform_fn = read_user_transform_fn; |
|
1095 |
#endif |
|
10576 | 1096 |
} |
2 | 1097 |
#endif |
10576 | 1098 |
|
1099 |
#ifdef PNG_READ_TRANSFORMS_SUPPORTED |
|
1100 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
1101 |
/* In the case of gamma transformations only do transformations on images where |
|
1102 |
* the [file] gamma and screen_gamma are not close reciprocals, otherwise it |
|
1103 |
* slows things down slightly, and also needlessly introduces small errors. |
|
1104 |
*/ |
|
1105 |
static int /* PRIVATE */ |
|
1106 |
png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) |
|
1107 |
{ |
|
1108 |
/* PNG_GAMMA_THRESHOLD is the threshold for performing gamma |
|
1109 |
* correction as a difference of the overall transform from 1.0 |
|
1110 |
* |
|
1111 |
* We want to compare the threshold with s*f - 1, if we get |
|
1112 |
* overflow here it is because of wacky gamma values so we |
|
1113 |
* turn on processing anyway. |
|
1114 |
*/ |
|
1115 |
png_fixed_point gtest; |
|
1116 |
return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) || |
|
1117 |
png_gamma_significant(gtest); |
|
2 | 1118 |
} |
1119 |
#endif |
|
1120 |
||
1121 |
/* Initialize everything needed for the read. This includes modifying |
|
1122 |
* the palette. |
|
1123 |
*/ |
|
10576 | 1124 |
|
29913 | 1125 |
/* For the moment 'png_init_palette_transformations' and |
10576 | 1126 |
* 'png_init_rgb_transformations' only do some flag canceling optimizations. |
1127 |
* The intent is that these two routines should have palette or rgb operations |
|
1128 |
* extracted from 'png_init_read_transformations'. |
|
1129 |
*/ |
|
1130 |
static void /* PRIVATE */ |
|
29913 | 1131 |
png_init_palette_transformations(png_structrp png_ptr) |
2 | 1132 |
{ |
10576 | 1133 |
/* Called to handle the (input) palette case. In png_do_read_transformations |
1134 |
* the first step is to expand the palette if requested, so this code must |
|
1135 |
* take care to only make changes that are invariant with respect to the |
|
1136 |
* palette expansion, or only do them if there is no expansion. |
|
1137 |
* |
|
1138 |
* STRIP_ALPHA has already been handled in the caller (by setting num_trans |
|
1139 |
* to 0.) |
|
1140 |
*/ |
|
1141 |
int input_has_alpha = 0; |
|
1142 |
int input_has_transparency = 0; |
|
1143 |
||
1144 |
if (png_ptr->num_trans > 0) |
|
1145 |
{ |
|
1146 |
int i; |
|
1147 |
||
1148 |
/* Ignore if all the entries are opaque (unlikely!) */ |
|
1149 |
for (i=0; i<png_ptr->num_trans; ++i) |
|
29913 | 1150 |
{ |
10576 | 1151 |
if (png_ptr->trans_alpha[i] == 255) |
1152 |
continue; |
|
1153 |
else if (png_ptr->trans_alpha[i] == 0) |
|
1154 |
input_has_transparency = 1; |
|
1155 |
else |
|
29913 | 1156 |
{ |
1157 |
input_has_transparency = 1; |
|
10576 | 1158 |
input_has_alpha = 1; |
29913 | 1159 |
break; |
1160 |
} |
|
1161 |
} |
|
10576 | 1162 |
} |
1163 |
||
1164 |
/* If no alpha we can optimize. */ |
|
29913 | 1165 |
if (input_has_alpha == 0) |
10576 | 1166 |
{ |
1167 |
/* Any alpha means background and associative alpha processing is |
|
29913 | 1168 |
* required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA |
10576 | 1169 |
* and ENCODE_ALPHA are irrelevant. |
1170 |
*/ |
|
1171 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
1172 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
1173 |
||
29913 | 1174 |
if (input_has_transparency == 0) |
10576 | 1175 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
1176 |
} |
|
2 | 1177 |
|
1178 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
|
10576 | 1179 |
/* png_set_background handling - deals with the complexity of whether the |
1180 |
* background color is in the file format or the screen format in the case |
|
1181 |
* where an 'expand' will happen. |
|
2 | 1182 |
*/ |
10576 | 1183 |
|
1184 |
/* The following code cannot be entered in the alpha pre-multiplication case |
|
1185 |
* because PNG_BACKGROUND_EXPAND is cancelled below. |
|
1186 |
*/ |
|
29913 | 1187 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
1188 |
(png_ptr->transformations & PNG_EXPAND) != 0) |
|
2 | 1189 |
{ |
1190 |
{ |
|
10576 | 1191 |
png_ptr->background.red = |
1192 |
png_ptr->palette[png_ptr->background.index].red; |
|
1193 |
png_ptr->background.green = |
|
1194 |
png_ptr->palette[png_ptr->background.index].green; |
|
1195 |
png_ptr->background.blue = |
|
1196 |
png_ptr->palette[png_ptr->background.index].blue; |
|
1197 |
||
1198 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
|
29913 | 1199 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
10576 | 1200 |
{ |
29913 | 1201 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
10576 | 1202 |
{ |
1203 |
/* Invert the alpha channel (in tRNS) unless the pixels are |
|
1204 |
* going to be expanded, in which case leave it for later |
|
1205 |
*/ |
|
1206 |
int i, istop = png_ptr->num_trans; |
|
1207 |
||
1208 |
for (i=0; i<istop; i++) |
|
1209 |
png_ptr->trans_alpha[i] = (png_byte)(255 - |
|
1210 |
png_ptr->trans_alpha[i]); |
|
1211 |
} |
|
1212 |
} |
|
29913 | 1213 |
#endif /* READ_INVERT_ALPHA */ |
10576 | 1214 |
} |
1215 |
} /* background expand and (therefore) no alpha association. */ |
|
29913 | 1216 |
#endif /* READ_EXPAND && READ_BACKGROUND */ |
10576 | 1217 |
} |
1218 |
||
1219 |
static void /* PRIVATE */ |
|
29913 | 1220 |
png_init_rgb_transformations(png_structrp png_ptr) |
10576 | 1221 |
{ |
1222 |
/* Added to libpng-1.5.4: check the color type to determine whether there |
|
1223 |
* is any alpha or transparency in the image and simply cancel the |
|
1224 |
* background and alpha mode stuff if there isn't. |
|
1225 |
*/ |
|
1226 |
int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
|
1227 |
int input_has_transparency = png_ptr->num_trans > 0; |
|
1228 |
||
1229 |
/* If no alpha we can optimize. */ |
|
29913 | 1230 |
if (input_has_alpha == 0) |
10576 | 1231 |
{ |
1232 |
/* Any alpha means background and associative alpha processing is |
|
29913 | 1233 |
* required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA |
10576 | 1234 |
* and ENCODE_ALPHA are irrelevant. |
1235 |
*/ |
|
1236 |
# ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
|
1237 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
1238 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
1239 |
# endif |
|
1240 |
||
29913 | 1241 |
if (input_has_transparency == 0) |
10576 | 1242 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); |
1243 |
} |
|
1244 |
||
1245 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
|
1246 |
/* png_set_background handling - deals with the complexity of whether the |
|
1247 |
* background color is in the file format or the screen format in the case |
|
1248 |
* where an 'expand' will happen. |
|
1249 |
*/ |
|
1250 |
||
1251 |
/* The following code cannot be entered in the alpha pre-multiplication case |
|
1252 |
* because PNG_BACKGROUND_EXPAND is cancelled below. |
|
1253 |
*/ |
|
29913 | 1254 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && |
1255 |
(png_ptr->transformations & PNG_EXPAND) != 0 && |
|
1256 |
(png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
|
10576 | 1257 |
/* i.e., GRAY or GRAY_ALPHA */ |
1258 |
{ |
|
1259 |
{ |
|
1260 |
/* Expand background and tRNS chunks */ |
|
29913 | 1261 |
int gray = png_ptr->background.gray; |
1262 |
int trans_gray = png_ptr->trans_color.gray; |
|
1263 |
||
2 | 1264 |
switch (png_ptr->bit_depth) |
1265 |
{ |
|
1266 |
case 1: |
|
29913 | 1267 |
gray *= 0xff; |
1268 |
trans_gray *= 0xff; |
|
2 | 1269 |
break; |
10576 | 1270 |
|
2 | 1271 |
case 2: |
29913 | 1272 |
gray *= 0x55; |
1273 |
trans_gray *= 0x55; |
|
2 | 1274 |
break; |
10576 | 1275 |
|
2 | 1276 |
case 4: |
29913 | 1277 |
gray *= 0x11; |
1278 |
trans_gray *= 0x11; |
|
2 | 1279 |
break; |
10576 | 1280 |
|
1281 |
default: |
|
1282 |
||
2 | 1283 |
case 8: |
29913 | 1284 |
/* FALL THROUGH (Already 8 bits) */ |
10576 | 1285 |
|
2 | 1286 |
case 16: |
29913 | 1287 |
/* Already a full 16 bits */ |
2 | 1288 |
break; |
1289 |
} |
|
29913 | 1290 |
|
1291 |
png_ptr->background.red = png_ptr->background.green = |
|
1292 |
png_ptr->background.blue = (png_uint_16)gray; |
|
1293 |
||
1294 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) |
|
1295 |
{ |
|
1296 |
png_ptr->trans_color.red = png_ptr->trans_color.green = |
|
1297 |
png_ptr->trans_color.blue = (png_uint_16)trans_gray; |
|
1298 |
} |
|
2 | 1299 |
} |
10576 | 1300 |
} /* background expand and (therefore) no alpha association. */ |
29913 | 1301 |
#endif /* READ_EXPAND && READ_BACKGROUND */ |
10576 | 1302 |
} |
1303 |
||
1304 |
void /* PRIVATE */ |
|
29913 | 1305 |
png_init_read_transformations(png_structrp png_ptr) |
10576 | 1306 |
{ |
1307 |
png_debug(1, "in png_init_read_transformations"); |
|
1308 |
||
1309 |
/* This internal function is called from png_read_start_row in pngrutil.c |
|
1310 |
* and it is called before the 'rowbytes' calculation is done, so the code |
|
1311 |
* in here can change or update the transformations flags. |
|
1312 |
* |
|
1313 |
* First do updates that do not depend on the details of the PNG image data |
|
1314 |
* being processed. |
|
1315 |
*/ |
|
1316 |
||
1317 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
1318 |
/* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds |
|
1319 |
* png_set_alpha_mode and this is another source for a default file gamma so |
|
1320 |
* the test needs to be performed later - here. In addition prior to 1.5.4 |
|
1321 |
* the tests were repeated for the PALETTE color type here - this is no |
|
1322 |
* longer necessary (and doesn't seem to have been necessary before.) |
|
1323 |
*/ |
|
1324 |
{ |
|
1325 |
/* The following temporary indicates if overall gamma correction is |
|
1326 |
* required. |
|
1327 |
*/ |
|
1328 |
int gamma_correction = 0; |
|
1329 |
||
29913 | 1330 |
if (png_ptr->colorspace.gamma != 0) /* has been set */ |
2 | 1331 |
{ |
10576 | 1332 |
if (png_ptr->screen_gamma != 0) /* screen set too */ |
29913 | 1333 |
gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, |
10576 | 1334 |
png_ptr->screen_gamma); |
1335 |
||
1336 |
else |
|
1337 |
/* Assume the output matches the input; a long time default behavior |
|
1338 |
* of libpng, although the standard has nothing to say about this. |
|
1339 |
*/ |
|
29913 | 1340 |
png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); |
10576 | 1341 |
} |
1342 |
||
1343 |
else if (png_ptr->screen_gamma != 0) |
|
1344 |
/* The converse - assume the file matches the screen, note that this |
|
1345 |
* perhaps undesireable default can (from 1.5.4) be changed by calling |
|
1346 |
* png_set_alpha_mode (even if the alpha handling mode isn't required |
|
1347 |
* or isn't changed from the default.) |
|
1348 |
*/ |
|
29913 | 1349 |
png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); |
10576 | 1350 |
|
1351 |
else /* neither are set */ |
|
1352 |
/* Just in case the following prevents any processing - file and screen |
|
1353 |
* are both assumed to be linear and there is no way to introduce a |
|
1354 |
* third gamma value other than png_set_background with 'UNIQUE', and, |
|
1355 |
* prior to 1.5.4 |
|
1356 |
*/ |
|
29913 | 1357 |
png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; |
1358 |
||
1359 |
/* We have a gamma value now. */ |
|
1360 |
png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; |
|
10576 | 1361 |
|
1362 |
/* Now turn the gamma transformation on or off as appropriate. Notice |
|
1363 |
* that PNG_GAMMA just refers to the file->screen correction. Alpha |
|
1364 |
* composition may independently cause gamma correction because it needs |
|
1365 |
* linear data (e.g. if the file has a gAMA chunk but the screen gamma |
|
1366 |
* hasn't been specified.) In any case this flag may get turned off in |
|
1367 |
* the code immediately below if the transform can be handled outside the |
|
1368 |
* row loop. |
|
1369 |
*/ |
|
29913 | 1370 |
if (gamma_correction != 0) |
10576 | 1371 |
png_ptr->transformations |= PNG_GAMMA; |
1372 |
||
1373 |
else |
|
1374 |
png_ptr->transformations &= ~PNG_GAMMA; |
|
1375 |
} |
|
2 | 1376 |
#endif |
10576 | 1377 |
|
1378 |
/* Certain transformations have the effect of preventing other |
|
29913 | 1379 |
* transformations that happen afterward in png_do_read_transformations; |
10576 | 1380 |
* resolve the interdependencies here. From the code of |
1381 |
* png_do_read_transformations the order is: |
|
1382 |
* |
|
1383 |
* 1) PNG_EXPAND (including PNG_EXPAND_tRNS) |
|
1384 |
* 2) PNG_STRIP_ALPHA (if no compose) |
|
1385 |
* 3) PNG_RGB_TO_GRAY |
|
1386 |
* 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY |
|
1387 |
* 5) PNG_COMPOSE |
|
1388 |
* 6) PNG_GAMMA |
|
1389 |
* 7) PNG_STRIP_ALPHA (if compose) |
|
1390 |
* 8) PNG_ENCODE_ALPHA |
|
1391 |
* 9) PNG_SCALE_16_TO_8 |
|
1392 |
* 10) PNG_16_TO_8 |
|
1393 |
* 11) PNG_QUANTIZE (converts to palette) |
|
1394 |
* 12) PNG_EXPAND_16 |
|
1395 |
* 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY |
|
1396 |
* 14) PNG_INVERT_MONO |
|
29913 | 1397 |
* 15) PNG_INVERT_ALPHA |
1398 |
* 16) PNG_SHIFT |
|
1399 |
* 17) PNG_PACK |
|
1400 |
* 18) PNG_BGR |
|
1401 |
* 19) PNG_PACKSWAP |
|
1402 |
* 20) PNG_FILLER (includes PNG_ADD_ALPHA) |
|
10576 | 1403 |
* 21) PNG_SWAP_ALPHA |
1404 |
* 22) PNG_SWAP_BYTES |
|
1405 |
* 23) PNG_USER_TRANSFORM [must be last] |
|
1406 |
*/ |
|
1407 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
|
29913 | 1408 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
1409 |
(png_ptr->transformations & PNG_COMPOSE) == 0) |
|
10576 | 1410 |
{ |
1411 |
/* Stripping the alpha channel happens immediately after the 'expand' |
|
1412 |
* transformations, before all other transformation, so it cancels out |
|
1413 |
* the alpha handling. It has the side effect negating the effect of |
|
1414 |
* PNG_EXPAND_tRNS too: |
|
1415 |
*/ |
|
1416 |
png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | |
|
1417 |
PNG_EXPAND_tRNS); |
|
1418 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
1419 |
||
1420 |
/* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen |
|
1421 |
* so transparency information would remain just so long as it wasn't |
|
1422 |
* expanded. This produces unexpected API changes if the set of things |
|
1423 |
* that do PNG_EXPAND_tRNS changes (perfectly possible given the |
|
1424 |
* documentation - which says ask for what you want, accept what you |
|
1425 |
* get.) This makes the behavior consistent from 1.5.4: |
|
1426 |
*/ |
|
1427 |
png_ptr->num_trans = 0; |
|
1428 |
} |
|
1429 |
#endif /* STRIP_ALPHA supported, no COMPOSE */ |
|
1430 |
||
1431 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
|
1432 |
/* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA |
|
1433 |
* settings will have no effect. |
|
1434 |
*/ |
|
29913 | 1435 |
if (png_gamma_significant(png_ptr->screen_gamma) == 0) |
10576 | 1436 |
{ |
1437 |
png_ptr->transformations &= ~PNG_ENCODE_ALPHA; |
|
1438 |
png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; |
|
2 | 1439 |
} |
1440 |
#endif |
|
1441 |
||
29913 | 1442 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
1443 |
/* Make sure the coefficients for the rgb to gray conversion are set |
|
1444 |
* appropriately. |
|
1445 |
*/ |
|
1446 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
|
1447 |
png_colorspace_set_rgb_coefficients(png_ptr); |
|
1448 |
#endif |
|
1449 |
||
1450 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
|
1451 |
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) |
|
10576 | 1452 |
/* Detect gray background and attempt to enable optimization for |
1453 |
* gray --> RGB case. |
|
1454 |
* |
|
1455 |
* Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or |
|
1456 |
* RGB_ALPHA (in which case need_expand is superfluous anyway), the |
|
1457 |
* background color might actually be gray yet not be flagged as such. |
|
1458 |
* This is not a problem for the current code, which uses |
|
1459 |
* PNG_BACKGROUND_IS_GRAY only to decide when to do the |
|
1460 |
* png_do_gray_to_rgb() transformation. |
|
1461 |
* |
|
1462 |
* TODO: this code needs to be revised to avoid the complexity and |
|
1463 |
* interdependencies. The color type of the background should be recorded in |
|
1464 |
* png_set_background, along with the bit depth, then the code has a record |
|
1465 |
* of exactly what color space the background is currently in. |
|
1466 |
*/ |
|
29913 | 1467 |
if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0) |
10576 | 1468 |
{ |
1469 |
/* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if |
|
29913 | 1470 |
* the file was grayscale the background value is gray. |
10576 | 1471 |
*/ |
29913 | 1472 |
if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) |
10576 | 1473 |
png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
1474 |
} |
|
1475 |
||
29913 | 1476 |
else if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
10576 | 1477 |
{ |
1478 |
/* PNG_COMPOSE: png_set_background was called with need_expand false, |
|
1479 |
* so the color is in the color space of the output or png_set_alpha_mode |
|
1480 |
* was called and the color is black. Ignore RGB_TO_GRAY because that |
|
1481 |
* happens before GRAY_TO_RGB. |
|
1482 |
*/ |
|
29913 | 1483 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
10576 | 1484 |
{ |
1485 |
if (png_ptr->background.red == png_ptr->background.green && |
|
1486 |
png_ptr->background.red == png_ptr->background.blue) |
|
1487 |
{ |
|
1488 |
png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; |
|
1489 |
png_ptr->background.gray = png_ptr->background.red; |
|
1490 |
} |
|
1491 |
} |
|
1492 |
} |
|
29913 | 1493 |
#endif /* READ_EXPAND && READ_BACKGROUND */ |
1494 |
#endif /* READ_GRAY_TO_RGB */ |
|
10576 | 1495 |
|
1496 |
/* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations |
|
1497 |
* can be performed directly on the palette, and some (such as rgb to gray) |
|
1498 |
* can be optimized inside the palette. This is particularly true of the |
|
1499 |
* composite (background and alpha) stuff, which can be pretty much all done |
|
1500 |
* in the palette even if the result is expanded to RGB or gray afterward. |
|
1501 |
* |
|
1502 |
* NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and |
|
1503 |
* earlier and the palette stuff is actually handled on the first row. This |
|
1504 |
* leads to the reported bug that the palette returned by png_get_PLTE is not |
|
1505 |
* updated. |
|
1506 |
*/ |
|
1507 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
|
1508 |
png_init_palette_transformations(png_ptr); |
|
1509 |
||
1510 |
else |
|
1511 |
png_init_rgb_transformations(png_ptr); |
|
1512 |
||
1513 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
|
1514 |
defined(PNG_READ_EXPAND_16_SUPPORTED) |
|
29913 | 1515 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
1516 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
|
1517 |
(png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
|
1518 |
png_ptr->bit_depth != 16) |
|
2 | 1519 |
{ |
10576 | 1520 |
/* TODO: fix this. Because the expand_16 operation is after the compose |
1521 |
* handling the background color must be 8, not 16, bits deep, but the |
|
1522 |
* application will supply a 16-bit value so reduce it here. |
|
1523 |
* |
|
1524 |
* The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at |
|
1525 |
* present, so that case is ok (until do_expand_16 is moved.) |
|
1526 |
* |
|
1527 |
* NOTE: this discards the low 16 bits of the user supplied background |
|
1528 |
* color, but until expand_16 works properly there is no choice! |
|
1529 |
*/ |
|
29913 | 1530 |
# define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) |
10576 | 1531 |
CHOP(png_ptr->background.red); |
1532 |
CHOP(png_ptr->background.green); |
|
1533 |
CHOP(png_ptr->background.blue); |
|
1534 |
CHOP(png_ptr->background.gray); |
|
1535 |
# undef CHOP |
|
2 | 1536 |
} |
29913 | 1537 |
#endif /* READ_BACKGROUND && READ_EXPAND_16 */ |
1538 |
||
1539 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ |
|
1540 |
(defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ |
|
1541 |
defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) |
|
1542 |
if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 && |
|
1543 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
|
1544 |
(png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && |
|
1545 |
png_ptr->bit_depth == 16) |
|
1546 |
{ |
|
1547 |
/* On the other hand, if a 16-bit file is to be reduced to 8-bits per |
|
1548 |
* component this will also happen after PNG_COMPOSE and so the background |
|
1549 |
* color must be pre-expanded here. |
|
1550 |
* |
|
1551 |
* TODO: fix this too. |
|
1552 |
*/ |
|
1553 |
png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); |
|
1554 |
png_ptr->background.green = |
|
1555 |
(png_uint_16)(png_ptr->background.green * 257); |
|
1556 |
png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257); |
|
1557 |
png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257); |
|
1558 |
} |
|
1559 |
#endif |
|
10576 | 1560 |
|
1561 |
/* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the |
|
1562 |
* background support (see the comments in scripts/pnglibconf.dfa), this |
|
1563 |
* allows pre-multiplication of the alpha channel to be implemented as |
|
1564 |
* compositing on black. This is probably sub-optimal and has been done in |
|
1565 |
* 1.5.4 betas simply to enable external critique and testing (i.e. to |
|
1566 |
* implement the new API quickly, without lots of internal changes.) |
|
1567 |
*/ |
|
1568 |
||
1569 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
1570 |
# ifdef PNG_READ_BACKGROUND_SUPPORTED |
|
1571 |
/* Includes ALPHA_MODE */ |
|
1572 |
png_ptr->background_1 = png_ptr->background; |
|
1573 |
# endif |
|
1574 |
||
1575 |
/* This needs to change - in the palette image case a whole set of tables are |
|
1576 |
* built when it would be quicker to just calculate the correct value for |
|
1577 |
* each palette entry directly. Also, the test is too tricky - why check |
|
1578 |
* PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that |
|
1579 |
* PNG_GAMMA is cancelled even if the gamma is known? The test excludes the |
|
1580 |
* PNG_COMPOSE case, so apparently if there is no *overall* gamma correction |
|
1581 |
* the gamma tables will not be built even if composition is required on a |
|
1582 |
* gamma encoded value. |
|
1583 |
* |
|
1584 |
* In 1.5.4 this is addressed below by an additional check on the individual |
|
1585 |
* file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the |
|
1586 |
* tables. |
|
1587 |
*/ |
|
29913 | 1588 |
if ((png_ptr->transformations & PNG_GAMMA) != 0 || |
1589 |
((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 && |
|
1590 |
(png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
|
1591 |
png_gamma_significant(png_ptr->screen_gamma) != 0)) || |
|
1592 |
((png_ptr->transformations & PNG_COMPOSE) != 0 && |
|
1593 |
(png_gamma_significant(png_ptr->colorspace.gamma) != 0 || |
|
1594 |
png_gamma_significant(png_ptr->screen_gamma) != 0 |
|
10576 | 1595 |
# ifdef PNG_READ_BACKGROUND_SUPPORTED |
29913 | 1596 |
|| (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE && |
1597 |
png_gamma_significant(png_ptr->background_gamma) != 0) |
|
10576 | 1598 |
# endif |
29913 | 1599 |
)) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
1600 |
png_gamma_significant(png_ptr->screen_gamma) != 0)) |
|
2 | 1601 |
{ |
10576 | 1602 |
png_build_gamma_table(png_ptr, png_ptr->bit_depth); |
1603 |
||
1604 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
|
29913 | 1605 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
2 | 1606 |
{ |
29913 | 1607 |
/* Issue a warning about this combination: because RGB_TO_GRAY is |
1608 |
* optimized to do the gamma transform if present yet do_background has |
|
1609 |
* to do the same thing if both options are set a |
|
1610 |
* double-gamma-correction happens. This is true in all versions of |
|
1611 |
* libpng to date. |
|
1612 |
*/ |
|
1613 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
|
1614 |
png_warning(png_ptr, |
|
1615 |
"libpng does not support gamma+background+rgb_to_gray"); |
|
1616 |
||
1617 |
if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0) |
|
2 | 1618 |
{ |
10576 | 1619 |
/* We don't get to here unless there is a tRNS chunk with non-opaque |
1620 |
* entries - see the checking code at the start of this function. |
|
1621 |
*/ |
|
2 | 1622 |
png_color back, back_1; |
1623 |
png_colorp palette = png_ptr->palette; |
|
1624 |
int num_palette = png_ptr->num_palette; |
|
1625 |
int i; |
|
1626 |
if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) |
|
1627 |
{ |
|
10576 | 1628 |
|
2 | 1629 |
back.red = png_ptr->gamma_table[png_ptr->background.red]; |
1630 |
back.green = png_ptr->gamma_table[png_ptr->background.green]; |
|
1631 |
back.blue = png_ptr->gamma_table[png_ptr->background.blue]; |
|
1632 |
||
1633 |
back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; |
|
1634 |
back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; |
|
1635 |
back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; |
|
1636 |
} |
|
1637 |
else |
|
1638 |
{ |
|
10576 | 1639 |
png_fixed_point g, gs; |
2 | 1640 |
|
1641 |
switch (png_ptr->background_gamma_type) |
|
1642 |
{ |
|
1643 |
case PNG_BACKGROUND_GAMMA_SCREEN: |
|
1644 |
g = (png_ptr->screen_gamma); |
|
10576 | 1645 |
gs = PNG_FP_1; |
2 | 1646 |
break; |
10576 | 1647 |
|
2 | 1648 |
case PNG_BACKGROUND_GAMMA_FILE: |
29913 | 1649 |
g = png_reciprocal(png_ptr->colorspace.gamma); |
1650 |
gs = png_reciprocal2(png_ptr->colorspace.gamma, |
|
10576 | 1651 |
png_ptr->screen_gamma); |
2 | 1652 |
break; |
10576 | 1653 |
|
2 | 1654 |
case PNG_BACKGROUND_GAMMA_UNIQUE: |
10576 | 1655 |
g = png_reciprocal(png_ptr->background_gamma); |
1656 |
gs = png_reciprocal2(png_ptr->background_gamma, |
|
1657 |
png_ptr->screen_gamma); |
|
2 | 1658 |
break; |
1659 |
default: |
|
10576 | 1660 |
g = PNG_FP_1; /* back_1 */ |
1661 |
gs = PNG_FP_1; /* back */ |
|
1662 |
break; |
|
2 | 1663 |
} |
1664 |
||
29913 | 1665 |
if (png_gamma_significant(gs) != 0) |
10576 | 1666 |
{ |
1667 |
back.red = png_gamma_8bit_correct(png_ptr->background.red, |
|
1668 |
gs); |
|
1669 |
back.green = png_gamma_8bit_correct(png_ptr->background.green, |
|
1670 |
gs); |
|
1671 |
back.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
|
1672 |
gs); |
|
1673 |
} |
|
1674 |
||
1675 |
else |
|
2 | 1676 |
{ |
1677 |
back.red = (png_byte)png_ptr->background.red; |
|
1678 |
back.green = (png_byte)png_ptr->background.green; |
|
1679 |
back.blue = (png_byte)png_ptr->background.blue; |
|
1680 |
} |
|
10576 | 1681 |
|
29913 | 1682 |
if (png_gamma_significant(g) != 0) |
10576 | 1683 |
{ |
1684 |
back_1.red = png_gamma_8bit_correct(png_ptr->background.red, |
|
1685 |
g); |
|
1686 |
back_1.green = png_gamma_8bit_correct( |
|
1687 |
png_ptr->background.green, g); |
|
1688 |
back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, |
|
1689 |
g); |
|
1690 |
} |
|
1691 |
||
2 | 1692 |
else |
1693 |
{ |
|
10576 | 1694 |
back_1.red = (png_byte)png_ptr->background.red; |
1695 |
back_1.green = (png_byte)png_ptr->background.green; |
|
1696 |
back_1.blue = (png_byte)png_ptr->background.blue; |
|
2 | 1697 |
} |
1698 |
} |
|
10576 | 1699 |
|
2 | 1700 |
for (i = 0; i < num_palette; i++) |
1701 |
{ |
|
10576 | 1702 |
if (i < (int)png_ptr->num_trans && |
1703 |
png_ptr->trans_alpha[i] != 0xff) |
|
2 | 1704 |
{ |
10576 | 1705 |
if (png_ptr->trans_alpha[i] == 0) |
2 | 1706 |
{ |
1707 |
palette[i] = back; |
|
1708 |
} |
|
10576 | 1709 |
else /* if (png_ptr->trans_alpha[i] != 0xff) */ |
2 | 1710 |
{ |
1711 |
png_byte v, w; |
|
1712 |
||
1713 |
v = png_ptr->gamma_to_1[palette[i].red]; |
|
10576 | 1714 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.red); |
2 | 1715 |
palette[i].red = png_ptr->gamma_from_1[w]; |
1716 |
||
1717 |
v = png_ptr->gamma_to_1[palette[i].green]; |
|
10576 | 1718 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.green); |
2 | 1719 |
palette[i].green = png_ptr->gamma_from_1[w]; |
1720 |
||
1721 |
v = png_ptr->gamma_to_1[palette[i].blue]; |
|
10576 | 1722 |
png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue); |
2 | 1723 |
palette[i].blue = png_ptr->gamma_from_1[w]; |
1724 |
} |
|
1725 |
} |
|
1726 |
else |
|
1727 |
{ |
|
1728 |
palette[i].red = png_ptr->gamma_table[palette[i].red]; |
|
1729 |
palette[i].green = png_ptr->gamma_table[palette[i].green]; |
|
1730 |
palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
|
1731 |
} |
|
1732 |
} |
|
10576 | 1733 |
|
1734 |
/* Prevent the transformations being done again. |
|
1735 |
* |
|
29913 | 1736 |
* NOTE: this is highly dubious; it removes the transformations in |
10576 | 1737 |
* place. This seems inconsistent with the general treatment of the |
1738 |
* transformations elsewhere. |
|
1739 |
*/ |
|
1740 |
png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA); |
|
1741 |
} /* color_type == PNG_COLOR_TYPE_PALETTE */ |
|
1742 |
||
2 | 1743 |
/* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ |
10576 | 1744 |
else /* color_type != PNG_COLOR_TYPE_PALETTE */ |
2 | 1745 |
{ |
29913 | 1746 |
int gs_sig, g_sig; |
1747 |
png_fixed_point g = PNG_FP_1; /* Correction to linear */ |
|
1748 |
png_fixed_point gs = PNG_FP_1; /* Correction to screen */ |
|
2 | 1749 |
|
1750 |
switch (png_ptr->background_gamma_type) |
|
1751 |
{ |
|
1752 |
case PNG_BACKGROUND_GAMMA_SCREEN: |
|
10576 | 1753 |
g = png_ptr->screen_gamma; |
1754 |
/* gs = PNG_FP_1; */ |
|
2 | 1755 |
break; |
10576 | 1756 |
|
2 | 1757 |
case PNG_BACKGROUND_GAMMA_FILE: |
29913 | 1758 |
g = png_reciprocal(png_ptr->colorspace.gamma); |
1759 |
gs = png_reciprocal2(png_ptr->colorspace.gamma, |
|
1760 |
png_ptr->screen_gamma); |
|
2 | 1761 |
break; |
10576 | 1762 |
|
2 | 1763 |
case PNG_BACKGROUND_GAMMA_UNIQUE: |
10576 | 1764 |
g = png_reciprocal(png_ptr->background_gamma); |
1765 |
gs = png_reciprocal2(png_ptr->background_gamma, |
|
1766 |
png_ptr->screen_gamma); |
|
2 | 1767 |
break; |
10576 | 1768 |
|
1769 |
default: |
|
1770 |
png_error(png_ptr, "invalid background gamma type"); |
|
2 | 1771 |
} |
1772 |
||
29913 | 1773 |
g_sig = png_gamma_significant(g); |
1774 |
gs_sig = png_gamma_significant(gs); |
|
1775 |
||
1776 |
if (g_sig != 0) |
|
1777 |
png_ptr->background_1.gray = png_gamma_correct(png_ptr, |
|
1778 |
png_ptr->background.gray, g); |
|
1779 |
||
1780 |
if (gs_sig != 0) |
|
1781 |
png_ptr->background.gray = png_gamma_correct(png_ptr, |
|
1782 |
png_ptr->background.gray, gs); |
|
2 | 1783 |
|
1784 |
if ((png_ptr->background.red != png_ptr->background.green) || |
|
1785 |
(png_ptr->background.red != png_ptr->background.blue) || |
|
1786 |
(png_ptr->background.red != png_ptr->background.gray)) |
|
1787 |
{ |
|
1788 |
/* RGB or RGBA with color background */ |
|
29913 | 1789 |
if (g_sig != 0) |
1790 |
{ |
|
1791 |
png_ptr->background_1.red = png_gamma_correct(png_ptr, |
|
1792 |
png_ptr->background.red, g); |
|
1793 |
||
1794 |
png_ptr->background_1.green = png_gamma_correct(png_ptr, |
|
1795 |
png_ptr->background.green, g); |
|
1796 |
||
1797 |
png_ptr->background_1.blue = png_gamma_correct(png_ptr, |
|
1798 |
png_ptr->background.blue, g); |
|
1799 |
} |
|
1800 |
||
1801 |
if (gs_sig != 0) |
|
1802 |
{ |
|
1803 |
png_ptr->background.red = png_gamma_correct(png_ptr, |
|
1804 |
png_ptr->background.red, gs); |
|
1805 |
||
1806 |
png_ptr->background.green = png_gamma_correct(png_ptr, |
|
1807 |
png_ptr->background.green, gs); |
|
1808 |
||
1809 |
png_ptr->background.blue = png_gamma_correct(png_ptr, |
|
1810 |
png_ptr->background.blue, gs); |
|
1811 |
} |
|
2 | 1812 |
} |
10576 | 1813 |
|
2 | 1814 |
else |
1815 |
{ |
|
1816 |
/* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ |
|
1817 |
png_ptr->background_1.red = png_ptr->background_1.green |
|
10576 | 1818 |
= png_ptr->background_1.blue = png_ptr->background_1.gray; |
1819 |
||
2 | 1820 |
png_ptr->background.red = png_ptr->background.green |
10576 | 1821 |
= png_ptr->background.blue = png_ptr->background.gray; |
2 | 1822 |
} |
29913 | 1823 |
|
1824 |
/* The background is now in screen gamma: */ |
|
1825 |
png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; |
|
10576 | 1826 |
} /* color_type != PNG_COLOR_TYPE_PALETTE */ |
1827 |
}/* png_ptr->transformations & PNG_BACKGROUND */ |
|
1828 |
||
2 | 1829 |
else |
10576 | 1830 |
/* Transformation does not include PNG_BACKGROUND */ |
29913 | 1831 |
#endif /* READ_BACKGROUND */ |
1832 |
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE |
|
1833 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
|
1834 |
/* RGB_TO_GRAY needs to have non-gamma-corrected values! */ |
|
1835 |
&& ((png_ptr->transformations & PNG_EXPAND) == 0 || |
|
1836 |
(png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) |
|
1837 |
#endif |
|
1838 |
) |
|
2 | 1839 |
{ |
1840 |
png_colorp palette = png_ptr->palette; |
|
1841 |
int num_palette = png_ptr->num_palette; |
|
1842 |
int i; |
|
1843 |
||
29913 | 1844 |
/* NOTE: there are other transformations that should probably be in |
1845 |
* here too. |
|
10576 | 1846 |
*/ |
2 | 1847 |
for (i = 0; i < num_palette; i++) |
1848 |
{ |
|
1849 |
palette[i].red = png_ptr->gamma_table[palette[i].red]; |
|
1850 |
palette[i].green = png_ptr->gamma_table[palette[i].green]; |
|
1851 |
palette[i].blue = png_ptr->gamma_table[palette[i].blue]; |
|
1852 |
} |
|
10576 | 1853 |
|
1854 |
/* Done the gamma correction. */ |
|
1855 |
png_ptr->transformations &= ~PNG_GAMMA; |
|
1856 |
} /* color_type == PALETTE && !PNG_BACKGROUND transformation */ |
|
2 | 1857 |
} |
10576 | 1858 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
2 | 1859 |
else |
1860 |
#endif |
|
29913 | 1861 |
#endif /* READ_GAMMA */ |
10576 | 1862 |
|
1863 |
#ifdef PNG_READ_BACKGROUND_SUPPORTED |
|
1864 |
/* No GAMMA transformation (see the hanging else 4 lines above) */ |
|
29913 | 1865 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0 && |
10576 | 1866 |
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
2 | 1867 |
{ |
1868 |
int i; |
|
1869 |
int istop = (int)png_ptr->num_trans; |
|
1870 |
png_color back; |
|
1871 |
png_colorp palette = png_ptr->palette; |
|
1872 |
||
1873 |
back.red = (png_byte)png_ptr->background.red; |
|
1874 |
back.green = (png_byte)png_ptr->background.green; |
|
1875 |
back.blue = (png_byte)png_ptr->background.blue; |
|
1876 |
||
1877 |
for (i = 0; i < istop; i++) |
|
1878 |
{ |
|
10576 | 1879 |
if (png_ptr->trans_alpha[i] == 0) |
2 | 1880 |
{ |
1881 |
palette[i] = back; |
|
1882 |
} |
|
10576 | 1883 |
|
1884 |
else if (png_ptr->trans_alpha[i] != 0xff) |
|
2 | 1885 |
{ |
1886 |
/* The png_composite() macro is defined in png.h */ |
|
1887 |
png_composite(palette[i].red, palette[i].red, |
|
10576 | 1888 |
png_ptr->trans_alpha[i], back.red); |
1889 |
||
2 | 1890 |
png_composite(palette[i].green, palette[i].green, |
10576 | 1891 |
png_ptr->trans_alpha[i], back.green); |
1892 |
||
2 | 1893 |
png_composite(palette[i].blue, palette[i].blue, |
10576 | 1894 |
png_ptr->trans_alpha[i], back.blue); |
2 | 1895 |
} |
1896 |
} |
|
10576 | 1897 |
|
1898 |
png_ptr->transformations &= ~PNG_COMPOSE; |
|
2 | 1899 |
} |
29913 | 1900 |
#endif /* READ_BACKGROUND */ |
2 | 1901 |
|
10576 | 1902 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
29913 | 1903 |
if ((png_ptr->transformations & PNG_SHIFT) != 0 && |
1904 |
(png_ptr->transformations & PNG_EXPAND) == 0 && |
|
10576 | 1905 |
(png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) |
2 | 1906 |
{ |
29913 | 1907 |
int i; |
1908 |
int istop = png_ptr->num_palette; |
|
1909 |
int shift = 8 - png_ptr->sig_bit.red; |
|
1910 |
||
1911 |
png_ptr->transformations &= ~PNG_SHIFT; |
|
1912 |
||
1913 |
/* significant bits can be in the range 1 to 7 for a meaninful result, if |
|
1914 |
* the number of significant bits is 0 then no shift is done (this is an |
|
1915 |
* error condition which is silently ignored.) |
|
1916 |
*/ |
|
1917 |
if (shift > 0 && shift < 8) |
|
1918 |
for (i=0; i<istop; ++i) |
|
1919 |
{ |
|
1920 |
int component = png_ptr->palette[i].red; |
|
1921 |
||
1922 |
component >>= shift; |
|
1923 |
png_ptr->palette[i].red = (png_byte)component; |
|
1924 |
} |
|
1925 |
||
1926 |
shift = 8 - png_ptr->sig_bit.green; |
|
1927 |
if (shift > 0 && shift < 8) |
|
1928 |
for (i=0; i<istop; ++i) |
|
1929 |
{ |
|
1930 |
int component = png_ptr->palette[i].green; |
|
1931 |
||
1932 |
component >>= shift; |
|
1933 |
png_ptr->palette[i].green = (png_byte)component; |
|
1934 |
} |
|
1935 |
||
1936 |
shift = 8 - png_ptr->sig_bit.blue; |
|
1937 |
if (shift > 0 && shift < 8) |
|
1938 |
for (i=0; i<istop; ++i) |
|
1939 |
{ |
|
1940 |
int component = png_ptr->palette[i].blue; |
|
1941 |
||
1942 |
component >>= shift; |
|
1943 |
png_ptr->palette[i].blue = (png_byte)component; |
|
1944 |
} |
|
2 | 1945 |
} |
40436
94ae0ede53a5
8155690: Update libPNG library to the latest up-to-date
azvegint
parents:
35692
diff
changeset
|
1946 |
#endif /* READ_SHIFT */ |
2 | 1947 |
} |
1948 |
||
1949 |
/* Modify the info structure to reflect the transformations. The |
|
1950 |
* info should be updated so a PNG file could be written with it, |
|
1951 |
* assuming the transformations result in valid PNG data. |
|
1952 |
*/ |
|
1953 |
void /* PRIVATE */ |
|
29913 | 1954 |
png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) |
2 | 1955 |
{ |
10576 | 1956 |
png_debug(1, "in png_read_transform_info"); |
1957 |
||
1958 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
|
29913 | 1959 |
if ((png_ptr->transformations & PNG_EXPAND) != 0) |
2 | 1960 |
{ |
1961 |
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
|
1962 |
{ |
|
10576 | 1963 |
/* This check must match what actually happens in |
1964 |
* png_do_expand_palette; if it ever checks the tRNS chunk to see if |
|
1965 |
* it is all opaque we must do the same (at present it does not.) |
|
1966 |
*/ |
|
1967 |
if (png_ptr->num_trans > 0) |
|
2 | 1968 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
10576 | 1969 |
|
2 | 1970 |
else |
1971 |
info_ptr->color_type = PNG_COLOR_TYPE_RGB; |
|
10576 | 1972 |
|
2 | 1973 |
info_ptr->bit_depth = 8; |
1974 |
info_ptr->num_trans = 0; |
|
23903 | 1975 |
|
1976 |
if (png_ptr->palette == NULL) |
|
1977 |
png_error (png_ptr, "Palette is NULL in indexed image"); |
|
2 | 1978 |
} |
1979 |
else |
|
1980 |
{ |
|
29913 | 1981 |
if (png_ptr->num_trans != 0) |
2 | 1982 |
{ |
29913 | 1983 |
if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
10576 | 1984 |
info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
2 | 1985 |
} |
1986 |
if (info_ptr->bit_depth < 8) |
|
1987 |
info_ptr->bit_depth = 8; |
|
10576 | 1988 |
|
2 | 1989 |
info_ptr->num_trans = 0; |
1990 |
} |
|
1991 |
} |
|
1992 |
#endif |
|
1993 |
||
10576 | 1994 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
1995 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
|
1996 |
/* The following is almost certainly wrong unless the background value is in |
|
1997 |
* the screen space! |
|
1998 |
*/ |
|
29913 | 1999 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
2 | 2000 |
info_ptr->background = png_ptr->background; |
2001 |
#endif |
|
2002 |
||
10576 | 2003 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2004 |
/* The following used to be conditional on PNG_GAMMA (prior to 1.5.4), |
|
2005 |
* however it seems that the code in png_init_read_transformations, which has |
|
2006 |
* been called before this from png_read_update_info->png_read_start_row |
|
2007 |
* sometimes does the gamma transform and cancels the flag. |
|
29913 | 2008 |
* |
2009 |
* TODO: this looks wrong; the info_ptr should end up with a gamma equal to |
|
2010 |
* the screen_gamma value. The following probably results in weirdness if |
|
2011 |
* the info_ptr is used by the app after the rows have been read. |
|
10576 | 2012 |
*/ |
29913 | 2013 |
info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; |
10576 | 2014 |
#endif |
2015 |
||
2016 |
if (info_ptr->bit_depth == 16) |
|
2 | 2017 |
{ |
10576 | 2018 |
# ifdef PNG_READ_16BIT_SUPPORTED |
2019 |
# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
|
29913 | 2020 |
if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
10576 | 2021 |
info_ptr->bit_depth = 8; |
2022 |
# endif |
|
2023 |
||
2024 |
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
|
29913 | 2025 |
if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
10576 | 2026 |
info_ptr->bit_depth = 8; |
2027 |
# endif |
|
2028 |
||
2029 |
# else |
|
35296 | 2030 |
/* No 16-bit support: force chopping 16-bit input down to 8, in this case |
10576 | 2031 |
* the app program can chose if both APIs are available by setting the |
2032 |
* correct scaling to use. |
|
2033 |
*/ |
|
2034 |
# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
|
2035 |
/* For compatibility with previous versions use the strip method by |
|
2036 |
* default. This code works because if PNG_SCALE_16_TO_8 is already |
|
2037 |
* set the code below will do that in preference to the chop. |
|
2038 |
*/ |
|
2039 |
png_ptr->transformations |= PNG_16_TO_8; |
|
2040 |
info_ptr->bit_depth = 8; |
|
2041 |
# else |
|
2042 |
||
29913 | 2043 |
# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
10576 | 2044 |
png_ptr->transformations |= PNG_SCALE_16_TO_8; |
2045 |
info_ptr->bit_depth = 8; |
|
2046 |
# else |
|
2047 |
||
2048 |
CONFIGURATION ERROR: you must enable at least one 16 to 8 method |
|
2049 |
# endif |
|
2050 |
# endif |
|
29913 | 2051 |
#endif /* !READ_16BIT */ |
2 | 2052 |
} |
10576 | 2053 |
|
2054 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
|
29913 | 2055 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) |
2056 |
info_ptr->color_type = (png_byte)(info_ptr->color_type | |
|
2057 |
PNG_COLOR_MASK_COLOR); |
|
2 | 2058 |
#endif |
2059 |
||
10576 | 2060 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
29913 | 2061 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
2062 |
info_ptr->color_type = (png_byte)(info_ptr->color_type & |
|
2063 |
~PNG_COLOR_MASK_COLOR); |
|
2 | 2064 |
#endif |
2065 |
||
10576 | 2066 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
29913 | 2067 |
if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
2 | 2068 |
{ |
2069 |
if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || |
|
10576 | 2070 |
(info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && |
29913 | 2071 |
png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8) |
2 | 2072 |
{ |
2073 |
info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; |
|
2074 |
} |
|
2075 |
} |
|
2076 |
#endif |
|
2077 |
||
10576 | 2078 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
29913 | 2079 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && |
2080 |
info_ptr->bit_depth == 8 && |
|
2081 |
info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) |
|
10576 | 2082 |
{ |
2083 |
info_ptr->bit_depth = 16; |
|
2084 |
} |
|
2085 |
#endif |
|
2086 |
||
2087 |
#ifdef PNG_READ_PACK_SUPPORTED |
|
29913 | 2088 |
if ((png_ptr->transformations & PNG_PACK) != 0 && |
2089 |
(info_ptr->bit_depth < 8)) |
|
2 | 2090 |
info_ptr->bit_depth = 8; |
2091 |
#endif |
|
2092 |
||
2093 |
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) |
|
2094 |
info_ptr->channels = 1; |
|
10576 | 2095 |
|
29913 | 2096 |
else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) |
2 | 2097 |
info_ptr->channels = 3; |
10576 | 2098 |
|
2 | 2099 |
else |
2100 |
info_ptr->channels = 1; |
|
2101 |
||
10576 | 2102 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
29913 | 2103 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0) |
10576 | 2104 |
{ |
29913 | 2105 |
info_ptr->color_type = (png_byte)(info_ptr->color_type & |
2106 |
~PNG_COLOR_MASK_ALPHA); |
|
10576 | 2107 |
info_ptr->num_trans = 0; |
2108 |
} |
|
2 | 2109 |
#endif |
2110 |
||
29913 | 2111 |
if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
2 | 2112 |
info_ptr->channels++; |
2113 |
||
10576 | 2114 |
#ifdef PNG_READ_FILLER_SUPPORTED |
2 | 2115 |
/* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ |
29913 | 2116 |
if ((png_ptr->transformations & PNG_FILLER) != 0 && |
2117 |
(info_ptr->color_type == PNG_COLOR_TYPE_RGB || |
|
2118 |
info_ptr->color_type == PNG_COLOR_TYPE_GRAY)) |
|
2 | 2119 |
{ |
2120 |
info_ptr->channels++; |
|
10576 | 2121 |
/* If adding a true alpha channel not just filler */ |
29913 | 2122 |
if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0) |
10576 | 2123 |
info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; |
2 | 2124 |
} |
2125 |
#endif |
|
2126 |
||
2127 |
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ |
|
2128 |
defined(PNG_READ_USER_TRANSFORM_SUPPORTED) |
|
29913 | 2129 |
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
10576 | 2130 |
{ |
35296 | 2131 |
if (png_ptr->user_transform_depth != 0) |
2 | 2132 |
info_ptr->bit_depth = png_ptr->user_transform_depth; |
10576 | 2133 |
|
35296 | 2134 |
if (png_ptr->user_transform_channels != 0) |
2 | 2135 |
info_ptr->channels = png_ptr->user_transform_channels; |
10576 | 2136 |
} |
2 | 2137 |
#endif |
2138 |
||
2139 |
info_ptr->pixel_depth = (png_byte)(info_ptr->channels * |
|
10576 | 2140 |
info_ptr->bit_depth); |
2141 |
||
2142 |
info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); |
|
2143 |
||
2144 |
/* Adding in 1.5.4: cache the above value in png_struct so that we can later |
|
2145 |
* check in png_rowbytes that the user buffer won't get overwritten. Note |
|
2146 |
* that the field is not always set - if png_read_update_info isn't called |
|
2147 |
* the application has to either not do any transforms or get the calculation |
|
2148 |
* right itself. |
|
2149 |
*/ |
|
2150 |
png_ptr->info_rowbytes = info_ptr->rowbytes; |
|
2151 |
||
2152 |
#ifndef PNG_READ_EXPAND_SUPPORTED |
|
29913 | 2153 |
if (png_ptr != NULL) |
2 | 2154 |
return; |
2155 |
#endif |
|
2156 |
} |
|
2157 |
||
10576 | 2158 |
#ifdef PNG_READ_PACK_SUPPORTED |
2 | 2159 |
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, |
2160 |
* without changing the actual values. Thus, if you had a row with |
|
2161 |
* a bit depth of 1, you would end up with bytes that only contained |
|
2162 |
* the numbers 0 or 1. If you would rather they contain 0 and 255, use |
|
2163 |
* png_do_shift() after this. |
|
2164 |
*/ |
|
29913 | 2165 |
static void |
2 | 2166 |
png_do_unpack(png_row_infop row_info, png_bytep row) |
2167 |
{ |
|
10576 | 2168 |
png_debug(1, "in png_do_unpack"); |
2169 |
||
2 | 2170 |
if (row_info->bit_depth < 8) |
2171 |
{ |
|
2172 |
png_uint_32 i; |
|
2173 |
png_uint_32 row_width=row_info->width; |
|
2174 |
||
2175 |
switch (row_info->bit_depth) |
|
2176 |
{ |
|
2177 |
case 1: |
|
2178 |
{ |
|
2179 |
png_bytep sp = row + (png_size_t)((row_width - 1) >> 3); |
|
2180 |
png_bytep dp = row + (png_size_t)row_width - 1; |
|
2181 |
png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07); |
|
2182 |
for (i = 0; i < row_width; i++) |
|
2183 |
{ |
|
2184 |
*dp = (png_byte)((*sp >> shift) & 0x01); |
|
10576 | 2185 |
|
2 | 2186 |
if (shift == 7) |
2187 |
{ |
|
2188 |
shift = 0; |
|
2189 |
sp--; |
|
2190 |
} |
|
10576 | 2191 |
|
2 | 2192 |
else |
2193 |
shift++; |
|
2194 |
||
2195 |
dp--; |
|
2196 |
} |
|
2197 |
break; |
|
2198 |
} |
|
10576 | 2199 |
|
2 | 2200 |
case 2: |
2201 |
{ |
|
2202 |
||
2203 |
png_bytep sp = row + (png_size_t)((row_width - 1) >> 2); |
|
2204 |
png_bytep dp = row + (png_size_t)row_width - 1; |
|
2205 |
png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
|
2206 |
for (i = 0; i < row_width; i++) |
|
2207 |
{ |
|
2208 |
*dp = (png_byte)((*sp >> shift) & 0x03); |
|
10576 | 2209 |
|
2 | 2210 |
if (shift == 6) |
2211 |
{ |
|
2212 |
shift = 0; |
|
2213 |
sp--; |
|
2214 |
} |
|
10576 | 2215 |
|
2 | 2216 |
else |
2217 |
shift += 2; |
|
2218 |
||
2219 |
dp--; |
|
2220 |
} |
|
2221 |
break; |
|
2222 |
} |
|
10576 | 2223 |
|
2 | 2224 |
case 4: |
2225 |
{ |
|
2226 |
png_bytep sp = row + (png_size_t)((row_width - 1) >> 1); |
|
2227 |
png_bytep dp = row + (png_size_t)row_width - 1; |
|
2228 |
png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
|
2229 |
for (i = 0; i < row_width; i++) |
|
2230 |
{ |
|
2231 |
*dp = (png_byte)((*sp >> shift) & 0x0f); |
|
10576 | 2232 |
|
2 | 2233 |
if (shift == 4) |
2234 |
{ |
|
2235 |
shift = 0; |
|
2236 |
sp--; |
|
2237 |
} |
|
10576 | 2238 |
|
2 | 2239 |
else |
2240 |
shift = 4; |
|
2241 |
||
2242 |
dp--; |
|
2243 |
} |
|
2244 |
break; |
|
2245 |
} |
|
10576 | 2246 |
|
2247 |
default: |
|
2248 |
break; |
|
2 | 2249 |
} |
2250 |
row_info->bit_depth = 8; |
|
2251 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
|
2252 |
row_info->rowbytes = row_width * row_info->channels; |
|
2253 |
} |
|
2254 |
} |
|
2255 |
#endif |
|
2256 |
||
10576 | 2257 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
2 | 2258 |
/* Reverse the effects of png_do_shift. This routine merely shifts the |
2259 |
* pixels back to their significant bits values. Thus, if you have |
|
2260 |
* a row of bit depth 8, but only 5 are significant, this will shift |
|
2261 |
* the values back to 0 through 31. |
|
2262 |
*/ |
|
29913 | 2263 |
static void |
10576 | 2264 |
png_do_unshift(png_row_infop row_info, png_bytep row, |
2265 |
png_const_color_8p sig_bits) |
|
2 | 2266 |
{ |
29913 | 2267 |
int color_type; |
2268 |
||
10576 | 2269 |
png_debug(1, "in png_do_unshift"); |
2270 |
||
29913 | 2271 |
/* The palette case has already been handled in the _init routine. */ |
2272 |
color_type = row_info->color_type; |
|
2273 |
||
2274 |
if (color_type != PNG_COLOR_TYPE_PALETTE) |
|
2 | 2275 |
{ |
2276 |
int shift[4]; |
|
2277 |
int channels = 0; |
|
29913 | 2278 |
int bit_depth = row_info->bit_depth; |
2279 |
||
2280 |
if ((color_type & PNG_COLOR_MASK_COLOR) != 0) |
|
2 | 2281 |
{ |
29913 | 2282 |
shift[channels++] = bit_depth - sig_bits->red; |
2283 |
shift[channels++] = bit_depth - sig_bits->green; |
|
2284 |
shift[channels++] = bit_depth - sig_bits->blue; |
|
2 | 2285 |
} |
10576 | 2286 |
|
2 | 2287 |
else |
2288 |
{ |
|
29913 | 2289 |
shift[channels++] = bit_depth - sig_bits->gray; |
2 | 2290 |
} |
10576 | 2291 |
|
29913 | 2292 |
if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) |
2 | 2293 |
{ |
29913 | 2294 |
shift[channels++] = bit_depth - sig_bits->alpha; |
2 | 2295 |
} |
2296 |
||
2297 |
{ |
|
29913 | 2298 |
int c, have_shift; |
2299 |
||
2300 |
for (c = have_shift = 0; c < channels; ++c) |
|
2301 |
{ |
|
2302 |
/* A shift of more than the bit depth is an error condition but it |
|
2303 |
* gets ignored here. |
|
2304 |
*/ |
|
2305 |
if (shift[c] <= 0 || shift[c] >= bit_depth) |
|
2306 |
shift[c] = 0; |
|
2307 |
||
2308 |
else |
|
2309 |
have_shift = 1; |
|
2310 |
} |
|
2311 |
||
2312 |
if (have_shift == 0) |
|
2313 |
return; |
|
2 | 2314 |
} |
2315 |
||
29913 | 2316 |
switch (bit_depth) |
2 | 2317 |
{ |
10576 | 2318 |
default: |
29913 | 2319 |
/* Must be 1bpp gray: should not be here! */ |
2320 |
/* NOTREACHED */ |
|
10576 | 2321 |
break; |
2322 |
||
2 | 2323 |
case 2: |
29913 | 2324 |
/* Must be 2bpp gray */ |
2325 |
/* assert(channels == 1 && shift[0] == 1) */ |
|
2 | 2326 |
{ |
29913 | 2327 |
png_bytep bp = row; |
2328 |
png_bytep bp_end = bp + row_info->rowbytes; |
|
2329 |
||
2330 |
while (bp < bp_end) |
|
2 | 2331 |
{ |
29913 | 2332 |
int b = (*bp >> 1) & 0x55; |
2333 |
*bp++ = (png_byte)b; |
|
2 | 2334 |
} |
2335 |
break; |
|
2336 |
} |
|
10576 | 2337 |
|
2 | 2338 |
case 4: |
29913 | 2339 |
/* Must be 4bpp gray */ |
2340 |
/* assert(channels == 1) */ |
|
2 | 2341 |
{ |
2342 |
png_bytep bp = row; |
|
29913 | 2343 |
png_bytep bp_end = bp + row_info->rowbytes; |
2344 |
int gray_shift = shift[0]; |
|
2345 |
int mask = 0xf >> gray_shift; |
|
2346 |
||
2347 |
mask |= mask << 4; |
|
2348 |
||
2349 |
while (bp < bp_end) |
|
2 | 2350 |
{ |
29913 | 2351 |
int b = (*bp >> gray_shift) & mask; |
2352 |
*bp++ = (png_byte)b; |
|
2 | 2353 |
} |
2354 |
break; |
|
2355 |
} |
|
10576 | 2356 |
|
2 | 2357 |
case 8: |
29913 | 2358 |
/* Single byte components, G, GA, RGB, RGBA */ |
2 | 2359 |
{ |
2360 |
png_bytep bp = row; |
|
29913 | 2361 |
png_bytep bp_end = bp + row_info->rowbytes; |
2362 |
int channel = 0; |
|
2363 |
||
2364 |
while (bp < bp_end) |
|
2 | 2365 |
{ |
29913 | 2366 |
int b = *bp >> shift[channel]; |
2367 |
if (++channel >= channels) |
|
2368 |
channel = 0; |
|
2369 |
*bp++ = (png_byte)b; |
|
2 | 2370 |
} |
2371 |
break; |
|
2372 |
} |
|
10576 | 2373 |
|
2374 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2 | 2375 |
case 16: |
29913 | 2376 |
/* Double byte components, G, GA, RGB, RGBA */ |
2 | 2377 |
{ |
2378 |
png_bytep bp = row; |
|
29913 | 2379 |
png_bytep bp_end = bp + row_info->rowbytes; |
2380 |
int channel = 0; |
|
2381 |
||
2382 |
while (bp < bp_end) |
|
2 | 2383 |
{ |
29913 | 2384 |
int value = (bp[0] << 8) + bp[1]; |
2385 |
||
2386 |
value >>= shift[channel]; |
|
2387 |
if (++channel >= channels) |
|
2388 |
channel = 0; |
|
2 | 2389 |
*bp++ = (png_byte)(value >> 8); |
35296 | 2390 |
*bp++ = (png_byte)value; |
2 | 2391 |
} |
2392 |
break; |
|
2393 |
} |
|
10576 | 2394 |
#endif |
2 | 2395 |
} |
2396 |
} |
|
2397 |
} |
|
2398 |
#endif |
|
2399 |
||
10576 | 2400 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
2401 |
/* Scale rows of bit depth 16 down to 8 accurately */ |
|
29913 | 2402 |
static void |
10576 | 2403 |
png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) |
2 | 2404 |
{ |
10576 | 2405 |
png_debug(1, "in png_do_scale_16_to_8"); |
2406 |
||
2 | 2407 |
if (row_info->bit_depth == 16) |
2408 |
{ |
|
10576 | 2409 |
png_bytep sp = row; /* source */ |
29913 | 2410 |
png_bytep dp = row; /* destination */ |
10576 | 2411 |
png_bytep ep = sp + row_info->rowbytes; /* end+1 */ |
2412 |
||
2413 |
while (sp < ep) |
|
2 | 2414 |
{ |
35296 | 2415 |
/* The input is an array of 16-bit components, these must be scaled to |
2416 |
* 8 bits each. For a 16-bit value V the required value (from the PNG |
|
10576 | 2417 |
* specification) is: |
2418 |
* |
|
2419 |
* (V * 255) / 65535 |
|
2420 |
* |
|
2421 |
* This reduces to round(V / 257), or floor((V + 128.5)/257) |
|
2422 |
* |
|
2423 |
* Represent V as the two byte value vhi.vlo. Make a guess that the |
|
2424 |
* result is the top byte of V, vhi, then the correction to this value |
|
2425 |
* is: |
|
2426 |
* |
|
2427 |
* error = floor(((V-vhi.vhi) + 128.5) / 257) |
|
2428 |
* = floor(((vlo-vhi) + 128.5) / 257) |
|
2429 |
* |
|
2430 |
* This can be approximated using integer arithmetic (and a signed |
|
2431 |
* shift): |
|
2432 |
* |
|
2433 |
* error = (vlo-vhi+128) >> 8; |
|
2434 |
* |
|
2435 |
* The approximate differs from the exact answer only when (vlo-vhi) is |
|
2436 |
* 128; it then gives a correction of +1 when the exact correction is |
|
35296 | 2437 |
* 0. This gives 128 errors. The exact answer (correct for all 16-bit |
10576 | 2438 |
* input values) is: |
2439 |
* |
|
2440 |
* error = (vlo-vhi+128)*65535 >> 24; |
|
2441 |
* |
|
2442 |
* An alternative arithmetic calculation which also gives no errors is: |
|
2443 |
* |
|
2444 |
* (V * 255 + 32895) >> 16 |
|
2445 |
*/ |
|
2446 |
||
2447 |
png_int_32 tmp = *sp++; /* must be signed! */ |
|
2448 |
tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; |
|
2449 |
*dp++ = (png_byte)tmp; |
|
2 | 2450 |
} |
10576 | 2451 |
|
2 | 2452 |
row_info->bit_depth = 8; |
2453 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
|
2454 |
row_info->rowbytes = row_info->width * row_info->channels; |
|
2455 |
} |
|
2456 |
} |
|
2457 |
#endif |
|
2458 |
||
10576 | 2459 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
29913 | 2460 |
static void |
10576 | 2461 |
/* Simply discard the low byte. This was the default behavior prior |
2462 |
* to libpng-1.5.4. |
|
2463 |
*/ |
|
2464 |
png_do_chop(png_row_infop row_info, png_bytep row) |
|
2465 |
{ |
|
2466 |
png_debug(1, "in png_do_chop"); |
|
2467 |
||
2468 |
if (row_info->bit_depth == 16) |
|
2469 |
{ |
|
2470 |
png_bytep sp = row; /* source */ |
|
29913 | 2471 |
png_bytep dp = row; /* destination */ |
10576 | 2472 |
png_bytep ep = sp + row_info->rowbytes; /* end+1 */ |
2473 |
||
2474 |
while (sp < ep) |
|
2475 |
{ |
|
2476 |
*dp++ = *sp; |
|
2477 |
sp += 2; /* skip low byte */ |
|
2478 |
} |
|
2479 |
||
2480 |
row_info->bit_depth = 8; |
|
2481 |
row_info->pixel_depth = (png_byte)(8 * row_info->channels); |
|
2482 |
row_info->rowbytes = row_info->width * row_info->channels; |
|
2483 |
} |
|
2484 |
} |
|
2485 |
#endif |
|
2486 |
||
2487 |
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
|
29913 | 2488 |
static void |
2 | 2489 |
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) |
2490 |
{ |
|
10576 | 2491 |
png_debug(1, "in png_do_read_swap_alpha"); |
2492 |
||
2 | 2493 |
{ |
2494 |
png_uint_32 row_width = row_info->width; |
|
2495 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
|
2496 |
{ |
|
2497 |
/* This converts from RGBA to ARGB */ |
|
2498 |
if (row_info->bit_depth == 8) |
|
2499 |
{ |
|
2500 |
png_bytep sp = row + row_info->rowbytes; |
|
2501 |
png_bytep dp = sp; |
|
2502 |
png_byte save; |
|
2503 |
png_uint_32 i; |
|
2504 |
||
2505 |
for (i = 0; i < row_width; i++) |
|
2506 |
{ |
|
2507 |
save = *(--sp); |
|
2508 |
*(--dp) = *(--sp); |
|
2509 |
*(--dp) = *(--sp); |
|
2510 |
*(--dp) = *(--sp); |
|
2511 |
*(--dp) = save; |
|
2512 |
} |
|
2513 |
} |
|
10576 | 2514 |
|
2515 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2 | 2516 |
/* This converts from RRGGBBAA to AARRGGBB */ |
2517 |
else |
|
2518 |
{ |
|
2519 |
png_bytep sp = row + row_info->rowbytes; |
|
2520 |
png_bytep dp = sp; |
|
2521 |
png_byte save[2]; |
|
2522 |
png_uint_32 i; |
|
2523 |
||
2524 |
for (i = 0; i < row_width; i++) |
|
2525 |
{ |
|
2526 |
save[0] = *(--sp); |
|
2527 |
save[1] = *(--sp); |
|
2528 |
*(--dp) = *(--sp); |
|
2529 |
*(--dp) = *(--sp); |
|
2530 |
*(--dp) = *(--sp); |
|
2531 |
*(--dp) = *(--sp); |
|
2532 |
*(--dp) = *(--sp); |
|
2533 |
*(--dp) = *(--sp); |
|
2534 |
*(--dp) = save[0]; |
|
2535 |
*(--dp) = save[1]; |
|
2536 |
} |
|
2537 |
} |
|
10576 | 2538 |
#endif |
2 | 2539 |
} |
10576 | 2540 |
|
2 | 2541 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
2542 |
{ |
|
2543 |
/* This converts from GA to AG */ |
|
2544 |
if (row_info->bit_depth == 8) |
|
2545 |
{ |
|
2546 |
png_bytep sp = row + row_info->rowbytes; |
|
2547 |
png_bytep dp = sp; |
|
2548 |
png_byte save; |
|
2549 |
png_uint_32 i; |
|
2550 |
||
2551 |
for (i = 0; i < row_width; i++) |
|
2552 |
{ |
|
2553 |
save = *(--sp); |
|
2554 |
*(--dp) = *(--sp); |
|
2555 |
*(--dp) = save; |
|
2556 |
} |
|
2557 |
} |
|
10576 | 2558 |
|
2559 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2 | 2560 |
/* This converts from GGAA to AAGG */ |
2561 |
else |
|
2562 |
{ |
|
2563 |
png_bytep sp = row + row_info->rowbytes; |
|
2564 |
png_bytep dp = sp; |
|
2565 |
png_byte save[2]; |
|
2566 |
png_uint_32 i; |
|
2567 |
||
2568 |
for (i = 0; i < row_width; i++) |
|
2569 |
{ |
|
2570 |
save[0] = *(--sp); |
|
2571 |
save[1] = *(--sp); |
|
2572 |
*(--dp) = *(--sp); |
|
2573 |
*(--dp) = *(--sp); |
|
2574 |
*(--dp) = save[0]; |
|
2575 |
*(--dp) = save[1]; |
|
2576 |
} |
|
2577 |
} |
|
10576 | 2578 |
#endif |
2 | 2579 |
} |
2580 |
} |
|
2581 |
} |
|
2582 |
#endif |
|
2583 |
||
10576 | 2584 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
29913 | 2585 |
static void |
2 | 2586 |
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) |
2587 |
{ |
|
10576 | 2588 |
png_uint_32 row_width; |
2589 |
png_debug(1, "in png_do_read_invert_alpha"); |
|
2590 |
||
2591 |
row_width = row_info->width; |
|
2592 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) |
|
2 | 2593 |
{ |
10576 | 2594 |
if (row_info->bit_depth == 8) |
2 | 2595 |
{ |
2596 |
/* This inverts the alpha channel in RGBA */ |
|
10576 | 2597 |
png_bytep sp = row + row_info->rowbytes; |
2598 |
png_bytep dp = sp; |
|
2599 |
png_uint_32 i; |
|
2600 |
||
2601 |
for (i = 0; i < row_width; i++) |
|
2 | 2602 |
{ |
10576 | 2603 |
*(--dp) = (png_byte)(255 - *(--sp)); |
2604 |
||
2605 |
/* This does nothing: |
|
2606 |
*(--dp) = *(--sp); |
|
2607 |
*(--dp) = *(--sp); |
|
2608 |
*(--dp) = *(--sp); |
|
2609 |
We can replace it with: |
|
2 | 2610 |
*/ |
10576 | 2611 |
sp-=3; |
2612 |
dp=sp; |
|
2 | 2613 |
} |
2614 |
} |
|
10576 | 2615 |
|
2616 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2617 |
/* This inverts the alpha channel in RRGGBBAA */ |
|
2618 |
else |
|
2619 |
{ |
|
2620 |
png_bytep sp = row + row_info->rowbytes; |
|
2621 |
png_bytep dp = sp; |
|
2622 |
png_uint_32 i; |
|
2623 |
||
2624 |
for (i = 0; i < row_width; i++) |
|
2625 |
{ |
|
2626 |
*(--dp) = (png_byte)(255 - *(--sp)); |
|
2627 |
*(--dp) = (png_byte)(255 - *(--sp)); |
|
2628 |
||
2629 |
/* This does nothing: |
|
2630 |
*(--dp) = *(--sp); |
|
2631 |
*(--dp) = *(--sp); |
|
2632 |
*(--dp) = *(--sp); |
|
2633 |
*(--dp) = *(--sp); |
|
2634 |
*(--dp) = *(--sp); |
|
2635 |
*(--dp) = *(--sp); |
|
2636 |
We can replace it with: |
|
2637 |
*/ |
|
2638 |
sp-=6; |
|
2639 |
dp=sp; |
|
2640 |
} |
|
2641 |
} |
|
2642 |
#endif |
|
2643 |
} |
|
2644 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
|
2645 |
{ |
|
2646 |
if (row_info->bit_depth == 8) |
|
2 | 2647 |
{ |
2648 |
/* This inverts the alpha channel in GA */ |
|
10576 | 2649 |
png_bytep sp = row + row_info->rowbytes; |
2650 |
png_bytep dp = sp; |
|
2651 |
png_uint_32 i; |
|
2652 |
||
2653 |
for (i = 0; i < row_width; i++) |
|
2 | 2654 |
{ |
10576 | 2655 |
*(--dp) = (png_byte)(255 - *(--sp)); |
2656 |
*(--dp) = *(--sp); |
|
2 | 2657 |
} |
2658 |
} |
|
10576 | 2659 |
|
2660 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2661 |
else |
|
2662 |
{ |
|
2663 |
/* This inverts the alpha channel in GGAA */ |
|
2664 |
png_bytep sp = row + row_info->rowbytes; |
|
2665 |
png_bytep dp = sp; |
|
2666 |
png_uint_32 i; |
|
2667 |
||
2668 |
for (i = 0; i < row_width; i++) |
|
2669 |
{ |
|
2670 |
*(--dp) = (png_byte)(255 - *(--sp)); |
|
2671 |
*(--dp) = (png_byte)(255 - *(--sp)); |
|
2672 |
/* |
|
2673 |
*(--dp) = *(--sp); |
|
2674 |
*(--dp) = *(--sp); |
|
2675 |
*/ |
|
2676 |
sp-=2; |
|
2677 |
dp=sp; |
|
2678 |
} |
|
2679 |
} |
|
2680 |
#endif |
|
2 | 2681 |
} |
2682 |
} |
|
2683 |
#endif |
|
2684 |
||
10576 | 2685 |
#ifdef PNG_READ_FILLER_SUPPORTED |
2 | 2686 |
/* Add filler channel if we have RGB color */ |
29913 | 2687 |
static void |
2 | 2688 |
png_do_read_filler(png_row_infop row_info, png_bytep row, |
10576 | 2689 |
png_uint_32 filler, png_uint_32 flags) |
2 | 2690 |
{ |
2691 |
png_uint_32 i; |
|
2692 |
png_uint_32 row_width = row_info->width; |
|
2693 |
||
10576 | 2694 |
#ifdef PNG_READ_16BIT_SUPPORTED |
35296 | 2695 |
png_byte hi_filler = (png_byte)(filler>>8); |
10576 | 2696 |
#endif |
35296 | 2697 |
png_byte lo_filler = (png_byte)filler; |
2 | 2698 |
|
10576 | 2699 |
png_debug(1, "in png_do_read_filler"); |
2700 |
||
2 | 2701 |
if ( |
2702 |
row_info->color_type == PNG_COLOR_TYPE_GRAY) |
|
2703 |
{ |
|
10576 | 2704 |
if (row_info->bit_depth == 8) |
2 | 2705 |
{ |
29913 | 2706 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
2 | 2707 |
{ |
10576 | 2708 |
/* This changes the data from G to GX */ |
2 | 2709 |
png_bytep sp = row + (png_size_t)row_width; |
2710 |
png_bytep dp = sp + (png_size_t)row_width; |
|
2711 |
for (i = 1; i < row_width; i++) |
|
2712 |
{ |
|
2713 |
*(--dp) = lo_filler; |
|
2714 |
*(--dp) = *(--sp); |
|
2715 |
} |
|
2716 |
*(--dp) = lo_filler; |
|
2717 |
row_info->channels = 2; |
|
2718 |
row_info->pixel_depth = 16; |
|
2719 |
row_info->rowbytes = row_width * 2; |
|
2720 |
} |
|
10576 | 2721 |
|
2 | 2722 |
else |
2723 |
{ |
|
10576 | 2724 |
/* This changes the data from G to XG */ |
2 | 2725 |
png_bytep sp = row + (png_size_t)row_width; |
2726 |
png_bytep dp = sp + (png_size_t)row_width; |
|
2727 |
for (i = 0; i < row_width; i++) |
|
2728 |
{ |
|
2729 |
*(--dp) = *(--sp); |
|
2730 |
*(--dp) = lo_filler; |
|
2731 |
} |
|
2732 |
row_info->channels = 2; |
|
2733 |
row_info->pixel_depth = 16; |
|
2734 |
row_info->rowbytes = row_width * 2; |
|
2735 |
} |
|
2736 |
} |
|
10576 | 2737 |
|
2738 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2739 |
else if (row_info->bit_depth == 16) |
|
2 | 2740 |
{ |
29913 | 2741 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
2 | 2742 |
{ |
10576 | 2743 |
/* This changes the data from GG to GGXX */ |
2 | 2744 |
png_bytep sp = row + (png_size_t)row_width * 2; |
2745 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2746 |
for (i = 1; i < row_width; i++) |
|
2747 |
{ |
|
35296 | 2748 |
*(--dp) = lo_filler; |
2 | 2749 |
*(--dp) = hi_filler; |
2750 |
*(--dp) = *(--sp); |
|
2751 |
*(--dp) = *(--sp); |
|
2752 |
} |
|
35296 | 2753 |
*(--dp) = lo_filler; |
2 | 2754 |
*(--dp) = hi_filler; |
2755 |
row_info->channels = 2; |
|
2756 |
row_info->pixel_depth = 32; |
|
2757 |
row_info->rowbytes = row_width * 4; |
|
2758 |
} |
|
10576 | 2759 |
|
2 | 2760 |
else |
2761 |
{ |
|
10576 | 2762 |
/* This changes the data from GG to XXGG */ |
2 | 2763 |
png_bytep sp = row + (png_size_t)row_width * 2; |
2764 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2765 |
for (i = 0; i < row_width; i++) |
|
2766 |
{ |
|
2767 |
*(--dp) = *(--sp); |
|
2768 |
*(--dp) = *(--sp); |
|
35296 | 2769 |
*(--dp) = lo_filler; |
2 | 2770 |
*(--dp) = hi_filler; |
2771 |
} |
|
2772 |
row_info->channels = 2; |
|
2773 |
row_info->pixel_depth = 32; |
|
2774 |
row_info->rowbytes = row_width * 4; |
|
2775 |
} |
|
2776 |
} |
|
10576 | 2777 |
#endif |
2 | 2778 |
} /* COLOR_TYPE == GRAY */ |
2779 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB) |
|
2780 |
{ |
|
10576 | 2781 |
if (row_info->bit_depth == 8) |
2 | 2782 |
{ |
29913 | 2783 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
2 | 2784 |
{ |
10576 | 2785 |
/* This changes the data from RGB to RGBX */ |
2 | 2786 |
png_bytep sp = row + (png_size_t)row_width * 3; |
2787 |
png_bytep dp = sp + (png_size_t)row_width; |
|
2788 |
for (i = 1; i < row_width; i++) |
|
2789 |
{ |
|
2790 |
*(--dp) = lo_filler; |
|
2791 |
*(--dp) = *(--sp); |
|
2792 |
*(--dp) = *(--sp); |
|
2793 |
*(--dp) = *(--sp); |
|
2794 |
} |
|
2795 |
*(--dp) = lo_filler; |
|
2796 |
row_info->channels = 4; |
|
2797 |
row_info->pixel_depth = 32; |
|
2798 |
row_info->rowbytes = row_width * 4; |
|
2799 |
} |
|
10576 | 2800 |
|
2 | 2801 |
else |
2802 |
{ |
|
10576 | 2803 |
/* This changes the data from RGB to XRGB */ |
2 | 2804 |
png_bytep sp = row + (png_size_t)row_width * 3; |
2805 |
png_bytep dp = sp + (png_size_t)row_width; |
|
2806 |
for (i = 0; i < row_width; i++) |
|
2807 |
{ |
|
2808 |
*(--dp) = *(--sp); |
|
2809 |
*(--dp) = *(--sp); |
|
2810 |
*(--dp) = *(--sp); |
|
2811 |
*(--dp) = lo_filler; |
|
2812 |
} |
|
2813 |
row_info->channels = 4; |
|
2814 |
row_info->pixel_depth = 32; |
|
2815 |
row_info->rowbytes = row_width * 4; |
|
2816 |
} |
|
2817 |
} |
|
10576 | 2818 |
|
2819 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
2820 |
else if (row_info->bit_depth == 16) |
|
2 | 2821 |
{ |
29913 | 2822 |
if ((flags & PNG_FLAG_FILLER_AFTER) != 0) |
2 | 2823 |
{ |
10576 | 2824 |
/* This changes the data from RRGGBB to RRGGBBXX */ |
2 | 2825 |
png_bytep sp = row + (png_size_t)row_width * 6; |
2826 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2827 |
for (i = 1; i < row_width; i++) |
|
2828 |
{ |
|
35296 | 2829 |
*(--dp) = lo_filler; |
2 | 2830 |
*(--dp) = hi_filler; |
2831 |
*(--dp) = *(--sp); |
|
2832 |
*(--dp) = *(--sp); |
|
2833 |
*(--dp) = *(--sp); |
|
2834 |
*(--dp) = *(--sp); |
|
2835 |
*(--dp) = *(--sp); |
|
2836 |
*(--dp) = *(--sp); |
|
2837 |
} |
|
35296 | 2838 |
*(--dp) = lo_filler; |
2 | 2839 |
*(--dp) = hi_filler; |
2840 |
row_info->channels = 4; |
|
2841 |
row_info->pixel_depth = 64; |
|
2842 |
row_info->rowbytes = row_width * 8; |
|
2843 |
} |
|
10576 | 2844 |
|
2 | 2845 |
else |
2846 |
{ |
|
10576 | 2847 |
/* This changes the data from RRGGBB to XXRRGGBB */ |
2 | 2848 |
png_bytep sp = row + (png_size_t)row_width * 6; |
2849 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2850 |
for (i = 0; i < row_width; i++) |
|
2851 |
{ |
|
2852 |
*(--dp) = *(--sp); |
|
2853 |
*(--dp) = *(--sp); |
|
2854 |
*(--dp) = *(--sp); |
|
2855 |
*(--dp) = *(--sp); |
|
2856 |
*(--dp) = *(--sp); |
|
2857 |
*(--dp) = *(--sp); |
|
35296 | 2858 |
*(--dp) = lo_filler; |
2 | 2859 |
*(--dp) = hi_filler; |
2860 |
} |
|
10576 | 2861 |
|
2 | 2862 |
row_info->channels = 4; |
2863 |
row_info->pixel_depth = 64; |
|
2864 |
row_info->rowbytes = row_width * 8; |
|
2865 |
} |
|
2866 |
} |
|
10576 | 2867 |
#endif |
2 | 2868 |
} /* COLOR_TYPE == RGB */ |
2869 |
} |
|
2870 |
#endif |
|
2871 |
||
10576 | 2872 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
2873 |
/* Expand grayscale files to RGB, with or without alpha */ |
|
29913 | 2874 |
static void |
2 | 2875 |
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) |
2876 |
{ |
|
2877 |
png_uint_32 i; |
|
2878 |
png_uint_32 row_width = row_info->width; |
|
2879 |
||
10576 | 2880 |
png_debug(1, "in png_do_gray_to_rgb"); |
2881 |
||
2 | 2882 |
if (row_info->bit_depth >= 8 && |
29913 | 2883 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) == 0) |
2 | 2884 |
{ |
2885 |
if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
|
2886 |
{ |
|
2887 |
if (row_info->bit_depth == 8) |
|
2888 |
{ |
|
10576 | 2889 |
/* This changes G to RGB */ |
2 | 2890 |
png_bytep sp = row + (png_size_t)row_width - 1; |
2891 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2892 |
for (i = 0; i < row_width; i++) |
|
2893 |
{ |
|
2894 |
*(dp--) = *sp; |
|
2895 |
*(dp--) = *sp; |
|
2896 |
*(dp--) = *(sp--); |
|
2897 |
} |
|
2898 |
} |
|
10576 | 2899 |
|
2 | 2900 |
else |
2901 |
{ |
|
10576 | 2902 |
/* This changes GG to RRGGBB */ |
2 | 2903 |
png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
2904 |
png_bytep dp = sp + (png_size_t)row_width * 4; |
|
2905 |
for (i = 0; i < row_width; i++) |
|
2906 |
{ |
|
2907 |
*(dp--) = *sp; |
|
2908 |
*(dp--) = *(sp - 1); |
|
2909 |
*(dp--) = *sp; |
|
2910 |
*(dp--) = *(sp - 1); |
|
2911 |
*(dp--) = *(sp--); |
|
2912 |
*(dp--) = *(sp--); |
|
2913 |
} |
|
2914 |
} |
|
2915 |
} |
|
10576 | 2916 |
|
2 | 2917 |
else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
2918 |
{ |
|
2919 |
if (row_info->bit_depth == 8) |
|
2920 |
{ |
|
10576 | 2921 |
/* This changes GA to RGBA */ |
2 | 2922 |
png_bytep sp = row + (png_size_t)row_width * 2 - 1; |
2923 |
png_bytep dp = sp + (png_size_t)row_width * 2; |
|
2924 |
for (i = 0; i < row_width; i++) |
|
2925 |
{ |
|
2926 |
*(dp--) = *(sp--); |
|
2927 |
*(dp--) = *sp; |
|
2928 |
*(dp--) = *sp; |
|
2929 |
*(dp--) = *(sp--); |
|
2930 |
} |
|
2931 |
} |
|
10576 | 2932 |
|
2 | 2933 |
else |
2934 |
{ |
|
10576 | 2935 |
/* This changes GGAA to RRGGBBAA */ |
2 | 2936 |
png_bytep sp = row + (png_size_t)row_width * 4 - 1; |
2937 |
png_bytep dp = sp + (png_size_t)row_width * 4; |
|
2938 |
for (i = 0; i < row_width; i++) |
|
2939 |
{ |
|
2940 |
*(dp--) = *(sp--); |
|
2941 |
*(dp--) = *(sp--); |
|
2942 |
*(dp--) = *sp; |
|
2943 |
*(dp--) = *(sp - 1); |
|
2944 |
*(dp--) = *sp; |
|
2945 |
*(dp--) = *(sp - 1); |
|
2946 |
*(dp--) = *(sp--); |
|
2947 |
*(dp--) = *(sp--); |
|
2948 |
} |
|
2949 |
} |
|
2950 |
} |
|
29913 | 2951 |
row_info->channels = (png_byte)(row_info->channels + 2); |
2 | 2952 |
row_info->color_type |= PNG_COLOR_MASK_COLOR; |
2953 |
row_info->pixel_depth = (png_byte)(row_info->channels * |
|
10576 | 2954 |
row_info->bit_depth); |
2955 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
|
2 | 2956 |
} |
2957 |
} |
|
2958 |
#endif |
|
2959 |
||
10576 | 2960 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
2961 |
/* Reduce RGB files to grayscale, with or without alpha |
|
29913 | 2962 |
* using the equation given in Poynton's ColorFAQ of 1998-01-04 at |
2963 |
* <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but |
|
2964 |
* versions dated 1998 through November 2002 have been archived at |
|
2965 |
* http://web.archive.org/web/20000816232553/http://www.inforamp.net/ |
|
2966 |
* ~poynton/notes/colour_and_gamma/ColorFAQ.txt ) |
|
10576 | 2967 |
* Charles Poynton poynton at poynton.com |
2 | 2968 |
* |
2969 |
* Y = 0.212671 * R + 0.715160 * G + 0.072169 * B |
|
2970 |
* |
|
2971 |
* which can be expressed with integers as |
|
2972 |
* |
|
2973 |
* Y = (6969 * R + 23434 * G + 2365 * B)/32768 |
|
2974 |
* |
|
29913 | 2975 |
* Poynton's current link (as of January 2003 through July 2011): |
2976 |
* <http://www.poynton.com/notes/colour_and_gamma/> |
|
2977 |
* has changed the numbers slightly: |
|
2978 |
* |
|
2979 |
* Y = 0.2126*R + 0.7152*G + 0.0722*B |
|
2980 |
* |
|
2981 |
* which can be expressed with integers as |
|
2982 |
* |
|
2983 |
* Y = (6966 * R + 23436 * G + 2366 * B)/32768 |
|
2984 |
* |
|
2985 |
* Historically, however, libpng uses numbers derived from the ITU-R Rec 709 |
|
2986 |
* end point chromaticities and the D65 white point. Depending on the |
|
2987 |
* precision used for the D65 white point this produces a variety of different |
|
2988 |
* numbers, however if the four decimal place value used in ITU-R Rec 709 is |
|
2989 |
* used (0.3127,0.3290) the Y calculation would be: |
|
2990 |
* |
|
2991 |
* Y = (6968 * R + 23435 * G + 2366 * B)/32768 |
|
2 | 2992 |
* |
29913 | 2993 |
* While this is correct the rounding results in an overflow for white, because |
2994 |
* the sum of the rounded coefficients is 32769, not 32768. Consequently |
|
2995 |
* libpng uses, instead, the closest non-overflowing approximation: |
|
2996 |
* |
|
2997 |
* Y = (6968 * R + 23434 * G + 2366 * B)/32768 |
|
2998 |
* |
|
2999 |
* Starting with libpng-1.5.5, if the image being converted has a cHRM chunk |
|
3000 |
* (including an sRGB chunk) then the chromaticities are used to calculate the |
|
3001 |
* coefficients. See the chunk handling in pngrutil.c for more information. |
|
3002 |
* |
|
3003 |
* In all cases the calculation is to be done in a linear colorspace. If no |
|
3004 |
* gamma information is available to correct the encoding of the original RGB |
|
3005 |
* values this results in an implicit assumption that the original PNG RGB |
|
3006 |
* values were linear. |
|
3007 |
* |
|
3008 |
* Other integer coefficents can be used via png_set_rgb_to_gray(). Because |
|
3009 |
* the API takes just red and green coefficients the blue coefficient is |
|
3010 |
* calculated to make the sum 32768. This will result in different rounding |
|
3011 |
* to that used above. |
|
2 | 3012 |
*/ |
29913 | 3013 |
static int |
3014 |
png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) |
|
2 | 3015 |
|
3016 |
{ |
|
3017 |
int rgb_error = 0; |
|
3018 |
||
10576 | 3019 |
png_debug(1, "in png_do_rgb_to_gray"); |
3020 |
||
29913 | 3021 |
if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 && |
3022 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) |
|
2 | 3023 |
{ |
29913 | 3024 |
PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; |
3025 |
PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; |
|
3026 |
PNG_CONST png_uint_32 bc = 32768 - rc - gc; |
|
3027 |
PNG_CONST png_uint_32 row_width = row_info->width; |
|
3028 |
PNG_CONST int have_alpha = |
|
3029 |
(row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; |
|
3030 |
||
3031 |
if (row_info->bit_depth == 8) |
|
2 | 3032 |
{ |
29913 | 3033 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
3034 |
/* Notice that gamma to/from 1 are not necessarily inverses (if |
|
3035 |
* there is an overall gamma correction). Prior to 1.5.5 this code |
|
3036 |
* checked the linearized values for equality; this doesn't match |
|
3037 |
* the documentation, the original values must be checked. |
|
3038 |
*/ |
|
3039 |
if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) |
|
2 | 3040 |
{ |
29913 | 3041 |
png_bytep sp = row; |
3042 |
png_bytep dp = row; |
|
3043 |
png_uint_32 i; |
|
3044 |
||
3045 |
for (i = 0; i < row_width; i++) |
|
2 | 3046 |
{ |
29913 | 3047 |
png_byte red = *(sp++); |
3048 |
png_byte green = *(sp++); |
|
3049 |
png_byte blue = *(sp++); |
|
3050 |
||
3051 |
if (red != green || red != blue) |
|
3052 |
{ |
|
3053 |
red = png_ptr->gamma_to_1[red]; |
|
3054 |
green = png_ptr->gamma_to_1[green]; |
|
3055 |
blue = png_ptr->gamma_to_1[blue]; |
|
3056 |
||
3057 |
rgb_error |= 1; |
|
3058 |
*(dp++) = png_ptr->gamma_from_1[ |
|
3059 |
(rc*red + gc*green + bc*blue + 16384)>>15]; |
|
3060 |
} |
|
3061 |
||
3062 |
else |
|
2 | 3063 |
{ |
29913 | 3064 |
/* If there is no overall correction the table will not be |
3065 |
* set. |
|
3066 |
*/ |
|
3067 |
if (png_ptr->gamma_table != NULL) |
|
3068 |
red = png_ptr->gamma_table[red]; |
|
3069 |
||
3070 |
*(dp++) = red; |
|
3071 |
} |
|
3072 |
||
3073 |
if (have_alpha != 0) |
|
3074 |
*(dp++) = *(sp++); |
|
3075 |
} |
|
3076 |
} |
|
3077 |
else |
|
3078 |
#endif |
|
3079 |
{ |
|
3080 |
png_bytep sp = row; |
|
3081 |
png_bytep dp = row; |
|
3082 |
png_uint_32 i; |
|
3083 |
||
3084 |
for (i = 0; i < row_width; i++) |
|
3085 |
{ |
|
3086 |
png_byte red = *(sp++); |
|
3087 |
png_byte green = *(sp++); |
|
3088 |
png_byte blue = *(sp++); |
|
3089 |
||
3090 |
if (red != green || red != blue) |
|
3091 |
{ |
|
3092 |
rgb_error |= 1; |
|
3093 |
/* NOTE: this is the historical approach which simply |
|
3094 |
* truncates the results. |
|
3095 |
*/ |
|
3096 |
*(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); |
|
3097 |
} |
|
3098 |
||
3099 |
else |
|
3100 |
*(dp++) = red; |
|
3101 |
||
3102 |
if (have_alpha != 0) |
|
3103 |
*(dp++) = *(sp++); |
|
3104 |
} |
|
3105 |
} |
|
3106 |
} |
|
3107 |
||
3108 |
else /* RGB bit_depth == 16 */ |
|
3109 |
{ |
|
3110 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
3111 |
if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) |
|
3112 |
{ |
|
3113 |
png_bytep sp = row; |
|
3114 |
png_bytep dp = row; |
|
3115 |
png_uint_32 i; |
|
3116 |
||
3117 |
for (i = 0; i < row_width; i++) |
|
3118 |
{ |
|
3119 |
png_uint_16 red, green, blue, w; |
|
35296 | 3120 |
png_byte hi,lo; |
3121 |
||
3122 |
hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
|
3123 |
hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
|
3124 |
hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
|
29913 | 3125 |
|
3126 |
if (red == green && red == blue) |
|
3127 |
{ |
|
3128 |
if (png_ptr->gamma_16_table != NULL) |
|
3129 |
w = png_ptr->gamma_16_table[(red & 0xff) |
|
3130 |
>> png_ptr->gamma_shift][red >> 8]; |
|
10576 | 3131 |
|
2 | 3132 |
else |
29913 | 3133 |
w = red; |
2 | 3134 |
} |
29913 | 3135 |
|
3136 |
else |
|
2 | 3137 |
{ |
35296 | 3138 |
png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff) |
29913 | 3139 |
>> png_ptr->gamma_shift][red>>8]; |
3140 |
png_uint_16 green_1 = |
|
35296 | 3141 |
png_ptr->gamma_16_to_1[(green & 0xff) >> |
29913 | 3142 |
png_ptr->gamma_shift][green>>8]; |
35296 | 3143 |
png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff) |
29913 | 3144 |
>> png_ptr->gamma_shift][blue>>8]; |
3145 |
png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 |
|
3146 |
+ bc*blue_1 + 16384)>>15); |
|
35296 | 3147 |
w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >> |
29913 | 3148 |
png_ptr->gamma_shift][gray16 >> 8]; |
3149 |
rgb_error |= 1; |
|
3150 |
} |
|
3151 |
||
3152 |
*(dp++) = (png_byte)((w>>8) & 0xff); |
|
3153 |
*(dp++) = (png_byte)(w & 0xff); |
|
3154 |
||
3155 |
if (have_alpha != 0) |
|
3156 |
{ |
|
3157 |
*(dp++) = *(sp++); |
|
3158 |
*(dp++) = *(sp++); |
|
2 | 3159 |
} |
3160 |
} |
|
3161 |
} |
|
29913 | 3162 |
else |
3163 |
#endif |
|
2 | 3164 |
{ |
29913 | 3165 |
png_bytep sp = row; |
3166 |
png_bytep dp = row; |
|
3167 |
png_uint_32 i; |
|
3168 |
||
3169 |
for (i = 0; i < row_width; i++) |
|
2 | 3170 |
{ |
29913 | 3171 |
png_uint_16 red, green, blue, gray16; |
35296 | 3172 |
png_byte hi,lo; |
3173 |
||
3174 |
hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); |
|
3175 |
hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); |
|
3176 |
hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); |
|
29913 | 3177 |
|
3178 |
if (red != green || red != blue) |
|
3179 |
rgb_error |= 1; |
|
3180 |
||
35296 | 3181 |
/* From 1.5.5 in the 16-bit case do the accurate conversion even |
29913 | 3182 |
* in the 'fast' case - this is because this is where the code |
35296 | 3183 |
* ends up when handling linear 16-bit data. |
29913 | 3184 |
*/ |
3185 |
gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> |
|
3186 |
15); |
|
3187 |
*(dp++) = (png_byte)((gray16 >> 8) & 0xff); |
|
3188 |
*(dp++) = (png_byte)(gray16 & 0xff); |
|
3189 |
||
3190 |
if (have_alpha != 0) |
|
2 | 3191 |
{ |
3192 |
*(dp++) = *(sp++); |
|
3193 |
*(dp++) = *(sp++); |
|
3194 |
} |
|
3195 |
} |
|
3196 |
} |
|
3197 |
} |
|
29913 | 3198 |
|
3199 |
row_info->channels = (png_byte)(row_info->channels - 2); |
|
10576 | 3200 |
row_info->color_type = (png_byte)(row_info->color_type & |
3201 |
~PNG_COLOR_MASK_COLOR); |
|
2 | 3202 |
row_info->pixel_depth = (png_byte)(row_info->channels * |
10576 | 3203 |
row_info->bit_depth); |
3204 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
|
2 | 3205 |
} |
3206 |
return rgb_error; |
|
3207 |
} |
|
3208 |
#endif |
|
29913 | 3209 |
|
3210 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
|
3211 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
|
2 | 3212 |
/* Replace any alpha or transparency with the supplied background color. |
3213 |
* "background" is already in the screen gamma, while "background_1" is |
|
3214 |
* at a gamma of 1.0. Paletted files have already been taken care of. |
|
3215 |
*/ |
|
29913 | 3216 |
static void |
3217 |
png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
|
2 | 3218 |
{ |
10576 | 3219 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
3220 |
png_const_bytep gamma_table = png_ptr->gamma_table; |
|
3221 |
png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; |
|
3222 |
png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; |
|
3223 |
png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; |
|
3224 |
png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; |
|
3225 |
png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; |
|
3226 |
int gamma_shift = png_ptr->gamma_shift; |
|
29913 | 3227 |
int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; |
10576 | 3228 |
#endif |
3229 |
||
3230 |
png_bytep sp; |
|
2 | 3231 |
png_uint_32 i; |
10576 | 3232 |
png_uint_32 row_width = row_info->width; |
2 | 3233 |
int shift; |
3234 |
||
10576 | 3235 |
png_debug(1, "in png_do_compose"); |
3236 |
||
2 | 3237 |
{ |
3238 |
switch (row_info->color_type) |
|
3239 |
{ |
|
3240 |
case PNG_COLOR_TYPE_GRAY: |
|
3241 |
{ |
|
3242 |
switch (row_info->bit_depth) |
|
3243 |
{ |
|
3244 |
case 1: |
|
3245 |
{ |
|
3246 |
sp = row; |
|
3247 |
shift = 7; |
|
3248 |
for (i = 0; i < row_width; i++) |
|
3249 |
{ |
|
3250 |
if ((png_uint_16)((*sp >> shift) & 0x01) |
|
10576 | 3251 |
== png_ptr->trans_color.gray) |
2 | 3252 |
{ |
29913 | 3253 |
unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); |
3254 |
tmp |= png_ptr->background.gray << shift; |
|
3255 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3256 |
} |
10576 | 3257 |
|
29913 | 3258 |
if (shift == 0) |
2 | 3259 |
{ |
3260 |
shift = 7; |
|
3261 |
sp++; |
|
3262 |
} |
|
10576 | 3263 |
|
2 | 3264 |
else |
3265 |
shift--; |
|
3266 |
} |
|
3267 |
break; |
|
3268 |
} |
|
10576 | 3269 |
|
2 | 3270 |
case 2: |
3271 |
{ |
|
10576 | 3272 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3273 |
if (gamma_table != NULL) |
3274 |
{ |
|
3275 |
sp = row; |
|
3276 |
shift = 6; |
|
3277 |
for (i = 0; i < row_width; i++) |
|
3278 |
{ |
|
3279 |
if ((png_uint_16)((*sp >> shift) & 0x03) |
|
10576 | 3280 |
== png_ptr->trans_color.gray) |
2 | 3281 |
{ |
29913 | 3282 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
3283 |
tmp |= png_ptr->background.gray << shift; |
|
3284 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3285 |
} |
10576 | 3286 |
|
2 | 3287 |
else |
3288 |
{ |
|
29913 | 3289 |
unsigned int p = (*sp >> shift) & 0x03; |
3290 |
unsigned int g = (gamma_table [p | (p << 2) | |
|
3291 |
(p << 4) | (p << 6)] >> 6) & 0x03; |
|
3292 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
|
3293 |
tmp |= g << shift; |
|
3294 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3295 |
} |
10576 | 3296 |
|
29913 | 3297 |
if (shift == 0) |
2 | 3298 |
{ |
3299 |
shift = 6; |
|
3300 |
sp++; |
|
3301 |
} |
|
10576 | 3302 |
|
2 | 3303 |
else |
3304 |
shift -= 2; |
|
3305 |
} |
|
3306 |
} |
|
10576 | 3307 |
|
2 | 3308 |
else |
3309 |
#endif |
|
3310 |
{ |
|
3311 |
sp = row; |
|
3312 |
shift = 6; |
|
3313 |
for (i = 0; i < row_width; i++) |
|
3314 |
{ |
|
3315 |
if ((png_uint_16)((*sp >> shift) & 0x03) |
|
10576 | 3316 |
== png_ptr->trans_color.gray) |
2 | 3317 |
{ |
29913 | 3318 |
unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); |
3319 |
tmp |= png_ptr->background.gray << shift; |
|
3320 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3321 |
} |
10576 | 3322 |
|
29913 | 3323 |
if (shift == 0) |
2 | 3324 |
{ |
3325 |
shift = 6; |
|
3326 |
sp++; |
|
3327 |
} |
|
10576 | 3328 |
|
2 | 3329 |
else |
3330 |
shift -= 2; |
|
3331 |
} |
|
3332 |
} |
|
3333 |
break; |
|
3334 |
} |
|
10576 | 3335 |
|
2 | 3336 |
case 4: |
3337 |
{ |
|
10576 | 3338 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3339 |
if (gamma_table != NULL) |
3340 |
{ |
|
3341 |
sp = row; |
|
3342 |
shift = 4; |
|
3343 |
for (i = 0; i < row_width; i++) |
|
3344 |
{ |
|
3345 |
if ((png_uint_16)((*sp >> shift) & 0x0f) |
|
10576 | 3346 |
== png_ptr->trans_color.gray) |
2 | 3347 |
{ |
35296 | 3348 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
29913 | 3349 |
tmp |= png_ptr->background.gray << shift; |
3350 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3351 |
} |
10576 | 3352 |
|
2 | 3353 |
else |
3354 |
{ |
|
29913 | 3355 |
unsigned int p = (*sp >> shift) & 0x0f; |
3356 |
unsigned int g = (gamma_table[p | (p << 4)] >> 4) & |
|
3357 |
0x0f; |
|
35296 | 3358 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
29913 | 3359 |
tmp |= g << shift; |
3360 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3361 |
} |
10576 | 3362 |
|
29913 | 3363 |
if (shift == 0) |
2 | 3364 |
{ |
3365 |
shift = 4; |
|
3366 |
sp++; |
|
3367 |
} |
|
10576 | 3368 |
|
2 | 3369 |
else |
3370 |
shift -= 4; |
|
3371 |
} |
|
3372 |
} |
|
10576 | 3373 |
|
2 | 3374 |
else |
3375 |
#endif |
|
3376 |
{ |
|
3377 |
sp = row; |
|
3378 |
shift = 4; |
|
3379 |
for (i = 0; i < row_width; i++) |
|
3380 |
{ |
|
3381 |
if ((png_uint_16)((*sp >> shift) & 0x0f) |
|
10576 | 3382 |
== png_ptr->trans_color.gray) |
2 | 3383 |
{ |
35296 | 3384 |
unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); |
29913 | 3385 |
tmp |= png_ptr->background.gray << shift; |
3386 |
*sp = (png_byte)(tmp & 0xff); |
|
2 | 3387 |
} |
10576 | 3388 |
|
29913 | 3389 |
if (shift == 0) |
2 | 3390 |
{ |
3391 |
shift = 4; |
|
3392 |
sp++; |
|
3393 |
} |
|
10576 | 3394 |
|
2 | 3395 |
else |
3396 |
shift -= 4; |
|
3397 |
} |
|
3398 |
} |
|
3399 |
break; |
|
3400 |
} |
|
10576 | 3401 |
|
2 | 3402 |
case 8: |
3403 |
{ |
|
10576 | 3404 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3405 |
if (gamma_table != NULL) |
3406 |
{ |
|
3407 |
sp = row; |
|
3408 |
for (i = 0; i < row_width; i++, sp++) |
|
3409 |
{ |
|
10576 | 3410 |
if (*sp == png_ptr->trans_color.gray) |
3411 |
*sp = (png_byte)png_ptr->background.gray; |
|
3412 |
||
2 | 3413 |
else |
3414 |
*sp = gamma_table[*sp]; |
|
3415 |
} |
|
3416 |
} |
|
3417 |
else |
|
3418 |
#endif |
|
3419 |
{ |
|
3420 |
sp = row; |
|
3421 |
for (i = 0; i < row_width; i++, sp++) |
|
3422 |
{ |
|
10576 | 3423 |
if (*sp == png_ptr->trans_color.gray) |
3424 |
*sp = (png_byte)png_ptr->background.gray; |
|
2 | 3425 |
} |
3426 |
} |
|
3427 |
break; |
|
3428 |
} |
|
10576 | 3429 |
|
2 | 3430 |
case 16: |
3431 |
{ |
|
10576 | 3432 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3433 |
if (gamma_16 != NULL) |
3434 |
{ |
|
3435 |
sp = row; |
|
3436 |
for (i = 0; i < row_width; i++, sp += 2) |
|
3437 |
{ |
|
3438 |
png_uint_16 v; |
|
3439 |
||
3440 |
v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
10576 | 3441 |
|
3442 |
if (v == png_ptr->trans_color.gray) |
|
2 | 3443 |
{ |
10576 | 3444 |
/* Background is already in screen gamma */ |
29913 | 3445 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
3446 |
& 0xff); |
|
3447 |
*(sp + 1) = (png_byte)(png_ptr->background.gray |
|
3448 |
& 0xff); |
|
2 | 3449 |
} |
10576 | 3450 |
|
2 | 3451 |
else |
3452 |
{ |
|
3453 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
|
3454 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
3455 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
3456 |
} |
|
3457 |
} |
|
3458 |
} |
|
3459 |
else |
|
3460 |
#endif |
|
3461 |
{ |
|
3462 |
sp = row; |
|
3463 |
for (i = 0; i < row_width; i++, sp += 2) |
|
3464 |
{ |
|
3465 |
png_uint_16 v; |
|
3466 |
||
3467 |
v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
10576 | 3468 |
|
3469 |
if (v == png_ptr->trans_color.gray) |
|
2 | 3470 |
{ |
29913 | 3471 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
3472 |
& 0xff); |
|
3473 |
*(sp + 1) = (png_byte)(png_ptr->background.gray |
|
3474 |
& 0xff); |
|
2 | 3475 |
} |
3476 |
} |
|
3477 |
} |
|
3478 |
break; |
|
3479 |
} |
|
10576 | 3480 |
|
3481 |
default: |
|
3482 |
break; |
|
2 | 3483 |
} |
3484 |
break; |
|
3485 |
} |
|
10576 | 3486 |
|
2 | 3487 |
case PNG_COLOR_TYPE_RGB: |
3488 |
{ |
|
3489 |
if (row_info->bit_depth == 8) |
|
3490 |
{ |
|
10576 | 3491 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3492 |
if (gamma_table != NULL) |
3493 |
{ |
|
3494 |
sp = row; |
|
3495 |
for (i = 0; i < row_width; i++, sp += 3) |
|
3496 |
{ |
|
10576 | 3497 |
if (*sp == png_ptr->trans_color.red && |
3498 |
*(sp + 1) == png_ptr->trans_color.green && |
|
3499 |
*(sp + 2) == png_ptr->trans_color.blue) |
|
2 | 3500 |
{ |
10576 | 3501 |
*sp = (png_byte)png_ptr->background.red; |
3502 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
|
3503 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
|
2 | 3504 |
} |
10576 | 3505 |
|
2 | 3506 |
else |
3507 |
{ |
|
3508 |
*sp = gamma_table[*sp]; |
|
3509 |
*(sp + 1) = gamma_table[*(sp + 1)]; |
|
3510 |
*(sp + 2) = gamma_table[*(sp + 2)]; |
|
3511 |
} |
|
3512 |
} |
|
3513 |
} |
|
3514 |
else |
|
3515 |
#endif |
|
3516 |
{ |
|
3517 |
sp = row; |
|
3518 |
for (i = 0; i < row_width; i++, sp += 3) |
|
3519 |
{ |
|
10576 | 3520 |
if (*sp == png_ptr->trans_color.red && |
3521 |
*(sp + 1) == png_ptr->trans_color.green && |
|
3522 |
*(sp + 2) == png_ptr->trans_color.blue) |
|
2 | 3523 |
{ |
10576 | 3524 |
*sp = (png_byte)png_ptr->background.red; |
3525 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
|
3526 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
|
2 | 3527 |
} |
3528 |
} |
|
3529 |
} |
|
3530 |
} |
|
3531 |
else /* if (row_info->bit_depth == 16) */ |
|
3532 |
{ |
|
10576 | 3533 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3534 |
if (gamma_16 != NULL) |
3535 |
{ |
|
3536 |
sp = row; |
|
3537 |
for (i = 0; i < row_width; i++, sp += 6) |
|
3538 |
{ |
|
3539 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
10576 | 3540 |
|
3541 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
|
3542 |
+ *(sp + 3)); |
|
3543 |
||
3544 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
|
3545 |
+ *(sp + 5)); |
|
3546 |
||
3547 |
if (r == png_ptr->trans_color.red && |
|
3548 |
g == png_ptr->trans_color.green && |
|
3549 |
b == png_ptr->trans_color.blue) |
|
2 | 3550 |
{ |
10576 | 3551 |
/* Background is already in screen gamma */ |
3552 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
|
3553 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
|
29913 | 3554 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
3555 |
& 0xff); |
|
3556 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
|
3557 |
& 0xff); |
|
3558 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
|
3559 |
& 0xff); |
|
10576 | 3560 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
2 | 3561 |
} |
10576 | 3562 |
|
2 | 3563 |
else |
3564 |
{ |
|
3565 |
png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
|
3566 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
3567 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
10576 | 3568 |
|
2 | 3569 |
v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
3570 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
|
3571 |
*(sp + 3) = (png_byte)(v & 0xff); |
|
10576 | 3572 |
|
2 | 3573 |
v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
3574 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
|
3575 |
*(sp + 5) = (png_byte)(v & 0xff); |
|
3576 |
} |
|
3577 |
} |
|
3578 |
} |
|
10576 | 3579 |
|
3580 |
else |
|
3581 |
#endif |
|
3582 |
{ |
|
3583 |
sp = row; |
|
3584 |
for (i = 0; i < row_width; i++, sp += 6) |
|
3585 |
{ |
|
3586 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
3587 |
||
3588 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
|
3589 |
+ *(sp + 3)); |
|
3590 |
||
3591 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
|
3592 |
+ *(sp + 5)); |
|
3593 |
||
3594 |
if (r == png_ptr->trans_color.red && |
|
3595 |
g == png_ptr->trans_color.green && |
|
3596 |
b == png_ptr->trans_color.blue) |
|
3597 |
{ |
|
3598 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
|
3599 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
|
29913 | 3600 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
3601 |
& 0xff); |
|
3602 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
|
3603 |
& 0xff); |
|
3604 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
|
3605 |
& 0xff); |
|
10576 | 3606 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
3607 |
} |
|
3608 |
} |
|
3609 |
} |
|
3610 |
} |
|
3611 |
break; |
|
3612 |
} |
|
3613 |
||
3614 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
|
3615 |
{ |
|
3616 |
if (row_info->bit_depth == 8) |
|
3617 |
{ |
|
3618 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
3619 |
if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
|
3620 |
gamma_table != NULL) |
|
3621 |
{ |
|
3622 |
sp = row; |
|
3623 |
for (i = 0; i < row_width; i++, sp += 2) |
|
3624 |
{ |
|
3625 |
png_uint_16 a = *(sp + 1); |
|
3626 |
||
3627 |
if (a == 0xff) |
|
3628 |
*sp = gamma_table[*sp]; |
|
3629 |
||
3630 |
else if (a == 0) |
|
3631 |
{ |
|
3632 |
/* Background is already in screen gamma */ |
|
3633 |
*sp = (png_byte)png_ptr->background.gray; |
|
3634 |
} |
|
3635 |
||
3636 |
else |
|
3637 |
{ |
|
3638 |
png_byte v, w; |
|
3639 |
||
3640 |
v = gamma_to_1[*sp]; |
|
3641 |
png_composite(w, v, a, png_ptr->background_1.gray); |
|
29913 | 3642 |
if (optimize == 0) |
10576 | 3643 |
w = gamma_from_1[w]; |
3644 |
*sp = w; |
|
3645 |
} |
|
3646 |
} |
|
3647 |
} |
|
2 | 3648 |
else |
3649 |
#endif |
|
3650 |
{ |
|
3651 |
sp = row; |
|
10576 | 3652 |
for (i = 0; i < row_width; i++, sp += 2) |
2 | 3653 |
{ |
10576 | 3654 |
png_byte a = *(sp + 1); |
3655 |
||
3656 |
if (a == 0) |
|
3657 |
*sp = (png_byte)png_ptr->background.gray; |
|
3658 |
||
3659 |
else if (a < 0xff) |
|
29913 | 3660 |
png_composite(*sp, *sp, a, png_ptr->background.gray); |
2 | 3661 |
} |
3662 |
} |
|
3663 |
} |
|
10576 | 3664 |
else /* if (png_ptr->bit_depth == 16) */ |
2 | 3665 |
{ |
10576 | 3666 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
3667 |
if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
|
3668 |
gamma_16_to_1 != NULL) |
|
2 | 3669 |
{ |
3670 |
sp = row; |
|
10576 | 3671 |
for (i = 0; i < row_width; i++, sp += 4) |
2 | 3672 |
{ |
10576 | 3673 |
png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
3674 |
+ *(sp + 3)); |
|
3675 |
||
3676 |
if (a == (png_uint_16)0xffff) |
|
2 | 3677 |
{ |
10576 | 3678 |
png_uint_16 v; |
3679 |
||
3680 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
|
3681 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
3682 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
2 | 3683 |
} |
10576 | 3684 |
|
2 | 3685 |
else if (a == 0) |
3686 |
{ |
|
10576 | 3687 |
/* Background is already in screen gamma */ |
29913 | 3688 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
3689 |
& 0xff); |
|
10576 | 3690 |
*(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
2 | 3691 |
} |
10576 | 3692 |
|
2 | 3693 |
else |
3694 |
{ |
|
10576 | 3695 |
png_uint_16 g, v, w; |
3696 |
||
3697 |
g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
|
3698 |
png_composite_16(v, g, a, png_ptr->background_1.gray); |
|
29913 | 3699 |
if (optimize != 0) |
10576 | 3700 |
w = v; |
3701 |
else |
|
35296 | 3702 |
w = gamma_16_from_1[(v & 0xff) >> |
3703 |
gamma_shift][v >> 8]; |
|
10576 | 3704 |
*sp = (png_byte)((w >> 8) & 0xff); |
3705 |
*(sp + 1) = (png_byte)(w & 0xff); |
|
2 | 3706 |
} |
3707 |
} |
|
3708 |
} |
|
3709 |
else |
|
3710 |
#endif |
|
3711 |
{ |
|
3712 |
sp = row; |
|
10576 | 3713 |
for (i = 0; i < row_width; i++, sp += 4) |
2 | 3714 |
{ |
10576 | 3715 |
png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) |
3716 |
+ *(sp + 3)); |
|
3717 |
||
3718 |
if (a == 0) |
|
2 | 3719 |
{ |
29913 | 3720 |
*sp = (png_byte)((png_ptr->background.gray >> 8) |
3721 |
& 0xff); |
|
10576 | 3722 |
*(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); |
2 | 3723 |
} |
10576 | 3724 |
|
3725 |
else if (a < 0xffff) |
|
2 | 3726 |
{ |
3727 |
png_uint_16 g, v; |
|
3728 |
||
3729 |
g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
29913 | 3730 |
png_composite_16(v, g, a, png_ptr->background.gray); |
10576 | 3731 |
*sp = (png_byte)((v >> 8) & 0xff); |
3732 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
2 | 3733 |
} |
3734 |
} |
|
3735 |
} |
|
3736 |
} |
|
3737 |
break; |
|
3738 |
} |
|
10576 | 3739 |
|
2 | 3740 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
3741 |
{ |
|
3742 |
if (row_info->bit_depth == 8) |
|
3743 |
{ |
|
10576 | 3744 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3745 |
if (gamma_to_1 != NULL && gamma_from_1 != NULL && |
3746 |
gamma_table != NULL) |
|
3747 |
{ |
|
3748 |
sp = row; |
|
10576 | 3749 |
for (i = 0; i < row_width; i++, sp += 4) |
2 | 3750 |
{ |
3751 |
png_byte a = *(sp + 3); |
|
3752 |
||
3753 |
if (a == 0xff) |
|
3754 |
{ |
|
10576 | 3755 |
*sp = gamma_table[*sp]; |
3756 |
*(sp + 1) = gamma_table[*(sp + 1)]; |
|
3757 |
*(sp + 2) = gamma_table[*(sp + 2)]; |
|
2 | 3758 |
} |
10576 | 3759 |
|
2 | 3760 |
else if (a == 0) |
3761 |
{ |
|
10576 | 3762 |
/* Background is already in screen gamma */ |
3763 |
*sp = (png_byte)png_ptr->background.red; |
|
3764 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
|
3765 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
|
2 | 3766 |
} |
10576 | 3767 |
|
2 | 3768 |
else |
3769 |
{ |
|
3770 |
png_byte v, w; |
|
3771 |
||
3772 |
v = gamma_to_1[*sp]; |
|
10576 | 3773 |
png_composite(w, v, a, png_ptr->background_1.red); |
29913 | 3774 |
if (optimize == 0) w = gamma_from_1[w]; |
10576 | 3775 |
*sp = w; |
3776 |
||
2 | 3777 |
v = gamma_to_1[*(sp + 1)]; |
10576 | 3778 |
png_composite(w, v, a, png_ptr->background_1.green); |
29913 | 3779 |
if (optimize == 0) w = gamma_from_1[w]; |
10576 | 3780 |
*(sp + 1) = w; |
3781 |
||
2 | 3782 |
v = gamma_to_1[*(sp + 2)]; |
10576 | 3783 |
png_composite(w, v, a, png_ptr->background_1.blue); |
29913 | 3784 |
if (optimize == 0) w = gamma_from_1[w]; |
10576 | 3785 |
*(sp + 2) = w; |
2 | 3786 |
} |
3787 |
} |
|
3788 |
} |
|
3789 |
else |
|
3790 |
#endif |
|
3791 |
{ |
|
3792 |
sp = row; |
|
10576 | 3793 |
for (i = 0; i < row_width; i++, sp += 4) |
2 | 3794 |
{ |
3795 |
png_byte a = *(sp + 3); |
|
3796 |
||
10576 | 3797 |
if (a == 0) |
2 | 3798 |
{ |
10576 | 3799 |
*sp = (png_byte)png_ptr->background.red; |
3800 |
*(sp + 1) = (png_byte)png_ptr->background.green; |
|
3801 |
*(sp + 2) = (png_byte)png_ptr->background.blue; |
|
2 | 3802 |
} |
10576 | 3803 |
|
3804 |
else if (a < 0xff) |
|
2 | 3805 |
{ |
10576 | 3806 |
png_composite(*sp, *sp, a, png_ptr->background.red); |
3807 |
||
3808 |
png_composite(*(sp + 1), *(sp + 1), a, |
|
3809 |
png_ptr->background.green); |
|
3810 |
||
3811 |
png_composite(*(sp + 2), *(sp + 2), a, |
|
3812 |
png_ptr->background.blue); |
|
2 | 3813 |
} |
3814 |
} |
|
3815 |
} |
|
3816 |
} |
|
3817 |
else /* if (row_info->bit_depth == 16) */ |
|
3818 |
{ |
|
10576 | 3819 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3820 |
if (gamma_16 != NULL && gamma_16_from_1 != NULL && |
3821 |
gamma_16_to_1 != NULL) |
|
3822 |
{ |
|
3823 |
sp = row; |
|
10576 | 3824 |
for (i = 0; i < row_width; i++, sp += 8) |
2 | 3825 |
{ |
3826 |
png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
|
3827 |
<< 8) + (png_uint_16)(*(sp + 7))); |
|
10576 | 3828 |
|
2 | 3829 |
if (a == (png_uint_16)0xffff) |
3830 |
{ |
|
3831 |
png_uint_16 v; |
|
3832 |
||
3833 |
v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; |
|
10576 | 3834 |
*sp = (png_byte)((v >> 8) & 0xff); |
3835 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
3836 |
||
2 | 3837 |
v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
10576 | 3838 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
3839 |
*(sp + 3) = (png_byte)(v & 0xff); |
|
3840 |
||
2 | 3841 |
v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
10576 | 3842 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
3843 |
*(sp + 5) = (png_byte)(v & 0xff); |
|
2 | 3844 |
} |
10576 | 3845 |
|
2 | 3846 |
else if (a == 0) |
3847 |
{ |
|
10576 | 3848 |
/* Background is already in screen gamma */ |
3849 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
|
3850 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
|
29913 | 3851 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
3852 |
& 0xff); |
|
3853 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
|
3854 |
& 0xff); |
|
3855 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
|
3856 |
& 0xff); |
|
10576 | 3857 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
2 | 3858 |
} |
10576 | 3859 |
|
2 | 3860 |
else |
3861 |
{ |
|
10576 | 3862 |
png_uint_16 v, w; |
2 | 3863 |
|
3864 |
v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; |
|
10576 | 3865 |
png_composite_16(w, v, a, png_ptr->background_1.red); |
29913 | 3866 |
if (optimize == 0) |
35296 | 3867 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
29913 | 3868 |
8]; |
10576 | 3869 |
*sp = (png_byte)((w >> 8) & 0xff); |
3870 |
*(sp + 1) = (png_byte)(w & 0xff); |
|
3871 |
||
2 | 3872 |
v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; |
10576 | 3873 |
png_composite_16(w, v, a, png_ptr->background_1.green); |
29913 | 3874 |
if (optimize == 0) |
35296 | 3875 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
29913 | 3876 |
8]; |
10576 | 3877 |
|
3878 |
*(sp + 2) = (png_byte)((w >> 8) & 0xff); |
|
3879 |
*(sp + 3) = (png_byte)(w & 0xff); |
|
3880 |
||
2 | 3881 |
v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; |
10576 | 3882 |
png_composite_16(w, v, a, png_ptr->background_1.blue); |
29913 | 3883 |
if (optimize == 0) |
35296 | 3884 |
w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> |
29913 | 3885 |
8]; |
10576 | 3886 |
|
3887 |
*(sp + 4) = (png_byte)((w >> 8) & 0xff); |
|
3888 |
*(sp + 5) = (png_byte)(w & 0xff); |
|
2 | 3889 |
} |
3890 |
} |
|
3891 |
} |
|
10576 | 3892 |
|
2 | 3893 |
else |
3894 |
#endif |
|
3895 |
{ |
|
3896 |
sp = row; |
|
10576 | 3897 |
for (i = 0; i < row_width; i++, sp += 8) |
2 | 3898 |
{ |
3899 |
png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) |
|
10576 | 3900 |
<< 8) + (png_uint_16)(*(sp + 7))); |
3901 |
||
3902 |
if (a == 0) |
|
2 | 3903 |
{ |
10576 | 3904 |
*sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); |
3905 |
*(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); |
|
29913 | 3906 |
*(sp + 2) = (png_byte)((png_ptr->background.green >> 8) |
3907 |
& 0xff); |
|
3908 |
*(sp + 3) = (png_byte)(png_ptr->background.green |
|
3909 |
& 0xff); |
|
3910 |
*(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) |
|
3911 |
& 0xff); |
|
10576 | 3912 |
*(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); |
2 | 3913 |
} |
10576 | 3914 |
|
3915 |
else if (a < 0xffff) |
|
2 | 3916 |
{ |
3917 |
png_uint_16 v; |
|
3918 |
||
3919 |
png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); |
|
3920 |
png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) |
|
3921 |
+ *(sp + 3)); |
|
3922 |
png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) |
|
3923 |
+ *(sp + 5)); |
|
3924 |
||
10576 | 3925 |
png_composite_16(v, r, a, png_ptr->background.red); |
3926 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
3927 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
3928 |
||
3929 |
png_composite_16(v, g, a, png_ptr->background.green); |
|
3930 |
*(sp + 2) = (png_byte)((v >> 8) & 0xff); |
|
3931 |
*(sp + 3) = (png_byte)(v & 0xff); |
|
3932 |
||
3933 |
png_composite_16(v, b, a, png_ptr->background.blue); |
|
3934 |
*(sp + 4) = (png_byte)((v >> 8) & 0xff); |
|
3935 |
*(sp + 5) = (png_byte)(v & 0xff); |
|
2 | 3936 |
} |
3937 |
} |
|
3938 |
} |
|
3939 |
} |
|
3940 |
break; |
|
3941 |
} |
|
10576 | 3942 |
|
3943 |
default: |
|
3944 |
break; |
|
2 | 3945 |
} |
3946 |
} |
|
3947 |
} |
|
29913 | 3948 |
#endif /* READ_BACKGROUND || READ_ALPHA_MODE */ |
2 | 3949 |
|
10576 | 3950 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
2 | 3951 |
/* Gamma correct the image, avoiding the alpha channel. Make sure |
3952 |
* you do this after you deal with the transparency issue on grayscale |
|
3953 |
* or RGB images. If your bit depth is 8, use gamma_table, if it |
|
3954 |
* is 16, use gamma_16_table and gamma_shift. Build these with |
|
3955 |
* build_gamma_table(). |
|
3956 |
*/ |
|
29913 | 3957 |
static void |
3958 |
png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
|
2 | 3959 |
{ |
10576 | 3960 |
png_const_bytep gamma_table = png_ptr->gamma_table; |
3961 |
png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; |
|
3962 |
int gamma_shift = png_ptr->gamma_shift; |
|
3963 |
||
2 | 3964 |
png_bytep sp; |
3965 |
png_uint_32 i; |
|
3966 |
png_uint_32 row_width=row_info->width; |
|
3967 |
||
10576 | 3968 |
png_debug(1, "in png_do_gamma"); |
3969 |
||
3970 |
if (((row_info->bit_depth <= 8 && gamma_table != NULL) || |
|
3971 |
(row_info->bit_depth == 16 && gamma_16_table != NULL))) |
|
2 | 3972 |
{ |
3973 |
switch (row_info->color_type) |
|
3974 |
{ |
|
3975 |
case PNG_COLOR_TYPE_RGB: |
|
3976 |
{ |
|
3977 |
if (row_info->bit_depth == 8) |
|
3978 |
{ |
|
3979 |
sp = row; |
|
3980 |
for (i = 0; i < row_width; i++) |
|
3981 |
{ |
|
3982 |
*sp = gamma_table[*sp]; |
|
3983 |
sp++; |
|
3984 |
*sp = gamma_table[*sp]; |
|
3985 |
sp++; |
|
3986 |
*sp = gamma_table[*sp]; |
|
3987 |
sp++; |
|
3988 |
} |
|
3989 |
} |
|
10576 | 3990 |
|
2 | 3991 |
else /* if (row_info->bit_depth == 16) */ |
3992 |
{ |
|
3993 |
sp = row; |
|
3994 |
for (i = 0; i < row_width; i++) |
|
3995 |
{ |
|
3996 |
png_uint_16 v; |
|
3997 |
||
3998 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
|
3999 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4000 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4001 |
sp += 2; |
|
10576 | 4002 |
|
2 | 4003 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
4004 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4005 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4006 |
sp += 2; |
|
10576 | 4007 |
|
2 | 4008 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
4009 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4010 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4011 |
sp += 2; |
|
4012 |
} |
|
4013 |
} |
|
4014 |
break; |
|
4015 |
} |
|
10576 | 4016 |
|
2 | 4017 |
case PNG_COLOR_TYPE_RGB_ALPHA: |
4018 |
{ |
|
4019 |
if (row_info->bit_depth == 8) |
|
4020 |
{ |
|
4021 |
sp = row; |
|
4022 |
for (i = 0; i < row_width; i++) |
|
4023 |
{ |
|
4024 |
*sp = gamma_table[*sp]; |
|
4025 |
sp++; |
|
10576 | 4026 |
|
2 | 4027 |
*sp = gamma_table[*sp]; |
4028 |
sp++; |
|
10576 | 4029 |
|
2 | 4030 |
*sp = gamma_table[*sp]; |
4031 |
sp++; |
|
10576 | 4032 |
|
2 | 4033 |
sp++; |
4034 |
} |
|
4035 |
} |
|
10576 | 4036 |
|
2 | 4037 |
else /* if (row_info->bit_depth == 16) */ |
4038 |
{ |
|
4039 |
sp = row; |
|
4040 |
for (i = 0; i < row_width; i++) |
|
4041 |
{ |
|
4042 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
|
4043 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4044 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4045 |
sp += 2; |
|
10576 | 4046 |
|
2 | 4047 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
4048 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4049 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4050 |
sp += 2; |
|
10576 | 4051 |
|
2 | 4052 |
v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
4053 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4054 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4055 |
sp += 4; |
|
4056 |
} |
|
4057 |
} |
|
4058 |
break; |
|
4059 |
} |
|
10576 | 4060 |
|
2 | 4061 |
case PNG_COLOR_TYPE_GRAY_ALPHA: |
4062 |
{ |
|
4063 |
if (row_info->bit_depth == 8) |
|
4064 |
{ |
|
4065 |
sp = row; |
|
4066 |
for (i = 0; i < row_width; i++) |
|
4067 |
{ |
|
4068 |
*sp = gamma_table[*sp]; |
|
4069 |
sp += 2; |
|
4070 |
} |
|
4071 |
} |
|
10576 | 4072 |
|
2 | 4073 |
else /* if (row_info->bit_depth == 16) */ |
4074 |
{ |
|
4075 |
sp = row; |
|
4076 |
for (i = 0; i < row_width; i++) |
|
4077 |
{ |
|
4078 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
|
4079 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4080 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4081 |
sp += 4; |
|
4082 |
} |
|
4083 |
} |
|
4084 |
break; |
|
4085 |
} |
|
10576 | 4086 |
|
2 | 4087 |
case PNG_COLOR_TYPE_GRAY: |
4088 |
{ |
|
4089 |
if (row_info->bit_depth == 2) |
|
4090 |
{ |
|
4091 |
sp = row; |
|
4092 |
for (i = 0; i < row_width; i += 4) |
|
4093 |
{ |
|
4094 |
int a = *sp & 0xc0; |
|
4095 |
int b = *sp & 0x30; |
|
4096 |
int c = *sp & 0x0c; |
|
4097 |
int d = *sp & 0x03; |
|
4098 |
||
4099 |
*sp = (png_byte)( |
|
10576 | 4100 |
((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| |
4101 |
((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| |
|
4102 |
((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| |
|
4103 |
((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); |
|
2 | 4104 |
sp++; |
4105 |
} |
|
4106 |
} |
|
10576 | 4107 |
|
2 | 4108 |
if (row_info->bit_depth == 4) |
4109 |
{ |
|
4110 |
sp = row; |
|
4111 |
for (i = 0; i < row_width; i += 2) |
|
4112 |
{ |
|
4113 |
int msb = *sp & 0xf0; |
|
4114 |
int lsb = *sp & 0x0f; |
|
4115 |
||
4116 |
*sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
|
10576 | 4117 |
| (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); |
2 | 4118 |
sp++; |
4119 |
} |
|
4120 |
} |
|
10576 | 4121 |
|
2 | 4122 |
else if (row_info->bit_depth == 8) |
4123 |
{ |
|
4124 |
sp = row; |
|
4125 |
for (i = 0; i < row_width; i++) |
|
4126 |
{ |
|
4127 |
*sp = gamma_table[*sp]; |
|
4128 |
sp++; |
|
4129 |
} |
|
4130 |
} |
|
10576 | 4131 |
|
2 | 4132 |
else if (row_info->bit_depth == 16) |
4133 |
{ |
|
4134 |
sp = row; |
|
4135 |
for (i = 0; i < row_width; i++) |
|
4136 |
{ |
|
4137 |
png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; |
|
4138 |
*sp = (png_byte)((v >> 8) & 0xff); |
|
4139 |
*(sp + 1) = (png_byte)(v & 0xff); |
|
4140 |
sp += 2; |
|
4141 |
} |
|
4142 |
} |
|
4143 |
break; |
|
4144 |
} |
|
10576 | 4145 |
|
4146 |
default: |
|
4147 |
break; |
|
2 | 4148 |
} |
4149 |
} |
|
4150 |
} |
|
4151 |
#endif |
|
4152 |
||
10576 | 4153 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
4154 |
/* Encode the alpha channel to the output gamma (the input channel is always |
|
4155 |
* linear.) Called only with color types that have an alpha channel. Needs the |
|
4156 |
* from_1 tables. |
|
4157 |
*/ |
|
29913 | 4158 |
static void |
4159 |
png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) |
|
10576 | 4160 |
{ |
4161 |
png_uint_32 row_width = row_info->width; |
|
4162 |
||
4163 |
png_debug(1, "in png_do_encode_alpha"); |
|
4164 |
||
29913 | 4165 |
if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
10576 | 4166 |
{ |
4167 |
if (row_info->bit_depth == 8) |
|
4168 |
{ |
|
4169 |
PNG_CONST png_bytep table = png_ptr->gamma_from_1; |
|
4170 |
||
4171 |
if (table != NULL) |
|
4172 |
{ |
|
4173 |
PNG_CONST int step = |
|
4174 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; |
|
4175 |
||
4176 |
/* The alpha channel is the last component: */ |
|
4177 |
row += step - 1; |
|
4178 |
||
4179 |
for (; row_width > 0; --row_width, row += step) |
|
4180 |
*row = table[*row]; |
|
4181 |
||
4182 |
return; |
|
4183 |
} |
|
4184 |
} |
|
4185 |
||
4186 |
else if (row_info->bit_depth == 16) |
|
4187 |
{ |
|
4188 |
PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1; |
|
4189 |
PNG_CONST int gamma_shift = png_ptr->gamma_shift; |
|
4190 |
||
4191 |
if (table != NULL) |
|
4192 |
{ |
|
4193 |
PNG_CONST int step = |
|
4194 |
(row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4; |
|
4195 |
||
4196 |
/* The alpha channel is the last component: */ |
|
4197 |
row += step - 2; |
|
4198 |
||
4199 |
for (; row_width > 0; --row_width, row += step) |
|
4200 |
{ |
|
4201 |
png_uint_16 v; |
|
4202 |
||
4203 |
v = table[*(row + 1) >> gamma_shift][*row]; |
|
4204 |
*row = (png_byte)((v >> 8) & 0xff); |
|
4205 |
*(row + 1) = (png_byte)(v & 0xff); |
|
4206 |
} |
|
4207 |
||
4208 |
return; |
|
4209 |
} |
|
4210 |
} |
|
4211 |
} |
|
4212 |
||
4213 |
/* Only get to here if called with a weird row_info; no harm has been done, |
|
4214 |
* so just issue a warning. |
|
4215 |
*/ |
|
4216 |
png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); |
|
4217 |
} |
|
4218 |
#endif |
|
4219 |
||
4220 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
|
2 | 4221 |
/* Expands a palette row to an RGB or RGBA row depending |
4222 |
* upon whether you supply trans and num_trans. |
|
4223 |
*/ |
|
29913 | 4224 |
static void |
2 | 4225 |
png_do_expand_palette(png_row_infop row_info, png_bytep row, |
10576 | 4226 |
png_const_colorp palette, png_const_bytep trans_alpha, int num_trans) |
2 | 4227 |
{ |
4228 |
int shift, value; |
|
4229 |
png_bytep sp, dp; |
|
4230 |
png_uint_32 i; |
|
4231 |
png_uint_32 row_width=row_info->width; |
|
4232 |
||
10576 | 4233 |
png_debug(1, "in png_do_expand_palette"); |
4234 |
||
4235 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
|
2 | 4236 |
{ |
4237 |
if (row_info->bit_depth < 8) |
|
4238 |
{ |
|
4239 |
switch (row_info->bit_depth) |
|
4240 |
{ |
|
4241 |
case 1: |
|
4242 |
{ |
|
4243 |
sp = row + (png_size_t)((row_width - 1) >> 3); |
|
4244 |
dp = row + (png_size_t)row_width - 1; |
|
4245 |
shift = 7 - (int)((row_width + 7) & 0x07); |
|
4246 |
for (i = 0; i < row_width; i++) |
|
4247 |
{ |
|
4248 |
if ((*sp >> shift) & 0x01) |
|
4249 |
*dp = 1; |
|
10576 | 4250 |
|
2 | 4251 |
else |
4252 |
*dp = 0; |
|
10576 | 4253 |
|
2 | 4254 |
if (shift == 7) |
4255 |
{ |
|
4256 |
shift = 0; |
|
4257 |
sp--; |
|
4258 |
} |
|
10576 | 4259 |
|
2 | 4260 |
else |
4261 |
shift++; |
|
4262 |
||
4263 |
dp--; |
|
4264 |
} |
|
4265 |
break; |
|
4266 |
} |
|
10576 | 4267 |
|
2 | 4268 |
case 2: |
4269 |
{ |
|
4270 |
sp = row + (png_size_t)((row_width - 1) >> 2); |
|
4271 |
dp = row + (png_size_t)row_width - 1; |
|
4272 |
shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
|
4273 |
for (i = 0; i < row_width; i++) |
|
4274 |
{ |
|
4275 |
value = (*sp >> shift) & 0x03; |
|
4276 |
*dp = (png_byte)value; |
|
4277 |
if (shift == 6) |
|
4278 |
{ |
|
4279 |
shift = 0; |
|
4280 |
sp--; |
|
4281 |
} |
|
10576 | 4282 |
|
2 | 4283 |
else |
4284 |
shift += 2; |
|
4285 |
||
4286 |
dp--; |
|
4287 |
} |
|
4288 |
break; |
|
4289 |
} |
|
10576 | 4290 |
|
2 | 4291 |
case 4: |
4292 |
{ |
|
4293 |
sp = row + (png_size_t)((row_width - 1) >> 1); |
|
4294 |
dp = row + (png_size_t)row_width - 1; |
|
4295 |
shift = (int)((row_width & 0x01) << 2); |
|
4296 |
for (i = 0; i < row_width; i++) |
|
4297 |
{ |
|
4298 |
value = (*sp >> shift) & 0x0f; |
|
4299 |
*dp = (png_byte)value; |
|
4300 |
if (shift == 4) |
|
4301 |
{ |
|
4302 |
shift = 0; |
|
4303 |
sp--; |
|
4304 |
} |
|
10576 | 4305 |
|
2 | 4306 |
else |
4307 |
shift += 4; |
|
4308 |
||
4309 |
dp--; |
|
4310 |
} |
|
4311 |
break; |
|
4312 |
} |
|
10576 | 4313 |
|
4314 |
default: |
|
4315 |
break; |
|
2 | 4316 |
} |
4317 |
row_info->bit_depth = 8; |
|
4318 |
row_info->pixel_depth = 8; |
|
4319 |
row_info->rowbytes = row_width; |
|
4320 |
} |
|
10576 | 4321 |
|
4322 |
if (row_info->bit_depth == 8) |
|
2 | 4323 |
{ |
4324 |
{ |
|
10576 | 4325 |
if (num_trans > 0) |
2 | 4326 |
{ |
4327 |
sp = row + (png_size_t)row_width - 1; |
|
4328 |
dp = row + (png_size_t)(row_width << 2) - 1; |
|
4329 |
||
4330 |
for (i = 0; i < row_width; i++) |
|
4331 |
{ |
|
4332 |
if ((int)(*sp) >= num_trans) |
|
4333 |
*dp-- = 0xff; |
|
10576 | 4334 |
|
2 | 4335 |
else |
10576 | 4336 |
*dp-- = trans_alpha[*sp]; |
4337 |
||
2 | 4338 |
*dp-- = palette[*sp].blue; |
4339 |
*dp-- = palette[*sp].green; |
|
4340 |
*dp-- = palette[*sp].red; |
|
4341 |
sp--; |
|
4342 |
} |
|
4343 |
row_info->bit_depth = 8; |
|
4344 |
row_info->pixel_depth = 32; |
|
4345 |
row_info->rowbytes = row_width * 4; |
|
4346 |
row_info->color_type = 6; |
|
4347 |
row_info->channels = 4; |
|
4348 |
} |
|
10576 | 4349 |
|
2 | 4350 |
else |
4351 |
{ |
|
4352 |
sp = row + (png_size_t)row_width - 1; |
|
4353 |
dp = row + (png_size_t)(row_width * 3) - 1; |
|
4354 |
||
4355 |
for (i = 0; i < row_width; i++) |
|
4356 |
{ |
|
4357 |
*dp-- = palette[*sp].blue; |
|
4358 |
*dp-- = palette[*sp].green; |
|
4359 |
*dp-- = palette[*sp].red; |
|
4360 |
sp--; |
|
4361 |
} |
|
10576 | 4362 |
|
2 | 4363 |
row_info->bit_depth = 8; |
4364 |
row_info->pixel_depth = 24; |
|
4365 |
row_info->rowbytes = row_width * 3; |
|
4366 |
row_info->color_type = 2; |
|
4367 |
row_info->channels = 3; |
|
4368 |
} |
|
4369 |
} |
|
4370 |
} |
|
4371 |
} |
|
4372 |
} |
|
4373 |
||
4374 |
/* If the bit depth < 8, it is expanded to 8. Also, if the already |
|
4375 |
* expanded transparency value is supplied, an alpha channel is built. |
|
4376 |
*/ |
|
29913 | 4377 |
static void |
2 | 4378 |
png_do_expand(png_row_infop row_info, png_bytep row, |
10576 | 4379 |
png_const_color_16p trans_color) |
2 | 4380 |
{ |
4381 |
int shift, value; |
|
4382 |
png_bytep sp, dp; |
|
4383 |
png_uint_32 i; |
|
4384 |
png_uint_32 row_width=row_info->width; |
|
4385 |
||
10576 | 4386 |
png_debug(1, "in png_do_expand"); |
4387 |
||
2 | 4388 |
{ |
4389 |
if (row_info->color_type == PNG_COLOR_TYPE_GRAY) |
|
4390 |
{ |
|
29913 | 4391 |
unsigned int gray = trans_color != NULL ? trans_color->gray : 0; |
2 | 4392 |
|
4393 |
if (row_info->bit_depth < 8) |
|
4394 |
{ |
|
4395 |
switch (row_info->bit_depth) |
|
4396 |
{ |
|
4397 |
case 1: |
|
4398 |
{ |
|
29913 | 4399 |
gray = (gray & 0x01) * 0xff; |
2 | 4400 |
sp = row + (png_size_t)((row_width - 1) >> 3); |
4401 |
dp = row + (png_size_t)row_width - 1; |
|
4402 |
shift = 7 - (int)((row_width + 7) & 0x07); |
|
4403 |
for (i = 0; i < row_width; i++) |
|
4404 |
{ |
|
4405 |
if ((*sp >> shift) & 0x01) |
|
4406 |
*dp = 0xff; |
|
10576 | 4407 |
|
2 | 4408 |
else |
4409 |
*dp = 0; |
|
10576 | 4410 |
|
2 | 4411 |
if (shift == 7) |
4412 |
{ |
|
4413 |
shift = 0; |
|
4414 |
sp--; |
|
4415 |
} |
|
10576 | 4416 |
|
2 | 4417 |
else |
4418 |
shift++; |
|
4419 |
||
4420 |
dp--; |
|
4421 |
} |
|
4422 |
break; |
|
4423 |
} |
|
10576 | 4424 |
|
2 | 4425 |
case 2: |
4426 |
{ |
|
29913 | 4427 |
gray = (gray & 0x03) * 0x55; |
2 | 4428 |
sp = row + (png_size_t)((row_width - 1) >> 2); |
4429 |
dp = row + (png_size_t)row_width - 1; |
|
4430 |
shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); |
|
4431 |
for (i = 0; i < row_width; i++) |
|
4432 |
{ |
|
4433 |
value = (*sp >> shift) & 0x03; |
|
4434 |
*dp = (png_byte)(value | (value << 2) | (value << 4) | |
|
4435 |
(value << 6)); |
|
4436 |
if (shift == 6) |
|
4437 |
{ |
|
4438 |
shift = 0; |
|
4439 |
sp--; |
|
4440 |
} |
|
10576 | 4441 |
|
2 | 4442 |
else |
4443 |
shift += 2; |
|
4444 |
||
4445 |
dp--; |
|
4446 |
} |
|
4447 |
break; |
|
4448 |
} |
|
10576 | 4449 |
|
2 | 4450 |
case 4: |
4451 |
{ |
|
29913 | 4452 |
gray = (gray & 0x0f) * 0x11; |
2 | 4453 |
sp = row + (png_size_t)((row_width - 1) >> 1); |
4454 |
dp = row + (png_size_t)row_width - 1; |
|
4455 |
shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); |
|
4456 |
for (i = 0; i < row_width; i++) |
|
4457 |
{ |
|
4458 |
value = (*sp >> shift) & 0x0f; |
|
4459 |
*dp = (png_byte)(value | (value << 4)); |
|
4460 |
if (shift == 4) |
|
4461 |
{ |
|
4462 |
shift = 0; |
|
4463 |
sp--; |
|
4464 |
} |
|
10576 | 4465 |
|
2 | 4466 |
else |
4467 |
shift = 4; |
|
4468 |
||
4469 |
dp--; |
|
4470 |
} |
|
4471 |
break; |
|
4472 |
} |
|
10576 | 4473 |
|
4474 |
default: |
|
4475 |
break; |
|
2 | 4476 |
} |
10576 | 4477 |
|
2 | 4478 |
row_info->bit_depth = 8; |
4479 |
row_info->pixel_depth = 8; |
|
4480 |
row_info->rowbytes = row_width; |
|
4481 |
} |
|
4482 |
||
10576 | 4483 |
if (trans_color != NULL) |
2 | 4484 |
{ |
4485 |
if (row_info->bit_depth == 8) |
|
4486 |
{ |
|
10576 | 4487 |
gray = gray & 0xff; |
2 | 4488 |
sp = row + (png_size_t)row_width - 1; |
4489 |
dp = row + (png_size_t)(row_width << 1) - 1; |
|
10576 | 4490 |
|
2 | 4491 |
for (i = 0; i < row_width; i++) |
4492 |
{ |
|
35296 | 4493 |
if ((*sp & 0xffU) == gray) |
2 | 4494 |
*dp-- = 0; |
10576 | 4495 |
|
2 | 4496 |
else |
4497 |
*dp-- = 0xff; |
|
10576 | 4498 |
|
2 | 4499 |
*dp-- = *sp--; |
4500 |
} |
|
4501 |
} |
|
10576 | 4502 |
|
2 | 4503 |
else if (row_info->bit_depth == 16) |
4504 |
{ |
|
29913 | 4505 |
unsigned int gray_high = (gray >> 8) & 0xff; |
4506 |
unsigned int gray_low = gray & 0xff; |
|
2 | 4507 |
sp = row + row_info->rowbytes - 1; |
4508 |
dp = row + (row_info->rowbytes << 1) - 1; |
|
4509 |
for (i = 0; i < row_width; i++) |
|
4510 |
{ |
|
35296 | 4511 |
if ((*(sp - 1) & 0xffU) == gray_high && |
4512 |
(*(sp) & 0xffU) == gray_low) |
|
2 | 4513 |
{ |
4514 |
*dp-- = 0; |
|
4515 |
*dp-- = 0; |
|
4516 |
} |
|
10576 | 4517 |
|
2 | 4518 |
else |
4519 |
{ |
|
4520 |
*dp-- = 0xff; |
|
4521 |
*dp-- = 0xff; |
|
4522 |
} |
|
10576 | 4523 |
|
2 | 4524 |
*dp-- = *sp--; |
4525 |
*dp-- = *sp--; |
|
4526 |
} |
|
4527 |
} |
|
10576 | 4528 |
|
2 | 4529 |
row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; |
4530 |
row_info->channels = 2; |
|
4531 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); |
|
4532 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, |
|
4533 |
row_width); |
|
4534 |
} |
|
4535 |
} |
|
29913 | 4536 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB && |
4537 |
trans_color != NULL) |
|
2 | 4538 |
{ |
4539 |
if (row_info->bit_depth == 8) |
|
4540 |
{ |
|
10576 | 4541 |
png_byte red = (png_byte)(trans_color->red & 0xff); |
4542 |
png_byte green = (png_byte)(trans_color->green & 0xff); |
|
4543 |
png_byte blue = (png_byte)(trans_color->blue & 0xff); |
|
2 | 4544 |
sp = row + (png_size_t)row_info->rowbytes - 1; |
4545 |
dp = row + (png_size_t)(row_width << 2) - 1; |
|
4546 |
for (i = 0; i < row_width; i++) |
|
4547 |
{ |
|
10576 | 4548 |
if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) |
2 | 4549 |
*dp-- = 0; |
10576 | 4550 |
|
2 | 4551 |
else |
4552 |
*dp-- = 0xff; |
|
10576 | 4553 |
|
2 | 4554 |
*dp-- = *sp--; |
4555 |
*dp-- = *sp--; |
|
4556 |
*dp-- = *sp--; |
|
4557 |
} |
|
4558 |
} |
|
4559 |
else if (row_info->bit_depth == 16) |
|
4560 |
{ |
|
10576 | 4561 |
png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff); |
4562 |
png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff); |
|
4563 |
png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff); |
|
4564 |
png_byte red_low = (png_byte)(trans_color->red & 0xff); |
|
4565 |
png_byte green_low = (png_byte)(trans_color->green & 0xff); |
|
4566 |
png_byte blue_low = (png_byte)(trans_color->blue & 0xff); |
|
2 | 4567 |
sp = row + row_info->rowbytes - 1; |
4568 |
dp = row + (png_size_t)(row_width << 3) - 1; |
|
4569 |
for (i = 0; i < row_width; i++) |
|
4570 |
{ |
|
10576 | 4571 |
if (*(sp - 5) == red_high && |
4572 |
*(sp - 4) == red_low && |
|
4573 |
*(sp - 3) == green_high && |
|
4574 |
*(sp - 2) == green_low && |
|
4575 |
*(sp - 1) == blue_high && |
|
4576 |
*(sp ) == blue_low) |
|
2 | 4577 |
{ |
4578 |
*dp-- = 0; |
|
4579 |
*dp-- = 0; |
|
4580 |
} |
|
10576 | 4581 |
|
2 | 4582 |
else |
4583 |
{ |
|
4584 |
*dp-- = 0xff; |
|
4585 |
*dp-- = 0xff; |
|
4586 |
} |
|
10576 | 4587 |
|
2 | 4588 |
*dp-- = *sp--; |
4589 |
*dp-- = *sp--; |
|
4590 |
*dp-- = *sp--; |
|
4591 |
*dp-- = *sp--; |
|
4592 |
*dp-- = *sp--; |
|
4593 |
*dp-- = *sp--; |
|
4594 |
} |
|
4595 |
} |
|
4596 |
row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; |
|
4597 |
row_info->channels = 4; |
|
4598 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); |
|
10576 | 4599 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
2 | 4600 |
} |
4601 |
} |
|
4602 |
} |
|
4603 |
#endif |
|
4604 |
||
10576 | 4605 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
29913 | 4606 |
/* If the bit depth is 8 and the color type is not a palette type expand the |
10576 | 4607 |
* whole row to 16 bits. Has no effect otherwise. |
4608 |
*/ |
|
29913 | 4609 |
static void |
10576 | 4610 |
png_do_expand_16(png_row_infop row_info, png_bytep row) |
4611 |
{ |
|
4612 |
if (row_info->bit_depth == 8 && |
|
4613 |
row_info->color_type != PNG_COLOR_TYPE_PALETTE) |
|
4614 |
{ |
|
4615 |
/* The row have a sequence of bytes containing [0..255] and we need |
|
4616 |
* to turn it into another row containing [0..65535], to do this we |
|
4617 |
* calculate: |
|
4618 |
* |
|
4619 |
* (input / 255) * 65535 |
|
4620 |
* |
|
4621 |
* Which happens to be exactly input * 257 and this can be achieved |
|
4622 |
* simply by byte replication in place (copying backwards). |
|
4623 |
*/ |
|
4624 |
png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ |
|
4625 |
png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ |
|
4626 |
while (dp > sp) |
|
4627 |
dp[-2] = dp[-1] = *--sp, dp -= 2; |
|
4628 |
||
4629 |
row_info->rowbytes *= 2; |
|
4630 |
row_info->bit_depth = 16; |
|
4631 |
row_info->pixel_depth = (png_byte)(row_info->channels * 16); |
|
4632 |
} |
|
4633 |
} |
|
4634 |
#endif |
|
4635 |
||
4636 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
|
29913 | 4637 |
static void |
10576 | 4638 |
png_do_quantize(png_row_infop row_info, png_bytep row, |
4639 |
png_const_bytep palette_lookup, png_const_bytep quantize_lookup) |
|
2 | 4640 |
{ |
4641 |
png_bytep sp, dp; |
|
4642 |
png_uint_32 i; |
|
4643 |
png_uint_32 row_width=row_info->width; |
|
4644 |
||
10576 | 4645 |
png_debug(1, "in png_do_quantize"); |
4646 |
||
4647 |
if (row_info->bit_depth == 8) |
|
2 | 4648 |
{ |
10576 | 4649 |
if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup) |
2 | 4650 |
{ |
4651 |
int r, g, b, p; |
|
4652 |
sp = row; |
|
4653 |
dp = row; |
|
4654 |
for (i = 0; i < row_width; i++) |
|
4655 |
{ |
|
4656 |
r = *sp++; |
|
4657 |
g = *sp++; |
|
4658 |
b = *sp++; |
|
4659 |
||
10576 | 4660 |
/* This looks real messy, but the compiler will reduce |
4661 |
* it down to a reasonable formula. For example, with |
|
4662 |
* 5 bits per color, we get: |
|
4663 |
* p = (((r >> 3) & 0x1f) << 10) | |
|
4664 |
* (((g >> 3) & 0x1f) << 5) | |
|
4665 |
* ((b >> 3) & 0x1f); |
|
4666 |
*/ |
|
4667 |
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
|
4668 |
((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
|
4669 |
(PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
|
4670 |
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
|
4671 |
((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
|
4672 |
(PNG_QUANTIZE_BLUE_BITS)) | |
|
4673 |
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
|
4674 |
((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
|
2 | 4675 |
|
4676 |
*dp++ = palette_lookup[p]; |
|
4677 |
} |
|
10576 | 4678 |
|
2 | 4679 |
row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
4680 |
row_info->channels = 1; |
|
4681 |
row_info->pixel_depth = row_info->bit_depth; |
|
10576 | 4682 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
2 | 4683 |
} |
10576 | 4684 |
|
2 | 4685 |
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && |
10576 | 4686 |
palette_lookup != NULL) |
2 | 4687 |
{ |
4688 |
int r, g, b, p; |
|
4689 |
sp = row; |
|
4690 |
dp = row; |
|
4691 |
for (i = 0; i < row_width; i++) |
|
4692 |
{ |
|
4693 |
r = *sp++; |
|
4694 |
g = *sp++; |
|
4695 |
b = *sp++; |
|
4696 |
sp++; |
|
4697 |
||
10576 | 4698 |
p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & |
4699 |
((1 << PNG_QUANTIZE_RED_BITS) - 1)) << |
|
4700 |
(PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | |
|
4701 |
(((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & |
|
4702 |
((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << |
|
4703 |
(PNG_QUANTIZE_BLUE_BITS)) | |
|
4704 |
((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & |
|
4705 |
((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); |
|
2 | 4706 |
|
4707 |
*dp++ = palette_lookup[p]; |
|
4708 |
} |
|
10576 | 4709 |
|
2 | 4710 |
row_info->color_type = PNG_COLOR_TYPE_PALETTE; |
4711 |
row_info->channels = 1; |
|
4712 |
row_info->pixel_depth = row_info->bit_depth; |
|
10576 | 4713 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); |
2 | 4714 |
} |
10576 | 4715 |
|
2 | 4716 |
else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
10576 | 4717 |
quantize_lookup) |
2 | 4718 |
{ |
4719 |
sp = row; |
|
10576 | 4720 |
|
2 | 4721 |
for (i = 0; i < row_width; i++, sp++) |
4722 |
{ |
|
10576 | 4723 |
*sp = quantize_lookup[*sp]; |
2 | 4724 |
} |
4725 |
} |
|
4726 |
} |
|
4727 |
} |
|
29913 | 4728 |
#endif /* READ_QUANTIZE */ |
4729 |
||
4730 |
/* Transform the row. The order of transformations is significant, |
|
4731 |
* and is very touchy. If you add a transformation, take care to |
|
4732 |
* decide how it fits in with the other transformations here. |
|
4733 |
*/ |
|
2 | 4734 |
void /* PRIVATE */ |
29913 | 4735 |
png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) |
2 | 4736 |
{ |
29913 | 4737 |
png_debug(1, "in png_do_read_transformations"); |
4738 |
||
4739 |
if (png_ptr->row_buf == NULL) |
|
2 | 4740 |
{ |
29913 | 4741 |
/* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this |
4742 |
* error is incredibly rare and incredibly easy to debug without this |
|
4743 |
* information. |
|
4744 |
*/ |
|
4745 |
png_error(png_ptr, "NULL row buffer"); |
|
4746 |
} |
|
4747 |
||
4748 |
/* The following is debugging; prior to 1.5.4 the code was never compiled in; |
|
4749 |
* in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro |
|
4750 |
* PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for |
|
4751 |
* all transformations, however in practice the ROW_INIT always gets done on |
|
4752 |
* demand, if necessary. |
|
4753 |
*/ |
|
4754 |
if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && |
|
4755 |
(png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) |
|
4756 |
{ |
|
4757 |
/* Application has failed to call either png_read_start_image() or |
|
4758 |
* png_read_update_info() after setting transforms that expand pixels. |
|
4759 |
* This check added to libpng-1.2.19 (but not enabled until 1.5.4). |
|
4760 |
*/ |
|
4761 |
png_error(png_ptr, "Uninitialized row"); |
|
4762 |
} |
|
4763 |
||
4764 |
#ifdef PNG_READ_EXPAND_SUPPORTED |
|
4765 |
if ((png_ptr->transformations & PNG_EXPAND) != 0) |
|
4766 |
{ |
|
4767 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) |
|
2 | 4768 |
{ |
29913 | 4769 |
png_do_expand_palette(row_info, png_ptr->row_buf + 1, |
4770 |
png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); |
|
4771 |
} |
|
4772 |
||
4773 |
else |
|
4774 |
{ |
|
4775 |
if (png_ptr->num_trans != 0 && |
|
4776 |
(png_ptr->transformations & PNG_EXPAND_tRNS) != 0) |
|
4777 |
png_do_expand(row_info, png_ptr->row_buf + 1, |
|
4778 |
&(png_ptr->trans_color)); |
|
10576 | 4779 |
|
2 | 4780 |
else |
29913 | 4781 |
png_do_expand(row_info, png_ptr->row_buf + 1, |
4782 |
NULL); |
|
2 | 4783 |
} |
29913 | 4784 |
} |
4785 |
#endif |
|
4786 |
||
4787 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
|
4788 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
|
4789 |
(png_ptr->transformations & PNG_COMPOSE) == 0 && |
|
4790 |
(row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
|
4791 |
row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
|
4792 |
png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
|
4793 |
0 /* at_start == false, because SWAP_ALPHA happens later */); |
|
4794 |
#endif |
|
4795 |
||
4796 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
|
4797 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) |
|
4798 |
{ |
|
4799 |
int rgb_error = |
|
4800 |
png_do_rgb_to_gray(png_ptr, row_info, |
|
4801 |
png_ptr->row_buf + 1); |
|
4802 |
||
4803 |
if (rgb_error != 0) |
|
2 | 4804 |
{ |
29913 | 4805 |
png_ptr->rgb_to_gray_status=1; |
4806 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
|
4807 |
PNG_RGB_TO_GRAY_WARN) |
|
4808 |
png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
|
4809 |
||
4810 |
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == |
|
4811 |
PNG_RGB_TO_GRAY_ERR) |
|
4812 |
png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); |
|
2 | 4813 |
} |
4814 |
} |
|
29913 | 4815 |
#endif |
4816 |
||
4817 |
/* From Andreas Dilger e-mail to png-implement, 26 March 1998: |
|
4818 |
* |
|
4819 |
* In most cases, the "simple transparency" should be done prior to doing |
|
4820 |
* gray-to-RGB, or you will have to test 3x as many bytes to check if a |
|
4821 |
* pixel is transparent. You would also need to make sure that the |
|
4822 |
* transparency information is upgraded to RGB. |
|
4823 |
* |
|
4824 |
* To summarize, the current flow is: |
|
4825 |
* - Gray + simple transparency -> compare 1 or 2 gray bytes and composite |
|
4826 |
* with background "in place" if transparent, |
|
4827 |
* convert to RGB if necessary |
|
4828 |
* - Gray + alpha -> composite with gray background and remove alpha bytes, |
|
4829 |
* convert to RGB if necessary |
|
4830 |
* |
|
4831 |
* To support RGB backgrounds for gray images we need: |
|
4832 |
* - Gray + simple transparency -> convert to RGB + simple transparency, |
|
4833 |
* compare 3 or 6 bytes and composite with |
|
4834 |
* background "in place" if transparent |
|
4835 |
* (3x compare/pixel compared to doing |
|
4836 |
* composite with gray bkgrnd) |
|
4837 |
* - Gray + alpha -> convert to RGB + alpha, composite with background and |
|
4838 |
* remove alpha bytes (3x float |
|
4839 |
* operations/pixel compared with composite |
|
4840 |
* on gray background) |
|
4841 |
* |
|
4842 |
* Greg's change will do this. The reason it wasn't done before is for |
|
4843 |
* performance, as this increases the per-pixel operations. If we would check |
|
4844 |
* in advance if the background was gray or RGB, and position the gray-to-RGB |
|
4845 |
* transform appropriately, then it would save a lot of work/time. |
|
4846 |
*/ |
|
4847 |
||
4848 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
|
4849 |
/* If gray -> RGB, do so now only if background is non-gray; else do later |
|
4850 |
* for performance reasons |
|
4851 |
*/ |
|
4852 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
|
4853 |
(png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0) |
|
4854 |
png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
|
4855 |
#endif |
|
4856 |
||
4857 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
|
4858 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
|
4859 |
if ((png_ptr->transformations & PNG_COMPOSE) != 0) |
|
4860 |
png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); |
|
4861 |
#endif |
|
4862 |
||
4863 |
#ifdef PNG_READ_GAMMA_SUPPORTED |
|
4864 |
if ((png_ptr->transformations & PNG_GAMMA) != 0 && |
|
4865 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED |
|
4866 |
/* Because RGB_TO_GRAY does the gamma transform. */ |
|
4867 |
(png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 && |
|
4868 |
#endif |
|
4869 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ |
|
4870 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) |
|
4871 |
/* Because PNG_COMPOSE does the gamma transform if there is something to |
|
4872 |
* do (if there is an alpha channel or transparency.) |
|
4873 |
*/ |
|
35296 | 4874 |
!((png_ptr->transformations & PNG_COMPOSE) != 0 && |
29913 | 4875 |
((png_ptr->num_trans != 0) || |
4876 |
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) && |
|
4877 |
#endif |
|
4878 |
/* Because png_init_read_transformations transforms the palette, unless |
|
4879 |
* RGB_TO_GRAY will do the transform. |
|
4880 |
*/ |
|
4881 |
(png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) |
|
4882 |
png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); |
|
4883 |
#endif |
|
4884 |
||
4885 |
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED |
|
4886 |
if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && |
|
4887 |
(png_ptr->transformations & PNG_COMPOSE) != 0 && |
|
4888 |
(row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || |
|
4889 |
row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) |
|
4890 |
png_do_strip_channel(row_info, png_ptr->row_buf + 1, |
|
4891 |
0 /* at_start == false, because SWAP_ALPHA happens later */); |
|
4892 |
#endif |
|
4893 |
||
4894 |
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED |
|
4895 |
if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && |
|
4896 |
(row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) |
|
4897 |
png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); |
|
4898 |
#endif |
|
4899 |
||
4900 |
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED |
|
4901 |
if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) |
|
4902 |
png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); |
|
4903 |
#endif |
|
4904 |
||
4905 |
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED |
|
4906 |
/* There is no harm in doing both of these because only one has any effect, |
|
4907 |
* by putting the 'scale' option first if the app asks for scale (either by |
|
4908 |
* calling the API or in a TRANSFORM flag) this is what happens. |
|
4909 |
*/ |
|
4910 |
if ((png_ptr->transformations & PNG_16_TO_8) != 0) |
|
4911 |
png_do_chop(row_info, png_ptr->row_buf + 1); |
|
4912 |
#endif |
|
4913 |
||
4914 |
#ifdef PNG_READ_QUANTIZE_SUPPORTED |
|
4915 |
if ((png_ptr->transformations & PNG_QUANTIZE) != 0) |
|
4916 |
{ |
|
4917 |
png_do_quantize(row_info, png_ptr->row_buf + 1, |
|
4918 |
png_ptr->palette_lookup, png_ptr->quantize_index); |
|
4919 |
||
4920 |
if (row_info->rowbytes == 0) |
|
4921 |
png_error(png_ptr, "png_do_quantize returned rowbytes=0"); |
|
4922 |
} |
|
4923 |
#endif /* READ_QUANTIZE */ |
|
4924 |
||
4925 |
#ifdef PNG_READ_EXPAND_16_SUPPORTED |
|
4926 |
/* Do the expansion now, after all the arithmetic has been done. Notice |
|
4927 |
* that previous transformations can handle the PNG_EXPAND_16 flag if this |
|
4928 |
* is efficient (particularly true in the case of gamma correction, where |
|
4929 |
* better accuracy results faster!) |
|
4930 |
*/ |
|
4931 |
if ((png_ptr->transformations & PNG_EXPAND_16) != 0) |
|
4932 |
png_do_expand_16(row_info, png_ptr->row_buf + 1); |
|
4933 |
#endif |
|
4934 |
||
4935 |
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED |
|
4936 |
/* NOTE: moved here in 1.5.4 (from much later in this list.) */ |
|
4937 |
if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && |
|
4938 |
(png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0) |
|
4939 |
png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); |
|
4940 |
#endif |
|
4941 |
||
4942 |
#ifdef PNG_READ_INVERT_SUPPORTED |
|
4943 |
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) |
|
4944 |
png_do_invert(row_info, png_ptr->row_buf + 1); |
|
4945 |
#endif |
|
4946 |
||
4947 |
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED |
|
4948 |
if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) |
|
4949 |
png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); |
|
4950 |
#endif |
|
4951 |
||
4952 |
#ifdef PNG_READ_SHIFT_SUPPORTED |
|
4953 |
if ((png_ptr->transformations & PNG_SHIFT) != 0) |
|
4954 |
png_do_unshift(row_info, png_ptr->row_buf + 1, |
|
4955 |
&(png_ptr->shift)); |
|
4956 |
#endif |
|
4957 |
||
4958 |
#ifdef PNG_READ_PACK_SUPPORTED |
|
4959 |
if ((png_ptr->transformations & PNG_PACK) != 0) |
|
4960 |
png_do_unpack(row_info, png_ptr->row_buf + 1); |
|
4961 |
#endif |
|
4962 |
||
4963 |
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED |
|
4964 |
/* Added at libpng-1.5.10 */ |
|
4965 |
if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && |
|
4966 |
png_ptr->num_palette_max >= 0) |
|
4967 |
png_do_check_palette_indexes(png_ptr, row_info); |
|
4968 |
#endif |
|
4969 |
||
4970 |
#ifdef PNG_READ_BGR_SUPPORTED |
|
4971 |
if ((png_ptr->transformations & PNG_BGR) != 0) |
|
4972 |
png_do_bgr(row_info, png_ptr->row_buf + 1); |
|
4973 |
#endif |
|
4974 |
||
4975 |
#ifdef PNG_READ_PACKSWAP_SUPPORTED |
|
4976 |
if ((png_ptr->transformations & PNG_PACKSWAP) != 0) |
|
4977 |
png_do_packswap(row_info, png_ptr->row_buf + 1); |
|
4978 |
#endif |
|
4979 |
||
4980 |
#ifdef PNG_READ_FILLER_SUPPORTED |
|
4981 |
if ((png_ptr->transformations & PNG_FILLER) != 0) |
|
4982 |
png_do_read_filler(row_info, png_ptr->row_buf + 1, |
|
4983 |
(png_uint_32)png_ptr->filler, png_ptr->flags); |
|
4984 |
#endif |
|
4985 |
||
4986 |
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED |
|
4987 |
if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) |
|
4988 |
png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); |
|
4989 |
#endif |
|
4990 |
||
4991 |
#ifdef PNG_READ_16BIT_SUPPORTED |
|
4992 |
#ifdef PNG_READ_SWAP_SUPPORTED |
|
4993 |
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) |
|
4994 |
png_do_swap(row_info, png_ptr->row_buf + 1); |
|
4995 |
#endif |
|
4996 |
#endif |
|
4997 |
||
4998 |
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED |
|
4999 |
if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) |
|
5000 |
{ |
|
5001 |
if (png_ptr->read_user_transform_fn != NULL) |
|
5002 |
(*(png_ptr->read_user_transform_fn)) /* User read transform function */ |
|
5003 |
(png_ptr, /* png_ptr */ |
|
5004 |
row_info, /* row_info: */ |
|
5005 |
/* png_uint_32 width; width of row */ |
|
5006 |
/* png_size_t rowbytes; number of bytes in row */ |
|
5007 |
/* png_byte color_type; color type of pixels */ |
|
5008 |
/* png_byte bit_depth; bit depth of samples */ |
|
5009 |
/* png_byte channels; number of channels (1-4) */ |
|
5010 |
/* png_byte pixel_depth; bits per pixel (depth*channels) */ |
|
5011 |
png_ptr->row_buf + 1); /* start of pixel data for row */ |
|
5012 |
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED |
|
5013 |
if (png_ptr->user_transform_depth != 0) |
|
5014 |
row_info->bit_depth = png_ptr->user_transform_depth; |
|
5015 |
||
5016 |
if (png_ptr->user_transform_channels != 0) |
|
5017 |
row_info->channels = png_ptr->user_transform_channels; |
|
5018 |
#endif |
|
5019 |
row_info->pixel_depth = (png_byte)(row_info->bit_depth * |
|
5020 |
row_info->channels); |
|
5021 |
||
5022 |
row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); |
|
5023 |
} |
|
5024 |
#endif |
|
2 | 5025 |
} |
29913 | 5026 |
|
5027 |
#endif /* READ_TRANSFORMS */ |
|
5028 |
#endif /* READ */ |