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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// mask.rs      Alpha mask pixel format.
//
// Copyright (c) 2019-2020  Douglas P Lau
// Copyright (c) 2019-2020  Jeron Aldaron Lau
//
use crate::alpha::{
    Alpha, AlphaMode, AlphaModeID, PremultipliedAlpha, StraightAlpha,
    Translucent,
};
use crate::gamma::{GammaMode, GammaModeID, LinearGamma};
use crate::{Ch16, Ch32, Ch8, Channel, Format, Gray, Rgb};
use std::ops::Mul;

/// [Translucent](struct.Translucent.html) alpha mask pixel
/// [Format](trait.Format.html).
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[repr(C)]
pub struct Mask<A: Alpha> {
    alpha: A,
}

impl<A: Alpha> GammaMode for Mask<A> {
    const ID: GammaModeID = GammaModeID::UnknownGamma;

    /// Encode one `Channel` using the gamma mode.
    fn encode<H: Channel>(h: H) -> H {
        // Gamma Mode is a no-op on Mask
        LinearGamma::encode(h)
    }
    /// Decode one `Channel` using the gamma mode.
    fn decode<H: Channel>(h: H) -> H {
        // Gamma Mode is a no-op on Mask
        LinearGamma::decode(h)
    }
}

impl<A: Alpha> AlphaMode for Mask<A> {
    const ID: AlphaModeID = AlphaModeID::UnknownAlpha;

    /// Encode one `Channel` using the alpha mode.
    fn encode<H: Channel, B: Alpha<Chan = H>>(h: H, b: B) -> H {
        // Alpha Mode is a no-op on Mask
        StraightAlpha::encode::<H, B>(h, b)
    }
    /// Decode one `Channel` using the alpha mode.
    fn decode<H: Channel, B: Alpha<Chan = H>>(h: H, b: B) -> H {
        // Alpha Mode is a no-op on Mask
        StraightAlpha::decode::<H, B>(h, b)
    }
}

impl<A: Alpha> Iterator for Mask<A> {
    type Item = Self;

    fn next(&mut self) -> Option<Self::Item> {
        Some(*self)
    }
}

impl From<u8> for Mask8 {
    /// Get a `Mask` from a `u8`
    fn from(c: u8) -> Self {
        Mask::new(c)
    }
}

impl From<u16> for Mask16 {
    /// Get a `Mask` from a `u16`
    fn from(c: u16) -> Self {
        Mask::new(c)
    }
}

impl From<f32> for Mask32 {
    /// Get a `Mask` from an `f32`
    fn from(c: f32) -> Self {
        Mask::new(c)
    }
}

impl<C, A, G: GammaMode> From<Mask<A>> for Rgb<C, A, StraightAlpha, G>
where
    C: Channel,
    A: Alpha<Chan = C>,
{
    /// Get an `Rgb` from a `Mask`
    fn from(c: Mask<A>) -> Self {
        let red = C::MAX;
        let green = C::MAX;
        let blue = C::MAX;
        let alpha = c.alpha();
        Rgb::with_alpha(red, green, blue, alpha)
    }
}

impl<C, A, G: GammaMode> From<Mask<A>> for Rgb<C, A, PremultipliedAlpha, G>
where
    C: Channel,
    A: Alpha<Chan = C>,
{
    /// Get an `Rgb` from a `Mask`
    fn from(c: Mask<A>) -> Self {
        let red = c.alpha().value();
        let green = c.alpha().value();
        let blue = c.alpha().value();
        let alpha = c.alpha();
        Rgb::with_alpha(red, green, blue, alpha)
    }
}

impl<C, A, G: GammaMode> From<Mask<A>> for Gray<C, A, StraightAlpha, G>
where
    C: Channel,
    A: Alpha<Chan = C>,
{
    /// Get a `Gray` from a `Mask`
    fn from(c: Mask<A>) -> Self {
        let value = C::MAX;
        let alpha = c.alpha();
        Gray::with_alpha(value, alpha)
    }
}

impl<C, A, G: GammaMode> From<Mask<A>> for Gray<C, A, PremultipliedAlpha, G>
where
    C: Channel,
    A: Alpha<Chan = C>,
{
    /// Get a `Gray` from a `Mask`
    fn from(c: Mask<A>) -> Self {
        let value = c.alpha().value();
        let alpha = c.alpha();
        Gray::with_alpha(value, alpha)
    }
}

impl<A: Alpha> Mul<Self> for Mask<A> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        let alpha = self.alpha * rhs.alpha;
        Mask { alpha }
    }
}

impl<A: Alpha> Mask<A> {
    /// Create a new `Mask` value.
    pub fn new<B>(alpha: B) -> Self
    where
        A: From<B>,
    {
        let alpha = A::from(alpha);
        Mask { alpha }
    }
    /// Get the alpha value.
    pub fn alpha(self) -> A {
        self.alpha
    }
}

impl<C, A> Format for Mask<A>
where
    C: Channel,
    A: Alpha<Chan = C> + From<C>,
{
    type Chan = C;

    /// Get *red*, *green*, *blue* and *alpha* `Channel`s
    fn rgba(self) -> [Self::Chan; 4] {
        [C::MAX, C::MAX, C::MAX, self.alpha.value()]
    }

    /// Make a pixel with given RGBA `Channel`s
    fn with_rgba(rgba: [Self::Chan; 4]) -> Self {
        let alpha = rgba[3];
        Mask::new(alpha)
    }

    /// Get channel-wise difference
    fn difference(self, rhs: Self) -> Self {
        let a = if self.alpha.value() > rhs.alpha.value() {
            self.alpha.value() - rhs.alpha.value()
        } else {
            rhs.alpha.value() - self.alpha.value()
        };
        Mask::new(a)
    }

    /// Check if all `Channel`s are within threshold
    fn within_threshold(self, rhs: Self) -> bool {
        self.alpha.value() <= rhs.alpha.value()
    }
}

/// [Translucent](struct.Translucent.html) 8-bit alpha [Mask](struct.Mask.html)
/// pixel [Format](trait.Format.html).
pub type Mask8 = Mask<Translucent<Ch8>>;

/// [Translucent](struct.Translucent.html) 16-bit alpha [Mask](struct.Mask.html)
/// pixel [Format](trait.Format.html).
pub type Mask16 = Mask<Translucent<Ch16>>;

/// [Translucent](struct.Translucent.html) 32-bit alpha [Mask](struct.Mask.html)
/// pixel [Format](trait.Format.html).
pub type Mask32 = Mask<Translucent<Ch32>>;

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn check_sizes() {
        assert_eq!(std::mem::size_of::<Mask8>(), 1);
        assert_eq!(std::mem::size_of::<Mask16>(), 2);
        assert_eq!(std::mem::size_of::<Mask32>(), 4);
    }
}