Fix embedding onto transparent backgrounds #366

Fully automate embed tests to prevent regression
This commit is contained in:
Lovell Fuller 2016-02-25 18:35:56 +00:00
parent 62e04f7784
commit df33c3024a
7 changed files with 26 additions and 32 deletions

View File

@ -2,6 +2,12 @@
### v0.13 - "*mind*" ### v0.13 - "*mind*"
#### v0.13.1 - TBD
* Fix embedding onto transparent backgrounds; regression introduced in v0.13.0.
[#366](https://github.com/lovell/sharp/issues/366)
[@diegocsandrim](https://github.com/diegocsandrim)
#### v0.13.0 - 15<sup>th</sup> February 2016 #### v0.13.0 - 15<sup>th</sup> February 2016
* Improve vector image support by allowing control of density/DPI. * Improve vector image support by allowing control of density/DPI.

View File

@ -585,9 +585,9 @@ class PipelineWorker : public AsyncWorker {
} }
// Add non-transparent alpha channel, if required // Add non-transparent alpha channel, if required
if (baton->background[3] < 255.0 && !HasAlpha(image)) { if (baton->background[3] < 255.0 && !HasAlpha(image)) {
VImage alpha = VImage::new_matrix(image.width(), image.height()) image = image.bandjoin(
.new_from_image(baton->background[3] * multiplier); VImage::new_matrix(image.width(), image.height()).new_from_image(255 * multiplier)
image = image.bandjoin(alpha); );
} }
// Embed // Embed
int left = static_cast<int>(round((baton->width - image.width()) / 2)); int left = static_cast<int>(round((baton->width - image.width()) / 2));

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 B

View File

@ -60,31 +60,25 @@ describe('Alpha transparency', function() {
it('Do not flatten', function(done) { it('Do not flatten', function(done) {
sharp(fixtures.inputPngWithTransparency) sharp(fixtures.inputPngWithTransparency)
.flatten(false) .flatten(false)
.toBuffer(function(err, data) { .toBuffer(function(err, data, info) {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function(err, metadata) { assert.strictEqual('png', info.format);
if (err) throw err; assert.strictEqual(4, info.channels);
assert.strictEqual('png', metadata.format);
assert.strictEqual(4, metadata.channels);
done(); done();
}); });
}); });
});
it('Ignored for JPEG', function(done) { it('Ignored for JPEG', function(done) {
sharp(fixtures.inputJpg) sharp(fixtures.inputJpg)
.background('#ff0000') .background('#ff0000')
.flatten() .flatten()
.toBuffer(function(err, data) { .toBuffer(function(err, data, info) {
if (err) throw err; if (err) throw err;
sharp(data).metadata(function(err, metadata) { assert.strictEqual('jpeg', info.format);
if (err) throw err; assert.strictEqual(3, info.channels);
assert.strictEqual('jpeg', metadata.format);
assert.strictEqual(3, metadata.channels);
done(); done();
}); });
}); });
});
it('Enlargement with non-nearest neighbor interpolation shouldnt cause dark edges', function(done) { it('Enlargement with non-nearest neighbor interpolation shouldnt cause dark edges', function(done) {
var BASE_NAME = 'alpha-premultiply-enlargement-2048x1536-paper.png'; var BASE_NAME = 'alpha-premultiply-enlargement-2048x1536-paper.png';

View File

@ -18,11 +18,8 @@ describe('Embed', function() {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function(err, metadata) { assert.strictEqual(3, info.channels);
if (err) throw err; fixtures.assertSimilar(fixtures.expected('embed-3-into-3.png'), data, done);
assert.strictEqual(3, metadata.channels);
done();
});
}); });
}); });
@ -39,11 +36,8 @@ describe('Embed', function() {
assert.strictEqual('webp', info.format); assert.strictEqual('webp', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
sharp(data).metadata(function(err, metadata) { assert.strictEqual(4, info.channels);
if (err) throw err; fixtures.assertSimilar(fixtures.expected('embed-3-into-4.webp'), data, done);
assert.strictEqual(4, metadata.channels);
done();
});
}); });
}); });
} }
@ -58,11 +52,8 @@ describe('Embed', function() {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(50, info.width); assert.strictEqual(50, info.width);
assert.strictEqual(50, info.height); assert.strictEqual(50, info.height);
sharp(data).metadata(function(err, metadata) { assert.strictEqual(4, info.channels);
if (err) throw err; fixtures.assertSimilar(fixtures.expected('embed-4-into-4.png'), data, done);
assert.strictEqual(4, metadata.channels);
done();
});
}); });
}); });
@ -76,6 +67,7 @@ describe('Embed', function() {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(32, info.width); assert.strictEqual(32, info.width);
assert.strictEqual(16, info.height); assert.strictEqual(16, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('embed-16bit.png'), data, done); fixtures.assertSimilar(fixtures.expected('embed-16bit.png'), data, done);
}); });
}); });
@ -91,6 +83,7 @@ describe('Embed', function() {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(32, info.width); assert.strictEqual(32, info.width);
assert.strictEqual(16, info.height); assert.strictEqual(16, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('embed-16bit-rgba.png'), data, done); fixtures.assertSimilar(fixtures.expected('embed-16bit-rgba.png'), data, done);
}); });
}); });
@ -105,6 +98,7 @@ describe('Embed', function() {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
assert.strictEqual(240, info.height); assert.strictEqual(240, info.height);
assert.strictEqual(3, info.channels);
fixtures.assertSimilar(fixtures.expected('embed-enlarge.png'), data, done); fixtures.assertSimilar(fixtures.expected('embed-enlarge.png'), data, done);
}); });
}); });