Ensure shortest resized edge is >= 1px #1003

This commit is contained in:
Lovell Fuller 2019-01-03 12:01:55 +00:00
parent f5d3721fe0
commit 7c631c0787
3 changed files with 46 additions and 0 deletions

View File

@ -6,6 +6,9 @@ Requires libvips v8.7.0.
#### v0.21.2 - TBD #### 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. * Expose `pages` and `pageHeight` metadata for multi-page input images.
[#1205](https://github.com/lovell/sharp/issues/1205) [#1205](https://github.com/lovell/sharp/issues/1205)

View File

@ -385,6 +385,15 @@ class PipelineWorker : public Nan::AsyncWorker {
) { ) {
throw vips::VError("Unknown kernel"); 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() image = image.resize(1.0 / xfactor, VImage::option()
->set("vscale", 1.0 / yfactor) ->set("vscale", 1.0 / yfactor)
->set("kernel", kernel)); ->set("kernel", kernel));

View File

@ -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 () { it('unknown kernel throws', function () {
assert.throws(function () { assert.throws(function () {
sharp().resize(null, null, { kernel: 'unknown' }); sharp().resize(null, null, { kernel: 'unknown' });