Aliasing problem due to shrink-on-load(?)
See original GitHub issueI discovered this bug by chance while testing some images. See this testcase:
const sharp = require("sharp");
sharp('test_800x600.jpg')
.resize(400)
.toFile('test_800x600_aliasing.jpg');
(original picture can be downloaded from here)
Output (tested with sharp v0.17.3):
(nasty aliasing artifacts at the roof)
Expected output:
(with vipsthumbnail)
The thumbnail operator from libvips has fixed this by leaving at least a factor of two for the final resize step. See thumbnail.c#L203-L217, and the wiki.
While looking at the source code of sharp, I noticed that the shrink-on-load doesn’t take this into account. See pipeline.cc#L230-L241.
In an attempt to fix this, I came to this patch (line 230-241 in pipeline.cc):
- if (xshrink >= 8) {
+ if (xshrink >= 16) {
xfactor = xfactor / 8;
yfactor = yfactor / 8;
shrink_on_load = 8;
- } else if (xshrink >= 4) {
+ } else if (xshrink >= 8) {
xfactor = xfactor / 4;
yfactor = yfactor / 4;
shrink_on_load = 4;
- } else if (xshrink >= 2) {
+ } else if (xshrink >= 4) {
xfactor = xfactor / 2;
yfactor = yfactor / 2;
shrink_on_load = 2;
}
But that doesn’t seem to work (maybe I’m missing something).
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
What is aliasing? What causes it? How to avoid it? - WolfSound
It is caused by too low sample rate for sampling a particular signal or too high frequencies present in the signal for a...
Read more >Aliasing - Wikipedia
In signal processing and related disciplines, aliasing is an effect that causes different signals to become indistinguishable when sampled.
Read more >Sampling and Aliasing - NJIT
When we sample at a rate which is less than the Nyquist rate, we say we are undersampling and aliasing will yield misleading...
Read more >What are aliasing errors? Are they hard to detect? - Tektronix
An alias is a false lower frequency component that appears in sampled data acquired at too low a sampling rate. Aliasing errors occur...
Read more >Sampling, aliasing, and analog anti-alias filtering
The problem of aliasing that arises when analog data is sampled for filtering in a digital system, and anti-alias filters to prevent it....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Commit e398b47 adds the dynamic shrink(-on-load) behaviour to ensure a kernel-based reduction will always occur. This will be in v0.18.0. Thanks again for your original, well-written report about this.
Quick update: I’ve got a prototype working that dynamically varies the use of shrink-on-load and box shrink based on the level of kernel-based reduction. The tl;dr is “same high performance most of the time, occasionally take a slight performance hit to ensure high quality”.