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