Skip to content

Commit

Permalink
Added deconstruct to the MagickImageCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 6, 2024
1 parent 4cb8959 commit b85a636
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/magick-image-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ export interface IMagickImageCollection extends Array<IMagickImage>, IDisposable
*/
complex<TReturnType>(settings: ComplexSettings, func: (image: IMagickImage) => Promise<TReturnType>): Promise<TReturnType>;

/**
* Break down an image sequence into constituent parts. This is useful for creating GIF or
* MNG animation sequences.
*/
deconstruct(): void;

/**
* Evaluate image pixels into a single image. All the images in the collection must be the
Expand Down Expand Up @@ -323,6 +328,21 @@ export class MagickImageCollection extends Array<MagickImage> implements IMagick
});
}

deconstruct(): void {
this.throwIfEmpty();

const result = this.attachImages((instance) => {
return Exception.use(exception => {
const result = ImageMagick._api._MagickImageCollection_Deconstruct(instance, exception.ptr);
return this.checkResult(result, exception);
});
});

const settings = this.getSettings()._clone();
this.dispose();
this.addImages(result, settings);
}

evaluate<TReturnType>(evaluateOperator: EvaluateOperator, func: (image: IMagickImage) => TReturnType): TReturnType;
evaluate<TReturnType>(evaluateOperator: EvaluateOperator, func: (image: IMagickImage) => Promise<TReturnType>): Promise<TReturnType>;
evaluate<TReturnType>(evaluateOperator: EvaluateOperator, func: (image: IMagickImage) => TReturnType | Promise<TReturnType>): TReturnType | Promise<TReturnType> {
Expand Down
40 changes: 40 additions & 0 deletions tests/magick-image-collection/deconstruct.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { MagickColors } from '@src/magick-colors';
import { MagickGeometry } from '@src/types/magick-geometry';
import { MagickImage } from '@src/magick-image';
import { TestImages } from '@test/test-images';

describe('MagickImageCollection#deconstruct', () => {
it('should throw exception when collection is empty', () => {
TestImages.emptyCollection.use((images) => {
expect(() => {
images.deconstruct();
}).toThrowError('operation requires at least one image');
});
});

it('should break the images down into constituent parts.', () => {
TestImages.emptyCollection.use((images) => {
images.push(MagickImage.create(MagickColors.Green, 10, 10));

TestImages.emptyCollection.use((frames) => {
frames.push(MagickImage.create(MagickColors.Yellow, 10, 5));
frames.push(MagickImage.create(MagickColors.Green, 10, 5));

frames.appendHorizontally(image => {
images.push(image);

images.deconstruct();

expect(images[1].width).toBe(10);
expect(images[1].height).toBe(5);
expect(images[1].page).toStrictEqual(new MagickGeometry(0, 0, 10, 5));
});
});
});
});
});

0 comments on commit b85a636

Please sign in to comment.