1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! Collection of composition types.

pub mod basic_f32;
pub mod basic_premultiplied_f32;
#[cfg(feature = "image-crate")]
pub mod image_rgb_rgba;

/// Compositor attributes
pub trait CompositorAttr {
    /// If true, the compositor requires updating destinations even alpha is zero.
    fn keep_dst_on_transparent_src(&self) -> bool;
}

/// Compositor composites two pixels with alpha value.
pub trait Compositor<T>: CompositorAttr {
    fn composite(&self, dst: &T, src: &T, alpha: f64) -> T;
}

#[derive(Clone)]
pub struct Clear;
#[derive(Clone)]
pub struct Src;
#[derive(Clone)]
pub struct Dst;
#[derive(Clone)]
pub struct SrcOver;
#[derive(Clone)]
pub struct SrcIn;
#[derive(Clone)]
pub struct SrcOut;
#[derive(Clone)]
pub struct SrcAtop;
#[derive(Clone)]
pub struct DstOver;
#[derive(Clone)]
pub struct DstIn;
#[derive(Clone)]
pub struct DstOut;
#[derive(Clone)]
pub struct DstAtop;
#[derive(Clone)]
pub struct Xor;
#[derive(Clone)]
pub struct Add;
#[derive(Clone)]
pub struct Darken;
#[derive(Clone)]
pub struct Lighten;
#[derive(Clone)]
pub struct Multiply;
#[derive(Clone)]
pub struct Screen;
#[derive(Clone)]
pub struct Overlay;
#[derive(Clone)]
pub struct HardLight;
#[derive(Clone)]
pub struct Dodge;
#[derive(Clone)]
pub struct Burn;
#[derive(Clone)]
pub struct SoftLight;
#[derive(Clone)]
pub struct Difference;
#[derive(Clone)]
pub struct Exclusion;

/// Dynamically composition type.
#[derive(Clone)]
pub enum Basic {
    Clear,
    Src,
    Dst,
    SrcOver,
    SrcIn,
    SrcOut,
    SrcAtop,
    DstOver,
    DstIn,
    DstOut,
    DstAtop,
    Xor,
    Add,
    Darken,
    Lighten,
    Multiply,
    Screen,
    Overlay,
    HardLight,
    Dodge,
    Burn,
    SoftLight,
    Difference,
    Exclusion,
}

impl CompositorAttr for Clear {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for Src {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for Dst {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for SrcOver {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for SrcIn {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for SrcOut {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for SrcAtop {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for DstOver {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for DstIn {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for DstOut {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for DstAtop {fn keep_dst_on_transparent_src(&self) -> bool {false}}
impl CompositorAttr for Xor {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Add {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Darken {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Lighten {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Multiply {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Screen {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Overlay {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for HardLight {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Dodge {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Burn {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for SoftLight {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Difference {fn keep_dst_on_transparent_src(&self) -> bool {true}}
impl CompositorAttr for Exclusion {fn keep_dst_on_transparent_src(&self) -> bool {true}}

impl CompositorAttr for Basic {
    fn keep_dst_on_transparent_src(&self) -> bool {
        use Basic::*;
        match self {
            Clear => Clear.keep_dst_on_transparent_src(),
            Src => Src.keep_dst_on_transparent_src(),
            Dst => Dst.keep_dst_on_transparent_src(),
            SrcOver => SrcOver.keep_dst_on_transparent_src(),
            SrcIn => SrcIn.keep_dst_on_transparent_src(),
            SrcOut => SrcOut.keep_dst_on_transparent_src(),
            SrcAtop => SrcAtop.keep_dst_on_transparent_src(),
            DstOver => DstOver.keep_dst_on_transparent_src(),
            DstIn => DstIn.keep_dst_on_transparent_src(),
            DstOut => DstOut.keep_dst_on_transparent_src(),
            DstAtop => DstAtop.keep_dst_on_transparent_src(),
            Xor => Xor.keep_dst_on_transparent_src(),
            Add => Add.keep_dst_on_transparent_src(),
            Darken => Darken.keep_dst_on_transparent_src(),
            Lighten => Lighten.keep_dst_on_transparent_src(),
            Multiply => Multiply.keep_dst_on_transparent_src(),
            Screen => Screen.keep_dst_on_transparent_src(),
            Overlay => Overlay.keep_dst_on_transparent_src(),
            HardLight => HardLight.keep_dst_on_transparent_src(),
            Dodge => Dodge.keep_dst_on_transparent_src(),
            Burn => Burn.keep_dst_on_transparent_src(),
            SoftLight => SoftLight.keep_dst_on_transparent_src(),
            Difference => Difference.keep_dst_on_transparent_src(),
            Exclusion => Exclusion.keep_dst_on_transparent_src(),
        }
    }
}

/// For Measuring performance.
pub mod perf {
    use crate::pixel::Rgba;
    use super::*;

    #[derive(Clone)]
    pub struct Perf;

    impl CompositorAttr for Perf {
        fn keep_dst_on_transparent_src(&self) -> bool {
            false
        }
    }

    #[cfg(feature = "image-crate")]
    impl Compositor<image::Rgba<u8>> for Perf {
        #[allow(unused_variables)]
        fn composite(&self, a: &image::Rgba<u8>, b: &image::Rgba<u8>, alpha: f64) -> image::Rgba<u8> {
            image::Rgba([a.0[0], b.0[0], alpha as u8, 255])
        }
    }

    #[cfg(feature = "image-crate")]
    impl Compositor<image::Rgb<u8>> for Perf {
        #[allow(unused_variables)]
        fn composite(&self, a: &image::Rgb<u8>, b: &image::Rgb<u8>, alpha: f64) -> image::Rgb<u8> {
            image::Rgb([a.0[0], b.0[0], alpha as u8])
        }
    }

    impl Compositor<Rgba> for Perf {
        #[allow(unused_variables)]
        fn composite(&self, a: &Rgba, b: &Rgba, alpha: f64) -> Rgba {
            Rgba([a.0[0], b.0[0], alpha as f32, 1.0])
        }
    }
}