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