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
use crate::Pixel::generic::RgbFormats;
use crate::Pixel::generic::RgbaFormats;
use crate::Pixel::generic::GrayFormats;

use rgb::alt::Gray;
use rgb::*;

/// Glue for backwards compatibility with old version of the crate
#[deprecated(note="Use APIs based on the newer PixelFormat")]
#[doc(hidden)]
pub trait PixelFormatBackCompatShim: PixelFormat {
    type Subpixel: Copy;
    fn input(arr: &[Self::Subpixel]) -> &[Self::InputPixel];
    fn output(arr: &mut [Self::Subpixel]) -> &mut [Self::OutputPixel];
}

/// Use [`Pixel`](Pixel) presets to specify pixel format.
///
/// 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;
}

#[allow(deprecated)]
impl<F: ToFloat> PixelFormatBackCompatShim for RgbFormats<F, F> {
    type Subpixel = F;

    fn input(arr: &[Self::Subpixel]) -> &[Self::InputPixel] {
        arr.as_rgb()
    }
    fn output(arr: &mut [Self::Subpixel]) -> &mut [Self::OutputPixel] {
        arr.as_rgb_mut()
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for RgbFormats<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),
        }
    }
}

#[allow(deprecated)]
impl<F: ToFloat> PixelFormatBackCompatShim for RgbaFormats<F, F> {
    type Subpixel = F;

    fn input(arr: &[Self::Subpixel]) -> &[Self::InputPixel] {
        arr.as_rgba()
    }
    fn output(arr: &mut [Self::Subpixel]) -> &mut [Self::OutputPixel] {
        arr.as_rgba_mut()
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for RgbaFormats<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),
        }
    }
}

#[allow(deprecated)]
impl<F: ToFloat> PixelFormatBackCompatShim for GrayFormats<F, F> {
    type Subpixel = F;

    fn input(arr: &[Self::Subpixel]) -> &[Self::InputPixel] {
        arr.as_gray()
    }
    fn output(arr: &mut [Self::Subpixel]) -> &mut [Self::OutputPixel] {
        arr.as_gray_mut()
    }
}

impl<F: ToFloat, T: ToFloat> PixelFormat for GrayFormats<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))
    }
}

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
    }
}