Skip to content

Issue #3272: AI masks are enlarged along the photo's edges, no outline under a raised Exposure - #3298

Merged
aiongg merged 1 commit into
storytold:mainfrom
Peaperfish:ai-mask-edge-aware-upsample
Oct 31, 2026
Merged

aiongg merged 1 commit into
storytold:mainfrom
Peaperfish:ai-mask-edge-aware-upsample

Conversation

@Peaperfish

@Peaperfish Peaperfish commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3272

Problem

An Object / Describe (AI) mask is stored as the model's logit grid: 288² over the whole photo, so ~20 px cells on a 24 MP export. It was enlarged to the output by bilinear sampling alone, without looking at the photo, so its 0.5 contour could sit up to a cell away from the subject's real edge and the mask spilled 1–2 px onto the background. Raising the mask's Exposure brightened that strip into a thin outline around the whole subject, in the preview and in exports (the reporter's rider on light asphalt). Refine Edges alone doesn't fix it: on the test scene it still left 0.29 one pixel outside the edge.

Change

crates/pipeline/src/masks.rs, in sample_seg, the one path for these shapes. Preview, export, overlay and windowed renders all use it, and the GPU renderer evaluates these shapes with the same CPU shape_alpha, so CPU and GPU are both covered.

  • Image-guided enlargement: where the grid is coarser than the output, the sampled logits are guided-filtered by the photo's log luminance with the fast (subsampled) guided filter (He & Sun 2015). The window is 1.5 grid cells, measured in output pixels, so a preview and an export put the edge in the same place of the photo.
  • Logits, not coverage: filtering logits lets the confident side win, so the spill strip goes to ≈ 0. Filtering 0–1 coverage only averaged it down to ≈ 0.1.
  • Only edges change: pixels whose window is all inside or all outside, and edges the photo has no contrast across (the filter's own var / (var + eps) edge term), keep the plain sample exactly, bit for bit. So texture inside a subject can't punch holes, and featureless areas aren't blurred.
  • Refine Edges still runs afterwards on the combined mask and finds the edges in place: no second halo, and the edge moves less than 1 px.
  • One implementation: guided_cross (Refine Edges, Auto Mask) and the new guided_cross_fast share guided_coeffs; the subsampled form uses local::guided_fast_step and a box resize, like local::guided_fast. guided_cross's output is unchanged. local::guided_fast itself isn't touched (its maths feeds the frozen V2 tone base).
  • Docs: docs/ai-masks.md (Edge section), docs/process-versions.md (bug-fix note), the LR-MASK-OBJECTS row in docs/parity-checklist.md, and docs/gaps.md (gap 5 stays open).

Not in this PR:

  • Edges that differ only in colour at equal brightness are the luminance guide's weak spot. A colour guide would cost about 3×.
  • Prompt mask + Refine Edges in a zoomed window doesn't match the whole render. That's already true on main (max difference 22 levels, 37 with this change): Refine Edges gets no padding around a window, so its filter sees only the window's pixels. That's a general Refine Edges limitation, so that case is left out of the window test.
  • Embedded DNG mattes (matte_alpha, e.g. iPhone ProRAW) are still enlarged bilinearly.

How it was checked

  • Tests that fail without this change, on a dark disc over a light background with a 32² grid (crates/pipeline/src/masks/tests.rs):
    • ai_masks_follow_the_photos_edge_when_enlarged: 1 px outside the edge was 0.66, now < 0.05; > 0.95 one and two px inside, all around the disc; identical to the plain sample away from the edge.
    • refine_edges_on_an_enlarged_ai_mask_neither_halos_nor_tightens: Refine Edges 50 / 100 on top. No second halo, edge within 1 px of the unrefined one.
    • enlarged_ai_masks_put_the_edge_in_the_same_place_at_any_size: the edge lands at the disc's edge at 256, 512 and 1024 px.
    • crates/pipeline/src/tests_window.rs: a Prompt mask rendered in a window matches the whole render.
  • cargo xtask ci with CRAFT_FONTS_DIR set (macOS, stable 1.99.1): fmt passes; clippy stops only at main's own apps/lightcraft/src/native_menu.rs:492, fixed in MENU-BAR: merge the native menu's identical quit/about/settings branches (clippy::if_same_then_else) #3295. The remaining steps were run on their own and pass: heif (clippy and tests), the workspace tests (5075 passed, 0 failed), doctests, parity, layers, assets and wasm.
  • User-visible change: not checked in the app. These masks need a stored model grid, which the CLI and demo can't easily make, so the evidence is the synthetic tests.

Cost per AI selection (one shape_alpha, M2 Pro, before → after, same build):

Output Time Peak heap
24 MP export 25.7 → 48.2 ms 96.5 → 108.8 MB
60 MP export 64.1 → 109.9 ms 239.9 → 256.7 MB
2.7 MP preview 3.1 → 10.6 ms 11.2 → 26.5 MB

Only reduced-size planes are added, no full-size ones. It runs only for components that hold a model grid. The +7.5 ms on a preview counts against the slider budget; caching mask results (#1147) would mostly remove it.

Merging

Checklist

  • No new panic path in non-test code: no unwrap / expect / panic!, no unchecked indexing (sizes checked, get)
  • No new text on screen
  • The feature's row in docs/parity-checklist.md (LR-MASK-OBJECTS) and docs/gaps.md are updated in the same commit
  • New code has a caller
  • No assets added
  • Ready to merge

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enlarged AI subject masks now better follow contrasting photo edges, reducing background spill and bright outlines when exposure is raised.
    • Mask interiors, exteriors, and edges without photo contrast remain unchanged. Refine Edges continues to work on the combined mask.
  • Documentation
    • Updated the AI masks guide and process-version notes to explain the edge behavior and its performance impact.

…no outline under a raised Exposure

An Object / Describe mask is stored as a model's logit grid (288² over the whole photo,
~20 px cells on a 24 MP export) and was enlarged by bilinear sampling alone, so its edge
spilled 1–2 px onto the background; raising the mask's Exposure turned that strip into a
bright outline in previews and exports (CPU and GPU: the GPU renderer evaluates these
shapes with the same CPU `shape_alpha`).

`sample_seg` now guided-filters the sampled logits by the photo's log luminance where the
grid is coarser than the output: the fast (subsampled) guided filter, a window of 1.5 grid
cells, so a preview and an export put the edge in the same place. Filtering logits, not
coverage, lets the confident side win (the spill goes to ≈ 0, not to a mean of ≈ 0.1).
Pixels whose window is all inside or all outside, and edges the photo has no contrast
across, keep the plain sample exactly. Refine Edges still runs afterwards on the combined
mask and finds the edges in place: no second halo, no tightening.

One implementation: `guided_cross` (Refine Edges, Auto Mask) and the new
`guided_cross_fast` share `guided_coeffs`; the subsampled form uses
`local::guided_fast_step` and a box `resize`, like `local::guided_fast`. `guided_cross`'s
output is unchanged.

Existing AI masks become 1–2 px tighter at a subject's edges, on every process; their
inside, outside and contrast-less edges are unchanged. This lands in place under the
bug-fix rule of docs/process-versions.md (output that is in neither the photo nor the
edit), noted there.

Cost per AI selection (M2 Pro, 12 cores; one shape_alpha, before → after):
24 MP 25.7 → 48.2 ms, heap peak 96.5 → 108.8 MB; 60 MP 64.1 → 109.9 ms, 239.9 → 256.7 MB;
2.7 MP preview 3.1 → 10.6 ms, 11.2 → 26.5 MB.

Tests (a dark disc on a light background, its mask on a 32² grid): ≈ 0 one and two px
outside and ≈ 1 inside, all around (1 px outside was 0.66), bit-identical away from the
edge; with Refine Edges 50 / 100 (was 0.29 outside) and the edge within 1 px of the
unrefined one; the same edge position at 256, 512 and 1024 px; a Prompt mask's window
matches the whole render.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 77c0a6a and f14d166.

Files selected for processing: 7
  • crates/pipeline/src/masks.rs
  • crates/pipeline/src/masks/tests.rs
  • crates/pipeline/src/tests_window.rs
  • docs/ai-masks.md
  • docs/gaps.md
  • docs/parity-checklist.md
  • docs/process-versions.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Walkthrough

AI segmentation masks now use log-luminance-guided filtering on sampled logits when the segmentation grid is sufficiently coarse. Added tests check mask-edge alignment and consistency. Documentation records the behavior and reported processing costs.

Changes: AI mask edge refinement

Layer / File(s) Summary
Filter sampled segmentation logits
crates/pipeline/src/masks.rs
sample_seg now uses log luminance to guide filtering of sampled logits before converting them to coverage. The filtering path shares coefficient calculations with guided_cross.
Validate and document mask edges
crates/pipeline/src/masks/tests.rs, crates/pipeline/src/tests_window.rs, docs/ai-masks.md, docs/gaps.md, docs/parity-checklist.md, docs/process-versions.md
Tests check mask edges across output sizes, after Refine Edges, and in mask-overlay cases. Documentation describes the edge behavior, reported processing costs, and revision history.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • storytold/lightcraft#3270 — Its section on the prompt-mask outline describes the same edge-spill behavior addressed here; its other reported topics are outside this change.

Suggested reviewers: echelon, kovalskiguinther, emmertex

Merge Risk: Minimal · up to f14d1

No actionable issue remains from this review; the change is mergeable after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the issue number and the main behavior change: AI mask enlargement now follows photo edges, which removes the outline under raised Exposure. The changeset guide-filters sampled mask lo…
Linked Issues check Passed Issue #3272 requires reducing the outline caused by coarse AI-mask enlargement. sample_seg now guides sampled segmentation logits with photo log luminance when the grid is coarse. shape_alpha stil…
Out of Scope Changes check Passed The code changes, regression tests, window-render test fixture, and AI-mask documentation support the #3272 fix. The process-version, parity, and gap notes record the same behavior. The reviewed evide…
Docstring Coverage Passed Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentati…
Full details: Title check

Explanation

The title names the issue number and the main behavior change: AI mask enlargement now follows photo edges, which removes the outline under raised Exposure. The changeset guide-filters sampled mask logits with the photo's log luminance in sample_seg, so the title matches the primary change. The title is long (94 characters, 16 words) and includes an issue reference, but it is specific and related to the code and docs changes.

Full details: Linked Issues check

Explanation

Issue #3272 requires reducing the outline caused by coarse AI-mask enlargement. sample_seg now guides sampled segmentation logits with photo log luminance when the grid is coarse. shape_alpha still applies Refine Edges after mask composition. Tests cover the outside spill, inside coverage, unchanged flat regions, Refine Edges, and consistent preview/export edge placement. The documented changes also identify #3272.

Full details: Out of Scope Changes check

Explanation

The code changes, regression tests, window-render test fixture, and AI-mask documentation support the #3272 fix. The process-version, parity, and gap notes record the same behavior. The reviewed evidence shows no unrelated implementation, dependency, build, or asset changes.

Full details: Docstring Coverage

Explanation

Docstring coverage is 90.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 10 functions across 3 files. (4 skipped: 4 unsupported.)

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@aiongg
aiongg merged commit 9e732c2 into storytold:main Oct 31, 2026
6 checks passed
clydecbrown referenced this pull request in emmertex/chromaforge Nov 7, 2026
Adapted from upstream #3298: an Object / Describe mask is stored as a model's
logit grid (288² over the whole photo, ~20 px cells on a 24 MP export) and was
enlarged by bilinear sampling alone, so its edge spilled 1-2 px onto the
background; a raised Exposure turned that strip into a bright outline, in
previews and exports (the GPU renderer evaluates these shapes with the same
CPU shape_alpha).

sample_seg now guided-filters the sampled logits by the photo's log luminance
where the grid is coarser than the output (the fast guided filter, a window of
1.5 grid cells). guided_cross and the new guided_cross_fast share
guided_coeffs; guided_cross's output is unchanged. Chromaforge's People and
Concept (remembered object) grids keep the plain enlargement: sample_seg takes
the guide as an Option, and they pass None until the filter is checked on them.

Lands in place as a bug fix (docs/process-versions.md); RENDER_CACHE_VERSION is
already bumped on this branch.

Measured here (release build, best of 5, load ~8; one sample_seg on a 288²
grid, plain -> guided): 24 MP 13.2 -> 36.6 ms, 60 MP 32.0 -> 85.9 ms,
2.7 MP 1.9 -> 10.8 ms. On the tests' disc scene (32² grid, 512 px): 1 px
outside the edge 0.658 -> 0.000, 2 px outside 0.430 -> 0.000, 1 px inside
0.831 -> 1.000.

Adapted from upstream commit f14d166, with the doc-comment fix of
737c686; the tests move to masks.rs's inline tests.

Sources: upstream #3298, commits f14d166 and 737c686; He, Sun and Tang,
"Guided Image Filtering"; He and Sun, "Fast Guided Filter" (2015).

Co-authored-by: Brain Wang <wm20062006@gmail.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AI subject mask leaves a bright outline around the subject when it raises exposure

2 participants