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.

Peformance is also a priority, decoding is 8% faster than libpng for truecolor images with an alpha channel and 3% faster for truecolor images. See the comparison page.

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


  • December 2, 2018: Released v0.4.1 with bugfixes and improved documentation.
  • November 12, 2018: Released v0.4.0, first stable relase 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 build with CMake:

cd libspng-0.4.1
mkdir cbuild
cd cbuild
cmake ..
make install

or build with meson:

cd libspng-0.4.1
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);

/* Get an 8-bit RGBA image, regardless of PNG format */
spng_decode_image(ctx, SPNG_FMT_RGBA8, out, out_size, 0);

/* Get 16-bit RGBA image, 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. Releases are scanned with Clang Static Analyzer and Coverity Scan and have a Defect Density of 0.00.


libspng comes with an extensive test suite. There are 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 ""