use core::fmt;
use std::fmt::{Display, Formatter};
use crate::utils::four_char_code::FourCharCode;
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, Default)]
#[non_exhaustive]
pub enum PixelFormat {
#[default]
BGRA,
l10r,
YCbCr_420v,
YCbCr_420f,
xf44,
RGhA,
Unknown(FourCharCode),
}
impl PartialEq for PixelFormat {
fn eq(&self, other: &Self) -> bool {
FourCharCode::from(*self) == FourCharCode::from(*other)
}
}
impl Eq for PixelFormat {}
impl std::hash::Hash for PixelFormat {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
FourCharCode::from(*self).hash(state);
}
}
impl Display for PixelFormat {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let c: FourCharCode = (*self).into();
write!(f, "{}", c.display())
}
}
impl From<PixelFormat> for FourCharCode {
fn from(val: PixelFormat) -> Self {
match val {
PixelFormat::BGRA => Self::from_bytes(*b"BGRA"),
PixelFormat::l10r => Self::from_bytes(*b"l10r"),
PixelFormat::YCbCr_420v => Self::from_bytes(*b"420v"),
PixelFormat::YCbCr_420f => Self::from_bytes(*b"420f"),
PixelFormat::xf44 => Self::from_bytes(*b"xf44"),
PixelFormat::RGhA => Self::from_bytes(*b"RGhA"),
PixelFormat::Unknown(code) => code,
}
}
}
impl From<u32> for PixelFormat {
fn from(value: u32) -> Self {
let c = FourCharCode::from_u32(value);
c.into()
}
}
impl From<FourCharCode> for PixelFormat {
fn from(val: FourCharCode) -> Self {
match val.display().as_str() {
"BGRA" => Self::BGRA,
"l10r" => Self::l10r,
"420v" => Self::YCbCr_420v,
"420f" => Self::YCbCr_420f,
"xf44" => Self::xf44,
"RGhA" => Self::RGhA,
_ => Self::Unknown(val),
}
}
}