Skip to content

Commit 2b84b89

Browse files
committed
Added trimBounds to MagickImageCollection.
1 parent 357a519 commit 2b84b89

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/magick-image-collection.ts

+11
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,13 @@ export interface IMagickImageCollection extends Array<IMagickImage>, IDisposable
346346
*/
347347
smushVertical<TReturnType>(offset: number, func: AsyncImageCallback<TReturnType>): Promise<TReturnType>;
348348

349+
/**
350+
* Determine the overall bounds of all the image layers just as in {@link MagickImageCollection#merge},
351+
* then adjust the the canvas and offsets to be relative to those bounds,
352+
* without overlaying the images.
353+
*/
354+
trimBounds(): void;
355+
349356
/**
350357
* Write all image frames to a byte array.
351358
* @param func The function to execute with the byte array.
@@ -645,6 +652,10 @@ export class MagickImageCollection extends Array<MagickImage> implements IMagick
645652
return this.smush(offset, true, func);
646653
}
647654

655+
trimBounds(): void {
656+
this.mergeImages(LayerMethod.Trimbounds, () => { /* special case where the returned image is null */ });
657+
}
658+
648659
write<TReturnType>(func: (data: Uint8Array) => TReturnType): TReturnType;
649660
write<TReturnType>(format: MagickFormat, func: (data: Uint8Array) => TReturnType): TReturnType;
650661
write<TReturnType>(func: (data: Uint8Array) => Promise<TReturnType>): Promise<TReturnType>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
3+
Licensed under the Apache License, Version 2.0.
4+
*/
5+
6+
import { MagickColors } from '@src/magick-colors';
7+
import { MagickImage } from '@src/magick-image';
8+
import { TestImages } from '@test/test-images';
9+
10+
describe('MagickImageCollection#trimBounds', () => {
11+
it('should throw exception when collection is empty', () => {
12+
TestImages.emptyCollection.use((images) => {
13+
expect(() => {
14+
images.trimBounds();
15+
}).toThrowError('operation requires at least one image');
16+
});
17+
});
18+
19+
it('should adjust the canvas', () => {
20+
TestImages.emptyCollection.use(images => {
21+
22+
images.push(MagickImage.create(MagickColors.Red, 6, 7));
23+
images.push(MagickImage.create(MagickColors.Red, 8, 5));
24+
25+
images.trimBounds();
26+
27+
expect(images.length).toBe(2);
28+
expect(images[0].page.toString()).toBe('8x7+0+0');
29+
expect(images[1].page.toString()).toBe('8x7+0+0');
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)