Coverage Report

Created: 2025-12-03 07:28

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