diff --git a/src/magick-image.ts b/src/magick-image.ts index c2e8555..40bf738 100644 --- a/src/magick-image.ts +++ b/src/magick-image.ts @@ -518,6 +518,22 @@ export interface IMagickImage extends IDisposable { */ autoThreshold(method: AutoThresholdMethod): void; + /** + * Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. + * @param width The width of the neighborhood in pixels. + * @param height The height of the neighborhood in pixels. + */ + bilateralBlur(width: number, height: number): void; + + /** + * Applies a non-linear, edge-preserving, and noise-reducing smoothing filter. + * @param width The width of the neighborhood in pixels. + * @param height The height of the neighborhood in pixels. + * @param intensitySigma The sigma in the intensity space. + * @param spatialSigma The sigma in the coordinate space. + */ + bilateralBlur(width: number, height: number, intensitySigma: number, spatialSigma: number): void; + /** * Blur image with the default blur factor (0x1). */ @@ -2380,6 +2396,18 @@ export class MagickImage extends NativeInstance implements IMagickImage { }); } + bilateralBlur(width: number, height: number): void; + bilateralBlur(width: number, height: number, intensitySigma: number, spatialSigma: number): void; + bilateralBlur(width: number, height: number, intensitySigmaOrUndefined?: number, spatialSigmaOrUndefined?: number): void + { + const intensitySigma = this.valueOrComputedDefault(intensitySigmaOrUndefined, () => Math.sqrt((width * width) + (height * height))); + const spatialSigma = this.valueOrDefault(spatialSigmaOrUndefined, intensitySigma * 0.25); + this.useException(exception => { + const instance = ImageMagick._api._MagickImage_BilateralBlur(this._instance, width, height, intensitySigma, spatialSigma, exception.ptr); + this._setInstance(instance, exception); + }); + } + blur(): void; blur(channels: Channels): void; blur(radius: number, sigma: number): void; @@ -3732,6 +3760,13 @@ export class MagickImage extends NativeInstance implements IMagickImage { return value; } + private valueOrComputedDefault(value: TType | undefined, defaultValue: () => TType): TType { + if (value === undefined) + return defaultValue(); + + return value; + } + private useException(func: (exception: Exception) => TReturnType): TReturnType { return Exception.use(func, error => { if (this.onWarning !== undefined) diff --git a/tests/magick-image/bilateral-blur.spec.ts b/tests/magick-image/bilateral-blur.spec.ts new file mode 100644 index 0000000..d17814e --- /dev/null +++ b/tests/magick-image/bilateral-blur.spec.ts @@ -0,0 +1,22 @@ +/* + Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. + Licensed under the Apache License, Version 2.0. +*/ + +import { TestFiles } from '@test/test-files'; + +describe('MagickImage#bilateralBlur', () => { + it('should change pixels of the image', () => { + TestFiles.Images.Builtin.logo.use(image => { + image.bilateralBlur(5, 5); + expect(image).toHavePixelWithColor(387, 435, '#b2191dff'); + }); + }); + + it('should use the specified sigma values', () => { + TestFiles.Images.Builtin.logo.use(image => { + image.bilateralBlur(5, 5, 10, 10); + expect(image).toHavePixelWithColor(387, 435, '#c11b1fff'); + }); + }); +});