Use bounding box of alpha+non-alpha for trim op #2166

This commit is contained in:
Lovell Fuller
2022-07-05 18:19:17 +01:00
parent e3cab7f10f
commit e0d3c6e05d
8 changed files with 44 additions and 14 deletions

View File

@@ -94,6 +94,7 @@ module.exports = {
inputPngSolidAlpha: getPath('with-alpha.png'), // https://github.com/lovell/sharp/issues/1599
inputPngP3: getPath('p3.png'), // https://github.com/lovell/sharp/issues/2862
inputPngPalette: getPath('swiss.png'), // https://github.com/randy408/libspng/issues/188
inputPngTrimIncludeAlpha: getPath('trim-mc.png'), // https://github.com/lovell/sharp/issues/2166
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp

BIN
test/fixtures/trim-mc.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@@ -128,6 +128,18 @@ describe('Trim borders', function () {
)
);
it('Ensure trim uses bounding box of alpha and non-alpha channels', async () => {
const { info } = await sharp(fixtures.inputPngTrimIncludeAlpha)
.trim()
.toBuffer({ resolveWithObject: true });
const { width, height, trimOffsetTop, trimOffsetLeft } = info;
assert.strictEqual(width, 179);
assert.strictEqual(height, 123);
assert.strictEqual(trimOffsetTop, -44);
assert.strictEqual(trimOffsetLeft, -13);
});
describe('Invalid thresholds', function () {
[-1, 'fail', {}].forEach(function (threshold) {
it(JSON.stringify(threshold), function () {