spng.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /* SPDX-License-Identifier: BSD-2-Clause */
  2. #ifndef SPNG_H
  3. #define SPNG_H
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. #if (defined(_WIN32) || defined(__CYGWIN__)) && !defined(SPNG_STATIC)
  8. #if defined(SPNG__BUILD)
  9. #define SPNG_API __declspec(dllexport)
  10. #else
  11. #define SPNG_API __declspec(dllimport)
  12. #endif
  13. #else
  14. #define SPNG_API
  15. #endif
  16. #if defined(_MSC_VER)
  17. #define SPNG_CDECL __cdecl
  18. #else
  19. #define SPNG_CDECL
  20. #endif
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #define SPNG_VERSION_MAJOR 0
  25. #define SPNG_VERSION_MINOR 7
  26. #define SPNG_VERSION_PATCH 4
  27. enum spng_errno
  28. {
  29. SPNG_IO_ERROR = -2,
  30. SPNG_IO_EOF = -1,
  31. SPNG_OK = 0,
  32. SPNG_EINVAL,
  33. SPNG_EMEM,
  34. SPNG_EOVERFLOW,
  35. SPNG_ESIGNATURE,
  36. SPNG_EWIDTH,
  37. SPNG_EHEIGHT,
  38. SPNG_EUSER_WIDTH,
  39. SPNG_EUSER_HEIGHT,
  40. SPNG_EBIT_DEPTH,
  41. SPNG_ECOLOR_TYPE,
  42. SPNG_ECOMPRESSION_METHOD,
  43. SPNG_EFILTER_METHOD,
  44. SPNG_EINTERLACE_METHOD,
  45. SPNG_EIHDR_SIZE,
  46. SPNG_ENOIHDR,
  47. SPNG_ECHUNK_POS,
  48. SPNG_ECHUNK_SIZE,
  49. SPNG_ECHUNK_CRC,
  50. SPNG_ECHUNK_TYPE,
  51. SPNG_ECHUNK_UNKNOWN_CRITICAL,
  52. SPNG_EDUP_PLTE,
  53. SPNG_EDUP_CHRM,
  54. SPNG_EDUP_GAMA,
  55. SPNG_EDUP_ICCP,
  56. SPNG_EDUP_SBIT,
  57. SPNG_EDUP_SRGB,
  58. SPNG_EDUP_BKGD,
  59. SPNG_EDUP_HIST,
  60. SPNG_EDUP_TRNS,
  61. SPNG_EDUP_PHYS,
  62. SPNG_EDUP_TIME,
  63. SPNG_EDUP_OFFS,
  64. SPNG_EDUP_EXIF,
  65. SPNG_ECHRM,
  66. SPNG_EPLTE_IDX,
  67. SPNG_ETRNS_COLOR_TYPE,
  68. SPNG_ETRNS_NO_PLTE,
  69. SPNG_EGAMA,
  70. SPNG_EICCP_NAME,
  71. SPNG_EICCP_COMPRESSION_METHOD,
  72. SPNG_ESBIT,
  73. SPNG_ESRGB,
  74. SPNG_ETEXT,
  75. SPNG_ETEXT_KEYWORD,
  76. SPNG_EZTXT,
  77. SPNG_EZTXT_COMPRESSION_METHOD,
  78. SPNG_EITXT,
  79. SPNG_EITXT_COMPRESSION_FLAG,
  80. SPNG_EITXT_COMPRESSION_METHOD,
  81. SPNG_EITXT_LANG_TAG,
  82. SPNG_EITXT_TRANSLATED_KEY,
  83. SPNG_EBKGD_NO_PLTE,
  84. SPNG_EBKGD_PLTE_IDX,
  85. SPNG_EHIST_NO_PLTE,
  86. SPNG_EPHYS,
  87. SPNG_ESPLT_NAME,
  88. SPNG_ESPLT_DUP_NAME,
  89. SPNG_ESPLT_DEPTH,
  90. SPNG_ETIME,
  91. SPNG_EOFFS,
  92. SPNG_EEXIF,
  93. SPNG_EIDAT_TOO_SHORT,
  94. SPNG_EIDAT_STREAM,
  95. SPNG_EZLIB,
  96. SPNG_EFILTER,
  97. SPNG_EBUFSIZ,
  98. SPNG_EIO,
  99. SPNG_EOF,
  100. SPNG_EBUF_SET,
  101. SPNG_EBADSTATE,
  102. SPNG_EFMT,
  103. SPNG_EFLAGS,
  104. SPNG_ECHUNKAVAIL,
  105. SPNG_ENCODE_ONLY,
  106. SPNG_EOI,
  107. SPNG_ENOPLTE,
  108. SPNG_ECHUNK_LIMITS,
  109. SPNG_EZLIB_INIT,
  110. SPNG_ECHUNK_STDLEN,
  111. SPNG_EINTERNAL,
  112. SPNG_ECTXTYPE,
  113. SPNG_ENOSRC,
  114. SPNG_ENODST,
  115. SPNG_EOPSTATE,
  116. SPNG_ENOTFINAL,
  117. };
  118. enum spng_text_type
  119. {
  120. SPNG_TEXT = 1,
  121. SPNG_ZTXT = 2,
  122. SPNG_ITXT = 3
  123. };
  124. enum spng_color_type
  125. {
  126. SPNG_COLOR_TYPE_GRAYSCALE = 0,
  127. SPNG_COLOR_TYPE_TRUECOLOR = 2,
  128. SPNG_COLOR_TYPE_INDEXED = 3,
  129. SPNG_COLOR_TYPE_GRAYSCALE_ALPHA = 4,
  130. SPNG_COLOR_TYPE_TRUECOLOR_ALPHA = 6
  131. };
  132. enum spng_filter
  133. {
  134. SPNG_FILTER_NONE = 0,
  135. SPNG_FILTER_SUB = 1,
  136. SPNG_FILTER_UP = 2,
  137. SPNG_FILTER_AVERAGE = 3,
  138. SPNG_FILTER_PAETH = 4
  139. };
  140. enum spng_filter_choice
  141. {
  142. SPNG_DISABLE_FILTERING = 0,
  143. SPNG_FILTER_CHOICE_NONE = 8,
  144. SPNG_FILTER_CHOICE_SUB = 16,
  145. SPNG_FILTER_CHOICE_UP = 32,
  146. SPNG_FILTER_CHOICE_AVG = 64,
  147. SPNG_FILTER_CHOICE_PAETH = 128,
  148. SPNG_FILTER_CHOICE_ALL = (8|16|32|64|128)
  149. };
  150. enum spng_interlace_method
  151. {
  152. SPNG_INTERLACE_NONE = 0,
  153. SPNG_INTERLACE_ADAM7 = 1
  154. };
  155. /* Channels are always in byte-order */
  156. enum spng_format
  157. {
  158. SPNG_FMT_RGBA8 = 1,
  159. SPNG_FMT_RGBA16 = 2,
  160. SPNG_FMT_RGB8 = 4,
  161. /* Partially implemented, see documentation */
  162. SPNG_FMT_GA8 = 16,
  163. SPNG_FMT_GA16 = 32,
  164. SPNG_FMT_G8 = 64,
  165. /* No conversion or scaling */
  166. SPNG_FMT_PNG = 256,
  167. SPNG_FMT_RAW = 512 /* big-endian (everything else is host-endian) */
  168. };
  169. enum spng_ctx_flags
  170. {
  171. SPNG_CTX_IGNORE_ADLER32 = 1, /* Ignore checksum in DEFLATE streams */
  172. SPNG_CTX_ENCODER = 2 /* Create an encoder context */
  173. };
  174. enum spng_decode_flags
  175. {
  176. SPNG_DECODE_USE_TRNS = 1, /* Deprecated */
  177. SPNG_DECODE_USE_GAMA = 2, /* Deprecated */
  178. SPNG_DECODE_USE_SBIT = 8, /* Undocumented */
  179. SPNG_DECODE_TRNS = 1, /* Apply transparency */
  180. SPNG_DECODE_GAMMA = 2, /* Apply gamma correction */
  181. SPNG_DECODE_PROGRESSIVE = 256 /* Initialize for progressive reads */
  182. };
  183. enum spng_crc_action
  184. {
  185. /* Default for critical chunks */
  186. SPNG_CRC_ERROR = 0,
  187. /* Discard chunk, invalid for critical chunks.
  188. Since v0.6.2: default for ancillary chunks */
  189. SPNG_CRC_DISCARD = 1,
  190. /* Ignore and don't calculate checksum.
  191. Since v0.6.2: also ignores checksums in DEFLATE streams */
  192. SPNG_CRC_USE = 2
  193. };
  194. enum spng_encode_flags
  195. {
  196. SPNG_ENCODE_PROGRESSIVE = 1, /* Initialize for progressive writes */
  197. SPNG_ENCODE_FINALIZE = 2, /* Finalize PNG after encoding image */
  198. };
  199. struct spng_ihdr
  200. {
  201. uint32_t width;
  202. uint32_t height;
  203. uint8_t bit_depth;
  204. uint8_t color_type;
  205. uint8_t compression_method;
  206. uint8_t filter_method;
  207. uint8_t interlace_method;
  208. };
  209. struct spng_plte_entry
  210. {
  211. uint8_t red;
  212. uint8_t green;
  213. uint8_t blue;
  214. uint8_t alpha; /* Reserved for internal use */
  215. };
  216. struct spng_plte
  217. {
  218. uint32_t n_entries;
  219. struct spng_plte_entry entries[256];
  220. };
  221. struct spng_trns
  222. {
  223. uint16_t gray;
  224. uint16_t red;
  225. uint16_t green;
  226. uint16_t blue;
  227. uint32_t n_type3_entries;
  228. uint8_t type3_alpha[256];
  229. };
  230. struct spng_chrm_int
  231. {
  232. uint32_t white_point_x;
  233. uint32_t white_point_y;
  234. uint32_t red_x;
  235. uint32_t red_y;
  236. uint32_t green_x;
  237. uint32_t green_y;
  238. uint32_t blue_x;
  239. uint32_t blue_y;
  240. };
  241. struct spng_chrm
  242. {
  243. double white_point_x;
  244. double white_point_y;
  245. double red_x;
  246. double red_y;
  247. double green_x;
  248. double green_y;
  249. double blue_x;
  250. double blue_y;
  251. };
  252. struct spng_iccp
  253. {
  254. char profile_name[80];
  255. size_t profile_len;
  256. char *profile;
  257. };
  258. struct spng_sbit
  259. {
  260. uint8_t grayscale_bits;
  261. uint8_t red_bits;
  262. uint8_t green_bits;
  263. uint8_t blue_bits;
  264. uint8_t alpha_bits;
  265. };
  266. struct spng_text
  267. {
  268. char keyword[80];
  269. int type;
  270. size_t length;
  271. char *text;
  272. uint8_t compression_flag; /* iTXt only */
  273. uint8_t compression_method; /* iTXt, ztXt only */
  274. char *language_tag; /* iTXt only */
  275. char *translated_keyword; /* iTXt only */
  276. };
  277. struct spng_bkgd
  278. {
  279. uint16_t gray; /* Only for gray/gray alpha */
  280. uint16_t red;
  281. uint16_t green;
  282. uint16_t blue;
  283. uint16_t plte_index; /* Only for indexed color */
  284. };
  285. struct spng_hist
  286. {
  287. uint16_t frequency[256];
  288. };
  289. struct spng_phys
  290. {
  291. uint32_t ppu_x, ppu_y;
  292. uint8_t unit_specifier;
  293. };
  294. struct spng_splt_entry
  295. {
  296. uint16_t red;
  297. uint16_t green;
  298. uint16_t blue;
  299. uint16_t alpha;
  300. uint16_t frequency;
  301. };
  302. struct spng_splt
  303. {
  304. char name[80];
  305. uint8_t sample_depth;
  306. uint32_t n_entries;
  307. struct spng_splt_entry *entries;
  308. };
  309. struct spng_time
  310. {
  311. uint16_t year;
  312. uint8_t month;
  313. uint8_t day;
  314. uint8_t hour;
  315. uint8_t minute;
  316. uint8_t second;
  317. };
  318. struct spng_offs
  319. {
  320. int32_t x, y;
  321. uint8_t unit_specifier;
  322. };
  323. struct spng_exif
  324. {
  325. size_t length;
  326. char *data;
  327. };
  328. struct spng_chunk
  329. {
  330. size_t offset;
  331. uint32_t length;
  332. uint8_t type[4];
  333. uint32_t crc;
  334. };
  335. enum spng_location
  336. {
  337. SPNG_AFTER_IHDR = 1,
  338. SPNG_AFTER_PLTE = 2,
  339. SPNG_AFTER_IDAT = 8,
  340. };
  341. struct spng_unknown_chunk
  342. {
  343. uint8_t type[4];
  344. size_t length;
  345. void *data;
  346. enum spng_location location;
  347. };
  348. enum spng_option
  349. {
  350. SPNG_KEEP_UNKNOWN_CHUNKS = 1,
  351. SPNG_IMG_COMPRESSION_LEVEL,
  352. SPNG_IMG_WINDOW_BITS,
  353. SPNG_IMG_MEM_LEVEL,
  354. SPNG_IMG_COMPRESSION_STRATEGY,
  355. SPNG_TEXT_COMPRESSION_LEVEL,
  356. SPNG_TEXT_WINDOW_BITS,
  357. SPNG_TEXT_MEM_LEVEL,
  358. SPNG_TEXT_COMPRESSION_STRATEGY,
  359. SPNG_FILTER_CHOICE,
  360. SPNG_CHUNK_COUNT_LIMIT,
  361. SPNG_ENCODE_TO_BUFFER,
  362. };
  363. typedef void* SPNG_CDECL spng_malloc_fn(size_t size);
  364. typedef void* SPNG_CDECL spng_realloc_fn(void* ptr, size_t size);
  365. typedef void* SPNG_CDECL spng_calloc_fn(size_t count, size_t size);
  366. typedef void SPNG_CDECL spng_free_fn(void* ptr);
  367. struct spng_alloc
  368. {
  369. spng_malloc_fn *malloc_fn;
  370. spng_realloc_fn *realloc_fn;
  371. spng_calloc_fn *calloc_fn;
  372. spng_free_fn *free_fn;
  373. };
  374. struct spng_row_info
  375. {
  376. uint32_t scanline_idx;
  377. uint32_t row_num; /* deinterlaced row index */
  378. int pass;
  379. uint8_t filter;
  380. };
  381. typedef struct spng_ctx spng_ctx;
  382. typedef int spng_read_fn(spng_ctx *ctx, void *user, void *dest, size_t length);
  383. typedef int spng_write_fn(spng_ctx *ctx, void *user, void *src, size_t length);
  384. typedef int spng_rw_fn(spng_ctx *ctx, void *user, void *dst_src, size_t length);
  385. SPNG_API spng_ctx *spng_ctx_new(int flags);
  386. SPNG_API spng_ctx *spng_ctx_new2(struct spng_alloc *alloc, int flags);
  387. SPNG_API void spng_ctx_free(spng_ctx *ctx);
  388. SPNG_API int spng_set_png_buffer(spng_ctx *ctx, const void *buf, size_t size);
  389. SPNG_API int spng_set_png_stream(spng_ctx *ctx, spng_rw_fn *rw_func, void *user);
  390. SPNG_API int spng_set_png_file(spng_ctx *ctx, FILE *file);
  391. SPNG_API void *spng_get_png_buffer(spng_ctx *ctx, size_t *len, int *error);
  392. SPNG_API int spng_set_image_limits(spng_ctx *ctx, uint32_t width, uint32_t height);
  393. SPNG_API int spng_get_image_limits(spng_ctx *ctx, uint32_t *width, uint32_t *height);
  394. SPNG_API int spng_set_chunk_limits(spng_ctx *ctx, size_t chunk_size, size_t cache_size);
  395. SPNG_API int spng_get_chunk_limits(spng_ctx *ctx, size_t *chunk_size, size_t *cache_size);
  396. SPNG_API int spng_set_crc_action(spng_ctx *ctx, int critical, int ancillary);
  397. SPNG_API int spng_set_option(spng_ctx *ctx, enum spng_option option, int value);
  398. SPNG_API int spng_get_option(spng_ctx *ctx, enum spng_option option, int *value);
  399. SPNG_API int spng_decoded_image_size(spng_ctx *ctx, int fmt, size_t *len);
  400. /* Decode */
  401. SPNG_API int spng_decode_image(spng_ctx *ctx, void *out, size_t len, int fmt, int flags);
  402. /* Progressive decode */
  403. SPNG_API int spng_decode_scanline(spng_ctx *ctx, void *out, size_t len);
  404. SPNG_API int spng_decode_row(spng_ctx *ctx, void *out, size_t len);
  405. SPNG_API int spng_decode_chunks(spng_ctx *ctx);
  406. /* Encode/decode */
  407. SPNG_API int spng_get_row_info(spng_ctx *ctx, struct spng_row_info *row_info);
  408. /* Encode */
  409. SPNG_API int spng_encode_image(spng_ctx *ctx, const void *img, size_t len, int fmt, int flags);
  410. /* Progressive encode */
  411. SPNG_API int spng_encode_scanline(spng_ctx *ctx, const void *scanline, size_t len);
  412. SPNG_API int spng_encode_row(spng_ctx *ctx, const void *row, size_t len);
  413. SPNG_API int spng_encode_chunks(spng_ctx *ctx);
  414. SPNG_API int spng_get_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
  415. SPNG_API int spng_get_plte(spng_ctx *ctx, struct spng_plte *plte);
  416. SPNG_API int spng_get_trns(spng_ctx *ctx, struct spng_trns *trns);
  417. SPNG_API int spng_get_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
  418. SPNG_API int spng_get_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
  419. SPNG_API int spng_get_gama(spng_ctx *ctx, double *gamma);
  420. SPNG_API int spng_get_gama_int(spng_ctx *ctx, uint32_t *gama_int);
  421. SPNG_API int spng_get_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
  422. SPNG_API int spng_get_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
  423. SPNG_API int spng_get_srgb(spng_ctx *ctx, uint8_t *rendering_intent);
  424. SPNG_API int spng_get_text(spng_ctx *ctx, struct spng_text *text, uint32_t *n_text);
  425. SPNG_API int spng_get_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
  426. SPNG_API int spng_get_hist(spng_ctx *ctx, struct spng_hist *hist);
  427. SPNG_API int spng_get_phys(spng_ctx *ctx, struct spng_phys *phys);
  428. SPNG_API int spng_get_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t *n_splt);
  429. SPNG_API int spng_get_time(spng_ctx *ctx, struct spng_time *time);
  430. SPNG_API int spng_get_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t *n_chunks);
  431. /* Official extensions */
  432. SPNG_API int spng_get_offs(spng_ctx *ctx, struct spng_offs *offs);
  433. SPNG_API int spng_get_exif(spng_ctx *ctx, struct spng_exif *exif);
  434. SPNG_API int spng_set_ihdr(spng_ctx *ctx, struct spng_ihdr *ihdr);
  435. SPNG_API int spng_set_plte(spng_ctx *ctx, struct spng_plte *plte);
  436. SPNG_API int spng_set_trns(spng_ctx *ctx, struct spng_trns *trns);
  437. SPNG_API int spng_set_chrm(spng_ctx *ctx, struct spng_chrm *chrm);
  438. SPNG_API int spng_set_chrm_int(spng_ctx *ctx, struct spng_chrm_int *chrm_int);
  439. SPNG_API int spng_set_gama(spng_ctx *ctx, double gamma);
  440. SPNG_API int spng_set_gama_int(spng_ctx *ctx, uint32_t gamma);
  441. SPNG_API int spng_set_iccp(spng_ctx *ctx, struct spng_iccp *iccp);
  442. SPNG_API int spng_set_sbit(spng_ctx *ctx, struct spng_sbit *sbit);
  443. SPNG_API int spng_set_srgb(spng_ctx *ctx, uint8_t rendering_intent);
  444. SPNG_API int spng_set_text(spng_ctx *ctx, struct spng_text *text, uint32_t n_text);
  445. SPNG_API int spng_set_bkgd(spng_ctx *ctx, struct spng_bkgd *bkgd);
  446. SPNG_API int spng_set_hist(spng_ctx *ctx, struct spng_hist *hist);
  447. SPNG_API int spng_set_phys(spng_ctx *ctx, struct spng_phys *phys);
  448. SPNG_API int spng_set_splt(spng_ctx *ctx, struct spng_splt *splt, uint32_t n_splt);
  449. SPNG_API int spng_set_time(spng_ctx *ctx, struct spng_time *time);
  450. SPNG_API int spng_set_unknown_chunks(spng_ctx *ctx, struct spng_unknown_chunk *chunks, uint32_t n_chunks);
  451. /* Official extensions */
  452. SPNG_API int spng_set_offs(spng_ctx *ctx, struct spng_offs *offs);
  453. SPNG_API int spng_set_exif(spng_ctx *ctx, struct spng_exif *exif);
  454. SPNG_API const char *spng_strerror(int err);
  455. SPNG_API const char *spng_version_string(void);
  456. #ifdef __cplusplus
  457. }
  458. #endif
  459. #endif /* SPNG_H */