Coverage Report

Created: 2025-06-24 07:01

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