Skip to content

Commit

Permalink
Add threshold to MagickImage (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
with-heart authored Oct 17, 2023
1 parent 1f55996 commit eb38214
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions tests/magick-image/threshold.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});

0 comments on commit eb38214

Please sign in to comment.