Add alpha channels, if missing, to overlayWith images (#540)

This commit is contained in:
cmtt 2016-08-13 18:19:15 +02:00 committed by Lovell Fuller
parent 82ec2715f1
commit fc2002fbd0
3 changed files with 28 additions and 4 deletions

View File

@ -26,7 +26,8 @@
"Chintan Thakkar <lemnisk8@gmail.com>", "Chintan Thakkar <lemnisk8@gmail.com>",
"F. Orlando Galashan <frulo@gmx.de>", "F. Orlando Galashan <frulo@gmx.de>",
"Kleis Auke Wolthuizen <info@kleisauke.nl>", "Kleis Auke Wolthuizen <info@kleisauke.nl>",
"Matt Hirsch <mhirsch@media.mit.edu>" "Matt Hirsch <mhirsch@media.mit.edu>",
"Matthias Thoemmes <thoemmes@gmail.com>"
], ],
"description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images", "description": "High performance Node.js image processing, the fastest module to resize JPEG, PNG, WebP and TIFF images",
"scripts": { "scripts": {

View File

@ -581,6 +581,20 @@ class PipelineWorker : public Nan::AsyncWorker {
// 'cut out' the image, premultiplication is not required // 'cut out' the image, premultiplication is not required
image = sharp::Cutout(overlayImage, image, baton->overlayGravity); image = sharp::Cutout(overlayImage, image, baton->overlayGravity);
} else { } else {
// Ensure overlay has alpha channel
if (!HasAlpha(overlayImage)) {
double const multiplier = sharp::Is16Bit(overlayImage.interpretation()) ? 256.0 : 1.0;
overlayImage = overlayImage.bandjoin(
VImage::new_matrix(overlayImage.width(), overlayImage.height()).new_from_image(255 * multiplier)
);
}
// Ensure image has alpha channel
if (!HasAlpha(image)) {
double const multiplier = sharp::Is16Bit(image.interpretation()) ? 256.0 : 1.0;
image = image.bandjoin(
VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
);
}
// Ensure overlay is premultiplied sRGB // Ensure overlay is premultiplied sRGB
overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB).premultiply(); overlayImage = overlayImage.colourspace(VIPS_INTERPRETATION_sRGB).premultiply();
if (baton->overlayXOffset >= 0 && baton->overlayYOffset >= 0) { if (baton->overlayXOffset >= 0 && baton->overlayYOffset >= 0) {

View File

@ -154,11 +154,20 @@ describe('Overlays', function() {
}); });
} }
it('Fail when overlay does not contain alpha channel', function(done) { it('Composite JPEG onto PNG', function(done) {
sharp(fixtures.inputPngOverlayLayer1) sharp(fixtures.inputPngOverlayLayer1)
.overlayWith(fixtures.inputJpg) .overlayWith(fixtures.inputJpgWithLandscapeExif1)
.toBuffer(function(error) { .toBuffer(function(error) {
assert.strictEqual(true, error instanceof Error); if (error) return done(error);
done();
});
});
it('Composite opaque JPEG onto JPEG', function(done) {
sharp(fixtures.inputJpg)
.overlayWith(fixtures.inputJpgWithLandscapeExif1)
.toBuffer(function(error) {
if (error) return done(error);
done(); done();
}); });
}); });