Repository navigation
fix(svgr): apply the feDisplacementMap scale once and read the map unpremultiplied - #24
khanhthanhdev wants to merge 1 commit into
Conversation
…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.
|
No actionable comments were generated in the recent review.
|
| 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:
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: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
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.
Comment @coderabbitai help to get the list of available commands.

feDisplacementMapdraws garbage. A uniform map that should move an element by a few pixelsmoves 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
scaleis applied twice.apply_displacement_mapconverts the scale to pixels withscale_coordinates(fe.scale(), fe.scale(), ts), anddisplacement_map::applythenmultiplied the offset by
fe.scale()again. Pixels moved byscale² × (C − 0.5): atscale="20"that is 400 × (C − 0.5) px instead of 20 × (C − 0.5).values from
in2are performed using non-premultiplied color values", anddisplacement_map::apply's doc comment asks for the same, but the caller passed thepremultiplied 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:
uniform-map(#c0c0c0, scale 20)semi-transparent-map(white at 50%, scale 20)Through fframes: a 960 × 540 frame with a uniform map, a
feTurbulence type="turbulence"map andan opaque
fractalNoisemap at scale 29, compared with fframes' Skia-CPU render (odiff,threshold 0.15):
--renderer cpu--renderer skia-cpu(Comparison image below.)
Tests
filters/feDisplacementMap/uniform-mapandsemi-transparent-map, references madeby the harness (
MAKE_REF=1) and compressed withoxipng -o 6 -Z.cargo test -p svgr --release: 1,707 passed, 0 failed.-actual.pngfiles; 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
TODOabout anti-aliasing indisplacement_map.rs), which is thelikely cause. resvg#1157 also adds
scale=0,turbulence-mapandprimitiveUnits=objectBoundingBoxtests; they can follow separately.Summary by CodeRabbit