Coverage Report

Created: 2026-03-31 11:00

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