Pngnq is a tool for quantizing PNG images in RGBA format.
Version 1.0 includes improvements made by porneL: http://pornel.net/pngnq
The neuquant algorithm uses a neural network to optimise the color map selection. This is fast and quite accurate, giving good results on many types of images.
Why another quantizer?
Pngnq exists because I needed a lot (several thousand) of png images in RGBA format to be quantized. After some searching, the only tool I could find that worked for RGBA was pngquant. I tried pngquant but found that the median cut algorithm that it uses, with or without dithering, gave inferior looking results to the neuquant algorithm. You can see the difference demonstrated on the neuquant page.
Pngnq was written to fulfill the specific needs of the small project I was working on, and as such does not have all the functionality one could want. It is developing though, I have recently (June 2006) come back to it to add functionality and make it more useful in various situations. It will stay as a command line quantization tool rather than evolve into a full compression process as I'd rather keep the program as performing one specific function. If optimal compression is needed one can use something like pngcrush after quantization.
I use Greg Roelf's png reading and writing code unchanged for the most part. The pngnq source code includes neuquant32, which is an extension of Anthony Dekker's neuquant. This has been modified to allow it to handle 4 channels of pixel data and to vary the number of colors quantized to. Thanks go to these people for developing such useful code and making it open source.
Comparisons with other quantizers.
The quantizer is fairly slow compared to median cut or octree methods. It is most certainly faster than Spatial Quantization techniques. It is limited mostly to off-line uses rather than real time image delivery. Even so, I am sure Moore's Law will make that statement false at some short time in the future. I've not run any comprehensive speed comparisons with other algorithms.
The speed of the algorithm can be increased by increasing the sample factor ( -s option ). The sample factor determines what fraction of pixels are examined by the algorithm.
Pngnq seems to work best when quantizing to a fairly large number of colors (>=64). This mainly a result of the visible edges due to the lack of dithering. The amount of compression of images gained is determined largely by the level of quantization. Using a tool like pngcrush to further process these images results in slightly smaller images than with methods that dither the image. This is probably due to the lack of high frequency data in the non-dithered images.
To quantize and compress an image, for example image.png, with the compressed output as smallimage.png, I do this:
pngnq -n 256 image.png && pngcrush image-nq8.png smallimage.png
This usually results in a greater than 50% compression with a small loss of detail.
I have run tests on the measured difference between quantized and unquantized images using pngnq, pngquant and scolorq. The error measure used is the Euclidian distance in RGBA space between pixels in the quantized and unquantized images. These have shown that the neuquant algorithm performs better than the median cut for most images, I suspect though that each reach close to the . Since reading Puzicha et. al. and running some tests, I have decided that this may not be the best measure of percieved difference in images. A better test of “percieved” image distance is in development.
I've run some comparisons here: Sample quantized images.
I have tested pngnq with these test images. No errors occur except in the deliberately corrupt test images, in which case the image loading correctly reports the corrupt image and the quantizer carries on as normal.
Things that pngnq needs work on:
I want to change the error measure to use CIE u'v' color space and see if results are visually better. Or perhaps add a perception filter model.
Limited image input formats. I am not doing much checking on the image pixel depth etc.
There seem to be few image editing programs that correctly deal with paletted RGBA png images. This is a pity since the alpha channel is so useful on the web, when browsers support it correctly. The only other quantization program I have found that does this is pngquant.
A plug in for GIMP (Problematic since GIMP does not display RGBA paletted mode although it loads and saves them alright.)
An ImageMagick library extension to do RGBA quantization.
May 2009: Merged 'improved pngnq' and added configure script. Changes include gamma setting, better assignment of transparent colors so they don't take up too much of the palette space, Floyd Steinberg dithering. Many thanks to Kornel Lesinski for his great improvements. Im calling this version 1.0, for what it's worth. It's in svn, and Linux and Mac binaries have been built. The other binaries will be forthcoming. I'll also be adding example images using the new version soon so you can see what it does.
December 2007: moved pngnq to Sourceforge.
October-December 2007. Windows binary and Mac universal binary made available. A Gentoo ebuild for 0.5 is also available. Version 0.6 has been started.
17th June 2006. Sample Factor Bug. I have found a bug which I inadvertently introduced into the Neuquant code which causes the neuquant algorithm to scan only 3/4 of the pixels. The bug effectively causes the sample factor to be 25% too large. Whilst this gives acceptable results the image quality is improved when all pixels are scanned. The patch for this bug is available for version 0.3 and version 0.4.0. Version 0.4.1 and 0.3.1 have been patched.
0.5 Update and bugfix.
Fixed a bug that caused images to turn green. Thanks to Chen Bin.
Added -e and -d options so that output filenames can be modified
Compiled windows 32 bit version.
0.4 Second release.
Cleaned up the code and added a few comments.
Added -n option to specify how many colours to quantize to. This can be from 2 to 256. and defualts to 256.
Added -h option to display help. Compiled Mac, Linux AMD64 and BSD binaries.
Added -V option to display version information.
Created pngcomp, a test program to compare images for testing purposes.
0.3. First release June 2004.
I can be contacted via the my sourceforge page.
Bug reports and feature requests should be directed at the appropriate tracker page.
I also will be keeping an eye on the forum for anyone requiring help and advice.
Pngnq is now hosted on Sourceforge.net, for all binaries go to pngnq downloads
For the current source code see the svn repository.