Coverage Report

Created: 2023-12-08 06:53

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