Skip to content

Commit

Permalink
Added extractArea to the MagickReadSettings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Sep 18, 2024
1 parent 23fa6ac commit 71f619b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/settings/magick-read-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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.
*/
Expand All @@ -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);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/settings/magick-read-settings/extract-area.spec.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});
});

0 comments on commit 71f619b

Please sign in to comment.