Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / lcms2 / src / cmswtpnt.c
blobf6a7b7a66ca5e6d94dc570dfd4c36a58ee0b7e08
1 //---------------------------------------------------------------------------------
2 //
3 // Little Color Management System
4 // Copyright (c) 1998-2012 Marti Maria Saguer
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the Software
11 // is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
18 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //---------------------------------------------------------------------------------
27 #include "lcms2_internal.h"
30 // D50 - Widely used
31 const cmsCIEXYZ* CMSEXPORT cmsD50_XYZ(void)
33 static cmsCIEXYZ D50XYZ = {cmsD50X, cmsD50Y, cmsD50Z};
35 return &D50XYZ;
38 const cmsCIExyY* CMSEXPORT cmsD50_xyY(void)
40 static cmsCIExyY D50xyY;
42 cmsXYZ2xyY(&D50xyY, cmsD50_XYZ());
44 return &D50xyY;
47 // Obtains WhitePoint from Temperature
48 cmsBool CMSEXPORT cmsWhitePointFromTemp(cmsCIExyY* WhitePoint, cmsFloat64Number TempK)
50 cmsFloat64Number x, y;
51 cmsFloat64Number T, T2, T3;
52 // cmsFloat64Number M1, M2;
54 _cmsAssert(WhitePoint != NULL);
56 T = TempK;
57 T2 = T*T; // Square
58 T3 = T2*T; // Cube
60 // For correlated color temperature (T) between 4000K and 7000K:
62 if (T >= 4000. && T <= 7000.)
64 x = -4.6070*(1E9/T3) + 2.9678*(1E6/T2) + 0.09911*(1E3/T) + 0.244063;
66 else // or for correlated color temperature (T) between 7000K and 25000K:
67 if (T > 7000.0 && T <= 25000.0)
69 x = -2.0064*(1E9/T3) + 1.9018*(1E6/T2) + 0.24748*(1E3/T) + 0.237040;
71 else {
72 cmsSignalError(0, cmsERROR_RANGE, "cmsWhitePointFromTemp: invalid temp");
73 return FALSE;
76 // Obtain y(x)
78 y = -3.000*(x*x) + 2.870*x - 0.275;
80 // wave factors (not used, but here for futures extensions)
82 // M1 = (-1.3515 - 1.7703*x + 5.9114 *y)/(0.0241 + 0.2562*x - 0.7341*y);
83 // M2 = (0.0300 - 31.4424*x + 30.0717*y)/(0.0241 + 0.2562*x - 0.7341*y);
85 WhitePoint -> x = x;
86 WhitePoint -> y = y;
87 WhitePoint -> Y = 1.0;
89 return TRUE;
94 typedef struct {
96 cmsFloat64Number mirek; // temp (in microreciprocal kelvin)
97 cmsFloat64Number ut; // u coord of intersection w/ blackbody locus
98 cmsFloat64Number vt; // v coord of intersection w/ blackbody locus
99 cmsFloat64Number tt; // slope of ISOTEMPERATURE. line
101 } ISOTEMPERATURE;
103 static ISOTEMPERATURE isotempdata[] = {
104 // {Mirek, Ut, Vt, Tt }
105 {0, 0.18006, 0.26352, -0.24341},
106 {10, 0.18066, 0.26589, -0.25479},
107 {20, 0.18133, 0.26846, -0.26876},
108 {30, 0.18208, 0.27119, -0.28539},
109 {40, 0.18293, 0.27407, -0.30470},
110 {50, 0.18388, 0.27709, -0.32675},
111 {60, 0.18494, 0.28021, -0.35156},
112 {70, 0.18611, 0.28342, -0.37915},
113 {80, 0.18740, 0.28668, -0.40955},
114 {90, 0.18880, 0.28997, -0.44278},
115 {100, 0.19032, 0.29326, -0.47888},
116 {125, 0.19462, 0.30141, -0.58204},
117 {150, 0.19962, 0.30921, -0.70471},
118 {175, 0.20525, 0.31647, -0.84901},
119 {200, 0.21142, 0.32312, -1.0182 },
120 {225, 0.21807, 0.32909, -1.2168 },
121 {250, 0.22511, 0.33439, -1.4512 },
122 {275, 0.23247, 0.33904, -1.7298 },
123 {300, 0.24010, 0.34308, -2.0637 },
124 {325, 0.24702, 0.34655, -2.4681 },
125 {350, 0.25591, 0.34951, -2.9641 },
126 {375, 0.26400, 0.35200, -3.5814 },
127 {400, 0.27218, 0.35407, -4.3633 },
128 {425, 0.28039, 0.35577, -5.3762 },
129 {450, 0.28863, 0.35714, -6.7262 },
130 {475, 0.29685, 0.35823, -8.5955 },
131 {500, 0.30505, 0.35907, -11.324 },
132 {525, 0.31320, 0.35968, -15.628 },
133 {550, 0.32129, 0.36011, -23.325 },
134 {575, 0.32931, 0.36038, -40.770 },
135 {600, 0.33724, 0.36051, -116.45 }
138 #define NISO sizeof(isotempdata)/sizeof(ISOTEMPERATURE)
141 // Robertson's method
142 cmsBool CMSEXPORT cmsTempFromWhitePoint(cmsFloat64Number* TempK, const cmsCIExyY* WhitePoint)
144 cmsUInt32Number j;
145 cmsFloat64Number us,vs;
146 cmsFloat64Number uj,vj,tj,di,dj,mi,mj;
147 cmsFloat64Number xs, ys;
149 _cmsAssert(WhitePoint != NULL);
150 _cmsAssert(TempK != NULL);
152 di = mi = 0;
153 xs = WhitePoint -> x;
154 ys = WhitePoint -> y;
156 // convert (x,y) to CIE 1960 (u,WhitePoint)
158 us = (2*xs) / (-xs + 6*ys + 1.5);
159 vs = (3*ys) / (-xs + 6*ys + 1.5);
162 for (j=0; j < NISO; j++) {
164 uj = isotempdata[j].ut;
165 vj = isotempdata[j].vt;
166 tj = isotempdata[j].tt;
167 mj = isotempdata[j].mirek;
169 dj = ((vs - vj) - tj * (us - uj)) / sqrt(1.0 + tj * tj);
171 if ((j != 0) && (di/dj < 0.0)) {
173 // Found a match
174 *TempK = 1000000.0 / (mi + (di / (di - dj)) * (mj - mi));
175 return TRUE;
178 di = dj;
179 mi = mj;
182 // Not found
183 return FALSE;
187 // Compute chromatic adaptation matrix using Chad as cone matrix
189 static
190 cmsBool ComputeChromaticAdaptation(cmsMAT3* Conversion,
191 const cmsCIEXYZ* SourceWhitePoint,
192 const cmsCIEXYZ* DestWhitePoint,
193 const cmsMAT3* Chad)
197 cmsMAT3 Chad_Inv;
198 cmsVEC3 ConeSourceXYZ, ConeSourceRGB;
199 cmsVEC3 ConeDestXYZ, ConeDestRGB;
200 cmsMAT3 Cone, Tmp;
203 Tmp = *Chad;
204 if (!_cmsMAT3inverse(&Tmp, &Chad_Inv)) return FALSE;
206 _cmsVEC3init(&ConeSourceXYZ, SourceWhitePoint -> X,
207 SourceWhitePoint -> Y,
208 SourceWhitePoint -> Z);
210 _cmsVEC3init(&ConeDestXYZ, DestWhitePoint -> X,
211 DestWhitePoint -> Y,
212 DestWhitePoint -> Z);
214 _cmsMAT3eval(&ConeSourceRGB, Chad, &ConeSourceXYZ);
215 _cmsMAT3eval(&ConeDestRGB, Chad, &ConeDestXYZ);
217 // Build matrix
218 _cmsVEC3init(&Cone.v[0], ConeDestRGB.n[0]/ConeSourceRGB.n[0], 0.0, 0.0);
219 _cmsVEC3init(&Cone.v[1], 0.0, ConeDestRGB.n[1]/ConeSourceRGB.n[1], 0.0);
220 _cmsVEC3init(&Cone.v[2], 0.0, 0.0, ConeDestRGB.n[2]/ConeSourceRGB.n[2]);
223 // Normalize
224 _cmsMAT3per(&Tmp, &Cone, Chad);
225 _cmsMAT3per(Conversion, &Chad_Inv, &Tmp);
227 return TRUE;
230 // Returns the final chrmatic adaptation from illuminant FromIll to Illuminant ToIll
231 // The cone matrix can be specified in ConeMatrix. If NULL, Bradford is assumed
232 cmsBool _cmsAdaptationMatrix(cmsMAT3* r, const cmsMAT3* ConeMatrix, const cmsCIEXYZ* FromIll, const cmsCIEXYZ* ToIll)
234 cmsMAT3 LamRigg = {{ // Bradford matrix
235 {{ 0.8951, 0.2664, -0.1614 }},
236 {{ -0.7502, 1.7135, 0.0367 }},
237 {{ 0.0389, -0.0685, 1.0296 }}
240 if (ConeMatrix == NULL)
241 ConeMatrix = &LamRigg;
243 return ComputeChromaticAdaptation(r, FromIll, ToIll, ConeMatrix);
246 // Same as anterior, but assuming D50 destination. White point is given in xyY
247 static
248 cmsBool _cmsAdaptMatrixToD50(cmsMAT3* r, const cmsCIExyY* SourceWhitePt)
250 cmsCIEXYZ Dn;
251 cmsMAT3 Bradford;
252 cmsMAT3 Tmp;
254 cmsxyY2XYZ(&Dn, SourceWhitePt);
256 if (!_cmsAdaptationMatrix(&Bradford, NULL, &Dn, cmsD50_XYZ())) return FALSE;
258 Tmp = *r;
259 _cmsMAT3per(r, &Bradford, &Tmp);
261 return TRUE;
264 // Build a White point, primary chromas transfer matrix from RGB to CIE XYZ
265 // This is just an approximation, I am not handling all the non-linear
266 // aspects of the RGB to XYZ process, and assumming that the gamma correction
267 // has transitive property in the tranformation chain.
269 // the alghoritm:
271 // - First I build the absolute conversion matrix using
272 // primaries in XYZ. This matrix is next inverted
273 // - Then I eval the source white point across this matrix
274 // obtaining the coeficients of the transformation
275 // - Then, I apply these coeficients to the original matrix
277 cmsBool _cmsBuildRGB2XYZtransferMatrix(cmsMAT3* r, const cmsCIExyY* WhitePt, const cmsCIExyYTRIPLE* Primrs)
279 cmsVEC3 WhitePoint, Coef;
280 cmsMAT3 Result, Primaries;
281 cmsFloat64Number xn, yn;
282 cmsFloat64Number xr, yr;
283 cmsFloat64Number xg, yg;
284 cmsFloat64Number xb, yb;
286 xn = WhitePt -> x;
287 yn = WhitePt -> y;
288 xr = Primrs -> Red.x;
289 yr = Primrs -> Red.y;
290 xg = Primrs -> Green.x;
291 yg = Primrs -> Green.y;
292 xb = Primrs -> Blue.x;
293 yb = Primrs -> Blue.y;
295 // Build Primaries matrix
296 _cmsVEC3init(&Primaries.v[0], xr, xg, xb);
297 _cmsVEC3init(&Primaries.v[1], yr, yg, yb);
298 _cmsVEC3init(&Primaries.v[2], (1-xr-yr), (1-xg-yg), (1-xb-yb));
301 // Result = Primaries ^ (-1) inverse matrix
302 if (!_cmsMAT3inverse(&Primaries, &Result))
303 return FALSE;
306 _cmsVEC3init(&WhitePoint, xn/yn, 1.0, (1.0-xn-yn)/yn);
308 // Across inverse primaries ...
309 _cmsMAT3eval(&Coef, &Result, &WhitePoint);
311 // Give us the Coefs, then I build transformation matrix
312 _cmsVEC3init(&r -> v[0], Coef.n[VX]*xr, Coef.n[VY]*xg, Coef.n[VZ]*xb);
313 _cmsVEC3init(&r -> v[1], Coef.n[VX]*yr, Coef.n[VY]*yg, Coef.n[VZ]*yb);
314 _cmsVEC3init(&r -> v[2], Coef.n[VX]*(1.0-xr-yr), Coef.n[VY]*(1.0-xg-yg), Coef.n[VZ]*(1.0-xb-yb));
317 return _cmsAdaptMatrixToD50(r, WhitePt);
322 // Adapts a color to a given illuminant. Original color is expected to have
323 // a SourceWhitePt white point.
324 cmsBool CMSEXPORT cmsAdaptToIlluminant(cmsCIEXYZ* Result,
325 const cmsCIEXYZ* SourceWhitePt,
326 const cmsCIEXYZ* Illuminant,
327 const cmsCIEXYZ* Value)
329 cmsMAT3 Bradford;
330 cmsVEC3 In, Out;
332 _cmsAssert(Result != NULL);
333 _cmsAssert(SourceWhitePt != NULL);
334 _cmsAssert(Illuminant != NULL);
335 _cmsAssert(Value != NULL);
337 if (!_cmsAdaptationMatrix(&Bradford, NULL, SourceWhitePt, Illuminant)) return FALSE;
339 _cmsVEC3init(&In, Value -> X, Value -> Y, Value -> Z);
340 _cmsMAT3eval(&Out, &Bradford, &In);
342 Result -> X = Out.n[0];
343 Result -> Y = Out.n[1];
344 Result -> Z = Out.n[2];
346 return TRUE;