annotate lib/libpng/png.c @ 2406:33659ef7fbac

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