channel.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2013 Lovell Fuller and others.
  2. // SPDX-License-Identifier: Apache-2.0
  3. 'use strict';
  4. const is = require('./is');
  5. /**
  6. * Boolean operations for bandbool.
  7. * @private
  8. */
  9. const bool = {
  10. and: 'and',
  11. or: 'or',
  12. eor: 'eor'
  13. };
  14. /**
  15. * Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.
  16. *
  17. * See also {@link /api-operation#flatten|flatten}.
  18. *
  19. * @example
  20. * sharp('rgba.png')
  21. * .removeAlpha()
  22. * .toFile('rgb.png', function(err, info) {
  23. * // rgb.png is a 3 channel image without an alpha channel
  24. * });
  25. *
  26. * @returns {Sharp}
  27. */
  28. function removeAlpha () {
  29. this.options.removeAlpha = true;
  30. return this;
  31. }
  32. /**
  33. * Ensure the output image has an alpha transparency channel.
  34. * If missing, the added alpha channel will have the specified
  35. * transparency level, defaulting to fully-opaque (1).
  36. * This is a no-op if the image already has an alpha channel.
  37. *
  38. * @since 0.21.2
  39. *
  40. * @example
  41. * // rgba.png will be a 4 channel image with a fully-opaque alpha channel
  42. * await sharp('rgb.jpg')
  43. * .ensureAlpha()
  44. * .toFile('rgba.png')
  45. *
  46. * @example
  47. * // rgba is a 4 channel image with a fully-transparent alpha channel
  48. * const rgba = await sharp(rgb)
  49. * .ensureAlpha(0)
  50. * .toBuffer();
  51. *
  52. * @param {number} [alpha=1] - alpha transparency level (0=fully-transparent, 1=fully-opaque)
  53. * @returns {Sharp}
  54. * @throws {Error} Invalid alpha transparency level
  55. */
  56. function ensureAlpha (alpha) {
  57. if (is.defined(alpha)) {
  58. if (is.number(alpha) && is.inRange(alpha, 0, 1)) {
  59. this.options.ensureAlpha = alpha;
  60. } else {
  61. throw is.invalidParameterError('alpha', 'number between 0 and 1', alpha);
  62. }
  63. } else {
  64. this.options.ensureAlpha = 1;
  65. }
  66. return this;
  67. }
  68. /**
  69. * Extract a single channel from a multi-channel image.
  70. *
  71. * @example
  72. * // green.jpg is a greyscale image containing the green channel of the input
  73. * await sharp(input)
  74. * .extractChannel('green')
  75. * .toFile('green.jpg');
  76. *
  77. * @example
  78. * // red1 is the red value of the first pixel, red2 the second pixel etc.
  79. * const [red1, red2, ...] = await sharp(input)
  80. * .extractChannel(0)
  81. * .raw()
  82. * .toBuffer();
  83. *
  84. * @param {number|string} channel - zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`.
  85. * @returns {Sharp}
  86. * @throws {Error} Invalid channel
  87. */
  88. function extractChannel (channel) {
  89. const channelMap = { red: 0, green: 1, blue: 2, alpha: 3 };
  90. if (Object.keys(channelMap).includes(channel)) {
  91. channel = channelMap[channel];
  92. }
  93. if (is.integer(channel) && is.inRange(channel, 0, 4)) {
  94. this.options.extractChannel = channel;
  95. } else {
  96. throw is.invalidParameterError('channel', 'integer or one of: red, green, blue, alpha', channel);
  97. }
  98. return this;
  99. }
  100. /**
  101. * Join one or more channels to the image.
  102. * The meaning of the added channels depends on the output colourspace, set with `toColourspace()`.
  103. * By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
  104. * Channel ordering follows vips convention:
  105. * - sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
  106. * - CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
  107. *
  108. * Buffers may be any of the image formats supported by sharp.
  109. * For raw pixel input, the `options` object should contain a `raw` attribute, which follows the format of the attribute of the same name in the `sharp()` constructor.
  110. *
  111. * @param {Array<string|Buffer>|string|Buffer} images - one or more images (file paths, Buffers).
  112. * @param {Object} options - image options, see `sharp()` constructor.
  113. * @returns {Sharp}
  114. * @throws {Error} Invalid parameters
  115. */
  116. function joinChannel (images, options) {
  117. if (Array.isArray(images)) {
  118. images.forEach(function (image) {
  119. this.options.joinChannelIn.push(this._createInputDescriptor(image, options));
  120. }, this);
  121. } else {
  122. this.options.joinChannelIn.push(this._createInputDescriptor(images, options));
  123. }
  124. return this;
  125. }
  126. /**
  127. * Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
  128. *
  129. * @example
  130. * sharp('3-channel-rgb-input.png')
  131. * .bandbool(sharp.bool.and)
  132. * .toFile('1-channel-output.png', function (err, info) {
  133. * // The output will be a single channel image where each pixel `P = R & G & B`.
  134. * // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]`
  135. * // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`.
  136. * });
  137. *
  138. * @param {string} boolOp - one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
  139. * @returns {Sharp}
  140. * @throws {Error} Invalid parameters
  141. */
  142. function bandbool (boolOp) {
  143. if (is.string(boolOp) && is.inArray(boolOp, ['and', 'or', 'eor'])) {
  144. this.options.bandBoolOp = boolOp;
  145. } else {
  146. throw is.invalidParameterError('boolOp', 'one of: and, or, eor', boolOp);
  147. }
  148. return this;
  149. }
  150. /**
  151. * Decorate the Sharp prototype with channel-related functions.
  152. * @private
  153. */
  154. module.exports = function (Sharp) {
  155. Object.assign(Sharp.prototype, {
  156. // Public instance functions
  157. removeAlpha,
  158. ensureAlpha,
  159. extractChannel,
  160. joinChannel,
  161. bandbool
  162. });
  163. // Class attributes
  164. Sharp.bool = bool;
  165. };