pub mod colr;
pub mod hvcc;
pub mod simple;
use alloc::vec::Vec;
use crate::error::Result;
use crate::meta::iprp::Properties;
use colr::ColorInfo;
use hvcc::HevcConfig;
use simple::{AuxC, Clap, Ispe, Mirror, Pasp, Pixi, Rotation};
#[derive(Debug, Clone, Default)]
pub struct ItemProps<'a> {
pub hvcc: Option<HevcConfig<'a>>,
pub ispe: Option<Ispe>,
pub pixi: Option<Pixi>,
pub nclx: Option<colr::Nclx>,
pub icc: Option<&'a [u8]>,
pub icc_restricted: bool,
pub transforms: Vec<Transform>,
pub auxc: Option<AuxC<'a>>,
pub pasp: Option<Pasp>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Transform {
Crop(Clap),
Rotate(Rotation),
Mirror(Mirror),
}
impl ItemProps<'_> {
pub fn rotation(&self) -> Rotation {
self.transforms
.iter()
.find_map(|t| match t {
Transform::Rotate(r) => Some(*r),
_ => None,
})
.unwrap_or(Rotation::None)
}
pub fn mirror(&self) -> Option<Mirror> {
self.transforms.iter().find_map(|t| match t {
Transform::Mirror(m) => Some(*m),
_ => None,
})
}
pub fn clap(&self) -> Option<Clap> {
self.transforms.iter().find_map(|t| match t {
Transform::Crop(c) => Some(*c),
_ => None,
})
}
pub fn bit_depth(&self) -> Option<u8> {
if let Some(first) = self.pixi.as_ref().and_then(|p| p.bits_per_channel.first()) {
return Some(*first);
}
self.hvcc.as_ref().map(|c| c.bit_depth_luma)
}
}
pub fn gather<'a>(props: &Properties<'a>, item: u32) -> Result<ItemProps<'a>> {
let mut out = ItemProps::default();
for a in props.associations(item) {
let Some(b) = props.resolve(*a) else { continue };
match &b.boxtype {
b"hvcC" => out.hvcc = Some(hvcc::parse(b)?),
b"ispe" => out.ispe = Some(simple::parse_ispe(b)?),
b"pixi" => out.pixi = Some(simple::parse_pixi(b)?),
b"colr" => match colr::parse(b)? {
ColorInfo::Nclx(n) => out.nclx = Some(n),
ColorInfo::Icc { restricted, data } => {
out.icc = Some(data);
out.icc_restricted = restricted;
}
},
b"irot" => out
.transforms
.push(Transform::Rotate(simple::parse_irot(b)?)),
b"imir" => out
.transforms
.push(Transform::Mirror(simple::parse_imir(b)?)),
b"clap" => out.transforms.push(Transform::Crop(simple::parse_clap(b)?)),
b"auxC" => out.auxc = Some(simple::parse_auxc(b)?),
b"pasp" => out.pasp = Some(simple::parse_pasp(b)?),
_ => {}
}
}
Ok(out)
}