Skip to content

Commit

Permalink
Added autoLevel to MagickImage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peeterush committed Feb 6, 2024
1 parent fb9b32a commit 4c0d052
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,19 @@ export interface IMagickImage extends IDisposable {
*/
autoGamma(channels: Channels): void;

/**
* Adjusts the levels of a particular image channel by scaling the minimum and maximum
* values to the full quantum range.
*/
autoLevel(): void;

/**
* Adjusts the levels of a particular image channel by scaling the minimum and maximum
* values to the full quantum range.
* @param channels - The channel(s) to level.
*/
autoLevel(channels: Channels): void;

/**
* Adjusts an image so that its orientation is suitable for viewing.
*/
Expand Down Expand Up @@ -1937,6 +1950,14 @@ export class MagickImage extends NativeInstance implements IMagickImage {
});
}

autoLevel(): void;
autoLevel(channelsOrUndefined?: Channels): void {
this.useExceptionPointer(exception => {
const channels = this.valueOrDefault(channelsOrUndefined, Channels.Undefined);
ImageMagick._api._MagickImage_AutoLevel(this._instance, channels, exception);
});
}

autoOrient(): void {
this.useException(exception => {
const instance = ImageMagick._api._MagickImage_AutoOrient(this._instance, exception.ptr);
Expand Down
36 changes: 36 additions & 0 deletions tests/magick-image/auto-level.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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#autoLevel', () => {
it('should auto level to the image', () => {
TestImages.Builtin.rose.use(image => {
image.autoLevel();

expect(image).toHavePixelWithColor(5, 40, '#5b5646ff');
});
});

it('should only auto level the specified channels', () => {
TestImages.Builtin.rose.use(image => {
image.autoLevel(Channels.Red);

expect(image).toHavePixelWithColor(5, 40, '#516556ff');
});
});

it('should use the correct default channels', () => {
TestImages.Builtin.rose.use(imageA => {
imageA.clone(imageB => {
imageA.autoLevel();
imageB.autoLevel(Channels.Undefined);

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

0 comments on commit 4c0d052

Please sign in to comment.