Coverage Report

Created: 2026-06-30 06:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libpng/pngrtran.c
Line
Count
Source
1
/* pngrtran.c - transforms the data in a row for PNG readers
2
 *
3
 * Copyright (c) 2018-2026 Cosmin Truta
4
 * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
5
 * Copyright (c) 1996-1997 Andreas Dilger
6
 * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
7
 *
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
10
 * and license in png.h
11
 *
12
 * This file contains functions optionally called by an application
13
 * in order to tell libpng how to handle data when reading a PNG.
14
 * Transformations that are used in both reading and writing are
15
 * in pngtrans.c.
16
 */
17
18
#include "pngpriv.h"
19
20
#ifdef PNG_ARM_NEON_IMPLEMENTATION
21
#  if PNG_ARM_NEON_IMPLEMENTATION == 1
22
#    define PNG_ARM_NEON_INTRINSICS_AVAILABLE
23
#    if defined(_MSC_VER) && !defined(__clang__) && defined(_M_ARM64)
24
#      include <arm64_neon.h>
25
#    else
26
#      include <arm_neon.h>
27
#    endif
28
#  endif
29
#endif
30
31
#ifdef PNG_RISCV_RVV_IMPLEMENTATION
32
#  if PNG_RISCV_RVV_IMPLEMENTATION == 1
33
#    define PNG_RISCV_RVV_INTRINSICS_AVAILABLE
34
#  endif
35
#endif
36
37
#ifdef PNG_READ_SUPPORTED
38
39
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
40
void PNGAPI
41
png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
42
7.90k
{
43
7.90k
   png_debug(1, "in png_set_crc_action");
44
45
7.90k
   if (png_ptr == NULL)
46
0
      return;
47
48
   /* Tell libpng how we react to CRC errors in critical chunks */
49
7.90k
   switch (crit_action)
50
7.90k
   {
51
0
      case PNG_CRC_NO_CHANGE:                        /* Leave setting as is */
52
0
         break;
53
54
0
      case PNG_CRC_WARN_USE:                               /* Warn/use data */
55
0
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
56
0
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
57
0
         break;
58
59
7.90k
      case PNG_CRC_QUIET_USE:                             /* Quiet/use data */
60
7.90k
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
61
7.90k
         png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
62
7.90k
                           PNG_FLAG_CRC_CRITICAL_IGNORE;
63
7.90k
         break;
64
65
0
      case PNG_CRC_WARN_DISCARD:    /* Not a valid action for critical data */
66
0
         png_warning(png_ptr,
67
0
             "Can't discard critical data on CRC error");
68
         /* FALLTHROUGH */
69
0
      case PNG_CRC_ERROR_QUIT:                                /* Error/quit */
70
71
0
      case PNG_CRC_DEFAULT:
72
0
      default:
73
0
         png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
74
0
         break;
75
7.90k
   }
76
77
   /* Tell libpng how we react to CRC errors in ancillary chunks */
78
7.90k
   switch (ancil_action)
79
7.90k
   {
80
0
      case PNG_CRC_NO_CHANGE:                       /* Leave setting as is */
81
0
         break;
82
83
0
      case PNG_CRC_WARN_USE:                              /* Warn/use data */
84
0
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
85
0
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
86
0
         break;
87
88
7.90k
      case PNG_CRC_QUIET_USE:                            /* Quiet/use data */
89
7.90k
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
90
7.90k
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
91
7.90k
                           PNG_FLAG_CRC_ANCILLARY_NOWARN;
92
7.90k
         break;
93
94
0
      case PNG_CRC_ERROR_QUIT:                               /* Error/quit */
95
0
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
96
0
         png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
97
0
         break;
98
99
0
      case PNG_CRC_WARN_DISCARD:                      /* Warn/discard data */
100
101
0
      case PNG_CRC_DEFAULT:
102
0
      default:
103
0
         png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
104
0
         break;
105
7.90k
   }
106
7.90k
}
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
62.4k
{
117
62.4k
   if (png_ptr != NULL)
118
62.4k
   {
119
62.4k
      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
62.4k
      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
62.4k
      else
127
62.4k
      {
128
         /* Turn on failure to initialize correctly for all transforms. */
129
62.4k
         png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
130
131
62.4k
         return 1; /* Ok */
132
62.4k
      }
133
62.4k
   }
134
135
0
   return 0; /* no png_error possible! */
136
62.4k
}
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
473
{
146
473
   png_debug(1, "in png_set_background_fixed");
147
148
473
   if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
149
0
      return;
150
151
473
   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
473
   png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
158
473
   png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
159
473
   png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
160
161
473
   png_ptr->background = *background_color;
162
473
   png_ptr->background_gamma = background_gamma;
163
473
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
164
473
   if (need_expand != 0)
165
0
      png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
166
473
   else
167
473
      png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
168
473
}
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
8.63k
{
190
8.63k
   png_debug(1, "in png_set_scale_16");
191
192
8.63k
   if (png_rtran_ok(png_ptr, 0) == 0)
193
0
      return;
194
195
8.63k
   png_ptr->transformations |= PNG_SCALE_16_TO_8;
196
8.63k
}
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.62k
{
204
2.62k
   png_debug(1, "in png_set_strip_16");
205
206
2.62k
   if (png_rtran_ok(png_ptr, 0) == 0)
207
0
      return;
208
209
2.62k
   png_ptr->transformations |= PNG_16_TO_8;
210
2.62k
}
211
#endif
212
213
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
214
void PNGAPI
215
png_set_strip_alpha(png_structrp png_ptr)
216
2.57k
{
217
2.57k
   png_debug(1, "in png_set_strip_alpha");
218
219
2.57k
   if (png_rtran_ok(png_ptr, 0) == 0)
220
0
      return;
221
222
2.57k
   png_ptr->transformations |= PNG_STRIP_ALPHA;
223
2.57k
}
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
12.4k
{
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
12.4k
   if (output_gamma == PNG_DEFAULT_sRGB ||
287
3.57k
      output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
288
8.91k
   {
289
8.91k
      if (is_screen != 0)
290
8.91k
         output_gamma = PNG_GAMMA_sRGB;
291
0
      else
292
0
         output_gamma = PNG_GAMMA_sRGB_INVERSE;
293
8.91k
   }
294
295
3.57k
   else if (output_gamma == PNG_GAMMA_MAC_18 ||
296
3.57k
      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
12.4k
   return output_gamma;
305
12.4k
}
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
12.4k
{
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
12.4k
   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
12.4k
   return 0;
356
12.4k
}
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
12.4k
{
364
12.4k
   png_fixed_point file_gamma;
365
12.4k
   int compose = 0;
366
367
12.4k
   png_debug(1, "in png_set_alpha_mode_fixed");
368
369
12.4k
   if (png_rtran_ok(png_ptr, 0) == 0)
370
0
      return;
371
372
12.4k
   output_gamma = translate_gamma_flags(output_gamma, 1/*screen*/);
373
12.4k
   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
12.4k
   file_gamma = png_ptr->default_gamma;
382
12.4k
   if (file_gamma == 0)
383
5.97k
   {
384
5.97k
      file_gamma = png_reciprocal(output_gamma);
385
5.97k
      png_ptr->default_gamma = file_gamma;
386
5.97k
   }
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
12.4k
   switch (mode)
405
12.4k
   {
406
11.8k
      case PNG_ALPHA_PNG:        /* default: png standard */
407
         /* No compose, but it may be set by png_set_background! */
408
11.8k
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
409
11.8k
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
410
11.8k
         break;
411
412
203
      case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
413
203
         compose = 1;
414
203
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
415
203
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
416
         /* The output is linear: */
417
203
         output_gamma = PNG_FP_1;
418
203
         break;
419
420
393
      case PNG_ALPHA_OPTIMIZED:  /* associated, non-opaque pixels linear */
421
393
         compose = 1;
422
393
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
423
393
         png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
424
         /* output_gamma records the encoding of opaque pixels! */
425
393
         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
12.4k
   }
436
437
   /* Set the screen gamma values: */
438
12.4k
   png_ptr->screen_gamma = output_gamma;
439
440
   /* Finally, if pre-multiplying, set the background fields to achieve the
441
    * desired result.
442
    */
443
12.4k
   if (compose != 0)
444
596
   {
445
      /* And obtain alpha pre-multiplication by composing on black: */
446
596
      memset(&png_ptr->background, 0, (sizeof png_ptr->background));
447
596
      png_ptr->background_gamma = file_gamma; /* just in case */
448
596
      png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
449
596
      png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
450
451
596
      if ((png_ptr->transformations & PNG_COMPOSE) != 0)
452
3
         png_error(png_ptr,
453
3
             "conflicting calls to set alpha mode and background");
454
455
593
      png_ptr->transformations |= PNG_COMPOSE;
456
593
   }
457
12.4k
}
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
11.8k
{
951
11.8k
   png_debug(1, "in png_set_expand");
952
953
11.8k
   if (png_rtran_ok(png_ptr, 0) == 0)
954
0
      return;
955
956
11.8k
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
957
11.8k
}
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
7.91k
{
993
7.91k
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
994
995
7.91k
   if (png_rtran_ok(png_ptr, 0) == 0)
996
0
      return;
997
998
7.91k
   png_ptr->transformations |= PNG_EXPAND;
999
7.91k
}
1000
1001
/* Expand tRNS chunks to alpha channels. */
1002
void PNGAPI
1003
png_set_tRNS_to_alpha(png_structrp png_ptr)
1004
3.60k
{
1005
3.60k
   png_debug(1, "in png_set_tRNS_to_alpha");
1006
1007
3.60k
   if (png_rtran_ok(png_ptr, 0) == 0)
1008
0
      return;
1009
1010
3.60k
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
1011
3.60k
}
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.03k
{
1021
3.03k
   png_debug(1, "in png_set_expand_16");
1022
1023
3.03k
   if (png_rtran_ok(png_ptr, 0) == 0)
1024
0
      return;
1025
1026
3.03k
   png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
1027
3.03k
}
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
7.91k
{
1034
7.91k
   png_debug(1, "in png_set_gray_to_rgb");
1035
1036
7.91k
   if (png_rtran_ok(png_ptr, 0) == 0)
1037
0
      return;
1038
1039
   /* Because rgb must be 8 bits or more: */
1040
7.91k
   png_set_expand_gray_1_2_4_to_8(png_ptr);
1041
7.91k
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
1042
7.91k
}
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.30k
{
1050
1.30k
   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.30k
   if (png_rtran_ok(png_ptr, 1) == 0)
1055
0
      return;
1056
1057
1.30k
   switch (error_action)
1058
1.30k
   {
1059
1.30k
      case PNG_ERROR_ACTION_NONE:
1060
1.30k
         png_ptr->transformations |= PNG_RGB_TO_GRAY;
1061
1.30k
         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.30k
   }
1074
1075
1.30k
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1076
63
#ifdef PNG_READ_EXPAND_SUPPORTED
1077
63
      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.30k
   {
1090
1.30k
      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.30k
      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.30k
   }
1111
1.30k
}
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
6.42k
{
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
6.42k
   png_fixed_point gtest;
1162
6.42k
   return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
1163
6.36k
       png_gamma_significant(gtest);
1164
6.42k
}
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
1.46k
{
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
1.46k
   int input_has_alpha = 0;
1188
1.46k
   int input_has_transparency = 0;
1189
1190
1.46k
   if (png_ptr->num_trans > 0)
1191
526
   {
1192
526
      int i;
1193
1194
      /* Ignore if all the entries are opaque (unlikely!) */
1195
4.49k
      for (i=0; i<png_ptr->num_trans; ++i)
1196
4.31k
      {
1197
4.31k
         if (png_ptr->trans_alpha[i] == 255)
1198
619
            continue;
1199
3.70k
         else if (png_ptr->trans_alpha[i] == 0)
1200
3.35k
            input_has_transparency = 1;
1201
349
         else
1202
349
         {
1203
349
            input_has_transparency = 1;
1204
349
            input_has_alpha = 1;
1205
349
            break;
1206
349
         }
1207
4.31k
      }
1208
526
   }
1209
1210
   /* If no alpha we can optimize. */
1211
1.46k
   if (input_has_alpha == 0)
1212
1.11k
   {
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.11k
      png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1218
1.11k
      png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1219
1220
1.11k
      if (input_has_transparency == 0)
1221
969
         png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1222
1.11k
   }
1223
1224
1.46k
#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
1.46k
   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
1.46k
#endif /* READ_EXPAND && READ_BACKGROUND */
1263
1.46k
}
1264
1265
static void /* PRIVATE */
1266
png_init_rgb_transformations(png_structrp png_ptr)
1267
13.2k
{
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
13.2k
   int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
1273
13.2k
   int input_has_transparency = png_ptr->num_trans > 0;
1274
1275
   /* If no alpha we can optimize. */
1276
13.2k
   if (input_has_alpha == 0)
1277
8.70k
   {
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
8.70k
#     ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1283
8.70k
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1284
8.70k
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1285
8.70k
#     endif
1286
1287
8.70k
      if (input_has_transparency == 0)
1288
7.74k
         png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
1289
8.70k
   }
1290
1291
13.2k
#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
13.2k
   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
13.2k
#endif /* READ_EXPAND && READ_BACKGROUND */
1348
13.2k
}
1349
1350
#ifdef PNG_READ_GAMMA_SUPPORTED
1351
png_fixed_point /* PRIVATE */
1352
png_resolve_file_gamma(png_const_structrp png_ptr)
1353
16.2k
{
1354
16.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
16.2k
   file_gamma = png_ptr->file_gamma;
1366
16.2k
   if (file_gamma != 0)
1367
0
      return file_gamma;
1368
1369
16.2k
   file_gamma = png_ptr->chunk_gamma;
1370
16.2k
   if (file_gamma != 0)
1371
1.73k
      return file_gamma;
1372
1373
14.4k
   file_gamma = png_ptr->default_gamma;
1374
14.4k
   if (file_gamma != 0)
1375
7.42k
      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
7.05k
   if (png_ptr->screen_gamma != 0)
1382
0
      file_gamma = png_reciprocal(png_ptr->screen_gamma);
1383
1384
7.05k
   return file_gamma;
1385
14.4k
}
1386
1387
static int
1388
png_init_gamma_values(png_structrp png_ptr)
1389
14.7k
{
1390
   /* The following temporary indicates if overall gamma correction is
1391
    * required.
1392
    */
1393
14.7k
   int gamma_correction = 0;
1394
14.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
14.7k
   file_gamma = png_resolve_file_gamma(png_ptr);
1400
14.7k
   screen_gamma = png_ptr->screen_gamma;
1401
1402
14.7k
   if (file_gamma > 0) /* file has been set */
1403
7.69k
   {
1404
7.69k
      if (screen_gamma > 0) /* screen set too */
1405
6.42k
         gamma_correction = png_gamma_threshold(file_gamma, screen_gamma);
1406
1407
1.27k
      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.27k
         screen_gamma = png_reciprocal(file_gamma);
1412
7.69k
   }
1413
1414
7.05k
   else /* both unset, prevent corrections: */
1415
7.05k
      file_gamma = screen_gamma = PNG_FP_1;
1416
1417
14.7k
   png_ptr->file_gamma = file_gamma;
1418
14.7k
   png_ptr->screen_gamma = screen_gamma;
1419
14.7k
   return gamma_correction;
1420
1421
14.7k
}
1422
#endif /* READ_GAMMA */
1423
1424
void /* PRIVATE */
1425
png_init_read_transformations(png_structrp png_ptr)
1426
14.7k
{
1427
14.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
14.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
14.7k
   if (png_init_gamma_values(png_ptr) != 0)
1455
3.08k
      png_ptr->transformations |= PNG_GAMMA;
1456
1457
11.6k
   else
1458
11.6k
      png_ptr->transformations &= ~PNG_GAMMA;
1459
14.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
14.7k
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1491
14.7k
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1492
3.02k
       (png_ptr->transformations & PNG_COMPOSE) == 0)
1493
2.42k
   {
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.42k
      png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
1500
2.42k
         PNG_EXPAND_tRNS);
1501
2.42k
      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.42k
      png_ptr->num_trans = 0;
1511
2.42k
   }
1512
14.7k
#endif /* STRIP_ALPHA supported, no COMPOSE */
1513
1514
14.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
14.7k
   if (png_gamma_significant(png_ptr->screen_gamma) == 0)
1519
7.98k
   {
1520
7.98k
      png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1521
7.98k
      png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1522
7.98k
   }
1523
14.7k
#endif
1524
1525
14.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
14.7k
   if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1530
1.28k
      png_set_rgb_coefficients(png_ptr);
1531
14.7k
#endif
1532
1533
14.7k
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1534
14.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
14.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
14.7k
   else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1560
1.02k
   {
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
1.02k
      if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
1567
149
      {
1568
149
         if (png_ptr->background.red == png_ptr->background.green &&
1569
110
             png_ptr->background.red == png_ptr->background.blue)
1570
110
         {
1571
110
            png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1572
110
            png_ptr->background.gray = png_ptr->background.red;
1573
110
         }
1574
149
      }
1575
1.02k
   }
1576
14.7k
#endif /* READ_EXPAND && READ_BACKGROUND */
1577
14.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
14.7k
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1591
1.46k
      png_init_palette_transformations(png_ptr);
1592
1593
13.2k
   else
1594
13.2k
      png_init_rgb_transformations(png_ptr);
1595
1596
14.7k
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1597
14.7k
   defined(PNG_READ_EXPAND_16_SUPPORTED)
1598
14.7k
   if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
1599
2.99k
       (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1600
195
       (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1601
195
       png_ptr->bit_depth != 16)
1602
195
   {
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
780
#     define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
1614
195
      CHOP(png_ptr->background.red);
1615
195
      CHOP(png_ptr->background.green);
1616
195
      CHOP(png_ptr->background.blue);
1617
195
      CHOP(png_ptr->background.gray);
1618
195
#     undef CHOP
1619
195
   }
1620
14.7k
#endif /* READ_BACKGROUND && READ_EXPAND_16 */
1621
1622
14.7k
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1623
14.7k
   (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1624
14.7k
   defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
1625
14.7k
   if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
1626
9.29k
       (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1627
276
       (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1628
276
       png_ptr->bit_depth == 16)
1629
276
   {
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
276
      png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
1637
276
      png_ptr->background.green =
1638
276
         (png_uint_16)(png_ptr->background.green * 257);
1639
276
      png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
1640
276
      png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
1641
276
   }
1642
14.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
14.7k
#ifdef PNG_READ_GAMMA_SUPPORTED
1653
14.7k
#  ifdef PNG_READ_BACKGROUND_SUPPORTED
1654
      /* Includes ALPHA_MODE */
1655
14.7k
      png_ptr->background_1 = png_ptr->background;
1656
14.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
14.7k
   if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
1672
11.6k
       ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
1673
784
        (png_gamma_significant(png_ptr->file_gamma) != 0 ||
1674
144
         png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
1675
11.0k
        ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1676
381
         (png_gamma_significant(png_ptr->file_gamma) != 0 ||
1677
255
          png_gamma_significant(png_ptr->screen_gamma) != 0
1678
255
#  ifdef PNG_READ_BACKGROUND_SUPPORTED
1679
255
         || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
1680
0
           png_gamma_significant(png_ptr->background_gamma) != 0)
1681
381
#  endif
1682
10.8k
        )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
1683
0
       png_gamma_significant(png_ptr->screen_gamma) != 0))
1684
3.84k
   {
1685
3.84k
      png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1686
1687
3.84k
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1688
3.84k
      if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1689
612
      {
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
612
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1697
176
            png_warning(png_ptr,
1698
176
                "libpng does not support gamma+background+rgb_to_gray");
1699
1700
612
         if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
1701
51
         {
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
51
            png_color back, back_1;
1706
51
            png_colorp palette = png_ptr->palette;
1707
51
            int num_palette = png_ptr->num_palette;
1708
51
            int i;
1709
51
            if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1710
47
            {
1711
1712
47
               back.red = png_ptr->gamma_table[png_ptr->background.red];
1713
47
               back.green = png_ptr->gamma_table[png_ptr->background.green];
1714
47
               back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1715
1716
47
               back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1717
47
               back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1718
47
               back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1719
47
            }
1720
4
            else
1721
4
            {
1722
4
               png_fixed_point g, gs;
1723
1724
4
               switch (png_ptr->background_gamma_type)
1725
4
               {
1726
4
                  case PNG_BACKGROUND_GAMMA_SCREEN:
1727
4
                     g = (png_ptr->screen_gamma);
1728
4
                     gs = PNG_FP_1;
1729
4
                     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
4
               }
1747
1748
4
               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
4
               else
1759
4
               {
1760
4
                  back.red   = (png_byte)png_ptr->background.red;
1761
4
                  back.green = (png_byte)png_ptr->background.green;
1762
4
                  back.blue  = (png_byte)png_ptr->background.blue;
1763
4
               }
1764
1765
4
               if (png_gamma_significant(g) != 0)
1766
4
               {
1767
4
                  back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1768
4
                      g);
1769
4
                  back_1.green = png_gamma_8bit_correct(
1770
4
                      png_ptr->background.green, g);
1771
4
                  back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1772
4
                      g);
1773
4
               }
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
4
            }
1782
1783
1.09k
            for (i = 0; i < num_palette; i++)
1784
1.04k
            {
1785
1.04k
               if (i < (int)png_ptr->num_trans &&
1786
829
                   png_ptr->trans_alpha[i] != 0xff)
1787
631
               {
1788
631
                  if (png_ptr->trans_alpha[i] == 0)
1789
226
                  {
1790
226
                     palette[i] = back;
1791
226
                  }
1792
405
                  else /* if (png_ptr->trans_alpha[i] != 0xff) */
1793
405
                  {
1794
405
                     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
209
                     else
1817
209
                     {
1818
                        /* Composite with background color:
1819
                         * component =
1820
                         *    alpha * component + (1 - alpha) * background
1821
                         */
1822
209
                        png_byte v, w;
1823
1824
209
                        v = png_ptr->gamma_to_1[palette[i].red];
1825
209
                        png_composite(w, v,
1826
209
                            png_ptr->trans_alpha[i], back_1.red);
1827
209
                        palette[i].red = png_ptr->gamma_from_1[w];
1828
1829
209
                        v = png_ptr->gamma_to_1[palette[i].green];
1830
209
                        png_composite(w, v,
1831
209
                            png_ptr->trans_alpha[i], back_1.green);
1832
209
                        palette[i].green = png_ptr->gamma_from_1[w];
1833
1834
209
                        v = png_ptr->gamma_to_1[palette[i].blue];
1835
209
                        png_composite(w, v,
1836
209
                            png_ptr->trans_alpha[i], back_1.blue);
1837
209
                        palette[i].blue = png_ptr->gamma_from_1[w];
1838
209
                     }
1839
405
                  }
1840
631
               }
1841
409
               else
1842
409
               {
1843
409
                  palette[i].red = png_ptr->gamma_table[palette[i].red];
1844
409
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
1845
409
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1846
409
               }
1847
1.04k
            }
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
51
            png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1856
51
            png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1857
51
         } /* color_type == PNG_COLOR_TYPE_PALETTE */
1858
1859
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1860
561
         else /* color_type != PNG_COLOR_TYPE_PALETTE */
1861
561
         {
1862
561
            int gs_sig, g_sig;
1863
561
            png_fixed_point g = PNG_FP_1;  /* Correction to linear */
1864
561
            png_fixed_point gs = PNG_FP_1; /* Correction to screen */
1865
1866
561
            switch (png_ptr->background_gamma_type)
1867
561
            {
1868
401
               case PNG_BACKGROUND_GAMMA_SCREEN:
1869
401
                  g = png_ptr->screen_gamma;
1870
                  /* gs = PNG_FP_1; */
1871
401
                  break;
1872
1873
160
               case PNG_BACKGROUND_GAMMA_FILE:
1874
160
                  g = png_reciprocal(png_ptr->file_gamma);
1875
160
                  gs = png_reciprocal2(png_ptr->file_gamma,
1876
160
                      png_ptr->screen_gamma);
1877
160
                  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
561
            }
1888
1889
561
            g_sig = png_gamma_significant(g);
1890
561
            gs_sig = png_gamma_significant(gs);
1891
1892
561
            if (g_sig != 0)
1893
523
               png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1894
523
                   png_ptr->background.gray, g);
1895
1896
561
            if (gs_sig != 0)
1897
124
               png_ptr->background.gray = png_gamma_correct(png_ptr,
1898
124
                   png_ptr->background.gray, gs);
1899
1900
561
            if ((png_ptr->background.red != png_ptr->background.green) ||
1901
504
                (png_ptr->background.red != png_ptr->background.blue) ||
1902
504
                (png_ptr->background.red != png_ptr->background.gray))
1903
57
            {
1904
               /* RGB or RGBA with color background */
1905
57
               if (g_sig != 0)
1906
57
               {
1907
57
                  png_ptr->background_1.red = png_gamma_correct(png_ptr,
1908
57
                      png_ptr->background.red, g);
1909
1910
57
                  png_ptr->background_1.green = png_gamma_correct(png_ptr,
1911
57
                      png_ptr->background.green, g);
1912
1913
57
                  png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1914
57
                      png_ptr->background.blue, g);
1915
57
               }
1916
1917
57
               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
57
            }
1929
1930
504
            else
1931
504
            {
1932
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1933
504
               png_ptr->background_1.red = png_ptr->background_1.green
1934
504
                   = png_ptr->background_1.blue = png_ptr->background_1.gray;
1935
1936
504
               png_ptr->background.red = png_ptr->background.green
1937
504
                   = png_ptr->background.blue = png_ptr->background.gray;
1938
504
            }
1939
1940
            /* The background is now in screen gamma: */
1941
561
            png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
1942
561
         } /* color_type != PNG_COLOR_TYPE_PALETTE */
1943
612
      }/* png_ptr->transformations & PNG_BACKGROUND */
1944
1945
3.23k
      else
1946
      /* Transformation does not include PNG_BACKGROUND */
1947
3.23k
#endif /* READ_BACKGROUND */
1948
3.23k
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
1949
105
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1950
         /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1951
105
         && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1952
105
         (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1953
3.23k
#endif
1954
3.23k
         )
1955
72
      {
1956
72
         png_colorp palette = png_ptr->palette;
1957
72
         int num_palette = png_ptr->num_palette;
1958
72
         int i;
1959
1960
         /* NOTE: there are other transformations that should probably be in
1961
          * here too.
1962
          */
1963
865
         for (i = 0; i < num_palette; i++)
1964
793
         {
1965
793
            palette[i].red = png_ptr->gamma_table[palette[i].red];
1966
793
            palette[i].green = png_ptr->gamma_table[palette[i].green];
1967
793
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1968
793
         }
1969
1970
         /* Done the gamma correction. */
1971
72
         png_ptr->transformations &= ~PNG_GAMMA;
1972
72
      } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1973
3.84k
   }
1974
10.8k
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1975
10.8k
   else
1976
10.8k
#endif
1977
10.8k
#endif /* READ_GAMMA */
1978
1979
10.8k
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1980
   /* No GAMMA transformation (see the hanging else 4 lines above) */
1981
10.8k
   if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1982
255
       (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
14.7k
#endif /* READ_BACKGROUND */
2017
2018
14.7k
#ifdef PNG_READ_SHIFT_SUPPORTED
2019
14.7k
   if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
2020
133
       (png_ptr->transformations & PNG_EXPAND) == 0 &&
2021
55
       (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
220
         for (i=0; i<istop; ++i)
2035
210
         {
2036
210
            int component = png_ptr->palette[i].red;
2037
2038
210
            component >>= shift;
2039
210
            png_ptr->palette[i].red = (png_byte)component;
2040
210
         }
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
14.7k
#endif /* READ_SHIFT */
2063
14.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
14.7k
{
2072
14.7k
   png_debug(1, "in png_read_transform_info");
2073
2074
14.7k
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
2075
1.46k
       info_ptr->palette != NULL && png_ptr->palette != NULL)
2076
1.46k
   {
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
1.46k
      memcpy(info_ptr->palette, png_ptr->palette,
2082
1.46k
          PNG_MAX_PALETTE_LENGTH * (sizeof (png_color)));
2083
1.46k
   }
2084
2085
14.7k
#ifdef PNG_READ_EXPAND_SUPPORTED
2086
14.7k
   if ((png_ptr->transformations & PNG_EXPAND) != 0)
2087
12.7k
   {
2088
12.7k
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2089
995
      {
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
995
         if (png_ptr->num_trans > 0)
2095
375
            info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
2096
2097
620
         else
2098
620
            info_ptr->color_type = PNG_COLOR_TYPE_RGB;
2099
2100
995
         info_ptr->bit_depth = 8;
2101
995
         info_ptr->num_trans = 0;
2102
2103
995
         if (png_ptr->palette == NULL)
2104
0
            png_error (png_ptr, "Palette is NULL in indexed image");
2105
995
      }
2106
11.7k
      else
2107
11.7k
      {
2108
11.7k
         if (png_ptr->num_trans != 0)
2109
767
         {
2110
767
            if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
2111
750
               info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2112
767
         }
2113
11.7k
         if (info_ptr->bit_depth < 8)
2114
2.56k
            info_ptr->bit_depth = 8;
2115
2116
11.7k
         info_ptr->num_trans = 0;
2117
11.7k
      }
2118
12.7k
   }
2119
14.7k
#endif
2120
2121
14.7k
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2122
14.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
14.7k
   if ((png_ptr->transformations & PNG_COMPOSE) != 0)
2127
791
      info_ptr->background = png_ptr->background;
2128
14.7k
#endif
2129
2130
14.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
14.7k
   info_ptr->gamma = png_ptr->file_gamma;
2141
14.7k
#endif
2142
2143
14.7k
   if (info_ptr->bit_depth == 16)
2144
5.60k
   {
2145
5.60k
#  ifdef PNG_READ_16BIT_SUPPORTED
2146
5.60k
#     ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2147
5.60k
         if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
2148
4.89k
            info_ptr->bit_depth = 8;
2149
5.60k
#     endif
2150
2151
5.60k
#     ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2152
5.60k
         if ((png_ptr->transformations & PNG_16_TO_8) != 0)
2153
958
            info_ptr->bit_depth = 8;
2154
5.60k
#     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
5.60k
   }
2180
2181
14.7k
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2182
14.7k
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
2183
7.89k
      info_ptr->color_type = (png_byte)(info_ptr->color_type |
2184
7.89k
         PNG_COLOR_MASK_COLOR);
2185
14.7k
#endif
2186
2187
14.7k
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2188
14.7k
   if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
2189
1.28k
      info_ptr->color_type = (png_byte)(info_ptr->color_type &
2190
1.28k
         ~PNG_COLOR_MASK_COLOR);
2191
14.7k
#endif
2192
2193
14.7k
#ifdef PNG_READ_QUANTIZE_SUPPORTED
2194
14.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
14.7k
#endif
2204
2205
14.7k
#ifdef PNG_READ_EXPAND_16_SUPPORTED
2206
14.7k
   if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
2207
2.99k
       info_ptr->bit_depth == 8 &&
2208
2.86k
       info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
2209
2.86k
   {
2210
2.86k
      info_ptr->bit_depth = 16;
2211
2.86k
   }
2212
14.7k
#endif
2213
2214
14.7k
#ifdef PNG_READ_PACK_SUPPORTED
2215
14.7k
   if ((png_ptr->transformations & PNG_PACK) != 0 &&
2216
1.97k
       (info_ptr->bit_depth < 8))
2217
320
      info_ptr->bit_depth = 8;
2218
14.7k
#endif
2219
2220
14.7k
   if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2221
470
      info_ptr->channels = 1;
2222
2223
14.2k
   else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
2224
11.9k
      info_ptr->channels = 3;
2225
2226
2.34k
   else
2227
2.34k
      info_ptr->channels = 1;
2228
2229
14.7k
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2230
14.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
14.7k
#endif
2237
2238
14.7k
   if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
2239
4.07k
      info_ptr->channels++;
2240
2241
14.7k
#ifdef PNG_READ_FILLER_SUPPORTED
2242
   /* STRIP_ALPHA and FILLER allowed:  MASK_ALPHA bit stripped above */
2243
14.7k
   if ((png_ptr->transformations & PNG_FILLER) != 0 &&
2244
3.11k
       (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2245
116
       info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
2246
3.11k
   {
2247
3.11k
      info_ptr->channels++;
2248
      /* If adding a true alpha channel not just filler */
2249
3.11k
      if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
2250
3.11k
         info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2251
3.11k
   }
2252
14.7k
#endif
2253
2254
14.7k
#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2255
14.7k
defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2256
14.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
14.7k
#endif
2265
2266
14.7k
   info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2267
14.7k
       info_ptr->bit_depth);
2268
2269
14.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
14.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
14.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
298k
{
2295
298k
   png_debug(1, "in png_do_unpack");
2296
2297
298k
   if (row_info->bit_depth < 8)
2298
2.91k
   {
2299
2.91k
      png_uint_32 i;
2300
2.91k
      png_uint_32 row_width=row_info->width;
2301
2302
2.91k
      switch (row_info->bit_depth)
2303
2.91k
      {
2304
658
         case 1:
2305
658
         {
2306
658
            png_bytep sp = row + (size_t)((row_width - 1) >> 3);
2307
658
            png_bytep dp = row + (size_t)row_width - 1;
2308
658
            png_uint_32 shift = 7U - ((row_width + 7U) & 0x07);
2309
8.91M
            for (i = 0; i < row_width; i++)
2310
8.91M
            {
2311
8.91M
               *dp = (png_byte)((*sp >> shift) & 0x01);
2312
2313
8.91M
               if (shift == 7)
2314
1.11M
               {
2315
1.11M
                  shift = 0;
2316
1.11M
                  sp--;
2317
1.11M
               }
2318
2319
7.79M
               else
2320
7.79M
                  shift++;
2321
2322
8.91M
               dp--;
2323
8.91M
            }
2324
658
            break;
2325
0
         }
2326
2327
1.14k
         case 2:
2328
1.14k
         {
2329
2330
1.14k
            png_bytep sp = row + (size_t)((row_width - 1) >> 2);
2331
1.14k
            png_bytep dp = row + (size_t)row_width - 1;
2332
1.14k
            png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1);
2333
634k
            for (i = 0; i < row_width; i++)
2334
633k
            {
2335
633k
               *dp = (png_byte)((*sp >> shift) & 0x03);
2336
2337
633k
               if (shift == 6)
2338
158k
               {
2339
158k
                  shift = 0;
2340
158k
                  sp--;
2341
158k
               }
2342
2343
474k
               else
2344
474k
                  shift += 2;
2345
2346
633k
               dp--;
2347
633k
            }
2348
1.14k
            break;
2349
0
         }
2350
2351
1.11k
         case 4:
2352
1.11k
         {
2353
1.11k
            png_bytep sp = row + (size_t)((row_width - 1) >> 1);
2354
1.11k
            png_bytep dp = row + (size_t)row_width - 1;
2355
1.11k
            png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2);
2356
1.25M
            for (i = 0; i < row_width; i++)
2357
1.25M
            {
2358
1.25M
               *dp = (png_byte)((*sp >> shift) & 0x0f);
2359
2360
1.25M
               if (shift == 4)
2361
626k
               {
2362
626k
                  shift = 0;
2363
626k
                  sp--;
2364
626k
               }
2365
2366
625k
               else
2367
625k
                  shift = 4;
2368
2369
1.25M
               dp--;
2370
1.25M
            }
2371
1.11k
            break;
2372
0
         }
2373
2374
0
         default:
2375
0
            break;
2376
2.91k
      }
2377
2.91k
      row_info->bit_depth = 8;
2378
2.91k
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2379
2.91k
      row_info->rowbytes = (size_t)row_width * row_info->channels;
2380
2.91k
   }
2381
298k
}
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.17k
{
2394
2.17k
   int color_type;
2395
2396
2.17k
   png_debug(1, "in png_do_unshift");
2397
2398
   /* The palette case has already been handled in the _init routine. */
2399
2.17k
   color_type = row_info->color_type;
2400
2401
2.17k
   if (color_type != PNG_COLOR_TYPE_PALETTE)
2402
2.17k
   {
2403
2.17k
      int shift[4];
2404
2.17k
      int channels = 0;
2405
2.17k
      int bit_depth = row_info->bit_depth;
2406
2407
2.17k
      if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
2408
1.19k
      {
2409
1.19k
         shift[channels++] = bit_depth - sig_bits->red;
2410
1.19k
         shift[channels++] = bit_depth - sig_bits->green;
2411
1.19k
         shift[channels++] = bit_depth - sig_bits->blue;
2412
1.19k
      }
2413
2414
978
      else
2415
978
      {
2416
978
         shift[channels++] = bit_depth - sig_bits->gray;
2417
978
      }
2418
2419
2.17k
      if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
2420
222
      {
2421
222
         shift[channels++] = bit_depth - sig_bits->alpha;
2422
222
      }
2423
2424
2.17k
      {
2425
2.17k
         int c, have_shift;
2426
2427
6.95k
         for (c = have_shift = 0; c < channels; ++c)
2428
4.77k
         {
2429
            /* A shift of more than the bit depth is an error condition but it
2430
             * gets ignored here.
2431
             */
2432
4.77k
            if (shift[c] <= 0 || shift[c] >= bit_depth)
2433
1.50k
               shift[c] = 0;
2434
2435
3.27k
            else
2436
3.27k
               have_shift = 1;
2437
4.77k
         }
2438
2439
2.17k
         if (have_shift == 0)
2440
576
            return;
2441
2.17k
      }
2442
2443
1.59k
      switch (bit_depth)
2444
1.59k
      {
2445
0
         default:
2446
         /* Must be 1bpp gray: should not be here! */
2447
            /* NOTREACHED */
2448
0
            break;
2449
2450
487
         case 2:
2451
         /* Must be 2bpp gray */
2452
         /* assert(channels == 1 && shift[0] == 1) */
2453
487
         {
2454
487
            png_bytep bp = row;
2455
487
            png_bytep bp_end = bp + row_info->rowbytes;
2456
2457
2.09k
            while (bp < bp_end)
2458
1.61k
            {
2459
1.61k
               int b = (*bp >> 1) & 0x55;
2460
1.61k
               *bp++ = (png_byte)b;
2461
1.61k
            }
2462
487
            break;
2463
0
         }
2464
2465
269
         case 4:
2466
         /* Must be 4bpp gray */
2467
         /* assert(channels == 1) */
2468
269
         {
2469
269
            png_bytep bp = row;
2470
269
            png_bytep bp_end = bp + row_info->rowbytes;
2471
269
            int gray_shift = shift[0];
2472
269
            int mask =  0xf >> gray_shift;
2473
2474
269
            mask |= mask << 4;
2475
2476
1.14k
            while (bp < bp_end)
2477
876
            {
2478
876
               int b = (*bp >> gray_shift) & mask;
2479
876
               *bp++ = (png_byte)b;
2480
876
            }
2481
269
            break;
2482
0
         }
2483
2484
494
         case 8:
2485
         /* Single byte components, G, GA, RGB, RGBA */
2486
494
         {
2487
494
            png_bytep bp = row;
2488
494
            png_bytep bp_end = bp + row_info->rowbytes;
2489
494
            int channel = 0;
2490
2491
732k
            while (bp < bp_end)
2492
732k
            {
2493
732k
               int b = *bp >> shift[channel];
2494
732k
               if (++channel >= channels)
2495
596k
                  channel = 0;
2496
732k
               *bp++ = (png_byte)b;
2497
732k
            }
2498
494
            break;
2499
0
         }
2500
2501
0
#ifdef PNG_READ_16BIT_SUPPORTED
2502
345
         case 16:
2503
         /* Double byte components, G, GA, RGB, RGBA */
2504
345
         {
2505
345
            png_bytep bp = row;
2506
345
            png_bytep bp_end = bp + row_info->rowbytes;
2507
345
            int channel = 0;
2508
2509
31.0k
            while (bp < bp_end)
2510
30.7k
            {
2511
30.7k
               int value = (bp[0] << 8) + bp[1];
2512
2513
30.7k
               value >>= shift[channel];
2514
30.7k
               if (++channel >= channels)
2515
10.2k
                  channel = 0;
2516
30.7k
               *bp++ = (png_byte)(value >> 8);
2517
30.7k
               *bp++ = (png_byte)value;
2518
30.7k
            }
2519
345
            break;
2520
0
         }
2521
1.59k
#endif
2522
1.59k
      }
2523
1.59k
   }
2524
2.17k
}
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
442k
{
2532
442k
   png_debug(1, "in png_do_scale_16_to_8");
2533
2534
442k
   if (row_info->bit_depth == 16)
2535
75.5k
   {
2536
75.5k
      png_bytep sp = row; /* source */
2537
75.5k
      png_bytep dp = row; /* destination */
2538
75.5k
      png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2539
2540
43.9M
      while (sp < ep)
2541
43.8M
      {
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
43.8M
         png_int_32 tmp = *sp++; /* must be signed! */
2575
43.8M
         tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
2576
43.8M
         *dp++ = (png_byte)tmp;
2577
43.8M
      }
2578
2579
75.5k
      row_info->bit_depth = 8;
2580
75.5k
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2581
75.5k
      row_info->rowbytes = (size_t)row_info->width * row_info->channels;
2582
75.5k
   }
2583
442k
}
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
19.5k
{
2593
19.5k
   png_debug(1, "in png_do_chop");
2594
2595
19.5k
   if (row_info->bit_depth == 16)
2596
1.61k
   {
2597
1.61k
      png_bytep sp = row; /* source */
2598
1.61k
      png_bytep dp = row; /* destination */
2599
1.61k
      png_bytep ep = sp + row_info->rowbytes; /* end+1 */
2600
2601
2.00M
      while (sp < ep)
2602
1.99M
      {
2603
1.99M
         *dp++ = *sp;
2604
1.99M
         sp += 2; /* skip low byte */
2605
1.99M
      }
2606
2607
1.61k
      row_info->bit_depth = 8;
2608
1.61k
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2609
1.61k
      row_info->rowbytes = (size_t)row_info->width * row_info->channels;
2610
1.61k
   }
2611
19.5k
}
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
23.1k
{
2618
23.1k
   png_uint_32 row_width = row_info->width;
2619
2620
23.1k
   png_debug(1, "in png_do_read_swap_alpha");
2621
2622
23.1k
   if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2623
2.88k
   {
2624
      /* This converts from RGBA to ARGB */
2625
2.88k
      if (row_info->bit_depth == 8)
2626
1.52k
      {
2627
1.52k
         png_bytep sp = row + row_info->rowbytes;
2628
1.52k
         png_bytep dp = sp;
2629
1.52k
         png_byte save;
2630
1.52k
         png_uint_32 i;
2631
2632
2.91M
         for (i = 0; i < row_width; i++)
2633
2.91M
         {
2634
2.91M
            save = *(--sp);
2635
2.91M
            *(--dp) = *(--sp);
2636
2.91M
            *(--dp) = *(--sp);
2637
2.91M
            *(--dp) = *(--sp);
2638
2.91M
            *(--dp) = save;
2639
2.91M
         }
2640
1.52k
      }
2641
2642
1.36k
#ifdef PNG_READ_16BIT_SUPPORTED
2643
      /* This converts from RRGGBBAA to AARRGGBB */
2644
1.36k
      else
2645
1.36k
      {
2646
1.36k
         png_bytep sp = row + row_info->rowbytes;
2647
1.36k
         png_bytep dp = sp;
2648
1.36k
         png_byte save[2];
2649
1.36k
         png_uint_32 i;
2650
2651
4.91M
         for (i = 0; i < row_width; i++)
2652
4.91M
         {
2653
4.91M
            save[0] = *(--sp);
2654
4.91M
            save[1] = *(--sp);
2655
4.91M
            *(--dp) = *(--sp);
2656
4.91M
            *(--dp) = *(--sp);
2657
4.91M
            *(--dp) = *(--sp);
2658
4.91M
            *(--dp) = *(--sp);
2659
4.91M
            *(--dp) = *(--sp);
2660
4.91M
            *(--dp) = *(--sp);
2661
4.91M
            *(--dp) = save[0];
2662
4.91M
            *(--dp) = save[1];
2663
4.91M
         }
2664
1.36k
      }
2665
2.88k
#endif
2666
2.88k
   }
2667
2668
20.2k
   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2669
1.67k
   {
2670
      /* This converts from GA to AG */
2671
1.67k
      if (row_info->bit_depth == 8)
2672
1.21k
      {
2673
1.21k
         png_bytep sp = row + row_info->rowbytes;
2674
1.21k
         png_bytep dp = sp;
2675
1.21k
         png_byte save;
2676
1.21k
         png_uint_32 i;
2677
2678
2.10M
         for (i = 0; i < row_width; i++)
2679
2.10M
         {
2680
2.10M
            save = *(--sp);
2681
2.10M
            *(--dp) = *(--sp);
2682
2.10M
            *(--dp) = save;
2683
2.10M
         }
2684
1.21k
      }
2685
2686
459
#ifdef PNG_READ_16BIT_SUPPORTED
2687
      /* This converts from GGAA to AAGG */
2688
459
      else
2689
459
      {
2690
459
         png_bytep sp = row + row_info->rowbytes;
2691
459
         png_bytep dp = sp;
2692
459
         png_byte save[2];
2693
459
         png_uint_32 i;
2694
2695
6.39k
         for (i = 0; i < row_width; i++)
2696
5.93k
         {
2697
5.93k
            save[0] = *(--sp);
2698
5.93k
            save[1] = *(--sp);
2699
5.93k
            *(--dp) = *(--sp);
2700
5.93k
            *(--dp) = *(--sp);
2701
5.93k
            *(--dp) = save[0];
2702
5.93k
            *(--dp) = save[1];
2703
5.93k
         }
2704
459
      }
2705
1.67k
#endif
2706
1.67k
   }
2707
23.1k
}
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.8k
{
2714
21.8k
   png_uint_32 row_width;
2715
21.8k
   png_debug(1, "in png_do_read_invert_alpha");
2716
2717
21.8k
   row_width = row_info->width;
2718
21.8k
   if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
2719
2.14k
   {
2720
2.14k
      if (row_info->bit_depth == 8)
2721
1.26k
      {
2722
         /* This inverts the alpha channel in RGBA */
2723
1.26k
         png_bytep sp = row + row_info->rowbytes;
2724
1.26k
         png_bytep dp = sp;
2725
1.26k
         png_uint_32 i;
2726
2727
1.54M
         for (i = 0; i < row_width; i++)
2728
1.54M
         {
2729
1.54M
            *(--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.54M
            sp-=3;
2738
1.54M
            dp=sp;
2739
1.54M
         }
2740
1.26k
      }
2741
2742
879
#ifdef PNG_READ_16BIT_SUPPORTED
2743
      /* This inverts the alpha channel in RRGGBBAA */
2744
879
      else
2745
879
      {
2746
879
         png_bytep sp = row + row_info->rowbytes;
2747
879
         png_bytep dp = sp;
2748
879
         png_uint_32 i;
2749
2750
1.94M
         for (i = 0; i < row_width; i++)
2751
1.93M
         {
2752
1.93M
            *(--dp) = (png_byte)(255 - *(--sp));
2753
1.93M
            *(--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.93M
            sp-=6;
2765
1.93M
            dp=sp;
2766
1.93M
         }
2767
879
      }
2768
2.14k
#endif
2769
2.14k
   }
2770
19.7k
   else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
2771
1.00k
   {
2772
1.00k
      if (row_info->bit_depth == 8)
2773
610
      {
2774
         /* This inverts the alpha channel in GA */
2775
610
         png_bytep sp = row + row_info->rowbytes;
2776
610
         png_bytep dp = sp;
2777
610
         png_uint_32 i;
2778
2779
1.87M
         for (i = 0; i < row_width; i++)
2780
1.87M
         {
2781
1.87M
            *(--dp) = (png_byte)(255 - *(--sp));
2782
1.87M
            *(--dp) = *(--sp);
2783
1.87M
         }
2784
610
      }
2785
2786
395
#ifdef PNG_READ_16BIT_SUPPORTED
2787
395
      else
2788
395
      {
2789
         /* This inverts the alpha channel in GGAA */
2790
395
         png_bytep sp  = row + row_info->rowbytes;
2791
395
         png_bytep dp = sp;
2792
395
         png_uint_32 i;
2793
2794
5.75k
         for (i = 0; i < row_width; i++)
2795
5.35k
         {
2796
5.35k
            *(--dp) = (png_byte)(255 - *(--sp));
2797
5.35k
            *(--dp) = (png_byte)(255 - *(--sp));
2798
/*
2799
            *(--dp) = *(--sp);
2800
            *(--dp) = *(--sp);
2801
*/
2802
5.35k
            sp-=2;
2803
5.35k
            dp=sp;
2804
5.35k
         }
2805
395
      }
2806
1.00k
#endif
2807
1.00k
   }
2808
21.8k
}
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
368k
{
2817
368k
   png_uint_32 i;
2818
368k
   png_uint_32 row_width = row_info->width;
2819
2820
368k
#ifdef PNG_READ_16BIT_SUPPORTED
2821
368k
   png_byte hi_filler = (png_byte)(filler>>8);
2822
368k
#endif
2823
368k
   png_byte lo_filler = (png_byte)filler;
2824
2825
368k
   png_debug(1, "in png_do_read_filler");
2826
2827
368k
   if (
2828
368k
       row_info->color_type == PNG_COLOR_TYPE_GRAY)
2829
2.16k
   {
2830
2.16k
      if (row_info->bit_depth == 8)
2831
882
      {
2832
882
         if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2833
476
         {
2834
            /* This changes the data from G to GX */
2835
476
            png_bytep sp = row + (size_t)row_width;
2836
476
            png_bytep dp =  sp + (size_t)row_width;
2837
330k
            for (i = 1; i < row_width; i++)
2838
330k
            {
2839
330k
               *(--dp) = lo_filler;
2840
330k
               *(--dp) = *(--sp);
2841
330k
            }
2842
476
            *(--dp) = lo_filler;
2843
476
            row_info->channels = 2;
2844
476
            row_info->pixel_depth = 16;
2845
476
            row_info->rowbytes = (size_t)row_width * 2;
2846
476
         }
2847
2848
406
         else
2849
406
         {
2850
            /* This changes the data from G to XG */
2851
406
            png_bytep sp = row + (size_t)row_width;
2852
406
            png_bytep dp = sp  + (size_t)row_width;
2853
1.08M
            for (i = 0; i < row_width; i++)
2854
1.08M
            {
2855
1.08M
               *(--dp) = *(--sp);
2856
1.08M
               *(--dp) = lo_filler;
2857
1.08M
            }
2858
406
            row_info->channels = 2;
2859
406
            row_info->pixel_depth = 16;
2860
406
            row_info->rowbytes = (size_t)row_width * 2;
2861
406
         }
2862
882
      }
2863
2864
1.28k
#ifdef PNG_READ_16BIT_SUPPORTED
2865
1.28k
      else if (row_info->bit_depth == 16)
2866
1.28k
      {
2867
1.28k
         if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2868
749
         {
2869
            /* This changes the data from GG to GGXX */
2870
749
            png_bytep sp = row + (size_t)row_width * 2;
2871
749
            png_bytep dp = sp  + (size_t)row_width * 2;
2872
3.54k
            for (i = 1; i < row_width; i++)
2873
2.80k
            {
2874
2.80k
               *(--dp) = lo_filler;
2875
2.80k
               *(--dp) = hi_filler;
2876
2.80k
               *(--dp) = *(--sp);
2877
2.80k
               *(--dp) = *(--sp);
2878
2.80k
            }
2879
749
            *(--dp) = lo_filler;
2880
749
            *(--dp) = hi_filler;
2881
749
            row_info->channels = 2;
2882
749
            row_info->pixel_depth = 32;
2883
749
            row_info->rowbytes = (size_t)row_width * 4;
2884
749
         }
2885
2886
533
         else
2887
533
         {
2888
            /* This changes the data from GG to XXGG */
2889
533
            png_bytep sp = row + (size_t)row_width * 2;
2890
533
            png_bytep dp = sp  + (size_t)row_width * 2;
2891
333k
            for (i = 0; i < row_width; i++)
2892
333k
            {
2893
333k
               *(--dp) = *(--sp);
2894
333k
               *(--dp) = *(--sp);
2895
333k
               *(--dp) = lo_filler;
2896
333k
               *(--dp) = hi_filler;
2897
333k
            }
2898
533
            row_info->channels = 2;
2899
533
            row_info->pixel_depth = 32;
2900
533
            row_info->rowbytes = (size_t)row_width * 4;
2901
533
         }
2902
1.28k
      }
2903
2.16k
#endif
2904
2.16k
   } /* COLOR_TYPE == GRAY */
2905
366k
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
2906
366k
   {
2907
366k
      if (row_info->bit_depth == 8)
2908
365k
      {
2909
365k
         if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2910
364k
         {
2911
            /* This changes the data from RGB to RGBX */
2912
364k
            png_bytep sp = row + (size_t)row_width * 3;
2913
364k
            png_bytep dp = sp  + (size_t)row_width;
2914
21.1M
            for (i = 1; i < row_width; i++)
2915
20.7M
            {
2916
20.7M
               *(--dp) = lo_filler;
2917
20.7M
               *(--dp) = *(--sp);
2918
20.7M
               *(--dp) = *(--sp);
2919
20.7M
               *(--dp) = *(--sp);
2920
20.7M
            }
2921
364k
            *(--dp) = lo_filler;
2922
364k
            row_info->channels = 4;
2923
364k
            row_info->pixel_depth = 32;
2924
364k
            row_info->rowbytes = (size_t)row_width * 4;
2925
364k
         }
2926
2927
387
         else
2928
387
         {
2929
            /* This changes the data from RGB to XRGB */
2930
387
            png_bytep sp = row + (size_t)row_width * 3;
2931
387
            png_bytep dp = sp + (size_t)row_width;
2932
694k
            for (i = 0; i < row_width; i++)
2933
694k
            {
2934
694k
               *(--dp) = *(--sp);
2935
694k
               *(--dp) = *(--sp);
2936
694k
               *(--dp) = *(--sp);
2937
694k
               *(--dp) = lo_filler;
2938
694k
            }
2939
387
            row_info->channels = 4;
2940
387
            row_info->pixel_depth = 32;
2941
387
            row_info->rowbytes = (size_t)row_width * 4;
2942
387
         }
2943
365k
      }
2944
2945
1.25k
#ifdef PNG_READ_16BIT_SUPPORTED
2946
1.25k
      else if (row_info->bit_depth == 16)
2947
1.25k
      {
2948
1.25k
         if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
2949
670
         {
2950
            /* This changes the data from RRGGBB to RRGGBBXX */
2951
670
            png_bytep sp = row + (size_t)row_width * 6;
2952
670
            png_bytep dp = sp  + (size_t)row_width * 2;
2953
2.20M
            for (i = 1; i < row_width; i++)
2954
2.20M
            {
2955
2.20M
               *(--dp) = lo_filler;
2956
2.20M
               *(--dp) = hi_filler;
2957
2.20M
               *(--dp) = *(--sp);
2958
2.20M
               *(--dp) = *(--sp);
2959
2.20M
               *(--dp) = *(--sp);
2960
2.20M
               *(--dp) = *(--sp);
2961
2.20M
               *(--dp) = *(--sp);
2962
2.20M
               *(--dp) = *(--sp);
2963
2.20M
            }
2964
670
            *(--dp) = lo_filler;
2965
670
            *(--dp) = hi_filler;
2966
670
            row_info->channels = 4;
2967
670
            row_info->pixel_depth = 64;
2968
670
            row_info->rowbytes = (size_t)row_width * 8;
2969
670
         }
2970
2971
581
         else
2972
581
         {
2973
            /* This changes the data from RRGGBB to XXRRGGBB */
2974
581
            png_bytep sp = row + (size_t)row_width * 6;
2975
581
            png_bytep dp = sp  + (size_t)row_width * 2;
2976
1.11M
            for (i = 0; i < row_width; i++)
2977
1.11M
            {
2978
1.11M
               *(--dp) = *(--sp);
2979
1.11M
               *(--dp) = *(--sp);
2980
1.11M
               *(--dp) = *(--sp);
2981
1.11M
               *(--dp) = *(--sp);
2982
1.11M
               *(--dp) = *(--sp);
2983
1.11M
               *(--dp) = *(--sp);
2984
1.11M
               *(--dp) = lo_filler;
2985
1.11M
               *(--dp) = hi_filler;
2986
1.11M
            }
2987
2988
581
            row_info->channels = 4;
2989
581
            row_info->pixel_depth = 64;
2990
581
            row_info->rowbytes = (size_t)row_width * 8;
2991
581
         }
2992
1.25k
      }
2993
366k
#endif
2994
366k
   } /* COLOR_TYPE == RGB */
2995
368k
}
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
749k
{
3003
749k
   png_uint_32 i;
3004
749k
   png_uint_32 row_width = row_info->width;
3005
3006
749k
   png_debug(1, "in png_do_gray_to_rgb");
3007
3008
749k
   if (row_info->bit_depth >= 8 &&
3009
749k
       (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
3010
707k
   {
3011
707k
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3012
697k
      {
3013
697k
         if (row_info->bit_depth == 8)
3014
642k
         {
3015
            /* This changes G to RGB */
3016
642k
            png_bytep sp = row + (size_t)row_width - 1;
3017
642k
            png_bytep dp = sp  + (size_t)row_width * 2;
3018
42.7M
            for (i = 0; i < row_width; i++)
3019
42.0M
            {
3020
42.0M
               *(dp--) = *sp;
3021
42.0M
               *(dp--) = *sp;
3022
42.0M
               *(dp--) = *(sp--);
3023
42.0M
            }
3024
642k
         }
3025
3026
54.8k
         else
3027
54.8k
         {
3028
            /* This changes GG to RRGGBB */
3029
54.8k
            png_bytep sp = row + (size_t)row_width * 2 - 1;
3030
54.8k
            png_bytep dp = sp  + (size_t)row_width * 4;
3031
11.6M
            for (i = 0; i < row_width; i++)
3032
11.5M
            {
3033
11.5M
               *(dp--) = *sp;
3034
11.5M
               *(dp--) = *(sp - 1);
3035
11.5M
               *(dp--) = *sp;
3036
11.5M
               *(dp--) = *(sp - 1);
3037
11.5M
               *(dp--) = *(sp--);
3038
11.5M
               *(dp--) = *(sp--);
3039
11.5M
            }
3040
54.8k
         }
3041
697k
      }
3042
3043
10.3k
      else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
3044
10.3k
      {
3045
10.3k
         if (row_info->bit_depth == 8)
3046
5.48k
         {
3047
            /* This changes GA to RGBA */
3048
5.48k
            png_bytep sp = row + (size_t)row_width * 2 - 1;
3049
5.48k
            png_bytep dp = sp  + (size_t)row_width * 2;
3050
11.0M
            for (i = 0; i < row_width; i++)
3051
11.0M
            {
3052
11.0M
               *(dp--) = *(sp--);
3053
11.0M
               *(dp--) = *sp;
3054
11.0M
               *(dp--) = *sp;
3055
11.0M
               *(dp--) = *(sp--);
3056
11.0M
            }
3057
5.48k
         }
3058
3059
4.83k
         else
3060
4.83k
         {
3061
            /* This changes GGAA to RRGGBBAA */
3062
4.83k
            png_bytep sp = row + (size_t)row_width * 4 - 1;
3063
4.83k
            png_bytep dp = sp  + (size_t)row_width * 4;
3064
4.80M
            for (i = 0; i < row_width; i++)
3065
4.79M
            {
3066
4.79M
               *(dp--) = *(sp--);
3067
4.79M
               *(dp--) = *(sp--);
3068
4.79M
               *(dp--) = *sp;
3069
4.79M
               *(dp--) = *(sp - 1);
3070
4.79M
               *(dp--) = *sp;
3071
4.79M
               *(dp--) = *(sp - 1);
3072
4.79M
               *(dp--) = *(sp--);
3073
4.79M
               *(dp--) = *(sp--);
3074
4.79M
            }
3075
4.83k
         }
3076
10.3k
      }
3077
707k
      row_info->channels = (png_byte)(row_info->channels + 2);
3078
707k
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
3079
707k
      row_info->pixel_depth = (png_byte)(row_info->channels *
3080
707k
          row_info->bit_depth);
3081
707k
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3082
707k
   }
3083
749k
}
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
5.30k
{
3142
5.30k
   int rgb_error = 0;
3143
3144
5.30k
   png_debug(1, "in png_do_rgb_to_gray");
3145
3146
5.30k
   if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
3147
5.30k
       (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
3148
5.30k
   {
3149
5.30k
      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
3150
5.30k
      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
3151
5.30k
      png_uint_32 bc = 32768 - rc - gc;
3152
5.30k
      png_uint_32 row_width = row_info->width;
3153
5.30k
      int have_alpha = (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
3154
3155
5.30k
      if (row_info->bit_depth == 8)
3156
2.75k
      {
3157
2.75k
#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.75k
         if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3164
2.22k
         {
3165
2.22k
            png_bytep sp = row;
3166
2.22k
            png_bytep dp = row;
3167
2.22k
            png_uint_32 i;
3168
3169
2.38M
            for (i = 0; i < row_width; i++)
3170
2.38M
            {
3171
2.38M
               png_byte red   = *(sp++);
3172
2.38M
               png_byte green = *(sp++);
3173
2.38M
               png_byte blue  = *(sp++);
3174
3175
2.38M
               if (red != green || red != blue)
3176
1.20M
               {
3177
1.20M
                  red = png_ptr->gamma_to_1[red];
3178
1.20M
                  green = png_ptr->gamma_to_1[green];
3179
1.20M
                  blue = png_ptr->gamma_to_1[blue];
3180
3181
1.20M
                  rgb_error |= 1;
3182
1.20M
                  *(dp++) = png_ptr->gamma_from_1[
3183
1.20M
                      (rc*red + gc*green + bc*blue + 16384)>>15];
3184
1.20M
               }
3185
3186
1.18M
               else
3187
1.18M
               {
3188
                  /* If there is no overall correction the table will not be
3189
                   * set.
3190
                   */
3191
1.18M
                  if (png_ptr->gamma_table != NULL)
3192
1.18M
                     red = png_ptr->gamma_table[red];
3193
3194
1.18M
                  *(dp++) = red;
3195
1.18M
               }
3196
3197
2.38M
               if (have_alpha != 0)
3198
1.29M
                  *(dp++) = *(sp++);
3199
2.38M
            }
3200
2.22k
         }
3201
526
         else
3202
526
#endif
3203
526
         {
3204
526
            png_bytep sp = row;
3205
526
            png_bytep dp = row;
3206
526
            png_uint_32 i;
3207
3208
4.30k
            for (i = 0; i < row_width; i++)
3209
3.78k
            {
3210
3.78k
               png_byte red   = *(sp++);
3211
3.78k
               png_byte green = *(sp++);
3212
3.78k
               png_byte blue  = *(sp++);
3213
3214
3.78k
               if (red != green || red != blue)
3215
940
               {
3216
940
                  rgb_error |= 1;
3217
                  /* NOTE: this is the historical approach which simply
3218
                   * truncates the results.
3219
                   */
3220
940
                  *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3221
940
               }
3222
3223
2.84k
               else
3224
2.84k
                  *(dp++) = red;
3225
3226
3.78k
               if (have_alpha != 0)
3227
753
                  *(dp++) = *(sp++);
3228
3.78k
            }
3229
526
         }
3230
2.75k
      }
3231
3232
2.55k
      else /* RGB bit_depth == 16 */
3233
2.55k
      {
3234
2.55k
#ifdef PNG_READ_GAMMA_SUPPORTED
3235
2.55k
         if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
3236
1.49k
         {
3237
1.49k
            png_bytep sp = row;
3238
1.49k
            png_bytep dp = row;
3239
1.49k
            png_uint_32 i;
3240
3241
13.5k
            for (i = 0; i < row_width; i++)
3242
12.0k
            {
3243
12.0k
               png_uint_16 red, green, blue, w;
3244
12.0k
               png_byte hi,lo;
3245
3246
12.0k
               hi=*(sp)++; lo=*(sp)++; red   = (png_uint_16)((hi << 8) | (lo));
3247
12.0k
               hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3248
12.0k
               hi=*(sp)++; lo=*(sp)++; blue  = (png_uint_16)((hi << 8) | (lo));
3249
3250
12.0k
               if (red == green && red == blue)
3251
3.01k
               {
3252
3.01k
                  if (png_ptr->gamma_16_table != NULL)
3253
3.01k
                     w = png_ptr->gamma_16_table[(red & 0xff)
3254
3.01k
                         >> png_ptr->gamma_shift][red >> 8];
3255
3256
0
                  else
3257
0
                     w = red;
3258
3.01k
               }
3259
3260
9.05k
               else
3261
9.05k
               {
3262
9.05k
                  png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red & 0xff)
3263
9.05k
                      >> png_ptr->gamma_shift][red>>8];
3264
9.05k
                  png_uint_16 green_1 =
3265
9.05k
                      png_ptr->gamma_16_to_1[(green & 0xff) >>
3266
9.05k
                      png_ptr->gamma_shift][green>>8];
3267
9.05k
                  png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue & 0xff)
3268
9.05k
                      >> png_ptr->gamma_shift][blue>>8];
3269
9.05k
                  png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
3270
9.05k
                      + bc*blue_1 + 16384)>>15);
3271
9.05k
                  w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >>
3272
9.05k
                      png_ptr->gamma_shift][gray16 >> 8];
3273
9.05k
                  rgb_error |= 1;
3274
9.05k
               }
3275
3276
12.0k
               *(dp++) = (png_byte)((w>>8) & 0xff);
3277
12.0k
               *(dp++) = (png_byte)(w & 0xff);
3278
3279
12.0k
               if (have_alpha != 0)
3280
10.9k
               {
3281
10.9k
                  *(dp++) = *(sp++);
3282
10.9k
                  *(dp++) = *(sp++);
3283
10.9k
               }
3284
12.0k
            }
3285
1.49k
         }
3286
1.06k
         else
3287
1.06k
#endif
3288
1.06k
         {
3289
1.06k
            png_bytep sp = row;
3290
1.06k
            png_bytep dp = row;
3291
1.06k
            png_uint_32 i;
3292
3293
155k
            for (i = 0; i < row_width; i++)
3294
154k
            {
3295
154k
               png_uint_16 red, green, blue, gray16;
3296
154k
               png_byte hi,lo;
3297
3298
154k
               hi=*(sp)++; lo=*(sp)++; red   = (png_uint_16)((hi << 8) | (lo));
3299
154k
               hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
3300
154k
               hi=*(sp)++; lo=*(sp)++; blue  = (png_uint_16)((hi << 8) | (lo));
3301
3302
154k
               if (red != green || red != blue)
3303
151k
                  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
154k
               gray16  = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
3310
154k
                  15);
3311
154k
               *(dp++) = (png_byte)((gray16 >> 8) & 0xff);
3312
154k
               *(dp++) = (png_byte)(gray16 & 0xff);
3313
3314
154k
               if (have_alpha != 0)
3315
152k
               {
3316
152k
                  *(dp++) = *(sp++);
3317
152k
                  *(dp++) = *(sp++);
3318
152k
               }
3319
154k
            }
3320
1.06k
         }
3321
2.55k
      }
3322
3323
5.30k
      row_info->channels = (png_byte)(row_info->channels - 2);
3324
5.30k
      row_info->color_type = (png_byte)(row_info->color_type &
3325
5.30k
          ~PNG_COLOR_MASK_COLOR);
3326
5.30k
      row_info->pixel_depth = (png_byte)(row_info->channels *
3327
5.30k
          row_info->bit_depth);
3328
5.30k
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3329
5.30k
   }
3330
5.30k
   return rgb_error;
3331
5.30k
}
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.65k
{
3343
6.65k
#ifdef PNG_READ_GAMMA_SUPPORTED
3344
6.65k
   png_const_bytep gamma_table = png_ptr->gamma_table;
3345
6.65k
   png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
3346
6.65k
   png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
3347
6.65k
   png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
3348
6.65k
   png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
3349
6.65k
   png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
3350
6.65k
   int gamma_shift = png_ptr->gamma_shift;
3351
6.65k
   int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
3352
6.65k
#endif
3353
3354
6.65k
   png_bytep sp;
3355
6.65k
   png_uint_32 i;
3356
6.65k
   png_uint_32 row_width = row_info->width;
3357
6.65k
   int shift;
3358
3359
6.65k
   png_debug(1, "in png_do_compose");
3360
3361
6.65k
   switch (row_info->color_type)
3362
6.65k
   {
3363
315
      case PNG_COLOR_TYPE_GRAY:
3364
315
      {
3365
315
         switch (row_info->bit_depth)
3366
315
         {
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
315
            case 16:
3559
315
            {
3560
315
#ifdef PNG_READ_GAMMA_SUPPORTED
3561
315
               if (gamma_16 != NULL)
3562
315
               {
3563
315
                  sp = row;
3564
795k
                  for (i = 0; i < row_width; i++, sp += 2)
3565
795k
                  {
3566
795k
                     png_uint_16 v;
3567
3568
795k
                     v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3569
3570
795k
                     if (v == png_ptr->trans_color.gray)
3571
22.1k
                     {
3572
                        /* Background is already in screen gamma */
3573
22.1k
                        *sp = (png_byte)((png_ptr->background.gray >> 8)
3574
22.1k
                             & 0xff);
3575
22.1k
                        *(sp + 1) = (png_byte)(png_ptr->background.gray
3576
22.1k
                             & 0xff);
3577
22.1k
                     }
3578
3579
773k
                     else
3580
773k
                     {
3581
773k
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3582
773k
                        *sp = (png_byte)((v >> 8) & 0xff);
3583
773k
                        *(sp + 1) = (png_byte)(v & 0xff);
3584
773k
                     }
3585
795k
                  }
3586
315
               }
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
315
               break;
3607
0
            }
3608
3609
0
            default:
3610
0
               break;
3611
315
         }
3612
315
         break;
3613
315
      }
3614
3615
1.08k
      case PNG_COLOR_TYPE_RGB:
3616
1.08k
      {
3617
1.08k
         if (row_info->bit_depth == 8)
3618
469
         {
3619
469
#ifdef PNG_READ_GAMMA_SUPPORTED
3620
469
            if (gamma_table != NULL)
3621
469
            {
3622
469
               sp = row;
3623
2.31k
               for (i = 0; i < row_width; i++, sp += 3)
3624
1.84k
               {
3625
1.84k
                  if (*sp == png_ptr->trans_color.red &&
3626
950
                      *(sp + 1) == png_ptr->trans_color.green &&
3627
540
                      *(sp + 2) == png_ptr->trans_color.blue)
3628
269
                  {
3629
269
                     *sp = (png_byte)png_ptr->background.red;
3630
269
                     *(sp + 1) = (png_byte)png_ptr->background.green;
3631
269
                     *(sp + 2) = (png_byte)png_ptr->background.blue;
3632
269
                  }
3633
3634
1.57k
                  else
3635
1.57k
                  {
3636
1.57k
                     *sp = gamma_table[*sp];
3637
1.57k
                     *(sp + 1) = gamma_table[*(sp + 1)];
3638
1.57k
                     *(sp + 2) = gamma_table[*(sp + 2)];
3639
1.57k
                  }
3640
1.84k
               }
3641
469
            }
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
469
         }
3659
611
         else /* if (row_info->bit_depth == 16) */
3660
611
         {
3661
611
#ifdef PNG_READ_GAMMA_SUPPORTED
3662
611
            if (gamma_16 != NULL)
3663
611
            {
3664
611
               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
503
                      b == png_ptr->trans_color.blue)
3678
269
                  {
3679
                     /* Background is already in screen gamma */
3680
269
                     *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3681
269
                     *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3682
269
                     *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3683
269
                             & 0xff);
3684
269
                     *(sp + 3) = (png_byte)(png_ptr->background.green
3685
269
                             & 0xff);
3686
269
                     *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3687
269
                             & 0xff);
3688
269
                     *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3689
269
                  }
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
611
            }
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
611
         }
3739
1.08k
         break;
3740
315
      }
3741
3742
3.34k
      case PNG_COLOR_TYPE_GRAY_ALPHA:
3743
3.34k
      {
3744
3.34k
         if (row_info->bit_depth == 8)
3745
1.93k
         {
3746
1.93k
#ifdef PNG_READ_GAMMA_SUPPORTED
3747
1.93k
            if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3748
1.51k
                gamma_table != NULL)
3749
1.51k
            {
3750
1.51k
               sp = row;
3751
1.19M
               for (i = 0; i < row_width; i++, sp += 2)
3752
1.19M
               {
3753
1.19M
                  png_uint_16 a = *(sp + 1);
3754
3755
1.19M
                  if (a == 0xff)
3756
598k
                     *sp = gamma_table[*sp];
3757
3758
592k
                  else if (a == 0)
3759
590k
                  {
3760
                     /* Background is already in screen gamma */
3761
590k
                     *sp = (png_byte)png_ptr->background.gray;
3762
590k
                  }
3763
3764
1.92k
                  else
3765
1.92k
                  {
3766
1.92k
                     png_byte v, w;
3767
3768
1.92k
                     v = gamma_to_1[*sp];
3769
1.92k
                     png_composite(w, v, a, png_ptr->background_1.gray);
3770
1.92k
                     if (optimize == 0)
3771
1.70k
                        w = gamma_from_1[w];
3772
1.92k
                     *sp = w;
3773
1.92k
                  }
3774
1.19M
               }
3775
1.51k
            }
3776
420
            else
3777
420
#endif
3778
420
            {
3779
420
               sp = row;
3780
1.28M
               for (i = 0; i < row_width; i++, sp += 2)
3781
1.28M
               {
3782
1.28M
                  png_byte a = *(sp + 1);
3783
3784
1.28M
                  if (a == 0)
3785
661k
                     *sp = (png_byte)png_ptr->background.gray;
3786
3787
619k
                  else if (a < 0xff)
3788
932
                     png_composite(*sp, *sp, a, png_ptr->background.gray);
3789
1.28M
               }
3790
420
            }
3791
1.93k
         }
3792
1.40k
         else /* if (png_ptr->bit_depth == 16) */
3793
1.40k
         {
3794
1.40k
#ifdef PNG_READ_GAMMA_SUPPORTED
3795
1.40k
            if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3796
400
                gamma_16_to_1 != NULL)
3797
400
            {
3798
400
               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
236
                  {
3806
236
                     png_uint_16 v;
3807
3808
236
                     v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3809
236
                     *sp = (png_byte)((v >> 8) & 0xff);
3810
236
                     *(sp + 1) = (png_byte)(v & 0xff);
3811
236
                  }
3812
3813
84.4k
                  else if (a == 0)
3814
15.3k
                  {
3815
                     /* Background is already in screen gamma */
3816
15.3k
                     *sp = (png_byte)((png_ptr->background.gray >> 8)
3817
15.3k
                             & 0xff);
3818
15.3k
                     *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3819
15.3k
                  }
3820
3821
69.0k
                  else
3822
69.0k
                  {
3823
69.0k
                     png_uint_16 g, v, w;
3824
3825
69.0k
                     g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3826
69.0k
                     png_composite_16(v, g, a, png_ptr->background_1.gray);
3827
69.0k
                     if (optimize != 0)
3828
311
                        w = v;
3829
68.7k
                     else
3830
68.7k
                        w = gamma_16_from_1[(v & 0xff) >>
3831
68.7k
                            gamma_shift][v >> 8];
3832
69.0k
                     *sp = (png_byte)((w >> 8) & 0xff);
3833
69.0k
                     *(sp + 1) = (png_byte)(w & 0xff);
3834
69.0k
                  }
3835
84.6k
               }
3836
400
            }
3837
1.00k
            else
3838
1.00k
#endif
3839
1.00k
            {
3840
1.00k
               sp = row;
3841
156k
               for (i = 0; i < row_width; i++, sp += 4)
3842
155k
               {
3843
155k
                  png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3844
155k
                      + *(sp + 3));
3845
3846
155k
                  if (a == 0)
3847
1.37k
                  {
3848
1.37k
                     *sp = (png_byte)((png_ptr->background.gray >> 8)
3849
1.37k
                             & 0xff);
3850
1.37k
                     *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3851
1.37k
                  }
3852
3853
153k
                  else if (a < 0xffff)
3854
17.1k
                  {
3855
17.1k
                     png_uint_16 g, v;
3856
3857
17.1k
                     g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3858
17.1k
                     png_composite_16(v, g, a, png_ptr->background.gray);
3859
17.1k
                     *sp = (png_byte)((v >> 8) & 0xff);
3860
17.1k
                     *(sp + 1) = (png_byte)(v & 0xff);
3861
17.1k
                  }
3862
155k
               }
3863
1.00k
            }
3864
1.40k
         }
3865
3.34k
         break;
3866
315
      }
3867
3868
1.92k
      case PNG_COLOR_TYPE_RGB_ALPHA:
3869
1.92k
      {
3870
1.92k
         if (row_info->bit_depth == 8)
3871
815
         {
3872
815
#ifdef PNG_READ_GAMMA_SUPPORTED
3873
815
            if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3874
410
                gamma_table != NULL)
3875
410
            {
3876
410
               sp = row;
3877
1.59M
               for (i = 0; i < row_width; i++, sp += 4)
3878
1.59M
               {
3879
1.59M
                  png_byte a = *(sp + 3);
3880
3881
1.59M
                  if (a == 0xff)
3882
873k
                  {
3883
873k
                     *sp = gamma_table[*sp];
3884
873k
                     *(sp + 1) = gamma_table[*(sp + 1)];
3885
873k
                     *(sp + 2) = gamma_table[*(sp + 2)];
3886
873k
                  }
3887
3888
723k
                  else if (a == 0)
3889
718k
                  {
3890
                     /* Background is already in screen gamma */
3891
718k
                     *sp = (png_byte)png_ptr->background.red;
3892
718k
                     *(sp + 1) = (png_byte)png_ptr->background.green;
3893
718k
                     *(sp + 2) = (png_byte)png_ptr->background.blue;
3894
718k
                  }
3895
3896
4.96k
                  else
3897
4.96k
                  {
3898
4.96k
                     png_byte v, w;
3899
3900
4.96k
                     v = gamma_to_1[*sp];
3901
4.96k
                     png_composite(w, v, a, png_ptr->background_1.red);
3902
4.96k
                     if (optimize == 0) w = gamma_from_1[w];
3903
4.96k
                     *sp = w;
3904
3905
4.96k
                     v = gamma_to_1[*(sp + 1)];
3906
4.96k
                     png_composite(w, v, a, png_ptr->background_1.green);
3907
4.96k
                     if (optimize == 0) w = gamma_from_1[w];
3908
4.96k
                     *(sp + 1) = w;
3909
3910
4.96k
                     v = gamma_to_1[*(sp + 2)];
3911
4.96k
                     png_composite(w, v, a, png_ptr->background_1.blue);
3912
4.96k
                     if (optimize == 0) w = gamma_from_1[w];
3913
4.96k
                     *(sp + 2) = w;
3914
4.96k
                  }
3915
1.59M
               }
3916
410
            }
3917
405
            else
3918
405
#endif
3919
405
            {
3920
405
               sp = row;
3921
112k
               for (i = 0; i < row_width; i++, sp += 4)
3922
112k
               {
3923
112k
                  png_byte a = *(sp + 3);
3924
3925
112k
                  if (a == 0)
3926
1.23k
                  {
3927
1.23k
                     *sp = (png_byte)png_ptr->background.red;
3928
1.23k
                     *(sp + 1) = (png_byte)png_ptr->background.green;
3929
1.23k
                     *(sp + 2) = (png_byte)png_ptr->background.blue;
3930
1.23k
                  }
3931
3932
111k
                  else if (a < 0xff)
3933
110k
                  {
3934
110k
                     png_composite(*sp, *sp, a, png_ptr->background.red);
3935
3936
110k
                     png_composite(*(sp + 1), *(sp + 1), a,
3937
110k
                         png_ptr->background.green);
3938
3939
110k
                     png_composite(*(sp + 2), *(sp + 2), a,
3940
110k
                         png_ptr->background.blue);
3941
110k
                  }
3942
112k
               }
3943
405
            }
3944
815
         }
3945
1.10k
         else /* if (row_info->bit_depth == 16) */
3946
1.10k
         {
3947
1.10k
#ifdef PNG_READ_GAMMA_SUPPORTED
3948
1.10k
            if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3949
415
                gamma_16_to_1 != NULL)
3950
415
            {
3951
415
               sp = row;
3952
7.61k
               for (i = 0; i < row_width; i++, sp += 8)
3953
7.19k
               {
3954
7.19k
                  png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3955
7.19k
                      << 8) + (png_uint_16)(*(sp + 7)));
3956
3957
7.19k
                  if (a == (png_uint_16)0xffff)
3958
236
                  {
3959
236
                     png_uint_16 v;
3960
3961
236
                     v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3962
236
                     *sp = (png_byte)((v >> 8) & 0xff);
3963
236
                     *(sp + 1) = (png_byte)(v & 0xff);
3964
3965
236
                     v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
3966
236
                     *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3967
236
                     *(sp + 3) = (png_byte)(v & 0xff);
3968
3969
236
                     v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
3970
236
                     *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3971
236
                     *(sp + 5) = (png_byte)(v & 0xff);
3972
236
                  }
3973
3974
6.96k
                  else if (a == 0)
3975
245
                  {
3976
                     /* Background is already in screen gamma */
3977
245
                     *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3978
245
                     *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3979
245
                     *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3980
245
                             & 0xff);
3981
245
                     *(sp + 3) = (png_byte)(png_ptr->background.green
3982
245
                             & 0xff);
3983
245
                     *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3984
245
                             & 0xff);
3985
245
                     *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3986
245
                  }
3987
3988
6.71k
                  else
3989
6.71k
                  {
3990
6.71k
                     png_uint_16 v, w;
3991
3992
6.71k
                     v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3993
6.71k
                     png_composite_16(w, v, a, png_ptr->background_1.red);
3994
6.71k
                     if (optimize == 0)
3995
4.59k
                        w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
3996
4.59k
                             8];
3997
6.71k
                     *sp = (png_byte)((w >> 8) & 0xff);
3998
6.71k
                     *(sp + 1) = (png_byte)(w & 0xff);
3999
4000
6.71k
                     v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
4001
6.71k
                     png_composite_16(w, v, a, png_ptr->background_1.green);
4002
6.71k
                     if (optimize == 0)
4003
4.59k
                        w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
4004
4.59k
                             8];
4005
4006
6.71k
                     *(sp + 2) = (png_byte)((w >> 8) & 0xff);
4007
6.71k
                     *(sp + 3) = (png_byte)(w & 0xff);
4008
4009
6.71k
                     v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
4010
6.71k
                     png_composite_16(w, v, a, png_ptr->background_1.blue);
4011
6.71k
                     if (optimize == 0)
4012
4.59k
                        w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
4013
4.59k
                             8];
4014
4015
6.71k
                     *(sp + 4) = (png_byte)((w >> 8) & 0xff);
4016
6.71k
                     *(sp + 5) = (png_byte)(w & 0xff);
4017
6.71k
                  }
4018
7.19k
               }
4019
415
            }
4020
4021
691
            else
4022
691
#endif
4023
691
            {
4024
691
               sp = row;
4025
361k
               for (i = 0; i < row_width; i++, sp += 8)
4026
360k
               {
4027
360k
                  png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
4028
360k
                      << 8) + (png_uint_16)(*(sp + 7)));
4029
4030
360k
                  if (a == 0)
4031
1.17k
                  {
4032
1.17k
                     *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
4033
1.17k
                     *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
4034
1.17k
                     *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
4035
1.17k
                             & 0xff);
4036
1.17k
                     *(sp + 3) = (png_byte)(png_ptr->background.green
4037
1.17k
                             & 0xff);
4038
1.17k
                     *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
4039
1.17k
                             & 0xff);
4040
1.17k
                     *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
4041
1.17k
                  }
4042
4043
359k
                  else if (a < 0xffff)
4044
91.1k
                  {
4045
91.1k
                     png_uint_16 v;
4046
4047
91.1k
                     png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
4048
91.1k
                     png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
4049
91.1k
                         + *(sp + 3));
4050
91.1k
                     png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
4051
91.1k
                         + *(sp + 5));
4052
4053
91.1k
                     png_composite_16(v, r, a, png_ptr->background.red);
4054
91.1k
                     *sp = (png_byte)((v >> 8) & 0xff);
4055
91.1k
                     *(sp + 1) = (png_byte)(v & 0xff);
4056
4057
91.1k
                     png_composite_16(v, g, a, png_ptr->background.green);
4058
91.1k
                     *(sp + 2) = (png_byte)((v >> 8) & 0xff);
4059
91.1k
                     *(sp + 3) = (png_byte)(v & 0xff);
4060
4061
91.1k
                     png_composite_16(v, b, a, png_ptr->background.blue);
4062
91.1k
                     *(sp + 4) = (png_byte)((v >> 8) & 0xff);
4063
91.1k
                     *(sp + 5) = (png_byte)(v & 0xff);
4064
91.1k
                  }
4065
360k
               }
4066
691
            }
4067
1.10k
         }
4068
1.92k
         break;
4069
315
      }
4070
4071
0
      default:
4072
0
         break;
4073
6.65k
   }
4074
6.65k
}
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
35.4k
{
4087
35.4k
   png_const_bytep gamma_table = png_ptr->gamma_table;
4088
35.4k
   png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
4089
35.4k
   int gamma_shift = png_ptr->gamma_shift;
4090
4091
35.4k
   png_bytep sp;
4092
35.4k
   png_uint_32 i;
4093
35.4k
   png_uint_32 row_width=row_info->width;
4094
4095
35.4k
   png_debug(1, "in png_do_gamma");
4096
4097
35.4k
   if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
4098
31.0k
       (row_info->bit_depth == 16 && gamma_16_table != NULL)))
4099
35.4k
   {
4100
35.4k
      switch (row_info->color_type)
4101
35.4k
      {
4102
27.1k
         case PNG_COLOR_TYPE_RGB:
4103
27.1k
         {
4104
27.1k
            if (row_info->bit_depth == 8)
4105
1.90k
            {
4106
1.90k
               sp = row;
4107
1.32M
               for (i = 0; i < row_width; i++)
4108
1.32M
               {
4109
1.32M
                  *sp = gamma_table[*sp];
4110
1.32M
                  sp++;
4111
1.32M
                  *sp = gamma_table[*sp];
4112
1.32M
                  sp++;
4113
1.32M
                  *sp = gamma_table[*sp];
4114
1.32M
                  sp++;
4115
1.32M
               }
4116
1.90k
            }
4117
4118
25.2k
            else /* if (row_info->bit_depth == 16) */
4119
25.2k
            {
4120
25.2k
               sp = row;
4121
2.84M
               for (i = 0; i < row_width; i++)
4122
2.81M
               {
4123
2.81M
                  png_uint_16 v;
4124
4125
2.81M
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4126
2.81M
                  *sp = (png_byte)((v >> 8) & 0xff);
4127
2.81M
                  *(sp + 1) = (png_byte)(v & 0xff);
4128
2.81M
                  sp += 2;
4129
4130
2.81M
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4131
2.81M
                  *sp = (png_byte)((v >> 8) & 0xff);
4132
2.81M
                  *(sp + 1) = (png_byte)(v & 0xff);
4133
2.81M
                  sp += 2;
4134
4135
2.81M
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4136
2.81M
                  *sp = (png_byte)((v >> 8) & 0xff);
4137
2.81M
                  *(sp + 1) = (png_byte)(v & 0xff);
4138
2.81M
                  sp += 2;
4139
2.81M
               }
4140
25.2k
            }
4141
27.1k
            break;
4142
0
         }
4143
4144
5.32k
         case PNG_COLOR_TYPE_RGB_ALPHA:
4145
5.32k
         {
4146
5.32k
            if (row_info->bit_depth == 8)
4147
567
            {
4148
567
               sp = row;
4149
3.15M
               for (i = 0; i < row_width; i++)
4150
3.14M
               {
4151
3.14M
                  *sp = gamma_table[*sp];
4152
3.14M
                  sp++;
4153
4154
3.14M
                  *sp = gamma_table[*sp];
4155
3.14M
                  sp++;
4156
4157
3.14M
                  *sp = gamma_table[*sp];
4158
3.14M
                  sp++;
4159
4160
3.14M
                  sp++;
4161
3.14M
               }
4162
567
            }
4163
4164
4.75k
            else /* if (row_info->bit_depth == 16) */
4165
4.75k
            {
4166
4.75k
               sp = row;
4167
2.04M
               for (i = 0; i < row_width; i++)
4168
2.03M
               {
4169
2.03M
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4170
2.03M
                  *sp = (png_byte)((v >> 8) & 0xff);
4171
2.03M
                  *(sp + 1) = (png_byte)(v & 0xff);
4172
2.03M
                  sp += 2;
4173
4174
2.03M
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4175
2.03M
                  *sp = (png_byte)((v >> 8) & 0xff);
4176
2.03M
                  *(sp + 1) = (png_byte)(v & 0xff);
4177
2.03M
                  sp += 2;
4178
4179
2.03M
                  v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4180
2.03M
                  *sp = (png_byte)((v >> 8) & 0xff);
4181
2.03M
                  *(sp + 1) = (png_byte)(v & 0xff);
4182
2.03M
                  sp += 4;
4183
2.03M
               }
4184
4.75k
            }
4185
5.32k
            break;
4186
0
         }
4187
4188
849
         case PNG_COLOR_TYPE_GRAY_ALPHA:
4189
849
         {
4190
849
            if (row_info->bit_depth == 8)
4191
239
            {
4192
239
               sp = row;
4193
768
               for (i = 0; i < row_width; i++)
4194
529
               {
4195
529
                  *sp = gamma_table[*sp];
4196
529
                  sp += 2;
4197
529
               }
4198
239
            }
4199
4200
610
            else /* if (row_info->bit_depth == 16) */
4201
610
            {
4202
610
               sp = row;
4203
836k
               for (i = 0; i < row_width; i++)
4204
836k
               {
4205
836k
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4206
836k
                  *sp = (png_byte)((v >> 8) & 0xff);
4207
836k
                  *(sp + 1) = (png_byte)(v & 0xff);
4208
836k
                  sp += 4;
4209
836k
               }
4210
610
            }
4211
849
            break;
4212
0
         }
4213
4214
2.18k
         case PNG_COLOR_TYPE_GRAY:
4215
2.18k
         {
4216
2.18k
            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.18k
            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.18k
            else if (row_info->bit_depth == 8)
4250
1.71k
            {
4251
1.71k
               sp = row;
4252
2.98M
               for (i = 0; i < row_width; i++)
4253
2.98M
               {
4254
2.98M
                  *sp = gamma_table[*sp];
4255
2.98M
                  sp++;
4256
2.98M
               }
4257
1.71k
            }
4258
4259
478
            else if (row_info->bit_depth == 16)
4260
478
            {
4261
478
               sp = row;
4262
1.46M
               for (i = 0; i < row_width; i++)
4263
1.46M
               {
4264
1.46M
                  png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
4265
1.46M
                  *sp = (png_byte)((v >> 8) & 0xff);
4266
1.46M
                  *(sp + 1) = (png_byte)(v & 0xff);
4267
1.46M
                  sp += 2;
4268
1.46M
               }
4269
478
            }
4270
2.18k
            break;
4271
0
         }
4272
4273
0
         default:
4274
0
            break;
4275
35.4k
      }
4276
35.4k
   }
4277
35.4k
}
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
6.76k
{
4354
6.76k
   int shift, value;
4355
6.76k
   png_bytep sp, dp;
4356
6.76k
   png_uint_32 i;
4357
6.76k
   png_uint_32 row_width=row_info->width;
4358
4359
6.76k
   png_debug(1, "in png_do_expand_palette");
4360
4361
6.76k
   if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4362
6.76k
   {
4363
6.76k
      if (row_info->bit_depth < 8)
4364
5.09k
      {
4365
5.09k
         switch (row_info->bit_depth)
4366
5.09k
         {
4367
3.11k
            case 1:
4368
3.11k
            {
4369
3.11k
               sp = row + (size_t)((row_width - 1) >> 3);
4370
3.11k
               dp = row + (size_t)row_width - 1;
4371
3.11k
               shift = 7 - (int)((row_width + 7) & 0x07);
4372
9.68M
               for (i = 0; i < row_width; i++)
4373
9.67M
               {
4374
9.67M
                  if ((*sp >> shift) & 0x01)
4375
2.83M
                     *dp = 1;
4376
4377
6.84M
                  else
4378
6.84M
                     *dp = 0;
4379
4380
9.67M
                  if (shift == 7)
4381
1.21M
                  {
4382
1.21M
                     shift = 0;
4383
1.21M
                     sp--;
4384
1.21M
                  }
4385
4386
8.46M
                  else
4387
8.46M
                     shift++;
4388
4389
9.67M
                  dp--;
4390
9.67M
               }
4391
3.11k
               break;
4392
0
            }
4393
4394
935
            case 2:
4395
935
            {
4396
935
               sp = row + (size_t)((row_width - 1) >> 2);
4397
935
               dp = row + (size_t)row_width - 1;
4398
935
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4399
3.76M
               for (i = 0; i < row_width; i++)
4400
3.76M
               {
4401
3.76M
                  value = (*sp >> shift) & 0x03;
4402
3.76M
                  *dp = (png_byte)value;
4403
3.76M
                  if (shift == 6)
4404
941k
                  {
4405
941k
                     shift = 0;
4406
941k
                     sp--;
4407
941k
                  }
4408
4409
2.82M
                  else
4410
2.82M
                     shift += 2;
4411
4412
3.76M
                  dp--;
4413
3.76M
               }
4414
935
               break;
4415
0
            }
4416
4417
1.05k
            case 4:
4418
1.05k
            {
4419
1.05k
               sp = row + (size_t)((row_width - 1) >> 1);
4420
1.05k
               dp = row + (size_t)row_width - 1;
4421
1.05k
               shift = (int)((row_width & 0x01) << 2);
4422
1.98M
               for (i = 0; i < row_width; i++)
4423
1.98M
               {
4424
1.98M
                  value = (*sp >> shift) & 0x0f;
4425
1.98M
                  *dp = (png_byte)value;
4426
1.98M
                  if (shift == 4)
4427
991k
                  {
4428
991k
                     shift = 0;
4429
991k
                     sp--;
4430
991k
                  }
4431
4432
990k
                  else
4433
990k
                     shift += 4;
4434
4435
1.98M
                  dp--;
4436
1.98M
               }
4437
1.05k
               break;
4438
0
            }
4439
4440
0
            default:
4441
0
               break;
4442
5.09k
         }
4443
5.09k
         row_info->bit_depth = 8;
4444
5.09k
         row_info->pixel_depth = 8;
4445
5.09k
         row_info->rowbytes = row_width;
4446
5.09k
      }
4447
4448
6.76k
      if (row_info->bit_depth == 8)
4449
6.76k
      {
4450
6.76k
         {
4451
6.76k
            if (num_trans > 0)
4452
1.53k
            {
4453
1.53k
               sp = row + (size_t)row_width - 1;
4454
1.53k
               dp = row + ((size_t)row_width << 2) - 1;
4455
4456
1.53k
               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.53k
               PNG_UNUSED(png_ptr)
4469
1.53k
#endif
4470
4471
5.45M
               for (; i < row_width; i++)
4472
5.45M
               {
4473
5.45M
                  if ((int)(*sp) >= num_trans)
4474
1.72M
                     *dp-- = 0xff;
4475
3.72M
                  else
4476
3.72M
                     *dp-- = trans_alpha[*sp];
4477
5.45M
                  *dp-- = palette[*sp].blue;
4478
5.45M
                  *dp-- = palette[*sp].green;
4479
5.45M
                  *dp-- = palette[*sp].red;
4480
5.45M
                  sp--;
4481
5.45M
               }
4482
1.53k
               row_info->bit_depth = 8;
4483
1.53k
               row_info->pixel_depth = 32;
4484
1.53k
               row_info->rowbytes = (size_t)row_width * 4;
4485
1.53k
               row_info->color_type = 6;
4486
1.53k
               row_info->channels = 4;
4487
1.53k
            }
4488
4489
5.23k
            else
4490
5.23k
            {
4491
5.23k
               sp = row + (size_t)row_width - 1;
4492
5.23k
               dp = row + (size_t)row_width * 3 - 1;
4493
5.23k
               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
5.23k
               PNG_UNUSED(png_ptr)
4499
5.23k
#endif
4500
4501
9.98M
               for (; i < row_width; i++)
4502
9.97M
               {
4503
9.97M
                  *dp-- = palette[*sp].blue;
4504
9.97M
                  *dp-- = palette[*sp].green;
4505
9.97M
                  *dp-- = palette[*sp].red;
4506
9.97M
                  sp--;
4507
9.97M
               }
4508
4509
5.23k
               row_info->bit_depth = 8;
4510
5.23k
               row_info->pixel_depth = 24;
4511
5.23k
               row_info->rowbytes = (size_t)row_width * 3;
4512
5.23k
               row_info->color_type = 2;
4513
5.23k
               row_info->channels = 3;
4514
5.23k
            }
4515
6.76k
         }
4516
6.76k
      }
4517
6.76k
   }
4518
6.76k
}
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
791k
{
4527
791k
   int shift, value;
4528
791k
   png_bytep sp, dp;
4529
791k
   png_uint_32 i;
4530
791k
   png_uint_32 row_width=row_info->width;
4531
4532
791k
   png_debug(1, "in png_do_expand");
4533
4534
791k
   if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
4535
702k
   {
4536
702k
      unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
4537
4538
702k
      if (row_info->bit_depth < 8)
4539
577k
      {
4540
577k
         switch (row_info->bit_depth)
4541
577k
         {
4542
551k
            case 1:
4543
551k
            {
4544
551k
               gray = (gray & 0x01) * 0xff;
4545
551k
               sp = row + (size_t)((row_width - 1) >> 3);
4546
551k
               dp = row + (size_t)row_width - 1;
4547
551k
               shift = 7 - (int)((row_width + 7) & 0x07);
4548
51.1M
               for (i = 0; i < row_width; i++)
4549
50.6M
               {
4550
50.6M
                  if ((*sp >> shift) & 0x01)
4551
15.3M
                     *dp = 0xff;
4552
4553
35.2M
                  else
4554
35.2M
                     *dp = 0;
4555
4556
50.6M
                  if (shift == 7)
4557
6.79M
                  {
4558
6.79M
                     shift = 0;
4559
6.79M
                     sp--;
4560
6.79M
                  }
4561
4562
43.8M
                  else
4563
43.8M
                     shift++;
4564
4565
50.6M
                  dp--;
4566
50.6M
               }
4567
551k
               break;
4568
0
            }
4569
4570
18.0k
            case 2:
4571
18.0k
            {
4572
18.0k
               gray = (gray & 0x03) * 0x55;
4573
18.0k
               sp = row + (size_t)((row_width - 1) >> 2);
4574
18.0k
               dp = row + (size_t)row_width - 1;
4575
18.0k
               shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4576
5.97M
               for (i = 0; i < row_width; i++)
4577
5.96M
               {
4578
5.96M
                  value = (*sp >> shift) & 0x03;
4579
5.96M
                  *dp = (png_byte)(value | (value << 2) | (value << 4) |
4580
5.96M
                     (value << 6));
4581
5.96M
                  if (shift == 6)
4582
1.49M
                  {
4583
1.49M
                     shift = 0;
4584
1.49M
                     sp--;
4585
1.49M
                  }
4586
4587
4.46M
                  else
4588
4.46M
                     shift += 2;
4589
4590
5.96M
                  dp--;
4591
5.96M
               }
4592
18.0k
               break;
4593
0
            }
4594
4595
7.74k
            case 4:
4596
7.74k
            {
4597
7.74k
               gray = (gray & 0x0f) * 0x11;
4598
7.74k
               sp = row + (size_t)((row_width - 1) >> 1);
4599
7.74k
               dp = row + (size_t)row_width - 1;
4600
7.74k
               shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
4601
5.40M
               for (i = 0; i < row_width; i++)
4602
5.39M
               {
4603
5.39M
                  value = (*sp >> shift) & 0x0f;
4604
5.39M
                  *dp = (png_byte)(value | (value << 4));
4605
5.39M
                  if (shift == 4)
4606
2.69M
                  {
4607
2.69M
                     shift = 0;
4608
2.69M
                     sp--;
4609
2.69M
                  }
4610
4611
2.69M
                  else
4612
2.69M
                     shift = 4;
4613
4614
5.39M
                  dp--;
4615
5.39M
               }
4616
7.74k
               break;
4617
0
            }
4618
4619
0
            default:
4620
0
               break;
4621
577k
         }
4622
4623
577k
         row_info->bit_depth = 8;
4624
577k
         row_info->pixel_depth = 8;
4625
577k
         row_info->rowbytes = row_width;
4626
577k
      }
4627
4628
702k
      if (trans_color != NULL)
4629
3.99k
      {
4630
3.99k
         if (row_info->bit_depth == 8)
4631
2.00k
         {
4632
2.00k
            gray = gray & 0xff;
4633
2.00k
            sp = row + (size_t)row_width - 1;
4634
2.00k
            dp = row + ((size_t)row_width << 1) - 1;
4635
4636
16.0M
            for (i = 0; i < row_width; i++)
4637
16.0M
            {
4638
16.0M
               if ((*sp & 0xffU) == gray)
4639
8.77M
                  *dp-- = 0;
4640
4641
7.30M
               else
4642
7.30M
                  *dp-- = 0xff;
4643
4644
16.0M
               *dp-- = *sp--;
4645
16.0M
            }
4646
2.00k
         }
4647
4648
1.99k
         else if (row_info->bit_depth == 16)
4649
1.99k
         {
4650
1.99k
            unsigned int gray_high = (gray >> 8) & 0xff;
4651
1.99k
            unsigned int gray_low = gray & 0xff;
4652
1.99k
            sp = row + row_info->rowbytes - 1;
4653
1.99k
            dp = row + (row_info->rowbytes << 1) - 1;
4654
3.67M
            for (i = 0; i < row_width; i++)
4655
3.67M
            {
4656
3.67M
               if ((*(sp - 1) & 0xffU) == gray_high &&
4657
376k
                   (*(sp) & 0xffU) == gray_low)
4658
244k
               {
4659
244k
                  *dp-- = 0;
4660
244k
                  *dp-- = 0;
4661
244k
               }
4662
4663
3.43M
               else
4664
3.43M
               {
4665
3.43M
                  *dp-- = 0xff;
4666
3.43M
                  *dp-- = 0xff;
4667
3.43M
               }
4668
4669
3.67M
               *dp-- = *sp--;
4670
3.67M
               *dp-- = *sp--;
4671
3.67M
            }
4672
1.99k
         }
4673
4674
3.99k
         row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
4675
3.99k
         row_info->channels = 2;
4676
3.99k
         row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
4677
3.99k
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4678
3.99k
             row_width);
4679
3.99k
      }
4680
702k
   }
4681
89.0k
   else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
4682
66.8k
       trans_color != NULL)
4683
3.91k
   {
4684
3.91k
      if (row_info->bit_depth == 8)
4685
1.55k
      {
4686
1.55k
         png_byte red = (png_byte)(trans_color->red & 0xff);
4687
1.55k
         png_byte green = (png_byte)(trans_color->green & 0xff);
4688
1.55k
         png_byte blue = (png_byte)(trans_color->blue & 0xff);
4689
1.55k
         sp = row + (size_t)row_info->rowbytes - 1;
4690
1.55k
         dp = row + ((size_t)row_width << 2) - 1;
4691
332k
         for (i = 0; i < row_width; i++)
4692
331k
         {
4693
331k
            if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4694
643
               *dp-- = 0;
4695
4696
330k
            else
4697
330k
               *dp-- = 0xff;
4698
4699
331k
            *dp-- = *sp--;
4700
331k
            *dp-- = *sp--;
4701
331k
            *dp-- = *sp--;
4702
331k
         }
4703
1.55k
      }
4704
2.35k
      else if (row_info->bit_depth == 16)
4705
2.35k
      {
4706
2.35k
         png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
4707
2.35k
         png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
4708
2.35k
         png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
4709
2.35k
         png_byte red_low = (png_byte)(trans_color->red & 0xff);
4710
2.35k
         png_byte green_low = (png_byte)(trans_color->green & 0xff);
4711
2.35k
         png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
4712
2.35k
         sp = row + row_info->rowbytes - 1;
4713
2.35k
         dp = row + ((size_t)row_width << 3) - 1;
4714
1.01M
         for (i = 0; i < row_width; i++)
4715
1.00M
         {
4716
1.00M
            if (*(sp - 5) == red_high &&
4717
89.6k
                *(sp - 4) == red_low &&
4718
6.24k
                *(sp - 3) == green_high &&
4719
3.05k
                *(sp - 2) == green_low &&
4720
2.59k
                *(sp - 1) == blue_high &&
4721
1.78k
                *(sp    ) == blue_low)
4722
963
            {
4723
963
               *dp-- = 0;
4724
963
               *dp-- = 0;
4725
963
            }
4726
4727
1.00M
            else
4728
1.00M
            {
4729
1.00M
               *dp-- = 0xff;
4730
1.00M
               *dp-- = 0xff;
4731
1.00M
            }
4732
4733
1.00M
            *dp-- = *sp--;
4734
1.00M
            *dp-- = *sp--;
4735
1.00M
            *dp-- = *sp--;
4736
1.00M
            *dp-- = *sp--;
4737
1.00M
            *dp-- = *sp--;
4738
1.00M
            *dp-- = *sp--;
4739
1.00M
         }
4740
2.35k
      }
4741
3.91k
      row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
4742
3.91k
      row_info->channels = 4;
4743
3.91k
      row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
4744
3.91k
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4745
3.91k
   }
4746
791k
}
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
23.3k
{
4756
23.3k
   if (row_info->bit_depth == 8 &&
4757
22.5k
      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
4758
22.5k
   {
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
22.5k
      png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
4769
22.5k
      png_byte *dp = sp + row_info->rowbytes;  /* destination, end + 1 */
4770
70.6M
      while (dp > sp)
4771
70.6M
      {
4772
70.6M
         dp[-2] = dp[-1] = *--sp; dp -= 2;
4773
70.6M
      }
4774
4775
22.5k
      row_info->rowbytes *= 2;
4776
22.5k
      row_info->bit_depth = 16;
4777
22.5k
      row_info->pixel_depth = (png_byte)(row_info->channels * 16);
4778
22.5k
   }
4779
23.3k
}
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
816k
{
4883
816k
   png_debug(1, "in png_do_read_transformations");
4884
4885
816k
   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
816k
   if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
4901
812k
       (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
816k
#ifdef PNG_READ_EXPAND_SUPPORTED
4911
816k
   if ((png_ptr->transformations & PNG_EXPAND) != 0)
4912
798k
   {
4913
798k
      if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4914
6.76k
      {
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
6.76k
         png_do_expand_palette(png_ptr, row_info, png_ptr->row_buf + 1,
4928
6.76k
             png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
4929
6.76k
      }
4930
4931
791k
      else
4932
791k
      {
4933
791k
         if (png_ptr->num_trans != 0 &&
4934
8.23k
             (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
4935
7.90k
            png_do_expand(row_info, png_ptr->row_buf + 1,
4936
7.90k
                &(png_ptr->trans_color));
4937
4938
783k
         else
4939
783k
            png_do_expand(row_info, png_ptr->row_buf + 1, NULL);
4940
791k
      }
4941
798k
   }
4942
816k
#endif
4943
4944
816k
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4945
816k
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4946
23.1k
       (png_ptr->transformations & PNG_COMPOSE) == 0 &&
4947
17.8k
       (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4948
13.5k
       row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4949
6.89k
      png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4950
6.89k
          0 /* at_start == false, because SWAP_ALPHA happens later */);
4951
816k
#endif
4952
4953
816k
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4954
816k
   if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
4955
5.30k
   {
4956
5.30k
      int rgb_error =
4957
5.30k
          png_do_rgb_to_gray(png_ptr, row_info,
4958
5.30k
              png_ptr->row_buf + 1);
4959
4960
5.30k
      if (rgb_error != 0)
4961
2.02k
      {
4962
2.02k
         png_ptr->rgb_to_gray_status=1;
4963
2.02k
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4964
2.02k
             PNG_RGB_TO_GRAY_WARN)
4965
0
            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4966
4967
2.02k
         if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4968
2.02k
             PNG_RGB_TO_GRAY_ERR)
4969
0
            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4970
2.02k
      }
4971
5.30k
   }
4972
816k
#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
816k
#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
816k
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
5010
749k
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
5011
747k
      png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
5012
816k
#endif
5013
5014
816k
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
5015
816k
   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
5016
816k
   if ((png_ptr->transformations & PNG_COMPOSE) != 0)
5017
6.65k
      png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
5018
816k
#endif
5019
5020
816k
#ifdef PNG_READ_GAMMA_SUPPORTED
5021
816k
   if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
5022
41.2k
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
5023
      /* Because RGB_TO_GRAY does the gamma transform. */
5024
41.2k
      (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
5025
38.3k
#endif
5026
38.3k
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
5027
38.3k
   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
38.3k
       !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
5032
2.89k
       ((png_ptr->num_trans != 0) ||
5033
1.57k
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
5034
35.4k
#endif
5035
      /* Because png_init_read_transformations transforms the palette, unless
5036
       * RGB_TO_GRAY will do the transform.
5037
       */
5038
35.4k
       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
5039
35.4k
      png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
5040
816k
#endif
5041
5042
816k
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
5043
816k
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
5044
23.1k
       (png_ptr->transformations & PNG_COMPOSE) != 0 &&
5045
5.28k
       (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
5046
3.75k
       row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
5047
3.88k
      png_do_strip_channel(row_info, png_ptr->row_buf + 1,
5048
3.88k
          0 /* at_start == false, because SWAP_ALPHA happens later */);
5049
816k
#endif
5050
5051
816k
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
5052
816k
   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
816k
#endif
5056
5057
816k
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
5058
816k
   if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
5059
442k
      png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
5060
816k
#endif
5061
5062
816k
#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
816k
   if ((png_ptr->transformations & PNG_16_TO_8) != 0)
5068
19.5k
      png_do_chop(row_info, png_ptr->row_buf + 1);
5069
816k
#endif
5070
5071
816k
#ifdef PNG_READ_QUANTIZE_SUPPORTED
5072
816k
   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
816k
#endif /* READ_QUANTIZE */
5076
5077
816k
#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
816k
   if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
5084
23.3k
      png_do_expand_16(row_info, png_ptr->row_buf + 1);
5085
816k
#endif
5086
5087
816k
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
5088
   /* NOTE: moved here in 1.5.4 (from much later in this list.) */
5089
816k
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
5090
749k
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
5091
1.44k
      png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
5092
816k
#endif
5093
5094
816k
#ifdef PNG_READ_INVERT_SUPPORTED
5095
816k
   if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
5096
25.9k
      png_do_invert(row_info, png_ptr->row_buf + 1);
5097
816k
#endif
5098
5099
816k
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
5100
816k
   if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
5101
21.8k
      png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
5102
816k
#endif
5103
5104
816k
#ifdef PNG_READ_SHIFT_SUPPORTED
5105
816k
   if ((png_ptr->transformations & PNG_SHIFT) != 0)
5106
2.17k
      png_do_unshift(row_info, png_ptr->row_buf + 1,
5107
2.17k
          &(png_ptr->shift));
5108
816k
#endif
5109
5110
816k
#ifdef PNG_READ_PACK_SUPPORTED
5111
816k
   if ((png_ptr->transformations & PNG_PACK) != 0)
5112
298k
      png_do_unpack(row_info, png_ptr->row_buf + 1);
5113
816k
#endif
5114
5115
816k
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
5116
   /* Added at libpng-1.5.10 */
5117
816k
   if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
5118
3.70k
       png_ptr->num_palette_max >= 0)
5119
3.70k
      png_do_check_palette_indexes(png_ptr, row_info);
5120
816k
#endif
5121
5122
816k
#ifdef PNG_READ_BGR_SUPPORTED
5123
816k
   if ((png_ptr->transformations & PNG_BGR) != 0)
5124
23.8k
      png_do_bgr(row_info, png_ptr->row_buf + 1);
5125
816k
#endif
5126
5127
816k
#ifdef PNG_READ_PACKSWAP_SUPPORTED
5128
816k
   if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
5129
6.73k
      png_do_packswap(row_info, png_ptr->row_buf + 1);
5130
816k
#endif
5131
5132
816k
#ifdef PNG_READ_FILLER_SUPPORTED
5133
816k
   if ((png_ptr->transformations & PNG_FILLER) != 0)
5134
368k
      png_do_read_filler(row_info, png_ptr->row_buf + 1,
5135
368k
          (png_uint_32)png_ptr->filler, png_ptr->flags);
5136
816k
#endif
5137
5138
816k
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
5139
816k
   if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
5140
23.1k
      png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
5141
816k
#endif
5142
5143
816k
#ifdef PNG_READ_16BIT_SUPPORTED
5144
816k
#ifdef PNG_READ_SWAP_SUPPORTED
5145
816k
   if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
5146
9.29k
      png_do_swap(row_info, png_ptr->row_buf + 1);
5147
816k
#endif
5148
816k
#endif
5149
5150
816k
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
5151
816k
   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
816k
#endif
5177
816k
}
5178
5179
#endif /* READ_TRANSFORMS */
5180
#endif /* READ */