1 /* SPDX-License-Identifier: (BSD-2-Clause AND libpng-2.0) */
9 #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(SPNG_STATIC)
10 #if defined(SPNG__BUILD)
11 #define SPNG_API __declspec(dllexport)
13 #define SPNG_API __declspec(dllimport)
20 #define SPNG_CDECL __cdecl
29 #define SPNG_VERSION_MAJOR 0
30 #define SPNG_VERSION_MINOR 7
31 #define SPNG_VERSION_PATCH 3
48 SPNG_ECOMPRESSION_METHOD,
50 SPNG_EINTERLACE_METHOD,
57 SPNG_ECHUNK_UNKNOWN_CRITICAL,
73 SPNG_ETRNS_COLOR_TYPE,
77 SPNG_EICCP_COMPRESSION_METHOD,
83 SPNG_EZTXT_COMPRESSION_METHOD,
85 SPNG_EITXT_COMPRESSION_FLAG,
86 SPNG_EITXT_COMPRESSION_METHOD,
88 SPNG_EITXT_TRANSLATED_KEY,
134 SPNG_COLOR_TYPE_GRAYSCALE = 0,
135 SPNG_COLOR_TYPE_TRUECOLOR = 2,
136 SPNG_COLOR_TYPE_INDEXED = 3,
137 SPNG_COLOR_TYPE_GRAYSCALE_ALPHA = 4,
138 SPNG_COLOR_TYPE_TRUECOLOR_ALPHA = 6
143 SPNG_FILTER_NONE = 0,
146 SPNG_FILTER_AVERAGE = 3,
147 SPNG_FILTER_PAETH = 4
150 enum spng_filter_choice
152 SPNG_DISABLE_FILTERING = 0,
153 SPNG_FILTER_CHOICE_NONE = 8,
154 SPNG_FILTER_CHOICE_SUB = 16,
155 SPNG_FILTER_CHOICE_UP = 32,
156 SPNG_FILTER_CHOICE_AVG = 64,
157 SPNG_FILTER_CHOICE_PAETH = 128,
158 SPNG_FILTER_CHOICE_ALL = (8|16|32|64|128)
161 enum spng_interlace_method
163 SPNG_INTERLACE_NONE = 0,
164 SPNG_INTERLACE_ADAM7 = 1
167 /* Channels are always in byte-order */
174 /* Partially implemented, see documentation */
179 /* No conversion or scaling */
181 SPNG_FMT_RAW = 512 /* big-endian (everything else is host-endian) */
186 SPNG_CTX_IGNORE_ADLER32 = 1, /* Ignore checksum in DEFLATE streams */
187 SPNG_CTX_ENCODER = 2 /* Create an encoder context */
190 enum spng_decode_flags
192 SPNG_DECODE_USE_TRNS = 1, /* Deprecated */
193 SPNG_DECODE_USE_GAMA = 2, /* Deprecated */
194 SPNG_DECODE_USE_SBIT = 8, /* Undocumented */
196 SPNG_DECODE_TRNS = 1, /* Apply transparency */
197 SPNG_DECODE_GAMMA = 2, /* Apply gamma correction */
198 SPNG_DECODE_PROGRESSIVE = 256 /* Initialize for progressive reads */
203 /* Default for critical chunks */
206 /* Discard chunk, invalid for critical chunks.
207 Since v0.6.2: default for ancillary chunks */
208 SPNG_CRC_DISCARD = 1,
210 /* Ignore and don't calculate checksum.
211 Since v0.6.2: also ignores checksums in DEFLATE streams */
215 enum spng_encode_flags
217 SPNG_ENCODE_PROGRESSIVE = 1, /* Initialize for progressive writes */
218 SPNG_ENCODE_FINALIZE = 2, /* Finalize PNG after encoding image */
227 uint8_t compression_method;
228 uint8_t filter_method;
229 uint8_t interlace_method;
232 struct spng_plte_entry
238 uint8_t alpha; /* Reserved for internal use */
244 struct spng_plte_entry entries[256];
255 uint32_t n_type3_entries;
256 uint8_t type3_alpha[256];
261 uint32_t white_point_x;
262 uint32_t white_point_y;
273 double white_point_x;
274 double white_point_y;
285 char profile_name[80];
292 uint8_t grayscale_bits;
307 uint8_t compression_flag; /* iTXt only */
308 uint8_t compression_method; /* iTXt, ztXt only */
309 char *language_tag; /* iTXt only */
310 char *translated_keyword; /* iTXt only */
315 uint16_t gray; /* Only for gray/gray alpha */
319 uint16_t plte_index; /* Only for indexed color */
324 uint16_t frequency[256];
329 uint32_t ppu_x, ppu_y;
330 uint8_t unit_specifier;
333 struct spng_splt_entry
345 uint8_t sample_depth;
347 struct spng_splt_entry *entries;
363 uint8_t unit_specifier;
387 struct spng_unknown_chunk
392 enum spng_location location;
397 SPNG_KEEP_UNKNOWN_CHUNKS = 1,
399 SPNG_IMG_COMPRESSION_LEVEL,
400 SPNG_IMG_WINDOW_BITS,
402 SPNG_IMG_COMPRESSION_STRATEGY,
404 SPNG_TEXT_COMPRESSION_LEVEL,
405 SPNG_TEXT_WINDOW_BITS,
407 SPNG_TEXT_COMPRESSION_STRATEGY,
410 SPNG_CHUNK_COUNT_LIMIT,
411 SPNG_ENCODE_TO_BUFFER,
414 typedef void* SPNG_CDECL spng_malloc_fn(size_t size);
415 typedef void* SPNG_CDECL spng_realloc_fn(void* ptr, size_t size);
416 typedef void* SPNG_CDECL spng_calloc_fn(size_t count, size_t size);
417 typedef void SPNG_CDECL spng_free_fn(void* ptr);
421 spng_malloc_fn *malloc_fn;
422 spng_realloc_fn *realloc_fn;
423 spng_calloc_fn *calloc_fn;
424 spng_free_fn *free_fn;
429 uint32_t scanline_idx;
430 uint32_t row_num; /* deinterlaced row index */
435 typedef struct spng_ctx spng_ctx;
437 typedef int spng_read_fn(spng_ctx *ctx, void *user, void *dest, size_t length);
438 typedef int spng_write_fn(spng_ctx *ctx, void *user, void *src, size_t length);
440 typedef int spng_rw_fn(spng_ctx *ctx, void *user, void *dst_src, size_t length);
442 SPNG_API spng_ctx *spng_ctx_new(int flags);
443 SPNG_API spng_ctx *spng_ctx_new2(struct spng_alloc *alloc, int flags);
444 SPNG_API void spng_ctx_free(spng_ctx *ctx);
446 SPNG_API int spng_set_png_buffer(spng_ctx *ctx, const void *buf, size_t size);
447 SPNG_API int spng_set_png_stream(spng_ctx *ctx, spng_rw_fn *rw_func, void *user);
448 SPNG_API int spng_set_png_file(spng_ctx *ctx, FILE *file);
450 SPNG_API void *spng_get_png_buffer(spng_ctx *ctx, size_t *len, int *error);
452 SPNG_API int spng_set_image_limits(spng_ctx *ctx, uint32_t width, uint32_t height);
453 SPNG_API int spng_get_image_limits(spng_ctx *ctx, uint32_t *width, uint32_t *height);
455 SPNG_API int spng_set_chunk_limits(spng_ctx *ctx, size_t chunk_size, size_t cache_size);
456 SPNG_API int spng_get_chunk_limits(spng_ctx *ctx, size_t *chunk_size, size_t *cache_size);
458 SPNG_API int spng_set_crc_action(spng_ctx *ctx, int critical, int ancillary);
460 SPNG_API int spng_set_option(spng_ctx *ctx, enum spng_option option, int value);
461 SPNG_API int spng_get_option(spng_ctx *ctx, enum spng_option option, int *value);
463 SPNG_API int spng_decoded_image_size(spng_ctx *ctx, int fmt, size_t *len);
466 SPNG_API int spng_decode_image(spng_ctx *ctx, void *out, size_t len, int fmt, int flags);
468 /* Progressive decode */
469 SPNG_API int spng_decode_scanline(spng_ctx *ctx, void *out, size_t len);
470 SPNG_API int spng_decode_row(spng_ctx *ctx, void *out, size_t len);
471 SPNG_API int spng_decode_chunks(spng_ctx *ctx);
474 SPNG_API int spng_get_row_info(spng_ctx *ctx, struct spng_row_info *row_info);
477 SPNG_API int spng_encode_image(spng_ctx *ctx, const void *img, size_t len, int fmt, int flags);
479 /* Progressive encode */
480 SPNG_API int spng_encode_scanline(spng_ctx *ctx, const void *scanline, size_t len);
481 SPNG_API int spng_encode_row(spng_ctx *ctx, const void *row, size_t len);
482 SPNG_API int spng_encode_chunks(spng_ctx *ctx);
484 SPNG_API int spng_get_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
485 SPNG_API int spng_get_plte(spng_ctx *ctx, struct spng_plte *plte);
486 SPNG_API int spng_get_trns(spng_ctx *ctx, struct spng_trns *trns);
487 SPNG_API int spng_get_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
488 SPNG_API int spng_get_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
489 SPNG_API int spng_get_gama(spng_ctx *ctx, double *gamma);
490 SPNG_API int spng_get_gama_int(spng_ctx *ctx, uint32_t *gama_int);
491 SPNG_API int spng_get_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
492 SPNG_API int spng_get_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
493 SPNG_API int spng_get_srgb(spng_ctx *ctx, uint8_t *rendering_intent);
494 SPNG_API int spng_get_text(spng_ctx *ctx, struct spng_text *text, uint32_t *n_text);
495 SPNG_API int spng_get_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
496 SPNG_API int spng_get_hist(spng_ctx *ctx, struct spng_hist *hist);
497 SPNG_API int spng_get_phys(spng_ctx *ctx, struct spng_phys *phys);
498 SPNG_API int spng_get_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t *n_splt);
499 SPNG_API int spng_get_time(spng_ctx *ctx, struct spng_time *time);
500 SPNG_API int spng_get_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t *n_chunks);
502 /* Official extensions */
503 SPNG_API int spng_get_offs(spng_ctx *ctx, struct spng_offs *offs);
504 SPNG_API int spng_get_exif(spng_ctx *ctx, struct spng_exif *exif);
507 SPNG_API int spng_set_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
508 SPNG_API int spng_set_plte(spng_ctx *ctx, struct spng_plte *plte);
509 SPNG_API int spng_set_trns(spng_ctx *ctx, struct spng_trns *trns);
510 SPNG_API int spng_set_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
511 SPNG_API int spng_set_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
512 SPNG_API int spng_set_gama(spng_ctx *ctx, double gamma);
513 SPNG_API int spng_set_gama_int(spng_ctx *ctx, uint32_t gamma);
514 SPNG_API int spng_set_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
515 SPNG_API int spng_set_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
516 SPNG_API int spng_set_srgb(spng_ctx *ctx, uint8_t rendering_intent);
517 SPNG_API int spng_set_text(spng_ctx *ctx, struct spng_text *text, uint32_t n_text);
518 SPNG_API int spng_set_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
519 SPNG_API int spng_set_hist(spng_ctx *ctx, struct spng_hist *hist);
520 SPNG_API int spng_set_phys(spng_ctx *ctx, struct spng_phys *phys);
521 SPNG_API int spng_set_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t n_splt);
522 SPNG_API int spng_set_time(spng_ctx *ctx, struct spng_time *time);
523 SPNG_API int spng_set_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t n_chunks);
525 /* Official extensions */
526 SPNG_API int spng_set_offs(spng_ctx *ctx, struct spng_offs *offs);
527 SPNG_API int spng_set_exif(spng_ctx *ctx, struct spng_exif *exif);
530 SPNG_API const char *spng_strerror(int err);
531 SPNG_API const char *spng_version_string(void);