diff --git a/docs/src/content/docs/changelog.md b/docs/src/content/docs/changelog.md index 5e37a495..5d202f98 100644 --- a/docs/src/content/docs/changelog.md +++ b/docs/src/content/docs/changelog.md @@ -18,6 +18,9 @@ Requires libvips v8.16.1 [#4375](https://github.com/lovell/sharp/pull/4375) [@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. [#4383](https://github.com/lovell/sharp/issues/4383) diff --git a/src/common.cc b/src/common.cc index 0f68be34..575b54e7 100644 --- a/src/common.cc +++ b/src/common.cc @@ -1001,9 +1001,11 @@ namespace sharp { 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()) { - alphaColour.push_back(colour[3] * multiplier); + do { + alphaColour.push_back(colour[3] * multiplier); + } while (alphaColour.size() < static_cast(image.bands())); } // Ensure alphaColour colour uses correct colourspace alphaColour = sharp::GetRgbaAsColourspace(alphaColour, image.interpretation(), premultiply); diff --git a/test/unit/resize-contain.js b/test/unit/resize-contain.js index 04c70df1..6e7f9ccc 100644 --- a/test/unit/resize-contain.js +++ b/test/unit/resize-contain.js @@ -806,4 +806,33 @@ describe('Resize fit=contain', function () { 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); + }); });