Skip to content

Commit

Permalink
Correct channel detection in MagickImage.blur
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeterush committed Feb 6, 2024
1 parent 96040f7 commit f69e514
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1935,15 +1935,17 @@ export class MagickImage extends NativeInstance implements IMagickImage {
blur(channels: Channels): void;
blur(radius: number, sigma: number): void;
blur(radius: number, sigma: number, channels: Channels): void;
blur(radiusOrChannel?: number | Channels, sigmaOrUndefined?: number, channelsOrUndefined?: Channels): void {
blur(radiusOrChannels?: number | Channels, sigmaOrUndefined?: number, channelsOrUndefined?: Channels): void {
let radius = 0;
const sigma = this.valueOrDefault(sigmaOrUndefined, 1);
let channels = this.valueOrDefault(channelsOrUndefined, Channels.Undefined);

if (typeof radiusOrChannel === 'number')
radius = radiusOrChannel;
else if (radiusOrChannel !== undefined)
channels = radiusOrChannel;
if (radiusOrChannels !== undefined) {
if (sigmaOrUndefined === undefined)
channels = radiusOrChannels;
else
radius = radiusOrChannels;
}

this.useException(exception => {
const instance = ImageMagick._api._MagickImage_Blur(this._instance, radius, sigma, channels, exception.ptr);
Expand Down
13 changes: 13 additions & 0 deletions tests/magick-image/blur.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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#blur', () => {
Expand All @@ -12,6 +13,18 @@ describe('MagickImage#blur', () => {
});
});

it('should not confuse channels for radius', () => {
TestImages.Builtin.logo.use(imageA => {
imageA.clone(imageB => {
imageA.blur(Channels.Blue);
imageB.blur(4, 1);

const difference = imageB.compare(imageA, ErrorMetric.RootMeanSquared);
expect(difference).not.toBe(0);
})
});
});

it('should only blur the specified channel', () => {
TestImages.Builtin.logo.use(image => {
image.blur(5, 5, Channels.Green);
Expand Down

0 comments on commit f69e514

Please sign in to comment.