use super::boxes::Boxes;
use super::header::Jp2Header;
use super::metadata::{Ratio, Res, Resolution};
pub(crate) fn read(header: &Jp2Header<'_>) -> Resolution {
let mut resolution = Resolution::default();
for payload in header.res() {
for child in Boxes::children(payload, "the `res ` box") {
let Ok(child) = child else {
break;
};
let slot = match &child.kind {
b"resc" => &mut resolution.capture,
b"resd" => &mut resolution.display,
_ => continue,
};
if slot.is_none() {
*slot = parse_res(child.payload);
}
}
}
resolution
}
fn parse_res(payload: &[u8]) -> Option<Res> {
let &[vn0, vn1, vd0, vd1, hn0, hn1, hd0, hd1, ve, he] = payload else {
return None;
};
Some(Res {
vertical: Ratio {
numerator: u16::from_be_bytes([vn0, vn1]),
denominator: u16::from_be_bytes([vd0, vd1]),
exponent: i8::from_be_bytes([ve]),
},
horizontal: Ratio {
numerator: u16::from_be_bytes([hn0, hn1]),
denominator: u16::from_be_bytes([hd0, hd1]),
exponent: i8::from_be_bytes([he]),
},
})
}