Coverage Report

Created: 2026-03-31 06:35

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