From b97f65cb50ab6cad95e1fea2047b69536fa02d93 Mon Sep 17 00:00:00 2001 From: Pepijn Peeters Date: Mon, 5 Feb 2024 15:41:59 +0100 Subject: [PATCH] Added gaussianBlur to MagickImage. --- src/magick-image.ts | 34 ++++++++++++++++++++ tests/magick-image/gaussian-blur.spec.ts | 40 ++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/magick-image/gaussian-blur.spec.ts diff --git a/src/magick-image.ts b/src/magick-image.ts index e04ef390..d1c777a5 100644 --- a/src/magick-image.ts +++ b/src/magick-image.ts @@ -938,6 +938,27 @@ export interface IMagickImage extends IDisposable { */ flop(): void; + /** + * Gaussian blur image. + * @param radius - The number of neighbor pixels to be included in the convolution. + */ + gaussianBlur(radius: number): void; + + /** + * Gaussian blur image. + * @param radius - The number of neighbor pixels to be included in the convolution. + * @param sigma - The standard deviation of the gaussian bell curve. + */ + gaussianBlur(radius: number, sigma: number): void; + + /** + * Gaussian blur image. + * @param radius - The number of neighbor pixels to be included in the convolution. + * @param sigma - The standard deviation of the gaussian bell curve. + * @param sigma - The channel(s) to blur. + */ + gaussianBlur(radius: number, sigma: number, channels: Channels): void; + /** * Returns the value of the artifact with the specified name. * @param name - The name of the artifact. @@ -2432,6 +2453,19 @@ export class MagickImage extends NativeInstance implements IMagickImage { }); } + gaussianBlur(radius: number): void; + gaussianBlur(radius: number, sigma: number): void; + gaussianBlur(radius: number, sigma: number, channels: Channels): void; + gaussianBlur(radius: number, sigmaOrUndefined?: number, channelsOrUndefined?: Channels): void { + const sigma = this.valueOrDefault(sigmaOrUndefined, 1.0); + const channels = this.valueOrDefault(channelsOrUndefined, Channels.Undefined); + + this.useException(exception => { + const instance = ImageMagick._api._MagickImage_GaussianBlur(this._instance, radius, sigma, channels, exception.ptr); + this._setInstance(instance, exception); + }); + } + getArtifact(name: string): string | null { return _withString(name, namePtr => { const value = ImageMagick._api._MagickImage_GetArtifact(this._instance, namePtr); diff --git a/tests/magick-image/gaussian-blur.spec.ts b/tests/magick-image/gaussian-blur.spec.ts new file mode 100644 index 00000000..5bd8b811 --- /dev/null +++ b/tests/magick-image/gaussian-blur.spec.ts @@ -0,0 +1,40 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. +// Licensed under the Apache License, Version 2.0. + +import { Channels } from '@src/enums/channels'; +import { ErrorMetric } from '@src/enums/error-metric'; +import { TestImages } from '@test/test-images'; + +describe('MagickImage#gaussianBlur', () => { + it('should gaussian blur the image', () => { + TestImages.Builtin.wizard.use(image => { + image.clone(other => { + image.gaussianBlur(5.5, 10.2); + other.blur(5.5, 10.2); + + const difference = other.compare(image, ErrorMetric.RootMeanSquared); + expect(difference).toBeCloseTo(0.000665, 6); + }) + }); + }); + + it('should use the correct default sigma', () => { + TestImages.Builtin.wizard.use(image => { + image.clone(other => { + image.gaussianBlur(4.2); + other.gaussianBlur(4.2, 1.0); + + const difference = other.compare(image, ErrorMetric.RootMeanSquared); + expect(difference).toEqual(0); + }); + }); + }); + + it('should only blur the specified channels', () => { + TestImages.Builtin.wizard.use(image => { + image.gaussianBlur(4.2, 1, Channels.Green); + + expect(image).toHavePixelWithColor(120, 200, '#185338ff'); + }); + }); +});