Skip to content

Commit

Permalink
Ensure image >= 3x3 before attempting trim operation
Browse files Browse the repository at this point in the history
  • Loading branch information
webgurucan committed Aug 13, 2019
1 parent 8833acd commit 0f0b87c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Requires libvips v8.8.1.
[#1834](https://github.com/lovell/sharp/pull/1834)
[@jaubourg](https://github.com/jaubourg)

* Ensure image is at least 3x3 pixels before attempting trim operation.

#### v0.23.0 - 29<sup>th</sup> July 2019

* Remove `overlayWith` previously deprecated in v0.22.0.
Expand Down
3 changes: 3 additions & 0 deletions src/operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ namespace sharp {
Trim an image
*/
VImage Trim(VImage image, double const threshold) {
if (image.width() < 3 && image.height() < 3) {
throw VError("Image to trim must be at least 3x3 pixels");
}
// Top-left pixel provides the background colour
VImage background = image.extract_area(0, 0, 1, 1);
if (HasAlpha(background)) {
Expand Down
21 changes: 21 additions & 0 deletions test/unit/trim.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ describe('Trim borders', function () {
});
});

it('Attempt to trim 2x2 pixel image fails', function (done) {
sharp({
create: {
width: 2,
height: 2,
channels: 3,
background: 'red'
}
})
.trim()
.toBuffer()
.then(() => {
done(new Error('Expected an error'));
})
.catch(err => {
assert.strictEqual('Image to trim must be at least 3x3 pixels', err.message);
done();
})
.catch(done);
});

describe('Invalid thresholds', function () {
[-1, 'fail', {}].forEach(function (threshold) {
it(JSON.stringify(threshold), function () {
Expand Down

0 comments on commit 0f0b87c

Please sign in to comment.