Coverage Report

Created: 2025-12-31 10:39

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