define __KERNEL_STRICT_NAMES to avoid inclusion of kernel types on systems that carry...
[cake.git] / compiler / libpng / pngread.c
blob1d1b6ec91e9d3deab5fd97f680991445ab7cb96e
2 /* pngread.c - read a PNG file
4 * libpng 1.0.10 - March 30, 2001
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1998-2001 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.)
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
14 #define PNG_INTERNAL
15 #include "png.h"
17 /* Create a PNG structure for reading, and allocate any memory needed. */
18 png_structp PNGAPI
19 png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr,
20 png_error_ptr error_fn, png_error_ptr warn_fn)
23 #ifdef PNG_USER_MEM_SUPPORTED
24 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
25 warn_fn, NULL, NULL, NULL));
28 /* Alternate create PNG structure for reading, and allocate any memory needed. */
29 png_structp PNGAPI
30 png_create_read_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
31 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
32 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
34 #endif /* PNG_USER_MEM_SUPPORTED */
36 png_structp png_ptr;
38 #ifdef PNG_SETJMP_SUPPORTED
39 #ifdef USE_FAR_KEYWORD
40 jmp_buf jmpbuf;
41 #endif
42 #endif
44 int i;
46 png_debug(1, "in png_create_read_struct\n");
47 #ifdef PNG_USER_MEM_SUPPORTED
48 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
49 (png_malloc_ptr)malloc_fn)) == NULL)
50 #else
51 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
52 #endif
54 return (png_structp)NULL;
57 #ifdef PNG_SETJMP_SUPPORTED
58 #ifdef USE_FAR_KEYWORD
59 if (setjmp(jmpbuf))
60 #else
61 if (setjmp(png_ptr->jmpbuf))
62 #endif
64 png_free(png_ptr, png_ptr->zbuf);
65 png_ptr->zbuf=NULL;
66 png_destroy_struct(png_ptr);
67 return (png_structp)NULL;
69 #ifdef USE_FAR_KEYWORD
70 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
71 #endif
72 #endif
74 #ifdef PNG_USER_MEM_SUPPORTED
75 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
76 #endif
78 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
80 i=0;
83 if(user_png_ver[i] != png_libpng_ver[i])
84 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
85 } while (png_libpng_ver[i++]);
87 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
89 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
90 * we must recompile any applications that use any older library version.
91 * For versions after libpng 1.0, we will be compatible, so we need
92 * only check the first digit.
94 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
95 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
97 png_error(png_ptr,
98 "Incompatible libpng version in application and library");
101 /* Libpng 1.0.6 was not binary compatible, due to insertion of the
102 info_ptr->free_me member. Note to maintainer: this test can be
103 removed from version 2.0.0 and beyond because the previous test
104 would have already rejected it. */
106 if (user_png_ver[4] == '6' && user_png_ver[2] == '0' &&
107 user_png_ver[0] == '1' && user_png_ver[5] == '\0')
109 png_error(png_ptr,
110 "Application must be recompiled; version 1.0.6 was incompatible");
114 /* initialize zbuf - compression buffer */
115 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
116 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
117 (png_uint_32)png_ptr->zbuf_size);
118 png_ptr->zstream.zalloc = png_zalloc;
119 png_ptr->zstream.zfree = png_zfree;
120 png_ptr->zstream.opaque = (voidpf)png_ptr;
122 switch (inflateInit(&png_ptr->zstream))
124 case Z_OK: /* Do nothing */ break;
125 case Z_MEM_ERROR:
126 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
127 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
128 default: png_error(png_ptr, "Unknown zlib error");
131 png_ptr->zstream.next_out = png_ptr->zbuf;
132 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
134 png_set_read_fn(png_ptr, NULL, NULL);
136 return (png_ptr);
139 /* Initialize PNG structure for reading, and allocate any memory needed.
140 This interface is deprecated in favour of the png_create_read_struct(),
141 and it will eventually disappear. */
142 #undef png_read_init
143 void PNGAPI
144 png_read_init(png_structp png_ptr)
146 /* We only come here via pre-1.0.7-compiled applications */
147 png_read_init_2(png_ptr, "1.0.0", 10000, 10000);
150 void PNGAPI
151 png_read_init_2(png_structp png_ptr, png_const_charp user_png_ver,
152 png_size_t png_struct_size, png_size_t png_info_size)
154 #ifdef PNG_SETJMP_SUPPORTED
155 jmp_buf tmp_jmp; /* to save current jump buffer */
156 #endif
158 int i=0;
161 if(user_png_ver[i] != png_libpng_ver[i])
163 #ifdef PNG_LEGACY_SUPPORTED
164 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
165 #else
166 png_ptr->error_fn=(png_error_ptr)NULL;
167 png_error(png_ptr,
168 "Application uses deprecated png_read_init() and must be recompiled.");
169 #endif
171 } while (png_libpng_ver[i++]);
173 if(sizeof(png_struct) > png_struct_size ||
174 sizeof(png_info) > png_info_size)
176 png_ptr->error_fn=(png_error_ptr)NULL;
177 png_error(png_ptr,
178 "Application and library have different sized structs. Please recompile.");
181 png_debug(1, "in png_read_init_2\n");
183 #ifdef PNG_SETJMP_SUPPORTED
184 /* save jump buffer and error functions */
185 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
186 #endif
188 /* reset all variables to 0 */
189 png_memset(png_ptr, 0, sizeof (png_struct));
191 #ifdef PNG_SETJMP_SUPPORTED
192 /* restore jump buffer */
193 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
194 #endif
196 /* initialize zbuf - compression buffer */
197 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
198 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
199 (png_uint_32)png_ptr->zbuf_size);
200 png_ptr->zstream.zalloc = png_zalloc;
201 png_ptr->zstream.zfree = png_zfree;
202 png_ptr->zstream.opaque = (voidpf)png_ptr;
204 switch (inflateInit(&png_ptr->zstream))
206 case Z_OK: /* Do nothing */ break;
207 case Z_MEM_ERROR:
208 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
209 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
210 default: png_error(png_ptr, "Unknown zlib error");
213 png_ptr->zstream.next_out = png_ptr->zbuf;
214 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
216 png_set_read_fn(png_ptr, NULL, NULL);
219 /* Read the information before the actual image data. This has been
220 * changed in v0.90 to allow reading a file that already has the magic
221 * bytes read from the stream. You can tell libpng how many bytes have
222 * been read from the beginning of the stream (up to the maximum of 8)
223 * via png_set_sig_bytes(), and we will only check the remaining bytes
224 * here. The application can then have access to the signature bytes we
225 * read if it is determined that this isn't a valid PNG file.
227 void PNGAPI
228 png_read_info(png_structp png_ptr, png_infop info_ptr)
230 png_debug(1, "in png_read_info\n");
231 /* save jump buffer and error functions */
232 /* If we haven't checked all of the PNG signature bytes, do so now. */
233 if (png_ptr->sig_bytes < 8)
235 png_size_t num_checked = png_ptr->sig_bytes,
236 num_to_check = 8 - num_checked;
238 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
239 png_ptr->sig_bytes = 8;
241 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
243 if (num_checked < 4 &&
244 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
245 png_error(png_ptr, "Not a PNG file");
246 else
247 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
249 if (num_checked < 3)
250 png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
253 for(;;)
255 #ifdef PNG_USE_LOCAL_ARRAYS
256 PNG_IHDR;
257 PNG_IDAT;
258 PNG_IEND;
259 PNG_PLTE;
260 #if defined(PNG_READ_bKGD_SUPPORTED)
261 PNG_bKGD;
262 #endif
263 #if defined(PNG_READ_cHRM_SUPPORTED)
264 PNG_cHRM;
265 #endif
266 #if defined(PNG_READ_gAMA_SUPPORTED)
267 PNG_gAMA;
268 #endif
269 #if defined(PNG_READ_hIST_SUPPORTED)
270 PNG_hIST;
271 #endif
272 #if defined(PNG_READ_iCCP_SUPPORTED)
273 PNG_iCCP;
274 #endif
275 #if defined(PNG_READ_iTXt_SUPPORTED)
276 PNG_iTXt;
277 #endif
278 #if defined(PNG_READ_oFFs_SUPPORTED)
279 PNG_oFFs;
280 #endif
281 #if defined(PNG_READ_pCAL_SUPPORTED)
282 PNG_pCAL;
283 #endif
284 #if defined(PNG_READ_pHYs_SUPPORTED)
285 PNG_pHYs;
286 #endif
287 #if defined(PNG_READ_sBIT_SUPPORTED)
288 PNG_sBIT;
289 #endif
290 #if defined(PNG_READ_sCAL_SUPPORTED)
291 PNG_sCAL;
292 #endif
293 #if defined(PNG_READ_sPLT_SUPPORTED)
294 PNG_sPLT;
295 #endif
296 #if defined(PNG_READ_sRGB_SUPPORTED)
297 PNG_sRGB;
298 #endif
299 #if defined(PNG_READ_tEXt_SUPPORTED)
300 PNG_tEXt;
301 #endif
302 #if defined(PNG_READ_tIME_SUPPORTED)
303 PNG_tIME;
304 #endif
305 #if defined(PNG_READ_tRNS_SUPPORTED)
306 PNG_tRNS;
307 #endif
308 #if defined(PNG_READ_zTXt_SUPPORTED)
309 PNG_zTXt;
310 #endif
311 #endif /* PNG_GLOBAL_ARRAYS */
312 png_byte chunk_length[4];
313 png_uint_32 length;
315 png_read_data(png_ptr, chunk_length, 4);
316 length = png_get_uint_32(chunk_length);
318 png_reset_crc(png_ptr);
319 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
321 png_debug2(0, "Reading %s chunk, length=%lu.\n", png_ptr->chunk_name,
322 length);
324 /* This should be a binary subdivision search or a hash for
325 * matching the chunk name rather than a linear search.
327 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
328 png_handle_IHDR(png_ptr, info_ptr, length);
329 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
330 png_handle_IEND(png_ptr, info_ptr, length);
331 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
332 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
334 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
335 png_ptr->mode |= PNG_HAVE_IDAT;
336 png_handle_unknown(png_ptr, info_ptr, length);
337 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
338 png_ptr->mode |= PNG_HAVE_PLTE;
339 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
341 if (!(png_ptr->mode & PNG_HAVE_IHDR))
342 png_error(png_ptr, "Missing IHDR before IDAT");
343 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
344 !(png_ptr->mode & PNG_HAVE_PLTE))
345 png_error(png_ptr, "Missing PLTE before IDAT");
346 break;
349 #endif
350 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
351 png_handle_PLTE(png_ptr, info_ptr, length);
352 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
354 if (!(png_ptr->mode & PNG_HAVE_IHDR))
355 png_error(png_ptr, "Missing IHDR before IDAT");
356 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
357 !(png_ptr->mode & PNG_HAVE_PLTE))
358 png_error(png_ptr, "Missing PLTE before IDAT");
360 png_ptr->idat_size = length;
361 png_ptr->mode |= PNG_HAVE_IDAT;
362 break;
364 #if defined(PNG_READ_bKGD_SUPPORTED)
365 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
366 png_handle_bKGD(png_ptr, info_ptr, length);
367 #endif
368 #if defined(PNG_READ_cHRM_SUPPORTED)
369 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
370 png_handle_cHRM(png_ptr, info_ptr, length);
371 #endif
372 #if defined(PNG_READ_gAMA_SUPPORTED)
373 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
374 png_handle_gAMA(png_ptr, info_ptr, length);
375 #endif
376 #if defined(PNG_READ_hIST_SUPPORTED)
377 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
378 png_handle_hIST(png_ptr, info_ptr, length);
379 #endif
380 #if defined(PNG_READ_oFFs_SUPPORTED)
381 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
382 png_handle_oFFs(png_ptr, info_ptr, length);
383 #endif
384 #if defined(PNG_READ_pCAL_SUPPORTED)
385 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
386 png_handle_pCAL(png_ptr, info_ptr, length);
387 #endif
388 #if defined(PNG_READ_sCAL_SUPPORTED)
389 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
390 png_handle_sCAL(png_ptr, info_ptr, length);
391 #endif
392 #if defined(PNG_READ_pHYs_SUPPORTED)
393 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
394 png_handle_pHYs(png_ptr, info_ptr, length);
395 #endif
396 #if defined(PNG_READ_sBIT_SUPPORTED)
397 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
398 png_handle_sBIT(png_ptr, info_ptr, length);
399 #endif
400 #if defined(PNG_READ_sRGB_SUPPORTED)
401 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
402 png_handle_sRGB(png_ptr, info_ptr, length);
403 #endif
404 #if defined(PNG_READ_iCCP_SUPPORTED)
405 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
406 png_handle_iCCP(png_ptr, info_ptr, length);
407 #endif
408 #if defined(PNG_READ_sPLT_SUPPORTED)
409 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
410 png_handle_sPLT(png_ptr, info_ptr, length);
411 #endif
412 #if defined(PNG_READ_tEXt_SUPPORTED)
413 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
414 png_handle_tEXt(png_ptr, info_ptr, length);
415 #endif
416 #if defined(PNG_READ_tIME_SUPPORTED)
417 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
418 png_handle_tIME(png_ptr, info_ptr, length);
419 #endif
420 #if defined(PNG_READ_tRNS_SUPPORTED)
421 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
422 png_handle_tRNS(png_ptr, info_ptr, length);
423 #endif
424 #if defined(PNG_READ_zTXt_SUPPORTED)
425 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
426 png_handle_zTXt(png_ptr, info_ptr, length);
427 #endif
428 #if defined(PNG_READ_iTXt_SUPPORTED)
429 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
430 png_handle_iTXt(png_ptr, info_ptr, length);
431 #endif
432 else
433 png_handle_unknown(png_ptr, info_ptr, length);
437 /* optional call to update the users info_ptr structure */
438 void PNGAPI
439 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
441 png_debug(1, "in png_read_update_info\n");
442 /* save jump buffer and error functions */
443 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
444 png_read_start_row(png_ptr);
445 else
446 png_warning(png_ptr,
447 "Ignoring extra png_read_update_info() call; row buffer not reallocated");
448 png_read_transform_info(png_ptr, info_ptr);
451 /* Initialize palette, background, etc, after transformations
452 * are set, but before any reading takes place. This allows
453 * the user to obtain a gamma-corrected palette, for example.
454 * If the user doesn't call this, we will do it ourselves.
456 void PNGAPI
457 png_start_read_image(png_structp png_ptr)
459 png_debug(1, "in png_start_read_image\n");
460 /* save jump buffer and error functions */
461 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
462 png_read_start_row(png_ptr);
465 void PNGAPI
466 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
468 #ifdef PNG_USE_LOCAL_ARRAYS
469 PNG_IDAT;
470 const int png_pass_dsp_mask[7] = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
471 const int png_pass_mask[7] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
472 #endif
473 int ret;
474 png_debug2(1, "in png_read_row (row %lu, pass %d)\n",
475 png_ptr->row_number, png_ptr->pass);
476 /* save jump buffer and error functions */
477 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
478 png_read_start_row(png_ptr);
479 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
481 /* check for transforms that have been set but were defined out */
482 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
483 if (png_ptr->transformations & PNG_INVERT_MONO)
484 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
485 #endif
486 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
487 if (png_ptr->transformations & PNG_FILLER)
488 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
489 #endif
490 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
491 if (png_ptr->transformations & PNG_PACKSWAP)
492 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
493 #endif
494 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
495 if (png_ptr->transformations & PNG_PACK)
496 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
497 #endif
498 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
499 if (png_ptr->transformations & PNG_SHIFT)
500 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
501 #endif
502 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
503 if (png_ptr->transformations & PNG_BGR)
504 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
505 #endif
506 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
507 if (png_ptr->transformations & PNG_SWAP_BYTES)
508 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
509 #endif
512 #if defined(PNG_READ_INTERLACING_SUPPORTED)
513 /* if interlaced and we do not need a new row, combine row and return */
514 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
516 switch (png_ptr->pass)
518 case 0:
519 if (png_ptr->row_number & 0x07)
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;
527 break;
528 case 1:
529 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
531 if (dsp_row != NULL)
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;
537 break;
538 case 2:
539 if ((png_ptr->row_number & 0x07) != 4)
541 if (dsp_row != NULL && (png_ptr->row_number & 4))
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;
547 break;
548 case 3:
549 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
551 if (dsp_row != NULL)
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;
557 break;
558 case 4:
559 if ((png_ptr->row_number & 3) != 2)
561 if (dsp_row != NULL && (png_ptr->row_number & 2))
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;
567 break;
568 case 5:
569 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
571 if (dsp_row != NULL)
572 png_combine_row(png_ptr, dsp_row,
573 png_pass_dsp_mask[png_ptr->pass]);
574 png_read_finish_row(png_ptr);
575 return;
577 break;
578 case 6:
579 if (!(png_ptr->row_number & 1))
581 png_read_finish_row(png_ptr);
582 return;
584 break;
587 #endif
589 if (!(png_ptr->mode & PNG_HAVE_IDAT))
590 png_error(png_ptr, "Invalid attempt to read row data");
592 png_ptr->zstream.next_out = png_ptr->row_buf;
593 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
596 if (!(png_ptr->zstream.avail_in))
598 while (!png_ptr->idat_size)
600 png_byte chunk_length[4];
602 png_crc_finish(png_ptr, 0);
604 png_read_data(png_ptr, chunk_length, 4);
605 png_ptr->idat_size = png_get_uint_32(chunk_length);
607 png_reset_crc(png_ptr);
608 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
609 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
610 png_error(png_ptr, "Not enough image data");
612 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
613 png_ptr->zstream.next_in = png_ptr->zbuf;
614 if (png_ptr->zbuf_size > png_ptr->idat_size)
615 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
616 png_crc_read(png_ptr, png_ptr->zbuf,
617 (png_size_t)png_ptr->zstream.avail_in);
618 png_ptr->idat_size -= png_ptr->zstream.avail_in;
620 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
621 if (ret == Z_STREAM_END)
623 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
624 png_ptr->idat_size)
625 png_error(png_ptr, "Extra compressed data");
626 png_ptr->mode |= PNG_AFTER_IDAT;
627 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
628 break;
630 if (ret != Z_OK)
631 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
632 "Decompression error");
634 } while (png_ptr->zstream.avail_out);
636 png_ptr->row_info.color_type = png_ptr->color_type;
637 png_ptr->row_info.width = png_ptr->iwidth;
638 png_ptr->row_info.channels = png_ptr->channels;
639 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
640 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
641 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
642 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
644 if(png_ptr->row_buf[0])
645 png_read_filter_row(png_ptr, &(png_ptr->row_info),
646 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
647 (int)(png_ptr->row_buf[0]));
649 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
650 png_ptr->rowbytes + 1);
652 #if defined(PNG_MNG_FEATURES_SUPPORTED)
653 if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
654 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
656 /* Intrapixel differencing */
657 png_do_read_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
659 #endif
661 if (png_ptr->transformations)
662 png_do_read_transformations(png_ptr);
664 #if defined(PNG_READ_INTERLACING_SUPPORTED)
665 /* blow up interlaced rows to full size */
666 if (png_ptr->interlaced &&
667 (png_ptr->transformations & PNG_INTERLACE))
669 if (png_ptr->pass < 6)
670 /* old interface (pre-1.0.9):
671 png_do_read_interlace(&(png_ptr->row_info),
672 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
674 png_do_read_interlace(png_ptr);
676 if (dsp_row != NULL)
677 png_combine_row(png_ptr, dsp_row,
678 png_pass_dsp_mask[png_ptr->pass]);
679 if (row != NULL)
680 png_combine_row(png_ptr, row,
681 png_pass_mask[png_ptr->pass]);
683 else
684 #endif
686 if (row != NULL)
687 png_combine_row(png_ptr, row, 0xff);
688 if (dsp_row != NULL)
689 png_combine_row(png_ptr, dsp_row, 0xff);
691 png_read_finish_row(png_ptr);
693 if (png_ptr->read_row_fn != NULL)
694 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
697 /* Read one or more rows of image data. If the image is interlaced,
698 * and png_set_interlace_handling() has been called, the rows need to
699 * contain the contents of the rows from the previous pass. If the
700 * image has alpha or transparency, and png_handle_alpha()[*] has been
701 * called, the rows contents must be initialized to the contents of the
702 * screen.
704 * "row" holds the actual image, and pixels are placed in it
705 * as they arrive. If the image is displayed after each pass, it will
706 * appear to "sparkle" in. "display_row" can be used to display a
707 * "chunky" progressive image, with finer detail added as it becomes
708 * available. If you do not want this "chunky" display, you may pass
709 * NULL for display_row. If you do not want the sparkle display, and
710 * you have not called png_handle_alpha(), you may pass NULL for rows.
711 * If you have called png_handle_alpha(), and the image has either an
712 * alpha channel or a transparency chunk, you must provide a buffer for
713 * rows. In this case, you do not have to provide a display_row buffer
714 * also, but you may. If the image is not interlaced, or if you have
715 * not called png_set_interlace_handling(), the display_row buffer will
716 * be ignored, so pass NULL to it.
718 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.10
721 void PNGAPI
722 png_read_rows(png_structp png_ptr, png_bytepp row,
723 png_bytepp display_row, png_uint_32 num_rows)
725 png_uint_32 i;
726 png_bytepp rp;
727 png_bytepp dp;
729 png_debug(1, "in png_read_rows\n");
730 /* save jump buffer and error functions */
731 rp = row;
732 dp = display_row;
733 if (rp != NULL && dp != NULL)
734 for (i = 0; i < num_rows; i++)
736 png_bytep rptr = *rp++;
737 png_bytep dptr = *dp++;
739 png_read_row(png_ptr, rptr, dptr);
741 else if(rp != NULL)
742 for (i = 0; i < num_rows; i++)
744 png_bytep rptr = *rp;
745 png_read_row(png_ptr, rptr, NULL);
746 rp++;
748 else if(dp != NULL)
749 for (i = 0; i < num_rows; i++)
751 png_bytep dptr = *dp;
752 png_read_row(png_ptr, NULL, dptr);
753 dp++;
757 /* Read the entire image. If the image has an alpha channel or a tRNS
758 * chunk, and you have called png_handle_alpha()[*], you will need to
759 * initialize the image to the current image that PNG will be overlaying.
760 * We set the num_rows again here, in case it was incorrectly set in
761 * png_read_start_row() by a call to png_read_update_info() or
762 * png_start_read_image() if png_set_interlace_handling() wasn't called
763 * prior to either of these functions like it should have been. You can
764 * only call this function once. If you desire to have an image for
765 * each pass of a interlaced image, use png_read_rows() instead.
767 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.10
769 void PNGAPI
770 png_read_image(png_structp png_ptr, png_bytepp image)
772 png_uint_32 i,image_height;
773 int pass, j;
774 png_bytepp rp;
776 png_debug(1, "in png_read_image\n");
777 /* save jump buffer and error functions */
779 #ifdef PNG_READ_INTERLACING_SUPPORTED
780 pass = png_set_interlace_handling(png_ptr);
781 #else
782 if (png_ptr->interlaced)
783 png_error(png_ptr,
784 "Cannot read interlaced image -- interlace handler disabled.");
785 pass = 1;
786 #endif
789 image_height=png_ptr->height;
790 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
792 for (j = 0; j < pass; j++)
794 rp = image;
795 for (i = 0; i < image_height; i++)
797 png_read_row(png_ptr, *rp, NULL);
798 rp++;
803 /* Read the end of the PNG file. Will not read past the end of the
804 * file, will verify the end is accurate, and will read any comments
805 * or time information at the end of the file, if info is not NULL.
807 void PNGAPI
808 png_read_end(png_structp png_ptr, png_infop info_ptr)
810 png_byte chunk_length[4];
811 png_uint_32 length;
813 png_debug(1, "in png_read_end\n");
814 /* save jump buffer and error functions */
815 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
819 #ifdef PNG_USE_LOCAL_ARRAYS
820 PNG_IHDR;
821 PNG_IDAT;
822 PNG_IEND;
823 PNG_PLTE;
824 #if defined(PNG_READ_bKGD_SUPPORTED)
825 PNG_bKGD;
826 #endif
827 #if defined(PNG_READ_cHRM_SUPPORTED)
828 PNG_cHRM;
829 #endif
830 #if defined(PNG_READ_gAMA_SUPPORTED)
831 PNG_gAMA;
832 #endif
833 #if defined(PNG_READ_hIST_SUPPORTED)
834 PNG_hIST;
835 #endif
836 #if defined(PNG_READ_iCCP_SUPPORTED)
837 PNG_iCCP;
838 #endif
839 #if defined(PNG_READ_iTXt_SUPPORTED)
840 PNG_iTXt;
841 #endif
842 #if defined(PNG_READ_oFFs_SUPPORTED)
843 PNG_oFFs;
844 #endif
845 #if defined(PNG_READ_pCAL_SUPPORTED)
846 PNG_pCAL;
847 #endif
848 #if defined(PNG_READ_pHYs_SUPPORTED)
849 PNG_pHYs;
850 #endif
851 #if defined(PNG_READ_sBIT_SUPPORTED)
852 PNG_sBIT;
853 #endif
854 #if defined(PNG_READ_sCAL_SUPPORTED)
855 PNG_sCAL;
856 #endif
857 #if defined(PNG_READ_sPLT_SUPPORTED)
858 PNG_sPLT;
859 #endif
860 #if defined(PNG_READ_sRGB_SUPPORTED)
861 PNG_sRGB;
862 #endif
863 #if defined(PNG_READ_tEXt_SUPPORTED)
864 PNG_tEXt;
865 #endif
866 #if defined(PNG_READ_tIME_SUPPORTED)
867 PNG_tIME;
868 #endif
869 #if defined(PNG_READ_tRNS_SUPPORTED)
870 PNG_tRNS;
871 #endif
872 #if defined(PNG_READ_zTXt_SUPPORTED)
873 PNG_zTXt;
874 #endif
875 #endif /* PNG_GLOBAL_ARRAYS */
877 png_read_data(png_ptr, chunk_length, 4);
878 length = png_get_uint_32(chunk_length);
880 png_reset_crc(png_ptr);
881 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
883 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
885 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
886 png_handle_IHDR(png_ptr, info_ptr, length);
887 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
888 png_handle_IEND(png_ptr, info_ptr, length);
889 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
890 else if (png_handle_as_unknown(png_ptr, png_ptr->chunk_name))
892 if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
894 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
895 png_error(png_ptr, "Too many IDAT's found");
897 else
898 png_ptr->mode |= PNG_AFTER_IDAT;
899 png_handle_unknown(png_ptr, info_ptr, length);
900 if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
901 png_ptr->mode |= PNG_HAVE_PLTE;
903 #endif
904 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
906 /* Zero length IDATs are legal after the last IDAT has been
907 * read, but not after other chunks have been read.
909 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
910 png_error(png_ptr, "Too many IDAT's found");
911 png_crc_finish(png_ptr, length);
913 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
914 png_handle_PLTE(png_ptr, info_ptr, length);
915 #if defined(PNG_READ_bKGD_SUPPORTED)
916 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
917 png_handle_bKGD(png_ptr, info_ptr, length);
918 #endif
919 #if defined(PNG_READ_cHRM_SUPPORTED)
920 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
921 png_handle_cHRM(png_ptr, info_ptr, length);
922 #endif
923 #if defined(PNG_READ_gAMA_SUPPORTED)
924 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
925 png_handle_gAMA(png_ptr, info_ptr, length);
926 #endif
927 #if defined(PNG_READ_hIST_SUPPORTED)
928 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
929 png_handle_hIST(png_ptr, info_ptr, length);
930 #endif
931 #if defined(PNG_READ_oFFs_SUPPORTED)
932 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
933 png_handle_oFFs(png_ptr, info_ptr, length);
934 #endif
935 #if defined(PNG_READ_pCAL_SUPPORTED)
936 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
937 png_handle_pCAL(png_ptr, info_ptr, length);
938 #endif
939 #if defined(PNG_READ_sCAL_SUPPORTED)
940 else if (!png_memcmp(png_ptr->chunk_name, png_sCAL, 4))
941 png_handle_sCAL(png_ptr, info_ptr, length);
942 #endif
943 #if defined(PNG_READ_pHYs_SUPPORTED)
944 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
945 png_handle_pHYs(png_ptr, info_ptr, length);
946 #endif
947 #if defined(PNG_READ_sBIT_SUPPORTED)
948 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
949 png_handle_sBIT(png_ptr, info_ptr, length);
950 #endif
951 #if defined(PNG_READ_sRGB_SUPPORTED)
952 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
953 png_handle_sRGB(png_ptr, info_ptr, length);
954 #endif
955 #if defined(PNG_READ_iCCP_SUPPORTED)
956 else if (!png_memcmp(png_ptr->chunk_name, png_iCCP, 4))
957 png_handle_iCCP(png_ptr, info_ptr, length);
958 #endif
959 #if defined(PNG_READ_sPLT_SUPPORTED)
960 else if (!png_memcmp(png_ptr->chunk_name, png_sPLT, 4))
961 png_handle_sPLT(png_ptr, info_ptr, length);
962 #endif
963 #if defined(PNG_READ_tEXt_SUPPORTED)
964 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
965 png_handle_tEXt(png_ptr, info_ptr, length);
966 #endif
967 #if defined(PNG_READ_tIME_SUPPORTED)
968 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
969 png_handle_tIME(png_ptr, info_ptr, length);
970 #endif
971 #if defined(PNG_READ_tRNS_SUPPORTED)
972 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
973 png_handle_tRNS(png_ptr, info_ptr, length);
974 #endif
975 #if defined(PNG_READ_zTXt_SUPPORTED)
976 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
977 png_handle_zTXt(png_ptr, info_ptr, length);
978 #endif
979 #if defined(PNG_READ_iTXt_SUPPORTED)
980 else if (!png_memcmp(png_ptr->chunk_name, png_iTXt, 4))
981 png_handle_iTXt(png_ptr, info_ptr, length);
982 #endif
983 else
984 png_handle_unknown(png_ptr, info_ptr, length);
985 } while (!(png_ptr->mode & PNG_HAVE_IEND));
988 /* free all memory used by the read */
989 void PNGAPI
990 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
991 png_infopp end_info_ptr_ptr)
993 png_structp png_ptr = NULL;
994 png_infop info_ptr = NULL, end_info_ptr = NULL;
995 #ifdef PNG_USER_MEM_SUPPORTED
996 png_free_ptr free_fn = NULL;
997 #endif
999 png_debug(1, "in png_destroy_read_struct\n");
1000 /* save jump buffer and error functions */
1001 if (png_ptr_ptr != NULL)
1002 png_ptr = *png_ptr_ptr;
1004 if (info_ptr_ptr != NULL)
1005 info_ptr = *info_ptr_ptr;
1007 if (end_info_ptr_ptr != NULL)
1008 end_info_ptr = *end_info_ptr_ptr;
1010 #ifdef PNG_USER_MEM_SUPPORTED
1011 free_fn = png_ptr->free_fn;
1012 #endif
1014 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
1016 if (info_ptr != NULL)
1018 #if defined(PNG_TEXT_SUPPORTED)
1019 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, -1);
1020 #endif
1022 #ifdef PNG_USER_MEM_SUPPORTED
1023 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
1024 #else
1025 png_destroy_struct((png_voidp)info_ptr);
1026 #endif
1027 *info_ptr_ptr = (png_infop)NULL;
1030 if (end_info_ptr != NULL)
1032 #if defined(PNG_READ_TEXT_SUPPORTED)
1033 png_free_data(png_ptr, end_info_ptr, PNG_FREE_TEXT, -1);
1034 #endif
1035 #ifdef PNG_USER_MEM_SUPPORTED
1036 png_destroy_struct_2((png_voidp)end_info_ptr, free_fn);
1037 #else
1038 png_destroy_struct((png_voidp)end_info_ptr);
1039 #endif
1040 *end_info_ptr_ptr = (png_infop)NULL;
1043 if (png_ptr != NULL)
1045 #ifdef PNG_USER_MEM_SUPPORTED
1046 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
1047 #else
1048 png_destroy_struct((png_voidp)png_ptr);
1049 #endif
1050 *png_ptr_ptr = (png_structp)NULL;
1054 /* free all memory used by the read (old method) */
1055 void PNGAPI
1056 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
1058 #ifdef PNG_SETJMP_SUPPORTED
1059 jmp_buf tmp_jmp;
1060 #endif
1061 png_error_ptr error_fn;
1062 png_error_ptr warning_fn;
1063 png_voidp error_ptr;
1064 #ifdef PNG_USER_MEM_SUPPORTED
1065 png_free_ptr free_fn;
1066 #endif
1068 png_debug(1, "in png_read_destroy\n");
1069 /* save jump buffer and error functions */
1070 if (info_ptr != NULL)
1071 png_info_destroy(png_ptr, info_ptr);
1073 if (end_info_ptr != NULL)
1074 png_info_destroy(png_ptr, end_info_ptr);
1076 png_free(png_ptr, png_ptr->zbuf);
1077 png_free(png_ptr, png_ptr->row_buf);
1078 png_free(png_ptr, png_ptr->prev_row);
1079 #if defined(PNG_READ_DITHER_SUPPORTED)
1080 png_free(png_ptr, png_ptr->palette_lookup);
1081 png_free(png_ptr, png_ptr->dither_index);
1082 #endif
1083 #if defined(PNG_READ_GAMMA_SUPPORTED)
1084 png_free(png_ptr, png_ptr->gamma_table);
1085 #endif
1086 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1087 png_free(png_ptr, png_ptr->gamma_from_1);
1088 png_free(png_ptr, png_ptr->gamma_to_1);
1089 #endif
1090 #ifdef PNG_FREE_ME_SUPPORTED
1091 if (png_ptr->free_me & PNG_FREE_PLTE)
1092 png_zfree(png_ptr, png_ptr->palette);
1093 png_ptr->free_me &= ~PNG_FREE_PLTE;
1094 #else
1095 if (png_ptr->flags & PNG_FLAG_FREE_PLTE)
1096 png_zfree(png_ptr, png_ptr->palette);
1097 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
1098 #endif
1099 #if defined(PNG_tRNS_SUPPORTED) || \
1100 defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
1101 #ifdef PNG_FREE_ME_SUPPORTED
1102 if (png_ptr->free_me & PNG_FREE_TRNS)
1103 png_free(png_ptr, png_ptr->trans);
1104 png_ptr->free_me &= ~PNG_FREE_TRNS;
1105 #else
1106 if (png_ptr->flags & PNG_FLAG_FREE_TRNS)
1107 png_free(png_ptr, png_ptr->trans);
1108 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
1109 #endif
1110 #endif
1111 #if defined(PNG_READ_hIST_SUPPORTED)
1112 #ifdef PNG_FREE_ME_SUPPORTED
1113 if (png_ptr->free_me & PNG_FREE_HIST)
1114 png_free(png_ptr, png_ptr->hist);
1115 png_ptr->free_me &= ~PNG_FREE_HIST;
1116 #else
1117 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
1118 png_free(png_ptr, png_ptr->hist);
1119 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
1120 #endif
1121 #endif
1122 #if defined(PNG_READ_GAMMA_SUPPORTED)
1123 if (png_ptr->gamma_16_table != NULL)
1125 int i;
1126 int istop = (1 << (8 - png_ptr->gamma_shift));
1127 for (i = 0; i < istop; i++)
1129 png_free(png_ptr, png_ptr->gamma_16_table[i]);
1131 png_free(png_ptr, png_ptr->gamma_16_table);
1133 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
1134 if (png_ptr->gamma_16_from_1 != NULL)
1136 int i;
1137 int istop = (1 << (8 - png_ptr->gamma_shift));
1138 for (i = 0; i < istop; i++)
1140 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
1142 png_free(png_ptr, png_ptr->gamma_16_from_1);
1144 if (png_ptr->gamma_16_to_1 != NULL)
1146 int i;
1147 int istop = (1 << (8 - png_ptr->gamma_shift));
1148 for (i = 0; i < istop; i++)
1150 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
1152 png_free(png_ptr, png_ptr->gamma_16_to_1);
1154 #endif
1155 #endif
1156 #if defined(PNG_TIME_RFC1123_SUPPORTED)
1157 png_free(png_ptr, png_ptr->time_buffer);
1158 #endif
1160 inflateEnd(&png_ptr->zstream);
1161 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
1162 png_free(png_ptr, png_ptr->save_buffer);
1163 #endif
1165 /* Save the important info out of the png_struct, in case it is
1166 * being used again.
1168 #ifdef PNG_SETJMP_SUPPORTED
1169 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
1170 #endif
1172 error_fn = png_ptr->error_fn;
1173 warning_fn = png_ptr->warning_fn;
1174 error_ptr = png_ptr->error_ptr;
1175 #ifdef PNG_USER_MEM_SUPPORTED
1176 free_fn = png_ptr->free_fn;
1177 #endif
1179 png_memset(png_ptr, 0, sizeof (png_struct));
1181 png_ptr->error_fn = error_fn;
1182 png_ptr->warning_fn = warning_fn;
1183 png_ptr->error_ptr = error_ptr;
1184 #ifdef PNG_USER_MEM_SUPPORTED
1185 png_ptr->free_fn = free_fn;
1186 #endif
1188 #ifdef PNG_SETJMP_SUPPORTED
1189 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
1190 #endif
1194 void PNGAPI
1195 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
1197 png_ptr->read_row_fn = read_row_fn;
1200 #if defined(PNG_INFO_IMAGE_SUPPORTED)
1201 void PNGAPI
1202 png_read_png(png_structp png_ptr, png_infop info_ptr,
1203 int transforms,
1204 voidp params)
1206 int row;
1208 #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
1209 /* invert the alpha channel from opacity to transparency */
1210 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
1211 png_set_invert_alpha(png_ptr);
1212 #endif
1214 /* The call to png_read_info() gives us all of the information from the
1215 * PNG file before the first IDAT (image data chunk).
1217 png_read_info(png_ptr, info_ptr);
1219 /* -------------- image transformations start here ------------------- */
1221 #if defined(PNG_READ_16_TO_8_SUPPORTED)
1222 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
1223 if (transforms & PNG_TRANSFORM_STRIP_16)
1224 png_set_strip_16(png_ptr);
1225 #endif
1227 #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
1228 /* Strip alpha bytes from the input data without combining with the
1229 * background (not recommended).
1231 if (transforms & PNG_TRANSFORM_STRIP_ALPHA)
1232 png_set_strip_alpha(png_ptr);
1233 #endif
1235 #if defined(PNG_READ_PACK_SUPPORTED) && !defined(PNG_READ_EXPAND_SUPPORTED)
1236 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
1237 * byte into separate bytes (useful for paletted and grayscale images).
1239 if (transforms & PNG_TRANSFORM_PACKING)
1240 png_set_packing(png_ptr);
1241 #endif
1243 #if defined(PNG_READ_PACKSWAP_SUPPORTED)
1244 /* Change the order of packed pixels to least significant bit first
1245 * (not useful if you are using png_set_packing). */
1246 if (transforms & PNG_TRANSFORM_PACKSWAP)
1247 png_set_packswap(png_ptr);
1248 #endif
1250 #if defined(PNG_READ_EXPAND_SUPPORTED)
1251 /* Expand paletted colors into true RGB triplets
1252 * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
1253 * Expand paletted or RGB images with transparency to full alpha
1254 * channels so the data will be available as RGBA quartets.
1256 if (transforms & PNG_TRANSFORM_EXPAND)
1257 if ((png_ptr->bit_depth < 8) ||
1258 (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ||
1259 (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)))
1260 png_set_expand(png_ptr);
1261 #endif
1263 /* We don't handle background color or gamma transformation or dithering. */
1265 #if defined(PNG_READ_INVERT_SUPPORTED)
1266 /* invert monochrome files to have 0 as white and 1 as black */
1267 if (transforms & PNG_TRANSFORM_INVERT_MONO)
1268 png_set_invert_mono(png_ptr);
1269 #endif
1271 #if defined(PNG_READ_SHIFT_SUPPORTED)
1272 /* If you want to shift the pixel values from the range [0,255] or
1273 * [0,65535] to the original [0,7] or [0,31], or whatever range the
1274 * colors were originally in:
1276 if ((transforms & PNG_TRANSFORM_SHIFT)
1277 && png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
1279 png_color_8p sig_bit;
1281 png_get_sBIT(png_ptr, info_ptr, &sig_bit);
1282 png_set_shift(png_ptr, sig_bit);
1284 #endif
1286 #if defined(PNG_READ_BGR_SUPPORTED)
1287 /* flip the RGB pixels to BGR (or RGBA to BGRA) */
1288 if (transforms & PNG_TRANSFORM_BGR)
1289 png_set_bgr(png_ptr);
1290 #endif
1292 #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
1293 /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
1294 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
1295 png_set_swap_alpha(png_ptr);
1296 #endif
1298 #if defined(PNG_READ_SWAP_SUPPORTED)
1299 /* swap bytes of 16 bit files to least significant byte first */
1300 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
1301 png_set_swap(png_ptr);
1302 #endif
1304 /* We don't handle adding filler bytes */
1306 /* Optional call to gamma correct and add the background to the palette
1307 * and update info structure. REQUIRED if you are expecting libpng to
1308 * update the palette for you (ie you selected such a transform above).
1310 png_read_update_info(png_ptr, info_ptr);
1312 /* -------------- image transformations end here ------------------- */
1314 #ifdef PNG_FREE_ME_SUPPORTED
1315 png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
1316 #endif
1317 if(info_ptr->row_pointers == NULL)
1319 info_ptr->row_pointers = (png_bytepp)png_malloc(png_ptr,
1320 info_ptr->height * sizeof(png_bytep));
1321 #ifdef PNG_FREE_ME_SUPPORTED
1322 info_ptr->free_me |= PNG_FREE_ROWS;
1323 #endif
1324 for (row = 0; row < (int)info_ptr->height; row++)
1325 info_ptr->row_pointers[row] = png_malloc(png_ptr,
1326 png_get_rowbytes(png_ptr, info_ptr));
1329 png_read_image(png_ptr, info_ptr->row_pointers);
1330 info_ptr->valid |= PNG_INFO_IDAT;
1332 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
1333 png_read_end(png_ptr, info_ptr);
1335 if(transforms == 0 || params == (voidp)NULL)
1336 /* quiet compiler warnings */ return;
1339 #endif