Skip to content

Commit

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

/**
* Extracts the 'mean' from the image and adjust the image to try make set its gamma appropriately.
*/
autoGamma(): void;

/**
* Extracts the 'mean' from the image and adjust the image to try make set its gamma appropriately.
* @param channels - The channel(s) to set the gamma for.
*/
autoGamma(channels: Channels): void;

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

autoGamma(): void;
autoGamma(channelsOrUndefined?: Channels): void {
this.useExceptionPointer(exception => {
const channels = this.valueOrDefault(channelsOrUndefined, Channels.Composite);
ImageMagick._api._MagickImage_AutoGamma(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-gamma.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#autoGamma', () => {
it('should apply gamma correction to the image', () => {
TestImages.Builtin.logo.use(image => {
image.autoGamma();

expect(image).toHavePixelWithColor(496, 429, '#000001ff');
});
});

it('should only apply gamma correction to the specified channels', () => {
TestImages.Builtin.logo.use(image => {
image.autoGamma(Channels.Red);

expect(image).toHavePixelWithColor(496, 429, '#002d73ff');
});
});

it('should use the correct default channels', () => {
TestImages.Builtin.logo.use(imageA => {
imageA.clone(imageB => {
imageA.autoGamma();
imageB.autoGamma(Channels.Composite);

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

0 comments on commit fb9b32a

Please sign in to comment.