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
use std::mem::{size_of, transmute, transmute_copy};
use {Scalar, ScalarVal, Pixel, PixelArithmetic, PixelVal};

/// TODO: Struct documentation
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Gray<BaseTypeP>
    where BaseTypeP: Scalar
{
    intensity: BaseTypeP,
}

/// TODO: Impl documentation
impl<BaseTypeP> Pixel for Gray<BaseTypeP>
    where BaseTypeP: Scalar
{
    fn calc_minimum_pitch(width: u32, _height: u32) -> usize {
        (width as usize) * size_of::<BaseTypeP>()
    }

    fn calc_size_in_bytes(width: u32, height: u32, pitch: u32) -> Option<usize> {
        if pitch as usize >= Self::calc_minimum_pitch(width, height) {
            Some((height as usize) * (pitch as usize))
        } else {
            None
        }
    }

    fn load_from_raw_buffer(x: u32, y: u32, pitch: u32, buffer: &[u8]) -> Self {
        let start = (y * pitch) as usize + x as usize * size_of::<BaseTypeP>();
        let end = start + size_of::<BaseTypeP>();
        assert!(end <= buffer.len());
        Gray { intensity: unsafe { transmute_copy(&buffer[start]) } }
    }

    fn write_into_raw_buffer(&self, x: u32, y: u32, pitch: u32, buffer: &mut [u8]) {
        let start = (y * pitch) as usize + x as usize * size_of::<BaseTypeP>();
        let end = start + size_of::<BaseTypeP>();

        assert!(end <= buffer.len());
        let intensity: &mut BaseTypeP = unsafe { transmute(&mut buffer[start]) };
        *intensity = self.intensity;
    }
}

/// TODO: Impl documentation
impl<BaseTypeP> PixelArithmetic for Gray<BaseTypeP>
    where BaseTypeP: Scalar
{
    type ScalarT = BaseTypeP;

    fn add_px_px(self, rhs: Self) -> Self {
        Gray { intensity: self.intensity + rhs.intensity }
    }
    fn sub_px_px(self, rhs: Self) -> Self {
        Gray { intensity: self.intensity - rhs.intensity }
    }
    fn mul_px_px(self, rhs: Self) -> Self {
        Gray { intensity: self.intensity * rhs.intensity }
    }
    fn div_px_px(self, rhs: Self) -> Self {
        Gray { intensity: self.intensity / rhs.intensity }
    }

    fn add_px_sc(self, rhs: Self::ScalarT) -> Self {
        Gray { intensity: self.intensity + rhs }
    }
    fn sub_px_sc(self, rhs: Self::ScalarT) -> Self {
        Gray { intensity: self.intensity - rhs }
    }
    fn mul_px_sc(self, rhs: Self::ScalarT) -> Self {
        Gray { intensity: self.intensity * rhs }
    }
    fn div_px_sc(self, rhs: Self::ScalarT) -> Self {
        Gray { intensity: self.intensity / rhs }
    }

    fn add_sc_px(self, lhs: Self::ScalarT) -> Self {
        Gray { intensity: lhs + self.intensity }
    }
    fn sub_sc_px(self, lhs: Self::ScalarT) -> Self {
        Gray { intensity: lhs - self.intensity }
    }
    fn mul_sc_px(self, lhs: Self::ScalarT) -> Self {
        Gray { intensity: lhs * self.intensity }
    }
    fn div_sc_px(self, lhs: Self::ScalarT) -> Self {
        Gray { intensity: lhs / self.intensity }
    }
}

// implement gray specific stuff
pub type GrayVal<BaseTypeP> = PixelVal<Gray<BaseTypeP>>;

impl<BaseTypeP> GrayVal<BaseTypeP>
    where BaseTypeP: Scalar
{
    pub fn new(intensity: ScalarVal<BaseTypeP>) -> GrayVal<BaseTypeP> {
        PixelVal(Gray { intensity: intensity.0 })
    }

    pub fn intensity(&self) -> ScalarVal<BaseTypeP> {
        ScalarVal(self.0.intensity)
    }
}

// abbreviations
pub type Gray8U = Gray<u8>;
pub type Gray16U = Gray<u16>;
pub type Gray32U = Gray<u32>;
pub type Gray32F = Gray<f32>;
pub type Gray64F = Gray<f64>;

pub type GrayVal8U = GrayVal<u8>;
pub type GrayVal16U = GrayVal<u16>;
pub type GrayVal32U = GrayVal<u32>;
pub type GrayVal32F = GrayVal<f32>;
pub type GrayVal64F = GrayVal<f64>;