Coverage Report

Created: 2025-09-27 07:21

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