OptimizedPaletteQuantizerConfigureBitLevel Method

Configures the bit level per color channel to be used while optimizing the palette. If the input image is a monochromatic one, then may determine the bit depth of the result, depending on the used algorithm. Affects the quality, speed and memory usage.


Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.1.0
public OptimizedPaletteQuantizer ConfigureBitLevel(
	int? bitLevel


bitLevel  NullableInt32
Specifies the desired bit level. If , then the value is automatically set by the chosen algorithm.

Return Value

An OptimizedPaletteQuantizer instance that has the specified bit level.


As a primary effect, bitLevel determines the upper limit of the possible colors in the generated palette. For example, if bitLevel is 1, then the result palette will not have more than 8 colors, or when it is 2, more than 64 colors. If you want to quantize an image using the allowed maximum of 65536 colors, then bitLevel should be at least 6 because 5 allows up to 32768 colors.

When using the MedianCut algorithm, configuring the bit level has no other effects. When using the Octree or Wu's algorithms, bitLevel determines also the amount of minimum distinguishable monochromatic shades. For example, when bitLevel is 5, then up to 32 monochromatic shades can be differentiated so close shades might be merged even if the requested number of colors would allow returning all the shades.

For the Octree algorithm the default value is the ceiling of the base 2 logarithm of the requested number of colors (eg. 1 for 2 colors, 8 for 129 or more colors). This is alright for most cases. You can increase the default value if the image has only a few but very close colors or decrease it if the image has so many colors that the quantization would use too much memory.

For Wu's algorithm the default value is 5 for no more than 256 colors (requires about 1.5 MB fix memory) and 6 for more colors (requires about 10 MB). This provides good enough quality in most cases but may cause visible banding if the input image is monochrome. To avoid that you can increase the bit level, which dramatically increases also the memory requirement: 7 bits requires about 80 MB memory, whereas 8 bits demands about 650 MB, regardless of the actual number of colors in the source image.


ArgumentOutOfRangeExceptionbitLevel must be either , or between 1 and 8.

See Also