mod float32;
mod layer;
mod uint16;
mod uint8;
pub use layer::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum PixelDepthKind {
U8,
U16,
F32,
}
impl PixelDepthKind {
pub fn bits(self) -> u32 {
match self {
Self::U8 => 8,
Self::U16 => 16,
Self::F32 => 32,
}
}
pub fn from_bits(bits: u32) -> Option<Self> {
match bits {
8 => Some(Self::U8),
16 => Some(Self::U16),
32 => Some(Self::F32),
_ => None,
}
}
pub fn bytes_per_pixel(self) -> usize {
self.bits() as usize / 2
}
}
impl core::fmt::Display for PixelDepthKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{} bpc", self.bits())
}
}
mod sealed {
pub trait Sealed {}
}
pub trait PixelDepth: sealed::Sealed + Copy + Default + core::fmt::Debug + Send + Sync + 'static {
type Depth: Default + Copy + Clone + PartialEq + Send + Sync + core::fmt::Debug;
const KIND: PixelDepthKind;
fn max_value() -> Self::Depth;
fn to_unit(value: Self::Depth) -> f32;
fn from_unit(value: f32) -> Self::Depth;
#[doc(hidden)]
fn wrap(layer: Layer<Self>) -> AnyLayer;
#[doc(hidden)]
fn unwrap_ref(layer: &AnyLayer) -> Option<&Layer<Self>>;
#[doc(hidden)]
fn unwrap_mut(layer: &mut AnyLayer) -> Option<&mut Layer<Self>>;
}
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct Pixel<T: PixelDepth> {
pub alpha: T::Depth,
pub red: T::Depth,
pub green: T::Depth,
pub blue: T::Depth,
}
impl<T: PixelDepth> Pixel<T> {
pub fn blank() -> Self {
Pixel {
alpha: T::Depth::default(),
red: T::Depth::default(),
green: T::Depth::default(),
blue: T::Depth::default(),
}
}
pub fn black() -> Self {
Self::opaque(false, false, false)
}
pub fn white() -> Self {
Self::opaque(true, true, true)
}
pub fn red() -> Self {
Self::opaque(true, false, false)
}
pub fn green() -> Self {
Self::opaque(false, true, false)
}
pub fn blue() -> Self {
Self::opaque(false, false, true)
}
pub fn cyan() -> Self {
Self::opaque(false, true, true)
}
pub fn magenta() -> Self {
Self::opaque(true, false, true)
}
#[deprecated(note = "this is magenta; use `Pixel::magenta`")]
pub fn purple() -> Self {
Self::magenta()
}
pub fn yellow() -> Self {
Self::opaque(true, true, false)
}
pub fn transparent() -> Self {
Self::blank()
}
fn opaque(red: bool, green: bool, blue: bool) -> Self {
let channel = |on| if on { T::max_value() } else { T::Depth::default() };
Pixel {
alpha: T::max_value(),
red: channel(red),
green: channel(green),
blue: channel(blue),
}
}
pub fn from_argb([alpha, red, green, blue]: [T::Depth; 4]) -> Self {
Pixel {
alpha,
red,
green,
blue,
}
}
pub fn to_argb(&self) -> [T::Depth; 4] {
[self.alpha, self.red, self.green, self.blue]
}
pub fn from_rgba([red, green, blue, alpha]: [T::Depth; 4]) -> Self {
Pixel {
alpha,
red,
green,
blue,
}
}
pub fn to_rgba(&self) -> [T::Depth; 4] {
[self.red, self.green, self.blue, self.alpha]
}
pub fn to_unit(&self) -> [f32; 4] {
[
T::to_unit(self.red),
T::to_unit(self.green),
T::to_unit(self.blue),
T::to_unit(self.alpha),
]
}
pub fn from_unit([red, green, blue, alpha]: [f32; 4]) -> Self {
Pixel {
alpha: T::from_unit(alpha),
red: T::from_unit(red),
green: T::from_unit(green),
blue: T::from_unit(blue),
}
}
}
pub use float32::Depth32;
pub use uint8::Depth8;
pub use uint16::Depth16;
#[cfg(test)]
mod tests;