#graphics #dithering #image #clamp #error-diffusion #simple-dither

dithereens

Functions & traits for quantizing values with error-diffusion

3 releases

Uses new Rust 2024

new 0.1.2 May 14, 2025
0.1.1 May 14, 2025
0.1.0 Jun 17, 2024

#353 in Images

Apache-2.0…

8KB
77 lines

dithereens

Functions and traits for quantizing values with error-diffusion.

This is mostly useful when e.g. quantizing from a f32- or f16-per-channel color resolution to something like u16- or u8-per-channel. In these cases quantization without error-diffusion would lead to banding.

The crate uses generics to allow interpolation of any type for which certain traits are defined.

Examples

let mut rng = rand::rng();

let value: f32 = 0.5;

// Dither `value` to `127u8` or `128u8`, with a probability of
// 50%.
// Note that we still clamp the value since it could be outside
// the target type's range.
let dithered_value: u8 =
    clamp(simple_dither(value, 255.0, &mut rng) as u8, 0, 255);

assert!(dithered_value == 127 || 128 == dithered_value);

License

Apache-2.0 OR BSD-3-Clause OR MIT OR Zlib at your discretion.

Dependencies

~550KB