Skip to content

Commit

Permalink
Simplify Pixel trait
Browse files Browse the repository at this point in the history
Signed-off-by: Christopher N. Hesse <[email protected]>
  • Loading branch information
raymanfx committed Oct 29, 2023
1 parent 24652fb commit 8577bbd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 55 deletions.
10 changes: 2 additions & 8 deletions ffimage-yuv/src/yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@ impl<T, const Y: usize, const U: usize, const V: usize> DerefMut for Yuv<T, Y, U
}

impl<T, const Y: usize, const U: usize, const V: usize> Pixel for Yuv<T, Y, U, V> {
fn channels() -> u8 {
3
}

fn subpixels() -> u8 {
1
}
const CHANNELS: u8 = 3;
}

impl<
Expand Down Expand Up @@ -109,6 +103,6 @@ mod tests {

#[test]
fn channels() {
assert_eq!(Yuv::<u8>::channels(), 3);
assert_eq!(Yuv::<u8>::CHANNELS, 3);
}
}
11 changes: 3 additions & 8 deletions ffimage-yuv/src/yuv422.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize> DerefM
impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize> Pixel
for Yuv422<T, Y0, Y1, U, V>
{
fn channels() -> u8 {
4
}

fn subpixels() -> u8 {
2
}
const CHANNELS: u8 = 4;
const SUBPIXELS: u8 = 2;
}

impl<T, const Y0: usize, const Y1: usize, const U: usize, const V: usize>
Expand Down Expand Up @@ -89,6 +84,6 @@ mod tests {

#[test]
fn channels() {
assert_eq!(Yuv422::<u8, 0, 2, 1, 3>::channels(), 4);
assert_eq!(Yuv422::<u8, 0, 2, 1, 3>::CHANNELS, 4);
}
}
10 changes: 2 additions & 8 deletions ffimage/src/color/gray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ impl<T> DerefMut for Gray<T> {
}

impl<T> Pixel for Gray<T> {
fn channels() -> u8 {
1
}

fn subpixels() -> u8 {
1
}
const CHANNELS: u8 = 1;
}

impl<T, U, const R: usize, const G: usize, const B: usize> From<Rgb<U, R, G, B>> for Gray<T>
Expand Down Expand Up @@ -68,7 +62,7 @@ mod tests {

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

#[test]
Expand Down
44 changes: 16 additions & 28 deletions ffimage/src/color/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use crate::Pixel;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Rgb<T, const R: usize = 0, const G: usize = 1, const B: usize = 2>(pub [T; 3]);

/// BGR pixel
pub type Bgr<T> = Rgb<T, 2, 1, 0>;

impl<T, const R: usize, const G: usize, const B: usize> From<[T; 3]> for Rgb<T, R, G, B> {
fn from(value: [T; 3]) -> Self {
Rgb(value)
}
}

/// BGR pixel
pub type Bgr<T> = Rgb<T, 2, 1, 0>;

impl<T, const R: usize, const G: usize, const B: usize> Deref for Rgb<T, R, G, B> {
type Target = [T; 3];

Expand All @@ -32,13 +32,7 @@ impl<T, const R: usize, const G: usize, const B: usize> DerefMut for Rgb<T, R, G
}

impl<T, const R: usize, const G: usize, const B: usize> Pixel for Rgb<T, R, G, B> {
fn channels() -> u8 {
3
}

fn subpixels() -> u8 {
1
}
const CHANNELS: u8 = 3;
}

impl<T, U> From<Rgb<U, 2, 1, 0>> for Rgb<T, 0, 1, 2>
Expand All @@ -61,6 +55,15 @@ where
}
}

/// RGB pixel with alpha channel
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Rgba<T, const R: usize = 0, const G: usize = 1, const B: usize = 2, const A: usize = 3>(
pub [T; 4],
);

/// BGR pixel with alpha channel
pub type Bgra<T> = Rgba<T, 2, 1, 0, 3>;

impl<
T,
U,
Expand All @@ -85,15 +88,6 @@ where
}
}

/// RGB pixel with alpha channel
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Rgba<T, const R: usize = 0, const G: usize = 1, const B: usize = 2, const A: usize = 3>(
pub [T; 4],
);

/// BGR pixel with alpha channel
pub type Bgra<T> = Rgba<T, 2, 1, 0, 3>;

impl<T, const R: usize, const G: usize, const B: usize, const A: usize> Deref
for Rgba<T, R, G, B, A>
{
Expand All @@ -115,13 +109,7 @@ impl<T, const R: usize, const G: usize, const B: usize, const A: usize> DerefMut
impl<T, const R: usize, const G: usize, const B: usize, const A: usize> Pixel
for Rgba<T, R, G, B, A>
{
fn channels() -> u8 {
4
}

fn subpixels() -> u8 {
1
}
const CHANNELS: u8 = 4;
}

impl<T, U> From<Rgba<U, 2, 1, 0, 3>> for Rgba<T, 0, 1, 2, 3>
Expand Down Expand Up @@ -185,8 +173,8 @@ mod tests {

#[test]
fn channels() {
assert_eq!(Rgb::<u8>::channels(), 3);
assert_eq!(Rgba::<u8>::channels(), 4);
assert_eq!(Rgb::<u8>::CHANNELS, 3);
assert_eq!(Rgba::<u8>::CHANNELS, 4);
}

#[test]
Expand Down
5 changes: 2 additions & 3 deletions ffimage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 8577bbd

Please sign in to comment.