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