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