You need to sign in or sign up before continuing.
pngrtran.c 136.3 KB
Newer Older
G
Guy Schalnat 已提交
1

A
Andreas Dilger 已提交
2
/* pngrtran.c - transforms the data in a row for PNG readers
3
 *
4
 * Last changed in libpng 1.4.0 [August 4, 2008]
5
 * For conditions of distribution and use, see copyright notice in png.h
6
 * Copyright (c) 1998-2008 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

#include "png.h"
17
#if defined(PNG_READ_SUPPORTED)
18
#include "pngpriv.h"
19

20
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
21
void PNGAPI
22 23 24 25
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 */
26
   if (png_ptr == NULL) return;
27 28 29 30 31 32 33 34 35 36 37 38 39 40
   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 */
41 42
         png_warning(png_ptr,
            "Can't discard critical data on CRC error");
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 72 73 74
      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;
   }
}

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

G
Guy Schalnat 已提交
91
   png_ptr->transformations |= PNG_BACKGROUND;
92 93
   png_memcpy(&(png_ptr->background), background_color,
      png_sizeof(png_color_16));
G
Guy Schalnat 已提交
94
   png_ptr->background_gamma = (float)background_gamma;
G
Guy Schalnat 已提交
95
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
G
Guy Schalnat 已提交
96
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
G
Guy Schalnat 已提交
97
}
G
Guy Schalnat 已提交
98
#endif
G
Guy Schalnat 已提交
99

G
Guy Schalnat 已提交
100
#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
101
/* strip 16 bit depth files to 8 bit depth */
102
void PNGAPI
G
Guy Schalnat 已提交
103
png_set_strip_16(png_structp png_ptr)
G
Guy Schalnat 已提交
104
{
A
Andreas Dilger 已提交
105
   png_debug(1, "in png_set_strip_16\n");
106
   if (png_ptr == NULL) return;
G
Guy Schalnat 已提交
107 108
   png_ptr->transformations |= PNG_16_TO_8;
}
G
Guy Schalnat 已提交
109
#endif
G
Guy Schalnat 已提交
110

A
Andreas Dilger 已提交
111
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
112
void PNGAPI
A
Andreas Dilger 已提交
113 114 115
png_set_strip_alpha(png_structp png_ptr)
{
   png_debug(1, "in png_set_strip_alpha\n");
116
   if (png_ptr == NULL) return;
117
   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
A
Andreas Dilger 已提交
118 119 120
}
#endif

G
Guy Schalnat 已提交
121
#if defined(PNG_READ_DITHER_SUPPORTED)
A
Andreas Dilger 已提交
122
/* Dither file to 8 bit.  Supply a palette, the current number
123 124 125 126 127 128 129
 * 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 已提交
130 131

typedef struct png_dsort_struct
G
Guy Schalnat 已提交
132
{
G
Guy Schalnat 已提交
133
   struct png_dsort_struct FAR * next;
G
Guy Schalnat 已提交
134 135
   png_byte left;
   png_byte right;
G
Guy Schalnat 已提交
136 137 138
} png_dsort;
typedef png_dsort FAR *       png_dsortp;
typedef png_dsort FAR * FAR * png_dsortpp;
G
Guy Schalnat 已提交
139

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

G
Guy Schalnat 已提交
149
   if (!full_dither)
G
Guy Schalnat 已提交
150 151 152
   {
      int i;

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

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

         int i;

         /* initialize an array to sort colors */
169
         png_ptr->dither_sort = (png_bytep)png_malloc(png_ptr,
170
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
G
Guy Schalnat 已提交
171

172
         /* initialize the dither_sort array */
G
Guy Schalnat 已提交
173
         for (i = 0; i < num_palette; i++)
174
            png_ptr->dither_sort[i] = (png_byte)i;
G
Guy Schalnat 已提交
175

A
Andreas Dilger 已提交
176
         /* Find the least used palette entries by starting a
G
Guy Schalnat 已提交
177 178 179 180 181 182 183 184
            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 已提交
185
            int j;
G
Guy Schalnat 已提交
186 187 188 189

            done = 1;
            for (j = 0; j < i; j++)
            {
190 191
               if (histogram[png_ptr->dither_sort[j]]
                   < histogram[png_ptr->dither_sort[j + 1]])
G
Guy Schalnat 已提交
192 193 194
               {
                  png_byte t;

195 196 197
                  t = png_ptr->dither_sort[j];
                  png_ptr->dither_sort[j] = png_ptr->dither_sort[j + 1];
                  png_ptr->dither_sort[j + 1] = t;
G
Guy Schalnat 已提交
198 199 200 201 202 203 204 205 206 207
                  done = 0;
               }
            }
            if (done)
               break;
         }

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

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

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

                  do
                     j--;
238
                  while ((int)png_ptr->dither_sort[j] >= maximum_colors);
G
Guy Schalnat 已提交
239 240 241 242 243

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

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

G
Guy Schalnat 已提交
256
                  /* find the closest color to one we threw out */
A
Andreas Dilger 已提交
257 258 259
                  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 已提交
260 261 262
                  {
                     int d;

A
Andreas Dilger 已提交
263
                     d = PNG_COLOR_DIST(palette[d_index], palette[k]);
G
Guy Schalnat 已提交
264 265 266 267

                     if (d < min_d)
                     {
                        min_d = d;
A
Andreas Dilger 已提交
268
                        min_k = k;
G
Guy Schalnat 已提交
269 270
                     }
                  }
G
Guy Schalnat 已提交
271
                  /* point to closest color */
A
Andreas Dilger 已提交
272
                  png_ptr->dither_index[i] = (png_byte)min_k;
G
Guy Schalnat 已提交
273 274 275
               }
            }
         }
276
         png_free(png_ptr, png_ptr->dither_sort);
277
         png_ptr->dither_sort = NULL;
G
Guy Schalnat 已提交
278 279 280
      }
      else
      {
A
Andreas Dilger 已提交
281
         /* This is much harder to do simply (and quickly).  Perhaps
G
Guy Schalnat 已提交
282 283 284 285
            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).
286 287
            [We don't understand this at all, so if someone wants to
             work on improving it, be our guest - AED, GRP]
G
Guy Schalnat 已提交
288 289 290 291
            */
         int i;
         int max_d;
         int num_new_palette;
292
         png_dsortp t;
G
Guy Schalnat 已提交
293
         png_dsortpp hash;
294

295
         t = NULL;
G
Guy Schalnat 已提交
296 297

         /* initialize palette index arrays */
298
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
299
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
300
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
301
            (png_uint_32)(num_palette * png_sizeof(png_byte)));
G
Guy Schalnat 已提交
302 303 304 305

         /* initialize the sort array */
         for (i = 0; i < num_palette; i++)
         {
306 307
            png_ptr->index_to_palette[i] = (png_byte)i;
            png_ptr->palette_to_index[i] = (png_byte)i;
G
Guy Schalnat 已提交
308 309
         }

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

         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 已提交
328
         while (num_new_palette > maximum_colors)
G
Guy Schalnat 已提交
329 330 331 332 333 334 335 336 337
         {
            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 已提交
338
                  d = PNG_COLOR_DIST(palette[i], palette[j]);
G
Guy Schalnat 已提交
339 340 341 342

                  if (d <= max_d)
                  {

343
                     t = (png_dsortp)png_malloc_warn(png_ptr,
344
                         (png_uint_32)(png_sizeof(png_dsort)));
345 346
                     if (t == NULL)
                         break;
G
Guy Schalnat 已提交
347
                     t->next = hash[d];
G
Guy Schalnat 已提交
348 349
                     t->left = (png_byte)i;
                     t->right = (png_byte)j;
G
Guy Schalnat 已提交
350 351 352
                     hash[d] = t;
                  }
               }
353 354
               if (t == NULL)
                  break;
G
Guy Schalnat 已提交
355 356
            }

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

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

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

                        num_new_palette--;
385 386
                        palette[png_ptr->index_to_palette[j]]
                          = palette[num_new_palette];
G
Guy Schalnat 已提交
387 388 389 390 391 392 393
                        if (!full_dither)
                        {
                           int k;

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

404 405 406 407
                        png_ptr->index_to_palette[png_ptr->palette_to_index
                           [num_new_palette]] = png_ptr->index_to_palette[j];
                        png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
                           = png_ptr->palette_to_index[num_new_palette];
G
Guy Schalnat 已提交
408

409 410
                        png_ptr->index_to_palette[j] = (png_byte)num_new_palette;
                        png_ptr->palette_to_index[num_new_palette] = (png_byte)j;
G
Guy Schalnat 已提交
411 412 413 414 415 416 417 418 419 420 421
                     }
                     if (num_new_palette <= maximum_colors)
                        break;
                  }
                  if (num_new_palette <= maximum_colors)
                     break;
               }
            }

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

   if (full_dither)
   {
      int i;
G
Guy Schalnat 已提交
453
      png_bytep distance;
454
      int total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
G
Guy Schalnat 已提交
455
         PNG_DITHER_BLUE_BITS;
456 457 458 459
      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 已提交
460

461 462
      png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
         (png_uint_32)(num_entries * png_sizeof(png_byte)));
463

464 465
      png_memset(png_ptr->palette_lookup, 0, num_entries *
         png_sizeof(png_byte));
G
Guy Schalnat 已提交
466

467 468
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
         png_sizeof(png_byte)));
469

470
      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
471 472 473

      for (i = 0; i < num_palette; i++)
      {
474 475 476 477
         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 已提交
478 479 480

         for (ir = 0; ir < num_red; ir++)
         {
481 482
            /* int dr = abs(ir - r); */
            int dr = ((ir > r) ? ir - r : r - ir);
483
            int index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
G
Guy Schalnat 已提交
484 485

            for (ig = 0; ig < num_green; ig++)
G
Guy Schalnat 已提交
486
            {
487 488
               /* int dg = abs(ig - g); */
               int dg = ((ig > g) ? ig - g : g - ig);
489 490 491
               int dt = dr + dg;
               int dm = ((dr > dg) ? dr : dg);
               int index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
G
Guy Schalnat 已提交
492 493

               for (ib = 0; ib < num_blue; ib++)
G
Guy Schalnat 已提交
494
               {
495
                  int d_index = index_g | ib;
496 497
                  /* int db = abs(ib - b); */
                  int db = ((ib > b) ? ib - b : b - ib);
498 499
                  int dmax = ((dm > db) ? dm : db);
                  int d = dmax + dt + db;
G
Guy Schalnat 已提交
500

501
                  if (d < (int)distance[d_index])
G
Guy Schalnat 已提交
502
                  {
A
Andreas Dilger 已提交
503 504
                     distance[d_index] = (png_byte)d;
                     png_ptr->palette_lookup[d_index] = (png_byte)i;
G
Guy Schalnat 已提交
505 506
                  }
               }
507 508 509
            }
         }
      }
G
Guy Schalnat 已提交
510

A
Andreas Dilger 已提交
511
      png_free(png_ptr, distance);
G
Guy Schalnat 已提交
512 513
   }
}
G
Guy Schalnat 已提交
514
#endif
G
Guy Schalnat 已提交
515

516
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
A
Andreas Dilger 已提交
517
/* Transform the image from the file_gamma to the screen_gamma.  We
518 519 520
 * 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.
521 522 523 524
 *
 * We will turn off gamma transformation later if no semitransparent entries
 * are present in the tRNS array for palette images.  We can't do it here
 * because we don't necessarily have the tRNS chunk yet.
525
 */
526
void PNGAPI
527
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
G
Guy Schalnat 已提交
528
{
A
Andreas Dilger 已提交
529
   png_debug(1, "in png_set_gamma\n");
530
   if (png_ptr == NULL) return;
531 532 533 534
   if ((fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD) ||
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
     png_ptr->transformations |= PNG_GAMMA;
G
Guy Schalnat 已提交
535
   png_ptr->gamma = (float)file_gamma;
536
   png_ptr->screen_gamma = (float)scrn_gamma;
G
Guy Schalnat 已提交
537
}
G
Guy Schalnat 已提交
538
#endif
G
Guy Schalnat 已提交
539

G
Guy Schalnat 已提交
540
#if defined(PNG_READ_EXPAND_SUPPORTED)
541
/* Expand paletted images to RGB, expand grayscale images of
542
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
543 544
 * to alpha channels.
 */
545
void PNGAPI
G
Guy Schalnat 已提交
546
png_set_expand(png_structp png_ptr)
G
Guy Schalnat 已提交
547
{
A
Andreas Dilger 已提交
548
   png_debug(1, "in png_set_expand\n");
549
   if (png_ptr == NULL) return;
550
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
551
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
G
Guy Schalnat 已提交
552
}
553 554 555 556 557 558 559 560 561 562 563 564 565

/* 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.
566 567 568
 *
 *  GRP 20060307: In libpng-1.4.0, png_set_gray_1_2_4_to_8() was modified
 *  to expand only the sample depth but not to expand the tRNS to alpha.
569 570 571
 */

/* Expand paletted images to RGB. */
572
void PNGAPI
573 574
png_set_palette_to_rgb(png_structp png_ptr)
{
575
   png_debug(1, "in png_set_palette_to_rgb\n");
576
   if (png_ptr == NULL) return;
577
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
578
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
579 580 581 582 583 584 585
}

/* Expand grayscale images of less than 8-bit depth to 8 bits. */
void PNGAPI
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
{
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8\n");
586 587 588
   if (png_ptr == NULL) return;
   png_ptr->transformations |= PNG_EXPAND;
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
589 590
}

591 592


593
/* Expand tRNS chunks to alpha channels. */
594
void PNGAPI
595 596
png_set_tRNS_to_alpha(png_structp png_ptr)
{
597
   png_debug(1, "in png_set_tRNS_to_alpha\n");
598
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
599
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
600 601
}
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
G
Guy Schalnat 已提交
602

G
Guy Schalnat 已提交
603
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
604
void PNGAPI
G
Guy Schalnat 已提交
605
png_set_gray_to_rgb(png_structp png_ptr)
G
Guy Schalnat 已提交
606
{
A
Andreas Dilger 已提交
607
   png_debug(1, "in png_set_gray_to_rgb\n");
G
Guy Schalnat 已提交
608
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
609
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
G
Guy Schalnat 已提交
610
}
G
Guy Schalnat 已提交
611
#endif
G
Guy Schalnat 已提交
612

613 614
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
#if defined(PNG_FLOATING_POINT_SUPPORTED)
615 616
/* 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.
617
 */
618

619
void PNGAPI
620
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
621 622 623 624
   double green)
{
      int red_fixed = (int)((float)red*100000.0 + 0.5);
      int green_fixed = (int)((float)green*100000.0 + 0.5);
625
      if (png_ptr == NULL) return;
626 627 628 629
      png_set_rgb_to_gray_fixed(png_ptr, error_action, red_fixed, green_fixed);
}
#endif

630
void PNGAPI
631 632
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
   png_fixed_point red, png_fixed_point green)
A
Andreas Dilger 已提交
633
{
A
Andreas Dilger 已提交
634
   png_debug(1, "in png_set_rgb_to_gray\n");
635
   if (png_ptr == NULL) return;
636 637 638 639 640 641 642 643 644 645 646 647 648
   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
   {
649 650
      png_warning(png_ptr,
        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
651 652 653 654
      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
   }
#endif
   {
655
      png_uint_16 red_int, green_int;
656
      if (red < 0 || green < 0)
657
      {
658 659
         red_int   =  6968; /* .212671 * 32768 + .5 */
         green_int = 23434; /* .715160 * 32768 + .5 */
660
      }
661
      else if (red + green < 100000L)
662 663 664 665 666
      {
        red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
        green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
      }
      else
667 668
      {
         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
669 670
         red_int   =  6968;
         green_int = 23434;
671
      }
672 673
      png_ptr->rgb_to_gray_red_coeff   = red_int;
      png_ptr->rgb_to_gray_green_coeff = green_int;
674 675
      png_ptr->rgb_to_gray_blue_coeff  = 
         (png_uint_16)(32768 - red_int - green_int);
676
   }
A
Andreas Dilger 已提交
677 678 679
}
#endif

680 681 682
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) || \
    defined(PNG_LEGACY_SUPPORTED)
683
void PNGAPI
684 685 686 687
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");
688
   if (png_ptr == NULL) return;
689
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
690 691
   png_ptr->transformations |= PNG_USER_TRANSFORM;
   png_ptr->read_user_transform_fn = read_user_transform_fn;
692 693
#endif
#ifdef PNG_LEGACY_SUPPORTED
694
   if (read_user_transform_fn)
695 696 697
      png_warning(png_ptr,
        "This version of libpng does not support user transforms");
#endif
698 699 700
}
#endif

A
Andreas Dilger 已提交
701
/* Initialize everything needed for the read.  This includes modifying
702 703
 * the palette.
 */
704
void /* PRIVATE */
G
Guy Schalnat 已提交
705
png_init_read_transformations(png_structp png_ptr)
G
Guy Schalnat 已提交
706
{
707 708 709 710 711
   png_debug(1, "in png_init_read_transformations\n");
  {
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || defined(PNG_READ_SHIFT_SUPPORTED) \
 || defined(PNG_READ_GAMMA_SUPPORTED)
   int color_type = png_ptr->color_type;
712
#endif
G
Guy Schalnat 已提交
713

G
Guy Schalnat 已提交
714
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   /* Detect gray background and attempt to enable optimization
    * for gray --> RGB case */
   /* Note:  if PNG_BACKGROUND_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 for the current code, which uses
    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
    * png_do_gray_to_rgb() transformation.
    */
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
       !(color_type & PNG_COLOR_MASK_COLOR))
   {
          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
   } else if ((png_ptr->transformations & PNG_BACKGROUND) &&
              !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
              (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
              png_ptr->background.red == png_ptr->background.green &&
              png_ptr->background.red == png_ptr->background.blue)
   {
          png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
          png_ptr->background.gray = png_ptr->background.red;
   }
#endif

741 742
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
       (png_ptr->transformations & PNG_EXPAND))
G
Guy Schalnat 已提交
743
   {
744
      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
G
Guy Schalnat 已提交
745
      {
746
         /* expand background and tRNS chunks */
G
Guy Schalnat 已提交
747 748 749
         switch (png_ptr->bit_depth)
         {
            case 1:
G
Guy Schalnat 已提交
750
               png_ptr->background.gray *= (png_uint_16)0xff;
751 752
               png_ptr->background.red = png_ptr->background.green
                 =  png_ptr->background.blue = png_ptr->background.gray;
753 754 755 756 757 758
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
               {
                 png_ptr->trans_values.gray *= (png_uint_16)0xff;
                 png_ptr->trans_values.red = png_ptr->trans_values.green
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
               }
G
Guy Schalnat 已提交
759 760
               break;
            case 2:
G
Guy Schalnat 已提交
761
               png_ptr->background.gray *= (png_uint_16)0x55;
762 763
               png_ptr->background.red = png_ptr->background.green
                 = png_ptr->background.blue = png_ptr->background.gray;
764 765 766 767 768 769
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
               {
                 png_ptr->trans_values.gray *= (png_uint_16)0x55;
                 png_ptr->trans_values.red = png_ptr->trans_values.green
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
               }
G
Guy Schalnat 已提交
770 771
               break;
            case 4:
G
Guy Schalnat 已提交
772
               png_ptr->background.gray *= (png_uint_16)0x11;
773 774
               png_ptr->background.red = png_ptr->background.green
                 = png_ptr->background.blue = png_ptr->background.gray;
775 776 777 778 779 780
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
               {
                 png_ptr->trans_values.gray *= (png_uint_16)0x11;
                 png_ptr->trans_values.red = png_ptr->trans_values.green
                   = png_ptr->trans_values.blue = png_ptr->trans_values.gray;
               }
G
Guy Schalnat 已提交
781 782 783
               break;
            case 8:
            case 16:
784 785
               png_ptr->background.red = png_ptr->background.green
                 = png_ptr->background.blue = png_ptr->background.gray;
G
Guy Schalnat 已提交
786 787 788
               break;
         }
      }
G
Guy Schalnat 已提交
789
      else if (color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
790
      {
G
Guy Schalnat 已提交
791
         png_ptr->background.red   =
G
Guy Schalnat 已提交
792 793 794
            png_ptr->palette[png_ptr->background.index].red;
         png_ptr->background.green =
            png_ptr->palette[png_ptr->background.index].green;
G
Guy Schalnat 已提交
795
         png_ptr->background.blue  =
G
Guy Schalnat 已提交
796
            png_ptr->palette[png_ptr->background.index].blue;
797 798 799 800 801

#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
        {
#if defined(PNG_READ_EXPAND_SUPPORTED)
802
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
803 804
#endif
           {
805
           /* invert the alpha channel (in tRNS) unless the pixels are
806
              going to be expanded, in which case leave it for later */
807
              int i, istop;
808 809
              istop=(int)png_ptr->num_trans;
              for (i=0; i<istop; i++)
810
                 png_ptr->trans[i] = (png_byte)(255 - png_ptr->trans[i]);
811 812 813 814
           }
        }
#endif

G
Guy Schalnat 已提交
815 816
      }
   }
G
Guy Schalnat 已提交
817
#endif
G
Guy Schalnat 已提交
818

819
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
G
Guy Schalnat 已提交
820
   png_ptr->background_1 = png_ptr->background;
G
Guy Schalnat 已提交
821
#endif
822
#if defined(PNG_READ_GAMMA_SUPPORTED) && defined(PNG_FLOATING_POINT_SUPPORTED)
823 824 825 826 827

   if ((color_type == PNG_COLOR_TYPE_PALETTE && png_ptr->num_trans != 0)
       && (fabs(png_ptr->screen_gamma * png_ptr->gamma - 1.0)
         < PNG_GAMMA_THRESHOLD))
   {
828
    int i, k;
829 830 831 832 833 834 835
    k=0;
    for (i=0; i<png_ptr->num_trans; i++)
    {
      if (png_ptr->trans[i] != 0 && png_ptr->trans[i] != 0xff)
        k=1; /* partial transparency is present */
    }
    if (k == 0)
836
      png_ptr->transformations &= ~PNG_GAMMA;
837 838
   }

839 840
   if ((png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY)) &&
        png_ptr->gamma != 0.0)
G
Guy Schalnat 已提交
841 842
   {
      png_build_gamma_table(png_ptr);
G
Guy Schalnat 已提交
843
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
G
Guy Schalnat 已提交
844
      if (png_ptr->transformations & PNG_BACKGROUND)
G
Guy Schalnat 已提交
845
      {
G
Guy Schalnat 已提交
846 847
         if (color_type == PNG_COLOR_TYPE_PALETTE)
         {
848
           /* could skip if no transparency and
849
           */
G
Guy Schalnat 已提交
850
            png_color back, back_1;
851 852 853
            png_colorp palette = png_ptr->palette;
            int num_palette = png_ptr->num_palette;
            int i;
A
Andreas Dilger 已提交
854 855 856 857 858 859 860 861 862 863 864 865
            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
            {
866
               double g, gs;
A
Andreas Dilger 已提交
867

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
               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 已提交
887

888
               if ( fabs(gs - 1.0) < PNG_GAMMA_THRESHOLD)
A
Andreas Dilger 已提交
889
               {
890
                  back.red   = (png_byte)png_ptr->background.red;
A
Andreas Dilger 已提交
891
                  back.green = (png_byte)png_ptr->background.green;
892
                  back.blue  = (png_byte)png_ptr->background.blue;
A
Andreas Dilger 已提交
893 894 895
               }
               else
               {
896 897 898 899 900 901
                  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 已提交
902
               }
G
Guy Schalnat 已提交
903

904 905 906 907 908 909
               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 已提交
910
            }
G
Guy Schalnat 已提交
911 912
            for (i = 0; i < num_palette; i++)
            {
A
Andreas Dilger 已提交
913
               if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
G
Guy Schalnat 已提交
914 915 916 917 918
               {
                  if (png_ptr->trans[i] == 0)
                  {
                     palette[i] = back;
                  }
A
Andreas Dilger 已提交
919
                  else /* if (png_ptr->trans[i] != 0xff) */
G
Guy Schalnat 已提交
920
                  {
A
Andreas Dilger 已提交
921
                     png_byte v, w;
G
Guy Schalnat 已提交
922 923

                     v = png_ptr->gamma_to_1[palette[i].red];
A
Andreas Dilger 已提交
924
                     png_composite(w, v, png_ptr->trans[i], back_1.red);
G
Guy Schalnat 已提交
925 926 927
                     palette[i].red = png_ptr->gamma_from_1[w];

                     v = png_ptr->gamma_to_1[palette[i].green];
A
Andreas Dilger 已提交
928
                     png_composite(w, v, png_ptr->trans[i], back_1.green);
G
Guy Schalnat 已提交
929 930 931
                     palette[i].green = png_ptr->gamma_from_1[w];

                     v = png_ptr->gamma_to_1[palette[i].blue];
A
Andreas Dilger 已提交
932
                     png_composite(w, v, png_ptr->trans[i], back_1.blue);
G
Guy Schalnat 已提交
933 934 935 936 937 938 939 940 941 942
                     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];
               }
            }
943 944 945 946 947 948 949 950
	    /* Prevent the transformations being done again, and make sure
	     * that the now spurious alpha channel is stripped - the code
	     * has just reduced background composition and gamma correction
	     * to a simple alpha channel strip.
	     */
	    png_ptr->transformations &= ~PNG_BACKGROUND;
	    png_ptr->transformations &= ~PNG_GAMMA;
	    png_ptr->transformations |= PNG_STRIP_ALPHA;
G
Guy Schalnat 已提交
951
         }
952
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
953 954
         else
         /* color_type != PNG_COLOR_TYPE_PALETTE */
G
Guy Schalnat 已提交
955
         {
956 957 958
            double m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
            double g = 1.0;
            double gs = 1.0;
G
Guy Schalnat 已提交
959 960

            switch (png_ptr->background_gamma_type)
G
Guy Schalnat 已提交
961
            {
G
Guy Schalnat 已提交
962
               case PNG_BACKGROUND_GAMMA_SCREEN:
963
                  g = (png_ptr->screen_gamma);
G
Guy Schalnat 已提交
964 965 966 967
                  gs = 1.0;
                  break;
               case PNG_BACKGROUND_GAMMA_FILE:
                  g = 1.0 / (png_ptr->gamma);
968
                  gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
G
Guy Schalnat 已提交
969 970 971 972
                  break;
               case PNG_BACKGROUND_GAMMA_UNIQUE:
                  g = 1.0 / (png_ptr->background_gamma);
                  gs = 1.0 / (png_ptr->background_gamma *
973
                     png_ptr->screen_gamma);
G
Guy Schalnat 已提交
974 975 976
                  break;
            }

977 978 979 980 981
            png_ptr->background_1.gray = (png_uint_16)(pow(
               (double)png_ptr->background.gray / m, g) * m + .5);
            png_ptr->background.gray = (png_uint_16)(pow(
               (double)png_ptr->background.gray / m, gs) * m + .5);

982 983 984
            if ((png_ptr->background.red != png_ptr->background.green) ||
                (png_ptr->background.red != png_ptr->background.blue) ||
                (png_ptr->background.red != png_ptr->background.gray))
G
Guy Schalnat 已提交
985
            {
986
               /* RGB or RGBA with color background */
G
Guy Schalnat 已提交
987
               png_ptr->background_1.red = (png_uint_16)(pow(
G
Guy Schalnat 已提交
988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
                  (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
            {
1002 1003 1004 1005 1006
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
               png_ptr->background_1.red = png_ptr->background_1.green
                 = png_ptr->background_1.blue = png_ptr->background_1.gray;
               png_ptr->background.red = png_ptr->background.green
                 = png_ptr->background.blue = png_ptr->background.gray;
G
Guy Schalnat 已提交
1007 1008 1009
            }
         }
      }
G
Guy Schalnat 已提交
1010
      else
1011
      /* transformation does not include PNG_BACKGROUND */
1012
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
G
Guy Schalnat 已提交
1013 1014
      if (color_type == PNG_COLOR_TYPE_PALETTE)
      {
1015 1016 1017
         png_colorp palette = png_ptr->palette;
         int num_palette = png_ptr->num_palette;
         int i;
G
Guy Schalnat 已提交
1018 1019 1020 1021 1022 1023 1024

         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];
         }
1025 1026 1027

	 /* Done the gamma correction. */
	 png_ptr->transformations &= ~PNG_GAMMA;
G
Guy Schalnat 已提交
1028 1029 1030 1031
      }
   }
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
   else
G
Guy Schalnat 已提交
1032
#endif
1033
#endif /* PNG_READ_GAMMA_SUPPORTED && PNG_FLOATING_POINT_SUPPORTED */
G
Guy Schalnat 已提交
1034
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1035
   /* No GAMMA transformation */
1036 1037
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
       (color_type == PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
1038
   {
1039 1040
      int i;
      int istop = (int)png_ptr->num_trans;
G
Guy Schalnat 已提交
1041
      png_color back;
1042
      png_colorp palette = png_ptr->palette;
G
Guy Schalnat 已提交
1043 1044 1045 1046 1047

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

1048
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1049 1050 1051 1052 1053 1054 1055
      {
         if (png_ptr->trans[i] == 0)
         {
            palette[i] = back;
         }
         else if (png_ptr->trans[i] != 0xff)
         {
1056
            /* The png_composite() macro is defined in png.h */
A
Andreas Dilger 已提交
1057 1058 1059 1060 1061 1062
            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 已提交
1063 1064
         }
      }
1065 1066 1067 1068

      /* Handled alpha, still need to strip the channel. */
      png_ptr->transformations &= ~PNG_BACKGROUND;
      png_ptr->transformations |= PNG_STRIP_ALPHA;
G
Guy Schalnat 已提交
1069
   }
1070
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
G
Guy Schalnat 已提交
1071

G
Guy Schalnat 已提交
1072
#if defined(PNG_READ_SHIFT_SUPPORTED)
G
Guy Schalnat 已提交
1073
   if ((png_ptr->transformations & PNG_SHIFT) &&
1074
      (color_type == PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
1075
   {
1076 1077 1078 1079 1080
      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 已提交
1081 1082 1083 1084 1085 1086 1087

      if (sr < 0 || sr > 8)
         sr = 0;
      if (sg < 0 || sg > 8)
         sg = 0;
      if (sb < 0 || sb > 8)
         sb = 0;
1088
      for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1089 1090 1091 1092 1093 1094
      {
         png_ptr->palette[i].red >>= sr;
         png_ptr->palette[i].green >>= sg;
         png_ptr->palette[i].blue >>= sb;
      }
   }
1095
#endif  /* PNG_READ_SHIFT_SUPPORTED */
1096
 }
1097 1098
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1099
   if (png_ptr)
1100 1101
      return;
#endif
G
Guy Schalnat 已提交
1102 1103
}

A
Andreas Dilger 已提交
1104
/* Modify the info structure to reflect the transformations.  The
1105 1106 1107
 * info should be updated so a PNG file could be written with it,
 * assuming the transformations result in valid PNG data.
 */
1108
void /* PRIVATE */
G
Guy Schalnat 已提交
1109
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
G
Guy Schalnat 已提交
1110
{
A
Andreas Dilger 已提交
1111
   png_debug(1, "in png_read_transform_info\n");
G
Guy Schalnat 已提交
1112
#if defined(PNG_READ_EXPAND_SUPPORTED)
A
Andreas Dilger 已提交
1113
   if (png_ptr->transformations & PNG_EXPAND)
G
Guy Schalnat 已提交
1114
   {
A
Andreas Dilger 已提交
1115 1116
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
      {
1117 1118
         if (png_ptr->num_trans &&
              (png_ptr->transformations & PNG_EXPAND_tRNS))
A
Andreas Dilger 已提交
1119 1120 1121
            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
         else
            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
G
Guy Schalnat 已提交
1122
         info_ptr->bit_depth = 8;
A
Andreas Dilger 已提交
1123 1124 1125 1126 1127
         info_ptr->num_trans = 0;
      }
      else
      {
         if (png_ptr->num_trans)
1128 1129 1130
         {
            if (png_ptr->transformations & PNG_EXPAND_tRNS)
              info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1131
#if 0 /* Removed from libpng-1.2.27 */
1132 1133
            else
              info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
1134
#endif
1135
         }
A
Andreas Dilger 已提交
1136 1137 1138 1139
         if (info_ptr->bit_depth < 8)
            info_ptr->bit_depth = 8;
         info_ptr->num_trans = 0;
      }
G
Guy Schalnat 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
   }
#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

1152 1153
#if defined(PNG_READ_GAMMA_SUPPORTED)
   if (png_ptr->transformations & PNG_GAMMA)
1154 1155
   {
#ifdef PNG_FLOATING_POINT_SUPPORTED
1156 1157
      info_ptr->gamma = png_ptr->gamma;
#endif
1158 1159 1160 1161 1162
#ifdef PNG_FIXED_POINT_SUPPORTED
      info_ptr->int_gamma = png_ptr->int_gamma;
#endif
   }
#endif
1163

G
Guy Schalnat 已提交
1164
#if defined(PNG_READ_16_TO_8_SUPPORTED)
1165
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
G
Guy Schalnat 已提交
1166
      info_ptr->bit_depth = 8;
G
Guy Schalnat 已提交
1167 1168
#endif

1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
#endif

#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 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187
#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 已提交
1188
   }
G
Guy Schalnat 已提交
1189 1190 1191
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
1192
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
G
Guy Schalnat 已提交
1193 1194 1195
      info_ptr->bit_depth = 8;
#endif

G
Guy Schalnat 已提交
1196
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1197 1198 1199 1200 1201
      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 已提交
1202

1203
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1204
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
A
Andreas Dilger 已提交
1205 1206 1207
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
#endif

1208 1209 1210
   if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
      info_ptr->channels++;

A
Andreas Dilger 已提交
1211
#if defined(PNG_READ_FILLER_SUPPORTED)
1212
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
1213 1214 1215
   if ((png_ptr->transformations & PNG_FILLER) &&
       ((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
       (info_ptr->color_type == PNG_COLOR_TYPE_GRAY)))
1216
   {
1217
      info_ptr->channels++;
1218 1219 1220
      /* if adding a true alpha channel not just filler */
      if (png_ptr->transformations & PNG_ADD_ALPHA)
        info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1221
   }
A
Andreas Dilger 已提交
1222 1223
#endif

1224 1225
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
1226
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1227
     {
1228
       if (info_ptr->bit_depth < png_ptr->user_transform_depth)
1229
         info_ptr->bit_depth = png_ptr->user_transform_depth;
1230
       if (info_ptr->channels < png_ptr->user_transform_channels)
1231 1232 1233 1234
         info_ptr->channels = png_ptr->user_transform_channels;
     }
#endif

G
Guy Schalnat 已提交
1235 1236
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
      info_ptr->bit_depth);
1237

1238
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
1239

1240
#if !defined(PNG_READ_EXPAND_SUPPORTED)
1241
   if (png_ptr)
1242 1243
      return;
#endif
G
Guy Schalnat 已提交
1244 1245
}

1246 1247 1248 1249
/* 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.
 */
1250
void /* PRIVATE */
G
Guy Schalnat 已提交
1251
png_do_read_transformations(png_structp png_ptr)
G
Guy Schalnat 已提交
1252
{
A
Andreas Dilger 已提交
1253 1254
   png_debug(1, "in png_do_read_transformations\n");
   if (png_ptr->row_buf == NULL)
G
Guy Schalnat 已提交
1255
   {
1256
#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
A
Andreas Dilger 已提交
1257 1258
      char msg[50];

1259 1260 1261
      png_snprintf2(msg, 50,
         "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
         png_ptr->pass);
A
Andreas Dilger 已提交
1262
      png_error(png_ptr, msg);
1263 1264 1265
#else
      png_error(png_ptr, "NULL row buffer");
#endif
G
Guy Schalnat 已提交
1266
   }
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
#ifdef PNG_WARN_UNINITIALIZED_ROW
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
      /* Application has failed to call either png_read_start_image()
       * or png_read_update_info() after setting transforms that expand
       * pixels.  This check added to libpng-1.2.19 */
#if (PNG_WARN_UNINITIALIZED_ROW==1)
      png_error(png_ptr, "Uninitialized row");
#else
      png_warning(png_ptr, "Uninitialized row");
#endif
A
Andreas Dilger 已提交
1277 1278 1279 1280
#endif

#if defined(PNG_READ_EXPAND_SUPPORTED)
   if (png_ptr->transformations & PNG_EXPAND)
G
Guy Schalnat 已提交
1281
   {
A
Andreas Dilger 已提交
1282 1283 1284 1285 1286
      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);
      }
1287
      else
A
Andreas Dilger 已提交
1288
      {
1289 1290
         if (png_ptr->num_trans &&
             (png_ptr->transformations & PNG_EXPAND_tRNS))
A
Andreas Dilger 已提交
1291 1292 1293 1294 1295 1296
            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 已提交
1297
   }
G
Guy Schalnat 已提交
1298 1299
#endif

A
Andreas Dilger 已提交
1300
#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1301
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
A
Andreas Dilger 已提交
1302
      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
1303
         PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
A
Andreas Dilger 已提交
1304 1305
#endif

1306 1307 1308 1309 1310
#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);
1311
      if (rgb_error)
1312 1313
      {
         png_ptr->rgb_to_gray_status=1;
1314 1315
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 
             PNG_RGB_TO_GRAY_WARN)
1316
            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
1317 1318
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
             PNG_RGB_TO_GRAY_ERR)
1319 1320 1321 1322 1323
            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
      }
   }
#endif

1324 1325 1326 1327 1328
/*
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
1329
  pixel is transparent.  You would also need to make sure that the
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
  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 */
1357
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1358
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1359 1360 1361
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
#endif

G
Guy Schalnat 已提交
1362
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
1363 1364 1365
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
      ((png_ptr->num_trans != 0 ) ||
      (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
G
Guy Schalnat 已提交
1366
      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
1367 1368 1369
         &(png_ptr->trans_values), &(png_ptr->background)
#if defined(PNG_READ_GAMMA_SUPPORTED)
         , &(png_ptr->background_1),
G
Guy Schalnat 已提交
1370 1371 1372
         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,
1373 1374 1375
         png_ptr->gamma_shift
#endif
);
G
Guy Schalnat 已提交
1376 1377 1378 1379
#endif

#if defined(PNG_READ_GAMMA_SUPPORTED)
   if ((png_ptr->transformations & PNG_GAMMA) &&
1380 1381 1382 1383 1384
#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 已提交
1385
      (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
G
Guy Schalnat 已提交
1386 1387 1388
      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 已提交
1389 1390 1391
#endif

#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
1392 1393
   if (png_ptr->transformations & PNG_16_TO_8)
      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1394 1395 1396
#endif

#if defined(PNG_READ_DITHER_SUPPORTED)
G
Guy Schalnat 已提交
1397
   if (png_ptr->transformations & PNG_DITHER)
G
Guy Schalnat 已提交
1398
   {
A
Andreas Dilger 已提交
1399 1400
      png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
         png_ptr->palette_lookup, png_ptr->dither_index);
1401
      if (png_ptr->row_info.rowbytes == (png_uint_32)0)
1402
         png_error(png_ptr, "png_do_dither returned rowbytes=0");
A
Andreas Dilger 已提交
1403
   }
G
Guy Schalnat 已提交
1404 1405 1406
#endif

#if defined(PNG_READ_INVERT_SUPPORTED)
G
Guy Schalnat 已提交
1407
   if (png_ptr->transformations & PNG_INVERT_MONO)
G
Guy Schalnat 已提交
1408
      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1409 1410 1411
#endif

#if defined(PNG_READ_SHIFT_SUPPORTED)
G
Guy Schalnat 已提交
1412 1413 1414
   if (png_ptr->transformations & PNG_SHIFT)
      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
         &(png_ptr->shift));
G
Guy Schalnat 已提交
1415 1416 1417
#endif

#if defined(PNG_READ_PACK_SUPPORTED)
G
Guy Schalnat 已提交
1418
   if (png_ptr->transformations & PNG_PACK)
G
Guy Schalnat 已提交
1419
      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1420 1421 1422
#endif

#if defined(PNG_READ_BGR_SUPPORTED)
G
Guy Schalnat 已提交
1423 1424
   if (png_ptr->transformations & PNG_BGR)
      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1425 1426
#endif

A
Andreas Dilger 已提交
1427 1428 1429 1430 1431
#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 已提交
1432
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
1433
   /* if gray -> RGB, do so now only if we did not do so above */
1434 1435
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
G
Guy Schalnat 已提交
1436
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
G
Guy Schalnat 已提交
1437 1438 1439 1440 1441
#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 已提交
1442 1443 1444
         (png_uint_32)png_ptr->filler, png_ptr->flags);
#endif

1445 1446 1447 1448 1449
#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

1450 1451 1452 1453 1454
#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 已提交
1455 1456 1457
#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 已提交
1458
#endif
1459 1460 1461

#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1462
    {
1463
      if (png_ptr->read_user_transform_fn != NULL)
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
        (*(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 */
1474
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
1475
      if (png_ptr->user_transform_depth)
1476
         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
1477
      if (png_ptr->user_transform_channels)
1478
         png_ptr->row_info.channels = png_ptr->user_transform_channels;
1479
#endif
1480 1481
      png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
         png_ptr->row_info.channels);
1482 1483
      png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
         png_ptr->row_info.width);
1484
   }
1485 1486
#endif

G
Guy Schalnat 已提交
1487 1488
}

G
Guy Schalnat 已提交
1489
#if defined(PNG_READ_PACK_SUPPORTED)
1490 1491 1492 1493 1494 1495
/* 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.
 */
1496
void /* PRIVATE */
G
Guy Schalnat 已提交
1497
png_do_unpack(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
1498
{
A
Andreas Dilger 已提交
1499 1500
   png_debug(1, "in png_do_unpack\n");
   if (row_info->bit_depth < 8)
G
Guy Schalnat 已提交
1501
   {
1502
      png_uint_32 i;
1503
      png_uint_32 row_width=row_info->width;
A
Andreas Dilger 已提交
1504

G
Guy Schalnat 已提交
1505 1506 1507 1508
      switch (row_info->bit_depth)
      {
         case 1:
         {
1509 1510
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 3);
            png_bytep dp = row + (png_size_t)row_width - 1;
1511
            png_uint_32 shift = 7 - (int)((row_width + 7) & 0x07);
1512
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1513
            {
1514
               *dp = (png_byte)((*sp >> shift) & 0x01);
G
Guy Schalnat 已提交
1515 1516 1517 1518
               if (shift == 7)
               {
                  shift = 0;
                  sp--;
G
Guy Schalnat 已提交
1519
               }
G
Guy Schalnat 已提交
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
               else
                  shift++;

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

1530 1531
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 2);
            png_bytep dp = row + (png_size_t)row_width - 1;
1532
            png_uint_32 shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
1533
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1534
            {
1535
               *dp = (png_byte)((*sp >> shift) & 0x03);
G
Guy Schalnat 已提交
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
               if (shift == 6)
               {
                  shift = 0;
                  sp--;
               }
               else
                  shift += 2;

               dp--;
            }
            break;
         }
         case 4:
         {
1550 1551
            png_bytep sp = row + (png_size_t)((row_width - 1) >> 1);
            png_bytep dp = row + (png_size_t)row_width - 1;
1552
            png_uint_32 shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
1553
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
1554
            {
1555
               *dp = (png_byte)((*sp >> shift) & 0x0f);
G
Guy Schalnat 已提交
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
               if (shift == 4)
               {
                  shift = 0;
                  sp--;
               }
               else
                  shift = 4;

               dp--;
            }
            break;
         }
      }
      row_info->bit_depth = 8;
G
Guy Schalnat 已提交
1570
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1571
      row_info->rowbytes = row_width * row_info->channels;
G
Guy Schalnat 已提交
1572 1573
   }
}
G
Guy Schalnat 已提交
1574
#endif
G
Guy Schalnat 已提交
1575

G
Guy Schalnat 已提交
1576
#if defined(PNG_READ_SHIFT_SUPPORTED)
1577 1578 1579 1580 1581
/* 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.
 */
1582
void /* PRIVATE */
A
Andreas Dilger 已提交
1583
png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
G
Guy Schalnat 已提交
1584
{
A
Andreas Dilger 已提交
1585 1586 1587
   png_debug(1, "in png_do_unshift\n");
   if (
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
1588 1589
   {
      int shift[4];
1590 1591 1592
      int channels = 0;
      int c;
      png_uint_16 value = 0;
1593
      png_uint_32 row_width = row_info->width;
G
Guy Schalnat 已提交
1594 1595 1596

      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
      {
G
Guy Schalnat 已提交
1597 1598 1599
         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 已提交
1600 1601 1602
      }
      else
      {
G
Guy Schalnat 已提交
1603
         shift[channels++] = row_info->bit_depth - sig_bits->gray;
G
Guy Schalnat 已提交
1604 1605 1606
      }
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
      {
G
Guy Schalnat 已提交
1607
         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
G
Guy Schalnat 已提交
1608
      }
G
Guy Schalnat 已提交
1609

A
Andreas Dilger 已提交
1610
      for (c = 0; c < channels; c++)
G
Guy Schalnat 已提交
1611
      {
A
Andreas Dilger 已提交
1612 1613
         if (shift[c] <= 0)
            shift[c] = 0;
G
Guy Schalnat 已提交
1614 1615 1616
         else
            value = 1;
      }
G
Guy Schalnat 已提交
1617

G
Guy Schalnat 已提交
1618 1619
      if (!value)
         return;
G
Guy Schalnat 已提交
1620

G
Guy Schalnat 已提交
1621
      switch (row_info->bit_depth)
G
Guy Schalnat 已提交
1622 1623 1624
      {
         case 2:
         {
A
Andreas Dilger 已提交
1625
            png_bytep bp;
1626 1627
            png_uint_32 i;
            png_uint_32 istop = row_info->rowbytes;
A
Andreas Dilger 已提交
1628

1629
            for (bp = row, i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1630 1631
            {
               *bp >>= 1;
1632
               *bp++ &= 0x55;
G
Guy Schalnat 已提交
1633 1634 1635 1636 1637
            }
            break;
         }
         case 4:
         {
1638
            png_bytep bp = row;
1639 1640
            png_uint_32 i;
            png_uint_32 istop = row_info->rowbytes;
1641 1642
            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
               (png_byte)((int)0xf >> shift[0]));
1643

1644
            for (i = 0; i < istop; i++)
G
Guy Schalnat 已提交
1645 1646
            {
               *bp >>= shift[0];
1647
               *bp++ &= mask;
G
Guy Schalnat 已提交
1648 1649 1650 1651 1652
            }
            break;
         }
         case 8:
         {
1653
            png_bytep bp = row;
1654
            png_uint_32 i;
1655
            png_uint_32 istop = row_width * channels;
G
Guy Schalnat 已提交
1656

1657
            for (i = 0; i < istop; i++)
A
Andreas Dilger 已提交
1658
            {
1659
               *bp++ >>= shift[i%channels];
G
Guy Schalnat 已提交
1660 1661 1662 1663 1664
            }
            break;
         }
         case 16:
         {
1665 1666 1667
            png_bytep bp = row;
            png_uint_32 i;
            png_uint_32 istop = channels * row_width;
G
Guy Schalnat 已提交
1668

1669
            for (i = 0; i < istop; i++)
A
Andreas Dilger 已提交
1670
            {
1671 1672 1673 1674
               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 已提交
1675 1676 1677 1678 1679 1680
            }
            break;
         }
      }
   }
}
G
Guy Schalnat 已提交
1681
#endif
G
Guy Schalnat 已提交
1682

G
Guy Schalnat 已提交
1683
#if defined(PNG_READ_16_TO_8_SUPPORTED)
G
Guy Schalnat 已提交
1684
/* chop rows of bit depth 16 down to 8 */
1685
void /* PRIVATE */
G
Guy Schalnat 已提交
1686
png_do_chop(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
1687
{
A
Andreas Dilger 已提交
1688 1689
   png_debug(1, "in png_do_chop\n");
   if (row_info->bit_depth == 16)
G
Guy Schalnat 已提交
1690
   {
1691 1692 1693 1694
      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 已提交
1695

1696
      for (i = 0; i<istop; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
1697
      {
A
Andreas Dilger 已提交
1698
#if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
      /* 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:
       */
1720

A
Andreas Dilger 已提交
1721 1722
         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
#else
1723
       /* Simply discard the low order byte */
G
Guy Schalnat 已提交
1724
         *dp = *sp;
A
Andreas Dilger 已提交
1725
#endif
G
Guy Schalnat 已提交
1726 1727
      }
      row_info->bit_depth = 8;
G
Guy Schalnat 已提交
1728
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1729
      row_info->rowbytes = row_info->width * row_info->channels;
A
Andreas Dilger 已提交
1730 1731 1732 1733 1734
   }
}
#endif

#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1735
void /* PRIVATE */
A
Andreas Dilger 已提交
1736 1737 1738 1739
png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_swap_alpha\n");
   {
1740
      png_uint_32 row_width = row_info->width;
A
Andreas Dilger 已提交
1741 1742 1743 1744 1745
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      {
         /* This converts from RGBA to ARGB */
         if (row_info->bit_depth == 8)
         {
1746 1747
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1748 1749
            png_byte save;
            png_uint_32 i;
1750

1751
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
            {
               save = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save;
            }
         }
         /* This converts from RRGGBBAA to AARRGGBB */
         else
         {
1763 1764
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1765 1766
            png_byte save[2];
            png_uint_32 i;
1767

1768
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
            {
               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)
         {
1788 1789
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1790 1791
            png_byte save;
            png_uint_32 i;
1792

1793
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1794 1795 1796 1797 1798 1799 1800 1801 1802
            {
               save = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save;
            }
         }
         /* This converts from GGAA to AAGG */
         else
         {
1803 1804
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
A
Andreas Dilger 已提交
1805 1806 1807
            png_byte save[2];
            png_uint_32 i;

1808
            for (i = 0; i < row_width; i++)
A
Andreas Dilger 已提交
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818
            {
               save[0] = *(--sp);
               save[1] = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = save[0];
               *(--dp) = save[1];
            }
         }
      }
G
Guy Schalnat 已提交
1819 1820
   }
}
G
Guy Schalnat 已提交
1821
#endif
G
Guy Schalnat 已提交
1822

1823
#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1824
void /* PRIVATE */
1825 1826 1827 1828
png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
{
   png_debug(1, "in png_do_read_invert_alpha\n");
   {
1829
      png_uint_32 row_width = row_info->width;
1830 1831 1832 1833 1834
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
      {
         /* This inverts the alpha channel in RGBA */
         if (row_info->bit_depth == 8)
         {
1835 1836
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1837 1838
            png_uint_32 i;

1839
            for (i = 0; i < row_width; i++)
1840
            {
1841
               *(--dp) = (png_byte)(255 - *(--sp));
1842 1843

/*             This does nothing:
1844 1845 1846
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1847 1848 1849 1850
               We can replace it with:
*/
               sp-=3;
               dp=sp;
1851 1852 1853 1854 1855
            }
         }
         /* This inverts the alpha channel in RRGGBBAA */
         else
         {
1856 1857
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1858 1859
            png_uint_32 i;

1860
            for (i = 0; i < row_width; i++)
1861
            {
1862 1863
               *(--dp) = (png_byte)(255 - *(--sp));
               *(--dp) = (png_byte)(255 - *(--sp));
1864 1865

/*             This does nothing:
1866 1867 1868 1869 1870 1871
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1872 1873 1874 1875
               We can replace it with:
*/
               sp-=6;
               dp=sp;
1876 1877 1878 1879 1880
            }
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      {
1881
         /* This inverts the alpha channel in GA */
1882 1883
         if (row_info->bit_depth == 8)
         {
1884 1885
            png_bytep sp = row + row_info->rowbytes;
            png_bytep dp = sp;
1886 1887
            png_uint_32 i;

1888
            for (i = 0; i < row_width; i++)
1889
            {
1890
               *(--dp) = (png_byte)(255 - *(--sp));
1891
               *(--dp) = *(--sp);
1892 1893
            }
         }
1894
         /* This inverts the alpha channel in GGAA */
1895 1896
         else
         {
1897 1898
            png_bytep sp  = row + row_info->rowbytes;
            png_bytep dp = sp;
1899 1900
            png_uint_32 i;

1901
            for (i = 0; i < row_width; i++)
1902
            {
1903 1904
               *(--dp) = (png_byte)(255 - *(--sp));
               *(--dp) = (png_byte)(255 - *(--sp));
1905
/*
1906 1907
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
1908 1909 1910
*/
               sp-=2;
               dp=sp;
1911 1912 1913 1914 1915 1916 1917
            }
         }
      }
   }
}
#endif

G
Guy Schalnat 已提交
1918
#if defined(PNG_READ_FILLER_SUPPORTED)
A
Andreas Dilger 已提交
1919
/* Add filler channel if we have RGB color */
1920
void /* PRIVATE */
G
Guy Schalnat 已提交
1921
png_do_read_filler(png_row_infop row_info, png_bytep row,
A
Andreas Dilger 已提交
1922
   png_uint_32 filler, png_uint_32 flags)
G
Guy Schalnat 已提交
1923
{
G
Guy Schalnat 已提交
1924
   png_uint_32 i;
1925 1926
   png_uint_32 row_width = row_info->width;

1927
   png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
1928
   png_byte lo_filler = (png_byte)(filler & 0xff);
A
Andreas Dilger 已提交
1929 1930 1931

   png_debug(1, "in png_do_read_filler\n");
   if (
1932
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
G
Guy Schalnat 已提交
1933
   {
1934
      if (row_info->bit_depth == 8)
G
Guy Schalnat 已提交
1935
      {
1936 1937
         /* This changes the data from G to GX */
         if (flags & PNG_FLAG_FILLER_AFTER)
G
Guy Schalnat 已提交
1938
         {
1939 1940
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp =  sp + (png_size_t)row_width;
1941 1942
            for (i = 1; i < row_width; i++)
            {
1943
               *(--dp) = lo_filler;
1944 1945
               *(--dp) = *(--sp);
            }
1946
            *(--dp) = lo_filler;
1947 1948 1949 1950 1951 1952 1953
            row_info->channels = 2;
            row_info->pixel_depth = 16;
            row_info->rowbytes = row_width * 2;
         }
      /* This changes the data from G to XG */
         else
         {
1954 1955
            png_bytep sp = row + (png_size_t)row_width;
            png_bytep dp = sp  + (png_size_t)row_width;
1956 1957 1958
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
1959
               *(--dp) = lo_filler;
1960 1961 1962 1963 1964 1965
            }
            row_info->channels = 2;
            row_info->pixel_depth = 16;
            row_info->rowbytes = row_width * 2;
         }
      }
1966
      else if (row_info->bit_depth == 16)
1967 1968 1969 1970
      {
         /* This changes the data from GG to GGXX */
         if (flags & PNG_FLAG_FILLER_AFTER)
         {
1971 1972
            png_bytep sp = row + (png_size_t)row_width * 2;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
1973 1974 1975
            for (i = 1; i < row_width; i++)
            {
               *(--dp) = hi_filler;
1976
               *(--dp) = lo_filler;
1977 1978 1979
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
1980 1981
            *(--dp) = hi_filler;
            *(--dp) = lo_filler;
1982 1983
            row_info->channels = 2;
            row_info->pixel_depth = 32;
1984
            row_info->rowbytes = row_width * 4;
1985 1986 1987 1988
         }
         /* This changes the data from GG to XXGG */
         else
         {
1989 1990
            png_bytep sp = row + (png_size_t)row_width * 2;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
1991 1992 1993 1994 1995
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = hi_filler;
1996
               *(--dp) = lo_filler;
1997 1998
            }
            row_info->channels = 2;
1999 2000
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
G
Guy Schalnat 已提交
2001
         }
G
Guy Schalnat 已提交
2002
      }
2003 2004 2005
   } /* COLOR_TYPE == GRAY */
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
   {
2006
      if (row_info->bit_depth == 8)
2007 2008 2009 2010
      {
         /* This changes the data from RGB to RGBX */
         if (flags & PNG_FLAG_FILLER_AFTER)
         {
2011 2012
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp  + (png_size_t)row_width;
2013 2014
            for (i = 1; i < row_width; i++)
            {
2015
               *(--dp) = lo_filler;
2016 2017 2018 2019
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
2020
            *(--dp) = lo_filler;
2021 2022 2023 2024
            row_info->channels = 4;
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
         }
A
Andreas Dilger 已提交
2025
      /* This changes the data from RGB to XRGB */
2026 2027
         else
         {
2028 2029
            png_bytep sp = row + (png_size_t)row_width * 3;
            png_bytep dp = sp + (png_size_t)row_width;
2030 2031 2032 2033 2034
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
2035
               *(--dp) = lo_filler;
2036 2037 2038 2039 2040 2041
            }
            row_info->channels = 4;
            row_info->pixel_depth = 32;
            row_info->rowbytes = row_width * 4;
         }
      }
2042
      else if (row_info->bit_depth == 16)
G
Guy Schalnat 已提交
2043
      {
2044 2045
         /* This changes the data from RRGGBB to RRGGBBXX */
         if (flags & PNG_FLAG_FILLER_AFTER)
G
Guy Schalnat 已提交
2046
         {
2047 2048
            png_bytep sp = row + (png_size_t)row_width * 6;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2049 2050 2051
            for (i = 1; i < row_width; i++)
            {
               *(--dp) = hi_filler;
2052
               *(--dp) = lo_filler;
2053 2054 2055 2056 2057 2058 2059
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
            }
2060 2061
            *(--dp) = hi_filler;
            *(--dp) = lo_filler;
2062 2063
            row_info->channels = 4;
            row_info->pixel_depth = 64;
2064
            row_info->rowbytes = row_width * 8;
2065 2066 2067 2068
         }
         /* This changes the data from RRGGBB to XXRRGGBB */
         else
         {
2069 2070
            png_bytep sp = row + (png_size_t)row_width * 6;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2071 2072 2073 2074 2075 2076 2077 2078 2079
            for (i = 0; i < row_width; i++)
            {
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = *(--sp);
               *(--dp) = hi_filler;
2080
               *(--dp) = lo_filler;
2081 2082 2083
            }
            row_info->channels = 4;
            row_info->pixel_depth = 64;
2084
            row_info->rowbytes = row_width * 8;
G
Guy Schalnat 已提交
2085
         }
G
Guy Schalnat 已提交
2086
      }
2087
   } /* COLOR_TYPE == RGB */
G
Guy Schalnat 已提交
2088
}
G
Guy Schalnat 已提交
2089
#endif
G
Guy Schalnat 已提交
2090

G
Guy Schalnat 已提交
2091
#if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
A
Andreas Dilger 已提交
2092
/* expand grayscale files to RGB, with or without alpha */
2093
void /* PRIVATE */
G
Guy Schalnat 已提交
2094
png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
G
Guy Schalnat 已提交
2095
{
G
Guy Schalnat 已提交
2096
   png_uint_32 i;
2097
   png_uint_32 row_width = row_info->width;
G
Guy Schalnat 已提交
2098

A
Andreas Dilger 已提交
2099 2100
   png_debug(1, "in png_do_gray_to_rgb\n");
   if (row_info->bit_depth >= 8 &&
G
Guy Schalnat 已提交
2101 2102 2103 2104 2105 2106
      !(row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
      {
         if (row_info->bit_depth == 8)
         {
2107 2108
            png_bytep sp = row + (png_size_t)row_width - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2109
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2110 2111 2112
            {
               *(dp--) = *sp;
               *(dp--) = *sp;
2113
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2114 2115 2116 2117
            }
         }
         else
         {
2118 2119
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2120
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2121 2122 2123 2124 2125
            {
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
2126 2127
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2128 2129 2130 2131 2132 2133 2134
            }
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
      {
         if (row_info->bit_depth == 8)
         {
2135 2136
            png_bytep sp = row + (png_size_t)row_width * 2 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 2;
2137
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2138 2139 2140 2141
            {
               *(dp--) = *(sp--);
               *(dp--) = *sp;
               *(dp--) = *sp;
2142
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2143 2144 2145 2146
            }
         }
         else
         {
2147 2148
            png_bytep sp = row + (png_size_t)row_width * 4 - 1;
            png_bytep dp = sp  + (png_size_t)row_width * 4;
2149
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2150 2151 2152 2153 2154 2155 2156
            {
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
               *(dp--) = *sp;
               *(dp--) = *(sp - 1);
2157 2158
               *(dp--) = *(sp--);
               *(dp--) = *(sp--);
G
Guy Schalnat 已提交
2159 2160 2161
            }
         }
      }
G
Guy Schalnat 已提交
2162
      row_info->channels += (png_byte)2;
G
Guy Schalnat 已提交
2163
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
G
Guy Schalnat 已提交
2164 2165
      row_info->pixel_depth = (png_byte)(row_info->channels *
         row_info->bit_depth);
2166
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
G
Guy Schalnat 已提交
2167 2168
   }
}
G
Guy Schalnat 已提交
2169
#endif
G
Guy Schalnat 已提交
2170

2171
#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
2172
/* reduce RGB files to grayscale, with or without alpha
2173
 * using the equation given in Poynton's ColorFAQ at
2174
 * <http://www.inforamp.net/~poynton/>  (THIS LINK IS DEAD June 2008)
2175
 * New link:
2176
 * <http://www.poynton.com/notes/colour_and_gamma/>
2177
 * Charles Poynton poynton at poynton.com
2178 2179 2180 2181
 *
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
 *
 *  We approximate this with
2182
 *
2183
 *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
2184 2185 2186
 *
 *  which can be expressed with integers as
 *
2187
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
2188 2189 2190 2191 2192
 *
 *  The calculation is to be done in a linear colorspace.
 *
 *  Other integer coefficents can be used via png_set_rgb_to_gray().
 */
2193
int /* PRIVATE */
2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205
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 (
      (row_info->color_type & PNG_COLOR_MASK_COLOR))
   {
2206 2207 2208
      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;
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224

      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++)];
2225
                  if (red != green || red != blue)
2226 2227 2228
                  {
                     rgb_error |= 1;
                     *(dp++) = png_ptr->gamma_from_1[
2229
                       (rc*red + gc*green + bc*blue)>>15];
2230 2231
                  }
                  else
2232
                     *(dp++) = *(sp - 1);
2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244
               }
            }
            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++);
2245
                  if (red != green || red != blue)
2246 2247
                  {
                     rgb_error |= 1;
2248
                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2249 2250
                  }
                  else
2251
                     *(dp++) = *(sp - 1);
2252 2253 2254
               }
            }
         }
2255

2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267
         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;

2268 2269 2270
                  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;
2271

2272
                  if (red == green && red == blue)
2273 2274 2275 2276 2277 2278 2279
                     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];
2280
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
2281
                                  png_ptr->gamma_shift][blue>>8];
2282
                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
2283
                                  + bc*blue_1)>>15);
2284 2285 2286 2287
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
                         png_ptr->gamma_shift][gray16 >> 8];
                     rgb_error |= 1;
                  }
2288

2289 2290
                  *(dp++) = (png_byte)((w>>8) & 0xff);
                  *(dp++) = (png_byte)(w & 0xff);
2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301
               }
            }
            else
#endif
            {
               png_bytep sp = row;
               png_bytep dp = row;
               for (i = 0; i < row_width; i++)
               {
                  png_uint_16 red, green, blue, gray16;

2302 2303 2304
                  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;
2305

2306
                  if (red != green || red != blue)
2307
                     rgb_error |= 1;
2308
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2309 2310
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
                  *(dp++) = (png_byte)(gray16 & 0xff);
2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328
               }
            }
         }
      }
      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++)];
2329
                  if (red != green || red != blue)
2330 2331
                     rgb_error |= 1;
                  *(dp++) =  png_ptr->gamma_from_1
2332
                             [(rc*red + gc*green + bc*blue)>>15];
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345
                  *(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++);
2346
                  if (red != green || red != blue)
2347
                     rgb_error |= 1;
2348
                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
                  *(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;

2365 2366 2367
                  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;
2368

2369
                  if (red == green && red == blue)
2370 2371 2372 2373 2374 2375 2376
                     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];
2377
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
2378
                                  png_ptr->gamma_shift][blue>>8];
2379
                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
2380
                                  + gc * green_1 + bc * blue_1)>>15);
2381 2382 2383 2384
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
                         png_ptr->gamma_shift][gray16 >> 8];
                     rgb_error |= 1;
                  }
2385

2386 2387
                  *(dp++) = (png_byte)((w>>8) & 0xff);
                  *(dp++) = (png_byte)(w & 0xff);
2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399
                  *(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;
2400 2401 2402
                  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;
2403
                  if (red != green || red != blue)
2404
                     rgb_error |= 1;
2405
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
2406 2407
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
                  *(dp++) = (png_byte)(gray16 & 0xff);
2408 2409 2410 2411 2412 2413 2414 2415 2416 2417
                  *(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);
2418
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2419 2420 2421 2422 2423
   }
   return rgb_error;
}
#endif

2424 2425 2426 2427 2428
/* 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.
 */
2429
void PNGAPI
G
Guy Schalnat 已提交
2430
png_build_grayscale_palette(int bit_depth, png_colorp palette)
G
Guy Schalnat 已提交
2431 2432 2433 2434 2435 2436
{
   int num_palette;
   int color_inc;
   int i;
   int v;

A
Andreas Dilger 已提交
2437 2438
   png_debug(1, "in png_do_build_grayscale_palette\n");
   if (palette == NULL)
G
Guy Schalnat 已提交
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
      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 已提交
2460
         num_palette = 0;
G
Guy Schalnat 已提交
2461
         color_inc = 0;
G
Guy Schalnat 已提交
2462 2463 2464 2465 2466
         break;
   }

   for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
   {
G
Guy Schalnat 已提交
2467 2468 2469
      palette[i].red = (png_byte)v;
      palette[i].green = (png_byte)v;
      palette[i].blue = (png_byte)v;
G
Guy Schalnat 已提交
2470 2471 2472 2473
   }
}


G
Guy Schalnat 已提交
2474
#if defined(PNG_READ_BACKGROUND_SUPPORTED)
A
Andreas Dilger 已提交
2475
/* Replace any alpha or transparency with the supplied background color.
2476 2477 2478
 * "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.
 */
2479
void /* PRIVATE */
G
Guy Schalnat 已提交
2480
png_do_background(png_row_infop row_info, png_bytep row,
2481 2482 2483
   png_color_16p trans_values, png_color_16p background
#if defined(PNG_READ_GAMMA_SUPPORTED)
   , png_color_16p background_1,
G
Guy Schalnat 已提交
2484
   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
G
Guy Schalnat 已提交
2485
   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
2486 2487 2488
   png_uint_16pp gamma_16_to_1, int gamma_shift
#endif
   )
G
Guy Schalnat 已提交
2489
{
G
Guy Schalnat 已提交
2490
   png_bytep sp, dp;
G
Guy Schalnat 已提交
2491
   png_uint_32 i;
2492
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
2493
   int shift;
G
Guy Schalnat 已提交
2494

A
Andreas Dilger 已提交
2495 2496
   png_debug(1, "in png_do_background\n");
   if (background != NULL &&
G
Guy Schalnat 已提交
2497
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
A
Andreas Dilger 已提交
2498
      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
G
Guy Schalnat 已提交
2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
   {
      switch (row_info->color_type)
      {
         case PNG_COLOR_TYPE_GRAY:
         {
            switch (row_info->bit_depth)
            {
               case 1:
               {
                  sp = row;
                  shift = 7;
2510
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2511
                  {
2512
                     if ((png_uint_16)((*sp >> shift) & 0x01)
2513
                        == trans_values->gray)
G
Guy Schalnat 已提交
2514
                     {
G
Guy Schalnat 已提交
2515 2516
                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
                        *sp |= (png_byte)(background->gray << shift);
G
Guy Schalnat 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529
                     }
                     if (!shift)
                     {
                        shift = 7;
                        sp++;
                     }
                     else
                        shift--;
                  }
                  break;
               }
               case 2:
               {
2530 2531
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2532
                  {
2533 2534 2535
                     sp = row;
                     shift = 6;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2536
                     {
2537
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2538 2539 2540 2541 2542 2543 2544
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        else
                        {
2545
                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
2546
                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
2547
                               (p << 4) | (p << 6)] >> 6) & 0x03);
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
                           *sp |= (png_byte)(g << shift);
                        }
                        if (!shift)
                        {
                           shift = 6;
                           sp++;
                        }
                        else
                           shift -= 2;
G
Guy Schalnat 已提交
2558
                     }
2559 2560 2561 2562 2563 2564 2565
                  }
                  else
#endif
                  {
                     sp = row;
                     shift = 6;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2566
                     {
2567
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
                            == 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 已提交
2580 2581 2582 2583 2584 2585
                     }
                  }
                  break;
               }
               case 4:
               {
2586 2587
#if defined(PNG_READ_GAMMA_SUPPORTED)
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2588
                  {
2589 2590 2591
                     sp = row;
                     shift = 4;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2592
                     {
2593
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2594 2595 2596 2597 2598 2599 2600
                            == trans_values->gray)
                        {
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                           *sp |= (png_byte)(background->gray << shift);
                        }
                        else
                        {
2601
                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
2602
                           png_byte g = (png_byte)((gamma_table[p |
2603
                             (p << 4)] >> 4) & 0x0f);
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
                           *sp |= (png_byte)(g << shift);
                        }
                        if (!shift)
                        {
                           shift = 4;
                           sp++;
                        }
                        else
                           shift -= 4;
G
Guy Schalnat 已提交
2614
                     }
2615 2616 2617 2618 2619 2620 2621
                  }
                  else
#endif
                  {
                     sp = row;
                     shift = 4;
                     for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
2622
                     {
2623
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635
                            == 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 已提交
2636 2637 2638 2639 2640 2641
                     }
                  }
                  break;
               }
               case 8:
               {
G
Guy Schalnat 已提交
2642
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2643
                  if (gamma_table != NULL)
G
Guy Schalnat 已提交
2644
                  {
2645 2646
                     sp = row;
                     for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
2647 2648 2649
                     {
                        if (*sp == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2650
                           *sp = (png_byte)background->gray;
G
Guy Schalnat 已提交
2651 2652 2653 2654 2655 2656 2657 2658
                        }
                        else
                        {
                           *sp = gamma_table[*sp];
                        }
                     }
                  }
                  else
G
Guy Schalnat 已提交
2659
#endif
G
Guy Schalnat 已提交
2660
                  {
2661 2662
                     sp = row;
                     for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
2663 2664 2665
                     {
                        if (*sp == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2666
                           *sp = (png_byte)background->gray;
G
Guy Schalnat 已提交
2667 2668 2669 2670 2671 2672 2673
                        }
                     }
                  }
                  break;
               }
               case 16:
               {
G
Guy Schalnat 已提交
2674
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2675
                  if (gamma_16 != NULL)
G
Guy Schalnat 已提交
2676
                  {
2677 2678
                     sp = row;
                     for (i = 0; i < row_width; i++, sp += 2)
G
Guy Schalnat 已提交
2679 2680 2681
                     {
                        png_uint_16 v;

2682
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
G
Guy Schalnat 已提交
2683 2684
                        if (v == trans_values->gray)
                        {
A
Andreas Dilger 已提交
2685
                           /* background is already in screen gamma */
G
Guy Schalnat 已提交
2686 2687
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
G
Guy Schalnat 已提交
2688 2689 2690
                        }
                        else
                        {
A
Andreas Dilger 已提交
2691
                           v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
2692
                           *sp = (png_byte)((v >> 8) & 0xff);
G
Guy Schalnat 已提交
2693
                           *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
2694
                        }
G
Guy Schalnat 已提交
2695
                     }
G
Guy Schalnat 已提交
2696 2697
                  }
                  else
G
Guy Schalnat 已提交
2698
#endif
G
Guy Schalnat 已提交
2699
                  {
2700 2701
                     sp = row;
                     for (i = 0; i < row_width; i++, sp += 2)
G
Guy Schalnat 已提交
2702 2703 2704
                     {
                        png_uint_16 v;

2705
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
G
Guy Schalnat 已提交
2706 2707
                        if (v == trans_values->gray)
                        {
G
Guy Schalnat 已提交
2708 2709
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
G
Guy Schalnat 已提交
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721
                        }
                     }
                  }
                  break;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
G
Guy Schalnat 已提交
2722
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2723
               if (gamma_table != NULL)
G
Guy Schalnat 已提交
2724
               {
2725 2726
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 3)
G
Guy Schalnat 已提交
2727 2728 2729 2730 2731
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2732 2733 2734
                        *sp = (png_byte)background->red;
                        *(sp + 1) = (png_byte)background->green;
                        *(sp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
                     }
                     else
                     {
                        *sp = gamma_table[*sp];
                        *(sp + 1) = gamma_table[*(sp + 1)];
                        *(sp + 2) = gamma_table[*(sp + 2)];
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
2745
#endif
G
Guy Schalnat 已提交
2746
               {
2747 2748
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 3)
G
Guy Schalnat 已提交
2749 2750 2751 2752 2753
                  {
                     if (*sp == trans_values->red &&
                        *(sp + 1) == trans_values->green &&
                        *(sp + 2) == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2754 2755 2756
                        *sp = (png_byte)background->red;
                        *(sp + 1) = (png_byte)background->green;
                        *(sp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
2757 2758 2759 2760
                     }
                  }
               }
            }
A
Andreas Dilger 已提交
2761
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
2762
            {
G
Guy Schalnat 已提交
2763
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2764
               if (gamma_16 != NULL)
G
Guy Schalnat 已提交
2765
               {
2766 2767
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 6)
G
Guy Schalnat 已提交
2768
                  {
2769 2770 2771
                     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 已提交
2772
                     if (r == trans_values->red && g == trans_values->green &&
G
Guy Schalnat 已提交
2773 2774
                        b == trans_values->blue)
                     {
A
Andreas Dilger 已提交
2775
                        /* background is already in screen gamma */
G
Guy Schalnat 已提交
2776 2777 2778 2779 2780 2781
                        *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 已提交
2782 2783 2784
                     }
                     else
                     {
2785
                        png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
2786
                        *sp = (png_byte)((v >> 8) & 0xff);
G
Guy Schalnat 已提交
2787
                        *(sp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
2788
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
G
Guy Schalnat 已提交
2789 2790
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 3) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
2791
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
G
Guy Schalnat 已提交
2792 2793
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(sp + 5) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
2794 2795 2796 2797
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
2798
#endif
G
Guy Schalnat 已提交
2799
               {
2800 2801
                  sp = row;
                  for (i = 0; i < row_width; i++, sp += 6)
G
Guy Schalnat 已提交
2802
                  {
2803 2804 2805
                     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 已提交
2806

A
Andreas Dilger 已提交
2807
                     if (r == trans_values->red && g == trans_values->green &&
G
Guy Schalnat 已提交
2808
                        b == trans_values->blue)
G
Guy Schalnat 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822
                     {
                        *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 已提交
2823
         {
A
Andreas Dilger 已提交
2824
            if (row_info->bit_depth == 8)
G
Guy Schalnat 已提交
2825
            {
G
Guy Schalnat 已提交
2826
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2827 2828 2829
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                   gamma_table != NULL)
               {
2830 2831 2832
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
2833
                  {
2834
                     png_uint_16 a = *(sp + 1);
G
Guy Schalnat 已提交
2835

A
Andreas Dilger 已提交
2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
                     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 已提交
2848

A
Andreas Dilger 已提交
2849 2850 2851
                        v = gamma_to_1[*sp];
                        png_composite(w, v, a, background_1->gray);
                        *dp = gamma_from_1[w];
G
Guy Schalnat 已提交
2852 2853
                     }
                  }
A
Andreas Dilger 已提交
2854 2855
               }
               else
G
Guy Schalnat 已提交
2856
#endif
A
Andreas Dilger 已提交
2857
               {
2858 2859 2860
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
G
Guy Schalnat 已提交
2861
                  {
2862
                     png_byte a = *(sp + 1);
G
Guy Schalnat 已提交
2863

A
Andreas Dilger 已提交
2864 2865 2866 2867
                     if (a == 0xff)
                     {
                        *dp = *sp;
                     }
2868
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2869 2870 2871 2872 2873 2874 2875
                     else if (a == 0)
                     {
                        *dp = (png_byte)background->gray;
                     }
                     else
                     {
                        png_composite(*dp, *sp, a, background_1->gray);
G
Guy Schalnat 已提交
2876
                     }
2877 2878 2879
#else
                     *dp = (png_byte)background->gray;
#endif
G
Guy Schalnat 已提交
2880 2881
                  }
               }
A
Andreas Dilger 已提交
2882 2883 2884
            }
            else /* if (png_ptr->bit_depth == 16) */
            {
G
Guy Schalnat 已提交
2885
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2886 2887 2888
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                   gamma_16_to_1 != NULL)
               {
2889 2890 2891
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
G
Guy Schalnat 已提交
2892
                  {
2893
                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
A
Andreas Dilger 已提交
2894 2895

                     if (a == (png_uint_16)0xffff)
G
Guy Schalnat 已提交
2896
                     {
A
Andreas Dilger 已提交
2897
                        png_uint_16 v;
G
Guy Schalnat 已提交
2898

A
Andreas Dilger 已提交
2899 2900 2901 2902
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
                     }
2903
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2904
                     else if (a == 0)
2905 2906 2907
#else
                     else
#endif
A
Andreas Dilger 已提交
2908 2909 2910 2911 2912
                     {
                        /* background is already in screen gamma */
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
                     }
2913
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2914 2915 2916
                     else
                     {
                        png_uint_16 g, v, w;
G
Guy Schalnat 已提交
2917

A
Andreas Dilger 已提交
2918 2919 2920 2921 2922
                        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 已提交
2923
                     }
2924
#endif
G
Guy Schalnat 已提交
2925
                  }
A
Andreas Dilger 已提交
2926 2927
               }
               else
G
Guy Schalnat 已提交
2928
#endif
A
Andreas Dilger 已提交
2929
               {
2930 2931 2932
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
G
Guy Schalnat 已提交
2933
                  {
2934
                     png_uint_16 a = (png_uint_16)(((*(sp+2)) << 8) + *(sp+3));
A
Andreas Dilger 已提交
2935
                     if (a == (png_uint_16)0xffff)
G
Guy Schalnat 已提交
2936
                     {
A
Andreas Dilger 已提交
2937 2938
                        png_memcpy(dp, sp, 2);
                     }
2939
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2940
                     else if (a == 0)
2941 2942 2943
#else
                     else
#endif
A
Andreas Dilger 已提交
2944 2945 2946 2947
                     {
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
                     }
2948
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2949 2950 2951
                     else
                     {
                        png_uint_16 g, v;
G
Guy Schalnat 已提交
2952

2953
                        g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
A
Andreas Dilger 已提交
2954 2955 2956
                        png_composite_16(v, g, a, background_1->gray);
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
2957
                     }
2958
#endif
G
Guy Schalnat 已提交
2959 2960 2961 2962 2963 2964 2965 2966 2967
                  }
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_RGB_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
G
Guy Schalnat 已提交
2968
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
2969 2970
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
                   gamma_table != NULL)
G
Guy Schalnat 已提交
2971
               {
2972 2973 2974
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
G
Guy Schalnat 已提交
2975
                  {
2976
                     png_byte a = *(sp + 3);
G
Guy Schalnat 已提交
2977 2978 2979 2980 2981 2982 2983 2984 2985

                     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 已提交
2986 2987 2988 2989
                        /* 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 已提交
2990 2991 2992
                     }
                     else
                     {
A
Andreas Dilger 已提交
2993
                        png_byte v, w;
G
Guy Schalnat 已提交
2994 2995

                        v = gamma_to_1[*sp];
A
Andreas Dilger 已提交
2996 2997
                        png_composite(w, v, a, background_1->red);
                        *dp = gamma_from_1[w];
G
Guy Schalnat 已提交
2998
                        v = gamma_to_1[*(sp + 1)];
A
Andreas Dilger 已提交
2999 3000
                        png_composite(w, v, a, background_1->green);
                        *(dp + 1) = gamma_from_1[w];
G
Guy Schalnat 已提交
3001
                        v = gamma_to_1[*(sp + 2)];
A
Andreas Dilger 已提交
3002 3003
                        png_composite(w, v, a, background_1->blue);
                        *(dp + 2) = gamma_from_1[w];
G
Guy Schalnat 已提交
3004 3005 3006 3007
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
3008
#endif
G
Guy Schalnat 已提交
3009
               {
3010 3011 3012
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
G
Guy Schalnat 已提交
3013
                  {
3014
                     png_byte a = *(sp + 3);
G
Guy Schalnat 已提交
3015 3016 3017 3018 3019 3020 3021 3022 3023

                     if (a == 0xff)
                     {
                        *dp = *sp;
                        *(dp + 1) = *(sp + 1);
                        *(dp + 2) = *(sp + 2);
                     }
                     else if (a == 0)
                     {
A
Andreas Dilger 已提交
3024 3025 3026
                        *dp = (png_byte)background->red;
                        *(dp + 1) = (png_byte)background->green;
                        *(dp + 2) = (png_byte)background->blue;
G
Guy Schalnat 已提交
3027 3028 3029
                     }
                     else
                     {
A
Andreas Dilger 已提交
3030 3031 3032 3033 3034
                        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 已提交
3035 3036 3037 3038
                     }
                  }
               }
            }
A
Andreas Dilger 已提交
3039
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3040
            {
G
Guy Schalnat 已提交
3041
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3042 3043
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
                   gamma_16_to_1 != NULL)
G
Guy Schalnat 已提交
3044
               {
3045 3046 3047
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
G
Guy Schalnat 已提交
3048
                  {
3049 3050
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
                         << 8) + (png_uint_16)(*(sp + 7)));
G
Guy Schalnat 已提交
3051 3052 3053 3054
                     if (a == (png_uint_16)0xffff)
                     {
                        png_uint_16 v;

A
Andreas Dilger 已提交
3055
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3056 3057
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3058
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
G
Guy Schalnat 已提交
3059 3060
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3061
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
G
Guy Schalnat 已提交
3062 3063
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3064 3065 3066
                     }
                     else if (a == 0)
                     {
A
Andreas Dilger 已提交
3067
                        /* background is already in screen gamma */
G
Guy Schalnat 已提交
3068 3069 3070 3071 3072 3073
                        *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 已提交
3074 3075 3076
                     }
                     else
                     {
A
Andreas Dilger 已提交
3077 3078 3079
                        png_uint_16 v, w, x;

                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3080
                        png_composite_16(w, v, a, background_1->red);
A
Andreas Dilger 已提交
3081 3082 3083 3084
                        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)];
3085
                        png_composite_16(w, v, a, background_1->green);
A
Andreas Dilger 已提交
3086 3087 3088 3089
                        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)];
3090
                        png_composite_16(w, v, a, background_1->blue);
A
Andreas Dilger 已提交
3091 3092 3093
                        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 已提交
3094 3095 3096 3097
                     }
                  }
               }
               else
G
Guy Schalnat 已提交
3098
#endif
G
Guy Schalnat 已提交
3099
               {
3100 3101 3102
                  sp = row;
                  dp = row;
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
G
Guy Schalnat 已提交
3103
                  {
3104 3105
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
                        << 8) + (png_uint_16)(*(sp + 7)));
G
Guy Schalnat 已提交
3106 3107
                     if (a == (png_uint_16)0xffff)
                     {
G
Guy Schalnat 已提交
3108
                        png_memcpy(dp, sp, 6);
G
Guy Schalnat 已提交
3109 3110 3111
                     }
                     else if (a == 0)
                     {
G
Guy Schalnat 已提交
3112 3113 3114 3115 3116 3117 3118 3119
                        *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 已提交
3120
                     {
3121
                        png_uint_16 v;
A
Andreas Dilger 已提交
3122

3123 3124 3125 3126 3127
                        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 已提交
3128 3129

                        png_composite_16(v, r, a, background->red);
G
Guy Schalnat 已提交
3130 3131
                        *dp = (png_byte)((v >> 8) & 0xff);
                        *(dp + 1) = (png_byte)(v & 0xff);
A
Andreas Dilger 已提交
3132
                        png_composite_16(v, g, a, background->green);
G
Guy Schalnat 已提交
3133 3134
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 3) = (png_byte)(v & 0xff);
3135
                        png_composite_16(v, b, a, background->blue);
G
Guy Schalnat 已提交
3136 3137 3138 3139 3140 3141 3142 3143 3144
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
                        *(dp + 5) = (png_byte)(v & 0xff);
                     }
                  }
               }
            }
            break;
         }
      }
A
Andreas Dilger 已提交
3145

G
Guy Schalnat 已提交
3146 3147 3148
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
      {
         row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
G
Guy Schalnat 已提交
3149
         row_info->channels--;
G
Guy Schalnat 已提交
3150 3151
         row_info->pixel_depth = (png_byte)(row_info->channels *
            row_info->bit_depth);
3152
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
G
Guy Schalnat 已提交
3153 3154 3155
      }
   }
}
G
Guy Schalnat 已提交
3156
#endif
G
Guy Schalnat 已提交
3157

G
Guy Schalnat 已提交
3158
#if defined(PNG_READ_GAMMA_SUPPORTED)
A
Andreas Dilger 已提交
3159
/* Gamma correct the image, avoiding the alpha channel.  Make sure
3160
 * you do this after you deal with the transparency issue on grayscale
3161
 * or RGB images. If your bit depth is 8, use gamma_table, if it
3162 3163 3164
 * is 16, use gamma_16_table and gamma_shift.  Build these with
 * build_gamma_table().
 */
3165
void /* PRIVATE */
G
Guy Schalnat 已提交
3166 3167
png_do_gamma(png_row_infop row_info, png_bytep row,
   png_bytep gamma_table, png_uint_16pp gamma_16_table,
G
Guy Schalnat 已提交
3168 3169
   int gamma_shift)
{
G
Guy Schalnat 已提交
3170
   png_bytep sp;
G
Guy Schalnat 已提交
3171
   png_uint_32 i;
3172
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3173

A
Andreas Dilger 已提交
3174 3175 3176 3177
   png_debug(1, "in png_do_gamma\n");
   if (
       ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
        (row_info->bit_depth == 16 && gamma_16_table != NULL)))
G
Guy Schalnat 已提交
3178 3179 3180 3181 3182 3183 3184
   {
      switch (row_info->color_type)
      {
         case PNG_COLOR_TYPE_RGB:
         {
            if (row_info->bit_depth == 8)
            {
3185 3186
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3187 3188 3189 3190 3191 3192 3193 3194 3195
               {
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3196
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3197
            {
3198 3199
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3200 3201 3202
               {
                  png_uint_16 v;

A
Andreas Dilger 已提交
3203
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3204 3205 3206
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
A
Andreas Dilger 已提交
3207
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3208 3209
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3210
                  sp += 2;
A
Andreas Dilger 已提交
3211
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222
                  *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)
            {
3223 3224
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3225
               {
G
Guy Schalnat 已提交
3226 3227 3228 3229 3230 3231 3232 3233 3234
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  *sp = gamma_table[*sp];
                  sp++;
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3235
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3236
            {
3237 3238
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3239
               {
3240
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3241 3242
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3243
                  sp += 2;
A
Andreas Dilger 已提交
3244
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3245 3246 3247
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 2;
A
Andreas Dilger 已提交
3248
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3249 3250 3251 3252 3253
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
                  sp += 4;
               }
            }
G
Guy Schalnat 已提交
3254 3255 3256 3257 3258 3259
            break;
         }
         case PNG_COLOR_TYPE_GRAY_ALPHA:
         {
            if (row_info->bit_depth == 8)
            {
3260 3261
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3262 3263
               {
                  *sp = gamma_table[*sp];
A
Andreas Dilger 已提交
3264
                  sp += 2;
G
Guy Schalnat 已提交
3265 3266
               }
            }
A
Andreas Dilger 已提交
3267
            else /* if (row_info->bit_depth == 16) */
G
Guy Schalnat 已提交
3268
            {
3269 3270
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3271
               {
3272
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3273 3274
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3275 3276 3277 3278 3279 3280 3281
                  sp += 4;
               }
            }
            break;
         }
         case PNG_COLOR_TYPE_GRAY:
         {
3282 3283
            if (row_info->bit_depth == 2)
            {
3284 3285
               sp = row;
               for (i = 0; i < row_width; i += 4)
3286 3287 3288 3289 3290 3291
               {
                  int a = *sp & 0xc0;
                  int b = *sp & 0x30;
                  int c = *sp & 0x0c;
                  int d = *sp & 0x03;

3292 3293
                  *sp = (png_byte)(
                        ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)])   ) & 0xc0)|
3294 3295
                        ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
                        ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
3296
                        ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
3297 3298 3299
                  sp++;
               }
            }
A
Andreas Dilger 已提交
3300
            if (row_info->bit_depth == 4)
G
Guy Schalnat 已提交
3301
            {
3302 3303
               sp = row;
               for (i = 0; i < row_width; i += 2)
A
Andreas Dilger 已提交
3304 3305 3306 3307
               {
                  int msb = *sp & 0xf0;
                  int lsb = *sp & 0x0f;

3308 3309
                  *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
                          | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
A
Andreas Dilger 已提交
3310 3311 3312 3313 3314
                  sp++;
               }
            }
            else if (row_info->bit_depth == 8)
            {
3315 3316
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3317 3318 3319 3320 3321 3322 3323
               {
                  *sp = gamma_table[*sp];
                  sp++;
               }
            }
            else if (row_info->bit_depth == 16)
            {
3324 3325
               sp = row;
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3326
               {
3327
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
G
Guy Schalnat 已提交
3328 3329
                  *sp = (png_byte)((v >> 8) & 0xff);
                  *(sp + 1) = (png_byte)(v & 0xff);
G
Guy Schalnat 已提交
3330 3331 3332 3333 3334 3335 3336 3337
                  sp += 2;
               }
            }
            break;
         }
      }
   }
}
G
Guy Schalnat 已提交
3338
#endif
G
Guy Schalnat 已提交
3339

G
Guy Schalnat 已提交
3340
#if defined(PNG_READ_EXPAND_SUPPORTED)
3341
/* Expands a palette row to an RGB or RGBA row depending
3342 3343
 * upon whether you supply trans and num_trans.
 */
3344
void /* PRIVATE */
G
Guy Schalnat 已提交
3345
png_do_expand_palette(png_row_infop row_info, png_bytep row,
A
Andreas Dilger 已提交
3346
   png_colorp palette, png_bytep trans, int num_trans)
G
Guy Schalnat 已提交
3347
{
G
Guy Schalnat 已提交
3348
   int shift, value;
G
Guy Schalnat 已提交
3349
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3350
   png_uint_32 i;
3351
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3352

A
Andreas Dilger 已提交
3353 3354 3355
   png_debug(1, "in png_do_expand_palette\n");
   if (
       row_info->color_type == PNG_COLOR_TYPE_PALETTE)
G
Guy Schalnat 已提交
3356 3357 3358 3359 3360 3361 3362
   {
      if (row_info->bit_depth < 8)
      {
         switch (row_info->bit_depth)
         {
            case 1:
            {
3363 3364
               sp = row + (png_size_t)((row_width - 1) >> 3);
               dp = row + (png_size_t)row_width - 1;
3365
               shift = 7 - (int)((row_width + 7) & 0x07);
3366
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3367
               {
3368
                  if ((*sp >> shift) & 0x01)
G
Guy Schalnat 已提交
3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385
                     *dp = 1;
                  else
                     *dp = 0;
                  if (shift == 7)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift++;

                  dp--;
               }
               break;
            }
            case 2:
            {
3386 3387
               sp = row + (png_size_t)((row_width - 1) >> 2);
               dp = row + (png_size_t)row_width - 1;
3388
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3389
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3390
               {
3391
                  value = (*sp >> shift) & 0x03;
G
Guy Schalnat 已提交
3392
                  *dp = (png_byte)value;
G
Guy Schalnat 已提交
3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406
                  if (shift == 6)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 2;

                  dp--;
               }
               break;
            }
            case 4:
            {
3407 3408
               sp = row + (png_size_t)((row_width - 1) >> 1);
               dp = row + (png_size_t)row_width - 1;
3409
               shift = (int)((row_width & 0x01) << 2);
3410
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3411
               {
3412
                  value = (*sp >> shift) & 0x0f;
G
Guy Schalnat 已提交
3413
                  *dp = (png_byte)value;
G
Guy Schalnat 已提交
3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428
                  if (shift == 4)
                  {
                     shift = 0;
                     sp--;
                  }
                  else
                     shift += 4;

                  dp--;
               }
               break;
            }
         }
         row_info->bit_depth = 8;
         row_info->pixel_depth = 8;
3429
         row_info->rowbytes = row_width;
G
Guy Schalnat 已提交
3430 3431 3432 3433 3434
      }
      switch (row_info->bit_depth)
      {
         case 8:
         {
A
Andreas Dilger 已提交
3435
            if (trans != NULL)
G
Guy Schalnat 已提交
3436
            {
3437 3438
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width << 2) - 1;
G
Guy Schalnat 已提交
3439

3440
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3441
               {
A
Andreas Dilger 已提交
3442
                  if ((int)(*sp) >= num_trans)
G
Guy Schalnat 已提交
3443 3444 3445 3446 3447 3448 3449 3450 3451 3452
                     *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;
3453
               row_info->rowbytes = row_width * 4;
G
Guy Schalnat 已提交
3454 3455 3456 3457 3458
               row_info->color_type = 6;
               row_info->channels = 4;
            }
            else
            {
3459 3460
               sp = row + (png_size_t)row_width - 1;
               dp = row + (png_size_t)(row_width * 3) - 1;
G
Guy Schalnat 已提交
3461

3462
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3463 3464 3465 3466 3467 3468 3469 3470
               {
                  *dp-- = palette[*sp].blue;
                  *dp-- = palette[*sp].green;
                  *dp-- = palette[*sp].red;
                  sp--;
               }
               row_info->bit_depth = 8;
               row_info->pixel_depth = 24;
3471
               row_info->rowbytes = row_width * 3;
G
Guy Schalnat 已提交
3472 3473 3474 3475 3476 3477 3478 3479 3480
               row_info->color_type = 2;
               row_info->channels = 3;
            }
            break;
         }
      }
   }
}

3481 3482
/* If the bit depth < 8, it is expanded to 8.  Also, if the already
 * expanded transparency value is supplied, an alpha channel is built.
3483
 */
3484
void /* PRIVATE */
G
Guy Schalnat 已提交
3485
png_do_expand(png_row_infop row_info, png_bytep row,
G
Guy Schalnat 已提交
3486
   png_color_16p trans_value)
G
Guy Schalnat 已提交
3487
{
G
Guy Schalnat 已提交
3488
   int shift, value;
G
Guy Schalnat 已提交
3489
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3490
   png_uint_32 i;
3491
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3492

A
Andreas Dilger 已提交
3493
   png_debug(1, "in png_do_expand\n");
G
Guy Schalnat 已提交
3494
   {
A
Andreas Dilger 已提交
3495
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
G
Guy Schalnat 已提交
3496
      {
3497
         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
A
Andreas Dilger 已提交
3498 3499

         if (row_info->bit_depth < 8)
G
Guy Schalnat 已提交
3500
         {
A
Andreas Dilger 已提交
3501
            switch (row_info->bit_depth)
G
Guy Schalnat 已提交
3502
            {
A
Andreas Dilger 已提交
3503
               case 1:
G
Guy Schalnat 已提交
3504
               {
3505
                  gray = (png_uint_16)((gray&0x01)*0xff);
3506 3507
                  sp = row + (png_size_t)((row_width - 1) >> 3);
                  dp = row + (png_size_t)row_width - 1;
3508
                  shift = 7 - (int)((row_width + 7) & 0x07);
3509
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3510
                  {
3511
                     if ((*sp >> shift) & 0x01)
A
Andreas Dilger 已提交
3512 3513 3514 3515 3516 3517 3518 3519 3520 3521
                        *dp = 0xff;
                     else
                        *dp = 0;
                     if (shift == 7)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift++;
G
Guy Schalnat 已提交
3522

A
Andreas Dilger 已提交
3523 3524 3525
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3526
               }
A
Andreas Dilger 已提交
3527
               case 2:
G
Guy Schalnat 已提交
3528
               {
3529
                  gray = (png_uint_16)((gray&0x03)*0x55);
3530 3531
                  sp = row + (png_size_t)((row_width - 1) >> 2);
                  dp = row + (png_size_t)row_width - 1;
3532
                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3533
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3534
                  {
3535
                     value = (*sp >> shift) & 0x03;
A
Andreas Dilger 已提交
3536 3537 3538 3539 3540 3541 3542 3543 3544
                     *dp = (png_byte)(value | (value << 2) | (value << 4) |
                        (value << 6));
                     if (shift == 6)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift += 2;
G
Guy Schalnat 已提交
3545

A
Andreas Dilger 已提交
3546 3547 3548
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3549
               }
A
Andreas Dilger 已提交
3550
               case 4:
G
Guy Schalnat 已提交
3551
               {
3552
                  gray = (png_uint_16)((gray&0x0f)*0x11);
3553 3554
                  sp = row + (png_size_t)((row_width - 1) >> 1);
                  dp = row + (png_size_t)row_width - 1;
3555
                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3556
                  for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3557
                  {
3558
                     value = (*sp >> shift) & 0x0f;
A
Andreas Dilger 已提交
3559 3560 3561 3562 3563 3564 3565 3566
                     *dp = (png_byte)(value | (value << 4));
                     if (shift == 4)
                     {
                        shift = 0;
                        sp--;
                     }
                     else
                        shift = 4;
G
Guy Schalnat 已提交
3567

A
Andreas Dilger 已提交
3568 3569 3570
                     dp--;
                  }
                  break;
G
Guy Schalnat 已提交
3571 3572
               }
            }
A
Andreas Dilger 已提交
3573 3574
            row_info->bit_depth = 8;
            row_info->pixel_depth = 8;
3575
            row_info->rowbytes = row_width;
G
Guy Schalnat 已提交
3576
         }
A
Andreas Dilger 已提交
3577

A
Andreas Dilger 已提交
3578
         if (trans_value != NULL)
G
Guy Schalnat 已提交
3579
         {
A
Andreas Dilger 已提交
3580
            if (row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3581
            {
3582
               gray = gray & 0xff;
3583 3584 3585
               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 已提交
3586
               {
A
Andreas Dilger 已提交
3587 3588 3589 3590 3591
                  if (*sp == gray)
                     *dp-- = 0;
                  else
                     *dp-- = 0xff;
                  *dp-- = *sp--;
G
Guy Schalnat 已提交
3592
               }
A
Andreas Dilger 已提交
3593 3594 3595
            }
            else if (row_info->bit_depth == 16)
            {
3596 3597
               png_byte gray_high = (gray >> 8) & 0xff;
               png_byte gray_low = gray & 0xff;
A
Andreas Dilger 已提交
3598 3599
               sp = row + row_info->rowbytes - 1;
               dp = row + (row_info->rowbytes << 1) - 1;
3600
               for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3601
               {
3602
                  if (*(sp - 1) == gray_high && *(sp) == gray_low) 
A
Andreas Dilger 已提交
3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613
                  {
                     *dp-- = 0;
                     *dp-- = 0;
                  }
                  else
                  {
                     *dp-- = 0xff;
                     *dp-- = 0xff;
                  }
                  *dp-- = *sp--;
                  *dp-- = *sp--;
G
Guy Schalnat 已提交
3614 3615
               }
            }
A
Andreas Dilger 已提交
3616 3617 3618
            row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
            row_info->channels = 2;
            row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
3619 3620
            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
               row_width);
G
Guy Schalnat 已提交
3621 3622 3623 3624 3625 3626
         }
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
      {
         if (row_info->bit_depth == 8)
         {
3627 3628 3629
            png_byte red = trans_value->red & 0xff;
            png_byte green = trans_value->green & 0xff;
            png_byte blue = trans_value->blue & 0xff;
G
Guy Schalnat 已提交
3630
            sp = row + (png_size_t)row_info->rowbytes - 1;
3631 3632
            dp = row + (png_size_t)(row_width << 2) - 1;
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3633
            {
3634
               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
G
Guy Schalnat 已提交
3635 3636 3637 3638 3639 3640 3641 3642 3643 3644
                  *dp-- = 0;
               else
                  *dp-- = 0xff;
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
            }
         }
         else if (row_info->bit_depth == 16)
         {
3645 3646 3647 3648 3649 3650
            png_byte red_high = (trans_value->red >> 8) & 0xff;
            png_byte green_high = (trans_value->green >> 8) & 0xff;
            png_byte blue_high = (trans_value->blue >> 8) & 0xff;
            png_byte red_low = trans_value->red & 0xff;
            png_byte green_low = trans_value->green & 0xff;
            png_byte blue_low = trans_value->blue & 0xff;
A
Andreas Dilger 已提交
3651
            sp = row + row_info->rowbytes - 1;
3652 3653
            dp = row + (png_size_t)(row_width << 3) - 1;
            for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3654
            {
3655 3656 3657 3658 3659 3660
               if (*(sp - 5) == red_high &&
                  *(sp - 4) == red_low &&
                  *(sp - 3) == green_high &&
                  *(sp - 2) == green_low &&
                  *(sp - 1) == blue_high &&
                  *(sp    ) == blue_low)
G
Guy Schalnat 已提交
3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673
               {
                  *dp-- = 0;
                  *dp-- = 0;
               }
               else
               {
                  *dp-- = 0xff;
                  *dp-- = 0xff;
               }
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
               *dp-- = *sp--;
G
Guy Schalnat 已提交
3674
               *dp-- = *sp--;
G
Guy Schalnat 已提交
3675 3676 3677 3678 3679
               *dp-- = *sp--;
            }
         }
         row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
         row_info->channels = 4;
G
Guy Schalnat 已提交
3680
         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
3681
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
G
Guy Schalnat 已提交
3682 3683 3684
      }
   }
}
G
Guy Schalnat 已提交
3685
#endif
G
Guy Schalnat 已提交
3686

G
Guy Schalnat 已提交
3687
#if defined(PNG_READ_DITHER_SUPPORTED)
3688
void /* PRIVATE */
G
Guy Schalnat 已提交
3689
png_do_dither(png_row_infop row_info, png_bytep row,
G
Guy Schalnat 已提交
3690
    png_bytep palette_lookup, png_bytep dither_lookup)
G
Guy Schalnat 已提交
3691
{
G
Guy Schalnat 已提交
3692
   png_bytep sp, dp;
G
Guy Schalnat 已提交
3693
   png_uint_32 i;
3694
   png_uint_32 row_width=row_info->width;
G
Guy Schalnat 已提交
3695

A
Andreas Dilger 已提交
3696
   png_debug(1, "in png_do_dither\n");
G
Guy Schalnat 已提交
3697 3698 3699 3700 3701 3702 3703
   {
      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;
3704
         for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720
         {
            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 已提交
3721
               ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
G
Guy Schalnat 已提交
3722 3723 3724 3725 3726 3727 3728 3729 3730
               (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;
3731
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
G
Guy Schalnat 已提交
3732 3733
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
A
Andreas Dilger 已提交
3734
         palette_lookup != NULL && row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3735 3736 3737 3738
      {
         int r, g, b, p;
         sp = row;
         dp = row;
3739
         for (i = 0; i < row_width; i++)
G
Guy Schalnat 已提交
3740 3741 3742 3743 3744 3745 3746
         {
            r = *sp++;
            g = *sp++;
            b = *sp++;
            sp++;

            p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
G
Guy Schalnat 已提交
3747
               ((1 << PNG_DITHER_RED_BITS) - 1)) <<
G
Guy Schalnat 已提交
3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759
               (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;
3760
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
G
Guy Schalnat 已提交
3761 3762 3763
      }
      else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
         dither_lookup && row_info->bit_depth == 8)
G
Guy Schalnat 已提交
3764
      {
G
Guy Schalnat 已提交
3765
         sp = row;
3766
         for (i = 0; i < row_width; i++, sp++)
G
Guy Schalnat 已提交
3767 3768 3769 3770 3771 3772
         {
            *sp = dither_lookup[*sp];
         }
      }
   }
}
G
Guy Schalnat 已提交
3773
#endif
G
Guy Schalnat 已提交
3774

3775
#ifdef PNG_FLOATING_POINT_SUPPORTED
G
Guy Schalnat 已提交
3776
#if defined(PNG_READ_GAMMA_SUPPORTED)
3777 3778
static PNG_CONST int png_gamma_shift[] =
   {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0, 0x00};
G
Guy Schalnat 已提交
3779

A
Andreas Dilger 已提交
3780
/* We build the 8- or 16-bit gamma tables here.  Note that for 16-bit
3781 3782 3783 3784
 * 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.
 */
3785
void /* PRIVATE */
G
Guy Schalnat 已提交
3786
png_build_gamma_table(png_structp png_ptr)
G
Guy Schalnat 已提交
3787
{
3788
  png_debug(1, "in png_build_gamma_table\n");
3789 3790

  if (png_ptr->bit_depth <= 8)
3791
  {
3792 3793
     int i;
     double g;
G
Guy Schalnat 已提交
3794

3795 3796 3797 3798
     if (png_ptr->screen_gamma > .000001)
        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
     else
        g = 1.0;
G
Guy Schalnat 已提交
3799

3800 3801
     png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
        (png_uint_32)256);
G
Guy Schalnat 已提交
3802

3803 3804 3805 3806 3807
     for (i = 0; i < 256; i++)
     {
        png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
           g) * 255.0 + .5);
     }
G
Guy Schalnat 已提交
3808

3809
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3810 3811 3812
   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
     if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY))
     {
3813

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

3816 3817
        png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
           (png_uint_32)256);
G
Guy Schalnat 已提交
3818

3819 3820 3821 3822 3823
        for (i = 0; i < 256; i++)
        {
           png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
              g) * 255.0 + .5);
        }
G
Guy Schalnat 已提交
3824

3825

3826 3827
        png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
           (png_uint_32)256);
G
Guy Schalnat 已提交
3828

3829
        if (png_ptr->screen_gamma > 0.000001)
3830 3831 3832
           g = 1.0 / png_ptr->screen_gamma;
        else
           g = png_ptr->gamma;   /* probably doing rgb_to_gray */
3833

3834 3835 3836 3837
        for (i = 0; i < 256; i++)
        {
           png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
              g) * 255.0 + .5);
3838

3839 3840
        }
     }
3841
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
3842 3843 3844 3845 3846 3847 3848
  }
  else
  {
     double g;
     int i, j, shift, num;
     int sig_bit;
     png_uint_32 ig;
G
Guy Schalnat 已提交
3849

3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861
     if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
     {
        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)
           sig_bit = png_ptr->sig_bit.blue;
     }
     else
     {
        sig_bit = (int)png_ptr->sig_bit.gray;
     }
G
Guy Schalnat 已提交
3862

3863 3864 3865 3866
     if (sig_bit > 0)
        shift = 16 - sig_bit;
     else
        shift = 0;
G
Guy Schalnat 已提交
3867

3868 3869 3870 3871 3872
     if (png_ptr->transformations & PNG_16_TO_8)
     {
        if (shift < (16 - PNG_MAX_GAMMA_8))
           shift = (16 - PNG_MAX_GAMMA_8);
     }
G
Guy Schalnat 已提交
3873

3874 3875 3876 3877
     if (shift > 8)
        shift = 8;
     if (shift < 0)
        shift = 0;
G
Guy Schalnat 已提交
3878

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

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

3883 3884 3885 3886
     if (png_ptr->screen_gamma > .000001)
        g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
     else
        g = 1.0;
G
Guy Schalnat 已提交
3887

3888
     png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
3889
        (png_uint_32)(num * png_sizeof(png_uint_16p)));
G
Guy Schalnat 已提交
3890

3891 3892 3893 3894
     if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
     {
        double fin, fout;
        png_uint_32 last, max;
G
Guy Schalnat 已提交
3895

3896 3897 3898
        for (i = 0; i < num; i++)
        {
           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
3899
              (png_uint_32)(256 * png_sizeof(png_uint_16)));
3900
        }
G
Guy Schalnat 已提交
3901

3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928
        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))]
              [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
           last++;
        }
     }
     else
     {
        for (i = 0; i < num; i++)
        {
           png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
3929
              (png_uint_32)(256 * png_sizeof(png_uint_16)));
G
Guy Schalnat 已提交
3930

3931 3932 3933 3934 3935 3936 3937 3938 3939
           ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
           for (j = 0; j < 256; j++)
           {
              png_ptr->gamma_16_table[i][j] =
                 (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
                    65535.0, g) * 65535.0 + .5);
           }
        }
     }
G
Guy Schalnat 已提交
3940

3941
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3942 3943 3944
   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
     if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
     {
3945

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

3948
        png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
3949
           (png_uint_32)(num * png_sizeof(png_uint_16p )));
G
Guy Schalnat 已提交
3950

3951 3952 3953
        for (i = 0; i < num; i++)
        {
           png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
3954
              (png_uint_32)(256 * png_sizeof(png_uint_16)));
G
Guy Schalnat 已提交
3955

3956 3957 3958 3959 3960 3961 3962 3963 3964
           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);
           }
        }
3965

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

3971
        png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
3972
           (png_uint_32)(num * png_sizeof(png_uint_16p)));
G
Guy Schalnat 已提交
3973

3974 3975 3976
        for (i = 0; i < num; i++)
        {
           png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
3977
              (png_uint_32)(256 * png_sizeof(png_uint_16)));
G
Guy Schalnat 已提交
3978

3979 3980 3981 3982 3983 3984 3985 3986 3987 3988
           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);
           }
        }
     }
3989
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
3990
  }
G
Guy Schalnat 已提交
3991
}
G
Guy Schalnat 已提交
3992
#endif
3993 3994
/* To do: install integer version of png_build_gamma_table here */
#endif
G
Guy Schalnat 已提交
3995

3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038
#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 (
       (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)
         {
4039 4040 4041 4042 4043
            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  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
4044 4045 4046 4047
            *(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);
4048 4049 4050 4051 4052
         }
      }
   }
}
#endif /* PNG_MNG_FEATURES_SUPPORTED */
4053
#endif /* PNG_READ_SUPPORTED */