From 34a2e14a144b8768f326751a88c81c0e2d19c59b Mon Sep 17 00:00:00 2001 From: SHG42 Date: Wed, 24 Feb 2021 12:02:13 -0500 Subject: [PATCH] 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. --- src/pipeline.cc | 9 +++++++-- test/unit/composite.js | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/pipeline.cc b/src/pipeline.cc index 4837a939..285cbfe2 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -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(), diff --git a/test/unit/composite.js b/test/unit/composite.js index 069d807f..389e6e62 100644 --- a/test/unit/composite.js +++ b/test/unit/composite.js @@ -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 }); + }); });