Skip to content

Commit

Permalink
src/fg_apply.rs: Deduplicate w/ generics (#529)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkysen authored Oct 18, 2023
2 parents b5bb376 + b98a747 commit 4fc7fc0
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 445 deletions.
43 changes: 28 additions & 15 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ impl_FromPrimitive!(isize => {, ...});
impl_FromPrimitive!(f32 => {, ...});
impl_FromPrimitive!(f64 => {, ...});

/// [`Default`] isn't `impl`emented for all arrays `[T; N]`
/// because they were implemented before `const` generics
/// and thus only for low values of `N`.
pub trait ArrayDefault {
fn default() -> Self;
}

impl<T: Default + Copy, const N: usize> ArrayDefault for [T; N] {
fn default() -> Self {
[T::default(); N]
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BPC {
BPC8,
Expand Down Expand Up @@ -108,6 +121,11 @@ pub trait BitDepth: Clone + Copy {
+ Add<Output = Self::Coef>
+ Display;

type Entry: Copy + Default;

type Scaling: AsRef<[u8]> + AsMut<[u8]> + ArrayDefault + Copy;
const SCALING_SIZE: usize;

type BitDepthMax;

type DisplayPixel: Display;
Expand Down Expand Up @@ -174,11 +192,6 @@ pub trait BitDepth: Clone + Copy {
T: BitDepthDependentType,
T::T<BitDepth8>: Copy,
T::T<BitDepth16>: Copy;

type GrainLut;
type Scaling;

const SCALING_LEN: usize;
}

#[derive(Clone, Copy)]
Expand All @@ -195,6 +208,11 @@ impl BitDepth for BitDepth8 {

type Coef = i16;

type Entry = i8;

type Scaling = [u8; Self::SCALING_SIZE];
const SCALING_SIZE: usize = 256;

type BitDepthMax = ();

type DisplayPixel = DisplayPixel8;
Expand Down Expand Up @@ -256,11 +274,6 @@ impl BitDepth for BitDepth8 {
{
bd.bpc8
}

type GrainLut = i8;
type Scaling = [u8; Self::SCALING_LEN];

const SCALING_LEN: usize = 256;
}

#[derive(Clone, Copy)]
Expand All @@ -276,6 +289,11 @@ impl BitDepth for BitDepth16 {

type Coef = i32;

type Entry = i16;

type Scaling = [u8; Self::SCALING_SIZE];
const SCALING_SIZE: usize = 4096;

type BitDepthMax = Self::Pixel;

type DisplayPixel = DisplayPixel16;
Expand Down Expand Up @@ -341,11 +359,6 @@ impl BitDepth for BitDepth16 {
{
bd.bpc16
}

type GrainLut = i16;
type Scaling = [u8; Self::SCALING_LEN];

const SCALING_LEN: usize = 4096;
}

pub struct DisplayPixel8(<BitDepth8 as BitDepth>::Pixel);
Expand Down
5 changes: 1 addition & 4 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ pub mod src {
mod dequant_tables;
mod env;
pub(crate) mod error;
#[cfg(feature = "bitdepth_16")]
mod fg_apply_tmpl_16;
#[cfg(feature = "bitdepth_8")]
mod fg_apply_tmpl_8;
mod fg_apply;
mod filmgrain;
#[cfg(feature = "bitdepth_16")]
mod filmgrain_tmpl_16;
Expand Down
Loading

0 comments on commit 4fc7fc0

Please sign in to comment.