use serde::{Deserialize, Serialize};
use super::BBox;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AxisOrder {
Xy,
Yx,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Letterbox {
Stretch,
Center,
TopLeft,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct LetterboxGeometry {
pub scale_x: f64,
pub scale_y: f64,
pub pad_left: u32,
pub pad_top: u32,
pub resized: (u32, u32),
}
impl Letterbox {
pub fn geometry(self, sent: (u32, u32), input: (u32, u32)) -> LetterboxGeometry {
let (w, h) = (f64::from(sent.0.max(1)), f64::from(sent.1.max(1)));
let (iw, ih) = (f64::from(input.0), f64::from(input.1));
match self {
Self::Stretch => LetterboxGeometry {
scale_x: iw / w,
scale_y: ih / h,
pad_left: 0,
pad_top: 0,
resized: input,
},
Self::Center | Self::TopLeft => {
let s = (iw / w).min(ih / h);
let rw = ((w * s).round() as u32).clamp(1, input.0.max(1));
let rh = ((h * s).round() as u32).clamp(1, input.1.max(1));
let (pad_left, pad_top) = if self == Self::Center {
((input.0 - rw) / 2, (input.1 - rh) / 2)
} else {
(0, 0)
};
LetterboxGeometry {
scale_x: f64::from(rw) / w,
scale_y: f64::from(rh) / h,
pad_left,
pad_top,
resized: (rw, rh),
}
}
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "kind")]
pub enum CoordFrame {
SentPixels,
Normalized {
scale: u32,
order: AxisOrder,
},
ModelInput {
width: u32,
height: u32,
letterbox: Letterbox,
},
}
impl CoordFrame {
pub fn box_from_raw(&self, raw: [f32; 4]) -> BBox {
match self {
Self::Normalized { order: AxisOrder::Yx, .. } => {
BBox::new(raw[1], raw[0], raw[3], raw[2])
}
_ => BBox::new(raw[0], raw[1], raw[2], raw[3]),
}
}
pub fn point_from_raw(&self, raw: [f32; 2]) -> [f32; 2] {
match self {
Self::Normalized { order: AxisOrder::Yx, .. } => [raw[1], raw[0]],
_ => raw,
}
}
pub fn point_to_sent(&self, point: [f64; 2], sent: (u32, u32)) -> [f64; 2] {
let (w, h) = (f64::from(sent.0), f64::from(sent.1));
match self {
Self::SentPixels => point,
Self::Normalized { scale, .. } => {
let s = f64::from((*scale).max(1));
[point[0] / s * w, point[1] / s * h]
}
Self::ModelInput { width, height, letterbox } => {
let g = letterbox.geometry(sent, (*width, *height));
[
(point[0] - f64::from(g.pad_left)) / g.scale_x,
(point[1] - f64::from(g.pad_top)) / g.scale_y,
]
}
}
}
pub fn point_from_sent(&self, point: [f64; 2], sent: (u32, u32)) -> [f64; 2] {
let (w, h) = (f64::from(sent.0.max(1)), f64::from(sent.1.max(1)));
match self {
Self::SentPixels => point,
Self::Normalized { scale, .. } => {
let s = f64::from((*scale).max(1));
[point[0] / w * s, point[1] / h * s]
}
Self::ModelInput { width, height, letterbox } => {
let g = letterbox.geometry(sent, (*width, *height));
[
point[0] * g.scale_x + f64::from(g.pad_left),
point[1] * g.scale_y + f64::from(g.pad_top),
]
}
}
}
pub fn bbox_to_sent(&self, bbox: &BBox, sent: (u32, u32)) -> BBox {
let a = self.point_to_sent([f64::from(bbox.x0), f64::from(bbox.y0)], sent);
let b = self.point_to_sent([f64::from(bbox.x1), f64::from(bbox.y1)], sent);
BBox::new(a[0] as f32, a[1] as f32, b[0] as f32, b[1] as f32)
}
pub fn mask_raster(&self, sent: (u32, u32)) -> Option<(u32, u32)> {
match self {
Self::SentPixels => Some(sent),
Self::Normalized { scale, .. } => Some(((*scale).max(1), (*scale).max(1))),
Self::ModelInput { width, height, .. } => Some((*width, *height)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn letterbox_round_trips_points() {
for letterbox in [Letterbox::Stretch, Letterbox::Center, Letterbox::TopLeft] {
let frame = CoordFrame::ModelInput { width: 640, height: 640, letterbox };
let p = [1234.5, 777.25];
let model = frame.point_from_sent(p, (3024, 1964));
let back = frame.point_to_sent(model, (3024, 1964));
assert!((back[0] - p[0]).abs() < 1e-6 && (back[1] - p[1]).abs() < 1e-6);
}
}
#[test]
fn top_left_letterbox_has_no_leading_padding() {
let g = Letterbox::TopLeft.geometry((1000, 500), (640, 640));
assert_eq!((g.pad_left, g.pad_top, g.resized), (0, 0, (640, 320)));
}
}