b=450088 backing out (new reftest failed)
[wine-gecko.git] / modules / lcms / src / cmscnvrt.c
blob7a4b23d44bc831b375c4790a8c627bce61220874
1 //
2 // Little cms
3 // Copyright (C) 1998-2007 Marti Maria
4 //
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.
23 #include "lcms.h"
29 This module provides conversion stages for handling intents.
31 The chain of evaluation in a transform is:
33 PCS1 PCS2 PCS3 PCS4
35 |From | |From | |Conversion | |Preview | |Gamut | |Conversion | |To | |To |
36 |Input|->|Device|->|Stage 1 |->|handling|->|Checking|->|Stage 2 |->|Device|->|output |
38 -------- ------- ------------- --------- ---------- ------------- ------- ---------
40 AToB0 prew0 gamut BToA0
41 Formatting LUT Adjusting LUT LUT Adjusting LUT Formatting
42 Intent Intent 1 intent intent Intent 2 Intent
45 Some of these LUT may be missing
47 There are two intents involved here, the intent of the transform itself, and the
48 intent the proof is being done, if is the case. Since the first intent is to be
49 applied to preview, is the proofing intent. The second intent identifies the
50 transform intent. Input data of any stage is taked as relative colorimetric
51 always.
54 NOTES: V4 states than perceptual & saturation intents between mixed v2 & v4 profiles should
55 scale PCS from a black point equal to ZERO in v2 profiles to the reference media black of
56 perceptual v4 PCS. Since I found many v2 profiles to be using a perceptual intent with black
57 point not zero at all, I'm implementing that as a black point compensation from whatever
58 black from perceptal intent to the reference media black for v4 profiles.
65 int cdecl cmsChooseCnvrt(int Absolute,
66 int Phase1, LPcmsCIEXYZ BlackPointIn,
67 LPcmsCIEXYZ WhitePointIn,
68 LPcmsCIEXYZ IlluminantIn,
69 LPMAT3 ChromaticAdaptationMatrixIn,
71 int Phase2, LPcmsCIEXYZ BlackPointOut,
72 LPcmsCIEXYZ WhitePointOut,
73 LPcmsCIEXYZ IlluminantOut,
74 LPMAT3 ChromaticAdaptationMatrixOut,
76 int DoBlackPointCompensation,
77 double AdaptationState,
78 _cmsADJFN *fn1,
79 LPWMAT3 wm, LPWVEC3 wof);
82 // -------------------------------------------------------------------------
84 // D50 - Widely used
86 LCMSAPI LPcmsCIEXYZ LCMSEXPORT cmsD50_XYZ(void)
88 static cmsCIEXYZ D50XYZ = {D50X, D50Y, D50Z};
90 return &D50XYZ;
93 LCMSAPI LPcmsCIExyY LCMSEXPORT cmsD50_xyY(void)
95 static cmsCIExyY D50xyY;
96 cmsXYZ2xyY(&D50xyY, cmsD50_XYZ());
98 return &D50xyY;
102 // ---------------- From LUT to LUT --------------------------
105 // Calculate m, offset Relativ -> Absolute undoing any chromatic
106 // adaptation done by the profile.
108 #ifdef _MSC_VER
109 #pragma warning(disable : 4100 4505)
110 #endif
114 // join scalings to obtain:
115 // relative input to absolute and then to relative output
117 static
118 void Rel2RelStepAbsCoefs(double AdaptationState,
120 LPcmsCIEXYZ BlackPointIn,
121 LPcmsCIEXYZ WhitePointIn,
122 LPcmsCIEXYZ IlluminantIn,
123 LPMAT3 ChromaticAdaptationMatrixIn,
125 LPcmsCIEXYZ BlackPointOut,
126 LPcmsCIEXYZ WhitePointOut,
127 LPcmsCIEXYZ IlluminantOut,
128 LPMAT3 ChromaticAdaptationMatrixOut,
130 LPMAT3 m, LPVEC3 of)
133 VEC3 WtPtIn, WtPtInAdapted;
134 VEC3 WtPtOut, WtPtOutAdapted;
135 MAT3 Scale, m1, m2, m3;
137 VEC3init(&WtPtIn, WhitePointIn->X, WhitePointIn->Y, WhitePointIn->Z);
138 MAT3eval(&WtPtInAdapted, ChromaticAdaptationMatrixIn, &WtPtIn);
140 VEC3init(&WtPtOut, WhitePointOut->X, WhitePointOut->Y, WhitePointOut->Z);
141 MAT3eval(&WtPtOutAdapted, ChromaticAdaptationMatrixOut, &WtPtOut);
143 VEC3init(&Scale.v[0], WtPtInAdapted.n[0] / WtPtOutAdapted.n[0], 0, 0);
144 VEC3init(&Scale.v[1], 0, WtPtInAdapted.n[1] / WtPtOutAdapted.n[1], 0);
145 VEC3init(&Scale.v[2], 0, 0, WtPtInAdapted.n[2] / WtPtOutAdapted.n[2]);
148 // Adaptation state
150 if (AdaptationState == 1.0) {
152 // Observer is fully adapted. Keep chromatic adaptation
154 CopyMemory(m, &Scale, sizeof(MAT3));
157 else {
159 // Observer is not adapted, undo the chromatic adaptation
160 m1 = *ChromaticAdaptationMatrixIn;
161 MAT3inverse(&m1, &m2);
163 MAT3per(&m3, &m2, &Scale);
164 MAT3per(m, &m3, ChromaticAdaptationMatrixOut);
168 VEC3init(of, 0.0, 0.0, 0.0);
173 // The (in)famous black point compensation. Right now implemented as
174 // a linear scaling in XYZ
176 static
177 void ComputeBlackPointCompensationFactors(LPcmsCIEXYZ BlackPointIn,
178 LPcmsCIEXYZ WhitePointIn,
179 LPcmsCIEXYZ IlluminantIn,
180 LPcmsCIEXYZ BlackPointOut,
181 LPcmsCIEXYZ WhitePointOut,
182 LPcmsCIEXYZ IlluminantOut,
183 LPMAT3 m, LPVEC3 of)
187 cmsCIEXYZ RelativeBlackPointIn, RelativeBlackPointOut;
188 double ax, ay, az, bx, by, bz, tx, ty, tz;
190 // At first, convert both black points to relative.
192 cmsAdaptToIlluminant(&RelativeBlackPointIn, WhitePointIn, IlluminantIn, BlackPointIn);
193 cmsAdaptToIlluminant(&RelativeBlackPointOut, WhitePointOut, IlluminantOut, BlackPointOut);
195 // Now we need to compute a matrix plus an offset m and of such of
196 // [m]*bpin + off = bpout
197 // [m]*D50 + off = D50
199 // This is a linear scaling in the form ax+b, where
200 // a = (bpout - D50) / (bpin - D50)
201 // b = - D50* (bpout - bpin) / (bpin - D50)
204 tx = RelativeBlackPointIn.X - IlluminantIn ->X;
205 ty = RelativeBlackPointIn.Y - IlluminantIn ->Y;
206 tz = RelativeBlackPointIn.Z - IlluminantIn ->Z;
208 ax = (RelativeBlackPointOut.X - IlluminantOut ->X) / tx;
209 ay = (RelativeBlackPointOut.Y - IlluminantOut ->Y) / ty;
210 az = (RelativeBlackPointOut.Z - IlluminantOut ->Z) / tz;
212 bx = - IlluminantOut -> X * (RelativeBlackPointOut.X - RelativeBlackPointIn.X) / tx;
213 by = - IlluminantOut -> Y * (RelativeBlackPointOut.Y - RelativeBlackPointIn.Y) / ty;
214 bz = - IlluminantOut -> Z * (RelativeBlackPointOut.Z - RelativeBlackPointIn.Z) / tz;
217 MAT3identity(m);
219 m->v[VX].n[0] = ax;
220 m->v[VY].n[1] = ay;
221 m->v[VZ].n[2] = az;
223 VEC3init(of, bx, by, bz);
227 // Return TRUE if both m and of are empy -- "m" being identity and "of" being 0
229 static
230 LCMSBOOL IdentityParameters(LPWMAT3 m, LPWVEC3 of)
232 WVEC3 wv0;
234 VEC3initF(&wv0, 0, 0, 0);
236 if (!MAT3isIdentity(m, 0.00001)) return FALSE;
237 if (!VEC3equal(of, &wv0, 0.00001)) return FALSE;
239 return TRUE;
245 // ----------------------------------------- Inter PCS conversions
247 // XYZ to XYZ linear scaling. Aso used on Black point compensation
249 static
250 void XYZ2XYZ(WORD In[], WORD Out[], LPWMAT3 m, LPWVEC3 of)
253 WVEC3 a, r;
255 a.n[0] = In[0] << 1;
256 a.n[1] = In[1] << 1;
257 a.n[2] = In[2] << 1;
259 MAT3evalW(&r, m, &a);
261 Out[0] = _cmsClampWord((r.n[VX] + of->n[VX]) >> 1);
262 Out[1] = _cmsClampWord((r.n[VY] + of->n[VY]) >> 1);
263 Out[2] = _cmsClampWord((r.n[VZ] + of->n[VZ]) >> 1);
267 // XYZ to Lab, scaling first
269 static
270 void XYZ2Lab(WORD In[], WORD Out[], LPWMAT3 m, LPWVEC3 of)
272 WORD XYZ[3];
274 XYZ2XYZ(In, XYZ, m, of);
275 cmsXYZ2LabEncoded(XYZ, Out);
278 // Lab to XYZ, then scalling
280 static
281 void Lab2XYZ(WORD In[], WORD Out[], LPWMAT3 m, LPWVEC3 of)
283 WORD XYZ[3];
285 cmsLab2XYZEncoded(In, XYZ);
286 XYZ2XYZ(XYZ, Out, m, of);
289 // Lab to XYZ, scalling and then, back to Lab
291 static
292 void Lab2XYZ2Lab(WORD In[], WORD Out[], LPWMAT3 m, LPWVEC3 of)
294 WORD XYZ[3], XYZ2[3];
296 cmsLab2XYZEncoded(In, XYZ);
297 XYZ2XYZ(XYZ, XYZ2, m, of);
298 cmsXYZ2LabEncoded(XYZ2, Out);
301 // ------------------------------------------------------------------
303 // Dispatcher for XYZ Relative LUT
305 static
306 int FromXYZRelLUT(int Absolute,
307 LPcmsCIEXYZ BlackPointIn,
308 LPcmsCIEXYZ WhitePointIn,
309 LPcmsCIEXYZ IlluminantIn,
310 LPMAT3 ChromaticAdaptationMatrixIn,
312 int Phase2, LPcmsCIEXYZ BlackPointOut,
313 LPcmsCIEXYZ WhitePointOut,
314 LPcmsCIEXYZ IlluminantOut,
315 LPMAT3 ChromaticAdaptationMatrixOut,
317 int DoBlackPointCompensation,
318 double AdaptationState,
319 _cmsADJFN *fn1,
320 LPMAT3 m, LPVEC3 of)
323 switch (Phase2) {
325 // From relative XYZ to Relative XYZ.
327 case XYZRel:
329 if (Absolute)
331 // From input relative to absolute, and then
332 // back to output relative
334 Rel2RelStepAbsCoefs(AdaptationState,
335 BlackPointIn,
336 WhitePointIn,
337 IlluminantIn,
338 ChromaticAdaptationMatrixIn,
339 BlackPointOut,
340 WhitePointOut,
341 IlluminantOut,
342 ChromaticAdaptationMatrixOut,
343 m, of);
344 *fn1 = XYZ2XYZ;
347 else
349 // XYZ Relative to XYZ relative, no op required
350 *fn1 = NULL;
351 if (DoBlackPointCompensation) {
353 *fn1 = XYZ2XYZ;
354 ComputeBlackPointCompensationFactors(BlackPointIn,
355 WhitePointIn,
356 IlluminantIn,
357 BlackPointOut,
358 WhitePointOut,
359 IlluminantOut,
360 m, of);
364 break;
367 // From relative XYZ to Relative Lab
369 case LabRel:
371 // First pass XYZ to absolute, then to relative and
372 // finally to Lab. I use here D50 for output in order
373 // to prepare the "to Lab" conversion.
375 if (Absolute)
378 Rel2RelStepAbsCoefs(AdaptationState,
379 BlackPointIn,
380 WhitePointIn,
381 IlluminantIn,
382 ChromaticAdaptationMatrixIn,
383 BlackPointOut,
384 WhitePointOut,
385 IlluminantOut,
386 ChromaticAdaptationMatrixOut,
387 m, of);
389 *fn1 = XYZ2Lab;
392 else
394 // Just Convert to Lab
396 MAT3identity(m);
397 VEC3init(of, 0, 0, 0);
398 *fn1 = XYZ2Lab;
400 if (DoBlackPointCompensation) {
402 ComputeBlackPointCompensationFactors(BlackPointIn,
403 WhitePointIn,
404 IlluminantIn,
405 BlackPointOut,
406 WhitePointOut,
407 IlluminantOut,
408 m, of);
411 break;
414 default: return FALSE;
417 return TRUE;
423 // From Lab Relative type LUT
425 static
426 int FromLabRelLUT(int Absolute,
427 LPcmsCIEXYZ BlackPointIn,
428 LPcmsCIEXYZ WhitePointIn,
429 LPcmsCIEXYZ IlluminantIn,
430 LPMAT3 ChromaticAdaptationMatrixIn,
432 int Phase2, LPcmsCIEXYZ BlackPointOut,
433 LPcmsCIEXYZ WhitePointOut,
434 LPcmsCIEXYZ IlluminantOut,
435 LPMAT3 ChromaticAdaptationMatrixOut,
437 int DoBlackPointCompensation,
438 double AdaptationState,
440 _cmsADJFN *fn1,
441 LPMAT3 m, LPVEC3 of)
444 switch (Phase2) {
446 // From Lab Relative to XYZ Relative, very usual case
448 case XYZRel:
450 if (Absolute) { // Absolute intent
452 // From lab relative, to XYZ absolute, and then,
453 // back to XYZ relative
455 Rel2RelStepAbsCoefs(AdaptationState,
456 BlackPointIn,
457 WhitePointIn,
458 cmsD50_XYZ(),
459 ChromaticAdaptationMatrixIn,
460 BlackPointOut,
461 WhitePointOut,
462 IlluminantOut,
463 ChromaticAdaptationMatrixOut,
464 m, of);
466 *fn1 = Lab2XYZ;
469 else
471 // From Lab relative, to XYZ relative.
473 *fn1 = Lab2XYZ;
474 if (DoBlackPointCompensation) {
476 ComputeBlackPointCompensationFactors(BlackPointIn,
477 WhitePointIn,
478 IlluminantIn,
479 BlackPointOut,
480 WhitePointOut,
481 IlluminantOut,
482 m, of);
486 break;
490 case LabRel:
492 if (Absolute) {
494 // First pass to XYZ using the input illuminant
495 // * InIlluminant / D50, then to absolute. Then
496 // to relative, but for input
498 Rel2RelStepAbsCoefs(AdaptationState,
499 BlackPointIn,
500 WhitePointIn, IlluminantIn,
501 ChromaticAdaptationMatrixIn,
502 BlackPointOut,
503 WhitePointOut, cmsD50_XYZ(),
504 ChromaticAdaptationMatrixOut,
505 m, of);
506 *fn1 = Lab2XYZ2Lab;
508 else
509 { // Lab -> Lab relative don't need any adjust unless
510 // black point compensation
512 *fn1 = NULL;
513 if (DoBlackPointCompensation) {
515 *fn1 = Lab2XYZ2Lab;
516 ComputeBlackPointCompensationFactors(BlackPointIn,
517 WhitePointIn,
518 IlluminantIn,
519 BlackPointOut,
520 WhitePointOut,
521 IlluminantOut,
522 m, of);
527 break;
530 default: return FALSE;
533 return TRUE;
537 // This function does calculate the necessary conversion operations
538 // needed from transpassing data from a LUT to a LUT. The conversion
539 // is modeled as a pointer of function and two coefficients, a and b
540 // The function is actually called only if not null pointer is provided,
541 // and the two paramaters are passed in. There are several types of
542 // conversions, but basically they do a linear scalling and a interchange
546 // Main dispatcher
548 int cmsChooseCnvrt(int Absolute,
549 int Phase1, LPcmsCIEXYZ BlackPointIn,
550 LPcmsCIEXYZ WhitePointIn,
551 LPcmsCIEXYZ IlluminantIn,
552 LPMAT3 ChromaticAdaptationMatrixIn,
554 int Phase2, LPcmsCIEXYZ BlackPointOut,
555 LPcmsCIEXYZ WhitePointOut,
556 LPcmsCIEXYZ IlluminantOut,
557 LPMAT3 ChromaticAdaptationMatrixOut,
559 int DoBlackPointCompensation,
560 double AdaptationState,
561 _cmsADJFN *fn1,
562 LPWMAT3 wm, LPWVEC3 wof)
565 int rc;
566 MAT3 m;
567 VEC3 of;
570 MAT3identity(&m);
571 VEC3init(&of, 0, 0, 0);
573 switch (Phase1) {
575 // Input LUT is giving XYZ relative values.
577 case XYZRel: rc = FromXYZRelLUT(Absolute,
578 BlackPointIn,
579 WhitePointIn,
580 IlluminantIn,
581 ChromaticAdaptationMatrixIn,
582 Phase2,
583 BlackPointOut,
584 WhitePointOut,
585 IlluminantOut,
586 ChromaticAdaptationMatrixOut,
587 DoBlackPointCompensation,
588 AdaptationState,
589 fn1, &m, &of);
590 break;
594 // Input LUT is giving Lab relative values
596 case LabRel: rc = FromLabRelLUT(Absolute,
597 BlackPointIn,
598 WhitePointIn,
599 IlluminantIn,
600 ChromaticAdaptationMatrixIn,
601 Phase2,
602 BlackPointOut,
603 WhitePointOut,
604 IlluminantOut,
605 ChromaticAdaptationMatrixOut,
606 DoBlackPointCompensation,
607 AdaptationState,
608 fn1, &m, &of);
609 break;
614 // Unrecognized combination
616 default: cmsSignalError(LCMS_ERRC_ABORTED, "(internal) Phase error");
617 return FALSE;
621 MAT3toFix(wm, &m);
622 VEC3toFix(wof, &of);
624 // Do some optimization -- discard conversion if identity parameters.
626 if (*fn1 == XYZ2XYZ || *fn1 == Lab2XYZ2Lab) {
628 if (IdentityParameters(wm, wof))
629 *fn1 = NULL;
633 return rc;