beffroi-tools/crates/midra-layout/src/formats.rs

300 lines
9.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Source-space cropping and output-space framing. No device protocol assumptions.
use crate::Geometry;
#[derive(Clone, Copy, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Aspect {
FourThree,
SixteenNine,
SixteenTen,
Square,
Custom,
}
impl Aspect {
pub const ALL: [Self; 5] = [
Self::FourThree,
Self::SixteenNine,
Self::SixteenTen,
Self::Square,
Self::Custom,
];
pub fn label(self) -> &'static str {
match self {
Self::FourThree => "4:3",
Self::SixteenNine => "16:9",
Self::SixteenTen => "16:10",
Self::Square => "1:1",
Self::Custom => "Custom",
}
}
pub fn ratio(self) -> Option<f64> {
match self {
Self::FourThree => Some(4.0 / 3.0),
Self::SixteenNine => Some(16.0 / 9.0),
Self::SixteenTen => Some(1.6),
Self::Square => Some(1.0),
Self::Custom => None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Crop {
pub left: f64,
pub right: f64,
pub top: f64,
pub bottom: f64,
}
impl Crop {
pub fn remaining(self, size: [f64; 2]) -> Result<[f64; 2], String> {
if [self.left, self.right, self.top, self.bottom]
.iter()
.any(|v| !v.is_finite() || *v < 0.0)
{
return Err("Crop edges must be finite and nonnegative.".into());
}
let remaining = [
size[0] - self.left - self.right,
size[1] - self.top - self.bottom,
];
validate_size(remaining)?;
Ok(remaining)
}
pub fn centered(native: [f64; 2], size: [f64; 2]) -> Result<Self, String> {
validate_size(native)?;
validate_size(size)?;
if size[0] > native[0] || size[1] > native[1] {
return Err("Input resolution cannot exceed the native source dimensions.".into());
}
Ok(Self {
left: (native[0] - size[0]) / 2.0,
right: (native[0] - size[0]) / 2.0,
top: (native[1] - size[1]) / 2.0,
bottom: (native[1] - size[1]) / 2.0,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Source {
pub aspect: Aspect,
pub native: [u32; 2],
pub input_crop: Crop,
}
impl Default for Source {
fn default() -> Self {
Self {
aspect: Aspect::SixteenNine,
native: [1920, 1080],
input_crop: Crop::default(),
}
}
}
impl Source {
pub fn input(self) -> Result<[f64; 2], String> {
validate_resolution(self.native)?;
if let Some(ratio) = self.aspect.ratio()
&& (self.native[1] as f64 - self.native[0] as f64 / ratio).abs() > 0.500001
{
return Err("Native dimensions do not match Source format. Choose Custom or correct the native dimensions.".into());
}
self.input_crop.remaining(self.native.map(f64::from))
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Framing {
pub zoom: f64,
pub offset: [f64; 2],
}
impl Default for Framing {
fn default() -> Self {
Self {
zoom: 1.0,
offset: [0.0; 2],
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Mapping {
pub framing: Framing,
pub scale: f64,
pub retained: [f64; 2],
pub offset_limits: [f64; 2],
}
pub fn validate_size(size: [f64; 2]) -> Result<(), String> {
if size
.iter()
.any(|v| !v.is_finite() || *v <= 0.0 || *v > 1_000_000.0)
{
return Err(
"Dimensions must be positive, finite and no larger than 1,000,000 pixels.".into(),
);
}
Ok(())
}
pub fn resolution_from_values(size: [f64; 2]) -> Result<[u32; 2], String> {
if size
.iter()
.any(|v| !v.is_finite() || v.fract() != 0.0 || *v < 1.0 || *v > 16384.0)
{
return Err("Resolution must use whole pixels from 1 to 16384 per side.".into());
}
Ok(size.map(|v| v as u32))
}
pub fn validate_resolution(size: [u32; 2]) -> Result<(), String> {
if size.iter().any(|v| !(1..=16384).contains(v)) {
return Err("Resolution must use whole pixels from 1 to 16384 per side (planning limit, not a device mode).".into());
}
Ok(())
}
pub fn limits(input: [f64; 2], size: [f64; 2], zoom: f64) -> Result<(f64, [f64; 2]), String> {
validate_size(input)?;
validate_size(size)?;
if !zoom.is_finite() || !(1.0..=100.0).contains(&zoom) {
return Err("Content zoom must be between 100% and 10000% (planning limit).".into());
}
let k = (size[0] / input[0]).max(size[1] / input[1]) * zoom;
Ok((
k,
[
(k * input[0] - size[0]).max(0.0) / 2.0,
(k * input[1] - size[1]).max(0.0) / 2.0,
],
))
}
pub fn crop_from_framing(
input: [f64; 2],
size: [f64; 2],
framing: Framing,
) -> Result<Crop, String> {
let (k, limit) = limits(input, size, framing.zoom)?;
for (i, bound) in limit.iter().enumerate() {
if !framing.offset[i].is_finite() || framing.offset[i].abs() > bound + 1e-7 {
return Err(format!(
"Content offset {} must be between {:.1} and {:.1} output px.",
if i == 0 { "X" } else { "Y" },
-bound,
bound
));
}
}
Ok(Crop {
left: ((input[0] - size[0] / k) / 2.0 - framing.offset[0] / k).max(0.0),
right: ((input[0] - size[0] / k) / 2.0 + framing.offset[0] / k).max(0.0),
top: ((input[1] - size[1] / k) / 2.0 - framing.offset[1] / k).max(0.0),
bottom: ((input[1] - size[1] / k) / 2.0 + framing.offset[1] / k).max(0.0),
})
}
pub fn mapping(input: [f64; 2], size: [f64; 2], crop: Crop) -> Result<Mapping, String> {
validate_size(input)?;
validate_size(size)?;
let retained = crop.remaining(input)?;
let kx = size[0] / retained[0];
let ky = size[1] / retained[1];
// Recover scale from the less-cropped axis to avoid cancellation when
// a tiny retained span is subtracted from a very wide source.
let k = if retained[0] / input[0] >= retained[1] / input[1] {
kx
} else {
ky
};
let tolerance =
1e-8_f64.max(8.0 * f64::EPSILON * (input[0] / retained[0] + input[1] / retained[1]));
if (kx - ky).abs() > tolerance * kx.max(ky) {
return Err("Advanced crop aspect does not match the visible layer box.".into());
}
let zoom = k / (size[0] / input[0]).max(size[1] / input[1]);
// Tolerate only floating-point roundoff at the documented zoom boundaries.
if !(1.0 - 1e-8..=100.0 + 1e-8).contains(&zoom) {
return Err("Crop requires content zoom outside 100%–10000%.".into());
}
let framing = Framing {
zoom: zoom.clamp(1.0, 100.0),
offset: [
k * (crop.right - crop.left) / 2.0,
k * (crop.bottom - crop.top) / 2.0,
],
};
let (_, offset_limits) = limits(input, size, framing.zoom)?;
Ok(Mapping {
framing,
scale: k,
retained,
offset_limits,
})
}
/// Preserve zoom and the normalized retained center after source or box changes.
/// Returns proposed offsets and whether moving the retained center was necessary.
pub fn reframed(
old_input: [f64; 2],
old_size: [f64; 2],
crop: Crop,
input: [f64; 2],
size: [f64; 2],
) -> Result<(Crop, bool), String> {
let old = mapping(old_input, old_size, crop)?;
let center = [
(crop.left + old.retained[0] / 2.0) / old_input[0],
(crop.top + old.retained[1] / 2.0) / old_input[1],
];
let (k, bounds) = limits(input, size, old.framing.zoom)?;
let desired = [
(0.5 - center[0]) * input[0] * k,
(0.5 - center[1]) * input[1] * k,
];
let offset = [
desired[0].clamp(-bounds[0], bounds[0]),
desired[1].clamp(-bounds[1], bounds[1]),
];
let adjusted = (desired[0] - offset[0]).abs() > 1e-7 || (desired[1] - offset[1]).abs() > 1e-7;
Ok((
crop_from_framing(
input,
size,
Framing {
zoom: old.framing.zoom,
offset,
},
)?,
adjusted,
))
}
#[derive(Clone, Debug, PartialEq)]
pub struct Formats {
pub output: [u32; 2],
pub sources: Vec<Source>,
pub source_for: Vec<usize>,
pub advanced: Vec<Crop>,
}
impl Default for Formats {
fn default() -> Self {
Self {
output: [1920, 1080],
sources: vec![Source::default(); 2],
source_for: vec![0, 1],
advanced: vec![Crop::default(); 2],
}
}
}
impl Formats {
pub fn input(&self, i: usize) -> Result<[f64; 2], String> {
self.sources[self.source_for[i]].input()
}
pub fn mapping(&self, i: usize, g: Geometry) -> Result<Mapping, String> {
mapping(self.input(i)?, [g.width, g.height], self.advanced[i])
}
pub fn resize(&mut self, i: usize, before: Geometry, after: Geometry) -> Result<(), String> {
if [before.width, before.height] == [after.width, after.height] {
return Ok(());
}
let (crop, _) = reframed(
self.input(i)?,
[before.width, before.height],
self.advanced[i],
self.input(i)?,
[after.width, after.height],
)?;
self.advanced[i] = crop;
Ok(())
}
}