Because of https://github.com/nothings/stb/issues/113 I was wondering how good/bad stb_image_write’s PNG compression really is in comparison to other encoders.
So I did a quick comparison between stb_image_write (v0.98) LodePNG (version 20150418),
tdefl_write_image_to_png_file_in_memory_ex() (v1.15) and libpng (version 1.2.50
from Ubuntu 14.04), always with the highest possible compression I could configure.
I used four different test images, all 24bit RGB:
A screenshot from Quake2: Zaero in 1680x1050, a screenshot from RBDoom3BFG in 1392x920, the classic “Lenna” test image in 512x512 and a colorful wallpaper with several parrots in 2560x1600.
Update: I also tested a screenshot of Snakebird
I tested libpng with highest compression (
png_set_compression_level(png_ptr, 9);) and default compression.
libpng with highest compression is used as 100%, as it almost always yields the smallest image.
Sean was right with “It looks to me like the miniz doesn’t use PNG filters,
so it’s just compressing the raw image data as is, which isn’t going to be
very good compression either, but along a different axis.“.
Sometimes the compression of miniz (or more correctly
is better and sometimes
stbi_write_png() is better - so it doesn’t matter
which one you use, if you wanna use one of them.
However, compared to libpng and LodePNG both don’t compress very well -
the resulting images are 29%-78% bigger.
LodePNG on the other hand produces almost as good results as libpng (only 5%-8% bigger) and is significantly easier to use - integrating it in your project is easy (just drop the source and the header file to your project) and using it is about as easy as stbi_write_png().
(For image loading however I found stb_image much better than LodePNG, as it loads PNGs faster, see my other blogpost.
UPDATE: A kinda strange case is the Snakebird-Screenshot: Sean said that
“something more computer-arty – flat colors or gradients”
will yield different results, because
“that’s one of the places where the PNG filters are beneficial”.
And he was right: In that case the
stbi_write_png() output was only 22% bigger
than the libpng output - but for some reason miniz, which does not seem to
do PNG filtering, compressed even better than libpng (the resulting file was 13% smaller).
Can be found at: https://gist.github.com/DanielGibson/eb322f8054c2dfef06a9
- stb_image.h and stb_image_write.h from https://github.com/nothings/stb
- lodepng.c and lodepng.h from http://lodev.org/lodepng/
- miniz.c from https://github.com/richgel999/miniz
- libpng from http://libpng.org/ or your Linux distro or whatever (tested v1.2.50)
For Reference: The images
(click for full size)