mirror of
https://github.com/lovell/sharp.git
synced 2025-12-18 23:05:04 +01:00
Expose optional precision parameter of blur operation (#4168)
This commit is contained in:
committed by
GitHub
parent
10c6f474d9
commit
67a4592756
@@ -226,6 +226,7 @@ const Sharp = function (input, options) {
|
||||
negateAlpha: true,
|
||||
medianSize: 0,
|
||||
blurSigma: 0,
|
||||
precision: 'integer',
|
||||
sharpenSigma: 0,
|
||||
sharpenM1: 1,
|
||||
sharpenM2: 2,
|
||||
|
||||
11
lib/index.d.ts
vendored
11
lib/index.d.ts
vendored
@@ -464,7 +464,7 @@ declare namespace sharp {
|
||||
* @throws {Error} Invalid parameters
|
||||
* @returns A sharp instance that can be used to chain operations
|
||||
*/
|
||||
blur(sigma?: number | boolean): Sharp;
|
||||
blur(sigma?: number | boolean | BlurOptions): Sharp;
|
||||
|
||||
/**
|
||||
* Merge alpha transparency channel, if any, with background.
|
||||
@@ -1342,6 +1342,15 @@ declare namespace sharp {
|
||||
background?: Color | undefined;
|
||||
}
|
||||
|
||||
type Precision = 'integer' | 'float' | 'approximate';
|
||||
|
||||
interface BlurOptions {
|
||||
/** A value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2` */
|
||||
sigma: number;
|
||||
/** How accurate the operation should be, one of: integer, float, approximate. (optional, default "integer") */
|
||||
precision?: Precision | undefined;
|
||||
}
|
||||
|
||||
interface FlattenOptions {
|
||||
/** background colour, parsed by the color module, defaults to black. (optional, default {r:0,g:0,b:0}) */
|
||||
background?: Color | undefined;
|
||||
|
||||
@@ -6,6 +6,17 @@
|
||||
const color = require('color');
|
||||
const is = require('./is');
|
||||
|
||||
/**
|
||||
* How accurate an operation should be.
|
||||
* @member
|
||||
* @private
|
||||
*/
|
||||
const vipsPrecision = {
|
||||
integer: 'integer',
|
||||
float: 'float',
|
||||
approximate: 'approximate'
|
||||
};
|
||||
|
||||
/**
|
||||
* Rotate the output image by either an explicit angle
|
||||
* or auto-orient based on the EXIF `Orientation` tag.
|
||||
@@ -367,23 +378,43 @@ function median (size) {
|
||||
* .blur(5)
|
||||
* .toBuffer();
|
||||
*
|
||||
* @param {number} [sigma] a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
* @param {Object|number|Boolean} [options]
|
||||
* @param {number} [options.sigma] a value between 0.3 and 1000 representing the sigma of the Gaussian mask, where `sigma = 1 + radius / 2`.
|
||||
* @param {string} [options.precision='integer'] How accurate the operation should be, one of: integer, float, approximate.
|
||||
* @returns {Sharp}
|
||||
* @throws {Error} Invalid parameters
|
||||
*/
|
||||
function blur (sigma) {
|
||||
if (!is.defined(sigma)) {
|
||||
function blur (options) {
|
||||
let sigma;
|
||||
if (is.number(options)) {
|
||||
sigma = options;
|
||||
} else if (is.plainObject(options)) {
|
||||
if (!is.number(options.sigma)) {
|
||||
throw is.invalidParameterError('options.sigma', 'number between 0.3 and 1000', sigma);
|
||||
}
|
||||
sigma = options.sigma;
|
||||
if ('precision' in options) {
|
||||
if (is.string(vipsPrecision[options.precision])) {
|
||||
this.options.precision = vipsPrecision[options.precision];
|
||||
} else {
|
||||
throw is.invalidParameterError('precision', 'one of: integer, float, approximate', options.precision);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!is.defined(options)) {
|
||||
// No arguments: default to mild blur
|
||||
this.options.blurSigma = -1;
|
||||
} else if (is.bool(sigma)) {
|
||||
} else if (is.bool(options)) {
|
||||
// Boolean argument: apply mild blur?
|
||||
this.options.blurSigma = sigma ? -1 : 0;
|
||||
this.options.blurSigma = options ? -1 : 0;
|
||||
} else if (is.number(sigma) && is.inRange(sigma, 0.3, 1000)) {
|
||||
// Numeric argument: specific sigma
|
||||
this.options.blurSigma = sigma;
|
||||
} else {
|
||||
throw is.invalidParameterError('sigma', 'number between 0.3 and 1000', sigma);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user