blob: 96b6cab2530b8a299334a93683f261a0c0315155 [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-Pehrson98c9d732000-05-03 21:06:11 -05004 * libpng 1.0.6j - May 4, 2000
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-Pehrson61c32d92000-02-04 23:40:16 -06008 * Copyright (c) 1998, 1999, 2000 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;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060037
38#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060039#ifdef USE_FAR_KEYWORD
40 jmp_buf jmpbuf;
41#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060042#endif
43
Andreas Dilger47a0c421997-05-16 02:46:07 -050044 png_debug(1, "in png_create_read_struct\n");
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050045#ifdef PNG_USER_MEM_SUPPORTED
46 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
47 (png_malloc_ptr)malloc_fn)) == NULL)
48#else
Guy Schalnate5a37791996-06-05 15:50:50 -050049 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050050#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050051 {
52 return (png_structp)NULL;
53 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060054
55#ifdef PNG_SETJMP_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060056#ifdef USE_FAR_KEYWORD
57 if (setjmp(jmpbuf))
58#else
Guy Schalnate5a37791996-06-05 15:50:50 -050059 if (setjmp(png_ptr->jmpbuf))
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060060#endif
Guy Schalnate5a37791996-06-05 15:50:50 -050061 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060062 png_free(png_ptr, png_ptr->zbuf);
Guy Schalnate5a37791996-06-05 15:50:50 -050063 png_destroy_struct(png_ptr);
64 return (png_structp)NULL;
65 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060066#ifdef USE_FAR_KEYWORD
67 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
68#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -060069#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050070
71#ifdef PNG_USER_MEM_SUPPORTED
72 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -060073#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -050074
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060075 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
Guy Schalnat6d764711995-12-19 03:22:19 -060076
Andreas Dilger47a0c421997-05-16 02:46:07 -050077 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
78 * we must recompile any applications that use any older library version.
79 * For versions after libpng 1.0, we will be compatible, so we need
80 * only check the first digit.
81 */
82 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
83 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
Guy Schalnate5a37791996-06-05 15:50:50 -050084 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -060085 png_error(png_ptr,
Andreas Dilger47a0c421997-05-16 02:46:07 -050086 "Incompatible libpng version in application and library");
Guy Schalnate5a37791996-06-05 15:50:50 -050087 }
88
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -050089 /* Libpng 1.0.6 was not binary compatible, due to insertion of the
90 info_ptr->free_me member. Note to maintainer: this test can be
91 removed from version 2.0.0 and beyond because the previous test
92 would have already rejected it. */
93
94 if (user_png_ver[4] == '6' && user_png_ver[2] == '0' &&
95 user_png_ver[0] == '1' && user_png_ver[5] == '\0')
96 {
97 png_error(png_ptr,
98 "Application must be recompiled; version 1.0.6 was incompatible");
99 }
100
Guy Schalnate5a37791996-06-05 15:50:50 -0500101 /* initialize zbuf - compression buffer */
Guy Schalnat0d580581995-07-20 02:43:20 -0500102 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600103 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
104 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600105 png_ptr->zstream.zalloc = png_zalloc;
106 png_ptr->zstream.zfree = png_zfree;
107 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500108
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600109 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -0500110 {
111 case Z_OK: /* Do nothing */ break;
112 case Z_MEM_ERROR:
113 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
114 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
115 default: png_error(png_ptr, "Unknown zlib error");
116 }
117
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600118 png_ptr->zstream.next_out = png_ptr->zbuf;
119 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500120
121 png_set_read_fn(png_ptr, NULL, NULL);
122
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -0500123 png_ptr->mode |= PNG_CREATED_READ_STRUCT;
124
Guy Schalnate5a37791996-06-05 15:50:50 -0500125 return (png_ptr);
126}
127
Andreas Dilger47a0c421997-05-16 02:46:07 -0500128/* Initialize PNG structure for reading, and allocate any memory needed.
Glenn Randers-Pehrsond0dce401998-05-09 10:02:29 -0500129 This interface is deprecated in favour of the png_create_read_struct(),
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600130 and it will eventually disappear. */
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500131#undef png_read_init
Guy Schalnate5a37791996-06-05 15:50:50 -0500132void
133png_read_init(png_structp png_ptr)
134{
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500135 /* We only come here via pre-1.0.7-compiled applications */
136 png_read_init_2(png_ptr, "1.0.0", 10000, 10000);
137}
138
139void
140png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
141 png_size_t png_struct_size, png_size_t png_info_size)
142{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600143#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500144 jmp_buf tmp_jmp; /* to save current jump buffer */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600145#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500146
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500147#ifndef PNG_LEGACY_SUPPORTED
148 int i=0;
149 do
150 {
151 if(user_png_ver[i] != png_libpng_ver[i])
152 {
153 png_ptr->error_fn=(png_error_ptr)NULL;
154 png_error(png_ptr,
155 "Application uses deprecated png_read_init() and must be recompiled.");
156 }
157 } while (png_libpng_ver[i++]);
158#endif
159
160 if(sizeof(png_struct) > png_struct_size ||
161 sizeof(png_info) > png_info_size)
162 {
163 png_ptr->error_fn=(png_error_ptr)NULL;
164 png_error(png_ptr,
165 "Application and library have different sized structs. Please recompile.");
166 }
167
168 png_debug(1, "in png_read_init_2\n");
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600169
170#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500171 /* save jump buffer and error functions */
172 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600173#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500174
175 /* reset all variables to 0 */
176 png_memset(png_ptr, 0, sizeof (png_struct));
177
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600178#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnate5a37791996-06-05 15:50:50 -0500179 /* restore jump buffer */
180 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600181#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500182
183 /* initialize zbuf - compression buffer */
184 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600185 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
186 (png_uint_32)png_ptr->zbuf_size);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600187 png_ptr->zstream.zalloc = png_zalloc;
188 png_ptr->zstream.zfree = png_zfree;
189 png_ptr->zstream.opaque = (voidpf)png_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500190
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600191 switch (inflateInit(&png_ptr->zstream))
Guy Schalnate5a37791996-06-05 15:50:50 -0500192 {
193 case Z_OK: /* Do nothing */ break;
194 case Z_MEM_ERROR:
195 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
196 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
197 default: png_error(png_ptr, "Unknown zlib error");
198 }
199
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600200 png_ptr->zstream.next_out = png_ptr->zbuf;
201 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
Guy Schalnate5a37791996-06-05 15:50:50 -0500202
203 png_set_read_fn(png_ptr, NULL, NULL);
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500204
205#ifdef PNG_LEGACY_SUPPORTED
206 if (user_png_ver)
207 return;
208#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500209}
210
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600211/* Read the information before the actual image data. This has been
Glenn Randers-Pehrsonf9f2fe01998-03-15 18:20:23 -0600212 * changed in v0.90 to allow reading a file that already has the magic
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600213 * bytes read from the stream. You can tell libpng how many bytes have
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500214 * been read from the beginning of the stream (up to the maximum of 8)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600215 * via png_set_sig_bytes(), and we will only check the remaining bytes
216 * here. The application can then have access to the signature bytes we
217 * read if it is determined that this isn't a valid PNG file.
218 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500219void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600220png_read_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500221{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500222 png_debug(1, "in png_read_info\n");
223 /* save jump buffer and error functions */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600224 /* If we haven't checked all of the PNG signature bytes, do so now. */
225 if (png_ptr->sig_bytes < 8)
Guy Schalnate5a37791996-06-05 15:50:50 -0500226 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500227 png_size_t num_checked = png_ptr->sig_bytes,
228 num_to_check = 8 - num_checked;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600229
Andreas Dilger47a0c421997-05-16 02:46:07 -0500230 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600231 png_ptr->sig_bytes = 8;
232
233 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
234 {
235 if (num_checked < 4 &&
236 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
237 png_error(png_ptr, "Not a PNG file");
238 else
239 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
240 }
Guy Schalnate5a37791996-06-05 15:50:50 -0500241 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500242
Glenn Randers-Pehrsonea3bcd71998-03-07 14:33:00 -0600243 for(;;)
Guy Schalnat0d580581995-07-20 02:43:20 -0500244 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600245#ifdef PNG_USE_LOCAL_ARRAYS
246 PNG_IHDR;
247 PNG_IDAT;
248 PNG_IEND;
249 PNG_PLTE;
250#if defined(PNG_READ_bKGD_SUPPORTED)
251 PNG_bKGD;
252#endif
253#if defined(PNG_READ_cHRM_SUPPORTED)
254 PNG_cHRM;
255#endif
256#if defined(PNG_READ_gAMA_SUPPORTED)
257 PNG_gAMA;
258#endif
259#if defined(PNG_READ_hIST_SUPPORTED)
260 PNG_hIST;
261#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600262#if defined(PNG_READ_iCCP_SUPPORTED)
263 PNG_iCCP;
264#endif
265#if defined(PNG_READ_iTXt_SUPPORTED)
266 PNG_iTXt;
267#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600268#if defined(PNG_READ_oFFs_SUPPORTED)
269 PNG_oFFs;
270#endif
271#if defined(PNG_READ_pCAL_SUPPORTED)
272 PNG_pCAL;
273#endif
274#if defined(PNG_READ_pHYs_SUPPORTED)
275 PNG_pHYs;
276#endif
277#if defined(PNG_READ_sBIT_SUPPORTED)
278 PNG_sBIT;
279#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600280#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600281 PNG_sCAL;
282#endif
283#if defined(PNG_READ_sPLT_SUPPORTED)
284 PNG_sPLT;
285#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600286#if defined(PNG_READ_sRGB_SUPPORTED)
287 PNG_sRGB;
288#endif
289#if defined(PNG_READ_tEXt_SUPPORTED)
290 PNG_tEXt;
291#endif
292#if defined(PNG_READ_tIME_SUPPORTED)
293 PNG_tIME;
294#endif
295#if defined(PNG_READ_tRNS_SUPPORTED)
296 PNG_tRNS;
297#endif
298#if defined(PNG_READ_zTXt_SUPPORTED)
299 PNG_zTXt;
300#endif
301#endif /* PNG_GLOBAL_ARRAYS */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600302 png_byte chunk_length[4];
303 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500304
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600305 png_read_data(png_ptr, chunk_length, 4);
306 length = png_get_uint_32(chunk_length);
307
Guy Schalnat0d580581995-07-20 02:43:20 -0500308 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600309 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
310
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500311 png_debug2(0, "Reading %s chunk, length=%d.\n", png_ptr->chunk_name,
312 length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500313
314 /* This should be a binary subdivision search or a hash for
315 * matching the chunk name rather than a linear search.
316 */
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600317 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
318 png_handle_IHDR(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600319 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
320 png_handle_IEND(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600321#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
322 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
323 {
324 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
325 png_ptr->mode |= PNG_HAVE_IDAT;
326 png_handle_unknown(png_ptr, info_ptr, length);
327 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
328 png_ptr->mode |= PNG_HAVE_PLTE;
329 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
330 {
331 if (!(png_ptr->mode & PNG_HAVE_IHDR))
332 png_error(png_ptr, "Missing IHDR before IDAT");
333 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
334 !(png_ptr->mode & PNG_HAVE_PLTE))
335 png_error(png_ptr, "Missing PLTE before IDAT");
336 break;
337 }
338 }
339#endif
340 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
341 png_handle_PLTE(png_ptr, info_ptr, length);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600342 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500343 {
Guy Schalnate5a37791996-06-05 15:50:50 -0500344 if (!(png_ptr->mode & PNG_HAVE_IHDR))
345 png_error(png_ptr, "Missing IHDR before IDAT");
346 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
347 !(png_ptr->mode & PNG_HAVE_PLTE))
348 png_error(png_ptr, "Missing PLTE before IDAT");
349
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500350 png_ptr->idat_size = length;
Guy Schalnate5a37791996-06-05 15:50:50 -0500351 png_ptr->mode |= PNG_HAVE_IDAT;
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500352 break;
353 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500354#if defined(PNG_READ_bKGD_SUPPORTED)
355 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
356 png_handle_bKGD(png_ptr, info_ptr, length);
357#endif
358#if defined(PNG_READ_cHRM_SUPPORTED)
359 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
360 png_handle_cHRM(png_ptr, info_ptr, length);
361#endif
362#if defined(PNG_READ_gAMA_SUPPORTED)
363 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
364 png_handle_gAMA(png_ptr, info_ptr, length);
365#endif
366#if defined(PNG_READ_hIST_SUPPORTED)
367 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
368 png_handle_hIST(png_ptr, info_ptr, length);
369#endif
370#if defined(PNG_READ_oFFs_SUPPORTED)
371 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
372 png_handle_oFFs(png_ptr, info_ptr, length);
373#endif
374#if defined(PNG_READ_pCAL_SUPPORTED)
375 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
376 png_handle_pCAL(png_ptr, info_ptr, length);
377#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600378#if defined(PNG_READ_sCAL_SUPPORTED)
379 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
380 png_handle_sCAL(png_ptr, info_ptr, length);
381#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500382#if defined(PNG_READ_pHYs_SUPPORTED)
383 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
384 png_handle_pHYs(png_ptr, info_ptr, length);
385#endif
386#if defined(PNG_READ_sBIT_SUPPORTED)
387 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
388 png_handle_sBIT(png_ptr, info_ptr, length);
389#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600390#if defined(PNG_READ_sRGB_SUPPORTED)
391 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
392 png_handle_sRGB(png_ptr, info_ptr, length);
393#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600394#if defined(PNG_READ_iCCP_SUPPORTED)
395 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
396 png_handle_iCCP(png_ptr, info_ptr, length);
397#endif
398#if defined(PNG_READ_sPLT_SUPPORTED)
399 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
400 png_handle_sPLT(png_ptr, info_ptr, length);
401#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500402#if defined(PNG_READ_tEXt_SUPPORTED)
403 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
404 png_handle_tEXt(png_ptr, info_ptr, length);
405#endif
406#if defined(PNG_READ_tIME_SUPPORTED)
407 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
408 png_handle_tIME(png_ptr, info_ptr, length);
409#endif
410#if defined(PNG_READ_tRNS_SUPPORTED)
411 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
412 png_handle_tRNS(png_ptr, info_ptr, length);
413#endif
414#if defined(PNG_READ_zTXt_SUPPORTED)
415 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
416 png_handle_zTXt(png_ptr, info_ptr, length);
417#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600418#if defined(PNG_READ_iTXt_SUPPORTED)
419 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
420 png_handle_iTXt(png_ptr, info_ptr, length);
421#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500422 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600423 png_handle_unknown(png_ptr, info_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500424 }
425}
426
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600427/* optional call to update the users info_ptr structure */
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500428void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600429png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500430{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500431 png_debug(1, "in png_read_update_info\n");
432 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500433 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500434 png_read_start_row(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600435 png_read_transform_info(png_ptr, info_ptr);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500436}
437
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600438/* Initialize palette, background, etc, after transformations
439 * are set, but before any reading takes place. This allows
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500440 * the user to obtain a gamma-corrected palette, for example.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600441 * If the user doesn't call this, we will do it ourselves.
442 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500443void
Guy Schalnat6d764711995-12-19 03:22:19 -0600444png_start_read_image(png_structp png_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500445{
Andreas Dilger47a0c421997-05-16 02:46:07 -0500446 png_debug(1, "in png_start_read_image\n");
447 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500448 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500449 png_read_start_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500450}
451
452void
Guy Schalnat6d764711995-12-19 03:22:19 -0600453png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
Guy Schalnat0d580581995-07-20 02:43:20 -0500454{
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600455#ifdef PNG_USE_LOCAL_ARRAYS
456 PNG_IDAT;
Glenn Randers-Pehrson5379b241999-11-27 10:22:33 -0600457 const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
458 const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600459#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500460 int ret;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500461 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
462 png_ptr->row_number, png_ptr->pass);
463 /* save jump buffer and error functions */
Guy Schalnate5a37791996-06-05 15:50:50 -0500464 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
Guy Schalnat0d580581995-07-20 02:43:20 -0500465 png_read_start_row(png_ptr);
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500466 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
467 {
468 /* check for transforms that have been set but were defined out */
469#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
470 if (png_ptr->transformations & PNG_INVERT_MONO)
471 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
472#endif
473#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
474 if (png_ptr->transformations & PNG_FILLER)
475 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
476#endif
477#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
478 if (png_ptr->transformations & PNG_PACKSWAP)
479 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
480#endif
481#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
482 if (png_ptr->transformations & PNG_PACK)
483 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
484#endif
485#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
486 if (png_ptr->transformations & PNG_SHIFT)
487 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
488#endif
489#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
490 if (png_ptr->transformations & PNG_BGR)
491 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
492#endif
493#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
494 if (png_ptr->transformations & PNG_SWAP_BYTES)
495 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
496#endif
497 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500498
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500499#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500500 /* if interlaced and we do not need a new row, combine row and return */
501 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
502 {
503 switch (png_ptr->pass)
504 {
505 case 0:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600506 if (png_ptr->row_number & 0x07)
Guy Schalnat0d580581995-07-20 02:43:20 -0500507 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500508 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500509 png_combine_row(png_ptr, dsp_row,
510 png_pass_dsp_mask[png_ptr->pass]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600511 png_read_finish_row(png_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -0500512 return;
513 }
514 break;
515 case 1:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600516 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
Guy Schalnat0d580581995-07-20 02:43:20 -0500517 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500518 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500519 png_combine_row(png_ptr, dsp_row,
520 png_pass_dsp_mask[png_ptr->pass]);
521 png_read_finish_row(png_ptr);
522 return;
523 }
524 break;
525 case 2:
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600526 if ((png_ptr->row_number & 0x07) != 4)
Guy Schalnat0d580581995-07-20 02:43:20 -0500527 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500528 if (dsp_row != NULL && (png_ptr->row_number & 4))
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600529 png_combine_row(png_ptr, dsp_row,
Guy Schalnat0d580581995-07-20 02:43:20 -0500530 png_pass_dsp_mask[png_ptr->pass]);
531 png_read_finish_row(png_ptr);
532 return;
533 }
534 break;
535 case 3:
536 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
537 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500538 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500539 png_combine_row(png_ptr, dsp_row,
540 png_pass_dsp_mask[png_ptr->pass]);
541 png_read_finish_row(png_ptr);
542 return;
543 }
544 break;
545 case 4:
546 if ((png_ptr->row_number & 3) != 2)
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600547 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500548 if (dsp_row != NULL && (png_ptr->row_number & 2))
Guy Schalnat0d580581995-07-20 02:43:20 -0500549 png_combine_row(png_ptr, dsp_row,
550 png_pass_dsp_mask[png_ptr->pass]);
551 png_read_finish_row(png_ptr);
552 return;
553 }
554 break;
555 case 5:
556 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
557 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500558 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500559 png_combine_row(png_ptr, dsp_row,
560 png_pass_dsp_mask[png_ptr->pass]);
561 png_read_finish_row(png_ptr);
562 return;
563 }
564 break;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600565 case 6:
Guy Schalnat0d580581995-07-20 02:43:20 -0500566 if (!(png_ptr->row_number & 1))
567 {
568 png_read_finish_row(png_ptr);
569 return;
570 }
571 break;
572 }
573 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500574#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500575
Guy Schalnate5a37791996-06-05 15:50:50 -0500576 if (!(png_ptr->mode & PNG_HAVE_IDAT))
577 png_error(png_ptr, "Invalid attempt to read row data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500578
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600579 png_ptr->zstream.next_out = png_ptr->row_buf;
580 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
Guy Schalnat0d580581995-07-20 02:43:20 -0500581 do
582 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600583 if (!(png_ptr->zstream.avail_in))
Guy Schalnat0d580581995-07-20 02:43:20 -0500584 {
585 while (!png_ptr->idat_size)
586 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600587 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500588
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600589 png_crc_finish(png_ptr, 0);
Guy Schalnat0d580581995-07-20 02:43:20 -0500590
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600591 png_read_data(png_ptr, chunk_length, 4);
592 png_ptr->idat_size = png_get_uint_32(chunk_length);
593
Guy Schalnat0d580581995-07-20 02:43:20 -0500594 png_reset_crc(png_ptr);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600595 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
596 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
597 png_error(png_ptr, "Not enough image data");
Guy Schalnat0d580581995-07-20 02:43:20 -0500598 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600599 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
600 png_ptr->zstream.next_in = png_ptr->zbuf;
Guy Schalnat0d580581995-07-20 02:43:20 -0500601 if (png_ptr->zbuf_size > png_ptr->idat_size)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600602 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
Andreas Dilger47a0c421997-05-16 02:46:07 -0500603 png_crc_read(png_ptr, png_ptr->zbuf,
604 (png_size_t)png_ptr->zstream.avail_in);
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600605 png_ptr->idat_size -= png_ptr->zstream.avail_in;
Guy Schalnat0d580581995-07-20 02:43:20 -0500606 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600607 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
Guy Schalnat0d580581995-07-20 02:43:20 -0500608 if (ret == Z_STREAM_END)
609 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600610 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
Guy Schalnat0d580581995-07-20 02:43:20 -0500611 png_ptr->idat_size)
Guy Schalnat6d764711995-12-19 03:22:19 -0600612 png_error(png_ptr, "Extra compressed data");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600613 png_ptr->mode |= PNG_AFTER_IDAT;
Guy Schalnate5a37791996-06-05 15:50:50 -0500614 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600615 break;
Guy Schalnat0d580581995-07-20 02:43:20 -0500616 }
617 if (ret != Z_OK)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600618 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
Guy Schalnate5a37791996-06-05 15:50:50 -0500619 "Decompression error");
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600620
621 } while (png_ptr->zstream.avail_out);
Guy Schalnat0d580581995-07-20 02:43:20 -0500622
Guy Schalnat0d580581995-07-20 02:43:20 -0500623 png_ptr->row_info.color_type = png_ptr->color_type;
624 png_ptr->row_info.width = png_ptr->iwidth;
625 png_ptr->row_info.channels = png_ptr->channels;
626 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
627 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
Glenn Randers-Pehrson860ab2b1999-10-14 07:43:10 -0500628 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
629 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
Guy Schalnat0d580581995-07-20 02:43:20 -0500630
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600631 if(png_ptr->row_buf[0])
Guy Schalnate5a37791996-06-05 15:50:50 -0500632 png_read_filter_row(png_ptr, &(png_ptr->row_info),
633 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
634 (int)(png_ptr->row_buf[0]));
Guy Schalnat0d580581995-07-20 02:43:20 -0500635
Glenn Randers-Pehrsona357b991998-02-08 20:56:40 -0600636 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
Glenn Randers-Pehrson0f881d61998-02-07 10:20:57 -0600637 png_ptr->rowbytes + 1);
Guy Schalnat0d580581995-07-20 02:43:20 -0500638
639 if (png_ptr->transformations)
640 png_do_read_transformations(png_ptr);
641
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500642#if defined(PNG_READ_INTERLACING_SUPPORTED)
Guy Schalnat0d580581995-07-20 02:43:20 -0500643 /* blow up interlaced rows to full size */
644 if (png_ptr->interlaced &&
645 (png_ptr->transformations & PNG_INTERLACE))
646 {
647 if (png_ptr->pass < 6)
648 png_do_read_interlace(&(png_ptr->row_info),
Andreas Dilger47a0c421997-05-16 02:46:07 -0500649 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
Guy Schalnat0d580581995-07-20 02:43:20 -0500650
Andreas Dilger47a0c421997-05-16 02:46:07 -0500651 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500652 png_combine_row(png_ptr, dsp_row,
653 png_pass_dsp_mask[png_ptr->pass]);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500654 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500655 png_combine_row(png_ptr, row,
656 png_pass_mask[png_ptr->pass]);
657 }
658 else
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500659#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500660 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500661 if (row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500662 png_combine_row(png_ptr, row, 0xff);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500663 if (dsp_row != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -0500664 png_combine_row(png_ptr, dsp_row, 0xff);
665 }
666 png_read_finish_row(png_ptr);
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -0600667
668 if (png_ptr->read_row_fn != NULL)
669 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
Guy Schalnat0d580581995-07-20 02:43:20 -0500670}
671
Andreas Dilger47a0c421997-05-16 02:46:07 -0500672/* Read one or more rows of image data. If the image is interlaced,
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600673 * and png_set_interlace_handling() has been called, the rows need to
674 * contain the contents of the rows from the previous pass. If the
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500675 * image has alpha or transparency, and png_handle_alpha()[*] has been
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600676 * called, the rows contents must be initialized to the contents of the
677 * screen.
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600678 *
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600679 * "row" holds the actual image, and pixels are placed in it
680 * as they arrive. If the image is displayed after each pass, it will
681 * appear to "sparkle" in. "display_row" can be used to display a
682 * "chunky" progressive image, with finer detail added as it becomes
683 * available. If you do not want this "chunky" display, you may pass
684 * NULL for display_row. If you do not want the sparkle display, and
685 * you have not called png_handle_alpha(), you may pass NULL for rows.
686 * If you have called png_handle_alpha(), and the image has either an
687 * alpha channel or a transparency chunk, you must provide a buffer for
688 * rows. In this case, you do not have to provide a display_row buffer
689 * also, but you may. If the image is not interlaced, or if you have
690 * not called png_set_interlace_handling(), the display_row buffer will
691 * be ignored, so pass NULL to it.
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500692 *
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500693 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.6j.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600694 */
Guy Schalnat6d764711995-12-19 03:22:19 -0600695
Guy Schalnat0d580581995-07-20 02:43:20 -0500696void
Guy Schalnat6d764711995-12-19 03:22:19 -0600697png_read_rows(png_structp png_ptr, png_bytepp row,
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600698 png_bytepp display_row, png_uint_32 num_rows)
Guy Schalnat0d580581995-07-20 02:43:20 -0500699{
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600700 png_uint_32 i;
701 png_bytepp rp;
702 png_bytepp dp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500703
Andreas Dilger47a0c421997-05-16 02:46:07 -0500704 png_debug(1, "in png_read_rows\n");
705 /* save jump buffer and error functions */
Guy Schalnat0f716451995-11-28 11:22:13 -0600706 rp = row;
707 dp = display_row;
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500708 if (rp != NULL && dp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500709 for (i = 0; i < num_rows; i++)
710 {
711 png_bytep rptr = *rp++;
712 png_bytep dptr = *dp++;
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -0600713
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500714 png_read_row(png_ptr, rptr, dptr);
715 }
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500716 else if(rp != NULL)
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500717 for (i = 0; i < num_rows; i++)
718 {
Glenn Randers-Pehrson8686fff1998-05-21 09:27:50 -0500719 png_bytep rptr = *rp;
720 png_read_row(png_ptr, rptr, NULL);
721 rp++;
722 }
723 else if(dp != NULL)
724 for (i = 0; i < num_rows; i++)
725 {
726 png_bytep dptr = *dp;
727 png_read_row(png_ptr, NULL, dptr);
728 dp++;
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -0500729 }
Guy Schalnat0d580581995-07-20 02:43:20 -0500730}
731
Andreas Dilger47a0c421997-05-16 02:46:07 -0500732/* Read the entire image. If the image has an alpha channel or a tRNS
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500733 * chunk, and you have called png_handle_alpha()[*], you will need to
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600734 * initialize the image to the current image that PNG will be overlaying.
735 * We set the num_rows again here, in case it was incorrectly set in
736 * png_read_start_row() by a call to png_read_update_info() or
737 * png_start_read_image() if png_set_interlace_handling() wasn't called
738 * prior to either of these functions like it should have been. You can
739 * only call this function once. If you desire to have an image for
740 * each pass of a interlaced image, use png_read_rows() instead.
Glenn Randers-Pehrson345bc271998-06-14 14:43:31 -0500741 *
Glenn Randers-Pehrson98c9d732000-05-03 21:06:11 -0500742 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.6j.
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600743 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500744void
Guy Schalnat6d764711995-12-19 03:22:19 -0600745png_read_image(png_structp png_ptr, png_bytepp image)
Guy Schalnat0d580581995-07-20 02:43:20 -0500746{
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500747 png_uint_32 i,image_height;
Guy Schalnat0d580581995-07-20 02:43:20 -0500748 int pass, j;
Guy Schalnat4ee97b01996-01-16 01:51:56 -0600749 png_bytepp rp;
Guy Schalnat0d580581995-07-20 02:43:20 -0500750
Andreas Dilger47a0c421997-05-16 02:46:07 -0500751 png_debug(1, "in png_read_image\n");
752 /* save jump buffer and error functions */
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500753
754#ifdef PNG_READ_INTERLACING_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -0500755 pass = png_set_interlace_handling(png_ptr);
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -0500756#else
757 if (png_ptr->interlaced)
758 png_error(png_ptr,
759 "Cannot read interlaced image -- interlace handler disabled.");
760 pass = 1;
761#endif
762
Guy Schalnate5a37791996-06-05 15:50:50 -0500763
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500764 image_height=png_ptr->height;
765 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
Guy Schalnate5a37791996-06-05 15:50:50 -0500766
Guy Schalnat0d580581995-07-20 02:43:20 -0500767 for (j = 0; j < pass; j++)
768 {
769 rp = image;
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -0500770 for (i = 0; i < image_height; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -0500771 {
772 png_read_row(png_ptr, *rp, NULL);
773 rp++;
774 }
775 }
776}
777
Andreas Dilger47a0c421997-05-16 02:46:07 -0500778/* Read the end of the PNG file. Will not read past the end of the
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600779 * file, will verify the end is accurate, and will read any comments
780 * or time information at the end of the file, if info is not NULL.
781 */
Guy Schalnat0d580581995-07-20 02:43:20 -0500782void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600783png_read_end(png_structp png_ptr, png_infop info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -0500784{
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600785 png_byte chunk_length[4];
Guy Schalnat0d580581995-07-20 02:43:20 -0500786 png_uint_32 length;
Guy Schalnat0d580581995-07-20 02:43:20 -0500787
Andreas Dilger47a0c421997-05-16 02:46:07 -0500788 png_debug(1, "in png_read_end\n");
789 /* save jump buffer and error functions */
790 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
Guy Schalnat0d580581995-07-20 02:43:20 -0500791
792 do
793 {
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600794#ifdef PNG_USE_LOCAL_ARRAYS
795 PNG_IHDR;
796 PNG_IDAT;
797 PNG_IEND;
798 PNG_PLTE;
799#if defined(PNG_READ_bKGD_SUPPORTED)
800 PNG_bKGD;
801#endif
802#if defined(PNG_READ_cHRM_SUPPORTED)
803 PNG_cHRM;
804#endif
805#if defined(PNG_READ_gAMA_SUPPORTED)
806 PNG_gAMA;
807#endif
808#if defined(PNG_READ_hIST_SUPPORTED)
809 PNG_hIST;
810#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600811#if defined(PNG_READ_iCCP_SUPPORTED)
812 PNG_iCCP;
813#endif
814#if defined(PNG_READ_iTXt_SUPPORTED)
815 PNG_iTXt;
816#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600817#if defined(PNG_READ_oFFs_SUPPORTED)
818 PNG_oFFs;
819#endif
820#if defined(PNG_READ_pCAL_SUPPORTED)
821 PNG_pCAL;
822#endif
823#if defined(PNG_READ_pHYs_SUPPORTED)
824 PNG_pHYs;
825#endif
826#if defined(PNG_READ_sBIT_SUPPORTED)
827 PNG_sBIT;
828#endif
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600829#if defined(PNG_READ_sCAL_SUPPORTED)
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600830 PNG_sCAL;
831#endif
832#if defined(PNG_READ_sPLT_SUPPORTED)
833 PNG_sPLT;
834#endif
Glenn Randers-Pehrson074af5e1999-11-28 23:32:18 -0600835#if defined(PNG_READ_sRGB_SUPPORTED)
836 PNG_sRGB;
837#endif
838#if defined(PNG_READ_tEXt_SUPPORTED)
839 PNG_tEXt;
840#endif
841#if defined(PNG_READ_tIME_SUPPORTED)
842 PNG_tIME;
843#endif
844#if defined(PNG_READ_tRNS_SUPPORTED)
845 PNG_tRNS;
846#endif
847#if defined(PNG_READ_zTXt_SUPPORTED)
848 PNG_zTXt;
849#endif
850#endif /* PNG_GLOBAL_ARRAYS */
851
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600852 png_read_data(png_ptr, chunk_length, 4);
853 length = png_get_uint_32(chunk_length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500854
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600855 png_reset_crc(png_ptr);
856 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
857
Andreas Dilger47a0c421997-05-16 02:46:07 -0500858 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
859
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600860 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
861 png_handle_IHDR(png_ptr, info_ptr, length);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -0600862 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
863 png_handle_IEND(png_ptr, info_ptr, length);
864#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
865 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
866 {
867 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
868 {
869 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
870 png_error(png_ptr, "Too many IDAT's found");
871 }
872 else
873 png_ptr->mode |= PNG_AFTER_IDAT;
874 png_handle_unknown(png_ptr, info_ptr, length);
875 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
876 png_ptr->mode |= PNG_HAVE_PLTE;
877 }
878#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600879 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
Guy Schalnat0d580581995-07-20 02:43:20 -0500880 {
Andreas Dilger47a0c421997-05-16 02:46:07 -0500881 /* Zero length IDATs are legal after the last IDAT has been
882 * read, but not after other chunks have been read.
883 */
Guy Schalnate5a37791996-06-05 15:50:50 -0500884 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
885 png_error(png_ptr, "Too many IDAT's found");
Glenn Randers-Pehrson4accabb2000-04-14 14:20:47 -0500886 png_crc_finish(png_ptr, length);
Guy Schalnat0d580581995-07-20 02:43:20 -0500887 }
Andreas Dilger47a0c421997-05-16 02:46:07 -0500888 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
889 png_handle_PLTE(png_ptr, info_ptr, length);
Andreas Dilger47a0c421997-05-16 02:46:07 -0500890#if defined(PNG_READ_bKGD_SUPPORTED)
891 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
892 png_handle_bKGD(png_ptr, info_ptr, length);
893#endif
894#if defined(PNG_READ_cHRM_SUPPORTED)
895 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
896 png_handle_cHRM(png_ptr, info_ptr, length);
897#endif
898#if defined(PNG_READ_gAMA_SUPPORTED)
899 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
900 png_handle_gAMA(png_ptr, info_ptr, length);
901#endif
902#if defined(PNG_READ_hIST_SUPPORTED)
903 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
904 png_handle_hIST(png_ptr, info_ptr, length);
905#endif
906#if defined(PNG_READ_oFFs_SUPPORTED)
907 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
908 png_handle_oFFs(png_ptr, info_ptr, length);
909#endif
910#if defined(PNG_READ_pCAL_SUPPORTED)
911 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
912 png_handle_pCAL(png_ptr, info_ptr, length);
913#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600914#if defined(PNG_READ_sCAL_SUPPORTED)
915 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
916 png_handle_sCAL(png_ptr, info_ptr, length);
917#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500918#if defined(PNG_READ_pHYs_SUPPORTED)
919 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
920 png_handle_pHYs(png_ptr, info_ptr, length);
921#endif
922#if defined(PNG_READ_sBIT_SUPPORTED)
923 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
924 png_handle_sBIT(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500925#endif
Glenn Randers-Pehrsonb6ce43d1998-01-01 07:13:13 -0600926#if defined(PNG_READ_sRGB_SUPPORTED)
927 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
928 png_handle_sRGB(png_ptr, info_ptr, length);
929#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600930#if defined(PNG_READ_iCCP_SUPPORTED)
931 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
932 png_handle_iCCP(png_ptr, info_ptr, length);
933#endif
934#if defined(PNG_READ_sPLT_SUPPORTED)
935 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
936 png_handle_sPLT(png_ptr, info_ptr, length);
937#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500938#if defined(PNG_READ_tEXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600939 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
940 png_handle_tEXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500941#endif
Andreas Dilger47a0c421997-05-16 02:46:07 -0500942#if defined(PNG_READ_tIME_SUPPORTED)
943 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
944 png_handle_tIME(png_ptr, info_ptr, length);
945#endif
946#if defined(PNG_READ_tRNS_SUPPORTED)
947 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
948 png_handle_tRNS(png_ptr, info_ptr, length);
949#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500950#if defined(PNG_READ_zTXt_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600951 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
952 png_handle_zTXt(png_ptr, info_ptr, length);
Guy Schalnat51f0eb41995-09-26 05:22:39 -0500953#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600954#if defined(PNG_READ_iTXt_SUPPORTED)
955 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
956 png_handle_iTXt(png_ptr, info_ptr, length);
957#endif
Guy Schalnat0d580581995-07-20 02:43:20 -0500958 else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600959 png_handle_unknown(png_ptr, info_ptr, length);
960 } while (!(png_ptr->mode & PNG_HAVE_IEND));
Guy Schalnat0d580581995-07-20 02:43:20 -0500961}
962
963/* free all memory used by the read */
964void
Guy Schalnate5a37791996-06-05 15:50:50 -0500965png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600966 png_infopp end_info_ptr_ptr)
Guy Schalnate5a37791996-06-05 15:50:50 -0500967{
968 png_structp png_ptr = NULL;
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600969 png_infop info_ptr = NULL, end_info_ptr = NULL;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500970#ifdef PNG_USER_MEM_SUPPORTED
971 png_free_ptr free_fn = NULL;
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600972#endif
Guy Schalnate5a37791996-06-05 15:50:50 -0500973
Andreas Dilger47a0c421997-05-16 02:46:07 -0500974 png_debug(1, "in png_destroy_read_struct\n");
975 /* save jump buffer and error functions */
976 if (png_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500977 png_ptr = *png_ptr_ptr;
978
Andreas Dilger47a0c421997-05-16 02:46:07 -0500979 if (info_ptr_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500980 info_ptr = *info_ptr_ptr;
981
Andreas Dilger47a0c421997-05-16 02:46:07 -0500982 if (end_info_ptr_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600983 end_info_ptr = *end_info_ptr_ptr;
Guy Schalnate5a37791996-06-05 15:50:50 -0500984
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500985#ifdef PNG_USER_MEM_SUPPORTED
986 free_fn = png_ptr->free_fn;
987#endif
988
Andreas Dilger02ad0ef1997-01-17 01:34:35 -0600989 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
Guy Schalnate5a37791996-06-05 15:50:50 -0500990
Andreas Dilger47a0c421997-05-16 02:46:07 -0500991 if (info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -0500992 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -0600993#if defined(PNG_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -0600994 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -0600995#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -0500996
997#ifdef PNG_USER_MEM_SUPPORTED
998 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
999#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001000 png_destroy_struct((png_voidp)info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001001#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001002 *info_ptr_ptr = (png_infop)NULL;
1003 }
1004
Andreas Dilger47a0c421997-05-16 02:46:07 -05001005 if (end_info_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001006 {
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001007#if defined(PNG_READ_TEXT_SUPPORTED)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001008 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
Glenn Randers-Pehrsonc4a2ae61998-01-16 22:06:18 -06001009#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001010#ifdef PNG_USER_MEM_SUPPORTED
1011 png_destroy_struct_2((png_voidp)end_info_ptr, free_fn);
1012#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001013 png_destroy_struct((png_voidp)end_info_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001014#endif
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001015 *end_info_ptr_ptr = (png_infop)NULL;
Guy Schalnate5a37791996-06-05 15:50:50 -05001016 }
1017
Andreas Dilger47a0c421997-05-16 02:46:07 -05001018 if (png_ptr != NULL)
Guy Schalnate5a37791996-06-05 15:50:50 -05001019 {
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001020#ifdef PNG_USER_MEM_SUPPORTED
1021 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
1022#else
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001023 png_destroy_struct((png_voidp)png_ptr);
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001024#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001025 *png_ptr_ptr = (png_structp)NULL;
1026 }
1027}
1028
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001029/* free all memory used by the read (old method) */
Guy Schalnate5a37791996-06-05 15:50:50 -05001030void
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001031png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
Guy Schalnat0d580581995-07-20 02:43:20 -05001032{
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001033#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat0d580581995-07-20 02:43:20 -05001034 jmp_buf tmp_jmp;
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001035#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001036 png_error_ptr error_fn;
1037 png_error_ptr warning_fn;
1038 png_voidp error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001039#ifdef PNG_USER_MEM_SUPPORTED
1040 png_free_ptr free_fn;
1041#endif
Guy Schalnat0d580581995-07-20 02:43:20 -05001042
Andreas Dilger47a0c421997-05-16 02:46:07 -05001043 png_debug(1, "in png_read_destroy\n");
1044 /* save jump buffer and error functions */
1045 if (info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001046 png_info_destroy(png_ptr, info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001047
Andreas Dilger47a0c421997-05-16 02:46:07 -05001048 if (end_info_ptr != NULL)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001049 png_info_destroy(png_ptr, end_info_ptr);
Guy Schalnat0d580581995-07-20 02:43:20 -05001050
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001051 png_free(png_ptr, png_ptr->zbuf);
1052 png_free(png_ptr, png_ptr->row_buf);
1053 png_free(png_ptr, png_ptr->prev_row);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001054#if defined(PNG_READ_DITHER_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001055 png_free(png_ptr, png_ptr->palette_lookup);
1056 png_free(png_ptr, png_ptr->dither_index);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001057#endif
1058#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001059 png_free(png_ptr, png_ptr->gamma_table);
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001060#endif
1061#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001062 png_free(png_ptr, png_ptr->gamma_from_1);
1063 png_free(png_ptr, png_ptr->gamma_to_1);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001064#endif
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001065#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001066 if (png_ptr->free_me & PNG_FREE_PLTE)
Glenn Randers-Pehrson46f61e21998-01-30 21:45:12 -06001067 png_zfree(png_ptr, png_ptr->palette);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001068 png_ptr->free_me &= ~PNG_FREE_PLTE;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001069#else
1070 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1071 png_zfree(png_ptr, png_ptr->palette);
1072 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1073#endif
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001074#if defined(PNG_tRNS_SUPPORTED) || \
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001075 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001076#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001077 if (png_ptr->free_me & PNG_FREE_TRNS)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001078 png_free(png_ptr, png_ptr->trans);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001079 png_ptr->free_me &= ~PNG_FREE_TRNS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001080#else
1081 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1082 png_free(png_ptr, png_ptr->trans);
1083 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1084#endif
Glenn Randers-Pehrson4393a9a1999-09-17 12:27:26 -05001085#endif
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001086#if defined(PNG_READ_hIST_SUPPORTED)
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001087#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001088 if (png_ptr->free_me & PNG_FREE_HIST)
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001089 png_free(png_ptr, png_ptr->hist);
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001090 png_ptr->free_me &= ~PNG_FREE_HIST;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001091#else
1092 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1093 png_free(png_ptr, png_ptr->hist);
1094 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1095#endif
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001096#endif
1097#if defined(PNG_READ_GAMMA_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001098 if (png_ptr->gamma_16_table != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001099 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001100 int i;
1101 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001102 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001103 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001104 png_free(png_ptr, png_ptr->gamma_16_table[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001105 }
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001106 png_free(png_ptr, png_ptr->gamma_16_table);
Guy Schalnat0d580581995-07-20 02:43:20 -05001107 }
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001108#if defined(PNG_READ_BACKGROUND_SUPPORTED)
Andreas Dilger47a0c421997-05-16 02:46:07 -05001109 if (png_ptr->gamma_16_from_1 != NULL)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001110 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001111 int i;
1112 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001113 for (i = 0; i < istop; i++)
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001114 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001115 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001116 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001117 png_free(png_ptr, png_ptr->gamma_16_from_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001118 }
Andreas Dilger47a0c421997-05-16 02:46:07 -05001119 if (png_ptr->gamma_16_to_1 != NULL)
Guy Schalnat0d580581995-07-20 02:43:20 -05001120 {
Glenn Randers-Pehrson1d963611998-05-02 12:52:25 -05001121 int i;
1122 int istop = (1 << (8 - png_ptr->gamma_shift));
Glenn Randers-Pehrson896239b1998-04-21 15:03:57 -05001123 for (i = 0; i < istop; i++)
Guy Schalnat0d580581995-07-20 02:43:20 -05001124 {
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001125 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
Guy Schalnat0d580581995-07-20 02:43:20 -05001126 }
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001127 png_free(png_ptr, png_ptr->gamma_16_to_1);
Glenn Randers-Pehrson5c6aeb21998-12-29 11:47:59 -06001128 }
Guy Schalnat51f0eb41995-09-26 05:22:39 -05001129#endif
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001130#endif
1131#if defined(PNG_TIME_RFC1123_SUPPORTED)
1132 png_free(png_ptr, png_ptr->time_buffer);
Glenn Randers-Pehrson166c5a31999-12-10 09:43:02 -06001133#endif
Guy Schalnat0f716451995-11-28 11:22:13 -06001134
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001135 inflateEnd(&png_ptr->zstream);
Guy Schalnat6d764711995-12-19 03:22:19 -06001136#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
Andreas Dilger02ad0ef1997-01-17 01:34:35 -06001137 png_free(png_ptr, png_ptr->save_buffer);
Guy Schalnat6d764711995-12-19 03:22:19 -06001138#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001139
1140 /* Save the important info out of the png_struct, in case it is
1141 * being used again.
1142 */
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001143#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001144 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001145#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001146
1147 error_fn = png_ptr->error_fn;
1148 warning_fn = png_ptr->warning_fn;
1149 error_ptr = png_ptr->error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001150#ifdef PNG_USER_MEM_SUPPORTED
1151 free_fn = png_ptr->free_fn;
1152#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001153
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001154 png_memset(png_ptr, 0, sizeof (png_struct));
Guy Schalnate5a37791996-06-05 15:50:50 -05001155
1156 png_ptr->error_fn = error_fn;
1157 png_ptr->warning_fn = warning_fn;
1158 png_ptr->error_ptr = error_ptr;
Glenn Randers-Pehrsonf7d1a171998-06-06 15:31:35 -05001159#ifdef PNG_USER_MEM_SUPPORTED
1160 png_ptr->free_fn = free_fn;
1161#endif
Guy Schalnate5a37791996-06-05 15:50:50 -05001162
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001163#ifdef PNG_SETJMP_SUPPORTED
Guy Schalnat4ee97b01996-01-16 01:51:56 -06001164 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001165#endif
1166
Guy Schalnat0d580581995-07-20 02:43:20 -05001167}
Glenn Randers-Pehrson08a33431998-03-07 06:06:55 -06001168
1169void
1170png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1171{
1172 png_ptr->read_row_fn = read_row_fn;
1173}
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001174
1175#if defined(PNG_INFO_IMAGE_SUPPORTED)
1176void png_read_png(png_structp png_ptr, png_infop info_ptr,
1177 int transforms,
1178 voidp params)
1179{
1180 int row;
1181
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001182#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1183 /* invert the alpha channel from opacity to transparency */
1184 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1185 png_set_invert_alpha(png_ptr);
1186#endif
1187
1188 /* The call to png_read_info() gives us all of the information from the
1189 * PNG file before the first IDAT (image data chunk).
1190 */
1191 png_read_info(png_ptr, info_ptr);
1192
1193 /* -------------- image transformations start here ------------------- */
1194
1195#if defined(PNG_READ_16_TO_8_SUPPORTED)
1196 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
1197 if (transforms & PNG_TRANSFORM_STRIP_16)
1198 png_set_strip_16(png_ptr);
1199#endif
1200
1201#if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1202 /* Strip alpha bytes from the input data without combining with the
1203 * background (not recommended).
1204 */
1205 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1206 png_set_strip_alpha(png_ptr);
1207#endif
1208
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001209#if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001210 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
1211 * byte into separate bytes (useful for paletted and grayscale images).
1212 */
1213 if (transforms & PNG_TRANSFORM_PACKING)
1214 png_set_packing(png_ptr);
1215#endif
1216
1217#if defined(PNG_READ_PACKSWAP_SUPPORTED)
1218 /* Change the order of packed pixels to least significant bit first
1219 * (not useful if you are using png_set_packing). */
1220 if (transforms & PNG_TRANSFORM_PACKSWAP)
1221 png_set_packswap(png_ptr);
1222#endif
1223
1224#if defined(PNG_READ_EXPAND_SUPPORTED)
1225 /* Expand paletted colors into true RGB triplets
1226 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1227 * Expand paletted or RGB images with transparency to full alpha
1228 * channels so the data will be available as RGBA quartets.
1229 */
1230 if (transforms & PNG_TRANSFORM_EXPAND)
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001231 if ((png_ptr->bit_depth < 8) ||
1232 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1233 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1234 png_set_expand(png_ptr);
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001235#endif
1236
1237 /* We don't handle background color or gamma transformation or dithering. */
1238
1239#if defined(PNG_READ_INVERT_SUPPORTED)
1240 /* invert monochrome files to have 0 as white and 1 as black */
1241 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1242 png_set_invert_mono(png_ptr);
1243#endif
1244
1245#if defined(PNG_READ_SHIFT_SUPPORTED)
1246 /* If you want to shift the pixel values from the range [0,255] or
1247 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1248 * colors were originally in:
1249 */
1250 if ((transforms & PNG_TRANSFORM_SHIFT)
1251 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1252 {
1253 png_color_8p sig_bit;
1254
1255 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1256 png_set_shift(png_ptr, sig_bit);
1257 }
1258#endif
1259
1260#if defined(PNG_READ_BGR_SUPPORTED)
1261 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
1262 if (transforms & PNG_TRANSFORM_BGR)
1263 png_set_bgr(png_ptr);
1264#endif
1265
1266#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1267 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1268 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1269 png_set_swap_alpha(png_ptr);
1270#endif
1271
1272#if defined(PNG_READ_SWAP_SUPPORTED)
1273 /* swap bytes of 16 bit files to least significant byte first */
1274 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1275 png_set_swap(png_ptr);
1276#endif
1277
1278 /* We don't handle adding filler bytes */
1279
1280 /* Optional call to gamma correct and add the background to the palette
1281 * and update info structure. REQUIRED if you are expecting libpng to
1282 * update the palette for you (ie you selected such a transform above).
1283 */
1284 png_read_update_info(png_ptr, info_ptr);
1285
1286 /* -------------- image transformations end here ------------------- */
1287
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001288 if(info_ptr->row_pointers)
1289 {
1290#ifdef PNG_FREE_ME_SUPPORTED
1291 if(info_ptr->free_me & PNG_FREE_ROWS)
1292 {
1293 for (row = 0; row < (int)info_ptr->height; row++)
1294 png_free(png_ptr, info_ptr->row_pointers[row]);
1295 png_free(png_ptr, info_ptr->row_pointers);
1296 info_ptr->row_pointers = NULL;
1297 }
1298#endif
1299 }
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001300 if(info_ptr->row_pointers == NULL)
1301 {
1302 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001303 info_ptr->height * sizeof(png_bytep));
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001304#ifdef PNG_FREE_ME_SUPPORTED
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001305 info_ptr->free_me |= PNG_FREE_ROWS;
Glenn Randers-Pehrson6942d532000-05-01 09:31:54 -05001306#endif
1307 for (row = 0; row < (int)info_ptr->height; row++)
1308 info_ptr->row_pointers[row] = png_malloc(png_ptr,
1309 png_get_rowbytes(png_ptr, info_ptr));
Glenn Randers-Pehrsona77ef622000-02-18 13:48:52 -06001310 }
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001311
1312 png_read_image(png_ptr, info_ptr->row_pointers);
1313 info_ptr->valid |= PNG_INFO_IDAT;
1314
1315 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1316 png_read_end(png_ptr, info_ptr);
Glenn Randers-Pehrson520a7642000-03-21 05:13:06 -06001317
1318 if(transforms == 0 || params == (voidp)NULL)
1319 /* quiet compiler warnings */ return;
1320
Glenn Randers-Pehrson61c32d92000-02-04 23:40:16 -06001321}
1322#endif