pngstruct.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* pngstruct.h - internal structures for libpng
  2. *
  3. * Copyright (c) 2018-2025 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #ifndef PNGPRIV_H
  13. # error This file must not be included by applications; please include <png.h>
  14. #endif
  15. #ifndef PNGSTRUCT_H
  16. #define PNGSTRUCT_H
  17. /* zlib.h defines the structure z_stream, an instance of which is included
  18. * in this structure and is required for decompressing the LZ compressed
  19. * data in PNG files.
  20. */
  21. #ifndef ZLIB_CONST
  22. /* We must ensure that zlib uses 'const' in declarations. */
  23. # define ZLIB_CONST
  24. #endif
  25. #include "zlib.h"
  26. #ifdef const
  27. /* zlib.h sometimes #defines const to nothing, undo this. */
  28. # undef const
  29. #endif
  30. /* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility
  31. * with older builds.
  32. */
  33. #if ZLIB_VERNUM < 0x1260
  34. # define PNGZ_MSG_CAST(s) png_constcast(char*,s)
  35. # define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b)
  36. #else
  37. # define PNGZ_MSG_CAST(s) (s)
  38. # define PNGZ_INPUT_CAST(b) (b)
  39. #endif
  40. /* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib
  41. * can handle at once. This type need be no larger than 16 bits (so maximum of
  42. * 65535), this define allows us to discover how big it is, but limited by the
  43. * maximum for size_t. The value can be overridden in a library build
  44. * (pngusr.h, or set it in CPPFLAGS) and it works to set it to a considerably
  45. * lower value (e.g. 255 works). A lower value may help memory usage (slightly)
  46. * and may even improve performance on some systems (and degrade it on others.)
  47. */
  48. #ifndef ZLIB_IO_MAX
  49. # define ZLIB_IO_MAX ((uInt)-1)
  50. #endif
  51. #ifdef PNG_WRITE_SUPPORTED
  52. /* The type of a compression buffer list used by the write code. */
  53. typedef struct png_compression_buffer
  54. {
  55. struct png_compression_buffer *next;
  56. png_byte output[1]; /* actually zbuf_size */
  57. } png_compression_buffer, *png_compression_bufferp;
  58. #define PNG_COMPRESSION_BUFFER_SIZE(pp)\
  59. (offsetof(png_compression_buffer, output) + (pp)->zbuffer_size)
  60. #endif
  61. /* Colorspace support; structures used in png_struct, png_info and in internal
  62. * functions to hold and communicate information about the color space.
  63. */
  64. /* The chromaticities of the red, green and blue colorants and the chromaticity
  65. * of the corresponding white point (i.e. of rgb(1.0,1.0,1.0)).
  66. */
  67. typedef struct png_xy
  68. {
  69. png_fixed_point redx, redy;
  70. png_fixed_point greenx, greeny;
  71. png_fixed_point bluex, bluey;
  72. png_fixed_point whitex, whitey;
  73. } png_xy;
  74. /* The same data as above but encoded as CIE XYZ values. When this data comes
  75. * from chromaticities the sum of the Y values is assumed to be 1.0
  76. */
  77. typedef struct png_XYZ
  78. {
  79. png_fixed_point red_X, red_Y, red_Z;
  80. png_fixed_point green_X, green_Y, green_Z;
  81. png_fixed_point blue_X, blue_Y, blue_Z;
  82. } png_XYZ;
  83. /* Chunk index values as an enum, PNG_INDEX_unknown is also a count of the
  84. * number of chunks.
  85. */
  86. #define PNG_CHUNK(cHNK, i) PNG_INDEX_ ## cHNK = (i),
  87. typedef enum
  88. {
  89. PNG_KNOWN_CHUNKS
  90. PNG_INDEX_unknown
  91. } png_index;
  92. #undef PNG_CHUNK
  93. /* Chunk flag values. These are (png_uint_32 values) with exactly one bit set
  94. * and can be combined into a flag set with bitwise 'or'.
  95. *
  96. * TODO: C23: convert these macros to C23 inlines (which are static).
  97. */
  98. #define png_chunk_flag_from_index(i) (0x80000000U >> (31 - (i)))
  99. /* The flag coresponding to the given png_index enum value. This is defined
  100. * for png_unknown as well (until it reaches the value 32) but this should
  101. * not be relied on.
  102. */
  103. #define png_file_has_chunk(png_ptr, i)\
  104. (((png_ptr)->chunks & png_chunk_flag_from_index(i)) != 0)
  105. /* The chunk has been recorded in png_struct */
  106. #define png_file_add_chunk(pnt_ptr, i)\
  107. ((void)((png_ptr)->chunks |= png_chunk_flag_from_index(i)))
  108. /* Record the chunk in the png_struct */
  109. struct png_struct_def
  110. {
  111. #ifdef PNG_SETJMP_SUPPORTED
  112. jmp_buf jmp_buf_local; /* New name in 1.6.0 for jmp_buf in png_struct */
  113. png_longjmp_ptr longjmp_fn;/* setjmp non-local goto function. */
  114. jmp_buf *jmp_buf_ptr; /* passed to longjmp_fn */
  115. size_t jmp_buf_size; /* size of the above, if allocated */
  116. #endif
  117. png_error_ptr error_fn; /* function for printing errors and aborting */
  118. #ifdef PNG_WARNINGS_SUPPORTED
  119. png_error_ptr warning_fn; /* function for printing warnings */
  120. #endif
  121. png_voidp error_ptr; /* user supplied struct for error functions */
  122. png_rw_ptr write_data_fn; /* function for writing output data */
  123. png_rw_ptr read_data_fn; /* function for reading input data */
  124. png_voidp io_ptr; /* ptr to application struct for I/O functions */
  125. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  126. png_user_transform_ptr read_user_transform_fn; /* user read transform */
  127. #endif
  128. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  129. png_user_transform_ptr write_user_transform_fn; /* user write transform */
  130. #endif
  131. /* These were added in libpng-1.0.2 */
  132. #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
  133. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  134. defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  135. png_voidp user_transform_ptr; /* user supplied struct for user transform */
  136. png_byte user_transform_depth; /* bit depth of user transformed pixels */
  137. png_byte user_transform_channels; /* channels in user transformed pixels */
  138. #endif
  139. #endif
  140. png_uint_32 mode; /* tells us where we are in the PNG file */
  141. png_uint_32 flags; /* flags indicating various things to libpng */
  142. png_uint_32 transformations; /* which transformations to perform */
  143. png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */
  144. z_stream zstream; /* decompression structure */
  145. #ifdef PNG_WRITE_SUPPORTED
  146. png_compression_bufferp zbuffer_list; /* Created on demand during write */
  147. uInt zbuffer_size; /* size of the actual buffer */
  148. int zlib_level; /* holds zlib compression level */
  149. int zlib_method; /* holds zlib compression method */
  150. int zlib_window_bits; /* holds zlib compression window bits */
  151. int zlib_mem_level; /* holds zlib compression memory level */
  152. int zlib_strategy; /* holds zlib compression strategy */
  153. #endif
  154. /* Added at libpng 1.5.4 */
  155. #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  156. int zlib_text_level; /* holds zlib compression level */
  157. int zlib_text_method; /* holds zlib compression method */
  158. int zlib_text_window_bits; /* holds zlib compression window bits */
  159. int zlib_text_mem_level; /* holds zlib compression memory level */
  160. int zlib_text_strategy; /* holds zlib compression strategy */
  161. #endif
  162. /* End of material added at libpng 1.5.4 */
  163. /* Added at libpng 1.6.0 */
  164. #ifdef PNG_WRITE_SUPPORTED
  165. int zlib_set_level; /* Actual values set into the zstream on write */
  166. int zlib_set_method;
  167. int zlib_set_window_bits;
  168. int zlib_set_mem_level;
  169. int zlib_set_strategy;
  170. #endif
  171. png_uint_32 chunks; /* PNG_CF_ for every chunk read or (NYI) written */
  172. # define png_has_chunk(png_ptr, cHNK)\
  173. png_file_has_chunk(png_ptr, PNG_INDEX_ ## cHNK)
  174. /* Convenience accessor - use this to check for a known chunk by name */
  175. png_uint_32 width; /* width of image in pixels */
  176. png_uint_32 height; /* height of image in pixels */
  177. png_uint_32 num_rows; /* number of rows in current pass */
  178. png_uint_32 usr_width; /* width of row at start of write */
  179. size_t rowbytes; /* size of row in bytes */
  180. png_uint_32 iwidth; /* width of current interlaced row in pixels */
  181. png_uint_32 row_number; /* current row in interlace pass */
  182. png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */
  183. png_bytep prev_row; /* buffer to save previous (unfiltered) row.
  184. * While reading this is a pointer into
  185. * big_prev_row; while writing it is separately
  186. * allocated if needed.
  187. */
  188. png_bytep row_buf; /* buffer to save current (unfiltered) row.
  189. * While reading, this is a pointer into
  190. * big_row_buf; while writing it is separately
  191. * allocated.
  192. */
  193. #ifdef PNG_WRITE_FILTER_SUPPORTED
  194. png_bytep try_row; /* buffer to save trial row when filtering */
  195. png_bytep tst_row; /* buffer to save best trial row when filtering */
  196. #endif
  197. size_t info_rowbytes; /* Added in 1.5.4: cache of updated row bytes */
  198. png_uint_32 idat_size; /* current IDAT size for read */
  199. png_uint_32 crc; /* current chunk CRC value */
  200. png_colorp palette; /* palette from the input file */
  201. png_uint_16 num_palette; /* number of color entries in palette */
  202. /* Added at libpng-1.5.10 */
  203. #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  204. int num_palette_max; /* maximum palette index found in IDAT */
  205. #endif
  206. png_uint_16 num_trans; /* number of transparency values */
  207. png_byte compression; /* file compression type (always 0) */
  208. png_byte filter; /* file filter type (always 0) */
  209. png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
  210. png_byte pass; /* current interlace pass (0 - 6) */
  211. png_byte do_filter; /* row filter flags (see PNG_FILTER_ in png.h ) */
  212. png_byte color_type; /* color type of file */
  213. png_byte bit_depth; /* bit depth of file */
  214. png_byte usr_bit_depth; /* bit depth of users row: write only */
  215. png_byte pixel_depth; /* number of bits per pixel */
  216. png_byte channels; /* number of channels in file */
  217. #ifdef PNG_WRITE_SUPPORTED
  218. png_byte usr_channels; /* channels at start of write: write only */
  219. #endif
  220. png_byte sig_bytes; /* magic bytes read/written from start of file */
  221. png_byte maximum_pixel_depth;
  222. /* pixel depth used for the row buffers */
  223. png_byte transformed_pixel_depth;
  224. /* pixel depth after read/write transforms */
  225. #if ZLIB_VERNUM >= 0x1240
  226. png_byte zstream_start; /* at start of an input zlib stream */
  227. #endif /* Zlib >= 1.2.4 */
  228. #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
  229. png_uint_16 filler; /* filler bytes for pixel expansion */
  230. #endif
  231. #if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  232. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  233. png_byte background_gamma_type;
  234. png_fixed_point background_gamma;
  235. png_color_16 background; /* background color in screen gamma space */
  236. #ifdef PNG_READ_GAMMA_SUPPORTED
  237. png_color_16 background_1; /* background normalized to gamma 1.0 */
  238. #endif
  239. #endif /* bKGD */
  240. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  241. png_flush_ptr output_flush_fn; /* Function for flushing output */
  242. png_uint_32 flush_dist; /* how many rows apart to flush, 0 - no flush */
  243. png_uint_32 flush_rows; /* number of rows written since last flush */
  244. #endif
  245. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  246. png_xy chromaticities; /* From mDVC, cICP, [iCCP], sRGB or cHRM */
  247. #endif
  248. #ifdef PNG_READ_GAMMA_SUPPORTED
  249. int gamma_shift; /* number of "insignificant" bits in 16-bit gamma */
  250. png_fixed_point screen_gamma; /* screen gamma value (display exponent) */
  251. png_fixed_point file_gamma; /* file gamma value (encoding exponent) */
  252. png_fixed_point chunk_gamma; /* from cICP, iCCP, sRGB or gAMA */
  253. png_fixed_point default_gamma;/* from png_set_alpha_mode */
  254. png_bytep gamma_table; /* gamma table for 8-bit depth files */
  255. png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */
  256. #if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
  257. defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \
  258. defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  259. png_bytep gamma_from_1; /* converts from 1.0 to screen */
  260. png_bytep gamma_to_1; /* converts from file to 1.0 */
  261. png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
  262. png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
  263. #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
  264. #endif /* READ_GAMMA */
  265. #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED)
  266. png_color_8 sig_bit; /* significant bits in each available channel */
  267. #endif
  268. #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
  269. png_color_8 shift; /* shift for significant bit transformation */
  270. #endif
  271. #if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \
  272. || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  273. png_bytep trans_alpha; /* alpha values for paletted files */
  274. png_color_16 trans_color; /* transparent color for non-paletted files */
  275. #endif
  276. png_read_status_ptr read_row_fn; /* called after each row is decoded */
  277. png_write_status_ptr write_row_fn; /* called after each row is encoded */
  278. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  279. png_progressive_info_ptr info_fn; /* called after header data fully read */
  280. png_progressive_row_ptr row_fn; /* called after a prog. row is decoded */
  281. png_progressive_end_ptr end_fn; /* called after image is complete */
  282. png_bytep save_buffer_ptr; /* current location in save_buffer */
  283. png_bytep save_buffer; /* buffer for previously read data */
  284. png_bytep current_buffer_ptr; /* current location in current_buffer */
  285. png_bytep current_buffer; /* buffer for recently used data */
  286. png_uint_32 push_length; /* size of current input chunk */
  287. png_uint_32 skip_length; /* bytes to skip in input data */
  288. size_t save_buffer_size; /* amount of data now in save_buffer */
  289. size_t save_buffer_max; /* total size of save_buffer */
  290. size_t buffer_size; /* total amount of available input data */
  291. size_t current_buffer_size; /* amount of data now in current_buffer */
  292. int process_mode; /* what push library is currently doing */
  293. int cur_palette; /* current push library palette index */
  294. #endif /* PROGRESSIVE_READ */
  295. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  296. png_bytep palette_lookup; /* lookup table for quantizing */
  297. png_bytep quantize_index; /* index translation for palette files */
  298. #endif
  299. /* Options */
  300. #ifdef PNG_SET_OPTION_SUPPORTED
  301. png_uint_32 options; /* On/off state (up to 16 options) */
  302. #endif
  303. #if PNG_LIBPNG_VER < 10700
  304. /* To do: remove this from libpng-1.7 */
  305. #ifdef PNG_TIME_RFC1123_SUPPORTED
  306. char time_buffer[29]; /* String to hold RFC 1123 time text */
  307. #endif /* TIME_RFC1123 */
  308. #endif /* LIBPNG_VER < 10700 */
  309. /* New members added in libpng-1.0.6 */
  310. png_uint_32 free_me; /* flags items libpng is responsible for freeing */
  311. #ifdef PNG_USER_CHUNKS_SUPPORTED
  312. png_voidp user_chunk_ptr;
  313. #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
  314. png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
  315. #endif /* READ_USER_CHUNKS */
  316. #endif /* USER_CHUNKS */
  317. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  318. int unknown_default; /* As PNG_HANDLE_* */
  319. unsigned int num_chunk_list; /* Number of entries in the list */
  320. png_bytep chunk_list; /* List of png_byte[5]; the textual chunk name
  321. * followed by a PNG_HANDLE_* byte */
  322. #endif
  323. /* New members added in libpng-1.0.3 */
  324. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  325. png_byte rgb_to_gray_status;
  326. /* Added in libpng 1.5.5 to record setting of coefficients: */
  327. png_byte rgb_to_gray_coefficients_set;
  328. /* These were changed from png_byte in libpng-1.0.6 */
  329. png_uint_16 rgb_to_gray_red_coeff;
  330. png_uint_16 rgb_to_gray_green_coeff;
  331. /* deleted in 1.5.5: rgb_to_gray_blue_coeff; */
  332. #endif
  333. /* New member added in libpng-1.6.36 */
  334. #if defined(PNG_READ_EXPAND_SUPPORTED) && \
  335. defined(PNG_ARM_NEON_IMPLEMENTATION)
  336. png_bytep riffled_palette; /* buffer for accelerated palette expansion */
  337. #endif
  338. /* New member added in libpng-1.0.4 (renamed in 1.0.9) */
  339. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  340. /* Changed from png_byte to png_uint_32 at version 1.2.0 */
  341. png_uint_32 mng_features_permitted;
  342. #endif
  343. /* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */
  344. #ifdef PNG_MNG_FEATURES_SUPPORTED
  345. png_byte filter_type;
  346. #endif
  347. /* New members added in libpng-1.2.0 */
  348. /* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */
  349. #ifdef PNG_USER_MEM_SUPPORTED
  350. png_voidp mem_ptr; /* user supplied struct for mem functions */
  351. png_malloc_ptr malloc_fn; /* function for allocating memory */
  352. png_free_ptr free_fn; /* function for freeing memory */
  353. #endif
  354. /* New member added in libpng-1.0.13 and 1.2.0 */
  355. png_bytep big_row_buf; /* buffer to save current (unfiltered) row */
  356. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  357. /* The following three members were added at version 1.0.14 and 1.2.4 */
  358. png_bytep quantize_sort; /* working sort array */
  359. png_bytep index_to_palette; /* where the original index currently is
  360. in the palette */
  361. png_bytep palette_to_index; /* which original index points to this
  362. palette color */
  363. #endif
  364. /* New members added in libpng-1.0.16 and 1.2.6 */
  365. png_byte compression_type;
  366. #ifdef PNG_USER_LIMITS_SUPPORTED
  367. png_uint_32 user_width_max;
  368. png_uint_32 user_height_max;
  369. /* Added in libpng-1.4.0: Total number of sPLT, text, and unknown
  370. * chunks that can be stored (0 means unlimited).
  371. */
  372. png_uint_32 user_chunk_cache_max;
  373. /* Total memory that a zTXt, sPLT, iTXt, iCCP, or unknown chunk
  374. * can occupy when decompressed. 0 means unlimited.
  375. */
  376. png_alloc_size_t user_chunk_malloc_max;
  377. #endif
  378. /* New member added in libpng-1.0.25 and 1.2.17 */
  379. #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
  380. /* Temporary storage for unknown chunk that the library doesn't recognize,
  381. * used while reading the chunk.
  382. */
  383. png_unknown_chunk unknown_chunk;
  384. #endif
  385. /* New member added in libpng-1.2.26 */
  386. size_t old_big_row_buf_size;
  387. #ifdef PNG_READ_SUPPORTED
  388. /* New member added in libpng-1.2.30 */
  389. png_bytep read_buffer; /* buffer for reading chunk data */
  390. png_alloc_size_t read_buffer_size; /* current size of the buffer */
  391. #endif
  392. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  393. uInt IDAT_read_size; /* limit on read buffer size for IDAT */
  394. #endif
  395. #ifdef PNG_IO_STATE_SUPPORTED
  396. /* New member added in libpng-1.4.0 */
  397. png_uint_32 io_state;
  398. #endif
  399. /* New member added in libpng-1.5.6 */
  400. png_bytep big_prev_row;
  401. /* New member added in libpng-1.5.7 */
  402. void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info,
  403. png_bytep row, png_const_bytep prev_row);
  404. };
  405. #endif /* PNGSTRUCT_H */