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