Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(683)

Side by Side Diff: third_party/libpng/png.c

Issue 1591483003: XFA: Upgrade libpng to 1.6.20. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/libpng/png.h ('k') | third_party/libpng/pngconf.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1
1 /* png.c - location for general purpose libpng functions 2 /* png.c - location for general purpose libpng functions
2 * 3 *
3 * Last changed in libpng 1.6.2 [April 25, 2013] 4 * Last changed in libpng 1.6.19 [November 12, 2015]
4 * Copyright (c) 1998-2013 Glenn Randers-Pehrson 5 * Copyright (c) 1998-2015 Glenn Randers-Pehrson
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger) 6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) 7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
7 * 8 *
8 * This code is released under the libpng license. 9 * This code is released under the libpng license.
9 * For conditions of distribution and use, see the disclaimer 10 * For conditions of distribution and use, see the disclaimer
10 * and license in png.h 11 * and license in png.h
11 */ 12 */
12 13
13 #include "pngpriv.h" 14 #include "pngpriv.h"
14 15
15 /* Generate a compiler error if there is an old png.h in the search path. */ 16 /* Generate a compiler error if there is an old png.h in the search path. */
16 typedef png_libpng_version_1_6_3 Your_png_h_is_not_version_1_6_3; 17 typedef png_libpng_version_1_6_20 Your_png_h_is_not_version_1_6_20;
17 18
18 /* Tells libpng that we have already handled the first "num_bytes" bytes 19 /* Tells libpng that we have already handled the first "num_bytes" bytes
19 * of the PNG file signature. If the PNG data is embedded into another 20 * of the PNG file signature. If the PNG data is embedded into another
20 * stream we can set num_bytes = 8 so that libpng will not attempt to read 21 * stream we can set num_bytes = 8 so that libpng will not attempt to read
21 * or write any of the magic bytes before it starts on the IHDR. 22 * or write any of the magic bytes before it starts on the IHDR.
22 */ 23 */
23 24
24 #ifdef PNG_READ_SUPPORTED 25 #ifdef PNG_READ_SUPPORTED
25 void PNGAPI 26 void PNGAPI
26 png_set_sig_bytes(png_structrp png_ptr, int num_bytes) 27 png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
27 { 28 {
29 unsigned int nb = (unsigned int)num_bytes;
30
28 png_debug(1, "in png_set_sig_bytes"); 31 png_debug(1, "in png_set_sig_bytes");
29 32
30 if (png_ptr == NULL) 33 if (png_ptr == NULL)
31 return; 34 return;
32 35
33 if (num_bytes > 8) 36 if (num_bytes < 0)
37 nb = 0;
38
39 if (nb > 8)
34 png_error(png_ptr, "Too many bytes for PNG signature"); 40 png_error(png_ptr, "Too many bytes for PNG signature");
35 41
36 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes); 42 png_ptr->sig_bytes = (png_byte)nb;
37 } 43 }
38 44
39 /* Checks whether the supplied bytes match the PNG signature. We allow 45 /* Checks whether the supplied bytes match the PNG signature. We allow
40 * checking less than the full 8-byte signature so that those apps that 46 * checking less than the full 8-byte signature so that those apps that
41 * already read the first few bytes of a file to determine the file type 47 * already read the first few bytes of a file to determine the file type
42 * can simply check the remaining bytes for extra assurance. Returns 48 * can simply check the remaining bytes for extra assurance. Returns
43 * an integer less than, equal to, or greater than zero if sig is found, 49 * an integer less than, equal to, or greater than zero if sig is found,
44 * respectively, to be less than, to match, or be greater than the correct 50 * respectively, to be less than, to match, or be greater than the correct
45 * PNG signature (this is the same behavior as strcmp, memcmp, etc). 51 * PNG signature (this is the same behavior as strcmp, memcmp, etc).
46 */ 52 */
(...skipping 10 matching lines...) Expand all
57 63
58 if (start > 7) 64 if (start > 7)
59 return (-1); 65 return (-1);
60 66
61 if (start + num_to_check > 8) 67 if (start + num_to_check > 8)
62 num_to_check = 8 - start; 68 num_to_check = 8 - start;
63 69
64 return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check))); 70 return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check)));
65 } 71 }
66 72
67 #endif /* PNG_READ_SUPPORTED */ 73 #endif /* READ */
68 74
69 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) 75 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
70 /* Function to allocate memory for zlib */ 76 /* Function to allocate memory for zlib */
71 PNG_FUNCTION(voidpf /* PRIVATE */, 77 PNG_FUNCTION(voidpf /* PRIVATE */,
72 png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) 78 png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED)
73 { 79 {
74 png_alloc_size_t num_bytes = size; 80 png_alloc_size_t num_bytes = size;
75 81
76 if (png_ptr == NULL) 82 if (png_ptr == NULL)
77 return NULL; 83 return NULL;
(...skipping 15 matching lines...) Expand all
93 { 99 {
94 png_free(png_voidcast(png_const_structrp,png_ptr), ptr); 100 png_free(png_voidcast(png_const_structrp,png_ptr), ptr);
95 } 101 }
96 102
97 /* Reset the CRC variable to 32 bits of 1's. Care must be taken 103 /* Reset the CRC variable to 32 bits of 1's. Care must be taken
98 * in case CRC is > 32 bits to leave the top bits 0. 104 * in case CRC is > 32 bits to leave the top bits 0.
99 */ 105 */
100 void /* PRIVATE */ 106 void /* PRIVATE */
101 png_reset_crc(png_structrp png_ptr) 107 png_reset_crc(png_structrp png_ptr)
102 { 108 {
103 /* The cast is safe because the crc is a 32 bit value. */ 109 /* The cast is safe because the crc is a 32-bit value. */
104 png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0); 110 png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0);
105 } 111 }
106 112
107 /* Calculate the CRC over a section of data. We can only pass as 113 /* Calculate the CRC over a section of data. We can only pass as
108 * much data to this routine as the largest single buffer size. We 114 * much data to this routine as the largest single buffer size. We
109 * also check that this data will actually be used before going to the 115 * also check that this data will actually be used before going to the
110 * trouble of calculating it. 116 * trouble of calculating it.
111 */ 117 */
112 void /* PRIVATE */ 118 void /* PRIVATE */
113 png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length) 119 png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
114 { 120 {
115 int need_crc = 1; 121 int need_crc = 1;
116 122
117 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)) 123 if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
118 { 124 {
119 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == 125 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
120 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) 126 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
121 need_crc = 0; 127 need_crc = 0;
122 } 128 }
123 129
124 else /* critical */ 130 else /* critical */
125 { 131 {
126 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) 132 if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
127 need_crc = 0; 133 need_crc = 0;
128 } 134 }
129 135
130 /* 'uLong' is defined in zlib.h as unsigned long; this means that on some 136 /* 'uLong' is defined in zlib.h as unsigned long; this means that on some
131 * systems it is a 64 bit value. crc32, however, returns 32 bits so the 137 * systems it is a 64-bit value. crc32, however, returns 32 bits so the
132 * following cast is safe. 'uInt' may be no more than 16 bits, so it is 138 * following cast is safe. 'uInt' may be no more than 16 bits, so it is
133 * necessary to perform a loop here. 139 * necessary to perform a loop here.
134 */ 140 */
135 if (need_crc && length > 0) 141 if (need_crc != 0 && length > 0)
136 { 142 {
137 uLong crc = png_ptr->crc; /* Should never issue a warning */ 143 uLong crc = png_ptr->crc; /* Should never issue a warning */
138 144
139 do 145 do
140 { 146 {
141 uInt safe_length = (uInt)length; 147 uInt safe_length = (uInt)length;
148 #ifndef __COVERITY__
142 if (safe_length == 0) 149 if (safe_length == 0)
143 safe_length = (uInt)-1; /* evil, but safe */ 150 safe_length = (uInt)-1; /* evil, but safe */
151 #endif
144 152
145 crc = crc32(crc, ptr, safe_length); 153 crc = crc32(crc, ptr, safe_length);
146 154
147 /* The following should never issue compiler warnings; if they do the 155 /* The following should never issue compiler warnings; if they do the
148 * target system has characteristics that will probably violate other 156 * target system has characteristics that will probably violate other
149 * assumptions within the libpng code. 157 * assumptions within the libpng code.
150 */ 158 */
151 ptr += safe_length; 159 ptr += safe_length;
152 length -= safe_length; 160 length -= safe_length;
153 } 161 }
154 while (length > 0); 162 while (length > 0);
155 163
156 /* And the following is always safe because the crc is only 32 bits. */ 164 /* And the following is always safe because the crc is only 32 bits. */
157 png_ptr->crc = (png_uint_32)crc; 165 png_ptr->crc = (png_uint_32)crc;
158 } 166 }
159 } 167 }
160 168
161 /* Check a user supplied version number, called from both read and write 169 /* Check a user supplied version number, called from both read and write
162 * functions that create a png_struct. 170 * functions that create a png_struct.
163 */ 171 */
164 int 172 int
165 png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver) 173 png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver)
166 { 174 {
167 if (user_png_ver) 175 /* Libpng versions 1.0.0 and later are binary compatible if the version
168 { 176 * string matches through the second '.'; we must recompile any
169 int i = 0; 177 * applications that use any older library version.
178 */
179
180 if (user_png_ver != NULL)
181 {
182 int i = -1;
183 int found_dots = 0;
170 184
171 do 185 do
172 { 186 {
173 if (user_png_ver[i] != png_libpng_ver[i]) 187 i++;
188 if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i])
174 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; 189 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
175 } while (png_libpng_ver[i++]); 190 if (user_png_ver[i] == '.')
191 found_dots++;
192 } while (found_dots < 2 && user_png_ver[i] != 0 &&
193 PNG_LIBPNG_VER_STRING[i] != 0);
176 } 194 }
177 195
178 else 196 else
179 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; 197 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
180 198
181 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) 199 if ((png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) != 0)
182 { 200 {
183 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
184 * we must recompile any applications that use any older library version.
185 * For versions after libpng 1.0, we will be compatible, so we need
186 * only check the first and third digits (note that when we reach version
187 * 1.10 we will need to check the fourth symbol, namely user_png_ver[3]).
188 */
189 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
190 (user_png_ver[0] == '1' && (user_png_ver[2] != png_libpng_ver[2] ||
191 user_png_ver[3] != png_libpng_ver[3])) ||
192 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
193 {
194 #ifdef PNG_WARNINGS_SUPPORTED 201 #ifdef PNG_WARNINGS_SUPPORTED
195 size_t pos = 0; 202 size_t pos = 0;
196 char m[128]; 203 char m[128];
197 204
198 pos = png_safecat(m, (sizeof m), pos, 205 pos = png_safecat(m, (sizeof m), pos,
199 "Application built with libpng-"); 206 "Application built with libpng-");
200 pos = png_safecat(m, (sizeof m), pos, user_png_ver); 207 pos = png_safecat(m, (sizeof m), pos, user_png_ver);
201 pos = png_safecat(m, (sizeof m), pos, " but running with "); 208 pos = png_safecat(m, (sizeof m), pos, " but running with ");
202 pos = png_safecat(m, (sizeof m), pos, png_libpng_ver); 209 pos = png_safecat(m, (sizeof m), pos, PNG_LIBPNG_VER_STRING);
203 210 PNG_UNUSED(pos)
204 png_warning(png_ptr, m); 211
212 png_warning(png_ptr, m);
205 #endif 213 #endif
206 214
207 #ifdef PNG_ERROR_NUMBERS_SUPPORTED 215 #ifdef PNG_ERROR_NUMBERS_SUPPORTED
208 png_ptr->flags = 0; 216 png_ptr->flags = 0;
209 #endif 217 #endif
210 218
211 return 0; 219 return 0;
212 }
213 } 220 }
214 221
215 /* Success return. */ 222 /* Success return. */
216 return 1; 223 return 1;
217 } 224 }
218 225
219 /* Generic function to create a png_struct for either read or write - this 226 /* Generic function to create a png_struct for either read or write - this
220 * contains the common initialization. 227 * contains the common initialization.
221 */ 228 */
222 PNG_FUNCTION(png_structp /* PRIVATE */, 229 PNG_FUNCTION(png_structp /* PRIVATE */,
(...skipping 11 matching lines...) Expand all
234 * to be called. 241 * to be called.
235 */ 242 */
236 memset(&create_struct, 0, (sizeof create_struct)); 243 memset(&create_struct, 0, (sizeof create_struct));
237 244
238 /* Added at libpng-1.2.6 */ 245 /* Added at libpng-1.2.6 */
239 # ifdef PNG_USER_LIMITS_SUPPORTED 246 # ifdef PNG_USER_LIMITS_SUPPORTED
240 create_struct.user_width_max = PNG_USER_WIDTH_MAX; 247 create_struct.user_width_max = PNG_USER_WIDTH_MAX;
241 create_struct.user_height_max = PNG_USER_HEIGHT_MAX; 248 create_struct.user_height_max = PNG_USER_HEIGHT_MAX;
242 249
243 # ifdef PNG_USER_CHUNK_CACHE_MAX 250 # ifdef PNG_USER_CHUNK_CACHE_MAX
244 /* Added at libpng-1.2.43 and 1.4.0 */ 251 /* Added at libpng-1.2.43 and 1.4.0 */
245 create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; 252 create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
246 # endif 253 # endif
247 254
248 # ifdef PNG_USER_CHUNK_MALLOC_MAX 255 # ifdef PNG_USER_CHUNK_MALLOC_MAX
249 /* Added at libpng-1.2.43 and 1.4.1, required only for read but exists 256 /* Added at libpng-1.2.43 and 1.4.1, required only for read but exists
250 * in png_struct regardless. 257 * in png_struct regardless.
251 */ 258 */
252 create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; 259 create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
253 # endif 260 # endif
254 # endif 261 # endif
255 262
256 /* The following two API calls simply set fields in png_struct, so it is safe 263 /* The following two API calls simply set fields in png_struct, so it is safe
257 * to do them now even though error handling is not yet set up. 264 * to do them now even though error handling is not yet set up.
258 */ 265 */
259 # ifdef PNG_USER_MEM_SUPPORTED 266 # ifdef PNG_USER_MEM_SUPPORTED
260 png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn); 267 png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn);
268 # else
269 PNG_UNUSED(mem_ptr)
270 PNG_UNUSED(malloc_fn)
271 PNG_UNUSED(free_fn)
261 # endif 272 # endif
262 273
263 /* (*error_fn) can return control to the caller after the error_ptr is set, 274 /* (*error_fn) can return control to the caller after the error_ptr is set,
264 * this will result in a memory leak unless the error_fn does something 275 * this will result in a memory leak unless the error_fn does something
265 * extremely sophisticated. The design lacks merit but is implicit in the 276 * extremely sophisticated. The design lacks merit but is implicit in the
266 * API. 277 * API.
267 */ 278 */
268 png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn); 279 png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn);
269 280
270 # ifdef PNG_SETJMP_SUPPORTED 281 # ifdef PNG_SETJMP_SUPPORTED
271 if (!setjmp(create_jmp_buf)) 282 if (!setjmp(create_jmp_buf))
283 # endif
272 { 284 {
285 # ifdef PNG_SETJMP_SUPPORTED
273 /* Temporarily fake out the longjmp information until we have 286 /* Temporarily fake out the longjmp information until we have
274 * successfully completed this function. This only works if we have 287 * successfully completed this function. This only works if we have
275 * setjmp() support compiled in, but it is safe - this stuff should 288 * setjmp() support compiled in, but it is safe - this stuff should
276 * never happen. 289 * never happen.
277 */ 290 */
278 create_struct.jmp_buf_ptr = &create_jmp_buf; 291 create_struct.jmp_buf_ptr = &create_jmp_buf;
279 create_struct.jmp_buf_size = 0; /*stack allocation*/ 292 create_struct.jmp_buf_size = 0; /*stack allocation*/
280 create_struct.longjmp_fn = longjmp; 293 create_struct.longjmp_fn = longjmp;
281 # else
282 {
283 # endif 294 # endif
284 /* Call the general version checker (shared with read and write code): 295 /* Call the general version checker (shared with read and write code):
285 */ 296 */
286 if (png_user_version_check(&create_struct, user_png_ver)) 297 if (png_user_version_check(&create_struct, user_png_ver) != 0)
287 { 298 {
288 png_structrp png_ptr = png_voidcast(png_structrp, 299 png_structrp png_ptr = png_voidcast(png_structrp,
289 png_malloc_warn(&create_struct, (sizeof *png_ptr))); 300 png_malloc_warn(&create_struct, (sizeof *png_ptr)));
290 301
291 if (png_ptr != NULL) 302 if (png_ptr != NULL)
292 { 303 {
293 /* png_ptr->zstream holds a back-pointer to the png_struct, so 304 /* png_ptr->zstream holds a back-pointer to the png_struct, so
294 * this can only be done now: 305 * this can only be done now:
295 */ 306 */
296 create_struct.zstream.zalloc = png_zalloc; 307 create_struct.zstream.zalloc = png_zalloc;
297 create_struct.zstream.zfree = png_zfree; 308 create_struct.zstream.zfree = png_zfree;
298 create_struct.zstream.opaque = png_ptr; 309 create_struct.zstream.opaque = png_ptr;
299 310
300 # ifdef PNG_SETJMP_SUPPORTED 311 # ifdef PNG_SETJMP_SUPPORTED
301 /* Eliminate the local error handling: */ 312 /* Eliminate the local error handling: */
302 create_struct.jmp_buf_ptr = NULL; 313 create_struct.jmp_buf_ptr = NULL;
303 create_struct.jmp_buf_size = 0; 314 create_struct.jmp_buf_size = 0;
304 create_struct.longjmp_fn = 0; 315 create_struct.longjmp_fn = 0;
305 # endif 316 # endif
306 317
307 *png_ptr = create_struct; 318 *png_ptr = create_struct;
308 319
309 /* This is the successful return point */ 320 /* This is the successful return point */
310 return png_ptr; 321 return png_ptr;
311 } 322 }
312 } 323 }
313 } 324 }
314 325
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 if (info_ptr == NULL) 411 if (info_ptr == NULL)
401 return; 412 return;
402 413
403 if ((sizeof (png_info)) > png_info_struct_size) 414 if ((sizeof (png_info)) > png_info_struct_size)
404 { 415 {
405 *ptr_ptr = NULL; 416 *ptr_ptr = NULL;
406 /* The following line is why this API should not be used: */ 417 /* The following line is why this API should not be used: */
407 free(info_ptr); 418 free(info_ptr);
408 info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL, 419 info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL,
409 (sizeof *info_ptr))); 420 (sizeof *info_ptr)));
421 if (info_ptr == NULL)
422 return;
410 *ptr_ptr = info_ptr; 423 *ptr_ptr = info_ptr;
411 } 424 }
412 425
413 /* Set everything to 0 */ 426 /* Set everything to 0 */
414 memset(info_ptr, 0, (sizeof *info_ptr)); 427 memset(info_ptr, 0, (sizeof *info_ptr));
415 } 428 }
416 429
417 /* The following API is not called internally */ 430 /* The following API is not called internally */
418 void PNGAPI 431 void PNGAPI
419 png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr, 432 png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr,
(...skipping 18 matching lines...) Expand all
438 png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, 451 png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
439 int num) 452 int num)
440 { 453 {
441 png_debug(1, "in png_free_data"); 454 png_debug(1, "in png_free_data");
442 455
443 if (png_ptr == NULL || info_ptr == NULL) 456 if (png_ptr == NULL || info_ptr == NULL)
444 return; 457 return;
445 458
446 #ifdef PNG_TEXT_SUPPORTED 459 #ifdef PNG_TEXT_SUPPORTED
447 /* Free text item num or (if num == -1) all text items */ 460 /* Free text item num or (if num == -1) all text items */
448 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me) 461 if (info_ptr->text != 0 &&
462 ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
449 { 463 {
450 if (num != -1) 464 if (num != -1)
451 { 465 {
452 if (info_ptr->text && info_ptr->text[num].key) 466 png_free(png_ptr, info_ptr->text[num].key);
453 { 467 info_ptr->text[num].key = NULL;
454 png_free(png_ptr, info_ptr->text[num].key);
455 info_ptr->text[num].key = NULL;
456 }
457 } 468 }
458 469
459 else 470 else
460 { 471 {
461 int i; 472 int i;
473
462 for (i = 0; i < info_ptr->num_text; i++) 474 for (i = 0; i < info_ptr->num_text; i++)
463 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i); 475 png_free(png_ptr, info_ptr->text[i].key);
476
464 png_free(png_ptr, info_ptr->text); 477 png_free(png_ptr, info_ptr->text);
465 info_ptr->text = NULL; 478 info_ptr->text = NULL;
466 info_ptr->num_text=0; 479 info_ptr->num_text = 0;
467 } 480 }
468 } 481 }
469 #endif 482 #endif
470 483
471 #ifdef PNG_tRNS_SUPPORTED 484 #ifdef PNG_tRNS_SUPPORTED
472 /* Free any tRNS entry */ 485 /* Free any tRNS entry */
473 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me) 486 if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0)
474 { 487 {
488 info_ptr->valid &= ~PNG_INFO_tRNS;
475 png_free(png_ptr, info_ptr->trans_alpha); 489 png_free(png_ptr, info_ptr->trans_alpha);
476 info_ptr->trans_alpha = NULL; 490 info_ptr->trans_alpha = NULL;
477 info_ptr->valid &= ~PNG_INFO_tRNS; 491 info_ptr->num_trans = 0;
478 } 492 }
479 #endif 493 #endif
480 494
481 #ifdef PNG_sCAL_SUPPORTED 495 #ifdef PNG_sCAL_SUPPORTED
482 /* Free any sCAL entry */ 496 /* Free any sCAL entry */
483 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me) 497 if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0)
484 { 498 {
485 png_free(png_ptr, info_ptr->scal_s_width); 499 png_free(png_ptr, info_ptr->scal_s_width);
486 png_free(png_ptr, info_ptr->scal_s_height); 500 png_free(png_ptr, info_ptr->scal_s_height);
487 info_ptr->scal_s_width = NULL; 501 info_ptr->scal_s_width = NULL;
488 info_ptr->scal_s_height = NULL; 502 info_ptr->scal_s_height = NULL;
489 info_ptr->valid &= ~PNG_INFO_sCAL; 503 info_ptr->valid &= ~PNG_INFO_sCAL;
490 } 504 }
491 #endif 505 #endif
492 506
493 #ifdef PNG_pCAL_SUPPORTED 507 #ifdef PNG_pCAL_SUPPORTED
494 /* Free any pCAL entry */ 508 /* Free any pCAL entry */
495 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me) 509 if (((mask & PNG_FREE_PCAL) & info_ptr->free_me) != 0)
496 { 510 {
497 png_free(png_ptr, info_ptr->pcal_purpose); 511 png_free(png_ptr, info_ptr->pcal_purpose);
498 png_free(png_ptr, info_ptr->pcal_units); 512 png_free(png_ptr, info_ptr->pcal_units);
499 info_ptr->pcal_purpose = NULL; 513 info_ptr->pcal_purpose = NULL;
500 info_ptr->pcal_units = NULL; 514 info_ptr->pcal_units = NULL;
515
501 if (info_ptr->pcal_params != NULL) 516 if (info_ptr->pcal_params != NULL)
502 { 517 {
503 unsigned int i; 518 int i;
519
504 for (i = 0; i < info_ptr->pcal_nparams; i++) 520 for (i = 0; i < info_ptr->pcal_nparams; i++)
505 {
506 png_free(png_ptr, info_ptr->pcal_params[i]); 521 png_free(png_ptr, info_ptr->pcal_params[i]);
507 info_ptr->pcal_params[i] = NULL; 522
508 }
509 png_free(png_ptr, info_ptr->pcal_params); 523 png_free(png_ptr, info_ptr->pcal_params);
510 info_ptr->pcal_params = NULL; 524 info_ptr->pcal_params = NULL;
511 } 525 }
512 info_ptr->valid &= ~PNG_INFO_pCAL; 526 info_ptr->valid &= ~PNG_INFO_pCAL;
513 } 527 }
514 #endif 528 #endif
515 529
516 #ifdef PNG_iCCP_SUPPORTED 530 #ifdef PNG_iCCP_SUPPORTED
517 /* Free any profile entry */ 531 /* Free any profile entry */
518 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me) 532 if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0)
519 { 533 {
520 png_free(png_ptr, info_ptr->iccp_name); 534 png_free(png_ptr, info_ptr->iccp_name);
521 png_free(png_ptr, info_ptr->iccp_profile); 535 png_free(png_ptr, info_ptr->iccp_profile);
522 info_ptr->iccp_name = NULL; 536 info_ptr->iccp_name = NULL;
523 info_ptr->iccp_profile = NULL; 537 info_ptr->iccp_profile = NULL;
524 info_ptr->valid &= ~PNG_INFO_iCCP; 538 info_ptr->valid &= ~PNG_INFO_iCCP;
525 } 539 }
526 #endif 540 #endif
527 541
528 #ifdef PNG_sPLT_SUPPORTED 542 #ifdef PNG_sPLT_SUPPORTED
529 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ 543 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
530 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me) 544 if (info_ptr->splt_palettes != 0 &&
545 ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
531 { 546 {
532 if (num != -1) 547 if (num != -1)
533 { 548 {
534 if (info_ptr->splt_palettes) 549 png_free(png_ptr, info_ptr->splt_palettes[num].name);
535 { 550 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
536 png_free(png_ptr, info_ptr->splt_palettes[num].name); 551 info_ptr->splt_palettes[num].name = NULL;
537 png_free(png_ptr, info_ptr->splt_palettes[num].entries); 552 info_ptr->splt_palettes[num].entries = NULL;
538 info_ptr->splt_palettes[num].name = NULL;
539 info_ptr->splt_palettes[num].entries = NULL;
540 }
541 }
542
543 else
544 {
545 if (info_ptr->splt_palettes_num)
546 {
547 int i;
548 for (i = 0; i < info_ptr->splt_palettes_num; i++)
549 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, (int)i);
550
551 png_free(png_ptr, info_ptr->splt_palettes);
552 info_ptr->splt_palettes = NULL;
553 info_ptr->splt_palettes_num = 0;
554 }
555 info_ptr->valid &= ~PNG_INFO_sPLT;
556 }
557 }
558 #endif
559
560 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
561 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
562 {
563 if (num != -1)
564 {
565 if (info_ptr->unknown_chunks)
566 {
567 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
568 info_ptr->unknown_chunks[num].data = NULL;
569 }
570 } 553 }
571 554
572 else 555 else
573 { 556 {
574 int i; 557 int i;
575 558
576 if (info_ptr->unknown_chunks_num) 559 for (i = 0; i < info_ptr->splt_palettes_num; i++)
577 { 560 {
578 for (i = 0; i < info_ptr->unknown_chunks_num; i++) 561 png_free(png_ptr, info_ptr->splt_palettes[i].name);
579 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, (int)i); 562 png_free(png_ptr, info_ptr->splt_palettes[i].entries);
580
581 png_free(png_ptr, info_ptr->unknown_chunks);
582 info_ptr->unknown_chunks = NULL;
583 info_ptr->unknown_chunks_num = 0;
584 } 563 }
564
565 png_free(png_ptr, info_ptr->splt_palettes);
566 info_ptr->splt_palettes = NULL;
567 info_ptr->splt_palettes_num = 0;
568 info_ptr->valid &= ~PNG_INFO_sPLT;
569 }
570 }
571 #endif
572
573 #ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
574 if (info_ptr->unknown_chunks != 0 &&
575 ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
576 {
577 if (num != -1)
578 {
579 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
580 info_ptr->unknown_chunks[num].data = NULL;
581 }
582
583 else
584 {
585 int i;
586
587 for (i = 0; i < info_ptr->unknown_chunks_num; i++)
588 png_free(png_ptr, info_ptr->unknown_chunks[i].data);
589
590 png_free(png_ptr, info_ptr->unknown_chunks);
591 info_ptr->unknown_chunks = NULL;
592 info_ptr->unknown_chunks_num = 0;
585 } 593 }
586 } 594 }
587 #endif 595 #endif
588 596
589 #ifdef PNG_hIST_SUPPORTED 597 #ifdef PNG_hIST_SUPPORTED
590 /* Free any hIST entry */ 598 /* Free any hIST entry */
591 if ((mask & PNG_FREE_HIST) & info_ptr->free_me) 599 if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0)
592 { 600 {
593 png_free(png_ptr, info_ptr->hist); 601 png_free(png_ptr, info_ptr->hist);
594 info_ptr->hist = NULL; 602 info_ptr->hist = NULL;
595 info_ptr->valid &= ~PNG_INFO_hIST; 603 info_ptr->valid &= ~PNG_INFO_hIST;
596 } 604 }
597 #endif 605 #endif
598 606
599 /* Free any PLTE entry that was internally allocated */ 607 /* Free any PLTE entry that was internally allocated */
600 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me) 608 if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0)
601 { 609 {
602 png_free(png_ptr, info_ptr->palette); 610 png_free(png_ptr, info_ptr->palette);
603 info_ptr->palette = NULL; 611 info_ptr->palette = NULL;
604 info_ptr->valid &= ~PNG_INFO_PLTE; 612 info_ptr->valid &= ~PNG_INFO_PLTE;
605 info_ptr->num_palette = 0; 613 info_ptr->num_palette = 0;
606 } 614 }
607 615
608 #ifdef PNG_INFO_IMAGE_SUPPORTED 616 #ifdef PNG_INFO_IMAGE_SUPPORTED
609 /* Free any image bits attached to the info structure */ 617 /* Free any image bits attached to the info structure */
610 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me) 618 if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
611 { 619 {
612 if (info_ptr->row_pointers) 620 if (info_ptr->row_pointers != 0)
613 { 621 {
614 png_uint_32 row; 622 png_uint_32 row;
615 for (row = 0; row < info_ptr->height; row++) 623 for (row = 0; row < info_ptr->height; row++)
616 {
617 png_free(png_ptr, info_ptr->row_pointers[row]); 624 png_free(png_ptr, info_ptr->row_pointers[row]);
618 info_ptr->row_pointers[row] = NULL; 625
619 }
620 png_free(png_ptr, info_ptr->row_pointers); 626 png_free(png_ptr, info_ptr->row_pointers);
621 info_ptr->row_pointers = NULL; 627 info_ptr->row_pointers = NULL;
622 } 628 }
623 info_ptr->valid &= ~PNG_INFO_IDAT; 629 info_ptr->valid &= ~PNG_INFO_IDAT;
624 } 630 }
625 #endif 631 #endif
626 632
627 if (num != -1) 633 if (num != -1)
628 mask &= ~PNG_FREE_MUL; 634 mask &= ~PNG_FREE_MUL;
629 635
630 info_ptr->free_me &= ~mask; 636 info_ptr->free_me &= ~mask;
631 } 637 }
632 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 638 #endif /* READ || WRITE */
633 639
634 /* This function returns a pointer to the io_ptr associated with the user 640 /* This function returns a pointer to the io_ptr associated with the user
635 * functions. The application should free any memory associated with this 641 * functions. The application should free any memory associated with this
636 * pointer before png_write_destroy() or png_read_destroy() are called. 642 * pointer before png_write_destroy() or png_read_destroy() are called.
637 */ 643 */
638 png_voidp PNGAPI 644 png_voidp PNGAPI
639 png_get_io_ptr(png_const_structrp png_ptr) 645 png_get_io_ptr(png_const_structrp png_ptr)
640 { 646 {
641 if (png_ptr == NULL) 647 if (png_ptr == NULL)
642 return (NULL); 648 return (NULL);
(...skipping 14 matching lines...) Expand all
657 { 663 {
658 png_debug(1, "in png_init_io"); 664 png_debug(1, "in png_init_io");
659 665
660 if (png_ptr == NULL) 666 if (png_ptr == NULL)
661 return; 667 return;
662 668
663 png_ptr->io_ptr = (png_voidp)fp; 669 png_ptr->io_ptr = (png_voidp)fp;
664 } 670 }
665 # endif 671 # endif
666 672
667 #ifdef PNG_SAVE_INT_32_SUPPORTED 673 # ifdef PNG_SAVE_INT_32_SUPPORTED
668 /* The png_save_int_32 function assumes integers are stored in two's 674 /* PNG signed integers are saved in 32-bit 2's complement format. ANSI C-90
669 * complement format. If this isn't the case, then this routine needs to 675 * defines a cast of a signed integer to an unsigned integer either to preserve
670 * be modified to write data in two's complement format. Note that, 676 * the value, if it is positive, or to calculate:
671 * the following works correctly even if png_int_32 has more than 32 bits 677 *
672 * (compare the more complex code required on read for sign extension.) 678 * (UNSIGNED_MAX+1) + integer
679 *
680 * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the
681 * negative integral value is added the result will be an unsigned value
682 * correspnding to the 2's complement representation.
673 */ 683 */
674 void PNGAPI 684 void PNGAPI
675 png_save_int_32(png_bytep buf, png_int_32 i) 685 png_save_int_32(png_bytep buf, png_int_32 i)
676 { 686 {
677 buf[0] = (png_byte)((i >> 24) & 0xff); 687 png_save_uint_32(buf, i);
678 buf[1] = (png_byte)((i >> 16) & 0xff); 688 }
679 buf[2] = (png_byte)((i >> 8) & 0xff); 689 # endif
680 buf[3] = (png_byte)(i & 0xff);
681 }
682 #endif
683 690
684 # ifdef PNG_TIME_RFC1123_SUPPORTED 691 # ifdef PNG_TIME_RFC1123_SUPPORTED
685 /* Convert the supplied time into an RFC 1123 string suitable for use in 692 /* Convert the supplied time into an RFC 1123 string suitable for use in
686 * a "Creation Time" or other text-based time string. 693 * a "Creation Time" or other text-based time string.
687 */ 694 */
688 int PNGAPI 695 int PNGAPI
689 png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime) 696 png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
690 { 697 {
691 static PNG_CONST char short_months[12][4] = 698 static PNG_CONST char short_months[12][4] =
692 {"Jan", "Feb", "Mar", "Apr", "May", "Jun", 699 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
(...skipping 23 matching lines...) Expand all
716 APPEND_STRING(short_months[(ptime->month - 1)]); 723 APPEND_STRING(short_months[(ptime->month - 1)]);
717 APPEND(' '); 724 APPEND(' ');
718 APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year); 725 APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year);
719 APPEND(' '); 726 APPEND(' ');
720 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour); 727 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour);
721 APPEND(':'); 728 APPEND(':');
722 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute); 729 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute);
723 APPEND(':'); 730 APPEND(':');
724 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second); 731 APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second);
725 APPEND_STRING(" +0000"); /* This reliably terminates the buffer */ 732 APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
733 PNG_UNUSED (pos)
726 734
727 # undef APPEND 735 # undef APPEND
728 # undef APPEND_NUMBER 736 # undef APPEND_NUMBER
729 # undef APPEND_STRING 737 # undef APPEND_STRING
730 } 738 }
731 739
732 return 1; 740 return 1;
733 } 741 }
734 742
735 # if PNG_LIBPNG_VER < 10700 743 # if PNG_LIBPNG_VER < 10700
736 /* To do: remove the following from libpng-1.7 */ 744 /* To do: remove the following from libpng-1.7 */
737 /* Original API that uses a private buffer in png_struct. 745 /* Original API that uses a private buffer in png_struct.
738 * Deprecated because it causes png_struct to carry a spurious temporary 746 * Deprecated because it causes png_struct to carry a spurious temporary
739 * buffer (png_struct::time_buffer), better to have the caller pass this in. 747 * buffer (png_struct::time_buffer), better to have the caller pass this in.
740 */ 748 */
741 png_const_charp PNGAPI 749 png_const_charp PNGAPI
742 png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime) 750 png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime)
743 { 751 {
744 if (png_ptr != NULL) 752 if (png_ptr != NULL)
745 { 753 {
746 /* The only failure above if png_ptr != NULL is from an invalid ptime */ 754 /* The only failure above if png_ptr != NULL is from an invalid ptime */
747 if (!png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime)) 755 if (png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime) == 0)
748 png_warning(png_ptr, "Ignoring invalid time value"); 756 png_warning(png_ptr, "Ignoring invalid time value");
749 757
750 else 758 else
751 return png_ptr->time_buffer; 759 return png_ptr->time_buffer;
752 } 760 }
753 761
754 return NULL; 762 return NULL;
755 } 763 }
756 # endif 764 # endif /* LIBPNG_VER < 10700 */
757 # endif /* PNG_TIME_RFC1123_SUPPORTED */ 765 # endif /* TIME_RFC1123 */
758 766
759 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 767 #endif /* READ || WRITE */
760 768
761 png_const_charp PNGAPI 769 png_const_charp PNGAPI
762 png_get_copyright(png_const_structrp png_ptr) 770 png_get_copyright(png_const_structrp png_ptr)
763 { 771 {
764 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ 772 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
765 #ifdef PNG_STRING_COPYRIGHT 773 #ifdef PNG_STRING_COPYRIGHT
766 return PNG_STRING_COPYRIGHT 774 return PNG_STRING_COPYRIGHT
767 #else 775 #else
768 # ifdef __STDC__ 776 # ifdef __STDC__
769 return PNG_STRING_NEWLINE \ 777 return PNG_STRING_NEWLINE \
770 "libpng version 1.6.3 - July 18, 2013" PNG_STRING_NEWLINE \ 778 "libpng version 1.6.20 - December 3, 2015" PNG_STRING_NEWLINE \
771 "Copyright (c) 1998-2013 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \ 779 "Copyright (c) 1998-2015 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
772 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ 780 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
773 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ 781 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
774 PNG_STRING_NEWLINE; 782 PNG_STRING_NEWLINE;
775 # else 783 # else
776 return "libpng version 1.6.3 - July 18, 2013\ 784 return "libpng version 1.6.20 - December 3, 2015\
777 Copyright (c) 1998-2013 Glenn Randers-Pehrson\ 785 Copyright (c) 1998-2015 Glenn Randers-Pehrson\
778 Copyright (c) 1996-1997 Andreas Dilger\ 786 Copyright (c) 1996-1997 Andreas Dilger\
779 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc."; 787 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
780 # endif 788 # endif
781 #endif 789 #endif
782 } 790 }
783 791
784 /* The following return the library version as a short string in the 792 /* The following return the library version as a short string in the
785 * format 1.0.0 through 99.99.99zz. To get the version of *.h files 793 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
786 * used with your application, print out PNG_LIBPNG_VER_STRING, which 794 * used with your application, print out PNG_LIBPNG_VER_STRING, which
787 * is defined in png.h. 795 * is defined in png.h.
(...skipping 17 matching lines...) Expand all
805 } 813 }
806 814
807 png_const_charp PNGAPI 815 png_const_charp PNGAPI
808 png_get_header_version(png_const_structrp png_ptr) 816 png_get_header_version(png_const_structrp png_ptr)
809 { 817 {
810 /* Returns longer string containing both version and date */ 818 /* Returns longer string containing both version and date */
811 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ 819 PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
812 #ifdef __STDC__ 820 #ifdef __STDC__
813 return PNG_HEADER_VERSION_STRING 821 return PNG_HEADER_VERSION_STRING
814 # ifndef PNG_READ_SUPPORTED 822 # ifndef PNG_READ_SUPPORTED
815 " (NO READ SUPPORT)" 823 " (NO READ SUPPORT)"
816 # endif 824 # endif
817 PNG_STRING_NEWLINE; 825 PNG_STRING_NEWLINE;
818 #else 826 #else
819 return PNG_HEADER_VERSION_STRING; 827 return PNG_HEADER_VERSION_STRING;
820 #endif 828 #endif
821 } 829 }
822 830
831 #ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
832 /* NOTE: this routine is not used internally! */
833 /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
834 * large of png_color. This lets grayscale images be treated as
835 * paletted. Most useful for gamma correction and simplification
836 * of code. This API is not used internally.
837 */
838 void PNGAPI
839 png_build_grayscale_palette(int bit_depth, png_colorp palette)
840 {
841 int num_palette;
842 int color_inc;
843 int i;
844 int v;
845
846 png_debug(1, "in png_do_build_grayscale_palette");
847
848 if (palette == NULL)
849 return;
850
851 switch (bit_depth)
852 {
853 case 1:
854 num_palette = 2;
855 color_inc = 0xff;
856 break;
857
858 case 2:
859 num_palette = 4;
860 color_inc = 0x55;
861 break;
862
863 case 4:
864 num_palette = 16;
865 color_inc = 0x11;
866 break;
867
868 case 8:
869 num_palette = 256;
870 color_inc = 1;
871 break;
872
873 default:
874 num_palette = 0;
875 color_inc = 0;
876 break;
877 }
878
879 for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
880 {
881 palette[i].red = (png_byte)(v & 0xff);
882 palette[i].green = (png_byte)(v & 0xff);
883 palette[i].blue = (png_byte)(v & 0xff);
884 }
885 }
886 #endif
887
823 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED 888 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
824 int PNGAPI 889 int PNGAPI
825 png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name) 890 png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name)
826 { 891 {
827 /* Check chunk_name and return "keep" value if it's on the list, else 0 */ 892 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
828 png_const_bytep p, p_end; 893 png_const_bytep p, p_end;
829 894
830 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0) 895 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0)
831 return PNG_HANDLE_CHUNK_AS_DEFAULT; 896 return PNG_HANDLE_CHUNK_AS_DEFAULT;
832 897
833 p_end = png_ptr->chunk_list; 898 p_end = png_ptr->chunk_list;
834 p = p_end + png_ptr->num_chunk_list*5; /* beyond end */ 899 p = p_end + png_ptr->num_chunk_list*5; /* beyond end */
835 900
836 /* The code is the fifth byte after each four byte string. Historically this 901 /* The code is the fifth byte after each four byte string. Historically this
837 * code was always searched from the end of the list, this is no longer 902 * code was always searched from the end of the list, this is no longer
838 * necessary because the 'set' routine handles duplicate entries correcty. 903 * necessary because the 'set' routine handles duplicate entries correcty.
839 */ 904 */
840 do /* num_chunk_list > 0, so at least one */ 905 do /* num_chunk_list > 0, so at least one */
841 { 906 {
842 p -= 5; 907 p -= 5;
843 908
844 if (!memcmp(chunk_name, p, 4)) 909 if (memcmp(chunk_name, p, 4) == 0)
845 return p[4]; 910 return p[4];
846 } 911 }
847 while (p > p_end); 912 while (p > p_end);
848 913
849 /* This means that known chunks should be processed and unknown chunks should 914 /* This means that known chunks should be processed and unknown chunks should
850 * be handled according to the value of png_ptr->unknown_default; this can be 915 * be handled according to the value of png_ptr->unknown_default; this can be
851 * confusing because, as a result, there are two levels of defaulting for 916 * confusing because, as a result, there are two levels of defaulting for
852 * unknown chunks. 917 * unknown chunks.
853 */ 918 */
854 return PNG_HANDLE_CHUNK_AS_DEFAULT; 919 return PNG_HANDLE_CHUNK_AS_DEFAULT;
855 } 920 }
856 921
857 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED 922 #if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\
923 defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
858 int /* PRIVATE */ 924 int /* PRIVATE */
859 png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name) 925 png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name)
860 { 926 {
861 png_byte chunk_string[5]; 927 png_byte chunk_string[5];
862 928
863 PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name); 929 PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name);
864 return png_handle_as_unknown(png_ptr, chunk_string); 930 return png_handle_as_unknown(png_ptr, chunk_string);
865 } 931 }
866 #endif /* READ_UNKNOWN_CHUNKS */ 932 #endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */
867 #endif /* SET_UNKNOWN_CHUNKS */ 933 #endif /* SET_UNKNOWN_CHUNKS */
868 934
869 #ifdef PNG_READ_SUPPORTED 935 #ifdef PNG_READ_SUPPORTED
870 /* This function, added to libpng-1.0.6g, is untested. */ 936 /* This function, added to libpng-1.0.6g, is untested. */
871 int PNGAPI 937 int PNGAPI
872 png_reset_zstream(png_structrp png_ptr) 938 png_reset_zstream(png_structrp png_ptr)
873 { 939 {
874 if (png_ptr == NULL) 940 if (png_ptr == NULL)
875 return Z_STREAM_ERROR; 941 return Z_STREAM_ERROR;
876 942
877 /* WARNING: this resets the window bits to the maximum! */ 943 /* WARNING: this resets the window bits to the maximum! */
878 return (inflateReset(&png_ptr->zstream)); 944 return (inflateReset(&png_ptr->zstream));
879 } 945 }
880 #endif /* PNG_READ_SUPPORTED */ 946 #endif /* READ */
881 947
882 /* This function was added to libpng-1.0.7 */ 948 /* This function was added to libpng-1.0.7 */
883 png_uint_32 PNGAPI 949 png_uint_32 PNGAPI
884 png_access_version_number(void) 950 png_access_version_number(void)
885 { 951 {
886 /* Version of *.c files used when building libpng */ 952 /* Version of *.c files used when building libpng */
887 return((png_uint_32)PNG_LIBPNG_VER); 953 return((png_uint_32)PNG_LIBPNG_VER);
888 } 954 }
889 955
890
891
892 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) 956 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
893 /* Ensure that png_ptr->zstream.msg holds some appropriate error message string. 957 /* Ensure that png_ptr->zstream.msg holds some appropriate error message string.
894 * If it doesn't 'ret' is used to set it to something appropriate, even in cases 958 * If it doesn't 'ret' is used to set it to something appropriate, even in cases
895 * like Z_OK or Z_STREAM_END where the error code is apparently a success code. 959 * like Z_OK or Z_STREAM_END where the error code is apparently a success code.
896 */ 960 */
897 void /* PRIVATE */ 961 void /* PRIVATE */
898 png_zstream_error(png_structrp png_ptr, int ret) 962 png_zstream_error(png_structrp png_ptr, int ret)
899 { 963 {
900 /* Translate 'ret' into an appropriate error string, priority is given to the 964 /* Translate 'ret' into an appropriate error string, priority is given to the
901 * one in zstream if set. This always returns a string, even in cases like 965 * one in zstream if set. This always returns a string, even in cases like
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
975 * 'from' says where the new gamma value comes from: 1039 * 'from' says where the new gamma value comes from:
976 * 1040 *
977 * 0: the new gamma value is the libpng estimate for an ICC profile 1041 * 0: the new gamma value is the libpng estimate for an ICC profile
978 * 1: the new gamma value comes from a gAMA chunk 1042 * 1: the new gamma value comes from a gAMA chunk
979 * 2: the new gamma value comes from an sRGB chunk 1043 * 2: the new gamma value comes from an sRGB chunk
980 */ 1044 */
981 { 1045 {
982 png_fixed_point gtest; 1046 png_fixed_point gtest;
983 1047
984 if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && 1048 if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&
985 (!png_muldiv(&gtest, colorspace->gamma, PNG_FP_1, gAMA) || 1049 (png_muldiv(&gtest, colorspace->gamma, PNG_FP_1, gAMA) == 0 ||
986 png_gamma_significant(gtest))) 1050 png_gamma_significant(gtest) != 0))
987 { 1051 {
988 /* Either this is an sRGB image, in which case the calculated gamma 1052 /* Either this is an sRGB image, in which case the calculated gamma
989 * approximation should match, or this is an image with a profile and the 1053 * approximation should match, or this is an image with a profile and the
990 * value libpng calculates for the gamma of the profile does not match the 1054 * value libpng calculates for the gamma of the profile does not match the
991 * value recorded in the file. The former, sRGB, case is an error, the 1055 * value recorded in the file. The former, sRGB, case is an error, the
992 * latter is just a warning. 1056 * latter is just a warning.
993 */ 1057 */
994 if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2) 1058 if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2)
995 { 1059 {
996 png_chunk_report(png_ptr, "gamma value does not match sRGB", 1060 png_chunk_report(png_ptr, "gamma value does not match sRGB",
(...skipping 11 matching lines...) Expand all
1008 } 1072 }
1009 1073
1010 return 1; 1074 return 1;
1011 } 1075 }
1012 1076
1013 void /* PRIVATE */ 1077 void /* PRIVATE */
1014 png_colorspace_set_gamma(png_const_structrp png_ptr, 1078 png_colorspace_set_gamma(png_const_structrp png_ptr,
1015 png_colorspacerp colorspace, png_fixed_point gAMA) 1079 png_colorspacerp colorspace, png_fixed_point gAMA)
1016 { 1080 {
1017 /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't 1081 /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
1018 * occur. Since the fixed point representation is assymetrical it is 1082 * occur. Since the fixed point representation is asymetrical it is
1019 * possible for 1/gamma to overflow the limit of 21474 and this means the 1083 * possible for 1/gamma to overflow the limit of 21474 and this means the
1020 * gamma value must be at least 5/100000 and hence at most 20000.0. For 1084 * gamma value must be at least 5/100000 and hence at most 20000.0. For
1021 * safety the limits here are a little narrower. The values are 0.00016 to 1085 * safety the limits here are a little narrower. The values are 0.00016 to
1022 * 6250.0, which are truly ridiculous gamma values (and will produce 1086 * 6250.0, which are truly ridiculous gamma values (and will produce
1023 * displays that are all black or all white.) 1087 * displays that are all black or all white.)
1024 * 1088 *
1025 * In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk 1089 * In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk
1026 * handling code, which only required the value to be >0. 1090 * handling code, which only required the value to be >0.
1027 */ 1091 */
1028 png_const_charp errmsg; 1092 png_const_charp errmsg;
1029 1093
1030 if (gAMA < 16 || gAMA > 625000000) 1094 if (gAMA < 16 || gAMA > 625000000)
1031 errmsg = "gamma value out of range"; 1095 errmsg = "gamma value out of range";
1032 1096
1033 # ifdef PNG_READ_gAMA_SUPPORTED 1097 # ifdef PNG_READ_gAMA_SUPPORTED
1034 /* Allow the application to set the gamma value more than once */ 1098 /* Allow the application to set the gamma value more than once */
1035 else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && 1099 else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
1036 (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0) 1100 (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0)
1037 errmsg = "duplicate"; 1101 errmsg = "duplicate";
1038 # endif 1102 # endif
1039 1103
1040 /* Do nothing if the colorspace is already invalid */ 1104 /* Do nothing if the colorspace is already invalid */
1041 else if (colorspace->flags & PNG_COLORSPACE_INVALID) 1105 else if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
1042 return; 1106 return;
1043 1107
1044 else 1108 else
1045 { 1109 {
1046 if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA, 1/*from gAMA*/)) 1110 if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA,
1111 1/*from gAMA*/) != 0)
1047 { 1112 {
1048 /* Store this gamma value. */ 1113 /* Store this gamma value. */
1049 colorspace->gamma = gAMA; 1114 colorspace->gamma = gAMA;
1050 colorspace->flags |= 1115 colorspace->flags |=
1051 (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA); 1116 (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA);
1052 } 1117 }
1053 1118
1054 /* At present if the check_gamma test fails the gamma of the colorspace is 1119 /* At present if the check_gamma test fails the gamma of the colorspace is
1055 * not updated however the colorspace is not invalidated. This 1120 * not updated however the colorspace is not invalidated. This
1056 * corresponds to the case where the existing gamma comes from an sRGB 1121 * corresponds to the case where the existing gamma comes from an sRGB
1057 * chunk or profile. An error message has already been output. 1122 * chunk or profile. An error message has already been output.
1058 */ 1123 */
1059 return; 1124 return;
1060 } 1125 }
1061 1126
1062 /* Error exit - errmsg has been set. */ 1127 /* Error exit - errmsg has been set. */
1063 colorspace->flags |= PNG_COLORSPACE_INVALID; 1128 colorspace->flags |= PNG_COLORSPACE_INVALID;
1064 png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR); 1129 png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR);
1065 } 1130 }
1066 1131
1067 void /* PRIVATE */ 1132 void /* PRIVATE */
1068 png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr) 1133 png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr)
1069 { 1134 {
1070 if (info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) 1135 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1071 { 1136 {
1072 /* Everything is invalid */ 1137 /* Everything is invalid */
1073 info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB| 1138 info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB|
1074 PNG_INFO_iCCP); 1139 PNG_INFO_iCCP);
1075 1140
1076 # ifdef PNG_COLORSPACE_SUPPORTED 1141 # ifdef PNG_COLORSPACE_SUPPORTED
1077 /* Clean up the iCCP profile now if it won't be used. */ 1142 /* Clean up the iCCP profile now if it won't be used. */
1078 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/); 1143 png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/);
1079 # else 1144 # else
1080 PNG_UNUSED(png_ptr) 1145 PNG_UNUSED(png_ptr)
1081 # endif 1146 # endif
1082 } 1147 }
1083 1148
1084 else 1149 else
1085 { 1150 {
1086 # ifdef PNG_COLORSPACE_SUPPORTED 1151 # ifdef PNG_COLORSPACE_SUPPORTED
1087 /* Leave the INFO_iCCP flag set if the pngset.c code has already set 1152 /* Leave the INFO_iCCP flag set if the pngset.c code has already set
1088 * it; this allows a PNG to contain a profile which matches sRGB and 1153 * it; this allows a PNG to contain a profile which matches sRGB and
1089 * yet still have that profile retrievable by the application. 1154 * yet still have that profile retrievable by the application.
1090 */ 1155 */
1091 if (info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB) 1156 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB) != 0)
1092 info_ptr->valid |= PNG_INFO_sRGB; 1157 info_ptr->valid |= PNG_INFO_sRGB;
1093 1158
1094 else 1159 else
1095 info_ptr->valid &= ~PNG_INFO_sRGB; 1160 info_ptr->valid &= ~PNG_INFO_sRGB;
1096 1161
1097 if (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) 1162 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
1098 info_ptr->valid |= PNG_INFO_cHRM; 1163 info_ptr->valid |= PNG_INFO_cHRM;
1099 1164
1100 else 1165 else
1101 info_ptr->valid &= ~PNG_INFO_cHRM; 1166 info_ptr->valid &= ~PNG_INFO_cHRM;
1102 # endif 1167 # endif
1103 1168
1104 if (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) 1169 if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0)
1105 info_ptr->valid |= PNG_INFO_gAMA; 1170 info_ptr->valid |= PNG_INFO_gAMA;
1106 1171
1107 else 1172 else
1108 info_ptr->valid &= ~PNG_INFO_gAMA; 1173 info_ptr->valid &= ~PNG_INFO_gAMA;
1109 } 1174 }
1110 } 1175 }
1111 1176
1112 #ifdef PNG_READ_SUPPORTED 1177 #ifdef PNG_READ_SUPPORTED
1113 void /* PRIVATE */ 1178 void /* PRIVATE */
1114 png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr) 1179 png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
1115 { 1180 {
1116 if (info_ptr == NULL) /* reduce code size; check here not in the caller */ 1181 if (info_ptr == NULL) /* reduce code size; check here not in the caller */
1117 return; 1182 return;
1118 1183
1119 info_ptr->colorspace = png_ptr->colorspace; 1184 info_ptr->colorspace = png_ptr->colorspace;
1120 png_colorspace_sync_info(png_ptr, info_ptr); 1185 png_colorspace_sync_info(png_ptr, info_ptr);
1121 } 1186 }
1122 #endif 1187 #endif
1123 #endif 1188 #endif /* GAMMA */
1124 1189
1125 #ifdef PNG_COLORSPACE_SUPPORTED 1190 #ifdef PNG_COLORSPACE_SUPPORTED
1126 /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for 1191 /* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for
1127 * cHRM, as opposed to using chromaticities. These internal APIs return 1192 * cHRM, as opposed to using chromaticities. These internal APIs return
1128 * non-zero on a parameter error. The X, Y and Z values are required to be 1193 * non-zero on a parameter error. The X, Y and Z values are required to be
1129 * positive and less than 1.0. 1194 * positive and less than 1.0.
1130 */ 1195 */
1131 static int 1196 static int
1132 png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ) 1197 png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ)
1133 { 1198 {
1134 png_int_32 d, dwhite, whiteX, whiteY; 1199 png_int_32 d, dwhite, whiteX, whiteY;
1135 1200
1136 d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z; 1201 d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z;
1137 if (!png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d)) return 1; 1202 if (png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d) == 0)
1138 if (!png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d)) return 1; 1203 return 1;
1204 if (png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d) == 0)
1205 return 1;
1139 dwhite = d; 1206 dwhite = d;
1140 whiteX = XYZ->red_X; 1207 whiteX = XYZ->red_X;
1141 whiteY = XYZ->red_Y; 1208 whiteY = XYZ->red_Y;
1142 1209
1143 d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z; 1210 d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z;
1144 if (!png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d)) return 1; 1211 if (png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d) == 0)
1145 if (!png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d)) return 1; 1212 return 1;
1213 if (png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d) == 0)
1214 return 1;
1146 dwhite += d; 1215 dwhite += d;
1147 whiteX += XYZ->green_X; 1216 whiteX += XYZ->green_X;
1148 whiteY += XYZ->green_Y; 1217 whiteY += XYZ->green_Y;
1149 1218
1150 d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z; 1219 d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z;
1151 if (!png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d)) return 1; 1220 if (png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d) == 0)
1152 if (!png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d)) return 1; 1221 return 1;
1222 if (png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d) == 0)
1223 return 1;
1153 dwhite += d; 1224 dwhite += d;
1154 whiteX += XYZ->blue_X; 1225 whiteX += XYZ->blue_X;
1155 whiteY += XYZ->blue_Y; 1226 whiteY += XYZ->blue_Y;
1156 1227
1157 /* The reference white is simply the sum of the end-point (X,Y,Z) vectors, 1228 /* The reference white is simply the sum of the end-point (X,Y,Z) vectors,
1158 * thus: 1229 * thus:
1159 */ 1230 */
1160 if (!png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite)) return 1; 1231 if (png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite) == 0)
1161 if (!png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite)) return 1; 1232 return 1;
1233 if (png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite) == 0)
1234 return 1;
1162 1235
1163 return 0; 1236 return 0;
1164 } 1237 }
1165 1238
1166 static int 1239 static int
1167 png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy) 1240 png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy)
1168 { 1241 {
1169 png_fixed_point red_inverse, green_inverse, blue_scale; 1242 png_fixed_point red_inverse, green_inverse, blue_scale;
1170 png_fixed_point left, right, denominator; 1243 png_fixed_point left, right, denominator;
1171 1244
1172 /* Check xy and, implicitly, z. Note that wide gamut color spaces typically 1245 /* Check xy and, implicitly, z. Note that wide gamut color spaces typically
1173 * have end points with 0 tristimulus values (these are impossible end 1246 * have end points with 0 tristimulus values (these are impossible end
1174 * points, but they are used to cover the possible colors.) 1247 * points, but they are used to cover the possible colors). We check
1175 */ 1248 * xy->whitey against 5, not 0, to avoid a possible integer overflow.
1176 if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1; 1249 */
1177 if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1; 1250 if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1;
1251 if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
1178 if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1; 1252 if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1;
1179 if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1; 1253 if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1;
1180 if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1; 1254 if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1;
1181 if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1; 1255 if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
1182 if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1; 1256 if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1;
1183 if (xy->whitey < 0 || xy->whitey > PNG_FP_1-xy->whitex) return 1; 1257 if (xy->whitey < 5 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
1184 1258
1185 /* The reverse calculation is more difficult because the original tristimulus 1259 /* The reverse calculation is more difficult because the original tristimulus
1186 * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8 1260 * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8
1187 * derived values were recorded in the cHRM chunk; 1261 * derived values were recorded in the cHRM chunk;
1188 * (red,green,blue,white)x(x,y). This loses one degree of freedom and 1262 * (red,green,blue,white)x(x,y). This loses one degree of freedom and
1189 * therefore an arbitrary ninth value has to be introduced to undo the 1263 * therefore an arbitrary ninth value has to be introduced to undo the
1190 * original transformations. 1264 * original transformations.
1191 * 1265 *
1192 * Think of the original end-points as points in (X,Y,Z) space. The 1266 * Think of the original end-points as points in (X,Y,Z) space. The
1193 * chromaticity values (c) have the property: 1267 * chromaticity values (c) have the property:
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
1248 * and it is certain that it becomes unstable where the end points are close 1322 * and it is certain that it becomes unstable where the end points are close
1249 * together. 1323 * together.
1250 * 1324 *
1251 * So this code uses the perhaps slightly less optimal but more 1325 * So this code uses the perhaps slightly less optimal but more
1252 * understandable and totally obvious approach of calculating color-scale. 1326 * understandable and totally obvious approach of calculating color-scale.
1253 * 1327 *
1254 * This algorithm depends on the precision in white-scale and that is 1328 * This algorithm depends on the precision in white-scale and that is
1255 * (1/white-y), so we can immediately see that as white-y approaches 0 the 1329 * (1/white-y), so we can immediately see that as white-y approaches 0 the
1256 * accuracy inherent in the cHRM chunk drops off substantially. 1330 * accuracy inherent in the cHRM chunk drops off substantially.
1257 * 1331 *
1258 * libpng arithmetic: a simple invertion of the above equations 1332 * libpng arithmetic: a simple inversion of the above equations
1259 * ------------------------------------------------------------ 1333 * ------------------------------------------------------------
1260 * 1334 *
1261 * white_scale = 1/white-y 1335 * white_scale = 1/white-y
1262 * white-X = white-x * white-scale 1336 * white-X = white-x * white-scale
1263 * white-Y = 1.0 1337 * white-Y = 1.0
1264 * white-Z = (1 - white-x - white-y) * white_scale 1338 * white-Z = (1 - white-x - white-y) * white_scale
1265 * 1339 *
1266 * white-C = red-C + green-C + blue-C 1340 * white-C = red-C + green-C + blue-C
1267 * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale 1341 * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
1268 * 1342 *
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1354 * Kodak ProPhoto 1428 * Kodak ProPhoto
1355 * 0.288071128229293 0.711843217810102 0.000085653960605 1429 * 0.288071128229293 0.711843217810102 0.000085653960605
1356 * Adobe RGB 1430 * Adobe RGB
1357 * 0.297344975250536 0.627363566255466 0.075291458493998 1431 * 0.297344975250536 0.627363566255466 0.075291458493998
1358 * Adobe Wide Gamut RGB 1432 * Adobe Wide Gamut RGB
1359 * 0.258728243040113 0.724682314948566 0.016589442011321 1433 * 0.258728243040113 0.724682314948566 0.016589442011321
1360 */ 1434 */
1361 /* By the argument, above overflow should be impossible here. The return 1435 /* By the argument, above overflow should be impossible here. The return
1362 * value of 2 indicates an internal error to the caller. 1436 * value of 2 indicates an internal error to the caller.
1363 */ 1437 */
1364 if (!png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7)) 1438 if (png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7) == 0)
1365 return 2; 1439 return 2;
1366 if (!png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7)) 1440 if (png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7) == 0)
1367 return 2; 1441 return 2;
1368 denominator = left - right; 1442 denominator = left - right;
1369 1443
1370 /* Now find the red numerator. */ 1444 /* Now find the red numerator. */
1371 if (!png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7)) 1445 if (png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
1372 return 2; 1446 return 2;
1373 if (!png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7)) 1447 if (png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
1374 return 2; 1448 return 2;
1375 1449
1376 /* Overflow is possible here and it indicates an extreme set of PNG cHRM 1450 /* Overflow is possible here and it indicates an extreme set of PNG cHRM
1377 * chunk values. This calculation actually returns the reciprocal of the 1451 * chunk values. This calculation actually returns the reciprocal of the
1378 * scale value because this allows us to delay the multiplication of white-y 1452 * scale value because this allows us to delay the multiplication of white-y
1379 * into the denominator, which tends to produce a small number. 1453 * into the denominator, which tends to produce a small number.
1380 */ 1454 */
1381 if (!png_muldiv(&red_inverse, xy->whitey, denominator, left-right) || 1455 if (png_muldiv(&red_inverse, xy->whitey, denominator, left-right) == 0 ||
1382 red_inverse <= xy->whitey /* r+g+b scales = white scale */) 1456 red_inverse <= xy->whitey /* r+g+b scales = white scale */)
1383 return 1; 1457 return 1;
1384 1458
1385 /* Similarly for green_inverse: */ 1459 /* Similarly for green_inverse: */
1386 if (!png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7)) 1460 if (png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
1387 return 2; 1461 return 2;
1388 if (!png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7)) 1462 if (png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
1389 return 2; 1463 return 2;
1390 if (!png_muldiv(&green_inverse, xy->whitey, denominator, left-right) || 1464 if (png_muldiv(&green_inverse, xy->whitey, denominator, left-right) == 0 ||
1391 green_inverse <= xy->whitey) 1465 green_inverse <= xy->whitey)
1392 return 1; 1466 return 1;
1393 1467
1394 /* And the blue scale, the checks above guarantee this can't overflow but it 1468 /* And the blue scale, the checks above guarantee this can't overflow but it
1395 * can still produce 0 for extreme cHRM values. 1469 * can still produce 0 for extreme cHRM values.
1396 */ 1470 */
1397 blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) - 1471 blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) -
1398 png_reciprocal(green_inverse); 1472 png_reciprocal(green_inverse);
1399 if (blue_scale <= 0) return 1; 1473 if (blue_scale <= 0)
1474 return 1;
1400 1475
1401 1476
1402 /* And fill in the png_XYZ: */ 1477 /* And fill in the png_XYZ: */
1403 if (!png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse)) return 1; 1478 if (png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse) == 0)
1404 if (!png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse)) return 1;
1405 if (!png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1,
1406 red_inverse))
1407 return 1; 1479 return 1;
1408 1480 if (png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse) == 0)
1409 if (!png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse))
1410 return 1; 1481 return 1;
1411 if (!png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse)) 1482 if (png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1,
1483 red_inverse) == 0)
1412 return 1; 1484 return 1;
1413 if (!png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1, 1485
1414 green_inverse)) 1486 if (png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse) == 0)
1415 return 1; 1487 return 1;
1416 1488 if (png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse) == 0)
1417 if (!png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1)) return 1; 1489 return 1;
1418 if (!png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1)) return 1; 1490 if (png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1,
1419 if (!png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale, 1491 green_inverse) == 0)
1420 PNG_FP_1)) 1492 return 1;
1493
1494 if (png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1) == 0)
1495 return 1;
1496 if (png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1) == 0)
1497 return 1;
1498 if (png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale,
1499 PNG_FP_1) == 0)
1421 return 1; 1500 return 1;
1422 1501
1423 return 0; /*success*/ 1502 return 0; /*success*/
1424 } 1503 }
1425 1504
1426 static int 1505 static int
1427 png_XYZ_normalize(png_XYZ *XYZ) 1506 png_XYZ_normalize(png_XYZ *XYZ)
1428 { 1507 {
1429 png_int_32 Y; 1508 png_int_32 Y;
1430 1509
1431 if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 || 1510 if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 ||
1432 XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 || 1511 XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 ||
1433 XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0) 1512 XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0)
1434 return 1; 1513 return 1;
1435 1514
1436 /* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1. 1515 /* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1.
1437 * IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore 1516 * IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore
1438 * relying on addition of two positive values producing a negative one is not 1517 * relying on addition of two positive values producing a negative one is not
1439 * safe. 1518 * safe.
1440 */ 1519 */
1441 Y = XYZ->red_Y; 1520 Y = XYZ->red_Y;
1442 if (0x7fffffff - Y < XYZ->green_X) return 1; 1521 if (0x7fffffff - Y < XYZ->green_X)
1522 return 1;
1443 Y += XYZ->green_Y; 1523 Y += XYZ->green_Y;
1444 if (0x7fffffff - Y < XYZ->blue_X) return 1; 1524 if (0x7fffffff - Y < XYZ->blue_X)
1525 return 1;
1445 Y += XYZ->blue_Y; 1526 Y += XYZ->blue_Y;
1446 1527
1447 if (Y != PNG_FP_1) 1528 if (Y != PNG_FP_1)
1448 { 1529 {
1449 if (!png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y)) return 1; 1530 if (png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y) == 0)
1450 if (!png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y)) return 1; 1531 return 1;
1451 if (!png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y)) return 1; 1532 if (png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y) == 0)
1452 1533 return 1;
1453 if (!png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y)) return 1; 1534 if (png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y) == 0)
1454 if (!png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y)) return 1; 1535 return 1;
1455 if (!png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y)) return 1; 1536
1456 1537 if (png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y) == 0)
1457 if (!png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y)) return 1; 1538 return 1;
1458 if (!png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y)) return 1; 1539 if (png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y) == 0)
1459 if (!png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y)) return 1; 1540 return 1;
1541 if (png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y) == 0)
1542 return 1;
1543
1544 if (png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y) == 0)
1545 return 1;
1546 if (png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y) == 0)
1547 return 1;
1548 if (png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y) == 0)
1549 return 1;
1460 } 1550 }
1461 1551
1462 return 0; 1552 return 0;
1463 } 1553 }
1464 1554
1465 static int 1555 static int
1466 png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta) 1556 png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
1467 { 1557 {
1468 /* Allow an error of +/-0.01 (absolute value) on each chromaticity */ 1558 /* Allow an error of +/-0.01 (absolute value) on each chromaticity */
1469 return !(PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) || 1559 if (PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) ||
1470 PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) || 1560 PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) ||
1471 PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) || 1561 PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) ||
1472 PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) || 1562 PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) ||
1473 PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) || 1563 PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) ||
1474 PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) || 1564 PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) ||
1475 PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) || 1565 PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) ||
1476 PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta)); 1566 PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta))
1567 return 0;
1568 return 1;
1477 } 1569 }
1478 1570
1479 /* Added in libpng-1.6.0, a different check for the validity of a set of cHRM 1571 /* Added in libpng-1.6.0, a different check for the validity of a set of cHRM
1480 * chunk chromaticities. Earlier checks used to simply look for the overflow 1572 * chunk chromaticities. Earlier checks used to simply look for the overflow
1481 * condition (where the determinant of the matrix to solve for XYZ ends up zero 1573 * condition (where the determinant of the matrix to solve for XYZ ends up zero
1482 * because the chromaticity values are not all distinct.) Despite this it is 1574 * because the chromaticity values are not all distinct.) Despite this it is
1483 * theoretically possible to produce chromaticities that are apparently valid 1575 * theoretically possible to produce chromaticities that are apparently valid
1484 * but that rapidly degrade to invalid, potentially crashing, sets because of 1576 * but that rapidly degrade to invalid, potentially crashing, sets because of
1485 * arithmetic inaccuracies when calculations are performed on them. The new 1577 * arithmetic inaccuracies when calculations are performed on them. The new
1486 * check is to round-trip xy -> XYZ -> xy and then check that the result is 1578 * check is to round-trip xy -> XYZ -> xy and then check that the result is
1487 * within a small percentage of the original. 1579 * within a small percentage of the original.
1488 */ 1580 */
1489 static int 1581 static int
1490 png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy) 1582 png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy)
1491 { 1583 {
1492 int result; 1584 int result;
1493 png_xy xy_test; 1585 png_xy xy_test;
1494 1586
1495 /* As a side-effect this routine also returns the XYZ endpoints. */ 1587 /* As a side-effect this routine also returns the XYZ endpoints. */
1496 result = png_XYZ_from_xy(XYZ, xy); 1588 result = png_XYZ_from_xy(XYZ, xy);
1497 if (result) return result; 1589 if (result != 0)
1590 return result;
1498 1591
1499 result = png_xy_from_XYZ(&xy_test, XYZ); 1592 result = png_xy_from_XYZ(&xy_test, XYZ);
1500 if (result) return result; 1593 if (result != 0)
1594 return result;
1501 1595
1502 if (png_colorspace_endpoints_match(xy, &xy_test, 1596 if (png_colorspace_endpoints_match(xy, &xy_test,
1503 5/*actually, the math is pretty accurate*/)) 1597 5/*actually, the math is pretty accurate*/) != 0)
1504 return 0; 1598 return 0;
1505 1599
1506 /* Too much slip */ 1600 /* Too much slip */
1507 return 1; 1601 return 1;
1508 } 1602 }
1509 1603
1510 /* This is the check going the other way. The XYZ is modified to normalize it 1604 /* This is the check going the other way. The XYZ is modified to normalize it
1511 * (another side-effect) and the xy chromaticities are returned. 1605 * (another side-effect) and the xy chromaticities are returned.
1512 */ 1606 */
1513 static int 1607 static int
1514 png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ) 1608 png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ)
1515 { 1609 {
1516 int result; 1610 int result;
1517 png_XYZ XYZtemp; 1611 png_XYZ XYZtemp;
1518 1612
1519 result = png_XYZ_normalize(XYZ); 1613 result = png_XYZ_normalize(XYZ);
1520 if (result) return result; 1614 if (result != 0)
1615 return result;
1521 1616
1522 result = png_xy_from_XYZ(xy, XYZ); 1617 result = png_xy_from_XYZ(xy, XYZ);
1523 if (result) return result; 1618 if (result != 0)
1619 return result;
1524 1620
1525 XYZtemp = *XYZ; 1621 XYZtemp = *XYZ;
1526 return png_colorspace_check_xy(&XYZtemp, xy); 1622 return png_colorspace_check_xy(&XYZtemp, xy);
1527 } 1623 }
1528 1624
1529 /* Used to check for an endpoint match against sRGB */ 1625 /* Used to check for an endpoint match against sRGB */
1530 static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */ 1626 static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
1531 { 1627 {
1532 /* color x y */ 1628 /* color x y */
1533 /* red */ 64000, 33000, 1629 /* red */ 64000, 33000,
1534 /* green */ 30000, 60000, 1630 /* green */ 30000, 60000,
1535 /* blue */ 15000, 6000, 1631 /* blue */ 15000, 6000,
1536 /* white */ 31270, 32900 1632 /* white */ 31270, 32900
1537 }; 1633 };
1538 1634
1539 static int 1635 static int
1540 png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr, 1636 png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr,
1541 png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ, 1637 png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ,
1542 int preferred) 1638 int preferred)
1543 { 1639 {
1544 if (colorspace->flags & PNG_COLORSPACE_INVALID) 1640 if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
1545 return 0; 1641 return 0;
1546 1642
1547 /* The consistency check is performed on the chromaticities; this factors out 1643 /* The consistency check is performed on the chromaticities; this factors out
1548 * variations because of the normalization (or not) of the end point Y 1644 * variations because of the normalization (or not) of the end point Y
1549 * values. 1645 * values.
1550 */ 1646 */
1551 if (preferred < 2 && (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS)) 1647 if (preferred < 2 &&
1648 (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
1552 { 1649 {
1553 /* The end points must be reasonably close to any we already have. The 1650 /* The end points must be reasonably close to any we already have. The
1554 * following allows an error of up to +/-.001 1651 * following allows an error of up to +/-.001
1555 */ 1652 */
1556 if (!png_colorspace_endpoints_match(xy, &colorspace->end_points_xy, 100)) 1653 if (png_colorspace_endpoints_match(xy, &colorspace->end_points_xy,
1654 100) == 0)
1557 { 1655 {
1558 colorspace->flags |= PNG_COLORSPACE_INVALID; 1656 colorspace->flags |= PNG_COLORSPACE_INVALID;
1559 png_benign_error(png_ptr, "inconsistent chromaticities"); 1657 png_benign_error(png_ptr, "inconsistent chromaticities");
1560 return 0; /* failed */ 1658 return 0; /* failed */
1561 } 1659 }
1562 1660
1563 /* Only overwrite with preferred values */ 1661 /* Only overwrite with preferred values */
1564 if (!preferred) 1662 if (preferred == 0)
1565 return 1; /* ok, but no change */ 1663 return 1; /* ok, but no change */
1566 } 1664 }
1567 1665
1568 colorspace->end_points_xy = *xy; 1666 colorspace->end_points_xy = *xy;
1569 colorspace->end_points_XYZ = *XYZ; 1667 colorspace->end_points_XYZ = *XYZ;
1570 colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS; 1668 colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS;
1571 1669
1572 /* The end points are normally quoted to two decimal digits, so allow +/-0.01 1670 /* The end points are normally quoted to two decimal digits, so allow +/-0.01
1573 * on this test. 1671 * on this test.
1574 */ 1672 */
1575 if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000)) 1673 if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000) != 0)
1576 colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB; 1674 colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB;
1577 1675
1578 else 1676 else
1579 colorspace->flags &= PNG_COLORSPACE_CANCEL( 1677 colorspace->flags &= PNG_COLORSPACE_CANCEL(
1580 PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB); 1678 PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
1581 1679
1582 return 2; /* ok and changed */ 1680 return 2; /* ok and changed */
1583 } 1681 }
1584 1682
1585 int /* PRIVATE */ 1683 int /* PRIVATE */
(...skipping 21 matching lines...) Expand all
1607 colorspace->flags |= PNG_COLORSPACE_INVALID; 1705 colorspace->flags |= PNG_COLORSPACE_INVALID;
1608 png_benign_error(png_ptr, "invalid chromaticities"); 1706 png_benign_error(png_ptr, "invalid chromaticities");
1609 break; 1707 break;
1610 1708
1611 default: 1709 default:
1612 /* libpng is broken; this should be a warning but if it happens we 1710 /* libpng is broken; this should be a warning but if it happens we
1613 * want error reports so for the moment it is an error. 1711 * want error reports so for the moment it is an error.
1614 */ 1712 */
1615 colorspace->flags |= PNG_COLORSPACE_INVALID; 1713 colorspace->flags |= PNG_COLORSPACE_INVALID;
1616 png_error(png_ptr, "internal error checking chromaticities"); 1714 png_error(png_ptr, "internal error checking chromaticities");
1617 break;
1618 } 1715 }
1619 1716
1620 return 0; /* failed */ 1717 return 0; /* failed */
1621 } 1718 }
1622 1719
1623 int /* PRIVATE */ 1720 int /* PRIVATE */
1624 png_colorspace_set_endpoints(png_const_structrp png_ptr, 1721 png_colorspace_set_endpoints(png_const_structrp png_ptr,
1625 png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred) 1722 png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred)
1626 { 1723 {
1627 png_XYZ XYZ = *XYZ_in; 1724 png_XYZ XYZ = *XYZ_in;
1628 png_xy xy; 1725 png_xy xy;
1629 1726
1630 switch (png_colorspace_check_XYZ(&xy, &XYZ)) 1727 switch (png_colorspace_check_XYZ(&xy, &XYZ))
1631 { 1728 {
1632 case 0: 1729 case 0:
1633 return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ, 1730 return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ,
1634 preferred); 1731 preferred);
1635 1732
1636 case 1: 1733 case 1:
1637 /* End points are invalid. */ 1734 /* End points are invalid. */
1638 colorspace->flags |= PNG_COLORSPACE_INVALID; 1735 colorspace->flags |= PNG_COLORSPACE_INVALID;
1639 png_benign_error(png_ptr, "invalid end points"); 1736 png_benign_error(png_ptr, "invalid end points");
1640 break; 1737 break;
1641 1738
1642 default: 1739 default:
1643 colorspace->flags |= PNG_COLORSPACE_INVALID; 1740 colorspace->flags |= PNG_COLORSPACE_INVALID;
1644 png_error(png_ptr, "internal error checking chromaticities"); 1741 png_error(png_ptr, "internal error checking chromaticities");
1645 break;
1646 } 1742 }
1647 1743
1648 return 0; /* failed */ 1744 return 0; /* failed */
1649 } 1745 }
1650 1746
1651 #if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED) 1747 #if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED)
1652 /* Error message generation */ 1748 /* Error message generation */
1653 static char 1749 static char
1654 png_icc_tag_char(png_uint_32 byte) 1750 png_icc_tag_char(png_uint_32 byte)
1655 { 1751 {
(...skipping 15 matching lines...) Expand all
1671 name[5] = '\''; 1767 name[5] = '\'';
1672 } 1768 }
1673 1769
1674 static int 1770 static int
1675 is_ICC_signature_char(png_alloc_size_t it) 1771 is_ICC_signature_char(png_alloc_size_t it)
1676 { 1772 {
1677 return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) || 1773 return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) ||
1678 (it >= 97 && it <= 122); 1774 (it >= 97 && it <= 122);
1679 } 1775 }
1680 1776
1681 static int is_ICC_signature(png_alloc_size_t it) 1777 static int
1778 is_ICC_signature(png_alloc_size_t it)
1682 { 1779 {
1683 return is_ICC_signature_char(it >> 24) /* checks all the top bits */ && 1780 return is_ICC_signature_char(it >> 24) /* checks all the top bits */ &&
1684 is_ICC_signature_char((it >> 16) & 0xff) && 1781 is_ICC_signature_char((it >> 16) & 0xff) &&
1685 is_ICC_signature_char((it >> 8) & 0xff) && 1782 is_ICC_signature_char((it >> 8) & 0xff) &&
1686 is_ICC_signature_char(it & 0xff); 1783 is_ICC_signature_char(it & 0xff);
1687 } 1784 }
1688 1785
1689 static int 1786 static int
1690 png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace, 1787 png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace,
1691 png_const_charp name, png_alloc_size_t value, png_const_charp reason) 1788 png_const_charp name, png_alloc_size_t value, png_const_charp reason)
1692 { 1789 {
1693 size_t pos; 1790 size_t pos;
1694 char message[196]; /* see below for calculation */ 1791 char message[196]; /* see below for calculation */
1695 1792
1696 if (colorspace != NULL) 1793 if (colorspace != NULL)
1697 colorspace->flags |= PNG_COLORSPACE_INVALID; 1794 colorspace->flags |= PNG_COLORSPACE_INVALID;
1698 1795
1699 pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */ 1796 pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */
1700 pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */ 1797 pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */
1701 pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */ 1798 pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */
1702 if (is_ICC_signature(value)) 1799 if (is_ICC_signature(value) != 0)
1703 { 1800 {
1704 /* So 'value' is at most 4 bytes and the following cast is safe */ 1801 /* So 'value' is at most 4 bytes and the following cast is safe */
1705 png_icc_tag_name(message+pos, (png_uint_32)value); 1802 png_icc_tag_name(message+pos, (png_uint_32)value);
1706 pos += 6; /* total +8; less than the else clause */ 1803 pos += 6; /* total +8; less than the else clause */
1707 message[pos++] = ':'; 1804 message[pos++] = ':';
1708 message[pos++] = ' '; 1805 message[pos++] = ' ';
1709 } 1806 }
1710 # ifdef PNG_WARNINGS_SUPPORTED 1807 # ifdef PNG_WARNINGS_SUPPORTED
1711 else 1808 else
1712 { 1809 {
1713 char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/ 1810 char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/
1714 1811
1715 pos = png_safecat(message, (sizeof message), pos, 1812 pos = png_safecat(message, (sizeof message), pos,
1716 png_format_number(number, number+(sizeof number), 1813 png_format_number(number, number+(sizeof number),
1717 PNG_NUMBER_FORMAT_x, value)); 1814 PNG_NUMBER_FORMAT_x, value));
1718 pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/ 1815 pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/
1719 } 1816 }
1720 # endif 1817 # endif
1721 /* The 'reason' is an arbitrary message, allow +79 maximum 195 */ 1818 /* The 'reason' is an arbitrary message, allow +79 maximum 195 */
1722 pos = png_safecat(message, (sizeof message), pos, reason); 1819 pos = png_safecat(message, (sizeof message), pos, reason);
1820 PNG_UNUSED(pos)
1723 1821
1724 /* This is recoverable, but make it unconditionally an app_error on write to 1822 /* This is recoverable, but make it unconditionally an app_error on write to
1725 * avoid writing invalid ICC profiles into PNG files. (I.e. we handle them 1823 * avoid writing invalid ICC profiles into PNG files (i.e., we handle them
1726 * on read, with a warning, but on write unless the app turns off 1824 * on read, with a warning, but on write unless the app turns off
1727 * application errors the PNG won't be written.) 1825 * application errors the PNG won't be written.)
1728 */ 1826 */
1729 png_chunk_report(png_ptr, message, 1827 png_chunk_report(png_ptr, message,
1730 (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR); 1828 (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR);
1731 1829
1732 return 0; 1830 return 0;
1733 } 1831 }
1734 #endif /* sRGB || iCCP */ 1832 #endif /* sRGB || iCCP */
1735 1833
1736 #ifdef PNG_sRGB_SUPPORTED 1834 #ifdef PNG_sRGB_SUPPORTED
1737 int /* PRIVATE */ 1835 int /* PRIVATE */
1738 png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, 1836 png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace,
1739 int intent) 1837 int intent)
1740 { 1838 {
1741 /* sRGB sets known gamma, end points and (from the chunk) intent. */ 1839 /* sRGB sets known gamma, end points and (from the chunk) intent. */
1742 /* IMPORTANT: these are not necessarily the values found in an ICC profile 1840 /* IMPORTANT: these are not necessarily the values found in an ICC profile
1743 * because ICC profiles store values adapted to a D50 environment; it is 1841 * because ICC profiles store values adapted to a D50 environment; it is
1744 * expected that the ICC profile mediaWhitePointTag will be D50, see the 1842 * expected that the ICC profile mediaWhitePointTag will be D50; see the
1745 * checks and code elsewhere to understand this better. 1843 * checks and code elsewhere to understand this better.
1746 * 1844 *
1747 * These XYZ values, which are accurate to 5dp, produce rgb to gray 1845 * These XYZ values, which are accurate to 5dp, produce rgb to gray
1748 * coefficients of (6968,23435,2366), which are reduced (because they add up 1846 * coefficients of (6968,23435,2366), which are reduced (because they add up
1749 * to 32769 not 32768) to (6968,23434,2366). These are the values that 1847 * to 32769 not 32768) to (6968,23434,2366). These are the values that
1750 * libpng has traditionally used (and are the best values given the 15bit 1848 * libpng has traditionally used (and are the best values given the 15bit
1751 * algorithm used by the rgb to gray code.) 1849 * algorithm used by the rgb to gray code.)
1752 */ 1850 */
1753 static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */ 1851 static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */
1754 { 1852 {
1755 /* color X Y Z */ 1853 /* color X Y Z */
1756 /* red */ 41239, 21264, 1933, 1854 /* red */ 41239, 21264, 1933,
1757 /* green */ 35758, 71517, 11919, 1855 /* green */ 35758, 71517, 11919,
1758 /* blue */ 18048, 7219, 95053 1856 /* blue */ 18048, 7219, 95053
1759 }; 1857 };
1760 1858
1761 /* Do nothing if the colorspace is already invalidated. */ 1859 /* Do nothing if the colorspace is already invalidated. */
1762 if (colorspace->flags & PNG_COLORSPACE_INVALID) 1860 if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
1763 return 0; 1861 return 0;
1764 1862
1765 /* Check the intent, then check for existing settings. It is valid for the 1863 /* Check the intent, then check for existing settings. It is valid for the
1766 * PNG file to have cHRM or gAMA chunks along with sRGB, but the values must 1864 * PNG file to have cHRM or gAMA chunks along with sRGB, but the values must
1767 * be consistent with the correct values. If, however, this function is 1865 * be consistent with the correct values. If, however, this function is
1768 * called below because an iCCP chunk matches sRGB then it is quite 1866 * called below because an iCCP chunk matches sRGB then it is quite
1769 * conceivable that an older app recorded incorrect gAMA and cHRM because of 1867 * conceivable that an older app recorded incorrect gAMA and cHRM because of
1770 * an incorrect calculation based on the values in the profile - this does 1868 * an incorrect calculation based on the values in the profile - this does
1771 * *not* invalidate the profile (though it still produces an error, which can 1869 * *not* invalidate the profile (though it still produces an error, which can
1772 * be ignored.) 1870 * be ignored.)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1833 { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d }; 1931 { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
1834 1932
1835 int /* PRIVATE */ 1933 int /* PRIVATE */
1836 png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, 1934 png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
1837 png_const_charp name, png_uint_32 profile_length) 1935 png_const_charp name, png_uint_32 profile_length)
1838 { 1936 {
1839 if (profile_length < 132) 1937 if (profile_length < 132)
1840 return png_icc_profile_error(png_ptr, colorspace, name, profile_length, 1938 return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1841 "too short"); 1939 "too short");
1842 1940
1843 if (profile_length & 3)
1844 return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1845 "invalid length");
1846
1847 return 1; 1941 return 1;
1848 } 1942 }
1849 1943
1850 int /* PRIVATE */ 1944 int /* PRIVATE */
1851 png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, 1945 png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
1852 png_const_charp name, png_uint_32 profile_length, 1946 png_const_charp name, png_uint_32 profile_length,
1853 png_const_bytep profile/* first 132 bytes only */, int color_type) 1947 png_const_bytep profile/* first 132 bytes only */, int color_type)
1854 { 1948 {
1855 png_uint_32 temp; 1949 png_uint_32 temp;
1856 1950
1857 /* Length check; this cannot be ignored in this code because profile_length 1951 /* Length check; this cannot be ignored in this code because profile_length
1858 * is used later to check the tag table, so even if the profile seems over 1952 * is used later to check the tag table, so even if the profile seems over
1859 * long profile_length from the caller must be correct. The caller can fix 1953 * long profile_length from the caller must be correct. The caller can fix
1860 * this up on read or write by just passing in the profile header length. 1954 * this up on read or write by just passing in the profile header length.
1861 */ 1955 */
1862 temp = png_get_uint_32(profile); 1956 temp = png_get_uint_32(profile);
1863 if (temp != profile_length) 1957 if (temp != profile_length)
1864 return png_icc_profile_error(png_ptr, colorspace, name, temp, 1958 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1865 "length does not match profile"); 1959 "length does not match profile");
1866 1960
1961 temp = (png_uint_32) (*(profile+8));
1962 if (temp > 3 && (profile_length & 3))
1963 return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
1964 "invalid length");
1965
1867 temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */ 1966 temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */
1868 if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */ 1967 if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */
1869 profile_length < 132+12*temp) /* truncated tag table */ 1968 profile_length < 132+12*temp) /* truncated tag table */
1870 return png_icc_profile_error(png_ptr, colorspace, name, temp, 1969 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1871 "tag count too large"); 1970 "tag count too large");
1872 1971
1873 /* The 'intent' must be valid or we can't store it, ICC limits the intent to 1972 /* The 'intent' must be valid or we can't store it, ICC limits the intent to
1874 * 16 bits. 1973 * 16 bits.
1875 */ 1974 */
1876 temp = png_get_uint_32(profile+64); 1975 temp = png_get_uint_32(profile+64);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1930 * 2029 *
1931 * Previously it was suggested that an RGB profile on grayscale data could be 2030 * Previously it was suggested that an RGB profile on grayscale data could be
1932 * handled. However it it is clear that using an RGB profile in this context 2031 * handled. However it it is clear that using an RGB profile in this context
1933 * must be an error - there is no specification of what it means. Thus it is 2032 * must be an error - there is no specification of what it means. Thus it is
1934 * almost certainly more correct to ignore the profile. 2033 * almost certainly more correct to ignore the profile.
1935 */ 2034 */
1936 temp = png_get_uint_32(profile+16); /* data colour space field */ 2035 temp = png_get_uint_32(profile+16); /* data colour space field */
1937 switch (temp) 2036 switch (temp)
1938 { 2037 {
1939 case 0x52474220: /* 'RGB ' */ 2038 case 0x52474220: /* 'RGB ' */
1940 if (!(color_type & PNG_COLOR_MASK_COLOR)) 2039 if ((color_type & PNG_COLOR_MASK_COLOR) == 0)
1941 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2040 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1942 "RGB color space not permitted on grayscale PNG"); 2041 "RGB color space not permitted on grayscale PNG");
1943 break; 2042 break;
1944 2043
1945 case 0x47524159: /* 'GRAY' */ 2044 case 0x47524159: /* 'GRAY' */
1946 if (color_type & PNG_COLOR_MASK_COLOR) 2045 if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
1947 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2046 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1948 "Gray color space not permitted on RGB PNG"); 2047 "Gray color space not permitted on RGB PNG");
1949 break; 2048 break;
1950 2049
1951 default: 2050 default:
1952 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2051 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1953 "invalid ICC profile color space"); 2052 "invalid ICC profile color space");
1954 } 2053 }
1955 2054
1956 /* It is up to the application to check that the profile class matches the 2055 /* It is up to the application to check that the profile class matches the
1957 * application requirements; the spec provides no guidance, but it's pretty 2056 * application requirements; the spec provides no guidance, but it's pretty
1958 * weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer 2057 * weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer
1959 * ('prtr') or 'spac' (for generic color spaces). Issue a warning in these 2058 * ('prtr') or 'spac' (for generic color spaces). Issue a warning in these
1960 * cases. Issue an error for device link or abstract profiles - these don't 2059 * cases. Issue an error for device link or abstract profiles - these don't
1961 * contain the records necessary to transform the color-space to anything 2060 * contain the records necessary to transform the color-space to anything
1962 * other than the target device (and not even that for an abstract profile). 2061 * other than the target device (and not even that for an abstract profile).
1963 * Profiles of these classes may not be embedded in images. 2062 * Profiles of these classes may not be embedded in images.
1964 */ 2063 */
1965 temp = png_get_uint_32(profile+12); /* profile/device class */ 2064 temp = png_get_uint_32(profile+12); /* profile/device class */
1966 switch (temp) 2065 switch (temp)
1967 { 2066 {
1968 case 0x73636E72: /* 'scnr' */ 2067 case 0x73636e72: /* 'scnr' */
1969 case 0x6D6E7472: /* 'mntr' */ 2068 case 0x6d6e7472: /* 'mntr' */
1970 case 0x70727472: /* 'prtr' */ 2069 case 0x70727472: /* 'prtr' */
1971 case 0x73706163: /* 'spac' */ 2070 case 0x73706163: /* 'spac' */
1972 /* All supported */ 2071 /* All supported */
1973 break; 2072 break;
1974 2073
1975 case 0x61627374: /* 'abst' */ 2074 case 0x61627374: /* 'abst' */
1976 /* May not be embedded in an image */ 2075 /* May not be embedded in an image */
1977 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2076 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1978 "invalid embedded Abstract ICC profile"); 2077 "invalid embedded Abstract ICC profile");
1979 2078
1980 case 0x6C696E6B: /* 'link' */ 2079 case 0x6c696e6b: /* 'link' */
1981 /* DeviceLink profiles cannnot be interpreted in a non-device specific 2080 /* DeviceLink profiles cannot be interpreted in a non-device specific
1982 * fashion, if an app uses the AToB0Tag in the profile the results are 2081 * fashion, if an app uses the AToB0Tag in the profile the results are
1983 * undefined unless the result is sent to the intended device, 2082 * undefined unless the result is sent to the intended device,
1984 * therefore a DeviceLink profile should not be found embedded in a 2083 * therefore a DeviceLink profile should not be found embedded in a
1985 * PNG. 2084 * PNG.
1986 */ 2085 */
1987 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2086 return png_icc_profile_error(png_ptr, colorspace, name, temp,
1988 "unexpected DeviceLink ICC profile class"); 2087 "unexpected DeviceLink ICC profile class");
1989 2088
1990 case 0x6E6D636C: /* 'nmcl' */ 2089 case 0x6e6d636c: /* 'nmcl' */
1991 /* A NamedColor profile is also device specific, however it doesn't 2090 /* A NamedColor profile is also device specific, however it doesn't
1992 * contain an AToB0 tag that is open to misintrepretation. Almost 2091 * contain an AToB0 tag that is open to misinterpretation. Almost
1993 * certainly it will fail the tests below. 2092 * certainly it will fail the tests below.
1994 */ 2093 */
1995 (void)png_icc_profile_error(png_ptr, NULL, name, temp, 2094 (void)png_icc_profile_error(png_ptr, NULL, name, temp,
1996 "unexpected NamedColor ICC profile class"); 2095 "unexpected NamedColor ICC profile class");
1997 break; 2096 break;
1998 2097
1999 default: 2098 default:
2000 /* To allow for future enhancements to the profile accept unrecognized 2099 /* To allow for future enhancements to the profile accept unrecognized
2001 * profile classes with a warning, these then hit the test below on the 2100 * profile classes with a warning, these then hit the test below on the
2002 * tag content to ensure they are backward compatible with one of the 2101 * tag content to ensure they are backward compatible with one of the
2003 * understood profiles. 2102 * understood profiles.
2004 */ 2103 */
2005 (void)png_icc_profile_error(png_ptr, NULL, name, temp, 2104 (void)png_icc_profile_error(png_ptr, NULL, name, temp,
2006 "unrecognized ICC profile class"); 2105 "unrecognized ICC profile class");
2007 break; 2106 break;
2008 } 2107 }
2009 2108
2010 /* For any profile other than a device link one the PCS must be encoded 2109 /* For any profile other than a device link one the PCS must be encoded
2011 * either in XYZ or Lab. 2110 * either in XYZ or Lab.
2012 */ 2111 */
2013 temp = png_get_uint_32(profile+20); 2112 temp = png_get_uint_32(profile+20);
2014 switch (temp) 2113 switch (temp)
2015 { 2114 {
2016 case 0x58595A20: /* 'XYZ ' */ 2115 case 0x58595a20: /* 'XYZ ' */
2017 case 0x4C616220: /* 'Lab ' */ 2116 case 0x4c616220: /* 'Lab ' */
2018 break; 2117 break;
2019 2118
2020 default: 2119 default:
2021 return png_icc_profile_error(png_ptr, colorspace, name, temp, 2120 return png_icc_profile_error(png_ptr, colorspace, name, temp,
2022 "unexpected ICC PCS encoding"); 2121 "unexpected ICC PCS encoding");
2023 } 2122 }
2024 2123
2025 return 1; 2124 return 1;
2026 } 2125 }
2027 2126
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
2064 */ 2163 */
2065 if (tag_start > profile_length || tag_length > profile_length - tag_start) 2164 if (tag_start > profile_length || tag_length > profile_length - tag_start)
2066 return png_icc_profile_error(png_ptr, colorspace, name, tag_id, 2165 return png_icc_profile_error(png_ptr, colorspace, name, tag_id,
2067 "ICC profile tag outside profile"); 2166 "ICC profile tag outside profile");
2068 } 2167 }
2069 2168
2070 return 1; /* success, maybe with warnings */ 2169 return 1; /* success, maybe with warnings */
2071 } 2170 }
2072 2171
2073 #ifdef PNG_sRGB_SUPPORTED 2172 #ifdef PNG_sRGB_SUPPORTED
2173 #if PNG_sRGB_PROFILE_CHECKS >= 0
2074 /* Information about the known ICC sRGB profiles */ 2174 /* Information about the known ICC sRGB profiles */
2075 static const struct 2175 static const struct
2076 { 2176 {
2077 png_uint_32 adler, crc, length; 2177 png_uint_32 adler, crc, length;
2078 png_uint_32 md5[4]; 2178 png_uint_32 md5[4];
2079 png_byte have_md5; 2179 png_byte have_md5;
2080 png_byte is_broken; 2180 png_byte is_broken;
2081 png_uint_16 intent; 2181 png_uint_16 intent;
2082 2182
2083 # define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0) 2183 # define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
2137 png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr, 2237 png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
2138 png_const_bytep profile, uLong adler) 2238 png_const_bytep profile, uLong adler)
2139 { 2239 {
2140 /* The quick check is to verify just the MD5 signature and trust the 2240 /* The quick check is to verify just the MD5 signature and trust the
2141 * rest of the data. Because the profile has already been verified for 2241 * rest of the data. Because the profile has already been verified for
2142 * correctness this is safe. png_colorspace_set_sRGB will check the 'intent' 2242 * correctness this is safe. png_colorspace_set_sRGB will check the 'intent'
2143 * field too, so if the profile has been edited with an intent not defined 2243 * field too, so if the profile has been edited with an intent not defined
2144 * by sRGB (but maybe defined by a later ICC specification) the read of 2244 * by sRGB (but maybe defined by a later ICC specification) the read of
2145 * the profile will fail at that point. 2245 * the profile will fail at that point.
2146 */ 2246 */
2247
2147 png_uint_32 length = 0; 2248 png_uint_32 length = 0;
2148 png_uint_32 intent = 0x10000; /* invalid */ 2249 png_uint_32 intent = 0x10000; /* invalid */
2149 #if PNG_sRGB_PROFILE_CHECKS > 1 2250 #if PNG_sRGB_PROFILE_CHECKS > 1
2150 uLong crc = 0; /* the value for 0 length data */ 2251 uLong crc = 0; /* the value for 0 length data */
2151 #endif 2252 #endif
2152 unsigned int i; 2253 unsigned int i;
2153 2254
2255 #ifdef PNG_SET_OPTION_SUPPORTED
2256 /* First see if PNG_SKIP_sRGB_CHECK_PROFILE has been set to "on" */
2257 if (((png_ptr->options >> PNG_SKIP_sRGB_CHECK_PROFILE) & 3) ==
2258 PNG_OPTION_ON)
2259 return 0;
2260 #endif
2261
2154 for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i) 2262 for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i)
2155 { 2263 {
2156 if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] && 2264 if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] &&
2157 png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] && 2265 png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] &&
2158 png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] && 2266 png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] &&
2159 png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3]) 2267 png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3])
2160 { 2268 {
2161 /* This may be one of the old HP profiles without an MD5, in that 2269 /* This may be one of the old HP profiles without an MD5, in that
2162 * case we can only use the length and Adler32 (note that these 2270 * case we can only use the length and Adler32 (note that these
2163 * are not used by default if there is an MD5!) 2271 * are not used by default if there is an MD5!)
2164 */ 2272 */
2165 # if PNG_sRGB_PROFILE_CHECKS == 0 2273 # if PNG_sRGB_PROFILE_CHECKS == 0
2166 if (png_sRGB_checks[i].have_md5) 2274 if (png_sRGB_checks[i].have_md5 != 0)
2167 return 1+png_sRGB_checks[i].is_broken; 2275 return 1+png_sRGB_checks[i].is_broken;
2168 # endif 2276 # endif
2169 2277
2170 /* Profile is unsigned or more checks have been configured in. */ 2278 /* Profile is unsigned or more checks have been configured in. */
2171 if (length == 0) 2279 if (length == 0)
2172 { 2280 {
2173 length = png_get_uint_32(profile); 2281 length = png_get_uint_32(profile);
2174 intent = png_get_uint_32(profile+64); 2282 intent = png_get_uint_32(profile+64);
2175 } 2283 }
2176 2284
2177 /* Length *and* intent must match */ 2285 /* Length *and* intent must match */
2178 if (length == png_sRGB_checks[i].length && 2286 if (length == (png_uint_32) png_sRGB_checks[i].length &&
2179 intent == png_sRGB_checks[i].intent) 2287 intent == (png_uint_32) png_sRGB_checks[i].intent)
2180 { 2288 {
2181 /* Now calculate the adler32 if not done already. */ 2289 /* Now calculate the adler32 if not done already. */
2182 if (adler == 0) 2290 if (adler == 0)
2183 { 2291 {
2184 adler = adler32(0, NULL, 0); 2292 adler = adler32(0, NULL, 0);
2185 adler = adler32(adler, profile, length); 2293 adler = adler32(adler, profile, length);
2186 } 2294 }
2187 2295
2188 if (adler == png_sRGB_checks[i].adler) 2296 if (adler == png_sRGB_checks[i].adler)
2189 { 2297 {
2190 /* These basic checks suggest that the data has not been 2298 /* These basic checks suggest that the data has not been
2191 * modified, but if the check level is more than 1 perform 2299 * modified, but if the check level is more than 1 perform
2192 * our own crc32 checksum on the data. 2300 * our own crc32 checksum on the data.
2193 */ 2301 */
2194 # if PNG_sRGB_PROFILE_CHECKS > 1 2302 # if PNG_sRGB_PROFILE_CHECKS > 1
2195 if (crc == 0) 2303 if (crc == 0)
2196 { 2304 {
2197 crc = crc32(0, NULL, 0); 2305 crc = crc32(0, NULL, 0);
2198 crc = crc32(crc, profile, length); 2306 crc = crc32(crc, profile, length);
2199 } 2307 }
2200 2308
2201 /* So this check must pass for the 'return' below to happen. 2309 /* So this check must pass for the 'return' below to happen.
2202 */ 2310 */
2203 if (crc == png_sRGB_checks[i].crc) 2311 if (crc == png_sRGB_checks[i].crc)
2204 # endif 2312 # endif
2205 { 2313 {
2206 if (png_sRGB_checks[i].is_broken) 2314 if (png_sRGB_checks[i].is_broken != 0)
2207 { 2315 {
2208 /* These profiles are known to have bad data that may cause 2316 /* These profiles are known to have bad data that may cause
2209 * problems if they are used, therefore attempt to 2317 * problems if they are used, therefore attempt to
2210 * discourage their use, skip the 'have_md5' warning below, 2318 * discourage their use, skip the 'have_md5' warning below,
2211 * which is made irrelevant by this error. 2319 * which is made irrelevant by this error.
2212 */ 2320 */
2213 png_chunk_report(png_ptr, "known incorrect sRGB profile", 2321 png_chunk_report(png_ptr, "known incorrect sRGB profile",
2214 PNG_CHUNK_ERROR); 2322 PNG_CHUNK_ERROR);
2215 } 2323 }
2216 2324
2217 /* Warn that this being done; this isn't even an error since 2325 /* Warn that this being done; this isn't even an error since
2218 * the profile is perfectly valid, but it would be nice if 2326 * the profile is perfectly valid, but it would be nice if
2219 * people used the up-to-date ones. 2327 * people used the up-to-date ones.
2220 */ 2328 */
2221 else if (!png_sRGB_checks[i].have_md5) 2329 else if (png_sRGB_checks[i].have_md5 == 0)
2222 { 2330 {
2223 png_chunk_report(png_ptr, 2331 png_chunk_report(png_ptr,
2224 "out-of-date sRGB profile with no signature", 2332 "out-of-date sRGB profile with no signature",
2225 PNG_CHUNK_WARNING); 2333 PNG_CHUNK_WARNING);
2226 } 2334 }
2227 2335
2228 return 1+png_sRGB_checks[i].is_broken; 2336 return 1+png_sRGB_checks[i].is_broken;
2229 } 2337 }
2230 } 2338 }
2339
2340 # if PNG_sRGB_PROFILE_CHECKS > 0
2341 /* The signature matched, but the profile had been changed in some
2342 * way. This probably indicates a data error or uninformed hacking.
2343 * Fall through to "no match".
2344 */
2345 png_chunk_report(png_ptr,
2346 "Not recognizing known sRGB profile that has been edited",
2347 PNG_CHUNK_WARNING);
2348 break;
2349 # endif
2231 } 2350 }
2232
2233 # if PNG_sRGB_PROFILE_CHECKS > 0
2234 /* The signature matched, but the profile had been changed in some
2235 * way. This is an apparent violation of the ICC terms of use and,
2236 * anyway, probably indicates a data error or uninformed hacking.
2237 */
2238 if (png_sRGB_checks[i].have_md5)
2239 png_benign_error(png_ptr,
2240 "copyright violation: edited ICC profile ignored");
2241 # endif
2242 } 2351 }
2243 } 2352 }
2244 2353
2245 return 0; /* no match */ 2354 return 0; /* no match */
2246 } 2355 }
2247 #endif 2356 #endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */
2248 2357
2249 #ifdef PNG_sRGB_SUPPORTED
2250 void /* PRIVATE */ 2358 void /* PRIVATE */
2251 png_icc_set_sRGB(png_const_structrp png_ptr, 2359 png_icc_set_sRGB(png_const_structrp png_ptr,
2252 png_colorspacerp colorspace, png_const_bytep profile, uLong adler) 2360 png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
2253 { 2361 {
2254 /* Is this profile one of the known ICC sRGB profiles? If it is, just set 2362 /* Is this profile one of the known ICC sRGB profiles? If it is, just set
2255 * the sRGB information. 2363 * the sRGB information.
2256 */ 2364 */
2257 if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler)) 2365 #if PNG_sRGB_PROFILE_CHECKS >= 0
2366 if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0)
2367 #endif
2258 (void)png_colorspace_set_sRGB(png_ptr, colorspace, 2368 (void)png_colorspace_set_sRGB(png_ptr, colorspace,
2259 (int)/*already checked*/png_get_uint_32(profile+64)); 2369 (int)/*already checked*/png_get_uint_32(profile+64));
2260 } 2370 }
2261 #endif /* PNG_READ_sRGB_SUPPORTED */ 2371 #endif /* sRGB */
2262 2372
2263 int /* PRIVATE */ 2373 int /* PRIVATE */
2264 png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace, 2374 png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
2265 png_const_charp name, png_uint_32 profile_length, png_const_bytep profile, 2375 png_const_charp name, png_uint_32 profile_length, png_const_bytep profile,
2266 int color_type) 2376 int color_type)
2267 { 2377 {
2268 if (colorspace->flags & PNG_COLORSPACE_INVALID) 2378 if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
2269 return 0; 2379 return 0;
2270 2380
2271 if (png_icc_check_length(png_ptr, colorspace, name, profile_length) && 2381 if (png_icc_check_length(png_ptr, colorspace, name, profile_length) != 0 &&
2272 png_icc_check_header(png_ptr, colorspace, name, profile_length, profile, 2382 png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
2273 color_type) && 2383 color_type) != 0 &&
2274 png_icc_check_tag_table(png_ptr, colorspace, name, profile_length, 2384 png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
2275 profile)) 2385 profile) != 0)
2276 { 2386 {
2277 # ifdef PNG_sRGB_SUPPORTED 2387 # ifdef PNG_sRGB_SUPPORTED
2278 /* If no sRGB support, don't try storing sRGB information */ 2388 /* If no sRGB support, don't try storing sRGB information */
2279 png_icc_set_sRGB(png_ptr, colorspace, profile, 0); 2389 png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
2280 # endif 2390 # endif
2281 return 1; 2391 return 1;
2282 } 2392 }
2283 2393
2284 /* Failure case */ 2394 /* Failure case */
2285 return 0; 2395 return 0;
2286 } 2396 }
2287 #endif /* iCCP */ 2397 #endif /* iCCP */
2288 2398
2289 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED 2399 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2290 void /* PRIVATE */ 2400 void /* PRIVATE */
2291 png_colorspace_set_rgb_coefficients(png_structrp png_ptr) 2401 png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
2292 { 2402 {
2293 /* Set the rgb_to_gray coefficients from the colorspace. */ 2403 /* Set the rgb_to_gray coefficients from the colorspace. */
2294 if (!png_ptr->rgb_to_gray_coefficients_set && 2404 if (png_ptr->rgb_to_gray_coefficients_set == 0 &&
2295 (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) 2405 (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
2296 { 2406 {
2297 /* png_set_background has not been called, get the coefficients from the Y 2407 /* png_set_background has not been called, get the coefficients from the Y
2298 * values of the colorspace colorants. 2408 * values of the colorspace colorants.
2299 */ 2409 */
2300 png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y; 2410 png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y;
2301 png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y; 2411 png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y;
2302 png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y; 2412 png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y;
2303 png_fixed_point total = r+g+b; 2413 png_fixed_point total = r+g+b;
2304 2414
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
2343 } 2453 }
2344 2454
2345 /* This is a png_error at present even though it could be ignored - 2455 /* This is a png_error at present even though it could be ignored -
2346 * it should never happen, but it is important that if it does, the 2456 * it should never happen, but it is important that if it does, the
2347 * bug is fixed. 2457 * bug is fixed.
2348 */ 2458 */
2349 else 2459 else
2350 png_error(png_ptr, "internal error handling cHRM->XYZ"); 2460 png_error(png_ptr, "internal error handling cHRM->XYZ");
2351 } 2461 }
2352 } 2462 }
2463 #endif /* READ_RGB_TO_GRAY */
2464
2465 #endif /* COLORSPACE */
2466
2467 #ifdef __GNUC__
2468 /* This exists solely to work round a warning from GNU C. */
2469 static int /* PRIVATE */
2470 png_gt(size_t a, size_t b)
2471 {
2472 return a > b;
2473 }
2474 #else
2475 # define png_gt(a,b) ((a) > (b))
2353 #endif 2476 #endif
2354 2477
2355 #endif /* COLORSPACE */
2356
2357 void /* PRIVATE */ 2478 void /* PRIVATE */
2358 png_check_IHDR(png_const_structrp png_ptr, 2479 png_check_IHDR(png_const_structrp png_ptr,
2359 png_uint_32 width, png_uint_32 height, int bit_depth, 2480 png_uint_32 width, png_uint_32 height, int bit_depth,
2360 int color_type, int interlace_type, int compression_type, 2481 int color_type, int interlace_type, int compression_type,
2361 int filter_type) 2482 int filter_type)
2362 { 2483 {
2363 int error = 0; 2484 int error = 0;
2364 2485
2365 /* Check for width and height valid values */ 2486 /* Check for width and height valid values */
2366 if (width == 0) 2487 if (width == 0)
2367 { 2488 {
2368 png_warning(png_ptr, "Image width is zero in IHDR"); 2489 png_warning(png_ptr, "Image width is zero in IHDR");
2369 error = 1; 2490 error = 1;
2370 } 2491 }
2371 2492
2493 if (width > PNG_UINT_31_MAX)
2494 {
2495 png_warning(png_ptr, "Invalid image width in IHDR");
2496 error = 1;
2497 }
2498
2499 if (png_gt(((width + 7) & (~7)),
2500 ((PNG_SIZE_MAX
2501 - 48 /* big_row_buf hack */
2502 - 1) /* filter byte */
2503 / 8) /* 8-byte RGBA pixels */
2504 - 1)) /* extra max_pixel_depth pad */
2505 {
2506 /* The size of the row must be within the limits of this architecture.
2507 * Because the read code can perform arbitrary transformations the
2508 * maximum size is checked here. Because the code in png_read_start_row
2509 * adds extra space "for safety's sake" in several places a conservative
2510 * limit is used here.
2511 *
2512 * NOTE: it would be far better to check the size that is actually used,
2513 * but the effect in the real world is minor and the changes are more
2514 * extensive, therefore much more dangerous and much more difficult to
2515 * write in a way that avoids compiler warnings.
2516 */
2517 png_warning(png_ptr, "Image width is too large for this architecture");
2518 error = 1;
2519 }
2520
2521 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
2522 if (width > png_ptr->user_width_max)
2523 #else
2524 if (width > PNG_USER_WIDTH_MAX)
2525 #endif
2526 {
2527 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
2528 error = 1;
2529 }
2530
2372 if (height == 0) 2531 if (height == 0)
2373 { 2532 {
2374 png_warning(png_ptr, "Image height is zero in IHDR"); 2533 png_warning(png_ptr, "Image height is zero in IHDR");
2375 error = 1; 2534 error = 1;
2376 } 2535 }
2377 2536
2378 # ifdef PNG_SET_USER_LIMITS_SUPPORTED 2537 if (height > PNG_UINT_31_MAX)
2379 if (width > png_ptr->user_width_max) 2538 {
2380 2539 png_warning(png_ptr, "Invalid image height in IHDR");
2381 # else
2382 if (width > PNG_USER_WIDTH_MAX)
2383 # endif
2384 {
2385 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
2386 error = 1; 2540 error = 1;
2387 } 2541 }
2388 2542
2389 # ifdef PNG_SET_USER_LIMITS_SUPPORTED 2543 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
2390 if (height > png_ptr->user_height_max) 2544 if (height > png_ptr->user_height_max)
2391 # else 2545 #else
2392 if (height > PNG_USER_HEIGHT_MAX) 2546 if (height > PNG_USER_HEIGHT_MAX)
2393 # endif 2547 #endif
2394 { 2548 {
2395 png_warning(png_ptr, "Image height exceeds user limit in IHDR"); 2549 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
2396 error = 1; 2550 error = 1;
2397 } 2551 }
2398 2552
2399 if (width > PNG_UINT_31_MAX)
2400 {
2401 png_warning(png_ptr, "Invalid image width in IHDR");
2402 error = 1;
2403 }
2404
2405 if (height > PNG_UINT_31_MAX)
2406 {
2407 png_warning(png_ptr, "Invalid image height in IHDR");
2408 error = 1;
2409 }
2410
2411 if (width > (PNG_UINT_32_MAX
2412 >> 3) /* 8-byte RGBA pixels */
2413 - 48 /* bigrowbuf hack */
2414 - 1 /* filter byte */
2415 - 7*8 /* rounding of width to multiple of 8 pixels */
2416 - 8) /* extra max_pixel_depth pad */
2417 png_warning(png_ptr, "Width is too large for libpng to process pixels");
2418
2419 /* Check other values */ 2553 /* Check other values */
2420 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && 2554 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
2421 bit_depth != 8 && bit_depth != 16) 2555 bit_depth != 8 && bit_depth != 16)
2422 { 2556 {
2423 png_warning(png_ptr, "Invalid bit depth in IHDR"); 2557 png_warning(png_ptr, "Invalid bit depth in IHDR");
2424 error = 1; 2558 error = 1;
2425 } 2559 }
2426 2560
2427 if (color_type < 0 || color_type == 1 || 2561 if (color_type < 0 || color_type == 1 ||
2428 color_type == 5 || color_type > 6) 2562 color_type == 5 || color_type > 6)
(...skipping 16 matching lines...) Expand all
2445 png_warning(png_ptr, "Unknown interlace method in IHDR"); 2579 png_warning(png_ptr, "Unknown interlace method in IHDR");
2446 error = 1; 2580 error = 1;
2447 } 2581 }
2448 2582
2449 if (compression_type != PNG_COMPRESSION_TYPE_BASE) 2583 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
2450 { 2584 {
2451 png_warning(png_ptr, "Unknown compression method in IHDR"); 2585 png_warning(png_ptr, "Unknown compression method in IHDR");
2452 error = 1; 2586 error = 1;
2453 } 2587 }
2454 2588
2455 # ifdef PNG_MNG_FEATURES_SUPPORTED 2589 #ifdef PNG_MNG_FEATURES_SUPPORTED
2456 /* Accept filter_method 64 (intrapixel differencing) only if 2590 /* Accept filter_method 64 (intrapixel differencing) only if
2457 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and 2591 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
2458 * 2. Libpng did not read a PNG signature (this filter_method is only 2592 * 2. Libpng did not read a PNG signature (this filter_method is only
2459 * used in PNG datastreams that are embedded in MNG datastreams) and 2593 * used in PNG datastreams that are embedded in MNG datastreams) and
2460 * 3. The application called png_permit_mng_features with a mask that 2594 * 3. The application called png_permit_mng_features with a mask that
2461 * included PNG_FLAG_MNG_FILTER_64 and 2595 * included PNG_FLAG_MNG_FILTER_64 and
2462 * 4. The filter_method is 64 and 2596 * 4. The filter_method is 64 and
2463 * 5. The color_type is RGB or RGBA 2597 * 5. The color_type is RGB or RGBA
2464 */ 2598 */
2465 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) && 2599 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 &&
2466 png_ptr->mng_features_permitted) 2600 png_ptr->mng_features_permitted != 0)
2467 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); 2601 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
2468 2602
2469 if (filter_type != PNG_FILTER_TYPE_BASE) 2603 if (filter_type != PNG_FILTER_TYPE_BASE)
2470 { 2604 {
2471 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) && 2605 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
2472 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && 2606 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
2473 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && 2607 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
2474 (color_type == PNG_COLOR_TYPE_RGB || 2608 (color_type == PNG_COLOR_TYPE_RGB ||
2475 color_type == PNG_COLOR_TYPE_RGB_ALPHA))) 2609 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
2476 { 2610 {
2477 png_warning(png_ptr, "Unknown filter method in IHDR"); 2611 png_warning(png_ptr, "Unknown filter method in IHDR");
2478 error = 1; 2612 error = 1;
2479 } 2613 }
2480 2614
2481 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) 2615 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0)
2482 { 2616 {
2483 png_warning(png_ptr, "Invalid filter method in IHDR"); 2617 png_warning(png_ptr, "Invalid filter method in IHDR");
2484 error = 1; 2618 error = 1;
2485 } 2619 }
2486 } 2620 }
2487 2621
2488 # else 2622 #else
2489 if (filter_type != PNG_FILTER_TYPE_BASE) 2623 if (filter_type != PNG_FILTER_TYPE_BASE)
2490 { 2624 {
2491 png_warning(png_ptr, "Unknown filter method in IHDR"); 2625 png_warning(png_ptr, "Unknown filter method in IHDR");
2492 error = 1; 2626 error = 1;
2493 } 2627 }
2494 # endif 2628 #endif
2495 2629
2496 if (error == 1) 2630 if (error == 1)
2497 png_error(png_ptr, "Invalid IHDR data"); 2631 png_error(png_ptr, "Invalid IHDR data");
2498 } 2632 }
2499 2633
2500 #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) 2634 #if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED)
2501 /* ASCII to fp functions */ 2635 /* ASCII to fp functions */
2502 /* Check an ASCII formated floating point value, see the more detailed 2636 /* Check an ASCII formated floating point value, see the more detailed
2503 * comments in pngpriv.h 2637 * comments in pngpriv.h
2504 */ 2638 */
(...skipping 26 matching lines...) Expand all
2531 default: goto PNG_FP_End; 2665 default: goto PNG_FP_End;
2532 } 2666 }
2533 2667
2534 /* Now deal with this type according to the current 2668 /* Now deal with this type according to the current
2535 * state, the type is arranged to not overlap the 2669 * state, the type is arranged to not overlap the
2536 * bits of the PNG_FP_STATE. 2670 * bits of the PNG_FP_STATE.
2537 */ 2671 */
2538 switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY)) 2672 switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY))
2539 { 2673 {
2540 case PNG_FP_INTEGER + PNG_FP_SAW_SIGN: 2674 case PNG_FP_INTEGER + PNG_FP_SAW_SIGN:
2541 if (state & PNG_FP_SAW_ANY) 2675 if ((state & PNG_FP_SAW_ANY) != 0)
2542 goto PNG_FP_End; /* not a part of the number */ 2676 goto PNG_FP_End; /* not a part of the number */
2543 2677
2544 png_fp_add(state, type); 2678 png_fp_add(state, type);
2545 break; 2679 break;
2546 2680
2547 case PNG_FP_INTEGER + PNG_FP_SAW_DOT: 2681 case PNG_FP_INTEGER + PNG_FP_SAW_DOT:
2548 /* Ok as trailer, ok as lead of fraction. */ 2682 /* Ok as trailer, ok as lead of fraction. */
2549 if (state & PNG_FP_SAW_DOT) /* two dots */ 2683 if ((state & PNG_FP_SAW_DOT) != 0) /* two dots */
2550 goto PNG_FP_End; 2684 goto PNG_FP_End;
2551 2685
2552 else if (state & PNG_FP_SAW_DIGIT) /* trailing dot? */ 2686 else if ((state & PNG_FP_SAW_DIGIT) != 0) /* trailing dot? */
2553 png_fp_add(state, type); 2687 png_fp_add(state, type);
2554 2688
2555 else 2689 else
2556 png_fp_set(state, PNG_FP_FRACTION | type); 2690 png_fp_set(state, PNG_FP_FRACTION | type);
2557 2691
2558 break; 2692 break;
2559 2693
2560 case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT: 2694 case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT:
2561 if (state & PNG_FP_SAW_DOT) /* delayed fraction */ 2695 if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */
2562 png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); 2696 png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
2563 2697
2564 png_fp_add(state, type | PNG_FP_WAS_VALID); 2698 png_fp_add(state, type | PNG_FP_WAS_VALID);
2565 2699
2566 break; 2700 break;
2567 2701
2568 case PNG_FP_INTEGER + PNG_FP_SAW_E: 2702 case PNG_FP_INTEGER + PNG_FP_SAW_E:
2569 if ((state & PNG_FP_SAW_DIGIT) == 0) 2703 if ((state & PNG_FP_SAW_DIGIT) == 0)
2570 goto PNG_FP_End; 2704 goto PNG_FP_End;
2571 2705
(...skipping 17 matching lines...) Expand all
2589 * with the sequence ".E" (with no preceding digits). 2723 * with the sequence ".E" (with no preceding digits).
2590 */ 2724 */
2591 if ((state & PNG_FP_SAW_DIGIT) == 0) 2725 if ((state & PNG_FP_SAW_DIGIT) == 0)
2592 goto PNG_FP_End; 2726 goto PNG_FP_End;
2593 2727
2594 png_fp_set(state, PNG_FP_EXPONENT); 2728 png_fp_set(state, PNG_FP_EXPONENT);
2595 2729
2596 break; 2730 break;
2597 2731
2598 case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN: 2732 case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN:
2599 if (state & PNG_FP_SAW_ANY) 2733 if ((state & PNG_FP_SAW_ANY) != 0)
2600 goto PNG_FP_End; /* not a part of the number */ 2734 goto PNG_FP_End; /* not a part of the number */
2601 2735
2602 png_fp_add(state, PNG_FP_SAW_SIGN); 2736 png_fp_add(state, PNG_FP_SAW_SIGN);
2603 2737
2604 break; 2738 break;
2605 2739
2606 /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT: 2740 /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT:
2607 goto PNG_FP_End; */ 2741 goto PNG_FP_End; */
2608 2742
2609 case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT: 2743 case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT:
(...skipping 22 matching lines...) Expand all
2632 } 2766 }
2633 2767
2634 2768
2635 /* The same but for a complete string. */ 2769 /* The same but for a complete string. */
2636 int 2770 int
2637 png_check_fp_string(png_const_charp string, png_size_t size) 2771 png_check_fp_string(png_const_charp string, png_size_t size)
2638 { 2772 {
2639 int state=0; 2773 int state=0;
2640 png_size_t char_index=0; 2774 png_size_t char_index=0;
2641 2775
2642 if (png_check_fp_number(string, size, &state, &char_index) && 2776 if (png_check_fp_number(string, size, &state, &char_index) != 0 &&
2643 (char_index == size || string[char_index] == 0)) 2777 (char_index == size || string[char_index] == 0))
2644 return state /* must be non-zero - see above */; 2778 return state /* must be non-zero - see above */;
2645 2779
2646 return 0; /* i.e. fail */ 2780 return 0; /* i.e. fail */
2647 } 2781 }
2648 #endif /* pCAL or sCAL */ 2782 #endif /* pCAL || sCAL */
2649 2783
2650 #ifdef PNG_sCAL_SUPPORTED 2784 #ifdef PNG_sCAL_SUPPORTED
2651 # ifdef PNG_FLOATING_POINT_SUPPORTED 2785 # ifdef PNG_FLOATING_POINT_SUPPORTED
2652 /* Utility used below - a simple accurate power of ten from an integral 2786 /* Utility used below - a simple accurate power of ten from an integral
2653 * exponent. 2787 * exponent.
2654 */ 2788 */
2655 static double 2789 static double
2656 png_pow10(int power) 2790 png_pow10(int power)
2657 { 2791 {
2658 int recip = 0; 2792 int recip = 0;
(...skipping 13 matching lines...) Expand all
2672 /* Decompose power bitwise. */ 2806 /* Decompose power bitwise. */
2673 double mult = 10; 2807 double mult = 10;
2674 do 2808 do
2675 { 2809 {
2676 if (power & 1) d *= mult; 2810 if (power & 1) d *= mult;
2677 mult *= mult; 2811 mult *= mult;
2678 power >>= 1; 2812 power >>= 1;
2679 } 2813 }
2680 while (power > 0); 2814 while (power > 0);
2681 2815
2682 if (recip) d = 1/d; 2816 if (recip != 0) d = 1/d;
2683 } 2817 }
2684 /* else power is 0 and d is 1 */ 2818 /* else power is 0 and d is 1 */
2685 2819
2686 return d; 2820 return d;
2687 } 2821 }
2688 2822
2689 /* Function to format a floating point value in ASCII with a given 2823 /* Function to format a floating point value in ASCII with a given
2690 * precision. 2824 * precision.
2691 */ 2825 */
2692 void /* PRIVATE */ 2826 void /* PRIVATE */
(...skipping 17 matching lines...) Expand all
2710 { 2844 {
2711 if (fp < 0) 2845 if (fp < 0)
2712 { 2846 {
2713 fp = -fp; 2847 fp = -fp;
2714 *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */ 2848 *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */
2715 --size; 2849 --size;
2716 } 2850 }
2717 2851
2718 if (fp >= DBL_MIN && fp <= DBL_MAX) 2852 if (fp >= DBL_MIN && fp <= DBL_MAX)
2719 { 2853 {
2720 int exp_b10; /* A base 10 exponent */ 2854 int exp_b10; /* A base 10 exponent */
2721 double base; /* 10^exp_b10 */ 2855 double base; /* 10^exp_b10 */
2722 2856
2723 /* First extract a base 10 exponent of the number, 2857 /* First extract a base 10 exponent of the number,
2724 * the calculation below rounds down when converting 2858 * the calculation below rounds down when converting
2725 * from base 2 to base 10 (multiply by log10(2) - 2859 * from base 2 to base 10 (multiply by log10(2) -
2726 * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to 2860 * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to
2727 * be increased. Note that the arithmetic shift 2861 * be increased. Note that the arithmetic shift
2728 * performs a floor() unlike C arithmetic - using a 2862 * performs a floor() unlike C arithmetic - using a
2729 * C multiply would break the following for negative 2863 * C multiply would break the following for negative
2730 * exponents. 2864 * exponents.
(...skipping 27 matching lines...) Expand all
2758 fp /= base; 2892 fp /= base;
2759 while (fp >= 1) fp /= 10, ++exp_b10; 2893 while (fp >= 1) fp /= 10, ++exp_b10;
2760 2894
2761 /* Because of the code above fp may, at this point, be 2895 /* Because of the code above fp may, at this point, be
2762 * less than .1, this is ok because the code below can 2896 * less than .1, this is ok because the code below can
2763 * handle the leading zeros this generates, so no attempt 2897 * handle the leading zeros this generates, so no attempt
2764 * is made to correct that here. 2898 * is made to correct that here.
2765 */ 2899 */
2766 2900
2767 { 2901 {
2768 int czero, clead, cdigits; 2902 unsigned int czero, clead, cdigits;
2769 char exponent[10]; 2903 char exponent[10];
2770 2904
2771 /* Allow up to two leading zeros - this will not lengthen 2905 /* Allow up to two leading zeros - this will not lengthen
2772 * the number compared to using E-n. 2906 * the number compared to using E-n.
2773 */ 2907 */
2774 if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */ 2908 if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
2775 { 2909 {
2776 czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */ 2910 czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
2777 exp_b10 = 0; /* Dot added below before first output. */ 2911 exp_b10 = 0; /* Dot added below before first output. */
2778 } 2912 }
2779 else 2913 else
2780 czero = 0; /* No zeros to add */ 2914 czero = 0; /* No zeros to add */
2781 2915
2782 /* Generate the digit list, stripping trailing zeros and 2916 /* Generate the digit list, stripping trailing zeros and
2783 * inserting a '.' before a digit if the exponent is 0. 2917 * inserting a '.' before a digit if the exponent is 0.
2784 */ 2918 */
2785 clead = czero; /* Count of leading zeros */ 2919 clead = czero; /* Count of leading zeros */
2786 cdigits = 0; /* Count of digits in list. */ 2920 cdigits = 0; /* Count of digits in list. */
2787 2921
2788 do 2922 do
2789 { 2923 {
2790 double d; 2924 double d;
2791 2925
2792 fp *= 10; 2926 fp *= 10;
2793 /* Use modf here, not floor and subtract, so that 2927 /* Use modf here, not floor and subtract, so that
2794 * the separation is done in one step. At the end 2928 * the separation is done in one step. At the end
2795 * of the loop don't break the number into parts so 2929 * of the loop don't break the number into parts so
2796 * that the final digit is rounded. 2930 * that the final digit is rounded.
2797 */ 2931 */
2798 if (cdigits+czero-clead+1 < (int)precision) 2932 if (cdigits+czero+1 < precision+clead)
2799 fp = modf(fp, &d); 2933 fp = modf(fp, &d);
2800 2934
2801 else 2935 else
2802 { 2936 {
2803 d = floor(fp + .5); 2937 d = floor(fp + .5);
2804 2938
2805 if (d > 9) 2939 if (d > 9)
2806 { 2940 {
2807 /* Rounding up to 10, handle that here. */ 2941 /* Rounding up to 10, handle that here. */
2808 if (czero > 0) 2942 if (czero > 0)
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
2886 { 3020 {
2887 if (exp_b10 == 0) *ascii++ = 46, --size; 3021 if (exp_b10 == 0) *ascii++ = 46, --size;
2888 /* PLUS 1: TOTAL 4 */ 3022 /* PLUS 1: TOTAL 4 */
2889 --exp_b10; 3023 --exp_b10;
2890 } 3024 }
2891 *ascii++ = 48, --czero; 3025 *ascii++ = 48, --czero;
2892 } 3026 }
2893 3027
2894 if (exp_b10 != (-1)) 3028 if (exp_b10 != (-1))
2895 { 3029 {
2896 if (exp_b10 == 0) *ascii++ = 46, --size; /* counted 3030 if (exp_b10 == 0)
2897 above */ 3031 *ascii++ = 46, --size; /* counted above */
3032
2898 --exp_b10; 3033 --exp_b10;
2899 } 3034 }
2900 *ascii++ = (char)(48 + (int)d), ++cdigits; 3035 *ascii++ = (char)(48 + (int)d), ++cdigits;
2901 } 3036 }
2902 } 3037 }
2903 while (cdigits+czero-clead < (int)precision && fp > DBL_MIN); 3038 while (cdigits+czero < precision+clead && fp > DBL_MIN);
2904 3039
2905 /* The total output count (max) is now 4+precision */ 3040 /* The total output count (max) is now 4+precision */
2906 3041
2907 /* Check for an exponent, if we don't need one we are 3042 /* Check for an exponent, if we don't need one we are
2908 * done and just need to terminate the string. At 3043 * done and just need to terminate the string. At
2909 * this point exp_b10==(-1) is effectively if flag - it got 3044 * this point exp_b10==(-1) is effectively if flag - it got
2910 * to '-1' because of the decrement after outputing 3045 * to '-1' because of the decrement after outputting
2911 * the decimal point above (the exponent required is 3046 * the decimal point above (the exponent required is
2912 * *not* -1!) 3047 * *not* -1!)
2913 */ 3048 */
2914 if (exp_b10 >= (-1) && exp_b10 <= 2) 3049 if (exp_b10 >= (-1) && exp_b10 <= 2)
2915 { 3050 {
2916 /* The following only happens if we didn't output the 3051 /* The following only happens if we didn't output the
2917 * leading zeros above for negative exponent, so this 3052 * leading zeros above for negative exponent, so this
2918 * doest add to the digit requirement. Note that the 3053 * doesn't add to the digit requirement. Note that the
2919 * two zeros here can only be output if the two leading 3054 * two zeros here can only be output if the two leading
2920 * zeros were *not* output, so this doesn't increase 3055 * zeros were *not* output, so this doesn't increase
2921 * the output count. 3056 * the output count.
2922 */ 3057 */
2923 while (--exp_b10 >= 0) *ascii++ = 48; 3058 while (--exp_b10 >= 0) *ascii++ = 48;
2924 3059
2925 *ascii = 0; 3060 *ascii = 0;
2926 3061
2927 /* Total buffer requirement (including the '\0') is 3062 /* Total buffer requirement (including the '\0') is
2928 * 5+precision - see check at the start. 3063 * 5+precision - see check at the start.
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2961 while (uexp_b10 > 0) 3096 while (uexp_b10 > 0)
2962 { 3097 {
2963 exponent[cdigits++] = (char)(48 + uexp_b10 % 10); 3098 exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
2964 uexp_b10 /= 10; 3099 uexp_b10 /= 10;
2965 } 3100 }
2966 } 3101 }
2967 3102
2968 /* Need another size check here for the exponent digits, so 3103 /* Need another size check here for the exponent digits, so
2969 * this need not be considered above. 3104 * this need not be considered above.
2970 */ 3105 */
2971 if ((int)size > cdigits) 3106 if (size > cdigits)
2972 { 3107 {
2973 while (cdigits > 0) *ascii++ = exponent[--cdigits]; 3108 while (cdigits > 0) *ascii++ = exponent[--cdigits];
2974 3109
2975 *ascii = 0; 3110 *ascii = 0;
2976 3111
2977 return; 3112 return;
2978 } 3113 }
2979 } 3114 }
2980 } 3115 }
2981 else if (!(fp >= DBL_MIN)) 3116 else if (!(fp >= DBL_MIN))
(...skipping 27 matching lines...) Expand all
3009 { 3144 {
3010 /* Require space for 10 decimal digits, a decimal point, a minus sign and a 3145 /* Require space for 10 decimal digits, a decimal point, a minus sign and a
3011 * trailing \0, 13 characters: 3146 * trailing \0, 13 characters:
3012 */ 3147 */
3013 if (size > 12) 3148 if (size > 12)
3014 { 3149 {
3015 png_uint_32 num; 3150 png_uint_32 num;
3016 3151
3017 /* Avoid overflow here on the minimum integer. */ 3152 /* Avoid overflow here on the minimum integer. */
3018 if (fp < 0) 3153 if (fp < 0)
3019 *ascii++ = 45, --size, num = -fp; 3154 *ascii++ = 45, num = -fp;
3020 else 3155 else
3021 num = fp; 3156 num = fp;
3022 3157
3023 if (num <= 0x80000000) /* else overflowed */ 3158 if (num <= 0x80000000) /* else overflowed */
3024 { 3159 {
3025 unsigned int ndigits = 0, first = 16 /* flag value */; 3160 unsigned int ndigits = 0, first = 16 /* flag value */;
3026 char digits[10]; 3161 char digits[10];
3027 3162
3028 while (num) 3163 while (num)
3029 { 3164 {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
3065 /* And null terminate the string: */ 3200 /* And null terminate the string: */
3066 *ascii = 0; 3201 *ascii = 0;
3067 return; 3202 return;
3068 } 3203 }
3069 } 3204 }
3070 3205
3071 /* Here on buffer too small. */ 3206 /* Here on buffer too small. */
3072 png_error(png_ptr, "ASCII conversion buffer too small"); 3207 png_error(png_ptr, "ASCII conversion buffer too small");
3073 } 3208 }
3074 # endif /* FIXED_POINT */ 3209 # endif /* FIXED_POINT */
3075 #endif /* READ_SCAL */ 3210 #endif /* SCAL */
3076 3211
3077 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \ 3212 #if defined(PNG_FLOATING_POINT_SUPPORTED) && \
3078 !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ 3213 !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \
3079 (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ 3214 (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \
3080 defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ 3215 defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3081 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ 3216 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \
3082 (defined(PNG_sCAL_SUPPORTED) && \ 3217 (defined(PNG_sCAL_SUPPORTED) && \
3083 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) 3218 defined(PNG_FLOATING_ARITHMETIC_SUPPORTED))
3084 png_fixed_point 3219 png_fixed_point
3085 png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text) 3220 png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text)
3086 { 3221 {
3087 double r = floor(100000 * fp + .5); 3222 double r = floor(100000 * fp + .5);
3088 3223
3089 if (r > 2147483647. || r < -2147483648.) 3224 if (r > 2147483647. || r < -2147483648.)
3090 png_fixed_error(png_ptr, text); 3225 png_fixed_error(png_ptr, text);
3091 3226
3227 # ifndef PNG_ERROR_TEXT_SUPPORTED
3228 PNG_UNUSED(text)
3229 # endif
3230
3092 return (png_fixed_point)r; 3231 return (png_fixed_point)r;
3093 } 3232 }
3094 #endif 3233 #endif
3095 3234
3096 #if defined(PNG_READ_GAMMA_SUPPORTED) || \ 3235 #if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_COLORSPACE_SUPPORTED) ||\
3097 defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) 3236 defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED)
3098 /* muldiv functions */ 3237 /* muldiv functions */
3099 /* This API takes signed arguments and rounds the result to the nearest 3238 /* This API takes signed arguments and rounds the result to the nearest
3100 * integer (or, for a fixed point number - the standard argument - to 3239 * integer (or, for a fixed point number - the standard argument - to
3101 * the nearest .00001). Overflow and divide by zero are signalled in 3240 * the nearest .00001). Overflow and divide by zero are signalled in
3102 * the result, a boolean - true on success, false on overflow. 3241 * the result, a boolean - true on success, false on overflow.
3103 */ 3242 */
3104 int 3243 int
3105 png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, 3244 png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
3106 png_int_32 divisor) 3245 png_int_32 divisor)
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
3191 3330
3192 else 3331 else
3193 if (s32 == d32 && s00 >= d00) 3332 if (s32 == d32 && s00 >= d00)
3194 s32 = 0, s00 -= d00, result += 1<<bitshift; 3333 s32 = 0, s00 -= d00, result += 1<<bitshift;
3195 } 3334 }
3196 3335
3197 /* Handle the rounding. */ 3336 /* Handle the rounding. */
3198 if (s00 >= (D >> 1)) 3337 if (s00 >= (D >> 1))
3199 ++result; 3338 ++result;
3200 3339
3201 if (negative) 3340 if (negative != 0)
3202 result = -result; 3341 result = -result;
3203 3342
3204 /* Check for overflow. */ 3343 /* Check for overflow. */
3205 if ((negative && result <= 0) || (!negative && result >= 0)) 3344 if ((negative != 0 && result <= 0) ||
3345 (negative == 0 && result >= 0))
3206 { 3346 {
3207 *res = result; 3347 *res = result;
3208 return 1; 3348 return 1;
3209 } 3349 }
3210 } 3350 }
3211 #endif 3351 #endif
3212 } 3352 }
3213 } 3353 }
3214 3354
3215 return 0; 3355 return 0;
3216 } 3356 }
3217 #endif /* READ_GAMMA || INCH_CONVERSIONS */ 3357 #endif /* READ_GAMMA || INCH_CONVERSIONS */
3218 3358
3219 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) 3359 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED)
3220 /* The following is for when the caller doesn't much care about the 3360 /* The following is for when the caller doesn't much care about the
3221 * result. 3361 * result.
3222 */ 3362 */
3223 png_fixed_point 3363 png_fixed_point
3224 png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times, 3364 png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times,
3225 png_int_32 divisor) 3365 png_int_32 divisor)
3226 { 3366 {
3227 png_fixed_point result; 3367 png_fixed_point result;
3228 3368
3229 if (png_muldiv(&result, a, times, divisor)) 3369 if (png_muldiv(&result, a, times, divisor) != 0)
3230 return result; 3370 return result;
3231 3371
3232 png_warning(png_ptr, "fixed point overflow ignored"); 3372 png_warning(png_ptr, "fixed point overflow ignored");
3233 return 0; 3373 return 0;
3234 } 3374 }
3235 #endif 3375 #endif
3236 3376
3237 #ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */ 3377 #ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */
3238 /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ 3378 /* Calculate a reciprocal, return 0 on div-by-zero or overflow. */
3239 png_fixed_point 3379 png_fixed_point
3240 png_reciprocal(png_fixed_point a) 3380 png_reciprocal(png_fixed_point a)
3241 { 3381 {
3242 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3382 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3243 double r = floor(1E10/a+.5); 3383 double r = floor(1E10/a+.5);
3244 3384
3245 if (r <= 2147483647. && r >= -2147483648.) 3385 if (r <= 2147483647. && r >= -2147483648.)
3246 return (png_fixed_point)r; 3386 return (png_fixed_point)r;
3247 #else 3387 #else
3248 png_fixed_point res; 3388 png_fixed_point res;
3249 3389
3250 if (png_muldiv(&res, 100000, 100000, a)) 3390 if (png_muldiv(&res, 100000, 100000, a) != 0)
3251 return res; 3391 return res;
3252 #endif 3392 #endif
3253 3393
3254 return 0; /* error/overflow */ 3394 return 0; /* error/overflow */
3255 } 3395 }
3256 3396
3257 /* This is the shared test on whether a gamma value is 'significant' - whether 3397 /* This is the shared test on whether a gamma value is 'significant' - whether
3258 * it is worth doing gamma correction. 3398 * it is worth doing gamma correction.
3259 */ 3399 */
3260 int /* PRIVATE */ 3400 int /* PRIVATE */
3261 png_gamma_significant(png_fixed_point gamma_val) 3401 png_gamma_significant(png_fixed_point gamma_val)
3262 { 3402 {
3263 return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || 3403 return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
3264 gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; 3404 gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
3265 } 3405 }
3266 #endif 3406 #endif
3267 3407
3268 #ifdef PNG_READ_GAMMA_SUPPORTED 3408 #ifdef PNG_READ_GAMMA_SUPPORTED
3409 #ifdef PNG_16BIT_SUPPORTED
3269 /* A local convenience routine. */ 3410 /* A local convenience routine. */
3270 static png_fixed_point 3411 static png_fixed_point
3271 png_product2(png_fixed_point a, png_fixed_point b) 3412 png_product2(png_fixed_point a, png_fixed_point b)
3272 { 3413 {
3273 /* The required result is 1/a * 1/b; the following preserves accuracy. */ 3414 /* The required result is 1/a * 1/b; the following preserves accuracy. */
3274 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3415 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3275 double r = a * 1E-5; 3416 double r = a * 1E-5;
3276 r *= b; 3417 r *= b;
3277 r = floor(r+.5); 3418 r = floor(r+.5);
3278 3419
3279 if (r <= 2147483647. && r >= -2147483648.) 3420 if (r <= 2147483647. && r >= -2147483648.)
3280 return (png_fixed_point)r; 3421 return (png_fixed_point)r;
3281 #else 3422 #else
3282 png_fixed_point res; 3423 png_fixed_point res;
3283 3424
3284 if (png_muldiv(&res, a, b, 100000)) 3425 if (png_muldiv(&res, a, b, 100000) != 0)
3285 return res; 3426 return res;
3286 #endif 3427 #endif
3287 3428
3288 return 0; /* overflow */ 3429 return 0; /* overflow */
3289 } 3430 }
3431 #endif /* 16BIT */
3290 3432
3291 /* The inverse of the above. */ 3433 /* The inverse of the above. */
3292 png_fixed_point 3434 png_fixed_point
3293 png_reciprocal2(png_fixed_point a, png_fixed_point b) 3435 png_reciprocal2(png_fixed_point a, png_fixed_point b)
3294 { 3436 {
3295 /* The required result is 1/a * 1/b; the following preserves accuracy. */ 3437 /* The required result is 1/a * 1/b; the following preserves accuracy. */
3296 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3438 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3297 double r = 1E15/a; 3439 if (a != 0 && b != 0)
3298 r /= b; 3440 {
3299 r = floor(r+.5); 3441 double r = 1E15/a;
3300 3442 r /= b;
3301 if (r <= 2147483647. && r >= -2147483648.) 3443 r = floor(r+.5);
3302 return (png_fixed_point)r; 3444
3445 if (r <= 2147483647. && r >= -2147483648.)
3446 return (png_fixed_point)r;
3447 }
3303 #else 3448 #else
3304 /* This may overflow because the range of png_fixed_point isn't symmetric, 3449 /* This may overflow because the range of png_fixed_point isn't symmetric,
3305 * but this API is only used for the product of file and screen gamma so it 3450 * but this API is only used for the product of file and screen gamma so it
3306 * doesn't matter that the smallest number it can produce is 1/21474, not 3451 * doesn't matter that the smallest number it can produce is 1/21474, not
3307 * 1/100000 3452 * 1/100000
3308 */ 3453 */
3309 png_fixed_point res = png_product2(a, b); 3454 png_fixed_point res = png_product2(a, b);
3310 3455
3311 if (res != 0) 3456 if (res != 0)
3312 return png_reciprocal(res); 3457 return png_reciprocal(res);
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
3431 * 3576 *
3432 * Since these numbers are so close to '1' we can use simple linear 3577 * Since these numbers are so close to '1' we can use simple linear
3433 * interpolation between the two end values 256/257 (result -368.61) and 258/257 3578 * interpolation between the two end values 256/257 (result -368.61) and 258/257
3434 * (result 367.179). The values used below are scaled by a further 64 to give 3579 * (result 367.179). The values used below are scaled by a further 64 to give
3435 * 16-bit precision in the interpolation: 3580 * 16-bit precision in the interpolation:
3436 * 3581 *
3437 * Start (256): -23591 3582 * Start (256): -23591
3438 * Zero (257): 0 3583 * Zero (257): 0
3439 * End (258): 23499 3584 * End (258): 23499
3440 */ 3585 */
3586 #ifdef PNG_16BIT_SUPPORTED
3441 static png_int_32 3587 static png_int_32
3442 png_log16bit(png_uint_32 x) 3588 png_log16bit(png_uint_32 x)
3443 { 3589 {
3444 unsigned int lg2 = 0; 3590 unsigned int lg2 = 0;
3445 3591
3446 /* As above, but now the input has 16 bits. */ 3592 /* As above, but now the input has 16 bits. */
3447 if ((x &= 0xffff) == 0) 3593 if ((x &= 0xffff) == 0)
3448 return -1; 3594 return -1;
3449 3595
3450 if ((x & 0xff00) == 0) 3596 if ((x & 0xff00) == 0)
(...skipping 30 matching lines...) Expand all
3481 3627
3482 if (x <= 65536U) /* <= '257' */ 3628 if (x <= 65536U) /* <= '257' */
3483 lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); 3629 lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
3484 3630
3485 else 3631 else
3486 lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); 3632 lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
3487 3633
3488 /* Safe, because the result can't have more than 20 bits: */ 3634 /* Safe, because the result can't have more than 20 bits: */
3489 return (png_int_32)((lg2 + 2048) >> 12); 3635 return (png_int_32)((lg2 + 2048) >> 12);
3490 } 3636 }
3637 #endif /* 16BIT */
3491 3638
3492 /* The 'exp()' case must invert the above, taking a 20-bit fixed point 3639 /* The 'exp()' case must invert the above, taking a 20-bit fixed point
3493 * logarithmic value and returning a 16 or 8-bit number as appropriate. In 3640 * logarithmic value and returning a 16 or 8-bit number as appropriate. In
3494 * each case only the low 16 bits are relevant - the fraction - since the 3641 * each case only the low 16 bits are relevant - the fraction - since the
3495 * integer bits (the top 4) simply determine a shift. 3642 * integer bits (the top 4) simply determine a shift.
3496 * 3643 *
3497 * The worst case is the 16-bit distinction between 65535 and 65534, this 3644 * The worst case is the 16-bit distinction between 65535 and 65534. This
3498 * requires perhaps spurious accuracty in the decoding of the logarithm to 3645 * requires perhaps spurious accuracy in the decoding of the logarithm to
3499 * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance 3646 * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance
3500 * of getting this accuracy in practice. 3647 * of getting this accuracy in practice.
3501 * 3648 *
3502 * To deal with this the following exp() function works out the exponent of the 3649 * To deal with this the following exp() function works out the exponent of the
3503 * frational part of the logarithm by using an accurate 32-bit value from the 3650 * frational part of the logarithm by using an accurate 32-bit value from the
3504 * top four fractional bits then multiplying in the remaining bits. 3651 * top four fractional bits then multiplying in the remaining bits.
3505 */ 3652 */
3506 static const png_uint_32 3653 static const png_uint_32
3507 png_32bit_exp[16] = 3654 png_32bit_exp[16] =
3508 { 3655 {
(...skipping 19 matching lines...) Expand all
3528 1 45425.61317555035558641664 3675 1 45425.61317555035558641664
3529 0 45425.85339951654943850496 3676 0 45425.85339951654943850496
3530 #endif 3677 #endif
3531 3678
3532 static png_uint_32 3679 static png_uint_32
3533 png_exp(png_fixed_point x) 3680 png_exp(png_fixed_point x)
3534 { 3681 {
3535 if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ 3682 if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
3536 { 3683 {
3537 /* Obtain a 4-bit approximation */ 3684 /* Obtain a 4-bit approximation */
3538 png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf]; 3685 png_uint_32 e = png_32bit_exp[(x >> 12) & 0x0f];
3539 3686
3540 /* Incorporate the low 12 bits - these decrease the returned value by 3687 /* Incorporate the low 12 bits - these decrease the returned value by
3541 * multiplying by a number less than 1 if the bit is set. The multiplier 3688 * multiplying by a number less than 1 if the bit is set. The multiplier
3542 * is determined by the above table and the shift. Notice that the values 3689 * is determined by the above table and the shift. Notice that the values
3543 * converge on 45426 and this is used to allow linear interpolation of the 3690 * converge on 45426 and this is used to allow linear interpolation of the
3544 * low bits. 3691 * low bits.
3545 */ 3692 */
3546 if (x & 0x800) 3693 if (x & 0x800)
3547 e -= (((e >> 16) * 44938U) + 16U) >> 5; 3694 e -= (((e >> 16) * 44938U) + 16U) >> 5;
3548 3695
(...skipping 27 matching lines...) Expand all
3576 /* Else underflow */ 3723 /* Else underflow */
3577 return 0; 3724 return 0;
3578 } 3725 }
3579 3726
3580 static png_byte 3727 static png_byte
3581 png_exp8bit(png_fixed_point lg2) 3728 png_exp8bit(png_fixed_point lg2)
3582 { 3729 {
3583 /* Get a 32-bit value: */ 3730 /* Get a 32-bit value: */
3584 png_uint_32 x = png_exp(lg2); 3731 png_uint_32 x = png_exp(lg2);
3585 3732
3586 /* Convert the 32-bit value to 0..255 by multiplying by 256-1, note that the 3733 /* Convert the 32-bit value to 0..255 by multiplying by 256-1. Note that the
3587 * second, rounding, step can't overflow because of the first, subtraction, 3734 * second, rounding, step can't overflow because of the first, subtraction,
3588 * step. 3735 * step.
3589 */ 3736 */
3590 x -= x >> 8; 3737 x -= x >> 8;
3591 return (png_byte)((x + 0x7fffffU) >> 24); 3738 return (png_byte)(((x + 0x7fffffU) >> 24) & 0xff);
3592 } 3739 }
3593 3740
3741 #ifdef PNG_16BIT_SUPPORTED
3594 static png_uint_16 3742 static png_uint_16
3595 png_exp16bit(png_fixed_point lg2) 3743 png_exp16bit(png_fixed_point lg2)
3596 { 3744 {
3597 /* Get a 32-bit value: */ 3745 /* Get a 32-bit value: */
3598 png_uint_32 x = png_exp(lg2); 3746 png_uint_32 x = png_exp(lg2);
3599 3747
3600 /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */ 3748 /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */
3601 x -= x >> 16; 3749 x -= x >> 16;
3602 return (png_uint_16)((x + 32767U) >> 16); 3750 return (png_uint_16)((x + 32767U) >> 16);
3603 } 3751 }
3752 #endif /* 16BIT */
3604 #endif /* FLOATING_ARITHMETIC */ 3753 #endif /* FLOATING_ARITHMETIC */
3605 3754
3606 png_byte 3755 png_byte
3607 png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) 3756 png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
3608 { 3757 {
3609 if (value > 0 && value < 255) 3758 if (value > 0 && value < 255)
3610 { 3759 {
3611 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3760 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3612 double r = floor(255*pow(value/255.,gamma_val*.00001)+.5); 3761 /* 'value' is unsigned, ANSI-C90 requires the compiler to correctly
3762 * convert this to a floating point value. This includes values that
3763 * would overflow if 'value' were to be converted to 'int'.
3764 *
3765 * Apparently GCC, however, does an intermediate conversion to (int)
3766 * on some (ARM) but not all (x86) platforms, possibly because of
3767 * hardware FP limitations. (E.g. if the hardware conversion always
3768 * assumes the integer register contains a signed value.) This results
3769 * in ANSI-C undefined behavior for large values.
3770 *
3771 * Other implementations on the same machine might actually be ANSI-C90
3772 * conformant and therefore compile spurious extra code for the large
3773 * values.
3774 *
3775 * We can be reasonably sure that an unsigned to float conversion
3776 * won't be faster than an int to float one. Therefore this code
3777 * assumes responsibility for the undefined behavior, which it knows
3778 * can't happen because of the check above.
3779 *
3780 * Note the argument to this routine is an (unsigned int) because, on
3781 * 16-bit platforms, it is assigned a value which might be out of
3782 * range for an (int); that would result in undefined behavior in the
3783 * caller if the *argument* ('value') were to be declared (int).
3784 */
3785 double r = floor(255*pow((int)/*SAFE*/value/255.,gamma_val*.00001)+.5);
3613 return (png_byte)r; 3786 return (png_byte)r;
3614 # else 3787 # else
3615 png_int_32 lg2 = png_log8bit(value); 3788 png_int_32 lg2 = png_log8bit(value);
3616 png_fixed_point res; 3789 png_fixed_point res;
3617 3790
3618 if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) 3791 if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0)
3619 return png_exp8bit(res); 3792 return png_exp8bit(res);
3620 3793
3621 /* Overflow. */ 3794 /* Overflow. */
3622 value = 0; 3795 value = 0;
3623 # endif 3796 # endif
3624 } 3797 }
3625 3798
3626 return (png_byte)value; 3799 return (png_byte)(value & 0xff);
3627 } 3800 }
3628 3801
3802 #ifdef PNG_16BIT_SUPPORTED
3629 png_uint_16 3803 png_uint_16
3630 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) 3804 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
3631 { 3805 {
3632 if (value > 0 && value < 65535) 3806 if (value > 0 && value < 65535)
3633 { 3807 {
3634 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3808 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3635 double r = floor(65535*pow(value/65535.,gamma_val*.00001)+.5); 3809 /* The same (unsigned int)->(double) constraints apply here as above,
3810 * however in this case the (unsigned int) to (int) conversion can
3811 * overflow on an ANSI-C90 compliant system so the cast needs to ensure
3812 * that this is not possible.
3813 */
3814 double r = floor(65535*pow((png_int_32)value/65535.,
3815 gamma_val*.00001)+.5);
3636 return (png_uint_16)r; 3816 return (png_uint_16)r;
3637 # else 3817 # else
3638 png_int_32 lg2 = png_log16bit(value); 3818 png_int_32 lg2 = png_log16bit(value);
3639 png_fixed_point res; 3819 png_fixed_point res;
3640 3820
3641 if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1)) 3821 if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0)
3642 return png_exp16bit(res); 3822 return png_exp16bit(res);
3643 3823
3644 /* Overflow. */ 3824 /* Overflow. */
3645 value = 0; 3825 value = 0;
3646 # endif 3826 # endif
3647 } 3827 }
3648 3828
3649 return (png_uint_16)value; 3829 return (png_uint_16)value;
3650 } 3830 }
3831 #endif /* 16BIT */
3651 3832
3652 /* This does the right thing based on the bit_depth field of the 3833 /* This does the right thing based on the bit_depth field of the
3653 * png_struct, interpreting values as 8-bit or 16-bit. While the result 3834 * png_struct, interpreting values as 8-bit or 16-bit. While the result
3654 * is nominally a 16-bit value if bit depth is 8 then the result is 3835 * is nominally a 16-bit value if bit depth is 8 then the result is
3655 * 8-bit (as are the arguments.) 3836 * 8-bit (as are the arguments.)
3656 */ 3837 */
3657 png_uint_16 /* PRIVATE */ 3838 png_uint_16 /* PRIVATE */
3658 png_gamma_correct(png_structrp png_ptr, unsigned int value, 3839 png_gamma_correct(png_structrp png_ptr, unsigned int value,
3659 png_fixed_point gamma_val) 3840 png_fixed_point gamma_val)
3660 { 3841 {
3661 if (png_ptr->bit_depth == 8) 3842 if (png_ptr->bit_depth == 8)
3662 return png_gamma_8bit_correct(value, gamma_val); 3843 return png_gamma_8bit_correct(value, gamma_val);
3663 3844
3845 #ifdef PNG_16BIT_SUPPORTED
3664 else 3846 else
3665 return png_gamma_16bit_correct(value, gamma_val); 3847 return png_gamma_16bit_correct(value, gamma_val);
3666 } 3848 #else
3667 3849 /* should not reach this */
3850 return 0;
3851 #endif /* 16BIT */
3852 }
3853
3854 #ifdef PNG_16BIT_SUPPORTED
3668 /* Internal function to build a single 16-bit table - the table consists of 3855 /* Internal function to build a single 16-bit table - the table consists of
3669 * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount 3856 * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount
3670 * to shift the input values right (or 16-number_of_signifiant_bits). 3857 * to shift the input values right (or 16-number_of_signifiant_bits).
3671 * 3858 *
3672 * The caller is responsible for ensuring that the table gets cleaned up on 3859 * The caller is responsible for ensuring that the table gets cleaned up on
3673 * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument 3860 * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument
3674 * should be somewhere that will be cleaned. 3861 * should be somewhere that will be cleaned.
3675 */ 3862 */
3676 static void 3863 static void
3677 png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable, 3864 png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable,
3678 PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) 3865 PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
3679 { 3866 {
3680 /* Various values derived from 'shift': */ 3867 /* Various values derived from 'shift': */
3681 PNG_CONST unsigned int num = 1U << (8U - shift); 3868 PNG_CONST unsigned int num = 1U << (8U - shift);
3869 #ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3870 /* CSE the division and work round wacky GCC warnings (see the comments
3871 * in png_gamma_8bit_correct for where these come from.)
3872 */
3873 PNG_CONST double fmax = 1./(((png_int_32)1 << (16U - shift))-1);
3874 #endif
3682 PNG_CONST unsigned int max = (1U << (16U - shift))-1U; 3875 PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
3683 PNG_CONST unsigned int max_by_2 = 1U << (15U-shift); 3876 PNG_CONST unsigned int max_by_2 = 1U << (15U-shift);
3684 unsigned int i; 3877 unsigned int i;
3685 3878
3686 png_uint_16pp table = *ptable = 3879 png_uint_16pp table = *ptable =
3687 (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); 3880 (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
3688 3881
3689 for (i = 0; i < num; i++) 3882 for (i = 0; i < num; i++)
3690 { 3883 {
3691 png_uint_16p sub_table = table[i] = 3884 png_uint_16p sub_table = table[i] =
3692 (png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16))); 3885 (png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16)));
3693 3886
3694 /* The 'threshold' test is repeated here because it can arise for one of 3887 /* The 'threshold' test is repeated here because it can arise for one of
3695 * the 16-bit tables even if the others don't hit it. 3888 * the 16-bit tables even if the others don't hit it.
3696 */ 3889 */
3697 if (png_gamma_significant(gamma_val)) 3890 if (png_gamma_significant(gamma_val) != 0)
3698 { 3891 {
3699 /* The old code would overflow at the end and this would cause the 3892 /* The old code would overflow at the end and this would cause the
3700 * 'pow' function to return a result >1, resulting in an 3893 * 'pow' function to return a result >1, resulting in an
3701 * arithmetic error. This code follows the spec exactly; ig is 3894 * arithmetic error. This code follows the spec exactly; ig is
3702 * the recovered input sample, it always has 8-16 bits. 3895 * the recovered input sample, it always has 8-16 bits.
3703 * 3896 *
3704 * We want input * 65535/max, rounded, the arithmetic fits in 32 3897 * We want input * 65535/max, rounded, the arithmetic fits in 32
3705 * bits (unsigned) so long as max <= 32767. 3898 * bits (unsigned) so long as max <= 32767.
3706 */ 3899 */
3707 unsigned int j; 3900 unsigned int j;
3708 for (j = 0; j < 256; j++) 3901 for (j = 0; j < 256; j++)
3709 { 3902 {
3710 png_uint_32 ig = (j << (8-shift)) + i; 3903 png_uint_32 ig = (j << (8-shift)) + i;
3711 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED 3904 # ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
3712 /* Inline the 'max' scaling operation: */ 3905 /* Inline the 'max' scaling operation: */
3713 double d = floor(65535*pow(ig/(double)max, gamma_val*.00001)+.5); 3906 /* See png_gamma_8bit_correct for why the cast to (int) is
3907 * required here.
3908 */
3909 double d = floor(65535.*pow(ig*fmax, gamma_val*.00001)+.5);
3714 sub_table[j] = (png_uint_16)d; 3910 sub_table[j] = (png_uint_16)d;
3715 # else 3911 # else
3716 if (shift) 3912 if (shift != 0)
3717 ig = (ig * 65535U + max_by_2)/max; 3913 ig = (ig * 65535U + max_by_2)/max;
3718 3914
3719 sub_table[j] = png_gamma_16bit_correct(ig, gamma_val); 3915 sub_table[j] = png_gamma_16bit_correct(ig, gamma_val);
3720 # endif 3916 # endif
3721 } 3917 }
3722 } 3918 }
3723 else 3919 else
3724 { 3920 {
3725 /* We must still build a table, but do it the fast way. */ 3921 /* We must still build a table, but do it the fast way. */
3726 unsigned int j; 3922 unsigned int j;
3727 3923
3728 for (j = 0; j < 256; j++) 3924 for (j = 0; j < 256; j++)
3729 { 3925 {
3730 png_uint_32 ig = (j << (8-shift)) + i; 3926 png_uint_32 ig = (j << (8-shift)) + i;
3731 3927
3732 if (shift) 3928 if (shift != 0)
3733 ig = (ig * 65535U + max_by_2)/max; 3929 ig = (ig * 65535U + max_by_2)/max;
3734 3930
3735 sub_table[j] = (png_uint_16)ig; 3931 sub_table[j] = (png_uint_16)ig;
3736 } 3932 }
3737 } 3933 }
3738 } 3934 }
3739 } 3935 }
3740 3936
3741 /* NOTE: this function expects the *inverse* of the overall gamma transformation 3937 /* NOTE: this function expects the *inverse* of the overall gamma transformation
3742 * required. 3938 * required.
3743 */ 3939 */
3744 static void 3940 static void
3745 png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable, 3941 png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable,
3746 PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val) 3942 PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
3747 { 3943 {
3748 PNG_CONST unsigned int num = 1U << (8U - shift); 3944 PNG_CONST unsigned int num = 1U << (8U - shift);
3749 PNG_CONST unsigned int max = (1U << (16U - shift))-1U; 3945 PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
3750 unsigned int i; 3946 unsigned int i;
3751 png_uint_32 last; 3947 png_uint_32 last;
3752 3948
3753 png_uint_16pp table = *ptable = 3949 png_uint_16pp table = *ptable =
3754 (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); 3950 (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
3755 3951
3756 /* 'num' is the number of tables and also the number of low bits of low 3952 /* 'num' is the number of tables and also the number of low bits of low
3757 * bits of the input 16-bit value used to select a table. Each table is 3953 * bits of the input 16-bit value used to select a table. Each table is
3758 * itself index by the high 8 bits of the value. 3954 * itself indexed by the high 8 bits of the value.
3759 */ 3955 */
3760 for (i = 0; i < num; i++) 3956 for (i = 0; i < num; i++)
3761 table[i] = (png_uint_16p)png_malloc(png_ptr, 3957 table[i] = (png_uint_16p)png_malloc(png_ptr,
3762 256 * (sizeof (png_uint_16))); 3958 256 * (sizeof (png_uint_16)));
3763 3959
3764 /* 'gamma_val' is set to the reciprocal of the value calculated above, so 3960 /* 'gamma_val' is set to the reciprocal of the value calculated above, so
3765 * pow(out,g) is an *input* value. 'last' is the last input value set. 3961 * pow(out,g) is an *input* value. 'last' is the last input value set.
3766 * 3962 *
3767 * In the loop 'i' is used to find output values. Since the output is 3963 * In the loop 'i' is used to find output values. Since the output is
3768 * 8-bit there are only 256 possible values. The tables are set up to 3964 * 8-bit there are only 256 possible values. The tables are set up to
(...skipping 27 matching lines...) Expand all
3796 } 3992 }
3797 } 3993 }
3798 3994
3799 /* And fill in the final entries. */ 3995 /* And fill in the final entries. */
3800 while (last < (num << 8)) 3996 while (last < (num << 8))
3801 { 3997 {
3802 table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U; 3998 table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U;
3803 last++; 3999 last++;
3804 } 4000 }
3805 } 4001 }
4002 #endif /* 16BIT */
3806 4003
3807 /* Build a single 8-bit table: same as the 16-bit case but much simpler (and 4004 /* Build a single 8-bit table: same as the 16-bit case but much simpler (and
3808 * typically much faster). Note that libpng currently does no sBIT processing 4005 * typically much faster). Note that libpng currently does no sBIT processing
3809 * (apparently contrary to the spec) so a 256 entry table is always generated. 4006 * (apparently contrary to the spec) so a 256-entry table is always generated.
3810 */ 4007 */
3811 static void 4008 static void
3812 png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable, 4009 png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable,
3813 PNG_CONST png_fixed_point gamma_val) 4010 PNG_CONST png_fixed_point gamma_val)
3814 { 4011 {
3815 unsigned int i; 4012 unsigned int i;
3816 png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256); 4013 png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256);
3817 4014
3818 if (png_gamma_significant(gamma_val)) for (i=0; i<256; i++) 4015 if (png_gamma_significant(gamma_val) != 0)
3819 table[i] = png_gamma_8bit_correct(i, gamma_val); 4016 for (i=0; i<256; i++)
3820 4017 table[i] = png_gamma_8bit_correct(i, gamma_val);
3821 else for (i=0; i<256; ++i) 4018
3822 table[i] = (png_byte)i; 4019 else
4020 for (i=0; i<256; ++i)
4021 table[i] = (png_byte)(i & 0xff);
3823 } 4022 }
3824 4023
3825 /* Used from png_read_destroy and below to release the memory used by the gamma 4024 /* Used from png_read_destroy and below to release the memory used by the gamma
3826 * tables. 4025 * tables.
3827 */ 4026 */
3828 void /* PRIVATE */ 4027 void /* PRIVATE */
3829 png_destroy_gamma_table(png_structrp png_ptr) 4028 png_destroy_gamma_table(png_structrp png_ptr)
3830 { 4029 {
3831 png_free(png_ptr, png_ptr->gamma_table); 4030 png_free(png_ptr, png_ptr->gamma_table);
3832 png_ptr->gamma_table = NULL; 4031 png_ptr->gamma_table = NULL;
3833 4032
4033 #ifdef PNG_16BIT_SUPPORTED
3834 if (png_ptr->gamma_16_table != NULL) 4034 if (png_ptr->gamma_16_table != NULL)
3835 { 4035 {
3836 int i; 4036 int i;
3837 int istop = (1 << (8 - png_ptr->gamma_shift)); 4037 int istop = (1 << (8 - png_ptr->gamma_shift));
3838 for (i = 0; i < istop; i++) 4038 for (i = 0; i < istop; i++)
3839 { 4039 {
3840 png_free(png_ptr, png_ptr->gamma_16_table[i]); 4040 png_free(png_ptr, png_ptr->gamma_16_table[i]);
3841 } 4041 }
3842 png_free(png_ptr, png_ptr->gamma_16_table); 4042 png_free(png_ptr, png_ptr->gamma_16_table);
3843 png_ptr->gamma_16_table = NULL; 4043 png_ptr->gamma_16_table = NULL;
3844 } 4044 }
4045 #endif /* 16BIT */
3845 4046
3846 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ 4047 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3847 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ 4048 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
3848 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 4049 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
3849 png_free(png_ptr, png_ptr->gamma_from_1); 4050 png_free(png_ptr, png_ptr->gamma_from_1);
3850 png_ptr->gamma_from_1 = NULL; 4051 png_ptr->gamma_from_1 = NULL;
3851 png_free(png_ptr, png_ptr->gamma_to_1); 4052 png_free(png_ptr, png_ptr->gamma_to_1);
3852 png_ptr->gamma_to_1 = NULL; 4053 png_ptr->gamma_to_1 = NULL;
3853 4054
4055 #ifdef PNG_16BIT_SUPPORTED
3854 if (png_ptr->gamma_16_from_1 != NULL) 4056 if (png_ptr->gamma_16_from_1 != NULL)
3855 { 4057 {
3856 int i; 4058 int i;
3857 int istop = (1 << (8 - png_ptr->gamma_shift)); 4059 int istop = (1 << (8 - png_ptr->gamma_shift));
3858 for (i = 0; i < istop; i++) 4060 for (i = 0; i < istop; i++)
3859 { 4061 {
3860 png_free(png_ptr, png_ptr->gamma_16_from_1[i]); 4062 png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
3861 } 4063 }
3862 png_free(png_ptr, png_ptr->gamma_16_from_1); 4064 png_free(png_ptr, png_ptr->gamma_16_from_1);
3863 png_ptr->gamma_16_from_1 = NULL; 4065 png_ptr->gamma_16_from_1 = NULL;
3864 } 4066 }
3865 if (png_ptr->gamma_16_to_1 != NULL) 4067 if (png_ptr->gamma_16_to_1 != NULL)
3866 { 4068 {
3867 int i; 4069 int i;
3868 int istop = (1 << (8 - png_ptr->gamma_shift)); 4070 int istop = (1 << (8 - png_ptr->gamma_shift));
3869 for (i = 0; i < istop; i++) 4071 for (i = 0; i < istop; i++)
3870 { 4072 {
3871 png_free(png_ptr, png_ptr->gamma_16_to_1[i]); 4073 png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
3872 } 4074 }
3873 png_free(png_ptr, png_ptr->gamma_16_to_1); 4075 png_free(png_ptr, png_ptr->gamma_16_to_1);
3874 png_ptr->gamma_16_to_1 = NULL; 4076 png_ptr->gamma_16_to_1 = NULL;
3875 } 4077 }
4078 #endif /* 16BIT */
3876 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ 4079 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
3877 } 4080 }
3878 4081
3879 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit 4082 /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
3880 * tables, we don't make a full table if we are reducing to 8-bit in 4083 * tables, we don't make a full table if we are reducing to 8-bit in
3881 * the future. Note also how the gamma_16 tables are segmented so that 4084 * the future. Note also how the gamma_16 tables are segmented so that
3882 * we don't need to allocate > 64K chunks for a full 16-bit table. 4085 * we don't need to allocate > 64K chunks for a full 16-bit table.
3883 */ 4086 */
3884 void /* PRIVATE */ 4087 void /* PRIVATE */
3885 png_build_gamma_table(png_structrp png_ptr, int bit_depth) 4088 png_build_gamma_table(png_structrp png_ptr, int bit_depth)
(...skipping 14 matching lines...) Expand all
3900 4103
3901 if (bit_depth <= 8) 4104 if (bit_depth <= 8)
3902 { 4105 {
3903 png_build_8bit_table(png_ptr, &png_ptr->gamma_table, 4106 png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
3904 png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, 4107 png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
3905 png_ptr->screen_gamma) : PNG_FP_1); 4108 png_ptr->screen_gamma) : PNG_FP_1);
3906 4109
3907 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ 4110 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3908 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ 4111 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
3909 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 4112 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
3910 if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) 4113 if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
3911 { 4114 {
3912 png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1, 4115 png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
3913 png_reciprocal(png_ptr->colorspace.gamma)); 4116 png_reciprocal(png_ptr->colorspace.gamma));
3914 4117
3915 png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, 4118 png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
3916 png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : 4119 png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
3917 png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); 4120 png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
3918 } 4121 }
3919 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ 4122 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
3920 } 4123 }
4124 #ifdef PNG_16BIT_SUPPORTED
3921 else 4125 else
3922 { 4126 {
3923 png_byte shift, sig_bit; 4127 png_byte shift, sig_bit;
3924 4128
3925 if (png_ptr->color_type & PNG_COLOR_MASK_COLOR) 4129 if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
3926 { 4130 {
3927 sig_bit = png_ptr->sig_bit.red; 4131 sig_bit = png_ptr->sig_bit.red;
3928 4132
3929 if (png_ptr->sig_bit.green > sig_bit) 4133 if (png_ptr->sig_bit.green > sig_bit)
3930 sig_bit = png_ptr->sig_bit.green; 4134 sig_bit = png_ptr->sig_bit.green;
3931 4135
3932 if (png_ptr->sig_bit.blue > sig_bit) 4136 if (png_ptr->sig_bit.blue > sig_bit)
3933 sig_bit = png_ptr->sig_bit.blue; 4137 sig_bit = png_ptr->sig_bit.blue;
3934 } 4138 }
3935 else 4139 else
3936 sig_bit = png_ptr->sig_bit.gray; 4140 sig_bit = png_ptr->sig_bit.gray;
3937 4141
3938 /* 16-bit gamma code uses this equation: 4142 /* 16-bit gamma code uses this equation:
3939 * 4143 *
3940 * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8] 4144 * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
3941 * 4145 *
3942 * Where 'iv' is the input color value and 'ov' is the output value - 4146 * Where 'iv' is the input color value and 'ov' is the output value -
3943 * pow(iv, gamma). 4147 * pow(iv, gamma).
3944 * 4148 *
3945 * Thus the gamma table consists of up to 256 256 entry tables. The table 4149 * Thus the gamma table consists of up to 256 256-entry tables. The table
3946 * is selected by the (8-gamma_shift) most significant of the low 8 bits of 4150 * is selected by the (8-gamma_shift) most significant of the low 8 bits of
3947 * the color value then indexed by the upper 8 bits: 4151 * the color value then indexed by the upper 8 bits:
3948 * 4152 *
3949 * table[low bits][high 8 bits] 4153 * table[low bits][high 8 bits]
3950 * 4154 *
3951 * So the table 'n' corresponds to all those 'iv' of: 4155 * So the table 'n' corresponds to all those 'iv' of:
3952 * 4156 *
3953 * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1> 4157 * <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
3954 * 4158 *
3955 */ 4159 */
3956 if (sig_bit > 0 && sig_bit < 16U) 4160 if (sig_bit > 0 && sig_bit < 16U)
3957 shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */ 4161 /* shift == insignificant bits */
4162 shift = (png_byte)((16U - sig_bit) & 0xff);
3958 4163
3959 else 4164 else
3960 shift = 0; /* keep all 16 bits */ 4165 shift = 0; /* keep all 16 bits */
3961 4166
3962 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) 4167 if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
3963 { 4168 {
3964 /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively 4169 /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
3965 * the significant bits in the *input* when the output will 4170 * the significant bits in the *input* when the output will
3966 * eventually be 8 bits. By default it is 11. 4171 * eventually be 8 bits. By default it is 11.
3967 */ 4172 */
3968 if (shift < (16U - PNG_MAX_GAMMA_8)) 4173 if (shift < (16U - PNG_MAX_GAMMA_8))
3969 shift = (16U - PNG_MAX_GAMMA_8); 4174 shift = (16U - PNG_MAX_GAMMA_8);
3970 } 4175 }
3971 4176
3972 if (shift > 8U) 4177 if (shift > 8U)
3973 shift = 8U; /* Guarantees at least one table! */ 4178 shift = 8U; /* Guarantees at least one table! */
3974 4179
3975 png_ptr->gamma_shift = shift; 4180 png_ptr->gamma_shift = shift;
3976 4181
3977 #ifdef PNG_16BIT_SUPPORTED
3978 /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now 4182 /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
3979 * PNG_COMPOSE). This effectively smashed the background calculation for 4183 * PNG_COMPOSE). This effectively smashed the background calculation for
3980 * 16-bit output because the 8-bit table assumes the result will be reduced 4184 * 16-bit output because the 8-bit table assumes the result will be reduced
3981 * to 8 bits. 4185 * to 8 bits.
3982 */ 4186 */
3983 if (png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) 4187 if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
3984 #endif
3985 png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, 4188 png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
3986 png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma, 4189 png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
3987 png_ptr->screen_gamma) : PNG_FP_1); 4190 png_ptr->screen_gamma) : PNG_FP_1);
3988 4191
3989 #ifdef PNG_16BIT_SUPPORTED
3990 else 4192 else
3991 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, 4193 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
3992 png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, 4194 png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
3993 png_ptr->screen_gamma) : PNG_FP_1); 4195 png_ptr->screen_gamma) : PNG_FP_1);
3994 #endif
3995 4196
3996 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ 4197 #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
3997 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ 4198 defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
3998 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) 4199 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
3999 if (png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) 4200 if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
4000 { 4201 {
4001 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift, 4202 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
4002 png_reciprocal(png_ptr->colorspace.gamma)); 4203 png_reciprocal(png_ptr->colorspace.gamma));
4003 4204
4004 /* Notice that the '16 from 1' table should be full precision, however 4205 /* Notice that the '16 from 1' table should be full precision, however
4005 * the lookup on this table still uses gamma_shift, so it can't be. 4206 * the lookup on this table still uses gamma_shift, so it can't be.
4006 * TODO: fix this. 4207 * TODO: fix this.
4007 */ 4208 */
4008 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift, 4209 png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
4009 png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : 4210 png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
4010 png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); 4211 png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
4011 } 4212 }
4012 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ 4213 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
4013 } 4214 }
4215 #endif /* 16BIT */
4014 } 4216 }
4015 #endif /* READ_GAMMA */ 4217 #endif /* READ_GAMMA */
4016 4218
4017 /* HARDWARE OPTION SUPPORT */ 4219 /* HARDWARE OR SOFTWARE OPTION SUPPORT */
4018 #ifdef PNG_SET_OPTION_SUPPORTED 4220 #ifdef PNG_SET_OPTION_SUPPORTED
4019 int PNGAPI 4221 int PNGAPI
4020 png_set_option(png_structrp png_ptr, int option, int onoff) 4222 png_set_option(png_structrp png_ptr, int option, int onoff)
4021 { 4223 {
4022 if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT && 4224 if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
4023 (option & 1) == 0) 4225 (option & 1) == 0)
4024 { 4226 {
4025 int mask = 3 << option; 4227 int mask = 3 << option;
4026 int setting = (2 + (onoff != 0)) << option; 4228 int setting = (2 + (onoff != 0)) << option;
4027 int current = png_ptr->options; 4229 int current = png_ptr->options;
4028 4230
4029 png_ptr->options = (png_byte)((current & ~mask) | setting); 4231 png_ptr->options = (png_byte)(((current & ~mask) | setting) & 0xff);
4030 4232
4031 return (current & mask) >> option; 4233 return (current & mask) >> option;
4032 } 4234 }
4033 4235
4034 return PNG_OPTION_INVALID; 4236 return PNG_OPTION_INVALID;
4035 } 4237 }
4036 #endif 4238 #endif
4037 4239
4038 /* sRGB support */ 4240 /* sRGB support */
4039 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ 4241 #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
4040 defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) 4242 defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
4041 /* sRGB conversion tables; these are machine generated with the code in 4243 /* sRGB conversion tables; these are machine generated with the code in
4042 * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the 4244 * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the
4043 * specification (see the article at http://en.wikipedia.org/wiki/SRGB) 4245 * specification (see the article at http://en.wikipedia.org/wiki/SRGB)
4044 * is used, not the gamma=1/2.2 approximation use elsewhere in libpng. 4246 * is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
4045 * The sRGB to linear table is exact (to the nearest 16 bit linear fraction). 4247 * The sRGB to linear table is exact (to the nearest 16-bit linear fraction).
4046 * The inverse (linear to sRGB) table has accuracies as follows: 4248 * The inverse (linear to sRGB) table has accuracies as follows:
4047 * 4249 *
4048 * For all possible (255*65535+1) input values: 4250 * For all possible (255*65535+1) input values:
4049 * 4251 *
4050 * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact 4252 * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact
4051 * 4253 *
4052 * For the input values corresponding to the 65536 16-bit values: 4254 * For the input values corresponding to the 65536 16-bit values:
4053 * 4255 *
4054 * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact 4256 * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact
4055 * 4257 *
4056 * In all cases the inexact readings are off by one. 4258 * In all cases the inexact readings are only off by one.
4057 */ 4259 */
4058 4260
4059 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED 4261 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
4060 /* The convert-to-sRGB table is only currently required for read. */ 4262 /* The convert-to-sRGB table is only currently required for read. */
4061 const png_uint_16 png_sRGB_table[256] = 4263 const png_uint_16 png_sRGB_table[256] =
4062 { 4264 {
4063 0,20,40,60,80,99,119,139, 4265 0,20,40,60,80,99,119,139,
4064 159,179,199,219,241,264,288,313, 4266 159,179,199,219,241,264,288,313,
4065 340,367,396,427,458,491,526,562, 4267 340,367,396,427,458,491,526,562,
4066 599,637,677,718,761,805,851,898, 4268 599,637,677,718,761,805,851,898,
(...skipping 19 matching lines...) Expand all
4086 31412,31794,32179,32567,32957,33350,33745,34143, 4288 31412,31794,32179,32567,32957,33350,33745,34143,
4087 34544,34948,35355,35764,36176,36591,37008,37429, 4289 34544,34948,35355,35764,36176,36591,37008,37429,
4088 37852,38278,38706,39138,39572,40009,40449,40891, 4290 37852,38278,38706,39138,39572,40009,40449,40891,
4089 41337,41785,42236,42690,43147,43606,44069,44534, 4291 41337,41785,42236,42690,43147,43606,44069,44534,
4090 45002,45473,45947,46423,46903,47385,47871,48359, 4292 45002,45473,45947,46423,46903,47385,47871,48359,
4091 48850,49344,49841,50341,50844,51349,51858,52369, 4293 48850,49344,49841,50341,50844,51349,51858,52369,
4092 52884,53401,53921,54445,54971,55500,56032,56567, 4294 52884,53401,53921,54445,54971,55500,56032,56567,
4093 57105,57646,58190,58737,59287,59840,60396,60955, 4295 57105,57646,58190,58737,59287,59840,60396,60955,
4094 61517,62082,62650,63221,63795,64372,64952,65535 4296 61517,62082,62650,63221,63795,64372,64952,65535
4095 }; 4297 };
4096 4298 #endif /* SIMPLIFIED_READ */
4097 #endif /* simplified read only */
4098 4299
4099 /* The base/delta tables are required for both read and write (but currently 4300 /* The base/delta tables are required for both read and write (but currently
4100 * only the simplified versions.) 4301 * only the simplified versions.)
4101 */ 4302 */
4102 const png_uint_16 png_sRGB_base[512] = 4303 const png_uint_16 png_sRGB_base[512] =
4103 { 4304 {
4104 128,1782,3383,4644,5675,6564,7357,8074, 4305 128,1782,3383,4644,5675,6564,7357,8074,
4105 8732,9346,9921,10463,10977,11466,11935,12384, 4306 8732,9346,9921,10463,10977,11466,11935,12384,
4106 12816,13233,13634,14024,14402,14769,15125,15473, 4307 12816,13233,13634,14024,14402,14769,15125,15473,
4107 15812,16142,16466,16781,17090,17393,17690,17981, 4308 15812,16142,16466,16781,17090,17393,17690,17981,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
4215 png_control c; 4416 png_control c;
4216 4417
4217 /* Double check that we have a png_ptr - it should be impossible to get here 4418 /* Double check that we have a png_ptr - it should be impossible to get here
4218 * without one. 4419 * without one.
4219 */ 4420 */
4220 if (cp->png_ptr == NULL) 4421 if (cp->png_ptr == NULL)
4221 return 0; 4422 return 0;
4222 4423
4223 /* First free any data held in the control structure. */ 4424 /* First free any data held in the control structure. */
4224 # ifdef PNG_STDIO_SUPPORTED 4425 # ifdef PNG_STDIO_SUPPORTED
4225 if (cp->owned_file) 4426 if (cp->owned_file != 0)
4226 { 4427 {
4227 FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr); 4428 FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr);
4228 cp->owned_file = 0; 4429 cp->owned_file = 0;
4229 4430
4230 /* Ignore errors here. */ 4431 /* Ignore errors here. */
4231 if (fp != NULL) 4432 if (fp != NULL)
4232 { 4433 {
4233 cp->png_ptr->io_ptr = NULL; 4434 cp->png_ptr->io_ptr = NULL;
4234 (void)fclose(fp); 4435 (void)fclose(fp);
4235 } 4436 }
4236 } 4437 }
4237 # endif 4438 # endif
4238 4439
4239 /* Copy the control structure so that the original, allocated, version can be 4440 /* Copy the control structure so that the original, allocated, version can be
4240 * safely freed. Notice that a png_error here stops the remainder of the 4441 * safely freed. Notice that a png_error here stops the remainder of the
4241 * cleanup, but this is probably fine because that would indicate bad memory 4442 * cleanup, but this is probably fine because that would indicate bad memory
4242 * problems anyway. 4443 * problems anyway.
4243 */ 4444 */
4244 c = *cp; 4445 c = *cp;
4245 image->opaque = &c; 4446 image->opaque = &c;
4246 png_free(c.png_ptr, cp); 4447 png_free(c.png_ptr, cp);
4247 4448
4248 /* Then the structures, calling the correct API. */ 4449 /* Then the structures, calling the correct API. */
4249 if (c.for_write) 4450 if (c.for_write != 0)
4250 { 4451 {
4251 # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED 4452 # ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
4252 png_destroy_write_struct(&c.png_ptr, &c.info_ptr); 4453 png_destroy_write_struct(&c.png_ptr, &c.info_ptr);
4253 # else 4454 # else
4254 png_error(c.png_ptr, "simplified write not supported"); 4455 png_error(c.png_ptr, "simplified write not supported");
4255 # endif 4456 # endif
4256 } 4457 }
4257 else 4458 else
4258 { 4459 {
4259 # ifdef PNG_SIMPLIFIED_READ_SUPPORTED 4460 # ifdef PNG_SIMPLIFIED_READ_SUPPORTED
(...skipping 27 matching lines...) Expand all
4287 png_image_error(png_imagep image, png_const_charp error_message) 4488 png_image_error(png_imagep image, png_const_charp error_message)
4288 { 4489 {
4289 /* Utility to log an error. */ 4490 /* Utility to log an error. */
4290 png_safecat(image->message, (sizeof image->message), 0, error_message); 4491 png_safecat(image->message, (sizeof image->message), 0, error_message);
4291 image->warning_or_error |= PNG_IMAGE_ERROR; 4492 image->warning_or_error |= PNG_IMAGE_ERROR;
4292 png_image_free(image); 4493 png_image_free(image);
4293 return 0; 4494 return 0;
4294 } 4495 }
4295 4496
4296 #endif /* SIMPLIFIED READ/WRITE */ 4497 #endif /* SIMPLIFIED READ/WRITE */
4297 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */ 4498 #endif /* READ || WRITE */
OLDNEW
« no previous file with comments | « third_party/libpng/png.h ('k') | third_party/libpng/pngconf.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698