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
use crate::color::Color;
use crate::image::Image;
use crate::ty::Type;

/// Executes `a` then `b` and passes the results to `f`
pub struct Join<'a, A: 'a + Filter, B: Filter, F: Fn(f64, f64) -> f64> {
    a: &'a A,
    b: B,
    f: F,
}

/// Executes `a` then `f(a)`
pub struct AndThen<'a, A: 'a + Filter, F: Fn(f64) -> f64> {
    a: &'a A,
    f: F,
}

impl<'a, A: Filter, F: Sync + Fn(f64) -> f64> Filter for AndThen<'a, A, F> {
    fn compute_at<T: Type, C: Color, I: Image<T, C>>(
        &self,
        x: usize,
        y: usize,
        c: usize,
        input: &[&I],
    ) -> f64 {
        let f = &self.f;
        f(self.a.compute_at(x, y, c, input))
    }
}

impl<'a, A: Filter, B: Filter, F: Sync + Fn(f64, f64) -> f64> Filter for Join<'a, A, B, F> {
    fn compute_at<T: Type, C: Color, I: Image<T, C>>(
        &self,
        x: usize,
        y: usize,
        c: usize,
        input: &[&I],
    ) -> f64 {
        let f = &self.f;
        f(
            self.a.compute_at(x, y, c, input),
            self.b.compute_at(x, y, c, input),
        )
    }
}

/// Filters are used to manipulate images in a generic, composable manner
pub trait Filter: Sized + Sync {
    fn compute_at<T: Type, C: Color, I: Image<T, C>>(
        &self,
        x: usize,
        y: usize,
        c: usize,
        input: &[&I],
    ) -> f64;

    /// Evaluate a filter on part of an image
    fn eval_partial<T: Type, C: Color, U: Type, D: Color, I: Image<T, C>, J: Image<U, D>>(
        &self,
        start_x: usize,
        start_y: usize,
        width: usize,
        height: usize,
        output: &mut I,
        input: &[&J],
    ) {
        for y in start_y..start_y + height {
            for x in start_x..start_x + width {
                let px = output.at_mut(x, y);
                for c in 0..C::channels() {
                    px[c] = T::from_f(self.compute_at(x, y, c, input));
                }
            }
        }
    }

    /// Evaluate filter in parallel
    fn eval<
        T: Send + Type,
        C: Color,
        U: Type,
        D: Color,
        I: Sync + Send + Image<T, C>,
        J: Sync + Image<U, D>,
    >(
        &self,
        output: &mut I,
        input: &[&J],
    ) {
        output.for_each(|(x, y), pixel| {
            for c in 0..C::channels() {
                pixel[c] = T::from_f(self.compute_at(x, y, c, input));
            }
        });
    }

    /// Evaluate filter in parallel in place
    fn eval_in_place<T: Send + Type, C: Color, I: Sync + Send + Image<T, C>>(&self, image: &mut I) {
        let input_image: crate::ImagePtr<T, C> = crate::ImagePtr::new(
            image.width(),
            image.height(),
            image.data_mut().as_mut_ptr(),
            crate::image_ptr::Free::Ignore,
        );

        let input = &[&input_image];
        image.for_each(|(x, y), pixel| {
            for c in 0..C::channels() {
                pixel[c] = T::from_f(self.compute_at(x, y, c, input));
            }
        });
    }

    fn join<A: Filter, F: Fn(f64, f64) -> f64>(&self, other: A, f: F) -> Join<Self, A, F> {
        Join {
            a: self,
            b: other,
            f,
        }
    }

    fn and_then<F: Fn(f64) -> f64>(&self, f: F) -> AndThen<Self, F> {
        AndThen { a: self, f }
    }
}

/// filter is used to simplify the process of defining `compute_at` to create new filters
#[macro_export]
macro_rules! filter {
    ($name:ident, $input:ident, $f:expr) => {
        impl $crate::Filter for $name {
            fn compute_at<T: crate::Type, C: crate::Color, I: crate::Image<T, C>>(
                &self,
                x: usize,
                y: usize,
                c: usize,
                $input: &[&I],
            ) -> f64 {
                $f(self, x, y, c)
            }
        }
    };
    (.$name:ident, $input:ident, $f:expr) => {
        pub struct $name;

        filter!($name, $input, $f);
    };
}

filter!(.Invert, input, |_, x, y, c| T::max_f()
    - input[0].get_f(x, y, c));

filter!(.Blend, input, |_, x, y, c| (input[0].get_f(x, y, c)
    + input[1].get_f(x, y, c))
    / 2.0);

filter!(.ToGrayscale, input, |_, x, y, _c| {
    let a: &I = input[0];
    let v = a.get_f(x, y, 0) * 0.21 + a.get_f(x, y, 1) * 0.72 + a.get_f(x, y, 2) * 0.07;
    if C::has_alpha() {
        return v * a.get_f(x, y, C::channels() - 1);
    }
    v
});

filter!(.ToColor, input, |_, x, y, c| {
    if c == 4 {
        return T::max_f();
    }

    input[0].get_f(x, y, c % C::channels())
});

filter!(.AlphaBlend, input, |_, x, y, c| {
    let a = input[0];

    if c == a.channels() - 1 {
        return 1.0;
    }

    a.get_f(x, y, c) * a.get_f(x, y, a.channels() - 1)
});

pub struct Gamma(pub f64);
filter!(Gamma, input, |this: &Gamma, x, y, c| {
    let a = input[0];

    a.get_f(x, y, c).powf(1.0 / this.0)
});

pub struct Multiply(pub crate::PixelVec<f64>);
filter!(Multiply, input, |this: &Multiply, x, y, c| {
    let a = input[0];

    a.get_f(x, y, c) * this.0.as_ref()[c]
});

pub struct Add(pub crate::PixelVec<f64>);
filter!(Add, input, |this: &Add, x, y, c| {
    let a = input[0];

    a.get_f(x, y, c) + this.0.as_ref()[c]
});

pub struct SwapChannel(pub usize, pub usize);
filter!(
    SwapChannel,
    input,
    |this: &SwapChannel, x, y, c| if c == this.0 {
        input[0].get_f(x, y, this.1)
    } else if c == this.1 {
        input[0].get_f(x, y, this.0)
    } else {
        input[0].get_f(x, y, c)
    }
);