@revizly/sharp 0.33.2-revizly3

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.
package/lib/output.js ADDED
@@ -0,0 +1,1572 @@
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 bits per pixel.
508
+ *
509
+ * Indexed PNG input at 1, 2 or 4 bits per pixel is converted to 8 bits per pixel.
510
+ * Set `palette` to `true` for slower, indexed PNG output.
511
+ *
512
+ * For 16 bits per pixel output, convert to `rgb16` via
513
+ * {@link /api-colour#tocolourspace|toColourspace}.
514
+ *
515
+ * @example
516
+ * // Convert any input to full colour PNG output
517
+ * const data = await sharp(input)
518
+ * .png()
519
+ * .toBuffer();
520
+ *
521
+ * @example
522
+ * // Convert any input to indexed PNG output (slower)
523
+ * const data = await sharp(input)
524
+ * .png({ palette: true })
525
+ * .toBuffer();
526
+ *
527
+ * @example
528
+ * // Output 16 bits per pixel RGB(A)
529
+ * const data = await sharp(input)
530
+ * .toColourspace('rgb16')
531
+ * .png()
532
+ * .toBuffer();
533
+ *
534
+ * @param {Object} [options]
535
+ * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
536
+ * @param {number} [options.compressionLevel=6] - zlib compression level, 0 (fastest, largest) to 9 (slowest, smallest)
537
+ * @param {boolean} [options.adaptiveFiltering=false] - use adaptive row filtering
538
+ * @param {boolean} [options.palette=false] - quantise to a palette-based image with alpha transparency support
539
+ * @param {number} [options.quality=100] - use the lowest number of colours needed to achieve given quality, sets `palette` to `true`
540
+ * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest), sets `palette` to `true`
541
+ * @param {number} [options.colours=256] - maximum number of palette entries, sets `palette` to `true`
542
+ * @param {number} [options.colors=256] - alternative spelling of `options.colours`, sets `palette` to `true`
543
+ * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, sets `palette` to `true`
544
+ * @param {boolean} [options.force=true] - force PNG output, otherwise attempt to use input format
545
+ * @returns {Sharp}
546
+ * @throws {Error} Invalid options
547
+ */
548
+ function png (options) {
549
+ if (is.object(options)) {
550
+ if (is.defined(options.progressive)) {
551
+ this._setBooleanOption('pngProgressive', options.progressive);
552
+ }
553
+ if (is.defined(options.compressionLevel)) {
554
+ if (is.integer(options.compressionLevel) && is.inRange(options.compressionLevel, 0, 9)) {
555
+ this.options.pngCompressionLevel = options.compressionLevel;
556
+ } else {
557
+ throw is.invalidParameterError('compressionLevel', 'integer between 0 and 9', options.compressionLevel);
558
+ }
559
+ }
560
+ if (is.defined(options.adaptiveFiltering)) {
561
+ this._setBooleanOption('pngAdaptiveFiltering', options.adaptiveFiltering);
562
+ }
563
+ const colours = options.colours || options.colors;
564
+ if (is.defined(colours)) {
565
+ if (is.integer(colours) && is.inRange(colours, 2, 256)) {
566
+ this.options.pngBitdepth = bitdepthFromColourCount(colours);
567
+ } else {
568
+ throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
569
+ }
570
+ }
571
+ if (is.defined(options.palette)) {
572
+ this._setBooleanOption('pngPalette', options.palette);
573
+ } else if ([options.quality, options.effort, options.colours, options.colors, options.dither].some(is.defined)) {
574
+ this._setBooleanOption('pngPalette', true);
575
+ }
576
+ if (this.options.pngPalette) {
577
+ if (is.defined(options.quality)) {
578
+ if (is.integer(options.quality) && is.inRange(options.quality, 0, 100)) {
579
+ this.options.pngQuality = options.quality;
580
+ } else {
581
+ throw is.invalidParameterError('quality', 'integer between 0 and 100', options.quality);
582
+ }
583
+ }
584
+ if (is.defined(options.effort)) {
585
+ if (is.integer(options.effort) && is.inRange(options.effort, 1, 10)) {
586
+ this.options.pngEffort = options.effort;
587
+ } else {
588
+ throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort);
589
+ }
590
+ }
591
+ if (is.defined(options.dither)) {
592
+ if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
593
+ this.options.pngDither = options.dither;
594
+ } else {
595
+ throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither);
596
+ }
597
+ }
598
+ }
599
+ }
600
+ return this._updateFormatOut('png', options);
601
+ }
602
+
603
+ /**
604
+ * Use these WebP options for output image.
605
+ *
606
+ * @example
607
+ * // Convert any input to lossless WebP output
608
+ * const data = await sharp(input)
609
+ * .webp({ lossless: true })
610
+ * .toBuffer();
611
+ *
612
+ * @example
613
+ * // Optimise the file size of an animated WebP
614
+ * const outputWebp = await sharp(inputWebp, { animated: true })
615
+ * .webp({ effort: 6 })
616
+ * .toBuffer();
617
+ *
618
+ * @param {Object} [options] - output options
619
+ * @param {number} [options.quality=80] - quality, integer 1-100
620
+ * @param {number} [options.alphaQuality=100] - quality of alpha layer, integer 0-100
621
+ * @param {boolean} [options.lossless=false] - use lossless compression mode
622
+ * @param {boolean} [options.nearLossless=false] - use near_lossless compression mode
623
+ * @param {boolean} [options.smartSubsample=false] - use high quality chroma subsampling
624
+ * @param {string} [options.preset='default'] - named preset for preprocessing/filtering, one of: default, photo, picture, drawing, icon, text
625
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 6 (slowest)
626
+ * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
627
+ * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
628
+ * @param {boolean} [options.minSize=false] - prevent use of animation key frames to minimise file size (slow)
629
+ * @param {boolean} [options.mixed=false] - allow mixture of lossy and lossless animation frames (slow)
630
+ * @param {boolean} [options.force=true] - force WebP output, otherwise attempt to use input format
631
+ * @returns {Sharp}
632
+ * @throws {Error} Invalid options
633
+ */
634
+ function webp (options) {
635
+ if (is.object(options)) {
636
+ if (is.defined(options.quality)) {
637
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
638
+ this.options.webpQuality = options.quality;
639
+ } else {
640
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
641
+ }
642
+ }
643
+ if (is.defined(options.alphaQuality)) {
644
+ if (is.integer(options.alphaQuality) && is.inRange(options.alphaQuality, 0, 100)) {
645
+ this.options.webpAlphaQuality = options.alphaQuality;
646
+ } else {
647
+ throw is.invalidParameterError('alphaQuality', 'integer between 0 and 100', options.alphaQuality);
648
+ }
649
+ }
650
+ if (is.defined(options.lossless)) {
651
+ this._setBooleanOption('webpLossless', options.lossless);
652
+ }
653
+ if (is.defined(options.nearLossless)) {
654
+ this._setBooleanOption('webpNearLossless', options.nearLossless);
655
+ }
656
+ if (is.defined(options.smartSubsample)) {
657
+ this._setBooleanOption('webpSmartSubsample', options.smartSubsample);
658
+ }
659
+ if (is.defined(options.preset)) {
660
+ if (is.string(options.preset) && is.inArray(options.preset, ['default', 'photo', 'picture', 'drawing', 'icon', 'text'])) {
661
+ this.options.webpPreset = options.preset;
662
+ } else {
663
+ throw is.invalidParameterError('preset', 'one of: default, photo, picture, drawing, icon, text', options.preset);
664
+ }
665
+ }
666
+ if (is.defined(options.effort)) {
667
+ if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) {
668
+ this.options.webpEffort = options.effort;
669
+ } else {
670
+ throw is.invalidParameterError('effort', 'integer between 0 and 6', options.effort);
671
+ }
672
+ }
673
+ if (is.defined(options.minSize)) {
674
+ this._setBooleanOption('webpMinSize', options.minSize);
675
+ }
676
+ if (is.defined(options.mixed)) {
677
+ this._setBooleanOption('webpMixed', options.mixed);
678
+ }
679
+ }
680
+ trySetAnimationOptions(options, this.options);
681
+ return this._updateFormatOut('webp', options);
682
+ }
683
+
684
+ /**
685
+ * Use these GIF options for the output image.
686
+ *
687
+ * The first entry in the palette is reserved for transparency.
688
+ *
689
+ * The palette of the input image will be re-used if possible.
690
+ *
691
+ * @since 0.30.0
692
+ *
693
+ * @example
694
+ * // Convert PNG to GIF
695
+ * await sharp(pngBuffer)
696
+ * .gif()
697
+ * .toBuffer();
698
+ *
699
+ * @example
700
+ * // Convert animated WebP to animated GIF
701
+ * await sharp('animated.webp', { animated: true })
702
+ * .toFile('animated.gif');
703
+ *
704
+ * @example
705
+ * // Create a 128x128, cropped, non-dithered, animated thumbnail of an animated GIF
706
+ * const out = await sharp('in.gif', { animated: true })
707
+ * .resize({ width: 128, height: 128 })
708
+ * .gif({ dither: 0 })
709
+ * .toBuffer();
710
+ *
711
+ * @example
712
+ * // Lossy file size reduction of animated GIF
713
+ * await sharp('in.gif', { animated: true })
714
+ * .gif({ interFrameMaxError: 8 })
715
+ * .toFile('optim.gif');
716
+ *
717
+ * @param {Object} [options] - output options
718
+ * @param {boolean} [options.reuse=true] - re-use existing palette, otherwise generate new (slow)
719
+ * @param {boolean} [options.progressive=false] - use progressive (interlace) scan
720
+ * @param {number} [options.colours=256] - maximum number of palette entries, including transparency, between 2 and 256
721
+ * @param {number} [options.colors=256] - alternative spelling of `options.colours`
722
+ * @param {number} [options.effort=7] - CPU effort, between 1 (fastest) and 10 (slowest)
723
+ * @param {number} [options.dither=1.0] - level of Floyd-Steinberg error diffusion, between 0 (least) and 1 (most)
724
+ * @param {number} [options.interFrameMaxError=0] - maximum inter-frame error for transparency, between 0 (lossless) and 32
725
+ * @param {number} [options.interPaletteMaxError=3] - maximum inter-palette error for palette reuse, between 0 and 256
726
+ * @param {number} [options.loop=0] - number of animation iterations, use 0 for infinite animation
727
+ * @param {number|number[]} [options.delay] - delay(s) between animation frames (in milliseconds)
728
+ * @param {boolean} [options.force=true] - force GIF output, otherwise attempt to use input format
729
+ * @returns {Sharp}
730
+ * @throws {Error} Invalid options
731
+ */
732
+ function gif (options) {
733
+ if (is.object(options)) {
734
+ if (is.defined(options.reuse)) {
735
+ this._setBooleanOption('gifReuse', options.reuse);
736
+ }
737
+ if (is.defined(options.progressive)) {
738
+ this._setBooleanOption('gifProgressive', options.progressive);
739
+ }
740
+ const colours = options.colours || options.colors;
741
+ if (is.defined(colours)) {
742
+ if (is.integer(colours) && is.inRange(colours, 2, 256)) {
743
+ this.options.gifBitdepth = bitdepthFromColourCount(colours);
744
+ } else {
745
+ throw is.invalidParameterError('colours', 'integer between 2 and 256', colours);
746
+ }
747
+ }
748
+ if (is.defined(options.effort)) {
749
+ if (is.number(options.effort) && is.inRange(options.effort, 1, 10)) {
750
+ this.options.gifEffort = options.effort;
751
+ } else {
752
+ throw is.invalidParameterError('effort', 'integer between 1 and 10', options.effort);
753
+ }
754
+ }
755
+ if (is.defined(options.dither)) {
756
+ if (is.number(options.dither) && is.inRange(options.dither, 0, 1)) {
757
+ this.options.gifDither = options.dither;
758
+ } else {
759
+ throw is.invalidParameterError('dither', 'number between 0.0 and 1.0', options.dither);
760
+ }
761
+ }
762
+ if (is.defined(options.interFrameMaxError)) {
763
+ if (is.number(options.interFrameMaxError) && is.inRange(options.interFrameMaxError, 0, 32)) {
764
+ this.options.gifInterFrameMaxError = options.interFrameMaxError;
765
+ } else {
766
+ throw is.invalidParameterError('interFrameMaxError', 'number between 0.0 and 32.0', options.interFrameMaxError);
767
+ }
768
+ }
769
+ if (is.defined(options.interPaletteMaxError)) {
770
+ if (is.number(options.interPaletteMaxError) && is.inRange(options.interPaletteMaxError, 0, 256)) {
771
+ this.options.gifInterPaletteMaxError = options.interPaletteMaxError;
772
+ } else {
773
+ throw is.invalidParameterError('interPaletteMaxError', 'number between 0.0 and 256.0', options.interPaletteMaxError);
774
+ }
775
+ }
776
+ }
777
+ trySetAnimationOptions(options, this.options);
778
+ return this._updateFormatOut('gif', options);
779
+ }
780
+
781
+ /* istanbul ignore next */
782
+ /**
783
+ * Use these JP2 options for output image.
784
+ *
785
+ * Requires libvips compiled with support for OpenJPEG.
786
+ * The prebuilt binaries do not include this - see
787
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
788
+ *
789
+ * @example
790
+ * // Convert any input to lossless JP2 output
791
+ * const data = await sharp(input)
792
+ * .jp2({ lossless: true })
793
+ * .toBuffer();
794
+ *
795
+ * @example
796
+ * // Convert any input to very high quality JP2 output
797
+ * const data = await sharp(input)
798
+ * .jp2({
799
+ * quality: 100,
800
+ * chromaSubsampling: '4:4:4'
801
+ * })
802
+ * .toBuffer();
803
+ *
804
+ * @since 0.29.1
805
+ *
806
+ * @param {Object} [options] - output options
807
+ * @param {number} [options.quality=80] - quality, integer 1-100
808
+ * @param {boolean} [options.lossless=false] - use lossless compression mode
809
+ * @param {number} [options.tileWidth=512] - horizontal tile size
810
+ * @param {number} [options.tileHeight=512] - vertical tile size
811
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
812
+ * @returns {Sharp}
813
+ * @throws {Error} Invalid options
814
+ */
815
+ function jp2 (options) {
816
+ if (!this.constructor.format.jp2k.output.buffer) {
817
+ throw errJp2Save();
818
+ }
819
+ if (is.object(options)) {
820
+ if (is.defined(options.quality)) {
821
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
822
+ this.options.jp2Quality = options.quality;
823
+ } else {
824
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
825
+ }
826
+ }
827
+ if (is.defined(options.lossless)) {
828
+ if (is.bool(options.lossless)) {
829
+ this.options.jp2Lossless = options.lossless;
830
+ } else {
831
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
832
+ }
833
+ }
834
+ if (is.defined(options.tileWidth)) {
835
+ if (is.integer(options.tileWidth) && is.inRange(options.tileWidth, 1, 32768)) {
836
+ this.options.jp2TileWidth = options.tileWidth;
837
+ } else {
838
+ throw is.invalidParameterError('tileWidth', 'integer between 1 and 32768', options.tileWidth);
839
+ }
840
+ }
841
+ if (is.defined(options.tileHeight)) {
842
+ if (is.integer(options.tileHeight) && is.inRange(options.tileHeight, 1, 32768)) {
843
+ this.options.jp2TileHeight = options.tileHeight;
844
+ } else {
845
+ throw is.invalidParameterError('tileHeight', 'integer between 1 and 32768', options.tileHeight);
846
+ }
847
+ }
848
+ if (is.defined(options.chromaSubsampling)) {
849
+ if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
850
+ this.options.jp2ChromaSubsampling = options.chromaSubsampling;
851
+ } else {
852
+ throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
853
+ }
854
+ }
855
+ }
856
+ return this._updateFormatOut('jp2', options);
857
+ }
858
+
859
+ /**
860
+ * Set animation options if available.
861
+ * @private
862
+ *
863
+ * @param {Object} [source] - output options
864
+ * @param {number} [source.loop=0] - number of animation iterations, use 0 for infinite animation
865
+ * @param {number[]} [source.delay] - list of delays between animation frames (in milliseconds)
866
+ * @param {Object} [target] - target object for valid options
867
+ * @throws {Error} Invalid options
868
+ */
869
+ function trySetAnimationOptions (source, target) {
870
+ if (is.object(source) && is.defined(source.loop)) {
871
+ if (is.integer(source.loop) && is.inRange(source.loop, 0, 65535)) {
872
+ target.loop = source.loop;
873
+ } else {
874
+ throw is.invalidParameterError('loop', 'integer between 0 and 65535', source.loop);
875
+ }
876
+ }
877
+ if (is.object(source) && is.defined(source.delay)) {
878
+ // We allow singular values as well
879
+ if (is.integer(source.delay) && is.inRange(source.delay, 0, 65535)) {
880
+ target.delay = [source.delay];
881
+ } else if (
882
+ Array.isArray(source.delay) &&
883
+ source.delay.every(is.integer) &&
884
+ source.delay.every(v => is.inRange(v, 0, 65535))) {
885
+ target.delay = source.delay;
886
+ } else {
887
+ throw is.invalidParameterError('delay', 'integer or an array of integers between 0 and 65535', source.delay);
888
+ }
889
+ }
890
+ }
891
+
892
+ /**
893
+ * Use these TIFF options for output image.
894
+ *
895
+ * The `density` can be set in pixels/inch via {@link #withmetadata|withMetadata}
896
+ * instead of providing `xres` and `yres` in pixels/mm.
897
+ *
898
+ * @example
899
+ * // Convert SVG input to LZW-compressed, 1 bit per pixel TIFF output
900
+ * sharp('input.svg')
901
+ * .tiff({
902
+ * compression: 'lzw',
903
+ * bitdepth: 1
904
+ * })
905
+ * .toFile('1-bpp-output.tiff')
906
+ * .then(info => { ... });
907
+ *
908
+ * @param {Object} [options] - output options
909
+ * @param {number} [options.quality=80] - quality, integer 1-100
910
+ * @param {boolean} [options.force=true] - force TIFF output, otherwise attempt to use input format
911
+ * @param {string} [options.compression='jpeg'] - compression options: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k
912
+ * @param {string} [options.predictor='horizontal'] - compression predictor options: none, horizontal, float
913
+ * @param {boolean} [options.pyramid=false] - write an image pyramid
914
+ * @param {boolean} [options.tile=false] - write a tiled tiff
915
+ * @param {number} [options.tileWidth=256] - horizontal tile size
916
+ * @param {number} [options.tileHeight=256] - vertical tile size
917
+ * @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm
918
+ * @param {number} [options.yres=1.0] - vertical resolution in pixels/mm
919
+ * @param {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm
920
+ * @param {number} [options.bitdepth=8] - reduce bitdepth to 1, 2 or 4 bit
921
+ * @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite
922
+ * @returns {Sharp}
923
+ * @throws {Error} Invalid options
924
+ */
925
+ function tiff (options) {
926
+ if (is.object(options)) {
927
+ if (is.defined(options.quality)) {
928
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
929
+ this.options.tiffQuality = options.quality;
930
+ } else {
931
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
932
+ }
933
+ }
934
+ if (is.defined(options.bitdepth)) {
935
+ if (is.integer(options.bitdepth) && is.inArray(options.bitdepth, [1, 2, 4, 8])) {
936
+ this.options.tiffBitdepth = options.bitdepth;
937
+ } else {
938
+ throw is.invalidParameterError('bitdepth', '1, 2, 4 or 8', options.bitdepth);
939
+ }
940
+ }
941
+ // tiling
942
+ if (is.defined(options.tile)) {
943
+ this._setBooleanOption('tiffTile', options.tile);
944
+ }
945
+ if (is.defined(options.tileWidth)) {
946
+ if (is.integer(options.tileWidth) && options.tileWidth > 0) {
947
+ this.options.tiffTileWidth = options.tileWidth;
948
+ } else {
949
+ throw is.invalidParameterError('tileWidth', 'integer greater than zero', options.tileWidth);
950
+ }
951
+ }
952
+ if (is.defined(options.tileHeight)) {
953
+ if (is.integer(options.tileHeight) && options.tileHeight > 0) {
954
+ this.options.tiffTileHeight = options.tileHeight;
955
+ } else {
956
+ throw is.invalidParameterError('tileHeight', 'integer greater than zero', options.tileHeight);
957
+ }
958
+ }
959
+ // miniswhite
960
+ if (is.defined(options.miniswhite)) {
961
+ this._setBooleanOption('tiffMiniswhite', options.miniswhite);
962
+ }
963
+ // pyramid
964
+ if (is.defined(options.pyramid)) {
965
+ this._setBooleanOption('tiffPyramid', options.pyramid);
966
+ }
967
+ // resolution
968
+ if (is.defined(options.xres)) {
969
+ if (is.number(options.xres) && options.xres > 0) {
970
+ this.options.tiffXres = options.xres;
971
+ } else {
972
+ throw is.invalidParameterError('xres', 'number greater than zero', options.xres);
973
+ }
974
+ }
975
+ if (is.defined(options.yres)) {
976
+ if (is.number(options.yres) && options.yres > 0) {
977
+ this.options.tiffYres = options.yres;
978
+ } else {
979
+ throw is.invalidParameterError('yres', 'number greater than zero', options.yres);
980
+ }
981
+ }
982
+ // compression
983
+ if (is.defined(options.compression)) {
984
+ if (is.string(options.compression) && is.inArray(options.compression, ['none', 'jpeg', 'deflate', 'packbits', 'ccittfax4', 'lzw', 'webp', 'zstd', 'jp2k'])) {
985
+ this.options.tiffCompression = options.compression;
986
+ } else {
987
+ throw is.invalidParameterError('compression', 'one of: none, jpeg, deflate, packbits, ccittfax4, lzw, webp, zstd, jp2k', options.compression);
988
+ }
989
+ }
990
+ // predictor
991
+ if (is.defined(options.predictor)) {
992
+ if (is.string(options.predictor) && is.inArray(options.predictor, ['none', 'horizontal', 'float'])) {
993
+ this.options.tiffPredictor = options.predictor;
994
+ } else {
995
+ throw is.invalidParameterError('predictor', 'one of: none, horizontal, float', options.predictor);
996
+ }
997
+ }
998
+ // resolutionUnit
999
+ if (is.defined(options.resolutionUnit)) {
1000
+ if (is.string(options.resolutionUnit) && is.inArray(options.resolutionUnit, ['inch', 'cm'])) {
1001
+ this.options.tiffResolutionUnit = options.resolutionUnit;
1002
+ } else {
1003
+ throw is.invalidParameterError('resolutionUnit', 'one of: inch, cm', options.resolutionUnit);
1004
+ }
1005
+ }
1006
+ }
1007
+ return this._updateFormatOut('tiff', options);
1008
+ }
1009
+
1010
+ /**
1011
+ * Use these AVIF options for output image.
1012
+ *
1013
+ * AVIF image sequences are not supported.
1014
+ *
1015
+ * @example
1016
+ * const data = await sharp(input)
1017
+ * .avif({ effort: 2 })
1018
+ * .toBuffer();
1019
+ *
1020
+ * @example
1021
+ * const data = await sharp(input)
1022
+ * .avif({ lossless: true })
1023
+ * .toBuffer();
1024
+ *
1025
+ * @since 0.27.0
1026
+ *
1027
+ * @param {Object} [options] - output options
1028
+ * @param {number} [options.quality=50] - quality, integer 1-100
1029
+ * @param {boolean} [options.lossless=false] - use lossless compression
1030
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest)
1031
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
1032
+ * @returns {Sharp}
1033
+ * @throws {Error} Invalid options
1034
+ */
1035
+ function avif (options) {
1036
+ return this.heif({ ...options, compression: 'av1' });
1037
+ }
1038
+
1039
+ /**
1040
+ * Use these HEIF options for output image.
1041
+ *
1042
+ * Support for patent-encumbered HEIC images using `hevc` compression requires the use of a
1043
+ * globally-installed libvips compiled with support for libheif, libde265 and x265.
1044
+ *
1045
+ * @example
1046
+ * const data = await sharp(input)
1047
+ * .heif({ compression: 'hevc' })
1048
+ * .toBuffer();
1049
+ *
1050
+ * @since 0.23.0
1051
+ *
1052
+ * @param {Object} options - output options
1053
+ * @param {string} options.compression - compression format: av1, hevc
1054
+ * @param {number} [options.quality=50] - quality, integer 1-100
1055
+ * @param {boolean} [options.lossless=false] - use lossless compression
1056
+ * @param {number} [options.effort=4] - CPU effort, between 0 (fastest) and 9 (slowest)
1057
+ * @param {string} [options.chromaSubsampling='4:4:4'] - set to '4:2:0' to use chroma subsampling
1058
+ * @returns {Sharp}
1059
+ * @throws {Error} Invalid options
1060
+ */
1061
+ function heif (options) {
1062
+ if (is.object(options)) {
1063
+ if (is.string(options.compression) && is.inArray(options.compression, ['av1', 'hevc'])) {
1064
+ this.options.heifCompression = options.compression;
1065
+ } else {
1066
+ throw is.invalidParameterError('compression', 'one of: av1, hevc', options.compression);
1067
+ }
1068
+ if (is.defined(options.quality)) {
1069
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
1070
+ this.options.heifQuality = options.quality;
1071
+ } else {
1072
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
1073
+ }
1074
+ }
1075
+ if (is.defined(options.lossless)) {
1076
+ if (is.bool(options.lossless)) {
1077
+ this.options.heifLossless = options.lossless;
1078
+ } else {
1079
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
1080
+ }
1081
+ }
1082
+ if (is.defined(options.effort)) {
1083
+ if (is.integer(options.effort) && is.inRange(options.effort, 0, 9)) {
1084
+ this.options.heifEffort = options.effort;
1085
+ } else {
1086
+ throw is.invalidParameterError('effort', 'integer between 0 and 9', options.effort);
1087
+ }
1088
+ }
1089
+ if (is.defined(options.chromaSubsampling)) {
1090
+ if (is.string(options.chromaSubsampling) && is.inArray(options.chromaSubsampling, ['4:2:0', '4:4:4'])) {
1091
+ this.options.heifChromaSubsampling = options.chromaSubsampling;
1092
+ } else {
1093
+ throw is.invalidParameterError('chromaSubsampling', 'one of: 4:2:0, 4:4:4', options.chromaSubsampling);
1094
+ }
1095
+ }
1096
+ } else {
1097
+ throw is.invalidParameterError('options', 'Object', options);
1098
+ }
1099
+ return this._updateFormatOut('heif', options);
1100
+ }
1101
+
1102
+ /**
1103
+ * Use these JPEG-XL (JXL) options for output image.
1104
+ *
1105
+ * This feature is experimental, please do not use in production systems.
1106
+ *
1107
+ * Requires libvips compiled with support for libjxl.
1108
+ * The prebuilt binaries do not include this - see
1109
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
1110
+ *
1111
+ * Image metadata (EXIF, XMP) is unsupported.
1112
+ *
1113
+ * @since 0.31.3
1114
+ *
1115
+ * @param {Object} [options] - output options
1116
+ * @param {number} [options.distance=1.0] - maximum encoding error, between 0 (highest quality) and 15 (lowest quality)
1117
+ * @param {number} [options.quality] - calculate `distance` based on JPEG-like quality, between 1 and 100, overrides distance if specified
1118
+ * @param {number} [options.decodingTier=0] - target decode speed tier, between 0 (highest quality) and 4 (lowest quality)
1119
+ * @param {boolean} [options.lossless=false] - use lossless compression
1120
+ * @param {number} [options.effort=7] - CPU effort, between 3 (fastest) and 9 (slowest)
1121
+ * @returns {Sharp}
1122
+ * @throws {Error} Invalid options
1123
+ */
1124
+ function jxl (options) {
1125
+ if (is.object(options)) {
1126
+ if (is.defined(options.quality)) {
1127
+ if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
1128
+ // https://github.com/libjxl/libjxl/blob/0aeea7f180bafd6893c1db8072dcb67d2aa5b03d/tools/cjxl_main.cc#L640-L644
1129
+ this.options.jxlDistance = options.quality >= 30
1130
+ ? 0.1 + (100 - options.quality) * 0.09
1131
+ : 53 / 3000 * options.quality * options.quality - 23 / 20 * options.quality + 25;
1132
+ } else {
1133
+ throw is.invalidParameterError('quality', 'integer between 1 and 100', options.quality);
1134
+ }
1135
+ } else if (is.defined(options.distance)) {
1136
+ if (is.number(options.distance) && is.inRange(options.distance, 0, 15)) {
1137
+ this.options.jxlDistance = options.distance;
1138
+ } else {
1139
+ throw is.invalidParameterError('distance', 'number between 0.0 and 15.0', options.distance);
1140
+ }
1141
+ }
1142
+ if (is.defined(options.decodingTier)) {
1143
+ if (is.integer(options.decodingTier) && is.inRange(options.decodingTier, 0, 4)) {
1144
+ this.options.jxlDecodingTier = options.decodingTier;
1145
+ } else {
1146
+ throw is.invalidParameterError('decodingTier', 'integer between 0 and 4', options.decodingTier);
1147
+ }
1148
+ }
1149
+ if (is.defined(options.lossless)) {
1150
+ if (is.bool(options.lossless)) {
1151
+ this.options.jxlLossless = options.lossless;
1152
+ } else {
1153
+ throw is.invalidParameterError('lossless', 'boolean', options.lossless);
1154
+ }
1155
+ }
1156
+ if (is.defined(options.effort)) {
1157
+ if (is.integer(options.effort) && is.inRange(options.effort, 3, 9)) {
1158
+ this.options.jxlEffort = options.effort;
1159
+ } else {
1160
+ throw is.invalidParameterError('effort', 'integer between 3 and 9', options.effort);
1161
+ }
1162
+ }
1163
+ }
1164
+ return this._updateFormatOut('jxl', options);
1165
+ }
1166
+
1167
+ /**
1168
+ * Force output to be raw, uncompressed pixel data.
1169
+ * Pixel ordering is left-to-right, top-to-bottom, without padding.
1170
+ * Channel ordering will be RGB or RGBA for non-greyscale colourspaces.
1171
+ *
1172
+ * @example
1173
+ * // Extract raw, unsigned 8-bit RGB pixel data from JPEG input
1174
+ * const { data, info } = await sharp('input.jpg')
1175
+ * .raw()
1176
+ * .toBuffer({ resolveWithObject: true });
1177
+ *
1178
+ * @example
1179
+ * // Extract alpha channel as raw, unsigned 16-bit pixel data from PNG input
1180
+ * const data = await sharp('input.png')
1181
+ * .ensureAlpha()
1182
+ * .extractChannel(3)
1183
+ * .toColourspace('b-w')
1184
+ * .raw({ depth: 'ushort' })
1185
+ * .toBuffer();
1186
+ *
1187
+ * @param {Object} [options] - output options
1188
+ * @param {string} [options.depth='uchar'] - bit depth, one of: char, uchar (default), short, ushort, int, uint, float, complex, double, dpcomplex
1189
+ * @returns {Sharp}
1190
+ * @throws {Error} Invalid options
1191
+ */
1192
+ function raw (options) {
1193
+ if (is.object(options)) {
1194
+ if (is.defined(options.depth)) {
1195
+ if (is.string(options.depth) && is.inArray(options.depth,
1196
+ ['char', 'uchar', 'short', 'ushort', 'int', 'uint', 'float', 'complex', 'double', 'dpcomplex']
1197
+ )) {
1198
+ this.options.rawDepth = options.depth;
1199
+ } else {
1200
+ throw is.invalidParameterError('depth', 'one of: char, uchar, short, ushort, int, uint, float, complex, double, dpcomplex', options.depth);
1201
+ }
1202
+ }
1203
+ }
1204
+ return this._updateFormatOut('raw');
1205
+ }
1206
+
1207
+ /**
1208
+ * Use tile-based deep zoom (image pyramid) output.
1209
+ *
1210
+ * Set the format and options for tile images via the `toFormat`, `jpeg`, `png` or `webp` functions.
1211
+ * Use a `.zip` or `.szi` file extension with `toFile` to write to a compressed archive file format.
1212
+ *
1213
+ * The container will be set to `zip` when the output is a Buffer or Stream, otherwise it will default to `fs`.
1214
+ *
1215
+ * Requires libvips compiled with support for libgsf.
1216
+ * The prebuilt binaries do not include this - see
1217
+ * {@link https://sharp.pixelplumbing.com/install#custom-libvips installing a custom libvips}.
1218
+ *
1219
+ * @example
1220
+ * sharp('input.tiff')
1221
+ * .png()
1222
+ * .tile({
1223
+ * size: 512
1224
+ * })
1225
+ * .toFile('output.dz', function(err, info) {
1226
+ * // output.dzi is the Deep Zoom XML definition
1227
+ * // output_files contains 512x512 tiles grouped by zoom level
1228
+ * });
1229
+ *
1230
+ * @example
1231
+ * const zipFileWithTiles = await sharp(input)
1232
+ * .tile({ basename: "tiles" })
1233
+ * .toBuffer();
1234
+ *
1235
+ * @example
1236
+ * const iiififier = sharp().tile({ layout: "iiif" });
1237
+ * readableStream
1238
+ * .pipe(iiififier)
1239
+ * .pipe(writeableStream);
1240
+ *
1241
+ * @param {Object} [options]
1242
+ * @param {number} [options.size=256] tile size in pixels, a value between 1 and 8192.
1243
+ * @param {number} [options.overlap=0] tile overlap in pixels, a value between 0 and 8192.
1244
+ * @param {number} [options.angle=0] tile angle of rotation, must be a multiple of 90.
1245
+ * @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.
1246
+ * @param {string} [options.depth] how deep to make the pyramid, possible values are `onepixel`, `onetile` or `one`, default based on layout.
1247
+ * @param {number} [options.skipBlanks=-1] Threshold to skip tile generation. Range is 0-255 for 8-bit images, 0-65535 for 16-bit images. Default is 5 for `google` layout, -1 (no skip) otherwise.
1248
+ * @param {string} [options.container='fs'] tile container, with value `fs` (filesystem) or `zip` (compressed file).
1249
+ * @param {string} [options.layout='dz'] filesystem layout, possible values are `dz`, `iiif`, `iiif3`, `zoomify` or `google`.
1250
+ * @param {boolean} [options.centre=false] centre image in tile.
1251
+ * @param {boolean} [options.center=false] alternative spelling of centre.
1252
+ * @param {string} [options.id='https://example.com/iiif'] when `layout` is `iiif`/`iiif3`, sets the `@id`/`id` attribute of `info.json`
1253
+ * @param {string} [options.basename] the name of the directory within the zip file when container is `zip`.
1254
+ * @returns {Sharp}
1255
+ * @throws {Error} Invalid parameters
1256
+ */
1257
+ function tile (options) {
1258
+ if (is.object(options)) {
1259
+ // Size of square tiles, in pixels
1260
+ if (is.defined(options.size)) {
1261
+ if (is.integer(options.size) && is.inRange(options.size, 1, 8192)) {
1262
+ this.options.tileSize = options.size;
1263
+ } else {
1264
+ throw is.invalidParameterError('size', 'integer between 1 and 8192', options.size);
1265
+ }
1266
+ }
1267
+ // Overlap of tiles, in pixels
1268
+ if (is.defined(options.overlap)) {
1269
+ if (is.integer(options.overlap) && is.inRange(options.overlap, 0, 8192)) {
1270
+ if (options.overlap > this.options.tileSize) {
1271
+ throw is.invalidParameterError('overlap', `<= size (${this.options.tileSize})`, options.overlap);
1272
+ }
1273
+ this.options.tileOverlap = options.overlap;
1274
+ } else {
1275
+ throw is.invalidParameterError('overlap', 'integer between 0 and 8192', options.overlap);
1276
+ }
1277
+ }
1278
+ // Container
1279
+ if (is.defined(options.container)) {
1280
+ if (is.string(options.container) && is.inArray(options.container, ['fs', 'zip'])) {
1281
+ this.options.tileContainer = options.container;
1282
+ } else {
1283
+ throw is.invalidParameterError('container', 'one of: fs, zip', options.container);
1284
+ }
1285
+ }
1286
+ // Layout
1287
+ if (is.defined(options.layout)) {
1288
+ if (is.string(options.layout) && is.inArray(options.layout, ['dz', 'google', 'iiif', 'iiif3', 'zoomify'])) {
1289
+ this.options.tileLayout = options.layout;
1290
+ } else {
1291
+ throw is.invalidParameterError('layout', 'one of: dz, google, iiif, iiif3, zoomify', options.layout);
1292
+ }
1293
+ }
1294
+ // Angle of rotation,
1295
+ if (is.defined(options.angle)) {
1296
+ if (is.integer(options.angle) && !(options.angle % 90)) {
1297
+ this.options.tileAngle = options.angle;
1298
+ } else {
1299
+ throw is.invalidParameterError('angle', 'positive/negative multiple of 90', options.angle);
1300
+ }
1301
+ }
1302
+ // Background colour
1303
+ this._setBackgroundColourOption('tileBackground', options.background);
1304
+ // Depth of tiles
1305
+ if (is.defined(options.depth)) {
1306
+ if (is.string(options.depth) && is.inArray(options.depth, ['onepixel', 'onetile', 'one'])) {
1307
+ this.options.tileDepth = options.depth;
1308
+ } else {
1309
+ throw is.invalidParameterError('depth', 'one of: onepixel, onetile, one', options.depth);
1310
+ }
1311
+ }
1312
+ // Threshold to skip blank tiles
1313
+ if (is.defined(options.skipBlanks)) {
1314
+ if (is.integer(options.skipBlanks) && is.inRange(options.skipBlanks, -1, 65535)) {
1315
+ this.options.tileSkipBlanks = options.skipBlanks;
1316
+ } else {
1317
+ throw is.invalidParameterError('skipBlanks', 'integer between -1 and 255/65535', options.skipBlanks);
1318
+ }
1319
+ } else if (is.defined(options.layout) && options.layout === 'google') {
1320
+ this.options.tileSkipBlanks = 5;
1321
+ }
1322
+ // Center image in tile
1323
+ const centre = is.bool(options.center) ? options.center : options.centre;
1324
+ if (is.defined(centre)) {
1325
+ this._setBooleanOption('tileCentre', centre);
1326
+ }
1327
+ // @id attribute for IIIF layout
1328
+ if (is.defined(options.id)) {
1329
+ if (is.string(options.id)) {
1330
+ this.options.tileId = options.id;
1331
+ } else {
1332
+ throw is.invalidParameterError('id', 'string', options.id);
1333
+ }
1334
+ }
1335
+ // Basename for zip container
1336
+ if (is.defined(options.basename)) {
1337
+ if (is.string(options.basename)) {
1338
+ this.options.tileBasename = options.basename;
1339
+ } else {
1340
+ throw is.invalidParameterError('basename', 'string', options.basename);
1341
+ }
1342
+ }
1343
+ }
1344
+ // Format
1345
+ if (is.inArray(this.options.formatOut, ['jpeg', 'png', 'webp'])) {
1346
+ this.options.tileFormat = this.options.formatOut;
1347
+ } else if (this.options.formatOut !== 'input') {
1348
+ throw is.invalidParameterError('format', 'one of: jpeg, png, webp', this.options.formatOut);
1349
+ }
1350
+ return this._updateFormatOut('dz');
1351
+ }
1352
+
1353
+ /**
1354
+ * Set a timeout for processing, in seconds.
1355
+ * Use a value of zero to continue processing indefinitely, the default behaviour.
1356
+ *
1357
+ * The clock starts when libvips opens an input image for processing.
1358
+ * Time spent waiting for a libuv thread to become available is not included.
1359
+ *
1360
+ * @example
1361
+ * // Ensure processing takes no longer than 3 seconds
1362
+ * try {
1363
+ * const data = await sharp(input)
1364
+ * .blur(1000)
1365
+ * .timeout({ seconds: 3 })
1366
+ * .toBuffer();
1367
+ * } catch (err) {
1368
+ * if (err.message.includes('timeout')) { ... }
1369
+ * }
1370
+ *
1371
+ * @since 0.29.2
1372
+ *
1373
+ * @param {Object} options
1374
+ * @param {number} options.seconds - Number of seconds after which processing will be stopped
1375
+ * @returns {Sharp}
1376
+ */
1377
+ function timeout (options) {
1378
+ if (!is.plainObject(options)) {
1379
+ throw is.invalidParameterError('options', 'object', options);
1380
+ }
1381
+ if (is.integer(options.seconds) && is.inRange(options.seconds, 0, 3600)) {
1382
+ this.options.timeoutSeconds = options.seconds;
1383
+ } else {
1384
+ throw is.invalidParameterError('seconds', 'integer between 0 and 3600', options.seconds);
1385
+ }
1386
+ return this;
1387
+ }
1388
+
1389
+ /**
1390
+ * Update the output format unless options.force is false,
1391
+ * in which case revert to input format.
1392
+ * @private
1393
+ * @param {string} formatOut
1394
+ * @param {Object} [options]
1395
+ * @param {boolean} [options.force=true] - force output format, otherwise attempt to use input format
1396
+ * @returns {Sharp}
1397
+ */
1398
+ function _updateFormatOut (formatOut, options) {
1399
+ if (!(is.object(options) && options.force === false)) {
1400
+ this.options.formatOut = formatOut;
1401
+ }
1402
+ return this;
1403
+ }
1404
+
1405
+ /**
1406
+ * Update a boolean attribute of the this.options Object.
1407
+ * @private
1408
+ * @param {string} key
1409
+ * @param {boolean} val
1410
+ * @throws {Error} Invalid key
1411
+ */
1412
+ function _setBooleanOption (key, val) {
1413
+ if (is.bool(val)) {
1414
+ this.options[key] = val;
1415
+ } else {
1416
+ throw is.invalidParameterError(key, 'boolean', val);
1417
+ }
1418
+ }
1419
+
1420
+ /**
1421
+ * Called by a WriteableStream to notify us it is ready for data.
1422
+ * @private
1423
+ */
1424
+ function _read () {
1425
+ /* istanbul ignore else */
1426
+ if (!this.options.streamOut) {
1427
+ this.options.streamOut = true;
1428
+ const stack = Error();
1429
+ this._pipeline(undefined, stack);
1430
+ }
1431
+ }
1432
+
1433
+ /**
1434
+ * Invoke the C++ image processing pipeline
1435
+ * Supports callback, stream and promise variants
1436
+ * @private
1437
+ */
1438
+ function _pipeline (callback, stack) {
1439
+ if (typeof callback === 'function') {
1440
+ // output=file/buffer
1441
+ if (this._isStreamInput()) {
1442
+ // output=file/buffer, input=stream
1443
+ this.on('finish', () => {
1444
+ this._flattenBufferIn();
1445
+ sharp.pipeline(this.options, (err, data, info) => {
1446
+ if (err) {
1447
+ callback(is.nativeError(err, stack));
1448
+ } else {
1449
+ callback(null, data, info);
1450
+ }
1451
+ });
1452
+ });
1453
+ } else {
1454
+ // output=file/buffer, input=file/buffer
1455
+ sharp.pipeline(this.options, (err, data, info) => {
1456
+ if (err) {
1457
+ callback(is.nativeError(err, stack));
1458
+ } else {
1459
+ callback(null, data, info);
1460
+ }
1461
+ });
1462
+ }
1463
+ return this;
1464
+ } else if (this.options.streamOut) {
1465
+ // output=stream
1466
+ if (this._isStreamInput()) {
1467
+ // output=stream, input=stream
1468
+ this.once('finish', () => {
1469
+ this._flattenBufferIn();
1470
+ sharp.pipeline(this.options, (err, data, info) => {
1471
+ if (err) {
1472
+ this.emit('error', is.nativeError(err, stack));
1473
+ } else {
1474
+ this.emit('info', info);
1475
+ this.push(data);
1476
+ }
1477
+ this.push(null);
1478
+ this.on('end', () => this.emit('close'));
1479
+ });
1480
+ });
1481
+ if (this.streamInFinished) {
1482
+ this.emit('finish');
1483
+ }
1484
+ } else {
1485
+ // output=stream, input=file/buffer
1486
+ sharp.pipeline(this.options, (err, data, info) => {
1487
+ if (err) {
1488
+ this.emit('error', is.nativeError(err, stack));
1489
+ } else {
1490
+ this.emit('info', info);
1491
+ this.push(data);
1492
+ }
1493
+ this.push(null);
1494
+ this.on('end', () => this.emit('close'));
1495
+ });
1496
+ }
1497
+ return this;
1498
+ } else {
1499
+ // output=promise
1500
+ if (this._isStreamInput()) {
1501
+ // output=promise, input=stream
1502
+ return new Promise((resolve, reject) => {
1503
+ this.once('finish', () => {
1504
+ this._flattenBufferIn();
1505
+ sharp.pipeline(this.options, (err, data, info) => {
1506
+ if (err) {
1507
+ reject(is.nativeError(err, stack));
1508
+ } else {
1509
+ if (this.options.resolveWithObject) {
1510
+ resolve({ data, info });
1511
+ } else {
1512
+ resolve(data);
1513
+ }
1514
+ }
1515
+ });
1516
+ });
1517
+ });
1518
+ } else {
1519
+ // output=promise, input=file/buffer
1520
+ return new Promise((resolve, reject) => {
1521
+ sharp.pipeline(this.options, (err, data, info) => {
1522
+ if (err) {
1523
+ reject(is.nativeError(err, stack));
1524
+ } else {
1525
+ if (this.options.resolveWithObject) {
1526
+ resolve({ data, info });
1527
+ } else {
1528
+ resolve(data);
1529
+ }
1530
+ }
1531
+ });
1532
+ });
1533
+ }
1534
+ }
1535
+ }
1536
+
1537
+ /**
1538
+ * Decorate the Sharp prototype with output-related functions.
1539
+ * @private
1540
+ */
1541
+ module.exports = function (Sharp) {
1542
+ Object.assign(Sharp.prototype, {
1543
+ // Public
1544
+ toFile,
1545
+ toBuffer,
1546
+ keepExif,
1547
+ withExif,
1548
+ withExifMerge,
1549
+ keepIccProfile,
1550
+ withIccProfile,
1551
+ keepMetadata,
1552
+ withMetadata,
1553
+ toFormat,
1554
+ jpeg,
1555
+ jp2,
1556
+ png,
1557
+ webp,
1558
+ tiff,
1559
+ avif,
1560
+ heif,
1561
+ jxl,
1562
+ gif,
1563
+ raw,
1564
+ tile,
1565
+ timeout,
1566
+ // Private
1567
+ _updateFormatOut,
1568
+ _setBooleanOption,
1569
+ _read,
1570
+ _pipeline
1571
+ });
1572
+ };