diff --git a/crates/algo/src/lens_flare.rs b/crates/algo/src/lens_flare.rs index d50fc2e504..71c6975bbc 100644 --- a/crates/algo/src/lens_flare.rs +++ b/crates/algo/src/lens_flare.rs @@ -180,6 +180,12 @@ fn add_level(level: &Level, weight: f32, g: &Geometry, light: &mut [[f32; 3]]) { let (r0, r1) = (r.r0 * g.unit, r.r1 * g.unit); let length = (r1 - r0).max(1e-6); let fade = r.fade.max(1e-3); + // Skip a ray whose segment misses this tile: none of its samples can land inside it. + let (sx, sy) = (g.centre.0 + r0 * cos, g.centre.1 + r0 * sin); + let (ex, ey) = (g.centre.0 + r1 * cos, g.centre.1 + r1 * sin); + if sx.max(ex) < ox - 1.0 || sx.min(ex) > ox + w as f32 + 1.0 || sy.max(ey) < oy - 1.0 || sy.min(ey) > oy + h as f32 + 1.0 { + continue; + } let steps = ((r1 - r0) / 0.35).ceil().clamp(0.0, 1e6) as usize; let mut last = None; for i in 0..steps { @@ -258,8 +264,8 @@ mod tests { let (lo, hi, f) = bracket(lv, mid).unwrap(); assert!(std::ptr::eq(lo, a) && std::ptr::eq(hi, b) && (f - 0.5).abs() < 1e-6); // Exactly on a level, and outside the measured range, one level is used as is. - let (lo, _, f) = bracket(lv, b.brightness).unwrap(); - assert!(std::ptr::eq(lo, b) || f == 1.0); + let (_, hi, f) = bracket(lv, b.brightness).unwrap(); + assert!(std::ptr::eq(hi, b) && f == 1.0); let (lo, hi, f) = bracket(lv, 1.0).unwrap(); assert!(std::ptr::eq(lo, a) && std::ptr::eq(hi, a) && f == 0.0); let last = lv.last().unwrap(); diff --git a/crates/algo/src/tests/ext.rs b/crates/algo/src/tests/ext.rs index 765e860372..88b5138ded 100644 --- a/crates/algo/src/tests/ext.rs +++ b/crates/algo/src/tests/ext.rs @@ -492,6 +492,25 @@ fn lens_flare_with_an_overflowing_centre_leaves_finite_pixels() { } } +#[test] +fn lens_flare_between_two_measured_brightnesses_blends_their_light() { + // 75 % and 100 % are measured levels; 87.5 % is halfway, so its light is their mean. On black + // 32-bit the result is the light itself; at 0 and 1 the clamp makes the blend nonlinear. + let black = flat(SampleType::F32, R, [0.0, 0.0, 0.0, 1.0]); + for lens in [LensType::Zoom, LensType::Prime35, LensType::Prime105, LensType::MoviePrime] { + let at = |brightness: f32| run(&black, &FilterParams::LensFlare { brightness, center_x: 0.25, center_y: 0.5, lens }).read_region(R); + let (lo, mid, hi) = (at(75.0), at(87.5), at(100.0)); + let mut checked = 0; + for ((l, m), h) in lo.iter().zip(&mid).zip(&hi) { + if [*l, *m, *h].iter().all(|v| *v > 0.0 && *v < 1.0) { + assert!((m - 0.5 * (l + h)).abs() < 1e-4, "{lens:?}: {m} is not the mean of {l} and {h}"); + checked += 1; + } + } + assert!(checked >= 100, "{lens:?}: too few unclipped values ({checked})"); + } +} + #[test] fn lens_flare_never_darkens_32_bit_values_above_one() { // #5785: the screen formula turned 2.0 into 1.5 (and 1.0 at the core).