Subversion Repositories Kolibri OS

Rev

Rev 1897 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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