diff --git a/include/common/bitdepth.rs b/include/common/bitdepth.rs index a06e56343..4970f02a5 100644 --- a/include/common/bitdepth.rs +++ b/include/common/bitdepth.rs @@ -8,8 +8,11 @@ use std::fmt::Display; use std::fmt::Formatter; use std::mem; use std::ops::Add; +use std::ops::Div; use std::ops::Mul; +use std::ops::Rem; use std::ops::Shr; +use to_method::To as _; pub trait FromPrimitive { fn from_prim(t: T) -> Self; @@ -174,7 +177,15 @@ pub trait BitDepth: Clone + Copy { clip(pixel, 0.into(), self.bitdepth_max()) } - fn pxstride(n: usize) -> usize; + /// `T` is generally meant to be `usize` or `isize`. + fn pxstride(n: T) -> T + where + T: Copy + Eq + TryFrom + From + Div + Rem, + { + let scale = mem::size_of::().try_to::().ok().unwrap(); + debug_assert!(n % scale == 0.into()); + n / scale + } fn bitdepth(&self) -> u8; @@ -238,10 +249,6 @@ impl BitDepth for BitDepth8 { DisplayPixel8(pixel) } - fn pxstride(n: usize) -> usize { - n - } - fn bitdepth(&self) -> u8 { Self::BITDEPTH } @@ -319,11 +326,6 @@ impl BitDepth for BitDepth16 { DisplayPixel16(pixel) } - fn pxstride(n: usize) -> usize { - debug_assert!(n & 1 == 0); - n >> 1 - } - fn bitdepth(&self) -> u8 { (Self::Pixel::BITS - self.bitdepth_max.leading_zeros()) as u8 }