VirtualBox

source: vbox/trunk/src/libs/libpng-1.2.54/pngread.c@ 75439

最後變更 在這個檔案從75439是 58796,由 vboxsync 提交於 9 年 前

libpng 1.2.54 unmodified

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette