Skip to content

Commit

Permalink
Fix rotate/extract ordering for non-90 angles (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
iovdin authored and lovell committed Jul 26, 2019
1 parent 50b4610 commit 690bc43
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
2 changes: 1 addition & 1 deletion lib/resize.js
Expand Up @@ -353,7 +353,7 @@ function extract (options) {
}
}, this);
// Ensure existing rotation occurs before pre-resize extraction
if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true)) {
if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true || this.options.rotationAngle !== 0)) {
this.options.rotateBeforePreExtract = true;
}
return this;
Expand Down
26 changes: 17 additions & 9 deletions src/pipeline.cc
Expand Up @@ -108,9 +108,16 @@ class PipelineWorker : public Nan::AsyncWorker {
}

// Rotate pre-extract
if (baton->rotateBeforePreExtract && rotation != VIPS_ANGLE_D0) {
image = image.rot(rotation);
sharp::RemoveExifOrientation(image);
if (baton->rotateBeforePreExtract) {
if (rotation != VIPS_ANGLE_D0) {
image = image.rot(rotation);
sharp::RemoveExifOrientation(image);
}
if (baton->rotationAngle != 0.0) {
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
}
}

// Trim
Expand Down Expand Up @@ -403,12 +410,13 @@ class PipelineWorker : public Nan::AsyncWorker {
->set("kernel", kernel));
}

// Rotate
if (!baton->rotateBeforePreExtract && rotation != VIPS_ANGLE_D0) {
image = image.rot(rotation);
sharp::RemoveExifOrientation(image);
// Rotate post-extract 90-angle
if (!baton->rotateBeforePreExtract && rotation != VIPS_ANGLE_D0) {
image = image.rot(rotation);
sharp::RemoveExifOrientation(image);
}


// Flip (mirror about Y axis)
if (baton->flip) {
image = image.flip(VIPS_DIRECTION_VERTICAL);
Expand Down Expand Up @@ -491,8 +499,8 @@ class PipelineWorker : public Nan::AsyncWorker {
}
}

// Rotate by degree
if (baton->rotationAngle != 0.0) {
// Rotate post-extract non-90 angle
if (!baton->rotateBeforePreExtract && baton->rotationAngle != 0.0) {
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
Expand Down
Binary file added test/fixtures/expected/extract-rotate-45.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/expected/rotate-extract-45.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions test/unit/extract.js
Expand Up @@ -121,6 +121,32 @@ describe('Partial image extraction', function () {
});
});

it('Extract then rotate non-90 anagle', function (done) {
sharp(fixtures.inputPngWithGreyAlpha)
.extract({ left: 20, top: 10, width: 380, height: 280 })
.rotate(45)
.jpeg()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(467, info.width);
assert.strictEqual(467, info.height);
fixtures.assertSimilar(fixtures.expected('extract-rotate-45.jpg'), data, done);
});
});

it('Rotate then extract non-90 angle', function (done) {
sharp(fixtures.inputPngWithGreyAlpha)
.rotate(45)
.extract({ left: 20, top: 10, width: 380, height: 280 })
.jpeg()
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(380, info.width);
assert.strictEqual(280, info.height);
fixtures.assertSimilar(fixtures.expected('rotate-extract-45.jpg'), data, { threshold: 7 }, done);
});
});

describe('Invalid parameters', function () {
describe('using the legacy extract(top,left,width,height) syntax', function () {
it('String top', function () {
Expand Down

0 comments on commit 690bc43

Please sign in to comment.