Coverage Report

Created: 2026-06-07 06:57

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