diff --git a/docs/changelog.md b/docs/changelog.md index dd5ebe5b..7c4894ce 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -41,6 +41,10 @@ Requires libvips v8.5.2. [#772](https://github.com/lovell/sharp/issues/772) [@ajiratech2](https://github.com/ajiratech2) +* Prevent aliasing by using dynamic values for shrink(-on-load). + [#781](https://github.com/lovell/sharp/issues/781) + [@kleisauke](https://github.com/kleisauke) + * Expose libvips' "squash" parameter to enable 1-bit TIFF output. [#783](https://github.com/lovell/sharp/pull/783) [@YvesBos](https://github.com/YvesBos) diff --git a/src/pipeline.cc b/src/pipeline.cc index 47c50772..706315b4 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -242,6 +242,12 @@ class PipelineWorker : public Nan::AsyncWorker { shrink_on_load = 2; } } + // Help ensure a final kernel-based reduction to prevent shrink aliasing + if (shrink_on_load > 1 && (xresidual == 1.0 || yresidual == 1.0)) { + shrink_on_load = shrink_on_load / 2; + xfactor = xfactor * 2; + yfactor = yfactor * 2; + } if (shrink_on_load > 1) { // Reload input using shrink-on-load vips::VOption *option = VImage::option()->set("shrink", shrink_on_load); @@ -285,6 +291,13 @@ class PipelineWorker : public Nan::AsyncWorker { std::swap(xresidual, yresidual); } } + // Help ensure a final kernel-based reduction to prevent shrink aliasing + if ((xshrink > 1 || yshrink > 1) && (xresidual == 1.0 || yresidual == 1.0)) { + xshrink = xshrink / 2; + yshrink = yshrink / 2; + xresidual = xresidual / 2.0; + yresidual = yresidual / 2.0; + } // Ensure we're using a device-independent colour space if (sharp::HasProfile(image)) { diff --git a/test/fixtures/expected/embed-16bit-rgba.png b/test/fixtures/expected/embed-16bit-rgba.png index eb31604d..29b8f635 100644 Binary files a/test/fixtures/expected/embed-16bit-rgba.png and b/test/fixtures/expected/embed-16bit-rgba.png differ diff --git a/test/fixtures/expected/extend-2channel.png b/test/fixtures/expected/extend-2channel.png index bcd4cad4..bd57b5a9 100644 Binary files a/test/fixtures/expected/extend-2channel.png and b/test/fixtures/expected/extend-2channel.png differ diff --git a/test/fixtures/expected/gamma-0.0.jpg b/test/fixtures/expected/gamma-0.0.jpg index 01dba1c1..4ae2fe07 100644 Binary files a/test/fixtures/expected/gamma-0.0.jpg and b/test/fixtures/expected/gamma-0.0.jpg differ diff --git a/test/unit/io.js b/test/unit/io.js index 316e8b70..07889a2b 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -1335,7 +1335,7 @@ describe('Input/output', function () { assert.strictEqual(256, info.width); assert.strictEqual(192, info.height); assert.strictEqual(4, info.channels); - fixtures.assertSimilar(fixtures.inputPngOverlayLayer1, data, done); + fixtures.assertSimilar(fixtures.inputPngOverlayLayer1, data, { threshold: 7 }, done); }); }); });