Skip to content

Commit

Permalink
trait BitDepth: Add fn from_c to uniformly convert from C's repr …
Browse files Browse the repository at this point in the history
…(w/ docs).
  • Loading branch information
kkysen committed Jul 24, 2023
1 parent d6ec80e commit 04a0a81
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions include/common/bitdepth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ pub trait BitDepth: Clone + Copy {

fn new(bitdepth_max: Self::BitDepthMax) -> Self;

/// While [`Self::new`] is the implementation specific way to construct a [`BitDepth`],
/// [`Self::from_c`] is a uniform way to construct a [`BitDepth`]
/// from its C representation, which is a [`c_int`].
///
/// Since we're deduplicating [`BitDepth`]-dependent `fn` ptr types through type erasure,
/// and always passing the `bitdepth_max` last argument
/// even for `8bpc` `fn`s where it's superfluous (it's constant),
/// we need to convert from that `bitdepth_max: c_int` arg back to a [`BitDepth`].
fn from_c(bitdepth_max: c_int) -> Self;

fn pixel_copy(dest: &mut [Self::Pixel], src: &[Self::Pixel], n: usize) {
dest[..n].copy_from_slice(&src[..n]);
}
Expand Down Expand Up @@ -145,6 +155,10 @@ impl BitDepth for BitDepth8 {
Self { bitdepth_max }
}

fn from_c(_bitdepth_max: c_int) -> Self {
Self::new(())
}

fn display(pixel: Self::Pixel) -> Self::DisplayPixel {
DisplayPixel8(pixel)
}
Expand Down Expand Up @@ -189,6 +203,10 @@ impl BitDepth for BitDepth16 {
Self { bitdepth_max }
}

fn from_c(bitdepth_max: c_int) -> Self {
Self::new(bitdepth_max.as_())
}

fn display(pixel: Self::Pixel) -> Self::DisplayPixel {
DisplayPixel16(pixel)
}
Expand Down

0 comments on commit 04a0a81

Please sign in to comment.