]> git.saurik.com Git - wxWidgets.git/blame - src/png/pngread.c
fixing RTTI
[wxWidgets.git] / src / png / pngread.c
CommitLineData
0272a10d
VZ
1
2/* pngread.c - read a PNG file
3 *
9c0d9ce3
DS
4 * Last changed in libpng 1.5.6 [November 3, 2011]
5 * Copyright (c) 1998-2011 Glenn Randers-Pehrson
0272a10d
VZ
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 *
b61cc19c
PC
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 *
0272a10d
VZ
13 * This file contains routines that an application calls directly to
14 * read a PNG file or stream.
15 */
16
b61cc19c
PC
17#include "pngpriv.h"
18
9c0d9ce3 19#ifdef PNG_READ_SUPPORTED
0272a10d
VZ
20
21/* Create a PNG structure for reading, and allocate any memory needed. */
9c0d9ce3
DS
22PNG_FUNCTION(png_structp,PNGAPI
23png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
24 png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
0272a10d
VZ
25{
26
27#ifdef PNG_USER_MEM_SUPPORTED
28 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
9c0d9ce3 29 warn_fn, NULL, NULL, NULL));
0272a10d
VZ
30}
31
b61cc19c
PC
32/* Alternate create PNG structure for reading, and allocate any memory
33 * needed.
34 */
9c0d9ce3
DS
35PNG_FUNCTION(png_structp,PNGAPI
36png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
37 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
38 png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
0272a10d
VZ
39{
40#endif /* PNG_USER_MEM_SUPPORTED */
41
970f6abe
VZ
42#ifdef PNG_SETJMP_SUPPORTED
43 volatile
44#endif
0272a10d 45 png_structp png_ptr;
b61cc19c 46 volatile int png_cleanup_needed = 0;
0272a10d
VZ
47
48#ifdef PNG_SETJMP_SUPPORTED
49#ifdef USE_FAR_KEYWORD
9c0d9ce3 50 jmp_buf tmp_jmpbuf;
0272a10d
VZ
51#endif
52#endif
53
970f6abe 54 png_debug(1, "in png_create_read_struct");
b61cc19c 55
0272a10d
VZ
56#ifdef PNG_USER_MEM_SUPPORTED
57 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
9c0d9ce3 58 malloc_fn, mem_ptr);
0272a10d
VZ
59#else
60 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
61#endif
62 if (png_ptr == NULL)
63 return (NULL);
64
b61cc19c
PC
65 /* Added at libpng-1.2.6 */
66#ifdef PNG_USER_LIMITS_SUPPORTED
67 png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
68 png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
9c0d9ce3 69
b61cc19c
PC
70# ifdef PNG_USER_CHUNK_CACHE_MAX
71 /* Added at libpng-1.2.43 and 1.4.0 */
72 png_ptr->user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
73# endif
9c0d9ce3 74
b61cc19c
PC
75# ifdef PNG_SET_USER_CHUNK_MALLOC_MAX
76 /* Added at libpng-1.2.43 and 1.4.1 */
77 png_ptr->user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
78# endif
0272a10d
VZ
79#endif
80
81#ifdef PNG_SETJMP_SUPPORTED
b61cc19c
PC
82/* Applications that neglect to set up their own setjmp() and then
83 encounter a png_error() will longjmp here. Since the jmpbuf is
84 then meaningless we abort instead of returning. */
0272a10d 85#ifdef USE_FAR_KEYWORD
9c0d9ce3 86 if (setjmp(tmp_jmpbuf))
0272a10d 87#else
b61cc19c 88 if (setjmp(png_jmpbuf(png_ptr))) /* Sets longjmp to match setjmp */
0272a10d 89#endif
b61cc19c 90 PNG_ABORT();
0272a10d 91#ifdef USE_FAR_KEYWORD
9c0d9ce3 92 png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
0272a10d 93#endif
b61cc19c 94#endif /* PNG_SETJMP_SUPPORTED */
0272a10d
VZ
95
96#ifdef PNG_USER_MEM_SUPPORTED
97 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
98#endif
99
100 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
101
9c0d9ce3
DS
102 /* Call the general version checker (shared with read and write code): */
103 if (!png_user_version_check(png_ptr, user_png_ver))
104 png_cleanup_needed = 1;
0272a10d 105
b61cc19c
PC
106 if (!png_cleanup_needed)
107 {
108 /* Initialize zbuf - compression buffer */
0272a10d 109 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
9c0d9ce3
DS
110 png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr, png_ptr->zbuf_size);
111
b61cc19c 112 if (png_ptr->zbuf == NULL)
9c0d9ce3 113 png_cleanup_needed = 1;
b61cc19c 114 }
9c0d9ce3 115
0272a10d
VZ
116 png_ptr->zstream.zalloc = png_zalloc;
117 png_ptr->zstream.zfree = png_zfree;
118 png_ptr->zstream.opaque = (voidpf)png_ptr;
119
b61cc19c 120 if (!png_cleanup_needed)
0272a10d 121 {
b61cc19c 122 switch (inflateInit(&png_ptr->zstream))
0272a10d 123 {
9c0d9ce3
DS
124 case Z_OK:
125 break; /* Do nothing */
126
b61cc19c 127 case Z_MEM_ERROR:
9c0d9ce3
DS
128 png_warning(png_ptr, "zlib memory error");
129 png_cleanup_needed = 1;
130 break;
131
132 case Z_STREAM_ERROR:
133 png_warning(png_ptr, "zlib stream error");
134 png_cleanup_needed = 1;
135 break;
136
137 case Z_VERSION_ERROR:
138 png_warning(png_ptr, "zlib version error");
139 png_cleanup_needed = 1;
140 break;
141
b61cc19c
PC
142 default: png_warning(png_ptr, "Unknown zlib error");
143 png_cleanup_needed = 1;
0272a10d 144 }
0272a10d 145 }
0272a10d 146
b61cc19c 147 if (png_cleanup_needed)
0272a10d 148 {
b61cc19c
PC
149 /* Clean up PNG structure and deallocate any memory. */
150 png_free(png_ptr, png_ptr->zbuf);
151 png_ptr->zbuf = NULL;
152#ifdef PNG_USER_MEM_SUPPORTED
153 png_destroy_struct_2((png_voidp)png_ptr,
9c0d9ce3 154 (png_free_ptr)free_fn, (png_voidp)mem_ptr);
0272a10d 155#else
b61cc19c 156 png_destroy_struct((png_voidp)png_ptr);
0272a10d 157#endif
b61cc19c 158 return (NULL);
0272a10d
VZ
159 }
160
161 png_ptr->zstream.next_out = png_ptr->zbuf;
162 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
163
b61cc19c
PC
164 png_set_read_fn(png_ptr, NULL, NULL);
165
166
167 return (png_ptr);
0272a10d
VZ
168}
169
b61cc19c
PC
170
171#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
172/* Read the information before the actual image data. This has been
173 * changed in v0.90 to allow reading a file that already has the magic
174 * bytes read from the stream. You can tell libpng how many bytes have
175 * been read from the beginning of the stream (up to the maximum of 8)
176 * via png_set_sig_bytes(), and we will only check the remaining bytes
177 * here. The application can then have access to the signature bytes we
178 * read if it is determined that this isn't a valid PNG file.
179 */
180void PNGAPI
181png_read_info(png_structp png_ptr, png_infop info_ptr)
182{
970f6abe 183 png_debug(1, "in png_read_info");
9c0d9ce3 184
b61cc19c
PC
185 if (png_ptr == NULL || info_ptr == NULL)
186 return;
0272a10d 187
9c0d9ce3
DS
188 /* Read and check the PNG file signature. */
189 png_read_sig(png_ptr, info_ptr);
0272a10d 190
970f6abe 191 for (;;)
0272a10d 192 {
970f6abe 193 png_uint_32 length = png_read_chunk_header(png_ptr);
9c0d9ce3 194 png_uint_32 chunk_name = png_ptr->chunk_name;
0272a10d
VZ
195
196 /* This should be a binary subdivision search or a hash for
197 * matching the chunk name rather than a linear search.
198 */
9c0d9ce3
DS
199 if (chunk_name == png_IDAT)
200 if (png_ptr->mode & PNG_AFTER_IDAT)
201 png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
0272a10d 202
9c0d9ce3 203 if (chunk_name == png_IHDR)
0272a10d 204 png_handle_IHDR(png_ptr, info_ptr, length);
9c0d9ce3
DS
205
206 else if (chunk_name == png_IEND)
0272a10d 207 png_handle_IEND(png_ptr, info_ptr, length);
9c0d9ce3 208
0272a10d 209#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
9c0d9ce3
DS
210 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
211 PNG_HANDLE_CHUNK_AS_DEFAULT)
0272a10d 212 {
9c0d9ce3 213 if (chunk_name == png_IDAT)
0272a10d 214 png_ptr->mode |= PNG_HAVE_IDAT;
9c0d9ce3 215
0272a10d 216 png_handle_unknown(png_ptr, info_ptr, length);
9c0d9ce3
DS
217
218 if (chunk_name == png_PLTE)
0272a10d 219 png_ptr->mode |= PNG_HAVE_PLTE;
9c0d9ce3
DS
220
221 else if (chunk_name == png_IDAT)
0272a10d
VZ
222 {
223 if (!(png_ptr->mode & PNG_HAVE_IHDR))
224 png_error(png_ptr, "Missing IHDR before IDAT");
9c0d9ce3 225
0272a10d 226 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
9c0d9ce3 227 !(png_ptr->mode & PNG_HAVE_PLTE))
0272a10d 228 png_error(png_ptr, "Missing PLTE before IDAT");
9c0d9ce3 229
0272a10d
VZ
230 break;
231 }
232 }
233#endif
9c0d9ce3 234 else if (chunk_name == png_PLTE)
0272a10d 235 png_handle_PLTE(png_ptr, info_ptr, length);
9c0d9ce3
DS
236
237 else if (chunk_name == png_IDAT)
0272a10d
VZ
238 {
239 if (!(png_ptr->mode & PNG_HAVE_IHDR))
240 png_error(png_ptr, "Missing IHDR before IDAT");
9c0d9ce3 241
0272a10d 242 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
9c0d9ce3 243 !(png_ptr->mode & PNG_HAVE_PLTE))
0272a10d
VZ
244 png_error(png_ptr, "Missing PLTE before IDAT");
245
246 png_ptr->idat_size = length;
247 png_ptr->mode |= PNG_HAVE_IDAT;
248 break;
249 }
9c0d9ce3 250
b61cc19c 251#ifdef PNG_READ_bKGD_SUPPORTED
9c0d9ce3 252 else if (chunk_name == png_bKGD)
0272a10d
VZ
253 png_handle_bKGD(png_ptr, info_ptr, length);
254#endif
9c0d9ce3 255
b61cc19c 256#ifdef PNG_READ_cHRM_SUPPORTED
9c0d9ce3 257 else if (chunk_name == png_cHRM)
0272a10d
VZ
258 png_handle_cHRM(png_ptr, info_ptr, length);
259#endif
9c0d9ce3 260
b61cc19c 261#ifdef PNG_READ_gAMA_SUPPORTED
9c0d9ce3 262 else if (chunk_name == png_gAMA)
0272a10d
VZ
263 png_handle_gAMA(png_ptr, info_ptr, length);
264#endif
9c0d9ce3 265
b61cc19c 266#ifdef PNG_READ_hIST_SUPPORTED
9c0d9ce3 267 else if (chunk_name == png_hIST)
0272a10d
VZ
268 png_handle_hIST(png_ptr, info_ptr, length);
269#endif
9c0d9ce3 270
b61cc19c 271#ifdef PNG_READ_oFFs_SUPPORTED
9c0d9ce3 272 else if (chunk_name == png_oFFs)
0272a10d
VZ
273 png_handle_oFFs(png_ptr, info_ptr, length);
274#endif
9c0d9ce3 275
b61cc19c 276#ifdef PNG_READ_pCAL_SUPPORTED
9c0d9ce3 277 else if (chunk_name == png_pCAL)
0272a10d
VZ
278 png_handle_pCAL(png_ptr, info_ptr, length);
279#endif
9c0d9ce3 280
b61cc19c 281#ifdef PNG_READ_sCAL_SUPPORTED
9c0d9ce3 282 else if (chunk_name == png_sCAL)
0272a10d
VZ
283 png_handle_sCAL(png_ptr, info_ptr, length);
284#endif
9c0d9ce3 285
b61cc19c 286#ifdef PNG_READ_pHYs_SUPPORTED
9c0d9ce3 287 else if (chunk_name == png_pHYs)
0272a10d
VZ
288 png_handle_pHYs(png_ptr, info_ptr, length);
289#endif
9c0d9ce3 290
b61cc19c 291#ifdef PNG_READ_sBIT_SUPPORTED
9c0d9ce3 292 else if (chunk_name == png_sBIT)
0272a10d
VZ
293 png_handle_sBIT(png_ptr, info_ptr, length);
294#endif
9c0d9ce3 295
b61cc19c 296#ifdef PNG_READ_sRGB_SUPPORTED
9c0d9ce3 297 else if (chunk_name == png_sRGB)
0272a10d
VZ
298 png_handle_sRGB(png_ptr, info_ptr, length);
299#endif
9c0d9ce3 300
b61cc19c 301#ifdef PNG_READ_iCCP_SUPPORTED
9c0d9ce3 302 else if (chunk_name == png_iCCP)
0272a10d
VZ
303 png_handle_iCCP(png_ptr, info_ptr, length);
304#endif
9c0d9ce3 305
b61cc19c 306#ifdef PNG_READ_sPLT_SUPPORTED
9c0d9ce3 307 else if (chunk_name == png_sPLT)
0272a10d
VZ
308 png_handle_sPLT(png_ptr, info_ptr, length);
309#endif
9c0d9ce3 310
b61cc19c 311#ifdef PNG_READ_tEXt_SUPPORTED
9c0d9ce3 312 else if (chunk_name == png_tEXt)
0272a10d
VZ
313 png_handle_tEXt(png_ptr, info_ptr, length);
314#endif
9c0d9ce3 315
b61cc19c 316#ifdef PNG_READ_tIME_SUPPORTED
9c0d9ce3 317 else if (chunk_name == png_tIME)
0272a10d
VZ
318 png_handle_tIME(png_ptr, info_ptr, length);
319#endif
9c0d9ce3 320
b61cc19c 321#ifdef PNG_READ_tRNS_SUPPORTED
9c0d9ce3 322 else if (chunk_name == png_tRNS)
0272a10d
VZ
323 png_handle_tRNS(png_ptr, info_ptr, length);
324#endif
9c0d9ce3 325
b61cc19c 326#ifdef PNG_READ_zTXt_SUPPORTED
9c0d9ce3 327 else if (chunk_name == png_zTXt)
0272a10d
VZ
328 png_handle_zTXt(png_ptr, info_ptr, length);
329#endif
9c0d9ce3 330
b61cc19c 331#ifdef PNG_READ_iTXt_SUPPORTED
9c0d9ce3 332 else if (chunk_name == png_iTXt)
0272a10d
VZ
333 png_handle_iTXt(png_ptr, info_ptr, length);
334#endif
9c0d9ce3 335
0272a10d
VZ
336 else
337 png_handle_unknown(png_ptr, info_ptr, length);
338 }
339}
b61cc19c 340#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 341
b61cc19c 342/* Optional call to update the users info_ptr structure */
0272a10d
VZ
343void PNGAPI
344png_read_update_info(png_structp png_ptr, png_infop info_ptr)
345{
970f6abe 346 png_debug(1, "in png_read_update_info");
9c0d9ce3 347
b61cc19c
PC
348 if (png_ptr == NULL)
349 return;
b61cc19c 350
9c0d9ce3
DS
351 png_read_start_row(png_ptr);
352
353#ifdef PNG_READ_TRANSFORMS_SUPPORTED
0272a10d 354 png_read_transform_info(png_ptr, info_ptr);
9c0d9ce3
DS
355#else
356 PNG_UNUSED(info_ptr)
357#endif
0272a10d
VZ
358}
359
b61cc19c 360#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
361/* Initialize palette, background, etc, after transformations
362 * are set, but before any reading takes place. This allows
363 * the user to obtain a gamma-corrected palette, for example.
364 * If the user doesn't call this, we will do it ourselves.
365 */
366void PNGAPI
367png_start_read_image(png_structp png_ptr)
368{
970f6abe 369 png_debug(1, "in png_start_read_image");
9c0d9ce3
DS
370
371 if (png_ptr != NULL)
372 png_read_start_row(png_ptr);
0272a10d 373}
b61cc19c 374#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 375
b61cc19c 376#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
377void PNGAPI
378png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
379{
0272a10d 380 int ret;
9c0d9ce3
DS
381
382 png_row_info row_info;
383
b61cc19c
PC
384 if (png_ptr == NULL)
385 return;
9c0d9ce3 386
970f6abe 387 png_debug2(1, "in png_read_row (row %lu, pass %d)",
9c0d9ce3 388 (unsigned long)png_ptr->row_number, png_ptr->pass);
b61cc19c 389
9c0d9ce3
DS
390 /* png_read_start_row sets the information (in particular iwidth) for this
391 * interlace pass.
392 */
0272a10d
VZ
393 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
394 png_read_start_row(png_ptr);
9c0d9ce3
DS
395
396 /* 1.5.6: row_info moved out of png_struct to a local here. */
397 row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
398 row_info.color_type = png_ptr->color_type;
399 row_info.bit_depth = png_ptr->bit_depth;
400 row_info.channels = png_ptr->channels;
401 row_info.pixel_depth = png_ptr->pixel_depth;
402 row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
403
0272a10d
VZ
404 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
405 {
b61cc19c 406 /* Check for transforms that have been set but were defined out */
0272a10d
VZ
407#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
408 if (png_ptr->transformations & PNG_INVERT_MONO)
b61cc19c 409 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
0272a10d 410#endif
9c0d9ce3 411
0272a10d
VZ
412#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
413 if (png_ptr->transformations & PNG_FILLER)
b61cc19c 414 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
0272a10d 415#endif
9c0d9ce3 416
b61cc19c
PC
417#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
418 !defined(PNG_READ_PACKSWAP_SUPPORTED)
0272a10d 419 if (png_ptr->transformations & PNG_PACKSWAP)
b61cc19c 420 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
0272a10d 421#endif
9c0d9ce3 422
0272a10d
VZ
423#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
424 if (png_ptr->transformations & PNG_PACK)
b61cc19c 425 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
0272a10d 426#endif
9c0d9ce3 427
0272a10d
VZ
428#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
429 if (png_ptr->transformations & PNG_SHIFT)
b61cc19c 430 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
0272a10d 431#endif
9c0d9ce3 432
0272a10d
VZ
433#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
434 if (png_ptr->transformations & PNG_BGR)
b61cc19c 435 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
0272a10d 436#endif
9c0d9ce3 437
0272a10d
VZ
438#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
439 if (png_ptr->transformations & PNG_SWAP_BYTES)
b61cc19c 440 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
0272a10d
VZ
441#endif
442 }
443
b61cc19c 444#ifdef PNG_READ_INTERLACING_SUPPORTED
9c0d9ce3
DS
445 /* If interlaced and we do not need a new row, combine row and return.
446 * Notice that the pixels we have from previous rows have been transformed
447 * already; we can only combine like with like (transformed or
448 * untransformed) and, because of the libpng API for interlaced images, this
449 * means we must transform before de-interlacing.
450 */
0272a10d
VZ
451 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
452 {
453 switch (png_ptr->pass)
454 {
455 case 0:
456 if (png_ptr->row_number & 0x07)
457 {
458 if (dsp_row != NULL)
9c0d9ce3 459 png_combine_row(png_ptr, dsp_row, 1/*display*/);
0272a10d
VZ
460 png_read_finish_row(png_ptr);
461 return;
462 }
463 break;
9c0d9ce3 464
0272a10d
VZ
465 case 1:
466 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
467 {
468 if (dsp_row != NULL)
9c0d9ce3
DS
469 png_combine_row(png_ptr, dsp_row, 1/*display*/);
470
0272a10d
VZ
471 png_read_finish_row(png_ptr);
472 return;
473 }
474 break;
9c0d9ce3 475
0272a10d
VZ
476 case 2:
477 if ((png_ptr->row_number & 0x07) != 4)
478 {
479 if (dsp_row != NULL && (png_ptr->row_number & 4))
9c0d9ce3
DS
480 png_combine_row(png_ptr, dsp_row, 1/*display*/);
481
0272a10d
VZ
482 png_read_finish_row(png_ptr);
483 return;
484 }
485 break;
9c0d9ce3 486
0272a10d
VZ
487 case 3:
488 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
489 {
490 if (dsp_row != NULL)
9c0d9ce3
DS
491 png_combine_row(png_ptr, dsp_row, 1/*display*/);
492
0272a10d
VZ
493 png_read_finish_row(png_ptr);
494 return;
495 }
496 break;
9c0d9ce3 497
0272a10d
VZ
498 case 4:
499 if ((png_ptr->row_number & 3) != 2)
500 {
501 if (dsp_row != NULL && (png_ptr->row_number & 2))
9c0d9ce3
DS
502 png_combine_row(png_ptr, dsp_row, 1/*display*/);
503
0272a10d
VZ
504 png_read_finish_row(png_ptr);
505 return;
506 }
507 break;
508 case 5:
509 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
510 {
511 if (dsp_row != NULL)
9c0d9ce3
DS
512 png_combine_row(png_ptr, dsp_row, 1/*display*/);
513
0272a10d
VZ
514 png_read_finish_row(png_ptr);
515 return;
516 }
517 break;
9c0d9ce3
DS
518
519 default:
0272a10d
VZ
520 case 6:
521 if (!(png_ptr->row_number & 1))
522 {
523 png_read_finish_row(png_ptr);
524 return;
525 }
526 break;
527 }
528 }
529#endif
530
531 if (!(png_ptr->mode & PNG_HAVE_IDAT))
532 png_error(png_ptr, "Invalid attempt to read row data");
533
534 png_ptr->zstream.next_out = png_ptr->row_buf;
b61cc19c
PC
535 png_ptr->zstream.avail_out =
536 (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
537 png_ptr->iwidth) + 1);
9c0d9ce3 538
0272a10d
VZ
539 do
540 {
541 if (!(png_ptr->zstream.avail_in))
542 {
543 while (!png_ptr->idat_size)
544 {
0272a10d
VZ
545 png_crc_finish(png_ptr, 0);
546
970f6abe 547 png_ptr->idat_size = png_read_chunk_header(png_ptr);
9c0d9ce3 548 if (png_ptr->chunk_name != png_IDAT)
0272a10d
VZ
549 png_error(png_ptr, "Not enough image data");
550 }
551 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
552 png_ptr->zstream.next_in = png_ptr->zbuf;
553 if (png_ptr->zbuf_size > png_ptr->idat_size)
554 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
555 png_crc_read(png_ptr, png_ptr->zbuf,
9c0d9ce3 556 (png_size_t)png_ptr->zstream.avail_in);
0272a10d
VZ
557 png_ptr->idat_size -= png_ptr->zstream.avail_in;
558 }
9c0d9ce3 559
0272a10d 560 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
9c0d9ce3 561
0272a10d
VZ
562 if (ret == Z_STREAM_END)
563 {
564 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
565 png_ptr->idat_size)
b61cc19c 566 png_benign_error(png_ptr, "Extra compressed data");
0272a10d
VZ
567 png_ptr->mode |= PNG_AFTER_IDAT;
568 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
569 break;
570 }
9c0d9ce3 571
0272a10d
VZ
572 if (ret != Z_OK)
573 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
9c0d9ce3 574 "Decompression error");
0272a10d
VZ
575
576 } while (png_ptr->zstream.avail_out);
577
9c0d9ce3
DS
578 if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
579 {
580 if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
581 png_read_filter_row(&row_info, png_ptr->row_buf + 1,
582 png_ptr->prev_row + 1, png_ptr->row_buf[0]);
583 else
584 png_error(png_ptr, "bad adaptive filter value");
585 }
0272a10d 586
9c0d9ce3
DS
587 /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
588 * 1.5.6, while the buffer really is this big in current versions of libpng
589 * it may not be in the future, so this was changed just to copy the
590 * interlaced count:
591 */
592 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
0272a10d 593
b61cc19c 594#ifdef PNG_MNG_FEATURES_SUPPORTED
970f6abe 595 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
9c0d9ce3 596 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
0272a10d
VZ
597 {
598 /* Intrapixel differencing */
9c0d9ce3 599 png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
0272a10d
VZ
600 }
601#endif
602
603
9c0d9ce3
DS
604#ifdef PNG_READ_TRANSFORMS_SUPPORTED
605 if (png_ptr->transformations)
606 png_do_read_transformations(png_ptr, &row_info);
607#endif
608
609 /* The transformed pixel depth should match the depth now in row_info. */
610 if (png_ptr->transformed_pixel_depth == 0)
611 {
612 png_ptr->transformed_pixel_depth = row_info.pixel_depth;
613 if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
614 png_error(png_ptr, "sequential row overflow");
615 }
616
617 else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
618 png_error(png_ptr, "internal sequential row size calculation error");
0272a10d 619
b61cc19c
PC
620#ifdef PNG_READ_INTERLACING_SUPPORTED
621 /* Blow up interlaced rows to full size */
0272a10d
VZ
622 if (png_ptr->interlaced &&
623 (png_ptr->transformations & PNG_INTERLACE))
624 {
625 if (png_ptr->pass < 6)
9c0d9ce3
DS
626 png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
627 png_ptr->transformations);
0272a10d
VZ
628
629 if (dsp_row != NULL)
9c0d9ce3
DS
630 png_combine_row(png_ptr, dsp_row, 1/*display*/);
631
0272a10d 632 if (row != NULL)
9c0d9ce3 633 png_combine_row(png_ptr, row, 0/*row*/);
0272a10d 634 }
9c0d9ce3 635
0272a10d
VZ
636 else
637#endif
638 {
639 if (row != NULL)
9c0d9ce3
DS
640 png_combine_row(png_ptr, row, -1/*ignored*/);
641
0272a10d 642 if (dsp_row != NULL)
9c0d9ce3 643 png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
0272a10d
VZ
644 }
645 png_read_finish_row(png_ptr);
646
647 if (png_ptr->read_row_fn != NULL)
648 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
649}
b61cc19c 650#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 651
b61cc19c 652#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
653/* Read one or more rows of image data. If the image is interlaced,
654 * and png_set_interlace_handling() has been called, the rows need to
655 * contain the contents of the rows from the previous pass. If the
656 * image has alpha or transparency, and png_handle_alpha()[*] has been
657 * called, the rows contents must be initialized to the contents of the
658 * screen.
659 *
660 * "row" holds the actual image, and pixels are placed in it
661 * as they arrive. If the image is displayed after each pass, it will
662 * appear to "sparkle" in. "display_row" can be used to display a
663 * "chunky" progressive image, with finer detail added as it becomes
664 * available. If you do not want this "chunky" display, you may pass
665 * NULL for display_row. If you do not want the sparkle display, and
666 * you have not called png_handle_alpha(), you may pass NULL for rows.
667 * If you have called png_handle_alpha(), and the image has either an
668 * alpha channel or a transparency chunk, you must provide a buffer for
669 * rows. In this case, you do not have to provide a display_row buffer
670 * also, but you may. If the image is not interlaced, or if you have
671 * not called png_set_interlace_handling(), the display_row buffer will
672 * be ignored, so pass NULL to it.
673 *
674 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
675 */
676
677void PNGAPI
678png_read_rows(png_structp png_ptr, png_bytepp row,
9c0d9ce3 679 png_bytepp display_row, png_uint_32 num_rows)
0272a10d
VZ
680{
681 png_uint_32 i;
682 png_bytepp rp;
683 png_bytepp dp;
684
970f6abe 685 png_debug(1, "in png_read_rows");
9c0d9ce3 686
b61cc19c
PC
687 if (png_ptr == NULL)
688 return;
9c0d9ce3 689
0272a10d
VZ
690 rp = row;
691 dp = display_row;
692 if (rp != NULL && dp != NULL)
693 for (i = 0; i < num_rows; i++)
694 {
695 png_bytep rptr = *rp++;
696 png_bytep dptr = *dp++;
697
698 png_read_row(png_ptr, rptr, dptr);
699 }
9c0d9ce3 700
970f6abe 701 else if (rp != NULL)
0272a10d
VZ
702 for (i = 0; i < num_rows; i++)
703 {
704 png_bytep rptr = *rp;
b61cc19c 705 png_read_row(png_ptr, rptr, NULL);
0272a10d
VZ
706 rp++;
707 }
9c0d9ce3 708
970f6abe 709 else if (dp != NULL)
0272a10d
VZ
710 for (i = 0; i < num_rows; i++)
711 {
712 png_bytep dptr = *dp;
b61cc19c 713 png_read_row(png_ptr, NULL, dptr);
0272a10d
VZ
714 dp++;
715 }
716}
b61cc19c 717#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 718
b61cc19c 719#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
720/* Read the entire image. If the image has an alpha channel or a tRNS
721 * chunk, and you have called png_handle_alpha()[*], you will need to
722 * initialize the image to the current image that PNG will be overlaying.
723 * We set the num_rows again here, in case it was incorrectly set in
724 * png_read_start_row() by a call to png_read_update_info() or
725 * png_start_read_image() if png_set_interlace_handling() wasn't called
726 * prior to either of these functions like it should have been. You can
727 * only call this function once. If you desire to have an image for
728 * each pass of a interlaced image, use png_read_rows() instead.
729 *
730 * [*] png_handle_alpha() does not exist yet, as of this version of libpng
731 */
732void PNGAPI
733png_read_image(png_structp png_ptr, png_bytepp image)
734{
970f6abe 735 png_uint_32 i, image_height;
0272a10d
VZ
736 int pass, j;
737 png_bytepp rp;
738
970f6abe 739 png_debug(1, "in png_read_image");
9c0d9ce3 740
b61cc19c
PC
741 if (png_ptr == NULL)
742 return;
0272a10d
VZ
743
744#ifdef PNG_READ_INTERLACING_SUPPORTED
9c0d9ce3
DS
745 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
746 {
747 pass = png_set_interlace_handling(png_ptr);
748 /* And make sure transforms are initialized. */
749 png_start_read_image(png_ptr);
750 }
751 else
752 {
753 if (png_ptr->interlaced && !(png_ptr->transformations & PNG_INTERLACE))
754 {
755 /* Caller called png_start_read_image or png_read_update_info without
756 * first turning on the PNG_INTERLACE transform. We can fix this here,
757 * but the caller should do it!
758 */
759 png_warning(png_ptr, "Interlace handling should be turned on when "
760 "using png_read_image");
761 /* Make sure this is set correctly */
762 png_ptr->num_rows = png_ptr->height;
763 }
764
765 /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
766 * the above error case.
767 */
768 pass = png_set_interlace_handling(png_ptr);
769 }
0272a10d
VZ
770#else
771 if (png_ptr->interlaced)
772 png_error(png_ptr,
9c0d9ce3
DS
773 "Cannot read interlaced image -- interlace handler disabled");
774
0272a10d
VZ
775 pass = 1;
776#endif
777
0272a10d 778 image_height=png_ptr->height;
0272a10d
VZ
779
780 for (j = 0; j < pass; j++)
781 {
782 rp = image;
783 for (i = 0; i < image_height; i++)
784 {
b61cc19c 785 png_read_row(png_ptr, *rp, NULL);
0272a10d
VZ
786 rp++;
787 }
788 }
789}
b61cc19c 790#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 791
b61cc19c 792#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
0272a10d
VZ
793/* Read the end of the PNG file. Will not read past the end of the
794 * file, will verify the end is accurate, and will read any comments
795 * or time information at the end of the file, if info is not NULL.
796 */
797void PNGAPI
798png_read_end(png_structp png_ptr, png_infop info_ptr)
799{
970f6abe 800 png_debug(1, "in png_read_end");
9c0d9ce3 801
b61cc19c
PC
802 if (png_ptr == NULL)
803 return;
9c0d9ce3 804
0272a10d
VZ
805 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
806
807 do
808 {
970f6abe 809 png_uint_32 length = png_read_chunk_header(png_ptr);
9c0d9ce3 810 png_uint_32 chunk_name = png_ptr->chunk_name;
0272a10d 811
9c0d9ce3 812 if (chunk_name == png_IHDR)
0272a10d 813 png_handle_IHDR(png_ptr, info_ptr, length);
9c0d9ce3
DS
814
815 else if (chunk_name == png_IEND)
0272a10d 816 png_handle_IEND(png_ptr, info_ptr, length);
9c0d9ce3 817
0272a10d 818#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
9c0d9ce3
DS
819 else if (png_chunk_unknown_handling(png_ptr, chunk_name) !=
820 PNG_HANDLE_CHUNK_AS_DEFAULT)
0272a10d 821 {
9c0d9ce3 822 if (chunk_name == png_IDAT)
0272a10d
VZ
823 {
824 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
b61cc19c 825 png_benign_error(png_ptr, "Too many IDATs found");
0272a10d
VZ
826 }
827 png_handle_unknown(png_ptr, info_ptr, length);
9c0d9ce3 828 if (chunk_name == png_PLTE)
0272a10d
VZ
829 png_ptr->mode |= PNG_HAVE_PLTE;
830 }
831#endif
9c0d9ce3
DS
832
833 else if (chunk_name == png_IDAT)
0272a10d
VZ
834 {
835 /* Zero length IDATs are legal after the last IDAT has been
836 * read, but not after other chunks have been read.
837 */
838 if ((length > 0) || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT))
b61cc19c 839 png_benign_error(png_ptr, "Too many IDATs found");
9c0d9ce3 840
0272a10d
VZ
841 png_crc_finish(png_ptr, length);
842 }
9c0d9ce3 843 else if (chunk_name == png_PLTE)
0272a10d 844 png_handle_PLTE(png_ptr, info_ptr, length);
9c0d9ce3 845
b61cc19c 846#ifdef PNG_READ_bKGD_SUPPORTED
9c0d9ce3 847 else if (chunk_name == png_bKGD)
0272a10d
VZ
848 png_handle_bKGD(png_ptr, info_ptr, length);
849#endif
9c0d9ce3 850
b61cc19c 851#ifdef PNG_READ_cHRM_SUPPORTED
9c0d9ce3 852 else if (chunk_name == png_cHRM)
0272a10d
VZ
853 png_handle_cHRM(png_ptr, info_ptr, length);
854#endif
9c0d9ce3 855
b61cc19c 856#ifdef PNG_READ_gAMA_SUPPORTED
9c0d9ce3 857 else if (chunk_name == png_gAMA)
0272a10d
VZ
858 png_handle_gAMA(png_ptr, info_ptr, length);
859#endif
9c0d9ce3 860
b61cc19c 861#ifdef PNG_READ_hIST_SUPPORTED
9c0d9ce3 862 else if (chunk_name == png_hIST)
0272a10d
VZ
863 png_handle_hIST(png_ptr, info_ptr, length);
864#endif
9c0d9ce3 865
b61cc19c 866#ifdef PNG_READ_oFFs_SUPPORTED
9c0d9ce3 867 else if (chunk_name == png_oFFs)
0272a10d
VZ
868 png_handle_oFFs(png_ptr, info_ptr, length);
869#endif
9c0d9ce3 870
b61cc19c 871#ifdef PNG_READ_pCAL_SUPPORTED
9c0d9ce3 872 else if (chunk_name == png_pCAL)
0272a10d
VZ
873 png_handle_pCAL(png_ptr, info_ptr, length);
874#endif
9c0d9ce3 875
b61cc19c 876#ifdef PNG_READ_sCAL_SUPPORTED
9c0d9ce3 877 else if (chunk_name == png_sCAL)
0272a10d
VZ
878 png_handle_sCAL(png_ptr, info_ptr, length);
879#endif
9c0d9ce3 880
b61cc19c 881#ifdef PNG_READ_pHYs_SUPPORTED
9c0d9ce3 882 else if (chunk_name == png_pHYs)
0272a10d
VZ
883 png_handle_pHYs(png_ptr, info_ptr, length);
884#endif
9c0d9ce3 885
b61cc19c 886#ifdef PNG_READ_sBIT_SUPPORTED
9c0d9ce3 887 else if (chunk_name == png_sBIT)
0272a10d
VZ
888 png_handle_sBIT(png_ptr, info_ptr, length);
889#endif
9c0d9ce3 890
b61cc19c 891#ifdef PNG_READ_sRGB_SUPPORTED
9c0d9ce3 892 else if (chunk_name == png_sRGB)
0272a10d
VZ
893 png_handle_sRGB(png_ptr, info_ptr, length);
894#endif
9c0d9ce3 895
b61cc19c 896#ifdef PNG_READ_iCCP_SUPPORTED
9c0d9ce3 897 else if (chunk_name == png_iCCP)
0272a10d
VZ
898 png_handle_iCCP(png_ptr, info_ptr, length);
899#endif
9c0d9ce3 900
b61cc19c 901#ifdef PNG_READ_sPLT_SUPPORTED
9c0d9ce3 902 else if (chunk_name == png_sPLT)
0272a10d
VZ
903 png_handle_sPLT(png_ptr, info_ptr, length);
904#endif
9c0d9ce3 905
b61cc19c 906#ifdef PNG_READ_tEXt_SUPPORTED
9c0d9ce3 907 else if (chunk_name == png_tEXt)
0272a10d
VZ
908 png_handle_tEXt(png_ptr, info_ptr, length);
909#endif
9c0d9ce3 910
b61cc19c 911#ifdef PNG_READ_tIME_SUPPORTED
9c0d9ce3 912 else if (chunk_name == png_tIME)
0272a10d
VZ
913 png_handle_tIME(png_ptr, info_ptr, length);
914#endif
9c0d9ce3 915
b61cc19c 916#ifdef PNG_READ_tRNS_SUPPORTED
9c0d9ce3 917 else if (chunk_name == png_tRNS)
0272a10d
VZ
918 png_handle_tRNS(png_ptr, info_ptr, length);
919#endif
9c0d9ce3 920
b61cc19c 921#ifdef PNG_READ_zTXt_SUPPORTED
9c0d9ce3 922 else if (chunk_name == png_zTXt)
0272a10d
VZ
923 png_handle_zTXt(png_ptr, info_ptr, length);
924#endif
9c0d9ce3 925
b61cc19c 926#ifdef PNG_READ_iTXt_SUPPORTED
9c0d9ce3 927 else if (chunk_name == png_iTXt)
0272a10d
VZ
928 png_handle_iTXt(png_ptr, info_ptr, length);
929#endif
9c0d9ce3 930
0272a10d
VZ
931 else
932 png_handle_unknown(png_ptr, info_ptr, length);
933 } while (!(png_ptr->mode & PNG_HAVE_IEND));
934}
b61cc19c 935#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 936
b61cc19c 937/* Free all memory used by the read */
0272a10d
VZ
938void PNGAPI
939png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
9c0d9ce3 940 png_infopp end_info_ptr_ptr)
0272a10d
VZ
941{
942 png_structp png_ptr = NULL;
943 png_infop info_ptr = NULL, end_info_ptr = NULL;
944#ifdef PNG_USER_MEM_SUPPORTED
970f6abe
VZ
945 png_free_ptr free_fn = NULL;
946 png_voidp mem_ptr = NULL;
0272a10d
VZ
947#endif
948
970f6abe 949 png_debug(1, "in png_destroy_read_struct");
9c0d9ce3 950
0272a10d
VZ
951 if (png_ptr_ptr != NULL)
952 png_ptr = *png_ptr_ptr;
970f6abe
VZ
953 if (png_ptr == NULL)
954 return;
955
956#ifdef PNG_USER_MEM_SUPPORTED
957 free_fn = png_ptr->free_fn;
958 mem_ptr = png_ptr->mem_ptr;
959#endif
0272a10d
VZ
960
961 if (info_ptr_ptr != NULL)
962 info_ptr = *info_ptr_ptr;
963
964 if (end_info_ptr_ptr != NULL)
965 end_info_ptr = *end_info_ptr_ptr;
966
0272a10d
VZ
967 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
968
969 if (info_ptr != NULL)
970 {
b61cc19c 971#ifdef PNG_TEXT_SUPPORTED
0272a10d
VZ
972 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
973#endif
974
975#ifdef PNG_USER_MEM_SUPPORTED
976 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
977 (png_voidp)mem_ptr);
978#else
979 png_destroy_struct((png_voidp)info_ptr);
980#endif
981 *info_ptr_ptr = NULL;
982 }
983
984 if (end_info_ptr != NULL)
985 {
b61cc19c 986#ifdef PNG_READ_TEXT_SUPPORTED
0272a10d
VZ
987 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
988#endif
989#ifdef PNG_USER_MEM_SUPPORTED
990 png_destroy_struct_2((png_voidp)end_info_ptr, (png_free_ptr)free_fn,
9c0d9ce3 991 (png_voidp)mem_ptr);
0272a10d
VZ
992#else
993 png_destroy_struct((png_voidp)end_info_ptr);
994#endif
995 *end_info_ptr_ptr = NULL;
996 }
997
998 if (png_ptr != NULL)
999 {
1000#ifdef PNG_USER_MEM_SUPPORTED
1001 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1002 (png_voidp)mem_ptr);
1003#else
1004 png_destroy_struct((png_voidp)png_ptr);
1005#endif
1006 *png_ptr_ptr = NULL;
1007 }
1008}
1009
b61cc19c 1010/* Free all memory used by the read (old method) */
0272a10d 1011void /* PRIVATE */
b61cc19c
PC
1012png_read_destroy(png_structp png_ptr, png_infop info_ptr,
1013 png_infop end_info_ptr)
0272a10d
VZ
1014{
1015#ifdef PNG_SETJMP_SUPPORTED
1016 jmp_buf tmp_jmp;
1017#endif
1018 png_error_ptr error_fn;
9c0d9ce3 1019#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 1020 png_error_ptr warning_fn;
9c0d9ce3 1021#endif
0272a10d
VZ
1022 png_voidp error_ptr;
1023#ifdef PNG_USER_MEM_SUPPORTED
1024 png_free_ptr free_fn;
1025#endif
1026
970f6abe 1027 png_debug(1, "in png_read_destroy");
9c0d9ce3 1028
0272a10d
VZ
1029 if (info_ptr != NULL)
1030 png_info_destroy(png_ptr, info_ptr);
1031
1032 if (end_info_ptr != NULL)
1033 png_info_destroy(png_ptr, end_info_ptr);
1034
9c0d9ce3
DS
1035#ifdef PNG_READ_GAMMA_SUPPORTED
1036 png_destroy_gamma_table(png_ptr);
1037#endif
1038
0272a10d
VZ
1039 png_free(png_ptr, png_ptr->zbuf);
1040 png_free(png_ptr, png_ptr->big_row_buf);
9c0d9ce3 1041 png_free(png_ptr, png_ptr->big_prev_row);
970f6abe 1042 png_free(png_ptr, png_ptr->chunkdata);
9c0d9ce3 1043
b61cc19c 1044#ifdef PNG_READ_QUANTIZE_SUPPORTED
0272a10d 1045 png_free(png_ptr, png_ptr->palette_lookup);
b61cc19c 1046 png_free(png_ptr, png_ptr->quantize_index);
0272a10d 1047#endif
9c0d9ce3 1048
0272a10d
VZ
1049 if (png_ptr->free_me & PNG_FREE_PLTE)
1050 png_zfree(png_ptr, png_ptr->palette);
1051 png_ptr->free_me &= ~PNG_FREE_PLTE;
9c0d9ce3 1052
0272a10d
VZ
1053#if defined(PNG_tRNS_SUPPORTED) || \
1054 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
0272a10d 1055 if (png_ptr->free_me & PNG_FREE_TRNS)
b61cc19c 1056 png_free(png_ptr, png_ptr->trans_alpha);
0272a10d 1057 png_ptr->free_me &= ~PNG_FREE_TRNS;
0272a10d 1058#endif
9c0d9ce3 1059
b61cc19c 1060#ifdef PNG_READ_hIST_SUPPORTED
0272a10d
VZ
1061 if (png_ptr->free_me & PNG_FREE_HIST)
1062 png_free(png_ptr, png_ptr->hist);
1063 png_ptr->free_me &= ~PNG_FREE_HIST;
0272a10d 1064#endif
0272a10d
VZ
1065
1066 inflateEnd(&png_ptr->zstream);
9c0d9ce3 1067
0272a10d
VZ
1068#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1069 png_free(png_ptr, png_ptr->save_buffer);
1070#endif
1071
1072#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1073#ifdef PNG_TEXT_SUPPORTED
1074 png_free(png_ptr, png_ptr->current_text);
1075#endif /* PNG_TEXT_SUPPORTED */
1076#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
1077
1078 /* Save the important info out of the png_struct, in case it is
1079 * being used again.
1080 */
1081#ifdef PNG_SETJMP_SUPPORTED
9c0d9ce3 1082 png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
0272a10d
VZ
1083#endif
1084
1085 error_fn = png_ptr->error_fn;
9c0d9ce3 1086#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 1087 warning_fn = png_ptr->warning_fn;
9c0d9ce3 1088#endif
0272a10d
VZ
1089 error_ptr = png_ptr->error_ptr;
1090#ifdef PNG_USER_MEM_SUPPORTED
1091 free_fn = png_ptr->free_fn;
1092#endif
1093
970f6abe 1094 png_memset(png_ptr, 0, png_sizeof(png_struct));
0272a10d
VZ
1095
1096 png_ptr->error_fn = error_fn;
9c0d9ce3 1097#ifdef PNG_WARNINGS_SUPPORTED
0272a10d 1098 png_ptr->warning_fn = warning_fn;
9c0d9ce3 1099#endif
0272a10d
VZ
1100 png_ptr->error_ptr = error_ptr;
1101#ifdef PNG_USER_MEM_SUPPORTED
1102 png_ptr->free_fn = free_fn;
1103#endif
1104
1105#ifdef PNG_SETJMP_SUPPORTED
9c0d9ce3 1106 png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
0272a10d
VZ
1107#endif
1108
1109}
1110
1111void PNGAPI
1112png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1113{
b61cc19c
PC
1114 if (png_ptr == NULL)
1115 return;
9c0d9ce3 1116
0272a10d
VZ
1117 png_ptr->read_row_fn = read_row_fn;
1118}
1119
1120
b61cc19c
PC
1121#ifdef PNG_SEQUENTIAL_READ_SUPPORTED
1122#ifdef PNG_INFO_IMAGE_SUPPORTED
0272a10d
VZ
1123void PNGAPI
1124png_read_png(png_structp png_ptr, png_infop info_ptr,
1125 int transforms,
1126 voidp params)
1127{
1128 int row;
1129
9c0d9ce3 1130 if (png_ptr == NULL || info_ptr == NULL)
b61cc19c 1131 return;
0272a10d
VZ
1132
1133 /* png_read_info() gives us all of the information from the
1134 * PNG file before the first IDAT (image data chunk).
1135 */
1136 png_read_info(png_ptr, info_ptr);
1137 if (info_ptr->height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
970f6abe 1138 png_error(png_ptr, "Image is too high to process with png_read_png()");
0272a10d
VZ
1139
1140 /* -------------- image transformations start here ------------------- */
1141
9c0d9ce3
DS
1142#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1143 /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
1144 */
1145 if (transforms & PNG_TRANSFORM_SCALE_16)
1146 {
1147 /* Added at libpng-1.5.4. "strip_16" produces the same result that it
1148 * did in earlier versions, while "scale_16" is now more accurate.
1149 */
1150 png_set_scale_16(png_ptr);
1151 }
1152#endif
1153
1154#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1155 /* If both SCALE and STRIP are required pngrtran will effectively cancel the
1156 * latter by doing SCALE first. This is ok and allows apps not to check for
1157 * which is supported to get the right answer.
0272a10d
VZ
1158 */
1159 if (transforms & PNG_TRANSFORM_STRIP_16)
b61cc19c 1160 png_set_strip_16(png_ptr);
0272a10d
VZ
1161#endif
1162
b61cc19c 1163#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
0272a10d
VZ
1164 /* Strip alpha bytes from the input data without combining with
1165 * the background (not recommended).
1166 */
1167 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
b61cc19c 1168 png_set_strip_alpha(png_ptr);
0272a10d
VZ
1169#endif
1170
1171#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1172 /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
1173 * byte into separate bytes (useful for paletted and grayscale images).
1174 */
1175 if (transforms & PNG_TRANSFORM_PACKING)
b61cc19c 1176 png_set_packing(png_ptr);
0272a10d
VZ
1177#endif
1178
b61cc19c 1179#ifdef PNG_READ_PACKSWAP_SUPPORTED
0272a10d
VZ
1180 /* Change the order of packed pixels to least significant bit first
1181 * (not useful if you are using png_set_packing).
1182 */
1183 if (transforms & PNG_TRANSFORM_PACKSWAP)
b61cc19c 1184 png_set_packswap(png_ptr);
0272a10d
VZ
1185#endif
1186
b61cc19c 1187#ifdef PNG_READ_EXPAND_SUPPORTED
0272a10d
VZ
1188 /* Expand paletted colors into true RGB triplets
1189 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1190 * Expand paletted or RGB images with transparency to full alpha
1191 * channels so the data will be available as RGBA quartets.
1192 */
1193 if (transforms & PNG_TRANSFORM_EXPAND)
b61cc19c
PC
1194 if ((png_ptr->bit_depth < 8) ||
1195 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1196 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
0272a10d
VZ
1197 png_set_expand(png_ptr);
1198#endif
1199
b61cc19c 1200 /* We don't handle background color or gamma transformation or quantizing.
0272a10d
VZ
1201 */
1202
b61cc19c
PC
1203#ifdef PNG_READ_INVERT_SUPPORTED
1204 /* Invert monochrome files to have 0 as white and 1 as black
0272a10d
VZ
1205 */
1206 if (transforms & PNG_TRANSFORM_INVERT_MONO)
b61cc19c 1207 png_set_invert_mono(png_ptr);
0272a10d
VZ
1208#endif
1209
b61cc19c 1210#ifdef PNG_READ_SHIFT_SUPPORTED
0272a10d
VZ
1211 /* If you want to shift the pixel values from the range [0,255] or
1212 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1213 * colors were originally in:
1214 */
1215 if ((transforms & PNG_TRANSFORM_SHIFT)
1216 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1217 {
1218 png_color_8p sig_bit;
1219
1220 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1221 png_set_shift(png_ptr, sig_bit);
1222 }
1223#endif
1224
b61cc19c 1225#ifdef PNG_READ_BGR_SUPPORTED
9c0d9ce3 1226 /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
0272a10d 1227 if (transforms & PNG_TRANSFORM_BGR)
b61cc19c 1228 png_set_bgr(png_ptr);
0272a10d
VZ
1229#endif
1230
b61cc19c 1231#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
9c0d9ce3 1232 /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
0272a10d 1233 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
9c0d9ce3 1234 png_set_swap_alpha(png_ptr);
0272a10d
VZ
1235#endif
1236
b61cc19c 1237#ifdef PNG_READ_SWAP_SUPPORTED
9c0d9ce3 1238 /* Swap bytes of 16-bit files to least significant byte first */
0272a10d 1239 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
b61cc19c
PC
1240 png_set_swap(png_ptr);
1241#endif
1242
1243/* Added at libpng-1.2.41 */
1244#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
9c0d9ce3 1245 /* Invert the alpha channel from opacity to transparency */
b61cc19c 1246 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
9c0d9ce3 1247 png_set_invert_alpha(png_ptr);
b61cc19c
PC
1248#endif
1249
1250/* Added at libpng-1.2.41 */
1251#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
9c0d9ce3 1252 /* Expand grayscale image to RGB */
b61cc19c 1253 if (transforms & PNG_TRANSFORM_GRAY_TO_RGB)
9c0d9ce3
DS
1254 png_set_gray_to_rgb(png_ptr);
1255#endif
1256
1257/* Added at libpng-1.5.4 */
1258#ifdef PNG_READ_EXPAND_16_SUPPORTED
1259 if (transforms & PNG_TRANSFORM_EXPAND_16)
1260 png_set_expand_16(png_ptr);
0272a10d
VZ
1261#endif
1262
1263 /* We don't handle adding filler bytes */
1264
9c0d9ce3
DS
1265 /* We use png_read_image and rely on that for interlace handling, but we also
1266 * call png_read_update_info therefore must turn on interlace handling now:
1267 */
1268 (void)png_set_interlace_handling(png_ptr);
1269
0272a10d
VZ
1270 /* Optional call to gamma correct and add the background to the palette
1271 * and update info structure. REQUIRED if you are expecting libpng to
1272 * update the palette for you (i.e., you selected such a transform above).
1273 */
1274 png_read_update_info(png_ptr, info_ptr);
1275
1276 /* -------------- image transformations end here ------------------- */
1277
0272a10d 1278 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
970f6abe 1279 if (info_ptr->row_pointers == NULL)
0272a10d 1280 {
9c0d9ce3 1281 png_uint_32 iptr;
b61cc19c 1282
0272a10d 1283 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
9c0d9ce3 1284 info_ptr->height * png_sizeof(png_bytep));
b61cc19c
PC
1285 for (iptr=0; iptr<info_ptr->height; iptr++)
1286 info_ptr->row_pointers[iptr] = NULL;
1287
0272a10d 1288 info_ptr->free_me |= PNG_FREE_ROWS;
b61cc19c 1289
0272a10d 1290 for (row = 0; row < (int)info_ptr->height; row++)
0272a10d
VZ
1291 info_ptr->row_pointers[row] = (png_bytep)png_malloc(png_ptr,
1292 png_get_rowbytes(png_ptr, info_ptr));
0272a10d
VZ
1293 }
1294
1295 png_read_image(png_ptr, info_ptr->row_pointers);
1296 info_ptr->valid |= PNG_INFO_IDAT;
1297
b61cc19c 1298 /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
0272a10d
VZ
1299 png_read_end(png_ptr, info_ptr);
1300
9c0d9ce3
DS
1301 PNG_UNUSED(transforms) /* Quiet compiler warnings */
1302 PNG_UNUSED(params)
0272a10d
VZ
1303
1304}
1305#endif /* PNG_INFO_IMAGE_SUPPORTED */
b61cc19c 1306#endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
0272a10d 1307#endif /* PNG_READ_SUPPORTED */