2 /* pngstruct.h - header file for PNG reference library
4 * Copyright (c) 1998-2014 Glenn Randers-Pehrson
5 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
6 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
8 * Last changed in libpng 1.7.0 [(PENDING RELEASE)]
10 * This code is released under the libpng license.
11 * For conditions of distribution and use, see the disclaimer
12 * and license in png.h
15 /* The structure that holds the information to read and write PNG files.
16 * The only people who need to care about what is inside of this are the
17 * people who will be modifying the library for their own special needs.
18 * It should NOT be accessed directly by an application.
23 /* zlib.h defines the structure z_stream, an instance of which is included
24 * in this structure and is required for decompressing the LZ compressed
28 /* We must ensure that zlib uses 'const' in declarations. */
32 #include PNG_ZLIB_HEADER
35 /* zlib.h sometimes #defines const to nothing, undo this. */
39 /* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility
42 #if ZLIB_VERNUM < 0x1260
43 # define PNGZ_MSG_CAST(s) png_constcast(char*,s)
44 # define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b)
46 # define PNGZ_MSG_CAST(s) (s)
47 # define PNGZ_INPUT_CAST(b) (b)
50 /* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib
51 * can handle at once. This type need be no larger than 16 bits (so maximum of
52 * 65535), this define allows us to discover how big it is, but limited by the
53 * maximum for size_t. The value can be overridden in a library build (pngusr.h,
54 * or set it in CPPFLAGS) and it works to set it to a considerably lower value
55 * (e.g. 255 works). A lower value may help memory usage (slightly) and may
56 * even improve performance on some systems (and degrade it on others.)
59 # define ZLIB_IO_MAX ((uInt)-1)
62 #ifdef PNG_WRITE_SUPPORTED
63 /* The type of a compression buffer list used by the write code. */
64 typedef struct png_compression_buffer
66 struct png_compression_buffer *next;
67 png_byte output[1]; /* actually zbuf_size */
68 } png_compression_buffer, *png_compression_bufferp;
70 #define PNG_COMPRESSION_BUFFER_SIZE(pp)\
71 (offsetof(png_compression_buffer, output) + (pp)->zbuffer_size)
74 /* Colorspace support; structures used in png_struct, png_info and in internal
75 * functions to hold and communicate information about the color space.
77 * PNG_COLORSPACE_SUPPORTED is only required if the application will perform
78 * colorspace corrections, otherwise all the colorspace information can be
79 * skipped and the size of libpng can be reduced (significantly) by compiling
80 * out the colorspace support.
82 #ifdef PNG_COLORSPACE_SUPPORTED
83 /* The chromaticities of the red, green and blue colorants and the chromaticity
84 * of the corresponding white point (i.e. of rgb(1.0,1.0,1.0)).
88 png_fixed_point redx, redy;
89 png_fixed_point greenx, greeny;
90 png_fixed_point bluex, bluey;
91 png_fixed_point whitex, whitey;
94 /* The same data as above but encoded as CIE XYZ values. When this data comes
95 * from chromaticities the sum of the Y values is assumed to be 1.0
97 typedef struct png_XYZ
99 png_fixed_point red_X, red_Y, red_Z;
100 png_fixed_point green_X, green_Y, green_Z;
101 png_fixed_point blue_X, blue_Y, blue_Z;
103 #endif /* COLORSPACE */
105 #if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED)
106 /* A colorspace is all the above plus, potentially, profile information,
107 * however at present libpng does not use the profile internally so it is only
108 * stored in the png_info struct (if iCCP is supported.) The rendering intent
109 * is retained here and is checked.
111 * The file gamma encoding information is also stored here and gamma correction
112 * is done by libpng, whereas color correction must currently be done by the
115 typedef struct png_colorspace
117 #ifdef PNG_GAMMA_SUPPORTED
118 png_fixed_point gamma; /* File gamma */
121 #ifdef PNG_COLORSPACE_SUPPORTED
122 png_xy end_points_xy; /* End points as chromaticities */
123 png_XYZ end_points_XYZ; /* End points as CIE XYZ colorant values */
124 png_uint_16 rendering_intent; /* Rendering intent of a profile */
127 /* Flags are always defined to simplify the code. */
128 png_uint_16 flags; /* As defined below */
129 } png_colorspace, * PNG_RESTRICT png_colorspacerp;
131 typedef const png_colorspace * PNG_RESTRICT png_const_colorspacerp;
133 /* General flags for the 'flags' field */
134 #define PNG_COLORSPACE_HAVE_GAMMA 0x0001
135 #define PNG_COLORSPACE_HAVE_ENDPOINTS 0x0002
136 #define PNG_COLORSPACE_HAVE_INTENT 0x0004
137 #define PNG_COLORSPACE_FROM_gAMA 0x0008
138 #define PNG_COLORSPACE_FROM_cHRM 0x0010
139 #define PNG_COLORSPACE_FROM_sRGB 0x0020
140 #define PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB 0x0040
141 #define PNG_COLORSPACE_MATCHES_sRGB 0x0080 /* exact match on profile */
142 #define PNG_COLORSPACE_RGB_TO_GRAY_SET 0x0100 /* user specified coeffs */
143 #define PNG_COLORSPACE_INVALID 0x8000
144 #define PNG_COLORSPACE_CANCEL(flags) (0xffff ^ (flags))
145 #endif /* COLORSPACE || GAMMA */
147 struct png_struct_def
149 /* Rearranged in libpng 1.7 to attempt to lessen padding; in general
150 * (char), (short), (int) and pointer types are kept separate, however
151 * associated members under the control of the same #define are still
154 #ifdef PNG_SETJMP_SUPPORTED
155 /* jmp_buf can have very high alignment requirements on some systems, so put
156 * it first (the other setjmp members are later as they are infrequently
159 jmp_buf jmp_buf_local;
162 /* Next the frequently accessed fields. Many processors perform arithmetic
163 * in the address pipeline, but frequently the amount of addition or
164 * subtraction is limited. By putting these fields at the head of png_struct
165 * the hope is that such processors will generate code that is both smaller
168 png_colorp palette; /* palette from the input file */
169 size_t rowbytes; /* size of row in bytes */
170 size_t info_rowbytes; /* cache of updated row bytes */
171 png_uint_32 width; /* width of image in pixels */
172 png_uint_32 height; /* height of image in pixels */
173 png_uint_32 num_rows; /* number of rows in current pass */
174 png_uint_32 usr_width; /* width of row at start of write */
175 png_uint_32 iwidth; /* width of current interlaced row in pixels */
176 png_uint_32 row_number; /* current row in interlace pass */
177 png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */
178 png_uint_32 crc; /* current chunk CRC value */
179 png_uint_32 mode; /* tells us where we are in the PNG file */
180 png_uint_32 flags; /* flags indicating various things to libpng */
181 png_uint_32 transformations;/* which transformations to perform */
182 png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */
183 png_uint_32 free_me; /* items libpng is responsible for freeing */
185 int maximum_pixel_depth; /* pixel depth used for the row buffers */
186 #ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
187 int num_palette_max; /* maximum palette index found in IDAT */
190 png_uint_16 num_palette; /* number of color entries in palette */
191 png_uint_16 num_trans; /* number of transparency values */
193 /* Single byte values, typically used either to save space or to hold 1-byte
194 * values from the PNG chunk specifications.
196 png_byte compression_type; /* file compression type (always 0) */
197 png_byte filter; /* file filter type (always 0) */
198 png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */
199 png_byte pass; /* current interlace pass (0 - 6) */
200 png_byte do_filter; /* row filter flags (see PNG_FILTER_ below ) */
201 png_byte color_type; /* color type of file */
202 png_byte bit_depth; /* bit depth of file */
203 png_byte pixel_depth; /* number of bits per pixel */
204 png_byte channels; /* number of channels in file */
205 png_byte sig_bytes; /* magic bytes read/written from start of file */
206 png_byte transformed_pixel_depth;
207 /* pixel depth after read/write transforms */
210 #ifdef PNG_SETJMP_SUPPORTED
211 jmp_buf *jmp_buf_ptr; /* passed to longjmp_fn */
212 png_longjmp_ptr longjmp_fn; /* setjmp non-local goto function. */
213 size_t jmp_buf_size; /* size of *jmp_buf_ptr, if allocated */
216 /* Error/warning callbacks */
217 png_error_ptr error_fn; /* print an error message and abort */
218 #ifdef PNG_WARNINGS_SUPPORTED
219 png_error_ptr warning_fn; /* print a warning and continue */
221 png_voidp error_ptr; /* user supplied data for the above */
223 /* MEMORY ALLOCATION */
224 #ifdef PNG_USER_MEM_SUPPORTED
225 png_malloc_ptr malloc_fn; /* allocate memory */
226 png_free_ptr free_fn; /* free memory */
227 png_voidp mem_ptr; /* user supplied data for the above */
230 /* IO and BASIC READ/WRITE SUPPORT */
231 png_voidp io_ptr; /* user supplied data for IO callbacks */
233 #ifdef PNG_READ_SUPPORTED
234 png_rw_ptr read_data_fn; /* read some bytes (must succeed) */
235 png_read_status_ptr read_row_fn; /* called after each row is decoded */
236 png_bytep read_buffer; /* buffer for reading chunk data */
238 /* During read the following array is set up to point to the appropriate
239 * un-filter function, this allows per-image and per-processor optimization.
241 void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info,
242 png_bytep row, png_const_bytep prev_row);
244 #if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED)
245 /* The png_struct colorspace structure is only required on read - on write it
246 * is in (just) the info_struct.
248 png_colorspace colorspace;
250 #endif /* PNG_READ_SUPPORTED */
252 #ifdef PNG_SET_USER_LIMITS_SUPPORTED
253 png_uint_32 user_width_max; /* Maximum width on read */
254 png_uint_32 user_height_max; /* Maximum height on read */
255 /* Total memory that a single zTXt, sPLT, iTXt, iCCP, or unknown chunk
256 * can occupy when decompressed. 0 means unlimited. This field is a counter
257 * - it is decremented as memory is allocated.
259 png_alloc_size_t user_chunk_malloc_max;
261 #ifdef PNG_USER_LIMITS_SUPPORTED
262 /* limit on total *number* of sPLT, text and unknown chunks that can be
263 * stored. 0 means unlimited. This field is a counter - it is decremented
264 * as chunks are encountered.
266 png_uint_32 user_chunk_cache_max;
269 /* The progressive reader gets passed data and calls application handling
270 * functions when appropriate.
272 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
273 png_progressive_info_ptr info_fn; /* called after header data fully read */
274 png_progressive_row_ptr row_fn; /* called after a row is decoded */
275 png_progressive_end_ptr end_fn; /* called after image is complete */
277 /* Progressive read control data */
278 png_bytep save_buffer_ptr; /* current location in save_buffer */
279 png_bytep save_buffer; /* buffer for previously read data */
280 png_bytep current_buffer_ptr; /* current location in current_buffer */
281 png_bytep current_buffer; /* buffer for recently used data */
283 size_t save_buffer_size; /* amount of data now in save_buffer */
284 size_t save_buffer_max; /* total size of save_buffer */
285 size_t buffer_size; /* total amount of available input data */
286 size_t current_buffer_size; /* amount of data now in current_buffer */
288 png_uint_32 push_length; /* size of current input chunk */
289 png_uint_32 skip_length; /* bytes to skip in input data */
291 int process_mode; /* what push library is currently doing */
292 int cur_palette; /* current push library palette index */
295 #ifdef PNG_WRITE_SUPPORTED
296 png_rw_ptr write_data_fn;/* write some bytes (must succeed) */
297 png_write_status_ptr write_row_fn; /* called after each row is encoded */
300 #ifdef PNG_WRITE_FLUSH_SUPPORTED
301 png_flush_ptr output_flush_fn; /* Function for flushing output */
302 png_uint_32 flush_dist; /* how many rows apart to flush, 0 - no flush */
303 png_uint_32 flush_rows; /* number of rows written since last flush */
306 #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
307 png_bytep prev_filters; /* filter type(s) of previous row(s) */
308 png_uint_16p filter_weights; /* weight(s) for previous line(s) */
309 png_uint_16p inv_filter_weights; /* 1/weight(s) for previous line(s) */
310 png_uint_16p filter_costs; /* relative filter calculation cost */
311 png_uint_16p inv_filter_costs; /* 1/relative filter calculation cost */
312 png_byte heuristic_method; /* heuristic for row filter selection */
313 png_byte num_prev_filters; /* number of weights for previous rows */
316 #ifdef PNG_WRITE_SUPPORTED
317 png_byte usr_bit_depth; /* bit depth of users row */
318 png_byte usr_channels; /* channels at start of write */
321 #ifdef PNG_IO_STATE_SUPPORTED
322 png_uint_32 io_state; /* tells the app read/write progress */
327 * Members that hold pointers to the decompressed image rows.
329 png_bytep row_buf; /* buffer for the current (unfiltered) row */
330 #if defined(PNG_WRITE_FILTER_SUPPORTED) || defined(PNG_READ_SUPPORTED)
331 png_bytep prev_row; /* buffer to save the previous (unfiltered) row */
334 #ifdef PNG_READ_SUPPORTED
335 /* The row_buf and prev_row pointers are misaligned so that the start of the
336 * row - after the filter byte - is aligned, the 'big_' pointers record the
337 * original allocated pointer.
339 png_bytep big_row_buf;
340 png_bytep big_prev_row;
341 size_t big_row_buf_size; /* Actual size of both */
344 #ifdef PNG_WRITE_SUPPORTED
345 /* This is somewhat excessive, there is no obvious reason on write to
346 * allocate a buffer for each possible filtered row, only for the one being
347 * tested and the current best.
351 png_bytep sub_row; /* buffer to save "sub" row when filtering */
352 png_bytep up_row; /* buffer to save "up" row when filtering */
353 png_bytep avg_row; /* buffer to save "avg" row when filtering */
354 png_bytep paeth_row; /* buffer to save "Paeth" row when filtering */
357 /* UNKNOWN CHUNK HANDLING */
358 /* TODO: this is excessively complicated, there are multiple ways of doing
359 * the same thing. It should be cleaned up, possibly by finding out which
360 * APIs applications really use.
362 #ifdef PNG_USER_CHUNKS_SUPPORTED
363 /* General purpose pointer for all user/unknown chunk handling; points to
364 * application supplied data for use in the read_user_chunk_fn callback
365 * (currently there is no write side support - the write side must use the
366 * set_unknown_chunks interface.)
368 png_voidp user_chunk_ptr;
371 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
372 /* This is called back from the unknown chunk handling */
373 png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */
375 #ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
376 /* Temporary storage for unknown chunk that the library doesn't recognize,
377 * used while reading the chunk.
379 png_unknown_chunk unknown_chunk;
382 #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
383 png_bytep chunk_list; /* List of png_byte[5]; the textual chunk name
384 * followed by a PNG_HANDLE_* byte */
385 int unknown_default; /* As PNG_HANDLE_* */
386 unsigned int num_chunk_list; /* Number of entries in the list */
389 /* USER TRANSFORM SUPPORT */
390 #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
391 png_user_transform_ptr read_user_transform_fn; /* user read transform */
393 #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
394 png_user_transform_ptr write_user_transform_fn; /* user write transform */
396 #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
397 png_voidp user_transform_ptr; /* user supplied data for the above */
398 png_byte user_transform_depth; /* bit depth of user transformed pixels */
399 png_byte user_transform_channels; /* channels in user transformed pixels */
402 /* READ TRANSFORM SUPPORT
404 * Quite a lot of things can be done to the original image data on read, and
405 * most of these are configurable. The data required by the configurable
406 * read transforms should be stored here. The png_color_16 and png_color_8
407 * structures have low alignment requirements and odd sizes, so may cause
408 * misalignment when present. Member alignment is as follows:
410 * png_color_16 png_uint_16
411 * png_color_8 png_byte
413 /* GAMMA/BACKGROUND/ALPHA-MODE/RGB-TO-GRAY/tRNS/sBIT
415 * These things are all interrelated because they need some or all of the
416 * gamma tables. Some attempt has been made below to order these members by
417 * size, so that as little padding as possible is required.
419 #ifdef PNG_READ_GAMMA_SUPPORTED
420 png_bytep gamma_table; /* gamma table for 8-bit depth files */
421 png_uint_16p gamma_16_table; /* gamma table for 16-bit depth files */
423 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
424 defined(PNG_READ_ALPHA_MODE_SUPPORTED) ||\
425 defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
426 png_bytep gamma_from_1; /* converts from 1.0 to screen */
427 png_uint_16p gamma_to_1; /* converts from file to 1.0 */
428 png_uint_16p gamma_16_from_1; /* converts from 1.0 to screen */
429 png_uint_16p gamma_16_to_1; /* converts from file to 1.0 */
430 #endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */
431 #endif /* PNG_READ_GAMMA_SUPPORTED */
433 #if defined(PNG_READ_tRNS_SUPPORTED) || \
434 defined(PNG_READ_BACKGROUND_SUPPORTED) || \
435 defined(PNG_READ_EXPAND_SUPPORTED)
436 png_bytep trans_alpha; /* alpha values for paletted files */
440 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
441 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
442 png_fixed_point background_gamma;
444 #ifdef PNG_READ_GAMMA_SUPPORTED
445 png_fixed_point screen_gamma; /* screen gamma value (display_exponent) */
446 int gamma_shift; /* number of "insignificant" bits in 16-bit gamma */
450 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
451 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
452 png_color_16 background; /* background color in screen gamma space */
453 png_color_16 background_1; /* background normalized to gamma 1.0 */
455 #if defined(PNG_READ_tRNS_SUPPORTED) || \
456 defined(PNG_READ_BACKGROUND_SUPPORTED) || \
457 defined(PNG_READ_EXPAND_SUPPORTED)
458 png_color_16 trans_color; /* transparent color for non-paletted files */
462 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
463 png_uint_16 rgb_to_gray_red_coeff;
464 png_uint_16 rgb_to_gray_green_coeff;
465 /* The blue coefficient is calculated from the above */
469 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_sBIT_SUPPORTED)
470 png_color_8 sig_bit; /* significant bits in each available channel */
474 #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
475 defined(PNG_READ_ALPHA_MODE_SUPPORTED)
476 png_byte background_gamma_type;
478 #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
479 png_byte rgb_to_gray_status;
482 /* SHIFT - both READ_SHIFT and WRITE_SHIFT */
483 #if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED)
484 png_color_8 shift; /* shift for significant bit tranformation */
487 /* FILLER SUPPORT (pixel expansion or read, contraction on write) */
488 #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
489 png_uint_16 filler; /* filler bytes for pixel expansion */
492 /* QUANTIZE (convert to color-mapped) */
493 #ifdef PNG_READ_QUANTIZE_SUPPORTED
494 png_bytep palette_lookup; /* lookup table for quantizing */
495 png_bytep quantize_index; /* index translation for palette files */
496 png_bytep quantize_sort; /* working sort array */
497 png_bytep index_to_palette; /* where the original index currently is in the
500 png_bytep palette_to_index; /* which original index points to this palette
506 #ifdef PNG_MNG_FEATURES_SUPPORTED
507 png_uint_32 mng_features_permitted;
508 png_byte filter_type;
512 #ifdef PNG_SET_OPTION_SUPPORTED
513 png_byte options; /* On/off state (up to 4 options) */
516 /* COMPRESSION AND DECOMPRESSION SUPPORT.
518 * zlib expects a 'zstream' as the fundamental control structure, it allows
519 * all the parameters to be passed as one pointer.
521 z_stream zstream; /* decompression structure */
523 #ifdef PNG_READ_SUPPORTED
524 /* These, and IDAT_read_size below, control how much input and output (at
525 * most) is available to zlib.
527 png_uint_32 idat_size; /* current IDAT size for read */
528 png_alloc_size_t read_buffer_size; /* current size of the buffer */
531 #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
532 int zlib_text_level; /* holds zlib compression level */
533 int zlib_text_method; /* holds zlib compression method */
534 int zlib_text_window_bits; /* holds zlib compression window bits */
535 int zlib_text_mem_level; /* holds zlib compression memory level */
536 int zlib_text_strategy; /* holds zlib compression strategy */
539 #ifdef PNG_WRITE_SUPPORTED
540 int zlib_level; /* holds zlib compression level */
541 int zlib_method; /* holds zlib compression method */
542 int zlib_window_bits; /* holds zlib compression window bits */
543 int zlib_mem_level; /* holds zlib compression memory level */
544 int zlib_strategy; /* holds zlib compression strategy */
546 int zlib_set_level; /* Actual values set into the zstream on write */
548 int zlib_set_window_bits;
549 int zlib_set_mem_level;
550 int zlib_set_strategy;
552 png_compression_bufferp zbuffer_list; /* Created on demand during write */
553 uInt zbuffer_size; /* size of the actual zlib buffer */
556 #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
557 uInt IDAT_read_size; /* limit on read buffer size for IDAT */
560 #endif /* PNGSTRUCT_H */