Skip to content

Commit

Permalink
Fix erroneous top/left clipping in composite #2571
Browse files Browse the repository at this point in the history
Fixes bug where certain input values for top/left parameters
in composite can conflict with clipping logic, resulting in
inaccurate alignment in output.
  • Loading branch information
SHG42 authored and lovell committed Mar 22, 2021
1 parent 83fe65b commit 34a2e14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -602,8 +602,13 @@ class PipelineWorker : public Napi::AsyncWorker {
int top;
if (composite->hasOffset) {
// Composite image at given offsets
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
compositeImage.width(), compositeImage.height(), composite->left, composite->top);
if (composite->tile) {
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
compositeImage.width(), compositeImage.height(), composite->left, composite->top);
} else {
left = composite->left;
top = composite->top;
}
} else {
// Composite image with given gravity
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
Expand Down
16 changes: 16 additions & 0 deletions test/unit/composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,20 @@ describe('composite', () => {
}, /Expected valid gravity for gravity but received invalid of type string/);
});
});

it('Allow offset beyond bottom/right edge', async () => {
const red = { r: 255, g: 0, b: 0 };
const blue = { r: 0, g: 0, b: 255 };

const [r, g, b] = await sharp({ create: { width: 2, height: 2, channels: 4, background: red } })
.composite([{
input: { create: { width: 2, height: 2, channels: 4, background: blue } },
top: 1,
left: 1
}])
.raw()
.toBuffer();

assert.deepStrictEqual(red, { r, g, b });
});
});

0 comments on commit 34a2e14

Please sign in to comment.