Skip to content

Commit

Permalink
Added formatExpression to MagickImage (#191).
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Dec 5, 2024
1 parent 07b86b4 commit f3b6c70
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/internal/native/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ export function _createString(instance: number, defaultValueOrUndefined?: string
return ImageMagick._api.UTF8ToString(instance);
}

/** @internal */
export function _createStringAndRelinquish(api: ImageMagickApi, instance: number): string | null {
const result = _createString(instance);

api._MagickMemory_Relinquish(instance);

return result;
}

/** @internal */
export function _withNativeString<TReturnType>(api: ImageMagickApi, str: string, func: (instance: number) => TReturnType): TReturnType {
const length = api.lengthBytesUTF8(str) + 1;
Expand Down
23 changes: 22 additions & 1 deletion src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ import { StringInfo } from './internal/string-info';
import { TemporaryDefines } from './helpers/temporary-defines';
import { VirtualPixelMethod } from './enums/virtual-pixel-method';
import { WarningEvent } from './events/warning-event';
import { _createString, _withString } from './internal/native/string';
import { _createString, _createStringAndRelinquish, _withString } from './internal/native/string';
import { _getEdges } from './enums/gravity';
import { _withByteArray, _withDoubleArray } from './internal/native/array';

Expand Down Expand Up @@ -1106,6 +1106,12 @@ export interface IMagickImage extends IDisposable {
*/
flop(): void;

/**
* Formats the specified expression (more info can be found here: https://imagemagick.org/script/escape.php).
* @param expression The expression.
*/
formatExpression(expression: string): string | null;

/**
* Gamma correct image.
* @param gamma The image gamma.
Expand Down Expand Up @@ -2782,6 +2788,21 @@ export class MagickImage extends NativeInstance implements IMagickImage {
});
}

/**
* Formats the specified expression (more info can be found here: https://imagemagick.org/script/escape.php).
* @param expression The expression.
*/
formatExpression(expression: string): string | null {
return this.useExceptionPointer(exception => {
return this._settings._use(settings => {
return _withString(expression, expressionPtr => {
const instance = ImageMagick._api._MagickImage_FormatExpression(this._instance, settings._instance, expressionPtr, exception);
return _createStringAndRelinquish(ImageMagick._api, instance);
});
});
});
}

gammaCorrect(gamma: number): void;
gammaCorrect(gamma: number, channels: Channels): void;
gammaCorrect(gamma: number, channelsOrUndefined?: Channels): void {
Expand Down
27 changes: 27 additions & 0 deletions tests/magick-image/format-expression.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { ColorSpace } from '@src/enums/color-space';
import { TestFiles } from '@test/test-files';

describe('MagickImage#formatExpression', () => {
it('should format the specified expression', () => {
TestFiles.Images.Color.purple.use(image => {
let result = image.formatExpression("%[pixel:u]");

expect(image.colorSpace).toBe(ColorSpace.sRGB);

expect(result).toBe('srgb(128,0,128)')

const rgbProfile = TestFiles.Profiles.Color.SRGB.load();
const cmykProfile = TestFiles.Profiles.Color.USWebCoatedSWOP.load();
image.transformColorSpace(rgbProfile, cmykProfile);

result = image.formatExpression("%[pixel:u]");

expect(result).toBe('cmyk(161,255,47,15)')
});
});
});

0 comments on commit f3b6c70

Please sign in to comment.