Skip to content

fix: composite a layer's effects into the layer, not onto the backdrop - #884

Merged
kyamagu merged 9 commits into
mainfrom
fix/effects-composite-into-layer
Sep 25, 2026
Merged

kyamagu merged 9 commits into
mainfrom
fix/effects-composite-into-layer

Conversation

@kyamagu

@kyamagu kyamagu commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #846.

Compositor._apply_effects() ran after the layer was already in the backdrop,
so a stroke or an overlay blended against the finished composite rather than
against the layer it belongs to. Where a layer's coverage is partial that
leaves a backdrop term in every pixel of the effect: an outset stroke came out
at t*S + (1 - t)*(alpha*L + (1 - alpha)*B) where Photoshop renders
alpha*L + (1 - alpha)*S.

What the fix does

An effect lands in one of two places relative to the layer's own region, and
the two are composited differently.

Inside it — an overlay, or the part of a stroke within the boundary —
paints over the layer, knocks out what it covers, and never widens the region.
_EffectCanvas composites those into the layer's own source, in coverages
that are a share of the region rather than of the pixel, and hands back one
source for the layer and its inner effects together.

Outside it — the part of a stroke past the boundary — is disjoint from
the layer rather than over or under it: a band drawn from the layer's own
coverage is complementary to it, so on a pixel the boundary cuts an outset
band comes out at 1 - alpha and the two together fill the pixel. Their
coverages therefore add, which is not what compositing one over the other
gives. The band goes on before the layer, scaled by what the layer will
leave of the pixel — t / (1 - covered) — after which the union the layer
composites with resolves to covered + t exactly. Going on separately is also
what lets it keep its own blend mode against the backdrop, which one merged
source composited with the layer's blend mode could not.

draw_stroke_effect_split() divides the band at the boundary as a share of
what draw_stroke_effect() draws, so the two halves always sum to it. Drawing
them separately would not: _distance_band() multiplies the two edges' ramps,
which is the area of a pixel cut by a straight edge only while the band is a
pixel wide or more, and halving a band halves both halves. The part of an
inner band that falls where the layer has no paint — reachable when a stroke
traces the mask rather than the layer — paints beside the layer rather than
being dropped.

Two things the old ordering got for free and this has to do on purpose:

  • Fill opacity fades the layer's paint but not its effects. The canvas
    folds it into the layer's own paint and carries the coverage a knockout
    punches with in _Source.knockout_shape, because fill opacity 0 under a
    knockout is a hole, not a no-op.
  • Layer opacity fades the layer and its style alike, so it applies once,
    to the two together. It was going in twice — once in the source's alpha and
    once per effect — which lifted a half-opaque layer with a full-strength
    overlay to 0.75. That is pre-existing, and fixed here because the new class
    is where it would otherwise be written into the design.

Measured

Every fixture in tests/psd_files, both force modes, against each document's
own Photoshop preview. 9 documents move nearer, 3 further, the rest are bit
for bit unchanged.

fixture before after
effects/feathered-stroke.psd 1.58e-3 9.91e-5
effects/antialiased-stroke-edge.psd 5.95e-4 1.03e-4
effects/shape-fx2.psd 6.56e-4 8.82e-5 (alpha 6.97e-4 → 9.10e-8)
effect-stroke-gradient.psd 3.88e-2 3.62e-2
effects/stroke-composite.psd 3.77e-3 3.54e-3 (alpha 1.56e-4 → 0)
effects/stroke-effects.psd 9.34e-2 9.09e-2
layer_comps.psb 6.81e-3 6.71e-3
layer_effects.psd 6.67e-3 6.60e-3

Per layer is where the size of it shows. stroke-effects.psd's four
soft-edged pixel layers are 90–92% partial alpha, so the B term is on
almost every pixel of them, and each carries a stroke of a different position:

layer before after
Raster InsetFrame 3.99e-2 4.5e-13
Raster Rectangle 9.44e-3 1.17e-5
Raster Ellipse 6.38e-3 1.33e-5
Raster OutsetFrame 4.77e-3 1.16e-5

That document's remaining 9.09e-2 is its shape layers, which are wrong for
an unrelated reason: a shape layer's stroke is traced from its rasterized
fill, so where the fill fades to transparent the trace fades with it and the
stroke lands across an interior Photoshop leaves alone — Photoshop strokes the
path. force=True redraws the fill from the vector mask and scores 3.60e-2.
It is also why that document's alpha term moves from 1.05e-1 to 1.19e-1 while
its colour term moves from 1.33e-1 to 1.23e-1: added coverage is only sound
while the traced boundary is the layer's own coverage.

The three that move further

Each is a pre-existing coverage error that the correct order stops cancelling,
attributed pixel by pixel:

  • clipping-mask2.psd (1.10e-4 → 1.27e-4). Polygon 1's stored alpha
    channel and its layer mask are the same array, pixel for pixel, and the
    compositor multiplies one by the other, so its edge coverage comes out
    squared: 0.694 becomes 0.482. Solving Photoshop's own pixel for the layer
    alpha and the overlay's weight gives 0.696 and 0.486 against the 0.482 and
    0.480 psd-tools uses — the overlay's weight is right and the coverage is
    not.
  • effects/shape-fx2.psd under force=True (1.56e-2 → 2.66e-2, while
    force=False improves 7x). aggdraw rasterizes the polygon's edge at 0.33
    where Photoshop has 0.70. An overlay applied as a separate source used to
    union a second 0.33 on top of the first, which inflated the alpha toward the
    truth by accident; composited into the layer it cannot, because an effect
    inside the layer cannot make the layer cover more than its own region. Most
    of the MSE is colour at zero alpha, which is arbitrary.
  • layer_params.psb (1.185e-2 → 1.191e-2, +0.5%). Antialiased text,
    which psd-tools does not render; more of its pixels move nearer the preview
    (745) than further (600).

What this does not settle

An inner effect is composited with the layer's own blend mode, which is what
Tag.BLEND_INTERIOR_ELEMENTS ("Blend Interior Effects as Group") asks for and
Photoshop leaves off by default. No fixture pairs a non-normal layer blend
mode with an effect that draws, so the corpus cannot say which way is right;
it is recorded on the existing TODO for that tag rather than guessed at.

Under a knockout an outer band goes on before the hole is punched, so a
partial-coverage pixel of the layer attenuates the band beside it. Drop
shadow, inner shadow, the two glows, satin and bevel are still not drawn, so
where they would land is not settled here either.

Cost

layer_effects.psd, the heaviest effect document in the corpus: peak
allocation 70.2 MB → 68.5 MB, composite 256 ms → 281 ms.

Tests

  • test_a_stroke_is_composited_into_the_layer_not_onto_the_backdrop states
    Stroke effects composite onto the backdrop instead of into the layer #846's table as the arithmetic it is a table of, and checks both renders
    against the closed form — Photoshop's preview and psd-tools' — on all three
    stroke positions at two sizes. The centered rows are a control: there t is
    1 and the old order's B term is multiplied away, so they passed before.
  • test_a_soft_edged_pixel_layer_renders_its_stroke_to_the_preview pins the
    four per-layer numbers above.
  • test_the_split_band_accounts_for_the_whole_stroke pins that the halves are
    the band divided, never the band changed, per pixel and not only in total.
  • Three forged combinations the corpus does not have: a Normal stroke on a
    Multiply layer keeps its own colour, a knockout punches with the layer's own
    coverage, and an overlay leaves a half-opaque layer at its opacity.
  • Thresholds tightened: feathered-stroke.psd 5e-3 → 2e-4,
    antialiased-stroke-edge.psd 1e-3 → 2e-4, shape-fx2.psd 1e-3 → 2e-4.

Every new and tightened assertion fails against the pre-fix compositor (14 of
them), checked by restoring the two source files and re-running.

🤖 Generated with Claude Code

kyamagu and others added 5 commits September 25, 2026 17:52
Compositor._apply_effects() ran after the layer was already in the
backdrop, so a stroke or overlay blended against the finished composite
rather than against the layer it belongs to. Where the layer's coverage
is partial that leaves a backdrop term in every pixel of the effect:
an outset stroke came out at t*S + (1 - t)*(alpha*L + (1 - alpha)*B)
where Photoshop renders alpha*L + (1 - alpha)*S.

Effects now composite into the layer's own source, which _EffectCanvas
holds, and the layer and its effects reach the canvas as one source.
An effect lands in one of two places relative to the layer's region:

- over(), inside it: an overlay, or the part of a stroke within the
  boundary. It paints over the layer and knocks out what it covers.
- beside(), outside it: the part of a stroke past the boundary. Its
  coverage adds to the layer's rather than being unioned with it,
  because a band drawn from the layer's own coverage is complementary
  to it -- on a pixel the boundary cuts, an outset band comes out at
  1 - alpha and the two together fill the pixel.

draw_stroke_effect_split() divides the band at the boundary as a share
of what draw_stroke_effect() draws, so the halves always sum to it, and
the part of an inner band that falls where the layer has no paint --
reachable when a stroke traces the mask rather than the layer -- paints
beside the layer rather than being dropped.

Fill opacity fades the layer's paint and not its effects, which the old
order got for free; the canvas folds it into the layer's own paint and
carries the coverage a knockout punches with in _Source.knockout_shape.

Measured over the fixture corpus in both force modes, 9 documents move
nearer Photoshop's preview and 3 further. feathered-stroke.psd goes
1.6e-3 -> 9.9e-5, antialiased-stroke-edge.psd 6.0e-4 -> 1.0e-4, and
shape-fx2.psd 6.6e-4 -> 8.8e-5 with its alpha at 9.1e-8. Per layer, the
four soft-edged pixel layers of stroke-effects.psd go 4.8e-3..4.0e-2 to
1.3e-5 and better, one of them bit for bit. The three that move further
are each a pre-existing coverage error the correct order stops
cancelling: clipping-mask2.psd applies a shape layer's vector mask twice,
shape-fx2.psd under force=True rasterizes its polygon 0.33 where
Photoshop has 0.70, and layer_params.psb is text psd-tools cannot set.

Co-Authored-By: Claude Opus 5 <[email protected]>
_EffectCanvas is held for the whole of a layer's effect stack rather
than freed between effects the way a source is, so what it allocates up
front it keeps. The three outer accumulators and the shape start as
scalars and widen only when something paints on them, which is the same
allocation-avoidance path _get_mask() takes, and the un-premultiply an
inner effect blends against is skipped for the normal blend function,
which ignores it -- and which is also what an unknown blend mode
answers.

layer_effects.psd's peak goes 96.5 MB to 80.1 MB against 70.2 MB before
the fix, and its composite 288 ms to 271 ms against 256 ms. Every
fixture in the corpus renders bit for bit as it did.

Co-Authored-By: Claude Opus 5 <[email protected]>
Two notes on the new arithmetic, both about what it does not decide.

The composed source carries one colour per pixel and is composited with
the layer's own blend mode, so an inner effect now goes through that
mode -- which is what Tag.BLEND_INTERIOR_ELEMENTS asks for and Photoshop
leaves off by default. No fixture in the corpus pairs a non-normal layer
blend mode with an effect that draws, so the sweep cannot say which way
is right; the existing TODO on that tag is where it is recorded.

draw_stroke_effect_split() can hand back the same array twice, an inset
band being the whole of the coverage, which is a contract on its callers
rather than something they can see.

Co-Authored-By: Claude Opus 5 <[email protected]>
Review of the merged-source design found three things the corpus cannot
reach, because no fixture anywhere in it pairs a drawn effect with a
layer opacity below 255, a knockout, a non-normal layer blend mode or a
pass-through group.

Merging the outer half of a stroke into the layer's source handed it the
*layer's* blend mode: a Normal stroke on a Multiply layer came out
multiplied against the backdrop it sits beside, red 0.863 to 0.000. It
also put the band into the coverage a knockout punches with, so a
half-opaque band erased the backdrop under itself over ground the layer
never touches -- alpha 1.0 to 0.5 across an opaque Background.

An outer band is disjoint from the layer rather than merged with it, so
it goes on first, scaled by what the layer will leave of the pixel:
``t / (1 - covered)``, after which the union the layer composites with
resolves to ``covered + t`` exactly. That is the same arithmetic the
merge gave -- every fixture in the corpus scores the same to four
figures -- while the band keeps its own blend mode against the backdrop
and the knockout punches with the layer's own coverage again. A
pass-through group keeps the old order, since the interpolation that
re-applies it would interpolate away anything painted beside it first.

The layer opacity was also going in twice, once in the source's alpha
and once per effect, which is pre-existing but was written into the new
class: an overlay at full strength lifted a half-opaque layer to 0.75.
It now applies once, to the layer and its effects together, which is
what it means.

_EffectCanvas is the inner half alone and allocates less than before:
layer_effects.psd's peak is 68.5 MB against 70.2 MB on main.

Co-Authored-By: Claude Opus 5 <[email protected]>
No fixture pairs an effect that draws with a knockout, a non-normal
layer blend mode or a layer opacity below 255, which is why merging the
outer half of a stroke into the layer's source broke all three without
moving a single corpus render. Each is made here from
feathered-stroke.psd's outset square, whose band covers its whole ramp
so that where the stroke went reads off one pixel:

- a Normal stroke on a Multiply layer keeps its own colour against the
  backdrop, rather than being multiplied by it;
- a knockout with fill opacity 0 punches with the layer's own coverage,
  leaving the opaque Background beside it alone;
- an overlay at full strength leaves a half-opaque layer at its opacity.

All three fail against the merged-source commit and the first two pass
against main, which is what they are for: they guard the shape of the
fix rather than the fix.

Co-Authored-By: Claude Opus 5 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 25, 2026 09:42
@kyamagu kyamagu added this to the 1.21.0 milestone Sep 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Outstanding compositor issues affect outer-stroke opacity, blend interactions, and pass-through groups.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity

Open (1)
What changed in this PR

Updates compositing so supported overlays and strokes are combined with their layer before backdrop composition.

Changes:

  • Adds _EffectCanvas and separates inner/outer stroke handling.
  • Splits stroke coverage at layer boundaries.
  • Adds regression tests and changelog documentation.
File Summary
tests/​psd_tools/​composite/​test_widen.py Updates overlay tests.
tests/​psd_tools/​composite/​test_effects.py Adds stroke accuracy and coverage tests.
tests/​psd_tools/​composite/​test_composite.py Tests opacity, knockout, blend, and outer effects.
tests/​psd_tools/​composite/​__init__.py Adds shared test helpers.
src/​psd_tools/​composite/​effects.py Splits stroke coverage into inner and outer portions.
src/​psd_tools/​composite/​composite.py Implements effect composition and source handling; outer-effect opacity, blend interactions, and pass-through groups need changes.
docs/​changelog.rst Documents the fix; scope wording needs qualification.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/psd_tools/composite/composite.py Resolved
The inner effects get the layer opacity from being composited inside the
source it scales. An outer band is not in that source -- it goes on
before the layer, carrying its own blend mode -- and nothing later
applies the factor, so an outset stroke on a half-opaque layer rendered
at full strength wherever the layer covered nothing.

Each _OuterEffect now carries the layer's opacity along with its own,
which the union the layer composites with resolves to
``covered + t*o*op`` exactly. The two halves of the class agree about
what the layer opacity means again.

No fixture pairs a layer opacity below 255 with an effect that draws, so
the corpus renders bit for bit as it did and the case is forged.

Reported by Copilot on #884.

Co-Authored-By: Claude Opus 5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

An unresolved fill-opacity issue affects outset strokes; changelog wording and traceability also need updates.

Review effort: Lite
Findings: None

Resolved since last review (1)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Prevent fill opacity from attenuating outset-only strokes

src/​psd_tools/​composite/​composite.py:1424

For an outset-only stroke, canvas.over() receives no inside coverage, so _EffectCanvas never starts and canvas.source() returns the original source with fill_opacity still below 1. source.alpha here is therefore the pre-fill paint alpha, while _composite_source() applies fill_opacity only afterward; a partially masked layer with fill opacity 0 (or less than 1) consequently attenuates its outer stroke as though the hidden fill were still covering the pixel. This contradicts the stated “fill opacity fades the layer's paint but not its effects” behavior. Compute the outer-band scale from the post-fill visible coverage for non-knockout sources, while retaining the separate knockout-hole coverage, and add an outset/fill-opacity regression case.

kyamagu and others added 2 commits September 25, 2026 19:01
The band goes on scaled by what the layer will leave of the pixel, and
what the layer will leave is its alpha after fill opacity. The two agree
once an effect canvas has folded that in -- but an outset-only stroke
puts nothing inside the layer, so the canvas is never started and hands
back a source with fill opacity still on it. The band was then scaled by
a coverage the layer was never going to put there.

On feathered-stroke.psd's outset square at fill opacity 0, the band came
out opaque across the whole ramp instead of at 1 - coverage; it is now
that exactly. No fixture pairs fill opacity with an outset stroke --
the two that fade a layer carrying one stroke it inside -- so the corpus
renders bit for bit and the case is forged.

Reported by Copilot on #884.

Co-Authored-By: Claude Opus 5 <[email protected]>
The entry claimed partial coverage alone, and two of the changes reach a
hard-edged layer too: layer opacity now fades a layer's effects along
with the layer, where it used to fade the layer alone, and fill opacity
still fades neither.

Reported by Copilot on #884.

Co-Authored-By: Claude Opus 5 <[email protected]>
@kyamagu

kyamagu commented Sep 25, 2026

Copy link
Copy Markdown
Contributor Author

Both points from the re-review are fixed. The second one had no inline thread,
so it is answered here.

Fill opacity attenuating an outset-only stroke — real, and worse than
attenuation: the band came out opaque. An outset-only stroke puts nothing
inside the layer, so _EffectCanvas is never started and hands its source
back with fill_opacity still on it, and _apply_outer_effects was scaling
by the unfaded coverage. On feathered-stroke.psd's outset square at fill
opacity 0, across the layer's ramp:

layer coverage 0.082 0.251 0.416 0.584 0.749 0.918
band should be 0.918 0.749 0.584 0.416 0.251 0.082
was 1.000 1.000 1.000 1.000 1.000 1.000
now 0.918 0.749 0.584 0.416 0.251 0.082

The scale is now source.alpha * source.fill_opacity, which is exactly what
_composite_source() is about to put on, and is right whether or not the
canvas started: once it has, fill opacity is folded in and the field is 1.0.
The knockout hole keeps its own coverage in knockout_shape and is untouched.
Forged as test_fill_opacity_leaves_room_beside_a_layer_it_has_faded; the two
corpus fixtures that fade a layer carrying a stroke both stroke it inside,
so the corpus renders bit for bit either way.

Changelog scope — qualified. It claimed only partial coverage, and two of
the changes reach a hard-edged layer as well: layer opacity now fades a
layer's effects along with the layer, where it used to fade the layer alone.
The entry now names both and says what is unchanged.

Every fixture in tests/psd_files renders bit for bit against the previous
commit in both force modes, which is the check that neither fix moved
anything the corpus does have.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Address the two compositor semantics issues and correct the changelog wording before approval.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Low severity

Open (1)

Comment thread docs/changelog.rst Resolved
"fill opacity still fades neither" read as neither the layer nor its
effects, in a sentence whose other half is about the layer opacity fading
both. Fill opacity fades the layer's own paint; what it leaves alone is
the style over it, which is the whole point of the setting.

Reported by Copilot on #884.

Co-Authored-By: Claude Opus 5 <[email protected]>
@kyamagu
kyamagu merged commit 8f9a25e into main Sep 25, 2026
23 checks passed
@kyamagu
kyamagu deleted the fix/effects-composite-into-layer branch September 25, 2026 10:17
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.

Stroke effects composite onto the backdrop instead of into the layer

2 participants