Skip to content

Commit

Permalink
Merge 15dd015 into 513fb40
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Dec 11, 2021
2 parents 513fb40 + 15dd015 commit 769f0ea
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/pipeline.cc
Expand Up @@ -98,6 +98,7 @@ class PipelineWorker : public Napi::AsyncWorker {
image = sharp::RemoveExifOrientation(image);
}
if (baton->rotationAngle != 0.0) {
MultiPageUnsupported(nPages, "Rotate");
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground, FALSE);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
Expand All @@ -106,6 +107,7 @@ class PipelineWorker : public Napi::AsyncWorker {

// Trim
if (baton->trimThreshold > 0.0) {
MultiPageUnsupported(nPages, "Trim");
image = sharp::Trim(image, baton->trimThreshold);
baton->trimOffsetLeft = image.xoffset();
baton->trimOffsetTop = image.yoffset();
Expand Down Expand Up @@ -461,8 +463,9 @@ class PipelineWorker : public Napi::AsyncWorker {
? sharp::CropMultiPage(image,
left, top, width, height, nPages, &targetPageHeight)
: image.extract_area(left, top, width, height);
} else if (nPages == 1) { // Skip smart crop for multi-page images
} else {
// Attention-based or Entropy-based crop
MultiPageUnsupported(nPages, "Resize strategy");
image = image.tilecache(VImage::option()
->set("access", VIPS_ACCESS_RANDOM)
->set("threaded", TRUE));
Expand All @@ -477,6 +480,7 @@ class PipelineWorker : public Napi::AsyncWorker {

// Rotate post-extract non-90 angle
if (!baton->rotateBeforePreExtract && baton->rotationAngle != 0.0) {
MultiPageUnsupported(nPages, "Rotate");
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->rotationBackground, shouldPremultiplyAlpha);
image = image.rotate(baton->rotationAngle, VImage::option()->set("background", background));
Expand All @@ -499,6 +503,7 @@ class PipelineWorker : public Napi::AsyncWorker {

// Affine transform
if (baton->affineMatrix.size() > 0) {
MultiPageUnsupported(nPages, "Affine");
std::vector<double> background;
std::tie(image, background) = sharp::ApplyAlpha(image, baton->affineBackground, shouldPremultiplyAlpha);
image = image.affine(baton->affineMatrix, VImage::option()->set("background", background)
Expand Down Expand Up @@ -1224,6 +1229,12 @@ class PipelineWorker : public Napi::AsyncWorker {
Napi::FunctionReference debuglog;
Napi::FunctionReference queueListener;

void MultiPageUnsupported(int const pages, std::string op) {
if (pages > 1) {
throw vips::VError(op + " is not supported for multi-page images");
}
}

/*
Calculate the angle of rotation and need-to-flip for the given Exif orientation
By default, returns zero, i.e. no rotation.
Expand Down
9 changes: 9 additions & 0 deletions test/unit/affine.js
Expand Up @@ -155,6 +155,15 @@ describe('Affine transform', () => {
fixtures.assertSimilar(fixtures.expected('affine-background-all-offsets-expected.jpg'), data, done);
});
});

it('Animated image rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.affine([1, 1, 1, 1])
.toBuffer(),
/Affine is not supported for multi-page images/
)
);

describe('Interpolations', () => {
const input = fixtures.inputJpg320x240;
const inputWidth = 320;
Expand Down
24 changes: 24 additions & 0 deletions test/unit/resize-cover.js
Expand Up @@ -347,6 +347,18 @@ describe('Resize fit=cover', function () {
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});

it('Animated image rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.resize({
width: 100,
height: 8,
position: sharp.strategy.entropy
})
.toBuffer(),
/Resize strategy is not supported for multi-page images/
)
);
});

describe('Attention strategy', function () {
Expand Down Expand Up @@ -403,5 +415,17 @@ describe('Resize fit=cover', function () {
fixtures.assertSimilar(fixtures.expected('crop-strategy.png'), data, done);
});
});

it('Animated image rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.resize({
width: 100,
height: 8,
position: sharp.strategy.attention
})
.toBuffer(),
/Resize strategy is not supported for multi-page images/
)
);
});
});
28 changes: 28 additions & 0 deletions test/unit/rotate.js
Expand Up @@ -272,6 +272,34 @@ describe('Rotation', function () {
});
});

it('Animated image rotate-then-extract rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.rotate(1)
.extract({
top: 1,
left: 1,
width: 10,
height: 10
})
.toBuffer(),
/Rotate is not supported for multi-page images/
)
);

it('Animated image extract-then-rotate rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.extract({
top: 1,
left: 1,
width: 10,
height: 10
})
.rotate(1)
.toBuffer(),
/Rotate is not supported for multi-page images/
)
);

it('Flip - vertical', function (done) {
sharp(fixtures.inputJpg)
.resize(320)
Expand Down
8 changes: 8 additions & 0 deletions test/unit/trim.js
Expand Up @@ -120,6 +120,14 @@ describe('Trim borders', function () {
)
);

it('Animated image rejects', () =>
assert.rejects(() => sharp(fixtures.inputGifAnimated, { animated: true })
.trim()
.toBuffer(),
/Trim is not supported for multi-page images/
)
);

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

0 comments on commit 769f0ea

Please sign in to comment.