Add alpha channel, if required, before extend operation (#439)

This commit is contained in:
frulo 2016-05-26 10:46:14 +02:00 committed by Lovell Fuller
parent 331926dc3c
commit e699e36270
3 changed files with 20 additions and 1 deletions

View File

@ -598,12 +598,19 @@ class PipelineWorker : public AsyncWorker {
baton->background[2] * multiplier
};
// Add alpha channel to background colour
if (HasAlpha(image)) {
if (baton->background[3] < 255.0 || HasAlpha(image)) {
background.push_back(baton->background[3] * multiplier);
}
// Add non-transparent alpha channel, if required
if (baton->background[3] < 255.0 && !HasAlpha(image)) {
image = image.bandjoin(
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
);
}
// Embed
baton->width = image.width() + baton->extendLeft + baton->extendRight;
baton->height = image.height() + baton->extendTop + baton->extendBottom;
image = image.embed(baton->extendLeft, baton->extendTop, baton->width, baton->height,
VImage::option()->set("extend", VIPS_EXTEND_BACKGROUND)->set("background", background));
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 644 KiB

View File

@ -49,4 +49,16 @@ describe('Extend', function () {
});
});
it('should add alpha channel before extending with a transparent Background', function( done ){
sharp(fixtures.inputJpgWithLandscapeExif1)
.background({r: 0, g: 0, b: 0, a: 0})
.toFormat( sharp.format.png )
.extend({top: 0, bottom: 10, left: 0, right: 10})
.toBuffer( function(err, data, info){
assert.strictEqual(610, info.width);
assert.strictEqual(460, info.height);
fixtures.assertSimilar(fixtures.expected('addAlphaChanelBeforeExtend.png'), data, done);
});
});
});