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
// Copyright (c) 2017, Marty Mills <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 std::ops::{Add, Sub};

use dnum::Zero;
use dvec::{Vec2, Rect2};

use bounds::{Boundless, Bounds, IsBoundless, Size2};
use vec_image::VecImage2;

/// Raster image trait.
pub trait Image {
    type Bounds : Bounds<Index = Self::Index>;
    type Index : Copy;
    type Pixel;

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

/// Image extensions.
pub trait ImageExt : Image {
    fn cloned (self) -> Cloned<Self>
        where Self: Sized
    {
        Cloned { parent: self }
    }

    fn map<U, F> (self, f: F) -> Map<Self, U, F>
        where Self: Sized,
              F: Fn(Self::Pixel) -> U
    {
        Map {
            _phantom: PhantomData,
            parent: self,
            f,
        }
    }

    fn render_vec2<T> (&self) -> VecImage2<T>
        where VecImage2<T>: RenderFrom<Self>
    {
        VecImage2::render_from(self)
    }

    fn sub2<S> (self, rect: Rect2<S>) -> SubImage2<S, Self>
        where Self: Sized + Image<Bounds = Size2<S>, Index = Vec2<S>>,
              S: Copy + Ord + Zero + Sub<Output = S>
    {
        let src_rect = Rect2(Vec2::zero(), self.bounds().as_vec());
        assert!(src_rect.intersect(rect) == rect);

        SubImage2 {
            parent: self,
            rect: rect,
            size: Size2::from(rect.1 - rect.0),
        }
    }

    fn transform<F, I> (self, f: F) -> Transform<Self, F, I>
        where Self: Sized
    {
        Transform {
            _phantom: PhantomData,
            parent: self,
            f
        }
    }

    fn with_size2<S, I> (self, size: S) -> Bounded<Self, Size2<I>>
        where Self: Sized,
              S: Into<Size2<I>>
    {
        Bounded { parent: self, bounds: size.into() }
    }
}

impl<T: Image> ImageExt for T {}

/// Renders an image from a source.
pub trait RenderFrom<T: ?Sized> {
    fn render_from (src: &T) -> Self;
}

/// Imposes boundaries on a boundless image.
pub struct Bounded<P, C> {
    parent: P,
    bounds: C,
}

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

    fn bounds (&self) -> &C { &self.bounds }
    fn pixel (&self, index: I) -> T { self.parent.pixel(index) }
}

/// Clones pixels from references.
pub struct Cloned<P> {
    parent: P,
}

impl<'a, P, B, T, I> Image for Cloned<P>
    where P: Image<Bounds = B, Index = I, Pixel = &'a T>,
          B: Bounds<Index = I>,
          T: Clone + 'a,
          I: Copy
{
    type Bounds = B;
    type Index = I;
    type Pixel = T;

    fn bounds (&self) -> &B { self.parent.bounds() }
    fn pixel (&self, index: I) -> T { self.parent.pixel(index).clone() }
}

/// Unbounded image which returns the same value at any index.
pub struct Fill<T, I> {
    bounds: Boundless<I>,
    pixel: T,
}

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

    fn bounds (&self) -> &Boundless<I> { &self.bounds }
    fn pixel (&self, _: I) -> T { self.pixel }
}

/// Maps pixel values.
pub struct Map<P, U, F> {
    _phantom: PhantomData<U>,
    parent: P,
    f: F,
}

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

    fn bounds (&self) -> &P::Bounds { self.parent.bounds() }

    fn pixel (&self, index: P::Index) -> U {
        (self.f)(self.parent.pixel(index))
    }
}

/// Sub image within another 2-dimensional image.
pub struct SubImage2<S, P> {
    parent: P,
    rect: Rect2<S>,
    size: Size2<S>,
}

impl<S, P> Image for SubImage2<S, P>
    where P: Image<Bounds = Size2<S>, Index = Vec2<S>>,
          S: Copy + Ord + Zero,
          Vec2<S>: Add<Output = Vec2<S>>
{
    type Bounds = Size2<S>;
    type Index = Vec2<S>;
    type Pixel = P::Pixel;

    fn bounds (&self) -> &Size2<S> { &self.size }

    fn pixel (&self, pt: Vec2<S>) -> P::Pixel {
        let zero = S::zero();
        assert!(pt.x >= zero && pt.x < self.size.width && pt.y >= zero && pt.y < self.size.height);
        self.parent.pixel(self.rect.0 + pt)
    }
}

/// Transforms pixel indices.
pub struct Transform<P, F, I> {
    _phantom: PhantomData<I>,
    parent: P,
    f: F,
}

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

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

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

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

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

impl<P, F, I, J, B> IsBoundless for Transform<P, F, I>
    where P: Image<Bounds = B, Index = J>,
          F: Fn(I) -> J,
          I: Copy,
          J: Copy,
          B: Bounds<Index = J> + IsBoundless
{
}

/// Constructs a `Fill`.
pub fn fill<T, I: Copy> (pixel: T) -> Fill<T, I> {
    Fill { bounds: Boundless::new(), pixel }
}