From e0d58266be05da72c1072c5c7048637113b172c0 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 14 Apr 2016 21:39:17 +0100 Subject: [PATCH] Allow use of embed with 1 and 2 channel images #411 --- docs/changelog.md | 4 ++++ src/pipeline.cc | 20 +++++++++++++++----- test/fixtures/expected/embed-2channel.png | Bin 0 -> 703 bytes test/unit/embed.js | 16 ++++++++++++++++ test/unit/gamma.js | 4 ++-- 5 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 test/fixtures/expected/embed-2channel.png 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 0000000000000000000000000000000000000000..ca6f220aef4133bf2f5c2e7b67af6eb5d9668291 GIT binary patch literal 703 zcmV;w0zmzVP)l9R(>R zA0UFzAyDY7TOFiBhc1O`gNw97hh%W7EhT~mY7z_W)7XS0@98r!4Uc=zy{AKcAw!MU z=+GY=IB@R$|Ia!10{mx$0pKP8gifj7IH_McER{+P$H&Jyo6Y`gwOVv|cu216lH)j3uh*&3 zXwb^a%8RkFG2TnOXAuBke0=-?0DzQ|q?90p2!bIZ8yy`T_I;la5fBlAGwpU8i0J7J=YU3G&E#KqtQpEX>vpar4)2GqLc!y^`D72 zC&MsUy;CGyf%WzE9}^Q3!-Io^F|9R8DH$SmR?RsF&N&!{0ijR`B9RE2 znVE@av)SBP+Gjq&32JI;DzUx2{Y6U2lu`fy#25q9G{H1Y2#3RDSr%B9#b#$`lWS{h z>2qrT6Oq-`)pxdSQ>j#<{r!C^m&;VGR<+|ew70h>=I7_1UWIWt*O!- 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); }); });