Skip to content

Commit

Permalink
Added support for setting the ColorTransformMode and default to Quant…
Browse files Browse the repository at this point in the history
…um instead of HighRes.
  • Loading branch information
dlemstra committed Dec 6, 2024
1 parent f3b6c70 commit f227582
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
19 changes: 19 additions & 0 deletions src/enums/color-transform-mode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
Licensed under the Apache License, Version 2.0.
*/

/**
* Specifies color transform modes.
*/
export enum ColorTransformMode {
/**
* High resolution (double).
*/
HighRes,

/**
* Quantum.
*/
Quantum,
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export * from './enums/auto-threshold-method';
export * from './enums/channels';
export * from './enums/class-type';
export * from './enums/color-space';
export * from './enums/color-transform-mode';
export * from './enums/color-type';
export * from './enums/complex-operator';
export * from './enums/composite-operator';
Expand Down
46 changes: 42 additions & 4 deletions src/magick-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ChromaticityInfo } from './types/chromaticity-info';
import { ClassType } from './enums/class-type';
import { ColorProfile, IColorProfile } from './profiles/color/color-profile';
import { ColorSpace } from './enums/color-space';
import { ColorTransformMode } from './enums/color-transform-mode';
import { ColorType } from './enums/color-type';
import { CompareResult } from './types/compare-result';
import { CompareSettings } from './settings/compare-settings';
Expand Down Expand Up @@ -1705,6 +1706,15 @@ export interface IMagickImage extends IDisposable {
*/
transformColorSpace(target: IColorProfile): boolean;

/**
* 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.
* @param mode The color transform node.
* @returns A value indicating whether the transformation was successful.
*/
transformColorSpace(target: IColorProfile, mode: ColorTransformMode): 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
Expand All @@ -1715,6 +1725,17 @@ export interface IMagickImage extends IDisposable {
*/
transformColorSpace(source: IColorProfile, 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.
* @param mode The color transform node.
* @returns A value indicating whether the transformation was successful.
*/
transformColorSpace(source: IColorProfile, target: IColorProfile, mode: ColorTransformMode): boolean;

/**
* Threshold image.
* @param percentage The threshold percentage.
Expand Down Expand Up @@ -3281,12 +3302,21 @@ export class MagickImage extends NativeInstance implements IMagickImage {
}

transformColorSpace(target: IColorProfile): boolean;
transformColorSpace(target: IColorProfile, mode: ColorTransformMode): boolean;
transformColorSpace(source: IColorProfile, target: IColorProfile): boolean;
transformColorSpace(sourceOrTarget: IColorProfile, targetOrUndefined?: IColorProfile): boolean {
transformColorSpace(source: IColorProfile, target: IColorProfile, mode: ColorTransformMode): boolean;
transformColorSpace(sourceOrTarget: IColorProfile, targetModeOrUndefined?: IColorProfile | ColorTransformMode, modeOrUndefined?: ColorTransformMode): boolean {
const source = sourceOrTarget;
let target: IColorProfile | undefined;
if (targetOrUndefined !== undefined)
target = targetOrUndefined;
let mode = ColorTransformMode.Quantum;
if (targetModeOrUndefined !== undefined) {
if (typeof targetModeOrUndefined === 'number')
mode = targetModeOrUndefined;
else
target = targetModeOrUndefined;
}
if (modeOrUndefined !== undefined)
mode = modeOrUndefined;

const hasColorProfile = this.hasProfile('icc') || this.hasProfile('icm');
if (target === undefined) {
Expand All @@ -3303,7 +3333,15 @@ export class MagickImage extends NativeInstance implements IMagickImage {
this.setProfile(source);
}

this.setProfile(target);
if (mode === ColorTransformMode.Quantum) {
TemporaryDefines.use(this, temporaryDefines => {
temporaryDefines.setArtifact('profile:highres-transform', false);
this.setProfile(target);
});
}
else {
this.setProfile(target);
}

return true;
}
Expand Down
Binary file added tests/files/color-profiles/CoatedFOGRA39.icc
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/magick-image/transform-color-space.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { ColorSpace } from '@src/enums/color-space';
import { ColorTransformMode } from '@src/enums/color-transform-mode';
import { TestFiles } from '@test/test-files';

describe('MagickImage#transformColorSpace', () => {
Expand Down Expand Up @@ -52,4 +53,26 @@ describe('MagickImage#transformColorSpace', () => {
expect(image.colorSpace).toBe(ColorSpace.CMYK);
});
});

it('should use quantum color transform mode by default', () => {
const source = TestFiles.Profiles.Color.SRGB.load();
const target = TestFiles.Profiles.Color.CoatedFOGRA39.load();

TestFiles.Images.Color.white.use((image) => {
image.transformColorSpace(source, target);
const result = image.formatExpression('%[pixel:u]');
expect(result).toBe('cmyk(0,0,0,0)');
});
});

it('should support high res transform mode', () => {
const source = TestFiles.Profiles.Color.SRGB.load();
const target = TestFiles.Profiles.Color.CoatedFOGRA39.load();

TestFiles.Images.Color.white.use((image) => {
image.transformColorSpace(source, target, ColorTransformMode.HighRes);
const result = image.formatExpression('%[pixel:u]');
expect(result).toBe('cmyk(0,1,0,0)');
});
});
});
3 changes: 2 additions & 1 deletion tests/test-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,15 @@ export class TestFiles {
red: new TestImageFromColor(MagickColors.Red, 1, 1),
black: new TestImageFromColor(MagickColors.Black, 1, 1),
purple: new TestImageFromColor(MagickColors.Purple, 1, 1),
white: new TestImageFromColor(MagickColors.Black, 2, 2),
white: new TestImageFromColor(MagickColors.White, 2, 2),
}
}

static readonly Profiles = {
Color: {
SRGB: new TestColorProfile('tests/files/color-profiles/SRGB.icm'),
USWebCoatedSWOP: new TestColorProfile('tests/files/color-profiles/USWebCoatedSWOP.icc'),
CoatedFOGRA39: new TestColorProfile('tests/files/color-profiles/CoatedFOGRA39.icc'),
}
}
}

0 comments on commit f227582

Please sign in to comment.