source: trunk/src/3rdparty/libpng/pngread.c

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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