1 | // Copyright 2013 Lovell Fuller and others.
|
2 | // SPDX-License-Identifier: Apache-2.0
|
3 |
|
4 | ;
|
5 |
|
6 | const color = require('color');
|
7 | const is = require('./is');
|
8 |
|
9 | /**
|
10 | * How accurate an operation should be.
|
11 | * @member
|
12 | * @private
|
13 | */
|
14 | const vipsPrecision = {
|
15 | integer: 'integer',
|
16 | float: 'float',
|
17 | approximate: 'approximate'
|
18 | };
|
19 |
|
20 | /**
|
21 | * Rotate the output image by either an explicit angle
|
22 | * or auto-orient based on the EXIF `Orientation` tag.
|
23 | *
|
24 | * If an angle is provided, it is converted to a valid positive degree rotation.
|
25 | * For example, `-450` will produce a 270 degree rotation.
|
26 | *
|
27 | * When rotating by an angle other than a multiple of 90,
|
28 | * the background colour can be provided with the `background` option.
|
29 | *
|
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.
|
34 | *
|
35 | * Only one rotation can occur per pipeline.
|
36 | * Previous calls to `rotate` in the same pipeline will be ignored.
|
37 | *
|
38 | * Multi-page images can only be rotated by 180 degrees.
|
39 | *
|
40 | * Method order is important when rotating, resizing and/or extracting regions,
|
41 | * for example `.rotate(x).extract(y)` will produce a different result to `.extract(y).rotate(x)`.
|
42 | *
|
43 | * @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 | * const rotateThenResize = await sharp(input)
|
56 | * .rotate(90)
|
57 | * .resize({ width: 16, height: 8, fit: 'fill' })
|
58 | * .toBuffer();
|
59 | * const resizeThenRotate = await sharp(input)
|
60 | * .resize({ width: 16, height: 8, fit: 'fill' })
|
61 | * .rotate(90)
|
62 | * .toBuffer();
|
63 | *
|
64 | * @param {number} [angle=auto] angle of rotation.
|
65 | * @param {Object} [options] - if present, is an Object with optional attributes.
|
66 | * @param {string|Object} [options.background="#000000"] parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
67 | * @returns {Sharp}
|
68 | * @throws {Error} Invalid parameters
|
69 | */
|
70 | function rotate (angle, options) {
|
71 | if (this.options.useExifOrientation || this.options.angle || this.options.rotationAngle) {
|
72 | this.options.debuglog('ignoring previous rotate options');
|
73 | }
|
74 | if (!is.defined(angle)) {
|
75 | this.options.useExifOrientation = true;
|
76 | } else if (is.integer(angle) && !(angle % 90)) {
|
77 | this.options.angle = angle;
|
78 | } else if (is.number(angle)) {
|
79 | this.options.rotationAngle = angle;
|
80 | 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 | ];
|
88 | }
|
89 | } else {
|
90 | throw is.invalidParameterError('angle', 'numeric', angle);
|
91 | }
|
92 | return this;
|
93 | }
|
94 |
|
95 | /**
|
96 | * Mirror the image vertically (up-down) about the x-axis.
|
97 | * This always occurs before rotation, if any.
|
98 | *
|
99 | * This operation does not work correctly with multi-page images.
|
100 | *
|
101 | * @example
|
102 | * const output = await sharp(input).flip().toBuffer();
|
103 | *
|
104 | * @param {Boolean} [flip=true]
|
105 | * @returns {Sharp}
|
106 | */
|
107 | function flip (flip) {
|
108 | this.options.flip = is.bool(flip) ? flip : true;
|
109 | return this;
|
110 | }
|
111 |
|
112 | /**
|
113 | * Mirror the image horizontally (left-right) about the y-axis.
|
114 | * This always occurs before rotation, if any.
|
115 | *
|
116 | * @example
|
117 | * const output = await sharp(input).flop().toBuffer();
|
118 | *
|
119 | * @param {Boolean} [flop=true]
|
120 | * @returns {Sharp}
|
121 | */
|
122 | function flop (flop) {
|
123 | this.options.flop = is.bool(flop) ? flop : true;
|
124 | return this;
|
125 | }
|
126 |
|
127 | /**
|
128 | * Perform an affine transform on an image. This operation will always occur after resizing, extraction and rotation, if any.
|
129 | *
|
130 | * 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.
|
132 | * 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 | *
|
134 | * In the case of a 2x2 matrix, the transform is:
|
135 | * - X = `matrix[0, 0]` \* (x + `idx`) + `matrix[0, 1]` \* (y + `idy`) + `odx`
|
136 | * - Y = `matrix[1, 0]` \* (x + `idx`) + `matrix[1, 1]` \* (y + `idy`) + `ody`
|
137 | *
|
138 | * where:
|
139 | * - x and y are the coordinates in input image.
|
140 | * - X and Y are the coordinates in output image.
|
141 | * - (0,0) is the upper left corner.
|
142 | *
|
143 | * @since 0.27.0
|
144 | *
|
145 | * @example
|
146 | * const pipeline = sharp()
|
147 | * .affine([[1, 0.3], [0.1, 0.7]], {
|
148 | * background: 'white',
|
149 | * interpolator: sharp.interpolators.nohalo
|
150 | * })
|
151 | * .toBuffer((err, outputBuffer, info) => {
|
152 | * // outputBuffer contains the transformed image
|
153 | * // info.width and info.height contain the new dimensions
|
154 | * });
|
155 | *
|
156 | * inputStream
|
157 | * .pipe(pipeline);
|
158 | *
|
159 | * @param {Array<Array<number>>|Array<number>} matrix - affine transformation matrix
|
160 | * @param {Object} [options] - if present, is an Object with optional attributes.
|
161 | * @param {String|Object} [options.background="#000000"] - parsed by the [color](https://www.npmjs.org/package/color) module to extract values for red, green, blue and alpha.
|
162 | * @param {Number} [options.idx=0] - input horizontal offset
|
163 | * @param {Number} [options.idy=0] - input vertical offset
|
164 | * @param {Number} [options.odx=0] - output horizontal offset
|
165 | * @param {Number} [options.ody=0] - output vertical offset
|
166 | * @param {String} [options.interpolator=sharp.interpolators.bicubic] - interpolator
|
167 | * @returns {Sharp}
|
168 | * @throws {Error} Invalid parameters
|
169 | */
|
170 | function affine (matrix, options) {
|
171 | const flatMatrix = [].concat(...matrix);
|
172 | if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
|
173 | this.options.affineMatrix = flatMatrix;
|
174 | } else {
|
175 | throw is.invalidParameterError('matrix', '1x4 or 2x2 array', matrix);
|
176 | }
|
177 |
|
178 | if (is.defined(options)) {
|
179 | if (is.object(options)) {
|
180 | this._setBackgroundColourOption('affineBackground', options.background);
|
181 | if (is.defined(options.idx)) {
|
182 | if (is.number(options.idx)) {
|
183 | this.options.affineIdx = options.idx;
|
184 | } else {
|
185 | throw is.invalidParameterError('options.idx', 'number', options.idx);
|
186 | }
|
187 | }
|
188 | if (is.defined(options.idy)) {
|
189 | if (is.number(options.idy)) {
|
190 | this.options.affineIdy = options.idy;
|
191 | } else {
|
192 | throw is.invalidParameterError('options.idy', 'number', options.idy);
|
193 | }
|
194 | }
|
195 | if (is.defined(options.odx)) {
|
196 | if (is.number(options.odx)) {
|
197 | this.options.affineOdx = options.odx;
|
198 | } else {
|
199 | throw is.invalidParameterError('options.odx', 'number', options.odx);
|
200 | }
|
201 | }
|
202 | if (is.defined(options.ody)) {
|
203 | if (is.number(options.ody)) {
|
204 | this.options.affineOdy = options.ody;
|
205 | } else {
|
206 | throw is.invalidParameterError('options.ody', 'number', options.ody);
|
207 | }
|
208 | }
|
209 | if (is.defined(options.interpolator)) {
|
210 | if (is.inArray(options.interpolator, Object.values(this.constructor.interpolators))) {
|
211 | this.options.affineInterpolator = options.interpolator;
|
212 | } else {
|
213 | throw is.invalidParameterError('options.interpolator', 'valid interpolator name', options.interpolator);
|
214 | }
|
215 | }
|
216 | } else {
|
217 | throw is.invalidParameterError('options', 'object', options);
|
218 | }
|
219 | }
|
220 |
|
221 | return this;
|
222 | }
|
223 |
|
224 | /**
|
225 | * Sharpen the image.
|
226 | *
|
227 | * When used without parameters, performs a fast, mild sharpen of the output image.
|
228 | *
|
229 | * When a `sigma` is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
|
230 | * Fine-grained control over the level of sharpening in "flat" (m1) and "jagged" (m2) areas is available.
|
231 | *
|
232 | * See {@link https://www.libvips.org/API/current/libvips-convolution.html#vips-sharpen|libvips sharpen} operation.
|
233 | *
|
234 | * @example
|
235 | * const data = await sharp(input).sharpen().toBuffer();
|
236 | *
|
237 | * @example
|
238 | * const data = await sharp(input).sharpen({ sigma: 2 }).toBuffer();
|
239 | *
|
240 | * @example
|
241 | * const data = await sharp(input)
|
242 | * .sharpen({
|
243 | * sigma: 2,
|
244 | * m1: 0,
|
245 | * m2: 3,
|
246 | * x1: 3,
|
247 | * y2: 15,
|
248 | * y3: 15,
|
249 | * })
|
250 | * .toBuffer();
|
251 | *
|
252 | * @param {Object|number} [options] - if present, is an Object with attributes
|
253 | * @param {number} [options.sigma] - the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`, between 0.000001 and 10
|
254 | * @param {number} [options.m1=1.0] - the level of sharpening to apply to "flat" areas, between 0 and 1000000
|
255 | * @param {number} [options.m2=2.0] - the level of sharpening to apply to "jagged" areas, between 0 and 1000000
|
256 | * @param {number} [options.x1=2.0] - threshold between "flat" and "jagged", between 0 and 1000000
|
257 | * @param {number} [options.y2=10.0] - maximum amount of brightening, between 0 and 1000000
|
258 | * @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 | * @returns {Sharp}
|
262 | * @throws {Error} Invalid parameters
|
263 | */
|
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)) {
|
291 | if (is.number(options.sigma) && is.inRange(options.sigma, 0.000001, 10)) {
|
292 | this.options.sharpenSigma = options.sigma;
|
293 | } else {
|
294 | throw is.invalidParameterError('options.sigma', 'number between 0.000001 and 10', options.sigma);
|
295 | }
|
296 | if (is.defined(options.m1)) {
|
297 | if (is.number(options.m1) && is.inRange(options.m1, 0, 1000000)) {
|
298 | this.options.sharpenM1 = options.m1;
|
299 | } else {
|
300 | throw is.invalidParameterError('options.m1', 'number between 0 and 1000000', options.m1);
|
301 | }
|
302 | }
|
303 | if (is.defined(options.m2)) {
|
304 | if (is.number(options.m2) && is.inRange(options.m2, 0, 1000000)) {
|
305 | this.options.sharpenM2 = options.m2;
|
306 | } else {
|
307 | throw is.invalidParameterError('options.m2', 'number between 0 and 1000000', options.m2);
|
308 | }
|
309 | }
|
310 | if (is.defined(options.x1)) {
|
311 | if (is.number(options.x1) && is.inRange(options.x1, 0, 1000000)) {
|
312 | this.options.sharpenX1 = options.x1;
|
313 | } else {
|
314 | throw is.invalidParameterError('options.x1', 'number between 0 and 1000000', options.x1);
|
315 | }
|
316 | }
|
317 | if (is.defined(options.y2)) {
|
318 | if (is.number(options.y2) && is.inRange(options.y2, 0, 1000000)) {
|
319 | this.options.sharpenY2 = options.y2;
|
320 | } else {
|
321 | throw is.invalidParameterError('options.y2', 'number between 0 and 1000000', options.y2);
|
322 | }
|
323 | }
|
324 | if (is.defined(options.y3)) {
|
325 | if (is.number(options.y3) && is.inRange(options.y3, 0, 1000000)) {
|
326 | this.options.sharpenY3 = options.y3;
|
327 | } else {
|
328 | throw is.invalidParameterError('options.y3', 'number between 0 and 1000000', options.y3);
|
329 | }
|
330 | }
|
331 | } else {
|
332 | throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', options);
|
333 | }
|
334 | return this;
|
335 | }
|
336 |
|
337 | /**
|
338 | * Apply median filter.
|
339 | * When used without parameters the default window is 3x3.
|
340 | *
|
341 | * @example
|
342 | * const output = await sharp(input).median().toBuffer();
|
343 | *
|
344 | * @example
|
345 | * const output = await sharp(input).median(5).toBuffer();
|
346 | *
|
347 | * @param {number} [size=3] square mask size: size x size
|
348 | * @returns {Sharp}
|
349 | * @throws {Error} Invalid parameters
|
350 | */
|
351 | function median (size) {
|
352 | if (!is.defined(size)) {
|
353 | // No arguments: default to 3x3
|
354 | this.options.medianSize = 3;
|
355 | } else if (is.integer(size) && is.inRange(size, 1, 1000)) {
|
356 | // Numeric argument: specific sigma
|
357 | this.options.medianSize = size;
|
358 | } else {
|
359 | throw is.invalidParameterError('size', 'integer between 1 and 1000', size);
|
360 | }
|
361 | return this;
|
362 | }
|
363 |
|
364 | /**
|
365 | * Blur the image.
|
366 | *
|
367 | * When used without parameters, performs a fast 3x3 box blur (equivalent to a box linear filter).
|
368 | *
|
369 | * When a `sigma` is provided, performs a slower, more accurate Gaussian blur.
|
370 | *
|
371 | * @example
|
372 | * const boxBlurred = await sharp(input)
|
373 | * .blur()
|
374 | * .toBuffer();
|
375 | *
|
376 | * @example
|
377 | * const gaussianBlurred = await sharp(input)
|
378 | * .blur(5)
|
379 | * .toBuffer();
|
380 | *
|
381 | * @param {Object|number|Boolean} [options]
|
382 | * @param {number} [options.sigma] a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
383 | * @param {string} [options.precision='integer'] How accurate the operation should be, one of: integer, float, approximate.
|
384 | * @param {number} [options.minAmplitude=0.2] A value between 0.001 and 1. A smaller value will generate a larger, more accurate mask.
|
385 | * @returns {Sharp}
|
386 | * @throws {Error} Invalid parameters
|
387 | */
|
388 | function blur (options) {
|
389 | let sigma;
|
390 | if (is.number(options)) {
|
391 | sigma = options;
|
392 | } else if (is.plainObject(options)) {
|
393 | if (!is.number(options.sigma)) {
|
394 | throw is.invalidParameterError('options.sigma', 'number between 0.3 and 1000', sigma);
|
395 | }
|
396 | sigma = options.sigma;
|
397 | if ('precision' in options) {
|
398 | if (is.string(vipsPrecision[options.precision])) {
|
399 | this.options.precision = vipsPrecision[options.precision];
|
400 | } else {
|
401 | throw is.invalidParameterError('precision', 'one of: integer, float, approximate', options.precision);
|
402 | }
|
403 | }
|
404 | if ('minAmplitude' in options) {
|
405 | if (is.number(options.minAmplitude) && is.inRange(options.minAmplitude, 0.001, 1)) {
|
406 | this.options.minAmpl = options.minAmplitude;
|
407 | } else {
|
408 | throw is.invalidParameterError('minAmplitude', 'number between 0.001 and 1', options.minAmplitude);
|
409 | }
|
410 | }
|
411 | }
|
412 |
|
413 | if (!is.defined(options)) {
|
414 | // No arguments: default to mild blur
|
415 | this.options.blurSigma = -1;
|
416 | } else if (is.bool(options)) {
|
417 | // Boolean argument: apply mild blur?
|
418 | this.options.blurSigma = options ? -1 : 0;
|
419 | } else if (is.number(sigma) && is.inRange(sigma, 0.3, 1000)) {
|
420 | // Numeric argument: specific sigma
|
421 | this.options.blurSigma = sigma;
|
422 | } else {
|
423 | throw is.invalidParameterError('sigma', 'number between 0.3 and 1000', sigma);
|
424 | }
|
425 |
|
426 | return this;
|
427 | }
|
428 |
|
429 | /**
|
430 | * Merge alpha transparency channel, if any, with a background, then remove the alpha channel.
|
431 | *
|
432 | * See also {@link /api-channel#removealpha|removeAlpha}.
|
433 | *
|
434 | * @example
|
435 | * await sharp(rgbaInput)
|
436 | * .flatten({ background: '#F0A703' })
|
437 | * .toBuffer();
|
438 | *
|
439 | * @param {Object} [options]
|
440 | * @param {string|Object} [options.background={r: 0, g: 0, b: 0}] - background colour, parsed by the [color](https://www.npmjs.org/package/color) module, defaults to black.
|
441 | * @returns {Sharp}
|
442 | */
|
443 | function flatten (options) {
|
444 | this.options.flatten = is.bool(options) ? options : true;
|
445 | if (is.object(options)) {
|
446 | this._setBackgroundColourOption('flattenBackground', options.background);
|
447 | }
|
448 | return this;
|
449 | }
|
450 |
|
451 | /**
|
452 | * Ensure the image has an alpha channel
|
453 | * with all white pixel values made fully transparent.
|
454 | *
|
455 | * Existing alpha channel values for non-white pixels remain unchanged.
|
456 | *
|
457 | * This feature is experimental and the API may change.
|
458 | *
|
459 | * @since 0.32.1
|
460 | *
|
461 | * @example
|
462 | * await sharp(rgbInput)
|
463 | * .unflatten()
|
464 | * .toBuffer();
|
465 | *
|
466 | * @example
|
467 | * await sharp(rgbInput)
|
468 | * .threshold(128, { grayscale: false }) // converter bright pixels to white
|
469 | * .unflatten()
|
470 | * .toBuffer();
|
471 | */
|
472 | function unflatten () {
|
473 | this.options.unflatten = true;
|
474 | return this;
|
475 | }
|
476 |
|
477 | /**
|
478 | * Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of `1/gamma`
|
479 | * then increasing the encoding (brighten) post-resize at a factor of `gamma`.
|
480 | * This can improve the perceived brightness of a resized image in non-linear colour spaces.
|
481 | * JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation
|
482 | * when applying a gamma correction.
|
483 | *
|
484 | * Supply a second argument to use a different output gamma value, otherwise the first value is used in both cases.
|
485 | *
|
486 | * @param {number} [gamma=2.2] value between 1.0 and 3.0.
|
487 | * @param {number} [gammaOut] value between 1.0 and 3.0. (optional, defaults to same as `gamma`)
|
488 | * @returns {Sharp}
|
489 | * @throws {Error} Invalid parameters
|
490 | */
|
491 | function gamma (gamma, gammaOut) {
|
492 | if (!is.defined(gamma)) {
|
493 | // Default gamma correction of 2.2 (sRGB)
|
494 | this.options.gamma = 2.2;
|
495 | } else if (is.number(gamma) && is.inRange(gamma, 1, 3)) {
|
496 | this.options.gamma = gamma;
|
497 | } else {
|
498 | throw is.invalidParameterError('gamma', 'number between 1.0 and 3.0', gamma);
|
499 | }
|
500 | if (!is.defined(gammaOut)) {
|
501 | // Default gamma correction for output is same as input
|
502 | this.options.gammaOut = this.options.gamma;
|
503 | } else if (is.number(gammaOut) && is.inRange(gammaOut, 1, 3)) {
|
504 | this.options.gammaOut = gammaOut;
|
505 | } else {
|
506 | throw is.invalidParameterError('gammaOut', 'number between 1.0 and 3.0', gammaOut);
|
507 | }
|
508 | return this;
|
509 | }
|
510 |
|
511 | /**
|
512 | * Produce the "negative" of the image.
|
513 | *
|
514 | * @example
|
515 | * const output = await sharp(input)
|
516 | * .negate()
|
517 | * .toBuffer();
|
518 | *
|
519 | * @example
|
520 | * const output = await sharp(input)
|
521 | * .negate({ alpha: false })
|
522 | * .toBuffer();
|
523 | *
|
524 | * @param {Object} [options]
|
525 | * @param {Boolean} [options.alpha=true] Whether or not to negate any alpha channel
|
526 | * @returns {Sharp}
|
527 | */
|
528 | function negate (options) {
|
529 | this.options.negate = is.bool(options) ? options : true;
|
530 | if (is.plainObject(options) && 'alpha' in options) {
|
531 | if (!is.bool(options.alpha)) {
|
532 | throw is.invalidParameterError('alpha', 'should be boolean value', options.alpha);
|
533 | } else {
|
534 | this.options.negateAlpha = options.alpha;
|
535 | }
|
536 | }
|
537 | return this;
|
538 | }
|
539 |
|
540 | /**
|
541 | * Enhance output image contrast by stretching its luminance to cover a full dynamic range.
|
542 | *
|
543 | * Uses a histogram-based approach, taking a default range of 1% to 99% to reduce sensitivity to noise at the extremes.
|
544 | *
|
545 | * Luminance values below the `lower` percentile will be underexposed by clipping to zero.
|
546 | * Luminance values above the `upper` percentile will be overexposed by clipping to the max pixel value.
|
547 | *
|
548 | * @example
|
549 | * const output = await sharp(input)
|
550 | * .normalise()
|
551 | * .toBuffer();
|
552 | *
|
553 | * @example
|
554 | * const output = await sharp(input)
|
555 | * .normalise({ lower: 0, upper: 100 })
|
556 | * .toBuffer();
|
557 | *
|
558 | * @param {Object} [options]
|
559 | * @param {number} [options.lower=1] - Percentile below which luminance values will be underexposed.
|
560 | * @param {number} [options.upper=99] - Percentile above which luminance values will be overexposed.
|
561 | * @returns {Sharp}
|
562 | */
|
563 | function normalise (options) {
|
564 | if (is.plainObject(options)) {
|
565 | if (is.defined(options.lower)) {
|
566 | if (is.number(options.lower) && is.inRange(options.lower, 0, 99)) {
|
567 | this.options.normaliseLower = options.lower;
|
568 | } else {
|
569 | throw is.invalidParameterError('lower', 'number between 0 and 99', options.lower);
|
570 | }
|
571 | }
|
572 | if (is.defined(options.upper)) {
|
573 | if (is.number(options.upper) && is.inRange(options.upper, 1, 100)) {
|
574 | this.options.normaliseUpper = options.upper;
|
575 | } else {
|
576 | throw is.invalidParameterError('upper', 'number between 1 and 100', options.upper);
|
577 | }
|
578 | }
|
579 | }
|
580 | if (this.options.normaliseLower >= this.options.normaliseUpper) {
|
581 | throw is.invalidParameterError('range', 'lower to be less than upper',
|
582 | `${this.options.normaliseLower} >= ${this.options.normaliseUpper}`);
|
583 | }
|
584 | this.options.normalise = true;
|
585 | return this;
|
586 | }
|
587 |
|
588 | /**
|
589 | * Alternative spelling of normalise.
|
590 | *
|
591 | * @example
|
592 | * const output = await sharp(input)
|
593 | * .normalize()
|
594 | * .toBuffer();
|
595 | *
|
596 | * @param {Object} [options]
|
597 | * @param {number} [options.lower=1] - Percentile below which luminance values will be underexposed.
|
598 | * @param {number} [options.upper=99] - Percentile above which luminance values will be overexposed.
|
599 | * @returns {Sharp}
|
600 | */
|
601 | function normalize (options) {
|
602 | return this.normalise(options);
|
603 | }
|
604 |
|
605 | /**
|
606 | * Perform contrast limiting adaptive histogram equalization
|
607 | * {@link https://en.wikipedia.org/wiki/Adaptive_histogram_equalization#Contrast_Limited_AHE|CLAHE}.
|
608 | *
|
609 | * This will, in general, enhance the clarity of the image by bringing out darker details.
|
610 | *
|
611 | * @since 0.28.3
|
612 | *
|
613 | * @example
|
614 | * const output = await sharp(input)
|
615 | * .clahe({
|
616 | * width: 3,
|
617 | * height: 3,
|
618 | * })
|
619 | * .toBuffer();
|
620 | *
|
621 | * @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.
|
624 | * @param {number} [options.maxSlope=3] - Integral level of brightening, between 0 and 100, where 0 disables contrast limiting.
|
625 | * @returns {Sharp}
|
626 | * @throws {Error} Invalid parameters
|
627 | */
|
628 | function clahe (options) {
|
629 | if (is.plainObject(options)) {
|
630 | if (is.integer(options.width) && options.width > 0) {
|
631 | this.options.claheWidth = options.width;
|
632 | } else {
|
633 | throw is.invalidParameterError('width', 'integer greater than zero', options.width);
|
634 | }
|
635 | if (is.integer(options.height) && options.height > 0) {
|
636 | this.options.claheHeight = options.height;
|
637 | } else {
|
638 | throw is.invalidParameterError('height', 'integer greater than zero', options.height);
|
639 | }
|
640 | if (is.defined(options.maxSlope)) {
|
641 | if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) {
|
642 | this.options.claheMaxSlope = options.maxSlope;
|
643 | } else {
|
644 | throw is.invalidParameterError('maxSlope', 'integer between 0 and 100', options.maxSlope);
|
645 | }
|
646 | }
|
647 | } else {
|
648 | throw is.invalidParameterError('options', 'plain object', options);
|
649 | }
|
650 | return this;
|
651 | }
|
652 |
|
653 | /**
|
654 | * Convolve the image with the specified kernel.
|
655 | *
|
656 | * @example
|
657 | * sharp(input)
|
658 | * .convolve({
|
659 | * width: 3,
|
660 | * height: 3,
|
661 | * kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
|
662 | * })
|
663 | * .raw()
|
664 | * .toBuffer(function(err, data, info) {
|
665 | * // data contains the raw pixel data representing the convolution
|
666 | * // of the input image with the horizontal Sobel operator
|
667 | * });
|
668 | *
|
669 | * @param {Object} kernel
|
670 | * @param {number} kernel.width - width of the kernel in pixels.
|
671 | * @param {number} kernel.height - height of the kernel in pixels.
|
672 | * @param {Array<number>} kernel.kernel - Array of length `width*height` containing the kernel values.
|
673 | * @param {number} [kernel.scale=sum] - the scale of the kernel in pixels.
|
674 | * @param {number} [kernel.offset=0] - the offset of the kernel in pixels.
|
675 | * @returns {Sharp}
|
676 | * @throws {Error} Invalid parameters
|
677 | */
|
678 | function convolve (kernel) {
|
679 | if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
|
680 | !is.integer(kernel.width) || !is.integer(kernel.height) ||
|
681 | !is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
|
682 | kernel.height * kernel.width !== kernel.kernel.length
|
683 | ) {
|
684 | // must pass in a kernel
|
685 | throw new Error('Invalid convolution kernel');
|
686 | }
|
687 | // Default scale is sum of kernel values
|
688 | if (!is.integer(kernel.scale)) {
|
689 | kernel.scale = kernel.kernel.reduce(function (a, b) {
|
690 | return a + b;
|
691 | }, 0);
|
692 | }
|
693 | // Clip scale to a minimum value of 1
|
694 | if (kernel.scale < 1) {
|
695 | kernel.scale = 1;
|
696 | }
|
697 | if (!is.integer(kernel.offset)) {
|
698 | kernel.offset = 0;
|
699 | }
|
700 | this.options.convKernel = kernel;
|
701 | return this;
|
702 | }
|
703 |
|
704 | /**
|
705 | * Any pixel value greater than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
|
706 | * @param {number} [threshold=128] - a value in the range 0-255 representing the level at which the threshold will be applied.
|
707 | * @param {Object} [options]
|
708 | * @param {Boolean} [options.greyscale=true] - convert to single channel greyscale.
|
709 | * @param {Boolean} [options.grayscale=true] - alternative spelling for greyscale.
|
710 | * @returns {Sharp}
|
711 | * @throws {Error} Invalid parameters
|
712 | */
|
713 | function threshold (threshold, options) {
|
714 | if (!is.defined(threshold)) {
|
715 | this.options.threshold = 128;
|
716 | } else if (is.bool(threshold)) {
|
717 | this.options.threshold = threshold ? 128 : 0;
|
718 | } else if (is.integer(threshold) && is.inRange(threshold, 0, 255)) {
|
719 | this.options.threshold = threshold;
|
720 | } else {
|
721 | throw is.invalidParameterError('threshold', 'integer between 0 and 255', threshold);
|
722 | }
|
723 | if (!is.object(options) || options.greyscale === true || options.grayscale === true) {
|
724 | this.options.thresholdGrayscale = true;
|
725 | } else {
|
726 | this.options.thresholdGrayscale = false;
|
727 | }
|
728 | return this;
|
729 | }
|
730 |
|
731 | /**
|
732 | * Perform a bitwise boolean operation with operand image.
|
733 | *
|
734 | * This operation creates an output image where each pixel is the result of
|
735 | * the selected bitwise boolean `operation` between the corresponding pixels of the input images.
|
736 | *
|
737 | * @param {Buffer|string} operand - Buffer containing image data or string containing the path to an image file.
|
738 | * @param {string} operator - one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
|
739 | * @param {Object} [options]
|
740 | * @param {Object} [options.raw] - describes operand when using raw pixel data.
|
741 | * @param {number} [options.raw.width]
|
742 | * @param {number} [options.raw.height]
|
743 | * @param {number} [options.raw.channels]
|
744 | * @returns {Sharp}
|
745 | * @throws {Error} Invalid parameters
|
746 | */
|
747 | function boolean (operand, operator, options) {
|
748 | this.options.boolean = this._createInputDescriptor(operand, options);
|
749 | if (is.string(operator) && is.inArray(operator, ['and', 'or', 'eor'])) {
|
750 | this.options.booleanOp = operator;
|
751 | } else {
|
752 | throw is.invalidParameterError('operator', 'one of: and, or, eor', operator);
|
753 | }
|
754 | return this;
|
755 | }
|
756 |
|
757 | /**
|
758 | * Apply the linear formula `a` * input + `b` to the image to adjust image levels.
|
759 | *
|
760 | * When a single number is provided, it will be used for all image channels.
|
761 | * When an array of numbers is provided, the array length must match the number of channels.
|
762 | *
|
763 | * @example
|
764 | * await sharp(input)
|
765 | * .linear(0.5, 2)
|
766 | * .toBuffer();
|
767 | *
|
768 | * @example
|
769 | * await sharp(rgbInput)
|
770 | * .linear(
|
771 | * [0.25, 0.5, 0.75],
|
772 | * [150, 100, 50]
|
773 | * )
|
774 | * .toBuffer();
|
775 | *
|
776 | * @param {(number|number[])} [a=[]] multiplier
|
777 | * @param {(number|number[])} [b=[]] offset
|
778 | * @returns {Sharp}
|
779 | * @throws {Error} Invalid parameters
|
780 | */
|
781 | function linear (a, b) {
|
782 | if (!is.defined(a) && is.number(b)) {
|
783 | a = 1.0;
|
784 | } else if (is.number(a) && !is.defined(b)) {
|
785 | b = 0.0;
|
786 | }
|
787 | if (!is.defined(a)) {
|
788 | this.options.linearA = [];
|
789 | } else if (is.number(a)) {
|
790 | this.options.linearA = [a];
|
791 | } else if (Array.isArray(a) && a.length && a.every(is.number)) {
|
792 | this.options.linearA = a;
|
793 | } else {
|
794 | throw is.invalidParameterError('a', 'number or array of numbers', a);
|
795 | }
|
796 | if (!is.defined(b)) {
|
797 | this.options.linearB = [];
|
798 | } else if (is.number(b)) {
|
799 | this.options.linearB = [b];
|
800 | } else if (Array.isArray(b) && b.length && b.every(is.number)) {
|
801 | this.options.linearB = b;
|
802 | } else {
|
803 | throw is.invalidParameterError('b', 'number or array of numbers', b);
|
804 | }
|
805 | if (this.options.linearA.length !== this.options.linearB.length) {
|
806 | throw new Error('Expected a and b to be arrays of the same length');
|
807 | }
|
808 | return this;
|
809 | }
|
810 |
|
811 | /**
|
812 | * Recombine the image with the specified matrix.
|
813 | *
|
814 | * @since 0.21.1
|
815 | *
|
816 | * @example
|
817 | * sharp(input)
|
818 | * .recomb([
|
819 | * [0.3588, 0.7044, 0.1368],
|
820 | * [0.2990, 0.5870, 0.1140],
|
821 | * [0.2392, 0.4696, 0.0912],
|
822 | * ])
|
823 | * .raw()
|
824 | * .toBuffer(function(err, data, info) {
|
825 | * // data contains the raw pixel data after applying the matrix
|
826 | * // With this example input, a sepia filter has been applied
|
827 | * });
|
828 | *
|
829 | * @param {Array<Array<number>>} inputMatrix - 3x3 or 4x4 Recombination matrix
|
830 | * @returns {Sharp}
|
831 | * @throws {Error} Invalid parameters
|
832 | */
|
833 | function recomb (inputMatrix) {
|
834 | if (!Array.isArray(inputMatrix)) {
|
835 | throw is.invalidParameterError('inputMatrix', 'array', inputMatrix);
|
836 | }
|
837 | if (inputMatrix.length !== 3 && inputMatrix.length !== 4) {
|
838 | throw is.invalidParameterError('inputMatrix', '3x3 or 4x4 array', inputMatrix.length);
|
839 | }
|
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);
|
843 | }
|
844 | this.options.recombMatrix = recombMatrix;
|
845 | return this;
|
846 | }
|
847 |
|
848 | /**
|
849 | * Transforms the image using brightness, saturation, hue rotation, and lightness.
|
850 | * Brightness and lightness both operate on luminance, with the difference being that
|
851 | * brightness is multiplicative whereas lightness is additive.
|
852 | *
|
853 | * @since 0.22.1
|
854 | *
|
855 | * @example
|
856 | * // increase brightness by a factor of 2
|
857 | * const output = await sharp(input)
|
858 | * .modulate({
|
859 | * brightness: 2
|
860 | * })
|
861 | * .toBuffer();
|
862 | *
|
863 | * @example
|
864 | * // hue-rotate by 180 degrees
|
865 | * const output = await sharp(input)
|
866 | * .modulate({
|
867 | * hue: 180
|
868 | * })
|
869 | * .toBuffer();
|
870 | *
|
871 | * @example
|
872 | * // increase lightness by +50
|
873 | * const output = await sharp(input)
|
874 | * .modulate({
|
875 | * lightness: 50
|
876 | * })
|
877 | * .toBuffer();
|
878 | *
|
879 | * @example
|
880 | * // decrease brightness and saturation while also hue-rotating by 90 degrees
|
881 | * const output = await sharp(input)
|
882 | * .modulate({
|
883 | * brightness: 0.5,
|
884 | * saturation: 0.5,
|
885 | * hue: 90,
|
886 | * })
|
887 | * .toBuffer();
|
888 | *
|
889 | * @param {Object} [options]
|
890 | * @param {number} [options.brightness] Brightness multiplier
|
891 | * @param {number} [options.saturation] Saturation multiplier
|
892 | * @param {number} [options.hue] Degrees for hue rotation
|
893 | * @param {number} [options.lightness] Lightness addend
|
894 | * @returns {Sharp}
|
895 | */
|
896 | function modulate (options) {
|
897 | if (!is.plainObject(options)) {
|
898 | throw is.invalidParameterError('options', 'plain object', options);
|
899 | }
|
900 | if ('brightness' in options) {
|
901 | if (is.number(options.brightness) && options.brightness >= 0) {
|
902 | this.options.brightness = options.brightness;
|
903 | } else {
|
904 | throw is.invalidParameterError('brightness', 'number above zero', options.brightness);
|
905 | }
|
906 | }
|
907 | if ('saturation' in options) {
|
908 | if (is.number(options.saturation) && options.saturation >= 0) {
|
909 | this.options.saturation = options.saturation;
|
910 | } else {
|
911 | throw is.invalidParameterError('saturation', 'number above zero', options.saturation);
|
912 | }
|
913 | }
|
914 | if ('hue' in options) {
|
915 | if (is.integer(options.hue)) {
|
916 | this.options.hue = options.hue % 360;
|
917 | } else {
|
918 | throw is.invalidParameterError('hue', 'number', options.hue);
|
919 | }
|
920 | }
|
921 | if ('lightness' in options) {
|
922 | if (is.number(options.lightness)) {
|
923 | this.options.lightness = options.lightness;
|
924 | } else {
|
925 | throw is.invalidParameterError('lightness', 'number', options.lightness);
|
926 | }
|
927 | }
|
928 | return this;
|
929 | }
|
930 |
|
931 | /**
|
932 | * Decorate the Sharp prototype with operation-related functions.
|
933 | * @private
|
934 | */
|
935 | module.exports = function (Sharp) {
|
936 | Object.assign(Sharp.prototype, {
|
937 | rotate,
|
938 | flip,
|
939 | flop,
|
940 | affine,
|
941 | sharpen,
|
942 | median,
|
943 | blur,
|
944 | flatten,
|
945 | unflatten,
|
946 | gamma,
|
947 | negate,
|
948 | normalise,
|
949 | normalize,
|
950 | clahe,
|
951 | convolve,
|
952 | threshold,
|
953 | boolean,
|
954 | linear,
|
955 | recomb,
|
956 | modulate
|
957 | });
|
958 | };
|