sharp 0.35.2 → 0.35.3-rc.2

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/dist/colour.cjs CHANGED
@@ -146,12 +146,16 @@ function _getBackgroundColourOption (value) {
146
146
  (is.string(value) && value.length >= 3 && value.length <= 200)
147
147
  ) {
148
148
  const colour = color(value);
149
- return [
150
- colour.red(),
151
- colour.green(),
152
- colour.blue(),
153
- Math.round(colour.alpha() * 255)
154
- ];
149
+ const red = colour.red();
150
+ const green = colour.green();
151
+ const blue = colour.blue();
152
+ const alpha = Math.round(colour.alpha() * 255);
153
+ for (const [channel, component] of [['red', red], ['green', green], ['blue', blue], ['alpha', alpha]]) {
154
+ if (!is.number(component)) {
155
+ throw is.invalidParameterError(`background.${channel}`, 'number', component);
156
+ }
157
+ }
158
+ return [red, green, blue, alpha];
155
159
  } else {
156
160
  throw is.invalidParameterError('background', 'object or string', value);
157
161
  }
package/dist/colour.mjs CHANGED
@@ -146,12 +146,16 @@ function _getBackgroundColourOption (value) {
146
146
  (is.string(value) && value.length >= 3 && value.length <= 200)
147
147
  ) {
148
148
  const colour = color(value);
149
- return [
150
- colour.red(),
151
- colour.green(),
152
- colour.blue(),
153
- Math.round(colour.alpha() * 255)
154
- ];
149
+ const red = colour.red();
150
+ const green = colour.green();
151
+ const blue = colour.blue();
152
+ const alpha = Math.round(colour.alpha() * 255);
153
+ for (const [channel, component] of [['red', red], ['green', green], ['blue', blue], ['alpha', alpha]]) {
154
+ if (!is.number(component)) {
155
+ throw is.invalidParameterError(`background.${channel}`, 'number', component);
156
+ }
157
+ }
158
+ return [red, green, blue, alpha];
155
159
  } else {
156
160
  throw is.invalidParameterError('background', 'object or string', value);
157
161
  }
package/dist/index.d.cts CHANGED
@@ -658,25 +658,27 @@ declare namespace sharp {
658
658
  * @param callback Callback function called on completion with three arguments (err, buffer, info).
659
659
  * @returns A sharp instance that can be used to chain operations
660
660
  */
661
- toBuffer(callback: (err: Error, buffer: Buffer, info: OutputInfo) => void): Sharp;
661
+ toBuffer(callback: (err: Error, buffer: Buffer<ArrayBuffer>, info: OutputInfo) => void): Sharp;
662
662
 
663
663
  /**
664
664
  * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
665
665
  * By default, the format will match the input image, except SVG input which becomes PNG output.
666
+ * The underlying `ArrayBuffer` may be marked as non-transferable by some JavaScript runtimes.
666
667
  * @param options resolve options
667
668
  * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data.
668
669
  * @returns A promise that resolves with the Buffer data.
669
670
  */
670
- toBuffer(options?: { resolveWithObject: false }): Promise<Buffer>;
671
+ toBuffer(options?: { resolveWithObject: false }): Promise<Buffer<ArrayBuffer>>;
671
672
 
672
673
  /**
673
674
  * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
674
675
  * By default, the format will match the input image, except SVG input which becomes PNG output.
676
+ * The underlying `ArrayBuffer` may be marked as non-transferable by some JavaScript runtimes.
675
677
  * @param options resolve options
676
678
  * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data.
677
679
  * @returns A promise that resolves with an object containing the Buffer data and an info object containing the output image format, size (bytes), width, height and channels
678
680
  */
679
- toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer; info: OutputInfo }>;
681
+ toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer<ArrayBuffer>; info: OutputInfo }>;
680
682
 
681
683
  /**
682
684
  * Write output to a Uint8Array backed by a transferable ArrayBuffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
@@ -1508,11 +1510,11 @@ declare namespace sharp {
1508
1510
  tile?: boolean | undefined;
1509
1511
  /** Horizontal tile size (optional, default 256) */
1510
1512
  tileWidth?: number | undefined;
1511
- /** Vertical tile size (optional, default 256) */
1513
+ /** Vertical tile size, valid values are integers in the range 1-32768 (optional, default 256) */
1512
1514
  tileHeight?: number | undefined;
1513
- /** Horizontal resolution in pixels/mm (optional, default 1.0) */
1515
+ /** Horizontal resolution in pixels/mm, valid values are numbers in the range 0.001-1000000 (optional, default 1.0) */
1514
1516
  xres?: number | undefined;
1515
- /** Vertical resolution in pixels/mm (optional, default 1.0) */
1517
+ /** Vertical resolution in pixels/mm, valid values are numbers in the range 0.001-1000000 (optional, default 1.0) */
1516
1518
  yres?: number | undefined;
1517
1519
  /** Reduce bitdepth to 1, 2 or 4 bit (optional) */
1518
1520
  bitdepth?: 1 | 2 | 4 | undefined;
@@ -1598,13 +1600,13 @@ declare namespace sharp {
1598
1600
  }
1599
1601
 
1600
1602
  interface Region {
1601
- /** zero-indexed offset from left edge */
1603
+ /** zero-indexed offset from left edge, an integer between 0 and 100000000 */
1602
1604
  left: number;
1603
- /** zero-indexed offset from top edge */
1605
+ /** zero-indexed offset from top edge, an integer between 0 and 100000000 */
1604
1606
  top: number;
1605
- /** dimension of extracted image */
1607
+ /** dimension of extracted image, an integer between 0 and 100000000 */
1606
1608
  width: number;
1607
- /** dimension of extracted image */
1609
+ /** dimension of extracted image, an integer between 0 and 100000000 */
1608
1610
  height: number;
1609
1611
  }
1610
1612
 
@@ -1620,13 +1622,13 @@ declare namespace sharp {
1620
1622
  type ExtendWith = 'background' | 'copy' | 'repeat' | 'mirror';
1621
1623
 
1622
1624
  interface ExtendOptions {
1623
- /** single pixel count to top edge (optional, default 0) */
1625
+ /** single pixel count to top edge, valid values are integers in the range 0-10000 (optional, default 0) */
1624
1626
  top?: number | undefined;
1625
- /** single pixel count to left edge (optional, default 0) */
1627
+ /** single pixel count to left edge, valid values are integers in the range 0-10000 (optional, default 0) */
1626
1628
  left?: number | undefined;
1627
- /** single pixel count to bottom edge (optional, default 0) */
1629
+ /** single pixel count to bottom edge, valid values are integers in the range 0-10000 (optional, default 0) */
1628
1630
  bottom?: number | undefined;
1629
- /** single pixel count to right edge (optional, default 0) */
1631
+ /** single pixel count to right edge, valid values are integers in the range 0-10000 (optional, default 0) */
1630
1632
  right?: number | undefined;
1631
1633
  /** background colour, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */
1632
1634
  background?: ColorLike | undefined;
@@ -1641,7 +1643,7 @@ declare namespace sharp {
1641
1643
  threshold?: number | undefined;
1642
1644
  /** Does the input more closely resemble line art (e.g. vector) rather than being photographic? (optional, default false) */
1643
1645
  lineArt?: boolean | undefined;
1644
- /** Leave a margin around trimmed content, value is in pixels. (optional, default 0) */
1646
+ /** Leave a margin around trimmed content, integral number of pixels between 0 and 10000000. (optional, default 0) */
1645
1647
  margin?: number | undefined;
1646
1648
  }
1647
1649
 
@@ -1669,11 +1671,11 @@ declare namespace sharp {
1669
1671
  }
1670
1672
 
1671
1673
  interface ClaheOptions {
1672
- /** width of the region */
1674
+ /** width of the region. Valid values are integers in the range 1-65536. */
1673
1675
  width: number;
1674
- /** height of the region */
1676
+ /** height of the region. Valid values are integers in the range 1-65536. */
1675
1677
  height: number;
1676
- /** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100 (inclusive) (optional, default 3) */
1678
+ /** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100. (optional, default 3) */
1677
1679
  maxSlope?: number | undefined;
1678
1680
  }
1679
1681
 
@@ -1783,6 +1785,8 @@ declare namespace sharp {
1783
1785
  channels: Channels;
1784
1786
  /** indicating if premultiplication was used */
1785
1787
  premultiplied: boolean;
1788
+ /** Indicates if the output image has an alpha channel */
1789
+ hasAlpha: boolean;
1786
1790
  /** Only defined when using a crop strategy */
1787
1791
  cropOffsetLeft?: number | undefined;
1788
1792
  /** Only defined when using a crop strategy */
package/dist/index.d.mts CHANGED
@@ -652,25 +652,27 @@ export interface Sharp extends Duplex {
652
652
  * @param callback Callback function called on completion with three arguments (err, buffer, info).
653
653
  * @returns A sharp instance that can be used to chain operations
654
654
  */
655
- toBuffer(callback: (err: Error, buffer: Buffer, info: OutputInfo) => void): Sharp;
655
+ toBuffer(callback: (err: Error, buffer: Buffer<ArrayBuffer>, info: OutputInfo) => void): Sharp;
656
656
 
657
657
  /**
658
658
  * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
659
659
  * By default, the format will match the input image, except SVG input which becomes PNG output.
660
+ * The underlying `ArrayBuffer` may be marked as non-transferable by some JavaScript runtimes.
660
661
  * @param options resolve options
661
662
  * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data.
662
663
  * @returns A promise that resolves with the Buffer data.
663
664
  */
664
- toBuffer(options?: { resolveWithObject: false }): Promise<Buffer>;
665
+ toBuffer(options?: { resolveWithObject: false }): Promise<Buffer<ArrayBuffer>>;
665
666
 
666
667
  /**
667
668
  * Write output to a Buffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
668
669
  * By default, the format will match the input image, except SVG input which becomes PNG output.
670
+ * The underlying `ArrayBuffer` may be marked as non-transferable by some JavaScript runtimes.
669
671
  * @param options resolve options
670
672
  * @param options.resolveWithObject Resolve the Promise with an Object containing data and info properties instead of resolving only with data.
671
673
  * @returns A promise that resolves with an object containing the Buffer data and an info object containing the output image format, size (bytes), width, height and channels
672
674
  */
673
- toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer; info: OutputInfo }>;
675
+ toBuffer(options: { resolveWithObject: true }): Promise<{ data: Buffer<ArrayBuffer>; info: OutputInfo }>;
674
676
 
675
677
  /**
676
678
  * Write output to a Uint8Array backed by a transferable ArrayBuffer. JPEG, PNG, WebP, AVIF, TIFF, GIF and RAW output are supported.
@@ -1502,11 +1504,11 @@ export interface TiffOptions extends OutputOptions {
1502
1504
  tile?: boolean | undefined;
1503
1505
  /** Horizontal tile size (optional, default 256) */
1504
1506
  tileWidth?: number | undefined;
1505
- /** Vertical tile size (optional, default 256) */
1507
+ /** Vertical tile size, valid values are integers in the range 1-32768 (optional, default 256) */
1506
1508
  tileHeight?: number | undefined;
1507
- /** Horizontal resolution in pixels/mm (optional, default 1.0) */
1509
+ /** Horizontal resolution in pixels/mm, valid values are numbers in the range 0.001-1000000 (optional, default 1.0) */
1508
1510
  xres?: number | undefined;
1509
- /** Vertical resolution in pixels/mm (optional, default 1.0) */
1511
+ /** Vertical resolution in pixels/mm, valid values are numbers in the range 0.001-1000000 (optional, default 1.0) */
1510
1512
  yres?: number | undefined;
1511
1513
  /** Reduce bitdepth to 1, 2 or 4 bit (optional) */
1512
1514
  bitdepth?: 1 | 2 | 4 | undefined;
@@ -1592,13 +1594,13 @@ export interface ResizeOptions {
1592
1594
  }
1593
1595
 
1594
1596
  export interface Region {
1595
- /** zero-indexed offset from left edge */
1597
+ /** zero-indexed offset from left edge, an integer between 0 and 100000000 */
1596
1598
  left: number;
1597
- /** zero-indexed offset from top edge */
1599
+ /** zero-indexed offset from top edge, an integer between 0 and 100000000 */
1598
1600
  top: number;
1599
- /** dimension of extracted image */
1601
+ /** dimension of extracted image, an integer between 0 and 100000000 */
1600
1602
  width: number;
1601
- /** dimension of extracted image */
1603
+ /** dimension of extracted image, an integer between 0 and 100000000 */
1602
1604
  height: number;
1603
1605
  }
1604
1606
 
@@ -1614,13 +1616,13 @@ export interface Noise {
1614
1616
  export type ExtendWith = 'background' | 'copy' | 'repeat' | 'mirror';
1615
1617
 
1616
1618
  export interface ExtendOptions {
1617
- /** single pixel count to top edge (optional, default 0) */
1619
+ /** single pixel count to top edge, valid values are integers in the range 0-10000 (optional, default 0) */
1618
1620
  top?: number | undefined;
1619
- /** single pixel count to left edge (optional, default 0) */
1621
+ /** single pixel count to left edge, valid values are integers in the range 0-10000 (optional, default 0) */
1620
1622
  left?: number | undefined;
1621
- /** single pixel count to bottom edge (optional, default 0) */
1623
+ /** single pixel count to bottom edge, valid values are integers in the range 0-10000 (optional, default 0) */
1622
1624
  bottom?: number | undefined;
1623
- /** single pixel count to right edge (optional, default 0) */
1625
+ /** single pixel count to right edge, valid values are integers in the range 0-10000 (optional, default 0) */
1624
1626
  right?: number | undefined;
1625
1627
  /** background colour, parsed by the color module, defaults to black without transparency. (optional, default {r:0,g:0,b:0,alpha:1}) */
1626
1628
  background?: ColorLike | undefined;
@@ -1635,7 +1637,7 @@ export interface TrimOptions {
1635
1637
  threshold?: number | undefined;
1636
1638
  /** Does the input more closely resemble line art (e.g. vector) rather than being photographic? (optional, default false) */
1637
1639
  lineArt?: boolean | undefined;
1638
- /** Leave a margin around trimmed content, value is in pixels. (optional, default 0) */
1640
+ /** Leave a margin around trimmed content, integral number of pixels between 0 and 10000000. (optional, default 0) */
1639
1641
  margin?: number | undefined;
1640
1642
  }
1641
1643
 
@@ -1663,11 +1665,11 @@ export interface Kernel {
1663
1665
  }
1664
1666
 
1665
1667
  export interface ClaheOptions {
1666
- /** width of the region */
1668
+ /** width of the region. Valid values are integers in the range 1-65536. */
1667
1669
  width: number;
1668
- /** height of the region */
1670
+ /** height of the region. Valid values are integers in the range 1-65536. */
1669
1671
  height: number;
1670
- /** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100 (inclusive) (optional, default 3) */
1672
+ /** max slope of the cumulative contrast. A value of 0 disables contrast limiting. Valid values are integers in the range 0-100. (optional, default 3) */
1671
1673
  maxSlope?: number | undefined;
1672
1674
  }
1673
1675
 
@@ -1777,6 +1779,8 @@ export interface OutputInfo {
1777
1779
  channels: Channels;
1778
1780
  /** indicating if premultiplication was used */
1779
1781
  premultiplied: boolean;
1782
+ /** Indicates if the output image has an alpha channel */
1783
+ hasAlpha: boolean;
1780
1784
  /** Only defined when using a crop strategy */
1781
1785
  cropOffsetLeft?: number | undefined;
1782
1786
  /** Only defined when using a crop strategy */
package/dist/input.cjs CHANGED
@@ -181,8 +181,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
181
181
  if (is.defined(inputOptions.raw)) {
182
182
  if (
183
183
  is.object(inputOptions.raw) &&
184
- is.integer(inputOptions.raw.width) && inputOptions.raw.width > 0 &&
185
- is.integer(inputOptions.raw.height) && inputOptions.raw.height > 0 &&
184
+ is.integer(inputOptions.raw.width) && is.inRange(inputOptions.raw.width, 1, 100000000) &&
185
+ is.integer(inputOptions.raw.height) && is.inRange(inputOptions.raw.height, 1, 100000000) &&
186
186
  is.integer(inputOptions.raw.channels) && is.inRange(inputOptions.raw.channels, 1, 4)
187
187
  ) {
188
188
  inputDescriptor.rawWidth = inputOptions.raw.width;
@@ -329,8 +329,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
329
329
  if (is.defined(inputOptions.create)) {
330
330
  if (
331
331
  is.object(inputOptions.create) &&
332
- is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
333
- is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
332
+ is.integer(inputOptions.create.width) && is.inRange(inputOptions.create.width, 1, 100000000) &&
333
+ is.integer(inputOptions.create.height) && is.inRange(inputOptions.create.height, 1, 100000000) &&
334
334
  is.integer(inputOptions.create.channels)
335
335
  ) {
336
336
  inputDescriptor.createWidth = inputOptions.create.width;
@@ -410,17 +410,17 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
410
410
  }
411
411
  }
412
412
  if (is.defined(inputOptions.text.width)) {
413
- if (is.integer(inputOptions.text.width) && inputOptions.text.width > 0) {
413
+ if (is.integer(inputOptions.text.width) && is.inRange(inputOptions.text.width, 1, 1000000)) {
414
414
  inputDescriptor.textWidth = inputOptions.text.width;
415
415
  } else {
416
- throw is.invalidParameterError('text.width', 'positive integer', inputOptions.text.width);
416
+ throw is.invalidParameterError('text.width', 'integer between 1 and 1000000', inputOptions.text.width);
417
417
  }
418
418
  }
419
419
  if (is.defined(inputOptions.text.height)) {
420
- if (is.integer(inputOptions.text.height) && inputOptions.text.height > 0) {
420
+ if (is.integer(inputOptions.text.height) && is.inRange(inputOptions.text.height, 1, 1000000)) {
421
421
  inputDescriptor.textHeight = inputOptions.text.height;
422
422
  } else {
423
- throw is.invalidParameterError('text.height', 'positive integer', inputOptions.text.height);
423
+ throw is.invalidParameterError('text.height', 'integer between 1 and 1000000', inputOptions.text.height);
424
424
  }
425
425
  }
426
426
  if (is.defined(inputOptions.text.align)) {
package/dist/input.mjs CHANGED
@@ -181,8 +181,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
181
181
  if (is.defined(inputOptions.raw)) {
182
182
  if (
183
183
  is.object(inputOptions.raw) &&
184
- is.integer(inputOptions.raw.width) && inputOptions.raw.width > 0 &&
185
- is.integer(inputOptions.raw.height) && inputOptions.raw.height > 0 &&
184
+ is.integer(inputOptions.raw.width) && is.inRange(inputOptions.raw.width, 1, 100000000) &&
185
+ is.integer(inputOptions.raw.height) && is.inRange(inputOptions.raw.height, 1, 100000000) &&
186
186
  is.integer(inputOptions.raw.channels) && is.inRange(inputOptions.raw.channels, 1, 4)
187
187
  ) {
188
188
  inputDescriptor.rawWidth = inputOptions.raw.width;
@@ -329,8 +329,8 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
329
329
  if (is.defined(inputOptions.create)) {
330
330
  if (
331
331
  is.object(inputOptions.create) &&
332
- is.integer(inputOptions.create.width) && inputOptions.create.width > 0 &&
333
- is.integer(inputOptions.create.height) && inputOptions.create.height > 0 &&
332
+ is.integer(inputOptions.create.width) && is.inRange(inputOptions.create.width, 1, 100000000) &&
333
+ is.integer(inputOptions.create.height) && is.inRange(inputOptions.create.height, 1, 100000000) &&
334
334
  is.integer(inputOptions.create.channels)
335
335
  ) {
336
336
  inputDescriptor.createWidth = inputOptions.create.width;
@@ -410,17 +410,17 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
410
410
  }
411
411
  }
412
412
  if (is.defined(inputOptions.text.width)) {
413
- if (is.integer(inputOptions.text.width) && inputOptions.text.width > 0) {
413
+ if (is.integer(inputOptions.text.width) && is.inRange(inputOptions.text.width, 1, 1000000)) {
414
414
  inputDescriptor.textWidth = inputOptions.text.width;
415
415
  } else {
416
- throw is.invalidParameterError('text.width', 'positive integer', inputOptions.text.width);
416
+ throw is.invalidParameterError('text.width', 'integer between 1 and 1000000', inputOptions.text.width);
417
417
  }
418
418
  }
419
419
  if (is.defined(inputOptions.text.height)) {
420
- if (is.integer(inputOptions.text.height) && inputOptions.text.height > 0) {
420
+ if (is.integer(inputOptions.text.height) && is.inRange(inputOptions.text.height, 1, 1000000)) {
421
421
  inputDescriptor.textHeight = inputOptions.text.height;
422
422
  } else {
423
- throw is.invalidParameterError('text.height', 'positive integer', inputOptions.text.height);
423
+ throw is.invalidParameterError('text.height', 'integer between 1 and 1000000', inputOptions.text.height);
424
424
  }
425
425
  }
426
426
  if (is.defined(inputOptions.text.align)) {
package/dist/is.cjs CHANGED
@@ -78,7 +78,7 @@ const string = (val) => typeof val === 'string' && val.length > 0;
78
78
  * Is this value a real number?
79
79
  * @private
80
80
  */
81
- const number = (val) => typeof val === 'number' && !Number.isNaN(val);
81
+ const number = (val) => typeof val === 'number' && Number.isFinite(val);
82
82
 
83
83
  /**
84
84
  * Is this value an integer?
package/dist/is.mjs CHANGED
@@ -78,7 +78,7 @@ const string = (val) => typeof val === 'string' && val.length > 0;
78
78
  * Is this value a real number?
79
79
  * @private
80
80
  */
81
- const number = (val) => typeof val === 'number' && !Number.isNaN(val);
81
+ const number = (val) => typeof val === 'number' && Number.isFinite(val);
82
82
 
83
83
  /**
84
84
  * Is this value an integer?
@@ -178,7 +178,13 @@ function flop (flop) {
178
178
  * @throws {Error} Invalid parameters
179
179
  */
180
180
  function affine (matrix, options) {
181
- const flatMatrix = Array.isArray(matrix) ? [].concat(...matrix) : [];
181
+ const isValidShape = Array.isArray(matrix) && (
182
+ // 1x4 array of numbers
183
+ (matrix.length === 4 && matrix.every(is.number)) ||
184
+ // 2x2 array of arrays of numbers
185
+ (matrix.length === 2 && matrix.every((row) => Array.isArray(row) && row.length === 2))
186
+ );
187
+ const flatMatrix = isValidShape ? matrix.flat() : [];
182
188
  if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
183
189
  this.options.affineMatrix = flatMatrix;
184
190
  } else {
@@ -646,23 +652,23 @@ function normalize (options) {
646
652
  * .toBuffer();
647
653
  *
648
654
  * @param {Object} options
649
- * @param {number} options.width - Integral width of the search window, in pixels.
650
- * @param {number} options.height - Integral height of the search window, in pixels.
655
+ * @param {number} options.width - Integral width of the search window, in pixels, between 1 and 65536.
656
+ * @param {number} options.height - Integral height of the search window, in pixels, between 1 and 65536.
651
657
  * @param {number} [options.maxSlope=3] - Integral level of brightening, between 0 and 100, where 0 disables contrast limiting.
652
658
  * @returns {Sharp}
653
659
  * @throws {Error} Invalid parameters
654
660
  */
655
661
  function clahe (options) {
656
662
  if (is.plainObject(options)) {
657
- if (is.integer(options.width) && options.width > 0) {
663
+ if (is.integer(options.width) && is.inRange(options.width, 1, 65536)) {
658
664
  this.options.claheWidth = options.width;
659
665
  } else {
660
- throw is.invalidParameterError('width', 'integer greater than zero', options.width);
666
+ throw is.invalidParameterError('width', 'integer between 1 and 65536', options.width);
661
667
  }
662
- if (is.integer(options.height) && options.height > 0) {
668
+ if (is.integer(options.height) && is.inRange(options.height, 1, 65536)) {
663
669
  this.options.claheHeight = options.height;
664
670
  } else {
665
- throw is.invalidParameterError('height', 'integer greater than zero', options.height);
671
+ throw is.invalidParameterError('height', 'integer between 1 and 65536', options.height);
666
672
  }
667
673
  if (is.defined(options.maxSlope)) {
668
674
  if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) {
@@ -860,13 +866,14 @@ function recomb (inputMatrix) {
860
866
  if (!Array.isArray(inputMatrix)) {
861
867
  throw is.invalidParameterError('inputMatrix', 'array', inputMatrix);
862
868
  }
863
- if (inputMatrix.length !== 3 && inputMatrix.length !== 4) {
864
- throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', inputMatrix.length);
869
+ const dimensions = inputMatrix.length;
870
+ if (dimensions !== 3 && dimensions !== 4) {
871
+ throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', dimensions);
865
872
  }
866
- const recombMatrix = inputMatrix.flat();
867
- if (recombMatrix.length !== 9 && recombMatrix.length !== 16) {
868
- throw is.invalidParameterError('inputMatrix', 'cardinality of 9 or 16', recombMatrix.length);
873
+ if (!inputMatrix.every((row) => Array.isArray(row) && row.length === dimensions)) {
874
+ throw is.invalidParameterError('inputMatrix', `array of ${dimensions} arrays of length ${dimensions}`, inputMatrix);
869
875
  }
876
+ const recombMatrix = inputMatrix.flat();
870
877
  if (!recombMatrix.every(is.number)) {
871
878
  throw is.invalidParameterError('inputMatrix', 'array of numbers', recombMatrix);
872
879
  }
@@ -178,7 +178,13 @@ function flop (flop) {
178
178
  * @throws {Error} Invalid parameters
179
179
  */
180
180
  function affine (matrix, options) {
181
- const flatMatrix = Array.isArray(matrix) ? [].concat(...matrix) : [];
181
+ const isValidShape = Array.isArray(matrix) && (
182
+ // 1x4 array of numbers
183
+ (matrix.length === 4 && matrix.every(is.number)) ||
184
+ // 2x2 array of arrays of numbers
185
+ (matrix.length === 2 && matrix.every((row) => Array.isArray(row) && row.length === 2))
186
+ );
187
+ const flatMatrix = isValidShape ? matrix.flat() : [];
182
188
  if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
183
189
  this.options.affineMatrix = flatMatrix;
184
190
  } else {
@@ -646,23 +652,23 @@ function normalize (options) {
646
652
  * .toBuffer();
647
653
  *
648
654
  * @param {Object} options
649
- * @param {number} options.width - Integral width of the search window, in pixels.
650
- * @param {number} options.height - Integral height of the search window, in pixels.
655
+ * @param {number} options.width - Integral width of the search window, in pixels, between 1 and 65536.
656
+ * @param {number} options.height - Integral height of the search window, in pixels, between 1 and 65536.
651
657
  * @param {number} [options.maxSlope=3] - Integral level of brightening, between 0 and 100, where 0 disables contrast limiting.
652
658
  * @returns {Sharp}
653
659
  * @throws {Error} Invalid parameters
654
660
  */
655
661
  function clahe (options) {
656
662
  if (is.plainObject(options)) {
657
- if (is.integer(options.width) && options.width > 0) {
663
+ if (is.integer(options.width) && is.inRange(options.width, 1, 65536)) {
658
664
  this.options.claheWidth = options.width;
659
665
  } else {
660
- throw is.invalidParameterError('width', 'integer greater than zero', options.width);
666
+ throw is.invalidParameterError('width', 'integer between 1 and 65536', options.width);
661
667
  }
662
- if (is.integer(options.height) && options.height > 0) {
668
+ if (is.integer(options.height) && is.inRange(options.height, 1, 65536)) {
663
669
  this.options.claheHeight = options.height;
664
670
  } else {
665
- throw is.invalidParameterError('height', 'integer greater than zero', options.height);
671
+ throw is.invalidParameterError('height', 'integer between 1 and 65536', options.height);
666
672
  }
667
673
  if (is.defined(options.maxSlope)) {
668
674
  if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) {
@@ -860,13 +866,14 @@ function recomb (inputMatrix) {
860
866
  if (!Array.isArray(inputMatrix)) {
861
867
  throw is.invalidParameterError('inputMatrix', 'array', inputMatrix);
862
868
  }
863
- if (inputMatrix.length !== 3 && inputMatrix.length !== 4) {
864
- throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', inputMatrix.length);
869
+ const dimensions = inputMatrix.length;
870
+ if (dimensions !== 3 && dimensions !== 4) {
871
+ throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', dimensions);
865
872
  }
866
- const recombMatrix = inputMatrix.flat();
867
- if (recombMatrix.length !== 9 && recombMatrix.length !== 16) {
868
- throw is.invalidParameterError('inputMatrix', 'cardinality of 9 or 16', recombMatrix.length);
873
+ if (!inputMatrix.every((row) => Array.isArray(row) && row.length === dimensions)) {
874
+ throw is.invalidParameterError('inputMatrix', `array of ${dimensions} arrays of length ${dimensions}`, inputMatrix);
869
875
  }
876
+ const recombMatrix = inputMatrix.flat();
870
877
  if (!recombMatrix.every(is.number)) {
871
878
  throw is.invalidParameterError('inputMatrix', 'array of numbers', recombMatrix);
872
879
  }
package/dist/output.cjs CHANGED
@@ -113,24 +113,20 @@ function toFile (fileOut, callback) {
113
113
  * Animated output will also contain `pageHeight` and `pages`.
114
114
  * May also contain `textAutofitDpi` (dpi the font was rendered at) if image was created from text.
115
115
  *
116
- * A `Promise` is returned when `callback` is not provided.
116
+ * The underlying `ArrayBuffer` may be marked as non-transferable by some JavaScript runtimes.
117
+ * Use {@link #touint8array toUint8Array} for a guaranteed transferable `ArrayBuffer`.
117
118
  *
118
- * @example
119
- * sharp(input)
120
- * .toBuffer((err, data, info) => { ... });
119
+ * A `Promise` is returned when `callback` is not provided.
121
120
  *
122
121
  * @example
123
- * sharp(input)
124
- * .toBuffer()
125
- * .then(data => { ... })
126
- * .catch(err => { ... });
122
+ * const data = await sharp(input)
123
+ * .png()
124
+ * .toBuffer();
127
125
  *
128
126
  * @example
129
- * sharp(input)
127
+ * const { data, info } = await sharp(input)
130
128
  * .png()
131
- * .toBuffer({ resolveWithObject: true })
132
- * .then(({ data, info }) => { ... })
133
- * .catch(err => { ... });
129
+ * .toBuffer({ resolveWithObject: true });
134
130
  *
135
131
  * @example
136
132
  * const { data, info } = await sharp('my-image.jpg')
@@ -1103,10 +1099,10 @@ function trySetAnimationOptions (source, target) {
1103
1099
  * @param {string} [options.predictor='horizontal'] - compression predictor options: none, horizontal, float
1104
1100
  * @param {boolean} [options.pyramid=false] - write an image pyramid
1105
1101
  * @param {boolean} [options.tile=false] - write a tiled tiff
1106
- * @param {number} [options.tileWidth=256] - horizontal tile size
1107
- * @param {number} [options.tileHeight=256] - vertical tile size
1108
- * @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm
1109
- * @param {number} [options.yres=1.0] - vertical resolution in pixels/mm
1102
+ * @param {number} [options.tileWidth=256] - horizontal tile size, valid values are integers in the range 1-32768
1103
+ * @param {number} [options.tileHeight=256] - vertical tile size, valid values are integers in the range 1-32768
1104
+ * @param {number} [options.xres=1.0] - horizontal resolution in pixels/mm, valid values are numbers in the range 0.001-1000000
1105
+ * @param {number} [options.yres=1.0] - vertical resolution in pixels/mm, valid values are numbers in the range 0.001-1000000
1110
1106
  * @param {string} [options.resolutionUnit='inch'] - resolution unit options: inch, cm
1111
1107
  * @param {number} [options.bitdepth=0] - reduce bitdepth to 1, 2 or 4 bit
1112
1108
  * @param {boolean} [options.miniswhite=false] - write 1-bit images as miniswhite
@@ -1134,17 +1130,17 @@ function tiff (options) {
1134
1130
  this._setBooleanOption('tiffTile', options.tile);
1135
1131
  }
1136
1132
  if (is.defined(options.tileWidth)) {
1137
- if (is.integer(options.tileWidth) && options.tileWidth > 0) {
1133
+ if (is.integer(options.tileWidth) && is.inRange(options.tileWidth, 1, 32768)) {
1138
1134
  this.options.tiffTileWidth = options.tileWidth;
1139
1135
  } else {
1140
- throw is.invalidParameterError('tileWidth', 'integer greater than zero', options.tileWidth);
1136
+ throw is.invalidParameterError('tileWidth', 'integer between 1 and 32768', options.tileWidth);
1141
1137
  }
1142
1138
  }
1143
1139
  if (is.defined(options.tileHeight)) {
1144
- if (is.integer(options.tileHeight) && options.tileHeight > 0) {
1140
+ if (is.integer(options.tileHeight) && is.inRange(options.tileHeight, 1, 32768)) {
1145
1141
  this.options.tiffTileHeight = options.tileHeight;
1146
1142
  } else {
1147
- throw is.invalidParameterError('tileHeight', 'integer greater than zero', options.tileHeight);
1143
+ throw is.invalidParameterError('tileHeight', 'integer between 1 and 32768', options.tileHeight);
1148
1144
  }
1149
1145
  }
1150
1146
  // miniswhite
@@ -1157,17 +1153,17 @@ function tiff (options) {
1157
1153
  }
1158
1154
  // resolution
1159
1155
  if (is.defined(options.xres)) {
1160
- if (is.number(options.xres) && options.xres > 0) {
1156
+ if (is.number(options.xres) && is.inRange(options.xres, 0.001, 1000000)) {
1161
1157
  this.options.tiffXres = options.xres;
1162
1158
  } else {
1163
- throw is.invalidParameterError('xres', 'number greater than zero', options.xres);
1159
+ throw is.invalidParameterError('xres', 'number between 0.001 and 1000000', options.xres);
1164
1160
  }
1165
1161
  }
1166
1162
  if (is.defined(options.yres)) {
1167
- if (is.number(options.yres) && options.yres > 0) {
1163
+ if (is.number(options.yres) && is.inRange(options.yres, 0.001, 1000000)) {
1168
1164
  this.options.tiffYres = options.yres;
1169
1165
  } else {
1170
- throw is.invalidParameterError('yres', 'number greater than zero', options.yres);
1166
+ throw is.invalidParameterError('yres', 'number between 0.001 and 1000000', options.yres);
1171
1167
  }
1172
1168
  }
1173
1169
  // compression