2 /* png.c - location for general purpose libpng functions
\r
4 * Last changed in libpng 1.4.2 [May 6, 2010]
\r
5 * Copyright (c) 1998-2010 Glenn Randers-Pehrson
\r
6 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
\r
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
\r
9 * This code is released under the libpng license.
\r
10 * For conditions of distribution and use, see the disclaimer
\r
11 * and license in png.h
\r
14 #define PNG_NO_EXTERN
\r
15 #define PNG_NO_PEDANTIC_WARNINGS
\r
17 #include "pngpriv.h"
\r
19 /* Generate a compiler error if there is an old png.h in the search path. */
\r
20 typedef version_1_4_3 Your_png_h_is_not_version_1_4_3;
\r
22 /* Version information for C files. This had better match the version
\r
23 * string defined in png.h.
\r
26 /* Tells libpng that we have already handled the first "num_bytes" bytes
\r
27 * of the PNG file signature. If the PNG data is embedded into another
\r
28 * stream we can set num_bytes = 8 so that libpng will not attempt to read
\r
29 * or write any of the magic bytes before it starts on the IHDR.
\r
32 #ifdef PNG_READ_SUPPORTED
\r
34 png_set_sig_bytes(png_structp png_ptr, int num_bytes)
\r
36 png_debug(1, "in png_set_sig_bytes");
\r
38 if (png_ptr == NULL)
\r
42 png_error(png_ptr, "Too many bytes for PNG signature");
\r
44 png_ptr->sig_bytes = (png_byte)(num_bytes < 0 ? 0 : num_bytes);
\r
47 /* Checks whether the supplied bytes match the PNG signature. We allow
\r
48 * checking less than the full 8-byte signature so that those apps that
\r
49 * already read the first few bytes of a file to determine the file type
\r
50 * can simply check the remaining bytes for extra assurance. Returns
\r
51 * an integer less than, equal to, or greater than zero if sig is found,
\r
52 * respectively, to be less than, to match, or be greater than the correct
\r
53 * PNG signature (this is the same behaviour as strcmp, memcmp, etc).
\r
56 png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
\r
58 png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
\r
59 if (num_to_check > 8)
\r
61 else if (num_to_check < 1)
\r
67 if (start + num_to_check > 8)
\r
68 num_to_check = 8 - start;
\r
70 return ((int)(png_memcmp(&sig[start], &png_signature[start], num_to_check)));
\r
73 #endif /* PNG_READ_SUPPORTED */
\r
75 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
\r
76 /* Function to allocate memory for zlib and clear it to 0. */
\r
77 voidpf /* PRIVATE */
\r
78 png_zalloc(voidpf png_ptr, uInt items, uInt size)
\r
81 png_structp p=(png_structp)png_ptr;
\r
82 png_uint_32 save_flags=p->flags;
\r
83 png_alloc_size_t num_bytes;
\r
85 if (png_ptr == NULL)
\r
87 if (items > PNG_UINT_32_MAX/size)
\r
89 png_warning (p, "Potential overflow in png_zalloc()");
\r
92 num_bytes = (png_alloc_size_t)items * size;
\r
94 p->flags|=PNG_FLAG_MALLOC_NULL_MEM_OK;
\r
95 ptr = (png_voidp)png_malloc((png_structp)png_ptr, num_bytes);
\r
96 p->flags=save_flags;
\r
98 return ((voidpf)ptr);
\r
101 /* Function to free memory for zlib */
\r
103 png_zfree(voidpf png_ptr, voidpf ptr)
\r
105 png_free((png_structp)png_ptr, (png_voidp)ptr);
\r
108 /* Reset the CRC variable to 32 bits of 1's. Care must be taken
\r
109 * in case CRC is > 32 bits to leave the top bits 0.
\r
112 png_reset_crc(png_structp png_ptr)
\r
114 png_ptr->crc = crc32(0, Z_NULL, 0);
\r
117 /* Calculate the CRC over a section of data. We can only pass as
\r
118 * much data to this routine as the largest single buffer size. We
\r
119 * also check that this data will actually be used before going to the
\r
120 * trouble of calculating it.
\r
123 png_calculate_crc(png_structp png_ptr, png_bytep ptr, png_size_t length)
\r
127 if (png_ptr->chunk_name[0] & 0x20) /* ancillary */
\r
129 if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
\r
130 (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN))
\r
133 else /* critical */
\r
135 if (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE)
\r
140 png_ptr->crc = crc32(png_ptr->crc, ptr, (uInt)length);
\r
143 /* Allocate the memory for an info_struct for the application. We don't
\r
144 * really need the png_ptr, but it could potentially be useful in the
\r
145 * future. This should be used in favour of malloc(png_sizeof(png_info))
\r
146 * and png_info_init() so that applications that want to use a shared
\r
147 * libpng don't have to be recompiled if png_info changes size.
\r
150 png_create_info_struct(png_structp png_ptr)
\r
152 png_infop info_ptr;
\r
154 png_debug(1, "in png_create_info_struct");
\r
156 if (png_ptr == NULL)
\r
159 #ifdef PNG_USER_MEM_SUPPORTED
\r
160 info_ptr = (png_infop)png_create_struct_2(PNG_STRUCT_INFO,
\r
161 png_ptr->malloc_fn, png_ptr->mem_ptr);
\r
163 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
\r
165 if (info_ptr != NULL)
\r
166 png_info_init_3(&info_ptr, png_sizeof(png_info));
\r
171 /* This function frees the memory associated with a single info struct.
\r
172 * Normally, one would use either png_destroy_read_struct() or
\r
173 * png_destroy_write_struct() to free an info struct, but this may be
\r
174 * useful for some applications.
\r
177 png_destroy_info_struct(png_structp png_ptr, png_infopp info_ptr_ptr)
\r
179 png_infop info_ptr = NULL;
\r
181 png_debug(1, "in png_destroy_info_struct");
\r
183 if (png_ptr == NULL)
\r
186 if (info_ptr_ptr != NULL)
\r
187 info_ptr = *info_ptr_ptr;
\r
189 if (info_ptr != NULL)
\r
191 png_info_destroy(png_ptr, info_ptr);
\r
193 #ifdef PNG_USER_MEM_SUPPORTED
\r
194 png_destroy_struct_2((png_voidp)info_ptr, png_ptr->free_fn,
\r
197 png_destroy_struct((png_voidp)info_ptr);
\r
199 *info_ptr_ptr = NULL;
\r
203 /* Initialize the info structure. This is now an internal function (0.89)
\r
204 * and applications using it are urged to use png_create_info_struct()
\r
209 png_info_init_3(png_infopp ptr_ptr, png_size_t png_info_struct_size)
\r
211 png_infop info_ptr = *ptr_ptr;
\r
213 png_debug(1, "in png_info_init_3");
\r
215 if (info_ptr == NULL)
\r
218 if (png_sizeof(png_info) > png_info_struct_size)
\r
220 png_destroy_struct(info_ptr);
\r
221 info_ptr = (png_infop)png_create_struct(PNG_STRUCT_INFO);
\r
222 *ptr_ptr = info_ptr;
\r
225 /* Set everything to 0 */
\r
226 png_memset(info_ptr, 0, png_sizeof(png_info));
\r
230 png_data_freer(png_structp png_ptr, png_infop info_ptr,
\r
231 int freer, png_uint_32 mask)
\r
233 png_debug(1, "in png_data_freer");
\r
235 if (png_ptr == NULL || info_ptr == NULL)
\r
238 if (freer == PNG_DESTROY_WILL_FREE_DATA)
\r
239 info_ptr->free_me |= mask;
\r
240 else if (freer == PNG_USER_WILL_FREE_DATA)
\r
241 info_ptr->free_me &= ~mask;
\r
243 png_warning(png_ptr,
\r
244 "Unknown freer parameter in png_data_freer");
\r
248 png_free_data(png_structp png_ptr, png_infop info_ptr, png_uint_32 mask,
\r
251 png_debug(1, "in png_free_data");
\r
253 if (png_ptr == NULL || info_ptr == NULL)
\r
256 #ifdef PNG_TEXT_SUPPORTED
\r
257 /* Free text item num or (if num == -1) all text items */
\r
258 if ((mask & PNG_FREE_TEXT) & info_ptr->free_me)
\r
262 if (info_ptr->text && info_ptr->text[num].key)
\r
264 png_free(png_ptr, info_ptr->text[num].key);
\r
265 info_ptr->text[num].key = NULL;
\r
271 for (i = 0; i < info_ptr->num_text; i++)
\r
272 png_free_data(png_ptr, info_ptr, PNG_FREE_TEXT, i);
\r
273 png_free(png_ptr, info_ptr->text);
\r
274 info_ptr->text = NULL;
\r
275 info_ptr->num_text=0;
\r
280 #ifdef PNG_tRNS_SUPPORTED
\r
281 /* Free any tRNS entry */
\r
282 if ((mask & PNG_FREE_TRNS) & info_ptr->free_me)
\r
284 png_free(png_ptr, info_ptr->trans_alpha);
\r
285 info_ptr->trans_alpha = NULL;
\r
286 info_ptr->valid &= ~PNG_INFO_tRNS;
\r
290 #ifdef PNG_sCAL_SUPPORTED
\r
291 /* Free any sCAL entry */
\r
292 if ((mask & PNG_FREE_SCAL) & info_ptr->free_me)
\r
294 #if defined(PNG_FIXED_POINT_SUPPORTED) && !defined(PNG_FLOATING_POINT_SUPPORTED)
\r
295 png_free(png_ptr, info_ptr->scal_s_width);
\r
296 png_free(png_ptr, info_ptr->scal_s_height);
\r
297 info_ptr->scal_s_width = NULL;
\r
298 info_ptr->scal_s_height = NULL;
\r
300 info_ptr->valid &= ~PNG_INFO_sCAL;
\r
304 #ifdef PNG_pCAL_SUPPORTED
\r
305 /* Free any pCAL entry */
\r
306 if ((mask & PNG_FREE_PCAL) & info_ptr->free_me)
\r
308 png_free(png_ptr, info_ptr->pcal_purpose);
\r
309 png_free(png_ptr, info_ptr->pcal_units);
\r
310 info_ptr->pcal_purpose = NULL;
\r
311 info_ptr->pcal_units = NULL;
\r
312 if (info_ptr->pcal_params != NULL)
\r
315 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
\r
317 png_free(png_ptr, info_ptr->pcal_params[i]);
\r
318 info_ptr->pcal_params[i] = NULL;
\r
320 png_free(png_ptr, info_ptr->pcal_params);
\r
321 info_ptr->pcal_params = NULL;
\r
323 info_ptr->valid &= ~PNG_INFO_pCAL;
\r
327 #ifdef PNG_iCCP_SUPPORTED
\r
328 /* Free any iCCP entry */
\r
329 if ((mask & PNG_FREE_ICCP) & info_ptr->free_me)
\r
331 png_free(png_ptr, info_ptr->iccp_name);
\r
332 png_free(png_ptr, info_ptr->iccp_profile);
\r
333 info_ptr->iccp_name = NULL;
\r
334 info_ptr->iccp_profile = NULL;
\r
335 info_ptr->valid &= ~PNG_INFO_iCCP;
\r
339 #ifdef PNG_sPLT_SUPPORTED
\r
340 /* Free a given sPLT entry, or (if num == -1) all sPLT entries */
\r
341 if ((mask & PNG_FREE_SPLT) & info_ptr->free_me)
\r
345 if (info_ptr->splt_palettes)
\r
347 png_free(png_ptr, info_ptr->splt_palettes[num].name);
\r
348 png_free(png_ptr, info_ptr->splt_palettes[num].entries);
\r
349 info_ptr->splt_palettes[num].name = NULL;
\r
350 info_ptr->splt_palettes[num].entries = NULL;
\r
355 if (info_ptr->splt_palettes_num)
\r
358 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
\r
359 png_free_data(png_ptr, info_ptr, PNG_FREE_SPLT, i);
\r
361 png_free(png_ptr, info_ptr->splt_palettes);
\r
362 info_ptr->splt_palettes = NULL;
\r
363 info_ptr->splt_palettes_num = 0;
\r
365 info_ptr->valid &= ~PNG_INFO_sPLT;
\r
370 #ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED
\r
371 if (png_ptr->unknown_chunk.data)
\r
373 png_free(png_ptr, png_ptr->unknown_chunk.data);
\r
374 png_ptr->unknown_chunk.data = NULL;
\r
377 if ((mask & PNG_FREE_UNKN) & info_ptr->free_me)
\r
381 if (info_ptr->unknown_chunks)
\r
383 png_free(png_ptr, info_ptr->unknown_chunks[num].data);
\r
384 info_ptr->unknown_chunks[num].data = NULL;
\r
391 if (info_ptr->unknown_chunks_num)
\r
393 for (i = 0; i < (int)info_ptr->unknown_chunks_num; i++)
\r
394 png_free_data(png_ptr, info_ptr, PNG_FREE_UNKN, i);
\r
396 png_free(png_ptr, info_ptr->unknown_chunks);
\r
397 info_ptr->unknown_chunks = NULL;
\r
398 info_ptr->unknown_chunks_num = 0;
\r
404 #ifdef PNG_hIST_SUPPORTED
\r
405 /* Free any hIST entry */
\r
406 if ((mask & PNG_FREE_HIST) & info_ptr->free_me)
\r
408 png_free(png_ptr, info_ptr->hist);
\r
409 info_ptr->hist = NULL;
\r
410 info_ptr->valid &= ~PNG_INFO_hIST;
\r
414 /* Free any PLTE entry that was internally allocated */
\r
415 if ((mask & PNG_FREE_PLTE) & info_ptr->free_me)
\r
417 png_zfree(png_ptr, info_ptr->palette);
\r
418 info_ptr->palette = NULL;
\r
419 info_ptr->valid &= ~PNG_INFO_PLTE;
\r
420 info_ptr->num_palette = 0;
\r
423 #ifdef PNG_INFO_IMAGE_SUPPORTED
\r
424 /* Free any image bits attached to the info structure */
\r
425 if ((mask & PNG_FREE_ROWS) & info_ptr->free_me)
\r
427 if (info_ptr->row_pointers)
\r
430 for (row = 0; row < (int)info_ptr->height; row++)
\r
432 png_free(png_ptr, info_ptr->row_pointers[row]);
\r
433 info_ptr->row_pointers[row] = NULL;
\r
435 png_free(png_ptr, info_ptr->row_pointers);
\r
436 info_ptr->row_pointers = NULL;
\r
438 info_ptr->valid &= ~PNG_INFO_IDAT;
\r
443 info_ptr->free_me &= ~mask;
\r
445 info_ptr->free_me &= ~(mask & ~PNG_FREE_MUL);
\r
448 /* This is an internal routine to free any memory that the info struct is
\r
449 * pointing to before re-using it or freeing the struct itself. Recall
\r
450 * that png_free() checks for NULL pointers for us.
\r
453 png_info_destroy(png_structp png_ptr, png_infop info_ptr)
\r
455 png_debug(1, "in png_info_destroy");
\r
457 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
\r
459 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
\r
460 if (png_ptr->num_chunk_list)
\r
462 png_free(png_ptr, png_ptr->chunk_list);
\r
463 png_ptr->chunk_list = NULL;
\r
464 png_ptr->num_chunk_list = 0;
\r
468 png_info_init_3(&info_ptr, png_sizeof(png_info));
\r
470 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
\r
472 /* This function returns a pointer to the io_ptr associated with the user
\r
473 * functions. The application should free any memory associated with this
\r
474 * pointer before png_write_destroy() or png_read_destroy() are called.
\r
477 png_get_io_ptr(png_structp png_ptr)
\r
479 if (png_ptr == NULL)
\r
481 return (png_ptr->io_ptr);
\r
484 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
\r
485 #ifdef PNG_STDIO_SUPPORTED
\r
486 /* Initialize the default input/output functions for the PNG file. If you
\r
487 * use your own read or write routines, you can call either png_set_read_fn()
\r
488 * or png_set_write_fn() instead of png_init_io(). If you have defined
\r
489 * PNG_NO_STDIO, you must use a function of your own because "FILE *" isn't
\r
490 * necessarily available.
\r
493 png_init_io(png_structp png_ptr, png_FILE_p fp)
\r
495 png_debug(1, "in png_init_io");
\r
497 if (png_ptr == NULL)
\r
500 png_ptr->io_ptr = (png_voidp)fp;
\r
504 #ifdef PNG_TIME_RFC1123_SUPPORTED
\r
505 /* Convert the supplied time into an RFC 1123 string suitable for use in
\r
506 * a "Creation Time" or other text-based time string.
\r
509 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
\r
511 static PNG_CONST char short_months[12][4] =
\r
512 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
\r
513 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
\r
515 if (png_ptr == NULL)
\r
517 if (png_ptr->time_buffer == NULL)
\r
519 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
\r
520 png_sizeof(char)));
\r
523 #ifdef USE_FAR_KEYWORD
\r
525 char near_time_buf[29];
\r
526 png_snprintf6(near_time_buf, 29, "%d %s %d %02d:%02d:%02d +0000",
\r
527 ptime->day % 32, short_months[(ptime->month - 1) % 12],
\r
528 ptime->year, ptime->hour % 24, ptime->minute % 60,
\r
529 ptime->second % 61);
\r
530 png_memcpy(png_ptr->time_buffer, near_time_buf,
\r
531 29*png_sizeof(char));
\r
534 png_snprintf6(png_ptr->time_buffer, 29, "%d %s %d %02d:%02d:%02d +0000",
\r
535 ptime->day % 32, short_months[(ptime->month - 1) % 12],
\r
536 ptime->year, ptime->hour % 24, ptime->minute % 60,
\r
537 ptime->second % 61);
\r
539 return ((png_charp)png_ptr->time_buffer);
\r
541 #endif /* PNG_TIME_RFC1123_SUPPORTED */
\r
543 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
\r
546 png_get_copyright(png_structp png_ptr)
\r
548 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
\r
549 #ifdef PNG_STRING_COPYRIGHT
\r
550 return PNG_STRING_COPYRIGHT
\r
553 return ((png_charp) PNG_STRING_NEWLINE \
\r
554 "libpng version 1.4.3 - June 26, 2010" PNG_STRING_NEWLINE \
\r
555 "Copyright (c) 1998-2010 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
\r
556 "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
\r
557 "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
\r
558 PNG_STRING_NEWLINE);
\r
560 return ((png_charp) "libpng version 1.4.3 - June 26, 2010\
\r
561 Copyright (c) 1998-2010 Glenn Randers-Pehrson\
\r
562 Copyright (c) 1996-1997 Andreas Dilger\
\r
563 Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.");
\r
568 /* The following return the library version as a short string in the
\r
569 * format 1.0.0 through 99.99.99zz. To get the version of *.h files
\r
570 * used with your application, print out PNG_LIBPNG_VER_STRING, which
\r
571 * is defined in png.h.
\r
572 * Note: now there is no difference between png_get_libpng_ver() and
\r
573 * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard,
\r
574 * it is guaranteed that png.c uses the correct version of png.h.
\r
577 png_get_libpng_ver(png_structp png_ptr)
\r
579 /* Version of *.c files used when building libpng */
\r
580 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
\r
581 return ((png_charp) PNG_LIBPNG_VER_STRING);
\r
585 png_get_header_ver(png_structp png_ptr)
\r
587 /* Version of *.h files used when building libpng */
\r
588 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
\r
589 return ((png_charp) PNG_LIBPNG_VER_STRING);
\r
593 png_get_header_version(png_structp png_ptr)
\r
595 /* Returns longer string containing both version and date */
\r
596 png_ptr = png_ptr; /* Silence compiler warning about unused png_ptr */
\r
598 return ((png_charp) PNG_HEADER_VERSION_STRING
\r
599 #ifndef PNG_READ_SUPPORTED
\r
600 " (NO READ SUPPORT)"
\r
602 PNG_STRING_NEWLINE);
\r
604 return ((png_charp) PNG_HEADER_VERSION_STRING);
\r
608 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
\r
609 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
\r
611 png_handle_as_unknown(png_structp png_ptr, png_bytep chunk_name)
\r
613 /* Check chunk_name and return "keep" value if it's on the list, else 0 */
\r
616 if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list<=0)
\r
618 p = png_ptr->chunk_list + png_ptr->num_chunk_list*5 - 5;
\r
619 for (i = png_ptr->num_chunk_list; i; i--, p -= 5)
\r
620 if (!png_memcmp(chunk_name, p, 4))
\r
621 return ((int)*(p + 4));
\r
625 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
\r
627 #ifdef PNG_READ_SUPPORTED
\r
628 /* This function, added to libpng-1.0.6g, is untested. */
\r
630 png_reset_zstream(png_structp png_ptr)
\r
632 if (png_ptr == NULL)
\r
633 return Z_STREAM_ERROR;
\r
634 return (inflateReset(&png_ptr->zstream));
\r
636 #endif /* PNG_READ_SUPPORTED */
\r
638 /* This function was added to libpng-1.0.7 */
\r
640 png_access_version_number(void)
\r
642 /* Version of *.c files used when building libpng */
\r
643 return((png_uint_32) PNG_LIBPNG_VER);
\r
648 #if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED)
\r
650 /* Added at libpng version 1.2.6 */
\r
651 PNG_EXTERN png_size_t PNGAPI png_convert_size PNGARG((size_t size));
\r
653 png_convert_size(size_t size)
\r
655 if (size > (png_size_t)-1)
\r
656 PNG_ABORT(); /* We haven't got access to png_ptr, so no png_error() */
\r
657 return ((png_size_t)size);
\r
659 #endif /* PNG_SIZE_T */
\r
661 /* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */
\r
662 #ifdef PNG_cHRM_SUPPORTED
\r
663 #ifdef PNG_CHECK_cHRM_SUPPORTED
\r
666 * Multiply two 32-bit numbers, V1 and V2, using 32-bit
\r
667 * arithmetic, to produce a 64 bit result in the HI/LO words.
\r
675 * where A and B are the high and low 16-bit words of V1,
\r
676 * C and D are the 16-bit words of V2, AD is the product of
\r
677 * A and D, and X || Y is (X << 16) + Y.
\r
681 png_64bit_product (long v1, long v2, unsigned long *hi_product,
\r
682 unsigned long *lo_product)
\r
687 a = (v1 >> 16) & 0xffff;
\r
689 c = (v2 >> 16) & 0xffff;
\r
692 lo = b * d; /* BD */
\r
693 x = a * d + c * b; /* AD + CB */
\r
694 y = ((lo >> 16) & 0xffff) + x;
\r
696 lo = (lo & 0xffff) | ((y & 0xffff) << 16);
\r
697 hi = (y >> 16) & 0xffff;
\r
699 hi += a * c; /* AC */
\r
701 *hi_product = (unsigned long)hi;
\r
702 *lo_product = (unsigned long)lo;
\r
706 png_check_cHRM_fixed(png_structp png_ptr,
\r
707 png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x,
\r
708 png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y,
\r
709 png_fixed_point blue_x, png_fixed_point blue_y)
\r
712 unsigned long xy_hi,xy_lo,yx_hi,yx_lo;
\r
714 png_debug(1, "in function png_check_cHRM_fixed");
\r
716 if (png_ptr == NULL)
\r
719 if (white_x < 0 || white_y <= 0 ||
\r
720 red_x < 0 || red_y < 0 ||
\r
721 green_x < 0 || green_y < 0 ||
\r
722 blue_x < 0 || blue_y < 0)
\r
724 png_warning(png_ptr,
\r
725 "Ignoring attempt to set negative chromaticity value");
\r
728 if (white_x > (png_fixed_point) PNG_UINT_31_MAX ||
\r
729 white_y > (png_fixed_point) PNG_UINT_31_MAX ||
\r
730 red_x > (png_fixed_point) PNG_UINT_31_MAX ||
\r
731 red_y > (png_fixed_point) PNG_UINT_31_MAX ||
\r
732 green_x > (png_fixed_point) PNG_UINT_31_MAX ||
\r
733 green_y > (png_fixed_point) PNG_UINT_31_MAX ||
\r
734 blue_x > (png_fixed_point) PNG_UINT_31_MAX ||
\r
735 blue_y > (png_fixed_point) PNG_UINT_31_MAX )
\r
737 png_warning(png_ptr,
\r
738 "Ignoring attempt to set chromaticity value exceeding 21474.83");
\r
741 if (white_x > 100000L - white_y)
\r
743 png_warning(png_ptr, "Invalid cHRM white point");
\r
746 if (red_x > 100000L - red_y)
\r
748 png_warning(png_ptr, "Invalid cHRM red point");
\r
751 if (green_x > 100000L - green_y)
\r
753 png_warning(png_ptr, "Invalid cHRM green point");
\r
756 if (blue_x > 100000L - blue_y)
\r
758 png_warning(png_ptr, "Invalid cHRM blue point");
\r
762 png_64bit_product(green_x - red_x, blue_y - red_y, &xy_hi, &xy_lo);
\r
763 png_64bit_product(green_y - red_y, blue_x - red_x, &yx_hi, &yx_lo);
\r
765 if (xy_hi == yx_hi && xy_lo == yx_lo)
\r
767 png_warning(png_ptr,
\r
768 "Ignoring attempt to set cHRM RGB triangle with zero area");
\r
774 #endif /* PNG_CHECK_cHRM_SUPPORTED */
\r
775 #endif /* PNG_cHRM_SUPPORTED */
\r
778 png_check_IHDR(png_structp png_ptr,
\r
779 png_uint_32 width, png_uint_32 height, int bit_depth,
\r
780 int color_type, int interlace_type, int compression_type,
\r
785 /* Check for width and height valid values */
\r
788 png_warning(png_ptr, "Image width is zero in IHDR");
\r
794 png_warning(png_ptr, "Image height is zero in IHDR");
\r
798 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
\r
799 if (width > png_ptr->user_width_max || width > PNG_USER_WIDTH_MAX)
\r
801 if (width > PNG_USER_WIDTH_MAX)
\r
804 png_warning(png_ptr, "Image width exceeds user limit in IHDR");
\r
808 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
\r
809 if (height > png_ptr->user_height_max || height > PNG_USER_HEIGHT_MAX)
\r
811 if (height > PNG_USER_HEIGHT_MAX)
\r
814 png_warning(png_ptr, "Image height exceeds user limit in IHDR");
\r
818 if (width > PNG_UINT_31_MAX)
\r
820 png_warning(png_ptr, "Invalid image width in IHDR");
\r
824 if ( height > PNG_UINT_31_MAX)
\r
826 png_warning(png_ptr, "Invalid image height in IHDR");
\r
830 if ( width > (PNG_UINT_32_MAX
\r
831 >> 3) /* 8-byte RGBA pixels */
\r
832 - 64 /* bigrowbuf hack */
\r
833 - 1 /* filter byte */
\r
834 - 7*8 /* rounding of width to multiple of 8 pixels */
\r
835 - 8) /* extra max_pixel_depth pad */
\r
836 png_warning(png_ptr, "Width is too large for libpng to process pixels");
\r
838 /* Check other values */
\r
839 if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 &&
\r
840 bit_depth != 8 && bit_depth != 16)
\r
842 png_warning(png_ptr, "Invalid bit depth in IHDR");
\r
846 if (color_type < 0 || color_type == 1 ||
\r
847 color_type == 5 || color_type > 6)
\r
849 png_warning(png_ptr, "Invalid color type in IHDR");
\r
853 if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) ||
\r
854 ((color_type == PNG_COLOR_TYPE_RGB ||
\r
855 color_type == PNG_COLOR_TYPE_GRAY_ALPHA ||
\r
856 color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8))
\r
858 png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR");
\r
862 if (interlace_type >= PNG_INTERLACE_LAST)
\r
864 png_warning(png_ptr, "Unknown interlace method in IHDR");
\r
868 if (compression_type != PNG_COMPRESSION_TYPE_BASE)
\r
870 png_warning(png_ptr, "Unknown compression method in IHDR");
\r
874 #ifdef PNG_MNG_FEATURES_SUPPORTED
\r
875 /* Accept filter_method 64 (intrapixel differencing) only if
\r
876 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
\r
877 * 2. Libpng did not read a PNG signature (this filter_method is only
\r
878 * used in PNG datastreams that are embedded in MNG datastreams) and
\r
879 * 3. The application called png_permit_mng_features with a mask that
\r
880 * included PNG_FLAG_MNG_FILTER_64 and
\r
881 * 4. The filter_method is 64 and
\r
882 * 5. The color_type is RGB or RGBA
\r
884 if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) &&
\r
885 png_ptr->mng_features_permitted)
\r
886 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
\r
888 if (filter_type != PNG_FILTER_TYPE_BASE)
\r
890 if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
\r
891 (filter_type == PNG_INTRAPIXEL_DIFFERENCING) &&
\r
892 ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
\r
893 (color_type == PNG_COLOR_TYPE_RGB ||
\r
894 color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
\r
896 png_warning(png_ptr, "Unknown filter method in IHDR");
\r
900 if (png_ptr->mode & PNG_HAVE_PNG_SIGNATURE)
\r
902 png_warning(png_ptr, "Invalid filter method in IHDR");
\r
908 if (filter_type != PNG_FILTER_TYPE_BASE)
\r
910 png_warning(png_ptr, "Unknown filter method in IHDR");
\r
916 png_error(png_ptr, "Invalid IHDR data");
\r
918 #endif /* defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) */
\r