From 316136dd57446cbdcf4ce0191cdaab8da5d70275 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 4 Aug 2024 19:09:18 +0200 Subject: [PATCH] Refactored the attachImages method. --- src/magick-image-collection.ts | 70 ++++++++++++++-------------------- 1 file changed, 29 insertions(+), 41 deletions(-) diff --git a/src/magick-image-collection.ts b/src/magick-image-collection.ts index a4cf862..e4968d2 100644 --- a/src/magick-image-collection.ts +++ b/src/magick-image-collection.ts @@ -240,18 +240,12 @@ export class MagickImageCollection extends Array implements IMagick coalesce(): void { this.throwIfEmpty(); - let result = 0; - - try { - this.attachImages(); - - result = Exception.use(exception => { - const result = ImageMagick._api._MagickImageCollection_Coalesce(this[0]._instance, exception.ptr); + const result = this.attachImages((instance) => { + return Exception.use(exception => { + const result = ImageMagick._api._MagickImageCollection_Coalesce(instance, exception.ptr); return this.checkResult(result, exception); }); - } finally { - this.detachImages(); - } + }); const settings = this.getSettings()._clone(); this.dispose(); @@ -283,12 +277,10 @@ export class MagickImageCollection extends Array implements IMagick montage(settings: MontageSettings, func: (image: IMagickImage) => TReturnType | Promise): TReturnType | Promise { this.throwIfEmpty(); - try { - this.attachImages(); - + return this.attachImages((instance) => { const result = settings._use(settingsPtr => { return Exception.use(exception => { - const images = ImageMagick._api._MagickImageCollection_Montage(this[0]._instance, settingsPtr._instance, exception.ptr); + const images = ImageMagick._api._MagickImageCollection_Montage(instance, settingsPtr._instance, exception.ptr); return this.checkResult(images, exception); }); }); @@ -302,9 +294,7 @@ export class MagickImageCollection extends Array implements IMagick } return collection.merge(func); - } finally { - this.detachImages(); - } + }); } mosaic(func: (image: IMagickImage) => TReturnType): TReturnType; @@ -368,13 +358,10 @@ export class MagickImageCollection extends Array implements IMagick Exception.use(exception => { IntPointer.use(pointer => { settings._use(nativeSettings => { - try { - this.attachImages(); - data = ImageMagick._api._MagickImage_WriteBlob(image._instance, nativeSettings._instance, pointer.ptr, exception.ptr); + this.attachImages((instance) => { + data = ImageMagick._api._MagickImage_WriteBlob(instance, nativeSettings._instance, pointer.ptr, exception.ptr); length = pointer.value; - } finally { - this.detachImages(); - } + }); }); }); }); @@ -414,9 +401,19 @@ export class MagickImageCollection extends Array implements IMagick } } - private attachImages() { - for (let i = 0; i < this.length - 1; i++) - ImageMagick._api._MagickImage_SetNext(this[i]._instance, this[i + 1]._instance); + private attachImages(func: (instance: number) => TReturnType): TReturnType { + try + { + for (let i = 0; i < this.length - 1; i++) + ImageMagick._api._MagickImage_SetNext(this[i]._instance, this[i + 1]._instance); + + return func(this[0]._instance); + } + finally + { + for (let i = 0; i < this.length - 1; i++) + ImageMagick._api._MagickImage_SetNext(this[i]._instance, 0); + } } private static createObject(): MagickImageCollection { @@ -426,19 +423,15 @@ export class MagickImageCollection extends Array implements IMagick private createImage(createImages: (instance: number, exception: Exception) => number, func: (image: IMagickImage) => TReturnType | Promise): TReturnType | Promise { this.throwIfEmpty(); - try { - this.attachImages(); - - const result = Exception.use(exception => { - const images = createImages(this[0]._instance, exception); + const result = this.attachImages((instance) => { + return Exception.use(exception => { + const images = createImages(instance, exception); return this.checkResult(images, exception); }); + }); - const image = MagickImage._createFromImage(result, this.getSettings()); - return image._use(func); - } finally { - this.detachImages(); - } + const image = MagickImage._createFromImage(result, this.getSettings()); + return image._use(func); } private static createSettings(settings?: MagickReadSettings): MagickSettings { @@ -448,11 +441,6 @@ export class MagickImageCollection extends Array implements IMagick return new MagickReadSettings(settings); } - private detachImages() { - for (let i = 0; i < this.length - 1; i++) - ImageMagick._api._MagickImage_SetNext(this[i]._instance, 0); - } - private getSettings(): MagickSettings { return this[0]._getSettings()._clone(); }