From 8577bbd70d08e68988f283e68ccff412562b5e4d Mon Sep 17 00:00:00 2001 From: "Christopher N. Hesse" Date: Sun, 5 Feb 2023 20:58:12 +0100 Subject: [PATCH] Simplify Pixel trait Signed-off-by: Christopher N. Hesse --- ffimage-yuv/src/yuv.rs | 10 ++------- ffimage-yuv/src/yuv422.rs | 11 +++------- ffimage/src/color/gray.rs | 10 ++------- ffimage/src/color/rgb.rs | 44 ++++++++++++++------------------------- ffimage/src/lib.rs | 5 ++--- 5 files changed, 25 insertions(+), 55 deletions(-) diff --git a/ffimage-yuv/src/yuv.rs b/ffimage-yuv/src/yuv.rs index 47fc13f..83559f4 100644 --- a/ffimage-yuv/src/yuv.rs +++ b/ffimage-yuv/src/yuv.rs @@ -33,13 +33,7 @@ impl DerefMut for Yuv Pixel for Yuv { - fn channels() -> u8 { - 3 - } - - fn subpixels() -> u8 { - 1 - } + const CHANNELS: u8 = 3; } impl< @@ -109,6 +103,6 @@ mod tests { #[test] fn channels() { - assert_eq!(Yuv::::channels(), 3); + assert_eq!(Yuv::::CHANNELS, 3); } } diff --git a/ffimage-yuv/src/yuv422.rs b/ffimage-yuv/src/yuv422.rs index f03a9b7..a4d7205 100644 --- a/ffimage-yuv/src/yuv422.rs +++ b/ffimage-yuv/src/yuv422.rs @@ -46,13 +46,8 @@ impl DerefM impl Pixel for Yuv422 { - fn channels() -> u8 { - 4 - } - - fn subpixels() -> u8 { - 2 - } + const CHANNELS: u8 = 4; + const SUBPIXELS: u8 = 2; } impl @@ -89,6 +84,6 @@ mod tests { #[test] fn channels() { - assert_eq!(Yuv422::::channels(), 4); + assert_eq!(Yuv422::::CHANNELS, 4); } } diff --git a/ffimage/src/color/gray.rs b/ffimage/src/color/gray.rs index 1d4965b..7f46760 100644 --- a/ffimage/src/color/gray.rs +++ b/ffimage/src/color/gray.rs @@ -29,13 +29,7 @@ impl DerefMut for Gray { } impl Pixel for Gray { - fn channels() -> u8 { - 1 - } - - fn subpixels() -> u8 { - 1 - } + const CHANNELS: u8 = 1; } impl From> for Gray @@ -68,7 +62,7 @@ mod tests { #[test] fn channels() { - assert_eq!(Gray::::channels(), 1); + assert_eq!(Gray::::CHANNELS, 1); } #[test] diff --git a/ffimage/src/color/rgb.rs b/ffimage/src/color/rgb.rs index 44b3942..6bcb7cc 100644 --- a/ffimage/src/color/rgb.rs +++ b/ffimage/src/color/rgb.rs @@ -8,15 +8,15 @@ use crate::Pixel; #[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] pub struct Rgb(pub [T; 3]); +/// BGR pixel +pub type Bgr = Rgb; + impl From<[T; 3]> for Rgb { fn from(value: [T; 3]) -> Self { Rgb(value) } } -/// BGR pixel -pub type Bgr = Rgb; - impl Deref for Rgb { type Target = [T; 3]; @@ -32,13 +32,7 @@ impl DerefMut for Rgb Pixel for Rgb { - fn channels() -> u8 { - 3 - } - - fn subpixels() -> u8 { - 1 - } + const CHANNELS: u8 = 3; } impl From> for Rgb @@ -61,6 +55,15 @@ where } } +/// RGB pixel with alpha channel +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] +pub struct Rgba( + pub [T; 4], +); + +/// BGR pixel with alpha channel +pub type Bgra = Rgba; + impl< T, U, @@ -85,15 +88,6 @@ where } } -/// RGB pixel with alpha channel -#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)] -pub struct Rgba( - pub [T; 4], -); - -/// BGR pixel with alpha channel -pub type Bgra = Rgba; - impl Deref for Rgba { @@ -115,13 +109,7 @@ impl DerefMut impl Pixel for Rgba { - fn channels() -> u8 { - 4 - } - - fn subpixels() -> u8 { - 1 - } + const CHANNELS: u8 = 4; } impl From> for Rgba @@ -185,8 +173,8 @@ mod tests { #[test] fn channels() { - assert_eq!(Rgb::::channels(), 3); - assert_eq!(Rgba::::channels(), 4); + assert_eq!(Rgb::::CHANNELS, 3); + assert_eq!(Rgba::::CHANNELS, 4); } #[test] diff --git a/ffimage/src/lib.rs b/ffimage/src/lib.rs index 0c94fbd..c59fdea 100644 --- a/ffimage/src/lib.rs +++ b/ffimage/src/lib.rs @@ -45,10 +45,9 @@ /// Generic pixel attributes pub trait Pixel { /// Number of channels for this pixel - fn channels() -> u8; - + const CHANNELS: u8; /// Number of image pixels for this pixel - fn subpixels() -> u8; + const SUBPIXELS: u8 = 1; } pub mod color;