blob: 0d3619860db29ece40bf9253b67a4e15d62abc12 [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-Pehrson8686fff1998-05-21 09:27:50 -05004 * 1.0.1d
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-Pehrson8686fff1998-05-21 09:27:50 -05009 * May 21, 1998
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -060010 *
11 * This file contains routines that an application calls directly to
12 * read a PNG file or stream.
13 */
Guy Schalnat0d580581995-07-20 02:43:20 -050014
15#define PNG_INTERNAL
16#include "png.h"
17
Andreas Dilger47a0c421997-05-16 02:46:07 -050018/* Create a PNG structure for reading, and allocate any memory needed. */
Guy Schalnate5a37791996-06-05 15:50:50 -050019png_structp
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050020png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060021 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050022{
Guy Schalnate5a37791996-06-05 15:50:50 -050023 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060024#ifdef USE_FAR_KEYWORD
25 jmp_buf jmpbuf;
26#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050027 png_debug(1, "in png_create_read_struct\n");
Guy Schalnate5a37791996-06-05 15:50:50 -050028 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
29 {
30 return (png_structp)NULL;
31 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060032#ifdef USE_FAR_KEYWORD
33 if (setjmp(jmpbuf))
34#else
Guy Schalnate5a37791996-06-05 15:50:50 -050035 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060036#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050037 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -050039 png_destroy_struct(png_ptr);
40 return (png_structp)NULL;
41 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060042#ifdef USE_FAR_KEYWORD
43 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
44#endif
45 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060046
Andreas Dilger47a0c421997-05-16 02:46:07 -050047 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
48 * we must recompile any applications that use any older library version.
49 * For versions after libpng 1.0, we will be compatible, so we need
50 * only check the first digit.
51 */
52 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
53 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -050054 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060055 png_error(png_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050056 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -050057 }
58
59 /* initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -050060 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -060061 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
62 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060063 png_ptr->zstream.zalloc = png_zalloc;
64 png_ptr->zstream.zfree = png_zfree;
65 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050066
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060067 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -050068 {
69 case Z_OK: /* Do nothing */ break;
70 case Z_MEM_ERROR:
71 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
72 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
73 default: png_error(png_ptr, "Unknown zlib error");
74 }
75
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060076 png_ptr->zstream.next_out = png_ptr->zbuf;
77 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -050078
79 png_set_read_fn(png_ptr, NULL, NULL);
80
Guy Schalnate5a37791996-06-05 15:50:50 -050081 return (png_ptr);
82}
83
Andreas Dilger47a0c421997-05-16 02:46:07 -050084/* Initialize PNG structure for reading, and allocate any memory needed.
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -050085 This interface is deprecated in favour of the png_create_read_struct(),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060086 and it will eventually disappear. */
Guy Schalnate5a37791996-06-05 15:50:50 -050087void
88png_read_init(png_structp png_ptr)
89{
90 jmp_buf tmp_jmp; /* to save current jump buffer */
91
Andreas Dilger47a0c421997-05-16 02:46:07 -050092 png_debug(1, "in png_read_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -050093 /* save jump buffer and error functions */
94 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
95
96 /* reset all variables to 0 */
97 png_memset(png_ptr, 0, sizeof (png_struct));
98
99 /* restore jump buffer */
100 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
101
102 /* initialize zbuf - compression buffer */
103 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600104 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
105 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600106 png_ptr->zstream.zalloc = png_zalloc;
107 png_ptr->zstream.zfree = png_zfree;
108 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500109
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600110 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -0500111 {
112 case Z_OK: /* Do nothing */ break;
113 case Z_MEM_ERROR:
114 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
115 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
116 default: png_error(png_ptr, "Unknown zlib error");
117 }
118
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600119 png_ptr->zstream.next_out = png_ptr->zbuf;
120 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500121
122 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500123}
124
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600125/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600126 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600127 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500128 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600129 * via png_set_sig_bytes(), and we will only check the remaining bytes
130 * here. The application can then have access to the signature bytes we
131 * read if it is determined that this isn't a valid PNG file.
132 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500133void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600134png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500135{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 png_debug(1, "in png_read_info\n");
137 /* save jump buffer and error functions */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138 /* If we haven't checked all of the PNG signature bytes, do so now. */
139 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500140 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500141 png_size_t num_checked = png_ptr->sig_bytes,
142 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600143
Andreas Dilger47a0c421997-05-16 02:46:07 -0500144 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600145 png_ptr->sig_bytes = 8;
146
147 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
148 {
149 if (num_checked < 4 &&
150 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
151 png_error(png_ptr, "Not a PNG file");
152 else
153 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
154 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500155 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500156
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600157 for(;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500158 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600159 png_byte chunk_length[4];
160 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500161
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600162 png_read_data(png_ptr, chunk_length, 4);
163 length = png_get_uint_32(chunk_length);
164
Guy Schalnat0d580581995-07-20 02:43:20 -0500165 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600166 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
167
Andreas Dilger47a0c421997-05-16 02:46:07 -0500168 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
169
170 /* This should be a binary subdivision search or a hash for
171 * matching the chunk name rather than a linear search.
172 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600173 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
174 png_handle_IHDR(png_ptr, info_ptr, length);
175 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
176 png_handle_PLTE(png_ptr, info_ptr, length);
177 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
178 png_handle_IEND(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600179 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500180 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500181 if (!(png_ptr->mode & PNG_HAVE_IHDR))
182 png_error(png_ptr, "Missing IHDR before IDAT");
183 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
184 !(png_ptr->mode & PNG_HAVE_PLTE))
185 png_error(png_ptr, "Missing PLTE before IDAT");
186
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500187 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500188 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500189 break;
190 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500191#if defined(PNG_READ_bKGD_SUPPORTED)
192 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
193 png_handle_bKGD(png_ptr, info_ptr, length);
194#endif
195#if defined(PNG_READ_cHRM_SUPPORTED)
196 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
197 png_handle_cHRM(png_ptr, info_ptr, length);
198#endif
199#if defined(PNG_READ_gAMA_SUPPORTED)
200 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
201 png_handle_gAMA(png_ptr, info_ptr, length);
202#endif
203#if defined(PNG_READ_hIST_SUPPORTED)
204 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
205 png_handle_hIST(png_ptr, info_ptr, length);
206#endif
207#if defined(PNG_READ_oFFs_SUPPORTED)
208 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
209 png_handle_oFFs(png_ptr, info_ptr, length);
210#endif
211#if defined(PNG_READ_pCAL_SUPPORTED)
212 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
213 png_handle_pCAL(png_ptr, info_ptr, length);
214#endif
215#if defined(PNG_READ_pHYs_SUPPORTED)
216 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
217 png_handle_pHYs(png_ptr, info_ptr, length);
218#endif
219#if defined(PNG_READ_sBIT_SUPPORTED)
220 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
221 png_handle_sBIT(png_ptr, info_ptr, length);
222#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600223#if defined(PNG_READ_sRGB_SUPPORTED)
224 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
225 png_handle_sRGB(png_ptr, info_ptr, length);
226#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227#if defined(PNG_READ_tEXt_SUPPORTED)
228 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
229 png_handle_tEXt(png_ptr, info_ptr, length);
230#endif
231#if defined(PNG_READ_tIME_SUPPORTED)
232 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
233 png_handle_tIME(png_ptr, info_ptr, length);
234#endif
235#if defined(PNG_READ_tRNS_SUPPORTED)
236 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
237 png_handle_tRNS(png_ptr, info_ptr, length);
238#endif
239#if defined(PNG_READ_zTXt_SUPPORTED)
240 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
241 png_handle_zTXt(png_ptr, info_ptr, length);
242#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500243 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600244 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500245 }
246}
247
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600248/* optional call to update the users info_ptr structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500249void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600250png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500251{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500252 png_debug(1, "in png_read_update_info\n");
253 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500254 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500255 png_read_start_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600256 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500257}
258
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600259/* Initialize palette, background, etc, after transformations
260 * are set, but before any reading takes place. This allows
261 * the user to obtail a gamma corrected palette, for example.
262 * If the user doesn't call this, we will do it ourselves.
263 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500264void
Guy Schalnat6d764711995-12-19 03:22:19 -0600265png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500266{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 png_debug(1, "in png_start_read_image\n");
268 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500269 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500270 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500271}
272
273void
Guy Schalnat6d764711995-12-19 03:22:19 -0600274png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500275{
276 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500277 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
278 png_ptr->row_number, png_ptr->pass);
279 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500280 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500281 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500282 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
283 {
284 /* check for transforms that have been set but were defined out */
285#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
286 if (png_ptr->transformations & PNG_INVERT_MONO)
287 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
288#endif
289#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
290 if (png_ptr->transformations & PNG_FILLER)
291 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
292#endif
293#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
294 if (png_ptr->transformations & PNG_PACKSWAP)
295 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
296#endif
297#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
298 if (png_ptr->transformations & PNG_PACK)
299 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
300#endif
301#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
302 if (png_ptr->transformations & PNG_SHIFT)
303 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
304#endif
305#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
306 if (png_ptr->transformations & PNG_BGR)
307 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
308#endif
309#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
310 if (png_ptr->transformations & PNG_SWAP_BYTES)
311 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
312#endif
313 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500314
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500315#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500316 /* if interlaced and we do not need a new row, combine row and return */
317 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
318 {
319 switch (png_ptr->pass)
320 {
321 case 0:
322 if (png_ptr->row_number & 7)
323 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500324 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500325 png_combine_row(png_ptr, dsp_row,
326 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600327 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500328 return;
329 }
330 break;
331 case 1:
332 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
333 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500334 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500335 png_combine_row(png_ptr, dsp_row,
336 png_pass_dsp_mask[png_ptr->pass]);
337 png_read_finish_row(png_ptr);
338 return;
339 }
340 break;
341 case 2:
342 if ((png_ptr->row_number & 7) != 4)
343 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500344 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600345 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500346 png_pass_dsp_mask[png_ptr->pass]);
347 png_read_finish_row(png_ptr);
348 return;
349 }
350 break;
351 case 3:
352 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
353 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500355 png_combine_row(png_ptr, dsp_row,
356 png_pass_dsp_mask[png_ptr->pass]);
357 png_read_finish_row(png_ptr);
358 return;
359 }
360 break;
361 case 4:
362 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600363 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500364 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500365 png_combine_row(png_ptr, dsp_row,
366 png_pass_dsp_mask[png_ptr->pass]);
367 png_read_finish_row(png_ptr);
368 return;
369 }
370 break;
371 case 5:
372 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
373 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500374 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500375 png_combine_row(png_ptr, dsp_row,
376 png_pass_dsp_mask[png_ptr->pass]);
377 png_read_finish_row(png_ptr);
378 return;
379 }
380 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600381 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500382 if (!(png_ptr->row_number & 1))
383 {
384 png_read_finish_row(png_ptr);
385 return;
386 }
387 break;
388 }
389 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500390#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500391
Guy Schalnate5a37791996-06-05 15:50:50 -0500392 if (!(png_ptr->mode & PNG_HAVE_IDAT))
393 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500394
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600395 png_ptr->zstream.next_out = png_ptr->row_buf;
396 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 do
398 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600399 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500400 {
401 while (!png_ptr->idat_size)
402 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600403 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500404
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600405 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500406
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600407 png_read_data(png_ptr, chunk_length, 4);
408 png_ptr->idat_size = png_get_uint_32(chunk_length);
409
Guy Schalnat0d580581995-07-20 02:43:20 -0500410 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600411 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
412 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
413 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500414 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600415 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
416 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500417 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600418 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500419 png_crc_read(png_ptr, png_ptr->zbuf,
420 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600421 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500422 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600423 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 if (ret == Z_STREAM_END)
425 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600426 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500427 png_ptr->idat_size)
Guy Schalnat6d764711995-12-19 03:22:19 -0600428 png_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600429 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500430 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600431 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500432 }
433 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600434 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500435 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600436
437 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500438
Guy Schalnat0d580581995-07-20 02:43:20 -0500439 png_ptr->row_info.color_type = png_ptr->color_type;
440 png_ptr->row_info.width = png_ptr->iwidth;
441 png_ptr->row_info.channels = png_ptr->channels;
442 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
443 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600444 {
445 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
446 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
447 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500448
Guy Schalnate5a37791996-06-05 15:50:50 -0500449 png_read_filter_row(png_ptr, &(png_ptr->row_info),
450 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
451 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500452
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600453 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600454 png_ptr->rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500455
456 if (png_ptr->transformations)
457 png_do_read_transformations(png_ptr);
458
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500459#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500460 /* blow up interlaced rows to full size */
461 if (png_ptr->interlaced &&
462 (png_ptr->transformations & PNG_INTERLACE))
463 {
464 if (png_ptr->pass < 6)
465 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500466 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500467
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500469 png_combine_row(png_ptr, dsp_row,
470 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500471 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500472 png_combine_row(png_ptr, row,
473 png_pass_mask[png_ptr->pass]);
474 }
475 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500476#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500477 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500478 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500479 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500480 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500481 png_combine_row(png_ptr, dsp_row, 0xff);
482 }
483 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600484
485 if (png_ptr->read_row_fn != NULL)
486 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500487}
488
Andreas Dilger47a0c421997-05-16 02:46:07 -0500489/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600490 * and png_set_interlace_handling() has been called, the rows need to
491 * contain the contents of the rows from the previous pass. If the
492 * image has alpha or transparency, and png_handle_alpha() has been
493 * called, the rows contents must be initialized to the contents of the
494 * screen.
495 *
496 * "row" holds the actual image, and pixels are placed in it
497 * as they arrive. If the image is displayed after each pass, it will
498 * appear to "sparkle" in. "display_row" can be used to display a
499 * "chunky" progressive image, with finer detail added as it becomes
500 * available. If you do not want this "chunky" display, you may pass
501 * NULL for display_row. If you do not want the sparkle display, and
502 * you have not called png_handle_alpha(), you may pass NULL for rows.
503 * If you have called png_handle_alpha(), and the image has either an
504 * alpha channel or a transparency chunk, you must provide a buffer for
505 * rows. In this case, you do not have to provide a display_row buffer
506 * also, but you may. If the image is not interlaced, or if you have
507 * not called png_set_interlace_handling(), the display_row buffer will
508 * be ignored, so pass NULL to it.
509 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600510
Guy Schalnat0d580581995-07-20 02:43:20 -0500511void
Guy Schalnat6d764711995-12-19 03:22:19 -0600512png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600513 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500514{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600515 png_uint_32 i;
516 png_bytepp rp;
517 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500518
Andreas Dilger47a0c421997-05-16 02:46:07 -0500519 png_debug(1, "in png_read_rows\n");
520 /* save jump buffer and error functions */
Guy Schalnat0f716451995-11-28 11:22:13 -0600521 rp = row;
522 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500523 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500524 for (i = 0; i < num_rows; i++)
525 {
526 png_bytep rptr = *rp++;
527 png_bytep dptr = *dp++;
528
529 png_read_row(png_ptr, rptr, dptr);
530 }
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500531 else if(rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500532 for (i = 0; i < num_rows; i++)
533 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500534 png_bytep rptr = *rp;
535 png_read_row(png_ptr, rptr, NULL);
536 rp++;
537 }
538 else if(dp != NULL)
539 for (i = 0; i < num_rows; i++)
540 {
541 png_bytep dptr = *dp;
542 png_read_row(png_ptr, NULL, dptr);
543 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500544 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500545}
546
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600548 * chunk, and you have called png_handle_alpha(), you will need to
549 * initialize the image to the current image that PNG will be overlaying.
550 * We set the num_rows again here, in case it was incorrectly set in
551 * png_read_start_row() by a call to png_read_update_info() or
552 * png_start_read_image() if png_set_interlace_handling() wasn't called
553 * prior to either of these functions like it should have been. You can
554 * only call this function once. If you desire to have an image for
555 * each pass of a interlaced image, use png_read_rows() instead.
556 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500557void
Guy Schalnat6d764711995-12-19 03:22:19 -0600558png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500559{
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500560 png_uint_32 i,image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500561 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600562 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500563
Andreas Dilger47a0c421997-05-16 02:46:07 -0500564 png_debug(1, "in png_read_image\n");
565 /* save jump buffer and error functions */
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 pass = png_set_interlace_handling(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500567
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500568 image_height=png_ptr->height;
569 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500570
Guy Schalnat0d580581995-07-20 02:43:20 -0500571 for (j = 0; j < pass; j++)
572 {
573 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500574 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500575 {
576 png_read_row(png_ptr, *rp, NULL);
577 rp++;
578 }
579 }
580}
581
Andreas Dilger47a0c421997-05-16 02:46:07 -0500582/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600583 * file, will verify the end is accurate, and will read any comments
584 * or time information at the end of the file, if info is not NULL.
585 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500586void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500588{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500590 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500591
Andreas Dilger47a0c421997-05-16 02:46:07 -0500592 png_debug(1, "in png_read_end\n");
593 /* save jump buffer and error functions */
594 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500595
596 do
597 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600598 png_read_data(png_ptr, chunk_length, 4);
599 length = png_get_uint_32(chunk_length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500600
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600601 png_reset_crc(png_ptr);
602 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
603
Andreas Dilger47a0c421997-05-16 02:46:07 -0500604 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
605
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600606 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
607 png_handle_IHDR(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600608 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500609 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500610 /* Zero length IDATs are legal after the last IDAT has been
611 * read, but not after other chunks have been read.
612 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500613 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
614 png_error(png_ptr, "Too many IDAT's found");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500615 else
616 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500617 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500618 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
619 png_handle_PLTE(png_ptr, info_ptr, length);
620 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
621 png_handle_IEND(png_ptr, info_ptr, length);
622#if defined(PNG_READ_bKGD_SUPPORTED)
623 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
624 png_handle_bKGD(png_ptr, info_ptr, length);
625#endif
626#if defined(PNG_READ_cHRM_SUPPORTED)
627 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
628 png_handle_cHRM(png_ptr, info_ptr, length);
629#endif
630#if defined(PNG_READ_gAMA_SUPPORTED)
631 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
632 png_handle_gAMA(png_ptr, info_ptr, length);
633#endif
634#if defined(PNG_READ_hIST_SUPPORTED)
635 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
636 png_handle_hIST(png_ptr, info_ptr, length);
637#endif
638#if defined(PNG_READ_oFFs_SUPPORTED)
639 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
640 png_handle_oFFs(png_ptr, info_ptr, length);
641#endif
642#if defined(PNG_READ_pCAL_SUPPORTED)
643 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
644 png_handle_pCAL(png_ptr, info_ptr, length);
645#endif
646#if defined(PNG_READ_pHYs_SUPPORTED)
647 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
648 png_handle_pHYs(png_ptr, info_ptr, length);
649#endif
650#if defined(PNG_READ_sBIT_SUPPORTED)
651 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
652 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500653#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600654#if defined(PNG_READ_sRGB_SUPPORTED)
655 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
656 png_handle_sRGB(png_ptr, info_ptr, length);
657#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500658#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600659 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
660 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500661#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500662#if defined(PNG_READ_tIME_SUPPORTED)
663 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
664 png_handle_tIME(png_ptr, info_ptr, length);
665#endif
666#if defined(PNG_READ_tRNS_SUPPORTED)
667 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
668 png_handle_tRNS(png_ptr, info_ptr, length);
669#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500670#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600671 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
672 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500673#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500674 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600675 png_handle_unknown(png_ptr, info_ptr, length);
676 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500677}
678
679/* free all memory used by the read */
680void
Guy Schalnate5a37791996-06-05 15:50:50 -0500681png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600682 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500683{
684 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600685 png_infop info_ptr = NULL, end_info_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500686
Andreas Dilger47a0c421997-05-16 02:46:07 -0500687 png_debug(1, "in png_destroy_read_struct\n");
688 /* save jump buffer and error functions */
689 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500690 png_ptr = *png_ptr_ptr;
691
Andreas Dilger47a0c421997-05-16 02:46:07 -0500692 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500693 info_ptr = *info_ptr_ptr;
694
Andreas Dilger47a0c421997-05-16 02:46:07 -0500695 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600696 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500697
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600698 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500699
Andreas Dilger47a0c421997-05-16 02:46:07 -0500700 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500701 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600702#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
703 png_free(png_ptr, info_ptr->text);
704#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600705 png_destroy_struct((png_voidp)info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500706 *info_ptr_ptr = (png_infop)NULL;
707 }
708
Andreas Dilger47a0c421997-05-16 02:46:07 -0500709 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500710 {
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600711#if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600712 png_free(png_ptr, end_info_ptr->text);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600713#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600714 png_destroy_struct((png_voidp)end_info_ptr);
715 *end_info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500716 }
717
Andreas Dilger47a0c421997-05-16 02:46:07 -0500718 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500719 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600720 png_destroy_struct((png_voidp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500721 *png_ptr_ptr = (png_structp)NULL;
722 }
723}
724
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600725/* free all memory used by the read (old method) */
Guy Schalnate5a37791996-06-05 15:50:50 -0500726void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600727png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500728{
Guy Schalnat0d580581995-07-20 02:43:20 -0500729 jmp_buf tmp_jmp;
Guy Schalnate5a37791996-06-05 15:50:50 -0500730 png_error_ptr error_fn;
731 png_error_ptr warning_fn;
732 png_voidp error_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500733
Andreas Dilger47a0c421997-05-16 02:46:07 -0500734 png_debug(1, "in png_read_destroy\n");
735 /* save jump buffer and error functions */
736 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600737 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500738
Andreas Dilger47a0c421997-05-16 02:46:07 -0500739 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600740 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500741
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600742 png_free(png_ptr, png_ptr->zbuf);
743 png_free(png_ptr, png_ptr->row_buf);
744 png_free(png_ptr, png_ptr->prev_row);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500745#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600746 png_free(png_ptr, png_ptr->palette_lookup);
747 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500748#endif
749#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600750 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500751#endif
752#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600753 png_free(png_ptr, png_ptr->gamma_from_1);
754 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600755#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600756 if (png_ptr->flags & PNG_FLAG_FREE_PALETTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -0600757 png_zfree(png_ptr, png_ptr->palette);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600758 if (png_ptr->flags & PNG_FLAG_FREE_TRANS)
759 png_free(png_ptr, png_ptr->trans);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600760#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600761 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
762 png_free(png_ptr, png_ptr->hist);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500763#endif
764#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500765 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500766 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500767 int i;
768 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500769 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500770 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600771 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500772 }
773 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600774#endif
775#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600776 png_free(png_ptr, png_ptr->gamma_16_table);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500777 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600778 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500779 int i;
780 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500781 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600782 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600783 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600784 }
785 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600786 png_free(png_ptr, png_ptr->gamma_16_from_1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500787 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500788 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500789 int i;
790 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500791 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500792 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600793 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500794 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600795 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600796 png_free(png_ptr, png_ptr->gamma_16_to_1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500797#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600798
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600799 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -0600800#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600801 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600802#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500803
804 /* Save the important info out of the png_struct, in case it is
805 * being used again.
806 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600807 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500808
809 error_fn = png_ptr->error_fn;
810 warning_fn = png_ptr->warning_fn;
811 error_ptr = png_ptr->error_ptr;
812
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600813 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500814
815 png_ptr->error_fn = error_fn;
816 png_ptr->warning_fn = warning_fn;
817 png_ptr->error_ptr = error_ptr;
818
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600819 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Guy Schalnat0d580581995-07-20 02:43:20 -0500820}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600821
822void
823png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
824{
825 png_ptr->read_row_fn = read_row_fn;
826}