pngcrush  1.8.13
About: Pngcrush is an optimizer for PNG (Portable Network Graphics) files.
  Fossies Dox: pngcrush-1.8.13.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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