3 // Copyright (C) 1998-2007 Marti Maria
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the Software
10 // is furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
17 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 void LCMSEXPORT
cmsXYZ2xyY(LPcmsCIExyY Dest
, const cmsCIEXYZ
* Source
)
32 ISum
= 1./(Source
-> X
+ Source
-> Y
+ Source
-> Z
);
34 Dest
-> x
= (Source
-> X
) * ISum
;
35 Dest
-> y
= (Source
-> Y
) * ISum
;
36 Dest
-> Y
= Source
-> Y
;
40 void LCMSEXPORT
cmsxyY2XYZ(LPcmsCIEXYZ Dest
, const cmsCIExyY
* Source
)
43 Dest
-> X
= (Source
-> x
/ Source
-> y
) * Source
-> Y
;
44 Dest
-> Y
= Source
-> Y
;
45 Dest
-> Z
= ((1 - Source
-> x
- Source
-> y
) / Source
-> y
) * Source
-> Y
;
49 // Obtains WhitePoint from Temperature
51 LCMSBOOL LCMSEXPORT
cmsWhitePointFromTemp(int TempK
, LPcmsCIExyY WhitePoint
)
58 // No optimization provided.
64 // For correlated color temperature (T) between 4000K and 7000K:
66 if (T
>= 4000. && T
<= 7000.)
68 x
= -4.6070*(1E9
/T3
) + 2.9678*(1E6
/T2
) + 0.09911*(1E3
/T
) + 0.244063;
71 // or for correlated color temperature (T) between 7000K and 25000K:
73 if (T
> 7000.0 && T
<= 25000.0)
75 x
= -2.0064*(1E9
/T3
) + 1.9018*(1E6
/T2
) + 0.24748*(1E3
/T
) + 0.237040;
78 cmsSignalError(LCMS_ERRC_ABORTED
, "cmsWhitePointFromTemp: invalid temp");
84 y
= -3.000*(x
*x
) + 2.870*x
- 0.275;
86 // wave factors (not used, but here for futures extensions)
88 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
89 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
93 // Fill WhitePoint struct
97 WhitePoint
-> Y
= 1.0;
102 // Build a White point, primary chromas transfer matrix from RGB to CIE XYZ
103 // This is just an approximation, I am not handling all the non-linear
104 // aspects of the RGB to XYZ process, and assumming that the gamma correction
105 // has transitive property in the tranformation chain.
109 // - First I build the absolute conversion matrix using
110 // primaries in XYZ. This matrix is next inverted
111 // - Then I eval the source white point across this matrix
112 // obtaining the coeficients of the transformation
113 // - Then, I apply these coeficients to the original matrix
116 LCMSBOOL LCMSEXPORT
cmsBuildRGB2XYZtransferMatrix(LPMAT3 r
, LPcmsCIExyY WhitePt
,
117 LPcmsCIExyYTRIPLE Primrs
)
119 VEC3 WhitePoint
, Coef
;
120 MAT3 Result
, Primaries
;
131 xr
= Primrs
-> Red
.x
;
132 yr
= Primrs
-> Red
.y
;
133 xg
= Primrs
-> Green
.x
;
134 yg
= Primrs
-> Green
.y
;
135 xb
= Primrs
-> Blue
.x
;
136 yb
= Primrs
-> Blue
.y
;
139 // Build Primaries matrix
140 VEC3init(&Primaries
.v
[0], xr
, xg
, xb
);
141 VEC3init(&Primaries
.v
[1], yr
, yg
, yb
);
142 VEC3init(&Primaries
.v
[2], (1-xr
-yr
), (1-xg
-yg
), (1-xb
-yb
));
145 // Result = Primaries ^ (-1) inverse matrix
146 if (MAT3inverse(&Primaries
, &Result
) < 0)
150 VEC3init(&WhitePoint
, xn
/yn
, 1.0, (1.0-xn
-yn
)/yn
);
152 // Across inverse primaries ...
153 MAT3eval(&Coef
, &Result
, &WhitePoint
);
155 // Give us the Coefs, then I build transformation matrix
156 VEC3init(&r
-> v
[0], Coef
.n
[VX
]*xr
, Coef
.n
[VY
]*xg
, Coef
.n
[VZ
]*xb
);
157 VEC3init(&r
-> v
[1], Coef
.n
[VX
]*yr
, Coef
.n
[VY
]*yg
, Coef
.n
[VZ
]*yb
);
158 VEC3init(&r
-> v
[2], Coef
.n
[VX
]*(1.0-xr
-yr
), Coef
.n
[VY
]*(1.0-xg
-yg
), Coef
.n
[VZ
]*(1.0-xb
-yb
));
166 // Compute chromatic adaptation matrix using Chad as cone matrix
169 void ComputeChromaticAdaptation(LPMAT3 Conversion
,
170 LPcmsCIEXYZ SourceWhitePoint
,
171 LPcmsCIEXYZ DestWhitePoint
,
177 VEC3 ConeSourceXYZ
, ConeSourceRGB
;
178 VEC3 ConeDestXYZ
, ConeDestRGB
;
183 MAT3inverse(&Tmp
, &Chad_Inv
);
185 VEC3init(&ConeSourceXYZ
, SourceWhitePoint
-> X
,
186 SourceWhitePoint
-> Y
,
187 SourceWhitePoint
-> Z
);
189 VEC3init(&ConeDestXYZ
, DestWhitePoint
-> X
,
191 DestWhitePoint
-> Z
);
193 MAT3eval(&ConeSourceRGB
, Chad
, &ConeSourceXYZ
);
194 MAT3eval(&ConeDestRGB
, Chad
, &ConeDestXYZ
);
198 VEC3init(&Cone
.v
[0], ConeDestRGB
.n
[0]/ConeSourceRGB
.n
[0], 0.0, 0.0);
199 VEC3init(&Cone
.v
[1], 0.0, ConeDestRGB
.n
[1]/ConeSourceRGB
.n
[1], 0.0);
200 VEC3init(&Cone
.v
[2], 0.0, 0.0, ConeDestRGB
.n
[2]/ConeSourceRGB
.n
[2]);
204 MAT3per(&Tmp
, &Cone
, Chad
);
205 MAT3per(Conversion
, &Chad_Inv
, &Tmp
);
210 // Returns the final chrmatic adaptation from illuminant FromIll to Illuminant ToIll
211 // The cone matrix can be specified in ConeMatrix. If NULL, Bradford is assumed
213 LCMSBOOL
cmsAdaptationMatrix(LPMAT3 r
, LPMAT3 ConeMatrix
, LPcmsCIEXYZ FromIll
, LPcmsCIEXYZ ToIll
)
215 MAT3 LamRigg
= {{ // Bradford matrix
216 {{ 0.8951, 0.2664, -0.1614 }},
217 {{ -0.7502, 1.7135, 0.0367 }},
218 {{ 0.0389, -0.0685, 1.0296 }}
222 if (ConeMatrix
== NULL
)
223 ConeMatrix
= &LamRigg
;
225 ComputeChromaticAdaptation(r
, FromIll
, ToIll
, ConeMatrix
);
230 // Same as anterior, but assuming D50 destination. White point is given in xyY
232 LCMSBOOL
cmsAdaptMatrixToD50(LPMAT3 r
, LPcmsCIExyY SourceWhitePt
)
238 cmsxyY2XYZ(&Dn
, SourceWhitePt
);
240 cmsAdaptationMatrix(&Bradford
, NULL
, &Dn
, cmsD50_XYZ());
243 MAT3per(r
, &Bradford
, &Tmp
);
249 // Same as anterior, but assuming D50 source. White point is given in xyY
251 LCMSBOOL
cmsAdaptMatrixFromD50(LPMAT3 r
, LPcmsCIExyY DestWhitePt
)
257 cmsxyY2XYZ(&Dn
, DestWhitePt
);
259 cmsAdaptationMatrix(&Bradford
, NULL
, cmsD50_XYZ(), &Dn
);
262 MAT3per(r
, &Bradford
, &Tmp
);
268 // Adapts a color to a given illuminant. Original color is expected to have
269 // a SourceWhitePt white point.
271 LCMSBOOL LCMSEXPORT
cmsAdaptToIlluminant(LPcmsCIEXYZ Result
,
272 LPcmsCIEXYZ SourceWhitePt
,
273 LPcmsCIEXYZ Illuminant
,
279 // BradfordLamRiggChromaticAdaptation(&Bradford, SourceWhitePt, Illuminant);
281 cmsAdaptationMatrix(&Bradford
, NULL
, SourceWhitePt
, Illuminant
);
283 VEC3init(&In
, Value
-> X
, Value
-> Y
, Value
-> Z
);
284 MAT3eval(&Out
, &Bradford
, &In
);
286 Result
-> X
= Out
.n
[0];
287 Result
-> Y
= Out
.n
[1];
288 Result
-> Z
= Out
.n
[2];
297 double mirek
; // temp (in microreciprocal kelvin)
298 double ut
; // u coord of intersection w/ blackbody locus
299 double vt
; // v coord of intersection w/ blackbody locus
300 double tt
; // slope of ISOTEMPERATURE. line
302 } ISOTEMPERATURE
,FAR
* LPISOTEMPERATURE
;
304 static ISOTEMPERATURE isotempdata
[] = {
305 // {Mirek, Ut, Vt, Tt }
306 {0, 0.18006, 0.26352, -0.24341},
307 {10, 0.18066, 0.26589, -0.25479},
308 {20, 0.18133, 0.26846, -0.26876},
309 {30, 0.18208, 0.27119, -0.28539},
310 {40, 0.18293, 0.27407, -0.30470},
311 {50, 0.18388, 0.27709, -0.32675},
312 {60, 0.18494, 0.28021, -0.35156},
313 {70, 0.18611, 0.28342, -0.37915},
314 {80, 0.18740, 0.28668, -0.40955},
315 {90, 0.18880, 0.28997, -0.44278},
316 {100, 0.19032, 0.29326, -0.47888},
317 {125, 0.19462, 0.30141, -0.58204},
318 {150, 0.19962, 0.30921, -0.70471},
319 {175, 0.20525, 0.31647, -0.84901},
320 {200, 0.21142, 0.32312, -1.0182 },
321 {225, 0.21807, 0.32909, -1.2168 },
322 {250, 0.22511, 0.33439, -1.4512 },
323 {275, 0.23247, 0.33904, -1.7298 },
324 {300, 0.24010, 0.34308, -2.0637 },
325 {325, 0.24702, 0.34655, -2.4681 },
326 {350, 0.25591, 0.34951, -2.9641 },
327 {375, 0.26400, 0.35200, -3.5814 },
328 {400, 0.27218, 0.35407, -4.3633 },
329 {425, 0.28039, 0.35577, -5.3762 },
330 {450, 0.28863, 0.35714, -6.7262 },
331 {475, 0.29685, 0.35823, -8.5955 },
332 {500, 0.30505, 0.35907, -11.324 },
333 {525, 0.31320, 0.35968, -15.628 },
334 {550, 0.32129, 0.36011, -23.325 },
335 {575, 0.32931, 0.36038, -40.770 },
336 {600, 0.33724, 0.36051, -116.45 }
339 #define NISO sizeof(isotempdata)/sizeof(ISOTEMPERATURE)
342 // Robertson's method
345 double Robertson(LPcmsCIExyY v
)
349 double uj
,vj
,tj
,di
,dj
,mi
,mj
;
350 double Tc
= -1, xs
, ys
;
356 // convert (x,y) to CIE 1960 (u,v)
358 us
= (2*xs
) / (-xs
+ 6*ys
+ 1.5);
359 vs
= (3*ys
) / (-xs
+ 6*ys
+ 1.5);
362 for (j
=0; j
< NISO
; j
++) {
364 uj
= isotempdata
[j
].ut
;
365 vj
= isotempdata
[j
].vt
;
366 tj
= isotempdata
[j
].tt
;
367 mj
= isotempdata
[j
].mirek
;
369 dj
= ((vs
- vj
) - tj
* (us
- uj
)) / sqrt(1 + tj
*tj
);
371 if ((j
!=0) && (di
/dj
< 0.0)) {
372 Tc
= 1000000.0 / (mi
+ (di
/ (di
- dj
)) * (mj
- mi
));
381 if (j
== NISO
) return -1;
388 LCMSBOOL
InRange(LPcmsCIExyY a
, LPcmsCIExyY b
, double tolerance
)
390 double dist_x
, dist_y
;
392 dist_x
= fabs(a
->x
- b
->x
);
393 dist_y
= fabs(a
->y
- b
->y
);
395 return (tolerance
>= dist_x
* dist_x
+ dist_y
* dist_y
);
404 } WHITEPOINTS
,FAR
*LPWHITEPOINTS
;
407 int FromD40toD150(LPWHITEPOINTS pts
)
412 for (i
=40; i
< 150; i
++)
414 sprintf(pts
[n
].Name
, "D%d", i
);
415 cmsWhitePointFromTemp((int) (i
*100.0), &pts
[n
].Val
);
423 // To be removed in future versions
424 void _cmsIdentifyWhitePoint(char *Buffer
, LPcmsCIEXYZ WhitePt
)
429 WHITEPOINTS SomeIlluminants
[140] = {
431 {"CIE illuminant A", {0.4476, 0.4074, 1.0}},
432 {"CIE illuminant C", {0.3101, 0.3162, 1.0}},
433 {"D65 (daylight)", {0.3127, 0.3291, 1.0}},
436 n
= FromD40toD150(&SomeIlluminants
[3]) + 3;
438 cmsXYZ2xyY(&Val
, WhitePt
);
441 for (i
=0; i
< n
; i
++)
444 if (InRange(&Val
, &SomeIlluminants
[i
].Val
, 0.000005))
446 strcpy(Buffer
, "WhitePoint : ");
447 strcat(Buffer
, SomeIlluminants
[i
].Name
);
455 sprintf(Buffer
, "White point near %dK", (int) T
);
458 sprintf(Buffer
, "Unknown white point (X:%1.2g, Y:%1.2g, Z:%1.2g)",
459 WhitePt
-> X
, WhitePt
-> Y
, WhitePt
-> Z
);
466 // Use darker colorant to obtain black point
469 int BlackPointAsDarkerColorant(cmsHPROFILE hInput
,
471 LPcmsCIEXYZ BlackPoint
,
476 icColorSpaceSignature Space
;
481 cmsCIEXYZ BlackXYZ
, MediaWhite
;
483 // If the profile does not support input direction, assume Black point 0
484 if (!cmsIsIntentSupported(hInput
, Intent
, LCMS_USED_AS_INPUT
)) {
486 BlackPoint
-> X
= BlackPoint
->Y
= BlackPoint
-> Z
= 0.0;
491 // Try to get black by using black colorant
492 Space
= cmsGetColorSpace(hInput
);
494 if (!_cmsEndPointsBySpace(Space
, &White
, &Black
, &nChannels
)) {
496 BlackPoint
-> X
= BlackPoint
->Y
= BlackPoint
-> Z
= 0.0;
500 dwFormat
= CHANNELS_SH(nChannels
)|BYTES_SH(2);
502 hLab
= cmsCreateLabProfile(NULL
);
504 xform
= cmsCreateTransform(hInput
, dwFormat
,
505 hLab
, TYPE_Lab_DBL
, Intent
, cmsFLAGS_NOTPRECALC
);
508 cmsDoTransform(xform
, Black
, &Lab
, 1);
510 // Force it to be neutral, clip to max. L* of 50
513 if (Lab
.L
> 50) Lab
.L
= 50;
515 cmsCloseProfile(hLab
);
516 cmsDeleteTransform(xform
);
518 cmsLab2XYZ(NULL
, &BlackXYZ
, &Lab
);
520 if (Intent
== INTENT_ABSOLUTE_COLORIMETRIC
) {
522 *BlackPoint
= BlackXYZ
;
526 if (!(dwFlags
& LCMS_BPFLAGS_D50_ADAPTED
)) {
528 cmsTakeMediaWhitePoint(&MediaWhite
, hInput
);
529 cmsAdaptToIlluminant(BlackPoint
, cmsD50_XYZ(), &MediaWhite
, &BlackXYZ
);
532 *BlackPoint
= BlackXYZ
;
539 // Get a black point of output CMYK profile, discounting any ink-limiting embedded
540 // in the profile. For doing that, use perceptual intent in input direction:
541 // Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
544 int BlackPointUsingPerceptualBlack(LPcmsCIEXYZ BlackPoint
,
545 cmsHPROFILE hProfile
,
548 cmsHTRANSFORM hPercLab2CMYK
, hRelColCMYK2Lab
;
550 cmsCIELab LabIn
, LabOut
;
551 WORD CMYK
[MAXCHANNELS
];
552 cmsCIEXYZ BlackXYZ
, MediaWhite
;
555 if (!cmsIsIntentSupported(hProfile
, INTENT_PERCEPTUAL
, LCMS_USED_AS_INPUT
)) {
557 BlackPoint
-> X
= BlackPoint
->Y
= BlackPoint
-> Z
= 0.0;
561 hLab
= cmsCreateLabProfile(NULL
);
563 hPercLab2CMYK
= cmsCreateTransform(hLab
, TYPE_Lab_DBL
,
564 hProfile
, TYPE_CMYK_16
,
565 INTENT_PERCEPTUAL
, cmsFLAGS_NOTPRECALC
);
567 hRelColCMYK2Lab
= cmsCreateTransform(hProfile
, TYPE_CMYK_16
,
569 INTENT_RELATIVE_COLORIMETRIC
, cmsFLAGS_NOTPRECALC
);
571 LabIn
.L
= LabIn
.a
= LabIn
.b
= 0;
573 cmsDoTransform(hPercLab2CMYK
, &LabIn
, CMYK
, 1);
574 cmsDoTransform(hRelColCMYK2Lab
, CMYK
, &LabOut
, 1);
576 if (LabOut
.L
> 50) LabOut
.L
= 50;
577 LabOut
.a
= LabOut
.b
= 0;
579 cmsDeleteTransform(hPercLab2CMYK
);
580 cmsDeleteTransform(hRelColCMYK2Lab
);
581 cmsCloseProfile(hLab
);
583 cmsLab2XYZ(NULL
, &BlackXYZ
, &LabOut
);
585 if (!(dwFlags
& LCMS_BPFLAGS_D50_ADAPTED
)){
586 cmsTakeMediaWhitePoint(&MediaWhite
, hProfile
);
587 cmsAdaptToIlluminant(BlackPoint
, cmsD50_XYZ(), &MediaWhite
, &BlackXYZ
);
590 *BlackPoint
= BlackXYZ
;
597 // Get Perceptual black of v4 profiles.
599 int GetV4PerceptualBlack(LPcmsCIEXYZ BlackPoint
, cmsHPROFILE hProfile
, DWORD dwFlags
)
601 if (dwFlags
& LCMS_BPFLAGS_D50_ADAPTED
) {
603 BlackPoint
->X
= PERCEPTUAL_BLACK_X
;
604 BlackPoint
->Y
= PERCEPTUAL_BLACK_Y
;
605 BlackPoint
->Z
= PERCEPTUAL_BLACK_Z
;
609 cmsCIEXYZ D50BlackPoint
, MediaWhite
;
611 cmsTakeMediaWhitePoint(&MediaWhite
, hProfile
);
612 D50BlackPoint
.X
= PERCEPTUAL_BLACK_X
;
613 D50BlackPoint
.Y
= PERCEPTUAL_BLACK_Y
;
614 D50BlackPoint
.Z
= PERCEPTUAL_BLACK_Z
;
616 // Obtain the absolute XYZ. Adapt perceptual black back from D50 to whatever media white
617 cmsAdaptToIlluminant(BlackPoint
, cmsD50_XYZ(), &MediaWhite
, &D50BlackPoint
);
625 // This function shouldn't exist at all -- there is such quantity of broken
626 // profiles on black point tag, that we must somehow fix chromaticity to
627 // avoid huge tint when doing Black point compensation. This function does
628 // just that. There is a special flag for using black point tag, but turned
629 // off by default because it is bogus on most profiles. The detection algorithm
630 // involves to turn BP to neutral and to use only L component.
632 int cmsDetectBlackPoint(LPcmsCIEXYZ BlackPoint
, cmsHPROFILE hProfile
, int Intent
, DWORD dwFlags
)
635 // v4 + perceptual & saturation intents does have its own black point, and it is
636 // well specified enough to use it.
638 if ((cmsGetProfileICCversion(hProfile
) >= 0x4000000) &&
639 (Intent
== INTENT_PERCEPTUAL
|| Intent
== INTENT_SATURATION
)) {
641 // Matrix shaper share MRC & perceptual intents
642 if (_cmsIsMatrixShaper(hProfile
))
643 return BlackPointAsDarkerColorant(hProfile
, INTENT_RELATIVE_COLORIMETRIC
, BlackPoint
, cmsFLAGS_NOTPRECALC
);
645 // CLUT based - Get perceptual black point (fixed value)
646 return GetV4PerceptualBlack(BlackPoint
, hProfile
, dwFlags
);
650 #ifdef HONOR_BLACK_POINT_TAG
652 // v2, v4 rel/abs colorimetric
653 if (cmsIsTag(hProfile
, icSigMediaBlackPointTag
) &&
654 Intent
== INTENT_RELATIVE_COLORIMETRIC
) {
656 cmsCIEXYZ BlackXYZ
, UntrustedBlackPoint
, TrustedBlackPoint
, MediaWhite
;
659 // If black point is specified, then use it,
661 cmsTakeMediaBlackPoint(&BlackXYZ
, hProfile
);
662 cmsTakeMediaWhitePoint(&MediaWhite
, hProfile
);
664 // Black point is absolute XYZ, so adapt to D50 to get PCS value
665 cmsAdaptToIlluminant(&UntrustedBlackPoint
, &MediaWhite
, cmsD50_XYZ(), &BlackXYZ
);
667 // Force a=b=0 to get rid of any chroma
669 cmsXYZ2Lab(NULL
, &Lab
, &UntrustedBlackPoint
);
671 if (Lab
.L
> 50) Lab
.L
= 50; // Clip to L* <= 50
673 cmsLab2XYZ(NULL
, &TrustedBlackPoint
, &Lab
);
675 // Return BP as D50 relative or absolute XYZ (depends on flags)
676 if (!(dwFlags
& LCMS_BPFLAGS_D50_ADAPTED
))
677 cmsAdaptToIlluminant(BlackPoint
, cmsD50_XYZ(), &MediaWhite
, &TrustedBlackPoint
);
679 *BlackPoint
= TrustedBlackPoint
;
686 // That is about v2 profiles.
688 // If output profile, discount ink-limiting and that's all
689 if (Intent
== INTENT_RELATIVE_COLORIMETRIC
&&
690 (cmsGetDeviceClass(hProfile
) == icSigOutputClass
) &&
691 (cmsGetColorSpace(hProfile
) == icSigCmykData
))
692 return BlackPointUsingPerceptualBlack(BlackPoint
, hProfile
, dwFlags
);
694 // Nope, compute BP using current intent.
695 return BlackPointAsDarkerColorant(hProfile
, Intent
, BlackPoint
, dwFlags
);