@basemaps/lambda-tiler 7.12.0 → 7.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/build/routes/__tests__/tile.style.json.test.js +3 -1
  3. package/build/routes/__tests__/tile.style.json.test.js.map +1 -1
  4. package/build/routes/tile.style.json.d.ts +4 -0
  5. package/build/routes/tile.style.json.js +1 -1
  6. package/build/routes/tile.style.json.js.map +1 -1
  7. package/build/util/__test__/nztm.style.test.js +16 -7
  8. package/build/util/__test__/nztm.style.test.js.map +1 -1
  9. package/build/util/nztm.style.js +0 -3
  10. package/build/util/nztm.style.js.map +1 -1
  11. package/dist/index.js +86535 -0
  12. package/dist/node_modules/.package-lock.json +179 -0
  13. package/dist/node_modules/@img/sharp-libvips-linux-arm64/README.md +47 -0
  14. package/dist/node_modules/@img/sharp-libvips-linux-arm64/lib/glib-2.0/include/glibconfig.h +219 -0
  15. package/dist/node_modules/@img/sharp-libvips-linux-arm64/lib/index.js +1 -0
  16. package/dist/node_modules/@img/sharp-libvips-linux-arm64/lib/libvips-cpp.so.42 +0 -0
  17. package/dist/node_modules/@img/sharp-libvips-linux-arm64/package.json +45 -0
  18. package/dist/node_modules/@img/sharp-libvips-linux-arm64/versions.json +31 -0
  19. package/dist/node_modules/@img/sharp-linux-arm64/LICENSE +191 -0
  20. package/dist/node_modules/@img/sharp-linux-arm64/README.md +18 -0
  21. package/dist/node_modules/@img/sharp-linux-arm64/lib/sharp-linux-arm64.node +0 -0
  22. package/dist/node_modules/@img/sharp-linux-arm64/package.json +47 -0
  23. package/dist/node_modules/color/LICENSE +21 -0
  24. package/dist/node_modules/color/README.md +123 -0
  25. package/dist/node_modules/color/index.js +496 -0
  26. package/dist/node_modules/color/package.json +47 -0
  27. package/dist/node_modules/color-convert/CHANGELOG.md +54 -0
  28. package/dist/node_modules/color-convert/LICENSE +21 -0
  29. package/dist/node_modules/color-convert/README.md +68 -0
  30. package/dist/node_modules/color-convert/conversions.js +839 -0
  31. package/dist/node_modules/color-convert/index.js +81 -0
  32. package/dist/node_modules/color-convert/package.json +48 -0
  33. package/dist/node_modules/color-convert/route.js +97 -0
  34. package/dist/node_modules/color-name/LICENSE +8 -0
  35. package/dist/node_modules/color-name/README.md +11 -0
  36. package/dist/node_modules/color-name/index.js +152 -0
  37. package/dist/node_modules/color-name/package.json +28 -0
  38. package/dist/node_modules/color-string/LICENSE +21 -0
  39. package/dist/node_modules/color-string/README.md +62 -0
  40. package/dist/node_modules/color-string/index.js +242 -0
  41. package/dist/node_modules/color-string/package.json +39 -0
  42. package/dist/node_modules/detect-libc/LICENSE +201 -0
  43. package/dist/node_modules/detect-libc/README.md +163 -0
  44. package/dist/node_modules/detect-libc/index.d.ts +14 -0
  45. package/dist/node_modules/detect-libc/lib/detect-libc.js +267 -0
  46. package/dist/node_modules/detect-libc/lib/filesystem.js +41 -0
  47. package/dist/node_modules/detect-libc/lib/process.js +24 -0
  48. package/dist/node_modules/detect-libc/package.json +40 -0
  49. package/dist/node_modules/is-arrayish/LICENSE +21 -0
  50. package/dist/node_modules/is-arrayish/README.md +16 -0
  51. package/dist/node_modules/is-arrayish/index.js +9 -0
  52. package/dist/node_modules/is-arrayish/package.json +45 -0
  53. package/dist/node_modules/is-arrayish/yarn-error.log +1443 -0
  54. package/dist/node_modules/lerc/CHANGELOG.md +69 -0
  55. package/dist/node_modules/lerc/LercDecode.d.ts +61 -0
  56. package/dist/node_modules/lerc/LercDecode.es.d.ts +61 -0
  57. package/dist/node_modules/lerc/LercDecode.es.js +434 -0
  58. package/dist/node_modules/lerc/LercDecode.es.min.js +17 -0
  59. package/dist/node_modules/lerc/LercDecode.js +448 -0
  60. package/dist/node_modules/lerc/LercDecode.min.js +17 -0
  61. package/dist/node_modules/lerc/README.md +123 -0
  62. package/dist/node_modules/lerc/lerc-wasm.wasm +0 -0
  63. package/dist/node_modules/lerc/package.json +30 -0
  64. package/dist/node_modules/semver/LICENSE +15 -0
  65. package/dist/node_modules/semver/README.md +654 -0
  66. package/dist/node_modules/semver/bin/semver.js +188 -0
  67. package/dist/node_modules/semver/classes/comparator.js +141 -0
  68. package/dist/node_modules/semver/classes/index.js +5 -0
  69. package/dist/node_modules/semver/classes/range.js +554 -0
  70. package/dist/node_modules/semver/classes/semver.js +302 -0
  71. package/dist/node_modules/semver/functions/clean.js +6 -0
  72. package/dist/node_modules/semver/functions/cmp.js +52 -0
  73. package/dist/node_modules/semver/functions/coerce.js +60 -0
  74. package/dist/node_modules/semver/functions/compare-build.js +7 -0
  75. package/dist/node_modules/semver/functions/compare-loose.js +3 -0
  76. package/dist/node_modules/semver/functions/compare.js +5 -0
  77. package/dist/node_modules/semver/functions/diff.js +65 -0
  78. package/dist/node_modules/semver/functions/eq.js +3 -0
  79. package/dist/node_modules/semver/functions/gt.js +3 -0
  80. package/dist/node_modules/semver/functions/gte.js +3 -0
  81. package/dist/node_modules/semver/functions/inc.js +19 -0
  82. package/dist/node_modules/semver/functions/lt.js +3 -0
  83. package/dist/node_modules/semver/functions/lte.js +3 -0
  84. package/dist/node_modules/semver/functions/major.js +3 -0
  85. package/dist/node_modules/semver/functions/minor.js +3 -0
  86. package/dist/node_modules/semver/functions/neq.js +3 -0
  87. package/dist/node_modules/semver/functions/parse.js +16 -0
  88. package/dist/node_modules/semver/functions/patch.js +3 -0
  89. package/dist/node_modules/semver/functions/prerelease.js +6 -0
  90. package/dist/node_modules/semver/functions/rcompare.js +3 -0
  91. package/dist/node_modules/semver/functions/rsort.js +3 -0
  92. package/dist/node_modules/semver/functions/satisfies.js +10 -0
  93. package/dist/node_modules/semver/functions/sort.js +3 -0
  94. package/dist/node_modules/semver/functions/valid.js +6 -0
  95. package/dist/node_modules/semver/index.js +89 -0
  96. package/dist/node_modules/semver/internal/constants.js +35 -0
  97. package/dist/node_modules/semver/internal/debug.js +9 -0
  98. package/dist/node_modules/semver/internal/identifiers.js +23 -0
  99. package/dist/node_modules/semver/internal/lrucache.js +40 -0
  100. package/dist/node_modules/semver/internal/parse-options.js +15 -0
  101. package/dist/node_modules/semver/internal/re.js +217 -0
  102. package/dist/node_modules/semver/package.json +77 -0
  103. package/dist/node_modules/semver/preload.js +2 -0
  104. package/dist/node_modules/semver/range.bnf +16 -0
  105. package/dist/node_modules/semver/ranges/gtr.js +4 -0
  106. package/dist/node_modules/semver/ranges/intersects.js +7 -0
  107. package/dist/node_modules/semver/ranges/ltr.js +4 -0
  108. package/dist/node_modules/semver/ranges/max-satisfying.js +25 -0
  109. package/dist/node_modules/semver/ranges/min-satisfying.js +24 -0
  110. package/dist/node_modules/semver/ranges/min-version.js +61 -0
  111. package/dist/node_modules/semver/ranges/outside.js +80 -0
  112. package/dist/node_modules/semver/ranges/simplify.js +47 -0
  113. package/dist/node_modules/semver/ranges/subset.js +247 -0
  114. package/dist/node_modules/semver/ranges/to-comparators.js +8 -0
  115. package/dist/node_modules/semver/ranges/valid.js +11 -0
  116. package/dist/node_modules/sharp/LICENSE +191 -0
  117. package/dist/node_modules/sharp/README.md +118 -0
  118. package/dist/node_modules/sharp/install/check.js +36 -0
  119. package/dist/node_modules/sharp/lib/channel.js +174 -0
  120. package/dist/node_modules/sharp/lib/colour.js +182 -0
  121. package/dist/node_modules/sharp/lib/composite.js +210 -0
  122. package/dist/node_modules/sharp/lib/constructor.js +444 -0
  123. package/dist/node_modules/sharp/lib/index.d.ts +1717 -0
  124. package/dist/node_modules/sharp/lib/index.js +16 -0
  125. package/dist/node_modules/sharp/lib/input.js +657 -0
  126. package/dist/node_modules/sharp/lib/is.js +169 -0
  127. package/dist/node_modules/sharp/lib/libvips.js +171 -0
  128. package/dist/node_modules/sharp/lib/operation.js +919 -0
  129. package/dist/node_modules/sharp/lib/output.js +1561 -0
  130. package/dist/node_modules/sharp/lib/resize.js +582 -0
  131. package/dist/node_modules/sharp/lib/sharp.js +86 -0
  132. package/dist/node_modules/sharp/lib/utility.js +287 -0
  133. package/dist/node_modules/sharp/package.json +219 -0
  134. package/dist/node_modules/sharp/src/binding.gyp +277 -0
  135. package/dist/node_modules/sharp/src/common.cc +1090 -0
  136. package/dist/node_modules/sharp/src/common.h +393 -0
  137. package/dist/node_modules/sharp/src/metadata.cc +287 -0
  138. package/dist/node_modules/sharp/src/metadata.h +82 -0
  139. package/dist/node_modules/sharp/src/operations.cc +471 -0
  140. package/dist/node_modules/sharp/src/operations.h +125 -0
  141. package/dist/node_modules/sharp/src/pipeline.cc +1724 -0
  142. package/dist/node_modules/sharp/src/pipeline.h +385 -0
  143. package/dist/node_modules/sharp/src/sharp.cc +40 -0
  144. package/dist/node_modules/sharp/src/stats.cc +183 -0
  145. package/dist/node_modules/sharp/src/stats.h +59 -0
  146. package/dist/node_modules/sharp/src/utilities.cc +269 -0
  147. package/dist/node_modules/sharp/src/utilities.h +19 -0
  148. package/dist/node_modules/simple-swizzle/LICENSE +21 -0
  149. package/dist/node_modules/simple-swizzle/README.md +39 -0
  150. package/dist/node_modules/simple-swizzle/index.js +29 -0
  151. package/dist/node_modules/simple-swizzle/package.json +36 -0
  152. package/dist/package-lock.json +610 -0
  153. package/dist/package.json +40 -0
  154. package/dist/static/expected_tile_2193_153_255_z7.png +0 -0
  155. package/dist/static/expected_tile_NZTM2000Quad_30_33_z6.png +0 -0
  156. package/dist/static/expected_tile_WebMercatorQuad_252_156_z8.png +0 -0
  157. package/package.json +2 -2
  158. package/src/routes/__tests__/tile.style.json.test.ts +3 -2
  159. package/src/routes/tile.style.json.ts +1 -1
  160. package/src/util/__test__/nztm.style.test.ts +18 -8
  161. package/src/util/nztm.style.ts +0 -3
  162. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,1561 @@
1
+ // Copyright 2013 Lovell Fuller and others.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ 'use strict';
5
+
6
+ const path = require('node:path');
7
+ const is = require('./is');
8
+ const sharp = require('./sharp');
9
+
10
+ const formats = new Map([
11
+ ['heic', 'heif'],
12
+ ['heif', 'heif'],
13
+ ['avif', 'avif'],
14
+ ['jpeg', 'jpeg'],
15
+ ['jpg', 'jpeg'],
16
+ ['jpe', 'jpeg'],
17
+ ['tile', 'tile'],
18
+ ['dz', 'tile'],
19
+ ['png', 'png'],
20
+ ['raw', 'raw'],
21
+ ['tiff', 'tiff'],
22
+ ['tif', 'tiff'],
23
+ ['webp', 'webp'],
24
+ ['gif', 'gif'],
25
+ ['jp2', 'jp2'],
26
+ ['jpx', 'jp2'],
27
+ ['j2k', 'jp2'],
28
+ ['j2c', 'jp2'],
29
+ ['jxl', 'jxl']
30
+ ]);
31
+
32
+ const jp2Regex = /\.(jp[2x]|j2[kc])$/i;
33
+
34
+ const errJp2Save = () => new Error('JP2 output requires libvips with support for OpenJPEG');
35
+
36
+ const bitdepthFromColourCount = (colours) => 1 << 31 - Math.clz32(Math.ceil(Math.log2(colours)));
37
+
38
+ /**
39
+ * Write output image data to a file.
40
+ *
41
+ * If an explicit output format is not selected, it will be inferred from the extension,
42
+ * with JPEG, PNG, WebP, AVIF, TIFF, GIF, DZI, and libvips' V format supported.
43
+ * Note that raw pixel data is only supported for buffer output.
44
+ *
45
+ * By default all metadata will be removed, which includes EXIF-based orientation.
46
+ * See {@link #withmetadata|withMetadata} for control over this.
47
+ *
48
+ * The caller is responsible for ensuring directory structures and permissions exist.
49
+ *
50
+ * A `Promise` is returned when `callback` is not provided.
51
+ *
52
+ * @example
53
+ * sharp(input)
54
+ * .toFile('output.png', (err, info) => { ... });
55
+ *
56
+ * @example
57
+ * sharp(input)
58
+ * .toFile('output.png')
59
+ * .then(info => { ... })
60
+ * .catch(err => { ... });
61
+ *
62
+ * @param {string} fileOut - the path to write the image data to.
63
+ * @param {Function} [callback] - called on completion with two arguments `(err, info)`.
64
+ * `info` contains the output image `format`, `size` (bytes), `width`, `height`,
65
+ * `channels` and `premultiplied` (indicating if premultiplication was used).
66
+ * When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
67
+ * When using the attention crop strategy also contains `attentionX` and `attentionY`, the focal point of the cropped region.
68
+ * May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text.
69
+ * @returns {Promise<Object>} - when no callback is provided
70
+ * @throws {Error} Invalid parameters
71
+ */
72
+ function toFile (fileOut, callback) {
73
+ let err;
74
+ if (!is.string(fileOut)) {
75
+ err = new Error('Missing output file path');
76
+ } else if (is.string(this.options.input.file) && path.resolve(this.options.input.file) === path.resolve(fileOut)) {
77
+ err = new Error('Cannot use same file for input and output');
78
+ } else if (jp2Regex.test(path.extname(fileOut)) && !this.constructor.format.jp2k.output.file) {
79
+ err = errJp2Save();
80
+ }
81
+ if (err) {
82
+ if (is.fn(callback)) {
83
+ callback(err);
84
+ } else {
85
+ return Promise.reject(err);
86
+ }
87
+ } else {
88
+ this.options.fileOut = fileOut;
89
+ const stack = Error();
90
+ return this._pipeline(callback, stack);
91
+ }
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * Write output to a Buffer.
97
+ * JPEG, PNG, WebP, AVIF, TIFF, GIF and raw pixel data output are supported.
98
+ *
99
+ * Use {@link #toformat|toFormat} or one of the format-specific functions such as {@link jpeg}, {@link png} etc. to set the output format.
100
+ *
101
+ * If no explicit format is set, the output format will match the input image, except SVG input which becomes PNG output.
102
+ *
103
+ * By default all metadata will be removed, which includes EXIF-based orientation.
104
+ * See {@link #withmetadata|withMetadata} for control over this.
105
+ *
106
+ * `callback`, if present, gets three arguments `(err, data, info)` where:
107
+ * - `err` is an error, if any.
108
+ * - `data` is the output image data.
109
+ * - `info` contains the output image `format`, `size` (bytes), `width`, `height`,
110
+ * `channels` and `premultiplied` (indicating if premultiplication was used).
111
+ * When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
112
+ * May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text.
113
+ *
114
+ * A `Promise` is returned when `callback` is not provided.
115
+ *
116
+ * @example
117
+ * sharp(input)
118
+ * .toBuffer((err, data, info) => { ... });
119
+ *
120
+ * @example
121
+ * sharp(input)
122
+ * .toBuffer()
123
+ * .then(data => { ... })
124
+ * .catch(err => { ... });
125
+ *
126
+ * @example
127
+ * sharp(input)
128
+ * .png()
129
+ * .toBuffer({ resolveWithObject: true })
130
+ * .then(({ data, info }) => { ... })
131
+ * .catch(err => { ... });
132
+ *
133
+ * @example
134
+ * const { data, info } = await sharp('my-image.jpg')
135
+ * // output the raw pixels
136
+ * .raw()
137
+ * .toBuffer({ resolveWithObject: true });
138
+ *
139
+ * // create a more type safe way to work with the raw pixel data
140
+ * // this will not copy the data, instead it will change `data`s underlying ArrayBuffer
141
+ * // so `data` and `pixelArray` point to the same memory location
142
+ * const pixelArray = new Uint8ClampedArray(data.buffer);
143
+ *
144
+ * // When you are done changing the pixelArray, sharp takes the `pixelArray` as an input
145
+ * const { width, height, channels } = info;
146
+ * await sharp(pixelArray, { raw: { width, height, channels } })
147
+ * .toFile('my-changed-image.jpg');
148
+ *
149
+ * @param {Object} [options]
150
+ * @param {boolean} [options.resolveWithObject] Resolve the Promise with an Object containing `data` and `info` properties instead of resolving only with `data`.
151
+ * @param {Function} [callback]
152
+ * @returns {Promise<Buffer>} - when no callback is provided
153
+ */
154
+ function toBuffer (options, callback) {
155
+ if (is.object(options)) {
156
+ this._setBooleanOption('resolveWithObject', options.resolveWithObject);
157
+ } else if (this.options.resolveWithObject) {
158
+ this.options.resolveWithObject = false;
159
+ }
160
+ this.options.fileOut = '';
161
+ const stack = Error();
162
+ return this._pipeline(is.fn(options) ? options : callback, stack);
163
+ }
164
+
165
+ /**
166
+ * Keep all EXIF metadata from the input image in the output image.
167
+ *
168
+ * EXIF metadata is unsupported for TIFF output.
169
+ *
170
+ * @since 0.33.0
171
+ *
172
+ * @example
173
+ * const outputWithExif = await sharp(inputWithExif)
174
+ * .keepExif()
175
+ * .toBuffer();
176
+ *
177
+ * @returns {Sharp}
178
+ */
179
+ function keepExif () {
180
+ this.options.keepMetadata |= 0b00001;
181
+ return this;
182
+ }
183
+
184
+ /**
185
+ * Set EXIF metadata in the output image, ignoring any EXIF in the input image.
186
+ *
187
+ * @since 0.33.0
188
+ *
189
+ * @example
190
+ * const dataWithExif = await sharp(input)
191
+ * .withExif({
192
+ * IFD0: {
193
+ * Copyright: 'The National Gallery'
194
+ * },
195
+ * IFD3: {
196
+ * GPSLatitudeRef: 'N',
197
+ * GPSLatitude: '51/1 30/1 3230/100',
198
+ * GPSLongitudeRef: 'W',
199
+ * GPSLongitude: '0/1 7/1 4366/100'
200
+ * }
201
+ * })
202
+ * .toBuffer();
203
+ *
204
+ * @param {Object<string, Object<string, string>>} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data.
205
+ * @returns {Sharp}
206
+ * @throws {Error} Invalid parameters
207
+ */
208
+ function withExif (exif) {
209
+ if (is.object(exif)) {
210
+ for (const [ifd, entries] of Object.entries(exif)) {
211
+ if (is.object(entries)) {
212
+ for (const [k, v] of Object.entries(entries)) {
213
+ if (is.string(v)) {
214
+ this.options.withExif[`exif-${ifd.toLowerCase()}-${k}`] = v;
215
+ } else {
216
+ throw is.invalidParameterError(`${ifd}.${k}`, 'string', v);
217
+ }
218
+ }
219
+ } else {
220
+ throw is.invalidParameterError(ifd, 'object', entries);
221
+ }
222
+ }
223
+ } else {
224
+ throw is.invalidParameterError('exif', 'object', exif);
225
+ }
226
+ this.options.withExifMerge = false;
227
+ return this.keepExif();
228
+ }
229
+
230
+ /**
231
+ * Update EXIF metadata from the input image in the output image.
232
+ *
233
+ * @since 0.33.0
234
+ *
235
+ * @example
236
+ * const dataWithMergedExif = await sharp(inputWithExif)
237
+ * .withExifMerge({
238
+ * IFD0: {
239
+ * Copyright: 'The National Gallery'
240
+ * }
241
+ * })
242
+ * .toBuffer();
243
+ *
244
+ * @param {Object<string, Object<string, string>>} exif Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data.
245
+ * @returns {Sharp}
246
+ * @throws {Error} Invalid parameters
247
+ */
248
+ function withExifMerge (exif) {
249
+ this.withExif(exif);
250
+ this.options.withExifMerge = true;
251
+ return this;
252
+ }
253
+
254
+ /**
255
+ * Keep ICC profile from the input image in the output image.
256
+ *
257
+ * Where necessary, will attempt to convert the output colour space to match the profile.
258
+ *
259
+ * @since 0.33.0
260
+ *
261
+ * @example
262
+ * const outputWithIccProfile = await sharp(inputWithIccProfile)
263
+ * .keepIccProfile()
264
+ * .toBuffer();
265
+ *
266
+ * @returns {Sharp}
267
+ */
268
+ function keepIccProfile () {
269
+ this.options.keepMetadata |= 0b01000;
270
+ return this;
271
+ }
272
+
273
+ /**
274
+ * Transform using an ICC profile and attach to the output image.
275
+ *
276
+ * This can either be an absolute filesystem path or
277
+ * built-in profile name (`srgb`, `p3`, `cmyk`).
278
+ *
279
+ * @since 0.33.0
280
+ *
281
+ * @example
282
+ * const outputWithP3 = await sharp(input)
283
+ * .withIccProfile('p3')
284
+ * .toBuffer();
285
+ *
286
+ * @param {string} icc - Absolute filesystem path to output ICC profile or built-in profile name (srgb, p3, cmyk).
287
+ * @param {Object} [options]
288
+ * @param {number} [options.attach=true] Should the ICC profile be included in the output image metadata?
289
+ * @returns {Sharp}
290
+ * @throws {Error} Invalid parameters
291
+ */
292
+ function withIccProfile (icc, options) {
293
+ if (is.string(icc)) {
294
+ this.options.withIccProfile = icc;
295
+ } else {
296
+ throw is.invalidParameterError('icc', 'string', icc);
297
+ }
298
+ this.keepIccProfile();
299
+ if (is.object(options)) {
300
+ if (is.defined(options.attach)) {
301
+ if (is.bool(options.attach)) {
302
+ if (!options.attach) {
303
+ this.options.keepMetadata &= ~0b01000;
304
+ }
305
+ } else {
306
+ throw is.invalidParameterError('attach', 'boolean', options.attach);
307
+ }
308
+ }
309
+ }
310
+ return this;
311
+ }
312
+
313
+ /**
314
+ * Keep all metadata (EXIF, ICC, XMP, IPTC) from the input image in the output image.
315
+ *
316
+ * The default behaviour, when `keepMetadata` is not used, is to convert to the device-independent
317
+ * sRGB colour space and strip all metadata, including the removal of any ICC profile.
318
+ *
319
+ * @since 0.33.0
320
+ *
321
+ * @example
322
+ * const outputWithMetadata = await sharp(inputWithMetadata)
323
+ * .keepMetadata()
324
+ * .toBuffer();
325
+ *
326
+ * @returns {Sharp}
327
+ */
328
+ function keepMetadata () {
329
+ this.options.keepMetadata = 0b11111;
330
+ return this;
331
+ }
332
+
333
+ /**
334
+ * Keep most metadata (EXIF, XMP, IPTC) from the input image in the output image.
335
+ *
336
+ * This will also convert to and add a web-friendly sRGB ICC profile if appropriate.
337
+ *
338
+ * Allows orientation and density to be set or updated.
339
+ *
340
+ * @example
341
+ * const outputSrgbWithMetadata = await sharp(inputRgbWithMetadata)
342
+ * .withMetadata()
343
+ * .toBuffer();
344
+ *
345
+ * @example
346
+ * // Set output metadata to 96 DPI
347
+ * const data = await sharp(input)
348
+ * .withMetadata({ density: 96 })
349
+ * .toBuffer();
350
+ *
351
+ * @param {Object} [options]
352
+ * @param {number} [options.orientation] Used to update the EXIF `Orientation` tag, integer between 1 and 8.
353
+ * @param {number} [options.density] Number of pixels per inch (DPI).
354
+ * @returns {Sharp}
355
+ * @throws {Error} Invalid parameters
356
+ */
357
+ function withMetadata (options) {
358
+ this.keepMetadata();
359
+ this.withIccProfile('srgb');
360
+ if (is.object(options)) {
361
+ if (is.defined(options.orientation)) {
362
+ if (is.integer(options.orientation) && is.inRange(options.orientation, 1, 8)) {
363
+ this.options.withMetadataOrientation = options.orientation;
364
+ } else {
365
+ throw is.invalidParameterError('orientation', 'integer between 1 and 8', options.orientation);
366
+ }
367
+ }
368
+ if (is.defined(options.density)) {
369
+ if (is.number(options.density) && options.density > 0) {
370
+ this.options.withMetadataDensity = options.density;
371
+ } else {
372
+ throw is.invalidParameterError('density', 'positive number', options.density);
373
+ }
374
+ }
375
+ if (is.defined(options.icc)) {
376
+ this.withIccProfile(options.icc);
377
+ }
378
+ if (is.defined(options.exif)) {
379
+ this.withExifMerge(options.exif);
380
+ }
381
+ }
382
+ return this;
383
+ }
384
+
385
+ /**
386
+ * Force output to a given format.
387
+ *
388
+ * @example
389
+ * // Convert any input to PNG output
390
+ * const data = await sharp(input)
391
+ * .toFormat('png')
392
+ * .toBuffer();
393
+ *
394
+ * @param {(string|Object)} format - as a string or an Object with an 'id' attribute
395
+ * @param {Object} options - output options
396
+ * @returns {Sharp}
397
+ * @throws {Error} unsupported format or options
398
+ */
399
+ function toFormat (format, options) {
400
+ const actualFormat = formats.get((is.object(format) && is.string(format.id) ? format.id : format).toLowerCase());
401
+ if (!actualFormat) {
402
+ throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(', ')}`, format);
403
+ }
404
+ return this[actualFormat](options);
405
+ }
406
+
407
+ /**
408
+ * Use these JPEG options for output image.
409
+ *
410
+ * @example
411
+ * // Convert any input to very high quality JPEG output
412
+ * const data = await sharp(input)
413
+ * .jpeg({
414
+ * quality: 100,
415
+ * chromaSubsampling: '4:4:4'
416
+ * })
417
+ * .toBuffer();
418
+ *
419
+ * @example
420
+ * // Use mozjpeg to reduce output JPEG file size (slower)
421
+ * const data = await sharp(input)
422
+ * .jpeg({ mozjpeg: true })
423
+ * .toBuffer();
424
+ *
425
+ * @param {Object} [options] - output options
426
+ * @param {number} [options.quality=80] - quality, integer 1-100
427
+ * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
428
+ * @param {string} [options.chromaSubsampling='4:2:0'] - set to '4:4:4' to prevent chroma subsampling otherwise defaults to '4:2:0' chroma subsampling
429
+ * @param {boolean} [options.optimiseCoding=true] - optimise Huffman coding tables
430
+ * @param {boolean} [options.optimizeCoding=true] - alternative spelling of optimiseCoding
431
+ * @param {boolean} [options.mozjpeg=false] - use mozjpeg defaults, equivalent to `{ trellisQuantisation: true, overshootDeringing: true, optimiseScans: true, quantisationTable: 3 }`
432
+ * @param {boolean} [options.trellisQuantisation=false] - apply trellis quantisation
433
+ * @param {boolean} [options.overshootDeringing=false] - apply overshoot deringing
434
+ * @param {boolean} [options.optimiseScans=false] - optimise progressive scans, forces progressive
435
+ * @param {boolean} [options.optimizeScans=false] - alternative spelling of optimiseScans
436
+ * @param {number} [options.quantisationTable=0] - quantization table to use, integer 0-8
437
+ * @param {number} [options.quantizationTable=0] - alternative spelling of quantisationTable
438
+ * @param {boolean} [options.force=true] - force JPEG output, otherwise attempt to use input format
439
+ * @returns {Sharp}
440
+ * @throws {Error} Invalid options
441
+ */
442
+ function jpeg (options) {
443
+ if (is.object(options)) {
444
+ if (is.defined(options.quality)) {
445
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
446
+ this.options.jpegQuality = options.quality;
447
+ } else {
448
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
449
+ }
450
+ }
451
+ if (is.defined(options.progressive)) {
452
+ this._setBooleanOption('jpegProgressive', options.progressive);
453
+ }
454
+ if (is.defined(options.chromaSubsampling)) {
455
+ if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
456
+ this.options.jpegChromaSubsampling = options.chromaSubsampling;
457
+ } else {
458
+ throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
459
+ }
460
+ }
461
+ const optimiseCoding = is.bool(options.optimizeCoding) ? options.optimizeCoding : options.optimiseCoding;
462
+ if (is.defined(optimiseCoding)) {
463
+ this._setBooleanOption('jpegOptimiseCoding', optimiseCoding);
464
+ }
465
+ if (is.defined(options.mozjpeg)) {
466
+ if (is.bool(options.mozjpeg)) {
467
+ if (options.mozjpeg) {
468
+ this.options.jpegTrellisQuantisation = true;
469
+ this.options.jpegOvershootDeringing = true;
470
+ this.options.jpegOptimiseScans = true;
471
+ this.options.jpegProgressive = true;
472
+ this.options.jpegQuantisationTable = 3;
473
+ }
474
+ } else {
475
+ throw is.invalidParameterError('mozjpeg', 'boolean', options.mozjpeg);
476
+ }
477
+ }
478
+ const trellisQuantisation = is.bool(options.trellisQuantization) ? options.trellisQuantization : options.trellisQuantisation;
479
+ if (is.defined(trellisQuantisation)) {
480
+ this._setBooleanOption('jpegTrellisQuantisation', trellisQuantisation);
481
+ }
482
+ if (is.defined(options.overshootDeringing)) {
483
+ this._setBooleanOption('jpegOvershootDeringing', options.overshootDeringing);
484
+ }
485
+ const optimiseScans = is.bool(options.optimizeScans) ? options.optimizeScans : options.optimiseScans;
486
+ if (is.defined(optimiseScans)) {
487
+ this._setBooleanOption('jpegOptimiseScans', optimiseScans);
488
+ if (optimiseScans) {
489
+ this.options.jpegProgressive = true;
490
+ }
491
+ }
492
+ const quantisationTable = is.number(options.quantizationTable) ? options.quantizationTable : options.quantisationTable;
493
+ if (is.defined(quantisationTable)) {
494
+ if (is.integer(quantisationTable) && is.inRange(quantisationTable, 0, 8)) {
495
+ this.options.jpegQuantisationTable = quantisationTable;
496
+ } else {
497
+ throw is.invalidParameterError('quantisationTable', 'integer between 0 and 8', quantisationTable);
498
+ }
499
+ }
500
+ }
501
+ return this._updateFormatOut('jpeg', options);
502
+ }
503
+
504
+ /**
505
+ * Use these PNG options for output image.
506
+ *
507
+ * By default, PNG output is full colour at 8 or 16 bits per pixel.
508
+ * Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
509
+ * Set `palette` to `true` for slower, indexed PNG output.
510
+ *
511
+ * @example
512
+ * // Convert any input to full colour PNG output
513
+ * const data = await sharp(input)
514
+ * .png()
515
+ * .toBuffer();
516
+ *
517
+ * @example
518
+ * // Convert any input to indexed PNG output (slower)
519
+ * const data = await sharp(input)
520
+ * .png({ palette: true })
521
+ * .toBuffer();
522
+ *
523
+ * @param {Object} [options]
524
+ * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
525
+ * @param {number} [options.compressionLevel=6] - zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest)
526
+ * @param {boolean} [options.adaptiveFiltering=false] - use adaptive row filtering
527
+ * @param {boolean} [options.palette=false] - quantise to a palette-based image with alpha transparency support
528
+ * @param {number} [options.quality=100] - use the lowest number of colours needed to achieve given quality, sets `palette` to `true`
529
+ * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest), sets `palette` to `true`
530
+ * @param {number} [options.colours=256] - maximum number of palette entries, sets `palette` to `true`
531
+ * @param {number} [options.colors=256] - alternative spelling of `options.colours`, sets `palette` to `true`
532
+ * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, sets `palette` to `true`
533
+ * @param {boolean} [options.force=true] - force PNG output, otherwise attempt to use input format
534
+ * @returns {Sharp}
535
+ * @throws {Error} Invalid options
536
+ */
537
+ function png (options) {
538
+ if (is.object(options)) {
539
+ if (is.defined(options.progressive)) {
540
+ this._setBooleanOption('pngProgressive', options.progressive);
541
+ }
542
+ if (is.defined(options.compressionLevel)) {
543
+ if (is.integer(options.compressionLevel) && is.inRange(options.compressionLevel, 0, 9)) {
544
+ this.options.pngCompressionLevel = options.compressionLevel;
545
+ } else {
546
+ throw is.invalidParameterError('compressionLevel', 'integer between 0 and 9', options.compressionLevel);
547
+ }
548
+ }
549
+ if (is.defined(options.adaptiveFiltering)) {
550
+ this._setBooleanOption('pngAdaptiveFiltering', options.adaptiveFiltering);
551
+ }
552
+ const colours = options.colours || options.colors;
553
+ if (is.defined(colours)) {
554
+ if (is.integer(colours) && is.inRange(colours, 2, 256)) {
555
+ this.options.pngBitdepth = bitdepthFromColourCount(colours);
556
+ } else {
557
+ throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
558
+ }
559
+ }
560
+ if (is.defined(options.palette)) {
561
+ this._setBooleanOption('pngPalette', options.palette);
562
+ } else if ([options.quality, options.effort, options.colours, options.colors, options.dither].some(is.defined)) {
563
+ this._setBooleanOption('pngPalette', true);
564
+ }
565
+ if (this.options.pngPalette) {
566
+ if (is.defined(options.quality)) {
567
+ if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) {
568
+ this.options.pngQuality = options.quality;
569
+ } else {
570
+ throw is.invalidParameterError('quality', 'integer between 0 and 100', options.quality);
571
+ }
572
+ }
573
+ if (is.defined(options.effort)) {
574
+ if (is.integer(options.effort) && is.inRange(options.effort, 1, 10)) {
575
+ this.options.pngEffort = options.effort;
576
+ } else {
577
+ throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort);
578
+ }
579
+ }
580
+ if (is.defined(options.dither)) {
581
+ if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
582
+ this.options.pngDither = options.dither;
583
+ } else {
584
+ throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither);
585
+ }
586
+ }
587
+ }
588
+ }
589
+ return this._updateFormatOut('png', options);
590
+ }
591
+
592
+ /**
593
+ * Use these WebP options for output image.
594
+ *
595
+ * @example
596
+ * // Convert any input to lossless WebP output
597
+ * const data = await sharp(input)
598
+ * .webp({ lossless: true })
599
+ * .toBuffer();
600
+ *
601
+ * @example
602
+ * // Optimise the file size of an animated WebP
603
+ * const outputWebp = await sharp(inputWebp, { animated: true })
604
+ * .webp({ effort: 6 })
605
+ * .toBuffer();
606
+ *
607
+ * @param {Object} [options] - output options
608
+ * @param {number} [options.quality=80] - quality, integer 1-100
609
+ * @param {number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100
610
+ * @param {boolean} [options.lossless=false] - use lossless compression mode
611
+ * @param {boolean} [options.nearLossless=false] - use near_lossless compression mode
612
+ * @param {boolean} [options.smartSubsample=false] - use high quality chroma subsampling
613
+ * @param {string} [options.preset='default'] - named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text
614
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 6 (slowest)
615
+ * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
616
+ * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
617
+ * @param {boolean} [options.minSize=false] - prevent use of animation key frames to minimise file size (slow)
618
+ * @param {boolean} [options.mixed=false] - allow mixture of lossy and lossless animation frames (slow)
619
+ * @param {boolean} [options.force=true] - force WebP output, otherwise attempt to use input format
620
+ * @returns {Sharp}
621
+ * @throws {Error} Invalid options
622
+ */
623
+ function webp (options) {
624
+ if (is.object(options)) {
625
+ if (is.defined(options.quality)) {
626
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
627
+ this.options.webpQuality = options.quality;
628
+ } else {
629
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
630
+ }
631
+ }
632
+ if (is.defined(options.alphaQuality)) {
633
+ if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 0, 100)) {
634
+ this.options.webpAlphaQuality = options.alphaQuality;
635
+ } else {
636
+ throw is.invalidParameterError('alphaQuality', 'integer between 0 and 100', options.alphaQuality);
637
+ }
638
+ }
639
+ if (is.defined(options.lossless)) {
640
+ this._setBooleanOption('webpLossless', options.lossless);
641
+ }
642
+ if (is.defined(options.nearLossless)) {
643
+ this._setBooleanOption('webpNearLossless', options.nearLossless);
644
+ }
645
+ if (is.defined(options.smartSubsample)) {
646
+ this._setBooleanOption('webpSmartSubsample', options.smartSubsample);
647
+ }
648
+ if (is.defined(options.preset)) {
649
+ if (is.string(options.preset) && is.inArray(options.preset, ['default', 'photo', 'picture', 'drawing', 'icon', 'text'])) {
650
+ this.options.webpPreset = options.preset;
651
+ } else {
652
+ throw is.invalidParameterError('preset', 'one of: default, photo, picture, drawing, icon, text', options.preset);
653
+ }
654
+ }
655
+ if (is.defined(options.effort)) {
656
+ if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) {
657
+ this.options.webpEffort = options.effort;
658
+ } else {
659
+ throw is.invalidParameterError('effort', 'integer between 0 and 6', options.effort);
660
+ }
661
+ }
662
+ if (is.defined(options.minSize)) {
663
+ this._setBooleanOption('webpMinSize', options.minSize);
664
+ }
665
+ if (is.defined(options.mixed)) {
666
+ this._setBooleanOption('webpMixed', options.mixed);
667
+ }
668
+ }
669
+ trySetAnimationOptions(options, this.options);
670
+ return this._updateFormatOut('webp', options);
671
+ }
672
+
673
+ /**
674
+ * Use these GIF options for the output image.
675
+ *
676
+ * The first entry in the palette is reserved for transparency.
677
+ *
678
+ * The palette of the input image will be re-used if possible.
679
+ *
680
+ * @since 0.30.0
681
+ *
682
+ * @example
683
+ * // Convert PNG to GIF
684
+ * await sharp(pngBuffer)
685
+ * .gif()
686
+ * .toBuffer();
687
+ *
688
+ * @example
689
+ * // Convert animated WebP to animated GIF
690
+ * await sharp('animated.webp', { animated: true })
691
+ * .toFile('animated.gif');
692
+ *
693
+ * @example
694
+ * // Create a 128x128, cropped, non-dithered, animated thumbnail of an animated GIF
695
+ * const out = await sharp('in.gif', { animated: true })
696
+ * .resize({ width: 128, height: 128 })
697
+ * .gif({ dither: 0 })
698
+ * .toBuffer();
699
+ *
700
+ * @example
701
+ * // Lossy file size reduction of animated GIF
702
+ * await sharp('in.gif', { animated: true })
703
+ * .gif({ interFrameMaxError: 8 })
704
+ * .toFile('optim.gif');
705
+ *
706
+ * @param {Object} [options] - output options
707
+ * @param {boolean} [options.reuse=true] - re-use existing palette, otherwise generate new (slow)
708
+ * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
709
+ * @param {number} [options.colours=256] - maximum number of palette entries, including transparency, between 2 and 256
710
+ * @param {number} [options.colors=256] - alternative spelling of `options.colours`
711
+ * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest)
712
+ * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most)
713
+ * @param {number} [options.interFrameMaxError=0] - maximum inter-frame error for transparency, between 0 (lossless) and 32
714
+ * @param {number} [options.interPaletteMaxError=3] - maximum inter-palette error for palette reuse, between 0 and 256
715
+ * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
716
+ * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
717
+ * @param {boolean} [options.force=true] - force GIF output, otherwise attempt to use input format
718
+ * @returns {Sharp}
719
+ * @throws {Error} Invalid options
720
+ */
721
+ function gif (options) {
722
+ if (is.object(options)) {
723
+ if (is.defined(options.reuse)) {
724
+ this._setBooleanOption('gifReuse', options.reuse);
725
+ }
726
+ if (is.defined(options.progressive)) {
727
+ this._setBooleanOption('gifProgressive', options.progressive);
728
+ }
729
+ const colours = options.colours || options.colors;
730
+ if (is.defined(colours)) {
731
+ if (is.integer(colours) && is.inRange(colours, 2, 256)) {
732
+ this.options.gifBitdepth = bitdepthFromColourCount(colours);
733
+ } else {
734
+ throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
735
+ }
736
+ }
737
+ if (is.defined(options.effort)) {
738
+ if (is.number(options.effort) && is.inRange(options.effort, 1, 10)) {
739
+ this.options.gifEffort = options.effort;
740
+ } else {
741
+ throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort);
742
+ }
743
+ }
744
+ if (is.defined(options.dither)) {
745
+ if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
746
+ this.options.gifDither = options.dither;
747
+ } else {
748
+ throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither);
749
+ }
750
+ }
751
+ if (is.defined(options.interFrameMaxError)) {
752
+ if (is.number(options.interFrameMaxError) && is.inRange(options.interFrameMaxError, 0, 32)) {
753
+ this.options.gifInterFrameMaxError = options.interFrameMaxError;
754
+ } else {
755
+ throw is.invalidParameterError('interFrameMaxError', 'number between 0.0 and 32.0', options.interFrameMaxError);
756
+ }
757
+ }
758
+ if (is.defined(options.interPaletteMaxError)) {
759
+ if (is.number(options.interPaletteMaxError) && is.inRange(options.interPaletteMaxError, 0, 256)) {
760
+ this.options.gifInterPaletteMaxError = options.interPaletteMaxError;
761
+ } else {
762
+ throw is.invalidParameterError('interPaletteMaxError', 'number between 0.0 and 256.0', options.interPaletteMaxError);
763
+ }
764
+ }
765
+ }
766
+ trySetAnimationOptions(options, this.options);
767
+ return this._updateFormatOut('gif', options);
768
+ }
769
+
770
+ /* istanbul ignore next */
771
+ /**
772
+ * Use these JP2 options for output image.
773
+ *
774
+ * Requires libvips compiled with support for OpenJPEG.
775
+ * The prebuilt binaries do not include this - see
776
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
777
+ *
778
+ * @example
779
+ * // Convert any input to lossless JP2 output
780
+ * const data = await sharp(input)
781
+ * .jp2({ lossless: true })
782
+ * .toBuffer();
783
+ *
784
+ * @example
785
+ * // Convert any input to very high quality JP2 output
786
+ * const data = await sharp(input)
787
+ * .jp2({
788
+ * quality: 100,
789
+ * chromaSubsampling: '4:4:4'
790
+ * })
791
+ * .toBuffer();
792
+ *
793
+ * @since 0.29.1
794
+ *
795
+ * @param {Object} [options] - output options
796
+ * @param {number} [options.quality=80] - quality, integer 1-100
797
+ * @param {boolean} [options.lossless=false] - use lossless compression mode
798
+ * @param {number} [options.tileWidth=512] - horizontal tile size
799
+ * @param {number} [options.tileHeight=512] - vertical tile size
800
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
801
+ * @returns {Sharp}
802
+ * @throws {Error} Invalid options
803
+ */
804
+ function jp2 (options) {
805
+ if (!this.constructor.format.jp2k.output.buffer) {
806
+ throw errJp2Save();
807
+ }
808
+ if (is.object(options)) {
809
+ if (is.defined(options.quality)) {
810
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
811
+ this.options.jp2Quality = options.quality;
812
+ } else {
813
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
814
+ }
815
+ }
816
+ if (is.defined(options.lossless)) {
817
+ if (is.bool(options.lossless)) {
818
+ this.options.jp2Lossless = options.lossless;
819
+ } else {
820
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
821
+ }
822
+ }
823
+ if (is.defined(options.tileWidth)) {
824
+ if (is.integer(options.tileWidth) && is.inRange(options.tileWidth, 1, 32768)) {
825
+ this.options.jp2TileWidth = options.tileWidth;
826
+ } else {
827
+ throw is.invalidParameterError('tileWidth', 'integer between 1 and 32768', options.tileWidth);
828
+ }
829
+ }
830
+ if (is.defined(options.tileHeight)) {
831
+ if (is.integer(options.tileHeight) && is.inRange(options.tileHeight, 1, 32768)) {
832
+ this.options.jp2TileHeight = options.tileHeight;
833
+ } else {
834
+ throw is.invalidParameterError('tileHeight', 'integer between 1 and 32768', options.tileHeight);
835
+ }
836
+ }
837
+ if (is.defined(options.chromaSubsampling)) {
838
+ if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
839
+ this.options.jp2ChromaSubsampling = options.chromaSubsampling;
840
+ } else {
841
+ throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
842
+ }
843
+ }
844
+ }
845
+ return this._updateFormatOut('jp2', options);
846
+ }
847
+
848
+ /**
849
+ * Set animation options if available.
850
+ * @private
851
+ *
852
+ * @param {Object} [source] - output options
853
+ * @param {number} [source.loop=0] - number of animation iterations, use 0 for infinite animation
854
+ * @param {number[]} [source.delay] - list of delays between animation frames (in milliseconds)
855
+ * @param {Object} [target] - target object for valid options
856
+ * @throws {Error} Invalid options
857
+ */
858
+ function trySetAnimationOptions (source, target) {
859
+ if (is.object(source) && is.defined(source.loop)) {
860
+ if (is.integer(source.loop) && is.inRange(source.loop, 0, 65535)) {
861
+ target.loop = source.loop;
862
+ } else {
863
+ throw is.invalidParameterError('loop', 'integer between 0 and 65535', source.loop);
864
+ }
865
+ }
866
+ if (is.object(source) && is.defined(source.delay)) {
867
+ // We allow singular values as well
868
+ if (is.integer(source.delay) && is.inRange(source.delay, 0, 65535)) {
869
+ target.delay = [source.delay];
870
+ } else if (
871
+ Array.isArray(source.delay) &&
872
+ source.delay.every(is.integer) &&
873
+ source.delay.every(v => is.inRange(v, 0, 65535))) {
874
+ target.delay = source.delay;
875
+ } else {
876
+ throw is.invalidParameterError('delay', 'integer or an array of integers between 0 and 65535', source.delay);
877
+ }
878
+ }
879
+ }
880
+
881
+ /**
882
+ * Use these TIFF options for output image.
883
+ *
884
+ * The `density` can be set in pixels/inch via {@link #withmetadata|withMetadata}
885
+ * instead of providing `xres` and `yres` in pixels/mm.
886
+ *
887
+ * @example
888
+ * // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
889
+ * sharp('input.svg')
890
+ * .tiff({
891
+ * compression: 'lzw',
892
+ * bitdepth: 1
893
+ * })
894
+ * .toFile('1-bpp-output.tiff')
895
+ * .then(info => { ... });
896
+ *
897
+ * @param {Object} [options] - output options
898
+ * @param {number} [options.quality=80] - quality, integer 1-100
899
+ * @param {boolean} [options.force=true] - force TIFF output, otherwise attempt to use input format
900
+ * @param {string} [options.compression='jpeg'] - compression options: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k
901
+ * @param {string} [options.predictor='horizontal'] - compression predictor options: none, horizontal, float
902
+ * @param {boolean} [options.pyramid=false] - write an image pyramid
903
+ * @param {boolean} [options.tile=false] - write a tiled tiff
904
+ * @param {number} [options.tileWidth=256] - horizontal tile size
905
+ * @param {number} [options.tileHeight=256] - vertical tile size
906
+ * @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm
907
+ * @param {number} [options.yres=1.0] - vertical resolution in pixels/mm
908
+ * @param {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm
909
+ * @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
910
+ * @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite
911
+ * @returns {Sharp}
912
+ * @throws {Error} Invalid options
913
+ */
914
+ function tiff (options) {
915
+ if (is.object(options)) {
916
+ if (is.defined(options.quality)) {
917
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
918
+ this.options.tiffQuality = options.quality;
919
+ } else {
920
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
921
+ }
922
+ }
923
+ if (is.defined(options.bitdepth)) {
924
+ if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4, 8])) {
925
+ this.options.tiffBitdepth = options.bitdepth;
926
+ } else {
927
+ throw is.invalidParameterError('bitdepth', '1, 2, 4 or 8', options.bitdepth);
928
+ }
929
+ }
930
+ // tiling
931
+ if (is.defined(options.tile)) {
932
+ this._setBooleanOption('tiffTile', options.tile);
933
+ }
934
+ if (is.defined(options.tileWidth)) {
935
+ if (is.integer(options.tileWidth) && options.tileWidth > 0) {
936
+ this.options.tiffTileWidth = options.tileWidth;
937
+ } else {
938
+ throw is.invalidParameterError('tileWidth', 'integer greater than zero', options.tileWidth);
939
+ }
940
+ }
941
+ if (is.defined(options.tileHeight)) {
942
+ if (is.integer(options.tileHeight) && options.tileHeight > 0) {
943
+ this.options.tiffTileHeight = options.tileHeight;
944
+ } else {
945
+ throw is.invalidParameterError('tileHeight', 'integer greater than zero', options.tileHeight);
946
+ }
947
+ }
948
+ // miniswhite
949
+ if (is.defined(options.miniswhite)) {
950
+ this._setBooleanOption('tiffMiniswhite', options.miniswhite);
951
+ }
952
+ // pyramid
953
+ if (is.defined(options.pyramid)) {
954
+ this._setBooleanOption('tiffPyramid', options.pyramid);
955
+ }
956
+ // resolution
957
+ if (is.defined(options.xres)) {
958
+ if (is.number(options.xres) && options.xres > 0) {
959
+ this.options.tiffXres = options.xres;
960
+ } else {
961
+ throw is.invalidParameterError('xres', 'number greater than zero', options.xres);
962
+ }
963
+ }
964
+ if (is.defined(options.yres)) {
965
+ if (is.number(options.yres) && options.yres > 0) {
966
+ this.options.tiffYres = options.yres;
967
+ } else {
968
+ throw is.invalidParameterError('yres', 'number greater than zero', options.yres);
969
+ }
970
+ }
971
+ // compression
972
+ if (is.defined(options.compression)) {
973
+ if (is.string(options.compression) && is.inArray(options.compression, ['none', 'jpeg', 'deflate', 'packbits', 'ccittfax4', 'lzw', 'webp', 'zstd', 'jp2k'])) {
974
+ this.options.tiffCompression = options.compression;
975
+ } else {
976
+ throw is.invalidParameterError('compression', 'one of: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k', options.compression);
977
+ }
978
+ }
979
+ // predictor
980
+ if (is.defined(options.predictor)) {
981
+ if (is.string(options.predictor) && is.inArray(options.predictor, ['none', 'horizontal', 'float'])) {
982
+ this.options.tiffPredictor = options.predictor;
983
+ } else {
984
+ throw is.invalidParameterError('predictor', 'one of: none, horizontal, float', options.predictor);
985
+ }
986
+ }
987
+ // resolutionUnit
988
+ if (is.defined(options.resolutionUnit)) {
989
+ if (is.string(options.resolutionUnit) && is.inArray(options.resolutionUnit, ['inch', 'cm'])) {
990
+ this.options.tiffResolutionUnit = options.resolutionUnit;
991
+ } else {
992
+ throw is.invalidParameterError('resolutionUnit', 'one of: inch, cm', options.resolutionUnit);
993
+ }
994
+ }
995
+ }
996
+ return this._updateFormatOut('tiff', options);
997
+ }
998
+
999
+ /**
1000
+ * Use these AVIF options for output image.
1001
+ *
1002
+ * AVIF image sequences are not supported.
1003
+ *
1004
+ * @example
1005
+ * const data = await sharp(input)
1006
+ * .avif({ effort: 2 })
1007
+ * .toBuffer();
1008
+ *
1009
+ * @example
1010
+ * const data = await sharp(input)
1011
+ * .avif({ lossless: true })
1012
+ * .toBuffer();
1013
+ *
1014
+ * @since 0.27.0
1015
+ *
1016
+ * @param {Object} [options] - output options
1017
+ * @param {number} [options.quality=50] - quality, integer 1-100
1018
+ * @param {boolean} [options.lossless=false] - use lossless compression
1019
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest)
1020
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
1021
+ * @returns {Sharp}
1022
+ * @throws {Error} Invalid options
1023
+ */
1024
+ function avif (options) {
1025
+ return this.heif({ ...options, compression: 'av1' });
1026
+ }
1027
+
1028
+ /**
1029
+ * Use these HEIF options for output image.
1030
+ *
1031
+ * Support for patent-encumbered HEIC images using `hevc` compression requires the use of a
1032
+ * globally-installed libvips compiled with support for libheif, libde265 and x265.
1033
+ *
1034
+ * @example
1035
+ * const data = await sharp(input)
1036
+ * .heif({ compression: 'hevc' })
1037
+ * .toBuffer();
1038
+ *
1039
+ * @since 0.23.0
1040
+ *
1041
+ * @param {Object} options - output options
1042
+ * @param {string} options.compression - compression format: av1, hevc
1043
+ * @param {number} [options.quality=50] - quality, integer 1-100
1044
+ * @param {boolean} [options.lossless=false] - use lossless compression
1045
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest)
1046
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
1047
+ * @returns {Sharp}
1048
+ * @throws {Error} Invalid options
1049
+ */
1050
+ function heif (options) {
1051
+ if (is.object(options)) {
1052
+ if (is.string(options.compression) && is.inArray(options.compression, ['av1', 'hevc'])) {
1053
+ this.options.heifCompression = options.compression;
1054
+ } else {
1055
+ throw is.invalidParameterError('compression', 'one of: av1, hevc', options.compression);
1056
+ }
1057
+ if (is.defined(options.quality)) {
1058
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
1059
+ this.options.heifQuality = options.quality;
1060
+ } else {
1061
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
1062
+ }
1063
+ }
1064
+ if (is.defined(options.lossless)) {
1065
+ if (is.bool(options.lossless)) {
1066
+ this.options.heifLossless = options.lossless;
1067
+ } else {
1068
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
1069
+ }
1070
+ }
1071
+ if (is.defined(options.effort)) {
1072
+ if (is.integer(options.effort) && is.inRange(options.effort, 0, 9)) {
1073
+ this.options.heifEffort = options.effort;
1074
+ } else {
1075
+ throw is.invalidParameterError('effort', 'integer between 0 and 9', options.effort);
1076
+ }
1077
+ }
1078
+ if (is.defined(options.chromaSubsampling)) {
1079
+ if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
1080
+ this.options.heifChromaSubsampling = options.chromaSubsampling;
1081
+ } else {
1082
+ throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
1083
+ }
1084
+ }
1085
+ } else {
1086
+ throw is.invalidParameterError('options', 'Object', options);
1087
+ }
1088
+ return this._updateFormatOut('heif', options);
1089
+ }
1090
+
1091
+ /**
1092
+ * Use these JPEG-XL (JXL) options for output image.
1093
+ *
1094
+ * This feature is experimental, please do not use in production systems.
1095
+ *
1096
+ * Requires libvips compiled with support for libjxl.
1097
+ * The prebuilt binaries do not include this - see
1098
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
1099
+ *
1100
+ * Image metadata (EXIF, XMP) is unsupported.
1101
+ *
1102
+ * @since 0.31.3
1103
+ *
1104
+ * @param {Object} [options] - output options
1105
+ * @param {number} [options.distance=1.0] - maximum encoding error, between 0 (highest quality) and 15 (lowest quality)
1106
+ * @param {number} [options.quality] - calculate `distance` based on JPEG-like quality, between 1 and 100, overrides distance if specified
1107
+ * @param {number} [options.decodingTier=0] - target decode speed tier, between 0 (highest quality) and 4 (lowest quality)
1108
+ * @param {boolean} [options.lossless=false] - use lossless compression
1109
+ * @param {number} [options.effort=7] - CPU effort, between 3 (fastest) and 9 (slowest)
1110
+ * @returns {Sharp}
1111
+ * @throws {Error} Invalid options
1112
+ */
1113
+ function jxl (options) {
1114
+ if (is.object(options)) {
1115
+ if (is.defined(options.quality)) {
1116
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
1117
+ // https://github.com/libjxl/libjxl/blob/0aeea7f180bafd6893c1db8072dcb67d2aa5b03d/tools/cjxl_main.cc#L640-L644
1118
+ this.options.jxlDistance = options.quality >= 30
1119
+ ? 0.1 + (100 - options.quality) * 0.09
1120
+ : 53 / 3000 * options.quality * options.quality - 23 / 20 * options.quality + 25;
1121
+ } else {
1122
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
1123
+ }
1124
+ } else if (is.defined(options.distance)) {
1125
+ if (is.number(options.distance) && is.inRange(options.distance, 0, 15)) {
1126
+ this.options.jxlDistance = options.distance;
1127
+ } else {
1128
+ throw is.invalidParameterError('distance', 'number between 0.0 and 15.0', options.distance);
1129
+ }
1130
+ }
1131
+ if (is.defined(options.decodingTier)) {
1132
+ if (is.integer(options.decodingTier) && is.inRange(options.decodingTier, 0, 4)) {
1133
+ this.options.jxlDecodingTier = options.decodingTier;
1134
+ } else {
1135
+ throw is.invalidParameterError('decodingTier', 'integer between 0 and 4', options.decodingTier);
1136
+ }
1137
+ }
1138
+ if (is.defined(options.lossless)) {
1139
+ if (is.bool(options.lossless)) {
1140
+ this.options.jxlLossless = options.lossless;
1141
+ } else {
1142
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
1143
+ }
1144
+ }
1145
+ if (is.defined(options.effort)) {
1146
+ if (is.integer(options.effort) && is.inRange(options.effort, 3, 9)) {
1147
+ this.options.jxlEffort = options.effort;
1148
+ } else {
1149
+ throw is.invalidParameterError('effort', 'integer between 3 and 9', options.effort);
1150
+ }
1151
+ }
1152
+ }
1153
+ return this._updateFormatOut('jxl', options);
1154
+ }
1155
+
1156
+ /**
1157
+ * Force output to be raw, uncompressed pixel data.
1158
+ * Pixel ordering is left-to-right, top-to-bottom, without padding.
1159
+ * Channel ordering will be RGB or RGBA for non-greyscale colourspaces.
1160
+ *
1161
+ * @example
1162
+ * // Extract raw, unsigned 8-bit RGB pixel data from JPEG input
1163
+ * const { data, info } = await sharp('input.jpg')
1164
+ * .raw()
1165
+ * .toBuffer({ resolveWithObject: true });
1166
+ *
1167
+ * @example
1168
+ * // Extract alpha channel as raw, unsigned 16-bit pixel data from PNG input
1169
+ * const data = await sharp('input.png')
1170
+ * .ensureAlpha()
1171
+ * .extractChannel(3)
1172
+ * .toColourspace('b-w')
1173
+ * .raw({ depth: 'ushort' })
1174
+ * .toBuffer();
1175
+ *
1176
+ * @param {Object} [options] - output options
1177
+ * @param {string} [options.depth='uchar'] - bit depth, one of: char, uchar (default), short, ushort, int, uint, float, complex, double, dpcomplex
1178
+ * @returns {Sharp}
1179
+ * @throws {Error} Invalid options
1180
+ */
1181
+ function raw (options) {
1182
+ if (is.object(options)) {
1183
+ if (is.defined(options.depth)) {
1184
+ if (is.string(options.depth) && is.inArray(options.depth,
1185
+ ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'float', 'complex', 'double', 'dpcomplex']
1186
+ )) {
1187
+ this.options.rawDepth = options.depth;
1188
+ } else {
1189
+ throw is.invalidParameterError('depth', 'one of: char, uchar, short, ushort, int, uint, float, complex, double, dpcomplex', options.depth);
1190
+ }
1191
+ }
1192
+ }
1193
+ return this._updateFormatOut('raw');
1194
+ }
1195
+
1196
+ /**
1197
+ * Use tile-based deep zoom (image pyramid) output.
1198
+ *
1199
+ * Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions.
1200
+ * Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format.
1201
+ *
1202
+ * The container will be set to `zip` when the output is a Buffer or Stream, otherwise it will default to `fs`.
1203
+ *
1204
+ * Requires libvips compiled with support for libgsf.
1205
+ * The prebuilt binaries do not include this - see
1206
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
1207
+ *
1208
+ * @example
1209
+ * sharp('input.tiff')
1210
+ * .png()
1211
+ * .tile({
1212
+ * size: 512
1213
+ * })
1214
+ * .toFile('output.dz', function(err, info) {
1215
+ * // output.dzi is the Deep Zoom XML definition
1216
+ * // output_files contains 512x512 tiles grouped by zoom level
1217
+ * });
1218
+ *
1219
+ * @example
1220
+ * const zipFileWithTiles = await sharp(input)
1221
+ * .tile({ basename: "tiles" })
1222
+ * .toBuffer();
1223
+ *
1224
+ * @example
1225
+ * const iiififier = sharp().tile({ layout: "iiif" });
1226
+ * readableStream
1227
+ * .pipe(iiififier)
1228
+ * .pipe(writeableStream);
1229
+ *
1230
+ * @param {Object} [options]
1231
+ * @param {number} [options.size=256] tile size in pixels, a value between 1 and 8192.
1232
+ * @param {number} [options.overlap=0] tile overlap in pixels, a value between 0 and 8192.
1233
+ * @param {number} [options.angle=0] tile angle of rotation, must be a multiple of 90.
1234
+ * @param {string|Object} [options.background={r: 255, g: 255, b: 255, alpha: 1}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to white without transparency.
1235
+ * @param {string} [options.depth] how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout.
1236
+ * @param {number} [options.skipBlanks=-1] threshold to skip tile generation, a value 0 - 255 for 8-bit images or 0 - 65535 for 16-bit images
1237
+ * @param {string} [options.container='fs'] tile container, with value `fs` (filesystem) or `zip` (compressed file).
1238
+ * @param {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `iiif3`, `zoomify` or `google`.
1239
+ * @param {boolean} [options.centre=false] centre image in tile.
1240
+ * @param {boolean} [options.center=false] alternative spelling of centre.
1241
+ * @param {string} [options.id='https://example.com/iiif'] when `layout` is `iiif`/`iiif3`, sets the `@id`/`id` attribute of `info.json`
1242
+ * @param {string} [options.basename] the name of the directory within the zip file when container is `zip`.
1243
+ * @returns {Sharp}
1244
+ * @throws {Error} Invalid parameters
1245
+ */
1246
+ function tile (options) {
1247
+ if (is.object(options)) {
1248
+ // Size of square tiles, in pixels
1249
+ if (is.defined(options.size)) {
1250
+ if (is.integer(options.size) && is.inRange(options.size, 1, 8192)) {
1251
+ this.options.tileSize = options.size;
1252
+ } else {
1253
+ throw is.invalidParameterError('size', 'integer between 1 and 8192', options.size);
1254
+ }
1255
+ }
1256
+ // Overlap of tiles, in pixels
1257
+ if (is.defined(options.overlap)) {
1258
+ if (is.integer(options.overlap) && is.inRange(options.overlap, 0, 8192)) {
1259
+ if (options.overlap > this.options.tileSize) {
1260
+ throw is.invalidParameterError('overlap', `<= size (${this.options.tileSize})`, options.overlap);
1261
+ }
1262
+ this.options.tileOverlap = options.overlap;
1263
+ } else {
1264
+ throw is.invalidParameterError('overlap', 'integer between 0 and 8192', options.overlap);
1265
+ }
1266
+ }
1267
+ // Container
1268
+ if (is.defined(options.container)) {
1269
+ if (is.string(options.container) && is.inArray(options.container, ['fs', 'zip'])) {
1270
+ this.options.tileContainer = options.container;
1271
+ } else {
1272
+ throw is.invalidParameterError('container', 'one of: fs, zip', options.container);
1273
+ }
1274
+ }
1275
+ // Layout
1276
+ if (is.defined(options.layout)) {
1277
+ if (is.string(options.layout) && is.inArray(options.layout, ['dz', 'google', 'iiif', 'iiif3', 'zoomify'])) {
1278
+ this.options.tileLayout = options.layout;
1279
+ } else {
1280
+ throw is.invalidParameterError('layout', 'one of: dz, google, iiif, iiif3, zoomify', options.layout);
1281
+ }
1282
+ }
1283
+ // Angle of rotation,
1284
+ if (is.defined(options.angle)) {
1285
+ if (is.integer(options.angle) && !(options.angle % 90)) {
1286
+ this.options.tileAngle = options.angle;
1287
+ } else {
1288
+ throw is.invalidParameterError('angle', 'positive/negative multiple of 90', options.angle);
1289
+ }
1290
+ }
1291
+ // Background colour
1292
+ this._setBackgroundColourOption('tileBackground', options.background);
1293
+ // Depth of tiles
1294
+ if (is.defined(options.depth)) {
1295
+ if (is.string(options.depth) && is.inArray(options.depth, ['onepixel', 'onetile', 'one'])) {
1296
+ this.options.tileDepth = options.depth;
1297
+ } else {
1298
+ throw is.invalidParameterError('depth', 'one of: onepixel, onetile, one', options.depth);
1299
+ }
1300
+ }
1301
+ // Threshold to skip blank tiles
1302
+ if (is.defined(options.skipBlanks)) {
1303
+ if (is.integer(options.skipBlanks) && is.inRange(options.skipBlanks, -1, 65535)) {
1304
+ this.options.tileSkipBlanks = options.skipBlanks;
1305
+ } else {
1306
+ throw is.invalidParameterError('skipBlanks', 'integer between -1 and 255/65535', options.skipBlanks);
1307
+ }
1308
+ } else if (is.defined(options.layout) && options.layout === 'google') {
1309
+ this.options.tileSkipBlanks = 5;
1310
+ }
1311
+ // Center image in tile
1312
+ const centre = is.bool(options.center) ? options.center : options.centre;
1313
+ if (is.defined(centre)) {
1314
+ this._setBooleanOption('tileCentre', centre);
1315
+ }
1316
+ // @id attribute for IIIF layout
1317
+ if (is.defined(options.id)) {
1318
+ if (is.string(options.id)) {
1319
+ this.options.tileId = options.id;
1320
+ } else {
1321
+ throw is.invalidParameterError('id', 'string', options.id);
1322
+ }
1323
+ }
1324
+ // Basename for zip container
1325
+ if (is.defined(options.basename)) {
1326
+ if (is.string(options.basename)) {
1327
+ this.options.tileBasename = options.basename;
1328
+ } else {
1329
+ throw is.invalidParameterError('basename', 'string', options.basename);
1330
+ }
1331
+ }
1332
+ }
1333
+ // Format
1334
+ if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) {
1335
+ this.options.tileFormat = this.options.formatOut;
1336
+ } else if (this.options.formatOut !== 'input') {
1337
+ throw is.invalidParameterError('format', 'one of: jpeg, png, webp', this.options.formatOut);
1338
+ }
1339
+ return this._updateFormatOut('dz');
1340
+ }
1341
+
1342
+ /**
1343
+ * Set a timeout for processing, in seconds.
1344
+ * Use a value of zero to continue processing indefinitely, the default behaviour.
1345
+ *
1346
+ * The clock starts when libvips opens an input image for processing.
1347
+ * Time spent waiting for a libuv thread to become available is not included.
1348
+ *
1349
+ * @example
1350
+ * // Ensure processing takes no longer than 3 seconds
1351
+ * try {
1352
+ * const data = await sharp(input)
1353
+ * .blur(1000)
1354
+ * .timeout({ seconds: 3 })
1355
+ * .toBuffer();
1356
+ * } catch (err) {
1357
+ * if (err.message.includes('timeout')) { ... }
1358
+ * }
1359
+ *
1360
+ * @since 0.29.2
1361
+ *
1362
+ * @param {Object} options
1363
+ * @param {number} options.seconds - Number of seconds after which processing will be stopped
1364
+ * @returns {Sharp}
1365
+ */
1366
+ function timeout (options) {
1367
+ if (!is.plainObject(options)) {
1368
+ throw is.invalidParameterError('options', 'object', options);
1369
+ }
1370
+ if (is.integer(options.seconds) && is.inRange(options.seconds, 0, 3600)) {
1371
+ this.options.timeoutSeconds = options.seconds;
1372
+ } else {
1373
+ throw is.invalidParameterError('seconds', 'integer between 0 and 3600', options.seconds);
1374
+ }
1375
+ return this;
1376
+ }
1377
+
1378
+ /**
1379
+ * Update the output format unless options.force is false,
1380
+ * in which case revert to input format.
1381
+ * @private
1382
+ * @param {string} formatOut
1383
+ * @param {Object} [options]
1384
+ * @param {boolean} [options.force=true] - force output format, otherwise attempt to use input format
1385
+ * @returns {Sharp}
1386
+ */
1387
+ function _updateFormatOut (formatOut, options) {
1388
+ if (!(is.object(options) && options.force === false)) {
1389
+ this.options.formatOut = formatOut;
1390
+ }
1391
+ return this;
1392
+ }
1393
+
1394
+ /**
1395
+ * Update a boolean attribute of the this.options Object.
1396
+ * @private
1397
+ * @param {string} key
1398
+ * @param {boolean} val
1399
+ * @throws {Error} Invalid key
1400
+ */
1401
+ function _setBooleanOption (key, val) {
1402
+ if (is.bool(val)) {
1403
+ this.options[key] = val;
1404
+ } else {
1405
+ throw is.invalidParameterError(key, 'boolean', val);
1406
+ }
1407
+ }
1408
+
1409
+ /**
1410
+ * Called by a WriteableStream to notify us it is ready for data.
1411
+ * @private
1412
+ */
1413
+ function _read () {
1414
+ /* istanbul ignore else */
1415
+ if (!this.options.streamOut) {
1416
+ this.options.streamOut = true;
1417
+ const stack = Error();
1418
+ this._pipeline(undefined, stack);
1419
+ }
1420
+ }
1421
+
1422
+ /**
1423
+ * Invoke the C++ image processing pipeline
1424
+ * Supports callback, stream and promise variants
1425
+ * @private
1426
+ */
1427
+ function _pipeline (callback, stack) {
1428
+ if (typeof callback === 'function') {
1429
+ // output=file/buffer
1430
+ if (this._isStreamInput()) {
1431
+ // output=file/buffer, input=stream
1432
+ this.on('finish', () => {
1433
+ this._flattenBufferIn();
1434
+ sharp.pipeline(this.options, (err, data, info) => {
1435
+ if (err) {
1436
+ callback(is.nativeError(err, stack));
1437
+ } else {
1438
+ callback(null, data, info);
1439
+ }
1440
+ });
1441
+ });
1442
+ } else {
1443
+ // output=file/buffer, input=file/buffer
1444
+ sharp.pipeline(this.options, (err, data, info) => {
1445
+ if (err) {
1446
+ callback(is.nativeError(err, stack));
1447
+ } else {
1448
+ callback(null, data, info);
1449
+ }
1450
+ });
1451
+ }
1452
+ return this;
1453
+ } else if (this.options.streamOut) {
1454
+ // output=stream
1455
+ if (this._isStreamInput()) {
1456
+ // output=stream, input=stream
1457
+ this.once('finish', () => {
1458
+ this._flattenBufferIn();
1459
+ sharp.pipeline(this.options, (err, data, info) => {
1460
+ if (err) {
1461
+ this.emit('error', is.nativeError(err, stack));
1462
+ } else {
1463
+ this.emit('info', info);
1464
+ this.push(data);
1465
+ }
1466
+ this.push(null);
1467
+ this.on('end', () => this.emit('close'));
1468
+ });
1469
+ });
1470
+ if (this.streamInFinished) {
1471
+ this.emit('finish');
1472
+ }
1473
+ } else {
1474
+ // output=stream, input=file/buffer
1475
+ sharp.pipeline(this.options, (err, data, info) => {
1476
+ if (err) {
1477
+ this.emit('error', is.nativeError(err, stack));
1478
+ } else {
1479
+ this.emit('info', info);
1480
+ this.push(data);
1481
+ }
1482
+ this.push(null);
1483
+ this.on('end', () => this.emit('close'));
1484
+ });
1485
+ }
1486
+ return this;
1487
+ } else {
1488
+ // output=promise
1489
+ if (this._isStreamInput()) {
1490
+ // output=promise, input=stream
1491
+ return new Promise((resolve, reject) => {
1492
+ this.once('finish', () => {
1493
+ this._flattenBufferIn();
1494
+ sharp.pipeline(this.options, (err, data, info) => {
1495
+ if (err) {
1496
+ reject(is.nativeError(err, stack));
1497
+ } else {
1498
+ if (this.options.resolveWithObject) {
1499
+ resolve({ data, info });
1500
+ } else {
1501
+ resolve(data);
1502
+ }
1503
+ }
1504
+ });
1505
+ });
1506
+ });
1507
+ } else {
1508
+ // output=promise, input=file/buffer
1509
+ return new Promise((resolve, reject) => {
1510
+ sharp.pipeline(this.options, (err, data, info) => {
1511
+ if (err) {
1512
+ reject(is.nativeError(err, stack));
1513
+ } else {
1514
+ if (this.options.resolveWithObject) {
1515
+ resolve({ data, info });
1516
+ } else {
1517
+ resolve(data);
1518
+ }
1519
+ }
1520
+ });
1521
+ });
1522
+ }
1523
+ }
1524
+ }
1525
+
1526
+ /**
1527
+ * Decorate the Sharp prototype with output-related functions.
1528
+ * @private
1529
+ */
1530
+ module.exports = function (Sharp) {
1531
+ Object.assign(Sharp.prototype, {
1532
+ // Public
1533
+ toFile,
1534
+ toBuffer,
1535
+ keepExif,
1536
+ withExif,
1537
+ withExifMerge,
1538
+ keepIccProfile,
1539
+ withIccProfile,
1540
+ keepMetadata,
1541
+ withMetadata,
1542
+ toFormat,
1543
+ jpeg,
1544
+ jp2,
1545
+ png,
1546
+ webp,
1547
+ tiff,
1548
+ avif,
1549
+ heif,
1550
+ jxl,
1551
+ gif,
1552
+ raw,
1553
+ tile,
1554
+ timeout,
1555
+ // Private
1556
+ _updateFormatOut,
1557
+ _setBooleanOption,
1558
+ _read,
1559
+ _pipeline
1560
+ });
1561
+ };