Fix excessive sampling of linear gradients in PDF and SVG#7818
Fix excessive sampling of linear gradients in PDF and SVG#7818laurmaedje merged 4 commits intotypst:mainfrom
Conversation
|
I've mentioned this somewhere else, but in case it hasn't been tried, I would strongly suggest trying to use the color crate and the gradient method to create the optimal number and location of stops to approximate the color in SRGB. I don't know the theory behind it, but I think they use some more advanced approach to determine the best approximation for the given color space. This won't work for CMYK unfortunately, so we would need fallback behavior there, but I believe all other color spaces are supported. |
|
Thank you, I looked at how they do it and basically followed the same algorithm: bisecting the gradient until the Euclidean difference between the rgb-linearly interpolated midpoint color and the midpoint actual color between two stops falls below a threshold. I decided against using the color create to avoid adding it as dependency, to avoid the copying and type conversion necessary to call that method, to avoid the non-CMYK restriction and because in the end it's a rather simple algorithm. |
|
Fair enough! |
|
@eltos Is this ready for review? Since this and your other PR are still marked as draft. |
|
Not yet, there is a similar stop duplication behaviour in SVG export which I plan to cover, too. Please bear with me, I'll notify you once I come to it and the PR is ready. |
|
Sure, no rush, it's just that sometimes people open a draft PR when they're actually waiting for a review, so I wanted to double check. |
d42c217 to
ba121d9
Compare
|
@laurmaedje PR ready for review. SVG handling added and threshold lowered to 0.001, see details in updated PR description. |
saecki
left a comment
There was a problem hiding this comment.
Very nice! Just one little nitpick. This also reduces the time for the test suite to run on my machine from ~4.3s to ~3.1s.
3c3b8fb to
76d966c
Compare
|
Thank you! |
Description
Commit a82dce9
Fixes duplicating stops at equal offset during interpolation, which caused sharp gradients in PDF to become blurry.
Also fixes duplicating stops in SVG where both stops of the (overlapping) window strides were added.
This results in visual changes (fixes) to the tests containing sharp gradients (see examples below).
Maybe also Sharp gradients overlap when printing #6968, to be clearified
Contributes to SVG Export size reductions #6359
Commit ba121d9
Instead of always inserting a fixed number of in-between-stops (30 for PDF, 256/len for SVG), only insert actually required stops.
How many stops are required is determined by comparing the linearly rgb-interpolated color with the actual color at the midpoints, iteratively subdividing the distance between stops until the error falls below a threshold.
The threshold of 0.001 used results in no visible change.
I also tested with 0.01 where there are very small almost unnoticable changes (see examples below).
Since the step 0.001 to 0.01 reduces the size only little, I decided to use 0.001 in the end, but this can be adjusted if desired.
Contributes to SVG Export size reductions #6359
Size statistics for
cargo testit gradientStop count bloat factor denotes the number of stops after the interpolation, compared to the number of stops of the original gradient (mean of factors for all tests)
Changes (examples)
Changes due to sharp gradient fix:
Changes in case of th=0.01: