]> git.saurik.com Git - wxWidgets.git/blob - src/png/pngread.c
fixed status bar drawing broken by previous compilation fix
[wxWidgets.git] / src / png / pngread.c
1
2 /* pngread.c - read a PNG file
3 *
4 * libpng 1.0.3 - January 14, 1999
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, 1999 Glenn Randers-Pehrson
9 *
10 * This file contains routines that an application calls directly to
11 * read a PNG file or stream.
12 */
13
14 #define PNG_INTERNAL
15 #include "png.h"
16
17 /* Create a PNG structure for reading, and allocate any memory needed. */
18 png_structp
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)
21 {
22
23 #ifdef PNG_USER_MEM_SUPPORTED
24 return (png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
25 warn_fn, NULL, NULL, NULL));
26 }
27
28 /* Alternate create PNG structure for reading, and allocate any memory needed. */
29 png_structp
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)
33 {
34 #endif /* PNG_USER_MEM_SUPPORTED */
35
36 png_structp png_ptr;
37 #ifdef USE_FAR_KEYWORD
38 jmp_buf jmpbuf;
39 #endif
40 png_debug(1, "in png_create_read_struct\n");
41 #ifdef PNG_USER_MEM_SUPPORTED
42 if ((png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
43 (png_malloc_ptr)malloc_fn)) == NULL)
44 #else
45 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
46 #endif
47 {
48 return (png_structp)NULL;
49 }
50 #ifdef USE_FAR_KEYWORD
51 if (setjmp(jmpbuf))
52 #else
53 if (setjmp(png_ptr->jmpbuf))
54 #endif
55 {
56 png_free(png_ptr, png_ptr->zbuf);
57 png_destroy_struct(png_ptr);
58 return (png_structp)NULL;
59 }
60 #ifdef USE_FAR_KEYWORD
61 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
62 #endif
63
64 #ifdef PNG_USER_MEM_SUPPORTED
65 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
66 #endif /* PNG_USER_MEM_SUPPORTED */
67
68 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
69
70 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
71 * we must recompile any applications that use any older library version.
72 * For versions after libpng 1.0, we will be compatible, so we need
73 * only check the first digit.
74 */
75 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
76 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
77 {
78 png_error(png_ptr,
79 "Incompatible libpng version in application and library");
80 }
81
82 /* initialize zbuf - compression buffer */
83 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
84 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
85 (png_uint_32)png_ptr->zbuf_size);
86 png_ptr->zstream.zalloc = png_zalloc;
87 png_ptr->zstream.zfree = png_zfree;
88 png_ptr->zstream.opaque = (voidpf)png_ptr;
89
90 switch (inflateInit(&png_ptr->zstream))
91 {
92 case Z_OK: /* Do nothing */ break;
93 case Z_MEM_ERROR:
94 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory error"); break;
95 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version error"); break;
96 default: png_error(png_ptr, "Unknown zlib error");
97 }
98
99 png_ptr->zstream.next_out = png_ptr->zbuf;
100 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
101
102 png_set_read_fn(png_ptr, NULL, NULL);
103
104 return (png_ptr);
105 }
106
107 /* Initialize PNG structure for reading, and allocate any memory needed.
108 This interface is deprecated in favour of the png_create_read_struct(),
109 and it will eventually disappear. */
110 void
111 png_read_init(png_structp png_ptr)
112 {
113 jmp_buf tmp_jmp; /* to save current jump buffer */
114
115 png_debug(1, "in png_read_init\n");
116 /* save jump buffer and error functions */
117 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
118
119 /* reset all variables to 0 */
120 png_memset(png_ptr, 0, sizeof (png_struct));
121
122 /* restore jump buffer */
123 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
124
125 /* initialize zbuf - compression buffer */
126 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
127 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
128 (png_uint_32)png_ptr->zbuf_size);
129 png_ptr->zstream.zalloc = png_zalloc;
130 png_ptr->zstream.zfree = png_zfree;
131 png_ptr->zstream.opaque = (voidpf)png_ptr;
132
133 switch (inflateInit(&png_ptr->zstream))
134 {
135 case Z_OK: /* Do nothing */ break;
136 case Z_MEM_ERROR:
137 case Z_STREAM_ERROR: png_error(png_ptr, "zlib memory"); break;
138 case Z_VERSION_ERROR: png_error(png_ptr, "zlib version"); break;
139 default: png_error(png_ptr, "Unknown zlib error");
140 }
141
142 png_ptr->zstream.next_out = png_ptr->zbuf;
143 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
144
145 png_set_read_fn(png_ptr, NULL, NULL);
146 }
147
148 /* Read the information before the actual image data. This has been
149 * changed in v0.90 to allow reading a file that already has the magic
150 * bytes read from the stream. You can tell libpng how many bytes have
151 * been read from the beginning of the stream (up to the maximum of 8)
152 * via png_set_sig_bytes(), and we will only check the remaining bytes
153 * here. The application can then have access to the signature bytes we
154 * read if it is determined that this isn't a valid PNG file.
155 */
156 void
157 png_read_info(png_structp png_ptr, png_infop info_ptr)
158 {
159 png_debug(1, "in png_read_info\n");
160 /* save jump buffer and error functions */
161 /* If we haven't checked all of the PNG signature bytes, do so now. */
162 if (png_ptr->sig_bytes < 8)
163 {
164 png_size_t num_checked = png_ptr->sig_bytes,
165 num_to_check = 8 - num_checked;
166
167 png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
168 png_ptr->sig_bytes = 8;
169
170 if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
171 {
172 if (num_checked < 4 &&
173 png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
174 png_error(png_ptr, "Not a PNG file");
175 else
176 png_error(png_ptr, "PNG file corrupted by ASCII conversion");
177 }
178 }
179
180 for(;;)
181 {
182 png_byte chunk_length[4];
183 png_uint_32 length;
184
185 png_read_data(png_ptr, chunk_length, 4);
186 length = png_get_uint_32(chunk_length);
187
188 png_reset_crc(png_ptr);
189 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
190
191 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
192
193 /* This should be a binary subdivision search or a hash for
194 * matching the chunk name rather than a linear search.
195 */
196 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
197 png_handle_IHDR(png_ptr, info_ptr, length);
198 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
199 png_handle_PLTE(png_ptr, info_ptr, length);
200 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
201 png_handle_IEND(png_ptr, info_ptr, length);
202 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
203 {
204 if (!(png_ptr->mode & PNG_HAVE_IHDR))
205 png_error(png_ptr, "Missing IHDR before IDAT");
206 else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
207 !(png_ptr->mode & PNG_HAVE_PLTE))
208 png_error(png_ptr, "Missing PLTE before IDAT");
209
210 png_ptr->idat_size = length;
211 png_ptr->mode |= PNG_HAVE_IDAT;
212 break;
213 }
214 #if defined(PNG_READ_bKGD_SUPPORTED)
215 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
216 png_handle_bKGD(png_ptr, info_ptr, length);
217 #endif
218 #if defined(PNG_READ_cHRM_SUPPORTED)
219 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
220 png_handle_cHRM(png_ptr, info_ptr, length);
221 #endif
222 #if defined(PNG_READ_gAMA_SUPPORTED)
223 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
224 png_handle_gAMA(png_ptr, info_ptr, length);
225 #endif
226 #if defined(PNG_READ_hIST_SUPPORTED)
227 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
228 png_handle_hIST(png_ptr, info_ptr, length);
229 #endif
230 #if defined(PNG_READ_oFFs_SUPPORTED)
231 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
232 png_handle_oFFs(png_ptr, info_ptr, length);
233 #endif
234 #if defined(PNG_READ_pCAL_SUPPORTED)
235 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
236 png_handle_pCAL(png_ptr, info_ptr, length);
237 #endif
238 #if defined(PNG_READ_pHYs_SUPPORTED)
239 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
240 png_handle_pHYs(png_ptr, info_ptr, length);
241 #endif
242 #if defined(PNG_READ_sBIT_SUPPORTED)
243 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
244 png_handle_sBIT(png_ptr, info_ptr, length);
245 #endif
246 #if defined(PNG_READ_sRGB_SUPPORTED)
247 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
248 png_handle_sRGB(png_ptr, info_ptr, length);
249 #endif
250 #if defined(PNG_READ_tEXt_SUPPORTED)
251 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
252 png_handle_tEXt(png_ptr, info_ptr, length);
253 #endif
254 #if defined(PNG_READ_tIME_SUPPORTED)
255 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
256 png_handle_tIME(png_ptr, info_ptr, length);
257 #endif
258 #if defined(PNG_READ_tRNS_SUPPORTED)
259 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
260 png_handle_tRNS(png_ptr, info_ptr, length);
261 #endif
262 #if defined(PNG_READ_zTXt_SUPPORTED)
263 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
264 png_handle_zTXt(png_ptr, info_ptr, length);
265 #endif
266 else
267 png_handle_unknown(png_ptr, info_ptr, length);
268 }
269 }
270
271 /* optional call to update the users info_ptr structure */
272 void
273 png_read_update_info(png_structp png_ptr, png_infop info_ptr)
274 {
275 png_debug(1, "in png_read_update_info\n");
276 /* save jump buffer and error functions */
277 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
278 png_read_start_row(png_ptr);
279 png_read_transform_info(png_ptr, info_ptr);
280 }
281
282 /* Initialize palette, background, etc, after transformations
283 * are set, but before any reading takes place. This allows
284 * the user to obtain a gamma-corrected palette, for example.
285 * If the user doesn't call this, we will do it ourselves.
286 */
287 void
288 png_start_read_image(png_structp png_ptr)
289 {
290 png_debug(1, "in png_start_read_image\n");
291 /* save jump buffer and error functions */
292 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
293 png_read_start_row(png_ptr);
294 }
295
296 void
297 png_read_row(png_structp png_ptr, png_bytep row, png_bytep dsp_row)
298 {
299 int ret;
300 png_debug2(1, "in png_read_row (row %d, pass %d)\n",
301 png_ptr->row_number, png_ptr->pass);
302 /* save jump buffer and error functions */
303 if (!(png_ptr->flags & PNG_FLAG_ROW_INIT))
304 png_read_start_row(png_ptr);
305 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
306 {
307 /* check for transforms that have been set but were defined out */
308 #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
309 if (png_ptr->transformations & PNG_INVERT_MONO)
310 png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined.");
311 #endif
312 #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
313 if (png_ptr->transformations & PNG_FILLER)
314 png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined.");
315 #endif
316 #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && !defined(PNG_READ_PACKSWAP_SUPPORTED)
317 if (png_ptr->transformations & PNG_PACKSWAP)
318 png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined.");
319 #endif
320 #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
321 if (png_ptr->transformations & PNG_PACK)
322 png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined.");
323 #endif
324 #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
325 if (png_ptr->transformations & PNG_SHIFT)
326 png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined.");
327 #endif
328 #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
329 if (png_ptr->transformations & PNG_BGR)
330 png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined.");
331 #endif
332 #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
333 if (png_ptr->transformations & PNG_SWAP_BYTES)
334 png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined.");
335 #endif
336 }
337
338 #if defined(PNG_READ_INTERLACING_SUPPORTED)
339 /* if interlaced and we do not need a new row, combine row and return */
340 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
341 {
342 switch (png_ptr->pass)
343 {
344 case 0:
345 if (png_ptr->row_number & 7)
346 {
347 if (dsp_row != NULL)
348 png_combine_row(png_ptr, dsp_row,
349 png_pass_dsp_mask[png_ptr->pass]);
350 png_read_finish_row(png_ptr);
351 return;
352 }
353 break;
354 case 1:
355 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
356 {
357 if (dsp_row != NULL)
358 png_combine_row(png_ptr, dsp_row,
359 png_pass_dsp_mask[png_ptr->pass]);
360 png_read_finish_row(png_ptr);
361 return;
362 }
363 break;
364 case 2:
365 if ((png_ptr->row_number & 7) != 4)
366 {
367 if (dsp_row != NULL && (png_ptr->row_number & 4))
368 png_combine_row(png_ptr, dsp_row,
369 png_pass_dsp_mask[png_ptr->pass]);
370 png_read_finish_row(png_ptr);
371 return;
372 }
373 break;
374 case 3:
375 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
376 {
377 if (dsp_row != NULL)
378 png_combine_row(png_ptr, dsp_row,
379 png_pass_dsp_mask[png_ptr->pass]);
380 png_read_finish_row(png_ptr);
381 return;
382 }
383 break;
384 case 4:
385 if ((png_ptr->row_number & 3) != 2)
386 {
387 if (dsp_row != NULL && (png_ptr->row_number & 2))
388 png_combine_row(png_ptr, dsp_row,
389 png_pass_dsp_mask[png_ptr->pass]);
390 png_read_finish_row(png_ptr);
391 return;
392 }
393 break;
394 case 5:
395 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
396 {
397 if (dsp_row != NULL)
398 png_combine_row(png_ptr, dsp_row,
399 png_pass_dsp_mask[png_ptr->pass]);
400 png_read_finish_row(png_ptr);
401 return;
402 }
403 break;
404 case 6:
405 if (!(png_ptr->row_number & 1))
406 {
407 png_read_finish_row(png_ptr);
408 return;
409 }
410 break;
411 }
412 }
413 #endif
414
415 if (!(png_ptr->mode & PNG_HAVE_IDAT))
416 png_error(png_ptr, "Invalid attempt to read row data");
417
418 png_ptr->zstream.next_out = png_ptr->row_buf;
419 png_ptr->zstream.avail_out = (uInt)png_ptr->irowbytes;
420 do
421 {
422 if (!(png_ptr->zstream.avail_in))
423 {
424 while (!png_ptr->idat_size)
425 {
426 png_byte chunk_length[4];
427
428 png_crc_finish(png_ptr, 0);
429
430 png_read_data(png_ptr, chunk_length, 4);
431 png_ptr->idat_size = png_get_uint_32(chunk_length);
432
433 png_reset_crc(png_ptr);
434 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
435 if (png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
436 png_error(png_ptr, "Not enough image data");
437 }
438 png_ptr->zstream.avail_in = (uInt)png_ptr->zbuf_size;
439 png_ptr->zstream.next_in = png_ptr->zbuf;
440 if (png_ptr->zbuf_size > png_ptr->idat_size)
441 png_ptr->zstream.avail_in = (uInt)png_ptr->idat_size;
442 png_crc_read(png_ptr, png_ptr->zbuf,
443 (png_size_t)png_ptr->zstream.avail_in);
444 png_ptr->idat_size -= png_ptr->zstream.avail_in;
445 }
446 ret = inflate(&png_ptr->zstream, Z_PARTIAL_FLUSH);
447 if (ret == Z_STREAM_END)
448 {
449 if (png_ptr->zstream.avail_out || png_ptr->zstream.avail_in ||
450 png_ptr->idat_size)
451 png_error(png_ptr, "Extra compressed data");
452 png_ptr->mode |= PNG_AFTER_IDAT;
453 png_ptr->flags |= PNG_FLAG_ZLIB_FINISHED;
454 break;
455 }
456 if (ret != Z_OK)
457 png_error(png_ptr, png_ptr->zstream.msg ? png_ptr->zstream.msg :
458 "Decompression error");
459
460 } while (png_ptr->zstream.avail_out);
461
462 png_ptr->row_info.color_type = png_ptr->color_type;
463 png_ptr->row_info.width = png_ptr->iwidth;
464 png_ptr->row_info.channels = png_ptr->channels;
465 png_ptr->row_info.bit_depth = png_ptr->bit_depth;
466 png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
467 {
468 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
469 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
470 }
471
472 png_read_filter_row(png_ptr, &(png_ptr->row_info),
473 png_ptr->row_buf + 1, png_ptr->prev_row + 1,
474 (int)(png_ptr->row_buf[0]));
475
476 png_memcpy_check(png_ptr, png_ptr->prev_row, png_ptr->row_buf,
477 png_ptr->rowbytes + 1);
478
479 if (png_ptr->transformations)
480 png_do_read_transformations(png_ptr);
481
482 #if defined(PNG_READ_INTERLACING_SUPPORTED)
483 /* blow up interlaced rows to full size */
484 if (png_ptr->interlaced &&
485 (png_ptr->transformations & PNG_INTERLACE))
486 {
487 if (png_ptr->pass < 6)
488 png_do_read_interlace(&(png_ptr->row_info),
489 png_ptr->row_buf + 1, png_ptr->pass, png_ptr->transformations);
490
491 if (dsp_row != NULL)
492 png_combine_row(png_ptr, dsp_row,
493 png_pass_dsp_mask[png_ptr->pass]);
494 if (row != NULL)
495 png_combine_row(png_ptr, row,
496 png_pass_mask[png_ptr->pass]);
497 }
498 else
499 #endif
500 {
501 if (row != NULL)
502 png_combine_row(png_ptr, row, 0xff);
503 if (dsp_row != NULL)
504 png_combine_row(png_ptr, dsp_row, 0xff);
505 }
506 png_read_finish_row(png_ptr);
507
508 if (png_ptr->read_row_fn != NULL)
509 (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
510 }
511
512 /* Read one or more rows of image data. If the image is interlaced,
513 * and png_set_interlace_handling() has been called, the rows need to
514 * contain the contents of the rows from the previous pass. If the
515 * image has alpha or transparency, and png_handle_alpha()[*] has been
516 * called, the rows contents must be initialized to the contents of the
517 * screen.
518 *
519 * "row" holds the actual image, and pixels are placed in it
520 * as they arrive. If the image is displayed after each pass, it will
521 * appear to "sparkle" in. "display_row" can be used to display a
522 * "chunky" progressive image, with finer detail added as it becomes
523 * available. If you do not want this "chunky" display, you may pass
524 * NULL for display_row. If you do not want the sparkle display, and
525 * you have not called png_handle_alpha(), you may pass NULL for rows.
526 * If you have called png_handle_alpha(), and the image has either an
527 * alpha channel or a transparency chunk, you must provide a buffer for
528 * rows. In this case, you do not have to provide a display_row buffer
529 * also, but you may. If the image is not interlaced, or if you have
530 * not called png_set_interlace_handling(), the display_row buffer will
531 * be ignored, so pass NULL to it.
532 *
533 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.3.
534 */
535
536 void
537 png_read_rows(png_structp png_ptr, png_bytepp row,
538 png_bytepp display_row, png_uint_32 num_rows)
539 {
540 png_uint_32 i;
541 png_bytepp rp;
542 png_bytepp dp;
543
544 png_debug(1, "in png_read_rows\n");
545 /* save jump buffer and error functions */
546 rp = row;
547 dp = display_row;
548 if (rp != NULL && dp != NULL)
549 for (i = 0; i < num_rows; i++)
550 {
551 png_bytep rptr = *rp++;
552 png_bytep dptr = *dp++;
553
554 png_read_row(png_ptr, rptr, dptr);
555 }
556 else if(rp != NULL)
557 for (i = 0; i < num_rows; i++)
558 {
559 png_bytep rptr = *rp;
560 png_read_row(png_ptr, rptr, NULL);
561 rp++;
562 }
563 else if(dp != NULL)
564 for (i = 0; i < num_rows; i++)
565 {
566 png_bytep dptr = *dp;
567 png_read_row(png_ptr, NULL, dptr);
568 dp++;
569 }
570 }
571
572 /* Read the entire image. If the image has an alpha channel or a tRNS
573 * chunk, and you have called png_handle_alpha()[*], you will need to
574 * initialize the image to the current image that PNG will be overlaying.
575 * We set the num_rows again here, in case it was incorrectly set in
576 * png_read_start_row() by a call to png_read_update_info() or
577 * png_start_read_image() if png_set_interlace_handling() wasn't called
578 * prior to either of these functions like it should have been. You can
579 * only call this function once. If you desire to have an image for
580 * each pass of a interlaced image, use png_read_rows() instead.
581 *
582 * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.3.
583 */
584 void
585 png_read_image(png_structp png_ptr, png_bytepp image)
586 {
587 png_uint_32 i,image_height;
588 int pass, j;
589 png_bytepp rp;
590
591 png_debug(1, "in png_read_image\n");
592 /* save jump buffer and error functions */
593 pass = png_set_interlace_handling(png_ptr);
594
595 image_height=png_ptr->height;
596 png_ptr->num_rows = image_height; /* Make sure this is set correctly */
597
598 for (j = 0; j < pass; j++)
599 {
600 rp = image;
601 for (i = 0; i < image_height; i++)
602 {
603 png_read_row(png_ptr, *rp, NULL);
604 rp++;
605 }
606 }
607 }
608
609 /* Read the end of the PNG file. Will not read past the end of the
610 * file, will verify the end is accurate, and will read any comments
611 * or time information at the end of the file, if info is not NULL.
612 */
613 void
614 png_read_end(png_structp png_ptr, png_infop info_ptr)
615 {
616 png_byte chunk_length[4];
617 png_uint_32 length;
618
619 png_debug(1, "in png_read_end\n");
620 /* save jump buffer and error functions */
621 png_crc_finish(png_ptr, 0); /* Finish off CRC from last IDAT chunk */
622
623 do
624 {
625 png_read_data(png_ptr, chunk_length, 4);
626 length = png_get_uint_32(chunk_length);
627
628 png_reset_crc(png_ptr);
629 png_crc_read(png_ptr, png_ptr->chunk_name, 4);
630
631 png_debug1(0, "Reading %s chunk.\n", png_ptr->chunk_name);
632
633 if (!png_memcmp(png_ptr->chunk_name, png_IHDR, 4))
634 png_handle_IHDR(png_ptr, info_ptr, length);
635 else if (!png_memcmp(png_ptr->chunk_name, png_IDAT, 4))
636 {
637 /* Zero length IDATs are legal after the last IDAT has been
638 * read, but not after other chunks have been read.
639 */
640 if (length > 0 || png_ptr->mode & PNG_AFTER_IDAT)
641 png_error(png_ptr, "Too many IDAT's found");
642 else
643 png_crc_finish(png_ptr, 0);
644 }
645 else if (!png_memcmp(png_ptr->chunk_name, png_PLTE, 4))
646 png_handle_PLTE(png_ptr, info_ptr, length);
647 else if (!png_memcmp(png_ptr->chunk_name, png_IEND, 4))
648 png_handle_IEND(png_ptr, info_ptr, length);
649 #if defined(PNG_READ_bKGD_SUPPORTED)
650 else if (!png_memcmp(png_ptr->chunk_name, png_bKGD, 4))
651 png_handle_bKGD(png_ptr, info_ptr, length);
652 #endif
653 #if defined(PNG_READ_cHRM_SUPPORTED)
654 else if (!png_memcmp(png_ptr->chunk_name, png_cHRM, 4))
655 png_handle_cHRM(png_ptr, info_ptr, length);
656 #endif
657 #if defined(PNG_READ_gAMA_SUPPORTED)
658 else if (!png_memcmp(png_ptr->chunk_name, png_gAMA, 4))
659 png_handle_gAMA(png_ptr, info_ptr, length);
660 #endif
661 #if defined(PNG_READ_hIST_SUPPORTED)
662 else if (!png_memcmp(png_ptr->chunk_name, png_hIST, 4))
663 png_handle_hIST(png_ptr, info_ptr, length);
664 #endif
665 #if defined(PNG_READ_oFFs_SUPPORTED)
666 else if (!png_memcmp(png_ptr->chunk_name, png_oFFs, 4))
667 png_handle_oFFs(png_ptr, info_ptr, length);
668 #endif
669 #if defined(PNG_READ_pCAL_SUPPORTED)
670 else if (!png_memcmp(png_ptr->chunk_name, png_pCAL, 4))
671 png_handle_pCAL(png_ptr, info_ptr, length);
672 #endif
673 #if defined(PNG_READ_pHYs_SUPPORTED)
674 else if (!png_memcmp(png_ptr->chunk_name, png_pHYs, 4))
675 png_handle_pHYs(png_ptr, info_ptr, length);
676 #endif
677 #if defined(PNG_READ_sBIT_SUPPORTED)
678 else if (!png_memcmp(png_ptr->chunk_name, png_sBIT, 4))
679 png_handle_sBIT(png_ptr, info_ptr, length);
680 #endif
681 #if defined(PNG_READ_sRGB_SUPPORTED)
682 else if (!png_memcmp(png_ptr->chunk_name, png_sRGB, 4))
683 png_handle_sRGB(png_ptr, info_ptr, length);
684 #endif
685 #if defined(PNG_READ_tEXt_SUPPORTED)
686 else if (!png_memcmp(png_ptr->chunk_name, png_tEXt, 4))
687 png_handle_tEXt(png_ptr, info_ptr, length);
688 #endif
689 #if defined(PNG_READ_tIME_SUPPORTED)
690 else if (!png_memcmp(png_ptr->chunk_name, png_tIME, 4))
691 png_handle_tIME(png_ptr, info_ptr, length);
692 #endif
693 #if defined(PNG_READ_tRNS_SUPPORTED)
694 else if (!png_memcmp(png_ptr->chunk_name, png_tRNS, 4))
695 png_handle_tRNS(png_ptr, info_ptr, length);
696 #endif
697 #if defined(PNG_READ_zTXt_SUPPORTED)
698 else if (!png_memcmp(png_ptr->chunk_name, png_zTXt, 4))
699 png_handle_zTXt(png_ptr, info_ptr, length);
700 #endif
701 else
702 png_handle_unknown(png_ptr, info_ptr, length);
703 } while (!(png_ptr->mode & PNG_HAVE_IEND));
704 }
705
706 /* free all memory used by the read */
707 void
708 png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
709 png_infopp end_info_ptr_ptr)
710 {
711 png_structp png_ptr = NULL;
712 png_infop info_ptr = NULL, end_info_ptr = NULL;
713 #ifdef PNG_USER_MEM_SUPPORTED
714 png_free_ptr free_fn = NULL;
715 #endif /* PNG_USER_MEM_SUPPORTED */
716
717 png_debug(1, "in png_destroy_read_struct\n");
718 /* save jump buffer and error functions */
719 if (png_ptr_ptr != NULL)
720 png_ptr = *png_ptr_ptr;
721
722 if (info_ptr_ptr != NULL)
723 info_ptr = *info_ptr_ptr;
724
725 if (end_info_ptr_ptr != NULL)
726 end_info_ptr = *end_info_ptr_ptr;
727
728 #ifdef PNG_USER_MEM_SUPPORTED
729 free_fn = png_ptr->free_fn;
730 #endif
731
732 png_read_destroy(png_ptr, info_ptr, end_info_ptr);
733
734 if (info_ptr != NULL)
735 {
736 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
737 png_free(png_ptr, info_ptr->text);
738 #endif
739
740 #ifdef PNG_USER_MEM_SUPPORTED
741 png_destroy_struct_2((png_voidp)info_ptr, free_fn);
742 #else
743 png_destroy_struct((png_voidp)info_ptr);
744 #endif
745 *info_ptr_ptr = (png_infop)NULL;
746 }
747
748 if (end_info_ptr != NULL)
749 {
750 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
751 png_free(png_ptr, end_info_ptr->text);
752 #endif
753 #ifdef PNG_USER_MEM_SUPPORTED
754 png_destroy_struct_2((png_voidp)end_info_ptr, free_fn);
755 #else
756 png_destroy_struct((png_voidp)end_info_ptr);
757 #endif
758 *end_info_ptr_ptr = (png_infop)NULL;
759 }
760
761 if (png_ptr != NULL)
762 {
763 #ifdef PNG_USER_MEM_SUPPORTED
764 png_destroy_struct_2((png_voidp)png_ptr, free_fn);
765 #else
766 png_destroy_struct((png_voidp)png_ptr);
767 #endif
768 *png_ptr_ptr = (png_structp)NULL;
769 }
770 }
771
772 /* free all memory used by the read (old method) */
773 void
774 png_read_destroy(png_structp png_ptr, png_infop info_ptr, png_infop end_info_ptr)
775 {
776 jmp_buf tmp_jmp;
777 png_error_ptr error_fn;
778 png_error_ptr warning_fn;
779 png_voidp error_ptr;
780 #ifdef PNG_USER_MEM_SUPPORTED
781 png_free_ptr free_fn;
782 #endif
783
784 png_debug(1, "in png_read_destroy\n");
785 /* save jump buffer and error functions */
786 if (info_ptr != NULL)
787 png_info_destroy(png_ptr, info_ptr);
788
789 if (end_info_ptr != NULL)
790 png_info_destroy(png_ptr, end_info_ptr);
791
792 png_free(png_ptr, png_ptr->zbuf);
793 png_free(png_ptr, png_ptr->row_buf);
794 png_free(png_ptr, png_ptr->prev_row);
795 #if defined(PNG_READ_DITHER_SUPPORTED)
796 png_free(png_ptr, png_ptr->palette_lookup);
797 png_free(png_ptr, png_ptr->dither_index);
798 #endif
799 #if defined(PNG_READ_GAMMA_SUPPORTED)
800 png_free(png_ptr, png_ptr->gamma_table);
801 #endif
802 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
803 png_free(png_ptr, png_ptr->gamma_from_1);
804 png_free(png_ptr, png_ptr->gamma_to_1);
805 #endif
806 if (png_ptr->flags & PNG_FLAG_FREE_PALETTE)
807 png_zfree(png_ptr, png_ptr->palette);
808 if (png_ptr->flags & PNG_FLAG_FREE_TRANS)
809 png_free(png_ptr, png_ptr->trans);
810 #if defined(PNG_READ_hIST_SUPPORTED)
811 if (png_ptr->flags & PNG_FLAG_FREE_HIST)
812 png_free(png_ptr, png_ptr->hist);
813 #endif
814 #if defined(PNG_READ_GAMMA_SUPPORTED)
815 if (png_ptr->gamma_16_table != NULL)
816 {
817 int i;
818 int istop = (1 << (8 - png_ptr->gamma_shift));
819 for (i = 0; i < istop; i++)
820 {
821 png_free(png_ptr, png_ptr->gamma_16_table[i]);
822 }
823 png_free(png_ptr, png_ptr->gamma_16_table);
824 }
825 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
826 if (png_ptr->gamma_16_from_1 != NULL)
827 {
828 int i;
829 int istop = (1 << (8 - png_ptr->gamma_shift));
830 for (i = 0; i < istop; i++)
831 {
832 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
833 }
834 png_free(png_ptr, png_ptr->gamma_16_from_1);
835 }
836 if (png_ptr->gamma_16_to_1 != NULL)
837 {
838 int i;
839 int istop = (1 << (8 - png_ptr->gamma_shift));
840 for (i = 0; i < istop; i++)
841 {
842 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
843 }
844 png_free(png_ptr, png_ptr->gamma_16_to_1);
845 }
846 #endif
847 #endif
848 #if defined(PNG_TIME_RFC1123_SUPPORTED)
849 png_free(png_ptr, png_ptr->time_buffer);
850 #endif /* PNG_TIME_RFC1123_SUPPORTED */
851
852 inflateEnd(&png_ptr->zstream);
853 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
854 png_free(png_ptr, png_ptr->save_buffer);
855 #endif
856
857 /* Save the important info out of the png_struct, in case it is
858 * being used again.
859 */
860 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
861
862 error_fn = png_ptr->error_fn;
863 warning_fn = png_ptr->warning_fn;
864 error_ptr = png_ptr->error_ptr;
865 #ifdef PNG_USER_MEM_SUPPORTED
866 free_fn = png_ptr->free_fn;
867 #endif
868
869 png_memset(png_ptr, 0, sizeof (png_struct));
870
871 png_ptr->error_fn = error_fn;
872 png_ptr->warning_fn = warning_fn;
873 png_ptr->error_ptr = error_ptr;
874 #ifdef PNG_USER_MEM_SUPPORTED
875 png_ptr->free_fn = free_fn;
876 #endif
877
878 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
879 }
880
881 void
882 png_set_read_status_fn(png_structp png_ptr, png_read_status_ptr read_row_fn)
883 {
884 png_ptr->read_row_fn = read_row_fn;
885 }