CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

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