]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libpng/lib/dist/pngrtran.c
update
[l4.git] / l4 / pkg / libpng / lib / dist / pngrtran.c
1
2 /* pngrtran.c - transforms the data in a row for PNG readers
3  *
4  * Last changed in libpng 1.5.2 [March 31, 2011]
5  * Copyright (c) 1998-2011 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_structp 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 |
46                            PNG_FLAG_CRC_CRITICAL_IGNORE;
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       case PNG_CRC_ERROR_QUIT:                                /* Error/quit */
53
54       case PNG_CRC_DEFAULT:
55       default:
56          png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
57          break;
58    }
59
60    /* Tell libpng how we react to CRC errors in ancillary chunks */
61    switch (ancil_action)
62    {
63       case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */
64          break;
65
66       case PNG_CRC_WARN_USE:                              /* Warn/use data */
67          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
68          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
69          break;
70
71       case PNG_CRC_QUIET_USE:                            /* Quiet/use data */
72          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
73          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
74                            PNG_FLAG_CRC_ANCILLARY_NOWARN;
75          break;
76
77       case PNG_CRC_ERROR_QUIT:                               /* Error/quit */
78          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
79          png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
80          break;
81
82       case PNG_CRC_WARN_DISCARD:                      /* Warn/discard data */
83
84       case PNG_CRC_DEFAULT:
85       default:
86          png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
87          break;
88    }
89 }
90
91 #ifdef PNG_READ_BACKGROUND_SUPPORTED
92 /* Handle alpha and tRNS via a background color */
93 void PNGFAPI
94 png_set_background_fixed(png_structp png_ptr,
95     png_const_color_16p background_color, int background_gamma_code,
96     int need_expand, png_fixed_point background_gamma)
97 {
98    png_debug(1, "in png_set_background_fixed");
99
100    if (png_ptr == NULL)
101       return;
102
103    if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
104    {
105       png_warning(png_ptr, "Application must supply a known background gamma");
106       return;
107    }
108
109    png_ptr->transformations |= PNG_BACKGROUND;
110    png_memcpy(&(png_ptr->background), background_color,
111       png_sizeof(png_color_16));
112    png_ptr->background_gamma = background_gamma;
113    png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
114    png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
115 }
116
117 #  ifdef PNG_FLOATING_POINT_SUPPORTED
118 void PNGAPI
119 png_set_background(png_structp png_ptr,
120     png_const_color_16p background_color, int background_gamma_code,
121     int need_expand, double background_gamma)
122 {
123    png_set_background_fixed(png_ptr, background_color, background_gamma_code,
124       need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
125 }
126 #  endif  /* FLOATING_POINT */
127 #endif /* READ_BACKGROUND */
128
129 #ifdef PNG_READ_16_TO_8_SUPPORTED
130 /* Strip 16 bit depth files to 8 bit depth */
131 void PNGAPI
132 png_set_strip_16(png_structp png_ptr)
133 {
134    png_debug(1, "in png_set_strip_16");
135
136    if (png_ptr == NULL)
137       return;
138
139    png_ptr->transformations |= PNG_16_TO_8;
140    png_ptr->transformations &= ~PNG_EXPAND_16;
141 }
142 #endif
143
144 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
145 void PNGAPI
146 png_set_strip_alpha(png_structp png_ptr)
147 {
148    png_debug(1, "in png_set_strip_alpha");
149
150    if (png_ptr == NULL)
151       return;
152
153    png_ptr->transformations |= PNG_STRIP_ALPHA;
154 }
155 #endif
156
157 #ifdef PNG_READ_QUANTIZE_SUPPORTED
158 /* Dither file to 8 bit.  Supply a palette, the current number
159  * of elements in the palette, the maximum number of elements
160  * allowed, and a histogram if possible.  If the current number
161  * of colors is greater then the maximum number, the palette will be
162  * modified to fit in the maximum number.  "full_quantize" indicates
163  * whether we need a quantizing cube set up for RGB images, or if we
164  * simply are reducing the number of colors in a paletted image.
165  */
166
167 typedef struct png_dsort_struct
168 {
169    struct png_dsort_struct FAR * next;
170    png_byte left;
171    png_byte right;
172 } png_dsort;
173 typedef png_dsort FAR *       png_dsortp;
174 typedef png_dsort FAR * FAR * png_dsortpp;
175
176 void PNGAPI
177 png_set_quantize(png_structp png_ptr, png_colorp palette,
178     int num_palette, int maximum_colors, png_const_uint_16p histogram,
179     int full_quantize)
180 {
181    png_debug(1, "in png_set_quantize");
182
183    if (png_ptr == NULL)
184       return;
185
186    png_ptr->transformations |= PNG_QUANTIZE;
187
188    if (!full_quantize)
189    {
190       int i;
191
192       png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
193           (png_uint_32)(num_palette * png_sizeof(png_byte)));
194       for (i = 0; i < num_palette; i++)
195          png_ptr->quantize_index[i] = (png_byte)i;
196    }
197
198    if (num_palette > maximum_colors)
199    {
200       if (histogram != NULL)
201       {
202          /* This is easy enough, just throw out the least used colors.
203           * Perhaps not the best solution, but good enough.
204           */
205
206          int i;
207
208          /* Initialize an array to sort colors */
209          png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
210              (png_uint_32)(num_palette * png_sizeof(png_byte)));
211
212          /* Initialize the quantize_sort array */
213          for (i = 0; i < num_palette; i++)
214             png_ptr->quantize_sort[i] = (png_byte)i;
215
216          /* Find the least used palette entries by starting a
217           * bubble sort, and running it until we have sorted
218           * out enough colors.  Note that we don't care about
219           * sorting all the colors, just finding which are
220           * least used.
221           */
222
223          for (i = num_palette - 1; i >= maximum_colors; i--)
224          {
225             int done; /* To stop early if the list is pre-sorted */
226             int j;
227
228             done = 1;
229             for (j = 0; j < i; j++)
230             {
231                if (histogram[png_ptr->quantize_sort[j]]
232                    < histogram[png_ptr->quantize_sort[j + 1]])
233                {
234                   png_byte t;
235
236                   t = png_ptr->quantize_sort[j];
237                   png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
238                   png_ptr->quantize_sort[j + 1] = t;
239                   done = 0;
240                }
241             }
242
243             if (done)
244                break;
245          }
246
247          /* Swap the palette around, and set up a table, if necessary */
248          if (full_quantize)
249          {
250             int j = num_palette;
251
252             /* Put all the useful colors within the max, but don't
253              * move the others.
254              */
255             for (i = 0; i < maximum_colors; i++)
256             {
257                if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
258                {
259                   do
260                      j--;
261                   while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
262
263                   palette[i] = palette[j];
264                }
265             }
266          }
267          else
268          {
269             int j = num_palette;
270
271             /* Move all the used colors inside the max limit, and
272              * develop a translation table.
273              */
274             for (i = 0; i < maximum_colors; i++)
275             {
276                /* Only move the colors we need to */
277                if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
278                {
279                   png_color tmp_color;
280
281                   do
282                      j--;
283                   while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
284
285                   tmp_color = palette[j];
286                   palette[j] = palette[i];
287                   palette[i] = tmp_color;
288                   /* Indicate where the color went */
289                   png_ptr->quantize_index[j] = (png_byte)i;
290                   png_ptr->quantize_index[i] = (png_byte)j;
291                }
292             }
293
294             /* Find closest color for those colors we are not using */
295             for (i = 0; i < num_palette; i++)
296             {
297                if ((int)png_ptr->quantize_index[i] >= maximum_colors)
298                {
299                   int min_d, k, min_k, d_index;
300
301                   /* Find the closest color to one we threw out */
302                   d_index = png_ptr->quantize_index[i];
303                   min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
304                   for (k = 1, min_k = 0; k < maximum_colors; k++)
305                   {
306                      int d;
307
308                      d = PNG_COLOR_DIST(palette[d_index], palette[k]);
309
310                      if (d < min_d)
311                      {
312                         min_d = d;
313                         min_k = k;
314                      }
315                   }
316                   /* Point to closest color */
317                   png_ptr->quantize_index[i] = (png_byte)min_k;
318                }
319             }
320          }
321          png_free(png_ptr, png_ptr->quantize_sort);
322          png_ptr->quantize_sort = NULL;
323       }
324       else
325       {
326          /* This is much harder to do simply (and quickly).  Perhaps
327           * we need to go through a median cut routine, but those
328           * don't always behave themselves with only a few colors
329           * as input.  So we will just find the closest two colors,
330           * and throw out one of them (chosen somewhat randomly).
331           * [We don't understand this at all, so if someone wants to
332           *  work on improving it, be our guest - AED, GRP]
333           */
334          int i;
335          int max_d;
336          int num_new_palette;
337          png_dsortp t;
338          png_dsortpp hash;
339
340          t = NULL;
341
342          /* Initialize palette index arrays */
343          png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
344              (png_uint_32)(num_palette * png_sizeof(png_byte)));
345          png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
346              (png_uint_32)(num_palette * png_sizeof(png_byte)));
347
348          /* Initialize the sort array */
349          for (i = 0; i < num_palette; i++)
350          {
351             png_ptr->index_to_palette[i] = (png_byte)i;
352             png_ptr->palette_to_index[i] = (png_byte)i;
353          }
354
355          hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
356              png_sizeof(png_dsortp)));
357
358          num_new_palette = num_palette;
359
360          /* Initial wild guess at how far apart the farthest pixel
361           * pair we will be eliminating will be.  Larger
362           * numbers mean more areas will be allocated, Smaller
363           * numbers run the risk of not saving enough data, and
364           * having to do this all over again.
365           *
366           * I have not done extensive checking on this number.
367           */
368          max_d = 96;
369
370          while (num_new_palette > maximum_colors)
371          {
372             for (i = 0; i < num_new_palette - 1; i++)
373             {
374                int j;
375
376                for (j = i + 1; j < num_new_palette; j++)
377                {
378                   int d;
379
380                   d = PNG_COLOR_DIST(palette[i], palette[j]);
381
382                   if (d <= max_d)
383                   {
384
385                      t = (png_dsortp)png_malloc_warn(png_ptr,
386                          (png_uint_32)(png_sizeof(png_dsort)));
387
388                      if (t == NULL)
389                          break;
390
391                      t->next = hash[d];
392                      t->left = (png_byte)i;
393                      t->right = (png_byte)j;
394                      hash[d] = t;
395                   }
396                }
397                if (t == NULL)
398                   break;
399             }
400
401             if (t != NULL)
402             for (i = 0; i <= max_d; i++)
403             {
404                if (hash[i] != NULL)
405                {
406                   png_dsortp p;
407
408                   for (p = hash[i]; p; p = p->next)
409                   {
410                      if ((int)png_ptr->index_to_palette[p->left]
411                          < num_new_palette &&
412                          (int)png_ptr->index_to_palette[p->right]
413                          < num_new_palette)
414                      {
415                         int j, next_j;
416
417                         if (num_new_palette & 0x01)
418                         {
419                            j = p->left;
420                            next_j = p->right;
421                         }
422                         else
423                         {
424                            j = p->right;
425                            next_j = p->left;
426                         }
427
428                         num_new_palette--;
429                         palette[png_ptr->index_to_palette[j]]
430                             = palette[num_new_palette];
431                         if (!full_quantize)
432                         {
433                            int k;
434
435                            for (k = 0; k < num_palette; k++)
436                            {
437                               if (png_ptr->quantize_index[k] ==
438                                   png_ptr->index_to_palette[j])
439                                  png_ptr->quantize_index[k] =
440                                      png_ptr->index_to_palette[next_j];
441
442                               if ((int)png_ptr->quantize_index[k] ==
443                                   num_new_palette)
444                                  png_ptr->quantize_index[k] =
445                                      png_ptr->index_to_palette[j];
446                            }
447                         }
448
449                         png_ptr->index_to_palette[png_ptr->palette_to_index
450                             [num_new_palette]] = png_ptr->index_to_palette[j];
451
452                         png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
453                             = png_ptr->palette_to_index[num_new_palette];
454
455                         png_ptr->index_to_palette[j] =
456                             (png_byte)num_new_palette;
457
458                         png_ptr->palette_to_index[num_new_palette] =
459                             (png_byte)j;
460                      }
461                      if (num_new_palette <= maximum_colors)
462                         break;
463                   }
464                   if (num_new_palette <= maximum_colors)
465                      break;
466                }
467             }
468
469             for (i = 0; i < 769; i++)
470             {
471                if (hash[i] != NULL)
472                {
473                   png_dsortp p = hash[i];
474                   while (p)
475                   {
476                      t = p->next;
477                      png_free(png_ptr, p);
478                      p = t;
479                   }
480                }
481                hash[i] = 0;
482             }
483             max_d += 96;
484          }
485          png_free(png_ptr, hash);
486          png_free(png_ptr, png_ptr->palette_to_index);
487          png_free(png_ptr, png_ptr->index_to_palette);
488          png_ptr->palette_to_index = NULL;
489          png_ptr->index_to_palette = NULL;
490       }
491       num_palette = maximum_colors;
492    }
493    if (png_ptr->palette == NULL)
494    {
495       png_ptr->palette = palette;
496    }
497    png_ptr->num_palette = (png_uint_16)num_palette;
498
499    if (full_quantize)
500    {
501       int i;
502       png_bytep distance;
503       int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
504           PNG_QUANTIZE_BLUE_BITS;
505       int num_red = (1 << PNG_QUANTIZE_RED_BITS);
506       int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
507       int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
508       png_size_t num_entries = ((png_size_t)1 << total_bits);
509
510       png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
511           (png_uint_32)(num_entries * png_sizeof(png_byte)));
512
513       distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
514           png_sizeof(png_byte)));
515
516       png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
517
518       for (i = 0; i < num_palette; i++)
519       {
520          int ir, ig, ib;
521          int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
522          int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
523          int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
524
525          for (ir = 0; ir < num_red; ir++)
526          {
527             /* int dr = abs(ir - r); */
528             int dr = ((ir > r) ? ir - r : r - ir);
529             int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
530                 PNG_QUANTIZE_GREEN_BITS));
531
532             for (ig = 0; ig < num_green; ig++)
533             {
534                /* int dg = abs(ig - g); */
535                int dg = ((ig > g) ? ig - g : g - ig);
536                int dt = dr + dg;
537                int dm = ((dr > dg) ? dr : dg);
538                int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
539
540                for (ib = 0; ib < num_blue; ib++)
541                {
542                   int d_index = index_g | ib;
543                   /* int db = abs(ib - b); */
544                   int db = ((ib > b) ? ib - b : b - ib);
545                   int dmax = ((dm > db) ? dm : db);
546                   int d = dmax + dt + db;
547
548                   if (d < (int)distance[d_index])
549                   {
550                      distance[d_index] = (png_byte)d;
551                      png_ptr->palette_lookup[d_index] = (png_byte)i;
552                   }
553                }
554             }
555          }
556       }
557
558       png_free(png_ptr, distance);
559    }
560 }
561 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
562
563 #ifdef PNG_READ_GAMMA_SUPPORTED
564 /* Transform the image from the file_gamma to the screen_gamma.  We
565  * only do transformations on images where the file_gamma and screen_gamma
566  * are not close reciprocals, otherwise it slows things down slightly, and
567  * also needlessly introduces small errors.
568  *
569  * We will turn off gamma transformation later if no semitransparent entries
570  * are present in the tRNS array for palette images.  We can't do it here
571  * because we don't necessarily have the tRNS chunk yet.
572  */
573 static int /* PRIVATE */
574 png_gamma_threshold(png_fixed_point scrn_gamma, png_fixed_point file_gamma)
575 {
576    /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
577     * correction as a difference of the overall transform from 1.0
578     *
579     * We want to compare the threshold with s*f - 1, if we get
580     * overflow here it is because of wacky gamma values so we
581     * turn on processing anyway.
582     */
583    png_fixed_point gtest;
584    return !png_muldiv(&gtest, scrn_gamma, file_gamma, PNG_FP_1) ||
585        png_gamma_significant(gtest);
586 }
587
588 void PNGFAPI
589 png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
590    png_fixed_point file_gamma)
591 {
592    png_debug(1, "in png_set_gamma_fixed");
593
594    if (png_ptr == NULL)
595       return;
596
597    if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
598        (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
599        png_gamma_threshold(scrn_gamma, file_gamma))
600       png_ptr->transformations |= PNG_GAMMA;
601    png_ptr->gamma = file_gamma;
602    png_ptr->screen_gamma = scrn_gamma;
603 }
604
605 #  ifdef PNG_FLOATING_POINT_SUPPORTED
606 void PNGAPI
607 png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
608 {
609    png_set_gamma_fixed(png_ptr,
610       png_fixed(png_ptr, scrn_gamma, "png_set_gamma screen gamma"),
611       png_fixed(png_ptr, file_gamma, "png_set_gamma file gamma"));
612 }
613 #  endif /* FLOATING_POINT_SUPPORTED */
614 #endif /* READ_GAMMA */
615
616 #ifdef PNG_READ_EXPAND_SUPPORTED
617 /* Expand paletted images to RGB, expand grayscale images of
618  * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
619  * to alpha channels.
620  */
621 void PNGAPI
622 png_set_expand(png_structp png_ptr)
623 {
624    png_debug(1, "in png_set_expand");
625
626    if (png_ptr == NULL)
627       return;
628
629    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
630    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
631 }
632
633 /* GRR 19990627:  the following three functions currently are identical
634  *  to png_set_expand().  However, it is entirely reasonable that someone
635  *  might wish to expand an indexed image to RGB but *not* expand a single,
636  *  fully transparent palette entry to a full alpha channel--perhaps instead
637  *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
638  *  the transparent color with a particular RGB value, or drop tRNS entirely.
639  *  IOW, a future version of the library may make the transformations flag
640  *  a bit more fine-grained, with separate bits for each of these three
641  *  functions.
642  *
643  *  More to the point, these functions make it obvious what libpng will be
644  *  doing, whereas "expand" can (and does) mean any number of things.
645  *
646  *  GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
647  *  to expand only the sample depth but not to expand the tRNS to alpha
648  *  and its name was changed to png_set_expand_gray_1_2_4_to_8().
649  */
650
651 /* Expand paletted images to RGB. */
652 void PNGAPI
653 png_set_palette_to_rgb(png_structp png_ptr)
654 {
655    png_debug(1, "in png_set_palette_to_rgb");
656
657    if (png_ptr == NULL)
658       return;
659
660    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
661    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
662 }
663
664 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
665 void PNGAPI
666 png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
667 {
668    png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
669
670    if (png_ptr == NULL)
671       return;
672
673    png_ptr->transformations |= PNG_EXPAND;
674    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
675 }
676
677
678
679 /* Expand tRNS chunks to alpha channels. */
680 void PNGAPI
681 png_set_tRNS_to_alpha(png_structp png_ptr)
682 {
683    png_debug(1, "in png_set_tRNS_to_alpha");
684
685    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
686    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
687 }
688 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
689
690 #ifdef PNG_READ_EXPAND_16_SUPPORTED
691 /* Expand to 16 bit channels, expand the tRNS chunk too (because otherwise
692  * it may not work correctly.)
693  */
694 void PNGAPI
695 png_set_expand_16(png_structp png_ptr)
696 {
697    png_debug(1, "in png_set_expand_16");
698
699    if (png_ptr == NULL)
700       return;
701
702    png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
703    png_ptr->transformations &= ~PNG_16_TO_8;
704
705    png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
706 }
707 #endif
708
709 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
710 void PNGAPI
711 png_set_gray_to_rgb(png_structp png_ptr)
712 {
713    png_debug(1, "in png_set_gray_to_rgb");
714
715    if (png_ptr != NULL)
716    {
717       /* Because rgb must be 8 bits or more: */
718       png_set_expand_gray_1_2_4_to_8(png_ptr);
719       png_ptr->transformations |= PNG_GRAY_TO_RGB;
720       png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
721    }
722 }
723 #endif
724
725 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
726 void PNGFAPI
727 png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
728     png_fixed_point red, png_fixed_point green)
729 {
730    png_debug(1, "in png_set_rgb_to_gray");
731
732    if (png_ptr == NULL)
733       return;
734
735    switch(error_action)
736    {
737       case 1:
738          png_ptr->transformations |= PNG_RGB_TO_GRAY;
739          break;
740
741       case 2:
742          png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
743          break;
744
745       case 3:
746          png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
747          break;
748
749       default:
750          png_error(png_ptr, "invalid error action to rgb_to_gray");
751          break;
752    }
753    if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
754 #ifdef PNG_READ_EXPAND_SUPPORTED
755       png_ptr->transformations |= PNG_EXPAND;
756 #else
757    {
758       png_warning(png_ptr,
759         "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
760
761       png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
762    }
763 #endif
764    {
765       png_uint_16 red_int, green_int;
766       if (red < 0 || green < 0)
767       {
768          red_int   =  6968; /* .212671 * 32768 + .5 */
769          green_int = 23434; /* .715160 * 32768 + .5 */
770       }
771
772       else if (red + green < 100000L)
773       {
774          red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
775          green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
776       }
777
778       else
779       {
780          png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
781          red_int   =  6968;
782          green_int = 23434;
783       }
784
785       png_ptr->rgb_to_gray_red_coeff   = red_int;
786       png_ptr->rgb_to_gray_green_coeff = green_int;
787       png_ptr->rgb_to_gray_blue_coeff  =
788           (png_uint_16)(32768 - red_int - green_int);
789    }
790 }
791
792 #ifdef PNG_FLOATING_POINT_SUPPORTED
793 /* Convert a RGB image to a grayscale of the same width.  This allows us,
794  * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
795  */
796
797 void PNGAPI
798 png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
799    double green)
800 {
801    if (png_ptr == NULL)
802       return;
803
804    png_set_rgb_to_gray_fixed(png_ptr, error_action,
805       png_fixed(png_ptr, red, "rgb to gray red coefficient"),
806       png_fixed(png_ptr, green, "rgb to gray green coefficient"));
807 }
808 #endif /* FLOATING POINT */
809
810 #endif
811
812 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
813     defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
814 void PNGAPI
815 png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
816     read_user_transform_fn)
817 {
818    png_debug(1, "in png_set_read_user_transform_fn");
819
820    if (png_ptr == NULL)
821       return;
822
823 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
824    png_ptr->transformations |= PNG_USER_TRANSFORM;
825    png_ptr->read_user_transform_fn = read_user_transform_fn;
826 #endif
827 }
828 #endif
829
830 /* Initialize everything needed for the read.  This includes modifying
831  * the palette.
832  */
833 void /* PRIVATE */
834 png_init_read_transformations(png_structp png_ptr)
835 {
836    png_debug(1, "in png_init_read_transformations");
837
838   {
839 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
840     defined(PNG_READ_SHIFT_SUPPORTED) || \
841     defined(PNG_READ_GAMMA_SUPPORTED)
842    int color_type = png_ptr->color_type;
843 #endif
844
845 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
846
847 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
848    /* Detect gray background and attempt to enable optimization
849     * for gray --> RGB case
850     *
851     * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
852     * RGB_ALPHA (in which case need_expand is superfluous anyway), the
853     * background color might actually be gray yet not be flagged as such.
854     * This is not a problem for the current code, which uses
855     * PNG_BACKGROUND_IS_GRAY only to decide when to do the
856     * png_do_gray_to_rgb() transformation.
857     */
858    if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
859        !(color_type & PNG_COLOR_MASK_COLOR))
860    {
861       png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
862    }
863
864    else if ((png_ptr->transformations & PNG_BACKGROUND) &&
865        !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
866        (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
867        png_ptr->background.red == png_ptr->background.green &&
868        png_ptr->background.red == png_ptr->background.blue)
869    {
870       png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
871       png_ptr->background.gray = png_ptr->background.red;
872    }
873 #endif
874
875    if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
876        (png_ptr->transformations & PNG_EXPAND))
877    {
878       if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
879       {
880          /* Expand background and tRNS chunks */
881          switch (png_ptr->bit_depth)
882          {
883             case 1:
884                png_ptr->background.gray *= (png_uint_16)0xff;
885                png_ptr->background.red = png_ptr->background.green
886                    =  png_ptr->background.blue = png_ptr->background.gray;
887                if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
888                {
889                  png_ptr->trans_color.gray *= (png_uint_16)0xff;
890                  png_ptr->trans_color.red = png_ptr->trans_color.green
891                      = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
892                }
893                break;
894
895             case 2:
896                png_ptr->background.gray *= (png_uint_16)0x55;
897                png_ptr->background.red = png_ptr->background.green
898                    = png_ptr->background.blue = png_ptr->background.gray;
899                if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
900                {
901                   png_ptr->trans_color.gray *= (png_uint_16)0x55;
902                   png_ptr->trans_color.red = png_ptr->trans_color.green
903                       = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
904                }
905                break;
906
907             case 4:
908                png_ptr->background.gray *= (png_uint_16)0x11;
909                png_ptr->background.red = png_ptr->background.green
910                    = png_ptr->background.blue = png_ptr->background.gray;
911                if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
912                {
913                   png_ptr->trans_color.gray *= (png_uint_16)0x11;
914                   png_ptr->trans_color.red = png_ptr->trans_color.green
915                       = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
916                }
917                break;
918
919             default:
920
921             case 8:
922
923             case 16:
924                png_ptr->background.red = png_ptr->background.green
925                    = png_ptr->background.blue = png_ptr->background.gray;
926                break;
927          }
928       }
929       else if (color_type == PNG_COLOR_TYPE_PALETTE)
930       {
931          png_ptr->background.red   =
932              png_ptr->palette[png_ptr->background.index].red;
933          png_ptr->background.green =
934              png_ptr->palette[png_ptr->background.index].green;
935          png_ptr->background.blue  =
936              png_ptr->palette[png_ptr->background.index].blue;
937
938 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
939         if (png_ptr->transformations & PNG_INVERT_ALPHA)
940         {
941 #ifdef PNG_READ_EXPAND_SUPPORTED
942            if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
943 #endif
944            {
945               /* Invert the alpha channel (in tRNS) unless the pixels are
946                * going to be expanded, in which case leave it for later
947                */
948               int i, istop;
949               istop=(int)png_ptr->num_trans;
950               for (i=0; i<istop; i++)
951                  png_ptr->trans_alpha[i] = (png_byte)(255 -
952                     png_ptr->trans_alpha[i]);
953            }
954         }
955 #endif
956
957       }
958    }
959 #endif
960
961 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
962    png_ptr->background_1 = png_ptr->background;
963 #endif
964 #ifdef PNG_READ_GAMMA_SUPPORTED
965
966    if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
967        && png_gamma_threshold(png_ptr->screen_gamma, png_ptr->gamma))
968    {
969       int i, k;
970       k=0;
971       for (i=0; i<png_ptr->num_trans; i++)
972       {
973         if (png_ptr->trans_alpha[i] != 0 && png_ptr->trans_alpha[i] != 0xff)
974            k=1; /* Partial transparency is present */
975       }
976       if (k == 0)
977          png_ptr->transformations &= ~PNG_GAMMA;
978    }
979
980    if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
981        png_ptr->gamma != 0)
982    {
983       png_build_gamma_table(png_ptr, png_ptr->bit_depth);
984
985 #ifdef PNG_READ_BACKGROUND_SUPPORTED
986       if (png_ptr->transformations & PNG_BACKGROUND)
987       {
988          if (color_type == PNG_COLOR_TYPE_PALETTE)
989          {
990             /* Could skip if no transparency */
991             png_color back, back_1;
992             png_colorp palette = png_ptr->palette;
993             int num_palette = png_ptr->num_palette;
994             int i;
995             if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
996             {
997
998                back.red = png_ptr->gamma_table[png_ptr->background.red];
999                back.green = png_ptr->gamma_table[png_ptr->background.green];
1000                back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1001
1002                back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1003                back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1004                back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1005             }
1006             else
1007             {
1008                png_fixed_point g, gs;
1009
1010                switch (png_ptr->background_gamma_type)
1011                {
1012                   case PNG_BACKGROUND_GAMMA_SCREEN:
1013                      g = (png_ptr->screen_gamma);
1014                      gs = PNG_FP_1;
1015                      break;
1016
1017                   case PNG_BACKGROUND_GAMMA_FILE:
1018                      g = png_reciprocal(png_ptr->gamma);
1019                      gs = png_reciprocal2(png_ptr->gamma,
1020                         png_ptr->screen_gamma);
1021                      break;
1022
1023                   case PNG_BACKGROUND_GAMMA_UNIQUE:
1024                      g = png_reciprocal(png_ptr->background_gamma);
1025                      gs = png_reciprocal2(png_ptr->background_gamma,
1026                         png_ptr->screen_gamma);
1027                      break;
1028                   default:
1029                      g = PNG_FP_1;    /* back_1 */
1030                      gs = PNG_FP_1;   /* back */
1031                      break;
1032                }
1033
1034                if (png_gamma_significant(gs))
1035                {
1036                   back.red   = (png_byte)png_ptr->background.red;
1037                   back.green = (png_byte)png_ptr->background.green;
1038                   back.blue  = (png_byte)png_ptr->background.blue;
1039                }
1040
1041                else
1042                {
1043                   back.red = png_gamma_8bit_correct(png_ptr->background.red,
1044                       gs);
1045                   back.green = png_gamma_8bit_correct(png_ptr->background.green,
1046                       gs);
1047                   back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1048                       gs);
1049                }
1050                back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g);
1051                back_1.green = png_gamma_8bit_correct(png_ptr->background.green,
1052                    g);
1053                back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1054                    g);
1055             }
1056             for (i = 0; i < num_palette; i++)
1057             {
1058                if (i < (int)png_ptr->num_trans &&
1059                    png_ptr->trans_alpha[i] != 0xff)
1060                {
1061                   if (png_ptr->trans_alpha[i] == 0)
1062                   {
1063                      palette[i] = back;
1064                   }
1065                   else /* if (png_ptr->trans_alpha[i] != 0xff) */
1066                   {
1067                      png_byte v, w;
1068
1069                      v = png_ptr->gamma_to_1[palette[i].red];
1070                      png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1071                      palette[i].red = png_ptr->gamma_from_1[w];
1072
1073                      v = png_ptr->gamma_to_1[palette[i].green];
1074                      png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1075                      palette[i].green = png_ptr->gamma_from_1[w];
1076
1077                      v = png_ptr->gamma_to_1[palette[i].blue];
1078                      png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1079                      palette[i].blue = png_ptr->gamma_from_1[w];
1080                   }
1081                }
1082                else
1083                {
1084                   palette[i].red = png_ptr->gamma_table[palette[i].red];
1085                   palette[i].green = png_ptr->gamma_table[palette[i].green];
1086                   palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1087                }
1088             }
1089             /* Prevent the transformations being done again, and make sure
1090              * that the now spurious alpha channel is stripped - the code
1091              * has just reduced background composition and gamma correction
1092              * to a simple alpha channel strip.
1093              */
1094             png_ptr->transformations &= ~PNG_BACKGROUND;
1095             png_ptr->transformations &= ~PNG_GAMMA;
1096             png_ptr->transformations |= PNG_STRIP_ALPHA;
1097          }
1098
1099          /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1100          else
1101          /* color_type != PNG_COLOR_TYPE_PALETTE */
1102          {
1103             png_fixed_point g = PNG_FP_1;
1104             png_fixed_point gs = PNG_FP_1;
1105
1106             switch (png_ptr->background_gamma_type)
1107             {
1108                case PNG_BACKGROUND_GAMMA_SCREEN:
1109                   g = png_ptr->screen_gamma;
1110                   /* gs = PNG_FP_1; */
1111                   break;
1112
1113                case PNG_BACKGROUND_GAMMA_FILE:
1114                   g = png_reciprocal(png_ptr->gamma);
1115                   gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
1116                   break;
1117
1118                case PNG_BACKGROUND_GAMMA_UNIQUE:
1119                   g = png_reciprocal(png_ptr->background_gamma);
1120                   gs = png_reciprocal2(png_ptr->background_gamma,
1121                       png_ptr->screen_gamma);
1122                   break;
1123
1124                default:
1125                   png_error(png_ptr, "invalid background gamma type");
1126             }
1127
1128             png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1129                 png_ptr->background.gray, g);
1130
1131             png_ptr->background.gray = png_gamma_correct(png_ptr,
1132                 png_ptr->background.gray, gs);
1133
1134             if ((png_ptr->background.red != png_ptr->background.green) ||
1135                 (png_ptr->background.red != png_ptr->background.blue) ||
1136                 (png_ptr->background.red != png_ptr->background.gray))
1137             {
1138                /* RGB or RGBA with color background */
1139                png_ptr->background_1.red = png_gamma_correct(png_ptr,
1140                    png_ptr->background.red, g);
1141
1142                png_ptr->background_1.green = png_gamma_correct(png_ptr,
1143                    png_ptr->background.green, g);
1144
1145                png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1146                    png_ptr->background.blue, g);
1147
1148                png_ptr->background.red = png_gamma_correct(png_ptr,
1149                    png_ptr->background.red, gs);
1150
1151                png_ptr->background.green = png_gamma_correct(png_ptr,
1152                    png_ptr->background.green, gs);
1153
1154                png_ptr->background.blue = png_gamma_correct(png_ptr,
1155                    png_ptr->background.blue, gs);
1156             }
1157
1158             else
1159             {
1160                /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1161                png_ptr->background_1.red = png_ptr->background_1.green
1162                    = png_ptr->background_1.blue = png_ptr->background_1.gray;
1163
1164                png_ptr->background.red = png_ptr->background.green
1165                    = png_ptr->background.blue = png_ptr->background.gray;
1166             }
1167          }
1168       }
1169       else
1170       /* Transformation does not include PNG_BACKGROUND */
1171 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1172       if (color_type == PNG_COLOR_TYPE_PALETTE)
1173       {
1174          png_colorp palette = png_ptr->palette;
1175          int num_palette = png_ptr->num_palette;
1176          int i;
1177
1178          for (i = 0; i < num_palette; i++)
1179          {
1180             palette[i].red = png_ptr->gamma_table[palette[i].red];
1181             palette[i].green = png_ptr->gamma_table[palette[i].green];
1182             palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1183          }
1184
1185          /* Done the gamma correction. */
1186          png_ptr->transformations &= ~PNG_GAMMA;
1187       }
1188    }
1189 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1190    else
1191 #endif
1192 #endif /* PNG_READ_GAMMA_SUPPORTED */
1193 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1194    /* No GAMMA transformation */
1195    if ((png_ptr->transformations & PNG_BACKGROUND) &&
1196        (color_type == PNG_COLOR_TYPE_PALETTE))
1197    {
1198       int i;
1199       int istop = (int)png_ptr->num_trans;
1200       png_color back;
1201       png_colorp palette = png_ptr->palette;
1202
1203       back.red   = (png_byte)png_ptr->background.red;
1204       back.green = (png_byte)png_ptr->background.green;
1205       back.blue  = (png_byte)png_ptr->background.blue;
1206
1207       for (i = 0; i < istop; i++)
1208       {
1209          if (png_ptr->trans_alpha[i] == 0)
1210          {
1211             palette[i] = back;
1212          }
1213
1214          else if (png_ptr->trans_alpha[i] != 0xff)
1215          {
1216             /* The png_composite() macro is defined in png.h */
1217             png_composite(palette[i].red, palette[i].red,
1218                 png_ptr->trans_alpha[i], back.red);
1219
1220             png_composite(palette[i].green, palette[i].green,
1221                 png_ptr->trans_alpha[i], back.green);
1222
1223             png_composite(palette[i].blue, palette[i].blue,
1224                 png_ptr->trans_alpha[i], back.blue);
1225          }
1226       }
1227
1228       /* Handled alpha, still need to strip the channel. */
1229       png_ptr->transformations &= ~PNG_BACKGROUND;
1230       png_ptr->transformations |= PNG_STRIP_ALPHA;
1231    }
1232 #endif /* PNG_READ_BACKGROUND_SUPPORTED */
1233
1234 #ifdef PNG_READ_SHIFT_SUPPORTED
1235    if ((png_ptr->transformations & PNG_SHIFT) &&
1236        (color_type == PNG_COLOR_TYPE_PALETTE))
1237    {
1238       png_uint_16 i;
1239       png_uint_16 istop = png_ptr->num_palette;
1240       int sr = 8 - png_ptr->sig_bit.red;
1241       int sg = 8 - png_ptr->sig_bit.green;
1242       int sb = 8 - png_ptr->sig_bit.blue;
1243
1244       if (sr < 0 || sr > 8)
1245          sr = 0;
1246
1247       if (sg < 0 || sg > 8)
1248          sg = 0;
1249
1250       if (sb < 0 || sb > 8)
1251          sb = 0;
1252
1253       for (i = 0; i < istop; i++)
1254       {
1255          png_ptr->palette[i].red >>= sr;
1256          png_ptr->palette[i].green >>= sg;
1257          png_ptr->palette[i].blue >>= sb;
1258       }
1259    }
1260 #endif  /* PNG_READ_SHIFT_SUPPORTED */
1261  }
1262 #if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
1263  && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1264    if (png_ptr)
1265       return;
1266 #endif
1267 }
1268
1269 /* Modify the info structure to reflect the transformations.  The
1270  * info should be updated so a PNG file could be written with it,
1271  * assuming the transformations result in valid PNG data.
1272  */
1273 void /* PRIVATE */
1274 png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1275 {
1276    png_debug(1, "in png_read_transform_info");
1277
1278 #ifdef PNG_READ_EXPAND_SUPPORTED
1279    if (png_ptr->transformations & PNG_EXPAND)
1280    {
1281       if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1282       {
1283          if (png_ptr->num_trans &&
1284              (png_ptr->transformations & PNG_EXPAND_tRNS))
1285             info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1286
1287          else
1288             info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1289
1290          info_ptr->bit_depth = 8;
1291          info_ptr->num_trans = 0;
1292       }
1293       else
1294       {
1295          if (png_ptr->num_trans)
1296          {
1297             if (png_ptr->transformations & PNG_EXPAND_tRNS)
1298                info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1299          }
1300          if (info_ptr->bit_depth < 8)
1301             info_ptr->bit_depth = 8;
1302
1303          info_ptr->num_trans = 0;
1304       }
1305    }
1306 #endif
1307
1308 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1309    if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
1310       info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
1311    {
1312       info_ptr->bit_depth = 16;
1313    }
1314 #endif
1315
1316 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1317    if (png_ptr->transformations & PNG_BACKGROUND)
1318    {
1319       info_ptr->color_type = (png_byte)(info_ptr->color_type &
1320           ~PNG_COLOR_MASK_ALPHA);
1321       info_ptr->num_trans = 0;
1322       info_ptr->background = png_ptr->background;
1323    }
1324 #endif
1325
1326 #ifdef PNG_READ_GAMMA_SUPPORTED
1327    if (png_ptr->transformations & PNG_GAMMA)
1328    {
1329       info_ptr->gamma = png_ptr->gamma;
1330    }
1331 #endif
1332
1333 #ifdef PNG_READ_16_TO_8_SUPPORTED
1334 #ifdef PNG_READ_16BIT_SUPPORTED
1335    if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1336       info_ptr->bit_depth = 8;
1337 #else
1338    /* Force chopping 16-bit input down to 8 */
1339    if (info_ptr->bit_depth == 16)
1340    {
1341       png_ptr->transformations |=PNG_16_TO_8;
1342       info_ptr->bit_depth = 8;
1343    }
1344 #endif
1345 #endif
1346
1347 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1348    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
1349       info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1350 #endif
1351
1352 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1353    if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1354       info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
1355 #endif
1356
1357 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1358    if (png_ptr->transformations & PNG_QUANTIZE)
1359    {
1360       if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1361           (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
1362           png_ptr->palette_lookup && info_ptr->bit_depth == 8)
1363       {
1364          info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
1365       }
1366    }
1367 #endif
1368
1369 #ifdef PNG_READ_PACK_SUPPORTED
1370    if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1371       info_ptr->bit_depth = 8;
1372 #endif
1373
1374    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1375       info_ptr->channels = 1;
1376
1377    else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
1378       info_ptr->channels = 3;
1379
1380    else
1381       info_ptr->channels = 1;
1382
1383 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1384    if (png_ptr->transformations & PNG_STRIP_ALPHA)
1385       info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
1386 #endif
1387
1388    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
1389       info_ptr->channels++;
1390
1391 #ifdef PNG_READ_FILLER_SUPPORTED
1392    /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
1393    if ((png_ptr->transformations & PNG_FILLER) &&
1394        ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
1395        (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1396    {
1397       info_ptr->channels++;
1398       /* If adding a true alpha channel not just filler */
1399       if (png_ptr->transformations & PNG_ADD_ALPHA)
1400          info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1401    }
1402 #endif
1403
1404 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
1405 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1406    if (png_ptr->transformations & PNG_USER_TRANSFORM)
1407    {
1408       if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1409          info_ptr->bit_depth = png_ptr->user_transform_depth;
1410
1411       if (info_ptr->channels < png_ptr->user_transform_channels)
1412          info_ptr->channels = png_ptr->user_transform_channels;
1413    }
1414 #endif
1415
1416    info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
1417        info_ptr->bit_depth);
1418
1419    info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1420
1421 #ifndef PNG_READ_EXPAND_SUPPORTED
1422    if (png_ptr)
1423       return;
1424 #endif
1425 }
1426
1427 /* Transform the row.  The order of transformations is significant,
1428  * and is very touchy.  If you add a transformation, take care to
1429  * decide how it fits in with the other transformations here.
1430  */
1431 void /* PRIVATE */
1432 png_do_read_transformations(png_structp png_ptr)
1433 {
1434    png_debug(1, "in png_do_read_transformations");
1435
1436    if (png_ptr->row_buf == NULL)
1437    {
1438 #ifdef PNG_CONSOLE_IO_SUPPORTED
1439       char msg[50];
1440
1441       png_snprintf2(msg, 50,
1442           "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
1443           png_ptr->pass);
1444       png_error(png_ptr, msg);
1445 #else
1446       png_error(png_ptr, "NULL row buffer");
1447 #endif
1448    }
1449 #ifdef PNG_WARN_UNINITIALIZED_ROW
1450    if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
1451       /* Application has failed to call either png_read_start_image()
1452        * or png_read_update_info() after setting transforms that expand
1453        * pixels.  This check added to libpng-1.2.19
1454        */
1455 #if (PNG_WARN_UNINITIALIZED_ROW==1)
1456       png_error(png_ptr, "Uninitialized row");
1457 #else
1458       png_warning(png_ptr, "Uninitialized row");
1459 #endif
1460 #endif
1461
1462 #ifdef PNG_READ_EXPAND_SUPPORTED
1463    if (png_ptr->transformations & PNG_EXPAND)
1464    {
1465       if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
1466       {
1467          png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
1468              png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
1469       }
1470
1471       else
1472       {
1473          if (png_ptr->num_trans &&
1474              (png_ptr->transformations & PNG_EXPAND_tRNS))
1475             png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1476                 &(png_ptr->trans_color));
1477
1478          else
1479             png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
1480                 NULL);
1481       }
1482    }
1483 #endif
1484
1485    /* Delay the 'expand 16' step until later for efficiency, so that the
1486     * intermediate steps work with 8 bit data.
1487     */
1488
1489 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1490    if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
1491       (png_ptr->row_info.color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
1492       png_ptr->row_info.color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
1493       png_do_strip_channel(&(png_ptr->row_info), png_ptr->row_buf + 1,
1494          0/*!at_start, because SWAP_ALPHA happens later*/);
1495 #endif
1496
1497 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1498    if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1499    {
1500       int rgb_error =
1501           png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
1502               png_ptr->row_buf + 1);
1503
1504       if (rgb_error)
1505       {
1506          png_ptr->rgb_to_gray_status=1;
1507          if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1508              PNG_RGB_TO_GRAY_WARN)
1509             png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1510
1511          if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
1512              PNG_RGB_TO_GRAY_ERR)
1513             png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1514       }
1515    }
1516 #endif
1517
1518 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
1519  *
1520  *   In most cases, the "simple transparency" should be done prior to doing
1521  *   gray-to-RGB, or you will have to test 3x as many bytes to check if a
1522  *   pixel is transparent.  You would also need to make sure that the
1523  *   transparency information is upgraded to RGB.
1524  *
1525  *   To summarize, the current flow is:
1526  *   - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
1527  *                                   with background "in place" if transparent,
1528  *                                   convert to RGB if necessary
1529  *   - Gray + alpha -> composite with gray background and remove alpha bytes,
1530  *                                   convert to RGB if necessary
1531  *
1532  *   To support RGB backgrounds for gray images we need:
1533  *   - Gray + simple transparency -> convert to RGB + simple transparency,
1534  *                                   compare 3 or 6 bytes and composite with
1535  *                                   background "in place" if transparent
1536  *                                   (3x compare/pixel compared to doing
1537  *                                   composite with gray bkgrnd)
1538  *   - Gray + alpha -> convert to RGB + alpha, composite with background and
1539  *                                   remove alpha bytes (3x float
1540  *                                   operations/pixel compared with composite
1541  *                                   on gray background)
1542  *
1543  *  Greg's change will do this.  The reason it wasn't done before is for
1544  *  performance, as this increases the per-pixel operations.  If we would check
1545  *  in advance if the background was gray or RGB, and position the gray-to-RGB
1546  *  transform appropriately, then it would save a lot of work/time.
1547  */
1548
1549 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1550    /* If gray -> RGB, do so now only if background is non-gray; else do later
1551     * for performance reasons
1552     */
1553    if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1554        !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1555       png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1556 #endif
1557
1558 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1559    if ((png_ptr->transformations & PNG_BACKGROUND) &&
1560        ((png_ptr->num_trans != 0) ||
1561        (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
1562       png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1563           &(png_ptr->trans_color), &(png_ptr->background)
1564 #ifdef PNG_READ_GAMMA_SUPPORTED
1565           , &(png_ptr->background_1),
1566           png_ptr->gamma_table, png_ptr->gamma_from_1,
1567           png_ptr->gamma_to_1, png_ptr->gamma_16_table,
1568           png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1569           png_ptr->gamma_shift
1570 #endif
1571           );
1572 #endif
1573
1574 #ifdef PNG_READ_GAMMA_SUPPORTED
1575    if ((png_ptr->transformations & PNG_GAMMA) &&
1576 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1577        !((png_ptr->transformations & PNG_BACKGROUND) &&
1578        ((png_ptr->num_trans != 0) ||
1579        (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
1580 #endif
1581        (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
1582       png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
1583           png_ptr->gamma_table, png_ptr->gamma_16_table,
1584           png_ptr->gamma_shift);
1585 #endif
1586
1587 #ifdef PNG_READ_16_TO_8_SUPPORTED
1588    if (png_ptr->transformations & PNG_16_TO_8)
1589       png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
1590 #endif
1591
1592 #ifdef PNG_READ_QUANTIZE_SUPPORTED
1593    if (png_ptr->transformations & PNG_QUANTIZE)
1594    {
1595       png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
1596           png_ptr->palette_lookup, png_ptr->quantize_index);
1597
1598       if (png_ptr->row_info.rowbytes == 0)
1599          png_error(png_ptr, "png_do_quantize returned rowbytes=0");
1600    }
1601 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
1602
1603 #ifdef PNG_READ_EXPAND_16_SUPPORTED
1604    /* Do the expansion now, after all the arithmetic has been done.  Notice
1605     * that previous transformations can handle the PNG_EXPAND_16 flag if this
1606     * is efficient (particularly true in the case of gamma correction, where
1607     * better accuracy results faster!)
1608     */
1609    if (png_ptr->transformations & PNG_EXPAND_16)
1610       png_do_expand_16(&png_ptr->row_info, png_ptr->row_buf + 1);
1611 #endif
1612
1613 #ifdef PNG_READ_INVERT_SUPPORTED
1614    if (png_ptr->transformations & PNG_INVERT_MONO)
1615       png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
1616 #endif
1617
1618 #ifdef PNG_READ_SHIFT_SUPPORTED
1619    if (png_ptr->transformations & PNG_SHIFT)
1620       png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
1621           &(png_ptr->shift));
1622 #endif
1623
1624 #ifdef PNG_READ_PACK_SUPPORTED
1625    if (png_ptr->transformations & PNG_PACK)
1626       png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
1627 #endif
1628
1629 #ifdef PNG_READ_BGR_SUPPORTED
1630    if (png_ptr->transformations & PNG_BGR)
1631       png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
1632 #endif
1633
1634 #ifdef PNG_READ_PACKSWAP_SUPPORTED
1635    if (png_ptr->transformations & PNG_PACKSWAP)
1636       png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1637 #endif
1638
1639 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1640    /*NOTE: this must be in the wrong place - what happens if BGR is set too?
1641     * Need pngvalid to test this combo.
1642     */
1643    /* If gray -> RGB, do so now only if we did not do so above */
1644    if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1645        (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1646       png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
1647 #endif
1648
1649 #ifdef PNG_READ_FILLER_SUPPORTED
1650    if (png_ptr->transformations & PNG_FILLER)
1651       png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1652           (png_uint_32)png_ptr->filler, png_ptr->flags);
1653 #endif
1654
1655 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1656    if (png_ptr->transformations & PNG_INVERT_ALPHA)
1657       png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1658 #endif
1659
1660 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1661    if (png_ptr->transformations & PNG_SWAP_ALPHA)
1662       png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
1663 #endif
1664
1665 #ifdef PNG_READ_16BIT_SUPPORTED
1666 #ifdef PNG_READ_SWAP_SUPPORTED
1667    if (png_ptr->transformations & PNG_SWAP_BYTES)
1668       png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
1669 #endif
1670 #endif
1671
1672 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1673    if (png_ptr->transformations & PNG_USER_TRANSFORM)
1674     {
1675       if (png_ptr->read_user_transform_fn != NULL)
1676          (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1677              (png_ptr,                    /* png_ptr */
1678              &(png_ptr->row_info),     /* row_info: */
1679                 /*  png_uint_32 width;       width of row */
1680                 /*  png_size_t rowbytes;     number of bytes in row */
1681                 /*  png_byte color_type;     color type of pixels */
1682                 /*  png_byte bit_depth;      bit depth of samples */
1683                 /*  png_byte channels;       number of channels (1-4) */
1684                 /*  png_byte pixel_depth;    bits per pixel (depth*channels) */
1685              png_ptr->row_buf + 1);    /* start of pixel data for row */
1686 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
1687       if (png_ptr->user_transform_depth)
1688          png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1689
1690       if (png_ptr->user_transform_channels)
1691          png_ptr->row_info.channels = png_ptr->user_transform_channels;
1692 #endif
1693       png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
1694           png_ptr->row_info.channels);
1695
1696       png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
1697           png_ptr->row_info.width);
1698    }
1699 #endif
1700
1701 }
1702
1703 #ifdef PNG_READ_PACK_SUPPORTED
1704 /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
1705  * without changing the actual values.  Thus, if you had a row with
1706  * a bit depth of 1, you would end up with bytes that only contained
1707  * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
1708  * png_do_shift() after this.
1709  */
1710 void /* PRIVATE */
1711 png_do_unpack(png_row_infop row_info, png_bytep row)
1712 {
1713    png_debug(1, "in png_do_unpack");
1714
1715    if (row_info->bit_depth < 8)
1716    {
1717       png_uint_32 i;
1718       png_uint_32 row_width=row_info->width;
1719
1720       switch (row_info->bit_depth)
1721       {
1722          case 1:
1723          {
1724             png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
1725             png_bytep dp = row + (png_size_t)row_width - 1;
1726             png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1727             for (i = 0; i < row_width; i++)
1728             {
1729                *dp = (png_byte)((*sp >> shift) & 0x01);
1730
1731                if (shift == 7)
1732                {
1733                   shift = 0;
1734                   sp--;
1735                }
1736
1737                else
1738                   shift++;
1739
1740                dp--;
1741             }
1742             break;
1743          }
1744
1745          case 2:
1746          {
1747
1748             png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
1749             png_bytep dp = row + (png_size_t)row_width - 1;
1750             png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1751             for (i = 0; i < row_width; i++)
1752             {
1753                *dp = (png_byte)((*sp >> shift) & 0x03);
1754
1755                if (shift == 6)
1756                {
1757                   shift = 0;
1758                   sp--;
1759                }
1760
1761                else
1762                   shift += 2;
1763
1764                dp--;
1765             }
1766             break;
1767          }
1768
1769          case 4:
1770          {
1771             png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
1772             png_bytep dp = row + (png_size_t)row_width - 1;
1773             png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1774             for (i = 0; i < row_width; i++)
1775             {
1776                *dp = (png_byte)((*sp >> shift) & 0x0f);
1777
1778                if (shift == 4)
1779                {
1780                   shift = 0;
1781                   sp--;
1782                }
1783
1784                else
1785                   shift = 4;
1786
1787                dp--;
1788             }
1789             break;
1790          }
1791
1792          default:
1793             break;
1794       }
1795       row_info->bit_depth = 8;
1796       row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1797       row_info->rowbytes = row_width * row_info->channels;
1798    }
1799 }
1800 #endif
1801
1802 #ifdef PNG_READ_SHIFT_SUPPORTED
1803 /* Reverse the effects of png_do_shift.  This routine merely shifts the
1804  * pixels back to their significant bits values.  Thus, if you have
1805  * a row of bit depth 8, but only 5 are significant, this will shift
1806  * the values back to 0 through 31.
1807  */
1808 void /* PRIVATE */
1809 png_do_unshift(png_row_infop row_info, png_bytep row,
1810     png_const_color_8p sig_bits)
1811 {
1812    png_debug(1, "in png_do_unshift");
1813
1814    if (
1815        row_info->color_type != PNG_COLOR_TYPE_PALETTE)
1816    {
1817       int shift[4];
1818       int channels = 0;
1819       int c;
1820       png_uint_16 value = 0;
1821       png_uint_32 row_width = row_info->width;
1822
1823       if (row_info->color_type & PNG_COLOR_MASK_COLOR)
1824       {
1825          shift[channels++] = row_info->bit_depth - sig_bits->red;
1826          shift[channels++] = row_info->bit_depth - sig_bits->green;
1827          shift[channels++] = row_info->bit_depth - sig_bits->blue;
1828       }
1829
1830       else
1831       {
1832          shift[channels++] = row_info->bit_depth - sig_bits->gray;
1833       }
1834
1835       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
1836       {
1837          shift[channels++] = row_info->bit_depth - sig_bits->alpha;
1838       }
1839
1840       for (c = 0; c < channels; c++)
1841       {
1842          if (shift[c] <= 0)
1843             shift[c] = 0;
1844
1845          else
1846             value = 1;
1847       }
1848
1849       if (!value)
1850          return;
1851
1852       switch (row_info->bit_depth)
1853       {
1854          default:
1855             break;
1856
1857          case 2:
1858          {
1859             png_bytep bp;
1860             png_size_t i;
1861             png_size_t istop = row_info->rowbytes;
1862
1863             for (bp = row, i = 0; i < istop; i++)
1864             {
1865                *bp >>= 1;
1866                *bp++ &= 0x55;
1867             }
1868             break;
1869          }
1870
1871          case 4:
1872          {
1873             png_bytep bp = row;
1874             png_size_t i;
1875             png_size_t istop = row_info->rowbytes;
1876             png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
1877                 (png_byte)((int)0xf >> shift[0]));
1878
1879             for (i = 0; i < istop; i++)
1880             {
1881                *bp >>= shift[0];
1882                *bp++ &= mask;
1883             }
1884             break;
1885          }
1886
1887          case 8:
1888          {
1889             png_bytep bp = row;
1890             png_uint_32 i;
1891             png_uint_32 istop = row_width * channels;
1892
1893             for (i = 0; i < istop; i++)
1894             {
1895                *bp++ >>= shift[i%channels];
1896             }
1897             break;
1898          }
1899
1900 #ifdef PNG_READ_16BIT_SUPPORTED
1901          case 16:
1902          {
1903             png_bytep bp = row;
1904             png_uint_32 i;
1905             png_uint_32 istop = channels * row_width;
1906
1907             for (i = 0; i < istop; i++)
1908             {
1909                value = (png_uint_16)((*bp << 8) + *(bp + 1));
1910                value >>= shift[i%channels];
1911                *bp++ = (png_byte)(value >> 8);
1912                *bp++ = (png_byte)(value & 0xff);
1913             }
1914             break;
1915          }
1916 #endif
1917       }
1918    }
1919 }
1920 #endif
1921
1922 #ifdef PNG_READ_16_TO_8_SUPPORTED
1923 /* Chop rows of bit depth 16 down to 8 */
1924 void /* PRIVATE */
1925 png_do_chop(png_row_infop row_info, png_bytep row)
1926 {
1927    png_debug(1, "in png_do_chop");
1928
1929    if (row_info->bit_depth == 16)
1930    {
1931       png_bytep sp = row;
1932       png_bytep dp = row;
1933       png_uint_32 i;
1934       png_uint_32 istop = row_info->width * row_info->channels;
1935
1936       for (i = 0; i<istop; i++, sp += 2, dp++)
1937       {
1938 #ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
1939       /* This does a more accurate scaling of the 16-bit color
1940        * value, rather than a simple low-byte truncation.
1941        *
1942        * What the ideal calculation should be:
1943        *   *dp = (((((png_uint_32)(*sp) << 8) |
1944        *          (png_uint_32)(*(sp + 1))) * 255 + 127)
1945        *          / (png_uint_32)65535L;
1946        *
1947        * GRR: no, I think this is what it really should be:
1948        *   *dp = (((((png_uint_32)(*sp) << 8) |
1949        *           (png_uint_32)(*(sp + 1))) + 128L)
1950        *           / (png_uint_32)257L;
1951        *
1952        * GRR: here's the exact calculation with shifts:
1953        *   temp = (((png_uint_32)(*sp) << 8) |
1954        *           (png_uint_32)(*(sp + 1))) + 128L;
1955        *   *dp = (temp - (temp >> 8)) >> 8;
1956        *
1957        * Approximate calculation with shift/add instead of multiply/divide:
1958        *   *dp = ((((png_uint_32)(*sp) << 8) |
1959        *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
1960        *
1961        * What we actually do to avoid extra shifting and conversion:
1962        */
1963
1964          *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
1965 #else
1966        /* Simply discard the low order byte */
1967          *dp = *sp;
1968 #endif
1969       }
1970       row_info->bit_depth = 8;
1971       row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1972       row_info->rowbytes = row_info->width * row_info->channels;
1973    }
1974 }
1975 #endif
1976
1977 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1978 void /* PRIVATE */
1979 png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
1980 {
1981    png_debug(1, "in png_do_read_swap_alpha");
1982
1983    {
1984       png_uint_32 row_width = row_info->width;
1985       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
1986       {
1987          /* This converts from RGBA to ARGB */
1988          if (row_info->bit_depth == 8)
1989          {
1990             png_bytep sp = row + row_info->rowbytes;
1991             png_bytep dp = sp;
1992             png_byte save;
1993             png_uint_32 i;
1994
1995             for (i = 0; i < row_width; i++)
1996             {
1997                save = *(--sp);
1998                *(--dp) = *(--sp);
1999                *(--dp) = *(--sp);
2000                *(--dp) = *(--sp);
2001                *(--dp) = save;
2002             }
2003          }
2004
2005 #ifdef PNG_READ_16BIT_SUPPORTED
2006          /* This converts from RRGGBBAA to AARRGGBB */
2007          else
2008          {
2009             png_bytep sp = row + row_info->rowbytes;
2010             png_bytep dp = sp;
2011             png_byte save[2];
2012             png_uint_32 i;
2013
2014             for (i = 0; i < row_width; i++)
2015             {
2016                save[0] = *(--sp);
2017                save[1] = *(--sp);
2018                *(--dp) = *(--sp);
2019                *(--dp) = *(--sp);
2020                *(--dp) = *(--sp);
2021                *(--dp) = *(--sp);
2022                *(--dp) = *(--sp);
2023                *(--dp) = *(--sp);
2024                *(--dp) = save[0];
2025                *(--dp) = save[1];
2026             }
2027          }
2028 #endif
2029       }
2030
2031       else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2032       {
2033          /* This converts from GA to AG */
2034          if (row_info->bit_depth == 8)
2035          {
2036             png_bytep sp = row + row_info->rowbytes;
2037             png_bytep dp = sp;
2038             png_byte save;
2039             png_uint_32 i;
2040
2041             for (i = 0; i < row_width; i++)
2042             {
2043                save = *(--sp);
2044                *(--dp) = *(--sp);
2045                *(--dp) = save;
2046             }
2047          }
2048
2049 #ifdef PNG_READ_16BIT_SUPPORTED
2050          /* This converts from GGAA to AAGG */
2051          else
2052          {
2053             png_bytep sp = row + row_info->rowbytes;
2054             png_bytep dp = sp;
2055             png_byte save[2];
2056             png_uint_32 i;
2057
2058             for (i = 0; i < row_width; i++)
2059             {
2060                save[0] = *(--sp);
2061                save[1] = *(--sp);
2062                *(--dp) = *(--sp);
2063                *(--dp) = *(--sp);
2064                *(--dp) = save[0];
2065                *(--dp) = save[1];
2066             }
2067          }
2068 #endif
2069       }
2070    }
2071 }
2072 #endif
2073
2074 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2075 void /* PRIVATE */
2076 png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
2077 {
2078    png_uint_32 row_width;
2079    png_debug(1, "in png_do_read_invert_alpha");
2080
2081    row_width = row_info->width;
2082    if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2083    {
2084       if (row_info->bit_depth == 8)
2085       {
2086          /* This inverts the alpha channel in RGBA */
2087          png_bytep sp = row + row_info->rowbytes;
2088          png_bytep dp = sp;
2089          png_uint_32 i;
2090
2091          for (i = 0; i < row_width; i++)
2092          {
2093             *(--dp) = (png_byte)(255 - *(--sp));
2094
2095 /*          This does nothing:
2096             *(--dp) = *(--sp);
2097             *(--dp) = *(--sp);
2098             *(--dp) = *(--sp);
2099             We can replace it with:
2100 */
2101             sp-=3;
2102             dp=sp;
2103          }
2104       }
2105
2106 #ifdef PNG_READ_16BIT_SUPPORTED
2107       /* This inverts the alpha channel in RRGGBBAA */
2108       else
2109       {
2110          png_bytep sp = row + row_info->rowbytes;
2111          png_bytep dp = sp;
2112          png_uint_32 i;
2113
2114          for (i = 0; i < row_width; i++)
2115          {
2116             *(--dp) = (png_byte)(255 - *(--sp));
2117             *(--dp) = (png_byte)(255 - *(--sp));
2118
2119 /*          This does nothing:
2120             *(--dp) = *(--sp);
2121             *(--dp) = *(--sp);
2122             *(--dp) = *(--sp);
2123             *(--dp) = *(--sp);
2124             *(--dp) = *(--sp);
2125             *(--dp) = *(--sp);
2126             We can replace it with:
2127 */
2128             sp-=6;
2129             dp=sp;
2130          }
2131       }
2132 #endif
2133    }
2134    else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2135    {
2136       if (row_info->bit_depth == 8)
2137       {
2138          /* This inverts the alpha channel in GA */
2139          png_bytep sp = row + row_info->rowbytes;
2140          png_bytep dp = sp;
2141          png_uint_32 i;
2142
2143          for (i = 0; i < row_width; i++)
2144          {
2145             *(--dp) = (png_byte)(255 - *(--sp));
2146             *(--dp) = *(--sp);
2147          }
2148       }
2149
2150 #ifdef PNG_READ_16BIT_SUPPORTED
2151       else
2152       {
2153          /* This inverts the alpha channel in GGAA */
2154          png_bytep sp  = row + row_info->rowbytes;
2155          png_bytep dp = sp;
2156          png_uint_32 i;
2157
2158          for (i = 0; i < row_width; i++)
2159          {
2160             *(--dp) = (png_byte)(255 - *(--sp));
2161             *(--dp) = (png_byte)(255 - *(--sp));
2162 /*
2163             *(--dp) = *(--sp);
2164             *(--dp) = *(--sp);
2165 */
2166             sp-=2;
2167             dp=sp;
2168          }
2169       }
2170 #endif
2171    }
2172 }
2173 #endif
2174
2175 #ifdef PNG_READ_FILLER_SUPPORTED
2176 /* Add filler channel if we have RGB color */
2177 void /* PRIVATE */
2178 png_do_read_filler(png_row_infop row_info, png_bytep row,
2179     png_uint_32 filler, png_uint_32 flags)
2180 {
2181    png_uint_32 i;
2182    png_uint_32 row_width = row_info->width;
2183
2184 #ifdef PNG_READ_16BIT_SUPPORTED
2185    png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
2186 #endif
2187    png_byte lo_filler = (png_byte)(filler & 0xff);
2188
2189    png_debug(1, "in png_do_read_filler");
2190
2191    if (
2192        row_info->color_type == PNG_COLOR_TYPE_GRAY)
2193    {
2194       if (row_info->bit_depth == 8)
2195       {
2196          if (flags & PNG_FLAG_FILLER_AFTER)
2197          {
2198             /* This changes the data from G to GX */
2199             png_bytep sp = row + (png_size_t)row_width;
2200             png_bytep dp =  sp + (png_size_t)row_width;
2201             for (i = 1; i < row_width; i++)
2202             {
2203                *(--dp) = lo_filler;
2204                *(--dp) = *(--sp);
2205             }
2206             *(--dp) = lo_filler;
2207             row_info->channels = 2;
2208             row_info->pixel_depth = 16;
2209             row_info->rowbytes = row_width * 2;
2210          }
2211
2212          else
2213          {
2214             /* This changes the data from G to XG */
2215             png_bytep sp = row + (png_size_t)row_width;
2216             png_bytep dp = sp  + (png_size_t)row_width;
2217             for (i = 0; i < row_width; i++)
2218             {
2219                *(--dp) = *(--sp);
2220                *(--dp) = lo_filler;
2221             }
2222             row_info->channels = 2;
2223             row_info->pixel_depth = 16;
2224             row_info->rowbytes = row_width * 2;
2225          }
2226       }
2227
2228 #ifdef PNG_READ_16BIT_SUPPORTED
2229       else if (row_info->bit_depth == 16)
2230       {
2231          if (flags & PNG_FLAG_FILLER_AFTER)
2232          {
2233             /* This changes the data from GG to GGXX */
2234             png_bytep sp = row + (png_size_t)row_width * 2;
2235             png_bytep dp = sp  + (png_size_t)row_width * 2;
2236             for (i = 1; i < row_width; i++)
2237             {
2238                *(--dp) = hi_filler;
2239                *(--dp) = lo_filler;
2240                *(--dp) = *(--sp);
2241                *(--dp) = *(--sp);
2242             }
2243             *(--dp) = hi_filler;
2244             *(--dp) = lo_filler;
2245             row_info->channels = 2;
2246             row_info->pixel_depth = 32;
2247             row_info->rowbytes = row_width * 4;
2248          }
2249
2250          else
2251          {
2252             /* This changes the data from GG to XXGG */
2253             png_bytep sp = row + (png_size_t)row_width * 2;
2254             png_bytep dp = sp  + (png_size_t)row_width * 2;
2255             for (i = 0; i < row_width; i++)
2256             {
2257                *(--dp) = *(--sp);
2258                *(--dp) = *(--sp);
2259                *(--dp) = hi_filler;
2260                *(--dp) = lo_filler;
2261             }
2262             row_info->channels = 2;
2263             row_info->pixel_depth = 32;
2264             row_info->rowbytes = row_width * 4;
2265          }
2266       }
2267 #endif
2268    } /* COLOR_TYPE == GRAY */
2269    else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2270    {
2271       if (row_info->bit_depth == 8)
2272       {
2273          if (flags & PNG_FLAG_FILLER_AFTER)
2274          {
2275             /* This changes the data from RGB to RGBX */
2276             png_bytep sp = row + (png_size_t)row_width * 3;
2277             png_bytep dp = sp  + (png_size_t)row_width;
2278             for (i = 1; i < row_width; i++)
2279             {
2280                *(--dp) = lo_filler;
2281                *(--dp) = *(--sp);
2282                *(--dp) = *(--sp);
2283                *(--dp) = *(--sp);
2284             }
2285             *(--dp) = lo_filler;
2286             row_info->channels = 4;
2287             row_info->pixel_depth = 32;
2288             row_info->rowbytes = row_width * 4;
2289          }
2290
2291          else
2292          {
2293             /* This changes the data from RGB to XRGB */
2294             png_bytep sp = row + (png_size_t)row_width * 3;
2295             png_bytep dp = sp + (png_size_t)row_width;
2296             for (i = 0; i < row_width; i++)
2297             {
2298                *(--dp) = *(--sp);
2299                *(--dp) = *(--sp);
2300                *(--dp) = *(--sp);
2301                *(--dp) = lo_filler;
2302             }
2303             row_info->channels = 4;
2304             row_info->pixel_depth = 32;
2305             row_info->rowbytes = row_width * 4;
2306          }
2307       }
2308
2309 #ifdef PNG_READ_16BIT_SUPPORTED
2310       else if (row_info->bit_depth == 16)
2311       {
2312          if (flags & PNG_FLAG_FILLER_AFTER)
2313          {
2314             /* This changes the data from RRGGBB to RRGGBBXX */
2315             png_bytep sp = row + (png_size_t)row_width * 6;
2316             png_bytep dp = sp  + (png_size_t)row_width * 2;
2317             for (i = 1; i < row_width; i++)
2318             {
2319                *(--dp) = hi_filler;
2320                *(--dp) = lo_filler;
2321                *(--dp) = *(--sp);
2322                *(--dp) = *(--sp);
2323                *(--dp) = *(--sp);
2324                *(--dp) = *(--sp);
2325                *(--dp) = *(--sp);
2326                *(--dp) = *(--sp);
2327             }
2328             *(--dp) = hi_filler;
2329             *(--dp) = lo_filler;
2330             row_info->channels = 4;
2331             row_info->pixel_depth = 64;
2332             row_info->rowbytes = row_width * 8;
2333          }
2334
2335          else
2336          {
2337             /* This changes the data from RRGGBB to XXRRGGBB */
2338             png_bytep sp = row + (png_size_t)row_width * 6;
2339             png_bytep dp = sp  + (png_size_t)row_width * 2;
2340             for (i = 0; i < row_width; i++)
2341             {
2342                *(--dp) = *(--sp);
2343                *(--dp) = *(--sp);
2344                *(--dp) = *(--sp);
2345                *(--dp) = *(--sp);
2346                *(--dp) = *(--sp);
2347                *(--dp) = *(--sp);
2348                *(--dp) = hi_filler;
2349                *(--dp) = lo_filler;
2350             }
2351
2352             row_info->channels = 4;
2353             row_info->pixel_depth = 64;
2354             row_info->rowbytes = row_width * 8;
2355          }
2356       }
2357 #endif
2358    } /* COLOR_TYPE == RGB */
2359 }
2360 #endif
2361
2362 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2363 /* Expand grayscale files to RGB, with or without alpha */
2364 void /* PRIVATE */
2365 png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
2366 {
2367    png_uint_32 i;
2368    png_uint_32 row_width = row_info->width;
2369
2370    png_debug(1, "in png_do_gray_to_rgb");
2371
2372    if (row_info->bit_depth >= 8 &&
2373        !(row_info->color_type & PNG_COLOR_MASK_COLOR))
2374    {
2375       if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
2376       {
2377          if (row_info->bit_depth == 8)
2378          {
2379             /* This changes G to RGB */
2380             png_bytep sp = row + (png_size_t)row_width - 1;
2381             png_bytep dp = sp  + (png_size_t)row_width * 2;
2382             for (i = 0; i < row_width; i++)
2383             {
2384                *(dp--) = *sp;
2385                *(dp--) = *sp;
2386                *(dp--) = *(sp--);
2387             }
2388          }
2389
2390          else
2391          {
2392             /* This changes GG to RRGGBB */
2393             png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2394             png_bytep dp = sp  + (png_size_t)row_width * 4;
2395             for (i = 0; i < row_width; i++)
2396             {
2397                *(dp--) = *sp;
2398                *(dp--) = *(sp - 1);
2399                *(dp--) = *sp;
2400                *(dp--) = *(sp - 1);
2401                *(dp--) = *(sp--);
2402                *(dp--) = *(sp--);
2403             }
2404          }
2405       }
2406
2407       else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2408       {
2409          if (row_info->bit_depth == 8)
2410          {
2411             /* This changes GA to RGBA */
2412             png_bytep sp = row + (png_size_t)row_width * 2 - 1;
2413             png_bytep dp = sp  + (png_size_t)row_width * 2;
2414             for (i = 0; i < row_width; i++)
2415             {
2416                *(dp--) = *(sp--);
2417                *(dp--) = *sp;
2418                *(dp--) = *sp;
2419                *(dp--) = *(sp--);
2420             }
2421          }
2422
2423          else
2424          {
2425             /* This changes GGAA to RRGGBBAA */
2426             png_bytep sp = row + (png_size_t)row_width * 4 - 1;
2427             png_bytep dp = sp  + (png_size_t)row_width * 4;
2428             for (i = 0; i < row_width; i++)
2429             {
2430                *(dp--) = *(sp--);
2431                *(dp--) = *(sp--);
2432                *(dp--) = *sp;
2433                *(dp--) = *(sp - 1);
2434                *(dp--) = *sp;
2435                *(dp--) = *(sp - 1);
2436                *(dp--) = *(sp--);
2437                *(dp--) = *(sp--);
2438             }
2439          }
2440       }
2441       row_info->channels += (png_byte)2;
2442       row_info->color_type |= PNG_COLOR_MASK_COLOR;
2443       row_info->pixel_depth = (png_byte)(row_info->channels *
2444           row_info->bit_depth);
2445       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2446    }
2447 }
2448 #endif
2449
2450 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2451 /* Reduce RGB files to grayscale, with or without alpha
2452  * using the equation given in Poynton's ColorFAQ at
2453  * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
2454  * New link:
2455  * <http://www.poynton.com/notes/colour_and_gamma/>
2456  * Charles Poynton poynton at poynton.com
2457  *
2458  *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2459  *
2460  *  We approximate this with
2461  *
2462  *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
2463  *
2464  *  which can be expressed with integers as
2465  *
2466  *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
2467  *
2468  *  The calculation is to be done in a linear colorspace.
2469  *
2470  *  Other integer coefficents can be used via png_set_rgb_to_gray().
2471  */
2472 int /* PRIVATE */
2473 png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
2474
2475 {
2476    png_uint_32 i;
2477
2478    png_uint_32 row_width = row_info->width;
2479    int rgb_error = 0;
2480
2481    png_debug(1, "in png_do_rgb_to_gray");
2482
2483    if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
2484        (row_info->color_type & PNG_COLOR_MASK_COLOR))
2485    {
2486       png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
2487       png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
2488       png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2489
2490       if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2491       {
2492          if (row_info->bit_depth == 8)
2493          {
2494 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2495             if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2496             {
2497                png_bytep sp = row;
2498                png_bytep dp = row;
2499
2500                for (i = 0; i < row_width; i++)
2501                {
2502                   png_byte red   = png_ptr->gamma_to_1[*(sp++)];
2503                   png_byte green = png_ptr->gamma_to_1[*(sp++)];
2504                   png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
2505
2506                   if (red != green || red != blue)
2507                   {
2508                      rgb_error |= 1;
2509                      *(dp++) = png_ptr->gamma_from_1[
2510                          (rc*red + gc*green + bc*blue)>>15];
2511                   }
2512
2513                   else
2514                      *(dp++) = *(sp - 1);
2515                }
2516             }
2517             else
2518 #endif
2519             {
2520                png_bytep sp = row;
2521                png_bytep dp = row;
2522                for (i = 0; i < row_width; i++)
2523                {
2524                   png_byte red   = *(sp++);
2525                   png_byte green = *(sp++);
2526                   png_byte blue  = *(sp++);
2527
2528                   if (red != green || red != blue)
2529                   {
2530                      rgb_error |= 1;
2531                      *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2532                   }
2533
2534                   else
2535                      *(dp++) = *(sp - 1);
2536                }
2537             }
2538          }
2539
2540          else /* RGB bit_depth == 16 */
2541          {
2542 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2543             if (png_ptr->gamma_16_to_1 != NULL &&
2544                 png_ptr->gamma_16_from_1 != NULL)
2545             {
2546                png_bytep sp = row;
2547                png_bytep dp = row;
2548                for (i = 0; i < row_width; i++)
2549                {
2550                   png_uint_16 red, green, blue, w;
2551
2552                   red   = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2553                   green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2554                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2555
2556                   if (red == green && red == blue)
2557                      w = red;
2558
2559                   else
2560                   {
2561                      png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff)
2562                          >> png_ptr->gamma_shift][red>>8];
2563                      png_uint_16 green_1 =
2564                          png_ptr->gamma_16_to_1[(green&0xff) >>
2565                          png_ptr->gamma_shift][green>>8];
2566                      png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff)
2567                          >> png_ptr->gamma_shift][blue>>8];
2568                      png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
2569                          + bc*blue_1)>>15);
2570                      w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2571                          png_ptr->gamma_shift][gray16 >> 8];
2572                      rgb_error |= 1;
2573                   }
2574
2575                   *(dp++) = (png_byte)((w>>8) & 0xff);
2576                   *(dp++) = (png_byte)(w & 0xff);
2577                }
2578             }
2579             else
2580 #endif
2581             {
2582                png_bytep sp = row;
2583                png_bytep dp = row;
2584                for (i = 0; i < row_width; i++)
2585                {
2586                   png_uint_16 red, green, blue, gray16;
2587
2588                   red   = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2589                   green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2590                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2591
2592                   if (red != green || red != blue)
2593                      rgb_error |= 1;
2594
2595                   gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2596                   *(dp++) = (png_byte)((gray16>>8) & 0xff);
2597                   *(dp++) = (png_byte)(gray16 & 0xff);
2598                }
2599             }
2600          }
2601       }
2602       if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2603       {
2604          if (row_info->bit_depth == 8)
2605          {
2606 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2607             if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
2608             {
2609                png_bytep sp = row;
2610                png_bytep dp = row;
2611                for (i = 0; i < row_width; i++)
2612                {
2613                   png_byte red   = png_ptr->gamma_to_1[*(sp++)];
2614                   png_byte green = png_ptr->gamma_to_1[*(sp++)];
2615                   png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
2616
2617                   if (red != green || red != blue)
2618                      rgb_error |= 1;
2619
2620                   *(dp++) =  png_ptr->gamma_from_1
2621                       [(rc*red + gc*green + bc*blue)>>15];
2622
2623                   *(dp++) = *(sp++);  /* alpha */
2624                }
2625             }
2626             else
2627 #endif
2628             {
2629                png_bytep sp = row;
2630                png_bytep dp = row;
2631                for (i = 0; i < row_width; i++)
2632                {
2633                   png_byte red   = *(sp++);
2634                   png_byte green = *(sp++);
2635                   png_byte blue  = *(sp++);
2636                   if (red != green || red != blue)
2637                      rgb_error |= 1;
2638
2639                   *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
2640                   *(dp++) = *(sp++);  /* alpha */
2641                }
2642             }
2643          }
2644          else /* RGBA bit_depth == 16 */
2645          {
2646 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
2647             if (png_ptr->gamma_16_to_1 != NULL &&
2648                 png_ptr->gamma_16_from_1 != NULL)
2649             {
2650                png_bytep sp = row;
2651                png_bytep dp = row;
2652                for (i = 0; i < row_width; i++)
2653                {
2654                   png_uint_16 red, green, blue, w;
2655
2656                   red   = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2657                   green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2658                   blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2659
2660                   if (red == green && red == blue)
2661                      w = red;
2662
2663                   else
2664                   {
2665                      png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
2666                          png_ptr->gamma_shift][red>>8];
2667
2668                      png_uint_16 green_1 =
2669                          png_ptr->gamma_16_to_1[(green&0xff) >>
2670                          png_ptr->gamma_shift][green>>8];
2671
2672                      png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
2673                          png_ptr->gamma_shift][blue>>8];
2674
2675                      png_uint_16 gray16  = (png_uint_16)((rc * red_1
2676                          + gc * green_1 + bc * blue_1)>>15);
2677
2678                      w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2679                          png_ptr->gamma_shift][gray16 >> 8];
2680
2681                      rgb_error |= 1;
2682                   }
2683
2684                   *(dp++) = (png_byte)((w>>8) & 0xff);
2685                   *(dp++) = (png_byte)(w & 0xff);
2686                   *(dp++) = *(sp++);  /* alpha */
2687                   *(dp++) = *(sp++);
2688                }
2689             }
2690             else
2691 #endif
2692             {
2693                png_bytep sp = row;
2694                png_bytep dp = row;
2695                for (i = 0; i < row_width; i++)
2696                {
2697                   png_uint_16 red, green, blue, gray16;
2698                   red   = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2699                   green = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2700                   blue  = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
2701
2702                   if (red != green || red != blue)
2703                      rgb_error |= 1;
2704
2705                   gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2706                   *(dp++) = (png_byte)((gray16>>8) & 0xff);
2707                   *(dp++) = (png_byte)(gray16 & 0xff);
2708                   *(dp++) = *(sp++);  /* alpha */
2709                   *(dp++) = *(sp++);
2710                }
2711             }
2712          }
2713       }
2714       row_info->channels -= 2;
2715       row_info->color_type = (png_byte)(row_info->color_type &
2716           ~PNG_COLOR_MASK_COLOR);
2717       row_info->pixel_depth = (png_byte)(row_info->channels *
2718           row_info->bit_depth);
2719       row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2720    }
2721    return rgb_error;
2722 }
2723 #endif
2724
2725 /* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
2726  * large of png_color.  This lets grayscale images be treated as
2727  * paletted.  Most useful for gamma correction and simplification
2728  * of code.
2729  */
2730 void PNGAPI
2731 png_build_grayscale_palette(int bit_depth, png_colorp palette)
2732 {
2733    int num_palette;
2734    int color_inc;
2735    int i;
2736    int v;
2737
2738    png_debug(1, "in png_do_build_grayscale_palette");
2739
2740    if (palette == NULL)
2741       return;
2742
2743    switch (bit_depth)
2744    {
2745       case 1:
2746          num_palette = 2;
2747          color_inc = 0xff;
2748          break;
2749
2750       case 2:
2751          num_palette = 4;
2752          color_inc = 0x55;
2753          break;
2754
2755       case 4:
2756          num_palette = 16;
2757          color_inc = 0x11;
2758          break;
2759
2760       case 8:
2761          num_palette = 256;
2762          color_inc = 1;
2763          break;
2764
2765       default:
2766          num_palette = 0;
2767          color_inc = 0;
2768          break;
2769    }
2770
2771    for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
2772    {
2773       palette[i].red = (png_byte)v;
2774       palette[i].green = (png_byte)v;
2775       palette[i].blue = (png_byte)v;
2776    }
2777 }
2778
2779
2780 #ifdef PNG_READ_BACKGROUND_SUPPORTED
2781 /* Replace any alpha or transparency with the supplied background color.
2782  * "background" is already in the screen gamma, while "background_1" is
2783  * at a gamma of 1.0.  Paletted files have already been taken care of.
2784  */
2785 void /* PRIVATE */
2786 png_do_background(png_row_infop row_info, png_bytep row,
2787     png_const_color_16p trans_color, png_const_color_16p background
2788 #ifdef PNG_READ_GAMMA_SUPPORTED
2789     , png_const_color_16p background_1, png_const_bytep gamma_table,
2790     png_const_bytep gamma_from_1, png_const_bytep gamma_to_1,
2791     png_const_uint_16pp gamma_16, png_const_uint_16pp gamma_16_from_1,
2792     png_const_uint_16pp gamma_16_to_1, int gamma_shift
2793 #endif
2794     )
2795 {
2796    png_bytep sp, dp;
2797    png_uint_32 i;
2798    png_uint_32 row_width = row_info->width;
2799    int shift;
2800
2801    png_debug(1, "in png_do_background");
2802
2803    if (background != NULL &&
2804       (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
2805       (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
2806    {
2807       switch (row_info->color_type)
2808       {
2809          case PNG_COLOR_TYPE_GRAY:
2810          {
2811             switch (row_info->bit_depth)
2812             {
2813                case 1:
2814                {
2815                   sp = row;
2816                   shift = 7;
2817                   for (i = 0; i < row_width; i++)
2818                   {
2819                      if ((png_uint_16)((*sp >> shift) & 0x01)
2820                         == trans_color->gray)
2821                      {
2822                         *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
2823                         *sp |= (png_byte)(background->gray << shift);
2824                      }
2825
2826                      if (!shift)
2827                      {
2828                         shift = 7;
2829                         sp++;
2830                      }
2831
2832                      else
2833                         shift--;
2834                   }
2835                   break;
2836                }
2837
2838                case 2:
2839                {
2840 #ifdef PNG_READ_GAMMA_SUPPORTED
2841                   if (gamma_table != NULL)
2842                   {
2843                      sp = row;
2844                      shift = 6;
2845                      for (i = 0; i < row_width; i++)
2846                      {
2847                         if ((png_uint_16)((*sp >> shift) & 0x03)
2848                             == trans_color->gray)
2849                         {
2850                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2851                            *sp |= (png_byte)(background->gray << shift);
2852                         }
2853
2854                         else
2855                         {
2856                            png_byte p = (png_byte)((*sp >> shift) & 0x03);
2857                            png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2858                                (p << 4) | (p << 6)] >> 6) & 0x03);
2859                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2860                            *sp |= (png_byte)(g << shift);
2861                         }
2862
2863                         if (!shift)
2864                         {
2865                            shift = 6;
2866                            sp++;
2867                         }
2868
2869                         else
2870                            shift -= 2;
2871                      }
2872                   }
2873
2874                   else
2875 #endif
2876                   {
2877                      sp = row;
2878                      shift = 6;
2879                      for (i = 0; i < row_width; i++)
2880                      {
2881                         if ((png_uint_16)((*sp >> shift) & 0x03)
2882                             == trans_color->gray)
2883                         {
2884                            *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
2885                            *sp |= (png_byte)(background->gray << shift);
2886                         }
2887
2888                         if (!shift)
2889                         {
2890                            shift = 6;
2891                            sp++;
2892                         }
2893
2894                         else
2895                            shift -= 2;
2896                      }
2897                   }
2898                   break;
2899                }
2900
2901                case 4:
2902                {
2903 #ifdef PNG_READ_GAMMA_SUPPORTED
2904                   if (gamma_table != NULL)
2905                   {
2906                      sp = row;
2907                      shift = 4;
2908                      for (i = 0; i < row_width; i++)
2909                      {
2910                         if ((png_uint_16)((*sp >> shift) & 0x0f)
2911                             == trans_color->gray)
2912                         {
2913                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2914                            *sp |= (png_byte)(background->gray << shift);
2915                         }
2916
2917                         else
2918                         {
2919                            png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2920                            png_byte g = (png_byte)((gamma_table[p |
2921                                (p << 4)] >> 4) & 0x0f);
2922                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2923                            *sp |= (png_byte)(g << shift);
2924                         }
2925
2926                         if (!shift)
2927                         {
2928                            shift = 4;
2929                            sp++;
2930                         }
2931
2932                         else
2933                            shift -= 4;
2934                      }
2935                   }
2936
2937                   else
2938 #endif
2939                   {
2940                      sp = row;
2941                      shift = 4;
2942                      for (i = 0; i < row_width; i++)
2943                      {
2944                         if ((png_uint_16)((*sp >> shift) & 0x0f)
2945                             == trans_color->gray)
2946                         {
2947                            *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
2948                            *sp |= (png_byte)(background->gray << shift);
2949                         }
2950
2951                         if (!shift)
2952                         {
2953                            shift = 4;
2954                            sp++;
2955                         }
2956
2957                         else
2958                            shift -= 4;
2959                      }
2960                   }
2961                   break;
2962                }
2963
2964                case 8:
2965                {
2966 #ifdef PNG_READ_GAMMA_SUPPORTED
2967                   if (gamma_table != NULL)
2968                   {
2969                      sp = row;
2970                      for (i = 0; i < row_width; i++, sp++)
2971                      {
2972                         if (*sp == trans_color->gray)
2973                            *sp = (png_byte)background->gray;
2974
2975                         else
2976                            *sp = gamma_table[*sp];
2977                      }
2978                   }
2979                   else
2980 #endif
2981                   {
2982                      sp = row;
2983                      for (i = 0; i < row_width; i++, sp++)
2984                      {
2985                         if (*sp == trans_color->gray)
2986                            *sp = (png_byte)background->gray;
2987                      }
2988                   }
2989                   break;
2990                }
2991
2992                case 16:
2993                {
2994 #ifdef PNG_READ_GAMMA_SUPPORTED
2995                   if (gamma_16 != NULL)
2996                   {
2997                      sp = row;
2998                      for (i = 0; i < row_width; i++, sp += 2)
2999                      {
3000                         png_uint_16 v;
3001
3002                         v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3003
3004                         if (v == trans_color->gray)
3005                         {
3006                            /* Background is already in screen gamma */
3007                            *sp = (png_byte)((background->gray >> 8) & 0xff);
3008                            *(sp + 1) = (png_byte)(background->gray & 0xff);
3009                         }
3010
3011                         else
3012                         {
3013                            v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3014                            *sp = (png_byte)((v >> 8) & 0xff);
3015                            *(sp + 1) = (png_byte)(v & 0xff);
3016                         }
3017                      }
3018                   }
3019                   else
3020 #endif
3021                   {
3022                      sp = row;
3023                      for (i = 0; i < row_width; i++, sp += 2)
3024                      {
3025                         png_uint_16 v;
3026
3027                         v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3028
3029                         if (v == trans_color->gray)
3030                         {
3031                            *sp = (png_byte)((background->gray >> 8) & 0xff);
3032                            *(sp + 1) = (png_byte)(background->gray & 0xff);
3033                         }
3034                      }
3035                   }
3036                   break;
3037                }
3038
3039                default:
3040                   break;
3041             }
3042             break;
3043          }
3044
3045          case PNG_COLOR_TYPE_RGB:
3046          {
3047             if (row_info->bit_depth == 8)
3048             {
3049 #ifdef PNG_READ_GAMMA_SUPPORTED
3050                if (gamma_table != NULL)
3051                {
3052                   sp = row;
3053                   for (i = 0; i < row_width; i++, sp += 3)
3054                   {
3055                      if (*sp == trans_color->red &&
3056                          *(sp + 1) == trans_color->green &&
3057                          *(sp + 2) == trans_color->blue)
3058                      {
3059                         *sp = (png_byte)background->red;
3060                         *(sp + 1) = (png_byte)background->green;
3061                         *(sp + 2) = (png_byte)background->blue;
3062                      }
3063
3064                      else
3065                      {
3066                         *sp = gamma_table[*sp];
3067                         *(sp + 1) = gamma_table[*(sp + 1)];
3068                         *(sp + 2) = gamma_table[*(sp + 2)];
3069                      }
3070                   }
3071                }
3072                else
3073 #endif
3074                {
3075                   sp = row;
3076                   for (i = 0; i < row_width; i++, sp += 3)
3077                   {
3078                      if (*sp == trans_color->red &&
3079                          *(sp + 1) == trans_color->green &&
3080                          *(sp + 2) == trans_color->blue)
3081                      {
3082                         *sp = (png_byte)background->red;
3083                         *(sp + 1) = (png_byte)background->green;
3084                         *(sp + 2) = (png_byte)background->blue;
3085                      }
3086                   }
3087                }
3088             }
3089             else /* if (row_info->bit_depth == 16) */
3090             {
3091 #ifdef PNG_READ_GAMMA_SUPPORTED
3092                if (gamma_16 != NULL)
3093                {
3094                   sp = row;
3095                   for (i = 0; i < row_width; i++, sp += 6)
3096                   {
3097                      png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3098
3099                      png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3100                          + *(sp + 3));
3101
3102                      png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3103                          + *(sp + 5));
3104
3105                      if (r == trans_color->red && g == trans_color->green &&
3106                          b == trans_color->blue)
3107                      {
3108                         /* Background is already in screen gamma */
3109                         *sp = (png_byte)((background->red >> 8) & 0xff);
3110                         *(sp + 1) = (png_byte)(background->red & 0xff);
3111                         *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3112                         *(sp + 3) = (png_byte)(background->green & 0xff);
3113                         *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3114                         *(sp + 5) = (png_byte)(background->blue & 0xff);
3115                      }
3116
3117                      else
3118                      {
3119                         png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3120                         *sp = (png_byte)((v >> 8) & 0xff);
3121                         *(sp + 1) = (png_byte)(v & 0xff);
3122
3123                         v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3124                         *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3125                         *(sp + 3) = (png_byte)(v & 0xff);
3126
3127                         v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3128                         *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3129                         *(sp + 5) = (png_byte)(v & 0xff);
3130                      }
3131                   }
3132                }
3133
3134                else
3135 #endif
3136                {
3137                   sp = row;
3138                   for (i = 0; i < row_width; i++, sp += 6)
3139                   {
3140                      png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3141
3142                      png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3143                          + *(sp + 3));
3144
3145                      png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3146                          + *(sp + 5));
3147
3148                      if (r == trans_color->red && g == trans_color->green &&
3149                          b == trans_color->blue)
3150                      {
3151                         *sp = (png_byte)((background->red >> 8) & 0xff);
3152                         *(sp + 1) = (png_byte)(background->red & 0xff);
3153                         *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3154                         *(sp + 3) = (png_byte)(background->green & 0xff);
3155                         *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3156                         *(sp + 5) = (png_byte)(background->blue & 0xff);
3157                      }
3158                   }
3159                }
3160             }
3161             break;
3162          }
3163
3164          case PNG_COLOR_TYPE_GRAY_ALPHA:
3165          {
3166             if (row_info->bit_depth == 8)
3167             {
3168 #ifdef PNG_READ_GAMMA_SUPPORTED
3169                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3170                    gamma_table != NULL)
3171                {
3172                   sp = row;
3173                   dp = row;
3174                   for (i = 0; i < row_width; i++, sp += 2, dp++)
3175                   {
3176                      png_uint_16 a = *(sp + 1);
3177
3178                      if (a == 0xff)
3179                         *dp = gamma_table[*sp];
3180
3181                      else if (a == 0)
3182                      {
3183                         /* Background is already in screen gamma */
3184                         *dp = (png_byte)background->gray;
3185                      }
3186
3187                      else
3188                      {
3189                         png_byte v, w;
3190
3191                         v = gamma_to_1[*sp];
3192                         png_composite(w, v, a, background_1->gray);
3193                         *dp = gamma_from_1[w];
3194                      }
3195                   }
3196                }
3197                else
3198 #endif
3199                {
3200                   sp = row;
3201                   dp = row;
3202                   for (i = 0; i < row_width; i++, sp += 2, dp++)
3203                   {
3204                      png_byte a = *(sp + 1);
3205
3206                      if (a == 0xff)
3207                         *dp = *sp;
3208
3209 #ifdef PNG_READ_GAMMA_SUPPORTED
3210                      else if (a == 0)
3211                         *dp = (png_byte)background->gray;
3212
3213                      else
3214                         png_composite(*dp, *sp, a, background_1->gray);
3215
3216 #else
3217                      *dp = (png_byte)background->gray;
3218 #endif
3219                   }
3220                }
3221             }
3222             else /* if (png_ptr->bit_depth == 16) */
3223             {
3224 #ifdef PNG_READ_GAMMA_SUPPORTED
3225                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3226                    gamma_16_to_1 != NULL)
3227                {
3228                   sp = row;
3229                   dp = row;
3230                   for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3231                   {
3232                      png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3233                          + *(sp + 3));
3234
3235                      if (a == (png_uint_16)0xffff)
3236                      {
3237                         png_uint_16 v;
3238
3239                         v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3240                         *dp = (png_byte)((v >> 8) & 0xff);
3241                         *(dp + 1) = (png_byte)(v & 0xff);
3242                      }
3243
3244 #ifdef PNG_READ_GAMMA_SUPPORTED
3245                      else if (a == 0)
3246 #else
3247                      else
3248 #endif
3249                      {
3250                         /* Background is already in screen gamma */
3251                         *dp = (png_byte)((background->gray >> 8) & 0xff);
3252                         *(dp + 1) = (png_byte)(background->gray & 0xff);
3253                      }
3254
3255 #ifdef PNG_READ_GAMMA_SUPPORTED
3256                      else
3257                      {
3258                         png_uint_16 g, v, w;
3259
3260                         g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3261                         png_composite_16(v, g, a, background_1->gray);
3262                         w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3263                         *dp = (png_byte)((w >> 8) & 0xff);
3264                         *(dp + 1) = (png_byte)(w & 0xff);
3265                      }
3266 #endif
3267                   }
3268                }
3269                else
3270 #endif
3271                {
3272                   sp = row;
3273                   dp = row;
3274                   for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3275                   {
3276                      png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3277                          + *(sp + 3));
3278
3279                      if (a == (png_uint_16)0xffff)
3280                         png_memcpy(dp, sp, 2);
3281
3282 #ifdef PNG_READ_GAMMA_SUPPORTED
3283                      else if (a == 0)
3284 #else
3285                      else
3286 #endif
3287                      {
3288                         *dp = (png_byte)((background->gray >> 8) & 0xff);
3289                         *(dp + 1) = (png_byte)(background->gray & 0xff);
3290                      }
3291
3292 #ifdef PNG_READ_GAMMA_SUPPORTED
3293                      else
3294                      {
3295                         png_uint_16 g, v;
3296
3297                         g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3298                         png_composite_16(v, g, a, background_1->gray);
3299                         *dp = (png_byte)((v >> 8) & 0xff);
3300                         *(dp + 1) = (png_byte)(v & 0xff);
3301                      }
3302 #endif
3303                   }
3304                }
3305             }
3306             break;
3307          }
3308
3309          case PNG_COLOR_TYPE_RGB_ALPHA:
3310          {
3311             if (row_info->bit_depth == 8)
3312             {
3313 #ifdef PNG_READ_GAMMA_SUPPORTED
3314                if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3315                    gamma_table != NULL)
3316                {
3317                   sp = row;
3318                   dp = row;
3319                   for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3320                   {
3321                      png_byte a = *(sp + 3);
3322
3323                      if (a == 0xff)
3324                      {
3325                         *dp = gamma_table[*sp];
3326                         *(dp + 1) = gamma_table[*(sp + 1)];
3327                         *(dp + 2) = gamma_table[*(sp + 2)];
3328                      }
3329
3330                      else if (a == 0)
3331                      {
3332                         /* Background is already in screen gamma */
3333                         *dp = (png_byte)background->red;
3334                         *(dp + 1) = (png_byte)background->green;
3335                         *(dp + 2) = (png_byte)background->blue;
3336                      }
3337
3338                      else
3339                      {
3340                         png_byte v, w;
3341
3342                         v = gamma_to_1[*sp];
3343                         png_composite(w, v, a, background_1->red);
3344                         *dp = gamma_from_1[w];
3345
3346                         v = gamma_to_1[*(sp + 1)];
3347                         png_composite(w, v, a, background_1->green);
3348                         *(dp + 1) = gamma_from_1[w];
3349
3350                         v = gamma_to_1[*(sp + 2)];
3351                         png_composite(w, v, a, background_1->blue);
3352                         *(dp + 2) = gamma_from_1[w];
3353                      }
3354                   }
3355                }
3356                else
3357 #endif
3358                {
3359                   sp = row;
3360                   dp = row;
3361                   for (i = 0; i < row_width; i++, sp += 4, dp += 3)
3362                   {
3363                      png_byte a = *(sp + 3);
3364
3365                      if (a == 0xff)
3366                      {
3367                         *dp = *sp;
3368                         *(dp + 1) = *(sp + 1);
3369                         *(dp + 2) = *(sp + 2);
3370                      }
3371
3372                      else if (a == 0)
3373                      {
3374                         *dp = (png_byte)background->red;
3375                         *(dp + 1) = (png_byte)background->green;
3376                         *(dp + 2) = (png_byte)background->blue;
3377                      }
3378
3379                      else
3380                      {
3381                         png_composite(*dp, *sp, a, background->red);
3382
3383                         png_composite(*(dp + 1), *(sp + 1), a,
3384                             background->green);
3385
3386                         png_composite(*(dp + 2), *(sp + 2), a,
3387                             background->blue);
3388                      }
3389                   }
3390                }
3391             }
3392             else /* if (row_info->bit_depth == 16) */
3393             {
3394 #ifdef PNG_READ_GAMMA_SUPPORTED
3395                if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3396                    gamma_16_to_1 != NULL)
3397                {
3398                   sp = row;
3399                   dp = row;
3400                   for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3401                   {
3402                      png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3403                          << 8) + (png_uint_16)(*(sp + 7)));
3404
3405                      if (a == (png_uint_16)0xffff)
3406                      {
3407                         png_uint_16 v;
3408
3409                         v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3410                         *dp = (png_byte)((v >> 8) & 0xff);
3411                         *(dp + 1) = (png_byte)(v & 0xff);
3412
3413                         v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3414                         *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3415                         *(dp + 3) = (png_byte)(v & 0xff);
3416
3417                         v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3418                         *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3419                         *(dp + 5) = (png_byte)(v & 0xff);
3420                      }
3421
3422                      else if (a == 0)
3423                      {
3424                         /* Background is already in screen gamma */
3425                         *dp = (png_byte)((background->red >> 8) & 0xff);
3426                         *(dp + 1) = (png_byte)(background->red & 0xff);
3427                         *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3428                         *(dp + 3) = (png_byte)(background->green & 0xff);
3429                         *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3430                         *(dp + 5) = (png_byte)(background->blue & 0xff);
3431                      }
3432
3433                      else
3434                      {
3435                         png_uint_16 v, w, x;
3436
3437                         v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3438                         png_composite_16(w, v, a, background_1->red);
3439
3440                         x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3441                         *dp = (png_byte)((x >> 8) & 0xff);
3442                         *(dp + 1) = (png_byte)(x & 0xff);
3443
3444                         v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3445                         png_composite_16(w, v, a, background_1->green);
3446
3447                         x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
3448                         *(dp + 2) = (png_byte)((x >> 8) & 0xff);
3449                         *(dp + 3) = (png_byte)(x & 0xff);
3450
3451                         v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
3452                         png_composite_16(w, v, a, background_1->blue);
3453
3454                         x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
3455                         *(dp + 4) = (png_byte)((x >> 8) & 0xff);
3456                         *(dp + 5) = (png_byte)(x & 0xff);
3457                      }
3458                   }
3459                }
3460
3461                else
3462 #endif
3463                {
3464                   sp = row;
3465                   dp = row;
3466                   for (i = 0; i < row_width; i++, sp += 8, dp += 6)
3467                   {
3468                      png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3469                          << 8) + (png_uint_16)(*(sp + 7)));
3470
3471                      if (a == (png_uint_16)0xffff)
3472                      {
3473                         png_memcpy(dp, sp, 6);
3474                      }
3475
3476                      else if (a == 0)
3477                      {
3478                         *dp = (png_byte)((background->red >> 8) & 0xff);
3479                         *(dp + 1) = (png_byte)(background->red & 0xff);
3480                         *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
3481                         *(dp + 3) = (png_byte)(background->green & 0xff);
3482                         *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3483                         *(dp + 5) = (png_byte)(background->blue & 0xff);
3484                      }
3485
3486                      else
3487                      {
3488                         png_uint_16 v;
3489
3490                         png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3491                         png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3492                             + *(sp + 3));
3493                         png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3494                             + *(sp + 5));
3495
3496                         png_composite_16(v, r, a, background->red);
3497                         *dp = (png_byte)((v >> 8) & 0xff);
3498                         *(dp + 1) = (png_byte)(v & 0xff);
3499
3500                         png_composite_16(v, g, a, background->green);
3501                         *(dp + 2) = (png_byte)((v >> 8) & 0xff);
3502                         *(dp + 3) = (png_byte)(v & 0xff);
3503
3504                         png_composite_16(v, b, a, background->blue);
3505                         *(dp + 4) = (png_byte)((v >> 8) & 0xff);
3506                         *(dp + 5) = (png_byte)(v & 0xff);
3507                      }
3508                   }
3509                }
3510             }
3511             break;
3512          }
3513
3514          default:
3515             break;
3516       }
3517
3518       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
3519       {
3520          row_info->color_type = (png_byte)(row_info->color_type &
3521              ~PNG_COLOR_MASK_ALPHA);
3522          row_info->channels--;
3523          row_info->pixel_depth = (png_byte)(row_info->channels *
3524              row_info->bit_depth);
3525          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3526       }
3527    }
3528 }
3529 #endif
3530
3531 #ifdef PNG_READ_GAMMA_SUPPORTED
3532 /* Gamma correct the image, avoiding the alpha channel.  Make sure
3533  * you do this after you deal with the transparency issue on grayscale
3534  * or RGB images. If your bit depth is 8, use gamma_table, if it
3535  * is 16, use gamma_16_table and gamma_shift.  Build these with
3536  * build_gamma_table().
3537  */
3538 void /* PRIVATE */
3539 png_do_gamma(png_row_infop row_info, png_bytep row,
3540     png_const_bytep gamma_table, png_const_uint_16pp gamma_16_table,
3541     int gamma_shift)
3542 {
3543    png_bytep sp;
3544    png_uint_32 i;
3545    png_uint_32 row_width=row_info->width;
3546
3547    png_debug(1, "in png_do_gamma");
3548
3549    if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
3550        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
3551    {
3552       switch (row_info->color_type)
3553       {
3554          case PNG_COLOR_TYPE_RGB:
3555          {
3556             if (row_info->bit_depth == 8)
3557             {
3558                sp = row;
3559                for (i = 0; i < row_width; i++)
3560                {
3561                   *sp = gamma_table[*sp];
3562                   sp++;
3563                   *sp = gamma_table[*sp];
3564                   sp++;
3565                   *sp = gamma_table[*sp];
3566                   sp++;
3567                }
3568             }
3569
3570             else /* if (row_info->bit_depth == 16) */
3571             {
3572                sp = row;
3573                for (i = 0; i < row_width; i++)
3574                {
3575                   png_uint_16 v;
3576
3577                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3578                   *sp = (png_byte)((v >> 8) & 0xff);
3579                   *(sp + 1) = (png_byte)(v & 0xff);
3580                   sp += 2;
3581
3582                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3583                   *sp = (png_byte)((v >> 8) & 0xff);
3584                   *(sp + 1) = (png_byte)(v & 0xff);
3585                   sp += 2;
3586
3587                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3588                   *sp = (png_byte)((v >> 8) & 0xff);
3589                   *(sp + 1) = (png_byte)(v & 0xff);
3590                   sp += 2;
3591                }
3592             }
3593             break;
3594          }
3595
3596          case PNG_COLOR_TYPE_RGB_ALPHA:
3597          {
3598             if (row_info->bit_depth == 8)
3599             {
3600                sp = row;
3601                for (i = 0; i < row_width; i++)
3602                {
3603                   *sp = gamma_table[*sp];
3604                   sp++;
3605
3606                   *sp = gamma_table[*sp];
3607                   sp++;
3608
3609                   *sp = gamma_table[*sp];
3610                   sp++;
3611
3612                   sp++;
3613                }
3614             }
3615
3616             else /* if (row_info->bit_depth == 16) */
3617             {
3618                sp = row;
3619                for (i = 0; i < row_width; i++)
3620                {
3621                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3622                   *sp = (png_byte)((v >> 8) & 0xff);
3623                   *(sp + 1) = (png_byte)(v & 0xff);
3624                   sp += 2;
3625
3626                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3627                   *sp = (png_byte)((v >> 8) & 0xff);
3628                   *(sp + 1) = (png_byte)(v & 0xff);
3629                   sp += 2;
3630
3631                   v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3632                   *sp = (png_byte)((v >> 8) & 0xff);
3633                   *(sp + 1) = (png_byte)(v & 0xff);
3634                   sp += 4;
3635                }
3636             }
3637             break;
3638          }
3639
3640          case PNG_COLOR_TYPE_GRAY_ALPHA:
3641          {
3642             if (row_info->bit_depth == 8)
3643             {
3644                sp = row;
3645                for (i = 0; i < row_width; i++)
3646                {
3647                   *sp = gamma_table[*sp];
3648                   sp += 2;
3649                }
3650             }
3651
3652             else /* if (row_info->bit_depth == 16) */
3653             {
3654                sp = row;
3655                for (i = 0; i < row_width; i++)
3656                {
3657                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3658                   *sp = (png_byte)((v >> 8) & 0xff);
3659                   *(sp + 1) = (png_byte)(v & 0xff);
3660                   sp += 4;
3661                }
3662             }
3663             break;
3664          }
3665
3666          case PNG_COLOR_TYPE_GRAY:
3667          {
3668             if (row_info->bit_depth == 2)
3669             {
3670                sp = row;
3671                for (i = 0; i < row_width; i += 4)
3672                {
3673                   int a = *sp & 0xc0;
3674                   int b = *sp & 0x30;
3675                   int c = *sp & 0x0c;
3676                   int d = *sp & 0x03;
3677
3678                   *sp = (png_byte)(
3679                       ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
3680                       ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
3681                       ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3682                       ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3683                   sp++;
3684                }
3685             }
3686
3687             if (row_info->bit_depth == 4)
3688             {
3689                sp = row;
3690                for (i = 0; i < row_width; i += 2)
3691                {
3692                   int msb = *sp & 0xf0;
3693                   int lsb = *sp & 0x0f;
3694
3695                   *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
3696                       | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
3697                   sp++;
3698                }
3699             }
3700
3701             else if (row_info->bit_depth == 8)
3702             {
3703                sp = row;
3704                for (i = 0; i < row_width; i++)
3705                {
3706                   *sp = gamma_table[*sp];
3707                   sp++;
3708                }
3709             }
3710
3711             else if (row_info->bit_depth == 16)
3712             {
3713                sp = row;
3714                for (i = 0; i < row_width; i++)
3715                {
3716                   png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
3717                   *sp = (png_byte)((v >> 8) & 0xff);
3718                   *(sp + 1) = (png_byte)(v & 0xff);
3719                   sp += 2;
3720                }
3721             }
3722             break;
3723          }
3724
3725          default:
3726             break;
3727       }
3728    }
3729 }
3730 #endif
3731
3732 #ifdef PNG_READ_EXPAND_SUPPORTED
3733 /* Expands a palette row to an RGB or RGBA row depending
3734  * upon whether you supply trans and num_trans.
3735  */
3736 void /* PRIVATE */
3737 png_do_expand_palette(png_row_infop row_info, png_bytep row,
3738    png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
3739 {
3740    int shift, value;
3741    png_bytep sp, dp;
3742    png_uint_32 i;
3743    png_uint_32 row_width=row_info->width;
3744
3745    png_debug(1, "in png_do_expand_palette");
3746
3747    if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
3748    {
3749       if (row_info->bit_depth < 8)
3750       {
3751          switch (row_info->bit_depth)
3752          {
3753             case 1:
3754             {
3755                sp = row + (png_size_t)((row_width - 1) >> 3);
3756                dp = row + (png_size_t)row_width - 1;
3757                shift = 7 - (int)((row_width + 7) & 0x07);
3758                for (i = 0; i < row_width; i++)
3759                {
3760                   if ((*sp >> shift) & 0x01)
3761                      *dp = 1;
3762
3763                   else
3764                      *dp = 0;
3765
3766                   if (shift == 7)
3767                   {
3768                      shift = 0;
3769                      sp--;
3770                   }
3771
3772                   else
3773                      shift++;
3774
3775                   dp--;
3776                }
3777                break;
3778             }
3779
3780             case 2:
3781             {
3782                sp = row + (png_size_t)((row_width - 1) >> 2);
3783                dp = row + (png_size_t)row_width - 1;
3784                shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3785                for (i = 0; i < row_width; i++)
3786                {
3787                   value = (*sp >> shift) & 0x03;
3788                   *dp = (png_byte)value;
3789                   if (shift == 6)
3790                   {
3791                      shift = 0;
3792                      sp--;
3793                   }
3794
3795                   else
3796                      shift += 2;
3797
3798                   dp--;
3799                }
3800                break;
3801             }
3802
3803             case 4:
3804             {
3805                sp = row + (png_size_t)((row_width - 1) >> 1);
3806                dp = row + (png_size_t)row_width - 1;
3807                shift = (int)((row_width & 0x01) << 2);
3808                for (i = 0; i < row_width; i++)
3809                {
3810                   value = (*sp >> shift) & 0x0f;
3811                   *dp = (png_byte)value;
3812                   if (shift == 4)
3813                   {
3814                      shift = 0;
3815                      sp--;
3816                   }
3817
3818                   else
3819                      shift += 4;
3820
3821                   dp--;
3822                }
3823                break;
3824             }
3825
3826             default:
3827                break;
3828          }
3829          row_info->bit_depth = 8;
3830          row_info->pixel_depth = 8;
3831          row_info->rowbytes = row_width;
3832       }
3833
3834       if (row_info->bit_depth == 8)
3835       {
3836          {
3837             if (trans_alpha != NULL)
3838             {
3839                sp = row + (png_size_t)row_width - 1;
3840                dp = row + (png_size_t)(row_width << 2) - 1;
3841
3842                for (i = 0; i < row_width; i++)
3843                {
3844                   if ((int)(*sp) >= num_trans)
3845                      *dp-- = 0xff;
3846
3847                   else
3848                      *dp-- = trans_alpha[*sp];
3849
3850                   *dp-- = palette[*sp].blue;
3851                   *dp-- = palette[*sp].green;
3852                   *dp-- = palette[*sp].red;
3853                   sp--;
3854                }
3855                row_info->bit_depth = 8;
3856                row_info->pixel_depth = 32;
3857                row_info->rowbytes = row_width * 4;
3858                row_info->color_type = 6;
3859                row_info->channels = 4;
3860             }
3861
3862             else
3863             {
3864                sp = row + (png_size_t)row_width - 1;
3865                dp = row + (png_size_t)(row_width * 3) - 1;
3866
3867                for (i = 0; i < row_width; i++)
3868                {
3869                   *dp-- = palette[*sp].blue;
3870                   *dp-- = palette[*sp].green;
3871                   *dp-- = palette[*sp].red;
3872                   sp--;
3873                }
3874
3875                row_info->bit_depth = 8;
3876                row_info->pixel_depth = 24;
3877                row_info->rowbytes = row_width * 3;
3878                row_info->color_type = 2;
3879                row_info->channels = 3;
3880             }
3881          }
3882       }
3883    }
3884 }
3885
3886 /* If the bit depth < 8, it is expanded to 8.  Also, if the already
3887  * expanded transparency value is supplied, an alpha channel is built.
3888  */
3889 void /* PRIVATE */
3890 png_do_expand(png_row_infop row_info, png_bytep row,
3891     png_const_color_16p trans_value)
3892 {
3893    int shift, value;
3894    png_bytep sp, dp;
3895    png_uint_32 i;
3896    png_uint_32 row_width=row_info->width;
3897
3898    png_debug(1, "in png_do_expand");
3899
3900    {
3901       if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3902       {
3903          png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
3904
3905          if (row_info->bit_depth < 8)
3906          {
3907             switch (row_info->bit_depth)
3908             {
3909                case 1:
3910                {
3911                   gray = (png_uint_16)((gray & 0x01) * 0xff);
3912                   sp = row + (png_size_t)((row_width - 1) >> 3);
3913                   dp = row + (png_size_t)row_width - 1;
3914                   shift = 7 - (int)((row_width + 7) & 0x07);
3915                   for (i = 0; i < row_width; i++)
3916                   {
3917                      if ((*sp >> shift) & 0x01)
3918                         *dp = 0xff;
3919
3920                      else
3921                         *dp = 0;
3922
3923                      if (shift == 7)
3924                      {
3925                         shift = 0;
3926                         sp--;
3927                      }
3928
3929                      else
3930                         shift++;
3931
3932                      dp--;
3933                   }
3934                   break;
3935                }
3936
3937                case 2:
3938                {
3939                   gray = (png_uint_16)((gray & 0x03) * 0x55);
3940                   sp = row + (png_size_t)((row_width - 1) >> 2);
3941                   dp = row + (png_size_t)row_width - 1;
3942                   shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3943                   for (i = 0; i < row_width; i++)
3944                   {
3945                      value = (*sp >> shift) & 0x03;
3946                      *dp = (png_byte)(value | (value << 2) | (value << 4) |
3947                         (value << 6));
3948                      if (shift == 6)
3949                      {
3950                         shift = 0;
3951                         sp--;
3952                      }
3953
3954                      else
3955                         shift += 2;
3956
3957                      dp--;
3958                   }
3959                   break;
3960                }
3961
3962                case 4:
3963                {
3964                   gray = (png_uint_16)((gray & 0x0f) * 0x11);
3965                   sp = row + (png_size_t)((row_width - 1) >> 1);
3966                   dp = row + (png_size_t)row_width - 1;
3967                   shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3968                   for (i = 0; i < row_width; i++)
3969                   {
3970                      value = (*sp >> shift) & 0x0f;
3971                      *dp = (png_byte)(value | (value << 4));
3972                      if (shift == 4)
3973                      {
3974                         shift = 0;
3975                         sp--;
3976                      }
3977
3978                      else
3979                         shift = 4;
3980
3981                      dp--;
3982                   }
3983                   break;
3984                }
3985
3986                default:
3987                   break;
3988             }
3989
3990             row_info->bit_depth = 8;
3991             row_info->pixel_depth = 8;
3992             row_info->rowbytes = row_width;
3993          }
3994
3995          if (trans_value != NULL)
3996          {
3997             if (row_info->bit_depth == 8)
3998             {
3999                gray = gray & 0xff;
4000                sp = row + (png_size_t)row_width - 1;
4001                dp = row + (png_size_t)(row_width << 1) - 1;
4002
4003                for (i = 0; i < row_width; i++)
4004                {
4005                   if (*sp == gray)
4006                      *dp-- = 0;
4007
4008                   else
4009                      *dp-- = 0xff;
4010
4011                   *dp-- = *sp--;
4012                }
4013             }
4014
4015             else if (row_info->bit_depth == 16)
4016             {
4017                png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
4018                png_byte gray_low = (png_byte)(gray & 0xff);
4019                sp = row + row_info->rowbytes - 1;
4020                dp = row + (row_info->rowbytes << 1) - 1;
4021                for (i = 0; i < row_width; i++)
4022                {
4023                   if (*(sp - 1) == gray_high && *(sp) == gray_low)
4024                   {
4025                      *dp-- = 0;
4026                      *dp-- = 0;
4027                   }
4028
4029                   else
4030                   {
4031                      *dp-- = 0xff;
4032                      *dp-- = 0xff;
4033                   }
4034
4035                   *dp-- = *sp--;
4036                   *dp-- = *sp--;
4037                }
4038             }
4039
4040             row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4041             row_info->channels = 2;
4042             row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4043             row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4044                row_width);
4045          }
4046       }
4047       else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
4048       {
4049          if (row_info->bit_depth == 8)
4050          {
4051             png_byte red = (png_byte)(trans_value->red & 0xff);
4052             png_byte green = (png_byte)(trans_value->green & 0xff);
4053             png_byte blue = (png_byte)(trans_value->blue & 0xff);
4054             sp = row + (png_size_t)row_info->rowbytes - 1;
4055             dp = row + (png_size_t)(row_width << 2) - 1;
4056             for (i = 0; i < row_width; i++)
4057             {
4058                if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4059                   *dp-- = 0;
4060
4061                else
4062                   *dp-- = 0xff;
4063
4064                *dp-- = *sp--;
4065                *dp-- = *sp--;
4066                *dp-- = *sp--;
4067             }
4068          }
4069          else if (row_info->bit_depth == 16)
4070          {
4071             png_byte red_high = (png_byte)((trans_value->red >> 8) & 0xff);
4072             png_byte green_high = (png_byte)((trans_value->green >> 8) & 0xff);
4073             png_byte blue_high = (png_byte)((trans_value->blue >> 8) & 0xff);
4074             png_byte red_low = (png_byte)(trans_value->red & 0xff);
4075             png_byte green_low = (png_byte)(trans_value->green & 0xff);
4076             png_byte blue_low = (png_byte)(trans_value->blue & 0xff);
4077             sp = row + row_info->rowbytes - 1;
4078             dp = row + (png_size_t)(row_width << 3) - 1;
4079             for (i = 0; i < row_width; i++)
4080             {
4081                if (*(sp - 5) == red_high &&
4082                    *(sp - 4) == red_low &&
4083                    *(sp - 3) == green_high &&
4084                    *(sp - 2) == green_low &&
4085                    *(sp - 1) == blue_high &&
4086                    *(sp    ) == blue_low)
4087                {
4088                   *dp-- = 0;
4089                   *dp-- = 0;
4090                }
4091
4092                else
4093                {
4094                   *dp-- = 0xff;
4095                   *dp-- = 0xff;
4096                }
4097
4098                *dp-- = *sp--;
4099                *dp-- = *sp--;
4100                *dp-- = *sp--;
4101                *dp-- = *sp--;
4102                *dp-- = *sp--;
4103                *dp-- = *sp--;
4104             }
4105          }
4106          row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4107          row_info->channels = 4;
4108          row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4109          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4110       }
4111    }
4112 }
4113 #endif
4114
4115 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4116 /* If the bit depth is 8 and the colour type is not a palette type expand the
4117  * whole row to 16 bits.  Has no effect otherwise.
4118  */
4119 void /* PRIVATE */
4120 png_do_expand_16(png_row_infop row_info, png_bytep row)
4121 {
4122    if (row_info->bit_depth == 8 &&
4123       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4124    {
4125       /* The row have a sequence of bytes containing [0..255] and we need
4126        * to turn it into another row containing [0..65535], to do this we
4127        * calculate:
4128        *
4129        *  (input / 255) * 65535
4130        *
4131        *  Which happens to be exactly input * 257 and this can be achieved
4132        *  simply by byte replication in place (copying backwards).
4133        */
4134       png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4135       png_byte *dp = sp + row_info->rowbytes;  /* destination, end + 1 */
4136       while (dp > sp)
4137          dp[-2] = dp[-1] = *--sp, dp -= 2;
4138
4139       row_info->rowbytes *= 2;
4140       row_info->bit_depth = 16;
4141       row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4142    }
4143 }
4144 #endif
4145
4146 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4147 void /* PRIVATE */
4148 png_do_quantize(png_row_infop row_info, png_bytep row,
4149     png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4150 {
4151    png_bytep sp, dp;
4152    png_uint_32 i;
4153    png_uint_32 row_width=row_info->width;
4154
4155    png_debug(1, "in png_do_quantize");
4156
4157    if (row_info->bit_depth == 8)
4158    {
4159       if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
4160       {
4161          int r, g, b, p;
4162          sp = row;
4163          dp = row;
4164          for (i = 0; i < row_width; i++)
4165          {
4166             r = *sp++;
4167             g = *sp++;
4168             b = *sp++;
4169
4170             /* This looks real messy, but the compiler will reduce
4171              * it down to a reasonable formula.  For example, with
4172              * 5 bits per color, we get:
4173              * p = (((r >> 3) & 0x1f) << 10) |
4174              *    (((g >> 3) & 0x1f) << 5) |
4175              *    ((b >> 3) & 0x1f);
4176              */
4177             p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4178                 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4179                 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4180                 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4181                 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4182                 (PNG_QUANTIZE_BLUE_BITS)) |
4183                 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4184                 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4185
4186             *dp++ = palette_lookup[p];
4187          }
4188
4189          row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4190          row_info->channels = 1;
4191          row_info->pixel_depth = row_info->bit_depth;
4192          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4193       }
4194
4195       else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
4196          palette_lookup != NULL)
4197       {
4198          int r, g, b, p;
4199          sp = row;
4200          dp = row;
4201          for (i = 0; i < row_width; i++)
4202          {
4203             r = *sp++;
4204             g = *sp++;
4205             b = *sp++;
4206             sp++;
4207
4208             p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
4209                 ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
4210                 (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
4211                 (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
4212                 ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
4213                 (PNG_QUANTIZE_BLUE_BITS)) |
4214                 ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
4215                 ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
4216
4217             *dp++ = palette_lookup[p];
4218          }
4219
4220          row_info->color_type = PNG_COLOR_TYPE_PALETTE;
4221          row_info->channels = 1;
4222          row_info->pixel_depth = row_info->bit_depth;
4223          row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4224       }
4225
4226       else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4227          quantize_lookup)
4228       {
4229          sp = row;
4230
4231          for (i = 0; i < row_width; i++, sp++)
4232          {
4233             *sp = quantize_lookup[*sp];
4234          }
4235       }
4236    }
4237 }
4238 #endif /* PNG_READ_QUANTIZE_SUPPORTED */
4239
4240 #ifdef PNG_MNG_FEATURES_SUPPORTED
4241 /* Undoes intrapixel differencing  */
4242 void /* PRIVATE */
4243 png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
4244 {
4245    png_debug(1, "in png_do_read_intrapixel");
4246
4247    if (
4248        (row_info->color_type & PNG_COLOR_MASK_COLOR))
4249    {
4250       int bytes_per_pixel;
4251       png_uint_32 row_width = row_info->width;
4252
4253       if (row_info->bit_depth == 8)
4254       {
4255          png_bytep rp;
4256          png_uint_32 i;
4257
4258          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4259             bytes_per_pixel = 3;
4260
4261          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4262             bytes_per_pixel = 4;
4263
4264          else
4265             return;
4266
4267          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4268          {
4269             *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
4270             *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
4271          }
4272       }
4273       else if (row_info->bit_depth == 16)
4274       {
4275          png_bytep rp;
4276          png_uint_32 i;
4277
4278          if (row_info->color_type == PNG_COLOR_TYPE_RGB)
4279             bytes_per_pixel = 6;
4280
4281          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4282             bytes_per_pixel = 8;
4283
4284          else
4285             return;
4286
4287          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
4288          {
4289             png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
4290             png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
4291             png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
4292             png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
4293             png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4294             *(rp    ) = (png_byte)((red >> 8) & 0xff);
4295             *(rp + 1) = (png_byte)(red & 0xff);
4296             *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
4297             *(rp + 5) = (png_byte)(blue & 0xff);
4298          }
4299       }
4300    }
4301 }
4302 #endif /* PNG_MNG_FEATURES_SUPPORTED */
4303 #endif /* PNG_READ_SUPPORTED */