Skip to content

Commit

Permalink
docs: add section on memory usage [skip-ci]
Browse files Browse the repository at this point in the history
also moved notes on memory usage from usage.md
  • Loading branch information
randy408 committed Apr 23, 2021
1 parent 573f2db commit de6f83b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 24 additions & 0 deletions docs/decode.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,30 @@ it implements a subset of its features:
The `SPNG_CTX_IGNORE_ADLER32` context flag has the same effect as `PNG_IGNORE_ADLER32`
used with `png_set_option()`.

## Memory usage

The library will always allocate a context buffer,
if the input PNG is a stream or file it will also create a read buffer.

Decoding an image requires at least 2 rows to be kept in memory,
3 if the image is interlaced.

Gamma correction requires an additional 128KB for a lookup table if
the output format has 16 bits per channel (e.g. `SPNG_FMT_RGBA16`).

To limit memory usage for image decoding use `spng_set_image_limits()`
to set an image width/height limit.
This is the equivalent of `png_set_user_limits()`.

Moreover the size calculated by `spng_decoded_image_size()` can be checked
against a hard limit before allocating memory for the output image.

Chunks of arbitrary length (e.g. text, color profiles) take up additional memory,
`spng_set_chunk_limits()` is used to set hard limits on chunk length- and cache limits,
note that reaching either limit is handled as a fatal error.

This function combines the functionality of libpng's `png_set_chunk_cache_max()` and `png_set_chunk_malloc_max()`.

# API

# spng_set_png_buffer()
Expand Down
8 changes: 1 addition & 7 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ spng_ctx_free(ctx);

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

0 comments on commit de6f83b

Please sign in to comment.