Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Filter › Render › Relight: redirect a photo's lighting (#735)

Matt Van Hornb70bf524eae0
diff --git a/crates/algo/src/lib.rs b/crates/algo/src/lib.rs
index f3435f686..06b46faa6 100644
--- a/crates/algo/src/lib.rs
+++ b/crates/algo/src/lib.rs
@@ -52,6 +52,7 @@ pub mod poisson;
pub mod puppet;
pub mod pyramid;
pub mod quantize;
+mod relight;
mod render;
pub mod render2;
pub mod resample;
@@ -411,6 +412,16 @@ pub enum FilterParams {
height: f32,
white_is_high: bool,
},
+ /// Photographic relight: angle −180…180°, elevation 0…90°, intensity/ambient 0…100,
+ /// warmth −100…100, softness 1…100. Intensity 0 is an identity copy.
+ Relight {
+ angle: f32,
+ elevation: f32,
+ intensity: f32,
+ ambient: f32,
+ warmth: f32,
+ softness: f32,
+ },
// ---- Noise ----
/// Strength 0–10, the rest 0–100 %.
@@ -587,6 +598,7 @@ impl FilterParams {
FilterParams::Fibers { .. } => "Fibers",
FilterParams::LensFlare { .. } => "Lens Flare",
FilterParams::LightingEffects { .. } => "Lighting Effects",
+ FilterParams::Relight { .. } => "Relight",
FilterParams::ReduceNoise { .. } => "Reduce Noise",
FilterParams::SmartBlur { .. } => "Smart Blur",
FilterParams::LensBlur { .. } => "Lens Blur",
@@ -628,6 +640,8 @@ fn halo_ext(p: &FilterParams) -> Halo {
// Generators and per-pixel renders only need the bounds geometry (always in the context).
FilterParams::Fibers { .. } | FilterParams::LensFlare { .. } => Halo::Radius(0),
FilterParams::LightingEffects { .. } => r(1.0),
+ FilterParams::Relight { intensity, .. } if *intensity == 0.0 => Halo::Radius(0),
+ FilterParams::Relight { .. } => Halo::Radius(relight::HALO_RADIUS),
FilterParams::ReduceNoise { .. } => r(denoise::reach()),
FilterParams::SmartBlur { radius, .. } => r(*radius),
FilterParams::LensBlur { radius, .. } => r(*radius),
@@ -761,6 +775,12 @@ pub fn kernel(params: &FilterParams, src: &Image, out: Rect, ctx: &Ctx) -> Vec<f
white_is_high: *white_is_high,
},
),
+ FilterParams::Relight { angle, elevation, intensity, ambient, warmth, softness } => relight::relight(
+ src,
+ out,
+ ctx,
+ relight::Params { angle: *angle, elevation: *elevation, intensity: *intensity, ambient: *ambient, warmth: *warmth, softness: *softness },
+ ),
FilterParams::ReduceNoise { strength, preserve_details, reduce_color_noise, sharpen_details, remove_jpeg_artifact } => denoise::reduce_noise(
src,
out,
diff --git a/crates/algo/src/relight.rs b/crates/algo/src/relight.rs
new file mode 100644
index 000000000..ba8ab8231
--- /dev/null
+++ b/crates/algo/src/relight.rs
@@ -0,0 +1,453 @@
+//! Photographic relight: recover shading with a blur (Retinex) and replace it with a
+//! Lambertian lamp whose direction comes from angle / elevation.
+
+use photocraft_geom::Rect;
+
+use crate::Ctx;
+use crate::fxutil::{MAXC, rgba, set_rgba, xy};
+use crate::image::Image;
+
+/// Neighbourhood the kernel reads: `3σ + 1` at `σ = 32` (the typical cap for parallel tiles).
+pub(crate) const HALO_RADIUS: i32 = 97;
+
+/// Dialog units (degrees / 0–100), matching the command params.
+#[derive(Clone, Copy, Debug)]
+pub(crate) struct Params {
+ pub angle: f32,
+ pub elevation: f32,
+ pub intensity: f32,
+ pub ambient: f32,
+ pub warmth: f32,
+ pub softness: f32,
+}
+
+/// Blur σ from softness and the reference bounds, clamped to `[2, 96]`.
+pub(crate) fn sigma(softness: f32, bounds: Rect) -> f32 {
+ let side = bounds.width().min(bounds.height()) as f32;
+ let s = (softness.clamp(1.0, 100.0) / 100.0) * 0.08 * side.max(1.0);
+ s.clamp(2.0, 96.0)
+}
+
+/// Infinite-light direction: 0° from the right, 90° from above (image space, y down).
+fn light_dir(angle: f32, elevation: f32) -> [f32; 3] {
+ let (s, co) = angle.to_radians().sin_cos();
+ let e = elevation.clamp(0.0, 90.0).to_radians();
+ norm3([co * e.cos(), -s * e.cos(), e.sin()])
+}
+
+fn norm3(v: [f32; 3]) -> [f32; 3] {
+ let m = (v[0] * v[0] + v[1] * v[1] + v[2] * v[2]).sqrt();
+ if !m.is_finite() || m < 1e-9 { [0.0, 0.0, 1.0] } else { [v[0] / m, v[1] / m, v[2] / m] }
+}
+
+fn light_color(warmth: f32) -> [f32; 3] {
+ let w = warmth.clamp(-100.0, 100.0) / 100.0;
+ if w >= 0.0 {
+ [1.0 + 0.15 * w, 1.0 - 0.05 * w, 1.0 - 0.25 * w]
+ } else {
+ let t = -w;
+ [1.0 - 0.20 * t, 1.0 - 0.10 * t, 1.0 + 0.15 * t]
+ }
+}
+
+fn rec709_y(c: [f32; 4]) -> f32 {
+ 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
+}
+
+fn finite_all(v: &[f32]) -> bool {
+ v.iter().all(|x| x.is_finite())
+}
+
+fn sample_y(buf: &[f32], w: usize, h: usize, rect: Rect, x: i32, y: i32) -> f32 {
+ if rect.is_empty() || w == 0 || h == 0 {
+ return 0.04;
+ }
+ let xx = x.clamp(rect.x0, rect.x1.saturating_sub(1));
+ let yy = y.clamp(rect.y0, rect.y1.saturating_sub(1));
+ let ox = (xx - rect.x0) as usize;
+ let oy = (yy - rect.y0) as usize;
+ if ox >= w || oy >= h {
+ return 0.04;
+ }
+ buf.get(oy.saturating_mul(w).saturating_add(ox)).copied().filter(|v| v.is_finite()).unwrap_or(0.04)
+}
+
+/// Separable Gaussian of a single-channel field. Non-finite samples are skipped (kernel
+/// renormalised) so a bad pixel does not poison its neighbours.
+fn blur_channel(src: &[f32], w: usize, h: usize, sigma: f32) -> Vec<f32> {
+ let n = w.saturating_mul(h);
+ if n == 0 || src.len() < n {
+ return vec![0.04; n];
+ }
+ let k = crate::blur::gaussian_kernel(sigma);
+ let r = (k.len() / 2) as i32;
+ let acc_at = |buf: &[f32], ww: usize, hh: usize, x: i32, y: i32, col: bool| -> f32 {
+ let mut acc = 0.0f64;
+ let mut wgt = 0.0f64;
+ for (i, kv) in k.iter().enumerate() {
+ let t = i as i32 - r;
+ let (sx, sy) = if col { (x, y + t) } else { (x + t, y) };
+ if sx < 0 || sy < 0 || (sx as usize) >= ww || (sy as usize) >= hh {
+ continue;
+ }
+ let Some(v) = buf.get((sy as usize).saturating_mul(ww).saturating_add(sx as usize)) else {
+ continue;
+ };
+ if !v.is_finite() {
+ continue;
+ }
+ acc += *v as f64 * *kv as f64;
+ wgt += *kv as f64;
+ }
+ if wgt > 1e-12 { (acc / wgt) as f32 } else { 0.5 }
+ };
+ let mut tmp = vec![0.0f32; n];
+ for y in 0..h {
+ for x in 0..w {
+ if let Some(slot) = tmp.get_mut(y.saturating_mul(w).saturating_add(x)) {
+ *slot = acc_at(src, w, h, x as i32, y as i32, false);
+ }
+ }
+ }
+ let mut out = vec![0.0f32; n];
+ for y in 0..h {
+ for x in 0..w {
+ if let Some(slot) = out.get_mut(y.saturating_mul(w).saturating_add(x)) {
+ *slot = acc_at(&tmp, w, h, x as i32, y as i32, true);
+ }
+ }
+ }
+ out
+}
+
+fn read_rgba(src: &Image, ctx: &Ctx, x: i32, y: i32) -> [f32; 4] {
+ let n = src.ch.min(MAXC);
+ let mut tmp = [0.0f32; MAXC];
+ for c in 0..n {
+ if let Some(slot) = tmp.get_mut(c) {
+ *slot = src.get(x, y, c);
+ }
+ }
+ rgba(ctx, &tmp[..n])
+}
+
+/// Relight `out` of `src`. Identity when `intensity` is 0 (or not finite).
+pub(crate) fn relight(src: &Image, out: Rect, ctx: &Ctx, p: Params) -> Vec<f32> {
+ let n = src.ch;
+ if out.is_empty() || n == 0 {
+ return src.crop(out);
+ }
+ let mut res = src.crop(out);
+ if !p.intensity.is_finite() || p.intensity == 0.0 {
+ return res;
+ }
+ let sig = sigma(p.softness, ctx.bounds);
+ let radius = (sig * 3.0).ceil() as i32;
+ // Finite differences at the shading scale (`σ`), not 1 px: a 1 px step vanishes on
+ // large documents and the lamp collapses to a flat multiply.
+ const SCALE: f32 = 2.0;
+ let step = sig.round().clamp(1.0, 32.0) as i32;
+ let s_rect = out.inflate(step.max(1));
+ let y_rect = s_rect.inflate(radius.max(0));
+ if y_rect.is_empty() {
+ return res;
+ }
+ let yw = y_rect.width() as usize;
+ let yh = y_rect.height() as usize;
+ let n_px = yw.saturating_mul(yh);
+ if n_px == 0 || n_px > 8_000_000 {
+ return res;
+ }
+ let mut ybuf = vec![0.5f32; n_px];
+ for y in y_rect.y0..y_rect.y1 {
+ for x in y_rect.x0..y_rect.x1 {
+ let ox = (x - y_rect.x0) as usize;
+ let oy = (y - y_rect.y0) as usize;
+ let Some(slot) = ybuf.get_mut(oy.saturating_mul(yw).saturating_add(ox)) else {
+ continue;
+ };
+ let c = read_rgba(src, ctx, x, y);
+ if !finite_all(&c) {
+ *slot = f32::NAN;
+ continue;
+ }
+ let a = if ctx.alpha { c[3] } else { 1.0 };
+ *slot = if a > 0.0 && a.is_finite() { rec709_y(c) } else { f32::NAN };
+ }
+ }
+ let sbuf = blur_channel(&ybuf, yw, yh, sig);
+ let ldir = light_dir(p.angle, p.elevation);
+ let lcol = light_color(p.warmth);
+ let amb = (if p.ambient.is_finite() { p.ambient } else { 0.0 }).clamp(0.0, 100.0) / 100.0;
+ let inten = p.intensity.clamp(0.0, 100.0) / 100.0;
+ for (i, px) in res.chunks_exact_mut(n).enumerate() {
+ let (x, y) = xy(out, i);
+ if !finite_all(px) {
+ continue;
+ }
+ let a = if ctx.alpha { px[n - 1] } else { 1.0 };
+ if ctx.alpha && a <= 0.0 {
+ continue;
+ }
+ let c = rgba(ctx, px);
+ if !finite_all(&c) {
+ continue;
+ }
+ let s0 = sample_y(&sbuf, yw, yh, y_rect, x, y).max(0.04);
+ let nx = (sample_y(&sbuf, yw, yh, y_rect, x - step, y) - sample_y(&sbuf, yw, yh, y_rect, x + step, y)) * SCALE;
+ let ny = (sample_y(&sbuf, yw, yh, y_rect, x, y - step) - sample_y(&sbuf, yw, yh, y_rect, x, y + step)) * SCALE;
+ let nrm = norm3([nx, ny, 1.0]);
+ let ndl = (nrm[0] * ldir[0] + nrm[1] * ldir[1] + nrm[2] * ldir[2]).max(0.0);
+ let shade = amb + inten * ndl;
+ let mut out_c = c;
+ for (k, lc) in lcol.iter().enumerate() {
+ let Some(ch) = c.get(k).copied() else { continue };
+ let Some(slot) = out_c.get_mut(k) else { continue };
+ *slot = (ch / s0) * shade * *lc;
+ }
+ if !finite_all(&out_c) {
+ continue;
+ }
+ set_rgba(ctx, px, out_c);
+ if ctx.alpha
+ && let Some(slot) = px.get_mut(n - 1)
+ {
+ *slot = a;
+ }
+ }
+ res
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::{FilterParams, Halo, apply, apply_tiled, output_area};
+ use photocraft_color::{ColorMode, PixelFormat, SampleType};
+ use photocraft_raster::Surface;
+
+ fn fmt(s: SampleType) -> PixelFormat {
+ PixelFormat::new(ColorMode::Rgb, s, true)
+ }
+
+ fn gray_fmt(s: SampleType) -> PixelFormat {
+ PixelFormat::new(ColorMode::Grayscale, s, true)
+ }
+
+ fn params(intensity: f32, angle: f32, warmth: f32, softness: f32) -> FilterParams {
+ FilterParams::Relight { angle, elevation: 40.0, intensity, ambient: 55.0, warmth, softness }
+ }
+
+ fn relight_p(angle: f32, elevation: f32, intensity: f32, ambient: f32, warmth: f32, softness: f32) -> FilterParams {
+ FilterParams::Relight { angle, elevation, intensity, ambient, warmth, softness }
+ }
+
+ fn run(s: &Surface, p: &FilterParams, bounds: Rect) -> Surface {
+ let area = output_area(p, s.content_bounds(), bounds, None);
+ apply(s, p, area, bounds, None)
+ }
+
+ fn max_diff(a: &[f32], b: &[f32]) -> f32 {
+ a.iter().zip(b).map(|(x, y)| (x - y).abs()).fold(0.0, f32::max)
+ }
+
+ fn luma(px: &[f32]) -> f32 {
+ 0.2126 * px[0] + 0.7152 * px[1] + 0.0722 * px[2]
+ }
+
+ fn shaded_sphere(st: SampleType, size: i32) -> Surface {
+ let r = Rect::new(0, 0, size, size);
+ let mut surf = Surface::new(fmt(st));
+ let cx = size as f32 / 2.0;
+ let rad = size as f32 * 0.42;
+ let mut v = Vec::new();
+ for y in 0..size {
+ for x in 0..size {
+ let dx = x as f32 + 0.5 - cx;
+ let dy = y as f32 + 0.5 - cx;
+ let d2 = dx * dx + dy * dy;
+ if d2 <= rad * rad {
+ let nz = (rad * rad - d2).max(0.0).sqrt() / rad;
+ let sh = 0.22 + 0.78 * nz;
+ v.extend_from_slice(&[0.55 * sh, 0.55 * sh, 0.55 * sh, 1.0]);
+ } else {
+ v.extend_from_slice(&[0.18, 0.18, 0.18, 1.0]);
+ }
+ }
+ }
+ surf.write_region(r, &v);
+ surf
+ }
+
+ fn sphere_half_mean(s: &Surface, size: i32, left: bool) -> f32 {
+ let cx = size as f32 / 2.0;
+ let rad = size as f32 * 0.42;
+ let mut sum = 0.0;
+ let mut n: f32 = 0.0;
+ for y in 0..size {
+ for x in 0..size {
+ let dx = x as f32 + 0.5 - cx;
+ let dy = y as f32 + 0.5 - cx;
+ if dx * dx + dy * dy > rad * rad {
+ continue;
+ }
+ if left && (x as f32) >= cx - 4.0 {
+ continue;
+ }
+ if !left && (x as f32) < cx + 4.0 {
+ continue;
+ }
+ sum += luma(&s.pixel(x, y));
+ n += 1.0;
+ }
+ }
+ sum / n.max(1.0)
+ }
+
+ fn pattern(st: SampleType, r: Rect) -> Surface {
+ let mut surf = Surface::new(fmt(st));
+ let mut v = Vec::new();
+ for y in r.y0..r.y1 {
+ for x in r.x0..r.x1 {
+ v.extend_from_slice(&[((x * 7 + y * 3) % 64) as f32 / 63.0, ((x * x + y) % 50) as f32 / 49.0, (y % 9) as f32 / 8.0, 1.0]);
+ }
+ }
+ surf.write_region(r, &v);
+ surf
+ }
+
+ fn flat(st: SampleType, r: Rect, px: [f32; 4]) -> Surface {
+ let mut surf = Surface::new(fmt(st));
+ surf.fill_rect(r, &px);
+ surf
+ }
+
+ #[test]
+ fn halo_is_zero_at_identity_and_97_otherwise() {
+ assert_eq!(params(0.0, 45.0, 0.0, 25.0).halo(), Halo::Radius(0));
+ assert_eq!(params(40.0, 45.0, 0.0, 25.0).halo(), Halo::Radius(HALO_RADIUS));
+ }
+
+ #[test]
+ fn identity_at_zero_intensity_all_depths() {
+ let r = Rect::new(0, 0, 24, 16);
+ let p = params(0.0, 120.0, 80.0, 90.0);
+ for st in [SampleType::U8, SampleType::U16, SampleType::F32] {
+ let s = pattern(st, r);
+ let out = run(&s, &p, r);
+ assert_eq!(out.read_region(r), s.read_region(r), "{st:?}");
+ }
+ let mut g = Surface::new(gray_fmt(SampleType::U8));
+ g.fill_rect(r, &[0.4, 1.0]);
+ let out = run(&g, &p, r);
+ assert_eq!(out.read_region(r), g.read_region(r));
+ let mut g32 = Surface::new(gray_fmt(SampleType::F32));
+ g32.fill_rect(r, &[0.4, 1.0]);
+ let out = run(&g32, &p, r);
+ assert_eq!(out.read_region(r), g32.read_region(r));
+ let mut g16 = Surface::new(gray_fmt(SampleType::U16));
+ g16.fill_rect(r, &[0.4, 1.0]);
+ let out = run(&g16, &p, r);
+ assert_eq!(out.read_region(r), g16.read_region(r));
+ }
+
+ fn assert_direction_flips(size: i32) {
+ let bounds = Rect::new(0, 0, size, size);
+ let s = shaded_sphere(SampleType::F32, size);
+ let left_lit = run(&s, &relight_p(180.0, 30.0, 90.0, 12.0, 0.0, 15.0), bounds);
+ let right_lit = run(&s, &relight_p(0.0, 30.0, 90.0, 12.0, 0.0, 15.0), bounds);
+ let l_left = sphere_half_mean(&left_lit, size, true);
+ let l_right = sphere_half_mean(&left_lit, size, false);
+ let r_left = sphere_half_mean(&right_lit, size, true);
+ let r_right = sphere_half_mean(&right_lit, size, false);
+ assert!(l_left - l_right >= 0.05, "light from the left ({size}px): {l_left} vs {l_right}");
+ assert!(r_right - r_left >= 0.05, "light from the right ({size}px): {r_left} vs {r_right}");
+ }
+
+ #[test]
+ fn direction_flips_on_a_shaded_sphere() {
+ assert_direction_flips(64);
+ }
+
+ #[test]
+ fn direction_flips_on_a_large_shaded_sphere() {
+ // A 1 px gradient vanishes at this size unless the kernel steps by σ.
+ assert_direction_flips(256);
+ }
+
+ #[test]
+ fn warmth_tints_a_neutral_field() {
+ let r = Rect::new(0, 0, 32, 32);
+ let s = flat(SampleType::F32, r, [0.5, 0.5, 0.5, 1.0]);
+ let warm = run(&s, &relight_p(45.0, 40.0, 50.0, 55.0, 80.0, 25.0), r);
+ let cool = run(&s, &relight_p(45.0, 40.0, 50.0, 55.0, -80.0, 25.0), r);
+ let w = warm.pixel(16, 16);
+ let c = cool.pixel(16, 16);
+ assert!(w[0] > w[2], "warmth should raise R over B: {w:?}");
+ assert!(c[2] > c[0], "cool light should raise B over R: {c:?}");
+ }
+
+ #[test]
+ fn high_ambient_keeps_white_finite() {
+ let r = Rect::new(0, 0, 16, 16);
+ let s = flat(SampleType::F32, r, [1.0, 1.0, 1.0, 1.0]);
+ let out = run(&s, &relight_p(45.0, 40.0, 40.0, 100.0, 0.0, 25.0), r);
+ let px = out.pixel(8, 8);
+ assert!(finite_all(&px), "{px:?}");
+ }
+
+ #[test]
+ fn alpha_is_preserved() {
+ let r = Rect::new(0, 0, 16, 16);
+ let mut s = Surface::new(fmt(SampleType::F32));
+ s.fill_rect(r, &[0.4, 0.4, 0.5, 1.0]);
+ s.write_pixel(3, 3, &[0.4, 0.4, 0.5, 0.0]);
+ s.write_pixel(4, 4, &[0.2, 0.3, 0.4, 0.5]);
+ let out = run(&s, &params(50.0, 180.0, 20.0, 25.0), r);
+ assert_eq!(out.pixel(3, 3)[3], 0.0);
+ assert!((out.pixel(5, 5)[3] - 1.0).abs() < 1e-6);
+ assert!((out.pixel(4, 4)[3] - 0.5).abs() < 1e-6);
+ }
+
+ #[test]
+ fn tiles_match_a_full_apply() {
+ let size = 64;
+ let bounds = Rect::new(0, 0, size, size);
+ let s = shaded_sphere(SampleType::F32, size);
+ let p = relight_p(180.0, 40.0, 60.0, 40.0, 0.0, 25.0);
+ let area = output_area(&p, s.content_bounds(), bounds, None);
+ let full = apply_tiled(&s, &p, area, bounds, None, 256, None);
+ let tiled = apply_tiled(&s, &p, area, bounds, None, 32, None);
+ let inner = Rect::new(0, 0, size, size);
+ let d = max_diff(&full.read_region(inner), &tiled.read_region(inner));
+ assert!(d <= 1e-4, "tile seam {d}");
+ }
+
+ #[test]
+ fn hostile_sizes_do_not_panic() {
+ let p = params(40.0, 45.0, 0.0, 25.0);
+ for (w, h) in [(1, 1), (2, 2), (1, 2), (0, 0)] {
+ let r = Rect::new(0, 0, w, h);
+ let mut s = Surface::new(fmt(SampleType::F32));
+ if w > 0 && h > 0 {
+ s.fill_rect(r, &[0.4, 0.5, 0.6, 1.0]);
+ }
+ let _ = run(&s, &p, if r.is_empty() { Rect::new(0, 0, 1, 1) } else { r });
+ let _ = apply(&s, &p, Rect::EMPTY, Rect::new(0, 0, 4, 4), None);
+ }
+ }
+
+ #[test]
+ fn nan_and_inf_pixels_pass_through() {
+ let r = Rect::new(0, 0, 24, 24);
+ let mut s = pattern(SampleType::F32, r);
+ s.write_pixel(5, 5, &[f32::NAN, 0.4, 0.4, 1.0]);
+ s.write_pixel(6, 8, &[f32::INFINITY, 0.2, 0.2, 1.0]);
+ let before_n = s.pixel(7, 5);
+ let out = run(&s, &params(50.0, 180.0, 0.0, 20.0), r);
+ assert!(out.pixel(5, 5)[0].is_nan());
+ assert!(out.pixel(6, 8)[0].is_infinite());
+ let n = out.pixel(7, 5);
+ assert!(finite_all(&n));
+ assert_ne!(n, before_n);
+ }
+}
diff --git a/crates/algo/src/tests_ext.rs b/crates/algo/src/tests_ext.rs
index 61bdfce64..14d9d70fe 100644
--- a/crates/algo/src/tests_ext.rs
+++ b/crates/algo/src/tests_ext.rs
@@ -74,6 +74,7 @@ fn new_filters() -> Vec<FilterParams> {
height: 50.0,
white_is_high: true,
},
+ FilterParams::Relight { angle: 45.0, elevation: 40.0, intensity: 40.0, ambient: 55.0, warmth: 0.0, softness: 25.0 },
FilterParams::ReduceNoise { strength: 6.0, preserve_details: 60.0, reduce_color_noise: 45.0, sharpen_details: 25.0, remove_jpeg_artifact: true },
FilterParams::SmartBlur { radius: 3.0, threshold: 40.0, quality: BlurQuality::High, mode: SmartBlurMode::Normal },
FilterParams::SmartBlur { radius: 3.0, threshold: 25.0, quality: BlurQuality::Low, mode: SmartBlurMode::OverlayEdge },
diff --git a/crates/engine/src/filters_ext.rs b/crates/engine/src/filters_ext.rs
index 7fae4a211..d21980816 100644
--- a/crates/engine/src/filters_ext.rs
+++ b/crates/engine/src/filters_ext.rs
@@ -1,5 +1,5 @@
//! The second batch of `filter.*` commands: Pixelate, Stylize, Distort (Displace, Shear,
-//! ZigZag), Render (Fibers, Lens Flare, Lighting Effects), Reduce Noise, Smart/Lens/Shape Blur,
+//! ZigZag), Render (Fibers, Lens Flare, Lighting Effects, Relight), Reduce Noise, Smart/Lens/Shape Blur,
//! Blur Gallery, Other (Custom, HSB/HSL) and Video.
//!
//! They run through [`crate::filters::run_filter`] (selection, smart objects, history and
@@ -190,6 +190,14 @@ pub fn params_for(id: &str, p: &Value) -> Option<FilterParams> {
_ => LensType::Zoom,
},
},
+ "filter.render.relight" => FilterParams::Relight {
+ angle: f(p, "angle", 45.0).clamp(-180.0, 180.0),
+ elevation: f(p, "elevation", 40.0).clamp(0.0, 90.0),
+ intensity: f(p, "intensity", 40.0).clamp(0.0, 100.0),
+ ambient: f(p, "ambient", 55.0).clamp(0.0, 100.0),
+ warmth: f(p, "warmth", 0.0).clamp(-100.0, 100.0),
+ softness: f(p, "softness", 25.0).clamp(1.0, 100.0),
+ },
"filter.render.lightingEffects" => FilterParams::LightingEffects {
lights: list::<Light>(p, "lights").unwrap_or_else(|| {
vec![Light {
@@ -436,6 +444,60 @@ pub(crate) fn resolve_in_layer(fp: &mut FilterParams, layer: &Layer, bounds: Rec
}
}
+fn relight_bad(msg: &str) -> EngineError {
+ EngineError::BadParams { cmd: "filter.render.relight".into(), msg: msg.into() }
+}
+
+fn relight_num(p: &Value, key: &str, default: f32, min: f32, max: f32) -> Result<f32> {
+ match p.get(key) {
+ None => Ok(default),
+ Some(Value::Number(n)) => {
+ let Some(x) = n.as_f64() else {
+ return Err(relight_bad(&format!("\"{key}\" must be a finite number")));
+ };
+ let x = x as f32;
+ if !x.is_finite() {
+ return Err(relight_bad(&format!("\"{key}\" must be finite")));
+ }
+ if x < min || x > max {
+ return Err(relight_bad(&format!("\"{key}\" must be in {min}..{max}")));
+ }
+ Ok(x)
+ }
+ Some(_) => Err(relight_bad(&format!("\"{key}\" must be a number"))),
+ }
+}
+
+fn validate_relight(p: &Value) -> Result<()> {
+ if p.is_null() {
+ return Err(relight_bad("params must be a JSON object"));
+ }
+ if !p.is_object() {
+ return Err(relight_bad("params must be a JSON object"));
+ }
+ relight_num(p, "angle", 45.0, -180.0, 180.0)?;
+ relight_num(p, "elevation", 40.0, 0.0, 90.0)?;
+ relight_num(p, "intensity", 40.0, 0.0, 100.0)?;
+ relight_num(p, "ambient", 55.0, 0.0, 100.0)?;
+ relight_num(p, "warmth", 0.0, -100.0, 100.0)?;
+ relight_num(p, "softness", 25.0, 1.0, 100.0)?;
+ Ok(())
+}
+
+fn relight_enabled(s: &Session) -> std::result::Result<(), String> {
+ crate::filters::has_filterable_layer(s)?;
+ if crate::channel_cmds::edits_channel(s) {
+ return Ok(());
+ }
+ let l = crate::active_layer_of(s)?;
+ if l.locks.pixels || l.locks.all { Err(format!("the layer \"{}\" is locked", l.name)) } else { Ok(()) }
+}
+
+fn run_relight(s: &mut Session, p: &Value) -> Result<Value> {
+ validate_relight(p)?;
+ crate::filters::run_filter(s, "filter.render.relight", p)
+}
+
macro_rules! cmd {
($id:literal, $label:literal, [$($m:literal),*], $params:literal) => {
CommandSpec {
@@ -526,6 +588,16 @@ pub fn specs() -> Vec<CommandSpec> {
["Filter", "Render"],
r##"{"lightType":"spot|point|infinite","intensity":-100..100=75,"lightX":0..1=0.25,"lightY":0..1=0.2,"lightZ":0..2=0.6,"targetX":0..1=0.5,"targetY":0..1=0.55,"cone":1..89=45,"hotspot":0..100=50,"angle":-180..180=135,"elevation":0..90=45,"gloss":-100..100=0,"metallic":-100..100=0,"exposure":-100..100=0,"ambience":-100..100=8,"texture":"none|red|green|blue|alpha|luminance","height":0..100=50,"whiteIsHigh":bool=true,"lights":json}"##
),
+ CommandSpec {
+ id: "filter.render.relight",
+ label: "Relight…",
+ menu: &["Filter", "Render"],
+ shortcut: None,
+ params: r##"{"angle":-180..180=45,"elevation":0..90=40,"intensity":0..100=40,"ambient":0..100=55,"warmth":-100..100=0,"softness":1..100=25}"##,
+ enabled: relight_enabled,
+ run: run_relight,
+ journal: true,
+ },
// Noise
cmd!(
"filter.noise.reduceNoise",
diff --git a/crates/engine/src/filters_ext/tests.rs b/crates/engine/src/filters_ext/tests.rs
index 67d6897bd..b616101f7 100644
--- a/crates/engine/src/filters_ext/tests.rs
+++ b/crates/engine/src/filters_ext/tests.rs
@@ -24,7 +24,7 @@ fn pixels(s: &Session) -> Vec<f32> {
d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 48, 32))
}
-const IDS: [&str; 33] = [
+const IDS: [&str; 34] = [
"filter.pixelate.colorHalftone",
"filter.pixelate.crystallize",
"filter.pixelate.facet",
@@ -42,6 +42,7 @@ const IDS: [&str; 33] = [
"filter.render.fibers",
"filter.render.lensFlare",
"filter.render.lightingEffects",
+ "filter.render.relight",
"filter.noise.reduceNoise",
"filter.blur.smartBlur",
"filter.blur.lensBlur",
@@ -214,4 +215,136 @@ fn params_map_to_algorithm_units() {
let Some(FilterParams::Shear { points, .. }) = params_for("filter.distort.shear", &json!({"points": [[0, 0.1], [1, -0.1]]})) else { panic!() };
assert_eq!(points, vec![[0.0, 0.1], [1.0, -0.1]]);
assert!(params_for("filter.nope", &json!({})).is_none());
+ assert_eq!(
+ params_for("filter.render.relight", &json!({})),
+ Some(FilterParams::Relight { angle: 45.0, elevation: 40.0, intensity: 40.0, ambient: 55.0, warmth: 0.0, softness: 25.0 })
+ );
+}
+
+fn paint_shaded_disc(s: &mut Session, size: i32) {
+ s.edit("disc", |doc, active| {
+ let surf = doc.layer_mut(active.unwrap()).unwrap().surface_mut().unwrap();
+ let cx = size as f32 / 2.0;
+ let rad = size as f32 * 0.42;
+ for y in 0..size {
+ for x in 0..size {
+ let dx = x as f32 + 0.5 - cx;
+ let dy = y as f32 + 0.5 - cx;
+ let d2 = dx * dx + dy * dy;
+ let px = if d2 <= rad * rad {
+ let nz = (rad * rad - d2).max(0.0).sqrt() / rad;
+ let sh = 0.22 + 0.78 * nz;
+ [0.55 * sh, 0.55 * sh, 0.55 * sh, 1.0]
+ } else {
+ [0.18, 0.18, 0.18, 1.0]
+ };
+ surf.write_pixel(x, y, &px);
+ }
+ }
+ Ok(())
+ })
+ .unwrap();
+}
+
+fn relight_session(w: i32, h: i32) -> Session {
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": w, "height": h})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ paint_shaded_disc(&mut s, w.min(h));
+ s
+}
+
+#[test]
+fn relight_command_exists_and_toggles_with_document() {
+ let spec = crate::commands::find("filter.render.relight").expect("registered");
+ assert_eq!(spec.menu, &["Filter", "Render"]);
+ assert_eq!(spec.label, "Relight…");
+ assert!(spec.journal);
+ let mut s = Session::new();
+ assert!(!s.is_enabled("filter.render.relight"));
+ s.execute("file.new", json!({"width": 32, "height": 32})).unwrap();
+ s.execute("layer.new.layer", json!({})).unwrap();
+ assert!(s.is_enabled("filter.render.relight"));
+ s.execute("type.create", json!({"x": 8, "y": 16, "text": "Hi", "size": 12})).unwrap();
+ assert!(!s.is_enabled("filter.render.relight"));
+}
+
+#[test]
+fn relight_is_disabled_on_a_pixel_locked_layer() {
+ let mut s = relight_session(32, 32);
+ assert!(s.is_enabled("filter.render.relight"));
+ s.execute("layer.setProps", json!({"locks": {"pixels": true}})).unwrap();
+ assert!(!s.is_enabled("filter.render.relight"));
+ assert!(s.execute("filter.render.relight", json!({})).is_err());
+}
+
+#[test]
+fn relight_defaults_change_a_shaded_disc_and_undo() {
+ let mut s = relight_session(48, 48);
+ let before = {
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 48, 48))
+ };
+ let depth = s.active().unwrap().history.past_len();
+ s.execute("filter.render.relight", json!({})).unwrap();
+ let after = {
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 48, 48))
+ };
+ assert_ne!(after, before);
+ assert_eq!(s.active().unwrap().history.past_len(), depth + 1);
+ s.execute("edit.undo", json!({})).unwrap();
+ let undone = {
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 48, 48))
+ };
+ assert_eq!(undone, before);
+}
+
+#[test]
+fn relight_records_a_smart_filter_and_can_be_hidden() {
+ let mut s = relight_session(32, 32);
+ let before = {
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 32, 32))
+ };
+ s.execute("filter.convertForSmartFilters", json!({})).unwrap();
+ s.execute("filter.render.relight", json!({"angle": 180, "intensity": 70})).unwrap();
+ let (is_relight, lit) = {
+ let d = s.active().unwrap();
+ let layer = d.doc.layer(d.active_layer.unwrap()).unwrap();
+ let photocraft_doc::LayerContent::Smart(sm) = &layer.content else { panic!("expected a smart object") };
+ let ok = sm.smart_filters.iter().any(|f| f.command == "filter.render.relight");
+ (ok, layer.surface().unwrap().read_region(Rect::new(0, 0, 32, 32)))
+ };
+ assert!(is_relight);
+ assert_ne!(lit, before);
+ s.execute("layer.smartFilter.setVisible", json!({"index": 0, "visible": false})).unwrap();
+ let restored = {
+ let d = s.active().unwrap();
+ d.doc.layer(d.active_layer.unwrap()).unwrap().surface().unwrap().read_region(Rect::new(0, 0, 32, 32))
+ };
+ assert_eq!(restored, before);
+}
+
+#[test]
+fn relight_rejects_bad_params() {
+ let mut s = relight_session(16, 16);
+ assert!(s.execute("filter.render.relight", json!({"intensity": 999})).is_err());
+ assert!(s.execute("filter.render.relight", json!({"elevation": -3})).is_err());
+ assert!(s.execute("filter.render.relight", json!({"angle": "east"})).is_err());
+ assert!(s.execute("filter.render.relight", json!({"softness": 0})).is_err());
+ assert!(s.execute("filter.render.relight", json!(null)).is_err());
+}
+
+#[test]
+fn relight_honours_the_selection() {
+ let mut s = relight_session(48, 32);
+ s.execute("select.rect", json!({"x": 0, "y": 0, "width": 12, "height": 12})).unwrap();
+ let before = pixels(&s);
+ s.execute("filter.render.relight", json!({"angle": 180, "intensity": 70})).unwrap();
+ let after = pixels(&s);
+ let far = (28 * 48 + 40) * 4;
+ assert_eq!(after[far..far + 4], before[far..far + 4]);
+ assert_ne!(after[..12 * 4], before[..12 * 4]);
}
diff --git a/crates/ui-egui/src/filter_dialog.rs b/crates/ui-egui/src/filter_dialog.rs
index beafc714a..1c0733966 100644
--- a/crates/ui-egui/src/filter_dialog.rs
+++ b/crates/ui-egui/src/filter_dialog.rs
@@ -471,6 +471,7 @@ mod tests {
"filter.distort.displace",
"filter.pixelate.mezzotint",
"filter.render.lightingEffects",
+ "filter.render.relight",
] {
assert!(has_dialog(id), "{id}");
}
diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsv
index ec2361ea9..ea8b41224 100644
--- a/crates/ui-egui/src/i18n/cs.tsv
+++ b/crates/ui-egui/src/i18n/cs.tsv
@@ -531,6 +531,7 @@
Flame… Plamen…
Lens Flare… Odlesk objektivu…
Lighting Effects… Světelné efekty…
+ Relight… Přeosvětlení…
Picture Frame… Rám obrazu…
Tree… Strom…
Sharpen Doostření
diff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsv
index ecdb9ba61..bd3f61e1c 100644
--- a/crates/ui-egui/src/i18n/de.tsv
+++ b/crates/ui-egui/src/i18n/de.tsv
@@ -527,6 +527,7 @@
Flame… Flamme…
Lens Flare… Objektivreflex…
Lighting Effects… Beleuchtungseffekte…
+ Relight… Neu beleuchten…
Picture Frame… Bilderrahmen…
Tree… Baum…
Sharpen Scharfzeichnen
diff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsv
index 3ae2abe67..6f72b9cd7 100644
--- a/crates/ui-egui/src/i18n/es.tsv
+++ b/crates/ui-egui/src/i18n/es.tsv
@@ -523,6 +523,7 @@
Flame… Llama…
Lens Flare… Destello de lente…
Lighting Effects… Efectos de iluminación…
+ Relight… Reiluminar…
Picture Frame… Marco de imagen…
Tree… Árbol…
Sharpen Enfocar
diff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsv
index 08c22be83..c6e8610cc 100644
--- a/crates/ui-egui/src/i18n/fr.tsv
+++ b/crates/ui-egui/src/i18n/fr.tsv
@@ -515,6 +515,7 @@
Flame… Flamme…
Lens Flare… Reflet d'objectif…
Lighting Effects… Effets d'éclairage…
+ Relight… Rééclairage…
Picture Frame… Cadre d'image…
Tree… Arbre…
Sharpen Netteté
diff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsv
index 58ccd6281..154b72231 100644
--- a/crates/ui-egui/src/i18n/id.tsv
+++ b/crates/ui-egui/src/i18n/id.tsv
@@ -508,6 +508,7 @@
Flame… Api…
Lens Flare… Lens Flare…
Lighting Effects… Efek Pencahayaan…
+ Relight… Terangi ulang…
Picture Frame… Bingkai Foto…
Tree… Pohon…
Sharpen Pertajam
diff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsv
index 8dc1fa648..9bbfddce7 100644
--- a/crates/ui-egui/src/i18n/ja.tsv
+++ b/crates/ui-egui/src/i18n/ja.tsv
@@ -527,6 +527,7 @@
Flame… 炎…
Lens Flare… レンズフレア…
Lighting Effects… 照明効果…
+ Relight… 再照明…
Picture Frame… フレーム…
Tree… 木…
Sharpen シャープ
diff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsv
index 9a0df15d3..55d07e550 100644
--- a/crates/ui-egui/src/i18n/ko.tsv
+++ b/crates/ui-egui/src/i18n/ko.tsv
@@ -513,6 +513,7 @@
Flame… 불꽃…
Lens Flare… 렌즈 플레어…
Lighting Effects… 조명 효과…
+ Relight… 다시 조명…
Picture Frame… 사진 프레임…
Tree… 나무…
Sharpen 선명 효과
diff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsv
index 7d78deee9..249dc6963 100644
--- a/crates/ui-egui/src/i18n/pt-br.tsv
+++ b/crates/ui-egui/src/i18n/pt-br.tsv
@@ -512,6 +512,7 @@
Flame… Chama…
Lens Flare… Reflexo de lente…
Lighting Effects… Efeitos de iluminação…
+ Relight… Reiluminar…
Picture Frame… Moldura…
Tree… Árvore…
Sharpen Nitidez
diff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsv
index 4d882a6b5..738dfc125 100644
--- a/crates/ui-egui/src/i18n/ru.tsv
+++ b/crates/ui-egui/src/i18n/ru.tsv
@@ -1811,6 +1811,7 @@
Range Диапазон
Raw Defaults… Значения RAW по умолчанию…
Relink to File… Повторно связать с файлом…
+ Relight… Переосвещение…
Render Рендеринг
Replace All Missing Fonts Заменить все отсутствующие шрифты
Resolve Missing Fonts… Устранить отсутствующие шрифты…
diff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsv
index 135f914a0..35e1766f1 100644
--- a/crates/ui-egui/src/i18n/zh-hans.tsv
+++ b/crates/ui-egui/src/i18n/zh-hans.tsv
@@ -527,6 +527,7 @@
Flame… 火焰…
Lens Flare… 镜头光晕…
Lighting Effects… 光照效果…
+ Relight… 重新打光…
Picture Frame… 相框…
Tree… 树…
Sharpen 锐化
diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsv
index 3d6f5a080..86e44fffd 100644
--- a/crates/ui-egui/src/i18n/zh-hant.tsv
+++ b/crates/ui-egui/src/i18n/zh-hant.tsv
@@ -531,6 +531,7 @@
Flame… 火焰…
Lens Flare… 反光效果…
Lighting Effects… 光照效果…
+ Relight… 重新打光…
Picture Frame… 相框…
Tree… 樹…
Sharpen 銳利化
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index bd3ecd3e0..21b40394e 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -625,7 +625,11 @@ pub fn is_live(id: &str) -> bool {
}
/// Commands outside the catalogue that belong right after a catalogue item: `(id, after)`.
-const PLACE_AFTER: &[(&str, &str)] = &[("file.newFromClipboard", "file.new"), ("layer.removeBackground", "layer.layerMask.fromTransparency")];
+const PLACE_AFTER: &[(&str, &str)] = &[
+ ("file.newFromClipboard", "file.new"),
+ ("layer.removeBackground", "layer.layerMask.fromTransparency"),
+ ("filter.render.relight", "filter.render.lightingEffects"),
+];
pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
// 1) Photoshop's full menu tree, in Photoshop order; live where we implement the command.