Skip to content

Commit

Permalink
Prevent error when cumulative rounding below target #1154
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Mar 13, 2018
1 parent 8f69023 commit f60f7da
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

Requires libvips v8.6.1.

#### v0.20.1 - TBD

* Prevent smartcrop error when cumulative rounding is below target size.
[#1154](https://github.com/lovell/sharp/issues/1154)
[@ralrom](https://github.com/ralrom)

#### v0.20.0 - 5<sup>th</sup> March 2018

* Add support for prebuilt sharp binaries on common platforms.
Expand Down
6 changes: 6 additions & 0 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,12 @@ class PipelineWorker : public Nan::AsyncWorker {
image = image.extract_area(left, top, width, height);
} else {
// Attention-based or Entropy-based crop
if (baton->width > image.width()) {
baton->width = image.width();
}
if (baton->height > image.height()) {
baton->height = image.height();
}
image = image.tilecache(VImage::option()
->set("access", baton->accessMethod)
->set("threaded", TRUE));
Expand Down
21 changes: 20 additions & 1 deletion test/unit/crop.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('Crop', function () {
});
});

it('Skip crop when post-resize dimensions are at or below target dimensions', function () {
it('Skip crop when post-resize dimensions are at target', function () {
return sharp(fixtures.inputJpg)
.resize(1600, 1200)
.toBuffer()
Expand All @@ -177,6 +177,25 @@ describe('Crop', function () {
});
});

it('Clamp before crop when one post-resize dimension is below target', function () {
return sharp(fixtures.inputJpg)
.resize(1024, 1034)
.toBuffer()
.then(function (input) {
return sharp(input)
.rotate(270)
.resize(256)
.crop(sharp.strategy.entropy)
.toBuffer({ resolveWithObject: true })
.then(function (result) {
assert.strictEqual(256, result.info.width);
assert.strictEqual(253, result.info.height);
assert.strictEqual(0, result.info.cropOffsetLeft);
assert.strictEqual(0, result.info.cropOffsetTop);
});
});
});

describe('Entropy-based strategy', function () {
it('JPEG', function (done) {
sharp(fixtures.inputJpg)
Expand Down

0 comments on commit f60f7da

Please sign in to comment.