From fb9b32a577a13aad7d44df7ff07539a94a0f0d19 Mon Sep 17 00:00:00 2001 From: Pepijn Peeters Date: Mon, 5 Feb 2024 15:40:30 +0100 Subject: [PATCH] Added autoGamma to MagickImage. --- src/magick-image.ts | 19 ++++++++++++++ tests/magick-image/auto-gamma.spec.ts | 36 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 tests/magick-image/auto-gamma.spec.ts diff --git a/src/magick-image.ts b/src/magick-image.ts index 01bfd420..9452ad06 100644 --- a/src/magick-image.ts +++ b/src/magick-image.ts @@ -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. */ @@ -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); diff --git a/tests/magick-image/auto-gamma.spec.ts b/tests/magick-image/auto-gamma.spec.ts new file mode 100644 index 00000000..19799368 --- /dev/null +++ b/tests/magick-image/auto-gamma.spec.ts @@ -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); + }) + }); + }); +});