2 /* png.c - location for general purpose libpng functions
4 * Last changed in libpng 1.2.43 [February 25, 2010]
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
9 * This code is released under the libpng license.
10 * For conditions of distribution and use, see the disclaimer
11 * and license in png.h
16 #define PNG_NO_PEDANTIC_WARNINGS
19 /* Generate a compiler error if there is an old png.h in the search path. */
20 typedef version_1_2_45 Your_png_h_is_not_version_1_2_45
;
22 /* Version information for C files. This had better match the version
23 * string defined in png.h.
26 #ifdef PNG_USE_GLOBAL_ARRAYS
27 /* png_libpng_ver was changed to a function in version 1.0.5c */
28 PNG_CONST
char png_libpng_ver
[18] = PNG_LIBPNG_VER_STRING
;
30 #ifdef PNG_READ_SUPPORTED
32 /* png_sig was changed to a function in version 1.0.5c */
33 /* Place to hold the signature string for a PNG file. */
34 PNG_CONST png_byte FARDATA png_sig
[8] = {137, 80, 78, 71, 13, 10, 26, 10};
35 #endif /* PNG_READ_SUPPORTED */
37 /* Invoke global declarations for constant strings for known chunk types */
60 #ifdef PNG_READ_SUPPORTED
61 /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
63 /* Start of interlace block */
64 PNG_CONST
int FARDATA png_pass_start
[] = {0, 4, 0, 2, 0, 1, 0};
66 /* Offset to next interlace block */
67 PNG_CONST
int FARDATA png_pass_inc
[] = {8, 8, 4, 4, 2, 2, 1};
69 /* Start of interlace block in the y direction */
70 PNG_CONST
int FARDATA png_pass_ystart
[] = {0, 0, 4, 0, 2, 0, 1};
72 /* Offset to next interlace block in the y direction */
73 PNG_CONST
int FARDATA png_pass_yinc
[] = {8, 8, 8, 4, 4, 2, 2};
75 /* Height of interlace block. This is not currently used - if you need
76 * it, uncomment it here and in png.h
77 PNG_CONST int FARDATA png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
80 /* Mask to determine which pixels are valid in a pass */
81 PNG_CONST
int FARDATA png_pass_mask
[] =
82 {0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff};
84 /* Mask to determine which pixels to overwrite while displaying */
85 PNG_CONST
int FARDATA png_pass_dsp_mask
[]
86 = {0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff};
88 #endif /* PNG_READ_SUPPORTED */
89 #endif /* PNG_USE_GLOBAL_ARRAYS */
91 /* Tells libpng that we have already handled the first "num_bytes" bytes
92 * of the PNG file signature. If the PNG data is embedded into another
93 * stream we can set num_bytes = 8 so that libpng will not attempt to read
94 * or write any of the magic bytes before it starts on the IHDR.
97 #ifdef PNG_READ_SUPPORTED
99 png_set_sig_bytes(png_structp png_ptr
, int num_bytes
)
101 png_debug(1, "in png_set_sig_bytes");
107 png_error(png_ptr
, "Too many bytes for PNG signature.");
109 png_ptr
->sig_bytes
= (png_byte
)(num_bytes
< 0 ? 0 : num_bytes
);
112 /* Checks whether the supplied bytes match the PNG signature. We allow
113 * checking less than the full 8-byte signature so that those apps that
114 * already read the first few bytes of a file to determine the file type
115 * can simply check the remaining bytes for extra assurance. Returns
116 * an integer less than, equal to, or greater than zero if sig is found,
117 * respectively, to be less than, to match, or be greater than the correct
118 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
121 png_sig_cmp(png_bytep sig
, png_size_t start
, png_size_t num_to_check
)
123 png_byte png_signature
[8] = {137, 80, 78, 71, 13, 10, 26, 10};
124 if (num_to_check
> 8)
126 else if (num_to_check
< 1)
132 if (start
+ num_to_check
> 8)
133 num_to_check
= 8 - start
;
135 return ((int)(png_memcmp(&sig
[start
], &png_signature
[start
], num_to_check
)));
138 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
139 /* (Obsolete) function to check signature bytes. It does not allow one
140 * to check a partial signature. This function might be removed in the
141 * future - use png_sig_cmp(). Returns true (nonzero) if the file is PNG.
144 png_check_sig(png_bytep sig
, int num
)
146 return ((int)!png_sig_cmp(sig
, (png_size_t
)0, (png_size_t
)num
));
149 #endif /* PNG_READ_SUPPORTED */
151 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
152 /* Function to allocate memory for zlib and clear it to 0. */
158 png_zalloc(voidpf png_ptr
, uInt items
, uInt size
)
161 png_structp p
=(png_structp
)png_ptr
;
162 png_uint_32 save_flags
=p
->flags
;
163 png_uint_32 num_bytes
;
167 if (items
> PNG_UINT_32_MAX
/size
)
169 png_warning (p
, "Potential overflow in png_zalloc()");
172 num_bytes
= (png_uint_32
)items
* size
;
174 p
->flags
|=PNG_FLAG_MALLOC_NULL_MEM_OK
;
175 ptr
= (png_voidp
)png_malloc((png_structp
)png_ptr
, num_bytes
);
178 #if defined(PNG_1_0_X) && !defined(PNG_NO_ZALLOC_ZERO)
180 return ((voidpf
)ptr
);
182 if (num_bytes
> (png_uint_32
)0x8000L
)
184 png_memset(ptr
, 0, (png_size_t
)0x8000L
);
185 png_memset((png_bytep
)ptr
+ (png_size_t
)0x8000L
, 0,
186 (png_size_t
)(num_bytes
- (png_uint_32
)0x8000L
));
190 png_memset(ptr
, 0, (png_size_t
)num_bytes
);
193 return ((voidpf
)ptr
);
196 /* Function to free memory for zlib */
202 png_zfree(voidpf png_ptr
, voidpf ptr
)
204 png_free((png_structp
)png_ptr
, (png_voidp
)ptr
);
207 /* Reset the CRC variable to 32 bits of 1's. Care must be taken
208 * in case CRC is > 32 bits to leave the top bits 0.
211 png_reset_crc(png_structp png_ptr
)
213 png_ptr
->crc
= crc32(0, Z_NULL
, 0);
216 /* Calculate the CRC over a section of data. We can only pass as
217 * much data to this routine as the largest single buffer size. We
218 * also check that this data will actually be used before going to the
219 * trouble of calculating it.
222 png_calculate_crc(png_structp png_ptr
, png_bytep ptr
, png_size_t length
)
226 if (png_ptr
->chunk_name
[0] & 0x20) /* ancillary */
228 if ((png_ptr
->flags
& PNG_FLAG_CRC_ANCILLARY_MASK
) ==
229 (PNG_FLAG_CRC_ANCILLARY_USE
| PNG_FLAG_CRC_ANCILLARY_NOWARN
))
234 if (png_ptr
->flags
& PNG_FLAG_CRC_CRITICAL_IGNORE
)
239 png_ptr
->crc
= crc32(png_ptr
->crc
, ptr
, (uInt
)length
);
242 /* Allocate the memory for an info_struct for the application. We don't
243 * really need the png_ptr, but it could potentially be useful in the
244 * future. This should be used in favour of malloc(png_sizeof(png_info))
245 * and png_info_init() so that applications that want to use a shared
246 * libpng don't have to be recompiled if png_info changes size.
249 png_create_info_struct(png_structp png_ptr
)
253 png_debug(1, "in png_create_info_struct");
258 #ifdef PNG_USER_MEM_SUPPORTED
259 info_ptr
= (png_infop
)png_create_struct_2(PNG_STRUCT_INFO
,
260 png_ptr
->malloc_fn
, png_ptr
->mem_ptr
);
262 info_ptr
= (png_infop
)png_create_struct(PNG_STRUCT_INFO
);
264 if (info_ptr
!= NULL
)
265 png_info_init_3(&info_ptr
, png_sizeof(png_info
));
270 /* This function frees the memory associated with a single info struct.
271 * Normally, one would use either png_destroy_read_struct() or
272 * png_destroy_write_struct() to free an info struct, but this may be
273 * useful for some applications.
276 png_destroy_info_struct(png_structp png_ptr
, png_infopp info_ptr_ptr
)
278 png_infop info_ptr
= NULL
;
280 png_debug(1, "in png_destroy_info_struct");
285 if (info_ptr_ptr
!= NULL
)
286 info_ptr
= *info_ptr_ptr
;
288 if (info_ptr
!= NULL
)
290 png_info_destroy(png_ptr
, info_ptr
);
292 #ifdef PNG_USER_MEM_SUPPORTED
293 png_destroy_struct_2((png_voidp
)info_ptr
, png_ptr
->free_fn
,
296 png_destroy_struct((png_voidp
)info_ptr
);
298 *info_ptr_ptr
= NULL
;
302 /* Initialize the info structure. This is now an internal function (0.89)
303 * and applications using it are urged to use png_create_info_struct()
306 #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
309 png_info_init(png_infop info_ptr
)
311 /* We only come here via pre-1.0.12-compiled applications */
312 png_info_init_3(&info_ptr
, 0);
317 png_info_init_3(png_infopp ptr_ptr
, png_size_t png_info_struct_size
)
319 png_infop info_ptr
= *ptr_ptr
;
321 png_debug(1, "in png_info_init_3");
323 if (info_ptr
== NULL
)
326 if (png_sizeof(png_info
) > png_info_struct_size
)
328 png_destroy_struct(info_ptr
);
329 info_ptr
= (png_infop
)png_create_struct(PNG_STRUCT_INFO
);
333 /* Set everything to 0 */
334 png_memset(info_ptr
, 0, png_sizeof(png_info
));
337 #ifdef PNG_FREE_ME_SUPPORTED
339 png_data_freer(png_structp png_ptr
, png_infop info_ptr
,
340 int freer
, png_uint_32 mask
)
342 png_debug(1, "in png_data_freer");
344 if (png_ptr
== NULL
|| info_ptr
== NULL
)
347 if (freer
== PNG_DESTROY_WILL_FREE_DATA
)
348 info_ptr
->free_me
|= mask
;
349 else if (freer
== PNG_USER_WILL_FREE_DATA
)
350 info_ptr
->free_me
&= ~mask
;
353 "Unknown freer parameter in png_data_freer.");
358 png_free_data(png_structp png_ptr
, png_infop info_ptr
, png_uint_32 mask
,
361 png_debug(1, "in png_free_data");
363 if (png_ptr
== NULL
|| info_ptr
== NULL
)
366 #ifdef PNG_TEXT_SUPPORTED
367 /* Free text item num or (if num == -1) all text items */
368 #ifdef PNG_FREE_ME_SUPPORTED
369 if ((mask
& PNG_FREE_TEXT
) & info_ptr
->free_me
)
371 if (mask
& PNG_FREE_TEXT
)
376 if (info_ptr
->text
&& info_ptr
->text
[num
].key
)
378 png_free(png_ptr
, info_ptr
->text
[num
].key
);
379 info_ptr
->text
[num
].key
= NULL
;
385 for (i
= 0; i
< info_ptr
->num_text
; i
++)
386 png_free_data(png_ptr
, info_ptr
, PNG_FREE_TEXT
, i
);
387 png_free(png_ptr
, info_ptr
->text
);
388 info_ptr
->text
= NULL
;
389 info_ptr
->num_text
=0;
394 #ifdef PNG_tRNS_SUPPORTED
395 /* Free any tRNS entry */
396 #ifdef PNG_FREE_ME_SUPPORTED
397 if ((mask
& PNG_FREE_TRNS
) & info_ptr
->free_me
)
399 if ((mask
& PNG_FREE_TRNS
) && (png_ptr
->flags
& PNG_FLAG_FREE_TRNS
))
402 png_free(png_ptr
, info_ptr
->trans
);
403 info_ptr
->trans
= NULL
;
404 info_ptr
->valid
&= ~PNG_INFO_tRNS
;
405 #ifndef PNG_FREE_ME_SUPPORTED
406 png_ptr
->flags
&= ~PNG_FLAG_FREE_TRNS
;
411 #ifdef PNG_sCAL_SUPPORTED
412 /* Free any sCAL entry */
413 #ifdef PNG_FREE_ME_SUPPORTED
414 if ((mask
& PNG_FREE_SCAL
) & info_ptr
->free_me
)
416 if (mask
& PNG_FREE_SCAL
)
419 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
420 png_free(png_ptr
, info_ptr
->scal_s_width
);
421 png_free(png_ptr
, info_ptr
->scal_s_height
);
422 info_ptr
->scal_s_width
= NULL
;
423 info_ptr
->scal_s_height
= NULL
;
425 info_ptr
->valid
&= ~PNG_INFO_sCAL
;
429 #ifdef PNG_pCAL_SUPPORTED
430 /* Free any pCAL entry */
431 #ifdef PNG_FREE_ME_SUPPORTED
432 if ((mask
& PNG_FREE_PCAL
) & info_ptr
->free_me
)
434 if (mask
& PNG_FREE_PCAL
)
437 png_free(png_ptr
, info_ptr
->pcal_purpose
);
438 png_free(png_ptr
, info_ptr
->pcal_units
);
439 info_ptr
->pcal_purpose
= NULL
;
440 info_ptr
->pcal_units
= NULL
;
441 if (info_ptr
->pcal_params
!= NULL
)
444 for (i
= 0; i
< (int)info_ptr
->pcal_nparams
; i
++)
446 png_free(png_ptr
, info_ptr
->pcal_params
[i
]);
447 info_ptr
->pcal_params
[i
] = NULL
;
449 png_free(png_ptr
, info_ptr
->pcal_params
);
450 info_ptr
->pcal_params
= NULL
;
452 info_ptr
->valid
&= ~PNG_INFO_pCAL
;
456 #ifdef PNG_iCCP_SUPPORTED
457 /* Free any iCCP entry */
458 #ifdef PNG_FREE_ME_SUPPORTED
459 if ((mask
& PNG_FREE_ICCP
) & info_ptr
->free_me
)
461 if (mask
& PNG_FREE_ICCP
)
464 png_free(png_ptr
, info_ptr
->iccp_name
);
465 png_free(png_ptr
, info_ptr
->iccp_profile
);
466 info_ptr
->iccp_name
= NULL
;
467 info_ptr
->iccp_profile
= NULL
;
468 info_ptr
->valid
&= ~PNG_INFO_iCCP
;
472 #ifdef PNG_sPLT_SUPPORTED
473 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
474 #ifdef PNG_FREE_ME_SUPPORTED
475 if ((mask
& PNG_FREE_SPLT
) & info_ptr
->free_me
)
477 if (mask
& PNG_FREE_SPLT
)
482 if (info_ptr
->splt_palettes
)
484 png_free(png_ptr
, info_ptr
->splt_palettes
[num
].name
);
485 png_free(png_ptr
, info_ptr
->splt_palettes
[num
].entries
);
486 info_ptr
->splt_palettes
[num
].name
= NULL
;
487 info_ptr
->splt_palettes
[num
].entries
= NULL
;
492 if (info_ptr
->splt_palettes_num
)
495 for (i
= 0; i
< (int)info_ptr
->splt_palettes_num
; i
++)
496 png_free_data(png_ptr
, info_ptr
, PNG_FREE_SPLT
, i
);
498 png_free(png_ptr
, info_ptr
->splt_palettes
);
499 info_ptr
->splt_palettes
= NULL
;
500 info_ptr
->splt_palettes_num
= 0;
502 info_ptr
->valid
&= ~PNG_INFO_sPLT
;
507 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
508 if (png_ptr
->unknown_chunk
.data
)
510 png_free(png_ptr
, png_ptr
->unknown_chunk
.data
);
511 png_ptr
->unknown_chunk
.data
= NULL
;
514 #ifdef PNG_FREE_ME_SUPPORTED
515 if ((mask
& PNG_FREE_UNKN
) & info_ptr
->free_me
)
517 if (mask
& PNG_FREE_UNKN
)
522 if (info_ptr
->unknown_chunks
)
524 png_free(png_ptr
, info_ptr
->unknown_chunks
[num
].data
);
525 info_ptr
->unknown_chunks
[num
].data
= NULL
;
532 if (info_ptr
->unknown_chunks_num
)
534 for (i
= 0; i
< (int)info_ptr
->unknown_chunks_num
; i
++)
535 png_free_data(png_ptr
, info_ptr
, PNG_FREE_UNKN
, i
);
537 png_free(png_ptr
, info_ptr
->unknown_chunks
);
538 info_ptr
->unknown_chunks
= NULL
;
539 info_ptr
->unknown_chunks_num
= 0;
545 #ifdef PNG_hIST_SUPPORTED
546 /* Free any hIST entry */
547 #ifdef PNG_FREE_ME_SUPPORTED
548 if ((mask
& PNG_FREE_HIST
) & info_ptr
->free_me
)
550 if ((mask
& PNG_FREE_HIST
) && (png_ptr
->flags
& PNG_FLAG_FREE_HIST
))
553 png_free(png_ptr
, info_ptr
->hist
);
554 info_ptr
->hist
= NULL
;
555 info_ptr
->valid
&= ~PNG_INFO_hIST
;
556 #ifndef PNG_FREE_ME_SUPPORTED
557 png_ptr
->flags
&= ~PNG_FLAG_FREE_HIST
;
562 /* Free any PLTE entry that was internally allocated */
563 #ifdef PNG_FREE_ME_SUPPORTED
564 if ((mask
& PNG_FREE_PLTE
) & info_ptr
->free_me
)
566 if ((mask
& PNG_FREE_PLTE
) && (png_ptr
->flags
& PNG_FLAG_FREE_PLTE
))
569 png_zfree(png_ptr
, info_ptr
->palette
);
570 info_ptr
->palette
= NULL
;
571 info_ptr
->valid
&= ~PNG_INFO_PLTE
;
572 #ifndef PNG_FREE_ME_SUPPORTED
573 png_ptr
->flags
&= ~PNG_FLAG_FREE_PLTE
;
575 info_ptr
->num_palette
= 0;
578 #ifdef PNG_INFO_IMAGE_SUPPORTED
579 /* Free any image bits attached to the info structure */
580 #ifdef PNG_FREE_ME_SUPPORTED
581 if ((mask
& PNG_FREE_ROWS
) & info_ptr
->free_me
)
583 if (mask
& PNG_FREE_ROWS
)
586 if (info_ptr
->row_pointers
)
589 for (row
= 0; row
< (int)info_ptr
->height
; row
++)
591 png_free(png_ptr
, info_ptr
->row_pointers
[row
]);
592 info_ptr
->row_pointers
[row
] = NULL
;
594 png_free(png_ptr
, info_ptr
->row_pointers
);
595 info_ptr
->row_pointers
= NULL
;
597 info_ptr
->valid
&= ~PNG_INFO_IDAT
;
601 #ifdef PNG_FREE_ME_SUPPORTED
603 info_ptr
->free_me
&= ~mask
;
605 info_ptr
->free_me
&= ~(mask
& ~PNG_FREE_MUL
);
609 /* This is an internal routine to free any memory that the info struct is
610 * pointing to before re-using it or freeing the struct itself. Recall
611 * that png_free() checks for NULL pointers for us.
614 png_info_destroy(png_structp png_ptr
, png_infop info_ptr
)
616 png_debug(1, "in png_info_destroy");
618 png_free_data(png_ptr
, info_ptr
, PNG_FREE_ALL
, -1);
620 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
621 if (png_ptr
->num_chunk_list
)
623 png_free(png_ptr
, png_ptr
->chunk_list
);
624 png_ptr
->chunk_list
= NULL
;
625 png_ptr
->num_chunk_list
= 0;
629 png_info_init_3(&info_ptr
, png_sizeof(png_info
));
631 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
633 /* This function returns a pointer to the io_ptr associated with the user
634 * functions. The application should free any memory associated with this
635 * pointer before png_write_destroy() or png_read_destroy() are called.
638 png_get_io_ptr(png_structp png_ptr
)
642 return (png_ptr
->io_ptr
);
645 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
646 #ifdef PNG_STDIO_SUPPORTED
647 /* Initialize the default input/output functions for the PNG file. If you
648 * use your own read or write routines, you can call either png_set_read_fn()
649 * or png_set_write_fn() instead of png_init_io(). If you have defined
650 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
651 * necessarily available.
654 png_init_io(png_structp png_ptr
, png_FILE_p fp
)
656 png_debug(1, "in png_init_io");
661 png_ptr
->io_ptr
= (png_voidp
)fp
;
665 #ifdef PNG_TIME_RFC1123_SUPPORTED
666 /* Convert the supplied time into an RFC 1123 string suitable for use in
667 * a "Creation Time" or other text-based time string.
670 png_convert_to_rfc1123(png_structp png_ptr
, png_timep ptime
)
672 static PNG_CONST
char short_months
[12][4] =
673 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
674 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
678 if (png_ptr
->time_buffer
== NULL
)
680 png_ptr
->time_buffer
= (png_charp
)png_malloc(png_ptr
, (png_uint_32
)(29*
686 wchar_t time_buf
[29];
687 wsprintf(time_buf
, TEXT("%d %S %d %02d:%02d:%02d +0000"),
688 ptime
->day
% 32, short_months
[(ptime
->month
- 1) % 12],
689 ptime
->year
, ptime
->hour
% 24, ptime
->minute
% 60,
691 WideCharToMultiByte(CP_ACP
, 0, time_buf
, -1, png_ptr
->time_buffer
,
695 #ifdef USE_FAR_KEYWORD
697 char near_time_buf
[29];
698 png_snprintf6(near_time_buf
, 29, "%d %s %d %02d:%02d:%02d +0000",
699 ptime
->day
% 32, short_months
[(ptime
->month
- 1) % 12],
700 ptime
->year
, ptime
->hour
% 24, ptime
->minute
% 60,
702 png_memcpy(png_ptr
->time_buffer
, near_time_buf
,
703 29*png_sizeof(char));
706 png_snprintf6(png_ptr
->time_buffer
, 29, "%d %s %d %02d:%02d:%02d +0000",
707 ptime
->day
% 32, short_months
[(ptime
->month
- 1) % 12],
708 ptime
->year
, ptime
->hour
% 24, ptime
->minute
% 60,
711 #endif /* _WIN32_WCE */
712 return ((png_charp
)png_ptr
->time_buffer
);
714 #endif /* PNG_TIME_RFC1123_SUPPORTED */
716 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
719 png_get_copyright(png_structp png_ptr
)
721 png_ptr
= png_ptr
; /* Silence compiler warning about unused png_ptr */
722 #ifdef PNG_STRING_COPYRIGHT
723 return PNG_STRING_COPYRIGHT
726 return ((png_charp
) PNG_STRING_NEWLINE \
727 "libpng version 1.2.45 - July 7, 2011" PNG_STRING_NEWLINE \
728 "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
729 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
730 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
733 return ((png_charp
) "libpng version 1.2.45 - July 7, 2011\
734 Copyright (c) 1998-2010 Glenn Randers-Pehrson\
735 Copyright (c) 1996-1997 Andreas Dilger\
736 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
741 /* The following return the library version as a short string in the
742 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
743 * used with your application, print out PNG_LIBPNG_VER_STRING, which
744 * is defined in png.h.
745 * Note: now there is no difference between png_get_libpng_ver() and
746 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
747 * it is guaranteed that png.c uses the correct version of png.h.
750 png_get_libpng_ver(png_structp png_ptr
)
752 /* Version of *.c files used when building libpng */
753 png_ptr
= png_ptr
; /* Silence compiler warning about unused png_ptr */
754 return ((png_charp
) PNG_LIBPNG_VER_STRING
);
758 png_get_header_ver(png_structp png_ptr
)
760 /* Version of *.h files used when building libpng */
761 png_ptr
= png_ptr
; /* Silence compiler warning about unused png_ptr */
762 return ((png_charp
) PNG_LIBPNG_VER_STRING
);
766 png_get_header_version(png_structp png_ptr
)
768 /* Returns longer string containing both version and date */
769 png_ptr
= png_ptr
; /* Silence compiler warning about unused png_ptr */
771 return ((png_charp
) PNG_HEADER_VERSION_STRING
772 #ifndef PNG_READ_SUPPORTED
777 return ((png_charp
) PNG_HEADER_VERSION_STRING
);
781 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
782 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
784 png_handle_as_unknown(png_structp png_ptr
, png_bytep chunk_name
)
786 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
789 if (png_ptr
== NULL
|| chunk_name
== NULL
|| png_ptr
->num_chunk_list
<=0)
791 p
= png_ptr
->chunk_list
+ png_ptr
->num_chunk_list
*5 - 5;
792 for (i
= png_ptr
->num_chunk_list
; i
; i
--, p
-= 5)
793 if (!png_memcmp(chunk_name
, p
, 4))
794 return ((int)*(p
+ 4));
799 /* This function, added to libpng-1.0.6g, is untested. */
801 png_reset_zstream(png_structp png_ptr
)
804 return Z_STREAM_ERROR
;
805 return (inflateReset(&png_ptr
->zstream
));
807 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
809 /* This function was added to libpng-1.0.7 */
811 png_access_version_number(void)
813 /* Version of *.c files used when building libpng */
814 return((png_uint_32
) PNG_LIBPNG_VER
);
818 #if defined(PNG_READ_SUPPORTED) && defined(PNG_ASSEMBLER_CODE_SUPPORTED)
820 /* This function was added to libpng 1.2.0 */
822 png_mmx_support(void)
824 /* Obsolete, to be removed from libpng-1.4.0 */
827 #endif /* PNG_1_0_X */
828 #endif /* PNG_READ_SUPPORTED && PNG_ASSEMBLER_CODE_SUPPORTED */
830 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
832 /* Added at libpng version 1.2.6 */
833 PNG_EXTERN png_size_t PNGAPI png_convert_size
PNGARG((size_t size
));
835 png_convert_size(size_t size
)
837 if (size
> (png_size_t
)-1)
838 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
839 return ((png_size_t
)size
);
841 #endif /* PNG_SIZE_T */
843 /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
844 #ifdef PNG_cHRM_SUPPORTED
845 #ifdef PNG_CHECK_cHRM_SUPPORTED
848 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
849 * arithmetic, to produce a 64 bit result in the HI/LO words.
857 * where A and B are the high and low 16-bit words of V1,
858 * C and D are the 16-bit words of V2, AD is the product of
859 * A and D, and X || Y is (X << 16) + Y.
863 png_64bit_product (long v1
, long v2
, unsigned long *hi_product
,
864 unsigned long *lo_product
)
869 a
= (v1
>> 16) & 0xffff;
871 c
= (v2
>> 16) & 0xffff;
875 x
= a
* d
+ c
* b
; /* AD + CB */
876 y
= ((lo
>> 16) & 0xffff) + x
;
878 lo
= (lo
& 0xffff) | ((y
& 0xffff) << 16);
879 hi
= (y
>> 16) & 0xffff;
881 hi
+= a
* c
; /* AC */
883 *hi_product
= (unsigned long)hi
;
884 *lo_product
= (unsigned long)lo
;
888 png_check_cHRM_fixed(png_structp png_ptr
,
889 png_fixed_point white_x
, png_fixed_point white_y
, png_fixed_point red_x
,
890 png_fixed_point red_y
, png_fixed_point green_x
, png_fixed_point green_y
,
891 png_fixed_point blue_x
, png_fixed_point blue_y
)
894 unsigned long xy_hi
,xy_lo
,yx_hi
,yx_lo
;
896 png_debug(1, "in function png_check_cHRM_fixed");
901 if (white_x
< 0 || white_y
<= 0 ||
902 red_x
< 0 || red_y
< 0 ||
903 green_x
< 0 || green_y
< 0 ||
904 blue_x
< 0 || blue_y
< 0)
907 "Ignoring attempt to set negative chromaticity value");
910 if (white_x
> (png_fixed_point
) PNG_UINT_31_MAX
||
911 white_y
> (png_fixed_point
) PNG_UINT_31_MAX
||
912 red_x
> (png_fixed_point
) PNG_UINT_31_MAX
||
913 red_y
> (png_fixed_point
) PNG_UINT_31_MAX
||
914 green_x
> (png_fixed_point
) PNG_UINT_31_MAX
||
915 green_y
> (png_fixed_point
) PNG_UINT_31_MAX
||
916 blue_x
> (png_fixed_point
) PNG_UINT_31_MAX
||
917 blue_y
> (png_fixed_point
) PNG_UINT_31_MAX
)
920 "Ignoring attempt to set chromaticity value exceeding 21474.83");
923 if (white_x
> 100000L - white_y
)
925 png_warning(png_ptr
, "Invalid cHRM white point");
928 if (red_x
> 100000L - red_y
)
930 png_warning(png_ptr
, "Invalid cHRM red point");
933 if (green_x
> 100000L - green_y
)
935 png_warning(png_ptr
, "Invalid cHRM green point");
938 if (blue_x
> 100000L - blue_y
)
940 png_warning(png_ptr
, "Invalid cHRM blue point");
944 png_64bit_product(green_x
- red_x
, blue_y
- red_y
, &xy_hi
, &xy_lo
);
945 png_64bit_product(green_y
- red_y
, blue_x
- red_x
, &yx_hi
, &yx_lo
);
947 if (xy_hi
== yx_hi
&& xy_lo
== yx_lo
)
950 "Ignoring attempt to set cHRM RGB triangle with zero area");
956 #endif /* PNG_CHECK_cHRM_SUPPORTED */
957 #endif /* PNG_cHRM_SUPPORTED */
960 png_check_IHDR(png_structp png_ptr
,
961 png_uint_32 width
, png_uint_32 height
, int bit_depth
,
962 int color_type
, int interlace_type
, int compression_type
,
967 /* Check for width and height valid values */
970 png_warning(png_ptr
, "Image width is zero in IHDR");
976 png_warning(png_ptr
, "Image height is zero in IHDR");
980 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
981 if (width
> png_ptr
->user_width_max
|| width
> PNG_USER_WIDTH_MAX
)
983 if (width
> PNG_USER_WIDTH_MAX
)
986 png_warning(png_ptr
, "Image width exceeds user limit in IHDR");
990 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
991 if (height
> png_ptr
->user_height_max
|| height
> PNG_USER_HEIGHT_MAX
)
993 if (height
> PNG_USER_HEIGHT_MAX
)
996 png_warning(png_ptr
, "Image height exceeds user limit in IHDR");
1000 if (width
> PNG_UINT_31_MAX
)
1002 png_warning(png_ptr
, "Invalid image width in IHDR");
1006 if ( height
> PNG_UINT_31_MAX
)
1008 png_warning(png_ptr
, "Invalid image height in IHDR");
1012 if ( width
> (PNG_UINT_32_MAX
1013 >> 3) /* 8-byte RGBA pixels */
1014 - 64 /* bigrowbuf hack */
1015 - 1 /* filter byte */
1016 - 7*8 /* rounding of width to multiple of 8 pixels */
1017 - 8) /* extra max_pixel_depth pad */
1018 png_warning(png_ptr
, "Width is too large for libpng to process pixels");
1020 /* Check other values */
1021 if (bit_depth
!= 1 && bit_depth
!= 2 && bit_depth
!= 4 &&
1022 bit_depth
!= 8 && bit_depth
!= 16)
1024 png_warning(png_ptr
, "Invalid bit depth in IHDR");
1028 if (color_type
< 0 || color_type
== 1 ||
1029 color_type
== 5 || color_type
> 6)
1031 png_warning(png_ptr
, "Invalid color type in IHDR");
1035 if (((color_type
== PNG_COLOR_TYPE_PALETTE
) && bit_depth
> 8) ||
1036 ((color_type
== PNG_COLOR_TYPE_RGB
||
1037 color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
||
1038 color_type
== PNG_COLOR_TYPE_RGB_ALPHA
) && bit_depth
< 8))
1040 png_warning(png_ptr
, "Invalid color type/bit depth combination in IHDR");
1044 if (interlace_type
>= PNG_INTERLACE_LAST
)
1046 png_warning(png_ptr
, "Unknown interlace method in IHDR");
1050 if (compression_type
!= PNG_COMPRESSION_TYPE_BASE
)
1052 png_warning(png_ptr
, "Unknown compression method in IHDR");
1056 #ifdef PNG_MNG_FEATURES_SUPPORTED
1057 /* Accept filter_method 64 (intrapixel differencing) only if
1058 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
1059 * 2. Libpng did not read a PNG signature (this filter_method is only
1060 * used in PNG datastreams that are embedded in MNG datastreams) and
1061 * 3. The application called png_permit_mng_features with a mask that
1062 * included PNG_FLAG_MNG_FILTER_64 and
1063 * 4. The filter_method is 64 and
1064 * 5. The color_type is RGB or RGBA
1066 if ((png_ptr
->mode
& PNG_HAVE_PNG_SIGNATURE
) &&
1067 png_ptr
->mng_features_permitted
)
1068 png_warning(png_ptr
, "MNG features are not allowed in a PNG datastream");
1070 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
1072 if (!((png_ptr
->mng_features_permitted
& PNG_FLAG_MNG_FILTER_64
) &&
1073 (filter_type
== PNG_INTRAPIXEL_DIFFERENCING
) &&
1074 ((png_ptr
->mode
& PNG_HAVE_PNG_SIGNATURE
) == 0) &&
1075 (color_type
== PNG_COLOR_TYPE_RGB
||
1076 color_type
== PNG_COLOR_TYPE_RGB_ALPHA
)))
1078 png_warning(png_ptr
, "Unknown filter method in IHDR");
1082 if (png_ptr
->mode
& PNG_HAVE_PNG_SIGNATURE
)
1084 png_warning(png_ptr
, "Invalid filter method in IHDR");
1090 if (filter_type
!= PNG_FILTER_TYPE_BASE
)
1092 png_warning(png_ptr
, "Unknown filter method in IHDR");
1098 png_error(png_ptr
, "Invalid IHDR data");
1100 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */