Coverage Report

Created: 2025-10-12 07:48

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