2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
5506
|
6 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
8 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
5506
|
20 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
21 |
* or visit www.oracle.com if you need additional information or have any
|
|
22 |
* questions.
|
2
|
23 |
*/
|
|
24 |
|
|
25 |
/* png.c - location for general purpose libpng functions
|
|
26 |
*
|
|
27 |
* This file is available under and governed by the GNU General Public
|
|
28 |
* License version 2 only, as published by the Free Software Foundation.
|
|
29 |
* However, the following notice accompanied the original version of this
|
|
30 |
* file and, per its terms, should not be removed:
|
|
31 |
*
|
29913
|
32 |
* Last changed in libpng 1.6.16 [December 22, 2014]
|
|
33 |
* Copyright (c) 1998-2014 Glenn Randers-Pehrson
|
2
|
34 |
* (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
|
|
35 |
* (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
|
10576
|
36 |
*
|
|
37 |
* This code is released under the libpng license.
|
|
38 |
* For conditions of distribution and use, see the disclaimer
|
|
39 |
* and license in png.h
|
2
|
40 |
*/
|
|
41 |
|
10576
|
42 |
#include "pngpriv.h"
|
2
|
43 |
|
|
44 |
/* Generate a compiler error if there is an old png.h in the search path. */
|
29913
|
45 |
typedef png_libpng_version_1_6_16 Your_png_h_is_not_version_1_6_16;
|
2
|
46 |
|
|
47 |
/* Tells libpng that we have already handled the first "num_bytes" bytes
|
|
48 |
* of the PNG file signature. If the PNG data is embedded into another
|
|
49 |
* stream we can set num_bytes = 8 so that libpng will not attempt to read
|
|
50 |
* or write any of the magic bytes before it starts on the IHDR.
|
|
51 |
*/
|
|
52 |
|
|
53 |
#ifdef PNG_READ_SUPPORTED
|
|
54 |
void PNGAPI
|
29913
|
55 |
png_set_sig_bytes(png_structrp png_ptr, int num_bytes)
|
2
|
56 |
{
|
10576
|
57 |
png_debug(1, "in png_set_sig_bytes");
|
|
58 |
|
|
59 |
if (png_ptr == NULL)
|
|
60 |
return;
|
|
61 |
|
2
|
62 |
if (num_bytes > 8)
|
10576
|
63 |
png_error(png_ptr, "Too many bytes for PNG signature");
|
2
|
64 |
|
|
65 |
png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
|
|
66 |
}
|
|
67 |
|
|
68 |
/* Checks whether the supplied bytes match the PNG signature. We allow
|
|
69 |
* checking less than the full 8-byte signature so that those apps that
|
|
70 |
* already read the first few bytes of a file to determine the file type
|
|
71 |
* can simply check the remaining bytes for extra assurance. Returns
|
|
72 |
* an integer less than, equal to, or greater than zero if sig is found,
|
|
73 |
* respectively, to be less than, to match, or be greater than the correct
|
29913
|
74 |
* PNG signature (this is the same behavior as strcmp, memcmp, etc).
|
2
|
75 |
*/
|
|
76 |
int PNGAPI
|
10576
|
77 |
png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
|
2
|
78 |
{
|
|
79 |
png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
|
10576
|
80 |
|
2
|
81 |
if (num_to_check > 8)
|
|
82 |
num_to_check = 8;
|
10576
|
83 |
|
2
|
84 |
else if (num_to_check < 1)
|
|
85 |
return (-1);
|
|
86 |
|
|
87 |
if (start > 7)
|
|
88 |
return (-1);
|
|
89 |
|
|
90 |
if (start + num_to_check > 8)
|
|
91 |
num_to_check = 8 - start;
|
|
92 |
|
29913
|
93 |
return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check)));
|
2
|
94 |
}
|
|
95 |
|
29913
|
96 |
#endif /* READ */
|
2
|
97 |
|
|
98 |
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
|
10576
|
99 |
/* Function to allocate memory for zlib */
|
|
100 |
PNG_FUNCTION(voidpf /* PRIVATE */,
|
|
101 |
png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED)
|
2
|
102 |
{
|
29913
|
103 |
png_alloc_size_t num_bytes = size;
|
2
|
104 |
|
10576
|
105 |
if (png_ptr == NULL)
|
29913
|
106 |
return NULL;
|
|
107 |
|
|
108 |
if (items >= (~(png_alloc_size_t)0)/size)
|
2
|
109 |
{
|
29913
|
110 |
png_warning (png_voidcast(png_structrp, png_ptr),
|
|
111 |
"Potential overflow in png_zalloc()");
|
|
112 |
return NULL;
|
2
|
113 |
}
|
29913
|
114 |
|
|
115 |
num_bytes *= items;
|
|
116 |
return png_malloc_warn(png_voidcast(png_structrp, png_ptr), num_bytes);
|
2
|
117 |
}
|
|
118 |
|
10576
|
119 |
/* Function to free memory for zlib */
|
|
120 |
void /* PRIVATE */
|
2
|
121 |
png_zfree(voidpf png_ptr, voidpf ptr)
|
|
122 |
{
|
29913
|
123 |
png_free(png_voidcast(png_const_structrp,png_ptr), ptr);
|
2
|
124 |
}
|
|
125 |
|
|
126 |
/* Reset the CRC variable to 32 bits of 1's. Care must be taken
|
|
127 |
* in case CRC is > 32 bits to leave the top bits 0.
|
|
128 |
*/
|
|
129 |
void /* PRIVATE */
|
29913
|
130 |
png_reset_crc(png_structrp png_ptr)
|
2
|
131 |
{
|
29913
|
132 |
/* The cast is safe because the crc is a 32 bit value. */
|
|
133 |
png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0);
|
2
|
134 |
}
|
|
135 |
|
|
136 |
/* Calculate the CRC over a section of data. We can only pass as
|
|
137 |
* much data to this routine as the largest single buffer size. We
|
|
138 |
* also check that this data will actually be used before going to the
|
|
139 |
* trouble of calculating it.
|
|
140 |
*/
|
|
141 |
void /* PRIVATE */
|
29913
|
142 |
png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
|
2
|
143 |
{
|
|
144 |
int need_crc = 1;
|
|
145 |
|
29913
|
146 |
if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
|
2
|
147 |
{
|
|
148 |
if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
|
|
149 |
(PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
|
|
150 |
need_crc = 0;
|
|
151 |
}
|
10576
|
152 |
|
29913
|
153 |
else /* critical */
|
2
|
154 |
{
|
29913
|
155 |
if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
|
2
|
156 |
need_crc = 0;
|
|
157 |
}
|
|
158 |
|
29913
|
159 |
/* 'uLong' is defined in zlib.h as unsigned long; this means that on some
|
|
160 |
* systems it is a 64 bit value. crc32, however, returns 32 bits so the
|
|
161 |
* following cast is safe. 'uInt' may be no more than 16 bits, so it is
|
|
162 |
* necessary to perform a loop here.
|
|
163 |
*/
|
|
164 |
if (need_crc != 0 && length > 0)
|
|
165 |
{
|
|
166 |
uLong crc = png_ptr->crc; /* Should never issue a warning */
|
|
167 |
|
|
168 |
do
|
|
169 |
{
|
|
170 |
uInt safe_length = (uInt)length;
|
|
171 |
if (safe_length == 0)
|
|
172 |
safe_length = (uInt)-1; /* evil, but safe */
|
|
173 |
|
|
174 |
crc = crc32(crc, ptr, safe_length);
|
|
175 |
|
|
176 |
/* The following should never issue compiler warnings; if they do the
|
|
177 |
* target system has characteristics that will probably violate other
|
|
178 |
* assumptions within the libpng code.
|
|
179 |
*/
|
|
180 |
ptr += safe_length;
|
|
181 |
length -= safe_length;
|
|
182 |
}
|
|
183 |
while (length > 0);
|
|
184 |
|
|
185 |
/* And the following is always safe because the crc is only 32 bits. */
|
|
186 |
png_ptr->crc = (png_uint_32)crc;
|
|
187 |
}
|
2
|
188 |
}
|
|
189 |
|
10576
|
190 |
/* Check a user supplied version number, called from both read and write
|
29913
|
191 |
* functions that create a png_struct.
|
10576
|
192 |
*/
|
|
193 |
int
|
29913
|
194 |
png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver)
|
10576
|
195 |
{
|
29913
|
196 |
/* Libpng versions 1.0.0 and later are binary compatible if the version
|
|
197 |
* string matches through the second '.'; we must recompile any
|
|
198 |
* applications that use any older library version.
|
|
199 |
*/
|
|
200 |
|
|
201 |
if (user_png_ver != NULL)
|
10576
|
202 |
{
|
29913
|
203 |
int i = -1;
|
|
204 |
int found_dots = 0;
|
10576
|
205 |
|
|
206 |
do
|
|
207 |
{
|
29913
|
208 |
i++;
|
|
209 |
if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i])
|
10576
|
210 |
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
|
29913
|
211 |
if (user_png_ver[i] == '.')
|
|
212 |
found_dots++;
|
|
213 |
} while (found_dots < 2 && user_png_ver[i] != 0 &&
|
|
214 |
PNG_LIBPNG_VER_STRING[i] != 0);
|
10576
|
215 |
}
|
|
216 |
|
|
217 |
else
|
|
218 |
png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
|
|
219 |
|
29913
|
220 |
if ((png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) != 0)
|
10576
|
221 |
{
|
|
222 |
#ifdef PNG_WARNINGS_SUPPORTED
|
29913
|
223 |
size_t pos = 0;
|
|
224 |
char m[128];
|
|
225 |
|
|
226 |
pos = png_safecat(m, (sizeof m), pos,
|
|
227 |
"Application built with libpng-");
|
|
228 |
pos = png_safecat(m, (sizeof m), pos, user_png_ver);
|
|
229 |
pos = png_safecat(m, (sizeof m), pos, " but running with ");
|
|
230 |
pos = png_safecat(m, (sizeof m), pos, PNG_LIBPNG_VER_STRING);
|
|
231 |
PNG_UNUSED(pos)
|
|
232 |
|
|
233 |
png_warning(png_ptr, m);
|
10576
|
234 |
#endif
|
|
235 |
|
|
236 |
#ifdef PNG_ERROR_NUMBERS_SUPPORTED
|
29913
|
237 |
png_ptr->flags = 0;
|
10576
|
238 |
#endif
|
|
239 |
|
29913
|
240 |
return 0;
|
10576
|
241 |
}
|
|
242 |
|
|
243 |
/* Success return. */
|
|
244 |
return 1;
|
|
245 |
}
|
|
246 |
|
29913
|
247 |
/* Generic function to create a png_struct for either read or write - this
|
|
248 |
* contains the common initialization.
|
2
|
249 |
*/
|
29913
|
250 |
PNG_FUNCTION(png_structp /* PRIVATE */,
|
|
251 |
png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
|
|
252 |
png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
|
|
253 |
png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
|
|
254 |
{
|
|
255 |
png_struct create_struct;
|
|
256 |
# ifdef PNG_SETJMP_SUPPORTED
|
|
257 |
jmp_buf create_jmp_buf;
|
|
258 |
# endif
|
|
259 |
|
|
260 |
/* This temporary stack-allocated structure is used to provide a place to
|
|
261 |
* build enough context to allow the user provided memory allocator (if any)
|
|
262 |
* to be called.
|
|
263 |
*/
|
|
264 |
memset(&create_struct, 0, (sizeof create_struct));
|
|
265 |
|
|
266 |
/* Added at libpng-1.2.6 */
|
|
267 |
# ifdef PNG_USER_LIMITS_SUPPORTED
|
|
268 |
create_struct.user_width_max = PNG_USER_WIDTH_MAX;
|
|
269 |
create_struct.user_height_max = PNG_USER_HEIGHT_MAX;
|
|
270 |
|
|
271 |
# ifdef PNG_USER_CHUNK_CACHE_MAX
|
|
272 |
/* Added at libpng-1.2.43 and 1.4.0 */
|
|
273 |
create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX;
|
|
274 |
# endif
|
|
275 |
|
|
276 |
# ifdef PNG_USER_CHUNK_MALLOC_MAX
|
|
277 |
/* Added at libpng-1.2.43 and 1.4.1, required only for read but exists
|
|
278 |
* in png_struct regardless.
|
|
279 |
*/
|
|
280 |
create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX;
|
|
281 |
# endif
|
|
282 |
# endif
|
|
283 |
|
|
284 |
/* The following two API calls simply set fields in png_struct, so it is safe
|
|
285 |
* to do them now even though error handling is not yet set up.
|
|
286 |
*/
|
|
287 |
# ifdef PNG_USER_MEM_SUPPORTED
|
|
288 |
png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn);
|
|
289 |
# else
|
|
290 |
PNG_UNUSED(mem_ptr)
|
|
291 |
PNG_UNUSED(malloc_fn)
|
|
292 |
PNG_UNUSED(free_fn)
|
|
293 |
# endif
|
|
294 |
|
|
295 |
/* (*error_fn) can return control to the caller after the error_ptr is set,
|
|
296 |
* this will result in a memory leak unless the error_fn does something
|
|
297 |
* extremely sophisticated. The design lacks merit but is implicit in the
|
|
298 |
* API.
|
|
299 |
*/
|
|
300 |
png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn);
|
|
301 |
|
|
302 |
# ifdef PNG_SETJMP_SUPPORTED
|
|
303 |
if (!setjmp(create_jmp_buf))
|
|
304 |
{
|
|
305 |
/* Temporarily fake out the longjmp information until we have
|
|
306 |
* successfully completed this function. This only works if we have
|
|
307 |
* setjmp() support compiled in, but it is safe - this stuff should
|
|
308 |
* never happen.
|
|
309 |
*/
|
|
310 |
create_struct.jmp_buf_ptr = &create_jmp_buf;
|
|
311 |
create_struct.jmp_buf_size = 0; /*stack allocation*/
|
|
312 |
create_struct.longjmp_fn = longjmp;
|
|
313 |
# else
|
|
314 |
{
|
|
315 |
# endif
|
|
316 |
/* Call the general version checker (shared with read and write code):
|
|
317 |
*/
|
|
318 |
if (png_user_version_check(&create_struct, user_png_ver) != 0)
|
|
319 |
{
|
|
320 |
png_structrp png_ptr = png_voidcast(png_structrp,
|
|
321 |
png_malloc_warn(&create_struct, (sizeof *png_ptr)));
|
|
322 |
|
|
323 |
if (png_ptr != NULL)
|
|
324 |
{
|
|
325 |
/* png_ptr->zstream holds a back-pointer to the png_struct, so
|
|
326 |
* this can only be done now:
|
|
327 |
*/
|
|
328 |
create_struct.zstream.zalloc = png_zalloc;
|
|
329 |
create_struct.zstream.zfree = png_zfree;
|
|
330 |
create_struct.zstream.opaque = png_ptr;
|
|
331 |
|
|
332 |
# ifdef PNG_SETJMP_SUPPORTED
|
|
333 |
/* Eliminate the local error handling: */
|
|
334 |
create_struct.jmp_buf_ptr = NULL;
|
|
335 |
create_struct.jmp_buf_size = 0;
|
|
336 |
create_struct.longjmp_fn = 0;
|
|
337 |
# endif
|
|
338 |
|
|
339 |
*png_ptr = create_struct;
|
|
340 |
|
|
341 |
/* This is the successful return point */
|
|
342 |
return png_ptr;
|
|
343 |
}
|
|
344 |
}
|
|
345 |
}
|
|
346 |
|
|
347 |
/* A longjmp because of a bug in the application storage allocator or a
|
|
348 |
* simple failure to allocate the png_struct.
|
|
349 |
*/
|
|
350 |
return NULL;
|
|
351 |
}
|
|
352 |
|
|
353 |
/* Allocate the memory for an info_struct for the application. */
|
10576
|
354 |
PNG_FUNCTION(png_infop,PNGAPI
|
29913
|
355 |
png_create_info_struct,(png_const_structrp png_ptr),PNG_ALLOCATED)
|
2
|
356 |
{
|
29913
|
357 |
png_inforp info_ptr;
|
2
|
358 |
|
10576
|
359 |
png_debug(1, "in png_create_info_struct");
|
|
360 |
|
|
361 |
if (png_ptr == NULL)
|
29913
|
362 |
return NULL;
|
|
363 |
|
|
364 |
/* Use the internal API that does not (or at least should not) error out, so
|
|
365 |
* that this call always returns ok. The application typically sets up the
|
|
366 |
* error handling *after* creating the info_struct because this is the way it
|
|
367 |
* has always been done in 'example.c'.
|
|
368 |
*/
|
|
369 |
info_ptr = png_voidcast(png_inforp, png_malloc_base(png_ptr,
|
|
370 |
(sizeof *info_ptr)));
|
|
371 |
|
2
|
372 |
if (info_ptr != NULL)
|
29913
|
373 |
memset(info_ptr, 0, (sizeof *info_ptr));
|
|
374 |
|
|
375 |
return info_ptr;
|
2
|
376 |
}
|
|
377 |
|
|
378 |
/* This function frees the memory associated with a single info struct.
|
|
379 |
* Normally, one would use either png_destroy_read_struct() or
|
|
380 |
* png_destroy_write_struct() to free an info struct, but this may be
|
29913
|
381 |
* useful for some applications. From libpng 1.6.0 this function is also used
|
|
382 |
* internally to implement the png_info release part of the 'struct' destroy
|
|
383 |
* APIs. This ensures that all possible approaches free the same data (all of
|
|
384 |
* it).
|
2
|
385 |
*/
|
|
386 |
void PNGAPI
|
29913
|
387 |
png_destroy_info_struct(png_const_structrp png_ptr, png_infopp info_ptr_ptr)
|
2
|
388 |
{
|
29913
|
389 |
png_inforp info_ptr = NULL;
|
10576
|
390 |
|
|
391 |
png_debug(1, "in png_destroy_info_struct");
|
2
|
392 |
|
10576
|
393 |
if (png_ptr == NULL)
|
|
394 |
return;
|
|
395 |
|
2
|
396 |
if (info_ptr_ptr != NULL)
|
|
397 |
info_ptr = *info_ptr_ptr;
|
|
398 |
|
|
399 |
if (info_ptr != NULL)
|
|
400 |
{
|
29913
|
401 |
/* Do this first in case of an error below; if the app implements its own
|
|
402 |
* memory management this can lead to png_free calling png_error, which
|
|
403 |
* will abort this routine and return control to the app error handler.
|
|
404 |
* An infinite loop may result if it then tries to free the same info
|
|
405 |
* ptr.
|
|
406 |
*/
|
2
|
407 |
*info_ptr_ptr = NULL;
|
29913
|
408 |
|
|
409 |
png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
|
|
410 |
memset(info_ptr, 0, (sizeof *info_ptr));
|
|
411 |
png_free(png_ptr, info_ptr);
|
2
|
412 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
/* Initialize the info structure. This is now an internal function (0.89)
|
|
416 |
* and applications using it are urged to use png_create_info_struct()
|
29913
|
417 |
* instead. Use deprecated in 1.6.0, internal use removed (used internally it
|
|
418 |
* is just a memset).
|
|
419 |
*
|
|
420 |
* NOTE: it is almost inconceivable that this API is used because it bypasses
|
|
421 |
* the user-memory mechanism and the user error handling/warning mechanisms in
|
|
422 |
* those cases where it does anything other than a memset.
|
2
|
423 |
*/
|
29913
|
424 |
PNG_FUNCTION(void,PNGAPI
|
|
425 |
png_info_init_3,(png_infopp ptr_ptr, png_size_t png_info_struct_size),
|
|
426 |
PNG_DEPRECATED)
|
2
|
427 |
{
|
29913
|
428 |
png_inforp info_ptr = *ptr_ptr;
|
2
|
429 |
|
10576
|
430 |
png_debug(1, "in png_info_init_3");
|
2
|
431 |
|
10576
|
432 |
if (info_ptr == NULL)
|
|
433 |
return;
|
2
|
434 |
|
29913
|
435 |
if ((sizeof (png_info)) > png_info_struct_size)
|
10576
|
436 |
{
|
29913
|
437 |
*ptr_ptr = NULL;
|
|
438 |
/* The following line is why this API should not be used: */
|
|
439 |
free(info_ptr);
|
|
440 |
info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL,
|
|
441 |
(sizeof *info_ptr)));
|
10576
|
442 |
*ptr_ptr = info_ptr;
|
|
443 |
}
|
2
|
444 |
|
10576
|
445 |
/* Set everything to 0 */
|
29913
|
446 |
memset(info_ptr, 0, (sizeof *info_ptr));
|
2
|
447 |
}
|
|
448 |
|
29913
|
449 |
/* The following API is not called internally */
|
2
|
450 |
void PNGAPI
|
29913
|
451 |
png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr,
|
2
|
452 |
int freer, png_uint_32 mask)
|
|
453 |
{
|
10576
|
454 |
png_debug(1, "in png_data_freer");
|
|
455 |
|
2
|
456 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
457 |
return;
|
10576
|
458 |
|
|
459 |
if (freer == PNG_DESTROY_WILL_FREE_DATA)
|
2
|
460 |
info_ptr->free_me |= mask;
|
10576
|
461 |
|
|
462 |
else if (freer == PNG_USER_WILL_FREE_DATA)
|
2
|
463 |
info_ptr->free_me &= ~mask;
|
10576
|
464 |
|
2
|
465 |
else
|
29913
|
466 |
png_error(png_ptr, "Unknown freer parameter in png_data_freer");
|
2
|
467 |
}
|
|
468 |
|
|
469 |
void PNGAPI
|
29913
|
470 |
png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask,
|
2
|
471 |
int num)
|
|
472 |
{
|
10576
|
473 |
png_debug(1, "in png_free_data");
|
|
474 |
|
2
|
475 |
if (png_ptr == NULL || info_ptr == NULL)
|
|
476 |
return;
|
|
477 |
|
10576
|
478 |
#ifdef PNG_TEXT_SUPPORTED
|
|
479 |
/* Free text item num or (if num == -1) all text items */
|
29913
|
480 |
if (info_ptr->text != 0 &&
|
|
481 |
((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0)
|
2
|
482 |
{
|
10576
|
483 |
if (num != -1)
|
|
484 |
{
|
29913
|
485 |
png_free(png_ptr, info_ptr->text[num].key);
|
|
486 |
info_ptr->text[num].key = NULL;
|
10576
|
487 |
}
|
|
488 |
|
|
489 |
else
|
|
490 |
{
|
|
491 |
int i;
|
29913
|
492 |
|
10576
|
493 |
for (i = 0; i < info_ptr->num_text; i++)
|
29913
|
494 |
png_free(png_ptr, info_ptr->text[i].key);
|
|
495 |
|
10576
|
496 |
png_free(png_ptr, info_ptr->text);
|
|
497 |
info_ptr->text = NULL;
|
29913
|
498 |
info_ptr->num_text = 0;
|
10576
|
499 |
}
|
2
|
500 |
}
|
|
501 |
#endif
|
|
502 |
|
10576
|
503 |
#ifdef PNG_tRNS_SUPPORTED
|
|
504 |
/* Free any tRNS entry */
|
29913
|
505 |
if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0)
|
10576
|
506 |
{
|
|
507 |
png_free(png_ptr, info_ptr->trans_alpha);
|
|
508 |
info_ptr->trans_alpha = NULL;
|
|
509 |
info_ptr->valid &= ~PNG_INFO_tRNS;
|
|
510 |
}
|
2
|
511 |
#endif
|
|
512 |
|
10576
|
513 |
#ifdef PNG_sCAL_SUPPORTED
|
|
514 |
/* Free any sCAL entry */
|
29913
|
515 |
if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0)
|
10576
|
516 |
{
|
|
517 |
png_free(png_ptr, info_ptr->scal_s_width);
|
|
518 |
png_free(png_ptr, info_ptr->scal_s_height);
|
|
519 |
info_ptr->scal_s_width = NULL;
|
|
520 |
info_ptr->scal_s_height = NULL;
|
|
521 |
info_ptr->valid &= ~PNG_INFO_sCAL;
|
|
522 |
}
|
2
|
523 |
#endif
|
|
524 |
|
10576
|
525 |
#ifdef PNG_pCAL_SUPPORTED
|
|
526 |
/* Free any pCAL entry */
|
29913
|
527 |
if (((mask & PNG_FREE_PCAL) & info_ptr->free_me) != 0)
|
10576
|
528 |
{
|
|
529 |
png_free(png_ptr, info_ptr->pcal_purpose);
|
|
530 |
png_free(png_ptr, info_ptr->pcal_units);
|
|
531 |
info_ptr->pcal_purpose = NULL;
|
|
532 |
info_ptr->pcal_units = NULL;
|
29913
|
533 |
|
10576
|
534 |
if (info_ptr->pcal_params != NULL)
|
|
535 |
{
|
|
536 |
int i;
|
29913
|
537 |
|
|
538 |
for (i = 0; i < info_ptr->pcal_nparams; i++)
|
10576
|
539 |
png_free(png_ptr, info_ptr->pcal_params[i]);
|
29913
|
540 |
|
10576
|
541 |
png_free(png_ptr, info_ptr->pcal_params);
|
|
542 |
info_ptr->pcal_params = NULL;
|
|
543 |
}
|
|
544 |
info_ptr->valid &= ~PNG_INFO_pCAL;
|
|
545 |
}
|
2
|
546 |
#endif
|
|
547 |
|
10576
|
548 |
#ifdef PNG_iCCP_SUPPORTED
|
29913
|
549 |
/* Free any profile entry */
|
|
550 |
if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0)
|
10576
|
551 |
{
|
|
552 |
png_free(png_ptr, info_ptr->iccp_name);
|
|
553 |
png_free(png_ptr, info_ptr->iccp_profile);
|
|
554 |
info_ptr->iccp_name = NULL;
|
|
555 |
info_ptr->iccp_profile = NULL;
|
|
556 |
info_ptr->valid &= ~PNG_INFO_iCCP;
|
|
557 |
}
|
2
|
558 |
#endif
|
|
559 |
|
10576
|
560 |
#ifdef PNG_sPLT_SUPPORTED
|
|
561 |
/* Free a given sPLT entry, or (if num == -1) all sPLT entries */
|
29913
|
562 |
if (info_ptr->splt_palettes != 0 &&
|
|
563 |
((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0)
|
2
|
564 |
{
|
10576
|
565 |
if (num != -1)
|
2
|
566 |
{
|
29913
|
567 |
png_free(png_ptr, info_ptr->splt_palettes[num].name);
|
|
568 |
png_free(png_ptr, info_ptr->splt_palettes[num].entries);
|
|
569 |
info_ptr->splt_palettes[num].name = NULL;
|
|
570 |
info_ptr->splt_palettes[num].entries = NULL;
|
10576
|
571 |
}
|
|
572 |
|
|
573 |
else
|
|
574 |
{
|
29913
|
575 |
if (info_ptr->splt_palettes_num != 0)
|
10576
|
576 |
{
|
|
577 |
int i;
|
29913
|
578 |
|
|
579 |
for (i = 0; i < info_ptr->splt_palettes_num; i++)
|
|
580 |
{
|
|
581 |
png_free(png_ptr, info_ptr->splt_palettes[i].name);
|
|
582 |
png_free(png_ptr, info_ptr->splt_palettes[i].entries);
|
|
583 |
}
|
10576
|
584 |
|
|
585 |
png_free(png_ptr, info_ptr->splt_palettes);
|
|
586 |
info_ptr->splt_palettes = NULL;
|
|
587 |
info_ptr->splt_palettes_num = 0;
|
|
588 |
}
|
|
589 |
info_ptr->valid &= ~PNG_INFO_sPLT;
|
2
|
590 |
}
|
|
591 |
}
|
|
592 |
#endif
|
|
593 |
|
29913
|
594 |
#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
|
|
595 |
if (info_ptr->unknown_chunks != 0 &&
|
|
596 |
((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0)
|
2
|
597 |
{
|
10576
|
598 |
if (num != -1)
|
|
599 |
{
|
29913
|
600 |
png_free(png_ptr, info_ptr->unknown_chunks[num].data);
|
|
601 |
info_ptr->unknown_chunks[num].data = NULL;
|
10576
|
602 |
}
|
2
|
603 |
|
10576
|
604 |
else
|
|
605 |
{
|
|
606 |
int i;
|
2
|
607 |
|
29913
|
608 |
if (info_ptr->unknown_chunks_num != 0)
|
10576
|
609 |
{
|
|
610 |
for (i = 0; i < info_ptr->unknown_chunks_num; i++)
|
29913
|
611 |
png_free(png_ptr, info_ptr->unknown_chunks[i].data);
|
10576
|
612 |
|
|
613 |
png_free(png_ptr, info_ptr->unknown_chunks);
|
|
614 |
info_ptr->unknown_chunks = NULL;
|
|
615 |
info_ptr->unknown_chunks_num = 0;
|
|
616 |
}
|
|
617 |
}
|
2
|
618 |
}
|
|
619 |
#endif
|
|
620 |
|
10576
|
621 |
#ifdef PNG_hIST_SUPPORTED
|
|
622 |
/* Free any hIST entry */
|
29913
|
623 |
if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0)
|
10576
|
624 |
{
|
|
625 |
png_free(png_ptr, info_ptr->hist);
|
|
626 |
info_ptr->hist = NULL;
|
|
627 |
info_ptr->valid &= ~PNG_INFO_hIST;
|
|
628 |
}
|
2
|
629 |
#endif
|
|
630 |
|
10576
|
631 |
/* Free any PLTE entry that was internally allocated */
|
29913
|
632 |
if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0)
|
10576
|
633 |
{
|
29913
|
634 |
png_free(png_ptr, info_ptr->palette);
|
10576
|
635 |
info_ptr->palette = NULL;
|
|
636 |
info_ptr->valid &= ~PNG_INFO_PLTE;
|
|
637 |
info_ptr->num_palette = 0;
|
|
638 |
}
|
2
|
639 |
|
10576
|
640 |
#ifdef PNG_INFO_IMAGE_SUPPORTED
|
|
641 |
/* Free any image bits attached to the info structure */
|
29913
|
642 |
if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0)
|
10576
|
643 |
{
|
29913
|
644 |
if (info_ptr->row_pointers != 0)
|
10576
|
645 |
{
|
29913
|
646 |
png_uint_32 row;
|
|
647 |
for (row = 0; row < info_ptr->height; row++)
|
10576
|
648 |
png_free(png_ptr, info_ptr->row_pointers[row]);
|
29913
|
649 |
|
10576
|
650 |
png_free(png_ptr, info_ptr->row_pointers);
|
|
651 |
info_ptr->row_pointers = NULL;
|
|
652 |
}
|
|
653 |
info_ptr->valid &= ~PNG_INFO_IDAT;
|
|
654 |
}
|
2
|
655 |
#endif
|
|
656 |
|
10576
|
657 |
if (num != -1)
|
|
658 |
mask &= ~PNG_FREE_MUL;
|
|
659 |
|
|
660 |
info_ptr->free_me &= ~mask;
|
2
|
661 |
}
|
29913
|
662 |
#endif /* READ || WRITE */
|
2
|
663 |
|
|
664 |
/* This function returns a pointer to the io_ptr associated with the user
|
|
665 |
* functions. The application should free any memory associated with this
|
|
666 |
* pointer before png_write_destroy() or png_read_destroy() are called.
|
|
667 |
*/
|
|
668 |
png_voidp PNGAPI
|
29913
|
669 |
png_get_io_ptr(png_const_structrp png_ptr)
|
2
|
670 |
{
|
10576
|
671 |
if (png_ptr == NULL)
|
|
672 |
return (NULL);
|
|
673 |
|
2
|
674 |
return (png_ptr->io_ptr);
|
|
675 |
}
|
|
676 |
|
|
677 |
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
|
10576
|
678 |
# ifdef PNG_STDIO_SUPPORTED
|
2
|
679 |
/* Initialize the default input/output functions for the PNG file. If you
|
|
680 |
* use your own read or write routines, you can call either png_set_read_fn()
|
|
681 |
* or png_set_write_fn() instead of png_init_io(). If you have defined
|
29913
|
682 |
* PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a
|
|
683 |
* function of your own because "FILE *" isn't necessarily available.
|
2
|
684 |
*/
|
|
685 |
void PNGAPI
|
29913
|
686 |
png_init_io(png_structrp png_ptr, png_FILE_p fp)
|
2
|
687 |
{
|
10576
|
688 |
png_debug(1, "in png_init_io");
|
|
689 |
|
|
690 |
if (png_ptr == NULL)
|
|
691 |
return;
|
|
692 |
|
2
|
693 |
png_ptr->io_ptr = (png_voidp)fp;
|
|
694 |
}
|
10576
|
695 |
# endif
|
2
|
696 |
|
29913
|
697 |
#ifdef PNG_SAVE_INT_32_SUPPORTED
|
|
698 |
/* The png_save_int_32 function assumes integers are stored in two's
|
|
699 |
* complement format. If this isn't the case, then this routine needs to
|
|
700 |
* be modified to write data in two's complement format. Note that,
|
|
701 |
* the following works correctly even if png_int_32 has more than 32 bits
|
|
702 |
* (compare the more complex code required on read for sign extension.)
|
|
703 |
*/
|
|
704 |
void PNGAPI
|
|
705 |
png_save_int_32(png_bytep buf, png_int_32 i)
|
|
706 |
{
|
|
707 |
buf[0] = (png_byte)((i >> 24) & 0xff);
|
|
708 |
buf[1] = (png_byte)((i >> 16) & 0xff);
|
|
709 |
buf[2] = (png_byte)((i >> 8) & 0xff);
|
|
710 |
buf[3] = (png_byte)(i & 0xff);
|
|
711 |
}
|
|
712 |
#endif
|
|
713 |
|
10576
|
714 |
# ifdef PNG_TIME_RFC1123_SUPPORTED
|
2
|
715 |
/* Convert the supplied time into an RFC 1123 string suitable for use in
|
|
716 |
* a "Creation Time" or other text-based time string.
|
|
717 |
*/
|
29913
|
718 |
int PNGAPI
|
|
719 |
png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
|
2
|
720 |
{
|
|
721 |
static PNG_CONST char short_months[12][4] =
|
|
722 |
{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
|
723 |
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
724 |
|
29913
|
725 |
if (out == NULL)
|
|
726 |
return 0;
|
|
727 |
|
|
728 |
if (ptime->year > 9999 /* RFC1123 limitation */ ||
|
|
729 |
ptime->month == 0 || ptime->month > 12 ||
|
|
730 |
ptime->day == 0 || ptime->day > 31 ||
|
|
731 |
ptime->hour > 23 || ptime->minute > 59 ||
|
|
732 |
ptime->second > 60)
|
|
733 |
return 0;
|
10576
|
734 |
|
2
|
735 |
{
|
10576
|
736 |
size_t pos = 0;
|
29913
|
737 |
char number_buf[5]; /* enough for a four-digit year */
|
|
738 |
|
|
739 |
# define APPEND_STRING(string) pos = png_safecat(out, 29, pos, (string))
|
10576
|
740 |
# define APPEND_NUMBER(format, value)\
|
|
741 |
APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
|
29913
|
742 |
# define APPEND(ch) if (pos < 28) out[pos++] = (ch)
|
|
743 |
|
|
744 |
APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day);
|
10576
|
745 |
APPEND(' ');
|
29913
|
746 |
APPEND_STRING(short_months[(ptime->month - 1)]);
|
10576
|
747 |
APPEND(' ');
|
|
748 |
APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year);
|
|
749 |
APPEND(' ');
|
29913
|
750 |
APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour);
|
10576
|
751 |
APPEND(':');
|
29913
|
752 |
APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute);
|
10576
|
753 |
APPEND(':');
|
29913
|
754 |
APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second);
|
10576
|
755 |
APPEND_STRING(" +0000"); /* This reliably terminates the buffer */
|
|
756 |
|
|
757 |
# undef APPEND
|
|
758 |
# undef APPEND_NUMBER
|
|
759 |
# undef APPEND_STRING
|
2
|
760 |
}
|
|
761 |
|
29913
|
762 |
return 1;
|
2
|
763 |
}
|
29913
|
764 |
|
|
765 |
# if PNG_LIBPNG_VER < 10700
|
|
766 |
/* To do: remove the following from libpng-1.7 */
|
|
767 |
/* Original API that uses a private buffer in png_struct.
|
|
768 |
* Deprecated because it causes png_struct to carry a spurious temporary
|
|
769 |
* buffer (png_struct::time_buffer), better to have the caller pass this in.
|
|
770 |
*/
|
|
771 |
png_const_charp PNGAPI
|
|
772 |
png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime)
|
|
773 |
{
|
|
774 |
if (png_ptr != NULL)
|
|
775 |
{
|
|
776 |
/* The only failure above if png_ptr != NULL is from an invalid ptime */
|
|
777 |
if (png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime) == 0)
|
|
778 |
png_warning(png_ptr, "Ignoring invalid time value");
|
|
779 |
|
|
780 |
else
|
|
781 |
return png_ptr->time_buffer;
|
|
782 |
}
|
|
783 |
|
|
784 |
return NULL;
|
|
785 |
}
|
|
786 |
# endif
|
|
787 |
# endif /* TIME_RFC1123 */
|
|
788 |
|
|
789 |
#endif /* READ || WRITE */
|
2
|
790 |
|
10576
|
791 |
png_const_charp PNGAPI
|
29913
|
792 |
png_get_copyright(png_const_structrp png_ptr)
|
2
|
793 |
{
|
10576
|
794 |
PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
|
|
795 |
#ifdef PNG_STRING_COPYRIGHT
|
|
796 |
return PNG_STRING_COPYRIGHT
|
|
797 |
#else
|
|
798 |
# ifdef __STDC__
|
|
799 |
return PNG_STRING_NEWLINE \
|
29913
|
800 |
"libpng version 1.6.16 - December 22, 2014" PNG_STRING_NEWLINE \
|
|
801 |
"Copyright (c) 1998-2014 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
|
10576
|
802 |
"Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
|
|
803 |
"Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
|
|
804 |
PNG_STRING_NEWLINE;
|
|
805 |
# else
|
29913
|
806 |
return "libpng version 1.6.16 - December 22, 2014\
|
|
807 |
Copyright (c) 1998-2014 Glenn Randers-Pehrson\
|
10576
|
808 |
Copyright (c) 1996-1997 Andreas Dilger\
|
|
809 |
Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
|
|
810 |
# endif
|
|
811 |
#endif
|
2
|
812 |
}
|
|
813 |
|
|
814 |
/* The following return the library version as a short string in the
|
|
815 |
* format 1.0.0 through 99.99.99zz. To get the version of *.h files
|
|
816 |
* used with your application, print out PNG_LIBPNG_VER_STRING, which
|
|
817 |
* is defined in png.h.
|
|
818 |
* Note: now there is no difference between png_get_libpng_ver() and
|
|
819 |
* png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
|
|
820 |
* it is guaranteed that png.c uses the correct version of png.h.
|
|
821 |
*/
|
10576
|
822 |
png_const_charp PNGAPI
|
29913
|
823 |
png_get_libpng_ver(png_const_structrp png_ptr)
|
2
|
824 |
{
|
|
825 |
/* Version of *.c files used when building libpng */
|
10576
|
826 |
return png_get_header_ver(png_ptr);
|
2
|
827 |
}
|
|
828 |
|
10576
|
829 |
png_const_charp PNGAPI
|
29913
|
830 |
png_get_header_ver(png_const_structrp png_ptr)
|
2
|
831 |
{
|
|
832 |
/* Version of *.h files used when building libpng */
|
10576
|
833 |
PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
|
|
834 |
return PNG_LIBPNG_VER_STRING;
|
2
|
835 |
}
|
|
836 |
|
10576
|
837 |
png_const_charp PNGAPI
|
29913
|
838 |
png_get_header_version(png_const_structrp png_ptr)
|
2
|
839 |
{
|
|
840 |
/* Returns longer string containing both version and date */
|
10576
|
841 |
PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */
|
|
842 |
#ifdef __STDC__
|
|
843 |
return PNG_HEADER_VERSION_STRING
|
|
844 |
# ifndef PNG_READ_SUPPORTED
|
|
845 |
" (NO READ SUPPORT)"
|
|
846 |
# endif
|
|
847 |
PNG_STRING_NEWLINE;
|
|
848 |
#else
|
|
849 |
return PNG_HEADER_VERSION_STRING;
|
|
850 |
#endif
|
2
|
851 |
}
|
|
852 |
|
29913
|
853 |
#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
|
|
854 |
/* NOTE: this routine is not used internally! */
|
|
855 |
/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
|
|
856 |
* large of png_color. This lets grayscale images be treated as
|
|
857 |
* paletted. Most useful for gamma correction and simplification
|
|
858 |
* of code. This API is not used internally.
|
|
859 |
*/
|
|
860 |
void PNGAPI
|
|
861 |
png_build_grayscale_palette(int bit_depth, png_colorp palette)
|
|
862 |
{
|
|
863 |
int num_palette;
|
|
864 |
int color_inc;
|
|
865 |
int i;
|
|
866 |
int v;
|
|
867 |
|
|
868 |
png_debug(1, "in png_do_build_grayscale_palette");
|
|
869 |
|
|
870 |
if (palette == NULL)
|
|
871 |
return;
|
|
872 |
|
|
873 |
switch (bit_depth)
|
|
874 |
{
|
|
875 |
case 1:
|
|
876 |
num_palette = 2;
|
|
877 |
color_inc = 0xff;
|
|
878 |
break;
|
|
879 |
|
|
880 |
case 2:
|
|
881 |
num_palette = 4;
|
|
882 |
color_inc = 0x55;
|
|
883 |
break;
|
|
884 |
|
|
885 |
case 4:
|
|
886 |
num_palette = 16;
|
|
887 |
color_inc = 0x11;
|
|
888 |
break;
|
|
889 |
|
|
890 |
case 8:
|
|
891 |
num_palette = 256;
|
|
892 |
color_inc = 1;
|
|
893 |
break;
|
|
894 |
|
|
895 |
default:
|
|
896 |
num_palette = 0;
|
|
897 |
color_inc = 0;
|
|
898 |
break;
|
|
899 |
}
|
|
900 |
|
|
901 |
for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
|
|
902 |
{
|
|
903 |
palette[i].red = (png_byte)v;
|
|
904 |
palette[i].green = (png_byte)v;
|
|
905 |
palette[i].blue = (png_byte)v;
|
|
906 |
}
|
|
907 |
}
|
|
908 |
#endif
|
|
909 |
|
|
910 |
#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
|
2
|
911 |
int PNGAPI
|
29913
|
912 |
png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name)
|
2
|
913 |
{
|
10576
|
914 |
/* Check chunk_name and return "keep" value if it's on the list, else 0 */
|
29913
|
915 |
png_const_bytep p, p_end;
|
|
916 |
|
|
917 |
if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0)
|
|
918 |
return PNG_HANDLE_CHUNK_AS_DEFAULT;
|
|
919 |
|
|
920 |
p_end = png_ptr->chunk_list;
|
|
921 |
p = p_end + png_ptr->num_chunk_list*5; /* beyond end */
|
|
922 |
|
|
923 |
/* The code is the fifth byte after each four byte string. Historically this
|
|
924 |
* code was always searched from the end of the list, this is no longer
|
|
925 |
* necessary because the 'set' routine handles duplicate entries correcty.
|
|
926 |
*/
|
|
927 |
do /* num_chunk_list > 0, so at least one */
|
|
928 |
{
|
|
929 |
p -= 5;
|
|
930 |
|
|
931 |
if (memcmp(chunk_name, p, 4) == 0)
|
|
932 |
return p[4];
|
|
933 |
}
|
|
934 |
while (p > p_end);
|
|
935 |
|
|
936 |
/* This means that known chunks should be processed and unknown chunks should
|
|
937 |
* be handled according to the value of png_ptr->unknown_default; this can be
|
|
938 |
* confusing because, as a result, there are two levels of defaulting for
|
|
939 |
* unknown chunks.
|
|
940 |
*/
|
|
941 |
return PNG_HANDLE_CHUNK_AS_DEFAULT;
|
2
|
942 |
}
|
29913
|
943 |
|
|
944 |
#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\
|
|
945 |
defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
|
|
946 |
int /* PRIVATE */
|
|
947 |
png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name)
|
|
948 |
{
|
|
949 |
png_byte chunk_string[5];
|
|
950 |
|
|
951 |
PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name);
|
|
952 |
return png_handle_as_unknown(png_ptr, chunk_string);
|
|
953 |
}
|
|
954 |
#endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */
|
|
955 |
#endif /* SET_UNKNOWN_CHUNKS */
|
2
|
956 |
|
10576
|
957 |
#ifdef PNG_READ_SUPPORTED
|
2
|
958 |
/* This function, added to libpng-1.0.6g, is untested. */
|
|
959 |
int PNGAPI
|
29913
|
960 |
png_reset_zstream(png_structrp png_ptr)
|
2
|
961 |
{
|
10576
|
962 |
if (png_ptr == NULL)
|
|
963 |
return Z_STREAM_ERROR;
|
|
964 |
|
29913
|
965 |
/* WARNING: this resets the window bits to the maximum! */
|
2
|
966 |
return (inflateReset(&png_ptr->zstream));
|
|
967 |
}
|
29913
|
968 |
#endif /* READ */
|
2
|
969 |
|
|
970 |
/* This function was added to libpng-1.0.7 */
|
|
971 |
png_uint_32 PNGAPI
|
|
972 |
png_access_version_number(void)
|
|
973 |
{
|
|
974 |
/* Version of *.c files used when building libpng */
|
10576
|
975 |
return((png_uint_32)PNG_LIBPNG_VER);
|
2
|
976 |
}
|
|
977 |
|
|
978 |
|
|
979 |
|
|
980 |
#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
|
29913
|
981 |
/* Ensure that png_ptr->zstream.msg holds some appropriate error message string.
|
|
982 |
* If it doesn't 'ret' is used to set it to something appropriate, even in cases
|
|
983 |
* like Z_OK or Z_STREAM_END where the error code is apparently a success code.
|
|
984 |
*/
|
|
985 |
void /* PRIVATE */
|
|
986 |
png_zstream_error(png_structrp png_ptr, int ret)
|
2
|
987 |
{
|
29913
|
988 |
/* Translate 'ret' into an appropriate error string, priority is given to the
|
|
989 |
* one in zstream if set. This always returns a string, even in cases like
|
|
990 |
* Z_OK or Z_STREAM_END where the error code is a success code.
|
|
991 |
*/
|
|
992 |
if (png_ptr->zstream.msg == NULL) switch (ret)
|
|
993 |
{
|
|
994 |
default:
|
|
995 |
case Z_OK:
|
|
996 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return code");
|
|
997 |
break;
|
|
998 |
|
|
999 |
case Z_STREAM_END:
|
|
1000 |
/* Normal exit */
|
|
1001 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected end of LZ stream");
|
|
1002 |
break;
|
|
1003 |
|
|
1004 |
case Z_NEED_DICT:
|
|
1005 |
/* This means the deflate stream did not have a dictionary; this
|
|
1006 |
* indicates a bogus PNG.
|
|
1007 |
*/
|
|
1008 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("missing LZ dictionary");
|
|
1009 |
break;
|
|
1010 |
|
|
1011 |
case Z_ERRNO:
|
|
1012 |
/* gz APIs only: should not happen */
|
|
1013 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("zlib IO error");
|
|
1014 |
break;
|
|
1015 |
|
|
1016 |
case Z_STREAM_ERROR:
|
|
1017 |
/* internal libpng error */
|
|
1018 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("bad parameters to zlib");
|
|
1019 |
break;
|
|
1020 |
|
|
1021 |
case Z_DATA_ERROR:
|
|
1022 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("damaged LZ stream");
|
|
1023 |
break;
|
|
1024 |
|
|
1025 |
case Z_MEM_ERROR:
|
|
1026 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("insufficient memory");
|
|
1027 |
break;
|
|
1028 |
|
|
1029 |
case Z_BUF_ERROR:
|
|
1030 |
/* End of input or output; not a problem if the caller is doing
|
|
1031 |
* incremental read or write.
|
|
1032 |
*/
|
|
1033 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("truncated");
|
|
1034 |
break;
|
|
1035 |
|
|
1036 |
case Z_VERSION_ERROR:
|
|
1037 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("unsupported zlib version");
|
|
1038 |
break;
|
|
1039 |
|
|
1040 |
case PNG_UNEXPECTED_ZLIB_RETURN:
|
|
1041 |
/* Compile errors here mean that zlib now uses the value co-opted in
|
|
1042 |
* pngpriv.h for PNG_UNEXPECTED_ZLIB_RETURN; update the switch above
|
|
1043 |
* and change pngpriv.h. Note that this message is "... return",
|
|
1044 |
* whereas the default/Z_OK one is "... return code".
|
|
1045 |
*/
|
|
1046 |
png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return");
|
|
1047 |
break;
|
|
1048 |
}
|
10576
|
1049 |
}
|
29913
|
1050 |
|
|
1051 |
/* png_convert_size: a PNGAPI but no longer in png.h, so deleted
|
|
1052 |
* at libpng 1.5.5!
|
|
1053 |
*/
|
10576
|
1054 |
|
|
1055 |
/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
|
29913
|
1056 |
#ifdef PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */
|
|
1057 |
static int
|
|
1058 |
png_colorspace_check_gamma(png_const_structrp png_ptr,
|
|
1059 |
png_colorspacerp colorspace, png_fixed_point gAMA, int from)
|
|
1060 |
/* This is called to check a new gamma value against an existing one. The
|
|
1061 |
* routine returns false if the new gamma value should not be written.
|
|
1062 |
*
|
|
1063 |
* 'from' says where the new gamma value comes from:
|
|
1064 |
*
|
|
1065 |
* 0: the new gamma value is the libpng estimate for an ICC profile
|
|
1066 |
* 1: the new gamma value comes from a gAMA chunk
|
|
1067 |
* 2: the new gamma value comes from an sRGB chunk
|
|
1068 |
*/
|
|
1069 |
{
|
|
1070 |
png_fixed_point gtest;
|
|
1071 |
|
|
1072 |
if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 &&
|
|
1073 |
(png_muldiv(>est, colorspace->gamma, PNG_FP_1, gAMA) == 0 ||
|
|
1074 |
png_gamma_significant(gtest) != 0))
|
|
1075 |
{
|
|
1076 |
/* Either this is an sRGB image, in which case the calculated gamma
|
|
1077 |
* approximation should match, or this is an image with a profile and the
|
|
1078 |
* value libpng calculates for the gamma of the profile does not match the
|
|
1079 |
* value recorded in the file. The former, sRGB, case is an error, the
|
|
1080 |
* latter is just a warning.
|
|
1081 |
*/
|
|
1082 |
if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2)
|
|
1083 |
{
|
|
1084 |
png_chunk_report(png_ptr, "gamma value does not match sRGB",
|
|
1085 |
PNG_CHUNK_ERROR);
|
|
1086 |
/* Do not overwrite an sRGB value */
|
|
1087 |
return from == 2;
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
else /* sRGB tag not involved */
|
|
1091 |
{
|
|
1092 |
png_chunk_report(png_ptr, "gamma value does not match libpng estimate",
|
|
1093 |
PNG_CHUNK_WARNING);
|
|
1094 |
return from == 1;
|
|
1095 |
}
|
|
1096 |
}
|
|
1097 |
|
|
1098 |
return 1;
|
|
1099 |
}
|
|
1100 |
|
|
1101 |
void /* PRIVATE */
|
|
1102 |
png_colorspace_set_gamma(png_const_structrp png_ptr,
|
|
1103 |
png_colorspacerp colorspace, png_fixed_point gAMA)
|
|
1104 |
{
|
|
1105 |
/* Changed in libpng-1.5.4 to limit the values to ensure overflow can't
|
|
1106 |
* occur. Since the fixed point representation is asymetrical it is
|
|
1107 |
* possible for 1/gamma to overflow the limit of 21474 and this means the
|
|
1108 |
* gamma value must be at least 5/100000 and hence at most 20000.0. For
|
|
1109 |
* safety the limits here are a little narrower. The values are 0.00016 to
|
|
1110 |
* 6250.0, which are truly ridiculous gamma values (and will produce
|
|
1111 |
* displays that are all black or all white.)
|
|
1112 |
*
|
|
1113 |
* In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk
|
|
1114 |
* handling code, which only required the value to be >0.
|
|
1115 |
*/
|
|
1116 |
png_const_charp errmsg;
|
|
1117 |
|
|
1118 |
if (gAMA < 16 || gAMA > 625000000)
|
|
1119 |
errmsg = "gamma value out of range";
|
|
1120 |
|
|
1121 |
# ifdef PNG_READ_gAMA_SUPPORTED
|
|
1122 |
/* Allow the application to set the gamma value more than once */
|
|
1123 |
else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 &&
|
|
1124 |
(colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0)
|
|
1125 |
errmsg = "duplicate";
|
|
1126 |
# endif
|
|
1127 |
|
|
1128 |
/* Do nothing if the colorspace is already invalid */
|
|
1129 |
else if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
|
|
1130 |
return;
|
|
1131 |
|
|
1132 |
else
|
|
1133 |
{
|
|
1134 |
if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA,
|
|
1135 |
1/*from gAMA*/) != 0)
|
|
1136 |
{
|
|
1137 |
/* Store this gamma value. */
|
|
1138 |
colorspace->gamma = gAMA;
|
|
1139 |
colorspace->flags |=
|
|
1140 |
(PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA);
|
|
1141 |
}
|
|
1142 |
|
|
1143 |
/* At present if the check_gamma test fails the gamma of the colorspace is
|
|
1144 |
* not updated however the colorspace is not invalidated. This
|
|
1145 |
* corresponds to the case where the existing gamma comes from an sRGB
|
|
1146 |
* chunk or profile. An error message has already been output.
|
|
1147 |
*/
|
|
1148 |
return;
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
/* Error exit - errmsg has been set. */
|
|
1152 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1153 |
png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR);
|
|
1154 |
}
|
|
1155 |
|
|
1156 |
void /* PRIVATE */
|
|
1157 |
png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr)
|
|
1158 |
{
|
|
1159 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
|
|
1160 |
{
|
|
1161 |
/* Everything is invalid */
|
|
1162 |
info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB|
|
|
1163 |
PNG_INFO_iCCP);
|
|
1164 |
|
|
1165 |
# ifdef PNG_COLORSPACE_SUPPORTED
|
|
1166 |
/* Clean up the iCCP profile now if it won't be used. */
|
|
1167 |
png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/);
|
|
1168 |
# else
|
|
1169 |
PNG_UNUSED(png_ptr)
|
|
1170 |
# endif
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
else
|
|
1174 |
{
|
|
1175 |
# ifdef PNG_COLORSPACE_SUPPORTED
|
|
1176 |
/* Leave the INFO_iCCP flag set if the pngset.c code has already set
|
|
1177 |
* it; this allows a PNG to contain a profile which matches sRGB and
|
|
1178 |
* yet still have that profile retrievable by the application.
|
|
1179 |
*/
|
|
1180 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB) != 0)
|
|
1181 |
info_ptr->valid |= PNG_INFO_sRGB;
|
|
1182 |
|
|
1183 |
else
|
|
1184 |
info_ptr->valid &= ~PNG_INFO_sRGB;
|
|
1185 |
|
|
1186 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
|
|
1187 |
info_ptr->valid |= PNG_INFO_cHRM;
|
|
1188 |
|
|
1189 |
else
|
|
1190 |
info_ptr->valid &= ~PNG_INFO_cHRM;
|
|
1191 |
# endif
|
|
1192 |
|
|
1193 |
if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0)
|
|
1194 |
info_ptr->valid |= PNG_INFO_gAMA;
|
|
1195 |
|
|
1196 |
else
|
|
1197 |
info_ptr->valid &= ~PNG_INFO_gAMA;
|
|
1198 |
}
|
|
1199 |
}
|
|
1200 |
|
|
1201 |
#ifdef PNG_READ_SUPPORTED
|
|
1202 |
void /* PRIVATE */
|
|
1203 |
png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
|
|
1204 |
{
|
|
1205 |
if (info_ptr == NULL) /* reduce code size; check here not in the caller */
|
|
1206 |
return;
|
|
1207 |
|
|
1208 |
info_ptr->colorspace = png_ptr->colorspace;
|
|
1209 |
png_colorspace_sync_info(png_ptr, info_ptr);
|
|
1210 |
}
|
|
1211 |
#endif
|
|
1212 |
#endif
|
|
1213 |
|
|
1214 |
#ifdef PNG_COLORSPACE_SUPPORTED
|
|
1215 |
/* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for
|
|
1216 |
* cHRM, as opposed to using chromaticities. These internal APIs return
|
|
1217 |
* non-zero on a parameter error. The X, Y and Z values are required to be
|
|
1218 |
* positive and less than 1.0.
|
|
1219 |
*/
|
|
1220 |
static int
|
|
1221 |
png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ)
|
|
1222 |
{
|
|
1223 |
png_int_32 d, dwhite, whiteX, whiteY;
|
|
1224 |
|
|
1225 |
d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z;
|
|
1226 |
if (png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d) == 0)
|
|
1227 |
return 1;
|
|
1228 |
if (png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d) == 0)
|
|
1229 |
return 1;
|
|
1230 |
dwhite = d;
|
|
1231 |
whiteX = XYZ->red_X;
|
|
1232 |
whiteY = XYZ->red_Y;
|
|
1233 |
|
|
1234 |
d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z;
|
|
1235 |
if (png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d) == 0)
|
|
1236 |
return 1;
|
|
1237 |
if (png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d) == 0)
|
|
1238 |
return 1;
|
|
1239 |
dwhite += d;
|
|
1240 |
whiteX += XYZ->green_X;
|
|
1241 |
whiteY += XYZ->green_Y;
|
|
1242 |
|
|
1243 |
d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z;
|
|
1244 |
if (png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d) == 0)
|
|
1245 |
return 1;
|
|
1246 |
if (png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d) == 0)
|
|
1247 |
return 1;
|
|
1248 |
dwhite += d;
|
|
1249 |
whiteX += XYZ->blue_X;
|
|
1250 |
whiteY += XYZ->blue_Y;
|
|
1251 |
|
|
1252 |
/* The reference white is simply the sum of the end-point (X,Y,Z) vectors,
|
|
1253 |
* thus:
|
|
1254 |
*/
|
|
1255 |
if (png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite) == 0)
|
|
1256 |
return 1;
|
|
1257 |
if (png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite) == 0)
|
|
1258 |
return 1;
|
|
1259 |
|
|
1260 |
return 0;
|
|
1261 |
}
|
|
1262 |
|
|
1263 |
static int
|
|
1264 |
png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy)
|
|
1265 |
{
|
|
1266 |
png_fixed_point red_inverse, green_inverse, blue_scale;
|
|
1267 |
png_fixed_point left, right, denominator;
|
|
1268 |
|
|
1269 |
/* Check xy and, implicitly, z. Note that wide gamut color spaces typically
|
|
1270 |
* have end points with 0 tristimulus values (these are impossible end
|
|
1271 |
* points, but they are used to cover the possible colors.)
|
|
1272 |
*/
|
|
1273 |
if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1;
|
|
1274 |
if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1;
|
|
1275 |
if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1;
|
|
1276 |
if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1;
|
|
1277 |
if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1;
|
|
1278 |
if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1;
|
|
1279 |
if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1;
|
|
1280 |
if (xy->whitey < 0 || xy->whitey > PNG_FP_1-xy->whitex) return 1;
|
|
1281 |
|
|
1282 |
/* The reverse calculation is more difficult because the original tristimulus
|
|
1283 |
* value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8
|
|
1284 |
* derived values were recorded in the cHRM chunk;
|
|
1285 |
* (red,green,blue,white)x(x,y). This loses one degree of freedom and
|
|
1286 |
* therefore an arbitrary ninth value has to be introduced to undo the
|
|
1287 |
* original transformations.
|
|
1288 |
*
|
|
1289 |
* Think of the original end-points as points in (X,Y,Z) space. The
|
|
1290 |
* chromaticity values (c) have the property:
|
|
1291 |
*
|
|
1292 |
* C
|
|
1293 |
* c = ---------
|
|
1294 |
* X + Y + Z
|
|
1295 |
*
|
|
1296 |
* For each c (x,y,z) from the corresponding original C (X,Y,Z). Thus the
|
|
1297 |
* three chromaticity values (x,y,z) for each end-point obey the
|
|
1298 |
* relationship:
|
|
1299 |
*
|
|
1300 |
* x + y + z = 1
|
|
1301 |
*
|
|
1302 |
* This describes the plane in (X,Y,Z) space that intersects each axis at the
|
|
1303 |
* value 1.0; call this the chromaticity plane. Thus the chromaticity
|
|
1304 |
* calculation has scaled each end-point so that it is on the x+y+z=1 plane
|
|
1305 |
* and chromaticity is the intersection of the vector from the origin to the
|
|
1306 |
* (X,Y,Z) value with the chromaticity plane.
|
|
1307 |
*
|
|
1308 |
* To fully invert the chromaticity calculation we would need the three
|
|
1309 |
* end-point scale factors, (red-scale, green-scale, blue-scale), but these
|
|
1310 |
* were not recorded. Instead we calculated the reference white (X,Y,Z) and
|
|
1311 |
* recorded the chromaticity of this. The reference white (X,Y,Z) would have
|
|
1312 |
* given all three of the scale factors since:
|
|
1313 |
*
|
|
1314 |
* color-C = color-c * color-scale
|
|
1315 |
* white-C = red-C + green-C + blue-C
|
|
1316 |
* = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
|
|
1317 |
*
|
|
1318 |
* But cHRM records only white-x and white-y, so we have lost the white scale
|
|
1319 |
* factor:
|
|
1320 |
*
|
|
1321 |
* white-C = white-c*white-scale
|
|
1322 |
*
|
|
1323 |
* To handle this the inverse transformation makes an arbitrary assumption
|
|
1324 |
* about white-scale:
|
|
1325 |
*
|
|
1326 |
* Assume: white-Y = 1.0
|
|
1327 |
* Hence: white-scale = 1/white-y
|
|
1328 |
* Or: red-Y + green-Y + blue-Y = 1.0
|
|
1329 |
*
|
|
1330 |
* Notice the last statement of the assumption gives an equation in three of
|
|
1331 |
* the nine values we want to calculate. 8 more equations come from the
|
|
1332 |
* above routine as summarised at the top above (the chromaticity
|
|
1333 |
* calculation):
|
|
1334 |
*
|
|
1335 |
* Given: color-x = color-X / (color-X + color-Y + color-Z)
|
|
1336 |
* Hence: (color-x - 1)*color-X + color.x*color-Y + color.x*color-Z = 0
|
|
1337 |
*
|
|
1338 |
* This is 9 simultaneous equations in the 9 variables "color-C" and can be
|
|
1339 |
* solved by Cramer's rule. Cramer's rule requires calculating 10 9x9 matrix
|
|
1340 |
* determinants, however this is not as bad as it seems because only 28 of
|
|
1341 |
* the total of 90 terms in the various matrices are non-zero. Nevertheless
|
|
1342 |
* Cramer's rule is notoriously numerically unstable because the determinant
|
|
1343 |
* calculation involves the difference of large, but similar, numbers. It is
|
|
1344 |
* difficult to be sure that the calculation is stable for real world values
|
|
1345 |
* and it is certain that it becomes unstable where the end points are close
|
|
1346 |
* together.
|
|
1347 |
*
|
|
1348 |
* So this code uses the perhaps slightly less optimal but more
|
|
1349 |
* understandable and totally obvious approach of calculating color-scale.
|
|
1350 |
*
|
|
1351 |
* This algorithm depends on the precision in white-scale and that is
|
|
1352 |
* (1/white-y), so we can immediately see that as white-y approaches 0 the
|
|
1353 |
* accuracy inherent in the cHRM chunk drops off substantially.
|
|
1354 |
*
|
|
1355 |
* libpng arithmetic: a simple inversion of the above equations
|
|
1356 |
* ------------------------------------------------------------
|
|
1357 |
*
|
|
1358 |
* white_scale = 1/white-y
|
|
1359 |
* white-X = white-x * white-scale
|
|
1360 |
* white-Y = 1.0
|
|
1361 |
* white-Z = (1 - white-x - white-y) * white_scale
|
|
1362 |
*
|
|
1363 |
* white-C = red-C + green-C + blue-C
|
|
1364 |
* = red-c*red-scale + green-c*green-scale + blue-c*blue-scale
|
|
1365 |
*
|
|
1366 |
* This gives us three equations in (red-scale,green-scale,blue-scale) where
|
|
1367 |
* all the coefficients are now known:
|
|
1368 |
*
|
|
1369 |
* red-x*red-scale + green-x*green-scale + blue-x*blue-scale
|
|
1370 |
* = white-x/white-y
|
|
1371 |
* red-y*red-scale + green-y*green-scale + blue-y*blue-scale = 1
|
|
1372 |
* red-z*red-scale + green-z*green-scale + blue-z*blue-scale
|
|
1373 |
* = (1 - white-x - white-y)/white-y
|
|
1374 |
*
|
|
1375 |
* In the last equation color-z is (1 - color-x - color-y) so we can add all
|
|
1376 |
* three equations together to get an alternative third:
|
|
1377 |
*
|
|
1378 |
* red-scale + green-scale + blue-scale = 1/white-y = white-scale
|
|
1379 |
*
|
|
1380 |
* So now we have a Cramer's rule solution where the determinants are just
|
|
1381 |
* 3x3 - far more tractible. Unfortunately 3x3 determinants still involve
|
|
1382 |
* multiplication of three coefficients so we can't guarantee to avoid
|
|
1383 |
* overflow in the libpng fixed point representation. Using Cramer's rule in
|
|
1384 |
* floating point is probably a good choice here, but it's not an option for
|
|
1385 |
* fixed point. Instead proceed to simplify the first two equations by
|
|
1386 |
* eliminating what is likely to be the largest value, blue-scale:
|
|
1387 |
*
|
|
1388 |
* blue-scale = white-scale - red-scale - green-scale
|
|
1389 |
*
|
|
1390 |
* Hence:
|
|
1391 |
*
|
|
1392 |
* (red-x - blue-x)*red-scale + (green-x - blue-x)*green-scale =
|
|
1393 |
* (white-x - blue-x)*white-scale
|
|
1394 |
*
|
|
1395 |
* (red-y - blue-y)*red-scale + (green-y - blue-y)*green-scale =
|
|
1396 |
* 1 - blue-y*white-scale
|
|
1397 |
*
|
|
1398 |
* And now we can trivially solve for (red-scale,green-scale):
|
|
1399 |
*
|
|
1400 |
* green-scale =
|
|
1401 |
* (white-x - blue-x)*white-scale - (red-x - blue-x)*red-scale
|
|
1402 |
* -----------------------------------------------------------
|
|
1403 |
* green-x - blue-x
|
|
1404 |
*
|
|
1405 |
* red-scale =
|
|
1406 |
* 1 - blue-y*white-scale - (green-y - blue-y) * green-scale
|
|
1407 |
* ---------------------------------------------------------
|
|
1408 |
* red-y - blue-y
|
|
1409 |
*
|
|
1410 |
* Hence:
|
|
1411 |
*
|
|
1412 |
* red-scale =
|
|
1413 |
* ( (green-x - blue-x) * (white-y - blue-y) -
|
|
1414 |
* (green-y - blue-y) * (white-x - blue-x) ) / white-y
|
|
1415 |
* -------------------------------------------------------------------------
|
|
1416 |
* (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
|
|
1417 |
*
|
|
1418 |
* green-scale =
|
|
1419 |
* ( (red-y - blue-y) * (white-x - blue-x) -
|
|
1420 |
* (red-x - blue-x) * (white-y - blue-y) ) / white-y
|
|
1421 |
* -------------------------------------------------------------------------
|
|
1422 |
* (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x)
|
|
1423 |
*
|
|
1424 |
* Accuracy:
|
|
1425 |
* The input values have 5 decimal digits of accuracy. The values are all in
|
|
1426 |
* the range 0 < value < 1, so simple products are in the same range but may
|
|
1427 |
* need up to 10 decimal digits to preserve the original precision and avoid
|
|
1428 |
* underflow. Because we are using a 32-bit signed representation we cannot
|
|
1429 |
* match this; the best is a little over 9 decimal digits, less than 10.
|
|
1430 |
*
|
|
1431 |
* The approach used here is to preserve the maximum precision within the
|
|
1432 |
* signed representation. Because the red-scale calculation above uses the
|
|
1433 |
* difference between two products of values that must be in the range -1..+1
|
|
1434 |
* it is sufficient to divide the product by 7; ceil(100,000/32767*2). The
|
|
1435 |
* factor is irrelevant in the calculation because it is applied to both
|
|
1436 |
* numerator and denominator.
|
|
1437 |
*
|
|
1438 |
* Note that the values of the differences of the products of the
|
|
1439 |
* chromaticities in the above equations tend to be small, for example for
|
|
1440 |
* the sRGB chromaticities they are:
|
|
1441 |
*
|
|
1442 |
* red numerator: -0.04751
|
|
1443 |
* green numerator: -0.08788
|
|
1444 |
* denominator: -0.2241 (without white-y multiplication)
|
|
1445 |
*
|
|
1446 |
* The resultant Y coefficients from the chromaticities of some widely used
|
|
1447 |
* color space definitions are (to 15 decimal places):
|
|
1448 |
*
|
|
1449 |
* sRGB
|
|
1450 |
* 0.212639005871510 0.715168678767756 0.072192315360734
|
|
1451 |
* Kodak ProPhoto
|
|
1452 |
* 0.288071128229293 0.711843217810102 0.000085653960605
|
|
1453 |
* Adobe RGB
|
|
1454 |
* 0.297344975250536 0.627363566255466 0.075291458493998
|
|
1455 |
* Adobe Wide Gamut RGB
|
|
1456 |
* 0.258728243040113 0.724682314948566 0.016589442011321
|
|
1457 |
*/
|
|
1458 |
/* By the argument, above overflow should be impossible here. The return
|
|
1459 |
* value of 2 indicates an internal error to the caller.
|
|
1460 |
*/
|
|
1461 |
if (png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7) == 0)
|
|
1462 |
return 2;
|
|
1463 |
if (png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7) == 0)
|
|
1464 |
return 2;
|
|
1465 |
denominator = left - right;
|
|
1466 |
|
|
1467 |
/* Now find the red numerator. */
|
|
1468 |
if (png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
|
|
1469 |
return 2;
|
|
1470 |
if (png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
|
|
1471 |
return 2;
|
|
1472 |
|
|
1473 |
/* Overflow is possible here and it indicates an extreme set of PNG cHRM
|
|
1474 |
* chunk values. This calculation actually returns the reciprocal of the
|
|
1475 |
* scale value because this allows us to delay the multiplication of white-y
|
|
1476 |
* into the denominator, which tends to produce a small number.
|
|
1477 |
*/
|
|
1478 |
if (png_muldiv(&red_inverse, xy->whitey, denominator, left-right) == 0 ||
|
|
1479 |
red_inverse <= xy->whitey /* r+g+b scales = white scale */)
|
|
1480 |
return 1;
|
|
1481 |
|
|
1482 |
/* Similarly for green_inverse: */
|
|
1483 |
if (png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7) == 0)
|
|
1484 |
return 2;
|
|
1485 |
if (png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7) == 0)
|
|
1486 |
return 2;
|
|
1487 |
if (png_muldiv(&green_inverse, xy->whitey, denominator, left-right) == 0 ||
|
|
1488 |
green_inverse <= xy->whitey)
|
|
1489 |
return 1;
|
|
1490 |
|
|
1491 |
/* And the blue scale, the checks above guarantee this can't overflow but it
|
|
1492 |
* can still produce 0 for extreme cHRM values.
|
|
1493 |
*/
|
|
1494 |
blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) -
|
|
1495 |
png_reciprocal(green_inverse);
|
|
1496 |
if (blue_scale <= 0)
|
|
1497 |
return 1;
|
|
1498 |
|
|
1499 |
|
|
1500 |
/* And fill in the png_XYZ: */
|
|
1501 |
if (png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse) == 0)
|
|
1502 |
return 1;
|
|
1503 |
if (png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse) == 0)
|
|
1504 |
return 1;
|
|
1505 |
if (png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1,
|
|
1506 |
red_inverse) == 0)
|
|
1507 |
return 1;
|
|
1508 |
|
|
1509 |
if (png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse) == 0)
|
|
1510 |
return 1;
|
|
1511 |
if (png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse) == 0)
|
|
1512 |
return 1;
|
|
1513 |
if (png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1,
|
|
1514 |
green_inverse) == 0)
|
|
1515 |
return 1;
|
|
1516 |
|
|
1517 |
if (png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1) == 0)
|
|
1518 |
return 1;
|
|
1519 |
if (png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1) == 0)
|
|
1520 |
return 1;
|
|
1521 |
if (png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale,
|
|
1522 |
PNG_FP_1) == 0)
|
|
1523 |
return 1;
|
|
1524 |
|
|
1525 |
return 0; /*success*/
|
|
1526 |
}
|
|
1527 |
|
|
1528 |
static int
|
|
1529 |
png_XYZ_normalize(png_XYZ *XYZ)
|
|
1530 |
{
|
|
1531 |
png_int_32 Y;
|
|
1532 |
|
|
1533 |
if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 ||
|
|
1534 |
XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 ||
|
|
1535 |
XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0)
|
|
1536 |
return 1;
|
|
1537 |
|
|
1538 |
/* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1.
|
|
1539 |
* IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore
|
|
1540 |
* relying on addition of two positive values producing a negative one is not
|
|
1541 |
* safe.
|
|
1542 |
*/
|
|
1543 |
Y = XYZ->red_Y;
|
|
1544 |
if (0x7fffffff - Y < XYZ->green_X)
|
|
1545 |
return 1;
|
|
1546 |
Y += XYZ->green_Y;
|
|
1547 |
if (0x7fffffff - Y < XYZ->blue_X)
|
|
1548 |
return 1;
|
|
1549 |
Y += XYZ->blue_Y;
|
|
1550 |
|
|
1551 |
if (Y != PNG_FP_1)
|
|
1552 |
{
|
|
1553 |
if (png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y) == 0)
|
|
1554 |
return 1;
|
|
1555 |
if (png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y) == 0)
|
|
1556 |
return 1;
|
|
1557 |
if (png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y) == 0)
|
|
1558 |
return 1;
|
|
1559 |
|
|
1560 |
if (png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y) == 0)
|
|
1561 |
return 1;
|
|
1562 |
if (png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y) == 0)
|
|
1563 |
return 1;
|
|
1564 |
if (png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y) == 0)
|
|
1565 |
return 1;
|
|
1566 |
|
|
1567 |
if (png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y) == 0)
|
|
1568 |
return 1;
|
|
1569 |
if (png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y) == 0)
|
|
1570 |
return 1;
|
|
1571 |
if (png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y) == 0)
|
|
1572 |
return 1;
|
|
1573 |
}
|
|
1574 |
|
|
1575 |
return 0;
|
|
1576 |
}
|
|
1577 |
|
|
1578 |
static int
|
|
1579 |
png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
|
|
1580 |
{
|
|
1581 |
/* Allow an error of +/-0.01 (absolute value) on each chromaticity */
|
|
1582 |
if (PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) ||
|
|
1583 |
PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) ||
|
|
1584 |
PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) ||
|
|
1585 |
PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) ||
|
|
1586 |
PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) ||
|
|
1587 |
PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) ||
|
|
1588 |
PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) ||
|
|
1589 |
PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta))
|
|
1590 |
return 0;
|
|
1591 |
return 1;
|
|
1592 |
}
|
|
1593 |
|
|
1594 |
/* Added in libpng-1.6.0, a different check for the validity of a set of cHRM
|
|
1595 |
* chunk chromaticities. Earlier checks used to simply look for the overflow
|
|
1596 |
* condition (where the determinant of the matrix to solve for XYZ ends up zero
|
|
1597 |
* because the chromaticity values are not all distinct.) Despite this it is
|
|
1598 |
* theoretically possible to produce chromaticities that are apparently valid
|
|
1599 |
* but that rapidly degrade to invalid, potentially crashing, sets because of
|
|
1600 |
* arithmetic inaccuracies when calculations are performed on them. The new
|
|
1601 |
* check is to round-trip xy -> XYZ -> xy and then check that the result is
|
|
1602 |
* within a small percentage of the original.
|
|
1603 |
*/
|
|
1604 |
static int
|
|
1605 |
png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy)
|
|
1606 |
{
|
|
1607 |
int result;
|
|
1608 |
png_xy xy_test;
|
|
1609 |
|
|
1610 |
/* As a side-effect this routine also returns the XYZ endpoints. */
|
|
1611 |
result = png_XYZ_from_xy(XYZ, xy);
|
|
1612 |
if (result != 0)
|
|
1613 |
return result;
|
|
1614 |
|
|
1615 |
result = png_xy_from_XYZ(&xy_test, XYZ);
|
|
1616 |
if (result != 0)
|
|
1617 |
return result;
|
|
1618 |
|
|
1619 |
if (png_colorspace_endpoints_match(xy, &xy_test,
|
|
1620 |
5/*actually, the math is pretty accurate*/) != 0)
|
|
1621 |
return 0;
|
|
1622 |
|
|
1623 |
/* Too much slip */
|
|
1624 |
return 1;
|
|
1625 |
}
|
|
1626 |
|
|
1627 |
/* This is the check going the other way. The XYZ is modified to normalize it
|
|
1628 |
* (another side-effect) and the xy chromaticities are returned.
|
|
1629 |
*/
|
|
1630 |
static int
|
|
1631 |
png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ)
|
|
1632 |
{
|
|
1633 |
int result;
|
|
1634 |
png_XYZ XYZtemp;
|
|
1635 |
|
|
1636 |
result = png_XYZ_normalize(XYZ);
|
|
1637 |
if (result != 0)
|
|
1638 |
return result;
|
|
1639 |
|
|
1640 |
result = png_xy_from_XYZ(xy, XYZ);
|
|
1641 |
if (result != 0)
|
|
1642 |
return result;
|
|
1643 |
|
|
1644 |
XYZtemp = *XYZ;
|
|
1645 |
return png_colorspace_check_xy(&XYZtemp, xy);
|
|
1646 |
}
|
|
1647 |
|
|
1648 |
/* Used to check for an endpoint match against sRGB */
|
|
1649 |
static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
|
|
1650 |
{
|
|
1651 |
/* color x y */
|
|
1652 |
/* red */ 64000, 33000,
|
|
1653 |
/* green */ 30000, 60000,
|
|
1654 |
/* blue */ 15000, 6000,
|
|
1655 |
/* white */ 31270, 32900
|
|
1656 |
};
|
|
1657 |
|
|
1658 |
static int
|
|
1659 |
png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr,
|
|
1660 |
png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ,
|
|
1661 |
int preferred)
|
|
1662 |
{
|
|
1663 |
if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
|
|
1664 |
return 0;
|
|
1665 |
|
|
1666 |
/* The consistency check is performed on the chromaticities; this factors out
|
|
1667 |
* variations because of the normalization (or not) of the end point Y
|
|
1668 |
* values.
|
|
1669 |
*/
|
|
1670 |
if (preferred < 2 &&
|
|
1671 |
(colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
|
|
1672 |
{
|
|
1673 |
/* The end points must be reasonably close to any we already have. The
|
|
1674 |
* following allows an error of up to +/-.001
|
|
1675 |
*/
|
|
1676 |
if (png_colorspace_endpoints_match(xy, &colorspace->end_points_xy,
|
|
1677 |
100) == 0)
|
|
1678 |
{
|
|
1679 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1680 |
png_benign_error(png_ptr, "inconsistent chromaticities");
|
|
1681 |
return 0; /* failed */
|
|
1682 |
}
|
|
1683 |
|
|
1684 |
/* Only overwrite with preferred values */
|
|
1685 |
if (preferred == 0)
|
|
1686 |
return 1; /* ok, but no change */
|
|
1687 |
}
|
|
1688 |
|
|
1689 |
colorspace->end_points_xy = *xy;
|
|
1690 |
colorspace->end_points_XYZ = *XYZ;
|
|
1691 |
colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS;
|
|
1692 |
|
|
1693 |
/* The end points are normally quoted to two decimal digits, so allow +/-0.01
|
|
1694 |
* on this test.
|
|
1695 |
*/
|
|
1696 |
if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000) != 0)
|
|
1697 |
colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB;
|
|
1698 |
|
|
1699 |
else
|
|
1700 |
colorspace->flags &= PNG_COLORSPACE_CANCEL(
|
|
1701 |
PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
|
|
1702 |
|
|
1703 |
return 2; /* ok and changed */
|
|
1704 |
}
|
10576
|
1705 |
|
|
1706 |
int /* PRIVATE */
|
29913
|
1707 |
png_colorspace_set_chromaticities(png_const_structrp png_ptr,
|
|
1708 |
png_colorspacerp colorspace, const png_xy *xy, int preferred)
|
|
1709 |
{
|
|
1710 |
/* We must check the end points to ensure they are reasonable - in the past
|
|
1711 |
* color management systems have crashed as a result of getting bogus
|
|
1712 |
* colorant values, while this isn't the fault of libpng it is the
|
|
1713 |
* responsibility of libpng because PNG carries the bomb and libpng is in a
|
|
1714 |
* position to protect against it.
|
|
1715 |
*/
|
|
1716 |
png_XYZ XYZ;
|
|
1717 |
|
|
1718 |
switch (png_colorspace_check_xy(&XYZ, xy))
|
|
1719 |
{
|
|
1720 |
case 0: /* success */
|
|
1721 |
return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, xy, &XYZ,
|
|
1722 |
preferred);
|
|
1723 |
|
|
1724 |
case 1:
|
|
1725 |
/* We can't invert the chromaticities so we can't produce value XYZ
|
|
1726 |
* values. Likely as not a color management system will fail too.
|
|
1727 |
*/
|
|
1728 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1729 |
png_benign_error(png_ptr, "invalid chromaticities");
|
|
1730 |
break;
|
|
1731 |
|
|
1732 |
default:
|
|
1733 |
/* libpng is broken; this should be a warning but if it happens we
|
|
1734 |
* want error reports so for the moment it is an error.
|
|
1735 |
*/
|
|
1736 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1737 |
png_error(png_ptr, "internal error checking chromaticities");
|
|
1738 |
break;
|
|
1739 |
}
|
|
1740 |
|
|
1741 |
return 0; /* failed */
|
|
1742 |
}
|
|
1743 |
|
|
1744 |
int /* PRIVATE */
|
|
1745 |
png_colorspace_set_endpoints(png_const_structrp png_ptr,
|
|
1746 |
png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred)
|
|
1747 |
{
|
|
1748 |
png_XYZ XYZ = *XYZ_in;
|
|
1749 |
png_xy xy;
|
|
1750 |
|
|
1751 |
switch (png_colorspace_check_XYZ(&xy, &XYZ))
|
|
1752 |
{
|
|
1753 |
case 0:
|
|
1754 |
return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ,
|
|
1755 |
preferred);
|
|
1756 |
|
|
1757 |
case 1:
|
|
1758 |
/* End points are invalid. */
|
|
1759 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1760 |
png_benign_error(png_ptr, "invalid end points");
|
|
1761 |
break;
|
|
1762 |
|
|
1763 |
default:
|
|
1764 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1765 |
png_error(png_ptr, "internal error checking chromaticities");
|
|
1766 |
break;
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
return 0; /* failed */
|
|
1770 |
}
|
|
1771 |
|
|
1772 |
#if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED)
|
|
1773 |
/* Error message generation */
|
|
1774 |
static char
|
|
1775 |
png_icc_tag_char(png_uint_32 byte)
|
|
1776 |
{
|
|
1777 |
byte &= 0xff;
|
|
1778 |
if (byte >= 32 && byte <= 126)
|
|
1779 |
return (char)byte;
|
|
1780 |
else
|
|
1781 |
return '?';
|
|
1782 |
}
|
|
1783 |
|
|
1784 |
static void
|
|
1785 |
png_icc_tag_name(char *name, png_uint_32 tag)
|
10576
|
1786 |
{
|
29913
|
1787 |
name[0] = '\'';
|
|
1788 |
name[1] = png_icc_tag_char(tag >> 24);
|
|
1789 |
name[2] = png_icc_tag_char(tag >> 16);
|
|
1790 |
name[3] = png_icc_tag_char(tag >> 8);
|
|
1791 |
name[4] = png_icc_tag_char(tag );
|
|
1792 |
name[5] = '\'';
|
|
1793 |
}
|
|
1794 |
|
|
1795 |
static int
|
|
1796 |
is_ICC_signature_char(png_alloc_size_t it)
|
|
1797 |
{
|
|
1798 |
return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) ||
|
|
1799 |
(it >= 97 && it <= 122);
|
|
1800 |
}
|
|
1801 |
|
|
1802 |
static int
|
|
1803 |
is_ICC_signature(png_alloc_size_t it)
|
|
1804 |
{
|
|
1805 |
return is_ICC_signature_char(it >> 24) /* checks all the top bits */ &&
|
|
1806 |
is_ICC_signature_char((it >> 16) & 0xff) &&
|
|
1807 |
is_ICC_signature_char((it >> 8) & 0xff) &&
|
|
1808 |
is_ICC_signature_char(it & 0xff);
|
|
1809 |
}
|
|
1810 |
|
|
1811 |
static int
|
|
1812 |
png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
1813 |
png_const_charp name, png_alloc_size_t value, png_const_charp reason)
|
|
1814 |
{
|
|
1815 |
size_t pos;
|
|
1816 |
char message[196]; /* see below for calculation */
|
|
1817 |
|
|
1818 |
if (colorspace != NULL)
|
|
1819 |
colorspace->flags |= PNG_COLORSPACE_INVALID;
|
|
1820 |
|
|
1821 |
pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */
|
|
1822 |
pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */
|
|
1823 |
pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */
|
|
1824 |
if (is_ICC_signature(value) != 0)
|
|
1825 |
{
|
|
1826 |
/* So 'value' is at most 4 bytes and the following cast is safe */
|
|
1827 |
png_icc_tag_name(message+pos, (png_uint_32)value);
|
|
1828 |
pos += 6; /* total +8; less than the else clause */
|
|
1829 |
message[pos++] = ':';
|
|
1830 |
message[pos++] = ' ';
|
|
1831 |
}
|
|
1832 |
# ifdef PNG_WARNINGS_SUPPORTED
|
|
1833 |
else
|
|
1834 |
{
|
|
1835 |
char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/
|
|
1836 |
|
|
1837 |
pos = png_safecat(message, (sizeof message), pos,
|
|
1838 |
png_format_number(number, number+(sizeof number),
|
|
1839 |
PNG_NUMBER_FORMAT_x, value));
|
|
1840 |
pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/
|
|
1841 |
}
|
|
1842 |
# endif
|
|
1843 |
/* The 'reason' is an arbitrary message, allow +79 maximum 195 */
|
|
1844 |
pos = png_safecat(message, (sizeof message), pos, reason);
|
|
1845 |
PNG_UNUSED(pos)
|
|
1846 |
|
|
1847 |
/* This is recoverable, but make it unconditionally an app_error on write to
|
|
1848 |
* avoid writing invalid ICC profiles into PNG files (i.e., we handle them
|
|
1849 |
* on read, with a warning, but on write unless the app turns off
|
|
1850 |
* application errors the PNG won't be written.)
|
|
1851 |
*/
|
|
1852 |
png_chunk_report(png_ptr, message,
|
|
1853 |
(colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR);
|
|
1854 |
|
|
1855 |
return 0;
|
|
1856 |
}
|
|
1857 |
#endif /* sRGB || iCCP */
|
|
1858 |
|
|
1859 |
#ifdef PNG_sRGB_SUPPORTED
|
|
1860 |
int /* PRIVATE */
|
|
1861 |
png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
1862 |
int intent)
|
|
1863 |
{
|
|
1864 |
/* sRGB sets known gamma, end points and (from the chunk) intent. */
|
|
1865 |
/* IMPORTANT: these are not necessarily the values found in an ICC profile
|
|
1866 |
* because ICC profiles store values adapted to a D50 environment; it is
|
|
1867 |
* expected that the ICC profile mediaWhitePointTag will be D50; see the
|
|
1868 |
* checks and code elsewhere to understand this better.
|
|
1869 |
*
|
|
1870 |
* These XYZ values, which are accurate to 5dp, produce rgb to gray
|
|
1871 |
* coefficients of (6968,23435,2366), which are reduced (because they add up
|
|
1872 |
* to 32769 not 32768) to (6968,23434,2366). These are the values that
|
|
1873 |
* libpng has traditionally used (and are the best values given the 15bit
|
|
1874 |
* algorithm used by the rgb to gray code.)
|
|
1875 |
*/
|
|
1876 |
static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */
|
|
1877 |
{
|
|
1878 |
/* color X Y Z */
|
|
1879 |
/* red */ 41239, 21264, 1933,
|
|
1880 |
/* green */ 35758, 71517, 11919,
|
|
1881 |
/* blue */ 18048, 7219, 95053
|
|
1882 |
};
|
|
1883 |
|
|
1884 |
/* Do nothing if the colorspace is already invalidated. */
|
|
1885 |
if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
|
10576
|
1886 |
return 0;
|
|
1887 |
|
29913
|
1888 |
/* Check the intent, then check for existing settings. It is valid for the
|
|
1889 |
* PNG file to have cHRM or gAMA chunks along with sRGB, but the values must
|
|
1890 |
* be consistent with the correct values. If, however, this function is
|
|
1891 |
* called below because an iCCP chunk matches sRGB then it is quite
|
|
1892 |
* conceivable that an older app recorded incorrect gAMA and cHRM because of
|
|
1893 |
* an incorrect calculation based on the values in the profile - this does
|
|
1894 |
* *not* invalidate the profile (though it still produces an error, which can
|
|
1895 |
* be ignored.)
|
|
1896 |
*/
|
|
1897 |
if (intent < 0 || intent >= PNG_sRGB_INTENT_LAST)
|
|
1898 |
return png_icc_profile_error(png_ptr, colorspace, "sRGB",
|
|
1899 |
(unsigned)intent, "invalid sRGB rendering intent");
|
|
1900 |
|
|
1901 |
if ((colorspace->flags & PNG_COLORSPACE_HAVE_INTENT) != 0 &&
|
|
1902 |
colorspace->rendering_intent != intent)
|
|
1903 |
return png_icc_profile_error(png_ptr, colorspace, "sRGB",
|
|
1904 |
(unsigned)intent, "inconsistent rendering intents");
|
|
1905 |
|
|
1906 |
if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0)
|
|
1907 |
{
|
|
1908 |
png_benign_error(png_ptr, "duplicate sRGB information ignored");
|
|
1909 |
return 0;
|
|
1910 |
}
|
|
1911 |
|
|
1912 |
/* If the standard sRGB cHRM chunk does not match the one from the PNG file
|
|
1913 |
* warn but overwrite the value with the correct one.
|
|
1914 |
*/
|
|
1915 |
if ((colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0 &&
|
|
1916 |
!png_colorspace_endpoints_match(&sRGB_xy, &colorspace->end_points_xy,
|
|
1917 |
100))
|
|
1918 |
png_chunk_report(png_ptr, "cHRM chunk does not match sRGB",
|
|
1919 |
PNG_CHUNK_ERROR);
|
|
1920 |
|
|
1921 |
/* This check is just done for the error reporting - the routine always
|
|
1922 |
* returns true when the 'from' argument corresponds to sRGB (2).
|
|
1923 |
*/
|
|
1924 |
(void)png_colorspace_check_gamma(png_ptr, colorspace, PNG_GAMMA_sRGB_INVERSE,
|
|
1925 |
2/*from sRGB*/);
|
|
1926 |
|
|
1927 |
/* intent: bugs in GCC force 'int' to be used as the parameter type. */
|
|
1928 |
colorspace->rendering_intent = (png_uint_16)intent;
|
|
1929 |
colorspace->flags |= PNG_COLORSPACE_HAVE_INTENT;
|
|
1930 |
|
|
1931 |
/* endpoints */
|
|
1932 |
colorspace->end_points_xy = sRGB_xy;
|
|
1933 |
colorspace->end_points_XYZ = sRGB_XYZ;
|
|
1934 |
colorspace->flags |=
|
|
1935 |
(PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB);
|
|
1936 |
|
|
1937 |
/* gamma */
|
|
1938 |
colorspace->gamma = PNG_GAMMA_sRGB_INVERSE;
|
|
1939 |
colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA;
|
|
1940 |
|
|
1941 |
/* Finally record that we have an sRGB profile */
|
|
1942 |
colorspace->flags |=
|
|
1943 |
(PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB);
|
|
1944 |
|
|
1945 |
return 1; /* set */
|
|
1946 |
}
|
|
1947 |
#endif /* sRGB */
|
|
1948 |
|
|
1949 |
#ifdef PNG_iCCP_SUPPORTED
|
|
1950 |
/* Encoded value of D50 as an ICC XYZNumber. From the ICC 2010 spec the value
|
|
1951 |
* is XYZ(0.9642,1.0,0.8249), which scales to:
|
|
1952 |
*
|
|
1953 |
* (63189.8112, 65536, 54060.6464)
|
|
1954 |
*/
|
|
1955 |
static const png_byte D50_nCIEXYZ[12] =
|
|
1956 |
{ 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d };
|
|
1957 |
|
|
1958 |
int /* PRIVATE */
|
|
1959 |
png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
1960 |
png_const_charp name, png_uint_32 profile_length)
|
|
1961 |
{
|
|
1962 |
if (profile_length < 132)
|
|
1963 |
return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
|
|
1964 |
"too short");
|
|
1965 |
|
|
1966 |
return 1;
|
|
1967 |
}
|
|
1968 |
|
|
1969 |
int /* PRIVATE */
|
|
1970 |
png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
1971 |
png_const_charp name, png_uint_32 profile_length,
|
|
1972 |
png_const_bytep profile/* first 132 bytes only */, int color_type)
|
|
1973 |
{
|
|
1974 |
png_uint_32 temp;
|
|
1975 |
|
|
1976 |
/* Length check; this cannot be ignored in this code because profile_length
|
|
1977 |
* is used later to check the tag table, so even if the profile seems over
|
|
1978 |
* long profile_length from the caller must be correct. The caller can fix
|
|
1979 |
* this up on read or write by just passing in the profile header length.
|
10576
|
1980 |
*/
|
29913
|
1981 |
temp = png_get_uint_32(profile);
|
|
1982 |
if (temp != profile_length)
|
|
1983 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
1984 |
"length does not match profile");
|
|
1985 |
|
|
1986 |
temp = (png_uint_32) (*(profile+8));
|
|
1987 |
if (temp > 3 && (profile_length & 3))
|
|
1988 |
return png_icc_profile_error(png_ptr, colorspace, name, profile_length,
|
|
1989 |
"invalid length");
|
|
1990 |
|
|
1991 |
temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */
|
|
1992 |
if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */
|
|
1993 |
profile_length < 132+12*temp) /* truncated tag table */
|
|
1994 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
1995 |
"tag count too large");
|
|
1996 |
|
|
1997 |
/* The 'intent' must be valid or we can't store it, ICC limits the intent to
|
|
1998 |
* 16 bits.
|
|
1999 |
*/
|
|
2000 |
temp = png_get_uint_32(profile+64);
|
|
2001 |
if (temp >= 0xffff) /* The ICC limit */
|
|
2002 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2003 |
"invalid rendering intent");
|
|
2004 |
|
|
2005 |
/* This is just a warning because the profile may be valid in future
|
|
2006 |
* versions.
|
|
2007 |
*/
|
|
2008 |
if (temp >= PNG_sRGB_INTENT_LAST)
|
|
2009 |
(void)png_icc_profile_error(png_ptr, NULL, name, temp,
|
|
2010 |
"intent outside defined range");
|
|
2011 |
|
|
2012 |
/* At this point the tag table can't be checked because it hasn't necessarily
|
|
2013 |
* been loaded; however, various header fields can be checked. These checks
|
|
2014 |
* are for values permitted by the PNG spec in an ICC profile; the PNG spec
|
|
2015 |
* restricts the profiles that can be passed in an iCCP chunk (they must be
|
|
2016 |
* appropriate to processing PNG data!)
|
|
2017 |
*/
|
|
2018 |
|
|
2019 |
/* Data checks (could be skipped). These checks must be independent of the
|
|
2020 |
* version number; however, the version number doesn't accomodate changes in
|
|
2021 |
* the header fields (just the known tags and the interpretation of the
|
|
2022 |
* data.)
|
|
2023 |
*/
|
|
2024 |
temp = png_get_uint_32(profile+36); /* signature 'ascp' */
|
|
2025 |
if (temp != 0x61637370)
|
|
2026 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2027 |
"invalid signature");
|
|
2028 |
|
|
2029 |
/* Currently the PCS illuminant/adopted white point (the computational
|
|
2030 |
* white point) are required to be D50,
|
|
2031 |
* however the profile contains a record of the illuminant so perhaps ICC
|
|
2032 |
* expects to be able to change this in the future (despite the rationale in
|
|
2033 |
* the introduction for using a fixed PCS adopted white.) Consequently the
|
|
2034 |
* following is just a warning.
|
|
2035 |
*/
|
|
2036 |
if (memcmp(profile+68, D50_nCIEXYZ, 12) != 0)
|
|
2037 |
(void)png_icc_profile_error(png_ptr, NULL, name, 0/*no tag value*/,
|
|
2038 |
"PCS illuminant is not D50");
|
|
2039 |
|
|
2040 |
/* The PNG spec requires this:
|
|
2041 |
* "If the iCCP chunk is present, the image samples conform to the colour
|
|
2042 |
* space represented by the embedded ICC profile as defined by the
|
|
2043 |
* International Color Consortium [ICC]. The colour space of the ICC profile
|
|
2044 |
* shall be an RGB colour space for colour images (PNG colour types 2, 3, and
|
|
2045 |
* 6), or a greyscale colour space for greyscale images (PNG colour types 0
|
|
2046 |
* and 4)."
|
|
2047 |
*
|
|
2048 |
* This checking code ensures the embedded profile (on either read or write)
|
|
2049 |
* conforms to the specification requirements. Notice that an ICC 'gray'
|
|
2050 |
* color-space profile contains the information to transform the monochrome
|
|
2051 |
* data to XYZ or L*a*b (according to which PCS the profile uses) and this
|
|
2052 |
* should be used in preference to the standard libpng K channel replication
|
|
2053 |
* into R, G and B channels.
|
|
2054 |
*
|
|
2055 |
* Previously it was suggested that an RGB profile on grayscale data could be
|
|
2056 |
* handled. However it it is clear that using an RGB profile in this context
|
|
2057 |
* must be an error - there is no specification of what it means. Thus it is
|
|
2058 |
* almost certainly more correct to ignore the profile.
|
|
2059 |
*/
|
|
2060 |
temp = png_get_uint_32(profile+16); /* data colour space field */
|
|
2061 |
switch (temp)
|
10576
|
2062 |
{
|
29913
|
2063 |
case 0x52474220: /* 'RGB ' */
|
|
2064 |
if ((color_type & PNG_COLOR_MASK_COLOR) == 0)
|
|
2065 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2066 |
"RGB color space not permitted on grayscale PNG");
|
|
2067 |
break;
|
|
2068 |
|
|
2069 |
case 0x47524159: /* 'GRAY' */
|
|
2070 |
if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
|
|
2071 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2072 |
"Gray color space not permitted on RGB PNG");
|
|
2073 |
break;
|
|
2074 |
|
|
2075 |
default:
|
|
2076 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2077 |
"invalid ICC profile color space");
|
10576
|
2078 |
}
|
29913
|
2079 |
|
|
2080 |
/* It is up to the application to check that the profile class matches the
|
|
2081 |
* application requirements; the spec provides no guidance, but it's pretty
|
|
2082 |
* weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer
|
|
2083 |
* ('prtr') or 'spac' (for generic color spaces). Issue a warning in these
|
|
2084 |
* cases. Issue an error for device link or abstract profiles - these don't
|
|
2085 |
* contain the records necessary to transform the color-space to anything
|
|
2086 |
* other than the target device (and not even that for an abstract profile).
|
|
2087 |
* Profiles of these classes may not be embedded in images.
|
|
2088 |
*/
|
|
2089 |
temp = png_get_uint_32(profile+12); /* profile/device class */
|
|
2090 |
switch (temp)
|
10576
|
2091 |
{
|
29913
|
2092 |
case 0x73636E72: /* 'scnr' */
|
|
2093 |
case 0x6D6E7472: /* 'mntr' */
|
|
2094 |
case 0x70727472: /* 'prtr' */
|
|
2095 |
case 0x73706163: /* 'spac' */
|
|
2096 |
/* All supported */
|
|
2097 |
break;
|
|
2098 |
|
|
2099 |
case 0x61627374: /* 'abst' */
|
|
2100 |
/* May not be embedded in an image */
|
|
2101 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2102 |
"invalid embedded Abstract ICC profile");
|
|
2103 |
|
|
2104 |
case 0x6C696E6B: /* 'link' */
|
|
2105 |
/* DeviceLink profiles cannot be interpreted in a non-device specific
|
|
2106 |
* fashion, if an app uses the AToB0Tag in the profile the results are
|
|
2107 |
* undefined unless the result is sent to the intended device,
|
|
2108 |
* therefore a DeviceLink profile should not be found embedded in a
|
|
2109 |
* PNG.
|
|
2110 |
*/
|
|
2111 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2112 |
"unexpected DeviceLink ICC profile class");
|
|
2113 |
|
|
2114 |
case 0x6E6D636C: /* 'nmcl' */
|
|
2115 |
/* A NamedColor profile is also device specific, however it doesn't
|
|
2116 |
* contain an AToB0 tag that is open to misinterpretation. Almost
|
|
2117 |
* certainly it will fail the tests below.
|
|
2118 |
*/
|
|
2119 |
(void)png_icc_profile_error(png_ptr, NULL, name, temp,
|
|
2120 |
"unexpected NamedColor ICC profile class");
|
|
2121 |
break;
|
|
2122 |
|
|
2123 |
default:
|
|
2124 |
/* To allow for future enhancements to the profile accept unrecognized
|
|
2125 |
* profile classes with a warning, these then hit the test below on the
|
|
2126 |
* tag content to ensure they are backward compatible with one of the
|
|
2127 |
* understood profiles.
|
|
2128 |
*/
|
|
2129 |
(void)png_icc_profile_error(png_ptr, NULL, name, temp,
|
|
2130 |
"unrecognized ICC profile class");
|
|
2131 |
break;
|
10576
|
2132 |
}
|
|
2133 |
|
29913
|
2134 |
/* For any profile other than a device link one the PCS must be encoded
|
|
2135 |
* either in XYZ or Lab.
|
|
2136 |
*/
|
|
2137 |
temp = png_get_uint_32(profile+20);
|
|
2138 |
switch (temp)
|
10576
|
2139 |
{
|
29913
|
2140 |
case 0x58595A20: /* 'XYZ ' */
|
|
2141 |
case 0x4C616220: /* 'Lab ' */
|
|
2142 |
break;
|
|
2143 |
|
|
2144 |
default:
|
|
2145 |
return png_icc_profile_error(png_ptr, colorspace, name, temp,
|
|
2146 |
"unexpected ICC PCS encoding");
|
10576
|
2147 |
}
|
|
2148 |
|
29913
|
2149 |
return 1;
|
|
2150 |
}
|
|
2151 |
|
|
2152 |
int /* PRIVATE */
|
|
2153 |
png_icc_check_tag_table(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
2154 |
png_const_charp name, png_uint_32 profile_length,
|
|
2155 |
png_const_bytep profile /* header plus whole tag table */)
|
|
2156 |
{
|
|
2157 |
png_uint_32 tag_count = png_get_uint_32(profile+128);
|
|
2158 |
png_uint_32 itag;
|
|
2159 |
png_const_bytep tag = profile+132; /* The first tag */
|
|
2160 |
|
|
2161 |
/* First scan all the tags in the table and add bits to the icc_info value
|
|
2162 |
* (temporarily in 'tags').
|
|
2163 |
*/
|
|
2164 |
for (itag=0; itag < tag_count; ++itag, tag += 12)
|
10576
|
2165 |
{
|
29913
|
2166 |
png_uint_32 tag_id = png_get_uint_32(tag+0);
|
|
2167 |
png_uint_32 tag_start = png_get_uint_32(tag+4); /* must be aligned */
|
|
2168 |
png_uint_32 tag_length = png_get_uint_32(tag+8);/* not padded */
|
|
2169 |
|
|
2170 |
/* The ICC specification does not exclude zero length tags, therefore the
|
|
2171 |
* start might actually be anywhere if there is no data, but this would be
|
|
2172 |
* a clear abuse of the intent of the standard so the start is checked for
|
|
2173 |
* being in range. All defined tag types have an 8 byte header - a 4 byte
|
|
2174 |
* type signature then 0.
|
|
2175 |
*/
|
|
2176 |
if ((tag_start & 3) != 0)
|
|
2177 |
{
|
|
2178 |
/* CNHP730S.icc shipped with Microsoft Windows 64 violates this, it is
|
|
2179 |
* only a warning here because libpng does not care about the
|
|
2180 |
* alignment.
|
|
2181 |
*/
|
|
2182 |
(void)png_icc_profile_error(png_ptr, NULL, name, tag_id,
|
|
2183 |
"ICC profile tag start not a multiple of 4");
|
|
2184 |
}
|
|
2185 |
|
|
2186 |
/* This is a hard error; potentially it can cause read outside the
|
|
2187 |
* profile.
|
|
2188 |
*/
|
|
2189 |
if (tag_start > profile_length || tag_length > profile_length - tag_start)
|
|
2190 |
return png_icc_profile_error(png_ptr, colorspace, name, tag_id,
|
|
2191 |
"ICC profile tag outside profile");
|
10576
|
2192 |
}
|
|
2193 |
|
29913
|
2194 |
return 1; /* success, maybe with warnings */
|
|
2195 |
}
|
|
2196 |
|
|
2197 |
#if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0
|
|
2198 |
/* Information about the known ICC sRGB profiles */
|
|
2199 |
static const struct
|
|
2200 |
{
|
|
2201 |
png_uint_32 adler, crc, length;
|
|
2202 |
png_uint_32 md5[4];
|
|
2203 |
png_byte have_md5;
|
|
2204 |
png_byte is_broken;
|
|
2205 |
png_uint_16 intent;
|
|
2206 |
|
|
2207 |
# define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
|
|
2208 |
# define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)\
|
|
2209 |
{ adler, crc, length, md5, broke, intent },
|
|
2210 |
|
|
2211 |
} png_sRGB_checks[] =
|
|
2212 |
{
|
|
2213 |
/* This data comes from contrib/tools/checksum-icc run on downloads of
|
|
2214 |
* all four ICC sRGB profiles from www.color.org.
|
|
2215 |
*/
|
|
2216 |
/* adler32, crc32, MD5[4], intent, date, length, file-name */
|
|
2217 |
PNG_ICC_CHECKSUM(0x0a3fd9f6, 0x3b8772b9,
|
|
2218 |
PNG_MD5(0x29f83dde, 0xaff255ae, 0x7842fae4, 0xca83390d), 0, 0,
|
|
2219 |
"2009/03/27 21:36:31", 3048, "sRGB_IEC61966-2-1_black_scaled.icc")
|
|
2220 |
|
|
2221 |
/* ICC sRGB v2 perceptual no black-compensation: */
|
|
2222 |
PNG_ICC_CHECKSUM(0x4909e5e1, 0x427ebb21,
|
|
2223 |
PNG_MD5(0xc95bd637, 0xe95d8a3b, 0x0df38f99, 0xc1320389), 1, 0,
|
|
2224 |
"2009/03/27 21:37:45", 3052, "sRGB_IEC61966-2-1_no_black_scaling.icc")
|
|
2225 |
|
|
2226 |
PNG_ICC_CHECKSUM(0xfd2144a1, 0x306fd8ae,
|
|
2227 |
PNG_MD5(0xfc663378, 0x37e2886b, 0xfd72e983, 0x8228f1b8), 0, 0,
|
|
2228 |
"2009/08/10 17:28:01", 60988, "sRGB_v4_ICC_preference_displayclass.icc")
|
|
2229 |
|
|
2230 |
/* ICC sRGB v4 perceptual */
|
|
2231 |
PNG_ICC_CHECKSUM(0x209c35d2, 0xbbef7812,
|
|
2232 |
PNG_MD5(0x34562abf, 0x994ccd06, 0x6d2c5721, 0xd0d68c5d), 0, 0,
|
|
2233 |
"2007/07/25 00:05:37", 60960, "sRGB_v4_ICC_preference.icc")
|
|
2234 |
|
|
2235 |
/* The following profiles have no known MD5 checksum. If there is a match
|
|
2236 |
* on the (empty) MD5 the other fields are used to attempt a match and
|
|
2237 |
* a warning is produced. The first two of these profiles have a 'cprt' tag
|
|
2238 |
* which suggests that they were also made by Hewlett Packard.
|
|
2239 |
*/
|
|
2240 |
PNG_ICC_CHECKSUM(0xa054d762, 0x5d5129ce,
|
|
2241 |
PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 0,
|
|
2242 |
"2004/07/21 18:57:42", 3024, "sRGB_IEC61966-2-1_noBPC.icc")
|
|
2243 |
|
|
2244 |
/* This is a 'mntr' (display) profile with a mediaWhitePointTag that does not
|
|
2245 |
* match the D50 PCS illuminant in the header (it is in fact the D65 values,
|
|
2246 |
* so the white point is recorded as the un-adapted value.) The profiles
|
|
2247 |
* below only differ in one byte - the intent - and are basically the same as
|
|
2248 |
* the previous profile except for the mediaWhitePointTag error and a missing
|
|
2249 |
* chromaticAdaptationTag.
|
|
2250 |
*/
|
|
2251 |
PNG_ICC_CHECKSUM(0xf784f3fb, 0x182ea552,
|
|
2252 |
PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/,
|
|
2253 |
"1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual")
|
|
2254 |
|
|
2255 |
PNG_ICC_CHECKSUM(0x0398f3fc, 0xf29e526d,
|
|
2256 |
PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/,
|
|
2257 |
"1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative")
|
|
2258 |
};
|
|
2259 |
|
|
2260 |
static int
|
|
2261 |
png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr,
|
|
2262 |
png_const_bytep profile, uLong adler)
|
|
2263 |
{
|
|
2264 |
/* The quick check is to verify just the MD5 signature and trust the
|
|
2265 |
* rest of the data. Because the profile has already been verified for
|
|
2266 |
* correctness this is safe. png_colorspace_set_sRGB will check the 'intent'
|
|
2267 |
* field too, so if the profile has been edited with an intent not defined
|
|
2268 |
* by sRGB (but maybe defined by a later ICC specification) the read of
|
|
2269 |
* the profile will fail at that point.
|
|
2270 |
*/
|
|
2271 |
|
|
2272 |
png_uint_32 length = 0;
|
|
2273 |
png_uint_32 intent = 0x10000; /* invalid */
|
|
2274 |
#if PNG_sRGB_PROFILE_CHECKS > 1
|
|
2275 |
uLong crc = 0; /* the value for 0 length data */
|
|
2276 |
#endif
|
|
2277 |
unsigned int i;
|
|
2278 |
|
|
2279 |
#ifdef PNG_SET_OPTION_SUPPORTED
|
|
2280 |
/* First see if PNG_SKIP_sRGB_CHECK_PROFILE has been set to "on" */
|
|
2281 |
if (((png_ptr->options >> PNG_SKIP_sRGB_CHECK_PROFILE) & 3) ==
|
|
2282 |
PNG_OPTION_ON)
|
|
2283 |
return 0;
|
|
2284 |
#endif
|
|
2285 |
|
|
2286 |
for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i)
|
10576
|
2287 |
{
|
29913
|
2288 |
if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] &&
|
|
2289 |
png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] &&
|
|
2290 |
png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] &&
|
|
2291 |
png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3])
|
|
2292 |
{
|
|
2293 |
/* This may be one of the old HP profiles without an MD5, in that
|
|
2294 |
* case we can only use the length and Adler32 (note that these
|
|
2295 |
* are not used by default if there is an MD5!)
|
|
2296 |
*/
|
|
2297 |
# if PNG_sRGB_PROFILE_CHECKS == 0
|
|
2298 |
if (png_sRGB_checks[i].have_md5 != 0)
|
|
2299 |
return 1+png_sRGB_checks[i].is_broken;
|
|
2300 |
# endif
|
|
2301 |
|
|
2302 |
/* Profile is unsigned or more checks have been configured in. */
|
|
2303 |
if (length == 0)
|
|
2304 |
{
|
|
2305 |
length = png_get_uint_32(profile);
|
|
2306 |
intent = png_get_uint_32(profile+64);
|
|
2307 |
}
|
|
2308 |
|
|
2309 |
/* Length *and* intent must match */
|
|
2310 |
if (length == png_sRGB_checks[i].length &&
|
|
2311 |
intent == png_sRGB_checks[i].intent)
|
|
2312 |
{
|
|
2313 |
/* Now calculate the adler32 if not done already. */
|
|
2314 |
if (adler == 0)
|
|
2315 |
{
|
|
2316 |
adler = adler32(0, NULL, 0);
|
|
2317 |
adler = adler32(adler, profile, length);
|
|
2318 |
}
|
|
2319 |
|
|
2320 |
if (adler == png_sRGB_checks[i].adler)
|
|
2321 |
{
|
|
2322 |
/* These basic checks suggest that the data has not been
|
|
2323 |
* modified, but if the check level is more than 1 perform
|
|
2324 |
* our own crc32 checksum on the data.
|
|
2325 |
*/
|
|
2326 |
# if PNG_sRGB_PROFILE_CHECKS > 1
|
|
2327 |
if (crc == 0)
|
|
2328 |
{
|
|
2329 |
crc = crc32(0, NULL, 0);
|
|
2330 |
crc = crc32(crc, profile, length);
|
|
2331 |
}
|
|
2332 |
|
|
2333 |
/* So this check must pass for the 'return' below to happen.
|
|
2334 |
*/
|
|
2335 |
if (crc == png_sRGB_checks[i].crc)
|
|
2336 |
# endif
|
|
2337 |
{
|
|
2338 |
if (png_sRGB_checks[i].is_broken != 0)
|
|
2339 |
{
|
|
2340 |
/* These profiles are known to have bad data that may cause
|
|
2341 |
* problems if they are used, therefore attempt to
|
|
2342 |
* discourage their use, skip the 'have_md5' warning below,
|
|
2343 |
* which is made irrelevant by this error.
|
|
2344 |
*/
|
|
2345 |
png_chunk_report(png_ptr, "known incorrect sRGB profile",
|
|
2346 |
PNG_CHUNK_ERROR);
|
|
2347 |
}
|
|
2348 |
|
|
2349 |
/* Warn that this being done; this isn't even an error since
|
|
2350 |
* the profile is perfectly valid, but it would be nice if
|
|
2351 |
* people used the up-to-date ones.
|
|
2352 |
*/
|
|
2353 |
else if (png_sRGB_checks[i].have_md5 == 0)
|
|
2354 |
{
|
|
2355 |
png_chunk_report(png_ptr, "out-of-date sRGB profile with"
|
|
2356 |
" no signature",
|
|
2357 |
PNG_CHUNK_WARNING);
|
|
2358 |
}
|
|
2359 |
|
|
2360 |
return 1+png_sRGB_checks[i].is_broken;
|
|
2361 |
}
|
|
2362 |
}
|
|
2363 |
|
|
2364 |
# if PNG_sRGB_PROFILE_CHECKS > 0
|
|
2365 |
/* The signature matched, but the profile had been changed in some
|
|
2366 |
* way. This probably indicates a data error or uninformed hacking.
|
|
2367 |
* Fall through to "no match".
|
|
2368 |
*/
|
|
2369 |
png_chunk_report(png_ptr, "Not recognizing known sRGB profile that"
|
|
2370 |
" has been edited",
|
|
2371 |
PNG_CHUNK_WARNING);
|
|
2372 |
break;
|
|
2373 |
# endif
|
|
2374 |
}
|
|
2375 |
}
|
10576
|
2376 |
}
|
|
2377 |
|
29913
|
2378 |
return 0; /* no match */
|
|
2379 |
}
|
|
2380 |
#endif
|
|
2381 |
|
|
2382 |
#ifdef PNG_sRGB_SUPPORTED
|
|
2383 |
void /* PRIVATE */
|
|
2384 |
png_icc_set_sRGB(png_const_structrp png_ptr,
|
|
2385 |
png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
|
|
2386 |
{
|
|
2387 |
/* Is this profile one of the known ICC sRGB profiles? If it is, just set
|
|
2388 |
* the sRGB information.
|
|
2389 |
*/
|
|
2390 |
#if PNG_sRGB_PROFILE_CHECKS >= 0
|
|
2391 |
if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0)
|
|
2392 |
#endif
|
|
2393 |
(void)png_colorspace_set_sRGB(png_ptr, colorspace,
|
|
2394 |
(int)/*already checked*/png_get_uint_32(profile+64));
|
|
2395 |
}
|
|
2396 |
#endif /* READ_sRGB */
|
|
2397 |
|
|
2398 |
int /* PRIVATE */
|
|
2399 |
png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace,
|
|
2400 |
png_const_charp name, png_uint_32 profile_length, png_const_bytep profile,
|
|
2401 |
int color_type)
|
|
2402 |
{
|
|
2403 |
if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0)
|
|
2404 |
return 0;
|
|
2405 |
|
|
2406 |
if (png_icc_check_length(png_ptr, colorspace, name, profile_length) != 0 &&
|
|
2407 |
png_icc_check_header(png_ptr, colorspace, name, profile_length, profile,
|
|
2408 |
color_type) != 0 &&
|
|
2409 |
png_icc_check_tag_table(png_ptr, colorspace, name, profile_length,
|
|
2410 |
profile) != 0)
|
10576
|
2411 |
{
|
29913
|
2412 |
# ifdef PNG_sRGB_SUPPORTED
|
|
2413 |
/* If no sRGB support, don't try storing sRGB information */
|
|
2414 |
png_icc_set_sRGB(png_ptr, colorspace, profile, 0);
|
|
2415 |
# endif
|
|
2416 |
return 1;
|
10576
|
2417 |
}
|
|
2418 |
|
29913
|
2419 |
/* Failure case */
|
|
2420 |
return 0;
|
10576
|
2421 |
}
|
29913
|
2422 |
#endif /* iCCP */
|
|
2423 |
|
|
2424 |
#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
|
|
2425 |
void /* PRIVATE */
|
|
2426 |
png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
|
|
2427 |
{
|
|
2428 |
/* Set the rgb_to_gray coefficients from the colorspace. */
|
|
2429 |
if (png_ptr->rgb_to_gray_coefficients_set == 0 &&
|
|
2430 |
(png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0)
|
|
2431 |
{
|
|
2432 |
/* png_set_background has not been called, get the coefficients from the Y
|
|
2433 |
* values of the colorspace colorants.
|
|
2434 |
*/
|
|
2435 |
png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y;
|
|
2436 |
png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y;
|
|
2437 |
png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y;
|
|
2438 |
png_fixed_point total = r+g+b;
|
|
2439 |
|
|
2440 |
if (total > 0 &&
|
|
2441 |
r >= 0 && png_muldiv(&r, r, 32768, total) && r >= 0 && r <= 32768 &&
|
|
2442 |
g >= 0 && png_muldiv(&g, g, 32768, total) && g >= 0 && g <= 32768 &&
|
|
2443 |
b >= 0 && png_muldiv(&b, b, 32768, total) && b >= 0 && b <= 32768 &&
|
|
2444 |
r+g+b <= 32769)
|
|
2445 |
{
|
|
2446 |
/* We allow 0 coefficients here. r+g+b may be 32769 if two or
|
|
2447 |
* all of the coefficients were rounded up. Handle this by
|
|
2448 |
* reducing the *largest* coefficient by 1; this matches the
|
|
2449 |
* approach used for the default coefficients in pngrtran.c
|
|
2450 |
*/
|
|
2451 |
int add = 0;
|
|
2452 |
|
|
2453 |
if (r+g+b > 32768)
|
|
2454 |
add = -1;
|
|
2455 |
else if (r+g+b < 32768)
|
|
2456 |
add = 1;
|
|
2457 |
|
|
2458 |
if (add != 0)
|
|
2459 |
{
|
|
2460 |
if (g >= r && g >= b)
|
|
2461 |
g += add;
|
|
2462 |
else if (r >= g && r >= b)
|
|
2463 |
r += add;
|
|
2464 |
else
|
|
2465 |
b += add;
|
|
2466 |
}
|
|
2467 |
|
|
2468 |
/* Check for an internal error. */
|
|
2469 |
if (r+g+b != 32768)
|
|
2470 |
png_error(png_ptr,
|
|
2471 |
"internal error handling cHRM coefficients");
|
|
2472 |
|
|
2473 |
else
|
|
2474 |
{
|
|
2475 |
png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r;
|
|
2476 |
png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g;
|
|
2477 |
}
|
|
2478 |
}
|
|
2479 |
|
|
2480 |
/* This is a png_error at present even though it could be ignored -
|
|
2481 |
* it should never happen, but it is important that if it does, the
|
|
2482 |
* bug is fixed.
|
|
2483 |
*/
|
|
2484 |
else
|
|
2485 |
png_error(png_ptr, "internal error handling cHRM->XYZ");
|
|
2486 |
}
|
|
2487 |
}
|
|
2488 |
#endif
|
|
2489 |
|
|
2490 |
#endif /* COLORSPACE */
|
|
2491 |
|
|
2492 |
#ifdef __GNUC__
|
|
2493 |
/* This exists solely to work round a warning from GNU C. */
|
|
2494 |
static int /* PRIVATE */
|
|
2495 |
png_gt(size_t a, size_t b)
|
|
2496 |
{
|
|
2497 |
return a > b;
|
|
2498 |
}
|
|
2499 |
#else
|
|
2500 |
# define png_gt(a,b) ((a) > (b))
|
|
2501 |
#endif
|
10576
|
2502 |
|
|
2503 |
void /* PRIVATE */
|
29913
|
2504 |
png_check_IHDR(png_const_structrp png_ptr,
|
10576
|
2505 |
png_uint_32 width, png_uint_32 height, int bit_depth,
|
|
2506 |
int color_type, int interlace_type, int compression_type,
|
|
2507 |
int filter_type)
|
|
2508 |
{
|
|
2509 |
int error = 0;
|
|
2510 |
|
|
2511 |
/* Check for width and height valid values */
|
|
2512 |
if (width == 0)
|
|
2513 |
{
|
|
2514 |
png_warning(png_ptr, "Image width is zero in IHDR");
|
|
2515 |
error = 1;
|
|
2516 |
}
|
29913
|
2517 |
else if (width > PNG_UINT_31_MAX)
|
|
2518 |
{
|
|
2519 |
png_warning(png_ptr, "Invalid image width in IHDR");
|
|
2520 |
error = 1;
|
|
2521 |
}
|
|
2522 |
|
|
2523 |
else if (png_gt(width,
|
|
2524 |
(PNG_SIZE_MAX >> 3) /* 8-byte RGBA pixels */
|
|
2525 |
- 48 /* big_row_buf hack */
|
|
2526 |
- 1 /* filter byte */
|
|
2527 |
- 7*8 /* rounding width to multiple of 8 pix */
|
|
2528 |
- 8)) /* extra max_pixel_depth pad */
|
|
2529 |
{
|
|
2530 |
/* The size of the row must be within the limits of this architecture.
|
|
2531 |
* Because the read code can perform arbitrary transformations the
|
|
2532 |
* maximum size is checked here. Because the code in png_read_start_row
|
|
2533 |
* adds extra space "for safety's sake" in several places a conservative
|
|
2534 |
* limit is used here.
|
|
2535 |
*
|
|
2536 |
* NOTE: it would be far better to check the size that is actually used,
|
|
2537 |
* but the effect in the real world is minor and the changes are more
|
|
2538 |
* extensive, therefore much more dangerous and much more difficult to
|
|
2539 |
* write in a way that avoids compiler warnings.
|
|
2540 |
*/
|
|
2541 |
png_warning(png_ptr, "Image width is too large for this architecture");
|
|
2542 |
error = 1;
|
|
2543 |
}
|
|
2544 |
else
|
|
2545 |
{
|
|
2546 |
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
|
2547 |
if (width > png_ptr->user_width_max)
|
|
2548 |
# else
|
|
2549 |
if (width > PNG_USER_WIDTH_MAX)
|
|
2550 |
# endif
|
|
2551 |
{
|
|
2552 |
png_warning(png_ptr, "Image width exceeds user limit in IHDR");
|
|
2553 |
error = 1;
|
|
2554 |
}
|
|
2555 |
}
|
10576
|
2556 |
|
|
2557 |
if (height == 0)
|
|
2558 |
{
|
|
2559 |
png_warning(png_ptr, "Image height is zero in IHDR");
|
|
2560 |
error = 1;
|
|
2561 |
}
|
29913
|
2562 |
else if (height > PNG_UINT_31_MAX)
|
10576
|
2563 |
{
|
|
2564 |
png_warning(png_ptr, "Invalid image height in IHDR");
|
|
2565 |
error = 1;
|
|
2566 |
}
|
29913
|
2567 |
else
|
|
2568 |
{
|
|
2569 |
# ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
|
2570 |
if (height > png_ptr->user_height_max)
|
|
2571 |
# else
|
|
2572 |
if (height > PNG_USER_HEIGHT_MAX)
|
|
2573 |
# endif
|
|
2574 |
{
|
|
2575 |
png_warning(png_ptr, "Image height exceeds user limit in IHDR");
|
|
2576 |
error = 1;
|
|
2577 |
}
|
|
2578 |
}
|
10576
|
2579 |
|
|
2580 |
/* Check other values */
|
|
2581 |
if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
|
|
2582 |
bit_depth != 8 && bit_depth != 16)
|
|
2583 |
{
|
|
2584 |
png_warning(png_ptr, "Invalid bit depth in IHDR");
|
|
2585 |
error = 1;
|
|
2586 |
}
|
|
2587 |
|
|
2588 |
if (color_type < 0 || color_type == 1 ||
|
|
2589 |
color_type == 5 || color_type > 6)
|
|
2590 |
{
|
|
2591 |
png_warning(png_ptr, "Invalid color type in IHDR");
|
|
2592 |
error = 1;
|
|
2593 |
}
|
|
2594 |
|
|
2595 |
if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
|
|
2596 |
((color_type == PNG_COLOR_TYPE_RGB ||
|
|
2597 |
color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
|
|
2598 |
color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
|
|
2599 |
{
|
|
2600 |
png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
|
|
2601 |
error = 1;
|
|
2602 |
}
|
|
2603 |
|
|
2604 |
if (interlace_type >= PNG_INTERLACE_LAST)
|
|
2605 |
{
|
|
2606 |
png_warning(png_ptr, "Unknown interlace method in IHDR");
|
|
2607 |
error = 1;
|
|
2608 |
}
|
|
2609 |
|
|
2610 |
if (compression_type != PNG_COMPRESSION_TYPE_BASE)
|
|
2611 |
{
|
|
2612 |
png_warning(png_ptr, "Unknown compression method in IHDR");
|
|
2613 |
error = 1;
|
|
2614 |
}
|
|
2615 |
|
|
2616 |
# ifdef PNG_MNG_FEATURES_SUPPORTED
|
|
2617 |
/* Accept filter_method 64 (intrapixel differencing) only if
|
|
2618 |
* 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
|
|
2619 |
* 2. Libpng did not read a PNG signature (this filter_method is only
|
|
2620 |
* used in PNG datastreams that are embedded in MNG datastreams) and
|
|
2621 |
* 3. The application called png_permit_mng_features with a mask that
|
|
2622 |
* included PNG_FLAG_MNG_FILTER_64 and
|
|
2623 |
* 4. The filter_method is 64 and
|
|
2624 |
* 5. The color_type is RGB or RGBA
|
|
2625 |
*/
|
29913
|
2626 |
if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 &&
|
|
2627 |
png_ptr->mng_features_permitted != 0)
|
10576
|
2628 |
png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
|
|
2629 |
|
|
2630 |
if (filter_type != PNG_FILTER_TYPE_BASE)
|
|
2631 |
{
|
29913
|
2632 |
if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
|
10576
|
2633 |
(filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
|
|
2634 |
((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
|
|
2635 |
(color_type == PNG_COLOR_TYPE_RGB ||
|
|
2636 |
color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
|
|
2637 |
{
|
|
2638 |
png_warning(png_ptr, "Unknown filter method in IHDR");
|
|
2639 |
error = 1;
|
|
2640 |
}
|
|
2641 |
|
29913
|
2642 |
if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0)
|
10576
|
2643 |
{
|
|
2644 |
png_warning(png_ptr, "Invalid filter method in IHDR");
|
|
2645 |
error = 1;
|
|
2646 |
}
|
|
2647 |
}
|
|
2648 |
|
|
2649 |
# else
|
|
2650 |
if (filter_type != PNG_FILTER_TYPE_BASE)
|
|
2651 |
{
|
|
2652 |
png_warning(png_ptr, "Unknown filter method in IHDR");
|
|
2653 |
error = 1;
|
|
2654 |
}
|
|
2655 |
# endif
|
|
2656 |
|
|
2657 |
if (error == 1)
|
|
2658 |
png_error(png_ptr, "Invalid IHDR data");
|
|
2659 |
}
|
|
2660 |
|
|
2661 |
#if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED)
|
|
2662 |
/* ASCII to fp functions */
|
|
2663 |
/* Check an ASCII formated floating point value, see the more detailed
|
|
2664 |
* comments in pngpriv.h
|
|
2665 |
*/
|
|
2666 |
/* The following is used internally to preserve the sticky flags */
|
|
2667 |
#define png_fp_add(state, flags) ((state) |= (flags))
|
|
2668 |
#define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY))
|
|
2669 |
|
|
2670 |
int /* PRIVATE */
|
|
2671 |
png_check_fp_number(png_const_charp string, png_size_t size, int *statep,
|
|
2672 |
png_size_tp whereami)
|
|
2673 |
{
|
|
2674 |
int state = *statep;
|
|
2675 |
png_size_t i = *whereami;
|
|
2676 |
|
|
2677 |
while (i < size)
|
|
2678 |
{
|
|
2679 |
int type;
|
|
2680 |
/* First find the type of the next character */
|
|
2681 |
switch (string[i])
|
|
2682 |
{
|
|
2683 |
case 43: type = PNG_FP_SAW_SIGN; break;
|
|
2684 |
case 45: type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break;
|
|
2685 |
case 46: type = PNG_FP_SAW_DOT; break;
|
|
2686 |
case 48: type = PNG_FP_SAW_DIGIT; break;
|
|
2687 |
case 49: case 50: case 51: case 52:
|
|
2688 |
case 53: case 54: case 55: case 56:
|
|
2689 |
case 57: type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break;
|
|
2690 |
case 69:
|
|
2691 |
case 101: type = PNG_FP_SAW_E; break;
|
|
2692 |
default: goto PNG_FP_End;
|
|
2693 |
}
|
|
2694 |
|
|
2695 |
/* Now deal with this type according to the current
|
|
2696 |
* state, the type is arranged to not overlap the
|
|
2697 |
* bits of the PNG_FP_STATE.
|
|
2698 |
*/
|
|
2699 |
switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY))
|
|
2700 |
{
|
|
2701 |
case PNG_FP_INTEGER + PNG_FP_SAW_SIGN:
|
29913
|
2702 |
if ((state & PNG_FP_SAW_ANY) != 0)
|
10576
|
2703 |
goto PNG_FP_End; /* not a part of the number */
|
|
2704 |
|
|
2705 |
png_fp_add(state, type);
|
|
2706 |
break;
|
|
2707 |
|
|
2708 |
case PNG_FP_INTEGER + PNG_FP_SAW_DOT:
|
|
2709 |
/* Ok as trailer, ok as lead of fraction. */
|
29913
|
2710 |
if ((state & PNG_FP_SAW_DOT) != 0) /* two dots */
|
10576
|
2711 |
goto PNG_FP_End;
|
|
2712 |
|
29913
|
2713 |
else if ((state & PNG_FP_SAW_DIGIT) != 0) /* trailing dot? */
|
10576
|
2714 |
png_fp_add(state, type);
|
|
2715 |
|
|
2716 |
else
|
|
2717 |
png_fp_set(state, PNG_FP_FRACTION | type);
|
|
2718 |
|
|
2719 |
break;
|
|
2720 |
|
|
2721 |
case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT:
|
29913
|
2722 |
if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */
|
10576
|
2723 |
png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
|
|
2724 |
|
|
2725 |
png_fp_add(state, type | PNG_FP_WAS_VALID);
|
|
2726 |
|
|
2727 |
break;
|
|
2728 |
|
|
2729 |
case PNG_FP_INTEGER + PNG_FP_SAW_E:
|
|
2730 |
if ((state & PNG_FP_SAW_DIGIT) == 0)
|
|
2731 |
goto PNG_FP_End;
|
|
2732 |
|
|
2733 |
png_fp_set(state, PNG_FP_EXPONENT);
|
|
2734 |
|
|
2735 |
break;
|
|
2736 |
|
|
2737 |
/* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN:
|
|
2738 |
goto PNG_FP_End; ** no sign in fraction */
|
|
2739 |
|
|
2740 |
/* case PNG_FP_FRACTION + PNG_FP_SAW_DOT:
|
|
2741 |
goto PNG_FP_End; ** Because SAW_DOT is always set */
|
|
2742 |
|
|
2743 |
case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT:
|
|
2744 |
png_fp_add(state, type | PNG_FP_WAS_VALID);
|
|
2745 |
break;
|
|
2746 |
|
|
2747 |
case PNG_FP_FRACTION + PNG_FP_SAW_E:
|
|
2748 |
/* This is correct because the trailing '.' on an
|
|
2749 |
* integer is handled above - so we can only get here
|
|
2750 |
* with the sequence ".E" (with no preceding digits).
|
|
2751 |
*/
|
|
2752 |
if ((state & PNG_FP_SAW_DIGIT) == 0)
|
|
2753 |
goto PNG_FP_End;
|
|
2754 |
|
|
2755 |
png_fp_set(state, PNG_FP_EXPONENT);
|
|
2756 |
|
|
2757 |
break;
|
|
2758 |
|
|
2759 |
case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN:
|
29913
|
2760 |
if ((state & PNG_FP_SAW_ANY) != 0)
|
10576
|
2761 |
goto PNG_FP_End; /* not a part of the number */
|
|
2762 |
|
|
2763 |
png_fp_add(state, PNG_FP_SAW_SIGN);
|
|
2764 |
|
|
2765 |
break;
|
|
2766 |
|
|
2767 |
/* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT:
|
|
2768 |
goto PNG_FP_End; */
|
|
2769 |
|
|
2770 |
case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT:
|
|
2771 |
png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID);
|
|
2772 |
|
|
2773 |
break;
|
|
2774 |
|
|
2775 |
/* case PNG_FP_EXPONEXT + PNG_FP_SAW_E:
|
|
2776 |
goto PNG_FP_End; */
|
|
2777 |
|
|
2778 |
default: goto PNG_FP_End; /* I.e. break 2 */
|
|
2779 |
}
|
|
2780 |
|
|
2781 |
/* The character seems ok, continue. */
|
|
2782 |
++i;
|
|
2783 |
}
|
|
2784 |
|
|
2785 |
PNG_FP_End:
|
|
2786 |
/* Here at the end, update the state and return the correct
|
|
2787 |
* return code.
|
|
2788 |
*/
|
|
2789 |
*statep = state;
|
|
2790 |
*whereami = i;
|
|
2791 |
|
|
2792 |
return (state & PNG_FP_SAW_DIGIT) != 0;
|
|
2793 |
}
|
|
2794 |
|
|
2795 |
|
|
2796 |
/* The same but for a complete string. */
|
|
2797 |
int
|
|
2798 |
png_check_fp_string(png_const_charp string, png_size_t size)
|
|
2799 |
{
|
|
2800 |
int state=0;
|
|
2801 |
png_size_t char_index=0;
|
|
2802 |
|
29913
|
2803 |
if (png_check_fp_number(string, size, &state, &char_index) != 0 &&
|
10576
|
2804 |
(char_index == size || string[char_index] == 0))
|
|
2805 |
return state /* must be non-zero - see above */;
|
|
2806 |
|
|
2807 |
return 0; /* i.e. fail */
|
|
2808 |
}
|
29913
|
2809 |
#endif /* pCAL || sCAL */
|
|
2810 |
|
|
2811 |
#ifdef PNG_sCAL_SUPPORTED
|
10576
|
2812 |
# ifdef PNG_FLOATING_POINT_SUPPORTED
|
|
2813 |
/* Utility used below - a simple accurate power of ten from an integral
|
|
2814 |
* exponent.
|
|
2815 |
*/
|
|
2816 |
static double
|
|
2817 |
png_pow10(int power)
|
|
2818 |
{
|
|
2819 |
int recip = 0;
|
|
2820 |
double d = 1;
|
|
2821 |
|
|
2822 |
/* Handle negative exponent with a reciprocal at the end because
|
|
2823 |
* 10 is exact whereas .1 is inexact in base 2
|
|
2824 |
*/
|
|
2825 |
if (power < 0)
|
|
2826 |
{
|
|
2827 |
if (power < DBL_MIN_10_EXP) return 0;
|
|
2828 |
recip = 1, power = -power;
|
|
2829 |
}
|
|
2830 |
|
|
2831 |
if (power > 0)
|
|
2832 |
{
|
|
2833 |
/* Decompose power bitwise. */
|
|
2834 |
double mult = 10;
|
|
2835 |
do
|
|
2836 |
{
|
|
2837 |
if (power & 1) d *= mult;
|
|
2838 |
mult *= mult;
|
|
2839 |
power >>= 1;
|
|
2840 |
}
|
|
2841 |
while (power > 0);
|
|
2842 |
|
29913
|
2843 |
if (recip != 0) d = 1/d;
|
10576
|
2844 |
}
|
|
2845 |
/* else power is 0 and d is 1 */
|
|
2846 |
|
|
2847 |
return d;
|
|
2848 |
}
|
|
2849 |
|
|
2850 |
/* Function to format a floating point value in ASCII with a given
|
|
2851 |
* precision.
|
|
2852 |
*/
|
|
2853 |
void /* PRIVATE */
|
29913
|
2854 |
png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, png_size_t size,
|
10576
|
2855 |
double fp, unsigned int precision)
|
|
2856 |
{
|
|
2857 |
/* We use standard functions from math.h, but not printf because
|
|
2858 |
* that would require stdio. The caller must supply a buffer of
|
|
2859 |
* sufficient size or we will png_error. The tests on size and
|
|
2860 |
* the space in ascii[] consumed are indicated below.
|
|
2861 |
*/
|
|
2862 |
if (precision < 1)
|
|
2863 |
precision = DBL_DIG;
|
|
2864 |
|
|
2865 |
/* Enforce the limit of the implementation precision too. */
|
|
2866 |
if (precision > DBL_DIG+1)
|
|
2867 |
precision = DBL_DIG+1;
|
|
2868 |
|
|
2869 |
/* Basic sanity checks */
|
|
2870 |
if (size >= precision+5) /* See the requirements below. */
|
|
2871 |
{
|
|
2872 |
if (fp < 0)
|
|
2873 |
{
|
|
2874 |
fp = -fp;
|
|
2875 |
*ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */
|
|
2876 |
--size;
|
|
2877 |
}
|
|
2878 |
|
|
2879 |
if (fp >= DBL_MIN && fp <= DBL_MAX)
|
|
2880 |
{
|
|
2881 |
int exp_b10; /* A base 10 exponent */
|
|
2882 |
double base; /* 10^exp_b10 */
|
|
2883 |
|
|
2884 |
/* First extract a base 10 exponent of the number,
|
|
2885 |
* the calculation below rounds down when converting
|
|
2886 |
* from base 2 to base 10 (multiply by log10(2) -
|
|
2887 |
* 0.3010, but 77/256 is 0.3008, so exp_b10 needs to
|
|
2888 |
* be increased. Note that the arithmetic shift
|
|
2889 |
* performs a floor() unlike C arithmetic - using a
|
|
2890 |
* C multiply would break the following for negative
|
|
2891 |
* exponents.
|
|
2892 |
*/
|
|
2893 |
(void)frexp(fp, &exp_b10); /* exponent to base 2 */
|
|
2894 |
|
|
2895 |
exp_b10 = (exp_b10 * 77) >> 8; /* <= exponent to base 10 */
|
|
2896 |
|
|
2897 |
/* Avoid underflow here. */
|
|
2898 |
base = png_pow10(exp_b10); /* May underflow */
|
|
2899 |
|
|
2900 |
while (base < DBL_MIN || base < fp)
|
|
2901 |
{
|
|
2902 |
/* And this may overflow. */
|
|
2903 |
double test = png_pow10(exp_b10+1);
|
|
2904 |
|
|
2905 |
if (test <= DBL_MAX)
|
|
2906 |
++exp_b10, base = test;
|
|
2907 |
|
|
2908 |
else
|
|
2909 |
break;
|
|
2910 |
}
|
|
2911 |
|
|
2912 |
/* Normalize fp and correct exp_b10, after this fp is in the
|
|
2913 |
* range [.1,1) and exp_b10 is both the exponent and the digit
|
|
2914 |
* *before* which the decimal point should be inserted
|
|
2915 |
* (starting with 0 for the first digit). Note that this
|
|
2916 |
* works even if 10^exp_b10 is out of range because of the
|
|
2917 |
* test on DBL_MAX above.
|
|
2918 |
*/
|
|
2919 |
fp /= base;
|
|
2920 |
while (fp >= 1) fp /= 10, ++exp_b10;
|
|
2921 |
|
|
2922 |
/* Because of the code above fp may, at this point, be
|
|
2923 |
* less than .1, this is ok because the code below can
|
|
2924 |
* handle the leading zeros this generates, so no attempt
|
|
2925 |
* is made to correct that here.
|
|
2926 |
*/
|
|
2927 |
|
|
2928 |
{
|
|
2929 |
int czero, clead, cdigits;
|
|
2930 |
char exponent[10];
|
|
2931 |
|
|
2932 |
/* Allow up to two leading zeros - this will not lengthen
|
|
2933 |
* the number compared to using E-n.
|
|
2934 |
*/
|
|
2935 |
if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */
|
|
2936 |
{
|
|
2937 |
czero = -exp_b10; /* PLUS 2 digits: TOTAL 3 */
|
|
2938 |
exp_b10 = 0; /* Dot added below before first output. */
|
|
2939 |
}
|
|
2940 |
else
|
|
2941 |
czero = 0; /* No zeros to add */
|
|
2942 |
|
|
2943 |
/* Generate the digit list, stripping trailing zeros and
|
|
2944 |
* inserting a '.' before a digit if the exponent is 0.
|
|
2945 |
*/
|
|
2946 |
clead = czero; /* Count of leading zeros */
|
|
2947 |
cdigits = 0; /* Count of digits in list. */
|
|
2948 |
|
|
2949 |
do
|
|
2950 |
{
|
|
2951 |
double d;
|
|
2952 |
|
|
2953 |
fp *= 10;
|
|
2954 |
/* Use modf here, not floor and subtract, so that
|
|
2955 |
* the separation is done in one step. At the end
|
|
2956 |
* of the loop don't break the number into parts so
|
|
2957 |
* that the final digit is rounded.
|
|
2958 |
*/
|
|
2959 |
if (cdigits+czero-clead+1 < (int)precision)
|
|
2960 |
fp = modf(fp, &d);
|
|
2961 |
|
|
2962 |
else
|
|
2963 |
{
|
|
2964 |
d = floor(fp + .5);
|
|
2965 |
|
|
2966 |
if (d > 9)
|
|
2967 |
{
|
|
2968 |
/* Rounding up to 10, handle that here. */
|
|
2969 |
if (czero > 0)
|
|
2970 |
{
|
|
2971 |
--czero, d = 1;
|
|
2972 |
if (cdigits == 0) --clead;
|
|
2973 |
}
|
|
2974 |
else
|
|
2975 |
{
|
|
2976 |
while (cdigits > 0 && d > 9)
|
|
2977 |
{
|
|
2978 |
int ch = *--ascii;
|
|
2979 |
|
|
2980 |
if (exp_b10 != (-1))
|
|
2981 |
++exp_b10;
|
|
2982 |
|
|
2983 |
else if (ch == 46)
|
|
2984 |
{
|
|
2985 |
ch = *--ascii, ++size;
|
|
2986 |
/* Advance exp_b10 to '1', so that the
|
|
2987 |
* decimal point happens after the
|
|
2988 |
* previous digit.
|
|
2989 |
*/
|
|
2990 |
exp_b10 = 1;
|
|
2991 |
}
|
|
2992 |
|
|
2993 |
--cdigits;
|
|
2994 |
d = ch - 47; /* I.e. 1+(ch-48) */
|
|
2995 |
}
|
|
2996 |
|
|
2997 |
/* Did we reach the beginning? If so adjust the
|
|
2998 |
* exponent but take into account the leading
|
|
2999 |
* decimal point.
|
|
3000 |
*/
|
|
3001 |
if (d > 9) /* cdigits == 0 */
|
|
3002 |
{
|
|
3003 |
if (exp_b10 == (-1))
|
|
3004 |
{
|
|
3005 |
/* Leading decimal point (plus zeros?), if
|
|
3006 |
* we lose the decimal point here it must
|
|
3007 |
* be reentered below.
|
|
3008 |
*/
|
|
3009 |
int ch = *--ascii;
|
|
3010 |
|
|
3011 |
if (ch == 46)
|
|
3012 |
++size, exp_b10 = 1;
|
|
3013 |
|
|
3014 |
/* Else lost a leading zero, so 'exp_b10' is
|
|
3015 |
* still ok at (-1)
|
|
3016 |
*/
|
|
3017 |
}
|
|
3018 |
else
|
|
3019 |
++exp_b10;
|
|
3020 |
|
|
3021 |
/* In all cases we output a '1' */
|
|
3022 |
d = 1;
|
|
3023 |
}
|
|
3024 |
}
|
|
3025 |
}
|
|
3026 |
fp = 0; /* Guarantees termination below. */
|
|
3027 |
}
|
|
3028 |
|
|
3029 |
if (d == 0)
|
|
3030 |
{
|
|
3031 |
++czero;
|
|
3032 |
if (cdigits == 0) ++clead;
|
|
3033 |
}
|
|
3034 |
else
|
|
3035 |
{
|
|
3036 |
/* Included embedded zeros in the digit count. */
|
|
3037 |
cdigits += czero - clead;
|
|
3038 |
clead = 0;
|
|
3039 |
|
|
3040 |
while (czero > 0)
|
|
3041 |
{
|
|
3042 |
/* exp_b10 == (-1) means we just output the decimal
|
|
3043 |
* place - after the DP don't adjust 'exp_b10' any
|
|
3044 |
* more!
|
|
3045 |
*/
|
|
3046 |
if (exp_b10 != (-1))
|
|
3047 |
{
|
|
3048 |
if (exp_b10 == 0) *ascii++ = 46, --size;
|
|
3049 |
/* PLUS 1: TOTAL 4 */
|
|
3050 |
--exp_b10;
|
|
3051 |
}
|
|
3052 |
*ascii++ = 48, --czero;
|
|
3053 |
}
|
|
3054 |
|
|
3055 |
if (exp_b10 != (-1))
|
|
3056 |
{
|
29913
|
3057 |
if (exp_b10 == 0)
|
|
3058 |
*ascii++ = 46, --size; /* counted above */
|
|
3059 |
|
10576
|
3060 |
--exp_b10;
|
|
3061 |
}
|
|
3062 |
*ascii++ = (char)(48 + (int)d), ++cdigits;
|
|
3063 |
}
|
|
3064 |
}
|
|
3065 |
while (cdigits+czero-clead < (int)precision && fp > DBL_MIN);
|
|
3066 |
|
|
3067 |
/* The total output count (max) is now 4+precision */
|
|
3068 |
|
|
3069 |
/* Check for an exponent, if we don't need one we are
|
|
3070 |
* done and just need to terminate the string. At
|
|
3071 |
* this point exp_b10==(-1) is effectively if flag - it got
|
|
3072 |
* to '-1' because of the decrement after outputing
|
|
3073 |
* the decimal point above (the exponent required is
|
|
3074 |
* *not* -1!)
|
|
3075 |
*/
|
|
3076 |
if (exp_b10 >= (-1) && exp_b10 <= 2)
|
|
3077 |
{
|
|
3078 |
/* The following only happens if we didn't output the
|
|
3079 |
* leading zeros above for negative exponent, so this
|
|
3080 |
* doest add to the digit requirement. Note that the
|
|
3081 |
* two zeros here can only be output if the two leading
|
|
3082 |
* zeros were *not* output, so this doesn't increase
|
|
3083 |
* the output count.
|
|
3084 |
*/
|
|
3085 |
while (--exp_b10 >= 0) *ascii++ = 48;
|
|
3086 |
|
|
3087 |
*ascii = 0;
|
|
3088 |
|
|
3089 |
/* Total buffer requirement (including the '\0') is
|
|
3090 |
* 5+precision - see check at the start.
|
|
3091 |
*/
|
|
3092 |
return;
|
|
3093 |
}
|
|
3094 |
|
|
3095 |
/* Here if an exponent is required, adjust size for
|
|
3096 |
* the digits we output but did not count. The total
|
|
3097 |
* digit output here so far is at most 1+precision - no
|
|
3098 |
* decimal point and no leading or trailing zeros have
|
|
3099 |
* been output.
|
|
3100 |
*/
|
|
3101 |
size -= cdigits;
|
|
3102 |
|
|
3103 |
*ascii++ = 69, --size; /* 'E': PLUS 1 TOTAL 2+precision */
|
29913
|
3104 |
|
|
3105 |
/* The following use of an unsigned temporary avoids ambiguities in
|
|
3106 |
* the signed arithmetic on exp_b10 and permits GCC at least to do
|
|
3107 |
* better optimization.
|
|
3108 |
*/
|
10576
|
3109 |
{
|
29913
|
3110 |
unsigned int uexp_b10;
|
|
3111 |
|
|
3112 |
if (exp_b10 < 0)
|
|
3113 |
{
|
|
3114 |
*ascii++ = 45, --size; /* '-': PLUS 1 TOTAL 3+precision */
|
|
3115 |
uexp_b10 = -exp_b10;
|
|
3116 |
}
|
|
3117 |
|
|
3118 |
else
|
|
3119 |
uexp_b10 = exp_b10;
|
|
3120 |
|
|
3121 |
cdigits = 0;
|
|
3122 |
|
|
3123 |
while (uexp_b10 > 0)
|
|
3124 |
{
|
|
3125 |
exponent[cdigits++] = (char)(48 + uexp_b10 % 10);
|
|
3126 |
uexp_b10 /= 10;
|
|
3127 |
}
|
10576
|
3128 |
}
|
|
3129 |
|
|
3130 |
/* Need another size check here for the exponent digits, so
|
|
3131 |
* this need not be considered above.
|
|
3132 |
*/
|
|
3133 |
if ((int)size > cdigits)
|
|
3134 |
{
|
|
3135 |
while (cdigits > 0) *ascii++ = exponent[--cdigits];
|
|
3136 |
|
|
3137 |
*ascii = 0;
|
|
3138 |
|
|
3139 |
return;
|
|
3140 |
}
|
|
3141 |
}
|
|
3142 |
}
|
|
3143 |
else if (!(fp >= DBL_MIN))
|
|
3144 |
{
|
|
3145 |
*ascii++ = 48; /* '0' */
|
|
3146 |
*ascii = 0;
|
|
3147 |
return;
|
|
3148 |
}
|
|
3149 |
else
|
|
3150 |
{
|
|
3151 |
*ascii++ = 105; /* 'i' */
|
|
3152 |
*ascii++ = 110; /* 'n' */
|
|
3153 |
*ascii++ = 102; /* 'f' */
|
|
3154 |
*ascii = 0;
|
|
3155 |
return;
|
|
3156 |
}
|
|
3157 |
}
|
|
3158 |
|
|
3159 |
/* Here on buffer too small. */
|
|
3160 |
png_error(png_ptr, "ASCII conversion buffer too small");
|
|
3161 |
}
|
|
3162 |
|
|
3163 |
# endif /* FLOATING_POINT */
|
|
3164 |
|
|
3165 |
# ifdef PNG_FIXED_POINT_SUPPORTED
|
|
3166 |
/* Function to format a fixed point value in ASCII.
|
|
3167 |
*/
|
|
3168 |
void /* PRIVATE */
|
29913
|
3169 |
png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii,
|
|
3170 |
png_size_t size, png_fixed_point fp)
|
10576
|
3171 |
{
|
|
3172 |
/* Require space for 10 decimal digits, a decimal point, a minus sign and a
|
|
3173 |
* trailing \0, 13 characters:
|
|
3174 |
*/
|
|
3175 |
if (size > 12)
|
|
3176 |
{
|
|
3177 |
png_uint_32 num;
|
|
3178 |
|
|
3179 |
/* Avoid overflow here on the minimum integer. */
|
|
3180 |
if (fp < 0)
|
|
3181 |
*ascii++ = 45, --size, num = -fp;
|
|
3182 |
else
|
|
3183 |
num = fp;
|
|
3184 |
|
29913
|
3185 |
if (num <= 0x80000000) /* else overflowed */
|
10576
|
3186 |
{
|
|
3187 |
unsigned int ndigits = 0, first = 16 /* flag value */;
|
|
3188 |
char digits[10];
|
|
3189 |
|
|
3190 |
while (num)
|
|
3191 |
{
|
|
3192 |
/* Split the low digit off num: */
|
|
3193 |
unsigned int tmp = num/10;
|
|
3194 |
num -= tmp*10;
|
|
3195 |
digits[ndigits++] = (char)(48 + num);
|
|
3196 |
/* Record the first non-zero digit, note that this is a number
|
|
3197 |
* starting at 1, it's not actually the array index.
|
|
3198 |
*/
|
|
3199 |
if (first == 16 && num > 0)
|
|
3200 |
first = ndigits;
|
|
3201 |
num = tmp;
|
|
3202 |
}
|
|
3203 |
|
|
3204 |
if (ndigits > 0)
|
|
3205 |
{
|
|
3206 |
while (ndigits > 5) *ascii++ = digits[--ndigits];
|
|
3207 |
/* The remaining digits are fractional digits, ndigits is '5' or
|
|
3208 |
* smaller at this point. It is certainly not zero. Check for a
|
|
3209 |
* non-zero fractional digit:
|
|
3210 |
*/
|
|
3211 |
if (first <= 5)
|
|
3212 |
{
|
|
3213 |
unsigned int i;
|
|
3214 |
*ascii++ = 46; /* decimal point */
|
|
3215 |
/* ndigits may be <5 for small numbers, output leading zeros
|
|
3216 |
* then ndigits digits to first:
|
|
3217 |
*/
|
|
3218 |
i = 5;
|
|
3219 |
while (ndigits < i) *ascii++ = 48, --i;
|
|
3220 |
while (ndigits >= first) *ascii++ = digits[--ndigits];
|
|
3221 |
/* Don't output the trailing zeros! */
|
|
3222 |
}
|
|
3223 |
}
|
|
3224 |
else
|
|
3225 |
*ascii++ = 48;
|
|
3226 |
|
|
3227 |
/* And null terminate the string: */
|
|
3228 |
*ascii = 0;
|
|
3229 |
return;
|
|
3230 |
}
|
|
3231 |
}
|
|
3232 |
|
|
3233 |
/* Here on buffer too small. */
|
|
3234 |
png_error(png_ptr, "ASCII conversion buffer too small");
|
|
3235 |
}
|
|
3236 |
# endif /* FIXED_POINT */
|
|
3237 |
#endif /* READ_SCAL */
|
|
3238 |
|
|
3239 |
#if defined(PNG_FLOATING_POINT_SUPPORTED) && \
|
29913
|
3240 |
!defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \
|
|
3241 |
(defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \
|
|
3242 |
defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \
|
|
3243 |
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \
|
|
3244 |
(defined(PNG_sCAL_SUPPORTED) && \
|
|
3245 |
defined(PNG_FLOATING_ARITHMETIC_SUPPORTED))
|
10576
|
3246 |
png_fixed_point
|
29913
|
3247 |
png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text)
|
10576
|
3248 |
{
|
|
3249 |
double r = floor(100000 * fp + .5);
|
|
3250 |
|
|
3251 |
if (r > 2147483647. || r < -2147483648.)
|
|
3252 |
png_fixed_error(png_ptr, text);
|
|
3253 |
|
29913
|
3254 |
# ifndef PNG_ERROR_TEXT_SUPPORTED
|
|
3255 |
PNG_UNUSED(text)
|
|
3256 |
# endif
|
|
3257 |
|
10576
|
3258 |
return (png_fixed_point)r;
|
2
|
3259 |
}
|
10576
|
3260 |
#endif
|
|
3261 |
|
29913
|
3262 |
#if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_COLORSPACE_SUPPORTED) ||\
|
|
3263 |
defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED)
|
10576
|
3264 |
/* muldiv functions */
|
|
3265 |
/* This API takes signed arguments and rounds the result to the nearest
|
|
3266 |
* integer (or, for a fixed point number - the standard argument - to
|
|
3267 |
* the nearest .00001). Overflow and divide by zero are signalled in
|
|
3268 |
* the result, a boolean - true on success, false on overflow.
|
|
3269 |
*/
|
|
3270 |
int
|
|
3271 |
png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times,
|
|
3272 |
png_int_32 divisor)
|
|
3273 |
{
|
|
3274 |
/* Return a * times / divisor, rounded. */
|
|
3275 |
if (divisor != 0)
|
|
3276 |
{
|
|
3277 |
if (a == 0 || times == 0)
|
|
3278 |
{
|
|
3279 |
*res = 0;
|
|
3280 |
return 1;
|
|
3281 |
}
|
|
3282 |
else
|
|
3283 |
{
|
|
3284 |
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3285 |
double r = a;
|
|
3286 |
r *= times;
|
|
3287 |
r /= divisor;
|
|
3288 |
r = floor(r+.5);
|
|
3289 |
|
|
3290 |
/* A png_fixed_point is a 32-bit integer. */
|
|
3291 |
if (r <= 2147483647. && r >= -2147483648.)
|
|
3292 |
{
|
|
3293 |
*res = (png_fixed_point)r;
|
|
3294 |
return 1;
|
|
3295 |
}
|
|
3296 |
#else
|
|
3297 |
int negative = 0;
|
|
3298 |
png_uint_32 A, T, D;
|
|
3299 |
png_uint_32 s16, s32, s00;
|
|
3300 |
|
|
3301 |
if (a < 0)
|
|
3302 |
negative = 1, A = -a;
|
|
3303 |
else
|
|
3304 |
A = a;
|
|
3305 |
|
|
3306 |
if (times < 0)
|
|
3307 |
negative = !negative, T = -times;
|
|
3308 |
else
|
|
3309 |
T = times;
|
|
3310 |
|
|
3311 |
if (divisor < 0)
|
|
3312 |
negative = !negative, D = -divisor;
|
|
3313 |
else
|
|
3314 |
D = divisor;
|
|
3315 |
|
|
3316 |
/* Following can't overflow because the arguments only
|
|
3317 |
* have 31 bits each, however the result may be 32 bits.
|
|
3318 |
*/
|
|
3319 |
s16 = (A >> 16) * (T & 0xffff) +
|
|
3320 |
(A & 0xffff) * (T >> 16);
|
|
3321 |
/* Can't overflow because the a*times bit is only 30
|
|
3322 |
* bits at most.
|
|
3323 |
*/
|
|
3324 |
s32 = (A >> 16) * (T >> 16) + (s16 >> 16);
|
|
3325 |
s00 = (A & 0xffff) * (T & 0xffff);
|
|
3326 |
|
|
3327 |
s16 = (s16 & 0xffff) << 16;
|
|
3328 |
s00 += s16;
|
|
3329 |
|
|
3330 |
if (s00 < s16)
|
|
3331 |
++s32; /* carry */
|
|
3332 |
|
|
3333 |
if (s32 < D) /* else overflow */
|
|
3334 |
{
|
|
3335 |
/* s32.s00 is now the 64-bit product, do a standard
|
|
3336 |
* division, we know that s32 < D, so the maximum
|
|
3337 |
* required shift is 31.
|
|
3338 |
*/
|
|
3339 |
int bitshift = 32;
|
|
3340 |
png_fixed_point result = 0; /* NOTE: signed */
|
|
3341 |
|
|
3342 |
while (--bitshift >= 0)
|
|
3343 |
{
|
|
3344 |
png_uint_32 d32, d00;
|
|
3345 |
|
|
3346 |
if (bitshift > 0)
|
|
3347 |
d32 = D >> (32-bitshift), d00 = D << bitshift;
|
|
3348 |
|
|
3349 |
else
|
|
3350 |
d32 = 0, d00 = D;
|
|
3351 |
|
|
3352 |
if (s32 > d32)
|
|
3353 |
{
|
|
3354 |
if (s00 < d00) --s32; /* carry */
|
|
3355 |
s32 -= d32, s00 -= d00, result += 1<<bitshift;
|
|
3356 |
}
|
|
3357 |
|
|
3358 |
else
|
|
3359 |
if (s32 == d32 && s00 >= d00)
|
|
3360 |
s32 = 0, s00 -= d00, result += 1<<bitshift;
|
|
3361 |
}
|
|
3362 |
|
|
3363 |
/* Handle the rounding. */
|
|
3364 |
if (s00 >= (D >> 1))
|
|
3365 |
++result;
|
|
3366 |
|
29913
|
3367 |
if (negative != 0)
|
10576
|
3368 |
result = -result;
|
|
3369 |
|
|
3370 |
/* Check for overflow. */
|
29913
|
3371 |
if ((negative != 0 && result <= 0) ||
|
|
3372 |
(negative == 0 && result >= 0))
|
10576
|
3373 |
{
|
|
3374 |
*res = result;
|
|
3375 |
return 1;
|
|
3376 |
}
|
|
3377 |
}
|
|
3378 |
#endif
|
|
3379 |
}
|
|
3380 |
}
|
|
3381 |
|
|
3382 |
return 0;
|
|
3383 |
}
|
|
3384 |
#endif /* READ_GAMMA || INCH_CONVERSIONS */
|
|
3385 |
|
|
3386 |
#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED)
|
|
3387 |
/* The following is for when the caller doesn't much care about the
|
|
3388 |
* result.
|
|
3389 |
*/
|
|
3390 |
png_fixed_point
|
29913
|
3391 |
png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times,
|
10576
|
3392 |
png_int_32 divisor)
|
|
3393 |
{
|
|
3394 |
png_fixed_point result;
|
|
3395 |
|
29913
|
3396 |
if (png_muldiv(&result, a, times, divisor) != 0)
|
10576
|
3397 |
return result;
|
|
3398 |
|
|
3399 |
png_warning(png_ptr, "fixed point overflow ignored");
|
|
3400 |
return 0;
|
|
3401 |
}
|
|
3402 |
#endif
|
|
3403 |
|
29913
|
3404 |
#ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */
|
10576
|
3405 |
/* Calculate a reciprocal, return 0 on div-by-zero or overflow. */
|
|
3406 |
png_fixed_point
|
|
3407 |
png_reciprocal(png_fixed_point a)
|
|
3408 |
{
|
|
3409 |
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3410 |
double r = floor(1E10/a+.5);
|
|
3411 |
|
|
3412 |
if (r <= 2147483647. && r >= -2147483648.)
|
|
3413 |
return (png_fixed_point)r;
|
|
3414 |
#else
|
|
3415 |
png_fixed_point res;
|
|
3416 |
|
29913
|
3417 |
if (png_muldiv(&res, 100000, 100000, a) != 0)
|
10576
|
3418 |
return res;
|
|
3419 |
#endif
|
|
3420 |
|
|
3421 |
return 0; /* error/overflow */
|
|
3422 |
}
|
|
3423 |
|
29913
|
3424 |
/* This is the shared test on whether a gamma value is 'significant' - whether
|
|
3425 |
* it is worth doing gamma correction.
|
|
3426 |
*/
|
|
3427 |
int /* PRIVATE */
|
|
3428 |
png_gamma_significant(png_fixed_point gamma_val)
|
|
3429 |
{
|
|
3430 |
return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
|
|
3431 |
gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
|
|
3432 |
}
|
|
3433 |
#endif
|
|
3434 |
|
|
3435 |
#ifdef PNG_READ_GAMMA_SUPPORTED
|
|
3436 |
#if defined(PNG_16BIT_SUPPORTED) || !defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)
|
10576
|
3437 |
/* A local convenience routine. */
|
|
3438 |
static png_fixed_point
|
|
3439 |
png_product2(png_fixed_point a, png_fixed_point b)
|
|
3440 |
{
|
|
3441 |
/* The required result is 1/a * 1/b; the following preserves accuracy. */
|
29913
|
3442 |
# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
10576
|
3443 |
double r = a * 1E-5;
|
|
3444 |
r *= b;
|
|
3445 |
r = floor(r+.5);
|
|
3446 |
|
|
3447 |
if (r <= 2147483647. && r >= -2147483648.)
|
|
3448 |
return (png_fixed_point)r;
|
29913
|
3449 |
# else
|
10576
|
3450 |
png_fixed_point res;
|
|
3451 |
|
29913
|
3452 |
if (png_muldiv(&res, a, b, 100000) != 0)
|
10576
|
3453 |
return res;
|
29913
|
3454 |
# endif
|
10576
|
3455 |
|
|
3456 |
return 0; /* overflow */
|
|
3457 |
}
|
29913
|
3458 |
#endif /* 16BIT || !FLOATING_ARITHMETIC */
|
10576
|
3459 |
|
|
3460 |
/* The inverse of the above. */
|
|
3461 |
png_fixed_point
|
|
3462 |
png_reciprocal2(png_fixed_point a, png_fixed_point b)
|
|
3463 |
{
|
|
3464 |
/* The required result is 1/a * 1/b; the following preserves accuracy. */
|
|
3465 |
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3466 |
double r = 1E15/a;
|
|
3467 |
r /= b;
|
|
3468 |
r = floor(r+.5);
|
|
3469 |
|
|
3470 |
if (r <= 2147483647. && r >= -2147483648.)
|
|
3471 |
return (png_fixed_point)r;
|
|
3472 |
#else
|
|
3473 |
/* This may overflow because the range of png_fixed_point isn't symmetric,
|
|
3474 |
* but this API is only used for the product of file and screen gamma so it
|
|
3475 |
* doesn't matter that the smallest number it can produce is 1/21474, not
|
|
3476 |
* 1/100000
|
|
3477 |
*/
|
|
3478 |
png_fixed_point res = png_product2(a, b);
|
|
3479 |
|
|
3480 |
if (res != 0)
|
|
3481 |
return png_reciprocal(res);
|
|
3482 |
#endif
|
|
3483 |
|
|
3484 |
return 0; /* overflow */
|
|
3485 |
}
|
|
3486 |
#endif /* READ_GAMMA */
|
|
3487 |
|
|
3488 |
#ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */
|
|
3489 |
#ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3490 |
/* Fixed point gamma.
|
|
3491 |
*
|
29913
|
3492 |
* The code to calculate the tables used below can be found in the shell script
|
|
3493 |
* contrib/tools/intgamma.sh
|
|
3494 |
*
|
10576
|
3495 |
* To calculate gamma this code implements fast log() and exp() calls using only
|
|
3496 |
* fixed point arithmetic. This code has sufficient precision for either 8-bit
|
|
3497 |
* or 16-bit sample values.
|
|
3498 |
*
|
|
3499 |
* The tables used here were calculated using simple 'bc' programs, but C double
|
29913
|
3500 |
* precision floating point arithmetic would work fine.
|
10576
|
3501 |
*
|
|
3502 |
* 8-bit log table
|
|
3503 |
* This is a table of -log(value/255)/log(2) for 'value' in the range 128 to
|
|
3504 |
* 255, so it's the base 2 logarithm of a normalized 8-bit floating point
|
|
3505 |
* mantissa. The numbers are 32-bit fractions.
|
|
3506 |
*/
|
29913
|
3507 |
static const png_uint_32
|
10576
|
3508 |
png_8bit_l2[128] =
|
|
3509 |
{
|
|
3510 |
4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U,
|
|
3511 |
3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U,
|
|
3512 |
3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U,
|
|
3513 |
3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U,
|
|
3514 |
3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U,
|
|
3515 |
2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U,
|
|
3516 |
2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U,
|
|
3517 |
2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U,
|
|
3518 |
2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U,
|
|
3519 |
2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U,
|
|
3520 |
1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U,
|
|
3521 |
1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U,
|
|
3522 |
1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U,
|
|
3523 |
1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U,
|
|
3524 |
1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U,
|
|
3525 |
971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U,
|
|
3526 |
803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U,
|
|
3527 |
639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U,
|
|
3528 |
479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U,
|
|
3529 |
324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U,
|
|
3530 |
172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U,
|
|
3531 |
24347096U, 0U
|
29913
|
3532 |
|
10576
|
3533 |
#if 0
|
|
3534 |
/* The following are the values for 16-bit tables - these work fine for the
|
|
3535 |
* 8-bit conversions but produce very slightly larger errors in the 16-bit
|
|
3536 |
* log (about 1.2 as opposed to 0.7 absolute error in the final value). To
|
|
3537 |
* use these all the shifts below must be adjusted appropriately.
|
|
3538 |
*/
|
|
3539 |
65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054,
|
|
3540 |
57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803,
|
|
3541 |
50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068,
|
|
3542 |
43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782,
|
|
3543 |
37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887,
|
|
3544 |
31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339,
|
|
3545 |
25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098,
|
|
3546 |
20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132,
|
|
3547 |
15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415,
|
|
3548 |
10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523,
|
|
3549 |
6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495,
|
|
3550 |
1119, 744, 372
|
|
3551 |
#endif
|
|
3552 |
};
|
|
3553 |
|
29913
|
3554 |
static png_int_32
|
10576
|
3555 |
png_log8bit(unsigned int x)
|
|
3556 |
{
|
|
3557 |
unsigned int lg2 = 0;
|
|
3558 |
/* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log,
|
|
3559 |
* because the log is actually negate that means adding 1. The final
|
|
3560 |
* returned value thus has the range 0 (for 255 input) to 7.994 (for 1
|
29913
|
3561 |
* input), return -1 for the overflow (log 0) case, - so the result is
|
10576
|
3562 |
* always at most 19 bits.
|
|
3563 |
*/
|
|
3564 |
if ((x &= 0xff) == 0)
|
29913
|
3565 |
return -1;
|
10576
|
3566 |
|
|
3567 |
if ((x & 0xf0) == 0)
|
|
3568 |
lg2 = 4, x <<= 4;
|
|
3569 |
|
|
3570 |
if ((x & 0xc0) == 0)
|
|
3571 |
lg2 += 2, x <<= 2;
|
|
3572 |
|
|
3573 |
if ((x & 0x80) == 0)
|
|
3574 |
lg2 += 1, x <<= 1;
|
|
3575 |
|
|
3576 |
/* result is at most 19 bits, so this cast is safe: */
|
|
3577 |
return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16));
|
|
3578 |
}
|
|
3579 |
|
|
3580 |
/* The above gives exact (to 16 binary places) log2 values for 8-bit images,
|
|
3581 |
* for 16-bit images we use the most significant 8 bits of the 16-bit value to
|
|
3582 |
* get an approximation then multiply the approximation by a correction factor
|
|
3583 |
* determined by the remaining up to 8 bits. This requires an additional step
|
|
3584 |
* in the 16-bit case.
|
|
3585 |
*
|
|
3586 |
* We want log2(value/65535), we have log2(v'/255), where:
|
|
3587 |
*
|
|
3588 |
* value = v' * 256 + v''
|
|
3589 |
* = v' * f
|
|
3590 |
*
|
|
3591 |
* So f is value/v', which is equal to (256+v''/v') since v' is in the range 128
|
|
3592 |
* to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less
|
|
3593 |
* than 258. The final factor also needs to correct for the fact that our 8-bit
|
|
3594 |
* value is scaled by 255, whereas the 16-bit values must be scaled by 65535.
|
|
3595 |
*
|
|
3596 |
* This gives a final formula using a calculated value 'x' which is value/v' and
|
|
3597 |
* scaling by 65536 to match the above table:
|
|
3598 |
*
|
|
3599 |
* log2(x/257) * 65536
|
|
3600 |
*
|
|
3601 |
* Since these numbers are so close to '1' we can use simple linear
|
|
3602 |
* interpolation between the two end values 256/257 (result -368.61) and 258/257
|
|
3603 |
* (result 367.179). The values used below are scaled by a further 64 to give
|
|
3604 |
* 16-bit precision in the interpolation:
|
|
3605 |
*
|
|
3606 |
* Start (256): -23591
|
|
3607 |
* Zero (257): 0
|
|
3608 |
* End (258): 23499
|
|
3609 |
*/
|
29913
|
3610 |
#ifdef PNG_16BIT_SUPPORTED
|
|
3611 |
static png_int_32
|
10576
|
3612 |
png_log16bit(png_uint_32 x)
|
|
3613 |
{
|
|
3614 |
unsigned int lg2 = 0;
|
|
3615 |
|
|
3616 |
/* As above, but now the input has 16 bits. */
|
|
3617 |
if ((x &= 0xffff) == 0)
|
29913
|
3618 |
return -1;
|
10576
|
3619 |
|
|
3620 |
if ((x & 0xff00) == 0)
|
|
3621 |
lg2 = 8, x <<= 8;
|
|
3622 |
|
|
3623 |
if ((x & 0xf000) == 0)
|
|
3624 |
lg2 += 4, x <<= 4;
|
|
3625 |
|
|
3626 |
if ((x & 0xc000) == 0)
|
|
3627 |
lg2 += 2, x <<= 2;
|
|
3628 |
|
|
3629 |
if ((x & 0x8000) == 0)
|
|
3630 |
lg2 += 1, x <<= 1;
|
|
3631 |
|
|
3632 |
/* Calculate the base logarithm from the top 8 bits as a 28-bit fractional
|
|
3633 |
* value.
|
|
3634 |
*/
|
|
3635 |
lg2 <<= 28;
|
|
3636 |
lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4;
|
|
3637 |
|
|
3638 |
/* Now we need to interpolate the factor, this requires a division by the top
|
|
3639 |
* 8 bits. Do this with maximum precision.
|
|
3640 |
*/
|
|
3641 |
x = ((x << 16) + (x >> 9)) / (x >> 8);
|
|
3642 |
|
|
3643 |
/* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24,
|
|
3644 |
* the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly
|
|
3645 |
* 16 bits to interpolate to get the low bits of the result. Round the
|
|
3646 |
* answer. Note that the end point values are scaled by 64 to retain overall
|
|
3647 |
* precision and that 'lg2' is current scaled by an extra 12 bits, so adjust
|
|
3648 |
* the overall scaling by 6-12. Round at every step.
|
|
3649 |
*/
|
|
3650 |
x -= 1U << 24;
|
|
3651 |
|
|
3652 |
if (x <= 65536U) /* <= '257' */
|
|
3653 |
lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12);
|
|
3654 |
|
|
3655 |
else
|
|
3656 |
lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12);
|
|
3657 |
|
|
3658 |
/* Safe, because the result can't have more than 20 bits: */
|
|
3659 |
return (png_int_32)((lg2 + 2048) >> 12);
|
|
3660 |
}
|
29913
|
3661 |
#endif /* 16BIT */
|
10576
|
3662 |
|
|
3663 |
/* The 'exp()' case must invert the above, taking a 20-bit fixed point
|
|
3664 |
* logarithmic value and returning a 16 or 8-bit number as appropriate. In
|
|
3665 |
* each case only the low 16 bits are relevant - the fraction - since the
|
|
3666 |
* integer bits (the top 4) simply determine a shift.
|
|
3667 |
*
|
29913
|
3668 |
* The worst case is the 16-bit distinction between 65535 and 65534. This
|
|
3669 |
* requires perhaps spurious accuracy in the decoding of the logarithm to
|
10576
|
3670 |
* distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance
|
|
3671 |
* of getting this accuracy in practice.
|
|
3672 |
*
|
|
3673 |
* To deal with this the following exp() function works out the exponent of the
|
|
3674 |
* frational part of the logarithm by using an accurate 32-bit value from the
|
|
3675 |
* top four fractional bits then multiplying in the remaining bits.
|
|
3676 |
*/
|
29913
|
3677 |
static const png_uint_32
|
10576
|
3678 |
png_32bit_exp[16] =
|
|
3679 |
{
|
|
3680 |
/* NOTE: the first entry is deliberately set to the maximum 32-bit value. */
|
|
3681 |
4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U,
|
|
3682 |
3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U,
|
|
3683 |
2553802834U, 2445529972U, 2341847524U, 2242560872U
|
|
3684 |
};
|
|
3685 |
|
|
3686 |
/* Adjustment table; provided to explain the numbers in the code below. */
|
29913
|
3687 |
#if 0
|
10576
|
3688 |
for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"}
|
|
3689 |
11 44937.64284865548751208448
|
|
3690 |
10 45180.98734845585101160448
|
|
3691 |
9 45303.31936980687359311872
|
|
3692 |
8 45364.65110595323018870784
|
|
3693 |
7 45395.35850361789624614912
|
|
3694 |
6 45410.72259715102037508096
|
|
3695 |
5 45418.40724413220722311168
|
|
3696 |
4 45422.25021786898173001728
|
|
3697 |
3 45424.17186732298419044352
|
|
3698 |
2 45425.13273269940811464704
|
|
3699 |
1 45425.61317555035558641664
|
|
3700 |
0 45425.85339951654943850496
|
|
3701 |
#endif
|
|
3702 |
|
29913
|
3703 |
static png_uint_32
|
10576
|
3704 |
png_exp(png_fixed_point x)
|
|
3705 |
{
|
|
3706 |
if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */
|
|
3707 |
{
|
|
3708 |
/* Obtain a 4-bit approximation */
|
|
3709 |
png_uint_32 e = png_32bit_exp[(x >> 12) & 0xf];
|
|
3710 |
|
|
3711 |
/* Incorporate the low 12 bits - these decrease the returned value by
|
|
3712 |
* multiplying by a number less than 1 if the bit is set. The multiplier
|
|
3713 |
* is determined by the above table and the shift. Notice that the values
|
|
3714 |
* converge on 45426 and this is used to allow linear interpolation of the
|
|
3715 |
* low bits.
|
|
3716 |
*/
|
|
3717 |
if (x & 0x800)
|
|
3718 |
e -= (((e >> 16) * 44938U) + 16U) >> 5;
|
|
3719 |
|
|
3720 |
if (x & 0x400)
|
|
3721 |
e -= (((e >> 16) * 45181U) + 32U) >> 6;
|
|
3722 |
|
|
3723 |
if (x & 0x200)
|
|
3724 |
e -= (((e >> 16) * 45303U) + 64U) >> 7;
|
|
3725 |
|
|
3726 |
if (x & 0x100)
|
|
3727 |
e -= (((e >> 16) * 45365U) + 128U) >> 8;
|
|
3728 |
|
|
3729 |
if (x & 0x080)
|
|
3730 |
e -= (((e >> 16) * 45395U) + 256U) >> 9;
|
|
3731 |
|
|
3732 |
if (x & 0x040)
|
|
3733 |
e -= (((e >> 16) * 45410U) + 512U) >> 10;
|
|
3734 |
|
|
3735 |
/* And handle the low 6 bits in a single block. */
|
|
3736 |
e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9;
|
|
3737 |
|
|
3738 |
/* Handle the upper bits of x. */
|
|
3739 |
e >>= x >> 16;
|
|
3740 |
return e;
|
|
3741 |
}
|
|
3742 |
|
|
3743 |
/* Check for overflow */
|
|
3744 |
if (x <= 0)
|
|
3745 |
return png_32bit_exp[0];
|
|
3746 |
|
|
3747 |
/* Else underflow */
|
|
3748 |
return 0;
|
|
3749 |
}
|
|
3750 |
|
29913
|
3751 |
static png_byte
|
10576
|
3752 |
png_exp8bit(png_fixed_point lg2)
|
|
3753 |
{
|
|
3754 |
/* Get a 32-bit value: */
|
|
3755 |
png_uint_32 x = png_exp(lg2);
|
|
3756 |
|
29913
|
3757 |
/* Convert the 32-bit value to 0..255 by multiplying by 256-1. Note that the
|
10576
|
3758 |
* second, rounding, step can't overflow because of the first, subtraction,
|
|
3759 |
* step.
|
|
3760 |
*/
|
|
3761 |
x -= x >> 8;
|
|
3762 |
return (png_byte)((x + 0x7fffffU) >> 24);
|
|
3763 |
}
|
|
3764 |
|
29913
|
3765 |
#ifdef PNG_16BIT_SUPPORTED
|
|
3766 |
static png_uint_16
|
10576
|
3767 |
png_exp16bit(png_fixed_point lg2)
|
|
3768 |
{
|
|
3769 |
/* Get a 32-bit value: */
|
|
3770 |
png_uint_32 x = png_exp(lg2);
|
|
3771 |
|
|
3772 |
/* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */
|
|
3773 |
x -= x >> 16;
|
|
3774 |
return (png_uint_16)((x + 32767U) >> 16);
|
|
3775 |
}
|
29913
|
3776 |
#endif /* 16BIT */
|
10576
|
3777 |
#endif /* FLOATING_ARITHMETIC */
|
|
3778 |
|
|
3779 |
png_byte
|
|
3780 |
png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
|
|
3781 |
{
|
|
3782 |
if (value > 0 && value < 255)
|
|
3783 |
{
|
|
3784 |
# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
29913
|
3785 |
/* 'value' is unsigned, ANSI-C90 requires the compiler to correctly
|
|
3786 |
* convert this to a floating point value. This includes values that
|
|
3787 |
* would overflow if 'value' were to be converted to 'int'.
|
|
3788 |
*
|
|
3789 |
* Apparently GCC, however, does an intermediate conversion to (int)
|
|
3790 |
* on some (ARM) but not all (x86) platforms, possibly because of
|
|
3791 |
* hardware FP limitations. (E.g. if the hardware conversion always
|
|
3792 |
* assumes the integer register contains a signed value.) This results
|
|
3793 |
* in ANSI-C undefined behavior for large values.
|
|
3794 |
*
|
|
3795 |
* Other implementations on the same machine might actually be ANSI-C90
|
|
3796 |
* conformant and therefore compile spurious extra code for the large
|
|
3797 |
* values.
|
|
3798 |
*
|
|
3799 |
* We can be reasonably sure that an unsigned to float conversion
|
|
3800 |
* won't be faster than an int to float one. Therefore this code
|
|
3801 |
* assumes responsibility for the undefined behavior, which it knows
|
|
3802 |
* can't happen because of the check above.
|
|
3803 |
*
|
|
3804 |
* Note the argument to this routine is an (unsigned int) because, on
|
|
3805 |
* 16-bit platforms, it is assigned a value which might be out of
|
|
3806 |
* range for an (int); that would result in undefined behavior in the
|
|
3807 |
* caller if the *argument* ('value') were to be declared (int).
|
|
3808 |
*/
|
|
3809 |
double r = floor(255*pow((int)/*SAFE*/value/255.,gamma_val*.00001)+.5);
|
10576
|
3810 |
return (png_byte)r;
|
|
3811 |
# else
|
|
3812 |
png_int_32 lg2 = png_log8bit(value);
|
|
3813 |
png_fixed_point res;
|
|
3814 |
|
29913
|
3815 |
if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0)
|
10576
|
3816 |
return png_exp8bit(res);
|
|
3817 |
|
|
3818 |
/* Overflow. */
|
|
3819 |
value = 0;
|
|
3820 |
# endif
|
|
3821 |
}
|
|
3822 |
|
|
3823 |
return (png_byte)value;
|
|
3824 |
}
|
|
3825 |
|
29913
|
3826 |
#ifdef PNG_16BIT_SUPPORTED
|
10576
|
3827 |
png_uint_16
|
|
3828 |
png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
|
|
3829 |
{
|
|
3830 |
if (value > 0 && value < 65535)
|
|
3831 |
{
|
|
3832 |
# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
29913
|
3833 |
/* The same (unsigned int)->(double) constraints apply here as above,
|
|
3834 |
* however in this case the (unsigned int) to (int) conversion can
|
|
3835 |
* overflow on an ANSI-C90 compliant system so the cast needs to ensure
|
|
3836 |
* that this is not possible.
|
|
3837 |
*/
|
|
3838 |
double r = floor(65535*pow((png_int_32)value/65535.,
|
|
3839 |
gamma_val*.00001)+.5);
|
10576
|
3840 |
return (png_uint_16)r;
|
|
3841 |
# else
|
|
3842 |
png_int_32 lg2 = png_log16bit(value);
|
|
3843 |
png_fixed_point res;
|
|
3844 |
|
29913
|
3845 |
if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0)
|
10576
|
3846 |
return png_exp16bit(res);
|
|
3847 |
|
|
3848 |
/* Overflow. */
|
|
3849 |
value = 0;
|
|
3850 |
# endif
|
|
3851 |
}
|
|
3852 |
|
|
3853 |
return (png_uint_16)value;
|
|
3854 |
}
|
29913
|
3855 |
#endif /* 16BIT */
|
10576
|
3856 |
|
|
3857 |
/* This does the right thing based on the bit_depth field of the
|
|
3858 |
* png_struct, interpreting values as 8-bit or 16-bit. While the result
|
|
3859 |
* is nominally a 16-bit value if bit depth is 8 then the result is
|
|
3860 |
* 8-bit (as are the arguments.)
|
|
3861 |
*/
|
|
3862 |
png_uint_16 /* PRIVATE */
|
29913
|
3863 |
png_gamma_correct(png_structrp png_ptr, unsigned int value,
|
10576
|
3864 |
png_fixed_point gamma_val)
|
|
3865 |
{
|
|
3866 |
if (png_ptr->bit_depth == 8)
|
|
3867 |
return png_gamma_8bit_correct(value, gamma_val);
|
|
3868 |
|
29913
|
3869 |
#ifdef PNG_16BIT_SUPPORTED
|
10576
|
3870 |
else
|
|
3871 |
return png_gamma_16bit_correct(value, gamma_val);
|
29913
|
3872 |
#else
|
|
3873 |
/* should not reach this */
|
|
3874 |
return 0;
|
|
3875 |
#endif /* 16BIT */
|
10576
|
3876 |
}
|
|
3877 |
|
29913
|
3878 |
#ifdef PNG_16BIT_SUPPORTED
|
10576
|
3879 |
/* Internal function to build a single 16-bit table - the table consists of
|
|
3880 |
* 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount
|
|
3881 |
* to shift the input values right (or 16-number_of_signifiant_bits).
|
|
3882 |
*
|
|
3883 |
* The caller is responsible for ensuring that the table gets cleaned up on
|
|
3884 |
* png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument
|
|
3885 |
* should be somewhere that will be cleaned.
|
|
3886 |
*/
|
|
3887 |
static void
|
29913
|
3888 |
png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable,
|
10576
|
3889 |
PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
|
|
3890 |
{
|
|
3891 |
/* Various values derived from 'shift': */
|
|
3892 |
PNG_CONST unsigned int num = 1U << (8U - shift);
|
29913
|
3893 |
#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3894 |
/* CSE the division and work round wacky GCC warnings (see the comments
|
|
3895 |
* in png_gamma_8bit_correct for where these come from.)
|
|
3896 |
*/
|
|
3897 |
PNG_CONST double fmax = 1./(((png_int_32)1 << (16U - shift))-1);
|
|
3898 |
#endif
|
10576
|
3899 |
PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
|
|
3900 |
PNG_CONST unsigned int max_by_2 = 1U << (15U-shift);
|
|
3901 |
unsigned int i;
|
|
3902 |
|
|
3903 |
png_uint_16pp table = *ptable =
|
29913
|
3904 |
(png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
|
10576
|
3905 |
|
|
3906 |
for (i = 0; i < num; i++)
|
|
3907 |
{
|
|
3908 |
png_uint_16p sub_table = table[i] =
|
29913
|
3909 |
(png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16)));
|
10576
|
3910 |
|
|
3911 |
/* The 'threshold' test is repeated here because it can arise for one of
|
|
3912 |
* the 16-bit tables even if the others don't hit it.
|
|
3913 |
*/
|
29913
|
3914 |
if (png_gamma_significant(gamma_val) != 0)
|
10576
|
3915 |
{
|
|
3916 |
/* The old code would overflow at the end and this would cause the
|
|
3917 |
* 'pow' function to return a result >1, resulting in an
|
|
3918 |
* arithmetic error. This code follows the spec exactly; ig is
|
|
3919 |
* the recovered input sample, it always has 8-16 bits.
|
|
3920 |
*
|
|
3921 |
* We want input * 65535/max, rounded, the arithmetic fits in 32
|
|
3922 |
* bits (unsigned) so long as max <= 32767.
|
|
3923 |
*/
|
|
3924 |
unsigned int j;
|
|
3925 |
for (j = 0; j < 256; j++)
|
|
3926 |
{
|
|
3927 |
png_uint_32 ig = (j << (8-shift)) + i;
|
|
3928 |
# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED
|
|
3929 |
/* Inline the 'max' scaling operation: */
|
29913
|
3930 |
/* See png_gamma_8bit_correct for why the cast to (int) is
|
|
3931 |
* required here.
|
|
3932 |
*/
|
|
3933 |
double d = floor(65535.*pow(ig*fmax, gamma_val*.00001)+.5);
|
10576
|
3934 |
sub_table[j] = (png_uint_16)d;
|
|
3935 |
# else
|
29913
|
3936 |
if (shift != 0)
|
10576
|
3937 |
ig = (ig * 65535U + max_by_2)/max;
|
|
3938 |
|
|
3939 |
sub_table[j] = png_gamma_16bit_correct(ig, gamma_val);
|
|
3940 |
# endif
|
|
3941 |
}
|
|
3942 |
}
|
|
3943 |
else
|
|
3944 |
{
|
|
3945 |
/* We must still build a table, but do it the fast way. */
|
|
3946 |
unsigned int j;
|
|
3947 |
|
|
3948 |
for (j = 0; j < 256; j++)
|
|
3949 |
{
|
|
3950 |
png_uint_32 ig = (j << (8-shift)) + i;
|
|
3951 |
|
29913
|
3952 |
if (shift != 0)
|
10576
|
3953 |
ig = (ig * 65535U + max_by_2)/max;
|
|
3954 |
|
|
3955 |
sub_table[j] = (png_uint_16)ig;
|
|
3956 |
}
|
|
3957 |
}
|
|
3958 |
}
|
|
3959 |
}
|
|
3960 |
|
|
3961 |
/* NOTE: this function expects the *inverse* of the overall gamma transformation
|
|
3962 |
* required.
|
|
3963 |
*/
|
|
3964 |
static void
|
29913
|
3965 |
png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable,
|
10576
|
3966 |
PNG_CONST unsigned int shift, PNG_CONST png_fixed_point gamma_val)
|
|
3967 |
{
|
|
3968 |
PNG_CONST unsigned int num = 1U << (8U - shift);
|
|
3969 |
PNG_CONST unsigned int max = (1U << (16U - shift))-1U;
|
|
3970 |
unsigned int i;
|
|
3971 |
png_uint_32 last;
|
|
3972 |
|
|
3973 |
png_uint_16pp table = *ptable =
|
29913
|
3974 |
(png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p)));
|
10576
|
3975 |
|
|
3976 |
/* 'num' is the number of tables and also the number of low bits of low
|
|
3977 |
* bits of the input 16-bit value used to select a table. Each table is
|
29913
|
3978 |
* itself indexed by the high 8 bits of the value.
|
10576
|
3979 |
*/
|
|
3980 |
for (i = 0; i < num; i++)
|
|
3981 |
table[i] = (png_uint_16p)png_malloc(png_ptr,
|
29913
|
3982 |
256 * (sizeof (png_uint_16)));
|
10576
|
3983 |
|
|
3984 |
/* 'gamma_val' is set to the reciprocal of the value calculated above, so
|
|
3985 |
* pow(out,g) is an *input* value. 'last' is the last input value set.
|
|
3986 |
*
|
|
3987 |
* In the loop 'i' is used to find output values. Since the output is
|
|
3988 |
* 8-bit there are only 256 possible values. The tables are set up to
|
|
3989 |
* select the closest possible output value for each input by finding
|
|
3990 |
* the input value at the boundary between each pair of output values
|
|
3991 |
* and filling the table up to that boundary with the lower output
|
|
3992 |
* value.
|
|
3993 |
*
|
|
3994 |
* The boundary values are 0.5,1.5..253.5,254.5. Since these are 9-bit
|
|
3995 |
* values the code below uses a 16-bit value in i; the values start at
|
|
3996 |
* 128.5 (for 0.5) and step by 257, for a total of 254 values (the last
|
|
3997 |
* entries are filled with 255). Start i at 128 and fill all 'last'
|
|
3998 |
* table entries <= 'max'
|
|
3999 |
*/
|
|
4000 |
last = 0;
|
|
4001 |
for (i = 0; i < 255; ++i) /* 8-bit output value */
|
|
4002 |
{
|
|
4003 |
/* Find the corresponding maximum input value */
|
|
4004 |
png_uint_16 out = (png_uint_16)(i * 257U); /* 16-bit output value */
|
|
4005 |
|
|
4006 |
/* Find the boundary value in 16 bits: */
|
|
4007 |
png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val);
|
|
4008 |
|
|
4009 |
/* Adjust (round) to (16-shift) bits: */
|
|
4010 |
bound = (bound * max + 32768U)/65535U + 1U;
|
|
4011 |
|
|
4012 |
while (last < bound)
|
|
4013 |
{
|
|
4014 |
table[last & (0xffU >> shift)][last >> (8U - shift)] = out;
|
|
4015 |
last++;
|
|
4016 |
}
|
|
4017 |
}
|
|
4018 |
|
|
4019 |
/* And fill in the final entries. */
|
|
4020 |
while (last < (num << 8))
|
|
4021 |
{
|
|
4022 |
table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U;
|
|
4023 |
last++;
|
|
4024 |
}
|
|
4025 |
}
|
29913
|
4026 |
#endif /* 16BIT */
|
10576
|
4027 |
|
|
4028 |
/* Build a single 8-bit table: same as the 16-bit case but much simpler (and
|
|
4029 |
* typically much faster). Note that libpng currently does no sBIT processing
|
29913
|
4030 |
* (apparently contrary to the spec) so a 256-entry table is always generated.
|
10576
|
4031 |
*/
|
|
4032 |
static void
|
29913
|
4033 |
png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable,
|
10576
|
4034 |
PNG_CONST png_fixed_point gamma_val)
|
|
4035 |
{
|
|
4036 |
unsigned int i;
|
|
4037 |
png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256);
|
|
4038 |
|
29913
|
4039 |
if (png_gamma_significant(gamma_val) != 0)
|
|
4040 |
for (i=0; i<256; i++)
|
|
4041 |
table[i] = png_gamma_8bit_correct(i, gamma_val);
|
|
4042 |
|
|
4043 |
else
|
|
4044 |
for (i=0; i<256; ++i)
|
|
4045 |
table[i] = (png_byte)i;
|
|
4046 |
}
|
|
4047 |
|
|
4048 |
/* Used from png_read_destroy and below to release the memory used by the gamma
|
|
4049 |
* tables.
|
|
4050 |
*/
|
|
4051 |
void /* PRIVATE */
|
|
4052 |
png_destroy_gamma_table(png_structrp png_ptr)
|
|
4053 |
{
|
|
4054 |
png_free(png_ptr, png_ptr->gamma_table);
|
|
4055 |
png_ptr->gamma_table = NULL;
|
|
4056 |
|
|
4057 |
#ifdef PNG_16BIT_SUPPORTED
|
|
4058 |
if (png_ptr->gamma_16_table != NULL)
|
|
4059 |
{
|
|
4060 |
int i;
|
|
4061 |
int istop = (1 << (8 - png_ptr->gamma_shift));
|
|
4062 |
for (i = 0; i < istop; i++)
|
|
4063 |
{
|
|
4064 |
png_free(png_ptr, png_ptr->gamma_16_table[i]);
|
|
4065 |
}
|
|
4066 |
png_free(png_ptr, png_ptr->gamma_16_table);
|
|
4067 |
png_ptr->gamma_16_table = NULL;
|
|
4068 |
}
|
|
4069 |
#endif /* 16BIT */
|
|
4070 |
|
|
4071 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
|
|
4072 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
|
|
4073 |
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
|
|
4074 |
png_free(png_ptr, png_ptr->gamma_from_1);
|
|
4075 |
png_ptr->gamma_from_1 = NULL;
|
|
4076 |
png_free(png_ptr, png_ptr->gamma_to_1);
|
|
4077 |
png_ptr->gamma_to_1 = NULL;
|
|
4078 |
|
|
4079 |
#ifdef PNG_16BIT_SUPPORTED
|
|
4080 |
if (png_ptr->gamma_16_from_1 != NULL)
|
|
4081 |
{
|
|
4082 |
int i;
|
|
4083 |
int istop = (1 << (8 - png_ptr->gamma_shift));
|
|
4084 |
for (i = 0; i < istop; i++)
|
|
4085 |
{
|
|
4086 |
png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
|
|
4087 |
}
|
|
4088 |
png_free(png_ptr, png_ptr->gamma_16_from_1);
|
|
4089 |
png_ptr->gamma_16_from_1 = NULL;
|
|
4090 |
}
|
|
4091 |
if (png_ptr->gamma_16_to_1 != NULL)
|
|
4092 |
{
|
|
4093 |
int i;
|
|
4094 |
int istop = (1 << (8 - png_ptr->gamma_shift));
|
|
4095 |
for (i = 0; i < istop; i++)
|
|
4096 |
{
|
|
4097 |
png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
|
|
4098 |
}
|
|
4099 |
png_free(png_ptr, png_ptr->gamma_16_to_1);
|
|
4100 |
png_ptr->gamma_16_to_1 = NULL;
|
|
4101 |
}
|
|
4102 |
#endif /* 16BIT */
|
|
4103 |
#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
|
10576
|
4104 |
}
|
|
4105 |
|
|
4106 |
/* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
|
|
4107 |
* tables, we don't make a full table if we are reducing to 8-bit in
|
|
4108 |
* the future. Note also how the gamma_16 tables are segmented so that
|
|
4109 |
* we don't need to allocate > 64K chunks for a full 16-bit table.
|
|
4110 |
*/
|
|
4111 |
void /* PRIVATE */
|
29913
|
4112 |
png_build_gamma_table(png_structrp png_ptr, int bit_depth)
|
10576
|
4113 |
{
|
|
4114 |
png_debug(1, "in png_build_gamma_table");
|
|
4115 |
|
29913
|
4116 |
/* Remove any existing table; this copes with multiple calls to
|
|
4117 |
* png_read_update_info. The warning is because building the gamma tables
|
|
4118 |
* multiple times is a performance hit - it's harmless but the ability to call
|
|
4119 |
* png_read_update_info() multiple times is new in 1.5.6 so it seems sensible
|
|
4120 |
* to warn if the app introduces such a hit.
|
|
4121 |
*/
|
|
4122 |
if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL)
|
|
4123 |
{
|
|
4124 |
png_warning(png_ptr, "gamma table being rebuilt");
|
|
4125 |
png_destroy_gamma_table(png_ptr);
|
|
4126 |
}
|
|
4127 |
|
10576
|
4128 |
if (bit_depth <= 8)
|
|
4129 |
{
|
|
4130 |
png_build_8bit_table(png_ptr, &png_ptr->gamma_table,
|
29913
|
4131 |
png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
|
10576
|
4132 |
png_ptr->screen_gamma) : PNG_FP_1);
|
|
4133 |
|
|
4134 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
|
|
4135 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
|
|
4136 |
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
|
29913
|
4137 |
if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
|
10576
|
4138 |
{
|
|
4139 |
png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1,
|
29913
|
4140 |
png_reciprocal(png_ptr->colorspace.gamma));
|
10576
|
4141 |
|
|
4142 |
png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1,
|
|
4143 |
png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
|
29913
|
4144 |
png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
|
10576
|
4145 |
}
|
|
4146 |
#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
|
|
4147 |
}
|
29913
|
4148 |
#ifdef PNG_16BIT_SUPPORTED
|
10576
|
4149 |
else
|
|
4150 |
{
|
|
4151 |
png_byte shift, sig_bit;
|
|
4152 |
|
29913
|
4153 |
if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
|
10576
|
4154 |
{
|
|
4155 |
sig_bit = png_ptr->sig_bit.red;
|
|
4156 |
|
|
4157 |
if (png_ptr->sig_bit.green > sig_bit)
|
|
4158 |
sig_bit = png_ptr->sig_bit.green;
|
|
4159 |
|
|
4160 |
if (png_ptr->sig_bit.blue > sig_bit)
|
|
4161 |
sig_bit = png_ptr->sig_bit.blue;
|
|
4162 |
}
|
|
4163 |
else
|
|
4164 |
sig_bit = png_ptr->sig_bit.gray;
|
|
4165 |
|
|
4166 |
/* 16-bit gamma code uses this equation:
|
|
4167 |
*
|
|
4168 |
* ov = table[(iv & 0xff) >> gamma_shift][iv >> 8]
|
|
4169 |
*
|
|
4170 |
* Where 'iv' is the input color value and 'ov' is the output value -
|
|
4171 |
* pow(iv, gamma).
|
|
4172 |
*
|
29913
|
4173 |
* Thus the gamma table consists of up to 256 256-entry tables. The table
|
10576
|
4174 |
* is selected by the (8-gamma_shift) most significant of the low 8 bits of
|
|
4175 |
* the color value then indexed by the upper 8 bits:
|
|
4176 |
*
|
|
4177 |
* table[low bits][high 8 bits]
|
|
4178 |
*
|
|
4179 |
* So the table 'n' corresponds to all those 'iv' of:
|
|
4180 |
*
|
|
4181 |
* <all high 8-bit values><n << gamma_shift>..<(n+1 << gamma_shift)-1>
|
|
4182 |
*
|
|
4183 |
*/
|
|
4184 |
if (sig_bit > 0 && sig_bit < 16U)
|
|
4185 |
shift = (png_byte)(16U - sig_bit); /* shift == insignificant bits */
|
|
4186 |
|
|
4187 |
else
|
|
4188 |
shift = 0; /* keep all 16 bits */
|
|
4189 |
|
29913
|
4190 |
if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
|
10576
|
4191 |
{
|
|
4192 |
/* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively
|
|
4193 |
* the significant bits in the *input* when the output will
|
|
4194 |
* eventually be 8 bits. By default it is 11.
|
|
4195 |
*/
|
|
4196 |
if (shift < (16U - PNG_MAX_GAMMA_8))
|
|
4197 |
shift = (16U - PNG_MAX_GAMMA_8);
|
|
4198 |
}
|
|
4199 |
|
|
4200 |
if (shift > 8U)
|
|
4201 |
shift = 8U; /* Guarantees at least one table! */
|
|
4202 |
|
|
4203 |
png_ptr->gamma_shift = shift;
|
|
4204 |
|
|
4205 |
/* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now
|
|
4206 |
* PNG_COMPOSE). This effectively smashed the background calculation for
|
|
4207 |
* 16-bit output because the 8-bit table assumes the result will be reduced
|
|
4208 |
* to 8 bits.
|
|
4209 |
*/
|
29913
|
4210 |
if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0)
|
10576
|
4211 |
png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift,
|
29913
|
4212 |
png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma,
|
10576
|
4213 |
png_ptr->screen_gamma) : PNG_FP_1);
|
|
4214 |
|
|
4215 |
else
|
|
4216 |
png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift,
|
29913
|
4217 |
png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma,
|
10576
|
4218 |
png_ptr->screen_gamma) : PNG_FP_1);
|
|
4219 |
|
|
4220 |
#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
|
|
4221 |
defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
|
|
4222 |
defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
|
29913
|
4223 |
if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0)
|
10576
|
4224 |
{
|
|
4225 |
png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift,
|
29913
|
4226 |
png_reciprocal(png_ptr->colorspace.gamma));
|
10576
|
4227 |
|
|
4228 |
/* Notice that the '16 from 1' table should be full precision, however
|
|
4229 |
* the lookup on this table still uses gamma_shift, so it can't be.
|
|
4230 |
* TODO: fix this.
|
|
4231 |
*/
|
|
4232 |
png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift,
|
|
4233 |
png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) :
|
29913
|
4234 |
png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */);
|
10576
|
4235 |
}
|
|
4236 |
#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
|
|
4237 |
}
|
29913
|
4238 |
#endif /* 16BIT */
|
10576
|
4239 |
}
|
|
4240 |
#endif /* READ_GAMMA */
|
29913
|
4241 |
|
|
4242 |
/* HARDWARE OR SOFTWARE OPTION SUPPORT */
|
|
4243 |
#ifdef PNG_SET_OPTION_SUPPORTED
|
|
4244 |
int PNGAPI
|
|
4245 |
png_set_option(png_structrp png_ptr, int option, int onoff)
|
|
4246 |
{
|
|
4247 |
if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
|
|
4248 |
(option & 1) == 0)
|
|
4249 |
{
|
|
4250 |
int mask = 3 << option;
|
|
4251 |
int setting = (2 + (onoff != 0)) << option;
|
|
4252 |
int current = png_ptr->options;
|
|
4253 |
|
|
4254 |
png_ptr->options = (png_byte)((current & ~mask) | setting);
|
|
4255 |
|
|
4256 |
return (current & mask) >> option;
|
|
4257 |
}
|
|
4258 |
|
|
4259 |
return PNG_OPTION_INVALID;
|
|
4260 |
}
|
|
4261 |
#endif
|
|
4262 |
|
|
4263 |
/* sRGB support */
|
|
4264 |
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
|
|
4265 |
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
|
|
4266 |
/* sRGB conversion tables; these are machine generated with the code in
|
|
4267 |
* contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the
|
|
4268 |
* specification (see the article at http://en.wikipedia.org/wiki/SRGB)
|
|
4269 |
* is used, not the gamma=1/2.2 approximation use elsewhere in libpng.
|
|
4270 |
* The sRGB to linear table is exact (to the nearest 16 bit linear fraction).
|
|
4271 |
* The inverse (linear to sRGB) table has accuracies as follows:
|
|
4272 |
*
|
|
4273 |
* For all possible (255*65535+1) input values:
|
|
4274 |
*
|
|
4275 |
* error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact
|
|
4276 |
*
|
|
4277 |
* For the input values corresponding to the 65536 16-bit values:
|
|
4278 |
*
|
|
4279 |
* error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact
|
|
4280 |
*
|
|
4281 |
* In all cases the inexact readings are only off by one.
|
|
4282 |
*/
|
|
4283 |
|
|
4284 |
#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
|
|
4285 |
/* The convert-to-sRGB table is only currently required for read. */
|
|
4286 |
const png_uint_16 png_sRGB_table[256] =
|
|
4287 |
{
|
|
4288 |
0,20,40,60,80,99,119,139,
|
|
4289 |
159,179,199,219,241,264,288,313,
|
|
4290 |
340,367,396,427,458,491,526,562,
|
|
4291 |
599,637,677,718,761,805,851,898,
|
|
4292 |
947,997,1048,1101,1156,1212,1270,1330,
|
|
4293 |
1391,1453,1517,1583,1651,1720,1790,1863,
|
|
4294 |
1937,2013,2090,2170,2250,2333,2418,2504,
|
|
4295 |
2592,2681,2773,2866,2961,3058,3157,3258,
|
|
4296 |
3360,3464,3570,3678,3788,3900,4014,4129,
|
|
4297 |
4247,4366,4488,4611,4736,4864,4993,5124,
|
|
4298 |
5257,5392,5530,5669,5810,5953,6099,6246,
|
|
4299 |
6395,6547,6700,6856,7014,7174,7335,7500,
|
|
4300 |
7666,7834,8004,8177,8352,8528,8708,8889,
|
|
4301 |
9072,9258,9445,9635,9828,10022,10219,10417,
|
|
4302 |
10619,10822,11028,11235,11446,11658,11873,12090,
|
|
4303 |
12309,12530,12754,12980,13209,13440,13673,13909,
|
|
4304 |
14146,14387,14629,14874,15122,15371,15623,15878,
|
|
4305 |
16135,16394,16656,16920,17187,17456,17727,18001,
|
|
4306 |
18277,18556,18837,19121,19407,19696,19987,20281,
|
|
4307 |
20577,20876,21177,21481,21787,22096,22407,22721,
|
|
4308 |
23038,23357,23678,24002,24329,24658,24990,25325,
|
|
4309 |
25662,26001,26344,26688,27036,27386,27739,28094,
|
|
4310 |
28452,28813,29176,29542,29911,30282,30656,31033,
|
|
4311 |
31412,31794,32179,32567,32957,33350,33745,34143,
|
|
4312 |
34544,34948,35355,35764,36176,36591,37008,37429,
|
|
4313 |
37852,38278,38706,39138,39572,40009,40449,40891,
|
|
4314 |
41337,41785,42236,42690,43147,43606,44069,44534,
|
|
4315 |
45002,45473,45947,46423,46903,47385,47871,48359,
|
|
4316 |
48850,49344,49841,50341,50844,51349,51858,52369,
|
|
4317 |
52884,53401,53921,54445,54971,55500,56032,56567,
|
|
4318 |
57105,57646,58190,58737,59287,59840,60396,60955,
|
|
4319 |
61517,62082,62650,63221,63795,64372,64952,65535
|
|
4320 |
};
|
|
4321 |
#endif /* SIMPLIFIED_READ */
|
|
4322 |
|
|
4323 |
/* The base/delta tables are required for both read and write (but currently
|
|
4324 |
* only the simplified versions.)
|
|
4325 |
*/
|
|
4326 |
const png_uint_16 png_sRGB_base[512] =
|
|
4327 |
{
|
|
4328 |
128,1782,3383,4644,5675,6564,7357,8074,
|
|
4329 |
8732,9346,9921,10463,10977,11466,11935,12384,
|
|
4330 |
12816,13233,13634,14024,14402,14769,15125,15473,
|
|
4331 |
15812,16142,16466,16781,17090,17393,17690,17981,
|
|
4332 |
18266,18546,18822,19093,19359,19621,19879,20133,
|
|
4333 |
20383,20630,20873,21113,21349,21583,21813,22041,
|
|
4334 |
22265,22487,22707,22923,23138,23350,23559,23767,
|
|
4335 |
23972,24175,24376,24575,24772,24967,25160,25352,
|
|
4336 |
25542,25730,25916,26101,26284,26465,26645,26823,
|
|
4337 |
27000,27176,27350,27523,27695,27865,28034,28201,
|
|
4338 |
28368,28533,28697,28860,29021,29182,29341,29500,
|
|
4339 |
29657,29813,29969,30123,30276,30429,30580,30730,
|
|
4340 |
30880,31028,31176,31323,31469,31614,31758,31902,
|
|
4341 |
32045,32186,32327,32468,32607,32746,32884,33021,
|
|
4342 |
33158,33294,33429,33564,33697,33831,33963,34095,
|
|
4343 |
34226,34357,34486,34616,34744,34873,35000,35127,
|
|
4344 |
35253,35379,35504,35629,35753,35876,35999,36122,
|
|
4345 |
36244,36365,36486,36606,36726,36845,36964,37083,
|
|
4346 |
37201,37318,37435,37551,37668,37783,37898,38013,
|
|
4347 |
38127,38241,38354,38467,38580,38692,38803,38915,
|
|
4348 |
39026,39136,39246,39356,39465,39574,39682,39790,
|
|
4349 |
39898,40005,40112,40219,40325,40431,40537,40642,
|
|
4350 |
40747,40851,40955,41059,41163,41266,41369,41471,
|
|
4351 |
41573,41675,41777,41878,41979,42079,42179,42279,
|
|
4352 |
42379,42478,42577,42676,42775,42873,42971,43068,
|
|
4353 |
43165,43262,43359,43456,43552,43648,43743,43839,
|
|
4354 |
43934,44028,44123,44217,44311,44405,44499,44592,
|
|
4355 |
44685,44778,44870,44962,45054,45146,45238,45329,
|
|
4356 |
45420,45511,45601,45692,45782,45872,45961,46051,
|
|
4357 |
46140,46229,46318,46406,46494,46583,46670,46758,
|
|
4358 |
46846,46933,47020,47107,47193,47280,47366,47452,
|
|
4359 |
47538,47623,47709,47794,47879,47964,48048,48133,
|
|
4360 |
48217,48301,48385,48468,48552,48635,48718,48801,
|
|
4361 |
48884,48966,49048,49131,49213,49294,49376,49458,
|
|
4362 |
49539,49620,49701,49782,49862,49943,50023,50103,
|
|
4363 |
50183,50263,50342,50422,50501,50580,50659,50738,
|
|
4364 |
50816,50895,50973,51051,51129,51207,51285,51362,
|
|
4365 |
51439,51517,51594,51671,51747,51824,51900,51977,
|
|
4366 |
52053,52129,52205,52280,52356,52432,52507,52582,
|
|
4367 |
52657,52732,52807,52881,52956,53030,53104,53178,
|
|
4368 |
53252,53326,53400,53473,53546,53620,53693,53766,
|
|
4369 |
53839,53911,53984,54056,54129,54201,54273,54345,
|
|
4370 |
54417,54489,54560,54632,54703,54774,54845,54916,
|
|
4371 |
54987,55058,55129,55199,55269,55340,55410,55480,
|
|
4372 |
55550,55620,55689,55759,55828,55898,55967,56036,
|
|
4373 |
56105,56174,56243,56311,56380,56448,56517,56585,
|
|
4374 |
56653,56721,56789,56857,56924,56992,57059,57127,
|
|
4375 |
57194,57261,57328,57395,57462,57529,57595,57662,
|
|
4376 |
57728,57795,57861,57927,57993,58059,58125,58191,
|
|
4377 |
58256,58322,58387,58453,58518,58583,58648,58713,
|
|
4378 |
58778,58843,58908,58972,59037,59101,59165,59230,
|
|
4379 |
59294,59358,59422,59486,59549,59613,59677,59740,
|
|
4380 |
59804,59867,59930,59993,60056,60119,60182,60245,
|
|
4381 |
60308,60370,60433,60495,60558,60620,60682,60744,
|
|
4382 |
60806,60868,60930,60992,61054,61115,61177,61238,
|
|
4383 |
61300,61361,61422,61483,61544,61605,61666,61727,
|
|
4384 |
61788,61848,61909,61969,62030,62090,62150,62211,
|
|
4385 |
62271,62331,62391,62450,62510,62570,62630,62689,
|
|
4386 |
62749,62808,62867,62927,62986,63045,63104,63163,
|
|
4387 |
63222,63281,63340,63398,63457,63515,63574,63632,
|
|
4388 |
63691,63749,63807,63865,63923,63981,64039,64097,
|
|
4389 |
64155,64212,64270,64328,64385,64443,64500,64557,
|
|
4390 |
64614,64672,64729,64786,64843,64900,64956,65013,
|
|
4391 |
65070,65126,65183,65239,65296,65352,65409,65465
|
|
4392 |
};
|
|
4393 |
|
|
4394 |
const png_byte png_sRGB_delta[512] =
|
|
4395 |
{
|
|
4396 |
207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54,
|
|
4397 |
52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36,
|
|
4398 |
35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28,
|
|
4399 |
28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24,
|
|
4400 |
23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21,
|
|
4401 |
21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19,
|
|
4402 |
19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17,
|
|
4403 |
17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16,
|
|
4404 |
16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15,
|
|
4405 |
15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14,
|
|
4406 |
14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13,
|
|
4407 |
13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12,
|
|
4408 |
12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
|
|
4409 |
12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11,
|
|
4410 |
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
|
|
4411 |
11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,
|
|
4412 |
11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
|
|
4413 |
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
|
|
4414 |
10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
|
|
4415 |
10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
|
4416 |
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
|
4417 |
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
|
4418 |
9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
|
|
4419 |
9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
4420 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
4421 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
4422 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
4423 |
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
|
|
4424 |
8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7,
|
|
4425 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
4426 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
|
4427 |
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
|
|
4428 |
};
|
|
4429 |
#endif /* SIMPLIFIED READ/WRITE sRGB support */
|
|
4430 |
|
|
4431 |
/* SIMPLIFIED READ/WRITE SUPPORT */
|
|
4432 |
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
|
|
4433 |
defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
|
|
4434 |
static int
|
|
4435 |
png_image_free_function(png_voidp argument)
|
|
4436 |
{
|
|
4437 |
png_imagep image = png_voidcast(png_imagep, argument);
|
|
4438 |
png_controlp cp = image->opaque;
|
|
4439 |
png_control c;
|
|
4440 |
|
|
4441 |
/* Double check that we have a png_ptr - it should be impossible to get here
|
|
4442 |
* without one.
|
|
4443 |
*/
|
|
4444 |
if (cp->png_ptr == NULL)
|
|
4445 |
return 0;
|
|
4446 |
|
|
4447 |
/* First free any data held in the control structure. */
|
|
4448 |
# ifdef PNG_STDIO_SUPPORTED
|
|
4449 |
if (cp->owned_file != 0)
|
|
4450 |
{
|
|
4451 |
FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr);
|
|
4452 |
cp->owned_file = 0;
|
|
4453 |
|
|
4454 |
/* Ignore errors here. */
|
|
4455 |
if (fp != NULL)
|
|
4456 |
{
|
|
4457 |
cp->png_ptr->io_ptr = NULL;
|
|
4458 |
(void)fclose(fp);
|
|
4459 |
}
|
|
4460 |
}
|
|
4461 |
# endif
|
|
4462 |
|
|
4463 |
/* Copy the control structure so that the original, allocated, version can be
|
|
4464 |
* safely freed. Notice that a png_error here stops the remainder of the
|
|
4465 |
* cleanup, but this is probably fine because that would indicate bad memory
|
|
4466 |
* problems anyway.
|
|
4467 |
*/
|
|
4468 |
c = *cp;
|
|
4469 |
image->opaque = &c;
|
|
4470 |
png_free(c.png_ptr, cp);
|
|
4471 |
|
|
4472 |
/* Then the structures, calling the correct API. */
|
|
4473 |
if (c.for_write != 0)
|
|
4474 |
{
|
|
4475 |
# ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
|
|
4476 |
png_destroy_write_struct(&c.png_ptr, &c.info_ptr);
|
|
4477 |
# else
|
|
4478 |
png_error(c.png_ptr, "simplified write not supported");
|
|
4479 |
# endif
|
|
4480 |
}
|
|
4481 |
else
|
|
4482 |
{
|
|
4483 |
# ifdef PNG_SIMPLIFIED_READ_SUPPORTED
|
|
4484 |
png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL);
|
|
4485 |
# else
|
|
4486 |
png_error(c.png_ptr, "simplified read not supported");
|
|
4487 |
# endif
|
|
4488 |
}
|
|
4489 |
|
|
4490 |
/* Success. */
|
|
4491 |
return 1;
|
|
4492 |
}
|
|
4493 |
|
|
4494 |
void PNGAPI
|
|
4495 |
png_image_free(png_imagep image)
|
|
4496 |
{
|
|
4497 |
/* Safely call the real function, but only if doing so is safe at this point
|
|
4498 |
* (if not inside an error handling context). Otherwise assume
|
|
4499 |
* png_safe_execute will call this API after the return.
|
|
4500 |
*/
|
|
4501 |
if (image != NULL && image->opaque != NULL &&
|
|
4502 |
image->opaque->error_buf == NULL)
|
|
4503 |
{
|
|
4504 |
/* Ignore errors here: */
|
|
4505 |
(void)png_safe_execute(image, png_image_free_function, image);
|
|
4506 |
image->opaque = NULL;
|
|
4507 |
}
|
|
4508 |
}
|
|
4509 |
|
|
4510 |
int /* PRIVATE */
|
|
4511 |
png_image_error(png_imagep image, png_const_charp error_message)
|
|
4512 |
{
|
|
4513 |
/* Utility to log an error. */
|
|
4514 |
png_safecat(image->message, (sizeof image->message), 0, error_message);
|
|
4515 |
image->warning_or_error |= PNG_IMAGE_ERROR;
|
|
4516 |
png_image_free(image);
|
|
4517 |
return 0;
|
|
4518 |
}
|
|
4519 |
|
|
4520 |
#endif /* SIMPLIFIED READ/WRITE */
|
|
4521 |
#endif /* READ || WRITE */
|