Skip to content

Commit 34a2e14

Browse files
SHG42lovell
authored andcommittedMar 22, 2021
Fix erroneous top/left clipping in composite #2571
Fixes bug where certain input values for top/left parameters in composite can conflict with clipping logic, resulting in inaccurate alignment in output.
1 parent 83fe65b commit 34a2e14

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎src/pipeline.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,13 @@ class PipelineWorker : public Napi::AsyncWorker {
602602
int top;
603603
if (composite->hasOffset) {
604604
// Composite image at given offsets
605-
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
606-
compositeImage.width(), compositeImage.height(), composite->left, composite->top);
605+
if (composite->tile) {
606+
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),
607+
compositeImage.width(), compositeImage.height(), composite->left, composite->top);
608+
} else {
609+
left = composite->left;
610+
top = composite->top;
611+
}
607612
} else {
608613
// Composite image with given gravity
609614
std::tie(left, top) = sharp::CalculateCrop(image.width(), image.height(),

‎test/unit/composite.js

+16
Original file line numberDiff line numberDiff line change
@@ -408,4 +408,20 @@ describe('composite', () => {
408408
}, /Expected valid gravity for gravity but received invalid of type string/);
409409
});
410410
});
411+
412+
it('Allow offset beyond bottom/right edge', async () => {
413+
const red = { r: 255, g: 0, b: 0 };
414+
const blue = { r: 0, g: 0, b: 255 };
415+
416+
const [r, g, b] = await sharp({ create: { width: 2, height: 2, channels: 4, background: red } })
417+
.composite([{
418+
input: { create: { width: 2, height: 2, channels: 4, background: blue } },
419+
top: 1,
420+
left: 1
421+
}])
422+
.raw()
423+
.toBuffer();
424+
425+
assert.deepStrictEqual(red, { r, g, b });
426+
});
411427
});

0 commit comments

Comments
 (0)
Please sign in to comment.