OptimizedPaletteQuantizer Class

KGy SOFT Drawing Libraries Help
Represents a quantizer that can generate an optimized set of colors best matching to the original image. Use the static methods to retrieve an instance. For using predefined colors see the PredefinedColorsQuantizer class.
See the Remarks section for details and results comparison.
Inheritance Hierarchy

SystemObject
  KGySoft.Drawing.ImagingOptimizedPaletteQuantizer

Namespace:  KGySoft.Drawing.Imaging
Assembly:  KGySoft.Drawing (in KGySoft.Drawing.dll) Version: 5.0.0
Syntax

public sealed class OptimizedPaletteQuantizer : IQuantizer

The OptimizedPaletteQuantizer type exposes the following members.

Properties

  NameDescription
Public propertyPixelFormatHint
Gets a PixelFormat representing the lowest bits-per-pixel value format, which is compatible with this OptimizedPaletteQuantizer instance.
Top
Methods

  NameDescription
Public methodStatic memberCode exampleMedianCut
Gets an OptimizedPaletteQuantizer instance that quantizes colors of an image using the Median Cut quantizing algorithm.
See the Examples section for an example, and the Remarks section of the OptimizedPaletteQuantizer for details and results comparison with the other algorithms.
Public methodStatic memberCode exampleOctree
Gets an OptimizedPaletteQuantizer instance that quantizes colors of an image using the Octree quantizing algorithm.
See the Examples section for an example, and the Remarks section of the OptimizedPaletteQuantizer for details and results comparison with the other algorithms.
Public methodStatic memberCode exampleWu
Gets an OptimizedPaletteQuantizer instance that quantizes colors of an image using Xiaolin Wu's quantizing algorithm.
See the Examples section for an example, and the Remarks section of the OptimizedPaletteQuantizer for details and results comparison with the other algorithms.
Top
Remarks

The OptimizedPaletteQuantizer class can be used to reduce colors of a Bitmap using a palette of up to 256 colors where the palette entries are optimized for the quantized image.

This class supports palette optimization by three different algorithms (see the Octree, MedianCut and Wu methods)

The following table compares the algorithms supported by the OptimizedPaletteQuantizer class:

OctreeMedianCutWu
SpeedSlower than the Wu's algorithm but faster than MedianCut.This is the slowest one of the three algorithms.This is the fastest one of the three algorithms (still much slower though than the quantizers of the PredefinedColorsQuantizer class).
Memory consumption*Generating the palette may consume quite a large amount of memory but it also depends on the number of different colors of the source image and the requested color count.Generating the palette may consume quite a large amount of memory but it also depends on the number of different colors of the source image and the requested color count.Quite large memory consumption even if the source has few colors and the requested color count is small.
Quality
  • Usually poorer quality for smaller palettes (below 16 colors).
  • Banding may appear in images with large low-frequency areas (eg. sky or water in photos).
  • Balanced quality for larger palettes and photo-like images.
  • Usually very good quality even for smaller palettes.
  • Excellent, nearly banding-free results with images with large low-frequency areas (eg. sky or water in photos).
  • May provide a slightly poorer quality for small areas with unique colors (eg. a smaller human face in a large photo).
  • Usually very good quality even for smaller palettes.
  • Banding may appear in images with large low-frequency areas (eg. sky or water in photos), though not as heavily as in case of the Octree algorithm (and it can be blended by dithering anyway). The banding may appear even if the image uses just a few tones because Wu's algorithm generates colors from the RGB555 color space (which uses 32 shades of each color channels).
  • Very good quality for photo-like images, especially if the image has no homogeneous low-frequency areas.
Note Note

*Memory consumption mentioned in the table affects palette generation only. That occurs when the IQuantizer.Initialize method of an OptimizedPaletteQuantizer instance is called. As soon as this method returns with an IQuantizingSession instance, the memory mentioned in the table can be reclaimed.

On the other hand, the IQuantizingSession can also consume a large amount of memory during the quantization because its Palette caches the quantization results of the source image pixels, though this caching does not depend on the chosen algorithm and can also be reclaimed when the IQuantizingSession is disposed.

So keeping a reference to an OptimizedPaletteQuantizer consumes almost no memory when there is no active quantization session in progress.

The following table compares the results of the OptimizedPaletteQuantizer instances returned by the Octree, MedianCut and Wu methods.

Note Note
For better comparison none of the images are dithered in the examples, though the visual quality can be improved by using dithering. See the OrderedDitherer, ErrorDiffusionDitherer, RandomNoiseDitherer and InterleavedGradientNoiseDitherer classes for some built-in IDitherer implementations.
Original imageQuantized image

Color hues with alpha gradient
Color hues with alpha gradient

Color hues quantized by Octree algorithm using 256 colors, silver background, zero alpha threshold
Octree algorithm, 256 colors, silver background, zero alpha threshold

Color hues quantized by Median Cut algorithm using 256 colors, silver background, zero alpha threshold
MedianCut algorithm, 256 colors, silver background, zero alpha threshold

Color hues quantized by Wu's algorithm using 256 colors, silver background, zero alpha threshold
Wu's algorithm, 256 colors, silver background, zero alpha threshold

Information icon with transparent background
Information icon with transparency

Information icon quantized by Octree algorithm using 4 colors, silver background, zero alpha threshold
Octree algorithm, 4 colors, silver background, zero alpha threshold

Information icon quantized by Median Cut algorithm using 4 colors, silver background, zero alpha threshold
MedianCut algorithm, 4 colors, silver background, zero alpha threshold

Information icon quantized by Wu's algorithm using 4 colors, silver background, zero alpha threshold
Wu's algorithm, 4 colors, silver background, zero alpha threshold

Information icon quantized by Octree algorithm using 256 colors, black background, alpha threshold = 128
Octree algorithm, 256 colors, black background, alpha threshold = 128. Banding appeared in the result.

Information icon quantized by Median Cut algorithm using 256 colors, black background, alpha threshold = 128
MedianCut algorithm, 256 colors, black background, alpha threshold = 128. Practically there is no banding in the result.

Information icon quantized by Wu's algorithm using 256 colors, black background, alpha threshold = 128
Wu's algorithm, 256 colors, black background, alpha threshold = 128. A slight banding can be observed, as if the image was quantized by the PredefinedColorsQuantizer.Argb1555 quantizer (see the details in the comparison table above).

See Also

Reference