Subversion Repositories Kolibri OS

Rev

Rev 1897 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1897 Rev 3928
Line 1... Line 1...
1
/* pngrtran.c - transforms the data in a row for PNG readers
1
/* pngrtran.c - transforms the data in a row for PNG readers
2
 *
2
 *
3
 * Last changed in libpng 1.5.1 [February 3, 2011]
3
 * Last changed in libpng 1.6.4 [September 14, 2013]
4
 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
4
 * Copyright (c) 1998-2013 Glenn Randers-Pehrson
5
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
5
 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
6
 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7
 *
7
 *
8
 * This code is released under the libpng license.
8
 * This code is released under the libpng license.
9
 * For conditions of distribution and use, see the disclaimer
9
 * For conditions of distribution and use, see the disclaimer
Line 20... Line 20...
20
#ifdef PNG_READ_SUPPORTED
20
#ifdef PNG_READ_SUPPORTED
Line 21... Line 21...
21
 
21
 
22
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
22
/* Set the action on getting a CRC error for an ancillary or critical chunk. */
23
void PNGAPI
23
void PNGAPI
24
png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
24
png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
25
{
25
{
Line 26... Line 26...
26
   png_debug(1, "in png_set_crc_action");
26
   png_debug(1, "in png_set_crc_action");
27
 
27
 
Line 86... Line 86...
86
         break;
86
         break;
87
   }
87
   }
88
}
88
}
89
 
89
 
Line -... Line 90...
-
 
90
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
-
 
91
/* Is it OK to set a transformation now?  Only if png_start_read_image or
-
 
92
 * png_read_update_info have not been called.  It is not necessary for the IHDR
-
 
93
 * to have been read in all cases, the parameter allows for this check too.
-
 
94
 */
-
 
95
static int
-
 
96
png_rtran_ok(png_structrp png_ptr, int need_IHDR)
-
 
97
{
-
 
98
   if (png_ptr != NULL)
-
 
99
   {
-
 
100
      if (png_ptr->flags & PNG_FLAG_ROW_INIT)
-
 
101
         png_app_error(png_ptr,
-
 
102
            "invalid after png_start_read_image or png_read_update_info");
-
 
103
 
-
 
104
      else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0)
-
 
105
         png_app_error(png_ptr, "invalid before the PNG header has been read");
-
 
106
 
-
 
107
      else
-
 
108
      {
-
 
109
         /* Turn on failure to initialize correctly for all transforms. */
-
 
110
         png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
-
 
111
 
-
 
112
         return 1; /* Ok */
-
 
113
      }
-
 
114
   }
-
 
115
 
-
 
116
   return 0; /* no png_error possible! */
-
 
117
}
-
 
118
#endif
-
 
119
 
90
#ifdef PNG_READ_BACKGROUND_SUPPORTED
120
#ifdef PNG_READ_BACKGROUND_SUPPORTED
91
/* Handle alpha and tRNS via a background color */
121
/* Handle alpha and tRNS via a background color */
92
void PNGFAPI
122
void PNGFAPI
93
png_set_background_fixed(png_structp png_ptr,
123
png_set_background_fixed(png_structrp png_ptr,
94
    png_const_color_16p background_color, int background_gamma_code,
124
    png_const_color_16p background_color, int background_gamma_code,
95
    int need_expand, png_fixed_point background_gamma)
125
    int need_expand, png_fixed_point background_gamma)
96
{
126
{
97
   png_debug(1, "in png_set_background_fixed");
127
   png_debug(1, "in png_set_background_fixed");
Line 98... Line 128...
98
 
128
 
99
   if (png_ptr == NULL)
129
   if (!png_rtran_ok(png_ptr, 0) || background_color == NULL)
Line 100... Line 130...
100
      return;
130
      return;
101
 
131
 
102
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
132
   if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
103
   {
133
   {
104
      png_warning(png_ptr, "Application must supply a known background gamma");
134
      png_warning(png_ptr, "Application must supply a known background gamma");
Line -... Line 135...
-
 
135
      return;
105
      return;
136
   }
106
   }
137
 
-
 
138
   png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
107
 
139
   png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
108
   png_ptr->transformations |= PNG_BACKGROUND;
140
   png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
109
   png_memcpy(&(png_ptr->background), background_color,
141
 
-
 
142
   png_ptr->background = *background_color;
110
      png_sizeof(png_color_16));
143
   png_ptr->background_gamma = background_gamma;
-
 
144
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
-
 
145
   if (need_expand)
111
   png_ptr->background_gamma = background_gamma;
146
      png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
Line 112... Line 147...
112
   png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
147
   else
113
   png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
148
      png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
114
}
149
}
115
 
150
 
116
#  ifdef PNG_FLOATING_POINT_SUPPORTED
151
#  ifdef PNG_FLOATING_POINT_SUPPORTED
117
void PNGAPI
152
void PNGAPI
118
png_set_background(png_structp png_ptr,
153
png_set_background(png_structrp png_ptr,
119
    png_const_color_16p background_color, int background_gamma_code,
154
    png_const_color_16p background_color, int background_gamma_code,
120
    int need_expand, double background_gamma)
155
    int need_expand, double background_gamma)
121
{
156
{
122
   png_set_background_fixed(png_ptr, background_color, background_gamma_code,
157
   png_set_background_fixed(png_ptr, background_color, background_gamma_code,
Line -... Line 158...
-
 
158
      need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
-
 
159
}
-
 
160
#  endif  /* FLOATING_POINT */
-
 
161
#endif /* READ_BACKGROUND */
123
      need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
162
 
-
 
163
/* Scale 16-bit depth files to 8-bit depth.  If both of these are set then the
-
 
164
 * one that pngrtran does first (scale) happens.  This is necessary to allow the
-
 
165
 * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
-
 
166
 */
-
 
167
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
-
 
168
void PNGAPI
-
 
169
png_set_scale_16(png_structrp png_ptr)
-
 
170
{
-
 
171
   png_debug(1, "in png_set_scale_16");
-
 
172
 
-
 
173
   if (!png_rtran_ok(png_ptr, 0))
-
 
174
      return;
-
 
175
 
124
}
176
   png_ptr->transformations |= PNG_SCALE_16_TO_8;
125
#  endif  /* FLOATING_POINT */
177
}
126
#endif /* READ_BACKGROUND */
178
#endif
127
 
179
 
128
#ifdef PNG_READ_16_TO_8_SUPPORTED
180
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
Line 129... Line 181...
129
/* Strip 16 bit depth files to 8 bit depth */
181
/* Chop 16-bit depth files to 8-bit depth */
130
void PNGAPI
182
void PNGAPI
Line 131... Line 183...
131
png_set_strip_16(png_structp png_ptr)
183
png_set_strip_16(png_structrp png_ptr)
132
{
184
{
133
   png_debug(1, "in png_set_strip_16");
185
   png_debug(1, "in png_set_strip_16");
Line 134... Line 186...
134
 
186
 
135
   if (png_ptr == NULL)
187
   if (!png_rtran_ok(png_ptr, 0))
136
      return;
188
      return;
137
 
189
 
138
   png_ptr->transformations |= PNG_16_TO_8;
190
   png_ptr->transformations |= PNG_16_TO_8;
Line 139... Line 191...
139
}
191
}
140
#endif
192
#endif
Line -... Line 193...
-
 
193
 
-
 
194
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
-
 
195
void PNGAPI
-
 
196
png_set_strip_alpha(png_structrp png_ptr)
-
 
197
{
-
 
198
   png_debug(1, "in png_set_strip_alpha");
-
 
199
 
-
 
200
   if (!png_rtran_ok(png_ptr, 0))
-
 
201
      return;
-
 
202
 
-
 
203
   png_ptr->transformations |= PNG_STRIP_ALPHA;
-
 
204
}
-
 
205
#endif
-
 
206
 
-
 
207
#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
-
 
208
static png_fixed_point
-
 
209
translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma,
-
 
210
   int is_screen)
-
 
211
{
-
 
212
   /* Check for flag values.  The main reason for having the old Mac value as a
-
 
213
    * flag is that it is pretty near impossible to work out what the correct
141
 
214
    * value is from Apple documentation - a working Mac system is needed to
-
 
215
    * discover the value!
-
 
216
    */
-
 
217
   if (output_gamma == PNG_DEFAULT_sRGB ||
-
 
218
      output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
-
 
219
   {
-
 
220
      /* If there is no sRGB support this just sets the gamma to the standard
-
 
221
       * sRGB value.  (This is a side effect of using this function!)
-
 
222
       */
-
 
223
#     ifdef PNG_READ_sRGB_SUPPORTED
-
 
224
         png_ptr->flags |= PNG_FLAG_ASSUME_sRGB;
-
 
225
#     else
-
 
226
         PNG_UNUSED(png_ptr)
-
 
227
#     endif
-
 
228
      if (is_screen)
-
 
229
         output_gamma = PNG_GAMMA_sRGB;
-
 
230
      else
-
 
231
         output_gamma = PNG_GAMMA_sRGB_INVERSE;
-
 
232
   }
-
 
233
 
-
 
234
   else if (output_gamma == PNG_GAMMA_MAC_18 ||
-
 
235
      output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
-
 
236
   {
-
 
237
      if (is_screen)
-
 
238
         output_gamma = PNG_GAMMA_MAC_OLD;
-
 
239
      else
-
 
240
         output_gamma = PNG_GAMMA_MAC_INVERSE;
-
 
241
   }
-
 
242
 
-
 
243
   return output_gamma;
-
 
244
}
-
 
245
 
-
 
246
#  ifdef PNG_FLOATING_POINT_SUPPORTED
-
 
247
static png_fixed_point
-
 
248
convert_gamma_value(png_structrp png_ptr, double output_gamma)
-
 
249
{
-
 
250
   /* The following silently ignores cases where fixed point (times 100,000)
-
 
251
    * gamma values are passed to the floating point API.  This is safe and it
-
 
252
    * means the fixed point constants work just fine with the floating point
-
 
253
    * API.  The alternative would just lead to undetected errors and spurious
-
 
254
    * bug reports.  Negative values fail inside the _fixed API unless they
-
 
255
    * correspond to the flag values.
-
 
256
    */
142
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
257
   if (output_gamma > 0 && output_gamma < 128)
143
void PNGAPI
258
      output_gamma *= PNG_FP_1;
-
 
259
 
-
 
260
   /* This preserves -1 and -2 exactly: */
-
 
261
   output_gamma = floor(output_gamma + .5);
-
 
262
 
-
 
263
   if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
-
 
264
      png_fixed_error(png_ptr, "gamma value");
-
 
265
 
-
 
266
   return (png_fixed_point)output_gamma;
-
 
267
}
-
 
268
#  endif
-
 
269
#endif /* READ_ALPHA_MODE || READ_GAMMA */
-
 
270
 
-
 
271
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
-
 
272
void PNGFAPI
-
 
273
png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
-
 
274
   png_fixed_point output_gamma)
-
 
275
{
-
 
276
   int compose = 0;
-
 
277
   png_fixed_point file_gamma;
-
 
278
 
-
 
279
   png_debug(1, "in png_set_alpha_mode");
-
 
280
 
-
 
281
   if (!png_rtran_ok(png_ptr, 0))
-
 
282
      return;
-
 
283
 
-
 
284
   output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/);
-
 
285
 
-
 
286
   /* Validate the value to ensure it is in a reasonable range. The value
-
 
287
    * is expected to be 1 or greater, but this range test allows for some
-
 
288
    * viewing correction values.  The intent is to weed out users of this API
-
 
289
    * who use the inverse of the gamma value accidentally!  Since some of these
-
 
290
    * values are reasonable this may have to be changed.
-
 
291
    */
-
 
292
   if (output_gamma < 70000 || output_gamma > 300000)
-
 
293
      png_error(png_ptr, "output gamma out of expected range");
-
 
294
 
-
 
295
   /* The default file gamma is the inverse of the output gamma; the output
-
 
296
    * gamma may be changed below so get the file value first:
-
 
297
    */
-
 
298
   file_gamma = png_reciprocal(output_gamma);
-
 
299
 
-
 
300
   /* There are really 8 possibilities here, composed of any combination
-
 
301
    * of:
-
 
302
    *
-
 
303
    *    premultiply the color channels
-
 
304
    *    do not encode non-opaque pixels
-
 
305
    *    encode the alpha as well as the color channels
-
 
306
    *
-
 
307
    * The differences disappear if the input/output ('screen') gamma is 1.0,
-
 
308
    * because then the encoding is a no-op and there is only the choice of
-
 
309
    * premultiplying the color channels or not.
-
 
310
    *
-
 
311
    * png_set_alpha_mode and png_set_background interact because both use
-
 
312
    * png_compose to do the work.  Calling both is only useful when
-
 
313
    * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
-
 
314
    * with a default gamma value.  Otherwise PNG_COMPOSE must not be set.
-
 
315
    */
-
 
316
   switch (mode)
-
 
317
   {
-
 
318
      case PNG_ALPHA_PNG:        /* default: png standard */
-
 
319
         /* No compose, but it may be set by png_set_background! */
-
 
320
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
-
 
321
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
322
         break;
-
 
323
 
-
 
324
      case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
-
 
325
         compose = 1;
-
 
326
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
-
 
327
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
328
         /* The output is linear: */
-
 
329
         output_gamma = PNG_FP_1;
-
 
330
         break;
-
 
331
 
-
 
332
      case PNG_ALPHA_OPTIMIZED:  /* associated, non-opaque pixels linear */
-
 
333
         compose = 1;
-
 
334
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
-
 
335
         png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
-
 
336
         /* output_gamma records the encoding of opaque pixels! */
-
 
337
         break;
-
 
338
 
-
 
339
      case PNG_ALPHA_BROKEN:     /* associated, non-linear, alpha encoded */
-
 
340
         compose = 1;
-
 
341
         png_ptr->transformations |= PNG_ENCODE_ALPHA;
-
 
342
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
343
         break;
-
 
344
 
-
 
345
      default:
-
 
346
         png_error(png_ptr, "invalid alpha mode");
-
 
347
   }
-
 
348
 
-
 
349
   /* Only set the default gamma if the file gamma has not been set (this has
-
 
350
    * the side effect that the gamma in a second call to png_set_alpha_mode will
-
 
351
    * be ignored.)
-
 
352
    */
-
 
353
   if (png_ptr->colorspace.gamma == 0)
-
 
354
   {
-
 
355
      png_ptr->colorspace.gamma = file_gamma;
-
 
356
      png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
-
 
357
   }
-
 
358
 
-
 
359
   /* But always set the output gamma: */
-
 
360
   png_ptr->screen_gamma = output_gamma;
-
 
361
 
-
 
362
   /* Finally, if pre-multiplying, set the background fields to achieve the
-
 
363
    * desired result.
-
 
364
    */
-
 
365
   if (compose)
-
 
366
   {
-
 
367
      /* And obtain alpha pre-multiplication by composing on black: */
-
 
368
      memset(&png_ptr->background, 0, (sizeof png_ptr->background));
-
 
369
      png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */
-
 
370
      png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
-
 
371
      png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
-
 
372
 
-
 
373
      if (png_ptr->transformations & PNG_COMPOSE)
-
 
374
         png_error(png_ptr,
-
 
375
            "conflicting calls to set alpha mode and background");
-
 
376
 
-
 
377
      png_ptr->transformations |= PNG_COMPOSE;
-
 
378
   }
-
 
379
}
Line 144... Line 380...
144
png_set_strip_alpha(png_structp png_ptr)
380
 
145
{
381
#  ifdef PNG_FLOATING_POINT_SUPPORTED
146
   png_debug(1, "in png_set_strip_alpha");
382
void PNGAPI
147
 
383
png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
148
   if (png_ptr == NULL)
384
{
149
      return;
385
   png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
150
 
386
      output_gamma));
151
   png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
387
}
152
}
388
#  endif
Line 153... Line 389...
153
#endif
389
#endif
154
 
390
 
155
#ifdef PNG_READ_QUANTIZE_SUPPORTED
391
#ifdef PNG_READ_QUANTIZE_SUPPORTED
156
/* Dither file to 8 bit.  Supply a palette, the current number
392
/* Dither file to 8-bit.  Supply a palette, the current number
157
 * of elements in the palette, the maximum number of elements
393
 * of elements in the palette, the maximum number of elements
158
 * allowed, and a histogram if possible.  If the current number
394
 * allowed, and a histogram if possible.  If the current number
159
 * of colors is greater then the maximum number, the palette will be
395
 * of colors is greater then the maximum number, the palette will be
160
 * modified to fit in the maximum number.  "full_quantize" indicates
396
 * modified to fit in the maximum number.  "full_quantize" indicates
Line 161... Line 397...
161
 * whether we need a quantizing cube set up for RGB images, or if we
397
 * whether we need a quantizing cube set up for RGB images, or if we
162
 * simply are reducing the number of colors in a paletted image.
398
 * simply are reducing the number of colors in a paletted image.
163
 */
399
 */
164
 
400
 
165
typedef struct png_dsort_struct
401
typedef struct png_dsort_struct
166
{
402
{
Line 167... Line 403...
167
   struct png_dsort_struct FAR * next;
403
   struct png_dsort_struct * next;
168
   png_byte left;
404
   png_byte left;
Line 169... Line 405...
169
   png_byte right;
405
   png_byte right;
Line 170... Line 406...
170
} png_dsort;
406
} png_dsort;
171
typedef png_dsort FAR *       png_dsortp;
407
typedef png_dsort *   png_dsortp;
172
typedef png_dsort FAR * FAR * png_dsortpp;
408
typedef png_dsort * * png_dsortpp;
Line 173... Line 409...
173
 
409
 
174
void PNGAPI
410
void PNGAPI
175
png_set_quantize(png_structp png_ptr, png_colorp palette,
411
png_set_quantize(png_structrp png_ptr, png_colorp palette,
176
    int num_palette, int maximum_colors, png_const_uint_16p histogram,
412
    int num_palette, int maximum_colors, png_const_uint_16p histogram,
177
    int full_quantize)
413
    int full_quantize)
Line 178... Line 414...
178
{
414
{
Line 204... Line 440...
204
         int i;
440
         int i;
Line 205... Line 441...
205
 
441
 
206
         /* Initialize an array to sort colors */
442
         /* Initialize an array to sort colors */
207
         png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
443
         png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
Line 208... Line 444...
208
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
444
             (png_uint_32)(num_palette * (sizeof (png_byte))));
209
 
445
 
210
         /* Initialize the quantize_sort array */
446
         /* Initialize the quantize_sort array */
Line 338... Line 574...
338
         t = NULL;
574
         t = NULL;
Line 339... Line 575...
339
 
575
 
340
         /* Initialize palette index arrays */
576
         /* Initialize palette index arrays */
341
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
577
         png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
342
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
578
             (png_uint_32)(num_palette * (sizeof (png_byte))));
343
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
579
         png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
Line 344... Line 580...
344
             (png_uint_32)(num_palette * png_sizeof(png_byte)));
580
             (png_uint_32)(num_palette * (sizeof (png_byte))));
345
 
581
 
346
         /* Initialize the sort array */
582
         /* Initialize the sort array */
347
         for (i = 0; i < num_palette; i++)
583
         for (i = 0; i < num_palette; i++)
348
         {
584
         {
349
            png_ptr->index_to_palette[i] = (png_byte)i;
585
            png_ptr->index_to_palette[i] = (png_byte)i;
Line 350... Line 586...
350
            png_ptr->palette_to_index[i] = (png_byte)i;
586
            png_ptr->palette_to_index[i] = (png_byte)i;
351
         }
587
         }
Line 352... Line 588...
352
 
588
 
Line 353... Line 589...
353
         hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
589
         hash = (png_dsortpp)png_calloc(png_ptr, (png_uint_32)(769 *
354
             png_sizeof(png_dsortp)));
590
             (sizeof (png_dsortp))));
Line 380... Line 616...
380
                  if (d <= max_d)
616
                  if (d <= max_d)
381
                  {
617
                  {
Line 382... Line 618...
382
 
618
 
383
                     t = (png_dsortp)png_malloc_warn(png_ptr,
619
                     t = (png_dsortp)png_malloc_warn(png_ptr,
Line 384... Line 620...
384
                         (png_uint_32)(png_sizeof(png_dsort)));
620
                         (png_uint_32)(sizeof (png_dsort)));
385
 
621
 
Line 386... Line 622...
386
                     if (t == NULL)
622
                     if (t == NULL)
Line 505... Line 741...
505
      int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
741
      int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
506
      png_size_t num_entries = ((png_size_t)1 << total_bits);
742
      png_size_t num_entries = ((png_size_t)1 << total_bits);
507
 
743
 
Line 508... Line 744...
508
      png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
744
      png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
509
          (png_uint_32)(num_entries * png_sizeof(png_byte)));
745
          (png_uint_32)(num_entries * (sizeof (png_byte))));
Line 510... Line 746...
510
 
746
 
511
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
747
      distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
Line 512... Line 748...
512
          png_sizeof(png_byte)));
748
          (sizeof (png_byte))));
Line 513... Line 749...
513
 
749
 
514
      png_memset(distance, 0xff, num_entries * png_sizeof(png_byte));
750
      memset(distance, 0xff, num_entries * (sizeof (png_byte)));
515
 
751
 
516
      for (i = 0; i < num_palette; i++)
752
      for (i = 0; i < num_palette; i++)
Line 558... Line 794...
558
}
794
}
559
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
795
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
560
 
796
 
Line 561... Line 797...
561
#ifdef PNG_READ_GAMMA_SUPPORTED
797
#ifdef PNG_READ_GAMMA_SUPPORTED
562
/* Transform the image from the file_gamma to the screen_gamma.  We
-
 
563
 * only do transformations on images where the file_gamma and screen_gamma
-
 
564
 * are not close reciprocals, otherwise it slows things down slightly, and
-
 
565
 * also needlessly introduces small errors.
-
 
566
 *
-
 
567
 * We will turn off gamma transformation later if no semitransparent entries
-
 
568
 * are present in the tRNS array for palette images.  We can't do it here
-
 
569
 * because we don't necessarily have the tRNS chunk yet.
-
 
570
 */
-
 
571
static int /* PRIVATE */
-
 
572
png_gamma_threshold(png_fixed_point scrn_gamma, png_fixed_point file_gamma)
-
 
573
{
-
 
574
   /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
-
 
575
    * correction as a difference of the overall transform from 1.0
-
 
576
    *
-
 
577
    * We want to compare the threshold with s*f - 1, if we get
-
 
578
    * overflow here it is because of wacky gamma values so we
-
 
579
    * turn on processing anyway.
-
 
580
    */
-
 
581
   png_fixed_point gtest;
-
 
582
   return !png_muldiv(>est, scrn_gamma, file_gamma, PNG_FP_1) ||
-
 
583
       png_gamma_significant(gtest);
-
 
584
}
-
 
585
 
-
 
586
void PNGFAPI
798
void PNGFAPI
587
png_set_gamma_fixed(png_structp png_ptr, png_fixed_point scrn_gamma,
799
png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
588
   png_fixed_point file_gamma)
800
   png_fixed_point file_gamma)
589
{
801
{
590
   png_debug(1, "in png_set_gamma_fixed");
802
   png_debug(1, "in png_set_gamma_fixed");
Line 591... Line 803...
591
 
803
 
592
   if (png_ptr == NULL)
804
   if (!png_rtran_ok(png_ptr, 0))
Line 593... Line 805...
593
      return;
805
      return;
-
 
806
 
-
 
807
   /* New in libpng-1.5.4 - reserve particular negative values as flags. */
-
 
808
   scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/);
-
 
809
   file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/);
-
 
810
 
-
 
811
   /* Checking the gamma values for being >0 was added in 1.5.4 along with the
-
 
812
    * premultiplied alpha support; this actually hides an undocumented feature
-
 
813
    * of the previous implementation which allowed gamma processing to be
-
 
814
    * disabled in background handling.  There is no evidence (so far) that this
-
 
815
    * was being used; however, png_set_background itself accepted and must still
-
 
816
    * accept '0' for the gamma value it takes, because it isn't always used.
-
 
817
    *
-
 
818
    * Since this is an API change (albeit a very minor one that removes an
-
 
819
    * undocumented API feature) the following checks were only enabled in
-
 
820
    * libpng-1.6.0.
594
 
821
    */
-
 
822
   if (file_gamma <= 0)
-
 
823
      png_error(png_ptr, "invalid file gamma in png_set_gamma");
595
   if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) ||
824
 
-
 
825
   if (scrn_gamma <= 0)
-
 
826
      png_error(png_ptr, "invalid screen gamma in png_set_gamma");
-
 
827
 
596
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
828
   /* Set the gamma values unconditionally - this overrides the value in the PNG
-
 
829
    * file if a gAMA chunk was present.  png_set_alpha_mode provides a
597
       png_gamma_threshold(scrn_gamma, file_gamma))
830
    * different, easier, way to default the file gamma.
-
 
831
    */
598
      png_ptr->transformations |= PNG_GAMMA;
832
   png_ptr->colorspace.gamma = file_gamma;
599
   png_ptr->gamma = file_gamma;
833
   png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
Line 600... Line 834...
600
   png_ptr->screen_gamma = scrn_gamma;
834
   png_ptr->screen_gamma = scrn_gamma;
601
}
835
}
602
 
836
 
603
#  ifdef PNG_FLOATING_POINT_SUPPORTED
837
#  ifdef PNG_FLOATING_POINT_SUPPORTED
604
void PNGAPI
-
 
605
png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
838
void PNGAPI
606
{
839
png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
607
   png_set_gamma_fixed(png_ptr,
840
{
608
      png_fixed(png_ptr, scrn_gamma, "png_set_gamma screen gamma"),
841
   png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
609
      png_fixed(png_ptr, file_gamma, "png_set_gamma file gamma"));
842
      convert_gamma_value(png_ptr, file_gamma));
Line 610... Line 843...
610
}
843
}
611
#  endif /* FLOATING_POINT_SUPPORTED */
844
#  endif /* FLOATING_POINT_SUPPORTED */
612
#endif /* READ_GAMMA */
845
#endif /* READ_GAMMA */
613
 
846
 
614
#ifdef PNG_READ_EXPAND_SUPPORTED
847
#ifdef PNG_READ_EXPAND_SUPPORTED
615
/* Expand paletted images to RGB, expand grayscale images of
848
/* Expand paletted images to RGB, expand grayscale images of
616
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
849
 * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
617
 * to alpha channels.
850
 * to alpha channels.
618
 */
851
 */
Line 619... Line 852...
619
void PNGAPI
852
void PNGAPI
620
png_set_expand(png_structp png_ptr)
853
png_set_expand(png_structrp png_ptr)
Line 621... Line 854...
621
{
854
{
622
   png_debug(1, "in png_set_expand");
-
 
623
 
855
   png_debug(1, "in png_set_expand");
Line 624... Line 856...
624
   if (png_ptr == NULL)
856
 
625
      return;
857
   if (!png_rtran_ok(png_ptr, 0))
626
 
858
      return;
Line 647... Line 879...
647
 */
879
 */
648
 
880
 
Line 649... Line 881...
649
/* Expand paletted images to RGB. */
881
/* Expand paletted images to RGB. */
650
void PNGAPI
882
void PNGAPI
651
png_set_palette_to_rgb(png_structp png_ptr)
883
png_set_palette_to_rgb(png_structrp png_ptr)
652
{
884
{
653
   png_debug(1, "in png_set_palette_to_rgb");
885
   png_debug(1, "in png_set_palette_to_rgb");
Line 654... Line 886...
654
 
886
 
655
   if (png_ptr == NULL)
887
   if (!png_rtran_ok(png_ptr, 0))
Line 656... Line 888...
656
      return;
888
      return;
657
 
-
 
658
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
889
 
Line 659... Line 890...
659
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
890
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
660
}
891
}
661
 
892
 
662
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
893
/* Expand grayscale images of less than 8-bit depth to 8 bits. */
663
void PNGAPI
894
void PNGAPI
Line 664... Line 895...
664
png_set_expand_gray_1_2_4_to_8(png_structp png_ptr)
895
png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
665
{
896
{
Line 666... Line 897...
666
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
897
   png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
667
 
-
 
668
   if (png_ptr == NULL)
898
 
Line 669... Line -...
669
      return;
-
 
670
 
-
 
671
   png_ptr->transformations |= PNG_EXPAND;
899
   if (!png_rtran_ok(png_ptr, 0))
672
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
900
      return;
673
}
901
 
674
 
902
   png_ptr->transformations |= PNG_EXPAND;
675
 
903
}
Line -... Line 904...
-
 
904
 
-
 
905
/* Expand tRNS chunks to alpha channels. */
-
 
906
void PNGAPI
676
 
907
png_set_tRNS_to_alpha(png_structrp png_ptr)
677
/* Expand tRNS chunks to alpha channels. */
-
 
678
void PNGAPI
908
{
679
png_set_tRNS_to_alpha(png_structp png_ptr)
909
   png_debug(1, "in png_set_tRNS_to_alpha");
Line -... Line 910...
-
 
910
 
-
 
911
   if (!png_rtran_ok(png_ptr, 0))
-
 
912
      return;
-
 
913
 
-
 
914
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-
 
915
}
-
 
916
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
-
 
917
 
-
 
918
#ifdef PNG_READ_EXPAND_16_SUPPORTED
-
 
919
/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
-
 
920
 * it may not work correctly.)
-
 
921
 */
-
 
922
void PNGAPI
-
 
923
png_set_expand_16(png_structrp png_ptr)
-
 
924
{
-
 
925
   png_debug(1, "in png_set_expand_16");
680
{
926
 
681
   png_debug(1, "in png_set_tRNS_to_alpha");
927
   if (!png_rtran_ok(png_ptr, 0))
682
 
928
      return;
683
   png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
929
 
684
   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
930
   png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
Line 685... Line 931...
685
}
931
}
686
#endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
932
#endif
-
 
933
 
687
 
934
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
688
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
935
void PNGAPI
689
void PNGAPI
936
png_set_gray_to_rgb(png_structrp png_ptr)
690
png_set_gray_to_rgb(png_structp png_ptr)
-
 
691
{
-
 
692
   png_debug(1, "in png_set_gray_to_rgb");
937
{
693
 
938
   png_debug(1, "in png_set_gray_to_rgb");
Line 694... Line 939...
694
   if (png_ptr != NULL)
939
 
695
   {
940
   if (!png_rtran_ok(png_ptr, 0))
696
      /* Because rgb must be 8 bits or more: */
941
      return;
697
      png_set_expand_gray_1_2_4_to_8(png_ptr);
942
 
698
      png_ptr->transformations |= PNG_GRAY_TO_RGB;
943
   /* Because rgb must be 8 bits or more: */
699
      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
944
   png_set_expand_gray_1_2_4_to_8(png_ptr);
Line -... Line 945...
-
 
945
   png_ptr->transformations |= PNG_GRAY_TO_RGB;
-
 
946
}
700
   }
947
#endif
701
}
948
 
Line 702... Line 949...
702
#endif
949
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
703
 
950
void PNGFAPI
704
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
951
png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
705
void PNGFAPI
952
    png_fixed_point red, png_fixed_point green)
706
png_set_rgb_to_gray_fixed(png_structp png_ptr, int error_action,
953
{
Line 707... Line 954...
707
    png_fixed_point red, png_fixed_point green)
954
   png_debug(1, "in png_set_rgb_to_gray");
708
{
955
 
709
   png_debug(1, "in png_set_rgb_to_gray");
956
   /* Need the IHDR here because of the check on color_type below. */
Line 710... Line 957...
710
 
957
   /* TODO: fix this */
711
   if (png_ptr == NULL)
958
   if (!png_rtran_ok(png_ptr, 1))
712
      return;
959
      return;
Line 713... Line 960...
713
 
960
 
714
   switch(error_action)
961
   switch(error_action)
715
   {
962
   {
716
      case 1:
963
      case PNG_ERROR_ACTION_NONE:
-
 
964
         png_ptr->transformations |= PNG_RGB_TO_GRAY;
717
         png_ptr->transformations |= PNG_RGB_TO_GRAY;
965
         break;
718
         break;
966
 
719
 
967
      case PNG_ERROR_ACTION_WARN:
720
      case 2:
968
         png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
721
         png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
969
         break;
-
 
970
 
-
 
971
      case PNG_ERROR_ACTION_ERROR:
-
 
972
         png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
722
         break;
973
         break;
723
 
974
 
Line 724... Line 975...
724
      case 3:
975
      default:
725
         png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
976
         png_error(png_ptr, "invalid error action to rgb_to_gray");
726
         break;
977
         break;
727
 
978
   }
728
      default:
-
 
729
         png_error(png_ptr, "invalid error action to rgb_to_gray");
979
 
730
         break;
980
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
731
   }
981
#ifdef PNG_READ_EXPAND_SUPPORTED
732
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-
 
733
#ifdef PNG_READ_EXPAND_SUPPORTED
-
 
Line -... Line 982...
-
 
982
      png_ptr->transformations |= PNG_EXPAND;
-
 
983
#else
-
 
984
   {
734
      png_ptr->transformations |= PNG_EXPAND;
985
      /* Make this an error in 1.6 because otherwise the application may assume
735
#else
986
       * that it just worked and get a memory overwrite.
736
   {
987
       */
737
      png_warning(png_ptr,
988
      png_error(png_ptr,
-
 
989
        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
-
 
990
 
-
 
991
      /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
-
 
992
   }
738
        "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
993
#endif
Line 739... Line 994...
739
 
994
   {
740
      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
995
      if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
741
   }
996
      {
742
#endif
997
         png_uint_16 red_int, green_int;
743
   {
998
 
744
      png_uint_16 red_int, green_int;
-
 
Line -... Line 999...
-
 
999
         /* NOTE: this calculation does not round, but this behavior is retained
-
 
1000
          * for consistency, the inaccuracy is very small.  The code here always
-
 
1001
          * overwrites the coefficients, regardless of whether they have been
-
 
1002
          * defaulted or set already.
-
 
1003
          */
-
 
1004
         red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
745
      if (red < 0 || green < 0)
1005
         green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
746
      {
1006
 
-
 
1007
         png_ptr->rgb_to_gray_red_coeff   = red_int;
747
         red_int   =  6968; /* .212671 * 32768 + .5 */
1008
         png_ptr->rgb_to_gray_green_coeff = green_int;
748
         green_int = 23434; /* .715160 * 32768 + .5 */
1009
         png_ptr->rgb_to_gray_coefficients_set = 1;
-
 
1010
      }
-
 
1011
 
-
 
1012
      else
749
      }
1013
      {
750
 
1014
         if (red >= 0 && green >= 0)
Line 751... Line 1015...
751
      else if (red + green < 100000L)
1015
            png_app_warning(png_ptr,
752
      {
1016
               "ignoring out of range rgb_to_gray coefficients");
753
         red_int = (png_uint_16)(((png_uint_32)red*32768L)/100000L);
1017
 
754
         green_int = (png_uint_16)(((png_uint_32)green*32768L)/100000L);
1018
         /* Use the defaults, from the cHRM chunk if set, else the historical
Line 755... Line 1019...
755
      }
1019
          * values which are close to the sRGB/HDTV/ITU-Rec 709 values.  See
756
 
1020
          * png_do_rgb_to_gray for more discussion of the values.  In this case
757
      else
1021
          * the coefficients are not marked as 'set' and are not overwritten if
758
      {
1022
          * something has already provided a default.
759
         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
-
 
760
         red_int   =  6968;
-
 
761
         green_int = 23434;
-
 
762
      }
1023
          */
763
 
1024
         if (png_ptr->rgb_to_gray_red_coeff == 0 &&
764
      png_ptr->rgb_to_gray_red_coeff   = red_int;
1025
            png_ptr->rgb_to_gray_green_coeff == 0)
765
      png_ptr->rgb_to_gray_green_coeff = green_int;
1026
         {
766
      png_ptr->rgb_to_gray_blue_coeff  =
1027
            png_ptr->rgb_to_gray_red_coeff   = 6968;
Line 767... Line 1028...
767
          (png_uint_16)(32768 - red_int - green_int);
1028
            png_ptr->rgb_to_gray_green_coeff = 23434;
Line 768... Line 1029...
768
   }
1029
            /* png_ptr->rgb_to_gray_blue_coeff  = 2366; */
769
}
1030
         }
770
 
1031
      }
771
#ifdef PNG_FLOATING_POINT_SUPPORTED
1032
   }
772
/* Convert a RGB image to a grayscale of the same width.  This allows us,
1033
}
773
 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
1034
 
774
 */
1035
#ifdef PNG_FLOATING_POINT_SUPPORTED
Line 775... Line -...
775
 
-
 
776
void PNGAPI
-
 
777
png_set_rgb_to_gray(png_structp png_ptr, int error_action, double red,
-
 
778
   double green)
1036
/* Convert a RGB image to a grayscale of the same width.  This allows us,
779
{
1037
 * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
780
   if (png_ptr == NULL)
1038
 */
781
      return;
1039
 
782
 
1040
void PNGAPI
783
   png_set_rgb_to_gray_fixed(png_ptr, error_action,
1041
png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red,
Line -... Line 1042...
-
 
1042
   double green)
-
 
1043
{
-
 
1044
   png_set_rgb_to_gray_fixed(png_ptr, error_action,
-
 
1045
      png_fixed(png_ptr, red, "rgb to gray red coefficient"),
-
 
1046
      png_fixed(png_ptr, green, "rgb to gray green coefficient"));
-
 
1047
}
-
 
1048
#endif /* FLOATING POINT */
-
 
1049
 
-
 
1050
#endif /* RGB_TO_GRAY */
-
 
1051
 
-
 
1052
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
-
 
1053
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
-
 
1054
void PNGAPI
-
 
1055
png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
-
 
1056
    read_user_transform_fn)
-
 
1057
{
-
 
1058
   png_debug(1, "in png_set_read_user_transform_fn");
-
 
1059
 
-
 
1060
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
-
 
1061
   png_ptr->transformations |= PNG_USER_TRANSFORM;
-
 
1062
   png_ptr->read_user_transform_fn = read_user_transform_fn;
-
 
1063
#endif
784
      png_fixed(png_ptr, red, "rgb to gray red coefficient"),
1064
}
785
      png_fixed(png_ptr, green, "rgb to gray green coefficient"));
1065
#endif
786
}
1066
 
-
 
1067
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
-
 
1068
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
1069
/* In the case of gamma transformations only do transformations on images where
-
 
1070
 * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
-
 
1071
 * slows things down slightly, and also needlessly introduces small errors.
-
 
1072
 */
787
#endif /* FLOATING POINT */
1073
static int /* PRIVATE */
788
 
1074
png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
789
#endif
1075
{
-
 
1076
   /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
-
 
1077
    * correction as a difference of the overall transform from 1.0
-
 
1078
    *
-
 
1079
    * We want to compare the threshold with s*f - 1, if we get
-
 
1080
    * overflow here it is because of wacky gamma values so we
-
 
1081
    * turn on processing anyway.
-
 
1082
    */
-
 
1083
   png_fixed_point gtest;
-
 
1084
   return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) ||
790
 
1085
       png_gamma_significant(gtest);
Line -... Line 1086...
-
 
1086
}
791
#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
1087
#endif
-
 
1088
 
-
 
1089
/* Initialize everything needed for the read.  This includes modifying
-
 
1090
 * the palette.
792
    defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1091
 */
-
 
1092
 
793
void PNGAPI
1093
/*For the moment 'png_init_palette_transformations' and
-
 
1094
 * 'png_init_rgb_transformations' only do some flag canceling optimizations.
-
 
1095
 * The intent is that these two routines should have palette or rgb operations
-
 
1096
 * extracted from 'png_init_read_transformations'.
-
 
1097
 */
-
 
1098
static void /* PRIVATE */
-
 
1099
png_init_palette_transformations(png_structrp png_ptr)
-
 
1100
{
-
 
1101
   /* Called to handle the (input) palette case.  In png_do_read_transformations
-
 
1102
    * the first step is to expand the palette if requested, so this code must
-
 
1103
    * take care to only make changes that are invariant with respect to the
-
 
1104
    * palette expansion, or only do them if there is no expansion.
-
 
1105
    *
-
 
1106
    * STRIP_ALPHA has already been handled in the caller (by setting num_trans
-
 
1107
    * to 0.)
-
 
1108
    */
-
 
1109
   int input_has_alpha = 0;
-
 
1110
   int input_has_transparency = 0;
794
png_set_read_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1111
 
-
 
1112
   if (png_ptr->num_trans > 0)
-
 
1113
   {
-
 
1114
      int i;
-
 
1115
 
795
    read_user_transform_fn)
1116
      /* Ignore if all the entries are opaque (unlikely!) */
-
 
1117
      for (i=0; inum_trans; ++i)
796
{
1118
      {
Line 797... Line 1119...
797
   png_debug(1, "in png_set_read_user_transform_fn");
1119
         if (png_ptr->trans_alpha[i] == 255)
-
 
1120
            continue;
-
 
1121
         else if (png_ptr->trans_alpha[i] == 0)
-
 
1122
            input_has_transparency = 1;
-
 
1123
         else
Line 798... Line -...
798
 
-
 
799
   if (png_ptr == NULL)
1124
         {
800
      return;
-
 
801
 
-
 
802
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1125
            input_has_transparency = 1;
803
   png_ptr->transformations |= PNG_USER_TRANSFORM;
-
 
804
   png_ptr->read_user_transform_fn = read_user_transform_fn;
-
 
805
#endif
-
 
806
}
-
 
807
#endif
-
 
808
 
1126
            input_has_alpha = 1;
809
/* Initialize everything needed for the read.  This includes modifying
1127
            break;
810
 * the palette.
1128
         }
811
 */
1129
      }
812
void /* PRIVATE */
-
 
813
png_init_read_transformations(png_structp png_ptr)
1130
   }
-
 
1131
 
-
 
1132
   /* If no alpha we can optimize. */
-
 
1133
   if (!input_has_alpha)
-
 
1134
   {
-
 
1135
      /* Any alpha means background and associative alpha processing is
-
 
1136
       * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
Line 814... Line 1137...
814
{
1137
       * and ENCODE_ALPHA are irrelevant.
815
   png_debug(1, "in png_init_read_transformations");
-
 
816
 
1138
       */
817
  {
-
 
818
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
-
 
819
    defined(PNG_READ_SHIFT_SUPPORTED) || \
1139
      png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
820
    defined(PNG_READ_GAMMA_SUPPORTED)
1140
      png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
1141
 
-
 
1142
      if (!input_has_transparency)
-
 
1143
         png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
-
 
1144
   }
821
   int color_type = png_ptr->color_type;
1145
 
-
 
1146
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
-
 
1147
   /* png_set_background handling - deals with the complexity of whether the
-
 
1148
    * background color is in the file format or the screen format in the case
-
 
1149
    * where an 'expand' will happen.
-
 
1150
    */
822
#endif
1151
 
-
 
1152
   /* The following code cannot be entered in the alpha pre-multiplication case
-
 
1153
    * because PNG_BACKGROUND_EXPAND is cancelled below.
-
 
1154
    */
-
 
1155
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-
 
1156
       (png_ptr->transformations & PNG_EXPAND))
-
 
1157
   {
-
 
1158
      {
-
 
1159
         png_ptr->background.red   =
-
 
1160
             png_ptr->palette[png_ptr->background.index].red;
-
 
1161
         png_ptr->background.green =
-
 
1162
             png_ptr->palette[png_ptr->background.index].green;
-
 
1163
         png_ptr->background.blue  =
-
 
1164
             png_ptr->palette[png_ptr->background.index].blue;
-
 
1165
 
-
 
1166
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
-
 
1167
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
-
 
1168
        {
-
 
1169
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-
 
1170
           {
-
 
1171
              /* Invert the alpha channel (in tRNS) unless the pixels are
-
 
1172
               * going to be expanded, in which case leave it for later
-
 
1173
               */
-
 
1174
              int i, istop = png_ptr->num_trans;
-
 
1175
 
-
 
1176
              for (i=0; i
-
 
1177
                 png_ptr->trans_alpha[i] = (png_byte)(255 -
823
 
1178
                    png_ptr->trans_alpha[i]);
Line -... Line 1179...
-
 
1179
           }
-
 
1180
        }
-
 
1181
#endif /* PNG_READ_INVERT_ALPHA_SUPPORTED */
-
 
1182
      }
-
 
1183
   } /* background expand and (therefore) no alpha association. */
-
 
1184
#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
-
 
1185
}
-
 
1186
 
-
 
1187
static void /* PRIVATE */
-
 
1188
png_init_rgb_transformations(png_structrp png_ptr)
-
 
1189
{
-
 
1190
   /* Added to libpng-1.5.4: check the color type to determine whether there
-
 
1191
    * is any alpha or transparency in the image and simply cancel the
824
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1192
    * background and alpha mode stuff if there isn't.
825
 
1193
    */
-
 
1194
   int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
-
 
1195
   int input_has_transparency = png_ptr->num_trans > 0;
826
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1196
 
827
   /* Detect gray background and attempt to enable optimization
-
 
828
    * for gray --> RGB case
1197
   /* If no alpha we can optimize. */
829
    *
1198
   if (!input_has_alpha)
-
 
1199
   {
-
 
1200
      /* Any alpha means background and associative alpha processing is
-
 
1201
       * required, however if the alpha is 0 or 1 throughout OPTIIMIZE_ALPHA
830
    * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1202
       * and ENCODE_ALPHA are irrelevant.
831
    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1203
       */
832
    * background color might actually be gray yet not be flagged as such.
1204
#     ifdef PNG_READ_ALPHA_MODE_SUPPORTED
833
    * This is not a problem for the current code, which uses
-
 
834
    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
-
 
835
    * png_do_gray_to_rgb() transformation.
-
 
836
    */
-
 
837
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1205
         png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
838
       !(color_type & PNG_COLOR_MASK_COLOR))
1206
         png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
839
   {
-
 
840
      png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
-
 
841
   }
-
 
842
 
1207
#     endif
Line 843... Line 1208...
843
   else if ((png_ptr->transformations & PNG_BACKGROUND) &&
1208
 
844
       !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-
 
845
       (png_ptr->transformations & PNG_GRAY_TO_RGB) &&
-
 
846
       png_ptr->background.red == png_ptr->background.green &&
-
 
847
       png_ptr->background.red == png_ptr->background.blue)
-
 
848
   {
1209
      if (!input_has_transparency)
849
      png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1210
         png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
850
      png_ptr->background.gray = png_ptr->background.red;
-
 
851
   }
-
 
852
#endif
-
 
853
 
1211
   }
Line 854... Line 1212...
854
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
1212
 
855
       (png_ptr->transformations & PNG_EXPAND))
-
 
856
   {
-
 
857
      if (!(color_type & PNG_COLOR_MASK_COLOR))  /* i.e., GRAY or GRAY_ALPHA */
-
 
858
      {
-
 
859
         /* Expand background and tRNS chunks */
1213
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
860
         switch (png_ptr->bit_depth)
1214
   /* png_set_background handling - deals with the complexity of whether the
861
         {
-
 
862
            case 1:
-
 
863
               png_ptr->background.gray *= (png_uint_16)0xff;
-
 
864
               png_ptr->background.red = png_ptr->background.green
1215
    * background color is in the file format or the screen format in the case
Line 865... Line 1216...
865
                   =  png_ptr->background.blue = png_ptr->background.gray;
1216
    * where an 'expand' will happen.
Line 866... Line 1217...
866
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1217
    */
-
 
1218
 
Line 867... Line 1219...
867
               {
1219
   /* The following code cannot be entered in the alpha pre-multiplication case
868
                 png_ptr->trans_color.gray *= (png_uint_16)0xff;
1220
    * because PNG_BACKGROUND_EXPAND is cancelled below.
869
                 png_ptr->trans_color.red = png_ptr->trans_color.green
-
 
870
                     = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1221
    */
871
               }
1222
   if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-
 
1223
       (png_ptr->transformations & PNG_EXPAND) &&
-
 
1224
       !(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
-
 
1225
       /* i.e., GRAY or GRAY_ALPHA */
-
 
1226
   {
-
 
1227
      {
-
 
1228
         /* Expand background and tRNS chunks */
-
 
1229
         int gray = png_ptr->background.gray;
-
 
1230
         int trans_gray = png_ptr->trans_color.gray;
-
 
1231
 
872
               break;
1232
         switch (png_ptr->bit_depth)
873
 
1233
         {
-
 
1234
            case 1:
-
 
1235
               gray *= 0xff;
-
 
1236
               trans_gray *= 0xff;
-
 
1237
               break;
-
 
1238
 
874
            case 2:
1239
            case 2:
875
               png_ptr->background.gray *= (png_uint_16)0x55;
-
 
876
               png_ptr->background.red = png_ptr->background.green
-
 
877
                   = png_ptr->background.blue = png_ptr->background.gray;
-
 
878
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
-
 
879
               {
-
 
880
                  png_ptr->trans_color.gray *= (png_uint_16)0x55;
1240
               gray *= 0x55;
Line -... Line 1241...
-
 
1241
               trans_gray *= 0x55;
-
 
1242
               break;
-
 
1243
 
-
 
1244
            case 4:
-
 
1245
               gray *= 0x11;
-
 
1246
               trans_gray *= 0x11;
-
 
1247
               break;
-
 
1248
 
881
                  png_ptr->trans_color.red = png_ptr->trans_color.green
1249
            default:
-
 
1250
 
-
 
1251
            case 8:
-
 
1252
               /* FALL THROUGH (Already 8 bits) */
-
 
1253
 
882
                      = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1254
            case 16:
-
 
1255
               /* Already a full 16 bits */
883
               }
1256
               break;
-
 
1257
         }
-
 
1258
 
-
 
1259
         png_ptr->background.red = png_ptr->background.green =
884
               break;
1260
            png_ptr->background.blue = (png_uint_16)gray;
-
 
1261
 
885
 
1262
         if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
886
            case 4:
-
 
887
               png_ptr->background.gray *= (png_uint_16)0x11;
1263
         {
-
 
1264
            png_ptr->trans_color.red = png_ptr->trans_color.green =
-
 
1265
               png_ptr->trans_color.blue = (png_uint_16)trans_gray;
-
 
1266
         }
-
 
1267
      }
-
 
1268
   } /* background expand and (therefore) no alpha association. */
888
               png_ptr->background.red = png_ptr->background.green
1269
#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
889
                   = png_ptr->background.blue = png_ptr->background.gray;
1270
}
890
               if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1271
 
891
               {
-
 
892
                  png_ptr->trans_color.gray *= (png_uint_16)0x11;
-
 
893
                  png_ptr->trans_color.red = png_ptr->trans_color.green
-
 
894
                      = png_ptr->trans_color.blue = png_ptr->trans_color.gray;
1272
void /* PRIVATE */
895
               }
-
 
896
               break;
1273
png_init_read_transformations(png_structrp png_ptr)
-
 
1274
{
-
 
1275
   png_debug(1, "in png_init_read_transformations");
-
 
1276
 
-
 
1277
   /* This internal function is called from png_read_start_row in pngrutil.c
-
 
1278
    * and it is called before the 'rowbytes' calculation is done, so the code
-
 
1279
    * in here can change or update the transformations flags.
-
 
1280
    *
-
 
1281
    * First do updates that do not depend on the details of the PNG image data
-
 
1282
    * being processed.
-
 
1283
    */
-
 
1284
 
-
 
1285
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
1286
   /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
-
 
1287
    * png_set_alpha_mode and this is another source for a default file gamma so
-
 
1288
    * the test needs to be performed later - here.  In addition prior to 1.5.4
-
 
1289
    * the tests were repeated for the PALETTE color type here - this is no
-
 
1290
    * longer necessary (and doesn't seem to have been necessary before.)
-
 
1291
    */
-
 
1292
   {
-
 
1293
      /* The following temporary indicates if overall gamma correction is
-
 
1294
       * required.
-
 
1295
       */
-
 
1296
      int gamma_correction = 0;
-
 
1297
 
-
 
1298
      if (png_ptr->colorspace.gamma != 0) /* has been set */
-
 
1299
      {
-
 
1300
         if (png_ptr->screen_gamma != 0) /* screen set too */
-
 
1301
            gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma,
-
 
1302
               png_ptr->screen_gamma);
-
 
1303
 
-
 
1304
         else
-
 
1305
            /* Assume the output matches the input; a long time default behavior
-
 
1306
             * of libpng, although the standard has nothing to say about this.
897
 
1307
             */
898
            default:
1308
            png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma);
Line -... Line 1309...
-
 
1309
      }
-
 
1310
 
-
 
1311
      else if (png_ptr->screen_gamma != 0)
-
 
1312
         /* The converse - assume the file matches the screen, note that this
-
 
1313
          * perhaps undesireable default can (from 1.5.4) be changed by calling
-
 
1314
          * png_set_alpha_mode (even if the alpha handling mode isn't required
-
 
1315
          * or isn't changed from the default.)
-
 
1316
          */
-
 
1317
         png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma);
-
 
1318
 
-
 
1319
      else /* neither are set */
-
 
1320
         /* Just in case the following prevents any processing - file and screen
-
 
1321
          * are both assumed to be linear and there is no way to introduce a
-
 
1322
          * third gamma value other than png_set_background with 'UNIQUE', and,
-
 
1323
          * prior to 1.5.4
-
 
1324
          */
-
 
1325
         png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1;
-
 
1326
 
-
 
1327
      /* We have a gamma value now. */
-
 
1328
      png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
-
 
1329
 
-
 
1330
      /* Now turn the gamma transformation on or off as appropriate.  Notice
-
 
1331
       * that PNG_GAMMA just refers to the file->screen correction.  Alpha
-
 
1332
       * composition may independently cause gamma correction because it needs
-
 
1333
       * linear data (e.g. if the file has a gAMA chunk but the screen gamma
-
 
1334
       * hasn't been specified.)  In any case this flag may get turned off in
-
 
1335
       * the code immediately below if the transform can be handled outside the
-
 
1336
       * row loop.
-
 
1337
       */
-
 
1338
      if (gamma_correction)
-
 
1339
         png_ptr->transformations |= PNG_GAMMA;
-
 
1340
 
-
 
1341
      else
-
 
1342
         png_ptr->transformations &= ~PNG_GAMMA;
-
 
1343
   }
-
 
1344
#endif
-
 
1345
 
-
 
1346
   /* Certain transformations have the effect of preventing other
-
 
1347
    * transformations that happen afterward in png_do_read_transformations,
-
 
1348
    * resolve the interdependencies here.  From the code of
-
 
1349
    * png_do_read_transformations the order is:
-
 
1350
    *
-
 
1351
    *  1) PNG_EXPAND (including PNG_EXPAND_tRNS)
-
 
1352
    *  2) PNG_STRIP_ALPHA (if no compose)
-
 
1353
    *  3) PNG_RGB_TO_GRAY
-
 
1354
    *  4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
-
 
1355
    *  5) PNG_COMPOSE
-
 
1356
    *  6) PNG_GAMMA
-
 
1357
    *  7) PNG_STRIP_ALPHA (if compose)
-
 
1358
    *  8) PNG_ENCODE_ALPHA
899
 
1359
    *  9) PNG_SCALE_16_TO_8
-
 
1360
    * 10) PNG_16_TO_8
-
 
1361
    * 11) PNG_QUANTIZE (converts to palette)
-
 
1362
    * 12) PNG_EXPAND_16
-
 
1363
    * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
-
 
1364
    * 14) PNG_INVERT_MONO
-
 
1365
    * 15) PNG_SHIFT
-
 
1366
    * 16) PNG_PACK
-
 
1367
    * 17) PNG_BGR
-
 
1368
    * 18) PNG_PACKSWAP
-
 
1369
    * 19) PNG_FILLER (includes PNG_ADD_ALPHA)
900
            case 8:
1370
    * 20) PNG_INVERT_ALPHA
901
 
1371
    * 21) PNG_SWAP_ALPHA
Line 902... Line 1372...
902
            case 16:
1372
    * 22) PNG_SWAP_BYTES
-
 
1373
    * 23) PNG_USER_TRANSFORM [must be last]
-
 
1374
    */
-
 
1375
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
903
               png_ptr->background.red = png_ptr->background.green
1376
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
-
 
1377
      !(png_ptr->transformations & PNG_COMPOSE))
904
                   = png_ptr->background.blue = png_ptr->background.gray;
1378
   {
905
               break;
-
 
Line -... Line 1379...
-
 
1379
      /* Stripping the alpha channel happens immediately after the 'expand'
-
 
1380
       * transformations, before all other transformation, so it cancels out
-
 
1381
       * the alpha handling.  It has the side effect negating the effect of
-
 
1382
       * PNG_EXPAND_tRNS too:
-
 
1383
       */
-
 
1384
      png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
-
 
1385
         PNG_EXPAND_tRNS);
-
 
1386
      png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
1387
 
-
 
1388
      /* Kill the tRNS chunk itself too.  Prior to 1.5.4 this did not happen
-
 
1389
       * so transparency information would remain just so long as it wasn't
-
 
1390
       * expanded.  This produces unexpected API changes if the set of things
-
 
1391
       * that do PNG_EXPAND_tRNS changes (perfectly possible given the
-
 
1392
       * documentation - which says ask for what you want, accept what you
-
 
1393
       * get.)  This makes the behavior consistent from 1.5.4:
-
 
1394
       */
-
 
1395
      png_ptr->num_trans = 0;
-
 
1396
   }
-
 
1397
#endif /* STRIP_ALPHA supported, no COMPOSE */
-
 
1398
 
-
 
1399
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
-
 
1400
   /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
906
         }
1401
    * settings will have no effect.
-
 
1402
    */
-
 
1403
   if (!png_gamma_significant(png_ptr->screen_gamma))
-
 
1404
   {
-
 
1405
      png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
-
 
1406
      png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
-
 
1407
   }
907
      }
1408
#endif
-
 
1409
 
-
 
1410
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
-
 
1411
   /* Make sure the coefficients for the rgb to gray conversion are set
-
 
1412
    * appropriately.
908
      else if (color_type == PNG_COLOR_TYPE_PALETTE)
1413
    */
909
      {
1414
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
910
         png_ptr->background.red   =
-
 
911
             png_ptr->palette[png_ptr->background.index].red;
1415
      png_colorspace_set_rgb_coefficients(png_ptr);
912
         png_ptr->background.green =
1416
#endif
913
             png_ptr->palette[png_ptr->background.index].green;
1417
 
914
         png_ptr->background.blue  =
1418
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
-
 
1419
#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
915
             png_ptr->palette[png_ptr->background.index].blue;
1420
   /* Detect gray background and attempt to enable optimization for
916
 
-
 
917
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
-
 
918
        if (png_ptr->transformations & PNG_INVERT_ALPHA)
1421
    * gray --> RGB case.
-
 
1422
    *
-
 
1423
    * Note:  if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
Line -... Line 1424...
-
 
1424
    * RGB_ALPHA (in which case need_expand is superfluous anyway), the
-
 
1425
    * background color might actually be gray yet not be flagged as such.
-
 
1426
    * This is not a problem for the current code, which uses
-
 
1427
    * PNG_BACKGROUND_IS_GRAY only to decide when to do the
-
 
1428
    * png_do_gray_to_rgb() transformation.
-
 
1429
    *
-
 
1430
    * TODO: this code needs to be revised to avoid the complexity and
-
 
1431
    * interdependencies.  The color type of the background should be recorded in
-
 
1432
    * png_set_background, along with the bit depth, then the code has a record
-
 
1433
    * of exactly what color space the background is currently in.
-
 
1434
    */
-
 
1435
   if (png_ptr->transformations & PNG_BACKGROUND_EXPAND)
-
 
1436
   {
-
 
1437
      /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
-
 
1438
       * the file was grayscale the background value is gray.
-
 
1439
       */
-
 
1440
      if (!(png_ptr->color_type & PNG_COLOR_MASK_COLOR))
-
 
1441
         png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
-
 
1442
   }
-
 
1443
 
-
 
1444
   else if (png_ptr->transformations & PNG_COMPOSE)
-
 
1445
   {
-
 
1446
      /* PNG_COMPOSE: png_set_background was called with need_expand false,
-
 
1447
       * so the color is in the color space of the output or png_set_alpha_mode
-
 
1448
       * was called and the color is black.  Ignore RGB_TO_GRAY because that
-
 
1449
       * happens before GRAY_TO_RGB.
-
 
1450
       */
-
 
1451
      if (png_ptr->transformations & PNG_GRAY_TO_RGB)
-
 
1452
      {
-
 
1453
         if (png_ptr->background.red == png_ptr->background.green &&
-
 
1454
             png_ptr->background.red == png_ptr->background.blue)
-
 
1455
         {
-
 
1456
            png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
-
 
1457
            png_ptr->background.gray = png_ptr->background.red;
-
 
1458
         }
-
 
1459
      }
-
 
1460
   }
-
 
1461
#endif /* PNG_READ_EXPAND_SUPPORTED && PNG_READ_BACKGROUND_SUPPORTED */
-
 
1462
#endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
-
 
1463
 
-
 
1464
   /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
-
 
1465
    * can be performed directly on the palette, and some (such as rgb to gray)
-
 
1466
    * can be optimized inside the palette.  This is particularly true of the
-
 
1467
    * composite (background and alpha) stuff, which can be pretty much all done
-
 
1468
    * in the palette even if the result is expanded to RGB or gray afterward.
-
 
1469
    *
919
        {
1470
    * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
-
 
1471
    * earlier and the palette stuff is actually handled on the first row.  This
-
 
1472
    * leads to the reported bug that the palette returned by png_get_PLTE is not
920
#ifdef PNG_READ_EXPAND_SUPPORTED
1473
    * updated.
-
 
1474
    */
-
 
1475
   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
-
 
1476
      png_init_palette_transformations(png_ptr);
-
 
1477
 
-
 
1478
   else
-
 
1479
      png_init_rgb_transformations(png_ptr);
-
 
1480
 
-
 
1481
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
-
 
1482
   defined(PNG_READ_EXPAND_16_SUPPORTED)
-
 
1483
   if ((png_ptr->transformations & PNG_EXPAND_16) &&
-
 
1484
      (png_ptr->transformations & PNG_COMPOSE) &&
-
 
1485
      !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-
 
1486
      png_ptr->bit_depth != 16)
-
 
1487
   {
-
 
1488
      /* TODO: fix this.  Because the expand_16 operation is after the compose
-
 
1489
       * handling the background color must be 8, not 16, bits deep, but the
-
 
1490
       * application will supply a 16-bit value so reduce it here.
-
 
1491
       *
-
 
1492
       * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
-
 
1493
       * present, so that case is ok (until do_expand_16 is moved.)
-
 
1494
       *
-
 
1495
       * NOTE: this discards the low 16 bits of the user supplied background
-
 
1496
       * color, but until expand_16 works properly there is no choice!
-
 
1497
       */
-
 
1498
#     define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
-
 
1499
      CHOP(png_ptr->background.red);
-
 
1500
      CHOP(png_ptr->background.green);
-
 
1501
      CHOP(png_ptr->background.blue);
-
 
1502
      CHOP(png_ptr->background.gray);
-
 
1503
#     undef CHOP
-
 
1504
   }
-
 
1505
#endif /* PNG_READ_BACKGROUND_SUPPORTED && PNG_READ_EXPAND_16_SUPPORTED */
-
 
1506
 
-
 
1507
#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
-
 
1508
   (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
-
 
1509
   defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
-
 
1510
   if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) &&
-
 
1511
      (png_ptr->transformations & PNG_COMPOSE) &&
-
 
1512
      !(png_ptr->transformations & PNG_BACKGROUND_EXPAND) &&
-
 
1513
      png_ptr->bit_depth == 16)
-
 
1514
   {
-
 
1515
      /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
-
 
1516
       * component this will also happen after PNG_COMPOSE and so the background
-
 
1517
       * color must be pre-expanded here.
-
 
1518
       *
-
 
1519
       * TODO: fix this too.
-
 
1520
       */
-
 
1521
      png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
-
 
1522
      png_ptr->background.green =
-
 
1523
         (png_uint_16)(png_ptr->background.green * 257);
-
 
1524
      png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
-
 
1525
      png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
-
 
1526
   }
-
 
1527
#endif
-
 
1528
 
-
 
1529
   /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
921
           if (!(png_ptr->transformations & PNG_EXPAND_tRNS))
1530
    * background support (see the comments in scripts/pnglibconf.dfa), this
922
#endif
1531
    * allows pre-multiplication of the alpha channel to be implemented as
Line 923... Line 1532...
923
           {
1532
    * compositing on black.  This is probably sub-optimal and has been done in
924
              /* Invert the alpha channel (in tRNS) unless the pixels are
1533
    * 1.5.4 betas simply to enable external critique and testing (i.e. to
925
               * going to be expanded, in which case leave it for later
1534
    * implement the new API quickly, without lots of internal changes.)
-
 
1535
    */
-
 
1536
 
-
 
1537
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
1538
#  ifdef PNG_READ_BACKGROUND_SUPPORTED
-
 
1539
      /* Includes ALPHA_MODE */
-
 
1540
      png_ptr->background_1 = png_ptr->background;
-
 
1541
#  endif
-
 
1542
 
-
 
1543
   /* This needs to change - in the palette image case a whole set of tables are
-
 
1544
    * built when it would be quicker to just calculate the correct value for
926
               */
1545
    * each palette entry directly.  Also, the test is too tricky - why check
927
              int i, istop;
1546
    * PNG_RGB_TO_GRAY if PNG_GAMMA is not set?  The answer seems to be that
-
 
1547
    * PNG_GAMMA is cancelled even if the gamma is known?  The test excludes the
928
              istop=(int)png_ptr->num_trans;
1548
    * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
-
 
1549
    * the gamma tables will not be built even if composition is required on a
929
              for (i=0; i
1550
    * gamma encoded value.
930
                 png_ptr->trans_alpha[i] = (png_byte)(255 -
1551
    *
931
                    png_ptr->trans_alpha[i]);
1552
    * In 1.5.4 this is addressed below by an additional check on the individual
932
           }
1553
    * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
933
        }
1554
    * tables.
Line 993... Line 1614...
993
                     gs = PNG_FP_1;
1614
                     gs = PNG_FP_1;
994
                     break;
1615
                     break;
995
 
1616
 
Line 996... Line 1617...
996
                  case PNG_BACKGROUND_GAMMA_FILE:
1617
                  case PNG_BACKGROUND_GAMMA_FILE:
997
                     g = png_reciprocal(png_ptr->gamma);
1618
                     g = png_reciprocal(png_ptr->colorspace.gamma);
998
                     gs = png_reciprocal2(png_ptr->gamma,
1619
                     gs = png_reciprocal2(png_ptr->colorspace.gamma,
999
                        png_ptr->screen_gamma);
1620
                        png_ptr->screen_gamma);
1000
                     break;
1621
                     break;
Line 1001... Line 1622...
1001
 
1622
 
1002
                  case PNG_BACKGROUND_GAMMA_UNIQUE:
1623
                  case PNG_BACKGROUND_GAMMA_UNIQUE:
Line 1011... Line 1632...
1011
               }
1632
               }
1012
 
1633
 
Line 1013... Line 1634...
1013
               if (png_gamma_significant(gs))
1634
               if (png_gamma_significant(gs))
1014
               {
1635
               {
1015
                  back.red   = (png_byte)png_ptr->background.red;
-
 
1016
                  back.green = (png_byte)png_ptr->background.green;
-
 
1017
                  back.blue  = (png_byte)png_ptr->background.blue;
-
 
1018
               }
-
 
1019
 
-
 
1020
               else
-
 
1021
               {
-
 
1022
                  back.red = png_gamma_8bit_correct(png_ptr->background.red,
1636
                  back.red = png_gamma_8bit_correct(png_ptr->background.red,
1023
                      gs);
1637
                      gs);
1024
                  back.green = png_gamma_8bit_correct(png_ptr->background.green,
1638
                  back.green = png_gamma_8bit_correct(png_ptr->background.green,
1025
                      gs);
1639
                      gs);
1026
                  back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1640
                  back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1027
                      gs);
1641
                      gs);
1028
               }
1642
               }
-
 
1643
 
-
 
1644
               else
-
 
1645
               {
1029
               back_1.red = png_gamma_8bit_correct(png_ptr->background.red, g);
1646
                  back.red   = (png_byte)png_ptr->background.red;
-
 
1647
                  back.green = (png_byte)png_ptr->background.green;
-
 
1648
                  back.blue  = (png_byte)png_ptr->background.blue;
-
 
1649
               }
-
 
1650
 
-
 
1651
               if (png_gamma_significant(g))
-
 
1652
               {
1030
               back_1.green = png_gamma_8bit_correct(png_ptr->background.green,
1653
                  back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1031
                   g);
1654
                     g);
-
 
1655
                  back_1.green = png_gamma_8bit_correct(
-
 
1656
                     png_ptr->background.green, g);
1032
               back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1657
                  back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1033
                   g);
1658
                     g);
1034
            }
1659
               }
-
 
1660
 
-
 
1661
               else
-
 
1662
               {
-
 
1663
                  back_1.red   = (png_byte)png_ptr->background.red;
-
 
1664
                  back_1.green = (png_byte)png_ptr->background.green;
-
 
1665
                  back_1.blue  = (png_byte)png_ptr->background.blue;
-
 
1666
               }
-
 
1667
            }
-
 
1668
 
1035
            for (i = 0; i < num_palette; i++)
1669
            for (i = 0; i < num_palette; i++)
1036
            {
1670
            {
1037
               if (i < (int)png_ptr->num_trans &&
1671
               if (i < (int)png_ptr->num_trans &&
1038
                   png_ptr->trans_alpha[i] != 0xff)
1672
                   png_ptr->trans_alpha[i] != 0xff)
1039
               {
1673
               {
Line 1064... Line 1698...
1064
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
1698
                  palette[i].green = png_ptr->gamma_table[palette[i].green];
1065
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1699
                  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1066
               }
1700
               }
1067
            }
1701
            }
1068
            /* Prevent the transformations being done again, and make sure
1702
 
-
 
1703
            /* Prevent the transformations being done again.
1069
             * that the now spurious alpha channel is stripped - the code
1704
             *
-
 
1705
             * NOTE: this is highly dubious; it removes the transformations in
1070
             * has just reduced background composition and gamma correction
1706
             * place.  This seems inconsistent with the general treatment of the
1071
             * to a simple alpha channel strip.
1707
             * transformations elsewhere.
1072
             */
1708
             */
1073
            png_ptr->transformations &= ~PNG_BACKGROUND;
1709
            png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1074
            png_ptr->transformations &= ~PNG_GAMMA;
-
 
1075
            png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
1710
         } /* color_type == PNG_COLOR_TYPE_PALETTE */
1076
         }
1711
 
1077
 
-
 
Line 1078... Line 1712...
1078
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1712
         /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1079
         else
-
 
1080
         /* color_type != PNG_COLOR_TYPE_PALETTE */
1713
         else /* color_type != PNG_COLOR_TYPE_PALETTE */
1081
         {
1714
         {
-
 
1715
            int gs_sig, g_sig;
1082
            png_fixed_point g = PNG_FP_1;
1716
            png_fixed_point g = PNG_FP_1;  /* Correction to linear */
1083
            png_fixed_point gs = PNG_FP_1;
1717
            png_fixed_point gs = PNG_FP_1; /* Correction to screen */
Line 1084... Line 1718...
1084
 
1718
 
1085
            switch (png_ptr->background_gamma_type)
1719
            switch (png_ptr->background_gamma_type)
1086
            {
1720
            {
1087
               case PNG_BACKGROUND_GAMMA_SCREEN:
1721
               case PNG_BACKGROUND_GAMMA_SCREEN:
1088
                  g = png_ptr->screen_gamma;
1722
                  g = png_ptr->screen_gamma;
1089
                  /* gs = PNG_FP_1; */
1723
                  /* gs = PNG_FP_1; */
Line 1090... Line 1724...
1090
                  break;
1724
                  break;
1091
 
1725
 
1092
               case PNG_BACKGROUND_GAMMA_FILE:
1726
               case PNG_BACKGROUND_GAMMA_FILE:
-
 
1727
                  g = png_reciprocal(png_ptr->colorspace.gamma);
1093
                  g = png_reciprocal(png_ptr->gamma);
1728
                  gs = png_reciprocal2(png_ptr->colorspace.gamma,
Line 1094... Line 1729...
1094
                  gs = png_reciprocal2(png_ptr->gamma, png_ptr->screen_gamma);
1729
                     png_ptr->screen_gamma);
1095
                  break;
1730
                  break;
1096
 
1731
 
Line 1103... Line 1738...
1103
               default:
1738
               default:
1104
                  png_error(png_ptr, "invalid background gamma type");
1739
                  png_error(png_ptr, "invalid background gamma type");
1105
            }
1740
            }
Line -... Line 1741...
-
 
1741
 
-
 
1742
            g_sig = png_gamma_significant(g);
-
 
1743
            gs_sig = png_gamma_significant(gs);
-
 
1744
 
1106
 
1745
            if (g_sig)
1107
            png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1746
               png_ptr->background_1.gray = png_gamma_correct(png_ptr,
Line -... Line 1747...
-
 
1747
                   png_ptr->background.gray, g);
1108
                png_ptr->background.gray, g);
1748
 
1109
 
1749
            if (gs_sig)
Line 1110... Line 1750...
1110
            png_ptr->background.gray = png_gamma_correct(png_ptr,
1750
               png_ptr->background.gray = png_gamma_correct(png_ptr,
1111
                png_ptr->background.gray, gs);
1751
                   png_ptr->background.gray, gs);
1112
 
1752
 
1113
            if ((png_ptr->background.red != png_ptr->background.green) ||
1753
            if ((png_ptr->background.red != png_ptr->background.green) ||
1114
                (png_ptr->background.red != png_ptr->background.blue) ||
1754
                (png_ptr->background.red != png_ptr->background.blue) ||
-
 
1755
                (png_ptr->background.red != png_ptr->background.gray))
-
 
1756
            {
1115
                (png_ptr->background.red != png_ptr->background.gray))
1757
               /* RGB or RGBA with color background */
1116
            {
1758
               if (g_sig)
Line 1117... Line 1759...
1117
               /* RGB or RGBA with color background */
1759
               {
1118
               png_ptr->background_1.red = png_gamma_correct(png_ptr,
1760
                  png_ptr->background_1.red = png_gamma_correct(png_ptr,
Line 1119... Line 1761...
1119
                   png_ptr->background.red, g);
1761
                      png_ptr->background.red, g);
1120
 
1762
 
-
 
1763
                  png_ptr->background_1.green = png_gamma_correct(png_ptr,
Line -... Line 1764...
-
 
1764
                      png_ptr->background.green, g);
-
 
1765
 
1121
               png_ptr->background_1.green = png_gamma_correct(png_ptr,
1766
                  png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1122
                   png_ptr->background.green, g);
1767
                      png_ptr->background.blue, g);
Line 1123... Line 1768...
1123
 
1768
               }
1124
               png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1769
 
Line 1125... Line 1770...
1125
                   png_ptr->background.blue, g);
1770
               if (gs_sig)
1126
 
1771
               {
1127
               png_ptr->background.red = png_gamma_correct(png_ptr,
1772
                  png_ptr->background.red = png_gamma_correct(png_ptr,
-
 
1773
                      png_ptr->background.red, gs);
Line 1128... Line 1774...
1128
                   png_ptr->background.red, gs);
1774
 
1129
 
1775
                  png_ptr->background.green = png_gamma_correct(png_ptr,
1130
               png_ptr->background.green = png_gamma_correct(png_ptr,
1776
                      png_ptr->background.green, gs);
1131
                   png_ptr->background.green, gs);
1777
 
1132
 
1778
                  png_ptr->background.blue = png_gamma_correct(png_ptr,
Line 1133... Line 1779...
1133
               png_ptr->background.blue = png_gamma_correct(png_ptr,
1779
                      png_ptr->background.blue, gs);
1134
                   png_ptr->background.blue, gs);
1780
               }
1135
            }
1781
            }
-
 
1782
 
-
 
1783
            else
-
 
1784
            {
1136
 
1785
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
-
 
1786
               png_ptr->background_1.red = png_ptr->background_1.green
1137
            else
1787
                   = png_ptr->background_1.blue = png_ptr->background_1.gray;
1138
            {
1788
 
1139
               /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1789
               png_ptr->background.red = png_ptr->background.green
1140
               png_ptr->background_1.red = png_ptr->background_1.green
1790
                   = png_ptr->background.blue = png_ptr->background.gray;
1141
                   = png_ptr->background_1.blue = png_ptr->background_1.gray;
1791
            }
-
 
1792
 
-
 
1793
            /* The background is now in screen gamma: */
-
 
1794
            png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
-
 
1795
         } /* color_type != PNG_COLOR_TYPE_PALETTE */
-
 
1796
      }/* png_ptr->transformations & PNG_BACKGROUND */
-
 
1797
 
1142
 
1798
      else
1143
               png_ptr->background.red = png_ptr->background.green
1799
      /* Transformation does not include PNG_BACKGROUND */
1144
                   = png_ptr->background.blue = png_ptr->background.gray;
1800
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1145
            }
1801
      if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
Line -... Line 1802...
-
 
1802
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
-
 
1803
         /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
-
 
1804
         && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1146
         }
1805
         (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1147
      }
1806
#endif
1148
      else
1807
         )
1149
      /* Transformation does not include PNG_BACKGROUND */
1808
      {
1150
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1809
         png_colorp palette = png_ptr->palette;
1151
      if (color_type == PNG_COLOR_TYPE_PALETTE)
1810
         int num_palette = png_ptr->num_palette;
Line 1152... Line 1811...
1152
      {
1811
         int i;
1153
         png_colorp palette = png_ptr->palette;
1812
 
1154
         int num_palette = png_ptr->num_palette;
1813
         /* NOTE: there are other transformations that should probably be in
1155
         int i;
1814
          * here too.
1156
 
1815
          */
1157
         for (i = 0; i < num_palette; i++)
1816
         for (i = 0; i < num_palette; i++)
1158
         {
1817
         {
1159
            palette[i].red = png_ptr->gamma_table[palette[i].red];
1818
            palette[i].red = png_ptr->gamma_table[palette[i].red];
-
 
1819
            palette[i].green = png_ptr->gamma_table[palette[i].green];
1160
            palette[i].green = png_ptr->gamma_table[palette[i].green];
1820
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1161
            palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1821
         }
1162
         }
1822
 
1163
 
1823
         /* Done the gamma correction. */
1164
         /* Done the gamma correction. */
1824
         png_ptr->transformations &= ~PNG_GAMMA;
1165
         png_ptr->transformations &= ~PNG_GAMMA;
1825
      } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1166
      }
1826
   }
1167
   }
1827
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1168
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1828
   else
Line 1203... Line 1863...
1203
                png_ptr->trans_alpha[i], back.blue);
1863
                png_ptr->trans_alpha[i], back.blue);
1204
         }
1864
         }
1205
      }
1865
      }
1206
 
1866
 
Line 1207... Line -...
1207
      /* Handled alpha, still need to strip the channel. */
-
 
1208
      png_ptr->transformations &= ~PNG_BACKGROUND;
1867
      png_ptr->transformations &= ~PNG_COMPOSE;
1209
      png_ptr->flags |= PNG_FLAG_STRIP_ALPHA;
-
 
1210
   }
1868
   }
1211
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
1869
#endif /* PNG_READ_BACKGROUND_SUPPORTED */
Line 1212... Line 1870...
1212
 
1870
 
1213
#ifdef PNG_READ_SHIFT_SUPPORTED
1871
#ifdef PNG_READ_SHIFT_SUPPORTED
-
 
1872
   if ((png_ptr->transformations & PNG_SHIFT) &&
1214
   if ((png_ptr->transformations & PNG_SHIFT) &&
1873
      !(png_ptr->transformations & PNG_EXPAND) &&
1215
       (color_type == PNG_COLOR_TYPE_PALETTE))
1874
       (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1216
   {
1875
   {
1217
      png_uint_16 i;
1876
      int i;
1218
      png_uint_16 istop = png_ptr->num_palette;
1877
      int istop = png_ptr->num_palette;
1219
      int sr = 8 - png_ptr->sig_bit.red;
-
 
1220
      int sg = 8 - png_ptr->sig_bit.green;
-
 
Line 1221... Line 1878...
1221
      int sb = 8 - png_ptr->sig_bit.blue;
1878
      int shift = 8 - png_ptr->sig_bit.red;
1222
 
-
 
Line -... Line 1879...
-
 
1879
 
-
 
1880
      png_ptr->transformations &= ~PNG_SHIFT;
-
 
1881
 
-
 
1882
      /* significant bits can be in the range 1 to 7 for a meaninful result, if
1223
      if (sr < 0 || sr > 8)
1883
       * the number of significant bits is 0 then no shift is done (this is an
-
 
1884
       * error condition which is silently ignored.)
1224
         sr = 0;
1885
       */
-
 
1886
      if (shift > 0 && shift < 8)
Line 1225... Line 1887...
1225
 
1887
         for (i=0; i
-
 
1888
         {
1226
      if (sg < 0 || sg > 8)
1889
            int component = png_ptr->palette[i].red;
Line -... Line 1890...
-
 
1890
 
-
 
1891
            component >>= shift;
1227
         sg = 0;
1892
            png_ptr->palette[i].red = (png_byte)component;
-
 
1893
         }
-
 
1894
 
-
 
1895
      shift = 8 - png_ptr->sig_bit.green;
-
 
1896
      if (shift > 0 && shift < 8)
-
 
1897
         for (i=0; i
-
 
1898
         {
-
 
1899
            int component = png_ptr->palette[i].green;
-
 
1900
 
-
 
1901
            component >>= shift;
-
 
1902
            png_ptr->palette[i].green = (png_byte)component;
1228
 
1903
         }
1229
      if (sb < 0 || sb > 8)
1904
 
-
 
1905
      shift = 8 - png_ptr->sig_bit.blue;
1230
         sb = 0;
1906
      if (shift > 0 && shift < 8)
1231
 
1907
         for (i=0; i
1232
      for (i = 0; i < istop; i++)
1908
         {
1233
      {
1909
            int component = png_ptr->palette[i].blue;
1234
         png_ptr->palette[i].red >>= sr;
1910
 
1235
         png_ptr->palette[i].green >>= sg;
1911
            component >>= shift;
1236
         png_ptr->palette[i].blue >>= sb;
-
 
1237
      }
-
 
1238
   }
-
 
1239
#endif  /* PNG_READ_SHIFT_SUPPORTED */
-
 
1240
 }
-
 
1241
#if !defined(PNG_READ_GAMMA_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) \
-
 
Line 1242... Line 1912...
1242
 && !defined(PNG_READ_BACKGROUND_SUPPORTED)
1912
            png_ptr->palette[i].blue = (png_byte)component;
1243
   if (png_ptr)
1913
         }
1244
      return;
1914
   }
1245
#endif
1915
#endif  /* PNG_READ_SHIFT_SUPPORTED */
1246
}
1916
}
1247
 
1917
 
1248
/* Modify the info structure to reflect the transformations.  The
1918
/* Modify the info structure to reflect the transformations.  The
1249
 * info should be updated so a PNG file could be written with it,
1919
 * info should be updated so a PNG file could be written with it,
Line 1250... Line 1920...
1250
 * assuming the transformations result in valid PNG data.
1920
 * assuming the transformations result in valid PNG data.
1251
 */
1921
 */
1252
void /* PRIVATE */
1922
void /* PRIVATE */
1253
png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
1923
png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
1254
{
1924
{
1255
   png_debug(1, "in png_read_transform_info");
1925
   png_debug(1, "in png_read_transform_info");
-
 
1926
 
-
 
1927
#ifdef PNG_READ_EXPAND_SUPPORTED
-
 
1928
   if (png_ptr->transformations & PNG_EXPAND)
1256
 
1929
   {
1257
#ifdef PNG_READ_EXPAND_SUPPORTED
1930
      if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
Line 1258... Line 1931...
1258
   if (png_ptr->transformations & PNG_EXPAND)
1931
      {
1259
   {
1932
         /* This check must match what actually happens in
Line 1283... Line 1956...
1283
      }
1956
      }
1284
   }
1957
   }
1285
#endif
1958
#endif
1286
 
1959
 
Line 1287... Line 1960...
1287
#ifdef PNG_READ_BACKGROUND_SUPPORTED
1960
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1288
   if (png_ptr->transformations & PNG_BACKGROUND)
1961
   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1289
   {
-
 
1290
      info_ptr->color_type = (png_byte)(info_ptr->color_type &
1962
   /* The following is almost certainly wrong unless the background value is in
1291
          ~PNG_COLOR_MASK_ALPHA);
1963
    * the screen space!
-
 
1964
    */
1292
      info_ptr->num_trans = 0;
1965
   if (png_ptr->transformations & PNG_COMPOSE)
1293
      info_ptr->background = png_ptr->background;
1966
      info_ptr->background = png_ptr->background;
1294
   }
-
 
1295
#endif
1967
#endif
Line 1296... Line 1968...
1296
 
1968
 
1297
#ifdef PNG_READ_GAMMA_SUPPORTED
1969
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
1970
   /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
-
 
1971
    * however it seems that the code in png_init_read_transformations, which has
-
 
1972
    * been called before this from png_read_update_info->png_read_start_row
1298
   if (png_ptr->transformations & PNG_GAMMA)
1973
    * sometimes does the gamma transform and cancels the flag.
-
 
1974
    *
-
 
1975
    * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1299
   {
1976
    * the screen_gamma value.  The following probably results in weirdness if
1300
      info_ptr->gamma = png_ptr->gamma;
1977
    * the info_ptr is used by the app after the rows have been read.
-
 
1978
    */
1301
   }
1979
   info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
Line 1302... Line 1980...
1302
#endif
1980
#endif
-
 
1981
 
1303
 
1982
   if (info_ptr->bit_depth == 16)
-
 
1983
   {
1304
#ifdef PNG_READ_16_TO_8_SUPPORTED
1984
#  ifdef PNG_READ_16BIT_SUPPORTED
1305
#ifdef PNG_READ_16BIT_SUPPORTED
1985
#     ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
-
 
1986
         if (png_ptr->transformations & PNG_SCALE_16_TO_8)
-
 
1987
            info_ptr->bit_depth = 8;
-
 
1988
#     endif
-
 
1989
 
-
 
1990
#     ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
-
 
1991
         if (png_ptr->transformations & PNG_16_TO_8)
-
 
1992
            info_ptr->bit_depth = 8;
1306
   if ((png_ptr->transformations & PNG_16_TO_8) && (info_ptr->bit_depth == 16))
1993
#     endif
1307
      info_ptr->bit_depth = 8;
1994
 
-
 
1995
#  else
1308
#else
1996
      /* No 16 bit support: force chopping 16-bit input down to 8, in this case
1309
   /* Force chopping 16-bit input down to 8 */
1997
       * the app program can chose if both APIs are available by setting the
-
 
1998
       * correct scaling to use.
-
 
1999
       */
-
 
2000
#     ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
-
 
2001
         /* For compatibility with previous versions use the strip method by
-
 
2002
          * default.  This code works because if PNG_SCALE_16_TO_8 is already
1310
   if (info_ptr->bit_depth == 16)
2003
          * set the code below will do that in preference to the chop.
1311
   {
2004
          */
1312
      png_ptr->transformations |=PNG_16_TO_8;
2005
         png_ptr->transformations |= PNG_16_TO_8;
-
 
2006
         info_ptr->bit_depth = 8;
-
 
2007
#     else
-
 
2008
 
-
 
2009
#        ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
-
 
2010
            png_ptr->transformations |= PNG_SCALE_16_TO_8;
-
 
2011
            info_ptr->bit_depth = 8;
-
 
2012
#        else
1313
      info_ptr->bit_depth = 8;
2013
 
1314
   }
2014
            CONFIGURATION ERROR: you must enable at least one 16 to 8 method
-
 
2015
#        endif
-
 
2016
#    endif
Line 1315... Line 2017...
1315
#endif
2017
#endif /* !READ_16BIT_SUPPORTED */
1316
#endif
2018
   }
1317
 
2019
 
-
 
2020
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1318
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2021
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
Line 1319... Line 2022...
1319
   if (png_ptr->transformations & PNG_GRAY_TO_RGB)
2022
      info_ptr->color_type = (png_byte)(info_ptr->color_type |
1320
      info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
2023
         PNG_COLOR_MASK_COLOR);
-
 
2024
#endif
1321
#endif
2025
 
1322
 
2026
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
Line 1323... Line 2027...
1323
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2027
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
1324
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
2028
      info_ptr->color_type = (png_byte)(info_ptr->color_type &
1325
      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
2029
         ~PNG_COLOR_MASK_COLOR);
Line 1336... Line 2040...
1336
      }
2040
      }
1337
   }
2041
   }
1338
#endif
2042
#endif
1339
 
2043
 
Line -... Line 2044...
-
 
2044
#ifdef PNG_READ_EXPAND_16_SUPPORTED
-
 
2045
   if (png_ptr->transformations & PNG_EXPAND_16 && info_ptr->bit_depth == 8 &&
-
 
2046
      info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
-
 
2047
   {
-
 
2048
      info_ptr->bit_depth = 16;
-
 
2049
   }
-
 
2050
#endif
-
 
2051
 
1340
#ifdef PNG_READ_PACK_SUPPORTED
2052
#ifdef PNG_READ_PACK_SUPPORTED
1341
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
2053
   if ((png_ptr->transformations & PNG_PACK) && (info_ptr->bit_depth < 8))
1342
      info_ptr->bit_depth = 8;
2054
      info_ptr->bit_depth = 8;
1343
#endif
2055
#endif
Line 1351... Line 2063...
1351
   else
2063
   else
1352
      info_ptr->channels = 1;
2064
      info_ptr->channels = 1;
Line 1353... Line 2065...
1353
 
2065
 
1354
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2066
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
-
 
2067
   if (png_ptr->transformations & PNG_STRIP_ALPHA)
-
 
2068
   {
1355
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
2069
      info_ptr->color_type = (png_byte)(info_ptr->color_type &
-
 
2070
         ~PNG_COLOR_MASK_ALPHA);
-
 
2071
      info_ptr->num_trans = 0;
1356
      info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
2072
   }
Line 1357... Line 2073...
1357
#endif
2073
#endif
1358
 
2074
 
Line 1388... Line 2104...
1388
       info_ptr->bit_depth);
2104
       info_ptr->bit_depth);
1389
 
2105
 
Line 1390... Line 2106...
1390
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
2106
   info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
Line -... Line 2107...
-
 
2107
 
-
 
2108
   /* Adding in 1.5.4: cache the above value in png_struct so that we can later
-
 
2109
    * check in png_rowbytes that the user buffer won't get overwritten.  Note
-
 
2110
    * that the field is not always set - if png_read_update_info isn't called
-
 
2111
    * the application has to either not do any transforms or get the calculation
-
 
2112
    * right itself.
-
 
2113
    */
-
 
2114
   png_ptr->info_rowbytes = info_ptr->rowbytes;
1391
 
2115
 
1392
#ifndef PNG_READ_EXPAND_SUPPORTED
2116
#ifndef PNG_READ_EXPAND_SUPPORTED
1393
   if (png_ptr)
2117
   if (png_ptr)
1394
      return;
2118
      return;
1395
#endif
2119
#endif
Line 1399... Line 2123...
1399
 * and is very touchy.  If you add a transformation, take care to
2123
 * and is very touchy.  If you add a transformation, take care to
1400
 * decide how it fits in with the other transformations here.
2124
 * decide how it fits in with the other transformations here.
1401
 */
2125
 */
1402
void /* PRIVATE */
2126
void /* PRIVATE */
1403
png_do_read_transformations(png_structp png_ptr)
2127
png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
1404
{
2128
{
1405
   png_debug(1, "in png_do_read_transformations");
2129
   png_debug(1, "in png_do_read_transformations");
1406
 
2130
 
Line 1407... Line 2131...
1407
   if (png_ptr->row_buf == NULL)
2131
   if (png_ptr->row_buf == NULL)
1408
   {
2132
   {
1409
#ifdef PNG_CONSOLE_IO_SUPPORTED
-
 
1410
      char msg[50];
-
 
1411
 
-
 
1412
      png_snprintf2(msg, 50,
2133
      /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
1413
          "NULL row buffer for row %ld, pass %d", (long)png_ptr->row_number,
2134
       * error is incredibly rare and incredibly easy to debug without this
1414
          png_ptr->pass);
2135
       * information.
1415
      png_error(png_ptr, msg);
-
 
1416
#else
2136
       */
1417
      png_error(png_ptr, "NULL row buffer");
2137
      png_error(png_ptr, "NULL row buffer");
1418
#endif
-
 
1419
   }
2138
   }
-
 
2139
 
-
 
2140
   /* The following is debugging; prior to 1.5.4 the code was never compiled in;
-
 
2141
    * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
-
 
2142
    * PNG_WARN_UNINITIALIZED_ROW removed.  In 1.6 the new flag is set only for
-
 
2143
    * all transformations, however in practice the ROW_INIT always gets done on
-
 
2144
    * demand, if necessary.
-
 
2145
    */
1420
#ifdef PNG_WARN_UNINITIALIZED_ROW
2146
   if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
1421
   if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
2147
      !(png_ptr->flags & PNG_FLAG_ROW_INIT))
-
 
2148
   {
1422
      /* Application has failed to call either png_read_start_image()
2149
      /* Application has failed to call either png_read_start_image() or
1423
       * or png_read_update_info() after setting transforms that expand
2150
       * png_read_update_info() after setting transforms that expand pixels.
1424
       * pixels.  This check added to libpng-1.2.19
2151
       * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
1425
       */
2152
       */
1426
#if (PNG_WARN_UNINITIALIZED_ROW==1)
-
 
1427
      png_error(png_ptr, "Uninitialized row");
2153
      png_error(png_ptr, "Uninitialized row");
1428
#else
2154
   }
1429
      png_warning(png_ptr, "Uninitialized row");
-
 
1430
#endif
-
 
1431
#endif
-
 
Line 1432... Line 2155...
1432
 
2155
 
1433
#ifdef PNG_READ_EXPAND_SUPPORTED
2156
#ifdef PNG_READ_EXPAND_SUPPORTED
1434
   if (png_ptr->transformations & PNG_EXPAND)
2157
   if (png_ptr->transformations & PNG_EXPAND)
1435
   {
2158
   {
1436
      if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
2159
      if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
1437
      {
2160
      {
1438
         png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
2161
         png_do_expand_palette(row_info, png_ptr->row_buf + 1,
1439
             png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
2162
             png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
-
 
2163
      }
1440
      }
2164
 
1441
      else
2165
      else
1442
      {
2166
      {
1443
         if (png_ptr->num_trans &&
2167
         if (png_ptr->num_trans &&
1444
             (png_ptr->transformations & PNG_EXPAND_tRNS))
2168
             (png_ptr->transformations & PNG_EXPAND_tRNS))
1445
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
2169
            png_do_expand(row_info, png_ptr->row_buf + 1,
1446
                &(png_ptr->trans_color));
-
 
Line -... Line 2170...
-
 
2170
                &(png_ptr->trans_color));
1447
         else
2171
 
1448
 
2172
         else
1449
            png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
2173
            png_do_expand(row_info, png_ptr->row_buf + 1,
1450
                NULL);
2174
                NULL);
1451
      }
2175
      }
Line 1452... Line 2176...
1452
   }
2176
   }
1453
#endif
2177
#endif
-
 
2178
 
-
 
2179
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
-
 
2180
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
1454
 
2181
      !(png_ptr->transformations & PNG_COMPOSE) &&
1455
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
2182
      (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
1456
   if (png_ptr->flags & PNG_FLAG_STRIP_ALPHA)
2183
      row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
Line 1457... Line 2184...
1457
      png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
2184
      png_do_strip_channel(row_info, png_ptr->row_buf + 1,
1458
          PNG_FLAG_FILLER_AFTER | (png_ptr->flags & PNG_FLAG_STRIP_ALPHA));
2185
         0 /* at_start == false, because SWAP_ALPHA happens later */);
1459
#endif
2186
#endif
1460
 
2187
 
1461
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2188
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1462
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
2189
   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
Line 1463... Line 2190...
1463
   {
2190
   {
1464
      int rgb_error =
2191
      int rgb_error =
1465
          png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info),
2192
          png_do_rgb_to_gray(png_ptr, row_info,
Line 1515... Line 2242...
1515
    * for performance reasons
2242
    * for performance reasons
1516
    */
2243
    */
1517
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2244
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1518
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2245
       !(png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1519
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
2246
      png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
1520
#endif
2247
#endif
1521
 
2248
 
Line 1522... Line 2249...
1522
#ifdef PNG_READ_BACKGROUND_SUPPORTED
2249
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1523
   if ((png_ptr->transformations & PNG_BACKGROUND) &&
-
 
1524
       ((png_ptr->num_trans != 0) ||
2250
   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1525
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
2251
   if (png_ptr->transformations & PNG_COMPOSE)
1526
      png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
2252
      png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
1527
          &(png_ptr->trans_color), &(png_ptr->background)
-
 
1528
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
1529
          , &(png_ptr->background_1),
-
 
1530
          png_ptr->gamma_table, png_ptr->gamma_from_1,
-
 
1531
          png_ptr->gamma_to_1, png_ptr->gamma_16_table,
-
 
1532
          png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
-
 
1533
          png_ptr->gamma_shift
-
 
1534
#endif
-
 
1535
          );
-
 
1536
#endif
2253
#endif
Line 1537... Line 2254...
1537
 
2254
 
1538
#ifdef PNG_READ_GAMMA_SUPPORTED
2255
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
2256
   if ((png_ptr->transformations & PNG_GAMMA) &&
-
 
2257
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
-
 
2258
      /* Because RGB_TO_GRAY does the gamma transform. */
-
 
2259
      !(png_ptr->transformations & PNG_RGB_TO_GRAY) &&
1539
   if ((png_ptr->transformations & PNG_GAMMA) &&
2260
#endif
-
 
2261
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
-
 
2262
   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
-
 
2263
      /* Because PNG_COMPOSE does the gamma transform if there is something to
-
 
2264
       * do (if there is an alpha channel or transparency.)
1540
#ifdef PNG_READ_BACKGROUND_SUPPORTED
2265
       */
1541
       !((png_ptr->transformations & PNG_BACKGROUND) &&
2266
       !((png_ptr->transformations & PNG_COMPOSE) &&
1542
       ((png_ptr->num_trans != 0) ||
2267
       ((png_ptr->num_trans != 0) ||
1543
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
2268
       (png_ptr->color_type & PNG_COLOR_MASK_ALPHA))) &&
-
 
2269
#endif
-
 
2270
      /* Because png_init_read_transformations transforms the palette, unless
-
 
2271
       * RGB_TO_GRAY will do the transform.
1544
#endif
2272
       */
-
 
2273
       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
-
 
2274
      png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
-
 
2275
#endif
-
 
2276
 
-
 
2277
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
-
 
2278
   if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
-
 
2279
      (png_ptr->transformations & PNG_COMPOSE) &&
-
 
2280
      (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
1545
       (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
2281
      row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
-
 
2282
      png_do_strip_channel(row_info, png_ptr->row_buf + 1,
-
 
2283
         0 /* at_start == false, because SWAP_ALPHA happens later */);
-
 
2284
#endif
-
 
2285
 
-
 
2286
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
-
 
2287
   if ((png_ptr->transformations & PNG_ENCODE_ALPHA) &&
-
 
2288
      (row_info->color_type & PNG_COLOR_MASK_ALPHA))
-
 
2289
      png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
-
 
2290
#endif
-
 
2291
 
1546
      png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
2292
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1547
          png_ptr->gamma_table, png_ptr->gamma_16_table,
2293
   if (png_ptr->transformations & PNG_SCALE_16_TO_8)
1548
          png_ptr->gamma_shift);
2294
      png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
Line 1549... Line 2295...
1549
#endif
2295
#endif
-
 
2296
 
-
 
2297
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
-
 
2298
   /* There is no harm in doing both of these because only one has any effect,
-
 
2299
    * by putting the 'scale' option first if the app asks for scale (either by
1550
 
2300
    * calling the API or in a TRANSFORM flag) this is what happens.
1551
#ifdef PNG_READ_16_TO_8_SUPPORTED
2301
    */
1552
   if (png_ptr->transformations & PNG_16_TO_8)
2302
   if (png_ptr->transformations & PNG_16_TO_8)
Line 1553... Line 2303...
1553
      png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
2303
      png_do_chop(row_info, png_ptr->row_buf + 1);
1554
#endif
2304
#endif
1555
 
2305
 
1556
#ifdef PNG_READ_QUANTIZE_SUPPORTED
2306
#ifdef PNG_READ_QUANTIZE_SUPPORTED
1557
   if (png_ptr->transformations & PNG_QUANTIZE)
2307
   if (png_ptr->transformations & PNG_QUANTIZE)
Line 1558... Line 2308...
1558
   {
2308
   {
1559
      png_do_quantize(&(png_ptr->row_info), png_ptr->row_buf + 1,
2309
      png_do_quantize(row_info, png_ptr->row_buf + 1,
1560
          png_ptr->palette_lookup, png_ptr->quantize_index);
2310
          png_ptr->palette_lookup, png_ptr->quantize_index);
1561
 
2311
 
Line -... Line 2312...
-
 
2312
      if (row_info->rowbytes == 0)
-
 
2313
         png_error(png_ptr, "png_do_quantize returned rowbytes=0");
-
 
2314
   }
-
 
2315
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
-
 
2316
 
-
 
2317
#ifdef PNG_READ_EXPAND_16_SUPPORTED
-
 
2318
   /* Do the expansion now, after all the arithmetic has been done.  Notice
-
 
2319
    * that previous transformations can handle the PNG_EXPAND_16 flag if this
-
 
2320
    * is efficient (particularly true in the case of gamma correction, where
-
 
2321
    * better accuracy results faster!)
-
 
2322
    */
-
 
2323
   if (png_ptr->transformations & PNG_EXPAND_16)
-
 
2324
      png_do_expand_16(row_info, png_ptr->row_buf + 1);
-
 
2325
#endif
-
 
2326
 
-
 
2327
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
-
 
2328
   /* NOTE: moved here in 1.5.4 (from much later in this list.) */
1562
      if (png_ptr->row_info.rowbytes == 0)
2329
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
1563
         png_error(png_ptr, "png_do_quantize returned rowbytes=0");
2330
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
1564
   }
2331
      png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
1565
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
2332
#endif
Line 1566... Line 2333...
1566
 
2333
 
1567
#ifdef PNG_READ_INVERT_SUPPORTED
2334
#ifdef PNG_READ_INVERT_SUPPORTED
1568
   if (png_ptr->transformations & PNG_INVERT_MONO)
2335
   if (png_ptr->transformations & PNG_INVERT_MONO)
1569
      png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
2336
      png_do_invert(row_info, png_ptr->row_buf + 1);
1570
#endif
2337
#endif
Line 1571... Line 2338...
1571
 
2338
 
1572
#ifdef PNG_READ_SHIFT_SUPPORTED
2339
#ifdef PNG_READ_SHIFT_SUPPORTED
1573
   if (png_ptr->transformations & PNG_SHIFT)
2340
   if (png_ptr->transformations & PNG_SHIFT)
-
 
2341
      png_do_unshift(row_info, png_ptr->row_buf + 1,
-
 
2342
          &(png_ptr->shift));
-
 
2343
#endif
-
 
2344
 
-
 
2345
#ifdef PNG_READ_PACK_SUPPORTED
-
 
2346
   if (png_ptr->transformations & PNG_PACK)
-
 
2347
      png_do_unpack(row_info, png_ptr->row_buf + 1);
1574
      png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
2348
#endif
Line 1575... Line 2349...
1575
          &(png_ptr->shift));
2349
 
1576
#endif
2350
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
1577
 
2351
   /* Added at libpng-1.5.10 */
1578
#ifdef PNG_READ_PACK_SUPPORTED
2352
   if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
Line 1579... Line 2353...
1579
   if (png_ptr->transformations & PNG_PACK)
2353
       png_ptr->num_palette_max >= 0)
1580
      png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
2354
      png_do_check_palette_indexes(png_ptr, row_info);
1581
#endif
2355
#endif
1582
 
-
 
1583
#ifdef PNG_READ_BGR_SUPPORTED
-
 
1584
   if (png_ptr->transformations & PNG_BGR)
-
 
1585
      png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
-
 
1586
#endif
-
 
1587
 
-
 
1588
#ifdef PNG_READ_PACKSWAP_SUPPORTED
-
 
1589
   if (png_ptr->transformations & PNG_PACKSWAP)
2356
 
Line 1590... Line 2357...
1590
      png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
2357
#ifdef PNG_READ_BGR_SUPPORTED
1591
#endif
2358
   if (png_ptr->transformations & PNG_BGR)
1592
 
2359
      png_do_bgr(row_info, png_ptr->row_buf + 1);
1593
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2360
#endif
1594
   /* If gray -> RGB, do so now only if we did not do so above */
2361
 
Line 1595... Line 2362...
1595
   if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
2362
#ifdef PNG_READ_PACKSWAP_SUPPORTED
1596
       (png_ptr->mode & PNG_BACKGROUND_IS_GRAY))
2363
   if (png_ptr->transformations & PNG_PACKSWAP)
1597
      png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
2364
      png_do_packswap(row_info, png_ptr->row_buf + 1);
1598
#endif
2365
#endif
Line 1599... Line 2366...
1599
 
2366
 
1600
#ifdef PNG_READ_FILLER_SUPPORTED
2367
#ifdef PNG_READ_FILLER_SUPPORTED
1601
   if (png_ptr->transformations & PNG_FILLER)
2368
   if (png_ptr->transformations & PNG_FILLER)
1602
      png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
2369
      png_do_read_filler(row_info, png_ptr->row_buf + 1,
Line 1603... Line 2370...
1603
          (png_uint_32)png_ptr->filler, png_ptr->flags);
2370
          (png_uint_32)png_ptr->filler, png_ptr->flags);
1604
#endif
2371
#endif
1605
 
2372
 
1606
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
2373
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1607
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
2374
   if (png_ptr->transformations & PNG_INVERT_ALPHA)
1608
      png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
2375
      png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
Line 1609... Line 2376...
1609
#endif
2376
#endif
1610
 
2377
 
1611
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
2378
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
1612
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
2379
   if (png_ptr->transformations & PNG_SWAP_ALPHA)
1613
      png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
2380
      png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
1614
#endif
2381
#endif
1615
 
2382
 
1616
#ifdef PNG_READ_16BIT_SUPPORTED
2383
#ifdef PNG_READ_16BIT_SUPPORTED
1617
#ifdef PNG_READ_SWAP_SUPPORTED
2384
#ifdef PNG_READ_SWAP_SUPPORTED
1618
   if (png_ptr->transformations & PNG_SWAP_BYTES)
2385
   if (png_ptr->transformations & PNG_SWAP_BYTES)
1619
      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
2386
      png_do_swap(row_info, png_ptr->row_buf + 1);
1620
#endif
2387
#endif
1621
#endif
2388
#endif
1622
 
2389
 
1623
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
2390
#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
1624
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
2391
   if (png_ptr->transformations & PNG_USER_TRANSFORM)
1625
    {
2392
    {
Line 1626... Line 2393...
1626
      if (png_ptr->read_user_transform_fn != NULL)
2393
      if (png_ptr->read_user_transform_fn != NULL)
1627
         (*(png_ptr->read_user_transform_fn)) /* User read transform function */
2394
         (*(png_ptr->read_user_transform_fn)) /* User read transform function */
1628
             (png_ptr,                    /* png_ptr */
2395
             (png_ptr,     /* png_ptr */
1629
             &(png_ptr->row_info),     /* row_info: */
2396
             row_info,     /* row_info: */
1630
                /*  png_uint_32 width;       width of row */
2397
                /*  png_uint_32 width;       width of row */
Line 1631... Line 2398...
1631
                /*  png_size_t rowbytes;     number of bytes in row */
2398
                /*  png_size_t rowbytes;     number of bytes in row */
1632
                /*  png_byte color_type;     color type of pixels */
-
 
1633
                /*  png_byte bit_depth;      bit depth of samples */
2399
                /*  png_byte color_type;     color type of pixels */
1634
                /*  png_byte channels;       number of channels (1-4) */
2400
                /*  png_byte bit_depth;      bit depth of samples */
1635
                /*  png_byte pixel_depth;    bits per pixel (depth*channels) */
-
 
1636
             png_ptr->row_buf + 1);    /* start of pixel data for row */
2401
                /*  png_byte channels;       number of channels (1-4) */
Line 1637... Line 2402...
1637
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
2402
                /*  png_byte pixel_depth;    bits per pixel (depth*channels) */
1638
      if (png_ptr->user_transform_depth)
2403
             png_ptr->row_buf + 1);    /* start of pixel data for row */
1639
         png_ptr->row_info.bit_depth = png_ptr->user_transform_depth;
2404
#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
Line 1759... Line 2524...
1759
void /* PRIVATE */
2524
void /* PRIVATE */
1760
png_do_unshift(png_row_infop row_info, png_bytep row,
2525
png_do_unshift(png_row_infop row_info, png_bytep row,
1761
    png_const_color_8p sig_bits)
2526
    png_const_color_8p sig_bits)
1762
{
2527
{
1763
   png_debug(1, "in png_do_unshift");
2528
   int color_type;
-
 
2529
 
-
 
2530
   png_debug(1, "in png_do_unshift");
1764
 
2531
 
Line -... Line 2532...
-
 
2532
   /* The palette case has already been handled in the _init routine. */
-
 
2533
   color_type = row_info->color_type;
1765
   if (
2534
 
1766
       row_info->color_type != PNG_COLOR_TYPE_PALETTE)
2535
   if (color_type != PNG_COLOR_TYPE_PALETTE)
1767
   {
2536
   {
1768
      int shift[4];
2537
      int shift[4];
1769
      int channels = 0;
2538
      int channels = 0;
1770
      int c;
-
 
1771
      png_uint_16 value = 0;
-
 
1772
      png_uint_32 row_width = row_info->width;
2539
      int bit_depth = row_info->bit_depth;
Line 1773... Line 2540...
1773
 
2540
 
1774
      if (row_info->color_type & PNG_COLOR_MASK_COLOR)
2541
      if (color_type & PNG_COLOR_MASK_COLOR)
1775
      {
2542
      {
1776
         shift[channels++] = row_info->bit_depth - sig_bits->red;
2543
         shift[channels++] = bit_depth - sig_bits->red;
1777
         shift[channels++] = row_info->bit_depth - sig_bits->green;
2544
         shift[channels++] = bit_depth - sig_bits->green;
1778
         shift[channels++] = row_info->bit_depth - sig_bits->blue;
2545
         shift[channels++] = bit_depth - sig_bits->blue;
Line 1779... Line 2546...
1779
      }
2546
      }
1780
 
2547
 
1781
      else
2548
      else
1782
      {
2549
      {
Line 1783... Line 2550...
1783
         shift[channels++] = row_info->bit_depth - sig_bits->gray;
2550
         shift[channels++] = bit_depth - sig_bits->gray;
1784
      }
2551
      }
1785
 
2552
 
1786
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
2553
      if (color_type & PNG_COLOR_MASK_ALPHA)
Line 1787... Line -...
1787
      {
-
 
1788
         shift[channels++] = row_info->bit_depth - sig_bits->alpha;
2554
      {
1789
      }
2555
         shift[channels++] = bit_depth - sig_bits->alpha;
-
 
2556
      }
-
 
2557
 
-
 
2558
      {
-
 
2559
         int c, have_shift;
-
 
2560
 
-
 
2561
         for (c = have_shift = 0; c < channels; ++c)
-
 
2562
         {
1790
 
2563
            /* A shift of more than the bit depth is an error condition but it
Line 1791... Line 2564...
1791
      for (c = 0; c < channels; c++)
2564
             * gets ignored here.
1792
      {
2565
             */
1793
         if (shift[c] <= 0)
2566
            if (shift[c] <= 0 || shift[c] >= bit_depth)
Line 1794... Line 2567...
1794
            shift[c] = 0;
2567
               shift[c] = 0;
1795
 
2568
 
-
 
2569
            else
Line 1796... Line 2570...
1796
         else
2570
               have_shift = 1;
1797
            value = 1;
2571
         }
1798
      }
2572
 
-
 
2573
         if (!have_shift)
-
 
2574
            return;
1799
 
2575
      }
Line 1800... Line 2576...
1800
      if (!value)
2576
 
-
 
2577
      switch (bit_depth)
-
 
2578
      {
1801
         return;
2579
         default:
1802
 
2580
         /* Must be 1bpp gray: should not be here! */
1803
      switch (row_info->bit_depth)
-
 
1804
      {
2581
            /* NOTREACHED */
Line 1805... Line 2582...
1805
         default:
2582
            break;
1806
            break; 
2583
 
1807
 
2584
         case 2:
1808
         case 2:
2585
         /* Must be 2bpp gray */
1809
         {
2586
         /* assert(channels == 1 && shift[0] == 1) */
1810
            png_bytep bp;
2587
         {
1811
            png_size_t i;
2588
            png_bytep bp = row;
Line 1812... Line 2589...
1812
            png_size_t istop = row_info->rowbytes;
2589
            png_bytep bp_end = bp + row_info->rowbytes;
-
 
2590
 
-
 
2591
            while (bp < bp_end)
1813
 
2592
            {
1814
            for (bp = row, i = 0; i < istop; i++)
2593
               int b = (*bp >> 1) & 0x55;
1815
            {
-
 
1816
               *bp >>= 1;
2594
               *bp++ = (png_byte)b;
1817
               *bp++ &= 0x55;
2595
            }
1818
            }
2596
            break;
Line -... Line 2597...
-
 
2597
         }
-
 
2598
 
1819
            break;
2599
         case 4:
1820
         }
2600
         /* Must be 4bpp gray */
1821
 
2601
         /* assert(channels == 1) */
1822
         case 4:
2602
         {
1823
         {
2603
            png_bytep bp = row;
1824
            png_bytep bp = row;
2604
            png_bytep bp_end = bp + row_info->rowbytes;
1825
            png_size_t i;
2605
            int gray_shift = shift[0];
Line 1826... Line 2606...
1826
            png_size_t istop = row_info->rowbytes;
2606
            int mask =  0xf >> gray_shift;
-
 
2607
 
1827
            png_byte mask = (png_byte)((((int)0xf0 >> shift[0]) & (int)0xf0) |
2608
            mask |= mask << 4;
1828
                (png_byte)((int)0xf >> shift[0]));
2609
 
1829
 
2610
            while (bp < bp_end)
1830
            for (i = 0; i < istop; i++)
2611
            {
Line 1831... Line 2612...
1831
            {
2612
               int b = (*bp >> gray_shift) & mask;
1832
               *bp >>= shift[0];
2613
               *bp++ = (png_byte)b;
1833
               *bp++ &= mask;
2614
            }
-
 
2615
            break;
-
 
2616
         }
-
 
2617
 
1834
            }
2618
         case 8:
1835
            break;
2619
         /* Single byte components, G, GA, RGB, RGBA */
1836
         }
2620
         {
Line 1837... Line 2621...
1837
 
2621
            png_bytep bp = row;
1838
         case 8:
2622
            png_bytep bp_end = bp + row_info->rowbytes;
-
 
2623
            int channel = 0;
1839
         {
2624
 
1840
            png_bytep bp = row;
2625
            while (bp < bp_end)
1841
            png_uint_32 i;
2626
            {
1842
            png_uint_32 istop = row_width * channels;
2627
               int b = *bp >> shift[channel];
Line 1843... Line 2628...
1843
 
2628
               if (++channel >= channels)
1844
            for (i = 0; i < istop; i++)
2629
                  channel = 0;
1845
            {
2630
               *bp++ = (png_byte)b;
-
 
2631
            }
1846
               *bp++ >>= shift[i%channels];
2632
            break;
-
 
2633
         }
-
 
2634
 
1847
            }
2635
#ifdef PNG_READ_16BIT_SUPPORTED
1848
            break;
2636
         case 16:
1849
         }
2637
         /* Double byte components, G, GA, RGB, RGBA */
1850
 
2638
         {
1851
#ifdef PNG_READ_16BIT_SUPPORTED
2639
            png_bytep bp = row;
Line 1869... Line 2657...
1869
   }
2657
   }
1870
}
2658
}
1871
#endif
2659
#endif
1872
 
2660
 
Line 1873... Line 2661...
1873
#ifdef PNG_READ_16_TO_8_SUPPORTED
2661
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1874
/* Chop rows of bit depth 16 down to 8 */
2662
/* Scale rows of bit depth 16 down to 8 accurately */
1875
void /* PRIVATE */
2663
void /* PRIVATE */
1876
png_do_chop(png_row_infop row_info, png_bytep row)
2664
png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
1877
{
2665
{
1878
   png_debug(1, "in png_do_chop");
2666
   png_debug(1, "in png_do_scale_16_to_8");
Line 1879... Line 2667...
1879
 
2667
 
1880
   if (row_info->bit_depth == 16)
2668
   if (row_info->bit_depth == 16)
1881
   {
2669
   {
1882
      png_bytep sp = row;
2670
      png_bytep sp = row; /* source */
1883
      png_bytep dp = row;
-
 
1884
      png_uint_32 i;
2671
      png_bytep dp = row; /* destination */
1885
      png_uint_32 istop = row_info->width * row_info->channels;
2672
      png_bytep ep = sp + row_info->rowbytes; /* end+1 */
1886
 
2673
 
1887
      for (i = 0; i
2674
      while (sp < ep)
1888
      {
2675
      {
1889
#ifdef PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
2676
         /* The input is an array of 16 bit components, these must be scaled to
1890
      /* This does a more accurate scaling of the 16-bit color
2677
          * 8 bits each.  For a 16 bit value V the required value (from the PNG
-
 
2678
          * specification) is:
-
 
2679
          *
1891
       * value, rather than a simple low-byte truncation.
2680
          *    (V * 255) / 65535
1892
       *
-
 
1893
       * What the ideal calculation should be:
-
 
1894
       *   *dp = (((((png_uint_32)(*sp) << 8) |
2681
          *
1895
       *          (png_uint_32)(*(sp + 1))) * 255 + 127)
-
 
1896
       *          / (png_uint_32)65535L;
2682
          * This reduces to round(V / 257), or floor((V + 128.5)/257)
1897
       *
2683
          *
1898
       * GRR: no, I think this is what it really should be:
-
 
1899
       *   *dp = (((((png_uint_32)(*sp) << 8) |
2684
          * Represent V as the two byte value vhi.vlo.  Make a guess that the
1900
       *           (png_uint_32)(*(sp + 1))) + 128L)
2685
          * result is the top byte of V, vhi, then the correction to this value
1901
       *           / (png_uint_32)257L;
2686
          * is:
1902
       *
-
 
1903
       * GRR: here's the exact calculation with shifts:
2687
          *
1904
       *   temp = (((png_uint_32)(*sp) << 8) |
2688
          *    error = floor(((V-vhi.vhi) + 128.5) / 257)
1905
       *           (png_uint_32)(*(sp + 1))) + 128L;
-
 
1906
       *   *dp = (temp - (temp >> 8)) >> 8;
2689
          *          = floor(((vlo-vhi) + 128.5) / 257)
1907
       *
2690
          *
1908
       * Approximate calculation with shift/add instead of multiply/divide:
2691
          * This can be approximated using integer arithmetic (and a signed
1909
       *   *dp = ((((png_uint_32)(*sp) << 8) |
-
 
1910
       *          (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
2692
          * shift):
-
 
2693
          *
-
 
2694
          *    error = (vlo-vhi+128) >> 8;
-
 
2695
          *
1911
       *
2696
          * The approximate differs from the exact answer only when (vlo-vhi) is
-
 
2697
          * 128; it then gives a correction of +1 when the exact correction is
-
 
2698
          * 0.  This gives 128 errors.  The exact answer (correct for all 16 bit
-
 
2699
          * input values) is:
-
 
2700
          *
-
 
2701
          *    error = (vlo-vhi+128)*65535 >> 24;
-
 
2702
          *
-
 
2703
          * An alternative arithmetic calculation which also gives no errors is:
-
 
2704
          *
1912
       * What we actually do to avoid extra shifting and conversion:
2705
          *    (V * 255 + 32895) >> 16
Line -... Line 2706...
-
 
2706
          */
1913
       */
2707
 
-
 
2708
         png_int_32 tmp = *sp++; /* must be signed! */
1914
 
2709
         tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
-
 
2710
         *dp++ = (png_byte)tmp;
1915
         *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
2711
      }
1916
#else
2712
 
-
 
2713
      row_info->bit_depth = 8;
-
 
2714
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
-
 
2715
      row_info->rowbytes = row_info->width * row_info->channels;
1917
       /* Simply discard the low order byte */
2716
   }
-
 
2717
}
-
 
2718
#endif
-
 
2719
 
-
 
2720
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
-
 
2721
void /* PRIVATE */
-
 
2722
/* Simply discard the low byte.  This was the default behavior prior
-
 
2723
 * to libpng-1.5.4.
-
 
2724
 */
-
 
2725
png_do_chop(png_row_infop row_info, png_bytep row)
-
 
2726
{
-
 
2727
   png_debug(1, "in png_do_chop");
-
 
2728
 
-
 
2729
   if (row_info->bit_depth == 16)
-
 
2730
   {
-
 
2731
      png_bytep sp = row; /* source */
-
 
2732
      png_bytep dp = row; /* destination */
-
 
2733
      png_bytep ep = sp + row_info->rowbytes; /* end+1 */
-
 
2734
 
-
 
2735
      while (sp < ep)
-
 
2736
      {
1918
         *dp = *sp;
2737
         *dp++ = *sp;
-
 
2738
         sp += 2; /* skip low byte */
1919
#endif
2739
      }
1920
      }
2740
 
1921
      row_info->bit_depth = 8;
2741
      row_info->bit_depth = 8;
1922
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
2742
      row_info->pixel_depth = (png_byte)(8 * row_info->channels);
1923
      row_info->rowbytes = row_info->width * row_info->channels;
2743
      row_info->rowbytes = row_info->width * row_info->channels;
Line 2388... Line 3208...
2388
               *(dp--) = *(sp--);
3208
               *(dp--) = *(sp--);
2389
            }
3209
            }
2390
         }
3210
         }
2391
      }
3211
      }
2392
      row_info->channels += (png_byte)2;
3212
      row_info->channels = (png_byte)(row_info->channels + 2);
2393
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
3213
      row_info->color_type |= PNG_COLOR_MASK_COLOR;
2394
      row_info->pixel_depth = (png_byte)(row_info->channels *
3214
      row_info->pixel_depth = (png_byte)(row_info->channels *
2395
          row_info->bit_depth);
3215
          row_info->bit_depth);
2396
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3216
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2397
   }
3217
   }
2398
}
3218
}
2399
#endif
3219
#endif
2400
 
3220
 
Line 2401... Line 3221...
2401
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
3221
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2402
/* Reduce RGB files to grayscale, with or without alpha
3222
/* Reduce RGB files to grayscale, with or without alpha
2403
 * using the equation given in Poynton's ColorFAQ at
3223
 * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
2404
 *   (THIS LINK IS DEAD June 2008)
3224
 *   (THIS LINK IS DEAD June 2008 but
2405
 * New link:
3225
 * versions dated 1998 through November 2002 have been archived at
-
 
3226
 * http://web.archive.org/web/20000816232553/http://www.inforamp.net/
2406
 * 
3227
 * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
2407
 * Charles Poynton poynton at poynton.com
3228
 * Charles Poynton poynton at poynton.com
2408
 *
3229
 *
2409
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
3230
 *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
2410
 *
3231
 *
-
 
3232
 *  which can be expressed with integers as
-
 
3233
 *
-
 
3234
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
-
 
3235
 *
-
 
3236
 * Poynton's current link (as of January 2003 through July 2011):
-
 
3237
 * 
2411
 *  We approximate this with
3238
 * has changed the numbers slightly:
2412
 *
3239
 *
2413
 *     Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
3240
 *     Y = 0.2126*R + 0.7152*G + 0.0722*B
2414
 *
3241
 *
2415
 *  which can be expressed with integers as
3242
 *  which can be expressed with integers as
2416
 *
3243
 *
2417
 *     Y = (6969 * R + 23434 * G + 2365 * B)/32768
3244
 *     Y = (6966 * R + 23436 * G + 2366 * B)/32768
-
 
3245
 *
-
 
3246
 *  Historically, however, libpng uses numbers derived from the ITU-R Rec 709
-
 
3247
 *  end point chromaticities and the D65 white point.  Depending on the
-
 
3248
 *  precision used for the D65 white point this produces a variety of different
-
 
3249
 *  numbers, however if the four decimal place value used in ITU-R Rec 709 is
-
 
3250
 *  used (0.3127,0.3290) the Y calculation would be:
-
 
3251
 *
-
 
3252
 *     Y = (6968 * R + 23435 * G + 2366 * B)/32768
-
 
3253
 *
-
 
3254
 *  While this is correct the rounding results in an overflow for white, because
-
 
3255
 *  the sum of the rounded coefficients is 32769, not 32768.  Consequently
-
 
3256
 *  libpng uses, instead, the closest non-overflowing approximation:
2418
 *
3257
 *
2419
 *  The calculation is to be done in a linear colorspace.
3258
 *     Y = (6968 * R + 23434 * G + 2366 * B)/32768
2420
 *
3259
 *
-
 
3260
 *  Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
-
 
3261
 *  (including an sRGB chunk) then the chromaticities are used to calculate the
-
 
3262
 *  coefficients.  See the chunk handling in pngrutil.c for more information.
-
 
3263
 *
-
 
3264
 *  In all cases the calculation is to be done in a linear colorspace.  If no
-
 
3265
 *  gamma information is available to correct the encoding of the original RGB
-
 
3266
 *  values this results in an implicit assumption that the original PNG RGB
-
 
3267
 *  values were linear.
-
 
3268
 *
2421
 *  Other integer coefficents can be used via png_set_rgb_to_gray().
3269
 *  Other integer coefficents can be used via png_set_rgb_to_gray().  Because
-
 
3270
 *  the API takes just red and green coefficients the blue coefficient is
-
 
3271
 *  calculated to make the sum 32768.  This will result in different rounding
-
 
3272
 *  to that used above.
2422
 */
3273
 */
2423
int /* PRIVATE */
3274
int /* PRIVATE */
2424
png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
3275
png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
Line 2425... Line 3276...
2425
 
3276
 
2426
{
-
 
2427
   png_uint_32 i;
-
 
2428
 
-
 
2429
   png_uint_32 row_width = row_info->width;
3277
{
Line 2430... Line 3278...
2430
   int rgb_error = 0;
3278
   int rgb_error = 0;
Line 2431... Line 3279...
2431
 
3279
 
2432
   png_debug(1, "in png_do_rgb_to_gray");
3280
   png_debug(1, "in png_do_rgb_to_gray");
2433
 
3281
 
2434
   if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
3282
   if (!(row_info->color_type & PNG_COLOR_MASK_PALETTE) &&
2435
       (row_info->color_type & PNG_COLOR_MASK_COLOR))
3283
       (row_info->color_type & PNG_COLOR_MASK_COLOR))
2436
   {
3284
   {
-
 
3285
      PNG_CONST png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
-
 
3286
      PNG_CONST png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
-
 
3287
      PNG_CONST png_uint_32 bc = 32768 - rc - gc;
Line 2437... Line -...
2437
      png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
-
 
2438
      png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
-
 
2439
      png_uint_32 bc = png_ptr->rgb_to_gray_blue_coeff;
3288
      PNG_CONST png_uint_32 row_width = row_info->width;
2440
 
3289
      PNG_CONST int have_alpha =
2441
      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
3290
         (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
-
 
3291
 
-
 
3292
      if (row_info->bit_depth == 8)
-
 
3293
      {
-
 
3294
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
3295
         /* Notice that gamma to/from 1 are not necessarily inverses (if
2442
      {
3296
          * there is an overall gamma correction).  Prior to 1.5.5 this code
2443
         if (row_info->bit_depth == 8)
3297
          * checked the linearized values for equality; this doesn't match
2444
         {
3298
          * the documentation, the original values must be checked.
2445
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
3299
          */
-
 
3300
         if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
Line 2446... Line 3301...
2446
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
3301
         {
2447
            {
3302
            png_bytep sp = row;
2448
               png_bytep sp = row;
3303
            png_bytep dp = row;
2449
               png_bytep dp = row;
3304
            png_uint_32 i;
2450
 
3305
 
Line 2451... Line 3306...
2451
               for (i = 0; i < row_width; i++)
3306
            for (i = 0; i < row_width; i++)
2452
               {
3307
            {
-
 
3308
               png_byte red   = *(sp++);
-
 
3309
               png_byte green = *(sp++);
-
 
3310
               png_byte blue  = *(sp++);
-
 
3311
 
2453
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
3312
               if (red != green || red != blue)
2454
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
3313
               {
2455
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
3314
                  red = png_ptr->gamma_to_1[red];
2456
 
3315
                  green = png_ptr->gamma_to_1[green];
Line 2457... Line 3316...
2457
                  if (red != green || red != blue)
3316
                  blue = png_ptr->gamma_to_1[blue];
-
 
3317
 
-
 
3318
                  rgb_error |= 1;
-
 
3319
                  *(dp++) = png_ptr->gamma_from_1[
-
 
3320
                      (rc*red + gc*green + bc*blue + 16384)>>15];
-
 
3321
               }
-
 
3322
 
-
 
3323
               else
-
 
3324
               {
-
 
3325
                  /* If there is no overall correction the table will not be
-
 
3326
                   * set.
-
 
3327
                   */
2458
                  {
3328
                  if (png_ptr->gamma_table != NULL)
2459
                     rgb_error |= 1;
3329
                     red = png_ptr->gamma_table[red];
2460
                     *(dp++) = png_ptr->gamma_from_1[
3330
 
2461
                         (rc*red + gc*green + bc*blue)>>15];
3331
                  *(dp++) = red;
2462
                  }
3332
               }
2463
 
3333
 
2464
                  else
3334
               if (have_alpha)
2465
                     *(dp++) = *(sp - 1);
3335
                  *(dp++) = *(sp++);
-
 
3336
            }
-
 
3337
         }
2466
               }
3338
         else
2467
            }
3339
#endif
2468
            else
3340
         {
2469
#endif
3341
            png_bytep sp = row;
2470
            {
3342
            png_bytep dp = row;
Line 2471... Line 3343...
2471
               png_bytep sp = row;
3343
            png_uint_32 i;
2472
               png_bytep dp = row;
3344
 
2473
               for (i = 0; i < row_width; i++)
3345
            for (i = 0; i < row_width; i++)
-
 
3346
            {
-
 
3347
               png_byte red   = *(sp++);
-
 
3348
               png_byte green = *(sp++);
2474
               {
3349
               png_byte blue  = *(sp++);
2475
                  png_byte red   = *(sp++);
3350
 
Line 2476... Line 3351...
2476
                  png_byte green = *(sp++);
3351
               if (red != green || red != blue)
-
 
3352
               {
-
 
3353
                  rgb_error |= 1;
-
 
3354
                  /* NOTE: this is the historical approach which simply
2477
                  png_byte blue  = *(sp++);
3355
                   * truncates the results.
2478
 
3356
                   */
2479
                  if (red != green || red != blue)
3357
                  *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
2480
                  {
3358
               }
Line 2481... Line 3359...
2481
                     rgb_error |= 1;
3359
 
2482
                     *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
3360
               else
2483
                  }
3361
                  *(dp++) = red;
2484
 
-
 
2485
                  else
3362
 
2486
                     *(dp++) = *(sp - 1);
3363
               if (have_alpha)
2487
               }
3364
                  *(dp++) = *(sp++);
2488
            }
3365
            }
-
 
3366
         }
-
 
3367
      }
2489
         }
3368
 
2490
 
3369
      else /* RGB bit_depth == 16 */
2491
         else /* RGB bit_depth == 16 */
3370
      {
Line 2492... Line 3371...
2492
         {
3371
#ifdef PNG_READ_GAMMA_SUPPORTED
2493
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
3372
         if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
2494
            if (png_ptr->gamma_16_to_1 != NULL &&
3373
         {
Line 2495... Line 3374...
2495
                png_ptr->gamma_16_from_1 != NULL)
3374
            png_bytep sp = row;
-
 
3375
            png_bytep dp = row;
-
 
3376
            png_uint_32 i;
-
 
3377
 
-
 
3378
            for (i = 0; i < row_width; i++)
-
 
3379
            {
-
 
3380
               png_uint_16 red, green, blue, w;
2496
            {
3381
 
-
 
3382
               red   = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
Line 2497... Line 3383...
2497
               png_bytep sp = row;
3383
               green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2498
               png_bytep dp = row;
3384
               blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
2499
               for (i = 0; i < row_width; i++)
3385
 
2500
               {
3386
               if (red == green && red == blue)
Line 2516... Line 3402...
2516
                         png_ptr->gamma_shift][green>>8];
3402
                      png_ptr->gamma_shift][green>>8];
2517
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff)
3403
                  png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff)
2518
                         >> png_ptr->gamma_shift][blue>>8];
3404
                      >> png_ptr->gamma_shift][blue>>8];
2519
                     png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
3405
                  png_uint_16 gray16  = (png_uint_16)((rc*red_1 + gc*green_1
2520
                         + bc*blue_1)>>15);
3406
                      + bc*blue_1 + 16384)>>15);
2521
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
3407
                  w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
2522
                         png_ptr->gamma_shift][gray16 >> 8];
3408
                      png_ptr->gamma_shift][gray16 >> 8];
2523
                     rgb_error |= 1;
3409
                  rgb_error |= 1;
2524
                  }
3410
               }
2525
 
3411
 
Line 2526... Line 3412...
2526
                  *(dp++) = (png_byte)((w>>8) & 0xff);
3412
               *(dp++) = (png_byte)((w>>8) & 0xff);
2527
                  *(dp++) = (png_byte)(w & 0xff);
3413
               *(dp++) = (png_byte)(w & 0xff);
-
 
3414
 
-
 
3415
               if (have_alpha)
-
 
3416
               {
-
 
3417
                  *(dp++) = *(sp++);
-
 
3418
                  *(dp++) = *(sp++);
-
 
3419
               }
2528
               }
3420
            }
2529
            }
3421
         }
2530
            else
3422
         else
2531
#endif
3423
#endif
2532
            {
3424
         {
2533
               png_bytep sp = row;
3425
            png_bytep sp = row;
2534
               png_bytep dp = row;
3426
            png_bytep dp = row;
-
 
3427
            png_uint_32 i;
-
 
3428
 
2535
               for (i = 0; i < row_width; i++)
3429
            for (i = 0; i < row_width; i++)
2536
               {
3430
            {
2537
                  png_uint_16 red, green, blue, gray16;
3431
               png_uint_16 red, green, blue, gray16;
Line 2538... Line 3432...
2538
 
3432
 
Line 2542... Line 3436...
2542
 
3436
 
Line 2543... Line 3437...
2543
                  if (red != green || red != blue)
3437
               if (red != green || red != blue)
2544
                     rgb_error |= 1;
3438
                  rgb_error |= 1;
Line -... Line 3439...
-
 
3439
 
-
 
3440
               /* From 1.5.5 in the 16 bit case do the accurate conversion even
-
 
3441
                * in the 'fast' case - this is because this is where the code
-
 
3442
                * ends up when handling linear 16 bit data.
2545
 
3443
                */
-
 
3444
               gray16  = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
2546
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
3445
                  15);
2547
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
3446
               *(dp++) = (png_byte)((gray16>>8) & 0xff);
2548
                  *(dp++) = (png_byte)(gray16 & 0xff);
-
 
2549
               }
-
 
2550
            }
-
 
2551
         }
-
 
2552
      }
-
 
2553
      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
-
 
2554
      {
-
 
2555
         if (row_info->bit_depth == 8)
-
 
2556
         {
-
 
2557
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-
 
2558
            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
-
 
2559
            {
-
 
2560
               png_bytep sp = row;
-
 
2561
               png_bytep dp = row;
-
 
2562
               for (i = 0; i < row_width; i++)
-
 
2563
               {
-
 
2564
                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
-
 
2565
                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
-
 
Line 2566... Line -...
2566
                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
-
 
2567
 
3447
               *(dp++) = (png_byte)(gray16 & 0xff);
2568
                  if (red != green || red != blue)
-
 
2569
                     rgb_error |= 1;
-
 
2570
 
-
 
2571
                  *(dp++) =  png_ptr->gamma_from_1
-
 
2572
                      [(rc*red + gc*green + bc*blue)>>15];
-
 
2573
 
-
 
2574
                  *(dp++) = *(sp++);  /* alpha */
-
 
2575
               }
-
 
2576
            }
-
 
2577
            else
3448
 
2578
#endif
-
 
2579
            {
-
 
2580
               png_bytep sp = row;
-
 
2581
               png_bytep dp = row;
-
 
2582
               for (i = 0; i < row_width; i++)
-
 
2583
               {
-
 
2584
                  png_byte red   = *(sp++);
-
 
2585
                  png_byte green = *(sp++);
-
 
2586
                  png_byte blue  = *(sp++);
-
 
2587
                  if (red != green || red != blue)
-
 
2588
                     rgb_error |= 1;
-
 
2589
 
-
 
2590
                  *(dp++) =  (png_byte)((rc*red + gc*green + bc*blue)>>15);
-
 
2591
                  *(dp++) = *(sp++);  /* alpha */
-
 
2592
               }
-
 
2593
            }
-
 
2594
         }
-
 
2595
         else /* RGBA bit_depth == 16 */
-
 
2596
         {
-
 
2597
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-
 
2598
            if (png_ptr->gamma_16_to_1 != NULL &&
-
 
2599
                png_ptr->gamma_16_from_1 != NULL)
-
 
2600
            {
-
 
2601
               png_bytep sp = row;
-
 
2602
               png_bytep dp = row;
-
 
2603
               for (i = 0; i < row_width; i++)
-
 
2604
               {
-
 
2605
                  png_uint_16 red, green, blue, w;
-
 
2606
 
-
 
2607
                  red   = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
-
 
2608
                  green = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
-
 
2609
                  blue  = (png_uint_16)(((*(sp))<<8) | *(sp + 1)); sp += 2;
-
 
2610
 
-
 
2611
                  if (red == green && red == blue)
-
 
2612
                     w = red;
-
 
2613
 
-
 
2614
                  else
-
 
2615
                  {
-
 
2616
                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
-
 
2617
                         png_ptr->gamma_shift][red>>8];
-
 
2618
 
-
 
2619
                     png_uint_16 green_1 =
-
 
2620
                         png_ptr->gamma_16_to_1[(green&0xff) >>
-
 
2621
                         png_ptr->gamma_shift][green>>8];
-
 
2622
 
-
 
2623
                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >>
-
 
2624
                         png_ptr->gamma_shift][blue>>8];
-
 
2625
 
-
 
2626
                     png_uint_16 gray16  = (png_uint_16)((rc * red_1
-
 
2627
                         + gc * green_1 + bc * blue_1)>>15);
-
 
2628
 
-
 
2629
                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
-
 
2630
                         png_ptr->gamma_shift][gray16 >> 8];
-
 
2631
 
-
 
2632
                     rgb_error |= 1;
-
 
2633
                  }
-
 
2634
 
-
 
2635
                  *(dp++) = (png_byte)((w>>8) & 0xff);
-
 
2636
                  *(dp++) = (png_byte)(w & 0xff);
3449
               if (have_alpha)
2637
                  *(dp++) = *(sp++);  /* alpha */
-
 
2638
                  *(dp++) = *(sp++);
-
 
2639
               }
-
 
2640
            }
-
 
2641
            else
-
 
2642
#endif
-
 
2643
            {
-
 
2644
               png_bytep sp = row;
-
 
2645
               png_bytep dp = row;
-
 
2646
               for (i = 0; i < row_width; i++)
-
 
2647
               {
-
 
2648
                  png_uint_16 red, green, blue, gray16;
-
 
2649
                  red   = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
-
 
2650
                  green = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
-
 
2651
                  blue  = (png_uint_16)((*(sp)<<8) | *(sp + 1)); sp += 2;
-
 
2652
 
-
 
2653
                  if (red != green || red != blue)
-
 
2654
                     rgb_error |= 1;
-
 
2655
 
-
 
2656
                  gray16  = (png_uint_16)((rc*red + gc*green + bc*blue)>>15);
-
 
2657
                  *(dp++) = (png_byte)((gray16>>8) & 0xff);
-
 
2658
                  *(dp++) = (png_byte)(gray16 & 0xff);
3450
               {
2659
                  *(dp++) = *(sp++);  /* alpha */
3451
                  *(dp++) = *(sp++);
2660
                  *(dp++) = *(sp++);
3452
                  *(dp++) = *(sp++);
2661
               }
3453
               }
2662
            }
3454
            }
-
 
3455
         }
2663
         }
3456
      }
2664
      }
3457
 
2665
      row_info->channels -= 2;
3458
      row_info->channels = (png_byte)(row_info->channels - 2);
2666
      row_info->color_type = (png_byte)(row_info->color_type &
3459
      row_info->color_type = (png_byte)(row_info->color_type &
2667
          ~PNG_COLOR_MASK_COLOR);
3460
          ~PNG_COLOR_MASK_COLOR);
2668
      row_info->pixel_depth = (png_byte)(row_info->channels *
3461
      row_info->pixel_depth = (png_byte)(row_info->channels *
2669
          row_info->bit_depth);
3462
          row_info->bit_depth);
2670
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
3463
      row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
2671
   }
3464
   }
2672
   return rgb_error;
3465
   return rgb_error;
-
 
3466
}
Line -... Line 3467...
-
 
3467
#endif
2673
}
3468
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
2674
#endif
3469
 
2675
 
3470
#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
2676
/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
3471
/* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
2677
 * large of png_color.  This lets grayscale images be treated as
3472
 * large of png_color.  This lets grayscale images be treated as
2678
 * paletted.  Most useful for gamma correction and simplification
3473
 * paletted.  Most useful for gamma correction and simplification
2679
 * of code.
3474
 * of code.  This API is not used internally.
2680
 */
3475
 */
2681
void PNGAPI
3476
void PNGAPI
Line 2725... Line 3520...
2725
      palette[i].green = (png_byte)v;
3520
      palette[i].green = (png_byte)v;
2726
      palette[i].blue = (png_byte)v;
3521
      palette[i].blue = (png_byte)v;
2727
   }
3522
   }
2728
}
3523
}
2729
 
3524
#endif
-
 
3525
 
Line 2730... Line 3526...
2730
 
3526
 
-
 
3527
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
-
 
3528
#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
2731
#ifdef PNG_READ_BACKGROUND_SUPPORTED
3529
   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
2732
/* Replace any alpha or transparency with the supplied background color.
3530
/* Replace any alpha or transparency with the supplied background color.
2733
 * "background" is already in the screen gamma, while "background_1" is
3531
 * "background" is already in the screen gamma, while "background_1" is
2734
 * at a gamma of 1.0.  Paletted files have already been taken care of.
3532
 * at a gamma of 1.0.  Paletted files have already been taken care of.
2735
 */
3533
 */
2736
void /* PRIVATE */
3534
void /* PRIVATE */
2737
png_do_background(png_row_infop row_info, png_bytep row,
-
 
-
 
3535
png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
2738
    png_const_color_16p trans_color, png_const_color_16p background
3536
{
2739
#ifdef PNG_READ_GAMMA_SUPPORTED
3537
#ifdef PNG_READ_GAMMA_SUPPORTED
2740
    , png_const_color_16p background_1, png_const_bytep gamma_table,
3538
   png_const_bytep gamma_table = png_ptr->gamma_table;
-
 
3539
   png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
-
 
3540
   png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
2741
    png_const_bytep gamma_from_1, png_const_bytep gamma_to_1,
3541
   png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
2742
    png_const_uint_16pp gamma_16, png_const_uint_16pp gamma_16_from_1,
3542
   png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
-
 
3543
   png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
-
 
3544
   int gamma_shift = png_ptr->gamma_shift;
2743
    png_const_uint_16pp gamma_16_to_1, int gamma_shift
3545
   int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
2744
#endif
-
 
2745
    )
3546
#endif
2746
{
3547
 
2747
   png_bytep sp, dp;
3548
   png_bytep sp;
2748
   png_uint_32 i;
3549
   png_uint_32 i;
2749
   png_uint_32 row_width = row_info->width;
3550
   png_uint_32 row_width = row_info->width;
Line 2750... Line 3551...
2750
   int shift;
3551
   int shift;
Line 2751... Line -...
2751
 
-
 
2752
   png_debug(1, "in png_do_background");
-
 
2753
 
-
 
2754
   if (background != NULL &&
3552
 
2755
      (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
3553
   png_debug(1, "in png_do_compose");
2756
      (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_color)))
3554
 
2757
   {
3555
   {
2758
      switch (row_info->color_type)
3556
      switch (row_info->color_type)
Line 2767... Line 3565...
2767
                  shift = 7;
3565
                  shift = 7;
2768
                  for (i = 0; i < row_width; i++)
3566
                  for (i = 0; i < row_width; i++)
2769
                  {
3567
                  {
2770
                     if ((png_uint_16)((*sp >> shift) & 0x01)
3568
                     if ((png_uint_16)((*sp >> shift) & 0x01)
2771
                        == trans_color->gray)
3569
                        == png_ptr->trans_color.gray)
2772
                     {
3570
                     {
2773
                        *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
3571
                        unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
2774
                        *sp |= (png_byte)(background->gray << shift);
3572
                        tmp |= png_ptr->background.gray << shift;
2775
                     }
3573
                        *sp = (png_byte)(tmp & 0xff);
-
 
3574
                     }
2776
 
3575
 
Line 2777... Line 3576...
2777
                     if (!shift)
3576
                     if (!shift)
2778
                     {
3577
                     {
2779
                        shift = 7;
3578
                        shift = 7;
Line 2795... Line 3594...
2795
                     shift = 6;
3594
                     shift = 6;
2796
                     for (i = 0; i < row_width; i++)
3595
                     for (i = 0; i < row_width; i++)
2797
                     {
3596
                     {
2798
                        if ((png_uint_16)((*sp >> shift) & 0x03)
3597
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2799
                            == trans_color->gray)
3598
                            == png_ptr->trans_color.gray)
2800
                        {
3599
                        {
2801
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3600
                           unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
2802
                           *sp |= (png_byte)(background->gray << shift);
3601
                           tmp |= png_ptr->background.gray << shift;
2803
                        }
3602
                           *sp = (png_byte)(tmp & 0xff);
-
 
3603
                        }
2804
 
3604
 
Line 2805... Line 3605...
2805
                        else
3605
                        else
2806
                        {
3606
                        {
2807
                           png_byte p = (png_byte)((*sp >> shift) & 0x03);
3607
                           unsigned int p = (*sp >> shift) & 0x03;
2808
                           png_byte g = (png_byte)((gamma_table [p | (p << 2) |
3608
                           unsigned int g = (gamma_table [p | (p << 2) |
2809
                               (p << 4) | (p << 6)] >> 6) & 0x03);
3609
                               (p << 4) | (p << 6)] >> 6) & 0x03;
2810
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3610
                           unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
-
 
3611
                           tmp |= g << shift;
2811
                           *sp |= (png_byte)(g << shift);
3612
                           *sp = (png_byte)(tmp & 0xff);
2812
                        }
3613
                        }
Line 2813... Line 3614...
2813
 
3614
 
2814
                        if (!shift)
3615
                        if (!shift)
2815
                        {
3616
                        {
Line 2829... Line 3630...
2829
                     shift = 6;
3630
                     shift = 6;
2830
                     for (i = 0; i < row_width; i++)
3631
                     for (i = 0; i < row_width; i++)
2831
                     {
3632
                     {
2832
                        if ((png_uint_16)((*sp >> shift) & 0x03)
3633
                        if ((png_uint_16)((*sp >> shift) & 0x03)
2833
                            == trans_color->gray)
3634
                            == png_ptr->trans_color.gray)
2834
                        {
3635
                        {
2835
                           *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
3636
                           unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
2836
                           *sp |= (png_byte)(background->gray << shift);
3637
                           tmp |= png_ptr->background.gray << shift;
2837
                        }
3638
                           *sp = (png_byte)(tmp & 0xff);
-
 
3639
                        }
2838
 
3640
 
Line 2839... Line 3641...
2839
                        if (!shift)
3641
                        if (!shift)
2840
                        {
3642
                        {
2841
                           shift = 6;
3643
                           shift = 6;
Line 2858... Line 3660...
2858
                     shift = 4;
3660
                     shift = 4;
2859
                     for (i = 0; i < row_width; i++)
3661
                     for (i = 0; i < row_width; i++)
2860
                     {
3662
                     {
2861
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
3663
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2862
                            == trans_color->gray)
3664
                            == png_ptr->trans_color.gray)
2863
                        {
3665
                        {
2864
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3666
                           unsigned int tmp = *sp & (0xf0f >> (4 - shift));
2865
                           *sp |= (png_byte)(background->gray << shift);
3667
                           tmp |= png_ptr->background.gray << shift;
2866
                        }
3668
                           *sp = (png_byte)(tmp & 0xff);
-
 
3669
                        }
2867
 
3670
 
Line 2868... Line 3671...
2868
                        else
3671
                        else
2869
                        {
3672
                        {
2870
                           png_byte p = (png_byte)((*sp >> shift) & 0x0f);
3673
                           unsigned int p = (*sp >> shift) & 0x0f;
2871
                           png_byte g = (png_byte)((gamma_table[p |
3674
                           unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
2872
                               (p << 4)] >> 4) & 0x0f);
3675
                              0x0f;
2873
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3676
                           unsigned int tmp = *sp & (0xf0f >> (4 - shift));
-
 
3677
                           tmp |= g << shift;
2874
                           *sp |= (png_byte)(g << shift);
3678
                           *sp = (png_byte)(tmp & 0xff);
2875
                        }
3679
                        }
Line 2876... Line 3680...
2876
 
3680
 
2877
                        if (!shift)
3681
                        if (!shift)
2878
                        {
3682
                        {
Line 2892... Line 3696...
2892
                     shift = 4;
3696
                     shift = 4;
2893
                     for (i = 0; i < row_width; i++)
3697
                     for (i = 0; i < row_width; i++)
2894
                     {
3698
                     {
2895
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
3699
                        if ((png_uint_16)((*sp >> shift) & 0x0f)
2896
                            == trans_color->gray)
3700
                            == png_ptr->trans_color.gray)
2897
                        {
3701
                        {
2898
                           *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
3702
                           unsigned int tmp = *sp & (0xf0f >> (4 - shift));
2899
                           *sp |= (png_byte)(background->gray << shift);
3703
                           tmp |= png_ptr->background.gray << shift;
2900
                        }
3704
                           *sp = (png_byte)(tmp & 0xff);
-
 
3705
                        }
2901
 
3706
 
Line 2902... Line 3707...
2902
                        if (!shift)
3707
                        if (!shift)
2903
                        {
3708
                        {
2904
                           shift = 4;
3709
                           shift = 4;
Line 2919... Line 3724...
2919
                  {
3724
                  {
2920
                     sp = row;
3725
                     sp = row;
2921
                     for (i = 0; i < row_width; i++, sp++)
3726
                     for (i = 0; i < row_width; i++, sp++)
2922
                     {
3727
                     {
2923
                        if (*sp == trans_color->gray)
3728
                        if (*sp == png_ptr->trans_color.gray)
2924
                           *sp = (png_byte)background->gray;
3729
                           *sp = (png_byte)png_ptr->background.gray;
2925
 
3730
 
Line 2926... Line 3731...
2926
                        else
3731
                        else
2927
                           *sp = gamma_table[*sp];
3732
                           *sp = gamma_table[*sp];
2928
                     }
3733
                     }
2929
                  }
3734
                  }
Line 2932... Line 3737...
2932
                  {
3737
                  {
2933
                     sp = row;
3738
                     sp = row;
2934
                     for (i = 0; i < row_width; i++, sp++)
3739
                     for (i = 0; i < row_width; i++, sp++)
2935
                     {
3740
                     {
2936
                        if (*sp == trans_color->gray)
3741
                        if (*sp == png_ptr->trans_color.gray)
2937
                           *sp = (png_byte)background->gray;
3742
                           *sp = (png_byte)png_ptr->background.gray;
2938
                     }
3743
                     }
2939
                  }
3744
                  }
2940
                  break;
3745
                  break;
2941
               }
3746
               }
2942
 
3747
 
Line 2951... Line 3756...
2951
                        png_uint_16 v;
3756
                        png_uint_16 v;
2952
 
3757
 
Line 2953... Line 3758...
2953
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3758
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Line 2954... Line 3759...
2954
 
3759
 
2955
                        if (v == trans_color->gray)
3760
                        if (v == png_ptr->trans_color.gray)
2956
                        {
3761
                        {
2957
                           /* Background is already in screen gamma */
3762
                           /* Background is already in screen gamma */
-
 
3763
                           *sp = (png_byte)((png_ptr->background.gray >> 8)
2958
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
3764
                                & 0xff);
-
 
3765
                           *(sp + 1) = (png_byte)(png_ptr->background.gray
2959
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
3766
                                & 0xff);
Line 2960... Line 3767...
2960
                        }
3767
                        }
2961
 
3768
 
2962
                        else
3769
                        else
Line 2976... Line 3783...
2976
                        png_uint_16 v;
3783
                        png_uint_16 v;
2977
 
3784
 
Line 2978... Line 3785...
2978
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3785
                        v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
Line 2979... Line 3786...
2979
 
3786
 
2980
                        if (v == trans_color->gray)
3787
                        if (v == png_ptr->trans_color.gray)
2981
                        {
3788
                        {
-
 
3789
                           *sp = (png_byte)((png_ptr->background.gray >> 8)
2982
                           *sp = (png_byte)((background->gray >> 8) & 0xff);
3790
                                & 0xff);
-
 
3791
                           *(sp + 1) = (png_byte)(png_ptr->background.gray
2983
                           *(sp + 1) = (png_byte)(background->gray & 0xff);
3792
                                & 0xff);
2984
                        }
3793
                        }
2985
                     }
3794
                     }
2986
                  }
3795
                  }
2987
                  break;
3796
                  break;
Line 3002... Line 3811...
3002
               {
3811
               {
3003
                  sp = row;
3812
                  sp = row;
3004
                  for (i = 0; i < row_width; i++, sp += 3)
3813
                  for (i = 0; i < row_width; i++, sp += 3)
3005
                  {
3814
                  {
3006
                     if (*sp == trans_color->red &&
3815
                     if (*sp == png_ptr->trans_color.red &&
3007
                         *(sp + 1) == trans_color->green &&
3816
                         *(sp + 1) == png_ptr->trans_color.green &&
3008
                         *(sp + 2) == trans_color->blue)
3817
                         *(sp + 2) == png_ptr->trans_color.blue)
3009
                     {
3818
                     {
3010
                        *sp = (png_byte)background->red;
3819
                        *sp = (png_byte)png_ptr->background.red;
3011
                        *(sp + 1) = (png_byte)background->green;
3820
                        *(sp + 1) = (png_byte)png_ptr->background.green;
3012
                        *(sp + 2) = (png_byte)background->blue;
3821
                        *(sp + 2) = (png_byte)png_ptr->background.blue;
3013
                     }
3822
                     }
3014
 
3823
 
Line 3015... Line 3824...
3015
                     else
3824
                     else
3016
                     {
3825
                     {
3017
                        *sp = gamma_table[*sp];
3826
                        *sp = gamma_table[*sp];
Line 3025... Line 3834...
3025
               {
3834
               {
3026
                  sp = row;
3835
                  sp = row;
3027
                  for (i = 0; i < row_width; i++, sp += 3)
3836
                  for (i = 0; i < row_width; i++, sp += 3)
3028
                  {
3837
                  {
3029
                     if (*sp == trans_color->red &&
3838
                     if (*sp == png_ptr->trans_color.red &&
3030
                         *(sp + 1) == trans_color->green &&
3839
                         *(sp + 1) == png_ptr->trans_color.green &&
3031
                         *(sp + 2) == trans_color->blue)
3840
                         *(sp + 2) == png_ptr->trans_color.blue)
3032
                     {
3841
                     {
3033
                        *sp = (png_byte)background->red;
3842
                        *sp = (png_byte)png_ptr->background.red;
3034
                        *(sp + 1) = (png_byte)background->green;
3843
                        *(sp + 1) = (png_byte)png_ptr->background.green;
3035
                        *(sp + 2) = (png_byte)background->blue;
3844
                        *(sp + 2) = (png_byte)png_ptr->background.blue;
3036
                     }
3845
                     }
3037
                  }
3846
                  }
3038
               }
3847
               }
3039
            }
3848
            }
3040
            else /* if (row_info->bit_depth == 16) */
3849
            else /* if (row_info->bit_depth == 16) */
3041
            {
3850
            {
Line 3052... Line 3861...
3052
 
3861
 
Line 3053... Line 3862...
3053
                     png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3862
                     png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3054
                         + *(sp + 5));
3863
                         + *(sp + 5));
Line 3055... Line 3864...
3055
 
3864
 
-
 
3865
                     if (r == png_ptr->trans_color.red &&
3056
                     if (r == trans_color->red && g == trans_color->green &&
3866
                         g == png_ptr->trans_color.green &&
3057
                         b == trans_color->blue)
3867
                         b == png_ptr->trans_color.blue)
3058
                     {
3868
                     {
3059
                        /* Background is already in screen gamma */
3869
                        /* Background is already in screen gamma */
3060
                        *sp = (png_byte)((background->red >> 8) & 0xff);
3870
                        *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3061
                        *(sp + 1) = (png_byte)(background->red & 0xff);
3871
                        *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
-
 
3872
                        *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3062
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3873
                                & 0xff);
-
 
3874
                        *(sp + 3) = (png_byte)(png_ptr->background.green
3063
                        *(sp + 3) = (png_byte)(background->green & 0xff);
3875
                                & 0xff);
-
 
3876
                        *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3064
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3877
                                & 0xff);
3065
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
3878
                        *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Line 3066... Line 3879...
3066
                     }
3879
                     }
3067
 
3880
 
3068
                     else
3881
                     else
Line 3095... Line 3908...
3095
 
3908
 
Line 3096... Line 3909...
3096
                     png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3909
                     png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3097
                         + *(sp + 5));
3910
                         + *(sp + 5));
Line 3098... Line 3911...
3098
 
3911
 
-
 
3912
                     if (r == png_ptr->trans_color.red &&
3099
                     if (r == trans_color->red && g == trans_color->green &&
3913
                         g == png_ptr->trans_color.green &&
3100
                         b == trans_color->blue)
3914
                         b == png_ptr->trans_color.blue)
3101
                     {
3915
                     {
3102
                        *sp = (png_byte)((background->red >> 8) & 0xff);
3916
                        *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
3103
                        *(sp + 1) = (png_byte)(background->red & 0xff);
3917
                        *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
-
 
3918
                        *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3104
                        *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
3919
                                & 0xff);
-
 
3920
                        *(sp + 3) = (png_byte)(png_ptr->background.green
3105
                        *(sp + 3) = (png_byte)(background->green & 0xff);
3921
                                & 0xff);
-
 
3922
                        *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3106
                        *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
3923
                                & 0xff);
3107
                        *(sp + 5) = (png_byte)(background->blue & 0xff);
3924
                        *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3108
                     }
3925
                     }
3109
                  }
3926
                  }
3110
               }
3927
               }
3111
            }
3928
            }
Line 3120... Line 3937...
3120
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3937
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3121
                   gamma_table != NULL)
3938
                   gamma_table != NULL)
3122
               {
3939
               {
3123
                  sp = row;
3940
                  sp = row;
3124
                  dp = row;
3941
                  for (i = 0; i < row_width; i++, sp += 2)
3125
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
-
 
3126
                  {
3942
                  {
3127
                     png_uint_16 a = *(sp + 1);
3943
                     png_uint_16 a = *(sp + 1);
3128
 
3944
 
Line 3129... Line 3945...
3129
                     if (a == 0xff)
3945
                     if (a == 0xff)
3130
                        *dp = gamma_table[*sp];
3946
                        *sp = gamma_table[*sp];
Line 3131... Line 3947...
3131
 
3947
 
3132
                     else if (a == 0)
3948
                     else if (a == 0)
3133
                     {
3949
                     {
3134
                        /* Background is already in screen gamma */
3950
                        /* Background is already in screen gamma */
3135
                        *dp = (png_byte)background->gray;
3951
                        *sp = (png_byte)png_ptr->background.gray;
Line 3136... Line 3952...
3136
                     }
3952
                     }
3137
 
3953
 
3138
                     else
3954
                     else
Line 3139... Line 3955...
3139
                     {
3955
                     {
3140
                        png_byte v, w;
3956
                        png_byte v, w;
-
 
3957
 
3141
 
3958
                        v = gamma_to_1[*sp];
-
 
3959
                        png_composite(w, v, a, png_ptr->background_1.gray);
3142
                        v = gamma_to_1[*sp];
3960
                        if (!optimize)
3143
                        png_composite(w, v, a, background_1->gray);
3961
                           w = gamma_from_1[w];
3144
                        *dp = gamma_from_1[w];
3962
                        *sp = w;
3145
                     }
3963
                     }
3146
                  }
3964
                  }
3147
               }
3965
               }
3148
               else
3966
               else
3149
#endif
-
 
3150
               {
3967
#endif
3151
                  sp = row;
3968
               {
3152
                  dp = row;
3969
                  sp = row;
Line 3153... Line 3970...
3153
                  for (i = 0; i < row_width; i++, sp += 2, dp++)
3970
                  for (i = 0; i < row_width; i++, sp += 2)
3154
                  {
-
 
3155
                     png_byte a = *(sp + 1);
-
 
3156
 
-
 
3157
                     if (a == 0xff)
-
 
3158
                        *dp = *sp;
3971
                  {
Line 3159... Line 3972...
3159
 
3972
                     png_byte a = *(sp + 1);
3160
#ifdef PNG_READ_GAMMA_SUPPORTED
3973
 
3161
                     else if (a == 0)
-
 
3162
                        *dp = (png_byte)background->gray;
-
 
3163
 
-
 
3164
                     else
-
 
3165
                        png_composite(*dp, *sp, a, background_1->gray);
3974
                     if (a == 0)
3166
 
3975
                        *sp = (png_byte)png_ptr->background.gray;
3167
#else
3976
 
3168
                     *dp = (png_byte)background->gray;
3977
                     else if (a < 0xff)
3169
#endif
3978
                        png_composite(*sp, *sp, a, png_ptr->background.gray);
3170
                  }
3979
                  }
3171
               }
3980
               }
3172
            }
3981
            }
3173
            else /* if (png_ptr->bit_depth == 16) */
3982
            else /* if (png_ptr->bit_depth == 16) */
3174
            {
3983
            {
3175
#ifdef PNG_READ_GAMMA_SUPPORTED
-
 
3176
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3984
#ifdef PNG_READ_GAMMA_SUPPORTED
3177
                   gamma_16_to_1 != NULL)
3985
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3178
               {
3986
                   gamma_16_to_1 != NULL)
3179
                  sp = row;
3987
               {
Line 3180... Line 3988...
3180
                  dp = row;
3988
                  sp = row;
3181
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
3989
                  for (i = 0; i < row_width; i++, sp += 4)
3182
                  {
3990
                  {
Line 3183... Line 3991...
3183
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3991
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
3184
                         + *(sp + 3));
3992
                         + *(sp + 3));
3185
 
3993
 
3186
                     if (a == (png_uint_16)0xffff)
3994
                     if (a == (png_uint_16)0xffff)
Line 3187... Line -...
3187
                     {
-
 
3188
                        png_uint_16 v;
3995
                     {
3189
 
-
 
3190
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-
 
3191
                        *dp = (png_byte)((v >> 8) & 0xff);
-
 
3192
                        *(dp + 1) = (png_byte)(v & 0xff);
3996
                        png_uint_16 v;
3193
                     }
3997
 
3194
 
3998
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
-
 
3999
                        *sp = (png_byte)((v >> 8) & 0xff);
3195
#ifdef PNG_READ_GAMMA_SUPPORTED
4000
                        *(sp + 1) = (png_byte)(v & 0xff);
3196
                     else if (a == 0)
4001
                     }
Line 3197... Line -...
3197
#else
-
 
3198
                     else
4002
 
3199
#endif
4003
                     else if (a == 0)
3200
                     {
4004
                     {
Line 3201... Line 4005...
3201
                        /* Background is already in screen gamma */
4005
                        /* Background is already in screen gamma */
3202
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
4006
                        *sp = (png_byte)((png_ptr->background.gray >> 8)
-
 
4007
                                & 0xff);
-
 
4008
                        *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
-
 
4009
                     }
3203
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
4010
 
3204
                     }
4011
                     else
3205
 
4012
                     {
3206
#ifdef PNG_READ_GAMMA_SUPPORTED
4013
                        png_uint_16 g, v, w;
3207
                     else
-
 
3208
                     {
4014
 
3209
                        png_uint_16 g, v, w;
4015
                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
3210
 
4016
                        png_composite_16(v, g, a, png_ptr->background_1.gray);
3211
                        g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
4017
                        if (optimize)
3212
                        png_composite_16(v, g, a, background_1->gray);
4018
                           w = v;
3213
                        w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
4019
                        else
3214
                        *dp = (png_byte)((w >> 8) & 0xff);
-
 
3215
                        *(dp + 1) = (png_byte)(w & 0xff);
4020
                           w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
3216
                     }
4021
                        *sp = (png_byte)((w >> 8) & 0xff);
3217
#endif
4022
                        *(sp + 1) = (png_byte)(w & 0xff);
3218
                  }
4023
                     }
Line 3219... Line -...
3219
               }
-
 
3220
               else
-
 
3221
#endif
-
 
3222
               {
-
 
3223
                  sp = row;
4024
                  }
3224
                  dp = row;
-
 
3225
                  for (i = 0; i < row_width; i++, sp += 4, dp += 2)
-
 
3226
                  {
-
 
3227
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
4025
               }
3228
                         + *(sp + 3));
4026
               else
-
 
4027
#endif
3229
 
4028
               {
3230
                     if (a == (png_uint_16)0xffff)
4029
                  sp = row;
Line 3231... Line -...
3231
                        png_memcpy(dp, sp, 2);
-
 
3232
 
4030
                  for (i = 0; i < row_width; i++, sp += 4)
3233
#ifdef PNG_READ_GAMMA_SUPPORTED
4031
                  {
3234
                     else if (a == 0)
4032
                     png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
Line 3235... Line 4033...
3235
#else
4033
                         + *(sp + 3));
3236
                     else
4034
 
3237
#endif
4035
                     if (a == 0)
3238
                     {
4036
                     {
3239
                        *dp = (png_byte)((background->gray >> 8) & 0xff);
4037
                        *sp = (png_byte)((png_ptr->background.gray >> 8)
3240
                        *(dp + 1) = (png_byte)(background->gray & 0xff);
-
 
3241
                     }
4038
                                & 0xff);
3242
 
4039
                        *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
3243
#ifdef PNG_READ_GAMMA_SUPPORTED
4040
                     }
3244
                     else
4041
 
3245
                     {
4042
                     else if (a < 0xffff)
Line 3265... Line 4062...
3265
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
4062
               if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
3266
                   gamma_table != NULL)
4063
                   gamma_table != NULL)
3267
               {
4064
               {
3268
                  sp = row;
4065
                  sp = row;
3269
                  dp = row;
4066
                  for (i = 0; i < row_width; i++, sp += 4)
3270
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
-
 
3271
                  {
4067
                  {
3272
                     png_byte a = *(sp + 3);
4068
                     png_byte a = *(sp + 3);
3273
 
4069
 
Line 3274... Line 4070...
3274
                     if (a == 0xff)
4070
                     if (a == 0xff)
3275
                     {
4071
                     {
3276
                        *dp = gamma_table[*sp];
4072
                        *sp = gamma_table[*sp];
3277
                        *(dp + 1) = gamma_table[*(sp + 1)];
4073
                        *(sp + 1) = gamma_table[*(sp + 1)];
3278
                        *(dp + 2) = gamma_table[*(sp + 2)];
4074
                        *(sp + 2) = gamma_table[*(sp + 2)];
3279
                     }
4075
                     }
Line 3280... Line 4076...
3280
 
4076
 
3281
                     else if (a == 0)
4077
                     else if (a == 0)
3282
                     {
4078
                     {
3283
                        /* Background is already in screen gamma */
4079
                        /* Background is already in screen gamma */
3284
                        *dp = (png_byte)background->red;
4080
                        *sp = (png_byte)png_ptr->background.red;
3285
                        *(dp + 1) = (png_byte)background->green;
4081
                        *(sp + 1) = (png_byte)png_ptr->background.green;
3286
                        *(dp + 2) = (png_byte)background->blue;
4082
                        *(sp + 2) = (png_byte)png_ptr->background.blue;
Line 3287... Line 4083...
3287
                     }
4083
                     }
3288
 
4084
 
3289
                     else
4085
                     else
Line 3290... Line 4086...
3290
                     {
4086
                     {
3291
                        png_byte v, w;
4087
                        png_byte v, w;
3292
 
4088
 
-
 
4089
                        v = gamma_to_1[*sp];
Line 3293... Line 4090...
3293
                        v = gamma_to_1[*sp];
4090
                        png_composite(w, v, a, png_ptr->background_1.red);
3294
                        png_composite(w, v, a, background_1->red);
4091
                        if (!optimize) w = gamma_from_1[w];
3295
                        *dp = gamma_from_1[w];
4092
                        *sp = w;
-
 
4093
 
Line 3296... Line 4094...
3296
 
4094
                        v = gamma_to_1[*(sp + 1)];
3297
                        v = gamma_to_1[*(sp + 1)];
4095
                        png_composite(w, v, a, png_ptr->background_1.green);
3298
                        png_composite(w, v, a, background_1->green);
4096
                        if (!optimize) w = gamma_from_1[w];
-
 
4097
                        *(sp + 1) = w;
3299
                        *(dp + 1) = gamma_from_1[w];
4098
 
3300
 
4099
                        v = gamma_to_1[*(sp + 2)];
3301
                        v = gamma_to_1[*(sp + 2)];
4100
                        png_composite(w, v, a, png_ptr->background_1.blue);
3302
                        png_composite(w, v, a, background_1->blue);
4101
                        if (!optimize) w = gamma_from_1[w];
3303
                        *(dp + 2) = gamma_from_1[w];
4102
                        *(sp + 2) = w;
3304
                     }
4103
                     }
3305
                  }
4104
                  }
3306
               }
-
 
3307
               else
4105
               }
3308
#endif
4106
               else
3309
               {
4107
#endif
Line 3310... Line 4108...
3310
                  sp = row;
4108
               {
3311
                  dp = row;
-
 
3312
                  for (i = 0; i < row_width; i++, sp += 4, dp += 3)
-
 
3313
                  {
-
 
3314
                     png_byte a = *(sp + 3);
-
 
3315
 
-
 
3316
                     if (a == 0xff)
-
 
3317
                     {
-
 
3318
                        *dp = *sp;
4109
                  sp = row;
3319
                        *(dp + 1) = *(sp + 1);
4110
                  for (i = 0; i < row_width; i++, sp += 4)
3320
                        *(dp + 2) = *(sp + 2);
4111
                  {
3321
                     }
4112
                     png_byte a = *(sp + 3);
3322
 
4113
 
Line 3323... Line 4114...
3323
                     else if (a == 0)
4114
                     if (a == 0)
3324
                     {
4115
                     {
3325
                        *dp = (png_byte)background->red;
4116
                        *sp = (png_byte)png_ptr->background.red;
Line 3326... Line 4117...
3326
                        *(dp + 1) = (png_byte)background->green;
4117
                        *(sp + 1) = (png_byte)png_ptr->background.green;
3327
                        *(dp + 2) = (png_byte)background->blue;
4118
                        *(sp + 2) = (png_byte)png_ptr->background.blue;
Line 3328... Line 4119...
3328
                     }
4119
                     }
3329
 
4120
 
3330
                     else
4121
                     else if (a < 0xff)
3331
                     {
4122
                     {
3332
                        png_composite(*dp, *sp, a, background->red);
4123
                        png_composite(*sp, *sp, a, png_ptr->background.red);
3333
 
4124
 
3334
                        png_composite(*(dp + 1), *(sp + 1), a,
4125
                        png_composite(*(sp + 1), *(sp + 1), a,
Line 3346... Line 4137...
3346
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
4137
               if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
3347
                   gamma_16_to_1 != NULL)
4138
                   gamma_16_to_1 != NULL)
3348
               {
4139
               {
3349
                  sp = row;
4140
                  sp = row;
3350
                  dp = row;
4141
                  for (i = 0; i < row_width; i++, sp += 8)
3351
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
-
 
3352
                  {
4142
                  {
3353
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
4143
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3354
                         << 8) + (png_uint_16)(*(sp + 7)));
4144
                         << 8) + (png_uint_16)(*(sp + 7)));
3355
 
4145
 
Line 3356... Line 4146...
3356
                     if (a == (png_uint_16)0xffff)
4146
                     if (a == (png_uint_16)0xffff)
3357
                     {
4147
                     {
3358
                        png_uint_16 v;
4148
                        png_uint_16 v;
Line 3359... Line 4149...
3359
 
4149
 
3360
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
4150
                        v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
3361
                        *dp = (png_byte)((v >> 8) & 0xff);
4151
                        *sp = (png_byte)((v >> 8) & 0xff);
Line 3362... Line 4152...
3362
                        *(dp + 1) = (png_byte)(v & 0xff);
4152
                        *(sp + 1) = (png_byte)(v & 0xff);
3363
 
4153
 
3364
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
4154
                        v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
Line 3365... Line 4155...
3365
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
4155
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3366
                        *(dp + 3) = (png_byte)(v & 0xff);
4156
                        *(sp + 3) = (png_byte)(v & 0xff);
3367
 
4157
 
3368
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
4158
                        v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
Line 3369... Line 4159...
3369
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
4159
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3370
                        *(dp + 5) = (png_byte)(v & 0xff);
4160
                        *(sp + 5) = (png_byte)(v & 0xff);
3371
                     }
4161
                     }
3372
 
4162
 
3373
                     else if (a == 0)
4163
                     else if (a == 0)
3374
                     {
4164
                     {
-
 
4165
                        /* Background is already in screen gamma */
3375
                        /* Background is already in screen gamma */
4166
                        *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
-
 
4167
                        *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3376
                        *dp = (png_byte)((background->red >> 8) & 0xff);
4168
                        *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
-
 
4169
                                & 0xff);
3377
                        *(dp + 1) = (png_byte)(background->red & 0xff);
4170
                        *(sp + 3) = (png_byte)(png_ptr->background.green
3378
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
4171
                                & 0xff);
Line 3379... Line 4172...
3379
                        *(dp + 3) = (png_byte)(background->green & 0xff);
4172
                        *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3380
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
4173
                                & 0xff);
3381
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
4174
                        *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
Line 3382... Line 4175...
3382
                     }
4175
                     }
3383
 
4176
 
3384
                     else
-
 
-
 
4177
                     else
3385
                     {
4178
                     {
-
 
4179
                        png_uint_16 v, w;
3386
                        png_uint_16 v, w, x;
4180
 
3387
 
4181
                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
Line 3388... Line 4182...
3388
                        v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
4182
                        png_composite_16(w, v, a, png_ptr->background_1.red);
3389
                        png_composite_16(w, v, a, background_1->red);
4183
                        if (!optimize)
-
 
4184
                           w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >>
-
 
4185
                                8];
-
 
4186
                        *sp = (png_byte)((w >> 8) & 0xff);
Line 3390... Line -...
3390
 
-
 
3391
                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
4187
                        *(sp + 1) = (png_byte)(w & 0xff);
3392
                        *dp = (png_byte)((x >> 8) & 0xff);
4188
 
Line 3393... Line 4189...
3393
                        *(dp + 1) = (png_byte)(x & 0xff);
4189
                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
3394
 
4190
                        png_composite_16(w, v, a, png_ptr->background_1.green);
-
 
4191
                        if (!optimize)
-
 
4192
                           w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >>
-
 
4193
                                8];
Line 3395... Line -...
3395
                        v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
-
 
3396
                        png_composite_16(w, v, a, background_1->green);
4194
 
3397
 
4195
                        *(sp + 2) = (png_byte)((w >> 8) & 0xff);
3398
                        x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
4196
                        *(sp + 3) = (png_byte)(w & 0xff);
3399
                        *(dp + 2) = (png_byte)((x >> 8) & 0xff);
4197
 
3400
                        *(dp + 3) = (png_byte)(x & 0xff);
4198
                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
Line 3401... Line 4199...
3401
 
4199
                        png_composite_16(w, v, a, png_ptr->background_1.blue);
3402
                        v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
4200
                        if (!optimize)
3403
                        png_composite_16(w, v, a, background_1->blue);
4201
                           w = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >>
3404
 
4202
                                8];
3405
                        x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
-
 
3406
                        *(dp + 4) = (png_byte)((x >> 8) & 0xff);
4203
 
3407
                        *(dp + 5) = (png_byte)(x & 0xff);
4204
                        *(sp + 4) = (png_byte)((w >> 8) & 0xff);
3408
                     }
4205
                        *(sp + 5) = (png_byte)(w & 0xff);
3409
                  }
4206
                     }
Line 3410... Line -...
3410
               }
-
 
3411
 
-
 
3412
               else
-
 
3413
#endif
-
 
3414
               {
-
 
3415
                  sp = row;
4207
                  }
3416
                  dp = row;
4208
               }
3417
                  for (i = 0; i < row_width; i++, sp += 8, dp += 6)
4209
 
3418
                  {
4210
               else
3419
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
4211
#endif
-
 
4212
               {
3420
                         << 8) + (png_uint_16)(*(sp + 7)));
4213
                  sp = row;
-
 
4214
                  for (i = 0; i < row_width; i++, sp += 8)
3421
 
4215
                  {
-
 
4216
                     png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
3422
                     if (a == (png_uint_16)0xffff)
4217
                         << 8) + (png_uint_16)(*(sp + 7)));
3423
                     {
4218
 
Line 3424... Line 4219...
3424
                        png_memcpy(dp, sp, 6);
4219
                     if (a == 0)
3425
                     }
4220
                     {
3426
 
4221
                        *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
Line 3427... Line 4222...
3427
                     else if (a == 0)
4222
                        *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
3428
                     {
4223
                        *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
3429
                        *dp = (png_byte)((background->red >> 8) & 0xff);
4224
                                & 0xff);
3430
                        *(dp + 1) = (png_byte)(background->red & 0xff);
4225
                        *(sp + 3) = (png_byte)(png_ptr->background.green
3431
                        *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
4226
                                & 0xff);
Line 3432... Line 4227...
3432
                        *(dp + 3) = (png_byte)(background->green & 0xff);
4227
                        *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
3433
                        *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
4228
                                & 0xff);
3434
                        *(dp + 5) = (png_byte)(background->blue & 0xff);
4229
                        *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
3435
                     }
4230
                     }
3436
 
4231
 
3437
                     else
4232
                     else if (a < 0xffff)
3438
                     {
4233
                     {
3439
                        png_uint_16 v;
4234
                        png_uint_16 v;
3440
 
4235
 
3441
                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
4236
                        png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
3442
                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
4237
                        png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
3443
                            + *(sp + 3));
4238
                            + *(sp + 3));
3444
                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
4239
                        png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
3445
                            + *(sp + 5));
4240
                            + *(sp + 5));
3446
 
4241
 
3447
                        png_composite_16(v, r, a, background->red);
4242
                        png_composite_16(v, r, a, png_ptr->background.red);
3448
                        *dp = (png_byte)((v >> 8) & 0xff);
4243
                        *sp = (png_byte)((v >> 8) & 0xff);
Line 3449... Line 4244...
3449
                        *(dp + 1) = (png_byte)(v & 0xff);
4244
                        *(sp + 1) = (png_byte)(v & 0xff);
3450
 
4245
 
3451
                        png_composite_16(v, g, a, background->green);
4246
                        png_composite_16(v, g, a, png_ptr->background.green);
3452
                        *(dp + 2) = (png_byte)((v >> 8) & 0xff);
-
 
3453
                        *(dp + 3) = (png_byte)(v & 0xff);
-
 
3454
 
-
 
3455
                        png_composite_16(v, b, a, background->blue);
-
 
3456
                        *(dp + 4) = (png_byte)((v >> 8) & 0xff);
-
 
3457
                        *(dp + 5) = (png_byte)(v & 0xff);
-
 
3458
                     }
-
 
3459
                  }
-
 
3460
               }
-
 
3461
            }
-
 
3462
            break;
4247
                        *(sp + 2) = (png_byte)((v >> 8) & 0xff);
3463
         }
4248
                        *(sp + 3) = (png_byte)(v & 0xff);
3464
 
-
 
-
 
4249
 
Line 3465... Line 4250...
3465
         default:
4250
                        png_composite_16(v, b, a, png_ptr->background.blue);
3466
            break;
4251
                        *(sp + 4) = (png_byte)((v >> 8) & 0xff);
3467
      }
4252
                        *(sp + 5) = (png_byte)(v & 0xff);
3468
 
4253
                     }
3469
      if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
4254
                  }
3470
      {
4255
               }
3471
         row_info->color_type = (png_byte)(row_info->color_type &
4256
            }
3472
             ~PNG_COLOR_MASK_ALPHA);
4257
            break;
3473
         row_info->channels--;
4258
         }
3474
         row_info->pixel_depth = (png_byte)(row_info->channels *
-
 
3475
             row_info->bit_depth);
-
 
3476
         row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
4259
 
-
 
4260
         default:
-
 
4261
            break;
-
 
4262
      }
-
 
4263
   }
3477
      }
4264
}
3478
   }
4265
#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_READ_ALPHA_MODE_SUPPORTED */
3479
}
4266
 
Line 3480... Line 4267...
3480
#endif
4267
#ifdef PNG_READ_GAMMA_SUPPORTED
Line 3679... Line 4466...
3679
   }
4466
   }
3680
}
4467
}
3681
#endif
4468
#endif
3682
 
4469
 
Line -... Line 4470...
-
 
4470
#ifdef PNG_READ_ALPHA_MODE_SUPPORTED
-
 
4471
/* Encode the alpha channel to the output gamma (the input channel is always
-
 
4472
 * linear.)  Called only with color types that have an alpha channel.  Needs the
-
 
4473
 * from_1 tables.
-
 
4474
 */
-
 
4475
void /* PRIVATE */
-
 
4476
png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
-
 
4477
{
-
 
4478
   png_uint_32 row_width = row_info->width;
-
 
4479
 
-
 
4480
   png_debug(1, "in png_do_encode_alpha");
-
 
4481
 
-
 
4482
   if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
-
 
4483
   {
-
 
4484
      if (row_info->bit_depth == 8)
-
 
4485
      {
-
 
4486
         PNG_CONST png_bytep table = png_ptr->gamma_from_1;
-
 
4487
 
-
 
4488
         if (table != NULL)
-
 
4489
         {
-
 
4490
            PNG_CONST int step =
-
 
4491
               (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
-
 
4492
 
-
 
4493
            /* The alpha channel is the last component: */
-
 
4494
            row += step - 1;
-
 
4495
 
-
 
4496
            for (; row_width > 0; --row_width, row += step)
-
 
4497
               *row = table[*row];
-
 
4498
 
-
 
4499
            return;
-
 
4500
         }
-
 
4501
      }
-
 
4502
 
-
 
4503
      else if (row_info->bit_depth == 16)
-
 
4504
      {
-
 
4505
         PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1;
-
 
4506
         PNG_CONST int gamma_shift = png_ptr->gamma_shift;
-
 
4507
 
-
 
4508
         if (table != NULL)
-
 
4509
         {
-
 
4510
            PNG_CONST int step =
-
 
4511
               (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
-
 
4512
 
-
 
4513
            /* The alpha channel is the last component: */
-
 
4514
            row += step - 2;
-
 
4515
 
-
 
4516
            for (; row_width > 0; --row_width, row += step)
-
 
4517
            {
-
 
4518
               png_uint_16 v;
-
 
4519
 
-
 
4520
               v = table[*(row + 1) >> gamma_shift][*row];
-
 
4521
               *row = (png_byte)((v >> 8) & 0xff);
-
 
4522
               *(row + 1) = (png_byte)(v & 0xff);
-
 
4523
            }
-
 
4524
 
-
 
4525
            return;
-
 
4526
         }
-
 
4527
      }
-
 
4528
   }
-
 
4529
 
-
 
4530
   /* Only get to here if called with a weird row_info; no harm has been done,
-
 
4531
    * so just issue a warning.
-
 
4532
    */
-
 
4533
   png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
-
 
4534
}
-
 
4535
#endif
-
 
4536
 
3683
#ifdef PNG_READ_EXPAND_SUPPORTED
4537
#ifdef PNG_READ_EXPAND_SUPPORTED
3684
/* Expands a palette row to an RGB or RGBA row depending
4538
/* Expands a palette row to an RGB or RGBA row depending
3685
 * upon whether you supply trans and num_trans.
4539
 * upon whether you supply trans and num_trans.
3686
 */
4540
 */
3687
void /* PRIVATE */
4541
void /* PRIVATE */
Line 3784... Line 4638...
3784
 
4638
 
Line 3785... Line 4639...
3785
      if (row_info->bit_depth == 8)
4639
      if (row_info->bit_depth == 8)
3786
      {
4640
      {
3787
         {
4641
         {
3788
            if (trans_alpha != NULL)
4642
            if (num_trans > 0)
3789
            {
4643
            {
3790
               sp = row + (png_size_t)row_width - 1;
4644
               sp = row + (png_size_t)row_width - 1;
3791
               dp = row + (png_size_t)(row_width << 2) - 1;
4645
               dp = row + (png_size_t)(row_width << 2) - 1;
Line 3792... Line 4646...
3792
 
4646
 
Line 3838... Line 4692...
3838
 * expanded transparency value is supplied, an alpha channel is built.
4692
 * expanded transparency value is supplied, an alpha channel is built.
3839
 */
4693
 */
3840
void /* PRIVATE */
4694
void /* PRIVATE */
3841
png_do_expand(png_row_infop row_info, png_bytep row,
4695
png_do_expand(png_row_infop row_info, png_bytep row,
3842
    png_const_color_16p trans_value)
4696
    png_const_color_16p trans_color)
3843
{
4697
{
3844
   int shift, value;
4698
   int shift, value;
3845
   png_bytep sp, dp;
4699
   png_bytep sp, dp;
3846
   png_uint_32 i;
4700
   png_uint_32 i;
3847
   png_uint_32 row_width=row_info->width;
4701
   png_uint_32 row_width=row_info->width;
3848
 
4702
 
Line 3850... Line 4704...
3850
 
4704
 
Line 3851... Line 4705...
3851
   {
4705
   {
3852
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
4706
      if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
3853
      {
4707
      {
3854
         png_uint_16 gray = (png_uint_16)(trans_value ? trans_value->gray : 0);
4708
         unsigned int gray = trans_color ? trans_color->gray : 0;
Line 3855... Line 4709...
3855
 
4709
 
3856
         if (row_info->bit_depth < 8)
4710
         if (row_info->bit_depth < 8)
3857
         {
4711
         {
3858
            switch (row_info->bit_depth)
4712
            switch (row_info->bit_depth)
3859
            {
4713
            {
3860
               case 1:
4714
               case 1:
3861
               {
4715
               {
3862
                  gray = (png_uint_16)((gray & 0x01) * 0xff);
4716
                  gray = (gray & 0x01) * 0xff;
3863
                  sp = row + (png_size_t)((row_width - 1) >> 3);
4717
                  sp = row + (png_size_t)((row_width - 1) >> 3);
3864
                  dp = row + (png_size_t)row_width - 1;
4718
                  dp = row + (png_size_t)row_width - 1;
3865
                  shift = 7 - (int)((row_width + 7) & 0x07);
4719
                  shift = 7 - (int)((row_width + 7) & 0x07);
3866
                  for (i = 0; i < row_width; i++)
4720
                  for (i = 0; i < row_width; i++)
Line 3886... Line 4740...
3886
               }
4740
               }
3887
 
4741
 
Line 3888... Line 4742...
3888
               case 2:
4742
               case 2:
3889
               {
4743
               {
3890
                  gray = (png_uint_16)((gray & 0x03) * 0x55);
4744
                  gray = (gray & 0x03) * 0x55;
3891
                  sp = row + (png_size_t)((row_width - 1) >> 2);
4745
                  sp = row + (png_size_t)((row_width - 1) >> 2);
3892
                  dp = row + (png_size_t)row_width - 1;
4746
                  dp = row + (png_size_t)row_width - 1;
3893
                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
4747
                  shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
3894
                  for (i = 0; i < row_width; i++)
4748
                  for (i = 0; i < row_width; i++)
3895
                  {
4749
                  {
Line 3911... Line 4765...
3911
               }
4765
               }
3912
 
4766
 
Line 3913... Line 4767...
3913
               case 4:
4767
               case 4:
3914
               {
4768
               {
3915
                  gray = (png_uint_16)((gray & 0x0f) * 0x11);
4769
                  gray = (gray & 0x0f) * 0x11;
3916
                  sp = row + (png_size_t)((row_width - 1) >> 1);
4770
                  sp = row + (png_size_t)((row_width - 1) >> 1);
3917
                  dp = row + (png_size_t)row_width - 1;
4771
                  dp = row + (png_size_t)row_width - 1;
3918
                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
4772
                  shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
3919
                  for (i = 0; i < row_width; i++)
4773
                  for (i = 0; i < row_width; i++)
3920
                  {
4774
                  {
Line 3942... Line 4796...
3942
            row_info->pixel_depth = 8;
4796
            row_info->pixel_depth = 8;
3943
            row_info->rowbytes = row_width;
4797
            row_info->rowbytes = row_width;
3944
         }
4798
         }
3945
 
4799
 
Line 3946... Line 4800...
3946
         if (trans_value != NULL)
4800
         if (trans_color != NULL)
3947
         {
4801
         {
3948
            if (row_info->bit_depth == 8)
4802
            if (row_info->bit_depth == 8)
3949
            {
4803
            {
3950
               gray = gray & 0xff;
4804
               gray = gray & 0xff;
3951
               sp = row + (png_size_t)row_width - 1;
4805
               sp = row + (png_size_t)row_width - 1;
Line 3964... Line 4818...
3964
            }
4818
            }
3965
 
4819
 
Line 3966... Line 4820...
3966
            else if (row_info->bit_depth == 16)
4820
            else if (row_info->bit_depth == 16)
3967
            {
4821
            {
3968
               png_byte gray_high = (png_byte)((gray >> 8) & 0xff);
4822
               unsigned int gray_high = (gray >> 8) & 0xff;
3969
               png_byte gray_low = (png_byte)(gray & 0xff);
4823
               unsigned int gray_low = gray & 0xff;
3970
               sp = row + row_info->rowbytes - 1;
4824
               sp = row + row_info->rowbytes - 1;
3971
               dp = row + (row_info->rowbytes << 1) - 1;
4825
               dp = row + (row_info->rowbytes << 1) - 1;
3972
               for (i = 0; i < row_width; i++)
4826
               for (i = 0; i < row_width; i++)
3973
               {
4827
               {
3974
                  if (*(sp - 1) == gray_high && *(sp) == gray_low)
4828
                  if (*(sp - 1) == gray_high && *(sp) == gray_low)
Line 3994... Line 4848...
3994
            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
4848
            row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
3995
               row_width);
4849
               row_width);
3996
         }
4850
         }
3997
      }
4851
      }
3998
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
4852
      else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_color)
3999
      {
4853
      {
4000
         if (row_info->bit_depth == 8)
4854
         if (row_info->bit_depth == 8)
4001
         {
4855
         {
4002
            png_byte red = (png_byte)(trans_value->red & 0xff);
4856
            png_byte red = (png_byte)(trans_color->red & 0xff);
4003
            png_byte green = (png_byte)(trans_value->green & 0xff);
4857
            png_byte green = (png_byte)(trans_color->green & 0xff);
4004
            png_byte blue = (png_byte)(trans_value->blue & 0xff);
4858
            png_byte blue = (png_byte)(trans_color->blue & 0xff);
4005
            sp = row + (png_size_t)row_info->rowbytes - 1;
4859
            sp = row + (png_size_t)row_info->rowbytes - 1;
4006
            dp = row + (png_size_t)(row_width << 2) - 1;
4860
            dp = row + (png_size_t)(row_width << 2) - 1;
4007
            for (i = 0; i < row_width; i++)
4861
            for (i = 0; i < row_width; i++)
4008
            {
4862
            {
4009
               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4863
               if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
4010
                  *dp-- = 0;
4864
                  *dp-- = 0;
Line 4018... Line 4872...
4018
            }
4872
            }
4019
         }
4873
         }
4020
         else if (row_info->bit_depth == 16)
4874
         else if (row_info->bit_depth == 16)
4021
         {
4875
         {
4022
            png_byte red_high = (png_byte)((trans_value->red >> 8) & 0xff);
4876
            png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
4023
            png_byte green_high = (png_byte)((trans_value->green >> 8) & 0xff);
4877
            png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
4024
            png_byte blue_high = (png_byte)((trans_value->blue >> 8) & 0xff);
4878
            png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
4025
            png_byte red_low = (png_byte)(trans_value->red & 0xff);
4879
            png_byte red_low = (png_byte)(trans_color->red & 0xff);
4026
            png_byte green_low = (png_byte)(trans_value->green & 0xff);
4880
            png_byte green_low = (png_byte)(trans_color->green & 0xff);
4027
            png_byte blue_low = (png_byte)(trans_value->blue & 0xff);
4881
            png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
4028
            sp = row + row_info->rowbytes - 1;
4882
            sp = row + row_info->rowbytes - 1;
4029
            dp = row + (png_size_t)(row_width << 3) - 1;
4883
            dp = row + (png_size_t)(row_width << 3) - 1;
4030
            for (i = 0; i < row_width; i++)
4884
            for (i = 0; i < row_width; i++)
4031
            {
4885
            {
4032
               if (*(sp - 5) == red_high &&
4886
               if (*(sp - 5) == red_high &&
4033
                   *(sp - 4) == red_low &&
4887
                   *(sp - 4) == red_low &&
Line 4062... Line 4916...
4062
   }
4916
   }
4063
}
4917
}
4064
#endif
4918
#endif
4065
 
4919
 
Line -... Line 4920...
-
 
4920
#ifdef PNG_READ_EXPAND_16_SUPPORTED
-
 
4921
/* If the bit depth is 8 and the color type is not a palette type expand the
-
 
4922
 * whole row to 16 bits.  Has no effect otherwise.
-
 
4923
 */
-
 
4924
void /* PRIVATE */
-
 
4925
png_do_expand_16(png_row_infop row_info, png_bytep row)
-
 
4926
{
-
 
4927
   if (row_info->bit_depth == 8 &&
-
 
4928
      row_info->color_type != PNG_COLOR_TYPE_PALETTE)
-
 
4929
   {
-
 
4930
      /* The row have a sequence of bytes containing [0..255] and we need
-
 
4931
       * to turn it into another row containing [0..65535], to do this we
-
 
4932
       * calculate:
-
 
4933
       *
-
 
4934
       *  (input / 255) * 65535
-
 
4935
       *
-
 
4936
       *  Which happens to be exactly input * 257 and this can be achieved
-
 
4937
       *  simply by byte replication in place (copying backwards).
-
 
4938
       */
-
 
4939
      png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
-
 
4940
      png_byte *dp = sp + row_info->rowbytes;  /* destination, end + 1 */
-
 
4941
      while (dp > sp)
-
 
4942
         dp[-2] = dp[-1] = *--sp, dp -= 2;
-
 
4943
 
-
 
4944
      row_info->rowbytes *= 2;
-
 
4945
      row_info->bit_depth = 16;
-
 
4946
      row_info->pixel_depth = (png_byte)(row_info->channels * 16);
-
 
4947
   }
-
 
4948
}
-
 
4949
#endif
-
 
4950
 
4066
#ifdef PNG_READ_QUANTIZE_SUPPORTED
4951
#ifdef PNG_READ_QUANTIZE_SUPPORTED
4067
void /* PRIVATE */
4952
void /* PRIVATE */
4068
png_do_quantize(png_row_infop row_info, png_bytep row,
4953
png_do_quantize(png_row_infop row_info, png_bytep row,
4069
    png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4954
    png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
4070
{
4955
{
Line 4155... Line 5040...
4155
      }
5040
      }
4156
   }
5041
   }
4157
}
5042
}
4158
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
5043
#endif /* PNG_READ_QUANTIZE_SUPPORTED */
4159
 
5044
#endif /* PNG_READ_TRANSFORMS_SUPPORTED */
-
 
5045
 
Line 4160... Line 5046...
4160
#ifdef PNG_MNG_FEATURES_SUPPORTED
5046
#ifdef PNG_MNG_FEATURES_SUPPORTED
4161
/* Undoes intrapixel differencing  */
5047
/* Undoes intrapixel differencing  */
4162
void /* PRIVATE */
5048
void /* PRIVATE */
4163
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
5049
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
Line 4208... Line 5094...
4208
         {
5094
         {
4209
            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
5095
            png_uint_32 s0   = (*(rp    ) << 8) | *(rp + 1);
4210
            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
5096
            png_uint_32 s1   = (*(rp + 2) << 8) | *(rp + 3);
4211
            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
5097
            png_uint_32 s2   = (*(rp + 4) << 8) | *(rp + 5);
4212
            png_uint_32 red  = (png_uint_32)((s0 + s1 + 65536L) & 0xffffL);
5098
            png_uint_32 red  = (s0 + s1 + 65536) & 0xffff;
4213
            png_uint_32 blue = (png_uint_32)((s2 + s1 + 65536L) & 0xffffL);
5099
            png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
4214
            *(rp    ) = (png_byte)((red >> 8) & 0xff);
5100
            *(rp    ) = (png_byte)((red >> 8) & 0xff);
4215
            *(rp + 1) = (png_byte)(red & 0xff);
5101
            *(rp + 1) = (png_byte)(red & 0xff);
4216
            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
5102
            *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
4217
            *(rp + 5) = (png_byte)(blue & 0xff);
5103
            *(rp + 5) = (png_byte)(blue & 0xff);
4218
         }
5104
         }
4219
      }
5105
      }