blob: 597d9d522fdcf2e57c8e9fb689485b1e9eddbefd [file] [log] [blame]
Guy Schalnat0d580581995-07-20 02:43:20 -05001
Andreas Dilger47a0c421997-05-16 02:46:07 -05002/* pngread.c - read a PNG file
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06003 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06004 * libpng 1.0.2a - December 29, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06005 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
Glenn Randers-Pehrson2687fcc1998-01-07 20:54:20 -06008 * Copyright (c) 1998, Glenn Randers-Pehrson
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -06009 *
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
12 */
Guy Schalnat0d580581995-07-20 02:43:20 -050013
14#define PNG_INTERNAL
15#include "png.h"
16
Andreas Dilger47a0c421997-05-16 02:46:07 -050017/* Create a PNG structure for reading, and allocate any memory needed. */
Guy Schalnate5a37791996-06-05 15:50:50 -050018png_structp
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050019png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060020 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050021{
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050022
23#ifdef PNG_USER_MEM_SUPPORTED
24 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
25 warn_fn, NULL, NULL, NULL));
26}
27
28/* Alternate create PNG structure for reading, and allocate any memory needed. */
29png_structp
30png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
31 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
32 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
33{
34#endif /* PNG_USER_MEM_SUPPORTED */
35
Guy Schalnate5a37791996-06-05 15:50:50 -050036 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060037#ifdef USE_FAR_KEYWORD
38 jmp_buf jmpbuf;
39#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050040 png_debug(1, "in png_create_read_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050041#ifdef PNG_USER_MEM_SUPPORTED
42 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
43 (png_malloc_ptr)malloc_fn)) == NULL)
44#else
Guy Schalnate5a37791996-06-05 15:50:50 -050045 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050046#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050047 {
48 return (png_structp)NULL;
49 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060050#ifdef USE_FAR_KEYWORD
51 if (setjmp(jmpbuf))
52#else
Guy Schalnate5a37791996-06-05 15:50:50 -050053 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060054#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050055 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060056 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -050057 png_destroy_struct(png_ptr);
58 return (png_structp)NULL;
59 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060#ifdef USE_FAR_KEYWORD
61 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
62#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050063
64#ifdef PNG_USER_MEM_SUPPORTED
65 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
66#endif /* PNG_USER_MEM_SUPPORTED */
67
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060068 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060069
Andreas Dilger47a0c421997-05-16 02:46:07 -050070 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
71 * we must recompile any applications that use any older library version.
72 * For versions after libpng 1.0, we will be compatible, so we need
73 * only check the first digit.
74 */
75 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
76 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -050077 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060078 png_error(png_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050079 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -050080 }
81
82 /* initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -050083 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060084 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
85 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060086 png_ptr->zstream.zalloc = png_zalloc;
87 png_ptr->zstream.zfree = png_zfree;
88 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050089
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060090 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -050091 {
92 case Z_OK: /* Do nothing */ break;
93 case Z_MEM_ERROR:
94 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
95 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
96 default: png_error(png_ptr, "Unknown zlib error");
97 }
98
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060099 png_ptr->zstream.next_out = png_ptr->zbuf;
100 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500101
102 png_set_read_fn(png_ptr, NULL, NULL);
103
Guy Schalnate5a37791996-06-05 15:50:50 -0500104 return (png_ptr);
105}
106
Andreas Dilger47a0c421997-05-16 02:46:07 -0500107/* Initialize PNG structure for reading, and allocate any memory needed.
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500108 This interface is deprecated in favour of the png_create_read_struct(),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600109 and it will eventually disappear. */
Guy Schalnate5a37791996-06-05 15:50:50 -0500110void
111png_read_init(png_structp png_ptr)
112{
113 jmp_buf tmp_jmp; /* to save current jump buffer */
114
Andreas Dilger47a0c421997-05-16 02:46:07 -0500115 png_debug(1, "in png_read_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -0500116 /* save jump buffer and error functions */
117 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
118
119 /* reset all variables to 0 */
120 png_memset(png_ptr, 0, sizeof (png_struct));
121
122 /* restore jump buffer */
123 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
124
125 /* initialize zbuf - compression buffer */
126 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600127 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
128 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600129 png_ptr->zstream.zalloc = png_zalloc;
130 png_ptr->zstream.zfree = png_zfree;
131 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500132
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600133 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -0500134 {
135 case Z_OK: /* Do nothing */ break;
136 case Z_MEM_ERROR:
137 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
138 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
139 default: png_error(png_ptr, "Unknown zlib error");
140 }
141
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600142 png_ptr->zstream.next_out = png_ptr->zbuf;
143 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500144
145 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500146}
147
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600148/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600149 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600150 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500151 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600152 * via png_set_sig_bytes(), and we will only check the remaining bytes
153 * here. The application can then have access to the signature bytes we
154 * read if it is determined that this isn't a valid PNG file.
155 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500156void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600157png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500159 png_debug(1, "in png_read_info\n");
160 /* save jump buffer and error functions */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600161 /* If we haven't checked all of the PNG signature bytes, do so now. */
162 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500163 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500164 png_size_t num_checked = png_ptr->sig_bytes,
165 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600166
Andreas Dilger47a0c421997-05-16 02:46:07 -0500167 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168 png_ptr->sig_bytes = 8;
169
170 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
171 {
172 if (num_checked < 4 &&
173 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
174 png_error(png_ptr, "Not a PNG file");
175 else
176 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
177 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500178 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500179
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600180 for(;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500181 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600182 png_byte chunk_length[4];
183 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500184
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600185 png_read_data(png_ptr, chunk_length, 4);
186 length = png_get_uint_32(chunk_length);
187
Guy Schalnat0d580581995-07-20 02:43:20 -0500188 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600189 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
190
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
192
193 /* This should be a binary subdivision search or a hash for
194 * matching the chunk name rather than a linear search.
195 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600196 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
197 png_handle_IHDR(png_ptr, info_ptr, length);
198 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
199 png_handle_PLTE(png_ptr, info_ptr, length);
200 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
201 png_handle_IEND(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600202 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500203 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500204 if (!(png_ptr->mode & PNG_HAVE_IHDR))
205 png_error(png_ptr, "Missing IHDR before IDAT");
206 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
207 !(png_ptr->mode & PNG_HAVE_PLTE))
208 png_error(png_ptr, "Missing PLTE before IDAT");
209
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500210 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500211 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500212 break;
213 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500214#if defined(PNG_READ_bKGD_SUPPORTED)
215 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
216 png_handle_bKGD(png_ptr, info_ptr, length);
217#endif
218#if defined(PNG_READ_cHRM_SUPPORTED)
219 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
220 png_handle_cHRM(png_ptr, info_ptr, length);
221#endif
222#if defined(PNG_READ_gAMA_SUPPORTED)
223 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
224 png_handle_gAMA(png_ptr, info_ptr, length);
225#endif
226#if defined(PNG_READ_hIST_SUPPORTED)
227 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
228 png_handle_hIST(png_ptr, info_ptr, length);
229#endif
230#if defined(PNG_READ_oFFs_SUPPORTED)
231 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
232 png_handle_oFFs(png_ptr, info_ptr, length);
233#endif
234#if defined(PNG_READ_pCAL_SUPPORTED)
235 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
236 png_handle_pCAL(png_ptr, info_ptr, length);
237#endif
238#if defined(PNG_READ_pHYs_SUPPORTED)
239 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
240 png_handle_pHYs(png_ptr, info_ptr, length);
241#endif
242#if defined(PNG_READ_sBIT_SUPPORTED)
243 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
244 png_handle_sBIT(png_ptr, info_ptr, length);
245#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600246#if defined(PNG_READ_sRGB_SUPPORTED)
247 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
248 png_handle_sRGB(png_ptr, info_ptr, length);
249#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500250#if defined(PNG_READ_tEXt_SUPPORTED)
251 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
252 png_handle_tEXt(png_ptr, info_ptr, length);
253#endif
254#if defined(PNG_READ_tIME_SUPPORTED)
255 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
256 png_handle_tIME(png_ptr, info_ptr, length);
257#endif
258#if defined(PNG_READ_tRNS_SUPPORTED)
259 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
260 png_handle_tRNS(png_ptr, info_ptr, length);
261#endif
262#if defined(PNG_READ_zTXt_SUPPORTED)
263 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
264 png_handle_zTXt(png_ptr, info_ptr, length);
265#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500266 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600267 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500268 }
269}
270
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600271/* optional call to update the users info_ptr structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500272void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600273png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500274{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500275 png_debug(1, "in png_read_update_info\n");
276 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500277 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500278 png_read_start_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600279 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500280}
281
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600282/* Initialize palette, background, etc, after transformations
283 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500284 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600285 * If the user doesn't call this, we will do it ourselves.
286 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500287void
Guy Schalnat6d764711995-12-19 03:22:19 -0600288png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500289{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500290 png_debug(1, "in png_start_read_image\n");
291 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500292 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500293 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500294}
295
296void
Guy Schalnat6d764711995-12-19 03:22:19 -0600297png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500298{
299 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500300 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
301 png_ptr->row_number, png_ptr->pass);
302 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500303 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500304 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500305 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
306 {
307 /* check for transforms that have been set but were defined out */
308#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
309 if (png_ptr->transformations & PNG_INVERT_MONO)
310 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
311#endif
312#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
313 if (png_ptr->transformations & PNG_FILLER)
314 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
315#endif
316#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
317 if (png_ptr->transformations & PNG_PACKSWAP)
318 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
319#endif
320#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
321 if (png_ptr->transformations & PNG_PACK)
322 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
323#endif
324#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
325 if (png_ptr->transformations & PNG_SHIFT)
326 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
327#endif
328#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
329 if (png_ptr->transformations & PNG_BGR)
330 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
331#endif
332#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
333 if (png_ptr->transformations & PNG_SWAP_BYTES)
334 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
335#endif
336 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500337
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500338#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500339 /* if interlaced and we do not need a new row, combine row and return */
340 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
341 {
342 switch (png_ptr->pass)
343 {
344 case 0:
345 if (png_ptr->row_number & 7)
346 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500347 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500348 png_combine_row(png_ptr, dsp_row,
349 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600350 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500351 return;
352 }
353 break;
354 case 1:
355 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
356 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500357 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500358 png_combine_row(png_ptr, dsp_row,
359 png_pass_dsp_mask[png_ptr->pass]);
360 png_read_finish_row(png_ptr);
361 return;
362 }
363 break;
364 case 2:
365 if ((png_ptr->row_number & 7) != 4)
366 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500367 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600368 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500369 png_pass_dsp_mask[png_ptr->pass]);
370 png_read_finish_row(png_ptr);
371 return;
372 }
373 break;
374 case 3:
375 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
376 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500378 png_combine_row(png_ptr, dsp_row,
379 png_pass_dsp_mask[png_ptr->pass]);
380 png_read_finish_row(png_ptr);
381 return;
382 }
383 break;
384 case 4:
385 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600386 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500387 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500388 png_combine_row(png_ptr, dsp_row,
389 png_pass_dsp_mask[png_ptr->pass]);
390 png_read_finish_row(png_ptr);
391 return;
392 }
393 break;
394 case 5:
395 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
396 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500397 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500398 png_combine_row(png_ptr, dsp_row,
399 png_pass_dsp_mask[png_ptr->pass]);
400 png_read_finish_row(png_ptr);
401 return;
402 }
403 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600404 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500405 if (!(png_ptr->row_number & 1))
406 {
407 png_read_finish_row(png_ptr);
408 return;
409 }
410 break;
411 }
412 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500413#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500414
Guy Schalnate5a37791996-06-05 15:50:50 -0500415 if (!(png_ptr->mode & PNG_HAVE_IDAT))
416 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500417
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600418 png_ptr->zstream.next_out = png_ptr->row_buf;
419 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500420 do
421 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600422 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500423 {
424 while (!png_ptr->idat_size)
425 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600426 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500427
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600428 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500429
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600430 png_read_data(png_ptr, chunk_length, 4);
431 png_ptr->idat_size = png_get_uint_32(chunk_length);
432
Guy Schalnat0d580581995-07-20 02:43:20 -0500433 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600434 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
435 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
436 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500437 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600438 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
439 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500440 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600441 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500442 png_crc_read(png_ptr, png_ptr->zbuf,
443 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600444 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500445 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600446 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 if (ret == Z_STREAM_END)
448 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600449 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500450 png_ptr->idat_size)
Guy Schalnat6d764711995-12-19 03:22:19 -0600451 png_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600452 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500453 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600454 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500455 }
456 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600457 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500458 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600459
460 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500461
Guy Schalnat0d580581995-07-20 02:43:20 -0500462 png_ptr->row_info.color_type = png_ptr->color_type;
463 png_ptr->row_info.width = png_ptr->iwidth;
464 png_ptr->row_info.channels = png_ptr->channels;
465 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
466 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600467 {
468 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
469 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
470 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500471
Guy Schalnate5a37791996-06-05 15:50:50 -0500472 png_read_filter_row(png_ptr, &(png_ptr->row_info),
473 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
474 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500475
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600476 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600477 png_ptr->rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500478
479 if (png_ptr->transformations)
480 png_do_read_transformations(png_ptr);
481
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500482#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500483 /* blow up interlaced rows to full size */
484 if (png_ptr->interlaced &&
485 (png_ptr->transformations & PNG_INTERLACE))
486 {
487 if (png_ptr->pass < 6)
488 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500490
Andreas Dilger47a0c421997-05-16 02:46:07 -0500491 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500492 png_combine_row(png_ptr, dsp_row,
493 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500494 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500495 png_combine_row(png_ptr, row,
496 png_pass_mask[png_ptr->pass]);
497 }
498 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500501 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500502 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500503 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500504 png_combine_row(png_ptr, dsp_row, 0xff);
505 }
506 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600507
508 if (png_ptr->read_row_fn != NULL)
509 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500510}
511
Andreas Dilger47a0c421997-05-16 02:46:07 -0500512/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600513 * and png_set_interlace_handling() has been called, the rows need to
514 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500515 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600516 * called, the rows contents must be initialized to the contents of the
517 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600518 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600519 * "row" holds the actual image, and pixels are placed in it
520 * as they arrive. If the image is displayed after each pass, it will
521 * appear to "sparkle" in. "display_row" can be used to display a
522 * "chunky" progressive image, with finer detail added as it becomes
523 * available. If you do not want this "chunky" display, you may pass
524 * NULL for display_row. If you do not want the sparkle display, and
525 * you have not called png_handle_alpha(), you may pass NULL for rows.
526 * If you have called png_handle_alpha(), and the image has either an
527 * alpha channel or a transparency chunk, you must provide a buffer for
528 * rows. In this case, you do not have to provide a display_row buffer
529 * also, but you may. If the image is not interlaced, or if you have
530 * not called png_set_interlace_handling(), the display_row buffer will
531 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500532 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600533 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2a.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600534 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600535
Guy Schalnat0d580581995-07-20 02:43:20 -0500536void
Guy Schalnat6d764711995-12-19 03:22:19 -0600537png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600538 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500539{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600540 png_uint_32 i;
541 png_bytepp rp;
542 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500543
Andreas Dilger47a0c421997-05-16 02:46:07 -0500544 png_debug(1, "in png_read_rows\n");
545 /* save jump buffer and error functions */
Guy Schalnat0f716451995-11-28 11:22:13 -0600546 rp = row;
547 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500548 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500549 for (i = 0; i < num_rows; i++)
550 {
551 png_bytep rptr = *rp++;
552 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600553
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500554 png_read_row(png_ptr, rptr, dptr);
555 }
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500556 else if(rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500557 for (i = 0; i < num_rows; i++)
558 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500559 png_bytep rptr = *rp;
560 png_read_row(png_ptr, rptr, NULL);
561 rp++;
562 }
563 else if(dp != NULL)
564 for (i = 0; i < num_rows; i++)
565 {
566 png_bytep dptr = *dp;
567 png_read_row(png_ptr, NULL, dptr);
568 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500569 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500570}
571
Andreas Dilger47a0c421997-05-16 02:46:07 -0500572/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500573 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600574 * initialize the image to the current image that PNG will be overlaying.
575 * We set the num_rows again here, in case it was incorrectly set in
576 * png_read_start_row() by a call to png_read_update_info() or
577 * png_start_read_image() if png_set_interlace_handling() wasn't called
578 * prior to either of these functions like it should have been. You can
579 * only call this function once. If you desire to have an image for
580 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500581 *
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600582 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2a.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600583 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500584void
Guy Schalnat6d764711995-12-19 03:22:19 -0600585png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500586{
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500587 png_uint_32 i,image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500588 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600589 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500590
Andreas Dilger47a0c421997-05-16 02:46:07 -0500591 png_debug(1, "in png_read_image\n");
592 /* save jump buffer and error functions */
Guy Schalnat0d580581995-07-20 02:43:20 -0500593 pass = png_set_interlace_handling(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500594
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500595 image_height=png_ptr->height;
596 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500597
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 for (j = 0; j < pass; j++)
599 {
600 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500601 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500602 {
603 png_read_row(png_ptr, *rp, NULL);
604 rp++;
605 }
606 }
607}
608
Andreas Dilger47a0c421997-05-16 02:46:07 -0500609/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600610 * file, will verify the end is accurate, and will read any comments
611 * or time information at the end of the file, if info is not NULL.
612 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500613void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600614png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500615{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600616 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500617 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500618
Andreas Dilger47a0c421997-05-16 02:46:07 -0500619 png_debug(1, "in png_read_end\n");
620 /* save jump buffer and error functions */
621 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500622
623 do
624 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600625 png_read_data(png_ptr, chunk_length, 4);
626 length = png_get_uint_32(chunk_length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500627
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600628 png_reset_crc(png_ptr);
629 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
630
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
632
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600633 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
634 png_handle_IHDR(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600635 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500636 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500637 /* Zero length IDATs are legal after the last IDAT has been
638 * read, but not after other chunks have been read.
639 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500640 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
641 png_error(png_ptr, "Too many IDAT's found");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500642 else
643 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500644 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
646 png_handle_PLTE(png_ptr, info_ptr, length);
647 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
648 png_handle_IEND(png_ptr, info_ptr, length);
649#if defined(PNG_READ_bKGD_SUPPORTED)
650 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
651 png_handle_bKGD(png_ptr, info_ptr, length);
652#endif
653#if defined(PNG_READ_cHRM_SUPPORTED)
654 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
655 png_handle_cHRM(png_ptr, info_ptr, length);
656#endif
657#if defined(PNG_READ_gAMA_SUPPORTED)
658 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
659 png_handle_gAMA(png_ptr, info_ptr, length);
660#endif
661#if defined(PNG_READ_hIST_SUPPORTED)
662 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
663 png_handle_hIST(png_ptr, info_ptr, length);
664#endif
665#if defined(PNG_READ_oFFs_SUPPORTED)
666 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
667 png_handle_oFFs(png_ptr, info_ptr, length);
668#endif
669#if defined(PNG_READ_pCAL_SUPPORTED)
670 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
671 png_handle_pCAL(png_ptr, info_ptr, length);
672#endif
673#if defined(PNG_READ_pHYs_SUPPORTED)
674 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
675 png_handle_pHYs(png_ptr, info_ptr, length);
676#endif
677#if defined(PNG_READ_sBIT_SUPPORTED)
678 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
679 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500680#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600681#if defined(PNG_READ_sRGB_SUPPORTED)
682 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
683 png_handle_sRGB(png_ptr, info_ptr, length);
684#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500685#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600686 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
687 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500688#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500689#if defined(PNG_READ_tIME_SUPPORTED)
690 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
691 png_handle_tIME(png_ptr, info_ptr, length);
692#endif
693#if defined(PNG_READ_tRNS_SUPPORTED)
694 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
695 png_handle_tRNS(png_ptr, info_ptr, length);
696#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500697#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600698 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
699 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500700#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500701 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600702 png_handle_unknown(png_ptr, info_ptr, length);
703 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500704}
705
706/* free all memory used by the read */
707void
Guy Schalnate5a37791996-06-05 15:50:50 -0500708png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600709 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500710{
711 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600712 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500713#ifdef PNG_USER_MEM_SUPPORTED
714 png_free_ptr free_fn = NULL;
715#endif /* PNG_USER_MEM_SUPPORTED */
Guy Schalnate5a37791996-06-05 15:50:50 -0500716
Andreas Dilger47a0c421997-05-16 02:46:07 -0500717 png_debug(1, "in png_destroy_read_struct\n");
718 /* save jump buffer and error functions */
719 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500720 png_ptr = *png_ptr_ptr;
721
Andreas Dilger47a0c421997-05-16 02:46:07 -0500722 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500723 info_ptr = *info_ptr_ptr;
724
Andreas Dilger47a0c421997-05-16 02:46:07 -0500725 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600726 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500727
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500728#ifdef PNG_USER_MEM_SUPPORTED
729 free_fn = png_ptr->free_fn;
730#endif
731
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600732 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500733
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500735 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600736#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
737 png_free(png_ptr, info_ptr->text);
738#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500739
740#ifdef PNG_USER_MEM_SUPPORTED
741 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
742#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600743 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500744#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500745 *info_ptr_ptr = (png_infop)NULL;
746 }
747
Andreas Dilger47a0c421997-05-16 02:46:07 -0500748 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500749 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600750#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600751 png_free(png_ptr, end_info_ptr->text);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600752#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500753#ifdef PNG_USER_MEM_SUPPORTED
754 png_destroy_struct_2((png_voidp)end_info_ptr, free_fn);
755#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600756 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500757#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600758 *end_info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500759 }
760
Andreas Dilger47a0c421997-05-16 02:46:07 -0500761 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500762 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500763#ifdef PNG_USER_MEM_SUPPORTED
764 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
765#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600766 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500767#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500768 *png_ptr_ptr = (png_structp)NULL;
769 }
770}
771
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600772/* free all memory used by the read (old method) */
Guy Schalnate5a37791996-06-05 15:50:50 -0500773void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600774png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500775{
Guy Schalnat0d580581995-07-20 02:43:20 -0500776 jmp_buf tmp_jmp;
Guy Schalnate5a37791996-06-05 15:50:50 -0500777 png_error_ptr error_fn;
778 png_error_ptr warning_fn;
779 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500780#ifdef PNG_USER_MEM_SUPPORTED
781 png_free_ptr free_fn;
782#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500783
Andreas Dilger47a0c421997-05-16 02:46:07 -0500784 png_debug(1, "in png_read_destroy\n");
785 /* save jump buffer and error functions */
786 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600787 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500788
Andreas Dilger47a0c421997-05-16 02:46:07 -0500789 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600790 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600792 png_free(png_ptr, png_ptr->zbuf);
793 png_free(png_ptr, png_ptr->row_buf);
794 png_free(png_ptr, png_ptr->prev_row);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500795#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600796 png_free(png_ptr, png_ptr->palette_lookup);
797 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500798#endif
799#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600800 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500801#endif
802#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600803 png_free(png_ptr, png_ptr->gamma_from_1);
804 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600805#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600806 if (png_ptr->flags & PNG_FLAG_FREE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600807 png_zfree(png_ptr, png_ptr->palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600808 if (png_ptr->flags & PNG_FLAG_FREE_TRANS)
809 png_free(png_ptr, png_ptr->trans);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600810#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600811 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
812 png_free(png_ptr, png_ptr->hist);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500813#endif
814#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500815 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500816 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500817 int i;
818 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500819 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500820 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600821 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500822 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600823 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -0500824 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600825#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500826 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600827 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500828 int i;
829 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500830 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600831 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600832 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600833 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600834 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600835 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500836 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500837 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500838 int i;
839 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500840 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500841 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600842 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500843 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600844 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600845 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500846#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500847#endif
848#if defined(PNG_TIME_RFC1123_SUPPORTED)
849 png_free(png_ptr, png_ptr->time_buffer);
850#endif /* PNG_TIME_RFC1123_SUPPORTED */
Guy Schalnat0f716451995-11-28 11:22:13 -0600851
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600852 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -0600853#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600854 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600855#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500856
857 /* Save the important info out of the png_struct, in case it is
858 * being used again.
859 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600860 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500861
862 error_fn = png_ptr->error_fn;
863 warning_fn = png_ptr->warning_fn;
864 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500865#ifdef PNG_USER_MEM_SUPPORTED
866 free_fn = png_ptr->free_fn;
867#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500868
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600869 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500870
871 png_ptr->error_fn = error_fn;
872 png_ptr->warning_fn = warning_fn;
873 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500874#ifdef PNG_USER_MEM_SUPPORTED
875 png_ptr->free_fn = free_fn;
876#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500877
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600878 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Guy Schalnat0d580581995-07-20 02:43:20 -0500879}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600880
881void
882png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
883{
884 png_ptr->read_row_fn = read_row_fn;
885}