From d705cffdd60a2941d4c27a9b8e2e292ae8acf31e Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sun, 12 Aug 2018 20:22:39 +0100 Subject: [PATCH] Ensure extractChannel works with 16-bit images #1330 --- docs/changelog.md | 3 +++ src/pipeline.cc | 5 ++++- test/fixtures/expected/extract-alpha-16bit.jpg | Bin 0 -> 685 bytes test/unit/extractChannel.js | 11 +++++++++++ 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/expected/extract-alpha-16bit.jpg diff --git a/docs/changelog.md b/docs/changelog.md index 0a9988a2..e731b8db 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -26,6 +26,9 @@ Requires libvips v8.6.1. * Improve install time error messages for FreeBSD users. [#1310](https://github.com/lovell/sharp/issues/1310) +* Ensure extractChannel works with 16-bit images. + [#1330](https://github.com/lovell/sharp/issues/1330) + * Add experimental entropy field to stats response. #### v0.20.5 - 27th June 2018 diff --git a/src/pipeline.cc b/src/pipeline.cc index 1328dd89..5cad5f01 100644 --- a/src/pipeline.cc +++ b/src/pipeline.cc @@ -694,9 +694,12 @@ class PipelineWorker : public Nan::AsyncWorker { (baton->err).append("Cannot extract channel from image. Too few channels in image."); return Error(); } + VipsInterpretation const interpretation = sharp::Is16Bit(image.interpretation()) + ? VIPS_INTERPRETATION_GREY16 + : VIPS_INTERPRETATION_B_W; image = image .extract_band(baton->extractChannel) - .copy(VImage::option()->set("interpretation", VIPS_INTERPRETATION_B_W)); + .copy(VImage::option()->set("interpretation", interpretation)); } // Remove alpha channel, if any diff --git a/test/fixtures/expected/extract-alpha-16bit.jpg b/test/fixtures/expected/extract-alpha-16bit.jpg new file mode 100644 index 0000000000000000000000000000000000000000..58c02669bc6fe3a9b76b4cf202665094884e0fc4 GIT binary patch literal 685 zcmex=5A1R0qH8UG()kYHd00R{x%U}xiG1wXMa)SoDO!E93{B%*xB#772!Rak7RP@04JDQqw*8xQ)!A-JLhyrY_W-d{?g^;Oec+i*i|~ ztR@5*o!_7t&h_NU`lZTGEWh_Pm9l=;`}9S2?b7Xz;UYmX?*xzQu&k_H=$dZ9K8I6G z$m8n-&YuZw*@xx^J5BSxHe==`L&$;GDnUn{#+e6CIOVR;#K=;z{>}19hFAYe>lZ$|8Le|$arc@`8P#YN z&I1R0WHd@84oYu(tK(>YVf~rhc< s#Wa>D30b>eK8b&2mB0P{A7|Z9mUX|cF|gm@EdIN#|I}a6D*pdB0ic58kN^Mx literal 0 HcmV?d00001 diff --git a/test/unit/extractChannel.js b/test/unit/extractChannel.js index 2b599b7e..060bc968 100644 --- a/test/unit/extractChannel.js +++ b/test/unit/extractChannel.js @@ -69,6 +69,17 @@ describe('Image channel extraction', function () { }); }); + it('Alpha from 16-bit PNG', function (done) { + const output = fixtures.path('output.extract-alpha-16bit.jpg'); + sharp(fixtures.inputPngWithTransparency16bit) + .extractChannel(3) + .toFile(output, function (err, info) { + if (err) throw err; + fixtures.assertMaxColourDistance(output, fixtures.expected('extract-alpha-16bit.jpg')); + done(); + }); + }); + it('Invalid channel number', function () { assert.throws(function () { sharp(fixtures.inputJpg)