LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
LR-EDIT-EFFECTS-GRAIN: skip grain on a degenerate frame instead of producing NaN pixels
Andrew Frahnf621d24753b7
diff --git a/crates/pipeline/src/finish.rs b/crates/pipeline/src/finish.rsindex c1e6a9bc2..b7ef8644a 100644--- a/crates/pipeline/src/finish.rs+++ b/crates/pipeline/src/finish.rs@@ -78,6 +78,10 @@ pub const GRAIN_HASH: [u32; 4] = [0x8da6_b343, 0xd816_3841, 0xcb1a_b31f, 0x5bd1_#[inline]fn grain_noise(x: f32, y: f32, seed: u32) -> f32 {+ // A degenerate frame (zero-width crop) maps pixels to NaN/inf coordinates: no grain there.+ if !x.is_finite() || !y.is_finite() {+ return 0.0;+ }let (x0, y0) = (x.floor(), y.floor());let (fx, fy) = (x - x0, y - y0);let h = |i: i32, j: i32| {@@ -207,7 +211,15 @@ impl FinishParams {};let ev = s.light.exposure as f32;let gain = 2f32.powf(ev);- let grain = (s.grain.amount > 0.0 && effects).then(|| {+ // A degenerate frame (zero-width crop, zero scale) has no finite grain mapping: skip grain (CPU and GPU).+ let out_to_norm = frame.out_to_norm(w, h);+ let finite_frame = px_per_long.is_finite()+ && px_per_long > 0.0+ && frame.ow.is_finite()+ && frame.oh.is_finite()+ && frame.ow.max(frame.oh) > 0.0+ && out_to_norm.0.iter().all(|v| v.is_finite());+ let grain = (s.grain.amount > 0.0 && effects && finite_frame).then(|| {let cell = (0.0006 + (s.grain.size / 100.0) as f32 * 0.0024) * px_per_long as f32;((s.grain.amount / 100.0) as f32 * 0.13, cell.max(0.6), (s.grain.roughness / 100.0) as f32, s.grain.seed)});@@ -239,7 +251,7 @@ impl FinishParams {gain,ev,grain,- out_to_norm: frame.out_to_norm(w, h),+ out_to_norm,ow: frame.ow,oh: frame.oh,w,@@ -642,7 +654,9 @@ pub(crate) fn finish_with<T: Copy + Default + Send>(lum = lum.clamp(0.0, 1.0);}let k = amt * g * (0.35 + 2.6 * lum * (1.0 - lum));- e = e.map(|v| v + k);+ if k.is_finite() {+ e = e.map(|v| v + k);+ }}let e = match &p_lut {Some((l, k)) if hdr => {@@ -767,6 +781,13 @@ fn enc(v: f32) -> u8 {mod tests {use super::*;+ #[test]+ fn grain_noise_is_finite_for_degenerate_coordinates() {+ for (x, y) in [(f32::NAN, 1.0), (1.0, f32::INFINITY), (f32::NEG_INFINITY, f32::NAN)] {+ assert_eq!(grain_noise(x, y, 7), 0.0);+ }+ }+#[test]fn refine_saturation_restores_the_pre_curve_saturation() {let before = [0.5, 0.3, 0.2];diff --git a/crates/pipeline/src/tests.rs b/crates/pipeline/src/tests.rsindex 23eca0ba7..5c5f1b95c 100644--- a/crates/pipeline/src/tests.rs+++ b/crates/pipeline/src/tests.rs@@ -840,3 +840,19 @@ fn sliver_crops_render_quickly_with_wide_stages_on() {assert!(r.image.width >= 1 && r.image.height >= 1, "{rect:?}");}}++/// A crop of no width used to give grain NaN coordinates (and NaN pixels); the render stays finite.+#[test]+fn zero_width_crop_with_grain_renders_finite_pixels() {+ let mut s = DevelopSettings::default();+ s.grain.amount = 60.0;+ s.crop.geometry.rect = lightcraft_geom::Rect::new(0.5, 0.1, 0.5, 0.9);+ let mut req = RenderRequest::fit(240, 240);+ req.depth = crate::OutputDepth::F32Linear;+ let r = render(&scene(), &SourceInfo::default(), &s, &req);+ if let Some(d) = r.deep+ && let crate::DeepSamples::F32(v) = d.samples+ {+ assert!(v.iter().all(|x| x.is_finite()), "non-finite sample in a zero-width crop with grain");+ }+}