pub trait Pixel: Copy + Default + Send + Sync + Into<i32> + 'static {
const BITDEPTH: u8;
const MAX: Self;
fn from_i32(v: i32) -> Self;
fn as_u16(self) -> u16;
fn slice_as_ne_bytes(samples: &[Self]) -> &[u8];
fn slice_as_ne_bytes_mut(samples: &mut [Self]) -> &mut [u8];
fn slice_from_plane_storage(storage: &crate::picture::PlaneStorage) -> Option<&[Self]>;
fn slice_from_plane_storage_mut(
storage: &mut crate::picture::PlaneStorage,
) -> Option<&mut [Self]>;
fn try_as_u8_slice(samples: &[Self]) -> Option<&[u8]>;
fn try_as_u8_slice_mut(samples: &mut [Self]) -> Option<&mut [u8]>;
fn try_as_u16_slice(samples: &[Self]) -> Option<&[u16]>;
fn try_as_u16_slice_mut(samples: &mut [Self]) -> Option<&mut [u16]>;
}
impl Pixel for u8 {
const BITDEPTH: u8 = 8;
const MAX: u8 = 0xFF;
#[inline(always)]
fn from_i32(v: i32) -> Self {
v as u8
}
#[inline(always)]
fn as_u16(self) -> u16 {
self as u16
}
#[inline(always)]
fn slice_as_ne_bytes(samples: &[Self]) -> &[u8] {
samples
}
#[inline(always)]
fn slice_as_ne_bytes_mut(samples: &mut [Self]) -> &mut [u8] {
samples
}
#[inline(always)]
fn slice_from_plane_storage(storage: &crate::picture::PlaneStorage) -> Option<&[Self]> {
match storage {
crate::picture::PlaneStorage::U8(v) => Some(v.as_slice()),
_ => None,
}
}
#[inline(always)]
fn slice_from_plane_storage_mut(
storage: &mut crate::picture::PlaneStorage,
) -> Option<&mut [Self]> {
match storage {
crate::picture::PlaneStorage::U8(v) => Some(std::sync::Arc::make_mut(v).as_mut_slice()),
_ => None,
}
}
#[inline(always)]
fn try_as_u8_slice(samples: &[Self]) -> Option<&[u8]> {
Some(samples)
}
#[inline(always)]
fn try_as_u8_slice_mut(samples: &mut [Self]) -> Option<&mut [u8]> {
Some(samples)
}
#[inline(always)]
fn try_as_u16_slice(_samples: &[Self]) -> Option<&[u16]> {
None
}
#[inline(always)]
fn try_as_u16_slice_mut(_samples: &mut [Self]) -> Option<&mut [u16]> {
None
}
}
impl Pixel for u16 {
const BITDEPTH: u8 = 16;
const MAX: u16 = 0xFFFF;
#[inline(always)]
fn from_i32(v: i32) -> Self {
v as u16
}
#[inline(always)]
fn as_u16(self) -> u16 {
self
}
#[inline(always)]
fn slice_as_ne_bytes(samples: &[Self]) -> &[u8] {
let len = std::mem::size_of_val(samples);
unsafe { core::slice::from_raw_parts(samples.as_ptr() as *const u8, len) }
}
#[inline(always)]
fn slice_as_ne_bytes_mut(samples: &mut [Self]) -> &mut [u8] {
let len = std::mem::size_of_val(samples);
unsafe { core::slice::from_raw_parts_mut(samples.as_mut_ptr() as *mut u8, len) }
}
#[inline(always)]
fn slice_from_plane_storage(storage: &crate::picture::PlaneStorage) -> Option<&[Self]> {
match storage {
crate::picture::PlaneStorage::U16(v) => Some(v.as_slice()),
_ => None,
}
}
#[inline(always)]
fn slice_from_plane_storage_mut(
storage: &mut crate::picture::PlaneStorage,
) -> Option<&mut [Self]> {
match storage {
crate::picture::PlaneStorage::U16(v) => {
Some(std::sync::Arc::make_mut(v).as_mut_slice())
}
_ => None,
}
}
#[inline(always)]
fn try_as_u8_slice(_samples: &[Self]) -> Option<&[u8]> {
None
}
#[inline(always)]
fn try_as_u8_slice_mut(_samples: &mut [Self]) -> Option<&mut [u8]> {
None
}
#[inline(always)]
fn try_as_u16_slice(samples: &[Self]) -> Option<&[u16]> {
Some(samples)
}
#[inline(always)]
fn try_as_u16_slice_mut(samples: &mut [Self]) -> Option<&mut [u16]> {
Some(samples)
}
}
pub trait Coeff: Copy + Default + Send + Sync + 'static {
const ZERO: Self;
#[allow(unused)]
const IS_I16: bool;
fn from_i32(v: i32) -> Self;
fn to_i32(self) -> i32;
#[allow(unused)]
#[inline(always)]
fn try_as_i16_slice(_samples: &[Self]) -> Option<&[i16]> {
None
}
#[inline(always)]
fn try_as_i16_slice_mut(_samples: &mut [Self]) -> Option<&mut [i16]> {
None
}
#[allow(unused)]
#[inline(always)]
fn try_as_i32_slice(_samples: &[Self]) -> Option<&[i32]> {
None
}
#[allow(unused)]
#[inline(always)]
fn try_as_i32_slice_mut(_samples: &mut [Self]) -> Option<&mut [i32]> {
None
}
}
impl Coeff for i16 {
const ZERO: Self = 0;
const IS_I16: bool = true;
#[inline(always)]
fn from_i32(v: i32) -> Self {
v as i16
}
#[inline(always)]
fn to_i32(self) -> i32 {
self as i32
}
#[inline(always)]
fn try_as_i16_slice(samples: &[Self]) -> Option<&[i16]> {
Some(samples)
}
#[inline(always)]
fn try_as_i16_slice_mut(samples: &mut [Self]) -> Option<&mut [i16]> {
Some(samples)
}
}
impl Coeff for i32 {
const ZERO: Self = 0;
const IS_I16: bool = false;
#[inline(always)]
fn from_i32(v: i32) -> Self {
v
}
#[inline(always)]
fn to_i32(self) -> i32 {
self
}
#[inline(always)]
fn try_as_i32_slice(samples: &[Self]) -> Option<&[i32]> {
Some(samples)
}
#[inline(always)]
fn try_as_i32_slice_mut(samples: &mut [Self]) -> Option<&mut [i32]> {
Some(samples)
}
}
pub trait BitDepth: Clone + Copy + Send + Sync + 'static {
type Pixel: Pixel;
type Coef: Coeff;
const BPC: u8;
fn new(bitdepth: u8) -> Self;
fn bitdepth(&self) -> u8;
fn bitdepth_max(&self) -> i32;
#[inline(always)]
fn bitdepth_min_8(&self) -> i32 {
self.bitdepth() as i32 - 8
}
#[inline(always)]
fn pixel_clip(&self, v: i32) -> Self::Pixel {
let max = self.bitdepth_max();
Self::Pixel::from_i32(v.clamp(0, max))
}
}
#[derive(Clone, Copy, Default)]
pub struct BitDepth8;
impl BitDepth for BitDepth8 {
type Pixel = u8;
type Coef = i16;
const BPC: u8 = 8;
#[inline(always)]
fn new(_bitdepth: u8) -> Self {
BitDepth8
}
#[inline(always)]
fn bitdepth(&self) -> u8 {
8
}
#[inline(always)]
fn bitdepth_max(&self) -> i32 {
255
}
}
#[derive(Clone, Copy)]
pub struct BitDepth16 {
bitdepth: u8,
}
impl BitDepth for BitDepth16 {
type Pixel = u16;
type Coef = i32;
const BPC: u8 = 16;
#[inline(always)]
fn new(bitdepth: u8) -> Self {
debug_assert!(bitdepth == 10 || bitdepth == 12);
BitDepth16 { bitdepth }
}
#[inline(always)]
fn bitdepth(&self) -> u8 {
self.bitdepth
}
#[inline(always)]
fn bitdepth_max(&self) -> i32 {
(1 << self.bitdepth) - 1
}
}