Preserve cropped input picture aspect before layer fitting

This commit is contained in:
Dandaedre 2026-11-18 17:20:37 +01:00
parent ee0b67c6d7
commit b6c4afb511
6 changed files with 139 additions and 7 deletions

2
Cargo.lock generated
View File

@ -602,7 +602,7 @@ dependencies = [
[[package]]
name = "canvas-pilot"
version = "0.9.75"
version = "0.9.76"
dependencies = [
"base64 0.22.1",
"craft-dwg",

View File

@ -4,7 +4,7 @@ default-members = ["crates/canvas-pilot"]
resolver = "3"
[workspace.package]
version = "0.9.75"
version = "0.9.76"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot"

View File

@ -777,12 +777,36 @@ fn paint_source_clipped(
};
let k = rect.width() as f64 / retained[0];
let ky = rect.height() as f64 / retained[1];
// A Midra still already contains the input crop. Fit its actual aspect
// into source space before applying the layer transform; stretching it
// to source.native here distorts even Centered and ordinary crop modes.
// Reduced-resolution captures and local pictures use the same rule.
let picture = painter
.ctx()
.tex_manager()
.read()
.meta(texture)
.map(|meta| meta.size.map(|n| n as f64))
.unwrap_or(input);
let fitted = canvas_pilot::formats::LayerFit::Centered.image(
picture,
canvas_pilot::Geometry {
x: 0.,
y: 0.,
width: input[0],
height: input[1],
},
);
let Ok(fitted) = fitted else {
return;
};
let native = Rect::from_min_size(
rect.min - Vec2::new(((crop.left) * k) as f32, ((crop.top) * ky) as f32),
Vec2::new(
(source.native[0] as f64 * k) as f32,
(source.native[1] as f64 * ky) as f32,
),
rect.min
+ Vec2::new(
((fitted.x - crop.left) * k) as f32,
((fitted.y - crop.top) * ky) as f32,
),
Vec2::new((fitted.width * k) as f32, (fitted.height * ky) as f32),
);
painter
.with_clip_rect(painter.clip_rect().intersect(clip))
@ -1497,6 +1521,84 @@ mod tests {
#[cfg(test)]
mod preview_paint_tests {
use super::*;
#[test]
fn cropped_capture_aspect_survives_layer_fit_and_helpers() {
use canvas_pilot::formats::LayerFit;
for size in [[361, 512], [512, 231], [512, 288]] {
for fit in std::iter::once(None).chain(LayerFit::ALL.into_iter().map(Some)) {
let ctx = egui::Context::default();
let texture = ctx.load_texture(
"cropped-input",
egui::ColorImage::filled(size, Color32::RED),
Default::default(),
);
let mut app = Editor::default();
if fit.is_some() {
app.layout
.set_layer_fit(
1,
fit,
Crop {
left: 120.,
right: 80.,
top: 30.,
bottom: 60.,
},
)
.unwrap();
}
let g = app.layout.layers()[1];
let rect = Rect::from_min_size(
Pos2::new(100., 100.),
Vec2::new(g.width as f32, g.height as f32) * 0.25,
);
let before = app.project();
let output = ctx.run_ui(egui::RawInput::default(), |ui| {
paint_layer_source(
ui.painter(),
rect,
&app.layout,
1,
Some((texture.id(), 0.0)),
);
paint_mask_helper(
ui.painter(),
rect,
&app.layout,
1,
Some((texture.id(), 0.0)),
);
});
let meshes: Vec<_> = output
.shapes
.iter()
.filter_map(|shape| match &shape.shape {
egui::Shape::Mesh(mesh) if mesh.texture_id == texture.id() => Some(mesh),
_ => None,
})
.collect();
assert!(!meshes.is_empty());
let retained = app.layout.formats().advanced[1]
.remaining(app.layout.formats().input(1).unwrap())
.unwrap();
let stretch = if fit == Some(LayerFit::Fullscreen) {
(g.width / retained[0]) / (g.height / retained[1])
} else {
1.
};
for mesh in meshes {
let bounds = mesh.calc_bounds();
let expected = size[0] as f64 / size[1] as f64 * stretch;
assert!(
(bounds.aspect_ratio() as f64 - expected).abs() < 0.00001,
"{size:?} {fit:?}: {bounds:?}"
);
}
assert_eq!(app.project(), before);
}
}
}
#[test]
fn masked_helper_is_translucent_hatched_and_confined_to_hidden_regions() {
use canvas_pilot::formats::{LayerFit, Mask, MaskOrigin};

View File

@ -4,6 +4,12 @@
## Unreleased
### v0.9.76
- Preserve the actual picture ratio when fitting input captures and local reference
pictures into source space. Midra cropped captures no longer stretch back to
the declared source ratio before layer crop, zoom, fit and mask are applied.
### v0.9.75
- Highlight Send changes when an input has pending edits. Keep view zoom, panning

View File

@ -374,3 +374,12 @@ inputs remains pending; the removal of local pre-crop does not claim that suppor
The old local source pre-crop model and saved input proposals have been removed.
Projects retain processed source dimensions and independent layer framing; legacy
native projects can be opened with a reported best-effort recovery.
### Captured picture proportions
Input captures and local reference pictures fit centered inside the source format
without changing their ratio. Midra captures already include the input crop; they
are not stretched back to the uncropped signal size. The layer’s crop, zoom, fit
and mask then apply normally. **Centered (fit)** preserves proportions;
**Fullscreen (stretch)** still deliberately stretches. This preview does not
change source dimensions or device input settings.

View File

@ -181,3 +181,18 @@ and 361×512 PNG; left 279/bottom 338 produced 1641×742 and 512×231 respective
Only numeric framing observations are recorded; source images are not fixtures.
Run `cargo run --example read_input_capture -- DEVICE_IP INPUT_NUMBER` for an
explicit read-only before/capture/after check.
## Layer preview aspect handling
The decoded picture is centered at the largest size that fits the declared source
rectangle while preserving its own aspect ratio. This happens before layer crop,
zoom, fit and mask, including the crop/mask helper views. Midra captures already
contain the retained input crop: do not stretch them back to the original signal
ratio or apply that input crop a second time. Local reference pictures use the same
fit rule. Layer Fullscreen remains an explicit stretch of the source-space image.
The input-settings editor separately places captures at their applied crop position
(as described above); it does not use this layer-preview fit.
This affects rendering only. Stored image bytes, source dimensions, project schema
and device transfers are unchanged. Input-settings/global fit inheritance is not
inferred from a PNG; this does not add support for those device modes.