Ensure 16-bit input images work with embed option #325

This commit is contained in:
Lovell Fuller 2015-12-23 20:46:49 +00:00
parent fd5b4a131f
commit 61b86744d7
5 changed files with 34 additions and 15 deletions

View File

@ -2,6 +2,12 @@
### v0.12 - "*look*" ### v0.12 - "*look*"
#### v0.12.2 - TBD
* Ensure 16-bit input images work with embed option.
[#325](https://github.com/lovell/sharp/issues/325)
[@janaz](https://github.com/janaz)
#### v0.12.1 - 12<sup>th</sup> December 2015 #### v0.12.1 - 12<sup>th</sup> December 2015
* Allow use of SIMD vector instructions (via liborc) to be toggled on/off. * Allow use of SIMD vector instructions (via liborc) to be toggled on/off.

View File

@ -1,6 +1,6 @@
{ {
"name": "sharp", "name": "sharp",
"version": "0.12.1", "version": "0.12.2",
"author": "Lovell Fuller <npm@lovell.info>", "author": "Lovell Fuller <npm@lovell.info>",
"contributors": [ "contributors": [
"Pierre Inglebert <pierre.inglebert@gmail.com>", "Pierre Inglebert <pierre.inglebert@gmail.com>",

View File

@ -627,16 +627,6 @@ class PipelineWorker : public AsyncWorker {
// Crop/embed // Crop/embed
if (image->Xsize != baton->width || image->Ysize != baton->height) { if (image->Xsize != baton->width || image->Ysize != baton->height) {
if (baton->canvas == Canvas::EMBED) { if (baton->canvas == Canvas::EMBED) {
// Match background colour space, namely sRGB
if (image->Type != VIPS_INTERPRETATION_sRGB) {
// Convert to sRGB colour space
VipsImage *colourspaced;
if (vips_colourspace(image, &colourspaced, VIPS_INTERPRETATION_sRGB, nullptr)) {
return Error();
}
vips_object_local(hook, colourspaced);
image = colourspaced;
}
// 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)) {
// Create single-channel transparency // Create single-channel transparency
@ -661,13 +651,22 @@ class PipelineWorker : public AsyncWorker {
} }
// Create background // Create background
VipsArrayDouble *background; VipsArrayDouble *background;
// Scale up 8-bit values to match 16-bit input image
double multiplier = (image->Type == VIPS_INTERPRETATION_RGB16) ? 256.0 : 1.0;
if (baton->background[3] < 255.0 || HasAlpha(image)) { if (baton->background[3] < 255.0 || HasAlpha(image)) {
background = vips_array_double_newv( // RGBA
4, baton->background[0], baton->background[1], baton->background[2], baton->background[3] background = vips_array_double_newv(4,
baton->background[0] * multiplier,
baton->background[1] * multiplier,
baton->background[2] * multiplier,
baton->background[3] * multiplier
); );
} else { } else {
background = vips_array_double_newv( // RGB
3, baton->background[0], baton->background[1], baton->background[2] background = vips_array_double_newv(3,
baton->background[0] * multiplier,
baton->background[1] * multiplier,
baton->background[2] * multiplier
); );
} }
// Embed // Embed

BIN
test/fixtures/expected/embed-16bit.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 827 B

View File

@ -68,6 +68,20 @@ describe('Embed', function() {
}); });
}); });
it('16-bit PNG with alpha channel', function(done) {
sharp(fixtures.inputPngWithTransparency16bit)
.resize(32, 16)
.embed()
.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);
fixtures.assertSimilar(fixtures.expected('embed-16bit.png'), data, done);
});
});
it('Enlarge and embed', function(done) { it('Enlarge and embed', function(done) {
sharp(fixtures.inputPngWithOneColor) sharp(fixtures.inputPngWithOneColor)
.embed() .embed()