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
use crate::formats;
pub use rgb::alt::Gray;
pub use rgb::RGB;
pub use rgb::RGBA;

/// Use [`Pixel`](crate::Pixel) presets to specify pixel format.
///
/// The trait represents a temporary object that adds pixels together.
pub trait PixelFormat {
    /// Pixel type in the source image
    type InputPixel: Copy;
    /// Pixel type in the destination image (usually the same as Input)
    type OutputPixel;
    /// Temporary struct for the pixel in floating-point
    type Accumulator: Copy;

    /// Create new floating-point pixel
    fn new() -> Self::Accumulator;
    /// Add new pixel with a given weight (first axis)
    fn add(&self, acc: &mut Self::Accumulator, inp: Self::InputPixel, coeff: f32);
    /// Add bunch of accumulated pixels with a weight (second axis)
    fn add_acc(acc: &mut Self::Accumulator, inp: Self::Accumulator, coeff: f32);
    /// Finalize, convert to output pixel format
    fn into_pixel(&self, acc: Self::Accumulator) -> Self::OutputPixel;
}

impl<F: ToFloat, T: ToFloat> PixelFormat for formats::Rgb<T, F> {
    type InputPixel = RGB<F>;
    type OutputPixel = RGB<T>;
    type Accumulator = RGB<f32>;

    #[inline(always)]
    fn new() -> Self::Accumulator {
        RGB::new(0.,0.,0.)
    }

    #[inline(always)]
    fn add(&self, acc: &mut Self::Accumulator, inp: RGB<F>, coeff: f32) {
        acc.r += inp.r.to_float() * coeff;
        acc.g += inp.g.to_float() * coeff;
        acc.b += inp.b.to_float() * coeff;
    }

    #[inline(always)]
    fn add_acc(acc: &mut Self::Accumulator, inp: Self::Accumulator, coeff: f32) {
        acc.r += inp.r * coeff;
        acc.g += inp.g * coeff;
        acc.b += inp.b * coeff;
    }

    #[inline(always)]
    fn into_pixel(&self, acc: Self::Accumulator) -> RGB<T> {
        RGB {
            r: T::from_float(acc.r),
            g: T::from_float(acc.g),
            b: T::from_float(acc.b),
        }
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for formats::Rgba<T, F> {
    type InputPixel = RGBA<F>;
    type OutputPixel = RGBA<T>;
    type Accumulator = RGBA<f32>;

    #[inline(always)]
    fn new() -> Self::Accumulator {
        RGBA::new(0.,0.,0.,0.)
    }

    #[inline(always)]
    fn add(&self, acc: &mut Self::Accumulator, inp: RGBA<F>, coeff: f32) {
        acc.r += inp.r.to_float() * coeff;
        acc.g += inp.g.to_float() * coeff;
        acc.b += inp.b.to_float() * coeff;
        acc.a += inp.a.to_float() * coeff;
    }

    #[inline(always)]
    fn add_acc(acc: &mut Self::Accumulator, inp: Self::Accumulator, coeff: f32) {
        acc.r += inp.r * coeff;
        acc.g += inp.g * coeff;
        acc.b += inp.b * coeff;
        acc.a += inp.a * coeff;
    }

    #[inline(always)]
    fn into_pixel(&self, acc: Self::Accumulator) -> RGBA<T> {
        RGBA {
            r: T::from_float(acc.r),
            g: T::from_float(acc.g),
            b: T::from_float(acc.b),
            a: T::from_float(acc.a),
        }
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for formats::RgbaPremultiply<T, F> {
    type InputPixel = RGBA<F>;
    type OutputPixel = RGBA<T>;
    type Accumulator = RGBA<f32>;

    #[inline(always)]
    fn new() -> Self::Accumulator {
        RGBA::new(0.,0.,0.,0.)
    }

    #[inline(always)]
    fn add(&self, acc: &mut Self::Accumulator, inp: RGBA<F>, coeff: f32) {
        let a_coeff = inp.a.to_float() * coeff;
        acc.r += inp.r.to_float() * a_coeff;
        acc.g += inp.g.to_float() * a_coeff;
        acc.b += inp.b.to_float() * a_coeff;
        acc.a += a_coeff;
    }

    #[inline(always)]
    fn add_acc(acc: &mut Self::Accumulator, inp: Self::Accumulator, coeff: f32) {
        acc.r += inp.r * coeff;
        acc.g += inp.g * coeff;
        acc.b += inp.b * coeff;
        acc.a += inp.a * coeff;
    }

    #[inline(always)]
    fn into_pixel(&self, acc: Self::Accumulator) -> RGBA<T> {
        if acc.a > 0. {
            let inv = 1.0 / acc.a;
            RGBA {
                r: T::from_float(acc.r * inv),
                g: T::from_float(acc.g * inv),
                b: T::from_float(acc.b * inv),
                a: T::from_float(acc.a),
            }
        } else {
            let zero = T::from_float(0.);
            RGBA::new(zero, zero, zero, zero)
        }
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for formats::Gray<F, T> {
    type InputPixel = Gray<F>;
    type OutputPixel = Gray<T>;
    type Accumulator = Gray<f32>;

    #[inline(always)]
    fn new() -> Self::Accumulator {
        Gray::new(0.)
    }

    #[inline(always)]
    fn add(&self, acc: &mut Self::Accumulator, inp: Gray<F>, coeff: f32) {
        acc.0 += inp.0.to_float() * coeff;
    }

    #[inline(always)]
    fn add_acc(acc: &mut Self::Accumulator, inp: Self::Accumulator, coeff: f32) {
        acc.0 += inp.0 * coeff;
    }

    #[inline(always)]
    fn into_pixel(&self, acc: Self::Accumulator) -> Gray<T> {
        Gray::new(T::from_float(acc.0))
    }
}

use self::f::ToFloat;
mod f {
    /// Internal, please don't use
    pub trait ToFloat: Sized + Copy + 'static {
        fn to_float(self) -> f32;
        fn from_float(f: f32) -> Self;
    }

    impl ToFloat for u8 {
        #[inline(always)]
        fn to_float(self) -> f32 {
            self as f32
        }

        #[inline(always)]
        fn from_float(f: f32) -> Self {
            unsafe {
                (0f32).max(f.round()).min(255.).to_int_unchecked()
            }
        }
    }

    impl ToFloat for u16 {
        #[inline(always)]
        fn to_float(self) -> f32 {
            self as f32
        }

        #[inline(always)]
        fn from_float(f: f32) -> Self {
            unsafe {
                (0f32).max(f.round()).min(65535.).to_int_unchecked()
            }
        }
    }

    impl ToFloat for f32 {
        #[inline(always)]
        fn to_float(self) -> f32 {
            self
        }

        #[inline(always)]
        fn from_float(f: f32) -> Self {
            f
        }
    }

    impl ToFloat for f64 {
        #[inline(always)]
        fn to_float(self) -> f32 {
            self as f32
        }

        #[inline(always)]
        fn from_float(f: f32) -> Self {
            f as f64
        }
    }

    // Inherent methods are preferred over traits, so this won't be used in newer rust
    trait OldRustWorkaround<T> {
        unsafe fn to_int_unchecked(self) -> T;
    }

    impl OldRustWorkaround<u16> for f32 {
        unsafe fn to_int_unchecked(self) -> u16 { self as u16 }
    }

    impl OldRustWorkaround<u8> for f32 {
        unsafe fn to_int_unchecked(self) -> u8 { self as u8 }
    }
}

// Inherent methods are preferred over traits, so this won't be used in newer rust
trait OldRustWorkaround<T> {
    unsafe fn to_int_unchecked(self) -> T;
}

impl OldRustWorkaround<u16> for f32 {
    unsafe fn to_int_unchecked(self) -> u16 { self as u16 }
}

impl OldRustWorkaround<u8> for f32 {
    unsafe fn to_int_unchecked(self) -> u8 { self as u8 }
}