Skip to content

Commit

Permalink
Added optimizeTransparency to MagickImageCollection.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Aug 10, 2024
1 parent dcf8443 commit 3d7515e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/magick-image-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,13 @@ export interface IMagickImageCollection extends Array<IMagickImage>, IDisposable
* optimizePlus is exactly as optimize, but may also add or even remove extra frames in the
* animation, if it improves the total number of pixels in the resulting GIF animation.
*/
optimizePlus(): void;

/**
* Compares each image the GIF disposed forms of the previous image in the sequence. Any
* pixel that does not change the displayed result is replaced with transparency.
*/
optimizeTransparency(): void;

/**
* Read all image frames.
Expand Down Expand Up @@ -484,6 +491,16 @@ export class MagickImageCollection extends Array<MagickImage> implements IMagick
});
}

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

this.attachImages(instance => {
Exception.usePointer(exception => {
ImageMagick._api._MagickImageCollection_OptimizeTransparency(instance, exception);
});
});
}

read(fileName: string, settings?: MagickReadSettings): void;
read(array: ByteArray, settings?: MagickReadSettings): void;
read(fileNameOrArray: string | ByteArray, settings?: MagickReadSettings): void {
Expand Down
48 changes: 48 additions & 0 deletions tests/magick-image-collection/optimize-transparency.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
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#optimizePlus', () => {
it('should throw exception when collection is empty', () => {
TestImages.emptyCollection.use((images) => {
expect(() => {
images.optimizeTransparency();
}).toThrowError('operation requires at least one image');
});
});

it('should make part of the images transparent', () => {
TestImages.emptyCollection.use((images) => {

images.push(MagickImage.create(MagickColors.Red, 1, 2));
images.push(MagickImage.create(MagickColors.Red, 1, 2));

const second = MagickImage.create(MagickColors.Red, 1, 1);
second.extent(1, 2, MagickColors.Green);
images.push(second);

images.optimizeTransparency();

expect(images.length).toBe(3);
expect(images[0].width).toBe(1);
expect(images[0].height).toBe(2);
expect(images[0]).toHavePixelWithColor(0, 0, '#ff0000ff');
expect(images[0]).toHavePixelWithColor(0, 1, '#ff0000ff');

expect(images[1].width).toBe(1);
expect(images[1].height).toBe(2);
expect(images[1]).toHavePixelWithColor(0, 0, '#ff000000');
expect(images[1]).toHavePixelWithColor(0, 1, '#ff000000');

expect(images[2].width).toBe(1);
expect(images[2].height).toBe(2);
expect(images[2]).toHavePixelWithColor(0, 0, '#ff000000');
expect(images[2]).toHavePixelWithColor(0, 1, '#008000ff');
});
});
});

0 comments on commit 3d7515e

Please sign in to comment.