colorspace-rs
colorspace-rs copied to clipboard
Add dithering for From<RGBf> for RGBu8/RGBu16
I.e. what good old RiQuantize does. I think it's safe to assume a dither amplitude of 0.5.
This should be built-in. I do not see anyone wanting to see banding when they do a conversion to one of those two types.
Only challenge I see is a nice API for seeding the RNG so results are deterministic on re-runs.
Rust version of RiQuantize:
use num_traits::float::Float;
use core::ops::Mul;
use oorandom;
pub fn quantize<T>(value: T, one: T, min: T, max: T, dither_amplitude: T, rng: &mut oorandom::Rand32) -> T
where
T: Float + Mul<f32, Output = T>
{
use clamped::Clamp;
(one * value + dither_amplitude * rng.rand_float())
.round()
.clamped(min, max)
}