123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919 |
- 'use strict';
- const color = require('color');
- const is = require('./is');
- function rotate (angle, options) {
- if (this.options.useExifOrientation || this.options.angle || this.options.rotationAngle) {
- this.options.debuglog('ignoring previous rotate options');
- }
- if (!is.defined(angle)) {
- this.options.useExifOrientation = true;
- } else if (is.integer(angle) && !(angle % 90)) {
- this.options.angle = angle;
- } else if (is.number(angle)) {
- this.options.rotationAngle = angle;
- if (is.object(options) && options.background) {
- const backgroundColour = color(options.background);
- this.options.rotationBackground = [
- backgroundColour.red(),
- backgroundColour.green(),
- backgroundColour.blue(),
- Math.round(backgroundColour.alpha() * 255)
- ];
- }
- } else {
- throw is.invalidParameterError('angle', 'numeric', angle);
- }
- return this;
- }
- function flip (flip) {
- this.options.flip = is.bool(flip) ? flip : true;
- return this;
- }
- function flop (flop) {
- this.options.flop = is.bool(flop) ? flop : true;
- return this;
- }
- function affine (matrix, options) {
- const flatMatrix = [].concat(...matrix);
- if (flatMatrix.length === 4 && flatMatrix.every(is.number)) {
- this.options.affineMatrix = flatMatrix;
- } else {
- throw is.invalidParameterError('matrix', '1x4 or 2x2 array', matrix);
- }
- if (is.defined(options)) {
- if (is.object(options)) {
- this._setBackgroundColourOption('affineBackground', options.background);
- if (is.defined(options.idx)) {
- if (is.number(options.idx)) {
- this.options.affineIdx = options.idx;
- } else {
- throw is.invalidParameterError('options.idx', 'number', options.idx);
- }
- }
- if (is.defined(options.idy)) {
- if (is.number(options.idy)) {
- this.options.affineIdy = options.idy;
- } else {
- throw is.invalidParameterError('options.idy', 'number', options.idy);
- }
- }
- if (is.defined(options.odx)) {
- if (is.number(options.odx)) {
- this.options.affineOdx = options.odx;
- } else {
- throw is.invalidParameterError('options.odx', 'number', options.odx);
- }
- }
- if (is.defined(options.ody)) {
- if (is.number(options.ody)) {
- this.options.affineOdy = options.ody;
- } else {
- throw is.invalidParameterError('options.ody', 'number', options.ody);
- }
- }
- if (is.defined(options.interpolator)) {
- if (is.inArray(options.interpolator, Object.values(this.constructor.interpolators))) {
- this.options.affineInterpolator = options.interpolator;
- } else {
- throw is.invalidParameterError('options.interpolator', 'valid interpolator name', options.interpolator);
- }
- }
- } else {
- throw is.invalidParameterError('options', 'object', options);
- }
- }
- return this;
- }
- function sharpen (options, flat, jagged) {
- if (!is.defined(options)) {
-
- this.options.sharpenSigma = -1;
- } else if (is.bool(options)) {
-
- this.options.sharpenSigma = options ? -1 : 0;
- } else if (is.number(options) && is.inRange(options, 0.01, 10000)) {
-
- this.options.sharpenSigma = options;
-
- if (is.defined(flat)) {
- if (is.number(flat) && is.inRange(flat, 0, 10000)) {
- this.options.sharpenM1 = flat;
- } else {
- throw is.invalidParameterError('flat', 'number between 0 and 10000', flat);
- }
- }
-
- if (is.defined(jagged)) {
- if (is.number(jagged) && is.inRange(jagged, 0, 10000)) {
- this.options.sharpenM2 = jagged;
- } else {
- throw is.invalidParameterError('jagged', 'number between 0 and 10000', jagged);
- }
- }
- } else if (is.plainObject(options)) {
- if (is.number(options.sigma) && is.inRange(options.sigma, 0.000001, 10)) {
- this.options.sharpenSigma = options.sigma;
- } else {
- throw is.invalidParameterError('options.sigma', 'number between 0.000001 and 10', options.sigma);
- }
- if (is.defined(options.m1)) {
- if (is.number(options.m1) && is.inRange(options.m1, 0, 1000000)) {
- this.options.sharpenM1 = options.m1;
- } else {
- throw is.invalidParameterError('options.m1', 'number between 0 and 1000000', options.m1);
- }
- }
- if (is.defined(options.m2)) {
- if (is.number(options.m2) && is.inRange(options.m2, 0, 1000000)) {
- this.options.sharpenM2 = options.m2;
- } else {
- throw is.invalidParameterError('options.m2', 'number between 0 and 1000000', options.m2);
- }
- }
- if (is.defined(options.x1)) {
- if (is.number(options.x1) && is.inRange(options.x1, 0, 1000000)) {
- this.options.sharpenX1 = options.x1;
- } else {
- throw is.invalidParameterError('options.x1', 'number between 0 and 1000000', options.x1);
- }
- }
- if (is.defined(options.y2)) {
- if (is.number(options.y2) && is.inRange(options.y2, 0, 1000000)) {
- this.options.sharpenY2 = options.y2;
- } else {
- throw is.invalidParameterError('options.y2', 'number between 0 and 1000000', options.y2);
- }
- }
- if (is.defined(options.y3)) {
- if (is.number(options.y3) && is.inRange(options.y3, 0, 1000000)) {
- this.options.sharpenY3 = options.y3;
- } else {
- throw is.invalidParameterError('options.y3', 'number between 0 and 1000000', options.y3);
- }
- }
- } else {
- throw is.invalidParameterError('sigma', 'number between 0.01 and 10000', options);
- }
- return this;
- }
- function median (size) {
- if (!is.defined(size)) {
-
- this.options.medianSize = 3;
- } else if (is.integer(size) && is.inRange(size, 1, 1000)) {
-
- this.options.medianSize = size;
- } else {
- throw is.invalidParameterError('size', 'integer between 1 and 1000', size);
- }
- return this;
- }
- function blur (sigma) {
- if (!is.defined(sigma)) {
-
- this.options.blurSigma = -1;
- } else if (is.bool(sigma)) {
-
- this.options.blurSigma = sigma ? -1 : 0;
- } else if (is.number(sigma) && is.inRange(sigma, 0.3, 1000)) {
-
- this.options.blurSigma = sigma;
- } else {
- throw is.invalidParameterError('sigma', 'number between 0.3 and 1000', sigma);
- }
- return this;
- }
- function flatten (options) {
- this.options.flatten = is.bool(options) ? options : true;
- if (is.object(options)) {
- this._setBackgroundColourOption('flattenBackground', options.background);
- }
- return this;
- }
- function unflatten () {
- this.options.unflatten = true;
- return this;
- }
- function gamma (gamma, gammaOut) {
- if (!is.defined(gamma)) {
-
- this.options.gamma = 2.2;
- } else if (is.number(gamma) && is.inRange(gamma, 1, 3)) {
- this.options.gamma = gamma;
- } else {
- throw is.invalidParameterError('gamma', 'number between 1.0 and 3.0', gamma);
- }
- if (!is.defined(gammaOut)) {
-
- this.options.gammaOut = this.options.gamma;
- } else if (is.number(gammaOut) && is.inRange(gammaOut, 1, 3)) {
- this.options.gammaOut = gammaOut;
- } else {
- throw is.invalidParameterError('gammaOut', 'number between 1.0 and 3.0', gammaOut);
- }
- return this;
- }
- function negate (options) {
- this.options.negate = is.bool(options) ? options : true;
- if (is.plainObject(options) && 'alpha' in options) {
- if (!is.bool(options.alpha)) {
- throw is.invalidParameterError('alpha', 'should be boolean value', options.alpha);
- } else {
- this.options.negateAlpha = options.alpha;
- }
- }
- return this;
- }
- function normalise (options) {
- if (is.plainObject(options)) {
- if (is.defined(options.lower)) {
- if (is.number(options.lower) && is.inRange(options.lower, 0, 99)) {
- this.options.normaliseLower = options.lower;
- } else {
- throw is.invalidParameterError('lower', 'number between 0 and 99', options.lower);
- }
- }
- if (is.defined(options.upper)) {
- if (is.number(options.upper) && is.inRange(options.upper, 1, 100)) {
- this.options.normaliseUpper = options.upper;
- } else {
- throw is.invalidParameterError('upper', 'number between 1 and 100', options.upper);
- }
- }
- }
- if (this.options.normaliseLower >= this.options.normaliseUpper) {
- throw is.invalidParameterError('range', 'lower to be less than upper',
- `${this.options.normaliseLower} >= ${this.options.normaliseUpper}`);
- }
- this.options.normalise = true;
- return this;
- }
- function normalize (options) {
- return this.normalise(options);
- }
- function clahe (options) {
- if (is.plainObject(options)) {
- if (is.integer(options.width) && options.width > 0) {
- this.options.claheWidth = options.width;
- } else {
- throw is.invalidParameterError('width', 'integer greater than zero', options.width);
- }
- if (is.integer(options.height) && options.height > 0) {
- this.options.claheHeight = options.height;
- } else {
- throw is.invalidParameterError('height', 'integer greater than zero', options.height);
- }
- if (is.defined(options.maxSlope)) {
- if (is.integer(options.maxSlope) && is.inRange(options.maxSlope, 0, 100)) {
- this.options.claheMaxSlope = options.maxSlope;
- } else {
- throw is.invalidParameterError('maxSlope', 'integer between 0 and 100', options.maxSlope);
- }
- }
- } else {
- throw is.invalidParameterError('options', 'plain object', options);
- }
- return this;
- }
- function convolve (kernel) {
- if (!is.object(kernel) || !Array.isArray(kernel.kernel) ||
- !is.integer(kernel.width) || !is.integer(kernel.height) ||
- !is.inRange(kernel.width, 3, 1001) || !is.inRange(kernel.height, 3, 1001) ||
- kernel.height * kernel.width !== kernel.kernel.length
- ) {
-
- throw new Error('Invalid convolution kernel');
- }
-
- if (!is.integer(kernel.scale)) {
- kernel.scale = kernel.kernel.reduce(function (a, b) {
- return a + b;
- }, 0);
- }
-
- if (kernel.scale < 1) {
- kernel.scale = 1;
- }
- if (!is.integer(kernel.offset)) {
- kernel.offset = 0;
- }
- this.options.convKernel = kernel;
- return this;
- }
- function threshold (threshold, options) {
- if (!is.defined(threshold)) {
- this.options.threshold = 128;
- } else if (is.bool(threshold)) {
- this.options.threshold = threshold ? 128 : 0;
- } else if (is.integer(threshold) && is.inRange(threshold, 0, 255)) {
- this.options.threshold = threshold;
- } else {
- throw is.invalidParameterError('threshold', 'integer between 0 and 255', threshold);
- }
- if (!is.object(options) || options.greyscale === true || options.grayscale === true) {
- this.options.thresholdGrayscale = true;
- } else {
- this.options.thresholdGrayscale = false;
- }
- return this;
- }
- function boolean (operand, operator, options) {
- this.options.boolean = this._createInputDescriptor(operand, options);
- if (is.string(operator) && is.inArray(operator, ['and', 'or', 'eor'])) {
- this.options.booleanOp = operator;
- } else {
- throw is.invalidParameterError('operator', 'one of: and, or, eor', operator);
- }
- return this;
- }
- function linear (a, b) {
- if (!is.defined(a) && is.number(b)) {
- a = 1.0;
- } else if (is.number(a) && !is.defined(b)) {
- b = 0.0;
- }
- if (!is.defined(a)) {
- this.options.linearA = [];
- } else if (is.number(a)) {
- this.options.linearA = [a];
- } else if (Array.isArray(a) && a.length && a.every(is.number)) {
- this.options.linearA = a;
- } else {
- throw is.invalidParameterError('a', 'number or array of numbers', a);
- }
- if (!is.defined(b)) {
- this.options.linearB = [];
- } else if (is.number(b)) {
- this.options.linearB = [b];
- } else if (Array.isArray(b) && b.length && b.every(is.number)) {
- this.options.linearB = b;
- } else {
- throw is.invalidParameterError('b', 'number or array of numbers', b);
- }
- if (this.options.linearA.length !== this.options.linearB.length) {
- throw new Error('Expected a and b to be arrays of the same length');
- }
- return this;
- }
- function recomb (inputMatrix) {
- if (!Array.isArray(inputMatrix) || inputMatrix.length !== 3 ||
- inputMatrix[0].length !== 3 ||
- inputMatrix[1].length !== 3 ||
- inputMatrix[2].length !== 3
- ) {
-
- throw new Error('Invalid recombination matrix');
- }
- this.options.recombMatrix = [
- inputMatrix[0][0], inputMatrix[0][1], inputMatrix[0][2],
- inputMatrix[1][0], inputMatrix[1][1], inputMatrix[1][2],
- inputMatrix[2][0], inputMatrix[2][1], inputMatrix[2][2]
- ].map(Number);
- return this;
- }
- function modulate (options) {
- if (!is.plainObject(options)) {
- throw is.invalidParameterError('options', 'plain object', options);
- }
- if ('brightness' in options) {
- if (is.number(options.brightness) && options.brightness >= 0) {
- this.options.brightness = options.brightness;
- } else {
- throw is.invalidParameterError('brightness', 'number above zero', options.brightness);
- }
- }
- if ('saturation' in options) {
- if (is.number(options.saturation) && options.saturation >= 0) {
- this.options.saturation = options.saturation;
- } else {
- throw is.invalidParameterError('saturation', 'number above zero', options.saturation);
- }
- }
- if ('hue' in options) {
- if (is.integer(options.hue)) {
- this.options.hue = options.hue % 360;
- } else {
- throw is.invalidParameterError('hue', 'number', options.hue);
- }
- }
- if ('lightness' in options) {
- if (is.number(options.lightness)) {
- this.options.lightness = options.lightness;
- } else {
- throw is.invalidParameterError('lightness', 'number', options.lightness);
- }
- }
- return this;
- }
- module.exports = function (Sharp) {
- Object.assign(Sharp.prototype, {
- rotate,
- flip,
- flop,
- affine,
- sharpen,
- median,
- blur,
- flatten,
- unflatten,
- gamma,
- negate,
- normalise,
- normalize,
- clahe,
- convolve,
- threshold,
- boolean,
- linear,
- recomb,
- modulate
- });
- };
|