sharp 0.33.5 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) 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 +195 -0
  5. package/{lib/colour.js → dist/colour.mjs} +38 -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.mjs +25 -0
  12. package/dist/input.cjs +814 -0
  13. package/{lib/input.js → dist/input.mjs} +224 -68
  14. package/{lib/is.js → dist/is.cjs} +19 -45
  15. package/dist/is.mjs +143 -0
  16. package/{lib/libvips.js → dist/libvips.cjs} +52 -43
  17. package/dist/libvips.mjs +212 -0
  18. package/dist/operation.cjs +987 -0
  19. package/{lib/operation.js → dist/operation.mjs} +108 -79
  20. package/{lib/output.js → dist/output.cjs} +243 -36
  21. package/dist/output.mjs +1794 -0
  22. package/dist/resize.cjs +613 -0
  23. package/{lib/resize.js → dist/resize.mjs} +38 -12
  24. package/dist/sharp.cjs +164 -0
  25. package/dist/sharp.mjs +164 -0
  26. package/{lib/utility.js → dist/utility.cjs} +23 -24
  27. package/dist/utility.mjs +295 -0
  28. package/install/build.js +38 -0
  29. package/lib/index.d.ts +347 -125
  30. package/package.json +75 -84
  31. package/src/binding.gyp +49 -26
  32. package/src/common.cc +203 -111
  33. package/src/common.h +68 -40
  34. package/src/metadata.cc +106 -22
  35. package/src/metadata.h +15 -5
  36. package/src/operations.cc +62 -21
  37. package/src/operations.h +15 -3
  38. package/src/pipeline.cc +374 -183
  39. package/src/pipeline.h +34 -7
  40. package/src/sharp.cc +11 -8
  41. package/src/stats.cc +16 -13
  42. package/src/stats.h +6 -3
  43. package/src/utilities.cc +32 -12
  44. package/src/utilities.h +5 -2
  45. package/install/check.js +0 -41
  46. package/lib/index.js +0 -16
  47. 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,7 @@ function flop (flop) {
168
178
  * @throws {Error} Invalid parameters
169
179
  */
170
180
  function affine (matrix, options) {
171
- const flatMatrix = [].concat(...matrix);
181
+ const flatMatrix = Array.isArray(matrix) ? [].concat(...matrix) : [];
172
182
  if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
173
183
  this.options.affineMatrix = flatMatrix;
174
184
  } else {
@@ -229,7 +239,7 @@ function affine (matrix, options) {
229
239
  * When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
230
240
  * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available.
231
241
  *
232
- * See {@link https://www.libvips.org/API/current/libvips-convolution.html#vips-sharpen|libvips sharpen} operation.
242
+ * See {@link https://www.libvips.org/API/current/method.Image.sharpen.html libvips sharpen} operation.
233
243
  *
234
244
  * @example
235
245
  * const data = await sharp(input).sharpen().toBuffer();
@@ -249,45 +259,18 @@ function affine (matrix, options) {
249
259
  * })
250
260
  * .toBuffer();
251
261
  *
252
- * @param {Object|number} [options] - if present, is an Object with attributes
262
+ * @param {Object} [options] - if present, is an Object with attributes
253
263
  * @param {number} [options.sigma] - the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`, between 0.000001 and 10
254
264
  * @param {number} [options.m1=1.0] - the level of sharpening to apply to "flat" areas, between 0 and 1000000
255
265
  * @param {number} [options.m2=2.0] - the level of sharpening to apply to "jagged" areas, between 0 and 1000000
256
266
  * @param {number} [options.x1=2.0] - threshold between "flat" and "jagged", between 0 and 1000000
257
267
  * @param {number} [options.y2=10.0] - maximum amount of brightening, between 0 and 1000000
258
268
  * @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
269
  * @returns {Sharp}
262
270
  * @throws {Error} Invalid parameters
263
271
  */
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)) {
272
+ function sharpen (options) {
273
+ if (is.plainObject(options)) {
291
274
  if (is.number(options.sigma) && is.inRange(options.sigma, 0.000001, 10)) {
292
275
  this.options.sharpenSigma = options.sigma;
293
276
  } else {
@@ -329,7 +312,7 @@ function sharpen (options, flat, jagged) {
329
312
  }
330
313
  }
331
314
  } else {
332
- throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', options);
315
+ this.options.sharpenSigma = -1;
333
316
  }
334
317
  return this;
335
318
  }
@@ -426,10 +409,56 @@ function blur (options) {
426
409
  return this;
427
410
  }
428
411
 
412
+ /**
413
+ * Expand foreground objects using the dilate morphological operator.
414
+ *
415
+ * @example
416
+ * const output = await sharp(input)
417
+ * .dilate()
418
+ * .toBuffer();
419
+ *
420
+ * @param {Number} [width=1] dilation width in pixels.
421
+ * @returns {Sharp}
422
+ * @throws {Error} Invalid parameters
423
+ */
424
+ function dilate (width) {
425
+ if (!is.defined(width)) {
426
+ this.options.dilateWidth = 1;
427
+ } else if (is.integer(width) && width > 0) {
428
+ this.options.dilateWidth = width;
429
+ } else {
430
+ throw is.invalidParameterError('dilate', 'positive integer', dilate);
431
+ }
432
+ return this;
433
+ }
434
+
435
+ /**
436
+ * Shrink foreground objects using the erode morphological operator.
437
+ *
438
+ * @example
439
+ * const output = await sharp(input)
440
+ * .erode()
441
+ * .toBuffer();
442
+ *
443
+ * @param {Number} [width=1] erosion width in pixels.
444
+ * @returns {Sharp}
445
+ * @throws {Error} Invalid parameters
446
+ */
447
+ function erode (width) {
448
+ if (!is.defined(width)) {
449
+ this.options.erodeWidth = 1;
450
+ } else if (is.integer(width) && width > 0) {
451
+ this.options.erodeWidth = width;
452
+ } else {
453
+ throw is.invalidParameterError('erode', 'positive integer', erode);
454
+ }
455
+ return this;
456
+ }
457
+
429
458
  /**
430
459
  * Merge alpha transparency channel, if any, with a background, then remove the alpha channel.
431
460
  *
432
- * See also {@link /api-channel#removealpha|removeAlpha}.
461
+ * See also {@link /api-channel#removealpha removeAlpha}.
433
462
  *
434
463
  * @example
435
464
  * await sharp(rgbaInput)
@@ -454,8 +483,6 @@ function flatten (options) {
454
483
  *
455
484
  * Existing alpha channel values for non-white pixels remain unchanged.
456
485
  *
457
- * This feature is experimental and the API may change.
458
- *
459
486
  * @since 0.32.1
460
487
  *
461
488
  * @example
@@ -604,7 +631,7 @@ function normalize (options) {
604
631
 
605
632
  /**
606
633
  * Perform contrast limiting adaptive histogram equalization
607
- * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE|CLAHE}.
634
+ * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE CLAHE}.
608
635
  *
609
636
  * This will, in general, enhance the clarity of the image by bringing out darker details.
610
637
  *
@@ -686,9 +713,7 @@ function convolve (kernel) {
686
713
  }
687
714
  // Default scale is sum of kernel values
688
715
  if (!is.integer(kernel.scale)) {
689
- kernel.scale = kernel.kernel.reduce(function (a, b) {
690
- return a + b;
691
- }, 0);
716
+ kernel.scale = kernel.kernel.reduce((a, b) => a + b, 0);
692
717
  }
693
718
  // Clip scale to a minimum value of 1
694
719
  if (kernel.scale < 1) {
@@ -930,15 +955,19 @@ function modulate (options) {
930
955
 
931
956
  /**
932
957
  * Decorate the Sharp prototype with operation-related functions.
958
+ * @module Sharp
933
959
  * @private
934
960
  */
935
- module.exports = function (Sharp) {
961
+ export default (Sharp) => {
936
962
  Object.assign(Sharp.prototype, {
963
+ autoOrient,
937
964
  rotate,
938
965
  flip,
939
966
  flop,
940
967
  affine,
941
968
  sharpen,
969
+ erode,
970
+ dilate,
942
971
  median,
943
972
  blur,
944
973
  flatten,