Coverage Report

Created: 2026-06-30 07:12

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