COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
pngpriv.h File Reference
#include <stdlib.h>
#include <string.h>
#include "pnglibconf.h"
#include "png.h"
#include <float.h>
#include <math.h>
#include "pngstruct.h"
#include "pnginfo.h"
#include "pngdebug.h"
Include dependency graph for pngpriv.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  png_control
 

Macros

#define _POSIX_SOURCE   1 /* Just the POSIX 1003.1 and C89 APIs */
 
#define PNGLIB_BUILD   /*libpng is being built, not used*/
 
#define PNG_ARM_NEON_OPT   0
 
#define PNG_IMPEXP
 
#define PNG_DEPRECATED
 
#define PNG_PRIVATE
 
#define PNG_INTERNAL_DATA(type, name, array)   extern type name array
 
#define PNG_INTERNAL_FUNCTION(type, name, args, attributes)   extern PNG_FUNCTION(type, name, args, PNG_EMPTY attributes)
 
#define PNG_INTERNAL_CALLBACK(type, name, args, attributes)   extern PNG_FUNCTION(type, (PNGCBAPI name), args, PNG_EMPTY attributes)
 
#define PNG_DLL_EXPORT
 
#define PNG_USER_WIDTH_MAX   0x7fffffff
 
#define PNG_USER_HEIGHT_MAX   0x7fffffff
 
#define PNG_USER_CHUNK_CACHE_MAX   0
 
#define PNG_USER_CHUNK_MALLOC_MAX   0
 
#define PNG_UNUSED(param)   (void)param;
 
#define PNG_WARNING_PARAMETERS(p)   png_warning_parameters p;
 
#define png_voidcast(type, value)   (value)
 
#define png_constcast(type, value)   ((type)(value))
 
#define png_aligncast(type, value)   ((void*)(value))
 
#define png_aligncastconst(type, value)   ((const void*)(value))
 
#define PNGFAPI   PNGAPI
 
#define PNG_ABORT()   abort()
 
#define PNG_ALIGN_NONE   0 /* do not use data alignment */
 
#define PNG_ALIGN_ALWAYS   1 /* assume unaligned accesses are OK */
 
#define PNG_ALIGN_OFFSET   -1 /* prevent the use of this */
 
#define PNG_ALIGN_SIZE   3 /* use sizeof to determine alignment */
 
#define PNG_ALIGN_TYPE   PNG_ALIGN_SIZE
 
#define png_alignof(type)   (sizeof (type))
 
#define png_isaligned(ptr, type)   ((((const char*)ptr-(const char*)0) & (png_alignof(type)-1)) == 0)
 
#define PNG_HAVE_IDAT   0x04
 
#define PNG_HAVE_IEND   0x10
 
#define PNG_HAVE_CHUNK_HEADER   0x100
 
#define PNG_WROTE_tIME   0x200
 
#define PNG_WROTE_INFO_BEFORE_PLTE   0x400
 
#define PNG_BACKGROUND_IS_GRAY   0x800
 
#define PNG_HAVE_PNG_SIGNATURE   0x1000
 
#define PNG_HAVE_CHUNK_AFTER_IDAT   0x2000 /* Have another chunk after IDAT */
 
#define PNG_IS_READ_STRUCT   0x8000 /* Else is a write struct */
 
#define PNG_BGR   0x0001
 
#define PNG_INTERLACE   0x0002
 
#define PNG_PACK   0x0004
 
#define PNG_SHIFT   0x0008
 
#define PNG_SWAP_BYTES   0x0010
 
#define PNG_INVERT_MONO   0x0020
 
#define PNG_QUANTIZE   0x0040
 
#define PNG_COMPOSE   0x0080 /* Was PNG_BACKGROUND */
 
#define PNG_BACKGROUND_EXPAND   0x0100
 
#define PNG_EXPAND_16   0x0200 /* Added to libpng 1.5.2 */
 
#define PNG_16_TO_8   0x0400 /* Becomes 'chop' in 1.5.4 */
 
#define PNG_RGBA   0x0800
 
#define PNG_EXPAND   0x1000
 
#define PNG_GAMMA   0x2000
 
#define PNG_GRAY_TO_RGB   0x4000
 
#define PNG_FILLER   0x8000
 
#define PNG_PACKSWAP   0x10000
 
#define PNG_SWAP_ALPHA   0x20000
 
#define PNG_STRIP_ALPHA   0x40000
 
#define PNG_INVERT_ALPHA   0x80000
 
#define PNG_USER_TRANSFORM   0x100000
 
#define PNG_RGB_TO_GRAY_ERR   0x200000
 
#define PNG_RGB_TO_GRAY_WARN   0x400000
 
#define PNG_RGB_TO_GRAY   0x600000 /* two bits, RGB_TO_GRAY_ERR|WARN */
 
#define PNG_ENCODE_ALPHA   0x800000 /* Added to libpng-1.5.4 */
 
#define PNG_ADD_ALPHA   0x1000000 /* Added to libpng-1.2.7 */
 
#define PNG_EXPAND_tRNS   0x2000000 /* Added to libpng-1.2.9 */
 
#define PNG_SCALE_16_TO_8   0x4000000 /* Added to libpng-1.5.4 */
 
#define PNG_STRUCT_PNG   0x0001
 
#define PNG_STRUCT_INFO   0x0002
 
#define PNG_WEIGHT_FACTOR   (1<<(PNG_WEIGHT_SHIFT))
 
#define PNG_COST_FACTOR   (1<<(PNG_COST_SHIFT))
 
#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY   0x0001
 
#define PNG_FLAG_ZSTREAM_INITIALIZED   0x0002 /* Added to libpng-1.6.0 */
 
#define PNG_FLAG_ZSTREAM_ENDED   0x0008 /* Added to libpng-1.6.0 */
 
#define PNG_FLAG_ROW_INIT   0x0040
 
#define PNG_FLAG_FILLER_AFTER   0x0080
 
#define PNG_FLAG_CRC_ANCILLARY_USE   0x0100
 
#define PNG_FLAG_CRC_ANCILLARY_NOWARN   0x0200
 
#define PNG_FLAG_CRC_CRITICAL_USE   0x0400
 
#define PNG_FLAG_CRC_CRITICAL_IGNORE   0x0800
 
#define PNG_FLAG_ASSUME_sRGB   0x1000 /* Added to libpng-1.5.4 */
 
#define PNG_FLAG_OPTIMIZE_ALPHA   0x2000 /* Added to libpng-1.5.4 */
 
#define PNG_FLAG_DETECT_UNINITIALIZED   0x4000 /* Added to libpng-1.5.4 */
 
#define PNG_FLAG_LIBRARY_MISMATCH   0x20000
 
#define PNG_FLAG_STRIP_ERROR_NUMBERS   0x40000
 
#define PNG_FLAG_STRIP_ERROR_TEXT   0x80000
 
#define PNG_FLAG_BENIGN_ERRORS_WARN   0x100000 /* Added to libpng-1.4.0 */
 
#define PNG_FLAG_APP_WARNINGS_WARN   0x200000 /* Added to libpng-1.6.0 */
 
#define PNG_FLAG_APP_ERRORS_WARN   0x400000 /* Added to libpng-1.6.0 */
 
#define PNG_FLAG_CRC_ANCILLARY_MASK
 
#define PNG_FLAG_CRC_CRITICAL_MASK
 
#define PNG_FLAG_CRC_MASK
 
#define PNG_COLOR_DIST(c1, c2)
 
#define PNG_DIV65535(v24)   (((v24) + 32895) >> 16)
 
#define PNG_DIV257(v16)   PNG_DIV65535((png_uint_32)(v16) * 255)
 
#define PNG_ROWBYTES(pixel_bits, width)
 
#define PNG_OUT_OF_RANGE(value, ideal, delta)   ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
 
#define png_float(png_ptr, fixed, s)   (.00001 * (fixed))
 
#define PNG_32b(b, s)   ((png_uint_32)(b) << (s))
 
#define PNG_U32(b1, b2, b3, b4)   (PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0))
 
#define png_IDAT   PNG_U32( 73, 68, 65, 84)
 
#define png_IEND   PNG_U32( 73, 69, 78, 68)
 
#define png_IHDR   PNG_U32( 73, 72, 68, 82)
 
#define png_PLTE   PNG_U32( 80, 76, 84, 69)
 
#define png_bKGD   PNG_U32( 98, 75, 71, 68)
 
#define png_cHRM   PNG_U32( 99, 72, 82, 77)
 
#define png_fRAc   PNG_U32(102, 82, 65, 99) /* registered, not defined */
 
#define png_gAMA   PNG_U32(103, 65, 77, 65)
 
#define png_gIFg   PNG_U32(103, 73, 70, 103)
 
#define png_gIFt   PNG_U32(103, 73, 70, 116) /* deprecated */
 
#define png_gIFx   PNG_U32(103, 73, 70, 120)
 
#define png_hIST   PNG_U32(104, 73, 83, 84)
 
#define png_iCCP   PNG_U32(105, 67, 67, 80)
 
#define png_iTXt   PNG_U32(105, 84, 88, 116)
 
#define png_oFFs   PNG_U32(111, 70, 70, 115)
 
#define png_pCAL   PNG_U32(112, 67, 65, 76)
 
#define png_pHYs   PNG_U32(112, 72, 89, 115)
 
#define png_sBIT   PNG_U32(115, 66, 73, 84)
 
#define png_sCAL   PNG_U32(115, 67, 65, 76)
 
#define png_sPLT   PNG_U32(115, 80, 76, 84)
 
#define png_sRGB   PNG_U32(115, 82, 71, 66)
 
#define png_sTER   PNG_U32(115, 84, 69, 82)
 
#define png_tEXt   PNG_U32(116, 69, 88, 116)
 
#define png_tIME   PNG_U32(116, 73, 77, 69)
 
#define png_tRNS   PNG_U32(116, 82, 78, 83)
 
#define png_zTXt   PNG_U32(122, 84, 88, 116)
 
#define PNG_CHUNK_FROM_STRING(s)   PNG_U32(0xff&(s)[0], 0xff&(s)[1], 0xff&(s)[2], 0xff&(s)[3])
 
#define PNG_STRING_FROM_CHUNK(s, c)
 
#define PNG_CSTRING_FROM_CHUNK(s, c)   (void)(PNG_STRING_FROM_CHUNK(s,c), ((char*)(s))[4] = 0)
 
#define PNG_CHUNK_ANCILLARY(c)   (1 & ((c) >> 29))
 
#define PNG_CHUNK_CRITICAL(c)   (!PNG_CHUNK_ANCILLARY(c))
 
#define PNG_CHUNK_PRIVATE(c)   (1 & ((c) >> 21))
 
#define PNG_CHUNK_RESERVED(c)   (1 & ((c) >> 13))
 
#define PNG_CHUNK_SAFE_TO_COPY(c)   (1 & ((c) >> 5))
 
#define PNG_GAMMA_MAC_OLD   151724 /* Assume '1.8' is really 2.2/1.45! */
 
#define PNG_GAMMA_MAC_INVERSE   65909
 
#define PNG_GAMMA_sRGB_INVERSE   45455
 
#define PNG_sRGB_FROM_LINEAR(linear)
 
#define PNG_UNEXPECTED_ZLIB_RETURN   (-7)
 
#define PNG_USE_COMPILE_TIME_MASKS   1
 
#define PNG_FORMAT_NUMBER(buffer, format, number)   png_format_number(buffer, buffer + (sizeof buffer), format, number)
 
#define PNG_NUMBER_BUFFER_SIZE   24
 
#define PNG_NUMBER_FORMAT_u   1 /* chose unsigned API! */
 
#define PNG_NUMBER_FORMAT_02u   2
 
#define PNG_NUMBER_FORMAT_d   1 /* chose signed API! */
 
#define PNG_NUMBER_FORMAT_02d   2
 
#define PNG_NUMBER_FORMAT_x   3
 
#define PNG_NUMBER_FORMAT_02x   4
 
#define PNG_NUMBER_FORMAT_fixed   5 /* choose the signed API */
 
#define PNG_WARNING_PARAMETER_SIZE   32
 
#define PNG_WARNING_PARAMETER_COUNT   8 /* Maximum 9; see pngerror.c */
 
#define PNG_CHUNK_WARNING   0 /* never an error */
 
#define PNG_CHUNK_WRITE_ERROR   1 /* an error only on write */
 
#define PNG_CHUNK_ERROR   2 /* always an error */
 
#define PNG_sCAL_MAX_DIGITS   (PNG_sCAL_PRECISION+1/*.*/+1/*E*/+10/*exponent*/)
 
#define PNG_FP_INTEGER   0 /* before or in integer */
 
#define PNG_FP_FRACTION   1 /* before or in fraction */
 
#define PNG_FP_EXPONENT   2 /* before or in exponent */
 
#define PNG_FP_STATE   3 /* mask for the above */
 
#define PNG_FP_SAW_SIGN   4 /* Saw +/- in current state */
 
#define PNG_FP_SAW_DIGIT   8 /* Saw a digit in current state */
 
#define PNG_FP_SAW_DOT   16 /* Saw a dot in current state */
 
#define PNG_FP_SAW_E   32 /* Saw an E (or e) in current state */
 
#define PNG_FP_SAW_ANY   60 /* Saw any of the above 4 */
 
#define PNG_FP_WAS_VALID   64 /* Preceding substring is a valid fp number */
 
#define PNG_FP_NEGATIVE   128 /* A negative number, including "-0" */
 
#define PNG_FP_NONZERO   256 /* A non-zero value */
 
#define PNG_FP_STICKY   448 /* The above three flags */
 
#define PNG_FP_INVALID   512 /* Available for callers as a distinct value */
 
#define PNG_FP_MAYBE   0 /* The number may be valid in the future */
 
#define PNG_FP_OK   1 /* The number is valid */
 
#define PNG_FP_NZ_MASK   (PNG_FP_SAW_DIGIT | PNG_FP_NEGATIVE | PNG_FP_NONZERO)
 
#define PNG_FP_Z_MASK   (PNG_FP_SAW_DIGIT | PNG_FP_NONZERO)
 
#define PNG_FP_IS_ZERO(state)   (((state) & PNG_FP_Z_MASK) == PNG_FP_SAW_DIGIT)
 
#define PNG_FP_IS_POSITIVE(state)   (((state) & PNG_FP_NZ_MASK) == PNG_FP_Z_MASK)
 
#define PNG_FP_IS_NEGATIVE(state)   (((state) & PNG_FP_NZ_MASK) == PNG_FP_NZ_MASK)
 
#define png_control_jmp_buf(pc)   ((pc)->error_buf)
 

Typedefs

typedef const png_uint_16ppng_const_uint_16pp
 
typedef char png_warning_parameters[8][32]
 
typedef struct png_control png_control
 

Functions

void png_zstream_error (png_structrp png_ptr, int ret)
 
void png_free_buffer_list (png_structrp png_ptr, png_compression_bufferp *list)
 
png_fixed_point png_fixed (png_const_structrp png_ptr, double fp, png_const_charp text)
 
int png_user_version_check (png_structrp png_ptr, png_const_charp user_png_ver)
 
png_voidp png_malloc_base (png_const_structrp png_ptr, png_alloc_size_t size)
 
png_voidp png_malloc_array (png_const_structrp png_ptr, int nelements, size_t element_size)
 
png_voidp png_realloc_array (png_const_structrp png_ptr, png_const_voidp array, int old_elements, int add_elements, size_t element_size)
 
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)
 
void png_destroy_png_struct (png_structrp png_ptr)
 
void png_free_jmpbuf (png_structrp png_ptr)
 
voidpf png_zalloc (voidpf png_ptr, uInt items, uInt size)
 
void png_zfree (voidpf png_ptr, voidpf ptr)
 
void png_default_read_data (png_structp png_ptr, png_bytep data, png_size_t length)
 
void png_push_fill_buffer (png_structp png_ptr, png_bytep buffer, png_size_t length)
 
void png_default_write_data (png_structp png_ptr, png_bytep data, png_size_t length)
 
void png_default_flush (png_structp png_ptr)
 
void png_reset_crc (png_structrp png_ptr)
 
void png_write_data (png_structrp png_ptr, png_const_bytep data, png_size_t length)
 
void png_read_sig (png_structrp png_ptr, png_inforp info_ptr)
 
png_uint_32 png_read_chunk_header (png_structrp png_ptr)
 
void png_read_data (png_structrp png_ptr, png_bytep data, png_size_t length)
 
void png_crc_read (png_structrp png_ptr, png_bytep buf, png_uint_32 length)
 
int png_crc_finish (png_structrp png_ptr, png_uint_32 skip)
 
int png_crc_error (png_structrp png_ptr)
 
void png_calculate_crc (png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
 
void png_flush (png_structrp png_ptr)
 
void png_write_IHDR (png_structrp png_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int compression_method, int filter_method, int interlace_method)
 
void png_write_PLTE (png_structrp png_ptr, png_const_colorp palette, png_uint_32 num_pal)
 
void png_compress_IDAT (png_structrp png_ptr, png_const_bytep row_data, png_alloc_size_t row_data_length, int flush)
 
void png_write_IEND (png_structrp png_ptr)
 
void png_write_gAMA_fixed (png_structrp png_ptr, png_fixed_point file_gamma)
 
void png_write_sBIT (png_structrp png_ptr, png_const_color_8p sbit, int color_type)
 
void png_write_cHRM_fixed (png_structrp png_ptr, const png_xy *xy)
 
void png_write_sRGB (png_structrp png_ptr, int intent)
 
void png_write_iCCP (png_structrp png_ptr, png_const_charp name, png_const_bytep profile)
 
void png_write_sPLT (png_structrp png_ptr, png_const_sPLT_tp palette)
 
void png_write_tRNS (png_structrp png_ptr, png_const_bytep trans, png_const_color_16p values, int number, int color_type)
 
void png_write_bKGD (png_structrp png_ptr, png_const_color_16p values, int color_type)
 
void png_write_hIST (png_structrp png_ptr, png_const_uint_16p hist, int num_hist)
 
void png_write_tEXt (png_structrp png_ptr, png_const_charp key, png_const_charp text, png_size_t text_len)
 
void png_write_zTXt (png_structrp png_ptr, png_const_charp key, png_const_charp text, int compression)
 
void png_write_iTXt (png_structrp png_ptr, int compression, png_const_charp key, png_const_charp lang, png_const_charp lang_key, png_const_charp text)
 
int png_set_text_2 (png_const_structrp png_ptr, png_inforp info_ptr, png_const_textp text_ptr, int num_text)
 
void png_write_oFFs (png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, int unit_type)
 
void png_write_pCAL (png_structrp png_ptr, png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_const_charp units, png_charpp params)
 
void png_write_pHYs (png_structrp png_ptr, png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, int unit_type)
 
void png_write_tIME (png_structrp png_ptr, png_const_timep mod_time)
 
void png_write_sCAL_s (png_structrp png_ptr, int unit, png_const_charp width, png_const_charp height)
 
void png_write_finish_row (png_structrp png_ptr)
 
void png_write_start_row (png_structrp png_ptr)
 
void png_combine_row (png_const_structrp png_ptr, png_bytep row, int display)
 
void png_do_read_interlace (png_row_infop row_info, png_bytep row, int pass, png_uint_32 transformations)
 
void png_do_write_interlace (png_row_infop row_info, png_bytep row, int pass)
 
void png_read_filter_row (png_structrp pp, png_row_infop row_info, png_bytep row, png_const_bytep prev_row, int filter)
 
void png_read_filter_row_up_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_sub3_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_sub4_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_avg3_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_avg4_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_paeth3_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_read_filter_row_paeth4_neon (png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
 
void png_write_find_filter (png_structrp png_ptr, png_row_infop row_info)
 
void png_read_IDAT_data (png_structrp png_ptr, png_bytep output, png_alloc_size_t avail_out)
 
void png_read_finish_IDAT (png_structrp png_ptr)
 
void png_read_finish_row (png_structrp png_ptr)
 
void png_read_start_row (png_structrp png_ptr)
 
void png_read_transform_info (png_structrp png_ptr, png_inforp info_ptr)
 
void png_do_strip_channel (png_row_infop row_info, png_bytep row, int at_start)
 
void png_do_swap (png_row_infop row_info, png_bytep row)
 
void png_do_packswap (png_row_infop row_info, png_bytep row)
 
void png_do_invert (png_row_infop row_info, png_bytep row)
 
void png_do_bgr (png_row_infop row_info, png_bytep row)
 
void png_handle_IHDR (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_PLTE (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_IEND (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_bKGD (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_cHRM (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_gAMA (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_hIST (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_iCCP (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_iTXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_oFFs (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_pCAL (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_pHYs (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_sBIT (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_sCAL (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_sPLT (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_sRGB (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_tEXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_tIME (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_tRNS (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_handle_zTXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_check_chunk_name (png_structrp png_ptr, png_uint_32 chunk_name)
 
void png_handle_unknown (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length, int keep)
 
int png_chunk_unknown_handling (png_const_structrp png_ptr, png_uint_32 chunk_name)
 
void png_do_read_transformations (png_structrp png_ptr, png_row_infop row_info)
 
void png_do_write_transformations (png_structrp png_ptr, png_row_infop row_info)
 
void png_init_read_transformations (png_structrp png_ptr)
 
void png_push_read_chunk (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_read_sig (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_check_crc (png_structrp png_ptr)
 
void png_push_crc_skip (png_structrp png_ptr, png_uint_32 length)
 
void png_push_crc_finish (png_structrp png_ptr)
 
void png_push_save_buffer (png_structrp png_ptr)
 
void png_push_restore_buffer (png_structrp png_ptr, png_bytep buffer, png_size_t buffer_length)
 
void png_push_read_IDAT (png_structrp png_ptr)
 
void png_process_IDAT_data (png_structrp png_ptr, png_bytep buffer, png_size_t buffer_length)
 
void png_push_process_row (png_structrp png_ptr)
 
void png_push_handle_unknown (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_push_have_info (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_have_end (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_have_row (png_structrp png_ptr, png_bytep row)
 
void png_push_read_end (png_structrp png_ptr, png_inforp info_ptr)
 
void png_process_some_data (png_structrp png_ptr, png_inforp info_ptr)
 
void png_read_push_finish_row (png_structrp png_ptr)
 
void png_push_handle_tEXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_push_read_tEXt (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_handle_zTXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_push_read_zTXt (png_structrp png_ptr, png_inforp info_ptr)
 
void png_push_handle_iTXt (png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
 
void png_push_read_iTXt (png_structrp png_ptr, png_inforp info_ptr)
 
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)
 
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, int preferred)
 
int png_colorspace_set_sRGB (png_const_structrp png_ptr, png_colorspacerp colorspace, int intent)
 
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)
 
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)
 
void png_icc_set_sRGB (png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_bytep profile, uLong adler)
 
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)
 
void png_do_check_palette_indexes (png_structrp png_ptr, png_row_infop row_info)
 
void png_fixed_error (png_const_structrp png_ptr, png_const_charp name)
 
size_t png_safecat (png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
 
png_charp png_format_number (png_const_charp start, png_charp end, int format, png_alloc_size_t number)
 
void png_warning_parameter (png_warning_parameters p, int number, png_const_charp string)
 
void png_warning_parameter_unsigned (png_warning_parameters p, int number, int format, png_alloc_size_t value)
 
void png_warning_parameter_signed (png_warning_parameters p, int number, int format, png_int_32 value)
 
void png_formatted_warning (png_const_structrp png_ptr, png_warning_parameters p, png_const_charp message)
 
void png_app_warning (png_const_structrp png_ptr, png_const_charp message)
 
void png_app_error (png_const_structrp png_ptr, png_const_charp message)
 
void png_chunk_report (png_const_structrp png_ptr, png_const_charp message, int error)
 
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)
 
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)
 
int png_muldiv (png_fixed_point_p res, png_fixed_point a, png_int_32 multiplied_by, png_int_32 divided_by)
 
png_fixed_point png_muldiv_warn (png_const_structrp png_ptr, png_fixed_point a, png_int_32 multiplied_by, png_int_32 divided_by)
 
png_fixed_point png_reciprocal (png_fixed_point a)
 
png_fixed_point png_reciprocal2 (png_fixed_point a, png_fixed_point b)
 
int png_gamma_significant (png_fixed_point gamma_value)
 
png_uint_16 png_gamma_correct (png_structrp png_ptr, unsigned int value, png_fixed_point gamma_value)
 
png_uint_16 png_gamma_16bit_correct (unsigned int value, png_fixed_point gamma_value)
 
png_byte png_gamma_8bit_correct (unsigned int value, png_fixed_point gamma_value)
 
void png_destroy_gamma_table (png_structrp png_ptr)
 
void png_build_gamma_table (png_structrp png_ptr, int bit_depth)
 
void() png_safe_error (png_structp png_ptr, png_const_charp error_message)
 
void() png_safe_warning (png_structp png_ptr, png_const_charp warning_message)
 
int png_safe_execute (png_imagep image, int(*function)(png_voidp), png_voidp arg)
 
int png_image_error (png_imagep image, png_const_charp error_message)
 
void png_init_filter_functions_neon (png_structp png_ptr, unsigned int bpp)
 

Variables

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 _POSIX_SOURCE   1 /* Just the POSIX 1003.1 and C89 APIs */
#define PNGLIB_BUILD   /*libpng is being built, not used*/
#define PNG_ARM_NEON_OPT   0
#define PNG_IMPEXP
#define PNG_DEPRECATED
#define PNG_PRIVATE
#define PNG_INTERNAL_DATA (   type,
  name,
  array 
)    extern type name array
#define PNG_INTERNAL_FUNCTION (   type,
  name,
  args,
  attributes 
)    extern PNG_FUNCTION(type, name, args, PNG_EMPTY attributes)
#define PNG_INTERNAL_CALLBACK (   type,
  name,
  args,
  attributes 
)    extern PNG_FUNCTION(type, (PNGCBAPI name), args, PNG_EMPTY attributes)
#define PNG_DLL_EXPORT
#define PNG_USER_WIDTH_MAX   0x7fffffff
#define PNG_USER_HEIGHT_MAX   0x7fffffff
#define PNG_USER_CHUNK_CACHE_MAX   0
#define PNG_USER_CHUNK_MALLOC_MAX   0
#define PNG_UNUSED (   param)    (void)param;
#define PNG_WARNING_PARAMETERS (   p)    png_warning_parameters p;
#define png_voidcast (   type,
  value 
)    (value)
#define png_constcast (   type,
  value 
)    ((type)(value))
#define png_aligncast (   type,
  value 
)    ((void*)(value))
#define png_aligncastconst (   type,
  value 
)    ((const void*)(value))
#define PNGFAPI   PNGAPI
#define PNG_ABORT ( )    abort()
#define PNG_ALIGN_NONE   0 /* do not use data alignment */
#define PNG_ALIGN_ALWAYS   1 /* assume unaligned accesses are OK */
#define PNG_ALIGN_OFFSET   -1 /* prevent the use of this */
#define PNG_ALIGN_SIZE   3 /* use sizeof to determine alignment */
#define PNG_ALIGN_TYPE   PNG_ALIGN_SIZE
#define png_alignof (   type)    (sizeof (type))
#define png_isaligned (   ptr,
  type 
)    ((((const char*)ptr-(const char*)0) & (png_alignof(type)-1)) == 0)
#define PNG_HAVE_IDAT   0x04
#define PNG_HAVE_IEND   0x10
#define PNG_HAVE_CHUNK_HEADER   0x100
#define PNG_WROTE_tIME   0x200
#define PNG_WROTE_INFO_BEFORE_PLTE   0x400
#define PNG_BACKGROUND_IS_GRAY   0x800
#define PNG_HAVE_PNG_SIGNATURE   0x1000
#define PNG_HAVE_CHUNK_AFTER_IDAT   0x2000 /* Have another chunk after IDAT */
#define PNG_IS_READ_STRUCT   0x8000 /* Else is a write struct */
#define PNG_BGR   0x0001
#define PNG_INTERLACE   0x0002
#define PNG_PACK   0x0004
#define PNG_SHIFT   0x0008
#define PNG_SWAP_BYTES   0x0010
#define PNG_INVERT_MONO   0x0020
#define PNG_QUANTIZE   0x0040
#define PNG_COMPOSE   0x0080 /* Was PNG_BACKGROUND */
#define PNG_BACKGROUND_EXPAND   0x0100
#define PNG_EXPAND_16   0x0200 /* Added to libpng 1.5.2 */
#define PNG_16_TO_8   0x0400 /* Becomes 'chop' in 1.5.4 */
#define PNG_RGBA   0x0800
#define PNG_EXPAND   0x1000
#define PNG_GAMMA   0x2000
#define PNG_GRAY_TO_RGB   0x4000
#define PNG_FILLER   0x8000
#define PNG_PACKSWAP   0x10000
#define PNG_SWAP_ALPHA   0x20000
#define PNG_STRIP_ALPHA   0x40000
#define PNG_INVERT_ALPHA   0x80000
#define PNG_USER_TRANSFORM   0x100000
#define PNG_RGB_TO_GRAY_ERR   0x200000
#define PNG_RGB_TO_GRAY_WARN   0x400000
#define PNG_RGB_TO_GRAY   0x600000 /* two bits, RGB_TO_GRAY_ERR|WARN */
#define PNG_ENCODE_ALPHA   0x800000 /* Added to libpng-1.5.4 */
#define PNG_ADD_ALPHA   0x1000000 /* Added to libpng-1.2.7 */
#define PNG_EXPAND_tRNS   0x2000000 /* Added to libpng-1.2.9 */
#define PNG_SCALE_16_TO_8   0x4000000 /* Added to libpng-1.5.4 */
#define PNG_STRUCT_PNG   0x0001
#define PNG_STRUCT_INFO   0x0002
#define PNG_WEIGHT_FACTOR   (1<<(PNG_WEIGHT_SHIFT))
#define PNG_COST_FACTOR   (1<<(PNG_COST_SHIFT))
#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY   0x0001
#define PNG_FLAG_ZSTREAM_INITIALIZED   0x0002 /* Added to libpng-1.6.0 */
#define PNG_FLAG_ZSTREAM_ENDED   0x0008 /* Added to libpng-1.6.0 */
#define PNG_FLAG_ROW_INIT   0x0040
#define PNG_FLAG_FILLER_AFTER   0x0080
#define PNG_FLAG_CRC_ANCILLARY_USE   0x0100
#define PNG_FLAG_CRC_ANCILLARY_NOWARN   0x0200
#define PNG_FLAG_CRC_CRITICAL_USE   0x0400
#define PNG_FLAG_CRC_CRITICAL_IGNORE   0x0800
#define PNG_FLAG_ASSUME_sRGB   0x1000 /* Added to libpng-1.5.4 */
#define PNG_FLAG_OPTIMIZE_ALPHA   0x2000 /* Added to libpng-1.5.4 */
#define PNG_FLAG_DETECT_UNINITIALIZED   0x4000 /* Added to libpng-1.5.4 */
#define PNG_FLAG_LIBRARY_MISMATCH   0x20000
#define PNG_FLAG_STRIP_ERROR_NUMBERS   0x40000
#define PNG_FLAG_STRIP_ERROR_TEXT   0x80000
#define PNG_FLAG_BENIGN_ERRORS_WARN   0x100000 /* Added to libpng-1.4.0 */
#define PNG_FLAG_APP_WARNINGS_WARN   0x200000 /* Added to libpng-1.6.0 */
#define PNG_FLAG_APP_ERRORS_WARN   0x400000 /* Added to libpng-1.6.0 */
#define PNG_FLAG_CRC_ANCILLARY_MASK
Value:
#define PNG_FLAG_CRC_ANCILLARY_USE
Definition: pngpriv.h:609
#define PNG_FLAG_CRC_ANCILLARY_NOWARN
Definition: pngpriv.h:610
#define PNG_FLAG_CRC_CRITICAL_MASK
Value:
#define PNG_FLAG_CRC_CRITICAL_IGNORE
Definition: pngpriv.h:612
#define PNG_FLAG_CRC_CRITICAL_USE
Definition: pngpriv.h:611
#define PNG_FLAG_CRC_MASK
Value:
#define PNG_FLAG_CRC_ANCILLARY_MASK
Definition: pngpriv.h:633
#define PNG_FLAG_CRC_CRITICAL_MASK
Definition: pngpriv.h:636
#define PNG_COLOR_DIST (   c1,
  c2 
)
Value:
(abs((int)((c1).red) - (int)((c2).red)) + \
abs((int)((c1).green) - (int)((c2).green)) + \
abs((int)((c1).blue) - (int)((c2).blue)))
#define PNG_DIV65535 (   v24)    (((v24) + 32895) >> 16)
#define PNG_DIV257 (   v16)    PNG_DIV65535((png_uint_32)(v16) * 255)
#define PNG_ROWBYTES (   pixel_bits,
  width 
)
Value:
((pixel_bits) >= 8 ? \
((png_size_t)(width) * (((png_size_t)(pixel_bits)) >> 3)) : \
(( ((png_size_t)(width) * ((png_size_t)(pixel_bits))) + 7) >> 3) )
size_t png_size_t
Definition: pngconf.h:543
#define PNG_OUT_OF_RANGE (   value,
  ideal,
  delta 
)    ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) )
#define png_float (   png_ptr,
  fixed,
 
)    (.00001 * (fixed))
#define PNG_32b (   b,
 
)    ((png_uint_32)(b) << (s))
#define PNG_U32 (   b1,
  b2,
  b3,
  b4 
)    (PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0))
#define png_IDAT   PNG_U32( 73, 68, 65, 84)
#define png_IEND   PNG_U32( 73, 69, 78, 68)
#define png_IHDR   PNG_U32( 73, 72, 68, 82)
#define png_PLTE   PNG_U32( 80, 76, 84, 69)
#define png_bKGD   PNG_U32( 98, 75, 71, 68)
#define png_cHRM   PNG_U32( 99, 72, 82, 77)
#define png_fRAc   PNG_U32(102, 82, 65, 99) /* registered, not defined */
#define png_gAMA   PNG_U32(103, 65, 77, 65)
#define png_gIFg   PNG_U32(103, 73, 70, 103)
#define png_gIFt   PNG_U32(103, 73, 70, 116) /* deprecated */
#define png_gIFx   PNG_U32(103, 73, 70, 120)
#define png_hIST   PNG_U32(104, 73, 83, 84)
#define png_iCCP   PNG_U32(105, 67, 67, 80)
#define png_iTXt   PNG_U32(105, 84, 88, 116)
#define png_oFFs   PNG_U32(111, 70, 70, 115)
#define png_pCAL   PNG_U32(112, 67, 65, 76)
#define png_pHYs   PNG_U32(112, 72, 89, 115)
#define png_sBIT   PNG_U32(115, 66, 73, 84)
#define png_sCAL   PNG_U32(115, 67, 65, 76)
#define png_sPLT   PNG_U32(115, 80, 76, 84)
#define png_sRGB   PNG_U32(115, 82, 71, 66)
#define png_sTER   PNG_U32(115, 84, 69, 82)
#define png_tEXt   PNG_U32(116, 69, 88, 116)
#define png_tIME   PNG_U32(116, 73, 77, 69)
#define png_tRNS   PNG_U32(116, 82, 78, 83)
#define png_zTXt   PNG_U32(122, 84, 88, 116)
#define PNG_CHUNK_FROM_STRING (   s)    PNG_U32(0xff&(s)[0], 0xff&(s)[1], 0xff&(s)[2], 0xff&(s)[3])
#define PNG_STRING_FROM_CHUNK (   s,
 
)
Value:
(void)(((char*)(s))[0]=(char)((c)>>24), ((char*)(s))[1]=(char)((c)>>16),\
((char*)(s))[2]=(char)((c)>>8), ((char*)(s))[3]=(char)((c)))
#define PNG_CSTRING_FROM_CHUNK (   s,
 
)    (void)(PNG_STRING_FROM_CHUNK(s,c), ((char*)(s))[4] = 0)
#define PNG_CHUNK_ANCILLARY (   c)    (1 & ((c) >> 29))
#define PNG_CHUNK_CRITICAL (   c)    (!PNG_CHUNK_ANCILLARY(c))
#define PNG_CHUNK_PRIVATE (   c)    (1 & ((c) >> 21))
#define PNG_CHUNK_RESERVED (   c)    (1 & ((c) >> 13))
#define PNG_CHUNK_SAFE_TO_COPY (   c)    (1 & ((c) >> 5))
#define PNG_GAMMA_MAC_OLD   151724 /* Assume '1.8' is really 2.2/1.45! */
#define PNG_GAMMA_MAC_INVERSE   65909
#define PNG_GAMMA_sRGB_INVERSE   45455
#define PNG_sRGB_FROM_LINEAR (   linear)
Value:
((png_byte)((png_sRGB_base[(linear)>>15] +\
((((linear)&0x7fff)*png_sRGB_delta[(linear)>>15])>>12)) >> 8))
const png_byte png_sRGB_delta[512]
Definition: png.c:4366
const png_uint_16 png_sRGB_base[512]
Definition: png.c:4298
#define PNG_UNEXPECTED_ZLIB_RETURN   (-7)
#define PNG_USE_COMPILE_TIME_MASKS   1
#define PNG_FORMAT_NUMBER (   buffer,
  format,
  number 
)    png_format_number(buffer, buffer + (sizeof buffer), format, number)
#define PNG_NUMBER_BUFFER_SIZE   24
#define PNG_NUMBER_FORMAT_u   1 /* chose unsigned API! */
#define PNG_NUMBER_FORMAT_02u   2
#define PNG_NUMBER_FORMAT_d   1 /* chose signed API! */
#define PNG_NUMBER_FORMAT_02d   2
#define PNG_NUMBER_FORMAT_x   3
#define PNG_NUMBER_FORMAT_02x   4
#define PNG_NUMBER_FORMAT_fixed   5 /* choose the signed API */
#define PNG_WARNING_PARAMETER_SIZE   32
#define PNG_WARNING_PARAMETER_COUNT   8 /* Maximum 9; see pngerror.c */
#define PNG_CHUNK_WARNING   0 /* never an error */
#define PNG_CHUNK_WRITE_ERROR   1 /* an error only on write */
#define PNG_CHUNK_ERROR   2 /* always an error */
#define PNG_sCAL_MAX_DIGITS   (PNG_sCAL_PRECISION+1/*.*/+1/*E*/+10/*exponent*/)
#define PNG_FP_INTEGER   0 /* before or in integer */
#define PNG_FP_FRACTION   1 /* before or in fraction */
#define PNG_FP_EXPONENT   2 /* before or in exponent */
#define PNG_FP_STATE   3 /* mask for the above */
#define PNG_FP_SAW_SIGN   4 /* Saw +/- in current state */
#define PNG_FP_SAW_DIGIT   8 /* Saw a digit in current state */
#define PNG_FP_SAW_DOT   16 /* Saw a dot in current state */
#define PNG_FP_SAW_E   32 /* Saw an E (or e) in current state */
#define PNG_FP_SAW_ANY   60 /* Saw any of the above 4 */
#define PNG_FP_WAS_VALID   64 /* Preceding substring is a valid fp number */
#define PNG_FP_NEGATIVE   128 /* A negative number, including "-0" */
#define PNG_FP_NONZERO   256 /* A non-zero value */
#define PNG_FP_STICKY   448 /* The above three flags */
#define PNG_FP_INVALID   512 /* Available for callers as a distinct value */
#define PNG_FP_MAYBE   0 /* The number may be valid in the future */
#define PNG_FP_OK   1 /* The number is valid */
#define PNG_FP_NZ_MASK   (PNG_FP_SAW_DIGIT | PNG_FP_NEGATIVE | PNG_FP_NONZERO)
#define PNG_FP_Z_MASK   (PNG_FP_SAW_DIGIT | PNG_FP_NONZERO)
#define PNG_FP_IS_ZERO (   state)    (((state) & PNG_FP_Z_MASK) == PNG_FP_SAW_DIGIT)
#define PNG_FP_IS_POSITIVE (   state)    (((state) & PNG_FP_NZ_MASK) == PNG_FP_Z_MASK)
#define PNG_FP_IS_NEGATIVE (   state)    (((state) & PNG_FP_NZ_MASK) == PNG_FP_NZ_MASK)
#define png_control_jmp_buf (   pc)    ((pc)->error_buf)

Typedef Documentation

typedef char png_warning_parameters[8][32]
typedef struct png_control png_control

Function Documentation

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
void png_free_buffer_list ( png_structrp  png_ptr,
png_compression_bufferp *  list 
)
441 {
442  png_compression_bufferp list = *listp;
443 
444  if (list != NULL)
445  {
446  *listp = NULL;
447 
448  do
449  {
450  png_compression_bufferp next = list->next;
451 
452  png_free(png_ptr, list);
453  list = next;
454  }
455  while (list != NULL);
456  }
457 }
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
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_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_voidp png_malloc_base ( png_const_structrp  png_ptr,
png_alloc_size_t  size 
)
70 {
71  /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS
72  * allocators have also been removed in 1.6.0, so any 16-bit system now has
73  * to implement a user memory handler. This checks to be sure it isn't
74  * called with big numbers.
75  */
76 #ifndef PNG_USER_MEM_SUPPORTED
77  PNG_UNUSED(png_ptr)
78 #endif
79 
80  if (size > 0 && size <= PNG_SIZE_MAX
81 # ifdef PNG_MAX_MALLOC_64K
82  && size <= 65536U
83 # endif
84  )
85  {
86 #ifdef PNG_USER_MEM_SUPPORTED
87  if (png_ptr != NULL && png_ptr->malloc_fn != NULL)
88  return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size);
89 
90  else
91 #endif
92  return malloc((size_t)size); /* checked for truncation above */
93  }
94 
95  else
96  return NULL;
97 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
Definition: pngstruct.h:144
#define PNG_SIZE_MAX
Definition: png.h:792
#define png_constcast(type, value)
Definition: pngpriv.h:409
png_voidp png_malloc_array ( png_const_structrp  png_ptr,
int  nelements,
size_t  element_size 
)
121 {
122  if (nelements <= 0 || element_size == 0)
123  png_error(png_ptr, "internal error: array alloc");
124 
125  return png_malloc_array_checked(png_ptr, nelements, element_size);
126 }
static png_voidp png_malloc_array_checked(png_const_structrp png_ptr, int nelements, size_t element_size)
Definition: pngmem.c:106
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_voidp png_realloc_array ( png_const_structrp  png_ptr,
png_const_voidp  array,
int  old_elements,
int  add_elements,
size_t  element_size 
)
131 {
132  /* These are internal errors: */
133  if (add_elements <= 0 || element_size == 0 || old_elements < 0 ||
134  (old_array == NULL && old_elements > 0))
135  png_error(png_ptr, "internal error: array realloc");
136 
137  /* Check for overflow on the elements count (so the caller does not have to
138  * check.)
139  */
140  if (add_elements <= INT_MAX - old_elements)
141  {
142  png_voidp new_array = png_malloc_array_checked(png_ptr,
143  old_elements+add_elements, element_size);
144 
145  if (new_array != NULL)
146  {
147  /* Because png_malloc_array worked the size calculations below cannot
148  * overflow.
149  */
150  if (old_elements > 0)
151  memcpy(new_array, old_array, element_size*(unsigned)old_elements);
152 
153  memset((char*)new_array + element_size*(unsigned)old_elements, 0,
154  element_size*(unsigned)add_elements);
155 
156  return new_array;
157  }
158  }
159 
160  return NULL; /* error */
161 }
static png_voidp png_malloc_array_checked(png_const_structrp png_ptr, int nelements, size_t element_size)
Definition: pngmem.c:106
void * png_voidp
Definition: pngconf.h:598
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
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
void png_destroy_png_struct ( png_structrp  png_ptr)
26 {
27  if (png_ptr != NULL)
28  {
29  /* png_free might call png_error and may certainly call
30  * png_get_mem_ptr, so fake a temporary png_struct to support this.
31  */
32  png_struct dummy_struct = *png_ptr;
33  memset(png_ptr, 0, (sizeof *png_ptr));
34  png_free(&dummy_struct, png_ptr);
35 
36 # ifdef PNG_SETJMP_SUPPORTED
37  /* We may have a jmp_buf left to deallocate. */
38  png_free_jmpbuf(&dummy_struct);
39 # endif
40  }
41 }
Definition: pngstruct.h:144
void png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void png_free_jmpbuf(png_structrp png_ptr)
Definition: pngerror.c:666
void png_free_jmpbuf ( png_structrp  png_ptr)
667 {
668  if (png_ptr != NULL)
669  {
670  jmp_buf *jb = png_ptr->jmp_buf_ptr;
671 
672  /* A size of 0 is used to indicate a local, stack, allocation of the
673  * pointer; used here and in png.c
674  */
675  if (jb != NULL && png_ptr->jmp_buf_size > 0)
676  {
677 
678  /* This stuff is so that a failure to free the error control structure
679  * does not leave libpng in a state with no valid error handling: the
680  * free always succeeds, if there is an error it gets ignored.
681  */
682  if (jb != &png_ptr->jmp_buf_local)
683  {
684  /* Make an internal, libpng, jmp_buf to return here */
685  jmp_buf free_jmp_buf;
686 
687  if (!setjmp(free_jmp_buf))
688  {
689  png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */
690  png_ptr->jmp_buf_size = 0; /* stack allocation */
691  png_ptr->longjmp_fn = longjmp;
692  png_free(png_ptr, jb); /* Return to setjmp on error */
693  }
694  }
695  }
696 
697  /* *Always* cancel everything out: */
698  png_ptr->jmp_buf_size = 0;
699  png_ptr->jmp_buf_ptr = NULL;
700  png_ptr->longjmp_fn = 0;
701  }
702 }
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
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_default_read_data ( png_structp  png_ptr,
png_bytep  data,
png_size_t  length 
)
51 {
52  png_size_t check;
53 
54  if (png_ptr == NULL)
55  return;
56 
57  /* fread() returns 0 on error, so it is OK to store this in a png_size_t
58  * instead of an int, which is what fread() actually returns.
59  */
60  check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr));
61 
62  if (check != length)
63  png_error(png_ptr, "Read Error");
64 }
#define png_voidcast(type, value)
Definition: pngpriv.h:408
png_voidp io_ptr
Definition: pngstruct.h:159
png_uint_32 length
Definition: png.c:2173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
size_t png_size_t
Definition: pngconf.h:543
void png_push_fill_buffer ( png_structp  png_ptr,
png_bytep  buffer,
png_size_t  length 
)
507 {
508  png_bytep ptr;
509 
510  if (png_ptr == NULL)
511  return;
512 
513  ptr = buffer;
514  if (png_ptr->save_buffer_size != 0)
515  {
516  png_size_t save_size;
517 
518  if (length < png_ptr->save_buffer_size)
519  save_size = length;
520 
521  else
522  save_size = png_ptr->save_buffer_size;
523 
524  memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
525  length -= save_size;
526  ptr += save_size;
527  png_ptr->buffer_size -= save_size;
528  png_ptr->save_buffer_size -= save_size;
529  png_ptr->save_buffer_ptr += save_size;
530  }
531  if (length != 0 && png_ptr->current_buffer_size != 0)
532  {
533  png_size_t save_size;
534 
535  if (length < png_ptr->current_buffer_size)
536  save_size = length;
537 
538  else
539  save_size = png_ptr->current_buffer_size;
540 
541  memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
542  png_ptr->buffer_size -= save_size;
543  png_ptr->current_buffer_size -= save_size;
544  png_ptr->current_buffer_ptr += save_size;
545  }
546 }
png_byte * png_bytep
Definition: pngconf.h:600
static char buffer[255]
Definition: propagator_simple.cpp:60
png_uint_32 length
Definition: png.c:2173
size_t png_size_t
Definition: pngconf.h:543
void png_default_write_data ( png_structp  png_ptr,
png_bytep  data,
png_size_t  length 
)
52 {
53  png_size_t check;
54 
55  if (png_ptr == NULL)
56  return;
57 
58  check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr));
59 
60  if (check != length)
61  png_error(png_ptr, "Write Error");
62 }
png_voidp io_ptr
Definition: pngstruct.h:159
png_uint_32 length
Definition: png.c:2173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
size_t png_size_t
Definition: pngconf.h:543
void png_default_flush ( png_structp  png_ptr)
80 {
81  png_FILE_p io_ptr;
82 
83  if (png_ptr == NULL)
84  return;
85 
86  io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr));
87  fflush(io_ptr);
88 }
#define png_voidcast(type, value)
Definition: pngpriv.h:408
png_voidp io_ptr
Definition: pngstruct.h:159
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_write_data ( png_structrp  png_ptr,
png_const_bytep  data,
png_size_t  length 
)
34 {
35  /* NOTE: write_data_fn must not change the buffer! */
36  if (png_ptr->write_data_fn != NULL )
37  (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data),
38  length);
39 
40  else
41  png_error(png_ptr, "Call to NULL write function");
42 }
png_byte * png_bytep
Definition: pngconf.h:600
png_rw_ptr write_data_fn
Definition: pngstruct.h:157
#define png_constcast(type, value)
Definition: pngpriv.h:409
png_uint_32 length
Definition: png.c:2173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
void png_read_sig ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
116 {
117  png_size_t num_checked, num_to_check;
118 
119  /* Exit if the user application does not expect a signature. */
120  if (png_ptr->sig_bytes >= 8)
121  return;
122 
123  num_checked = png_ptr->sig_bytes;
124  num_to_check = 8 - num_checked;
125 
126 #ifdef PNG_IO_STATE_SUPPORTED
127  png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE;
128 #endif
129 
130  /* The signature must be serialized in a single I/O call. */
131  png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check);
132  png_ptr->sig_bytes = 8;
133 
134  if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0)
135  {
136  if (num_checked < 4 &&
137  png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
138  png_error(png_ptr, "Not a PNG file");
139  else
140  png_error(png_ptr, "PNG file corrupted by ASCII conversion");
141  }
142  if (num_checked < 3)
143  png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
144 }
#define PNG_IO_SIGNATURE
Definition: png.h:2577
#define PNG_IO_READING
Definition: png.h:2575
void png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)
Definition: pngrio.c:32
png_byte sig_bytes
Definition: pngstruct.h:260
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
size_t png_size_t
Definition: pngconf.h:543
#define PNG_HAVE_PNG_SIGNATURE
Definition: pngpriv.h:554
int png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
Definition: png.c:49
png_uint_32 png_read_chunk_header ( png_structrp  png_ptr)
151 {
152  png_byte buf[8];
153  png_uint_32 length;
154 
155 #ifdef PNG_IO_STATE_SUPPORTED
156  png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR;
157 #endif
158 
159  /* Read the length and the chunk name.
160  * This must be performed in a single I/O call.
161  */
162  png_read_data(png_ptr, buf, 8);
163  length = png_get_uint_31(png_ptr, buf);
164 
165  /* Put the chunk name into png_ptr->chunk_name. */
166  png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4);
167 
168  png_debug2(0, "Reading %lx chunk, length = %lu",
169  (unsigned long)png_ptr->chunk_name, (unsigned long)length);
170 
171  /* Reset the crc and run it over the chunk name. */
172  png_reset_crc(png_ptr);
173  png_calculate_crc(png_ptr, buf + 4, 4);
174 
175  /* Check to see if chunk name is valid. */
176  png_check_chunk_name(png_ptr, png_ptr->chunk_name);
177 
178 #ifdef PNG_IO_STATE_SUPPORTED
179  png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA;
180 #endif
181 
182  return length;
183 }
png_uint_32 png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:22
#define PNG_IO_CHUNK_DATA
Definition: png.h:2579
void png_reset_crc(png_structrp png_ptr)
Definition: png.c:102
png_uint_32 chunk_name
Definition: pngstruct.h:221
#define PNG_CHUNK_FROM_STRING(s)
Definition: pngpriv.h:782
#define PNG_IO_CHUNK_HDR
Definition: png.h:2578
void png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
Definition: png.c:114
#define PNG_IO_READING
Definition: png.h:2575
void png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)
Definition: pngrio.c:32
png_uint_32 length
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
void png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Definition: pngrutil.c:2977
#define png_debug2(l, m, p1, p2)
Definition: pngdebug.h:152
void png_read_data ( png_structrp  png_ptr,
png_bytep  data,
png_size_t  length 
)
33 {
34  png_debug1(4, "reading %d bytes", (int)length);
35 
36  if (png_ptr->read_data_fn != NULL)
37  (*(png_ptr->read_data_fn))(png_ptr, data, length);
38 
39  else
40  png_error(png_ptr, "Call to NULL read function");
41 }
png_rw_ptr read_data_fn
Definition: pngstruct.h:158
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
png_uint_32 length
Definition: png.c:2173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
void png_crc_read ( png_structrp  png_ptr,
png_bytep  buf,
png_uint_32  length 
)
188 {
189  if (png_ptr == NULL)
190  return;
191 
192  png_read_data(png_ptr, buf, length);
193  png_calculate_crc(png_ptr, buf, length);
194 }
void png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
Definition: png.c:114
void png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)
Definition: pngrio.c:32
png_uint_32 length
Definition: png.c:2173
char buf[128]
Definition: rw_test.cpp:40
int png_crc_finish ( png_structrp  png_ptr,
png_uint_32  skip 
)
203 {
204  /* The size of the local buffer for inflate is a good guess as to a
205  * reasonable size to use for buffering reads from the application.
206  */
207  while (skip > 0)
208  {
209  png_uint_32 len;
210  png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
211 
212  len = (sizeof tmpbuf);
213  if (len > skip)
214  len = skip;
215  skip -= len;
216 
217  png_crc_read(png_ptr, tmpbuf, len);
218  }
219 
220  if (png_crc_error(png_ptr) != 0)
221  {
222  if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ?
223  (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 :
224  (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0)
225  {
226  png_chunk_warning(png_ptr, "CRC error");
227  }
228 
229  else
230  png_chunk_error(png_ptr, "CRC error");
231 
232  return (1);
233  }
234 
235  return (0);
236 }
#define PNG_CHUNK_ANCILLARY(c)
Definition: pngpriv.h:798
int png_crc_error(png_structrp png_ptr)
Definition: pngrutil.c:242
png_uint_32 chunk_name
Definition: pngstruct.h:221
#define PNG_INFLATE_BUF_SIZE
Definition: pnglibconf.h:194
PNG_IMPEXP void() png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:497
png_uint_32 flags
Definition: pngstruct.h:180
#define PNG_FLAG_CRC_CRITICAL_USE
Definition: pngpriv.h:611
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
#define PNG_FLAG_CRC_ANCILLARY_NOWARN
Definition: pngpriv.h:610
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
int png_crc_error ( png_structrp  png_ptr)
243 {
244  png_byte crc_bytes[4];
245  png_uint_32 crc;
246  int need_crc = 1;
247 
248  if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0)
249  {
250  if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) ==
252  need_crc = 0;
253  }
254 
255  else /* critical */
256  {
257  if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0)
258  need_crc = 0;
259  }
260 
261 #ifdef PNG_IO_STATE_SUPPORTED
262  png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC;
263 #endif
264 
265  /* The chunk CRC must be serialized in a single I/O call. */
266  png_read_data(png_ptr, crc_bytes, 4);
267 
268  if (need_crc != 0)
269  {
270  crc = png_get_uint_32(crc_bytes);
271  return ((int)(crc != png_ptr->crc));
272  }
273 
274  else
275  return (0);
276 }
#define PNG_CHUNK_ANCILLARY(c)
Definition: pngpriv.h:798
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
#define PNG_FLAG_CRC_ANCILLARY_NOWARN
Definition: pngpriv.h:610
#define PNG_IO_READING
Definition: png.h:2575
#define PNG_IO_CHUNK_CRC
Definition: png.h:2580
void png_read_data(png_structrp png_ptr, png_bytep data, png_size_t length)
Definition: pngrio.c:32
png_uint_32() png_get_uint_32(png_const_bytep buf)
Definition: pngrutil.c:68
#define PNG_FLAG_CRC_ANCILLARY_MASK
Definition: pngpriv.h:633
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
void png_flush ( png_structrp  png_ptr)
72 {
73  if (png_ptr->output_flush_fn != NULL)
74  (*(png_ptr->output_flush_fn))(png_ptr);
75 }
void png_write_IHDR ( png_structrp  png_ptr,
png_uint_32  width,
png_uint_32  height,
int  bit_depth,
int  color_type,
int  compression_method,
int  filter_method,
int  interlace_method 
)
760 {
761  png_byte buf[13]; /* Buffer to store the IHDR info */
762 
763  png_debug(1, "in png_write_IHDR");
764 
765  /* Check that we have valid input data from the application info */
766  switch (color_type)
767  {
768  case PNG_COLOR_TYPE_GRAY:
769  switch (bit_depth)
770  {
771  case 1:
772  case 2:
773  case 4:
774  case 8:
775 #ifdef PNG_WRITE_16BIT_SUPPORTED
776  case 16:
777 #endif
778  png_ptr->channels = 1; break;
779 
780  default:
781  png_error(png_ptr,
782  "Invalid bit depth for grayscale image");
783  }
784  break;
785 
786  case PNG_COLOR_TYPE_RGB:
787 #ifdef PNG_WRITE_16BIT_SUPPORTED
788  if (bit_depth != 8 && bit_depth != 16)
789 #else
790  if (bit_depth != 8)
791 #endif
792  png_error(png_ptr, "Invalid bit depth for RGB image");
793 
794  png_ptr->channels = 3;
795  break;
796 
798  switch (bit_depth)
799  {
800  case 1:
801  case 2:
802  case 4:
803  case 8:
804  png_ptr->channels = 1;
805  break;
806 
807  default:
808  png_error(png_ptr, "Invalid bit depth for paletted image");
809  }
810  break;
811 
813  if (bit_depth != 8 && bit_depth != 16)
814  png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
815 
816  png_ptr->channels = 2;
817  break;
818 
820 #ifdef PNG_WRITE_16BIT_SUPPORTED
821  if (bit_depth != 8 && bit_depth != 16)
822 #else
823  if (bit_depth != 8)
824 #endif
825  png_error(png_ptr, "Invalid bit depth for RGBA image");
826 
827  png_ptr->channels = 4;
828  break;
829 
830  default:
831  png_error(png_ptr, "Invalid image color type specified");
832  }
833 
834  if (compression_type != PNG_COMPRESSION_TYPE_BASE)
835  {
836  png_warning(png_ptr, "Invalid compression type specified");
837  compression_type = PNG_COMPRESSION_TYPE_BASE;
838  }
839 
840  /* Write filter_method 64 (intrapixel differencing) only if
841  * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
842  * 2. Libpng did not write a PNG signature (this filter_method is only
843  * used in PNG datastreams that are embedded in MNG datastreams) and
844  * 3. The application called png_permit_mng_features with a mask that
845  * included PNG_FLAG_MNG_FILTER_64 and
846  * 4. The filter_method is 64 and
847  * 5. The color_type is RGB or RGBA
848  */
849  if (
851  !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
852  ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) &&
853  (color_type == PNG_COLOR_TYPE_RGB ||
854  color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
855  (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
856 #endif
857  filter_type != PNG_FILTER_TYPE_BASE)
858  {
859  png_warning(png_ptr, "Invalid filter type specified");
860  filter_type = PNG_FILTER_TYPE_BASE;
861  }
862 
863 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
864  if (interlace_type != PNG_INTERLACE_NONE &&
865  interlace_type != PNG_INTERLACE_ADAM7)
866  {
867  png_warning(png_ptr, "Invalid interlace type specified");
868  interlace_type = PNG_INTERLACE_ADAM7;
869  }
870 #else
871  interlace_type=PNG_INTERLACE_NONE;
872 #endif
873 
874  /* Save the relevent information */
875  png_ptr->bit_depth = (png_byte)bit_depth;
876  png_ptr->color_type = (png_byte)color_type;
877  png_ptr->interlaced = (png_byte)interlace_type;
878 #ifdef PNG_MNG_FEATURES_SUPPORTED
879  png_ptr->filter_type = (png_byte)filter_type;
880 #endif
881  png_ptr->compression_type = (png_byte)compression_type;
882  png_ptr->width = width;
883  png_ptr->height = height;
884 
885  png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
886  png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
887  /* Set the usr info, so any transformations can modify it */
888  png_ptr->usr_width = png_ptr->width;
889  png_ptr->usr_bit_depth = png_ptr->bit_depth;
890  png_ptr->usr_channels = png_ptr->channels;
891 
892  /* Pack the header information into the buffer */
893  png_save_uint_32(buf, width);
894  png_save_uint_32(buf + 4, height);
895  buf[8] = (png_byte)bit_depth;
896  buf[9] = (png_byte)color_type;
897  buf[10] = (png_byte)compression_type;
898  buf[11] = (png_byte)filter_type;
899  buf[12] = (png_byte)interlace_type;
900 
901  /* Write the chunk */
902  png_write_complete_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
903 
904  if ((png_ptr->do_filter) == PNG_NO_FILTERS)
905  {
906  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
907  png_ptr->bit_depth < 8)
908  png_ptr->do_filter = PNG_FILTER_NONE;
909 
910  else
911  png_ptr->do_filter = PNG_ALL_FILTERS;
912  }
913 
914  png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */
915 }
#define PNG_NO_FILTERS
Definition: png.h:1600
#define PNG_INTERLACE_NONE
Definition: png.h:828
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
#define PNG_INTERLACE_ADAM7
Definition: png.h:829
png_uint_32 width
Definition: pngstruct.h:214
#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_debug(l, m)
Definition: pngdebug.h:146
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
void png_save_uint_32(png_bytep buf, png_uint_32 i)
Definition: pngwutil.c:24
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
png_byte do_filter
Definition: pngstruct.h:251
png_byte color_type
Definition: pngstruct.h:252
#define PNG_MNG_FEATURES_SUPPORTED
Definition: pnglibconf.h:45
#define PNG_HAVE_IHDR
Definition: png.h:785
#define png_IHDR
Definition: pngpriv.h:754
png_byte compression_type
Definition: pngstruct.h:434
png_uint_32 usr_width
Definition: pngstruct.h:217
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
#define PNG_ALL_FILTERS
Definition: png.h:1606
png_byte pixel_depth
Definition: pngstruct.h:255
#define PNG_FLAG_MNG_FILTER_64
Definition: png.h:996
png_byte bit_depth
Definition: pngstruct.h:253
#define PNG_FILTER_TYPE_BASE
Definition: png.h:823
png_byte channels
Definition: pngstruct.h:256
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
png_size_t rowbytes
Definition: pngstruct.h:218
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
png_byte usr_bit_depth
Definition: pngstruct.h:254
size_t png_size_t
Definition: pngconf.h:543
#define PNG_INTRAPIXEL_DIFFERENCING
Definition: png.h:824
char buf[128]
Definition: rw_test.cpp:40
png_uint_32 height
Definition: pngstruct.h:215
#define PNG_FILTER_NONE
Definition: png.h:1601
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
#define PNG_HAVE_PNG_SIGNATURE
Definition: pngpriv.h:554
png_byte interlaced
Definition: pngstruct.h:249
void png_write_PLTE ( png_structrp  png_ptr,
png_const_colorp  palette,
png_uint_32  num_pal 
)
924 {
925  png_uint_32 i;
926  png_const_colorp pal_ptr;
927  png_byte buf[3];
928 
929  png_debug(1, "in png_write_PLTE");
930 
931  if ((
933  (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 &&
934 #endif
935  num_pal == 0) || num_pal > 256)
936  {
937  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
938  {
939  png_error(png_ptr, "Invalid number of colors in palette");
940  }
941 
942  else
943  {
944  png_warning(png_ptr, "Invalid number of colors in palette");
945  return;
946  }
947  }
948 
949  if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
950  {
951  png_warning(png_ptr,
952  "Ignoring request to write a PLTE chunk in grayscale PNG");
953 
954  return;
955  }
956 
957  png_ptr->num_palette = (png_uint_16)num_pal;
958  png_debug1(3, "num_palette = %d", png_ptr->num_palette);
959 
960  png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3));
961 #ifdef PNG_POINTER_INDEXING_SUPPORTED
962 
963  for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
964  {
965  buf[0] = pal_ptr->red;
966  buf[1] = pal_ptr->green;
967  buf[2] = pal_ptr->blue;
968  png_write_chunk_data(png_ptr, buf, (png_size_t)3);
969  }
970 
971 #else
972  /* This is a little slower but some buggy compilers need to do this
973  * instead
974  */
975  pal_ptr=palette;
976 
977  for (i = 0; i < num_pal; i++)
978  {
979  buf[0] = pal_ptr[i].red;
980  buf[1] = pal_ptr[i].green;
981  buf[2] = pal_ptr[i].blue;
982  png_write_chunk_data(png_ptr, buf, (png_size_t)3);
983  }
984 
985 #endif
986  png_write_chunk_end(png_ptr);
987  png_ptr->mode |= PNG_HAVE_PLTE;
988 }
#define png_PLTE
Definition: pngpriv.h:755
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
int i
Definition: rw_test.cpp:37
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
png_byte red
Definition: png.h:621
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
#define PNG_HAVE_PLTE
Definition: png.h:786
png_byte color_type
Definition: pngstruct.h:252
#define PNG_MNG_FEATURES_SUPPORTED
Definition: pnglibconf.h:45
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
png_uint_16 num_palette
Definition: pngstruct.h:239
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
Definition: png.h:619
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
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
size_t png_size_t
Definition: pngconf.h:543
#define PNG_FLAG_MNG_EMPTY_PLTE
Definition: png.h:995
char buf[128]
Definition: rw_test.cpp:40
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
png_byte green
Definition: png.h:622
png_byte blue
Definition: png.h:623
void png_compress_IDAT ( png_structrp  png_ptr,
png_const_bytep  row_data,
png_alloc_size_t  row_data_length,
int  flush 
)
1010 {
1011  if (png_ptr->zowner != png_IDAT)
1012  {
1013  /* First time. Ensure we have a temporary buffer for compression and
1014  * trim the buffer list if it has more than one entry to free memory.
1015  * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been
1016  * created at this point, but the check here is quick and safe.
1017  */
1018  if (png_ptr->zbuffer_list == NULL)
1019  {
1020  png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp,
1021  png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr)));
1022  png_ptr->zbuffer_list->next = NULL;
1023  }
1024 
1025  else
1026  png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next);
1027 
1028  /* It is a terminal error if we can't claim the zstream. */
1029  if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK)
1030  png_error(png_ptr, png_ptr->zstream.msg);
1031 
1032  /* The output state is maintained in png_ptr->zstream, so it must be
1033  * initialized here after the claim.
1034  */
1035  png_ptr->zstream.next_out = png_ptr->zbuffer_list->output;
1036  png_ptr->zstream.avail_out = png_ptr->zbuffer_size;
1037  }
1038 
1039  /* Now loop reading and writing until all the input is consumed or an error
1040  * terminates the operation. The _out values are maintained across calls to
1041  * this function, but the input must be reset each time.
1042  */
1043  png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input);
1044  png_ptr->zstream.avail_in = 0; /* set below */
1045  for (;;)
1046  {
1047  int ret;
1048 
1049  /* INPUT: from the row data */
1050  uInt avail = ZLIB_IO_MAX;
1051 
1052  if (avail > input_len)
1053  avail = (uInt)input_len; /* safe because of the check */
1054 
1055  png_ptr->zstream.avail_in = avail;
1056  input_len -= avail;
1057 
1058  ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush);
1059 
1060  /* Include as-yet unconsumed input */
1061  input_len += png_ptr->zstream.avail_in;
1062  png_ptr->zstream.avail_in = 0;
1063 
1064  /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note
1065  * that these two zstream fields are preserved across the calls, therefore
1066  * there is no need to set these up on entry to the loop.
1067  */
1068  if (png_ptr->zstream.avail_out == 0)
1069  {
1070  png_bytep data = png_ptr->zbuffer_list->output;
1071  uInt size = png_ptr->zbuffer_size;
1072 
1073  /* Write an IDAT containing the data then reset the buffer. The
1074  * first IDAT may need deflate header optimization.
1075  */
1076 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
1077  if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
1079  optimize_cmf(data, png_image_size(png_ptr));
1080 #endif
1081 
1082  png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1083  png_ptr->mode |= PNG_HAVE_IDAT;
1084 
1085  png_ptr->zstream.next_out = data;
1086  png_ptr->zstream.avail_out = size;
1087 
1088  /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with
1089  * the same flush parameter until it has finished output, for NO_FLUSH
1090  * it doesn't matter.
1091  */
1092  if (ret == Z_OK && flush != Z_NO_FLUSH)
1093  continue;
1094  }
1095 
1096  /* The order of these checks doesn't matter much; it just affects which
1097  * possible error might be detected if multiple things go wrong at once.
1098  */
1099  if (ret == Z_OK) /* most likely return code! */
1100  {
1101  /* If all the input has been consumed then just return. If Z_FINISH
1102  * was used as the flush parameter something has gone wrong if we get
1103  * here.
1104  */
1105  if (input_len == 0)
1106  {
1107  if (flush == Z_FINISH)
1108  png_error(png_ptr, "Z_OK on Z_FINISH with output space");
1109 
1110  return;
1111  }
1112  }
1113 
1114  else if (ret == Z_STREAM_END && flush == Z_FINISH)
1115  {
1116  /* This is the end of the IDAT data; any pending output must be
1117  * flushed. For small PNG files we may still be at the beginning.
1118  */
1119  png_bytep data = png_ptr->zbuffer_list->output;
1120  uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out;
1121 
1122 #ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
1123  if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 &&
1125  optimize_cmf(data, png_image_size(png_ptr));
1126 #endif
1127 
1128  png_write_complete_chunk(png_ptr, png_IDAT, data, size);
1129  png_ptr->zstream.avail_out = 0;
1130  png_ptr->zstream.next_out = NULL;
1131  png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT;
1132 
1133  png_ptr->zowner = 0; /* Release the stream */
1134  return;
1135  }
1136 
1137  else
1138  {
1139  /* This is an error condition. */
1140  png_zstream_error(png_ptr, ret);
1141  png_error(png_ptr, png_ptr->zstream.msg);
1142  }
1143  }
1144 }
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
z_stream zstream
Definition: pngstruct.h:184
Bytef * next_in
Definition: zlib.h:86
#define Z_NO_FLUSH
Definition: zlib.h:164
uInt avail_in
Definition: zlib.h:87
png_byte * png_bytep
Definition: pngconf.h:600
char * msg
Definition: zlib.h:94
#define png_voidcast(type, value)
Definition: pngpriv.h:408
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
void png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp)
Definition: pngwutil.c:440
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define Z_FINISH
Definition: zlib.h:168
png_byte compression_type
Definition: pngstruct.h:434
#define ZLIB_IO_MAX
Definition: pngstruct.h:57
static int png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, png_alloc_size_t data_size)
Definition: pngwutil.c:293
#define Z_STREAM_END
Definition: zlib.h:174
Bytef * next_out
Definition: zlib.h:90
#define png_IDAT
Definition: pngpriv.h:752
#define PNG_AFTER_IDAT
Definition: png.h:787
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:665
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
static png_alloc_size_t png_image_size(png_structrp png_ptr)
Definition: pngwutil.c:205
static void optimize_cmf(png_bytep data, png_alloc_size_t data_size)
Definition: pngwutil.c:251
png_uint_32 mode
Definition: pngstruct.h:179
#define PNGZ_INPUT_CAST(b)
Definition: pngstruct.h:42
uInt avail_out
Definition: zlib.h:91
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_uint_32 zowner
Definition: pngstruct.h:183
void png_zstream_error(png_structrp png_ptr, int ret)
Definition: png.c:958
unsigned int uInt
Definition: zconf.h:370
void png_write_IEND ( png_structrp  png_ptr)
1149 {
1150  png_debug(1, "in png_write_IEND");
1151 
1152  png_write_complete_chunk(png_ptr, png_IEND, NULL, (png_size_t)0);
1153  png_ptr->mode |= PNG_HAVE_IEND;
1154 }
#define png_IEND
Definition: pngpriv.h:753
#define png_debug(l, m)
Definition: pngdebug.h:146
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
png_uint_32 mode
Definition: pngstruct.h:179
#define PNG_HAVE_IEND
Definition: pngpriv.h:546
size_t png_size_t
Definition: pngconf.h:543
void png_write_gAMA_fixed ( png_structrp  png_ptr,
png_fixed_point  file_gamma 
)
1160 {
1161  png_byte buf[4];
1162 
1163  png_debug(1, "in png_write_gAMA");
1164 
1165  /* file_gamma is saved in 1/100,000ths */
1166  png_save_uint_32(buf, (png_uint_32)file_gamma);
1167  png_write_complete_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
1168 }
#define png_debug(l, m)
Definition: pngdebug.h:146
void png_save_uint_32(png_bytep buf, png_uint_32 i)
Definition: pngwutil.c:24
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define png_gAMA
Definition: pngpriv.h:759
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
void png_write_sBIT ( png_structrp  png_ptr,
png_const_color_8p  sbit,
int  color_type 
)
1340 {
1341  png_byte buf[4];
1342  png_size_t size;
1343 
1344  png_debug(1, "in png_write_sBIT");
1345 
1346  /* Make sure we don't depend upon the order of PNG_COLOR_8 */
1347  if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
1348  {
1349  png_byte maxbits;
1350 
1351  maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
1352  png_ptr->usr_bit_depth);
1353 
1354  if (sbit->red == 0 || sbit->red > maxbits ||
1355  sbit->green == 0 || sbit->green > maxbits ||
1356  sbit->blue == 0 || sbit->blue > maxbits)
1357  {
1358  png_warning(png_ptr, "Invalid sBIT depth specified");
1359  return;
1360  }
1361 
1362  buf[0] = sbit->red;
1363  buf[1] = sbit->green;
1364  buf[2] = sbit->blue;
1365  size = 3;
1366  }
1367 
1368  else
1369  {
1370  if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
1371  {
1372  png_warning(png_ptr, "Invalid sBIT depth specified");
1373  return;
1374  }
1375 
1376  buf[0] = sbit->gray;
1377  size = 1;
1378  }
1379 
1380  if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
1381  {
1382  if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
1383  {
1384  png_warning(png_ptr, "Invalid sBIT depth specified");
1385  return;
1386  }
1387 
1388  buf[size++] = sbit->alpha;
1389  }
1390 
1391  png_write_complete_chunk(png_ptr, png_sBIT, buf, size);
1392 }
png_byte blue
Definition: png.h:645
png_byte red
Definition: png.h:643
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
png_byte green
Definition: png.h:644
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
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
#define PNG_COLOR_MASK_ALPHA
Definition: png.h:806
png_byte usr_bit_depth
Definition: pngstruct.h:254
size_t png_size_t
Definition: pngconf.h:543
png_byte alpha
Definition: png.h:647
char buf[128]
Definition: rw_test.cpp:40
#define png_sBIT
Definition: pngpriv.h:769
png_byte gray
Definition: png.h:646
void png_write_cHRM_fixed ( png_structrp  png_ptr,
const png_xy *  xy 
)
1399 {
1400  png_byte buf[32];
1401 
1402  png_debug(1, "in png_write_cHRM");
1403 
1404  /* Each value is saved in 1/100,000ths */
1405  png_save_int_32(buf, xy->whitex);
1406  png_save_int_32(buf + 4, xy->whitey);
1407 
1408  png_save_int_32(buf + 8, xy->redx);
1409  png_save_int_32(buf + 12, xy->redy);
1410 
1411  png_save_int_32(buf + 16, xy->greenx);
1412  png_save_int_32(buf + 20, xy->greeny);
1413 
1414  png_save_int_32(buf + 24, xy->bluex);
1415  png_save_int_32(buf + 28, xy->bluey);
1416 
1417  png_write_complete_chunk(png_ptr, png_cHRM, buf, 32);
1418 }
#define png_debug(l, m)
Definition: pngdebug.h:146
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
void png_save_int_32(png_bytep buf, png_int_32 i)
Definition: png.c:677
#define png_cHRM
Definition: pngpriv.h:757
char buf[128]
Definition: rw_test.cpp:40
void png_write_sRGB ( png_structrp  png_ptr,
int  intent 
)
1175 {
1176  png_byte buf[1];
1177 
1178  png_debug(1, "in png_write_sRGB");
1179 
1180  if (srgb_intent >= PNG_sRGB_INTENT_LAST)
1181  png_warning(png_ptr,
1182  "Invalid sRGB rendering intent specified");
1183 
1184  buf[0]=(png_byte)srgb_intent;
1185  png_write_complete_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
1186 }
#define png_sRGB
Definition: pngpriv.h:772
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
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define PNG_sRGB_INTENT_LAST
Definition: png.h:860
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
void png_write_iCCP ( png_structrp  png_ptr,
png_const_charp  name,
png_const_bytep  profile 
)
1194 {
1195  png_uint_32 name_len;
1196  png_uint_32 profile_len;
1197  png_byte new_name[81]; /* 1 byte for the compression byte */
1198  compression_state comp;
1199  png_uint_32 temp;
1200 
1201  png_debug(1, "in png_write_iCCP");
1202 
1203  /* These are all internal problems: the profile should have been checked
1204  * before when it was stored.
1205  */
1206  if (profile == NULL)
1207  png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */
1208 
1209  profile_len = png_get_uint_32(profile);
1210 
1211  if (profile_len < 132)
1212  png_error(png_ptr, "ICC profile too short");
1213 
1214  temp = (png_uint_32) (*(profile+8));
1215  if (temp > 3 && (profile_len & 0x03))
1216  png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)");
1217 
1218  {
1219  png_uint_32 embedded_profile_len = png_get_uint_32(profile);
1220 
1221  if (profile_len != embedded_profile_len)
1222  png_error(png_ptr, "Profile length does not match profile");
1223  }
1224 
1225  name_len = png_check_keyword(png_ptr, name, new_name);
1226 
1227  if (name_len == 0)
1228  png_error(png_ptr, "iCCP: invalid keyword");
1229 
1230  new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE;
1231 
1232  /* Make sure we include the NULL after the name and the compression type */
1233  ++name_len;
1234 
1235  png_text_compress_init(&comp, profile, profile_len);
1236 
1237  /* Allow for keyword terminator and compression byte */
1238  if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK)
1239  png_error(png_ptr, png_ptr->zstream.msg);
1240 
1241  png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len);
1242 
1243  png_write_chunk_data(png_ptr, new_name, name_len);
1244 
1245  png_write_compressed_data_out(png_ptr, &comp);
1246 
1247  png_write_chunk_end(png_ptr);
1248 }
Definition: pngwutil.c:470
z_stream zstream
Definition: pngstruct.h:184
#define png_get_uint_32(buf)
Definition: png.h:2744
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
#define png_iCCP
Definition: pngpriv.h:764
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
png_uint_32 output_len
Definition: pngwutil.c:474
static void png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
Definition: pngwutil.c:638
string name
Definition: cubesat2obj.cpp:6
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
static void png_text_compress_init(compression_state *comp, png_const_bytep input, png_alloc_size_t input_len)
Definition: pngwutil.c:479
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
static int png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, compression_state *comp, png_uint_32 prefix_len)
Definition: pngwutil.c:489
void png_write_sPLT ( png_structrp  png_ptr,
png_const_sPLT_tp  palette 
)
1255 {
1256  png_uint_32 name_len;
1257  png_byte new_name[80];
1258  png_byte entrybuf[10];
1259  png_size_t entry_size = (spalette->depth == 8 ? 6 : 10);
1260  png_size_t palette_size = entry_size * spalette->nentries;
1261  png_sPLT_entryp ep;
1262 #ifndef PNG_POINTER_INDEXING_SUPPORTED
1263  int i;
1264 #endif
1265 
1266  png_debug(1, "in png_write_sPLT");
1267 
1268  name_len = png_check_keyword(png_ptr, spalette->name, new_name);
1269 
1270  if (name_len == 0)
1271  png_error(png_ptr, "sPLT: invalid keyword");
1272 
1273  /* Make sure we include the NULL after the name */
1275  (png_uint_32)(name_len + 2 + palette_size));
1276 
1277  png_write_chunk_data(png_ptr, (png_bytep)new_name,
1278  (png_size_t)(name_len + 1));
1279 
1280  png_write_chunk_data(png_ptr, &spalette->depth, (png_size_t)1);
1281 
1282  /* Loop through each palette entry, writing appropriately */
1283 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1284  for (ep = spalette->entries; ep<spalette->entries + spalette->nentries; ep++)
1285  {
1286  if (spalette->depth == 8)
1287  {
1288  entrybuf[0] = (png_byte)ep->red;
1289  entrybuf[1] = (png_byte)ep->green;
1290  entrybuf[2] = (png_byte)ep->blue;
1291  entrybuf[3] = (png_byte)ep->alpha;
1292  png_save_uint_16(entrybuf + 4, ep->frequency);
1293  }
1294 
1295  else
1296  {
1297  png_save_uint_16(entrybuf + 0, ep->red);
1298  png_save_uint_16(entrybuf + 2, ep->green);
1299  png_save_uint_16(entrybuf + 4, ep->blue);
1300  png_save_uint_16(entrybuf + 6, ep->alpha);
1301  png_save_uint_16(entrybuf + 8, ep->frequency);
1302  }
1303 
1304  png_write_chunk_data(png_ptr, entrybuf, entry_size);
1305  }
1306 #else
1307  ep=spalette->entries;
1308  for (i = 0; i>spalette->nentries; i++)
1309  {
1310  if (spalette->depth == 8)
1311  {
1312  entrybuf[0] = (png_byte)ep[i].red;
1313  entrybuf[1] = (png_byte)ep[i].green;
1314  entrybuf[2] = (png_byte)ep[i].blue;
1315  entrybuf[3] = (png_byte)ep[i].alpha;
1316  png_save_uint_16(entrybuf + 4, ep[i].frequency);
1317  }
1318 
1319  else
1320  {
1321  png_save_uint_16(entrybuf + 0, ep[i].red);
1322  png_save_uint_16(entrybuf + 2, ep[i].green);
1323  png_save_uint_16(entrybuf + 4, ep[i].blue);
1324  png_save_uint_16(entrybuf + 6, ep[i].alpha);
1325  png_save_uint_16(entrybuf + 8, ep[i].frequency);
1326  }
1327 
1328  png_write_chunk_data(png_ptr, entrybuf, entry_size);
1329  }
1330 #endif
1331 
1332  png_write_chunk_end(png_ptr);
1333 }
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
png_uint_16 frequency
Definition: png.h:663
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
#define png_sPLT
Definition: pngpriv.h:771
#define png_debug(l, m)
Definition: pngdebug.h:146
png_uint_16 alpha
Definition: png.h:662
png_uint_16 blue
Definition: png.h:661
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
Definition: png.h:657
png_uint_16 green
Definition: png.h:660
png_uint_16 red
Definition: png.h:659
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
size_t png_size_t
Definition: pngconf.h:543
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
void png_save_uint_16(png_bytep buf, unsigned int i)
Definition: pngwutil.c:37
void png_write_tRNS ( png_structrp  png_ptr,
png_const_bytep  trans,
png_const_color_16p  values,
int  number,
int  color_type 
)
1426 {
1427  png_byte buf[6];
1428 
1429  png_debug(1, "in png_write_tRNS");
1430 
1431  if (color_type == PNG_COLOR_TYPE_PALETTE)
1432  {
1433  if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
1434  {
1435  png_app_warning(png_ptr,
1436  "Invalid number of transparent colors specified");
1437  return;
1438  }
1439 
1440  /* Write the chunk out as it is */
1441  png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha,
1442  (png_size_t)num_trans);
1443  }
1444 
1445  else if (color_type == PNG_COLOR_TYPE_GRAY)
1446  {
1447  /* One 16 bit value */
1448  if (tran->gray >= (1 << png_ptr->bit_depth))
1449  {
1450  png_app_warning(png_ptr,
1451  "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
1452 
1453  return;
1454  }
1455 
1456  png_save_uint_16(buf, tran->gray);
1457  png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
1458  }
1459 
1460  else if (color_type == PNG_COLOR_TYPE_RGB)
1461  {
1462  /* Three 16 bit values */
1463  png_save_uint_16(buf, tran->red);
1464  png_save_uint_16(buf + 2, tran->green);
1465  png_save_uint_16(buf + 4, tran->blue);
1466 #ifdef PNG_WRITE_16BIT_SUPPORTED
1467  if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
1468 #else
1469  if ((buf[0] | buf[2] | buf[4]) != 0)
1470 #endif
1471  {
1472  png_app_warning(png_ptr,
1473  "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
1474  return;
1475  }
1476 
1477  png_write_complete_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
1478  }
1479 
1480  else
1481  {
1482  png_app_warning(png_ptr, "Can't write tRNS with an alpha channel");
1483  }
1484 }
#define png_tRNS
Definition: pngpriv.h:776
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define png_debug(l, m)
Definition: pngdebug.h:146
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
png_uint_16 num_palette
Definition: pngstruct.h:239
png_byte bit_depth
Definition: pngstruct.h:253
size_t png_size_t
Definition: pngconf.h:543
void png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:392
char buf[128]
Definition: rw_test.cpp:40
void png_save_uint_16(png_bytep buf, unsigned int i)
Definition: pngwutil.c:37
void png_write_bKGD ( png_structrp  png_ptr,
png_const_color_16p  values,
int  color_type 
)
1491 {
1492  png_byte buf[6];
1493 
1494  png_debug(1, "in png_write_bKGD");
1495 
1496  if (color_type == PNG_COLOR_TYPE_PALETTE)
1497  {
1498  if (
1500  (png_ptr->num_palette != 0 ||
1501  (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) &&
1502 #endif
1503  back->index >= png_ptr->num_palette)
1504  {
1505  png_warning(png_ptr, "Invalid background palette index");
1506  return;
1507  }
1508 
1509  buf[0] = back->index;
1510  png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
1511  }
1512 
1513  else if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
1514  {
1515  png_save_uint_16(buf, back->red);
1516  png_save_uint_16(buf + 2, back->green);
1517  png_save_uint_16(buf + 4, back->blue);
1518 #ifdef PNG_WRITE_16BIT_SUPPORTED
1519  if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0)
1520 #else
1521  if ((buf[0] | buf[2] | buf[4]) != 0)
1522 #endif
1523  {
1524  png_warning(png_ptr,
1525  "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
1526 
1527  return;
1528  }
1529 
1530  png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
1531  }
1532 
1533  else
1534  {
1535  if (back->gray >= (1 << png_ptr->bit_depth))
1536  {
1537  png_warning(png_ptr,
1538  "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
1539 
1540  return;
1541  }
1542 
1543  png_save_uint_16(buf, back->gray);
1544  png_write_complete_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
1545  }
1546 }
#define png_bKGD
Definition: pngpriv.h:756
#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_debug(l, m)
Definition: pngdebug.h:146
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define PNG_MNG_FEATURES_SUPPORTED
Definition: pnglibconf.h:45
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
png_uint_16 num_palette
Definition: pngstruct.h:239
png_byte bit_depth
Definition: pngstruct.h:253
size_t png_size_t
Definition: pngconf.h:543
#define PNG_FLAG_MNG_EMPTY_PLTE
Definition: png.h:995
char buf[128]
Definition: rw_test.cpp:40
void png_save_uint_16(png_bytep buf, unsigned int i)
Definition: pngwutil.c:37
void png_write_hIST ( png_structrp  png_ptr,
png_const_uint_16p  hist,
int  num_hist 
)
1553 {
1554  int i;
1555  png_byte buf[3];
1556 
1557  png_debug(1, "in png_write_hIST");
1558 
1559  if (num_hist > (int)png_ptr->num_palette)
1560  {
1561  png_debug2(3, "num_hist = %d, num_palette = %d", num_hist,
1562  png_ptr->num_palette);
1563 
1564  png_warning(png_ptr, "Invalid number of histogram entries specified");
1565  return;
1566  }
1567 
1568  png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
1569 
1570  for (i = 0; i < num_hist; i++)
1571  {
1572  png_save_uint_16(buf, hist[i]);
1573  png_write_chunk_data(png_ptr, buf, (png_size_t)2);
1574  }
1575 
1576  png_write_chunk_end(png_ptr);
1577 }
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
int i
Definition: rw_test.cpp:37
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_uint_16 num_palette
Definition: pngstruct.h:239
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
#define png_hIST
Definition: pngpriv.h:763
void png_save_uint_16(png_bytep buf, unsigned int i)
Definition: pngwutil.c:37
#define png_debug2(l, m, p1, p2)
Definition: pngdebug.h:152
void png_write_tEXt ( png_structrp  png_ptr,
png_const_charp  key,
png_const_charp  text,
png_size_t  text_len 
)
1585 {
1586  png_uint_32 key_len;
1587  png_byte new_key[80];
1588 
1589  png_debug(1, "in png_write_tEXt");
1590 
1591  key_len = png_check_keyword(png_ptr, key, new_key);
1592 
1593  if (key_len == 0)
1594  png_error(png_ptr, "tEXt: invalid keyword");
1595 
1596  if (text == NULL || *text == '\0')
1597  text_len = 0;
1598 
1599  else
1600  text_len = strlen(text);
1601 
1602  if (text_len > PNG_UINT_31_MAX - (key_len+1))
1603  png_error(png_ptr, "tEXt: text too long");
1604 
1605  /* Make sure we include the 0 after the key */
1607  (png_uint_32)/*checked above*/(key_len + text_len + 1));
1608  /*
1609  * We leave it to the application to meet PNG-1.0 requirements on the
1610  * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1611  * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
1612  * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1613  */
1614  png_write_chunk_data(png_ptr, new_key, key_len + 1);
1615 
1616  if (text_len != 0)
1617  png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len);
1618 
1619  png_write_chunk_end(png_ptr);
1620 }
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
#define PNG_UINT_31_MAX
Definition: png.h:790
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
#define png_debug(l, m)
Definition: pngdebug.h:146
#define png_tEXt
Definition: pngpriv.h:774
const png_byte * png_const_bytep
Definition: pngconf.h:601
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
void png_write_zTXt ( png_structrp  png_ptr,
png_const_charp  key,
png_const_charp  text,
int  compression 
)
1628 {
1629  png_uint_32 key_len;
1630  png_byte new_key[81];
1631  compression_state comp;
1632 
1633  png_debug(1, "in png_write_zTXt");
1634 
1635  if (compression == PNG_TEXT_COMPRESSION_NONE)
1636  {
1637  png_write_tEXt(png_ptr, key, text, 0);
1638  return;
1639  }
1640 
1641  if (compression != PNG_TEXT_COMPRESSION_zTXt)
1642  png_error(png_ptr, "zTXt: invalid compression type");
1643 
1644  key_len = png_check_keyword(png_ptr, key, new_key);
1645 
1646  if (key_len == 0)
1647  png_error(png_ptr, "zTXt: invalid keyword");
1648 
1649  /* Add the compression method and 1 for the keyword separator. */
1650  new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
1651  ++key_len;
1652 
1653  /* Compute the compressed data; do it now for the length */
1655  text == NULL ? 0 : strlen(text));
1656 
1657  if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK)
1658  png_error(png_ptr, png_ptr->zstream.msg);
1659 
1660  /* Write start of chunk */
1661  png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len);
1662 
1663  /* Write key */
1664  png_write_chunk_data(png_ptr, new_key, key_len);
1665 
1666  /* Write the compressed data */
1667  png_write_compressed_data_out(png_ptr, &comp);
1668 
1669  /* Close the chunk */
1670  png_write_chunk_end(png_ptr);
1671 }
Definition: pngwutil.c:470
z_stream zstream
Definition: pngstruct.h:184
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
png_uint_32 output_len
Definition: pngwutil.c:474
void png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, png_size_t text_len)
Definition: pngwutil.c:1583
#define PNG_TEXT_COMPRESSION_zTXt
Definition: png.h:730
#define PNG_TEXT_COMPRESSION_NONE
Definition: png.h:729
static void png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
Definition: pngwutil.c:638
const png_byte * png_const_bytep
Definition: pngconf.h:601
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
static void png_text_compress_init(compression_state *comp, png_const_bytep input, png_alloc_size_t input_len)
Definition: pngwutil.c:479
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
static int png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, compression_state *comp, png_uint_32 prefix_len)
Definition: pngwutil.c:489
#define png_zTXt
Definition: pngpriv.h:777
void png_write_iTXt ( png_structrp  png_ptr,
int  compression,
png_const_charp  key,
png_const_charp  lang,
png_const_charp  lang_key,
png_const_charp  text 
)
1679 {
1680  png_uint_32 key_len, prefix_len;
1681  png_size_t lang_len, lang_key_len;
1682  png_byte new_key[82];
1683  compression_state comp;
1684 
1685  png_debug(1, "in png_write_iTXt");
1686 
1687  key_len = png_check_keyword(png_ptr, key, new_key);
1688 
1689  if (key_len == 0)
1690  png_error(png_ptr, "iTXt: invalid keyword");
1691 
1692  /* Set the compression flag */
1693  switch (compression)
1694  {
1697  compression = new_key[++key_len] = 0; /* no compression */
1698  break;
1699 
1702  compression = new_key[++key_len] = 1; /* compressed */
1703  break;
1704 
1705  default:
1706  png_error(png_ptr, "iTXt: invalid compression");
1707  }
1708 
1709  new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE;
1710  ++key_len; /* for the keywod separator */
1711 
1712  /* We leave it to the application to meet PNG-1.0 requirements on the
1713  * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
1714  * any non-Latin-1 characters except for NEWLINE. ISO PNG, however,
1715  * specifies that the text is UTF-8 and this really doesn't require any
1716  * checking.
1717  *
1718  * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
1719  *
1720  * TODO: validate the language tag correctly (see the spec.)
1721  */
1722  if (lang == NULL) lang = ""; /* empty language is valid */
1723  lang_len = strlen(lang)+1;
1724  if (lang_key == NULL) lang_key = ""; /* may be empty */
1725  lang_key_len = strlen(lang_key)+1;
1726  if (text == NULL) text = ""; /* may be empty */
1727 
1728  prefix_len = key_len;
1729  if (lang_len > PNG_UINT_31_MAX-prefix_len)
1730  prefix_len = PNG_UINT_31_MAX;
1731  else
1732  prefix_len = (png_uint_32)(prefix_len + lang_len);
1733 
1734  if (lang_key_len > PNG_UINT_31_MAX-prefix_len)
1735  prefix_len = PNG_UINT_31_MAX;
1736  else
1737  prefix_len = (png_uint_32)(prefix_len + lang_key_len);
1738 
1739  png_text_compress_init(&comp, (png_const_bytep)text, strlen(text));
1740 
1741  if (compression != 0)
1742  {
1743  if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK)
1744  png_error(png_ptr, png_ptr->zstream.msg);
1745  }
1746 
1747  else
1748  {
1749  if (comp.input_len > PNG_UINT_31_MAX-prefix_len)
1750  png_error(png_ptr, "iTXt: uncompressed text too long");
1751 
1752  /* So the string will fit in a chunk: */
1753  comp.output_len = (png_uint_32)/*SAFE*/comp.input_len;
1754  }
1755 
1756  png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len);
1757 
1758  png_write_chunk_data(png_ptr, new_key, key_len);
1759 
1760  png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len);
1761 
1762  png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len);
1763 
1764  if (compression != 0)
1765  png_write_compressed_data_out(png_ptr, &comp);
1766 
1767  else
1768  png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.input_len);
1769 
1770  png_write_chunk_end(png_ptr);
1771 }
Definition: pngwutil.c:470
png_alloc_size_t input_len
Definition: pngwutil.c:473
z_stream zstream
Definition: pngstruct.h:184
#define png_iTXt
Definition: pngpriv.h:765
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
#define PNG_UINT_31_MAX
Definition: png.h:790
#define PNG_ITXT_COMPRESSION_zTXt
Definition: png.h:732
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
png_uint_32 output_len
Definition: pngwutil.c:474
#define PNG_TEXT_COMPRESSION_zTXt
Definition: png.h:730
#define PNG_TEXT_COMPRESSION_NONE
Definition: png.h:729
#define PNG_ITXT_COMPRESSION_NONE
Definition: png.h:731
static void png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp)
Definition: pngwutil.c:638
const png_byte * png_const_bytep
Definition: pngconf.h:601
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
size_t png_size_t
Definition: pngconf.h:543
static void png_text_compress_init(compression_state *comp, png_const_bytep input, png_alloc_size_t input_len)
Definition: pngwutil.c:479
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
static int png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, compression_state *comp, png_uint_32 prefix_len)
Definition: pngwutil.c:489
int png_set_text_2 ( png_const_structrp  png_ptr,
png_inforp  info_ptr,
png_const_textp  text_ptr,
int  num_text 
)
701 {
702  int i;
703 
704  png_debug1(1, "in %lx storage function", png_ptr == NULL ? "unexpected" :
705  (unsigned long)png_ptr->chunk_name);
706 
707  if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL)
708  return(0);
709 
710  /* Make sure we have enough space in the "text" array in info_struct
711  * to hold all of the incoming text_ptr objects. This compare can't overflow
712  * because max_text >= num_text (anyway, subtract of two positive integers
713  * can't overflow in any case.)
714  */
715  if (num_text > info_ptr->max_text - info_ptr->num_text)
716  {
717  int old_num_text = info_ptr->num_text;
718  int max_text;
719  png_textp new_text = NULL;
720 
721  /* Calculate an appropriate max_text, checking for overflow. */
722  max_text = old_num_text;
723  if (num_text <= INT_MAX - max_text)
724  {
725  max_text += num_text;
726 
727  /* Round up to a multiple of 8 */
728  if (max_text < INT_MAX-8)
729  max_text = (max_text + 8) & ~0x7;
730 
731  else
732  max_text = INT_MAX;
733 
734  /* Now allocate a new array and copy the old members in; this does all
735  * the overflow checks.
736  */
737  new_text = png_voidcast(png_textp,png_realloc_array(png_ptr,
738  info_ptr->text, old_num_text, max_text-old_num_text,
739  sizeof *new_text));
740  }
741 
742  if (new_text == NULL)
743  {
744  png_chunk_report(png_ptr, "too many text chunks",
746  return 1;
747  }
748 
749  png_free(png_ptr, info_ptr->text);
750 
751  info_ptr->text = new_text;
752  info_ptr->free_me |= PNG_FREE_TEXT;
753  info_ptr->max_text = max_text;
754  /* num_text is adjusted below as the entries are copied in */
755 
756  png_debug1(3, "allocated %d entries for info_ptr->text", max_text);
757  }
758 
759  for (i = 0; i < num_text; i++)
760  {
761  size_t text_length, key_len;
762  size_t lang_len, lang_key_len;
763  png_textp textp = &(info_ptr->text[info_ptr->num_text]);
764 
765  if (text_ptr[i].key == NULL)
766  continue;
767 
768  if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE ||
769  text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST)
770  {
771  png_chunk_report(png_ptr, "text compression mode is out of range",
773  continue;
774  }
775 
776  key_len = strlen(text_ptr[i].key);
777 
778  if (text_ptr[i].compression <= 0)
779  {
780  lang_len = 0;
781  lang_key_len = 0;
782  }
783 
784  else
785 # ifdef PNG_iTXt_SUPPORTED
786  {
787  /* Set iTXt data */
788 
789  if (text_ptr[i].lang != NULL)
790  lang_len = strlen(text_ptr[i].lang);
791 
792  else
793  lang_len = 0;
794 
795  if (text_ptr[i].lang_key != NULL)
796  lang_key_len = strlen(text_ptr[i].lang_key);
797 
798  else
799  lang_key_len = 0;
800  }
801 # else /* PNG_iTXt_SUPPORTED */
802  {
803  png_chunk_report(png_ptr, "iTXt chunk not supported",
805  continue;
806  }
807 # endif
808 
809  if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0')
810  {
811  text_length = 0;
812 # ifdef PNG_iTXt_SUPPORTED
813  if (text_ptr[i].compression > 0)
815 
816  else
817 # endif
819  }
820 
821  else
822  {
823  text_length = strlen(text_ptr[i].text);
824  textp->compression = text_ptr[i].compression;
825  }
826 
827  textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr,
828  key_len + text_length + lang_len + lang_key_len + 4));
829 
830  if (textp->key == NULL)
831  {
832  png_chunk_report(png_ptr, "text chunk: out of memory",
834  return 1;
835  }
836 
837  png_debug2(2, "Allocated %lu bytes at %p in png_set_text",
838  (unsigned long)(png_uint_32)
839  (key_len + lang_len + lang_key_len + text_length + 4),
840  textp->key);
841 
842  memcpy(textp->key, text_ptr[i].key, key_len);
843  *(textp->key + key_len) = '\0';
844 
845  if (text_ptr[i].compression > 0)
846  {
847  textp->lang = textp->key + key_len + 1;
848  memcpy(textp->lang, text_ptr[i].lang, lang_len);
849  *(textp->lang + lang_len) = '\0';
850  textp->lang_key = textp->lang + lang_len + 1;
851  memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len);
852  *(textp->lang_key + lang_key_len) = '\0';
853  textp->text = textp->lang_key + lang_key_len + 1;
854  }
855 
856  else
857  {
858  textp->lang=NULL;
859  textp->lang_key=NULL;
860  textp->text = textp->key + key_len + 1;
861  }
862 
863  if (text_length != 0)
864  memcpy(textp->text, text_ptr[i].text, text_length);
865 
866  *(textp->text + text_length) = '\0';
867 
868 # ifdef PNG_iTXt_SUPPORTED
869  if (textp->compression > 0)
870  {
871  textp->text_length = 0;
872  textp->itxt_length = text_length;
873  }
874 
875  else
876 # endif
877  {
878  textp->text_length = text_length;
879  textp->itxt_length = 0;
880  }
881 
882  info_ptr->num_text++;
883  png_debug1(3, "transferred text chunk %d", info_ptr->num_text);
884  }
885 
886  return(0);
887 }
#define PNG_TEXT_COMPRESSION_LAST
Definition: png.h:733
#define PNG_FREE_TEXT
Definition: png.h:1924
png_charp text
Definition: png.h:711
int compression
Definition: png.h:705
int i
Definition: rw_test.cpp:37
char * png_charp
Definition: pngconf.h:610
png_charp lang
Definition: png.h:715
png_voidp png_realloc_array(png_const_structrp png_ptr, png_const_voidp old_array, int old_elements, int add_elements, size_t element_size)
Definition: pngmem.c:130
#define png_voidcast(type, value)
Definition: pngpriv.h:408
png_voidp png_malloc_base(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:69
png_uint_32 chunk_name
Definition: pngstruct.h:221
png_charp key
Definition: png.h:710
#define PNG_TEXT_COMPRESSION_NONE
Definition: png.h:729
Definition: png.h:703
#define PNG_ITXT_COMPRESSION_NONE
Definition: png.h:731
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
#define PNG_CHUNK_WRITE_ERROR
Definition: pngpriv.h:1673
png_size_t itxt_length
Definition: png.h:714
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error)
Definition: pngerror.c:531
png_charp lang_key
Definition: png.h:717
png_size_t text_length
Definition: png.h:713
png_uint_32 free_me
Definition: pnginfo.h:221
#define png_debug2(l, m, p1, p2)
Definition: pngdebug.h:152
void png_write_oFFs ( png_structrp  png_ptr,
png_int_32  x_offset,
png_int_32  y_offset,
int  unit_type 
)
1779 {
1780  png_byte buf[9];
1781 
1782  png_debug(1, "in png_write_oFFs");
1783 
1784  if (unit_type >= PNG_OFFSET_LAST)
1785  png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
1786 
1787  png_save_int_32(buf, x_offset);
1788  png_save_int_32(buf + 4, y_offset);
1789  buf[8] = (png_byte)unit_type;
1790 
1791  png_write_complete_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
1792 }
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
#define PNG_OFFSET_LAST
Definition: png.h:835
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
void png_save_int_32(png_bytep buf, png_int_32 i)
Definition: png.c:677
#define png_oFFs
Definition: pngpriv.h:766
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
void png_write_pCAL ( png_structrp  png_ptr,
png_charp  purpose,
png_int_32  X0,
png_int_32  X1,
int  type,
int  nparams,
png_const_charp  units,
png_charpp  params 
)
1800 {
1801  png_uint_32 purpose_len;
1802  png_size_t units_len, total_len;
1803  png_size_tp params_len;
1804  png_byte buf[10];
1805  png_byte new_purpose[80];
1806  int i;
1807 
1808  png_debug1(1, "in png_write_pCAL (%d parameters)", nparams);
1809 
1810  if (type >= PNG_EQUATION_LAST)
1811  png_error(png_ptr, "Unrecognized equation type for pCAL chunk");
1812 
1813  purpose_len = png_check_keyword(png_ptr, purpose, new_purpose);
1814 
1815  if (purpose_len == 0)
1816  png_error(png_ptr, "pCAL: invalid keyword");
1817 
1818  ++purpose_len; /* terminator */
1819 
1820  png_debug1(3, "pCAL purpose length = %d", (int)purpose_len);
1821  units_len = strlen(units) + (nparams == 0 ? 0 : 1);
1822  png_debug1(3, "pCAL units length = %d", (int)units_len);
1823  total_len = purpose_len + units_len + 10;
1824 
1825  params_len = (png_size_tp)png_malloc(png_ptr,
1826  (png_alloc_size_t)(nparams * (sizeof (png_size_t))));
1827 
1828  /* Find the length of each parameter, making sure we don't count the
1829  * null terminator for the last parameter.
1830  */
1831  for (i = 0; i < nparams; i++)
1832  {
1833  params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
1834  png_debug2(3, "pCAL parameter %d length = %lu", i,
1835  (unsigned long)params_len[i]);
1836  total_len += params_len[i];
1837  }
1838 
1839  png_debug1(3, "pCAL total length = %d", (int)total_len);
1840  png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len);
1841  png_write_chunk_data(png_ptr, new_purpose, purpose_len);
1842  png_save_int_32(buf, X0);
1843  png_save_int_32(buf + 4, X1);
1844  buf[8] = (png_byte)type;
1845  buf[9] = (png_byte)nparams;
1846  png_write_chunk_data(png_ptr, buf, (png_size_t)10);
1847  png_write_chunk_data(png_ptr, (png_const_bytep)units, (png_size_t)units_len);
1848 
1849  for (i = 0; i < nparams; i++)
1850  {
1851  png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]);
1852  }
1853 
1854  png_free(png_ptr, params_len);
1855  png_write_chunk_end(png_ptr);
1856 }
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
void png_write_chunk_end(png_structrp png_ptr)
Definition: pngwutil.c:147
int i
Definition: rw_test.cpp:37
static png_uint_32 png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key)
Definition: pngwutil.c:681
void png_save_int_32(png_bytep buf, png_int_32 i)
Definition: png.c:677
png_size_t png_alloc_size_t
Definition: pngconf.h:578
const png_byte * png_const_bytep
Definition: pngconf.h:601
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
void png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:127
#define PNG_EQUATION_LAST
Definition: png.h:842
#define png_pCAL
Definition: pngpriv.h:767
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
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
static void png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, png_uint_32 length)
Definition: pngwutil.c:73
png_size_t * png_size_tp
Definition: pngconf.h:614
#define png_debug2(l, m, p1, p2)
Definition: pngdebug.h:152
void png_write_pHYs ( png_structrp  png_ptr,
png_uint_32  x_pixels_per_unit,
png_uint_32  y_pixels_per_unit,
int  unit_type 
)
1895 {
1896  png_byte buf[9];
1897 
1898  png_debug(1, "in png_write_pHYs");
1899 
1900  if (unit_type >= PNG_RESOLUTION_LAST)
1901  png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
1902 
1903  png_save_uint_32(buf, x_pixels_per_unit);
1904  png_save_uint_32(buf + 4, y_pixels_per_unit);
1905  buf[8] = (png_byte)unit_type;
1906 
1907  png_write_complete_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
1908 }
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
void png_save_uint_32(png_bytep buf, png_uint_32 i)
Definition: pngwutil.c:24
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define PNG_RESOLUTION_LAST
Definition: png.h:853
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
#define png_pHYs
Definition: pngpriv.h:768
void png_write_tIME ( png_structrp  png_ptr,
png_const_timep  mod_time 
)
1917 {
1918  png_byte buf[7];
1919 
1920  png_debug(1, "in png_write_tIME");
1921 
1922  if (mod_time->month > 12 || mod_time->month < 1 ||
1923  mod_time->day > 31 || mod_time->day < 1 ||
1924  mod_time->hour > 23 || mod_time->second > 60)
1925  {
1926  png_warning(png_ptr, "Invalid time specified for tIME chunk");
1927  return;
1928  }
1929 
1930  png_save_uint_16(buf, mod_time->year);
1931  buf[2] = mod_time->month;
1932  buf[3] = mod_time->day;
1933  buf[4] = mod_time->hour;
1934  buf[5] = mod_time->minute;
1935  buf[6] = mod_time->second;
1936 
1937  png_write_complete_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
1938 }
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
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
png_byte hour
Definition: png.h:746
png_byte second
Definition: png.h:748
png_byte minute
Definition: png.h:747
png_byte day
Definition: png.h:745
png_byte month
Definition: png.h:744
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
void png_save_uint_16(png_bytep buf, unsigned int i)
Definition: pngwutil.c:37
#define png_tIME
Definition: pngpriv.h:775
png_uint_16 year
Definition: png.h:743
void png_write_sCAL_s ( png_structrp  png_ptr,
int  unit,
png_const_charp  width,
png_const_charp  height 
)
1864 {
1865  png_byte buf[64];
1866  png_size_t wlen, hlen, total_len;
1867 
1868  png_debug(1, "in png_write_sCAL_s");
1869 
1870  wlen = strlen(width);
1871  hlen = strlen(height);
1872  total_len = wlen + hlen + 2;
1873 
1874  if (total_len > 64)
1875  {
1876  png_warning(png_ptr, "Can't write sCAL (buffer too small)");
1877  return;
1878  }
1879 
1880  buf[0] = (png_byte)unit;
1881  memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */
1882  memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */
1883 
1884  png_debug1(3, "sCAL total length = %u", (unsigned int)total_len);
1885  png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len);
1886 }
#define png_sCAL
Definition: pngpriv.h:770
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
static void png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, png_const_bytep data, png_size_t length)
Definition: pngwutil.c:176
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
size_t png_size_t
Definition: pngconf.h:543
char buf[128]
Definition: rw_test.cpp:40
void png_write_finish_row ( png_structrp  png_ptr)
2051 {
2052 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2053  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2054 
2055  /* Start of interlace block */
2056  static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2057 
2058  /* Offset to next interlace block */
2059  static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2060 
2061  /* Start of interlace block in the y direction */
2062  static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
2063 
2064  /* Offset to next interlace block in the y direction */
2065  static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
2066 #endif
2067 
2068  png_debug(1, "in png_write_finish_row");
2069 
2070  /* Next row */
2071  png_ptr->row_number++;
2072 
2073  /* See if we are done */
2074  if (png_ptr->row_number < png_ptr->num_rows)
2075  return;
2076 
2077 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2078  /* If interlaced, go to next pass */
2079  if (png_ptr->interlaced != 0)
2080  {
2081  png_ptr->row_number = 0;
2082  if ((png_ptr->transformations & PNG_INTERLACE) != 0)
2083  {
2084  png_ptr->pass++;
2085  }
2086 
2087  else
2088  {
2089  /* Loop until we find a non-zero width or height pass */
2090  do
2091  {
2092  png_ptr->pass++;
2093 
2094  if (png_ptr->pass >= 7)
2095  break;
2096 
2097  png_ptr->usr_width = (png_ptr->width +
2098  png_pass_inc[png_ptr->pass] - 1 -
2099  png_pass_start[png_ptr->pass]) /
2100  png_pass_inc[png_ptr->pass];
2101 
2102  png_ptr->num_rows = (png_ptr->height +
2103  png_pass_yinc[png_ptr->pass] - 1 -
2104  png_pass_ystart[png_ptr->pass]) /
2105  png_pass_yinc[png_ptr->pass];
2106 
2107  if ((png_ptr->transformations & PNG_INTERLACE) != 0)
2108  break;
2109 
2110  } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
2111 
2112  }
2113 
2114  /* Reset the row above the image for the next pass */
2115  if (png_ptr->pass < 7)
2116  {
2117  if (png_ptr->prev_row != NULL)
2118  memset(png_ptr->prev_row, 0,
2119  (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
2120  png_ptr->usr_bit_depth, png_ptr->width)) + 1);
2121 
2122  return;
2123  }
2124  }
2125 #endif
2126 
2127  /* If we get here, we've just written the last row, so we need
2128  to flush the compressor */
2129  png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH);
2130 }
png_uint_32 row_number
Definition: pngstruct.h:220
png_uint_32 width
Definition: pngstruct.h:214
#define png_debug(l, m)
Definition: pngdebug.h:146
void png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, png_alloc_size_t input_len, int flush)
Definition: pngwutil.c:1008
#define Z_FINISH
Definition: zlib.h:168
png_bytep prev_row
Definition: pngstruct.h:222
png_uint_32 usr_width
Definition: pngstruct.h:217
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_INTERLACE
Definition: pngpriv.h:561
png_uint_32 num_rows
Definition: pngstruct.h:216
png_byte usr_bit_depth
Definition: pngstruct.h:254
size_t png_size_t
Definition: pngconf.h:543
png_uint_32 height
Definition: pngstruct.h:215
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
void png_write_start_row ( png_structrp  png_ptr)
1944 {
1945 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
1946  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1947 
1948  /* Start of interlace block */
1949  static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
1950 
1951  /* Offset to next interlace block */
1952  static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
1953 
1954  /* Start of interlace block in the y direction */
1955  static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
1956 
1957  /* Offset to next interlace block in the y direction */
1958  static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
1959 #endif
1960 
1961  png_alloc_size_t buf_size;
1962  int usr_pixel_depth;
1963 
1964  png_debug(1, "in png_write_start_row");
1965 
1966  usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth;
1967  buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1;
1968 
1969  /* 1.5.6: added to allow checking in the row write code. */
1970  png_ptr->transformed_pixel_depth = png_ptr->pixel_depth;
1971  png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth;
1972 
1973  /* Set up row buffer */
1974  png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, buf_size);
1975 
1976  png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
1977 
1978 #ifdef PNG_WRITE_FILTER_SUPPORTED
1979  /* Set up filtering buffer, if using this filter */
1980  if (png_ptr->do_filter & PNG_FILTER_SUB)
1981  {
1982  png_ptr->sub_row = (png_bytep)png_malloc(png_ptr, png_ptr->rowbytes + 1);
1983 
1984  png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1985  }
1986 
1987  /* We only need to keep the previous row if we are using one of these. */
1988  if ((png_ptr->do_filter &
1990  {
1991  /* Set up previous row buffer */
1992  png_ptr->prev_row = (png_bytep)png_calloc(png_ptr, buf_size);
1993 
1994  if ((png_ptr->do_filter & PNG_FILTER_UP) != 0)
1995  {
1996  png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1997  png_ptr->rowbytes + 1);
1998 
1999  png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
2000  }
2001 
2002  if ((png_ptr->do_filter & PNG_FILTER_AVG) != 0)
2003  {
2004  png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
2005  png_ptr->rowbytes + 1);
2006 
2007  png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
2008  }
2009 
2010  if ((png_ptr->do_filter & PNG_FILTER_PAETH) != 0)
2011  {
2012  png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
2013  png_ptr->rowbytes + 1);
2014 
2015  png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
2016  }
2017  }
2018 #endif /* WRITE_FILTER */
2019 
2020 #ifdef PNG_WRITE_INTERLACING_SUPPORTED
2021  /* If interlaced, we need to set up width and height of pass */
2022  if (png_ptr->interlaced != 0)
2023  {
2024  if ((png_ptr->transformations & PNG_INTERLACE) == 0)
2025  {
2026  png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
2027  png_pass_ystart[0]) / png_pass_yinc[0];
2028 
2029  png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
2030  png_pass_start[0]) / png_pass_inc[0];
2031  }
2032 
2033  else
2034  {
2035  png_ptr->num_rows = png_ptr->height;
2036  png_ptr->usr_width = png_ptr->width;
2037  }
2038  }
2039 
2040  else
2041 #endif
2042  {
2043  png_ptr->num_rows = png_ptr->height;
2044  png_ptr->usr_width = png_ptr->width;
2045  }
2046 }
#define PNG_FILTER_VALUE_AVG
Definition: png.h:1615
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
#define PNG_FILTER_PAETH
Definition: png.h:1605
#define PNG_FILTER_SUB
Definition: png.h:1602
png_uint_32 width
Definition: pngstruct.h:214
png_byte * png_bytep
Definition: pngconf.h:600
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:1616
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte transformed_pixel_depth
Definition: pngstruct.h:263
png_byte do_filter
Definition: pngstruct.h:251
png_size_t png_alloc_size_t
Definition: pngconf.h:578
png_bytep prev_row
Definition: pngstruct.h:222
png_uint_32 usr_width
Definition: pngstruct.h:217
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_FILTER_VALUE_SUB
Definition: png.h:1613
png_bytep row_buf
Definition: pngstruct.h:225
#define PNG_FILTER_UP
Definition: png.h:1603
#define PNG_FILTER_AVG
Definition: png.h:1604
#define PNG_FILTER_VALUE_UP
Definition: png.h:1614
png_byte pixel_depth
Definition: pngstruct.h:255
#define PNG_INTERLACE
Definition: pngpriv.h:561
PNG_IMPEXP png_voidp() png_calloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:50
png_uint_32 num_rows
Definition: pngstruct.h:216
png_size_t rowbytes
Definition: pngstruct.h:218
png_byte maximum_pixel_depth
Definition: pngstruct.h:261
#define PNG_FILTER_VALUE_NONE
Definition: png.h:1612
png_byte usr_bit_depth
Definition: pngstruct.h:254
png_uint_32 height
Definition: pngstruct.h:215
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
void png_combine_row ( png_const_structrp  png_ptr,
png_bytep  row,
int  display 
)
3003 {
3004  unsigned int pixel_depth = png_ptr->transformed_pixel_depth;
3005  png_const_bytep sp = png_ptr->row_buf + 1;
3006  png_alloc_size_t row_width = png_ptr->width;
3007  unsigned int pass = png_ptr->pass;
3008  png_bytep end_ptr = 0;
3009  png_byte end_byte = 0;
3010  unsigned int end_mask;
3011 
3012  png_debug(1, "in png_combine_row");
3013 
3014  /* Added in 1.5.6: it should not be possible to enter this routine until at
3015  * least one row has been read from the PNG data and transformed.
3016  */
3017  if (pixel_depth == 0)
3018  png_error(png_ptr, "internal row logic error");
3019 
3020  /* Added in 1.5.4: the pixel depth should match the information returned by
3021  * any call to png_read_update_info at this point. Do not continue if we got
3022  * this wrong.
3023  */
3024  if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes !=
3025  PNG_ROWBYTES(pixel_depth, row_width))
3026  png_error(png_ptr, "internal row size calculation error");
3027 
3028  /* Don't expect this to ever happen: */
3029  if (row_width == 0)
3030  png_error(png_ptr, "internal row width error");
3031 
3032  /* Preserve the last byte in cases where only part of it will be overwritten,
3033  * the multiply below may overflow, we don't care because ANSI-C guarantees
3034  * we get the low bits.
3035  */
3036  end_mask = (pixel_depth * row_width) & 7;
3037  if (end_mask != 0)
3038  {
3039  /* end_ptr == NULL is a flag to say do nothing */
3040  end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1;
3041  end_byte = *end_ptr;
3042 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3043  if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3044  /* little-endian byte */
3045  end_mask = 0xff << end_mask;
3046 
3047  else /* big-endian byte */
3048 # endif
3049  end_mask = 0xff >> end_mask;
3050  /* end_mask is now the bits to *keep* from the destination row */
3051  }
3052 
3053  /* For non-interlaced images this reduces to a memcpy(). A memcpy()
3054  * will also happen if interlacing isn't supported or if the application
3055  * does not call png_set_interlace_handling(). In the latter cases the
3056  * caller just gets a sequence of the unexpanded rows from each interlace
3057  * pass.
3058  */
3059 #ifdef PNG_READ_INTERLACING_SUPPORTED
3060  if (png_ptr->interlaced != 0 &&
3061  (png_ptr->transformations & PNG_INTERLACE) != 0 &&
3062  pass < 6 && (display == 0 ||
3063  /* The following copies everything for 'display' on passes 0, 2 and 4. */
3064  (display == 1 && (pass & 1) != 0)))
3065  {
3066  /* Narrow images may have no bits in a pass; the caller should handle
3067  * this, but this test is cheap:
3068  */
3069  if (row_width <= PNG_PASS_START_COL(pass))
3070  return;
3071 
3072  if (pixel_depth < 8)
3073  {
3074  /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit
3075  * into 32 bits, then a single loop over the bytes using the four byte
3076  * values in the 32-bit mask can be used. For the 'display' option the
3077  * expanded mask may also not require any masking within a byte. To
3078  * make this work the PACKSWAP option must be taken into account - it
3079  * simply requires the pixels to be reversed in each byte.
3080  *
3081  * The 'regular' case requires a mask for each of the first 6 passes,
3082  * the 'display' case does a copy for the even passes in the range
3083  * 0..6. This has already been handled in the test above.
3084  *
3085  * The masks are arranged as four bytes with the first byte to use in
3086  * the lowest bits (little-endian) regardless of the order (PACKSWAP or
3087  * not) of the pixels in each byte.
3088  *
3089  * NOTE: the whole of this logic depends on the caller of this function
3090  * only calling it on rows appropriate to the pass. This function only
3091  * understands the 'x' logic; the 'y' logic is handled by the caller.
3092  *
3093  * The following defines allow generation of compile time constant bit
3094  * masks for each pixel depth and each possibility of swapped or not
3095  * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index,
3096  * is in the range 0..7; and the result is 1 if the pixel is to be
3097  * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B'
3098  * for the block method.
3099  *
3100  * With some compilers a compile time expression of the general form:
3101  *
3102  * (shift >= 32) ? (a >> (shift-32)) : (b >> shift)
3103  *
3104  * Produces warnings with values of 'shift' in the range 33 to 63
3105  * because the right hand side of the ?: expression is evaluated by
3106  * the compiler even though it isn't used. Microsoft Visual C (various
3107  * versions) and the Intel C compiler are known to do this. To avoid
3108  * this the following macros are used in 1.5.6. This is a temporary
3109  * solution to avoid destabilizing the code during the release process.
3110  */
3111 # if PNG_USE_COMPILE_TIME_MASKS
3112 # define PNG_LSR(x,s) ((x)>>((s) & 0x1f))
3113 # define PNG_LSL(x,s) ((x)<<((s) & 0x1f))
3114 # else
3115 # define PNG_LSR(x,s) ((x)>>(s))
3116 # define PNG_LSL(x,s) ((x)<<(s))
3117 # endif
3118 # define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\
3119  PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1)
3120 # define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\
3121  PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1)
3122 
3123  /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is
3124  * little endian - the first pixel is at bit 0 - however the extra
3125  * parameter 's' can be set to cause the mask position to be swapped
3126  * within each byte, to match the PNG format. This is done by XOR of
3127  * the shift with 7, 6 or 4 for bit depths 1, 2 and 4.
3128  */
3129 # define PIXEL_MASK(p,x,d,s) \
3130  (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0))))
3131 
3132  /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask.
3133  */
3134 # define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3135 # define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0)
3136 
3137  /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp
3138  * cases the result needs replicating, for the 4-bpp case the above
3139  * generates a full 32 bits.
3140  */
3141 # define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1)))
3142 
3143 # define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\
3144  S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\
3145  S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d)
3146 
3147 # define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\
3148  B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\
3149  B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d)
3150 
3151 #if PNG_USE_COMPILE_TIME_MASKS
3152  /* Utility macros to construct all the masks for a depth/swap
3153  * combination. The 's' parameter says whether the format is PNG
3154  * (big endian bytes) or not. Only the three odd-numbered passes are
3155  * required for the display/block algorithm.
3156  */
3157 # define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\
3158  S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) }
3159 
3160 # define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) }
3161 
3162 # define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2))
3163 
3164  /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and
3165  * then pass:
3166  */
3167  static PNG_CONST png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] =
3168  {
3169  /* Little-endian byte masks for PACKSWAP */
3170  { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) },
3171  /* Normal (big-endian byte) masks - PNG format */
3172  { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) }
3173  };
3174 
3175  /* display_mask has only three entries for the odd passes, so index by
3176  * pass>>1.
3177  */
3178  static PNG_CONST png_uint_32 display_mask[2][3][3] =
3179  {
3180  /* Little-endian byte masks for PACKSWAP */
3181  { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) },
3182  /* Normal (big-endian byte) masks - PNG format */
3183  { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) }
3184  };
3185 
3186 # define MASK(pass,depth,display,png)\
3187  ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\
3188  row_mask[png][DEPTH_INDEX(depth)][pass])
3189 
3190 #else /* !PNG_USE_COMPILE_TIME_MASKS */
3191  /* This is the runtime alternative: it seems unlikely that this will
3192  * ever be either smaller or faster than the compile time approach.
3193  */
3194 # define MASK(pass,depth,display,png)\
3195  ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png))
3196 #endif /* !USE_COMPILE_TIME_MASKS */
3197 
3198  /* Use the appropriate mask to copy the required bits. In some cases
3199  * the byte mask will be 0 or 0xff; optimize these cases. row_width is
3200  * the number of pixels, but the code copies bytes, so it is necessary
3201  * to special case the end.
3202  */
3203  png_uint_32 pixels_per_byte = 8 / pixel_depth;
3204  png_uint_32 mask;
3205 
3206 # ifdef PNG_READ_PACKSWAP_SUPPORTED
3207  if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
3208  mask = MASK(pass, pixel_depth, display, 0);
3209 
3210  else
3211 # endif
3212  mask = MASK(pass, pixel_depth, display, 1);
3213 
3214  for (;;)
3215  {
3216  png_uint_32 m;
3217 
3218  /* It doesn't matter in the following if png_uint_32 has more than
3219  * 32 bits because the high bits always match those in m<<24; it is,
3220  * however, essential to use OR here, not +, because of this.
3221  */
3222  m = mask;
3223  mask = (m >> 8) | (m << 24); /* rotate right to good compilers */
3224  m &= 0xff;
3225 
3226  if (m != 0) /* something to copy */
3227  {
3228  if (m != 0xff)
3229  *dp = (png_byte)((*dp & ~m) | (*sp & m));
3230  else
3231  *dp = *sp;
3232  }
3233 
3234  /* NOTE: this may overwrite the last byte with garbage if the image
3235  * is not an exact number of bytes wide; libpng has always done
3236  * this.
3237  */
3238  if (row_width <= pixels_per_byte)
3239  break; /* May need to restore part of the last byte */
3240 
3241  row_width -= pixels_per_byte;
3242  ++dp;
3243  ++sp;
3244  }
3245  }
3246 
3247  else /* pixel_depth >= 8 */
3248  {
3249  unsigned int bytes_to_copy, bytes_to_jump;
3250 
3251  /* Validate the depth - it must be a multiple of 8 */
3252  if (pixel_depth & 7)
3253  png_error(png_ptr, "invalid user transform pixel depth");
3254 
3255  pixel_depth >>= 3; /* now in bytes */
3256  row_width *= pixel_depth;
3257 
3258  /* Regardless of pass number the Adam 7 interlace always results in a
3259  * fixed number of pixels to copy then to skip. There may be a
3260  * different number of pixels to skip at the start though.
3261  */
3262  {
3263  unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth;
3264 
3265  row_width -= offset;
3266  dp += offset;
3267  sp += offset;
3268  }
3269 
3270  /* Work out the bytes to copy. */
3271  if (display != 0)
3272  {
3273  /* When doing the 'block' algorithm the pixel in the pass gets
3274  * replicated to adjacent pixels. This is why the even (0,2,4,6)
3275  * passes are skipped above - the entire expanded row is copied.
3276  */
3277  bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth;
3278 
3279  /* But don't allow this number to exceed the actual row width. */
3280  if (bytes_to_copy > row_width)
3281  bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3282  }
3283 
3284  else /* normal row; Adam7 only ever gives us one pixel to copy. */
3285  bytes_to_copy = pixel_depth;
3286 
3287  /* In Adam7 there is a constant offset between where the pixels go. */
3288  bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth;
3289 
3290  /* And simply copy these bytes. Some optimization is possible here,
3291  * depending on the value of 'bytes_to_copy'. Special case the low
3292  * byte counts, which we know to be frequent.
3293  *
3294  * Notice that these cases all 'return' rather than 'break' - this
3295  * avoids an unnecessary test on whether to restore the last byte
3296  * below.
3297  */
3298  switch (bytes_to_copy)
3299  {
3300  case 1:
3301  for (;;)
3302  {
3303  *dp = *sp;
3304 
3305  if (row_width <= bytes_to_jump)
3306  return;
3307 
3308  dp += bytes_to_jump;
3309  sp += bytes_to_jump;
3310  row_width -= bytes_to_jump;
3311  }
3312 
3313  case 2:
3314  /* There is a possibility of a partial copy at the end here; this
3315  * slows the code down somewhat.
3316  */
3317  do
3318  {
3319  dp[0] = sp[0], dp[1] = sp[1];
3320 
3321  if (row_width <= bytes_to_jump)
3322  return;
3323 
3324  sp += bytes_to_jump;
3325  dp += bytes_to_jump;
3326  row_width -= bytes_to_jump;
3327  }
3328  while (row_width > 1);
3329 
3330  /* And there can only be one byte left at this point: */
3331  *dp = *sp;
3332  return;
3333 
3334  case 3:
3335  /* This can only be the RGB case, so each copy is exactly one
3336  * pixel and it is not necessary to check for a partial copy.
3337  */
3338  for (;;)
3339  {
3340  dp[0] = sp[0], dp[1] = sp[1], dp[2] = sp[2];
3341 
3342  if (row_width <= bytes_to_jump)
3343  return;
3344 
3345  sp += bytes_to_jump;
3346  dp += bytes_to_jump;
3347  row_width -= bytes_to_jump;
3348  }
3349 
3350  default:
3351 #if PNG_ALIGN_TYPE != PNG_ALIGN_NONE
3352  /* Check for double byte alignment and, if possible, use a
3353  * 16-bit copy. Don't attempt this for narrow images - ones that
3354  * are less than an interlace panel wide. Don't attempt it for
3355  * wide bytes_to_copy either - use the memcpy there.
3356  */
3357  if (bytes_to_copy < 16 /*else use memcpy*/ &&
3358  png_isaligned(dp, png_uint_16) &&
3359  png_isaligned(sp, png_uint_16) &&
3360  bytes_to_copy % (sizeof (png_uint_16)) == 0 &&
3361  bytes_to_jump % (sizeof (png_uint_16)) == 0)
3362  {
3363  /* Everything is aligned for png_uint_16 copies, but try for
3364  * png_uint_32 first.
3365  */
3366  if (png_isaligned(dp, png_uint_32) != 0 &&
3367  png_isaligned(sp, png_uint_32) != 0 &&
3368  bytes_to_copy % (sizeof (png_uint_32)) == 0 &&
3369  bytes_to_jump % (sizeof (png_uint_32)) == 0)
3370  {
3373  png_const_uint_32p, sp);
3374  size_t skip = (bytes_to_jump-bytes_to_copy) /
3375  (sizeof (png_uint_32));
3376 
3377  do
3378  {
3379  size_t c = bytes_to_copy;
3380  do
3381  {
3382  *dp32++ = *sp32++;
3383  c -= (sizeof (png_uint_32));
3384  }
3385  while (c > 0);
3386 
3387  if (row_width <= bytes_to_jump)
3388  return;
3389 
3390  dp32 += skip;
3391  sp32 += skip;
3392  row_width -= bytes_to_jump;
3393  }
3394  while (bytes_to_copy <= row_width);
3395 
3396  /* Get to here when the row_width truncates the final copy.
3397  * There will be 1-3 bytes left to copy, so don't try the
3398  * 16-bit loop below.
3399  */
3400  dp = (png_bytep)dp32;
3401  sp = (png_const_bytep)sp32;
3402  do
3403  *dp++ = *sp++;
3404  while (--row_width > 0);
3405  return;
3406  }
3407 
3408  /* Else do it in 16-bit quantities, but only if the size is
3409  * not too large.
3410  */
3411  else
3412  {
3415  png_const_uint_16p, sp);
3416  size_t skip = (bytes_to_jump-bytes_to_copy) /
3417  (sizeof (png_uint_16));
3418 
3419  do
3420  {
3421  size_t c = bytes_to_copy;
3422  do
3423  {
3424  *dp16++ = *sp16++;
3425  c -= (sizeof (png_uint_16));
3426  }
3427  while (c > 0);
3428 
3429  if (row_width <= bytes_to_jump)
3430  return;
3431 
3432  dp16 += skip;
3433  sp16 += skip;
3434  row_width -= bytes_to_jump;
3435  }
3436  while (bytes_to_copy <= row_width);
3437 
3438  /* End of row - 1 byte left, bytes_to_copy > row_width: */
3439  dp = (png_bytep)dp16;
3440  sp = (png_const_bytep)sp16;
3441  do
3442  *dp++ = *sp++;
3443  while (--row_width > 0);
3444  return;
3445  }
3446  }
3447 #endif /* ALIGN_TYPE code */
3448 
3449  /* The true default - use a memcpy: */
3450  for (;;)
3451  {
3452  memcpy(dp, sp, bytes_to_copy);
3453 
3454  if (row_width <= bytes_to_jump)
3455  return;
3456 
3457  sp += bytes_to_jump;
3458  dp += bytes_to_jump;
3459  row_width -= bytes_to_jump;
3460  if (bytes_to_copy > row_width)
3461  bytes_to_copy = (unsigned int)/*SAFE*/row_width;
3462  }
3463  }
3464 
3465  /* NOT REACHED*/
3466  } /* pixel_depth >= 8 */
3467 
3468  /* Here if pixel_depth < 8 to check 'end_ptr' below. */
3469  }
3470  else
3471 #endif /* READ_INTERLACING */
3472 
3473  /* If here then the switch above wasn't used so just memcpy the whole row
3474  * from the temporary row buffer (notice that this overwrites the end of the
3475  * destination row if it is a partial byte.)
3476  */
3477  memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width));
3478 
3479  /* Restore the overwritten bits from the last byte if necessary. */
3480  if (end_ptr != NULL)
3481  *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask));
3482 }
png_uint_32 width
Definition: pngstruct.h:214
png_byte * png_bytep
Definition: pngconf.h:600
png_size_t info_rowbytes
Definition: pngstruct.h:234
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte transformed_pixel_depth
Definition: pngstruct.h:263
const png_uint_32 * png_const_uint_32p
Definition: pngconf.h:603
png_size_t png_alloc_size_t
Definition: pngconf.h:578
#define png_aligncast(type, value)
Definition: pngpriv.h:410
#define PNG_PASS_START_COL(pass)
Definition: png.h:2596
#define PNG_CONST
Definition: pngconf.h:86
png_bytep row_buf
Definition: pngstruct.h:225
const png_byte * png_const_bytep
Definition: pngconf.h:601
const png_uint_16 * png_const_uint_16p
Definition: pngconf.h:607
#define PNG_PACKSWAP
Definition: pngpriv.h:576
#define MASK(pass, depth, display, png)
png_uint_32 * png_uint_32p
Definition: pngconf.h:602
#define PNG_INTERLACE
Definition: pngpriv.h:561
png_uint_16 * png_uint_16p
Definition: pngconf.h:606
#define S_MASKS(d, s)
#define PNG_PASS_COL_OFFSET(pass)
Definition: png.h:2604
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
#define png_isaligned(ptr, type)
Definition: pngpriv.h:524
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
#define B_MASKS(d, s)
#define png_aligncastconst(type, value)
Definition: pngpriv.h:411
void png_do_read_interlace ( png_row_infop  row_info,
png_bytep  row,
int  pass,
png_uint_32  transformations 
)
3488 {
3489  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
3490  /* Offset to next interlace block */
3491  static PNG_CONST int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
3492 
3493  png_debug(1, "in png_do_read_interlace");
3494  if (row != NULL && row_info != NULL)
3495  {
3496  png_uint_32 final_width;
3497 
3498  final_width = row_info->width * png_pass_inc[pass];
3499 
3500  switch (row_info->pixel_depth)
3501  {
3502  case 1:
3503  {
3504  png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 3);
3505  png_bytep dp = row + (png_size_t)((final_width - 1) >> 3);
3506  int sshift, dshift;
3507  int s_start, s_end, s_inc;
3508  int jstop = png_pass_inc[pass];
3509  png_byte v;
3510  png_uint_32 i;
3511  int j;
3512 
3513 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3514  if ((transformations & PNG_PACKSWAP) != 0)
3515  {
3516  sshift = (int)((row_info->width + 7) & 0x07);
3517  dshift = (int)((final_width + 7) & 0x07);
3518  s_start = 7;
3519  s_end = 0;
3520  s_inc = -1;
3521  }
3522 
3523  else
3524 #endif
3525  {
3526  sshift = 7 - (int)((row_info->width + 7) & 0x07);
3527  dshift = 7 - (int)((final_width + 7) & 0x07);
3528  s_start = 0;
3529  s_end = 7;
3530  s_inc = 1;
3531  }
3532 
3533  for (i = 0; i < row_info->width; i++)
3534  {
3535  v = (png_byte)((*sp >> sshift) & 0x01);
3536  for (j = 0; j < jstop; j++)
3537  {
3538  unsigned int tmp = *dp & (0x7f7f >> (7 - dshift));
3539  tmp |= v << dshift;
3540  *dp = (png_byte)(tmp & 0xff);
3541 
3542  if (dshift == s_end)
3543  {
3544  dshift = s_start;
3545  dp--;
3546  }
3547 
3548  else
3549  dshift += s_inc;
3550  }
3551 
3552  if (sshift == s_end)
3553  {
3554  sshift = s_start;
3555  sp--;
3556  }
3557 
3558  else
3559  sshift += s_inc;
3560  }
3561  break;
3562  }
3563 
3564  case 2:
3565  {
3566  png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2);
3567  png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2);
3568  int sshift, dshift;
3569  int s_start, s_end, s_inc;
3570  int jstop = png_pass_inc[pass];
3571  png_uint_32 i;
3572 
3573 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3574  if ((transformations & PNG_PACKSWAP) != 0)
3575  {
3576  sshift = (int)(((row_info->width + 3) & 0x03) << 1);
3577  dshift = (int)(((final_width + 3) & 0x03) << 1);
3578  s_start = 6;
3579  s_end = 0;
3580  s_inc = -2;
3581  }
3582 
3583  else
3584 #endif
3585  {
3586  sshift = (int)((3 - ((row_info->width + 3) & 0x03)) << 1);
3587  dshift = (int)((3 - ((final_width + 3) & 0x03)) << 1);
3588  s_start = 0;
3589  s_end = 6;
3590  s_inc = 2;
3591  }
3592 
3593  for (i = 0; i < row_info->width; i++)
3594  {
3595  png_byte v;
3596  int j;
3597 
3598  v = (png_byte)((*sp >> sshift) & 0x03);
3599  for (j = 0; j < jstop; j++)
3600  {
3601  unsigned int tmp = *dp & (0x3f3f >> (6 - dshift));
3602  tmp |= v << dshift;
3603  *dp = (png_byte)(tmp & 0xff);
3604 
3605  if (dshift == s_end)
3606  {
3607  dshift = s_start;
3608  dp--;
3609  }
3610 
3611  else
3612  dshift += s_inc;
3613  }
3614 
3615  if (sshift == s_end)
3616  {
3617  sshift = s_start;
3618  sp--;
3619  }
3620 
3621  else
3622  sshift += s_inc;
3623  }
3624  break;
3625  }
3626 
3627  case 4:
3628  {
3629  png_bytep sp = row + (png_size_t)((row_info->width - 1) >> 1);
3630  png_bytep dp = row + (png_size_t)((final_width - 1) >> 1);
3631  int sshift, dshift;
3632  int s_start, s_end, s_inc;
3633  png_uint_32 i;
3634  int jstop = png_pass_inc[pass];
3635 
3636 #ifdef PNG_READ_PACKSWAP_SUPPORTED
3637  if ((transformations & PNG_PACKSWAP) != 0)
3638  {
3639  sshift = (int)(((row_info->width + 1) & 0x01) << 2);
3640  dshift = (int)(((final_width + 1) & 0x01) << 2);
3641  s_start = 4;
3642  s_end = 0;
3643  s_inc = -4;
3644  }
3645 
3646  else
3647 #endif
3648  {
3649  sshift = (int)((1 - ((row_info->width + 1) & 0x01)) << 2);
3650  dshift = (int)((1 - ((final_width + 1) & 0x01)) << 2);
3651  s_start = 0;
3652  s_end = 4;
3653  s_inc = 4;
3654  }
3655 
3656  for (i = 0; i < row_info->width; i++)
3657  {
3658  png_byte v = (png_byte)((*sp >> sshift) & 0x0f);
3659  int j;
3660 
3661  for (j = 0; j < jstop; j++)
3662  {
3663  unsigned int tmp = *dp & (0xf0f >> (4 - dshift));
3664  tmp |= v << dshift;
3665  *dp = (png_byte)(tmp & 0xff);
3666 
3667  if (dshift == s_end)
3668  {
3669  dshift = s_start;
3670  dp--;
3671  }
3672 
3673  else
3674  dshift += s_inc;
3675  }
3676 
3677  if (sshift == s_end)
3678  {
3679  sshift = s_start;
3680  sp--;
3681  }
3682 
3683  else
3684  sshift += s_inc;
3685  }
3686  break;
3687  }
3688 
3689  default:
3690  {
3691  png_size_t pixel_bytes = (row_info->pixel_depth >> 3);
3692 
3693  png_bytep sp = row + (png_size_t)(row_info->width - 1)
3694  * pixel_bytes;
3695 
3696  png_bytep dp = row + (png_size_t)(final_width - 1) * pixel_bytes;
3697 
3698  int jstop = png_pass_inc[pass];
3699  png_uint_32 i;
3700 
3701  for (i = 0; i < row_info->width; i++)
3702  {
3703  png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */
3704  int j;
3705 
3706  memcpy(v, sp, pixel_bytes);
3707 
3708  for (j = 0; j < jstop; j++)
3709  {
3710  memcpy(dp, v, pixel_bytes);
3711  dp -= pixel_bytes;
3712  }
3713 
3714  sp -= pixel_bytes;
3715  }
3716  break;
3717  }
3718  }
3719 
3720  row_info->width = final_width;
3721  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width);
3722  }
3723 #ifndef PNG_READ_PACKSWAP_SUPPORTED
3724  PNG_UNUSED(transformations) /* Silence compiler warning */
3725 #endif
3726 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
png_uint_32 width
Definition: png.h:896
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_PACKSWAP
Definition: pngpriv.h:576
png_byte pixel_depth
Definition: png.h:901
png_size_t rowbytes
Definition: png.h:897
size_t png_size_t
Definition: pngconf.h:543
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
void png_do_write_interlace ( png_row_infop  row_info,
png_bytep  row,
int  pass 
)
2142 {
2143  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
2144 
2145  /* Start of interlace block */
2146  static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
2147 
2148  /* Offset to next interlace block */
2149  static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
2150 
2151  png_debug(1, "in png_do_write_interlace");
2152 
2153  /* We don't have to do anything on the last pass (6) */
2154  if (pass < 6)
2155  {
2156  /* Each pixel depth is handled separately */
2157  switch (row_info->pixel_depth)
2158  {
2159  case 1:
2160  {
2161  png_bytep sp;
2162  png_bytep dp;
2163  int shift;
2164  int d;
2165  int value;
2166  png_uint_32 i;
2167  png_uint_32 row_width = row_info->width;
2168 
2169  dp = row;
2170  d = 0;
2171  shift = 7;
2172 
2173  for (i = png_pass_start[pass]; i < row_width;
2174  i += png_pass_inc[pass])
2175  {
2176  sp = row + (png_size_t)(i >> 3);
2177  value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
2178  d |= (value << shift);
2179 
2180  if (shift == 0)
2181  {
2182  shift = 7;
2183  *dp++ = (png_byte)d;
2184  d = 0;
2185  }
2186 
2187  else
2188  shift--;
2189 
2190  }
2191  if (shift != 7)
2192  *dp = (png_byte)d;
2193 
2194  break;
2195  }
2196 
2197  case 2:
2198  {
2199  png_bytep sp;
2200  png_bytep dp;
2201  int shift;
2202  int d;
2203  int value;
2204  png_uint_32 i;
2205  png_uint_32 row_width = row_info->width;
2206 
2207  dp = row;
2208  shift = 6;
2209  d = 0;
2210 
2211  for (i = png_pass_start[pass]; i < row_width;
2212  i += png_pass_inc[pass])
2213  {
2214  sp = row + (png_size_t)(i >> 2);
2215  value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
2216  d |= (value << shift);
2217 
2218  if (shift == 0)
2219  {
2220  shift = 6;
2221  *dp++ = (png_byte)d;
2222  d = 0;
2223  }
2224 
2225  else
2226  shift -= 2;
2227  }
2228  if (shift != 6)
2229  *dp = (png_byte)d;
2230 
2231  break;
2232  }
2233 
2234  case 4:
2235  {
2236  png_bytep sp;
2237  png_bytep dp;
2238  int shift;
2239  int d;
2240  int value;
2241  png_uint_32 i;
2242  png_uint_32 row_width = row_info->width;
2243 
2244  dp = row;
2245  shift = 4;
2246  d = 0;
2247  for (i = png_pass_start[pass]; i < row_width;
2248  i += png_pass_inc[pass])
2249  {
2250  sp = row + (png_size_t)(i >> 1);
2251  value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
2252  d |= (value << shift);
2253 
2254  if (shift == 0)
2255  {
2256  shift = 4;
2257  *dp++ = (png_byte)d;
2258  d = 0;
2259  }
2260 
2261  else
2262  shift -= 4;
2263  }
2264  if (shift != 4)
2265  *dp = (png_byte)d;
2266 
2267  break;
2268  }
2269 
2270  default:
2271  {
2272  png_bytep sp;
2273  png_bytep dp;
2274  png_uint_32 i;
2275  png_uint_32 row_width = row_info->width;
2276  png_size_t pixel_bytes;
2277 
2278  /* Start at the beginning */
2279  dp = row;
2280 
2281  /* Find out how many bytes each pixel takes up */
2282  pixel_bytes = (row_info->pixel_depth >> 3);
2283 
2284  /* Loop through the row, only looking at the pixels that matter */
2285  for (i = png_pass_start[pass]; i < row_width;
2286  i += png_pass_inc[pass])
2287  {
2288  /* Find out where the original pixel is */
2289  sp = row + (png_size_t)i * pixel_bytes;
2290 
2291  /* Move the pixel */
2292  if (dp != sp)
2293  memcpy(dp, sp, pixel_bytes);
2294 
2295  /* Next pixel */
2296  dp += pixel_bytes;
2297  }
2298  break;
2299  }
2300  }
2301  /* Set new row width */
2302  row_info->width = (row_info->width +
2303  png_pass_inc[pass] - 1 -
2304  png_pass_start[pass]) /
2305  png_pass_inc[pass];
2306 
2307  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
2308  row_info->width);
2309  }
2310 }
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
png_uint_32 width
Definition: png.h:896
#define PNG_CONST
Definition: pngconf.h:86
png_byte pixel_depth
Definition: png.h:901
png_size_t rowbytes
Definition: png.h:897
size_t png_size_t
Definition: pngconf.h:543
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
void png_read_filter_row ( png_structrp  pp,
png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row,
int  filter 
)
3926 {
3927  /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define
3928  * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic
3929  * implementations. See png_init_filter_functions above.
3930  */
3931  if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST)
3932  {
3933  if (pp->read_filter[0] == NULL)
3935 
3936  pp->read_filter[filter-1](row_info, row, prev_row);
3937  }
3938 }
#define PNG_FILTER_VALUE_LAST
Definition: png.h:1617
static void png_init_filter_functions(png_structrp pp)
Definition: pngrutil.c:3887
void(* read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info, png_bytep row, png_const_bytep prev_row)
Definition: pngstruct.h:480
#define PNG_FILTER_VALUE_NONE
Definition: png.h:1612
void png_read_filter_row_up_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_sub3_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_sub4_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_avg3_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_avg4_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_paeth3_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_read_filter_row_paeth4_neon ( png_row_infop  row_info,
png_bytep  row,
png_const_bytep  prev_row 
)
void png_write_find_filter ( png_structrp  png_ptr,
png_row_infop  row_info 
)
2327 {
2328  png_bytep best_row;
2329 #ifdef PNG_WRITE_FILTER_SUPPORTED
2330  png_bytep prev_row, row_buf;
2331  png_uint_32 mins, bpp;
2332  png_byte filter_to_do = png_ptr->do_filter;
2333  png_size_t row_bytes = row_info->rowbytes;
2334 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2335  int num_p_filters = png_ptr->num_prev_filters;
2336 #endif
2337 
2338  png_debug(1, "in png_write_find_filter");
2339 
2340 #ifndef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2341  if (png_ptr->row_number == 0 && filter_to_do == PNG_ALL_FILTERS)
2342  {
2343  /* These will never be selected so we need not test them. */
2344  filter_to_do &= ~(PNG_FILTER_UP | PNG_FILTER_PAETH);
2345  }
2346 #endif
2347 
2348  /* Find out how many bytes offset each pixel is */
2349  bpp = (row_info->pixel_depth + 7) >> 3;
2350 
2351  prev_row = png_ptr->prev_row;
2352 #endif
2353  best_row = png_ptr->row_buf;
2354 #ifdef PNG_WRITE_FILTER_SUPPORTED
2355  row_buf = best_row;
2356  mins = PNG_MAXSUM;
2357 
2358  /* The prediction method we use is to find which method provides the
2359  * smallest value when summing the absolute values of the distances
2360  * from zero, using anything >= 128 as negative numbers. This is known
2361  * as the "minimum sum of absolute differences" heuristic. Other
2362  * heuristics are the "weighted minimum sum of absolute differences"
2363  * (experimental and can in theory improve compression), and the "zlib
2364  * predictive" method (not implemented yet), which does test compressions
2365  * of lines using different filter methods, and then chooses the
2366  * (series of) filter(s) that give minimum compressed data size (VERY
2367  * computationally expensive).
2368  *
2369  * GRR 980525: consider also
2370  *
2371  * (1) minimum sum of absolute differences from running average (i.e.,
2372  * keep running sum of non-absolute differences & count of bytes)
2373  * [track dispersion, too? restart average if dispersion too large?]
2374  *
2375  * (1b) minimum sum of absolute differences from sliding average, probably
2376  * with window size <= deflate window (usually 32K)
2377  *
2378  * (2) minimum sum of squared differences from zero or running average
2379  * (i.e., ~ root-mean-square approach)
2380  */
2381 
2382 
2383  /* We don't need to test the 'no filter' case if this is the only filter
2384  * that has been chosen, as it doesn't actually do anything to the data.
2385  */
2386  if ((filter_to_do & PNG_FILTER_NONE) != 0 && filter_to_do != PNG_FILTER_NONE)
2387  {
2388  png_bytep rp;
2389  png_uint_32 sum = 0;
2390  png_size_t i;
2391  int v;
2392 
2393  for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
2394  {
2395  v = *rp;
2396  sum += (v < 128) ? v : 256 - v;
2397  }
2398 
2399 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2400  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2401  {
2402  png_uint_32 sumhi, sumlo;
2403  int j;
2404  sumlo = sum & PNG_LOMASK;
2405  sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
2406 
2407  /* Reduce the sum if we match any of the previous rows */
2408  for (j = 0; j < num_p_filters; j++)
2409  {
2410  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2411  {
2412  sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2414 
2415  sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2417  }
2418  }
2419 
2420  /* Factor in the cost of this filter (this is here for completeness,
2421  * but it makes no sense to have a "cost" for the NONE filter, as
2422  * it has the minimum possible computational cost - none).
2423  */
2424  sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2426 
2427  sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
2429 
2430  if (sumhi > PNG_HIMASK)
2431  sum = PNG_MAXSUM;
2432 
2433  else
2434  sum = (sumhi << PNG_HISHIFT) + sumlo;
2435  }
2436 #endif
2437  mins = sum;
2438  }
2439 
2440  /* Sub filter */
2441  if (filter_to_do == PNG_FILTER_SUB)
2442  /* It's the only filter so no testing is needed */
2443  {
2444  png_bytep rp, lp, dp;
2445  png_size_t i;
2446 
2447  for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2448  i++, rp++, dp++)
2449  {
2450  *dp = *rp;
2451  }
2452 
2453  for (lp = row_buf + 1; i < row_bytes;
2454  i++, rp++, lp++, dp++)
2455  {
2456  *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2457  }
2458 
2459  best_row = png_ptr->sub_row;
2460  }
2461 
2462  else if ((filter_to_do & PNG_FILTER_SUB) != 0)
2463  {
2464  png_bytep rp, dp, lp;
2465  png_uint_32 sum = 0, lmins = mins;
2466  png_size_t i;
2467  int v;
2468 
2469 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2470  /* We temporarily increase the "minimum sum" by the factor we
2471  * would reduce the sum of this filter, so that we can do the
2472  * early exit comparison without scaling the sum each time.
2473  */
2474  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2475  {
2476  int j;
2477  png_uint_32 lmhi, lmlo;
2478  lmlo = lmins & PNG_LOMASK;
2479  lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2480 
2481  for (j = 0; j < num_p_filters; j++)
2482  {
2483  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2484  {
2485  lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2487 
2488  lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2490  }
2491  }
2492 
2493  lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2495 
2496  lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2498 
2499  if (lmhi > PNG_HIMASK)
2500  lmins = PNG_MAXSUM;
2501 
2502  else
2503  lmins = (lmhi << PNG_HISHIFT) + lmlo;
2504  }
2505 #endif
2506 
2507  for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
2508  i++, rp++, dp++)
2509  {
2510  v = *dp = *rp;
2511 
2512  sum += (v < 128) ? v : 256 - v;
2513  }
2514 
2515  for (lp = row_buf + 1; i < row_bytes;
2516  i++, rp++, lp++, dp++)
2517  {
2518  v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
2519 
2520  sum += (v < 128) ? v : 256 - v;
2521 
2522  if (sum > lmins) /* We are already worse, don't continue. */
2523  break;
2524  }
2525 
2526 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2527  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2528  {
2529  int j;
2530  png_uint_32 sumhi, sumlo;
2531  sumlo = sum & PNG_LOMASK;
2532  sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2533 
2534  for (j = 0; j < num_p_filters; j++)
2535  {
2536  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
2537  {
2538  sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
2540 
2541  sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
2543  }
2544  }
2545 
2546  sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2548 
2549  sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
2551 
2552  if (sumhi > PNG_HIMASK)
2553  sum = PNG_MAXSUM;
2554 
2555  else
2556  sum = (sumhi << PNG_HISHIFT) + sumlo;
2557  }
2558 #endif
2559 
2560  if (sum < mins)
2561  {
2562  mins = sum;
2563  best_row = png_ptr->sub_row;
2564  }
2565  }
2566 
2567  /* Up filter */
2568  if (filter_to_do == PNG_FILTER_UP)
2569  {
2570  png_bytep rp, dp, pp;
2571  png_size_t i;
2572 
2573  for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2574  pp = prev_row + 1; i < row_bytes;
2575  i++, rp++, pp++, dp++)
2576  {
2577  *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
2578  }
2579 
2580  best_row = png_ptr->up_row;
2581  }
2582 
2583  else if ((filter_to_do & PNG_FILTER_UP) != 0)
2584  {
2585  png_bytep rp, dp, pp;
2586  png_uint_32 sum = 0, lmins = mins;
2587  png_size_t i;
2588  int v;
2589 
2590 
2591 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2592  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2593  {
2594  int j;
2595  png_uint_32 lmhi, lmlo;
2596  lmlo = lmins & PNG_LOMASK;
2597  lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2598 
2599  for (j = 0; j < num_p_filters; j++)
2600  {
2601  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2602  {
2603  lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2605 
2606  lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2608  }
2609  }
2610 
2611  lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2613 
2614  lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
2616 
2617  if (lmhi > PNG_HIMASK)
2618  lmins = PNG_MAXSUM;
2619 
2620  else
2621  lmins = (lmhi << PNG_HISHIFT) + lmlo;
2622  }
2623 #endif
2624 
2625  for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
2626  pp = prev_row + 1; i < row_bytes; i++)
2627  {
2628  v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2629 
2630  sum += (v < 128) ? v : 256 - v;
2631 
2632  if (sum > lmins) /* We are already worse, don't continue. */
2633  break;
2634  }
2635 
2636 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2637  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2638  {
2639  int j;
2640  png_uint_32 sumhi, sumlo;
2641  sumlo = sum & PNG_LOMASK;
2642  sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2643 
2644  for (j = 0; j < num_p_filters; j++)
2645  {
2646  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
2647  {
2648  sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2650 
2651  sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2653  }
2654  }
2655 
2656  sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2658 
2659  sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
2661 
2662  if (sumhi > PNG_HIMASK)
2663  sum = PNG_MAXSUM;
2664 
2665  else
2666  sum = (sumhi << PNG_HISHIFT) + sumlo;
2667  }
2668 #endif
2669 
2670  if (sum < mins)
2671  {
2672  mins = sum;
2673  best_row = png_ptr->up_row;
2674  }
2675  }
2676 
2677  /* Avg filter */
2678  if (filter_to_do == PNG_FILTER_AVG)
2679  {
2680  png_bytep rp, dp, pp, lp;
2681  png_uint_32 i;
2682 
2683  for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2684  pp = prev_row + 1; i < bpp; i++)
2685  {
2686  *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2687  }
2688 
2689  for (lp = row_buf + 1; i < row_bytes; i++)
2690  {
2691  *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
2692  & 0xff);
2693  }
2694  best_row = png_ptr->avg_row;
2695  }
2696 
2697  else if ((filter_to_do & PNG_FILTER_AVG) != 0)
2698  {
2699  png_bytep rp, dp, pp, lp;
2700  png_uint_32 sum = 0, lmins = mins;
2701  png_size_t i;
2702  int v;
2703 
2704 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2705  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2706  {
2707  int j;
2708  png_uint_32 lmhi, lmlo;
2709  lmlo = lmins & PNG_LOMASK;
2710  lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2711 
2712  for (j = 0; j < num_p_filters; j++)
2713  {
2714  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
2715  {
2716  lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2718 
2719  lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2721  }
2722  }
2723 
2724  lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2726 
2727  lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
2729 
2730  if (lmhi > PNG_HIMASK)
2731  lmins = PNG_MAXSUM;
2732 
2733  else
2734  lmins = (lmhi << PNG_HISHIFT) + lmlo;
2735  }
2736 #endif
2737 
2738  for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
2739  pp = prev_row + 1; i < bpp; i++)
2740  {
2741  v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
2742 
2743  sum += (v < 128) ? v : 256 - v;
2744  }
2745 
2746  for (lp = row_buf + 1; i < row_bytes; i++)
2747  {
2748  v = *dp++ =
2749  (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
2750 
2751  sum += (v < 128) ? v : 256 - v;
2752 
2753  if (sum > lmins) /* We are already worse, don't continue. */
2754  break;
2755  }
2756 
2757 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2758  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2759  {
2760  int j;
2761  png_uint_32 sumhi, sumlo;
2762  sumlo = sum & PNG_LOMASK;
2763  sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2764 
2765  for (j = 0; j < num_p_filters; j++)
2766  {
2767  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
2768  {
2769  sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2771 
2772  sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2774  }
2775  }
2776 
2777  sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2779 
2780  sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
2782 
2783  if (sumhi > PNG_HIMASK)
2784  sum = PNG_MAXSUM;
2785 
2786  else
2787  sum = (sumhi << PNG_HISHIFT) + sumlo;
2788  }
2789 #endif
2790 
2791  if (sum < mins)
2792  {
2793  mins = sum;
2794  best_row = png_ptr->avg_row;
2795  }
2796  }
2797 
2798  /* Paeth filter */
2799  if ((filter_to_do == PNG_FILTER_PAETH) != 0)
2800  {
2801  png_bytep rp, dp, pp, cp, lp;
2802  png_size_t i;
2803 
2804  for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2805  pp = prev_row + 1; i < bpp; i++)
2806  {
2807  *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2808  }
2809 
2810  for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2811  {
2812  int a, b, c, pa, pb, pc, p;
2813 
2814  b = *pp++;
2815  c = *cp++;
2816  a = *lp++;
2817 
2818  p = b - c;
2819  pc = a - c;
2820 
2821 #ifdef PNG_USE_ABS
2822  pa = abs(p);
2823  pb = abs(pc);
2824  pc = abs(p + pc);
2825 #else
2826  pa = p < 0 ? -p : p;
2827  pb = pc < 0 ? -pc : pc;
2828  pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2829 #endif
2830 
2831  p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2832 
2833  *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2834  }
2835  best_row = png_ptr->paeth_row;
2836  }
2837 
2838  else if ((filter_to_do & PNG_FILTER_PAETH) != 0)
2839  {
2840  png_bytep rp, dp, pp, cp, lp;
2841  png_uint_32 sum = 0, lmins = mins;
2842  png_size_t i;
2843  int v;
2844 
2845 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2846  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2847  {
2848  int j;
2849  png_uint_32 lmhi, lmlo;
2850  lmlo = lmins & PNG_LOMASK;
2851  lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
2852 
2853  for (j = 0; j < num_p_filters; j++)
2854  {
2855  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2856  {
2857  lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
2859 
2860  lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
2862  }
2863  }
2864 
2865  lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2867 
2868  lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2870 
2871  if (lmhi > PNG_HIMASK)
2872  lmins = PNG_MAXSUM;
2873 
2874  else
2875  lmins = (lmhi << PNG_HISHIFT) + lmlo;
2876  }
2877 #endif
2878 
2879  for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
2880  pp = prev_row + 1; i < bpp; i++)
2881  {
2882  v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
2883 
2884  sum += (v < 128) ? v : 256 - v;
2885  }
2886 
2887  for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
2888  {
2889  int a, b, c, pa, pb, pc, p;
2890 
2891  b = *pp++;
2892  c = *cp++;
2893  a = *lp++;
2894 
2895 #ifndef PNG_SLOW_PAETH
2896  p = b - c;
2897  pc = a - c;
2898 #ifdef PNG_USE_ABS
2899  pa = abs(p);
2900  pb = abs(pc);
2901  pc = abs(p + pc);
2902 #else
2903  pa = p < 0 ? -p : p;
2904  pb = pc < 0 ? -pc : pc;
2905  pc = (p + pc) < 0 ? -(p + pc) : p + pc;
2906 #endif
2907  p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
2908 #else /* SLOW_PAETH */
2909  p = a + b - c;
2910  pa = abs(p - a);
2911  pb = abs(p - b);
2912  pc = abs(p - c);
2913 
2914  if (pa <= pb && pa <= pc)
2915  p = a;
2916 
2917  else if (pb <= pc)
2918  p = b;
2919 
2920  else
2921  p = c;
2922 #endif /* SLOW_PAETH */
2923 
2924  v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
2925 
2926  sum += (v < 128) ? v : 256 - v;
2927 
2928  if (sum > lmins) /* We are already worse, don't continue. */
2929  break;
2930  }
2931 
2932 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2933  if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
2934  {
2935  int j;
2936  png_uint_32 sumhi, sumlo;
2937  sumlo = sum & PNG_LOMASK;
2938  sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
2939 
2940  for (j = 0; j < num_p_filters; j++)
2941  {
2942  if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
2943  {
2944  sumlo = (sumlo * png_ptr->filter_weights[j]) >>
2946 
2947  sumhi = (sumhi * png_ptr->filter_weights[j]) >>
2949  }
2950  }
2951 
2952  sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2954 
2955  sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
2957 
2958  if (sumhi > PNG_HIMASK)
2959  sum = PNG_MAXSUM;
2960 
2961  else
2962  sum = (sumhi << PNG_HISHIFT) + sumlo;
2963  }
2964 #endif
2965 
2966  if (sum < mins)
2967  {
2968  best_row = png_ptr->paeth_row;
2969  }
2970  }
2971 #endif /* WRITE_FILTER */
2972 
2973  /* Do the actual writing of the filtered row data from the chosen filter. */
2974  png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1);
2975 
2976 #ifdef PNG_WRITE_FILTER_SUPPORTED
2977 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
2978  /* Save the type of filter we picked this time for future calculations */
2979  if (png_ptr->num_prev_filters > 0)
2980  {
2981  int j;
2982 
2983  for (j = 1; j < num_p_filters; j++)
2984  {
2985  png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
2986  }
2987 
2988  png_ptr->prev_filters[j] = best_row[0];
2989  }
2990 #endif
2991 #endif /* WRITE_FILTER */
2992 }
#define PNG_FILTER_VALUE_AVG
Definition: png.h:1615
#define PNG_WEIGHT_SHIFT
Definition: pnglibconf.h:201
#define PNG_FILTER_PAETH
Definition: png.h:1605
#define PNG_FILTER_HEURISTIC_WEIGHTED
Definition: png.h:1662
#define PNG_FILTER_SUB
Definition: png.h:1602
png_uint_32 row_number
Definition: pngstruct.h:220
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
#define PNG_FILTER_VALUE_PAETH
Definition: png.h:1616
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HIMASK
Definition: pngwutil.c:2324
static void png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, png_size_t row_bytes)
Definition: pngwutil.c:2997
long b
Definition: jpegint.h:371
png_byte do_filter
Definition: pngstruct.h:251
static double * p
Definition: gauss_jackson_test.cpp:42
#define PNG_LOMASK
Definition: pngwutil.c:2323
png_bytep prev_row
Definition: pngstruct.h:222
#define PNG_FILTER_VALUE_SUB
Definition: png.h:1613
png_bytep row_buf
Definition: pngstruct.h:225
#define PNG_ALL_FILTERS
Definition: png.h:1606
png_byte pixel_depth
Definition: png.h:901
#define PNG_FILTER_UP
Definition: png.h:1603
#define PNG_FILTER_AVG
Definition: png.h:1604
Definition: eci2kep_test.cpp:33
#define PNG_FILTER_VALUE_UP
Definition: png.h:1614
png_size_t rowbytes
Definition: png.h:897
#define PNG_HISHIFT
Definition: pngwutil.c:2322
#define PNG_FILTER_VALUE_NONE
Definition: png.h:1612
size_t png_size_t
Definition: pngconf.h:543
#define PNG_FILTER_NONE
Definition: png.h:1601
#define PNG_MAXSUM
Definition: pngwutil.c:2321
#define PNG_COST_SHIFT
Definition: pnglibconf.h:190
void png_read_IDAT_data ( png_structrp  png_ptr,
png_bytep  output,
png_alloc_size_t  avail_out 
)
3944 {
3945  /* Loop reading IDATs and decompressing the result into output[avail_out] */
3946  png_ptr->zstream.next_out = output;
3947  png_ptr->zstream.avail_out = 0; /* safety: set below */
3948 
3949  if (output == NULL)
3950  avail_out = 0;
3951 
3952  do
3953  {
3954  int ret;
3955  png_byte tmpbuf[PNG_INFLATE_BUF_SIZE];
3956 
3957  if (png_ptr->zstream.avail_in == 0)
3958  {
3959  uInt avail_in;
3960  png_bytep buffer;
3961 
3962  while (png_ptr->idat_size == 0)
3963  {
3964  png_crc_finish(png_ptr, 0);
3965 
3966  png_ptr->idat_size = png_read_chunk_header(png_ptr);
3967  /* This is an error even in the 'check' case because the code just
3968  * consumed a non-IDAT header.
3969  */
3970  if (png_ptr->chunk_name != png_IDAT)
3971  png_error(png_ptr, "Not enough image data");
3972  }
3973 
3974  avail_in = png_ptr->IDAT_read_size;
3975 
3976  if (avail_in > png_ptr->idat_size)
3977  avail_in = (uInt)png_ptr->idat_size;
3978 
3979  /* A PNG with a gradually increasing IDAT size will defeat this attempt
3980  * to minimize memory usage by causing lots of re-allocs, but
3981  * realistically doing IDAT_read_size re-allocs is not likely to be a
3982  * big problem.
3983  */
3984  buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/);
3985 
3986  png_crc_read(png_ptr, buffer, avail_in);
3987  png_ptr->idat_size -= avail_in;
3988 
3989  png_ptr->zstream.next_in = buffer;
3990  png_ptr->zstream.avail_in = avail_in;
3991  }
3992 
3993  /* And set up the output side. */
3994  if (output != NULL) /* standard read */
3995  {
3996  uInt out = ZLIB_IO_MAX;
3997 
3998  if (out > avail_out)
3999  out = (uInt)avail_out;
4000 
4001  avail_out -= out;
4002  png_ptr->zstream.avail_out = out;
4003  }
4004 
4005  else /* after last row, checking for end */
4006  {
4007  png_ptr->zstream.next_out = tmpbuf;
4008  png_ptr->zstream.avail_out = (sizeof tmpbuf);
4009  }
4010 
4011  /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the
4012  * process. If the LZ stream is truncated the sequential reader will
4013  * terminally damage the stream, above, by reading the chunk header of the
4014  * following chunk (it then exits with png_error).
4015  *
4016  * TODO: deal more elegantly with truncated IDAT lists.
4017  */
4018  ret = inflate(&png_ptr->zstream, Z_NO_FLUSH);
4019 
4020  /* Take the unconsumed output back. */
4021  if (output != NULL)
4022  avail_out += png_ptr->zstream.avail_out;
4023 
4024  else /* avail_out counts the extra bytes */
4025  avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out;
4026 
4027  png_ptr->zstream.avail_out = 0;
4028 
4029  if (ret == Z_STREAM_END)
4030  {
4031  /* Do this for safety; we won't read any more into this row. */
4032  png_ptr->zstream.next_out = NULL;
4033 
4034  png_ptr->mode |= PNG_AFTER_IDAT;
4035  png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4036 
4037  if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0)
4038  png_chunk_benign_error(png_ptr, "Extra compressed data");
4039  break;
4040  }
4041 
4042  if (ret != Z_OK)
4043  {
4044  png_zstream_error(png_ptr, ret);
4045 
4046  if (output != NULL)
4047  png_chunk_error(png_ptr, png_ptr->zstream.msg);
4048 
4049  else /* checking */
4050  {
4051  png_chunk_benign_error(png_ptr, png_ptr->zstream.msg);
4052  return;
4053  }
4054  }
4055  } while (avail_out > 0);
4056 
4057  if (avail_out > 0)
4058  {
4059  /* The stream ended before the image; this is the same as too few IDATs so
4060  * should be handled the same way.
4061  */
4062  if (output != NULL)
4063  png_error(png_ptr, "Not enough image data");
4064 
4065  else /* the deflate stream contained extra data */
4066  png_chunk_benign_error(png_ptr, "Too much image data");
4067  }
4068 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
z_stream zstream
Definition: pngstruct.h:184
Bytef * next_in
Definition: zlib.h:86
#define Z_NO_FLUSH
Definition: zlib.h:164
uInt avail_in
Definition: zlib.h:87
png_byte * png_bytep
Definition: pngconf.h:600
char * msg
Definition: zlib.h:94
string output
Definition: agent-2-0.cpp:56
png_uint_32 chunk_name
Definition: pngstruct.h:221
#define PNG_INFLATE_BUF_SIZE
Definition: pnglibconf.h:194
static char buffer[255]
Definition: propagator_simple.cpp:60
#define ZLIB_IO_MAX
Definition: pngstruct.h:57
png_uint_32 flags
Definition: pngstruct.h:180
#define Z_STREAM_END
Definition: zlib.h:174
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
png_uint_32 png_read_chunk_header(png_structrp png_ptr)
Definition: pngrutil.c:150
Bytef * next_out
Definition: zlib.h:90
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define png_IDAT
Definition: pngpriv.h:752
#define PNG_AFTER_IDAT
Definition: png.h:787
#define PNG_FLAG_ZSTREAM_ENDED
Definition: pngpriv.h:604
png_uint_32 mode
Definition: pngstruct.h:179
png_uint_32 idat_size
Definition: pngstruct.h:236
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
uInt avail_out
Definition: zlib.h:91
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:605
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
void png_zstream_error(png_structrp png_ptr, int ret)
Definition: png.c:958
unsigned int uInt
Definition: zconf.h:370
void png_read_finish_IDAT ( png_structrp  png_ptr)
4072 {
4073  /* We don't need any more data and the stream should have ended, however the
4074  * LZ end code may actually not have been processed. In this case we must
4075  * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk
4076  * may still remain to be consumed.
4077  */
4078  if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4079  {
4080  /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in
4081  * the compressed stream, but the stream may be damaged too, so even after
4082  * this call we may need to terminate the zstream ownership.
4083  */
4084  png_read_IDAT_data(png_ptr, NULL, 0);
4085  png_ptr->zstream.next_out = NULL; /* safety */
4086 
4087  /* Now clear everything out for safety; the following may not have been
4088  * done.
4089  */
4090  if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
4091  {
4092  png_ptr->mode |= PNG_AFTER_IDAT;
4093  png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
4094  }
4095  }
4096 
4097  /* If the zstream has not been released do it now *and* terminate the reading
4098  * of the final IDAT chunk.
4099  */
4100  if (png_ptr->zowner == png_IDAT)
4101  {
4102  /* Always do this; the pointers otherwise point into the read buffer. */
4103  png_ptr->zstream.next_in = NULL;
4104  png_ptr->zstream.avail_in = 0;
4105 
4106  /* Now we no longer own the zstream. */
4107  png_ptr->zowner = 0;
4108 
4109  /* The slightly weird semantics of the sequential IDAT reading is that we
4110  * are always in or at the end of an IDAT chunk, so we always need to do a
4111  * crc_finish here. If idat_size is non-zero we also need to read the
4112  * spurious bytes at the end of the chunk now.
4113  */
4114  (void)png_crc_finish(png_ptr, png_ptr->idat_size);
4115  }
4116 }
z_stream zstream
Definition: pngstruct.h:184
Bytef * next_in
Definition: zlib.h:86
uInt avail_in
Definition: zlib.h:87
void png_read_IDAT_data(png_structrp png_ptr, png_bytep output, png_alloc_size_t avail_out)
Definition: pngrutil.c:3942
png_uint_32 flags
Definition: pngstruct.h:180
Bytef * next_out
Definition: zlib.h:90
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define png_IDAT
Definition: pngpriv.h:752
#define PNG_AFTER_IDAT
Definition: png.h:787
#define PNG_FLAG_ZSTREAM_ENDED
Definition: pngpriv.h:604
png_uint_32 mode
Definition: pngstruct.h:179
png_uint_32 idat_size
Definition: pngstruct.h:236
png_uint_32 zowner
Definition: pngstruct.h:183
void png_read_finish_row ( png_structrp  png_ptr)
4120 {
4121  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4122 
4123  /* Start of interlace block */
4124  static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4125 
4126  /* Offset to next interlace block */
4127  static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4128 
4129  /* Start of interlace block in the y direction */
4130  static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4131 
4132  /* Offset to next interlace block in the y direction */
4133  static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4134 
4135  png_debug(1, "in png_read_finish_row");
4136  png_ptr->row_number++;
4137  if (png_ptr->row_number < png_ptr->num_rows)
4138  return;
4139 
4140  if (png_ptr->interlaced != 0)
4141  {
4142  png_ptr->row_number = 0;
4143 
4144  /* TO DO: don't do this if prev_row isn't needed (requires
4145  * read-ahead of the next row's filter byte.
4146  */
4147  memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4148 
4149  do
4150  {
4151  png_ptr->pass++;
4152 
4153  if (png_ptr->pass >= 7)
4154  break;
4155 
4156  png_ptr->iwidth = (png_ptr->width +
4157  png_pass_inc[png_ptr->pass] - 1 -
4158  png_pass_start[png_ptr->pass]) /
4159  png_pass_inc[png_ptr->pass];
4160 
4161  if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4162  {
4163  png_ptr->num_rows = (png_ptr->height +
4164  png_pass_yinc[png_ptr->pass] - 1 -
4165  png_pass_ystart[png_ptr->pass]) /
4166  png_pass_yinc[png_ptr->pass];
4167  }
4168 
4169  else /* if (png_ptr->transformations & PNG_INTERLACE) */
4170  break; /* libpng deinterlacing sees every row */
4171 
4172  } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0);
4173 
4174  if (png_ptr->pass < 7)
4175  return;
4176  }
4177 
4178  /* Here after at the end of the last row of the last pass. */
4179  png_read_finish_IDAT(png_ptr);
4180 }
png_uint_32 iwidth
Definition: pngstruct.h:219
png_uint_32 row_number
Definition: pngstruct.h:220
png_uint_32 width
Definition: pngstruct.h:214
#define png_debug(l, m)
Definition: pngdebug.h:146
png_bytep prev_row
Definition: pngstruct.h:222
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_INTERLACE
Definition: pngpriv.h:561
png_uint_32 num_rows
Definition: pngstruct.h:216
png_size_t rowbytes
Definition: pngstruct.h:218
void png_read_finish_IDAT(png_structrp png_ptr)
Definition: pngrutil.c:4071
png_uint_32 height
Definition: pngstruct.h:215
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
void png_read_start_row ( png_structrp  png_ptr)
4185 {
4186  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
4187 
4188  /* Start of interlace block */
4189  static PNG_CONST png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
4190 
4191  /* Offset to next interlace block */
4192  static PNG_CONST png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
4193 
4194  /* Start of interlace block in the y direction */
4195  static PNG_CONST png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
4196 
4197  /* Offset to next interlace block in the y direction */
4198  static PNG_CONST png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
4199 
4200  int max_pixel_depth;
4201  png_size_t row_bytes;
4202 
4203  png_debug(1, "in png_read_start_row");
4204 
4205 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
4207 #endif
4208  if (png_ptr->interlaced != 0)
4209  {
4210  if ((png_ptr->transformations & PNG_INTERLACE) == 0)
4211  png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
4212  png_pass_ystart[0]) / png_pass_yinc[0];
4213 
4214  else
4215  png_ptr->num_rows = png_ptr->height;
4216 
4217  png_ptr->iwidth = (png_ptr->width +
4218  png_pass_inc[png_ptr->pass] - 1 -
4219  png_pass_start[png_ptr->pass]) /
4220  png_pass_inc[png_ptr->pass];
4221  }
4222 
4223  else
4224  {
4225  png_ptr->num_rows = png_ptr->height;
4226  png_ptr->iwidth = png_ptr->width;
4227  }
4228 
4229  max_pixel_depth = png_ptr->pixel_depth;
4230 
4231  /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of
4232  * calculations to calculate the final pixel depth, then
4233  * png_do_read_transforms actually does the transforms. This means that the
4234  * code which effectively calculates this value is actually repeated in three
4235  * separate places. They must all match. Innocent changes to the order of
4236  * transformations can and will break libpng in a way that causes memory
4237  * overwrites.
4238  *
4239  * TODO: fix this.
4240  */
4241 #ifdef PNG_READ_PACK_SUPPORTED
4242  if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8)
4243  max_pixel_depth = 8;
4244 #endif
4245 
4246 #ifdef PNG_READ_EXPAND_SUPPORTED
4247  if ((png_ptr->transformations & PNG_EXPAND) != 0)
4248  {
4249  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4250  {
4251  if (png_ptr->num_trans != 0)
4252  max_pixel_depth = 32;
4253 
4254  else
4255  max_pixel_depth = 24;
4256  }
4257 
4258  else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4259  {
4260  if (max_pixel_depth < 8)
4261  max_pixel_depth = 8;
4262 
4263  if (png_ptr->num_trans != 0)
4264  max_pixel_depth *= 2;
4265  }
4266 
4267  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
4268  {
4269  if (png_ptr->num_trans != 0)
4270  {
4271  max_pixel_depth *= 4;
4272  max_pixel_depth /= 3;
4273  }
4274  }
4275  }
4276 #endif
4277 
4278 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4279  if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4280  {
4281 # ifdef PNG_READ_EXPAND_SUPPORTED
4282  /* In fact it is an error if it isn't supported, but checking is
4283  * the safe way.
4284  */
4285  if ((png_ptr->transformations & PNG_EXPAND) != 0)
4286  {
4287  if (png_ptr->bit_depth < 16)
4288  max_pixel_depth *= 2;
4289  }
4290  else
4291 # endif
4292  png_ptr->transformations &= ~PNG_EXPAND_16;
4293  }
4294 #endif
4295 
4296 #ifdef PNG_READ_FILLER_SUPPORTED
4297  if ((png_ptr->transformations & (PNG_FILLER)) != 0)
4298  {
4299  if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
4300  {
4301  if (max_pixel_depth <= 8)
4302  max_pixel_depth = 16;
4303 
4304  else
4305  max_pixel_depth = 32;
4306  }
4307 
4308  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB ||
4309  png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
4310  {
4311  if (max_pixel_depth <= 32)
4312  max_pixel_depth = 32;
4313 
4314  else
4315  max_pixel_depth = 64;
4316  }
4317  }
4318 #endif
4319 
4320 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4321  if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
4322  {
4323  if (
4325  (png_ptr->num_trans != 0 &&
4326  (png_ptr->transformations & PNG_EXPAND) != 0) ||
4327 #endif
4329  (png_ptr->transformations & (PNG_FILLER)) != 0 ||
4330 #endif
4332  {
4333  if (max_pixel_depth <= 16)
4334  max_pixel_depth = 32;
4335 
4336  else
4337  max_pixel_depth = 64;
4338  }
4339 
4340  else
4341  {
4342  if (max_pixel_depth <= 8)
4343  {
4344  if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4345  max_pixel_depth = 32;
4346 
4347  else
4348  max_pixel_depth = 24;
4349  }
4350 
4351  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
4352  max_pixel_depth = 64;
4353 
4354  else
4355  max_pixel_depth = 48;
4356  }
4357  }
4358 #endif
4359 
4360 #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \
4361 defined(PNG_USER_TRANSFORM_PTR_SUPPORTED)
4362  if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4363  {
4364  int user_pixel_depth = png_ptr->user_transform_depth *
4365  png_ptr->user_transform_channels;
4366 
4367  if (user_pixel_depth > max_pixel_depth)
4368  max_pixel_depth = user_pixel_depth;
4369  }
4370 #endif
4371 
4372  /* This value is stored in png_struct and double checked in the row read
4373  * code.
4374  */
4375  png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth;
4376  png_ptr->transformed_pixel_depth = 0; /* calculated on demand */
4377 
4378  /* Align the width on the next larger 8 pixels. Mainly used
4379  * for interlacing
4380  */
4381  row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7));
4382  /* Calculate the maximum bytes needed, adding a byte and a pixel
4383  * for safety's sake
4384  */
4385  row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) +
4386  1 + ((max_pixel_depth + 7) >> 3);
4387 
4388 #ifdef PNG_MAX_MALLOC_64K
4389  if (row_bytes > (png_uint_32)65536L)
4390  png_error(png_ptr, "This image requires a row greater than 64KB");
4391 #endif
4392 
4393  if (row_bytes + 48 > png_ptr->old_big_row_buf_size)
4394  {
4395  png_free(png_ptr, png_ptr->big_row_buf);
4396  png_free(png_ptr, png_ptr->big_prev_row);
4397 
4398  if (png_ptr->interlaced != 0)
4399  png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr,
4400  row_bytes + 48);
4401 
4402  else
4403  png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4404 
4405  png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48);
4406 
4407 #ifdef PNG_ALIGNED_MEMORY_SUPPORTED
4408  /* Use 16-byte aligned memory for row_buf with at least 16 bytes
4409  * of padding before and after row_buf; treat prev_row similarly.
4410  * NOTE: the alignment is to the start of the pixels, one beyond the start
4411  * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this
4412  * was incorrect; the filter byte was aligned, which had the exact
4413  * opposite effect of that intended.
4414  */
4415  {
4416  png_bytep temp = png_ptr->big_row_buf + 32;
4417  int extra = (int)((temp - (png_bytep)0) & 0x0f);
4418  png_ptr->row_buf = temp - extra - 1/*filter byte*/;
4419 
4420  temp = png_ptr->big_prev_row + 32;
4421  extra = (int)((temp - (png_bytep)0) & 0x0f);
4422  png_ptr->prev_row = temp - extra - 1/*filter byte*/;
4423  }
4424 
4425 #else
4426  /* Use 31 bytes of padding before and 17 bytes after row_buf. */
4427  png_ptr->row_buf = png_ptr->big_row_buf + 31;
4428  png_ptr->prev_row = png_ptr->big_prev_row + 31;
4429 #endif
4430  png_ptr->old_big_row_buf_size = row_bytes + 48;
4431  }
4432 
4433 #ifdef PNG_MAX_MALLOC_64K
4434  if (png_ptr->rowbytes > 65535)
4435  png_error(png_ptr, "This image requires a row greater than 64KB");
4436 
4437 #endif
4438  if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1))
4439  png_error(png_ptr, "Row has too many bytes to allocate in memory");
4440 
4441  memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
4442 
4443  png_debug1(3, "width = %u,", png_ptr->width);
4444  png_debug1(3, "height = %u,", png_ptr->height);
4445  png_debug1(3, "iwidth = %u,", png_ptr->iwidth);
4446  png_debug1(3, "num_rows = %u,", png_ptr->num_rows);
4447  png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes);
4448  png_debug1(3, "irowbytes = %lu",
4449  (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1);
4450 
4451  /* The sequential reader needs a buffer for IDAT, but the progressive reader
4452  * does not, so free the read buffer now regardless; the sequential reader
4453  * reallocates it on demand.
4454  */
4455  if (png_ptr->read_buffer != 0)
4456  {
4457  png_bytep buffer = png_ptr->read_buffer;
4458 
4459  png_ptr->read_buffer_size = 0;
4460  png_ptr->read_buffer = NULL;
4461  png_free(png_ptr, buffer);
4462  }
4463 
4464  /* Finally claim the zstream for the inflate of the IDAT data, use the bits
4465  * value from the stream (note that this will result in a fatal error if the
4466  * IDAT stream has a bogus deflate header window_bits value, but this should
4467  * not be happening any longer!)
4468  */
4469  if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK)
4470  png_error(png_ptr, png_ptr->zstream.msg);
4471 
4472  png_ptr->flags |= PNG_FLAG_ROW_INIT;
4473 }
#define PNG_GRAY_TO_RGB
Definition: pngpriv.h:574
png_size_t old_big_row_buf_size
Definition: pngstruct.h:460
PNG_IMPEXP png_voidp() png_malloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:169
z_stream zstream
Definition: pngstruct.h:184
png_uint_32 iwidth
Definition: pngstruct.h:219
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
png_uint_32 width
Definition: pngstruct.h:214
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
png_byte transformed_pixel_depth
Definition: pngstruct.h:263
png_byte color_type
Definition: pngstruct.h:252
#define PNG_FLAG_ROW_INIT
Definition: pngpriv.h:607
#define PNG_FILLER
Definition: pngpriv.h:575
png_bytep prev_row
Definition: pngstruct.h:222
png_bytep big_row_buf
Definition: pngstruct.h:422
static char buffer[255]
Definition: propagator_simple.cpp:60
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_SIZE_MAX
Definition: png.h:792
#define PNG_READ_EXPAND_SUPPORTED
Definition: pnglibconf.h:57
png_uint_32 flags
Definition: pngstruct.h:180
png_bytep row_buf
Definition: pngstruct.h:225
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
png_bytep big_prev_row
Definition: pngstruct.h:477
#define png_IDAT
Definition: pngpriv.h:752
#define PNG_READ_FILLER_SUPPORTED
Definition: pnglibconf.h:58
png_byte pixel_depth
Definition: pngstruct.h:255
#define PNG_INTERLACE
Definition: pngpriv.h:561
PNG_IMPEXP png_voidp() png_calloc(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:50
png_byte bit_depth
Definition: pngstruct.h:253
png_uint_32 num_rows
Definition: pngstruct.h:216
png_size_t rowbytes
Definition: pngstruct.h:218
png_byte maximum_pixel_depth
Definition: pngstruct.h:261
png_uint_16 num_trans
Definition: pngstruct.h:246
static int png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
Definition: pngrutil.c:332
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
size_t png_size_t
Definition: pngconf.h:543
#define PNG_USER_TRANSFORM
Definition: pngpriv.h:580
png_uint_32 height
Definition: pngstruct.h:215
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
#define PNG_PACK
Definition: pngpriv.h:562
#define PNG_EXPAND_16
Definition: pngpriv.h:569
void png_init_read_transformations(png_structrp png_ptr)
Definition: pngrtran.c:1275
#define PNG_EXPAND
Definition: pngpriv.h:572
void png_read_transform_info ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
1925 {
1926  png_debug(1, "in png_read_transform_info");
1927 
1928 #ifdef PNG_READ_EXPAND_SUPPORTED
1929  if ((png_ptr->transformations & PNG_EXPAND) != 0)
1930  {
1931  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1932  {
1933  /* This check must match what actually happens in
1934  * png_do_expand_palette; if it ever checks the tRNS chunk to see if
1935  * it is all opaque we must do the same (at present it does not.)
1936  */
1937  if (png_ptr->num_trans > 0)
1939 
1940  else
1941  info_ptr->color_type = PNG_COLOR_TYPE_RGB;
1942 
1943  info_ptr->bit_depth = 8;
1944  info_ptr->num_trans = 0;
1945 
1946  if (png_ptr->palette == NULL)
1947  png_error (png_ptr, "Palette is NULL in indexed image");
1948  }
1949  else
1950  {
1951  if (png_ptr->num_trans != 0)
1952  {
1953  if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
1954  info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
1955  }
1956  if (info_ptr->bit_depth < 8)
1957  info_ptr->bit_depth = 8;
1958 
1959  info_ptr->num_trans = 0;
1960  }
1961  }
1962 #endif
1963 
1964 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
1965  defined(PNG_READ_ALPHA_MODE_SUPPORTED)
1966  /* The following is almost certainly wrong unless the background value is in
1967  * the screen space!
1968  */
1969  if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1970  info_ptr->background = png_ptr->background;
1971 #endif
1972 
1973 #ifdef PNG_READ_GAMMA_SUPPORTED
1974  /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
1975  * however it seems that the code in png_init_read_transformations, which has
1976  * been called before this from png_read_update_info->png_read_start_row
1977  * sometimes does the gamma transform and cancels the flag.
1978  *
1979  * TODO: this looks wrong; the info_ptr should end up with a gamma equal to
1980  * the screen_gamma value. The following probably results in weirdness if
1981  * the info_ptr is used by the app after the rows have been read.
1982  */
1983  info_ptr->colorspace.gamma = png_ptr->colorspace.gamma;
1984 #endif
1985 
1986  if (info_ptr->bit_depth == 16)
1987  {
1988 # ifdef PNG_READ_16BIT_SUPPORTED
1989 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
1990  if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
1991  info_ptr->bit_depth = 8;
1992 # endif
1993 
1994 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
1995  if ((png_ptr->transformations & PNG_16_TO_8) != 0)
1996  info_ptr->bit_depth = 8;
1997 # endif
1998 
1999 # else
2000  /* No 16 bit support: force chopping 16-bit input down to 8, in this case
2001  * the app program can chose if both APIs are available by setting the
2002  * correct scaling to use.
2003  */
2004 # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
2005  /* For compatibility with previous versions use the strip method by
2006  * default. This code works because if PNG_SCALE_16_TO_8 is already
2007  * set the code below will do that in preference to the chop.
2008  */
2009  png_ptr->transformations |= PNG_16_TO_8;
2010  info_ptr->bit_depth = 8;
2011 # else
2012 
2013 # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
2014  png_ptr->transformations |= PNG_SCALE_16_TO_8;
2015  info_ptr->bit_depth = 8;
2016 # else
2017 
2018  CONFIGURATION ERROR: you must enable at least one 16 to 8 method
2019 # endif
2020 # endif
2021 #endif /* !READ_16BIT */
2022  }
2023 
2024 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
2025  if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
2026  info_ptr->color_type = (png_byte)(info_ptr->color_type |
2028 #endif
2029 
2030 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
2031  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
2032  info_ptr->color_type = (png_byte)(info_ptr->color_type &
2034 #endif
2035 
2036 #ifdef PNG_READ_QUANTIZE_SUPPORTED
2037  if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
2038  {
2039  if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
2040  (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
2041  png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8)
2042  {
2043  info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
2044  }
2045  }
2046 #endif
2047 
2048 #ifdef PNG_READ_EXPAND_16_SUPPORTED
2049  if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
2050  info_ptr->bit_depth == 8 &&
2051  info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
2052  {
2053  info_ptr->bit_depth = 16;
2054  }
2055 #endif
2056 
2057 #ifdef PNG_READ_PACK_SUPPORTED
2058  if ((png_ptr->transformations & PNG_PACK) != 0 &&
2059  (info_ptr->bit_depth < 8))
2060  info_ptr->bit_depth = 8;
2061 #endif
2062 
2063  if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
2064  info_ptr->channels = 1;
2065 
2066  else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
2067  info_ptr->channels = 3;
2068 
2069  else
2070  info_ptr->channels = 1;
2071 
2073  if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
2074  {
2075  info_ptr->color_type = (png_byte)(info_ptr->color_type &
2077  info_ptr->num_trans = 0;
2078  }
2079 #endif
2080 
2081  if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
2082  info_ptr->channels++;
2083 
2085  /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
2086  if ((png_ptr->transformations & PNG_FILLER) != 0 &&
2087  (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
2088  info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
2089  {
2090  info_ptr->channels++;
2091  /* If adding a true alpha channel not just filler */
2092  if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
2093  info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
2094  }
2095 #endif
2096 
2097 #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
2098 defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
2099  if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
2100  {
2101  if (info_ptr->bit_depth < png_ptr->user_transform_depth)
2102  info_ptr->bit_depth = png_ptr->user_transform_depth;
2103 
2104  if (info_ptr->channels < png_ptr->user_transform_channels)
2105  info_ptr->channels = png_ptr->user_transform_channels;
2106  }
2107 #endif
2108 
2109  info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
2110  info_ptr->bit_depth);
2111 
2112  info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
2113 
2114  /* Adding in 1.5.4: cache the above value in png_struct so that we can later
2115  * check in png_rowbytes that the user buffer won't get overwritten. Note
2116  * that the field is not always set - if png_read_update_info isn't called
2117  * the application has to either not do any transforms or get the calculation
2118  * right itself.
2119  */
2120  png_ptr->info_rowbytes = info_ptr->rowbytes;
2121 
2122 #ifndef PNG_READ_EXPAND_SUPPORTED
2123  if (png_ptr != NULL)
2124  return;
2125 #endif
2126 }
#define PNG_GRAY_TO_RGB
Definition: pngpriv.h:574
#define PNG_STRIP_ALPHA
Definition: pngpriv.h:578
png_size_t rowbytes
Definition: pnginfo.h:62
#define PNG_COMPOSE
Definition: pngpriv.h:567
#define PNG_READ_STRIP_ALPHA_SUPPORTED
Definition: pnglibconf.h:74
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define PNG_SCALE_16_TO_8
Definition: pngpriv.h:587
png_size_t info_rowbytes
Definition: pngstruct.h:234
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
#define PNG_FILLER
Definition: pngpriv.h:575
png_byte color_type
Definition: pnginfo.h:67
#define PNG_EXPAND_tRNS
Definition: pngpriv.h:586
png_colorp palette
Definition: pngstruct.h:238
png_byte channels
Definition: pnginfo.h:76
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
#define PNG_READ_FILLER_SUPPORTED
Definition: pnglibconf.h:58
png_byte bit_depth
Definition: pnginfo.h:66
#define PNG_ADD_ALPHA
Definition: pngpriv.h:585
#define PNG_COLOR_MASK_ALPHA
Definition: png.h:806
png_byte pixel_depth
Definition: pnginfo.h:77
png_uint_16 num_trans
Definition: pngstruct.h:246
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define PNG_QUANTIZE
Definition: pngpriv.h:566
#define PNG_USER_TRANSFORM
Definition: pngpriv.h:580
#define PNG_RGB_TO_GRAY
Definition: pngpriv.h:583
png_uint_32 width
Definition: pnginfo.h:59
png_uint_16 num_trans
Definition: pnginfo.h:65
#define PNG_16_TO_8
Definition: pngpriv.h:570
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 transformations
Definition: pngstruct.h:181
#define PNG_PACK
Definition: pngpriv.h:562
#define PNG_EXPAND_16
Definition: pngpriv.h:569
#define PNG_EXPAND
Definition: pngpriv.h:572
void png_do_strip_channel ( png_row_infop  row_info,
png_bytep  row,
int  at_start 
)
495 {
496  png_bytep sp = row; /* source pointer */
497  png_bytep dp = row; /* destination pointer */
498  png_bytep ep = row + row_info->rowbytes; /* One beyond end of row */
499 
500  /* At the start sp will point to the first byte to copy and dp to where
501  * it is copied to. ep always points just beyond the end of the row, so
502  * the loop simply copies (channels-1) channels until sp reaches ep.
503  *
504  * at_start: 0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc.
505  * nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc.
506  */
507 
508  /* GA, GX, XG cases */
509  if (row_info->channels == 2)
510  {
511  if (row_info->bit_depth == 8)
512  {
513  if (at_start != 0) /* Skip initial filler */
514  ++sp;
515  else /* Skip initial channel and, for sp, the filler */
516  sp += 2, ++dp;
517 
518  /* For a 1 pixel wide image there is nothing to do */
519  while (sp < ep)
520  *dp++ = *sp, sp += 2;
521 
522  row_info->pixel_depth = 8;
523  }
524 
525  else if (row_info->bit_depth == 16)
526  {
527  if (at_start != 0) /* Skip initial filler */
528  sp += 2;
529  else /* Skip initial channel and, for sp, the filler */
530  sp += 4, dp += 2;
531 
532  while (sp < ep)
533  *dp++ = *sp++, *dp++ = *sp, sp += 3;
534 
535  row_info->pixel_depth = 16;
536  }
537 
538  else
539  return; /* bad bit depth */
540 
541  row_info->channels = 1;
542 
543  /* Finally fix the color type if it records an alpha channel */
544  if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
545  row_info->color_type = PNG_COLOR_TYPE_GRAY;
546  }
547 
548  /* RGBA, RGBX, XRGB cases */
549  else if (row_info->channels == 4)
550  {
551  if (row_info->bit_depth == 8)
552  {
553  if (at_start != 0) /* Skip initial filler */
554  ++sp;
555  else /* Skip initial channels and, for sp, the filler */
556  sp += 4, dp += 3;
557 
558  /* Note that the loop adds 3 to dp and 4 to sp each time. */
559  while (sp < ep)
560  *dp++ = *sp++, *dp++ = *sp++, *dp++ = *sp, sp += 2;
561 
562  row_info->pixel_depth = 24;
563  }
564 
565  else if (row_info->bit_depth == 16)
566  {
567  if (at_start != 0) /* Skip initial filler */
568  sp += 2;
569  else /* Skip initial channels and, for sp, the filler */
570  sp += 8, dp += 6;
571 
572  while (sp < ep)
573  {
574  /* Copy 6 bytes, skip 2 */
575  *dp++ = *sp++, *dp++ = *sp++;
576  *dp++ = *sp++, *dp++ = *sp++;
577  *dp++ = *sp++, *dp++ = *sp, sp += 3;
578  }
579 
580  row_info->pixel_depth = 48;
581  }
582 
583  else
584  return; /* bad bit depth */
585 
586  row_info->channels = 3;
587 
588  /* Finally fix the color type if it records an alpha channel */
589  if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
590  row_info->color_type = PNG_COLOR_TYPE_RGB;
591  }
592 
593  else
594  return; /* The filler channel has gone already */
595 
596  /* Fix the rowbytes value. */
597  row_info->rowbytes = dp-row;
598 }
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
png_byte * png_bytep
Definition: pngconf.h:600
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
png_byte color_type
Definition: png.h:898
png_byte channels
Definition: png.h:900
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
png_byte pixel_depth
Definition: png.h:901
png_size_t rowbytes
Definition: png.h:897
png_byte bit_depth
Definition: png.h:899
void png_do_swap ( png_row_infop  row_info,
png_bytep  row 
)
319 {
320  png_debug(1, "in png_do_swap");
321 
322  if (row_info->bit_depth == 16)
323  {
324  png_bytep rp = row;
325  png_uint_32 i;
326  png_uint_32 istop= row_info->width * row_info->channels;
327 
328  for (i = 0; i < istop; i++, rp += 2)
329  {
330 #ifdef PNG_BUILTIN_BSWAP16_SUPPORTED
331  /* Feature added to libpng-1.6.11 for testing purposes, not
332  * enabled by default.
333  */
334  *(png_uint_16*)rp = __builtin_bswap16(*(png_uint_16*)rp);
335 #else
336  png_byte t = *rp;
337  *rp = *(rp + 1);
338  *(rp + 1) = t;
339 #endif
340  }
341  }
342 }
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
png_uint_32 width
Definition: png.h:896
png_byte channels
Definition: png.h:900
png_byte bit_depth
Definition: png.h:899
void png_do_packswap ( png_row_infop  row_info,
png_bytep  row 
)
455 {
456  png_debug(1, "in png_do_packswap");
457 
458  if (row_info->bit_depth < 8)
459  {
460  png_bytep rp;
461  png_const_bytep end, table;
462 
463  end = row + row_info->rowbytes;
464 
465  if (row_info->bit_depth == 1)
466  table = onebppswaptable;
467 
468  else if (row_info->bit_depth == 2)
469  table = twobppswaptable;
470 
471  else if (row_info->bit_depth == 4)
472  table = fourbppswaptable;
473 
474  else
475  return;
476 
477  for (rp = row; rp < end; rp++)
478  *rp = table[*rp];
479  }
480 }
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
const png_byte * png_const_bytep
Definition: pngconf.h:601
static const png_byte twobppswaptable[256]
Definition: pngtrans.c:382
png_size_t rowbytes
Definition: png.h:897
static const png_byte fourbppswaptable[256]
Definition: pngtrans.c:417
static const png_byte onebppswaptable[256]
Definition: pngtrans.c:347
png_byte bit_depth
Definition: png.h:899
void png_do_invert ( png_row_infop  row_info,
png_bytep  row 
)
262 {
263  png_debug(1, "in png_do_invert");
264 
265  /* This test removed from libpng version 1.0.13 and 1.2.0:
266  * if (row_info->bit_depth == 1 &&
267  */
268  if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
269  {
270  png_bytep rp = row;
271  png_size_t i;
272  png_size_t istop = row_info->rowbytes;
273 
274  for (i = 0; i < istop; i++)
275  {
276  *rp = (png_byte)(~(*rp));
277  rp++;
278  }
279  }
280 
281  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
282  row_info->bit_depth == 8)
283  {
284  png_bytep rp = row;
285  png_size_t i;
286  png_size_t istop = row_info->rowbytes;
287 
288  for (i = 0; i < istop; i += 2)
289  {
290  *rp = (png_byte)(~(*rp));
291  rp += 2;
292  }
293  }
294 
295 #ifdef PNG_16BIT_SUPPORTED
296  else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
297  row_info->bit_depth == 16)
298  {
299  png_bytep rp = row;
300  png_size_t i;
301  png_size_t istop = row_info->rowbytes;
302 
303  for (i = 0; i < istop; i += 4)
304  {
305  *rp = (png_byte)(~(*rp));
306  *(rp + 1) = (png_byte)(~(*(rp + 1)));
307  rp += 4;
308  }
309  }
310 #endif
311 }
int i
Definition: rw_test.cpp:37
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
png_byte color_type
Definition: png.h:898
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
png_size_t rowbytes
Definition: png.h:897
size_t png_size_t
Definition: pngconf.h:543
png_byte bit_depth
Definition: png.h:899
void png_do_bgr ( png_row_infop  row_info,
png_bytep  row 
)
605 {
606  png_debug(1, "in png_do_bgr");
607 
608  if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
609  {
610  png_uint_32 row_width = row_info->width;
611  if (row_info->bit_depth == 8)
612  {
613  if (row_info->color_type == PNG_COLOR_TYPE_RGB)
614  {
615  png_bytep rp;
616  png_uint_32 i;
617 
618  for (i = 0, rp = row; i < row_width; i++, rp += 3)
619  {
620  png_byte save = *rp;
621  *rp = *(rp + 2);
622  *(rp + 2) = save;
623  }
624  }
625 
626  else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
627  {
628  png_bytep rp;
629  png_uint_32 i;
630 
631  for (i = 0, rp = row; i < row_width; i++, rp += 4)
632  {
633  png_byte save = *rp;
634  *rp = *(rp + 2);
635  *(rp + 2) = save;
636  }
637  }
638  }
639 
640 #ifdef PNG_16BIT_SUPPORTED
641  else if (row_info->bit_depth == 16)
642  {
643  if (row_info->color_type == PNG_COLOR_TYPE_RGB)
644  {
645  png_bytep rp;
646  png_uint_32 i;
647 
648  for (i = 0, rp = row; i < row_width; i++, rp += 6)
649  {
650  png_byte save = *rp;
651  *rp = *(rp + 4);
652  *(rp + 4) = save;
653  save = *(rp + 1);
654  *(rp + 1) = *(rp + 5);
655  *(rp + 5) = save;
656  }
657  }
658 
659  else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
660  {
661  png_bytep rp;
662  png_uint_32 i;
663 
664  for (i = 0, rp = row; i < row_width; i++, rp += 8)
665  {
666  png_byte save = *rp;
667  *rp = *(rp + 4);
668  *(rp + 4) = save;
669  save = *(rp + 1);
670  *(rp + 1) = *(rp + 5);
671  *(rp + 5) = save;
672  }
673  }
674  }
675 #endif
676  }
677 }
int i
Definition: rw_test.cpp:37
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
png_byte * png_bytep
Definition: pngconf.h:600
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
png_uint_32 width
Definition: png.h:896
png_byte color_type
Definition: png.h:898
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
png_byte bit_depth
Definition: png.h:899
void png_handle_IHDR ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
794 {
795  png_byte buf[13];
796  png_uint_32 width, height;
797  int bit_depth, color_type, compression_type, filter_type;
798  int interlace_type;
799 
800  png_debug(1, "in png_handle_IHDR");
801 
802  if ((png_ptr->mode & PNG_HAVE_IHDR) != 0)
803  png_chunk_error(png_ptr, "out of place");
804 
805  /* Check the length */
806  if (length != 13)
807  png_chunk_error(png_ptr, "invalid");
808 
809  png_ptr->mode |= PNG_HAVE_IHDR;
810 
811  png_crc_read(png_ptr, buf, 13);
812  png_crc_finish(png_ptr, 0);
813 
814  width = png_get_uint_31(png_ptr, buf);
815  height = png_get_uint_31(png_ptr, buf + 4);
816  bit_depth = buf[8];
817  color_type = buf[9];
818  compression_type = buf[10];
819  filter_type = buf[11];
820  interlace_type = buf[12];
821 
822  /* Set internal variables */
823  png_ptr->width = width;
824  png_ptr->height = height;
825  png_ptr->bit_depth = (png_byte)bit_depth;
826  png_ptr->interlaced = (png_byte)interlace_type;
827  png_ptr->color_type = (png_byte)color_type;
828 #ifdef PNG_MNG_FEATURES_SUPPORTED
829  png_ptr->filter_type = (png_byte)filter_type;
830 #endif
831  png_ptr->compression_type = (png_byte)compression_type;
832 
833  /* Find number of channels */
834  switch (png_ptr->color_type)
835  {
836  default: /* invalid, png_set_IHDR calls png_error */
837  case PNG_COLOR_TYPE_GRAY:
839  png_ptr->channels = 1;
840  break;
841 
842  case PNG_COLOR_TYPE_RGB:
843  png_ptr->channels = 3;
844  break;
845 
847  png_ptr->channels = 2;
848  break;
849 
851  png_ptr->channels = 4;
852  break;
853  }
854 
855  /* Set up other useful info */
856  png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels);
857  png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width);
858  png_debug1(3, "bit_depth = %d", png_ptr->bit_depth);
859  png_debug1(3, "channels = %d", png_ptr->channels);
860  png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes);
861  png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
862  color_type, interlace_type, compression_type, filter_type);
863 }
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
png_uint_32 png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:22
PNG_IMPEXP void() png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_method, int compression_method, int filter_method)
Definition: pngset.c:206
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
png_uint_32 width
Definition: pngstruct.h:214
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
png_byte color_type
Definition: pngstruct.h:252
#define PNG_HAVE_IHDR
Definition: png.h:785
png_byte compression_type
Definition: pngstruct.h:434
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_byte pixel_depth
Definition: pngstruct.h:255
png_byte bit_depth
Definition: pngstruct.h:253
png_byte channels
Definition: pngstruct.h:256
png_uint_32 length
Definition: png.c:2173
png_size_t rowbytes
Definition: pngstruct.h:218
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
png_uint_32 height
Definition: pngstruct.h:215
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_byte interlaced
Definition: pngstruct.h:249
void png_handle_PLTE ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
868 {
870  int num, i;
871 #ifdef PNG_POINTER_INDEXING_SUPPORTED
872  png_colorp pal_ptr;
873 #endif
874 
875  png_debug(1, "in png_handle_PLTE");
876 
877  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
878  png_chunk_error(png_ptr, "missing IHDR");
879 
880  /* Moved to before the 'after IDAT' check below because otherwise duplicate
881  * PLTE chunks are potentially ignored (the spec says there shall not be more
882  * than one PLTE, the error is not treated as benign, so this check trumps
883  * the requirement that PLTE appears before IDAT.)
884  */
885  else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0)
886  png_chunk_error(png_ptr, "duplicate");
887 
888  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
889  {
890  /* This is benign because the non-benign error happened before, when an
891  * IDAT was encountered in a color-mapped image with no PLTE.
892  */
893  png_crc_finish(png_ptr, length);
894  png_chunk_benign_error(png_ptr, "out of place");
895  return;
896  }
897 
898  png_ptr->mode |= PNG_HAVE_PLTE;
899 
900  if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
901  {
902  png_crc_finish(png_ptr, length);
903  png_chunk_benign_error(png_ptr, "ignored in grayscale PNG");
904  return;
905  }
906 
907 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
908  if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
909  {
910  png_crc_finish(png_ptr, length);
911  return;
912  }
913 #endif
914 
915  if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3)
916  {
917  png_crc_finish(png_ptr, length);
918 
919  if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
920  png_chunk_benign_error(png_ptr, "invalid");
921 
922  else
923  png_chunk_error(png_ptr, "invalid");
924 
925  return;
926  }
927 
928  /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */
929  num = (int)length / 3;
930 
931 #ifdef PNG_POINTER_INDEXING_SUPPORTED
932  for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++)
933  {
934  png_byte buf[3];
935 
936  png_crc_read(png_ptr, buf, 3);
937  pal_ptr->red = buf[0];
938  pal_ptr->green = buf[1];
939  pal_ptr->blue = buf[2];
940  }
941 #else
942  for (i = 0; i < num; i++)
943  {
944  png_byte buf[3];
945 
946  png_crc_read(png_ptr, buf, 3);
947  /* Don't depend upon png_color being any order */
948  palette[i].red = buf[0];
949  palette[i].green = buf[1];
950  palette[i].blue = buf[2];
951  }
952 #endif
953 
954  /* If we actually need the PLTE chunk (ie for a paletted image), we do
955  * whatever the normal CRC configuration tells us. However, if we
956  * have an RGB image, the PLTE can be considered ancillary, so
957  * we will act as though it is.
958  */
959 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
960  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
961 #endif
962  {
963  png_crc_finish(png_ptr, 0);
964  }
965 
966 #ifndef PNG_READ_OPT_PLTE_SUPPORTED
967  else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */
968  {
969  /* If we don't want to use the data from an ancillary chunk,
970  * we have two options: an error abort, or a warning and we
971  * ignore the data in this chunk (which should be OK, since
972  * it's considered ancillary for a RGB or RGBA image).
973  *
974  * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the
975  * chunk type to determine whether to check the ancillary or the critical
976  * flags.
977  */
978  if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0)
979  {
980  if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0)
981  return;
982 
983  else
984  png_chunk_error(png_ptr, "CRC error");
985  }
986 
987  /* Otherwise, we (optionally) emit a warning and use the chunk. */
988  else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0)
989  png_chunk_warning(png_ptr, "CRC error");
990  }
991 #endif
992 
993  /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its
994  * own copy of the palette. This has the side effect that when png_start_row
995  * is called (this happens after any call to png_read_update_info) the
996  * info_ptr palette gets changed. This is extremely unexpected and
997  * confusing.
998  *
999  * Fix this by not sharing the palette in this way.
1000  */
1001  png_set_PLTE(png_ptr, info_ptr, palette, num);
1002 
1003  /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before
1004  * IDAT. Prior to 1.6.0 this was not checked; instead the code merely
1005  * checked the apparent validity of a tRNS chunk inserted before PLTE on a
1006  * palette PNG. 1.6.0 attempts to rigorously follow the standard and
1007  * therefore does a benign error if the erroneous condition is detected *and*
1008  * cancels the tRNS if the benign error returns. The alternative is to
1009  * amend the standard since it would be rather hypocritical of the standards
1010  * maintainers to ignore it.
1011  */
1012 #ifdef PNG_READ_tRNS_SUPPORTED
1013  if (png_ptr->num_trans > 0 ||
1014  (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0))
1015  {
1016  /* Cancel this because otherwise it would be used if the transforms
1017  * require it. Don't cancel the 'valid' flag because this would prevent
1018  * detection of duplicate chunks.
1019  */
1020  png_ptr->num_trans = 0;
1021 
1022  if (info_ptr != NULL)
1023  info_ptr->num_trans = 0;
1024 
1025  png_chunk_benign_error(png_ptr, "tRNS must be after");
1026  }
1027 #endif
1028 
1029 #ifdef PNG_READ_hIST_SUPPORTED
1030  if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1031  png_chunk_benign_error(png_ptr, "hIST must be after");
1032 #endif
1033 
1034 #ifdef PNG_READ_bKGD_SUPPORTED
1035  if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1036  png_chunk_benign_error(png_ptr, "bKGD must be after");
1037 #endif
1038 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define PNG_INFO_hIST
Definition: png.h:879
int i
Definition: rw_test.cpp:37
#define PNG_MAX_PALETTE_LENGTH
Definition: png.h:866
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
PNG_IMPEXP void() png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_const_colorp palette, int num_palette)
Definition: pngset.c:505
png_byte red
Definition: png.h:621
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
int png_crc_error(png_structrp png_ptr)
Definition: pngrutil.c:242
png_byte color_type
Definition: pngstruct.h:252
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_FLAG_CRC_ANCILLARY_USE
Definition: pngpriv.h:609
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:497
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
png_uint_32 flags
Definition: pngstruct.h:180
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_FLAG_CRC_ANCILLARY_NOWARN
Definition: pngpriv.h:610
Definition: png.h:619
png_uint_32 length
Definition: png.c:2173
#define PNG_INFO_tRNS
Definition: png.h:877
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
png_uint_16 num_trans
Definition: pngstruct.h:246
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
png_uint_16 num_trans
Definition: pnginfo.h:65
#define PNG_INFO_bKGD
Definition: png.h:878
png_byte green
Definition: png.h:622
png_byte blue
Definition: png.h:623
void png_handle_IEND ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1042 {
1043  png_debug(1, "in png_handle_IEND");
1044 
1045  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 ||
1046  (png_ptr->mode & PNG_HAVE_IDAT) == 0)
1047  png_chunk_error(png_ptr, "out of place");
1048 
1049  png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND);
1050 
1051  png_crc_finish(png_ptr, length);
1052 
1053  if (length != 0)
1054  png_chunk_benign_error(png_ptr, "invalid");
1055 
1056  PNG_UNUSED(info_ptr)
1057 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_AFTER_IDAT
Definition: png.h:787
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
#define PNG_HAVE_IEND
Definition: pngpriv.h:546
void png_handle_bKGD ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1855 {
1856  unsigned int truelen;
1857  png_byte buf[6];
1858  png_color_16 background;
1859 
1860  png_debug(1, "in png_handle_bKGD");
1861 
1862  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1863  png_chunk_error(png_ptr, "missing IHDR");
1864 
1865  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1866  (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
1867  (png_ptr->mode & PNG_HAVE_PLTE) == 0))
1868  {
1869  png_crc_finish(png_ptr, length);
1870  png_chunk_benign_error(png_ptr, "out of place");
1871  return;
1872  }
1873 
1874  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0)
1875  {
1876  png_crc_finish(png_ptr, length);
1877  png_chunk_benign_error(png_ptr, "duplicate");
1878  return;
1879  }
1880 
1881  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1882  truelen = 1;
1883 
1884  else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1885  truelen = 6;
1886 
1887  else
1888  truelen = 2;
1889 
1890  if (length != truelen)
1891  {
1892  png_crc_finish(png_ptr, length);
1893  png_chunk_benign_error(png_ptr, "invalid");
1894  return;
1895  }
1896 
1897  png_crc_read(png_ptr, buf, truelen);
1898 
1899  if (png_crc_finish(png_ptr, 0) != 0)
1900  return;
1901 
1902  /* We convert the index value into RGB components so that we can allow
1903  * arbitrary RGB values for background when we have transparency, and
1904  * so it is easy to determine the RGB values of the background color
1905  * from the info_ptr struct.
1906  */
1907  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1908  {
1909  background.index = buf[0];
1910 
1911  if (info_ptr != NULL && info_ptr->num_palette != 0)
1912  {
1913  if (buf[0] >= info_ptr->num_palette)
1914  {
1915  png_chunk_benign_error(png_ptr, "invalid index");
1916  return;
1917  }
1918 
1919  background.red = (png_uint_16)png_ptr->palette[buf[0]].red;
1920  background.green = (png_uint_16)png_ptr->palette[buf[0]].green;
1921  background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue;
1922  }
1923 
1924  else
1925  background.red = background.green = background.blue = 0;
1926 
1927  background.gray = 0;
1928  }
1929 
1930  else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */
1931  {
1932  background.index = 0;
1933  background.red =
1934  background.green =
1935  background.blue =
1936  background.gray = png_get_uint_16(buf);
1937  }
1938 
1939  else
1940  {
1941  background.index = 0;
1942  background.red = png_get_uint_16(buf);
1943  background.green = png_get_uint_16(buf + 2);
1944  background.blue = png_get_uint_16(buf + 4);
1945  background.gray = 0;
1946  }
1947 
1948  png_set_bKGD(png_ptr, info_ptr, &background);
1949 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
png_uint_16 blue
Definition: png.h:634
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
png_uint_16 gray
Definition: png.h:635
png_byte red
Definition: png.h:621
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
png_uint_16() png_get_uint_16(png_const_bytep buf)
Definition: pngrutil.c:97
png_byte color_type
Definition: pngstruct.h:252
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
png_uint_16 green
Definition: png.h:633
png_colorp palette
Definition: pngstruct.h:238
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
png_uint_16 num_palette
Definition: pnginfo.h:64
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_byte index
Definition: png.h:631
PNG_IMPEXP void() png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, png_const_color_16p background)
Definition: pngset.c:25
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
png_uint_16 red
Definition: png.h:632
Definition: png.h:629
#define PNG_INFO_bKGD
Definition: png.h:878
png_byte green
Definition: png.h:622
png_byte blue
Definition: png.h:623
void png_handle_cHRM ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1180 {
1181  png_byte buf[32];
1182  png_xy xy;
1183 
1184  png_debug(1, "in png_handle_cHRM");
1185 
1186  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1187  png_chunk_error(png_ptr, "missing IHDR");
1188 
1189  else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1190  {
1191  png_crc_finish(png_ptr, length);
1192  png_chunk_benign_error(png_ptr, "out of place");
1193  return;
1194  }
1195 
1196  if (length != 32)
1197  {
1198  png_crc_finish(png_ptr, length);
1199  png_chunk_benign_error(png_ptr, "invalid");
1200  return;
1201  }
1202 
1203  png_crc_read(png_ptr, buf, 32);
1204 
1205  if (png_crc_finish(png_ptr, 0) != 0)
1206  return;
1207 
1208  xy.whitex = png_get_fixed_point(NULL, buf);
1209  xy.whitey = png_get_fixed_point(NULL, buf + 4);
1210  xy.redx = png_get_fixed_point(NULL, buf + 8);
1211  xy.redy = png_get_fixed_point(NULL, buf + 12);
1212  xy.greenx = png_get_fixed_point(NULL, buf + 16);
1213  xy.greeny = png_get_fixed_point(NULL, buf + 20);
1214  xy.bluex = png_get_fixed_point(NULL, buf + 24);
1215  xy.bluey = png_get_fixed_point(NULL, buf + 28);
1216 
1217  if (xy.whitex == PNG_FIXED_ERROR ||
1218  xy.whitey == PNG_FIXED_ERROR ||
1219  xy.redx == PNG_FIXED_ERROR ||
1220  xy.redy == PNG_FIXED_ERROR ||
1221  xy.greenx == PNG_FIXED_ERROR ||
1222  xy.greeny == PNG_FIXED_ERROR ||
1223  xy.bluex == PNG_FIXED_ERROR ||
1224  xy.bluey == PNG_FIXED_ERROR)
1225  {
1226  png_chunk_benign_error(png_ptr, "invalid values");
1227  return;
1228  }
1229 
1230  /* If a colorspace error has already been output skip this chunk */
1231  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1232  return;
1233 
1234  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0)
1235  {
1236  png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1237  png_colorspace_sync(png_ptr, info_ptr);
1238  png_chunk_benign_error(png_ptr, "duplicate");
1239  return;
1240  }
1241 
1242  png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM;
1243  (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy,
1244  1/*prefer cHRM values*/);
1245  png_colorspace_sync(png_ptr, info_ptr);
1246 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
#define PNG_FIXED_ERROR
Definition: pngrutil.c:38
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
int png_colorspace_set_chromaticities(png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, int preferred)
Definition: png.c:1679
png_uint_32 flags
Definition: pngstruct.h:180
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
static png_fixed_point png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:41
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
void png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
Definition: png.c:1175
void png_handle_gAMA ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1062 {
1063  png_fixed_point igamma;
1064  png_byte buf[4];
1065 
1066  png_debug(1, "in png_handle_gAMA");
1067 
1068  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1069  png_chunk_error(png_ptr, "missing IHDR");
1070 
1071  else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1072  {
1073  png_crc_finish(png_ptr, length);
1074  png_chunk_benign_error(png_ptr, "out of place");
1075  return;
1076  }
1077 
1078  if (length != 4)
1079  {
1080  png_crc_finish(png_ptr, length);
1081  png_chunk_benign_error(png_ptr, "invalid");
1082  return;
1083  }
1084 
1085  png_crc_read(png_ptr, buf, 4);
1086 
1087  if (png_crc_finish(png_ptr, 0) != 0)
1088  return;
1089 
1090  igamma = png_get_fixed_point(NULL, buf);
1091 
1092  png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma);
1093  png_colorspace_sync(png_ptr, info_ptr);
1094 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
void png_colorspace_set_gamma(png_const_structrp png_ptr, png_colorspacerp colorspace, png_fixed_point gAMA)
Definition: png.c:1074
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
static png_fixed_point png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:41
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
png_int_32 png_fixed_point
Definition: pngconf.h:595
char buf[128]
Definition: rw_test.cpp:40
void png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
Definition: png.c:1175
void png_handle_hIST ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1955 {
1956  unsigned int num, i;
1957  png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH];
1958 
1959  png_debug(1, "in png_handle_hIST");
1960 
1961  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1962  png_chunk_error(png_ptr, "missing IHDR");
1963 
1964  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 ||
1965  (png_ptr->mode & PNG_HAVE_PLTE) == 0)
1966  {
1967  png_crc_finish(png_ptr, length);
1968  png_chunk_benign_error(png_ptr, "out of place");
1969  return;
1970  }
1971 
1972  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0)
1973  {
1974  png_crc_finish(png_ptr, length);
1975  png_chunk_benign_error(png_ptr, "duplicate");
1976  return;
1977  }
1978 
1979  num = length / 2 ;
1980 
1981  if (num != png_ptr->num_palette || num > PNG_MAX_PALETTE_LENGTH)
1982  {
1983  png_crc_finish(png_ptr, length);
1984  png_chunk_benign_error(png_ptr, "invalid");
1985  return;
1986  }
1987 
1988  for (i = 0; i < num; i++)
1989  {
1990  png_byte buf[2];
1991 
1992  png_crc_read(png_ptr, buf, 2);
1993  readbuf[i] = png_get_uint_16(buf);
1994  }
1995 
1996  if (png_crc_finish(png_ptr, 0) != 0)
1997  return;
1998 
1999  png_set_hIST(png_ptr, info_ptr, readbuf);
2000 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define PNG_INFO_hIST
Definition: png.h:879
int i
Definition: rw_test.cpp:37
#define PNG_MAX_PALETTE_LENGTH
Definition: png.h:866
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
png_uint_16() png_get_uint_16(png_const_bytep buf)
Definition: pngrutil.c:97
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
png_uint_16 num_palette
Definition: pngstruct.h:239
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
PNG_IMPEXP void() png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr, png_const_uint_16p hist)
Definition: pngset.c:163
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
void png_handle_iCCP ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1303 {
1304  png_const_charp errmsg = NULL; /* error message output, or no error */
1305  int finished = 0; /* crc checked */
1306 
1307  png_debug(1, "in png_handle_iCCP");
1308 
1309  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1310  png_chunk_error(png_ptr, "missing IHDR");
1311 
1312  else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1313  {
1314  png_crc_finish(png_ptr, length);
1315  png_chunk_benign_error(png_ptr, "out of place");
1316  return;
1317  }
1318 
1319  /* Consistent with all the above colorspace handling an obviously *invalid*
1320  * chunk is just ignored, so does not invalidate the color space. An
1321  * alternative is to set the 'invalid' flags at the start of this routine
1322  * and only clear them in they were not set before and all the tests pass.
1323  * The minimum 'deflate' stream is assumed to be just the 2 byte header and
1324  * 4 byte checksum. The keyword must be at least one character and there is
1325  * a terminator (0) byte and the compression method.
1326  */
1327  if (length < 9)
1328  {
1329  png_crc_finish(png_ptr, length);
1330  png_chunk_benign_error(png_ptr, "too short");
1331  return;
1332  }
1333 
1334  /* If a colorspace error has already been output skip this chunk */
1335  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1336  {
1337  png_crc_finish(png_ptr, length);
1338  return;
1339  }
1340 
1341  /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1342  * this.
1343  */
1344  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0)
1345  {
1346  uInt read_length, keyword_length;
1347  char keyword[81];
1348 
1349  /* Find the keyword; the keyword plus separator and compression method
1350  * bytes can be at most 81 characters long.
1351  */
1352  read_length = 81; /* maximum */
1353  if (read_length > length)
1354  read_length = (uInt)length;
1355 
1356  png_crc_read(png_ptr, (png_bytep)keyword, read_length);
1357  length -= read_length;
1358 
1359  keyword_length = 0;
1360  while (keyword_length < 80 && keyword_length < read_length &&
1361  keyword[keyword_length] != 0)
1362  ++keyword_length;
1363 
1364  /* TODO: make the keyword checking common */
1365  if (keyword_length >= 1 && keyword_length <= 79)
1366  {
1367  /* We only understand '0' compression - deflate - so if we get a
1368  * different value we can't safely decode the chunk.
1369  */
1370  if (keyword_length+1 < read_length &&
1371  keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE)
1372  {
1373  read_length -= keyword_length+2;
1374 
1375  if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK)
1376  {
1377  Byte profile_header[132];
1378  Byte local_buffer[PNG_INFLATE_BUF_SIZE];
1379  png_alloc_size_t size = (sizeof profile_header);
1380 
1381  png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2);
1382  png_ptr->zstream.avail_in = read_length;
1383  (void)png_inflate_read(png_ptr, local_buffer,
1384  (sizeof local_buffer), &length, profile_header, &size,
1385  0/*finish: don't, because the output is too small*/);
1386 
1387  if (size == 0)
1388  {
1389  /* We have the ICC profile header; do the basic header checks.
1390  */
1391  const png_uint_32 profile_length =
1392  png_get_uint_32(profile_header);
1393 
1394  if (png_icc_check_length(png_ptr, &png_ptr->colorspace,
1395  keyword, profile_length) != 0)
1396  {
1397  /* The length is apparently ok, so we can check the 132
1398  * byte header.
1399  */
1400  if (png_icc_check_header(png_ptr, &png_ptr->colorspace,
1401  keyword, profile_length, profile_header,
1402  png_ptr->color_type) != 0)
1403  {
1404  /* Now read the tag table; a variable size buffer is
1405  * needed at this point, allocate one for the whole
1406  * profile. The header check has already validated
1407  * that none of these stuff will overflow.
1408  */
1409  const png_uint_32 tag_count = png_get_uint_32(
1410  profile_header+128);
1411  png_bytep profile = png_read_buffer(png_ptr,
1412  profile_length, 2/*silent*/);
1413 
1414  if (profile != NULL)
1415  {
1416  memcpy(profile, profile_header,
1417  (sizeof profile_header));
1418 
1419  size = 12 * tag_count;
1420 
1421  (void)png_inflate_read(png_ptr, local_buffer,
1422  (sizeof local_buffer), &length,
1423  profile + (sizeof profile_header), &size, 0);
1424 
1425  /* Still expect a buffer error because we expect
1426  * there to be some tag data!
1427  */
1428  if (size == 0)
1429  {
1430  if (png_icc_check_tag_table(png_ptr,
1431  &png_ptr->colorspace, keyword, profile_length,
1432  profile) != 0)
1433  {
1434  /* The profile has been validated for basic
1435  * security issues, so read the whole thing in.
1436  */
1437  size = profile_length - (sizeof profile_header)
1438  - 12 * tag_count;
1439 
1440  (void)png_inflate_read(png_ptr, local_buffer,
1441  (sizeof local_buffer), &length,
1442  profile + (sizeof profile_header) +
1443  12 * tag_count, &size, 1/*finish*/);
1444 
1445  if (length > 0 && !(png_ptr->flags &
1447  errmsg = "extra compressed data";
1448 
1449  /* But otherwise allow extra data: */
1450  else if (size == 0)
1451  {
1452  if (length > 0)
1453  {
1454  /* This can be handled completely, so
1455  * keep going.
1456  */
1457  png_chunk_warning(png_ptr,
1458  "extra compressed data");
1459  }
1460 
1461  png_crc_finish(png_ptr, length);
1462  finished = 1;
1463 
1464 # ifdef PNG_sRGB_SUPPORTED
1465  /* Check for a match against sRGB */
1466  png_icc_set_sRGB(png_ptr,
1467  &png_ptr->colorspace, profile,
1468  png_ptr->zstream.adler);
1469 # endif
1470 
1471  /* Steal the profile for info_ptr. */
1472  if (info_ptr != NULL)
1473  {
1474  png_free_data(png_ptr, info_ptr,
1475  PNG_FREE_ICCP, 0);
1476 
1477  info_ptr->iccp_name = png_voidcast(char*,
1478  png_malloc_base(png_ptr,
1479  keyword_length+1));
1480  if (info_ptr->iccp_name != NULL)
1481  {
1482  memcpy(info_ptr->iccp_name, keyword,
1483  keyword_length+1);
1484  info_ptr->iccp_proflen =
1485  profile_length;
1486  info_ptr->iccp_profile = profile;
1487  png_ptr->read_buffer = NULL; /*steal*/
1488  info_ptr->free_me |= PNG_FREE_ICCP;
1489  info_ptr->valid |= PNG_INFO_iCCP;
1490  }
1491 
1492  else
1493  {
1494  png_ptr->colorspace.flags |=
1495  PNG_COLORSPACE_INVALID;
1496  errmsg = "out of memory";
1497  }
1498  }
1499 
1500  /* else the profile remains in the read
1501  * buffer which gets reused for subsequent
1502  * chunks.
1503  */
1504 
1505  if (info_ptr != NULL)
1506  png_colorspace_sync(png_ptr, info_ptr);
1507 
1508  if (errmsg == NULL)
1509  {
1510  png_ptr->zowner = 0;
1511  return;
1512  }
1513  }
1514 
1515  else if (size > 0)
1516  errmsg = "truncated";
1517 
1518  else
1519  errmsg = png_ptr->zstream.msg;
1520  }
1521 
1522  /* else png_icc_check_tag_table output an error */
1523  }
1524 
1525  else /* profile truncated */
1526  errmsg = png_ptr->zstream.msg;
1527  }
1528 
1529  else
1530  errmsg = "out of memory";
1531  }
1532 
1533  /* else png_icc_check_header output an error */
1534  }
1535 
1536  /* else png_icc_check_length output an error */
1537  }
1538 
1539  else /* profile truncated */
1540  errmsg = png_ptr->zstream.msg;
1541 
1542  /* Release the stream */
1543  png_ptr->zowner = 0;
1544  }
1545 
1546  else /* png_inflate_claim failed */
1547  errmsg = png_ptr->zstream.msg;
1548  }
1549 
1550  else
1551  errmsg = "bad compression method"; /* or missing */
1552  }
1553 
1554  else
1555  errmsg = "bad keyword";
1556  }
1557 
1558  else
1559  errmsg = "too many profiles";
1560 
1561  /* Failure: the reason is in 'errmsg' */
1562  if (finished == 0)
1563  png_crc_finish(png_ptr, length);
1564 
1565  png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1566  png_colorspace_sync(png_ptr, info_ptr);
1567  if (errmsg != NULL) /* else already output */
1568  png_chunk_benign_error(png_ptr, errmsg);
1569 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
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
z_stream zstream
Definition: pngstruct.h:184
const char * png_const_charp
Definition: pngconf.h:611
Bytef * next_in
Definition: zlib.h:86
#define png_iCCP
Definition: pngpriv.h:764
uInt avail_in
Definition: zlib.h:87
png_byte * png_bytep
Definition: pngconf.h:600
unsigned char Byte
Definition: test_imu.cpp:41
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
#define PNG_FREE_ICCP
Definition: png.h:1913
#define PNG_HAVE_PLTE
Definition: png.h:786
#define png_voidcast(type, value)
Definition: pngpriv.h:408
png_voidp png_malloc_base(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:69
void png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, int num)
Definition: png.c:442
png_byte color_type
Definition: pngstruct.h:252
png_size_t png_alloc_size_t
Definition: pngconf.h:578
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_INFLATE_BUF_SIZE
Definition: pnglibconf.h:194
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:497
int png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length, png_const_bytep profile, int color_type)
Definition: png.c:1942
png_uint_32 flags
Definition: pngstruct.h:180
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
#define PNG_FLAG_BENIGN_ERRORS_WARN
Definition: pngpriv.h:621
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_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
uLong adler
Definition: zlib.h:102
png_uint_32 length
Definition: png.c:2173
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
Byte Bytef
Definition: zconf.h:377
png_uint_32() png_get_uint_32(png_const_bytep buf)
Definition: pngrutil.c:68
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
static int png_inflate_claim(png_structrp png_ptr, png_uint_32 owner)
Definition: pngrutil.c:332
#define Z_OK
Definition: zlib.h:173
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
int png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, png_const_charp name, png_uint_32 profile_length)
Definition: png.c:1931
png_uint_32 free_me
Definition: pnginfo.h:221
png_uint_32 zowner
Definition: pngstruct.h:183
void png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
Definition: png.c:1175
#define PNG_INFO_iCCP
Definition: png.h:885
unsigned int uInt
Definition: zconf.h:370
static int png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, int finish)
Definition: pngrutil.c:728
void png_handle_iTXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2557 {
2558  png_const_charp errmsg = NULL;
2559  png_bytep buffer;
2560  png_uint_32 prefix_length;
2561 
2562  png_debug(1, "in png_handle_iTXt");
2563 
2564 #ifdef PNG_USER_LIMITS_SUPPORTED
2565  if (png_ptr->user_chunk_cache_max != 0)
2566  {
2567  if (png_ptr->user_chunk_cache_max == 1)
2568  {
2569  png_crc_finish(png_ptr, length);
2570  return;
2571  }
2572 
2573  if (--png_ptr->user_chunk_cache_max == 1)
2574  {
2575  png_crc_finish(png_ptr, length);
2576  png_chunk_benign_error(png_ptr, "no space in chunk cache");
2577  return;
2578  }
2579  }
2580 #endif
2581 
2582  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2583  png_chunk_error(png_ptr, "missing IHDR");
2584 
2585  if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2586  png_ptr->mode |= PNG_AFTER_IDAT;
2587 
2588  buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2589 
2590  if (buffer == NULL)
2591  {
2592  png_crc_finish(png_ptr, length);
2593  png_chunk_benign_error(png_ptr, "out of memory");
2594  return;
2595  }
2596 
2597  png_crc_read(png_ptr, buffer, length);
2598 
2599  if (png_crc_finish(png_ptr, 0) != 0)
2600  return;
2601 
2602  /* First the keyword. */
2603  for (prefix_length=0;
2604  prefix_length < length && buffer[prefix_length] != 0;
2605  ++prefix_length)
2606  /* Empty loop */ ;
2607 
2608  /* Perform a basic check on the keyword length here. */
2609  if (prefix_length > 79 || prefix_length < 1)
2610  errmsg = "bad keyword";
2611 
2612  /* Expect keyword, compression flag, compression type, language, translated
2613  * keyword (both may be empty but are 0 terminated) then the text, which may
2614  * be empty.
2615  */
2616  else if (prefix_length + 5 > length)
2617  errmsg = "truncated";
2618 
2619  else if (buffer[prefix_length+1] == 0 ||
2620  (buffer[prefix_length+1] == 1 &&
2621  buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE))
2622  {
2623  int compressed = buffer[prefix_length+1] != 0;
2624  png_uint_32 language_offset, translated_keyword_offset;
2625  png_alloc_size_t uncompressed_length = 0;
2626 
2627  /* Now the language tag */
2628  prefix_length += 3;
2629  language_offset = prefix_length;
2630 
2631  for (; prefix_length < length && buffer[prefix_length] != 0;
2632  ++prefix_length)
2633  /* Empty loop */ ;
2634 
2635  /* WARNING: the length may be invalid here, this is checked below. */
2636  translated_keyword_offset = ++prefix_length;
2637 
2638  for (; prefix_length < length && buffer[prefix_length] != 0;
2639  ++prefix_length)
2640  /* Empty loop */ ;
2641 
2642  /* prefix_length should now be at the trailing '\0' of the translated
2643  * keyword, but it may already be over the end. None of this arithmetic
2644  * can overflow because chunks are at most 2^31 bytes long, but on 16-bit
2645  * systems the available allocation may overflow.
2646  */
2647  ++prefix_length;
2648 
2649  if (compressed == 0 && prefix_length <= length)
2650  uncompressed_length = length - prefix_length;
2651 
2652  else if (compressed != 0 && prefix_length < length)
2653  {
2654  uncompressed_length = PNG_SIZE_MAX;
2655 
2656  /* TODO: at present png_decompress_chunk imposes a single application
2657  * level memory limit, this should be split to different values for
2658  * iCCP and text chunks.
2659  */
2660  if (png_decompress_chunk(png_ptr, length, prefix_length,
2661  &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2662  buffer = png_ptr->read_buffer;
2663 
2664  else
2665  errmsg = png_ptr->zstream.msg;
2666  }
2667 
2668  else
2669  errmsg = "truncated";
2670 
2671  if (errmsg == NULL)
2672  {
2673  png_text text;
2674 
2675  buffer[uncompressed_length+prefix_length] = 0;
2676 
2677  if (compressed == 0)
2679 
2680  else
2682 
2683  text.key = (png_charp)buffer;
2684  text.lang = (png_charp)buffer + language_offset;
2685  text.lang_key = (png_charp)buffer + translated_keyword_offset;
2686  text.text = (png_charp)buffer + prefix_length;
2687  text.text_length = 0;
2688  text.itxt_length = uncompressed_length;
2689 
2690  if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2691  errmsg = "insufficient memory";
2692  }
2693  }
2694 
2695  else
2696  errmsg = "bad compression info";
2697 
2698  if (errmsg != NULL)
2699  png_chunk_benign_error(png_ptr, errmsg);
2700 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
png_charp text
Definition: png.h:711
z_stream zstream
Definition: pngstruct.h:184
const char * png_const_charp
Definition: pngconf.h:611
int compression
Definition: png.h:705
static int png_decompress_chunk(png_structrp png_ptr, png_uint_32 chunklength, png_uint_32 prefix_size, png_alloc_size_t *newlength, int terminate)
Definition: pngrutil.c:564
png_byte * png_bytep
Definition: pngconf.h:600
#define PNG_ITXT_COMPRESSION_zTXt
Definition: png.h:732
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
png_charp lang
Definition: png.h:715
png_size_t png_alloc_size_t
Definition: pngconf.h:578
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
png_charp key
Definition: png.h:710
#define PNG_HAVE_IHDR
Definition: png.h:785
Definition: png.h:703
static char buffer[255]
Definition: propagator_simple.cpp:60
#define PNG_ITXT_COMPRESSION_NONE
Definition: png.h:731
#define PNG_SIZE_MAX
Definition: png.h:792
#define Z_STREAM_END
Definition: zlib.h:174
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_AFTER_IDAT
Definition: png.h:787
png_size_t itxt_length
Definition: png.h:714
png_uint_32 length
Definition: png.c:2173
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
int png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr, png_const_textp text_ptr, int num_text)
Definition: pngset.c:699
png_charp lang_key
Definition: png.h:717
png_size_t text_length
Definition: png.h:713
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
void png_handle_oFFs ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2052 {
2053  png_byte buf[9];
2054  png_int_32 offset_x, offset_y;
2055  int unit_type;
2056 
2057  png_debug(1, "in png_handle_oFFs");
2058 
2059  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2060  png_chunk_error(png_ptr, "missing IHDR");
2061 
2062  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2063  {
2064  png_crc_finish(png_ptr, length);
2065  png_chunk_benign_error(png_ptr, "out of place");
2066  return;
2067  }
2068 
2069  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0)
2070  {
2071  png_crc_finish(png_ptr, length);
2072  png_chunk_benign_error(png_ptr, "duplicate");
2073  return;
2074  }
2075 
2076  if (length != 9)
2077  {
2078  png_crc_finish(png_ptr, length);
2079  png_chunk_benign_error(png_ptr, "invalid");
2080  return;
2081  }
2082 
2083  png_crc_read(png_ptr, buf, 9);
2084 
2085  if (png_crc_finish(png_ptr, 0) != 0)
2086  return;
2087 
2088  offset_x = png_get_int_32(buf);
2089  offset_y = png_get_int_32(buf + 4);
2090  unit_type = buf[8];
2091  png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type);
2092 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define png_debug(l, m)
Definition: pngdebug.h:146
png_int_32() png_get_int_32(png_const_bytep buf)
Definition: pngrutil.c:85
#define PNG_INFO_oFFs
Definition: png.h:881
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
PNG_IMPEXP void() png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr, png_int_32 offset_x, png_int_32 offset_y, int unit_type)
Definition: pngset.c:247
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
void png_handle_pCAL ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2099 {
2100  png_int_32 X0, X1;
2101  png_byte type, nparams;
2102  png_bytep buffer, buf, units, endptr;
2103  png_charpp params;
2104  int i;
2105 
2106  png_debug(1, "in png_handle_pCAL");
2107 
2108  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2109  png_chunk_error(png_ptr, "missing IHDR");
2110 
2111  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2112  {
2113  png_crc_finish(png_ptr, length);
2114  png_chunk_benign_error(png_ptr, "out of place");
2115  return;
2116  }
2117 
2118  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0)
2119  {
2120  png_crc_finish(png_ptr, length);
2121  png_chunk_benign_error(png_ptr, "duplicate");
2122  return;
2123  }
2124 
2125  png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)",
2126  length + 1);
2127 
2128  buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2129 
2130  if (buffer == NULL)
2131  {
2132  png_crc_finish(png_ptr, length);
2133  png_chunk_benign_error(png_ptr, "out of memory");
2134  return;
2135  }
2136 
2137  png_crc_read(png_ptr, buffer, length);
2138 
2139  if (png_crc_finish(png_ptr, 0) != 0)
2140  return;
2141 
2142  buffer[length] = 0; /* Null terminate the last string */
2143 
2144  png_debug(3, "Finding end of pCAL purpose string");
2145  for (buf = buffer; *buf; buf++)
2146  /* Empty loop */ ;
2147 
2148  endptr = buffer + length;
2149 
2150  /* We need to have at least 12 bytes after the purpose string
2151  * in order to get the parameter information.
2152  */
2153  if (endptr <= buf + 12)
2154  {
2155  png_chunk_benign_error(png_ptr, "invalid");
2156  return;
2157  }
2158 
2159  png_debug(3, "Reading pCAL X0, X1, type, nparams, and units");
2160  X0 = png_get_int_32((png_bytep)buf+1);
2161  X1 = png_get_int_32((png_bytep)buf+5);
2162  type = buf[9];
2163  nparams = buf[10];
2164  units = buf + 11;
2165 
2166  png_debug(3, "Checking pCAL equation type and number of parameters");
2167  /* Check that we have the right number of parameters for known
2168  * equation types.
2169  */
2170  if ((type == PNG_EQUATION_LINEAR && nparams != 2) ||
2171  (type == PNG_EQUATION_BASE_E && nparams != 3) ||
2172  (type == PNG_EQUATION_ARBITRARY && nparams != 3) ||
2173  (type == PNG_EQUATION_HYPERBOLIC && nparams != 4))
2174  {
2175  png_chunk_benign_error(png_ptr, "invalid parameter count");
2176  return;
2177  }
2178 
2179  else if (type >= PNG_EQUATION_LAST)
2180  {
2181  png_chunk_benign_error(png_ptr, "unrecognized equation type");
2182  }
2183 
2184  for (buf = units; *buf; buf++)
2185  /* Empty loop to move past the units string. */ ;
2186 
2187  png_debug(3, "Allocating pCAL parameters array");
2188 
2189  params = png_voidcast(png_charpp, png_malloc_warn(png_ptr,
2190  nparams * (sizeof (png_charp))));
2191 
2192  if (params == NULL)
2193  {
2194  png_chunk_benign_error(png_ptr, "out of memory");
2195  return;
2196  }
2197 
2198  /* Get pointers to the start of each parameter string. */
2199  for (i = 0; i < nparams; i++)
2200  {
2201  buf++; /* Skip the null string terminator from previous parameter. */
2202 
2203  png_debug1(3, "Reading pCAL parameter %d", i);
2204 
2205  for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++)
2206  /* Empty loop to move past each parameter string */ ;
2207 
2208  /* Make sure we haven't run out of data yet */
2209  if (buf > endptr)
2210  {
2211  png_free(png_ptr, params);
2212  png_chunk_benign_error(png_ptr, "invalid data");
2213  return;
2214  }
2215  }
2216 
2217  png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams,
2218  (png_charp)units, params);
2219 
2220  png_free(png_ptr, params);
2221 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
PNG_IMPEXP void() png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr, png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, png_const_charp units, png_charpp params)
Definition: pngset.c:264
int i
Definition: rw_test.cpp:37
#define PNG_EQUATION_LINEAR
Definition: png.h:838
png_byte * png_bytep
Definition: pngconf.h:600
PNG_IMPEXP png_voidp() png_malloc_warn(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:210
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
png_int_32() png_get_int_32(png_const_bytep buf)
Definition: pngrutil.c:85
#define png_voidcast(type, value)
Definition: pngpriv.h:408
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
static char buffer[255]
Definition: propagator_simple.cpp:60
#define PNG_EQUATION_ARBITRARY
Definition: png.h:840
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
char ** png_charpp
Definition: pngconf.h:633
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_EQUATION_LAST
Definition: png.h:842
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
#define PNG_EQUATION_HYPERBOLIC
Definition: png.h:841
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
char buf[128]
Definition: rw_test.cpp:40
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
#define PNG_EQUATION_BASE_E
Definition: png.h:839
#define PNG_INFO_pCAL
Definition: png.h:883
void png_handle_pHYs ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2006 {
2007  png_byte buf[9];
2008  png_uint_32 res_x, res_y;
2009  int unit_type;
2010 
2011  png_debug(1, "in png_handle_pHYs");
2012 
2013  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2014  png_chunk_error(png_ptr, "missing IHDR");
2015 
2016  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2017  {
2018  png_crc_finish(png_ptr, length);
2019  png_chunk_benign_error(png_ptr, "out of place");
2020  return;
2021  }
2022 
2023  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0)
2024  {
2025  png_crc_finish(png_ptr, length);
2026  png_chunk_benign_error(png_ptr, "duplicate");
2027  return;
2028  }
2029 
2030  if (length != 9)
2031  {
2032  png_crc_finish(png_ptr, length);
2033  png_chunk_benign_error(png_ptr, "invalid");
2034  return;
2035  }
2036 
2037  png_crc_read(png_ptr, buf, 9);
2038 
2039  if (png_crc_finish(png_ptr, 0) != 0)
2040  return;
2041 
2042  res_x = png_get_uint_32(buf);
2043  res_y = png_get_uint_32(buf + 4);
2044  unit_type = buf[8];
2045  png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type);
2046 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define png_debug(l, m)
Definition: pngdebug.h:146
PNG_IMPEXP void() png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)
Definition: pngset.c:489
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32() png_get_uint_32(png_const_bytep buf)
Definition: pngrutil.c:68
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
#define PNG_INFO_pHYs
Definition: png.h:880
void png_handle_sBIT ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1100 {
1101  unsigned int truelen, i;
1102  png_byte sample_depth;
1103  png_byte buf[4];
1104 
1105  png_debug(1, "in png_handle_sBIT");
1106 
1107  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1108  png_chunk_error(png_ptr, "missing IHDR");
1109 
1110  else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1111  {
1112  png_crc_finish(png_ptr, length);
1113  png_chunk_benign_error(png_ptr, "out of place");
1114  return;
1115  }
1116 
1117  if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0)
1118  {
1119  png_crc_finish(png_ptr, length);
1120  png_chunk_benign_error(png_ptr, "duplicate");
1121  return;
1122  }
1123 
1124  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1125  {
1126  truelen = 3;
1127  sample_depth = 8;
1128  }
1129 
1130  else
1131  {
1132  truelen = png_ptr->channels;
1133  sample_depth = png_ptr->bit_depth;
1134  }
1135 
1136  if (length != truelen || length > 4)
1137  {
1138  png_chunk_benign_error(png_ptr, "invalid");
1139  png_crc_finish(png_ptr, length);
1140  return;
1141  }
1142 
1143  buf[0] = buf[1] = buf[2] = buf[3] = sample_depth;
1144  png_crc_read(png_ptr, buf, truelen);
1145 
1146  if (png_crc_finish(png_ptr, 0) != 0)
1147  return;
1148 
1149  for (i=0; i<truelen; ++i)
1150  if (buf[i] == 0 || buf[i] > sample_depth)
1151  {
1152  png_chunk_benign_error(png_ptr, "invalid");
1153  return;
1154  }
1155 
1156  if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
1157  {
1158  png_ptr->sig_bit.red = buf[0];
1159  png_ptr->sig_bit.green = buf[1];
1160  png_ptr->sig_bit.blue = buf[2];
1161  png_ptr->sig_bit.alpha = buf[3];
1162  }
1163 
1164  else
1165  {
1166  png_ptr->sig_bit.gray = buf[0];
1167  png_ptr->sig_bit.red = buf[0];
1168  png_ptr->sig_bit.green = buf[0];
1169  png_ptr->sig_bit.blue = buf[0];
1170  png_ptr->sig_bit.alpha = buf[1];
1171  }
1172 
1173  png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
1174 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
int i
Definition: rw_test.cpp:37
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
png_byte color_type
Definition: pngstruct.h:252
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
PNG_IMPEXP void() png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr, png_const_color_8p sig_bit)
Definition: pngset.c:565
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_INFO_sBIT
Definition: png.h:874
png_byte bit_depth
Definition: pngstruct.h:253
png_byte channels
Definition: pngstruct.h:256
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
void png_handle_sCAL ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2228 {
2229  png_bytep buffer;
2230  png_size_t i;
2231  int state;
2232 
2233  png_debug(1, "in png_handle_sCAL");
2234 
2235  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2236  png_chunk_error(png_ptr, "missing IHDR");
2237 
2238  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2239  {
2240  png_crc_finish(png_ptr, length);
2241  png_chunk_benign_error(png_ptr, "out of place");
2242  return;
2243  }
2244 
2245  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0)
2246  {
2247  png_crc_finish(png_ptr, length);
2248  png_chunk_benign_error(png_ptr, "duplicate");
2249  return;
2250  }
2251 
2252  /* Need unit type, width, \0, height: minimum 4 bytes */
2253  else if (length < 4)
2254  {
2255  png_crc_finish(png_ptr, length);
2256  png_chunk_benign_error(png_ptr, "invalid");
2257  return;
2258  }
2259 
2260  png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)",
2261  length + 1);
2262 
2263  buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
2264 
2265  if (buffer == NULL)
2266  {
2267  png_chunk_benign_error(png_ptr, "out of memory");
2268  png_crc_finish(png_ptr, length);
2269  return;
2270  }
2271 
2272  png_crc_read(png_ptr, buffer, length);
2273  buffer[length] = 0; /* Null terminate the last string */
2274 
2275  if (png_crc_finish(png_ptr, 0) != 0)
2276  return;
2277 
2278  /* Validate the unit. */
2279  if (buffer[0] != 1 && buffer[0] != 2)
2280  {
2281  png_chunk_benign_error(png_ptr, "invalid unit");
2282  return;
2283  }
2284 
2285  /* Validate the ASCII numbers, need two ASCII numbers separated by
2286  * a '\0' and they need to fit exactly in the chunk data.
2287  */
2288  i = 1;
2289  state = 0;
2290 
2291  if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 ||
2292  i >= length || buffer[i++] != 0)
2293  png_chunk_benign_error(png_ptr, "bad width format");
2294 
2295  else if (PNG_FP_IS_POSITIVE(state) == 0)
2296  png_chunk_benign_error(png_ptr, "non-positive width");
2297 
2298  else
2299  {
2300  png_size_t heighti = i;
2301 
2302  state = 0;
2304  &state, &i) == 0 || i != length)
2305  png_chunk_benign_error(png_ptr, "bad height format");
2306 
2307  else if (PNG_FP_IS_POSITIVE(state) == 0)
2308  png_chunk_benign_error(png_ptr, "non-positive height");
2309 
2310  else
2311  /* This is the (only) success case. */
2312  png_set_sCAL_s(png_ptr, info_ptr, buffer[0],
2313  (png_charp)buffer+1, (png_charp)buffer+heighti);
2314  }
2315 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
PNG_IMPEXP void() png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr, int unit, png_const_charp swidth, png_const_charp sheight)
Definition: pngset.c:365
const char * png_const_charp
Definition: pngconf.h:611
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_FP_IS_POSITIVE(state)
Definition: pngpriv.h:1769
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
static char buffer[255]
Definition: propagator_simple.cpp:60
int png_check_fp_number(png_const_charp string, png_size_t size, int *statep, png_size_tp whereami)
Definition: png.c:2643
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
#define png_debug1(l, m, p1)
Definition: pngdebug.h:149
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
size_t png_size_t
Definition: pngconf.h:543
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
#define PNG_INFO_sCAL
Definition: png.h:887
void png_handle_sPLT ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1576 {
1577  png_bytep entry_start, buffer;
1578  png_sPLT_t new_palette;
1579  png_sPLT_entryp pp;
1580  png_uint_32 data_length;
1581  int entry_size, i;
1582  png_uint_32 skip = 0;
1583  png_uint_32 dl;
1584  png_size_t max_dl;
1585 
1586  png_debug(1, "in png_handle_sPLT");
1587 
1588 #ifdef PNG_USER_LIMITS_SUPPORTED
1589  if (png_ptr->user_chunk_cache_max != 0)
1590  {
1591  if (png_ptr->user_chunk_cache_max == 1)
1592  {
1593  png_crc_finish(png_ptr, length);
1594  return;
1595  }
1596 
1597  if (--png_ptr->user_chunk_cache_max == 1)
1598  {
1599  png_warning(png_ptr, "No space in chunk cache for sPLT");
1600  png_crc_finish(png_ptr, length);
1601  return;
1602  }
1603  }
1604 #endif
1605 
1606  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1607  png_chunk_error(png_ptr, "missing IHDR");
1608 
1609  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1610  {
1611  png_crc_finish(png_ptr, length);
1612  png_chunk_benign_error(png_ptr, "out of place");
1613  return;
1614  }
1615 
1616 #ifdef PNG_MAX_MALLOC_64K
1617  if (length > 65535U)
1618  {
1619  png_crc_finish(png_ptr, length);
1620  png_chunk_benign_error(png_ptr, "too large to fit in memory");
1621  return;
1622  }
1623 #endif
1624 
1625  buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/);
1626  if (buffer == NULL)
1627  {
1628  png_crc_finish(png_ptr, length);
1629  png_chunk_benign_error(png_ptr, "out of memory");
1630  return;
1631  }
1632 
1633 
1634  /* WARNING: this may break if size_t is less than 32 bits; it is assumed
1635  * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a
1636  * potential breakage point if the types in pngconf.h aren't exactly right.
1637  */
1638  png_crc_read(png_ptr, buffer, length);
1639 
1640  if (png_crc_finish(png_ptr, skip) != 0)
1641  return;
1642 
1643  buffer[length] = 0;
1644 
1645  for (entry_start = buffer; *entry_start; entry_start++)
1646  /* Empty loop to find end of name */ ;
1647 
1648  ++entry_start;
1649 
1650  /* A sample depth should follow the separator, and we should be on it */
1651  if (entry_start > buffer + length - 2)
1652  {
1653  png_warning(png_ptr, "malformed sPLT chunk");
1654  return;
1655  }
1656 
1657  new_palette.depth = *entry_start++;
1658  entry_size = (new_palette.depth == 8 ? 6 : 10);
1659  /* This must fit in a png_uint_32 because it is derived from the original
1660  * chunk data length.
1661  */
1662  data_length = length - (png_uint_32)(entry_start - buffer);
1663 
1664  /* Integrity-check the data length */
1665  if ((data_length % entry_size) != 0)
1666  {
1667  png_warning(png_ptr, "sPLT chunk has bad length");
1668  return;
1669  }
1670 
1671  dl = (png_int_32)(data_length / entry_size);
1672  max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry));
1673 
1674  if (dl > max_dl)
1675  {
1676  png_warning(png_ptr, "sPLT chunk too long");
1677  return;
1678  }
1679 
1680  new_palette.nentries = (png_int_32)(data_length / entry_size);
1681 
1682  new_palette.entries = (png_sPLT_entryp)png_malloc_warn(
1683  png_ptr, new_palette.nentries * (sizeof (png_sPLT_entry)));
1684 
1685  if (new_palette.entries == NULL)
1686  {
1687  png_warning(png_ptr, "sPLT chunk requires too much memory");
1688  return;
1689  }
1690 
1691 #ifdef PNG_POINTER_INDEXING_SUPPORTED
1692  for (i = 0; i < new_palette.nentries; i++)
1693  {
1694  pp = new_palette.entries + i;
1695 
1696  if (new_palette.depth == 8)
1697  {
1698  pp->red = *entry_start++;
1699  pp->green = *entry_start++;
1700  pp->blue = *entry_start++;
1701  pp->alpha = *entry_start++;
1702  }
1703 
1704  else
1705  {
1706  pp->red = png_get_uint_16(entry_start); entry_start += 2;
1707  pp->green = png_get_uint_16(entry_start); entry_start += 2;
1708  pp->blue = png_get_uint_16(entry_start); entry_start += 2;
1709  pp->alpha = png_get_uint_16(entry_start); entry_start += 2;
1710  }
1711 
1712  pp->frequency = png_get_uint_16(entry_start); entry_start += 2;
1713  }
1714 #else
1715  pp = new_palette.entries;
1716 
1717  for (i = 0; i < new_palette.nentries; i++)
1718  {
1719 
1720  if (new_palette.depth == 8)
1721  {
1722  pp[i].red = *entry_start++;
1723  pp[i].green = *entry_start++;
1724  pp[i].blue = *entry_start++;
1725  pp[i].alpha = *entry_start++;
1726  }
1727 
1728  else
1729  {
1730  pp[i].red = png_get_uint_16(entry_start); entry_start += 2;
1731  pp[i].green = png_get_uint_16(entry_start); entry_start += 2;
1732  pp[i].blue = png_get_uint_16(entry_start); entry_start += 2;
1733  pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2;
1734  }
1735 
1736  pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2;
1737  }
1738 #endif
1739 
1740  /* Discard all chunk data except the name and stash that */
1741  new_palette.name = (png_charp)buffer;
1742 
1743  png_set_sPLT(png_ptr, info_ptr, &new_palette, 1);
1744 
1745  png_free(png_ptr, new_palette.entries);
1746 }
png_int_32 nentries
Definition: png.h:679
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
struct png_sPLT_entry_struct png_sPLT_entry
png_uint_16 frequency
Definition: png.h:663
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
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
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
png_uint_16 alpha
Definition: png.h:662
png_uint_16() png_get_uint_16(png_const_bytep buf)
Definition: pngrutil.c:97
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
png_uint_16 blue
Definition: png.h:661
static char buffer[255]
Definition: propagator_simple.cpp:60
png_byte depth
Definition: png.h:677
#define PNG_SIZE_MAX
Definition: png.h:792
png_sPLT_entryp entries
Definition: png.h:678
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_sPLT_entry * png_sPLT_entryp
Definition: png.h:665
Definition: png.h:657
png_uint_16 green
Definition: png.h:660
PNG_IMPEXP void() png_set_sPLT(png_const_structrp png_ptr, png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)
Definition: pngset.c:977
png_uint_16 red
Definition: png.h:659
Definition: png.h:674
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
size_t png_size_t
Definition: pngconf.h:543
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
png_charp name
Definition: png.h:676
void png_handle_sRGB ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1252 {
1253  png_byte intent;
1254 
1255  png_debug(1, "in png_handle_sRGB");
1256 
1257  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1258  png_chunk_error(png_ptr, "missing IHDR");
1259 
1260  else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0)
1261  {
1262  png_crc_finish(png_ptr, length);
1263  png_chunk_benign_error(png_ptr, "out of place");
1264  return;
1265  }
1266 
1267  if (length != 1)
1268  {
1269  png_crc_finish(png_ptr, length);
1270  png_chunk_benign_error(png_ptr, "invalid");
1271  return;
1272  }
1273 
1274  png_crc_read(png_ptr, &intent, 1);
1275 
1276  if (png_crc_finish(png_ptr, 0) != 0)
1277  return;
1278 
1279  /* If a colorspace error has already been output skip this chunk */
1280  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0)
1281  return;
1282 
1283  /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect
1284  * this.
1285  */
1286  if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0)
1287  {
1288  png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID;
1289  png_colorspace_sync(png_ptr, info_ptr);
1290  png_chunk_benign_error(png_ptr, "too many profiles");
1291  return;
1292  }
1293 
1294  (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent);
1295  png_colorspace_sync(png_ptr, info_ptr);
1296 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
png_uint_16 intent
Definition: png.c:2177
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
png_uint_32 flags
Definition: pngstruct.h:180
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
int png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, int intent)
Definition: png.c:1833
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
void png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr)
Definition: png.c:1175
void png_handle_tEXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2367 {
2368  png_text text_info;
2369  png_bytep buffer;
2370  png_charp key;
2371  png_charp text;
2372  png_uint_32 skip = 0;
2373 
2374  png_debug(1, "in png_handle_tEXt");
2375 
2376 #ifdef PNG_USER_LIMITS_SUPPORTED
2377  if (png_ptr->user_chunk_cache_max != 0)
2378  {
2379  if (png_ptr->user_chunk_cache_max == 1)
2380  {
2381  png_crc_finish(png_ptr, length);
2382  return;
2383  }
2384 
2385  if (--png_ptr->user_chunk_cache_max == 1)
2386  {
2387  png_crc_finish(png_ptr, length);
2388  png_chunk_benign_error(png_ptr, "no space in chunk cache");
2389  return;
2390  }
2391  }
2392 #endif
2393 
2394  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2395  png_chunk_error(png_ptr, "missing IHDR");
2396 
2397  if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2398  png_ptr->mode |= PNG_AFTER_IDAT;
2399 
2400 #ifdef PNG_MAX_MALLOC_64K
2401  if (length > 65535U)
2402  {
2403  png_crc_finish(png_ptr, length);
2404  png_chunk_benign_error(png_ptr, "too large to fit in memory");
2405  return;
2406  }
2407 #endif
2408 
2409  buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/);
2410 
2411  if (buffer == NULL)
2412  {
2413  png_chunk_benign_error(png_ptr, "out of memory");
2414  return;
2415  }
2416 
2417  png_crc_read(png_ptr, buffer, length);
2418 
2419  if (png_crc_finish(png_ptr, skip) != 0)
2420  return;
2421 
2422  key = (png_charp)buffer;
2423  key[length] = 0;
2424 
2425  for (text = key; *text; text++)
2426  /* Empty loop to find end of key */ ;
2427 
2428  if (text != key + length)
2429  text++;
2430 
2432  text_info.key = key;
2433  text_info.lang = NULL;
2434  text_info.lang_key = NULL;
2435  text_info.itxt_length = 0;
2436  text_info.text = text;
2437  text_info.text_length = strlen(text);
2438 
2439  if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0)
2440  png_warning(png_ptr, "Insufficient memory to process text chunk");
2441 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
png_charp text
Definition: png.h:711
int compression
Definition: png.h:705
png_byte * png_bytep
Definition: pngconf.h:600
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
png_charp lang
Definition: png.h:715
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
png_charp key
Definition: png.h:710
#define PNG_HAVE_IHDR
Definition: png.h:785
#define PNG_TEXT_COMPRESSION_NONE
Definition: png.h:729
Definition: png.h:703
static char buffer[255]
Definition: propagator_simple.cpp:60
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_AFTER_IDAT
Definition: png.h:787
png_size_t itxt_length
Definition: png.h:714
png_uint_32 length
Definition: png.c:2173
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
int png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr, png_const_textp text_ptr, int num_text)
Definition: pngset.c:699
png_charp lang_key
Definition: png.h:717
png_size_t text_length
Definition: png.h:713
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
void png_handle_tIME ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2321 {
2322  png_byte buf[7];
2323  png_time mod_time;
2324 
2325  png_debug(1, "in png_handle_tIME");
2326 
2327  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2328  png_chunk_error(png_ptr, "missing IHDR");
2329 
2330  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0)
2331  {
2332  png_crc_finish(png_ptr, length);
2333  png_chunk_benign_error(png_ptr, "duplicate");
2334  return;
2335  }
2336 
2337  if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2338  png_ptr->mode |= PNG_AFTER_IDAT;
2339 
2340  if (length != 7)
2341  {
2342  png_crc_finish(png_ptr, length);
2343  png_chunk_benign_error(png_ptr, "invalid");
2344  return;
2345  }
2346 
2347  png_crc_read(png_ptr, buf, 7);
2348 
2349  if (png_crc_finish(png_ptr, 0) != 0)
2350  return;
2351 
2352  mod_time.second = buf[6];
2353  mod_time.minute = buf[5];
2354  mod_time.hour = buf[4];
2355  mod_time.day = buf[3];
2356  mod_time.month = buf[2];
2357  mod_time.year = png_get_uint_16(buf);
2358 
2359  png_set_tIME(png_ptr, info_ptr, &mod_time);
2360 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define PNG_INFO_tIME
Definition: png.h:882
#define png_debug(l, m)
Definition: pngdebug.h:146
PNG_IMPEXP void() png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr, png_const_timep mod_time)
Definition: pngset.c:892
png_uint_16() png_get_uint_16(png_const_bytep buf)
Definition: pngrutil.c:97
png_byte hour
Definition: png.h:746
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
png_byte second
Definition: png.h:748
png_byte minute
Definition: png.h:747
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
Definition: png.h:741
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_AFTER_IDAT
Definition: png.h:787
png_uint_32 length
Definition: png.c:2173
png_byte day
Definition: png.h:745
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
png_byte month
Definition: png.h:744
char buf[128]
Definition: rw_test.cpp:40
png_uint_16 year
Definition: png.h:743
void png_handle_tRNS ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
1752 {
1753  png_byte readbuf[PNG_MAX_PALETTE_LENGTH];
1754 
1755  png_debug(1, "in png_handle_tRNS");
1756 
1757  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
1758  png_chunk_error(png_ptr, "missing IHDR");
1759 
1760  else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
1761  {
1762  png_crc_finish(png_ptr, length);
1763  png_chunk_benign_error(png_ptr, "out of place");
1764  return;
1765  }
1766 
1767  else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)
1768  {
1769  png_crc_finish(png_ptr, length);
1770  png_chunk_benign_error(png_ptr, "duplicate");
1771  return;
1772  }
1773 
1774  if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY)
1775  {
1776  png_byte buf[2];
1777 
1778  if (length != 2)
1779  {
1780  png_crc_finish(png_ptr, length);
1781  png_chunk_benign_error(png_ptr, "invalid");
1782  return;
1783  }
1784 
1785  png_crc_read(png_ptr, buf, 2);
1786  png_ptr->num_trans = 1;
1787  png_ptr->trans_color.gray = png_get_uint_16(buf);
1788  }
1789 
1790  else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
1791  {
1792  png_byte buf[6];
1793 
1794  if (length != 6)
1795  {
1796  png_crc_finish(png_ptr, length);
1797  png_chunk_benign_error(png_ptr, "invalid");
1798  return;
1799  }
1800 
1801  png_crc_read(png_ptr, buf, length);
1802  png_ptr->num_trans = 1;
1803  png_ptr->trans_color.red = png_get_uint_16(buf);
1804  png_ptr->trans_color.green = png_get_uint_16(buf + 2);
1805  png_ptr->trans_color.blue = png_get_uint_16(buf + 4);
1806  }
1807 
1808  else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1809  {
1810  if ((png_ptr->mode & PNG_HAVE_PLTE) == 0)
1811  {
1812  /* TODO: is this actually an error in the ISO spec? */
1813  png_crc_finish(png_ptr, length);
1814  png_chunk_benign_error(png_ptr, "out of place");
1815  return;
1816  }
1817 
1818  if (length > png_ptr->num_palette || length > PNG_MAX_PALETTE_LENGTH ||
1819  length == 0)
1820  {
1821  png_crc_finish(png_ptr, length);
1822  png_chunk_benign_error(png_ptr, "invalid");
1823  return;
1824  }
1825 
1826  png_crc_read(png_ptr, readbuf, length);
1827  png_ptr->num_trans = (png_uint_16)length;
1828  }
1829 
1830  else
1831  {
1832  png_crc_finish(png_ptr, length);
1833  png_chunk_benign_error(png_ptr, "invalid with alpha channel");
1834  return;
1835  }
1836 
1837  if (png_crc_finish(png_ptr, 0) != 0)
1838  {
1839  png_ptr->num_trans = 0;
1840  return;
1841  }
1842 
1843  /* TODO: this is a horrible side effect in the palette case because the
1844  * png_struct ends up with a pointer to the tRNS buffer owned by the
1845  * png_info. Fix this.
1846  */
1847  png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans,
1848  &(png_ptr->trans_color));
1849 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
#define PNG_MAX_PALETTE_LENGTH
Definition: png.h:866
#define PNG_COLOR_TYPE_RGB
Definition: png.h:811
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
PNG_IMPEXP void() png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color)
Definition: pngset.c:917
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HAVE_PLTE
Definition: png.h:786
png_uint_16() png_get_uint_16(png_const_bytep buf)
Definition: pngrutil.c:97
png_byte color_type
Definition: pngstruct.h:252
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:809
png_uint_16 num_palette
Definition: pngstruct.h:239
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
png_uint_32 length
Definition: png.c:2173
#define PNG_INFO_tRNS
Definition: png.h:877
png_uint_32 valid
Definition: pnginfo.h:61
png_uint_32 mode
Definition: pngstruct.h:179
png_uint_16 num_trans
Definition: pngstruct.h:246
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
char buf[128]
Definition: rw_test.cpp:40
void png_handle_zTXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
2448 {
2449  png_const_charp errmsg = NULL;
2450  png_bytep buffer;
2451  png_uint_32 keyword_length;
2452 
2453  png_debug(1, "in png_handle_zTXt");
2454 
2455 #ifdef PNG_USER_LIMITS_SUPPORTED
2456  if (png_ptr->user_chunk_cache_max != 0)
2457  {
2458  if (png_ptr->user_chunk_cache_max == 1)
2459  {
2460  png_crc_finish(png_ptr, length);
2461  return;
2462  }
2463 
2464  if (--png_ptr->user_chunk_cache_max == 1)
2465  {
2466  png_crc_finish(png_ptr, length);
2467  png_chunk_benign_error(png_ptr, "no space in chunk cache");
2468  return;
2469  }
2470  }
2471 #endif
2472 
2473  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
2474  png_chunk_error(png_ptr, "missing IHDR");
2475 
2476  if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
2477  png_ptr->mode |= PNG_AFTER_IDAT;
2478 
2479  buffer = png_read_buffer(png_ptr, length, 2/*silent*/);
2480 
2481  if (buffer == NULL)
2482  {
2483  png_crc_finish(png_ptr, length);
2484  png_chunk_benign_error(png_ptr, "out of memory");
2485  return;
2486  }
2487 
2488  png_crc_read(png_ptr, buffer, length);
2489 
2490  if (png_crc_finish(png_ptr, 0) != 0)
2491  return;
2492 
2493  /* TODO: also check that the keyword contents match the spec! */
2494  for (keyword_length = 0;
2495  keyword_length < length && buffer[keyword_length] != 0;
2496  ++keyword_length)
2497  /* Empty loop to find end of name */ ;
2498 
2499  if (keyword_length > 79 || keyword_length < 1)
2500  errmsg = "bad keyword";
2501 
2502  /* zTXt must have some LZ data after the keyword, although it may expand to
2503  * zero bytes; we need a '\0' at the end of the keyword, the compression type
2504  * then the LZ data:
2505  */
2506  else if (keyword_length + 3 > length)
2507  errmsg = "truncated";
2508 
2509  else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE)
2510  errmsg = "unknown compression type";
2511 
2512  else
2513  {
2514  png_alloc_size_t uncompressed_length = PNG_SIZE_MAX;
2515 
2516  /* TODO: at present png_decompress_chunk imposes a single application
2517  * level memory limit, this should be split to different values for iCCP
2518  * and text chunks.
2519  */
2520  if (png_decompress_chunk(png_ptr, length, keyword_length+2,
2521  &uncompressed_length, 1/*terminate*/) == Z_STREAM_END)
2522  {
2523  png_text text;
2524 
2525  /* It worked; png_ptr->read_buffer now looks like a tEXt chunk except
2526  * for the extra compression type byte and the fact that it isn't
2527  * necessarily '\0' terminated.
2528  */
2529  buffer = png_ptr->read_buffer;
2530  buffer[uncompressed_length+(keyword_length+2)] = 0;
2531 
2533  text.key = (png_charp)buffer;
2534  text.text = (png_charp)(buffer + keyword_length+2);
2535  text.text_length = uncompressed_length;
2536  text.itxt_length = 0;
2537  text.lang = NULL;
2538  text.lang_key = NULL;
2539 
2540  if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0)
2541  errmsg = "insufficient memory";
2542  }
2543 
2544  else
2545  errmsg = png_ptr->zstream.msg;
2546  }
2547 
2548  if (errmsg != NULL)
2549  png_chunk_benign_error(png_ptr, errmsg);
2550 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
png_charp text
Definition: png.h:711
z_stream zstream
Definition: pngstruct.h:184
const char * png_const_charp
Definition: pngconf.h:611
int compression
Definition: png.h:705
static int png_decompress_chunk(png_structrp png_ptr, png_uint_32 chunklength, png_uint_32 prefix_size, png_alloc_size_t *newlength, int terminate)
Definition: pngrutil.c:564
png_byte * png_bytep
Definition: pngconf.h:600
char * png_charp
Definition: pngconf.h:610
#define png_debug(l, m)
Definition: pngdebug.h:146
char * msg
Definition: zlib.h:94
png_charp lang
Definition: png.h:715
png_size_t png_alloc_size_t
Definition: pngconf.h:578
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
png_charp key
Definition: png.h:710
#define PNG_TEXT_COMPRESSION_zTXt
Definition: png.h:730
#define PNG_HAVE_IHDR
Definition: png.h:785
Definition: png.h:703
static char buffer[255]
Definition: propagator_simple.cpp:60
#define PNG_SIZE_MAX
Definition: png.h:792
#define Z_STREAM_END
Definition: zlib.h:174
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_AFTER_IDAT
Definition: png.h:787
png_size_t itxt_length
Definition: png.h:714
png_uint_32 length
Definition: png.c:2173
#define PNG_COMPRESSION_TYPE_BASE
Definition: png.h:819
png_uint_32 mode
Definition: pngstruct.h:179
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
int png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr, png_const_textp text_ptr, int num_text)
Definition: pngset.c:699
png_charp lang_key
Definition: png.h:717
png_size_t text_length
Definition: png.h:713
static png_bytep png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn)
Definition: pngrutil.c:289
void png_check_chunk_name ( png_structrp  png_ptr,
png_uint_32  chunk_name 
)
2978 {
2979  int i;
2980 
2981  png_debug(1, "in png_check_chunk_name");
2982 
2983  for (i=1; i<=4; ++i)
2984  {
2985  int c = chunk_name & 0xff;
2986 
2987  if (c < 65 || c > 122 || (c > 90 && c < 97))
2988  png_chunk_error(png_ptr, "invalid chunk type");
2989 
2990  chunk_name >>= 8;
2991  }
2992 }
int i
Definition: rw_test.cpp:37
#define png_debug(l, m)
Definition: pngdebug.h:146
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
void png_handle_unknown ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length,
int  keep 
)
2767 {
2768  int handled = 0; /* the chunk was handled */
2769 
2770  png_debug(1, "in png_handle_unknown");
2771 
2772 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
2773  /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing
2774  * the bug which meant that setting a non-default behavior for a specific
2775  * chunk would be ignored (the default was always used unless a user
2776  * callback was installed).
2777  *
2778  * 'keep' is the value from the png_chunk_unknown_handling, the setting for
2779  * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it
2780  * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here.
2781  * This is just an optimization to avoid multiple calls to the lookup
2782  * function.
2783  */
2784 # ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
2785 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2786  keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name);
2787 # endif
2788 # endif
2789 
2790  /* One of the following methods will read the chunk or skip it (at least one
2791  * of these is always defined because this is the only way to switch on
2792  * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
2793  */
2794 # ifdef PNG_READ_USER_CHUNKS_SUPPORTED
2795  /* The user callback takes precedence over the chunk keep value, but the
2796  * keep value is still required to validate a save of a critical chunk.
2797  */
2798  if (png_ptr->read_user_chunk_fn != NULL)
2799  {
2800  if (png_cache_unknown_chunk(png_ptr, length) != 0)
2801  {
2802  /* Callback to user unknown chunk handler */
2803  int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr,
2804  &png_ptr->unknown_chunk);
2805 
2806  /* ret is:
2807  * negative: An error occured, png_chunk_error will be called.
2808  * zero: The chunk was not handled, the chunk will be discarded
2809  * unless png_set_keep_unknown_chunks has been used to set
2810  * a 'keep' behavior for this particular chunk, in which
2811  * case that will be used. A critical chunk will cause an
2812  * error at this point unless it is to be saved.
2813  * positive: The chunk was handled, libpng will ignore/discard it.
2814  */
2815  if (ret < 0)
2816  png_chunk_error(png_ptr, "error in user chunk");
2817 
2818  else if (ret == 0)
2819  {
2820  /* If the keep value is 'default' or 'never' override it, but
2821  * still error out on critical chunks unless the keep value is
2822  * 'always' While this is weird it is the behavior in 1.4.12.
2823  * A possible improvement would be to obey the value set for the
2824  * chunk, but this would be an API change that would probably
2825  * damage some applications.
2826  *
2827  * The png_app_warning below catches the case that matters, where
2828  * the application has not set specific save or ignore for this
2829  * chunk or global save or ignore.
2830  */
2831  if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
2832  {
2833 # ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
2834  if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
2835  {
2836  png_chunk_warning(png_ptr, "Saving unknown chunk:");
2837  png_app_warning(png_ptr,
2838  "forcing save of an unhandled chunk;"
2839  " please call png_set_keep_unknown_chunks");
2840  /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
2841  }
2842 # endif
2843  keep = PNG_HANDLE_CHUNK_IF_SAFE;
2844  }
2845  }
2846 
2847  else /* chunk was handled */
2848  {
2849  handled = 1;
2850  /* Critical chunks can be safely discarded at this point. */
2851  keep = PNG_HANDLE_CHUNK_NEVER;
2852  }
2853  }
2854 
2855  else
2856  keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */
2857  }
2858 
2859  else
2860  /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */
2861 # endif /* READ_USER_CHUNKS */
2862 
2863 # ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
2864  {
2865  /* keep is currently just the per-chunk setting, if there was no
2866  * setting change it to the global default now (not that this may
2867  * still be AS_DEFAULT) then obtain the cache of the chunk if required,
2868  * if not simply skip the chunk.
2869  */
2870  if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT)
2871  keep = png_ptr->unknown_default;
2872 
2873  if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2874  (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2875  PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2876  {
2877  if (png_cache_unknown_chunk(png_ptr, length) == 0)
2878  keep = PNG_HANDLE_CHUNK_NEVER;
2879  }
2880 
2881  else
2882  png_crc_finish(png_ptr, length);
2883  }
2884 # else
2885 # ifndef PNG_READ_USER_CHUNKS_SUPPORTED
2886 # error no method to support READ_UNKNOWN_CHUNKS
2887 # endif
2888 
2889  {
2890  /* If here there is no read callback pointer set and no support is
2891  * compiled in to just save the unknown chunks, so simply skip this
2892  * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then
2893  * the app has erroneously asked for unknown chunk saving when there
2894  * is no support.
2895  */
2896  if (keep > PNG_HANDLE_CHUNK_NEVER)
2897  png_app_error(png_ptr, "no unknown chunk support available");
2898 
2899  png_crc_finish(png_ptr, length);
2900  }
2901 # endif
2902 
2903 # ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
2904  /* Now store the chunk in the chunk list if appropriate, and if the limits
2905  * permit it.
2906  */
2907  if (keep == PNG_HANDLE_CHUNK_ALWAYS ||
2908  (keep == PNG_HANDLE_CHUNK_IF_SAFE &&
2909  PNG_CHUNK_ANCILLARY(png_ptr->chunk_name)))
2910  {
2911 # ifdef PNG_USER_LIMITS_SUPPORTED
2912  switch (png_ptr->user_chunk_cache_max)
2913  {
2914  case 2:
2915  png_ptr->user_chunk_cache_max = 1;
2916  png_chunk_benign_error(png_ptr, "no space in chunk cache");
2917  /* FALL THROUGH */
2918  case 1:
2919  /* NOTE: prior to 1.6.0 this case resulted in an unknown critical
2920  * chunk being skipped, now there will be a hard error below.
2921  */
2922  break;
2923 
2924  default: /* not at limit */
2925  --(png_ptr->user_chunk_cache_max);
2926  /* FALL THROUGH */
2927  case 0: /* no limit */
2928 # endif /* USER_LIMITS */
2929  /* Here when the limit isn't reached or when limits are compiled
2930  * out; store the chunk.
2931  */
2932  png_set_unknown_chunks(png_ptr, info_ptr,
2933  &png_ptr->unknown_chunk, 1);
2934  handled = 1;
2935 # ifdef PNG_USER_LIMITS_SUPPORTED
2936  break;
2937  }
2938 # endif
2939  }
2940 # else /* no store support: the chunk must be handled by the user callback */
2941  PNG_UNUSED(info_ptr)
2942 # endif
2943 
2944  /* Regardless of the error handling below the cached data (if any) can be
2945  * freed now. Notice that the data is not freed if there is a png_error, but
2946  * it will be freed by destroy_read_struct.
2947  */
2948  if (png_ptr->unknown_chunk.data != NULL)
2949  png_free(png_ptr, png_ptr->unknown_chunk.data);
2950  png_ptr->unknown_chunk.data = NULL;
2951 
2952 #else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */
2953  /* There is no support to read an unknown chunk, so just skip it. */
2954  png_crc_finish(png_ptr, length);
2955  PNG_UNUSED(info_ptr)
2956  PNG_UNUSED(keep)
2957 #endif /* !READ_UNKNOWN_CHUNKS */
2958 
2959  /* Check for unhandled critical chunks */
2960  if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name))
2961  png_chunk_error(png_ptr, "unhandled critical chunk");
2962 }
PNG_IMPEXP void() png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:514
static int png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length)
Definition: pngrutil.c:2706
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_CHUNK_ANCILLARY(c)
Definition: pngpriv.h:798
#define png_debug(l, m)
Definition: pngdebug.h:146
#define PNG_HANDLE_CHUNK_AS_DEFAULT
Definition: png.h:2497
#define PNG_CHUNK_CRITICAL(c)
Definition: pngpriv.h:799
PNG_IMPEXP void() png_set_unknown_chunks(png_const_structrp png_ptr, png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns)
Definition: pngset.c:1113
png_uint_32 chunk_name
Definition: pngstruct.h:221
PNG_IMPEXP void() png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:497
void png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:405
#define PNG_HANDLE_CHUNK_ALWAYS
Definition: png.h:2500
PNG_IMPEXP void() png_chunk_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:481
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_HANDLE_CHUNK_IF_SAFE
Definition: png.h:2499
png_uint_32 length
Definition: png.c:2173
PNG_IMPEXP void() png_free(png_const_structrp png_ptr, png_voidp ptr)
Definition: pngmem.c:229
void png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:392
int png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name)
Definition: png.c:919
#define PNG_HANDLE_CHUNK_NEVER
Definition: png.h:2498
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
void png_do_read_transformations ( png_structrp  png_ptr,
png_row_infop  row_info 
)
4702 {
4703  png_debug(1, "in png_do_read_transformations");
4704 
4705  if (png_ptr->row_buf == NULL)
4706  {
4707  /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
4708  * error is incredibly rare and incredibly easy to debug without this
4709  * information.
4710  */
4711  png_error(png_ptr, "NULL row buffer");
4712  }
4713 
4714  /* The following is debugging; prior to 1.5.4 the code was never compiled in;
4715  * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
4716  * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
4717  * all transformations, however in practice the ROW_INIT always gets done on
4718  * demand, if necessary.
4719  */
4720  if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
4721  (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
4722  {
4723  /* Application has failed to call either png_read_start_image() or
4724  * png_read_update_info() after setting transforms that expand pixels.
4725  * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
4726  */
4727  png_error(png_ptr, "Uninitialized row");
4728  }
4729 
4730 #ifdef PNG_READ_EXPAND_SUPPORTED
4731  if ((png_ptr->transformations & PNG_EXPAND) != 0)
4732  {
4733  if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
4734  {
4735  png_do_expand_palette(row_info, png_ptr->row_buf + 1,
4736  png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
4737  }
4738 
4739  else
4740  {
4741  if (png_ptr->num_trans != 0 &&
4742  (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
4743  png_do_expand(row_info, png_ptr->row_buf + 1,
4744  &(png_ptr->trans_color));
4745 
4746  else
4747  png_do_expand(row_info, png_ptr->row_buf + 1,
4748  NULL);
4749  }
4750  }
4751 #endif
4752 
4753 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4754  if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4755  (png_ptr->transformations & PNG_COMPOSE) == 0 &&
4756  (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4757  row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4758  png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4759  0 /* at_start == false, because SWAP_ALPHA happens later */);
4760 #endif
4761 
4762 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
4763  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
4764  {
4765  int rgb_error =
4766  png_do_rgb_to_gray(png_ptr, row_info,
4767  png_ptr->row_buf + 1);
4768 
4769  if (rgb_error != 0)
4770  {
4771  png_ptr->rgb_to_gray_status=1;
4772  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4774  png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4775 
4776  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
4778  png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
4779  }
4780  }
4781 #endif
4782 
4783 /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
4784  *
4785  * In most cases, the "simple transparency" should be done prior to doing
4786  * gray-to-RGB, or you will have to test 3x as many bytes to check if a
4787  * pixel is transparent. You would also need to make sure that the
4788  * transparency information is upgraded to RGB.
4789  *
4790  * To summarize, the current flow is:
4791  * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
4792  * with background "in place" if transparent,
4793  * convert to RGB if necessary
4794  * - Gray + alpha -> composite with gray background and remove alpha bytes,
4795  * convert to RGB if necessary
4796  *
4797  * To support RGB backgrounds for gray images we need:
4798  * - Gray + simple transparency -> convert to RGB + simple transparency,
4799  * compare 3 or 6 bytes and composite with
4800  * background "in place" if transparent
4801  * (3x compare/pixel compared to doing
4802  * composite with gray bkgrnd)
4803  * - Gray + alpha -> convert to RGB + alpha, composite with background and
4804  * remove alpha bytes (3x float
4805  * operations/pixel compared with composite
4806  * on gray background)
4807  *
4808  * Greg's change will do this. The reason it wasn't done before is for
4809  * performance, as this increases the per-pixel operations. If we would check
4810  * in advance if the background was gray or RGB, and position the gray-to-RGB
4811  * transform appropriately, then it would save a lot of work/time.
4812  */
4813 
4814 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4815  /* If gray -> RGB, do so now only if background is non-gray; else do later
4816  * for performance reasons
4817  */
4818  if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4819  (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
4820  png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4821 #endif
4822 
4823 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4824  defined(PNG_READ_ALPHA_MODE_SUPPORTED)
4825  if ((png_ptr->transformations & PNG_COMPOSE) != 0)
4826  png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
4827 #endif
4828 
4829 #ifdef PNG_READ_GAMMA_SUPPORTED
4830  if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
4832  /* Because RGB_TO_GRAY does the gamma transform. */
4833  (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
4834 #endif
4835 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
4837  /* Because PNG_COMPOSE does the gamma transform if there is something to
4838  * do (if there is an alpha channel or transparency.)
4839  */
4840  !((png_ptr->transformations & PNG_COMPOSE) &&
4841  ((png_ptr->num_trans != 0) ||
4842  (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
4843 #endif
4844  /* Because png_init_read_transformations transforms the palette, unless
4845  * RGB_TO_GRAY will do the transform.
4846  */
4847  (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
4848  png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
4849 #endif
4850 
4851 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
4852  if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
4853  (png_ptr->transformations & PNG_COMPOSE) != 0 &&
4854  (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
4855  row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
4856  png_do_strip_channel(row_info, png_ptr->row_buf + 1,
4857  0 /* at_start == false, because SWAP_ALPHA happens later */);
4858 #endif
4859 
4860 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
4861  if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
4862  (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
4863  png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
4864 #endif
4865 
4866 #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
4867  if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
4868  png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
4869 #endif
4870 
4871 #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
4872  /* There is no harm in doing both of these because only one has any effect,
4873  * by putting the 'scale' option first if the app asks for scale (either by
4874  * calling the API or in a TRANSFORM flag) this is what happens.
4875  */
4876  if ((png_ptr->transformations & PNG_16_TO_8) != 0)
4877  png_do_chop(row_info, png_ptr->row_buf + 1);
4878 #endif
4879 
4880 #ifdef PNG_READ_QUANTIZE_SUPPORTED
4881  if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
4882  {
4883  png_do_quantize(row_info, png_ptr->row_buf + 1,
4884  png_ptr->palette_lookup, png_ptr->quantize_index);
4885 
4886  if (row_info->rowbytes == 0)
4887  png_error(png_ptr, "png_do_quantize returned rowbytes=0");
4888  }
4889 #endif /* READ_QUANTIZE */
4890 
4891 #ifdef PNG_READ_EXPAND_16_SUPPORTED
4892  /* Do the expansion now, after all the arithmetic has been done. Notice
4893  * that previous transformations can handle the PNG_EXPAND_16 flag if this
4894  * is efficient (particularly true in the case of gamma correction, where
4895  * better accuracy results faster!)
4896  */
4897  if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
4898  png_do_expand_16(row_info, png_ptr->row_buf + 1);
4899 #endif
4900 
4901 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
4902  /* NOTE: moved here in 1.5.4 (from much later in this list.) */
4903  if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
4904  (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
4905  png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
4906 #endif
4907 
4908 #ifdef PNG_READ_INVERT_SUPPORTED
4909  if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
4910  png_do_invert(row_info, png_ptr->row_buf + 1);
4911 #endif
4912 
4913 #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
4914  if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
4915  png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
4916 #endif
4917 
4918 #ifdef PNG_READ_SHIFT_SUPPORTED
4919  if ((png_ptr->transformations & PNG_SHIFT) != 0)
4920  png_do_unshift(row_info, png_ptr->row_buf + 1,
4921  &(png_ptr->shift));
4922 #endif
4923 
4924 #ifdef PNG_READ_PACK_SUPPORTED
4925  if ((png_ptr->transformations & PNG_PACK) != 0)
4926  png_do_unpack(row_info, png_ptr->row_buf + 1);
4927 #endif
4928 
4929 #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
4930  /* Added at libpng-1.5.10 */
4931  if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
4932  png_ptr->num_palette_max >= 0)
4933  png_do_check_palette_indexes(png_ptr, row_info);
4934 #endif
4935 
4936 #ifdef PNG_READ_BGR_SUPPORTED
4937  if ((png_ptr->transformations & PNG_BGR) != 0)
4938  png_do_bgr(row_info, png_ptr->row_buf + 1);
4939 #endif
4940 
4941 #ifdef PNG_READ_PACKSWAP_SUPPORTED
4942  if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
4943  png_do_packswap(row_info, png_ptr->row_buf + 1);
4944 #endif
4945 
4946 #ifdef PNG_READ_FILLER_SUPPORTED
4947  if ((png_ptr->transformations & PNG_FILLER) != 0)
4948  png_do_read_filler(row_info, png_ptr->row_buf + 1,
4949  (png_uint_32)png_ptr->filler, png_ptr->flags);
4950 #endif
4951 
4952 #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
4953  if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
4954  png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
4955 #endif
4956 
4957 #ifdef PNG_READ_16BIT_SUPPORTED
4958 #ifdef PNG_READ_SWAP_SUPPORTED
4959  if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
4960  png_do_swap(row_info, png_ptr->row_buf + 1);
4961 #endif
4962 #endif
4963 
4964 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
4965  if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
4966  {
4967  if (png_ptr->read_user_transform_fn != NULL)
4968  (*(png_ptr->read_user_transform_fn)) /* User read transform function */
4969  (png_ptr, /* png_ptr */
4970  row_info, /* row_info: */
4971  /* png_uint_32 width; width of row */
4972  /* png_size_t rowbytes; number of bytes in row */
4973  /* png_byte color_type; color type of pixels */
4974  /* png_byte bit_depth; bit depth of samples */
4975  /* png_byte channels; number of channels (1-4) */
4976  /* png_byte pixel_depth; bits per pixel (depth*channels) */
4977  png_ptr->row_buf + 1); /* start of pixel data for row */
4978 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
4979  if (png_ptr->user_transform_depth != 0)
4980  row_info->bit_depth = png_ptr->user_transform_depth;
4981 
4982  if (png_ptr->user_transform_channels != 0)
4983  row_info->channels = png_ptr->user_transform_channels;
4984 #endif
4985  row_info->pixel_depth = (png_byte)(row_info->bit_depth *
4986  row_info->channels);
4987 
4988  row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
4989  }
4990 #endif
4991 }
#define PNG_GRAY_TO_RGB
Definition: pngpriv.h:574
#define PNG_STRIP_ALPHA
Definition: pngpriv.h:578
static void png_do_expand_16(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:4576
#define PNG_BGR
Definition: pngpriv.h:560
#define PNG_COMPOSE
Definition: pngpriv.h:567
static void png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2556
static void png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2845
#define PNG_INVERT_ALPHA
Definition: pngpriv.h:579
#define PNG_INVERT_MONO
Definition: pngpriv.h:565
#define PNG_SWAP_ALPHA
Definition: pngpriv.h:577
static void png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2459
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:813
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define PNG_SCALE_16_TO_8
Definition: pngpriv.h:587
static void png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2373
static void png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
Definition: pngrtran.c:3925
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
#define PNG_COLOR_TYPE_RGB_ALPHA
Definition: png.h:812
void png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info)
Definition: pngtrans.c:684
#define PNG_BACKGROUND_IS_GRAY
Definition: pngpriv.h:553
#define PNG_READ_ALPHA_MODE_SUPPORTED
Definition: pnglibconf.h:49
#define PNG_RGB_TO_GRAY_ERR
Definition: pngpriv.h:581
png_byte color_type
Definition: pngstruct.h:252
static void png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
Definition: pngrtran.c:3185
#define PNG_RGB_TO_GRAY_WARN
Definition: pngpriv.h:582
#define PNG_FLAG_ROW_INIT
Definition: pngpriv.h:607
static void png_do_expand(png_row_infop row_info, png_bytep row, png_const_color_16p trans_color)
Definition: pngrtran.c:4345
static void png_do_unshift(png_row_infop row_info, png_bytep row, png_const_color_8p sig_bits)
Definition: pngrtran.c:2234
#define PNG_FILLER
Definition: pngpriv.h:575
#define PNG_EXPAND_tRNS
Definition: pngpriv.h:586
png_byte color_type
Definition: png.h:898
png_colorp palette
Definition: pngstruct.h:238
png_uint_32 flags
Definition: pngstruct.h:180
png_bytep row_buf
Definition: pngstruct.h:225
#define PNG_SWAP_BYTES
Definition: pngpriv.h:564
#define PNG_PACKSWAP
Definition: pngpriv.h:576
#define PNG_FLAG_DETECT_UNINITIALIZED
Definition: pngpriv.h:615
void png_do_swap(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:318
void png_do_packswap(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:454
static int png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2984
static void png_do_unpack(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2136
png_byte pixel_depth
Definition: pngstruct.h:255
png_size_t rowbytes
Definition: png.h:897
void png_do_invert(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:261
png_byte channels
Definition: pngstruct.h:256
#define PNG_COLOR_MASK_ALPHA
Definition: png.h:806
#define PNG_READ_RGB_TO_GRAY_SUPPORTED
Definition: pnglibconf.h:70
static void png_do_read_filler(png_row_infop row_info, png_bytep row, png_uint_32 filler, png_uint_32 flags)
Definition: pngrtran.c:2658
#define PNG_ENCODE_ALPHA
Definition: pngpriv.h:584
png_uint_32 mode
Definition: pngstruct.h:179
static void png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
Definition: pngrtran.c:4126
#define PNG_SHIFT
Definition: pngpriv.h:563
static void png_do_quantize(png_row_infop row_info, png_bytep row, png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
Definition: pngrtran.c:4604
png_uint_16 num_trans
Definition: pngstruct.h:246
static void png_do_expand_palette(png_row_infop row_info, png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
Definition: pngrtran.c:4192
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define PNG_QUANTIZE
Definition: pngpriv.h:566
#define PNG_USER_TRANSFORM
Definition: pngpriv.h:580
#define PNG_RGB_TO_GRAY
Definition: pngpriv.h:583
#define PNG_GAMMA
Definition: pngpriv.h:573
void png_do_bgr(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:604
void png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
Definition: pngtrans.c:494
static void png_do_chop(png_row_infop row_info, png_bytep row)
Definition: pngrtran.c:2434
#define PNG_16_TO_8
Definition: pngpriv.h:570
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
#define PNG_READ_BACKGROUND_SUPPORTED
Definition: pnglibconf.h:51
png_uint_32 transformations
Definition: pngstruct.h:181
#define PNG_PACK
Definition: pngpriv.h:562
#define PNG_EXPAND_16
Definition: pngpriv.h:569
#define PNG_EXPAND
Definition: pngpriv.h:572
void png_do_write_transformations ( png_structrp  png_ptr,
png_row_infop  row_info 
)
501 {
502  png_debug(1, "in png_do_write_transformations");
503 
504  if (png_ptr == NULL)
505  return;
506 
507 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
508  if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
509  if (png_ptr->write_user_transform_fn != NULL)
510  (*(png_ptr->write_user_transform_fn)) /* User write transform
511  function */
512  (png_ptr, /* png_ptr */
513  row_info, /* row_info: */
514  /* png_uint_32 width; width of row */
515  /* png_size_t rowbytes; number of bytes in row */
516  /* png_byte color_type; color type of pixels */
517  /* png_byte bit_depth; bit depth of samples */
518  /* png_byte channels; number of channels (1-4) */
519  /* png_byte pixel_depth; bits per pixel (depth*channels) */
520  png_ptr->row_buf + 1); /* start of pixel data for row */
521 #endif
522 
523 #ifdef PNG_WRITE_FILLER_SUPPORTED
524  if ((png_ptr->transformations & PNG_FILLER) != 0)
525  png_do_strip_channel(row_info, png_ptr->row_buf + 1,
526  !(png_ptr->flags & PNG_FLAG_FILLER_AFTER));
527 #endif
528 
529 #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
530  if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
531  png_do_packswap(row_info, png_ptr->row_buf + 1);
532 #endif
533 
534 #ifdef PNG_WRITE_PACK_SUPPORTED
535  if ((png_ptr->transformations & PNG_PACK) != 0)
536  png_do_pack(row_info, png_ptr->row_buf + 1,
537  (png_uint_32)png_ptr->bit_depth);
538 #endif
539 
540 #ifdef PNG_WRITE_SWAP_SUPPORTED
541 # ifdef PNG_16BIT_SUPPORTED
542  if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
543  png_do_swap(row_info, png_ptr->row_buf + 1);
544 # endif
545 #endif
546 
547 #ifdef PNG_WRITE_SHIFT_SUPPORTED
548  if ((png_ptr->transformations & PNG_SHIFT) != 0)
549  png_do_shift(row_info, png_ptr->row_buf + 1,
550  &(png_ptr->shift));
551 #endif
552 
553 #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
554  if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
555  png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1);
556 #endif
557 
558 #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
559  if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
560  png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1);
561 #endif
562 
563 #ifdef PNG_WRITE_BGR_SUPPORTED
564  if ((png_ptr->transformations & PNG_BGR) != 0)
565  png_do_bgr(row_info, png_ptr->row_buf + 1);
566 #endif
567 
568 #ifdef PNG_WRITE_INVERT_SUPPORTED
569  if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
570  png_do_invert(row_info, png_ptr->row_buf + 1);
571 #endif
572 }
#define PNG_BGR
Definition: pngpriv.h:560
#define PNG_INVERT_ALPHA
Definition: pngpriv.h:579
#define PNG_INVERT_MONO
Definition: pngpriv.h:565
#define PNG_SWAP_ALPHA
Definition: pngpriv.h:577
#define png_debug(l, m)
Definition: pngdebug.h:146
static void png_do_shift(png_row_infop row_info, png_bytep row, png_const_color_8p bit_depth)
Definition: pngwtran.c:170
static void png_do_write_swap_alpha(png_row_infop row_info, png_bytep row)
Definition: pngwtran.c:309
static void png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth)
Definition: pngwtran.c:25
#define PNG_FILLER
Definition: pngpriv.h:575
png_uint_32 flags
Definition: pngstruct.h:180
png_bytep row_buf
Definition: pngstruct.h:225
#define PNG_SWAP_BYTES
Definition: pngpriv.h:564
#define PNG_PACKSWAP
Definition: pngpriv.h:576
void png_do_swap(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:318
void png_do_packswap(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:454
png_byte bit_depth
Definition: pngstruct.h:253
void png_do_invert(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:261
#define PNG_SHIFT
Definition: pngpriv.h:563
#define PNG_USER_TRANSFORM
Definition: pngpriv.h:580
#define PNG_FLAG_FILLER_AFTER
Definition: pngpriv.h:608
void png_do_bgr(png_row_infop row_info, png_bytep row)
Definition: pngtrans.c:604
void png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start)
Definition: pngtrans.c:494
png_uint_32 transformations
Definition: pngstruct.h:181
static void png_do_write_invert_alpha(png_row_infop row_info, png_bytep row)
Definition: pngwtran.c:403
#define PNG_PACK
Definition: pngpriv.h:562
void png_init_read_transformations ( png_structrp  png_ptr)
1276 {
1277  png_debug(1, "in png_init_read_transformations");
1278 
1279  /* This internal function is called from png_read_start_row in pngrutil.c
1280  * and it is called before the 'rowbytes' calculation is done, so the code
1281  * in here can change or update the transformations flags.
1282  *
1283  * First do updates that do not depend on the details of the PNG image data
1284  * being processed.
1285  */
1286 
1287 #ifdef PNG_READ_GAMMA_SUPPORTED
1288  /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
1289  * png_set_alpha_mode and this is another source for a default file gamma so
1290  * the test needs to be performed later - here. In addition prior to 1.5.4
1291  * the tests were repeated for the PALETTE color type here - this is no
1292  * longer necessary (and doesn't seem to have been necessary before.)
1293  */
1294  {
1295  /* The following temporary indicates if overall gamma correction is
1296  * required.
1297  */
1298  int gamma_correction = 0;
1299 
1300  if (png_ptr->colorspace.gamma != 0) /* has been set */
1301  {
1302  if (png_ptr->screen_gamma != 0) /* screen set too */
1303  gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma,
1304  png_ptr->screen_gamma);
1305 
1306  else
1307  /* Assume the output matches the input; a long time default behavior
1308  * of libpng, although the standard has nothing to say about this.
1309  */
1310  png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma);
1311  }
1312 
1313  else if (png_ptr->screen_gamma != 0)
1314  /* The converse - assume the file matches the screen, note that this
1315  * perhaps undesireable default can (from 1.5.4) be changed by calling
1316  * png_set_alpha_mode (even if the alpha handling mode isn't required
1317  * or isn't changed from the default.)
1318  */
1319  png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma);
1320 
1321  else /* neither are set */
1322  /* Just in case the following prevents any processing - file and screen
1323  * are both assumed to be linear and there is no way to introduce a
1324  * third gamma value other than png_set_background with 'UNIQUE', and,
1325  * prior to 1.5.4
1326  */
1327  png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1;
1328 
1329  /* We have a gamma value now. */
1330  png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
1331 
1332  /* Now turn the gamma transformation on or off as appropriate. Notice
1333  * that PNG_GAMMA just refers to the file->screen correction. Alpha
1334  * composition may independently cause gamma correction because it needs
1335  * linear data (e.g. if the file has a gAMA chunk but the screen gamma
1336  * hasn't been specified.) In any case this flag may get turned off in
1337  * the code immediately below if the transform can be handled outside the
1338  * row loop.
1339  */
1340  if (gamma_correction != 0)
1341  png_ptr->transformations |= PNG_GAMMA;
1342 
1343  else
1344  png_ptr->transformations &= ~PNG_GAMMA;
1345  }
1346 #endif
1347 
1348  /* Certain transformations have the effect of preventing other
1349  * transformations that happen afterward in png_do_read_transformations;
1350  * resolve the interdependencies here. From the code of
1351  * png_do_read_transformations the order is:
1352  *
1353  * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
1354  * 2) PNG_STRIP_ALPHA (if no compose)
1355  * 3) PNG_RGB_TO_GRAY
1356  * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
1357  * 5) PNG_COMPOSE
1358  * 6) PNG_GAMMA
1359  * 7) PNG_STRIP_ALPHA (if compose)
1360  * 8) PNG_ENCODE_ALPHA
1361  * 9) PNG_SCALE_16_TO_8
1362  * 10) PNG_16_TO_8
1363  * 11) PNG_QUANTIZE (converts to palette)
1364  * 12) PNG_EXPAND_16
1365  * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
1366  * 14) PNG_INVERT_MONO
1367  * 15) PNG_INVERT_ALPHA
1368  * 16) PNG_SHIFT
1369  * 17) PNG_PACK
1370  * 18) PNG_BGR
1371  * 19) PNG_PACKSWAP
1372  * 20) PNG_FILLER (includes PNG_ADD_ALPHA)
1373  * 21) PNG_SWAP_ALPHA
1374  * 22) PNG_SWAP_BYTES
1375  * 23) PNG_USER_TRANSFORM [must be last]
1376  */
1377 #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
1378  if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
1379  (png_ptr->transformations & PNG_COMPOSE) == 0)
1380  {
1381  /* Stripping the alpha channel happens immediately after the 'expand'
1382  * transformations, before all other transformation, so it cancels out
1383  * the alpha handling. It has the side effect negating the effect of
1384  * PNG_EXPAND_tRNS too:
1385  */
1387  PNG_EXPAND_tRNS);
1388  png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1389 
1390  /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
1391  * so transparency information would remain just so long as it wasn't
1392  * expanded. This produces unexpected API changes if the set of things
1393  * that do PNG_EXPAND_tRNS changes (perfectly possible given the
1394  * documentation - which says ask for what you want, accept what you
1395  * get.) This makes the behavior consistent from 1.5.4:
1396  */
1397  png_ptr->num_trans = 0;
1398  }
1399 #endif /* STRIP_ALPHA supported, no COMPOSE */
1400 
1401 #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
1402  /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
1403  * settings will have no effect.
1404  */
1405  if (png_gamma_significant(png_ptr->screen_gamma) == 0)
1406  {
1407  png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
1408  png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
1409  }
1410 #endif
1411 
1412 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
1413  /* Make sure the coefficients for the rgb to gray conversion are set
1414  * appropriately.
1415  */
1416  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1418 #endif
1419 
1420 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
1421 #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
1422  /* Detect gray background and attempt to enable optimization for
1423  * gray --> RGB case.
1424  *
1425  * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
1426  * RGB_ALPHA (in which case need_expand is superfluous anyway), the
1427  * background color might actually be gray yet not be flagged as such.
1428  * This is not a problem for the current code, which uses
1429  * PNG_BACKGROUND_IS_GRAY only to decide when to do the
1430  * png_do_gray_to_rgb() transformation.
1431  *
1432  * TODO: this code needs to be revised to avoid the complexity and
1433  * interdependencies. The color type of the background should be recorded in
1434  * png_set_background, along with the bit depth, then the code has a record
1435  * of exactly what color space the background is currently in.
1436  */
1437  if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
1438  {
1439  /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
1440  * the file was grayscale the background value is gray.
1441  */
1442  if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
1443  png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1444  }
1445 
1446  else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1447  {
1448  /* PNG_COMPOSE: png_set_background was called with need_expand false,
1449  * so the color is in the color space of the output or png_set_alpha_mode
1450  * was called and the color is black. Ignore RGB_TO_GRAY because that
1451  * happens before GRAY_TO_RGB.
1452  */
1453  if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
1454  {
1455  if (png_ptr->background.red == png_ptr->background.green &&
1456  png_ptr->background.red == png_ptr->background.blue)
1457  {
1458  png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
1459  png_ptr->background.gray = png_ptr->background.red;
1460  }
1461  }
1462  }
1463 #endif /* READ_EXPAND && READ_BACKGROUND */
1464 #endif /* READ_GRAY_TO_RGB */
1465 
1466  /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
1467  * can be performed directly on the palette, and some (such as rgb to gray)
1468  * can be optimized inside the palette. This is particularly true of the
1469  * composite (background and alpha) stuff, which can be pretty much all done
1470  * in the palette even if the result is expanded to RGB or gray afterward.
1471  *
1472  * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
1473  * earlier and the palette stuff is actually handled on the first row. This
1474  * leads to the reported bug that the palette returned by png_get_PLTE is not
1475  * updated.
1476  */
1477  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
1479 
1480  else
1482 
1483 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1484  defined(PNG_READ_EXPAND_16_SUPPORTED)
1485  if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
1486  (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1487  (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1488  png_ptr->bit_depth != 16)
1489  {
1490  /* TODO: fix this. Because the expand_16 operation is after the compose
1491  * handling the background color must be 8, not 16, bits deep, but the
1492  * application will supply a 16-bit value so reduce it here.
1493  *
1494  * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
1495  * present, so that case is ok (until do_expand_16 is moved.)
1496  *
1497  * NOTE: this discards the low 16 bits of the user supplied background
1498  * color, but until expand_16 works properly there is no choice!
1499  */
1500 # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
1501  CHOP(png_ptr->background.red);
1502  CHOP(png_ptr->background.green);
1503  CHOP(png_ptr->background.blue);
1504  CHOP(png_ptr->background.gray);
1505 # undef CHOP
1506  }
1507 #endif /* READ_BACKGROUND && READ_EXPAND_16 */
1508 
1509 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
1510  (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
1511  defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
1512  if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
1513  (png_ptr->transformations & PNG_COMPOSE) != 0 &&
1514  (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
1515  png_ptr->bit_depth == 16)
1516  {
1517  /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
1518  * component this will also happen after PNG_COMPOSE and so the background
1519  * color must be pre-expanded here.
1520  *
1521  * TODO: fix this too.
1522  */
1523  png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
1524  png_ptr->background.green =
1525  (png_uint_16)(png_ptr->background.green * 257);
1526  png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
1527  png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
1528  }
1529 #endif
1530 
1531  /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
1532  * background support (see the comments in scripts/pnglibconf.dfa), this
1533  * allows pre-multiplication of the alpha channel to be implemented as
1534  * compositing on black. This is probably sub-optimal and has been done in
1535  * 1.5.4 betas simply to enable external critique and testing (i.e. to
1536  * implement the new API quickly, without lots of internal changes.)
1537  */
1538 
1539 #ifdef PNG_READ_GAMMA_SUPPORTED
1540 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1541  /* Includes ALPHA_MODE */
1542  png_ptr->background_1 = png_ptr->background;
1543 # endif
1544 
1545  /* This needs to change - in the palette image case a whole set of tables are
1546  * built when it would be quicker to just calculate the correct value for
1547  * each palette entry directly. Also, the test is too tricky - why check
1548  * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
1549  * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
1550  * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
1551  * the gamma tables will not be built even if composition is required on a
1552  * gamma encoded value.
1553  *
1554  * In 1.5.4 this is addressed below by an additional check on the individual
1555  * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
1556  * tables.
1557  */
1558  if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
1559  ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
1560  (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1561  png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
1562  ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1563  (png_gamma_significant(png_ptr->colorspace.gamma) != 0 ||
1564  png_gamma_significant(png_ptr->screen_gamma) != 0
1565 # ifdef PNG_READ_BACKGROUND_SUPPORTED
1566  || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
1567  png_gamma_significant(png_ptr->background_gamma) != 0)
1568 # endif
1569  )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
1570  png_gamma_significant(png_ptr->screen_gamma) != 0))
1571  {
1572  png_build_gamma_table(png_ptr, png_ptr->bit_depth);
1573 
1574 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1575  if ((png_ptr->transformations & PNG_COMPOSE) != 0)
1576  {
1577  /* Issue a warning about this combination: because RGB_TO_GRAY is
1578  * optimized to do the gamma transform if present yet do_background has
1579  * to do the same thing if both options are set a
1580  * double-gamma-correction happens. This is true in all versions of
1581  * libpng to date.
1582  */
1583  if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
1584  png_warning(png_ptr,
1585  "libpng does not support gamma+background+rgb_to_gray");
1586 
1587  if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
1588  {
1589  /* We don't get to here unless there is a tRNS chunk with non-opaque
1590  * entries - see the checking code at the start of this function.
1591  */
1592  png_color back, back_1;
1593  png_colorp palette = png_ptr->palette;
1594  int num_palette = png_ptr->num_palette;
1595  int i;
1596  if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
1597  {
1598 
1599  back.red = png_ptr->gamma_table[png_ptr->background.red];
1600  back.green = png_ptr->gamma_table[png_ptr->background.green];
1601  back.blue = png_ptr->gamma_table[png_ptr->background.blue];
1602 
1603  back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
1604  back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
1605  back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
1606  }
1607  else
1608  {
1609  png_fixed_point g, gs;
1610 
1611  switch (png_ptr->background_gamma_type)
1612  {
1614  g = (png_ptr->screen_gamma);
1615  gs = PNG_FP_1;
1616  break;
1617 
1619  g = png_reciprocal(png_ptr->colorspace.gamma);
1620  gs = png_reciprocal2(png_ptr->colorspace.gamma,
1621  png_ptr->screen_gamma);
1622  break;
1623 
1625  g = png_reciprocal(png_ptr->background_gamma);
1626  gs = png_reciprocal2(png_ptr->background_gamma,
1627  png_ptr->screen_gamma);
1628  break;
1629  default:
1630  g = PNG_FP_1; /* back_1 */
1631  gs = PNG_FP_1; /* back */
1632  break;
1633  }
1634 
1635  if (png_gamma_significant(gs) != 0)
1636  {
1637  back.red = png_gamma_8bit_correct(png_ptr->background.red,
1638  gs);
1639  back.green = png_gamma_8bit_correct(png_ptr->background.green,
1640  gs);
1641  back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1642  gs);
1643  }
1644 
1645  else
1646  {
1647  back.red = (png_byte)png_ptr->background.red;
1648  back.green = (png_byte)png_ptr->background.green;
1649  back.blue = (png_byte)png_ptr->background.blue;
1650  }
1651 
1652  if (png_gamma_significant(g) != 0)
1653  {
1654  back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
1655  g);
1656  back_1.green = png_gamma_8bit_correct(
1657  png_ptr->background.green, g);
1658  back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
1659  g);
1660  }
1661 
1662  else
1663  {
1664  back_1.red = (png_byte)png_ptr->background.red;
1665  back_1.green = (png_byte)png_ptr->background.green;
1666  back_1.blue = (png_byte)png_ptr->background.blue;
1667  }
1668  }
1669 
1670  for (i = 0; i < num_palette; i++)
1671  {
1672  if (i < (int)png_ptr->num_trans &&
1673  png_ptr->trans_alpha[i] != 0xff)
1674  {
1675  if (png_ptr->trans_alpha[i] == 0)
1676  {
1677  palette[i] = back;
1678  }
1679  else /* if (png_ptr->trans_alpha[i] != 0xff) */
1680  {
1681  png_byte v, w;
1682 
1683  v = png_ptr->gamma_to_1[palette[i].red];
1684  png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
1685  palette[i].red = png_ptr->gamma_from_1[w];
1686 
1687  v = png_ptr->gamma_to_1[palette[i].green];
1688  png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
1689  palette[i].green = png_ptr->gamma_from_1[w];
1690 
1691  v = png_ptr->gamma_to_1[palette[i].blue];
1692  png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
1693  palette[i].blue = png_ptr->gamma_from_1[w];
1694  }
1695  }
1696  else
1697  {
1698  palette[i].red = png_ptr->gamma_table[palette[i].red];
1699  palette[i].green = png_ptr->gamma_table[palette[i].green];
1700  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1701  }
1702  }
1703 
1704  /* Prevent the transformations being done again.
1705  *
1706  * NOTE: this is highly dubious; it removes the transformations in
1707  * place. This seems inconsistent with the general treatment of the
1708  * transformations elsewhere.
1709  */
1710  png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
1711  } /* color_type == PNG_COLOR_TYPE_PALETTE */
1712 
1713  /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
1714  else /* color_type != PNG_COLOR_TYPE_PALETTE */
1715  {
1716  int gs_sig, g_sig;
1717  png_fixed_point g = PNG_FP_1; /* Correction to linear */
1718  png_fixed_point gs = PNG_FP_1; /* Correction to screen */
1719 
1720  switch (png_ptr->background_gamma_type)
1721  {
1723  g = png_ptr->screen_gamma;
1724  /* gs = PNG_FP_1; */
1725  break;
1726 
1728  g = png_reciprocal(png_ptr->colorspace.gamma);
1729  gs = png_reciprocal2(png_ptr->colorspace.gamma,
1730  png_ptr->screen_gamma);
1731  break;
1732 
1734  g = png_reciprocal(png_ptr->background_gamma);
1735  gs = png_reciprocal2(png_ptr->background_gamma,
1736  png_ptr->screen_gamma);
1737  break;
1738 
1739  default:
1740  png_error(png_ptr, "invalid background gamma type");
1741  }
1742 
1743  g_sig = png_gamma_significant(g);
1744  gs_sig = png_gamma_significant(gs);
1745 
1746  if (g_sig != 0)
1747  png_ptr->background_1.gray = png_gamma_correct(png_ptr,
1748  png_ptr->background.gray, g);
1749 
1750  if (gs_sig != 0)
1751  png_ptr->background.gray = png_gamma_correct(png_ptr,
1752  png_ptr->background.gray, gs);
1753 
1754  if ((png_ptr->background.red != png_ptr->background.green) ||
1755  (png_ptr->background.red != png_ptr->background.blue) ||
1756  (png_ptr->background.red != png_ptr->background.gray))
1757  {
1758  /* RGB or RGBA with color background */
1759  if (g_sig != 0)
1760  {
1761  png_ptr->background_1.red = png_gamma_correct(png_ptr,
1762  png_ptr->background.red, g);
1763 
1764  png_ptr->background_1.green = png_gamma_correct(png_ptr,
1765  png_ptr->background.green, g);
1766 
1767  png_ptr->background_1.blue = png_gamma_correct(png_ptr,
1768  png_ptr->background.blue, g);
1769  }
1770 
1771  if (gs_sig != 0)
1772  {
1773  png_ptr->background.red = png_gamma_correct(png_ptr,
1774  png_ptr->background.red, gs);
1775 
1776  png_ptr->background.green = png_gamma_correct(png_ptr,
1777  png_ptr->background.green, gs);
1778 
1779  png_ptr->background.blue = png_gamma_correct(png_ptr,
1780  png_ptr->background.blue, gs);
1781  }
1782  }
1783 
1784  else
1785  {
1786  /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
1787  png_ptr->background_1.red = png_ptr->background_1.green
1788  = png_ptr->background_1.blue = png_ptr->background_1.gray;
1789 
1790  png_ptr->background.red = png_ptr->background.green
1791  = png_ptr->background.blue = png_ptr->background.gray;
1792  }
1793 
1794  /* The background is now in screen gamma: */
1795  png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
1796  } /* color_type != PNG_COLOR_TYPE_PALETTE */
1797  }/* png_ptr->transformations & PNG_BACKGROUND */
1798 
1799  else
1800  /* Transformation does not include PNG_BACKGROUND */
1801 #endif /* READ_BACKGROUND */
1802  if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
1804  /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
1805  && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
1806  (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
1807 #endif
1808  )
1809  {
1810  png_colorp palette = png_ptr->palette;
1811  int num_palette = png_ptr->num_palette;
1812  int i;
1813 
1814  /* NOTE: there are other transformations that should probably be in
1815  * here too.
1816  */
1817  for (i = 0; i < num_palette; i++)
1818  {
1819  palette[i].red = png_ptr->gamma_table[palette[i].red];
1820  palette[i].green = png_ptr->gamma_table[palette[i].green];
1821  palette[i].blue = png_ptr->gamma_table[palette[i].blue];
1822  }
1823 
1824  /* Done the gamma correction. */
1825  png_ptr->transformations &= ~PNG_GAMMA;
1826  } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
1827  }
1828 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1829  else
1830 #endif
1831 #endif /* READ_GAMMA */
1832 
1833 #ifdef PNG_READ_BACKGROUND_SUPPORTED
1834  /* No GAMMA transformation (see the hanging else 4 lines above) */
1835  if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
1836  (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1837  {
1838  int i;
1839  int istop = (int)png_ptr->num_trans;
1840  png_color back;
1841  png_colorp palette = png_ptr->palette;
1842 
1843  back.red = (png_byte)png_ptr->background.red;
1844  back.green = (png_byte)png_ptr->background.green;
1845  back.blue = (png_byte)png_ptr->background.blue;
1846 
1847  for (i = 0; i < istop; i++)
1848  {
1849  if (png_ptr->trans_alpha[i] == 0)
1850  {
1851  palette[i] = back;
1852  }
1853 
1854  else if (png_ptr->trans_alpha[i] != 0xff)
1855  {
1856  /* The png_composite() macro is defined in png.h */
1857  png_composite(palette[i].red, palette[i].red,
1858  png_ptr->trans_alpha[i], back.red);
1859 
1860  png_composite(palette[i].green, palette[i].green,
1861  png_ptr->trans_alpha[i], back.green);
1862 
1863  png_composite(palette[i].blue, palette[i].blue,
1864  png_ptr->trans_alpha[i], back.blue);
1865  }
1866  }
1867 
1868  png_ptr->transformations &= ~PNG_COMPOSE;
1869  }
1870 #endif /* READ_BACKGROUND */
1871 
1872 #ifdef PNG_READ_SHIFT_SUPPORTED
1873  if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
1874  (png_ptr->transformations & PNG_EXPAND) == 0 &&
1875  (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
1876  {
1877  int i;
1878  int istop = png_ptr->num_palette;
1879  int shift = 8 - png_ptr->sig_bit.red;
1880 
1881  png_ptr->transformations &= ~PNG_SHIFT;
1882 
1883  /* significant bits can be in the range 1 to 7 for a meaninful result, if
1884  * the number of significant bits is 0 then no shift is done (this is an
1885  * error condition which is silently ignored.)
1886  */
1887  if (shift > 0 && shift < 8)
1888  for (i=0; i<istop; ++i)
1889  {
1890  int component = png_ptr->palette[i].red;
1891 
1892  component >>= shift;
1893  png_ptr->palette[i].red = (png_byte)component;
1894  }
1895 
1896  shift = 8 - png_ptr->sig_bit.green;
1897  if (shift > 0 && shift < 8)
1898  for (i=0; i<istop; ++i)
1899  {
1900  int component = png_ptr->palette[i].green;
1901 
1902  component >>= shift;
1903  png_ptr->palette[i].green = (png_byte)component;
1904  }
1905 
1906  shift = 8 - png_ptr->sig_bit.blue;
1907  if (shift > 0 && shift < 8)
1908  for (i=0; i<istop; ++i)
1909  {
1910  int component = png_ptr->palette[i].blue;
1911 
1912  component >>= shift;
1913  png_ptr->palette[i].blue = (png_byte)component;
1914  }
1915  }
1916 #endif /* READ_SHIFT */
1917 }
#define PNG_GRAY_TO_RGB
Definition: pngpriv.h:574
#define PNG_STRIP_ALPHA
Definition: pngpriv.h:578
#define PNG_BACKGROUND_GAMMA_UNIQUE
Definition: png.h:1448
#define CHOP(x)
void png_build_gamma_table(png_structrp png_ptr, int bit_depth)
Definition: png.c:4084
#define PNG_COMPOSE
Definition: pngpriv.h:567
static void png_init_rgb_transformations(png_structrp png_ptr)
Definition: pngrtran.c:1190
#define png_composite(composite, fg, alpha, bg)
Definition: png.h:2662
#define PNG_BACKGROUND_GAMMA_SCREEN
Definition: png.h:1446
static int png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
Definition: pngrtran.c:1076
int i
Definition: rw_test.cpp:37
static void png_init_palette_transformations(png_structrp png_ptr)
Definition: pngrtran.c:1101
#define PNG_BACKGROUND_EXPAND
Definition: pngpriv.h:568
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
#define PNG_SCALE_16_TO_8
Definition: pngpriv.h:587
png_byte red
Definition: png.h:621
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
#define PNG_BACKGROUND_IS_GRAY
Definition: pngpriv.h:553
#define PNG_BACKGROUND_GAMMA_FILE
Definition: png.h:1447
png_byte color_type
Definition: pngstruct.h:252
#define PNG_EXPAND_tRNS
Definition: pngpriv.h:586
void png_colorspace_set_rgb_coefficients(png_structrp png_ptr)
Definition: png.c:2398
png_colorp palette
Definition: pngstruct.h:238
#define PNG_COLOR_MASK_COLOR
Definition: png.h:805
png_uint_32 flags
Definition: pngstruct.h:180
png_uint_16 num_palette
Definition: pngstruct.h:239
Definition: png.h:619
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
png_byte bit_depth
Definition: pngstruct.h:253
#define PNG_READ_RGB_TO_GRAY_SUPPORTED
Definition: pnglibconf.h:70
#define PNG_ENCODE_ALPHA
Definition: pngpriv.h:584
png_uint_32 mode
Definition: pngstruct.h:179
png_byte png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3752
#define PNG_SHIFT
Definition: pngpriv.h:563
png_uint_16 num_trans
Definition: pngstruct.h:246
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
#define PNG_RGB_TO_GRAY
Definition: pngpriv.h:583
#define PNG_GAMMA
Definition: pngpriv.h:573
#define PNG_16_TO_8
Definition: pngpriv.h:570
png_uint_32 transformations
Definition: pngstruct.h:181
png_uint_16 png_gamma_correct(png_structrp png_ptr, unsigned int value, png_fixed_point gamma_val)
Definition: png.c:3835
int png_gamma_significant(png_fixed_point gamma_val)
Definition: png.c:3400
png_byte green
Definition: png.h:622
#define PNG_EXPAND_16
Definition: pngpriv.h:569
png_byte blue
Definition: png.h:623
#define PNG_EXPAND
Definition: pngpriv.h:572
#define PNG_FLAG_OPTIMIZE_ALPHA
Definition: pngpriv.h:614
void png_push_read_chunk ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
193 {
194  png_uint_32 chunk_name;
195 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
196  int keep; /* unknown handling method */
197 #endif
198 
199  /* First we make sure we have enough data for the 4-byte chunk name
200  * and the 4-byte chunk length before proceeding with decoding the
201  * chunk data. To fully decode each of these chunks, we also make
202  * sure we have enough data in the buffer for the 4-byte CRC at the
203  * end of every chunk (except IDAT, which is handled separately).
204  */
205  if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
206  {
207  png_byte chunk_length[4];
208  png_byte chunk_tag[4];
209 
211  png_push_fill_buffer(png_ptr, chunk_length, 4);
212  png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
213  png_reset_crc(png_ptr);
214  png_crc_read(png_ptr, chunk_tag, 4);
215  png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
216  png_check_chunk_name(png_ptr, png_ptr->chunk_name);
217  png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
218  }
219 
220  chunk_name = png_ptr->chunk_name;
221 
222  if (chunk_name == png_IDAT)
223  {
224  if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
225  png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
226 
227  /* If we reach an IDAT chunk, this means we have read all of the
228  * header chunks, and we can start reading the image (or if this
229  * is called after the image has been read - we have an error).
230  */
231  if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
232  png_error(png_ptr, "Missing IHDR before IDAT");
233 
234  else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
235  (png_ptr->mode & PNG_HAVE_PLTE) == 0)
236  png_error(png_ptr, "Missing PLTE before IDAT");
237 
238  png_ptr->mode |= PNG_HAVE_IDAT;
239  png_ptr->process_mode = PNG_READ_IDAT_MODE;
240 
241  if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0)
242  if (png_ptr->push_length == 0)
243  return;
244 
245  if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
246  png_benign_error(png_ptr, "Too many IDATs found");
247  }
248 
249  if (chunk_name == png_IHDR)
250  {
251  if (png_ptr->push_length != 13)
252  png_error(png_ptr, "Invalid IHDR length");
253 
255  png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length);
256  }
257 
258  else if (chunk_name == png_IEND)
259  {
261  png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length);
262 
263  png_ptr->process_mode = PNG_READ_DONE_MODE;
264  png_push_have_end(png_ptr, info_ptr);
265  }
266 
267 #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
268  else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
269  {
271  png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep);
272 
273  if (chunk_name == png_PLTE)
274  png_ptr->mode |= PNG_HAVE_PLTE;
275  }
276 #endif
277 
278  else if (chunk_name == png_PLTE)
279  {
281  png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length);
282  }
283 
284  else if (chunk_name == png_IDAT)
285  {
286  png_ptr->idat_size = png_ptr->push_length;
287  png_ptr->process_mode = PNG_READ_IDAT_MODE;
288  png_push_have_info(png_ptr, info_ptr);
289  png_ptr->zstream.avail_out =
290  (uInt) PNG_ROWBYTES(png_ptr->pixel_depth,
291  png_ptr->iwidth) + 1;
292  png_ptr->zstream.next_out = png_ptr->row_buf;
293  return;
294  }
295 
296 #ifdef PNG_READ_gAMA_SUPPORTED
297  else if (png_ptr->chunk_name == png_gAMA)
298  {
300  png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length);
301  }
302 
303 #endif
304 #ifdef PNG_READ_sBIT_SUPPORTED
305  else if (png_ptr->chunk_name == png_sBIT)
306  {
308  png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length);
309  }
310 
311 #endif
312 #ifdef PNG_READ_cHRM_SUPPORTED
313  else if (png_ptr->chunk_name == png_cHRM)
314  {
316  png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length);
317  }
318 
319 #endif
320 #ifdef PNG_READ_sRGB_SUPPORTED
321  else if (chunk_name == png_sRGB)
322  {
324  png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length);
325  }
326 
327 #endif
328 #ifdef PNG_READ_iCCP_SUPPORTED
329  else if (png_ptr->chunk_name == png_iCCP)
330  {
332  png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length);
333  }
334 
335 #endif
336 #ifdef PNG_READ_sPLT_SUPPORTED
337  else if (chunk_name == png_sPLT)
338  {
340  png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length);
341  }
342 
343 #endif
344 #ifdef PNG_READ_tRNS_SUPPORTED
345  else if (chunk_name == png_tRNS)
346  {
348  png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length);
349  }
350 
351 #endif
352 #ifdef PNG_READ_bKGD_SUPPORTED
353  else if (chunk_name == png_bKGD)
354  {
356  png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length);
357  }
358 
359 #endif
360 #ifdef PNG_READ_hIST_SUPPORTED
361  else if (chunk_name == png_hIST)
362  {
364  png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length);
365  }
366 
367 #endif
368 #ifdef PNG_READ_pHYs_SUPPORTED
369  else if (chunk_name == png_pHYs)
370  {
372  png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length);
373  }
374 
375 #endif
376 #ifdef PNG_READ_oFFs_SUPPORTED
377  else if (chunk_name == png_oFFs)
378  {
380  png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length);
381  }
382 #endif
383 
384 #ifdef PNG_READ_pCAL_SUPPORTED
385  else if (chunk_name == png_pCAL)
386  {
388  png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length);
389  }
390 
391 #endif
392 #ifdef PNG_READ_sCAL_SUPPORTED
393  else if (chunk_name == png_sCAL)
394  {
396  png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length);
397  }
398 
399 #endif
400 #ifdef PNG_READ_tIME_SUPPORTED
401  else if (chunk_name == png_tIME)
402  {
404  png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length);
405  }
406 
407 #endif
408 #ifdef PNG_READ_tEXt_SUPPORTED
409  else if (chunk_name == png_tEXt)
410  {
412  png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length);
413  }
414 
415 #endif
416 #ifdef PNG_READ_zTXt_SUPPORTED
417  else if (chunk_name == png_zTXt)
418  {
420  png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length);
421  }
422 
423 #endif
424 #ifdef PNG_READ_iTXt_SUPPORTED
425  else if (chunk_name == png_iTXt)
426  {
428  png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length);
429  }
430 #endif
431 
432  else
433  {
435  png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length,
437  }
438 
439  png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
440 }
void png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Definition: pngpread.c:506
void png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1179
void png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length, int keep)
Definition: pngrutil.c:2765
#define png_PLTE
Definition: pngpriv.h:755
#define PNG_READ_DONE_MODE
Definition: pngpread.c:25
#define png_sCAL
Definition: pngpriv.h:770
#define png_iTXt
Definition: pngpriv.h:765
#define png_bKGD
Definition: pngpriv.h:756
void png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2447
#define png_tRNS
Definition: pngpriv.h:776
#define png_sRGB
Definition: pngpriv.h:772
#define PNG_HAVE_CHUNK_HEADER
Definition: pngpriv.h:550
void png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2098
void png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1954
#define png_iCCP
Definition: pngpriv.h:764
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:810
void png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2005
#define png_IEND
Definition: pngpriv.h:753
void png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1061
void png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1854
#define png_sPLT
Definition: pngpriv.h:771
#define PNG_HAVE_PLTE
Definition: png.h:786
void png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1251
void png_reset_crc(png_structrp png_ptr)
Definition: png.c:102
void png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2320
void png_check_chunk_name(png_structrp png_ptr, png_uint_32 chunk_name)
Definition: pngrutil.c:2977
#define PNG_PUSH_SAVE_BUFFER_IF_FULL
Definition: pngpread.c:29
void png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2366
#define PNG_HANDLE_CHUNK_AS_DEFAULT
Definition: png.h:2497
void png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2051
#define PNG_READ_IDAT_MODE
Definition: pngpread.c:21
#define PNG_HAVE_IDAT
Definition: pngpriv.h:544
#define PNG_HAVE_IHDR
Definition: png.h:785
#define png_IHDR
Definition: pngpriv.h:754
#define png_tEXt
Definition: pngpriv.h:774
#define PNG_CHUNK_FROM_STRING(s)
Definition: pngpriv.h:782
void png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1751
#define PNG_HAVE_CHUNK_AFTER_IDAT
Definition: pngpriv.h:555
void png_push_have_info(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngpread.c:1109
void png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1099
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N)
Definition: pngpread.c:32
#define png_IDAT
Definition: pngpriv.h:752
#define PNG_AFTER_IDAT
Definition: png.h:787
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
#define png_oFFs
Definition: pngpriv.h:766
#define png_gAMA
Definition: pngpriv.h:759
#define png_pCAL
Definition: pngpriv.h:767
PNG_IMPEXP png_uint_32() png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:22
#define png_cHRM
Definition: pngpriv.h:757
png_uint_32 mode
Definition: pngstruct.h:179
void png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1041
void png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2556
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
void png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:793
int png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name)
Definition: png.c:919
#define png_sBIT
Definition: pngpriv.h:769
void png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1574
void png_push_have_end(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngpread.c:1116
#define png_hIST
Definition: pngpriv.h:763
void png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:2227
#define png_pHYs
Definition: pngpriv.h:768
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
void png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:1301
#define png_tIME
Definition: pngpriv.h:775
void png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length)
Definition: pngrutil.c:867
#define png_zTXt
Definition: pngpriv.h:777
unsigned int uInt
Definition: zconf.h:370
PNG_IMPEXP void() png_benign_error(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:362
void png_push_read_sig ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
160 {
161  png_size_t num_checked = png_ptr->sig_bytes, /* SAFE, does not exceed 8 */
162  num_to_check = 8 - num_checked;
163 
164  if (png_ptr->buffer_size < num_to_check)
165  {
166  num_to_check = png_ptr->buffer_size;
167  }
168 
169  png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
170  num_to_check);
171  png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check);
172 
173  if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check))
174  {
175  if (num_checked < 4 &&
176  png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4))
177  png_error(png_ptr, "Not a PNG file");
178 
179  else
180  png_error(png_ptr, "PNG file corrupted by ASCII conversion");
181  }
182  else
183  {
184  if (png_ptr->sig_bytes >= 8)
185  {
186  png_ptr->process_mode = PNG_READ_CHUNK_MODE;
187  }
188  }
189 }
void png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Definition: pngpread.c:506
#define PNG_READ_CHUNK_MODE
Definition: pngpread.c:20
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
size_t png_size_t
Definition: pngconf.h:543
int png_sig_cmp(png_const_bytep sig, png_size_t start, png_size_t num_to_check)
Definition: png.c:49
void png_push_check_crc ( png_structrp  png_ptr)
void png_push_crc_skip ( png_structrp  png_ptr,
png_uint_32  length 
)
444 {
445  png_ptr->process_mode = PNG_SKIP_MODE;
446  png_ptr->skip_length = skip;
447 }
#define PNG_SKIP_MODE
Definition: pngpread.c:22
void png_push_crc_finish ( png_structrp  png_ptr)
451 {
452  if (png_ptr->skip_length != 0 && png_ptr->save_buffer_size != 0)
453  {
454  png_size_t save_size = png_ptr->save_buffer_size;
455  png_uint_32 skip_length = png_ptr->skip_length;
456 
457  /* We want the smaller of 'skip_length' and 'save_buffer_size', but
458  * they are of different types and we don't know which variable has the
459  * fewest bits. Carefully select the smaller and cast it to the type of
460  * the larger - this cannot overflow. Do not cast in the following test
461  * - it will break on either 16 or 64 bit platforms.
462  */
463  if (skip_length < save_size)
464  save_size = (png_size_t)skip_length;
465 
466  else
467  skip_length = (png_uint_32)save_size;
468 
469  png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
470 
471  png_ptr->skip_length -= skip_length;
472  png_ptr->buffer_size -= save_size;
473  png_ptr->save_buffer_size -= save_size;
474  png_ptr->save_buffer_ptr += save_size;
475  }
476  if (png_ptr->skip_length != 0 && png_ptr->current_buffer_size != 0)
477  {
478  png_size_t save_size = png_ptr->current_buffer_size;
479  png_uint_32 skip_length = png_ptr->skip_length;
480 
481  /* We want the smaller of 'skip_length' and 'current_buffer_size', here,
482  * the same problem exists as above and the same solution.
483  */
484  if (skip_length < save_size)
485  save_size = (png_size_t)skip_length;
486 
487  else
488  skip_length = (png_uint_32)save_size;
489 
490  png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
491 
492  png_ptr->skip_length -= skip_length;
493  png_ptr->buffer_size -= save_size;
494  png_ptr->current_buffer_size -= save_size;
495  png_ptr->current_buffer_ptr += save_size;
496  }
497  if (png_ptr->skip_length == 0)
498  {
500  png_crc_finish(png_ptr, 0);
501  png_ptr->process_mode = PNG_READ_CHUNK_MODE;
502  }
503 }
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N)
Definition: pngpread.c:32
void png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
Definition: png.c:114
#define PNG_READ_CHUNK_MODE
Definition: pngpread.c:20
size_t png_size_t
Definition: pngconf.h:543
void png_push_save_buffer ( png_structrp  png_ptr)
550 {
551  if (png_ptr->save_buffer_size != 0)
552  {
553  if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
554  {
555  png_size_t i, istop;
556  png_bytep sp;
557  png_bytep dp;
558 
559  istop = png_ptr->save_buffer_size;
560  for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
561  i < istop; i++, sp++, dp++)
562  {
563  *dp = *sp;
564  }
565  }
566  }
567  if (png_ptr->save_buffer_size + png_ptr->current_buffer_size >
568  png_ptr->save_buffer_max)
569  {
570  png_size_t new_max;
571  png_bytep old_buffer;
572 
573  if (png_ptr->save_buffer_size > PNG_SIZE_MAX -
574  (png_ptr->current_buffer_size + 256))
575  {
576  png_error(png_ptr, "Potential overflow of save_buffer");
577  }
578 
579  new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
580  old_buffer = png_ptr->save_buffer;
581  png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr,
582  (png_size_t)new_max);
583 
584  if (png_ptr->save_buffer == NULL)
585  {
586  png_free(png_ptr, old_buffer);
587  old_buffer = NULL;
588  png_error(png_ptr, "Insufficient memory for save_buffer");
589  }
590 
591  memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
592  png_free(png_ptr, old_buffer);
593  old_buffer = NULL;
594  png_ptr->save_buffer_max = new_max;
595  }
596  if (png_ptr->current_buffer_size)
597  {
598  memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
599  png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
600  png_ptr->save_buffer_size += png_ptr->current_buffer_size;
601  png_ptr->current_buffer_size = 0;
602  }
603  png_ptr->save_buffer_ptr = png_ptr->save_buffer;
604  png_ptr->buffer_size = 0;
605 }
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
PNG_IMPEXP png_voidp() png_malloc_warn(png_const_structrp png_ptr, png_alloc_size_t size)
Definition: pngmem.c:210
#define PNG_SIZE_MAX
Definition: png.h:792
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
size_t png_size_t
Definition: pngconf.h:543
void png_push_restore_buffer ( png_structrp  png_ptr,
png_bytep  buffer,
png_size_t  buffer_length 
)
610 {
611  png_ptr->current_buffer = buffer;
612  png_ptr->current_buffer_size = buffer_length;
613  png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size;
614  png_ptr->current_buffer_ptr = png_ptr->current_buffer;
615 }
static char buffer[255]
Definition: propagator_simple.cpp:60
void png_push_read_IDAT ( png_structrp  png_ptr)
619 {
620  if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0)
621  {
622  png_byte chunk_length[4];
623  png_byte chunk_tag[4];
624 
625  /* TODO: this code can be commoned up with the same code in push_read */
627  png_push_fill_buffer(png_ptr, chunk_length, 4);
628  png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length);
629  png_reset_crc(png_ptr);
630  png_crc_read(png_ptr, chunk_tag, 4);
631  png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag);
632  png_ptr->mode |= PNG_HAVE_CHUNK_HEADER;
633 
634  if (png_ptr->chunk_name != png_IDAT)
635  {
636  png_ptr->process_mode = PNG_READ_CHUNK_MODE;
637 
638  if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0)
639  png_error(png_ptr, "Not enough compressed data");
640 
641  return;
642  }
643 
644  png_ptr->idat_size = png_ptr->push_length;
645  }
646 
647  if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0)
648  {
649  png_size_t save_size = png_ptr->save_buffer_size;
650  png_uint_32 idat_size = png_ptr->idat_size;
651 
652  /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
653  * are of different types and we don't know which variable has the fewest
654  * bits. Carefully select the smaller and cast it to the type of the
655  * larger - this cannot overflow. Do not cast in the following test - it
656  * will break on either 16 or 64 bit platforms.
657  */
658  if (idat_size < save_size)
659  save_size = (png_size_t)idat_size;
660 
661  else
662  idat_size = (png_uint_32)save_size;
663 
664  png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size);
665 
666  png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size);
667 
668  png_ptr->idat_size -= idat_size;
669  png_ptr->buffer_size -= save_size;
670  png_ptr->save_buffer_size -= save_size;
671  png_ptr->save_buffer_ptr += save_size;
672  }
673 
674  if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0)
675  {
676  png_size_t save_size = png_ptr->current_buffer_size;
677  png_uint_32 idat_size = png_ptr->idat_size;
678 
679  /* We want the smaller of 'idat_size' and 'current_buffer_size', but they
680  * are of different types and we don't know which variable has the fewest
681  * bits. Carefully select the smaller and cast it to the type of the
682  * larger - this cannot overflow.
683  */
684  if (idat_size < save_size)
685  save_size = (png_size_t)idat_size;
686 
687  else
688  idat_size = (png_uint_32)save_size;
689 
690  png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size);
691 
692  png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size);
693 
694  png_ptr->idat_size -= idat_size;
695  png_ptr->buffer_size -= save_size;
696  png_ptr->current_buffer_size -= save_size;
697  png_ptr->current_buffer_ptr += save_size;
698  }
699  if (png_ptr->idat_size == 0)
700  {
702  png_crc_finish(png_ptr, 0);
703  png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER;
704  png_ptr->mode |= PNG_AFTER_IDAT;
705  png_ptr->zowner = 0;
706  }
707 }
void png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, png_size_t length)
Definition: pngpread.c:506
#define PNG_HAVE_CHUNK_HEADER
Definition: pngpriv.h:550
void png_reset_crc(png_structrp png_ptr)
Definition: png.c:102
int png_crc_finish(png_structrp png_ptr, png_uint_32 skip)
Definition: pngrutil.c:202
#define PNG_CHUNK_FROM_STRING(s)
Definition: pngpriv.h:782
#define PNG_PUSH_SAVE_BUFFER_IF_LT(N)
Definition: pngpread.c:32
#define png_IDAT
Definition: pngpriv.h:752
void png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer, png_size_t buffer_length)
Definition: pngpread.c:710
void png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, png_size_t length)
Definition: png.c:114
#define PNG_READ_CHUNK_MODE
Definition: pngpread.c:20
#define PNG_AFTER_IDAT
Definition: png.h:787
void png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length)
Definition: pngrutil.c:187
#define PNG_FLAG_ZSTREAM_ENDED
Definition: pngpriv.h:604
PNG_IMPEXP png_uint_32() png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf)
Definition: pngrutil.c:22
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
size_t png_size_t
Definition: pngconf.h:543
void png_process_IDAT_data ( png_structrp  png_ptr,
png_bytep  buffer,
png_size_t  buffer_length 
)
712 {
713  /* The caller checks for a non-zero buffer length. */
714  if (!(buffer_length > 0) || buffer == NULL)
715  png_error(png_ptr, "No IDAT data (internal error)");
716 
717  /* This routine must process all the data it has been given
718  * before returning, calling the row callback as required to
719  * handle the uncompressed results.
720  */
721  png_ptr->zstream.next_in = buffer;
722  /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
723  png_ptr->zstream.avail_in = (uInt)buffer_length;
724 
725  /* Keep going until the decompressed data is all processed
726  * or the stream marked as finished.
727  */
728  while (png_ptr->zstream.avail_in > 0 &&
729  !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
730  {
731  int ret;
732 
733  /* We have data for zlib, but we must check that zlib
734  * has someplace to put the results. It doesn't matter
735  * if we don't expect any results -- it may be the input
736  * data is just the LZ end code.
737  */
738  if (!(png_ptr->zstream.avail_out > 0))
739  {
740  /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */
741  png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth,
742  png_ptr->iwidth) + 1);
743 
744  png_ptr->zstream.next_out = png_ptr->row_buf;
745  }
746 
747  /* Using Z_SYNC_FLUSH here means that an unterminated
748  * LZ stream (a stream with a missing end code) can still
749  * be handled, otherwise (Z_NO_FLUSH) a future zlib
750  * implementation might defer output and therefore
751  * change the current behavior (see comments in inflate.c
752  * for why this doesn't happen at present with zlib 1.2.5).
753  */
754  ret = inflate(&png_ptr->zstream, Z_SYNC_FLUSH);
755 
756  /* Check for any failure before proceeding. */
757  if (ret != Z_OK && ret != Z_STREAM_END)
758  {
759  /* Terminate the decompression. */
760  png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
761  png_ptr->zowner = 0;
762 
763  /* This may be a truncated stream (missing or
764  * damaged end code). Treat that as a warning.
765  */
766  if (png_ptr->row_number >= png_ptr->num_rows ||
767  png_ptr->pass > 6)
768  png_warning(png_ptr, "Truncated compressed data in IDAT");
769 
770  else
771  png_error(png_ptr, "Decompression error in IDAT");
772 
773  /* Skip the check on unprocessed input */
774  return;
775  }
776 
777  /* Did inflate output any data? */
778  if (png_ptr->zstream.next_out != png_ptr->row_buf)
779  {
780  /* Is this unexpected data after the last row?
781  * If it is, artificially terminate the LZ output
782  * here.
783  */
784  if (png_ptr->row_number >= png_ptr->num_rows ||
785  png_ptr->pass > 6)
786  {
787  /* Extra data. */
788  png_warning(png_ptr, "Extra compressed data in IDAT");
789  png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
790  png_ptr->zowner = 0;
791 
792  /* Do no more processing; skip the unprocessed
793  * input check below.
794  */
795  return;
796  }
797 
798  /* Do we have a complete row? */
799  if (png_ptr->zstream.avail_out == 0)
800  png_push_process_row(png_ptr);
801  }
802 
803  /* And check for the end of the stream. */
804  if (ret == Z_STREAM_END)
805  png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED;
806  }
807 
808  /* All the data should have been processed, if anything
809  * is left at this point we have bytes of IDAT data
810  * after the zlib end code.
811  */
812  if (png_ptr->zstream.avail_in > 0)
813  png_warning(png_ptr, "Extra compression data in IDAT");
814 }
z_stream zstream
Definition: pngstruct.h:184
Bytef * next_in
Definition: zlib.h:86
png_uint_32 iwidth
Definition: pngstruct.h:219
png_uint_32 row_number
Definition: pngstruct.h:220
uInt avail_in
Definition: zlib.h:87
PNG_IMPEXP void() png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
static char buffer[255]
Definition: propagator_simple.cpp:60
png_uint_32 flags
Definition: pngstruct.h:180
png_bytep row_buf
Definition: pngstruct.h:225
#define Z_STREAM_END
Definition: zlib.h:174
Bytef * next_out
Definition: zlib.h:90
#define PNG_FLAG_ZSTREAM_ENDED
Definition: pngpriv.h:604
png_byte pixel_depth
Definition: pngstruct.h:255
void png_push_process_row(png_structrp png_ptr)
Definition: pngpread.c:817
png_uint_32 num_rows
Definition: pngstruct.h:216
uInt avail_out
Definition: zlib.h:91
#define Z_OK
Definition: zlib.h:173
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define Z_SYNC_FLUSH
Definition: zlib.h:166
int ZEXPORT inflate(z_streamp strm, int flush)
Definition: inflate.c:605
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 zowner
Definition: pngstruct.h:183
png_byte pass
Definition: pngstruct.h:250
unsigned int uInt
Definition: zconf.h:370
void png_push_process_row ( png_structrp  png_ptr)
818 {
819  /* 1.5.6: row_info moved out of png_struct to a local here. */
820  png_row_info row_info;
821 
822  row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
823  row_info.color_type = png_ptr->color_type;
824  row_info.bit_depth = png_ptr->bit_depth;
825  row_info.channels = png_ptr->channels;
826  row_info.pixel_depth = png_ptr->pixel_depth;
827  row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
828 
829  if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
830  {
831  if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
832  png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
833  png_ptr->prev_row + 1, png_ptr->row_buf[0]);
834  else
835  png_error(png_ptr, "bad adaptive filter value");
836  }
837 
838  /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
839  * 1.5.6, while the buffer really is this big in current versions of libpng
840  * it may not be in the future, so this was changed just to copy the
841  * interlaced row count:
842  */
843  memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
844 
845 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
846  if (png_ptr->transformations != 0)
847  png_do_read_transformations(png_ptr, &row_info);
848 #endif
849 
850  /* The transformed pixel depth should match the depth now in row_info. */
851  if (png_ptr->transformed_pixel_depth == 0)
852  {
853  png_ptr->transformed_pixel_depth = row_info.pixel_depth;
854  if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
855  png_error(png_ptr, "progressive row overflow");
856  }
857 
858  else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
859  png_error(png_ptr, "internal progressive row size calculation error");
860 
861 
862 #ifdef PNG_READ_INTERLACING_SUPPORTED
863  /* Expand interlaced rows to full size */
864  if (png_ptr->interlaced != 0 &&
865  (png_ptr->transformations & PNG_INTERLACE) != 0)
866  {
867  if (png_ptr->pass < 6)
868  png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
869  png_ptr->transformations);
870 
871  switch (png_ptr->pass)
872  {
873  case 0:
874  {
875  int i;
876  for (i = 0; i < 8 && png_ptr->pass == 0; i++)
877  {
878  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
879  png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */
880  }
881 
882  if (png_ptr->pass == 2) /* Pass 1 might be empty */
883  {
884  for (i = 0; i < 4 && png_ptr->pass == 2; i++)
885  {
886  png_push_have_row(png_ptr, NULL);
887  png_read_push_finish_row(png_ptr);
888  }
889  }
890 
891  if (png_ptr->pass == 4 && png_ptr->height <= 4)
892  {
893  for (i = 0; i < 2 && png_ptr->pass == 4; i++)
894  {
895  png_push_have_row(png_ptr, NULL);
896  png_read_push_finish_row(png_ptr);
897  }
898  }
899 
900  if (png_ptr->pass == 6 && png_ptr->height <= 4)
901  {
902  png_push_have_row(png_ptr, NULL);
903  png_read_push_finish_row(png_ptr);
904  }
905 
906  break;
907  }
908 
909  case 1:
910  {
911  int i;
912  for (i = 0; i < 8 && png_ptr->pass == 1; i++)
913  {
914  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
915  png_read_push_finish_row(png_ptr);
916  }
917 
918  if (png_ptr->pass == 2) /* Skip top 4 generated rows */
919  {
920  for (i = 0; i < 4 && png_ptr->pass == 2; i++)
921  {
922  png_push_have_row(png_ptr, NULL);
923  png_read_push_finish_row(png_ptr);
924  }
925  }
926 
927  break;
928  }
929 
930  case 2:
931  {
932  int i;
933 
934  for (i = 0; i < 4 && png_ptr->pass == 2; i++)
935  {
936  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
937  png_read_push_finish_row(png_ptr);
938  }
939 
940  for (i = 0; i < 4 && png_ptr->pass == 2; i++)
941  {
942  png_push_have_row(png_ptr, NULL);
943  png_read_push_finish_row(png_ptr);
944  }
945 
946  if (png_ptr->pass == 4) /* Pass 3 might be empty */
947  {
948  for (i = 0; i < 2 && png_ptr->pass == 4; i++)
949  {
950  png_push_have_row(png_ptr, NULL);
951  png_read_push_finish_row(png_ptr);
952  }
953  }
954 
955  break;
956  }
957 
958  case 3:
959  {
960  int i;
961 
962  for (i = 0; i < 4 && png_ptr->pass == 3; i++)
963  {
964  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
965  png_read_push_finish_row(png_ptr);
966  }
967 
968  if (png_ptr->pass == 4) /* Skip top two generated rows */
969  {
970  for (i = 0; i < 2 && png_ptr->pass == 4; i++)
971  {
972  png_push_have_row(png_ptr, NULL);
973  png_read_push_finish_row(png_ptr);
974  }
975  }
976 
977  break;
978  }
979 
980  case 4:
981  {
982  int i;
983 
984  for (i = 0; i < 2 && png_ptr->pass == 4; i++)
985  {
986  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
987  png_read_push_finish_row(png_ptr);
988  }
989 
990  for (i = 0; i < 2 && png_ptr->pass == 4; i++)
991  {
992  png_push_have_row(png_ptr, NULL);
993  png_read_push_finish_row(png_ptr);
994  }
995 
996  if (png_ptr->pass == 6) /* Pass 5 might be empty */
997  {
998  png_push_have_row(png_ptr, NULL);
999  png_read_push_finish_row(png_ptr);
1000  }
1001 
1002  break;
1003  }
1004 
1005  case 5:
1006  {
1007  int i;
1008 
1009  for (i = 0; i < 2 && png_ptr->pass == 5; i++)
1010  {
1011  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1012  png_read_push_finish_row(png_ptr);
1013  }
1014 
1015  if (png_ptr->pass == 6) /* Skip top generated row */
1016  {
1017  png_push_have_row(png_ptr, NULL);
1018  png_read_push_finish_row(png_ptr);
1019  }
1020 
1021  break;
1022  }
1023 
1024  default:
1025  case 6:
1026  {
1027  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1028  png_read_push_finish_row(png_ptr);
1029 
1030  if (png_ptr->pass != 6)
1031  break;
1032 
1033  png_push_have_row(png_ptr, NULL);
1034  png_read_push_finish_row(png_ptr);
1035  }
1036  }
1037  }
1038  else
1039  {
1040  png_push_have_row(png_ptr, png_ptr->row_buf + 1);
1041  png_read_push_finish_row(png_ptr);
1042  }
1043 }
void png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, png_uint_32 transformations)
Definition: pngrutil.c:3486
Definition: png.h:894
#define PNG_FILTER_VALUE_LAST
Definition: png.h:1617
png_uint_32 iwidth
Definition: pngstruct.h:219
int i
Definition: rw_test.cpp:37
png_byte transformed_pixel_depth
Definition: pngstruct.h:263
png_byte color_type
Definition: pngstruct.h:252
void png_push_have_row(png_structrp png_ptr, png_bytep row)
Definition: pngpread.c:1123
png_uint_32 width
Definition: png.h:896
png_byte color_type
Definition: png.h:898
png_bytep prev_row
Definition: pngstruct.h:222
png_byte channels
Definition: png.h:900
png_bytep row_buf
Definition: pngstruct.h:225
png_byte pixel_depth
Definition: png.h:901
png_byte pixel_depth
Definition: pngstruct.h:255
png_size_t rowbytes
Definition: png.h:897
#define PNG_INTERLACE
Definition: pngpriv.h:561
png_byte bit_depth
Definition: pngstruct.h:253
png_byte channels
Definition: pngstruct.h:256
png_byte maximum_pixel_depth
Definition: pngstruct.h:261
PNG_IMPEXP void() png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
#define PNG_FILTER_VALUE_NONE
Definition: png.h:1612
png_uint_32 height
Definition: pngstruct.h:215
void png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
Definition: pngrtran.c:4701
#define PNG_ROWBYTES(pixel_bits, width)
Definition: pngpriv.h:660
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
png_byte bit_depth
Definition: png.h:899
void png_read_push_finish_row(png_structrp png_ptr)
Definition: pngpread.c:1046
void png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, png_const_bytep prev_row, int filter)
Definition: pngrutil.c:3924
void png_push_handle_unknown ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
void png_push_have_info ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
1110 {
1111  if (png_ptr->info_fn != NULL)
1112  (*(png_ptr->info_fn))(png_ptr, info_ptr);
1113 }
void png_push_have_end ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
1117 {
1118  if (png_ptr->end_fn != NULL)
1119  (*(png_ptr->end_fn))(png_ptr, info_ptr);
1120 }
void png_push_have_row ( png_structrp  png_ptr,
png_bytep  row 
)
1124 {
1125  if (png_ptr->row_fn != NULL)
1126  (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number,
1127  (int)png_ptr->pass);
1128 }
png_uint_32 row_number
Definition: pngstruct.h:220
png_byte pass
Definition: pngstruct.h:250
void png_push_read_end ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
void png_process_some_data ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
114 {
115  if (png_ptr == NULL)
116  return;
117 
118  switch (png_ptr->process_mode)
119  {
120  case PNG_READ_SIG_MODE:
121  {
122  png_push_read_sig(png_ptr, info_ptr);
123  break;
124  }
125 
126  case PNG_READ_CHUNK_MODE:
127  {
128  png_push_read_chunk(png_ptr, info_ptr);
129  break;
130  }
131 
132  case PNG_READ_IDAT_MODE:
133  {
134  png_push_read_IDAT(png_ptr);
135  break;
136  }
137 
138  case PNG_SKIP_MODE:
139  {
140  png_push_crc_finish(png_ptr);
141  break;
142  }
143 
144  default:
145  {
146  png_ptr->buffer_size = 0;
147  break;
148  }
149  }
150 }
void png_push_read_IDAT(png_structrp png_ptr)
Definition: pngpread.c:618
void png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngpread.c:192
void png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr)
Definition: pngpread.c:159
#define PNG_READ_IDAT_MODE
Definition: pngpread.c:21
#define PNG_SKIP_MODE
Definition: pngpread.c:22
#define PNG_READ_CHUNK_MODE
Definition: pngpread.c:20
#define PNG_READ_SIG_MODE
Definition: pngpread.c:19
void png_push_crc_finish(png_structrp png_ptr)
Definition: pngpread.c:450
void png_read_push_finish_row ( png_structrp  png_ptr)
1047 {
1048  /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */
1049 
1050  /* Start of interlace block */
1051  static PNG_CONST png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0};
1052 
1053  /* Offset to next interlace block */
1054  static PNG_CONST png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1};
1055 
1056  /* Start of interlace block in the y direction */
1057  static PNG_CONST png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1};
1058 
1059  /* Offset to next interlace block in the y direction */
1060  static PNG_CONST png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2};
1061 
1062  /* Height of interlace block. This is not currently used - if you need
1063  * it, uncomment it here and in png.h
1064  static PNG_CONST png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1};
1065  */
1066 #endif
1067 
1068  png_ptr->row_number++;
1069  if (png_ptr->row_number < png_ptr->num_rows)
1070  return;
1071 
1072  if (png_ptr->interlaced != 0)
1073  {
1074  png_ptr->row_number = 0;
1075  memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1);
1076 
1077  do
1078  {
1079  png_ptr->pass++;
1080  if ((png_ptr->pass == 1 && png_ptr->width < 5) ||
1081  (png_ptr->pass == 3 && png_ptr->width < 3) ||
1082  (png_ptr->pass == 5 && png_ptr->width < 2))
1083  png_ptr->pass++;
1084 
1085  if (png_ptr->pass > 7)
1086  png_ptr->pass--;
1087 
1088  if (png_ptr->pass >= 7)
1089  break;
1090 
1091  png_ptr->iwidth = (png_ptr->width +
1092  png_pass_inc[png_ptr->pass] - 1 -
1093  png_pass_start[png_ptr->pass]) /
1094  png_pass_inc[png_ptr->pass];
1095 
1096  if ((png_ptr->transformations & PNG_INTERLACE) != 0)
1097  break;
1098 
1099  png_ptr->num_rows = (png_ptr->height +
1100  png_pass_yinc[png_ptr->pass] - 1 -
1101  png_pass_ystart[png_ptr->pass]) /
1102  png_pass_yinc[png_ptr->pass];
1103 
1104  } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
1105  }
1106 }
png_uint_32 iwidth
Definition: pngstruct.h:219
png_uint_32 row_number
Definition: pngstruct.h:220
png_uint_32 width
Definition: pngstruct.h:214
png_bytep prev_row
Definition: pngstruct.h:222
#define PNG_CONST
Definition: pngconf.h:86
#define PNG_INTERLACE
Definition: pngpriv.h:561
png_uint_32 num_rows
Definition: pngstruct.h:216
png_size_t rowbytes
Definition: pngstruct.h:218
png_uint_32 height
Definition: pngstruct.h:215
png_uint_32 transformations
Definition: pngstruct.h:181
png_byte interlaced
Definition: pngstruct.h:249
png_byte pass
Definition: pngstruct.h:250
void png_push_handle_tEXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
void png_push_read_tEXt ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
void png_push_handle_zTXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
void png_push_read_zTXt ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
void png_push_handle_iTXt ( png_structrp  png_ptr,
png_inforp  info_ptr,
png_uint_32  length 
)
void png_push_read_iTXt ( png_structrp  png_ptr,
png_inforp  info_ptr 
)
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
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,
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
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_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
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
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
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
void png_do_check_palette_indexes ( png_structrp  png_ptr,
png_row_infop  row_info 
)
685 {
686  if (png_ptr->num_palette < (1 << row_info->bit_depth) &&
687  png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */
688  {
689  /* Calculations moved outside switch in an attempt to stop different
690  * compiler warnings. 'padding' is in *bits* within the last byte, it is
691  * an 'int' because pixel_depth becomes an 'int' in the expression below,
692  * and this calculation is used because it avoids warnings that other
693  * forms produced on either GCC or MSVC.
694  */
695  int padding = (-row_info->pixel_depth * row_info->width) & 7;
696  png_bytep rp = png_ptr->row_buf + row_info->rowbytes;
697 
698  switch (row_info->bit_depth)
699  {
700  case 1:
701  {
702  /* in this case, all bytes must be 0 so we don't need
703  * to unpack the pixels except for the rightmost one.
704  */
705  for (; rp > png_ptr->row_buf; rp--)
706  {
707  if (*rp >> padding != 0)
708  png_ptr->num_palette_max = 1;
709  padding = 0;
710  }
711 
712  break;
713  }
714 
715  case 2:
716  {
717  for (; rp > png_ptr->row_buf; rp--)
718  {
719  int i = ((*rp >> padding) & 0x03);
720 
721  if (i > png_ptr->num_palette_max)
722  png_ptr->num_palette_max = i;
723 
724  i = (((*rp >> padding) >> 2) & 0x03);
725 
726  if (i > png_ptr->num_palette_max)
727  png_ptr->num_palette_max = i;
728 
729  i = (((*rp >> padding) >> 4) & 0x03);
730 
731  if (i > png_ptr->num_palette_max)
732  png_ptr->num_palette_max = i;
733 
734  i = (((*rp >> padding) >> 6) & 0x03);
735 
736  if (i > png_ptr->num_palette_max)
737  png_ptr->num_palette_max = i;
738 
739  padding = 0;
740  }
741 
742  break;
743  }
744 
745  case 4:
746  {
747  for (; rp > png_ptr->row_buf; rp--)
748  {
749  int i = ((*rp >> padding) & 0x0f);
750 
751  if (i > png_ptr->num_palette_max)
752  png_ptr->num_palette_max = i;
753 
754  i = (((*rp >> padding) >> 4) & 0x0f);
755 
756  if (i > png_ptr->num_palette_max)
757  png_ptr->num_palette_max = i;
758 
759  padding = 0;
760  }
761 
762  break;
763  }
764 
765  case 8:
766  {
767  for (; rp > png_ptr->row_buf; rp--)
768  {
769  if (*rp > png_ptr->num_palette_max)
770  png_ptr->num_palette_max = (int) *rp;
771  }
772 
773  break;
774  }
775 
776  default:
777  break;
778  }
779  }
780 }
int i
Definition: rw_test.cpp:37
png_byte * png_bytep
Definition: pngconf.h:600
png_uint_32 width
Definition: png.h:896
png_bytep row_buf
Definition: pngstruct.h:225
png_uint_16 num_palette
Definition: pngstruct.h:239
png_byte pixel_depth
Definition: png.h:901
png_size_t rowbytes
Definition: png.h:897
png_byte bit_depth
Definition: png.h:899
void png_fixed_error ( png_const_structrp  png_ptr,
png_const_charp  name 
)
573 {
574 # define fixed_message "fixed point overflow in "
575 # define fixed_message_ln ((sizeof fixed_message)-1)
576  int iin;
578  memcpy(msg, fixed_message, fixed_message_ln);
579  iin = 0;
580  if (name != NULL)
581  while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0)
582  {
583  msg[fixed_message_ln + iin] = name[iin];
584  ++iin;
585  }
586  msg[fixed_message_ln + iin] = 0;
587  png_error(png_ptr, msg);
588 }
#define fixed_message_ln
#define fixed_message
void png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
string name
Definition: cubesat2obj.cpp:6
#define PNG_MAX_ERROR_TEXT
Definition: pngerror.c:418
size_t png_safecat ( png_charp  buffer,
size_t  bufsize,
size_t  pos,
png_const_charp  string 
)
114 {
115  if (buffer != NULL && pos < bufsize)
116  {
117  if (string != NULL)
118  while (*string != '\0' && pos < bufsize-1)
119  buffer[pos++] = *string++;
120 
121  buffer[pos] = '\0';
122  }
123 
124  return pos;
125 }
static char buffer[255]
Definition: propagator_simple.cpp:60
png_charp png_format_number ( png_const_charp  start,
png_charp  end,
int  format,
png_alloc_size_t  number 
)
135 {
136  int count = 0; /* number of digits output */
137  int mincount = 1; /* minimum number required */
138  int output = 0; /* digit output (for the fixed point format) */
139 
140  *--end = '\0';
141 
142  /* This is written so that the loop always runs at least once, even with
143  * number zero.
144  */
145  while (end > start && (number != 0 || count < mincount))
146  {
147 
148  static const char digits[] = "0123456789ABCDEF";
149 
150  switch (format)
151  {
153  /* Needs five digits (the fraction) */
154  mincount = 5;
155  if (output != 0 || number % 10 != 0)
156  {
157  *--end = digits[number % 10];
158  output = 1;
159  }
160  number /= 10;
161  break;
162 
164  /* Expects at least 2 digits. */
165  mincount = 2;
166  /* FALL THROUGH */
167 
168  case PNG_NUMBER_FORMAT_u:
169  *--end = digits[number % 10];
170  number /= 10;
171  break;
172 
174  /* This format expects at least two digits */
175  mincount = 2;
176  /* FALL THROUGH */
177 
178  case PNG_NUMBER_FORMAT_x:
179  *--end = digits[number & 0xf];
180  number >>= 4;
181  break;
182 
183  default: /* an error */
184  number = 0;
185  break;
186  }
187 
188  /* Keep track of the number of digits added */
189  ++count;
190 
191  /* Float a fixed number here: */
192  if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start))
193  {
194  /* End of the fraction, but maybe nothing was output? In that case
195  * drop the decimal point. If the number is a true zero handle that
196  * here.
197  */
198  if (output != 0)
199  *--end = '.';
200  else if (number == 0) /* and !output */
201  *--end = '0';
202  }
203  }
204 
205  return end;
206 }
int count
Definition: rw_test.cpp:36
string output
Definition: agent-2-0.cpp:56
#define PNG_NUMBER_FORMAT_02x
Definition: pngpriv.h:1592
#define PNG_NUMBER_FORMAT_02u
Definition: pngpriv.h:1588
#define PNG_NUMBER_FORMAT_u
Definition: pngpriv.h:1587
#define PNG_NUMBER_FORMAT_x
Definition: pngpriv.h:1591
#define PNG_NUMBER_FORMAT_fixed
Definition: pngpriv.h:1593
void png_warning_parameter ( png_warning_parameters  p,
int  number,
png_const_charp  string 
)
249 {
250  if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT)
251  (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string);
252 }
#define PNG_WARNING_PARAMETER_COUNT
Definition: pngpriv.h:1599
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
static double * p
Definition: gauss_jackson_test.cpp:42
void png_warning_parameter_unsigned ( png_warning_parameters  p,
int  number,
int  format,
png_alloc_size_t  value 
)
257 {
259  png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value));
260 }
void png_warning_parameter(png_warning_parameters p, int number, png_const_charp string)
Definition: pngerror.c:247
#define PNG_FORMAT_NUMBER(buffer, format, number)
Definition: pngpriv.h:1578
static double * p
Definition: gauss_jackson_test.cpp:42
#define PNG_NUMBER_BUFFER_SIZE
Definition: pngpriv.h:1582
static char buffer[255]
Definition: propagator_simple.cpp:60
void png_warning_parameter_signed ( png_warning_parameters  p,
int  number,
int  format,
png_int_32  value 
)
265 {
267  png_charp str;
269 
270  /* Avoid overflow by doing the negate in a png_alloc_size_t: */
271  u = (png_alloc_size_t)value;
272  if (value < 0)
273  u = ~u + 1;
274 
275  str = PNG_FORMAT_NUMBER(buffer, format, u);
276 
277  if (value < 0 && str > buffer)
278  *--str = '-';
279 
280  png_warning_parameter(p, number, str);
281 }
void png_warning_parameter(png_warning_parameters p, int number, png_const_charp string)
Definition: pngerror.c:247
#define PNG_FORMAT_NUMBER(buffer, format, number)
Definition: pngpriv.h:1578
char * png_charp
Definition: pngconf.h:610
const string & str
Definition: json11.cpp:360
png_size_t png_alloc_size_t
Definition: pngconf.h:578
static double * p
Definition: gauss_jackson_test.cpp:42
#define PNG_NUMBER_BUFFER_SIZE
Definition: pngpriv.h:1582
static char buffer[255]
Definition: propagator_simple.cpp:60
void png_formatted_warning ( png_const_structrp  png_ptr,
png_warning_parameters  p,
png_const_charp  message 
)
286 {
287  /* The internal buffer is just 192 bytes - enough for all our messages,
288  * overflow doesn't happen because this code checks! If someone figures
289  * out how to send us a message longer than 192 bytes, all that will
290  * happen is that the message will be truncated appropriately.
291  */
292  size_t i = 0; /* Index in the msg[] buffer: */
293  char msg[192];
294 
295  /* Each iteration through the following loop writes at most one character
296  * to msg[i++] then returns here to validate that there is still space for
297  * the trailing '\0'. It may (in the case of a parameter) read more than
298  * one character from message[]; it must check for '\0' and continue to the
299  * test if it finds the end of string.
300  */
301  while (i<(sizeof msg)-1 && *message != '\0')
302  {
303  /* '@' at end of string is now just printed (previously it was skipped);
304  * it is an error in the calling code to terminate the string with @.
305  */
306  if (p != NULL && *message == '@' && message[1] != '\0')
307  {
308  int parameter_char = *++message; /* Consume the '@' */
309  static const char valid_parameters[] = "123456789";
310  int parameter = 0;
311 
312  /* Search for the parameter digit, the index in the string is the
313  * parameter to use.
314  */
315  while (valid_parameters[parameter] != parameter_char &&
316  valid_parameters[parameter] != '\0')
317  ++parameter;
318 
319  /* If the parameter digit is out of range it will just get printed. */
320  if (parameter < PNG_WARNING_PARAMETER_COUNT)
321  {
322  /* Append this parameter */
323  png_const_charp parm = p[parameter];
324  png_const_charp pend = p[parameter] + (sizeof p[parameter]);
325 
326  /* No need to copy the trailing '\0' here, but there is no guarantee
327  * that parm[] has been initialized, so there is no guarantee of a
328  * trailing '\0':
329  */
330  while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend)
331  msg[i++] = *parm++;
332 
333  /* Consume the parameter digit too: */
334  ++message;
335  continue;
336  }
337 
338  /* else not a parameter and there is a character after the @ sign; just
339  * copy that. This is known not to be '\0' because of the test above.
340  */
341  }
342 
343  /* At this point *message can't be '\0', even in the bad parameter case
344  * above where there is a lone '@' at the end of the message string.
345  */
346  msg[i++] = *message++;
347  }
348 
349  /* i is always less than (sizeof msg), so: */
350  msg[i] = '\0';
351 
352  /* And this is the formatted message. It may be larger than
353  * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these
354  * are not (currently) formatted.
355  */
356  png_warning(png_ptr, msg);
357 }
const char * png_const_charp
Definition: pngconf.h:611
int i
Definition: rw_test.cpp:37
#define PNG_WARNING_PARAMETER_COUNT
Definition: pngpriv.h:1599
static double * p
Definition: gauss_jackson_test.cpp:42
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
void png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
void png_app_warning ( png_const_structrp  png_ptr,
png_const_charp  message 
)
393 {
394  if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0)
395  png_warning(png_ptr, error_message);
396  else
397  png_error(png_ptr, error_message);
398 
399 # ifndef PNG_ERROR_TEXT_SUPPORTED
400  PNG_UNUSED(error_message)
401 # endif
402 }
#define PNG_FLAG_APP_WARNINGS_WARN
Definition: pngpriv.h:622
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
void png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_uint_32 flags
Definition: pngstruct.h:180
void png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
void png_app_error ( png_const_structrp  png_ptr,
png_const_charp  message 
)
406 {
407  if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0)
408  png_warning(png_ptr, error_message);
409  else
410  png_error(png_ptr, error_message);
411 
412 # ifndef PNG_ERROR_TEXT_SUPPORTED
413  PNG_UNUSED(error_message)
414 # endif
415 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
void png_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:40
png_uint_32 flags
Definition: pngstruct.h:180
#define PNG_FLAG_APP_ERRORS_WARN
Definition: pngpriv.h:623
void png_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:216
void png_chunk_report ( png_const_structrp  png_ptr,
png_const_charp  message,
int  error 
)
532 {
533 # ifndef PNG_WARNINGS_SUPPORTED
535 # endif
536 
537  /* This is always supported, but for just read or just write it
538  * unconditionally does the right thing.
539  */
540 # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
541  if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0)
542 # endif
543 
544 # ifdef PNG_READ_SUPPORTED
545  {
546  if (error < PNG_CHUNK_ERROR)
547  png_chunk_warning(png_ptr, message);
548 
549  else
551  }
552 # endif
553 
554 # if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
555  else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0)
556 # endif
557 
558 # ifdef PNG_WRITE_SUPPORTED
559  {
560  if (error < PNG_CHUNK_WRITE_ERROR)
561  png_app_warning(png_ptr, message);
562 
563  else
564  png_app_error(png_ptr, message);
565  }
566 # endif
567 }
#define PNG_UNUSED(param)
Definition: pngpriv.h:367
#define PNG_IS_READ_STRUCT
Definition: pngpriv.h:557
void png_app_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:405
uint8_t message[300]
Definition: kpc9612p_send.cpp:36
void png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:514
#define PNG_CHUNK_WRITE_ERROR
Definition: pngpriv.h:1673
void png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message)
Definition: pngerror.c:497
#define PNG_CHUNK_ERROR
Definition: pngpriv.h:1674
png_uint_32 mode
Definition: pngstruct.h:179
void png_app_warning(png_const_structrp png_ptr, png_const_charp error_message)
Definition: pngerror.c:392
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 }
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
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
int png_muldiv ( png_fixed_point_p  res,
png_fixed_point  a,
png_int_32  multiplied_by,
png_int_32  divided_by 
)
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  multiplied_by,
png_int_32  divided_by 
)
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
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
int png_gamma_significant ( png_fixed_point  gamma_value)
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
png_uint_16 png_gamma_correct ( png_structrp  png_ptr,
unsigned int  value,
png_fixed_point  gamma_value 
)
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
png_uint_16 png_gamma_16bit_correct ( unsigned int  value,
png_fixed_point  gamma_value 
)
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_byte png_gamma_8bit_correct ( unsigned int  value,
png_fixed_point  gamma_value 
)
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
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
void() png_safe_error ( png_structp  png_ptr,
png_const_charp  error_message 
)
887 {
888  const png_const_structrp png_ptr = png_nonconst_ptr;
890 
891  /* An error is always logged here, overwriting anything (typically a warning)
892  * that is already there:
893  */
894  if (image != NULL)
895  {
896  png_safecat(image->message, (sizeof image->message), 0, error_message);
898 
899  /* Retrieve the jmp_buf from within the png_control, making this work for
900  * C++ compilation too is pretty tricky: C++ wants a pointer to the first
901  * element of a jmp_buf, but C doesn't tell us the type of that.
902  */
903  if (image->opaque != NULL && image->opaque->error_buf != NULL)
904  longjmp(png_control_jmp_buf(image->opaque), 1);
905 
906  /* Missing longjmp buffer, the following is to help debugging: */
907  {
908  size_t pos = png_safecat(image->message, (sizeof image->message), 0,
909  "bad longjmp: ");
910  png_safecat(image->message, (sizeof image->message), pos,
911  error_message);
912  }
913  }
914 
915  /* Here on an internal programming error. */
916  abort();
917 }
uint8_t image[110000000]
Definition: gige_snap.cpp:38
char message[64]
Definition: png.h:2842
png_voidp error_ptr
Definition: pngstruct.h:156
png_controlp opaque
Definition: png.h:2809
#define png_control_jmp_buf(pc)
Definition: pngpriv.h:1884
Definition: pngstruct.h:144
Definition: png.h:2807
png_uint_32 warning_or_error
Definition: png.h:2840
#define png_voidcast(type, value)
Definition: pngpriv.h:408
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
png_voidp error_buf
Definition: pngpriv.h:1869
#define PNG_IMAGE_ERROR
Definition: png.h:2828
void() png_safe_warning ( png_structp  png_ptr,
png_const_charp  warning_message 
)
922 {
923  const png_const_structrp png_ptr = png_nonconst_ptr;
925 
926  /* A warning is only logged if there is no prior warning or error. */
927  if (image->warning_or_error == 0)
928  {
929  png_safecat(image->message, (sizeof image->message), 0, warning_message);
931  }
932 }
uint8_t image[110000000]
Definition: gige_snap.cpp:38
char message[64]
Definition: png.h:2842
png_voidp error_ptr
Definition: pngstruct.h:156
#define PNG_IMAGE_WARNING
Definition: png.h:2827
Definition: pngstruct.h:144
Definition: png.h:2807
png_uint_32 warning_or_error
Definition: png.h:2840
#define png_voidcast(type, value)
Definition: pngpriv.h:408
size_t png_safecat(png_charp buffer, size_t bufsize, size_t pos, png_const_charp string)
Definition: pngerror.c:112
int png_safe_execute ( png_imagep  image,
int(*)(png_voidp function,
png_voidp  arg 
)
937 {
938  volatile png_imagep image = image_in;
939  volatile int result;
940  volatile png_voidp saved_error_buf;
941  jmp_buf safe_jmpbuf;
942 
943  /* Safely execute function(arg) with png_error returning to this function. */
944  saved_error_buf = image->opaque->error_buf;
945  result = setjmp(safe_jmpbuf) == 0;
946 
947  if (result != 0)
948  {
949 
950  image->opaque->error_buf = safe_jmpbuf;
951  result = function(arg);
952  }
953 
954  image->opaque->error_buf = saved_error_buf;
955 
956  /* And do the cleanup prior to any failure return. */
957  if (result == 0)
958  png_image_free(image);
959 
960  return result;
961 }
void * png_voidp
Definition: pngconf.h:598
png_controlp opaque
Definition: png.h:2809
Definition: png.h:2807
png_voidp error_buf
Definition: pngpriv.h:1869
void png_image_free(png_imagep image)
Definition: png.c:4467
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
void png_init_filter_functions_neon ( png_structp  png_ptr,
unsigned int  bpp 
)

Variable Documentation

const png_uint_16 png_sRGB_table[256]
const png_uint_16 png_sRGB_base[512]
const png_byte png_sRGB_delta[512]