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