pixman/
dither.rs

1use pixman_sys as ffi;
2use thiserror::Error;
3
4/// Defiens the possible dither operations
5#[derive(Debug, Clone, Copy)]
6pub enum Dither {
7    /// No dithering
8    None,
9    /// Fast dithering
10    Fast,
11    /// Good dithering
12    Good,
13    /// Best dithering
14    Best,
15    /// Ordered bayer 8 dithering
16    OrderedBayer8,
17    /// Ordered blue noise 64
18    OrderedBlueNoise64,
19}
20
21/// The dither operation is unknown
22#[derive(Debug, Error)]
23#[error("Unknown dither {0}")]
24pub struct UnknownDither(ffi::pixman_dither_t);
25
26impl TryFrom<ffi::pixman_dither_t> for Dither {
27    type Error = UnknownDither;
28
29    fn try_from(value: ffi::pixman_dither_t) -> Result<Self, Self::Error> {
30        let repeat = match value {
31            ffi::pixman_dither_t_PIXMAN_DITHER_NONE => Dither::None,
32            ffi::pixman_dither_t_PIXMAN_DITHER_FAST => Dither::Fast,
33            ffi::pixman_dither_t_PIXMAN_DITHER_GOOD => Dither::Good,
34            ffi::pixman_dither_t_PIXMAN_DITHER_BEST => Dither::Best,
35            ffi::pixman_dither_t_PIXMAN_DITHER_ORDERED_BAYER_8 => Dither::OrderedBayer8,
36            ffi::pixman_dither_t_PIXMAN_DITHER_ORDERED_BLUE_NOISE_64 => Dither::OrderedBlueNoise64,
37            _ => return Err(UnknownDither(value)),
38        };
39        Ok(repeat)
40    }
41}
42
43impl From<Dither> for ffi::pixman_dither_t {
44    fn from(value: Dither) -> Self {
45        match value {
46            Dither::None => ffi::pixman_dither_t_PIXMAN_DITHER_NONE,
47            Dither::Fast => ffi::pixman_dither_t_PIXMAN_DITHER_FAST,
48            Dither::Good => ffi::pixman_dither_t_PIXMAN_DITHER_GOOD,
49            Dither::Best => ffi::pixman_dither_t_PIXMAN_DITHER_BEST,
50            Dither::OrderedBayer8 => ffi::pixman_dither_t_PIXMAN_DITHER_ORDERED_BAYER_8,
51            Dither::OrderedBlueNoise64 => ffi::pixman_dither_t_PIXMAN_DITHER_ORDERED_BLUE_NOISE_64,
52        }
53    }
54}