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
217
218
219
220
221
// alpha.rs     Alpha channel handling.
//
// Copyright (c) 2019  Douglas P Lau
// Copyright (c) 2019-2020  Jeron Aldaron Lau
//
use crate::{Ch16, Ch32, Ch8, Channel};
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ops::Mul;

/// [Channel](trait.Channel.html) for defining the opacity of pixels.
///
/// It is the inverse of translucency.
pub trait Alpha:
    Copy + Debug + Default + PartialEq + Mul<Output = Self>
{
    /// `Channel` type
    type Chan: Channel;

    /// Get the alpha `Channel` value.
    ///
    /// [Channel::MIN](trait.Channel.html#associatedconstant.MIN) is fully
    /// transparent, and
    /// [Channel::MAX](trait.Channel.html#associatedconstant.MAX) is fully
    /// opaque.
    fn value(&self) -> Self::Chan;
}

/// [Alpha](trait.Alpha.html) `Channel` for fully opaque pixels and
/// [Raster](struct.Raster.html)s.
///
/// Pixel [Format](trait.Format.html)s with `Opaque` alpha channels take less
/// memory than those with [translucent](struct.Translucent.html) ones.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Opaque<C> {
    value: PhantomData<C>,
}

impl<C, H> From<H> for Opaque<C>
where
    C: Channel + From<H>,
    H: Channel,
{
    fn from(_value: H) -> Self {
        Opaque::default()
    }
}
impl<C: Channel> From<Opaque<C>> for Ch8 {
    fn from(_value: Opaque<C>) -> Self {
        Ch8::MAX
    }
}
impl<C: Channel> From<Opaque<C>> for Ch16 {
    fn from(_value: Opaque<C>) -> Self {
        Ch16::MAX
    }
}
impl<C: Channel> From<Opaque<C>> for Ch32 {
    fn from(_value: Opaque<C>) -> Self {
        Ch32::MAX
    }
}

impl<C, A> From<Translucent<A>> for Opaque<C>
where
    C: Channel,
    A: Channel,
{
    /// Convert from a `Translucent` value.
    fn from(_: Translucent<A>) -> Self {
        Opaque::default()
    }
}

impl<C: Channel> Mul<Self> for Opaque<C> {
    type Output = Self;
    fn mul(self, _rhs: Self) -> Self {
        self
    }
}

impl<C: Channel> Alpha for Opaque<C> {
    type Chan = C;

    /// Get the alpha `Channel` value.
    ///
    /// Always returns
    /// [Channel::MAX](trait.Channel.html#associatedconstant.MAX) (fully
    /// opaque).
    fn value(&self) -> C {
        C::MAX
    }
}

/// [Alpha](trait.Alpha.html) channel for translucent or transparent pixels and
/// [Raster](struct.Raster.html)s.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Translucent<C: Channel> {
    value: C,
}

impl<C, H> From<H> for Translucent<C>
where
    C: Channel + From<H>,
    H: Channel,
{
    fn from(value: H) -> Self {
        let value = value.into();
        Translucent { value }
    }
}
impl From<u8> for Translucent<Ch8> {
    fn from(value: u8) -> Self {
        Ch8::new(value).into()
    }
}
impl From<u16> for Translucent<Ch16> {
    fn from(value: u16) -> Self {
        Ch16::new(value).into()
    }
}
impl From<f32> for Translucent<Ch32> {
    fn from(value: f32) -> Self {
        Ch32::new(value).into()
    }
}

impl<C: Channel> Mul<Self> for Translucent<C> {
    type Output = Self;
    fn mul(self, rhs: Self) -> Self {
        let value = self.value * rhs.value;
        Translucent { value }
    }
}

impl<C: Channel> Translucent<C> {
    /// Create a new `Translucent` alpha value.
    pub fn new(value: C) -> Self {
        Translucent { value }
    }
}

impl<C, A> From<Opaque<A>> for Translucent<C>
where
    C: Channel,
    A: Channel,
{
    /// Convert from an `Opaque` value.
    fn from(_: Opaque<A>) -> Self {
        Self::new(C::MAX)
    }
}

impl<C: Channel> Alpha for Translucent<C> {
    type Chan = C;

    /// Get the alpha `Channel` value.
    ///
    /// [Channel::MIN](trait.Channel.html#associatedconstant.MIN) is fully
    /// transparent, and
    /// [Channel::MAX](trait.Channel.html#associatedconstant.MAX) is fully
    /// opaque.
    fn value(&self) -> C {
        self.value
    }
}

/// Each `Channel` is associated, or premultiplied, with alpha
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct AssociatedAlpha;

/// Each `Channel` is separated from alpha (not premultiplied)
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct SeparatedAlpha;

/// Trait for handling associated versus separated alpha
pub trait AlphaMode: Copy + Clone + Debug + PartialEq + Default {
    const ID: AlphaModeID;

    /// Encode one `Channel` using the alpha mode.
    fn encode<C: Channel, A: Alpha<Chan = C>>(c: C, a: A) -> C;
    /// Decode one `Channel` using the alpha mode.
    fn decode<C: Channel, A: Alpha<Chan = C>>(c: C, a: A) -> C;
}

impl AlphaMode for AssociatedAlpha {
    const ID: AlphaModeID = AlphaModeID::Associated;

    /// Encode one `Channel` using the alpha mode.
    fn encode<C: Channel, A: Alpha<Chan = C>>(c: C, a: A) -> C {
        c * a.value()
    }
    /// Decode one `Channel` using the alpha mode.
    fn decode<C: Channel, A: Alpha<Chan = C>>(c: C, a: A) -> C {
        c / a.value()
    }
}

impl AlphaMode for SeparatedAlpha {
    const ID: AlphaModeID = AlphaModeID::Separated;

    /// Encode one `Channel` using the alpha mode.
    fn encode<C: Channel, A: Alpha<Chan = C>>(c: C, _a: A) -> C {
        c
    }
    /// Decode one `Channel` using the alpha mode.
    fn decode<C: Channel, A: Alpha<Chan = C>>(c: C, _a: A) -> C {
        c
    }
}

/// Mode for handling associated alpha.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum AlphaModeID {
    /// Each `Channel` is associated, or premultiplied, with alpha
    Associated,
    /// Each `Channel` is separated from alpha (not premultiplied)
    Separated,
    /// Unknown
    UnknownAlpha,
}