Skip to main content

spatialrust_vision/
pixel.rs

1/// Scalar component supported by generic CPU image kernels.
2pub trait PixelComponent: Copy + Send + Sync + 'static {
3    /// Converts a scalar into the kernel accumulator representation.
4    fn to_f64(self) -> f64;
5    /// Converts a finite accumulator value back into the scalar dtype.
6    fn from_f64(value: f64) -> Self;
7}
8
9impl PixelComponent for u8 {
10    fn to_f64(self) -> f64 {
11        f64::from(self)
12    }
13
14    fn from_f64(value: f64) -> Self {
15        value.round().clamp(0.0, 255.0) as Self
16    }
17}
18
19impl PixelComponent for u16 {
20    fn to_f64(self) -> f64 {
21        f64::from(self)
22    }
23
24    fn from_f64(value: f64) -> Self {
25        value.round().clamp(0.0, f64::from(u16::MAX)) as Self
26    }
27}
28
29impl PixelComponent for f32 {
30    fn to_f64(self) -> f64 {
31        f64::from(self)
32    }
33
34    fn from_f64(value: f64) -> Self {
35        value as Self
36    }
37}
38
39impl PixelComponent for f64 {
40    fn to_f64(self) -> f64 {
41        self
42    }
43
44    fn from_f64(value: f64) -> Self {
45        value
46    }
47}