Normalize rounded Midra fits to exact smart crop on import

This commit is contained in:
Dandaedre 2026-11-19 08:25:32 +01:00
parent 7daf2d18e8
commit cbbcde2a92
6 changed files with 147 additions and 7 deletions

View File

@ -1954,8 +1954,16 @@ mod tests {
&[layer.fit],
)
.unwrap();
assert_eq!(restored.formats().layer_fit[0], layer.fit);
assert_eq!(restored.formats().advanced[0], crop);
assert_eq!(restored.layers()[0], layer.geometry);
if sizing == Sizing::OutputBox {
assert_eq!(restored.formats().layer_fit[0], None);
let normal = restored.formats().mapping(0, layer.geometry).unwrap();
assert!((normal.framing.zoom - 2.0).abs() < 0.001);
assert!((normal.framing.offset[0] - 30.0).abs() < 0.01);
} else {
assert_eq!(restored.formats().layer_fit[0], layer.fit);
assert_eq!(restored.formats().advanced[0], crop);
}
let framing =
crate::formats::mapping(source.input().unwrap(), [image.width, image.height], crop)
.unwrap()

View File

@ -1520,6 +1520,7 @@ impl Layout {
)?;
}
if let Some((source, crop)) = framing {
let mut imported_crop = *crop;
if let Some(mode) = fit {
let image = mode.image(crop.remaining(source.input()?)?, *geometry)?;
// Normalize only an identical, uniformly scaled visible rectangle.
@ -1536,6 +1537,35 @@ impl Layout {
.is_ok()
{
draft.formats.layer_fit[i] = None;
} else if matches!(
mode,
formats::LayerFit::Centered | formats::LayerFit::Cropped
) && (image.width - geometry.width).abs() <= 1.0
&& (image.height - geometry.height).abs() <= 1.0
{
// Device crop fractions can round a uniform fit away from the
// exact box aspect. Trim symmetrically to cover, preserving
// the source-space center and never revealing new pixels.
let input = source.input()?;
let retained = crop.remaining(input)?;
let scale =
(geometry.width / retained[0]).max(geometry.height / retained[1]);
let trim = [
((retained[0] - geometry.width / scale) / 2.0).max(0.0),
((retained[1] - geometry.height / scale) / 2.0).max(0.0),
];
let normalized = formats::Crop {
left: crop.left + trim[0],
right: crop.right + trim[0],
top: crop.top + trim[1],
bottom: crop.bottom + trim[1],
};
if formats::mapping(input, [geometry.width, geometry.height], normalized)
.is_ok()
{
imported_crop = normalized;
draft.formats.layer_fit[i] = None;
}
}
} else {
formats::mapping(source.input()?, [geometry.width, geometry.height], *crop)?;
@ -1559,7 +1589,7 @@ impl Layout {
};
draft.formats.sources[source_id] = *source;
draft.formats.source_for[i] = source_id;
draft.formats.advanced[i] = *crop;
draft.formats.advanced[i] = imported_crop;
}
}
self.finish_drag();

View File

@ -417,3 +417,91 @@ fn exact_import_keeps_nonzero_crop_and_mask() {
assert_eq!(layout.formats().advanced[i], crop);
assert_eq!(layout.formats().masks[i], mask);
}
#[test]
fn midra_rounded_fit_imports_as_exact_smart_crop_without_moving_container_or_center() {
use canvas_pilot::formats::{LayerAppearance, Mask, Source};
// Read-only observation: Midra Screen 2 Layer 2, 2026-11-19.
let crop = Crop {
left: 408.75623712520024,
right: 460.02655069810027,
top: 262.6871137560082,
bottom: 225.98672465094987,
};
let g = Geometry {
x: 552.0,
y: 260.5,
width: 1296.0,
height: 729.0,
};
for fit in [LayerFit::Centered, LayerFit::Cropped] {
let mut layout = Layout::default();
let before = Project::from_layout(&layout);
let mask = Mask {
left: 0.1,
..Default::default()
};
layout
.import_device_layers_with_appearance(
&[(
Some(before.layers[0].id),
g,
Some((Source::default(), crop)),
)],
&[LayerAppearance {
fit: Some(fit),
mask,
}],
)
.unwrap();
let i = 0;
assert_eq!(layout.formats().layer_fit[i], None);
assert_eq!(layout.layers()[i], g);
assert_eq!(layout.formats().masks[i], mask);
let normalized = layout.formats().advanced[i];
near(normalized.left - normalized.right, crop.left - crop.right);
near(normalized.top - normalized.bottom, crop.top - crop.bottom);
let mapping = layout.formats().mapping(i, g).unwrap();
near(mapping.retained[0] * mapping.scale, g.width);
near(mapping.retained[1] * mapping.scale, g.height);
assert!(normalized.left >= crop.left && normalized.top >= crop.top);
layout.undo();
assert_eq!(Project::from_layout(&layout), before);
}
}
#[test]
fn rounded_import_tolerance_is_bounded_and_does_not_change_native_or_stretch_modes() {
use canvas_pilot::formats::Source;
for (fit, width, normal) in [
(LayerFit::Centered, 1919.0, true),
(LayerFit::Centered, 1918.0, false),
(LayerFit::Cropped, 1919.0, true),
(LayerFit::Cropped, 1918.0, false),
(LayerFit::Fullscreen, 1919.0, false),
(LayerFit::OneToOne, 1919.0, false),
] {
let mut layout = Layout::default();
let g = Geometry {
x: 0.0,
y: 0.0,
width,
height: 1080.0,
};
layout
.import_device_layers_with_fit(
&[(None, g, Some((Source::default(), Crop::default())))],
&[Some(fit)],
)
.unwrap();
let i = layout.layers().len() - 1;
assert_eq!(
layout.formats().layer_fit[i].is_none(),
normal,
"{fit:?} {width}"
);
if normal {
layout.formats().mapping(i, g).unwrap();
}
}
}

View File

@ -6,6 +6,11 @@
### v0.10.0-rc.13
- Tolerate up to one output pixel of device rounding when importing Centered/Cropped
fits. Normalize to an exact Smart crop without moving the container or source
center; preserve larger mismatches and stretched/native modes. Regression tests
include the observed Midra crop fractions that left a 0.036-pixel width gap.
- Import exact device fits as normal Smart crop. Preserve advanced modes for
letterboxing, overflow and stretching, and preserve saved project choices.
- Add green advanced edge-crop handles with a frozen image during dragging and

View File

@ -197,8 +197,11 @@ to centered cover framing. Pending Source edits still require Apply.
Device loads use Smart crop when the retained picture exactly fills the container
with uniform scaling and the crop is representable by the normal algorithm.
Letterboxing, overflow and stretching retain their advanced fit. Existing saved
projects keep their explicitly chosen modes.
Centered/Cropped fits with rounding differences of up to one output pixel per
image dimension also use Smart crop: a tiny symmetric crop adjustment restores
the exact aspect while preserving the source center and layer container. Larger
letterboxing/overflow differences and stretching retain their advanced fit.
Existing saved projects keep their explicitly chosen modes.
Sends use container dimensions regardless of sizing strategy, and never change
input routing. Aquilon sends reject advanced fit until its adapter supports it.
Projects/recovery preserve the mode. Pixel rounding is approximate pending the

View File

@ -221,7 +221,13 @@ Adapter snapshots retain the device fit. On import into the editor, an explicit
fit becomes normal (`null`) only if its retained-image width/height equal the
container within 1e-7 output pixels, horizontal/vertical scales differ by less than
1e-10, and the normal crop mapping validates. Source, crop, container and mask are
unchanged. This applies to linked and unlinked loads; project deserialization does
not normalize saved modes. Advanced edge-helper drags stage crop changes until
unchanged. Centered/Cropped fits also accept up to one output pixel of difference
in each retained-image dimension. These near-matches trim the retained source
symmetrically to the exact container aspect, preserving its source-space center;
the resulting normal mapping must validate (including zoom limits). This never
reveals new source pixels. Larger differences, stretching and native 1:1 modes
retain their explicit mode unless they meet the exact-match rule above. This
applies to linked and unlinked loads; project deserialization does not normalize
saved modes. Advanced edge-helper drags stage crop changes until
release, keeping the source transform fixed; cancellation discards the staged crop
and release creates one undo entry. This is editor behavior, not device acceptance.