Coverage Report

Created: 2022-11-20 06:11

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