Skip to content

Commit

Permalink
Added transformColorSpace to MagickImage (#190).
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Nov 28, 2024
1 parent ae12403 commit 81e3f54
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,24 @@ export interface IMagickImage extends IDisposable {
*/
strip(): void;

/**
* Transforms the image from the colorspace of the source profile to the target profile. This
* requires the image to have a color profile. Nothing will happen if the image has no color profile.
* @param target The target color profile.
* @returns A value indicating whether the transformation was successful.
*/
transformColorSpace(target: IColorProfile): boolean;

/**
* Transforms the image from the colorspace of the source profile to the target profile. The
* source profile will only be used if the image does not contain a color profile. Nothing
* will happen if the source profile has a different colorspace then that of the image.
* @param source The source color profile.
* @param target The target color profile.
* @returns A value indicating whether the transformation was successful.
*/
transformColorSpace(source: IColorProfile, target: IColorProfile): boolean;

/**
* Threshold image.
* @param percentage The threshold percentage.
Expand Down Expand Up @@ -3241,6 +3259,34 @@ export class MagickImage extends NativeInstance implements IMagickImage {
});
}

transformColorSpace(target: IColorProfile): boolean;
transformColorSpace(source: IColorProfile, target: IColorProfile): boolean;
transformColorSpace(sourceOrTarget: IColorProfile, targetOrUndefined?: IColorProfile): boolean {
const source = sourceOrTarget;
let target: IColorProfile | undefined = undefined;
if (targetOrUndefined !== undefined)
target = targetOrUndefined;

const hasColorProfile = this.hasProfile('icc') || this.hasProfile('icm');
if (target === undefined) {
if (!hasColorProfile)
return false;

target = source;
}
else {
if (source.colorSpace !== this.colorSpace)
return false;

if (!hasColorProfile)
this.setProfile(source);
}

this.setProfile(target);

return true;
}

threshold(percentage: Percentage): void
threshold(percentage: Percentage, channels: Channels): void
threshold(percentage: Percentage, channelsOrUndefined?: Channels): void {
Expand Down
42 changes: 42 additions & 0 deletions tests/magick-image/transform-color-space.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

import { TestFiles } from '@test/test-files';

describe('MagickImage#transformColorSpace', () => {
it('should return false when the image has no color profile', () => {
TestFiles.Images.Builtin.logo.use((image) => {
const target = TestFiles.Profiles.Color.sRGB.load();
const result = image.transformColorSpace(target);
expect(result).toBe(false);
});
});

it('should use the color profile of the image', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use((image) => {
const target = TestFiles.Profiles.Color.USWebCoatedSWOP.load();
const result = image.transformColorSpace(target);
expect(result).toBe(true);
});
});

it('should return false when the source profile color space is different', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use((image) => {
const source = TestFiles.Profiles.Color.USWebCoatedSWOP.load();
const target = TestFiles.Profiles.Color.sRGB.load();
const result = image.transformColorSpace(source, target);
expect(result).toBe(false);
});
});

it('should return true when the the colorspace could be transformed', () => {
TestFiles.Images.fujiFilmFinePixS1ProJpg.use((image) => {
const source = TestFiles.Profiles.Color.sRGB.load();
const target = TestFiles.Profiles.Color.USWebCoatedSWOP.load();
const result = image.transformColorSpace(source, target);
expect(result).toBe(true);
});
});
});

0 comments on commit 81e3f54

Please sign in to comment.