/* pngstruct.h - internal structures for libpng1*2* Copyright (c) 2018-2025 Cosmin Truta3* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson4* Copyright (c) 1996-1997 Andreas Dilger5* Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.6*7* This code is released under the libpng license.8* For conditions of distribution and use, see the disclaimer9* and license in png.h10*/1112#ifndef PNGPRIV_H13# error This file must not be included by applications; please include <png.h>14#endif1516#ifndef PNGSTRUCT_H17#define PNGSTRUCT_H18/* zlib.h defines the structure z_stream, an instance of which is included19* in this structure and is required for decompressing the LZ compressed20* data in PNG files.21*/22#ifndef ZLIB_CONST23/* We must ensure that zlib uses 'const' in declarations. */24# define ZLIB_CONST25#endif26#include "zlib.h"27#ifdef const28/* zlib.h sometimes #defines const to nothing, undo this. */29# undef const30#endif3132/* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility33* with older builds.34*/35#if ZLIB_VERNUM < 0x126036# define PNGZ_MSG_CAST(s) png_constcast(char*,s)37# define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b)38#else39# define PNGZ_MSG_CAST(s) (s)40# define PNGZ_INPUT_CAST(b) (b)41#endif4243/* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib44* can handle at once. This type need be no larger than 16 bits (so maximum of45* 65535), this define allows us to discover how big it is, but limited by the46* maximum for size_t. The value can be overridden in a library build47* (pngusr.h, or set it in CPPFLAGS) and it works to set it to a considerably48* lower value (e.g. 255 works). A lower value may help memory usage (slightly)49* and may even improve performance on some systems (and degrade it on others.)50*/51#ifndef ZLIB_IO_MAX52# define ZLIB_IO_MAX ((uInt)-1)53#endif5455#ifdef PNG_WRITE_SUPPORTED56/* The type of a compression buffer list used by the write code. */57typedef struct png_compression_buffer58{59struct png_compression_buffer *next;60png_byte output[1]; /* actually zbuf_size */61} png_compression_buffer, *png_compression_bufferp;6263#define PNG_COMPRESSION_BUFFER_SIZE(pp)\64(offsetof(png_compression_buffer, output) + (pp)->zbuffer_size)65#endif6667/* Colorspace support; structures used in png_struct, png_info and in internal68* functions to hold and communicate information about the color space.69*/70/* The chromaticities of the red, green and blue colorants and the chromaticity71* of the corresponding white point (i.e. of rgb(1.0,1.0,1.0)).72*/73typedef struct png_xy74{75png_fixed_point redx, redy;76png_fixed_point greenx, greeny;77png_fixed_point bluex, bluey;78png_fixed_point whitex, whitey;79} png_xy;8081/* The same data as above but encoded as CIE XYZ values. When this data comes82* from chromaticities the sum of the Y values is assumed to be 1.083*/84typedef struct png_XYZ85{86png_fixed_point red_X, red_Y, red_Z;87png_fixed_point green_X, green_Y, green_Z;88png_fixed_point blue_X, blue_Y, blue_Z;89} png_XYZ;9091/* Chunk index values as an enum, PNG_INDEX_unknown is also a count of the92* number of chunks.93*/94#define PNG_CHUNK(cHNK, i) PNG_INDEX_ ## cHNK = (i),95typedef enum96{97PNG_KNOWN_CHUNKS98PNG_INDEX_unknown99} png_index;100#undef PNG_CHUNK101102/* Chunk flag values. These are (png_uint_32 values) with exactly one bit set103* and can be combined into a flag set with bitwise 'or'.104*105* TODO: C23: convert these macros to C23 inlines (which are static).106*/107#define png_chunk_flag_from_index(i) (0x80000000U >> (31 - (i)))108/* The flag coresponding to the given png_index enum value. This is defined109* for png_unknown as well (until it reaches the value 32) but this should110* not be relied on.111*/112113#define png_file_has_chunk(png_ptr, i)\114(((png_ptr)->chunks & png_chunk_flag_from_index(i)) != 0)115/* The chunk has been recorded in png_struct */116117#define png_file_add_chunk(pnt_ptr, i)\118((void)((png_ptr)->chunks |= png_chunk_flag_from_index(i)))119/* Record the chunk in the png_struct */120121struct png_struct_def122{123#ifdef PNG_SETJMP_SUPPORTED124jmp_buf jmp_buf_local; /* New name in 1.6.0 for jmp_buf in png_struct */125png_longjmp_ptr longjmp_fn;/* setjmp non-local goto function. */126jmp_buf *jmp_buf_ptr; /* passed to longjmp_fn */127size_t jmp_buf_size; /* size of the above, if allocated */128#endif129png_error_ptr error_fn; /* function for printing errors and aborting */130#ifdef PNG_WARNINGS_SUPPORTED131png_error_ptr warning_fn; /* function for printing warnings */132#endif133png_voidp error_ptr; /* user supplied struct for error functions */134png_rw_ptr write_data_fn; /* function for writing output data */135png_rw_ptr read_data_fn; /* function for reading input data */136png_voidp io_ptr; /* ptr to application struct for I/O functions */137138#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED139png_user_transform_ptr read_user_transform_fn; /* user read transform */140#endif141142#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED143png_user_transform_ptr write_user_transform_fn; /* user write transform */144#endif145146/* These were added in libpng-1.0.2 */147#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED148#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \149defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)150png_voidp user_transform_ptr; /* user supplied struct for user transform */151png_byte user_transform_depth; /* bit depth of user transformed pixels */152png_byte user_transform_channels; /* channels in user transformed pixels */153#endif154#endif155156png_uint_32 mode; /* tells us where we are in the PNG file */157png_uint_32 flags; /* flags indicating various things to libpng */158png_uint_32 transformations; /* which transformations to perform */159160png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */161z_stream zstream; /* decompression structure */162163#ifdef PNG_WRITE_SUPPORTED164png_compression_bufferp zbuffer_list; /* Created on demand during write */165uInt zbuffer_size; /* size of the actual buffer */166167int zlib_level; /* holds zlib compression level */168int zlib_method; /* holds zlib compression method */169int zlib_window_bits; /* holds zlib compression window bits */170int zlib_mem_level; /* holds zlib compression memory level */171int zlib_strategy; /* holds zlib compression strategy */172#endif173/* Added at libpng 1.5.4 */174#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED175int zlib_text_level; /* holds zlib compression level */176int zlib_text_method; /* holds zlib compression method */177int zlib_text_window_bits; /* holds zlib compression window bits */178int zlib_text_mem_level; /* holds zlib compression memory level */179int zlib_text_strategy; /* holds zlib compression strategy */180#endif181/* End of material added at libpng 1.5.4 */182/* Added at libpng 1.6.0 */183#ifdef PNG_WRITE_SUPPORTED184int zlib_set_level; /* Actual values set into the zstream on write */185int zlib_set_method;186int zlib_set_window_bits;187int zlib_set_mem_level;188int zlib_set_strategy;189#endif190191png_uint_32 chunks; /* PNG_CF_ for every chunk read or (NYI) written */192# define png_has_chunk(png_ptr, cHNK)\193png_file_has_chunk(png_ptr, PNG_INDEX_ ## cHNK)194/* Convenience accessor - use this to check for a known chunk by name */195196png_uint_32 width; /* width of image in pixels */197png_uint_32 height; /* height of image in pixels */198png_uint_32 num_rows; /* number of rows in current pass */199png_uint_32 usr_width; /* width of row at start of write */200size_t rowbytes; /* size of row in bytes */201png_uint_32 iwidth; /* width of current interlaced row in pixels */202png_uint_32 row_number; /* current row in interlace pass */203png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */204png_bytep prev_row; /* buffer to save previous (unfiltered) row.205* While reading this is a pointer into206* big_prev_row; while writing it is separately207* allocated if needed.208*/209png_bytep row_buf; /* buffer to save current (unfiltered) row.210* While reading, this is a pointer into211* big_row_buf; while writing it is separately212* allocated.213*/214#ifdef PNG_WRITE_FILTER_SUPPORTED215png_bytep try_row; /* buffer to save trial row when filtering */216png_bytep tst_row; /* buffer to save best trial row when filtering */217#endif218size_t info_rowbytes; /* Added in 1.5.4: cache of updated row bytes */219220png_uint_32 idat_size; /* current IDAT size for read */221png_uint_32 crc; /* current chunk CRC value */222png_colorp palette; /* palette from the input file */223png_uint_16 num_palette; /* number of color entries in palette */224225/* Added at libpng-1.5.10 */226#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED227int num_palette_max; /* maximum palette index found in IDAT */228#endif229230png_uint_16 num_trans; /* number of transparency values */231png_byte compression; /* file compression type (always 0) */232png_byte filter; /* file filter type (always 0) */233png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */234png_byte pass; /* current interlace pass (0 - 6) */235png_byte do_filter; /* row filter flags (see PNG_FILTER_ in png.h ) */236png_byte color_type; /* color type of file */237png_byte bit_depth; /* bit depth of file */238png_byte usr_bit_depth; /* bit depth of users row: write only */239png_byte pixel_depth; /* number of bits per pixel */240png_byte channels; /* number of channels in file */241#ifdef PNG_WRITE_SUPPORTED242png_byte usr_channels; /* channels at start of write: write only */243#endif244png_byte sig_bytes; /* magic bytes read/written from start of file */245png_byte maximum_pixel_depth;246/* pixel depth used for the row buffers */247png_byte transformed_pixel_depth;248/* pixel depth after read/write transforms */249#if ZLIB_VERNUM >= 0x1240250png_byte zstream_start; /* at start of an input zlib stream */251#endif /* Zlib >= 1.2.4 */252#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)253png_uint_16 filler; /* filler bytes for pixel expansion */254#endif255256#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ||\257defined(PNG_READ_ALPHA_MODE_SUPPORTED)258png_byte background_gamma_type;259png_fixed_point background_gamma;260png_color_16 background; /* background color in screen gamma space */261#ifdef PNG_READ_GAMMA_SUPPORTED262png_color_16 background_1; /* background normalized to gamma 1.0 */263#endif264#endif /* bKGD */265266#ifdef PNG_WRITE_FLUSH_SUPPORTED267png_flush_ptr output_flush_fn; /* Function for flushing output */268png_uint_32 flush_dist; /* how many rows apart to flush, 0 - no flush */269png_uint_32 flush_rows; /* number of rows written since last flush */270#endif271272#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED273png_xy chromaticities; /* From mDVC, cICP, [iCCP], sRGB or cHRM */274#endif275276#ifdef PNG_READ_GAMMA_SUPPORTED277int gamma_shift; /* number of "insignificant" bits in 16-bit gamma */278png_fixed_point screen_gamma; /* screen gamma value (display exponent) */279png_fixed_point file_gamma; /* file gamma value (encoding exponent) */280png_fixed_point chunk_gamma; /* from cICP, iCCP, sRGB or gAMA */281png_fixed_point default_gamma;/* from png_set_alpha_mode */282283png_bytep gamma_table; /* gamma table for 8-bit depth files */284png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */285#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \286defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \287defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)288png_bytep gamma_from_1; /* converts from 1.0 to screen */289png_bytep gamma_to_1; /* converts from file to 1.0 */290png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */291png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */292#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */293#endif /* READ_GAMMA */294295#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)296png_color_8 sig_bit; /* significant bits in each available channel */297#endif298299#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)300png_color_8 shift; /* shift for significant bit transformation */301#endif302303#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \304|| defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)305png_bytep trans_alpha; /* alpha values for paletted files */306png_color_16 trans_color; /* transparent color for non-paletted files */307#endif308309png_read_status_ptr read_row_fn; /* called after each row is decoded */310png_write_status_ptr write_row_fn; /* called after each row is encoded */311#ifdef PNG_PROGRESSIVE_READ_SUPPORTED312png_progressive_info_ptr info_fn; /* called after header data fully read */313png_progressive_row_ptr row_fn; /* called after a prog. row is decoded */314png_progressive_end_ptr end_fn; /* called after image is complete */315png_bytep save_buffer_ptr; /* current location in save_buffer */316png_bytep save_buffer; /* buffer for previously read data */317png_bytep current_buffer_ptr; /* current location in current_buffer */318png_bytep current_buffer; /* buffer for recently used data */319png_uint_32 push_length; /* size of current input chunk */320png_uint_32 skip_length; /* bytes to skip in input data */321size_t save_buffer_size; /* amount of data now in save_buffer */322size_t save_buffer_max; /* total size of save_buffer */323size_t buffer_size; /* total amount of available input data */324size_t current_buffer_size; /* amount of data now in current_buffer */325int process_mode; /* what push library is currently doing */326int cur_palette; /* current push library palette index */327#endif /* PROGRESSIVE_READ */328329#ifdef PNG_READ_QUANTIZE_SUPPORTED330png_bytep palette_lookup; /* lookup table for quantizing */331png_bytep quantize_index; /* index translation for palette files */332#endif333334/* Options */335#ifdef PNG_SET_OPTION_SUPPORTED336png_uint_32 options; /* On/off state (up to 16 options) */337#endif338339#if PNG_LIBPNG_VER < 10700340/* To do: remove this from libpng-1.7 */341#ifdef PNG_TIME_RFC1123_SUPPORTED342char time_buffer[29]; /* String to hold RFC 1123 time text */343#endif /* TIME_RFC1123 */344#endif /* LIBPNG_VER < 10700 */345346/* New members added in libpng-1.0.6 */347348png_uint_32 free_me; /* flags items libpng is responsible for freeing */349350#ifdef PNG_USER_CHUNKS_SUPPORTED351png_voidp user_chunk_ptr;352#ifdef PNG_READ_USER_CHUNKS_SUPPORTED353png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */354#endif /* READ_USER_CHUNKS */355#endif /* USER_CHUNKS */356357#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED358int unknown_default; /* As PNG_HANDLE_* */359unsigned int num_chunk_list; /* Number of entries in the list */360png_bytep chunk_list; /* List of png_byte[5]; the textual chunk name361* followed by a PNG_HANDLE_* byte */362#endif363364/* New members added in libpng-1.0.3 */365#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED366png_byte rgb_to_gray_status;367/* Added in libpng 1.5.5 to record setting of coefficients: */368png_byte rgb_to_gray_coefficients_set;369/* These were changed from png_byte in libpng-1.0.6 */370png_uint_16 rgb_to_gray_red_coeff;371png_uint_16 rgb_to_gray_green_coeff;372/* deleted in 1.5.5: rgb_to_gray_blue_coeff; */373#endif374375/* New member added in libpng-1.6.36 */376#if defined(PNG_READ_EXPAND_SUPPORTED) && \377defined(PNG_ARM_NEON_IMPLEMENTATION)378png_bytep riffled_palette; /* buffer for accelerated palette expansion */379#endif380381/* New member added in libpng-1.0.4 (renamed in 1.0.9) */382#if defined(PNG_MNG_FEATURES_SUPPORTED)383/* Changed from png_byte to png_uint_32 at version 1.2.0 */384png_uint_32 mng_features_permitted;385#endif386387/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */388#ifdef PNG_MNG_FEATURES_SUPPORTED389png_byte filter_type;390#endif391392/* New members added in libpng-1.2.0 */393394/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */395#ifdef PNG_USER_MEM_SUPPORTED396png_voidp mem_ptr; /* user supplied struct for mem functions */397png_malloc_ptr malloc_fn; /* function for allocating memory */398png_free_ptr free_fn; /* function for freeing memory */399#endif400401/* New member added in libpng-1.0.13 and 1.2.0 */402png_bytep big_row_buf; /* buffer to save current (unfiltered) row */403404#ifdef PNG_READ_QUANTIZE_SUPPORTED405/* The following three members were added at version 1.0.14 and 1.2.4 */406png_bytep quantize_sort; /* working sort array */407png_bytep index_to_palette; /* where the original index currently is408in the palette */409png_bytep palette_to_index; /* which original index points to this410palette color */411#endif412413/* New members added in libpng-1.0.16 and 1.2.6 */414png_byte compression_type;415416#ifdef PNG_USER_LIMITS_SUPPORTED417png_uint_32 user_width_max;418png_uint_32 user_height_max;419420/* Added in libpng-1.4.0: Total number of sPLT, text, and unknown421* chunks that can be stored (0 means unlimited).422*/423png_uint_32 user_chunk_cache_max;424425/* Total memory that a zTXt, sPLT, iTXt, iCCP, or unknown chunk426* can occupy when decompressed. 0 means unlimited.427*/428png_alloc_size_t user_chunk_malloc_max;429#endif430431/* New member added in libpng-1.0.25 and 1.2.17 */432#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED433/* Temporary storage for unknown chunk that the library doesn't recognize,434* used while reading the chunk.435*/436png_unknown_chunk unknown_chunk;437#endif438439/* New member added in libpng-1.2.26 */440size_t old_big_row_buf_size;441442#ifdef PNG_READ_SUPPORTED443/* New member added in libpng-1.2.30 */444png_bytep read_buffer; /* buffer for reading chunk data */445png_alloc_size_t read_buffer_size; /* current size of the buffer */446#endif447#ifdef PNG_SEQUENTIAL_READ_SUPPORTED448uInt IDAT_read_size; /* limit on read buffer size for IDAT */449#endif450451#ifdef PNG_IO_STATE_SUPPORTED452/* New member added in libpng-1.4.0 */453png_uint_32 io_state;454#endif455456/* New member added in libpng-1.5.6 */457png_bytep big_prev_row;458459/* New member added in libpng-1.5.7 */460void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info,461png_bytep row, png_const_bytep prev_row);462};463#endif /* PNGSTRUCT_H */464465466