blob: d3a706edecb5ca14bda2f2b81d1c6f143f358a8b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Sun designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Sun in the LICENSE file that accompanied this code.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
22 * have any questions.
23 */
24
25/* png.c - location for general purpose libpng functions
26 *
27 * This file is available under and governed by the GNU General Public
28 * License version 2 only, as published by the Free Software Foundation.
29 * However, the following notice accompanied the original version of this
30 * file and, per its terms, should not be removed:
31 *
32 * Last changed in libpng 1.2.17 May 15, 2007
33 * For conditions of distribution and use, see copyright notice in png.h
34 * Copyright (c) 1998-2007 Glenn Randers-Pehrson
35 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
36 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
37 */
38
39#define PNG_INTERNAL
40#define PNG_NO_EXTERN
41#include "png.h"
42
43/* Generate a compiler error if there is an old png.h in the search path. */
44typedef version_1_2_18 Your_png_h_is_not_version_1_2_18;
45
46/* Version information for C files. This had better match the version
47 * string defined in png.h. */
48
49#ifdef PNG_USE_GLOBAL_ARRAYS
50/* png_libpng_ver was changed to a function in version 1.0.5c */
51const char png_libpng_ver[18] = PNG_LIBPNG_VER_STRING;
52
53#ifdef PNG_READ_SUPPORTED
54
55/* png_sig was changed to a function in version 1.0.5c */
56/* Place to hold the signature string for a PNG file. */
57const png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
58#endif /* PNG_READ_SUPPORTED */
59
60/* Invoke global declarations for constant strings for known chunk types */
61PNG_IHDR;
62PNG_IDAT;
63PNG_IEND;
64PNG_PLTE;
65PNG_bKGD;
66PNG_cHRM;
67PNG_gAMA;
68PNG_hIST;
69PNG_iCCP;
70PNG_iTXt;
71PNG_oFFs;
72PNG_pCAL;
73PNG_sCAL;
74PNG_pHYs;
75PNG_sBIT;
76PNG_sPLT;
77PNG_sRGB;
78PNG_tEXt;
79PNG_tIME;
80PNG_tRNS;
81PNG_zTXt;
82
83#ifdef PNG_READ_SUPPORTED
84/* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
85
86/* start of interlace block */
87const int FARDATA png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
88
89/* offset to next interlace block */
90const int FARDATA png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
91
92/* start of interlace block in the y direction */
93const int FARDATA png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
94
95/* offset to next interlace block in the y direction */
96const int FARDATA png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
97
98/* width of interlace block (used in assembler routines only) */
99#ifdef PNG_HAVE_MMX_COMBINE_ROW
100const int FARDATA png_pass_width[] = {8, 4, 4, 2, 2, 1, 1};
101#endif
102
103/* Height of interlace block. This is not currently used - if you need
104 * it, uncomment it here and in png.h
105const int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
106*/
107
108/* Mask to determine which pixels are valid in a pass */
109const int FARDATA png_pass_mask[] = {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
110
111/* Mask to determine which pixels to overwrite while displaying */
112const int FARDATA png_pass_dsp_mask[]
113 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
114
115#endif /* PNG_READ_SUPPORTED */
116#endif /* PNG_USE_GLOBAL_ARRAYS */
117
118/* Tells libpng that we have already handled the first "num_bytes" bytes
119 * of the PNG file signature. If the PNG data is embedded into another
120 * stream we can set num_bytes = 8 so that libpng will not attempt to read
121 * or write any of the magic bytes before it starts on the IHDR.
122 */
123
124#ifdef PNG_READ_SUPPORTED
125void PNGAPI
126png_set_sig_bytes(png_structp png_ptr, int num_bytes)
127{
128 if(png_ptr == NULL) return;
129 png_debug(1, "in png_set_sig_bytes\n");
130 if (num_bytes > 8)
131 png_error(png_ptr, "Too many bytes for PNG signature.");
132
133 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
134}
135
136/* Checks whether the supplied bytes match the PNG signature. We allow
137 * checking less than the full 8-byte signature so that those apps that
138 * already read the first few bytes of a file to determine the file type
139 * can simply check the remaining bytes for extra assurance. Returns
140 * an integer less than, equal to, or greater than zero if sig is found,
141 * respectively, to be less than, to match, or be greater than the correct
142 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
143 */
144int PNGAPI
145png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
146{
147 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
148 if (num_to_check > 8)
149 num_to_check = 8;
150 else if (num_to_check < 1)
151 return (-1);
152
153 if (start > 7)
154 return (-1);
155
156 if (start + num_to_check > 8)
157 num_to_check = 8 - start;
158
159 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
160}
161
162#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
163/* (Obsolete) function to check signature bytes. It does not allow one
164 * to check a partial signature. This function might be removed in the
165 * future - use png_sig_cmp(). Returns true (nonzero) if the file is a PNG.
166 */
167int PNGAPI
168png_check_sig(png_bytep sig, int num)
169{
170 return ((int)!png_sig_cmp(sig, (png_size_t)0, (png_size_t)num));
171}
172#endif
173#endif /* PNG_READ_SUPPORTED */
174
175#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
176/* Function to allocate memory for zlib and clear it to 0. */
177#ifdef PNG_1_0_X
178voidpf PNGAPI
179#else
180voidpf /* private */
181#endif
182png_zalloc(voidpf png_ptr, uInt items, uInt size)
183{
184 png_voidp ptr;
185 png_structp p=(png_structp)png_ptr;
186 png_uint_32 save_flags=p->flags;
187 png_uint_32 num_bytes;
188
189 if(png_ptr == NULL) return (NULL);
190 if (items > PNG_UINT_32_MAX/size)
191 {
192 png_warning (p, "Potential overflow in png_zalloc()");
193 return (NULL);
194 }
195 num_bytes = (png_uint_32)items * size;
196
197 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
198 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
199 p->flags=save_flags;
200
201#if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
202 if (ptr == NULL)
203 return ((voidpf)ptr);
204
205 if (num_bytes > (png_uint_32)0x8000L)
206 {
207 png_memset(ptr, 0, (png_size_t)0x8000L);
208 png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
209 (png_size_t)(num_bytes - (png_uint_32)0x8000L));
210 }
211 else
212 {
213 png_memset(ptr, 0, (png_size_t)num_bytes);
214 }
215#endif
216 return ((voidpf)ptr);
217}
218
219/* function to free memory for zlib */
220#ifdef PNG_1_0_X
221void PNGAPI
222#else
223void /* private */
224#endif
225png_zfree(voidpf png_ptr, voidpf ptr)
226{
227 png_free((png_structp)png_ptr, (png_voidp)ptr);
228}
229
230/* Reset the CRC variable to 32 bits of 1's. Care must be taken
231 * in case CRC is > 32 bits to leave the top bits 0.
232 */
233void /* PRIVATE */
234png_reset_crc(png_structp png_ptr)
235{
236 png_ptr->crc = crc32(0, Z_NULL, 0);
237}
238
239/* Calculate the CRC over a section of data. We can only pass as
240 * much data to this routine as the largest single buffer size. We
241 * also check that this data will actually be used before going to the
242 * trouble of calculating it.
243 */
244void /* PRIVATE */
245png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
246{
247 int need_crc = 1;
248
249 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
250 {
251 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
252 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
253 need_crc = 0;
254 }
255 else /* critical */
256 {
257 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
258 need_crc = 0;
259 }
260
261 if (need_crc)
262 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
263}
264
265/* Allocate the memory for an info_struct for the application. We don't
266 * really need the png_ptr, but it could potentially be useful in the
267 * future. This should be used in favour of malloc(png_sizeof(png_info))
268 * and png_info_init() so that applications that want to use a shared
269 * libpng don't have to be recompiled if png_info changes size.
270 */
271png_infop PNGAPI
272png_create_info_struct(png_structp png_ptr)
273{
274 png_infop info_ptr;
275
276 png_debug(1, "in png_create_info_struct\n");
277 if(png_ptr == NULL) return (NULL);
278#ifdef PNG_USER_MEM_SUPPORTED
279 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
280 png_ptr->malloc_fn, png_ptr->mem_ptr);
281#else
282 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
283#endif
284 if (info_ptr != NULL)
285 png_info_init_3(&info_ptr, png_sizeof(png_info));
286
287 return (info_ptr);
288}
289
290/* This function frees the memory associated with a single info struct.
291 * Normally, one would use either png_destroy_read_struct() or
292 * png_destroy_write_struct() to free an info struct, but this may be
293 * useful for some applications.
294 */
295void PNGAPI
296png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
297{
298 png_infop info_ptr = NULL;
299 if(png_ptr == NULL) return;
300
301 png_debug(1, "in png_destroy_info_struct\n");
302 if (info_ptr_ptr != NULL)
303 info_ptr = *info_ptr_ptr;
304
305 if (info_ptr != NULL)
306 {
307 png_info_destroy(png_ptr, info_ptr);
308
309#ifdef PNG_USER_MEM_SUPPORTED
310 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
311 png_ptr->mem_ptr);
312#else
313 png_destroy_struct((png_voidp)info_ptr);
314#endif
315 *info_ptr_ptr = NULL;
316 }
317}
318
319/* Initialize the info structure. This is now an internal function (0.89)
320 * and applications using it are urged to use png_create_info_struct()
321 * instead.
322 */
323#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
324#undef png_info_init
325void PNGAPI
326png_info_init(png_infop info_ptr)
327{
328 /* We only come here via pre-1.0.12-compiled applications */
329 png_info_init_3(&info_ptr, 0);
330}
331#endif
332
333void PNGAPI
334png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
335{
336 png_infop info_ptr = *ptr_ptr;
337
338 if(info_ptr == NULL) return;
339
340 png_debug(1, "in png_info_init_3\n");
341
342 if(png_sizeof(png_info) > png_info_struct_size)
343 {
344 png_destroy_struct(info_ptr);
345 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
346 *ptr_ptr = info_ptr;
347 }
348
349 /* set everything to 0 */
350 png_memset(info_ptr, 0, png_sizeof (png_info));
351}
352
353#ifdef PNG_FREE_ME_SUPPORTED
354void PNGAPI
355png_data_freer(png_structp png_ptr, png_infop info_ptr,
356 int freer, png_uint_32 mask)
357{
358 png_debug(1, "in png_data_freer\n");
359 if (png_ptr == NULL || info_ptr == NULL)
360 return;
361 if(freer == PNG_DESTROY_WILL_FREE_DATA)
362 info_ptr->free_me |= mask;
363 else if(freer == PNG_USER_WILL_FREE_DATA)
364 info_ptr->free_me &= ~mask;
365 else
366 png_warning(png_ptr,
367 "Unknown freer parameter in png_data_freer.");
368}
369#endif
370
371void PNGAPI
372png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
373 int num)
374{
375 png_debug(1, "in png_free_data\n");
376 if (png_ptr == NULL || info_ptr == NULL)
377 return;
378
379#if defined(PNG_TEXT_SUPPORTED)
380/* free text item num or (if num == -1) all text items */
381#ifdef PNG_FREE_ME_SUPPORTED
382if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
383#else
384if (mask & PNG_FREE_TEXT)
385#endif
386{
387 if (num != -1)
388 {
389 if (info_ptr->text && info_ptr->text[num].key)
390 {
391 png_free(png_ptr, info_ptr->text[num].key);
392 info_ptr->text[num].key = NULL;
393 }
394 }
395 else
396 {
397 int i;
398 for (i = 0; i < info_ptr->num_text; i++)
399 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
400 png_free(png_ptr, info_ptr->text);
401 info_ptr->text = NULL;
402 info_ptr->num_text=0;
403 }
404}
405#endif
406
407#if defined(PNG_tRNS_SUPPORTED)
408/* free any tRNS entry */
409#ifdef PNG_FREE_ME_SUPPORTED
410if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
411#else
412if ((mask & PNG_FREE_TRNS) && (png_ptr->flags & PNG_FLAG_FREE_TRNS))
413#endif
414{
415 png_free(png_ptr, info_ptr->trans);
416 info_ptr->valid &= ~PNG_INFO_tRNS;
417#ifndef PNG_FREE_ME_SUPPORTED
418 png_ptr->flags &= ~PNG_FLAG_FREE_TRNS;
419#endif
420 info_ptr->trans = NULL;
421}
422#endif
423
424#if defined(PNG_sCAL_SUPPORTED)
425/* free any sCAL entry */
426#ifdef PNG_FREE_ME_SUPPORTED
427if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
428#else
429if (mask & PNG_FREE_SCAL)
430#endif
431{
432#if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
433 png_free(png_ptr, info_ptr->scal_s_width);
434 png_free(png_ptr, info_ptr->scal_s_height);
435 info_ptr->scal_s_width = NULL;
436 info_ptr->scal_s_height = NULL;
437#endif
438 info_ptr->valid &= ~PNG_INFO_sCAL;
439}
440#endif
441
442#if defined(PNG_pCAL_SUPPORTED)
443/* free any pCAL entry */
444#ifdef PNG_FREE_ME_SUPPORTED
445if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
446#else
447if (mask & PNG_FREE_PCAL)
448#endif
449{
450 png_free(png_ptr, info_ptr->pcal_purpose);
451 png_free(png_ptr, info_ptr->pcal_units);
452 info_ptr->pcal_purpose = NULL;
453 info_ptr->pcal_units = NULL;
454 if (info_ptr->pcal_params != NULL)
455 {
456 int i;
457 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
458 {
459 png_free(png_ptr, info_ptr->pcal_params[i]);
460 info_ptr->pcal_params[i]=NULL;
461 }
462 png_free(png_ptr, info_ptr->pcal_params);
463 info_ptr->pcal_params = NULL;
464 }
465 info_ptr->valid &= ~PNG_INFO_pCAL;
466}
467#endif
468
469#if defined(PNG_iCCP_SUPPORTED)
470/* free any iCCP entry */
471#ifdef PNG_FREE_ME_SUPPORTED
472if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
473#else
474if (mask & PNG_FREE_ICCP)
475#endif
476{
477 png_free(png_ptr, info_ptr->iccp_name);
478 png_free(png_ptr, info_ptr->iccp_profile);
479 info_ptr->iccp_name = NULL;
480 info_ptr->iccp_profile = NULL;
481 info_ptr->valid &= ~PNG_INFO_iCCP;
482}
483#endif
484
485#if defined(PNG_sPLT_SUPPORTED)
486/* free a given sPLT entry, or (if num == -1) all sPLT entries */
487#ifdef PNG_FREE_ME_SUPPORTED
488if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
489#else
490if (mask & PNG_FREE_SPLT)
491#endif
492{
493 if (num != -1)
494 {
495 if(info_ptr->splt_palettes)
496 {
497 png_free(png_ptr, info_ptr->splt_palettes[num].name);
498 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
499 info_ptr->splt_palettes[num].name = NULL;
500 info_ptr->splt_palettes[num].entries = NULL;
501 }
502 }
503 else
504 {
505 if(info_ptr->splt_palettes_num)
506 {
507 int i;
508 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
509 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
510
511 png_free(png_ptr, info_ptr->splt_palettes);
512 info_ptr->splt_palettes = NULL;
513 info_ptr->splt_palettes_num = 0;
514 }
515 info_ptr->valid &= ~PNG_INFO_sPLT;
516 }
517}
518#endif
519
520#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
521 if(png_ptr->unknown_chunk.data)
522 {
523 png_free(png_ptr, png_ptr->unknown_chunk.data);
524 png_ptr->unknown_chunk.data = NULL;
525 }
526#ifdef PNG_FREE_ME_SUPPORTED
527if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
528#else
529if (mask & PNG_FREE_UNKN)
530#endif
531{
532 if (num != -1)
533 {
534 if(info_ptr->unknown_chunks)
535 {
536 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
537 info_ptr->unknown_chunks[num].data = NULL;
538 }
539 }
540 else
541 {
542 int i;
543
544 if(info_ptr->unknown_chunks_num)
545 {
546 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
547 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
548
549 png_free(png_ptr, info_ptr->unknown_chunks);
550 info_ptr->unknown_chunks = NULL;
551 info_ptr->unknown_chunks_num = 0;
552 }
553 }
554}
555#endif
556
557#if defined(PNG_hIST_SUPPORTED)
558/* free any hIST entry */
559#ifdef PNG_FREE_ME_SUPPORTED
560if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
561#else
562if ((mask & PNG_FREE_HIST) && (png_ptr->flags & PNG_FLAG_FREE_HIST))
563#endif
564{
565 png_free(png_ptr, info_ptr->hist);
566 info_ptr->hist = NULL;
567 info_ptr->valid &= ~PNG_INFO_hIST;
568#ifndef PNG_FREE_ME_SUPPORTED
569 png_ptr->flags &= ~PNG_FLAG_FREE_HIST;
570#endif
571}
572#endif
573
574/* free any PLTE entry that was internally allocated */
575#ifdef PNG_FREE_ME_SUPPORTED
576if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
577#else
578if ((mask & PNG_FREE_PLTE) && (png_ptr->flags & PNG_FLAG_FREE_PLTE))
579#endif
580{
581 png_zfree(png_ptr, info_ptr->palette);
582 info_ptr->palette = NULL;
583 info_ptr->valid &= ~PNG_INFO_PLTE;
584#ifndef PNG_FREE_ME_SUPPORTED
585 png_ptr->flags &= ~PNG_FLAG_FREE_PLTE;
586#endif
587 info_ptr->num_palette = 0;
588}
589
590#if defined(PNG_INFO_IMAGE_SUPPORTED)
591/* free any image bits attached to the info structure */
592#ifdef PNG_FREE_ME_SUPPORTED
593if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
594#else
595if (mask & PNG_FREE_ROWS)
596#endif
597{
598 if(info_ptr->row_pointers)
599 {
600 int row;
601 for (row = 0; row < (int)info_ptr->height; row++)
602 {
603 png_free(png_ptr, info_ptr->row_pointers[row]);
604 info_ptr->row_pointers[row]=NULL;
605 }
606 png_free(png_ptr, info_ptr->row_pointers);
607 info_ptr->row_pointers=NULL;
608 }
609 info_ptr->valid &= ~PNG_INFO_IDAT;
610}
611#endif
612
613#ifdef PNG_FREE_ME_SUPPORTED
614 if(num == -1)
615 info_ptr->free_me &= ~mask;
616 else
617 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
618#endif
619}
620
621/* This is an internal routine to free any memory that the info struct is
622 * pointing to before re-using it or freeing the struct itself. Recall
623 * that png_free() checks for NULL pointers for us.
624 */
625void /* PRIVATE */
626png_info_destroy(png_structp png_ptr, png_infop info_ptr)
627{
628 png_debug(1, "in png_info_destroy\n");
629
630 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
631
632#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
633 if (png_ptr->num_chunk_list)
634 {
635 png_free(png_ptr, png_ptr->chunk_list);
636 png_ptr->chunk_list=NULL;
637 png_ptr->num_chunk_list=0;
638 }
639#endif
640
641 png_info_init_3(&info_ptr, png_sizeof(png_info));
642}
643#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
644
645/* This function returns a pointer to the io_ptr associated with the user
646 * functions. The application should free any memory associated with this
647 * pointer before png_write_destroy() or png_read_destroy() are called.
648 */
649png_voidp PNGAPI
650png_get_io_ptr(png_structp png_ptr)
651{
652 if(png_ptr == NULL) return (NULL);
653 return (png_ptr->io_ptr);
654}
655
656#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
657#if !defined(PNG_NO_STDIO)
658/* Initialize the default input/output functions for the PNG file. If you
659 * use your own read or write routines, you can call either png_set_read_fn()
660 * or png_set_write_fn() instead of png_init_io(). If you have defined
661 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
662 * necessarily available.
663 */
664void PNGAPI
665png_init_io(png_structp png_ptr, png_FILE_p fp)
666{
667 png_debug(1, "in png_init_io\n");
668 if(png_ptr == NULL) return;
669 png_ptr->io_ptr = (png_voidp)fp;
670}
671#endif
672
673#if defined(PNG_TIME_RFC1123_SUPPORTED)
674/* Convert the supplied time into an RFC 1123 string suitable for use in
675 * a "Creation Time" or other text-based time string.
676 */
677png_charp PNGAPI
678png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
679{
680 static PNG_CONST char short_months[12][4] =
681 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
682 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
683
684 if(png_ptr == NULL) return (NULL);
685 if (png_ptr->time_buffer == NULL)
686 {
687 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
688 png_sizeof(char)));
689 }
690
691#if defined(_WIN32_WCE)
692 {
693 wchar_t time_buf[29];
694 wsprintf(time_buf, TEXT("%d %S %d %02d:%02d:%02d +0000"),
695 ptime->day % 32, short_months[(ptime->month - 1) % 12],
696 ptime->year, ptime->hour % 24, ptime->minute % 60,
697 ptime->second % 61);
698 WideCharToMultiByte(CP_ACP, 0, time_buf, -1, png_ptr->time_buffer, 29,
699 NULL, NULL);
700 }
701#else
702#ifdef USE_FAR_KEYWORD
703 {
704 char near_time_buf[29];
705 sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
706 ptime->day % 32, short_months[(ptime->month - 1) % 12],
707 ptime->year, ptime->hour % 24, ptime->minute % 60,
708 ptime->second % 61);
709 png_memcpy(png_ptr->time_buffer, near_time_buf,
710 29*png_sizeof(char));
711 }
712#else
713 sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
714 ptime->day % 32, short_months[(ptime->month - 1) % 12],
715 ptime->year, ptime->hour % 24, ptime->minute % 60,
716 ptime->second % 61);
717#endif
718#endif /* _WIN32_WCE */
719 return ((png_charp)png_ptr->time_buffer);
720}
721#endif /* PNG_TIME_RFC1123_SUPPORTED */
722
723#if 0
724/* Signature string for a PNG file. */
725png_bytep PNGAPI
726png_sig_bytes(void)
727{
728 return ((png_bytep)"\211\120\116\107\015\012\032\012");
729}
730#endif
731#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
732
733png_charp PNGAPI
734png_get_copyright(png_structp png_ptr)
735{
736 if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
737 return ((png_charp) "\n libpng version 1.2.18 - May 15, 2007\n\
738 Copyright (c) 1998-2007 Glenn Randers-Pehrson\n\
739 Copyright (c) 1996-1997 Andreas Dilger\n\
740 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.\n");
741 return ((png_charp) "");
742}
743
744/* The following return the library version as a short string in the
745 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
746 * used with your application, print out PNG_LIBPNG_VER_STRING, which
747 * is defined in png.h.
748 * Note: now there is no difference between png_get_libpng_ver() and
749 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
750 * it is guaranteed that png.c uses the correct version of png.h.
751 */
752png_charp PNGAPI
753png_get_libpng_ver(png_structp png_ptr)
754{
755 /* Version of *.c files used when building libpng */
756 if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
757 return ((png_charp) PNG_LIBPNG_VER_STRING);
758 return ((png_charp) "");
759}
760
761png_charp PNGAPI
762png_get_header_ver(png_structp png_ptr)
763{
764 /* Version of *.h files used when building libpng */
765 if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
766 return ((png_charp) PNG_LIBPNG_VER_STRING);
767 return ((png_charp) "");
768}
769
770png_charp PNGAPI
771png_get_header_version(png_structp png_ptr)
772{
773 /* Returns longer string containing both version and date */
774 if (&png_ptr != NULL) /* silence compiler warning about unused png_ptr */
775 return ((png_charp) PNG_HEADER_VERSION_STRING);
776 return ((png_charp) "");
777}
778
779#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
780#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
781int PNGAPI
782png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
783{
784 /* check chunk_name and return "keep" value if it's on the list, else 0 */
785 int i;
786 png_bytep p;
787 if((png_ptr == NULL && chunk_name == NULL) || png_ptr->num_chunk_list<=0)
788 return 0;
789 p=png_ptr->chunk_list+png_ptr->num_chunk_list*5-5;
790 for (i = png_ptr->num_chunk_list; i; i--, p-=5)
791 if (!png_memcmp(chunk_name, p, 4))
792 return ((int)*(p+4));
793 return 0;
794}
795#endif
796
797/* This function, added to libpng-1.0.6g, is untested. */
798int PNGAPI
799png_reset_zstream(png_structp png_ptr)
800{
801 if (png_ptr == NULL) return Z_STREAM_ERROR;
802 return (inflateReset(&png_ptr->zstream));
803}
804#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
805
806/* This function was added to libpng-1.0.7 */
807png_uint_32 PNGAPI
808png_access_version_number(void)
809{
810 /* Version of *.c files used when building libpng */
811 return((png_uint_32) PNG_LIBPNG_VER);
812}
813
814
815#if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
816#if !defined(PNG_1_0_X)
817#if defined(PNG_MMX_CODE_SUPPORTED)
818/* this INTERNAL function was added to libpng 1.2.0 */
819void /* PRIVATE */
820png_init_mmx_flags (png_structp png_ptr)
821{
822 if(png_ptr == NULL) return;
823 png_ptr->mmx_rowbytes_threshold = 0;
824 png_ptr->mmx_bitdepth_threshold = 0;
825
826# if (defined(PNG_USE_PNGVCRD) || defined(PNG_USE_PNGGCCRD))
827
828 png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_COMPILED;
829
830 if (png_mmx_support() > 0) {
831 png_ptr->asm_flags |= PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
832# ifdef PNG_HAVE_MMX_COMBINE_ROW
833 | PNG_ASM_FLAG_MMX_READ_COMBINE_ROW
834# endif
835# ifdef PNG_HAVE_MMX_READ_INTERLACE
836 | PNG_ASM_FLAG_MMX_READ_INTERLACE
837# endif
838# ifndef PNG_HAVE_MMX_READ_FILTER_ROW
839 ;
840# else
841 | PNG_ASM_FLAG_MMX_READ_FILTER_SUB
842 | PNG_ASM_FLAG_MMX_READ_FILTER_UP
843 | PNG_ASM_FLAG_MMX_READ_FILTER_AVG
844 | PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ;
845
846 png_ptr->mmx_rowbytes_threshold = PNG_MMX_ROWBYTES_THRESHOLD_DEFAULT;
847 png_ptr->mmx_bitdepth_threshold = PNG_MMX_BITDEPTH_THRESHOLD_DEFAULT;
848# endif
849 } else {
850 png_ptr->asm_flags &= ~( PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU
851 | PNG_MMX_READ_FLAGS
852 | PNG_MMX_WRITE_FLAGS );
853 }
854
855# else /* !(PNGVCRD || PNGGCCRD) */
856
857 /* clear all MMX flags; no support is compiled in */
858 png_ptr->asm_flags &= ~( PNG_MMX_FLAGS );
859
860# endif /* ?(PNGVCRD || PNGGCCRD) */
861}
862
863#endif /* !(PNG_MMX_CODE_SUPPORTED) */
864
865/* this function was added to libpng 1.2.0 */
866#if !defined(PNG_USE_PNGGCCRD) && \
867 !(defined(PNG_MMX_CODE_SUPPORTED) && defined(PNG_USE_PNGVCRD))
868int PNGAPI
869png_mmx_support(void)
870{
871 return -1;
872}
873#endif
874#endif /* PNG_1_0_X && PNG_ASSEMBLER_CODE_SUPPORTED */
875#endif /* PNG_READ_SUPPORTED */
876
877#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
878#ifdef PNG_SIZE_T
879/* Added at libpng version 1.2.6 */
880 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
881png_size_t PNGAPI
882png_convert_size(size_t size)
883{
884 if (size > (png_size_t)-1)
885 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
886 return ((png_size_t)size);
887}
888#endif /* PNG_SIZE_T */
889#endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */