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
use std::ops::{Index, IndexMut};
use num_traits::{Bounded, Num, NumCast};

/// A generalized pixel.
///
/// A pixel object is usually not used standalone but as a view into an image buffer.
pub trait Color
    : Copy + Clone + AsRef<<Self as Color>::Storage> + AsMut<<Self as Color>::Storage> + 'static
    {
    /// The underlying subpixel type.

    type Subpixel: Primitive;
    // TODO: Workaround until associated consts work.
    type Storage: AsRef<[Self::Subpixel]> + AsMut<[Self::Subpixel]> + 'static;
    // TODO: The preferred solution would be:
    // type Subpixel: Primitive;
    // const NUM_CHANNELS: usize;

    /// Returns the number of channels of this pixel type.
    // TODO: Remove is NUM_CHANNELS is available
    fn channel_count() -> usize;

    /// Returns the components as a slice.
    fn channels(&self) -> &Self::Storage;
    // TODO: The preferred solution would be:
    // fn channels(&self) -> &[Self::Subpixel; Self::NUM_CHANNELS];

    /// Returns the components as a mutable slice
    fn channels_mut(&mut self) -> &mut Self::Storage;
    // TODO: The preferred solution would be:
    // fn channels_mut(&mut self) -> &mut [Self::Subpixel; Self::NUM_CHANNELS];

    /// Construct a pixel from the 4 channels a, b, c and d.
    /// If the pixel does not contain 4 channels the extra are ignored.
    fn from_channels(Self::Storage) -> Self;
    // TODO: The preferred solution would be:
    // fn from_channels([Self::Subpixel; Self::NUM_CHANNELS]) -> Self;

    /// Returns a string that can help to interprete the meaning each channel
    /// See [gimp babl](http://gegl.org/babl/).
    fn color_model() -> &'static str;

    /// Returns a view into a slice.
    ///
    /// # Panics
    ///
    /// If the slice it not long enough this method will panic.
    fn from_slice<'a>(slice: &'a [Self::Subpixel]) -> &'a Self;

    /// Returns mutable view into a mutable slice.
    ///
    /// # Panics
    ///
    /// If the slice it not long enough this method will panic.
    fn from_slice_mut<'a>(slice: &'a mut [Self::Subpixel]) -> &'a mut Self;

    /// Apply the function ```f``` to each channel of this pixel.
    fn map<F>(&self, f: F) -> Self
        where F: Fn(Self::Subpixel) -> Self::Subpixel
    {
        let mut this = (*self).clone();
        this.apply(f);
        this
    }

    /// Apply the function ```f``` to each channel of this pixel.
    fn apply<F>(&mut self, f: F)
        where F: Fn(Self::Subpixel) -> Self::Subpixel
    {
        for v in self.as_mut().as_mut().iter_mut() {
            *v = f(*v)
        }
    }

    /// Apply the function ```f``` to each channel except the alpha channel.
    /// Apply the function ```g``` to the alpha channel.
    fn map_with_alpha<F, G>(&self, f: F, g: G) -> Self
        where F: Fn(Self::Subpixel) -> Self::Subpixel,
              G: Fn(Self::Subpixel) -> Self::Subpixel
    {
        let mut this = (*self).clone();
        this.apply_with_alpha(f, g);
        this
    }

    /// Apply the function ```f``` to each channel except the alpha channel.
    /// Apply the function ```g``` to the alpha channel. Works in-place.
    fn apply_with_alpha<F, G>(&mut self, f: F, g: G)
        where F: Fn(Self::Subpixel) -> Self::Subpixel,
              G: Fn(Self::Subpixel) -> Self::Subpixel;

    /// Apply the function ```f``` to each channel of this pixel and
    /// ```other``` pairwise.
    fn map2<F>(&self, other: &Self, f: F) -> Self
        where F: Fn(Self::Subpixel, Self::Subpixel) -> Self::Subpixel
    {
        let mut this = (*self).clone();
        this.apply2(other, f);
        this
    }
    /// Apply the function ```f``` to each channel of this pixel and
    /// ```other``` pairwise. Works in-place.
    fn apply2<F>(&mut self, other: &Self, f: F)
        where F: Fn(Self::Subpixel, Self::Subpixel) -> Self::Subpixel
    {
        for (a, &b) in self.as_mut().as_mut().iter_mut().zip(other.as_ref().as_ref().iter()) {
            *a = f(*a, b)
        }

    }
}

/// Color math operations.
///
/// Math operations on a color. Uses double dispatch to avoid type problems due to conflicting
/// implementations of `Add` and friends.
pub trait ColorMathOps<C: Color>: Sized {
    #[inline(always)]
    fn add(self, rhs: C) -> C;
    #[inline(always)]
    fn sub(self, rhs: C) -> C;
    #[inline(always)]
    fn div(self, rhs: C) -> C;
    #[inline(always)]
    fn mul(self, rhs: C) -> C;
}

/// A view into an image
pub trait ImageView<P: Color>
    : Index<(u32, u32), Output = P> + IndexMut<(u32, u32)> {
}

/// Returns value which is used to scale a value of a channel.
///
/// Returns `T::max_value()` for unsigned integers and `1.0` for floats.
pub trait ChannelMax {
    fn channel_max() -> Self;
}

impl ChannelMax for usize {
    fn channel_max() -> Self {
        usize::max_value()
    }
}
impl ChannelMax for u8 {
    fn channel_max() -> Self {
        u8::max_value()
    }
}
impl ChannelMax for u16 {
    fn channel_max() -> Self {
        u16::max_value()
    }
}
impl ChannelMax for u32 {
    fn channel_max() -> Self {
        u32::max_value()
    }
}
impl ChannelMax for u64 {
    fn channel_max() -> Self {
        u64::max_value()
    }
}
impl ChannelMax for f32 {
    fn channel_max() -> Self {
        1.0
    }
}
impl ChannelMax for f64 {
    fn channel_max() -> Self {
        1.0
    }
}

/// `Primitive` trait from old stdlib.
pub trait Primitive
    : Copy + Clone + NumCast + Num + PartialOrd<Self> + Bounded + 'static {
}

macro_rules! primitive_impls {
    {$(
        $ident: ident,
    )*} => {
$( // START Implementations

impl Primitive for $ident {}

impl<C: Color<Subpixel=$ident>> ColorMathOps<C> for $ident
    where C::Storage: AsRef<[$ident]> + AsMut<[$ident]>
{
    #[inline(always)]
    fn add(self, mut rhs: C) -> C {
        for val in rhs.as_mut().as_mut() {
            *val = *val + self
        }
        rhs
    }
    #[inline(always)]
    fn sub(self, mut rhs: C) -> C {
        for val in rhs.as_mut().as_mut() {
            *val = *val - self
        }
        rhs
    }
    #[inline(always)]
    fn div(self, mut rhs: C) -> C {
        for val in rhs.as_mut().as_mut() {
            *val = *val / self
        }
        rhs
    }
    #[inline(always)]
    fn mul(self, mut rhs: C) -> C {
        for val in rhs.as_mut().as_mut() {
            *val = *val * self
        }
        rhs
    }
}

)* // END Implementations

    }
}

primitive_impls!(
    usize,
    u8,
    u16,
    u32,
    u64,
    isize,
    i8,
    i16,
    i32,
    i64,
    f32,
    f64,
);