blob: 294ad615611642de0193ccaa149f5ff0e5a942d6 [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
Guy Schalnat0d580581995-07-20 02:43:20 -05003
Andreas Dilger47a0c421997-05-16 02:46:07 -05004 libpng 1.0 beta 6 - version 0.96
Guy Schalnat0d580581995-07-20 02:43:20 -05005 For conditions of distribution and use, see copyright notice in png.h
Guy Schalnat4ee97b01996-01-16 01:51:56 -06006 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
Andreas Dilger47a0c421997-05-16 02:46:07 -05007 Copyright (c) 1996, 1997 Andreas Dilger
8 May 12, 1997
Guy Schalnat0d580581995-07-20 02:43:20 -05009 */
10
11#define PNG_INTERNAL
12#include "png.h"
13
Andreas Dilger47a0c421997-05-16 02:46:07 -050014/* Create a PNG structure for reading, and allocate any memory needed. */
Guy Schalnate5a37791996-06-05 15:50:50 -050015png_structp
Andreas Dilger47a0c421997-05-16 02:46:07 -050016png_create_read_struct(png_charp user_png_ver, png_voidp error_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060017 png_error_ptr error_fn, png_error_ptr warn_fn)
Guy Schalnat0d580581995-07-20 02:43:20 -050018{
Guy Schalnate5a37791996-06-05 15:50:50 -050019 png_structp png_ptr;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060020#ifdef USE_FAR_KEYWORD
21 jmp_buf jmpbuf;
22#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -050023 png_debug(1, "in png_create_read_struct\n");
Guy Schalnate5a37791996-06-05 15:50:50 -050024 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
25 {
26 return (png_structp)NULL;
27 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060028#ifdef USE_FAR_KEYWORD
29 if (setjmp(jmpbuf))
30#else
Guy Schalnate5a37791996-06-05 15:50:50 -050031 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060032#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050033 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060034 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -050035 png_destroy_struct(png_ptr);
36 return (png_structp)NULL;
37 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060038#ifdef USE_FAR_KEYWORD
39 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
40#endif
41 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060042
Andreas Dilger47a0c421997-05-16 02:46:07 -050043 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
44 * we must recompile any applications that use any older library version.
45 * For versions after libpng 1.0, we will be compatible, so we need
46 * only check the first digit.
47 */
48 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
49 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -050050 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060051 png_error(png_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050052 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -050053 }
54
55 /* initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -050056 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060057 png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size);
58 png_ptr->zstream.zalloc = png_zalloc;
59 png_ptr->zstream.zfree = png_zfree;
60 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -050061
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -050063 {
64 case Z_OK: /* Do nothing */ break;
65 case Z_MEM_ERROR:
66 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
67 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
68 default: png_error(png_ptr, "Unknown zlib error");
69 }
70
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060071 png_ptr->zstream.next_out = png_ptr->zbuf;
72 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -050073
74 png_set_read_fn(png_ptr, NULL, NULL);
75
Guy Schalnate5a37791996-06-05 15:50:50 -050076 return (png_ptr);
77}
78
79
Andreas Dilger47a0c421997-05-16 02:46:07 -050080/* Initialize PNG structure for reading, and allocate any memory needed.
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060081 This interface is depreciated in favour of the png_create_read_struct(),
82 and it will eventually disappear. */
Guy Schalnate5a37791996-06-05 15:50:50 -050083void
84png_read_init(png_structp png_ptr)
85{
86 jmp_buf tmp_jmp; /* to save current jump buffer */
87
Andreas Dilger47a0c421997-05-16 02:46:07 -050088 png_debug(1, "in png_read_init\n");
Guy Schalnate5a37791996-06-05 15:50:50 -050089 /* save jump buffer and error functions */
90 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
91
92 /* reset all variables to 0 */
93 png_memset(png_ptr, 0, sizeof (png_struct));
94
95 /* restore jump buffer */
96 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
97
98 /* initialize zbuf - compression buffer */
99 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600100 png_ptr->zbuf = png_malloc(png_ptr, png_ptr->zbuf_size);
101 png_ptr->zstream.zalloc = png_zalloc;
102 png_ptr->zstream.zfree = png_zfree;
103 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500104
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -0500106 {
107 case Z_OK: /* Do nothing */ break;
108 case Z_MEM_ERROR:
109 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
110 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
111 default: png_error(png_ptr, "Unknown zlib error");
112 }
113
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600114 png_ptr->zstream.next_out = png_ptr->zbuf;
115 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500116
117 png_set_read_fn(png_ptr, NULL, NULL);
Guy Schalnat0d580581995-07-20 02:43:20 -0500118}
119
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600120/* Read the information before the actual image data. This has been
121 * changed in v0.90 to allow reading a file which already has the magic
122 * bytes read from the stream. You can tell libpng how many bytes have
123 * been read from the beginning of the stream (up to the maxumum of 8)
124 * via png_set_sig_bytes(), and we will only check the remaining bytes
125 * here. The application can then have access to the signature bytes we
126 * read if it is determined that this isn't a valid PNG file.
127 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500128void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600129png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500130{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500131 png_debug(1, "in png_read_info\n");
132 /* save jump buffer and error functions */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600133 /* If we haven't checked all of the PNG signature bytes, do so now. */
134 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500135 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500136 png_size_t num_checked = png_ptr->sig_bytes,
137 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600138
Andreas Dilger47a0c421997-05-16 02:46:07 -0500139 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600140 png_ptr->sig_bytes = 8;
141
142 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
143 {
144 if (num_checked < 4 &&
145 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
146 png_error(png_ptr, "Not a PNG file");
147 else
148 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
149 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500150 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500151
152 while (1)
153 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600154 png_byte chunk_length[4];
155 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500156
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600157 png_read_data(png_ptr, chunk_length, 4);
158 length = png_get_uint_32(chunk_length);
159
Guy Schalnat0d580581995-07-20 02:43:20 -0500160 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600161 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
162
Andreas Dilger47a0c421997-05-16 02:46:07 -0500163 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
164
165 /* This should be a binary subdivision search or a hash for
166 * matching the chunk name rather than a linear search.
167 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600168 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
169 png_handle_IHDR(png_ptr, info_ptr, length);
170 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
171 png_handle_PLTE(png_ptr, info_ptr, length);
172 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
173 png_handle_IEND(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600174 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500175 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500176 if (!(png_ptr->mode & PNG_HAVE_IHDR))
177 png_error(png_ptr, "Missing IHDR before IDAT");
178 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
179 !(png_ptr->mode & PNG_HAVE_PLTE))
180 png_error(png_ptr, "Missing PLTE before IDAT");
181
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500182 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500183 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500184 break;
185 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500186#if defined(PNG_READ_bKGD_SUPPORTED)
187 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
188 png_handle_bKGD(png_ptr, info_ptr, length);
189#endif
190#if defined(PNG_READ_cHRM_SUPPORTED)
191 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
192 png_handle_cHRM(png_ptr, info_ptr, length);
193#endif
194#if defined(PNG_READ_gAMA_SUPPORTED)
195 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
196 png_handle_gAMA(png_ptr, info_ptr, length);
197#endif
198#if defined(PNG_READ_hIST_SUPPORTED)
199 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
200 png_handle_hIST(png_ptr, info_ptr, length);
201#endif
202#if defined(PNG_READ_oFFs_SUPPORTED)
203 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
204 png_handle_oFFs(png_ptr, info_ptr, length);
205#endif
206#if defined(PNG_READ_pCAL_SUPPORTED)
207 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
208 png_handle_pCAL(png_ptr, info_ptr, length);
209#endif
210#if defined(PNG_READ_pHYs_SUPPORTED)
211 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
212 png_handle_pHYs(png_ptr, info_ptr, length);
213#endif
214#if defined(PNG_READ_sBIT_SUPPORTED)
215 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
216 png_handle_sBIT(png_ptr, info_ptr, length);
217#endif
218#if defined(PNG_READ_tEXt_SUPPORTED)
219 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
220 png_handle_tEXt(png_ptr, info_ptr, length);
221#endif
222#if defined(PNG_READ_tIME_SUPPORTED)
223 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
224 png_handle_tIME(png_ptr, info_ptr, length);
225#endif
226#if defined(PNG_READ_tRNS_SUPPORTED)
227 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
228 png_handle_tRNS(png_ptr, info_ptr, length);
229#endif
230#if defined(PNG_READ_zTXt_SUPPORTED)
231 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
232 png_handle_zTXt(png_ptr, info_ptr, length);
233#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500234 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600235 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500236 }
237}
238
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600239/* optional call to update the users info_ptr structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500240void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600241png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500242{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500243 png_debug(1, "in png_read_update_info\n");
244 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500245 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500246 png_read_start_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600247 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500248}
249
Guy Schalnat0d580581995-07-20 02:43:20 -0500250/* initialize palette, background, etc, after transformations
251 are set, but before any reading takes place. This allows
252 the user to obtail a gamma corrected palette, for example.
253 If the user doesn't call this, we will do it ourselves. */
254void
Guy Schalnat6d764711995-12-19 03:22:19 -0600255png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500256{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500257 png_debug(1, "in png_start_read_image\n");
258 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500259 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500260 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500261}
262
263void
Guy Schalnat6d764711995-12-19 03:22:19 -0600264png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500265{
266 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500267 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
268 png_ptr->row_number, png_ptr->pass);
269 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500270 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500271 png_read_start_row(png_ptr);
272
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500273#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500274 /* if interlaced and we do not need a new row, combine row and return */
275 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
276 {
277 switch (png_ptr->pass)
278 {
279 case 0:
280 if (png_ptr->row_number & 7)
281 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500282 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500283 png_combine_row(png_ptr, dsp_row,
284 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600285 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500286 return;
287 }
288 break;
289 case 1:
290 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
291 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500292 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500293 png_combine_row(png_ptr, dsp_row,
294 png_pass_dsp_mask[png_ptr->pass]);
295 png_read_finish_row(png_ptr);
296 return;
297 }
298 break;
299 case 2:
300 if ((png_ptr->row_number & 7) != 4)
301 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500302 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600303 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500304 png_pass_dsp_mask[png_ptr->pass]);
305 png_read_finish_row(png_ptr);
306 return;
307 }
308 break;
309 case 3:
310 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
311 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500312 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500313 png_combine_row(png_ptr, dsp_row,
314 png_pass_dsp_mask[png_ptr->pass]);
315 png_read_finish_row(png_ptr);
316 return;
317 }
318 break;
319 case 4:
320 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600321 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500322 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500323 png_combine_row(png_ptr, dsp_row,
324 png_pass_dsp_mask[png_ptr->pass]);
325 png_read_finish_row(png_ptr);
326 return;
327 }
328 break;
329 case 5:
330 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
331 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500332 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500333 png_combine_row(png_ptr, dsp_row,
334 png_pass_dsp_mask[png_ptr->pass]);
335 png_read_finish_row(png_ptr);
336 return;
337 }
338 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600339 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500340 if (!(png_ptr->row_number & 1))
341 {
342 png_read_finish_row(png_ptr);
343 return;
344 }
345 break;
346 }
347 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500348#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500349
Guy Schalnate5a37791996-06-05 15:50:50 -0500350 if (!(png_ptr->mode & PNG_HAVE_IDAT))
351 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500352
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600353 png_ptr->zstream.next_out = png_ptr->row_buf;
354 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500355 do
356 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600357 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500358 {
359 while (!png_ptr->idat_size)
360 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600361 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500362
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600363 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500364
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600365 png_read_data(png_ptr, chunk_length, 4);
366 png_ptr->idat_size = png_get_uint_32(chunk_length);
367
Guy Schalnat0d580581995-07-20 02:43:20 -0500368 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600369 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
370 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
371 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500372 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600373 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
374 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500375 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600376 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500377 png_crc_read(png_ptr, png_ptr->zbuf,
378 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600379 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500380 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600381 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500382 if (ret == Z_STREAM_END)
383 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600384 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500385 png_ptr->idat_size)
Guy Schalnat6d764711995-12-19 03:22:19 -0600386 png_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600387 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500388 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600389 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500390 }
391 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600392 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500393 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600394
395 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500396
Guy Schalnat0d580581995-07-20 02:43:20 -0500397 png_ptr->row_info.color_type = png_ptr->color_type;
398 png_ptr->row_info.width = png_ptr->iwidth;
399 png_ptr->row_info.channels = png_ptr->channels;
400 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
401 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
402 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
403 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
404
Guy Schalnate5a37791996-06-05 15:50:50 -0500405 png_read_filter_row(png_ptr, &(png_ptr->row_info),
406 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
407 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500408
Andreas Dilger47a0c421997-05-16 02:46:07 -0500409 png_memcpy(png_ptr->prev_row, png_ptr->row_buf, png_ptr->rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500410
411 if (png_ptr->transformations)
412 png_do_read_transformations(png_ptr);
413
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500414#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500415 /* blow up interlaced rows to full size */
416 if (png_ptr->interlaced &&
417 (png_ptr->transformations & PNG_INTERLACE))
418 {
419 if (png_ptr->pass < 6)
420 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500421 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500422
Andreas Dilger47a0c421997-05-16 02:46:07 -0500423 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 png_combine_row(png_ptr, dsp_row,
425 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500426 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500427 png_combine_row(png_ptr, row,
428 png_pass_mask[png_ptr->pass]);
429 }
430 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500431#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500432 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500433 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500434 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500435 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500436 png_combine_row(png_ptr, dsp_row, 0xff);
437 }
438 png_read_finish_row(png_ptr);
439}
440
Andreas Dilger47a0c421997-05-16 02:46:07 -0500441/* Read one or more rows of image data. If the image is interlaced,
Guy Schalnat0d580581995-07-20 02:43:20 -0500442 and png_set_interlace_handling() has been called, the rows need to
443 to contain the contents of the rows from the previous pass. If
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600444 the image has alpha or transparency, and png_handle_alpha() has been
Guy Schalnat0d580581995-07-20 02:43:20 -0500445 called, the rows contents must be initialized to the contents of the
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 screen. "row" holds the actual image, and pixels are placed in it
Guy Schalnat0d580581995-07-20 02:43:20 -0500447 as they arrive. If the image is displayed after each pass, it will
Andreas Dilger47a0c421997-05-16 02:46:07 -0500448 appear to "sparkle" in. "display_row" can be used to display a
Guy Schalnat0d580581995-07-20 02:43:20 -0500449 "chunky" progressive image, with finer detail added as it becomes
450 available. If you do not want this "chunky" display, you may pass
Andreas Dilger47a0c421997-05-16 02:46:07 -0500451 NULL for display_row. If you do not want the sparkle display, and
Guy Schalnat0d580581995-07-20 02:43:20 -0500452 you have not called png_handle_alpha(), you may pass NULL for rows.
453 If you have called png_handle_alpha(), and the image has either an
454 alpha channel or a transparency chunk, you must provide a buffer for
Andreas Dilger47a0c421997-05-16 02:46:07 -0500455 rows. In this case, you do not have to provide a display_row buffer
Guy Schalnat0d580581995-07-20 02:43:20 -0500456 also, but you may. If the image is not interlaced, or if you have
457 not called png_set_interlace_handling(), the display_row buffer will
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600458 be ignored, so pass NULL to it. */
Guy Schalnat6d764711995-12-19 03:22:19 -0600459
Guy Schalnat0d580581995-07-20 02:43:20 -0500460void
Guy Schalnat6d764711995-12-19 03:22:19 -0600461png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600462 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500463{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600464 png_uint_32 i;
465 png_bytepp rp;
466 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500467
Andreas Dilger47a0c421997-05-16 02:46:07 -0500468 png_debug(1, "in png_read_rows\n");
469 /* save jump buffer and error functions */
Guy Schalnat0f716451995-11-28 11:22:13 -0600470 rp = row;
471 dp = display_row;
472 for (i = 0; i < num_rows; i++)
473 {
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600474 png_bytep rptr;
475 png_bytep dptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500476
Andreas Dilger47a0c421997-05-16 02:46:07 -0500477 if (rp != NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600478 rptr = *rp;
479 else
480 rptr = NULL;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500481 if (dp != NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600482 dptr = *dp;
483 else
484 dptr = NULL;
Guy Schalnat0f716451995-11-28 11:22:13 -0600485 png_read_row(png_ptr, rptr, dptr);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500486 if (row != NULL)
Guy Schalnat6d764711995-12-19 03:22:19 -0600487 rp++;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500488 if (display_row != NULL)
Guy Schalnat0f716451995-11-28 11:22:13 -0600489 dp++;
490 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500491}
492
Andreas Dilger47a0c421997-05-16 02:46:07 -0500493/* Read the entire image. If the image has an alpha channel or a tRNS
Guy Schalnat0f716451995-11-28 11:22:13 -0600494 chunk, and you have called png_handle_alpha(), you will need to
Andreas Dilger47a0c421997-05-16 02:46:07 -0500495 initialize the image to the current image that PNG will be overlaying.
Guy Schalnate5a37791996-06-05 15:50:50 -0500496 We set the num_rows again here, in case it was incorrectly set in
497 png_read_start_row() by a call to png_read_update_info() or
498 png_start_read_image() if png_set_interlace_handling() wasn't called
Andreas Dilger47a0c421997-05-16 02:46:07 -0500499 prior to either of these functions like it should have been. You can
500 only call this function once. If you desire to have an image for
Guy Schalnate5a37791996-06-05 15:50:50 -0500501 each pass of a interlaced image, use png_read_rows() instead */
Guy Schalnat0d580581995-07-20 02:43:20 -0500502void
Guy Schalnat6d764711995-12-19 03:22:19 -0600503png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500504{
505 png_uint_32 i;
506 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600507 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500508
Andreas Dilger47a0c421997-05-16 02:46:07 -0500509 png_debug(1, "in png_read_image\n");
510 /* save jump buffer and error functions */
Guy Schalnat0d580581995-07-20 02:43:20 -0500511 pass = png_set_interlace_handling(png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500512
513 png_ptr->num_rows = png_ptr->height; /* Make sure this is set correctly */
514
Guy Schalnat0d580581995-07-20 02:43:20 -0500515 for (j = 0; j < pass; j++)
516 {
517 rp = image;
518 for (i = 0; i < png_ptr->height; i++)
519 {
520 png_read_row(png_ptr, *rp, NULL);
521 rp++;
522 }
523 }
524}
525
Andreas Dilger47a0c421997-05-16 02:46:07 -0500526/* Read the end of the PNG file. Will not read past the end of the
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 file, will verify the end is accurate, and will read any comments
528 or time information at the end of the file, if info is not NULL. */
529void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600530png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500531{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600532 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500533 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500534
Andreas Dilger47a0c421997-05-16 02:46:07 -0500535 png_debug(1, "in png_read_end\n");
536 /* save jump buffer and error functions */
537 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500538
539 do
540 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600541 png_read_data(png_ptr, chunk_length, 4);
542 length = png_get_uint_32(chunk_length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500543
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600544 png_reset_crc(png_ptr);
545 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
546
Andreas Dilger47a0c421997-05-16 02:46:07 -0500547 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
548
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600549 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
550 png_handle_IHDR(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600551 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500552 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500553 /* Zero length IDATs are legal after the last IDAT has been
554 * read, but not after other chunks have been read.
555 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500556 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
557 png_error(png_ptr, "Too many IDAT's found");
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 else
559 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500560 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500561 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
562 png_handle_PLTE(png_ptr, info_ptr, length);
563 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
564 png_handle_IEND(png_ptr, info_ptr, length);
565#if defined(PNG_READ_bKGD_SUPPORTED)
566 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
567 png_handle_bKGD(png_ptr, info_ptr, length);
568#endif
569#if defined(PNG_READ_cHRM_SUPPORTED)
570 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
571 png_handle_cHRM(png_ptr, info_ptr, length);
572#endif
573#if defined(PNG_READ_gAMA_SUPPORTED)
574 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
575 png_handle_gAMA(png_ptr, info_ptr, length);
576#endif
577#if defined(PNG_READ_hIST_SUPPORTED)
578 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
579 png_handle_hIST(png_ptr, info_ptr, length);
580#endif
581#if defined(PNG_READ_oFFs_SUPPORTED)
582 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
583 png_handle_oFFs(png_ptr, info_ptr, length);
584#endif
585#if defined(PNG_READ_pCAL_SUPPORTED)
586 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
587 png_handle_pCAL(png_ptr, info_ptr, length);
588#endif
589#if defined(PNG_READ_pHYs_SUPPORTED)
590 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
591 png_handle_pHYs(png_ptr, info_ptr, length);
592#endif
593#if defined(PNG_READ_sBIT_SUPPORTED)
594 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
595 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500596#endif
597#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600598 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
599 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500600#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500601#if defined(PNG_READ_tIME_SUPPORTED)
602 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
603 png_handle_tIME(png_ptr, info_ptr, length);
604#endif
605#if defined(PNG_READ_tRNS_SUPPORTED)
606 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
607 png_handle_tRNS(png_ptr, info_ptr, length);
608#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500609#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600610 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
611 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500612#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500613 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600614 png_handle_unknown(png_ptr, info_ptr, length);
615 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500616}
617
618/* free all memory used by the read */
619void
Guy Schalnate5a37791996-06-05 15:50:50 -0500620png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600621 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500622{
623 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600624 png_infop info_ptr = NULL, end_info_ptr = NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500625
Andreas Dilger47a0c421997-05-16 02:46:07 -0500626 png_debug(1, "in png_destroy_read_struct\n");
627 /* save jump buffer and error functions */
628 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500629 png_ptr = *png_ptr_ptr;
630
Andreas Dilger47a0c421997-05-16 02:46:07 -0500631 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500632 info_ptr = *info_ptr_ptr;
633
Andreas Dilger47a0c421997-05-16 02:46:07 -0500634 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600635 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500636
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600637 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500638
Andreas Dilger47a0c421997-05-16 02:46:07 -0500639 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500640 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600641 png_destroy_struct((png_voidp)info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500642 *info_ptr_ptr = (png_infop)NULL;
643 }
644
Andreas Dilger47a0c421997-05-16 02:46:07 -0500645 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500646 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600647 png_destroy_struct((png_voidp)end_info_ptr);
648 *end_info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -0500649 }
650
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500652 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600653 png_destroy_struct((png_voidp)png_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500654 *png_ptr_ptr = (png_structp)NULL;
655 }
656}
657
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600658/* free all memory used by the read (old method) */
Guy Schalnate5a37791996-06-05 15:50:50 -0500659void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600660png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500661{
662 int i;
663 jmp_buf tmp_jmp;
Guy Schalnate5a37791996-06-05 15:50:50 -0500664 png_error_ptr error_fn;
665 png_error_ptr warning_fn;
666 png_voidp error_ptr;
Guy Schalnat0d580581995-07-20 02:43:20 -0500667
Andreas Dilger47a0c421997-05-16 02:46:07 -0500668 png_debug(1, "in png_read_destroy\n");
669 /* save jump buffer and error functions */
670 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600671 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500672
Andreas Dilger47a0c421997-05-16 02:46:07 -0500673 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600674 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500675
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600676 png_free(png_ptr, png_ptr->zbuf);
677 png_free(png_ptr, png_ptr->row_buf);
678 png_free(png_ptr, png_ptr->prev_row);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500679#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600680 png_free(png_ptr, png_ptr->palette_lookup);
681 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500682#endif
683#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600684 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500685#endif
686#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600687 png_free(png_ptr, png_ptr->gamma_from_1);
688 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600689#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600690 if (png_ptr->flags & PNG_FLAG_FREE_PALETTE)
691 png_free(png_ptr, png_ptr->palette);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600692#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_bKGD_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600693 if (png_ptr->flags & PNG_FLAG_FREE_TRANS)
694 png_free(png_ptr, png_ptr->trans);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600695#endif
696#if defined(PNG_READ_hIST_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600697 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
698 png_free(png_ptr, png_ptr->hist);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500699#endif
700#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -0500701 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500702 {
703 for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
704 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600705 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500706 }
707 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600708#endif
709#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600710 png_free(png_ptr, png_ptr->gamma_16_table);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500711 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600712 {
713 for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
714 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600715 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600716 }
717 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600718 png_free(png_ptr, png_ptr->gamma_16_from_1);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500719 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500720 {
721 for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
722 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600723 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -0500724 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600725 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600726 png_free(png_ptr, png_ptr->gamma_16_to_1);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500727#endif
Guy Schalnat0f716451995-11-28 11:22:13 -0600728
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600729 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -0600730#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600731 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -0600732#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500733
734 /* Save the important info out of the png_struct, in case it is
735 * being used again.
736 */
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600737 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Guy Schalnate5a37791996-06-05 15:50:50 -0500738
739 error_fn = png_ptr->error_fn;
740 warning_fn = png_ptr->warning_fn;
741 error_ptr = png_ptr->error_ptr;
742
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600743 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -0500744
745 png_ptr->error_fn = error_fn;
746 png_ptr->warning_fn = warning_fn;
747 png_ptr->error_ptr = error_ptr;
748
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600749 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Guy Schalnat0d580581995-07-20 02:43:20 -0500750}