module

CrImage::Util::Dithering

Dithering algorithms for reducing color depth while maintaining visual quality. Implements error diffusion and ordered dithering techniques.

Class methods

apply(src : Image, palette : Color::Palette, algorithm : DitheringAlgorithm = DitheringAlgorithm::FloydSteinberg) : Paletted

Applies dithering to convert an image to a limited palette.

Dithering creates the illusion of more colors by distributing quantization error to neighboring pixels.

Parameters:

  • src : The source image
  • palette : Target color palette
  • algorithm : Dithering algorithm to use (default: FloydSteinberg)

Returns: A new Paletted image with dithering applied

Example:

img = CrImage::PNG.read("photo.png")
palette = CrImage::Color::Palette.new([
  CrImage::Color::BLACK,
  CrImage::Color::WHITE,
] of CrImage::Color::Color)
dithered = CrImage::Util::Dithering.apply(img, palette)
Source
atkinson(src : Image, palette : Color::Palette) : Paletted

Applies Atkinson dithering (convenience method).

Atkinson dithering distributes error to 6 pixels but only uses 75% of the error, creating a lighter, more artistic effect.

Example:

dithered = CrImage::Util::Dithering.atkinson(img, palette)
Source
floyd_steinberg(src : Image, palette : Color::Palette) : Paletted

Applies Floyd-Steinberg dithering (convenience method).

Floyd-Steinberg is the most common error diffusion algorithm, distributing error to 4 neighboring pixels.

Example:

dithered = CrImage::Util::Dithering.floyd_steinberg(img, palette)
Source
ordered(src : Image, palette : Color::Palette) : Paletted

Applies ordered (Bayer matrix) dithering (convenience method).

Ordered dithering uses a threshold matrix, faster than error diffusion but produces a more regular pattern.

Example:

dithered = CrImage::Util::Dithering.ordered(img, palette)
Source