Skip to content

Commit f227582

Browse files
committed
Added support for setting the ColorTransformMode and default to Quantum instead of HighRes.
1 parent f3b6c70 commit f227582

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

src/enums/color-transform-mode.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright Dirk Lemstra https://github.com/dlemstra/magick-wasm.
3+
Licensed under the Apache License, Version 2.0.
4+
*/
5+
6+
/**
7+
* Specifies color transform modes.
8+
*/
9+
export enum ColorTransformMode {
10+
/**
11+
* High resolution (double).
12+
*/
13+
HighRes,
14+
15+
/**
16+
* Quantum.
17+
*/
18+
Quantum,
19+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export * from './enums/auto-threshold-method';
3939
export * from './enums/channels';
4040
export * from './enums/class-type';
4141
export * from './enums/color-space';
42+
export * from './enums/color-transform-mode';
4243
export * from './enums/color-type';
4344
export * from './enums/complex-operator';
4445
export * from './enums/composite-operator';

src/magick-image.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { ChromaticityInfo } from './types/chromaticity-info';
1212
import { ClassType } from './enums/class-type';
1313
import { ColorProfile, IColorProfile } from './profiles/color/color-profile';
1414
import { ColorSpace } from './enums/color-space';
15+
import { ColorTransformMode } from './enums/color-transform-mode';
1516
import { ColorType } from './enums/color-type';
1617
import { CompareResult } from './types/compare-result';
1718
import { CompareSettings } from './settings/compare-settings';
@@ -1705,6 +1706,15 @@ export interface IMagickImage extends IDisposable {
17051706
*/
17061707
transformColorSpace(target: IColorProfile): boolean;
17071708

1709+
/**
1710+
* Transforms the image from the colorspace of the source profile to the target profile. This
1711+
* requires the image to have a color profile. Nothing will happen if the image has no color profile.
1712+
* @param target The target color profile.
1713+
* @param mode The color transform node.
1714+
* @returns A value indicating whether the transformation was successful.
1715+
*/
1716+
transformColorSpace(target: IColorProfile, mode: ColorTransformMode): boolean;
1717+
17081718
/**
17091719
* Transforms the image from the colorspace of the source profile to the target profile. The
17101720
* source profile will only be used if the image does not contain a color profile. Nothing
@@ -1715,6 +1725,17 @@ export interface IMagickImage extends IDisposable {
17151725
*/
17161726
transformColorSpace(source: IColorProfile, target: IColorProfile): boolean;
17171727

1728+
/**
1729+
* Transforms the image from the colorspace of the source profile to the target profile. The
1730+
* source profile will only be used if the image does not contain a color profile. Nothing
1731+
* will happen if the source profile has a different colorspace then that of the image.
1732+
* @param source The source color profile.
1733+
* @param target The target color profile.
1734+
* @param mode The color transform node.
1735+
* @returns A value indicating whether the transformation was successful.
1736+
*/
1737+
transformColorSpace(source: IColorProfile, target: IColorProfile, mode: ColorTransformMode): boolean;
1738+
17181739
/**
17191740
* Threshold image.
17201741
* @param percentage The threshold percentage.
@@ -3281,12 +3302,21 @@ export class MagickImage extends NativeInstance implements IMagickImage {
32813302
}
32823303

32833304
transformColorSpace(target: IColorProfile): boolean;
3305+
transformColorSpace(target: IColorProfile, mode: ColorTransformMode): boolean;
32843306
transformColorSpace(source: IColorProfile, target: IColorProfile): boolean;
3285-
transformColorSpace(sourceOrTarget: IColorProfile, targetOrUndefined?: IColorProfile): boolean {
3307+
transformColorSpace(source: IColorProfile, target: IColorProfile, mode: ColorTransformMode): boolean;
3308+
transformColorSpace(sourceOrTarget: IColorProfile, targetModeOrUndefined?: IColorProfile | ColorTransformMode, modeOrUndefined?: ColorTransformMode): boolean {
32863309
const source = sourceOrTarget;
32873310
let target: IColorProfile | undefined;
3288-
if (targetOrUndefined !== undefined)
3289-
target = targetOrUndefined;
3311+
let mode = ColorTransformMode.Quantum;
3312+
if (targetModeOrUndefined !== undefined) {
3313+
if (typeof targetModeOrUndefined === 'number')
3314+
mode = targetModeOrUndefined;
3315+
else
3316+
target = targetModeOrUndefined;
3317+
}
3318+
if (modeOrUndefined !== undefined)
3319+
mode = modeOrUndefined;
32903320

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

3306-
this.setProfile(target);
3336+
if (mode === ColorTransformMode.Quantum) {
3337+
TemporaryDefines.use(this, temporaryDefines => {
3338+
temporaryDefines.setArtifact('profile:highres-transform', false);
3339+
this.setProfile(target);
3340+
});
3341+
}
3342+
else {
3343+
this.setProfile(target);
3344+
}
33073345

33083346
return true;
33093347
}
639 KB
Binary file not shown.

tests/magick-image/transform-color-space.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

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

910
describe('MagickImage#transformColorSpace', () => {
@@ -52,4 +53,26 @@ describe('MagickImage#transformColorSpace', () => {
5253
expect(image.colorSpace).toBe(ColorSpace.CMYK);
5354
});
5455
});
56+
57+
it('should use quantum color transform mode by default', () => {
58+
const source = TestFiles.Profiles.Color.SRGB.load();
59+
const target = TestFiles.Profiles.Color.CoatedFOGRA39.load();
60+
61+
TestFiles.Images.Color.white.use((image) => {
62+
image.transformColorSpace(source, target);
63+
const result = image.formatExpression('%[pixel:u]');
64+
expect(result).toBe('cmyk(0,0,0,0)');
65+
});
66+
});
67+
68+
it('should support high res transform mode', () => {
69+
const source = TestFiles.Profiles.Color.SRGB.load();
70+
const target = TestFiles.Profiles.Color.CoatedFOGRA39.load();
71+
72+
TestFiles.Images.Color.white.use((image) => {
73+
image.transformColorSpace(source, target, ColorTransformMode.HighRes);
74+
const result = image.formatExpression('%[pixel:u]');
75+
expect(result).toBe('cmyk(0,1,0,0)');
76+
});
77+
});
5578
});

tests/test-files.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,15 @@ export class TestFiles {
162162
red: new TestImageFromColor(MagickColors.Red, 1, 1),
163163
black: new TestImageFromColor(MagickColors.Black, 1, 1),
164164
purple: new TestImageFromColor(MagickColors.Purple, 1, 1),
165-
white: new TestImageFromColor(MagickColors.Black, 2, 2),
165+
white: new TestImageFromColor(MagickColors.White, 2, 2),
166166
}
167167
}
168168

169169
static readonly Profiles = {
170170
Color: {
171171
SRGB: new TestColorProfile('tests/files/color-profiles/SRGB.icm'),
172172
USWebCoatedSWOP: new TestColorProfile('tests/files/color-profiles/USWebCoatedSWOP.icc'),
173+
CoatedFOGRA39: new TestColorProfile('tests/files/color-profiles/CoatedFOGRA39.icc'),
173174
}
174175
}
175176
}

0 commit comments

Comments
 (0)