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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
mod cie;
mod gray;
mod rgb;
mod alpha;

use std::ops::{Index, IndexMut};
use std::mem;

use traits::Color;
use traits::{Primitive, ColorMathOps};

pub use self::alpha::{Alpha2, Alpha3, Alpha4};

macro_rules! define_color_model {
    {$(
        $ident: ident,
        $alpha_ident: ident,
        $channels: expr,
        $alphas: expr,
        $interpretation: expr,
        #[$doc:meta];
    )*} => {
$( // START Structure definitions

#[$doc]
#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash)]
#[repr(C)]
#[allow(missing_docs)]
pub struct $ident<T: Primitive>([T; $channels]);

impl<T: Primitive> $ident<T> {
    pub fn new(array: [T; $channels]) -> Self {
        $ident(array)
    }
}

impl<T: Primitive> AsRef<[T; $channels]> for $ident<T> {
    fn as_ref(&self) -> &<Self as Color>::Storage {
        &self.0
    }
}

impl<T: Primitive> AsMut<[T; $channels]> for $ident<T> {
    fn as_mut(&mut self) -> &mut <Self as Color>::Storage {
        &mut self.0
    }
}

impl<T: Primitive> Color for $ident<T> {

    type Subpixel = T;
    type Storage = [T; $channels];

    /// Returns the number of channels of this pixel type.
    fn channel_count() -> usize {
        $channels
    }

    #[inline(always)]
    fn channels(&self) -> &[T; $channels] {
        &self.0
    }

    #[inline(always)]
    fn channels_mut(&mut self) -> &mut [T; $channels] {
        &mut self.0
    }

    fn from_channels(other: [T; $channels]) -> $ident<T> {
        *<$ident<T> as Color>::from_slice(&other[..$channels])
    }

    fn from_slice<'a>(slice: &'a [T]) -> &'a $ident<T> {
        unsafe {
            assert_eq!(slice.len(), $channels);
            mem::transmute(slice.as_ptr())
        }
    }

    fn from_slice_mut<'a>(slice: &'a mut [T]) -> &'a mut $ident<T> {
        unsafe {
            assert_eq!(slice.len(), $channels);
            mem::transmute(slice.as_ptr())
        }
    }

    fn apply_with_alpha<F, G>(&mut self, f: F, g: G) where F: Fn(T) -> T, G: Fn(T) -> T {
        for v in self.0[..$channels as usize-$alphas as usize].iter_mut() {
            *v = f(*v)
        }
        if $alphas as usize != 0 {
            let v = &mut self.0[$channels as usize-$alphas as usize-1];
            *v = g(*v)
        }
    }

    fn color_model() -> &'static str {
        $interpretation
    }
}

impl<T: Primitive> Index<usize> for $ident<T> {
    type Output = T;
    #[inline(always)]
    fn index<'a>(&'a self, _index: usize) -> &'a T {
        &self.0[_index]
    }
}

impl<T: Primitive> IndexMut<usize> for $ident<T> {
    #[inline(always)]
    fn index_mut<'a>(&'a mut self, _index: usize) -> &'a mut T {
        &mut self.0[_index]
    }
}

impl<T: Primitive> ColorMathOps<$ident<T>> for $ident<T> {
    #[inline(always)]
    fn add(mut self, rhs: Self) -> Self {
        for i in 0..$channels {
            self.0[i] = self.0[i] + rhs.0[i]
        }
        self
    }

    #[inline(always)]
    fn sub(mut self, rhs: Self) -> Self {
        for i in 0..$channels {
            self.0[i] = self.0[i] - rhs.0[i]
        }
        self
    }

    #[inline(always)]
    fn div(mut self, rhs: Self) -> Self {
        for i in 0..$channels {
            self.0[i] = self.0[i] / rhs.0[i]
        }
        self
    }

    #[inline(always)]
    fn mul(mut self, rhs: Self) -> Self {
        for i in 0..$channels {
            self.0[i] = self.0[i] * rhs.0[i]
        }
        self
    }
}

impl<T: Primitive, V: ColorMathOps<$ident<T>>> ::std::ops::Add<V> for $ident<T> {
    type Output = Self;
    #[inline]
    fn add(self, rhs: V) -> Self::Output {
        rhs.add(self)
    }
}

impl<T: Primitive> ::std::ops::AddAssign for $ident<T> {
    #[inline]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl<T: Primitive, V: ColorMathOps<$ident<T>>> ::std::ops::Sub<V> for $ident<T> {
    type Output = Self;
    #[inline]
    fn sub(self, rhs: V) -> Self::Output {
        rhs.sub(self)
    }
}

impl<T: Primitive> ::std::ops::SubAssign for $ident<T> {
    #[inline]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl<T: Primitive, V: ColorMathOps<$ident<T>>> ::std::ops::Div<V> for $ident<T> {
    type Output = Self;
    #[inline]
    fn div(self, rhs: V) -> Self::Output {
        rhs.div(self)
    }
}

impl<T: Primitive> ::std::ops::DivAssign for $ident<T> {
    #[inline]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

impl<T: Primitive, V: ColorMathOps<$ident<T>>> ::std::ops::Mul<V> for $ident<T> {
    type Output = Self;
    #[inline]
    fn mul(self, rhs: V) -> Self::Output {
        rhs.mul(self)
    }
}

impl<T: Primitive> ::std::ops::MulAssign for $ident<T> {
    #[inline]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl<T: Primitive> From<$alpha_ident<$ident<T>>> for $ident<T> {
    fn from(other: $alpha_ident<$ident<T>>) -> Self {
        *Color::from_slice(&other.as_ref().as_ref()[..$channels])
    }
}

)* // END Structure definitions

/// An enumeration over supported color types and their bit depths.
#[derive(Copy, PartialEq, Eq, Debug, Clone)]
pub enum ColorType {
    $(#[$doc]$ident(u8),)*
}

impl ColorType {
    /// Returns the number of bits contained in a pixel of ColorType `self`.
    pub fn bits_per_pixel(self) -> usize {
        match self {
            $(ColorType::$ident(n) => $channels * n as usize,)*
        }
    }

    /// Returns the number of color channels that are in a pixel of ColorType `self`.
    pub fn num_components(self) -> usize {
        match self {
            $(ColorType::$ident(_) => $channels,)*
        }
    }
}

    }
}

define_color_model! {
    Rgb, Alpha4, 3, 0, "RGB", #[doc = "sRGB."];
    Xyz, Alpha4, 3, 0, "XYZ", #[doc = "CIE XYZ."];
    Lab, Alpha4, 3, 0, "CIE Lab", #[doc = "CIE L*a*b*."];
    Gray, Alpha2, 1, 0, "Y", #[doc = "Grayscale"];
    Indexed, Alpha2, 1, 0, "Idx", #[doc = "Indexed colors.\n\nNo specific color moddel is assumed."];
}

pub type Rgba<T> = Alpha4<Rgb<T>>;
pub type Xyza<T> = Alpha4<Xyz<T>>;
pub type LabA<T> = Alpha4<Lab<T>>;
pub type GrayA<T> = Alpha2<Gray<T>>;

#[test]
fn test_add() {
    let a: Alpha4<Rgb<u8>> = Alpha4::new([0, 0, 0, 0]);
    let b = a + 1;
    assert_eq!(&[1, 1, 1, 1], b.as_ref());
    assert_eq!(&[2, 2, 2, 2], (b + b).as_ref());
}