Skip to content

Commit

Permalink
Added bilateralBlur to MagickImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Jan 8, 2025
1 parent ee5a20b commit 54e37a3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -3732,6 +3760,13 @@ export class MagickImage extends NativeInstance implements IMagickImage {
return value;
}

private valueOrComputedDefault<TType>(value: TType | undefined, defaultValue: () => TType): TType {
if (value === undefined)
return defaultValue();

return value;
}

private useException<TReturnType>(func: (exception: Exception) => TReturnType): TReturnType {
return Exception.use(func, error => {
if (this.onWarning !== undefined)
Expand Down
22 changes: 22 additions & 0 deletions tests/magick-image/bilateral-blur.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 54e37a3

Please sign in to comment.