Skip to content

Commit

Permalink
Added trimBounds to MagickImageCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 12, 2024
1 parent 357a519 commit 2b84b89
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/magick-image-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ export interface IMagickImageCollection extends Array<IMagickImage>, IDisposable
*/
smushVertical<TReturnType>(offset: number, func: AsyncImageCallback<TReturnType>): Promise<TReturnType>;

/**
* Determine the overall bounds of all the image layers just as in {@link MagickImageCollection#merge},
* then adjust the the canvas and offsets to be relative to those bounds,
* without overlaying the images.
*/
trimBounds(): void;

/**
* Write all image frames to a byte array.
* @param func The function to execute with the byte array.
Expand Down Expand Up @@ -645,6 +652,10 @@ export class MagickImageCollection extends Array<MagickImage> implements IMagick
return this.smush(offset, true, func);
}

trimBounds(): void {
this.mergeImages(LayerMethod.Trimbounds, () => { /* special case where the returned image is null */ });
}

write<TReturnType>(func: (data: Uint8Array) => TReturnType): TReturnType;
write<TReturnType>(format: MagickFormat, func: (data: Uint8Array) => TReturnType): TReturnType;
write<TReturnType>(func: (data: Uint8Array) => Promise<TReturnType>): Promise<TReturnType>;
Expand Down
32 changes: 32 additions & 0 deletions tests/magick-image-collection/trim-bounds.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

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

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

it('should adjust the canvas', () => {
TestImages.emptyCollection.use(images => {

images.push(MagickImage.create(MagickColors.Red, 6, 7));
images.push(MagickImage.create(MagickColors.Red, 8, 5));

images.trimBounds();

expect(images.length).toBe(2);
expect(images[0].page.toString()).toBe('8x7+0+0');
expect(images[1].page.toString()).toBe('8x7+0+0');
});
});
});

0 comments on commit 2b84b89

Please sign in to comment.