Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions crates/develop/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,22 @@ mod tests {
assert!(!s.masks.iter().any(|m| m.id == next), "{next}");
assert_eq!(next, 2);
}

/// Issue #3056 (review): a refitted crop keeps an explicit aspect lock, also on the whole photo,
/// and an "Original" lock follows the new photo's shape; the lock stays true to the crop.
#[test]
fn crop_refit_honours_the_aspect_lock() {
let (land, tall) = ((6000.0, 4000.0), (4000.0, 6000.0));
let three_two = Crop { aspect: Some((300, 200)), ..Crop::default() };
let r = three_two.refit(land, tall);
assert_eq!(r.aspect, Some((300, 200)));
assert!((r.geometry.rect_px(tall.0, tall.1).aspect() - 1.5).abs() < 1e-9, "{:?}", r.geometry);
let original = Crop { aspect: Some(Crop::original_aspect(land.0, land.1)), flip_h: true, ..Crop::default() };
let r = original.refit(land, tall);
assert_eq!(r.aspect, Some(Crop::original_aspect(tall.0, tall.1)));
assert_eq!(r.geometry, lightcraft_geom::CropGeometry::default());
assert!(r.flip_h);
// unlocked, the whole photo stays the whole photo
assert_eq!(Crop::default().refit(land, tall), Crop::default());
}
}
28 changes: 26 additions & 2 deletions crates/develop/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! exception is `process`: missing (or unreadable), it is V1, the rendering those older files were
//! made with.

use lightcraft_geom::{CropGeometry, Homography, Orientation, Point};
use lightcraft_geom::{CropGeometry, Homography, Orientation, Point, RefitShape};
use serde::{Deserialize, Serialize};

pub const SCHEMA_VERSION: u32 = 1;
Expand Down Expand Up @@ -868,12 +868,36 @@ impl Geometry {
#[serde(default)]
pub struct Crop {
pub geometry: CropGeometry,
/// Locked aspect (w, h); None = free / original.
/// Locked aspect (w × 100, h × 100); None = free. "Original" is the photo's own size as
/// oriented ([`Crop::original_aspect`]).
pub aspect: Option<(u32, u32)>,
pub flip_h: bool,
pub flip_v: bool,
}

impl Crop {
/// The "Original" aspect lock of a `w × h` photo (as oriented): its own size, in hundredths.
pub fn original_aspect(w: f64, h: f64) -> (u32, u32) {
((w * 100.0) as u32, (h * 100.0) as u32)
}

/// This crop, made on a photo `from` (w, h, as oriented) in size, for a photo of size `to`
/// (pasting or syncing it, [`CropGeometry::refit`]): a locked aspect ratio is kept, also by a
/// crop of the whole photo, and an "Original" lock takes the new photo's shape (and becomes its
/// Original lock); unlocked, the whole photo stays the whole photo and any other crop keeps its
/// aspect ratio.
pub fn refit(&self, from: (f64, f64), to: (f64, f64)) -> Crop {
let valid = |(w, h): (f64, f64)| w.is_finite() && h.is_finite() && w > 0.0 && h > 0.0;
let shape = match self.aspect {
None => RefitShape::Free,
Some(a) if valid(from) && a == Self::original_aspect(from.0, from.1) => RefitShape::Photo,
Some(_) => RefitShape::Locked,
};
let aspect = if shape == RefitShape::Photo && valid(to) { Some(Self::original_aspect(to.0, to.1)) } else { self.aspect };
Crop { geometry: self.geometry.refit(from, to, shape), aspect, ..*self }
}
}

// ---------------------------------------------------------------------------------------------
// Masks and local adjustments

Expand Down
49 changes: 33 additions & 16 deletions crates/engine/src/cmd/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lightcraft_geom::{CropGeometry, CropHandle, MIN_CROP_FRACTION, Point, Rect,
use serde_json::{Value, json};

use super::{CommandSpec, always, bad, bool_or, cmd, f64_or, f64_req, has_active, has_clipboard, has_selection, index_req, ok, str_param};
use crate::{Result, Session, media::SourceLevel};
use crate::{Result, Session, SettingsClipboard, media::SourceLevel};

/// Strokes one Lens Blur refinement brush keeps (a document can't grow without bound).
const MAX_LENS_BLUR_STROKES: usize = 2000;
Expand Down Expand Up @@ -36,6 +36,20 @@ fn groups_param(p: &Value, c: &str) -> Result<Option<Vec<SettingsGroup>>> {
}
}

/// The settings `partial` (groups taken from a photo `from` in size, as oriented) pasted onto photo
/// `id`'s settings `to`. A pasted crop keeps its shape on a photo of another shape, as in Lightroom
/// ([`lightcraft_develop::Crop::refit`]); without both sizes it is pasted as it is. The size of `id`
/// is taken with the pasted orientation (the Crop group carries it).
fn paste_onto(s: &Session, partial: &Value, from: Option<(f64, f64)>, id: PhotoId, to: &DevelopSettings) -> DevelopSettings {
let mut d = lightcraft_develop::apply_partial(to, partial, 1.0);
if partial.get("crop").is_some()
&& let (Some(from), Some(dims)) = (from, s.oriented_dims(id, d.orientation))
{
d.crop = d.crop.refit(from, dims);
}
d
}

/// The leaves of `a` that differ from `b` (same shape), as a partial JSON object: `None` when they
/// are equal. Used to store only what a batch-edit recipe names, so applying the saved preset
/// later never overrides the settings the recipe doesn't name.
Expand Down Expand Up @@ -993,7 +1007,7 @@ pub fn specs() -> Vec<CommandSpec> {
let aspect = match p.get("aspect") {
Some(Value::String(a)) if a == "toggle" => None,
Some(Value::String(a)) if a == "free" => None,
Some(Value::String(a)) if a == "original" => Some(((w * 100.0) as u32, (h * 100.0) as u32)),
Some(Value::String(a)) if a == "original" => Some(lightcraft_develop::Crop::original_aspect(w, h)),
Some(Value::String(a)) => {
let (x, y) = a.split_once('x').ok_or_else(|| bad("crop.aspect", "use WxH"))?;
let (x, y): (f64, f64) =
Expand Down Expand Up @@ -1136,8 +1150,10 @@ pub fn specs() -> Vec<CommandSpec> {
if let Some(g) = groups_param(p, c)? {
s.copy_groups = g;
}
let d = s.develop_of(id).unwrap_or_default();
s.clipboard = Some(lightcraft_develop::extract_groups(&d, &s.copy_groups));
// the whole settings: Paste Selected can paste a group the copy left out (#3056)
let settings = s.develop_of(id).unwrap_or_default();
let dims = s.oriented_dims(id, settings.orientation);
s.clipboard = Some(SettingsClipboard { settings, dims });
Ok(json!({"groups": s.copy_groups}))
}
),
Expand All @@ -1153,12 +1169,14 @@ pub fn specs() -> Vec<CommandSpec> {
let prev =
s.previous_active.filter(|id| s.catalog.photo(*id).is_some_and(|ph| !ph.deleted)).ok_or_else(|| bad(c, "no previous photo"))?;
let groups = groups_param(p, c)?.unwrap_or_else(|| s.copy_groups.clone());
let partial = lightcraft_develop::extract_groups(&s.develop_of(prev).unwrap_or_default(), &groups);
let from = s.develop_of(prev).unwrap_or_default();
let partial = lightcraft_develop::extract_groups(&from, &groups);
let dims = s.oriented_dims(prev, from.orientation);
let ids: Vec<PhotoId> = s.targets(p)?.into_iter().filter(|id| *id != prev).collect();
let ops = ids
.iter()
.filter_map(|id| s.develop_of(*id).map(|d| (*id, d)))
.filter_map(|(id, d)| s.develop_op(id, lightcraft_develop::apply_partial(&d, &partial, 1.0), "Paste from Previous"))
.filter_map(|(id, d)| s.develop_op(id, paste_onto(s, &partial, dims, id, &d), "Paste from Previous"))
.collect::<Vec<_>>();
let n = ops.len();
s.commit("Paste from Previous", Op::Batch { ops })?;
Expand All @@ -1170,22 +1188,18 @@ pub fn specs() -> Vec<CommandSpec> {
"Paste Edit Settings",
["Edit"],
Some("Cmd+V"),
"{ids?, groups?: [settingsGroup] (paste only these of the copied groups)}",
"{ids?, groups?: [settingsGroup] (default: the groups chosen when copying; any group of the copied photo, Paste Selected Settings)}",
has_clipboard,
|s, p| {
let c = "develop.paste";
let mut clip = s.clipboard.clone().unwrap_or_default();
if let Some(only) = groups_param(p, c)? {
// the clipboard holds whole groups: rebuild it with just the chosen (copied) ones
let only: Vec<_> = only.into_iter().filter(|g| s.copy_groups.contains(g)).collect();
let full = lightcraft_develop::apply_partial(&DevelopSettings::default(), &clip, 1.0);
clip = lightcraft_develop::extract_groups(&full, &only);
}
let clip = s.clipboard.clone().ok_or_else(|| bad(c, "no settings copied"))?;
let groups = groups_param(p, c)?.unwrap_or_else(|| s.copy_groups.clone());
let partial = lightcraft_develop::extract_groups(&clip.settings, &groups);
let ids = s.targets(p)?;
let ops = ids
.iter()
.filter_map(|id| s.develop_of(*id).map(|d| (*id, d)))
.filter_map(|(id, d)| s.develop_op(id, lightcraft_develop::apply_partial(&d, &clip, 1.0), "Paste Settings"))
.filter_map(|(id, d)| s.develop_op(id, paste_onto(s, &partial, clip.dims, id, &d), "Paste Settings"))
.collect::<Vec<_>>();
let n = ops.len();
s.commit("Paste Settings", Op::Batch { ops })?;
Expand All @@ -1205,14 +1219,15 @@ pub fn specs() -> Vec<CommandSpec> {
let groups = groups_param(p, c)?.unwrap_or_else(SettingsGroup::default_copy);
let d = s.develop_of(id).unwrap_or_default();
let partial = lightcraft_develop::extract_groups(&d, &groups);
let dims = s.oriented_dims(id, d.orientation);
let ops = s
.selection
.ids
.clone()
.into_iter()
.filter(|x| *x != id)
.filter_map(|x| s.develop_of(x).map(|d| (x, d)))
.filter_map(|(x, dd)| s.develop_op(x, lightcraft_develop::apply_partial(&dd, &partial, 1.0), "Sync Settings"))
.filter_map(|(x, dd)| s.develop_op(x, paste_onto(s, &partial, dims, x, &dd), "Sync Settings"))
.collect::<Vec<_>>();
let n = ops.len();
s.commit("Sync Settings", Op::Batch { ops })?;
Expand Down Expand Up @@ -1323,6 +1338,8 @@ pub fn specs() -> Vec<CommandSpec> {
let amount = f64_or(p, "amount", 100.0).clamp(0.0, 200.0) / 100.0;
// `id` names the preset here: the photos are `ids`, else the selection
let ids = s.targets(&json!({"ids": p.get("ids")}))?;
// a preset's crop is applied as stored: it has no source photo whose shape a refit
// could start from (unlike Paste and Sync, issue #3056)
let ops = ids
.iter()
.filter_map(|id| s.develop_of(*id).map(|d| (*id, d)))
Expand Down
57 changes: 52 additions & 5 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ fn filter_depends_on_clock(filter: &Filter, catalog: &Catalog) -> bool {
static VISIBLE_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
static LIBRARY_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

/// Copied develop settings (Copy Edit Settings). The copied photo's settings are kept whole, so
/// Paste Selected Settings can paste any group, also one the copy left out (crop, Remove, …; issue
/// #3056); a plain Paste applies [`Session::copy_groups`].
#[derive(Clone, Debug)]
pub struct SettingsClipboard {
/// The copied photo's settings, every group.
pub settings: Arc<DevelopSettings>,
/// The copied photo's size as oriented, to refit a pasted crop to a photo of another shape;
/// `None` when unknown (the crop is then pasted as it is).
pub dims: Option<(f64, f64)>,
}

pub struct Session {
library_generation: u64,
/// Auto Sync: edits to the active photo also change the other selected photos (the settings
Expand Down Expand Up @@ -306,8 +318,8 @@ pub struct Session {
/// Reading focus points for photos imported before they were read (`photo.readFocusPoints`, `focus.pump`).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) focus_scan: focus_scan::FocusScan,
/// Copied develop settings (partial JSON) for Paste.
pub clipboard: Option<Value>,
/// Copied develop settings for Paste (Copy Edit Settings).
pub clipboard: Option<SettingsClipboard>,
/// The folder on disk the [`LibrarySource::Folder`] view browses.
pub browse: Option<Browse>,
/// The folder the [`LibrarySource::LibraryFolder`] view shows: the library's photos imported
Expand All @@ -317,7 +329,7 @@ pub struct Session {
pub meta_clipboard: Option<Value>,
/// The photo that was active before the current one (Paste Settings from Previous).
pub previous_active: Option<PhotoId>,
/// Groups last used for Copy (Lightroom remembers them).
/// Groups last used for Copy (Lightroom remembers them): what a plain Paste applies.
pub copy_groups: Vec<lightcraft_develop::SettingsGroup>,
/// Built-in then user presets; its generation tells savers and caches when it changed.
pub presets: presets::PresetList,
Expand Down Expand Up @@ -1008,8 +1020,11 @@ impl Session {
/// With Auto Sync on, the ops that carry an edit of the active photo `id` (to `new`, as asked,
/// before its crop is fitted) over to the other selected photos: only the settings that changed;
/// never spot removal or red eye (they belong to one photo's pixels), nor the rendering process
/// (Sync doesn't carry it either), nor history / snapshot restores. Constrain Crop then fits each
/// photo's own crop: the active photo's fitted crop is never copied over another's.
/// (Sync doesn't carry it either), nor history / snapshot restores. A changed crop rect or aspect
/// lock is refitted to each photo's shape as Sync does ([`lightcraft_develop::Crop::refit`]),
/// leaving the crop fields the edit didn't change (a rect edit keeps each photo's angle).
/// Constrain Crop then fits each photo's own crop: the active photo's fitted crop is never
/// copied over another's.
///
/// White balance goes on each photo as the active photo's mode or value on the photo's own
/// scale ([`sync_same`]): As Shot its own camera white, Auto its own (`own_auto`, worked out on
Expand Down Expand Up @@ -1050,12 +1065,44 @@ impl Session {
if wb_changed {
sync_same::synced_wb(&ainfo, &old, new, &self.source_info(x), &d, own_auto.get(&x).copied(), &mut synced);
}
// a changed crop takes this photo's shape, as Sync and Paste give it (issue #3056).
// Only what changed: the refit starts from this photo's crop with the changed leaves
// applied (a new rect keeps this photo's angle) under the lock the edit was made with,
// and sets only the rect and lock the edit changed (the refit keeps angle and flips).
let crop = delta.get("crop");
let rect_changed = crop.and_then(|c| c.get("geometry")).is_some_and(|g| g.get("rect").is_some());
let aspect_changed = crop.is_some_and(|c| c.get("aspect").is_some());
if (rect_changed || aspect_changed)
&& let (Some(from), Some(to)) = (self.oriented_dims(id, new.orientation), self.oriented_dims(x, synced.orientation))
{
let mut start = lightcraft_develop::Crop { aspect: new.crop.aspect, ..synced.crop };
// a dragged edge or two mean nothing in a photo of another shape: it refits the
// active photo's whole rect (at its own angle); one of the same shape takes the edges
let same_shape = (from.0 * to.1 - to.0 * from.1).abs() <= 1e-6 * (to.0 * from.1);
if rect_changed && !same_shape {
start.geometry.rect = new.crop.geometry.rect;
}
let fit = start.refit(from, to);
if rect_changed {
synced.crop.geometry.rect = fit.geometry.rect;
}
if aspect_changed {
synced.crop.aspect = fit.aspect;
}
}
let synced = self.fit_constrained_crop(x, synced);
(synced != *d).then(|| self.develop_op(x, synced, label)).flatten()
})
.collect()
}

/// Photo `id`'s size as oriented with `orientation`; `None` when the catalog doesn't know it.
pub(crate) fn oriented_dims(&self, id: PhotoId, orientation: lightcraft_geom::Orientation) -> Option<(f64, f64)> {
let p = self.catalog.photo(id)?;
let (w, h) = (p.width as f64, p.height as f64);
(w > 0.0 && h > 0.0).then_some(if orientation.swaps_axes() { (h, w) } else { (w, h) })
}

/// Auto Sync carries an edit of photo `id` labelled `label` to the other selected photos: it is
/// on, `id` is the active photo of a selection of two or more, and the edit is not a History or
/// snapshot restore.
Expand Down
Loading
Loading