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