diff --git a/docs/changelog.md b/docs/changelog.md index fd0c2d44..a0704ce9 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -12,6 +12,10 @@ Requires libvips v8.2.3 [@anandthakker](https://github.com/anandthakker) [@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 - 2nd April 2016 * Add ability to extend (pad) the edges of an image. diff --git a/src/pipeline.cc b/src/pipeline.cc index 43def594..35e23a49 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -496,11 +496,21 @@ class PipelineWorker : public AsyncWorker { // Scale up 8-bit values to match 16-bit input image double multiplier = (image.interpretation() == VIPS_INTERPRETATION_RGB16) ? 256.0 : 1.0; // Create background colour - std::vector background { - baton->background[0] * multiplier, - baton->background[1] * multiplier, - baton->background[2] * multiplier - }; + std::vector background; + if (image.bands() > 2) { + background = { + 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 if (baton->background[3] < 255.0 || HasAlpha(image)) { background.push_back(baton->background[3] * multiplier); diff --git a/test/fixtures/expected/embed-2channel.png b/test/fixtures/expected/embed-2channel.png new file mode 100644 index 00000000..ca6f220a Binary files /dev/null and b/test/fixtures/expected/embed-2channel.png differ diff --git a/test/unit/embed.js b/test/unit/embed.js index c459eb3f..32d91fad 100644 --- a/test/unit/embed.js +++ b/test/unit/embed.js @@ -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) { sharp(fixtures.inputPngWithOneColor) .embed() diff --git a/test/unit/gamma.js b/test/unit/gamma.js index 2779ad6e..252dce65 100644 --- a/test/unit/gamma.js +++ b/test/unit/gamma.js @@ -38,7 +38,7 @@ describe('Gamma correction', function() { assert.strictEqual('jpeg', info.format); assert.strictEqual(129, info.width); 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) { assert.strictEqual('png', info.format); 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); }); });