Skip to content

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

Tone Curve: restore slider order to Lightroom layout and guard parametric curve against hostile inputs

Paul9c7b4f221927
diff --git a/crates/pipeline/src/curve.rs b/crates/pipeline/src/curve.rs
index d336274e0..b2c2b0b14 100644
--- a/crates/pipeline/src/curve.rs
+++ b/crates/pipeline/src/curve.rs
@@ -30,8 +30,11 @@ pub struct ParametricCurve {
impl ParametricCurve {
pub fn new(c: &ToneCurve) -> ParametricCurve {
- let (s0, s1, s2) = ((c.split_shadows / 100.0) as f32, (c.split_mid / 100.0) as f32, (c.split_highlights / 100.0) as f32);
- let (a0, a1, a2, a3) = (c.shadows, c.darks, c.lights, c.highlights);
+ let s0 = if c.split_shadows.is_nan() { 0.25 } else { (c.split_shadows / 100.0).clamp(0.0, 1.0) as f32 };
+ let s1 = if c.split_mid.is_nan() { 0.50_f32.max(s0) } else { (c.split_mid / 100.0).clamp(s0 as f64, 1.0) as f32 };
+ let s2 = if c.split_highlights.is_nan() { 0.75_f32.max(s1) } else { (c.split_highlights / 100.0).clamp(s1 as f64, 1.0) as f32 };
+ let clean_amt = |a: f64| if a.is_nan() { 0.0 } else { a.clamp(-100.0, 100.0) };
+ let (a0, a1, a2, a3) = (clean_amt(c.shadows), clean_amt(c.darks), clean_amt(c.lights), clean_amt(c.highlights));
let splits = [s0 as f64, s1 as f64, s2 as f64];
let amounts = [a0, a1, a2, a3];
let regions = [(0.0, s0, a0), (s0, s1, a1), (s1, s2, a2), (s2, 1.0, a3)];
@@ -45,6 +48,9 @@ impl ParametricCurve {
continue;
}
let (ctr, half) = ((a + b) / 2.0, (b - a) * 0.75 + 0.05);
+ if half <= f32::EPSILON {
+ continue;
+ }
let t = ((x - ctr) / half).clamp(-1.0, 1.0);
d += (amt / 100.0) as f32 * 0.22 * (0.5 + 0.5 * (t * std::f32::consts::PI).cos());
}
@@ -62,7 +68,7 @@ impl ParametricCurve {
/// The curve at `x` (0 = black, 1 = white), read off the table (so it never overshoots the ends).
pub fn at(&self, x: f64) -> f64 {
- let x = x.clamp(0.0, 1.0) as f32;
+ let x = if x.is_nan() { 0.0 } else { x.clamp(0.0, 1.0) as f32 };
let at = x * (PARAM_LUT - 1) as f32;
let i = at.floor().clamp(0.0, (PARAM_LUT - 1) as f32) as usize;
let cur = self.lut.get(i).copied().unwrap_or(0.0);
@@ -112,7 +118,7 @@ impl ParametricCurve {
/// windows tile 0..1, so this is always defined and a drag anywhere on the curve moves exactly
/// one slider — the region under the pointer.
pub fn region_at(&self, x: f64) -> usize {
- let x = x.clamp(0.0, 1.0);
+ let x = if x.is_nan() { 0.0 } else { x.clamp(0.0, 1.0) };
let s0 = self.split_x(0);
let s1 = self.split_x(1);
let s2 = self.split_x(2);
@@ -172,7 +178,7 @@ impl ParametricCurve {
((level.clamp(0.0, 1.0) - base) / per_unit).clamp(-100.0, 100.0).round()
}
- /// The graph position split `split` (0..3) sits at, 0..1.
+ /// The graph position split `split` (0..=2) sits at, 0..1.
pub fn split_x(&self, split: usize) -> f64 {
self.splits.get(split).copied().unwrap_or(0.5)
}
@@ -182,6 +188,25 @@ impl ParametricCurve {
mod tests {
use super::*;
+ #[test]
+ fn non_finite_or_inverted_splits_do_not_panic_or_poison_curve() {
+ let c = ToneCurve {
+ split_shadows: 80.0,
+ split_mid: 20.0,
+ split_highlights: f64::NAN,
+ shadows: f64::NAN,
+ lights: f64::INFINITY,
+ ..Default::default()
+ };
+ let model = ParametricCurve::new(&c);
+ assert!(model.at(0.5).is_finite());
+ assert!(model.at(f64::NAN).is_finite());
+ assert!(model.region_at(f64::NAN) < 4);
+ assert!((model.splits[0] - 0.8).abs() < 1e-6);
+ assert!((model.splits[1] - 0.8).abs() < 1e-6);
+ assert!((model.splits[2] - 0.8).abs() < 1e-6);
+ }
+
#[test]
fn horizontal_inversion_at_x_0_51_with_darks_60_preserves_zero_lights() {
let c = ToneCurve { darks: 60.0, lights: 0.0, ..Default::default() };
diff --git a/crates/ui-egui/src/panels/edit.rs b/crates/ui-egui/src/panels/edit.rs
index a1b6d362f..13471bd36 100644
--- a/crates/ui-egui/src/panels/edit.rs
+++ b/crates/ui-egui/src/panels/edit.rs
@@ -958,6 +958,9 @@ const PARAM_GRAB: f32 = 14.0;
/// gesture back, not the last frame of it), and every write goes through `develop.set`, so the
/// slider, the curve and what is drawn are the same state rather than three things kept in step.
fn param_drag(app: &mut LightcraftApp, ui: &mut egui::Ui, r: Rect, resp: &egui::Response, d: &DevelopSettings) {
+ if r.width() <= 1.0 || r.height() <= 1.0 {
+ return;
+ }
let t = Tokens::get(ui.ctx());
let model = ParametricCurve::new(&d.curve);
let to_screen = |x: f64, y: f64| pos2(r.left() + x as f32 * r.width(), r.bottom() - y as f32 * r.height());
@@ -1029,7 +1032,7 @@ fn param_drag(app: &mut LightcraftApp, ui: &mut egui::Ui, r: Rect, resp: &egui::
let shown = dragged.map(|(region, ..)| region).or_else(|| hovered.then(|| resp.hover_pos()).flatten().map(|q| model.region_at(from_screen_x(q))));
if let Some(region) = shown
&& let Some(&id) = PARAM_AMOUNTS.get(region)
- && let Some(v) = controls::get(d, id)
+ && let Some(v) = set.and_then(|(s_id, v)| (s_id == id).then_some(v)).or_else(|| controls::get(d, id))
{
let name = controls::find(id).map_or("", |s| s.label);
ui.painter().text(r.left_top() + vec2(6.0, 4.0), Align2::LEFT_TOP, format!("{name} {v:.0}"), t.font(11.0), t.text_dim);
@@ -1122,7 +1125,7 @@ fn curve_editor(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId, d: &Dev
p.add(egui::Shape::line(pts, Stroke::new(2.0, Color32::from_gray(220))));
param_drag(app, ui, r, &resp, d);
curve_footer(app, ui, d);
- for c in PARAM_AMOUNTS {
+ for &c in PARAM_AMOUNTS.iter().rev() {
control(app, ui, d, c, true);
}
control(app, ui, d, "curve.refineSaturation", true);