Skip to content

fix(svgr): apply the feDisplacementMap scale once and read the map unpremultiplied - #24

Open
khanhthanhdev wants to merge 1 commit into
dmtrKovalenko:mainfrom
khanhthanhdev:fix/displacement-map-scale
Open

khanhthanhdev wants to merge 1 commit into
dmtrKovalenko:mainfrom
khanhthanhdev:fix/displacement-map-scale

Conversation

@khanhthanhdev

@khanhthanhdev khanhthanhdev commented Oct 24, 2026 •

Copy link
Copy Markdown

feDisplacementMap draws garbage. A uniform map that should move an element by a few pixels
moves it by a hundred, or out of the filter region. fframes renders the same frame correctly
with its Skia backend, so the CPU and Skia renders of a video disagree.

This is the same fix as linebender#1157 (upstream issues linebender#1160 and
linebender#1019), ported to svgr.

Two bugs

  1. scale is applied twice. apply_displacement_map converts the scale to pixels with
    scale_coordinates(fe.scale(), fe.scale(), ts), and displacement_map::apply then
    multiplied the offset by fe.scale() again. Pixels moved by scale² × (C − 0.5): at
    scale="20" that is 400 × (C − 0.5) px instead of 20 × (C − 0.5).
  2. The map is read premultiplied. Filter Effects 1 says "The calculations using the pixel
    values from in2 are performed using non-premultiplied color values", and
    displacement_map::apply's doc comment asks for the same, but the caller passed the
    premultiplied pixmap. Semi-transparent maps, such as feTurbulence type="turbulence"
    output, then pulled every offset toward the top left.

Before and after

The new tests draw the expected position as a dashed red outline. Box of the blue pixels on a
200 × 200 canvas:

Test Expected Before After
uniform-map (#c0c0c0, scale 20) x 45..=144, y 45..=144 x 0..=48, y 0..=48 x 45..=144, y 45..=144
semi-transparent-map (white at 50%, scale 20) x 40..=139, y 40..=139 x 49..=148, y 49..=148 x 40..=139, y 40..=139

Through fframes: a 960 × 540 frame with a uniform map, a feTurbulence type="turbulence" map and
an opaque fractalNoise map at scale 29, compared with fframes' Skia-CPU render (odiff,
threshold 0.15):

fframes --renderer cpu Pixels that differ from --renderer skia-cpu
svgr 0.50.2 57.27%
svgr 0.50.2 with this fix 0.50%

(Comparison image below.)

Tests

  • New tests filters/feDisplacementMap/uniform-map and semi-transparent-map, references made
    by the harness (MAKE_REF=1) and compressed with oxipng -o 6 -Z.
  • cargo test -p svgr --release: 1,707 passed, 0 failed.
  • The two new tests come without -actual.png files; the harness writes those on every run.

Not in this PR

After the fix, 0.50% of the pixels of the fframes frame still differ from Skia-CPU: thin lines
along the edges of displaced shapes, almost all in the turbulence panel. svgr samples the
nearest whole pixel (the TODO about anti-aliasing in displacement_map.rs), which is the
likely cause. resvg#1157 also adds scale=0, turbulence-map and
primitiveUnits=objectBoundingBox tests; they can follow separately.

Summary by CodeRabbit

  • Bug Fixes
    • Improved rendering of SVG displacement-map effects when the map contains transparency or uniform colors.
    • Corrected displacement scaling so map offsets are applied consistently in pixel coordinates. This can improve the positioning and appearance of affected filtered graphics.

…premultiplied

scale_coordinates() already turns `scale` into pixels, and
displacement_map::apply() multiplied the offset by `scale` again, so
pixels moved by scale squared and mostly left the filter region. The map
was also read with premultiplied colors, which Filter Effects 1 forbids,
so semi-transparent maps such as feTurbulence output pulled every offset
toward the top left.

Same fix as linebender#1157. Adds a uniform-map and a
semi-transparent-map test, each with the expected outline drawn in.
Copilot AI balanced review requested due to automatic review settings October 24, 2026 14:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5b11ff7c-f775-4483-8344-96e8c9d15aac
Commits

Reviewing files that changed from the base of the PR and between 4804366 and 025e5e9.

Files ignored due to path filters: 4
  • crates/svgr/tests/tests/filters/feDisplacementMap/semi-transparent-map.png is excluded by !**/*.png
  • crates/svgr/tests/tests/filters/feDisplacementMap/semi-transparent-map.svg is excluded by !**/*.svg
  • crates/svgr/tests/tests/filters/feDisplacementMap/uniform-map.png is excluded by !**/*.png
  • crates/svgr/tests/tests/filters/feDisplacementMap/uniform-map.svg is excluded by !**/*.svg
Files selected for processing: 3
  • crates/svgr/src/filter/displacement_map.rs
  • crates/svgr/src/filter/mod.rs
  • crates/svgr/tests/integration/render.rs

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

Walkthrough

The displacement-map path now demultiplies the map input’s alpha and uses canvas-coordinate scales directly for channel offsets. Integration tests cover semi-transparent and uniform displacement maps.

Changes: Displacement map processing

Layer / File(s) Summary
Map preprocessing and displacement calculation
crates/svgr/src/filter/mod.rs, crates/svgr/src/filter/displacement_map.rs, crates/svgr/tests/integration/render.rs
The map input’s alpha is demultiplied before displacement mapping. The offset calculation uses the provided scales directly. Integration tests cover semi-transparent and uniform maps.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: dmtrkovalenko

Merge Risk: Minimal · up to 025e5

No actionable merge-blocking risk was established in the changed displacement-map path.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 9.09% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 11 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly identifies both primary fixes: preventing duplicate feDisplacementMap scale application and reading the displacement map as unpremultiplied.
Description check Passed The description explains the two bugs, the implementation scope, expected behavior, added tests, test results, comparison results, and remaining limitations. It demonstrates a meaningful project impro…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the two bugs, the implementation scope, expected behavior, added tests, test results, comparison results, and remaining limitations. It demonstrates a meaningful project improvement and does not describe a prohibited dependency, formatting, clippy, or compiler-warning change.

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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@khanhthanhdev

Copy link
Copy Markdown
Author
image

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.

2 participants