OrderedDitherer Class

Provides an IDitherer implementation for dithering patterns that are based on an ordered matrix. Use the static properties of this class to use predefined patterns or the constructor to create a custom ordered ditherer.

Definition

Namespace: KGySoft.Drawing.Imaging
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 8.2.0
C#
public sealed class OrderedDitherer : IDitherer
Inheritance
Object    OrderedDitherer
Implements
IDitherer

Remarks

The constructor can be used to create an ordered ditherer using a custom matrix.

Use the static properties to obtain an instance with a predefined pattern. For the best results use the Bayer8x8 or BlueNoise properties. Or you can use the DottedHalftone property for artistic results.

The OrderedDitherer class offers a very fast dithering technique based on an ordered pattern specified in a matrix of bytes. The more different values the matrix has the more number of different patterns can be mapped to the shades of the original pixels. While quantizing lighter and lighter colors, the different patterns appear in the order of the values in the specified matrix.

The following table demonstrates the effect of the dithering:

Original image
Quantized image

Color hues with alpha gradient
Color hues with alpha gradient

Color hues with system default 8 BPP palette and silver background
Quantizing with system default 8 BPP palette, no dithering

Color hues with system default 8 BPP palette, silver background and Bayer 8x8 ordered dithering
Quantizing with system default 8 BPP palette and Bayer 8x8 dithering

Color hues with system default 8 BPP palette, using silver background and blue noise dithering
Quantizing with system default 8 BPP palette and blue noise dithering

Color hues with system default 8 BPP palette, using silver background and rectangular 7x7 dotted halftone pattern dithering
Quantizing with system default 8 BPP palette and dotted halftone pattern dithering

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with black and white palette
Quantizing with black and white palette, no dithering

Grayscale color shades with black and white palette, using Bayer 8x8 ordered dithering
Quantizing with black and white palette and Bayer 8x8 dithering

Grayscale color shades with black and white palette using blue noise dithering
Quantizing with black and white palette and blue noise dithering

Grayscale color shades with black and white palette using rectangular 7x7 dotted halftone pattern dithering
Quantizing with black and white palette and dotted halftone pattern dithering

Unlike in case of the ErrorDiffusionDitherer, ordered dithering does not adjust strength to the quantization error of a pixel but simply uses the specified matrix values based on pixel coordinates to determine the quantized result. Therefore, a strength can be specified (see the constructor and the ConfigureStrength method), whose ideal value depends on the colors that a quantizer can return. If the strength is too low, then banding may appear in the result in place of gradients in the original image; whereas if the strength is too high, then dithering patterns may appear even in colors without quantization error (overdithering).

Every static property in the OrderedDitherer returns an instance with auto strength, meaning that strength will be calibrated for each dithering session so that neither the black, nor the white colors will suffer from overdithering in the result.

Auto strength can use different calibration strategies. The default strategy is usually correct if the quantizer returns evenly distributed colors. Otherwise, you can apply the Interpolated auto strength mode by the ConfigureAutoStrengthMode method that calibrates the strength both for the black and white colors and uses a dynamic strength to each pixel based on its brightness. If none of the auto strength modes provide the desired result you can obtain an OrderedDitherer instance with custom strength by the ConfigureStrength method.

The following table demonstrates the effect of different strengths:

Original image
Quantized image

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with system default 4 BPP palette
Quantizing with system default 4 BPP palette, no dithering. The asymmetry is due to the uneven distribution of gray shades of this palette.

Grayscale color shades with system default 4 BPP palette using Bayer 8x8 ordered dithering
Quantizing with system default 4 BPP palette and Bayer 8x8 dithering using auto strength. Darker shades have banding.

Grayscale color shades with system default 4 BPP palette using a stronger Bayer 8x8 ordered dithering
Quantizing with system default 4 BPP palette and Bayer 8x8 dithering using strength = 0.5. Now there is no banding but white suffers from overdithering.

Grayscale color shades with system default 4 BPP palette using 8x8 ordered dithering with interpolated ato strength
Quantizing with system default 4 BPP palette and Bayer 8x8 dithering using Interpolated auto strength strategy. Now there is neither banding nor overdithering for black or white colors.

  Tip

See the Examples section of the static properties for more examples.

Constructors

OrderedDitherer Initializes a new instance of the OrderedDitherer class using the specified matrix and strength.

Properties

Bayer2x2 Gets an OrderedDitherer using the standard Bayer 2x2 matrix.
Bayer3x3 Gets an OrderedDitherer using the standard Bayer 3x3 matrix.
Bayer4x4 Gets an OrderedDitherer using the standard Bayer 4x4 matrix.
Bayer8x8 Gets an OrderedDitherer using the standard Bayer 8x8 matrix.
BlueNoise Gets an OrderedDitherer using a fixed 64x64 blue noise pattern of 256 different values.
DottedHalftone Gets an OrderedDitherer using a 8x8 matrix of a dotted halftone pattern.

Methods

ConfigureAutoStrengthMode Gets a new OrderedDitherer instance that uses auto strength using the specified autoStrengthMode.
See the Remarks section of the ConfigureStrength method for details and image examples.
ConfigureStrength Gets a new OrderedDitherer instance that has the specified dithering strength.

Extension Methods

InitializeAsync Gets an IDitheringSession instance potentially asynchronously that can be used to dither the result of the specified IQuantizingSession applied to the specified source. If ditherer is a known ditherer that can be evaluated quickly, then this method might be executed synchronously.
This method is available in.NET Framework 4.0 and above.
(Defined by DithererExtensions)

See Also