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