sharp 0.33.5 → 0.35.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +12 -18
  2. package/dist/channel.cjs +177 -0
  3. package/{lib/channel.js → dist/channel.mjs} +11 -8
  4. package/dist/colour.cjs +199 -0
  5. package/{lib/colour.js → dist/colour.mjs} +42 -23
  6. package/dist/composite.cjs +213 -0
  7. package/{lib/composite.js → dist/composite.mjs} +14 -11
  8. package/{lib/constructor.js → dist/constructor.cjs} +102 -43
  9. package/dist/constructor.mjs +511 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +1999 -0
  12. package/dist/index.d.mts +2046 -0
  13. package/dist/index.mjs +25 -0
  14. package/dist/input.cjs +814 -0
  15. package/{lib/input.js → dist/input.mjs} +232 -76
  16. package/{lib/is.js → dist/is.cjs} +19 -45
  17. package/dist/is.mjs +143 -0
  18. package/{lib/libvips.js → dist/libvips.cjs} +52 -43
  19. package/dist/libvips.mjs +212 -0
  20. package/dist/operation.cjs +998 -0
  21. package/{lib/operation.js → dist/operation.mjs} +131 -91
  22. package/{lib/output.js → dist/output.cjs} +262 -61
  23. package/dist/output.mjs +1788 -0
  24. package/dist/resize.cjs +617 -0
  25. package/{lib/resize.js → dist/resize.mjs} +65 -35
  26. package/dist/sharp.cjs +174 -0
  27. package/dist/sharp.mjs +174 -0
  28. package/{lib/utility.js → dist/utility.cjs} +30 -25
  29. package/dist/utility.mjs +301 -0
  30. package/install/build.js +38 -0
  31. package/lib/index.d.ts +387 -142
  32. package/package.json +90 -85
  33. package/src/binding.gyp +49 -26
  34. package/src/common.cc +203 -111
  35. package/src/common.h +68 -40
  36. package/src/metadata.cc +106 -22
  37. package/src/metadata.h +15 -5
  38. package/src/operations.cc +62 -21
  39. package/src/operations.h +15 -3
  40. package/src/pipeline.cc +391 -201
  41. package/src/pipeline.h +36 -7
  42. package/src/sharp.cc +11 -8
  43. package/src/stats.cc +16 -13
  44. package/src/stats.h +6 -3
  45. package/src/utilities.cc +35 -15
  46. package/src/utilities.h +5 -2
  47. package/install/check.js +0 -41
  48. package/lib/index.js +0 -16
  49. package/lib/sharp.js +0 -114
@@ -1,10 +1,9 @@
1
- // Copyright 2013 Lovell Fuller and others.
2
- // SPDX-License-Identifier: Apache-2.0
1
+ /*!
2
+ Copyright 2013 Lovell Fuller and others.
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
3
5
 
4
- 'use strict';
5
-
6
- const color = require('color');
7
- const is = require('./is');
6
+ import is from './is.mjs';
8
7
 
9
8
  /**
10
9
  * How accurate an operation should be.
@@ -18,22 +17,19 @@ const vipsPrecision = {
18
17
  };
19
18
 
20
19
  /**
21
- * Rotate the output image by either an explicit angle
22
- * or auto-orient based on the EXIF `Orientation` tag.
20
+ * Rotate the output image.
23
21
  *
24
- * If an angle is provided, it is converted to a valid positive degree rotation.
22
+ * The provided angle is converted to a valid positive degree rotation.
25
23
  * For example, `-450` will produce a 270 degree rotation.
26
24
  *
27
25
  * When rotating by an angle other than a multiple of 90,
28
26
  * the background colour can be provided with the `background` option.
29
27
  *
30
- * If no angle is provided, it is determined from the EXIF data.
31
- * Mirroring is supported and may infer the use of a flip operation.
32
- *
33
- * The use of `rotate` without an angle will remove the EXIF `Orientation` tag, if any.
28
+ * For backwards compatibility, if no angle is provided, `.autoOrient()` will be called.
34
29
  *
35
- * Only one rotation can occur per pipeline.
36
- * Previous calls to `rotate` in the same pipeline will be ignored.
30
+ * Only one rotation can occur per pipeline (aside from an initial call without
31
+ * arguments to orient via EXIF data). Previous calls to `rotate` in the same
32
+ * pipeline will be ignored.
37
33
  *
38
34
  * Multi-page images can only be rotated by 180 degrees.
39
35
  *
@@ -41,17 +37,6 @@ const vipsPrecision = {
41
37
  * for example `.rotate(x).extract(y)` will produce a different result to `.extract(y).rotate(x)`.
42
38
  *
43
39
  * @example
44
- * const pipeline = sharp()
45
- * .rotate()
46
- * .resize(null, 200)
47
- * .toBuffer(function (err, outputBuffer, info) {
48
- * // outputBuffer contains 200px high JPEG image data,
49
- * // auto-rotated using EXIF Orientation tag
50
- * // info.width and info.height contain the dimensions of the resized image
51
- * });
52
- * readableStream.pipe(pipeline);
53
- *
54
- * @example
55
40
  * const rotateThenResize = await sharp(input)
56
41
  * .rotate(90)
57
42
  * .resize({ width: 16, height: 8, fit: 'fill' })
@@ -68,23 +53,20 @@ const vipsPrecision = {
68
53
  * @throws {Error} Invalid parameters
69
54
  */
70
55
  function rotate (angle, options) {
71
- if (this.options.useExifOrientation || this.options.angle || this.options.rotationAngle) {
56
+ if (!is.defined(angle)) {
57
+ return this.autoOrient();
58
+ }
59
+ if (this.options.angle || this.options.rotationAngle) {
72
60
  this.options.debuglog('ignoring previous rotate options');
61
+ this.options.angle = 0;
62
+ this.options.rotationAngle = 0;
73
63
  }
74
- if (!is.defined(angle)) {
75
- this.options.useExifOrientation = true;
76
- } else if (is.integer(angle) && !(angle % 90)) {
64
+ if (is.integer(angle) && !(angle % 90)) {
77
65
  this.options.angle = angle;
78
66
  } else if (is.number(angle)) {
79
67
  this.options.rotationAngle = angle;
80
68
  if (is.object(options) && options.background) {
81
- const backgroundColour = color(options.background);
82
- this.options.rotationBackground = [
83
- backgroundColour.red(),
84
- backgroundColour.green(),
85
- backgroundColour.blue(),
86
- Math.round(backgroundColour.alpha() * 255)
87
- ];
69
+ this._setBackgroundColourOption('rotationBackground', options.background);
88
70
  }
89
71
  } else {
90
72
  throw is.invalidParameterError('angle', 'numeric', angle);
@@ -92,6 +74,34 @@ function rotate (angle, options) {
92
74
  return this;
93
75
  }
94
76
 
77
+ /**
78
+ * Auto-orient based on the EXIF `Orientation` tag, then remove the tag.
79
+ * Mirroring is supported and may infer the use of a flip operation.
80
+ *
81
+ * Previous or subsequent use of `rotate(angle)` and either `flip()` or `flop()`
82
+ * will logically occur after auto-orientation, regardless of call order.
83
+ *
84
+ * @example
85
+ * const output = await sharp(input).autoOrient().toBuffer();
86
+ *
87
+ * @example
88
+ * const pipeline = sharp()
89
+ * .autoOrient()
90
+ * .resize(null, 200)
91
+ * .toBuffer(function (err, outputBuffer, info) {
92
+ * // outputBuffer contains 200px high JPEG image data,
93
+ * // auto-oriented using EXIF Orientation tag
94
+ * // info.width and info.height contain the dimensions of the resized image
95
+ * });
96
+ * readableStream.pipe(pipeline);
97
+ *
98
+ * @returns {Sharp}
99
+ */
100
+ function autoOrient () {
101
+ this.options.input.autoOrient = true;
102
+ return this;
103
+ }
104
+
95
105
  /**
96
106
  * Mirror the image vertically (up-down) about the x-axis.
97
107
  * This always occurs before rotation, if any.
@@ -128,7 +138,7 @@ function flop (flop) {
128
138
  * Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.
129
139
  *
130
140
  * You must provide an array of length 4 or a 2x2 affine transformation matrix.
131
- * By default, new pixels are filled with a black background. You can provide a background color with the `background` option.
141
+ * By default, new pixels are filled with a black background. You can provide a background colour with the `background` option.
132
142
  * A particular interpolator may also be specified. Set the `interpolator` option to an attribute of the `sharp.interpolators` Object e.g. `sharp.interpolators.nohalo`.
133
143
  *
134
144
  * In the case of a 2x2 matrix, the transform is:
@@ -168,7 +178,13 @@ function flop (flop) {
168
178
  * @throws {Error} Invalid parameters
169
179
  */
170
180
  function affine (matrix, options) {
171
- const flatMatrix = [].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() : [];
172
188
  if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
173
189
  this.options.affineMatrix = flatMatrix;
174
190
  } else {
@@ -229,7 +245,7 @@ function affine (matrix, options) {
229
245
  * When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
230
246
  * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available.
231
247
  *
232
- * See {@link https://www.libvips.org/API/current/libvips-convolution.html#vips-sharpen|libvips sharpen} operation.
248
+ * See {@link https://www.libvips.org/API/current/method.Image.sharpen.html libvips sharpen} operation.
233
249
  *
234
250
  * @example
235
251
  * const data = await sharp(input).sharpen().toBuffer();
@@ -249,45 +265,18 @@ function affine (matrix, options) {
249
265
  * })
250
266
  * .toBuffer();
251
267
  *
252
- * @param {Object|number} [options] - if present, is an Object with attributes
268
+ * @param {Object} [options] - if present, is an Object with attributes
253
269
  * @param {number} [options.sigma] - the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`, between 0.000001 and 10
254
270
  * @param {number} [options.m1=1.0] - the level of sharpening to apply to "flat" areas, between 0 and 1000000
255
271
  * @param {number} [options.m2=2.0] - the level of sharpening to apply to "jagged" areas, between 0 and 1000000
256
272
  * @param {number} [options.x1=2.0] - threshold between "flat" and "jagged", between 0 and 1000000
257
273
  * @param {number} [options.y2=10.0] - maximum amount of brightening, between 0 and 1000000
258
274
  * @param {number} [options.y3=20.0] - maximum amount of darkening, between 0 and 1000000
259
- * @param {number} [flat] - (deprecated) see `options.m1`.
260
- * @param {number} [jagged] - (deprecated) see `options.m2`.
261
275
  * @returns {Sharp}
262
276
  * @throws {Error} Invalid parameters
263
277
  */
264
- function sharpen (options, flat, jagged) {
265
- if (!is.defined(options)) {
266
- // No arguments: default to mild sharpen
267
- this.options.sharpenSigma = -1;
268
- } else if (is.bool(options)) {
269
- // Deprecated boolean argument: apply mild sharpen?
270
- this.options.sharpenSigma = options ? -1 : 0;
271
- } else if (is.number(options) && is.inRange(options, 0.01, 10000)) {
272
- // Deprecated numeric argument: specific sigma
273
- this.options.sharpenSigma = options;
274
- // Deprecated control over flat areas
275
- if (is.defined(flat)) {
276
- if (is.number(flat) && is.inRange(flat, 0, 10000)) {
277
- this.options.sharpenM1 = flat;
278
- } else {
279
- throw is.invalidParameterError('flat', 'number between 0 and 10000', flat);
280
- }
281
- }
282
- // Deprecated control over jagged areas
283
- if (is.defined(jagged)) {
284
- if (is.number(jagged) && is.inRange(jagged, 0, 10000)) {
285
- this.options.sharpenM2 = jagged;
286
- } else {
287
- throw is.invalidParameterError('jagged', 'number between 0 and 10000', jagged);
288
- }
289
- }
290
- } else if (is.plainObject(options)) {
278
+ function sharpen (options) {
279
+ if (is.plainObject(options)) {
291
280
  if (is.number(options.sigma) && is.inRange(options.sigma, 0.000001, 10)) {
292
281
  this.options.sharpenSigma = options.sigma;
293
282
  } else {
@@ -329,7 +318,7 @@ function sharpen (options, flat, jagged) {
329
318
  }
330
319
  }
331
320
  } else {
332
- throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', options);
321
+ this.options.sharpenSigma = -1;
333
322
  }
334
323
  return this;
335
324
  }
@@ -426,10 +415,56 @@ function blur (options) {
426
415
  return this;
427
416
  }
428
417
 
418
+ /**
419
+ * Expand foreground objects using the dilate morphological operator.
420
+ *
421
+ * @example
422
+ * const output = await sharp(input)
423
+ * .dilate()
424
+ * .toBuffer();
425
+ *
426
+ * @param {Number} [width=1] dilation width in pixels.
427
+ * @returns {Sharp}
428
+ * @throws {Error} Invalid parameters
429
+ */
430
+ function dilate (width) {
431
+ if (!is.defined(width)) {
432
+ this.options.dilateWidth = 1;
433
+ } else if (is.integer(width) && is.inRange(width, 1, 65536)) {
434
+ this.options.dilateWidth = width;
435
+ } else {
436
+ throw is.invalidParameterError('width', 'integer between 1 and 65536', width);
437
+ }
438
+ return this;
439
+ }
440
+
441
+ /**
442
+ * Shrink foreground objects using the erode morphological operator.
443
+ *
444
+ * @example
445
+ * const output = await sharp(input)
446
+ * .erode()
447
+ * .toBuffer();
448
+ *
449
+ * @param {Number} [width=1] erosion width in pixels.
450
+ * @returns {Sharp}
451
+ * @throws {Error} Invalid parameters
452
+ */
453
+ function erode (width) {
454
+ if (!is.defined(width)) {
455
+ this.options.erodeWidth = 1;
456
+ } else if (is.integer(width) && is.inRange(width, 1, 65536)) {
457
+ this.options.erodeWidth = width;
458
+ } else {
459
+ throw is.invalidParameterError('width', 'integer between 1 and 65536', width);
460
+ }
461
+ return this;
462
+ }
463
+
429
464
  /**
430
465
  * Merge alpha transparency channel, if any, with a background, then remove the alpha channel.
431
466
  *
432
- * See also {@link /api-channel#removealpha|removeAlpha}.
467
+ * See also {@link /api-channel#removealpha removeAlpha}.
433
468
  *
434
469
  * @example
435
470
  * await sharp(rgbaInput)
@@ -454,8 +489,6 @@ function flatten (options) {
454
489
  *
455
490
  * Existing alpha channel values for non-white pixels remain unchanged.
456
491
  *
457
- * This feature is experimental and the API may change.
458
- *
459
492
  * @since 0.32.1
460
493
  *
461
494
  * @example
@@ -604,7 +637,7 @@ function normalize (options) {
604
637
 
605
638
  /**
606
639
  * Perform contrast limiting adaptive histogram equalization
607
- * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE|CLAHE}.
640
+ * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE CLAHE}.
608
641
  *
609
642
  * This will, in general, enhance the clarity of the image by bringing out darker details.
610
643
  *
@@ -619,23 +652,23 @@ function normalize (options) {
619
652
  * .toBuffer();
620
653
  *
621
654
  * @param {Object} options
622
- * @param {number} options.width - Integral width of the search window, in pixels.
623
- * @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.
624
657
  * @param {number} [options.maxSlope=3] - Integral level of brightening, between 0 and 100, where 0 disables contrast limiting.
625
658
  * @returns {Sharp}
626
659
  * @throws {Error} Invalid parameters
627
660
  */
628
661
  function clahe (options) {
629
662
  if (is.plainObject(options)) {
630
- if (is.integer(options.width) && options.width > 0) {
663
+ if (is.integer(options.width) && is.inRange(options.width, 1, 65536)) {
631
664
  this.options.claheWidth = options.width;
632
665
  } else {
633
- throw is.invalidParameterError('width', 'integer greater than zero', options.width);
666
+ throw is.invalidParameterError('width', 'integer between 1 and 65536', options.width);
634
667
  }
635
- if (is.integer(options.height) && options.height > 0) {
668
+ if (is.integer(options.height) && is.inRange(options.height, 1, 65536)) {
636
669
  this.options.claheHeight = options.height;
637
670
  } else {
638
- throw is.invalidParameterError('height', 'integer greater than zero', options.height);
671
+ throw is.invalidParameterError('height', 'integer between 1 and 65536', options.height);
639
672
  }
640
673
  if (is.defined(options.maxSlope)) {
641
674
  if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) {
@@ -679,16 +712,15 @@ function convolve (kernel) {
679
712
  if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
680
713
  !is.integer(kernel.width) || !is.integer(kernel.height) ||
681
714
  !is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
682
- kernel.height * kernel.width !== kernel.kernel.length
715
+ kernel.height * kernel.width !== kernel.kernel.length ||
716
+ !kernel.kernel.every(is.number)
683
717
  ) {
684
718
  // must pass in a kernel
685
719
  throw new Error('Invalid convolution kernel');
686
720
  }
687
721
  // Default scale is sum of kernel values
688
722
  if (!is.integer(kernel.scale)) {
689
- kernel.scale = kernel.kernel.reduce(function (a, b) {
690
- return a + b;
691
- }, 0);
723
+ kernel.scale = kernel.kernel.reduce((a, b) => a + b, 0);
692
724
  }
693
725
  // Clip scale to a minimum value of 1
694
726
  if (kernel.scale < 1) {
@@ -834,12 +866,16 @@ function recomb (inputMatrix) {
834
866
  if (!Array.isArray(inputMatrix)) {
835
867
  throw is.invalidParameterError('inputMatrix', 'array', inputMatrix);
836
868
  }
837
- if (inputMatrix.length !== 3 && inputMatrix.length !== 4) {
838
- 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);
872
+ }
873
+ if (!inputMatrix.every((row) => Array.isArray(row) && row.length === dimensions)) {
874
+ throw is.invalidParameterError('inputMatrix', `array of ${dimensions} arrays of length ${dimensions}`, inputMatrix);
839
875
  }
840
- const recombMatrix = inputMatrix.flat().map(Number);
841
- if (recombMatrix.length !== 9 && recombMatrix.length !== 16) {
842
- throw is.invalidParameterError('inputMatrix', 'cardinality of 9 or 16', recombMatrix.length);
876
+ const recombMatrix = inputMatrix.flat();
877
+ if (!recombMatrix.every(is.number)) {
878
+ throw is.invalidParameterError('inputMatrix', 'array of numbers', recombMatrix);
843
879
  }
844
880
  this.options.recombMatrix = recombMatrix;
845
881
  return this;
@@ -930,15 +966,19 @@ function modulate (options) {
930
966
 
931
967
  /**
932
968
  * Decorate the Sharp prototype with operation-related functions.
969
+ * @module Sharp
933
970
  * @private
934
971
  */
935
- module.exports = function (Sharp) {
972
+ export default (Sharp) => {
936
973
  Object.assign(Sharp.prototype, {
974
+ autoOrient,
937
975
  rotate,
938
976
  flip,
939
977
  flop,
940
978
  affine,
941
979
  sharpen,
980
+ erode,
981
+ dilate,
942
982
  median,
943
983
  blur,
944
984
  flatten,