input.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*!
  2. Copyright 2013 Lovell Fuller and others.
  3. SPDX-License-Identifier: Apache-2.0
  4. */
  5. const is = require('./is');
  6. const sharp = require('./sharp');
  7. /**
  8. * Justification alignment
  9. * @member
  10. * @private
  11. */
  12. const align = {
  13. left: 'low',
  14. top: 'low',
  15. low: 'low',
  16. center: 'centre',
  17. centre: 'centre',
  18. right: 'high',
  19. bottom: 'high',
  20. high: 'high'
  21. };
  22. const inputStreamParameters = [
  23. // Limits and error handling
  24. 'failOn', 'limitInputPixels', 'unlimited',
  25. // Format-generic
  26. 'animated', 'autoOrient', 'density', 'ignoreIcc', 'page', 'pages', 'sequentialRead',
  27. // Format-specific
  28. 'jp2', 'openSlide', 'pdf', 'raw', 'svg', 'tiff',
  29. // Deprecated
  30. 'failOnError', 'openSlideLevel', 'pdfBackground', 'tiffSubifd'
  31. ];
  32. /**
  33. * Extract input options, if any, from an object.
  34. * @private
  35. */
  36. function _inputOptionsFromObject (obj) {
  37. const params = inputStreamParameters
  38. .filter(p => is.defined(obj[p]))
  39. .map(p => ([p, obj[p]]));
  40. return params.length
  41. ? Object.fromEntries(params)
  42. : undefined;
  43. }
  44. /**
  45. * Create Object containing input and input-related options.
  46. * @private
  47. */
  48. function _createInputDescriptor (input, inputOptions, containerOptions) {
  49. const inputDescriptor = {
  50. autoOrient: false,
  51. failOn: 'warning',
  52. limitInputPixels: 0x3FFF ** 2,
  53. ignoreIcc: false,
  54. unlimited: false,
  55. sequentialRead: true
  56. };
  57. if (is.string(input)) {
  58. // filesystem
  59. inputDescriptor.file = input;
  60. } else if (is.buffer(input)) {
  61. // Buffer
  62. if (input.length === 0) {
  63. throw Error('Input Buffer is empty');
  64. }
  65. inputDescriptor.buffer = input;
  66. } else if (is.arrayBuffer(input)) {
  67. if (input.byteLength === 0) {
  68. throw Error('Input bit Array is empty');
  69. }
  70. inputDescriptor.buffer = Buffer.from(input, 0, input.byteLength);
  71. } else if (is.typedArray(input)) {
  72. if (input.length === 0) {
  73. throw Error('Input Bit Array is empty');
  74. }
  75. inputDescriptor.buffer = Buffer.from(input.buffer, input.byteOffset, input.byteLength);
  76. } else if (is.plainObject(input) && !is.defined(inputOptions)) {
  77. // Plain Object descriptor, e.g. create
  78. inputOptions = input;
  79. if (_inputOptionsFromObject(inputOptions)) {
  80. // Stream with options
  81. inputDescriptor.buffer = [];
  82. }
  83. } else if (!is.defined(input) && !is.defined(inputOptions) && is.object(containerOptions) && containerOptions.allowStream) {
  84. // Stream without options
  85. inputDescriptor.buffer = [];
  86. } else if (Array.isArray(input)) {
  87. if (input.length > 1) {
  88. // Join images together
  89. if (!this.options.joining) {
  90. this.options.joining = true;
  91. this.options.join = input.map(i => this._createInputDescriptor(i));
  92. } else {
  93. throw new Error('Recursive join is unsupported');
  94. }
  95. } else {
  96. throw new Error('Expected at least two images to join');
  97. }
  98. } else {
  99. throw new Error(`Unsupported input '${input}' of type ${typeof input}${
  100. is.defined(inputOptions) ? ` when also providing options of type ${typeof inputOptions}` : ''
  101. }`);
  102. }
  103. if (is.object(inputOptions)) {
  104. // Deprecated: failOnError
  105. if (is.defined(inputOptions.failOnError)) {
  106. if (is.bool(inputOptions.failOnError)) {
  107. inputDescriptor.failOn = inputOptions.failOnError ? 'warning' : 'none';
  108. } else {
  109. throw is.invalidParameterError('failOnError', 'boolean', inputOptions.failOnError);
  110. }
  111. }
  112. // failOn
  113. if (is.defined(inputOptions.failOn)) {
  114. if (is.string(inputOptions.failOn) && is.inArray(inputOptions.failOn, ['none', 'truncated', 'error', 'warning'])) {
  115. inputDescriptor.failOn = inputOptions.failOn;
  116. } else {
  117. throw is.invalidParameterError('failOn', 'one of: none, truncated, error, warning', inputOptions.failOn);
  118. }
  119. }
  120. // autoOrient
  121. if (is.defined(inputOptions.autoOrient)) {
  122. if (is.bool(inputOptions.autoOrient)) {
  123. inputDescriptor.autoOrient = inputOptions.autoOrient;
  124. } else {
  125. throw is.invalidParameterError('autoOrient', 'boolean', inputOptions.autoOrient);
  126. }
  127. }
  128. // Density
  129. if (is.defined(inputOptions.density)) {
  130. if (is.inRange(inputOptions.density, 1, 100000)) {
  131. inputDescriptor.density = inputOptions.density;
  132. } else {
  133. throw is.invalidParameterError('density', 'number between 1 and 100000', inputOptions.density);
  134. }
  135. }
  136. // Ignore embeddded ICC profile
  137. if (is.defined(inputOptions.ignoreIcc)) {
  138. if (is.bool(inputOptions.ignoreIcc)) {
  139. inputDescriptor.ignoreIcc = inputOptions.ignoreIcc;
  140. } else {
  141. throw is.invalidParameterError('ignoreIcc', 'boolean', inputOptions.ignoreIcc);
  142. }
  143. }
  144. // limitInputPixels
  145. if (is.defined(inputOptions.limitInputPixels)) {
  146. if (is.bool(inputOptions.limitInputPixels)) {
  147. inputDescriptor.limitInputPixels = inputOptions.limitInputPixels
  148. ? 0x3FFF ** 2
  149. : 0;
  150. } else if (is.integer(inputOptions.limitInputPixels) && is.inRange(inputOptions.limitInputPixels, 0, Number.MAX_SAFE_INTEGER)) {
  151. inputDescriptor.limitInputPixels = inputOptions.limitInputPixels;
  152. } else {
  153. throw is.invalidParameterError('limitInputPixels', 'positive integer', inputOptions.limitInputPixels);
  154. }
  155. }
  156. // unlimited
  157. if (is.defined(inputOptions.unlimited)) {
  158. if (is.bool(inputOptions.unlimited)) {
  159. inputDescriptor.unlimited = inputOptions.unlimited;
  160. } else {
  161. throw is.invalidParameterError('unlimited', 'boolean', inputOptions.unlimited);
  162. }
  163. }
  164. // sequentialRead
  165. if (is.defined(inputOptions.sequentialRead)) {
  166. if (is.bool(inputOptions.sequentialRead)) {
  167. inputDescriptor.sequentialRead = inputOptions.sequentialRead;
  168. } else {
  169. throw is.invalidParameterError('sequentialRead', 'boolean', inputOptions.sequentialRead);
  170. }
  171. }
  172. // Raw pixel input
  173. if (is.defined(inputOptions.raw)) {
  174. if (
  175. is.object(inputOptions.raw) &&
  176. is.integer(inputOptions.raw.width) && inputOptions.raw.width > 0 &&
  177. is.integer(inputOptions.raw.height) && inputOptions.raw.height > 0 &&
  178. is.integer(inputOptions.raw.channels) && is.inRange(inputOptions.raw.channels, 1, 4)
  179. ) {
  180. inputDescriptor.rawWidth = inputOptions.raw.width;
  181. inputDescriptor.rawHeight = inputOptions.raw.height;
  182. inputDescriptor.rawChannels = inputOptions.raw.channels;
  183. switch (input.constructor) {
  184. case Uint8Array:
  185. case Uint8ClampedArray:
  186. inputDescriptor.rawDepth = 'uchar';
  187. break;
  188. case Int8Array:
  189. inputDescriptor.rawDepth = 'char';
  190. break;
  191. case Uint16Array:
  192. inputDescriptor.rawDepth = 'ushort';
  193. break;
  194. case Int16Array:
  195. inputDescriptor.rawDepth = 'short';
  196. break;
  197. case Uint32Array:
  198. inputDescriptor.rawDepth = 'uint';
  199. break;
  200. case Int32Array:
  201. inputDescriptor.rawDepth = 'int';
  202. break;
  203. case Float32Array:
  204. inputDescriptor.rawDepth = 'float';
  205. break;
  206. case Float64Array:
  207. inputDescriptor.rawDepth = 'double';
  208. break;
  209. default:
  210. inputDescriptor.rawDepth = 'uchar';
  211. break;
  212. }
  213. } else {
  214. throw new Error('Expected width, height and channels for raw pixel input');
  215. }
  216. inputDescriptor.rawPremultiplied = false;
  217. if (is.defined(inputOptions.raw.premultiplied)) {
  218. if (is.bool(inputOptions.raw.premultiplied)) {
  219. inputDescriptor.rawPremultiplied = inputOptions.raw.premultiplied;
  220. } else {
  221. throw is.invalidParameterError('raw.premultiplied', 'boolean', inputOptions.raw.premultiplied);
  222. }
  223. }
  224. inputDescriptor.rawPageHeight = 0;
  225. if (is.defined(inputOptions.raw.pageHeight)) {
  226. if (is.integer(inputOptions.raw.pageHeight) && inputOptions.raw.pageHeight > 0 && inputOptions.raw.pageHeight <= inputOptions.raw.height) {
  227. if (inputOptions.raw.height % inputOptions.raw.pageHeight !== 0) {
  228. throw new Error(`Expected raw.height ${inputOptions.raw.height} to be a multiple of raw.pageHeight ${inputOptions.raw.pageHeight}`);
  229. }
  230. inputDescriptor.rawPageHeight = inputOptions.raw.pageHeight;
  231. } else {
  232. throw is.invalidParameterError('raw.pageHeight', 'positive integer', inputOptions.raw.pageHeight);
  233. }
  234. }
  235. }
  236. // Multi-page input (GIF, TIFF, PDF)
  237. if (is.defined(inputOptions.animated)) {
  238. if (is.bool(inputOptions.animated)) {
  239. inputDescriptor.pages = inputOptions.animated ? -1 : 1;
  240. } else {
  241. throw is.invalidParameterError('animated', 'boolean', inputOptions.animated);
  242. }
  243. }
  244. if (is.defined(inputOptions.pages)) {
  245. if (is.integer(inputOptions.pages) && is.inRange(inputOptions.pages, -1, 100000)) {
  246. inputDescriptor.pages = inputOptions.pages;
  247. } else {
  248. throw is.invalidParameterError('pages', 'integer between -1 and 100000', inputOptions.pages);
  249. }
  250. }
  251. if (is.defined(inputOptions.page)) {
  252. if (is.integer(inputOptions.page) && is.inRange(inputOptions.page, 0, 100000)) {
  253. inputDescriptor.page = inputOptions.page;
  254. } else {
  255. throw is.invalidParameterError('page', 'integer between 0 and 100000', inputOptions.page);
  256. }
  257. }
  258. // OpenSlide specific options
  259. if (is.object(inputOptions.openSlide) && is.defined(inputOptions.openSlide.level)) {
  260. if (is.integer(inputOptions.openSlide.level) && is.inRange(inputOptions.openSlide.level, 0, 256)) {
  261. inputDescriptor.openSlideLevel = inputOptions.openSlide.level;
  262. } else {
  263. throw is.invalidParameterError('openSlide.level', 'integer between 0 and 256', inputOptions.openSlide.level);
  264. }
  265. } else if (is.defined(inputOptions.level)) {
  266. // Deprecated
  267. if (is.integer(inputOptions.level) && is.inRange(inputOptions.level, 0, 256)) {
  268. inputDescriptor.openSlideLevel = inputOptions.level;
  269. } else {
  270. throw is.invalidParameterError('level', 'integer between 0 and 256', inputOptions.level);
  271. }
  272. }
  273. // TIFF specific options
  274. if (is.object(inputOptions.tiff) && is.defined(inputOptions.tiff.subifd)) {
  275. if (is.integer(inputOptions.tiff.subifd) && is.inRange(inputOptions.tiff.subifd, -1, 100000)) {
  276. inputDescriptor.tiffSubifd = inputOptions.tiff.subifd;
  277. } else {
  278. throw is.invalidParameterError('tiff.subifd', 'integer between -1 and 100000', inputOptions.tiff.subifd);
  279. }
  280. } else if (is.defined(inputOptions.subifd)) {
  281. // Deprecated
  282. if (is.integer(inputOptions.subifd) && is.inRange(inputOptions.subifd, -1, 100000)) {
  283. inputDescriptor.tiffSubifd = inputOptions.subifd;
  284. } else {
  285. throw is.invalidParameterError('subifd', 'integer between -1 and 100000', inputOptions.subifd);
  286. }
  287. }
  288. // SVG specific options
  289. if (is.object(inputOptions.svg)) {
  290. if (is.defined(inputOptions.svg.stylesheet)) {
  291. if (is.string(inputOptions.svg.stylesheet)) {
  292. inputDescriptor.svgStylesheet = inputOptions.svg.stylesheet;
  293. } else {
  294. throw is.invalidParameterError('svg.stylesheet', 'string', inputOptions.svg.stylesheet);
  295. }
  296. }
  297. if (is.defined(inputOptions.svg.highBitdepth)) {
  298. if (is.bool(inputOptions.svg.highBitdepth)) {
  299. inputDescriptor.svgHighBitdepth = inputOptions.svg.highBitdepth;
  300. } else {
  301. throw is.invalidParameterError('svg.highBitdepth', 'boolean', inputOptions.svg.highBitdepth);
  302. }
  303. }
  304. }
  305. // PDF specific options
  306. if (is.object(inputOptions.pdf) && is.defined(inputOptions.pdf.background)) {
  307. inputDescriptor.pdfBackground = this._getBackgroundColourOption(inputOptions.pdf.background);
  308. } else if (is.defined(inputOptions.pdfBackground)) {
  309. // Deprecated
  310. inputDescriptor.pdfBackground = this._getBackgroundColourOption(inputOptions.pdfBackground);
  311. }
  312. // JPEG 2000 specific options
  313. if (is.object(inputOptions.jp2) && is.defined(inputOptions.jp2.oneshot)) {
  314. if (is.bool(inputOptions.jp2.oneshot)) {
  315. inputDescriptor.jp2Oneshot = inputOptions.jp2.oneshot;
  316. } else {
  317. throw is.invalidParameterError('jp2.oneshot', 'boolean', inputOptions.jp2.oneshot);
  318. }
  319. }
  320. // Create new image
  321. if (is.defined(inputOptions.create)) {
  322. if (
  323. is.object(inputOptions.create) &&
  324. is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
  325. is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
  326. is.integer(inputOptions.create.channels)
  327. ) {
  328. inputDescriptor.createWidth = inputOptions.create.width;
  329. inputDescriptor.createHeight = inputOptions.create.height;
  330. inputDescriptor.createChannels = inputOptions.create.channels;
  331. inputDescriptor.createPageHeight = 0;
  332. if (is.defined(inputOptions.create.pageHeight)) {
  333. if (is.integer(inputOptions.create.pageHeight) && inputOptions.create.pageHeight > 0 && inputOptions.create.pageHeight <= inputOptions.create.height) {
  334. if (inputOptions.create.height % inputOptions.create.pageHeight !== 0) {
  335. throw new Error(`Expected create.height ${inputOptions.create.height} to be a multiple of create.pageHeight ${inputOptions.create.pageHeight}`);
  336. }
  337. inputDescriptor.createPageHeight = inputOptions.create.pageHeight;
  338. } else {
  339. throw is.invalidParameterError('create.pageHeight', 'positive integer', inputOptions.create.pageHeight);
  340. }
  341. }
  342. // Noise
  343. if (is.defined(inputOptions.create.noise)) {
  344. if (!is.object(inputOptions.create.noise)) {
  345. throw new Error('Expected noise to be an object');
  346. }
  347. if (inputOptions.create.noise.type !== 'gaussian') {
  348. throw new Error('Only gaussian noise is supported at the moment');
  349. }
  350. inputDescriptor.createNoiseType = inputOptions.create.noise.type;
  351. if (!is.inRange(inputOptions.create.channels, 1, 4)) {
  352. throw is.invalidParameterError('create.channels', 'number between 1 and 4', inputOptions.create.channels);
  353. }
  354. inputDescriptor.createNoiseMean = 128;
  355. if (is.defined(inputOptions.create.noise.mean)) {
  356. if (is.number(inputOptions.create.noise.mean) && is.inRange(inputOptions.create.noise.mean, 0, 10000)) {
  357. inputDescriptor.createNoiseMean = inputOptions.create.noise.mean;
  358. } else {
  359. throw is.invalidParameterError('create.noise.mean', 'number between 0 and 10000', inputOptions.create.noise.mean);
  360. }
  361. }
  362. inputDescriptor.createNoiseSigma = 30;
  363. if (is.defined(inputOptions.create.noise.sigma)) {
  364. if (is.number(inputOptions.create.noise.sigma) && is.inRange(inputOptions.create.noise.sigma, 0, 10000)) {
  365. inputDescriptor.createNoiseSigma = inputOptions.create.noise.sigma;
  366. } else {
  367. throw is.invalidParameterError('create.noise.sigma', 'number between 0 and 10000', inputOptions.create.noise.sigma);
  368. }
  369. }
  370. } else if (is.defined(inputOptions.create.background)) {
  371. if (!is.inRange(inputOptions.create.channels, 3, 4)) {
  372. throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);
  373. }
  374. inputDescriptor.createBackground = this._getBackgroundColourOption(inputOptions.create.background);
  375. } else {
  376. throw new Error('Expected valid noise or background to create a new input image');
  377. }
  378. delete inputDescriptor.buffer;
  379. } else {
  380. throw new Error('Expected valid width, height and channels to create a new input image');
  381. }
  382. }
  383. // Create a new image with text
  384. if (is.defined(inputOptions.text)) {
  385. if (is.object(inputOptions.text) && is.string(inputOptions.text.text)) {
  386. inputDescriptor.textValue = inputOptions.text.text;
  387. if (is.defined(inputOptions.text.height) && is.defined(inputOptions.text.dpi)) {
  388. throw new Error('Expected only one of dpi or height');
  389. }
  390. if (is.defined(inputOptions.text.font)) {
  391. if (is.string(inputOptions.text.font)) {
  392. inputDescriptor.textFont = inputOptions.text.font;
  393. } else {
  394. throw is.invalidParameterError('text.font', 'string', inputOptions.text.font);
  395. }
  396. }
  397. if (is.defined(inputOptions.text.fontfile)) {
  398. if (is.string(inputOptions.text.fontfile)) {
  399. inputDescriptor.textFontfile = inputOptions.text.fontfile;
  400. } else {
  401. throw is.invalidParameterError('text.fontfile', 'string', inputOptions.text.fontfile);
  402. }
  403. }
  404. if (is.defined(inputOptions.text.width)) {
  405. if (is.integer(inputOptions.text.width) && inputOptions.text.width > 0) {
  406. inputDescriptor.textWidth = inputOptions.text.width;
  407. } else {
  408. throw is.invalidParameterError('text.width', 'positive integer', inputOptions.text.width);
  409. }
  410. }
  411. if (is.defined(inputOptions.text.height)) {
  412. if (is.integer(inputOptions.text.height) && inputOptions.text.height > 0) {
  413. inputDescriptor.textHeight = inputOptions.text.height;
  414. } else {
  415. throw is.invalidParameterError('text.height', 'positive integer', inputOptions.text.height);
  416. }
  417. }
  418. if (is.defined(inputOptions.text.align)) {
  419. if (is.string(inputOptions.text.align) && is.string(this.constructor.align[inputOptions.text.align])) {
  420. inputDescriptor.textAlign = this.constructor.align[inputOptions.text.align];
  421. } else {
  422. throw is.invalidParameterError('text.align', 'valid alignment', inputOptions.text.align);
  423. }
  424. }
  425. if (is.defined(inputOptions.text.justify)) {
  426. if (is.bool(inputOptions.text.justify)) {
  427. inputDescriptor.textJustify = inputOptions.text.justify;
  428. } else {
  429. throw is.invalidParameterError('text.justify', 'boolean', inputOptions.text.justify);
  430. }
  431. }
  432. if (is.defined(inputOptions.text.dpi)) {
  433. if (is.integer(inputOptions.text.dpi) && is.inRange(inputOptions.text.dpi, 1, 1000000)) {
  434. inputDescriptor.textDpi = inputOptions.text.dpi;
  435. } else {
  436. throw is.invalidParameterError('text.dpi', 'integer between 1 and 1000000', inputOptions.text.dpi);
  437. }
  438. }
  439. if (is.defined(inputOptions.text.rgba)) {
  440. if (is.bool(inputOptions.text.rgba)) {
  441. inputDescriptor.textRgba = inputOptions.text.rgba;
  442. } else {
  443. throw is.invalidParameterError('text.rgba', 'bool', inputOptions.text.rgba);
  444. }
  445. }
  446. if (is.defined(inputOptions.text.spacing)) {
  447. if (is.integer(inputOptions.text.spacing) && is.inRange(inputOptions.text.spacing, -1000000, 1000000)) {
  448. inputDescriptor.textSpacing = inputOptions.text.spacing;
  449. } else {
  450. throw is.invalidParameterError('text.spacing', 'integer between -1000000 and 1000000', inputOptions.text.spacing);
  451. }
  452. }
  453. if (is.defined(inputOptions.text.wrap)) {
  454. if (is.string(inputOptions.text.wrap) && is.inArray(inputOptions.text.wrap, ['word', 'char', 'word-char', 'none'])) {
  455. inputDescriptor.textWrap = inputOptions.text.wrap;
  456. } else {
  457. throw is.invalidParameterError('text.wrap', 'one of: word, char, word-char, none', inputOptions.text.wrap);
  458. }
  459. }
  460. delete inputDescriptor.buffer;
  461. } else {
  462. throw new Error('Expected a valid string to create an image with text.');
  463. }
  464. }
  465. // Join images together
  466. if (is.defined(inputOptions.join)) {
  467. if (is.defined(this.options.join)) {
  468. if (is.defined(inputOptions.join.animated)) {
  469. if (is.bool(inputOptions.join.animated)) {
  470. inputDescriptor.joinAnimated = inputOptions.join.animated;
  471. } else {
  472. throw is.invalidParameterError('join.animated', 'boolean', inputOptions.join.animated);
  473. }
  474. }
  475. if (is.defined(inputOptions.join.across)) {
  476. if (is.integer(inputOptions.join.across) && is.inRange(inputOptions.join.across, 1, 1000000)) {
  477. inputDescriptor.joinAcross = inputOptions.join.across;
  478. } else {
  479. throw is.invalidParameterError('join.across', 'integer between 1 and 100000', inputOptions.join.across);
  480. }
  481. }
  482. if (is.defined(inputOptions.join.shim)) {
  483. if (is.integer(inputOptions.join.shim) && is.inRange(inputOptions.join.shim, 0, 1000000)) {
  484. inputDescriptor.joinShim = inputOptions.join.shim;
  485. } else {
  486. throw is.invalidParameterError('join.shim', 'integer between 0 and 100000', inputOptions.join.shim);
  487. }
  488. }
  489. if (is.defined(inputOptions.join.background)) {
  490. inputDescriptor.joinBackground = this._getBackgroundColourOption(inputOptions.join.background);
  491. }
  492. if (is.defined(inputOptions.join.halign)) {
  493. if (is.string(inputOptions.join.halign) && is.string(this.constructor.align[inputOptions.join.halign])) {
  494. inputDescriptor.joinHalign = this.constructor.align[inputOptions.join.halign];
  495. } else {
  496. throw is.invalidParameterError('join.halign', 'valid alignment', inputOptions.join.halign);
  497. }
  498. }
  499. if (is.defined(inputOptions.join.valign)) {
  500. if (is.string(inputOptions.join.valign) && is.string(this.constructor.align[inputOptions.join.valign])) {
  501. inputDescriptor.joinValign = this.constructor.align[inputOptions.join.valign];
  502. } else {
  503. throw is.invalidParameterError('join.valign', 'valid alignment', inputOptions.join.valign);
  504. }
  505. }
  506. } else {
  507. throw new Error('Expected input to be an array of images to join');
  508. }
  509. }
  510. } else if (is.defined(inputOptions)) {
  511. throw new Error(`Invalid input options ${inputOptions}`);
  512. }
  513. return inputDescriptor;
  514. }
  515. /**
  516. * Handle incoming Buffer chunk on Writable Stream.
  517. * @private
  518. * @param {Buffer} chunk
  519. * @param {string} encoding - unused
  520. * @param {Function} callback
  521. */
  522. function _write (chunk, _encoding, callback) {
  523. if (Array.isArray(this.options.input.buffer)) {
  524. if (is.buffer(chunk)) {
  525. if (this.options.input.buffer.length === 0) {
  526. this.on('finish', () => {
  527. this.streamInFinished = true;
  528. });
  529. }
  530. this.options.input.buffer.push(chunk);
  531. callback();
  532. } else {
  533. callback(new Error('Non-Buffer data on Writable Stream'));
  534. }
  535. } else {
  536. callback(new Error('Unexpected data on Writable Stream'));
  537. }
  538. }
  539. /**
  540. * Flattens the array of chunks accumulated in input.buffer.
  541. * @private
  542. */
  543. function _flattenBufferIn () {
  544. if (this._isStreamInput()) {
  545. this.options.input.buffer = Buffer.concat(this.options.input.buffer);
  546. }
  547. }
  548. /**
  549. * Are we expecting Stream-based input?
  550. * @private
  551. * @returns {boolean}
  552. */
  553. function _isStreamInput () {
  554. return Array.isArray(this.options.input.buffer);
  555. }
  556. /**
  557. * Fast access to (uncached) image metadata without decoding any compressed pixel data.
  558. *
  559. * This is read from the header of the input image.
  560. * It does not take into consideration any operations to be applied to the output image,
  561. * such as resize or rotate.
  562. *
  563. * Dimensions in the response will respect the `page` and `pages` properties of the
  564. * {@link /api-constructor/ constructor parameters}.
  565. *
  566. * A `Promise` is returned when `callback` is not provided.
  567. *
  568. * - `format`: Name of decoder used to decompress image data e.g. `jpeg`, `png`, `webp`, `gif`, `svg`
  569. * - `size`: Total size of image in bytes, for Stream and Buffer input only
  570. * - `width`: Number of pixels wide (EXIF orientation is not taken into consideration, see example below)
  571. * - `height`: Number of pixels high (EXIF orientation is not taken into consideration, see example below)
  572. * - `space`: Name of colour space interpretation e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html)
  573. * - `channels`: Number of bands e.g. `3` for sRGB, `4` for CMYK
  574. * - `depth`: Name of pixel depth format e.g. `uchar`, `char`, `ushort`, `float` [...](https://www.libvips.org/API/current/enum.BandFormat.html)
  575. * - `density`: Number of pixels per inch (DPI), if present
  576. * - `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
  577. * - `isProgressive`: Boolean indicating whether the image is interlaced using a progressive scan
  578. * - `isPalette`: Boolean indicating whether the image is palette-based (GIF, PNG).
  579. * - `bitsPerSample`: Number of bits per sample for each channel (GIF, PNG, HEIF).
  580. * - `pages`: Number of pages/frames contained within the image, with support for TIFF, HEIF, PDF, animated GIF and animated WebP
  581. * - `pageHeight`: Number of pixels high each page in a multi-page image will be.
  582. * - `loop`: Number of times to loop an animated image, zero refers to a continuous loop.
  583. * - `delay`: Delay in ms between each page in an animated image, provided as an array of integers.
  584. * - `pagePrimary`: Number of the primary page in a HEIF image
  585. * - `levels`: Details of each level in a multi-level image provided as an array of objects, requires libvips compiled with support for OpenSlide
  586. * - `subifds`: Number of Sub Image File Directories in an OME-TIFF image
  587. * - `background`: Default background colour, if present, for PNG (bKGD) and GIF images
  588. * - `compression`: The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC)
  589. * - `resolutionUnit`: The unit of resolution (density), either `inch` or `cm`, if present
  590. * - `hasProfile`: Boolean indicating the presence of an embedded ICC profile
  591. * - `hasAlpha`: Boolean indicating the presence of an alpha transparency channel
  592. * - `orientation`: Number value of the EXIF Orientation header, if present
  593. * - `exif`: Buffer containing raw EXIF data, if present
  594. * - `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present
  595. * - `iptc`: Buffer containing raw IPTC data, if present
  596. * - `xmp`: Buffer containing raw XMP data, if present
  597. * - `xmpAsString`: String containing XMP data, if valid UTF-8.
  598. * - `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present
  599. * - `formatMagick`: String containing format for images loaded via *magick
  600. * - `comments`: Array of keyword/text pairs representing PNG text blocks, if present.
  601. *
  602. * @example
  603. * const metadata = await sharp(input).metadata();
  604. *
  605. * @example
  606. * const image = sharp(inputJpg);
  607. * image
  608. * .metadata()
  609. * .then(function(metadata) {
  610. * return image
  611. * .resize(Math.round(metadata.width / 2))
  612. * .webp()
  613. * .toBuffer();
  614. * })
  615. * .then(function(data) {
  616. * // data contains a WebP image half the width and height of the original JPEG
  617. * });
  618. *
  619. * @example
  620. * // Get dimensions taking EXIF Orientation into account.
  621. * const { autoOrient } = await sharp(input).metadata();
  622. * const { width, height } = autoOrient;
  623. *
  624. * @param {Function} [callback] - called with the arguments `(err, metadata)`
  625. * @returns {Promise<Object>|Sharp}
  626. */
  627. function metadata (callback) {
  628. const stack = Error();
  629. if (is.fn(callback)) {
  630. if (this._isStreamInput()) {
  631. this.on('finish', () => {
  632. this._flattenBufferIn();
  633. sharp.metadata(this.options, (err, metadata) => {
  634. if (err) {
  635. callback(is.nativeError(err, stack));
  636. } else {
  637. callback(null, metadata);
  638. }
  639. });
  640. });
  641. } else {
  642. sharp.metadata(this.options, (err, metadata) => {
  643. if (err) {
  644. callback(is.nativeError(err, stack));
  645. } else {
  646. callback(null, metadata);
  647. }
  648. });
  649. }
  650. return this;
  651. } else {
  652. if (this._isStreamInput()) {
  653. return new Promise((resolve, reject) => {
  654. const finished = () => {
  655. this._flattenBufferIn();
  656. sharp.metadata(this.options, (err, metadata) => {
  657. if (err) {
  658. reject(is.nativeError(err, stack));
  659. } else {
  660. resolve(metadata);
  661. }
  662. });
  663. };
  664. if (this.writableFinished) {
  665. finished();
  666. } else {
  667. this.once('finish', finished);
  668. }
  669. });
  670. } else {
  671. return new Promise((resolve, reject) => {
  672. sharp.metadata(this.options, (err, metadata) => {
  673. if (err) {
  674. reject(is.nativeError(err, stack));
  675. } else {
  676. resolve(metadata);
  677. }
  678. });
  679. });
  680. }
  681. }
  682. }
  683. /**
  684. * Access to pixel-derived image statistics for every channel in the image.
  685. * A `Promise` is returned when `callback` is not provided.
  686. *
  687. * - `channels`: Array of channel statistics for each channel in the image. Each channel statistic contains
  688. * - `min` (minimum value in the channel)
  689. * - `max` (maximum value in the channel)
  690. * - `sum` (sum of all values in a channel)
  691. * - `squaresSum` (sum of squared values in a channel)
  692. * - `mean` (mean of the values in a channel)
  693. * - `stdev` (standard deviation for the values in a channel)
  694. * - `minX` (x-coordinate of one of the pixel where the minimum lies)
  695. * - `minY` (y-coordinate of one of the pixel where the minimum lies)
  696. * - `maxX` (x-coordinate of one of the pixel where the maximum lies)
  697. * - `maxY` (y-coordinate of one of the pixel where the maximum lies)
  698. * - `isOpaque`: Is the image fully opaque? Will be `true` if the image has no alpha channel or if every pixel is fully opaque.
  699. * - `entropy`: Histogram-based estimation of greyscale entropy, discarding alpha channel if any.
  700. * - `sharpness`: Estimation of greyscale sharpness based on the standard deviation of a Laplacian convolution, discarding alpha channel if any.
  701. * - `dominant`: Object containing most dominant sRGB colour based on a 4096-bin 3D histogram.
  702. *
  703. * **Note**: Statistics are derived from the original input image. Any operations performed on the image must first be
  704. * written to a buffer in order to run `stats` on the result (see third example).
  705. *
  706. * @example
  707. * const image = sharp(inputJpg);
  708. * image
  709. * .stats()
  710. * .then(function(stats) {
  711. * // stats contains the channel-wise statistics array and the isOpaque value
  712. * });
  713. *
  714. * @example
  715. * const { entropy, sharpness, dominant } = await sharp(input).stats();
  716. * const { r, g, b } = dominant;
  717. *
  718. * @example
  719. * const image = sharp(input);
  720. * // store intermediate result
  721. * const part = await image.extract(region).toBuffer();
  722. * // create new instance to obtain statistics of extracted region
  723. * const stats = await sharp(part).stats();
  724. *
  725. * @param {Function} [callback] - called with the arguments `(err, stats)`
  726. * @returns {Promise<Object>}
  727. */
  728. function stats (callback) {
  729. const stack = Error();
  730. if (is.fn(callback)) {
  731. if (this._isStreamInput()) {
  732. this.on('finish', () => {
  733. this._flattenBufferIn();
  734. sharp.stats(this.options, (err, stats) => {
  735. if (err) {
  736. callback(is.nativeError(err, stack));
  737. } else {
  738. callback(null, stats);
  739. }
  740. });
  741. });
  742. } else {
  743. sharp.stats(this.options, (err, stats) => {
  744. if (err) {
  745. callback(is.nativeError(err, stack));
  746. } else {
  747. callback(null, stats);
  748. }
  749. });
  750. }
  751. return this;
  752. } else {
  753. if (this._isStreamInput()) {
  754. return new Promise((resolve, reject) => {
  755. this.on('finish', function () {
  756. this._flattenBufferIn();
  757. sharp.stats(this.options, (err, stats) => {
  758. if (err) {
  759. reject(is.nativeError(err, stack));
  760. } else {
  761. resolve(stats);
  762. }
  763. });
  764. });
  765. });
  766. } else {
  767. return new Promise((resolve, reject) => {
  768. sharp.stats(this.options, (err, stats) => {
  769. if (err) {
  770. reject(is.nativeError(err, stack));
  771. } else {
  772. resolve(stats);
  773. }
  774. });
  775. });
  776. }
  777. }
  778. }
  779. /**
  780. * Decorate the Sharp prototype with input-related functions.
  781. * @module Sharp
  782. * @private
  783. */
  784. module.exports = (Sharp) => {
  785. Object.assign(Sharp.prototype, {
  786. // Private
  787. _inputOptionsFromObject,
  788. _createInputDescriptor,
  789. _write,
  790. _flattenBufferIn,
  791. _isStreamInput,
  792. // Public
  793. metadata,
  794. stats
  795. });
  796. // Class attributes
  797. Sharp.align = align;
  798. };