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
use crate::*;

/// 4-channel Pixel type
pub use sys::Pixel;

impl Default for Pixel {
    fn default() -> Pixel {
        Pixel::new()
    }
}

impl std::ops::Add for Pixel {
    type Output = Pixel;
    fn add(self, mut other: Pixel) -> Pixel {
        unsafe {
            sys::pixelAdd(&self, &mut other);
        }
        other
    }
}

impl std::ops::Sub for Pixel {
    type Output = Pixel;
    fn sub(self, mut other: Pixel) -> Pixel {
        unsafe {
            sys::pixelSub(&self, &mut other);
        }
        other
    }
}

impl std::ops::Mul for Pixel {
    type Output = Pixel;
    fn mul(self, mut other: Pixel) -> Pixel {
        unsafe {
            sys::pixelMul(&self, &mut other);
        }
        other
    }
}

impl std::ops::Div for Pixel {
    type Output = Pixel;
    fn div(self, mut other: Pixel) -> Pixel {
        unsafe {
            sys::pixelDiv(&self, &mut other);
        }
        other
    }
}

impl Pixel {
    /// Empty pixel
    pub fn new() -> Pixel {
        unsafe { sys::pixelEmpty() }
    }

    /// RGB pixel, 3 channels
    pub fn rgb(r: f32, g: f32, b: f32) -> Pixel {
        unsafe { sys::pixelRGB(r, g, b) }
    }

    /// RGBA pixel, 4 channels
    pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Pixel {
        unsafe { sys::pixelRGBA(r, g, b, a) }
    }

    /// Gray pixel, 3 channels
    pub fn gray(x: f32) -> Pixel {
        unsafe { sys::pixelGray(x) }
    }

    /// Get pixel data
    pub fn data(&self) -> [f32; 4] {
        self.data
    }

    /// Get a mutable reference to the pixel data
    pub fn data_mut(&mut self) -> &mut [f32] {
        &mut self.data
    }
}