Coverage Report

Created: 2026-03-12 06:42

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