nQuantCpp

nQuantCpp includes top 6 color quantization algorithms for visual c++ producing high quality optimized images. I enhance each of the algorithms to support semi transparent images. nQuantCpp also provides a command line wrapper in case you want to use it from the command line.

Either download nQuantCpp from this site or add it to your Visual Studio project seamlessly. PNG is useful because it’s the only widely supported format which can store partially transparent images. The format uses compression, but the files can still be large. Use Color quantization algorithms can be chosen by command line since version 1.10 using the /a algorithm. Only png can support semi transparent image and desired color depth. Gif can ensure the number of colors for the converted image is 256 or less. Bmp does support desired color depth. Jpg only supports 24-bit image format.

Let’s climb up the mountain: Ready, Go!!!

Original photo of climbing

Reduced to 256 colors by Divisive hierarchical clustering algorithm

Reduced to 256 colors by NeuQuant Neural-Net Quantization Algorithm

Reduced to 16 colors by Fast pairwise nearest neighbor based algorithm

Reduced to 16 colors by Fast pairwise nearest neighbor based algorithm with CIELAB color space

Reduced to 16 colors by Xialoin Wu's fast optimal color Quantization Algorithm


Original photo of Aetna's Hartford headquarters

Reduced to 256 colors by NeuQuant Neural-Net Quantization Algorithm

Reduced to 256 colors by Fast pairwise nearest neighbor based algorithm


Original image of Hong Kong Cuisines

Fast pairwise nearest neighbor based algorithm with CIELAB color space in 16 colors
High quality and fast
Fast pairwise nearest neighbor based algorithm with CIELAB color space with CIELAB color space in 16 colors</p>

Fast pairwise nearest neighbor based algorithm with CIELAB color space Plus (parallel quantum inspired Genetic Algorithm) in 16 colors
Fast pairwise nearest neighbor based algorithm with CIELAB color space with CIELAB color space Plus (parallel quantum inspired Genetic Algorithm) in 16 colors

Efficient, Edge-Aware, Combined Color Quantization and Dithering with CIELAB color space in 16 colors
Higher quality for 32 or less colors but slower
Efficient, Edge-Aware, Combined Color Quantization and Dithering with CIELAB color space in 16 colors

Spatial color quantization with CIELAB color space in 16 colors
Higher quality for 32 or less colors but the slowest
Spatial color quantization with CIELAB color space in 16 colors

All in all, the top 3 color quantization algorithms for 256 colors are:

  1. Fast pairwise nearest neighbor based algorithm
  2. Xialoin Wu's fast optimal color quantizer
  3. NeuQuant Neural-Net Quantization Algorithm with CIELAB color space

The top 3 color quantization algorithms for 32 colors or less are:

  1. Fast pairwise nearest neighbor based algorithm with CIELAB color space Plus (parallel quantum inspired Genetic Algorithm)
  2. Efficient, Edge-Aware, Combined Color Quantization and Dithering algorithm with CIELAB color space
  3. Spatial color quantization algorithm with CIELAB color space

If you are using the command line. Assuming you are in the same directory as nQuantCpp.exe, you would enter: nQuantCpp yourImage.jpg /m 16 /a pnnlab.
To avoid dot gain, /d n can set the dithering to false. However, false contours will be resulted for gradient color zones.
nQuantCpp will quantize yourImage.jpg with maximum colors 16, algorithm pnnlab and create yourImage-PNNLABquant16.png in the same directory.

The readers can see coding of the error diffusion and dithering are quite similar among the above quantization algorithms. Each algorithm has its own advantages. I share the source of color quantization to invite further discussion and improvements. Such source code are written in C++ to gain best performance. It is readable and convertible to c#, java, or javascript. Welcome for C++ experts for further improvement or provide color quantization algorithms better than the above algorithms. Please use issues to track ideas, enhancements, tasks, or bugs for work on GitHub.