Ensure fit=contain resizing supports multiple alpha channels #4382

This commit is contained in:
Lovell Fuller 2025-05-13 14:30:34 +01:00
parent 32872ef840
commit 94481a967e
3 changed files with 36 additions and 2 deletions

View File

@ -18,6 +18,9 @@ Requires libvips v8.16.1
[#4375](https://github.com/lovell/sharp/pull/4375) [#4375](https://github.com/lovell/sharp/pull/4375)
[@hans00](https://github.com/hans00) [@hans00](https://github.com/hans00)
* Ensure resizing with a `fit` of `contain` supports multiple alpha channels.
[#4382](https://github.com/lovell/sharp/issues/4382)
* TypeScript: Ensure `metadata` response more closely matches reality. * TypeScript: Ensure `metadata` response more closely matches reality.
[#4383](https://github.com/lovell/sharp/issues/4383) [#4383](https://github.com/lovell/sharp/issues/4383)

View File

@ -1001,9 +1001,11 @@ namespace sharp {
0.0722 * colour[2]) 0.0722 * colour[2])
}; };
} }
// Add alpha channel to alphaColour colour // Add alpha channel(s) to alphaColour colour
if (colour[3] < 255.0 || image.has_alpha()) { if (colour[3] < 255.0 || image.has_alpha()) {
alphaColour.push_back(colour[3] * multiplier); do {
alphaColour.push_back(colour[3] * multiplier);
} while (alphaColour.size() < static_cast<size_t>(image.bands()));
} }
// Ensure alphaColour colour uses correct colourspace // Ensure alphaColour colour uses correct colourspace
alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation(), premultiply); alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation(), premultiply);

View File

@ -806,4 +806,33 @@ describe('Resize fit=contain', function () {
fixtures.assertSimilar(fixtures.expected('./embedgravitybird/9-c.png'), data, done); fixtures.assertSimilar(fixtures.expected('./embedgravitybird/9-c.png'), data, done);
}); });
}); });
it('multiple alpha channels', async () => {
const create = {
width: 20,
height: 12,
channels: 4,
background: 'green'
};
const multipleAlphaChannels = await sharp({ create })
.joinChannel({ create })
.tiff({ compression: 'deflate' })
.toBuffer();
const data = await sharp(multipleAlphaChannels)
.resize({
width: 8,
height: 8,
fit: 'contain',
background: 'blue'
})
.tiff({ compression: 'deflate' })
.toBuffer();
const { format, width, height, space, channels } = await sharp(data).metadata();
assert.deepStrictEqual(format, 'tiff');
assert.deepStrictEqual(width, 8);
assert.deepStrictEqual(height, 8);
assert.deepStrictEqual(space, 'srgb');
assert.deepStrictEqual(channels, 8);
});
}); });