Coverage Report

Created: 2025-12-13 07:57

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