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
// Copyright (c) 2016-2017 <daggerbot@gmail.com>
// This software is available under the terms of the zlib license.
// See COPYING.md for more information.

use std::marker::PhantomData;

use linear::{Vec2, Vec3};

use bounds::{Bounds, IsUnbounded, Size1, Size2, Size3, Unbounded};

/// Raster image trait.
pub trait Image {
    type Bounds : Bounds;
    type Pixel;

    fn bounds (&self) -> &Self::Bounds;
    fn pixel (&self, index: <Self::Bounds as Bounds>::Index) -> Self::Pixel;

    fn bounded<B, I> (self, bounds: B) -> Bounded<Self, B>
        where Self: Sized,
              Self::Bounds: Bounds<Index = I> + IsUnbounded,
              B: Bounds<Index = I>,
              I: Copy
    {
        Bounded {
            src: self,
            bounds: bounds,
        }
    }

    fn in_bounds (&self, index: <Self::Bounds as Bounds>::Index) -> bool {
        self.bounds().in_bounds(index)
    }

    fn map<F, Q> (self, f: F) -> Map<Self, F>
        where Self: Sized, F: Fn(Self::Pixel) -> Q
    {
        Map {
            src: self,
            f: f,
        }
    }

    fn render<T> (&self) -> T where Self: Sized, T: FromImage<Self> { T::from_image(self) }

    fn transform<F, I> (self, f: F) -> Transform<Self, F, I>
        where Self: Sized, F: Fn(I) -> <Self::Bounds as Bounds>::Index, I: Copy
    {
        Transform {
            _phantom: PhantomData,
            src: self,
            f: f,
        }
    }

    fn try_pixel (&self, index: <Self::Bounds as Bounds>::Index) -> Option<Self::Pixel> {
        if self.bounds().in_bounds(index) {
            Some(self.pixel(index))
        } else {
            None
        }
    }

    unsafe fn unsafe_pixel (&self, index: <Self::Bounds as Bounds>::Index) -> Self::Pixel {
        self.pixel(index)
    }

    fn with_size1<T> (self, len: T) -> Bounded<Self, Size1<T>>
        where Self: Sized, Self::Bounds: Bounds<Index = T> + IsUnbounded, T: Copy
    {
        Bounded {
            src: self,
            bounds: Size1(len),
        }
    }

    fn with_size2<T> (self, size: Vec2<T>) -> Bounded<Self, Size2<T>>
        where Self: Sized, Self::Bounds: Bounds<Index = Vec2<T>> + IsUnbounded, T: Copy
    {
        Bounded {
            src: self,
            bounds: Size2(size),
        }
    }

    fn with_size2_parts<T> (self, width: T, height: T) -> Bounded<Self, Size2<T>>
        where Self: Sized, Self::Bounds: Bounds<Index = Vec2<T>> + IsUnbounded, T: Copy
    {
        Bounded {
            src: self,
            bounds: Size2(Vec2(width, height)),
        }
    }

    fn with_size3<T> (self, size: Vec3<T>) -> Bounded<Self, Size3<T>>
        where Self: Sized, Self::Bounds: Bounds<Index = Vec3<T>> + IsUnbounded, T: Copy
    {
        Bounded {
            src: self,
            bounds: Size3(size),
        }
    }

    fn with_size3_parts<T> (self, width: T, height: T, depth: T) -> Bounded<Self, Size3<T>>
        where Self: Sized, Self::Bounds: Bounds<Index = Vec3<T>> + IsUnbounded, T: Copy
    {
        Bounded {
            src: self,
            bounds: Size3(Vec3(width, height, depth)),
        }
    }
}

/// Image which adds bounds to an unbounded source.
pub struct Bounded<T, B> {
    src: T,
    bounds: B,
}

impl<T, B, I> Image for Bounded<T, B>
    where T: Image, T::Bounds: Bounds<Index = I> + IsUnbounded, B: Bounds<Index = I>, I: Copy
{
    type Bounds = B;
    type Pixel = T::Pixel;

    fn bounds (&self) -> &B { &self.bounds }

    fn pixel (&self, index: I) -> T::Pixel {
        self.src.pixel(index)
    }

    fn try_pixel (&self, index: I) -> Option<T::Pixel> {
        if self.in_bounds(index) {
            Some(self.src.pixel(index))
        } else {
            None
        }
    }

    unsafe fn unsafe_pixel (&self, index: I) -> T::Pixel {
        self.src.unsafe_pixel(index)
    }
}

/// Unbounded image with a solid color.
pub struct Fill<I, T> {
    bounds: Unbounded<I>,
    pixel: T,
}

impl<I, T> Image for Fill<I, T> where I: Copy, T: Copy {
    type Bounds = Unbounded<I>;
    type Pixel = T;

    fn bounds (&self) -> &Unbounded<I> { &self.bounds }
    fn pixel (&self, _: I) -> T { self.pixel }
    fn try_pixel (&self, _: I) -> Option<T> { Some(self.pixel) }
    unsafe fn unsafe_pixel (&self, _: I) -> T { self.pixel }
}

/// Constructs an unbounded image with a solid color.
pub fn fill<I, T> (pixel: T) -> Fill<I, T> where I: Copy, T: Copy {
    Fill {
        bounds: Unbounded::new(),
        pixel: pixel,
    }
}

/// Trait for rendering an image buffer from a source image.
pub trait FromImage<T> {
    fn from_image (src: &T) -> Self;
}

/// Image which maps pixels from a source.
pub struct Map<T, F> {
    src: T,
    f: F,
}

impl<T, F, P, Q> Image for Map<T, F>
    where T: Image<Pixel = P>, F: Fn(P) -> Q
{
    type Bounds = T::Bounds;
    type Pixel = Q;

    fn bounds (&self) -> &T::Bounds { self.src.bounds() }

    fn pixel (&self, index: <T::Bounds as Bounds>::Index) -> Q {
        (self.f)(self.src.pixel(index))
    }

    fn try_pixel (&self, index: <T::Bounds as Bounds>::Index) -> Option<Q> {
        self.src.try_pixel(index).map(|p| (self.f)(p))
    }

    unsafe fn unsafe_pixel (&self, index: <T::Bounds as Bounds>::Index) -> Q {
        (self.f)(self.src.unsafe_pixel(index))
    }
}

/// Image which transforms an index before getting pixels from a source.
pub struct Transform<T, F, I> {
    // impl Image for Transform gave an error for unconstrained type param I.
    // Remove this phantom data and the I parameter on the Transform type when we find a way around.
    _phantom: PhantomData<I>,
    src: T,
    f: F,
}

impl<T, F, I> Bounds for Transform<T, F, I>
    where T: Image, F: Fn(I) -> <T::Bounds as Bounds>::Index, I: Copy
{
    type Index = I;

    fn in_bounds (&self, index: I) -> bool {
        self.src.in_bounds((self.f)(index))
    }
}

impl<T, F, I> Image for Transform<T, F, I>
    where T: Image, F: Fn(I) -> <T::Bounds as Bounds>::Index, I: Copy
{
    type Bounds = Transform<T, F, I>;
    type Pixel = T::Pixel;

    fn bounds (&self) -> &Transform<T, F, I> { self }

    fn pixel (&self, index: I) -> T::Pixel {
        self.src.pixel((self.f)(index))
    }
}

// Tests
//

#[test]
fn test_map () {
    assert_eq!(fill(0u8).with_size1(1).map(|p| p + 1).pixel(0), 1u8);
}

#[test]
fn test_transform () {
    let image = fill(0u8).with_size1(1).transform(|n: u8| n.wrapping_sub(1));
    assert_eq!(image.try_pixel(0), None);
    assert_eq!(image.try_pixel(1), Some(0u8));
}