Coverage Report

Created: 2026-05-16 07:22

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