COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
png.c File Reference
#include "pngpriv.h"
Include dependency graph for png.c:

Macros

#define APPEND_STRING(string)   pos = png_safecat(out, 29, pos, (string))
 
#define APPEND_NUMBER(format, value)   APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
 
#define APPEND(ch)   if (pos < 28) out[pos++] = (ch)
 
#define PNG_MD5(a, b, c, d)   { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
 
#define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)   { adler, crc, length, md5, broke, intent },
 
#define png_gt(a, b)   ((a) > (b))
 
#define png_fp_add(state, flags)   ((state) |= (flags))
 
#define png_fp_set(state, value)   ((state) = (value) | ((state) & PNG_FP_STICKY))
 

Typedefs

typedef png_libpng_version_1_6_16 Your_png_h_is_not_version_1_6_16
 

Functions

void png_set_sig_bytes (png_structrp png_ptr, int num_bytes)
 
int png_sig_cmp (png_const_bytep sig, png_size_t start, png_size_t num_to_check)
 
voidpf png_zalloc (voidpf png_ptr, uInt items, uInt size)
 
void png_zfree (voidpf png_ptr, voidpf ptr)
 
void png_reset_crc (png_structrp png_ptr)
 
void png_calculate_crc (png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
 
int png_user_version_check (png_structrp png_ptr, png_const_charp user_png_ver)
 
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)
 
png_infop png_create_info_struct (png_const_structrp png_ptr)
 
void png_destroy_info_struct (png_const_structrp png_ptr, png_infopp info_ptr_ptr)
 
void png_info_init_3 (png_infopp ptr_ptr, png_size_t png_info_struct_size)
 
void png_data_freer (png_const_structrp png_ptr, png_inforp info_ptr, int freer, png_uint_32 mask)
 
void png_free_data (png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num)
 
png_voidp png_get_io_ptr (png_const_structrp png_ptr)
 
void png_init_io (png_structrp png_ptr, png_FILE_p fp)
 
void png_save_int_32 (png_bytep buf, png_int_32 i)
 
int png_convert_to_rfc1123_buffer (char out[29], png_const_timep ptime)
 
png_const_charp png_convert_to_rfc1123 (png_structrp png_ptr, png_const_timep ptime)
 
png_const_charp png_get_copyright (png_const_structrp png_ptr)
 
png_const_charp png_get_libpng_ver (png_const_structrp png_ptr)
 
png_const_charp png_get_header_ver (png_const_structrp png_ptr)
 
png_const_charp png_get_header_version (png_const_structrp png_ptr)
 
void png_build_grayscale_palette (int bit_depth, png_colorp palette)
 
int png_handle_as_unknown (png_const_structrp png_ptr, png_const_bytep chunk_name)
 
int png_chunk_unknown_handling (png_const_structrp png_ptr, png_uint_32 chunk_name)
 
int png_reset_zstream (png_structrp png_ptr)
 
png_uint_32 png_access_version_number (void)
 
void png_zstream_error (png_structrp png_ptr, int ret)
 
static int png_colorspace_check_gamma (png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA, int from)
 
void png_colorspace_set_gamma (png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA)
 
void png_colorspace_sync_info (png_const_structrp png_ptr, png_inforp info_ptr)
 
void png_colorspace_sync (png_const_structrp png_ptr, png_inforp info_ptr)
 
static int png_xy_from_XYZ (png_xy *xy, const png_XYZ *XYZ)
 
static int png_XYZ_from_xy (png_XYZ *XYZ, const png_xy *xy)
 
static int png_XYZ_normalize (png_XYZ *XYZ)
 
static int png_colorspace_endpoints_match (const png_xy *xy1, const png_xy *xy2, int delta)
 
static int png_colorspace_check_xy (png_XYZ *XYZ, const png_xy *xy)
 
static int png_colorspace_check_XYZ (png_xy *xy, png_XYZ *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)
 
int png_colorspace_set_chromaticities (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, int preferred)
 
int png_colorspace_set_endpoints (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred)
 
static char png_icc_tag_char (png_uint_32 byte)
 
static void png_icc_tag_name (char *name, png_uint_32 tag)
 
static int is_ICC_signature_char (png_alloc_size_t it)
 
static int is_ICC_signature (png_alloc_size_t it)
 
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)
 
int png_colorspace_set_sRGB (png_const_structrp png_ptr, png_colorspacerp colorspace, int intent)
 
int png_icc_check_length (png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length)
 
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)
 
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)
 
static int png_compare_ICC_profile_with_sRGB (png_const_structrp png_ptr, png_const_bytep profile, uLong adler)
 
void png_icc_set_sRGB (png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
 
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)
 
void png_colorspace_set_rgb_coefficients (png_structrp png_ptr)
 
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)
 
int png_check_fp_number (png_const_charp string, png_size_t size, int *statep, png_size_tp whereami)
 
int png_check_fp_string (png_const_charp string, png_size_t size)
 
static double png_pow10 (int power)
 
void png_ascii_from_fp (png_const_structrp png_ptr, png_charp ascii, png_size_t size, double fp, unsigned int precision)
 
void png_ascii_from_fixed (png_const_structrp png_ptr, png_charp ascii, png_size_t size, png_fixed_point fp)
 
png_fixed_point png_fixed (png_const_structrp png_ptr, double fp, png_const_charp text)
 
int png_muldiv (png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
 
png_fixed_point png_muldiv_warn (png_const_structrp png_ptr, png_fixed_point a, png_int_32 times, png_int_32 divisor)
 
png_fixed_point png_reciprocal (png_fixed_point a)
 
int png_gamma_significant (png_fixed_point gamma_val)
 
static png_fixed_point png_product2 (png_fixed_point a, png_fixed_point b)
 
png_fixed_point png_reciprocal2 (png_fixed_point a, png_fixed_point b)
 
png_byte png_gamma_8bit_correct (unsigned int value, png_fixed_point gamma_val)
 
png_uint_16 png_gamma_16bit_correct (unsigned int value, png_fixed_point gamma_val)
 
png_uint_16 png_gamma_correct (png_structrp png_ptr, unsigned int value, png_fixed_point gamma_val)
 
static void png_build_16bit_table (png_structrp png_ptr, png_uint_16pp *ptable, const unsigned int shift, const png_fixed_point gamma_val)
 
static void png_build_16to8_table (png_structrp png_ptr, png_uint_16pp *ptable, const unsigned int shift, const png_fixed_point gamma_val)
 
static void png_build_8bit_table (png_structrp png_ptr, png_bytepp ptable, const png_fixed_point gamma_val)
 
void png_destroy_gamma_table (png_structrp png_ptr)
 
void png_build_gamma_table (png_structrp png_ptr, int bit_depth)
 
int png_set_option (png_structrp png_ptr, int option, int onoff)
 
static int png_image_free_function (png_voidp argument)
 
void png_image_free (png_imagep image)
 
int png_image_error (png_imagep image, png_const_charp error_message)
 

Variables

static const png_xy sRGB_xy
 
static const png_byte D50_nCIEXYZ [12]
 
struct {
   png_uint_32   adler
 
   png_uint_32   crc
 
   png_uint_32   length
 
   png_uint_32   md5 [4]
 
   png_byte   have_md5
 
   png_byte   is_broken
 
   png_uint_16   intent
 
png_sRGB_checks []
 
const png_uint_16 png_sRGB_table [256]
 
const png_uint_16 png_sRGB_base [512]
 
const png_byte png_sRGB_delta [512]
 

Macro Definition Documentation

#define APPEND_STRING (   string)    pos = png_safecat(out, 29, pos, (string))
#define APPEND_NUMBER (   format,
  value 
)    APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value)))
#define APPEND (   ch)    if (pos < 28) out[pos++] = (ch)
#define PNG_MD5 (   a,
  b,
  c,
 
)    { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0)
#define PNG_ICC_CHECKSUM (   adler,
  crc,
  md5,
  intent,
  broke,
  date,
  length,
  fname 
)    { adler, crc, length, md5, broke, intent },
#define png_gt (   a,
  b 
)    ((a) > (b))
#define png_fp_add (   state,
  flags 
)    ((state) |= (flags))
#define png_fp_set (   state,
  value 
)    ((state) = (value) | ((state) & PNG_FP_STICKY))

Typedef Documentation

Function Documentation

void 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 }
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte sig_bytes
Definition: pngstruct.h:260
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
int png_sig_cmp ( png_const_bytep  sig,
png_size_t  start,
png_size_t  num_to_check 
)
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 }
voidpf png_zalloc ( voidpf  png_ptr,
uInt  items,
uInt  size 
)
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 }
PNG_IMPEXP png_voidp() png_malloc_warn(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:210
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
Definition: pngstruct.h:144
#define png_voidcast(type, value)
Definition: pngpriv.h:408
png_size_t png_alloc_size_t
Definition: pngconf.h:578
void png_zfree ( voidpf  png_ptr,
voidpf  ptr 
)
94 {
96 }
Definition: pngstruct.h:144
#define png_voidcast(type, value)
Definition: pngpriv.h:408
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void png_reset_crc ( png_structrp  png_ptr)
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 }
png_uint_32 crc
Definition: pngstruct.h:237
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:204
#define Z_NULL
Definition: zlib.h:208
void png_calculate_crc ( png_structrp  png_ptr,
png_const_bytep  ptr,
png_size_t  length 
)
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 }
#define PNG_CHUNK_ANCILLARY(c)
Definition: pngpriv.h:798
unsigned long uLong
Definition: zconf.h:371
png_uint_32 crc
Definition: png.c:2173
png_uint_32 crc
Definition: pngstruct.h:237
png_uint_32 chunk_name
Definition: pngstruct.h:221
#define PNG_FLAG_CRC_ANCILLARY_USE
Definition: pngpriv.h:609
#define PNG_FLAG_CRC_CRITICAL_IGNORE
Definition: pngpriv.h:612
png_uint_32 flags
Definition: pngstruct.h:180
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:204
#define PNG_FLAG_CRC_ANCILLARY_NOWARN
Definition: pngpriv.h:610
png_uint_32 length
Definition: png.c:2173
#define PNG_FLAG_CRC_ANCILLARY_MASK
Definition: pngpriv.h:633
unsigned int uInt
Definition: zconf.h:370
int png_user_version_check ( png_structrp  png_ptr,
png_const_charp  user_png_ver 
)
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
int i
Definition: rw_test.cpp:37
#define PNG_LIBPNG_VER_STRING
Definition: png.h:426
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
png_uint_32 flags
Definition: pngstruct.h:180
#define PNG_FLAG_LIBRARY_MISMATCH
Definition: pngpriv.h:618
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 
)
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 }
void png_zfree(voidpf png_ptr, voidpf ptr)
Definition: png.c:93
z_stream zstream
Definition: pngstruct.h:184
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
PNG_IMPEXP png_voidp() png_malloc_warn(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:210
Definition: pngstruct.h:144
#define png_voidcast(type, value)
Definition: pngpriv.h:408
#define PNG_USER_CHUNK_MALLOC_MAX
Definition: pngpriv.h:336
voidpf opaque
Definition: zlib.h:99
free_func zfree
Definition: zlib.h:98
voidpf png_zalloc(voidpf png_ptr, uInt items, uInt size)
Definition: png.c:73
#define PNG_USER_WIDTH_MAX
Definition: pngpriv.h:327
int png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver)
Definition: png.c:166
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
alloc_func zalloc
Definition: zlib.h:97
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
#define PNG_USER_HEIGHT_MAX
Definition: pngpriv.h:330
#define PNG_USER_CHUNK_CACHE_MAX
Definition: pngpriv.h:333
png_infop png_create_info_struct ( png_const_structrp  png_ptr)
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 }
#define png_debug(l, m)
Definition: pngdebug.h:146
#define png_voidcast(type, value)
Definition: pngpriv.h:408
Definition: pnginfo.h:56
png_voidp png_malloc_base(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:69
void png_destroy_info_struct ( png_const_structrp  png_ptr,
png_infopp  info_ptr_ptr 
)
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 }
#define png_debug(l, m)
Definition: pngdebug.h:146
Definition: pnginfo.h:56
void png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num)
Definition: png.c:442
#define PNG_FREE_ALL
Definition: png.h:1925
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void 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);
412  info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL,
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 }
#define png_debug(l, m)
Definition: pngdebug.h:146
#define png_voidcast(type, value)
Definition: pngpriv.h:408
Definition: pnginfo.h:56
png_voidp png_malloc_base(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:69
void png_data_freer ( png_const_structrp  png_ptr,
png_inforp  info_ptr,
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 }
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_USER_WILL_FREE_DATA
Definition: png.h:1910
#define PNG_DESTROY_WILL_FREE_DATA
Definition: png.h:1908
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_uint_32 free_me
Definition: pnginfo.h:221
void png_free_data ( png_const_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  mask,
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 }
#define PNG_FREE_TEXT
Definition: png.h:1924
#define PNG_FREE_HIST
Definition: png.h:1912
#define PNG_INFO_hIST
Definition: png.h:879
int i
Definition: rw_test.cpp:37
png_colorp palette
Definition: pnginfo.h:63
#define PNG_FREE_PLTE
Definition: png.h:1922
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_FREE_ICCP
Definition: png.h:1913
#define PNG_FREE_ROWS
Definition: png.h:1915
png_uint_32 height
Definition: pnginfo.h:60
#define PNG_FREE_SPLT
Definition: png.h:1914
png_uint_16 num_palette
Definition: pnginfo.h:64
#define PNG_FREE_UNKN
Definition: png.h:1919
#define PNG_INFO_PLTE
Definition: png.h:876
#define PNG_INFO_IDAT
Definition: png.h:888
#define PNG_INFO_tRNS
Definition: png.h:877
png_uint_32 valid
Definition: pnginfo.h:61
#define PNG_FREE_PCAL
Definition: png.h:1916
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
#define PNG_FREE_MUL
Definition: png.h:1926
#define PNG_INFO_sPLT
Definition: png.h:886
png_uint_32 free_me
Definition: pnginfo.h:221
#define PNG_INFO_pCAL
Definition: png.h:883
#define PNG_FREE_SCAL
Definition: png.h:1917
#define PNG_INFO_iCCP
Definition: png.h:885
#define PNG_FREE_TRNS
Definition: png.h:1923
#define PNG_INFO_sCAL
Definition: png.h:887
png_voidp png_get_io_ptr ( png_const_structrp  png_ptr)
642 {
643  if (png_ptr == NULL)
644  return (NULL);
645 
646  return (png_ptr->io_ptr);
647 }
png_voidp io_ptr
Definition: pngstruct.h:159
void 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 }
void * png_voidp
Definition: pngconf.h:598
#define png_debug(l, m)
Definition: pngdebug.h:146
png_voidp io_ptr
Definition: pngstruct.h:159
FILE * fp
Definition: rw_test.cpp:38
void png_save_int_32 ( png_bytep  buf,
png_int_32  i 
)
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 }
int i
Definition: rw_test.cpp:37
char buf[128]
Definition: rw_test.cpp:40
int png_convert_to_rfc1123_buffer ( char  out[29],
png_const_timep  ptime 
)
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 }
#define APPEND(ch)
#define PNG_NUMBER_FORMAT_02u
Definition: pngpriv.h:1588
png_byte hour
Definition: png.h:746
png_byte second
Definition: png.h:748
#define PNG_NUMBER_FORMAT_u
Definition: pngpriv.h:1587
#define PNG_CONST
Definition: pngconf.h:86
png_byte minute
Definition: png.h:747
#define APPEND_STRING(string)
png_byte day
Definition: png.h:745
png_byte month
Definition: png.h:744
#define APPEND_NUMBER(format, value)
png_uint_16 year
Definition: png.h:743
png_const_charp png_convert_to_rfc1123 ( png_structrp  png_ptr,
png_const_timep  ptime 
)
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 }
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
int png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime)
Definition: png.c:691
png_const_charp png_get_copyright ( png_const_structrp  png_ptr)
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." \
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_STRING_NEWLINE
Definition: pngdebug.h:49
png_const_charp png_get_libpng_ver ( png_const_structrp  png_ptr)
796 {
797  /* Version of *.c files used when building libpng */
798  return png_get_header_ver(png_ptr);
799 }
png_const_charp png_get_header_ver(png_const_structrp png_ptr)
Definition: png.c:802
png_const_charp png_get_header_ver ( png_const_structrp  png_ptr)
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_LIBPNG_VER_STRING
Definition: png.h:426
png_const_charp png_get_header_version ( png_const_structrp  png_ptr)
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_STRING_NEWLINE
Definition: pngdebug.h:49
#define PNG_HEADER_VERSION_STRING
Definition: png.h:427
void png_build_grayscale_palette ( int  bit_depth,
png_colorp  palette 
)
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 }
int i
Definition: rw_test.cpp:37
png_byte red
Definition: png.h:621
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte green
Definition: png.h:622
png_byte blue
Definition: png.h:623
int png_handle_as_unknown ( png_const_structrp  png_ptr,
png_const_bytep  chunk_name 
)
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 }
#define PNG_HANDLE_CHUNK_AS_DEFAULT
Definition: png.h:2497
static double * p
Definition: gauss_jackson_test.cpp:42
const png_byte * png_const_bytep
Definition: pngconf.h:601
int 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 }
int png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name)
Definition: png.c:884
#define PNG_CSTRING_FROM_CHUNK(s, c)
Definition: pngpriv.h:794
int png_reset_zstream ( png_structrp  png_ptr)
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 }
z_stream zstream
Definition: pngstruct.h:184
int ZEXPORT inflateReset(z_streamp strm)
Definition: inflate.c:129
#define Z_STREAM_ERROR
Definition: zlib.h:177
png_uint_32 png_access_version_number ( void  )
945 {
946  /* Version of *.c files used when building libpng */
947  return((png_uint_32)PNG_LIBPNG_VER);
948 }
#define PNG_LIBPNG_VER
Definition: png.h:467
void png_zstream_error ( png_structrp  png_ptr,
int  ret 
)
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 }
#define PNG_UNEXPECTED_ZLIB_RETURN
Definition: pngpriv.h:873
#define PNGZ_MSG_CAST(s)
Definition: pngstruct.h:41
z_stream zstream
Definition: pngstruct.h:184
#define Z_ERRNO
Definition: zlib.h:176
#define Z_NEED_DICT
Definition: zlib.h:175
char * msg
Definition: zlib.h:94
#define Z_STREAM_ERROR
Definition: zlib.h:177
#define Z_DATA_ERROR
Definition: zlib.h:178
#define Z_STREAM_END
Definition: zlib.h:174
#define Z_MEM_ERROR
Definition: zlib.h:179
#define Z_VERSION_ERROR
Definition: zlib.h:181
#define Z_BUF_ERROR
Definition: zlib.h:180
#define Z_OK
Definition: zlib.h:173
static int png_colorspace_check_gamma ( png_const_structrp  png_ptr,
png_colorspacerp  colorspace,
png_fixed_point  gAMA,
int  from 
)
static
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 }
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
#define PNG_CHUNK_ERROR
Definition: pngpriv.h:1674
png_int_32 png_fixed_point
Definition: pngconf.h:595
#define PNG_CHUNK_WARNING
Definition: pngpriv.h:1672
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
int png_gamma_significant(png_fixed_point gamma_val)
Definition: png.c:3400
void png_colorspace_set_gamma ( png_const_structrp  png_ptr,
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 }
const char * png_const_charp
Definition: pngconf.h:611
#define PNG_IS_READ_STRUCT
Definition: pngpriv.h:557
static int png_colorspace_check_gamma(png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA, int from)
Definition: png.c:1030
#define PNG_CHUNK_WRITE_ERROR
Definition: pngpriv.h:1673
png_uint_32 mode
Definition: pngstruct.h:179
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
void png_colorspace_sync_info ( png_const_structrp  png_ptr,
png_inforp  info_ptr 
)
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 }
#define PNG_INFO_gAMA
Definition: png.h:873
#define PNG_INFO_sRGB
Definition: png.h:884
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_FREE_ICCP
Definition: png.h:1913
void png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num)
Definition: png.c:442
png_uint_32 valid
Definition: pnginfo.h:61
#define PNG_INFO_cHRM
Definition: png.h:875
#define PNG_INFO_iCCP
Definition: png.h:885
void png_colorspace_sync ( png_const_structrp  png_ptr,
png_inforp  info_ptr 
)
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 }
void png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr)
Definition: png.c:1129
static int png_xy_from_XYZ ( png_xy *  xy,
const png_XYZ *  XYZ 
)
static
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 }
double d[9]
Definition: nrlmsise-00.h:173
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
static int png_XYZ_from_xy ( png_XYZ *  XYZ,
const png_xy *  xy 
)
static
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 }
png_fixed_point png_reciprocal(png_fixed_point a)
Definition: png.c:3379
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
png_int_32 png_fixed_point
Definition: pngconf.h:595
static int png_XYZ_normalize ( png_XYZ *  XYZ)
static
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 }
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
static int png_colorspace_endpoints_match ( const png_xy *  xy1,
const png_xy *  xy2,
int  delta 
)
static
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 }
static double delta
Definition: agent_exec-2-0.cpp:141
#define PNG_OUT_OF_RANGE(value, ideal, delta)
Definition: pngpriv.h:670
static int png_colorspace_check_xy ( png_XYZ *  XYZ,
const png_xy *  xy 
)
static
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 }
static int png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy)
Definition: png.c:1236
static int png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
Definition: png.c:1551
static int png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ)
Definition: png.c:1193
static int png_colorspace_check_XYZ ( png_xy *  xy,
png_XYZ *  XYZ 
)
static
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 }
static int png_XYZ_normalize(png_XYZ *XYZ)
Definition: png.c:1501
static int png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ)
Definition: png.c:1193
static int png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy)
Definition: png.c:1577
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 
)
static
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 }
static int png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
Definition: png.c:1551
static const png_xy sRGB_xy
Definition: png.c:1621
PNG_IMPEXP void() png_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:362
int png_colorspace_set_chromaticities ( png_const_structrp  png_ptr,
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 }
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_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
static int png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy)
Definition: png.c:1577
PNG_IMPEXP void() png_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:362
int png_colorspace_set_endpoints ( png_const_structrp  png_ptr,
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 }
static int png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ)
Definition: png.c:1603
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_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
PNG_IMPEXP void() png_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:362
static char png_icc_tag_char ( png_uint_32  byte)
static
1748 {
1749  byte &= 0xff;
1750  if (byte >= 32 && byte <= 126)
1751  return (char)byte;
1752  else
1753  return '?';
1754 }
static void png_icc_tag_name ( char *  name,
png_uint_32  tag 
)
static
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 }
string name
Definition: cubesat2obj.cpp:6
static char png_icc_tag_char(png_uint_32 byte)
Definition: png.c:1747
static int is_ICC_signature_char ( png_alloc_size_t  it)
static
1769 {
1770  return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) ||
1771  (it >= 97 && it <= 122);
1772 }
static int is_ICC_signature ( png_alloc_size_t  it)
static
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 }
static int is_ICC_signature_char(png_alloc_size_t it)
Definition: png.c:1768
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 
)
static
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
static void png_icc_tag_name(char *name, png_uint_32 tag)
Definition: png.c:1757
png_charp png_format_number(png_const_charp start, png_charp end, int format, png_alloc_size_t number)
Definition: pngerror.c:133
static int is_ICC_signature(png_alloc_size_t it)
Definition: png.c:1775
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
#define PNG_NUMBER_BUFFER_SIZE
Definition: pngpriv.h:1582
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
string name
Definition: cubesat2obj.cpp:6
#define PNG_CHUNK_WRITE_ERROR
Definition: pngpriv.h:1673
#define PNG_CHUNK_ERROR
Definition: pngpriv.h:1674
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
#define PNG_NUMBER_FORMAT_x
Definition: pngpriv.h:1591
int png_colorspace_set_sRGB ( png_const_structrp  png_ptr,
png_colorspacerp  colorspace,
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 }
#define PNG_sRGB_INTENT_LAST
Definition: png.h:860
png_uint_16 intent
Definition: png.c:2177
static int png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta)
Definition: png.c:1551
static int png_colorspace_check_gamma(png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA, int from)
Definition: png.c:1030
static const png_xy sRGB_xy
Definition: png.c:1621
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
#define PNG_CHUNK_ERROR
Definition: pngpriv.h:1674
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
#define PNG_GAMMA_sRGB_INVERSE
Definition: pngpriv.h:807
PNG_IMPEXP void() png_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:362
int png_icc_check_length ( png_const_structrp  png_ptr,
png_colorspacerp  colorspace,
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 }
string name
Definition: cubesat2obj.cpp:6
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
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 
)
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 }
#define png_get_uint_32(buf)
Definition: png.h:2744
#define PNG_sRGB_INTENT_LAST
Definition: png.h:860
static const png_byte D50_nCIEXYZ[12]
Definition: png.c:1927
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
string name
Definition: cubesat2obj.cpp:6
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
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 
)
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 }
#define png_get_uint_32(buf)
Definition: png.h:2744
const png_byte * png_const_bytep
Definition: pngconf.h:601
string name
Definition: cubesat2obj.cpp:6
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
static int png_compare_ICC_profile_with_sRGB ( png_const_structrp  png_ptr,
png_const_bytep  profile,
uLong  adler 
)
static
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 &&
2283  intent == png_sRGB_checks[i].intent)
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 }
#define png_get_uint_32(buf)
Definition: png.h:2744
int i
Definition: rw_test.cpp:37
unsigned long uLong
Definition: zconf.h:371
png_uint_32 crc
Definition: png.c:2173
#define PNG_OPTION_ON
Definition: png.h:3255
png_uint_16 intent
Definition: png.c:2177
unsigned long ZEXPORT crc32(unsigned long crc, const unsigned char FAR *buf, uInt len)
Definition: crc32.c:204
png_byte is_broken
Definition: png.c:2176
static const struct @45 png_sRGB_checks[]
uLong ZEXPORT adler32(uLong adler, const Bytef *buf, uInt len)
Definition: adler32.c:65
png_uint_32 length
Definition: png.c:2173
#define PNG_CHUNK_ERROR
Definition: pngpriv.h:1674
#define PNG_CHUNK_WARNING
Definition: pngpriv.h:1672
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
png_byte have_md5
Definition: png.c:2175
#define PNG_SKIP_sRGB_CHECK_PROFILE
Definition: png.h:3248
png_uint_32 adler
Definition: png.c:2173
void png_icc_set_sRGB ( png_const_structrp  png_ptr,
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 }
static int png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr, png_const_bytep profile, uLong adler)
Definition: png.c:2233
#define png_get_uint_32(buf)
Definition: png.h:2744
int png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, int intent)
Definition: png.c:1833
png_uint_32 adler
Definition: png.c:2173
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 
)
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 }
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
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
string name
Definition: cubesat2obj.cpp:6
void png_icc_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
Definition: png.c:2356
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
void png_colorspace_set_rgb_coefficients ( png_structrp  png_ptr)
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 }
long b
Definition: jpegint.h:371
png_uint_32 flags
Definition: pngstruct.h:180
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_int_32 png_fixed_point
Definition: pngconf.h:595
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
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 
)
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 }
#define PNG_INTERLACE_LAST
Definition: png.h:830
#define PNG_UINT_31_MAX
Definition: png.h:790
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
#define PNG_USER_WIDTH_MAX
Definition: pngpriv.h:327
#define PNG_SIZE_MAX
Definition: png.h:792
#define PNG_FLAG_MNG_FILTER_64
Definition: png.h:996
#define PNG_FILTER_TYPE_BASE
Definition: png.h:823
#define PNG_USER_HEIGHT_MAX
Definition: pngpriv.h:330
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
png_uint_32 mode
Definition: pngstruct.h:179
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define PNG_INTRAPIXEL_DIFFERENCING
Definition: png.h:824
#define png_gt(a, b)
Definition: png.c:2472
#define PNG_HAVE_PNG_SIGNATURE
Definition: pngpriv.h:554
int png_check_fp_number ( png_const_charp  string,
png_size_t  size,
int *  statep,
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 
2693  case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT:
2694  if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */
2695  png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT);
2696 
2697  png_fp_add(state, type | PNG_FP_WAS_VALID);
2698 
2699  break;
2700 
2702  if ((state & PNG_FP_SAW_DIGIT) == 0)
2703  goto PNG_FP_End;
2704 
2705  png_fp_set(state, PNG_FP_EXPONENT);
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 
2715  case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT:
2716  png_fp_add(state, type | PNG_FP_WAS_VALID);
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 
2727  png_fp_set(state, PNG_FP_EXPONENT);
2728 
2729  break;
2730 
2732  if ((state & PNG_FP_SAW_ANY) != 0)
2733  goto PNG_FP_End; /* not a part of the number */
2734 
2735  png_fp_add(state, PNG_FP_SAW_SIGN);
2736 
2737  break;
2738 
2739  /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT:
2740  goto PNG_FP_End; */
2741 
2742  case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT:
2743  png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID);
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 }
int i
Definition: rw_test.cpp:37
#define PNG_FP_SAW_E
Definition: pngpriv.h:1737
#define PNG_FP_SAW_SIGN
Definition: pngpriv.h:1734
#define PNG_FP_FRACTION
Definition: pngpriv.h:1731
#define PNG_FP_SAW_ANY
Definition: pngpriv.h:1738
#define PNG_FP_EXPONENT
Definition: pngpriv.h:1732
#define PNG_FP_NONZERO
Definition: pngpriv.h:1744
#define PNG_FP_SAW_DIGIT
Definition: pngpriv.h:1735
#define PNG_FP_STATE
Definition: pngpriv.h:1733
#define PNG_FP_NEGATIVE
Definition: pngpriv.h:1743
#define PNG_FP_INTEGER
Definition: pngpriv.h:1730
size_t png_size_t
Definition: pngconf.h:543
#define PNG_FP_SAW_DOT
Definition: pngpriv.h:1736
#define png_fp_add(state, flags)
Definition: png.c:2639
#define PNG_FP_WAS_VALID
Definition: pngpriv.h:1742
#define png_fp_set(state, value)
Definition: png.c:2640
int png_check_fp_string ( png_const_charp  string,
png_size_t  size 
)
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 }
int png_check_fp_number(png_const_charp string, png_size_t size, int *statep, png_size_tp whereami)
Definition: png.c:2643
size_t png_size_t
Definition: pngconf.h:543
static double png_pow10 ( int  power)
static
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 }
double d[9]
Definition: nrlmsise-00.h:173
void png_ascii_from_fp ( png_const_structrp  png_ptr,
png_charp  ascii,
png_size_t  size,
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 }
double d[9]
Definition: nrlmsise-00.h:173
void test()
Definition: testprofiler.c:34
FILE * fp
Definition: rw_test.cpp:38
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
static double png_pow10(int power)
Definition: png.c:2789
void png_ascii_from_fixed ( png_const_structrp  png_ptr,
png_charp  ascii,
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 }
int i
Definition: rw_test.cpp:37
FILE * fp
Definition: rw_test.cpp:38
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_fixed_point png_fixed ( png_const_structrp  png_ptr,
double  fp,
png_const_charp  text 
)
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 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
void png_fixed_error(png_const_structrp png_ptr, png_const_charp name)
Definition: pngerror.c:572
png_int_32 png_fixed_point
Definition: pngconf.h:595
FILE * fp
Definition: rw_test.cpp:38
int png_muldiv ( png_fixed_point_p  res,
png_fixed_point  a,
png_int_32  times,
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 }
Definition: eci2kep_test.cpp:33
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_fixed_point png_muldiv_warn ( png_const_structrp  png_ptr,
png_fixed_point  a,
png_int_32  times,
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 }
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
Definition: eci2kep_test.cpp:33
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_fixed_point png_reciprocal ( png_fixed_point  a)
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 }
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
Definition: eci2kep_test.cpp:33
png_int_32 png_fixed_point
Definition: pngconf.h:595
int png_gamma_significant ( png_fixed_point  gamma_val)
3401 {
3402  return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED ||
3403  gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED;
3404 }
#define PNG_GAMMA_THRESHOLD_FIXED
Definition: pnglibconf.h:192
#define PNG_FP_1
Definition: png.h:797
static png_fixed_point png_product2 ( png_fixed_point  a,
png_fixed_point  b 
)
static
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 }
long b
Definition: jpegint.h:371
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
Definition: eci2kep_test.cpp:33
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_fixed_point png_reciprocal2 ( png_fixed_point  a,
png_fixed_point  b 
)
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 }
static png_fixed_point png_product2(png_fixed_point a, png_fixed_point b)
Definition: png.c:3411
png_fixed_point png_reciprocal(png_fixed_point a)
Definition: png.c:3379
long b
Definition: jpegint.h:371
Definition: eci2kep_test.cpp:33
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_byte 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 }
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_uint_16 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 }
int png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, png_int_32 divisor)
Definition: png.c:3243
#define PNG_FP_1
Definition: png.h:797
png_int_32 png_fixed_point
Definition: pngconf.h:595
png_uint_16 png_gamma_correct ( png_structrp  png_ptr,
unsigned int  value,
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 }
png_byte bit_depth
Definition: pngstruct.h:253
png_byte png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3752
png_uint_16 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3800
static void png_build_16bit_table ( png_structrp  png_ptr,
png_uint_16pp ptable,
const unsigned int  shift,
const png_fixed_point  gamma_val 
)
static
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 }
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
double d[9]
Definition: nrlmsise-00.h:173
int i
Definition: rw_test.cpp:37
#define PNG_CONST
Definition: pngconf.h:86
png_uint_16 ** png_uint_16pp
Definition: pngconf.h:630
PNG_IMPEXP png_voidp() png_calloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:50
png_uint_16 * png_uint_16p
Definition: pngconf.h:606
png_uint_16 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3800
int png_gamma_significant(png_fixed_point gamma_val)
Definition: png.c:3400
static void png_build_16to8_table ( png_structrp  png_ptr,
png_uint_16pp ptable,
const unsigned int  shift,
const png_fixed_point  gamma_val 
)
static
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 }
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
int i
Definition: rw_test.cpp:37
#define PNG_CONST
Definition: pngconf.h:86
png_uint_16 ** png_uint_16pp
Definition: pngconf.h:630
PNG_IMPEXP png_voidp() png_calloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:50
png_uint_16 * png_uint_16p
Definition: pngconf.h:606
png_uint_16 png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3800
static void png_build_8bit_table ( png_structrp  png_ptr,
png_bytepp  ptable,
const png_fixed_point  gamma_val 
)
static
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 }
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
png_byte png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3752
int png_gamma_significant(png_fixed_point gamma_val)
Definition: png.c:3400
void png_destroy_gamma_table ( png_structrp  png_ptr)
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 }
int i
Definition: rw_test.cpp:37
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void 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 }
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
#define PNG_COMPOSE
Definition: pngpriv.h:567
static png_fixed_point png_product2(png_fixed_point a, png_fixed_point b)
Definition: png.c:3411
#define PNG_SCALE_16_TO_8
Definition: pngpriv.h:587
png_fixed_point png_reciprocal(png_fixed_point a)
Definition: png.c:3379
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte color_type
Definition: pngstruct.h:252
#define PNG_MAX_GAMMA_8
Definition: pnglibconf.h:195
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
static void png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable, const png_fixed_point gamma_val)
Definition: png.c:4005
void png_destroy_gamma_table(png_structrp png_ptr)
Definition: png.c:4024
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_fixed_point png_reciprocal2(png_fixed_point a, png_fixed_point b)
Definition: png.c:3434
#define PNG_FP_1
Definition: png.h:797
#define PNG_RGB_TO_GRAY
Definition: pngpriv.h:583
#define PNG_16_TO_8
Definition: pngpriv.h:570
png_uint_32 transformations
Definition: pngstruct.h:181
int 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 }
#define PNG_OPTION_NEXT
Definition: png.h:3249
JCOPY_OPTION option
Definition: transupp.h:131
static antstruc current
Definition: agent_antenna.cpp:161
#define PNG_OPTION_INVALID
Definition: png.h:3253
static int png_image_free_function ( png_voidp  argument)
static
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 }
uint8_t image[110000000]
Definition: gige_snap.cpp:38
png_controlp opaque
Definition: png.h:2809
Definition: pngpriv.h:1865
Definition: png.h:2807
#define png_voidcast(type, value)
Definition: pngpriv.h:408
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_structp png_ptr
Definition: pngpriv.h:1867
png_voidp io_ptr
Definition: pngstruct.h:159
unsigned int owned_file
Definition: pngpriv.h:1875
PNG_IMPEXP void() png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
Definition: pngwrite.c:979
png_infop info_ptr
Definition: pngpriv.h:1868
unsigned int for_write
Definition: pngpriv.h:1874
FILE * fp
Definition: rw_test.cpp:38
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
void png_image_free ( png_imagep  image)
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 }
static int png_image_free_function(png_voidp argument)
Definition: png.c:4407
png_controlp opaque
Definition: png.h:2809
int png_safe_execute(png_imagep image_in, int(*function)(png_voidp), png_voidp arg)
Definition: pngerror.c:936
png_voidp error_buf
Definition: pngpriv.h:1869
int png_image_error ( png_imagep  image,
png_const_charp  error_message 
)
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 }
char message[64]
Definition: png.h:2842
png_uint_32 warning_or_error
Definition: png.h:2840
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
#define PNG_IMAGE_ERROR
Definition: png.h:2828
void png_image_free(png_imagep image)
Definition: png.c:4467

Variable Documentation

const png_xy sRGB_xy
static
Initial value:
=
{
64000, 33000,
30000, 60000,
15000, 6000,
31270, 32900
}
const png_byte D50_nCIEXYZ[12]
static
Initial value:
=
{ 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d }
png_uint_32 adler
png_uint_32 crc
png_uint_32 length
png_uint_32 md5[4]
png_byte have_md5
png_byte is_broken
png_uint_16 intent
const { ... } png_sRGB_checks[]
const png_uint_16 png_sRGB_table[256]
const png_uint_16 png_sRGB_base[512]
const png_byte png_sRGB_delta[512]