From 7c631c0787915416e20a567a039516e99c81c42d Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 3 Jan 2019 12:01:55 +0000 Subject: [PATCH] Ensure shortest resized edge is >= 1px #1003 --- docs/changelog.md | 3 +++ src/pipeline.cc | 9 +++++++++ test/unit/resize.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/docs/changelog.md b/docs/changelog.md index d43613a0..3d0d1637 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -6,6 +6,9 @@ Requires libvips v8.7.0. #### v0.21.2 - TBD +* Ensure shortest edge is at least one pixel after resizing. + [#1003](https://github.com/lovell/sharp/issues/1003) + * Expose `pages` and `pageHeight` metadata for multi-page input images. [#1205](https://github.com/lovell/sharp/issues/1205) diff --git a/src/pipeline.cc b/src/pipeline.cc index c3008126..029943b6 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -385,6 +385,15 @@ class PipelineWorker : public Nan::AsyncWorker { ) { throw vips::VError("Unknown kernel"); } + // Ensure shortest edge is at least 1 pixel + if (image.width() / xfactor < 0.5) { + xfactor = 2 * image.width(); + baton->width = 1; + } + if (image.height() / yfactor < 0.5) { + yfactor = 2 * image.height(); + baton->height = 1; + } image = image.resize(1.0 / xfactor, VImage::option() ->set("vscale", 1.0 / yfactor) ->set("kernel", kernel)); diff --git a/test/unit/resize.js b/test/unit/resize.js index fc5cbaf9..ee6ed639 100644 --- a/test/unit/resize.js +++ b/test/unit/resize.js @@ -525,6 +525,40 @@ describe('Resize dimensions', function () { }); }); + it('Ensure shortest edge (height) is at least 1 pixel', function () { + return sharp({ + create: { + width: 10, + height: 2, + channels: 3, + background: 'red' + } + }) + .resize(2) + .toBuffer({ resolveWithObject: true }) + .then(function (output) { + assert.strictEqual(2, output.info.width); + assert.strictEqual(1, output.info.height); + }); + }); + + it('Ensure shortest edge (width) is at least 1 pixel', function () { + return sharp({ + create: { + width: 2, + height: 10, + channels: 3, + background: 'red' + } + }) + .resize(null, 2) + .toBuffer({ resolveWithObject: true }) + .then(function (output) { + assert.strictEqual(1, output.info.width); + assert.strictEqual(2, output.info.height); + }); + }); + it('unknown kernel throws', function () { assert.throws(function () { sharp().resize(null, null, { kernel: 'unknown' });