From c65de3fe6d5c31df50fb092bfc3b7c1fed8e52a8 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Thu, 17 Jun 2021 20:02:16 +0100 Subject: [PATCH] Default to single-channel output from extractChannel #2658 --- docs/api-channel.md | 17 +++++++++++------ docs/api-colour.md | 2 +- docs/changelog.md | 3 +++ lib/channel.js | 18 +++++++++++------- lib/colour.js | 2 +- .../fixtures/expected/extract-alpha-16bit.jpg | Bin 685 -> 0 bytes .../fixtures/expected/extract-alpha-16bit.png | Bin 0 -> 262 bytes test/unit/extractChannel.js | 9 +++++---- 8 files changed, 32 insertions(+), 19 deletions(-) delete mode 100644 test/fixtures/expected/extract-alpha-16bit.jpg create mode 100644 test/fixtures/expected/extract-alpha-16bit.png diff --git a/docs/api-channel.md b/docs/api-channel.md index a19ec454..4877ec3f 100644 --- a/docs/api-channel.md +++ b/docs/api-channel.md @@ -64,13 +64,18 @@ Extract a single channel from a multi-channel image. ### Examples ```javascript -sharp(input) +// green.jpg is a greyscale image containing the green channel of the input +await sharp(input) .extractChannel('green') - .toColourspace('b-w') - .toFile('green.jpg', function(err, info) { - // info.channels === 1 - // green.jpg is a greyscale image containing the green channel of the input - }); + .toFile('green.jpg'); +``` + +```javascript +// red1 is the red value of the first pixel, red2 the second pixel etc. +const [red1, red2, ...] = await sharp(input) + .extractChannel(0) + .raw() + .toBuffer(); ``` * Throws **[Error][3]** Invalid channel diff --git a/docs/api-colour.md b/docs/api-colour.md index c2818eda..667a664d 100644 --- a/docs/api-colour.md +++ b/docs/api-colour.md @@ -86,4 +86,4 @@ Returns **Sharp** [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean -[6]: https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568 +[6]: https://github.com/libvips/libvips/blob/3c0bfdf74ce1dc37a6429bed47fa76f16e2cd70a/libvips/iofuncs/enumtypes.c#L777-L794 diff --git a/docs/changelog.md b/docs/changelog.md index ee590bfe..50fdc2b6 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -17,6 +17,9 @@ Requires libvips v8.11.0 * Allow multiple platform-arch binaries in same `node_modules` installation tree. [#2575](https://github.com/lovell/sharp/issues/2575) +* Default to single-channel `b-w` space when `extractChannel` is used. + [#2658](https://github.com/lovell/sharp/issues/2658) + ## v0.28 - *bijou* Requires libvips v8.10.6 diff --git a/lib/channel.js b/lib/channel.js index a500d9a9..7c1cc801 100644 --- a/lib/channel.js +++ b/lib/channel.js @@ -72,13 +72,17 @@ function ensureAlpha (alpha) { * Extract a single channel from a multi-channel image. * * @example - * sharp(input) + * // green.jpg is a greyscale image containing the green channel of the input + * await sharp(input) * .extractChannel('green') - * .toColourspace('b-w') - * .toFile('green.jpg', function(err, info) { - * // info.channels === 1 - * // green.jpg is a greyscale image containing the green channel of the input - * }); + * .toFile('green.jpg'); + * + * @example + * // red1 is the red value of the first pixel, red2 the second pixel etc. + * const [red1, red2, ...] = await sharp(input) + * .extractChannel(0) + * .raw() + * .toBuffer(); * * @param {number|string} channel - zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`. * @returns {Sharp} @@ -94,7 +98,7 @@ function extractChannel (channel) { } else { throw is.invalidParameterError('channel', 'integer or one of: red, green, blue, alpha', channel); } - return this; + return this.toColourspace('b-w'); } /** diff --git a/lib/colour.js b/lib/colour.js index c4516c5a..b31234e9 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -64,7 +64,7 @@ function grayscale (grayscale) { * .toColourspace('rgb16') * .toFile('16-bpp.png') * - * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568) + * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/3c0bfdf74ce1dc37a6429bed47fa76f16e2cd70a/libvips/iofuncs/enumtypes.c#L777-L794) * @returns {Sharp} * @throws {Error} Invalid parameters */ diff --git a/test/fixtures/expected/extract-alpha-16bit.jpg b/test/fixtures/expected/extract-alpha-16bit.jpg deleted file mode 100644 index 58c02669bc6fe3a9b76b4cf202665094884e0fc4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 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 diff --git a/test/fixtures/expected/extract-alpha-16bit.png b/test/fixtures/expected/extract-alpha-16bit.png new file mode 100644 index 0000000000000000000000000000000000000000..bb8b47c259e939a999e6df49a11b4d92d643ebb7 GIT binary patch literal 262 zcmeAS@N?(olHy`uVBq!ia0vp^0wBx*Bp9q_EZ7UAI14-?iy0WWg+Z8+Vb&Z8px_Qq z7sn8ZsiFPeSx$}uuKxSqwJX#aaYjz$Vk^{kQc!4dn3JNiOY9*3jJ*~DEbdJTpX|Qy z-u36*IeAs}Dy~;e8~HMrmt1RM+GcJ0zbEe1)WW$x9UZ4XUh`Pz>|B-kt96*}Y_FJP zByYawd`Z@UsMIf$xO^MRpO#N}E&bycbI^PF54)~4d}N+-tId6$vv}!MY0Vw{>wh>e z3+-Zbwhz{e&tUfKamkfjCzh}}r|(H{)!wtsLWc_+rv4Rwd$3R8;=)!VkOw_o{an^L HB{Ts5Q`}