Coverage Report

Created: 2026-08-14 07:18

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