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
use std::array;
use std::convert::TryFrom;

use num_traits::{AsPrimitive, FromPrimitive};

use crate::color::bgr::*;
use crate::color::rgb::*;
use crate::core::traits::{Pixel, StorageType};
use crate::{create_pixel, define_pixel, impl_Pixel};

macro_rules! impl_from_rgb_to_gray {
    ($src:ident, $dst:ident, $r:expr, $g:expr, $b:expr) => {
        impl<I: StorageType + AsPrimitive<f32>, O: StorageType + FromPrimitive> From<$src<I>>
            for $dst<O>
        {
            fn from(pix: $src<I>) -> Self {
                // rec601 luma
                let y = O::from_f32(
                    0.2126 * pix[$r].as_() + 0.7152 * pix[$g].as_() + 0.0722 * pix[$b].as_(),
                )
                .unwrap();

                $dst { 0: [y] }
            }
        }
    };
}

create_pixel!(Gray, 1, #[doc = "Grayscale pixel"]);

impl_from_rgb_to_gray!(Rgb, Gray, 0, 1, 2);
impl_from_rgb_to_gray!(Rgba, Gray, 0, 1, 2);
impl_from_rgb_to_gray!(Bgr, Gray, 2, 1, 0);
impl_from_rgb_to_gray!(Bgra, Gray, 2, 1, 0);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn at() {
        let pix: Gray<u8> = Gray { 0: [255; 1] };

        assert_eq!(pix.at(0), 255);
    }

    #[test]
    fn try_from() {
        let mem = vec![255; 1];
        let pix: Gray<u8> = Pixel::try_from(&mem).unwrap();

        assert_eq!(pix.at(0), 255);
    }

    #[test]
    fn channels() {
        assert_eq!(Gray::<u8>::channels(), 1);
    }
}