Files
lightcraft/crates/engine/src/sync_same.rs
T
67782d8f65 Auto Sync: As Shot, Auto and Temp apply to each selected photo's own white balance (#3571)
* LRC-DEV-AUTOSYNC: Auto Sync carries white balance as a mode or on each photo's own scale: As Shot and Auto per photo, a Temp move keeps each photo's tint (N-A3, note 18)

Auto Sync already sets every other setting to the active photo's number.
White balance is the exception: main stores the resolved Kelvin and tint
with the mode, and Auto Sync copied those numbers. As Shot then gave every
selected photo the active photo's camera white, Auto gave them the active
photo's Auto white, a Temp move from As Shot gave them the active photo's
tint too, and a JPEG beside a raw got the raw's Kelvin read on its own scale.

Now (sync_same.rs) the mode goes on each photo and each applies it to
itself: As Shot its own camera white, Auto its own white worked out on its
own pixels, a light preset its numbers. A typed or dragged Temp or Tint
sets the same Kelvin on every raw (an old relative-scale value converted
first) and only the slider that moved, and a photo on another scale moves
by as much as the active one did. One undo step, as before.

Tests (tests/autosync_wb.rs): As Shot / Tungsten / Auto per photo with
three different as-shot whites and undo; a typed and a dragged Temp keep
each photo's tint; the old relative scale; a JPEG beside a raw. Three of
the four fail on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* LRC-DEV-AUTOSYNC: Auto's per-photo pixels are read in parallel; the row says which sync does what

Review of #3571:
- When Auto is synced, the other selected photos' thumbnails that aren't
  in memory are now decoded in parallel (one per core), not one after
  another on the UI thread.
- The checklist row says the per-photo white balance belongs to Auto
  Sync, while Sync Settings copies the active photo's stored one.
- The row and the test notes now say a Temp is the same number only on a
  photo of the same scale, and that other photos move by as much as the
  active photo did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* LRC-DEV-AUTOSYNC: Auto's per-photo pixels are read a core's worth at a time, each with its decoder's facts

Review of #3571:
- A large selection no longer holds every decoded thumbnail at once. They
  are decoded in batches of one per core (at most 16), and each batch's
  white balance is worked out before the next is decoded.
- Each photo's Auto uses the facts its own decoder gave, read right after
  its source is cached, so a photo pushed out of the cache by later ones
  never falls back to catalog metadata.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* LRC-DEV-AUTOSYNC: a light preset with a tint of its own on a JPEG carries that tint; Auto's facts are read before the cache can evict them (review of #3585)

- On the relative scale the stored numbers are what renders, so a preset
  the active photo overrides (`develop.wb {mode, tint}`) travels as its
  numbers, like Custom, not as the preset's tint.
- `sources_now` reads each photo's decoder facts before storing the
  decoded source: the cache's budget may evict that entry at once, and
  the header facts alone lack the camera colour Auto uses.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com>
2026-11-05 06:09:04 +01:00

133 lines
5.9 KiB
Rust

//! Auto Sync carries the active photo's VALUE to every selected photo, as Lightroom Classic does
//! (test notes 2026-10-24 note 18, the user's rule "same value for all synced values"). Where white
//! balance is a mode rather than a number, the mode travels and each photo applies it to itself:
//! As Shot gives each photo its own camera white, Auto each photo's own white from its pixels, a
//! light preset its numbers.
//!
//! A typed or dragged Temp or Tint is set on each photo's own scale: a raw edited in Kelvin gets
//! the same Kelvin (an old relative-scale value converted first, never read as Kelvin, which turned
//! photos blue: test notes 2026-10-15), and only the slider that moved changes (a Temp move keeps
//! each photo's own tint). A photo on another scale than the active one (a JPEG next to a raw) has
//! no "same" number and moves by as much as the active photo instead.
use std::collections::HashMap;
use lightcraft_catalog::PhotoId;
use lightcraft_develop::{DevelopSettings, WbMode};
use lightcraft_pipeline::SourceInfo;
use crate::Session;
/// White balance on this source is edited in Kelvin (a raw with a measured camera scale), not on
/// the relative scale (6500 K / 0 = as shot).
fn kelvin(info: &SourceInfo) -> bool {
info.raw && !info.relative_wb
}
/// The temperature and tint `mode` stands for on a source with `info` (As Shot and the light
/// presets; `None` for Auto and Custom, which need the pixels or the numbers).
fn wb_of_mode(info: &SourceInfo, mode: WbMode) -> Option<(f64, f64)> {
match mode {
WbMode::AsShot => Some((info.as_shot_temp, info.as_shot_tint)),
m => m.preset().map(|(t, ti)| {
// presets are Kelvin on raws with that scale, relative to daylight (6500 K = as shot) elsewhere
if kelvin(info) { (t, ti) } else { (6500.0 * t / 5500.0, ti) }
}),
}
}
/// `synced.wb` for a photo (source `oinfo`, settings `own` before the sync) when the active
/// photo's white balance went from `old` to `new` (source `ainfo`). `auto`: the photo's own Auto
/// white balance, when `new` is Auto (`None`: it couldn't be worked out, and the photo keeps its
/// white balance).
pub(crate) fn synced_wb(
ainfo: &SourceInfo,
old: &DevelopSettings,
new: &DevelopSettings,
oinfo: &SourceInfo,
own: &DevelopSettings,
auto: Option<(f64, f64)>,
synced: &mut DevelopSettings,
) {
use lightcraft_pipeline::local::{effective_wb, normalize_wb};
let mut d = own.clone();
normalize_wb(&mut d, oinfo);
// A light preset with numbers of its own (`develop.wb {mode, tint}` on a photo on the
// relative scale, where the stored numbers are what renders): those numbers travel, as a
// Custom white balance does (review of #3585)
let overridden = new.wb.mode.preset().is_some()
&& wb_of_mode(ainfo, new.wb.mode).is_some_and(|(t, ti)| {
let (at, ati) = effective_wb(ainfo, new);
(at - t).abs() > 1e-6 || (ati - ti).abs() > 1e-6
});
let mode = if overridden { WbMode::Custom } else { new.wb.mode };
let resolved = match mode {
WbMode::Auto => auto,
WbMode::Custom => None,
m => wb_of_mode(oinfo, m),
};
match (mode, resolved) {
(WbMode::Custom, _) => {
let (ot, oti) = effective_wb(oinfo, own);
let (at0, ati0) = effective_wb(ainfo, old);
let (at, ati) = effective_wb(ainfo, new);
let same_scale = kelvin(ainfo) == kelvin(oinfo);
// only what moved: a Temp move leaves each photo's own tint
let pick = |own: f64, was: f64, now: f64| {
if (now - was).abs() < 1e-9 {
own
} else if same_scale {
now
} else {
own + (now - was)
}
};
d.wb.temp = pick(ot, at0, at);
d.wb.tint = pick(oti, ati0, ati);
d.wb.mode = WbMode::Custom;
}
(m, Some((t, ti))) => {
d.wb.temp = t;
d.wb.tint = ti;
d.wb.mode = m;
}
// Auto couldn't be worked out on this photo (its pixels didn't load): it keeps its own
(_, None) => {
synced.wb = own.wb;
return;
}
}
let (t, ti) = (d.wb.temp, d.wb.tint);
lightcraft_develop::controls::set(&mut d, "wb.temp", t);
lightcraft_develop::controls::set(&mut d, "wb.tint", ti);
synced.wb = d.wb;
}
impl Session {
/// Each other selected photo's own Auto white balance, worked out on its own pixels, when
/// Auto Sync is about to carry the active photo's (`id`) switch to Auto (`new`) over. Empty
/// otherwise. A photo whose pixels don't load is left out (it keeps its white balance).
pub(crate) fn own_auto_wb(&mut self, id: PhotoId, new: &DevelopSettings, label: &str) -> HashMap<PhotoId, (f64, f64)> {
let mut own = HashMap::new();
if !self.auto_sync_applies(id, label) || new.wb.mode != WbMode::Auto || self.develop_of(id).is_some_and(|old| old.wb == new.wb) {
return own;
}
let others: Vec<PhotoId> = self.selection.ids.iter().copied().filter(|x| *x != id).collect();
// decoded in parallel, a core's worth at a time: a big selection never holds more
// decoded thumbnails at once than that, whatever the cache keeps
let batch = rayon::current_num_threads().clamp(1, 16);
for chunk in others.chunks(batch) {
for (x, src) in self.sources_now(chunk, crate::media::SourceLevel::Thumb) {
match src {
// with the facts its own decoder gave
Ok((src, info)) => {
own.insert(x, lightcraft_pipeline::auto::auto_wb(&src, &info));
}
Err(e) => log::warn!("Auto Sync: Auto white balance on photo {}: {e}", x.0),
}
}
}
own
}
}