diff --git a/src/magick-image-collection.ts b/src/magick-image-collection.ts index 127c11df..b7366b35 100644 --- a/src/magick-image-collection.ts +++ b/src/magick-image-collection.ts @@ -242,6 +242,13 @@ export interface IMagickImageCollection extends Array, 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. @@ -484,6 +491,16 @@ export class MagickImageCollection extends Array 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 { diff --git a/tests/magick-image-collection/optimize-transparency.spec.ts b/tests/magick-image-collection/optimize-transparency.spec.ts new file mode 100644 index 00000000..5d41b015 --- /dev/null +++ b/tests/magick-image-collection/optimize-transparency.spec.ts @@ -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'); + }); + }); +});