Carry the learned denoise's switch and grain as edit settings

Whether to use the learned denoise, and how much grain to keep, are what a
photographer sets, so they travel the one road every setting does: published
as a capability, captured by Preset, stored in the sidecar, replayed by the
undo stack (FR-DEV-3c). Published only on a photograph that can take it, for
the lens switch's reason; the availability is derived from the file and is
not in the state. Off by default, grain 0; a reset returns both.
This commit is contained in:
2026-10-03 11:22:29 -04:00
parent 8ea3c3181a
commit ad6bb892f3
5 changed files with 200 additions and 18 deletions
+127
View File
@@ -170,6 +170,18 @@ pub struct EditGraph {
/// correction the photograph asked for — see
/// [`crate::descriptor::ParamDescriptor::switch_on`].
lens_profile_applied: bool,
/// TRACES: FR-DEV-3g
/// Whether this photograph can take the learned denoise — a Bayer
/// mosaic — set by whoever opened it. Derived from the file like the
/// lens profile, so not in the state; it only decides whether the
/// switch below is offered.
denoise_available: bool,
/// Whether the learned denoise replaces the demosaic. An edit: published
/// as [`crate::learned_denoise`], captured, stored and undone with the
/// rest (FR-DEV-3c).
denoise_applied: bool,
/// How much of the removed noise's brightness to put back, 0–100.
denoise_grain: f32,
}
/// TRACES: FR-DEV-3f
@@ -226,6 +238,9 @@ impl EditGraph {
],
lens_profile: None,
lens_profile_applied: true,
denoise_available: false,
denoise_applied: false,
denoise_grain: 0.0,
}
}
@@ -399,6 +414,26 @@ impl EditGraph {
self.lens_profile.as_ref()
}
/// TRACES: FR-DEV-3g
/// Offer the learned denoise, or not: true for a Bayer mosaic.
pub fn set_denoise_available(&mut self, available: bool) {
self.denoise_available = available;
}
/// TRACES: FR-DEV-3g
/// Whether the learned denoise is asked for. A setting kept on a
/// photograph that cannot take it is harmless and does nothing, as a
/// lens switch with no profile does.
pub fn denoise_applied(&self) -> bool {
self.denoise_applied
}
/// TRACES: FR-DEV-3g
/// The grain to keep, 0–1.
pub fn denoise_grain(&self) -> f32 {
self.denoise_grain / 100.0
}
/// TRACES: FR-DEV-3
/// Whether the matched profile is being applied.
pub fn lens_profile_applied(&self) -> bool {
@@ -581,9 +616,37 @@ impl EditGraph {
}
});
// TRACES: FR-DEV-3g
// Offered only where the photograph can take it, for the lens
// switch's reason: a control that can do nothing must not look as if
// it could.
let denoise = self.denoise_available.then(|| {
let desc = crate::learned_denoise::descriptor();
OpCapability {
id: desc.id,
label: desc.label,
active: self.denoise_applied,
params: desc
.params
.iter()
.map(|p| ParamCapability {
id: p.id,
label: p.label,
kind: p.kind.clone(),
default: p.default,
value: self.param(desc.id, p.id).unwrap_or(p.default),
facet: p.facet,
})
.collect(),
presentation: None,
attributes: desc.attributes.clone(),
}
});
switch
.into_iter()
.chain(warps)
.chain(denoise)
.chain(ops)
.chain(std::iter::once(framing))
.collect()
@@ -675,6 +738,11 @@ impl EditGraph {
// `capabilities`, with the operations and the warps and for the
// same reason (FR-DEV-3c).
lens_profile_applied: _,
// Derived from the file, like the profile above.
denoise_available: _,
// Edits, in the state through `capabilities` like the lens switch.
denoise_applied: _,
denoise_grain: _,
masks,
film,
spots,
@@ -746,6 +814,16 @@ impl EditGraph {
}
pub fn set_param(&mut self, op: OpId, param: ParamId, value: f32) {
if op == crate::learned_denoise::ID {
match param {
p if p == crate::learned_denoise::APPLY => self.denoise_applied = value != 0.0,
p if p == crate::learned_denoise::GRAIN => {
self.denoise_grain = value.clamp(0.0, 100.0)
}
_ => log::warn!("unknown parameter {param} on {op}; ignoring"),
}
return;
}
if op == crate::lens::profile_switch::ID {
if param != crate::lens::profile_switch::APPLY {
log::warn!("unknown parameter {param} on {op}; ignoring");
@@ -803,6 +881,15 @@ impl EditGraph {
/// Read a parameter back.
pub fn param(&self, op: OpId, param: ParamId) -> Option<f32> {
if op == crate::learned_denoise::ID {
return match param {
p if p == crate::learned_denoise::APPLY => {
Some(if self.denoise_applied { 1.0 } else { 0.0 })
}
p if p == crate::learned_denoise::GRAIN => Some(self.denoise_grain),
_ => None,
};
}
if op == crate::lens::profile_switch::ID {
return (param == crate::lens::profile_switch::APPLY)
.then_some(if self.lens_profile_applied { 1.0 } else { 0.0 });
@@ -849,6 +936,10 @@ impl EditGraph {
// a reset does not change which lens took the photograph. What returns
// to default is the answer to whether to use it, which is on.
self.set_lens_profile_applied(true);
// The learned denoise returns to off; whether it is available is the
// file's and stays.
self.denoise_applied = false;
self.denoise_grain = 0.0;
}
/// Set the crop rectangle. Clamped to keep it inside the frame.
@@ -1995,4 +2086,40 @@ mod tests {
let after = cropped.render_scale(source, (1500, 1000));
assert!(after.ratio() > fit.ratio());
}
#[test]
fn the_learned_denoise_is_offered_only_where_it_can_run() {
use crate::learned_denoise;
let mut g = EditGraph::default_chain();
assert!(!g.capabilities().iter().any(|c| c.id == learned_denoise::ID));
g.set_denoise_available(true);
let cap = g
.capabilities()
.into_iter()
.find(|c| c.id == learned_denoise::ID)
.expect("offered");
assert!(!cap.active, "off until asked for");
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 1.0);
g.set_param(learned_denoise::ID, learned_denoise::GRAIN, 30.0);
assert!(g.denoise_applied());
assert!((g.denoise_grain() - 0.3).abs() < 1e-6);
g.reset();
assert!(!g.denoise_applied());
assert_eq!(g.denoise_grain(), 0.0);
}
#[test]
fn the_learned_denoise_travels_in_the_state() {
use crate::learned_denoise;
let mut g = EditGraph::default_chain();
g.set_denoise_available(true);
g.set_param(learned_denoise::ID, learned_denoise::APPLY, 1.0);
g.set_param(learned_denoise::ID, learned_denoise::GRAIN, 40.0);
let state = g.state();
let mut h = EditGraph::default_chain();
h.set_denoise_available(true);
let _ = h.set_state(&state);
assert!(h.denoise_applied());
assert!((h.denoise_grain() - 0.4).abs() < 1e-6);
}
}
+51
View File
@@ -0,0 +1,51 @@
//! TRACES: FR-DEV-3g
//! The learned denoise's settings: whether to use it, and how much grain to
//! keep.
//!
//! Not an [`crate::operation::Operation`]: the learned stage replaces the
//! demosaic and runs once per photograph, off the render path
//! (docs/dev/denoise.md §2, §7), and the grain is a blend of its result with
//! the classical one, done where the source is chosen. But what a
//! photographer sets travels the one road every setting travels — the
//! capability list feeds the panel, [`crate::Preset`] captures it, the
//! sidecar stores it, the undo stack replays it (FR-DEV-3c) — so it is
//! published as a capability, like the lens profile switch.
use std::sync::{Arc, LazyLock};
use crate::descriptor::{Attribute, LocalizedKey, OpDescriptor, ParamDescriptor, Scale, Unit};
use crate::{OpId, ParamId};
pub const ID: OpId = OpId("learned_denoise");
pub const APPLY: ParamId = ParamId("apply");
pub const GRAIN: ParamId = ParamId("grain");
/// Off by default: it costs seconds per photograph and replaces the
/// demosaic, which is the photographer's call. Grain 0 is the network's
/// result as it is.
pub(crate) static DESCRIPTOR: LazyLock<Arc<OpDescriptor>> = LazyLock::new(|| {
Arc::new(OpDescriptor {
id: ID,
label: LocalizedKey("op.learned_denoise"),
params: vec![
ParamDescriptor::switch("apply", "param.learned_denoise.apply"),
ParamDescriptor::scalar(
"grain",
"param.learned_denoise.grain",
0.0,
100.0,
0.0,
Unit::Percent,
Scale::Linear,
0,
),
],
// With the classical noise reduction, which is what a photographer
// looks for it beside.
attributes: vec![Attribute::Detail],
})
});
pub fn descriptor() -> Arc<OpDescriptor> {
DESCRIPTOR.clone()
}
+1
View File
@@ -40,6 +40,7 @@ pub mod detail;
pub mod framing;
pub mod graph;
pub mod history;
pub mod learned_denoise;
pub mod lens;
pub mod mask;
pub mod neutral;
File diff suppressed because one or more lines are too long
+3
View File
@@ -203,6 +203,7 @@ fn catalogued(key: &str) -> Option<&'static str> {
// it is a tick box, and a checkbox labelled with a verb reads as a
// button that does something once rather than a state that is on.
"op.lens_profile" => "Lens Profile",
"op.learned_denoise" => "AI Denoise",
// The view transform (FR-DEV-3j). "Tone Mapping" rather than the
// "View Transform" `derive` would give: the id names where it sits in
// the pipeline, and the photographer is choosing how the scene's range
@@ -218,6 +219,8 @@ fn catalogued(key: &str) -> Option<&'static str> {
// parameter is titled by its operation and this one never reaches the
// panel under its own name — see `rows_filtered`.
"param.lens_profile.apply" => "Apply",
"param.learned_denoise.apply" => "Apply",
"param.learned_denoise.grain" => "Keep grain",
"param.camera_profile.apply" => "Use Profile",
// The LookTable's strength, as Lightroom's profile "Amount".
"param.camera_profile.look" => "Look Amount",