Skip to content

Commit

Permalink
Added gaussianBlur to MagickImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeterush authored and dlemstra committed Feb 8, 2024
1 parent a481acb commit 9b4fc7e
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2434,6 +2455,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);
Expand Down
40 changes: 40 additions & 0 deletions tests/magick-image/gaussian-blur.spec.ts
Original file line number Diff line number Diff line change
@@ -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');
});
});
});

0 comments on commit 9b4fc7e

Please sign in to comment.