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