Alpha compositing: support grey+alpha src and non-alpha dst

This commit is contained in:
Lovell Fuller
2015-06-02 14:34:23 +01:00
parent 36be0453dd
commit 1091be374e
10 changed files with 133 additions and 90 deletions

BIN
test/fixtures/5_webp_a.webp vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 230 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@@ -62,6 +62,7 @@ module.exports = {
inputPngAlphaPremultiplicationLarge: getPath('alpha-premultiply-2048x1536-paper.png'),
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
inputTiff: getPath('G31D.TIF'), // http://www.fileformat.info/format/tiff/sample/e6c9a6e5253348f4aef6d17b534360ab/index.htm
inputGif: getPath('Crash_test.gif'), // http://upload.wikimedia.org/wikipedia/commons/e/e3/Crash_test.gif
inputSvg: getPath('Wikimedia-logo.svg'), // http://commons.wikimedia.org/wiki/File:Wikimedia-logo.svg

View File

@@ -48,7 +48,7 @@ describe('Overlays', function() {
sharp(fixtures.inputPngOverlayLayer0)
.overlayWith(fixtures.inputPngOverlayLayer1)
.toBuffer(function (error, data, info) {
.toBuffer(function (error, data) {
if (error) return done(error);
sharp(data)
.overlayWith(fixtures.inputPngOverlayLayer2)
@@ -65,7 +65,7 @@ describe('Overlays', function() {
sharp(fixtures.inputPngOverlayLayer1)
.overlayWith(fixtures.inputPngOverlayLayer2)
.toFile(paths.actual, function (error, data, info) {
.toFile(paths.actual, function (error) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected);
done();
@@ -77,7 +77,7 @@ describe('Overlays', function() {
sharp(fixtures.inputPngOverlayLayer1LowAlpha)
.overlayWith(fixtures.inputPngOverlayLayer2LowAlpha)
.toFile(paths.actual, function (error, data, info) {
.toFile(paths.actual, function (error) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected, 2);
done();
@@ -89,12 +89,12 @@ describe('Overlays', function() {
sharp(fixtures.inputPngOverlayLayer0)
.overlayWith(fixtures.inputPngOverlayLayer1LowAlpha)
.toBuffer(function (error, data, info) {
.toBuffer(function (error, data) {
if (error) return done(error);
sharp(data)
.overlayWith(fixtures.inputPngOverlayLayer2LowAlpha)
.toFile(paths.actual, function (error, data, info) {
.toFile(paths.actual, function (error) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected);
done();
@@ -102,14 +102,63 @@ describe('Overlays', function() {
});
});
it('Composite transparent PNG onto JPEG', function(done) {
it('Composite rgb+alpha PNG onto JPEG', function(done) {
var paths = getPaths('overlay-jpeg-with-rgb', 'jpg');
sharp(fixtures.inputJpg)
.resize(2048, 1536)
.overlayWith(fixtures.inputPngOverlayLayer1)
.toBuffer(function (error) {
.toFile(paths.actual, function(error, info) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected, 102);
done();
});
});
it('Composite greyscale+alpha PNG onto JPEG', function(done) {
var paths = getPaths('overlay-jpeg-with-greyscale', 'jpg');
sharp(fixtures.inputJpg)
.resize(400, 300)
.overlayWith(fixtures.inputPngWithGreyAlpha)
.toFile(paths.actual, function(error, info) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected, 102);
done();
});
});
if (sharp.format.webp.input.file) {
it('Composite WebP onto JPEG', function(done) {
var paths = getPaths('overlay-jpeg-with-webp', 'jpg');
sharp(fixtures.inputJpg)
.resize(300, 300)
.overlayWith(fixtures.inputWebPWithTransparency)
.toFile(paths.actual, function(error, info) {
if (error) return done(error);
fixtures.assertMaxColourDistance(paths.actual, paths.expected, 102);
done();
});
});
}
it('Fail when compositing images with different dimensions', function(done) {
sharp(fixtures.inputJpg)
.overlayWith(fixtures.inputPngWithGreyAlpha)
.toBuffer(function(error) {
console.dir(error);
assert.strictEqual(true, error instanceof Error);
done();
});
});
it('Fail when compositing non-PNG image', function(done) {
sharp(fixtures.inputPngOverlayLayer1)
.overlayWith(fixtures.inputJpg)
.toBuffer(function(error) {
console.dir(error);
assert.strictEqual(true, error instanceof Error);
if (error.message !== 'Input image must have an alpha channel') {
return done(new Error('Unexpected error: ' + error.message));
}
done();
});
});