1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
# Data types
# spng_ctx
```c
typedef struct spng_ctx spng_ctx;
```
Context handle.
!!!note
The context handle has no public members.
# spng_ctx_flags
```c
enum spng_ctx_flags
{
SPNG_CTX_IGNORE_ADLER32 = 1, /* Ignore checksum in DEFLATE streams */
SPNG_CTX_ENCODER = 2 /* Create an encoder context */
};
```
# spng_read_fn
```c
typedef int spng_read_fn(spng_ctx *ctx, void *user, void *dest, size_t length)
```
Type definition for callback passed to `spng_set_png_stream()` for decoders.
A read callback function should copy `length` bytes to `dest` and return 0 or
`SPNG_IO_EOF`/`SPNG_IO_ERROR` on error.
# spng_write_fn
```c
typedef int spng_write_fn(spng_ctx *ctx, void *user, void *src, size_t length)
```
Type definition for callback passed to `spng_set_png_stream()` for encoders.
The write callback should process `length` bytes and return 0 or `SPNG_IO_ERROR` on error.
# spng_format
```c
enum spng_format
{
SPNG_FMT_RGBA8 = 1,
SPNG_FMT_RGBA16 = 2,
SPNG_FMT_RGB8 = 4,
SPNG_FMT_GA8 = 16,
SPNG_FMT_GA16 = 32,
SPNG_FMT_G8 = 64,
/* No conversion or scaling */
SPNG_FMT_PNG = 256,
SPNG_FMT_RAW = 512 /* big-endian (everything else is host-endian) */
};
```
!!! note
The channels are always in [byte-order](https://en.wikipedia.org/wiki/RGBA_color_model#RGBA8888) representation.
The alpha channel is always [straight alpha](https://en.wikipedia.org/wiki/Alpha_compositing#Straight_versus_premultiplied),
premultiplied alpha is not supported.
# spng_filter
```c
enum spng_filter
{
SPNG_FILTER_NONE = 0,
SPNG_FILTER_SUB = 1,
SPNG_FILTER_UP = 2,
SPNG_FILTER_AVERAGE = 3,
SPNG_FILTER_PAETH = 4
};
```
# spng_row_info
```c
struct spng_row_info
{
uint32_t scanline_idx;
uint32_t row_num;
int pass;
uint8_t filter;
};
```
Contains row and scanline information, used for progressive decoding and encoding.
# spng_option
```c
enum spng_option
{
SPNG_KEEP_UNKNOWN_CHUNKS = 1,
SPNG_IMG_COMPRESSION_LEVEL,
SPNG_IMG_WINDOW_BITS,
SPNG_IMG_MEM_LEVEL,
SPNG_IMG_COMPRESSION_STRATEGY,
SPNG_TEXT_COMPRESSION_LEVEL,
SPNG_TEXT_WINDOW_BITS,
SPNG_TEXT_MEM_LEVEL,
SPNG_TEXT_COMPRESSION_STRATEGY,
SPNG_FILTER_CHOICE,
SPNG_CHUNK_COUNT_LIMIT,
SPNG_ENCODE_TO_BUFFER,
};
```
# spng_filter_choice
```c
enum spng_filter_choice
{
SPNG_DISABLE_FILTERING = 0,
SPNG_FILTER_CHOICE_NONE = 8,
SPNG_FILTER_CHOICE_SUB = 16,
SPNG_FILTER_CHOICE_UP = 32,
SPNG_FILTER_CHOICE_AVG = 64,
SPNG_FILTER_CHOICE_PAETH = 128,
SPNG_FILTER_CHOICE_ALL = (8|16|32|64|128)
};
```
# API
# spng_ctx_new()
```c
spng_ctx *spng_ctx_new(int flags)
```
Creates a new context.
# spng_ctx_new2()
```c
spng_ctx *spng_ctx_new2(struct spng_alloc *alloc, int flags)
```
Creates a new context with a custom memory allocator, it is passed to zlib.
`alloc` and its members must be non-NULL.
# spng_ctx_free()
```c
void spng_ctx_free(spng_ctx *ctx)
```
Releases context resources.
# spng_set_png_stream()
```c
int spng_set_png_stream(spng_ctx *ctx, spng_rw_fn *rw_func, void *user)
```
Set input PNG stream or output PNG stream, depending on context type.
This can only be done once per context.
!!! info
PNG's are read up to the file end marker, this is identical behavior to libpng.
# spng_set_png_file()
```c
int spng_set_png_file(spng_ctx *ctx, FILE *file)
```
Set input PNG file or output PNG file, depending on context type.
This can only be done once per context.
# spng_set_image_limits()
```c
int spng_set_image_limits(spng_ctx *ctx, uint32_t width, uint32_t height)
```
Set image width and height limits, these may not be larger than 2<sup>31</sup>-1.
# spng_get_image_limits()
```c
int spng_get_image_limits(spng_ctx *ctx, uint32_t *width, uint32_t *height)
```
Get image width and height limits.
`width` and `height` must be non-NULL.
# spng_set_chunk_limits()
```c
int spng_set_chunk_limits(spng_ctx *ctx, size_t chunk_size, size_t cache_limit)
```
Set chunk size and chunk cache limits, the default chunk size limit is 2<sup>31</sup>-1,
the default chunk cache limit is `SIZE_MAX`.
Reaching either limit while decoding is handled as an out-of-memory error.
!!!note
This can only be used for limiting memory usage, most standard chunks
do not require additional memory and are stored regardless of these limits.
# spng_get_chunk_limits()
```c
int spng_get_chunk_limits(spng_ctx *ctx, size_t *chunk_size, size_t *cache_limit)
```
Get chunk size and chunk cache limits.
# spng_get_row_info()
```c
int spng_get_row_info(spng_ctx *ctx, struct spng_row_info *row_info)
```
Copies the current, to-be-decoded (or to-be-encoded) row's information to `row_info`.
# spng_set_option()
```c
int spng_set_option(spng_ctx *ctx, enum spng_option option, int value)
```
Set `option` to the specified `value`.
For details see [Decode options](decode.md#decode-options) and [Encode options](encode.md#encode-options).
# spng_get_option()
```c
int spng_get_option(spng_ctx *ctx, enum spng_option option, int *value)
```
Get the value for the specified `option`.
For details see [Decode options](decode.md#decode-options) and [Encode options](encode.md#encode-options).
|