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