diff --git a/brief-11361.md b/brief-11361.md deleted file mode 100644 index 7b4eb817e6..0000000000 --- a/brief-11361.md +++ /dev/null @@ -1,39 +0,0 @@ -# Brief — PhotoCraft #11361: Outer Glow with a contour starting above 0 fills its whole rectangle - -You are drafting work in a git worktree of storytold/photocraft (branch fix/outer-glow-contour) for issue #11361. Work only in this worktree. Do NOT commit, push, or post anything — your diff stays uncommitted for maintainer review. Do NOT run cargo: your shell's cargo calls block on permission approval in this setup, so leave all building/testing to the reviewer and say plainly in your summary that no gates were run. Read the repo's CLAUDE.md first and follow its conventions. - -## The bug - -An Outer Glow whose contour is above 0 at its left end — e.g. a flat contour (0,255)–(255,255), "always 100 %" — is painted over the whole rectangle the effect is computed in (layer bounds + glow reach), with square corners. Photoshop paints only where the glow reaches: a solid band *Size* px wide following the shape, with a soft outer edge. Both techniques (Precise and Softer), on PSD import and natively (`layer.layerStyle.outerGlow` with `"contour": {"points": [[0,1],[1,1]]}`). - -## The reporter's diagnosis (verify against the code, then build on it) - -- `glow_map` in `crates/compose/src/effects/paint.rs` maps the whole coverage buffer through the contour (`m = apply_lut(m, glow_lut(g))`), including pixels the glow does not reach (coverage 0). With `contour(0) > 0` those pixels light up too, out to the buffer's edge. -- The GPU path uses the same table (`fx::glow_lut` in `crates/gpu/src/fx/builders.rs`), so it does the same. A complete fix covers both paths, or fixes the shared table construction so both inherit it. -- The reporter attached a work-in-progress patch and explicitly offers it as a starting point, not a fix: https://github.com/user-attachments/files/34404901/glow-contour-fix.patch (curl it; it applies cleanly to main). What it does: a new `glow_paint_lut(g, inner)` sets the table's first entry — the one coverage-0 pixels consult — so unreached pixels stay dark. That removes the rectangle, but the band's outer edge still doesn't match Photoshop. Start by applying it in the worktree, understanding exactly what it changes, and treat the remaining edge mismatch as the real problem to solve. Credit the reporter in your summary. - -## Oracles (from the issue; use them as your acceptance tests) - -- One row through the left edge of the 4 px flat Precise ellipse (red channel; background 176, glow 88, shape 255): Photoshop gives `176, 102, 88, 88, 88, 255` — a one-pixel soft step at the band's outer edge, then solid glow up to the shape. Current PhotoCraft gives `88` all the way from the rectangle's edge to the shape. -- Glow-pixel counts (issue's table): flat Precise and flat Softer are orders of magnitude over Photoshop's; the Linear-contour Precise control is close (138 vs 0 stray). Your fix must not move the Linear/default-contour renders — they are the regression guard. -- Structural invariant: any pixel farther than the glow's reach (Size, adjusted by Range as Photoshop defines it) from the shape must end with glow alpha 0, for ANY contour, including one that is 100 % everywhere. -- The issue's attached corpus (glow-contour.psd, 600×300, 7 layers: Sizes 4 and 10 × {flat Precise, flat Softer, Linear Precise}) and the Photoshop 2021 reference renders are offered as a corpus oracle. The native command-path repro is enough for unit tests; use the PSD if the test harness has a natural place for it. - -## Where to look - -- `crates/compose/src/effects/paint.rs` — `glow_map`, and how the coverage buffer is produced for Precise vs Softer (distance-based falloff). Understand what "coverage" means at each pixel before touching the LUT: the contour in Photoshop remaps the glow's falloff *within* its reach; the question your fix must answer is how the falloff, the contour, and the reach compose — including the related closed issue #7899 (Softer's falloff has a tail beyond Size, which is exactly what the flat contour lights up; your fix may need to address the Softer extent, or at least define behaviour there deliberately). -- `crates/gpu/src/fx/builders.rs` — `fx::glow_lut`; `crates/gpu/tests/it/parity.rs` for how CPU/GPU parity is asserted. -- Existing outer-glow tests in `crates/compose/src/tests.rs` / the effects tests — model yours on them. - -## Tests to add - -- Flat contour (points [[0,1],[1,1]]), Precise and Softer: glow appears only within the reach band; a pixel row across the band edge matches the Photoshop profile shape (solid inside, one soft step, background outside) within the tolerance the existing parity tests use. -- A contour with contour(0) > 0 but not flat (e.g. [[0,0.5],[1,1]]): unreached pixels stay dark. -- Default/Linear contour unchanged: assert an existing glow render's pixels (or an existing test's expectations) are untouched. - -## Summary format (your final message) - -- What the WIP patch did, what was still wrong, and what your change does differently — in terms of how falloff, contour, and reach now compose, for both Precise and Softer and both CPU/GPU paths. -- The pixel evidence you would expect the reviewer to see (the row profile and the stray-pixel counts before/after), being explicit that you could not run anything. -- Any Photoshop-semantics judgement calls you had to make (e.g. how Range interacts with the cutoff), flagged as such. -- State plainly that no gates were run (cargo is blocked in your shell) and the reviewer must run: cargo test -p photocraft-compose, the gpu parity tests, the engine suites per CLAUDE.md, clippy, fmt. diff --git a/crates/compose/src/effects/paint.rs b/crates/compose/src/effects/paint.rs index 8c7cb2865b..510b556509 100644 --- a/crates/compose/src/effects/paint.rs +++ b/crates/compose/src/effects/paint.rs @@ -44,14 +44,28 @@ pub fn glow_lut(g: &Glow) -> Option> { /// The contour remaps the falloff only within the glow's reach. Run through `glow_lut` alone, /// a contour that starts above 0 (Photoshop's flat "always 100 %" one, `(0,1)–(1,1)`) also /// lights every pixel the glow never reaches (coverage 0), out to the edge of the effect's -/// rectangle. Here coverage `v` is also turned back into the pixel's distance inside the reach -/// (`glow_reach_px`, 0 at and beyond it), and the transfer is -/// `min(contour(u), max(clamp(reach_px, 0, 1), u))`, with `u = min(v / range, 1)` what a Linear -/// contour paints. So: +/// rectangle. Here coverage `v` is also turned into how much of the pixel the reach covers +/// (`glow_reach_edge`, 0 at and beyond the reach, 1 from a pixel inside it), and the transfer is +/// `min(contour(u), max(edge, u))`, with `u = min(v / range, 1)` what a Linear contour paints. +/// The edge is linear in `v` from 0 until it reaches 1 (`glow_edge_per_coverage`), so the +/// table's linear interpolation follows it, its first step from entry 0 included (Softer's knee +/// also lands on a table entry; Precise's falls between two, which chord it); the coverage +/// where it reaches 1 is per technique: +/// +/// - Precise: the falloff is linear in distance, so the edge is the distance left to the reach's +/// end: the last pixel steps down by how far inside the reach its centre is (one soft pixel, +/// as Photoshop's flat-contour band). +/// - Softer: coverage is a tent blur, and the same coverage means a different distance for a +/// straight edge and a single pixel, so no coverage-only model gives the distance. The edge +/// reaches 1 at the coverage one opaque pixel gives one pixel inside its reach along a row +/// (`softer_full_coverage`): every pixel a row or column inside any opaque feature's reach +/// paints at full strength, and the soft step is the reach's outermost pixel (or, near a +/// feature's diagonal corners, where the tent's coverage is lower still). +/// +/// So: /// /// - coverage 0 always paints 0, for any contour (nothing beyond the reach); -/// - a high contour is solid up to the last pixel of the reach, which steps down by how far -/// inside the reach its centre is (one soft pixel, as Photoshop's flat-contour band); +/// - a high contour is solid up to the last pixel of the reach, which steps down; /// - it never paints less than the Linear contour would, so a contour that already follows or /// stays under the falloff (Linear, or a custom curve with `contour(0) = 0` rising no faster /// than the reach edge) is unchanged. The reach does not depend on Range: Range stretches the @@ -66,45 +80,71 @@ pub fn glow_paint_lut(g: &Glow, inner: bool) -> Option> { } let lut = lut?; let range = if g.range.is_finite() { g.range.clamp(0.01, 1.0) } else { 1.0 }; + let per_coverage = glow_edge_per_coverage(g); // The GPU samples a 4096-entry table (`compose.wgsl` `lut`), as `ranged_lut` builds. - let n = 4096; Some( - (0..n) + (0..GLOW_LUT_LEN) .map(|k| { - let v = k as f32 / (n - 1) as f32; + let v = k as f32 / (GLOW_LUT_LEN - 1) as f32; let c = lut_at(&lut, v); let linear = (v / range).min(1.0); - let edge = glow_reach_px(g, v); - let edge = if edge.is_finite() { edge.clamp(0.0, 1.0) } else { 1.0 }; - c.min(edge.max(linear)) + c.min(glow_reach_edge(per_coverage, v).max(linear)) }) .collect(), ) } -/// How far (in pixels, centre to the reach's end) a pixel of glow coverage `v` lies inside the -/// glow's reach, from the falloff `glow_map` builds. Only its value up to 1 matters. +/// Entries in a glow's transfer table: the GPU's `compose.wgsl` `lut` reads 4096, linearly +/// interpolated, as the CPU's [`lut_at`] does. +const GLOW_LUT_LEN: usize = 4096; + +/// How much of a pixel of glow coverage `v` the glow's reach covers, 0..=1: 0 at and beyond the +/// reach (coverage 0, whatever `per_coverage` is), else `per_coverage · v` up to 1. +fn glow_reach_edge(per_coverage: f32, v: f32) -> f32 { + let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) }; + if v == 0.0 { + return 0.0; + } + let edge = per_coverage * v; + if edge.is_finite() { edge.clamp(0.0, 1.0) } else { 1.0 } +} + +/// The slope of [`glow_reach_edge`]: `1 / (the coverage at which the edge reaches 1)`, that +/// coverage rounded down to a whole table step (at least one), so the knee lands on a table entry +/// and the table interpolates the edge exactly (rounding down only widens what paints in full). /// /// - Precise: `v = ½·(1 − (d − solid) / soft)`, which reaches 0 at `d = size + 1` (centre to -/// centre), so the distance left is `size + 1 − d = 2·soft·v`. -/// - Softer: a two-box tent of width `w` (support ±`w`) falls off at a straight edge as -/// `v = e² / (2w²)` within `e` px of its end, so `e = w·√(2v)`. Curved edges and the discrete -/// taps make this approximate; it only shapes the last pixel. -fn glow_reach_px(g: &Glow, v: f32) -> f32 { - let v = v.max(0.0); - match g.technique { +/// centre), so the distance left to the reach's end is `size + 1 − d = 2·soft·v`: full at +/// `v = 1 / (2·soft)`. +/// - Softer: full at [`softer_full_coverage`]. +fn glow_edge_per_coverage(g: &Glow) -> f32 { + let full = match g.technique { GlowTechnique::Precise => { let solid = g.size * g.spread; - let soft = (g.size + 1.0 - solid).max(1e-3); - 2.0 * soft * v - } - GlowTechnique::Softer => { - let (_, bw) = spread_split(g.size, g.spread); - // As `blur::box_width`: the blur never runs narrower than 1 px or past MAX_REACH. - let w = if bw.is_nan() { 1.0 } else { bw.clamp(1.0, super::MAX_REACH) }; - w * (2.0 * v).sqrt() + 0.5 / (g.size + 1.0 - solid).max(1e-3) } + GlowTechnique::Softer => softer_full_coverage(spread_split(g.size, g.spread).1), + }; + let steps = (GLOW_LUT_LEN - 1) as f32; + // `max` also turns a NaN (never expected) into the one-step minimum. + steps / (full * steps).floor().max(1.0) +} + +/// The coverage the Softer blur of width `bw` (the tent `blur` runs, of radius `R` taps) gives +/// one opaque pixel one pixel inside its reach along a row: `t[R − 1]·t[0]`. Every pixel within +/// `R − 1` of an opaque pixel along a row or column has at least this coverage (the kernel is +/// non-negative and `t` falls monotonically from its centre), so a wider feature only adds to +/// it. A straight edge's outermost pixel has `t[R]`, at or above this for whole widths of 3 and +/// up (it is soft only at width 2 and at some fractional widths). 0 when the blur is a single +/// tap (no falloff). +fn softer_full_coverage(bw: f32) -> f32 { + let (r, t) = super::blur::tent_kernel(bw); + let at = |i: i32| usize::try_from(i).ok().and_then(|i| t.get(i)).copied().unwrap_or(0.0); + if r < 1 { + return 0.0; } + // The kernel is stored from `-R` to `R`: index `R` is its centre, `2R - 1` is tap `R - 1`. + at(r) * at(2 * r - 1) } /// A contour applied over a Range (`contour(min(v / range, 1))`); `None` = identity. diff --git a/crates/compose/src/effects/tests.rs b/crates/compose/src/effects/tests.rs index 5bb2027c48..b2e938ac54 100644 --- a/crates/compose/src/effects/tests.rs +++ b/crates/compose/src/effects/tests.rs @@ -952,15 +952,83 @@ fn flat_contour_outer_glow_is_a_solid_band_with_one_soft_pixel() { } assert!((at(&m, 15) - 0.5).abs() < 0.05, "precise, the soft outer pixel: {}", at(&m, 15)); assert!(at(&m, 14) == 0.0 && at(&m, 0) == 0.0, "precise, beyond the reach: {} {}", at(&m, 14), at(&m, 0)); - // Softer, size 4: the tent reaches the 3.5 px pixel (x 16) with coverage 1/64, which a Linear - // contour paints at 1/32 and the flat one at the reach's √(2v)·4 ≈ 0.71. + // Precise, pixel-aligned edge: the pixels out from it are 1..5 px (centre to centre) from the + // shape and the reach ends at the 5 px one's centre, so the band is solid through Size (x 16, + // 4 px out, 1.0) and the next pixel (x 15) is beyond it. Point-sampled, as the measured Linear + // falloff is: an area-averaged falloff would leave x 15 half covered (the reach ends 4.5 px + // past the shape's edge, half-way across it), not 0.84. let shape = square_20_40(); + let m = glow_map(&shape, &contour_glow(GlowTechnique::Precise, 4.0, &FLAT), false, (0, 0)); + for x in 16..20 { + assert!(at(&m, x) > 0.999, "precise aligned, solid band at x {x}: {}", at(&m, x)); + } + assert!(at(&m, 15) == 0.0, "precise aligned, beyond the reach: {}", at(&m, 15)); + // Softer, size 4: the tent's last tap (1/64, at x 16) is above what one opaque pixel gives one + // pixel inside its reach (t[3]·t[0] ≈ 0.0137), so a straight edge is solid through its reach. let m = glow_map(&shape, &contour_glow(GlowTechnique::Softer, 4.0, &FLAT), false, (0, 0)); - for x in 17..20 { + for x in 16..20 { assert!(at(&m, x) > 0.999, "softer, solid band at x {x}: {}", at(&m, x)); } - assert!(at(&m, 16) > 0.5 && at(&m, 16) < 0.95, "softer, the soft outer pixel: {}", at(&m, 16)); assert!(at(&m, 15) == 0.0 && at(&m, 0) == 0.0, "softer, beyond the reach: {} {}", at(&m, 15), at(&m, 0)); + // Softer, size 2: the last tap (1/16, at x 18) is below that (0.25·0.375), so the outermost + // pixel is soft: 383 table steps to full, 0.0625 · 4095 / 383 ≈ 0.668. + let m = glow_map(&shape, &contour_glow(GlowTechnique::Softer, 2.0, &FLAT), false, (0, 0)); + assert!(at(&m, 19) > 0.999, "softer size 2, solid band: {}", at(&m, 19)); + assert!((at(&m, 18) - 0.668).abs() < 0.01, "softer size 2, the soft outer pixel: {}", at(&m, 18)); + assert!(at(&m, 17) == 0.0, "softer size 2, beyond the reach: {}", at(&m, 17)); +} + +/// #11361 review: a feature narrow in both axes has far less Softer coverage at a given distance +/// than a straight edge. Read through the straight edge's tail (`e = w·√(2v)`), a single pixel's +/// flat-contour glow was dimmed 2 px inside its reach at size 4 (0.94, 0.66, then 0.33 at the +/// last pixel) and from about 5 px inside at size 10. The edge now reaches full strength at the +/// coverage a single opaque pixel gives one pixel inside its reach, so along its row and column +/// only the outermost pixel is soft. +#[test] +fn softer_flat_contour_keeps_a_single_pixel_solid_inside_its_reach() { + let n = 60; + let mut shape = Map::new(n, n, 0.0); + shape.v[30 * n + 30] = 1.0; + // (size, reach R in pixels, the outermost pixel's value t[R]·t[0] / knee). + // Size 4: t = [7/32, 3/16, 1/8, 1/16, 1/64], knee floor(4095·t[3]·t[0]) = 55 steps. + // Size 10: box [.05, .1 ×9, .05], t[0] = .095, t[9] = .01, t[10] = .0025, knee 3 steps. + for (size, reach, last) in [(4.0f32, 4usize, 0.2545f32), (10.0, 10, 0.3243)] { + let m = glow_map(&shape, &contour_glow(GlowTechnique::Softer, size, &FLAT), false, (0, 0)); + for d in 0..reach { + for (x, y) in [(30 + d, 30), (30 - d, 30), (30, 30 + d), (30, 30 - d)] { + let v = m.v[y * n + x]; + assert!(v > 0.999, "size {size}: ({x}, {y}), {d} px out, paints {v}"); + } + } + let v = m.v[30 * n + 30 + reach]; + assert!((v - last).abs() < 0.01, "size {size}: the outermost pixel paints {v}, not {last}"); + assert_eq!(m.v[30 * n + 31 + reach], 0.0, "size {size}: beyond the reach"); + } +} + +/// #11361 review: the table is linear from entry 0 up to a knee on a table entry, so the GPU's and +/// CPU's linear interpolation paint the model's value at any coverage, the first step +/// `(0, 1/4095]` included (a large Softer glow's outermost pixel has coverage there). +#[test] +fn flat_contour_glow_table_is_linear_up_to_a_knee_on_an_entry() { + for t in [GlowTechnique::Precise, GlowTechnique::Softer] { + for size in [1.0f32, 2.0, 4.0, 7.5, 10.0, 60.0, 512.0] { + let Some(f) = glow_paint_lut(&contour_glow(t, size, &FLAT), false) else { panic!("a flat contour has a table") }; + assert_eq!(f[0], 0.0); + let steps = 1.0 / f[1]; + assert!(steps >= 1.0 && (steps - steps.round()).abs() < 1e-3, "{t:?} size {size}: the knee at {steps} steps"); + for (k, v) in f.iter().enumerate() { + let want = (k as f32 / steps.round()).min(1.0); + assert!((v - want).abs() < 1e-5, "{t:?} size {size}: entry {k} is {v}, not {want}"); + } + // Between entries (here inside the first step) the interpolation is the model. + for frac in [0.1f32, 0.5, 0.9] { + let v = frac / 4095.0; + let want = (frac / steps.round()).min(1.0); + assert!((lut_at(&f, v) - want).abs() < 1e-5, "{t:?} size {size}: coverage {v}"); + } + } + } } /// #11361's regression guard: the reach cap never paints below what the Linear contour does, so