Blame libpng.3

Packit 0ba690
.TH LIBPNG 3 "December 29, 2016"
Packit 0ba690
.SH NAME
Packit 0ba690
libpng \- Portable Network Graphics (PNG) Reference Library 1.2.57
Packit 0ba690
.SH SYNOPSIS
Packit 0ba690
\fB
Packit 0ba690
#include <png.h>\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_access_version_number \fI(void\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_check_sig (png_bytep \fP\fIsig\fP\fB, int \fInum\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_chunk_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_chunk_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_convert_from_struct_tm (png_timep \fP\fIptime\fP\fB, struct tm FAR * \fIttime\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_convert_from_time_t (png_timep \fP\fIptime\fP\fB, time_t \fIttime\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_charp png_convert_to_rfc1123 (png_structp \fP\fIpng_ptr\fP\fB, png_timep \fIptime\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_infop png_create_info_struct (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_structp png_create_read_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_structp png_create_read_struct_2(png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_structp png_create_write_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_structp png_create_write_struct_2(png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_debug(int \fP\fIlevel\fP\fB, png_const_charp \fImessage\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_debug1(int \fP\fIlevel\fP\fB, png_const_charp \fP\fImessage\fP\fB, \fIp1\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_debug2(int \fP\fIlevel\fP\fB, png_const_charp \fP\fImessage\fP\fB, \fP\fIp1\fP\fB, \fIp2\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_destroy_info_struct (png_structp \fP\fIpng_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_destroy_read_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fP\fIinfo_ptr_ptr\fP\fB, png_infopp \fIend_info_ptr_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_destroy_write_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_free (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_free_chunk_list (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_free_default(png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_free_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fInum\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_bit_depth (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_bKGD (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fI*background\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_channels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fI*white_x\fP\fB, double \fP\fI*white_y\fP\fB, double \fP\fI*red_x\fP\fB, double \fP\fI*red_y\fP\fB, double \fP\fI*green_x\fP\fB, double \fP\fI*green_y\fP\fB, double \fP\fI*blue_x\fP\fB, double \fI*blue_y\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_cHRM_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*white_x\fP\fB, png_uint_32 \fP\fI*white_y\fP\fB, png_uint_32 \fP\fI*red_x\fP\fB, png_uint_32 \fP\fI*red_y\fP\fB, png_uint_32 \fP\fI*green_x\fP\fB, png_uint_32 \fP\fI*green_y\fP\fB, png_uint_32 \fP\fI*blue_x\fP\fB, png_uint_32 \fI*blue_y\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_color_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_compression_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_copyright (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_error_ptr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_filter_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_gAMA (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fI*file_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_gAMA_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fI*int_file_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_header_ver (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_header_version (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_hIST (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fI*hist\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_iCCP (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charpp \fP\fIname\fP\fB, int \fP\fI*compression_type\fP\fB, png_charpp \fP\fIprofile\fP\fB, png_uint_32 \fI*proflen\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*width\fP\fB, png_uint_32 \fP\fI*height\fP\fB, int \fP\fI*bit_depth\fP\fB, int \fP\fI*color_type\fP\fB, int \fP\fI*interlace_type\fP\fB, int \fP\fI*compression_type\fP\fB, int \fI*filter_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_image_height (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_image_width (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fB#if \fI!defined(PNG_1_0_X)
Packit 0ba690
Packit 0ba690
\fBpng_int_32 png_get_int_32 (png_bytep \fIbuf\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fI\fB#endif
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_interlace_type (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_io_ptr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_libpng_ver (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_mem_ptr(png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_oFFs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*offset_x\fP\fB, png_uint_32 \fP\fI*offset_y\fP\fB, int \fI*unit_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_pCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fI*purpose\fP\fB, png_int_32 \fP\fI*X0\fP\fB, png_int_32 \fP\fI*X1\fP\fB, int \fP\fI*type\fP\fB, int \fP\fI*nparams\fP\fB, png_charp \fP\fI*units\fP\fB, png_charpp \fI*params\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_pHYs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*res_x\fP\fB, png_uint_32 \fP\fI*res_y\fP\fB, int \fI*unit_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBfloat png_get_pixel_aspect_ratio (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_progressive_ptr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fI*palette\fP\fB, int \fI*num_palette\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_byte png_get_rgb_to_gray_status (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_rowbytes (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_bytepp png_get_rows (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_sBIT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fI*sig_bit\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_bytep png_get_signature (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_sPLT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fI*splt_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_sRGB (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fI*intent\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_text (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fI*text_ptr\fP\fB, int \fI*num_text\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_tIME (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fI*mod_time\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_tRNS (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fI*trans\fP\fB, int \fP\fI*num_trans\fP\fB, png_color_16p \fI*trans_values\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fB#if \fI!defined(PNG_1_0_X)
Packit 0ba690
Packit 0ba690
\fBpng_uint_16 png_get_uint_16 (png_bytep \fIbuf\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_uint_31 (png_bytep \fIbuf\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_uint_32 (png_bytep \fIbuf\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fI\fB#endif
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkpp \fIunknowns\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_user_chunk_ptr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_user_height_max( png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_get_user_transform_ptr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_user_width_max (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_valid (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIflag\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_int_32 png_get_x_offset_microns (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_int_32 png_get_x_offset_pixels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_x_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_int_32 png_get_y_offset_microns (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_int_32 png_get_y_offset_pixels (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_y_pixels_per_meter (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_get_compression_buffer_size (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_handle_as_unknown (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIchunk_name\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_init_io (png_structp \fP\fIpng_ptr\fP\fB, FILE \fI*fp\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_info_init (png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_info_init_2 (png_infopp \fP\fIptr_ptr\fP\fB, png_size_t \fIpng_info_struct_size\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_malloc (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_malloc_default(png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoidp png_memcpy (png_voidp \fP\fIs1\fP\fB, png_voidp \fP\fIs2\fP\fB, png_size_t \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_memcpy_check (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIs1\fP\fB, png_voidp \fP\fIs2\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoidp png_memset (png_voidp \fP\fIs1\fP\fB, int \fP\fIvalue\fP\fB, png_size_t \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_voidp png_memset_check (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIs1\fP\fB, int \fP\fIvalue\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_permit_empty_plte (png_structp \fP\fIpng_ptr\fP\fB, int \fIempty_plte_permitted\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_process_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fIbuffer\fP\fB, png_size_t \fIbuffer_size\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_progressive_combine_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIold_row\fP\fB, png_bytep \fInew_row\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_destroy (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_infop \fIend_info_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_read_init (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_read_init_2 (png_structpp \fP\fIptr_ptr\fP\fB, png_const_charp \fP\fIuser_png_ver\fP\fB, png_size_t \fP\fIpng_struct_size\fP\fB, png_size_t \fIpng_info_size\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIrow\fP\fB, png_bytep \fIdisplay_row\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_bytepp \fP\fIdisplay_row\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_read_update_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fB#if \fI!defined(PNG_1_0_X)
Packit 0ba690
Packit 0ba690
\fBpng_save_int_32 (png_bytep \fP\fIbuf\fP\fB, png_int_32 \fIi\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_save_uint_16 (png_bytep \fP\fIbuf\fP\fB, unsigned int \fIi\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_save_uint_32 (png_bytep \fP\fIbuf\fP\fB, png_uint_32 \fIi\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_add_alpha (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fI\fB#endif
Packit 0ba690
Packit 0ba690
\fBvoid png_set_background (png_structp \fP\fIpng_ptr\fP\fB, png_color_16p \fP\fIbackground_color\fP\fB, int \fP\fIbackground_gamma_code\fP\fB, int \fP\fIneed_expand\fP\fB, double \fIbackground_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_bgr (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_bKGD (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fIbackground\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fIwhite_x\fP\fB, double \fP\fIwhite_y\fP\fB, double \fP\fIred_x\fP\fB, double \fP\fIred_y\fP\fB, double \fP\fIgreen_x\fP\fB, double \fP\fIgreen_y\fP\fB, double \fP\fIblue_x\fP\fB, double \fIblue_y\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_cHRM_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwhite_x\fP\fB, png_uint_32 \fP\fIwhite_y\fP\fB, png_uint_32 \fP\fIred_x\fP\fB, png_uint_32 \fP\fIred_y\fP\fB, png_uint_32 \fP\fIgreen_x\fP\fB, png_uint_32 \fP\fIgreen_y\fP\fB, png_uint_32 \fP\fIblue_x\fP\fB, png_uint_32 \fIblue_y\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_level (png_structp \fP\fIpng_ptr\fP\fB, int \fIlevel\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_mem_level (png_structp \fP\fIpng_ptr\fP\fB, int \fImem_level\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_method (png_structp \fP\fIpng_ptr\fP\fB, int \fImethod\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_strategy (png_structp \fP\fIpng_ptr\fP\fB, int \fIstrategy\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_window_bits (png_structp \fP\fIpng_ptr\fP\fB, int \fIwindow_bits\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_crc_action (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIcrit_action\fP\fB, int \fIancil_action\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_dither (png_structp \fP\fIpng_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fP\fInum_palette\fP\fB, int \fP\fImaximum_colors\fP\fB, png_uint_16p \fP\fIhistogram\fP\fB, int \fIfull_dither\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_error_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarning_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_expand (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_expand_gray_1_2_4_to_8(png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_filler (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_filter (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fImethod\fP\fB, int \fIfilters\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_filter_heuristics (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIheuristic_method\fP\fB, int \fP\fInum_weights\fP\fB, png_doublep \fP\fIfilter_weights\fP\fB, png_doublep \fIfilter_costs\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_flush (png_structp \fP\fIpng_ptr\fP\fB, int \fInrows\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_gamma (png_structp \fP\fIpng_ptr\fP\fB, double \fP\fIscreen_gamma\fP\fB, double \fIdefault_file_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_gAMA (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fIfile_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_gAMA_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIfile_gamma\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_gray_1_2_4_to_8(png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_gray_to_rgb (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_hIST (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fIhist\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_iCCP (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIname\fP\fB, int \fP\fIcompression_type\fP\fB, png_charp \fP\fIprofile\fP\fB, png_uint_32 \fIproflen\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_set_interlace_handling (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_invalid (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fImask\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_invert_alpha (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_invert_mono (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwidth\fP\fB, png_uint_32 \fP\fIheight\fP\fB, int \fP\fIbit_depth\fP\fB, int \fP\fIcolor_type\fP\fB, int \fP\fIinterlace_type\fP\fB, int \fP\fIcompression_type\fP\fB, int \fIfilter_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_keep_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIkeep\fP\fB, png_bytep \fP\fIchunk_list\fP\fB, int \fInum_chunks\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_mem_fn(png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_oFFs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIoffset_x\fP\fB, png_uint_32 \fP\fIoffset_y\fP\fB, int \fIunit_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_packing (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_packswap (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_palette_to_rgb(png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_pCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIpurpose\fP\fB, png_int_32 \fP\fIX0\fP\fB, png_int_32 \fP\fIX1\fP\fB, int \fP\fItype\fP\fB, int \fP\fInparams\fP\fB, png_charp \fP\fIunits\fP\fB, png_charpp \fIparams\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_pHYs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIres_x\fP\fB, png_uint_32 \fP\fIres_y\fP\fB, int \fIunit_type\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_progressive_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIprogressive_ptr\fP\fB, png_progressive_info_ptr \fP\fIinfo_fn\fP\fB, png_progressive_row_ptr \fP\fIrow_fn\fP\fB, png_progressive_end_ptr \fIend_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fInum_palette\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fIread_data_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_read_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_read_status_ptr \fIread_row_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_read_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIread_user_transform_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_rgb_to_gray (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIerror_action\fP\fB, double \fP\fIred\fP\fB, double \fIgreen\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_rgb_to_gray_fixed (png_structp \fP\fIpng_ptr\fP\fB, int error_action png_fixed_point \fP\fIred\fP\fB, png_fixed_point \fIgreen\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_rows (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytepp \fIrow_pointers\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sBIT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fIsig_bit\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIunit\fP\fB, double \fP\fIwidth\fP\fB, double \fIheight\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_shift (png_structp \fP\fIpng_ptr\fP\fB, png_color_8p \fItrue_bits\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sig_bytes (png_structp \fP\fIpng_ptr\fP\fB, int \fInum_bytes\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sPLT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fP\fIsplt_ptr\fP\fB, int \fInum_spalettes\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sRGB (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIintent\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_sRGB_gAMA_and_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIintent\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_strip_16 (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_strip_alpha (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_swap (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_swap_alpha (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_text (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fItext_ptr\fP\fB, int \fInum_text\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_tIME (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fImod_time\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_tRNS (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fItrans\fP\fB, int \fP\fInum_trans\fP\fB, png_color_16p \fItrans_values\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_tRNS_to_alpha(png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBpng_uint_32 png_set_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkp \fP\fIunknowns\fP\fB, int \fP\fInum\fP\fB, int \fIlocation\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_unknown_chunk_location(png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIchunk\fP\fB, int \fIlocation\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_read_user_chunk_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_chunk_ptr\fP\fB, png_user_chunk_ptr \fIread_user_chunk_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_user_limits (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIuser_width_max\fP\fB, png_uint_32 \fIuser_height_max\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_user_transform_info (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_transform_ptr\fP\fB, int \fP\fIuser_transform_depth\fP\fB, int \fIuser_transform_channels\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_write_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fP\fIwrite_data_fn\fP\fB, png_flush_ptr \fIoutput_flush_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_write_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_write_status_ptr \fIwrite_row_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_write_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIwrite_user_transform_fn\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_set_compression_buffer_size(png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBint png_sig_cmp (png_bytep \fP\fIsig\fP\fB, png_size_t \fP\fIstart\fP\fB, png_size_t \fInum_to_check\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_start_read_image (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_chunk (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_bytep \fP\fIdata\fP\fB, png_size_t \fIlength\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_chunk_data (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIdata\fP\fB, png_size_t \fIlength\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_chunk_end (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_chunk_start (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_uint_32 \fIlength\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_destroy (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_flush (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_write_init (png_structp \fIpng_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBDEPRECATED void png_write_init_2 (png_structpp \fP\fIptr_ptr\fP\fB, png_const_charp \fP\fIuser_png_ver\fP\fB, png_size_t \fP\fIpng_struct_size\fP\fB, png_size_t \fIpng_info_size\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_info_before_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIrow\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_write_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoidpf png_zalloc (voidpf \fP\fIpng_ptr\fP\fB, uInt \fP\fIitems\fP\fB, uInt \fIsize\fP\fB);\fP
Packit 0ba690
Packit 0ba690
\fBvoid png_zfree (voidpf \fP\fIpng_ptr\fP\fB, voidpf \fIptr\fP\fB);\fP
Packit 0ba690
Packit 0ba690
.SH DESCRIPTION
Packit 0ba690
The
Packit 0ba690
.I libpng
Packit 0ba690
library supports encoding, decoding, and various manipulations of
Packit 0ba690
the Portable Network Graphics (PNG) format image files.  It uses the
Packit 0ba690
.IR zlib(3)
Packit 0ba690
compression library.
Packit 0ba690
Following is a copy of the libpng.txt file that accompanies libpng.
Packit 0ba690
.SH LIBPNG.TXT
Packit 0ba690
libpng.txt - A description on how to use and modify libpng
Packit 0ba690
Packit 0ba690
 libpng version 1.2.57 - December 29, 2016
Packit 0ba690
 Updated and distributed by Glenn Randers-Pehrson
Packit 0ba690
 <glennrp at users.sourceforge.net>
Packit 0ba690
 Copyright (c) 1998-2014 Glenn Randers-Pehrson
Packit 0ba690
Packit 0ba690
 This document is released under the libpng license.
Packit 0ba690
 For conditions of distribution and use, see the disclaimer
Packit 0ba690
 and license in png.h
Packit 0ba690
Packit 0ba690
 Based on:
Packit 0ba690
Packit 0ba690
 libpng versions 0.97, January 1998, through 1.2.57 - December 29, 2016
Packit 0ba690
 Updated and distributed by Glenn Randers-Pehrson
Packit 0ba690
 Copyright (c) 1998-2014 Glenn Randers-Pehrson
Packit 0ba690
Packit 0ba690
 libpng 1.0 beta 6  version 0.96 May 28, 1997
Packit 0ba690
 Updated and distributed by Andreas Dilger
Packit 0ba690
 Copyright (c) 1996, 1997 Andreas Dilger
Packit 0ba690
Packit 0ba690
 libpng 1.0 beta 2 - version 0.88  January 26, 1996
Packit 0ba690
 For conditions of distribution and use, see copyright
Packit 0ba690
 notice in png.h. Copyright (c) 1995, 1996 Guy Eric
Packit 0ba690
 Schalnat, Group 42, Inc.
Packit 0ba690
Packit 0ba690
 Updated/rewritten per request in the libpng FAQ
Packit 0ba690
 Copyright (c) 1995, 1996 Frank J. T. Wojcik
Packit 0ba690
 December 18, 1995 & January 20, 1996
Packit 0ba690
Packit 0ba690
.SH I. Introduction
Packit 0ba690
Packit 0ba690
This file describes how to use and modify the PNG reference library
Packit 0ba690
(known as libpng) for your own use.  There are five sections to this
Packit 0ba690
file: introduction, structures, reading, writing, and modification and
Packit 0ba690
configuration notes for various special platforms.  In addition to this
Packit 0ba690
file, example.c is a good starting point for using the library, as
Packit 0ba690
it is heavily commented and should include everything most people
Packit 0ba690
will need.  We assume that libpng is already installed; see the
Packit 0ba690
INSTALL file for instructions on how to install libpng.
Packit 0ba690
Packit 0ba690
For examples of libpng usage, see the files "example.c", "pngtest.c",
Packit 0ba690
and the files in the "contrib" directory, all of which are included in
Packit 0ba690
the libpng distribution.
Packit 0ba690
Packit 0ba690
Libpng was written as a companion to the PNG specification, as a way
Packit 0ba690
of reducing the amount of time and effort it takes to support the PNG
Packit 0ba690
file format in application programs.
Packit 0ba690
Packit 0ba690
The PNG specification (second edition), November 2003, is available as
Packit 0ba690
a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2003 (E)) at
Packit 0ba690
Packit 0ba690
The W3C and ISO documents have identical technical content.
Packit 0ba690
Packit 0ba690
The PNG-1.2 specification is available at
Packit 0ba690
<http://png-mng.sourceforge.net/pub/png/spec/1.2/>.
Packit 0ba690
It is technically equivalent
Packit 0ba690
to the PNG specification (second edition) but has some additional material.
Packit 0ba690
Packit 0ba690
The PNG-1.0 specification is available as RFC 2083 
Packit 0ba690
<http://png-mng.sourceforge.net/pub/png/spec/1.0/> and as a
Packit 0ba690
W3C Recommendation <http://www.w3.org/TR/REC-png-961001>.
Packit 0ba690
Packit 0ba690
Some additional chunks are described in the special-purpose public chunks
Packit 0ba690
documents at <http://www.libpng.org/pub/png/spec/register/>
Packit 0ba690
Packit 0ba690
Other information
Packit 0ba690
about PNG, and the latest version of libpng, can be found at the PNG home
Packit 0ba690
page, <http://www.libpng.org/pub/png/>.
Packit 0ba690
Packit 0ba690
Most users will not have to modify the library significantly; advanced
Packit 0ba690
users may want to modify it more.  All attempts were made to make it as
Packit 0ba690
complete as possible, while keeping the code easy to understand.
Packit 0ba690
Currently, this library only supports C.  Support for other languages
Packit 0ba690
is being considered.
Packit 0ba690
Packit 0ba690
Libpng has been designed to handle multiple sessions at one time,
Packit 0ba690
to be easily modifiable, to be portable to the vast majority of
Packit 0ba690
machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy
Packit 0ba690
to use.  The ultimate goal of libpng is to promote the acceptance of
Packit 0ba690
the PNG file format in whatever way possible.  While there is still
Packit 0ba690
work to be done (see the TODO file), libpng should cover the
Packit 0ba690
majority of the needs of its users.
Packit 0ba690
Packit 0ba690
Libpng uses zlib for its compression and decompression of PNG files.
Packit 0ba690
Further information about zlib, and the latest version of zlib, can
Packit 0ba690
be found at the zlib home page, <http://zlib.net/>.
Packit 0ba690
The zlib compression utility is a general purpose utility that is
Packit 0ba690
useful for more than PNG files, and can be used without libpng.
Packit 0ba690
See the documentation delivered with zlib for more details.
Packit 0ba690
You can usually find the source files for the zlib utility wherever you
Packit 0ba690
find the libpng source files.
Packit 0ba690
Packit 0ba690
Libpng is thread safe, provided the threads are using different
Packit 0ba690
instances of the structures.  Each thread should have its own
Packit 0ba690
png_struct and png_info instances, and thus its own image.
Packit 0ba690
Libpng does not protect itself against two threads using the
Packit 0ba690
same instance of a structure.
Packit 0ba690
Packit 0ba690
.SH II. Structures
Packit 0ba690
Packit 0ba690
There are two main structures that are important to libpng, png_struct
Packit 0ba690
and png_info.  The first, png_struct, is an internal structure that
Packit 0ba690
will not, for the most part, be used by a user except as the first
Packit 0ba690
variable passed to every libpng function call.
Packit 0ba690
Packit 0ba690
The png_info structure is designed to provide information about the
Packit 0ba690
PNG file.  At one time, the fields of png_info were intended to be
Packit 0ba690
directly accessible to the user.  However, this tended to cause problems
Packit 0ba690
with applications using dynamically loaded libraries, and as a result
Packit 0ba690
a set of interface functions for png_info (the png_get_*() and png_set_*()
Packit 0ba690
functions) was developed.  The fields of png_info are still available for
Packit 0ba690
older applications, but it is suggested that applications use the new
Packit 0ba690
interfaces if at all possible.
Packit 0ba690
Packit 0ba690
Applications that do make direct access to the members of png_struct (except
Packit 0ba690
for png_ptr->jmpbuf) must be recompiled whenever the library is updated,
Packit 0ba690
and applications that make direct access to the members of png_info must
Packit 0ba690
be recompiled if they were compiled or loaded with libpng version 1.0.6,
Packit 0ba690
in which the members were in a different order.  In version 1.0.7, the
Packit 0ba690
members of the png_info structure reverted to the old order, as they were
Packit 0ba690
in versions 0.97c through 1.0.5.  Starting with version 2.0.0, both
Packit 0ba690
structures are going to be hidden, and the contents of the structures will
Packit 0ba690
only be accessible through the png_get/png_set functions.
Packit 0ba690
Packit 0ba690
The png.h header file is an invaluable reference for programming with libpng.
Packit 0ba690
And while I'm on the topic, make sure you include the libpng header file:
Packit 0ba690
Packit 0ba690
#include <png.h>
Packit 0ba690
Packit 0ba690
.SH III. Reading
Packit 0ba690
Packit 0ba690
We'll now walk you through the possible functions to call when reading
Packit 0ba690
in a PNG file sequentially, briefly explaining the syntax and purpose
Packit 0ba690
of each one.  See example.c and png.h for more detail.  While
Packit 0ba690
progressive reading is covered in the next section, you will still
Packit 0ba690
need some of the functions discussed in this section to read a PNG
Packit 0ba690
file.
Packit 0ba690
Packit 0ba690
.SS Setup
Packit 0ba690
Packit 0ba690
You will want to do the I/O initialization(*) before you get into libpng,
Packit 0ba690
so if it doesn't work, you don't have much to undo.  Of course, you
Packit 0ba690
will also want to insure that you are, in fact, dealing with a PNG
Packit 0ba690
file.  Libpng provides a simple check to see if a file is a PNG file.
Packit 0ba690
To use it, pass in the first 1 to 8 bytes of the file to the function
Packit 0ba690
png_sig_cmp(), and it will return 0 (false) if the bytes match the
Packit 0ba690
corresponding bytes of the PNG signature, or nonzero (true) otherwise.
Packit 0ba690
Of course, the more bytes you pass in, the greater the accuracy of the
Packit 0ba690
prediction.
Packit 0ba690
Packit 0ba690
If you are intending to keep the file pointer open for use in libpng,
Packit 0ba690
you must ensure you don't read more than 8 bytes from the beginning
Packit 0ba690
of the file, and you also have to make a call to png_set_sig_bytes()
Packit 0ba690
with the number of bytes you read from the beginning.  Libpng will
Packit 0ba690
then only check the bytes (if any) that your program didn't read.
Packit 0ba690
Packit 0ba690
(*): If you are not using the standard I/O functions, you will need
Packit 0ba690
to replace them with custom functions.  See the discussion under
Packit 0ba690
Customizing libpng.
Packit 0ba690
Packit 0ba690
Packit 0ba690
    FILE *fp = fopen(file_name, "rb");
Packit 0ba690
    if (!fp)
Packit 0ba690
    {
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
    fread(header, 1, number, fp);
Packit 0ba690
    is_png = !png_sig_cmp(header, 0, number);
Packit 0ba690
    if (!is_png)
Packit 0ba690
    {
Packit 0ba690
        return (NOT_PNG);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
Packit 0ba690
Next, png_struct and png_info need to be allocated and initialized.  In
Packit 0ba690
order to ensure that the size of these structures is correct even with a
Packit 0ba690
dynamically linked libpng, there are functions to initialize and
Packit 0ba690
allocate the structures.  We also pass the library version, optional
Packit 0ba690
pointers to error handling functions, and a pointer to a data struct for
Packit 0ba690
use by the error functions, if necessary (the pointer and functions can
Packit 0ba690
be NULL if the default error handlers are to be used).  See the section
Packit 0ba690
on Changes to Libpng below regarding the old initialization functions.
Packit 0ba690
The structure allocation functions quietly return NULL if they fail to
Packit 0ba690
create the structure, so your application should check for that.
Packit 0ba690
Packit 0ba690
    png_structp png_ptr = png_create_read_struct
Packit 0ba690
       (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Packit 0ba690
        user_error_fn, user_warning_fn);
Packit 0ba690
    if (!png_ptr)
Packit 0ba690
        return (ERROR);
Packit 0ba690
Packit 0ba690
    png_infop info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
    if (!info_ptr)
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr,
Packit 0ba690
           (png_infopp)NULL, (png_infopp)NULL);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    png_infop end_info = png_create_info_struct(png_ptr);
Packit 0ba690
    if (!end_info)
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr, &info_ptr,
Packit 0ba690
          (png_infopp)NULL);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
If you want to use your own memory allocation routines,
Packit 0ba690
define PNG_USER_MEM_SUPPORTED and use
Packit 0ba690
png_create_read_struct_2() instead of png_create_read_struct():
Packit 0ba690
Packit 0ba690
    png_structp png_ptr = png_create_read_struct_2
Packit 0ba690
       (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Packit 0ba690
        user_error_fn, user_warning_fn, (png_voidp)
Packit 0ba690
        user_mem_ptr, user_malloc_fn, user_free_fn);
Packit 0ba690
Packit 0ba690
The error handling routines passed to png_create_read_struct()
Packit 0ba690
and the memory alloc/free routines passed to png_create_struct_2()
Packit 0ba690
are only necessary if you are not using the libpng supplied error
Packit 0ba690
handling and memory alloc/free functions.
Packit 0ba690
Packit 0ba690
When libpng encounters an error, it expects to longjmp back
Packit 0ba690
to your routine.  Therefore, you will need to call setjmp and pass
Packit 0ba690
your png_jmpbuf(png_ptr).  If you read the file from different
Packit 0ba690
routines, you will need to update the jmpbuf field every time you enter
Packit 0ba690
a new routine that will call a png_*() function.
Packit 0ba690
Packit 0ba690
See your documentation of setjmp/longjmp for your compiler for more
Packit 0ba690
information on setjmp/longjmp.  See the discussion on libpng error
Packit 0ba690
handling in the Customizing Libpng section below for more information
Packit 0ba690
on the libpng error handling.  If an error occurs, and libpng longjmp's
Packit 0ba690
back to your setjmp, you will want to call png_destroy_read_struct() to
Packit 0ba690
free any memory.
Packit 0ba690
Packit 0ba690
    if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr, &info_ptr,
Packit 0ba690
           &end_info);
Packit 0ba690
        fclose(fp);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
If you would rather avoid the complexity of setjmp/longjmp issues,
Packit 0ba690
you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
Packit 0ba690
errors will result in a call to PNG_ABORT() which defaults to abort().
Packit 0ba690
Packit 0ba690
Now you need to set up the input code.  The default for libpng is to
Packit 0ba690
use the C function fread().  If you use this, you will need to pass a
Packit 0ba690
valid FILE * in the function png_init_io().  Be sure that the file is
Packit 0ba690
opened in binary mode.  If you wish to handle reading data in another
Packit 0ba690
way, you need not call the png_init_io() function, but you must then
Packit 0ba690
implement the libpng I/O methods discussed in the Customizing Libpng
Packit 0ba690
section below.
Packit 0ba690
Packit 0ba690
    png_init_io(png_ptr, fp);
Packit 0ba690
Packit 0ba690
If you had previously opened the file and read any of the signature from
Packit 0ba690
the beginning in order to see if this was a PNG file, you need to let
Packit 0ba690
libpng know that there are some bytes missing from the start of the file.
Packit 0ba690
Packit 0ba690
    png_set_sig_bytes(png_ptr, number);
Packit 0ba690
Packit 0ba690
.SS Setting up callback code
Packit 0ba690
Packit 0ba690
You can set up a callback function to handle any unknown chunks in the
Packit 0ba690
input stream. You must supply the function
Packit 0ba690
Packit 0ba690
    read_chunk_callback(png_ptr ptr,
Packit 0ba690
         png_unknown_chunkp chunk);
Packit 0ba690
    {
Packit 0ba690
       /* The unknown chunk structure contains your
Packit 0ba690
          chunk data, along with similar data for any other
Packit 0ba690
          unknown chunks: */
Packit 0ba690
Packit 0ba690
           png_byte name[5];
Packit 0ba690
           png_byte *data;
Packit 0ba690
           png_size_t size;
Packit 0ba690
Packit 0ba690
       /* Note that libpng has already taken care of
Packit 0ba690
          the CRC handling */
Packit 0ba690
Packit 0ba690
       /* put your code here.  Search for your chunk in the
Packit 0ba690
          unknown chunk structure, process it, and return one
Packit 0ba690
          of the following: */
Packit 0ba690
Packit 0ba690
       return (\-n); /* chunk had an error */
Packit 0ba690
       return (0); /* did not recognize */
Packit 0ba690
       return (n); /* success */
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
(You can give your function another name that you like instead of
Packit 0ba690
"read_chunk_callback")
Packit 0ba690
Packit 0ba690
To inform libpng about your function, use
Packit 0ba690
Packit 0ba690
    png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr,
Packit 0ba690
        read_chunk_callback);
Packit 0ba690
Packit 0ba690
This names not only the callback function, but also a user pointer that
Packit 0ba690
you can retrieve with
Packit 0ba690
Packit 0ba690
    png_get_user_chunk_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
If you call the png_set_read_user_chunk_fn() function, then all unknown
Packit 0ba690
chunks will be saved when read, in case your callback function will need
Packit 0ba690
one or more of them.  This behavior can be changed with the
Packit 0ba690
png_set_keep_unknown_chunks() function, described below.
Packit 0ba690
Packit 0ba690
At this point, you can set up a callback function that will be
Packit 0ba690
called after each row has been read, which you can use to control
Packit 0ba690
a progress meter or the like.  It's demonstrated in pngtest.c.
Packit 0ba690
You must supply a function
Packit 0ba690
Packit 0ba690
    void read_row_callback(png_ptr ptr, png_uint_32 row,
Packit 0ba690
       int pass);
Packit 0ba690
    {
Packit 0ba690
      /* put your code here */
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
(You can give it another name that you like instead of "read_row_callback")
Packit 0ba690
Packit 0ba690
To inform libpng about your function, use
Packit 0ba690
Packit 0ba690
    png_set_read_status_fn(png_ptr, read_row_callback);
Packit 0ba690
Packit 0ba690
.SS Unknown-chunk handling
Packit 0ba690
Packit 0ba690
Now you get to set the way the library processes unknown chunks in the
Packit 0ba690
input PNG stream. Both known and unknown chunks will be read.  Normal
Packit 0ba690
behavior is that known chunks will be parsed into information in
Packit 0ba690
various info_ptr members while unknown chunks will be discarded. This
Packit 0ba690
behavior can be wasteful if your application will never use some known
Packit 0ba690
chunk types. To change this, you can call:
Packit 0ba690
Packit 0ba690
    png_set_keep_unknown_chunks(png_ptr, keep,
Packit 0ba690
        chunk_list, num_chunks);
Packit 0ba690
    keep       - 0: default unknown chunk handling
Packit 0ba690
                 1: ignore; do not keep
Packit 0ba690
                 2: keep only if safe-to-copy
Packit 0ba690
                 3: keep even if unsafe-to-copy
Packit 0ba690
               You can use these definitions:
Packit 0ba690
                 PNG_HANDLE_CHUNK_AS_DEFAULT   0
Packit 0ba690
                 PNG_HANDLE_CHUNK_NEVER        1
Packit 0ba690
                 PNG_HANDLE_CHUNK_IF_SAFE      2
Packit 0ba690
                 PNG_HANDLE_CHUNK_ALWAYS       3
Packit 0ba690
    chunk_list - list of chunks affected (a byte string,
Packit 0ba690
                 five bytes per chunk, NULL or '\0' if
Packit 0ba690
                 num_chunks is 0)
Packit 0ba690
    num_chunks - number of chunks affected; if 0, all
Packit 0ba690
                 unknown chunks are affected.  If nonzero,
Packit 0ba690
                 only the chunks in the list are affected
Packit 0ba690
Packit 0ba690
Unknown chunks declared in this way will be saved as raw data onto a
Packit 0ba690
list of png_unknown_chunk structures.  If a chunk that is normally
Packit 0ba690
known to libpng is named in the list, it will be handled as unknown,
Packit 0ba690
according to the "keep" directive.  If a chunk is named in successive
Packit 0ba690
instances of png_set_keep_unknown_chunks(), the final instance will
Packit 0ba690
take precedence.  The IHDR and IEND chunks should not be named in
Packit 0ba690
chunk_list; if they are, libpng will process them normally anyway.
Packit 0ba690
Packit 0ba690
Here is an example of the usage of png_set_keep_unknown_chunks(),
Packit 0ba690
where the private "vpAg" chunk will later be processed by a user chunk
Packit 0ba690
callback function:
Packit 0ba690
Packit 0ba690
    png_byte vpAg[5]={118, 112,  65, 103, (png_byte) '\0'};
Packit 0ba690
Packit 0ba690
    #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Packit 0ba690
      png_byte unused_chunks[]=
Packit 0ba690
      {
Packit 0ba690
        104,  73,  83,  84, (png_byte) '\0',   /* hIST */
Packit 0ba690
        105,  84,  88, 116, (png_byte) '\0',   /* iTXt */
Packit 0ba690
        112,  67,  65,  76, (png_byte) '\0',   /* pCAL */
Packit 0ba690
        115,  67,  65,  76, (png_byte) '\0',   /* sCAL */
Packit 0ba690
        115,  80,  76,  84, (png_byte) '\0',   /* sPLT */
Packit 0ba690
        116,  73,  77,  69, (png_byte) '\0',   /* tIME */
Packit 0ba690
      };
Packit 0ba690
    #endif
Packit 0ba690
Packit 0ba690
    ...
Packit 0ba690
Packit 0ba690
    #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
Packit 0ba690
      /* ignore all unknown chunks: */
Packit 0ba690
      png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
Packit 0ba690
      /* except for vpAg: */
Packit 0ba690
      png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
Packit 0ba690
      /* also ignore unused known chunks: */
Packit 0ba690
      png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
Packit 0ba690
         (int)sizeof(unused_chunks)/5);
Packit 0ba690
    #endif
Packit 0ba690
Packit 0ba690
.SS User limits
Packit 0ba690
Packit 0ba690
The PNG specification allows the width and height of an image to be as
Packit 0ba690
large as 2^(31\-1 (0x7fffffff), or about 2.147 billion rows and columns.
Packit 0ba690
Since very few applications really need to process such large images,
Packit 0ba690
we have imposed an arbitrary 1-million limit on rows and columns.
Packit 0ba690
Larger images will be rejected immediately with a png_error() call. If
Packit 0ba690
you wish to override this limit, you can use
Packit 0ba690
Packit 0ba690
   png_set_user_limits(png_ptr, width_max, height_max);
Packit 0ba690
Packit 0ba690
to set your own limits, or use width_max = height_max = 0x7fffffffL
Packit 0ba690
to allow all valid dimensions (libpng may reject some very large images
Packit 0ba690
anyway because of potential buffer overflow conditions).
Packit 0ba690
Packit 0ba690
You should put this statement after you create the PNG structure and
Packit 0ba690
before calling png_read_info(), png_read_png(), or png_process_data().
Packit 0ba690
If you need to retrieve the limits that are being applied, use
Packit 0ba690
Packit 0ba690
   width_max = png_get_user_width_max(png_ptr);
Packit 0ba690
   height_max = png_get_user_height_max(png_ptr);
Packit 0ba690
Packit 0ba690
The PNG specification sets no limit on the number of ancillary chunks
Packit 0ba690
allowed in a PNG datastream.  You can impose a limit on the total number
Packit 0ba690
of sPLT, tEXt, iTXt, zTXt, and unknown chunks that will be stored, with
Packit 0ba690
Packit 0ba690
   png_set_chunk_cache_max(png_ptr, user_chunk_cache_max);
Packit 0ba690
Packit 0ba690
where 0x7fffffffL means unlimited.  You can retrieve this limit with
Packit 0ba690
Packit 0ba690
   chunk_cache_max = png_get_chunk_cache_max(png_ptr);
Packit 0ba690
Packit 0ba690
This limit also applies to the number of buffers that can be allocated
Packit 0ba690
by png_decompress_chunk() while decompressing iTXt, zTXt, and iCCP chunks.
Packit 0ba690
Packit 0ba690
.SS The high-level read interface
Packit 0ba690
Packit 0ba690
At this point there are two ways to proceed; through the high-level
Packit 0ba690
read interface, or through a sequence of low-level read operations.
Packit 0ba690
You can use the high-level interface if (a) you are willing to read
Packit 0ba690
the entire image into memory, and (b) the input transformations
Packit 0ba690
you want to do are limited to the following set:
Packit 0ba690
Packit 0ba690
    PNG_TRANSFORM_IDENTITY      No transformation
Packit 0ba690
    PNG_TRANSFORM_STRIP_16      Strip 16-bit samples to
Packit 0ba690
                                8 bits
Packit 0ba690
    PNG_TRANSFORM_STRIP_ALPHA   Discard the alpha channel
Packit 0ba690
    PNG_TRANSFORM_PACKING       Expand 1, 2 and 4-bit
Packit 0ba690
                                samples to bytes
Packit 0ba690
    PNG_TRANSFORM_PACKSWAP      Change order of packed
Packit 0ba690
                                pixels to LSB first
Packit 0ba690
    PNG_TRANSFORM_EXPAND        Perform set_expand()
Packit 0ba690
    PNG_TRANSFORM_INVERT_MONO   Invert monochrome images
Packit 0ba690
    PNG_TRANSFORM_SHIFT         Normalize pixels to the
Packit 0ba690
                                sBIT depth
Packit 0ba690
    PNG_TRANSFORM_BGR           Flip RGB to BGR, RGBA
Packit 0ba690
                                to BGRA
Packit 0ba690
    PNG_TRANSFORM_SWAP_ALPHA    Flip RGBA to ARGB or GA
Packit 0ba690
                                to AG
Packit 0ba690
    PNG_TRANSFORM_INVERT_ALPHA  Change alpha from opacity
Packit 0ba690
                                to transparency
Packit 0ba690
    PNG_TRANSFORM_SWAP_ENDIAN   Byte-swap 16-bit samples
Packit 0ba690
    PNG_TRANSFORM_GRAY_TO_RGB   Expand grayscale samples
Packit 0ba690
                                to RGB (or GA to RGBA)
Packit 0ba690
Packit 0ba690
(This excludes setting a background color, doing gamma transformation,
Packit 0ba690
dithering, and setting filler.)  If this is the case, simply do this:
Packit 0ba690
Packit 0ba690
    png_read_png(png_ptr, info_ptr, png_transforms, NULL)
Packit 0ba690
Packit 0ba690
where png_transforms is an integer containing the bitwise OR of some
Packit 0ba690
set of transformation flags.  This call is equivalent to png_read_info(),
Packit 0ba690
followed the set of transformations indicated by the transform mask,
Packit 0ba690
then png_read_image(), and finally png_read_end().
Packit 0ba690
Packit 0ba690
(The final parameter of this call is not yet used.  Someday it might point
Packit 0ba690
to transformation parameters required by some future input transform.)
Packit 0ba690
Packit 0ba690
You must use png_transforms and not call any png_set_transform() functions
Packit 0ba690
when you use png_read_png().
Packit 0ba690
Packit 0ba690
After you have called png_read_png(), you can retrieve the image data
Packit 0ba690
with
Packit 0ba690
Packit 0ba690
   row_pointers = png_get_rows(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
where row_pointers is an array of pointers to the pixel data for each row:
Packit 0ba690
Packit 0ba690
   png_bytep row_pointers[height];
Packit 0ba690
Packit 0ba690
If you know your image size and pixel size ahead of time, you can allocate
Packit 0ba690
row_pointers prior to calling png_read_png() with
Packit 0ba690
Packit 0ba690
   if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
Packit 0ba690
      png_error (png_ptr,
Packit 0ba690
         "Image is too tall to process in memory");
Packit 0ba690
   if (width > PNG_UINT_32_MAX/pixel_size)
Packit 0ba690
      png_error (png_ptr,
Packit 0ba690
         "Image is too wide to process in memory");
Packit 0ba690
   row_pointers = png_malloc(png_ptr,
Packit 0ba690
      height*png_sizeof(png_bytep));
Packit 0ba690
   for (int i=0; i
Packit 0ba690
      row_pointers[i]=NULL;  /* security precaution */
Packit 0ba690
   for (int i=0; i
Packit 0ba690
      row_pointers[i]=png_malloc(png_ptr,
Packit 0ba690
         width*pixel_size);
Packit 0ba690
   png_set_rows(png_ptr, info_ptr, &row_pointers);
Packit 0ba690
Packit 0ba690
Alternatively you could allocate your image in one big block and define
Packit 0ba690
row_pointers[i] to point into the proper places in your block.
Packit 0ba690
Packit 0ba690
If you use png_set_rows(), the application is responsible for freeing
Packit 0ba690
row_pointers (and row_pointers[i], if they were separately allocated).
Packit 0ba690
Packit 0ba690
If you don't allocate row_pointers ahead of time, png_read_png() will
Packit 0ba690
do it, and it'll be free'ed when you call png_destroy_*().
Packit 0ba690
Packit 0ba690
.SS The low-level read interface
Packit 0ba690
Packit 0ba690
If you are going the low-level route, you are now ready to read all
Packit 0ba690
the file information up to the actual image data.  You do this with a
Packit 0ba690
call to png_read_info().
Packit 0ba690
Packit 0ba690
    png_read_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
This will process all chunks up to but not including the image data.
Packit 0ba690
Packit 0ba690
.SS Querying the info structure
Packit 0ba690
Packit 0ba690
Functions are used to get the information from the info_ptr once it
Packit 0ba690
has been read.  Note that these fields may not be completely filled
Packit 0ba690
in until png_read_end() has read the chunk data following the image.
Packit 0ba690
Packit 0ba690
    png_get_IHDR(png_ptr, info_ptr, &width, &height,
Packit 0ba690
       &bit_depth, &color_type, &interlace_type,
Packit 0ba690
       &compression_type, &filter_method);
Packit 0ba690
Packit 0ba690
    width          - holds the width of the image
Packit 0ba690
                     in pixels (up to 2^31).
Packit 0ba690
    height         - holds the height of the image
Packit 0ba690
                     in pixels (up to 2^31).
Packit 0ba690
    bit_depth      - holds the bit depth of one of the
Packit 0ba690
                     image channels.  (valid values are
Packit 0ba690
                     1, 2, 4, 8, 16 and depend also on
Packit 0ba690
                     the color_type.  See also
Packit 0ba690
                     significant bits (sBIT) below).
Packit 0ba690
    color_type     - describes which color/alpha channels
Packit 0ba690
                         are present.
Packit 0ba690
                     PNG_COLOR_TYPE_GRAY
Packit 0ba690
                        (bit depths 1, 2, 4, 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_GRAY_ALPHA
Packit 0ba690
                        (bit depths 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_PALETTE
Packit 0ba690
                        (bit depths 1, 2, 4, 8)
Packit 0ba690
                     PNG_COLOR_TYPE_RGB
Packit 0ba690
                        (bit_depths 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_RGB_ALPHA
Packit 0ba690
                        (bit_depths 8, 16)
Packit 0ba690
Packit 0ba690
                     PNG_COLOR_MASK_PALETTE
Packit 0ba690
                     PNG_COLOR_MASK_COLOR
Packit 0ba690
                     PNG_COLOR_MASK_ALPHA
Packit 0ba690
Packit 0ba690
    filter_method  - (must be PNG_FILTER_TYPE_BASE
Packit 0ba690
                     for PNG 1.0, and can also be
Packit 0ba690
                     PNG_INTRAPIXEL_DIFFERENCING if
Packit 0ba690
                     the PNG datastream is embedded in
Packit 0ba690
                     a MNG-1.0 datastream)
Packit 0ba690
    compression_type - (must be PNG_COMPRESSION_TYPE_BASE
Packit 0ba690
                     for PNG 1.0)
Packit 0ba690
    interlace_type - (PNG_INTERLACE_NONE or
Packit 0ba690
                     PNG_INTERLACE_ADAM7)
Packit 0ba690
Packit 0ba690
    Any or all of interlace_type, compression_type, or
Packit 0ba690
    filter_method can be NULL if you are
Packit 0ba690
    not interested in their values.
Packit 0ba690
Packit 0ba690
    Note that png_get_IHDR() returns 32-bit data into
Packit 0ba690
    the application's width and height variables.
Packit 0ba690
    This is an unsafe situation if these are 16-bit
Packit 0ba690
    variables.  In such situations, the
Packit 0ba690
    png_get_image_width() and png_get_image_height()
Packit 0ba690
    functions described below are safer.
Packit 0ba690
Packit 0ba690
    width            = png_get_image_width(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    height           = png_get_image_height(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    bit_depth        = png_get_bit_depth(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    color_type       = png_get_color_type(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    filter_method    = png_get_filter_type(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    compression_type = png_get_compression_type(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
    interlace_type   = png_get_interlace_type(png_ptr,
Packit 0ba690
                         info_ptr);
Packit 0ba690
Packit 0ba690
    channels = png_get_channels(png_ptr, info_ptr);
Packit 0ba690
    channels       - number of channels of info for the
Packit 0ba690
                     color type (valid values are 1 (GRAY,
Packit 0ba690
                     PALETTE), 2 (GRAY_ALPHA), 3 (RGB),
Packit 0ba690
                     4 (RGB_ALPHA or RGB + filler byte))
Packit 0ba690
    rowbytes = png_get_rowbytes(png_ptr, info_ptr);
Packit 0ba690
    rowbytes       - number of bytes needed to hold a row
Packit 0ba690
Packit 0ba690
    signature = png_get_signature(png_ptr, info_ptr);
Packit 0ba690
    signature      - holds the signature read from the
Packit 0ba690
                     file (if any).  The data is kept in
Packit 0ba690
                     the same offset it would be if the
Packit 0ba690
                     whole signature were read (i.e. if an
Packit 0ba690
                     application had already read in 4
Packit 0ba690
                     bytes of signature before starting
Packit 0ba690
                     libpng, the remaining 4 bytes would
Packit 0ba690
                     be in signature[4] through signature[7]
Packit 0ba690
                     (see png_set_sig_bytes())).
Packit 0ba690
Packit 0ba690
These are also important, but their validity depends on whether the chunk
Packit 0ba690
has been read.  The png_get_valid(png_ptr, info_ptr, PNG_INFO_<chunk>) and
Packit 0ba690
png_get_<chunk>(png_ptr, info_ptr, ...) functions return non-zero if the
Packit 0ba690
data has been read, or zero if it is missing.  The parameters to the
Packit 0ba690
png_get_<chunk> are set directly if they are simple data types, or a
Packit 0ba690
pointer into the info_ptr is returned for any complex types.
Packit 0ba690
Packit 0ba690
    png_get_PLTE(png_ptr, info_ptr, &palette,
Packit 0ba690
                     &num_palette);
Packit 0ba690
    palette        - the palette for the file
Packit 0ba690
                     (array of png_color)
Packit 0ba690
    num_palette    - number of entries in the palette
Packit 0ba690
Packit 0ba690
    png_get_gAMA(png_ptr, info_ptr, &gamma);
Packit 0ba690
    gamma          - the gamma the file is written
Packit 0ba690
                     at (PNG_INFO_gAMA)
Packit 0ba690
Packit 0ba690
    png_get_sRGB(png_ptr, info_ptr, &srgb_intent);
Packit 0ba690
    srgb_intent    - the rendering intent (PNG_INFO_sRGB)
Packit 0ba690
                     The presence of the sRGB chunk
Packit 0ba690
                     means that the pixel data is in the
Packit 0ba690
                     sRGB color space.  This chunk also
Packit 0ba690
                     implies specific values of gAMA and
Packit 0ba690
                     cHRM.
Packit 0ba690
Packit 0ba690
    png_get_iCCP(png_ptr, info_ptr, &name,
Packit 0ba690
       &compression_type, &profile, &proflen);
Packit 0ba690
    name            - The profile name.
Packit 0ba690
    compression     - The compression type; always
Packit 0ba690
                      PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
Packit 0ba690
                      You may give NULL to this argument to
Packit 0ba690
                      ignore it.
Packit 0ba690
    profile         - International Color Consortium color
Packit 0ba690
                      profile data. May contain NULs.
Packit 0ba690
    proflen         - length of profile data in bytes.
Packit 0ba690
Packit 0ba690
    png_get_sBIT(png_ptr, info_ptr, &sig_bit);
Packit 0ba690
    sig_bit        - the number of significant bits for
Packit 0ba690
                     (PNG_INFO_sBIT) each of the gray,
Packit 0ba690
                     red, green, and blue channels,
Packit 0ba690
                     whichever are appropriate for the
Packit 0ba690
                     given color type (png_color_16)
Packit 0ba690
Packit 0ba690
    png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans,
Packit 0ba690
                     &trans_values);
Packit 0ba690
    trans          - array of transparent
Packit 0ba690
                     entries for palette (PNG_INFO_tRNS)
Packit 0ba690
    trans_values   - graylevel or color sample values of
Packit 0ba690
                     the single transparent color for
Packit 0ba690
                     non-paletted images (PNG_INFO_tRNS)
Packit 0ba690
    num_trans      - number of transparent entries
Packit 0ba690
                     (PNG_INFO_tRNS)
Packit 0ba690
Packit 0ba690
    png_get_hIST(png_ptr, info_ptr, &hist);
Packit 0ba690
                     (PNG_INFO_hIST)
Packit 0ba690
    hist           - histogram of palette (array of
Packit 0ba690
                     png_uint_16)
Packit 0ba690
Packit 0ba690
    png_get_tIME(png_ptr, info_ptr, &mod_time);
Packit 0ba690
    mod_time       - time image was last modified
Packit 0ba690
                    (PNG_VALID_tIME)
Packit 0ba690
Packit 0ba690
    png_get_bKGD(png_ptr, info_ptr, &background);
Packit 0ba690
    background     - background color (PNG_VALID_bKGD)
Packit 0ba690
                     valid 16-bit red, green and blue
Packit 0ba690
                     values, regardless of color_type
Packit 0ba690
Packit 0ba690
    num_comments   = png_get_text(png_ptr, info_ptr,
Packit 0ba690
                     &text_ptr, &num_text);
Packit 0ba690
    num_comments   - number of comments
Packit 0ba690
    text_ptr       - array of png_text holding image
Packit 0ba690
                     comments
Packit 0ba690
    text_ptr[i].compression - type of compression used
Packit 0ba690
                 on "text" PNG_TEXT_COMPRESSION_NONE
Packit 0ba690
                           PNG_TEXT_COMPRESSION_zTXt
Packit 0ba690
                           PNG_ITXT_COMPRESSION_NONE
Packit 0ba690
                           PNG_ITXT_COMPRESSION_zTXt
Packit 0ba690
    text_ptr[i].key   - keyword for comment.  Must contain
Packit 0ba690
                         1-79 characters.
Packit 0ba690
    text_ptr[i].text  - text comments for current
Packit 0ba690
                         keyword.  Can be empty.
Packit 0ba690
    text_ptr[i].text_length - length of text string,
Packit 0ba690
                 after decompression, 0 for iTXt
Packit 0ba690
    text_ptr[i].itxt_length - length of itxt string,
Packit 0ba690
                 after decompression, 0 for tEXt/zTXt
Packit 0ba690
    text_ptr[i].lang  - language of comment (empty
Packit 0ba690
                         string for unknown).
Packit 0ba690
    text_ptr[i].lang_key  - keyword in UTF-8
Packit 0ba690
                         (empty string for unknown).
Packit 0ba690
    Note that the itxt_length, lang, and lang_key
Packit 0ba690
    members of the text_ptr structure only exist
Packit 0ba690
    when the library is built with iTXt chunk support.
Packit 0ba690
Packit 0ba690
    num_text       - number of comments (same as
Packit 0ba690
                     num_comments; you can put NULL here
Packit 0ba690
                     to avoid the duplication)
Packit 0ba690
    Note while png_set_text() will accept text, language,
Packit 0ba690
    and translated keywords that can be NULL pointers, the
Packit 0ba690
    structure returned by png_get_text will always contain
Packit 0ba690
    regular zero-terminated C strings.  They might be
Packit 0ba690
    empty strings but they will never be NULL pointers.
Packit 0ba690
Packit 0ba690
    num_spalettes = png_get_sPLT(png_ptr, info_ptr,
Packit 0ba690
       &palette_ptr);
Packit 0ba690
    palette_ptr    - array of palette structures holding
Packit 0ba690
                     contents of one or more sPLT chunks
Packit 0ba690
                     read.
Packit 0ba690
    num_spalettes  - number of sPLT chunks read.
Packit 0ba690
Packit 0ba690
    png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y,
Packit 0ba690
       &unit_type);
Packit 0ba690
    offset_x       - positive offset from the left edge
Packit 0ba690
                     of the screen
Packit 0ba690
    offset_y       - positive offset from the top edge
Packit 0ba690
                     of the screen
Packit 0ba690
    unit_type      - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
Packit 0ba690
Packit 0ba690
    png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y,
Packit 0ba690
       &unit_type);
Packit 0ba690
    res_x          - pixels/unit physical resolution in
Packit 0ba690
                     x direction
Packit 0ba690
    res_y          - pixels/unit physical resolution in
Packit 0ba690
                     x direction
Packit 0ba690
    unit_type      - PNG_RESOLUTION_UNKNOWN,
Packit 0ba690
                     PNG_RESOLUTION_METER
Packit 0ba690
Packit 0ba690
    png_get_sCAL(png_ptr, info_ptr, &unit, &width,
Packit 0ba690
       &height)
Packit 0ba690
    unit        - physical scale units (an integer)
Packit 0ba690
    width       - width of a pixel in physical scale units
Packit 0ba690
    height      - height of a pixel in physical scale units
Packit 0ba690
                 (width and height are doubles)
Packit 0ba690
Packit 0ba690
    png_get_sCAL_s(png_ptr, info_ptr, &unit, &width,
Packit 0ba690
       &height)
Packit 0ba690
    unit        - physical scale units (an integer)
Packit 0ba690
    width       - width of a pixel in physical scale units
Packit 0ba690
    height      - height of a pixel in physical scale units
Packit 0ba690
                 (width and height are strings like "2.54")
Packit 0ba690
Packit 0ba690
    num_unknown_chunks = png_get_unknown_chunks(png_ptr,
Packit 0ba690
       info_ptr, &unknowns)
Packit 0ba690
    unknowns          - array of png_unknown_chunk
Packit 0ba690
                        structures holding unknown chunks
Packit 0ba690
    unknowns[i].name  - name of unknown chunk
Packit 0ba690
    unknowns[i].data  - data of unknown chunk
Packit 0ba690
    unknowns[i].size  - size of unknown chunk's data
Packit 0ba690
    unknowns[i].location - position of chunk in file
Packit 0ba690
Packit 0ba690
    The value of "i" corresponds to the order in which the
Packit 0ba690
    chunks were read from the PNG file or inserted with the
Packit 0ba690
    png_set_unknown_chunks() function.
Packit 0ba690
Packit 0ba690
The data from the pHYs chunk can be retrieved in several convenient
Packit 0ba690
forms:
Packit 0ba690
Packit 0ba690
    res_x = png_get_x_pixels_per_meter(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    res_y = png_get_y_pixels_per_meter(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    res_x_and_y = png_get_pixels_per_meter(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    res_x = png_get_x_pixels_per_inch(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    res_y = png_get_y_pixels_per_inch(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    res_x_and_y = png_get_pixels_per_inch(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
    aspect_ratio = png_get_pixel_aspect_ratio(png_ptr,
Packit 0ba690
       info_ptr)
Packit 0ba690
Packit 0ba690
   (Each of these returns 0 [signifying "unknown"] if
Packit 0ba690
       the data is not present or if res_x is 0;
Packit 0ba690
       res_x_and_y is 0 if res_x != res_y)
Packit 0ba690
Packit 0ba690
The data from the oFFs chunk can be retrieved in several convenient
Packit 0ba690
forms:
Packit 0ba690
Packit 0ba690
    x_offset = png_get_x_offset_microns(png_ptr, info_ptr);
Packit 0ba690
    y_offset = png_get_y_offset_microns(png_ptr, info_ptr);
Packit 0ba690
    x_offset = png_get_x_offset_inches(png_ptr, info_ptr);
Packit 0ba690
    y_offset = png_get_y_offset_inches(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
   (Each of these returns 0 [signifying "unknown" if both
Packit 0ba690
       x and y are 0] if the data is not present or if the
Packit 0ba690
       chunk is present but the unit is the pixel)
Packit 0ba690
Packit 0ba690
For more information, see the png_info definition in png.h and the
Packit 0ba690
PNG specification for chunk contents.  Be careful with trusting
Packit 0ba690
rowbytes, as some of the transformations could increase the space
Packit 0ba690
needed to hold a row (expand, filler, gray_to_rgb, etc.).
Packit 0ba690
See png_read_update_info(), below.
Packit 0ba690
Packit 0ba690
A quick word about text_ptr and num_text.  PNG stores comments in
Packit 0ba690
keyword/text pairs, one pair per chunk, with no limit on the number
Packit 0ba690
of text chunks, and a 2^31 byte limit on their size.  While there are
Packit 0ba690
suggested keywords, there is no requirement to restrict the use to these
Packit 0ba690
strings.  It is strongly suggested that keywords and text be sensible
Packit 0ba690
to humans (that's the point), so don't use abbreviations.  Non-printing
Packit 0ba690
symbols are not allowed.  See the PNG specification for more details.
Packit 0ba690
There is also no requirement to have text after the keyword.
Packit 0ba690
Packit 0ba690
Keywords should be limited to 79 Latin-1 characters without leading or
Packit 0ba690
trailing spaces, but non-consecutive spaces are allowed within the
Packit 0ba690
keyword.  It is possible to have the same keyword any number of times.
Packit 0ba690
The text_ptr is an array of png_text structures, each holding a
Packit 0ba690
pointer to a language string, a pointer to a keyword and a pointer to
Packit 0ba690
a text string.  The text string, language code, and translated
Packit 0ba690
keyword may be empty or NULL pointers.  The keyword/text
Packit 0ba690
pairs are put into the array in the order that they are received.
Packit 0ba690
However, some or all of the text chunks may be after the image, so, to
Packit 0ba690
make sure you have read all the text chunks, don't mess with these
Packit 0ba690
until after you read the stuff after the image.  This will be
Packit 0ba690
mentioned again below in the discussion that goes with png_read_end().
Packit 0ba690
Packit 0ba690
.SS Input transformations
Packit 0ba690
Packit 0ba690
After you've read the header information, you can set up the library
Packit 0ba690
to handle any special transformations of the image data.  The various
Packit 0ba690
ways to transform the data will be described in the order that they
Packit 0ba690
should occur.  This is important, as some of these change the color
Packit 0ba690
type and/or bit depth of the data, and some others only work on
Packit 0ba690
certain color types and bit depths.  Even though each transformation
Packit 0ba690
checks to see if it has data that it can do something with, you should
Packit 0ba690
make sure to only enable a transformation if it will be valid for the
Packit 0ba690
data.  For example, don't swap red and blue on grayscale data.
Packit 0ba690
Packit 0ba690
The colors used for the background and transparency values should be
Packit 0ba690
supplied in the same format/depth as the current image data.  They
Packit 0ba690
are stored in the same format/depth as the image data in a bKGD or tRNS
Packit 0ba690
chunk, so this is what libpng expects for this data.  The colors are
Packit 0ba690
transformed to keep in sync with the image data when an application
Packit 0ba690
calls the png_read_update_info() routine (see below).
Packit 0ba690
Packit 0ba690
Data will be decoded into the supplied row buffers packed into bytes
Packit 0ba690
unless the library has been told to transform it into another format.
Packit 0ba690
For example, 4 bit/pixel paletted or grayscale data will be returned
Packit 0ba690
2 pixels/byte with the leftmost pixel in the high-order bits of the
Packit 0ba690
byte, unless png_set_packing() is called.  8-bit RGB data will be stored
Packit 0ba690
in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha()
Packit 0ba690
is called to insert filler bytes, either before or after each RGB triplet.
Packit 0ba690
16-bit RGB data will be returned RRGGBB RRGGBB, with the most significant
Packit 0ba690
byte of the color value first, unless png_set_strip_16() is called to
Packit 0ba690
transform it to regular RGB RGB triplets, or png_set_filler() or
Packit 0ba690
png_set_add alpha() is called to insert filler bytes, either before or
Packit 0ba690
after each RRGGBB triplet.  Similarly, 8-bit or 16-bit grayscale data can
Packit 0ba690
be modified with
Packit 0ba690
png_set_filler(), png_set_add_alpha(), or png_set_strip_16().
Packit 0ba690
Packit 0ba690
The following code transforms grayscale images of less than 8 to 8 bits,
Packit 0ba690
changes paletted images to RGB, and adds a full alpha channel if there is
Packit 0ba690
transparency information in a tRNS chunk.  This is most useful on
Packit 0ba690
grayscale images with bit depths of 2 or 4 or if there is a multiple-image
Packit 0ba690
viewing application that wishes to treat all images in the same way.
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
        png_set_palette_to_rgb(png_ptr);
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_GRAY &&
Packit 0ba690
        bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr);
Packit 0ba690
Packit 0ba690
    if (png_get_valid(png_ptr, info_ptr,
Packit 0ba690
        PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
These three functions are actually aliases for png_set_expand(), added
Packit 0ba690
in libpng version 1.0.4, with the function names expanded to improve code
Packit 0ba690
readability.  In some future version they may actually do different
Packit 0ba690
things.
Packit 0ba690
Packit 0ba690
As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was
Packit 0ba690
added.  It expands the sample depth without changing tRNS to alpha.
Packit 0ba690
Packit 0ba690
As of libpng version 1.2.57, not all possible expansions are supported.
Packit 0ba690
Packit 0ba690
In the following table, the 01 means grayscale with depth<8, 31 means
Packit 0ba690
indexed with depth<8, other numerals represent the color type, "T" means
Packit 0ba690
the tRNS chunk is present, A means an alpha channel is present, and O
Packit 0ba690
means tRNS or alpha is present but all pixels in the image are opaque.
Packit 0ba690
Packit 0ba690
  FROM  01  31   0  0T  0O   2  2T  2O   3  3T  3O  4A  4O  6A  6O 
Packit 0ba690
   TO
Packit 0ba690
   01    -                   
Packit 0ba690
   31        -
Packit 0ba690
    0    1       -           
Packit 0ba690
   0T                -
Packit 0ba690
   0O                    -
Packit 0ba690
    2           GX           -
Packit 0ba690
   2T                            -
Packit 0ba690
   2O                                -
Packit 0ba690
    3        1                           -
Packit 0ba690
   3T                                        -
Packit 0ba690
   3O                                            -
Packit 0ba690
   4A                T                               -
Packit 0ba690
   4O                                                    -
Packit 0ba690
   6A               GX         TX           TX               -
Packit 0ba690
   6O                   GX                      TX               -
Packit 0ba690
Packit 0ba690
Within the matrix,
Packit 0ba690
     "-" means the transformation is not supported.
Packit 0ba690
     "X" means the transformation is obtained by png_set_expand().
Packit 0ba690
     "1" means the transformation is obtained by
Packit 0ba690
         png_set_expand_gray_1_2_4_to_8
Packit 0ba690
     "G" means the transformation is obtained by
Packit 0ba690
         png_set_gray_to_rgb().
Packit 0ba690
     "P" means the transformation is obtained by
Packit 0ba690
         png_set_expand_palette_to_rgb().
Packit 0ba690
     "T" means the transformation is obtained by
Packit 0ba690
         png_set_tRNS_to_alpha().
Packit 0ba690
Packit 0ba690
PNG can have files with 16 bits per channel.  If you only can handle
Packit 0ba690
8 bits per channel, this will strip the pixels down to 8 bit.
Packit 0ba690
Packit 0ba690
    if (bit_depth == 16)
Packit 0ba690
        png_set_strip_16(png_ptr);
Packit 0ba690
Packit 0ba690
If, for some reason, you don't need the alpha channel on an image,
Packit 0ba690
and you want to remove it rather than combining it with the background
Packit 0ba690
(but the image author certainly had in mind that you *would* combine
Packit 0ba690
it with the background, so that's what you should probably do):
Packit 0ba690
Packit 0ba690
    if (color_type & PNG_COLOR_MASK_ALPHA)
Packit 0ba690
        png_set_strip_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
In PNG files, the alpha channel in an image
Packit 0ba690
is the level of opacity.  If you need the alpha channel in an image to
Packit 0ba690
be the level of transparency instead of opacity, you can invert the
Packit 0ba690
alpha channel (or the tRNS chunk data) after it's read, so that 0 is
Packit 0ba690
fully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bit
Packit 0ba690
images) is fully transparent, with
Packit 0ba690
Packit 0ba690
    png_set_invert_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
The PNG format only supports pixels with postmultiplied alpha.
Packit 0ba690
If you want to replace the pixels, after reading them, with pixels
Packit 0ba690
that have premultiplied color samples, you can do this with
Packit 0ba690
Packit 0ba690
    png_set_premultiply_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
If you do this, any input with a tRNS chunk will be expanded to
Packit 0ba690
have an alpha channel.
Packit 0ba690
Packit 0ba690
PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
Packit 0ba690
they can, resulting in, for example, 8 pixels per byte for 1 bit
Packit 0ba690
files.  This code expands to 1 pixel per byte without changing the
Packit 0ba690
values of the pixels:
Packit 0ba690
Packit 0ba690
    if (bit_depth < 8)
Packit 0ba690
        png_set_packing(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files have possible bit depths of 1, 2, 4, 8, and 16.  All pixels
Packit 0ba690
stored in a PNG image have been "scaled" or "shifted" up to the next
Packit 0ba690
higher possible bit depth (e.g. from 5 bits/sample in the range [0,31]
Packit 0ba690
to 8 bits/sample in the range [0, 255]).  However, it is also possible
Packit 0ba690
to convert the PNG pixel data back to the original bit depth of the
Packit 0ba690
image.  This call reduces the pixels back down to the original bit depth:
Packit 0ba690
Packit 0ba690
    png_color_8p sig_bit;
Packit 0ba690
Packit 0ba690
    if (png_get_sBIT(png_ptr, info_ptr, &sig_bit))
Packit 0ba690
        png_set_shift(png_ptr, sig_bit);
Packit 0ba690
Packit 0ba690
PNG files store 3-color pixels in red, green, blue order.  This code
Packit 0ba690
changes the storage of the pixels to blue, green, red:
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_RGB ||
Packit 0ba690
        color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
        png_set_bgr(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files store RGB pixels packed into 3 or 6 bytes. This code expands them
Packit 0ba690
into 4 or 8 bytes for windowing systems that need them in this format:
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_RGB)
Packit 0ba690
        png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE);
Packit 0ba690
Packit 0ba690
where "filler" is the 8 or 16-bit number to fill with, and the location is
Packit 0ba690
either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether
Packit 0ba690
you want the filler before the RGB or after.  This transformation
Packit 0ba690
does not affect images that already have full alpha channels.  To add an
Packit 0ba690
opaque alpha channel, use filler=0xff or 0xffff and PNG_FILLER_AFTER which
Packit 0ba690
will generate RGBA pixels.
Packit 0ba690
Packit 0ba690
Note that png_set_filler() does not change the color type.  If you want
Packit 0ba690
to do that, you can add a true alpha channel with
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_RGB ||
Packit 0ba690
           color_type == PNG_COLOR_TYPE_GRAY)
Packit 0ba690
    png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER);
Packit 0ba690
Packit 0ba690
where "filler" contains the alpha value to assign to each pixel.
Packit 0ba690
This function was added in libpng-1.2.7.
Packit 0ba690
Packit 0ba690
If you are reading an image with an alpha channel, and you need the
Packit 0ba690
data as ARGB instead of the normal PNG format RGBA:
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
        png_set_swap_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
For some uses, you may want a grayscale image to be represented as
Packit 0ba690
RGB.  This code will do that conversion:
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_GRAY ||
Packit 0ba690
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Packit 0ba690
          png_set_gray_to_rgb(png_ptr);
Packit 0ba690
Packit 0ba690
Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
Packit 0ba690
with alpha.
Packit 0ba690
Packit 0ba690
    if (color_type == PNG_COLOR_TYPE_RGB ||
Packit 0ba690
        color_type == PNG_COLOR_TYPE_RGB_ALPHA)
Packit 0ba690
          png_set_rgb_to_gray_fixed(png_ptr, error_action,
Packit 0ba690
             int red_weight, int green_weight);
Packit 0ba690
Packit 0ba690
    error_action = 1: silently do the conversion
Packit 0ba690
    error_action = 2: issue a warning if the original
Packit 0ba690
                      image has any pixel where
Packit 0ba690
                      red != green or red != blue
Packit 0ba690
    error_action = 3: issue an error and abort the
Packit 0ba690
                      conversion if the original
Packit 0ba690
                      image has any pixel where
Packit 0ba690
                      red != green or red != blue
Packit 0ba690
Packit 0ba690
    red_weight:       weight of red component times 100000
Packit 0ba690
    green_weight:     weight of green component times 100000
Packit 0ba690
                      If either weight is negative, default
Packit 0ba690
                      weights (21268, 71514) are used.
Packit 0ba690
Packit 0ba690
If you have set error_action = 1 or 2, you can
Packit 0ba690
later check whether the image really was gray, after processing
Packit 0ba690
the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
Packit 0ba690
It will return a png_byte that is zero if the image was gray or
Packit 0ba690
1 if there were any non-gray pixels.  bKGD and sBIT data
Packit 0ba690
will be silently converted to grayscale, using the green channel
Packit 0ba690
data, regardless of the error_action setting.
Packit 0ba690
Packit 0ba690
With red_weight+green_weight<=100000,
Packit 0ba690
the normalized graylevel is computed:
Packit 0ba690
Packit 0ba690
    int rw = red_weight * 65536;
Packit 0ba690
    int gw = green_weight * 65536;
Packit 0ba690
    int bw = 65536 - (rw + gw);
Packit 0ba690
    gray = (rw*red + gw*green + bw*blue)/65536;
Packit 0ba690
Packit 0ba690
The default values come from the PNG file cHRM chunk if present; otherwise, the
Packit 0ba690
defaults correspond to the ITU-R recommendation 709, and also the sRGB color
Packit 0ba690
space, as recommended in the Charles Poynton's Colour FAQ,
Packit 0ba690
Copyright (c) 2006-11-28 Charles Poynton, in section 9:
Packit 0ba690
Packit 0ba690
<http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html#RTFToC9>
Packit 0ba690
Packit 0ba690
    Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
Packit 0ba690
Packit 0ba690
Libpng approximates this with
Packit 0ba690
Packit 0ba690
    Y = 0.21268 * R    + 0.7151 * G    + 0.07217 * B
Packit 0ba690
Packit 0ba690
which can be expressed with integers as
Packit 0ba690
Packit 0ba690
    Y = (6969 * R + 23434 * G + 2365 * B)/32768
Packit 0ba690
Packit 0ba690
The calculation is done in a linear colorspace, if the image gamma
Packit 0ba690
is known.
Packit 0ba690
Packit 0ba690
If you have a grayscale and you are using png_set_expand_depth(),
Packit 0ba690
png_set_expand(), or png_set_gray_to_rgb to change to truecolor or to
Packit 0ba690
a higher bit-depth, you must either supply the background color as a gray
Packit 0ba690
value at the original file bit-depth (need_expand = 1) or else supply the
Packit 0ba690
background color as an RGB triplet at the final, expanded bit depth
Packit 0ba690
(need_expand = 0).  Similarly, if you are reading a paletted image, you
Packit 0ba690
must either supply the background color as a palette index (need_expand = 1)
Packit 0ba690
or as an RGB triplet that may or may not be in the palette (need_expand = 0).
Packit 0ba690
Packit 0ba690
    png_color_16 my_background;
Packit 0ba690
    png_color_16p image_background;
Packit 0ba690
Packit 0ba690
    if (png_get_bKGD(png_ptr, info_ptr, &image_background))
Packit 0ba690
        png_set_background(png_ptr, image_background,
Packit 0ba690
          PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Packit 0ba690
    else
Packit 0ba690
        png_set_background(png_ptr, &my_background,
Packit 0ba690
          PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
Packit 0ba690
Packit 0ba690
The png_set_background() function tells libpng to composite images
Packit 0ba690
with alpha or simple transparency against the supplied background
Packit 0ba690
color.  If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid),
Packit 0ba690
you may use this color, or supply another color more suitable for
Packit 0ba690
the current display (e.g., the background color from a web page).  You
Packit 0ba690
need to tell libpng whether the color is in the gamma space of the
Packit 0ba690
display (PNG_BACKGROUND_GAMMA_SCREEN for colors you supply), the file
Packit 0ba690
(PNG_BACKGROUND_GAMMA_FILE for colors from the bKGD chunk), or one
Packit 0ba690
that is neither of these gammas (PNG_BACKGROUND_GAMMA_UNIQUE - I don't
Packit 0ba690
know why anyone would use this, but it's here).
Packit 0ba690
Packit 0ba690
To properly display PNG images on any kind of system, the application needs
Packit 0ba690
to know what the display gamma is.  Ideally, the user will know this, and
Packit 0ba690
the application will allow them to set it.  One method of allowing the user
Packit 0ba690
to set the display gamma separately for each system is to check for a
Packit 0ba690
SCREEN_GAMMA or DISPLAY_GAMMA environment variable, which will hopefully be
Packit 0ba690
correctly set.
Packit 0ba690
Packit 0ba690
Note that display_gamma is the overall gamma correction required to produce
Packit 0ba690
pleasing results, which depends on the lighting conditions in the surrounding
Packit 0ba690
environment.  In a dim or brightly lit room, no compensation other than
Packit 0ba690
the physical gamma exponent of the monitor is needed, while in a dark room
Packit 0ba690
a slightly smaller exponent is better.
Packit 0ba690
Packit 0ba690
   double gamma, screen_gamma;
Packit 0ba690
Packit 0ba690
   if (/* We have a user-defined screen
Packit 0ba690
       gamma value */)
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = user_defined_screen_gamma;
Packit 0ba690
   }
Packit 0ba690
   /* One way that applications can share the same
Packit 0ba690
      screen gamma value */
Packit 0ba690
   else if ((gamma_str = getenv("SCREEN_GAMMA"))
Packit 0ba690
      != NULL)
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = (double)atof(gamma_str);
Packit 0ba690
   }
Packit 0ba690
   /* If we don't have another value */
Packit 0ba690
   else
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = 2.2; /* A good guess for a
Packit 0ba690
           PC monitor in a bright office or a dim room */
Packit 0ba690
      screen_gamma = 2.0; /* A good guess for a
Packit 0ba690
           PC monitor in a dark room */
Packit 0ba690
      screen_gamma = 1.7 or 1.0;  /* A good
Packit 0ba690
           guess for Mac systems */
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
The png_set_gamma() function handles gamma transformations of the data.
Packit 0ba690
Pass both the file gamma and the current screen_gamma.  If the file does
Packit 0ba690
not have a gamma value, you can pass one anyway if you have an idea what
Packit 0ba690
it is (usually 0.45455 is a good guess for GIF images on PCs).  Note
Packit 0ba690
that file gammas are inverted from screen gammas.  See the discussions
Packit 0ba690
on gamma in the PNG specification for an excellent description of what
Packit 0ba690
gamma is, and why all applications should support it.  It is strongly
Packit 0ba690
recommended that PNG viewers support gamma correction.
Packit 0ba690
Packit 0ba690
   if (png_get_gAMA(png_ptr, info_ptr, &gamma))
Packit 0ba690
      png_set_gamma(png_ptr, screen_gamma, gamma);
Packit 0ba690
   else
Packit 0ba690
      png_set_gamma(png_ptr, screen_gamma, 0.45455);
Packit 0ba690
Packit 0ba690
If you need to reduce an RGB file to a paletted file, or if a paletted
Packit 0ba690
file has more entries then will fit on your screen, png_set_dither()
Packit 0ba690
will do that.  Note that this is a simple match dither that merely
Packit 0ba690
finds the closest color available.  This should work fairly well with
Packit 0ba690
optimized palettes, and fairly badly with linear color cubes.  If you
Packit 0ba690
pass a palette that is larger then maximum_colors, the file will
Packit 0ba690
reduce the number of colors in the palette so it will fit into
Packit 0ba690
maximum_colors.  If there is a histogram, it will use it to make
Packit 0ba690
more intelligent choices when reducing the palette.  If there is no
Packit 0ba690
histogram, it may not do as good a job.
Packit 0ba690
Packit 0ba690
   if (color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
   {
Packit 0ba690
      if (png_get_valid(png_ptr, info_ptr,
Packit 0ba690
         PNG_INFO_PLTE))
Packit 0ba690
      {
Packit 0ba690
         png_uint_16p histogram = NULL;
Packit 0ba690
Packit 0ba690
         png_get_hIST(png_ptr, info_ptr,
Packit 0ba690
            &histogram);
Packit 0ba690
         png_set_dither(png_ptr, palette, num_palette,
Packit 0ba690
            max_screen_colors, histogram, 1);
Packit 0ba690
      }
Packit 0ba690
      else
Packit 0ba690
      {
Packit 0ba690
         png_color std_color_cube[MAX_SCREEN_COLORS] =
Packit 0ba690
            { ... colors ... };
Packit 0ba690
Packit 0ba690
         png_set_dither(png_ptr, std_color_cube,
Packit 0ba690
            MAX_SCREEN_COLORS, MAX_SCREEN_COLORS,
Packit 0ba690
            NULL,0);
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
PNG files describe monochrome as black being zero and white being one.
Packit 0ba690
The following code will reverse this (make black be one and white be
Packit 0ba690
zero):
Packit 0ba690
Packit 0ba690
   if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY)
Packit 0ba690
      png_set_invert_mono(png_ptr);
Packit 0ba690
Packit 0ba690
This function can also be used to invert grayscale and gray-alpha images:
Packit 0ba690
Packit 0ba690
   if (color_type == PNG_COLOR_TYPE_GRAY ||
Packit 0ba690
        color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
Packit 0ba690
      png_set_invert_mono(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files store 16 bit pixels in network byte order (big-endian,
Packit 0ba690
ie. most significant bits first).  This code changes the storage to the
Packit 0ba690
other way (little-endian, i.e. least significant bits first, the
Packit 0ba690
way PCs store them):
Packit 0ba690
Packit 0ba690
    if (bit_depth == 16)
Packit 0ba690
        png_set_swap(png_ptr);
Packit 0ba690
Packit 0ba690
If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
Packit 0ba690
need to change the order the pixels are packed into bytes, you can use:
Packit 0ba690
Packit 0ba690
    if (bit_depth < 8)
Packit 0ba690
       png_set_packswap(png_ptr);
Packit 0ba690
Packit 0ba690
Finally, you can write your own transformation function if none of
Packit 0ba690
the existing ones meets your needs.  This is done by setting a callback
Packit 0ba690
with
Packit 0ba690
Packit 0ba690
    png_set_read_user_transform_fn(png_ptr,
Packit 0ba690
       read_transform_fn);
Packit 0ba690
Packit 0ba690
You must supply the function
Packit 0ba690
Packit 0ba690
    void read_transform_fn(png_ptr ptr, row_info_ptr
Packit 0ba690
       row_info, png_bytep data)
Packit 0ba690
Packit 0ba690
See pngtest.c for a working example.  Your function will be called
Packit 0ba690
after all of the other transformations have been processed.
Packit 0ba690
Packit 0ba690
You can also set up a pointer to a user structure for use by your
Packit 0ba690
callback function, and you can inform libpng that your transform
Packit 0ba690
function will change the number of channels or bit depth with the
Packit 0ba690
function
Packit 0ba690
Packit 0ba690
    png_set_user_transform_info(png_ptr, user_ptr,
Packit 0ba690
       user_depth, user_channels);
Packit 0ba690
Packit 0ba690
The user's application, not libpng, is responsible for allocating and
Packit 0ba690
freeing any memory required for the user structure.
Packit 0ba690
Packit 0ba690
You can retrieve the pointer via the function
Packit 0ba690
png_get_user_transform_ptr().  For example:
Packit 0ba690
Packit 0ba690
    voidp read_user_transform_ptr =
Packit 0ba690
       png_get_user_transform_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
The last thing to handle is interlacing; this is covered in detail below,
Packit 0ba690
but you must call the function here if you want libpng to handle expansion
Packit 0ba690
of the interlaced image.
Packit 0ba690
Packit 0ba690
    number_of_passes = png_set_interlace_handling(png_ptr);
Packit 0ba690
Packit 0ba690
After setting the transformations, libpng can update your png_info
Packit 0ba690
structure to reflect any transformations you've requested with this
Packit 0ba690
call.  This is most useful to update the info structure's rowbytes
Packit 0ba690
field so you can use it to allocate your image memory.  This function
Packit 0ba690
will also update your palette with the correct screen_gamma and
Packit 0ba690
background if these have been given with the calls above.
Packit 0ba690
Packit 0ba690
    png_read_update_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
After you call png_read_update_info(), you can allocate any
Packit 0ba690
memory you need to hold the image.  The row data is simply
Packit 0ba690
raw byte data for all forms of images.  As the actual allocation
Packit 0ba690
varies among applications, no example will be given.  If you
Packit 0ba690
are allocating one large chunk, you will need to build an
Packit 0ba690
array of pointers to each row, as it will be needed for some
Packit 0ba690
of the functions below.
Packit 0ba690
Packit 0ba690
.SS Reading image data
Packit 0ba690
Packit 0ba690
After you've allocated memory, you can read the image data.
Packit 0ba690
The simplest way to do this is in one function call.  If you are
Packit 0ba690
allocating enough memory to hold the whole image, you can just
Packit 0ba690
call png_read_image() and libpng will read in all the image data
Packit 0ba690
and put it in the memory area supplied.  You will need to pass in
Packit 0ba690
an array of pointers to each row.
Packit 0ba690
Packit 0ba690
This function automatically handles interlacing, so you don't need
Packit 0ba690
to call png_set_interlace_handling() or call this function multiple
Packit 0ba690
times, or any of that other stuff necessary with png_read_rows().
Packit 0ba690
Packit 0ba690
   png_read_image(png_ptr, row_pointers);
Packit 0ba690
Packit 0ba690
where row_pointers is:
Packit 0ba690
Packit 0ba690
   png_bytep row_pointers[height];
Packit 0ba690
Packit 0ba690
You can point to void or char or whatever you use for pixels.
Packit 0ba690
Packit 0ba690
If you don't want to read in the whole image at once, you can
Packit 0ba690
use png_read_rows() instead.  If there is no interlacing (check
Packit 0ba690
interlace_type == PNG_INTERLACE_NONE), this is simple:
Packit 0ba690
Packit 0ba690
    png_read_rows(png_ptr, row_pointers, NULL,
Packit 0ba690
       number_of_rows);
Packit 0ba690
Packit 0ba690
where row_pointers is the same as in the png_read_image() call.
Packit 0ba690
Packit 0ba690
If you are doing this just one row at a time, you can do this with
Packit 0ba690
a single row_pointer instead of an array of row_pointers:
Packit 0ba690
Packit 0ba690
    png_bytep row_pointer = row;
Packit 0ba690
    png_read_row(png_ptr, row_pointer, NULL);
Packit 0ba690
Packit 0ba690
If the file is interlaced (interlace_type != 0 in the IHDR chunk), things
Packit 0ba690
get somewhat harder.  The only current (PNG Specification version 1.2)
Packit 0ba690
interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7)
Packit 0ba690
is a somewhat complicated 2D interlace scheme, known as Adam7, that
Packit 0ba690
breaks down an image into seven smaller images of varying size, based
Packit 0ba690
on an 8x8 grid.
Packit 0ba690
Packit 0ba690
libpng can fill out those images or it can give them to you "as is".
Packit 0ba690
If you want them filled out, there are two ways to do that.  The one
Packit 0ba690
mentioned in the PNG specification is to expand each pixel to cover
Packit 0ba690
those pixels that have not been read yet (the "rectangle" method).
Packit 0ba690
This results in a blocky image for the first pass, which gradually
Packit 0ba690
smooths out as more pixels are read.  The other method is the "sparkle"
Packit 0ba690
method, where pixels are drawn only in their final locations, with the
Packit 0ba690
rest of the image remaining whatever colors they were initialized to
Packit 0ba690
before the start of the read.  The first method usually looks better,
Packit 0ba690
but tends to be slower, as there are more pixels to put in the rows.
Packit 0ba690
Packit 0ba690
If you don't want libpng to handle the interlacing details, just call
Packit 0ba690
png_read_rows() seven times to read in all seven images.  Each of the
Packit 0ba690
images is a valid image by itself, or they can all be combined on an
Packit 0ba690
8x8 grid to form a single image (although if you intend to combine them
Packit 0ba690
you would be far better off using the libpng interlace handling).
Packit 0ba690
Packit 0ba690
The first pass will return an image 1/8 as wide as the entire image
Packit 0ba690
(every 8th column starting in column 0) and 1/8 as high as the original
Packit 0ba690
(every 8th row starting in row 0), the second will be 1/8 as wide
Packit 0ba690
(starting in column 4) and 1/8 as high (also starting in row 0).  The
Packit 0ba690
third pass will be 1/4 as wide (every 4th pixel starting in column 0) and
Packit 0ba690
1/8 as high (every 8th row starting in row 4), and the fourth pass will
Packit 0ba690
be 1/4 as wide and 1/4 as high (every 4th column starting in column 2,
Packit 0ba690
and every 4th row starting in row 0).  The fifth pass will return an
Packit 0ba690
image 1/2 as wide, and 1/4 as high (starting at column 0 and row 2),
Packit 0ba690
while the sixth pass will be 1/2 as wide and 1/2 as high as the original
Packit 0ba690
(starting in column 1 and row 0).  The seventh and final pass will be as
Packit 0ba690
wide as the original, and 1/2 as high, containing all of the odd
Packit 0ba690
numbered scanlines.  Phew!
Packit 0ba690
Packit 0ba690
If you want libpng to expand the images, call this before calling
Packit 0ba690
png_start_read_image() or png_read_update_info():
Packit 0ba690
Packit 0ba690
    if (interlace_type == PNG_INTERLACE_ADAM7)
Packit 0ba690
        number_of_passes
Packit 0ba690
           = png_set_interlace_handling(png_ptr);
Packit 0ba690
Packit 0ba690
This will return the number of passes needed.  Currently, this
Packit 0ba690
is seven, but may change if another interlace type is added.
Packit 0ba690
This function can be called even if the file is not interlaced,
Packit 0ba690
where it will return one pass.
Packit 0ba690
Packit 0ba690
If you are not going to display the image after each pass, but are
Packit 0ba690
going to wait until the entire image is read in, use the sparkle
Packit 0ba690
effect.  This effect is faster and the end result of either method
Packit 0ba690
is exactly the same.  If you are planning on displaying the image
Packit 0ba690
after each pass, the "rectangle" effect is generally considered the
Packit 0ba690
better looking one.
Packit 0ba690
Packit 0ba690
If you only want the "sparkle" effect, just call png_read_rows() as
Packit 0ba690
normal, with the third parameter NULL.  Make sure you make pass over
Packit 0ba690
the image number_of_passes times, and you don't change the data in the
Packit 0ba690
rows between calls.  You can change the locations of the data, just
Packit 0ba690
not the data.  Each pass only writes the pixels appropriate for that
Packit 0ba690
pass, and assumes the data from previous passes is still valid.
Packit 0ba690
Packit 0ba690
    png_read_rows(png_ptr, row_pointers, NULL,
Packit 0ba690
       number_of_rows);
Packit 0ba690
Packit 0ba690
If you only want the first effect (the rectangles), do the same as
Packit 0ba690
before except pass the row buffer in the third parameter, and leave
Packit 0ba690
the second parameter NULL.
Packit 0ba690
Packit 0ba690
    png_read_rows(png_ptr, NULL, row_pointers,
Packit 0ba690
       number_of_rows);
Packit 0ba690
Packit 0ba690
.SS Finishing a sequential read
Packit 0ba690
Packit 0ba690
After you are finished reading the image through the
Packit 0ba690
low-level interface, you can finish reading the file.  If you are
Packit 0ba690
interested in comments or time, which may be stored either before or
Packit 0ba690
after the image data, you should pass the separate png_info struct if
Packit 0ba690
you want to keep the comments from before and after the image
Packit 0ba690
separate.  If you are not interested, you can pass NULL.
Packit 0ba690
Packit 0ba690
   png_read_end(png_ptr, end_info);
Packit 0ba690
Packit 0ba690
When you are done, you can free all memory allocated by libpng like this:
Packit 0ba690
Packit 0ba690
   png_destroy_read_struct(&png_ptr, &info_ptr,
Packit 0ba690
       &end_info);
Packit 0ba690
Packit 0ba690
It is also possible to individually free the info_ptr members that
Packit 0ba690
point to libpng-allocated storage with the following function:
Packit 0ba690
Packit 0ba690
    png_free_data(png_ptr, info_ptr, mask, seq)
Packit 0ba690
    mask - identifies data to be freed, a mask
Packit 0ba690
           containing the bitwise OR of one or
Packit 0ba690
           more of
Packit 0ba690
             PNG_FREE_PLTE, PNG_FREE_TRNS,
Packit 0ba690
             PNG_FREE_HIST, PNG_FREE_ICCP,
Packit 0ba690
             PNG_FREE_PCAL, PNG_FREE_ROWS,
Packit 0ba690
             PNG_FREE_SCAL, PNG_FREE_SPLT,
Packit 0ba690
             PNG_FREE_TEXT, PNG_FREE_UNKN,
Packit 0ba690
           or simply PNG_FREE_ALL
Packit 0ba690
    seq  - sequence number of item to be freed
Packit 0ba690
           (\-1 for all items)
Packit 0ba690
Packit 0ba690
This function may be safely called when the relevant storage has
Packit 0ba690
already been freed, or has not yet been allocated, or was allocated
Packit 0ba690
by the user and not by libpng,  and will in those cases do nothing.
Packit 0ba690
The "seq" parameter is ignored if only one item of the selected data
Packit 0ba690
type, such as PLTE, is allowed.  If "seq" is not \-1, and multiple items
Packit 0ba690
are allowed for the data type identified in the mask, such as text or
Packit 0ba690
sPLT, only the n'th item in the structure is freed, where n is "seq".
Packit 0ba690
Packit 0ba690
The default behavior is only to free data that was allocated internally
Packit 0ba690
by libpng.  This can be changed, so that libpng will not free the data,
Packit 0ba690
or so that it will free data that was allocated by the user with png_malloc()
Packit 0ba690
or png_zalloc() and passed in via a png_set_*() function, with
Packit 0ba690
Packit 0ba690
    png_data_freer(png_ptr, info_ptr, freer, mask)
Packit 0ba690
    mask   - which data elements are affected
Packit 0ba690
             same choices as in png_free_data()
Packit 0ba690
    freer  - one of
Packit 0ba690
               PNG_DESTROY_WILL_FREE_DATA
Packit 0ba690
               PNG_SET_WILL_FREE_DATA
Packit 0ba690
               PNG_USER_WILL_FREE_DATA
Packit 0ba690
Packit 0ba690
This function only affects data that has already been allocated.
Packit 0ba690
You can call this function after reading the PNG data but before calling
Packit 0ba690
any png_set_*() functions, to control whether the user or the png_set_*()
Packit 0ba690
function is responsible for freeing any existing data that might be present,
Packit 0ba690
and again after the png_set_*() functions to control whether the user
Packit 0ba690
or png_destroy_*() is supposed to free the data.  When the user assumes
Packit 0ba690
responsibility for libpng-allocated data, the application must use
Packit 0ba690
png_free() to free it, and when the user transfers responsibility to libpng
Packit 0ba690
for data that the user has allocated, the user must have used png_malloc()
Packit 0ba690
or png_zalloc() to allocate it.
Packit 0ba690
Packit 0ba690
If you allocated your row_pointers in a single block, as suggested above in
Packit 0ba690
the description of the high level read interface, you must not transfer
Packit 0ba690
responsibility for freeing it to the png_set_rows or png_read_destroy function,
Packit 0ba690
because they would also try to free the individual row_pointers[i].
Packit 0ba690
Packit 0ba690
If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword
Packit 0ba690
separately, do not transfer responsibility for freeing text_ptr to libpng,
Packit 0ba690
because when libpng fills a png_text structure it combines these members with
Packit 0ba690
the key member, and png_free_data() will free only text_ptr.key.  Similarly,
Packit 0ba690
if you transfer responsibility for free'ing text_ptr from libpng to your
Packit 0ba690
application, your application must not separately free those members.
Packit 0ba690
Packit 0ba690
The png_free_data() function will turn off the "valid" flag for anything
Packit 0ba690
it frees.  If you need to turn the flag off for a chunk that was freed by
Packit 0ba690
your application instead of by libpng, you can use
Packit 0ba690
Packit 0ba690
    png_set_invalid(png_ptr, info_ptr, mask);
Packit 0ba690
    mask - identifies the chunks to be made invalid,
Packit 0ba690
           containing the bitwise OR of one or
Packit 0ba690
           more of
Packit 0ba690
             PNG_INFO_gAMA, PNG_INFO_sBIT,
Packit 0ba690
             PNG_INFO_cHRM, PNG_INFO_PLTE,
Packit 0ba690
             PNG_INFO_tRNS, PNG_INFO_bKGD,
Packit 0ba690
             PNG_INFO_hIST, PNG_INFO_pHYs,
Packit 0ba690
             PNG_INFO_oFFs, PNG_INFO_tIME,
Packit 0ba690
             PNG_INFO_pCAL, PNG_INFO_sRGB,
Packit 0ba690
             PNG_INFO_iCCP, PNG_INFO_sPLT,
Packit 0ba690
             PNG_INFO_sCAL, PNG_INFO_IDAT
Packit 0ba690
Packit 0ba690
For a more compact example of reading a PNG image, see the file example.c.
Packit 0ba690
Packit 0ba690
.SS Reading PNG files progressively
Packit 0ba690
Packit 0ba690
The progressive reader is slightly different then the non-progressive
Packit 0ba690
reader.  Instead of calling png_read_info(), png_read_rows(), and
Packit 0ba690
png_read_end(), you make one call to png_process_data(), which calls
Packit 0ba690
callbacks when it has the info, a row, or the end of the image.  You
Packit 0ba690
set up these callbacks with png_set_progressive_read_fn().  You don't
Packit 0ba690
have to worry about the input/output functions of libpng, as you are
Packit 0ba690
giving the library the data directly in png_process_data().  I will
Packit 0ba690
assume that you have read the section on reading PNG files above,
Packit 0ba690
so I will only highlight the differences (although I will show
Packit 0ba690
all of the code).
Packit 0ba690
Packit 0ba690
png_structp png_ptr;
Packit 0ba690
png_infop info_ptr;
Packit 0ba690
Packit 0ba690
 /*  An example code fragment of how you would
Packit 0ba690
     initialize the progressive reader in your
Packit 0ba690
     application. */
Packit 0ba690
 int
Packit 0ba690
 initialize_png_reader()
Packit 0ba690
 {
Packit 0ba690
    png_ptr = png_create_read_struct
Packit 0ba690
        (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Packit 0ba690
         user_error_fn, user_warning_fn);
Packit 0ba690
    if (!png_ptr)
Packit 0ba690
        return (ERROR);
Packit 0ba690
    info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
    if (!info_ptr)
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr, (png_infopp)NULL,
Packit 0ba690
           (png_infopp)NULL);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr, &info_ptr,
Packit 0ba690
           (png_infopp)NULL);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    /* This one's new.  You can provide functions
Packit 0ba690
       to be called when the header info is valid,
Packit 0ba690
       when each row is completed, and when the image
Packit 0ba690
       is finished.  If you aren't using all functions,
Packit 0ba690
       you can specify NULL parameters.  Even when all
Packit 0ba690
       three functions are NULL, you need to call
Packit 0ba690
       png_set_progressive_read_fn().  You can use
Packit 0ba690
       any struct as the user_ptr (cast to a void pointer
Packit 0ba690
       for the function call), and retrieve the pointer
Packit 0ba690
       from inside the callbacks using the function
Packit 0ba690
Packit 0ba690
          png_get_progressive_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
       which will return a void pointer, which you have
Packit 0ba690
       to cast appropriately.
Packit 0ba690
     */
Packit 0ba690
    png_set_progressive_read_fn(png_ptr, (void *)user_ptr,
Packit 0ba690
        info_callback, row_callback, end_callback);
Packit 0ba690
Packit 0ba690
    return 0;
Packit 0ba690
 }
Packit 0ba690
Packit 0ba690
 /* A code fragment that you call as you receive blocks
Packit 0ba690
   of data */
Packit 0ba690
 int
Packit 0ba690
 process_data(png_bytep buffer, png_uint_32 length)
Packit 0ba690
 {
Packit 0ba690
    if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
    {
Packit 0ba690
        png_destroy_read_struct(&png_ptr, &info_ptr,
Packit 0ba690
           (png_infopp)NULL);
Packit 0ba690
        return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    /* This one's new also.  Simply give it a chunk
Packit 0ba690
       of data from the file stream (in order, of
Packit 0ba690
       course).  On machines with segmented memory
Packit 0ba690
       models machines, don't give it any more than
Packit 0ba690
       64K.  The library seems to run fine with sizes
Packit 0ba690
       of 4K. Although you can give it much less if
Packit 0ba690
       necessary (I assume you can give it chunks of
Packit 0ba690
       1 byte, I haven't tried less then 256 bytes
Packit 0ba690
       yet).  When this function returns, you may
Packit 0ba690
       want to display any rows that were generated
Packit 0ba690
       in the row callback if you don't already do
Packit 0ba690
       so there.
Packit 0ba690
     */
Packit 0ba690
    png_process_data(png_ptr, info_ptr, buffer, length);
Packit 0ba690
    return 0;
Packit 0ba690
 }
Packit 0ba690
Packit 0ba690
 /* This function is called (as set by
Packit 0ba690
    png_set_progressive_read_fn() above) when enough data
Packit 0ba690
    has been supplied so all of the header has been
Packit 0ba690
    read.
Packit 0ba690
 */
Packit 0ba690
 void
Packit 0ba690
 info_callback(png_structp png_ptr, png_infop info)
Packit 0ba690
 {
Packit 0ba690
    /* Do any setup here, including setting any of
Packit 0ba690
       the transformations mentioned in the Reading
Packit 0ba690
       PNG files section.  For now, you _must_ call
Packit 0ba690
       either png_start_read_image() or
Packit 0ba690
       png_read_update_info() after all the
Packit 0ba690
       transformations are set (even if you don't set
Packit 0ba690
       any).  You may start getting rows before
Packit 0ba690
       png_process_data() returns, so this is your
Packit 0ba690
       last chance to prepare for that.
Packit 0ba690
     */
Packit 0ba690
 }
Packit 0ba690
Packit 0ba690
 /* This function is called when each row of image
Packit 0ba690
    data is complete */
Packit 0ba690
 void
Packit 0ba690
 row_callback(png_structp png_ptr, png_bytep new_row,
Packit 0ba690
    png_uint_32 row_num, int pass)
Packit 0ba690
 {
Packit 0ba690
    /* If the image is interlaced, and you turned
Packit 0ba690
       on the interlace handler, this function will
Packit 0ba690
       be called for every row in every pass.  Some
Packit 0ba690
       of these rows will not be changed from the
Packit 0ba690
       previous pass.  When the row is not changed,
Packit 0ba690
       the new_row variable will be NULL.  The rows
Packit 0ba690
       and passes are called in order, so you don't
Packit 0ba690
       really need the row_num and pass, but I'm
Packit 0ba690
       supplying them because it may make your life
Packit 0ba690
       easier.
Packit 0ba690
Packit 0ba690
       For the non-NULL rows of interlaced images,
Packit 0ba690
       you must call png_progressive_combine_row()
Packit 0ba690
       passing in the row and the old row.  You can
Packit 0ba690
       call this function for NULL rows (it will just
Packit 0ba690
       return) and for non-interlaced images (it just
Packit 0ba690
       does the memcpy for you) if it will make the
Packit 0ba690
       code easier.  Thus, you can just do this for
Packit 0ba690
       all cases:
Packit 0ba690
     */
Packit 0ba690
Packit 0ba690
        png_progressive_combine_row(png_ptr, old_row,
Packit 0ba690
          new_row);
Packit 0ba690
Packit 0ba690
    /* where old_row is what was displayed for
Packit 0ba690
       previously for the row.  Note that the first
Packit 0ba690
       pass (pass == 0, really) will completely cover
Packit 0ba690
       the old row, so the rows do not have to be
Packit 0ba690
       initialized.  After the first pass (and only
Packit 0ba690
       for interlaced images), you will have to pass
Packit 0ba690
       the current row, and the function will combine
Packit 0ba690
       the old row and the new row.
Packit 0ba690
    */
Packit 0ba690
 }
Packit 0ba690
Packit 0ba690
 void
Packit 0ba690
 end_callback(png_structp png_ptr, png_infop info)
Packit 0ba690
 {
Packit 0ba690
    /* This function is called after the whole image
Packit 0ba690
       has been read, including any chunks after the
Packit 0ba690
       image (up to and including the IEND).  You
Packit 0ba690
       will usually have the same info chunk as you
Packit 0ba690
       had in the header, although some data may have
Packit 0ba690
       been added to the comments and time fields.
Packit 0ba690
Packit 0ba690
       Most people won't do much here, perhaps setting
Packit 0ba690
       a flag that marks the image as finished.
Packit 0ba690
     */
Packit 0ba690
 }
Packit 0ba690
Packit 0ba690
Packit 0ba690
Packit 0ba690
.SH IV. Writing
Packit 0ba690
Packit 0ba690
Much of this is very similar to reading.  However, everything of
Packit 0ba690
importance is repeated here, so you won't have to constantly look
Packit 0ba690
back up in the reading section to understand writing.
Packit 0ba690
Packit 0ba690
.SS Setup
Packit 0ba690
Packit 0ba690
You will want to do the I/O initialization before you get into libpng,
Packit 0ba690
so if it doesn't work, you don't have anything to undo. If you are not
Packit 0ba690
using the standard I/O functions, you will need to replace them with
Packit 0ba690
custom writing functions.  See the discussion under Customizing libpng.
Packit 0ba690
Packit 0ba690
    FILE *fp = fopen(file_name, "wb");
Packit 0ba690
    if (!fp)
Packit 0ba690
    {
Packit 0ba690
       return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
Next, png_struct and png_info need to be allocated and initialized.
Packit 0ba690
As these can be both relatively large, you may not want to store these
Packit 0ba690
on the stack, unless you have stack space to spare.  Of course, you
Packit 0ba690
will want to check if they return NULL.  If you are also reading,
Packit 0ba690
you won't want to name your read structure and your write structure
Packit 0ba690
both "png_ptr"; you can call them anything you like, such as
Packit 0ba690
"read_ptr" and "write_ptr".  Look at pngtest.c, for example.
Packit 0ba690
Packit 0ba690
    png_structp png_ptr = png_create_write_struct
Packit 0ba690
       (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Packit 0ba690
        user_error_fn, user_warning_fn);
Packit 0ba690
    if (!png_ptr)
Packit 0ba690
       return (ERROR);
Packit 0ba690
Packit 0ba690
    png_infop info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
    if (!info_ptr)
Packit 0ba690
    {
Packit 0ba690
       png_destroy_write_struct(&png_ptr,
Packit 0ba690
         (png_infopp)NULL);
Packit 0ba690
       return (ERROR);
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
If you want to use your own memory allocation routines,
Packit 0ba690
define PNG_USER_MEM_SUPPORTED and use
Packit 0ba690
png_create_write_struct_2() instead of png_create_write_struct():
Packit 0ba690
Packit 0ba690
    png_structp png_ptr = png_create_write_struct_2
Packit 0ba690
       (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,
Packit 0ba690
        user_error_fn, user_warning_fn, (png_voidp)
Packit 0ba690
        user_mem_ptr, user_malloc_fn, user_free_fn);
Packit 0ba690
Packit 0ba690
After you have these structures, you will need to set up the
Packit 0ba690
error handling.  When libpng encounters an error, it expects to
Packit 0ba690
longjmp() back to your routine.  Therefore, you will need to call
Packit 0ba690
setjmp() and pass the png_jmpbuf(png_ptr).  If you
Packit 0ba690
write the file from different routines, you will need to update
Packit 0ba690
the png_jmpbuf(png_ptr) every time you enter a new routine that will
Packit 0ba690
call a png_*() function.  See your documentation of setjmp/longjmp
Packit 0ba690
for your compiler for more information on setjmp/longjmp.  See
Packit 0ba690
the discussion on libpng error handling in the Customizing Libpng
Packit 0ba690
section below for more information on the libpng error handling.
Packit 0ba690
Packit 0ba690
    if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
    {
Packit 0ba690
       png_destroy_write_struct(&png_ptr, &info_ptr);
Packit 0ba690
       fclose(fp);
Packit 0ba690
       return (ERROR);
Packit 0ba690
    }
Packit 0ba690
    ...
Packit 0ba690
    return;
Packit 0ba690
Packit 0ba690
If you would rather avoid the complexity of setjmp/longjmp issues,
Packit 0ba690
you can compile libpng with PNG_SETJMP_NOT_SUPPORTED, in which case
Packit 0ba690
errors will result in a call to PNG_ABORT() which defaults to abort().
Packit 0ba690
Packit 0ba690
Now you need to set up the output code.  The default for libpng is to
Packit 0ba690
use the C function fwrite().  If you use this, you will need to pass a
Packit 0ba690
valid FILE * in the function png_init_io().  Be sure that the file is
Packit 0ba690
opened in binary mode.  Again, if you wish to handle writing data in
Packit 0ba690
another way, see the discussion on libpng I/O handling in the Customizing
Packit 0ba690
Libpng section below.
Packit 0ba690
Packit 0ba690
    png_init_io(png_ptr, fp);
Packit 0ba690
Packit 0ba690
If you are embedding your PNG into a datastream such as MNG, and don't
Packit 0ba690
want libpng to write the 8-byte signature, or if you have already
Packit 0ba690
written the signature in your application, use
Packit 0ba690
Packit 0ba690
    png_set_sig_bytes(png_ptr, 8);
Packit 0ba690
Packit 0ba690
to inform libpng that it should not write a signature.
Packit 0ba690
Packit 0ba690
.SS Write callbacks
Packit 0ba690
Packit 0ba690
At this point, you can set up a callback function that will be
Packit 0ba690
called after each row has been written, which you can use to control
Packit 0ba690
a progress meter or the like.  It's demonstrated in pngtest.c.
Packit 0ba690
You must supply a function
Packit 0ba690
Packit 0ba690
    void write_row_callback(png_ptr, png_uint_32 row,
Packit 0ba690
       int pass);
Packit 0ba690
    {
Packit 0ba690
      /* put your code here */
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
(You can give it another name that you like instead of "write_row_callback")
Packit 0ba690
Packit 0ba690
To inform libpng about your function, use
Packit 0ba690
Packit 0ba690
    png_set_write_status_fn(png_ptr, write_row_callback);
Packit 0ba690
Packit 0ba690
You now have the option of modifying how the compression library will
Packit 0ba690
run.  The following functions are mainly for testing, but may be useful
Packit 0ba690
in some cases, like if you need to write PNG files extremely fast and
Packit 0ba690
are willing to give up some compression, or if you want to get the
Packit 0ba690
maximum possible compression at the expense of slower writing.  If you
Packit 0ba690
have no special needs in this area, let the library do what it wants by
Packit 0ba690
not calling this function at all, as it has been tuned to deliver a good
Packit 0ba690
speed/compression ratio. The second parameter to png_set_filter() is
Packit 0ba690
the filter method, for which the only valid values are 0 (as of the
Packit 0ba690
July 1999 PNG specification, version 1.2) or 64 (if you are writing
Packit 0ba690
a PNG datastream that is to be embedded in a MNG datastream).  The third
Packit 0ba690
parameter is a flag that indicates which filter type(s) are to be tested
Packit 0ba690
for each scanline.  See the PNG specification for details on the specific
Packit 0ba690
filter types.
Packit 0ba690
Packit 0ba690
Packit 0ba690
    /* turn on or off filtering, and/or choose
Packit 0ba690
       specific filters.  You can use either a single
Packit 0ba690
       PNG_FILTER_VALUE_NAME or the bitwise OR of one
Packit 0ba690
       or more PNG_FILTER_NAME masks. */
Packit 0ba690
    png_set_filter(png_ptr, 0,
Packit 0ba690
       PNG_FILTER_NONE  | PNG_FILTER_VALUE_NONE |
Packit 0ba690
       PNG_FILTER_SUB   | PNG_FILTER_VALUE_SUB  |
Packit 0ba690
       PNG_FILTER_UP    | PNG_FILTER_VALUE_UP   |
Packit 0ba690
       PNG_FILTER_AVG   | PNG_FILTER_VALUE_AVG  |
Packit 0ba690
       PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH|
Packit 0ba690
       PNG_ALL_FILTERS);
Packit 0ba690
Packit 0ba690
If an application
Packit 0ba690
wants to start and stop using particular filters during compression,
Packit 0ba690
it should start out with all of the filters (to ensure that the previous
Packit 0ba690
row of pixels will be stored in case it's needed later), and then add
Packit 0ba690
and remove them after the start of compression.
Packit 0ba690
Packit 0ba690
If you are writing a PNG datastream that is to be embedded in a MNG
Packit 0ba690
datastream, the second parameter can be either 0 or 64.
Packit 0ba690
Packit 0ba690
The png_set_compression_*() functions interface to the zlib compression
Packit 0ba690
library, and should mostly be ignored unless you really know what you are
Packit 0ba690
doing.  The only generally useful call is png_set_compression_level()
Packit 0ba690
which changes how much time zlib spends on trying to compress the image
Packit 0ba690
data.  See the Compression Library (zlib.h and algorithm.txt, distributed
Packit 0ba690
with zlib) for details on the compression levels.
Packit 0ba690
Packit 0ba690
    /* set the zlib compression level */
Packit 0ba690
    png_set_compression_level(png_ptr,
Packit 0ba690
        Z_BEST_COMPRESSION);
Packit 0ba690
Packit 0ba690
    /* set other zlib parameters */
Packit 0ba690
    png_set_compression_mem_level(png_ptr, 8);
Packit 0ba690
    png_set_compression_strategy(png_ptr,
Packit 0ba690
        Z_DEFAULT_STRATEGY);
Packit 0ba690
    png_set_compression_window_bits(png_ptr, 15);
Packit 0ba690
    png_set_compression_method(png_ptr, 8);
Packit 0ba690
    png_set_compression_buffer_size(png_ptr, 8192)
Packit 0ba690
Packit 0ba690
extern PNG_EXPORT(void,png_set_zbuf_size)
Packit 0ba690
Packit 0ba690
.SS Setting the contents of info for output
Packit 0ba690
Packit 0ba690
You now need to fill in the png_info structure with all the data you
Packit 0ba690
wish to write before the actual image.  Note that the only thing you
Packit 0ba690
are allowed to write after the image is the text chunks and the time
Packit 0ba690
chunk (as of PNG Specification 1.2, anyway).  See png_write_end() and
Packit 0ba690
the latest PNG specification for more information on that.  If you
Packit 0ba690
wish to write them before the image, fill them in now, and flag that
Packit 0ba690
data as being valid.  If you want to wait until after the data, don't
Packit 0ba690
fill them until png_write_end().  For all the fields in png_info and
Packit 0ba690
their data types, see png.h.  For explanations of what the fields
Packit 0ba690
contain, see the PNG specification.
Packit 0ba690
Packit 0ba690
Some of the more important parts of the png_info are:
Packit 0ba690
Packit 0ba690
    png_set_IHDR(png_ptr, info_ptr, width, height,
Packit 0ba690
       bit_depth, color_type, interlace_type,
Packit 0ba690
       compression_type, filter_method)
Packit 0ba690
    width          - holds the width of the image
Packit 0ba690
                     in pixels (up to 2^31).
Packit 0ba690
    height         - holds the height of the image
Packit 0ba690
                     in pixels (up to 2^31).
Packit 0ba690
    bit_depth      - holds the bit depth of one of the
Packit 0ba690
                     image channels.
Packit 0ba690
                     (valid values are 1, 2, 4, 8, 16
Packit 0ba690
                     and depend also on the
Packit 0ba690
                     color_type.  See also significant
Packit 0ba690
                     bits (sBIT) below).
Packit 0ba690
    color_type     - describes which color/alpha
Packit 0ba690
                     channels are present.
Packit 0ba690
                     PNG_COLOR_TYPE_GRAY
Packit 0ba690
                        (bit depths 1, 2, 4, 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_GRAY_ALPHA
Packit 0ba690
                        (bit depths 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_PALETTE
Packit 0ba690
                        (bit depths 1, 2, 4, 8)
Packit 0ba690
                     PNG_COLOR_TYPE_RGB
Packit 0ba690
                        (bit_depths 8, 16)
Packit 0ba690
                     PNG_COLOR_TYPE_RGB_ALPHA
Packit 0ba690
                        (bit_depths 8, 16)
Packit 0ba690
Packit 0ba690
                     PNG_COLOR_MASK_PALETTE
Packit 0ba690
                     PNG_COLOR_MASK_COLOR
Packit 0ba690
                     PNG_COLOR_MASK_ALPHA
Packit 0ba690
Packit 0ba690
    interlace_type - PNG_INTERLACE_NONE or
Packit 0ba690
                     PNG_INTERLACE_ADAM7
Packit 0ba690
    compression_type - (must be
Packit 0ba690
                     PNG_COMPRESSION_TYPE_DEFAULT)
Packit 0ba690
    filter_method  - (must be PNG_FILTER_TYPE_DEFAULT
Packit 0ba690
                     or, if you are writing a PNG to
Packit 0ba690
                     be embedded in a MNG datastream,
Packit 0ba690
                     can also be
Packit 0ba690
                     PNG_INTRAPIXEL_DIFFERENCING)
Packit 0ba690
Packit 0ba690
If you call png_set_IHDR(), the call must appear before any of the
Packit 0ba690
other png_set_*() functions, because they might require access to some of
Packit 0ba690
the IHDR settings.  The remaining png_set_*() functions can be called
Packit 0ba690
in any order.
Packit 0ba690
Packit 0ba690
If you wish, you can reset the compression_type, interlace_type, or
Packit 0ba690
filter_method later by calling png_set_IHDR() again; if you do this, the
Packit 0ba690
width, height, bit_depth, and color_type must be the same in each call.
Packit 0ba690
Packit 0ba690
    png_set_PLTE(png_ptr, info_ptr, palette,
Packit 0ba690
       num_palette);
Packit 0ba690
    palette        - the palette for the file
Packit 0ba690
                     (array of png_color)
Packit 0ba690
    num_palette    - number of entries in the palette
Packit 0ba690
Packit 0ba690
Packit 0ba690
    png_set_gAMA(png_ptr, info_ptr, gamma);
Packit 0ba690
    gamma          - the gamma the image was created
Packit 0ba690
                     at (PNG_INFO_gAMA)
Packit 0ba690
Packit 0ba690
    png_set_sRGB(png_ptr, info_ptr, srgb_intent);
Packit 0ba690
    srgb_intent    - the rendering intent
Packit 0ba690
                     (PNG_INFO_sRGB) The presence of
Packit 0ba690
                     the sRGB chunk means that the pixel
Packit 0ba690
                     data is in the sRGB color space.
Packit 0ba690
                     This chunk also implies specific
Packit 0ba690
                     values of gAMA and cHRM.  Rendering
Packit 0ba690
                     intent is the CSS-1 property that
Packit 0ba690
                     has been defined by the International
Packit 0ba690
                     Color Consortium
Packit 0ba690
                     (http://www.color.org).
Packit 0ba690
                     It can be one of
Packit 0ba690
                     PNG_sRGB_INTENT_SATURATION,
Packit 0ba690
                     PNG_sRGB_INTENT_PERCEPTUAL,
Packit 0ba690
                     PNG_sRGB_INTENT_ABSOLUTE, or
Packit 0ba690
                     PNG_sRGB_INTENT_RELATIVE.
Packit 0ba690
Packit 0ba690
Packit 0ba690
    png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
Packit 0ba690
       srgb_intent);
Packit 0ba690
    srgb_intent    - the rendering intent
Packit 0ba690
                     (PNG_INFO_sRGB) The presence of the
Packit 0ba690
                     sRGB chunk means that the pixel
Packit 0ba690
                     data is in the sRGB color space.
Packit 0ba690
                     This function also causes gAMA and
Packit 0ba690
                     cHRM chunks with the specific values
Packit 0ba690
                     that are consistent with sRGB to be
Packit 0ba690
                     written.
Packit 0ba690
Packit 0ba690
    png_set_iCCP(png_ptr, info_ptr, name, compression_type,
Packit 0ba690
                      profile, proflen);
Packit 0ba690
    name            - The profile name.
Packit 0ba690
    compression     - The compression type; always
Packit 0ba690
                      PNG_COMPRESSION_TYPE_BASE for PNG 1.0.
Packit 0ba690
                      You may give NULL to this argument to
Packit 0ba690
                      ignore it.
Packit 0ba690
    profile         - International Color Consortium color
Packit 0ba690
                      profile data. May contain NULs.
Packit 0ba690
    proflen         - length of profile data in bytes.
Packit 0ba690
Packit 0ba690
    png_set_sBIT(png_ptr, info_ptr, sig_bit);
Packit 0ba690
    sig_bit        - the number of significant bits for
Packit 0ba690
                     (PNG_INFO_sBIT) each of the gray, red,
Packit 0ba690
                     green, and blue channels, whichever are
Packit 0ba690
                     appropriate for the given color type
Packit 0ba690
                     (png_color_16)
Packit 0ba690
Packit 0ba690
    png_set_tRNS(png_ptr, info_ptr, trans, num_trans,
Packit 0ba690
       trans_values);
Packit 0ba690
    trans          - array of transparent
Packit 0ba690
                     entries for palette (PNG_INFO_tRNS)
Packit 0ba690
    trans_values   - graylevel or color sample values
Packit 0ba690
                     (in order red, green, blue) of the
Packit 0ba690
                     single transparent color for
Packit 0ba690
                     non-paletted images (PNG_INFO_tRNS)
Packit 0ba690
    num_trans      - number of transparent entries
Packit 0ba690
                     (PNG_INFO_tRNS)
Packit 0ba690
Packit 0ba690
    png_set_hIST(png_ptr, info_ptr, hist);
Packit 0ba690
                    (PNG_INFO_hIST)
Packit 0ba690
    hist           - histogram of palette (array of
Packit 0ba690
                     png_uint_16)
Packit 0ba690
Packit 0ba690
    png_set_tIME(png_ptr, info_ptr, mod_time);
Packit 0ba690
    mod_time       - time image was last modified
Packit 0ba690
                     (PNG_VALID_tIME)
Packit 0ba690
Packit 0ba690
    png_set_bKGD(png_ptr, info_ptr, background);
Packit 0ba690
    background     - background color (PNG_VALID_bKGD)
Packit 0ba690
Packit 0ba690
    png_set_text(png_ptr, info_ptr, text_ptr, num_text);
Packit 0ba690
    text_ptr       - array of png_text holding image
Packit 0ba690
                     comments
Packit 0ba690
    text_ptr[i].compression - type of compression used
Packit 0ba690
                 on "text" PNG_TEXT_COMPRESSION_NONE
Packit 0ba690
                           PNG_TEXT_COMPRESSION_zTXt
Packit 0ba690
                           PNG_ITXT_COMPRESSION_NONE
Packit 0ba690
                           PNG_ITXT_COMPRESSION_zTXt
Packit 0ba690
    text_ptr[i].key   - keyword for comment.  Must contain
Packit 0ba690
                 1-79 characters.
Packit 0ba690
    text_ptr[i].text  - text comments for current
Packit 0ba690
                         keyword.  Can be NULL or empty.
Packit 0ba690
    text_ptr[i].text_length - length of text string,
Packit 0ba690
                 after decompression, 0 for iTXt
Packit 0ba690
    text_ptr[i].itxt_length - length of itxt string,
Packit 0ba690
                 after decompression, 0 for tEXt/zTXt
Packit 0ba690
    text_ptr[i].lang  - language of comment (NULL or
Packit 0ba690
                         empty for unknown).
Packit 0ba690
    text_ptr[i].translated_keyword  - keyword in UTF-8 (NULL
Packit 0ba690
                         or empty for unknown).
Packit 0ba690
    Note that the itxt_length, lang, and lang_key
Packit 0ba690
    members of the text_ptr structure only exist
Packit 0ba690
    when the library is built with iTXt chunk support.
Packit 0ba690
Packit 0ba690
    num_text       - number of comments
Packit 0ba690
Packit 0ba690
    png_set_sPLT(png_ptr, info_ptr, &palette_ptr,
Packit 0ba690
       num_spalettes);
Packit 0ba690
    palette_ptr    - array of png_sPLT_struct structures
Packit 0ba690
                     to be added to the list of palettes
Packit 0ba690
                     in the info structure.
Packit 0ba690
    num_spalettes  - number of palette structures to be
Packit 0ba690
                     added.
Packit 0ba690
Packit 0ba690
    png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y,
Packit 0ba690
        unit_type);
Packit 0ba690
    offset_x  - positive offset from the left
Packit 0ba690
                     edge of the screen
Packit 0ba690
    offset_y  - positive offset from the top
Packit 0ba690
                     edge of the screen
Packit 0ba690
    unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER
Packit 0ba690
Packit 0ba690
    png_set_pHYs(png_ptr, info_ptr, res_x, res_y,
Packit 0ba690
        unit_type);
Packit 0ba690
    res_x       - pixels/unit physical resolution
Packit 0ba690
                  in x direction
Packit 0ba690
    res_y       - pixels/unit physical resolution
Packit 0ba690
                  in y direction
Packit 0ba690
    unit_type   - PNG_RESOLUTION_UNKNOWN,
Packit 0ba690
                  PNG_RESOLUTION_METER
Packit 0ba690
Packit 0ba690
    png_set_sCAL(png_ptr, info_ptr, unit, width, height)
Packit 0ba690
    unit        - physical scale units (an integer)
Packit 0ba690
    width       - width of a pixel in physical scale units
Packit 0ba690
    height      - height of a pixel in physical scale units
Packit 0ba690
                  (width and height are doubles)
Packit 0ba690
Packit 0ba690
    png_set_sCAL_s(png_ptr, info_ptr, unit, width, height)
Packit 0ba690
    unit        - physical scale units (an integer)
Packit 0ba690
    width       - width of a pixel in physical scale units
Packit 0ba690
    height      - height of a pixel in physical scale units
Packit 0ba690
                 (width and height are strings like "2.54")
Packit 0ba690
Packit 0ba690
    png_set_unknown_chunks(png_ptr, info_ptr, &unknowns,
Packit 0ba690
       num_unknowns)
Packit 0ba690
    unknowns          - array of png_unknown_chunk
Packit 0ba690
                        structures holding unknown chunks
Packit 0ba690
    unknowns[i].name  - name of unknown chunk
Packit 0ba690
    unknowns[i].data  - data of unknown chunk
Packit 0ba690
    unknowns[i].size  - size of unknown chunk's data
Packit 0ba690
    unknowns[i].location - position to write chunk in file
Packit 0ba690
                           0: do not write chunk
Packit 0ba690
                           PNG_HAVE_IHDR: before PLTE
Packit 0ba690
                           PNG_HAVE_PLTE: before IDAT
Packit 0ba690
                           PNG_AFTER_IDAT: after IDAT
Packit 0ba690
Packit 0ba690
The "location" member is set automatically according to
Packit 0ba690
what part of the output file has already been written.
Packit 0ba690
You can change its value after calling png_set_unknown_chunks()
Packit 0ba690
as demonstrated in pngtest.c.  Within each of the "locations",
Packit 0ba690
the chunks are sequenced according to their position in the
Packit 0ba690
structure (that is, the value of "i", which is the order in which
Packit 0ba690
the chunk was either read from the input file or defined with
Packit 0ba690
png_set_unknown_chunks).
Packit 0ba690
Packit 0ba690
A quick word about text and num_text.  text is an array of png_text
Packit 0ba690
structures.  num_text is the number of valid structures in the array.
Packit 0ba690
Each png_text structure holds a language code, a keyword, a text value,
Packit 0ba690
and a compression type.
Packit 0ba690
Packit 0ba690
The compression types have the same valid numbers as the compression
Packit 0ba690
types of the image data.  Currently, the only valid number is zero.
Packit 0ba690
However, you can store text either compressed or uncompressed, unlike
Packit 0ba690
images, which always have to be compressed.  So if you don't want the
Packit 0ba690
text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
Packit 0ba690
Because tEXt and zTXt chunks don't have a language field, if you
Packit 0ba690
specify PNG_TEXT_COMPRESSION_NONE or PNG_TEXT_COMPRESSION_zTXt
Packit 0ba690
any language code or translated keyword will not be written out.
Packit 0ba690
Packit 0ba690
Until text gets around 1000 bytes, it is not worth compressing it.
Packit 0ba690
After the text has been written out to the file, the compression type
Packit 0ba690
is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR,
Packit 0ba690
so that it isn't written out again at the end (in case you are calling
Packit 0ba690
png_write_end() with the same struct.
Packit 0ba690
Packit 0ba690
The keywords that are given in the PNG Specification are:
Packit 0ba690
Packit 0ba690
    Title            Short (one line) title or
Packit 0ba690
                     caption for image
Packit 0ba690
    Author           Name of image's creator
Packit 0ba690
    Description      Description of image (possibly long)
Packit 0ba690
    Copyright        Copyright notice
Packit 0ba690
    Creation Time    Time of original image creation
Packit 0ba690
                     (usually RFC 1123 format, see below)
Packit 0ba690
    Software         Software used to create the image
Packit 0ba690
    Disclaimer       Legal disclaimer
Packit 0ba690
    Warning          Warning of nature of content
Packit 0ba690
    Source           Device used to create the image
Packit 0ba690
    Comment          Miscellaneous comment; conversion
Packit 0ba690
                     from other image format
Packit 0ba690
Packit 0ba690
The keyword-text pairs work like this.  Keywords should be short
Packit 0ba690
simple descriptions of what the comment is about.  Some typical
Packit 0ba690
keywords are found in the PNG specification, as is some recommendations
Packit 0ba690
on keywords.  You can repeat keywords in a file.  You can even write
Packit 0ba690
some text before the image and some after.  For example, you may want
Packit 0ba690
to put a description of the image before the image, but leave the
Packit 0ba690
disclaimer until after, so viewers working over modem connections
Packit 0ba690
don't have to wait for the disclaimer to go over the modem before
Packit 0ba690
they start seeing the image.  Finally, keywords should be full
Packit 0ba690
words, not abbreviations.  Keywords and text are in the ISO 8859-1
Packit 0ba690
(Latin-1) character set (a superset of regular ASCII) and can not
Packit 0ba690
contain NUL characters, and should not contain control or other
Packit 0ba690
unprintable characters.  To make the comments widely readable, stick
Packit 0ba690
with basic ASCII, and avoid machine specific character set extensions
Packit 0ba690
like the IBM-PC character set.  The keyword must be present, but
Packit 0ba690
you can leave off the text string on non-compressed pairs.
Packit 0ba690
Compressed pairs must have a text string, as only the text string
Packit 0ba690
is compressed anyway, so the compression would be meaningless.
Packit 0ba690
Packit 0ba690
PNG supports modification time via the png_time structure.  Two
Packit 0ba690
conversion routines are provided, png_convert_from_time_t() for
Packit 0ba690
time_t and png_convert_from_struct_tm() for struct tm.  The
Packit 0ba690
time_t routine uses gmtime().  You don't have to use either of
Packit 0ba690
these, but if you wish to fill in the png_time structure directly,
Packit 0ba690
you should provide the time in universal time (GMT) if possible
Packit 0ba690
instead of your local time.  Note that the year number is the full
Packit 0ba690
year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and
Packit 0ba690
that months start with 1.
Packit 0ba690
Packit 0ba690
If you want to store the time of the original image creation, you should
Packit 0ba690
use a plain tEXt chunk with the "Creation Time" keyword.  This is
Packit 0ba690
necessary because the "creation time" of a PNG image is somewhat vague,
Packit 0ba690
depending on whether you mean the PNG file, the time the image was
Packit 0ba690
created in a non-PNG format, a still photo from which the image was
Packit 0ba690
scanned, or possibly the subject matter itself.  In order to facilitate
Packit 0ba690
machine-readable dates, it is recommended that the "Creation Time"
Packit 0ba690
tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"),
Packit 0ba690
although this isn't a requirement.  Unlike the tIME chunk, the
Packit 0ba690
"Creation Time" tEXt chunk is not expected to be automatically changed
Packit 0ba690
by the software.  To facilitate the use of RFC 1123 dates, a function
Packit 0ba690
png_convert_to_rfc1123(png_timep) is provided to convert from PNG
Packit 0ba690
time to an RFC 1123 format string.
Packit 0ba690
Packit 0ba690
.SS Writing unknown chunks
Packit 0ba690
Packit 0ba690
You can use the png_set_unknown_chunks function to queue up chunks
Packit 0ba690
for writing.  You give it a chunk name, raw data, and a size; that's
Packit 0ba690
all there is to it.  The chunks will be written by the next following
Packit 0ba690
png_write_info_before_PLTE, png_write_info, or png_write_end function.
Packit 0ba690
Any chunks previously read into the info structure's unknown-chunk
Packit 0ba690
list will also be written out in a sequence that satisfies the PNG
Packit 0ba690
specification's ordering rules.
Packit 0ba690
Packit 0ba690
.SS The high-level write interface
Packit 0ba690
Packit 0ba690
At this point there are two ways to proceed; through the high-level
Packit 0ba690
write interface, or through a sequence of low-level write operations.
Packit 0ba690
You can use the high-level interface if your image data is present
Packit 0ba690
in the info structure.  All defined output
Packit 0ba690
transformations are permitted, enabled by the following masks.
Packit 0ba690
Packit 0ba690
    PNG_TRANSFORM_IDENTITY      No transformation
Packit 0ba690
    PNG_TRANSFORM_PACKING       Pack 1, 2 and 4-bit samples
Packit 0ba690
    PNG_TRANSFORM_PACKSWAP      Change order of packed
Packit 0ba690
                                pixels to LSB first
Packit 0ba690
    PNG_TRANSFORM_INVERT_MONO   Invert monochrome images
Packit 0ba690
    PNG_TRANSFORM_SHIFT         Normalize pixels to the
Packit 0ba690
                                sBIT depth
Packit 0ba690
    PNG_TRANSFORM_BGR           Flip RGB to BGR, RGBA
Packit 0ba690
                                to BGRA
Packit 0ba690
    PNG_TRANSFORM_SWAP_ALPHA    Flip RGBA to ARGB or GA
Packit 0ba690
                                to AG
Packit 0ba690
    PNG_TRANSFORM_INVERT_ALPHA  Change alpha from opacity
Packit 0ba690
                                to transparency
Packit 0ba690
    PNG_TRANSFORM_SWAP_ENDIAN   Byte-swap 16-bit samples
Packit 0ba690
    PNG_TRANSFORM_STRIP_FILLER        Strip out filler
Packit 0ba690
                                      bytes (deprecated).
Packit 0ba690
    PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading
Packit 0ba690
                                      filler bytes
Packit 0ba690
    PNG_TRANSFORM_STRIP_FILLER_AFTER  Strip out trailing
Packit 0ba690
                                      filler bytes
Packit 0ba690
Packit 0ba690
If you have valid image data in the info structure (you can use
Packit 0ba690
png_set_rows() to put image data in the info structure), simply do this:
Packit 0ba690
Packit 0ba690
    png_write_png(png_ptr, info_ptr, png_transforms, NULL)
Packit 0ba690
Packit 0ba690
where png_transforms is an integer containing the bitwise OR of some set of
Packit 0ba690
transformation flags.  This call is equivalent to png_write_info(),
Packit 0ba690
followed the set of transformations indicated by the transform mask,
Packit 0ba690
then png_write_image(), and finally png_write_end().
Packit 0ba690
Packit 0ba690
(The final parameter of this call is not yet used.  Someday it might point
Packit 0ba690
to transformation parameters required by some future output transform.)
Packit 0ba690
Packit 0ba690
You must use png_transforms and not call any png_set_transform() functions
Packit 0ba690
when you use png_write_png().
Packit 0ba690
Packit 0ba690
.SS The low-level write interface
Packit 0ba690
Packit 0ba690
If you are going the low-level route instead, you are now ready to
Packit 0ba690
write all the file information up to the actual image data.  You do
Packit 0ba690
this with a call to png_write_info().
Packit 0ba690
Packit 0ba690
    png_write_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
Note that there is one transformation you may need to do before
Packit 0ba690
png_write_info().  In PNG files, the alpha channel in an image is the
Packit 0ba690
level of opacity.  If your data is supplied as a level of transparency,
Packit 0ba690
you can invert the alpha channel before you write it, so that 0 is
Packit 0ba690
fully transparent and 255 (in 8-bit or paletted images) or 65535
Packit 0ba690
(in 16-bit images) is fully opaque, with
Packit 0ba690
Packit 0ba690
    png_set_invert_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
This must appear before png_write_info() instead of later with the
Packit 0ba690
other transformations because in the case of paletted images the tRNS
Packit 0ba690
chunk data has to be inverted before the tRNS chunk is written.  If
Packit 0ba690
your image is not a paletted image, the tRNS data (which in such cases
Packit 0ba690
represents a single color to be rendered as transparent) won't need to
Packit 0ba690
be changed, and you can safely do this transformation after your
Packit 0ba690
png_write_info() call.
Packit 0ba690
Packit 0ba690
If you need to write a private chunk that you want to appear before
Packit 0ba690
the PLTE chunk when PLTE is present, you can write the PNG info in
Packit 0ba690
two steps, and insert code to write your own chunk between them:
Packit 0ba690
Packit 0ba690
    png_write_info_before_PLTE(png_ptr, info_ptr);
Packit 0ba690
    png_set_unknown_chunks(png_ptr, info_ptr, ...);
Packit 0ba690
    png_write_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
After you've written the file information, you can set up the library
Packit 0ba690
to handle any special transformations of the image data.  The various
Packit 0ba690
ways to transform the data will be described in the order that they
Packit 0ba690
should occur.  This is important, as some of these change the color
Packit 0ba690
type and/or bit depth of the data, and some others only work on
Packit 0ba690
certain color types and bit depths.  Even though each transformation
Packit 0ba690
checks to see if it has data that it can do something with, you should
Packit 0ba690
make sure to only enable a transformation if it will be valid for the
Packit 0ba690
data.  For example, don't swap red and blue on grayscale data.
Packit 0ba690
Packit 0ba690
PNG files store RGB pixels packed into 3 or 6 bytes.  This code tells
Packit 0ba690
the library to strip input data that has 4 or 8 bytes per pixel down
Packit 0ba690
to 3 or 6 bytes (or strip 2 or 4-byte grayscale+filler data to 1 or 2
Packit 0ba690
bytes per pixel).
Packit 0ba690
Packit 0ba690
    png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Packit 0ba690
Packit 0ba690
where the 0 is unused, and the location is either PNG_FILLER_BEFORE or
Packit 0ba690
PNG_FILLER_AFTER, depending upon whether the filler byte in the pixel
Packit 0ba690
is stored XRGB or RGBX.
Packit 0ba690
Packit 0ba690
PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as
Packit 0ba690
they can, resulting in, for example, 8 pixels per byte for 1 bit files.
Packit 0ba690
If the data is supplied at 1 pixel per byte, use this code, which will
Packit 0ba690
correctly pack the pixels into a single byte:
Packit 0ba690
Packit 0ba690
    png_set_packing(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  If your
Packit 0ba690
data is of another bit depth, you can write an sBIT chunk into the
Packit 0ba690
file so that decoders can recover the original data if desired.
Packit 0ba690
Packit 0ba690
    /* Set the true bit depth of the image data */
Packit 0ba690
    if (color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
    {
Packit 0ba690
        sig_bit.red = true_bit_depth;
Packit 0ba690
        sig_bit.green = true_bit_depth;
Packit 0ba690
        sig_bit.blue = true_bit_depth;
Packit 0ba690
    }
Packit 0ba690
    else
Packit 0ba690
    {
Packit 0ba690
        sig_bit.gray = true_bit_depth;
Packit 0ba690
    }
Packit 0ba690
    if (color_type & PNG_COLOR_MASK_ALPHA)
Packit 0ba690
    {
Packit 0ba690
        sig_bit.alpha = true_bit_depth;
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    png_set_sBIT(png_ptr, info_ptr, &sig_bit);
Packit 0ba690
Packit 0ba690
If the data is stored in the row buffer in a bit depth other than
Packit 0ba690
one supported by PNG (e.g. 3 bit data in the range 0-7 for a 4-bit PNG),
Packit 0ba690
this will scale the values to appear to be the correct bit depth as
Packit 0ba690
is required by PNG.
Packit 0ba690
Packit 0ba690
    png_set_shift(png_ptr, &sig_bit);
Packit 0ba690
Packit 0ba690
PNG files store 16 bit pixels in network byte order (big-endian,
Packit 0ba690
ie. most significant bits first).  This code would be used if they are
Packit 0ba690
supplied the other way (little-endian, i.e. least significant bits
Packit 0ba690
first, the way PCs store them):
Packit 0ba690
Packit 0ba690
    if (bit_depth > 8)
Packit 0ba690
       png_set_swap(png_ptr);
Packit 0ba690
Packit 0ba690
If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you
Packit 0ba690
need to change the order the pixels are packed into bytes, you can use:
Packit 0ba690
Packit 0ba690
    if (bit_depth < 8)
Packit 0ba690
       png_set_packswap(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files store 3 color pixels in red, green, blue order.  This code
Packit 0ba690
would be used if they are supplied as blue, green, red:
Packit 0ba690
Packit 0ba690
    png_set_bgr(png_ptr);
Packit 0ba690
Packit 0ba690
PNG files describe monochrome as black being zero and white being
Packit 0ba690
one. This code would be used if the pixels are supplied with this reversed
Packit 0ba690
(black being one and white being zero):
Packit 0ba690
Packit 0ba690
    png_set_invert_mono(png_ptr);
Packit 0ba690
Packit 0ba690
Finally, you can write your own transformation function if none of
Packit 0ba690
the existing ones meets your needs.  This is done by setting a callback
Packit 0ba690
with
Packit 0ba690
Packit 0ba690
    png_set_write_user_transform_fn(png_ptr,
Packit 0ba690
       write_transform_fn);
Packit 0ba690
Packit 0ba690
You must supply the function
Packit 0ba690
Packit 0ba690
    void write_transform_fn(png_ptr ptr, row_info_ptr
Packit 0ba690
       row_info, png_bytep data)
Packit 0ba690
Packit 0ba690
See pngtest.c for a working example.  Your function will be called
Packit 0ba690
before any of the other transformations are processed.
Packit 0ba690
Packit 0ba690
You can also set up a pointer to a user structure for use by your
Packit 0ba690
callback function.
Packit 0ba690
Packit 0ba690
    png_set_user_transform_info(png_ptr, user_ptr, 0, 0);
Packit 0ba690
Packit 0ba690
The user_channels and user_depth parameters of this function are ignored
Packit 0ba690
when writing; you can set them to zero as shown.
Packit 0ba690
Packit 0ba690
You can retrieve the pointer via the function png_get_user_transform_ptr().
Packit 0ba690
For example:
Packit 0ba690
Packit 0ba690
    voidp write_user_transform_ptr =
Packit 0ba690
       png_get_user_transform_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
It is possible to have libpng flush any pending output, either manually,
Packit 0ba690
or automatically after a certain number of lines have been written.  To
Packit 0ba690
flush the output stream a single time call:
Packit 0ba690
Packit 0ba690
    png_write_flush(png_ptr);
Packit 0ba690
Packit 0ba690
and to have libpng flush the output stream periodically after a certain
Packit 0ba690
number of scanlines have been written, call:
Packit 0ba690
Packit 0ba690
    png_set_flush(png_ptr, nrows);
Packit 0ba690
Packit 0ba690
Note that the distance between rows is from the last time png_write_flush()
Packit 0ba690
was called, or the first row of the image if it has never been called.
Packit 0ba690
So if you write 50 lines, and then png_set_flush 25, it will flush the
Packit 0ba690
output on the next scanline, and every 25 lines thereafter, unless
Packit 0ba690
png_write_flush() is called before 25 more lines have been written.
Packit 0ba690
If nrows is too small (less than about 10 lines for a 640 pixel wide
Packit 0ba690
RGB image) the image compression may decrease noticeably (although this
Packit 0ba690
may be acceptable for real-time applications).  Infrequent flushing will
Packit 0ba690
only degrade the compression performance by a few percent over images
Packit 0ba690
that do not use flushing.
Packit 0ba690
Packit 0ba690
.SS Writing the image data
Packit 0ba690
Packit 0ba690
That's it for the transformations.  Now you can write the image data.
Packit 0ba690
The simplest way to do this is in one function call.  If you have the
Packit 0ba690
whole image in memory, you can just call png_write_image() and libpng
Packit 0ba690
will write the image.  You will need to pass in an array of pointers to
Packit 0ba690
each row.  This function automatically handles interlacing, so you don't
Packit 0ba690
need to call png_set_interlace_handling() or call this function multiple
Packit 0ba690
times, or any of that other stuff necessary with png_write_rows().
Packit 0ba690
Packit 0ba690
    png_write_image(png_ptr, row_pointers);
Packit 0ba690
Packit 0ba690
where row_pointers is:
Packit 0ba690
Packit 0ba690
    png_byte *row_pointers[height];
Packit 0ba690
Packit 0ba690
You can point to void or char or whatever you use for pixels.
Packit 0ba690
Packit 0ba690
If you don't want to write the whole image at once, you can
Packit 0ba690
use png_write_rows() instead.  If the file is not interlaced,
Packit 0ba690
this is simple:
Packit 0ba690
Packit 0ba690
    png_write_rows(png_ptr, row_pointers,
Packit 0ba690
       number_of_rows);
Packit 0ba690
Packit 0ba690
row_pointers is the same as in the png_write_image() call.
Packit 0ba690
Packit 0ba690
If you are just writing one row at a time, you can do this with
Packit 0ba690
a single row_pointer instead of an array of row_pointers:
Packit 0ba690
Packit 0ba690
    png_bytep row_pointer = row;
Packit 0ba690
Packit 0ba690
    png_write_row(png_ptr, row_pointer);
Packit 0ba690
Packit 0ba690
When the file is interlaced, things can get a good deal more complicated.
Packit 0ba690
The only currently (as of the PNG Specification version 1.2, dated July
Packit 0ba690
1999) defined interlacing scheme for PNG files is the "Adam7" interlace
Packit 0ba690
scheme, that breaks down an image into seven smaller images of varying
Packit 0ba690
size.  libpng will build these images for you, or you can do them
Packit 0ba690
yourself.  If you want to build them yourself, see the PNG specification
Packit 0ba690
for details of which pixels to write when.
Packit 0ba690
Packit 0ba690
If you don't want libpng to handle the interlacing details, just
Packit 0ba690
use png_set_interlace_handling() and call png_write_rows() the
Packit 0ba690
correct number of times to write all seven sub-images.
Packit 0ba690
Packit 0ba690
If you want libpng to build the sub-images, call this before you start
Packit 0ba690
writing any rows:
Packit 0ba690
Packit 0ba690
    number_of_passes =
Packit 0ba690
       png_set_interlace_handling(png_ptr);
Packit 0ba690
Packit 0ba690
This will return the number of passes needed.  Currently, this is seven,
Packit 0ba690
but may change if another interlace type is added.
Packit 0ba690
Packit 0ba690
Then write the complete image number_of_passes times.
Packit 0ba690
Packit 0ba690
    png_write_rows(png_ptr, row_pointers,
Packit 0ba690
       number_of_rows);
Packit 0ba690
Packit 0ba690
As some of these rows are not used, and thus return immediately, you may
Packit 0ba690
want to read about interlacing in the PNG specification, and only update
Packit 0ba690
the rows that are actually used.
Packit 0ba690
Packit 0ba690
.SS Finishing a sequential write
Packit 0ba690
Packit 0ba690
After you are finished writing the image, you should finish writing
Packit 0ba690
the file.  If you are interested in writing comments or time, you should
Packit 0ba690
pass an appropriately filled png_info pointer.  If you are not interested,
Packit 0ba690
you can pass NULL.
Packit 0ba690
Packit 0ba690
    png_write_end(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
When you are done, you can free all memory used by libpng like this:
Packit 0ba690
Packit 0ba690
    png_destroy_write_struct(&png_ptr, &info_ptr);
Packit 0ba690
Packit 0ba690
It is also possible to individually free the info_ptr members that
Packit 0ba690
point to libpng-allocated storage with the following function:
Packit 0ba690
Packit 0ba690
    png_free_data(png_ptr, info_ptr, mask, seq)
Packit 0ba690
    mask  - identifies data to be freed, a mask
Packit 0ba690
            containing the bitwise OR of one or
Packit 0ba690
            more of
Packit 0ba690
              PNG_FREE_PLTE, PNG_FREE_TRNS,
Packit 0ba690
              PNG_FREE_HIST, PNG_FREE_ICCP,
Packit 0ba690
              PNG_FREE_PCAL, PNG_FREE_ROWS,
Packit 0ba690
              PNG_FREE_SCAL, PNG_FREE_SPLT,
Packit 0ba690
              PNG_FREE_TEXT, PNG_FREE_UNKN,
Packit 0ba690
            or simply PNG_FREE_ALL
Packit 0ba690
    seq   - sequence number of item to be freed
Packit 0ba690
            (\-1 for all items)
Packit 0ba690
Packit 0ba690
This function may be safely called when the relevant storage has
Packit 0ba690
already been freed, or has not yet been allocated, or was allocated
Packit 0ba690
by the user  and not by libpng,  and will in those cases do nothing.
Packit 0ba690
The "seq" parameter is ignored if only one item of the selected data
Packit 0ba690
type, such as PLTE, is allowed.  If "seq" is not \-1, and multiple items
Packit 0ba690
are allowed for the data type identified in the mask, such as text or
Packit 0ba690
sPLT, only the n'th item in the structure is freed, where n is "seq".
Packit 0ba690
Packit 0ba690
If you allocated data such as a palette that you passed in to libpng
Packit 0ba690
with png_set_*, you must not free it until just before the call to
Packit 0ba690
png_destroy_write_struct().
Packit 0ba690
Packit 0ba690
The default behavior is only to free data that was allocated internally
Packit 0ba690
by libpng.  This can be changed, so that libpng will not free the data,
Packit 0ba690
or so that it will free data that was allocated by the user with png_malloc()
Packit 0ba690
or png_zalloc() and passed in via a png_set_*() function, with
Packit 0ba690
Packit 0ba690
    png_data_freer(png_ptr, info_ptr, freer, mask)
Packit 0ba690
    mask   - which data elements are affected
Packit 0ba690
             same choices as in png_free_data()
Packit 0ba690
    freer  - one of
Packit 0ba690
               PNG_DESTROY_WILL_FREE_DATA
Packit 0ba690
               PNG_SET_WILL_FREE_DATA
Packit 0ba690
               PNG_USER_WILL_FREE_DATA
Packit 0ba690
Packit 0ba690
For example, to transfer responsibility for some data from a read structure
Packit 0ba690
to a write structure, you could use
Packit 0ba690
Packit 0ba690
    png_data_freer(read_ptr, read_info_ptr,
Packit 0ba690
       PNG_USER_WILL_FREE_DATA,
Packit 0ba690
       PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
Packit 0ba690
    png_data_freer(write_ptr, write_info_ptr,
Packit 0ba690
       PNG_DESTROY_WILL_FREE_DATA,
Packit 0ba690
       PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST)
Packit 0ba690
Packit 0ba690
thereby briefly reassigning responsibility for freeing to the user but
Packit 0ba690
immediately afterwards reassigning it once more to the write_destroy
Packit 0ba690
function.  Having done this, it would then be safe to destroy the read
Packit 0ba690
structure and continue to use the PLTE, tRNS, and hIST data in the write
Packit 0ba690
structure.
Packit 0ba690
Packit 0ba690
This function only affects data that has already been allocated.
Packit 0ba690
You can call this function before calling after the png_set_*() functions
Packit 0ba690
to control whether the user or png_destroy_*() is supposed to free the data.
Packit 0ba690
When the user assumes responsibility for libpng-allocated data, the
Packit 0ba690
application must use
Packit 0ba690
png_free() to free it, and when the user transfers responsibility to libpng
Packit 0ba690
for data that the user has allocated, the user must have used png_malloc()
Packit 0ba690
or png_zalloc() to allocate it.
Packit 0ba690
Packit 0ba690
If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword
Packit 0ba690
separately, do not transfer responsibility for freeing text_ptr to libpng,
Packit 0ba690
because when libpng fills a png_text structure it combines these members with
Packit 0ba690
the key member, and png_free_data() will free only text_ptr.key.  Similarly,
Packit 0ba690
if you transfer responsibility for free'ing text_ptr from libpng to your
Packit 0ba690
application, your application must not separately free those members.
Packit 0ba690
For a more compact example of writing a PNG image, see the file example.c.
Packit 0ba690
Packit 0ba690
.SH V. Modifying/Customizing libpng:
Packit 0ba690
Packit 0ba690
There are two issues here.  The first is changing how libpng does
Packit 0ba690
standard things like memory allocation, input/output, and error handling.
Packit 0ba690
The second deals with more complicated things like adding new chunks,
Packit 0ba690
adding new transformations, and generally changing how libpng works.
Packit 0ba690
Both of those are compile-time issues; that is, they are generally
Packit 0ba690
determined at the time the code is written, and there is rarely a need
Packit 0ba690
to provide the user with a means of changing them.
Packit 0ba690
Packit 0ba690
Memory allocation, input/output, and error handling
Packit 0ba690
Packit 0ba690
All of the memory allocation, input/output, and error handling in libpng
Packit 0ba690
goes through callbacks that are user-settable.  The default routines are
Packit 0ba690
in pngmem.c, pngrio.c, pngwio.c, and pngerror.c, respectively.  To change
Packit 0ba690
these functions, call the appropriate png_set_*_fn() function.
Packit 0ba690
Packit 0ba690
Memory allocation is done through the functions png_malloc(), png_calloc(),
Packit 0ba690
and png_free().  These currently just call the standard C functions.
Packit 0ba690
png_calloc() calls png_malloc() and then png_memset() to clear the newly
Packit 0ba690
allocated memory to zero.  If your pointers can't access more then 64K
Packit 0ba690
at a time, you will want to set MAXSEG_64K in zlib.h.  Since it is
Packit 0ba690
unlikely that the method of handling memory allocation on a platform
Packit 0ba690
will change between applications, these functions must be modified in
Packit 0ba690
the library at compile time.  If you prefer to use a different method
Packit 0ba690
of allocating and freeing data, you can use png_create_read_struct_2() or
Packit 0ba690
png_create_write_struct_2() to register your own functions as described
Packit 0ba690
above.  These functions also provide a void pointer that can be retrieved
Packit 0ba690
via
Packit 0ba690
Packit 0ba690
    mem_ptr=png_get_mem_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
Your replacement memory functions must have prototypes as follows:
Packit 0ba690
Packit 0ba690
    png_voidp malloc_fn(png_structp png_ptr,
Packit 0ba690
       png_size_t size);
Packit 0ba690
    void free_fn(png_structp png_ptr, png_voidp ptr);
Packit 0ba690
Packit 0ba690
Your malloc_fn() must return NULL in case of failure.  The png_malloc()
Packit 0ba690
function will normally call png_error() if it receives a NULL from the
Packit 0ba690
system memory allocator or from your replacement malloc_fn().
Packit 0ba690
Packit 0ba690
Your free_fn() will never be called with a NULL ptr, since libpng's
Packit 0ba690
png_free() checks for NULL before calling free_fn().
Packit 0ba690
Packit 0ba690
Input/Output in libpng is done through png_read() and png_write(),
Packit 0ba690
which currently just call fread() and fwrite().  The FILE * is stored in
Packit 0ba690
png_struct and is initialized via png_init_io().  If you wish to change
Packit 0ba690
the method of I/O, the library supplies callbacks that you can set
Packit 0ba690
through the function png_set_read_fn() and png_set_write_fn() at run
Packit 0ba690
time, instead of calling the png_init_io() function.  These functions
Packit 0ba690
also provide a void pointer that can be retrieved via the function
Packit 0ba690
png_get_io_ptr().  For example:
Packit 0ba690
Packit 0ba690
    png_set_read_fn(png_structp read_ptr,
Packit 0ba690
        voidp read_io_ptr, png_rw_ptr read_data_fn)
Packit 0ba690
Packit 0ba690
    png_set_write_fn(png_structp write_ptr,
Packit 0ba690
        voidp write_io_ptr, png_rw_ptr write_data_fn,
Packit 0ba690
        png_flush_ptr output_flush_fn);
Packit 0ba690
Packit 0ba690
    voidp read_io_ptr = png_get_io_ptr(read_ptr);
Packit 0ba690
    voidp write_io_ptr = png_get_io_ptr(write_ptr);
Packit 0ba690
Packit 0ba690
The replacement I/O functions must have prototypes as follows:
Packit 0ba690
Packit 0ba690
    void user_read_data(png_structp png_ptr,
Packit 0ba690
        png_bytep data, png_size_t length);
Packit 0ba690
    void user_write_data(png_structp png_ptr,
Packit 0ba690
        png_bytep data, png_size_t length);
Packit 0ba690
    void user_flush_data(png_structp png_ptr);
Packit 0ba690
Packit 0ba690
The user_read_data() function is responsible for detecting and
Packit 0ba690
handling end-of-data errors.
Packit 0ba690
Packit 0ba690
Supplying NULL for the read, write, or flush functions sets them back
Packit 0ba690
to using the default C stream functions, which expect the io_ptr to
Packit 0ba690
point to a standard *FILE structure.  It is probably a mistake
Packit 0ba690
to use NULL for one of write_data_fn and output_flush_fn but not both
Packit 0ba690
of them, unless you have built libpng with PNG_NO_WRITE_FLUSH defined.
Packit 0ba690
It is an error to read from a write stream, and vice versa.
Packit 0ba690
Packit 0ba690
Error handling in libpng is done through png_error() and png_warning().
Packit 0ba690
Errors handled through png_error() are fatal, meaning that png_error()
Packit 0ba690
should never return to its caller.  Currently, this is handled via
Packit 0ba690
setjmp() and longjmp() (unless you have compiled libpng with
Packit 0ba690
PNG_SETJMP_NOT_SUPPORTED, in which case it is handled via PNG_ABORT()),
Packit 0ba690
but you could change this to do things like exit() if you should wish.
Packit 0ba690
Packit 0ba690
On non-fatal errors, png_warning() is called
Packit 0ba690
to print a warning message, and then control returns to the calling code.
Packit 0ba690
By default png_error() and png_warning() print a message on stderr via
Packit 0ba690
fprintf() unless the library is compiled with PNG_NO_CONSOLE_IO defined
Packit 0ba690
(because you don't want the messages) or PNG_NO_STDIO defined (because
Packit 0ba690
fprintf() isn't available).  If you wish to change the behavior of the error
Packit 0ba690
functions, you will need to set up your own message callbacks.  These
Packit 0ba690
functions are normally supplied at the time that the png_struct is created.
Packit 0ba690
It is also possible to redirect errors and warnings to your own replacement
Packit 0ba690
functions after png_create_*_struct() has been called by calling:
Packit 0ba690
Packit 0ba690
    png_set_error_fn(png_structp png_ptr,
Packit 0ba690
        png_voidp error_ptr, png_error_ptr error_fn,
Packit 0ba690
        png_error_ptr warning_fn);
Packit 0ba690
Packit 0ba690
    png_voidp error_ptr = png_get_error_ptr(png_ptr);
Packit 0ba690
Packit 0ba690
If NULL is supplied for either error_fn or warning_fn, then the libpng
Packit 0ba690
default function will be used, calling fprintf() and/or longjmp() if a
Packit 0ba690
problem is encountered.  The replacement error functions should have
Packit 0ba690
parameters as follows:
Packit 0ba690
Packit 0ba690
    void user_error_fn(png_structp png_ptr,
Packit 0ba690
        png_const_charp error_msg);
Packit 0ba690
    void user_warning_fn(png_structp png_ptr,
Packit 0ba690
        png_const_charp warning_msg);
Packit 0ba690
Packit 0ba690
The motivation behind using setjmp() and longjmp() is the C++ throw and
Packit 0ba690
catch exception handling methods.  This makes the code much easier to write,
Packit 0ba690
as there is no need to check every return code of every function call.
Packit 0ba690
However, there are some uncertainties about the status of local variables
Packit 0ba690
after a longjmp, so the user may want to be careful about doing anything
Packit 0ba690
after setjmp returns non-zero besides returning itself.  Consult your
Packit 0ba690
compiler documentation for more details.  For an alternative approach, you
Packit 0ba690
may wish to use the "cexcept" facility (see http://cexcept.sourceforge.net).
Packit 0ba690
Packit 0ba690
.SS Custom chunks
Packit 0ba690
Packit 0ba690
If you need to read or write custom chunks, you may need to get deeper
Packit 0ba690
into the libpng code.  The library now has mechanisms for storing
Packit 0ba690
and writing chunks of unknown type; you can even declare callbacks
Packit 0ba690
for custom chunks.  However, this may not be good enough if the
Packit 0ba690
library code itself needs to know about interactions between your
Packit 0ba690
chunk and existing `intrinsic' chunks.
Packit 0ba690
Packit 0ba690
If you need to write a new intrinsic chunk, first read the PNG
Packit 0ba690
specification. Acquire a first level of understanding of how it works.
Packit 0ba690
Pay particular attention to the sections that describe chunk names,
Packit 0ba690
and look at how other chunks were designed, so you can do things
Packit 0ba690
similarly.  Second, check out the sections of libpng that read and
Packit 0ba690
write chunks.  Try to find a chunk that is similar to yours and use
Packit 0ba690
it as a template.  More details can be found in the comments inside
Packit 0ba690
the code.  It is best to handle unknown chunks in a generic method,
Packit 0ba690
via callback functions, instead of by modifying libpng functions.
Packit 0ba690
Packit 0ba690
If you wish to write your own transformation for the data, look through
Packit 0ba690
the part of the code that does the transformations, and check out some of
Packit 0ba690
the simpler ones to get an idea of how they work.  Try to find a similar
Packit 0ba690
transformation to the one you want to add and copy off of it.  More details
Packit 0ba690
can be found in the comments inside the code itself.
Packit 0ba690
Packit 0ba690
.SS Configuring for 16 bit platforms
Packit 0ba690
Packit 0ba690
You will want to look into zconf.h to tell zlib (and thus libpng) that
Packit 0ba690
it cannot allocate more then 64K at a time.  Even if you can, the memory
Packit 0ba690
won't be accessible.  So limit zlib and libpng to 64K by defining MAXSEG_64K.
Packit 0ba690
Packit 0ba690
.SS Configuring for DOS
Packit 0ba690
Packit 0ba690
For DOS users who only have access to the lower 640K, you will
Packit 0ba690
have to limit zlib's memory usage via a png_set_compression_mem_level()
Packit 0ba690
call.  See zlib.h or zconf.h in the zlib library for more information.
Packit 0ba690
Packit 0ba690
.SS Configuring for Medium Model
Packit 0ba690
Packit 0ba690
Libpng's support for medium model has been tested on most of the popular
Packit 0ba690
compilers.  Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets
Packit 0ba690
defined, and FAR gets defined to far in pngconf.h, and you should be
Packit 0ba690
all set.  Everything in the library (except for zlib's structure) is
Packit 0ba690
expecting far data.  You must use the typedefs with the p or pp on
Packit 0ba690
the end for pointers (or at least look at them and be careful).  Make
Packit 0ba690
note that the rows of data are defined as png_bytepp, which is an
Packit 0ba690
unsigned char far * far *.
Packit 0ba690
Packit 0ba690
.SS Configuring for gui/windowing platforms:
Packit 0ba690
Packit 0ba690
You will need to write new error and warning functions that use the GUI
Packit 0ba690
interface, as described previously, and set them to be the error and
Packit 0ba690
warning functions at the time that png_create_*_struct() is called,
Packit 0ba690
in order to have them available during the structure initialization.
Packit 0ba690
They can be changed later via png_set_error_fn().  On some compilers,
Packit 0ba690
you may also have to change the memory allocators (png_malloc, etc.).
Packit 0ba690
Packit 0ba690
.SS Configuring for compiler xxx:
Packit 0ba690
Packit 0ba690
All includes for libpng are in pngconf.h.  If you need to add, change
Packit 0ba690
or delete an include, this is the place to do it.
Packit 0ba690
The includes that are not needed outside libpng are protected by the
Packit 0ba690
PNG_INTERNAL definition, which is only defined for those routines inside
Packit 0ba690
libpng itself.  The files in libpng proper only include png.h, which
Packit 0ba690
includes pngconf.h.
Packit 0ba690
Packit 0ba690
.SS Configuring zlib:
Packit 0ba690
Packit 0ba690
There are special functions to configure the compression.  Perhaps the
Packit 0ba690
most useful one changes the compression level, which currently uses
Packit 0ba690
input compression values in the range 0 - 9.  The library normally
Packit 0ba690
uses the default compression level (Z_DEFAULT_COMPRESSION = 6).  Tests
Packit 0ba690
have shown that for a large majority of images, compression values in
Packit 0ba690
the range 3-6 compress nearly as well as higher levels, and do so much
Packit 0ba690
faster.  For online applications it may be desirable to have maximum speed
Packit 0ba690
(Z_BEST_SPEED = 1).  With versions of zlib after v0.99, you can also
Packit 0ba690
specify no compression (Z_NO_COMPRESSION = 0), but this would create
Packit 0ba690
files larger than just storing the raw bitmap.  You can specify the
Packit 0ba690
compression level by calling:
Packit 0ba690
Packit 0ba690
    png_set_compression_level(png_ptr, level);
Packit 0ba690
Packit 0ba690
Another useful one is to reduce the memory level used by the library.
Packit 0ba690
The memory level defaults to 8, but it can be lowered if you are
Packit 0ba690
short on memory (running DOS, for example, where you only have 640K).
Packit 0ba690
Note that the memory level does have an effect on compression; among
Packit 0ba690
other things, lower levels will result in sections of incompressible
Packit 0ba690
data being emitted in smaller stored blocks, with a correspondingly
Packit 0ba690
larger relative overhead of up to 15% in the worst case.
Packit 0ba690
Packit 0ba690
    png_set_compression_mem_level(png_ptr, level);
Packit 0ba690
Packit 0ba690
The other functions are for configuring zlib.  They are not recommended
Packit 0ba690
for normal use and may result in writing an invalid PNG file.  See
Packit 0ba690
zlib.h for more information on what these mean.
Packit 0ba690
Packit 0ba690
    png_set_compression_strategy(png_ptr,
Packit 0ba690
        strategy);
Packit 0ba690
    png_set_compression_window_bits(png_ptr,
Packit 0ba690
        window_bits);
Packit 0ba690
    png_set_compression_method(png_ptr, method);
Packit 0ba690
    png_set_compression_buffer_size(png_ptr, size);
Packit 0ba690
Packit 0ba690
.SS Controlling row filtering
Packit 0ba690
Packit 0ba690
If you want to control whether libpng uses filtering or not, which
Packit 0ba690
filters are used, and how it goes about picking row filters, you
Packit 0ba690
can call one of these functions.  The selection and configuration
Packit 0ba690
of row filters can have a significant impact on the size and
Packit 0ba690
encoding speed and a somewhat lesser impact on the decoding speed
Packit 0ba690
of an image.  Filtering is enabled by default for RGB and grayscale
Packit 0ba690
images (with and without alpha), but not for paletted images nor
Packit 0ba690
for any images with bit depths less than 8 bits/pixel.
Packit 0ba690
Packit 0ba690
The 'method' parameter sets the main filtering method, which is
Packit 0ba690
currently only '0' in the PNG 1.2 specification.  The 'filters'
Packit 0ba690
parameter sets which filter(s), if any, should be used for each
Packit 0ba690
scanline.  Possible values are PNG_ALL_FILTERS and PNG_NO_FILTERS
Packit 0ba690
to turn filtering on and off, respectively.
Packit 0ba690
Packit 0ba690
Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB,
Packit 0ba690
PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise
Packit 0ba690
ORed together with '|' to specify one or more filters to use.
Packit 0ba690
These filters are described in more detail in the PNG specification.
Packit 0ba690
If you intend to change the filter type during the course of writing
Packit 0ba690
the image, you should start with flags set for all of the filters
Packit 0ba690
you intend to use so that libpng can initialize its internal
Packit 0ba690
structures appropriately for all of the filter types.  (Note that this
Packit 0ba690
means the first row must always be adaptively filtered, because libpng
Packit 0ba690
currently does not allocate the filter buffers until png_write_row()
Packit 0ba690
is called for the first time.)
Packit 0ba690
Packit 0ba690
    filters = PNG_FILTER_NONE | PNG_FILTER_SUB
Packit 0ba690
              PNG_FILTER_UP | PNG_FILTER_AVG |
Packit 0ba690
              PNG_FILTER_PAETH | PNG_ALL_FILTERS;
Packit 0ba690
Packit 0ba690
    png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE,
Packit 0ba690
       filters);
Packit 0ba690
              The second parameter can also be
Packit 0ba690
              PNG_INTRAPIXEL_DIFFERENCING if you are
Packit 0ba690
              writing a PNG to be embedded in a MNG
Packit 0ba690
              datastream.  This parameter must be the
Packit 0ba690
              same as the value of filter_method used
Packit 0ba690
              in png_set_IHDR().
Packit 0ba690
Packit