Coverage Report

Created: 2025-11-16 06:41

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