libspng is a C library for reading and writing Portable Network Graphics (PNG) format files with a focus on security and ease of use.

It is licensed under the BSD 2-clause “Simplified” License.


The goal is to provide a PNG library with a simpler API than libpng with less code.

Performance is also a priority, decode time is within 5% of libpng for RGB/RGBA images, see the comparison page.

The testsuite is designed to test both libraries, it has already discovered a bug in libpng.


  • March 25, 2019: Released v0.4.5 with bugfixes.
  • March 11, 2019: Released v0.4.4 with amalgamation archive and performance optimizations.
  • February 12, 2019: Released v0.4.3 with bugfixes.
  • January 21, 2019: libspng is now available on Wrap DB.
  • January 18, 2019: Released v0.4.2 with bugfixes and performance optimizations for x86.
  • January 8, 2019: libspng is now continuously fuzzed by OSS-Fuzz.
  • December 2, 2018: Released v0.4.1 with bugfixes and improved documentation.
  • November 12, 2018: Released v0.4.0, first stable release with CMake support.
  • September 3, 2018: Released v0.3.1 with API documentation.
  • August 1, 2018: Released v0.3.0 with file validation and code improvements.
  • May 21, 2018: Released v0.2.0 with stream support, gamma correction bugfixes.
  • March 17, 2018: Initial release

Getting libspng

Extract and include spng.c/spng.h in your project.

Or build with Meson:

cd libspng-0.4.5
meson build
cd build
ninja install


/* Create a context */
spng_ctx *ctx = spng_ctx_new(0);

/* Set an input buffer */
spng_set_png_buffer(ctx, buf, buf_size);

/* Determine output image size */
spng_decoded_image_size(ctx, SPNG_FMT_RGBA8, &out_size);

/* Decode to 16-bit RGBA, do gamma-correction using gAMA chunk information if available. */
spng_decode_image(ctx, out, out_size, SPNG_FMT_RGBA16, SPNG_DECODE_USE_GAMA);

/* Free context memory */

See example.c, online documentation is available here.


Code is written according to the rules of the CERT C Coding Standard. All integer arithmetic is checked for overflow and all error conditions are handled gracefully.

The library is continuously fuzzed by OSS-Fuzz, releases are scanned with Clang Static Analyzer, PVS-Studio and Coverity Scan and have a Defect Density of 0.00.


The test suite consists of over 700 test cases, 175 test images are decoded with all possible output format and flag combinations and compared against libpng's output.

The testsuite also includes regression tests from libpng and is compiled with AddressSanitizer and UndefinedBehaviorSanitizer.


How does it compare to X?

See the comparison page.

Will it be drop-in compatible with libpng?

Probably not. A compatibility layer would need a lot of features implemented outside of libspng.

Are there any size limits?

There are no set limits. libspng should be safe to use with images or files of any size, at worst it will run out of memory or encounter an integer overflow, all errors are handled gracefully.

Is libspng threadsafe?

libspng is threadsafe as long as contexts are not shared across threads. It is safe to create multiple contexts on a thread.

Why are some functions returning SPNG_EBADSTATE?

A previous function call encountered an irrecoverable error, most decoding errors are not recoverable. Refer to the documentation on error handling.


results matching ""

    No results matching ""