fltk  1.3.5-source
About: FLTK (Fast Light Tool Kit) is a cross-platform C++ GUI toolkit for UNIX/Linux (X11), Microsoft Windows, and MacOS X.
  Fossies Dox: fltk-1.3.5-source.tar.bz2  ("inofficial" and yet experimental doxygen-generated source code documentation)  

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