Skip to content

Commit de6f83b

Browse files
committed
docs: add section on memory usage [skip-ci]
also moved notes on memory usage from usage.md
1 parent 573f2db commit de6f83b

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

docs/decode.md

+24
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,30 @@ it implements a subset of its features:
140140
The `SPNG_CTX_IGNORE_ADLER32` context flag has the same effect as `PNG_IGNORE_ADLER32`
141141
used with `png_set_option()`.
142142

143+
## Memory usage
144+
145+
The library will always allocate a context buffer,
146+
if the input PNG is a stream or file it will also create a read buffer.
147+
148+
Decoding an image requires at least 2 rows to be kept in memory,
149+
3 if the image is interlaced.
150+
151+
Gamma correction requires an additional 128KB for a lookup table if
152+
the output format has 16 bits per channel (e.g. `SPNG_FMT_RGBA16`).
153+
154+
To limit memory usage for image decoding use `spng_set_image_limits()`
155+
to set an image width/height limit.
156+
This is the equivalent of `png_set_user_limits()`.
157+
158+
Moreover the size calculated by `spng_decoded_image_size()` can be checked
159+
against a hard limit before allocating memory for the output image.
160+
161+
Chunks of arbitrary length (e.g. text, color profiles) take up additional memory,
162+
`spng_set_chunk_limits()` is used to set hard limits on chunk length- and cache limits,
163+
note that reaching either limit is handled as a fatal error.
164+
165+
This function combines the functionality of libpng's `png_set_chunk_cache_max()` and `png_set_chunk_malloc_max()`.
166+
143167
# API
144168

145169
# spng_set_png_buffer()

docs/usage.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ spng_ctx_free(ctx);
2626

2727
```
2828
29-
For a complete example see [example.c](https://github.com/randy408/libspng/blob/v0.6.3/examples/example.c)
29+
For a complete example see [example.c](https://github.com/randy408/libspng/blob/v0.6.3/examples/example.c).
3030
3131
3232
## Decoding untrusted files
@@ -42,9 +42,3 @@ To decode untrusted files safely it is required to at least:
4242
to avoid running out of memory. Note that exceeding either limit is
4343
handled as an out-of-memory error since v0.6.0.
4444
45-
### Notes on memory use
46-
47-
The library allocates 2 to 3 times the width of the PNG image for decoding.
48-
49-
Gamma correction requires an additional 128KB for a lookup table if
50-
the output format has 16-bits per channel (e.g. `SPNG_FMT_RGBA16`).

0 commit comments

Comments
 (0)