diff --git a/crates/engine/src/aidefringe.rs b/crates/engine/src/aidefringe.rs index fa1d15d3f0..1eaa3fe8ac 100644 --- a/crates/engine/src/aidefringe.rs +++ b/crates/engine/src/aidefringe.rs @@ -502,10 +502,13 @@ pub struct Publish { at: u64, } -/// The name of photo `p`'s correction by the model with SHA-256 `model`. +/// The name of photo `p`'s correction by the model with SHA-256 `model`, for its content and its lens profile. pub fn key(p: &Photo, model: &str) -> u64 { let mut h = Hasher128::new(); - h.str(VERSION).str(model).str(&crate::media::content_key(p)); + // the lens database's profile pinned to the photo too: the correction is made on the picture that profile + // corrects, and its gains are mapped back through that frame; a refreshed profile changes neither the file nor + // the model (review of #4545) + h.str(VERSION).str(model).str(&crate::media::content_key(p)).u64(crate::media::lens_key(p)); h.finish().0 as u64 | 1 } diff --git a/crates/engine/src/media.rs b/crates/engine/src/media.rs index dcba02494d..b267d0cb18 100644 --- a/crates/engine/src/media.rs +++ b/crates/engine/src/media.rs @@ -37,7 +37,7 @@ mod source; pub use cache::DenoiseIndex; #[cfg(test)] pub(crate) use cache::rendered_budget; -pub use quick::{QuickJob, QuickSource, content_key, source_info}; +pub use quick::{QuickJob, QuickSource, content_key, lens_key, source_info}; pub use render::{RenderJob, RenderResult, ScreenFrame, develop, develop_reporting, fit_cached, fit_for_display}; #[cfg(test)] use settings_hash::SETTINGS_HASH_ENTRIES; diff --git a/crates/engine/src/tests/aidefringe.rs b/crates/engine/src/tests/aidefringe.rs index 9d45f62de0..5b621c642b 100644 --- a/crates/engine/src/tests/aidefringe.rs +++ b/crates/engine/src/tests/aidefringe.rs @@ -141,6 +141,47 @@ fn the_switch_needs_a_model_and_the_correction_is_made_in_the_background() { let _ = std::fs::remove_dir_all(&dir); } +/// Review of #4545: a correction is made for the lens database's profile pinned to the photo. When that profile is +/// refreshed (same file, same model), the correction made before is not used again; switching back to the old +/// profile finds it again. +#[test] +fn a_refreshed_lens_profile_does_not_reuse_the_old_correction() { + use lightcraft_develop::{EmbeddedLens, EmbeddedWarp, LensSource}; + let mut x = setup("adf-lens"); + let id = photo(&x.s); + install(&mut x); + let lens = |k: f64| EmbeddedLens { + warp: Some(EmbeddedWarp { planes: [[1.0, k, 0.0, 0.0, 0.0, 0.0]; 3], radius: 0.6, ..Default::default() }), + source: LensSource::Database, + ..Default::default() + }; + x.s.catalog.apply(lightcraft_catalog::Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.02))) }).unwrap(); + x.s.execute("aiDefringe.toggle", &json!({"enabled": true})).unwrap(); + let ready = |s: &mut Session| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60); + loop { + s.execute("aiDefringe.pump", &json!({})).unwrap(); + match s.aidefringe_state(id) { + PhotoState::Ready { .. } => break, + PhotoState::Failed { error } => panic!("{error}"), + _ => {} + } + assert!(std::time::Instant::now() < deadline, "the correction never came"); + std::thread::sleep(std::time::Duration::from_millis(20)); + } + }; + ready(&mut x.s); + // the database refreshes the profile: same bytes, same model, but the old correction no longer fits + x.s.catalog.apply(lightcraft_catalog::Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(0.03))) }).unwrap(); + assert!(!matches!(x.s.aidefringe_state(id), PhotoState::Ready { .. }), "the correction for the old profile isn't reused"); + ready(&mut x.s); + assert_eq!(std::fs::read_dir(x.dir.join("library").join("ai-defringe")).unwrap().flatten().count(), 2, "one per profile"); + // the old profile back: its correction is found again + x.s.catalog.apply(lightcraft_catalog::Op::SetEmbeddedLens { id, lens: Some(Box::new(lens(-0.02))) }).unwrap(); + assert!(matches!(x.s.aidefringe_state(id), PhotoState::Ready { .. })); + let _ = std::fs::remove_dir_all(&x.dir); +} + #[test] fn switching_off_and_quickly_on_again_does_not_count_as_cancelling() { let mut x = setup("adf-offon"); diff --git a/docs/ai-defringe.md b/docs/ai-defringe.md index 212aa1e2c1..acd5ded1aa 100644 --- a/docs/ai-defringe.md +++ b/docs/ai-defringe.md @@ -30,8 +30,8 @@ was read from (spread over the four source pixels around it). So: model is installed. It never comes out without the correction it was asked for. The correction of the photo being edited is made in the background (Activity shows it and can cancel it) and kept in -`/ai-defringe/.lcadf`, named by a hash of the photo's content, the model file's SHA-256 and the algorithm -version (`crates/engine/src/aidefringe.rs`, `VERSION`). It is cached data: Clear (`aiDefringe.clear`) deletes it and it +`/ai-defringe/.lcadf`, named by a hash of the photo's content, the lens database profile pinned to it, the model file's +SHA-256 and the algorithm version (`crates/engine/src/aidefringe.rs`, `VERSION`). It is cached data: Clear (`aiDefringe.clear`) deletes it and it is made again when wanted. A file is compressed, checksummed and read as hostile input. Up to 24 corrections stay in memory; each holds only the pixels it changes.