question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aliasing problem due to shrink-on-load(?)

See original GitHub issue

I 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): test_800x600_aliasing_wrong (nasty aliasing artifacts at the roof)

Expected output: test_800x600_aliasing_good (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:closed
  • Created 6 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lovellcommented, May 13, 2017

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.

1reaction
lovellcommented, May 11, 2017

“worth experimenting with applying this change only when there will be no floating point residual”

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”.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found