dithereens

Functions and traits for quantizing values with error-diffusion.
Quantizing from f32/f16 to u16/u8 without dithering leads to.
banding. This crate provides dithering to reduce quantization artifacts.
Overview
- Single values:
dither(),simple_dither(). - Iterator processing:
dither_iter(),simple_dither_iter(). - In-place operations:
dither_slice(),simple_dither_slice(). - Iterator adapters:
DitherIteratorExtfor method chaining. - Trait-based API:
Dither,SimpleDithertraits. - no_std support: Works in embedded environments.
- Generic types:
f32,f64, or anyDitherFloatimplementation.
Quick Start
let mut rng = thread_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 =
simple_dither.clamp as u8;
assert!;
Iterator Adapters
Use DitherIteratorExt for ergonomic method chaining:
let mut rng = seed_from_u64;
let pixel_values = vec!0.2f32, 0.5, 0.8, 0.1, 0.9;
let result: = pixel_values
.iter
.copied
// +3/4 EV exposure.
.map
// Dither.
.simple_dither;
Performance Guide
Based on benchmarks with 10,000 values:
- Single values:
dither(),simple_dither(). - In-place slice operations:
dither_slice(),simple_dither_slice()(~5.6x faster than iterator methods) - Iterator chains:
dither_iter(),simple_dither_iter(), orDitherIteratorExtadapters (allocation overhead)
Parallel Processing
Via rayon -- enabled by default.
= { = "0.1", = "rayon" }
With rayon enabled, batch and slice functions use parallel processing..
RNG must implement Rng + Send + Clone..
no_std Support
This crate supports no_std environments. The libm crate can be used to
pull in a possibly faster, native round() implementation. Otherwise a
manual implementation is used in no_std environments.
# `no_std`
= { = "0.1", = false }
# Optional: uses `libm`'s `round()` function instead of a manual implementation for `no_std`.
= { = "0.1", = false, = "libm" }
License
Apache-2.0 OR BSD-3-Clause OR MIT OR Zlib at your discretion.