Allow use of embed with 1 and 2 channel images #411

This commit is contained in:
Lovell Fuller 2016-04-14 21:39:17 +01:00
parent 1b7c5816fc
commit e0d58266be
5 changed files with 37 additions and 7 deletions

View File

@ -12,6 +12,10 @@ Requires libvips v8.2.3
[@anandthakker](https://github.com/anandthakker) [@anandthakker](https://github.com/anandthakker)
[@kentongray](https://github.com/kentongray) [@kentongray](https://github.com/kentongray)
* Allow use of embed with 1 and 2 channel images.
[#411](https://github.com/lovell/sharp/issues/411)
[@janaz](https://github.com/janaz)
#### v0.14.0 - 2<sup>nd</sup> April 2016 #### v0.14.0 - 2<sup>nd</sup> April 2016
* Add ability to extend (pad) the edges of an image. * Add ability to extend (pad) the edges of an image.

View File

@ -496,11 +496,21 @@ class PipelineWorker : public AsyncWorker {
// Scale up 8-bit values to match 16-bit input image // Scale up 8-bit values to match 16-bit input image
double multiplier = (image.interpretation() == VIPS_INTERPRETATION_RGB16) ? 256.0 : 1.0; double multiplier = (image.interpretation() == VIPS_INTERPRETATION_RGB16) ? 256.0 : 1.0;
// Create background colour // Create background colour
std::vector<double> background { std::vector<double> background;
baton->background[0] * multiplier, if (image.bands() > 2) {
baton->background[1] * multiplier, background = {
baton->background[2] * multiplier multiplier * baton->background[0],
}; multiplier * baton->background[1],
multiplier * baton->background[2]
};
} else {
// Convert sRGB to greyscale
background = { multiplier * (
0.2126 * baton->background[0] +
0.7152 * baton->background[1] +
0.0722 * baton->background[2]
)};
}
// Add alpha channel to background colour // Add alpha channel to background colour
if (baton->background[3] < 255.0 || HasAlpha(image)) { if (baton->background[3] < 255.0 || HasAlpha(image)) {
background.push_back(baton->background[3] * multiplier); background.push_back(baton->background[3] * multiplier);

Binary file not shown.

After

Width:  |  Height:  |  Size: 703 B

View File

@ -88,6 +88,22 @@ describe('Embed', function() {
}); });
}); });
it('PNG with 2 channels', function(done) {
sharp(fixtures.inputPngWithGreyAlpha)
.resize(32, 16)
.embed()
.background({r: 0, g: 0, b: 0, a: 0})
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(true, data.length > 0);
assert.strictEqual('png', info.format);
assert.strictEqual(32, info.width);
assert.strictEqual(16, info.height);
assert.strictEqual(4, info.channels);
fixtures.assertSimilar(fixtures.expected('embed-2channel.png'), data, done);
});
});
it('Enlarge and embed', function(done) { it('Enlarge and embed', function(done) {
sharp(fixtures.inputPngWithOneColor) sharp(fixtures.inputPngWithOneColor)
.embed() .embed()

View File

@ -38,7 +38,7 @@ describe('Gamma correction', function() {
assert.strictEqual('jpeg', info.format); assert.strictEqual('jpeg', info.format);
assert.strictEqual(129, info.width); assert.strictEqual(129, info.width);
assert.strictEqual(111, info.height); assert.strictEqual(111, info.height);
fixtures.assertSimilar(fixtures.expected('gamma-3.0.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('gamma-3.0.jpg'), data, { threshold: 6 }, done);
}); });
}); });
@ -49,7 +49,7 @@ describe('Gamma correction', function() {
.toBuffer(function(err, data, info) { .toBuffer(function(err, data, info) {
assert.strictEqual('png', info.format); assert.strictEqual('png', info.format);
assert.strictEqual(320, info.width); assert.strictEqual(320, info.width);
fixtures.assertSimilar(fixtures.expected('gamma-alpha.jpg'), data, done); fixtures.assertSimilar(fixtures.expected('gamma-alpha.jpg'), data, { threshold: 11 }, done);
}); });
}); });