diff --git a/src/settings/magick-read-settings.ts b/src/settings/magick-read-settings.ts index 68720a56..5f66b2e3 100644 --- a/src/settings/magick-read-settings.ts +++ b/src/settings/magick-read-settings.ts @@ -5,6 +5,7 @@ import { Disposable } from '../internal/disposable'; import { ImageMagick } from '../image-magick'; +import { MagickGeometry } from '../types/magick-geometry'; import { MagickSettings } from './magick-settings'; import { NativeMagickSettings } from './native-magick-settings'; import { _withString } from '../internal/native/string'; @@ -20,6 +21,11 @@ export class MagickReadSettings extends MagickSettings { Object.assign(this, partialSettings); } + /** + * Gets or sets the specified area to extract from the image. + */ + extractArea?: MagickGeometry; + /** * Gets or sets the height. */ @@ -41,6 +47,12 @@ export class MagickReadSettings extends MagickSettings { }); } + if (this.extractArea !== undefined) { + _withString(this.extractArea.toString(), extractAreaPtr => { + ImageMagick._api._MagickSettings_Extract_Set(settings._instance, extractAreaPtr); + }); + } + return Disposable._disposeAfterExecution(settings, func); } diff --git a/tests/settings/magick-read-settings/extract-area.spec.ts b/tests/settings/magick-read-settings/extract-area.spec.ts new file mode 100644 index 00000000..35426c9a --- /dev/null +++ b/tests/settings/magick-read-settings/extract-area.spec.ts @@ -0,0 +1,35 @@ +/* + Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm. + Licensed under the Apache License, Version 2.0. +*/ + +import { ImageMagick } from '@src/image-magick'; +import { MagickGeometry } from '@src/types/magick-geometry'; +import { MagickReadSettings } from '@src/settings/magick-read-settings'; +import { MagickColor } from '@src/magick-color'; + +describe('MagickReadSettings#extractArea', () => { + it('should only read the specified area of the image', () => { + const settings = new MagickReadSettings(); + settings.extractArea = new MagickGeometry(350, 260, 100, 100); + + ImageMagick.read('logo:', settings, (image) => { + expect(image.width).toBe(100); + expect(image.height).toBe(100); + + expect(image).toHavePixelWithColor(0, 0, new MagickColor('#223e92ff')); + }); + }); + + it('should not outside the image', () => { + const settings = new MagickReadSettings(); + settings.extractArea = new MagickGeometry(600, 400, 100, 100); + + ImageMagick.read('logo:', settings, (image) => { + expect(image.width).toBe(40); + expect(image.height).toBe(80); + + expect(image).toHavePixelWithColor(0, 0, new MagickColor('#fff')); + }); + }); +});