pngrtran.c 137.1 KB
Newer Older
G
Guy Schalnat 已提交
1

A
Andreas Dilger 已提交
2
/* pngrtran.c - transforms the data in a row for PNG readers
3
 *
4
 * libpng 1.2.1 - December 12, 2001
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2001 Glenn Randers-Pehrson
7 8
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9
 *
10
 * This file contains functions optionally called by an application
11
 * in order to tell libpng how to handle data when reading a PNG.
12
 * Transformations that are used in both reading and writing are
13 14
 * in pngtrans.c.
 */
G
Guy Schalnat 已提交
15 16 17 18

#define PNG_INTERNAL
#include "png.h"

19
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
20
void PNGAPI
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
{
   png_debug(1, "in png_set_crc_action\n");
   /* Tell libpng how we react to CRC errors in critical chunks */
   switch (crit_action)
   {
      case PNG_CRC_NO_CHANGE:                        /* leave setting as is */
         break;
      case PNG_CRC_WARN_USE:                               /* warn/use data */
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
         break;
      case PNG_CRC_QUIET_USE:                             /* quiet/use data */
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
                           PNG_FLAG_CRC_CRITICAL_IGNORE;
         break;
      case PNG_CRC_WARN_DISCARD:    /* not a valid action for critical data */
         png_warning(png_ptr, "Can't discard critical data on CRC error.");
      case PNG_CRC_ERROR_QUIT:                                /* error/quit */
      case PNG_CRC_DEFAULT:
      default:
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
         break;
   }

   switch (ancil_action)
   {
      case PNG_CRC_NO_CHANGE:                       /* leave setting as is */
         break;
      case PNG_CRC_WARN_USE:                              /* warn/use data */
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
         break;
      case PNG_CRC_QUIET_USE:                            /* quiet/use data */
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
         break;
      case PNG_CRC_ERROR_QUIT:                               /* error/quit */
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
         break;
      case PNG_CRC_WARN_DISCARD:                      /* warn/discard data */
      case PNG_CRC_DEFAULT:
      default:
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
         break;
   }
}

72 73
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
    defined(PNG_FLOATING_POINT_SUPPORTED)
G
Guy Schalnat 已提交
74
/* handle alpha and tRNS via a background color */
75
void PNGAPI
G
Guy Schalnat 已提交
76 77
png_set_background(png_structp png_ptr,
   png_color_16p background_color, int background_gamma_code,
G
Guy Schalnat 已提交
78
   int need_expand, double background_gamma)
G
Guy Schalnat 已提交
79
{
A
Andreas Dilger 已提交
80
   png_debug(1, "in png_set_background\n");
A
Andreas Dilger 已提交
81 82 83 84 85 86
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
   {
      png_warning(png_ptr, "Application must supply a known background gamma");
      return;
   }

G
Guy Schalnat 已提交
87
   png_ptr->transformations |= PNG_BACKGROUND;
88
   png_memcpy(&(png_ptr->background), background_color, sizeof(png_color_16));
G
Guy Schalnat 已提交
89
   png_ptr->background_gamma = (float)background_gamma;
G
Guy Schalnat 已提交
90
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
G
Guy Schalnat 已提交
91
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
92 93 94 95

   /* Note:  if need_expand is set and color_type is either RGB or RGB_ALPHA
    * (in which case need_expand is superfluous anyway), the background color
    * might actually be gray yet not be flagged as such. This is not a problem
96
    * for the current code, which uses PNG_BACKGROUND_IS_GRAY only to
97 98 99 100 101
    * decide when to do the png_do_gray_to_rgb() transformation.
    */
   if ((need_expand && !(png_ptr->color_type & PNG_COLOR_MASK_COLOR)) ||
       (!need_expand && background_color->red == background_color->green &&
        background_color->red == background_color->blue))
102
      png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
G
Guy Schalnat 已提交
103
}
G
Guy Schalnat 已提交
104
#endif
G
Guy Schalnat 已提交
105

G
Guy Schalnat 已提交
106
#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
107
/* strip 16 bit depth files to 8 bit depth */
108
void PNGAPI
G
Guy Schalnat 已提交
109
png_set_strip_16(png_structp png_ptr)
G
Guy Schalnat 已提交
110
{
A
Andreas Dilger 已提交
111
   png_debug(1, "in png_set_strip_16\n");
G
Guy Schalnat 已提交
112 113
   png_ptr->transformations |= PNG_16_TO_8;
}
G
Guy Schalnat 已提交
114
#endif
G
Guy Schalnat 已提交
115

A
Andreas Dilger 已提交
116
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
117
void PNGAPI
A
Andreas Dilger 已提交
118 119 120 121 122 123 124
png_set_strip_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_strip_alpha\n");
   png_ptr->transformations |= PNG_STRIP_ALPHA;
}
#endif

G
Guy Schalnat 已提交
125
#if defined(PNG_READ_DITHER_SUPPORTED)
A
Andreas Dilger 已提交
126
/* Dither file to 8 bit.  Supply a palette, the current number
127 128 129 130 131 132 133
 * of elements in the palette, the maximum number of elements
 * allowed, and a histogram if possible.  If the current number
 * of colors is greater then the maximum number, the palette will be
 * modified to fit in the maximum number.  "full_dither" indicates
 * whether we need a dithering cube set up for RGB images, or if we
 * simply are reducing the number of colors in a paletted image.
 */
G
Guy Schalnat 已提交
134 135

typedef struct png_dsort_struct
G
Guy Schalnat 已提交
136
{
G
Guy Schalnat 已提交
137
   struct png_dsort_struct FAR * next;
G
Guy Schalnat 已提交
138 139
   png_byte left;
   png_byte right;
G
Guy Schalnat 已提交
140 141 142
} png_dsort;
typedef png_dsort FAR *       png_dsortp;
typedef png_dsort FAR * FAR * png_dsortpp;
G
Guy Schalnat 已提交
143

144
void PNGAPI
G
Guy Schalnat 已提交
145 146
png_set_dither(png_structp png_ptr, png_colorp palette,
   int num_palette, int maximum_colors, png_uint_16p histogram,
G
Guy Schalnat 已提交
147 148
   int full_dither)
{
A
Andreas Dilger 已提交
149
   png_debug(1, "in png_set_dither\n");
G
Guy Schalnat 已提交
150 151
   png_ptr->transformations |= PNG_DITHER;

G
Guy Schalnat 已提交
152
   if (!full_dither)
G
Guy Schalnat 已提交
153 154 155
   {
      int i;

A
Andreas Dilger 已提交
156
      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
157
         (png_uint_32)(num_palette * sizeof (png_byte)));
G
Guy Schalnat 已提交
158
      for (i = 0; i < num_palette; i++)
G
Guy Schalnat 已提交
159
         png_ptr->dither_index[i] = (png_byte)i;
G
Guy Schalnat 已提交
160 161 162 163
   }

   if (num_palette > maximum_colors)
   {
A
Andreas Dilger 已提交
164
      if (histogram != NULL)
G
Guy Schalnat 已提交
165
      {
A
Andreas Dilger 已提交
166 167
         /* This is easy enough, just throw out the least used colors.
            Perhaps not the best solution, but good enough. */
G
Guy Schalnat 已提交
168 169

         int i;
G
Guy Schalnat 已提交
170
         png_bytep sort;
G
Guy Schalnat 已提交
171 172

         /* initialize an array to sort colors */
173 174
         sort = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette
            * sizeof (png_byte)));
G
Guy Schalnat 已提交
175 176 177

         /* initialize the sort array */
         for (i = 0; i < num_palette; i++)
G
Guy Schalnat 已提交
178
            sort[i] = (png_byte)i;
G
Guy Schalnat 已提交
179

A
Andreas Dilger 已提交
180
         /* Find the least used palette entries by starting a
G
Guy Schalnat 已提交
181 182 183 184 185 186 187 188
            bubble sort, and running it until we have sorted
            out enough colors.  Note that we don't care about
            sorting all the colors, just finding which are
            least used. */

         for (i = num_palette - 1; i >= maximum_colors; i--)
         {
            int done; /* to stop early if the list is pre-sorted */
G
Guy Schalnat 已提交
189
            int j;
G
Guy Schalnat 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

            done = 1;
            for (j = 0; j < i; j++)
            {
               if (histogram[sort[j]] < histogram[sort[j + 1]])
               {
                  png_byte t;

                  t = sort[j];
                  sort[j] = sort[j + 1];
                  sort[j + 1] = t;
                  done = 0;
               }
            }
            if (done)
               break;
         }

         /* swap the palette around, and set up a table, if necessary */
         if (full_dither)
         {
211
            int j = num_palette;
G
Guy Schalnat 已提交
212 213 214

            /* put all the useful colors within the max, but don't
               move the others */
215
            for (i = 0; i < maximum_colors; i++)
G
Guy Schalnat 已提交
216
            {
217
               if ((int)sort[i] >= maximum_colors)
G
Guy Schalnat 已提交
218 219 220
               {
                  do
                     j--;
221
                  while ((int)sort[j] >= maximum_colors);
G
Guy Schalnat 已提交
222 223
                  palette[i] = palette[j];
               }
G
Guy Schalnat 已提交
224
            }
G
Guy Schalnat 已提交
225 226 227
         }
         else
         {
228
            int j = num_palette;
G
Guy Schalnat 已提交
229

G
Guy Schalnat 已提交
230
            /* move all the used colors inside the max limit, and
G
Guy Schalnat 已提交
231
               develop a translation table */
232
            for (i = 0; i < maximum_colors; i++)
G
Guy Schalnat 已提交
233 234
            {
               /* only move the colors we need to */
235
               if ((int)sort[i] >= maximum_colors)
G
Guy Schalnat 已提交
236 237 238 239 240
               {
                  png_color tmp_color;

                  do
                     j--;
241
                  while ((int)sort[j] >= maximum_colors);
G
Guy Schalnat 已提交
242 243 244 245 246

                  tmp_color = palette[j];
                  palette[j] = palette[i];
                  palette[i] = tmp_color;
                  /* indicate where the color went */
G
Guy Schalnat 已提交
247 248
                  png_ptr->dither_index[j] = (png_byte)i;
                  png_ptr->dither_index[i] = (png_byte)j;
G
Guy Schalnat 已提交
249 250
               }
            }
A
Andreas Dilger 已提交
251 252

            /* find closest color for those colors we are not using */
G
Guy Schalnat 已提交
253 254
            for (i = 0; i < num_palette; i++)
            {
255
               if ((int)png_ptr->dither_index[i] >= maximum_colors)
G
Guy Schalnat 已提交
256
               {
A
Andreas Dilger 已提交
257
                  int min_d, k, min_k, d_index;
G
Guy Schalnat 已提交
258

G
Guy Schalnat 已提交
259
                  /* find the closest color to one we threw out */
A
Andreas Dilger 已提交
260 261 262
                  d_index = png_ptr->dither_index[i];
                  min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
                  for (k = 1, min_k = 0; k < maximum_colors; k++)
G
Guy Schalnat 已提交
263 264 265
                  {
                     int d;

A
Andreas Dilger 已提交
266
                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
G
Guy Schalnat 已提交
267 268 269 270

                     if (d < min_d)
                     {
                        min_d = d;
A
Andreas Dilger 已提交
271
                        min_k = k;
G
Guy Schalnat 已提交
272 273
                     }
                  }
G
Guy Schalnat 已提交
274
                  /* point to closest color */
A
Andreas Dilger 已提交
275
                  png_ptr->dither_index[i] = (png_byte)min_k;
G
Guy Schalnat 已提交
276 277 278
               }
            }
         }
A
Andreas Dilger 已提交
279
         png_free(png_ptr, sort);
G
Guy Schalnat 已提交
280 281 282
      }
      else
      {
A
Andreas Dilger 已提交
283
         /* This is much harder to do simply (and quickly).  Perhaps
G
Guy Schalnat 已提交
284 285 286 287
            we need to go through a median cut routine, but those
            don't always behave themselves with only a few colors
            as input.  So we will just find the closest two colors,
            and throw out one of them (chosen somewhat randomly).
288 289
            [We don't understand this at all, so if someone wants to
             work on improving it, be our guest - AED, GRP]
G
Guy Schalnat 已提交
290 291 292 293
            */
         int i;
         int max_d;
         int num_new_palette;
G
Guy Schalnat 已提交
294 295
         png_dsortpp hash;
         png_bytep index_to_palette;
G
Guy Schalnat 已提交
296
            /* where the original index currently is in the palette */
G
Guy Schalnat 已提交
297
         png_bytep palette_to_index;
G
Guy Schalnat 已提交
298 299 300
            /* which original index points to this palette color */

         /* initialize palette index arrays */
A
Andreas Dilger 已提交
301
         index_to_palette = (png_bytep)png_malloc(png_ptr,
302
            (png_uint_32)(num_palette * sizeof (png_byte)));
A
Andreas Dilger 已提交
303
         palette_to_index = (png_bytep)png_malloc(png_ptr,
304
            (png_uint_32)(num_palette * sizeof (png_byte)));
G
Guy Schalnat 已提交
305 306 307 308

         /* initialize the sort array */
         for (i = 0; i < num_palette; i++)
         {
G
Guy Schalnat 已提交
309 310
            index_to_palette[i] = (png_byte)i;
            palette_to_index[i] = (png_byte)i;
G
Guy Schalnat 已提交
311 312
         }

313
         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
314
            sizeof (png_dsortp)));
G
Guy Schalnat 已提交
315
         for (i = 0; i < 769; i++)
A
Andreas Dilger 已提交
316
            hash[i] = NULL;
G
Guy Schalnat 已提交
317
/*         png_memset(hash, 0, 769 * sizeof (png_dsortp)); */
G
Guy Schalnat 已提交
318 319 320 321 322 323 324 325 326 327 328 329 330

         num_new_palette = num_palette;

         /* initial wild guess at how far apart the farthest pixel
            pair we will be eliminating will be.  Larger
            numbers mean more areas will be allocated, Smaller
            numbers run the risk of not saving enough data, and
            having to do this all over again.

            I have not done extensive checking on this number.
            */
         max_d = 96;

G
Guy Schalnat 已提交
331
         while (num_new_palette > maximum_colors)
G
Guy Schalnat 已提交
332 333 334 335 336 337 338 339 340
         {
            for (i = 0; i < num_new_palette - 1; i++)
            {
               int j;

               for (j = i + 1; j < num_new_palette; j++)
               {
                  int d;

G
Guy Schalnat 已提交
341
                  d = PNG_COLOR_DIST(palette[i], palette[j]);
G
Guy Schalnat 已提交
342 343 344

                  if (d <= max_d)
                  {
G
Guy Schalnat 已提交
345
                     png_dsortp t;
G
Guy Schalnat 已提交
346

347 348
                     t = (png_dsortp)png_malloc(png_ptr, (png_uint_32)(sizeof
                         (png_dsort)));
G
Guy Schalnat 已提交
349
                     t->next = hash[d];
G
Guy Schalnat 已提交
350 351
                     t->left = (png_byte)i;
                     t->right = (png_byte)j;
G
Guy Schalnat 已提交
352 353 354 355 356 357 358
                     hash[d] = t;
                  }
               }
            }

            for (i = 0; i <= max_d; i++)
            {
A
Andreas Dilger 已提交
359
               if (hash[i] != NULL)
G
Guy Schalnat 已提交
360
               {
G
Guy Schalnat 已提交
361
                  png_dsortp p;
G
Guy Schalnat 已提交
362 363 364

                  for (p = hash[i]; p; p = p->next)
                  {
365 366
                     if ((int)index_to_palette[p->left] < num_new_palette &&
                        (int)index_to_palette[p->right] < num_new_palette)
G
Guy Schalnat 已提交
367 368 369
                     {
                        int j, next_j;

370
                        if (num_new_palette & 0x01)
G
Guy Schalnat 已提交
371 372 373 374 375 376 377 378 379 380 381
                        {
                           j = p->left;
                           next_j = p->right;
                        }
                        else
                        {
                           j = p->right;
                           next_j = p->left;
                        }

                        num_new_palette--;
A
Andreas Dilger 已提交
382
                        palette[index_to_palette[j]] = palette[num_new_palette];
G
Guy Schalnat 已提交
383 384 385 386 387 388 389 390 391 392
                        if (!full_dither)
                        {
                           int k;

                           for (k = 0; k < num_palette; k++)
                           {
                              if (png_ptr->dither_index[k] ==
                                 index_to_palette[j])
                                 png_ptr->dither_index[k] =
                                    index_to_palette[next_j];
393
                              if ((int)png_ptr->dither_index[k] ==
G
Guy Schalnat 已提交
394 395 396 397 398 399 400 401 402 403 404
                                 num_new_palette)
                                 png_ptr->dither_index[k] =
                                    index_to_palette[j];
                           }
                        }

                        index_to_palette[palette_to_index[num_new_palette]] =
                           index_to_palette[j];
                        palette_to_index[index_to_palette[j]] =
                           palette_to_index[num_new_palette];

G
Guy Schalnat 已提交
405 406
                        index_to_palette[j] = (png_byte)num_new_palette;
                        palette_to_index[num_new_palette] = (png_byte)j;
G
Guy Schalnat 已提交
407 408 409 410 411 412 413 414 415 416 417
                     }
                     if (num_new_palette <= maximum_colors)
                        break;
                  }
                  if (num_new_palette <= maximum_colors)
                     break;
               }
            }

            for (i = 0; i < 769; i++)
            {
A
Andreas Dilger 已提交
418
               if (hash[i] != NULL)
G
Guy Schalnat 已提交
419
               {
420
                  png_dsortp p = hash[i];
G
Guy Schalnat 已提交
421 422
                  while (p)
                  {
G
Guy Schalnat 已提交
423
                     png_dsortp t;
G
Guy Schalnat 已提交
424 425

                     t = p->next;
A
Andreas Dilger 已提交
426
                     png_free(png_ptr, p);
G
Guy Schalnat 已提交
427 428 429 430 431 432 433
                     p = t;
                  }
               }
               hash[i] = 0;
            }
            max_d += 96;
         }
A
Andreas Dilger 已提交
434 435 436
         png_free(png_ptr, hash);
         png_free(png_ptr, palette_to_index);
         png_free(png_ptr, index_to_palette);
G
Guy Schalnat 已提交
437 438 439
      }
      num_palette = maximum_colors;
   }
A
Andreas Dilger 已提交
440
   if (png_ptr->palette == NULL)
G
Guy Schalnat 已提交
441
   {
G
Guy Schalnat 已提交
442 443
      png_ptr->palette = palette;
   }
G
Guy Schalnat 已提交
444
   png_ptr->num_palette = (png_uint_16)num_palette;
G
Guy Schalnat 已提交
445 446 447 448

   if (full_dither)
   {
      int i;
G
Guy Schalnat 已提交
449
      png_bytep distance;
450
      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
G
Guy Schalnat 已提交
451
         PNG_DITHER_BLUE_BITS;
452 453 454 455
      int num_red = (1 << PNG_DITHER_RED_BITS);
      int num_green = (1 << PNG_DITHER_GREEN_BITS);
      int num_blue = (1 << PNG_DITHER_BLUE_BITS);
      png_size_t num_entries = ((png_size_t)1 << total_bits);
G
Guy Schalnat 已提交
456

A
Andreas Dilger 已提交
457
      png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
458
         (png_uint_32)(num_entries * sizeof (png_byte)));
459 460

      png_memset(png_ptr->palette_lookup, 0, num_entries * sizeof (png_byte));
G
Guy Schalnat 已提交
461

462 463
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
         sizeof(png_byte)));
464

465 466 467 468
      png_memset(distance, 0xff, num_entries * sizeof(png_byte));

      for (i = 0; i < num_palette; i++)
      {
469 470 471 472
         int ir, ig, ib;
         int r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
         int g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
         int b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
G
Guy Schalnat 已提交
473 474 475

         for (ir = 0; ir < num_red; ir++)
         {
476 477
            int dr = abs(ir - r);
            int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
G
Guy Schalnat 已提交
478 479

            for (ig = 0; ig < num_green; ig++)
G
Guy Schalnat 已提交
480
            {
481 482 483 484
               int dg = abs(ig - g);
               int dt = dr + dg;
               int dm = ((dr > dg) ? dr : dg);
               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
G
Guy Schalnat 已提交
485 486

               for (ib = 0; ib < num_blue; ib++)
G
Guy Schalnat 已提交
487
               {
488 489 490 491
                  int d_index = index_g | ib;
                  int db = abs(ib - b);
                  int dmax = ((dm > db) ? dm : db);
                  int d = dmax + dt + db;
G
Guy Schalnat 已提交
492

493
                  if (d < (int)distance[d_index])
G
Guy Schalnat 已提交
494
                  {
A
Andreas Dilger 已提交
495 496
                     distance[d_index] = (png_byte)d;
                     png_ptr->palette_lookup[d_index] = (png_byte)i;
G
Guy Schalnat 已提交
497 498
                  }
               }
499 500 501
            }
         }
      }
G
Guy Schalnat 已提交
502

A
Andreas Dilger 已提交
503
      png_free(png_ptr, distance);
G
Guy Schalnat 已提交
504 505
   }
}
G
Guy Schalnat 已提交
506
#endif
G
Guy Schalnat 已提交
507

508
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
A
Andreas Dilger 已提交
509
/* Transform the image from the file_gamma to the screen_gamma.  We
510 511 512 513
 * only do transformations on images where the file_gamma and screen_gamma
 * are not close reciprocals, otherwise it slows things down slightly, and
 * also needlessly introduces small errors.
 */
514
void PNGAPI
515
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
G
Guy Schalnat 已提交
516
{
A
Andreas Dilger 已提交
517
   png_debug(1, "in png_set_gamma\n");
518
   if (fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD)
A
Andreas Dilger 已提交
519
      png_ptr->transformations |= PNG_GAMMA;
G
Guy Schalnat 已提交
520
   png_ptr->gamma = (float)file_gamma;
521
   png_ptr->screen_gamma = (float)scrn_gamma;
G
Guy Schalnat 已提交
522
}
G
Guy Schalnat 已提交
523
#endif
G
Guy Schalnat 已提交
524

G
Guy Schalnat 已提交
525
#if defined(PNG_READ_EXPAND_SUPPORTED)
526
/* Expand paletted images to RGB, expand grayscale images of
527
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
528 529
 * to alpha channels.
 */
530
void PNGAPI
G
Guy Schalnat 已提交
531
png_set_expand(png_structp png_ptr)
G
Guy Schalnat 已提交
532
{
A
Andreas Dilger 已提交
533
   png_debug(1, "in png_set_expand\n");
G
Guy Schalnat 已提交
534 535
   png_ptr->transformations |= PNG_EXPAND;
}
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

/* GRR 19990627:  the following three functions currently are identical
 *  to png_set_expand().  However, it is entirely reasonable that someone
 *  might wish to expand an indexed image to RGB but *not* expand a single,
 *  fully transparent palette entry to a full alpha channel--perhaps instead
 *  convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
 *  the transparent color with a particular RGB value, or drop tRNS entirely.
 *  IOW, a future version of the library may make the transformations flag
 *  a bit more fine-grained, with separate bits for each of these three
 *  functions.
 *
 *  More to the point, these functions make it obvious what libpng will be
 *  doing, whereas "expand" can (and does) mean any number of things.
 */

/* Expand paletted images to RGB. */
552
void PNGAPI
553 554 555 556 557 558 559
png_set_palette_to_rgb(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand\n");
   png_ptr->transformations |= PNG_EXPAND;
}

/* Expand grayscale images of less than 8-bit depth to 8 bits. */
560
void PNGAPI
561 562 563 564 565 566 567
png_set_gray_1_2_4_to_8(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand\n");
   png_ptr->transformations |= PNG_EXPAND;
}

/* Expand tRNS chunks to alpha channels. */
568
void PNGAPI
569 570 571 572 573 574
png_set_tRNS_to_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand\n");
   png_ptr->transformations |= PNG_EXPAND;
}
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
G
Guy Schalnat 已提交
575

G
Guy Schalnat 已提交
576
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
577
void PNGAPI
G
Guy Schalnat 已提交
578
png_set_gray_to_rgb(png_structp png_ptr)
G
Guy Schalnat 已提交
579
{
A
Andreas Dilger 已提交
580
   png_debug(1, "in png_set_gray_to_rgb\n");
G
Guy Schalnat 已提交
581 582
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
}
G
Guy Schalnat 已提交
583
#endif
G
Guy Schalnat 已提交
584

585 586
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
#if defined(PNG_FLOATING_POINT_SUPPORTED)
587 588
/* Convert a RGB image to a grayscale of the same width.  This allows us,
 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
589
 */
590

591
void PNGAPI
592
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
593 594 595 596 597 598 599 600
   double green)
{
      int red_fixed = (int)((float)red*100000.0 + 0.5);
      int green_fixed = (int)((float)green*100000.0 + 0.5);
      png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
}
#endif

601
void PNGAPI
602 603
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
   png_fixed_point red, png_fixed_point green)
A
Andreas Dilger 已提交
604
{
A
Andreas Dilger 已提交
605
   png_debug(1, "in png_set_rgb_to_gray\n");
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
   switch(error_action)
   {
      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
              break;
      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
              break;
      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
   }
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
#if defined(PNG_READ_EXPAND_SUPPORTED)
      png_ptr->transformations |= PNG_EXPAND;
#else
   {
      png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
   }
#endif
   {
624
      png_uint_16 red_int, green_int;
625
      if(red < 0 || green < 0)
626
      {
627 628
         red_int   =  6968; /* .212671 * 32768 + .5 */
         green_int = 23434; /* .715160 * 32768 + .5 */
629
      }
630 631 632 633 634 635
      else if(red + green < 100000L)
      {
        red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
        green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
      }
      else
636 637
      {
         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
638 639
         red_int   =  6968;
         green_int = 23434;
640
      }
641 642 643
      png_ptr->rgb_to_gray_red_coeff   = red_int;
      png_ptr->rgb_to_gray_green_coeff = green_int;
      png_ptr->rgb_to_gray_blue_coeff  = (png_uint_16)(32768-red_int-green_int);
644
   }
A
Andreas Dilger 已提交
645 646 647
}
#endif

648 649 650
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_LEGACY_SUPPORTED)
651
void PNGAPI
652 653 654 655
png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
   read_user_transform_fn)
{
   png_debug(1, "in png_set_read_user_transform_fn\n");
656
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
657 658
   png_ptr->transformations |= PNG_USER_TRANSFORM;
   png_ptr->read_user_transform_fn = read_user_transform_fn;
659 660 661 662 663 664
#endif
#ifdef PNG_LEGACY_SUPPORTED
   if(read_user_transform_fn)
      png_warning(png_ptr,
        "This version of libpng does not support user transforms");
#endif
665 666 667
}
#endif

A
Andreas Dilger 已提交
668
/* Initialize everything needed for the read.  This includes modifying
669 670
 * the palette.
 */
671
void /* PRIVATE */
G
Guy Schalnat 已提交
672
png_init_read_transformations(png_structp png_ptr)
G
Guy Schalnat 已提交
673
{
674
   png_debug(1, "in png_init_read_transformations\n");
675
#if defined(PNG_USELESS_TESTS_SUPPORTED)
676 677 678 679 680 681
   if(png_ptr != NULL)
#endif
  {
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
 || defined(PNG_READ_GAMMA_SUPPORTED)
   int color_type = png_ptr->color_type;
682
#endif
G
Guy Schalnat 已提交
683

G
Guy Schalnat 已提交
684
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
685 686
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
       (png_ptr->transformations & PNG_EXPAND))
G
Guy Schalnat 已提交
687
   {
688
      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
G
Guy Schalnat 已提交
689
      {
G
Guy Schalnat 已提交
690
         /* expand background chunk. */
G
Guy Schalnat 已提交
691 692 693
         switch (png_ptr->bit_depth)
         {
            case 1:
G
Guy Schalnat 已提交
694 695 696
               png_ptr->background.gray *= (png_uint_16)0xff;
               png_ptr->background.red = png_ptr->background.green =
               png_ptr->background.blue = png_ptr->background.gray;
G
Guy Schalnat 已提交
697 698
               break;
            case 2:
G
Guy Schalnat 已提交
699 700 701
               png_ptr->background.gray *= (png_uint_16)0x55;
               png_ptr->background.red = png_ptr->background.green =
               png_ptr->background.blue = png_ptr->background.gray;
G
Guy Schalnat 已提交
702 703
               break;
            case 4:
G
Guy Schalnat 已提交
704 705 706 707 708 709 710 711
               png_ptr->background.gray *= (png_uint_16)0x11;
               png_ptr->background.red = png_ptr->background.green =
               png_ptr->background.blue = png_ptr->background.gray;
               break;
            case 8:
            case 16:
               png_ptr->background.red = png_ptr->background.green =
               png_ptr->background.blue = png_ptr->background.gray;
G
Guy Schalnat 已提交
712 713 714
               break;
         }
      }
G
Guy Schalnat 已提交
715
      else if (color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
716
      {
G
Guy Schalnat 已提交
717
         png_ptr->background.red   =
G
Guy Schalnat 已提交
718 719 720
            png_ptr->palette[png_ptr->background.index].red;
         png_ptr->background.green =
            png_ptr->palette[png_ptr->background.index].green;
G
Guy Schalnat 已提交
721
         png_ptr->background.blue  =
G
Guy Schalnat 已提交
722
            png_ptr->palette[png_ptr->background.index].blue;
723 724 725 726 727

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
        {
#if defined(PNG_READ_EXPAND_SUPPORTED)
728
           if (!(png_ptr->transformations & PNG_EXPAND))
729 730
#endif
           {
731
           /* invert the alpha channel (in tRNS) unless the pixels are
732
              going to be expanded, in which case leave it for later */
733 734 735
              int i,istop;
              istop=(int)png_ptr->num_trans;
              for (i=0; i<istop; i++)
736
                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
737 738 739 740
           }
        }
#endif

G
Guy Schalnat 已提交
741 742
      }
   }
G
Guy Schalnat 已提交
743
#endif
G
Guy Schalnat 已提交
744

745
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
G
Guy Schalnat 已提交
746
   png_ptr->background_1 = png_ptr->background;
G
Guy Schalnat 已提交
747
#endif
748
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
749
   if (png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY))
G
Guy Schalnat 已提交
750 751
   {
      png_build_gamma_table(png_ptr);
G
Guy Schalnat 已提交
752
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
G
Guy Schalnat 已提交
753
      if (png_ptr->transformations & PNG_BACKGROUND)
G
Guy Schalnat 已提交
754
      {
G
Guy Schalnat 已提交
755 756 757
         if (color_type == PNG_COLOR_TYPE_PALETTE)
         {
            png_color back, back_1;
758 759 760
            png_colorp palette = png_ptr->palette;
            int num_palette = png_ptr->num_palette;
            int i;
A
Andreas Dilger 已提交
761 762 763 764 765 766 767 768 769 770 771 772
            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
            {
               back.red = png_ptr->gamma_table[png_ptr->background.red];
               back.green = png_ptr->gamma_table[png_ptr->background.green];
               back.blue = png_ptr->gamma_table[png_ptr->background.blue];

               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
            }
            else
            {
773
               double g, gs;
A
Andreas Dilger 已提交
774

775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
               switch (png_ptr->background_gamma_type)
               {
                  case PNG_BACKGROUND_GAMMA_SCREEN:
                     g = (png_ptr->screen_gamma);
                     gs = 1.0;
                     break;
                  case PNG_BACKGROUND_GAMMA_FILE:
                     g = 1.0 / (png_ptr->gamma);
                     gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
                     break;
                  case PNG_BACKGROUND_GAMMA_UNIQUE:
                     g = 1.0 / (png_ptr->background_gamma);
                     gs = 1.0 / (png_ptr->background_gamma *
                                 png_ptr->screen_gamma);
                     break;
                  default:
                     g = 1.0;    /* back_1 */
                     gs = 1.0;   /* back */
               }
A
Andreas Dilger 已提交
794

795
               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
A
Andreas Dilger 已提交
796
               {
797
                  back.red   = (png_byte)png_ptr->background.red;
A
Andreas Dilger 已提交
798
                  back.green = (png_byte)png_ptr->background.green;
799
                  back.blue  = (png_byte)png_ptr->background.blue;
A
Andreas Dilger 已提交
800 801 802
               }
               else
               {
803 804 805 806 807 808
                  back.red = (png_byte)(pow(
                     (double)png_ptr->background.red/255, gs) * 255.0 + .5);
                  back.green = (png_byte)(pow(
                     (double)png_ptr->background.green/255, gs) * 255.0 + .5);
                  back.blue = (png_byte)(pow(
                     (double)png_ptr->background.blue/255, gs) * 255.0 + .5);
A
Andreas Dilger 已提交
809
               }
G
Guy Schalnat 已提交
810

811 812 813 814 815 816
               back_1.red = (png_byte)(pow(
                  (double)png_ptr->background.red/255, g) * 255.0 + .5);
               back_1.green = (png_byte)(pow(
                  (double)png_ptr->background.green/255, g) * 255.0 + .5);
               back_1.blue = (png_byte)(pow(
                  (double)png_ptr->background.blue/255, g) * 255.0 + .5);
A
Andreas Dilger 已提交
817
            }
G
Guy Schalnat 已提交
818 819
            for (i = 0; i < num_palette; i++)
            {
A
Andreas Dilger 已提交
820
               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
G
Guy Schalnat 已提交
821 822 823 824 825
               {
                  if (png_ptr->trans[i] == 0)
                  {
                     palette[i] = back;
                  }
A
Andreas Dilger 已提交
826
                  else /* if (png_ptr->trans[i] != 0xff) */
G
Guy Schalnat 已提交
827
                  {
A
Andreas Dilger 已提交
828
                     png_byte v, w;
G
Guy Schalnat 已提交
829 830

                     v = png_ptr->gamma_to_1[palette[i].red];
A
Andreas Dilger 已提交
831
                     png_composite(w, v, png_ptr->trans[i], back_1.red);
G
Guy Schalnat 已提交
832 833 834
                     palette[i].red = png_ptr->gamma_from_1[w];

                     v = png_ptr->gamma_to_1[palette[i].green];
A
Andreas Dilger 已提交
835
                     png_composite(w, v, png_ptr->trans[i], back_1.green);
G
Guy Schalnat 已提交
836 837 838
                     palette[i].green = png_ptr->gamma_from_1[w];

                     v = png_ptr->gamma_to_1[palette[i].blue];
A
Andreas Dilger 已提交
839
                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
G
Guy Schalnat 已提交
840 841 842 843 844 845 846 847 848 849 850
                     palette[i].blue = png_ptr->gamma_from_1[w];
                  }
               }
               else
               {
                  palette[i].red = png_ptr->gamma_table[palette[i].red];
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
               }
            }
         }
A
Andreas Dilger 已提交
851
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN)*/
852 853
         else
         /* color_type != PNG_COLOR_TYPE_PALETTE */
G
Guy Schalnat 已提交
854
         {
855 856 857
            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
            double g = 1.0;
            double gs = 1.0;
G
Guy Schalnat 已提交
858 859

            switch (png_ptr->background_gamma_type)
G
Guy Schalnat 已提交
860
            {
G
Guy Schalnat 已提交
861
               case PNG_BACKGROUND_GAMMA_SCREEN:
862
                  g = (png_ptr->screen_gamma);
G
Guy Schalnat 已提交
863 864 865 866
                  gs = 1.0;
                  break;
               case PNG_BACKGROUND_GAMMA_FILE:
                  g = 1.0 / (png_ptr->gamma);
867
                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
G
Guy Schalnat 已提交
868 869 870 871
                  break;
               case PNG_BACKGROUND_GAMMA_UNIQUE:
                  g = 1.0 / (png_ptr->background_gamma);
                  gs = 1.0 / (png_ptr->background_gamma *
872
                     png_ptr->screen_gamma);
G
Guy Schalnat 已提交
873 874 875
                  break;
            }

G
Guy Schalnat 已提交
876
            if (color_type & PNG_COLOR_MASK_COLOR)
G
Guy Schalnat 已提交
877
            {
878
               /* RGB or RGBA */
G
Guy Schalnat 已提交
879
               png_ptr->background_1.red = (png_uint_16)(pow(
G
Guy Schalnat 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893
                  (double)png_ptr->background.red / m, g) * m + .5);
               png_ptr->background_1.green = (png_uint_16)(pow(
                  (double)png_ptr->background.green / m, g) * m + .5);
               png_ptr->background_1.blue = (png_uint_16)(pow(
                  (double)png_ptr->background.blue / m, g) * m + .5);
               png_ptr->background.red = (png_uint_16)(pow(
                  (double)png_ptr->background.red / m, gs) * m + .5);
               png_ptr->background.green = (png_uint_16)(pow(
                  (double)png_ptr->background.green / m, gs) * m + .5);
               png_ptr->background.blue = (png_uint_16)(pow(
                  (double)png_ptr->background.blue / m, gs) * m + .5);
            }
            else
            {
894
               /* GRAY or GRAY ALPHA */
G
Guy Schalnat 已提交
895
               png_ptr->background_1.gray = (png_uint_16)(pow(
G
Guy Schalnat 已提交
896
                  (double)png_ptr->background.gray / m, g) * m + .5);
G
Guy Schalnat 已提交
897
               png_ptr->background.gray = (png_uint_16)(pow(
G
Guy Schalnat 已提交
898
                  (double)png_ptr->background.gray / m, gs) * m + .5);
G
Guy Schalnat 已提交
899 900 901
            }
         }
      }
G
Guy Schalnat 已提交
902
      else
903
      /* transformation does not include PNG_BACKGROUND */
904
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
G
Guy Schalnat 已提交
905 906
      if (color_type == PNG_COLOR_TYPE_PALETTE)
      {
907 908 909
         png_colorp palette = png_ptr->palette;
         int num_palette = png_ptr->num_palette;
         int i;
G
Guy Schalnat 已提交
910 911 912 913 914 915 916 917 918 919 920

         for (i = 0; i < num_palette; i++)
         {
            palette[i].red = png_ptr->gamma_table[palette[i].red];
            palette[i].green = png_ptr->gamma_table[palette[i].green];
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
         }
      }
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   else
G
Guy Schalnat 已提交
921
#endif
922
#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
G
Guy Schalnat 已提交
923
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
924
   /* No GAMMA transformation */
925 926
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
       (color_type == PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
927
   {
928 929
      int i;
      int istop = (int)png_ptr->num_trans;
G
Guy Schalnat 已提交
930
      png_color back;
931
      png_colorp palette = png_ptr->palette;
G
Guy Schalnat 已提交
932 933 934 935 936

      back.red   = (png_byte)png_ptr->background.red;
      back.green = (png_byte)png_ptr->background.green;
      back.blue  = (png_byte)png_ptr->background.blue;

937
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
938 939 940 941 942 943 944
      {
         if (png_ptr->trans[i] == 0)
         {
            palette[i] = back;
         }
         else if (png_ptr->trans[i] != 0xff)
         {
945
            /* The png_composite() macro is defined in png.h */
A
Andreas Dilger 已提交
946 947 948 949 950 951
            png_composite(palette[i].red, palette[i].red,
               png_ptr->trans[i], back.red);
            png_composite(palette[i].green, palette[i].green,
               png_ptr->trans[i], back.green);
            png_composite(palette[i].blue, palette[i].blue,
               png_ptr->trans[i], back.blue);
G
Guy Schalnat 已提交
952 953
         }
      }
G
Guy Schalnat 已提交
954
   }
955
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
G
Guy Schalnat 已提交
956

G
Guy Schalnat 已提交
957
#if defined(PNG_READ_SHIFT_SUPPORTED)
G
Guy Schalnat 已提交
958
   if ((png_ptr->transformations & PNG_SHIFT) &&
959
      (color_type == PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
960
   {
961 962 963 964 965
      png_uint_16 i;
      png_uint_16 istop = png_ptr->num_palette;
      int sr = 8 - png_ptr->sig_bit.red;
      int sg = 8 - png_ptr->sig_bit.green;
      int sb = 8 - png_ptr->sig_bit.blue;
G
Guy Schalnat 已提交
966 967 968 969 970 971 972

      if (sr < 0 || sr > 8)
         sr = 0;
      if (sg < 0 || sg > 8)
         sg = 0;
      if (sb < 0 || sb > 8)
         sb = 0;
973
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
974 975 976 977 978 979
      {
         png_ptr->palette[i].red >>= sr;
         png_ptr->palette[i].green >>= sg;
         png_ptr->palette[i].blue >>= sb;
      }
   }
980
#endif  /* PNG_READ_SHIFT_SUPPORTED */
981
 }
982 983 984 985 986
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
   if(png_ptr)
      return;
#endif
G
Guy Schalnat 已提交
987 988
}

A
Andreas Dilger 已提交
989
/* Modify the info structure to reflect the transformations.  The
990 991 992
 * info should be updated so a PNG file could be written with it,
 * assuming the transformations result in valid PNG data.
 */
993
void /* PRIVATE */
G
Guy Schalnat 已提交
994
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
995
{
A
Andreas Dilger 已提交
996
   png_debug(1, "in png_read_transform_info\n");
G
Guy Schalnat 已提交
997
#if defined(PNG_READ_EXPAND_SUPPORTED)
A
Andreas Dilger 已提交
998
   if (png_ptr->transformations & PNG_EXPAND)
G
Guy Schalnat 已提交
999
   {
A
Andreas Dilger 已提交
1000 1001 1002 1003 1004 1005
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      {
         if (png_ptr->num_trans)
            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
         else
            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
G
Guy Schalnat 已提交
1006
         info_ptr->bit_depth = 8;
A
Andreas Dilger 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
         info_ptr->num_trans = 0;
      }
      else
      {
         if (png_ptr->num_trans)
            info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
         if (info_ptr->bit_depth < 8)
            info_ptr->bit_depth = 8;
         info_ptr->num_trans = 0;
      }
G
Guy Schalnat 已提交
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
   }
#endif

#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   if (png_ptr->transformations & PNG_BACKGROUND)
   {
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
      info_ptr->num_trans = 0;
      info_ptr->background = png_ptr->background;
   }
#endif

1029 1030
#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->transformations & PNG_GAMMA)
1031 1032
   {
#ifdef PNG_FLOATING_POINT_SUPPORTED
1033 1034
      info_ptr->gamma = png_ptr->gamma;
#endif
1035 1036 1037 1038 1039
#ifdef PNG_FIXED_POINT_SUPPORTED
      info_ptr->int_gamma = png_ptr->int_gamma;
#endif
   }
#endif
1040

G
Guy Schalnat 已提交
1041
#if defined(PNG_READ_16_TO_8_SUPPORTED)
1042
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
G
Guy Schalnat 已提交
1043
      info_ptr->bit_depth = 8;
G
Guy Schalnat 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
   if (png_ptr->transformations & PNG_DITHER)
   {
      if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
         (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
         png_ptr->palette_lookup && info_ptr->bit_depth == 8)
      {
         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
      }
G
Guy Schalnat 已提交
1055
   }
G
Guy Schalnat 已提交
1056 1057 1058
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
1059
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
G
Guy Schalnat 已提交
1060 1061 1062 1063
      info_ptr->bit_depth = 8;
#endif

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1064
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
G
Guy Schalnat 已提交
1065 1066
      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
#endif
A
Andreas Dilger 已提交
1067

1068 1069 1070 1071 1072
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
#endif

G
Guy Schalnat 已提交
1073
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1074 1075 1076 1077 1078
      info_ptr->channels = 1;
   else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
      info_ptr->channels = 3;
   else
      info_ptr->channels = 1;
A
Andreas Dilger 已提交
1079

1080
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1081
   if (png_ptr->transformations & PNG_STRIP_ALPHA)
A
Andreas Dilger 已提交
1082 1083 1084
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
#endif

1085 1086 1087
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
      info_ptr->channels++;

A
Andreas Dilger 已提交
1088
#if defined(PNG_READ_FILLER_SUPPORTED)
1089
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
1090 1091 1092
   if ((png_ptr->transformations & PNG_FILLER) &&
       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1093
   {
1094
      info_ptr->channels++;
1095 1096 1097 1098
#if 0 /* if adding a true alpha channel not just filler */
      info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
#endif
   }
A
Andreas Dilger 已提交
1099 1100
#endif

1101 1102
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1103 1104 1105 1106 1107 1108 1109 1110 1111
   if(png_ptr->transformations & PNG_USER_TRANSFORM)
     {
       if(info_ptr->bit_depth < png_ptr->user_transform_depth)
         info_ptr->bit_depth = png_ptr->user_transform_depth;
       if(info_ptr->channels < png_ptr->user_transform_channels)
         info_ptr->channels = png_ptr->user_transform_channels;
     }
#endif

G
Guy Schalnat 已提交
1112 1113
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
      info_ptr->bit_depth);
1114
   info_ptr->rowbytes = ((info_ptr->width * info_ptr->pixel_depth + 7) >> 3);
1115

1116 1117 1118 1119
#if !defined(PNG_READ_EXPAND_SUPPORTED)
   if(png_ptr)
      return;
#endif
G
Guy Schalnat 已提交
1120 1121
}

1122 1123 1124 1125
/* Transform the row.  The order of transformations is significant,
 * and is very touchy.  If you add a transformation, take care to
 * decide how it fits in with the other transformations here.
 */
1126
void /* PRIVATE */
G
Guy Schalnat 已提交
1127
png_do_read_transformations(png_structp png_ptr)
G
Guy Schalnat 已提交
1128
{
A
Andreas Dilger 已提交
1129 1130 1131
   png_debug(1, "in png_do_read_transformations\n");
#if !defined(PNG_USELESS_TESTS_SUPPORTED)
   if (png_ptr->row_buf == NULL)
G
Guy Schalnat 已提交
1132
   {
1133
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
A
Andreas Dilger 已提交
1134 1135 1136 1137 1138
      char msg[50];

      sprintf(msg, "NULL row buffer for row %ld, pass %d", png_ptr->row_number,
         png_ptr->pass);
      png_error(png_ptr, msg);
1139 1140 1141
#else
      png_error(png_ptr, "NULL row buffer");
#endif
G
Guy Schalnat 已提交
1142
   }
A
Andreas Dilger 已提交
1143 1144 1145 1146
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
G
Guy Schalnat 已提交
1147
   {
A
Andreas Dilger 已提交
1148 1149 1150 1151 1152
      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
      {
         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
            png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
      }
1153
      else
A
Andreas Dilger 已提交
1154 1155 1156 1157 1158 1159 1160 1161
      {
         if (png_ptr->num_trans)
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
               &(png_ptr->trans_values));
         else
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
               NULL);
      }
G
Guy Schalnat 已提交
1162
   }
G
Guy Schalnat 已提交
1163 1164
#endif

A
Andreas Dilger 已提交
1165 1166 1167 1168 1169 1170
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_STRIP_ALPHA)
      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
         PNG_FLAG_FILLER_AFTER);
#endif

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
   {
      int rgb_error =
         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
      if(rgb_error)
      {
         png_ptr->rgb_to_gray_status=1;
         if(png_ptr->transformations == PNG_RGB_TO_GRAY_WARN)
            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
         if(png_ptr->transformations == PNG_RGB_TO_GRAY_ERR)
            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
      }
   }
#endif

1187 1188 1189 1190 1191
/*
From Andreas Dilger e-mail to png-implement, 26 March 1998:

  In most cases, the "simple transparency" should be done prior to doing
  gray-to-RGB, or you will have to test 3x as many bytes to check if a
1192
  pixel is transparent.  You would also need to make sure that the
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
  transparency information is upgraded to RGB.

  To summarize, the current flow is:
  - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
                                  with background "in place" if transparent,
                                  convert to RGB if necessary
  - Gray + alpha -> composite with gray background and remove alpha bytes,
                                  convert to RGB if necessary

  To support RGB backgrounds for gray images we need:
  - Gray + simple transparency -> convert to RGB + simple transparency, compare
                                  3 or 6 bytes and composite with background
                                  "in place" if transparent (3x compare/pixel
                                  compared to doing composite with gray bkgrnd)
  - Gray + alpha -> convert to RGB + alpha, composite with background and
                                  remove alpha bytes (3x float operations/pixel
                                  compared with composite on gray background)

  Greg's change will do this.  The reason it wasn't done before is for
  performance, as this increases the per-pixel operations.  If we would check
  in advance if the background was gray or RGB, and position the gray-to-RGB
  transform appropriately, then it would save a lot of work/time.
 */

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   /* if gray -> RGB, do so now only if background is non-gray; else do later
    * for performance reasons */
1220
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1221
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1222 1223 1224
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

G
Guy Schalnat 已提交
1225
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1226 1227 1228
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
      ((png_ptr->num_trans != 0 ) ||
      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
G
Guy Schalnat 已提交
1229
      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1230 1231 1232
         &(png_ptr->trans_values), &(png_ptr->background)
#if defined(PNG_READ_GAMMA_SUPPORTED)
         , &(png_ptr->background_1),
G
Guy Schalnat 已提交
1233 1234 1235
         png_ptr->gamma_table, png_ptr->gamma_from_1,
         png_ptr->gamma_to_1, png_ptr->gamma_16_table,
         png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
1236 1237 1238
         png_ptr->gamma_shift
#endif
);
G
Guy Schalnat 已提交
1239 1240 1241 1242
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
   if ((png_ptr->transformations & PNG_GAMMA) &&
1243 1244 1245 1246 1247
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
      !((png_ptr->transformations & PNG_BACKGROUND) &&
      ((png_ptr->num_trans != 0) ||
      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
#endif
A
Andreas Dilger 已提交
1248
      (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
1249 1250 1251
      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
         png_ptr->gamma_table, png_ptr->gamma_16_table,
         png_ptr->gamma_shift);
G
Guy Schalnat 已提交
1252 1253 1254
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
1255 1256
   if (png_ptr->transformations & PNG_16_TO_8)
      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1257 1258 1259
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
G
Guy Schalnat 已提交
1260
   if (png_ptr->transformations & PNG_DITHER)
G
Guy Schalnat 已提交
1261
   {
A
Andreas Dilger 已提交
1262 1263
      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
         png_ptr->palette_lookup, png_ptr->dither_index);
1264 1265
      if(png_ptr->row_info.rowbytes == (png_uint_32)0)
         png_error(png_ptr, "png_do_dither returned rowbytes=0");
A
Andreas Dilger 已提交
1266
   }
G
Guy Schalnat 已提交
1267 1268 1269
#endif

#if defined(PNG_READ_INVERT_SUPPORTED)
G
Guy Schalnat 已提交
1270
   if (png_ptr->transformations & PNG_INVERT_MONO)
G
Guy Schalnat 已提交
1271
      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1272 1273 1274
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
G
Guy Schalnat 已提交
1275 1276 1277
   if (png_ptr->transformations & PNG_SHIFT)
      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
         &(png_ptr->shift));
G
Guy Schalnat 已提交
1278 1279 1280
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
G
Guy Schalnat 已提交
1281
   if (png_ptr->transformations & PNG_PACK)
G
Guy Schalnat 已提交
1282
      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1283 1284 1285
#endif

#if defined(PNG_READ_BGR_SUPPORTED)
G
Guy Schalnat 已提交
1286 1287
   if (png_ptr->transformations & PNG_BGR)
      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1288 1289
#endif

A
Andreas Dilger 已提交
1290 1291 1292 1293 1294
#if defined(PNG_READ_PACKSWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_PACKSWAP)
      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

G
Guy Schalnat 已提交
1295
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1296
   /* if gray -> RGB, do so now only if we did not do so above */
1297 1298
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
G
Guy Schalnat 已提交
1299
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1300 1301 1302 1303 1304
#endif

#if defined(PNG_READ_FILLER_SUPPORTED)
   if (png_ptr->transformations & PNG_FILLER)
      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
A
Andreas Dilger 已提交
1305 1306 1307
         (png_uint_32)png_ptr->filler, png_ptr->flags);
#endif

1308 1309 1310 1311 1312
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

1313 1314 1315 1316 1317
#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

A
Andreas Dilger 已提交
1318 1319 1320
#if defined(PNG_READ_SWAP_SUPPORTED)
   if (png_ptr->transformations & PNG_SWAP_BYTES)
      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1321
#endif
1322 1323 1324

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1325
    {
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
      if(png_ptr->read_user_transform_fn != NULL)
        (*(png_ptr->read_user_transform_fn)) /* user read transform function */
          (png_ptr,                    /* png_ptr */
           &(png_ptr->row_info),       /* row_info:     */
             /*  png_uint_32 width;          width of row */
             /*  png_uint_32 rowbytes;       number of bytes in row */
             /*  png_byte color_type;        color type of pixels */
             /*  png_byte bit_depth;         bit depth of samples */
             /*  png_byte channels;          number of channels (1-4) */
             /*  png_byte pixel_depth;       bits per pixel (depth*channels) */
           png_ptr->row_buf + 1);      /* start of pixel data for row */
1337
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
1338 1339 1340 1341
      if(png_ptr->user_transform_depth)
         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
      if(png_ptr->user_transform_channels)
         png_ptr->row_info.channels = png_ptr->user_transform_channels;
1342
#endif
1343 1344
      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
         png_ptr->row_info.channels);
1345
      png_ptr->row_info.rowbytes = (png_ptr->row_info.width *
1346 1347
         png_ptr->row_info.pixel_depth+7)>>3;
   }
1348 1349
#endif

G
Guy Schalnat 已提交
1350 1351
}

G
Guy Schalnat 已提交
1352
#if defined(PNG_READ_PACK_SUPPORTED)
1353 1354 1355 1356 1357 1358
/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
 * without changing the actual values.  Thus, if you had a row with
 * a bit depth of 1, you would end up with bytes that only contained
 * the numbers 0 or 1.  If you would rather they contain 0 and 255, use
 * png_do_shift() after this.
 */
1359
void /* PRIVATE */
G
Guy Schalnat 已提交
1360
png_do_unpack(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
1361
{
A
Andreas Dilger 已提交
1362 1363 1364 1365 1366 1367
   png_debug(1, "in png_do_unpack\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
#else
   if (row_info->bit_depth < 8)
#endif
G
Guy Schalnat 已提交
1368
   {
1369
      png_uint_32 i;
1370
      png_uint_32 row_width=row_info->width;
A
Andreas Dilger 已提交
1371

G
Guy Schalnat 已提交
1372 1373 1374 1375
      switch (row_info->bit_depth)
      {
         case 1:
         {
1376 1377
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
            png_bytep dp = row + (png_size_t)row_width - 1;
1378
            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1379
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1380
            {
1381
               *dp = (png_byte)((*sp >> shift) & 0x01);
G
Guy Schalnat 已提交
1382 1383 1384 1385
               if (shift == 7)
               {
                  shift = 0;
                  sp--;
G
Guy Schalnat 已提交
1386
               }
G
Guy Schalnat 已提交
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
               else
                  shift++;

               dp--;
            }
            break;
         }
         case 2:
         {

1397 1398
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
            png_bytep dp = row + (png_size_t)row_width - 1;
1399
            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1400
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1401
            {
1402
               *dp = (png_byte)((*sp >> shift) & 0x03);
G
Guy Schalnat 已提交
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
               if (shift == 6)
               {
                  shift = 0;
                  sp--;
               }
               else
                  shift += 2;

               dp--;
            }
            break;
         }
         case 4:
         {
1417 1418
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
            png_bytep dp = row + (png_size_t)row_width - 1;
1419
            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1420
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1421
            {
1422
               *dp = (png_byte)((*sp >> shift) & 0x0f);
G
Guy Schalnat 已提交
1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
               if (shift == 4)
               {
                  shift = 0;
                  sp--;
               }
               else
                  shift = 4;

               dp--;
            }
            break;
         }
      }
      row_info->bit_depth = 8;
G
Guy Schalnat 已提交
1437
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1438
      row_info->rowbytes = row_width * row_info->channels;
G
Guy Schalnat 已提交
1439 1440
   }
}
G
Guy Schalnat 已提交
1441
#endif
G
Guy Schalnat 已提交
1442

G
Guy Schalnat 已提交
1443
#if defined(PNG_READ_SHIFT_SUPPORTED)
1444 1445 1446 1447 1448
/* Reverse the effects of png_do_shift.  This routine merely shifts the
 * pixels back to their significant bits values.  Thus, if you have
 * a row of bit depth 8, but only 5 are significant, this will shift
 * the values back to 0 through 31.
 */
1449
void /* PRIVATE */
A
Andreas Dilger 已提交
1450
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
G
Guy Schalnat 已提交
1451
{
A
Andreas Dilger 已提交
1452 1453 1454 1455 1456 1457
   png_debug(1, "in png_do_unshift\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL && sig_bits != NULL &&
#endif
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1458 1459
   {
      int shift[4];
1460 1461 1462
      int channels = 0;
      int c;
      png_uint_16 value = 0;
1463
      png_uint_32 row_width = row_info->width;
G
Guy Schalnat 已提交
1464 1465 1466

      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
      {
G
Guy Schalnat 已提交
1467 1468 1469
         shift[channels++] = row_info->bit_depth - sig_bits->red;
         shift[channels++] = row_info->bit_depth - sig_bits->green;
         shift[channels++] = row_info->bit_depth - sig_bits->blue;
G
Guy Schalnat 已提交
1470 1471 1472
      }
      else
      {
G
Guy Schalnat 已提交
1473
         shift[channels++] = row_info->bit_depth - sig_bits->gray;
G
Guy Schalnat 已提交
1474 1475 1476
      }
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
      {
G
Guy Schalnat 已提交
1477
         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
G
Guy Schalnat 已提交
1478
      }
G
Guy Schalnat 已提交
1479

A
Andreas Dilger 已提交
1480
      for (c = 0; c < channels; c++)
G
Guy Schalnat 已提交
1481
      {
A
Andreas Dilger 已提交
1482 1483
         if (shift[c] <= 0)
            shift[c] = 0;
G
Guy Schalnat 已提交
1484 1485 1486
         else
            value = 1;
      }
G
Guy Schalnat 已提交
1487

G
Guy Schalnat 已提交
1488 1489
      if (!value)
         return;
G
Guy Schalnat 已提交
1490

G
Guy Schalnat 已提交
1491
      switch (row_info->bit_depth)
G
Guy Schalnat 已提交
1492 1493 1494
      {
         case 2:
         {
A
Andreas Dilger 已提交
1495
            png_bytep bp;
1496 1497
            png_uint_32 i;
            png_uint_32 istop = row_info->rowbytes;
A
Andreas Dilger 已提交
1498

1499
            for (bp = row, i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1500 1501
            {
               *bp >>= 1;
1502
               *bp++ &= 0x55;
G
Guy Schalnat 已提交
1503 1504 1505 1506 1507
            }
            break;
         }
         case 4:
         {
1508
            png_bytep bp = row;
1509 1510
            png_uint_32 i;
            png_uint_32 istop = row_info->rowbytes;
1511 1512
            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
               (png_byte)((int)0xf >> shift[0]));
1513

1514
            for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1515 1516
            {
               *bp >>= shift[0];
1517
               *bp++ &= mask;
G
Guy Schalnat 已提交
1518 1519 1520 1521 1522
            }
            break;
         }
         case 8:
         {
1523
            png_bytep bp = row;
1524
            png_uint_32 i;
1525
            png_uint_32 istop = row_width * channels;
G
Guy Schalnat 已提交
1526

1527
            for (i = 0; i < istop; i++)
A
Andreas Dilger 已提交
1528
            {
1529
               *bp++ >>= shift[i%channels];
G
Guy Schalnat 已提交
1530 1531 1532 1533 1534
            }
            break;
         }
         case 16:
         {
1535 1536 1537
            png_bytep bp = row;
            png_uint_32 i;
            png_uint_32 istop = channels * row_width;
G
Guy Schalnat 已提交
1538

1539
            for (i = 0; i < istop; i++)
A
Andreas Dilger 已提交
1540
            {
1541 1542 1543 1544
               value = (png_uint_16)((*bp << 8) + *(bp + 1));
               value >>= shift[i%channels];
               *bp++ = (png_byte)(value >> 8);
               *bp++ = (png_byte)(value & 0xff);
G
Guy Schalnat 已提交
1545 1546 1547 1548 1549 1550
            }
            break;
         }
      }
   }
}
G
Guy Schalnat 已提交
1551
#endif
G
Guy Schalnat 已提交
1552

G
Guy Schalnat 已提交
1553
#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
1554
/* chop rows of bit depth 16 down to 8 */
1555
void /* PRIVATE */
G
Guy Schalnat 已提交
1556
png_do_chop(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
1557
{
A
Andreas Dilger 已提交
1558 1559 1560 1561 1562 1563
   png_debug(1, "in png_do_chop\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
#else
   if (row_info->bit_depth == 16)
#endif
G
Guy Schalnat 已提交
1564
   {
1565 1566 1567 1568
      png_bytep sp = row;
      png_bytep dp = row;
      png_uint_32 i;
      png_uint_32 istop = row_info->width * row_info->channels;
A
Andreas Dilger 已提交
1569

1570
      for (i = 0; i<istop; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
1571
      {
A
Andreas Dilger 已提交
1572
#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593
      /* This does a more accurate scaling of the 16-bit color
       * value, rather than a simple low-byte truncation.
       *
       * What the ideal calculation should be:
       *   *dp = (((((png_uint_32)(*sp) << 8) |
       *          (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
       *
       * GRR: no, I think this is what it really should be:
       *   *dp = (((((png_uint_32)(*sp) << 8) |
       *           (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
       *
       * GRR: here's the exact calculation with shifts:
       *   temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
       *   *dp = (temp - (temp >> 8)) >> 8;
       *
       * Approximate calculation with shift/add instead of multiply/divide:
       *   *dp = ((((png_uint_32)(*sp) << 8) |
       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
       *
       * What we actually do to avoid extra shifting and conversion:
       */
1594

A
Andreas Dilger 已提交
1595 1596
         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
#else
1597
       /* Simply discard the low order byte */
G
Guy Schalnat 已提交
1598
         *dp = *sp;
A
Andreas Dilger 已提交
1599
#endif
G
Guy Schalnat 已提交
1600 1601
      }
      row_info->bit_depth = 8;
G
Guy Schalnat 已提交
1602
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1603
      row_info->rowbytes = row_info->width * row_info->channels;
A
Andreas Dilger 已提交
1604 1605 1606 1607 1608
   }
}
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1609
void /* PRIVATE */
A
Andreas Dilger 已提交
1610 1611 1612 1613 1614 1615 1616
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_swap_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
1617
      png_uint_32 row_width = row_info->width;
A
Andreas Dilger 已提交
1618 1619 1620 1621 1622
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      {
         /* This converts from RGBA to ARGB */
         if (row_info->bit_depth == 8)
         {
1623 1624
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1625 1626
            png_byte save;
            png_uint_32 i;
1627

1628
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
            {
               save = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save;
            }
         }
         /* This converts from RRGGBBAA to AARRGGBB */
         else
         {
1640 1641
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1642 1643
            png_byte save[2];
            png_uint_32 i;
1644

1645
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664
            {
               save[0] = *(--sp);
               save[1] = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save[0];
               *(--dp) = save[1];
            }
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      {
         /* This converts from GA to AG */
         if (row_info->bit_depth == 8)
         {
1665 1666
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1667 1668
            png_byte save;
            png_uint_32 i;
1669

1670
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1671 1672 1673 1674 1675 1676 1677 1678 1679
            {
               save = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save;
            }
         }
         /* This converts from GGAA to AAGG */
         else
         {
1680 1681
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1682 1683 1684
            png_byte save[2];
            png_uint_32 i;

1685
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
            {
               save[0] = *(--sp);
               save[1] = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save[0];
               *(--dp) = save[1];
            }
         }
      }
G
Guy Schalnat 已提交
1696 1697
   }
}
G
Guy Schalnat 已提交
1698
#endif
G
Guy Schalnat 已提交
1699

1700
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1701
void /* PRIVATE */
1702 1703 1704 1705 1706 1707 1708
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_invert_alpha\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
   {
1709
      png_uint_32 row_width = row_info->width;
1710 1711 1712 1713 1714
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      {
         /* This inverts the alpha channel in RGBA */
         if (row_info->bit_depth == 8)
         {
1715 1716
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1717 1718
            png_uint_32 i;

1719
            for (i = 0; i < row_width; i++)
1720
            {
1721
               *(--dp) = (png_byte)(255 - *(--sp));
1722 1723

/*             This does nothing:
1724 1725 1726
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1727 1728 1729 1730
               We can replace it with:
*/
               sp-=3;
               dp=sp;
1731 1732 1733 1734 1735
            }
         }
         /* This inverts the alpha channel in RRGGBBAA */
         else
         {
1736 1737
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1738 1739
            png_uint_32 i;

1740
            for (i = 0; i < row_width; i++)
1741
            {
1742 1743
               *(--dp) = (png_byte)(255 - *(--sp));
               *(--dp) = (png_byte)(255 - *(--sp));
1744 1745

/*             This does nothing:
1746 1747 1748 1749 1750 1751
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1752 1753 1754 1755
               We can replace it with:
*/
               sp-=6;
               dp=sp;
1756 1757 1758 1759 1760
            }
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      {
1761
         /* This inverts the alpha channel in GA */
1762 1763
         if (row_info->bit_depth == 8)
         {
1764 1765
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1766 1767
            png_uint_32 i;

1768
            for (i = 0; i < row_width; i++)
1769
            {
1770
               *(--dp) = (png_byte)(255 - *(--sp));
1771
               *(--dp) = *(--sp);
1772 1773
            }
         }
1774
         /* This inverts the alpha channel in GGAA */
1775 1776
         else
         {
1777 1778
            png_bytep sp  = row + row_info->rowbytes;
            png_bytep dp = sp;
1779 1780
            png_uint_32 i;

1781
            for (i = 0; i < row_width; i++)
1782
            {
1783 1784
               *(--dp) = (png_byte)(255 - *(--sp));
               *(--dp) = (png_byte)(255 - *(--sp));
1785
/*
1786 1787
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1788 1789 1790
*/
               sp-=2;
               dp=sp;
1791 1792 1793 1794 1795 1796 1797
            }
         }
      }
   }
}
#endif

G
Guy Schalnat 已提交
1798
#if defined(PNG_READ_FILLER_SUPPORTED)
A
Andreas Dilger 已提交
1799
/* Add filler channel if we have RGB color */
1800
void /* PRIVATE */
G
Guy Schalnat 已提交
1801
png_do_read_filler(png_row_infop row_info, png_bytep row,
A
Andreas Dilger 已提交
1802
   png_uint_32 filler, png_uint_32 flags)
G
Guy Schalnat 已提交
1803
{
G
Guy Schalnat 已提交
1804
   png_uint_32 i;
1805 1806
   png_uint_32 row_width = row_info->width;

1807
   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
1808
   png_byte lo_filler = (png_byte)(filler & 0xff);
A
Andreas Dilger 已提交
1809 1810 1811 1812 1813 1814

   png_debug(1, "in png_do_read_filler\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL  && row_info != NULL &&
#endif
1815
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
G
Guy Schalnat 已提交
1816
   {
1817
      if(row_info->bit_depth == 8)
G
Guy Schalnat 已提交
1818
      {
1819 1820
         /* This changes the data from G to GX */
         if (flags & PNG_FLAG_FILLER_AFTER)
G
Guy Schalnat 已提交
1821
         {
1822 1823
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp =  sp + (png_size_t)row_width;
1824 1825
            for (i = 1; i < row_width; i++)
            {
1826
               *(--dp) = lo_filler;
1827 1828
               *(--dp) = *(--sp);
            }
1829
            *(--dp) = lo_filler;
1830 1831 1832 1833 1834 1835 1836
            row_info->channels = 2;
            row_info->pixel_depth = 16;
            row_info->rowbytes = row_width * 2;
         }
      /* This changes the data from G to XG */
         else
         {
1837 1838
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp = sp  + (png_size_t)row_width;
1839 1840 1841
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
1842
               *(--dp) = lo_filler;
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
            }
            row_info->channels = 2;
            row_info->pixel_depth = 16;
            row_info->rowbytes = row_width * 2;
         }
      }
      else if(row_info->bit_depth == 16)
      {
         /* This changes the data from GG to GGXX */
         if (flags & PNG_FLAG_FILLER_AFTER)
         {
1854 1855
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp = sp  + (png_size_t)row_width;
1856 1857 1858
            for (i = 1; i < row_width; i++)
            {
               *(--dp) = hi_filler;
1859
               *(--dp) = lo_filler;
1860 1861 1862
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
1863 1864
            *(--dp) = hi_filler;
            *(--dp) = lo_filler;
1865 1866
            row_info->channels = 2;
            row_info->pixel_depth = 32;
1867
            row_info->rowbytes = row_width * 4;
1868 1869 1870 1871
         }
         /* This changes the data from GG to XXGG */
         else
         {
1872 1873
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp = sp  + (png_size_t)row_width;
1874 1875 1876 1877 1878
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = hi_filler;
1879
               *(--dp) = lo_filler;
1880 1881
            }
            row_info->channels = 2;
1882 1883
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
G
Guy Schalnat 已提交
1884
         }
G
Guy Schalnat 已提交
1885
      }
1886 1887 1888 1889 1890 1891 1892 1893
   } /* COLOR_TYPE == GRAY */
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   {
      if(row_info->bit_depth == 8)
      {
         /* This changes the data from RGB to RGBX */
         if (flags & PNG_FLAG_FILLER_AFTER)
         {
1894 1895
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp  + (png_size_t)row_width;
1896 1897
            for (i = 1; i < row_width; i++)
            {
1898
               *(--dp) = lo_filler;
1899 1900 1901 1902
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
1903
            *(--dp) = lo_filler;
1904 1905 1906 1907
            row_info->channels = 4;
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
         }
A
Andreas Dilger 已提交
1908
      /* This changes the data from RGB to XRGB */
1909 1910
         else
         {
1911 1912
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp + (png_size_t)row_width;
1913 1914 1915 1916 1917
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1918
               *(--dp) = lo_filler;
1919 1920 1921 1922 1923 1924 1925
            }
            row_info->channels = 4;
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
         }
      }
      else if(row_info->bit_depth == 16)
G
Guy Schalnat 已提交
1926
      {
1927 1928
         /* This changes the data from RRGGBB to RRGGBBXX */
         if (flags & PNG_FLAG_FILLER_AFTER)
G
Guy Schalnat 已提交
1929
         {
1930 1931
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp  + (png_size_t)row_width;
1932 1933 1934
            for (i = 1; i < row_width; i++)
            {
               *(--dp) = hi_filler;
1935
               *(--dp) = lo_filler;
1936 1937 1938 1939 1940 1941 1942
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
1943 1944
            *(--dp) = hi_filler;
            *(--dp) = lo_filler;
1945 1946
            row_info->channels = 4;
            row_info->pixel_depth = 64;
1947
            row_info->rowbytes = row_width * 8;
1948 1949 1950 1951
         }
         /* This changes the data from RRGGBB to XXRRGGBB */
         else
         {
1952 1953
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp  + (png_size_t)row_width;
1954 1955 1956 1957 1958 1959 1960 1961 1962
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = hi_filler;
1963
               *(--dp) = lo_filler;
1964 1965 1966
            }
            row_info->channels = 4;
            row_info->pixel_depth = 64;
1967
            row_info->rowbytes = row_width * 8;
G
Guy Schalnat 已提交
1968
         }
G
Guy Schalnat 已提交
1969
      }
1970
   } /* COLOR_TYPE == RGB */
G
Guy Schalnat 已提交
1971
}
G
Guy Schalnat 已提交
1972
#endif
G
Guy Schalnat 已提交
1973

G
Guy Schalnat 已提交
1974
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
A
Andreas Dilger 已提交
1975
/* expand grayscale files to RGB, with or without alpha */
1976
void /* PRIVATE */
G
Guy Schalnat 已提交
1977
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
1978
{
G
Guy Schalnat 已提交
1979
   png_uint_32 i;
1980
   png_uint_32 row_width = row_info->width;
G
Guy Schalnat 已提交
1981

A
Andreas Dilger 已提交
1982 1983 1984 1985 1986
   png_debug(1, "in png_do_gray_to_rgb\n");
   if (row_info->bit_depth >= 8 &&
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
G
Guy Schalnat 已提交
1987 1988 1989 1990 1991 1992
      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
      {
         if (row_info->bit_depth == 8)
         {
1993 1994
            png_bytep sp = row + (png_size_t)row_width - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
1995
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1996 1997 1998
            {
               *(dp--) = *sp;
               *(dp--) = *sp;
1999
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2000 2001 2002 2003
            }
         }
         else
         {
2004 2005
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2006
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2007 2008 2009 2010 2011
            {
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
2012 2013
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2014 2015 2016 2017 2018 2019 2020
            }
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      {
         if (row_info->bit_depth == 8)
         {
2021 2022
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2023
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2024 2025 2026 2027
            {
               *(dp--) = *(sp--);
               *(dp--) = *sp;
               *(dp--) = *sp;
2028
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2029 2030 2031 2032
            }
         }
         else
         {
2033 2034
            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2035
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2036 2037 2038 2039 2040 2041 2042
            {
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
2043 2044
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2045 2046 2047
            }
         }
      }
G
Guy Schalnat 已提交
2048
      row_info->channels += (png_byte)2;
G
Guy Schalnat 已提交
2049
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
G
Guy Schalnat 已提交
2050 2051
      row_info->pixel_depth = (png_byte)(row_info->channels *
         row_info->bit_depth);
2052
      row_info->rowbytes = ((row_width *
G
Guy Schalnat 已提交
2053 2054 2055
         row_info->pixel_depth + 7) >> 3);
   }
}
G
Guy Schalnat 已提交
2056
#endif
G
Guy Schalnat 已提交
2057

2058
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
2059
/* reduce RGB files to grayscale, with or without alpha
2060 2061 2062 2063 2064 2065 2066
 * using the equation given in Poynton's ColorFAQ at
 * <http://www.inforamp.net/~poynton/>
 * Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net
 *
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
 *
 *  We approximate this with
2067
 *
2068
 *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
2069 2070 2071
 *
 *  which can be expressed with integers as
 *
2072
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
2073 2074 2075 2076 2077
 *
 *  The calculation is to be done in a linear colorspace.
 *
 *  Other integer coefficents can be used via png_set_rgb_to_gray().
 */
2078
int /* PRIVATE */
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093
png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)

{
   png_uint_32 i;

   png_uint_32 row_width = row_info->width;
   int rgb_error = 0;

   png_debug(1, "in png_do_rgb_to_gray\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
      (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
2094 2095 2096
      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116

      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
      {
         if (row_info->bit_depth == 8)
         {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
            {
               png_bytep sp = row;
               png_bytep dp = row;

               for (i = 0; i < row_width; i++)
               {
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
                  if(red != green || red != blue)
                  {
                     rgb_error |= 1;
                     *(dp++) = png_ptr->gamma_from_1[
2117
                       (rc*red+gc*green+bc*blue)>>15];
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
                  }
                  else
                     *(dp++) = *(sp-1);
               }
            }
            else
#endif
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_byte red   = *(sp++);
                  png_byte green = *(sp++);
                  png_byte blue  = *(sp++);
                  if(red != green || red != blue)
                  {
                     rgb_error |= 1;
2136
                     *(dp++) = (png_byte)((rc*red+gc*green+bc*blue)>>15);
2137 2138 2139 2140 2141 2142
                  }
                  else
                     *(dp++) = *(sp-1);
               }
            }
         }
2143

2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
         else /* RGB bit_depth == 16 */
         {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
            if (png_ptr->gamma_16_to_1 != NULL &&
                png_ptr->gamma_16_from_1 != NULL)
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 red, green, blue, w;

2156 2157 2158
                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2159 2160 2161 2162 2163 2164 2165 2166 2167

                  if(red == green && red == blue)
                     w = red;
                  else
                  {
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
                                  png_ptr->gamma_shift][red>>8];
                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
                                  png_ptr->gamma_shift][green>>8];
2168
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
2169
                                  png_ptr->gamma_shift][blue>>8];
2170
                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
2171
                                  + bc*blue_1)>>15);
2172 2173 2174 2175
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
                         png_ptr->gamma_shift][gray16 >> 8];
                     rgb_error |= 1;
                  }
2176

2177 2178
                  *(dp++) = (png_byte)((w>>8) & 0xff);
                  *(dp++) = (png_byte)(w & 0xff);
2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
               }
            }
            else
#endif
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 red, green, blue, gray16;

2190 2191 2192
                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2193 2194 2195

                  if(red != green || red != blue)
                     rgb_error |= 1;
2196
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2197 2198
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
                  *(dp++) = (png_byte)(gray16 & 0xff);
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219
               }
            }
         }
      }
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      {
         if (row_info->bit_depth == 8)
         {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
                  if(red != green || red != blue)
                     rgb_error |= 1;
                  *(dp++) =  png_ptr->gamma_from_1
2220
                             [(rc*red + gc*green + bc*blue)>>15];
2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235
                  *(dp++) = *(sp++);  /* alpha */
               }
            }
            else
#endif
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_byte red   = *(sp++);
                  png_byte green = *(sp++);
                  png_byte blue  = *(sp++);
                  if(red != green || red != blue)
                     rgb_error |= 1;
2236
                  *(dp++) =  (png_byte)((gc*red + gc*green + bc*blue)>>8);
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252
                  *(dp++) = *(sp++);  /* alpha */
               }
            }
         }
         else /* RGBA bit_depth == 16 */
         {
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
            if (png_ptr->gamma_16_to_1 != NULL &&
                png_ptr->gamma_16_from_1 != NULL)
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 red, green, blue, w;

2253 2254 2255
                  red   = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  green = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp+1)); sp+=2;
2256 2257 2258 2259 2260 2261 2262 2263 2264

                  if(red == green && red == blue)
                     w = red;
                  else
                  {
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
                                  png_ptr->gamma_shift][red>>8];
                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
                                  png_ptr->gamma_shift][green>>8];
2265
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
2266
                                  png_ptr->gamma_shift][blue>>8];
2267
                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
2268
                                  + gc * green_1 + bc * blue_1)>>15);
2269 2270 2271 2272
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
                         png_ptr->gamma_shift][gray16 >> 8];
                     rgb_error |= 1;
                  }
2273

2274 2275
                  *(dp++) = (png_byte)((w>>8) & 0xff);
                  *(dp++) = (png_byte)(w & 0xff);
2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
                  *(dp++) = *(sp++);  /* alpha */
                  *(dp++) = *(sp++);
               }
            }
            else
#endif
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 red, green, blue, gray16;
2288 2289 2290
                  red   = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
                  green = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
                  blue  = (png_uint_16)((*(sp)<<8) | *(sp+1)); sp+=2;
2291 2292
                  if(red != green || red != blue)
                     rgb_error |= 1;
2293
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2294 2295
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
                  *(dp++) = (png_byte)(gray16 & 0xff);
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
                  *(dp++) = *(sp++);  /* alpha */
                  *(dp++) = *(sp++);
               }
            }
         }
      }
   row_info->channels -= (png_byte)2;
      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
      row_info->pixel_depth = (png_byte)(row_info->channels *
         row_info->bit_depth);
      row_info->rowbytes = ((row_width *
         row_info->pixel_depth + 7) >> 3);
   }
   return rgb_error;
}
#endif

2313 2314 2315 2316 2317
/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
 * large of png_color.  This lets grayscale images be treated as
 * paletted.  Most useful for gamma correction and simplification
 * of code.
 */
2318
void /* PRIVATE */
G
Guy Schalnat 已提交
2319
png_build_grayscale_palette(int bit_depth, png_colorp palette)
G
Guy Schalnat 已提交
2320 2321 2322 2323 2324 2325
{
   int num_palette;
   int color_inc;
   int i;
   int v;

A
Andreas Dilger 已提交
2326 2327
   png_debug(1, "in png_do_build_grayscale_palette\n");
   if (palette == NULL)
G
Guy Schalnat 已提交
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
      return;

   switch (bit_depth)
   {
      case 1:
         num_palette = 2;
         color_inc = 0xff;
         break;
      case 2:
         num_palette = 4;
         color_inc = 0x55;
         break;
      case 4:
         num_palette = 16;
         color_inc = 0x11;
         break;
      case 8:
         num_palette = 256;
         color_inc = 1;
         break;
      default:
G
Guy Schalnat 已提交
2349
         num_palette = 0;
G
Guy Schalnat 已提交
2350
         color_inc = 0;
G
Guy Schalnat 已提交
2351 2352 2353 2354 2355
         break;
   }

   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
   {
G
Guy Schalnat 已提交
2356 2357 2358
      palette[i].red = (png_byte)v;
      palette[i].green = (png_byte)v;
      palette[i].blue = (png_byte)v;
G
Guy Schalnat 已提交
2359 2360 2361
   }
}

A
Andreas Dilger 已提交
2362 2363
/* This function is currently unused.  Do we really need it? */
#if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
2364
void /* PRIVATE */
G
Guy Schalnat 已提交
2365
png_correct_palette(png_structp png_ptr, png_colorp palette,
G
Guy Schalnat 已提交
2366 2367
   int num_palette)
{
A
Andreas Dilger 已提交
2368
   png_debug(1, "in png_correct_palette\n");
2369 2370
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
    defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
2371
   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
G
Guy Schalnat 已提交
2372
   {
A
Andreas Dilger 已提交
2373
      png_color back, back_1;
G
Guy Schalnat 已提交
2374

A
Andreas Dilger 已提交
2375 2376
      if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
      {
G
Guy Schalnat 已提交
2377 2378 2379
         back.red = png_ptr->gamma_table[png_ptr->background.red];
         back.green = png_ptr->gamma_table[png_ptr->background.green];
         back.blue = png_ptr->gamma_table[png_ptr->background.blue];
G
Guy Schalnat 已提交
2380

G
Guy Schalnat 已提交
2381 2382 2383
         back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
         back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
         back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
A
Andreas Dilger 已提交
2384 2385 2386 2387
      }
      else
      {
         double g;
G
Guy Schalnat 已提交
2388

2389
         g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
A
Andreas Dilger 已提交
2390 2391 2392

         if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
             fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
G
Guy Schalnat 已提交
2393
         {
A
Andreas Dilger 已提交
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430
            back.red = png_ptr->background.red;
            back.green = png_ptr->background.green;
            back.blue = png_ptr->background.blue;
         }
         else
         {
            back.red =
               (png_byte)(pow((double)png_ptr->background.red/255, g) *
                255.0 + 0.5);
            back.green =
               (png_byte)(pow((double)png_ptr->background.green/255, g) *
                255.0 + 0.5);
            back.blue =
               (png_byte)(pow((double)png_ptr->background.blue/255, g) *
                255.0 + 0.5);
         }

         g = 1.0 / png_ptr->background_gamma;

         back_1.red =
            (png_byte)(pow((double)png_ptr->background.red/255, g) *
             255.0 + 0.5);
         back_1.green =
            (png_byte)(pow((double)png_ptr->background.green/255, g) *
             255.0 + 0.5);
         back_1.blue =
            (png_byte)(pow((double)png_ptr->background.blue/255, g) *
             255.0 + 0.5);
      }

      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      {
         png_uint_32 i;

         for (i = 0; i < (png_uint_32)num_palette; i++)
         {
            if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
G
Guy Schalnat 已提交
2431 2432 2433
            {
               palette[i] = back;
            }
A
Andreas Dilger 已提交
2434
            else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
G
Guy Schalnat 已提交
2435
            {
A
Andreas Dilger 已提交
2436
               png_byte v, w;
G
Guy Schalnat 已提交
2437 2438

               v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
A
Andreas Dilger 已提交
2439 2440
               png_composite(w, v, png_ptr->trans[i], back_1.red);
               palette[i].red = png_ptr->gamma_from_1[w];
G
Guy Schalnat 已提交
2441 2442

               v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
A
Andreas Dilger 已提交
2443 2444
               png_composite(w, v, png_ptr->trans[i], back_1.green);
               palette[i].green = png_ptr->gamma_from_1[w];
G
Guy Schalnat 已提交
2445 2446

               v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
A
Andreas Dilger 已提交
2447 2448
               png_composite(w, v, png_ptr->trans[i], back_1.blue);
               palette[i].blue = png_ptr->gamma_from_1[w];
G
Guy Schalnat 已提交
2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
            }
            else
            {
               palette[i].red = png_ptr->gamma_table[palette[i].red];
               palette[i].green = png_ptr->gamma_table[palette[i].green];
               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
            }
         }
      }
      else
      {
G
Guy Schalnat 已提交
2460
         int i;
G
Guy Schalnat 已提交
2461 2462 2463

         for (i = 0; i < num_palette; i++)
         {
A
Andreas Dilger 已提交
2464
            if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
G
Guy Schalnat 已提交
2465
            {
G
Guy Schalnat 已提交
2466
               palette[i] = back;
G
Guy Schalnat 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
            }
            else
            {
               palette[i].red = png_ptr->gamma_table[palette[i].red];
               palette[i].green = png_ptr->gamma_table[palette[i].green];
               palette[i].blue = png_ptr->gamma_table[palette[i].blue];
            }
         }
      }
   }
G
Guy Schalnat 已提交
2477 2478 2479
   else
#endif
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2480
   if (png_ptr->transformations & PNG_GAMMA)
G
Guy Schalnat 已提交
2481 2482 2483 2484 2485 2486 2487 2488 2489 2490
   {
      int i;

      for (i = 0; i < num_palette; i++)
      {
         palette[i].red = png_ptr->gamma_table[palette[i].red];
         palette[i].green = png_ptr->gamma_table[palette[i].green];
         palette[i].blue = png_ptr->gamma_table[palette[i].blue];
      }
   }
G
Guy Schalnat 已提交
2491 2492 2493 2494 2495
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   else
#endif
#endif
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
2496
   if (png_ptr->transformations & PNG_BACKGROUND)
G
Guy Schalnat 已提交
2497
   {
G
Guy Schalnat 已提交
2498
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
2499
      {
G
Guy Schalnat 已提交
2500
         png_color back;
G
Guy Schalnat 已提交
2501

G
Guy Schalnat 已提交
2502 2503 2504
         back.red   = (png_byte)png_ptr->background.red;
         back.green = (png_byte)png_ptr->background.green;
         back.blue  = (png_byte)png_ptr->background.blue;
G
Guy Schalnat 已提交
2505

2506
         for (i = 0; i < (int)png_ptr->num_trans; i++)
G
Guy Schalnat 已提交
2507
         {
2508
            if (png_ptr->trans[i] == 0)
G
Guy Schalnat 已提交
2509
            {
G
Guy Schalnat 已提交
2510 2511 2512
               palette[i].red = back.red;
               palette[i].green = back.green;
               palette[i].blue = back.blue;
G
Guy Schalnat 已提交
2513
            }
2514
            else if (png_ptr->trans[i] != 0xff)
G
Guy Schalnat 已提交
2515
            {
A
Andreas Dilger 已提交
2516 2517 2518 2519 2520 2521
               png_composite(palette[i].red, png_ptr->palette[i].red,
                  png_ptr->trans[i], back.red);
               png_composite(palette[i].green, png_ptr->palette[i].green,
                  png_ptr->trans[i], back.green);
               png_composite(palette[i].blue, png_ptr->palette[i].blue,
                  png_ptr->trans[i], back.blue);
G
Guy Schalnat 已提交
2522 2523 2524 2525 2526 2527 2528 2529 2530
            }
         }
      }
      else /* assume grayscale palette (what else could it be?) */
      {
         int i;

         for (i = 0; i < num_palette; i++)
         {
A
Andreas Dilger 已提交
2531
            if (i == (png_byte)png_ptr->trans_values.gray)
G
Guy Schalnat 已提交
2532
            {
G
Guy Schalnat 已提交
2533 2534 2535
               palette[i].red = (png_byte)png_ptr->background.red;
               palette[i].green = (png_byte)png_ptr->background.green;
               palette[i].blue = (png_byte)png_ptr->background.blue;
G
Guy Schalnat 已提交
2536 2537 2538 2539
            }
         }
      }
   }
G
Guy Schalnat 已提交
2540
#endif
G
Guy Schalnat 已提交
2541
}
G
Guy Schalnat 已提交
2542
#endif
G
Guy Schalnat 已提交
2543

G
Guy Schalnat 已提交
2544
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
2545
/* Replace any alpha or transparency with the supplied background color.
2546 2547 2548
 * "background" is already in the screen gamma, while "background_1" is
 * at a gamma of 1.0.  Paletted files have already been taken care of.
 */
2549
void /* PRIVATE */
G
Guy Schalnat 已提交
2550
png_do_background(png_row_infop row_info, png_bytep row,
2551 2552 2553
   png_color_16p trans_values, png_color_16p background
#if defined(PNG_READ_GAMMA_SUPPORTED)
   , png_color_16p background_1,
G
Guy Schalnat 已提交
2554
   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
G
Guy Schalnat 已提交
2555
   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
2556 2557 2558
   png_uint_16pp gamma_16_to_1, int gamma_shift
#endif
   )
G
Guy Schalnat 已提交
2559
{
G
Guy Schalnat 已提交
2560
   png_bytep sp, dp;
G
Guy Schalnat 已提交
2561
   png_uint_32 i;
2562
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
2563
   int shift;
G
Guy Schalnat 已提交
2564

A
Andreas Dilger 已提交
2565 2566 2567 2568 2569
   png_debug(1, "in png_do_background\n");
   if (background != NULL &&
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
G
Guy Schalnat 已提交
2570
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
A
Andreas Dilger 已提交
2571
      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
G
Guy Schalnat 已提交
2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582
   {
      switch (row_info->color_type)
      {
         case PNG_COLOR_TYPE_GRAY:
         {
            switch (row_info->bit_depth)
            {
               case 1:
               {
                  sp = row;
                  shift = 7;
2583
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2584
                  {
2585
                     if ((png_uint_16)((*sp >> shift) & 0x01)
2586
                        == trans_values->gray)
G
Guy Schalnat 已提交
2587
                     {
G
Guy Schalnat 已提交
2588 2589
                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
                        *sp |= (png_byte)(background->gray << shift);
G
Guy Schalnat 已提交
2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602
                     }
                     if (!shift)
                     {
                        shift = 7;
                        sp++;
                     }
                     else
                        shift--;
                  }
                  break;
               }
               case 2:
               {
2603 2604
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2605
                  {
2606 2607 2608
                     sp = row;
                     shift = 6;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2609
                     {
2610
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2611 2612 2613 2614 2615 2616 2617
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        else
                        {
2618
                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
2619
                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2620
                               (p << 4) | (p << 6)] >> 6) & 0x03);
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                           *sp |= (png_byte)(g << shift);
                        }
                        if (!shift)
                        {
                           shift = 6;
                           sp++;
                        }
                        else
                           shift -= 2;
G
Guy Schalnat 已提交
2631
                     }
2632 2633 2634 2635 2636 2637 2638
                  }
                  else
#endif
                  {
                     sp = row;
                     shift = 6;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2639
                     {
2640
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        if (!shift)
                        {
                           shift = 6;
                           sp++;
                        }
                        else
                           shift -= 2;
G
Guy Schalnat 已提交
2653 2654 2655 2656 2657 2658
                     }
                  }
                  break;
               }
               case 4:
               {
2659 2660
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2661
                  {
2662 2663 2664
                     sp = row;
                     shift = 4;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2665
                     {
2666
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2667 2668 2669 2670 2671 2672 2673
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        else
                        {
2674
                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2675
                           png_byte g = (png_byte)((gamma_table[p |
2676
                             (p << 4)] >> 4) & 0x0f);
2677 2678 2679 2680 2681 2682 2683 2684 2685 2686
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                           *sp |= (png_byte)(g << shift);
                        }
                        if (!shift)
                        {
                           shift = 4;
                           sp++;
                        }
                        else
                           shift -= 4;
G
Guy Schalnat 已提交
2687
                     }
2688 2689 2690 2691 2692 2693 2694
                  }
                  else
#endif
                  {
                     sp = row;
                     shift = 4;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2695
                     {
2696
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        if (!shift)
                        {
                           shift = 4;
                           sp++;
                        }
                        else
                           shift -= 4;
G
Guy Schalnat 已提交
2709 2710 2711 2712 2713 2714
                     }
                  }
                  break;
               }
               case 8:
               {
G
Guy Schalnat 已提交
2715
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2716
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2717
                  {
2718 2719
                     sp = row;
                     for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
2720 2721 2722
                     {
                        if (*sp == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2723
                           *sp = (png_byte)background->gray;
G
Guy Schalnat 已提交
2724 2725 2726 2727 2728 2729 2730 2731
                        }
                        else
                        {
                           *sp = gamma_table[*sp];
                        }
                     }
                  }
                  else
G
Guy Schalnat 已提交
2732
#endif
G
Guy Schalnat 已提交
2733
                  {
2734 2735
                     sp = row;
                     for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
2736 2737 2738
                     {
                        if (*sp == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2739
                           *sp = (png_byte)background->gray;
G
Guy Schalnat 已提交
2740 2741 2742 2743 2744 2745 2746
                        }
                     }
                  }
                  break;
               }
               case 16:
               {
G
Guy Schalnat 已提交
2747
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2748
                  if (gamma_16 != NULL)
G
Guy Schalnat 已提交
2749
                  {
2750 2751
                     sp = row;
                     for (i = 0; i < row_width; i++, sp += 2)
G
Guy Schalnat 已提交
2752 2753 2754
                     {
                        png_uint_16 v;

2755
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
G
Guy Schalnat 已提交
2756 2757
                        if (v == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2758
                           /* background is already in screen gamma */
G
Guy Schalnat 已提交
2759 2760
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
G
Guy Schalnat 已提交
2761 2762 2763
                        }
                        else
                        {
A
Andreas Dilger 已提交
2764
                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
2765
                           *sp = (png_byte)((v >> 8) & 0xff);
G
Guy Schalnat 已提交
2766
                           *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
2767
                        }
G
Guy Schalnat 已提交
2768
                     }
G
Guy Schalnat 已提交
2769 2770
                  }
                  else
G
Guy Schalnat 已提交
2771
#endif
G
Guy Schalnat 已提交
2772
                  {
2773 2774
                     sp = row;
                     for (i = 0; i < row_width; i++, sp += 2)
G
Guy Schalnat 已提交
2775 2776 2777
                     {
                        png_uint_16 v;

2778
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
G
Guy Schalnat 已提交
2779 2780
                        if (v == trans_values->gray)
                        {
G
Guy Schalnat 已提交
2781 2782
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
G
Guy Schalnat 已提交
2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794
                        }
                     }
                  }
                  break;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
G
Guy Schalnat 已提交
2795
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2796
               if (gamma_table != NULL)
G
Guy Schalnat 已提交
2797
               {
2798 2799
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 3)
G
Guy Schalnat 已提交
2800 2801 2802 2803 2804
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2805 2806 2807
                        *sp = (png_byte)background->red;
                        *(sp + 1) = (png_byte)background->green;
                        *(sp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
                     }
                     else
                     {
                        *sp = gamma_table[*sp];
                        *(sp + 1) = gamma_table[*(sp + 1)];
                        *(sp + 2) = gamma_table[*(sp + 2)];
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
2818
#endif
G
Guy Schalnat 已提交
2819
               {
2820 2821
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 3)
G
Guy Schalnat 已提交
2822 2823 2824 2825 2826
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2827 2828 2829
                        *sp = (png_byte)background->red;
                        *(sp + 1) = (png_byte)background->green;
                        *(sp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
2830 2831 2832 2833
                     }
                  }
               }
            }
A
Andreas Dilger 已提交
2834
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
2835
            {
G
Guy Schalnat 已提交
2836
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2837
               if (gamma_16 != NULL)
G
Guy Schalnat 已提交
2838
               {
2839 2840
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 6)
G
Guy Schalnat 已提交
2841
                  {
2842 2843 2844
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
A
Andreas Dilger 已提交
2845
                     if (r == trans_values->red && g == trans_values->green &&
G
Guy Schalnat 已提交
2846 2847
                        b == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2848
                        /* background is already in screen gamma */
G
Guy Schalnat 已提交
2849 2850 2851 2852 2853 2854
                        *sp = (png_byte)((background->red >> 8) & 0xff);
                        *(sp + 1) = (png_byte)(background->red & 0xff);
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(background->green & 0xff);
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
G
Guy Schalnat 已提交
2855 2856 2857
                     }
                     else
                     {
2858
                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
2859
                        *sp = (png_byte)((v >> 8) & 0xff);
G
Guy Schalnat 已提交
2860
                        *(sp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
2861
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
G
Guy Schalnat 已提交
2862 2863
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
2864
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
G
Guy Schalnat 已提交
2865 2866
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
2867 2868 2869 2870
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
2871
#endif
G
Guy Schalnat 已提交
2872
               {
2873 2874
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 6)
G
Guy Schalnat 已提交
2875
                  {
2876 2877 2878
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp+1));
                     png_uint_16 g = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
                     png_uint_16 b = (png_uint_16)(((*(sp+4)) << 8) + *(sp+5));
G
Guy Schalnat 已提交
2879

A
Andreas Dilger 已提交
2880
                     if (r == trans_values->red && g == trans_values->green &&
G
Guy Schalnat 已提交
2881
                        b == trans_values->blue)
G
Guy Schalnat 已提交
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
                     {
                        *sp = (png_byte)((background->red >> 8) & 0xff);
                        *(sp + 1) = (png_byte)(background->red & 0xff);
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(background->green & 0xff);
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
                     }
                  }
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_GRAY_ALPHA:
G
Guy Schalnat 已提交
2896
         {
A
Andreas Dilger 已提交
2897
            if (row_info->bit_depth == 8)
G
Guy Schalnat 已提交
2898
            {
G
Guy Schalnat 已提交
2899
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2900 2901 2902
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                   gamma_table != NULL)
               {
2903 2904 2905
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
2906
                  {
2907
                     png_uint_16 a = *(sp + 1);
G
Guy Schalnat 已提交
2908

A
Andreas Dilger 已提交
2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920
                     if (a == 0xff)
                     {
                        *dp = gamma_table[*sp];
                     }
                     else if (a == 0)
                     {
                        /* background is already in screen gamma */
                        *dp = (png_byte)background->gray;
                     }
                     else
                     {
                        png_byte v, w;
G
Guy Schalnat 已提交
2921

A
Andreas Dilger 已提交
2922 2923 2924
                        v = gamma_to_1[*sp];
                        png_composite(w, v, a, background_1->gray);
                        *dp = gamma_from_1[w];
G
Guy Schalnat 已提交
2925 2926
                     }
                  }
A
Andreas Dilger 已提交
2927 2928
               }
               else
G
Guy Schalnat 已提交
2929
#endif
A
Andreas Dilger 已提交
2930
               {
2931 2932 2933
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
2934
                  {
2935
                     png_byte a = *(sp + 1);
G
Guy Schalnat 已提交
2936

A
Andreas Dilger 已提交
2937 2938 2939 2940
                     if (a == 0xff)
                     {
                        *dp = *sp;
                     }
2941
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2942 2943 2944 2945 2946 2947 2948
                     else if (a == 0)
                     {
                        *dp = (png_byte)background->gray;
                     }
                     else
                     {
                        png_composite(*dp, *sp, a, background_1->gray);
G
Guy Schalnat 已提交
2949
                     }
2950 2951 2952
#else
                     *dp = (png_byte)background->gray;
#endif
G
Guy Schalnat 已提交
2953 2954
                  }
               }
A
Andreas Dilger 已提交
2955 2956 2957
            }
            else /* if (png_ptr->bit_depth == 16) */
            {
G
Guy Schalnat 已提交
2958
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2959 2960 2961
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                   gamma_16_to_1 != NULL)
               {
2962 2963 2964
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
G
Guy Schalnat 已提交
2965
                  {
2966
                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
A
Andreas Dilger 已提交
2967 2968

                     if (a == (png_uint_16)0xffff)
G
Guy Schalnat 已提交
2969
                     {
A
Andreas Dilger 已提交
2970
                        png_uint_16 v;
G
Guy Schalnat 已提交
2971

A
Andreas Dilger 已提交
2972 2973 2974 2975
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
                     }
2976
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2977
                     else if (a == 0)
2978 2979 2980
#else
                     else
#endif
A
Andreas Dilger 已提交
2981 2982 2983 2984 2985
                     {
                        /* background is already in screen gamma */
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
                     }
2986
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2987 2988 2989
                     else
                     {
                        png_uint_16 g, v, w;
G
Guy Schalnat 已提交
2990

A
Andreas Dilger 已提交
2991 2992 2993 2994 2995
                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
                        png_composite_16(v, g, a, background_1->gray);
                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
                        *dp = (png_byte)((w >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(w & 0xff);
G
Guy Schalnat 已提交
2996
                     }
2997
#endif
G
Guy Schalnat 已提交
2998
                  }
A
Andreas Dilger 已提交
2999 3000
               }
               else
G
Guy Schalnat 已提交
3001
#endif
A
Andreas Dilger 已提交
3002
               {
3003 3004 3005
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
G
Guy Schalnat 已提交
3006
                  {
3007
                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
A
Andreas Dilger 已提交
3008
                     if (a == (png_uint_16)0xffff)
G
Guy Schalnat 已提交
3009
                     {
A
Andreas Dilger 已提交
3010 3011
                        png_memcpy(dp, sp, 2);
                     }
3012
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3013
                     else if (a == 0)
3014 3015 3016
#else
                     else
#endif
A
Andreas Dilger 已提交
3017 3018 3019 3020
                     {
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
                     }
3021
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3022 3023 3024
                     else
                     {
                        png_uint_16 g, v;
G
Guy Schalnat 已提交
3025

3026
                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
A
Andreas Dilger 已提交
3027 3028 3029
                        png_composite_16(v, g, a, background_1->gray);
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3030
                     }
3031
#endif
G
Guy Schalnat 已提交
3032 3033 3034 3035 3036 3037 3038 3039 3040
                  }
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
G
Guy Schalnat 已提交
3041
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3042 3043
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                   gamma_table != NULL)
G
Guy Schalnat 已提交
3044
               {
3045 3046 3047
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
G
Guy Schalnat 已提交
3048
                  {
3049
                     png_byte a = *(sp + 3);
G
Guy Schalnat 已提交
3050 3051 3052 3053 3054 3055 3056 3057 3058

                     if (a == 0xff)
                     {
                        *dp = gamma_table[*sp];
                        *(dp + 1) = gamma_table[*(sp + 1)];
                        *(dp + 2) = gamma_table[*(sp + 2)];
                     }
                     else if (a == 0)
                     {
A
Andreas Dilger 已提交
3059 3060 3061 3062
                        /* background is already in screen gamma */
                        *dp = (png_byte)background->red;
                        *(dp + 1) = (png_byte)background->green;
                        *(dp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
3063 3064 3065
                     }
                     else
                     {
A
Andreas Dilger 已提交
3066
                        png_byte v, w;
G
Guy Schalnat 已提交
3067 3068

                        v = gamma_to_1[*sp];
A
Andreas Dilger 已提交
3069 3070
                        png_composite(w, v, a, background_1->red);
                        *dp = gamma_from_1[w];
G
Guy Schalnat 已提交
3071
                        v = gamma_to_1[*(sp + 1)];
A
Andreas Dilger 已提交
3072 3073
                        png_composite(w, v, a, background_1->green);
                        *(dp + 1) = gamma_from_1[w];
G
Guy Schalnat 已提交
3074
                        v = gamma_to_1[*(sp + 2)];
A
Andreas Dilger 已提交
3075 3076
                        png_composite(w, v, a, background_1->blue);
                        *(dp + 2) = gamma_from_1[w];
G
Guy Schalnat 已提交
3077 3078 3079 3080
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
3081
#endif
G
Guy Schalnat 已提交
3082
               {
3083 3084 3085
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
G
Guy Schalnat 已提交
3086
                  {
3087
                     png_byte a = *(sp + 3);
G
Guy Schalnat 已提交
3088 3089 3090 3091 3092 3093 3094 3095 3096

                     if (a == 0xff)
                     {
                        *dp = *sp;
                        *(dp + 1) = *(sp + 1);
                        *(dp + 2) = *(sp + 2);
                     }
                     else if (a == 0)
                     {
A
Andreas Dilger 已提交
3097 3098 3099
                        *dp = (png_byte)background->red;
                        *(dp + 1) = (png_byte)background->green;
                        *(dp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
3100 3101 3102
                     }
                     else
                     {
A
Andreas Dilger 已提交
3103 3104 3105 3106 3107
                        png_composite(*dp, *sp, a, background->red);
                        png_composite(*(dp + 1), *(sp + 1), a,
                           background->green);
                        png_composite(*(dp + 2), *(sp + 2), a,
                           background->blue);
G
Guy Schalnat 已提交
3108 3109 3110 3111
                     }
                  }
               }
            }
A
Andreas Dilger 已提交
3112
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3113
            {
G
Guy Schalnat 已提交
3114
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3115 3116
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                   gamma_16_to_1 != NULL)
G
Guy Schalnat 已提交
3117
               {
3118 3119 3120
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
G
Guy Schalnat 已提交
3121
                  {
3122 3123
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
                         << 8) + (png_uint_16)(*(sp + 7)));
G
Guy Schalnat 已提交
3124 3125 3126 3127
                     if (a == (png_uint_16)0xffff)
                     {
                        png_uint_16 v;

A
Andreas Dilger 已提交
3128
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3129 3130
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3131
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
G
Guy Schalnat 已提交
3132 3133
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3134
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
G
Guy Schalnat 已提交
3135 3136
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3137 3138 3139
                     }
                     else if (a == 0)
                     {
A
Andreas Dilger 已提交
3140
                        /* background is already in screen gamma */
G
Guy Schalnat 已提交
3141 3142 3143 3144 3145 3146
                        *dp = (png_byte)((background->red >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->red & 0xff);
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(background->green & 0xff);
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
G
Guy Schalnat 已提交
3147 3148 3149
                     }
                     else
                     {
A
Andreas Dilger 已提交
3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166
                        png_uint_16 v, w, x;

                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
                        png_composite_16(w, v, a, background->red);
                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
                        *dp = (png_byte)((x >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(x & 0xff);
                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
                        png_composite_16(w, v, a, background->green);
                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(x & 0xff);
                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
                        png_composite_16(w, v, a, background->blue);
                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(x & 0xff);
G
Guy Schalnat 已提交
3167 3168 3169 3170
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
3171
#endif
G
Guy Schalnat 已提交
3172
               {
3173 3174 3175
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
G
Guy Schalnat 已提交
3176
                  {
3177 3178
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
                        << 8) + (png_uint_16)(*(sp + 7)));
G
Guy Schalnat 已提交
3179 3180
                     if (a == (png_uint_16)0xffff)
                     {
G
Guy Schalnat 已提交
3181
                        png_memcpy(dp, sp, 6);
G
Guy Schalnat 已提交
3182 3183 3184
                     }
                     else if (a == 0)
                     {
G
Guy Schalnat 已提交
3185 3186 3187 3188 3189 3190 3191 3192
                        *dp = (png_byte)((background->red >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->red & 0xff);
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(background->green & 0xff);
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
                     }
                     else
G
Guy Schalnat 已提交
3193
                     {
3194
                        png_uint_16 v;
A
Andreas Dilger 已提交
3195

3196 3197 3198 3199 3200
                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
                            + *(sp + 3));
                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
                            + *(sp + 5));
A
Andreas Dilger 已提交
3201 3202

                        png_composite_16(v, r, a, background->red);
G
Guy Schalnat 已提交
3203 3204
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3205
                        png_composite_16(v, g, a, background->green);
G
Guy Schalnat 已提交
3206 3207
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(v & 0xff);
3208
                        png_composite_16(v, b, a, background->blue);
G
Guy Schalnat 已提交
3209 3210 3211 3212 3213 3214 3215 3216 3217
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(v & 0xff);
                     }
                  }
               }
            }
            break;
         }
      }
A
Andreas Dilger 已提交
3218

G
Guy Schalnat 已提交
3219 3220 3221
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
      {
         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
G
Guy Schalnat 已提交
3222
         row_info->channels--;
G
Guy Schalnat 已提交
3223 3224
         row_info->pixel_depth = (png_byte)(row_info->channels *
            row_info->bit_depth);
3225
         row_info->rowbytes = ((row_width *
G
Guy Schalnat 已提交
3226 3227 3228 3229
            row_info->pixel_depth + 7) >> 3);
      }
   }
}
G
Guy Schalnat 已提交
3230
#endif
G
Guy Schalnat 已提交
3231

G
Guy Schalnat 已提交
3232
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3233
/* Gamma correct the image, avoiding the alpha channel.  Make sure
3234
 * you do this after you deal with the transparency issue on grayscale
3235
 * or RGB images. If your bit depth is 8, use gamma_table, if it
3236 3237 3238
 * is 16, use gamma_16_table and gamma_shift.  Build these with
 * build_gamma_table().
 */
3239
void /* PRIVATE */
G
Guy Schalnat 已提交
3240 3241
png_do_gamma(png_row_infop row_info, png_bytep row,
   png_bytep gamma_table, png_uint_16pp gamma_16_table,
G
Guy Schalnat 已提交
3242 3243
   int gamma_shift)
{
G
Guy Schalnat 已提交
3244
   png_bytep sp;
G
Guy Schalnat 已提交
3245
   png_uint_32 i;
3246
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3247

A
Andreas Dilger 已提交
3248 3249 3250 3251 3252 3253 3254
   png_debug(1, "in png_do_gamma\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
G
Guy Schalnat 已提交
3255 3256 3257 3258 3259 3260 3261
   {
      switch (row_info->color_type)
      {
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
3262 3263
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3264 3265 3266 3267 3268 3269 3270 3271 3272
               {
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3273
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3274
            {
3275 3276
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3277 3278 3279
               {
                  png_uint_16 v;

A
Andreas Dilger 已提交
3280
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3281 3282 3283
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
A
Andreas Dilger 已提交
3284
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3285 3286
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3287
                  sp += 2;
A
Andreas Dilger 已提交
3288
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
3300 3301
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3302
               {
G
Guy Schalnat 已提交
3303 3304 3305 3306 3307 3308 3309 3310 3311
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3312
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3313
            {
3314 3315
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3316
               {
3317
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3318 3319
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3320
                  sp += 2;
A
Andreas Dilger 已提交
3321
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3322 3323 3324
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
A
Andreas Dilger 已提交
3325
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3326 3327 3328 3329 3330
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 4;
               }
            }
G
Guy Schalnat 已提交
3331 3332 3333 3334 3335 3336
            break;
         }
         case PNG_COLOR_TYPE_GRAY_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
3337 3338
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3339 3340
               {
                  *sp = gamma_table[*sp];
A
Andreas Dilger 已提交
3341
                  sp += 2;
G
Guy Schalnat 已提交
3342 3343
               }
            }
A
Andreas Dilger 已提交
3344
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3345
            {
3346 3347
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3348
               {
3349
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3350 3351
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3352 3353 3354 3355 3356 3357 3358
                  sp += 4;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_GRAY:
         {
3359 3360
            if (row_info->bit_depth == 2)
            {
3361 3362
               sp = row;
               for (i = 0; i < row_width; i += 4)
3363 3364 3365 3366 3367 3368
               {
                  int a = *sp & 0xc0;
                  int b = *sp & 0x30;
                  int c = *sp & 0x0c;
                  int d = *sp & 0x03;

3369 3370
                  *sp = (png_byte)(
                        ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
3371 3372
                        ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
                        ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3373
                        ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3374 3375 3376
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3377
            if (row_info->bit_depth == 4)
G
Guy Schalnat 已提交
3378
            {
3379 3380
               sp = row;
               for (i = 0; i < row_width; i += 2)
A
Andreas Dilger 已提交
3381 3382 3383 3384
               {
                  int msb = *sp & 0xf0;
                  int lsb = *sp & 0x0f;

3385 3386
                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
                          | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
A
Andreas Dilger 已提交
3387 3388 3389 3390 3391
                  sp++;
               }
            }
            else if (row_info->bit_depth == 8)
            {
3392 3393
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3394 3395 3396 3397 3398 3399 3400
               {
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }
            else if (row_info->bit_depth == 16)
            {
3401 3402
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3403
               {
3404
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3405 3406
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3407 3408 3409 3410 3411 3412 3413 3414
                  sp += 2;
               }
            }
            break;
         }
      }
   }
}
G
Guy Schalnat 已提交
3415
#endif
G
Guy Schalnat 已提交
3416

G
Guy Schalnat 已提交
3417
#if defined(PNG_READ_EXPAND_SUPPORTED)
3418
/* Expands a palette row to an RGB or RGBA row depending
3419 3420
 * upon whether you supply trans and num_trans.
 */
3421
void /* PRIVATE */
G
Guy Schalnat 已提交
3422
png_do_expand_palette(png_row_infop row_info, png_bytep row,
A
Andreas Dilger 已提交
3423
   png_colorp palette, png_bytep trans, int num_trans)
G
Guy Schalnat 已提交
3424
{
G
Guy Schalnat 已提交
3425
   int shift, value;
G
Guy Schalnat 已提交
3426
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3427
   png_uint_32 i;
3428
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3429

A
Andreas Dilger 已提交
3430 3431 3432 3433 3434 3435
   png_debug(1, "in png_do_expand_palette\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
3436 3437 3438 3439 3440 3441 3442
   {
      if (row_info->bit_depth < 8)
      {
         switch (row_info->bit_depth)
         {
            case 1:
            {
3443 3444
               sp = row + (png_size_t)((row_width - 1) >> 3);
               dp = row + (png_size_t)row_width - 1;
3445
               shift = 7 - (int)((row_width + 7) & 0x07);
3446
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3447
               {
3448
                  if ((*sp >> shift) & 0x01)
G
Guy Schalnat 已提交
3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465
                     *dp = 1;
                  else
                     *dp = 0;
                  if (shift == 7)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift++;

                  dp--;
               }
               break;
            }
            case 2:
            {
3466 3467
               sp = row + (png_size_t)((row_width - 1) >> 2);
               dp = row + (png_size_t)row_width - 1;
3468
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3469
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3470
               {
3471
                  value = (*sp >> shift) & 0x03;
G
Guy Schalnat 已提交
3472
                  *dp = (png_byte)value;
G
Guy Schalnat 已提交
3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486
                  if (shift == 6)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 2;

                  dp--;
               }
               break;
            }
            case 4:
            {
3487 3488
               sp = row + (png_size_t)((row_width - 1) >> 1);
               dp = row + (png_size_t)row_width - 1;
3489
               shift = (int)((row_width & 0x01) << 2);
3490
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3491
               {
3492
                  value = (*sp >> shift) & 0x0f;
G
Guy Schalnat 已提交
3493
                  *dp = (png_byte)value;
G
Guy Schalnat 已提交
3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508
                  if (shift == 4)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 4;

                  dp--;
               }
               break;
            }
         }
         row_info->bit_depth = 8;
         row_info->pixel_depth = 8;
3509
         row_info->rowbytes = row_width;
G
Guy Schalnat 已提交
3510 3511 3512 3513 3514
      }
      switch (row_info->bit_depth)
      {
         case 8:
         {
A
Andreas Dilger 已提交
3515
            if (trans != NULL)
G
Guy Schalnat 已提交
3516
            {
3517 3518
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width << 2) - 1;
G
Guy Schalnat 已提交
3519

3520
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3521
               {
A
Andreas Dilger 已提交
3522
                  if ((int)(*sp) >= num_trans)
G
Guy Schalnat 已提交
3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
                     *dp-- = 0xff;
                  else
                     *dp-- = trans[*sp];
                  *dp-- = palette[*sp].blue;
                  *dp-- = palette[*sp].green;
                  *dp-- = palette[*sp].red;
                  sp--;
               }
               row_info->bit_depth = 8;
               row_info->pixel_depth = 32;
3533
               row_info->rowbytes = row_width * 4;
G
Guy Schalnat 已提交
3534 3535 3536 3537 3538
               row_info->color_type = 6;
               row_info->channels = 4;
            }
            else
            {
3539 3540
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width * 3) - 1;
G
Guy Schalnat 已提交
3541

3542
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3543 3544 3545 3546 3547 3548 3549 3550
               {
                  *dp-- = palette[*sp].blue;
                  *dp-- = palette[*sp].green;
                  *dp-- = palette[*sp].red;
                  sp--;
               }
               row_info->bit_depth = 8;
               row_info->pixel_depth = 24;
3551
               row_info->rowbytes = row_width * 3;
G
Guy Schalnat 已提交
3552 3553 3554 3555 3556 3557 3558 3559 3560
               row_info->color_type = 2;
               row_info->channels = 3;
            }
            break;
         }
      }
   }
}

3561 3562 3563
/* If the bit depth < 8, it is expanded to 8.  Also, if the
 * transparency value is supplied, an alpha channel is built.
 */
3564
void /* PRIVATE */
G
Guy Schalnat 已提交
3565
png_do_expand(png_row_infop row_info, png_bytep row,
G
Guy Schalnat 已提交
3566
   png_color_16p trans_value)
G
Guy Schalnat 已提交
3567
{
G
Guy Schalnat 已提交
3568
   int shift, value;
G
Guy Schalnat 已提交
3569
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3570
   png_uint_32 i;
3571
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3572

A
Andreas Dilger 已提交
3573 3574 3575 3576
   png_debug(1, "in png_do_expand\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
G
Guy Schalnat 已提交
3577
   {
A
Andreas Dilger 已提交
3578
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
G
Guy Schalnat 已提交
3579
      {
3580
         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
A
Andreas Dilger 已提交
3581 3582

         if (row_info->bit_depth < 8)
G
Guy Schalnat 已提交
3583
         {
A
Andreas Dilger 已提交
3584
            switch (row_info->bit_depth)
G
Guy Schalnat 已提交
3585
            {
A
Andreas Dilger 已提交
3586
               case 1:
G
Guy Schalnat 已提交
3587
               {
3588
                  gray = (png_uint_16)(gray*0xff);
3589 3590
                  sp = row + (png_size_t)((row_width - 1) >> 3);
                  dp = row + (png_size_t)row_width - 1;
3591
                  shift = 7 - (int)((row_width + 7) & 0x07);
3592
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3593
                  {
3594
                     if ((*sp >> shift) & 0x01)
A
Andreas Dilger 已提交
3595 3596 3597 3598 3599 3600 3601 3602 3603 3604
                        *dp = 0xff;
                     else
                        *dp = 0;
                     if (shift == 7)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift++;
G
Guy Schalnat 已提交
3605

A
Andreas Dilger 已提交
3606 3607 3608
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3609
               }
A
Andreas Dilger 已提交
3610
               case 2:
G
Guy Schalnat 已提交
3611
               {
3612
                  gray = (png_uint_16)(gray*0x55);
3613 3614
                  sp = row + (png_size_t)((row_width - 1) >> 2);
                  dp = row + (png_size_t)row_width - 1;
3615
                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3616
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3617
                  {
3618
                     value = (*sp >> shift) & 0x03;
A
Andreas Dilger 已提交
3619 3620 3621 3622 3623 3624 3625 3626 3627
                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
                        (value << 6));
                     if (shift == 6)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift += 2;
G
Guy Schalnat 已提交
3628

A
Andreas Dilger 已提交
3629 3630 3631
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3632
               }
A
Andreas Dilger 已提交
3633
               case 4:
G
Guy Schalnat 已提交
3634
               {
3635
                  gray = (png_uint_16)(gray*0x11);
3636 3637
                  sp = row + (png_size_t)((row_width - 1) >> 1);
                  dp = row + (png_size_t)row_width - 1;
3638
                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3639
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3640
                  {
3641
                     value = (*sp >> shift) & 0x0f;
A
Andreas Dilger 已提交
3642 3643 3644 3645 3646 3647 3648 3649
                     *dp = (png_byte)(value | (value << 4));
                     if (shift == 4)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift = 4;
G
Guy Schalnat 已提交
3650

A
Andreas Dilger 已提交
3651 3652 3653
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3654 3655
               }
            }
A
Andreas Dilger 已提交
3656 3657
            row_info->bit_depth = 8;
            row_info->pixel_depth = 8;
3658
            row_info->rowbytes = row_width;
G
Guy Schalnat 已提交
3659
         }
A
Andreas Dilger 已提交
3660

A
Andreas Dilger 已提交
3661
         if (trans_value != NULL)
G
Guy Schalnat 已提交
3662
         {
A
Andreas Dilger 已提交
3663
            if (row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3664
            {
3665 3666 3667
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width << 1) - 1;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3668
               {
A
Andreas Dilger 已提交
3669 3670 3671 3672 3673
                  if (*sp == gray)
                     *dp-- = 0;
                  else
                     *dp-- = 0xff;
                  *dp-- = *sp--;
G
Guy Schalnat 已提交
3674
               }
A
Andreas Dilger 已提交
3675 3676 3677
            }
            else if (row_info->bit_depth == 16)
            {
A
Andreas Dilger 已提交
3678 3679
               sp = row + row_info->rowbytes - 1;
               dp = row + (row_info->rowbytes << 1) - 1;
3680
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3681
               {
A
Andreas Dilger 已提交
3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694
                  if (((png_uint_16)*(sp) |
                     ((png_uint_16)*(sp - 1) << 8)) == gray)
                  {
                     *dp-- = 0;
                     *dp-- = 0;
                  }
                  else
                  {
                     *dp-- = 0xff;
                     *dp-- = 0xff;
                  }
                  *dp-- = *sp--;
                  *dp-- = *sp--;
G
Guy Schalnat 已提交
3695 3696
               }
            }
A
Andreas Dilger 已提交
3697 3698 3699 3700
            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
            row_info->channels = 2;
            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
            row_info->rowbytes =
3701
               ((row_width * row_info->pixel_depth) >> 3);
G
Guy Schalnat 已提交
3702 3703 3704 3705 3706 3707 3708
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
      {
         if (row_info->bit_depth == 8)
         {
            sp = row + (png_size_t)row_info->rowbytes - 1;
3709 3710
            dp = row + (png_size_t)(row_width << 2) - 1;
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3711
            {
G
Guy Schalnat 已提交
3712
               if (*(sp - 2) == trans_value->red &&
G
Guy Schalnat 已提交
3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724
                  *(sp - 1) == trans_value->green &&
                  *(sp - 0) == trans_value->blue)
                  *dp-- = 0;
               else
                  *dp-- = 0xff;
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
            }
         }
         else if (row_info->bit_depth == 16)
         {
A
Andreas Dilger 已提交
3725
            sp = row + row_info->rowbytes - 1;
3726 3727
            dp = row + (png_size_t)(row_width << 3) - 1;
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3728 3729
            {
               if ((((png_uint_16)*(sp - 4) |
G
Guy Schalnat 已提交
3730
                  ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) &&
G
Guy Schalnat 已提交
3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747
                  (((png_uint_16)*(sp - 2) |
                  ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) &&
                  (((png_uint_16)*(sp - 0) |
                  ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue))
               {
                  *dp-- = 0;
                  *dp-- = 0;
               }
               else
               {
                  *dp-- = 0xff;
                  *dp-- = 0xff;
               }
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
G
Guy Schalnat 已提交
3748
               *dp-- = *sp--;
G
Guy Schalnat 已提交
3749 3750 3751 3752 3753
               *dp-- = *sp--;
            }
         }
         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
         row_info->channels = 4;
G
Guy Schalnat 已提交
3754
         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
G
Guy Schalnat 已提交
3755
         row_info->rowbytes =
3756
            ((row_width * row_info->pixel_depth) >> 3);
G
Guy Schalnat 已提交
3757 3758 3759
      }
   }
}
G
Guy Schalnat 已提交
3760
#endif
G
Guy Schalnat 已提交
3761

G
Guy Schalnat 已提交
3762
#if defined(PNG_READ_DITHER_SUPPORTED)
3763
void /* PRIVATE */
G
Guy Schalnat 已提交
3764
png_do_dither(png_row_infop row_info, png_bytep row,
G
Guy Schalnat 已提交
3765
    png_bytep palette_lookup, png_bytep dither_lookup)
G
Guy Schalnat 已提交
3766
{
G
Guy Schalnat 已提交
3767
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3768
   png_uint_32 i;
3769
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3770

A
Andreas Dilger 已提交
3771 3772 3773 3774
   png_debug(1, "in png_do_dither\n");
#if defined(PNG_USELESS_TESTS_SUPPORTED)
   if (row != NULL && row_info != NULL)
#endif
G
Guy Schalnat 已提交
3775 3776 3777 3778 3779 3780 3781
   {
      if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
         palette_lookup && row_info->bit_depth == 8)
      {
         int r, g, b, p;
         sp = row;
         dp = row;
3782
         for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798
         {
            r = *sp++;
            g = *sp++;
            b = *sp++;

            /* this looks real messy, but the compiler will reduce
               it down to a reasonable formula.  For example, with
               5 bits per color, we get:
               p = (((r >> 3) & 0x1f) << 10) |
                  (((g >> 3) & 0x1f) << 5) |
                  ((b >> 3) & 0x1f);
               */
            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
G
Guy Schalnat 已提交
3799
               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
G
Guy Schalnat 已提交
3800 3801 3802 3803 3804 3805 3806 3807 3808 3809
               (PNG_DITHER_BLUE_BITS)) |
               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
               ((1 << PNG_DITHER_BLUE_BITS) - 1));

            *dp++ = palette_lookup[p];
         }
         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
         row_info->channels = 1;
         row_info->pixel_depth = row_info->bit_depth;
         row_info->rowbytes =
3810
             ((row_width * row_info->pixel_depth + 7) >> 3);
G
Guy Schalnat 已提交
3811 3812
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
A
Andreas Dilger 已提交
3813
         palette_lookup != NULL && row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3814 3815 3816 3817
      {
         int r, g, b, p;
         sp = row;
         dp = row;
3818
         for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3819 3820 3821 3822 3823 3824 3825
         {
            r = *sp++;
            g = *sp++;
            b = *sp++;
            sp++;

            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
G
Guy Schalnat 已提交
3826
               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
G
Guy Schalnat 已提交
3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839
               (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
               (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
               (PNG_DITHER_BLUE_BITS)) |
               ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
               ((1 << PNG_DITHER_BLUE_BITS) - 1));

            *dp++ = palette_lookup[p];
         }
         row_info->color_type = PNG_COLOR_TYPE_PALETTE;
         row_info->channels = 1;
         row_info->pixel_depth = row_info->bit_depth;
         row_info->rowbytes =
3840
            ((row_width * row_info->pixel_depth + 7) >> 3);
G
Guy Schalnat 已提交
3841 3842 3843
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
         dither_lookup && row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3844
      {
G
Guy Schalnat 已提交
3845
         sp = row;
3846
         for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
3847 3848 3849 3850 3851 3852
         {
            *sp = dither_lookup[*sp];
         }
      }
   }
}
G
Guy Schalnat 已提交
3853
#endif
G
Guy Schalnat 已提交
3854

3855
#ifdef PNG_FLOATING_POINT_SUPPORTED
G
Guy Schalnat 已提交
3856
#if defined(PNG_READ_GAMMA_SUPPORTED)
G
Guy Schalnat 已提交
3857 3858 3859
static int png_gamma_shift[] =
   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0};

A
Andreas Dilger 已提交
3860
/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
3861 3862 3863 3864
 * tables, we don't make a full table if we are reducing to 8-bit in
 * the future.  Note also how the gamma_16 tables are segmented so that
 * we don't need to allocate > 64K chunks for a full 16-bit table.
 */
3865
void /* PRIVATE */
G
Guy Schalnat 已提交
3866
png_build_gamma_table(png_structp png_ptr)
G
Guy Schalnat 已提交
3867
{
3868 3869 3870
  png_debug(1, "in png_build_gamma_table\n");
  if(png_ptr->gamma != 0.0)
  {
G
Guy Schalnat 已提交
3871 3872 3873 3874 3875
   if (png_ptr->bit_depth <= 8)
   {
      int i;
      double g;

3876 3877 3878 3879
      if (png_ptr->screen_gamma > .000001)
         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
      else
         g = 1.0;
G
Guy Schalnat 已提交
3880

A
Andreas Dilger 已提交
3881
      png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
G
Guy Schalnat 已提交
3882 3883 3884 3885 3886 3887 3888 3889
         (png_uint_32)256);

      for (i = 0; i < 256; i++)
      {
         png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
            g) * 255.0 + .5);
      }

3890 3891
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
3892
      if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
G
Guy Schalnat 已提交
3893
      {
3894

G
Guy Schalnat 已提交
3895 3896
         g = 1.0 / (png_ptr->gamma);

A
Andreas Dilger 已提交
3897
         png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
G
Guy Schalnat 已提交
3898 3899 3900 3901 3902 3903 3904 3905
            (png_uint_32)256);

         for (i = 0; i < 256; i++)
         {
            png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
               g) * 255.0 + .5);
         }

3906

A
Andreas Dilger 已提交
3907
         png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
G
Guy Schalnat 已提交
3908 3909
            (png_uint_32)256);

3910 3911 3912 3913 3914
         if(png_ptr->screen_gamma > 0.000001)
            g = 1.0 / png_ptr->screen_gamma;
         else
            g = png_ptr->gamma;   /* probably doing rgb_to_gray */

G
Guy Schalnat 已提交
3915 3916 3917 3918
         for (i = 0; i < 256; i++)
         {
            png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
               g) * 255.0 + .5);
3919

G
Guy Schalnat 已提交
3920 3921
         }
      }
3922
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
G
Guy Schalnat 已提交
3923
   }
G
Guy Schalnat 已提交
3924
   else
G
Guy Schalnat 已提交
3925 3926
   {
      double g;
G
Guy Schalnat 已提交
3927 3928 3929
      int i, j, shift, num;
      int sig_bit;
      png_uint_32 ig;
G
Guy Schalnat 已提交
3930 3931

      if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
G
Guy Schalnat 已提交
3932
      {
G
Guy Schalnat 已提交
3933 3934 3935 3936
         sig_bit = (int)png_ptr->sig_bit.red;
         if ((int)png_ptr->sig_bit.green > sig_bit)
            sig_bit = png_ptr->sig_bit.green;
         if ((int)png_ptr->sig_bit.blue > sig_bit)
G
Guy Schalnat 已提交
3937
            sig_bit = png_ptr->sig_bit.blue;
G
Guy Schalnat 已提交
3938 3939 3940 3941
      }
      else
      {
         sig_bit = (int)png_ptr->sig_bit.gray;
G
Guy Schalnat 已提交
3942
      }
G
Guy Schalnat 已提交
3943 3944 3945 3946

      if (sig_bit > 0)
         shift = 16 - sig_bit;
      else
G
Guy Schalnat 已提交
3947
         shift = 0;
G
Guy Schalnat 已提交
3948 3949 3950 3951 3952 3953 3954

      if (png_ptr->transformations & PNG_16_TO_8)
      {
         if (shift < (16 - PNG_MAX_GAMMA_8))
            shift = (16 - PNG_MAX_GAMMA_8);
      }

G
Guy Schalnat 已提交
3955
      if (shift > 8)
G
Guy Schalnat 已提交
3956 3957 3958 3959
         shift = 8;
      if (shift < 0)
         shift = 0;

G
Guy Schalnat 已提交
3960
      png_ptr->gamma_shift = (png_byte)shift;
G
Guy Schalnat 已提交
3961

G
Guy Schalnat 已提交
3962
      num = (1 << (8 - shift));
G
Guy Schalnat 已提交
3963

3964 3965 3966 3967
      if (png_ptr->screen_gamma > .000001)
         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
      else
         g = 1.0;
G
Guy Schalnat 已提交
3968

A
Andreas Dilger 已提交
3969
      png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
3970
         (png_uint_32)(num * sizeof (png_uint_16p)));
G
Guy Schalnat 已提交
3971

3972
      if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
G
Guy Schalnat 已提交
3973 3974 3975 3976 3977 3978
      {
         double fin, fout;
         png_uint_32 last, max;

         for (i = 0; i < num; i++)
         {
A
Andreas Dilger 已提交
3979
            png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
3980
               (png_uint_32)(256 * sizeof (png_uint_16)));
G
Guy Schalnat 已提交
3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000
         }

         g = 1.0 / g;
         last = 0;
         for (i = 0; i < 256; i++)
         {
            fout = ((double)i + 0.5) / 256.0;
            fin = pow(fout, g);
            max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
            while (last <= max)
            {
               png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
                  [(int)(last >> (8 - shift))] = (png_uint_16)(
                  (png_uint_16)i | ((png_uint_16)i << 8));
               last++;
            }
         }
         while (last < ((png_uint_32)num << 8))
         {
            png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
A
Andreas Dilger 已提交
4001
               [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
G
Guy Schalnat 已提交
4002
            last++;
G
Guy Schalnat 已提交
4003
         }
G
Guy Schalnat 已提交
4004
      }
G
Guy Schalnat 已提交
4005 4006 4007 4008
      else
      {
         for (i = 0; i < num; i++)
         {
A
Andreas Dilger 已提交
4009
            png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
4010
               (png_uint_32)(256 * sizeof (png_uint_16)));
G
Guy Schalnat 已提交
4011

A
Andreas Dilger 已提交
4012
            ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
G
Guy Schalnat 已提交
4013
            for (j = 0; j < 256; j++)
G
Guy Schalnat 已提交
4014 4015 4016 4017 4018 4019 4020 4021
            {
               png_ptr->gamma_16_table[i][j] =
                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
                     65535.0, g) * 65535.0 + .5);
            }
         }
      }

4022 4023 4024
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
      if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
G
Guy Schalnat 已提交
4025
      {
4026

G
Guy Schalnat 已提交
4027 4028
         g = 1.0 / (png_ptr->gamma);

A
Andreas Dilger 已提交
4029
         png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
4030
            (png_uint_32)(num * sizeof (png_uint_16p )));
G
Guy Schalnat 已提交
4031 4032 4033

         for (i = 0; i < num; i++)
         {
A
Andreas Dilger 已提交
4034
            png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
4035
               (png_uint_32)(256 * sizeof (png_uint_16)));
G
Guy Schalnat 已提交
4036 4037 4038 4039 4040 4041 4042 4043 4044 4045

            ig = (((png_uint_32)i *
               (png_uint_32)png_gamma_shift[shift]) >> 4);
            for (j = 0; j < 256; j++)
            {
               png_ptr->gamma_16_to_1[i][j] =
                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
                     65535.0, g) * 65535.0 + .5);
            }
         }
4046 4047 4048 4049 4050

         if(png_ptr->screen_gamma > 0.000001)
            g = 1.0 / png_ptr->screen_gamma;
         else
            g = png_ptr->gamma;   /* probably doing rgb_to_gray */
G
Guy Schalnat 已提交
4051

A
Andreas Dilger 已提交
4052
         png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
4053
            (png_uint_32)(num * sizeof (png_uint_16p)));
G
Guy Schalnat 已提交
4054 4055 4056

         for (i = 0; i < num; i++)
         {
A
Andreas Dilger 已提交
4057
            png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
4058
               (png_uint_32)(256 * sizeof (png_uint_16)));
G
Guy Schalnat 已提交
4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069

            ig = (((png_uint_32)i *
               (png_uint_32)png_gamma_shift[shift]) >> 4);
            for (j = 0; j < 256; j++)
            {
               png_ptr->gamma_16_from_1[i][j] =
                  (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
                     65535.0, g) * 65535.0 + .5);
            }
         }
      }
4070
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
G
Guy Schalnat 已提交
4071
   }
4072
 }
G
Guy Schalnat 已提交
4073
}
G
Guy Schalnat 已提交
4074
#endif
4075 4076
/* To do: install integer version of png_build_gamma_table here */
#endif
G
Guy Schalnat 已提交
4077

4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137
#if defined(PNG_MNG_FEATURES_SUPPORTED)
/* undoes intrapixel differencing  */
void /* PRIVATE */
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_intrapixel\n");
   if (
#if defined(PNG_USELESS_TESTS_SUPPORTED)
       row != NULL && row_info != NULL &&
#endif
       (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
      int bytes_per_pixel;
      png_uint_32 row_width = row_info->width;
      if (row_info->bit_depth == 8)
      {
         png_bytep rp;
         png_uint_32 i;

         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
            bytes_per_pixel = 3;
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
            bytes_per_pixel = 4;
         else
            return;

         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
         {
            *(rp) = (png_byte)((256 + *rp + *(rp+1))&0xff);
            *(rp+2) = (png_byte)((256 + *(rp+2) + *(rp+1))&0xff);
         }
      }
      else if (row_info->bit_depth == 16)
      {
         png_bytep rp;
         png_uint_32 i;

         if (row_info->color_type == PNG_COLOR_TYPE_RGB)
            bytes_per_pixel = 6;
         else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
            bytes_per_pixel = 8;
         else
            return;

         for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
         {
            png_uint_32 s0=*(rp  )<<8 | *(rp+1);
            png_uint_32 s1=*(rp+2)<<8 | *(rp+3);
            png_uint_32 s2=*(rp+4)<<8 | *(rp+5);
            png_uint_32 red=(65536+s0+s1)&0xffff;
            png_uint_32 blue=(65536+s2+s1)&0xffff;
            *(rp  ) = (png_byte)((red>>8)&0xff);
            *(rp+1) = (png_byte)(red&0xff);
            *(rp+4) = (png_byte)((blue>>8)&0xff);
            *(rp+5) = (png_byte)(blue&0xff);
         }
      }
   }
}
#endif /* PNG_MNG_FEATURES_SUPPORTED */