Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fn BitDepth::pxstride: Make generic over stride type and BitDepth #769

Merged
merged 2 commits into from
Feb 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
fn from_prim(t: T) -> Self;
Expand Down Expand Up @@ -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<T>(n: T) -> T
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
}

fn bitdepth(&self) -> u8;

Expand Down Expand Up @@ -238,10 +249,6 @@ impl BitDepth for BitDepth8 {
DisplayPixel8(pixel)
}

fn pxstride(n: usize) -> usize {
n
}

fn bitdepth(&self) -> u8 {
Self::BITDEPTH
}
Expand Down Expand Up @@ -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
}
Expand Down
Loading