Skip to content

Commit

Permalink
fn BPC::pxstride: Move fn pxstride to BPC, too, to allow it to …
Browse files Browse the repository at this point in the history
…be used non-generically (#780)

@randomPoison, follow-up to
#777 (comment). I
wanted to share the implementation between `BPC::pxstride` and
`BitDepth::pxstride`.
  • Loading branch information
kkysen authored Mar 4, 2024
2 parents 5711988 + 59ba01a commit 72c5bf5
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use std::ops::Div;
use std::ops::Mul;
use std::ops::Rem;
use std::ops::Shr;
use to_method::To as _;

pub trait FromPrimitive<T> {
fn from_prim(t: T) -> Self;
Expand Down Expand Up @@ -96,11 +95,28 @@ impl BPC {
Self::BPC16
}
}

pub const fn bitdepth(&self) -> u8 {
match self {
Self::BPC8 => 8,
Self::BPC16 => 16,
}
}

/// `T` is generally meant to be `usize` or `isize`.
pub fn pxstride<T>(&self, n: T) -> T
where
T: Copy + Eq + From<u8> + Div<Output = T> + Rem<Output = T>,
{
let scale = (self.bitdepth() / 8).into();
debug_assert!(n % scale == 0.into());
n / scale
}
}

pub trait BitDepth: Clone + Copy {
const BPC: BPC;
const BITDEPTH: u8;
const BITDEPTH: u8 = Self::BPC.bitdepth();

type Pixel: Copy
+ Ord
Expand Down Expand Up @@ -182,9 +198,7 @@ pub trait BitDepth: Clone + Copy {
where
T: Copy + Eq + TryFrom<usize> + From<u8> + Div<Output = T> + Rem<Output = T>,
{
let scale = mem::size_of::<Self::Pixel>().try_to::<T>().ok().unwrap();
debug_assert!(n % scale == 0.into());
n / scale
Self::BPC.pxstride(n)
}

fn bitdepth(&self) -> u8;
Expand Down Expand Up @@ -222,7 +236,6 @@ pub struct BitDepth8 {

impl BitDepth for BitDepth8 {
const BPC: BPC = BPC::BPC8;
const BITDEPTH: u8 = 8;

type Pixel = u8;

Expand Down Expand Up @@ -299,7 +312,6 @@ pub struct BitDepth16 {

impl BitDepth for BitDepth16 {
const BPC: BPC = BPC::BPC16;
const BITDEPTH: u8 = 16;

type Pixel = u16;

Expand Down

0 comments on commit 72c5bf5

Please sign in to comment.