source: liacs/MIR2010/SourceCode/cximage/png/pngread.c@ 405

Last change on this file since 405 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

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