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 | 4be12aa | 2011-11-10 06:35:48 -0600 | [diff] [blame] | 4 | * Last changed in libpng 1.5.7 [(PENDING RELEASE)] |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 5 | * Copyright (c) 1998-2011 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 | c32de8c | 2011-11-11 20:23:42 -0600 | [diff] [blame] | 17 | typedef png_libpng_version_1_5_7beta03 Your_png_h_is_not_version_1_5_7beta03; |
Glenn Randers-Pehrson | 520a764 | 2000-03-21 05:13:06 -0600 | [diff] [blame] | 18 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 19 | /* Tells libpng that we have already handled the first "num_bytes" bytes |
| 20 | * of the PNG file signature. If the PNG data is embedded into another |
| 21 | * stream we can set num_bytes = 8 so that libpng will not attempt to read |
| 22 | * or write any of the magic bytes before it starts on the IHDR. |
| 23 | */ |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 24 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 25 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 26 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 27 | png_set_sig_bytes(png_structp png_ptr, int num_bytes) |
| 28 | { |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 29 | png_debug(1, "in png_set_sig_bytes"); |
| 30 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 31 | if (png_ptr == NULL) |
| 32 | return; |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 33 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 34 | if (num_bytes > 8) |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 35 | png_error(png_ptr, "Too many bytes for PNG signature"); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 36 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 37 | png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* Checks whether the supplied bytes match the PNG signature. We allow |
| 41 | * checking less than the full 8-byte signature so that those apps that |
| 42 | * already read the first few bytes of a file to determine the file type |
| 43 | * can simply check the remaining bytes for extra assurance. Returns |
| 44 | * an integer less than, equal to, or greater than zero if sig is found, |
| 45 | * respectively, to be less than, to match, or be greater than the correct |
Glenn Randers-Pehrson | 6d62ef1 | 2011-08-25 18:17:50 -0500 | [diff] [blame] | 46 | * PNG signature (this is the same behavior as strcmp, memcmp, etc). |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 47 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 48 | int PNGAPI |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 49 | 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] | 50 | { |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 51 | 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] | 52 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 53 | if (num_to_check > 8) |
| 54 | num_to_check = 8; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 55 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 56 | else if (num_to_check < 1) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 57 | return (-1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 58 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 59 | if (start > 7) |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 60 | return (-1); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 61 | |
| 62 | if (start + num_to_check > 8) |
| 63 | num_to_check = 8 - start; |
| 64 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 65 | return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check))); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 66 | } |
| 67 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 68 | #endif /* PNG_READ_SUPPORTED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 69 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 70 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | c508081 | 2010-10-23 08:26:26 -0500 | [diff] [blame] | 71 | /* Function to allocate memory for zlib */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 72 | PNG_FUNCTION(voidpf /* PRIVATE */, |
| 73 | png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 74 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 75 | png_voidp ptr; |
| 76 | png_structp p=(png_structp)png_ptr; |
| 77 | png_uint_32 save_flags=p->flags; |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 78 | png_alloc_size_t num_bytes; |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 79 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 80 | if (png_ptr == NULL) |
| 81 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 82 | |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 83 | if (items > PNG_UINT_32_MAX/size) |
| 84 | { |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 85 | png_warning (p, "Potential overflow in png_zalloc()"); |
| 86 | return (NULL); |
Glenn Randers-Pehrson | 5fea36f | 2004-07-28 08:20:44 -0500 | [diff] [blame] | 87 | } |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 88 | num_bytes = (png_alloc_size_t)items * size; |
Glenn Randers-Pehrson | 9c0f094 | 2002-02-21 23:14:23 -0600 | [diff] [blame] | 89 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 90 | p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK; |
| 91 | ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes); |
| 92 | p->flags=save_flags; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 93 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 94 | return ((voidpf)ptr); |
| 95 | } |
| 96 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 97 | /* Function to free memory for zlib */ |
Glenn Randers-Pehrson | 3f705ba | 2009-07-23 12:53:06 -0500 | [diff] [blame] | 98 | void /* PRIVATE */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 99 | png_zfree(voidpf png_ptr, voidpf ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 100 | { |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 101 | png_free((png_structp)png_ptr, (png_voidp)ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 104 | /* 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] | 105 | * in case CRC is > 32 bits to leave the top bits 0. |
| 106 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 107 | void /* PRIVATE */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 108 | png_reset_crc(png_structp png_ptr) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 109 | { |
John Bowler | f3f7e14 | 2011-09-09 07:32:37 -0500 | [diff] [blame] | 110 | /* The cast is safe because the crc is a 32 bit value. */ |
| 111 | png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 114 | /* 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] | 115 | * much data to this routine as the largest single buffer size. We |
| 116 | * also check that this data will actually be used before going to the |
| 117 | * trouble of calculating it. |
| 118 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 119 | void /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 120 | 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] | 121 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 122 | int need_crc = 1; |
| 123 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 124 | if (PNG_CHUNK_ANCILLIARY(png_ptr->chunk_name)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 125 | { |
| 126 | if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == |
| 127 | (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) |
| 128 | need_crc = 0; |
| 129 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 130 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 131 | else /* critical */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 132 | { |
| 133 | if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) |
| 134 | need_crc = 0; |
| 135 | } |
| 136 | |
John Bowler | f3f7e14 | 2011-09-09 07:32:37 -0500 | [diff] [blame] | 137 | /* 'uLong' is defined as unsigned long, this means that on some systems it is |
| 138 | * a 64 bit value. crc32, however, returns 32 bits so the following cast is |
| 139 | * safe. 'uInt' may be no more than 16 bits, so it is necessary to perform a |
| 140 | * loop here. |
| 141 | */ |
| 142 | if (need_crc && length > 0) |
| 143 | { |
| 144 | uLong crc = png_ptr->crc; /* Should never issue a warning */ |
| 145 | |
| 146 | do |
| 147 | { |
| 148 | uInt safeLength = (uInt)length; |
| 149 | if (safeLength == 0) |
| 150 | safeLength = (uInt)-1; /* evil, but safe */ |
| 151 | |
| 152 | crc = crc32(crc, ptr, safeLength); |
| 153 | |
| 154 | /* The following should never issue compiler warnings, if they do the |
| 155 | * target system has characteristics that will probably violate other |
| 156 | * assumptions within the libpng code. |
| 157 | */ |
| 158 | ptr += safeLength; |
| 159 | length -= safeLength; |
| 160 | } |
| 161 | while (length > 0); |
| 162 | |
| 163 | /* And the following is always safe because the crc is only 32 bits. */ |
| 164 | png_ptr->crc = (png_uint_32)crc; |
| 165 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 166 | } |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 167 | |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 168 | /* Check a user supplied version number, called from both read and write |
| 169 | * functions that create a png_struct |
| 170 | */ |
| 171 | int |
| 172 | png_user_version_check(png_structp png_ptr, png_const_charp user_png_ver) |
| 173 | { |
| 174 | if (user_png_ver) |
| 175 | { |
| 176 | int i = 0; |
| 177 | |
| 178 | do |
| 179 | { |
| 180 | if (user_png_ver[i] != png_libpng_ver[i]) |
| 181 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
| 182 | } while (png_libpng_ver[i++]); |
| 183 | } |
| 184 | |
| 185 | else |
| 186 | png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; |
| 187 | |
| 188 | if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) |
| 189 | { |
| 190 | /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so |
| 191 | * we must recompile any applications that use any older library version. |
| 192 | * For versions after libpng 1.0, we will be compatible, so we need |
| 193 | * only check the first digit. |
| 194 | */ |
| 195 | if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] || |
| 196 | (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) || |
| 197 | (user_png_ver[0] == '0' && user_png_ver[2] < '9')) |
| 198 | { |
| 199 | #ifdef PNG_WARNINGS_SUPPORTED |
| 200 | size_t pos = 0; |
| 201 | char m[128]; |
| 202 | |
| 203 | pos = png_safecat(m, sizeof m, pos, "Application built with libpng-"); |
| 204 | pos = png_safecat(m, sizeof m, pos, user_png_ver); |
| 205 | pos = png_safecat(m, sizeof m, pos, " but running with "); |
| 206 | pos = png_safecat(m, sizeof m, pos, png_libpng_ver); |
| 207 | |
| 208 | png_warning(png_ptr, m); |
| 209 | #endif |
| 210 | |
| 211 | #ifdef PNG_ERROR_NUMBERS_SUPPORTED |
| 212 | png_ptr->flags = 0; |
| 213 | #endif |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* Success return. */ |
| 220 | return 1; |
| 221 | } |
| 222 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 223 | /* 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] | 224 | * 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] | 225 | * 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] | 226 | * and png_info_init() so that applications that want to use a shared |
| 227 | * libpng don't have to be recompiled if png_info changes size. |
| 228 | */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 229 | PNG_FUNCTION(png_infop,PNGAPI |
| 230 | png_create_info_struct,(png_structp png_ptr),PNG_ALLOCATED) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 231 | { |
| 232 | png_infop info_ptr; |
| 233 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 234 | png_debug(1, "in png_create_info_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 235 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 236 | if (png_ptr == NULL) |
| 237 | return (NULL); |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 238 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 239 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 240 | info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO, |
| 241 | png_ptr->malloc_fn, png_ptr->mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 242 | #else |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 243 | info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 244 | #endif |
Glenn Randers-Pehrson | 5cded0b | 2001-11-07 07:10:08 -0600 | [diff] [blame] | 245 | if (info_ptr != NULL) |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 246 | png_info_init_3(&info_ptr, png_sizeof(png_info)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 247 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 248 | return (info_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 249 | } |
| 250 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 251 | /* This function frees the memory associated with a single info struct. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 252 | * Normally, one would use either png_destroy_read_struct() or |
| 253 | * png_destroy_write_struct() to free an info struct, but this may be |
| 254 | * useful for some applications. |
| 255 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 256 | void PNGAPI |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 257 | png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr) |
| 258 | { |
| 259 | png_infop info_ptr = NULL; |
| 260 | |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 261 | png_debug(1, "in png_destroy_info_struct"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 262 | |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 263 | if (png_ptr == NULL) |
| 264 | return; |
| 265 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 266 | if (info_ptr_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 267 | info_ptr = *info_ptr_ptr; |
| 268 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 269 | if (info_ptr != NULL) |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 270 | { |
| 271 | png_info_destroy(png_ptr, info_ptr); |
| 272 | |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 273 | #ifdef PNG_USER_MEM_SUPPORTED |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 274 | png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn, |
| 275 | png_ptr->mem_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 276 | #else |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 277 | png_destroy_struct((png_voidp)info_ptr); |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 278 | #endif |
Glenn Randers-Pehrson | 3f54925 | 2001-10-27 07:35:13 -0500 | [diff] [blame] | 279 | *info_ptr_ptr = NULL; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | /* 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] | 284 | * and applications using it are urged to use png_create_info_struct() |
| 285 | * instead. |
| 286 | */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 287 | |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 288 | void PNGAPI |
| 289 | png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size) |
| 290 | { |
| 291 | png_infop info_ptr = *ptr_ptr; |
| 292 | |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 293 | png_debug(1, "in png_info_init_3"); |
| 294 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 295 | if (info_ptr == NULL) |
| 296 | return; |
Glenn Randers-Pehrson | 6b12c08 | 2006-11-14 10:53:30 -0600 | [diff] [blame] | 297 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 298 | 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] | 299 | { |
| 300 | png_destroy_struct(info_ptr); |
| 301 | info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO); |
| 302 | *ptr_ptr = info_ptr; |
| 303 | } |
Glenn Randers-Pehrson | 8b6a889 | 2001-05-18 04:54:50 -0500 | [diff] [blame] | 304 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 305 | /* Set everything to 0 */ |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 306 | png_memset(info_ptr, 0, png_sizeof(png_info)); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 307 | } |
| 308 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 309 | void PNGAPI |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 310 | png_data_freer(png_structp png_ptr, png_infop info_ptr, |
| 311 | int freer, png_uint_32 mask) |
| 312 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 313 | png_debug(1, "in png_data_freer"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 314 | |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 315 | if (png_ptr == NULL || info_ptr == NULL) |
| 316 | return; |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 317 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 318 | if (freer == PNG_DESTROY_WILL_FREE_DATA) |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 319 | info_ptr->free_me |= mask; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 320 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 321 | else if (freer == PNG_USER_WILL_FREE_DATA) |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 322 | info_ptr->free_me &= ~mask; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 323 | |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 324 | else |
| 325 | png_warning(png_ptr, |
Glenn Randers-Pehrson | beb572e | 2006-08-19 13:59:24 -0500 | [diff] [blame] | 326 | "Unknown freer parameter in png_data_freer"); |
Glenn Randers-Pehrson | 38e6e77 | 2000-04-09 19:06:13 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 329 | void PNGAPI |
Glenn Randers-Pehrson | 82ae383 | 2001-04-20 10:32:10 -0500 | [diff] [blame] | 330 | png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask, |
| 331 | int num) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 332 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 333 | png_debug(1, "in png_free_data"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 334 | |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 335 | if (png_ptr == NULL || info_ptr == NULL) |
| 336 | return; |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 337 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 338 | #ifdef PNG_TEXT_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 339 | /* 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] | 340 | if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 341 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 342 | if (num != -1) |
| 343 | { |
| 344 | if (info_ptr->text && info_ptr->text[num].key) |
| 345 | { |
| 346 | png_free(png_ptr, info_ptr->text[num].key); |
| 347 | info_ptr->text[num].key = NULL; |
| 348 | } |
| 349 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 350 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 351 | else |
| 352 | { |
| 353 | int i; |
| 354 | for (i = 0; i < info_ptr->num_text; i++) |
| 355 | png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); |
| 356 | png_free(png_ptr, info_ptr->text); |
| 357 | info_ptr->text = NULL; |
| 358 | info_ptr->num_text=0; |
| 359 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 360 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 361 | #endif |
| 362 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 363 | #ifdef PNG_tRNS_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 364 | /* Free any tRNS entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 365 | 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] | 366 | { |
Glenn Randers-Pehrson | 6abea75 | 2009-08-08 16:52:06 -0500 | [diff] [blame] | 367 | png_free(png_ptr, info_ptr->trans_alpha); |
| 368 | info_ptr->trans_alpha = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 369 | info_ptr->valid &= ~PNG_INFO_tRNS; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 370 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 371 | #endif |
| 372 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 373 | #ifdef PNG_sCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 374 | /* Free any sCAL entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 375 | 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] | 376 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 377 | png_free(png_ptr, info_ptr->scal_s_width); |
| 378 | png_free(png_ptr, info_ptr->scal_s_height); |
| 379 | info_ptr->scal_s_width = NULL; |
| 380 | info_ptr->scal_s_height = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 381 | info_ptr->valid &= ~PNG_INFO_sCAL; |
| 382 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 383 | #endif |
| 384 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 385 | #ifdef PNG_pCAL_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 386 | /* Free any pCAL entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 387 | 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] | 388 | { |
| 389 | png_free(png_ptr, info_ptr->pcal_purpose); |
| 390 | png_free(png_ptr, info_ptr->pcal_units); |
| 391 | info_ptr->pcal_purpose = NULL; |
| 392 | info_ptr->pcal_units = NULL; |
| 393 | if (info_ptr->pcal_params != NULL) |
| 394 | { |
| 395 | int i; |
| 396 | for (i = 0; i < (int)info_ptr->pcal_nparams; i++) |
| 397 | { |
| 398 | png_free(png_ptr, info_ptr->pcal_params[i]); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 399 | info_ptr->pcal_params[i] = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 400 | } |
| 401 | png_free(png_ptr, info_ptr->pcal_params); |
| 402 | info_ptr->pcal_params = NULL; |
| 403 | } |
| 404 | info_ptr->valid &= ~PNG_INFO_pCAL; |
| 405 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 406 | #endif |
| 407 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 408 | #ifdef PNG_iCCP_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 409 | /* Free any iCCP entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 410 | 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] | 411 | { |
| 412 | png_free(png_ptr, info_ptr->iccp_name); |
| 413 | png_free(png_ptr, info_ptr->iccp_profile); |
| 414 | info_ptr->iccp_name = NULL; |
| 415 | info_ptr->iccp_profile = NULL; |
| 416 | info_ptr->valid &= ~PNG_INFO_iCCP; |
| 417 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 418 | #endif |
| 419 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 420 | #ifdef PNG_sPLT_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 421 | /* 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] | 422 | if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 423 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 424 | if (num != -1) |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 425 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 426 | if (info_ptr->splt_palettes) |
| 427 | { |
| 428 | png_free(png_ptr, info_ptr->splt_palettes[num].name); |
| 429 | png_free(png_ptr, info_ptr->splt_palettes[num].entries); |
| 430 | info_ptr->splt_palettes[num].name = NULL; |
| 431 | info_ptr->splt_palettes[num].entries = NULL; |
| 432 | } |
| 433 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 434 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 435 | else |
| 436 | { |
| 437 | if (info_ptr->splt_palettes_num) |
| 438 | { |
| 439 | int i; |
| 440 | for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) |
| 441 | png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i); |
| 442 | |
| 443 | png_free(png_ptr, info_ptr->splt_palettes); |
| 444 | info_ptr->splt_palettes = NULL; |
| 445 | info_ptr->splt_palettes_num = 0; |
| 446 | } |
| 447 | info_ptr->valid &= ~PNG_INFO_sPLT; |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 448 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 449 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 450 | #endif |
| 451 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 452 | #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 453 | if (png_ptr->unknown_chunk.data) |
| 454 | { |
| 455 | png_free(png_ptr, png_ptr->unknown_chunk.data); |
| 456 | png_ptr->unknown_chunk.data = NULL; |
| 457 | } |
Glenn Randers-Pehrson | 895a9c9 | 2008-07-25 08:51:18 -0500 | [diff] [blame] | 458 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 459 | if ((mask & PNG_FREE_UNKN) & info_ptr->free_me) |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 460 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 461 | if (num != -1) |
| 462 | { |
| 463 | if (info_ptr->unknown_chunks) |
| 464 | { |
| 465 | png_free(png_ptr, info_ptr->unknown_chunks[num].data); |
| 466 | info_ptr->unknown_chunks[num].data = NULL; |
| 467 | } |
| 468 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 469 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 470 | else |
| 471 | { |
| 472 | int i; |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 473 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 474 | if (info_ptr->unknown_chunks_num) |
| 475 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 476 | 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] | 477 | png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i); |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 478 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 479 | png_free(png_ptr, info_ptr->unknown_chunks); |
| 480 | info_ptr->unknown_chunks = NULL; |
| 481 | info_ptr->unknown_chunks_num = 0; |
| 482 | } |
| 483 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 484 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 485 | #endif |
| 486 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 487 | #ifdef PNG_hIST_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 488 | /* Free any hIST entry */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 489 | 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] | 490 | { |
| 491 | png_free(png_ptr, info_ptr->hist); |
| 492 | info_ptr->hist = NULL; |
| 493 | info_ptr->valid &= ~PNG_INFO_hIST; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 494 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 495 | #endif |
| 496 | |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 497 | /* Free any PLTE entry that was internally allocated */ |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 498 | 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] | 499 | { |
| 500 | png_zfree(png_ptr, info_ptr->palette); |
| 501 | info_ptr->palette = NULL; |
| 502 | info_ptr->valid &= ~PNG_INFO_PLTE; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 503 | info_ptr->num_palette = 0; |
| 504 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 505 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 506 | #ifdef PNG_INFO_IMAGE_SUPPORTED |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 507 | /* 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] | 508 | 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] | 509 | { |
| 510 | if (info_ptr->row_pointers) |
| 511 | { |
| 512 | int row; |
| 513 | for (row = 0; row < (int)info_ptr->height; row++) |
| 514 | { |
| 515 | png_free(png_ptr, info_ptr->row_pointers[row]); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 516 | info_ptr->row_pointers[row] = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 517 | } |
| 518 | png_free(png_ptr, info_ptr->row_pointers); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 519 | info_ptr->row_pointers = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 520 | } |
| 521 | info_ptr->valid &= ~PNG_INFO_IDAT; |
| 522 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 523 | #endif |
Glenn Randers-Pehrson | fc4a143 | 2000-05-17 17:39:34 -0500 | [diff] [blame] | 524 | |
John Bowler | 56a739b | 2010-12-19 16:33:20 -0600 | [diff] [blame] | 525 | if (num != -1) |
| 526 | mask &= ~PNG_FREE_MUL; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 527 | |
John Bowler | 56a739b | 2010-12-19 16:33:20 -0600 | [diff] [blame] | 528 | info_ptr->free_me &= ~mask; |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 529 | } |
Glenn Randers-Pehrson | 166c5a3 | 1999-12-10 09:43:02 -0600 | [diff] [blame] | 530 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 531 | /* 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] | 532 | * pointing to before re-using it or freeing the struct itself. Recall |
| 533 | * that png_free() checks for NULL pointers for us. |
| 534 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 535 | void /* PRIVATE */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 536 | png_info_destroy(png_structp png_ptr, png_infop info_ptr) |
| 537 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 538 | png_debug(1, "in png_info_destroy"); |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 539 | |
| 540 | png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); |
Glenn Randers-Pehrson | d56aca7 | 2000-11-23 11:51:42 -0600 | [diff] [blame] | 541 | |
Glenn Randers-Pehrson | e26c095 | 2009-09-23 11:22:08 -0500 | [diff] [blame] | 542 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 543 | if (png_ptr->num_chunk_list) |
| 544 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 545 | png_free(png_ptr, png_ptr->chunk_list); |
Glenn Randers-Pehrson | f81b50b | 2009-12-29 16:50:15 -0600 | [diff] [blame] | 546 | png_ptr->chunk_list = NULL; |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 547 | png_ptr->num_chunk_list = 0; |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 548 | } |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 549 | #endif |
Glenn Randers-Pehrson | a77ef62 | 2000-02-18 13:48:52 -0600 | [diff] [blame] | 550 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 551 | png_info_init_3(&info_ptr, png_sizeof(png_info)); |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 552 | } |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 553 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 554 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 555 | /* 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] | 556 | * functions. The application should free any memory associated with this |
| 557 | * pointer before png_write_destroy() or png_read_destroy() are called. |
| 558 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 559 | png_voidp PNGAPI |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 560 | png_get_io_ptr(png_structp png_ptr) |
| 561 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 562 | if (png_ptr == NULL) |
| 563 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 564 | |
Glenn Randers-Pehrson | b212002 | 1998-01-31 20:07:59 -0600 | [diff] [blame] | 565 | return (png_ptr->io_ptr); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 566 | } |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 567 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 568 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 569 | # ifdef PNG_STDIO_SUPPORTED |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 570 | /* 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] | 571 | * 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] | 572 | * or png_set_write_fn() instead of png_init_io(). If you have defined |
Glenn Randers-Pehrson | ce775cc | 2011-07-13 06:28:26 -0500 | [diff] [blame] | 573 | * PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a |
| 574 | * function of your own because "FILE *" isn't necessarily available. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 575 | */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 576 | void PNGAPI |
Glenn Randers-Pehrson | 316f97a | 2000-07-08 13:19:41 -0500 | [diff] [blame] | 577 | png_init_io(png_structp png_ptr, png_FILE_p fp) |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 578 | { |
Glenn Randers-Pehrson | 51650b8 | 2008-08-05 07:44:42 -0500 | [diff] [blame] | 579 | png_debug(1, "in png_init_io"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 580 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 581 | if (png_ptr == NULL) |
| 582 | return; |
Glenn Randers-Pehrson | b3ce365 | 2009-08-15 21:47:03 -0500 | [diff] [blame] | 583 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 584 | png_ptr->io_ptr = (png_voidp)fp; |
| 585 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 586 | # endif |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 587 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 588 | # ifdef PNG_TIME_RFC1123_SUPPORTED |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 589 | /* Convert the supplied time into an RFC 1123 string suitable for use in |
| 590 | * a "Creation Time" or other text-based time string. |
| 591 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 592 | png_const_charp PNGAPI |
| 593 | 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] | 594 | { |
| 595 | static PNG_CONST char short_months[12][4] = |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 596 | {"Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 597 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 598 | |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 599 | if (png_ptr == NULL) |
| 600 | return (NULL); |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 601 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 602 | { |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 603 | size_t pos = 0; |
| 604 | char number_buf[5]; /* enough for a four digit year */ |
| 605 | |
| 606 | # define APPEND_STRING(string)\ |
| 607 | pos = png_safecat(png_ptr->time_buffer, sizeof png_ptr->time_buffer,\ |
| 608 | pos, (string)) |
| 609 | # define APPEND_NUMBER(format, value)\ |
| 610 | APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value))) |
| 611 | # define APPEND(ch)\ |
| 612 | if (pos < (sizeof png_ptr->time_buffer)-1)\ |
| 613 | png_ptr->time_buffer[pos++] = (ch) |
Glenn Randers-Pehrson | af855e4 | 2011-05-07 10:52:49 -0500 | [diff] [blame] | 614 | |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 615 | APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day % 32); |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 616 | APPEND(' '); |
| 617 | APPEND_STRING(short_months[(ptime->month - 1) % 12]); |
| 618 | APPEND(' '); |
| 619 | APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year); |
| 620 | APPEND(' '); |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 621 | APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour % 24); |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 622 | APPEND(':'); |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 623 | APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute % 60); |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 624 | APPEND(':'); |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 625 | APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second % 61); |
John Bowler | 88b77cc | 2011-05-05 06:49:55 -0500 | [diff] [blame] | 626 | APPEND_STRING(" +0000"); /* This reliably terminates the buffer */ |
| 627 | |
| 628 | # undef APPEND |
| 629 | # undef APPEND_NUMBER |
| 630 | # undef APPEND_STRING |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 631 | } |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 632 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 633 | return png_ptr->time_buffer; |
Glenn Randers-Pehrson | f7d1a17 | 1998-06-06 15:31:35 -0500 | [diff] [blame] | 634 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 635 | # endif /* PNG_TIME_RFC1123_SUPPORTED */ |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 636 | |
Glenn Randers-Pehrson | 9c3ab68 | 2006-02-20 22:09:05 -0600 | [diff] [blame] | 637 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 638 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 639 | png_const_charp PNGAPI |
John Bowler | 0a5c9c0 | 2011-01-22 17:36:34 -0600 | [diff] [blame] | 640 | png_get_copyright(png_const_structp png_ptr) |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 641 | { |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 642 | PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 643 | #ifdef PNG_STRING_COPYRIGHT |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 644 | return PNG_STRING_COPYRIGHT |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 645 | #else |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 646 | # ifdef __STDC__ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 647 | return PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | 8596468 | 2011-11-16 08:52:36 -0600 | [diff] [blame] | 648 | "libpng version 1.5.7beta03 - November 16, 2011" PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 649 | "Copyright (c) 1998-2011 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ |
Glenn Randers-Pehrson | 43aaf6e | 2008-08-05 22:17:03 -0500 | [diff] [blame] | 650 | "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ |
| 651 | "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 652 | PNG_STRING_NEWLINE; |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 653 | # else |
Glenn Randers-Pehrson | 8596468 | 2011-11-16 08:52:36 -0600 | [diff] [blame] | 654 | return "libpng version 1.5.7beta03 - November 16, 2011\ |
Glenn Randers-Pehrson | 64b863c | 2011-01-04 09:57:06 -0600 | [diff] [blame] | 655 | Copyright (c) 1998-2011 Glenn Randers-Pehrson\ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 656 | Copyright (c) 1996-1997 Andreas Dilger\ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 657 | Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."; |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 658 | # endif |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 659 | #endif |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 660 | } |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 661 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 662 | /* 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] | 663 | * format 1.0.0 through 99.99.99zz. To get the version of *.h files |
| 664 | * used with your application, print out PNG_LIBPNG_VER_STRING, which |
| 665 | * is defined in png.h. |
| 666 | * Note: now there is no difference between png_get_libpng_ver() and |
| 667 | * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, |
| 668 | * 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] | 669 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 670 | png_const_charp PNGAPI |
John Bowler | 0a5c9c0 | 2011-01-22 17:36:34 -0600 | [diff] [blame] | 671 | png_get_libpng_ver(png_const_structp png_ptr) |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 672 | { |
| 673 | /* Version of *.c files used when building libpng */ |
Glenn Randers-Pehrson | 4c8f726 | 2010-03-16 19:30:01 -0500 | [diff] [blame] | 674 | return png_get_header_ver(png_ptr); |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 675 | } |
| 676 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 677 | png_const_charp PNGAPI |
John Bowler | 0a5c9c0 | 2011-01-22 17:36:34 -0600 | [diff] [blame] | 678 | png_get_header_ver(png_const_structp png_ptr) |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 679 | { |
| 680 | /* Version of *.h files used when building libpng */ |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 681 | PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 682 | return PNG_LIBPNG_VER_STRING; |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 683 | } |
| 684 | |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 685 | png_const_charp PNGAPI |
John Bowler | 0a5c9c0 | 2011-01-22 17:36:34 -0600 | [diff] [blame] | 686 | png_get_header_version(png_const_structp png_ptr) |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 687 | { |
| 688 | /* Returns longer string containing both version and date */ |
Glenn Randers-Pehrson | d546f43 | 2010-12-04 20:41:36 -0600 | [diff] [blame] | 689 | PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 690 | #ifdef __STDC__ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 691 | return PNG_HEADER_VERSION_STRING |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 692 | # ifndef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 693 | " (NO READ SUPPORT)" |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 694 | # endif |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 695 | PNG_STRING_NEWLINE; |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 696 | #else |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 697 | return PNG_HEADER_VERSION_STRING; |
Glenn Randers-Pehrson | e0784c7 | 2008-08-09 07:11:44 -0500 | [diff] [blame] | 698 | #endif |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame] | 699 | } |
| 700 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 701 | #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED |
Glenn Randers-Pehrson | c1bfe68 | 2002-03-06 22:08:00 -0600 | [diff] [blame] | 702 | int PNGAPI |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 703 | 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] | 704 | { |
Glenn Randers-Pehrson | 4bb4d01 | 2009-05-20 12:45:29 -0500 | [diff] [blame] | 705 | /* Check chunk_name and return "keep" value if it's on the list, else 0 */ |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 706 | png_const_bytep p, p_end; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 707 | |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 708 | if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list <= 0) |
| 709 | return PNG_HANDLE_CHUNK_AS_DEFAULT; |
| 710 | |
| 711 | p_end = png_ptr->chunk_list; |
| 712 | p = p_end + png_ptr->num_chunk_list*5; /* beyond end */ |
| 713 | |
| 714 | /* The code is the fifth byte after each four byte string. Historically this |
| 715 | * code was always searched from the end of the list, so it should continue |
| 716 | * to do so in case there are duplicated entries. |
| 717 | */ |
| 718 | do /* num_chunk_list > 0, so at least one */ |
| 719 | { |
| 720 | p -= 5; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 721 | if (!png_memcmp(chunk_name, p, 4)) |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 722 | return p[4]; |
| 723 | } |
| 724 | while (p > p_end); |
| 725 | |
| 726 | return PNG_HANDLE_CHUNK_AS_DEFAULT; |
Glenn Randers-Pehrson | 61c32d9 | 2000-02-04 23:40:16 -0600 | [diff] [blame] | 727 | } |
Glenn Randers-Pehrson | bb5cb14 | 2011-09-22 12:41:58 -0500 | [diff] [blame] | 728 | |
| 729 | int /* PRIVATE */ |
| 730 | png_chunk_unknown_handling(png_structp png_ptr, png_uint_32 chunk_name) |
| 731 | { |
| 732 | png_byte chunk_string[5]; |
| 733 | |
| 734 | PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name); |
| 735 | return png_handle_as_unknown(png_ptr, chunk_string); |
| 736 | } |
| 737 | #endif |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 738 | |
Glenn Randers-Pehrson | f10fa3c | 2010-04-29 08:25:29 -0500 | [diff] [blame] | 739 | #ifdef PNG_READ_SUPPORTED |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 740 | /* This function, added to libpng-1.0.6g, is untested. */ |
Glenn Randers-Pehrson | 7529457 | 2000-05-06 14:09:57 -0500 | [diff] [blame] | 741 | int PNGAPI |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 742 | png_reset_zstream(png_structp png_ptr) |
| 743 | { |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 744 | if (png_ptr == NULL) |
| 745 | return Z_STREAM_ERROR; |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 746 | |
Glenn Randers-Pehrson | 228bd39 | 2000-04-23 23:14:02 -0500 | [diff] [blame] | 747 | return (inflateReset(&png_ptr->zstream)); |
| 748 | } |
Glenn Randers-Pehrson | 2b8bef1 | 2010-04-29 11:50:24 -0500 | [diff] [blame] | 749 | #endif /* PNG_READ_SUPPORTED */ |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 750 | |
Glenn Randers-Pehrson | 5e5c1e1 | 2000-11-10 12:26:19 -0600 | [diff] [blame] | 751 | /* This function was added to libpng-1.0.7 */ |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 752 | png_uint_32 PNGAPI |
| 753 | png_access_version_number(void) |
| 754 | { |
| 755 | /* Version of *.c files used when building libpng */ |
Glenn Randers-Pehrson | d233287 | 2010-10-12 19:19:28 -0500 | [diff] [blame] | 756 | return((png_uint_32)PNG_LIBPNG_VER); |
Glenn Randers-Pehrson | 1ef65b6 | 2000-05-12 06:19:53 -0500 | [diff] [blame] | 757 | } |
Glenn Randers-Pehrson | 231e687 | 2001-01-12 15:13:06 -0600 | [diff] [blame] | 758 | |
Glenn Randers-Pehrson | 1fd5fb3 | 2001-05-06 05:34:26 -0500 | [diff] [blame] | 759 | |
Glenn Randers-Pehrson | 145f5c8 | 2008-07-10 09:13:13 -0500 | [diff] [blame] | 760 | |
| 761 | #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) |
John Bowler | 736f40f | 2011-08-25 16:19:44 -0500 | [diff] [blame] | 762 | /* png_convert_size: a PNGAPI but no longer in png.h, so deleted |
| 763 | * at libpng 1.5.5! |
| 764 | */ |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 765 | |
Glenn Randers-Pehrson | 02a5e33 | 2008-11-24 22:10:23 -0600 | [diff] [blame] | 766 | /* 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] | 767 | # ifdef PNG_CHECK_cHRM_SUPPORTED |
Glenn Randers-Pehrson glennrp@comcast.net | b1c0d33 | 2009-05-15 20:39:34 -0500 | [diff] [blame] | 768 | |
Glenn Randers-Pehrson | 3f705ba | 2009-07-23 12:53:06 -0500 | [diff] [blame] | 769 | int /* PRIVATE */ |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 770 | png_check_cHRM_fixed(png_structp png_ptr, |
| 771 | png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, |
| 772 | png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, |
| 773 | png_fixed_point blue_x, png_fixed_point blue_y) |
| 774 | { |
| 775 | int ret = 1; |
Glenn Randers-Pehrson | d0c4059 | 2008-11-22 07:09:51 -0600 | [diff] [blame] | 776 | unsigned long xy_hi,xy_lo,yx_hi,yx_lo; |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 777 | |
| 778 | png_debug(1, "in function png_check_cHRM_fixed"); |
Glenn Randers-Pehrson | da00980 | 2009-08-15 13:25:47 -0500 | [diff] [blame] | 779 | |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 780 | if (png_ptr == NULL) |
| 781 | return 0; |
| 782 | |
John Bowler | d2f0bc2 | 2011-06-11 06:42:06 -0500 | [diff] [blame] | 783 | /* (x,y,z) values are first limited to 0..100000 (PNG_FP_1), the white |
| 784 | * y must also be greater than 0. To test for the upper limit calculate |
| 785 | * (PNG_FP_1-y) - x must be <= to this for z to be >= 0 (and the expression |
| 786 | * cannot overflow.) At this point we know x and y are >= 0 and (x+y) is |
| 787 | * <= PNG_FP_1. The previous test on PNG_MAX_UINT_31 is removed because it |
| 788 | * pointless (and it produces compiler warnings!) |
| 789 | */ |
Glenn Randers-Pehrson | 02a5e33 | 2008-11-24 22:10:23 -0600 | [diff] [blame] | 790 | if (white_x < 0 || white_y <= 0 || |
| 791 | red_x < 0 || red_y < 0 || |
| 792 | green_x < 0 || green_y < 0 || |
| 793 | blue_x < 0 || blue_y < 0) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 794 | { |
| 795 | png_warning(png_ptr, |
| 796 | "Ignoring attempt to set negative chromaticity value"); |
| 797 | ret = 0; |
| 798 | } |
John Bowler | d2f0bc2 | 2011-06-11 06:42:06 -0500 | [diff] [blame] | 799 | /* And (x+y) must be <= PNG_FP_1 (so z is >= 0) */ |
| 800 | if (white_x > PNG_FP_1 - white_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 801 | { |
| 802 | png_warning(png_ptr, "Invalid cHRM white point"); |
| 803 | ret = 0; |
| 804 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 805 | |
John Bowler | d2f0bc2 | 2011-06-11 06:42:06 -0500 | [diff] [blame] | 806 | if (red_x > PNG_FP_1 - red_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 807 | { |
| 808 | png_warning(png_ptr, "Invalid cHRM red point"); |
| 809 | ret = 0; |
| 810 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 811 | |
John Bowler | d2f0bc2 | 2011-06-11 06:42:06 -0500 | [diff] [blame] | 812 | if (green_x > PNG_FP_1 - green_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 813 | { |
| 814 | png_warning(png_ptr, "Invalid cHRM green point"); |
| 815 | ret = 0; |
| 816 | } |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 817 | |
John Bowler | d2f0bc2 | 2011-06-11 06:42:06 -0500 | [diff] [blame] | 818 | if (blue_x > PNG_FP_1 - blue_y) |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 819 | { |
| 820 | png_warning(png_ptr, "Invalid cHRM blue point"); |
| 821 | ret = 0; |
| 822 | } |
Glenn Randers-Pehrson | d0c4059 | 2008-11-22 07:09:51 -0600 | [diff] [blame] | 823 | |
| 824 | png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo); |
| 825 | png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo); |
| 826 | |
| 827 | if (xy_hi == yx_hi && xy_lo == yx_lo) |
| 828 | { |
| 829 | png_warning(png_ptr, |
| 830 | "Ignoring attempt to set cHRM RGB triangle with zero area"); |
| 831 | ret = 0; |
| 832 | } |
| 833 | |
Glenn Randers-Pehrson | f783101 | 2008-11-13 06:05:13 -0600 | [diff] [blame] | 834 | return ret; |
| 835 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 836 | # endif /* PNG_CHECK_cHRM_SUPPORTED */ |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 837 | |
John Bowler | 736f40f | 2011-08-25 16:19:44 -0500 | [diff] [blame] | 838 | #ifdef PNG_cHRM_SUPPORTED |
| 839 | /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for |
| 840 | * cHRM, as opposed to using chromaticities. These internal APIs return |
| 841 | * non-zero on a parameter error. The X, Y and Z values are required to be |
| 842 | * positive and less than 1.0. |
| 843 | */ |
| 844 | int png_xy_from_XYZ(png_xy *xy, png_XYZ XYZ) |
| 845 | { |
| 846 | png_int_32 d, dwhite, whiteX, whiteY; |
| 847 | |
| 848 | d = XYZ.redX + XYZ.redY + XYZ.redZ; |
| 849 | if (!png_muldiv(&xy->redx, XYZ.redX, PNG_FP_1, d)) return 1; |
| 850 | if (!png_muldiv(&xy->redy, XYZ.redY, PNG_FP_1, d)) return 1; |
| 851 | dwhite = d; |
| 852 | whiteX = XYZ.redX; |
| 853 | whiteY = XYZ.redY; |
| 854 | |
| 855 | d = XYZ.greenX + XYZ.greenY + XYZ.greenZ; |
| 856 | if (!png_muldiv(&xy->greenx, XYZ.greenX, PNG_FP_1, d)) return 1; |
| 857 | if (!png_muldiv(&xy->greeny, XYZ.greenY, PNG_FP_1, d)) return 1; |
| 858 | dwhite += d; |
| 859 | whiteX += XYZ.greenX; |
| 860 | whiteY += XYZ.greenY; |
| 861 | |
| 862 | d = XYZ.blueX + XYZ.blueY + XYZ.blueZ; |
| 863 | if (!png_muldiv(&xy->bluex, XYZ.blueX, PNG_FP_1, d)) return 1; |
| 864 | if (!png_muldiv(&xy->bluey, XYZ.blueY, PNG_FP_1, d)) return 1; |
| 865 | dwhite += d; |
| 866 | whiteX += XYZ.blueX; |
| 867 | whiteY += XYZ.blueY; |
| 868 | |
| 869 | /* The reference white is simply the same of the end-point (X,Y,Z) vectors, |
| 870 | * thus: |
| 871 | */ |
| 872 | if (!png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite)) return 1; |
| 873 | if (!png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite)) return 1; |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | int png_XYZ_from_xy(png_XYZ *XYZ, png_xy xy) |
| 879 | { |
| 880 | png_fixed_point red_inverse, green_inverse, blue_scale; |
| 881 | png_fixed_point left, right, denominator; |
| 882 | |
| 883 | /* Check xy and, implicitly, z. Note that wide gamut color spaces typically |
| 884 | * have end points with 0 tristimulus values (these are impossible end |
| 885 | * points, but they are used to cover the possible colors.) |
| 886 | */ |
| 887 | if (xy.redx < 0 || xy.redx > PNG_FP_1) return 1; |
| 888 | if (xy.redy < 0 || xy.redy > PNG_FP_1-xy.redx) return 1; |
| 889 | if (xy.greenx < 0 || xy.greenx > PNG_FP_1) return 1; |
| 890 | if (xy.greeny < 0 || xy.greeny > PNG_FP_1-xy.greenx) return 1; |
| 891 | if (xy.bluex < 0 || xy.bluex > PNG_FP_1) return 1; |
| 892 | if (xy.bluey < 0 || xy.bluey > PNG_FP_1-xy.bluex) return 1; |
| 893 | if (xy.whitex < 0 || xy.whitex > PNG_FP_1) return 1; |
| 894 | if (xy.whitey < 0 || xy.whitey > PNG_FP_1-xy.whitex) return 1; |
| 895 | |
| 896 | /* The reverse calculation is more difficult because the original tristimulus |
| 897 | * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8 |
| 898 | * derived values were recorded in the cHRM chunk; |
| 899 | * (red,green,blue,white)x(x,y). This loses one degree of freedom and |
| 900 | * therefore an arbitrary ninth value has to be introduced to undo the |
| 901 | * original transformations. |
| 902 | * |
| 903 | * Think of the original end-points as points in (X,Y,Z) space. The |
| 904 | * chromaticity values (c) have the property: |
| 905 | * |
| 906 | * C |
| 907 | * c = --------- |
| 908 | * X + Y + Z |
| 909 | * |
| 910 | * For each c (x,y,z) from the corresponding original C (X,Y,Z). Thus the |
| 911 | * three chromaticity values (x,y,z) for each end-point obey the |
| 912 | * relationship: |
| 913 | * |
| 914 | * x + y + z = 1 |
| 915 | * |
| 916 | * This describes the plane in (X,Y,Z) space that intersects each axis at the |
| 917 | * value 1.0; call this the chromaticity plane. Thus the chromaticity |
| 918 | * calculation has scaled each end-point so that it is on the x+y+z=1 plane |
| 919 | * and chromaticity is the intersection of the vector from the origin to the |
| 920 | * (X,Y,Z) value with the chromaticity plane. |
| 921 | * |
| 922 | * To fully invert the chromaticity calculation we would need the three |
| 923 | * end-point scale factors, (red-scale, green-scale, blue-scale), but these |
| 924 | * were not recorded. Instead we calculated the reference white (X,Y,Z) and |
| 925 | * recorded the chromaticity of this. The reference white (X,Y,Z) would have |
| 926 | * given all three of the scale factors since: |
| 927 | * |
| 928 | * color-C = color-c * color-scale |
| 929 | * white-C = red-C + green-C + blue-C |
| 930 | * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale |
| 931 | * |
| 932 | * But cHRM records only white-x and white-y, so we have lost the white scale |
| 933 | * factor: |
| 934 | * |
| 935 | * white-C = white-c*white-scale |
| 936 | * |
| 937 | * To handle this the inverse transformation makes an arbitrary assumption |
| 938 | * about white-scale: |
| 939 | * |
| 940 | * Assume: white-Y = 1.0 |
| 941 | * Hence: white-scale = 1/white-y |
| 942 | * Or: red-Y + green-Y + blue-Y = 1.0 |
| 943 | * |
| 944 | * Notice the last statement of the assumption gives an equation in three of |
| 945 | * the nine values we want to calculate. 8 more equations come from the |
| 946 | * above routine as summarised at the top above (the chromaticity |
| 947 | * calculation): |
| 948 | * |
| 949 | * Given: color-x = color-X / (color-X + color-Y + color-Z) |
| 950 | * Hence: (color-x - 1)*color-X + color.x*color-Y + color.x*color-Z = 0 |
| 951 | * |
| 952 | * This is 9 simultaneous equations in the 9 variables "color-C" and can be |
| 953 | * solved by Cramer's rule. Cramer's rule requires calculating 10 9x9 matrix |
| 954 | * determinants, however this is not as bad as it seems because only 28 of |
| 955 | * the total of 90 terms in the various matrices are non-zero. Nevertheless |
| 956 | * Cramer's rule is notoriously numerically unstable because the determinant |
| 957 | * calculation involves the difference of large, but similar, numbers. It is |
| 958 | * difficult to be sure that the calculation is stable for real world values |
| 959 | * and it is certain that it becomes unstable where the end points are close |
| 960 | * together. |
| 961 | * |
| 962 | * So this code uses the perhaps slighly less optimal but more understandable |
| 963 | * and totally obvious approach of calculating color-scale. |
| 964 | * |
| 965 | * This algorithm depends on the precision in white-scale and that is |
| 966 | * (1/white-y), so we can immediately see that as white-y approaches 0 the |
| 967 | * accuracy inherent in the cHRM chunk drops off substantially. |
| 968 | * |
| 969 | * libpng arithmetic: a simple invertion of the above equations |
| 970 | * ------------------------------------------------------------ |
| 971 | * |
| 972 | * white_scale = 1/white-y |
| 973 | * white-X = white-x * white-scale |
| 974 | * white-Y = 1.0 |
| 975 | * white-Z = (1 - white-x - white-y) * white_scale |
| 976 | * |
| 977 | * white-C = red-C + green-C + blue-C |
| 978 | * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale |
| 979 | * |
| 980 | * This gives us three equations in (red-scale,green-scale,blue-scale) where |
| 981 | * all the coefficients are now known: |
| 982 | * |
| 983 | * red-x*red-scale + green-x*green-scale + blue-x*blue-scale |
| 984 | * = white-x/white-y |
| 985 | * red-y*red-scale + green-y*green-scale + blue-y*blue-scale = 1 |
| 986 | * red-z*red-scale + green-z*green-scale + blue-z*blue-scale |
| 987 | * = (1 - white-x - white-y)/white-y |
| 988 | * |
| 989 | * In the last equation color-z is (1 - color-x - color-y) so we can add all |
| 990 | * three equations together to get an alternative third: |
| 991 | * |
| 992 | * red-scale + green-scale + blue-scale = 1/white-y = white-scale |
| 993 | * |
| 994 | * So now we have a Cramer's rule solution where the determinants are just |
| 995 | * 3x3 - far more tractible. Unfortunately 3x3 determinants still involve |
| 996 | * multiplication of three coefficients so we can't guarantee to avoid |
| 997 | * overflow in the libpng fixed point representation. Using Cramer's rule in |
| 998 | * floating point is probably a good choice here, but it's not an option for |
| 999 | * fixed point. Instead proceed to simplify the first two equations by |
| 1000 | * eliminating what is likely to be the largest value, blue-scale: |
| 1001 | * |
| 1002 | * blue-scale = white-scale - red-scale - green-scale |
| 1003 | * |
| 1004 | * Hence: |
| 1005 | * |
| 1006 | * (red-x - blue-x)*red-scale + (green-x - blue-x)*green-scale = |
| 1007 | * (white-x - blue-x)*white-scale |
| 1008 | * |
| 1009 | * (red-y - blue-y)*red-scale + (green-y - blue-y)*green-scale = |
| 1010 | * 1 - blue-y*white-scale |
| 1011 | * |
| 1012 | * And now we can trivially solve for (red-scale,green-scale): |
| 1013 | * |
| 1014 | * green-scale = |
| 1015 | * (white-x - blue-x)*white-scale - (red-x - blue-x)*red-scale |
| 1016 | * ----------------------------------------------------------- |
| 1017 | * green-x - blue-x |
| 1018 | * |
| 1019 | * red-scale = |
| 1020 | * 1 - blue-y*white-scale - (green-y - blue-y) * green-scale |
| 1021 | * --------------------------------------------------------- |
| 1022 | * red-y - blue-y |
| 1023 | * |
| 1024 | * Hence: |
| 1025 | * |
| 1026 | * red-scale = |
| 1027 | * ( (green-x - blue-x) * (white-y - blue-y) - |
| 1028 | * (green-y - blue-y) * (white-x - blue-x) ) / white-y |
| 1029 | * ------------------------------------------------------------------------- |
| 1030 | * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x) |
| 1031 | * |
| 1032 | * green-scale = |
| 1033 | * ( (red-y - blue-y) * (white-x - blue-x) - |
| 1034 | * (red-x - blue-x) * (white-y - blue-y) ) / white-y |
| 1035 | * ------------------------------------------------------------------------- |
| 1036 | * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x) |
| 1037 | * |
| 1038 | * Accuracy: |
| 1039 | * The input values have 5 decimal digits of accuracy. The values are all in |
| 1040 | * the range 0 < value < 1, so simple products are in the same range but may |
| 1041 | * need up to 10 decimal digits to preserve the original precision and avoid |
| 1042 | * underflow. Because we are using a 32-bit signed representation we cannot |
| 1043 | * match this; the best is a little over 9 decimal digits, less than 10. |
| 1044 | * |
| 1045 | * The approach used here is to preserve the maximum precision within the |
| 1046 | * signed representation. Because the red-scale calculation above uses the |
| 1047 | * difference between two products of values that must be in the range -1..+1 |
| 1048 | * it is sufficient to divide the product by 7; ceil(100,000/32767*2). The |
| 1049 | * factor is irrelevant in the calculation because it is applied to both |
| 1050 | * numerator and denominator. |
| 1051 | * |
| 1052 | * Note that the values of the differences of the products of the |
| 1053 | * chromaticities in the above equations tend to be small, for example for |
| 1054 | * the sRGB chromaticities they are: |
| 1055 | * |
| 1056 | * red numerator: -0.04751 |
| 1057 | * green numerator: -0.08788 |
| 1058 | * denominator: -0.2241 (without white-y multiplication) |
| 1059 | * |
| 1060 | * The resultant Y coefficients from the chromaticities of some widely used |
| 1061 | * color space definitions are (to 15 decimal places): |
| 1062 | * |
| 1063 | * sRGB |
| 1064 | * 0.212639005871510 0.715168678767756 0.072192315360734 |
| 1065 | * Kodak ProPhoto |
| 1066 | * 0.288071128229293 0.711843217810102 0.000085653960605 |
| 1067 | * Adobe RGB |
| 1068 | * 0.297344975250536 0.627363566255466 0.075291458493998 |
| 1069 | * Adobe Wide Gamut RGB |
| 1070 | * 0.258728243040113 0.724682314948566 0.016589442011321 |
| 1071 | */ |
| 1072 | /* By the argument, above overflow should be impossible here. The return |
| 1073 | * value of 2 indicates an internal error to the caller. |
| 1074 | */ |
| 1075 | if (!png_muldiv(&left, xy.greenx-xy.bluex, xy.redy - xy.bluey, 7)) return 2; |
| 1076 | if (!png_muldiv(&right, xy.greeny-xy.bluey, xy.redx - xy.bluex, 7)) return 2; |
| 1077 | denominator = left - right; |
| 1078 | |
| 1079 | /* Now find the red numerator. */ |
| 1080 | if (!png_muldiv(&left, xy.greenx-xy.bluex, xy.whitey-xy.bluey, 7)) return 2; |
| 1081 | if (!png_muldiv(&right, xy.greeny-xy.bluey, xy.whitex-xy.bluex, 7)) return 2; |
| 1082 | |
| 1083 | /* Overflow is possible here and it indicates an extreme set of PNG cHRM |
| 1084 | * chunk values. This calculation actually returns the reciprocal of the |
| 1085 | * scale value because this allows us to delay the multiplication of white-y |
| 1086 | * into the denominator, which tends to produce a small number. |
| 1087 | */ |
| 1088 | if (!png_muldiv(&red_inverse, xy.whitey, denominator, left-right) || |
| 1089 | red_inverse <= xy.whitey /* r+g+b scales = white scale */) |
| 1090 | return 1; |
| 1091 | |
| 1092 | /* Similarly for green_inverse: */ |
| 1093 | if (!png_muldiv(&left, xy.redy-xy.bluey, xy.whitex-xy.bluex, 7)) return 2; |
| 1094 | if (!png_muldiv(&right, xy.redx-xy.bluex, xy.whitey-xy.bluey, 7)) return 2; |
| 1095 | if (!png_muldiv(&green_inverse, xy.whitey, denominator, left-right) || |
| 1096 | green_inverse <= xy.whitey) |
| 1097 | return 1; |
| 1098 | |
| 1099 | /* And the blue scale, the checks above guarantee this can't overflow but it |
| 1100 | * can still produce 0 for extreme cHRM values. |
| 1101 | */ |
| 1102 | blue_scale = png_reciprocal(xy.whitey) - png_reciprocal(red_inverse) - |
| 1103 | png_reciprocal(green_inverse); |
| 1104 | if (blue_scale <= 0) return 1; |
| 1105 | |
| 1106 | |
| 1107 | /* And fill in the png_XYZ: */ |
| 1108 | if (!png_muldiv(&XYZ->redX, xy.redx, PNG_FP_1, red_inverse)) return 1; |
| 1109 | if (!png_muldiv(&XYZ->redY, xy.redy, PNG_FP_1, red_inverse)) return 1; |
| 1110 | if (!png_muldiv(&XYZ->redZ, PNG_FP_1 - xy.redx - xy.redy, PNG_FP_1, |
| 1111 | red_inverse)) |
| 1112 | return 1; |
| 1113 | |
| 1114 | if (!png_muldiv(&XYZ->greenX, xy.greenx, PNG_FP_1, green_inverse)) return 1; |
| 1115 | if (!png_muldiv(&XYZ->greenY, xy.greeny, PNG_FP_1, green_inverse)) return 1; |
| 1116 | if (!png_muldiv(&XYZ->greenZ, PNG_FP_1 - xy.greenx - xy.greeny, PNG_FP_1, |
| 1117 | green_inverse)) |
| 1118 | return 1; |
| 1119 | |
| 1120 | if (!png_muldiv(&XYZ->blueX, xy.bluex, blue_scale, PNG_FP_1)) return 1; |
| 1121 | if (!png_muldiv(&XYZ->blueY, xy.bluey, blue_scale, PNG_FP_1)) return 1; |
| 1122 | if (!png_muldiv(&XYZ->blueZ, PNG_FP_1 - xy.bluex - xy.bluey, blue_scale, |
| 1123 | PNG_FP_1)) |
| 1124 | return 1; |
| 1125 | |
| 1126 | return 0; /*success*/ |
| 1127 | } |
| 1128 | |
| 1129 | int png_XYZ_from_xy_checked(png_structp png_ptr, png_XYZ *XYZ, png_xy xy) |
| 1130 | { |
| 1131 | switch (png_XYZ_from_xy(XYZ, xy)) |
| 1132 | { |
| 1133 | case 0: /* success */ |
| 1134 | return 1; |
| 1135 | |
| 1136 | case 1: |
| 1137 | /* The chunk may be technically valid, but we got png_fixed_point |
| 1138 | * overflow while trying to get XYZ values out of it. This is |
| 1139 | * entirely benign - the cHRM chunk is pretty extreme. |
| 1140 | */ |
Glenn Randers-Pehrson | b6e4d58 | 2011-09-08 12:25:52 -0500 | [diff] [blame] | 1141 | png_warning(png_ptr, |
John Bowler | 736f40f | 2011-08-25 16:19:44 -0500 | [diff] [blame] | 1142 | "extreme cHRM chunk cannot be converted to tristimulus values"); |
| 1143 | break; |
| 1144 | |
| 1145 | default: |
| 1146 | /* libpng is broken; this should be a warning but if it happens we |
| 1147 | * want error reports so for the moment it is an error. |
| 1148 | */ |
| 1149 | png_error(png_ptr, "internal error in png_XYZ_from_xy"); |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | /* ERROR RETURN */ |
| 1154 | return 0; |
| 1155 | } |
| 1156 | #endif |
| 1157 | |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1158 | void /* PRIVATE */ |
| 1159 | png_check_IHDR(png_structp png_ptr, |
| 1160 | png_uint_32 width, png_uint_32 height, int bit_depth, |
| 1161 | int color_type, int interlace_type, int compression_type, |
| 1162 | int filter_type) |
| 1163 | { |
| 1164 | int error = 0; |
| 1165 | |
| 1166 | /* Check for width and height valid values */ |
| 1167 | if (width == 0) |
| 1168 | { |
| 1169 | png_warning(png_ptr, "Image width is zero in IHDR"); |
| 1170 | error = 1; |
| 1171 | } |
| 1172 | |
| 1173 | if (height == 0) |
| 1174 | { |
| 1175 | png_warning(png_ptr, "Image height is zero in IHDR"); |
| 1176 | error = 1; |
| 1177 | } |
| 1178 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1179 | # ifdef PNG_SET_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | b704036 | 2011-06-11 14:20:22 -0500 | [diff] [blame] | 1180 | if (width > png_ptr->user_width_max) |
Glenn Randers-Pehrson | f24daf2 | 2010-05-06 09:44:04 -0500 | [diff] [blame] | 1181 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1182 | # else |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 1183 | if (width > PNG_USER_WIDTH_MAX) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1184 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1185 | { |
| 1186 | png_warning(png_ptr, "Image width exceeds user limit in IHDR"); |
| 1187 | error = 1; |
| 1188 | } |
| 1189 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1190 | # ifdef PNG_SET_USER_LIMITS_SUPPORTED |
Glenn Randers-Pehrson | b704036 | 2011-06-11 14:20:22 -0500 | [diff] [blame] | 1191 | if (height > png_ptr->user_height_max) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1192 | # else |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1193 | if (height > PNG_USER_HEIGHT_MAX) |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1194 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1195 | { |
| 1196 | png_warning(png_ptr, "Image height exceeds user limit in IHDR"); |
| 1197 | error = 1; |
| 1198 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1199 | |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 1200 | if (width > PNG_UINT_31_MAX) |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1201 | { |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 1202 | png_warning(png_ptr, "Invalid image width in IHDR"); |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1203 | error = 1; |
| 1204 | } |
| 1205 | |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame] | 1206 | if (height > PNG_UINT_31_MAX) |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1207 | { |
Glenn Randers-Pehrson | dd66f3e | 2009-09-30 14:58:28 -0500 | [diff] [blame] | 1208 | png_warning(png_ptr, "Invalid image height in IHDR"); |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1209 | error = 1; |
| 1210 | } |
| 1211 | |
Glenn Randers-Pehrson | bc363ec | 2010-10-12 21:17:00 -0500 | [diff] [blame] | 1212 | if (width > (PNG_UINT_32_MAX |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1213 | >> 3) /* 8-byte RGBA pixels */ |
Glenn Randers-Pehrson | c508081 | 2010-10-23 08:26:26 -0500 | [diff] [blame] | 1214 | - 48 /* bigrowbuf hack */ |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1215 | - 1 /* filter byte */ |
| 1216 | - 7*8 /* rounding of width to multiple of 8 pixels */ |
| 1217 | - 8) /* extra max_pixel_depth pad */ |
| 1218 | png_warning(png_ptr, "Width is too large for libpng to process pixels"); |
| 1219 | |
| 1220 | /* Check other values */ |
| 1221 | if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && |
| 1222 | bit_depth != 8 && bit_depth != 16) |
| 1223 | { |
| 1224 | png_warning(png_ptr, "Invalid bit depth in IHDR"); |
| 1225 | error = 1; |
| 1226 | } |
| 1227 | |
| 1228 | if (color_type < 0 || color_type == 1 || |
| 1229 | color_type == 5 || color_type > 6) |
| 1230 | { |
| 1231 | png_warning(png_ptr, "Invalid color type in IHDR"); |
| 1232 | error = 1; |
| 1233 | } |
| 1234 | |
| 1235 | if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || |
| 1236 | ((color_type == PNG_COLOR_TYPE_RGB || |
| 1237 | color_type == PNG_COLOR_TYPE_GRAY_ALPHA || |
| 1238 | color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) |
| 1239 | { |
| 1240 | png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR"); |
| 1241 | error = 1; |
| 1242 | } |
| 1243 | |
| 1244 | if (interlace_type >= PNG_INTERLACE_LAST) |
| 1245 | { |
| 1246 | png_warning(png_ptr, "Unknown interlace method in IHDR"); |
| 1247 | error = 1; |
| 1248 | } |
| 1249 | |
| 1250 | if (compression_type != PNG_COMPRESSION_TYPE_BASE) |
| 1251 | { |
| 1252 | png_warning(png_ptr, "Unknown compression method in IHDR"); |
| 1253 | error = 1; |
| 1254 | } |
| 1255 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1256 | # ifdef PNG_MNG_FEATURES_SUPPORTED |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1257 | /* Accept filter_method 64 (intrapixel differencing) only if |
| 1258 | * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and |
| 1259 | * 2. Libpng did not read a PNG signature (this filter_method is only |
| 1260 | * used in PNG datastreams that are embedded in MNG datastreams) and |
| 1261 | * 3. The application called png_permit_mng_features with a mask that |
| 1262 | * included PNG_FLAG_MNG_FILTER_64 and |
| 1263 | * 4. The filter_method is 64 and |
| 1264 | * 5. The color_type is RGB or RGBA |
| 1265 | */ |
| 1266 | if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) && |
| 1267 | png_ptr->mng_features_permitted) |
| 1268 | png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); |
| 1269 | |
| 1270 | if (filter_type != PNG_FILTER_TYPE_BASE) |
| 1271 | { |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 1272 | if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1273 | (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && |
| 1274 | ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && |
| 1275 | (color_type == PNG_COLOR_TYPE_RGB || |
| 1276 | color_type == PNG_COLOR_TYPE_RGB_ALPHA))) |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 1277 | { |
| 1278 | png_warning(png_ptr, "Unknown filter method in IHDR"); |
| 1279 | error = 1; |
| 1280 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1281 | |
Glenn Randers-Pehrson | 7ec330d | 2009-09-25 11:45:42 -0500 | [diff] [blame] | 1282 | if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) |
| 1283 | { |
| 1284 | png_warning(png_ptr, "Invalid filter method in IHDR"); |
| 1285 | error = 1; |
| 1286 | } |
| 1287 | } |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1288 | |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1289 | # else |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1290 | if (filter_type != PNG_FILTER_TYPE_BASE) |
| 1291 | { |
| 1292 | png_warning(png_ptr, "Unknown filter method in IHDR"); |
| 1293 | error = 1; |
| 1294 | } |
Glenn Randers-Pehrson | 6f6a91a | 2010-03-06 13:54:59 -0600 | [diff] [blame] | 1295 | # endif |
Glenn Randers-Pehrson | 134bbe4 | 2009-09-24 18:10:49 -0500 | [diff] [blame] | 1296 | |
| 1297 | if (error == 1) |
| 1298 | png_error(png_ptr, "Invalid IHDR data"); |
| 1299 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1300 | |
| 1301 | #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) |
| 1302 | /* ASCII to fp functions */ |
| 1303 | /* Check an ASCII formated floating point value, see the more detailed |
| 1304 | * comments in pngpriv.h |
| 1305 | */ |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1306 | /* The following is used internally to preserve the sticky flags */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1307 | #define png_fp_add(state, flags) ((state) |= (flags)) |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1308 | #define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1309 | |
| 1310 | int /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1311 | 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] | 1312 | png_size_tp whereami) |
| 1313 | { |
| 1314 | int state = *statep; |
| 1315 | png_size_t i = *whereami; |
| 1316 | |
| 1317 | while (i < size) |
| 1318 | { |
| 1319 | int type; |
| 1320 | /* First find the type of the next character */ |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1321 | switch (string[i]) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1322 | { |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1323 | case 43: type = PNG_FP_SAW_SIGN; break; |
| 1324 | case 45: type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break; |
| 1325 | case 46: type = PNG_FP_SAW_DOT; break; |
| 1326 | case 48: type = PNG_FP_SAW_DIGIT; break; |
| 1327 | case 49: case 50: case 51: case 52: |
| 1328 | case 53: case 54: case 55: case 56: |
| 1329 | case 57: type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break; |
| 1330 | case 69: |
| 1331 | case 101: type = PNG_FP_SAW_E; break; |
| 1332 | default: goto PNG_FP_End; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | /* Now deal with this type according to the current |
| 1336 | * state, the type is arranged to not overlap the |
| 1337 | * bits of the PNG_FP_STATE. |
| 1338 | */ |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1339 | switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1340 | { |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1341 | case PNG_FP_INTEGER + PNG_FP_SAW_SIGN: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1342 | if (state & PNG_FP_SAW_ANY) |
| 1343 | goto PNG_FP_End; /* not a part of the number */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1344 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1345 | png_fp_add(state, type); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1346 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1347 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1348 | case PNG_FP_INTEGER + PNG_FP_SAW_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1349 | /* Ok as trailer, ok as lead of fraction. */ |
| 1350 | if (state & PNG_FP_SAW_DOT) /* two dots */ |
| 1351 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1352 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1353 | else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */ |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1354 | png_fp_add(state, type); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1355 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1356 | else |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1357 | png_fp_set(state, PNG_FP_FRACTION | type); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1358 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1359 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1360 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1361 | case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1362 | if (state & PNG_FP_SAW_DOT) /* delayed fraction */ |
| 1363 | png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1364 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1365 | png_fp_add(state, type | PNG_FP_WAS_VALID); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1366 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1367 | break; |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1368 | |
| 1369 | case PNG_FP_INTEGER + PNG_FP_SAW_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1370 | if ((state & PNG_FP_SAW_DIGIT) == 0) |
| 1371 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1372 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1373 | png_fp_set(state, PNG_FP_EXPONENT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1374 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1375 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1376 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1377 | /* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN: |
| 1378 | goto PNG_FP_End; ** no sign in fraction */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1379 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1380 | /* case PNG_FP_FRACTION + PNG_FP_SAW_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1381 | goto PNG_FP_End; ** Because SAW_DOT is always set */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1382 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1383 | case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT: |
| 1384 | png_fp_add(state, type | PNG_FP_WAS_VALID); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1385 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1386 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1387 | case PNG_FP_FRACTION + PNG_FP_SAW_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1388 | /* This is correct because the trailing '.' on an |
| 1389 | * integer is handled above - so we can only get here |
| 1390 | * with the sequence ".E" (with no preceding digits). |
| 1391 | */ |
| 1392 | if ((state & PNG_FP_SAW_DIGIT) == 0) |
| 1393 | goto PNG_FP_End; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1394 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1395 | png_fp_set(state, PNG_FP_EXPONENT); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1396 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1397 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1398 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1399 | case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1400 | if (state & PNG_FP_SAW_ANY) |
| 1401 | goto PNG_FP_End; /* not a part of the number */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1402 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1403 | png_fp_add(state, PNG_FP_SAW_SIGN); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1404 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1405 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1406 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1407 | /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1408 | goto PNG_FP_End; */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1409 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1410 | case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT: |
| 1411 | 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] | 1412 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1413 | break; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1414 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1415 | /* case PNG_FP_EXPONEXT + PNG_FP_SAW_E: |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1416 | goto PNG_FP_End; */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1417 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1418 | default: goto PNG_FP_End; /* I.e. break 2 */ |
| 1419 | } |
| 1420 | |
| 1421 | /* The character seems ok, continue. */ |
| 1422 | ++i; |
| 1423 | } |
| 1424 | |
| 1425 | PNG_FP_End: |
| 1426 | /* Here at the end, update the state and return the correct |
| 1427 | * return code. |
| 1428 | */ |
| 1429 | *statep = state; |
| 1430 | *whereami = i; |
| 1431 | |
| 1432 | return (state & PNG_FP_SAW_DIGIT) != 0; |
| 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | /* The same but for a complete string. */ |
| 1437 | int |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1438 | 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] | 1439 | { |
| 1440 | int state=0; |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1441 | png_size_t char_index=0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1442 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 1443 | if (png_check_fp_number(string, size, &state, &char_index) && |
| 1444 | (char_index == size || string[char_index] == 0)) |
| 1445 | return state /* must be non-zero - see above */; |
| 1446 | |
| 1447 | return 0; /* i.e. fail */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1448 | } |
| 1449 | #endif /* pCAL or sCAL */ |
| 1450 | |
| 1451 | #ifdef PNG_READ_sCAL_SUPPORTED |
| 1452 | # ifdef PNG_FLOATING_POINT_SUPPORTED |
| 1453 | /* Utility used below - a simple accurate power of ten from an integral |
| 1454 | * exponent. |
| 1455 | */ |
| 1456 | static double |
| 1457 | png_pow10(int power) |
| 1458 | { |
| 1459 | int recip = 0; |
| 1460 | double d = 1; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1461 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1462 | /* Handle negative exponent with a reciprocal at the end because |
| 1463 | * 10 is exact whereas .1 is inexact in base 2 |
| 1464 | */ |
| 1465 | if (power < 0) |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1466 | { |
| 1467 | if (power < DBL_MIN_10_EXP) return 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1468 | recip = 1, power = -power; |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1469 | } |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1470 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1471 | if (power > 0) |
| 1472 | { |
| 1473 | /* Decompose power bitwise. */ |
| 1474 | double mult = 10; |
| 1475 | do |
| 1476 | { |
| 1477 | if (power & 1) d *= mult; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1478 | mult *= mult; |
| 1479 | power >>= 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1480 | } |
| 1481 | while (power > 0); |
| 1482 | |
| 1483 | if (recip) d = 1/d; |
| 1484 | } |
| 1485 | /* else power is 0 and d is 1 */ |
| 1486 | |
| 1487 | return d; |
| 1488 | } |
| 1489 | |
| 1490 | /* Function to format a floating point value in ASCII with a given |
| 1491 | * precision. |
| 1492 | */ |
| 1493 | void /* PRIVATE */ |
| 1494 | 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] | 1495 | double fp, unsigned int precision) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1496 | { |
| 1497 | /* We use standard functions from math.h, but not printf because |
| 1498 | * that would require stdio. The caller must supply a buffer of |
| 1499 | * sufficient size or we will png_error. The tests on size and |
| 1500 | * the space in ascii[] consumed are indicated below. |
| 1501 | */ |
| 1502 | if (precision < 1) |
| 1503 | precision = DBL_DIG; |
| 1504 | |
| 1505 | /* Enforce the limit of the implementation precision too. */ |
| 1506 | if (precision > DBL_DIG+1) |
| 1507 | precision = DBL_DIG+1; |
| 1508 | |
| 1509 | /* Basic sanity checks */ |
| 1510 | if (size >= precision+5) /* See the requirements below. */ |
| 1511 | { |
| 1512 | if (fp < 0) |
| 1513 | { |
| 1514 | fp = -fp; |
Glenn Randers-Pehrson | b75b241 | 2011-04-16 19:35:05 -0500 | [diff] [blame] | 1515 | *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1516 | --size; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | if (fp >= DBL_MIN && fp <= DBL_MAX) |
| 1520 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1521 | int exp_b10; /* A base 10 exponent */ |
| 1522 | double base; /* 10^exp_b10 */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1523 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1524 | /* First extract a base 10 exponent of the number, |
| 1525 | * the calculation below rounds down when converting |
| 1526 | * from base 2 to base 10 (multiply by log10(2) - |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1527 | * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1528 | * be increased. Note that the arithmetic shift |
| 1529 | * performs a floor() unlike C arithmetic - using a |
| 1530 | * C multiply would break the following for negative |
| 1531 | * exponents. |
| 1532 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1533 | (void)frexp(fp, &exp_b10); /* exponent to base 2 */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1534 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1535 | exp_b10 = (exp_b10 * 77) >> 8; /* <= exponent to base 10 */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1536 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1537 | /* Avoid underflow here. */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1538 | base = png_pow10(exp_b10); /* May underflow */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1539 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1540 | while (base < DBL_MIN || base < fp) |
| 1541 | { |
| 1542 | /* And this may overflow. */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1543 | double test = png_pow10(exp_b10+1); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1544 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1545 | if (test <= DBL_MAX) |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1546 | ++exp_b10, base = test; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1547 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1548 | else |
| 1549 | break; |
| 1550 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1551 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1552 | /* Normalize fp and correct exp_b10, after this fp is in the |
| 1553 | * range [.1,1) and exp_b10 is both the exponent and the digit |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1554 | * *before* which the decimal point should be inserted |
| 1555 | * (starting with 0 for the first digit). Note that this |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1556 | * works even if 10^exp_b10 is out of range because of the |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1557 | * test on DBL_MAX above. |
| 1558 | */ |
| 1559 | fp /= base; |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1560 | while (fp >= 1) fp /= 10, ++exp_b10; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1561 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1562 | /* Because of the code above fp may, at this point, be |
| 1563 | * less than .1, this is ok because the code below can |
| 1564 | * handle the leading zeros this generates, so no attempt |
| 1565 | * is made to correct that here. |
| 1566 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1567 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1568 | { |
| 1569 | int czero, clead, cdigits; |
| 1570 | char exponent[10]; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1571 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1572 | /* Allow up to two leading zeros - this will not lengthen |
| 1573 | * the number compared to using E-n. |
| 1574 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1575 | if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1576 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1577 | czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */ |
| 1578 | exp_b10 = 0; /* Dot added below before first output. */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1579 | } |
| 1580 | else |
| 1581 | czero = 0; /* No zeros to add */ |
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 | /* Generate the digit list, stripping trailing zeros and |
| 1584 | * inserting a '.' before a digit if the exponent is 0. |
| 1585 | */ |
| 1586 | clead = czero; /* Count of leading zeros */ |
| 1587 | cdigits = 0; /* Count of digits in list. */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1588 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1589 | do |
| 1590 | { |
| 1591 | double d; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1592 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1593 | fp *= 10; |
| 1594 | /* Use modf here, not floor and subtract, so that |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1595 | * the separation is done in one step. At the end |
| 1596 | * of the loop don't break the number into parts so |
| 1597 | * that the final digit is rounded. |
| 1598 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1599 | if (cdigits+czero-clead+1 < (int)precision) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1600 | fp = modf(fp, &d); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1601 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1602 | else |
| 1603 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1604 | d = floor(fp + .5); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1605 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1606 | if (d > 9) |
| 1607 | { |
| 1608 | /* Rounding up to 10, handle that here. */ |
| 1609 | if (czero > 0) |
| 1610 | { |
| 1611 | --czero, d = 1; |
| 1612 | if (cdigits == 0) --clead; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1613 | } |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1614 | else |
| 1615 | { |
| 1616 | while (cdigits > 0 && d > 9) |
| 1617 | { |
| 1618 | int ch = *--ascii; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1619 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1620 | if (exp_b10 != (-1)) |
| 1621 | ++exp_b10; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1622 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1623 | else if (ch == 46) |
| 1624 | { |
| 1625 | ch = *--ascii, ++size; |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1626 | /* Advance exp_b10 to '1', so that the |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1627 | * decimal point happens after the |
| 1628 | * previous digit. |
| 1629 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1630 | exp_b10 = 1; |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1631 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1632 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1633 | --cdigits; |
| 1634 | d = ch - 47; /* I.e. 1+(ch-48) */ |
| 1635 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1636 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1637 | /* Did we reach the beginning? If so adjust the |
| 1638 | * exponent but take into account the leading |
| 1639 | * decimal point. |
| 1640 | */ |
| 1641 | if (d > 9) /* cdigits == 0 */ |
| 1642 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1643 | if (exp_b10 == (-1)) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1644 | { |
| 1645 | /* Leading decimal point (plus zeros?), if |
| 1646 | * we lose the decimal point here it must |
| 1647 | * be reentered below. |
| 1648 | */ |
| 1649 | int ch = *--ascii; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1650 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1651 | if (ch == 46) |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1652 | ++size, exp_b10 = 1; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1653 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1654 | /* Else lost a leading zero, so 'exp_b10' is |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1655 | * still ok at (-1) |
| 1656 | */ |
| 1657 | } |
| 1658 | else |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1659 | ++exp_b10; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1660 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1661 | /* In all cases we output a '1' */ |
| 1662 | d = 1; |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | fp = 0; /* Guarantees termination below. */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1667 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1668 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1669 | if (d == 0) |
| 1670 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1671 | ++czero; |
| 1672 | if (cdigits == 0) ++clead; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1673 | } |
| 1674 | else |
| 1675 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1676 | /* Included embedded zeros in the digit count. */ |
| 1677 | cdigits += czero - clead; |
| 1678 | clead = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1679 | |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1680 | while (czero > 0) |
| 1681 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1682 | /* exp_b10 == (-1) means we just output the decimal |
| 1683 | * place - after the DP don't adjust 'exp_b10' any |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1684 | * more! |
| 1685 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1686 | if (exp_b10 != (-1)) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1687 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1688 | if (exp_b10 == 0) *ascii++ = 46, --size; |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1689 | /* PLUS 1: TOTAL 4 */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1690 | --exp_b10; |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1691 | } |
| 1692 | *ascii++ = 48, --czero; |
| 1693 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1694 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1695 | if (exp_b10 != (-1)) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1696 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1697 | if (exp_b10 == 0) *ascii++ = 46, --size; /* counted |
| 1698 | above */ |
| 1699 | --exp_b10; |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1700 | } |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1701 | *ascii++ = (char)(48 + (int)d), ++cdigits; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1702 | } |
| 1703 | } |
| 1704 | while (cdigits+czero-clead < (int)precision && fp > DBL_MIN); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1705 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1706 | /* The total output count (max) is now 4+precision */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1707 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1708 | /* Check for an exponent, if we don't need one we are |
| 1709 | * done and just need to terminate the string. At |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1710 | * this point exp_b10==(-1) is effectively if flag - it got |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1711 | * to '-1' because of the decrement after outputing |
| 1712 | * the decimal point above (the exponent required is |
| 1713 | * *not* -1!) |
| 1714 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1715 | if (exp_b10 >= (-1) && exp_b10 <= 2) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1716 | { |
| 1717 | /* The following only happens if we didn't output the |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1718 | * leading zeros above for negative exponent, so this |
| 1719 | * doest add to the digit requirement. Note that the |
| 1720 | * two zeros here can only be output if the two leading |
| 1721 | * zeros were *not* output, so this doesn't increase |
| 1722 | * the output count. |
| 1723 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1724 | while (--exp_b10 >= 0) *ascii++ = 48; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1725 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1726 | *ascii = 0; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1727 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1728 | /* Total buffer requirement (including the '\0') is |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1729 | * 5+precision - see check at the start. |
| 1730 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1731 | return; |
| 1732 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1733 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1734 | /* Here if an exponent is required, adjust size for |
| 1735 | * the digits we output but did not count. The total |
| 1736 | * digit output here so far is at most 1+precision - no |
| 1737 | * decimal point and no leading or trailing zeros have |
| 1738 | * been output. |
| 1739 | */ |
| 1740 | size -= cdigits; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1741 | |
Glenn Randers-Pehrson | b75b241 | 2011-04-16 19:35:05 -0500 | [diff] [blame] | 1742 | *ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1743 | |
John Bowler | 7515612 | 2011-09-09 17:21:44 -0500 | [diff] [blame] | 1744 | /* The following use of an unsigned temporary avoids ambiguities in |
| 1745 | * the signed arithmetic on exp_b10 and permits GCC at least to do |
| 1746 | * better optimization. |
| 1747 | */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1748 | { |
John Bowler | 7515612 | 2011-09-09 17:21:44 -0500 | [diff] [blame] | 1749 | unsigned int uexp_b10; |
| 1750 | |
| 1751 | if (exp_b10 < 0) |
| 1752 | { |
| 1753 | *ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */ |
| 1754 | uexp_b10 = -exp_b10; |
| 1755 | } |
| 1756 | |
| 1757 | else |
| 1758 | uexp_b10 = exp_b10; |
| 1759 | |
| 1760 | cdigits = 0; |
| 1761 | |
| 1762 | while (uexp_b10 > 0) |
| 1763 | { |
| 1764 | exponent[cdigits++] = (char)(48 + uexp_b10 % 10); |
| 1765 | uexp_b10 /= 10; |
| 1766 | } |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1767 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1768 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1769 | /* Need another size check here for the exponent digits, so |
| 1770 | * this need not be considered above. |
| 1771 | */ |
| 1772 | if ((int)size > cdigits) |
| 1773 | { |
| 1774 | while (cdigits > 0) *ascii++ = exponent[--cdigits]; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1775 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1776 | *ascii = 0; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1777 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1781 | } |
| 1782 | else if (!(fp >= DBL_MIN)) |
| 1783 | { |
| 1784 | *ascii++ = 48; /* '0' */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1785 | *ascii = 0; |
| 1786 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1787 | } |
| 1788 | else |
| 1789 | { |
| 1790 | *ascii++ = 105; /* 'i' */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1791 | *ascii++ = 110; /* 'n' */ |
| 1792 | *ascii++ = 102; /* 'f' */ |
| 1793 | *ascii = 0; |
| 1794 | return; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | /* Here on buffer too small. */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1799 | png_error(png_ptr, "ASCII conversion buffer too small"); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | # endif /* FLOATING_POINT */ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1803 | |
| 1804 | # ifdef PNG_FIXED_POINT_SUPPORTED |
| 1805 | /* Function to format a fixed point value in ASCII. |
| 1806 | */ |
| 1807 | void /* PRIVATE */ |
| 1808 | png_ascii_from_fixed(png_structp png_ptr, png_charp ascii, png_size_t size, |
| 1809 | png_fixed_point fp) |
| 1810 | { |
| 1811 | /* Require space for 10 decimal digits, a decimal point, a minus sign and a |
| 1812 | * trailing \0, 13 characters: |
| 1813 | */ |
| 1814 | if (size > 12) |
| 1815 | { |
| 1816 | png_uint_32 num; |
| 1817 | |
| 1818 | /* Avoid overflow here on the minimum integer. */ |
| 1819 | if (fp < 0) |
| 1820 | *ascii++ = 45, --size, num = -fp; |
| 1821 | else |
| 1822 | num = fp; |
| 1823 | |
John Bowler | 7515612 | 2011-09-09 17:21:44 -0500 | [diff] [blame] | 1824 | if (num <= 0x80000000) /* else overflowed */ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1825 | { |
Glenn Randers-Pehrson | b75b241 | 2011-04-16 19:35:05 -0500 | [diff] [blame] | 1826 | unsigned int ndigits = 0, first = 16 /* flag value */; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1827 | char digits[10]; |
| 1828 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1829 | while (num) |
| 1830 | { |
| 1831 | /* Split the low digit off num: */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1832 | unsigned int tmp = num/10; |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1833 | num -= tmp*10; |
| 1834 | digits[ndigits++] = (char)(48 + num); |
| 1835 | /* Record the first non-zero digit, note that this is a number |
| 1836 | * starting at 1, it's not actually the array index. |
| 1837 | */ |
| 1838 | if (first == 16 && num > 0) |
| 1839 | first = ndigits; |
| 1840 | num = tmp; |
| 1841 | } |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1842 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1843 | if (ndigits > 0) |
| 1844 | { |
| 1845 | while (ndigits > 5) *ascii++ = digits[--ndigits]; |
| 1846 | /* The remaining digits are fractional digits, ndigits is '5' or |
| 1847 | * smaller at this point. It is certainly not zero. Check for a |
| 1848 | * non-zero fractional digit: |
| 1849 | */ |
| 1850 | if (first <= 5) |
| 1851 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 1852 | unsigned int i; |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1853 | *ascii++ = 46; /* decimal point */ |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1854 | /* ndigits may be <5 for small numbers, output leading zeros |
| 1855 | * then ndigits digits to first: |
| 1856 | */ |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1857 | i = 5; |
| 1858 | while (ndigits < i) *ascii++ = 48, --i; |
| 1859 | while (ndigits >= first) *ascii++ = digits[--ndigits]; |
| 1860 | /* Don't output the trailing zeros! */ |
| 1861 | } |
| 1862 | } |
| 1863 | else |
| 1864 | *ascii++ = 48; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1865 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 1866 | /* And null terminate the string: */ |
| 1867 | *ascii = 0; |
| 1868 | return; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /* Here on buffer too small. */ |
| 1873 | png_error(png_ptr, "ASCII conversion buffer too small"); |
| 1874 | } |
| 1875 | # endif /* FIXED_POINT */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1876 | #endif /* READ_SCAL */ |
| 1877 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1878 | #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1879 | !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) |
| 1880 | png_fixed_point |
| 1881 | png_fixed(png_structp png_ptr, double fp, png_const_charp text) |
| 1882 | { |
| 1883 | double r = floor(100000 * fp + .5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1884 | |
Glenn Randers-Pehrson | 77c3bf1 | 2011-01-20 15:56:31 -0600 | [diff] [blame] | 1885 | if (r > 2147483647. || r < -2147483648.) |
| 1886 | png_fixed_error(png_ptr, text); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1887 | |
Glenn Randers-Pehrson | 77c3bf1 | 2011-01-20 15:56:31 -0600 | [diff] [blame] | 1888 | return (png_fixed_point)r; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1889 | } |
| 1890 | #endif |
| 1891 | |
Glenn Randers-Pehrson | 77396b6 | 2010-08-02 08:00:10 -0500 | [diff] [blame] | 1892 | #if defined(PNG_READ_GAMMA_SUPPORTED) || \ |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 1893 | defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG__READ_pHYs_SUPPORTED) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1894 | /* muldiv functions */ |
| 1895 | /* This API takes signed arguments and rounds the result to the nearest |
| 1896 | * integer (or, for a fixed point number - the standard argument - to |
| 1897 | * the nearest .00001). Overflow and divide by zero are signalled in |
| 1898 | * the result, a boolean - true on success, false on overflow. |
| 1899 | */ |
| 1900 | int |
| 1901 | png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1902 | png_int_32 divisor) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1903 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1904 | /* Return a * times / divisor, rounded. */ |
| 1905 | if (divisor != 0) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1906 | { |
| 1907 | if (a == 0 || times == 0) |
| 1908 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1909 | *res = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1910 | return 1; |
| 1911 | } |
| 1912 | else |
| 1913 | { |
| 1914 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1915 | double r = a; |
| 1916 | r *= times; |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1917 | r /= divisor; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1918 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1919 | |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 1920 | /* A png_fixed_point is a 32-bit integer. */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1921 | if (r <= 2147483647. && r >= -2147483648.) |
| 1922 | { |
| 1923 | *res = (png_fixed_point)r; |
| 1924 | return 1; |
| 1925 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1926 | #else |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1927 | int negative = 0; |
| 1928 | png_uint_32 A, T, D; |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1929 | png_uint_32 s16, s32, s00; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1930 | |
| 1931 | if (a < 0) |
| 1932 | negative = 1, A = -a; |
| 1933 | else |
| 1934 | A = a; |
| 1935 | |
| 1936 | if (times < 0) |
| 1937 | negative = !negative, T = -times; |
| 1938 | else |
| 1939 | T = times; |
| 1940 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1941 | if (divisor < 0) |
| 1942 | negative = !negative, D = -divisor; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1943 | else |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 1944 | D = divisor; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1945 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1946 | /* Following can't overflow because the arguments only |
| 1947 | * have 31 bits each, however the result may be 32 bits. |
| 1948 | */ |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1949 | s16 = (A >> 16) * (T & 0xffff) + |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1950 | (A & 0xffff) * (T >> 16); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1951 | /* Can't overflow because the a*times bit is only 30 |
| 1952 | * bits at most. |
| 1953 | */ |
Glenn Randers-Pehrson | 3b5d695 | 2010-08-19 08:06:12 -0500 | [diff] [blame] | 1954 | s32 = (A >> 16) * (T >> 16) + (s16 >> 16); |
| 1955 | s00 = (A & 0xffff) * (T & 0xffff); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1956 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1957 | s16 = (s16 & 0xffff) << 16; |
| 1958 | s00 += s16; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1959 | |
| 1960 | if (s00 < s16) |
| 1961 | ++s32; /* carry */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1962 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1963 | if (s32 < D) /* else overflow */ |
| 1964 | { |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 1965 | /* s32.s00 is now the 64-bit product, do a standard |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1966 | * division, we know that s32 < D, so the maximum |
| 1967 | * required shift is 31. |
| 1968 | */ |
| 1969 | int bitshift = 32; |
| 1970 | png_fixed_point result = 0; /* NOTE: signed */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1971 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1972 | while (--bitshift >= 0) |
| 1973 | { |
| 1974 | png_uint_32 d32, d00; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1975 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1976 | if (bitshift > 0) |
| 1977 | d32 = D >> (32-bitshift), d00 = D << bitshift; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1978 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1979 | else |
| 1980 | d32 = 0, d00 = D; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1981 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1982 | if (s32 > d32) |
| 1983 | { |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 1984 | if (s00 < d00) --s32; /* carry */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1985 | s32 -= d32, s00 -= d00, result += 1<<bitshift; |
| 1986 | } |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1987 | |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 1988 | else |
| 1989 | if (s32 == d32 && s00 >= d00) |
| 1990 | s32 = 0, s00 -= d00, result += 1<<bitshift; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1991 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1992 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 1993 | /* Handle the rounding. */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1994 | if (s00 >= (D >> 1)) |
| 1995 | ++result; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1996 | |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 1997 | if (negative) |
| 1998 | result = -result; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 1999 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2000 | /* Check for overflow. */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2001 | if ((negative && result <= 0) || (!negative && result >= 0)) |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2002 | { |
| 2003 | *res = result; |
| 2004 | return 1; |
| 2005 | } |
| 2006 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2007 | #endif |
| 2008 | } |
| 2009 | } |
| 2010 | |
| 2011 | return 0; |
| 2012 | } |
| 2013 | #endif /* READ_GAMMA || INCH_CONVERSIONS */ |
| 2014 | |
| 2015 | #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) |
| 2016 | /* The following is for when the caller doesn't much care about the |
| 2017 | * result. |
| 2018 | */ |
| 2019 | png_fixed_point |
| 2020 | png_muldiv_warn(png_structp png_ptr, png_fixed_point a, png_int_32 times, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2021 | png_int_32 divisor) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2022 | { |
| 2023 | png_fixed_point result; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2024 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2025 | if (png_muldiv(&result, a, times, divisor)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2026 | return result; |
| 2027 | |
| 2028 | png_warning(png_ptr, "fixed point overflow ignored"); |
| 2029 | return 0; |
| 2030 | } |
| 2031 | #endif |
| 2032 | |
| 2033 | #ifdef PNG_READ_GAMMA_SUPPORTED /* more fixed point functions for gammma */ |
| 2034 | /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ |
| 2035 | png_fixed_point |
| 2036 | png_reciprocal(png_fixed_point a) |
| 2037 | { |
| 2038 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 2039 | double r = floor(1E10/a+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2040 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2041 | if (r <= 2147483647. && r >= -2147483648.) |
| 2042 | return (png_fixed_point)r; |
| 2043 | #else |
| 2044 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2045 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2046 | if (png_muldiv(&res, 100000, 100000, a)) |
| 2047 | return res; |
| 2048 | #endif |
| 2049 | |
| 2050 | return 0; /* error/overflow */ |
| 2051 | } |
| 2052 | |
| 2053 | /* A local convenience routine. */ |
| 2054 | static png_fixed_point |
| 2055 | png_product2(png_fixed_point a, png_fixed_point b) |
| 2056 | { |
Glenn Randers-Pehrson | 9c69091 | 2010-08-27 11:39:38 -0500 | [diff] [blame] | 2057 | /* 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] | 2058 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 2059 | double r = a * 1E-5; |
| 2060 | r *= b; |
| 2061 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2062 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2063 | if (r <= 2147483647. && r >= -2147483648.) |
| 2064 | return (png_fixed_point)r; |
| 2065 | #else |
| 2066 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2067 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2068 | if (png_muldiv(&res, a, b, 100000)) |
| 2069 | return res; |
| 2070 | #endif |
| 2071 | |
| 2072 | return 0; /* overflow */ |
| 2073 | } |
| 2074 | |
| 2075 | /* The inverse of the above. */ |
| 2076 | png_fixed_point |
| 2077 | png_reciprocal2(png_fixed_point a, png_fixed_point b) |
| 2078 | { |
Glenn Randers-Pehrson | 9c69091 | 2010-08-27 11:39:38 -0500 | [diff] [blame] | 2079 | /* 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] | 2080 | #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 2081 | double r = 1E15/a; |
| 2082 | r /= b; |
| 2083 | r = floor(r+.5); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2084 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2085 | if (r <= 2147483647. && r >= -2147483648.) |
| 2086 | return (png_fixed_point)r; |
| 2087 | #else |
| 2088 | /* This may overflow because the range of png_fixed_point isn't symmetric, |
| 2089 | * but this API is only used for the product of file and screen gamma so it |
| 2090 | * doesn't matter that the smallest number it can produce is 1/21474, not |
| 2091 | * 1/100000 |
| 2092 | */ |
| 2093 | png_fixed_point res = png_product2(a, b); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2094 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2095 | if (res != 0) |
| 2096 | return png_reciprocal(res); |
| 2097 | #endif |
| 2098 | |
| 2099 | return 0; /* overflow */ |
| 2100 | } |
| 2101 | #endif /* READ_GAMMA */ |
| 2102 | |
| 2103 | #ifdef PNG_CHECK_cHRM_SUPPORTED |
| 2104 | /* Added at libpng version 1.2.34 (Dec 8, 2008) and 1.4.0 (Jan 2, |
| 2105 | * 2010: moved from pngset.c) */ |
| 2106 | /* |
| 2107 | * Multiply two 32-bit numbers, V1 and V2, using 32-bit |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2108 | * arithmetic, to produce a 64-bit result in the HI/LO words. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2109 | * |
| 2110 | * A B |
| 2111 | * x C D |
| 2112 | * ------ |
| 2113 | * AD || BD |
| 2114 | * AC || CB || 0 |
| 2115 | * |
| 2116 | * where A and B are the high and low 16-bit words of V1, |
| 2117 | * C and D are the 16-bit words of V2, AD is the product of |
| 2118 | * A and D, and X || Y is (X << 16) + Y. |
| 2119 | */ |
| 2120 | |
| 2121 | void /* PRIVATE */ |
| 2122 | png_64bit_product (long v1, long v2, unsigned long *hi_product, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2123 | unsigned long *lo_product) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2124 | { |
| 2125 | int a, b, c, d; |
| 2126 | long lo, hi, x, y; |
| 2127 | |
| 2128 | a = (v1 >> 16) & 0xffff; |
| 2129 | b = v1 & 0xffff; |
| 2130 | c = (v2 >> 16) & 0xffff; |
| 2131 | d = v2 & 0xffff; |
| 2132 | |
| 2133 | lo = b * d; /* BD */ |
| 2134 | x = a * d + c * b; /* AD + CB */ |
| 2135 | y = ((lo >> 16) & 0xffff) + x; |
| 2136 | |
| 2137 | lo = (lo & 0xffff) | ((y & 0xffff) << 16); |
| 2138 | hi = (y >> 16) & 0xffff; |
| 2139 | |
| 2140 | hi += a * c; /* AC */ |
| 2141 | |
| 2142 | *hi_product = (unsigned long)hi; |
| 2143 | *lo_product = (unsigned long)lo; |
| 2144 | } |
| 2145 | #endif /* CHECK_cHRM */ |
| 2146 | |
| 2147 | #ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */ |
| 2148 | #ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED |
| 2149 | /* Fixed point gamma. |
| 2150 | * |
| 2151 | * To calculate gamma this code implements fast log() and exp() calls using only |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2152 | * fixed point arithmetic. This code has sufficient precision for either 8-bit |
| 2153 | * or 16-bit sample values. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2154 | * |
| 2155 | * The tables used here were calculated using simple 'bc' programs, but C double |
| 2156 | * precision floating point arithmetic would work fine. The programs are given |
| 2157 | * at the head of each table. |
| 2158 | * |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2159 | * 8-bit log table |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2160 | * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2161 | * 255, so it's the base 2 logarithm of a normalized 8-bit floating point |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2162 | * mantissa. The numbers are 32-bit fractions. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2163 | */ |
| 2164 | static png_uint_32 |
| 2165 | png_8bit_l2[128] = |
| 2166 | { |
| 2167 | # if PNG_DO_BC |
| 2168 | for (i=128;i<256;++i) { .5 - l(i/255)/l(2)*65536*65536; } |
| 2169 | # endif |
| 2170 | 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U, |
| 2171 | 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U, |
| 2172 | 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U, |
| 2173 | 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U, |
| 2174 | 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U, |
| 2175 | 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U, |
| 2176 | 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U, |
| 2177 | 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U, |
| 2178 | 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U, |
| 2179 | 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U, |
| 2180 | 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U, |
| 2181 | 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U, |
| 2182 | 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U, |
| 2183 | 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U, |
| 2184 | 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U, |
| 2185 | 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U, |
| 2186 | 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U, |
| 2187 | 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U, |
| 2188 | 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U, |
| 2189 | 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U, |
| 2190 | 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U, |
| 2191 | 24347096U, 0U |
| 2192 | #if 0 |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2193 | /* The following are the values for 16-bit tables - these work fine for the |
| 2194 | * 8-bit conversions but produce very slightly larger errors in the 16-bit |
| 2195 | * log (about 1.2 as opposed to 0.7 absolute error in the final value). To |
| 2196 | * use these all the shifts below must be adjusted appropriately. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2197 | */ |
| 2198 | 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054, |
| 2199 | 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803, |
| 2200 | 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068, |
| 2201 | 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782, |
| 2202 | 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887, |
| 2203 | 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339, |
| 2204 | 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098, |
| 2205 | 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132, |
| 2206 | 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415, |
| 2207 | 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523, |
| 2208 | 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495, |
| 2209 | 1119, 744, 372 |
| 2210 | #endif |
| 2211 | }; |
| 2212 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2213 | PNG_STATIC png_int_32 |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2214 | png_log8bit(unsigned int x) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2215 | { |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2216 | unsigned int lg2 = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2217 | /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log, |
| 2218 | * because the log is actually negate that means adding 1. The final |
| 2219 | * returned value thus has the range 0 (for 255 input) to 7.994 (for 1 |
| 2220 | * input), return 7.99998 for the overflow (log 0) case - so the result is |
| 2221 | * always at most 19 bits. |
| 2222 | */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2223 | if ((x &= 0xff) == 0) |
Glenn Randers-Pehrson | a581556 | 2010-11-20 21:48:29 -0600 | [diff] [blame] | 2224 | return 0xffffffff; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2225 | |
| 2226 | if ((x & 0xf0) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2227 | lg2 = 4, x <<= 4; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2228 | |
| 2229 | if ((x & 0xc0) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2230 | lg2 += 2, x <<= 2; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2231 | |
| 2232 | if ((x & 0x80) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2233 | lg2 += 1, x <<= 1; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2234 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2235 | /* result is at most 19 bits, so this cast is safe: */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2236 | return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2237 | } |
| 2238 | |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2239 | /* The above gives exact (to 16 binary places) log2 values for 8-bit images, |
| 2240 | * for 16-bit images we use the most significant 8 bits of the 16-bit value to |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2241 | * get an approximation then multiply the approximation by a correction factor |
| 2242 | * determined by the remaining up to 8 bits. This requires an additional step |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2243 | * in the 16-bit case. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2244 | * |
| 2245 | * We want log2(value/65535), we have log2(v'/255), where: |
| 2246 | * |
| 2247 | * value = v' * 256 + v'' |
| 2248 | * = v' * f |
| 2249 | * |
| 2250 | * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128 |
| 2251 | * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2252 | * than 258. The final factor also needs to correct for the fact that our 8-bit |
| 2253 | * value is scaled by 255, whereas the 16-bit values must be scaled by 65535. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2254 | * |
| 2255 | * This gives a final formula using a calculated value 'x' which is value/v' and |
| 2256 | * scaling by 65536 to match the above table: |
| 2257 | * |
| 2258 | * log2(x/257) * 65536 |
| 2259 | * |
| 2260 | * Since these numbers are so close to '1' we can use simple linear |
| 2261 | * interpolation between the two end values 256/257 (result -368.61) and 258/257 |
| 2262 | * (result 367.179). The values used below are scaled by a further 64 to give |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2263 | * 16-bit precision in the interpolation: |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2264 | * |
| 2265 | * Start (256): -23591 |
| 2266 | * Zero (257): 0 |
| 2267 | * End (258): 23499 |
| 2268 | */ |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2269 | PNG_STATIC png_int_32 |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2270 | png_log16bit(png_uint_32 x) |
| 2271 | { |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2272 | unsigned int lg2 = 0; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2273 | |
| 2274 | /* As above, but now the input has 16 bits. */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2275 | if ((x &= 0xffff) == 0) |
| 2276 | return 0xffffffff; |
| 2277 | |
| 2278 | if ((x & 0xff00) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2279 | lg2 = 8, x <<= 8; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2280 | |
| 2281 | if ((x & 0xf000) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2282 | lg2 += 4, x <<= 4; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2283 | |
| 2284 | if ((x & 0xc000) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2285 | lg2 += 2, x <<= 2; |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2286 | |
| 2287 | if ((x & 0x8000) == 0) |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2288 | lg2 += 1, x <<= 1; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2289 | |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2290 | /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2291 | * value. |
| 2292 | */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2293 | lg2 <<= 28; |
| 2294 | lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2295 | |
| 2296 | /* Now we need to interpolate the factor, this requires a division by the top |
| 2297 | * 8 bits. Do this with maximum precision. |
| 2298 | */ |
| 2299 | x = ((x << 16) + (x >> 9)) / (x >> 8); |
| 2300 | |
| 2301 | /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24, |
| 2302 | * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly |
| 2303 | * 16 bits to interpolate to get the low bits of the result. Round the |
| 2304 | * answer. Note that the end point values are scaled by 64 to retain overall |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2305 | * precision and that 'lg2' is current scaled by an extra 12 bits, so adjust |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2306 | * the overall scaling by 6-12. Round at every step. |
| 2307 | */ |
| 2308 | x -= 1U << 24; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2309 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2310 | if (x <= 65536U) /* <= '257' */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2311 | lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2312 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2313 | else |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2314 | lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2315 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2316 | /* Safe, because the result can't have more than 20 bits: */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2317 | return (png_int_32)((lg2 + 2048) >> 12); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2318 | } |
| 2319 | |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2320 | /* The 'exp()' case must invert the above, taking a 20-bit fixed point |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2321 | * logarithmic value and returning a 16 or 8-bit number as appropriate. In |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2322 | * each case only the low 16 bits are relevant - the fraction - since the |
| 2323 | * integer bits (the top 4) simply determine a shift. |
| 2324 | * |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2325 | * The worst case is the 16-bit distinction between 65535 and 65534, this |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2326 | * requires perhaps spurious accuracty in the decoding of the logarithm to |
| 2327 | * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance |
| 2328 | * of getting this accuracy in practice. |
| 2329 | * |
| 2330 | * To deal with this the following exp() function works out the exponent of the |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2331 | * frational part of the logarithm by using an accurate 32-bit value from the |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2332 | * top four fractional bits then multiplying in the remaining bits. |
| 2333 | */ |
| 2334 | static png_uint_32 |
| 2335 | png_32bit_exp[16] = |
| 2336 | { |
| 2337 | # if PNG_DO_BC |
| 2338 | for (i=0;i<16;++i) { .5 + e(-i/16*l(2))*2^32; } |
| 2339 | # endif |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2340 | /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2341 | 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U, |
| 2342 | 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U, |
| 2343 | 2553802834U, 2445529972U, 2341847524U, 2242560872U |
| 2344 | }; |
| 2345 | |
| 2346 | /* Adjustment table; provided to explain the numbers in the code below. */ |
| 2347 | #if PNG_DO_BC |
| 2348 | for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} |
| 2349 | 11 44937.64284865548751208448 |
| 2350 | 10 45180.98734845585101160448 |
| 2351 | 9 45303.31936980687359311872 |
| 2352 | 8 45364.65110595323018870784 |
| 2353 | 7 45395.35850361789624614912 |
| 2354 | 6 45410.72259715102037508096 |
| 2355 | 5 45418.40724413220722311168 |
| 2356 | 4 45422.25021786898173001728 |
| 2357 | 3 45424.17186732298419044352 |
| 2358 | 2 45425.13273269940811464704 |
| 2359 | 1 45425.61317555035558641664 |
| 2360 | 0 45425.85339951654943850496 |
| 2361 | #endif |
| 2362 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2363 | PNG_STATIC png_uint_32 |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2364 | png_exp(png_fixed_point x) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2365 | { |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2366 | if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2367 | { |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2368 | /* Obtain a 4-bit approximation */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2369 | png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf]; |
| 2370 | |
| 2371 | /* Incorporate the low 12 bits - these decrease the returned value by |
| 2372 | * 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] | 2373 | * 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] | 2374 | * converge on 45426 and this is used to allow linear interpolation of the |
| 2375 | * low bits. |
| 2376 | */ |
Glenn Randers-Pehrson | 233357e | 2010-07-29 21:49:38 -0500 | [diff] [blame] | 2377 | if (x & 0x800) |
| 2378 | e -= (((e >> 16) * 44938U) + 16U) >> 5; |
| 2379 | |
| 2380 | if (x & 0x400) |
| 2381 | e -= (((e >> 16) * 45181U) + 32U) >> 6; |
| 2382 | |
| 2383 | if (x & 0x200) |
| 2384 | e -= (((e >> 16) * 45303U) + 64U) >> 7; |
| 2385 | |
| 2386 | if (x & 0x100) |
| 2387 | e -= (((e >> 16) * 45365U) + 128U) >> 8; |
| 2388 | |
| 2389 | if (x & 0x080) |
| 2390 | e -= (((e >> 16) * 45395U) + 256U) >> 9; |
| 2391 | |
| 2392 | if (x & 0x040) |
| 2393 | e -= (((e >> 16) * 45410U) + 512U) >> 10; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2394 | |
| 2395 | /* And handle the low 6 bits in a single block. */ |
| 2396 | e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9; |
| 2397 | |
| 2398 | /* Handle the upper bits of x. */ |
| 2399 | e >>= x >> 16; |
| 2400 | return e; |
| 2401 | } |
| 2402 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2403 | /* Check for overflow */ |
| 2404 | if (x <= 0) |
| 2405 | return png_32bit_exp[0]; |
| 2406 | |
| 2407 | /* Else underflow */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2408 | return 0; |
| 2409 | } |
| 2410 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2411 | PNG_STATIC png_byte |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2412 | png_exp8bit(png_fixed_point lg2) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2413 | { |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2414 | /* Get a 32-bit value: */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2415 | png_uint_32 x = png_exp(lg2); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2416 | |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2417 | /* Convert the 32-bit value to 0..255 by multiplying by 256-1, note that the |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2418 | * second, rounding, step can't overflow because of the first, subtraction, |
| 2419 | * step. |
| 2420 | */ |
| 2421 | x -= x >> 8; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2422 | return (png_byte)((x + 0x7fffffU) >> 24); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2423 | } |
| 2424 | |
John Bowler | 8d26126 | 2011-06-18 13:37:11 -0500 | [diff] [blame] | 2425 | PNG_STATIC png_uint_16 |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2426 | png_exp16bit(png_fixed_point lg2) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2427 | { |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2428 | /* Get a 32-bit value: */ |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2429 | png_uint_32 x = png_exp(lg2); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2430 | |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2431 | /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2432 | x -= x >> 16; |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2433 | return (png_uint_16)((x + 32767U) >> 16); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2434 | } |
| 2435 | #endif /* FLOATING_ARITHMETIC */ |
| 2436 | |
| 2437 | png_byte |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2438 | png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2439 | { |
| 2440 | if (value > 0 && value < 255) |
| 2441 | { |
| 2442 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2443 | double r = floor(255*pow(value/255.,gamma_val*.00001)+.5); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2444 | return (png_byte)r; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2445 | # else |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2446 | png_int_32 lg2 = png_log8bit(value); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2447 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2448 | |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2449 | if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2450 | return png_exp8bit(res); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2451 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 2452 | /* Overflow. */ |
| 2453 | value = 0; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2454 | # endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2455 | } |
| 2456 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2457 | return (png_byte)value; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | png_uint_16 |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2461 | png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2462 | { |
| 2463 | if (value > 0 && value < 65535) |
| 2464 | { |
| 2465 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2466 | double r = floor(65535*pow(value/65535.,gamma_val*.00001)+.5); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2467 | return (png_uint_16)r; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2468 | # else |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2469 | png_int_32 lg2 = png_log16bit(value); |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2470 | png_fixed_point res; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2471 | |
John Bowler | 168a433 | 2011-01-16 19:32:22 -0600 | [diff] [blame] | 2472 | if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2473 | return png_exp16bit(res); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2474 | |
Glenn Randers-Pehrson | 3df324d | 2010-07-31 13:45:04 -0500 | [diff] [blame] | 2475 | /* Overflow. */ |
| 2476 | value = 0; |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2477 | # endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2478 | } |
| 2479 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2480 | return (png_uint_16)value; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* This does the right thing based on the bit_depth field of the |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2484 | * png_struct, interpreting values as 8-bit or 16-bit. While the result |
| 2485 | * is nominally a 16-bit value if bit depth is 8 then the result is |
| 2486 | * 8-bit (as are the arguments.) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2487 | */ |
| 2488 | png_uint_16 /* PRIVATE */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2489 | png_gamma_correct(png_structp png_ptr, unsigned int value, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2490 | png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2491 | { |
| 2492 | if (png_ptr->bit_depth == 8) |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2493 | return png_gamma_8bit_correct(value, gamma_val); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2494 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2495 | else |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2496 | return png_gamma_16bit_correct(value, gamma_val); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | /* This is the shared test on whether a gamma value is 'significant' - whether |
| 2500 | * it is worth doing gamma correction. |
| 2501 | */ |
| 2502 | int /* PRIVATE */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2503 | png_gamma_significant(png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2504 | { |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2505 | return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || |
| 2506 | gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2507 | } |
| 2508 | |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2509 | /* Internal function to build a single 16-bit table - the table consists of |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2510 | * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount |
| 2511 | * to shift the input values right (or 16-number_of_signifiant_bits). |
| 2512 | * |
Glenn Randers-Pehrson | a774c5d | 2010-08-26 19:37:55 -0500 | [diff] [blame] | 2513 | * 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] | 2514 | * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument |
| 2515 | * should be somewhere that will be cleaned. |
| 2516 | */ |
| 2517 | static void |
| 2518 | png_build_16bit_table(png_structp png_ptr, png_uint_16pp *ptable, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2519 | PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2520 | { |
| 2521 | /* Various values derived from 'shift': */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2522 | PNG_CONST unsigned int num = 1U << (8U - shift); |
| 2523 | PNG_CONST unsigned int max = (1U << (16U - shift))-1U; |
| 2524 | PNG_CONST unsigned int max_by_2 = 1U << (15U-shift); |
| 2525 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2526 | |
| 2527 | png_uint_16pp table = *ptable = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2528 | (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] | 2529 | |
| 2530 | for (i = 0; i < num; i++) |
| 2531 | { |
| 2532 | png_uint_16p sub_table = table[i] = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2533 | (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] | 2534 | |
| 2535 | /* The 'threshold' test is repeated here because it can arise for one of |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2536 | * the 16-bit tables even if the others don't hit it. |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2537 | */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2538 | if (png_gamma_significant(gamma_val)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2539 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2540 | /* The old code would overflow at the end and this would cause the |
| 2541 | * 'pow' function to return a result >1, resulting in an |
| 2542 | * arithmetic error. This code follows the spec exactly; ig is |
| 2543 | * the recovered input sample, it always has 8-16 bits. |
| 2544 | * |
| 2545 | * We want input * 65535/max, rounded, the arithmetic fits in 32 |
| 2546 | * bits (unsigned) so long as max <= 32767. |
| 2547 | */ |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2548 | unsigned int j; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2549 | for (j = 0; j < 256; j++) |
| 2550 | { |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2551 | png_uint_32 ig = (j << (8-shift)) + i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2552 | # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2553 | /* Inline the 'max' scaling operation: */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2554 | double d = floor(65535*pow(ig/(double)max, gamma_val*.00001)+.5); |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2555 | sub_table[j] = (png_uint_16)d; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2556 | # else |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2557 | if (shift) |
Glenn Randers-Pehrson | 4eb18e9 | 2010-07-30 14:46:52 -0500 | [diff] [blame] | 2558 | ig = (ig * 65535U + max_by_2)/max; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2559 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2560 | sub_table[j] = png_gamma_16bit_correct(ig, gamma_val); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2561 | # endif |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2562 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2563 | } |
| 2564 | else |
| 2565 | { |
| 2566 | /* 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] | 2567 | unsigned int j; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2568 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2569 | for (j = 0; j < 256; j++) |
| 2570 | { |
| 2571 | png_uint_32 ig = (j << (8-shift)) + i; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2572 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2573 | if (shift) |
| 2574 | ig = (ig * 65535U + max_by_2)/max; |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2575 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2576 | sub_table[j] = (png_uint_16)ig; |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2577 | } |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | /* NOTE: this function expects the *inverse* of the overall gamma transformation |
| 2583 | * required. |
| 2584 | */ |
| 2585 | static void |
| 2586 | png_build_16to8_table(png_structp png_ptr, png_uint_16pp *ptable, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2587 | PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2588 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2589 | PNG_CONST unsigned int num = 1U << (8U - shift); |
| 2590 | PNG_CONST unsigned int max = (1U << (16U - shift))-1U; |
| 2591 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2592 | png_uint_32 last; |
| 2593 | |
| 2594 | png_uint_16pp table = *ptable = |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2595 | (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] | 2596 | |
| 2597 | /* 'num' is the number of tables and also the number of low bits of low |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2598 | * bits of the input 16-bit value used to select a table. Each table is |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2599 | * itself index by the high 8 bits of the value. |
| 2600 | */ |
| 2601 | for (i = 0; i < num; i++) |
| 2602 | table[i] = (png_uint_16p)png_malloc(png_ptr, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2603 | 256 * png_sizeof(png_uint_16)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2604 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2605 | /* 'gamma_val' is set to the reciprocal of the value calculated above, so |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2606 | * pow(out,g) is an *input* value. 'last' is the last input value set. |
| 2607 | * |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2608 | * In the loop 'i' is used to find output values. Since the output is |
| 2609 | * 8-bit there are only 256 possible values. The tables are set up to |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2610 | * select the closest possible output value for each input by finding |
| 2611 | * the input value at the boundary between each pair of output values |
| 2612 | * and filling the table up to that boundary with the lower output |
| 2613 | * value. |
| 2614 | * |
Glenn Randers-Pehrson | 8a7ec52 | 2011-05-17 07:14:30 -0500 | [diff] [blame] | 2615 | * The boundary values are 0.5,1.5..253.5,254.5. Since these are 9-bit |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2616 | * values the code below uses a 16-bit value in i; the values start at |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2617 | * 128.5 (for 0.5) and step by 257, for a total of 254 values (the last |
| 2618 | * entries are filled with 255). Start i at 128 and fill all 'last' |
| 2619 | * table entries <= 'max' |
| 2620 | */ |
| 2621 | last = 0; |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2622 | for (i = 0; i < 255; ++i) /* 8-bit output value */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2623 | { |
| 2624 | /* Find the corresponding maximum input value */ |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2625 | 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] | 2626 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2627 | /* Find the boundary value in 16 bits: */ |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2628 | png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2629 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2630 | /* Adjust (round) to (16-shift) bits: */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2631 | bound = (bound * max + 32768U)/65535U + 1U; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2632 | |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2633 | while (last < bound) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2634 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2635 | table[last & (0xffU >> shift)][last >> (8U - shift)] = out; |
| 2636 | last++; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | /* And fill in the final entries. */ |
| 2641 | while (last < (num << 8)) |
| 2642 | { |
| 2643 | table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U; |
| 2644 | last++; |
| 2645 | } |
| 2646 | } |
| 2647 | |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2648 | /* Build a single 8-bit table: same as the 16-bit case but much simpler (and |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2649 | * typically much faster). Note that libpng currently does no sBIT processing |
| 2650 | * (apparently contrary to the spec) so a 256 entry table is always generated. |
| 2651 | */ |
| 2652 | static void |
| 2653 | png_build_8bit_table(png_structp png_ptr, png_bytepp ptable, |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2654 | PNG_CONST png_fixed_point gamma_val) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2655 | { |
Glenn Randers-Pehrson | e600c51 | 2010-08-18 07:25:46 -0500 | [diff] [blame] | 2656 | unsigned int i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2657 | png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256); |
| 2658 | |
Glenn Randers-Pehrson | 2368a92 | 2011-01-16 13:32:05 -0600 | [diff] [blame] | 2659 | if (png_gamma_significant(gamma_val)) for (i=0; i<256; i++) |
| 2660 | table[i] = png_gamma_8bit_correct(i, gamma_val); |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2661 | |
Glenn Randers-Pehrson | 4009a76 | 2010-07-31 06:34:36 -0500 | [diff] [blame] | 2662 | else for (i=0; i<256; ++i) |
| 2663 | table[i] = (png_byte)i; |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2664 | } |
| 2665 | |
John Bowler | 07772cb | 2011-10-14 18:19:47 -0500 | [diff] [blame] | 2666 | /* Used from png_read_destroy and below to release the memory used by the gamma |
| 2667 | * tables. |
| 2668 | */ |
| 2669 | void /* PRIVATE */ |
| 2670 | png_destroy_gamma_table(png_structp png_ptr) |
| 2671 | { |
| 2672 | png_free(png_ptr, png_ptr->gamma_table); |
| 2673 | png_ptr->gamma_table = NULL; |
| 2674 | |
| 2675 | if (png_ptr->gamma_16_table != NULL) |
| 2676 | { |
| 2677 | int i; |
| 2678 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
| 2679 | for (i = 0; i < istop; i++) |
| 2680 | { |
| 2681 | png_free(png_ptr, png_ptr->gamma_16_table[i]); |
| 2682 | } |
| 2683 | png_free(png_ptr, png_ptr->gamma_16_table); |
| 2684 | png_ptr->gamma_16_table = NULL; |
| 2685 | } |
| 2686 | |
| 2687 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
| 2688 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ |
| 2689 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
| 2690 | png_free(png_ptr, png_ptr->gamma_from_1); |
| 2691 | png_ptr->gamma_from_1 = NULL; |
| 2692 | png_free(png_ptr, png_ptr->gamma_to_1); |
| 2693 | png_ptr->gamma_to_1 = NULL; |
| 2694 | |
| 2695 | if (png_ptr->gamma_16_from_1 != NULL) |
| 2696 | { |
| 2697 | int i; |
| 2698 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
| 2699 | for (i = 0; i < istop; i++) |
| 2700 | { |
| 2701 | png_free(png_ptr, png_ptr->gamma_16_from_1[i]); |
| 2702 | } |
| 2703 | png_free(png_ptr, png_ptr->gamma_16_from_1); |
| 2704 | png_ptr->gamma_16_from_1 = NULL; |
| 2705 | } |
| 2706 | if (png_ptr->gamma_16_to_1 != NULL) |
| 2707 | { |
| 2708 | int i; |
| 2709 | int istop = (1 << (8 - png_ptr->gamma_shift)); |
| 2710 | for (i = 0; i < istop; i++) |
| 2711 | { |
| 2712 | png_free(png_ptr, png_ptr->gamma_16_to_1[i]); |
| 2713 | } |
| 2714 | png_free(png_ptr, png_ptr->gamma_16_to_1); |
| 2715 | png_ptr->gamma_16_to_1 = NULL; |
| 2716 | } |
| 2717 | #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ |
| 2718 | } |
| 2719 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2720 | /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit |
| 2721 | * tables, we don't make a full table if we are reducing to 8-bit in |
| 2722 | * the future. Note also how the gamma_16 tables are segmented so that |
| 2723 | * we don't need to allocate > 64K chunks for a full 16-bit table. |
| 2724 | */ |
| 2725 | void /* PRIVATE */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2726 | png_build_gamma_table(png_structp png_ptr, int bit_depth) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2727 | { |
| 2728 | png_debug(1, "in png_build_gamma_table"); |
| 2729 | |
John Bowler | 07772cb | 2011-10-14 18:19:47 -0500 | [diff] [blame] | 2730 | /* Remove any existing table; this copes with multiple calls to |
| 2731 | * png_read_update_info. The warning is because building the gamma tables |
| 2732 | * multiple times is a performance hit - it's harmless but the ability to call |
| 2733 | * png_read_update_info() multiple times is new in 1.5.6 so it seems sensible |
| 2734 | * to warn if the app introduces such a hit. |
| 2735 | */ |
| 2736 | if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL) |
| 2737 | { |
| 2738 | png_warning(png_ptr, "gamma table being rebuilt"); |
| 2739 | png_destroy_gamma_table(png_ptr); |
| 2740 | } |
| 2741 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2742 | if (bit_depth <= 8) |
| 2743 | { |
| 2744 | png_build_8bit_table(png_ptr, &png_ptr->gamma_table, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2745 | png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, |
| 2746 | png_ptr->screen_gamma) : PNG_FP_1); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2747 | |
| 2748 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2749 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2750 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2751 | if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2752 | { |
| 2753 | png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2754 | png_reciprocal(png_ptr->gamma)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2755 | |
| 2756 | png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2757 | png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : |
| 2758 | png_ptr->gamma/* Probably doing rgb_to_gray */); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2759 | } |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2760 | #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2761 | } |
| 2762 | else |
| 2763 | { |
| 2764 | png_byte shift, sig_bit; |
| 2765 | |
| 2766 | if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) |
| 2767 | { |
| 2768 | sig_bit = png_ptr->sig_bit.red; |
| 2769 | |
| 2770 | if (png_ptr->sig_bit.green > sig_bit) |
| 2771 | sig_bit = png_ptr->sig_bit.green; |
| 2772 | |
| 2773 | if (png_ptr->sig_bit.blue > sig_bit) |
| 2774 | sig_bit = png_ptr->sig_bit.blue; |
| 2775 | } |
| 2776 | else |
| 2777 | sig_bit = png_ptr->sig_bit.gray; |
| 2778 | |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2779 | /* 16-bit gamma code uses this equation: |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2780 | * |
| 2781 | * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8] |
| 2782 | * |
| 2783 | * Where 'iv' is the input color value and 'ov' is the output value - |
| 2784 | * pow(iv, gamma). |
| 2785 | * |
| 2786 | * Thus the gamma table consists of up to 256 256 entry tables. The table |
| 2787 | * is selected by the (8-gamma_shift) most significant of the low 8 bits of |
| 2788 | * the color value then indexed by the upper 8 bits: |
| 2789 | * |
| 2790 | * table[low bits][high 8 bits] |
| 2791 | * |
| 2792 | * So the table 'n' corresponds to all those 'iv' of: |
| 2793 | * |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2794 | * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1> |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2795 | * |
| 2796 | */ |
Glenn Randers-Pehrson | 67439c4 | 2010-08-19 07:01:09 -0500 | [diff] [blame] | 2797 | if (sig_bit > 0 && sig_bit < 16U) |
| 2798 | shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */ |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2799 | |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2800 | else |
| 2801 | shift = 0; /* keep all 16 bits */ |
| 2802 | |
Glenn Randers-Pehrson | ab63dd0 | 2011-06-17 20:04:17 -0500 | [diff] [blame] | 2803 | if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2804 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2805 | /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively |
| 2806 | * the significant bits in the *input* when the output will |
| 2807 | * eventually be 8 bits. By default it is 11. |
| 2808 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2809 | if (shift < (16U - PNG_MAX_GAMMA_8)) |
| 2810 | shift = (16U - PNG_MAX_GAMMA_8); |
| 2811 | } |
| 2812 | |
| 2813 | if (shift > 8U) |
| 2814 | shift = 8U; /* Guarantees at least one table! */ |
| 2815 | |
| 2816 | png_ptr->gamma_shift = shift; |
| 2817 | |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2818 | #ifdef PNG_16BIT_SUPPORTED |
Glenn Randers-Pehrson | ef217b7 | 2011-06-15 12:58:27 -0500 | [diff] [blame] | 2819 | /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2820 | * PNG_COMPOSE). This effectively smashed the background calculation for |
Glenn Randers-Pehrson | 55fbff3 | 2011-05-17 06:49:32 -0500 | [diff] [blame] | 2821 | * 16-bit output because the 8-bit table assumes the result will be reduced |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2822 | * to 8 bits. |
| 2823 | */ |
Glenn Randers-Pehrson | ab63dd0 | 2011-06-17 20:04:17 -0500 | [diff] [blame] | 2824 | if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2825 | #endif |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2826 | png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, |
| 2827 | png_ptr->screen_gamma > 0 ? png_product2(png_ptr->gamma, |
| 2828 | png_ptr->screen_gamma) : PNG_FP_1); |
| 2829 | |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2830 | #ifdef PNG_16BIT_SUPPORTED |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2831 | else |
Glenn Randers-Pehrson | 7b81e2e | 2010-07-29 22:54:34 -0500 | [diff] [blame] | 2832 | png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, |
| 2833 | png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->gamma, |
| 2834 | png_ptr->screen_gamma) : PNG_FP_1); |
Glenn Randers-Pehrson | 4e48761 | 2010-08-26 21:41:04 -0500 | [diff] [blame] | 2835 | #endif |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2836 | |
| 2837 | #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2838 | defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2839 | defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2840 | if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2841 | { |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2842 | 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] | 2843 | png_reciprocal(png_ptr->gamma)); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2844 | |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 2845 | /* 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] | 2846 | * 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] | 2847 | * TODO: fix this. |
| 2848 | */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2849 | 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] | 2850 | png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : |
| 2851 | png_ptr->gamma/* Probably doing rgb_to_gray */); |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2852 | } |
John Bowler | d273ad2 | 2011-05-07 21:00:28 -0500 | [diff] [blame] | 2853 | #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ |
Glenn Randers-Pehrson | 31aee0d | 2010-07-29 17:39:14 -0500 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | #endif /* READ_GAMMA */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2857 | |
| 2858 | /* sRGB support */ |
| 2859 | #if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\ |
| 2860 | defined PNG_SIMPLIFIED_WRITE_SUPPORTED |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2861 | /* sRGB conversion tables; these are machine generated with the code in |
| 2862 | * contrib/sRGBtables/makesRGB.c. The sRGB to linear table is exact (to the |
| 2863 | * nearest 16 bit linear fraction). The inverse (linear to sRGB) table has |
| 2864 | * accuracies as follows: |
| 2865 | * |
| 2866 | * For all possible (255*65535+1) input values: |
| 2867 | * |
| 2868 | * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact |
| 2869 | * |
| 2870 | * For the input values corresponding to the 65536 16-bit values: |
| 2871 | * |
| 2872 | * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact |
| 2873 | * |
| 2874 | * In all cases the inexact readings are off by one. |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2875 | */ |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2876 | |
| 2877 | #ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 2878 | /* The convert-to-sRGB table is only currently required for read. */ |
Glenn Randers-Pehrson | 8596468 | 2011-11-16 08:52:36 -0600 | [diff] [blame] | 2879 | png_uint_16 png_sRGB_table[256] = |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2880 | { |
| 2881 | 0,20,40,60,80,99,119,139, |
| 2882 | 159,179,199,219,241,264,288,313, |
| 2883 | 340,367,396,427,458,491,526,562, |
| 2884 | 599,637,677,718,761,805,851,898, |
| 2885 | 947,997,1048,1101,1156,1212,1270,1330, |
| 2886 | 1391,1453,1517,1583,1651,1720,1790,1863, |
| 2887 | 1937,2013,2090,2170,2250,2333,2418,2504, |
| 2888 | 2592,2681,2773,2866,2961,3058,3157,3258, |
| 2889 | 3360,3464,3570,3678,3788,3900,4014,4129, |
| 2890 | 4247,4366,4488,4611,4736,4864,4993,5124, |
| 2891 | 5257,5392,5530,5669,5810,5953,6099,6246, |
| 2892 | 6395,6547,6700,6856,7014,7174,7335,7500, |
| 2893 | 7666,7834,8004,8177,8352,8528,8708,8889, |
| 2894 | 9072,9258,9445,9635,9828,10022,10219,10417, |
| 2895 | 10619,10822,11028,11235,11446,11658,11873,12090, |
| 2896 | 12309,12530,12754,12980,13209,13440,13673,13909, |
| 2897 | 14146,14387,14629,14874,15122,15371,15623,15878, |
| 2898 | 16135,16394,16656,16920,17187,17456,17727,18001, |
| 2899 | 18277,18556,18837,19121,19407,19696,19987,20281, |
| 2900 | 20577,20876,21177,21481,21787,22096,22407,22721, |
| 2901 | 23038,23357,23678,24002,24329,24658,24990,25325, |
| 2902 | 25662,26001,26344,26688,27036,27386,27739,28094, |
| 2903 | 28452,28813,29176,29542,29911,30282,30656,31033, |
| 2904 | 31412,31794,32179,32567,32957,33350,33745,34143, |
| 2905 | 34544,34948,35355,35764,36176,36591,37008,37429, |
| 2906 | 37852,38278,38706,39138,39572,40009,40449,40891, |
| 2907 | 41337,41785,42236,42690,43147,43606,44069,44534, |
| 2908 | 45002,45473,45947,46423,46903,47385,47871,48359, |
| 2909 | 48850,49344,49841,50341,50844,51349,51858,52369, |
| 2910 | 52884,53401,53921,54445,54971,55500,56032,56567, |
| 2911 | 57105,57646,58190,58737,59287,59840,60396,60955, |
| 2912 | 61517,62082,62650,63221,63795,64372,64952,65535 |
| 2913 | }; |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2914 | |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2915 | #endif /* simplified read only */ |
| 2916 | |
| 2917 | /* The base/delta tables are required for both read and write (but currently |
| 2918 | * only the simplified versions.) |
| 2919 | */ |
Glenn Randers-Pehrson | 8596468 | 2011-11-16 08:52:36 -0600 | [diff] [blame] | 2920 | png_uint_16 png_sRGB_base[512] = |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2921 | { |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2922 | 128,1782,3383,4644,5675,6564,7357,8074, |
| 2923 | 8732,9346,9921,10463,10977,11466,11935,12384, |
| 2924 | 12816,13233,13634,14024,14402,14769,15125,15473, |
| 2925 | 15812,16142,16466,16781,17090,17393,17690,17981, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2926 | 18266,18546,18822,19093,19359,19621,19879,20133, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2927 | 20383,20630,20873,21113,21349,21583,21813,22041, |
| 2928 | 22265,22487,22707,22923,23138,23350,23559,23767, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2929 | 23972,24175,24376,24575,24772,24967,25160,25352, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2930 | 25542,25730,25916,26101,26284,26465,26645,26823, |
| 2931 | 27000,27176,27350,27523,27695,27865,28034,28201, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2932 | 28368,28533,28697,28860,29021,29182,29341,29500, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2933 | 29657,29813,29969,30123,30276,30429,30580,30730, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2934 | 30880,31028,31176,31323,31469,31614,31758,31902, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2935 | 32045,32186,32327,32468,32607,32746,32884,33021, |
| 2936 | 33158,33294,33429,33564,33697,33831,33963,34095, |
| 2937 | 34226,34357,34486,34616,34744,34873,35000,35127, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2938 | 35253,35379,35504,35629,35753,35876,35999,36122, |
| 2939 | 36244,36365,36486,36606,36726,36845,36964,37083, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2940 | 37201,37318,37435,37551,37668,37783,37898,38013, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2941 | 38127,38241,38354,38467,38580,38692,38803,38915, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2942 | 39026,39136,39246,39356,39465,39574,39682,39790, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2943 | 39898,40005,40112,40219,40325,40431,40537,40642, |
| 2944 | 40747,40851,40955,41059,41163,41266,41369,41471, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2945 | 41573,41675,41777,41878,41979,42079,42179,42279, |
| 2946 | 42379,42478,42577,42676,42775,42873,42971,43068, |
| 2947 | 43165,43262,43359,43456,43552,43648,43743,43839, |
| 2948 | 43934,44028,44123,44217,44311,44405,44499,44592, |
| 2949 | 44685,44778,44870,44962,45054,45146,45238,45329, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2950 | 45420,45511,45601,45692,45782,45872,45961,46051, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2951 | 46140,46229,46318,46406,46494,46583,46670,46758, |
| 2952 | 46846,46933,47020,47107,47193,47280,47366,47452, |
| 2953 | 47538,47623,47709,47794,47879,47964,48048,48133, |
| 2954 | 48217,48301,48385,48468,48552,48635,48718,48801, |
| 2955 | 48884,48966,49048,49131,49213,49294,49376,49458, |
| 2956 | 49539,49620,49701,49782,49862,49943,50023,50103, |
| 2957 | 50183,50263,50342,50422,50501,50580,50659,50738, |
| 2958 | 50816,50895,50973,51051,51129,51207,51285,51362, |
| 2959 | 51439,51517,51594,51671,51747,51824,51900,51977, |
| 2960 | 52053,52129,52205,52280,52356,52432,52507,52582, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2961 | 52657,52732,52807,52881,52956,53030,53104,53178, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2962 | 53252,53326,53400,53473,53546,53620,53693,53766, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2963 | 53839,53911,53984,54056,54129,54201,54273,54345, |
| 2964 | 54417,54489,54560,54632,54703,54774,54845,54916, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2965 | 54987,55058,55129,55199,55269,55340,55410,55480, |
| 2966 | 55550,55620,55689,55759,55828,55898,55967,56036, |
| 2967 | 56105,56174,56243,56311,56380,56448,56517,56585, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2968 | 56653,56721,56789,56857,56924,56992,57059,57127, |
| 2969 | 57194,57261,57328,57395,57462,57529,57595,57662, |
| 2970 | 57728,57795,57861,57927,57993,58059,58125,58191, |
| 2971 | 58256,58322,58387,58453,58518,58583,58648,58713, |
| 2972 | 58778,58843,58908,58972,59037,59101,59165,59230, |
| 2973 | 59294,59358,59422,59486,59549,59613,59677,59740, |
| 2974 | 59804,59867,59930,59993,60056,60119,60182,60245, |
| 2975 | 60308,60370,60433,60495,60558,60620,60682,60744, |
| 2976 | 60806,60868,60930,60992,61054,61115,61177,61238, |
| 2977 | 61300,61361,61422,61483,61544,61605,61666,61727, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2978 | 61788,61848,61909,61969,62030,62090,62150,62211, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2979 | 62271,62331,62391,62450,62510,62570,62630,62689, |
| 2980 | 62749,62808,62867,62927,62986,63045,63104,63163, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2981 | 63222,63281,63340,63398,63457,63515,63574,63632, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2982 | 63691,63749,63807,63865,63923,63981,64039,64097, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2983 | 64155,64212,64270,64328,64385,64443,64500,64557, |
| 2984 | 64614,64672,64729,64786,64843,64900,64956,65013, |
| 2985 | 65070,65126,65183,65239,65296,65352,65409,65465 |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2986 | }; |
| 2987 | |
Glenn Randers-Pehrson | 8596468 | 2011-11-16 08:52:36 -0600 | [diff] [blame] | 2988 | png_byte png_sRGB_delta[512] = |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2989 | { |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2990 | 207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2991 | 52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36, |
| 2992 | 35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28, |
John Bowler | 0a521d3 | 2011-11-11 18:14:59 -0600 | [diff] [blame] | 2993 | 28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24, |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 2994 | 23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21, |
| 2995 | 21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19, |
| 2996 | 19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17, |
| 2997 | 17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16, |
| 2998 | 16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15, |
| 2999 | 15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14, |
| 3000 | 14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13, |
| 3001 | 13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12, |
| 3002 | 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, |
| 3003 | 12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11, |
| 3004 | 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, |
| 3005 | 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, |
| 3006 | 11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, |
| 3007 | 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, |
| 3008 | 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, |
| 3009 | 10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 3010 | 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 3011 | 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 3012 | 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, |
| 3013 | 9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 3014 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 3015 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 3016 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 3017 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 3018 | 8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7, |
| 3019 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 3020 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, |
| 3021 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 |
| 3022 | }; |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3023 | #endif /* SIMPLIFIED READ/WRITE sRGB support */ |
| 3024 | |
| 3025 | /* SIMPLIFIED READ/WRITE SUPPORT */ |
| 3026 | #if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\ |
| 3027 | defined PNG_SIMPLIFIED_WRITE_SUPPORTED |
| 3028 | static int |
| 3029 | png_image_free_function(png_voidp argument) |
| 3030 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame^] | 3031 | png_imagep image = png_voidcast(png_imagep, argument); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3032 | png_controlp cp = image->opaque; |
| 3033 | png_control c; |
| 3034 | |
| 3035 | /* Double check that we have a png_ptr - it should be impossible to get here |
| 3036 | * without one. |
| 3037 | */ |
| 3038 | if (cp->png_ptr == NULL) |
| 3039 | return 0; |
| 3040 | |
| 3041 | /* First free any data held in the control structure. */ |
| 3042 | # ifdef PNG_STDIO_SUPPORTED |
| 3043 | if (cp->owned_file) |
| 3044 | { |
John Bowler | 4fa96a4 | 2011-11-16 16:39:16 -0600 | [diff] [blame^] | 3045 | FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr); |
John Bowler | 7875d53 | 2011-11-07 22:33:49 -0600 | [diff] [blame] | 3046 | cp->owned_file = 0; |
| 3047 | |
| 3048 | /* Ignore errors here. */ |
| 3049 | if (fp != NULL) |
| 3050 | { |
| 3051 | cp->png_ptr->io_ptr = NULL; |
| 3052 | (void)fclose(fp); |
| 3053 | } |
| 3054 | } |
| 3055 | # endif |
| 3056 | |
| 3057 | /* Copy the control structure so that the original, allocated, version can be |
| 3058 | * safely freed. Notice that a png_error here stops the remainder of the |
| 3059 | * cleanup, but this is probably fine because that would indicate bad memory |
| 3060 | * problems anyway. |
| 3061 | */ |
| 3062 | c = *cp; |
| 3063 | image->opaque = &c; |
| 3064 | png_free(c.png_ptr, cp); |
| 3065 | |
| 3066 | /* Then the structures, calling the correct API. */ |
| 3067 | if (c.for_write) |
| 3068 | { |
| 3069 | # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED |
| 3070 | png_destroy_write_struct(&c.png_ptr, &c.info_ptr); |
| 3071 | # else |
| 3072 | png_error(c.png_ptr, "simplified write not supported"); |
| 3073 | # endif |
| 3074 | } |
| 3075 | else |
| 3076 | { |
| 3077 | # ifdef PNG_SIMPLIFIED_READ_SUPPORTED |
| 3078 | png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL); |
| 3079 | # else |
| 3080 | png_error(c.png_ptr, "simplified read not supported"); |
| 3081 | # endif |
| 3082 | } |
| 3083 | |
| 3084 | /* Success. */ |
| 3085 | return 1; |
| 3086 | } |
| 3087 | |
| 3088 | void PNGAPI |
| 3089 | png_image_free(png_imagep image) |
| 3090 | { |
| 3091 | /* Safely call the real function, but only if doing so is safe at this point |
| 3092 | * (if not inside an error handling context). Otherwise assume |
| 3093 | * png_safe_execute will call this API after the return. |
| 3094 | */ |
| 3095 | if (image != NULL && image->opaque != NULL && |
| 3096 | image->opaque->error_buf == NULL) |
| 3097 | { |
| 3098 | /* Ignore errors here: */ |
| 3099 | (void)png_safe_execute(image, png_image_free_function, image); |
| 3100 | image->opaque = NULL; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | int /* PRIVATE */ |
| 3105 | png_image_error(png_imagep image, png_const_charp error_message) |
| 3106 | { |
| 3107 | /* Utility to log an error. */ |
| 3108 | png_safecat(image->message, sizeof image->message, 0, error_message); |
| 3109 | image->warning_or_error = 1; |
| 3110 | png_image_free(image); |
| 3111 | return 0; |
| 3112 | } |
| 3113 | |
| 3114 | #endif /* SIMPLIFIED READ/WRITE */ |
Glenn Randers-Pehrson | 29034c5 | 2010-07-29 17:58:49 -0500 | [diff] [blame] | 3115 | #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ |