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