diff --git a/src/magick-image.ts b/src/magick-image.ts index d0de66b1..75a53cec 100644 --- a/src/magick-image.ts +++ b/src/magick-image.ts @@ -257,6 +257,8 @@ export interface IMagickImage extends IDisposable { statistics(): IStatistics; statistics(channels: Channels): IStatistics; strip(): void; + threshold(percentage: Percentage): void; + threshold(percentage: Percentage, channels: Channels): void; toString(): string; transparent(color: MagickColor): void; trim(): void; @@ -1386,6 +1388,15 @@ export class MagickImage extends NativeInstance implements IMagickImage { }); } + threshold(percentage: Percentage): void + threshold(percentage: Percentage, channels: Channels): void + threshold(percentage: Percentage, channelsOrUndefined?: Channels): void { + const channels = this.valueOrDefault(channelsOrUndefined, Channels.Undefined); + Exception.usePointer(exception => { + ImageMagick._api._MagickImage_Threshold(this._instance, percentage.toQuantum(), channels, exception); + }); + } + toString = (): string => `${this.format} ${this.width}x${this.height} ${this.depth}-bit ${ColorSpace[this.colorSpace]}` transparent(color: MagickColor): void { diff --git a/tests/magick-image/threshold.spec.ts b/tests/magick-image/threshold.spec.ts new file mode 100644 index 00000000..222f1966 --- /dev/null +++ b/tests/magick-image/threshold.spec.ts @@ -0,0 +1,32 @@ +// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. +// Licensed under the Apache License, Version 2.0. + +import { Channels } from '../../src/channels'; +import { ErrorMetric } from '../../src/error-metric'; +import { Percentage } from '../../src/percentage'; +import { TestImages } from '../test-images'; + +describe('MagickImage#threshold', () => { + it('should threshold the image with the correct default values', () => { + TestImages.Builtin.logo.use((image) => { + image.clone((other) => { + image.threshold(new Percentage(80)); + other.threshold(new Percentage(80), Channels.Undefined); + + const difference = other.compare(image, ErrorMetric.RootMeanSquared); + expect(difference).toBe(0); + }); + }); + }); + + it('should threshold the image', () => { + TestImages.Builtin.logo.use((image) => { + image.clone((other) => { + image.threshold(new Percentage(80)); + + const difference = other.compare(image, ErrorMetric.RootMeanSquared); + expect(difference).toBeCloseTo(0.165); + }); + }); + }); +});