Coverage Report

Created: 2025-12-31 07:31

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