input.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. 'use strict';
  2. const color = require('color');
  3. const is = require('./is');
  4. const sharp = require('../build/Release/sharp.node');
  5. /**
  6. * Extract input options, if any, from an object.
  7. * @private
  8. */
  9. function _inputOptionsFromObject (obj) {
  10. const { raw, density, limitInputPixels, sequentialRead, failOnError } = obj;
  11. return [raw, density, limitInputPixels, sequentialRead, failOnError].some(is.defined)
  12. ? { raw, density, limitInputPixels, sequentialRead, failOnError }
  13. : undefined;
  14. }
  15. /**
  16. * Create Object containing input and input-related options.
  17. * @private
  18. */
  19. function _createInputDescriptor (input, inputOptions, containerOptions) {
  20. const inputDescriptor = {
  21. failOnError: true,
  22. limitInputPixels: Math.pow(0x3FFF, 2),
  23. sequentialRead: false
  24. };
  25. if (is.string(input)) {
  26. // filesystem
  27. inputDescriptor.file = input;
  28. } else if (is.buffer(input)) {
  29. // Buffer
  30. inputDescriptor.buffer = input;
  31. } else if (is.plainObject(input) && !is.defined(inputOptions)) {
  32. // Plain Object descriptor, e.g. create
  33. inputOptions = input;
  34. if (_inputOptionsFromObject(inputOptions)) {
  35. // Stream with options
  36. inputDescriptor.buffer = [];
  37. }
  38. } else if (!is.defined(input) && !is.defined(inputOptions) && is.object(containerOptions) && containerOptions.allowStream) {
  39. // Stream without options
  40. inputDescriptor.buffer = [];
  41. } else {
  42. throw new Error(`Unsupported input '${input}' of type ${typeof input}${
  43. is.defined(inputOptions) ? ` when also providing options of type ${typeof inputOptions}` : ''
  44. }`);
  45. }
  46. if (is.object(inputOptions)) {
  47. // Fail on error
  48. if (is.defined(inputOptions.failOnError)) {
  49. if (is.bool(inputOptions.failOnError)) {
  50. inputDescriptor.failOnError = inputOptions.failOnError;
  51. } else {
  52. throw is.invalidParameterError('failOnError', 'boolean', inputOptions.failOnError);
  53. }
  54. }
  55. // Density
  56. if (is.defined(inputOptions.density)) {
  57. if (is.inRange(inputOptions.density, 1, 2400)) {
  58. inputDescriptor.density = inputOptions.density;
  59. } else {
  60. throw is.invalidParameterError('density', 'number between 1 and 2400', inputOptions.density);
  61. }
  62. }
  63. // limitInputPixels
  64. if (is.defined(inputOptions.limitInputPixels)) {
  65. if (is.bool(inputOptions.limitInputPixels)) {
  66. inputDescriptor.limitInputPixels = inputOptions.limitInputPixels
  67. ? Math.pow(0x3FFF, 2)
  68. : 0;
  69. } else if (is.integer(inputOptions.limitInputPixels) && inputOptions.limitInputPixels >= 0) {
  70. inputDescriptor.limitInputPixels = inputOptions.limitInputPixels;
  71. } else {
  72. throw is.invalidParameterError('limitInputPixels', 'integer >= 0', inputOptions.limitInputPixels);
  73. }
  74. }
  75. // sequentialRead
  76. if (is.defined(inputOptions.sequentialRead)) {
  77. if (is.bool(inputOptions.sequentialRead)) {
  78. inputDescriptor.sequentialRead = inputOptions.sequentialRead;
  79. } else {
  80. throw is.invalidParameterError('sequentialRead', 'boolean', inputOptions.sequentialRead);
  81. }
  82. }
  83. // Raw pixel input
  84. if (is.defined(inputOptions.raw)) {
  85. if (
  86. is.object(inputOptions.raw) &&
  87. is.integer(inputOptions.raw.width) && inputOptions.raw.width > 0 &&
  88. is.integer(inputOptions.raw.height) && inputOptions.raw.height > 0 &&
  89. is.integer(inputOptions.raw.channels) && is.inRange(inputOptions.raw.channels, 1, 4)
  90. ) {
  91. inputDescriptor.rawWidth = inputOptions.raw.width;
  92. inputDescriptor.rawHeight = inputOptions.raw.height;
  93. inputDescriptor.rawChannels = inputOptions.raw.channels;
  94. } else {
  95. throw new Error('Expected width, height and channels for raw pixel input');
  96. }
  97. }
  98. // Multi-page input (GIF, TIFF, PDF)
  99. if (is.defined(inputOptions.pages)) {
  100. if (is.integer(inputOptions.pages) && is.inRange(inputOptions.pages, -1, 100000)) {
  101. inputDescriptor.pages = inputOptions.pages;
  102. } else {
  103. throw is.invalidParameterError('pages', 'integer between -1 and 100000', inputOptions.pages);
  104. }
  105. }
  106. if (is.defined(inputOptions.page)) {
  107. if (is.integer(inputOptions.page) && is.inRange(inputOptions.page, 0, 100000)) {
  108. inputDescriptor.page = inputOptions.page;
  109. } else {
  110. throw is.invalidParameterError('page', 'integer between 0 and 100000', inputOptions.page);
  111. }
  112. }
  113. // Create new image
  114. if (is.defined(inputOptions.create)) {
  115. if (
  116. is.object(inputOptions.create) &&
  117. is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
  118. is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
  119. is.integer(inputOptions.create.channels) && is.inRange(inputOptions.create.channels, 3, 4) &&
  120. is.defined(inputOptions.create.background)
  121. ) {
  122. inputDescriptor.createWidth = inputOptions.create.width;
  123. inputDescriptor.createHeight = inputOptions.create.height;
  124. inputDescriptor.createChannels = inputOptions.create.channels;
  125. const background = color(inputOptions.create.background);
  126. inputDescriptor.createBackground = [
  127. background.red(),
  128. background.green(),
  129. background.blue(),
  130. Math.round(background.alpha() * 255)
  131. ];
  132. delete inputDescriptor.buffer;
  133. } else {
  134. throw new Error('Expected width, height, channels and background to create a new input image');
  135. }
  136. }
  137. } else if (is.defined(inputOptions)) {
  138. throw new Error('Invalid input options ' + inputOptions);
  139. }
  140. return inputDescriptor;
  141. }
  142. /**
  143. * Handle incoming Buffer chunk on Writable Stream.
  144. * @private
  145. * @param {Buffer} chunk
  146. * @param {string} encoding - unused
  147. * @param {Function} callback
  148. */
  149. function _write (chunk, encoding, callback) {
  150. /* istanbul ignore else */
  151. if (Array.isArray(this.options.input.buffer)) {
  152. /* istanbul ignore else */
  153. if (is.buffer(chunk)) {
  154. if (this.options.input.buffer.length === 0) {
  155. this.on('finish', () => {
  156. this.streamInFinished = true;
  157. });
  158. }
  159. this.options.input.buffer.push(chunk);
  160. callback();
  161. } else {
  162. callback(new Error('Non-Buffer data on Writable Stream'));
  163. }
  164. } else {
  165. callback(new Error('Unexpected data on Writable Stream'));
  166. }
  167. }
  168. /**
  169. * Flattens the array of chunks accumulated in input.buffer.
  170. * @private
  171. */
  172. function _flattenBufferIn () {
  173. if (this._isStreamInput()) {
  174. this.options.input.buffer = Buffer.concat(this.options.input.buffer);
  175. }
  176. }
  177. /**
  178. * Are we expecting Stream-based input?
  179. * @private
  180. * @returns {boolean}
  181. */
  182. function _isStreamInput () {
  183. return Array.isArray(this.options.input.buffer);
  184. }
  185. /**
  186. * Fast access to (uncached) image metadata without decoding any compressed image data.
  187. * A `Promise` is returned when `callback` is not provided.
  188. *
  189. * - `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
  190. * - `size`: Total size of image in bytes, for Stream and Buffer input only
  191. * - `width`: Number of pixels wide (EXIF orientation is not taken into consideration)
  192. * - `height`: Number of pixels high (EXIF orientation is not taken into consideration)
  193. * - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://libvips.github.io/libvips/API/current/VipsImage.html#VipsInterpretation)
  194. * - `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
  195. * - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://libvips.github.io/libvips/API/current/VipsImage.html#VipsBandFormat)
  196. * - `density`: Number of pixels per inch (DPI), if present
  197. * - `chromaSubsampling`: String containing JPEG chroma subsampling, `4:2:0` or `4:4:4` for RGB, `4:2:0:4` or `4:4:4:4` for CMYK
  198. * - `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan
  199. * - `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP
  200. * - `pageHeight`: Number of pixels high each page in a multi-page image will be.
  201. * - `loop`: Number of times to loop an animated image, zero refers to a continuous loop.
  202. * - `delay`: Delay in ms between each page in an animated image, provided as an array of integers.
  203. * - `pagePrimary`: Number of the primary page in a HEIF image
  204. * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
  205. * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
  206. * - `orientation`: Number value of the EXIF Orientation header, if present
  207. * - `exif`: Buffer containing raw EXIF data, if present
  208. * - `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present
  209. * - `iptc`: Buffer containing raw IPTC data, if present
  210. * - `xmp`: Buffer containing raw XMP data, if present
  211. * - `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present
  212. *
  213. * @example
  214. * const image = sharp(inputJpg);
  215. * image
  216. * .metadata()
  217. * .then(function(metadata) {
  218. * return image
  219. * .resize(Math.round(metadata.width / 2))
  220. * .webp()
  221. * .toBuffer();
  222. * })
  223. * .then(function(data) {
  224. * // data contains a WebP image half the width and height of the original JPEG
  225. * });
  226. *
  227. * @param {Function} [callback] - called with the arguments `(err, metadata)`
  228. * @returns {Promise<Object>|Sharp}
  229. */
  230. function metadata (callback) {
  231. if (is.fn(callback)) {
  232. if (this._isStreamInput()) {
  233. this.on('finish', () => {
  234. this._flattenBufferIn();
  235. sharp.metadata(this.options, callback);
  236. });
  237. } else {
  238. sharp.metadata(this.options, callback);
  239. }
  240. return this;
  241. } else {
  242. if (this._isStreamInput()) {
  243. return new Promise((resolve, reject) => {
  244. this.on('finish', () => {
  245. this._flattenBufferIn();
  246. sharp.metadata(this.options, (err, metadata) => {
  247. if (err) {
  248. reject(err);
  249. } else {
  250. resolve(metadata);
  251. }
  252. });
  253. });
  254. });
  255. } else {
  256. return new Promise((resolve, reject) => {
  257. sharp.metadata(this.options, (err, metadata) => {
  258. if (err) {
  259. reject(err);
  260. } else {
  261. resolve(metadata);
  262. }
  263. });
  264. });
  265. }
  266. }
  267. }
  268. /**
  269. * Access to pixel-derived image statistics for every channel in the image.
  270. * A `Promise` is returned when `callback` is not provided.
  271. *
  272. * - `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains
  273. * - `min` (minimum value in the channel)
  274. * - `max` (maximum value in the channel)
  275. * - `sum` (sum of all values in a channel)
  276. * - `squaresSum` (sum of squared values in a channel)
  277. * - `mean` (mean of the values in a channel)
  278. * - `stdev` (standard deviation for the values in a channel)
  279. * - `minX` (x-coordinate of one of the pixel where the minimum lies)
  280. * - `minY` (y-coordinate of one of the pixel where the minimum lies)
  281. * - `maxX` (x-coordinate of one of the pixel where the maximum lies)
  282. * - `maxY` (y-coordinate of one of the pixel where the maximum lies)
  283. * - `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque.
  284. * - `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any (experimental)
  285. *
  286. * @example
  287. * const image = sharp(inputJpg);
  288. * image
  289. * .stats()
  290. * .then(function(stats) {
  291. * // stats contains the channel-wise statistics array and the isOpaque value
  292. * });
  293. *
  294. * @param {Function} [callback] - called with the arguments `(err, stats)`
  295. * @returns {Promise<Object>}
  296. */
  297. function stats (callback) {
  298. if (is.fn(callback)) {
  299. if (this._isStreamInput()) {
  300. this.on('finish', () => {
  301. this._flattenBufferIn();
  302. sharp.stats(this.options, callback);
  303. });
  304. } else {
  305. sharp.stats(this.options, callback);
  306. }
  307. return this;
  308. } else {
  309. if (this._isStreamInput()) {
  310. return new Promise((resolve, reject) => {
  311. this.on('finish', function () {
  312. this._flattenBufferIn();
  313. sharp.stats(this.options, (err, stats) => {
  314. if (err) {
  315. reject(err);
  316. } else {
  317. resolve(stats);
  318. }
  319. });
  320. });
  321. });
  322. } else {
  323. return new Promise((resolve, reject) => {
  324. sharp.stats(this.options, (err, stats) => {
  325. if (err) {
  326. reject(err);
  327. } else {
  328. resolve(stats);
  329. }
  330. });
  331. });
  332. }
  333. }
  334. }
  335. /**
  336. * Decorate the Sharp prototype with input-related functions.
  337. * @private
  338. */
  339. module.exports = function (Sharp) {
  340. Object.assign(Sharp.prototype, {
  341. // Private
  342. _inputOptionsFromObject,
  343. _createInputDescriptor,
  344. _write,
  345. _flattenBufferIn,
  346. _isStreamInput,
  347. // Public
  348. metadata,
  349. stats
  350. });
  351. };