Coverage Report

Created: 2025-07-23 08:13

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