Handle Midra cropped input snapshots in the crop editor

This commit is contained in:
Dandaedre 2026-11-18 16:31:37 +01:00
parent 9f5a8d737d
commit 0a13642c8f
11 changed files with 356 additions and 51 deletions

2
Cargo.lock generated
View File

@ -602,7 +602,7 @@ dependencies = [
[[package]]
name = "canvas-pilot"
version = "0.9.71"
version = "0.9.72"
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.71"
version = "0.9.72"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/canvas-pilot"

View File

@ -0,0 +1,29 @@
//! Explicit read-only check of input capture framing; never changes device settings.
use canvas_pilot::{device, device_capture, device_inputs::InputDraft};
fn main() -> Result<(), String> {
let mut args = std::env::args().skip(1);
let host = args
.next()
.ok_or("Usage: read_input_capture DEVICE_IP INPUT_NUMBER")?;
let input: u32 = args
.next()
.ok_or("Input number required")?
.parse()
.map_err(|_| "Invalid input number")?;
let before = device::fetch_input(&host, input)?;
let draft = InputDraft::read(&before, input)?;
let bytes = device_capture::capture_for(&host, before.profile, input)?;
let image = image::load_from_memory(&bytes).map_err(|e| e.to_string())?;
let size = [image.width(), image.height()];
let after = device::fetch_input(&host, input)?;
draft.changes(&after)?;
let geometry = device_capture::capture_geometry(&before, input, size)?;
if geometry != device_capture::capture_geometry(&after, input, size)? {
return Err("Capture geometry changed during the request".into());
}
println!(
"{} · input {input} · PNG {} × {} · {geometry:?}",
before.identity, size[0], size[1]
);
Ok(())
}

View File

@ -359,17 +359,14 @@ impl Editor {
}
let after = canvas_pilot::device::fetch_input(&host, input)?;
draft.changes(&after)?;
let geometry = if profile == canvas_pilot::device::Profile::Aquilon6 {
draft.native.filter(|n|((reference.size[0] as f64/reference.size[1] as f64)/(n[0]/n[1])-1.).abs()<0.01).and_then(|native| {
let base=format!("/device/inputList/items/IN_{input}/plugList/items/{}/settings/cropping/status/pp",draft.plug);
let edge=|key|before.raw.pointer(&format!("{base}/{key}")).and_then(serde_json::Value::as_f64);
let crop=canvas_pilot::formats::Crop {left:edge("left")?,right:edge("right")?,top:edge("top")?,bottom:edge("bottom")?};
crop.remaining(native).ok()?;
Some(super::inputs_window::CaptureGeometry {native,crop})
})
} else {
None
};
let geometry = device_capture::capture_geometry(&before, input, reference.size)
.ok()
.filter(|geometry| {
device_capture::capture_geometry(&after, input, reference.size)
.ok()
.as_ref()
== Some(geometry)
});
let _ = geometry_tx.send(geometry);
}
Ok(reference)
@ -1564,6 +1561,7 @@ mod tests {
let (geometry_tx, geometry_rx) = std::sync::mpsc::channel();
geometry_tx
.send(Some(super::super::inputs_window::CaptureGeometry {
framing: device_capture::CaptureFraming::FullFrame,
native: [1920., 1080.],
crop: canvas_pilot::formats::Crop::default(),
}))

View File

@ -50,6 +50,128 @@ pub fn inputs(snapshot: &Snapshot) -> Vec<Input> {
inputs
}
/// How the PNG represents the applied input crop.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum CaptureFraming {
/// Aquilon retains full-source coordinates, blanking the cropped edges.
FullFrame,
/// Midra returns only the retained image, without the removed edges.
Cropped,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CaptureGeometry {
pub native: [f64; 2],
pub crop: crate::formats::Crop,
pub framing: CaptureFraming,
}
/// Interpret device readback, never an unsent editor draft. Callers must also
/// verify that input state stayed unchanged across the capture request.
pub fn capture_geometry(
snapshot: &Snapshot,
input: u32,
image_size: [u32; 2],
) -> Result<CaptureGeometry, String> {
use crate::device::Profile;
let draft = crate::device_inputs::InputDraft::read(snapshot, input)?;
let native = draft.native.ok_or("No valid input signal dimensions")?;
let prefix = if snapshot.profile == Profile::Aquilon6 {
"IN"
} else {
"INPUT"
};
let base = format!(
"/device/inputList/items/{prefix}_{input}/plugList/items/{}",
draft.plug
);
let plug = snapshot
.raw
.pointer(&base)
.ok_or("Input plug unavailable")?;
if plug
.pointer("/control/pp/enableCropFinder")
.and_then(serde_json::Value::as_bool)
== Some(true)
{
return Err("Disable Crop Finder to preview the applied input crop".into());
}
let (crop, framing) = match snapshot.profile {
Profile::Aquilon6 => {
let status = plug
.pointer("/settings/cropping/status/pp")
.ok_or("Applied input crop unavailable")?;
let edge = |key: &str| {
status
.get(key)
.and_then(serde_json::Value::as_f64)
.ok_or("Applied crop edge unavailable")
};
(
crate::formats::Crop {
left: edge("left")?,
right: edge("right")?,
top: edge("top")?,
bottom: edge("bottom")?,
},
CaptureFraming::FullFrame,
)
}
Profile::Midra4k => {
// Midra exposes crop controls plus the resulting image dimensions,
// not Aquilon's separate cropping/status object.
let controls = plug
.pointer("/settings/cropping/control/pp")
.ok_or("Input crop unavailable")?;
let edge = |key: &str| {
controls
.get(key)
.and_then(serde_json::Value::as_f64)
.ok_or("Crop edge unavailable")
};
(
crate::formats::Crop {
left: edge("left")?,
right: edge("right")?,
top: edge("top")?,
bottom: edge("bottom")?,
},
CaptureFraming::Cropped,
)
}
};
let retained = crop.remaining(native)?;
let expected = match framing {
CaptureFraming::FullFrame => native,
CaptureFraming::Cropped => {
for (key, expected) in [("imageWidth", retained[0]), ("imageHeight", retained[1])] {
let actual = plug
.pointer(&format!("/status/signal/pp/{key}"))
.and_then(serde_json::Value::as_f64)
.ok_or("Applied image dimensions unavailable")?;
if (actual - expected).abs() > 1.0 {
return Err(
"Input crop has not settled; waiting for matching image dimensions".into(),
);
}
}
retained
}
};
if image_size.contains(&0) {
return Err("Empty capture image".into());
}
let longest = usize::from(expected[1] > expected[0]);
let scale = image_size[longest] as f64 / expected[longest];
if (0..2).any(|axis| (image_size[axis] as f64 - expected[axis] * scale).abs() > 1.1) {
return Err("Capture dimensions do not match the applied input crop".into());
}
Ok(CaptureGeometry {
native,
crop,
framing,
})
}
/// A single on-demand GET; no routing, capture-library or device-setting writes.
pub fn capture(host: &str, input: u32) -> Result<Vec<u8>, String> {
capture_for(host, crate::device::Profile::Midra4k, input)
@ -155,6 +277,87 @@ mod tests {
assert!(capture("not a host", 1).is_err());
assert!(capture("127.0.0.1", 0).is_err());
}
fn geometry_snapshot(profile: crate::device::Profile, crop: [f64; 4]) -> Snapshot {
let [left, right, top, bottom] = crop;
let prefix = if profile == crate::device::Profile::Aquilon6 {
"IN"
} else {
"INPUT"
};
Snapshot {
profile,
identity: "capture geometry fixture".into(),
screens: vec![],
raw: serde_json::json!({"device":{"inputList":{"items":{format!("{prefix}_6"):{
"status":{"pp":{"isAvailable":true,"plug":"1"}},
"plugList":{"items":{"1":{
"control":{"pp":{"enableCropFinder":false}},
"status":{"signal":{"pp":{"isValid":true,"aspectWidth":1920,"aspectHeight":1080,"imageWidth":1920.-left-right,"imageHeight":1080.-top-bottom}}},
"settings":{"cropping":{
"control":{"pp":{"left":left,"right":right,"top":top,"bottom":bottom,"predefined":"NONE"}},
"status":{"pp":{"left":left,"right":right,"top":top,"bottom":bottom}}
}}
}}}
}}}}}),
}
}
#[test]
fn midra_capture_uses_retained_dimensions_including_portrait_and_pixel_rounding() {
use crate::device::Profile;
for (crop, size) in [
([0.; 4], [512, 288]),
([1157., 0., 0., 0.], [361, 512]),
([279., 0., 0., 338.], [512, 231]),
] {
let mut snapshot = geometry_snapshot(Profile::Midra4k, crop);
// Actual Eikos 4K state has no cropping/status subtree.
snapshot
.raw
.pointer_mut("/device/inputList/items/INPUT_6/plugList/items/1/settings/cropping")
.unwrap()
.as_object_mut()
.unwrap()
.remove("status");
let geometry = capture_geometry(&snapshot, 6, size).unwrap();
assert_eq!(geometry.framing, CaptureFraming::Cropped);
assert_eq!(geometry.native, [1920., 1080.]);
assert_eq!(geometry.crop.left, crop[0]);
}
}
#[test]
fn aquilon_capture_keeps_full_frame_coordinates_and_applied_status() {
let mut snapshot =
geometry_snapshot(crate::device::Profile::Aquilon6, [480., 480., 270., 270.]);
// Pending controls do not describe the captured pixels.
snapshot.raw["device"]["inputList"]["items"]["IN_6"]["plugList"]["items"]["1"]["settings"]
["cropping"]["control"]["pp"]["left"] = serde_json::json!(0);
let geometry = capture_geometry(&snapshot, 6, [512, 288]).unwrap();
assert_eq!(geometry.framing, CaptureFraming::FullFrame);
assert_eq!(geometry.crop.left, 480.);
assert!(capture_geometry(&snapshot, 6, [361, 512]).is_err());
}
#[test]
fn capture_geometry_rejects_unsettled_crop_missing_edges_finder_and_stale_image() {
let snapshot = geometry_snapshot(crate::device::Profile::Midra4k, [1157., 0., 0., 0.]);
assert!(capture_geometry(&snapshot, 6, [512, 288]).is_err());
assert!(capture_geometry(&snapshot, 6, [0, 512]).is_err());
for (path, value) in [
("status/signal/pp/imageWidth", serde_json::json!(1920)),
("status/signal/pp/isValid", serde_json::json!(false)),
("settings/cropping/control/pp/left", serde_json::Value::Null),
("control/pp/enableCropFinder", serde_json::json!(true)),
] {
let mut changed = snapshot.clone();
*changed
.raw
.pointer_mut(&format!(
"/device/inputList/items/INPUT_6/plugList/items/1/{path}"
))
.unwrap() = value;
assert!(capture_geometry(&changed, 6, [361, 512]).is_err(), "{path}");
}
}
#[test]
fn input_choices_use_available_hardware_and_current_plug_signal() {
let snapshot = Snapshot {

View File

@ -10,33 +10,38 @@ use std::collections::BTreeMap;
type Values = BTreeMap<String, Value>;
type CropDrag = (Crop, Pos2, [i8; 2], Values);
type SendJob = std::sync::mpsc::Receiver<Result<canvas_pilot::device::Snapshot, String>>;
#[derive(Clone, Copy, Debug)]
pub(super) struct CaptureGeometry {
pub native: [f64; 2],
pub crop: Crop,
}
impl CaptureGeometry {
pub fn rectangles(self, full: Rect, native: [f64; 2]) -> Option<(Rect, Rect)> {
if self.native != native || self.crop.remaining(native).is_err() {
return None;
}
let uv = Rect::from_min_max(
Pos2::new(
(self.crop.left / native[0]) as f32,
(self.crop.top / native[1]) as f32,
),
Pos2::new(
(1. - self.crop.right / native[0]) as f32,
(1. - self.crop.bottom / native[1]) as f32,
),
);
let rect = Rect::from_min_max(
full.min + uv.min.to_vec2() * full.size(),
full.min + uv.max.to_vec2() * full.size(),
);
Some((rect, uv))
pub(super) use canvas_pilot::device_capture::CaptureGeometry;
fn capture_rectangles(
geometry: CaptureGeometry,
full: Rect,
native: [f64; 2],
) -> Option<(Rect, Rect)> {
if geometry.native != native || geometry.crop.remaining(native).is_err() {
return None;
}
let region = Rect::from_min_max(
Pos2::new(
(geometry.crop.left / native[0]) as f32,
(geometry.crop.top / native[1]) as f32,
),
Pos2::new(
(1. - geometry.crop.right / native[0]) as f32,
(1. - geometry.crop.bottom / native[1]) as f32,
),
);
let rect = Rect::from_min_max(
full.min + region.min.to_vec2() * full.size(),
full.min + region.max.to_vec2() * full.size(),
);
let uv = match geometry.framing {
canvas_pilot::device_capture::CaptureFraming::FullFrame => region,
canvas_pilot::device_capture::CaptureFraming::Cropped => {
Rect::from_min_max(Pos2::ZERO, Pos2::new(1., 1.))
}
};
Some((rect, uv))
}
#[derive(Default)]
pub(super) struct InputsWindow {
pub open: bool,
@ -440,7 +445,7 @@ fn crop_editor(
content_painter.set_opacity(1.0);
super::format_ui::paint_pattern_clipped(&content_painter, full, source, Crop::default(), full);
if let Some((texture, geometry)) = preview
&& let Some((known, uv)) = geometry.rectangles(full, native)
&& let Some((known, uv)) = capture_rectangles(geometry, full, native)
{
content_painter.image(texture, known, uv, Color32::WHITE);
}
@ -558,6 +563,7 @@ mod tests {
Default::default(),
);
let geometry = CaptureGeometry {
framing: canvas_pilot::device_capture::CaptureFraming::FullFrame,
native: [1920., 1080.],
crop: Crop {
left: 480.,
@ -725,6 +731,7 @@ mod tests {
#[test]
fn captured_pixels_stay_in_original_source_region_when_crop_expands() {
let geometry = CaptureGeometry {
framing: canvas_pilot::device_capture::CaptureFraming::FullFrame,
native: [1920., 1080.],
crop: Crop {
left: 480.,
@ -734,7 +741,7 @@ mod tests {
},
};
let full = Rect::from_min_size(Pos2::new(10., 20.), Vec2::new(192., 108.));
let (known, uv) = geometry.rectangles(full, [1920., 1080.]).unwrap();
let (known, uv) = capture_rectangles(geometry, full, [1920., 1080.]).unwrap();
assert_eq!(
known,
Rect::from_min_size(Pos2::new(58., 47.), Vec2::new(96., 54.))
@ -743,9 +750,24 @@ mod tests {
uv,
Rect::from_min_max(Pos2::new(0.25, 0.25), Pos2::new(0.75, 0.75))
);
assert!(geometry.rectangles(full, [1280., 720.]).is_none());
let cropped = CaptureGeometry {
framing: canvas_pilot::device_capture::CaptureFraming::Cropped,
..geometry
};
let (cropped_rect, cropped_uv) = capture_rectangles(cropped, full, [1920., 1080.]).unwrap();
assert_eq!(cropped_rect, known);
assert_eq!(
cropped_uv,
Rect::from_min_max(Pos2::ZERO, Pos2::new(1., 1.))
);
assert!(capture_rectangles(geometry, full, [1280., 720.]).is_none());
// Mapping is fixed at capture time, independent of the editable crop box.
assert_eq!(geometry.rectangles(full, [1920., 1080.]).unwrap().0, known);
assert_eq!(
capture_rectangles(geometry, full, [1920., 1080.])
.unwrap()
.0,
known
);
}
fn input_app() -> Editor {
let mut app = Editor::default();

View File

@ -4,6 +4,14 @@
## Unreleased
### v0.9.72
- Fix input-crop capture on Midra: its PNG contains only the retained crop, unlike
Aquilon’s full-frame snapshot. Place it in the applied crop rectangle without
cropping twice, leaving unknown pixels as the sizing grid.
- Validate capture dimensions and before/after device state; read-only checks on
Eikos 4K 3.3.10 covered uncropped, portrait-cropped and asymmetrically cropped input.
### v0.9.71
- Load a local reference picture into a layer’s input preview for offline crop,

View File

@ -267,3 +267,16 @@ replaced; they are not saved in projects or exchange files. Input switching requ
a valid signal with native dimensions matching the editor source format and no
input crop/aspect correction. Unsupported combinations stop preparation with an
error. Sending to Program can visibly change the live source.
### Live picture in the input crop editor
In **Input settings**, **Capture input (live)** places the captured picture inside
its **applied device crop**. Unknown areas keep the sizing grid. Midra returns an
already-cropped picture; Aquilon retains the full frame with blanked edges. Canvas
Pilot handles these separately so the crop is not applied twice.
Moving the draft crop does not move the captured picture. After **Send changes**,
the preview refreshes against device readback. A missing signal or inconsistent
capture geometry keeps the grid until a matching capture arrives. Disable
**Crop Finder** in WebRCS when using this preview.

View File

@ -347,8 +347,8 @@ validation and verified readback.
stretches those pixels. After Send, the old capture is cleared while settings
are applied and verified; **Refreshing capture…** remains until a new capture
arrives. Unverified capture geometry shows the grid rather than a distorted image.
Aquilon captures are supported for the verified full-frame thumbnail geometry;
no raw/pre-crop endpoint is confirmed.
Midra’s already-cropped PNG and Aquilon’s full-frame thumbnail are handled
separately, avoiding a second crop. No raw/pre-crop endpoint is confirmed.
Drafts are session-only and are not included in projects or exports. Switching

View File

@ -324,13 +324,18 @@ opted-in layer/dialog consumers for the same input. It never changes input crop
to obtain a raw image. Sends clear the preview/key and suspend capture scheduling
until apply/readback completes; late results from invalidated requests are ignored.
The input crop editor uses the existing sizing pattern behind available pixels.
For Aquilon, fresh reads bracket capture; stable crop/aspect/plug state and a matching
thumbnail frame ratio are required for compositing. The image is clipped to the
captured status-crop region using corresponding full-frame UV coordinates; it is
never stretched to a newly edited crop. Unknown pixels retain the pattern. Other
capture geometries show only the grid (no unverified compositing). No raw-capture contract is claimed. Aquilon Cmax 6.2.79 emulator testing showed
blanked cropped edges; Pulse 4K 3.3.7 served an unchanged thumbnail. Input drafts
and captures are not serialized, and existing project/exchange versions are unchanged.
Fresh reads bracket capture for both adapters; stable crop/aspect/plug state and
matching image dimensions are required for compositing. Aquilon uses the applied
status-crop region with corresponding full-frame UV coordinates. Midra uses crop
controls verified against applied image dimensions and places the entire cropped
PNG within that region. Neither is stretched to a newly edited draft crop.
Unknown pixels retain the pattern. See the [capture framing contract and Eikos
4K read-only checks](midra-input-preview.md#input-settings-crop-capture-framing).
No raw-capture contract is claimed. Aquilon Cmax 6.2.79 emulator testing showed
blanked cropped edges; an earlier Pulse 4K 3.3.7 emulator served an unchanged
thumbnail, which must not pass validation if its dimensions conflict with crop
readback. Input drafts and captures are not serialized, and existing
project/exchange versions are unchanged.
Manual diagnostic: `cargo run -p canvas-pilot --example check_device_inputs -- HOST`
changes Input 1 crop and restores the original values. Run only against an expressly

View File

@ -154,3 +154,30 @@ disables automatic capture and Follow input, and rejects late capture results.
The project preview representation retains the original image bytes (see the
[project contract](canvas-pilot.md)). The layer inspector lists these
and captured previews under **Input reference pictures**, below fixed references.
## Input-settings crop capture framing
The input-settings editor reads input state before and after capture, rejecting
changed signal, plug or preprocessing. Capture geometry is resolved in
`device_capture::capture_geometry`, separately from the editable draft:
- **Midra:** source dimensions are `status/signal/pp/aspectWidth` and
`aspectHeight`; crop edges come from `settings/cropping/control/pp`. The applied
`imageWidth`/`imageHeight` must match the retained rectangle. The PNG contains
only that rectangle, so its full texture is placed at the applied crop position.
- **Aquilon:** source dimensions and `settings/cropping/status/pp` describe a
full-source PNG with blanked crop edges. Both placement and texture coordinates
use the retained rectangle; pending control edits are not applied to the image.
Allow one pixel of thumbnail rounding. Invalid signal, unavailable crop values,
unsettled dimensions, Crop Finder or a mismatched image keep the sizing grid.
Draft edits do not move or recrop captured pixels. These are transient rendering
values, not project fields or device writes.
Read-only Eikos 4K 3.3.10 checks on 2026-11-18 observed a 1920×1080 source with
an uncropped 512×288 PNG; a left crop of 1157 produced a 763×1080 retained region
and 361×512 PNG; left 279/bottom 338 produced 1641×742 and 512×231 respectively.
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.