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