Skip to content

Commit

Permalink
Moved setting of the artifacts to settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 5, 2024
1 parent 2870407 commit 3a8fb1b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2201,14 +2201,7 @@ export class MagickImage extends NativeInstance implements IMagickImage {

const compareResult = TemporaryDefines.use(this, temporaryDefines => {
if (hasCompareSettings) {
if (metricOrSettings.highlightColor !== undefined)
temporaryDefines.setArtifact('compare:highlight-color', metricOrSettings.highlightColor);

if (metricOrSettings.lowlightColor !== undefined)
temporaryDefines.setArtifact('compare:lowlight-color', metricOrSettings.lowlightColor);

if (metricOrSettings.masklightColor !== undefined)
temporaryDefines.setArtifact('compare:masklight-color', metricOrSettings.masklightColor);
metricOrSettings._setArtifacts(temporaryDefines);
}

return DoublePointer.use((pointer) => {
Expand Down Expand Up @@ -2489,12 +2482,7 @@ export class MagickImage extends NativeInstance implements IMagickImage {
} else {
method = methodOrSettings.method;
bestFit = methodOrSettings.bestFit ? 1 : 0;

if (methodOrSettings.scale !== undefined)
temporaryDefines.setArtifact('distort:scale', methodOrSettings.scale.toString());

if (methodOrSettings.viewport !== undefined)
temporaryDefines.setArtifact('distort:viewport', methodOrSettings.viewport.toString());
methodOrSettings._setArtifacts(temporaryDefines);
}

const distortArgs = params ?? [];
Expand Down
13 changes: 13 additions & 0 deletions src/settings/compare-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { ErrorMetric } from '../enums/error-metric';
import { IMagickColor } from '../magick-color';
import { TemporaryDefines } from '../helpers/temporary-defines';

/**
* Class that contains setting for the compare operation.
Expand Down Expand Up @@ -34,4 +35,16 @@ export class CompareSettings {
* Gets or sets the color of pixels that are inside the read mask.
*/
masklightColor?: IMagickColor;

/** @internal */
_setArtifacts(temporaryDefines: TemporaryDefines): void {
if (this.highlightColor !== undefined)
temporaryDefines.setArtifact('compare:highlight-color', this.highlightColor);

if (this.lowlightColor !== undefined)
temporaryDefines.setArtifact('compare:lowlight-color', this.lowlightColor);

if (this.masklightColor !== undefined)
temporaryDefines.setArtifact('compare:masklight-color', this.masklightColor);
}
}
10 changes: 10 additions & 0 deletions src/settings/distort-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { DistortMethod } from '../enums/distort-method';
import { TemporaryDefines } from '../helpers/temporary-defines';
import { IMagickGeometry } from '../types/magick-geometry';

/**
Expand Down Expand Up @@ -36,4 +37,13 @@ export class DistortSettings {
* resulting image, rather than use the original images canvas, or a calculated 'bestfit' canvas.
*/
viewport?: IMagickGeometry;

/** @internal */
_setArtifacts(temporaryDefines: TemporaryDefines): void {
if (this.scale !== undefined)
temporaryDefines.setArtifact('distort:scale', this.scale.toString());

if (this.viewport !== undefined)
temporaryDefines.setArtifact('distort:viewport', this.viewport.toString());
}
}
33 changes: 33 additions & 0 deletions tests/settings/compare-settings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { ErrorMetric } from '@src/enums/error-metric';
import { TemporaryDefines } from '@src/helpers/temporary-defines';
import { MagickColors } from '@src/magick-colors';
import { CompareSettings } from '@src/settings/compare-settings';
import { TestImages } from '@test/test-images';

describe('CompareSettings', () => {
const settings = new CompareSettings(ErrorMetric.NormalizedCrossCorrelation);
settings.highlightColor = MagickColors.Honeydew;
settings.lowlightColor = MagickColors.LemonChiffon;
settings.masklightColor = MagickColors.Moccasin;

describe('#_setArtifacts', () => {
it('should add all defined artifacts to the provided image', () => {
TestImages.Builtin.logo.use((image) => {
TemporaryDefines.use(image, (temporaryDefines) => {
settings._setArtifacts(temporaryDefines);

expect(settings.metric).toBe(ErrorMetric.NormalizedCrossCorrelation);
expect(image.artifactNames.length).toBe(3);
expect(image.getArtifact('compare:highlight-color')).toBe('#f0fff0ff');
expect(image.getArtifact('compare:lowlight-color')).toBe('#fffacdff');
expect(image.getArtifact('compare:masklight-color')).toBe('#ffe4b5ff');
});
});
});
});
});
31 changes: 31 additions & 0 deletions tests/settings/distort-settings.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { DistortMethod } from '@src/enums/distort-method';
import { TemporaryDefines } from '@src/helpers/temporary-defines';
import { DistortSettings } from '@src/settings/distort-settings';
import { MagickGeometry } from '@src/types/magick-geometry';
import { TestImages } from '@test/test-images';

describe('DistortSettings', () => {
const settings = new DistortSettings(DistortMethod.Perspective);
settings.scale = 42;
settings.viewport = new MagickGeometry(1, 2, 3, 4);

describe('#_setArtifacts', () => {
it('should add all defined artifacts to the provided image', () => {
TestImages.Builtin.logo.use((image) => {
TemporaryDefines.use(image, (temporaryDefines) => {
settings._setArtifacts(temporaryDefines);

expect(settings.method).toBe(DistortMethod.Perspective);
expect(image.artifactNames.length).toBe(2);
expect(image.getArtifact('distort:scale')).toBe('42');
expect(image.getArtifact('distort:viewport')).toBe('3x4+1+2');
});
});
});
});
});

0 comments on commit 3a8fb1b

Please sign in to comment.