Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

#323: Before/After at 1:1 compares like with like

Nathaniel Ledford72bab50c6ec2
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index 072edc334..93d58e994 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -96,8 +96,10 @@ pub fn inspect(app: &LightcraftApp, ctx: &egui::Context) -> Value {
"photo": p.0,
"source": src,
"pending": app.renderer.is_pending(crate::render::Slot::Main),
- // zoomed past the whole-frame render: the window rendered at the zoom scale (pixels of the zoomed frame)
+ // zoomed past the whole-frame render: the window rendered at no more than 100 % (pixels of its frame)
"region": app.region_view.map(|r| json!({"full": [r.full.0, r.full.1], "window": [r.window.x, r.window.y, r.window.w, r.window.h], "pending": app.renderer.is_pending(crate::render::Slot::Region)})),
+ // …and the same for the Before side of a Before/After view
+ "regionBefore": app.region_before_view.map(|r| json!({"full": [r.full.0, r.full.1], "window": [r.window.x, r.window.y, r.window.w, r.window.h], "pending": app.renderer.is_pending(crate::render::Slot::RegionBefore)})),
})),
"hoverPreview": app.hover_preview.as_ref().map(|h| h.label.clone()),
"status": app.ui.status,
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 8bc930fb3..51efb214f 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -203,6 +203,8 @@ pub struct LightcraftApp {
/// The window render the loupe asked for last, by job key (see [`region`]); kept for the few
/// windows whose textures can be on screen, and for the inspector.
pub region_view: Option<region::RegionView>,
+ /// The same for the Before side of a Before/After view.
+ pub region_before_view: Option<region::RegionView>,
pub(crate) region_tiles: std::collections::HashMap<u64, region::RegionView>,
/// Photo Merge dialog previews and background merges.
pub merge: merge::MergeState,
@@ -272,6 +274,7 @@ impl LightcraftApp {
gesture: None,
loupe_shown: None,
region_view: None,
+ region_before_view: None,
region_tiles: Default::default(),
merge: merge::MergeState::default(),
import: None,
diff --git a/crates/ui-egui/src/panels/detail.rs b/crates/ui-egui/src/panels/detail.rs
index 9c494e981..f31fc5304 100644
--- a/crates/ui-egui/src/panels/detail.rs
+++ b/crates/ui-egui/src/panels/detail.rs
@@ -214,6 +214,80 @@ fn animated_rect(ctx: &egui::Context, anim: &mut bool, target: Rect) -> Rect {
r
}
+/// What a window render of the loupe needs to know about the view it belongs to.
+struct WindowCtx {
+ id: PhotoId,
+ /// Long edge of the frame the windows are cut from (`None`: no windows).
+ frame_edge: Option<usize>,
+ drawn_long: f32,
+ aspect: f32,
+ ppp: f32,
+ texture_side: usize,
+ crop_tool: bool,
+ interacting: bool,
+}
+
+/// One side of the loupe that gets a window render.
+#[derive(Clone, Copy)]
+struct WindowView {
+ slot: Slot,
+ /// The Before side (the photo without its edits).
+ before: bool,
+ /// The rect the photo is drawn in.
+ img: Rect,
+ /// The rect it is requested for (the drawn rect without a click-zoom animation).
+ target: Rect,
+ /// The area whose pixels are on screen.
+ visible: Rect,
+}
+
+/// Ask for the window of `v` that holds what is on screen; remember it so its texture can be
+/// drawn where it belongs. `None`: no window is wanted (or possible) for this view.
+fn request_window(app: &mut LightcraftApp, c: &WindowCtx, v: &WindowView) -> Option<crate::region::RegionView> {
+ let frame_edge = c.frame_edge?;
+ let (fw, fh) =
+ if c.aspect >= 1.0 { (frame_edge as f32, frame_edge as f32 / c.aspect) } else { (frame_edge as f32 * c.aspect, frame_edge as f32) };
+ let (fw, fh) = (fw.round().max(1.0) as usize, fh.round().max(1.0) as usize);
+ // what is on screen, in pixels of the window's frame (the drawn frame, scaled)
+ let to_px = c.ppp * frame_edge as f32 / c.drawn_long.max(1.0);
+ let visible = (
+ to_px * (v.visible.left() - v.target.left()),
+ to_px * (v.visible.top() - v.target.top()),
+ to_px * (v.visible.right() - v.target.left()),
+ to_px * (v.visible.bottom() - v.target.top()),
+ );
+ let win = crate::region::window_for(fw, fh, visible, crate::region::max_span(c.texture_side))?;
+ let job = if v.before {
+ app.session.region_job_before(c.id, fw, fh, win, !c.crop_tool)?
+ } else {
+ app.session.region_job(c.id, fw, fh, win, !c.crop_tool)?
+ };
+ // a drag renders drafts of the window, as it does of the whole frame
+ let job = if c.interacting { job.draft() } else { job };
+ let look = job.settings.hash64();
+ let view = crate::region::RegionView { photo: c.id, before: v.before, key: job.key, full: (fw, fh), window: win, settings: look };
+ // windows of other photos, looks and zooms of this side can't be drawn any more
+ app.region_tiles.retain(|_, t| t.before != v.before || t.is_current(c.id, (fw, fh), look, c.interacting));
+ app.region_tiles.insert(job.key, view);
+ app.renderer.request(v.slot, job, 99);
+ Some(view)
+}
+
+/// Draw `v`'s window texture over the whole-frame render, if it is the one asked for (or, in a
+/// drag, an earlier draft of it).
+fn draw_window(p: &egui::Painter, app: &LightcraftApp, c: &WindowCtx, v: &WindowView, wanted: &crate::region::RegionView) {
+ let Some(tex) = app.renderer.textures.get(&v.slot).filter(|t| t.photo == c.id) else { return };
+ let Some(tile) =
+ app.region_tiles.get(&tex.key).filter(|t| t.before == v.before && t.is_current(c.id, wanted.full, wanted.settings, c.interacting))
+ else {
+ return;
+ };
+ let (fw, fh) = (tile.full.0 as f32, tile.full.1 as f32);
+ let at = v.img.min + vec2(tile.window.x as f32 / fw * v.img.width(), tile.window.y as f32 / fh * v.img.height());
+ let dst = Rect::from_min_size(at, vec2(tile.window.w as f32 / fw * v.img.width(), tile.window.h as f32 / fh * v.img.height()));
+ p.image(tex.tex.id(), dst, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
+}
+
pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let full = ui.max_rect();
@@ -365,46 +439,51 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
app.renderer.request(Slot::Before, job, 90);
}
// zoomed past what the whole-frame render holds: also render the part on screen, at no more
- // than 100 % (the GPU magnifies beyond that). Not while comparing, hovering a look or showing
- // an overlay (those draw one image), nor where the shown picture isn't the frame's own (an
- // unsupported raw's embedded JPEG with another crop: window coordinates are the frame's).
- let plain_view = !split
- && !show_before
- && !split_view
- && hover_key.is_none()
+ // than 100 % (the GPU magnifies beyond that), for each side that is shown. Not while hovering a
+ // look or showing an overlay (those draw another image), nor where the shown picture isn't the
+ // frame's own (an unsupported raw's embedded JPEG with another crop: window coordinates are
+ // the frame's).
+ let plain_view = hover_key.is_none()
&& !app.ui.soft_proof
&& view_overlay(app, &d) == lightcraft_pipeline::Overlay::None
&& crate::region::same_aspect(display_aspect, aspect);
- let look = d.hash64();
- let mut region_full = None;
- if let Some(frame_edge) = plan.window_edge.filter(|_| plain_view) {
- let (fw, fh) = if aspect >= 1.0 { (frame_edge as f32, frame_edge as f32 / aspect) } else { (frame_edge as f32 * aspect, frame_edge as f32) };
- let (fw, fh) = (fw.round().max(1.0) as usize, fh.round().max(1.0) as usize);
- region_full = Some((fw, fh));
- // what is on screen, in pixels of the window's frame (the drawn frame, scaled)
- let to_px = ppp * frame_edge as f32 / drawn_long.max(1.0);
- let visible = (
- to_px * (canvas.left() - target_rect.left()),
- to_px * (canvas.top() - target_rect.top()),
- to_px * (canvas.right() - target_rect.left()),
- to_px * (canvas.bottom() - target_rect.top()),
- );
- if let Some(win) = crate::region::window_for(fw, fh, visible, crate::region::max_span(texture_side))
- && let Some(job) = app.session.region_job(id, fw, fh, win, !crop_tool)
- {
- // a drag renders drafts of the window, as it does of the whole frame
- let job = if interacting { job.draft() } else { job };
- let view = crate::region::RegionView { photo: id, key: job.key, full: (fw, fh), window: win, settings: look };
- // windows of other photos, looks and zooms can't be drawn any more
- app.region_tiles.retain(|_, v| v.is_current(id, (fw, fh), look, interacting));
- app.region_tiles.insert(job.key, view);
- app.region_view = Some(view);
- app.renderer.request(Slot::Region, job, 99);
+ let window_ctx =
+ WindowCtx { id, frame_edge: plan.window_edge.filter(|_| plain_view), drawn_long, aspect, ppp, texture_side, crop_tool, interacting };
+ // (slot, before side?, rect to draw in, rect it is requested for, area whose pixels show)
+ let mut window_views: Vec<WindowView> = Vec::new();
+ if window_ctx.frame_edge.is_some() {
+ let before_rect = |r: Rect, area: Rect| WindowView { slot: Slot::RegionBefore, before: true, img: r, target: r, visible: area };
+ let after = WindowView { slot: Slot::Region, before: false, img: img_rect, target: target_rect, visible: main_area };
+ if split {
+ let br = fit_rect(areas[0], aspect, app.ui.zoom, native, ppp, app.ui.pan);
+ window_views.push(before_rect(br, areas[0]));
+ window_views.push(after);
+ } else if show_before {
+ window_views.push(WindowView { slot: Slot::RegionBefore, before: true, img: img_rect, target: target_rect, visible: canvas });
+ } else if split_view {
+ window_views.push(WindowView { slot: Slot::RegionBefore, before: true, img: img_rect, target: target_rect, visible: canvas });
+ window_views.push(WindowView { visible: canvas, ..after });
+ } else {
+ window_views.push(WindowView { visible: canvas, ..after });
+ }
+ }
+ let mut windows: Vec<(WindowView, crate::region::RegionView)> = Vec::new();
+ for v in window_views {
+ if let Some(view) = request_window(app, &window_ctx, &v) {
+ windows.push((v, view));
+ }
+ }
+ for (slot, before) in [(Slot::Region, false), (Slot::RegionBefore, true)] {
+ let shown = windows.iter().find(|(v, _)| v.before == before).map(|(_, view)| *view);
+ if before {
+ app.region_before_view = shown;
+ } else {
+ app.region_view = shown;
+ }
+ if shown.is_none() {
+ app.region_tiles.retain(|_, v| v.before != before);
+ app.renderer.release(slot);
}
- } else {
- app.region_view = None;
- app.region_tiles.clear();
- app.renderer.release(Slot::Region);
}
let p = ui.painter_at(canvas);
// what a view slot shows: its own render of this photo, else the stand-ins (no blank frame
@@ -470,15 +549,17 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
}
}
}
- // the window render, drawn over the whole-frame one where it belongs in the frame
- if let Some(full) = region_full
- && let Some(tex) = app.renderer.textures.get(&Slot::Region).filter(|t| t.photo == id)
- && let Some(v) = app.region_tiles.get(&tex.key).filter(|v| v.is_current(id, full, look, interacting))
- {
- let (fw, fh) = (v.full.0 as f32, v.full.1 as f32);
- let at = img_rect.min + vec2(v.window.x as f32 / fw * img_rect.width(), v.window.y as f32 / fh * img_rect.height());
- let dst = Rect::from_min_size(at, vec2(v.window.w as f32 / fw * img_rect.width(), v.window.h as f32 / fh * img_rect.height()));
- p.image(tex.tex.id(), dst, Rect::from_min_max(pos2(0.0, 0.0), pos2(1.0, 1.0)), Color32::WHITE);
+ // the window renders, drawn over the whole-frame ones where they belong in the frame (the
+ // Before side of a wipe only on its side of the line)
+ for (v, view) in &windows {
+ let clip = match (app.ui.before_after, v.before) {
+ (BeforeAfter::Split, true) => Rect::from_min_max(canvas.min, pos2(img_rect.center().x, canvas.bottom())),
+ (BeforeAfter::SplitTopBottom, true) => Rect::from_min_max(canvas.min, pos2(canvas.right(), img_rect.center().y)),
+ (BeforeAfter::Split, false) => Rect::from_min_max(pos2(img_rect.center().x, canvas.top()), canvas.max),
+ (BeforeAfter::SplitTopBottom, false) => Rect::from_min_max(pos2(canvas.left(), img_rect.center().y), canvas.max),
+ _ => canvas,
+ };
+ draw_window(&p.with_clip_rect(clip.intersect(canvas)), app, &window_ctx, v, view);
}
app.loupe_shown = Some((id, shown));
if app.ui.before_after == BeforeAfter::Split {
diff --git a/crates/ui-egui/src/region.rs b/crates/ui-egui/src/region.rs
index 96c882e7c..ac0c357ef 100644
--- a/crates/ui-egui/src/region.rs
+++ b/crates/ui-egui/src/region.rs
@@ -20,6 +20,8 @@ pub const MAX_SPAN: usize = if cfg!(target_arch = "wasm32") { 3072 } else { 6144
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RegionView {
pub photo: lightcraft_catalog::PhotoId,
+ /// The Before side of a Before/After view (the photo without its edits).
+ pub before: bool,
pub key: u64,
pub full: (usize, usize),
pub window: PixelWindow,
@@ -308,7 +310,14 @@ mod tests {
#[test]
fn a_tile_is_drawn_only_while_it_shows_what_the_loupe_shows() {
use lightcraft_catalog::PhotoId;
- let v = RegionView { photo: PhotoId(1), key: 7, full: (6000, 4000), window: PixelWindow { x: 0, y: 0, w: 256, h: 256 }, settings: 11 };
+ let v = RegionView {
+ photo: PhotoId(1),
+ before: false,
+ key: 7,
+ full: (6000, 4000),
+ window: PixelWindow { x: 0, y: 0, w: 256, h: 256 },
+ settings: 11,
+ };
assert!(v.is_current(PhotoId(1), (6000, 4000), 11, false));
assert!(!v.is_current(PhotoId(2), (6000, 4000), 11, false), "another photo");
assert!(!v.is_current(PhotoId(1), (6000, 4001), 11, false), "another zoom");
diff --git a/crates/ui-egui/src/render.rs b/crates/ui-egui/src/render.rs
index 4231e61b0..31a575303 100644
--- a/crates/ui-egui/src/render.rs
+++ b/crates/ui-egui/src/render.rs
@@ -33,6 +33,8 @@ pub enum Slot {
/// The loupe zoomed past what `Main` can hold: the window of the frame that is on screen,
/// rendered at the zoom scale and drawn over `Main` (see [`crate::region`]).
Region,
+ /// The Before side of a Before/After view zoomed the same way (see [`Slot::Region`]).
+ RegionBefore,
/// The loupe's stand-in until `Main` has the photo.
Preview,
Before,
@@ -88,7 +90,7 @@ struct Queued {
pub(crate) fn stage_trim_order(sizes: &[(Slot, usize)], budget: usize) -> Vec<Slot> {
let rank = |s: Slot| match s {
Slot::Hover => 0,
- Slot::Before => 1,
+ Slot::Before | Slot::RegionBefore => 1,
Slot::Compare(_) => 2,
Slot::Second => 3,
Slot::Region => 4,
@@ -181,7 +183,7 @@ impl Slot {
/// An interactive view of the open photo: slider drags redo only the stages they feed, and a
/// draft that finishes late still replaces older pixels.
pub fn is_view(self) -> bool {
- matches!(self, Slot::Main | Slot::Region | Slot::Before | Slot::Hover)
+ matches!(self, Slot::Main | Slot::Region | Slot::RegionBefore | Slot::Before | Slot::Hover)
}
}
diff --git a/crates/ui-egui/src/tests_preview_limit.rs b/crates/ui-egui/src/tests_preview_limit.rs
index c02454946..af1c36e91 100644
--- a/crates/ui-egui/src/tests_preview_limit.rs
+++ b/crates/ui-egui/src/tests_preview_limit.rs
@@ -365,4 +365,64 @@ mod in_the_loupe {
let (w, hh) = region_tile(&h).expect("a window draft");
assert!(w * hh <= 3_000_000, "{w}×{hh}: the window holds what is on screen, not the frame");
}
+
+ fn tile(h: &Headless, slot: Slot) -> Option<(usize, usize)> {
+ h.app.renderer.textures.get(&slot).map(|t| (t.size[0], t.size[1]))
+ }
+
+ // Given Before/After side by side at 1:1, both sides are sharp windows of the same frame: the
+ // Before is not a 2560 px stand-in beside a full resolution After
+ #[test]
+ fn side_by_side_at_one_to_one_compares_like_with_like() {
+ let (mut h, native) = detail();
+ h.app.ui.before_after = crate::state::BeforeAfter::SideBySide;
+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);
+ h.settle(SETTLE);
+ let (after, before) = (h.app.region_view.expect("After window"), h.app.region_before_view.expect("Before window"));
+ assert_eq!(after.full, before.full, "the same frame");
+ assert_eq!(after.full.0.max(after.full.1), native);
+ let (a, b) = (tile(&h, Slot::Region).expect("After tile"), tile(&h, Slot::RegionBefore).expect("Before tile"));
+ assert_eq!(a, b, "the same window of each");
+ let main = h.app.renderer.textures.get(&Slot::Main).unwrap();
+ let before_main = h.app.renderer.textures.get(&Slot::Before).unwrap();
+ assert_eq!(main.size, before_main.size, "and the same whole-frame size under them");
+ }
+
+ // Given the Before view alone (\), only the Before gets a window
+ #[test]
+ fn the_original_view_windows_only_the_before() {
+ let (mut h, _) = detail();
+ h.app.ui.before_after = crate::state::BeforeAfter::Original;
+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);
+ h.settle(SETTLE);
+ assert!(h.app.region_before_view.is_some());
+ assert_eq!(h.app.region_view, None);
+ assert!(tile(&h, Slot::RegionBefore).is_some() && tile(&h, Slot::Region).is_none());
+ }
+
+ // Given a wipe (Split), both sides are windows, drawn over the same rect
+ #[test]
+ fn a_wipe_windows_both_sides() {
+ let (mut h, _) = detail();
+ h.app.ui.before_after = crate::state::BeforeAfter::Split;
+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);
+ h.settle(SETTLE);
+ assert_eq!(h.app.region_view.map(|v| v.window), h.app.region_before_view.map(|v| v.window));
+ assert!(tile(&h, Slot::Region).is_some() && tile(&h, Slot::RegionBefore).is_some());
+ }
+
+ // Given the Before view turned off, its window is freed
+ #[test]
+ fn leaving_before_after_frees_the_before_window() {
+ let (mut h, _) = detail();
+ h.app.ui.before_after = crate::state::BeforeAfter::SideBySide;
+ h.request("engine.execute", json!({"command": "view.zoom100"}), T);
+ h.settle(SETTLE);
+ assert!(tile(&h, Slot::RegionBefore).is_some());
+ h.app.ui.before_after = crate::state::BeforeAfter::Off;
+ h.settle(SETTLE);
+ assert_eq!(tile(&h, Slot::RegionBefore), None);
+ assert_eq!(h.app.region_before_view, None);
+ assert!(tile(&h, Slot::Region).is_some());
+ }
}