use crate::err::EncodeError;
pub trait Pixel: Copy + Default + PartialEq + Send + Sync + std::fmt::Debug {
fn to_i32(self) -> i32;
fn to_f32(self) -> f32;
fn from_i32_clamped(v: i32, bit_depth: u8) -> Self;
}
impl Pixel for u8 {
#[inline]
fn to_i32(self) -> i32 {
self as i32
}
#[inline]
fn to_f32(self) -> f32 {
self as f32
}
#[inline]
fn from_i32_clamped(v: i32, _bit_depth: u8) -> Self {
v.clamp(0, 255) as u8
}
}
impl Pixel for u16 {
#[inline]
fn to_i32(self) -> i32 {
self as i32
}
#[inline]
fn to_f32(self) -> f32 {
self as f32
}
#[inline]
fn from_i32_clamped(v: i32, bit_depth: u8) -> Self {
let max = (1i32 << bit_depth) - 1;
v.clamp(0, max) as u16
}
}
impl Pixel for i32 {
#[inline]
fn to_i32(self) -> i32 {
self
}
#[inline]
fn to_f32(self) -> f32 {
self as f32
}
#[inline]
fn from_i32_clamped(v: i32, bit_depth: u8) -> Self {
let max = (1i32 << bit_depth) - 1;
v.clamp(0, max)
}
}
impl Pixel for f32 {
#[inline]
fn to_i32(self) -> i32 {
self as i32
}
#[inline]
fn to_f32(self) -> f32 {
self
}
#[inline]
fn from_i32_clamped(v: i32, bit_depth: u8) -> Self {
let max = (1i32 << bit_depth) - 1;
v.clamp(0, max) as f32
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BitDepth {
Eight,
Ten,
Twelve,
}
impl BitDepth {
pub fn bits(self) -> u8 {
match self {
BitDepth::Eight => 8,
BitDepth::Ten => 10,
BitDepth::Twelve => 12,
}
}
pub fn from_u8(bit_depth: u8) -> Result<Self, EncodeError> {
match bit_depth {
8 => Ok(BitDepth::Eight),
10 => Ok(BitDepth::Ten),
12 => Ok(BitDepth::Twelve),
_ => Err(EncodeError::InvalidInput),
}
}
}