Skip to content

Commit

Permalink
Added addProfile to MagickImage (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenscordeirobr authored May 23, 2023
1 parent 9285e9c commit 2375598
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export interface IMagickImage extends IDisposable {
virtualPixelMethod: VirtualPixelMethod;
width: number;

addProfile(name: string, data: Uint8Array): void
alpha(value: AlphaOption): void;
autoOrient(): void;
autoThreshold(method: AutoThresholdMethod): void;
Expand Down Expand Up @@ -552,6 +553,24 @@ export class MagickImage extends NativeInstance implements IMagickImage {

get width(): number { return ImageMagick._api._MagickImage_Width_Get(this._instance); }

addProfile(name: string, data:Uint8Array): void {
Exception.use(exception => {
_withString(name, namePtr => {
const length = data.byteLength;
let dataPtr = 0;
try {
dataPtr = ImageMagick._api._malloc(length);
ImageMagick._api.HEAPU8.set(data, dataPtr);
ImageMagick._api._MagickImage_AddProfile(this._instance, namePtr, dataPtr, length, exception.ptr);
}
finally {
if (dataPtr !== 0)
ImageMagick._api._free(dataPtr);
}
});
});
}

alpha(value: AlphaOption): void {
Exception.usePointer(exception => {
ImageMagick._api._MagickImage_SetAlpha(this._instance, value, exception);
Expand Down
46 changes: 46 additions & 0 deletions tests/magick-image/add-profile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { IMagickImage, MagickImage } from '../../src/magick-image';
import { TestImages } from '../test-images';

let image: IMagickImage;

beforeEach(() => {
image = MagickImage.create();
});

afterEach(() => {
image.dispose();
});

describe('MagickImage#addProfile', () => {
it('should add the profile', () => {
TestImages.fujiFilmFinePixS1ProJpg.use(sourceImage => {
const profile = sourceImage.getProfile('icc');
expect(profile).not.toBeNull();

if (profile !== null) {
expect(profile.name).toEqual('icc');
const profileData = profile.getData();
expect(profileData).not.toBeNull();

if (profileData !== null) {
expect(profileData.length).toBe(3144);
image.addProfile('icc', profileData);
const profile = image.getProfile('icc');
expect(profile).not.toBeNull();

if (profile !== null) {
expect(profile.name).toEqual('icc');
const data = profile.getData();
expect(data).not.toBeNull();

if (data !== null)
expect(data.length).toBe(3144);
}
}
}
});
});
});
5 changes: 1 addition & 4 deletions tests/magick-image/get-profile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ describe('MagickImage#getProfile', () => {
it('should return null when image does not contain profile', () => {
ImageMagick.read(MagickColors.Black, 1, 1, (image) => {
const profile = image.getProfile('foo');

expect(profile).toBeNull();
});
});
Expand All @@ -18,16 +17,14 @@ describe('MagickImage#getProfile', () => {
TestImages.fujiFilmFinePixS1ProJpg.use(image => {

const profile = image.getProfile('icc');

expect(profile).not.toBeNull();
if (profile !== null) {
expect(profile.name).toEqual('icc');

const data = profile.getData();
expect(data).not.toBeNull();
if (data !== null) {
if (data !== null)
expect(data.length).toBe(3144);
}
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion tests/magick-image/remove-profile.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
// Licensed under the Apache License, Version 2.0.

import { TestImages } from '../test-images';
Expand Down

0 comments on commit 2375598

Please sign in to comment.