Skip to content

Commit

Permalink
Use temporary defines to set the artifacts.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 5, 2024
1 parent 06afc3e commit c027d30
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 71 deletions.
25 changes: 13 additions & 12 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2338,23 +2338,24 @@ export class MagickImage extends NativeInstance implements IMagickImage {
connectedComponents(connectivityOrSettings: Connectivity | ConnectedComponentsSettings): ConnectedComponent[] {
const settings = typeof connectivityOrSettings === 'number' ? new ConnectedComponentsSettings(connectivityOrSettings) : connectivityOrSettings;

settings._setArtifacts(this);
const connectedComponents = TemporaryDefines.use(this, temporaryDefines => {
settings._setArtifacts(temporaryDefines);

const connectedComponents = this.useException((exception) => {
return IntPointer.use((objects) => {
try {
const instance = ImageMagick._api._MagickImage_ConnectedComponents(this._instance, settings.connectivity, objects.ptr, exception.ptr);
this._setInstance(instance, exception)
return ConnectedComponent._create(objects.value, this.colormapSize);
} finally {
if (objects.value !== 0) {
ImageMagick._api._ConnectedComponent_DisposeList(objects.value);
return this.useException((exception) => {
return IntPointer.use((objects) => {
try {
const instance = ImageMagick._api._MagickImage_ConnectedComponents(this._instance, settings.connectivity, objects.ptr, exception.ptr);
this._setInstance(instance, exception)
return ConnectedComponent._create(objects.value, this.colormapSize);
} finally {
if (objects.value !== 0) {
ImageMagick._api._ConnectedComponent_DisposeList(objects.value);
}
}
}
});
});
});

settings._removeArtifacts(this);
return connectedComponents;
}

Expand Down
86 changes: 27 additions & 59 deletions src/settings/connected-components-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Licensed under the Apache License, Version 2.0.
*/

import { IMagickImage } from '../magick-image';
import { TemporaryDefines } from '../helpers/temporary-defines';
import { Threshold } from '../types/threshold';

/**
Expand Down Expand Up @@ -80,64 +80,32 @@ export class ConnectedComponentsSettings {
}

/** @internal */
_removeArtifacts(image: IMagickImage): void {
if (this.angleThreshold !== undefined) {
image.removeArtifact('connected-components:angle-threshold');
}
if (this.areaThreshold !== undefined) {
image.removeArtifact('connected-components:area-threshold');
}
if (this.circularityThreshold !== undefined) {
image.removeArtifact('connected-components:circularity-threshold');
}
if (this.diameterThreshold !== undefined) {
image.removeArtifact('connected-components:diameter-threshold');
}
if (this.eccentricityThreshold !== undefined) {
image.removeArtifact('connected-components:eccentricity-threshold');
}
if (this.majorAxisThreshold !== undefined) {
image.removeArtifact('connected-components:major-axis-threshold');
}
if (this.meanColor !== undefined) {
image.removeArtifact('connected-components:mean-color');
}
if (this.minorAxisThreshold !== undefined) {
image.removeArtifact('connected-components:minor-axis-threshold');
}
if (this.perimeterThreshold !== undefined) {
image.removeArtifact('connected-components:perimeter-threshold');
}
}
_setArtifacts(temporaryDefines: TemporaryDefines): void {
if (this.angleThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:angle-threshold', this.angleThreshold.toString());

/** @internal */
_setArtifacts(image: IMagickImage): void {
if (this.angleThreshold !== undefined) {
image.setArtifact('connected-components:angle-threshold', this.angleThreshold.toString());
}
if (this.areaThreshold !== undefined) {
image.setArtifact('connected-components:area-threshold', this.areaThreshold.toString());
}
if (this.circularityThreshold !== undefined) {
image.setArtifact('connected-components:circularity-threshold', this.circularityThreshold.toString());
}
if (this.diameterThreshold !== undefined) {
image.setArtifact('connected-components:diameter-threshold', this.diameterThreshold.toString());
}
if (this.eccentricityThreshold !== undefined) {
image.setArtifact('connected-components:eccentricity-threshold', this.eccentricityThreshold.toString());
}
if (this.majorAxisThreshold !== undefined) {
image.setArtifact('connected-components:major-axis-threshold', this.majorAxisThreshold.toString());
}
if (this.meanColor !== undefined) {
image.setArtifact('connected-components:mean-color', this.meanColor.toString());
}
if (this.minorAxisThreshold !== undefined) {
image.setArtifact('connected-components:minor-axis-threshold', this.minorAxisThreshold.toString());
}
if (this.perimeterThreshold !== undefined) {
image.setArtifact('connected-components:perimeter-threshold', this.perimeterThreshold.toString());
}
if (this.areaThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:area-threshold', this.areaThreshold.toString());

if (this.circularityThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:circularity-threshold', this.circularityThreshold.toString());

if (this.diameterThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:diameter-threshold', this.diameterThreshold.toString());

if (this.eccentricityThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:eccentricity-threshold', this.eccentricityThreshold.toString());

if (this.majorAxisThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:major-axis-threshold', this.majorAxisThreshold.toString());

if (this.meanColor !== undefined)
temporaryDefines.setArtifact('connected-components:mean-color', this.meanColor.toString());

if (this.minorAxisThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:minor-axis-threshold', this.minorAxisThreshold.toString());

if (this.perimeterThreshold !== undefined)
temporaryDefines.setArtifact('connected-components:perimeter-threshold', this.perimeterThreshold.toString());
}
}

0 comments on commit c027d30

Please sign in to comment.