Import reference pictures with non-blocking scaling warnings
This commit is contained in:
parent
11df894b32
commit
2f39cd3001
|
|
@ -2396,6 +2396,8 @@ name = "midra-layout"
|
|||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"eframe",
|
||||
"image",
|
||||
"rfd",
|
||||
"roxmltree",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -9,3 +9,5 @@ description = "Offline Midra layer positioning and draw.io geometry import"
|
|||
[dependencies]
|
||||
roxmltree = "0.20"
|
||||
eframe = { version = "0.31", default-features = false, features = ["default_fonts", "glow", "x11", "wayland"] }
|
||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "bmp"] }
|
||||
rfd = "0.15"
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ pub struct Editor {
|
|||
panning: bool,
|
||||
marquee: Option<Marquee>,
|
||||
align_target: AlignTarget,
|
||||
reference: Option<super::reference::Reference>,
|
||||
reference_open: bool,
|
||||
reference_error: String,
|
||||
}
|
||||
#[derive(Clone, Copy)]
|
||||
struct Marquee {
|
||||
|
|
@ -57,6 +60,9 @@ impl Default for Editor {
|
|||
panning: false,
|
||||
marquee: None,
|
||||
align_target: AlignTarget::Selection,
|
||||
reference: None,
|
||||
reference_open: false,
|
||||
reference_error: String::new(),
|
||||
};
|
||||
this.sync();
|
||||
this
|
||||
|
|
@ -82,6 +88,69 @@ fn bounds(g: Geometry) -> String {
|
|||
)
|
||||
}
|
||||
impl Editor {
|
||||
fn load_reference(&mut self, ctx: &egui::Context, file: &egui::DroppedFile) {
|
||||
match super::reference::Reference::load(ctx, file) {
|
||||
Ok(reference) => {
|
||||
self.reference = Some(reference);
|
||||
self.reference_error.clear();
|
||||
}
|
||||
Err(error) => self.reference_error = error,
|
||||
}
|
||||
}
|
||||
fn reference_window(&mut self, ctx: &egui::Context) {
|
||||
let mut open = self.reference_open;
|
||||
egui::Window::new("Reference image")
|
||||
.open(&mut open)
|
||||
.default_width(420.0)
|
||||
.show(ctx, |ui| {
|
||||
if ui.button("Import picture…").clicked()
|
||||
&& let Some(path) = rfd::FileDialog::new()
|
||||
.add_filter("Pictures", &["png", "jpg", "jpeg", "bmp"])
|
||||
.pick_file()
|
||||
{
|
||||
self.load_reference(
|
||||
ctx,
|
||||
&egui::DroppedFile {
|
||||
path: Some(path),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
ui.label("Or drag one PNG, JPEG or BMP into the window.");
|
||||
let mut remove = false;
|
||||
if let Some(reference) = &mut self.reference {
|
||||
ui.label(format!(
|
||||
"{} · {} × {} px",
|
||||
reference.name, reference.size[0], reference.size[1]
|
||||
));
|
||||
ui.checkbox(&mut reference.visible, "Show reference");
|
||||
ui.add(egui::Slider::new(&mut reference.opacity, 0.0..=1.0).text("Opacity"));
|
||||
ui.radio_value(
|
||||
&mut reference.preserve_ratio,
|
||||
false,
|
||||
"Fill screen (stretch if needed)",
|
||||
);
|
||||
ui.radio_value(
|
||||
&mut reference.preserve_ratio,
|
||||
true,
|
||||
"Fit inside (preserve proportions)",
|
||||
);
|
||||
if let Some((severe, warning)) = reference.warning() {
|
||||
ui.colored_label(if severe { Color32::LIGHT_RED } else { AMBER }, warning);
|
||||
}
|
||||
remove = ui.button("Remove reference").clicked();
|
||||
}
|
||||
if remove {
|
||||
self.reference = None;
|
||||
self.reference_error.clear();
|
||||
}
|
||||
if !self.reference_error.is_empty() {
|
||||
ui.colored_label(Color32::LIGHT_RED, &self.reference_error);
|
||||
}
|
||||
ui.label("Reference stays fixed behind layers. Warnings never block placement.");
|
||||
});
|
||||
self.reference_open = open;
|
||||
}
|
||||
fn sync(&mut self) {
|
||||
if let Some(i) = self.layout.selected() {
|
||||
let g = self.layout.layers()[i];
|
||||
|
|
@ -676,8 +745,29 @@ impl Editor {
|
|||
if click(ui, &ink, [438.0, 67.0, 154.0, 34.0], "toolbar-paste") {
|
||||
self.open_paste(String::new());
|
||||
}
|
||||
ink.text(25.0, 132.0, "YOUR SCREEN, PRECISELY PLACED", 12.0, CYAN);
|
||||
ink.text(25.0, 157.0, "Select. Paste. Place.", 17.0, MUTED);
|
||||
ink.button(438.0, 20.0, 154.0, "Reference image…", false);
|
||||
if click(ui, &ink, [438.0, 20.0, 154.0, 34.0], "reference-settings") {
|
||||
self.reference_open = true;
|
||||
}
|
||||
let (text, color) = if !self.reference_error.is_empty() {
|
||||
(self.reference_error.clone(), Color32::LIGHT_RED)
|
||||
} else if let Some((severe, warning)) = self.reference.as_ref().and_then(|r| r.warning()) {
|
||||
(warning, if severe { Color32::LIGHT_RED } else { AMBER })
|
||||
} else {
|
||||
(
|
||||
self.reference
|
||||
.as_ref()
|
||||
.map(|r| format!("Reference: {} · {} × {} px", r.name, r.size[0], r.size[1]))
|
||||
.unwrap_or_else(|| {
|
||||
"Drop a reference picture here, or choose Reference image…".into()
|
||||
}),
|
||||
MUTED,
|
||||
)
|
||||
};
|
||||
ui.put(
|
||||
Rect::from_min_size(ink.point(25.0, 129.0), Vec2::new(r.width() - 50.0, 58.0)),
|
||||
egui::Label::new(egui::RichText::new(text).color(color)).wrap(),
|
||||
);
|
||||
let area = Rect::from_min_max(
|
||||
r.min + Vec2::new(20.0, 204.0),
|
||||
r.max - Vec2::new(20.0, 85.0),
|
||||
|
|
@ -791,7 +881,14 @@ impl Editor {
|
|||
origin,
|
||||
scale: scale * 2.0,
|
||||
};
|
||||
screen_artwork(&screen);
|
||||
if let Some(reference) = &self.reference {
|
||||
reference.paint(
|
||||
&screen.painter,
|
||||
Rect::from_min_size(origin, Vec2::new(1920.0, 1080.0) * scale),
|
||||
);
|
||||
} else {
|
||||
screen_artwork(&screen);
|
||||
}
|
||||
for (i, g) in self.layout.layers().iter().enumerate() {
|
||||
if !self.layout.visible(i) {
|
||||
continue;
|
||||
|
|
@ -809,7 +906,11 @@ impl Editor {
|
|||
p.rect(
|
||||
rect,
|
||||
0.0,
|
||||
Color32::from_rgb(23, 59, 80),
|
||||
if self.reference.as_ref().is_some_and(|r| r.visible) {
|
||||
Color32::from_rgba_unmultiplied(23, 59, 80, 35)
|
||||
} else {
|
||||
Color32::from_rgb(23, 59, 80)
|
||||
},
|
||||
Stroke::new(1.0_f32, color),
|
||||
StrokeKind::Inside,
|
||||
);
|
||||
|
|
@ -928,7 +1029,7 @@ impl Editor {
|
|||
ink.text(
|
||||
24.0,
|
||||
r.height() - 31.0,
|
||||
"Scroll to zoom · Space-drag to pan · Background is illustrative",
|
||||
"Scroll to zoom · Space-drag to pan · Reference image stays fixed",
|
||||
12.0,
|
||||
MUTED,
|
||||
);
|
||||
|
|
@ -974,6 +1075,15 @@ fn click(ui: &egui::Ui, ink: &Ink, b: [f32; 4], id: impl std::hash::Hash) -> boo
|
|||
}
|
||||
impl Editor {
|
||||
fn ui(&mut self, ctx: &egui::Context) {
|
||||
let dropped = ctx.input(|i| i.raw.dropped_files.clone());
|
||||
if !dropped.is_empty() {
|
||||
if dropped.len() == 1 {
|
||||
self.load_reference(ctx, &dropped[0]);
|
||||
} else {
|
||||
self.reference_error =
|
||||
"Drop one reference image at a time; current image kept.".into();
|
||||
}
|
||||
}
|
||||
self.shortcuts(ctx);
|
||||
egui::TopBottomPanel::top("header")
|
||||
.exact_height(64.0)
|
||||
|
|
@ -1035,6 +1145,7 @@ impl Editor {
|
|||
.frame(egui::Frame::NONE.fill(BG))
|
||||
.show(ctx, |ui| self.workspace(ui));
|
||||
self.paste_window(ctx);
|
||||
self.reference_window(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1065,6 +1176,48 @@ mod tests {
|
|||
}
|
||||
}
|
||||
#[test]
|
||||
fn dropped_reference_warns_without_editing_layers_and_failed_replace_keeps_image() {
|
||||
let mut app = Editor::default();
|
||||
let ctx = egui::Context::default();
|
||||
let before = *app.layout.layers();
|
||||
let pixels = image::RgbaImage::from_pixel(16, 10, image::Rgba([50, 90, 120, 255]));
|
||||
let mut bytes = std::io::Cursor::new(Vec::new());
|
||||
pixels
|
||||
.write_to(&mut bytes, image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let _ = ctx.run(
|
||||
egui::RawInput {
|
||||
screen_rect: Some(Rect::from_min_size(Pos2::ZERO, Vec2::new(1440.0, 940.0))),
|
||||
dropped_files: vec![egui::DroppedFile {
|
||||
name: "reference.png".into(),
|
||||
bytes: Some(bytes.into_inner().into()),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
},
|
||||
|ctx| app.ui(ctx),
|
||||
);
|
||||
assert!(app.reference.as_ref().unwrap().warning().unwrap().0);
|
||||
assert_eq!(*app.layout.layers(), before);
|
||||
let output = frame(&mut app, &ctx, vec![]);
|
||||
assert!(output.shapes.iter().any(|s| matches!(&s.shape, egui::epaint::Shape::Text(t) if t.galley.job.text.contains("SEVERE WARNING"))));
|
||||
let texture = app.reference.as_ref().unwrap().texture.id();
|
||||
app.load_reference(
|
||||
&ctx,
|
||||
&egui::DroppedFile {
|
||||
bytes: Some(Vec::from(b"broken").into()),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
assert!(!app.reference_error.is_empty());
|
||||
assert_eq!(app.reference.as_ref().unwrap().texture.id(), texture);
|
||||
app.layout.nudge(1.0, 0.0).unwrap();
|
||||
assert_eq!(app.layout.layers()[1].x, before[1].x + 1.0);
|
||||
app.layout.undo();
|
||||
assert_eq!(*app.layout.layers(), before);
|
||||
assert_eq!(app.reference.as_ref().unwrap().texture.id(), texture);
|
||||
}
|
||||
#[test]
|
||||
fn paste_opens_review_without_editing_and_escape_cancels() {
|
||||
let mut app = Editor::default();
|
||||
let ctx = egui::Context::default();
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ fn main() -> eframe::Result {
|
|||
}
|
||||
|
||||
mod gui;
|
||||
mod reference;
|
||||
|
||||
/// Draw in local logical points; viewport artwork alone uses a fitted scale.
|
||||
struct Ink {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,144 @@
|
|||
use super::*;
|
||||
use std::io::{Cursor, Read};
|
||||
|
||||
pub(super) struct Reference {
|
||||
pub texture: egui::TextureHandle,
|
||||
pub name: String,
|
||||
pub size: [u32; 2],
|
||||
pub visible: bool,
|
||||
pub opacity: f32,
|
||||
pub preserve_ratio: bool,
|
||||
}
|
||||
|
||||
impl Reference {
|
||||
pub fn load(ctx: &egui::Context, file: &egui::DroppedFile) -> Result<Self, String> {
|
||||
let bytes = if let Some(bytes) = &file.bytes {
|
||||
bytes.to_vec()
|
||||
} else if let Some(path) = &file.path {
|
||||
let mut bytes = Vec::new();
|
||||
std::fs::File::open(path)
|
||||
.map_err(|e| format!("Cannot open reference image: {e}"))?
|
||||
.take(64 * 1024 * 1024 + 1)
|
||||
.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Cannot read reference image: {e}"))?;
|
||||
bytes
|
||||
} else {
|
||||
return Err("Drop a PNG, JPEG or BMP file.".into());
|
||||
};
|
||||
let rgba = decode(&bytes, ctx.input(|i| i.max_texture_side) as u32)?;
|
||||
let size = [rgba.width(), rgba.height()];
|
||||
let color = egui::ColorImage::from_rgba_unmultiplied(
|
||||
[size[0] as usize, size[1] as usize],
|
||||
rgba.as_raw(),
|
||||
);
|
||||
let name = file
|
||||
.path
|
||||
.as_ref()
|
||||
.and_then(|p| p.file_name())
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| file.name.clone());
|
||||
Ok(Self {
|
||||
texture: ctx.load_texture("reference", color, egui::TextureOptions::NEAREST),
|
||||
name,
|
||||
size,
|
||||
visible: true,
|
||||
opacity: 1.0,
|
||||
preserve_ratio: false,
|
||||
})
|
||||
}
|
||||
pub fn warning(&self) -> Option<(bool, String)> {
|
||||
warning(self.size, self.preserve_ratio)
|
||||
}
|
||||
pub fn paint(&self, painter: &egui::Painter, screen: Rect) {
|
||||
painter.rect_filled(screen, 0.0, BG);
|
||||
if !self.visible {
|
||||
return;
|
||||
}
|
||||
let rect = if self.preserve_ratio {
|
||||
let size = Vec2::new(self.size[0] as f32, self.size[1] as f32);
|
||||
let scale = (screen.width() / size.x).min(screen.height() / size.y);
|
||||
Rect::from_center_size(screen.center(), size * scale)
|
||||
} else {
|
||||
screen
|
||||
};
|
||||
painter.image(
|
||||
self.texture.id(),
|
||||
rect,
|
||||
Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
|
||||
Color32::WHITE.gamma_multiply(self.opacity),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(bytes: &[u8], max_side: u32) -> Result<image::RgbaImage, String> {
|
||||
if bytes.len() > 64 * 1024 * 1024 {
|
||||
return Err("Reference image exceeds 64 MiB.".into());
|
||||
}
|
||||
let mut reader = image::ImageReader::new(Cursor::new(bytes))
|
||||
.with_guessed_format()
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !matches!(
|
||||
reader.format(),
|
||||
Some(image::ImageFormat::Png | image::ImageFormat::Jpeg | image::ImageFormat::Bmp)
|
||||
) {
|
||||
return Err("Unsupported reference image. Use PNG, JPEG or BMP.".into());
|
||||
}
|
||||
let mut limits = image::Limits::default();
|
||||
limits.max_image_width = Some(max_side.min(8192));
|
||||
limits.max_image_height = Some(max_side.min(8192));
|
||||
limits.max_alloc = Some(256 * 1024 * 1024);
|
||||
reader.limits(limits);
|
||||
reader
|
||||
.decode()
|
||||
.map(|i| i.to_rgba8())
|
||||
.map_err(|e| format!("Cannot decode reference image: {e}"))
|
||||
}
|
||||
|
||||
fn warning([w, h]: [u32; 2], preserve_ratio: bool) -> Option<(bool, String)> {
|
||||
let different_ratio = u64::from(w) * 1080 != u64::from(h) * 1920;
|
||||
if different_ratio && !preserve_ratio {
|
||||
Some((
|
||||
true,
|
||||
format!(
|
||||
"SEVERE WARNING · {w} × {h} to 1920 × 1080: aspect ratio changed; picture distorted. Import remains usable."
|
||||
),
|
||||
))
|
||||
} else if [w, h] != [1920, 1080] {
|
||||
Some((
|
||||
false,
|
||||
format!(
|
||||
"WARNING · {w} × {h}: scaled to fit, proportions preserved{}.",
|
||||
if different_ratio {
|
||||
"; empty margins"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
),
|
||||
))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn scaling_warnings_distinguish_resolution_and_ratio() {
|
||||
assert!(warning([1920, 1080], false).is_none());
|
||||
for size in [[3840, 2160], [1280, 720]] {
|
||||
assert!(!warning(size, false).unwrap().0);
|
||||
}
|
||||
assert!(warning([1920, 1200], false).unwrap().0);
|
||||
assert!(!warning([1920, 1200], true).unwrap().0);
|
||||
}
|
||||
#[test]
|
||||
fn decodes_pixels_and_rejects_corrupt_or_oversized_images() {
|
||||
let image = image::RgbaImage::from_pixel(4, 2, image::Rgba([30, 60, 90, 128]));
|
||||
let mut bytes = Cursor::new(Vec::new());
|
||||
image.write_to(&mut bytes, image::ImageFormat::Png).unwrap();
|
||||
assert_eq!(decode(bytes.get_ref(), 4096).unwrap(), image);
|
||||
assert!(decode(bytes.get_ref(), 2).is_err());
|
||||
assert!(decode(b"bad image", 4096).is_err());
|
||||
}
|
||||
}
|
||||
|
|
@ -437,3 +437,8 @@ WebRCS version.
|
|||
Open compatibility decisions: exact installed firmware and primary operator
|
||||
OS/display scale. Concept A is approved and draw.io is the first clipboard source,
|
||||
with the user-supplied percent-encoded rectangle retained as a regression fixture.
|
||||
|
||||
Reference-picture implementation update: PNG/JPEG/BMP import and file drop are
|
||||
available, with fill-screen or proportion-preserving fit, opacity and visibility.
|
||||
Resolution scaling produces a warning; aspect distortion a severe warning. Both
|
||||
remain non-blocking. Image offsets and persistent sessions remain future work.
|
||||
|
|
|
|||
|
|
@ -121,8 +121,8 @@ identifies its target explicitly and changes only that layer.
|
|||
## Current limits
|
||||
|
||||
The screen is currently fixed to **1920 × 1080**, with two live rectangles.
|
||||
Reference artwork is illustrative, not a loaded image or live video. Source/input
|
||||
formats, custom output resolution, image import, cropping and persistent
|
||||
The initial backdrop is illustrative; importing a reference picture replaces it.
|
||||
Live video, source/input formats, custom output resolution, cropping and persistent
|
||||
session files are future work. Sessions currently live only in memory.
|
||||
|
||||
[Layer management and export scope](midra-layout-plan.md#11-later-stretch-goal-layer-management-and-export-scope)
|
||||
|
|
@ -164,3 +164,21 @@ not unlock it. These are local session controls, separate from geometry undo/red
|
|||
Selected layers use gold outlines, labels, handles and position guides; unselected
|
||||
layers use muted outlines and labels. Gold in the layer list follows the same
|
||||
selection state, including when several layers are selected.
|
||||
|
||||
### Reference pictures
|
||||
|
||||
Drop one PNG, JPEG or BMP into the window, or open **Reference image… → Import
|
||||
picture…**. The image is fixed behind the layers in screen coordinates; zooming
|
||||
and panning keep it aligned. Layer fills become translucent to reveal the image.
|
||||
Use the reference window to hide it, adjust opacity, replace it or remove it.
|
||||
|
||||
**Fill screen** maps the entire image to the current 1920 × 1080 output. An exact
|
||||
pixel match needs no warning. Scaling up or down with matching proportions shows
|
||||
an amber warning; a different aspect ratio (such as 16:10) shows a red **SEVERE
|
||||
WARNING** because the image is distorted. Neither warning blocks importing or
|
||||
moving layers. **Fit inside** preserves proportions and centers the image with
|
||||
empty margins where needed; scaling still produces an amber warning.
|
||||
|
||||
Files are limited to 64 MiB and dimensions to 8192 pixels per side or the GPU's
|
||||
texture limit, whichever is smaller. Invalid imports keep the previous image.
|
||||
Reference settings are session-only and independent of layer undo/redo.
|
||||
|
|
|
|||
Loading…
Reference in New Issue