ErrorDiffusionDitherer Constructor

KGy SOFT Drawing Libraries Help
Initializes a new instance of the ErrorDiffusionDitherer class using the specified matrix, divisor and matrixFirstPixelIndex.
See the Examples section for some examples.

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

public ErrorDiffusionDitherer(
	byte[,] matrix,
	int divisor,
	int matrixFirstPixelIndex,
	bool serpentineProcessing = false,
	Nullable<bool> byBrightness = null
)

Parameters

matrix
Type: SystemByte
A matrix to be used as the coefficients for the quantization errors to be propagated to the neighboring pixels.
divisor
Type: SystemInt32
Each elements in the matrix will be divided by this value. If less than the sum of the elements in the matrix, then only a fraction of the error will be propagated.
matrixFirstPixelIndex
Type: SystemInt32
Specifies the first effective index in the first row of the matrix. If larger than zero, then the error will be propagated also to the bottom-left direction. Must be between 0 and matrix width, excluding upper bound.
serpentineProcessing (Optional)
Type: SystemBoolean
 to process odd lines right-to-left and even lines left-to-right;  to process all lines left-to-right.
See the Remarks section of the ConfigureProcessingDirection method for details. This parameter is optional.
Default value: .
byBrightness (Optional)
Type: SystemNullableBoolean
 to apply the same quantization error on every color channel determined by brightness difference;  to handle quantization errors on each color channels independently;  to auto select strategy. Deciding by brightness can produce a better result when fully saturated colors are mapped to a grayscale palette.
See the Remarks section of the ConfigureErrorDiffusionMode method for details. This parameter is optional.
Default value: .
Examples

The following example demonstrates how to use a custom ditherer using the ErrorDiffusionDitherer constructor:
C#
public static Bitmap ToCustomDithered(Bitmap source, IQuantizer quantizer)
{
    // This is actually the Fan dithering (by Zhihang Fan), and uses the same coefficients
    // as the Floyd-Steinberg dithering in a slightly different arrangement:
    byte[,] matrix =
    {
        { 0, 0, 0, 7 },
        { 1, 3, 5, 0 },
    };

    // The matrix values will be divided by this value to determine the portion
    // of the quantization error to propagate to neighboring pixels:
    int divisor = 16;

    // The current pixel to be processed is always one pixel left from this index.
    // This also means that if larger than 1, then some error is propagated also towards the
    // (bottom-)left direction. For the matrix above value "3" means that whenever a pixel is
    // processed, 7/16 of the error is propagated to the right, 1/16 and 3/16 to the
    // bottom-left direction and 5/16 one pixel down from the current pixel.
    int firstPixelIndex = 3;

    IDitherer ditherer = new ErrorDiffusionDitherer(matrix, divisor, firstPixelIndex);

    // a.) this solution returns a new bitmap and does not change the original one:
    return source.ConvertPixelFormat(quantizer.PixelFormatHint, quantizer, ditherer);

    // b.) alternatively, you can perform the dithering directly on the source bitmap:
    source.Dither(quantizer, ditherer);
    return source;
}

The example above may produce the following results:

Original imageQuantized and dithered image

Color hues with alpha gradient
Color hues with alpha gradient

Grayscale color shades with different bit depths
Grayscale color shades

Grayscale color shades with black and white palette using Fan dithering
Quantizing with black and white palette

Tip Tip
  • Use the static properties to perform dithering with predefined filters.
  • See the Remarks section of the ErrorDiffusionDitherer class for more details and image examples.
See Also

Reference