Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/libpng/pngread.c
9833 views
1
/* pngread.c - read a PNG file
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 routines that an application calls directly to
13
* read a PNG file or stream.
14
*/
15
16
#include "pngpriv.h"
17
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
18
# include <errno.h>
19
#endif
20
21
#ifdef PNG_READ_SUPPORTED
22
23
/* Create a PNG structure for reading, and allocate any memory needed. */
24
PNG_FUNCTION(png_structp,PNGAPI
25
png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
26
png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
27
{
28
#ifndef PNG_USER_MEM_SUPPORTED
29
png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
30
error_fn, warn_fn, NULL, NULL, NULL);
31
#else
32
return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
33
warn_fn, NULL, NULL, NULL);
34
}
35
36
/* Alternate create PNG structure for reading, and allocate any memory
37
* needed.
38
*/
39
PNG_FUNCTION(png_structp,PNGAPI
40
png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
41
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
42
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
43
{
44
png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
45
error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
46
#endif /* USER_MEM */
47
48
if (png_ptr != NULL)
49
{
50
png_ptr->mode = PNG_IS_READ_STRUCT;
51
52
/* Added in libpng-1.6.0; this can be used to detect a read structure if
53
* required (it will be zero in a write structure.)
54
*/
55
# ifdef PNG_SEQUENTIAL_READ_SUPPORTED
56
png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
57
# endif
58
59
# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
60
png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
61
62
/* In stable builds only warn if an application error can be completely
63
* handled.
64
*/
65
# if PNG_RELEASE_BUILD
66
png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
67
# endif
68
# endif
69
70
/* TODO: delay this, it can be done in png_init_io (if the app doesn't
71
* do it itself) avoiding setting the default function if it is not
72
* required.
73
*/
74
png_set_read_fn(png_ptr, NULL, NULL);
75
}
76
77
return png_ptr;
78
}
79
80
81
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
82
/* Read the information before the actual image data. This has been
83
* changed in v0.90 to allow reading a file that already has the magic
84
* bytes read from the stream. You can tell libpng how many bytes have
85
* been read from the beginning of the stream (up to the maximum of 8)
86
* via png_set_sig_bytes(), and we will only check the remaining bytes
87
* here. The application can then have access to the signature bytes we
88
* read if it is determined that this isn't a valid PNG file.
89
*/
90
void PNGAPI
91
png_read_info(png_structrp png_ptr, png_inforp info_ptr)
92
{
93
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
94
int keep;
95
#endif
96
97
png_debug(1, "in png_read_info");
98
99
if (png_ptr == NULL || info_ptr == NULL)
100
return;
101
102
/* Read and check the PNG file signature. */
103
png_read_sig(png_ptr, info_ptr);
104
105
for (;;)
106
{
107
png_uint_32 length = png_read_chunk_header(png_ptr);
108
png_uint_32 chunk_name = png_ptr->chunk_name;
109
110
/* IDAT logic needs to happen here to simplify getting the two flags
111
* right.
112
*/
113
if (chunk_name == png_IDAT)
114
{
115
if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
116
png_chunk_error(png_ptr, "Missing IHDR before IDAT");
117
118
else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
119
(png_ptr->mode & PNG_HAVE_PLTE) == 0)
120
png_chunk_error(png_ptr, "Missing PLTE before IDAT");
121
122
else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
123
png_chunk_benign_error(png_ptr, "Too many IDATs found");
124
125
png_ptr->mode |= PNG_HAVE_IDAT;
126
}
127
128
else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
129
{
130
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
131
png_ptr->mode |= PNG_AFTER_IDAT;
132
}
133
134
if (chunk_name == png_IHDR)
135
png_handle_chunk(png_ptr, info_ptr, length);
136
137
else if (chunk_name == png_IEND)
138
png_handle_chunk(png_ptr, info_ptr, length);
139
140
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
141
else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
142
{
143
png_handle_unknown(png_ptr, info_ptr, length, keep);
144
145
if (chunk_name == png_PLTE)
146
png_ptr->mode |= PNG_HAVE_PLTE;
147
148
else if (chunk_name == png_IDAT)
149
{
150
png_ptr->idat_size = 0; /* It has been consumed */
151
break;
152
}
153
}
154
#endif
155
156
else if (chunk_name == png_IDAT)
157
{
158
png_ptr->idat_size = length;
159
break;
160
}
161
162
else
163
png_handle_chunk(png_ptr, info_ptr, length);
164
}
165
}
166
#endif /* SEQUENTIAL_READ */
167
168
/* Optional call to update the users info_ptr structure */
169
void PNGAPI
170
png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
171
{
172
png_debug(1, "in png_read_update_info");
173
174
if (png_ptr != NULL)
175
{
176
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
177
{
178
png_read_start_row(png_ptr);
179
180
# ifdef PNG_READ_TRANSFORMS_SUPPORTED
181
png_read_transform_info(png_ptr, info_ptr);
182
# else
183
PNG_UNUSED(info_ptr)
184
# endif
185
}
186
187
/* New in 1.6.0 this avoids the bug of doing the initializations twice */
188
else
189
png_app_error(png_ptr,
190
"png_read_update_info/png_start_read_image: duplicate call");
191
}
192
}
193
194
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
195
/* Initialize palette, background, etc, after transformations
196
* are set, but before any reading takes place. This allows
197
* the user to obtain a gamma-corrected palette, for example.
198
* If the user doesn't call this, we will do it ourselves.
199
*/
200
void PNGAPI
201
png_start_read_image(png_structrp png_ptr)
202
{
203
png_debug(1, "in png_start_read_image");
204
205
if (png_ptr != NULL)
206
{
207
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
208
png_read_start_row(png_ptr);
209
210
/* New in 1.6.0 this avoids the bug of doing the initializations twice */
211
else
212
png_app_error(png_ptr,
213
"png_start_read_image/png_read_update_info: duplicate call");
214
}
215
}
216
#endif /* SEQUENTIAL_READ */
217
218
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
219
#ifdef PNG_MNG_FEATURES_SUPPORTED
220
/* Undoes intrapixel differencing,
221
* NOTE: this is apparently only supported in the 'sequential' reader.
222
*/
223
static void
224
png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
225
{
226
png_debug(1, "in png_do_read_intrapixel");
227
228
if (
229
(row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
230
{
231
int bytes_per_pixel;
232
png_uint_32 row_width = row_info->width;
233
234
if (row_info->bit_depth == 8)
235
{
236
png_bytep rp;
237
png_uint_32 i;
238
239
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
240
bytes_per_pixel = 3;
241
242
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
243
bytes_per_pixel = 4;
244
245
else
246
return;
247
248
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
249
{
250
*(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
251
*(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
252
}
253
}
254
else if (row_info->bit_depth == 16)
255
{
256
png_bytep rp;
257
png_uint_32 i;
258
259
if (row_info->color_type == PNG_COLOR_TYPE_RGB)
260
bytes_per_pixel = 6;
261
262
else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
263
bytes_per_pixel = 8;
264
265
else
266
return;
267
268
for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
269
{
270
png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
271
png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
272
png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
273
png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
274
png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
275
*(rp ) = (png_byte)((red >> 8) & 0xff);
276
*(rp + 1) = (png_byte)(red & 0xff);
277
*(rp + 4) = (png_byte)((blue >> 8) & 0xff);
278
*(rp + 5) = (png_byte)(blue & 0xff);
279
}
280
}
281
}
282
}
283
#endif /* MNG_FEATURES */
284
285
void PNGAPI
286
png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
287
{
288
png_row_info row_info;
289
290
if (png_ptr == NULL)
291
return;
292
293
png_debug2(1, "in png_read_row (row %lu, pass %d)",
294
(unsigned long)png_ptr->row_number, png_ptr->pass);
295
296
/* png_read_start_row sets the information (in particular iwidth) for this
297
* interlace pass.
298
*/
299
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
300
png_read_start_row(png_ptr);
301
302
/* 1.5.6: row_info moved out of png_struct to a local here. */
303
row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
304
row_info.color_type = png_ptr->color_type;
305
row_info.bit_depth = png_ptr->bit_depth;
306
row_info.channels = png_ptr->channels;
307
row_info.pixel_depth = png_ptr->pixel_depth;
308
row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
309
310
#ifdef PNG_WARNINGS_SUPPORTED
311
if (png_ptr->row_number == 0 && png_ptr->pass == 0)
312
{
313
/* Check for transforms that have been set but were defined out */
314
#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
315
if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
316
png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
317
#endif
318
319
#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
320
if ((png_ptr->transformations & PNG_FILLER) != 0)
321
png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
322
#endif
323
324
#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
325
!defined(PNG_READ_PACKSWAP_SUPPORTED)
326
if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
327
png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
328
#endif
329
330
#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
331
if ((png_ptr->transformations & PNG_PACK) != 0)
332
png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
333
#endif
334
335
#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
336
if ((png_ptr->transformations & PNG_SHIFT) != 0)
337
png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
338
#endif
339
340
#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
341
if ((png_ptr->transformations & PNG_BGR) != 0)
342
png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
343
#endif
344
345
#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
346
if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
347
png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
348
#endif
349
}
350
#endif /* WARNINGS */
351
352
#ifdef PNG_READ_INTERLACING_SUPPORTED
353
/* If interlaced and we do not need a new row, combine row and return.
354
* Notice that the pixels we have from previous rows have been transformed
355
* already; we can only combine like with like (transformed or
356
* untransformed) and, because of the libpng API for interlaced images, this
357
* means we must transform before de-interlacing.
358
*/
359
if (png_ptr->interlaced != 0 &&
360
(png_ptr->transformations & PNG_INTERLACE) != 0)
361
{
362
switch (png_ptr->pass)
363
{
364
case 0:
365
if (png_ptr->row_number & 0x07)
366
{
367
if (dsp_row != NULL)
368
png_combine_row(png_ptr, dsp_row, 1/*display*/);
369
png_read_finish_row(png_ptr);
370
return;
371
}
372
break;
373
374
case 1:
375
if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
376
{
377
if (dsp_row != NULL)
378
png_combine_row(png_ptr, dsp_row, 1/*display*/);
379
380
png_read_finish_row(png_ptr);
381
return;
382
}
383
break;
384
385
case 2:
386
if ((png_ptr->row_number & 0x07) != 4)
387
{
388
if (dsp_row != NULL && (png_ptr->row_number & 4))
389
png_combine_row(png_ptr, dsp_row, 1/*display*/);
390
391
png_read_finish_row(png_ptr);
392
return;
393
}
394
break;
395
396
case 3:
397
if ((png_ptr->row_number & 3) || png_ptr->width < 3)
398
{
399
if (dsp_row != NULL)
400
png_combine_row(png_ptr, dsp_row, 1/*display*/);
401
402
png_read_finish_row(png_ptr);
403
return;
404
}
405
break;
406
407
case 4:
408
if ((png_ptr->row_number & 3) != 2)
409
{
410
if (dsp_row != NULL && (png_ptr->row_number & 2))
411
png_combine_row(png_ptr, dsp_row, 1/*display*/);
412
413
png_read_finish_row(png_ptr);
414
return;
415
}
416
break;
417
418
case 5:
419
if ((png_ptr->row_number & 1) || png_ptr->width < 2)
420
{
421
if (dsp_row != NULL)
422
png_combine_row(png_ptr, dsp_row, 1/*display*/);
423
424
png_read_finish_row(png_ptr);
425
return;
426
}
427
break;
428
429
default:
430
case 6:
431
if ((png_ptr->row_number & 1) == 0)
432
{
433
png_read_finish_row(png_ptr);
434
return;
435
}
436
break;
437
}
438
}
439
#endif
440
441
if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
442
png_error(png_ptr, "Invalid attempt to read row data");
443
444
/* Fill the row with IDAT data: */
445
png_ptr->row_buf[0]=255; /* to force error if no data was found */
446
png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
447
448
if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
449
{
450
if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
451
png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
452
png_ptr->prev_row + 1, png_ptr->row_buf[0]);
453
else
454
png_error(png_ptr, "bad adaptive filter value");
455
}
456
457
/* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
458
* 1.5.6, while the buffer really is this big in current versions of libpng
459
* it may not be in the future, so this was changed just to copy the
460
* interlaced count:
461
*/
462
memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
463
464
#ifdef PNG_MNG_FEATURES_SUPPORTED
465
if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
466
(png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
467
{
468
/* Intrapixel differencing */
469
png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
470
}
471
#endif
472
473
#ifdef PNG_READ_TRANSFORMS_SUPPORTED
474
if (png_ptr->transformations
475
# ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
476
|| png_ptr->num_palette_max >= 0
477
# endif
478
)
479
png_do_read_transformations(png_ptr, &row_info);
480
#endif
481
482
/* The transformed pixel depth should match the depth now in row_info. */
483
if (png_ptr->transformed_pixel_depth == 0)
484
{
485
png_ptr->transformed_pixel_depth = row_info.pixel_depth;
486
if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
487
png_error(png_ptr, "sequential row overflow");
488
}
489
490
else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
491
png_error(png_ptr, "internal sequential row size calculation error");
492
493
#ifdef PNG_READ_INTERLACING_SUPPORTED
494
/* Expand interlaced rows to full size */
495
if (png_ptr->interlaced != 0 &&
496
(png_ptr->transformations & PNG_INTERLACE) != 0)
497
{
498
if (png_ptr->pass < 6)
499
png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
500
png_ptr->transformations);
501
502
if (dsp_row != NULL)
503
png_combine_row(png_ptr, dsp_row, 1/*display*/);
504
505
if (row != NULL)
506
png_combine_row(png_ptr, row, 0/*row*/);
507
}
508
509
else
510
#endif
511
{
512
if (row != NULL)
513
png_combine_row(png_ptr, row, -1/*ignored*/);
514
515
if (dsp_row != NULL)
516
png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
517
}
518
png_read_finish_row(png_ptr);
519
520
if (png_ptr->read_row_fn != NULL)
521
(*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
522
523
}
524
#endif /* SEQUENTIAL_READ */
525
526
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
527
/* Read one or more rows of image data. If the image is interlaced,
528
* and png_set_interlace_handling() has been called, the rows need to
529
* contain the contents of the rows from the previous pass. If the
530
* image has alpha or transparency, and png_handle_alpha()[*] has been
531
* called, the rows contents must be initialized to the contents of the
532
* screen.
533
*
534
* "row" holds the actual image, and pixels are placed in it
535
* as they arrive. If the image is displayed after each pass, it will
536
* appear to "sparkle" in. "display_row" can be used to display a
537
* "chunky" progressive image, with finer detail added as it becomes
538
* available. If you do not want this "chunky" display, you may pass
539
* NULL for display_row. If you do not want the sparkle display, and
540
* you have not called png_handle_alpha(), you may pass NULL for rows.
541
* If you have called png_handle_alpha(), and the image has either an
542
* alpha channel or a transparency chunk, you must provide a buffer for
543
* rows. In this case, you do not have to provide a display_row buffer
544
* also, but you may. If the image is not interlaced, or if you have
545
* not called png_set_interlace_handling(), the display_row buffer will
546
* be ignored, so pass NULL to it.
547
*
548
* [*] png_handle_alpha() does not exist yet, as of this version of libpng
549
*/
550
551
void PNGAPI
552
png_read_rows(png_structrp png_ptr, png_bytepp row,
553
png_bytepp display_row, png_uint_32 num_rows)
554
{
555
png_uint_32 i;
556
png_bytepp rp;
557
png_bytepp dp;
558
559
png_debug(1, "in png_read_rows");
560
561
if (png_ptr == NULL)
562
return;
563
564
rp = row;
565
dp = display_row;
566
if (rp != NULL && dp != NULL)
567
for (i = 0; i < num_rows; i++)
568
{
569
png_bytep rptr = *rp++;
570
png_bytep dptr = *dp++;
571
572
png_read_row(png_ptr, rptr, dptr);
573
}
574
575
else if (rp != NULL)
576
for (i = 0; i < num_rows; i++)
577
{
578
png_bytep rptr = *rp;
579
png_read_row(png_ptr, rptr, NULL);
580
rp++;
581
}
582
583
else if (dp != NULL)
584
for (i = 0; i < num_rows; i++)
585
{
586
png_bytep dptr = *dp;
587
png_read_row(png_ptr, NULL, dptr);
588
dp++;
589
}
590
}
591
#endif /* SEQUENTIAL_READ */
592
593
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
594
/* Read the entire image. If the image has an alpha channel or a tRNS
595
* chunk, and you have called png_handle_alpha()[*], you will need to
596
* initialize the image to the current image that PNG will be overlaying.
597
* We set the num_rows again here, in case it was incorrectly set in
598
* png_read_start_row() by a call to png_read_update_info() or
599
* png_start_read_image() if png_set_interlace_handling() wasn't called
600
* prior to either of these functions like it should have been. You can
601
* only call this function once. If you desire to have an image for
602
* each pass of a interlaced image, use png_read_rows() instead.
603
*
604
* [*] png_handle_alpha() does not exist yet, as of this version of libpng
605
*/
606
void PNGAPI
607
png_read_image(png_structrp png_ptr, png_bytepp image)
608
{
609
png_uint_32 i, image_height;
610
int pass, j;
611
png_bytepp rp;
612
613
png_debug(1, "in png_read_image");
614
615
if (png_ptr == NULL)
616
return;
617
618
#ifdef PNG_READ_INTERLACING_SUPPORTED
619
if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
620
{
621
pass = png_set_interlace_handling(png_ptr);
622
/* And make sure transforms are initialized. */
623
png_start_read_image(png_ptr);
624
}
625
else
626
{
627
if (png_ptr->interlaced != 0 &&
628
(png_ptr->transformations & PNG_INTERLACE) == 0)
629
{
630
/* Caller called png_start_read_image or png_read_update_info without
631
* first turning on the PNG_INTERLACE transform. We can fix this here,
632
* but the caller should do it!
633
*/
634
png_warning(png_ptr, "Interlace handling should be turned on when "
635
"using png_read_image");
636
/* Make sure this is set correctly */
637
png_ptr->num_rows = png_ptr->height;
638
}
639
640
/* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
641
* the above error case.
642
*/
643
pass = png_set_interlace_handling(png_ptr);
644
}
645
#else
646
if (png_ptr->interlaced)
647
png_error(png_ptr,
648
"Cannot read interlaced image -- interlace handler disabled");
649
650
pass = 1;
651
#endif
652
653
image_height=png_ptr->height;
654
655
for (j = 0; j < pass; j++)
656
{
657
rp = image;
658
for (i = 0; i < image_height; i++)
659
{
660
png_read_row(png_ptr, *rp, NULL);
661
rp++;
662
}
663
}
664
}
665
#endif /* SEQUENTIAL_READ */
666
667
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
668
/* Read the end of the PNG file. Will not read past the end of the
669
* file, will verify the end is accurate, and will read any comments
670
* or time information at the end of the file, if info is not NULL.
671
*/
672
void PNGAPI
673
png_read_end(png_structrp png_ptr, png_inforp info_ptr)
674
{
675
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
676
int keep;
677
#endif
678
679
png_debug(1, "in png_read_end");
680
681
if (png_ptr == NULL)
682
return;
683
684
/* If png_read_end is called in the middle of reading the rows there may
685
* still be pending IDAT data and an owned zstream. Deal with this here.
686
*/
687
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
688
if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
689
#endif
690
png_read_finish_IDAT(png_ptr);
691
692
#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
693
/* Report invalid palette index; added at libng-1.5.10 */
694
if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
695
png_ptr->num_palette_max >= png_ptr->num_palette)
696
png_benign_error(png_ptr, "Read palette index exceeding num_palette");
697
#endif
698
699
do
700
{
701
png_uint_32 length = png_read_chunk_header(png_ptr);
702
png_uint_32 chunk_name = png_ptr->chunk_name;
703
704
if (chunk_name != png_IDAT)
705
png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
706
707
if (chunk_name == png_IEND)
708
png_handle_chunk(png_ptr, info_ptr, length);
709
710
else if (chunk_name == png_IHDR)
711
png_handle_chunk(png_ptr, info_ptr, length);
712
713
else if (info_ptr == NULL)
714
png_crc_finish(png_ptr, length);
715
716
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
717
else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
718
{
719
if (chunk_name == png_IDAT)
720
{
721
if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
722
|| (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
723
png_benign_error(png_ptr, ".Too many IDATs found");
724
}
725
png_handle_unknown(png_ptr, info_ptr, length, keep);
726
if (chunk_name == png_PLTE)
727
png_ptr->mode |= PNG_HAVE_PLTE;
728
}
729
#endif
730
731
else if (chunk_name == png_IDAT)
732
{
733
/* Zero length IDATs are legal after the last IDAT has been
734
* read, but not after other chunks have been read. 1.6 does not
735
* always read all the deflate data; specifically it cannot be relied
736
* upon to read the Adler32 at the end. If it doesn't ignore IDAT
737
* chunks which are longer than zero as well:
738
*/
739
if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
740
|| (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
741
png_benign_error(png_ptr, "..Too many IDATs found");
742
743
png_crc_finish(png_ptr, length);
744
}
745
746
else
747
png_handle_chunk(png_ptr, info_ptr, length);
748
} while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
749
}
750
#endif /* SEQUENTIAL_READ */
751
752
/* Free all memory used in the read struct */
753
static void
754
png_read_destroy(png_structrp png_ptr)
755
{
756
png_debug(1, "in png_read_destroy");
757
758
#ifdef PNG_READ_GAMMA_SUPPORTED
759
png_destroy_gamma_table(png_ptr);
760
#endif
761
762
png_free(png_ptr, png_ptr->big_row_buf);
763
png_ptr->big_row_buf = NULL;
764
png_free(png_ptr, png_ptr->big_prev_row);
765
png_ptr->big_prev_row = NULL;
766
png_free(png_ptr, png_ptr->read_buffer);
767
png_ptr->read_buffer = NULL;
768
769
#ifdef PNG_READ_QUANTIZE_SUPPORTED
770
png_free(png_ptr, png_ptr->palette_lookup);
771
png_ptr->palette_lookup = NULL;
772
png_free(png_ptr, png_ptr->quantize_index);
773
png_ptr->quantize_index = NULL;
774
#endif
775
776
if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
777
{
778
png_zfree(png_ptr, png_ptr->palette);
779
png_ptr->palette = NULL;
780
}
781
png_ptr->free_me &= ~PNG_FREE_PLTE;
782
783
#if defined(PNG_tRNS_SUPPORTED) || \
784
defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
785
if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
786
{
787
png_free(png_ptr, png_ptr->trans_alpha);
788
png_ptr->trans_alpha = NULL;
789
}
790
png_ptr->free_me &= ~PNG_FREE_TRNS;
791
#endif
792
793
inflateEnd(&png_ptr->zstream);
794
795
#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
796
png_free(png_ptr, png_ptr->save_buffer);
797
png_ptr->save_buffer = NULL;
798
#endif
799
800
#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
801
defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
802
png_free(png_ptr, png_ptr->unknown_chunk.data);
803
png_ptr->unknown_chunk.data = NULL;
804
#endif
805
806
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
807
png_free(png_ptr, png_ptr->chunk_list);
808
png_ptr->chunk_list = NULL;
809
#endif
810
811
#if defined(PNG_READ_EXPAND_SUPPORTED) && \
812
defined(PNG_ARM_NEON_IMPLEMENTATION)
813
png_free(png_ptr, png_ptr->riffled_palette);
814
png_ptr->riffled_palette = NULL;
815
#endif
816
817
/* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
818
* callbacks are still set at this point. They are required to complete the
819
* destruction of the png_struct itself.
820
*/
821
}
822
823
/* Free all memory used by the read */
824
void PNGAPI
825
png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
826
png_infopp end_info_ptr_ptr)
827
{
828
png_structrp png_ptr = NULL;
829
830
png_debug(1, "in png_destroy_read_struct");
831
832
if (png_ptr_ptr != NULL)
833
png_ptr = *png_ptr_ptr;
834
835
if (png_ptr == NULL)
836
return;
837
838
/* libpng 1.6.0: use the API to destroy info structs to ensure consistent
839
* behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
840
* The extra was, apparently, unnecessary yet this hides memory leak bugs.
841
*/
842
png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
843
png_destroy_info_struct(png_ptr, info_ptr_ptr);
844
845
*png_ptr_ptr = NULL;
846
png_read_destroy(png_ptr);
847
png_destroy_png_struct(png_ptr);
848
}
849
850
void PNGAPI
851
png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
852
{
853
if (png_ptr == NULL)
854
return;
855
856
png_ptr->read_row_fn = read_row_fn;
857
}
858
859
860
#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
861
#ifdef PNG_INFO_IMAGE_SUPPORTED
862
void PNGAPI
863
png_read_png(png_structrp png_ptr, png_inforp info_ptr,
864
int transforms, voidp params)
865
{
866
png_debug(1, "in png_read_png");
867
868
if (png_ptr == NULL || info_ptr == NULL)
869
return;
870
871
/* png_read_info() gives us all of the information from the
872
* PNG file before the first IDAT (image data chunk).
873
*/
874
png_read_info(png_ptr, info_ptr);
875
if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
876
png_error(png_ptr, "Image is too high to process with png_read_png()");
877
878
/* -------------- image transformations start here ------------------- */
879
/* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
880
* is not implemented. This will only happen in de-configured (non-default)
881
* libpng builds. The results can be unexpected - png_read_png may return
882
* short or mal-formed rows because the transform is skipped.
883
*/
884
885
/* Tell libpng to strip 16-bit/color files down to 8 bits per color.
886
*/
887
if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
888
/* Added at libpng-1.5.4. "strip_16" produces the same result that it
889
* did in earlier versions, while "scale_16" is now more accurate.
890
*/
891
#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
892
png_set_scale_16(png_ptr);
893
#else
894
png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
895
#endif
896
897
/* If both SCALE and STRIP are required pngrtran will effectively cancel the
898
* latter by doing SCALE first. This is ok and allows apps not to check for
899
* which is supported to get the right answer.
900
*/
901
if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
902
#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
903
png_set_strip_16(png_ptr);
904
#else
905
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
906
#endif
907
908
/* Strip alpha bytes from the input data without combining with
909
* the background (not recommended).
910
*/
911
if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
912
#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
913
png_set_strip_alpha(png_ptr);
914
#else
915
png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
916
#endif
917
918
/* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
919
* byte into separate bytes (useful for paletted and grayscale images).
920
*/
921
if ((transforms & PNG_TRANSFORM_PACKING) != 0)
922
#ifdef PNG_READ_PACK_SUPPORTED
923
png_set_packing(png_ptr);
924
#else
925
png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
926
#endif
927
928
/* Change the order of packed pixels to least significant bit first
929
* (not useful if you are using png_set_packing).
930
*/
931
if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
932
#ifdef PNG_READ_PACKSWAP_SUPPORTED
933
png_set_packswap(png_ptr);
934
#else
935
png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
936
#endif
937
938
/* Expand paletted colors into true RGB triplets
939
* Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
940
* Expand paletted or RGB images with transparency to full alpha
941
* channels so the data will be available as RGBA quartets.
942
*/
943
if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
944
#ifdef PNG_READ_EXPAND_SUPPORTED
945
png_set_expand(png_ptr);
946
#else
947
png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
948
#endif
949
950
/* We don't handle background color or gamma transformation or quantizing.
951
*/
952
953
/* Invert monochrome files to have 0 as white and 1 as black
954
*/
955
if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
956
#ifdef PNG_READ_INVERT_SUPPORTED
957
png_set_invert_mono(png_ptr);
958
#else
959
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
960
#endif
961
962
/* If you want to shift the pixel values from the range [0,255] or
963
* [0,65535] to the original [0,7] or [0,31], or whatever range the
964
* colors were originally in:
965
*/
966
if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
967
#ifdef PNG_READ_SHIFT_SUPPORTED
968
if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
969
png_set_shift(png_ptr, &info_ptr->sig_bit);
970
#else
971
png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
972
#endif
973
974
/* Flip the RGB pixels to BGR (or RGBA to BGRA) */
975
if ((transforms & PNG_TRANSFORM_BGR) != 0)
976
#ifdef PNG_READ_BGR_SUPPORTED
977
png_set_bgr(png_ptr);
978
#else
979
png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
980
#endif
981
982
/* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
983
if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
984
#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
985
png_set_swap_alpha(png_ptr);
986
#else
987
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
988
#endif
989
990
/* Swap bytes of 16-bit files to least significant byte first */
991
if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
992
#ifdef PNG_READ_SWAP_SUPPORTED
993
png_set_swap(png_ptr);
994
#else
995
png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
996
#endif
997
998
/* Added at libpng-1.2.41 */
999
/* Invert the alpha channel from opacity to transparency */
1000
if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
1001
#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
1002
png_set_invert_alpha(png_ptr);
1003
#else
1004
png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
1005
#endif
1006
1007
/* Added at libpng-1.2.41 */
1008
/* Expand grayscale image to RGB */
1009
if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
1010
#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1011
png_set_gray_to_rgb(png_ptr);
1012
#else
1013
png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
1014
#endif
1015
1016
/* Added at libpng-1.5.4 */
1017
if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
1018
#ifdef PNG_READ_EXPAND_16_SUPPORTED
1019
png_set_expand_16(png_ptr);
1020
#else
1021
png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
1022
#endif
1023
1024
/* We don't handle adding filler bytes */
1025
1026
/* We use png_read_image and rely on that for interlace handling, but we also
1027
* call png_read_update_info therefore must turn on interlace handling now:
1028
*/
1029
(void)png_set_interlace_handling(png_ptr);
1030
1031
/* Optional call to gamma correct and add the background to the palette
1032
* and update info structure. REQUIRED if you are expecting libpng to
1033
* update the palette for you (i.e., you selected such a transform above).
1034
*/
1035
png_read_update_info(png_ptr, info_ptr);
1036
1037
/* -------------- image transformations end here ------------------- */
1038
1039
png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1040
if (info_ptr->row_pointers == NULL)
1041
{
1042
png_uint_32 iptr;
1043
1044
info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
1045
info_ptr->height * (sizeof (png_bytep))));
1046
1047
for (iptr=0; iptr<info_ptr->height; iptr++)
1048
info_ptr->row_pointers[iptr] = NULL;
1049
1050
info_ptr->free_me |= PNG_FREE_ROWS;
1051
1052
for (iptr = 0; iptr < info_ptr->height; iptr++)
1053
info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
1054
png_malloc(png_ptr, info_ptr->rowbytes));
1055
}
1056
1057
png_read_image(png_ptr, info_ptr->row_pointers);
1058
info_ptr->valid |= PNG_INFO_IDAT;
1059
1060
/* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
1061
png_read_end(png_ptr, info_ptr);
1062
1063
PNG_UNUSED(params)
1064
}
1065
#endif /* INFO_IMAGE */
1066
#endif /* SEQUENTIAL_READ */
1067
1068
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
1069
/* SIMPLIFIED READ
1070
*
1071
* This code currently relies on the sequential reader, though it could easily
1072
* be made to work with the progressive one.
1073
*/
1074
/* Arguments to png_image_finish_read: */
1075
1076
/* Encoding of PNG data (used by the color-map code) */
1077
# define P_NOTSET 0 /* File encoding not yet known */
1078
# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
1079
# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
1080
# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
1081
# define P_LINEAR8 4 /* 8-bit linear: only from a file value */
1082
1083
/* Color-map processing: after libpng has run on the PNG image further
1084
* processing may be needed to convert the data to color-map indices.
1085
*/
1086
#define PNG_CMAP_NONE 0
1087
#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
1088
#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
1089
#define PNG_CMAP_RGB 3 /* Process RGB data */
1090
#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
1091
1092
/* The following document where the background is for each processing case. */
1093
#define PNG_CMAP_NONE_BACKGROUND 256
1094
#define PNG_CMAP_GA_BACKGROUND 231
1095
#define PNG_CMAP_TRANS_BACKGROUND 254
1096
#define PNG_CMAP_RGB_BACKGROUND 256
1097
#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
1098
1099
typedef struct
1100
{
1101
/* Arguments: */
1102
png_imagep image;
1103
png_voidp buffer;
1104
png_int_32 row_stride;
1105
png_voidp colormap;
1106
png_const_colorp background;
1107
/* Local variables: */
1108
png_voidp local_row;
1109
png_voidp first_row;
1110
ptrdiff_t row_bytes; /* step between rows */
1111
int file_encoding; /* E_ values above */
1112
png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
1113
int colormap_processing; /* PNG_CMAP_ values above */
1114
} png_image_read_control;
1115
1116
/* Do all the *safe* initialization - 'safe' means that png_error won't be
1117
* called, so setting up the jmp_buf is not required. This means that anything
1118
* called from here must *not* call png_malloc - it has to call png_malloc_warn
1119
* instead so that control is returned safely back to this routine.
1120
*/
1121
static int
1122
png_image_read_init(png_imagep image)
1123
{
1124
if (image->opaque == NULL)
1125
{
1126
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
1127
png_safe_error, png_safe_warning);
1128
1129
/* And set the rest of the structure to NULL to ensure that the various
1130
* fields are consistent.
1131
*/
1132
memset(image, 0, (sizeof *image));
1133
image->version = PNG_IMAGE_VERSION;
1134
1135
if (png_ptr != NULL)
1136
{
1137
png_infop info_ptr = png_create_info_struct(png_ptr);
1138
1139
if (info_ptr != NULL)
1140
{
1141
png_controlp control = png_voidcast(png_controlp,
1142
png_malloc_warn(png_ptr, (sizeof *control)));
1143
1144
if (control != NULL)
1145
{
1146
memset(control, 0, (sizeof *control));
1147
1148
control->png_ptr = png_ptr;
1149
control->info_ptr = info_ptr;
1150
control->for_write = 0;
1151
1152
image->opaque = control;
1153
return 1;
1154
}
1155
1156
/* Error clean up */
1157
png_destroy_info_struct(png_ptr, &info_ptr);
1158
}
1159
1160
png_destroy_read_struct(&png_ptr, NULL, NULL);
1161
}
1162
1163
return png_image_error(image, "png_image_read: out of memory");
1164
}
1165
1166
return png_image_error(image, "png_image_read: opaque pointer not NULL");
1167
}
1168
1169
/* Utility to find the base format of a PNG file from a png_struct. */
1170
static png_uint_32
1171
png_image_format(png_structrp png_ptr)
1172
{
1173
png_uint_32 format = 0;
1174
1175
if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1176
format |= PNG_FORMAT_FLAG_COLOR;
1177
1178
if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
1179
format |= PNG_FORMAT_FLAG_ALPHA;
1180
1181
/* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
1182
* sets the png_struct fields; that's all we are interested in here. The
1183
* precise interaction with an app call to png_set_tRNS and PNG file reading
1184
* is unclear.
1185
*/
1186
else if (png_ptr->num_trans > 0)
1187
format |= PNG_FORMAT_FLAG_ALPHA;
1188
1189
if (png_ptr->bit_depth == 16)
1190
format |= PNG_FORMAT_FLAG_LINEAR;
1191
1192
if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
1193
format |= PNG_FORMAT_FLAG_COLORMAP;
1194
1195
return format;
1196
}
1197
1198
static int
1199
chromaticities_match_sRGB(const png_xy *xy)
1200
{
1201
# define sRGB_TOLERANCE 1000
1202
static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
1203
{
1204
/* color x y */
1205
/* red */ 64000, 33000,
1206
/* green */ 30000, 60000,
1207
/* blue */ 15000, 6000,
1208
/* white */ 31270, 32900
1209
};
1210
1211
if (PNG_OUT_OF_RANGE(xy->whitex, sRGB_xy.whitex,sRGB_TOLERANCE) ||
1212
PNG_OUT_OF_RANGE(xy->whitey, sRGB_xy.whitey,sRGB_TOLERANCE) ||
1213
PNG_OUT_OF_RANGE(xy->redx, sRGB_xy.redx, sRGB_TOLERANCE) ||
1214
PNG_OUT_OF_RANGE(xy->redy, sRGB_xy.redy, sRGB_TOLERANCE) ||
1215
PNG_OUT_OF_RANGE(xy->greenx, sRGB_xy.greenx,sRGB_TOLERANCE) ||
1216
PNG_OUT_OF_RANGE(xy->greeny, sRGB_xy.greeny,sRGB_TOLERANCE) ||
1217
PNG_OUT_OF_RANGE(xy->bluex, sRGB_xy.bluex, sRGB_TOLERANCE) ||
1218
PNG_OUT_OF_RANGE(xy->bluey, sRGB_xy.bluey, sRGB_TOLERANCE))
1219
return 0;
1220
return 1;
1221
}
1222
1223
/* Is the given gamma significantly different from sRGB? The test is the same
1224
* one used in pngrtran.c when deciding whether to do gamma correction. The
1225
* arithmetic optimizes the division by using the fact that the inverse of the
1226
* file sRGB gamma is 2.2
1227
*/
1228
static int
1229
png_gamma_not_sRGB(png_fixed_point g)
1230
{
1231
/* 1.6.47: use the same sanity checks as used in pngrtran.c */
1232
if (g < PNG_LIB_GAMMA_MIN || g > PNG_LIB_GAMMA_MAX)
1233
return 0; /* Includes the uninitialized value 0 */
1234
1235
return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
1236
}
1237
1238
/* Do the main body of a 'png_image_begin_read' function; read the PNG file
1239
* header and fill in all the information. This is executed in a safe context,
1240
* unlike the init routine above.
1241
*/
1242
static int
1243
png_image_is_not_sRGB(png_const_structrp png_ptr)
1244
{
1245
/* Does the colorspace **not** match sRGB? The flag is only set if the
1246
* answer can be determined reliably.
1247
*
1248
* png_struct::chromaticities always exists since the simplified API
1249
* requires rgb-to-gray. The mDCV, cICP and cHRM chunks may all set it to
1250
* a non-sRGB value, so it needs to be checked but **only** if one of
1251
* those chunks occured in the file.
1252
*/
1253
/* Highest priority: check to be safe. */
1254
if (png_has_chunk(png_ptr, cICP) || png_has_chunk(png_ptr, mDCV))
1255
return !chromaticities_match_sRGB(&png_ptr->chromaticities);
1256
1257
/* If the image is marked as sRGB then it is... */
1258
if (png_has_chunk(png_ptr, sRGB))
1259
return 0;
1260
1261
/* Last stop: cHRM, must check: */
1262
if (png_has_chunk(png_ptr, cHRM))
1263
return !chromaticities_match_sRGB(&png_ptr->chromaticities);
1264
1265
/* Else default to sRGB */
1266
return 0;
1267
}
1268
1269
static int
1270
png_image_read_header(png_voidp argument)
1271
{
1272
png_imagep image = png_voidcast(png_imagep, argument);
1273
png_structrp png_ptr = image->opaque->png_ptr;
1274
png_inforp info_ptr = image->opaque->info_ptr;
1275
1276
#ifdef PNG_BENIGN_ERRORS_SUPPORTED
1277
png_set_benign_errors(png_ptr, 1/*warn*/);
1278
#endif
1279
png_read_info(png_ptr, info_ptr);
1280
1281
/* Do this the fast way; just read directly out of png_struct. */
1282
image->width = png_ptr->width;
1283
image->height = png_ptr->height;
1284
1285
{
1286
png_uint_32 format = png_image_format(png_ptr);
1287
1288
image->format = format;
1289
1290
/* Greyscale images don't (typically) have colour space information and
1291
* using it is pretty much impossible, so use sRGB for grayscale (it
1292
* doesn't matter r==g==b so the transform is irrelevant.)
1293
*/
1294
if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
1295
png_image_is_not_sRGB(png_ptr))
1296
image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
1297
}
1298
1299
/* We need the maximum number of entries regardless of the format the
1300
* application sets here.
1301
*/
1302
{
1303
png_uint_32 cmap_entries;
1304
1305
switch (png_ptr->color_type)
1306
{
1307
case PNG_COLOR_TYPE_GRAY:
1308
cmap_entries = 1U << png_ptr->bit_depth;
1309
break;
1310
1311
case PNG_COLOR_TYPE_PALETTE:
1312
cmap_entries = (png_uint_32)png_ptr->num_palette;
1313
break;
1314
1315
default:
1316
cmap_entries = 256;
1317
break;
1318
}
1319
1320
if (cmap_entries > 256)
1321
cmap_entries = 256;
1322
1323
image->colormap_entries = cmap_entries;
1324
}
1325
1326
return 1;
1327
}
1328
1329
#ifdef PNG_STDIO_SUPPORTED
1330
int PNGAPI
1331
png_image_begin_read_from_stdio(png_imagep image, FILE *file)
1332
{
1333
if (image != NULL && image->version == PNG_IMAGE_VERSION)
1334
{
1335
if (file != NULL)
1336
{
1337
if (png_image_read_init(image) != 0)
1338
{
1339
/* This is slightly evil, but png_init_io doesn't do anything other
1340
* than this and we haven't changed the standard IO functions so
1341
* this saves a 'safe' function.
1342
*/
1343
image->opaque->png_ptr->io_ptr = file;
1344
return png_safe_execute(image, png_image_read_header, image);
1345
}
1346
}
1347
1348
else
1349
return png_image_error(image,
1350
"png_image_begin_read_from_stdio: invalid argument");
1351
}
1352
1353
else if (image != NULL)
1354
return png_image_error(image,
1355
"png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
1356
1357
return 0;
1358
}
1359
1360
int PNGAPI
1361
png_image_begin_read_from_file(png_imagep image, const char *file_name)
1362
{
1363
if (image != NULL && image->version == PNG_IMAGE_VERSION)
1364
{
1365
if (file_name != NULL)
1366
{
1367
FILE *fp = fopen(file_name, "rb");
1368
1369
if (fp != NULL)
1370
{
1371
if (png_image_read_init(image) != 0)
1372
{
1373
image->opaque->png_ptr->io_ptr = fp;
1374
image->opaque->owned_file = 1;
1375
return png_safe_execute(image, png_image_read_header, image);
1376
}
1377
1378
/* Clean up: just the opened file. */
1379
(void)fclose(fp);
1380
}
1381
1382
else
1383
return png_image_error(image, strerror(errno));
1384
}
1385
1386
else
1387
return png_image_error(image,
1388
"png_image_begin_read_from_file: invalid argument");
1389
}
1390
1391
else if (image != NULL)
1392
return png_image_error(image,
1393
"png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
1394
1395
return 0;
1396
}
1397
#endif /* STDIO */
1398
1399
static void PNGCBAPI
1400
png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need)
1401
{
1402
if (png_ptr != NULL)
1403
{
1404
png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
1405
if (image != NULL)
1406
{
1407
png_controlp cp = image->opaque;
1408
if (cp != NULL)
1409
{
1410
png_const_bytep memory = cp->memory;
1411
size_t size = cp->size;
1412
1413
if (memory != NULL && size >= need)
1414
{
1415
memcpy(out, memory, need);
1416
cp->memory = memory + need;
1417
cp->size = size - need;
1418
return;
1419
}
1420
1421
png_error(png_ptr, "read beyond end of data");
1422
}
1423
}
1424
1425
png_error(png_ptr, "invalid memory read");
1426
}
1427
}
1428
1429
int PNGAPI png_image_begin_read_from_memory(png_imagep image,
1430
png_const_voidp memory, size_t size)
1431
{
1432
if (image != NULL && image->version == PNG_IMAGE_VERSION)
1433
{
1434
if (memory != NULL && size > 0)
1435
{
1436
if (png_image_read_init(image) != 0)
1437
{
1438
/* Now set the IO functions to read from the memory buffer and
1439
* store it into io_ptr. Again do this in-place to avoid calling a
1440
* libpng function that requires error handling.
1441
*/
1442
image->opaque->memory = png_voidcast(png_const_bytep, memory);
1443
image->opaque->size = size;
1444
image->opaque->png_ptr->io_ptr = image;
1445
image->opaque->png_ptr->read_data_fn = png_image_memory_read;
1446
1447
return png_safe_execute(image, png_image_read_header, image);
1448
}
1449
}
1450
1451
else
1452
return png_image_error(image,
1453
"png_image_begin_read_from_memory: invalid argument");
1454
}
1455
1456
else if (image != NULL)
1457
return png_image_error(image,
1458
"png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
1459
1460
return 0;
1461
}
1462
1463
/* Utility function to skip chunks that are not used by the simplified image
1464
* read functions and an appropriate macro to call it.
1465
*/
1466
#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
1467
static void
1468
png_image_skip_unused_chunks(png_structrp png_ptr)
1469
{
1470
/* Prepare the reader to ignore all recognized chunks whose data will not
1471
* be used, i.e., all chunks recognized by libpng except for those
1472
* involved in basic image reading:
1473
*
1474
* IHDR, PLTE, IDAT, IEND
1475
*
1476
* Or image data handling:
1477
*
1478
* tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
1479
*
1480
* This provides a small performance improvement and eliminates any
1481
* potential vulnerability to security problems in the unused chunks.
1482
*
1483
* At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
1484
* too. This allows the simplified API to be compiled without iCCP support.
1485
*/
1486
{
1487
static const png_byte chunks_to_process[] = {
1488
98, 75, 71, 68, '\0', /* bKGD */
1489
99, 72, 82, 77, '\0', /* cHRM */
1490
99, 73, 67, 80, '\0', /* cICP */
1491
103, 65, 77, 65, '\0', /* gAMA */
1492
109, 68, 67, 86, '\0', /* mDCV */
1493
115, 66, 73, 84, '\0', /* sBIT */
1494
115, 82, 71, 66, '\0', /* sRGB */
1495
};
1496
1497
/* Ignore unknown chunks and all other chunks except for the
1498
* IHDR, PLTE, tRNS, IDAT, and IEND chunks.
1499
*/
1500
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
1501
NULL, -1);
1502
1503
/* But do not ignore image data handling chunks */
1504
png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
1505
chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
1506
}
1507
}
1508
1509
# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
1510
#else
1511
# define PNG_SKIP_CHUNKS(p) ((void)0)
1512
#endif /* HANDLE_AS_UNKNOWN */
1513
1514
/* The following macro gives the exact rounded answer for all values in the
1515
* range 0..255 (it actually divides by 51.2, but the rounding still generates
1516
* the correct numbers 0..5
1517
*/
1518
#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
1519
1520
/* Utility functions to make particular color-maps */
1521
static void
1522
set_file_encoding(png_image_read_control *display)
1523
{
1524
png_structrp png_ptr = display->image->opaque->png_ptr;
1525
png_fixed_point g = png_resolve_file_gamma(png_ptr);
1526
1527
/* PNGv3: the result may be 0 however the 'default_gamma' should have been
1528
* set before this is called so zero is an error:
1529
*/
1530
if (g == 0)
1531
png_error(png_ptr, "internal: default gamma not set");
1532
1533
if (png_gamma_significant(g) != 0)
1534
{
1535
if (png_gamma_not_sRGB(g) != 0)
1536
{
1537
display->file_encoding = P_FILE;
1538
display->gamma_to_linear = png_reciprocal(g);
1539
}
1540
1541
else
1542
display->file_encoding = P_sRGB;
1543
}
1544
1545
else
1546
display->file_encoding = P_LINEAR8;
1547
}
1548
1549
static unsigned int
1550
decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
1551
{
1552
if (encoding == P_FILE) /* double check */
1553
encoding = display->file_encoding;
1554
1555
if (encoding == P_NOTSET) /* must be the file encoding */
1556
{
1557
set_file_encoding(display);
1558
encoding = display->file_encoding;
1559
}
1560
1561
switch (encoding)
1562
{
1563
case P_FILE:
1564
value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
1565
break;
1566
1567
case P_sRGB:
1568
value = png_sRGB_table[value];
1569
break;
1570
1571
case P_LINEAR:
1572
break;
1573
1574
case P_LINEAR8:
1575
value *= 257;
1576
break;
1577
1578
#ifdef __GNUC__
1579
default:
1580
png_error(display->image->opaque->png_ptr,
1581
"unexpected encoding (internal error)");
1582
#endif
1583
}
1584
1585
return value;
1586
}
1587
1588
static png_uint_32
1589
png_colormap_compose(png_image_read_control *display,
1590
png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
1591
png_uint_32 background, int encoding)
1592
{
1593
/* The file value is composed on the background, the background has the given
1594
* encoding and so does the result, the file is encoded with P_FILE and the
1595
* file and alpha are 8-bit values. The (output) encoding will always be
1596
* P_LINEAR or P_sRGB.
1597
*/
1598
png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
1599
png_uint_32 b = decode_gamma(display, background, encoding);
1600
1601
/* The alpha is always an 8-bit value (it comes from the palette), the value
1602
* scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
1603
*/
1604
f = f * alpha + b * (255-alpha);
1605
1606
if (encoding == P_LINEAR)
1607
{
1608
/* Scale to 65535; divide by 255, approximately (in fact this is extremely
1609
* accurate, it divides by 255.00000005937181414556, with no overflow.)
1610
*/
1611
f *= 257; /* Now scaled by 65535 */
1612
f += f >> 16;
1613
f = (f+32768) >> 16;
1614
}
1615
1616
else /* P_sRGB */
1617
f = PNG_sRGB_FROM_LINEAR(f);
1618
1619
return f;
1620
}
1621
1622
/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
1623
* be 8-bit.
1624
*/
1625
static void
1626
png_create_colormap_entry(png_image_read_control *display,
1627
png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
1628
png_uint_32 alpha, int encoding)
1629
{
1630
png_imagep image = display->image;
1631
int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1632
P_LINEAR : P_sRGB;
1633
int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
1634
(red != green || green != blue);
1635
1636
if (ip > 255)
1637
png_error(image->opaque->png_ptr, "color-map index out of range");
1638
1639
/* Update the cache with whether the file gamma is significantly different
1640
* from sRGB.
1641
*/
1642
if (encoding == P_FILE)
1643
{
1644
if (display->file_encoding == P_NOTSET)
1645
set_file_encoding(display);
1646
1647
/* Note that the cached value may be P_FILE too, but if it is then the
1648
* gamma_to_linear member has been set.
1649
*/
1650
encoding = display->file_encoding;
1651
}
1652
1653
if (encoding == P_FILE)
1654
{
1655
png_fixed_point g = display->gamma_to_linear;
1656
1657
red = png_gamma_16bit_correct(red*257, g);
1658
green = png_gamma_16bit_correct(green*257, g);
1659
blue = png_gamma_16bit_correct(blue*257, g);
1660
1661
if (convert_to_Y != 0 || output_encoding == P_LINEAR)
1662
{
1663
alpha *= 257;
1664
encoding = P_LINEAR;
1665
}
1666
1667
else
1668
{
1669
red = PNG_sRGB_FROM_LINEAR(red * 255);
1670
green = PNG_sRGB_FROM_LINEAR(green * 255);
1671
blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1672
encoding = P_sRGB;
1673
}
1674
}
1675
1676
else if (encoding == P_LINEAR8)
1677
{
1678
/* This encoding occurs quite frequently in test cases because PngSuite
1679
* includes a gAMA 1.0 chunk with most images.
1680
*/
1681
red *= 257;
1682
green *= 257;
1683
blue *= 257;
1684
alpha *= 257;
1685
encoding = P_LINEAR;
1686
}
1687
1688
else if (encoding == P_sRGB &&
1689
(convert_to_Y != 0 || output_encoding == P_LINEAR))
1690
{
1691
/* The values are 8-bit sRGB values, but must be converted to 16-bit
1692
* linear.
1693
*/
1694
red = png_sRGB_table[red];
1695
green = png_sRGB_table[green];
1696
blue = png_sRGB_table[blue];
1697
alpha *= 257;
1698
encoding = P_LINEAR;
1699
}
1700
1701
/* This is set if the color isn't gray but the output is. */
1702
if (encoding == P_LINEAR)
1703
{
1704
if (convert_to_Y != 0)
1705
{
1706
/* NOTE: these values are copied from png_do_rgb_to_gray */
1707
png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
1708
(png_uint_32)2366 * blue;
1709
1710
if (output_encoding == P_LINEAR)
1711
y = (y + 16384) >> 15;
1712
1713
else
1714
{
1715
/* y is scaled by 32768, we need it scaled by 255: */
1716
y = (y + 128) >> 8;
1717
y *= 255;
1718
y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
1719
alpha = PNG_DIV257(alpha);
1720
encoding = P_sRGB;
1721
}
1722
1723
blue = red = green = y;
1724
}
1725
1726
else if (output_encoding == P_sRGB)
1727
{
1728
red = PNG_sRGB_FROM_LINEAR(red * 255);
1729
green = PNG_sRGB_FROM_LINEAR(green * 255);
1730
blue = PNG_sRGB_FROM_LINEAR(blue * 255);
1731
alpha = PNG_DIV257(alpha);
1732
encoding = P_sRGB;
1733
}
1734
}
1735
1736
if (encoding != output_encoding)
1737
png_error(image->opaque->png_ptr, "bad encoding (internal error)");
1738
1739
/* Store the value. */
1740
{
1741
# ifdef PNG_FORMAT_AFIRST_SUPPORTED
1742
int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
1743
(image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
1744
# else
1745
# define afirst 0
1746
# endif
1747
# ifdef PNG_FORMAT_BGR_SUPPORTED
1748
int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
1749
# else
1750
# define bgr 0
1751
# endif
1752
1753
if (output_encoding == P_LINEAR)
1754
{
1755
png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
1756
1757
entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1758
1759
/* The linear 16-bit values must be pre-multiplied by the alpha channel
1760
* value, if less than 65535 (this is, effectively, composite on black
1761
* if the alpha channel is removed.)
1762
*/
1763
switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1764
{
1765
case 4:
1766
entry[afirst ? 0 : 3] = (png_uint_16)alpha;
1767
/* FALLTHROUGH */
1768
1769
case 3:
1770
if (alpha < 65535)
1771
{
1772
if (alpha > 0)
1773
{
1774
blue = (blue * alpha + 32767U)/65535U;
1775
green = (green * alpha + 32767U)/65535U;
1776
red = (red * alpha + 32767U)/65535U;
1777
}
1778
1779
else
1780
red = green = blue = 0;
1781
}
1782
entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
1783
entry[afirst + 1] = (png_uint_16)green;
1784
entry[afirst + bgr] = (png_uint_16)red;
1785
break;
1786
1787
case 2:
1788
entry[1 ^ afirst] = (png_uint_16)alpha;
1789
/* FALLTHROUGH */
1790
1791
case 1:
1792
if (alpha < 65535)
1793
{
1794
if (alpha > 0)
1795
green = (green * alpha + 32767U)/65535U;
1796
1797
else
1798
green = 0;
1799
}
1800
entry[afirst] = (png_uint_16)green;
1801
break;
1802
1803
default:
1804
break;
1805
}
1806
}
1807
1808
else /* output encoding is P_sRGB */
1809
{
1810
png_bytep entry = png_voidcast(png_bytep, display->colormap);
1811
1812
entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
1813
1814
switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
1815
{
1816
case 4:
1817
entry[afirst ? 0 : 3] = (png_byte)alpha;
1818
/* FALLTHROUGH */
1819
case 3:
1820
entry[afirst + (2 ^ bgr)] = (png_byte)blue;
1821
entry[afirst + 1] = (png_byte)green;
1822
entry[afirst + bgr] = (png_byte)red;
1823
break;
1824
1825
case 2:
1826
entry[1 ^ afirst] = (png_byte)alpha;
1827
/* FALLTHROUGH */
1828
case 1:
1829
entry[afirst] = (png_byte)green;
1830
break;
1831
1832
default:
1833
break;
1834
}
1835
}
1836
1837
# ifdef afirst
1838
# undef afirst
1839
# endif
1840
# ifdef bgr
1841
# undef bgr
1842
# endif
1843
}
1844
}
1845
1846
static int
1847
make_gray_file_colormap(png_image_read_control *display)
1848
{
1849
unsigned int i;
1850
1851
for (i=0; i<256; ++i)
1852
png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
1853
1854
return (int)i;
1855
}
1856
1857
static int
1858
make_gray_colormap(png_image_read_control *display)
1859
{
1860
unsigned int i;
1861
1862
for (i=0; i<256; ++i)
1863
png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
1864
1865
return (int)i;
1866
}
1867
#define PNG_GRAY_COLORMAP_ENTRIES 256
1868
1869
static int
1870
make_ga_colormap(png_image_read_control *display)
1871
{
1872
unsigned int i, a;
1873
1874
/* Alpha is retained, the output will be a color-map with entries
1875
* selected by six levels of alpha. One transparent entry, 6 gray
1876
* levels for all the intermediate alpha values, leaving 230 entries
1877
* for the opaque grays. The color-map entries are the six values
1878
* [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
1879
* relevant entry.
1880
*
1881
* if (alpha > 229) // opaque
1882
* {
1883
* // The 231 entries are selected to make the math below work:
1884
* base = 0;
1885
* entry = (231 * gray + 128) >> 8;
1886
* }
1887
* else if (alpha < 26) // transparent
1888
* {
1889
* base = 231;
1890
* entry = 0;
1891
* }
1892
* else // partially opaque
1893
* {
1894
* base = 226 + 6 * PNG_DIV51(alpha);
1895
* entry = PNG_DIV51(gray);
1896
* }
1897
*/
1898
i = 0;
1899
while (i < 231)
1900
{
1901
unsigned int gray = (i * 256 + 115) / 231;
1902
png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
1903
}
1904
1905
/* 255 is used here for the component values for consistency with the code
1906
* that undoes premultiplication in pngwrite.c.
1907
*/
1908
png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
1909
1910
for (a=1; a<5; ++a)
1911
{
1912
unsigned int g;
1913
1914
for (g=0; g<6; ++g)
1915
png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
1916
P_sRGB);
1917
}
1918
1919
return (int)i;
1920
}
1921
1922
#define PNG_GA_COLORMAP_ENTRIES 256
1923
1924
static int
1925
make_rgb_colormap(png_image_read_control *display)
1926
{
1927
unsigned int i, r;
1928
1929
/* Build a 6x6x6 opaque RGB cube */
1930
for (i=r=0; r<6; ++r)
1931
{
1932
unsigned int g;
1933
1934
for (g=0; g<6; ++g)
1935
{
1936
unsigned int b;
1937
1938
for (b=0; b<6; ++b)
1939
png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
1940
P_sRGB);
1941
}
1942
}
1943
1944
return (int)i;
1945
}
1946
1947
#define PNG_RGB_COLORMAP_ENTRIES 216
1948
1949
/* Return a palette index to the above palette given three 8-bit sRGB values. */
1950
#define PNG_RGB_INDEX(r,g,b) \
1951
((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
1952
1953
static int
1954
png_image_read_colormap(png_voidp argument)
1955
{
1956
png_image_read_control *display =
1957
png_voidcast(png_image_read_control*, argument);
1958
png_imagep image = display->image;
1959
1960
png_structrp png_ptr = image->opaque->png_ptr;
1961
png_uint_32 output_format = image->format;
1962
int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
1963
P_LINEAR : P_sRGB;
1964
1965
unsigned int cmap_entries;
1966
unsigned int output_processing; /* Output processing option */
1967
unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
1968
1969
/* Background information; the background color and the index of this color
1970
* in the color-map if it exists (else 256).
1971
*/
1972
unsigned int background_index = 256;
1973
png_uint_32 back_r, back_g, back_b;
1974
1975
/* Flags to accumulate things that need to be done to the input. */
1976
int expand_tRNS = 0;
1977
1978
/* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
1979
* very difficult to do, the results look awful, and it is difficult to see
1980
* what possible use it is because the application can't control the
1981
* color-map.
1982
*/
1983
if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
1984
png_ptr->num_trans > 0) /* alpha in input */ &&
1985
((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
1986
{
1987
if (output_encoding == P_LINEAR) /* compose on black */
1988
back_b = back_g = back_r = 0;
1989
1990
else if (display->background == NULL /* no way to remove it */)
1991
png_error(png_ptr,
1992
"background color must be supplied to remove alpha/transparency");
1993
1994
/* Get a copy of the background color (this avoids repeating the checks
1995
* below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
1996
* output format.
1997
*/
1998
else
1999
{
2000
back_g = display->background->green;
2001
if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
2002
{
2003
back_r = display->background->red;
2004
back_b = display->background->blue;
2005
}
2006
else
2007
back_b = back_r = back_g;
2008
}
2009
}
2010
2011
else if (output_encoding == P_LINEAR)
2012
back_b = back_r = back_g = 65535;
2013
2014
else
2015
back_b = back_r = back_g = 255;
2016
2017
/* Default the input file gamma if required - this is necessary because
2018
* libpng assumes that if no gamma information is present the data is in the
2019
* output format, but the simplified API deduces the gamma from the input
2020
* format. The 'default' gamma value is also set by png_set_alpha_mode, but
2021
* this is happening before any such call, so:
2022
*
2023
* TODO: should be an internal API and all this code should be copied into a
2024
* single common gamma+colorspace file.
2025
*/
2026
if (png_ptr->bit_depth == 16 &&
2027
(image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
2028
png_ptr->default_gamma = PNG_GAMMA_LINEAR;
2029
2030
else
2031
png_ptr->default_gamma = PNG_GAMMA_sRGB_INVERSE;
2032
2033
/* Decide what to do based on the PNG color type of the input data. The
2034
* utility function png_create_colormap_entry deals with most aspects of the
2035
* output transformations; this code works out how to produce bytes of
2036
* color-map entries from the original format.
2037
*/
2038
switch (png_ptr->color_type)
2039
{
2040
case PNG_COLOR_TYPE_GRAY:
2041
if (png_ptr->bit_depth <= 8)
2042
{
2043
/* There at most 256 colors in the output, regardless of
2044
* transparency.
2045
*/
2046
unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
2047
2048
cmap_entries = 1U << png_ptr->bit_depth;
2049
if (cmap_entries > image->colormap_entries)
2050
png_error(png_ptr, "gray[8] color-map: too few entries");
2051
2052
step = 255 / (cmap_entries - 1);
2053
output_processing = PNG_CMAP_NONE;
2054
2055
/* If there is a tRNS chunk then this either selects a transparent
2056
* value or, if the output has no alpha, the background color.
2057
*/
2058
if (png_ptr->num_trans > 0)
2059
{
2060
trans = png_ptr->trans_color.gray;
2061
2062
if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
2063
back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2064
}
2065
2066
/* png_create_colormap_entry just takes an RGBA and writes the
2067
* corresponding color-map entry using the format from 'image',
2068
* including the required conversion to sRGB or linear as
2069
* appropriate. The input values are always either sRGB (if the
2070
* gamma correction flag is 0) or 0..255 scaled file encoded values
2071
* (if the function must gamma correct them).
2072
*/
2073
for (i=val=0; i<cmap_entries; ++i, val += step)
2074
{
2075
/* 'i' is a file value. While this will result in duplicated
2076
* entries for 8-bit non-sRGB encoded files it is necessary to
2077
* have non-gamma corrected values to do tRNS handling.
2078
*/
2079
if (i != trans)
2080
png_create_colormap_entry(display, i, val, val, val, 255,
2081
P_FILE/*8-bit with file gamma*/);
2082
2083
/* Else this entry is transparent. The colors don't matter if
2084
* there is an alpha channel (back_alpha == 0), but it does no
2085
* harm to pass them in; the values are not set above so this
2086
* passes in white.
2087
*
2088
* NOTE: this preserves the full precision of the application
2089
* supplied background color when it is used.
2090
*/
2091
else
2092
png_create_colormap_entry(display, i, back_r, back_g, back_b,
2093
back_alpha, output_encoding);
2094
}
2095
2096
/* We need libpng to preserve the original encoding. */
2097
data_encoding = P_FILE;
2098
2099
/* The rows from libpng, while technically gray values, are now also
2100
* color-map indices; however, they may need to be expanded to 1
2101
* byte per pixel. This is what png_set_packing does (i.e., it
2102
* unpacks the bit values into bytes.)
2103
*/
2104
if (png_ptr->bit_depth < 8)
2105
png_set_packing(png_ptr);
2106
}
2107
2108
else /* bit depth is 16 */
2109
{
2110
/* The 16-bit input values can be converted directly to 8-bit gamma
2111
* encoded values; however, if a tRNS chunk is present 257 color-map
2112
* entries are required. This means that the extra entry requires
2113
* special processing; add an alpha channel, sacrifice gray level
2114
* 254 and convert transparent (alpha==0) entries to that.
2115
*
2116
* Use libpng to chop the data to 8 bits. Convert it to sRGB at the
2117
* same time to minimize quality loss. If a tRNS chunk is present
2118
* this means libpng must handle it too; otherwise it is impossible
2119
* to do the exact match on the 16-bit value.
2120
*
2121
* If the output has no alpha channel *and* the background color is
2122
* gray then it is possible to let libpng handle the substitution by
2123
* ensuring that the corresponding gray level matches the background
2124
* color exactly.
2125
*/
2126
data_encoding = P_sRGB;
2127
2128
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2129
png_error(png_ptr, "gray[16] color-map: too few entries");
2130
2131
cmap_entries = (unsigned int)make_gray_colormap(display);
2132
2133
if (png_ptr->num_trans > 0)
2134
{
2135
unsigned int back_alpha;
2136
2137
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2138
back_alpha = 0;
2139
2140
else
2141
{
2142
if (back_r == back_g && back_g == back_b)
2143
{
2144
/* Background is gray; no special processing will be
2145
* required.
2146
*/
2147
png_color_16 c;
2148
png_uint_32 gray = back_g;
2149
2150
if (output_encoding == P_LINEAR)
2151
{
2152
gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2153
2154
/* And make sure the corresponding palette entry
2155
* matches.
2156
*/
2157
png_create_colormap_entry(display, gray, back_g, back_g,
2158
back_g, 65535, P_LINEAR);
2159
}
2160
2161
/* The background passed to libpng, however, must be the
2162
* sRGB value.
2163
*/
2164
c.index = 0; /*unused*/
2165
c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2166
2167
/* NOTE: does this work without expanding tRNS to alpha?
2168
* It should be the color->gray case below apparently
2169
* doesn't.
2170
*/
2171
png_set_background_fixed(png_ptr, &c,
2172
PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2173
0/*gamma: not used*/);
2174
2175
output_processing = PNG_CMAP_NONE;
2176
break;
2177
}
2178
#ifdef __COVERITY__
2179
/* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
2180
* here.
2181
*/
2182
back_alpha = 255;
2183
#else
2184
back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
2185
#endif
2186
}
2187
2188
/* output_processing means that the libpng-processed row will be
2189
* 8-bit GA and it has to be processing to single byte color-map
2190
* values. Entry 254 is replaced by either a completely
2191
* transparent entry or by the background color at full
2192
* precision (and the background color is not a simple gray
2193
* level in this case.)
2194
*/
2195
expand_tRNS = 1;
2196
output_processing = PNG_CMAP_TRANS;
2197
background_index = 254;
2198
2199
/* And set (overwrite) color-map entry 254 to the actual
2200
* background color at full precision.
2201
*/
2202
png_create_colormap_entry(display, 254, back_r, back_g, back_b,
2203
back_alpha, output_encoding);
2204
}
2205
2206
else
2207
output_processing = PNG_CMAP_NONE;
2208
}
2209
break;
2210
2211
case PNG_COLOR_TYPE_GRAY_ALPHA:
2212
/* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
2213
* of 65536 combinations. If, however, the alpha channel is to be
2214
* removed there are only 256 possibilities if the background is gray.
2215
* (Otherwise there is a subset of the 65536 possibilities defined by
2216
* the triangle between black, white and the background color.)
2217
*
2218
* Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
2219
* worry about tRNS matching - tRNS is ignored if there is an alpha
2220
* channel.
2221
*/
2222
data_encoding = P_sRGB;
2223
2224
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2225
{
2226
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2227
png_error(png_ptr, "gray+alpha color-map: too few entries");
2228
2229
cmap_entries = (unsigned int)make_ga_colormap(display);
2230
2231
background_index = PNG_CMAP_GA_BACKGROUND;
2232
output_processing = PNG_CMAP_GA;
2233
}
2234
2235
else /* alpha is removed */
2236
{
2237
/* Alpha must be removed as the PNG data is processed when the
2238
* background is a color because the G and A channels are
2239
* independent and the vector addition (non-parallel vectors) is a
2240
* 2-D problem.
2241
*
2242
* This can be reduced to the same algorithm as above by making a
2243
* colormap containing gray levels (for the opaque grays), a
2244
* background entry (for a transparent pixel) and a set of four six
2245
* level color values, one set for each intermediate alpha value.
2246
* See the comments in make_ga_colormap for how this works in the
2247
* per-pixel processing.
2248
*
2249
* If the background is gray, however, we only need a 256 entry gray
2250
* level color map. It is sufficient to make the entry generated
2251
* for the background color be exactly the color specified.
2252
*/
2253
if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
2254
(back_r == back_g && back_g == back_b))
2255
{
2256
/* Background is gray; no special processing will be required. */
2257
png_color_16 c;
2258
png_uint_32 gray = back_g;
2259
2260
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2261
png_error(png_ptr, "gray-alpha color-map: too few entries");
2262
2263
cmap_entries = (unsigned int)make_gray_colormap(display);
2264
2265
if (output_encoding == P_LINEAR)
2266
{
2267
gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2268
2269
/* And make sure the corresponding palette entry matches. */
2270
png_create_colormap_entry(display, gray, back_g, back_g,
2271
back_g, 65535, P_LINEAR);
2272
}
2273
2274
/* The background passed to libpng, however, must be the sRGB
2275
* value.
2276
*/
2277
c.index = 0; /*unused*/
2278
c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2279
2280
png_set_background_fixed(png_ptr, &c,
2281
PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2282
0/*gamma: not used*/);
2283
2284
output_processing = PNG_CMAP_NONE;
2285
}
2286
2287
else
2288
{
2289
png_uint_32 i, a;
2290
2291
/* This is the same as png_make_ga_colormap, above, except that
2292
* the entries are all opaque.
2293
*/
2294
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2295
png_error(png_ptr, "ga-alpha color-map: too few entries");
2296
2297
i = 0;
2298
while (i < 231)
2299
{
2300
png_uint_32 gray = (i * 256 + 115) / 231;
2301
png_create_colormap_entry(display, i++, gray, gray, gray,
2302
255, P_sRGB);
2303
}
2304
2305
/* NOTE: this preserves the full precision of the application
2306
* background color.
2307
*/
2308
background_index = i;
2309
png_create_colormap_entry(display, i++, back_r, back_g, back_b,
2310
#ifdef __COVERITY__
2311
/* Coverity claims that output_encoding
2312
* cannot be 2 (P_LINEAR) here.
2313
*/ 255U,
2314
#else
2315
output_encoding == P_LINEAR ? 65535U : 255U,
2316
#endif
2317
output_encoding);
2318
2319
/* For non-opaque input composite on the sRGB background - this
2320
* requires inverting the encoding for each component. The input
2321
* is still converted to the sRGB encoding because this is a
2322
* reasonable approximate to the logarithmic curve of human
2323
* visual sensitivity, at least over the narrow range which PNG
2324
* represents. Consequently 'G' is always sRGB encoded, while
2325
* 'A' is linear. We need the linear background colors.
2326
*/
2327
if (output_encoding == P_sRGB) /* else already linear */
2328
{
2329
/* This may produce a value not exactly matching the
2330
* background, but that's ok because these numbers are only
2331
* used when alpha != 0
2332
*/
2333
back_r = png_sRGB_table[back_r];
2334
back_g = png_sRGB_table[back_g];
2335
back_b = png_sRGB_table[back_b];
2336
}
2337
2338
for (a=1; a<5; ++a)
2339
{
2340
unsigned int g;
2341
2342
/* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
2343
* by an 8-bit alpha value (0..255).
2344
*/
2345
png_uint_32 alpha = 51 * a;
2346
png_uint_32 back_rx = (255-alpha) * back_r;
2347
png_uint_32 back_gx = (255-alpha) * back_g;
2348
png_uint_32 back_bx = (255-alpha) * back_b;
2349
2350
for (g=0; g<6; ++g)
2351
{
2352
png_uint_32 gray = png_sRGB_table[g*51] * alpha;
2353
2354
png_create_colormap_entry(display, i++,
2355
PNG_sRGB_FROM_LINEAR(gray + back_rx),
2356
PNG_sRGB_FROM_LINEAR(gray + back_gx),
2357
PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
2358
}
2359
}
2360
2361
cmap_entries = i;
2362
output_processing = PNG_CMAP_GA;
2363
}
2364
}
2365
break;
2366
2367
case PNG_COLOR_TYPE_RGB:
2368
case PNG_COLOR_TYPE_RGB_ALPHA:
2369
/* Exclude the case where the output is gray; we can always handle this
2370
* with the cases above.
2371
*/
2372
if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
2373
{
2374
/* The color-map will be grayscale, so we may as well convert the
2375
* input RGB values to a simple grayscale and use the grayscale
2376
* code above.
2377
*
2378
* NOTE: calling this apparently damages the recognition of the
2379
* transparent color in background color handling; call
2380
* png_set_tRNS_to_alpha before png_set_background_fixed.
2381
*/
2382
png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
2383
-1);
2384
data_encoding = P_sRGB;
2385
2386
/* The output will now be one or two 8-bit gray or gray+alpha
2387
* channels. The more complex case arises when the input has alpha.
2388
*/
2389
if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2390
png_ptr->num_trans > 0) &&
2391
(output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2392
{
2393
/* Both input and output have an alpha channel, so no background
2394
* processing is required; just map the GA bytes to the right
2395
* color-map entry.
2396
*/
2397
expand_tRNS = 1;
2398
2399
if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
2400
png_error(png_ptr, "rgb[ga] color-map: too few entries");
2401
2402
cmap_entries = (unsigned int)make_ga_colormap(display);
2403
background_index = PNG_CMAP_GA_BACKGROUND;
2404
output_processing = PNG_CMAP_GA;
2405
}
2406
2407
else
2408
{
2409
const png_fixed_point gamma = png_resolve_file_gamma(png_ptr);
2410
2411
/* Either the input or the output has no alpha channel, so there
2412
* will be no non-opaque pixels in the color-map; it will just be
2413
* grayscale.
2414
*/
2415
if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
2416
png_error(png_ptr, "rgb[gray] color-map: too few entries");
2417
2418
/* Ideally this code would use libpng to do the gamma correction,
2419
* but if an input alpha channel is to be removed we will hit the
2420
* libpng bug in gamma+compose+rgb-to-gray (the double gamma
2421
* correction bug). Fix this by dropping the gamma correction in
2422
* this case and doing it in the palette; this will result in
2423
* duplicate palette entries, but that's better than the
2424
* alternative of double gamma correction.
2425
*
2426
* NOTE: PNGv3: check the resolved result of all the potentially
2427
* different colour space chunks.
2428
*/
2429
if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2430
png_ptr->num_trans > 0) &&
2431
png_gamma_not_sRGB(gamma) != 0)
2432
{
2433
cmap_entries = (unsigned int)make_gray_file_colormap(display);
2434
data_encoding = P_FILE;
2435
}
2436
2437
else
2438
cmap_entries = (unsigned int)make_gray_colormap(display);
2439
2440
/* But if the input has alpha or transparency it must be removed
2441
*/
2442
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2443
png_ptr->num_trans > 0)
2444
{
2445
png_color_16 c;
2446
png_uint_32 gray = back_g;
2447
2448
/* We need to ensure that the application background exists in
2449
* the colormap and that completely transparent pixels map to
2450
* it. Achieve this simply by ensuring that the entry
2451
* selected for the background really is the background color.
2452
*/
2453
if (data_encoding == P_FILE) /* from the fixup above */
2454
{
2455
/* The app supplied a gray which is in output_encoding, we
2456
* need to convert it to a value of the input (P_FILE)
2457
* encoding then set this palette entry to the required
2458
* output encoding.
2459
*/
2460
if (output_encoding == P_sRGB)
2461
gray = png_sRGB_table[gray]; /* now P_LINEAR */
2462
2463
gray = PNG_DIV257(png_gamma_16bit_correct(gray, gamma));
2464
/* now P_FILE */
2465
2466
/* And make sure the corresponding palette entry contains
2467
* exactly the required sRGB value.
2468
*/
2469
png_create_colormap_entry(display, gray, back_g, back_g,
2470
back_g, 0/*unused*/, output_encoding);
2471
}
2472
2473
else if (output_encoding == P_LINEAR)
2474
{
2475
gray = PNG_sRGB_FROM_LINEAR(gray * 255);
2476
2477
/* And make sure the corresponding palette entry matches.
2478
*/
2479
png_create_colormap_entry(display, gray, back_g, back_g,
2480
back_g, 0/*unused*/, P_LINEAR);
2481
}
2482
2483
/* The background passed to libpng, however, must be the
2484
* output (normally sRGB) value.
2485
*/
2486
c.index = 0; /*unused*/
2487
c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
2488
2489
/* NOTE: the following is apparently a bug in libpng. Without
2490
* it the transparent color recognition in
2491
* png_set_background_fixed seems to go wrong.
2492
*/
2493
expand_tRNS = 1;
2494
png_set_background_fixed(png_ptr, &c,
2495
PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2496
0/*gamma: not used*/);
2497
}
2498
2499
output_processing = PNG_CMAP_NONE;
2500
}
2501
}
2502
2503
else /* output is color */
2504
{
2505
/* We could use png_quantize here so long as there is no transparent
2506
* color or alpha; png_quantize ignores alpha. Easier overall just
2507
* to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
2508
* Consequently we always want libpng to produce sRGB data.
2509
*/
2510
data_encoding = P_sRGB;
2511
2512
/* Is there any transparency or alpha? */
2513
if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
2514
png_ptr->num_trans > 0)
2515
{
2516
/* Is there alpha in the output too? If so all four channels are
2517
* processed into a special RGB cube with alpha support.
2518
*/
2519
if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
2520
{
2521
png_uint_32 r;
2522
2523
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2524
png_error(png_ptr, "rgb+alpha color-map: too few entries");
2525
2526
cmap_entries = (unsigned int)make_rgb_colormap(display);
2527
2528
/* Add a transparent entry. */
2529
png_create_colormap_entry(display, cmap_entries, 255, 255,
2530
255, 0, P_sRGB);
2531
2532
/* This is stored as the background index for the processing
2533
* algorithm.
2534
*/
2535
background_index = cmap_entries++;
2536
2537
/* Add 27 r,g,b entries each with alpha 0.5. */
2538
for (r=0; r<256; r = (r << 1) | 0x7f)
2539
{
2540
png_uint_32 g;
2541
2542
for (g=0; g<256; g = (g << 1) | 0x7f)
2543
{
2544
png_uint_32 b;
2545
2546
/* This generates components with the values 0, 127 and
2547
* 255
2548
*/
2549
for (b=0; b<256; b = (b << 1) | 0x7f)
2550
png_create_colormap_entry(display, cmap_entries++,
2551
r, g, b, 128, P_sRGB);
2552
}
2553
}
2554
2555
expand_tRNS = 1;
2556
output_processing = PNG_CMAP_RGB_ALPHA;
2557
}
2558
2559
else
2560
{
2561
/* Alpha/transparency must be removed. The background must
2562
* exist in the color map (achieved by setting adding it after
2563
* the 666 color-map). If the standard processing code will
2564
* pick up this entry automatically that's all that is
2565
* required; libpng can be called to do the background
2566
* processing.
2567
*/
2568
unsigned int sample_size =
2569
PNG_IMAGE_SAMPLE_SIZE(output_format);
2570
png_uint_32 r, g, b; /* sRGB background */
2571
2572
if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
2573
png_error(png_ptr, "rgb-alpha color-map: too few entries");
2574
2575
cmap_entries = (unsigned int)make_rgb_colormap(display);
2576
2577
png_create_colormap_entry(display, cmap_entries, back_r,
2578
back_g, back_b, 0/*unused*/, output_encoding);
2579
2580
if (output_encoding == P_LINEAR)
2581
{
2582
r = PNG_sRGB_FROM_LINEAR(back_r * 255);
2583
g = PNG_sRGB_FROM_LINEAR(back_g * 255);
2584
b = PNG_sRGB_FROM_LINEAR(back_b * 255);
2585
}
2586
2587
else
2588
{
2589
r = back_r;
2590
g = back_g;
2591
b = back_g;
2592
}
2593
2594
/* Compare the newly-created color-map entry with the one the
2595
* PNG_CMAP_RGB algorithm will use. If the two entries don't
2596
* match, add the new one and set this as the background
2597
* index.
2598
*/
2599
if (memcmp((png_const_bytep)display->colormap +
2600
sample_size * cmap_entries,
2601
(png_const_bytep)display->colormap +
2602
sample_size * PNG_RGB_INDEX(r,g,b),
2603
sample_size) != 0)
2604
{
2605
/* The background color must be added. */
2606
background_index = cmap_entries++;
2607
2608
/* Add 27 r,g,b entries each with created by composing with
2609
* the background at alpha 0.5.
2610
*/
2611
for (r=0; r<256; r = (r << 1) | 0x7f)
2612
{
2613
for (g=0; g<256; g = (g << 1) | 0x7f)
2614
{
2615
/* This generates components with the values 0, 127
2616
* and 255
2617
*/
2618
for (b=0; b<256; b = (b << 1) | 0x7f)
2619
png_create_colormap_entry(display, cmap_entries++,
2620
png_colormap_compose(display, r, P_sRGB, 128,
2621
back_r, output_encoding),
2622
png_colormap_compose(display, g, P_sRGB, 128,
2623
back_g, output_encoding),
2624
png_colormap_compose(display, b, P_sRGB, 128,
2625
back_b, output_encoding),
2626
0/*unused*/, output_encoding);
2627
}
2628
}
2629
2630
expand_tRNS = 1;
2631
output_processing = PNG_CMAP_RGB_ALPHA;
2632
}
2633
2634
else /* background color is in the standard color-map */
2635
{
2636
png_color_16 c;
2637
2638
c.index = 0; /*unused*/
2639
c.red = (png_uint_16)back_r;
2640
c.gray = c.green = (png_uint_16)back_g;
2641
c.blue = (png_uint_16)back_b;
2642
2643
png_set_background_fixed(png_ptr, &c,
2644
PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
2645
0/*gamma: not used*/);
2646
2647
output_processing = PNG_CMAP_RGB;
2648
}
2649
}
2650
}
2651
2652
else /* no alpha or transparency in the input */
2653
{
2654
/* Alpha in the output is irrelevant, simply map the opaque input
2655
* pixels to the 6x6x6 color-map.
2656
*/
2657
if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
2658
png_error(png_ptr, "rgb color-map: too few entries");
2659
2660
cmap_entries = (unsigned int)make_rgb_colormap(display);
2661
output_processing = PNG_CMAP_RGB;
2662
}
2663
}
2664
break;
2665
2666
case PNG_COLOR_TYPE_PALETTE:
2667
/* It's already got a color-map. It may be necessary to eliminate the
2668
* tRNS entries though.
2669
*/
2670
{
2671
unsigned int num_trans = png_ptr->num_trans;
2672
png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
2673
png_const_colorp colormap = png_ptr->palette;
2674
int do_background = trans != NULL &&
2675
(output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
2676
unsigned int i;
2677
2678
/* Just in case: */
2679
if (trans == NULL)
2680
num_trans = 0;
2681
2682
output_processing = PNG_CMAP_NONE;
2683
data_encoding = P_FILE; /* Don't change from color-map indices */
2684
cmap_entries = (unsigned int)png_ptr->num_palette;
2685
if (cmap_entries > 256)
2686
cmap_entries = 256;
2687
2688
if (cmap_entries > (unsigned int)image->colormap_entries)
2689
png_error(png_ptr, "palette color-map: too few entries");
2690
2691
for (i=0; i < cmap_entries; ++i)
2692
{
2693
if (do_background != 0 && i < num_trans && trans[i] < 255)
2694
{
2695
if (trans[i] == 0)
2696
png_create_colormap_entry(display, i, back_r, back_g,
2697
back_b, 0, output_encoding);
2698
2699
else
2700
{
2701
/* Must compose the PNG file color in the color-map entry
2702
* on the sRGB color in 'back'.
2703
*/
2704
png_create_colormap_entry(display, i,
2705
png_colormap_compose(display, colormap[i].red,
2706
P_FILE, trans[i], back_r, output_encoding),
2707
png_colormap_compose(display, colormap[i].green,
2708
P_FILE, trans[i], back_g, output_encoding),
2709
png_colormap_compose(display, colormap[i].blue,
2710
P_FILE, trans[i], back_b, output_encoding),
2711
output_encoding == P_LINEAR ? trans[i] * 257U :
2712
trans[i],
2713
output_encoding);
2714
}
2715
}
2716
2717
else
2718
png_create_colormap_entry(display, i, colormap[i].red,
2719
colormap[i].green, colormap[i].blue,
2720
i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
2721
}
2722
2723
/* The PNG data may have indices packed in fewer than 8 bits, it
2724
* must be expanded if so.
2725
*/
2726
if (png_ptr->bit_depth < 8)
2727
png_set_packing(png_ptr);
2728
}
2729
break;
2730
2731
default:
2732
png_error(png_ptr, "invalid PNG color type");
2733
/*NOT REACHED*/
2734
}
2735
2736
/* Now deal with the output processing */
2737
if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
2738
(png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
2739
png_set_tRNS_to_alpha(png_ptr);
2740
2741
switch (data_encoding)
2742
{
2743
case P_sRGB:
2744
/* Change to 8-bit sRGB */
2745
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
2746
/* FALLTHROUGH */
2747
2748
case P_FILE:
2749
if (png_ptr->bit_depth > 8)
2750
png_set_scale_16(png_ptr);
2751
break;
2752
2753
#ifdef __GNUC__
2754
default:
2755
png_error(png_ptr, "bad data option (internal error)");
2756
#endif
2757
}
2758
2759
if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
2760
png_error(png_ptr, "color map overflow (BAD internal error)");
2761
2762
image->colormap_entries = cmap_entries;
2763
2764
/* Double check using the recorded background index */
2765
switch (output_processing)
2766
{
2767
case PNG_CMAP_NONE:
2768
if (background_index != PNG_CMAP_NONE_BACKGROUND)
2769
goto bad_background;
2770
break;
2771
2772
case PNG_CMAP_GA:
2773
if (background_index != PNG_CMAP_GA_BACKGROUND)
2774
goto bad_background;
2775
break;
2776
2777
case PNG_CMAP_TRANS:
2778
if (background_index >= cmap_entries ||
2779
background_index != PNG_CMAP_TRANS_BACKGROUND)
2780
goto bad_background;
2781
break;
2782
2783
case PNG_CMAP_RGB:
2784
if (background_index != PNG_CMAP_RGB_BACKGROUND)
2785
goto bad_background;
2786
break;
2787
2788
case PNG_CMAP_RGB_ALPHA:
2789
if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
2790
goto bad_background;
2791
break;
2792
2793
default:
2794
png_error(png_ptr, "bad processing option (internal error)");
2795
2796
bad_background:
2797
png_error(png_ptr, "bad background index (internal error)");
2798
}
2799
2800
display->colormap_processing = (int)output_processing;
2801
2802
return 1/*ok*/;
2803
}
2804
2805
/* The final part of the color-map read called from png_image_finish_read. */
2806
static int
2807
png_image_read_and_map(png_voidp argument)
2808
{
2809
png_image_read_control *display = png_voidcast(png_image_read_control*,
2810
argument);
2811
png_imagep image = display->image;
2812
png_structrp png_ptr = image->opaque->png_ptr;
2813
int passes;
2814
2815
/* Called when the libpng data must be transformed into the color-mapped
2816
* form. There is a local row buffer in display->local and this routine must
2817
* do the interlace handling.
2818
*/
2819
switch (png_ptr->interlaced)
2820
{
2821
case PNG_INTERLACE_NONE:
2822
passes = 1;
2823
break;
2824
2825
case PNG_INTERLACE_ADAM7:
2826
passes = PNG_INTERLACE_ADAM7_PASSES;
2827
break;
2828
2829
default:
2830
png_error(png_ptr, "unknown interlace type");
2831
}
2832
2833
{
2834
png_uint_32 height = image->height;
2835
png_uint_32 width = image->width;
2836
int proc = display->colormap_processing;
2837
png_bytep first_row = png_voidcast(png_bytep, display->first_row);
2838
ptrdiff_t step_row = display->row_bytes;
2839
int pass;
2840
2841
for (pass = 0; pass < passes; ++pass)
2842
{
2843
unsigned int startx, stepx, stepy;
2844
png_uint_32 y;
2845
2846
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
2847
{
2848
/* The row may be empty for a short image: */
2849
if (PNG_PASS_COLS(width, pass) == 0)
2850
continue;
2851
2852
startx = PNG_PASS_START_COL(pass);
2853
stepx = PNG_PASS_COL_OFFSET(pass);
2854
y = PNG_PASS_START_ROW(pass);
2855
stepy = PNG_PASS_ROW_OFFSET(pass);
2856
}
2857
2858
else
2859
{
2860
y = 0;
2861
startx = 0;
2862
stepx = stepy = 1;
2863
}
2864
2865
for (; y<height; y += stepy)
2866
{
2867
png_bytep inrow = png_voidcast(png_bytep, display->local_row);
2868
png_bytep outrow = first_row + y * step_row;
2869
png_const_bytep end_row = outrow + width;
2870
2871
/* Read read the libpng data into the temporary buffer. */
2872
png_read_row(png_ptr, inrow, NULL);
2873
2874
/* Now process the row according to the processing option, note
2875
* that the caller verifies that the format of the libpng output
2876
* data is as required.
2877
*/
2878
outrow += startx;
2879
switch (proc)
2880
{
2881
case PNG_CMAP_GA:
2882
for (; outrow < end_row; outrow += stepx)
2883
{
2884
/* The data is always in the PNG order */
2885
unsigned int gray = *inrow++;
2886
unsigned int alpha = *inrow++;
2887
unsigned int entry;
2888
2889
/* NOTE: this code is copied as a comment in
2890
* make_ga_colormap above. Please update the
2891
* comment if you change this code!
2892
*/
2893
if (alpha > 229) /* opaque */
2894
{
2895
entry = (231 * gray + 128) >> 8;
2896
}
2897
else if (alpha < 26) /* transparent */
2898
{
2899
entry = 231;
2900
}
2901
else /* partially opaque */
2902
{
2903
entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
2904
}
2905
2906
*outrow = (png_byte)entry;
2907
}
2908
break;
2909
2910
case PNG_CMAP_TRANS:
2911
for (; outrow < end_row; outrow += stepx)
2912
{
2913
png_byte gray = *inrow++;
2914
png_byte alpha = *inrow++;
2915
2916
if (alpha == 0)
2917
*outrow = PNG_CMAP_TRANS_BACKGROUND;
2918
2919
else if (gray != PNG_CMAP_TRANS_BACKGROUND)
2920
*outrow = gray;
2921
2922
else
2923
*outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
2924
}
2925
break;
2926
2927
case PNG_CMAP_RGB:
2928
for (; outrow < end_row; outrow += stepx)
2929
{
2930
*outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
2931
inrow += 3;
2932
}
2933
break;
2934
2935
case PNG_CMAP_RGB_ALPHA:
2936
for (; outrow < end_row; outrow += stepx)
2937
{
2938
unsigned int alpha = inrow[3];
2939
2940
/* Because the alpha entries only hold alpha==0.5 values
2941
* split the processing at alpha==0.25 (64) and 0.75
2942
* (196).
2943
*/
2944
2945
if (alpha >= 196)
2946
*outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
2947
inrow[2]);
2948
2949
else if (alpha < 64)
2950
*outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
2951
2952
else
2953
{
2954
/* Likewise there are three entries for each of r, g
2955
* and b. We could select the entry by popcount on
2956
* the top two bits on those architectures that
2957
* support it, this is what the code below does,
2958
* crudely.
2959
*/
2960
unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
2961
2962
/* Here are how the values map:
2963
*
2964
* 0x00 .. 0x3f -> 0
2965
* 0x40 .. 0xbf -> 1
2966
* 0xc0 .. 0xff -> 2
2967
*
2968
* So, as above with the explicit alpha checks, the
2969
* breakpoints are at 64 and 196.
2970
*/
2971
if (inrow[0] & 0x80) back_i += 9; /* red */
2972
if (inrow[0] & 0x40) back_i += 9;
2973
if (inrow[0] & 0x80) back_i += 3; /* green */
2974
if (inrow[0] & 0x40) back_i += 3;
2975
if (inrow[0] & 0x80) back_i += 1; /* blue */
2976
if (inrow[0] & 0x40) back_i += 1;
2977
2978
*outrow = (png_byte)back_i;
2979
}
2980
2981
inrow += 4;
2982
}
2983
break;
2984
2985
default:
2986
break;
2987
}
2988
}
2989
}
2990
}
2991
2992
return 1;
2993
}
2994
2995
static int
2996
png_image_read_colormapped(png_voidp argument)
2997
{
2998
png_image_read_control *display = png_voidcast(png_image_read_control*,
2999
argument);
3000
png_imagep image = display->image;
3001
png_controlp control = image->opaque;
3002
png_structrp png_ptr = control->png_ptr;
3003
png_inforp info_ptr = control->info_ptr;
3004
3005
int passes = 0; /* As a flag */
3006
3007
PNG_SKIP_CHUNKS(png_ptr);
3008
3009
/* Update the 'info' structure and make sure the result is as required; first
3010
* make sure to turn on the interlace handling if it will be required
3011
* (because it can't be turned on *after* the call to png_read_update_info!)
3012
*/
3013
if (display->colormap_processing == PNG_CMAP_NONE)
3014
passes = png_set_interlace_handling(png_ptr);
3015
3016
png_read_update_info(png_ptr, info_ptr);
3017
3018
/* The expected output can be deduced from the colormap_processing option. */
3019
switch (display->colormap_processing)
3020
{
3021
case PNG_CMAP_NONE:
3022
/* Output must be one channel and one byte per pixel, the output
3023
* encoding can be anything.
3024
*/
3025
if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
3026
info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
3027
info_ptr->bit_depth == 8)
3028
break;
3029
3030
goto bad_output;
3031
3032
case PNG_CMAP_TRANS:
3033
case PNG_CMAP_GA:
3034
/* Output must be two channels and the 'G' one must be sRGB, the latter
3035
* can be checked with an exact number because it should have been set
3036
* to this number above!
3037
*/
3038
if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
3039
info_ptr->bit_depth == 8 &&
3040
png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3041
image->colormap_entries == 256)
3042
break;
3043
3044
goto bad_output;
3045
3046
case PNG_CMAP_RGB:
3047
/* Output must be 8-bit sRGB encoded RGB */
3048
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
3049
info_ptr->bit_depth == 8 &&
3050
png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3051
image->colormap_entries == 216)
3052
break;
3053
3054
goto bad_output;
3055
3056
case PNG_CMAP_RGB_ALPHA:
3057
/* Output must be 8-bit sRGB encoded RGBA */
3058
if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
3059
info_ptr->bit_depth == 8 &&
3060
png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
3061
image->colormap_entries == 244 /* 216 + 1 + 27 */)
3062
break;
3063
3064
goto bad_output;
3065
3066
default:
3067
bad_output:
3068
png_error(png_ptr, "bad color-map processing (internal error)");
3069
}
3070
3071
/* Now read the rows. Do this here if it is possible to read directly into
3072
* the output buffer, otherwise allocate a local row buffer of the maximum
3073
* size libpng requires and call the relevant processing routine safely.
3074
*/
3075
{
3076
png_voidp first_row = display->buffer;
3077
ptrdiff_t row_bytes = display->row_stride;
3078
3079
/* The following expression is designed to work correctly whether it gives
3080
* a signed or an unsigned result.
3081
*/
3082
if (row_bytes < 0)
3083
{
3084
char *ptr = png_voidcast(char*, first_row);
3085
ptr += (image->height-1) * (-row_bytes);
3086
first_row = png_voidcast(png_voidp, ptr);
3087
}
3088
3089
display->first_row = first_row;
3090
display->row_bytes = row_bytes;
3091
}
3092
3093
if (passes == 0)
3094
{
3095
int result;
3096
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3097
3098
display->local_row = row;
3099
result = png_safe_execute(image, png_image_read_and_map, display);
3100
display->local_row = NULL;
3101
png_free(png_ptr, row);
3102
3103
return result;
3104
}
3105
3106
else
3107
{
3108
png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
3109
3110
while (--passes >= 0)
3111
{
3112
png_uint_32 y = image->height;
3113
png_bytep row = png_voidcast(png_bytep, display->first_row);
3114
3115
for (; y > 0; --y)
3116
{
3117
png_read_row(png_ptr, row, NULL);
3118
row += row_bytes;
3119
}
3120
}
3121
3122
return 1;
3123
}
3124
}
3125
3126
/* Just the row reading part of png_image_read. */
3127
static int
3128
png_image_read_composite(png_voidp argument)
3129
{
3130
png_image_read_control *display = png_voidcast(png_image_read_control*,
3131
argument);
3132
png_imagep image = display->image;
3133
png_structrp png_ptr = image->opaque->png_ptr;
3134
int passes;
3135
3136
switch (png_ptr->interlaced)
3137
{
3138
case PNG_INTERLACE_NONE:
3139
passes = 1;
3140
break;
3141
3142
case PNG_INTERLACE_ADAM7:
3143
passes = PNG_INTERLACE_ADAM7_PASSES;
3144
break;
3145
3146
default:
3147
png_error(png_ptr, "unknown interlace type");
3148
}
3149
3150
{
3151
png_uint_32 height = image->height;
3152
png_uint_32 width = image->width;
3153
ptrdiff_t step_row = display->row_bytes;
3154
unsigned int channels =
3155
(image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
3156
int pass;
3157
3158
for (pass = 0; pass < passes; ++pass)
3159
{
3160
unsigned int startx, stepx, stepy;
3161
png_uint_32 y;
3162
3163
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3164
{
3165
/* The row may be empty for a short image: */
3166
if (PNG_PASS_COLS(width, pass) == 0)
3167
continue;
3168
3169
startx = PNG_PASS_START_COL(pass) * channels;
3170
stepx = PNG_PASS_COL_OFFSET(pass) * channels;
3171
y = PNG_PASS_START_ROW(pass);
3172
stepy = PNG_PASS_ROW_OFFSET(pass);
3173
}
3174
3175
else
3176
{
3177
y = 0;
3178
startx = 0;
3179
stepx = channels;
3180
stepy = 1;
3181
}
3182
3183
for (; y<height; y += stepy)
3184
{
3185
png_bytep inrow = png_voidcast(png_bytep, display->local_row);
3186
png_bytep outrow;
3187
png_const_bytep end_row;
3188
3189
/* Read the row, which is packed: */
3190
png_read_row(png_ptr, inrow, NULL);
3191
3192
outrow = png_voidcast(png_bytep, display->first_row);
3193
outrow += y * step_row;
3194
end_row = outrow + width * channels;
3195
3196
/* Now do the composition on each pixel in this row. */
3197
outrow += startx;
3198
for (; outrow < end_row; outrow += stepx)
3199
{
3200
png_byte alpha = inrow[channels];
3201
3202
if (alpha > 0) /* else no change to the output */
3203
{
3204
unsigned int c;
3205
3206
for (c=0; c<channels; ++c)
3207
{
3208
png_uint_32 component = inrow[c];
3209
3210
if (alpha < 255) /* else just use component */
3211
{
3212
/* This is PNG_OPTIMIZED_ALPHA, the component value
3213
* is a linear 8-bit value. Combine this with the
3214
* current outrow[c] value which is sRGB encoded.
3215
* Arithmetic here is 16-bits to preserve the output
3216
* values correctly.
3217
*/
3218
component *= 257*255; /* =65535 */
3219
component += (255-alpha)*png_sRGB_table[outrow[c]];
3220
3221
/* So 'component' is scaled by 255*65535 and is
3222
* therefore appropriate for the sRGB to linear
3223
* conversion table.
3224
*/
3225
component = PNG_sRGB_FROM_LINEAR(component);
3226
}
3227
3228
outrow[c] = (png_byte)component;
3229
}
3230
}
3231
3232
inrow += channels+1; /* components and alpha channel */
3233
}
3234
}
3235
}
3236
}
3237
3238
return 1;
3239
}
3240
3241
/* The do_local_background case; called when all the following transforms are to
3242
* be done:
3243
*
3244
* PNG_RGB_TO_GRAY
3245
* PNG_COMPOSITE
3246
* PNG_GAMMA
3247
*
3248
* This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
3249
* PNG_COMPOSITE code performs gamma correction, so we get double gamma
3250
* correction. The fix-up is to prevent the PNG_COMPOSITE operation from
3251
* happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
3252
* row and handles the removal or pre-multiplication of the alpha channel.
3253
*/
3254
static int
3255
png_image_read_background(png_voidp argument)
3256
{
3257
png_image_read_control *display = png_voidcast(png_image_read_control*,
3258
argument);
3259
png_imagep image = display->image;
3260
png_structrp png_ptr = image->opaque->png_ptr;
3261
png_inforp info_ptr = image->opaque->info_ptr;
3262
png_uint_32 height = image->height;
3263
png_uint_32 width = image->width;
3264
int pass, passes;
3265
3266
/* Double check the convoluted logic below. We expect to get here with
3267
* libpng doing rgb to gray and gamma correction but background processing
3268
* left to the png_image_read_background function. The rows libpng produce
3269
* might be 8 or 16-bit but should always have two channels; gray plus alpha.
3270
*/
3271
if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
3272
png_error(png_ptr, "lost rgb to gray");
3273
3274
if ((png_ptr->transformations & PNG_COMPOSE) != 0)
3275
png_error(png_ptr, "unexpected compose");
3276
3277
if (png_get_channels(png_ptr, info_ptr) != 2)
3278
png_error(png_ptr, "lost/gained channels");
3279
3280
/* Expect the 8-bit case to always remove the alpha channel */
3281
if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
3282
(image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
3283
png_error(png_ptr, "unexpected 8-bit transformation");
3284
3285
switch (png_ptr->interlaced)
3286
{
3287
case PNG_INTERLACE_NONE:
3288
passes = 1;
3289
break;
3290
3291
case PNG_INTERLACE_ADAM7:
3292
passes = PNG_INTERLACE_ADAM7_PASSES;
3293
break;
3294
3295
default:
3296
png_error(png_ptr, "unknown interlace type");
3297
}
3298
3299
/* Use direct access to info_ptr here because otherwise the simplified API
3300
* would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
3301
* checking the value after libpng expansions, not the original value in the
3302
* PNG.
3303
*/
3304
switch (info_ptr->bit_depth)
3305
{
3306
case 8:
3307
/* 8-bit sRGB gray values with an alpha channel; the alpha channel is
3308
* to be removed by composing on a background: either the row if
3309
* display->background is NULL or display->background->green if not.
3310
* Unlike the code above ALPHA_OPTIMIZED has *not* been done.
3311
*/
3312
{
3313
png_bytep first_row = png_voidcast(png_bytep, display->first_row);
3314
ptrdiff_t step_row = display->row_bytes;
3315
3316
for (pass = 0; pass < passes; ++pass)
3317
{
3318
unsigned int startx, stepx, stepy;
3319
png_uint_32 y;
3320
3321
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3322
{
3323
/* The row may be empty for a short image: */
3324
if (PNG_PASS_COLS(width, pass) == 0)
3325
continue;
3326
3327
startx = PNG_PASS_START_COL(pass);
3328
stepx = PNG_PASS_COL_OFFSET(pass);
3329
y = PNG_PASS_START_ROW(pass);
3330
stepy = PNG_PASS_ROW_OFFSET(pass);
3331
}
3332
3333
else
3334
{
3335
y = 0;
3336
startx = 0;
3337
stepx = stepy = 1;
3338
}
3339
3340
if (display->background == NULL)
3341
{
3342
for (; y<height; y += stepy)
3343
{
3344
png_bytep inrow = png_voidcast(png_bytep,
3345
display->local_row);
3346
png_bytep outrow = first_row + y * step_row;
3347
png_const_bytep end_row = outrow + width;
3348
3349
/* Read the row, which is packed: */
3350
png_read_row(png_ptr, inrow, NULL);
3351
3352
/* Now do the composition on each pixel in this row. */
3353
outrow += startx;
3354
for (; outrow < end_row; outrow += stepx)
3355
{
3356
png_byte alpha = inrow[1];
3357
3358
if (alpha > 0) /* else no change to the output */
3359
{
3360
png_uint_32 component = inrow[0];
3361
3362
if (alpha < 255) /* else just use component */
3363
{
3364
/* Since PNG_OPTIMIZED_ALPHA was not set it is
3365
* necessary to invert the sRGB transfer
3366
* function and multiply the alpha out.
3367
*/
3368
component = png_sRGB_table[component] * alpha;
3369
component += png_sRGB_table[outrow[0]] *
3370
(255-alpha);
3371
component = PNG_sRGB_FROM_LINEAR(component);
3372
}
3373
3374
outrow[0] = (png_byte)component;
3375
}
3376
3377
inrow += 2; /* gray and alpha channel */
3378
}
3379
}
3380
}
3381
3382
else /* constant background value */
3383
{
3384
png_byte background8 = display->background->green;
3385
png_uint_16 background = png_sRGB_table[background8];
3386
3387
for (; y<height; y += stepy)
3388
{
3389
png_bytep inrow = png_voidcast(png_bytep,
3390
display->local_row);
3391
png_bytep outrow = first_row + y * step_row;
3392
png_const_bytep end_row = outrow + width;
3393
3394
/* Read the row, which is packed: */
3395
png_read_row(png_ptr, inrow, NULL);
3396
3397
/* Now do the composition on each pixel in this row. */
3398
outrow += startx;
3399
for (; outrow < end_row; outrow += stepx)
3400
{
3401
png_byte alpha = inrow[1];
3402
3403
if (alpha > 0) /* else use background */
3404
{
3405
png_uint_32 component = inrow[0];
3406
3407
if (alpha < 255) /* else just use component */
3408
{
3409
component = png_sRGB_table[component] * alpha;
3410
component += background * (255-alpha);
3411
component = PNG_sRGB_FROM_LINEAR(component);
3412
}
3413
3414
outrow[0] = (png_byte)component;
3415
}
3416
3417
else
3418
outrow[0] = background8;
3419
3420
inrow += 2; /* gray and alpha channel */
3421
}
3422
}
3423
}
3424
}
3425
}
3426
break;
3427
3428
case 16:
3429
/* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
3430
* still be done and, maybe, the alpha channel removed. This code also
3431
* handles the alpha-first option.
3432
*/
3433
{
3434
png_uint_16p first_row = png_voidcast(png_uint_16p,
3435
display->first_row);
3436
/* The division by two is safe because the caller passed in a
3437
* stride which was multiplied by 2 (below) to get row_bytes.
3438
*/
3439
ptrdiff_t step_row = display->row_bytes / 2;
3440
unsigned int preserve_alpha = (image->format &
3441
PNG_FORMAT_FLAG_ALPHA) != 0;
3442
unsigned int outchannels = 1U+preserve_alpha;
3443
int swap_alpha = 0;
3444
3445
# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
3446
if (preserve_alpha != 0 &&
3447
(image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
3448
swap_alpha = 1;
3449
# endif
3450
3451
for (pass = 0; pass < passes; ++pass)
3452
{
3453
unsigned int startx, stepx, stepy;
3454
png_uint_32 y;
3455
3456
/* The 'x' start and step are adjusted to output components here.
3457
*/
3458
if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
3459
{
3460
/* The row may be empty for a short image: */
3461
if (PNG_PASS_COLS(width, pass) == 0)
3462
continue;
3463
3464
startx = PNG_PASS_START_COL(pass) * outchannels;
3465
stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
3466
y = PNG_PASS_START_ROW(pass);
3467
stepy = PNG_PASS_ROW_OFFSET(pass);
3468
}
3469
3470
else
3471
{
3472
y = 0;
3473
startx = 0;
3474
stepx = outchannels;
3475
stepy = 1;
3476
}
3477
3478
for (; y<height; y += stepy)
3479
{
3480
png_const_uint_16p inrow;
3481
png_uint_16p outrow = first_row + y*step_row;
3482
png_uint_16p end_row = outrow + width * outchannels;
3483
3484
/* Read the row, which is packed: */
3485
png_read_row(png_ptr, png_voidcast(png_bytep,
3486
display->local_row), NULL);
3487
inrow = png_voidcast(png_const_uint_16p, display->local_row);
3488
3489
/* Now do the pre-multiplication on each pixel in this row.
3490
*/
3491
outrow += startx;
3492
for (; outrow < end_row; outrow += stepx)
3493
{
3494
png_uint_32 component = inrow[0];
3495
png_uint_16 alpha = inrow[1];
3496
3497
if (alpha > 0) /* else 0 */
3498
{
3499
if (alpha < 65535) /* else just use component */
3500
{
3501
component *= alpha;
3502
component += 32767;
3503
component /= 65535;
3504
}
3505
}
3506
3507
else
3508
component = 0;
3509
3510
outrow[swap_alpha] = (png_uint_16)component;
3511
if (preserve_alpha != 0)
3512
outrow[1 ^ swap_alpha] = alpha;
3513
3514
inrow += 2; /* components and alpha channel */
3515
}
3516
}
3517
}
3518
}
3519
break;
3520
3521
#ifdef __GNUC__
3522
default:
3523
png_error(png_ptr, "unexpected bit depth");
3524
#endif
3525
}
3526
3527
return 1;
3528
}
3529
3530
/* The guts of png_image_finish_read as a png_safe_execute callback. */
3531
static int
3532
png_image_read_direct(png_voidp argument)
3533
{
3534
png_image_read_control *display = png_voidcast(png_image_read_control*,
3535
argument);
3536
png_imagep image = display->image;
3537
png_structrp png_ptr = image->opaque->png_ptr;
3538
png_inforp info_ptr = image->opaque->info_ptr;
3539
3540
png_uint_32 format = image->format;
3541
int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
3542
int do_local_compose = 0;
3543
int do_local_background = 0; /* to avoid double gamma correction bug */
3544
int passes = 0;
3545
3546
/* Add transforms to ensure the correct output format is produced then check
3547
* that the required implementation support is there. Always expand; always
3548
* need 8 bits minimum, no palette and expanded tRNS.
3549
*/
3550
png_set_expand(png_ptr);
3551
3552
/* Now check the format to see if it was modified. */
3553
{
3554
png_uint_32 base_format = png_image_format(png_ptr) &
3555
~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
3556
png_uint_32 change = format ^ base_format;
3557
png_fixed_point output_gamma;
3558
int mode; /* alpha mode */
3559
3560
/* Do this first so that we have a record if rgb to gray is happening. */
3561
if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
3562
{
3563
/* gray<->color transformation required. */
3564
if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3565
png_set_gray_to_rgb(png_ptr);
3566
3567
else
3568
{
3569
/* libpng can't do both rgb to gray and
3570
* background/pre-multiplication if there is also significant gamma
3571
* correction, because both operations require linear colors and
3572
* the code only supports one transform doing the gamma correction.
3573
* Handle this by doing the pre-multiplication or background
3574
* operation in this code, if necessary.
3575
*
3576
* TODO: fix this by rewriting pngrtran.c (!)
3577
*
3578
* For the moment (given that fixing this in pngrtran.c is an
3579
* enormous change) 'do_local_background' is used to indicate that
3580
* the problem exists.
3581
*/
3582
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3583
do_local_background = 1/*maybe*/;
3584
3585
png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
3586
PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
3587
}
3588
3589
change &= ~PNG_FORMAT_FLAG_COLOR;
3590
}
3591
3592
/* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
3593
*/
3594
{
3595
/* This is safe but should no longer be necessary as
3596
* png_ptr->default_gamma should have been set after the
3597
* info-before-IDAT was read in png_image_read_header.
3598
*
3599
* TODO: 1.8: remove this and see what happens.
3600
*/
3601
png_fixed_point input_gamma_default;
3602
3603
if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
3604
(image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
3605
input_gamma_default = PNG_GAMMA_LINEAR;
3606
else
3607
input_gamma_default = PNG_DEFAULT_sRGB;
3608
3609
/* Call png_set_alpha_mode to set the default for the input gamma; the
3610
* output gamma is set by a second call below.
3611
*/
3612
png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
3613
}
3614
3615
if (linear != 0)
3616
{
3617
/* If there *is* an alpha channel in the input it must be multiplied
3618
* out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
3619
*/
3620
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3621
mode = PNG_ALPHA_STANDARD; /* associated alpha */
3622
3623
else
3624
mode = PNG_ALPHA_PNG;
3625
3626
output_gamma = PNG_GAMMA_LINEAR;
3627
}
3628
3629
else
3630
{
3631
mode = PNG_ALPHA_PNG;
3632
output_gamma = PNG_DEFAULT_sRGB;
3633
}
3634
3635
if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
3636
{
3637
mode = PNG_ALPHA_OPTIMIZED;
3638
change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3639
}
3640
3641
/* If 'do_local_background' is set check for the presence of gamma
3642
* correction; this is part of the work-round for the libpng bug
3643
* described above.
3644
*
3645
* TODO: fix libpng and remove this.
3646
*/
3647
if (do_local_background != 0)
3648
{
3649
png_fixed_point gtest;
3650
3651
/* This is 'png_gamma_threshold' from pngrtran.c; the test used for
3652
* gamma correction, the screen gamma hasn't been set on png_struct
3653
* yet; it's set below. png_struct::gamma, however, is set to the
3654
* final value.
3655
*/
3656
if (png_muldiv(&gtest, output_gamma,
3657
png_resolve_file_gamma(png_ptr), PNG_FP_1) != 0 &&
3658
png_gamma_significant(gtest) == 0)
3659
do_local_background = 0;
3660
3661
else if (mode == PNG_ALPHA_STANDARD)
3662
{
3663
do_local_background = 2/*required*/;
3664
mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
3665
}
3666
3667
/* else leave as 1 for the checks below */
3668
}
3669
3670
/* If the bit-depth changes then handle that here. */
3671
if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
3672
{
3673
if (linear != 0 /*16-bit output*/)
3674
png_set_expand_16(png_ptr);
3675
3676
else /* 8-bit output */
3677
png_set_scale_16(png_ptr);
3678
3679
change &= ~PNG_FORMAT_FLAG_LINEAR;
3680
}
3681
3682
/* Now the background/alpha channel changes. */
3683
if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
3684
{
3685
/* Removing an alpha channel requires composition for the 8-bit
3686
* formats; for the 16-bit it is already done, above, by the
3687
* pre-multiplication and the channel just needs to be stripped.
3688
*/
3689
if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
3690
{
3691
/* If RGB->gray is happening the alpha channel must be left and the
3692
* operation completed locally.
3693
*
3694
* TODO: fix libpng and remove this.
3695
*/
3696
if (do_local_background != 0)
3697
do_local_background = 2/*required*/;
3698
3699
/* 16-bit output: just remove the channel */
3700
else if (linear != 0) /* compose on black (well, pre-multiply) */
3701
png_set_strip_alpha(png_ptr);
3702
3703
/* 8-bit output: do an appropriate compose */
3704
else if (display->background != NULL)
3705
{
3706
png_color_16 c;
3707
3708
c.index = 0; /*unused*/
3709
c.red = display->background->red;
3710
c.green = display->background->green;
3711
c.blue = display->background->blue;
3712
c.gray = display->background->green;
3713
3714
/* This is always an 8-bit sRGB value, using the 'green' channel
3715
* for gray is much better than calculating the luminance here;
3716
* we can get off-by-one errors in that calculation relative to
3717
* the app expectations and that will show up in transparent
3718
* pixels.
3719
*/
3720
png_set_background_fixed(png_ptr, &c,
3721
PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
3722
0/*gamma: not used*/);
3723
}
3724
3725
else /* compose on row: implemented below. */
3726
{
3727
do_local_compose = 1;
3728
/* This leaves the alpha channel in the output, so it has to be
3729
* removed by the code below. Set the encoding to the 'OPTIMIZE'
3730
* one so the code only has to hack on the pixels that require
3731
* composition.
3732
*/
3733
mode = PNG_ALPHA_OPTIMIZED;
3734
}
3735
}
3736
3737
else /* output needs an alpha channel */
3738
{
3739
/* This is tricky because it happens before the swap operation has
3740
* been accomplished; however, the swap does *not* swap the added
3741
* alpha channel (weird API), so it must be added in the correct
3742
* place.
3743
*/
3744
png_uint_32 filler; /* opaque filler */
3745
int where;
3746
3747
if (linear != 0)
3748
filler = 65535;
3749
3750
else
3751
filler = 255;
3752
3753
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
3754
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3755
{
3756
where = PNG_FILLER_BEFORE;
3757
change &= ~PNG_FORMAT_FLAG_AFIRST;
3758
}
3759
3760
else
3761
#endif
3762
where = PNG_FILLER_AFTER;
3763
3764
png_set_add_alpha(png_ptr, filler, where);
3765
}
3766
3767
/* This stops the (irrelevant) call to swap_alpha below. */
3768
change &= ~PNG_FORMAT_FLAG_ALPHA;
3769
}
3770
3771
/* Now set the alpha mode correctly; this is always done, even if there is
3772
* no alpha channel in either the input or the output because it correctly
3773
* sets the output gamma.
3774
*/
3775
png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
3776
3777
# ifdef PNG_FORMAT_BGR_SUPPORTED
3778
if ((change & PNG_FORMAT_FLAG_BGR) != 0)
3779
{
3780
/* Check only the output format; PNG is never BGR; don't do this if
3781
* the output is gray, but fix up the 'format' value in that case.
3782
*/
3783
if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
3784
png_set_bgr(png_ptr);
3785
3786
else
3787
format &= ~PNG_FORMAT_FLAG_BGR;
3788
3789
change &= ~PNG_FORMAT_FLAG_BGR;
3790
}
3791
# endif
3792
3793
# ifdef PNG_FORMAT_AFIRST_SUPPORTED
3794
if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
3795
{
3796
/* Only relevant if there is an alpha channel - it's particularly
3797
* important to handle this correctly because do_local_compose may
3798
* be set above and then libpng will keep the alpha channel for this
3799
* code to remove.
3800
*/
3801
if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
3802
{
3803
/* Disable this if doing a local background,
3804
* TODO: remove this when local background is no longer required.
3805
*/
3806
if (do_local_background != 2)
3807
png_set_swap_alpha(png_ptr);
3808
}
3809
3810
else
3811
format &= ~PNG_FORMAT_FLAG_AFIRST;
3812
3813
change &= ~PNG_FORMAT_FLAG_AFIRST;
3814
}
3815
# endif
3816
3817
/* If the *output* is 16-bit then we need to check for a byte-swap on this
3818
* architecture.
3819
*/
3820
if (linear != 0)
3821
{
3822
png_uint_16 le = 0x0001;
3823
3824
if ((*(png_const_bytep) & le) != 0)
3825
png_set_swap(png_ptr);
3826
}
3827
3828
/* If change is not now 0 some transformation is missing - error out. */
3829
if (change != 0)
3830
png_error(png_ptr, "png_read_image: unsupported transformation");
3831
}
3832
3833
PNG_SKIP_CHUNKS(png_ptr);
3834
3835
/* Update the 'info' structure and make sure the result is as required; first
3836
* make sure to turn on the interlace handling if it will be required
3837
* (because it can't be turned on *after* the call to png_read_update_info!)
3838
*
3839
* TODO: remove the do_local_background fixup below.
3840
*/
3841
if (do_local_compose == 0 && do_local_background != 2)
3842
passes = png_set_interlace_handling(png_ptr);
3843
3844
png_read_update_info(png_ptr, info_ptr);
3845
3846
{
3847
png_uint_32 info_format = 0;
3848
3849
if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3850
info_format |= PNG_FORMAT_FLAG_COLOR;
3851
3852
if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
3853
{
3854
/* do_local_compose removes this channel below. */
3855
if (do_local_compose == 0)
3856
{
3857
/* do_local_background does the same if required. */
3858
if (do_local_background != 2 ||
3859
(format & PNG_FORMAT_FLAG_ALPHA) != 0)
3860
info_format |= PNG_FORMAT_FLAG_ALPHA;
3861
}
3862
}
3863
3864
else if (do_local_compose != 0) /* internal error */
3865
png_error(png_ptr, "png_image_read: alpha channel lost");
3866
3867
if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
3868
info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
3869
}
3870
3871
if (info_ptr->bit_depth == 16)
3872
info_format |= PNG_FORMAT_FLAG_LINEAR;
3873
3874
#ifdef PNG_FORMAT_BGR_SUPPORTED
3875
if ((png_ptr->transformations & PNG_BGR) != 0)
3876
info_format |= PNG_FORMAT_FLAG_BGR;
3877
#endif
3878
3879
#ifdef PNG_FORMAT_AFIRST_SUPPORTED
3880
if (do_local_background == 2)
3881
{
3882
if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
3883
info_format |= PNG_FORMAT_FLAG_AFIRST;
3884
}
3885
3886
if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
3887
((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
3888
(png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
3889
{
3890
if (do_local_background == 2)
3891
png_error(png_ptr, "unexpected alpha swap transformation");
3892
3893
info_format |= PNG_FORMAT_FLAG_AFIRST;
3894
}
3895
# endif
3896
3897
/* This is actually an internal error. */
3898
if (info_format != format)
3899
png_error(png_ptr, "png_read_image: invalid transformations");
3900
}
3901
3902
/* Now read the rows. If do_local_compose is set then it is necessary to use
3903
* a local row buffer. The output will be GA, RGBA or BGRA and must be
3904
* converted to G, RGB or BGR as appropriate. The 'local_row' member of the
3905
* display acts as a flag.
3906
*/
3907
{
3908
png_voidp first_row = display->buffer;
3909
ptrdiff_t row_bytes = display->row_stride;
3910
3911
if (linear != 0)
3912
row_bytes *= 2;
3913
3914
/* The following expression is designed to work correctly whether it gives
3915
* a signed or an unsigned result.
3916
*/
3917
if (row_bytes < 0)
3918
{
3919
char *ptr = png_voidcast(char*, first_row);
3920
ptr += (image->height-1) * (-row_bytes);
3921
first_row = png_voidcast(png_voidp, ptr);
3922
}
3923
3924
display->first_row = first_row;
3925
display->row_bytes = row_bytes;
3926
}
3927
3928
if (do_local_compose != 0)
3929
{
3930
int result;
3931
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3932
3933
display->local_row = row;
3934
result = png_safe_execute(image, png_image_read_composite, display);
3935
display->local_row = NULL;
3936
png_free(png_ptr, row);
3937
3938
return result;
3939
}
3940
3941
else if (do_local_background == 2)
3942
{
3943
int result;
3944
png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
3945
3946
display->local_row = row;
3947
result = png_safe_execute(image, png_image_read_background, display);
3948
display->local_row = NULL;
3949
png_free(png_ptr, row);
3950
3951
return result;
3952
}
3953
3954
else
3955
{
3956
png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
3957
3958
while (--passes >= 0)
3959
{
3960
png_uint_32 y = image->height;
3961
png_bytep row = png_voidcast(png_bytep, display->first_row);
3962
3963
for (; y > 0; --y)
3964
{
3965
png_read_row(png_ptr, row, NULL);
3966
row += row_bytes;
3967
}
3968
}
3969
3970
return 1;
3971
}
3972
}
3973
3974
int PNGAPI
3975
png_image_finish_read(png_imagep image, png_const_colorp background,
3976
void *buffer, png_int_32 row_stride, void *colormap)
3977
{
3978
if (image != NULL && image->version == PNG_IMAGE_VERSION)
3979
{
3980
/* Check for row_stride overflow. This check is not performed on the
3981
* original PNG format because it may not occur in the output PNG format
3982
* and libpng deals with the issues of reading the original.
3983
*/
3984
unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
3985
3986
/* The following checks just the 'row_stride' calculation to ensure it
3987
* fits in a signed 32-bit value. Because channels/components can be
3988
* either 1 or 2 bytes in size the length of a row can still overflow 32
3989
* bits; this is just to verify that the 'row_stride' argument can be
3990
* represented.
3991
*/
3992
if (image->width <= 0x7fffffffU/channels) /* no overflow */
3993
{
3994
png_uint_32 check;
3995
png_uint_32 png_row_stride = image->width * channels;
3996
3997
if (row_stride == 0)
3998
row_stride = (png_int_32)/*SAFE*/png_row_stride;
3999
4000
if (row_stride < 0)
4001
check = (png_uint_32)(-row_stride);
4002
4003
else
4004
check = (png_uint_32)row_stride;
4005
4006
/* This verifies 'check', the absolute value of the actual stride
4007
* passed in and detects overflow in the application calculation (i.e.
4008
* if the app did actually pass in a non-zero 'row_stride'.
4009
*/
4010
if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
4011
{
4012
/* Now check for overflow of the image buffer calculation; this
4013
* limits the whole image size to 32 bits for API compatibility with
4014
* the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
4015
*
4016
* The PNG_IMAGE_BUFFER_SIZE macro is:
4017
*
4018
* (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
4019
*
4020
* And the component size is always 1 or 2, so make sure that the
4021
* number of *bytes* that the application is saying are available
4022
* does actually fit into a 32-bit number.
4023
*
4024
* NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
4025
* will be changed to use png_alloc_size_t; bigger images can be
4026
* accommodated on 64-bit systems.
4027
*/
4028
if (image->height <=
4029
0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
4030
{
4031
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
4032
(image->colormap_entries > 0 && colormap != NULL))
4033
{
4034
int result;
4035
png_image_read_control display;
4036
4037
memset(&display, 0, (sizeof display));
4038
display.image = image;
4039
display.buffer = buffer;
4040
display.row_stride = row_stride;
4041
display.colormap = colormap;
4042
display.background = background;
4043
display.local_row = NULL;
4044
4045
/* Choose the correct 'end' routine; for the color-map case
4046
* all the setup has already been done.
4047
*/
4048
if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
4049
result =
4050
png_safe_execute(image,
4051
png_image_read_colormap, &display) &&
4052
png_safe_execute(image,
4053
png_image_read_colormapped, &display);
4054
4055
else
4056
result =
4057
png_safe_execute(image,
4058
png_image_read_direct, &display);
4059
4060
png_image_free(image);
4061
return result;
4062
}
4063
4064
else
4065
return png_image_error(image,
4066
"png_image_finish_read[color-map]: no color-map");
4067
}
4068
4069
else
4070
return png_image_error(image,
4071
"png_image_finish_read: image too large");
4072
}
4073
4074
else
4075
return png_image_error(image,
4076
"png_image_finish_read: invalid argument");
4077
}
4078
4079
else
4080
return png_image_error(image,
4081
"png_image_finish_read: row_stride too large");
4082
}
4083
4084
else if (image != NULL)
4085
return png_image_error(image,
4086
"png_image_finish_read: damaged PNG_IMAGE_VERSION");
4087
4088
return 0;
4089
}
4090
4091
#endif /* SIMPLIFIED_READ */
4092
#endif /* READ */
4093
4094