Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 1 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 2 | /* png.c - location for general purpose libpng functions |
| 3 | * |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame^] | 4 | * Last changed in libpng 1.5.0 [October 13, 2010] |
Glenn Randers-Pehrson | e69b55d | 2010-01-01 10:29:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2010 Glenn Randers-Pehrson |
Glenn Randers-Pehrson | d436672 | 2000-06-04 14:29:29 -0500 | [diff] [blame] | 6 | * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) |
| 7 | * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
Glenn Randers-Pehrson | 3e61d79 | 2009-06-24 09:31:28 -0500 | [diff] [blame] | 8 | * |
Glenn Randers-Pehrson | bfbf865 | 2009-06-26 21:46:52 -0500 | [diff] [blame] | 9 | * This code is released under the libpng license. |
Glenn Randers-Pehrson | c332bbc | 2009-06-25 13:43:50 -0500 | [diff] [blame] | 10 | * For conditions of distribution and use, see the disclaimer |
Glenn Randers-Pehrson | 037023b | 2009-06-24 10:27:36 -0500 | [diff] [blame] | 11 | * and license in png.h |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 12 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 13 | |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 14 | #include "pngpriv.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 15 | |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 16 | /* Generate a compiler error if there is an old png.h in the search path. */ |
Glenn Randers-Pehrson | 94b582e | 2010-10-07 21:04:56 -0500 | [diff] [blame] | 17 | typedef version_1_5_0beta50 Your_png_h_is_not_version_1_5_0beta50; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 18 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 19 | /* Version information for C files. This had better match the version |
Glenn Randers-Pehrson | 5ade7ed | 2009-09-30 15:11:49 -0500 | [diff] [blame] | 20 | * string defined in png.h. |
| 21 | */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 22 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 23 | /* Tells libpng that we have already handled the first "num_bytes" bytes |
| 24 | * of the PNG file signature. If the PNG data is embedded into another |
| 25 | * stream we can set num_bytes = 8 so that libpng will not attempt to read |
| 26 | * or write any of the magic bytes before it starts on the IHDR. |
| 27 | */ |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 28 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 29 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 30 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 31 | png_set_sig_bytes(png_structp png_ptr, int num_bytes) |
| 32 | { |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 33 | png_debug(1, "in png_set_sig_bytes"); |
| 34 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 35 | if (png_ptr == NULL) |
| 36 | return; |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 37 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 38 | if (num_bytes > 8) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 39 | png_error(png_ptr, "Too many bytes for PNG signature"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 40 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 41 | png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | /* Checks whether the supplied bytes match the PNG signature. We allow |
| 45 | * checking less than the full 8-byte signature so that those apps that |
| 46 | * already read the first few bytes of a file to determine the file type |
| 47 | * can simply check the remaining bytes for extra assurance. Returns |
| 48 | * an integer less than, equal to, or greater than zero if sig is found, |
| 49 | * respectively, to be less than, to match, or be greater than the correct |
| 50 | * PNG signature (this is the same behaviour as strcmp, memcmp, etc). |
| 51 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 52 | int PNGAPI |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 53 | png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 54 | { |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 55 | png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 56 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 57 | if (num_to_check > 8) |
| 58 | num_to_check = 8; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 59 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 60 | else if (num_to_check < 1) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 61 | return (-1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 62 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 63 | if (start > 7) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 64 | return (-1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 65 | |
| 66 | if (start + num_to_check > 8) |
| 67 | num_to_check = 8 - start; |
| 68 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 69 | return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 72 | #endif /* PNG_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 73 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 74 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 75 | /* Function to allocate memory for zlib and clear it to 0. */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 76 | PNG_FUNCTION(voidpf /* PRIVATE */, |
| 77 | png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 78 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 79 | png_voidp ptr; |
| 80 | png_structp p=(png_structp)png_ptr; |
| 81 | png_uint_32 save_flags=p->flags; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 82 | png_alloc_size_t num_bytes; |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 83 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 84 | if (png_ptr == NULL) |
| 85 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 86 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 87 | if (items > PNG_UINT_32_MAX/size) |
| 88 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 89 | png_warning (p, "Potential overflow in png_zalloc()"); |
| 90 | return (NULL); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 91 | } |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 92 | num_bytes = (png_alloc_size_t)items * size; |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 93 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 94 | p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 95 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); |
| 96 | p->flags=save_flags; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 97 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 98 | return ((voidpf)ptr); |
| 99 | } |
| 100 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 101 | /* Function to free memory for zlib */ |
Glenn Randers-Pehrson | 3f705ba | 2009-07-23 12:53:06 -0500 | [diff] [blame] | 102 | void /* PRIVATE */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 103 | png_zfree(voidpf png_ptr, voidpf ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 104 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 105 | png_free((png_structp)png_ptr, (png_voidp)ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 108 | /* Reset the CRC variable to 32 bits of 1's. Care must be taken |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 109 | * in case CRC is > 32 bits to leave the top bits 0. |
| 110 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 111 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 112 | png_reset_crc(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 113 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 114 | png_ptr->crc = crc32(0, Z_NULL, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 117 | /* Calculate the CRC over a section of data. We can only pass as |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 118 | * much data to this routine as the largest single buffer size. We |
| 119 | * also check that this data will actually be used before going to the |
| 120 | * trouble of calculating it. |
| 121 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 122 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 123 | png_calculate_crc(png_structp png_ptr, png_const_bytep ptr, png_size_t length) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 124 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 125 | int need_crc = 1; |
| 126 | |
| 127 | if (png_ptr->chunk_name[0] & 0x20) /* ancillary */ |
| 128 | { |
| 129 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 130 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 131 | need_crc = 0; |
| 132 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 133 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 134 | else /* critical */ |
| 135 | { |
| 136 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 137 | need_crc = 0; |
| 138 | } |
| 139 | |
| 140 | if (need_crc) |
Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 141 | png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 142 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 143 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 144 | /* Allocate the memory for an info_struct for the application. We don't |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 145 | * really need the png_ptr, but it could potentially be useful in the |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 146 | * future. This should be used in favour of malloc(png_sizeof(png_info)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 147 | * and png_info_init() so that applications that want to use a shared |
| 148 | * libpng don't have to be recompiled if png_info changes size. |
| 149 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 150 | PNG_FUNCTION(png_infop,PNGAPI |
| 151 | png_create_info_struct,(png_structp png_ptr),PNG_ALLOCATED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 152 | { |
| 153 | png_infop info_ptr; |
| 154 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 155 | png_debug(1, "in png_create_info_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 156 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 157 | if (png_ptr == NULL) |
| 158 | return (NULL); |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 159 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 160 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 161 | info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, |
| 162 | png_ptr->malloc_fn, png_ptr->mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 163 | #else |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 164 | info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 165 | #endif |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 166 | if (info_ptr != NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 167 | png_info_init_3(&info_ptr, png_sizeof(png_info)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 168 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 169 | return (info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 172 | /* This function frees the memory associated with a single info struct. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 173 | * Normally, one would use either png_destroy_read_struct() or |
| 174 | * png_destroy_write_struct() to free an info struct, but this may be |
| 175 | * useful for some applications. |
| 176 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 177 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 178 | png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) |
| 179 | { |
| 180 | png_infop info_ptr = NULL; |
| 181 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 182 | png_debug(1, "in png_destroy_info_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 183 | |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 184 | if (png_ptr == NULL) |
| 185 | return; |
| 186 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 187 | if (info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 188 | info_ptr = *info_ptr_ptr; |
| 189 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 190 | if (info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 191 | { |
| 192 | png_info_destroy(png_ptr, info_ptr); |
| 193 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 194 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 195 | png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn, |
| 196 | png_ptr->mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 197 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 198 | png_destroy_struct((png_voidp)info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 199 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 200 | *info_ptr_ptr = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | /* Initialize the info structure. This is now an internal function (0.89) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 205 | * and applications using it are urged to use png_create_info_struct() |
| 206 | * instead. |
| 207 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 208 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 209 | void PNGAPI |
| 210 | png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) |
| 211 | { |
| 212 | png_infop info_ptr = *ptr_ptr; |
| 213 | |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 214 | png_debug(1, "in png_info_init_3"); |
| 215 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 216 | if (info_ptr == NULL) |
| 217 | return; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 218 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 219 | if (png_sizeof(png_info) > png_info_struct_size) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 220 | { |
| 221 | png_destroy_struct(info_ptr); |
| 222 | info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); |
| 223 | *ptr_ptr = info_ptr; |
| 224 | } |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 225 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 226 | /* Set everything to 0 */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 227 | png_memset(info_ptr, 0, png_sizeof(png_info)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 228 | } |
| 229 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 230 | void PNGAPI |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 231 | png_data_freer(png_structp png_ptr, png_infop info_ptr, |
| 232 | int freer, png_uint_32 mask) |
| 233 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 234 | png_debug(1, "in png_data_freer"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 235 | |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 236 | if (png_ptr == NULL || info_ptr == NULL) |
| 237 | return; |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 238 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 239 | if (freer == PNG_DESTROY_WILL_FREE_DATA) |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 240 | info_ptr->free_me |= mask; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 241 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 242 | else if (freer == PNG_USER_WILL_FREE_DATA) |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 243 | info_ptr->free_me &= ~mask; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 244 | |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 245 | else |
| 246 | png_warning(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 247 | "Unknown freer parameter in png_data_freer"); |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 250 | void PNGAPI |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 251 | png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, |
| 252 | int num) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 253 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 254 | png_debug(1, "in png_free_data"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 255 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 256 | if (png_ptr == NULL || info_ptr == NULL) |
| 257 | return; |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 258 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 259 | #ifdef PNG_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 260 | /* Free text item num or (if num == -1) all text items */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 261 | if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 262 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 263 | if (num != -1) |
| 264 | { |
| 265 | if (info_ptr->text && info_ptr->text[num].key) |
| 266 | { |
| 267 | png_free(png_ptr, info_ptr->text[num].key); |
| 268 | info_ptr->text[num].key = NULL; |
| 269 | } |
| 270 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 271 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 272 | else |
| 273 | { |
| 274 | int i; |
| 275 | for (i = 0; i < info_ptr->num_text; i++) |
| 276 | png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); |
| 277 | png_free(png_ptr, info_ptr->text); |
| 278 | info_ptr->text = NULL; |
| 279 | info_ptr->num_text=0; |
| 280 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 281 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 282 | #endif |
| 283 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 284 | #ifdef PNG_tRNS_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 285 | /* Free any tRNS entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 286 | if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 287 | { |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 288 | png_free(png_ptr, info_ptr->trans_alpha); |
| 289 | info_ptr->trans_alpha = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 290 | info_ptr->valid &= ~PNG_INFO_tRNS; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 291 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 292 | #endif |
| 293 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 294 | #ifdef PNG_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 295 | /* Free any sCAL entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 296 | if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 297 | { |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 298 | #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 299 | png_free(png_ptr, info_ptr->scal_s_width); |
| 300 | png_free(png_ptr, info_ptr->scal_s_height); |
| 301 | info_ptr->scal_s_width = NULL; |
| 302 | info_ptr->scal_s_height = NULL; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 303 | #endif |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 304 | info_ptr->valid &= ~PNG_INFO_sCAL; |
| 305 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 306 | #endif |
| 307 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 308 | #ifdef PNG_pCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 309 | /* Free any pCAL entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 310 | if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 311 | { |
| 312 | png_free(png_ptr, info_ptr->pcal_purpose); |
| 313 | png_free(png_ptr, info_ptr->pcal_units); |
| 314 | info_ptr->pcal_purpose = NULL; |
| 315 | info_ptr->pcal_units = NULL; |
| 316 | if (info_ptr->pcal_params != NULL) |
| 317 | { |
| 318 | int i; |
| 319 | for (i = 0; i < (int)info_ptr->pcal_nparams; i++) |
| 320 | { |
| 321 | png_free(png_ptr, info_ptr->pcal_params[i]); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 322 | info_ptr->pcal_params[i] = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 323 | } |
| 324 | png_free(png_ptr, info_ptr->pcal_params); |
| 325 | info_ptr->pcal_params = NULL; |
| 326 | } |
| 327 | info_ptr->valid &= ~PNG_INFO_pCAL; |
| 328 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 329 | #endif |
| 330 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 331 | #ifdef PNG_iCCP_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 332 | /* Free any iCCP entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 333 | if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 334 | { |
| 335 | png_free(png_ptr, info_ptr->iccp_name); |
| 336 | png_free(png_ptr, info_ptr->iccp_profile); |
| 337 | info_ptr->iccp_name = NULL; |
| 338 | info_ptr->iccp_profile = NULL; |
| 339 | info_ptr->valid &= ~PNG_INFO_iCCP; |
| 340 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 341 | #endif |
| 342 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 343 | #ifdef PNG_sPLT_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 344 | /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 345 | if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 346 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 347 | if (num != -1) |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 348 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 349 | if (info_ptr->splt_palettes) |
| 350 | { |
| 351 | png_free(png_ptr, info_ptr->splt_palettes[num].name); |
| 352 | png_free(png_ptr, info_ptr->splt_palettes[num].entries); |
| 353 | info_ptr->splt_palettes[num].name = NULL; |
| 354 | info_ptr->splt_palettes[num].entries = NULL; |
| 355 | } |
| 356 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 357 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 358 | else |
| 359 | { |
| 360 | if (info_ptr->splt_palettes_num) |
| 361 | { |
| 362 | int i; |
| 363 | for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) |
| 364 | png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i); |
| 365 | |
| 366 | png_free(png_ptr, info_ptr->splt_palettes); |
| 367 | info_ptr->splt_palettes = NULL; |
| 368 | info_ptr->splt_palettes_num = 0; |
| 369 | } |
| 370 | info_ptr->valid &= ~PNG_INFO_sPLT; |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 371 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 372 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 373 | #endif |
| 374 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 375 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 376 | if (png_ptr->unknown_chunk.data) |
| 377 | { |
| 378 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 379 | png_ptr->unknown_chunk.data = NULL; |
| 380 | } |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 381 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 382 | if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 383 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 384 | if (num != -1) |
| 385 | { |
| 386 | if (info_ptr->unknown_chunks) |
| 387 | { |
| 388 | png_free(png_ptr, info_ptr->unknown_chunks[num].data); |
| 389 | info_ptr->unknown_chunks[num].data = NULL; |
| 390 | } |
| 391 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 392 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 393 | else |
| 394 | { |
| 395 | int i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 396 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 397 | if (info_ptr->unknown_chunks_num) |
| 398 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 399 | for (i = 0; i < info_ptr->unknown_chunks_num; i++) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 400 | png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 401 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 402 | png_free(png_ptr, info_ptr->unknown_chunks); |
| 403 | info_ptr->unknown_chunks = NULL; |
| 404 | info_ptr->unknown_chunks_num = 0; |
| 405 | } |
| 406 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 407 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 408 | #endif |
| 409 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 410 | #ifdef PNG_hIST_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 411 | /* Free any hIST entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 412 | if ((mask & PNG_FREE_HIST) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 413 | { |
| 414 | png_free(png_ptr, info_ptr->hist); |
| 415 | info_ptr->hist = NULL; |
| 416 | info_ptr->valid &= ~PNG_INFO_hIST; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 417 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 418 | #endif |
| 419 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 420 | /* Free any PLTE entry that was internally allocated */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 421 | if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 422 | { |
| 423 | png_zfree(png_ptr, info_ptr->palette); |
| 424 | info_ptr->palette = NULL; |
| 425 | info_ptr->valid &= ~PNG_INFO_PLTE; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 426 | info_ptr->num_palette = 0; |
| 427 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 428 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 429 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 430 | /* Free any image bits attached to the info structure */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 431 | if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 432 | { |
| 433 | if (info_ptr->row_pointers) |
| 434 | { |
| 435 | int row; |
| 436 | for (row = 0; row < (int)info_ptr->height; row++) |
| 437 | { |
| 438 | png_free(png_ptr, info_ptr->row_pointers[row]); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 439 | info_ptr->row_pointers[row] = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 440 | } |
| 441 | png_free(png_ptr, info_ptr->row_pointers); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 442 | info_ptr->row_pointers = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 443 | } |
| 444 | info_ptr->valid &= ~PNG_INFO_IDAT; |
| 445 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 446 | #endif |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 447 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 448 | if (num == -1) |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 449 | info_ptr->free_me &= ~mask; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 450 | |
Glenn Randers-Pehrson | ec61c23 | 2000-05-16 06:17:36 -0500 | [diff] [blame] | 451 | else |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 452 | info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 453 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 454 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 455 | /* This is an internal routine to free any memory that the info struct is |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 456 | * pointing to before re-using it or freeing the struct itself. Recall |
| 457 | * that png_free() checks for NULL pointers for us. |
| 458 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 459 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 460 | png_info_destroy(png_structp png_ptr, png_infop info_ptr) |
| 461 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 462 | png_debug(1, "in png_info_destroy"); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 463 | |
| 464 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
Glenn Randers-Pehrson | d56aca7 | 2000-11-23 11:51:42 -0600 | [diff] [blame] | 465 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 466 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 467 | if (png_ptr->num_chunk_list) |
| 468 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 469 | png_free(png_ptr, png_ptr->chunk_list); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 470 | png_ptr->chunk_list = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 471 | png_ptr->num_chunk_list = 0; |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 472 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 473 | #endif |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 474 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 475 | png_info_init_3(&info_ptr, png_sizeof(png_info)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 476 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 477 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 478 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 479 | /* This function returns a pointer to the io_ptr associated with the user |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 480 | * functions. The application should free any memory associated with this |
| 481 | * pointer before png_write_destroy() or png_read_destroy() are called. |
| 482 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 483 | png_voidp PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 484 | png_get_io_ptr(png_structp png_ptr) |
| 485 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 486 | if (png_ptr == NULL) |
| 487 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 488 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 489 | return (png_ptr->io_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 490 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 491 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 492 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 493 | # ifdef PNG_STDIO_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 494 | /* Initialize the default input/output functions for the PNG file. If you |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 495 | * use your own read or write routines, you can call either png_set_read_fn() |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 496 | * or png_set_write_fn() instead of png_init_io(). If you have defined |
| 497 | * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't |
| 498 | * necessarily available. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 499 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 500 | void PNGAPI |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 501 | png_init_io(png_structp png_ptr, png_FILE_p fp) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 502 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 503 | png_debug(1, "in png_init_io"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 504 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 505 | if (png_ptr == NULL) |
| 506 | return; |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 507 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 508 | png_ptr->io_ptr = (png_voidp)fp; |
| 509 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 510 | # endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 511 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 512 | # ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 513 | /* Convert the supplied time into an RFC 1123 string suitable for use in |
| 514 | * a "Creation Time" or other text-based time string. |
| 515 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 516 | png_const_charp PNGAPI |
| 517 | png_convert_to_rfc1123(png_structp png_ptr, png_const_timep ptime) |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 518 | { |
| 519 | static PNG_CONST char short_months[12][4] = |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 520 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 521 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 522 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 523 | if (png_ptr == NULL) |
| 524 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 525 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 526 | if (png_ptr->time_buffer == NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 527 | { |
| 528 | png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29* |
| 529 | png_sizeof(char))); |
| 530 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 531 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 532 | # ifdef USE_FAR_KEYWORD |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 533 | { |
| 534 | char near_time_buf[29]; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 535 | png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000", |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 536 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
| 537 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
| 538 | ptime->second % 61); |
| 539 | png_memcpy(png_ptr->time_buffer, near_time_buf, |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 540 | 29*png_sizeof(char)); |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 541 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 542 | # else |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 543 | png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000", |
Glenn Randers-Pehrson | e1eff58 | 2001-04-14 20:15:41 -0500 | [diff] [blame] | 544 | ptime->day % 32, short_months[(ptime->month - 1) % 12], |
| 545 | ptime->year, ptime->hour % 24, ptime->minute % 60, |
| 546 | ptime->second % 61); |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 547 | # endif |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 548 | return png_ptr->time_buffer; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 549 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 550 | # endif /* PNG_TIME_RFC1123_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 551 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 552 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 553 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 554 | png_const_charp PNGAPI |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 555 | png_get_copyright(png_structp png_ptr) |
| 556 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 557 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 558 | #ifdef PNG_STRING_COPYRIGHT |
| 559 | return PNG_STRING_COPYRIGHT |
| 560 | #else |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 561 | # ifdef __STDC__ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 562 | return PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame^] | 563 | "libpng version 1.5.0beta50 - October 13, 2010" PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 564 | "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 565 | "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ |
| 566 | "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 567 | PNG_STRING_NEWLINE; |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 568 | # else |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame^] | 569 | return "libpng version 1.5.0beta50 - October 13, 2010\ |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 570 | Copyright (c) 1998-2010 Glenn Randers-Pehrson\ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 571 | Copyright (c) 1996-1997 Andreas Dilger\ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 572 | Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."; |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 573 | # endif |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 574 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 575 | } |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 576 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 577 | /* The following return the library version as a short string in the |
Glenn Randers-Pehrson | 5b5dcf8 | 2004-07-17 22:45:44 -0500 | [diff] [blame] | 578 | * format 1.0.0 through 99.99.99zz. To get the version of *.h files |
| 579 | * used with your application, print out PNG_LIBPNG_VER_STRING, which |
| 580 | * is defined in png.h. |
| 581 | * Note: now there is no difference between png_get_libpng_ver() and |
| 582 | * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, |
| 583 | * it is guaranteed that png.c uses the correct version of png.h. |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 584 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 585 | png_const_charp PNGAPI |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 586 | png_get_libpng_ver(png_structp png_ptr) |
| 587 | { |
| 588 | /* Version of *.c files used when building libpng */ |
Glenn Randers-Pehrson | 4c8f726 | 2010-03-16 19:30:01 -0500 | [diff] [blame] | 589 | return png_get_header_ver(png_ptr); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 590 | } |
| 591 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 592 | png_const_charp PNGAPI |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 593 | png_get_header_ver(png_structp png_ptr) |
| 594 | { |
| 595 | /* Version of *.h files used when building libpng */ |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 596 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 597 | return PNG_LIBPNG_VER_STRING; |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 598 | } |
| 599 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 600 | png_const_charp PNGAPI |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 601 | png_get_header_version(png_structp png_ptr) |
| 602 | { |
| 603 | /* Returns longer string containing both version and date */ |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 604 | png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 605 | #ifdef __STDC__ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 606 | return PNG_HEADER_VERSION_STRING |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 607 | # ifndef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 608 | " (NO READ SUPPORT)" |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 609 | # endif |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 610 | PNG_STRING_NEWLINE; |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 611 | #else |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 612 | return PNG_HEADER_VERSION_STRING; |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 613 | #endif |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 614 | } |
| 615 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 616 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 617 | # ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | c1bfe68 | 2002-03-06 22:08:00 -0600 | [diff] [blame] | 618 | int PNGAPI |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 619 | png_handle_as_unknown(png_structp png_ptr, png_const_bytep chunk_name) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 620 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 621 | /* Check chunk_name and return "keep" value if it's on the list, else 0 */ |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 622 | int i; |
| 623 | png_bytep p; |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 624 | if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 625 | return 0; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 626 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 627 | p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5; |
| 628 | for (i = png_ptr->num_chunk_list; i; i--, p -= 5) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 629 | if (!png_memcmp(chunk_name, p, 4)) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 630 | return ((int)*(p + 4)); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 631 | return 0; |
| 632 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 633 | # endif |
Glenn Randers-Pehrson | f10fa3c | 2010-04-29 08:25:29 -0500 | [diff] [blame] | 634 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 635 | |
Glenn Randers-Pehrson | f10fa3c | 2010-04-29 08:25:29 -0500 | [diff] [blame] | 636 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 637 | /* This function, added to libpng-1.0.6g, is untested. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 638 | int PNGAPI |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 639 | png_reset_zstream(png_structp png_ptr) |
| 640 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 641 | if (png_ptr == NULL) |
| 642 | return Z_STREAM_ERROR; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 643 | |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 644 | return (inflateReset(&png_ptr->zstream)); |
| 645 | } |
Glenn Randers-Pehrson | 2b8bef1 | 2010-04-29 11:50:24 -0500 | [diff] [blame] | 646 | #endif /* PNG_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 647 | |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 648 | /* This function was added to libpng-1.0.7 */ |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 649 | png_uint_32 PNGAPI |
| 650 | png_access_version_number(void) |
| 651 | { |
| 652 | /* Version of *.c files used when building libpng */ |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 653 | return((png_uint_32)PNG_LIBPNG_VER); |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 654 | } |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 655 | |
Glenn Randers-Pehrson | 1fd5fb3 | 2001-05-06 05:34:26 -0500 | [diff] [blame] | 656 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 657 | |
| 658 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 659 | # ifdef PNG_SIZE_T |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 660 | /* Added at libpng version 1.2.6 */ |
| 661 | PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size)); |
| 662 | png_size_t PNGAPI |
| 663 | png_convert_size(size_t size) |
Glenn Randers-Pehrson | 1fd5fb3 | 2001-05-06 05:34:26 -0500 | [diff] [blame] | 664 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 665 | if (size > (png_size_t)-1) |
| 666 | PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */ |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 667 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 668 | return ((png_size_t)size); |
Glenn Randers-Pehrson | 1fd5fb3 | 2001-05-06 05:34:26 -0500 | [diff] [blame] | 669 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 670 | # endif /* PNG_SIZE_T */ |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 671 | |
Glenn Randers-Pehrson | 02a5e33 | 2008-11-24 22:10:23 -0600 | [diff] [blame] | 672 | /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 673 | # ifdef PNG_CHECK_cHRM_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 674 | |
Glenn Randers-Pehrson | 3f705ba | 2009-07-23 12:53:06 -0500 | [diff] [blame] | 675 | int /* PRIVATE */ |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 676 | png_check_cHRM_fixed(png_structp png_ptr, |
| 677 | png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, |
| 678 | png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, |
| 679 | png_fixed_point blue_x, png_fixed_point blue_y) |
| 680 | { |
| 681 | int ret = 1; |
Glenn Randers-Pehrson | d0c4059 | 2008-11-22 07:09:51 -0600 | [diff] [blame] | 682 | unsigned long xy_hi,xy_lo,yx_hi,yx_lo; |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 683 | |
| 684 | png_debug(1, "in function png_check_cHRM_fixed"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 685 | |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 686 | if (png_ptr == NULL) |
| 687 | return 0; |
| 688 | |
Glenn Randers-Pehrson | 02a5e33 | 2008-11-24 22:10:23 -0600 | [diff] [blame] | 689 | if (white_x < 0 || white_y <= 0 || |
| 690 | red_x < 0 || red_y < 0 || |
| 691 | green_x < 0 || green_y < 0 || |
| 692 | blue_x < 0 || blue_y < 0) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 693 | { |
| 694 | png_warning(png_ptr, |
| 695 | "Ignoring attempt to set negative chromaticity value"); |
| 696 | ret = 0; |
| 697 | } |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 698 | if (white_x > (png_fixed_point)PNG_UINT_31_MAX || |
| 699 | white_y > (png_fixed_point)PNG_UINT_31_MAX || |
| 700 | red_x > (png_fixed_point)PNG_UINT_31_MAX || |
| 701 | red_y > (png_fixed_point)PNG_UINT_31_MAX || |
| 702 | green_x > (png_fixed_point)PNG_UINT_31_MAX || |
| 703 | green_y > (png_fixed_point)PNG_UINT_31_MAX || |
| 704 | blue_x > (png_fixed_point)PNG_UINT_31_MAX || |
| 705 | blue_y > (png_fixed_point)PNG_UINT_31_MAX ) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 706 | { |
| 707 | png_warning(png_ptr, |
| 708 | "Ignoring attempt to set chromaticity value exceeding 21474.83"); |
| 709 | ret = 0; |
| 710 | } |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 711 | if (white_x > 100000L - white_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 712 | { |
| 713 | png_warning(png_ptr, "Invalid cHRM white point"); |
| 714 | ret = 0; |
| 715 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 716 | |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 717 | if (red_x > 100000L - red_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 718 | { |
| 719 | png_warning(png_ptr, "Invalid cHRM red point"); |
| 720 | ret = 0; |
| 721 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 722 | |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 723 | if (green_x > 100000L - green_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 724 | { |
| 725 | png_warning(png_ptr, "Invalid cHRM green point"); |
| 726 | ret = 0; |
| 727 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 728 | |
Glenn Randers-Pehrson | 71a3c1f | 2008-11-26 12:00:38 -0600 | [diff] [blame] | 729 | if (blue_x > 100000L - blue_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 730 | { |
| 731 | png_warning(png_ptr, "Invalid cHRM blue point"); |
| 732 | ret = 0; |
| 733 | } |
Glenn Randers-Pehrson | d0c4059 | 2008-11-22 07:09:51 -0600 | [diff] [blame] | 734 | |
| 735 | png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo); |
| 736 | png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo); |
| 737 | |
| 738 | if (xy_hi == yx_hi && xy_lo == yx_lo) |
| 739 | { |
| 740 | png_warning(png_ptr, |
| 741 | "Ignoring attempt to set cHRM RGB triangle with zero area"); |
| 742 | ret = 0; |
| 743 | } |
| 744 | |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 745 | return ret; |
| 746 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 747 | # endif /* PNG_CHECK_cHRM_SUPPORTED */ |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 748 | |
| 749 | void /* PRIVATE */ |
| 750 | png_check_IHDR(png_structp png_ptr, |
| 751 | png_uint_32 width, png_uint_32 height, int bit_depth, |
| 752 | int color_type, int interlace_type, int compression_type, |
| 753 | int filter_type) |
| 754 | { |
| 755 | int error = 0; |
| 756 | |
| 757 | /* Check for width and height valid values */ |
| 758 | if (width == 0) |
| 759 | { |
| 760 | png_warning(png_ptr, "Image width is zero in IHDR"); |
| 761 | error = 1; |
| 762 | } |
| 763 | |
| 764 | if (height == 0) |
| 765 | { |
| 766 | png_warning(png_ptr, "Image height is zero in IHDR"); |
| 767 | error = 1; |
| 768 | } |
| 769 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 770 | # ifdef PNG_SET_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 771 | if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX) |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 772 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 773 | # else |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 774 | if (width > PNG_USER_WIDTH_MAX) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 775 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 776 | { |
| 777 | png_warning(png_ptr, "Image width exceeds user limit in IHDR"); |
| 778 | error = 1; |
| 779 | } |
| 780 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 781 | # ifdef PNG_SET_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 782 | if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 783 | # else |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 784 | if (height > PNG_USER_HEIGHT_MAX) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 785 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 786 | { |
| 787 | png_warning(png_ptr, "Image height exceeds user limit in IHDR"); |
| 788 | error = 1; |
| 789 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 790 | |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 791 | if (width > PNG_UINT_31_MAX) |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 792 | { |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 793 | png_warning(png_ptr, "Invalid image width in IHDR"); |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 794 | error = 1; |
| 795 | } |
| 796 | |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame^] | 797 | if (height > PNG_UINT_31_MAX) |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 798 | { |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 799 | png_warning(png_ptr, "Invalid image height in IHDR"); |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 800 | error = 1; |
| 801 | } |
| 802 | |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame^] | 803 | if (width > (PNG_UINT_32_MAX |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 804 | >> 3) /* 8-byte RGBA pixels */ |
| 805 | - 64 /* bigrowbuf hack */ |
| 806 | - 1 /* filter byte */ |
| 807 | - 7*8 /* rounding of width to multiple of 8 pixels */ |
| 808 | - 8) /* extra max_pixel_depth pad */ |
| 809 | png_warning(png_ptr, "Width is too large for libpng to process pixels"); |
| 810 | |
| 811 | /* Check other values */ |
| 812 | if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && |
| 813 | bit_depth != 8 && bit_depth != 16) |
| 814 | { |
| 815 | png_warning(png_ptr, "Invalid bit depth in IHDR"); |
| 816 | error = 1; |
| 817 | } |
| 818 | |
| 819 | if (color_type < 0 || color_type == 1 || |
| 820 | color_type == 5 || color_type > 6) |
| 821 | { |
| 822 | png_warning(png_ptr, "Invalid color type in IHDR"); |
| 823 | error = 1; |
| 824 | } |
| 825 | |
| 826 | if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || |
| 827 | ((color_type == PNG_COLOR_TYPE_RGB || |
| 828 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 829 | color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) |
| 830 | { |
| 831 | png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR"); |
| 832 | error = 1; |
| 833 | } |
| 834 | |
| 835 | if (interlace_type >= PNG_INTERLACE_LAST) |
| 836 | { |
| 837 | png_warning(png_ptr, "Unknown interlace method in IHDR"); |
| 838 | error = 1; |
| 839 | } |
| 840 | |
| 841 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
| 842 | { |
| 843 | png_warning(png_ptr, "Unknown compression method in IHDR"); |
| 844 | error = 1; |
| 845 | } |
| 846 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 847 | # ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 848 | /* Accept filter_method 64 (intrapixel differencing) only if |
| 849 | * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
| 850 | * 2. Libpng did not read a PNG signature (this filter_method is only |
| 851 | * used in PNG datastreams that are embedded in MNG datastreams) and |
| 852 | * 3. The application called png_permit_mng_features with a mask that |
| 853 | * included PNG_FLAG_MNG_FILTER_64 and |
| 854 | * 4. The filter_method is 64 and |
| 855 | * 5. The color_type is RGB or RGBA |
| 856 | */ |
| 857 | if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) && |
| 858 | png_ptr->mng_features_permitted) |
| 859 | png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); |
| 860 | |
| 861 | if (filter_type != PNG_FILTER_TYPE_BASE) |
| 862 | { |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 863 | if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 864 | (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && |
| 865 | ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && |
| 866 | (color_type == PNG_COLOR_TYPE_RGB || |
| 867 | color_type == PNG_COLOR_TYPE_RGB_ALPHA))) |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 868 | { |
| 869 | png_warning(png_ptr, "Unknown filter method in IHDR"); |
| 870 | error = 1; |
| 871 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 872 | |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 873 | if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) |
| 874 | { |
| 875 | png_warning(png_ptr, "Invalid filter method in IHDR"); |
| 876 | error = 1; |
| 877 | } |
| 878 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 879 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 880 | # else |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 881 | if (filter_type != PNG_FILTER_TYPE_BASE) |
| 882 | { |
| 883 | png_warning(png_ptr, "Unknown filter method in IHDR"); |
| 884 | error = 1; |
| 885 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 886 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 887 | |
| 888 | if (error == 1) |
| 889 | png_error(png_ptr, "Invalid IHDR data"); |
| 890 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 891 | |
| 892 | #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) |
| 893 | /* ASCII to fp functions */ |
| 894 | /* Check an ASCII formated floating point value, see the more detailed |
| 895 | * comments in pngpriv.h |
| 896 | */ |
| 897 | /* The following is used internally to preserve the 'valid' flag */ |
| 898 | #define png_fp_add(state, flags) ((state) |= (flags)) |
| 899 | #define png_fp_set(state, value)\ |
| 900 | ((state) = (value) | ((state) & PNG_FP_WAS_VALID)) |
| 901 | |
| 902 | /* Internal type codes: bits above the base state! */ |
| 903 | #define PNG_FP_SIGN 0 /* [+-] */ |
| 904 | #define PNG_FP_DOT 4 /* . */ |
| 905 | #define PNG_FP_DIGIT 8 /* [0123456789] */ |
| 906 | #define PNG_FP_E 12 /* [Ee] */ |
| 907 | |
| 908 | int /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 909 | png_check_fp_number(png_const_charp string, png_size_t size, int *statep, |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 910 | png_size_tp whereami) |
| 911 | { |
| 912 | int state = *statep; |
| 913 | png_size_t i = *whereami; |
| 914 | |
| 915 | while (i < size) |
| 916 | { |
| 917 | int type; |
| 918 | /* First find the type of the next character */ |
| 919 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 920 | char ch = string[i]; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 921 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 922 | if (ch >= 48 && ch <= 57) |
| 923 | type = PNG_FP_DIGIT; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 924 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 925 | else switch (ch) |
| 926 | { |
| 927 | case 43: case 45: type = PNG_FP_SIGN; break; |
| 928 | case 46: type = PNG_FP_DOT; break; |
| 929 | case 69: case 101: type = PNG_FP_E; break; |
| 930 | default: goto PNG_FP_End; |
| 931 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /* Now deal with this type according to the current |
| 935 | * state, the type is arranged to not overlap the |
| 936 | * bits of the PNG_FP_STATE. |
| 937 | */ |
| 938 | switch ((state & PNG_FP_STATE) + type) |
| 939 | { |
| 940 | case PNG_FP_INTEGER + PNG_FP_SIGN: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 941 | if (state & PNG_FP_SAW_ANY) |
| 942 | goto PNG_FP_End; /* not a part of the number */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 943 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 944 | png_fp_add(state, PNG_FP_SAW_SIGN); |
| 945 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 946 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 947 | case PNG_FP_INTEGER + PNG_FP_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 948 | /* Ok as trailer, ok as lead of fraction. */ |
| 949 | if (state & PNG_FP_SAW_DOT) /* two dots */ |
| 950 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 951 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 952 | else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */ |
| 953 | png_fp_add(state, PNG_FP_SAW_DOT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 954 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 955 | else |
| 956 | png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 957 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 958 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 959 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 960 | case PNG_FP_INTEGER + PNG_FP_DIGIT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 961 | if (state & PNG_FP_SAW_DOT) /* delayed fraction */ |
| 962 | png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 963 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 964 | png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 965 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 966 | break; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 967 | case PNG_FP_INTEGER + PNG_FP_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 968 | if ((state & PNG_FP_SAW_DIGIT) == 0) |
| 969 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 970 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 971 | png_fp_set(state, PNG_FP_EXPONENT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 972 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 973 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 974 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 975 | /* case PNG_FP_FRACTION + PNG_FP_SIGN: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 976 | goto PNG_FP_End; ** no sign in exponent */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 977 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 978 | /* case PNG_FP_FRACTION + PNG_FP_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 979 | goto PNG_FP_End; ** Because SAW_DOT is always set */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 980 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 981 | case PNG_FP_FRACTION + PNG_FP_DIGIT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 982 | png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); |
| 983 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 984 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 985 | case PNG_FP_FRACTION + PNG_FP_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 986 | /* This is correct because the trailing '.' on an |
| 987 | * integer is handled above - so we can only get here |
| 988 | * with the sequence ".E" (with no preceding digits). |
| 989 | */ |
| 990 | if ((state & PNG_FP_SAW_DIGIT) == 0) |
| 991 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 992 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 993 | png_fp_set(state, PNG_FP_EXPONENT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 994 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 995 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 996 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 997 | case PNG_FP_EXPONENT + PNG_FP_SIGN: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 998 | if (state & PNG_FP_SAW_ANY) |
| 999 | goto PNG_FP_End; /* not a part of the number */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1000 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1001 | png_fp_add(state, PNG_FP_SAW_SIGN); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1002 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1003 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1004 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1005 | /* case PNG_FP_EXPONENT + PNG_FP_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1006 | goto PNG_FP_End; */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1007 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1008 | case PNG_FP_EXPONENT + PNG_FP_DIGIT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1009 | png_fp_add(state, PNG_FP_SAW_DIGIT + PNG_FP_WAS_VALID); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1010 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1011 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1012 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1013 | /* case PNG_FP_EXPONEXT + PNG_FP_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1014 | goto PNG_FP_End; */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1015 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1016 | default: goto PNG_FP_End; /* I.e. break 2 */ |
| 1017 | } |
| 1018 | |
| 1019 | /* The character seems ok, continue. */ |
| 1020 | ++i; |
| 1021 | } |
| 1022 | |
| 1023 | PNG_FP_End: |
| 1024 | /* Here at the end, update the state and return the correct |
| 1025 | * return code. |
| 1026 | */ |
| 1027 | *statep = state; |
| 1028 | *whereami = i; |
| 1029 | |
| 1030 | return (state & PNG_FP_SAW_DIGIT) != 0; |
| 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | /* The same but for a complete string. */ |
| 1035 | int |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1036 | png_check_fp_string(png_const_charp string, png_size_t size) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1037 | { |
| 1038 | int state=0; |
| 1039 | png_size_t index=0; |
| 1040 | |
| 1041 | return png_check_fp_number(string, size, &state, &index) && |
| 1042 | (index == size || string[index] == 0); |
| 1043 | } |
| 1044 | #endif /* pCAL or sCAL */ |
| 1045 | |
| 1046 | #ifdef PNG_READ_sCAL_SUPPORTED |
| 1047 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1048 | /* Utility used below - a simple accurate power of ten from an integral |
| 1049 | * exponent. |
| 1050 | */ |
| 1051 | static double |
| 1052 | png_pow10(int power) |
| 1053 | { |
| 1054 | int recip = 0; |
| 1055 | double d = 1; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1056 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1057 | /* Handle negative exponent with a reciprocal at the end because |
| 1058 | * 10 is exact whereas .1 is inexact in base 2 |
| 1059 | */ |
| 1060 | if (power < 0) |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1061 | { |
| 1062 | if (power < DBL_MIN_10_EXP) return 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1063 | recip = 1, power = -power; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1064 | } |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1065 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1066 | if (power > 0) |
| 1067 | { |
| 1068 | /* Decompose power bitwise. */ |
| 1069 | double mult = 10; |
| 1070 | do |
| 1071 | { |
| 1072 | if (power & 1) d *= mult; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1073 | mult *= mult; |
| 1074 | power >>= 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1075 | } |
| 1076 | while (power > 0); |
| 1077 | |
| 1078 | if (recip) d = 1/d; |
| 1079 | } |
| 1080 | /* else power is 0 and d is 1 */ |
| 1081 | |
| 1082 | return d; |
| 1083 | } |
| 1084 | |
| 1085 | /* Function to format a floating point value in ASCII with a given |
| 1086 | * precision. |
| 1087 | */ |
| 1088 | void /* PRIVATE */ |
| 1089 | png_ascii_from_fp(png_structp png_ptr, png_charp ascii, png_size_t size, |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1090 | double fp, unsigned int precision) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1091 | { |
| 1092 | /* We use standard functions from math.h, but not printf because |
| 1093 | * that would require stdio. The caller must supply a buffer of |
| 1094 | * sufficient size or we will png_error. The tests on size and |
| 1095 | * the space in ascii[] consumed are indicated below. |
| 1096 | */ |
| 1097 | if (precision < 1) |
| 1098 | precision = DBL_DIG; |
| 1099 | |
| 1100 | /* Enforce the limit of the implementation precision too. */ |
| 1101 | if (precision > DBL_DIG+1) |
| 1102 | precision = DBL_DIG+1; |
| 1103 | |
| 1104 | /* Basic sanity checks */ |
| 1105 | if (size >= precision+5) /* See the requirements below. */ |
| 1106 | { |
| 1107 | if (fp < 0) |
| 1108 | { |
| 1109 | fp = -fp; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1110 | *ascii++ = 45; /* '-' PLUS 1 TOTAL 1*/ |
| 1111 | --size; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | if (fp >= DBL_MIN && fp <= DBL_MAX) |
| 1115 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1116 | int exp; /* A base 10 exponent */ |
| 1117 | double base; /* 10^exp */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1118 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1119 | /* First extract a base 10 exponent of the number, |
| 1120 | * the calculation below rounds down when converting |
| 1121 | * from base 2 to base 10 (multiply by log10(2) - |
| 1122 | * 0.3010, but 77/256 is 0.3008, so exp needs to |
| 1123 | * be increased. Note that the arithmetic shift |
| 1124 | * performs a floor() unlike C arithmetic - using a |
| 1125 | * C multiply would break the following for negative |
| 1126 | * exponents. |
| 1127 | */ |
| 1128 | (void)frexp(fp, &exp); /* exponent to base 2 */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1129 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1130 | exp = (exp * 77) >> 8; /* <= exponent to base 10 */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1131 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1132 | /* Avoid underflow here. */ |
| 1133 | base = png_pow10(exp); /* May underflow */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1134 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1135 | while (base < DBL_MIN || base < fp) |
| 1136 | { |
| 1137 | /* And this may overflow. */ |
| 1138 | double test = png_pow10(exp+1); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1139 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1140 | if (test <= DBL_MAX) |
| 1141 | ++exp, base = test; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1142 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1143 | else |
| 1144 | break; |
| 1145 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1146 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1147 | /* Normalize fp and correct exp, after this fp is in the |
| 1148 | * range [.1,1) and exp is both the exponent and the digit |
| 1149 | * *before* which the decimal point should be inserted |
| 1150 | * (starting with 0 for the first digit). Note that this |
| 1151 | * works even if 10^exp is out of range because of the |
| 1152 | * test on DBL_MAX above. |
| 1153 | */ |
| 1154 | fp /= base; |
| 1155 | while (fp >= 1) fp /= 10, ++exp; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1156 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1157 | /* Because of the code above fp may, at this point, be |
| 1158 | * less than .1, this is ok because the code below can |
| 1159 | * handle the leading zeros this generates, so no attempt |
| 1160 | * is made to correct that here. |
| 1161 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1162 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1163 | { |
| 1164 | int czero, clead, cdigits; |
| 1165 | char exponent[10]; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1166 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1167 | /* Allow up to two leading zeros - this will not lengthen |
| 1168 | * the number compared to using E-n. |
| 1169 | */ |
| 1170 | if (exp < 0 && exp > -3) /* PLUS 3 TOTAL 4 */ |
| 1171 | { |
| 1172 | czero = -exp; /* PLUS 2 digits: TOTAL 3 */ |
| 1173 | exp = 0; /* Dot added below before first output. */ |
| 1174 | } |
| 1175 | else |
| 1176 | czero = 0; /* No zeros to add */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1177 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1178 | /* Generate the digit list, stripping trailing zeros and |
| 1179 | * inserting a '.' before a digit if the exponent is 0. |
| 1180 | */ |
| 1181 | clead = czero; /* Count of leading zeros */ |
| 1182 | cdigits = 0; /* Count of digits in list. */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1183 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1184 | do |
| 1185 | { |
| 1186 | double d; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1187 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1188 | fp *= 10; |
| 1189 | /* Use modf here, not floor and subtract, so that |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1190 | * the separation is done in one step. At the end |
| 1191 | * of the loop don't break the number into parts so |
| 1192 | * that the final digit is rounded. |
| 1193 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1194 | if (cdigits+czero-clead+1 < (int)precision) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1195 | fp = modf(fp, &d); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1196 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1197 | else |
| 1198 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1199 | d = floor(fp + .5); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1200 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1201 | if (d > 9) |
| 1202 | { |
| 1203 | /* Rounding up to 10, handle that here. */ |
| 1204 | if (czero > 0) |
| 1205 | { |
| 1206 | --czero, d = 1; |
| 1207 | if (cdigits == 0) --clead; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1208 | } |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1209 | else |
| 1210 | { |
| 1211 | while (cdigits > 0 && d > 9) |
| 1212 | { |
| 1213 | int ch = *--ascii; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1214 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1215 | if (exp != (-1)) |
| 1216 | ++exp; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1217 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1218 | else if (ch == 46) |
| 1219 | { |
| 1220 | ch = *--ascii, ++size; |
| 1221 | /* Advance exp to '1', so that the |
| 1222 | * decimal point happens after the |
| 1223 | * previous digit. |
| 1224 | */ |
| 1225 | exp = 1; |
| 1226 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1227 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1228 | --cdigits; |
| 1229 | d = ch - 47; /* I.e. 1+(ch-48) */ |
| 1230 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1231 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1232 | /* Did we reach the beginning? If so adjust the |
| 1233 | * exponent but take into account the leading |
| 1234 | * decimal point. |
| 1235 | */ |
| 1236 | if (d > 9) /* cdigits == 0 */ |
| 1237 | { |
| 1238 | if (exp == (-1)) |
| 1239 | { |
| 1240 | /* Leading decimal point (plus zeros?), if |
| 1241 | * we lose the decimal point here it must |
| 1242 | * be reentered below. |
| 1243 | */ |
| 1244 | int ch = *--ascii; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1245 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1246 | if (ch == 46) |
| 1247 | ++size, exp = 1; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1248 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1249 | /* Else lost a leading zero, so 'exp' is |
| 1250 | * still ok at (-1) |
| 1251 | */ |
| 1252 | } |
| 1253 | else |
| 1254 | ++exp; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1255 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1256 | /* In all cases we output a '1' */ |
| 1257 | d = 1; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | fp = 0; /* Guarantees termination below. */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1262 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1263 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1264 | if (d == 0) |
| 1265 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1266 | ++czero; |
| 1267 | if (cdigits == 0) ++clead; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1268 | } |
| 1269 | else |
| 1270 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1271 | /* Included embedded zeros in the digit count. */ |
| 1272 | cdigits += czero - clead; |
| 1273 | clead = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1274 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1275 | while (czero > 0) |
| 1276 | { |
| 1277 | /* exp == (-1) means we just output the decimal |
| 1278 | * place - after the DP don't adjust 'exp' any |
| 1279 | * more! |
| 1280 | */ |
| 1281 | if (exp != (-1)) |
| 1282 | { |
| 1283 | if (exp == 0) *ascii++ = 46, --size; |
| 1284 | /* PLUS 1: TOTAL 4 */ |
| 1285 | --exp; |
| 1286 | } |
| 1287 | *ascii++ = 48, --czero; |
| 1288 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1289 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1290 | if (exp != (-1)) |
| 1291 | { |
| 1292 | if (exp == 0) *ascii++ = 46, --size; /* counted above */ |
| 1293 | --exp; |
| 1294 | } |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1295 | *ascii++ = (char)(48 + (int)d), ++cdigits; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | while (cdigits+czero-clead < (int)precision && fp > DBL_MIN); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1299 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1300 | /* The total output count (max) is now 4+precision */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1301 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1302 | /* Check for an exponent, if we don't need one we are |
| 1303 | * done and just need to terminate the string. At |
| 1304 | * this point exp==(-1) is effectively if flag - it got |
| 1305 | * to '-1' because of the decrement after outputing |
| 1306 | * the decimal point above (the exponent required is |
| 1307 | * *not* -1!) |
| 1308 | */ |
| 1309 | if (exp >= (-1) && exp <= 2) |
| 1310 | { |
| 1311 | /* The following only happens if we didn't output the |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1312 | * leading zeros above for negative exponent, so this |
| 1313 | * doest add to the digit requirement. Note that the |
| 1314 | * two zeros here can only be output if the two leading |
| 1315 | * zeros were *not* output, so this doesn't increase |
| 1316 | * the output count. |
| 1317 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1318 | while (--exp >= 0) *ascii++ = 48; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1319 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1320 | *ascii = 0; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1321 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1322 | /* Total buffer requirement (including the '\0') is |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1323 | * 5+precision - see check at the start. |
| 1324 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1325 | return; |
| 1326 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1327 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1328 | /* Here if an exponent is required, adjust size for |
| 1329 | * the digits we output but did not count. The total |
| 1330 | * digit output here so far is at most 1+precision - no |
| 1331 | * decimal point and no leading or trailing zeros have |
| 1332 | * been output. |
| 1333 | */ |
| 1334 | size -= cdigits; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1335 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1336 | *ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision*/ |
| 1337 | if (exp < 0) |
| 1338 | { |
| 1339 | *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */ |
| 1340 | exp = -exp; |
| 1341 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1342 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1343 | cdigits = 0; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1344 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1345 | while (exp > 0) |
| 1346 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1347 | exponent[cdigits++] = (char)(48 + exp % 10); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1348 | exp /= 10; |
| 1349 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1350 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1351 | /* Need another size check here for the exponent digits, so |
| 1352 | * this need not be considered above. |
| 1353 | */ |
| 1354 | if ((int)size > cdigits) |
| 1355 | { |
| 1356 | while (cdigits > 0) *ascii++ = exponent[--cdigits]; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1357 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1358 | *ascii = 0; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1359 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1360 | return; |
| 1361 | } |
| 1362 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1363 | } |
| 1364 | else if (!(fp >= DBL_MIN)) |
| 1365 | { |
| 1366 | *ascii++ = 48; /* '0' */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1367 | *ascii = 0; |
| 1368 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | *ascii++ = 105; /* 'i' */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1373 | *ascii++ = 110; /* 'n' */ |
| 1374 | *ascii++ = 102; /* 'f' */ |
| 1375 | *ascii = 0; |
| 1376 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* Here on buffer too small. */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1381 | png_error(png_ptr, "ASCII conversion buffer too small"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | # endif /* FLOATING_POINT */ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1385 | |
| 1386 | # ifdef PNG_FIXED_POINT_SUPPORTED |
| 1387 | /* Function to format a fixed point value in ASCII. |
| 1388 | */ |
| 1389 | void /* PRIVATE */ |
| 1390 | png_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size, |
| 1391 | png_fixed_point fp) |
| 1392 | { |
| 1393 | /* Require space for 10 decimal digits, a decimal point, a minus sign and a |
| 1394 | * trailing \0, 13 characters: |
| 1395 | */ |
| 1396 | if (size > 12) |
| 1397 | { |
| 1398 | png_uint_32 num; |
| 1399 | |
| 1400 | /* Avoid overflow here on the minimum integer. */ |
| 1401 | if (fp < 0) |
| 1402 | *ascii++ = 45, --size, num = -fp; |
| 1403 | else |
| 1404 | num = fp; |
| 1405 | |
| 1406 | if (num <= 0x80000000U) /* else overflowed */ |
| 1407 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1408 | unsigned int ndigits = 0, first = 16/*flag value*/; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1409 | char digits[10]; |
| 1410 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1411 | while (num) |
| 1412 | { |
| 1413 | /* Split the low digit off num: */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1414 | unsigned int tmp = num/10; |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1415 | num -= tmp*10; |
| 1416 | digits[ndigits++] = (char)(48 + num); |
| 1417 | /* Record the first non-zero digit, note that this is a number |
| 1418 | * starting at 1, it's not actually the array index. |
| 1419 | */ |
| 1420 | if (first == 16 && num > 0) |
| 1421 | first = ndigits; |
| 1422 | num = tmp; |
| 1423 | } |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1424 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1425 | if (ndigits > 0) |
| 1426 | { |
| 1427 | while (ndigits > 5) *ascii++ = digits[--ndigits]; |
| 1428 | /* The remaining digits are fractional digits, ndigits is '5' or |
| 1429 | * smaller at this point. It is certainly not zero. Check for a |
| 1430 | * non-zero fractional digit: |
| 1431 | */ |
| 1432 | if (first <= 5) |
| 1433 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1434 | unsigned int i; |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1435 | *ascii++ = 46; /* decimal point */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1436 | /* ndigits may be <5 for small numbers, output leading zeros |
| 1437 | * then ndigits digits to first: |
| 1438 | */ |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1439 | i = 5; |
| 1440 | while (ndigits < i) *ascii++ = 48, --i; |
| 1441 | while (ndigits >= first) *ascii++ = digits[--ndigits]; |
| 1442 | /* Don't output the trailing zeros! */ |
| 1443 | } |
| 1444 | } |
| 1445 | else |
| 1446 | *ascii++ = 48; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1447 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1448 | /* And null terminate the string: */ |
| 1449 | *ascii = 0; |
| 1450 | return; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | /* Here on buffer too small. */ |
| 1455 | png_error(png_ptr, "ASCII conversion buffer too small"); |
| 1456 | } |
| 1457 | # endif /* FIXED_POINT */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1458 | #endif /* READ_SCAL */ |
| 1459 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1460 | #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1461 | !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) |
| 1462 | png_fixed_point |
| 1463 | png_fixed(png_structp png_ptr, double fp, png_const_charp text) |
| 1464 | { |
| 1465 | double r = floor(100000 * fp + .5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1466 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1467 | if (r <= 2147483647. && r >= -2147483648.) |
| 1468 | return (png_fixed_point)r; |
| 1469 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1470 | png_fixed_error(png_ptr, text); |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1471 | /*NOT REACHED*/ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1472 | } |
| 1473 | #endif |
| 1474 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1475 | #if defined(PNG_READ_GAMMA_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1476 | defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG__READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1477 | /* muldiv functions */ |
| 1478 | /* This API takes signed arguments and rounds the result to the nearest |
| 1479 | * integer (or, for a fixed point number - the standard argument - to |
| 1480 | * the nearest .00001). Overflow and divide by zero are signalled in |
| 1481 | * the result, a boolean - true on success, false on overflow. |
| 1482 | */ |
| 1483 | int |
| 1484 | png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1485 | png_int_32 div) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1486 | { |
| 1487 | /* Return a * times / div, rounded. */ |
| 1488 | if (div != 0) |
| 1489 | { |
| 1490 | if (a == 0 || times == 0) |
| 1491 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1492 | *res = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1493 | return 1; |
| 1494 | } |
| 1495 | else |
| 1496 | { |
| 1497 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1498 | double r = a; |
| 1499 | r *= times; |
| 1500 | r /= div; |
| 1501 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1502 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1503 | /* A png_fixed_point is a 32 bit integer. */ |
| 1504 | if (r <= 2147483647. && r >= -2147483648.) |
| 1505 | { |
| 1506 | *res = (png_fixed_point)r; |
| 1507 | return 1; |
| 1508 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1509 | #else |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1510 | int negative = 0; |
| 1511 | png_uint_32 A, T, D; |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1512 | png_uint_32 s16, s32, s00; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1513 | |
| 1514 | if (a < 0) |
| 1515 | negative = 1, A = -a; |
| 1516 | else |
| 1517 | A = a; |
| 1518 | |
| 1519 | if (times < 0) |
| 1520 | negative = !negative, T = -times; |
| 1521 | else |
| 1522 | T = times; |
| 1523 | |
| 1524 | if (div < 0) |
| 1525 | negative = !negative, D = -div; |
| 1526 | else |
| 1527 | D = div; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1528 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1529 | /* Following can't overflow because the arguments only |
| 1530 | * have 31 bits each, however the result may be 32 bits. |
| 1531 | */ |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1532 | s16 = (A >> 16) * (T & 0xffff) + |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1533 | (A & 0xffff) * (T >> 16); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1534 | /* Can't overflow because the a*times bit is only 30 |
| 1535 | * bits at most. |
| 1536 | */ |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1537 | s32 = (A >> 16) * (T >> 16) + (s16 >> 16); |
| 1538 | s00 = (A & 0xffff) * (T & 0xffff); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1539 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1540 | s16 = (s16 & 0xffff) << 16; |
| 1541 | s00 += s16; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1542 | |
| 1543 | if (s00 < s16) |
| 1544 | ++s32; /* carry */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1545 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1546 | if (s32 < D) /* else overflow */ |
| 1547 | { |
| 1548 | /* s32.s00 is now the 64 bit product, do a standard |
| 1549 | * division, we know that s32 < D, so the maximum |
| 1550 | * required shift is 31. |
| 1551 | */ |
| 1552 | int bitshift = 32; |
| 1553 | png_fixed_point result = 0; /* NOTE: signed */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1554 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1555 | while (--bitshift >= 0) |
| 1556 | { |
| 1557 | png_uint_32 d32, d00; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1558 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1559 | if (bitshift > 0) |
| 1560 | d32 = D >> (32-bitshift), d00 = D << bitshift; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1561 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1562 | else |
| 1563 | d32 = 0, d00 = D; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1564 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1565 | if (s32 > d32) |
| 1566 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1567 | if (s00 < d00) --s32; /* carry */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1568 | s32 -= d32, s00 -= d00, result += 1<<bitshift; |
| 1569 | } |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1570 | |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1571 | else |
| 1572 | if (s32 == d32 && s00 >= d00) |
| 1573 | s32 = 0, s00 -= d00, result += 1<<bitshift; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1574 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1575 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1576 | /* Handle the rounding. */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1577 | if (s00 >= (D >> 1)) |
| 1578 | ++result; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1579 | |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1580 | if (negative) |
| 1581 | result = -result; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1582 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1583 | /* Check for overflow. */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1584 | if ((negative && result <= 0) || (!negative && result >= 0)) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1585 | { |
| 1586 | *res = result; |
| 1587 | return 1; |
| 1588 | } |
| 1589 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1590 | #endif |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | #endif /* READ_GAMMA || INCH_CONVERSIONS */ |
| 1597 | |
| 1598 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) |
| 1599 | /* The following is for when the caller doesn't much care about the |
| 1600 | * result. |
| 1601 | */ |
| 1602 | png_fixed_point |
| 1603 | png_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1604 | png_int_32 div) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1605 | { |
| 1606 | png_fixed_point result; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1607 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1608 | if (png_muldiv(&result, a, times, div)) |
| 1609 | return result; |
| 1610 | |
| 1611 | png_warning(png_ptr, "fixed point overflow ignored"); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | #endif |
| 1615 | |
| 1616 | #ifdef PNG_READ_GAMMA_SUPPORTED /* more fixed point functions for gammma */ |
| 1617 | /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ |
| 1618 | png_fixed_point |
| 1619 | png_reciprocal(png_fixed_point a) |
| 1620 | { |
| 1621 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 1622 | double r = floor(1E10/a+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1623 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1624 | if (r <= 2147483647. && r >= -2147483648.) |
| 1625 | return (png_fixed_point)r; |
| 1626 | #else |
| 1627 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1628 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1629 | if (png_muldiv(&res, 100000, 100000, a)) |
| 1630 | return res; |
| 1631 | #endif |
| 1632 | |
| 1633 | return 0; /* error/overflow */ |
| 1634 | } |
| 1635 | |
| 1636 | /* A local convenience routine. */ |
| 1637 | static png_fixed_point |
| 1638 | png_product2(png_fixed_point a, png_fixed_point b) |
| 1639 | { |
Glenn Randers-Pehrson | 9c69091 | 2010-08-27 11:39:38 -0500 | [diff] [blame] | 1640 | /* The required result is 1/a * 1/b; the following preserves accuracy. */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1641 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 1642 | double r = a * 1E-5; |
| 1643 | r *= b; |
| 1644 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1645 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1646 | if (r <= 2147483647. && r >= -2147483648.) |
| 1647 | return (png_fixed_point)r; |
| 1648 | #else |
| 1649 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1650 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1651 | if (png_muldiv(&res, a, b, 100000)) |
| 1652 | return res; |
| 1653 | #endif |
| 1654 | |
| 1655 | return 0; /* overflow */ |
| 1656 | } |
| 1657 | |
| 1658 | /* The inverse of the above. */ |
| 1659 | png_fixed_point |
| 1660 | png_reciprocal2(png_fixed_point a, png_fixed_point b) |
| 1661 | { |
Glenn Randers-Pehrson | 9c69091 | 2010-08-27 11:39:38 -0500 | [diff] [blame] | 1662 | /* The required result is 1/a * 1/b; the following preserves accuracy. */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1663 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 1664 | double r = 1E15/a; |
| 1665 | r /= b; |
| 1666 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1667 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1668 | if (r <= 2147483647. && r >= -2147483648.) |
| 1669 | return (png_fixed_point)r; |
| 1670 | #else |
| 1671 | /* This may overflow because the range of png_fixed_point isn't symmetric, |
| 1672 | * but this API is only used for the product of file and screen gamma so it |
| 1673 | * doesn't matter that the smallest number it can produce is 1/21474, not |
| 1674 | * 1/100000 |
| 1675 | */ |
| 1676 | png_fixed_point res = png_product2(a, b); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1677 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1678 | if (res != 0) |
| 1679 | return png_reciprocal(res); |
| 1680 | #endif |
| 1681 | |
| 1682 | return 0; /* overflow */ |
| 1683 | } |
| 1684 | #endif /* READ_GAMMA */ |
| 1685 | |
| 1686 | #ifdef PNG_CHECK_cHRM_SUPPORTED |
| 1687 | /* Added at libpng version 1.2.34 (Dec 8, 2008) and 1.4.0 (Jan 2, |
| 1688 | * 2010: moved from pngset.c) */ |
| 1689 | /* |
| 1690 | * Multiply two 32-bit numbers, V1 and V2, using 32-bit |
| 1691 | * arithmetic, to produce a 64 bit result in the HI/LO words. |
| 1692 | * |
| 1693 | * A B |
| 1694 | * x C D |
| 1695 | * ------ |
| 1696 | * AD || BD |
| 1697 | * AC || CB || 0 |
| 1698 | * |
| 1699 | * where A and B are the high and low 16-bit words of V1, |
| 1700 | * C and D are the 16-bit words of V2, AD is the product of |
| 1701 | * A and D, and X || Y is (X << 16) + Y. |
| 1702 | */ |
| 1703 | |
| 1704 | void /* PRIVATE */ |
| 1705 | png_64bit_product (long v1, long v2, unsigned long *hi_product, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1706 | unsigned long *lo_product) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1707 | { |
| 1708 | int a, b, c, d; |
| 1709 | long lo, hi, x, y; |
| 1710 | |
| 1711 | a = (v1 >> 16) & 0xffff; |
| 1712 | b = v1 & 0xffff; |
| 1713 | c = (v2 >> 16) & 0xffff; |
| 1714 | d = v2 & 0xffff; |
| 1715 | |
| 1716 | lo = b * d; /* BD */ |
| 1717 | x = a * d + c * b; /* AD + CB */ |
| 1718 | y = ((lo >> 16) & 0xffff) + x; |
| 1719 | |
| 1720 | lo = (lo & 0xffff) | ((y & 0xffff) << 16); |
| 1721 | hi = (y >> 16) & 0xffff; |
| 1722 | |
| 1723 | hi += a * c; /* AC */ |
| 1724 | |
| 1725 | *hi_product = (unsigned long)hi; |
| 1726 | *lo_product = (unsigned long)lo; |
| 1727 | } |
| 1728 | #endif /* CHECK_cHRM */ |
| 1729 | |
| 1730 | #ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */ |
| 1731 | #ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 1732 | /* Fixed point gamma. |
| 1733 | * |
| 1734 | * To calculate gamma this code implements fast log() and exp() calls using only |
| 1735 | * fixed point arithmetic. This code has sufficient precision for either 8 or |
| 1736 | * 16 bit sample values. |
| 1737 | * |
| 1738 | * The tables used here were calculated using simple 'bc' programs, but C double |
| 1739 | * precision floating point arithmetic would work fine. The programs are given |
| 1740 | * at the head of each table. |
| 1741 | * |
| 1742 | * 8 bit log table |
| 1743 | * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to |
| 1744 | * 255, so it's the base 2 logarithm of a normalized 8 bit floating point |
| 1745 | * mantissa. The numbers are 32 bit fractions. |
| 1746 | */ |
| 1747 | static png_uint_32 |
| 1748 | png_8bit_l2[128] = |
| 1749 | { |
| 1750 | # if PNG_DO_BC |
| 1751 | for (i=128;i<256;++i) { .5 - l(i/255)/l(2)*65536*65536; } |
| 1752 | # endif |
| 1753 | 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U, |
| 1754 | 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U, |
| 1755 | 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U, |
| 1756 | 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U, |
| 1757 | 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U, |
| 1758 | 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U, |
| 1759 | 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U, |
| 1760 | 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U, |
| 1761 | 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U, |
| 1762 | 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U, |
| 1763 | 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U, |
| 1764 | 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U, |
| 1765 | 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U, |
| 1766 | 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U, |
| 1767 | 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U, |
| 1768 | 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U, |
| 1769 | 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U, |
| 1770 | 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U, |
| 1771 | 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U, |
| 1772 | 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U, |
| 1773 | 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U, |
| 1774 | 24347096U, 0U |
| 1775 | #if 0 |
| 1776 | /* The following are the values for 16 bit tables - these work fine for the 8 |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1777 | * bit conversions but produce very slightly larger errors in the 16 bit log |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1778 | * (about 1.2 as opposed to 0.7 absolute error in the final value). To use |
| 1779 | * these all the shifts below must be adjusted appropriately. |
| 1780 | */ |
| 1781 | 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054, |
| 1782 | 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803, |
| 1783 | 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068, |
| 1784 | 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782, |
| 1785 | 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887, |
| 1786 | 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339, |
| 1787 | 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098, |
| 1788 | 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132, |
| 1789 | 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415, |
| 1790 | 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523, |
| 1791 | 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495, |
| 1792 | 1119, 744, 372 |
| 1793 | #endif |
| 1794 | }; |
| 1795 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1796 | static png_int_32 |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1797 | png_log8bit(unsigned int x) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1798 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1799 | unsigned int log = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1800 | /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log, |
| 1801 | * because the log is actually negate that means adding 1. The final |
| 1802 | * returned value thus has the range 0 (for 255 input) to 7.994 (for 1 |
| 1803 | * input), return 7.99998 for the overflow (log 0) case - so the result is |
| 1804 | * always at most 19 bits. |
| 1805 | */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1806 | if ((x &= 0xff) == 0) |
| 1807 | return 0xffffffff; |
| 1808 | |
| 1809 | if ((x & 0xf0) == 0) |
| 1810 | log = 4, x <<= 4; |
| 1811 | |
| 1812 | if ((x & 0xc0) == 0) |
| 1813 | log += 2, x <<= 2; |
| 1814 | |
| 1815 | if ((x & 0x80) == 0) |
| 1816 | log += 1, x <<= 1; |
| 1817 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1818 | /* result is at most 19 bits, so this cast is safe: */ |
| 1819 | return (png_int_32)((log << 16) + ((png_8bit_l2[x-128]+32768)>>16)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* The above gives exact (to 16 binary places) log2 values for 8 bit images, |
| 1823 | * for 16 bit images we use the most significant 8 bits of the 16 bit value to |
| 1824 | * get an approximation then multiply the approximation by a correction factor |
| 1825 | * determined by the remaining up to 8 bits. This requires an additional step |
| 1826 | * in the 16 bit case. |
| 1827 | * |
| 1828 | * We want log2(value/65535), we have log2(v'/255), where: |
| 1829 | * |
| 1830 | * value = v' * 256 + v'' |
| 1831 | * = v' * f |
| 1832 | * |
| 1833 | * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128 |
| 1834 | * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less |
| 1835 | * than 258. The final factor also needs to correct for the fact that our 8 bit |
| 1836 | * value is scaled by 255, whereas the 16 bit values must be scaled by 65535. |
| 1837 | * |
| 1838 | * This gives a final formula using a calculated value 'x' which is value/v' and |
| 1839 | * scaling by 65536 to match the above table: |
| 1840 | * |
| 1841 | * log2(x/257) * 65536 |
| 1842 | * |
| 1843 | * Since these numbers are so close to '1' we can use simple linear |
| 1844 | * interpolation between the two end values 256/257 (result -368.61) and 258/257 |
| 1845 | * (result 367.179). The values used below are scaled by a further 64 to give |
| 1846 | * 16 bit precision in the interpolation: |
| 1847 | * |
| 1848 | * Start (256): -23591 |
| 1849 | * Zero (257): 0 |
| 1850 | * End (258): 23499 |
| 1851 | */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1852 | static png_int_32 |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1853 | png_log16bit(png_uint_32 x) |
| 1854 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1855 | unsigned int log = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1856 | |
| 1857 | /* As above, but now the input has 16 bits. */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1858 | if ((x &= 0xffff) == 0) |
| 1859 | return 0xffffffff; |
| 1860 | |
| 1861 | if ((x & 0xff00) == 0) |
| 1862 | log = 8, x <<= 8; |
| 1863 | |
| 1864 | if ((x & 0xf000) == 0) |
| 1865 | log += 4, x <<= 4; |
| 1866 | |
| 1867 | if ((x & 0xc000) == 0) |
| 1868 | log += 2, x <<= 2; |
| 1869 | |
| 1870 | if ((x & 0x8000) == 0) |
| 1871 | log += 1, x <<= 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1872 | |
| 1873 | /* Calculate the base logarithm from the top 8 bits as a 28 bit fractional |
| 1874 | * value. |
| 1875 | */ |
| 1876 | log <<= 28; |
| 1877 | log += (png_8bit_l2[(x>>8)-128]+8) >> 4; |
| 1878 | |
| 1879 | /* Now we need to interpolate the factor, this requires a division by the top |
| 1880 | * 8 bits. Do this with maximum precision. |
| 1881 | */ |
| 1882 | x = ((x << 16) + (x >> 9)) / (x >> 8); |
| 1883 | |
| 1884 | /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24, |
| 1885 | * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly |
| 1886 | * 16 bits to interpolate to get the low bits of the result. Round the |
| 1887 | * answer. Note that the end point values are scaled by 64 to retain overall |
| 1888 | * precision and that 'log' is current scaled by an extra 12 bits, so adjust |
| 1889 | * the overall scaling by 6-12. Round at every step. |
| 1890 | */ |
| 1891 | x -= 1U << 24; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1892 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1893 | if (x <= 65536U) /* <= '257' */ |
| 1894 | log += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1895 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1896 | else |
| 1897 | log -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); |
| 1898 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1899 | /* Safe, because the result can't have more than 20 bits: */ |
| 1900 | return (png_int_32)((log + 2048) >> 12); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | /* The 'exp()' case must invert the above, taking a 20 bit fixed point |
| 1904 | * logarithmic value and returning a 16 or 8 bit number as appropriate. In |
| 1905 | * each case only the low 16 bits are relevant - the fraction - since the |
| 1906 | * integer bits (the top 4) simply determine a shift. |
| 1907 | * |
| 1908 | * The worst case is the 16 bit distinction between 65535 and 65534, this |
| 1909 | * requires perhaps spurious accuracty in the decoding of the logarithm to |
| 1910 | * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance |
| 1911 | * of getting this accuracy in practice. |
| 1912 | * |
| 1913 | * To deal with this the following exp() function works out the exponent of the |
| 1914 | * frational part of the logarithm by using an accurate 32 bit value from the |
| 1915 | * top four fractional bits then multiplying in the remaining bits. |
| 1916 | */ |
| 1917 | static png_uint_32 |
| 1918 | png_32bit_exp[16] = |
| 1919 | { |
| 1920 | # if PNG_DO_BC |
| 1921 | for (i=0;i<16;++i) { .5 + e(-i/16*l(2))*2^32; } |
| 1922 | # endif |
| 1923 | /* NOTE: the first entry is deliberately set to the maximum 32 bit value. */ |
| 1924 | 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U, |
| 1925 | 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U, |
| 1926 | 2553802834U, 2445529972U, 2341847524U, 2242560872U |
| 1927 | }; |
| 1928 | |
| 1929 | /* Adjustment table; provided to explain the numbers in the code below. */ |
| 1930 | #if PNG_DO_BC |
| 1931 | for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} |
| 1932 | 11 44937.64284865548751208448 |
| 1933 | 10 45180.98734845585101160448 |
| 1934 | 9 45303.31936980687359311872 |
| 1935 | 8 45364.65110595323018870784 |
| 1936 | 7 45395.35850361789624614912 |
| 1937 | 6 45410.72259715102037508096 |
| 1938 | 5 45418.40724413220722311168 |
| 1939 | 4 45422.25021786898173001728 |
| 1940 | 3 45424.17186732298419044352 |
| 1941 | 2 45425.13273269940811464704 |
| 1942 | 1 45425.61317555035558641664 |
| 1943 | 0 45425.85339951654943850496 |
| 1944 | #endif |
| 1945 | |
| 1946 | static png_uint_32 |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1947 | png_exp(png_fixed_point x) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1948 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1949 | if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1950 | { |
| 1951 | /* Obtain a 4 bit approximation */ |
| 1952 | png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf]; |
| 1953 | |
| 1954 | /* Incorporate the low 12 bits - these decrease the returned value by |
| 1955 | * multiplying by a number less than 1 if the bit is set. The multiplier |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1956 | * is determined by the above table and the shift. Notice that the values |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1957 | * converge on 45426 and this is used to allow linear interpolation of the |
| 1958 | * low bits. |
| 1959 | */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1960 | if (x & 0x800) |
| 1961 | e -= (((e >> 16) * 44938U) + 16U) >> 5; |
| 1962 | |
| 1963 | if (x & 0x400) |
| 1964 | e -= (((e >> 16) * 45181U) + 32U) >> 6; |
| 1965 | |
| 1966 | if (x & 0x200) |
| 1967 | e -= (((e >> 16) * 45303U) + 64U) >> 7; |
| 1968 | |
| 1969 | if (x & 0x100) |
| 1970 | e -= (((e >> 16) * 45365U) + 128U) >> 8; |
| 1971 | |
| 1972 | if (x & 0x080) |
| 1973 | e -= (((e >> 16) * 45395U) + 256U) >> 9; |
| 1974 | |
| 1975 | if (x & 0x040) |
| 1976 | e -= (((e >> 16) * 45410U) + 512U) >> 10; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1977 | |
| 1978 | /* And handle the low 6 bits in a single block. */ |
| 1979 | e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9; |
| 1980 | |
| 1981 | /* Handle the upper bits of x. */ |
| 1982 | e >>= x >> 16; |
| 1983 | return e; |
| 1984 | } |
| 1985 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1986 | /* Check for overflow */ |
| 1987 | if (x <= 0) |
| 1988 | return png_32bit_exp[0]; |
| 1989 | |
| 1990 | /* Else underflow */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1991 | return 0; |
| 1992 | } |
| 1993 | |
| 1994 | static png_byte |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 1995 | png_exp8bit(png_fixed_point log) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1996 | { |
| 1997 | /* Get a 32 bit value: */ |
| 1998 | png_uint_32 x = png_exp(log); |
| 1999 | |
| 2000 | /* Convert the 32 bit value to 0..255 by multiplying by 256-1, note that the |
| 2001 | * second, rounding, step can't overflow because of the first, subtraction, |
| 2002 | * step. |
| 2003 | */ |
| 2004 | x -= x >> 8; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2005 | return (png_byte)((x + 0x7fffffU) >> 24); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | static png_uint_16 |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2009 | png_exp16bit(png_fixed_point log) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2010 | { |
| 2011 | /* Get a 32 bit value: */ |
| 2012 | png_uint_32 x = png_exp(log); |
| 2013 | |
| 2014 | /* Convert the 32 bit value to 0..65535 by multiplying by 65536-1: */ |
| 2015 | x -= x >> 16; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2016 | return (png_uint_16)((x + 32767U) >> 16); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2017 | } |
| 2018 | #endif /* FLOATING_ARITHMETIC */ |
| 2019 | |
| 2020 | png_byte |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2021 | png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2022 | { |
| 2023 | if (value > 0 && value < 255) |
| 2024 | { |
| 2025 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2026 | double r = floor(255*pow(value/255.,gamma*.00001)+.5); |
| 2027 | return (png_byte)r; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2028 | # else |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2029 | png_int_32 log = png_log8bit(value); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2030 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2031 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2032 | if (png_muldiv(&res, gamma, log, PNG_FP_1)) |
| 2033 | return png_exp8bit(res); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2034 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 2035 | /* Overflow. */ |
| 2036 | value = 0; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2037 | # endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2038 | } |
| 2039 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2040 | return (png_byte)value; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2041 | } |
| 2042 | |
| 2043 | png_uint_16 |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2044 | png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2045 | { |
| 2046 | if (value > 0 && value < 65535) |
| 2047 | { |
| 2048 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2049 | double r = floor(65535*pow(value/65535.,gamma*.00001)+.5); |
| 2050 | return (png_uint_16)r; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2051 | # else |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2052 | png_int_32 log = png_log16bit(value); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2053 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2054 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2055 | if (png_muldiv(&res, gamma, log, PNG_FP_1)) |
| 2056 | return png_exp16bit(res); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2057 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 2058 | /* Overflow. */ |
| 2059 | value = 0; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2060 | # endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2061 | } |
| 2062 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2063 | return (png_uint_16)value; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | /* This does the right thing based on the bit_depth field of the |
| 2067 | * png_struct, interpreting values as 8 or 16 bit. While the result |
| 2068 | * is nominally a 16 bit value if bit depth is 8 then the result is |
| 2069 | * 8 bit (as are the arguments.) |
| 2070 | */ |
| 2071 | png_uint_16 /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2072 | png_gamma_correct(png_structp png_ptr, unsigned int value, |
| 2073 | png_fixed_point gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2074 | { |
| 2075 | if (png_ptr->bit_depth == 8) |
| 2076 | return png_gamma_8bit_correct(value, gamma); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2077 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2078 | else |
| 2079 | return png_gamma_16bit_correct(value, gamma); |
| 2080 | } |
| 2081 | |
| 2082 | /* This is the shared test on whether a gamma value is 'significant' - whether |
| 2083 | * it is worth doing gamma correction. |
| 2084 | */ |
| 2085 | int /* PRIVATE */ |
| 2086 | png_gamma_significant(png_fixed_point gamma) |
| 2087 | { |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2088 | return gamma < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || |
| 2089 | gamma > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2090 | } |
| 2091 | |
| 2092 | /* Internal function to build a single 16 bit table - the table consists of |
| 2093 | * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount |
| 2094 | * to shift the input values right (or 16-number_of_signifiant_bits). |
| 2095 | * |
Glenn Randers-Pehrson | a774c5d | 2010-08-26 19:37:55 -0500 | [diff] [blame] | 2096 | * The caller is responsible for ensuring that the table gets cleaned up on |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2097 | * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument |
| 2098 | * should be somewhere that will be cleaned. |
| 2099 | */ |
| 2100 | static void |
| 2101 | png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2102 | PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2103 | { |
| 2104 | /* Various values derived from 'shift': */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2105 | PNG_CONST unsigned int num = 1U << (8U - shift); |
| 2106 | PNG_CONST unsigned int max = (1U << (16U - shift))-1U; |
| 2107 | PNG_CONST unsigned int max_by_2 = 1U << (15U-shift); |
| 2108 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2109 | |
| 2110 | png_uint_16pp table = *ptable = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2111 | (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2112 | |
| 2113 | for (i = 0; i < num; i++) |
| 2114 | { |
| 2115 | png_uint_16p sub_table = table[i] = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2116 | (png_uint_16p)png_malloc(png_ptr, 256 * png_sizeof(png_uint_16)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2117 | |
| 2118 | /* The 'threshold' test is repeated here because it can arise for one of |
| 2119 | * the 16 bit tables even if the others don't hit it. |
| 2120 | */ |
| 2121 | if (png_gamma_significant(gamma)) |
| 2122 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2123 | /* The old code would overflow at the end and this would cause the |
| 2124 | * 'pow' function to return a result >1, resulting in an |
| 2125 | * arithmetic error. This code follows the spec exactly; ig is |
| 2126 | * the recovered input sample, it always has 8-16 bits. |
| 2127 | * |
| 2128 | * We want input * 65535/max, rounded, the arithmetic fits in 32 |
| 2129 | * bits (unsigned) so long as max <= 32767. |
| 2130 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2131 | unsigned int j; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2132 | for (j = 0; j < 256; j++) |
| 2133 | { |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2134 | png_uint_32 ig = (j << (8-shift)) + i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2135 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2136 | /* Inline the 'max' scaling operation: */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2137 | double d = floor(65535*pow(ig/(double)max, gamma*.00001)+.5); |
| 2138 | sub_table[j] = (png_uint_16)d; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2139 | # else |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2140 | if (shift) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 2141 | ig = (ig * 65535U + max_by_2)/max; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2142 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2143 | sub_table[j] = png_gamma_16bit_correct(ig, gamma); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2144 | # endif |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2145 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2146 | } |
| 2147 | else |
| 2148 | { |
| 2149 | /* We must still build a table, but do it the fast way. */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2150 | unsigned int j; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2151 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2152 | for (j = 0; j < 256; j++) |
| 2153 | { |
| 2154 | png_uint_32 ig = (j << (8-shift)) + i; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2155 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2156 | if (shift) |
| 2157 | ig = (ig * 65535U + max_by_2)/max; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2158 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2159 | sub_table[j] = (png_uint_16)ig; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2160 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | /* NOTE: this function expects the *inverse* of the overall gamma transformation |
| 2166 | * required. |
| 2167 | */ |
| 2168 | static void |
| 2169 | png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable, |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2170 | PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2171 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2172 | PNG_CONST unsigned int num = 1U << (8U - shift); |
| 2173 | PNG_CONST unsigned int max = (1U << (16U - shift))-1U; |
| 2174 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2175 | png_uint_32 last; |
| 2176 | |
| 2177 | png_uint_16pp table = *ptable = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2178 | (png_uint_16pp)png_calloc(png_ptr, num * png_sizeof(png_uint_16p)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2179 | |
| 2180 | /* 'num' is the number of tables and also the number of low bits of low |
| 2181 | * bits of the input 16 bit value used to select a table. Each table is |
| 2182 | * itself index by the high 8 bits of the value. |
| 2183 | */ |
| 2184 | for (i = 0; i < num; i++) |
| 2185 | table[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2186 | 256 * png_sizeof(png_uint_16)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2187 | |
| 2188 | /* 'gamma' is set to the reciprocal of the value calculated above, so |
| 2189 | * pow(out,g) is an *input* value. 'last' is the last input value set. |
| 2190 | * |
| 2191 | * In the loop 'i' is used to find output values. Since the output is 8 |
| 2192 | * bit there are only 256 possible values. The tables are set up to |
| 2193 | * select the closest possible output value for each input by finding |
| 2194 | * the input value at the boundary between each pair of output values |
| 2195 | * and filling the table up to that boundary with the lower output |
| 2196 | * value. |
| 2197 | * |
| 2198 | * The boundary values are 0.5,1.5..253.5,254.5. Since these are 9 bit |
| 2199 | * values the code below uses a 16 bit value in i; the values start at |
| 2200 | * 128.5 (for 0.5) and step by 257, for a total of 254 values (the last |
| 2201 | * entries are filled with 255). Start i at 128 and fill all 'last' |
| 2202 | * table entries <= 'max' |
| 2203 | */ |
| 2204 | last = 0; |
| 2205 | for (i = 0; i < 255; ++i) /* 8 bit output value */ |
| 2206 | { |
| 2207 | /* Find the corresponding maximum input value */ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2208 | png_uint_16 out = (png_uint_16)(i * 257U); /* 16 bit output value */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2209 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2210 | /* Find the boundary value in 16 bits: */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2211 | png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2212 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2213 | /* Adjust (round) to (16-shift) bits: */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2214 | bound = (bound * max + 32768U)/65535U + 1U; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2215 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2216 | while (last < bound) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2217 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2218 | table[last & (0xffU >> shift)][last >> (8U - shift)] = out; |
| 2219 | last++; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2220 | } |
| 2221 | } |
| 2222 | |
| 2223 | /* And fill in the final entries. */ |
| 2224 | while (last < (num << 8)) |
| 2225 | { |
| 2226 | table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U; |
| 2227 | last++; |
| 2228 | } |
| 2229 | } |
| 2230 | |
| 2231 | /* Build a single 8 bit table: same as the 16 bit case but much simpler (and |
| 2232 | * typically much faster). Note that libpng currently does no sBIT processing |
| 2233 | * (apparently contrary to the spec) so a 256 entry table is always generated. |
| 2234 | */ |
| 2235 | static void |
| 2236 | png_build_8bit_table(png_structp png_ptr, png_bytepp ptable, |
| 2237 | PNG_CONST png_fixed_point gamma) |
| 2238 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2239 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2240 | png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256); |
| 2241 | |
| 2242 | if (png_gamma_significant(gamma)) for (i=0; i<256; i++) |
| 2243 | table[i] = png_gamma_8bit_correct(i, gamma); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2244 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2245 | else for (i=0; i<256; ++i) |
| 2246 | table[i] = (png_byte)i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit |
| 2250 | * tables, we don't make a full table if we are reducing to 8-bit in |
| 2251 | * the future. Note also how the gamma_16 tables are segmented so that |
| 2252 | * we don't need to allocate > 64K chunks for a full 16-bit table. |
| 2253 | */ |
| 2254 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2255 | png_build_gamma_table(png_structp png_ptr, int bit_depth) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2256 | { |
| 2257 | png_debug(1, "in png_build_gamma_table"); |
| 2258 | |
| 2259 | if (bit_depth <= 8) |
| 2260 | { |
| 2261 | png_build_8bit_table(png_ptr, &png_ptr->gamma_table, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2262 | png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, |
| 2263 | png_ptr->screen_gamma) : PNG_FP_1); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2264 | |
| 2265 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 2266 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 2267 | if (png_ptr->transformations & ((PNG_BACKGROUND) | PNG_RGB_TO_GRAY)) |
| 2268 | { |
| 2269 | png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2270 | png_reciprocal(png_ptr->gamma)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2271 | |
| 2272 | png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2273 | png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : |
| 2274 | png_ptr->gamma/* Probably doing rgb_to_gray */); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2275 | } |
| 2276 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
| 2277 | } |
| 2278 | else |
| 2279 | { |
| 2280 | png_byte shift, sig_bit; |
| 2281 | |
| 2282 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 2283 | { |
| 2284 | sig_bit = png_ptr->sig_bit.red; |
| 2285 | |
| 2286 | if (png_ptr->sig_bit.green > sig_bit) |
| 2287 | sig_bit = png_ptr->sig_bit.green; |
| 2288 | |
| 2289 | if (png_ptr->sig_bit.blue > sig_bit) |
| 2290 | sig_bit = png_ptr->sig_bit.blue; |
| 2291 | } |
| 2292 | else |
| 2293 | sig_bit = png_ptr->sig_bit.gray; |
| 2294 | |
| 2295 | /* 16 bit gamma code uses this equation: |
| 2296 | * |
| 2297 | * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8] |
| 2298 | * |
| 2299 | * Where 'iv' is the input color value and 'ov' is the output value - |
| 2300 | * pow(iv, gamma). |
| 2301 | * |
| 2302 | * Thus the gamma table consists of up to 256 256 entry tables. The table |
| 2303 | * is selected by the (8-gamma_shift) most significant of the low 8 bits of |
| 2304 | * the color value then indexed by the upper 8 bits: |
| 2305 | * |
| 2306 | * table[low bits][high 8 bits] |
| 2307 | * |
| 2308 | * So the table 'n' corresponds to all those 'iv' of: |
| 2309 | * |
| 2310 | * <all high 8 bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1> |
| 2311 | * |
| 2312 | */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2313 | if (sig_bit > 0 && sig_bit < 16U) |
| 2314 | shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2315 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2316 | else |
| 2317 | shift = 0; /* keep all 16 bits */ |
| 2318 | |
| 2319 | if (png_ptr->transformations & PNG_16_TO_8) |
| 2320 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2321 | /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively |
| 2322 | * the significant bits in the *input* when the output will |
| 2323 | * eventually be 8 bits. By default it is 11. |
| 2324 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2325 | if (shift < (16U - PNG_MAX_GAMMA_8)) |
| 2326 | shift = (16U - PNG_MAX_GAMMA_8); |
| 2327 | } |
| 2328 | |
| 2329 | if (shift > 8U) |
| 2330 | shift = 8U; /* Guarantees at least one table! */ |
| 2331 | |
| 2332 | png_ptr->gamma_shift = shift; |
| 2333 | |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2334 | #ifdef PNG_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2335 | if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND)) |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2336 | #endif |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2337 | png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, |
| 2338 | png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma, |
| 2339 | png_ptr->screen_gamma) : PNG_FP_1); |
| 2340 | |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2341 | #ifdef PNG_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2342 | else |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2343 | png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, |
| 2344 | png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, |
| 2345 | png_ptr->screen_gamma) : PNG_FP_1); |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2346 | #endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2347 | |
| 2348 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 2349 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 2350 | if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY)) |
| 2351 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2352 | png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2353 | png_reciprocal(png_ptr->gamma)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2354 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2355 | /* Notice that the '16 from 1' table should be full precision, however |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2356 | * the lookup on this table still uses gamma_shift, so it can't be. |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2357 | * TODO: fix this. |
| 2358 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2359 | png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2360 | png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : |
| 2361 | png_ptr->gamma/* Probably doing rgb_to_gray */); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2362 | } |
| 2363 | #endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */ |
| 2364 | } |
| 2365 | } |
| 2366 | #endif /* READ_GAMMA */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2367 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |