2
|
1 |
/*
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
3 |
*
|
|
4 |
* This code is free software; you can redistribute it and/or modify it
|
|
5 |
* under the terms of the GNU General Public License version 2 only, as
|
|
6 |
* published by the Free Software Foundation. Sun designates this
|
|
7 |
* particular file as subject to the "Classpath" exception as provided
|
|
8 |
* by Sun in the LICENSE file that accompanied this code.
|
|
9 |
*
|
|
10 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
11 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
14 |
* accompanied this code).
|
|
15 |
*
|
|
16 |
* You should have received a copy of the GNU General Public License version
|
|
17 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
18 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
19 |
*
|
|
20 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
21 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
22 |
* have any questions.
|
|
23 |
*/
|
|
24 |
|
|
25 |
// This file is available under and governed by the GNU General Public
|
|
26 |
// License version 2 only, as published by the Free Software Foundation.
|
|
27 |
// However, the following notice accompanied the original version of this
|
|
28 |
// file:
|
|
29 |
//
|
|
30 |
//
|
|
31 |
// Little cms
|
2394
|
32 |
// Copyright (C) 1998-2007 Marti Maria
|
2
|
33 |
//
|
|
34 |
// Permission is hereby granted, free of charge, to any person obtaining
|
|
35 |
// a copy of this software and associated documentation files (the "Software"),
|
|
36 |
// to deal in the Software without restriction, including without limitation
|
|
37 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
38 |
// and/or sell copies of the Software, and to permit persons to whom the Software
|
|
39 |
// is furnished to do so, subject to the following conditions:
|
|
40 |
//
|
|
41 |
// The above copyright notice and this permission notice shall be included in
|
|
42 |
// all copies or substantial portions of the Software.
|
|
43 |
//
|
|
44 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
45 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
|
|
46 |
// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
47 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
48 |
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
49 |
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
50 |
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
51 |
|
|
52 |
|
|
53 |
#include "lcms.h"
|
|
54 |
|
|
55 |
/*
|
|
56 |
Gamut check by default is a catching of 0xFFFF/0xFFFF/0xFFFF PCS values, used
|
|
57 |
internally by lcms to hold invalid values. Matrix LUT's, operates in a way that
|
|
58 |
unencodeable values are marked as this combination, if PCS is XYZ, this is a very
|
|
59 |
high value since encoding is a 1.15 fixed point, something like 1.9997, 1.9997, 1.9997
|
|
60 |
not a very common color after all. Lab PCS is not to be a problem, since L>100 are truely
|
|
61 |
undefined. There is a posibility than ICC comitee defines L>100 as a valid means
|
|
62 |
to use highlights, then it will be lost.
|
|
63 |
|
|
64 |
(1.10 - Actually ICC did it, so this should be checked for full ICC 4.0 support)
|
|
65 |
|
|
66 |
*/
|
|
67 |
|
|
68 |
|
2394
|
69 |
LCMSBOOL _cmsEndPointsBySpace(icColorSpaceSignature Space, WORD **White, WORD **Black,
|
2
|
70 |
int *nOutputs)
|
|
71 |
{
|
|
72 |
// Only most common spaces
|
|
73 |
|
|
74 |
static WORD RGBblack[4] = { 0, 0, 0 };
|
|
75 |
static WORD RGBwhite[4] = { 0xffff, 0xffff, 0xffff };
|
|
76 |
static WORD CMYKblack[4] = { 0xffff, 0xffff, 0xffff, 0xffff }; // 400% of ink
|
|
77 |
static WORD CMYKwhite[4] = { 0, 0, 0, 0 };
|
|
78 |
static WORD LABblack[4] = { 0, 0x8000, 0x8000 };
|
|
79 |
static WORD LABwhite[4] = { 0xFF00, 0x8000, 0x8000 };
|
|
80 |
static WORD CMYblack[4] = { 0xffff, 0xffff, 0xffff };
|
|
81 |
static WORD CMYwhite[4] = { 0, 0, 0 };
|
|
82 |
static WORD Grayblack[4] = { 0 };
|
|
83 |
static WORD GrayWhite[4] = { 0xffff };
|
|
84 |
|
|
85 |
switch (Space) {
|
|
86 |
|
|
87 |
case icSigGrayData: if (White) *White = GrayWhite;
|
|
88 |
if (Black) *Black = Grayblack;
|
|
89 |
if (nOutputs) *nOutputs = 1;
|
|
90 |
return TRUE;
|
|
91 |
|
|
92 |
case icSigRgbData: if (White) *White = RGBwhite;
|
|
93 |
if (Black) *Black = RGBblack;
|
|
94 |
if (nOutputs) *nOutputs = 3;
|
|
95 |
return TRUE;
|
|
96 |
|
|
97 |
case icSigLabData: if (White) *White = LABwhite;
|
|
98 |
if (Black) *Black = LABblack;
|
|
99 |
if (nOutputs) *nOutputs = 3;
|
|
100 |
return TRUE;
|
|
101 |
|
|
102 |
case icSigCmykData: if (White) *White = CMYKwhite;
|
|
103 |
if (Black) *Black = CMYKblack;
|
|
104 |
if (nOutputs) *nOutputs = 4;
|
|
105 |
return TRUE;
|
|
106 |
|
|
107 |
case icSigCmyData: if (White) *White = CMYwhite;
|
|
108 |
if (Black) *Black = CMYblack;
|
|
109 |
if (nOutputs) *nOutputs = 3;
|
|
110 |
return TRUE;
|
|
111 |
|
|
112 |
default:;
|
|
113 |
}
|
|
114 |
|
|
115 |
return FALSE;
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
WORD *_cmsWhiteBySpace(icColorSpaceSignature Space)
|
|
120 |
{
|
|
121 |
WORD *White= NULL, *Black = NULL;
|
|
122 |
int Dummy;
|
|
123 |
static WORD Default[MAXCHANNELS];
|
|
124 |
|
|
125 |
if (_cmsEndPointsBySpace(Space, &White, &Black, &Dummy))
|
|
126 |
return White;
|
|
127 |
|
|
128 |
return Default;
|
|
129 |
|
|
130 |
}
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
WORD Clamp_L(Fixed32 in)
|
|
136 |
{
|
|
137 |
if (in == 0xFFFF) return 0xFFFFU; // Marker
|
|
138 |
|
|
139 |
if (in > 0xFF00) return 0xFF00U; // L* = 100.0
|
|
140 |
return (WORD) in;
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
#define ENCODE_AB(x) (WORD) (((x) + 128.0) * 256.0 + 0.5)
|
|
145 |
|
|
146 |
WORD Clamp_ab(Fixed32 in)
|
|
147 |
{
|
|
148 |
if (in == 0xFFFF) return 0xFFFFU; // Marker
|
|
149 |
|
|
150 |
if (in < 0) return ENCODE_AB(-128.0); // Max negative number
|
|
151 |
if (in > 0xFFFF) return ENCODE_AB(+127.9961); // Max positive number
|
|
152 |
return (WORD) in;
|
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
// Returns dE on two Lab values
|
|
158 |
|
|
159 |
double LCMSEXPORT cmsDeltaE(LPcmsCIELab Lab1, LPcmsCIELab Lab2)
|
|
160 |
{
|
|
161 |
double dL, da, db;
|
|
162 |
|
|
163 |
if (Lab1 -> L < 0 ||
|
|
164 |
Lab2 -> L < 0) return 65536.;
|
|
165 |
|
|
166 |
if (Lab1 -> a < -200 || Lab1 -> a > 200) return 65536.;
|
|
167 |
if (Lab1 -> b < -200 || Lab1 -> b > 200) return 65536.;
|
|
168 |
|
|
169 |
if (Lab2 -> a < -200 || Lab2 -> a > 200) return 65536.;
|
|
170 |
if (Lab2 -> b < -200 || Lab2 -> b > 200) return 65536.;
|
|
171 |
|
|
172 |
if (Lab1 ->L == 0 && Lab2 ->L == 0) return 0;
|
|
173 |
|
|
174 |
dL = fabs(Lab1 -> L - Lab2 -> L);
|
|
175 |
da = fabs(Lab1 -> a - Lab2 -> a);
|
|
176 |
db = fabs(Lab1 -> b - Lab2 -> b);
|
|
177 |
|
|
178 |
return pow(dL*dL + da * da + db * db, 0.5);
|
|
179 |
|
|
180 |
}
|
|
181 |
|
|
182 |
|
|
183 |
// Square
|
|
184 |
static
|
|
185 |
double Sqr(double v)
|
|
186 |
{
|
|
187 |
return v * v;
|
|
188 |
}
|
|
189 |
|
|
190 |
// Return the CIE94 Delta E
|
|
191 |
double LCMSEXPORT cmsCIE94DeltaE(LPcmsCIELab Lab1, LPcmsCIELab Lab2)
|
|
192 |
{
|
|
193 |
cmsCIELCh LCh1, LCh2;
|
|
194 |
double dE, dL, dC, dh, dhsq;
|
|
195 |
double c12, sc, sh;
|
|
196 |
|
|
197 |
if (Lab1 ->L == 0 && Lab2 ->L == 0) return 0;
|
|
198 |
|
|
199 |
dL = fabs(Lab1 ->L - Lab2 ->L);
|
|
200 |
|
|
201 |
cmsLab2LCh(&LCh1, Lab1);
|
|
202 |
cmsLab2LCh(&LCh2, Lab2);
|
|
203 |
|
|
204 |
dC = fabs(LCh1.C - LCh2.C);
|
|
205 |
dE = cmsDeltaE(Lab1, Lab2);
|
|
206 |
|
|
207 |
dhsq = Sqr(dE) - Sqr(dL) - Sqr(dC);
|
|
208 |
if (dhsq < 0)
|
|
209 |
dh = 0;
|
|
210 |
else
|
|
211 |
dh = pow(dhsq, 0.5);
|
|
212 |
|
|
213 |
c12 = sqrt(LCh1.C * LCh2.C);
|
|
214 |
|
|
215 |
sc = 1.0 + (0.048 * c12);
|
|
216 |
sh = 1.0 + (0.014 * c12);
|
|
217 |
|
|
218 |
return sqrt(Sqr(dL) + Sqr(dC) / Sqr(sc) + Sqr(dh) / Sqr(sh));
|
|
219 |
}
|
|
220 |
|
|
221 |
|
|
222 |
// Auxiliary
|
|
223 |
|
|
224 |
static
|
|
225 |
double ComputeLBFD(LPcmsCIELab Lab)
|
|
226 |
{
|
|
227 |
double yt;
|
|
228 |
|
|
229 |
if (Lab->L > 7.996969)
|
|
230 |
yt = (Sqr((Lab->L+16)/116)*((Lab->L+16)/116))*100;
|
|
231 |
else
|
|
232 |
yt = 100 * (Lab->L / 903.3);
|
|
233 |
|
|
234 |
return (54.6 * (LOGE * (log(yt + 1.5))) - 9.6);
|
|
235 |
}
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
// bfd - gets BFD(1:1) difference between Lab1, Lab2
|
|
240 |
double LCMSEXPORT cmsBFDdeltaE(LPcmsCIELab Lab1, LPcmsCIELab Lab2)
|
|
241 |
{
|
|
242 |
double lbfd1,lbfd2,AveC,Aveh,dE,deltaL,
|
|
243 |
deltaC,deltah,dc,t,g,dh,rh,rc,rt,bfd;
|
|
244 |
cmsCIELCh LCh1, LCh2;
|
|
245 |
|
|
246 |
|
|
247 |
if (Lab1 ->L == 0 && Lab2 ->L == 0) return 0;
|
|
248 |
|
|
249 |
lbfd1 = ComputeLBFD(Lab1);
|
|
250 |
lbfd2 = ComputeLBFD(Lab2);
|
|
251 |
deltaL = lbfd2 - lbfd1;
|
|
252 |
|
|
253 |
cmsLab2LCh(&LCh1, Lab1);
|
|
254 |
cmsLab2LCh(&LCh2, Lab2);
|
|
255 |
|
|
256 |
deltaC = LCh2.C - LCh1.C;
|
|
257 |
AveC = (LCh1.C+LCh2.C)/2;
|
|
258 |
Aveh = (LCh1.h+LCh2.h)/2;
|
|
259 |
|
|
260 |
dE = cmsDeltaE(Lab1, Lab2);
|
|
261 |
|
|
262 |
if (Sqr(dE)>(Sqr(Lab2->L-Lab1->L)+Sqr(deltaC)))
|
|
263 |
deltah = sqrt(Sqr(dE)-Sqr(Lab2->L-Lab1->L)-Sqr(deltaC));
|
|
264 |
else
|
|
265 |
deltah =0;
|
|
266 |
|
|
267 |
|
|
268 |
dc = 0.035 * AveC / (1 + 0.00365 * AveC)+0.521;
|
|
269 |
g = sqrt(Sqr(Sqr(AveC))/(Sqr(Sqr(AveC))+14000));
|
|
270 |
t = 0.627+(0.055*cos((Aveh-254)/(180/M_PI))-
|
|
271 |
0.040*cos((2*Aveh-136)/(180/M_PI))+
|
|
272 |
0.070*cos((3*Aveh-31)/(180/M_PI))+
|
|
273 |
0.049*cos((4*Aveh+114)/(180/M_PI))-
|
|
274 |
0.015*cos((5*Aveh-103)/(180/M_PI)));
|
|
275 |
|
|
276 |
dh = dc*(g*t+1-g);
|
|
277 |
rh = -0.260*cos((Aveh-308)/(180/M_PI))-
|
|
278 |
0.379*cos((2*Aveh-160)/(180/M_PI))-
|
|
279 |
0.636*cos((3*Aveh+254)/(180/M_PI))+
|
|
280 |
0.226*cos((4*Aveh+140)/(180/M_PI))-
|
|
281 |
0.194*cos((5*Aveh+280)/(180/M_PI));
|
|
282 |
|
|
283 |
rc = sqrt((AveC*AveC*AveC*AveC*AveC*AveC)/((AveC*AveC*AveC*AveC*AveC*AveC)+70000000));
|
|
284 |
rt = rh*rc;
|
|
285 |
|
|
286 |
bfd = sqrt(Sqr(deltaL)+Sqr(deltaC/dc)+Sqr(deltah/dh)+(rt*(deltaC/dc)*(deltah/dh)));
|
|
287 |
|
|
288 |
return bfd;
|
|
289 |
}
|
|
290 |
|
|
291 |
|
|
292 |
// cmc - CMC(1:1) difference between Lab1, Lab2
|
|
293 |
double LCMSEXPORT cmsCMCdeltaE(LPcmsCIELab Lab1, LPcmsCIELab Lab2)
|
|
294 |
{
|
|
295 |
double dE,dL,dC,dh,sl,sc,sh,t,f,cmc;
|
|
296 |
cmsCIELCh LCh1, LCh2;
|
|
297 |
|
|
298 |
if (Lab1 ->L == 0 && Lab2 ->L == 0) return 0;
|
|
299 |
|
|
300 |
cmsLab2LCh(&LCh1, Lab1);
|
|
301 |
cmsLab2LCh(&LCh2, Lab2);
|
|
302 |
|
|
303 |
|
|
304 |
dL = Lab2->L-Lab1->L;
|
|
305 |
dC = LCh2.C-LCh1.C;
|
|
306 |
|
|
307 |
dE = cmsDeltaE(Lab1, Lab2);
|
|
308 |
if (Sqr(dE)>(Sqr(dL)+Sqr(dC)))
|
|
309 |
dh = sqrt(Sqr(dE)-Sqr(dL)-Sqr(dC));
|
|
310 |
else
|
|
311 |
dh =0;
|
|
312 |
|
|
313 |
if ((LCh1.h > 164) && (LCh1.h<345))
|
|
314 |
t = 0.56 + fabs(0.2 * cos(((LCh1.h + 168)/(180/M_PI))));
|
|
315 |
else
|
|
316 |
t = 0.36 + fabs(0.4 * cos(((LCh1.h + 35 )/(180/M_PI))));
|
|
317 |
|
|
318 |
sc = 0.0638 * LCh1.C / (1 + 0.0131 * LCh1.C) + 0.638;
|
|
319 |
sl = 0.040975 * Lab1->L /(1 + 0.01765 * Lab1->L);
|
|
320 |
|
|
321 |
if (Lab1->L<16)
|
|
322 |
sl = 0.511;
|
|
323 |
|
|
324 |
f = sqrt((LCh1.C * LCh1.C * LCh1.C * LCh1.C)/((LCh1.C * LCh1.C * LCh1.C * LCh1.C)+1900));
|
|
325 |
sh = sc*(t*f+1-f);
|
|
326 |
cmc = sqrt(Sqr(dL/sl)+Sqr(dC/sc)+Sqr(dh/sh));
|
|
327 |
|
|
328 |
return cmc;
|
|
329 |
}
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
static
|
|
334 |
double atan2deg(double b, double a)
|
|
335 |
{
|
|
336 |
double h;
|
|
337 |
|
|
338 |
if (a == 0 && b == 0)
|
|
339 |
h = 0;
|
|
340 |
else
|
|
341 |
h = atan2(a, b);
|
|
342 |
|
|
343 |
h *= (180. / M_PI);
|
|
344 |
|
|
345 |
while (h > 360.)
|
|
346 |
h -= 360.;
|
|
347 |
|
|
348 |
while ( h < 0)
|
|
349 |
h += 360.;
|
|
350 |
|
|
351 |
return h;
|
|
352 |
|
|
353 |
}
|
|
354 |
|
|
355 |
|
|
356 |
static
|
|
357 |
double RADIANES(double deg)
|
|
358 |
{
|
|
359 |
return (deg * M_PI) / 180.;
|
|
360 |
}
|
|
361 |
|
|
362 |
|
|
363 |
// dE2000 The weightings KL, KC and KH can be modified to reflect the relative
|
|
364 |
// importance of lightness, chroma and hue in different industrial applications
|
|
365 |
|
|
366 |
double LCMSEXPORT cmsCIE2000DeltaE(LPcmsCIELab Lab1, LPcmsCIELab Lab2,
|
|
367 |
double Kl, double Kc, double Kh)
|
|
368 |
{
|
|
369 |
double L1 = Lab1->L;
|
|
370 |
double a1 = Lab1->a;
|
|
371 |
double b1 = Lab1->b;
|
|
372 |
double C = sqrt( Sqr(a1) + Sqr(b1) );
|
|
373 |
|
|
374 |
double Ls = Lab2 ->L;
|
|
375 |
double as = Lab2 ->a;
|
|
376 |
double bs = Lab2 ->b;
|
|
377 |
double Cs = sqrt( Sqr(as) + Sqr(bs) );
|
|
378 |
|
|
379 |
double G = 0.5 * ( 1 - sqrt(pow((C + Cs) / 2 , 7.0) / (pow((C + Cs) / 2, 7.0) + pow(25.0, 7.0) ) ));
|
|
380 |
|
|
381 |
double a_p = (1 + G ) * a1;
|
|
382 |
double b_p = b1;
|
|
383 |
double C_p = sqrt( Sqr(a_p) + Sqr(b_p));
|
|
384 |
double h_p = atan2deg(a_p, b_p);
|
|
385 |
|
|
386 |
|
|
387 |
double a_ps = (1 + G) * as;
|
|
388 |
double b_ps = bs;
|
|
389 |
double C_ps = sqrt(Sqr(a_ps) + Sqr(b_ps));
|
|
390 |
double h_ps = atan2deg(a_ps, b_ps);
|
|
391 |
|
|
392 |
double meanC_p =(C_p + C_ps) / 2;
|
|
393 |
|
2394
|
394 |
double hps_plus_hp = h_ps + h_p;
|
|
395 |
double hps_minus_hp = h_ps - h_p;
|
|
396 |
|
|
397 |
double meanh_p = fabs(hps_minus_hp) <= 180.000001 ? (hps_plus_hp)/2 :
|
|
398 |
(hps_plus_hp) < 360 ? (hps_plus_hp + 360)/2 :
|
|
399 |
(hps_plus_hp - 360)/2;
|
2
|
400 |
|
2394
|
401 |
double delta_h = (hps_minus_hp) <= -180.000001 ? (hps_minus_hp + 360) :
|
|
402 |
(hps_minus_hp) > 180 ? (hps_minus_hp - 360) :
|
|
403 |
(hps_minus_hp);
|
|
404 |
double delta_L = (Ls - L1);
|
|
405 |
double delta_C = (C_ps - C_p );
|
|
406 |
|
2
|
407 |
|
|
408 |
double delta_H =2 * sqrt(C_ps*C_p) * sin(RADIANES(delta_h) / 2);
|
|
409 |
|
|
410 |
double T = 1 - 0.17 * cos(RADIANES(meanh_p-30))
|
|
411 |
+ 0.24 * cos(RADIANES(2*meanh_p))
|
|
412 |
+ 0.32 * cos(RADIANES(3*meanh_p + 6))
|
|
413 |
- 0.2 * cos(RADIANES(4*meanh_p - 63));
|
|
414 |
|
|
415 |
double Sl = 1 + (0.015 * Sqr((Ls + L1) /2- 50) )/ sqrt(20 + Sqr( (Ls+L1)/2 - 50) );
|
|
416 |
|
|
417 |
double Sc = 1 + 0.045 * (C_p + C_ps)/2;
|
|
418 |
double Sh = 1 + 0.015 * ((C_ps + C_p)/2) * T;
|
|
419 |
|
|
420 |
double delta_ro = 30 * exp( -Sqr(((meanh_p - 275 ) / 25)));
|
|
421 |
|
|
422 |
double Rc = 2 * sqrt(( pow(meanC_p, 7.0) )/( pow(meanC_p, 7.0) + pow(25.0, 7.0)));
|
|
423 |
|
|
424 |
double Rt = -sin(2 * RADIANES(delta_ro)) * Rc;
|
|
425 |
|
|
426 |
double deltaE00 = sqrt( Sqr(delta_L /(Sl * Kl)) +
|
|
427 |
Sqr(delta_C/(Sc * Kc)) +
|
|
428 |
Sqr(delta_H/(Sh * Kh)) +
|
|
429 |
Rt*(delta_C/(Sc * Kc)) * (delta_H / (Sh * Kh)));
|
|
430 |
|
|
431 |
return deltaE00;
|
|
432 |
}
|
|
433 |
|
|
434 |
|
|
435 |
|
|
436 |
// Carefully, clamp on CIELab space.
|
|
437 |
|
|
438 |
void LCMSEXPORT cmsClampLab(LPcmsCIELab Lab, double amax, double amin,
|
|
439 |
double bmax, double bmin)
|
|
440 |
{
|
|
441 |
|
|
442 |
// Whole Luma surface to zero
|
|
443 |
|
|
444 |
if (Lab -> L < 0) {
|
|
445 |
|
|
446 |
Lab-> L = Lab->a = Lab-> b = 0.0;
|
|
447 |
return;
|
|
448 |
}
|
|
449 |
|
|
450 |
// Clamp white, DISCARD HIGHLIGHTS. This is done
|
|
451 |
// in such way because icc spec doesn't allow the
|
|
452 |
// use of L>100 as a highlight means.
|
|
453 |
|
|
454 |
if (Lab->L > 100)
|
|
455 |
Lab -> L = 100;
|
|
456 |
|
|
457 |
// Check out gamut prism, on a, b faces
|
|
458 |
|
|
459 |
if (Lab -> a < amin || Lab->a > amax||
|
|
460 |
Lab -> b < bmin || Lab->b > bmax) {
|
|
461 |
|
|
462 |
cmsCIELCh LCh;
|
|
463 |
double h, slope;
|
|
464 |
|
|
465 |
// Falls outside a, b limits. Transports to LCh space,
|
|
466 |
// and then do the clipping
|
|
467 |
|
|
468 |
|
|
469 |
if (Lab -> a == 0.0) { // Is hue exactly 90?
|
|
470 |
|
|
471 |
// atan will not work, so clamp here
|
|
472 |
Lab -> b = Lab->b < 0 ? bmin : bmax;
|
|
473 |
return;
|
|
474 |
}
|
|
475 |
|
|
476 |
cmsLab2LCh(&LCh, Lab);
|
|
477 |
|
|
478 |
slope = Lab -> b / Lab -> a;
|
|
479 |
h = LCh.h;
|
|
480 |
|
|
481 |
// There are 4 zones
|
|
482 |
|
|
483 |
if ((h >= 0. && h < 45.) ||
|
|
484 |
(h >= 315 && h <= 360.)) {
|
|
485 |
|
|
486 |
// clip by amax
|
|
487 |
Lab -> a = amax;
|
|
488 |
Lab -> b = amax * slope;
|
|
489 |
}
|
|
490 |
else
|
|
491 |
if (h >= 45. && h < 135)
|
|
492 |
{
|
|
493 |
// clip by bmax
|
|
494 |
Lab -> b = bmax;
|
|
495 |
Lab -> a = bmax / slope;
|
|
496 |
}
|
|
497 |
else
|
|
498 |
if (h >= 135 && h < 225) {
|
|
499 |
// clip by amin
|
|
500 |
Lab -> a = amin;
|
|
501 |
Lab -> b = amin * slope;
|
|
502 |
|
|
503 |
}
|
|
504 |
else
|
|
505 |
if (h >= 225 && h < 315) {
|
|
506 |
// clip by bmin
|
|
507 |
Lab -> b = bmin;
|
|
508 |
Lab -> a = bmin / slope;
|
|
509 |
}
|
|
510 |
else
|
|
511 |
cmsSignalError(LCMS_ERRC_ABORTED, "Invalid angle");
|
|
512 |
|
|
513 |
}
|
|
514 |
}
|
|
515 |
|
|
516 |
// Several utilities -------------------------------------------------------
|
|
517 |
|
|
518 |
// Translate from our colorspace to ICC representation
|
|
519 |
|
|
520 |
icColorSpaceSignature LCMSEXPORT _cmsICCcolorSpace(int OurNotation)
|
|
521 |
{
|
|
522 |
switch (OurNotation) {
|
|
523 |
|
|
524 |
case 1:
|
|
525 |
case PT_GRAY: return icSigGrayData;
|
|
526 |
|
|
527 |
case 2:
|
|
528 |
case PT_RGB: return icSigRgbData;
|
|
529 |
|
|
530 |
case PT_CMY: return icSigCmyData;
|
|
531 |
case PT_CMYK: return icSigCmykData;
|
|
532 |
case PT_YCbCr:return icSigYCbCrData;
|
|
533 |
case PT_YUV: return icSigLuvData;
|
|
534 |
case PT_XYZ: return icSigXYZData;
|
|
535 |
case PT_Lab: return icSigLabData;
|
|
536 |
case PT_YUVK: return icSigLuvKData;
|
|
537 |
case PT_HSV: return icSigHsvData;
|
|
538 |
case PT_HLS: return icSigHlsData;
|
|
539 |
case PT_Yxy: return icSigYxyData;
|
|
540 |
case PT_HiFi: return icSigHexachromeData;
|
|
541 |
case PT_HiFi7: return icSigHeptachromeData;
|
|
542 |
case PT_HiFi8: return icSigOctachromeData;
|
|
543 |
|
|
544 |
case PT_HiFi9: return icSigMCH9Data;
|
|
545 |
case PT_HiFi10: return icSigMCHAData;
|
|
546 |
case PT_HiFi11: return icSigMCHBData;
|
|
547 |
case PT_HiFi12: return icSigMCHCData;
|
|
548 |
case PT_HiFi13: return icSigMCHDData;
|
|
549 |
case PT_HiFi14: return icSigMCHEData;
|
|
550 |
case PT_HiFi15: return icSigMCHFData;
|
|
551 |
|
|
552 |
default: return icMaxEnumData;
|
|
553 |
}
|
|
554 |
}
|
|
555 |
|
|
556 |
|
|
557 |
int LCMSEXPORT _cmsLCMScolorSpace(icColorSpaceSignature ProfileSpace)
|
|
558 |
{
|
|
559 |
switch (ProfileSpace) {
|
|
560 |
|
|
561 |
case icSigGrayData: return PT_GRAY;
|
|
562 |
case icSigRgbData: return PT_RGB;
|
|
563 |
case icSigCmyData: return PT_CMY;
|
|
564 |
case icSigCmykData: return PT_CMYK;
|
|
565 |
case icSigYCbCrData:return PT_YCbCr;
|
|
566 |
case icSigLuvData: return PT_YUV;
|
|
567 |
case icSigXYZData: return PT_XYZ;
|
|
568 |
case icSigLabData: return PT_Lab;
|
|
569 |
case icSigLuvKData: return PT_YUVK;
|
|
570 |
case icSigHsvData: return PT_HSV;
|
|
571 |
case icSigHlsData: return PT_HLS;
|
|
572 |
case icSigYxyData: return PT_Yxy;
|
|
573 |
|
|
574 |
case icSig6colorData:
|
|
575 |
case icSigHexachromeData: return PT_HiFi;
|
|
576 |
|
|
577 |
case icSigHeptachromeData:
|
|
578 |
case icSig7colorData: return PT_HiFi7;
|
|
579 |
|
|
580 |
case icSigOctachromeData:
|
|
581 |
case icSig8colorData: return PT_HiFi8;
|
|
582 |
|
|
583 |
case icSigMCH9Data:
|
|
584 |
case icSig9colorData: return PT_HiFi9;
|
|
585 |
|
|
586 |
case icSigMCHAData:
|
|
587 |
case icSig10colorData: return PT_HiFi10;
|
|
588 |
|
|
589 |
case icSigMCHBData:
|
|
590 |
case icSig11colorData: return PT_HiFi11;
|
|
591 |
|
|
592 |
case icSigMCHCData:
|
|
593 |
case icSig12colorData: return PT_HiFi12;
|
|
594 |
|
|
595 |
case icSigMCHDData:
|
|
596 |
case icSig13colorData: return PT_HiFi13;
|
|
597 |
|
|
598 |
case icSigMCHEData:
|
|
599 |
case icSig14colorData: return PT_HiFi14;
|
|
600 |
|
|
601 |
case icSigMCHFData:
|
|
602 |
case icSig15colorData: return PT_HiFi15;
|
|
603 |
|
|
604 |
default: return icMaxEnumData;
|
|
605 |
}
|
|
606 |
}
|
|
607 |
|
|
608 |
|
|
609 |
int LCMSEXPORT _cmsChannelsOf(icColorSpaceSignature ColorSpace)
|
|
610 |
{
|
|
611 |
|
|
612 |
switch (ColorSpace) {
|
|
613 |
|
|
614 |
case icSigGrayData: return 1;
|
|
615 |
|
|
616 |
case icSig2colorData: return 2;
|
|
617 |
|
|
618 |
case icSigXYZData:
|
|
619 |
case icSigLabData:
|
|
620 |
case icSigLuvData:
|
|
621 |
case icSigYCbCrData:
|
|
622 |
case icSigYxyData:
|
|
623 |
case icSigRgbData:
|
|
624 |
case icSigHsvData:
|
|
625 |
case icSigHlsData:
|
|
626 |
case icSigCmyData:
|
|
627 |
case icSig3colorData: return 3;
|
|
628 |
|
|
629 |
case icSigLuvKData:
|
|
630 |
case icSigCmykData:
|
|
631 |
case icSig4colorData: return 4;
|
|
632 |
|
|
633 |
case icSigMCH5Data:
|
|
634 |
case icSig5colorData: return 5;
|
|
635 |
|
|
636 |
case icSigHexachromeData:
|
|
637 |
case icSig6colorData: return 6;
|
|
638 |
|
|
639 |
case icSigHeptachromeData:
|
|
640 |
case icSig7colorData: return 7;
|
|
641 |
|
|
642 |
case icSigOctachromeData:
|
|
643 |
case icSig8colorData: return 8;
|
|
644 |
|
|
645 |
case icSigMCH9Data:
|
|
646 |
case icSig9colorData: return 9;
|
|
647 |
|
|
648 |
case icSigMCHAData:
|
|
649 |
case icSig10colorData: return 10;
|
|
650 |
|
|
651 |
case icSigMCHBData:
|
|
652 |
case icSig11colorData: return 11;
|
|
653 |
|
|
654 |
case icSigMCHCData:
|
|
655 |
case icSig12colorData: return 12;
|
|
656 |
|
|
657 |
case icSigMCHDData:
|
|
658 |
case icSig13colorData: return 13;
|
|
659 |
|
|
660 |
case icSigMCHEData:
|
|
661 |
case icSig14colorData: return 14;
|
|
662 |
|
|
663 |
case icSigMCHFData:
|
|
664 |
case icSig15colorData: return 15;
|
|
665 |
|
|
666 |
default: return 3;
|
|
667 |
}
|
|
668 |
|
|
669 |
}
|
|
670 |
|
|
671 |
|
|
672 |
// v2 L=100 is supposed to be placed on 0xFF00. There is no reasonable
|
|
673 |
// number of gridpoints that would make exact match. However, a
|
|
674 |
// prelinearization of 258 entries, would map 0xFF00 on entry 257.
|
|
675 |
// This is almost what we need, unfortunately, the rest of entries
|
|
676 |
// should be scaled by (255*257/256) and this is not exact.
|
|
677 |
//
|
|
678 |
// An intermediate solution would be to use 257 entries. This does not
|
|
679 |
// map 0xFF00 exactly on a node, but so close that the dE induced is
|
|
680 |
// negligible. AND the rest of curve is exact.
|
|
681 |
|
|
682 |
static
|
|
683 |
void CreateLabPrelinearization(LPGAMMATABLE LabTable[])
|
|
684 |
{
|
|
685 |
int i;
|
|
686 |
|
|
687 |
LabTable[0] = cmsAllocGamma(257);
|
|
688 |
LabTable[1] = cmsBuildGamma(257, 1.0);
|
|
689 |
LabTable[2] = cmsBuildGamma(257, 1.0);
|
|
690 |
|
|
691 |
// L* uses 257 entries. Entry 256 holds 0xFFFF, so, the effective range
|
|
692 |
// is 0..0xFF00. Last entry (257) is also collapsed to 0xFFFF
|
|
693 |
|
|
694 |
// From 0 to 0xFF00
|
|
695 |
for (i=0; i < 256; i++)
|
|
696 |
LabTable[0]->GammaTable[i] = RGB_8_TO_16(i);
|
|
697 |
|
|
698 |
// Repeat last for 0xFFFF
|
|
699 |
LabTable[0] ->GammaTable[256] = 0xFFFF;
|
|
700 |
}
|
|
701 |
|
|
702 |
|
|
703 |
// Used by gamut & softproofing
|
|
704 |
|
|
705 |
typedef struct {
|
|
706 |
|
|
707 |
cmsHTRANSFORM hInput; // From whatever input color space. NULL for Lab
|
|
708 |
cmsHTRANSFORM hForward, hReverse; // Transforms going from Lab to colorant and back
|
|
709 |
double Thereshold; // The thereshold after which is considered out of gamut
|
|
710 |
|
|
711 |
} GAMUTCHAIN,FAR* LPGAMUTCHAIN;
|
|
712 |
|
|
713 |
// This sampler does compute gamut boundaries by comparing original
|
|
714 |
// values with a transform going back and forth. Values above ERR_THERESHOLD
|
|
715 |
// of maximum are considered out of gamut.
|
|
716 |
|
|
717 |
|
|
718 |
#define ERR_THERESHOLD 5
|
|
719 |
|
|
720 |
|
|
721 |
static
|
|
722 |
int GamutSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
723 |
{
|
|
724 |
LPGAMUTCHAIN t = (LPGAMUTCHAIN) Cargo;
|
|
725 |
WORD Proof[MAXCHANNELS], Check[MAXCHANNELS];
|
|
726 |
WORD Proof2[MAXCHANNELS], Check2[MAXCHANNELS];
|
|
727 |
cmsCIELab LabIn1, LabOut1;
|
|
728 |
cmsCIELab LabIn2, LabOut2;
|
|
729 |
double dE1, dE2, ErrorRatio;
|
|
730 |
|
|
731 |
// Assume in-gamut by default.
|
|
732 |
dE1 = 0.;
|
|
733 |
dE2 = 0;
|
|
734 |
ErrorRatio = 1.0;
|
|
735 |
|
|
736 |
|
|
737 |
// Any input space? I can use In[] no matter channels
|
|
738 |
// because is just one pixel
|
|
739 |
|
|
740 |
if (t -> hInput != NULL) cmsDoTransform(t -> hInput, In, In, 1);
|
|
741 |
|
|
742 |
// converts from PCS to colorant. This always
|
|
743 |
// does return in-gamut values,
|
|
744 |
cmsDoTransform(t -> hForward, In, Proof, 1);
|
|
745 |
|
|
746 |
// Now, do the inverse, from colorant to PCS.
|
|
747 |
cmsDoTransform(t -> hReverse, Proof, Check, 1);
|
|
748 |
|
|
749 |
|
|
750 |
// Try again, but this time taking Check as input
|
|
751 |
cmsDoTransform(t -> hForward, Check, Proof2, 1);
|
|
752 |
cmsDoTransform(t -> hReverse, Proof2, Check2, 1);
|
|
753 |
|
|
754 |
|
|
755 |
|
|
756 |
// Does the transform returns out-of-gamut?
|
|
757 |
if (Check[0] == 0xFFFF &&
|
|
758 |
Check[1] == 0xFFFF &&
|
|
759 |
Check[2] == 0xFFFF)
|
|
760 |
|
|
761 |
Out[0] = 0xFF00; // Out of gamut!
|
|
762 |
else {
|
|
763 |
|
|
764 |
// Transport encoded values
|
|
765 |
cmsLabEncoded2Float(&LabIn1, In);
|
|
766 |
cmsLabEncoded2Float(&LabOut1, Check);
|
|
767 |
|
|
768 |
// Take difference of direct value
|
|
769 |
dE1 = cmsDeltaE(&LabIn1, &LabOut1);
|
|
770 |
|
|
771 |
cmsLabEncoded2Float(&LabIn2, Check);
|
|
772 |
cmsLabEncoded2Float(&LabOut2, Check2);
|
|
773 |
|
|
774 |
// Take difference of converted value
|
|
775 |
dE2 = cmsDeltaE(&LabIn2, &LabOut2);
|
|
776 |
|
|
777 |
|
|
778 |
// if dE1 is small and dE2 is small, value is likely to be in gamut
|
|
779 |
if (dE1 < t->Thereshold && dE2 < t->Thereshold)
|
|
780 |
Out[0] = 0;
|
|
781 |
else
|
|
782 |
// if dE1 is small and dE2 is big, undefined. Assume in gamut
|
|
783 |
if (dE1 < t->Thereshold && dE2 > t->Thereshold)
|
|
784 |
Out[0] = 0;
|
|
785 |
else
|
|
786 |
// dE1 is big and dE2 is small, clearly out of gamut
|
|
787 |
if (dE1 > t->Thereshold && dE2 < t->Thereshold)
|
|
788 |
Out[0] = (WORD) _cmsQuickFloor((dE1 - t->Thereshold) + .5);
|
|
789 |
else {
|
|
790 |
|
|
791 |
// dE1 is big and dE2 is also big, could be due to perceptual mapping
|
|
792 |
// so take error ratio
|
|
793 |
if (dE2 == 0.0)
|
|
794 |
ErrorRatio = dE1;
|
|
795 |
else
|
|
796 |
ErrorRatio = dE1 / dE2;
|
|
797 |
|
|
798 |
if (ErrorRatio > t->Thereshold)
|
|
799 |
Out[0] = (WORD) _cmsQuickFloor((ErrorRatio - t->Thereshold) + .5);
|
|
800 |
else
|
|
801 |
Out[0] = 0;
|
|
802 |
}
|
|
803 |
|
|
804 |
}
|
|
805 |
|
|
806 |
return TRUE;
|
|
807 |
}
|
|
808 |
|
|
809 |
|
|
810 |
// Does compute a gamut LUT going back and forth across
|
|
811 |
// pcs -> relativ. colorimetric intent -> pcs
|
|
812 |
// the dE obtained is then annotated on the LUT.
|
|
813 |
// values truely out of gamut, are clipped to dE = 0xFFFE
|
|
814 |
// and values changed are supposed to be handled by
|
|
815 |
// any gamut remapping, so, are out of gamut as well.
|
|
816 |
//
|
|
817 |
// **WARNING: This algorithm does assume that gamut
|
|
818 |
// remapping algorithms does NOT move in-gamut colors,
|
|
819 |
// of course, many perceptual and saturation intents does
|
|
820 |
// not work in such way, but relativ. ones should.
|
|
821 |
|
|
822 |
static
|
|
823 |
LPLUT ComputeGamutWithInput(cmsHPROFILE hInput, cmsHPROFILE hProfile, int Intent)
|
|
824 |
{
|
|
825 |
cmsHPROFILE hLab;
|
|
826 |
LPLUT Gamut;
|
|
827 |
DWORD dwFormat;
|
|
828 |
GAMUTCHAIN Chain;
|
|
829 |
int nErrState, nChannels, nGridpoints;
|
|
830 |
LPGAMMATABLE Trans[3];
|
|
831 |
icColorSpaceSignature ColorSpace;
|
|
832 |
|
|
833 |
|
|
834 |
ZeroMemory(&Chain, sizeof(GAMUTCHAIN));
|
|
835 |
|
|
836 |
hLab = cmsCreateLabProfile(NULL);
|
|
837 |
|
|
838 |
// Safeguard against early abortion
|
|
839 |
nErrState = cmsErrorAction(LCMS_ERROR_IGNORE);
|
|
840 |
|
|
841 |
// The figure of merit. On matrix-shaper profiles, should be almost zero as
|
|
842 |
// the conversion is pretty exact. On LUT based profiles, different resolutions
|
|
843 |
// of input and output CLUT may result in differences.
|
|
844 |
|
|
845 |
if (!cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
|
|
846 |
!cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_OUTPUT))
|
|
847 |
|
|
848 |
Chain.Thereshold = 1.0;
|
|
849 |
else
|
|
850 |
Chain.Thereshold = ERR_THERESHOLD;
|
|
851 |
|
|
852 |
ColorSpace = cmsGetColorSpace(hProfile);
|
|
853 |
|
|
854 |
// If input profile specified, create a transform from such profile to Lab
|
|
855 |
if (hInput != NULL) {
|
|
856 |
|
|
857 |
nChannels = _cmsChannelsOf(ColorSpace);
|
|
858 |
nGridpoints = _cmsReasonableGridpointsByColorspace(ColorSpace, cmsFLAGS_HIGHRESPRECALC);
|
|
859 |
dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2));
|
|
860 |
|
|
861 |
Chain.hInput = cmsCreateTransform(hInput, dwFormat,
|
|
862 |
hLab, TYPE_Lab_16,
|
|
863 |
Intent,
|
|
864 |
cmsFLAGS_NOTPRECALC);
|
|
865 |
}
|
|
866 |
else {
|
|
867 |
// Input transform=NULL (Lab) Used to compute the gamut tag
|
|
868 |
// This table will take 53 points to give some accurancy,
|
|
869 |
// 53 * 53 * 53 * 2 = 291K
|
|
870 |
|
|
871 |
nChannels = 3; // For Lab
|
|
872 |
nGridpoints = 53;
|
|
873 |
Chain.hInput = NULL;
|
|
874 |
dwFormat = (CHANNELS_SH(_cmsChannelsOf(ColorSpace))|BYTES_SH(2));
|
|
875 |
}
|
|
876 |
|
|
877 |
|
|
878 |
// Does create the forward step
|
|
879 |
Chain.hForward = cmsCreateTransform(hLab, TYPE_Lab_16,
|
|
880 |
hProfile, dwFormat,
|
|
881 |
INTENT_RELATIVE_COLORIMETRIC,
|
|
882 |
cmsFLAGS_NOTPRECALC);
|
|
883 |
|
|
884 |
// Does create the backwards step
|
|
885 |
Chain.hReverse = cmsCreateTransform(hProfile, dwFormat,
|
|
886 |
hLab, TYPE_Lab_16,
|
|
887 |
INTENT_RELATIVE_COLORIMETRIC,
|
|
888 |
cmsFLAGS_NOTPRECALC);
|
|
889 |
|
|
890 |
// Restores error handler previous state
|
|
891 |
cmsErrorAction(nErrState);
|
|
892 |
|
|
893 |
|
|
894 |
// All ok?
|
|
895 |
if (Chain.hForward && Chain.hReverse) {
|
|
896 |
|
|
897 |
// Go on, try to compute gamut LUT from PCS.
|
|
898 |
// This consist on a single channel containing
|
|
899 |
// dE when doing a transform back and forth on
|
|
900 |
// the colorimetric intent.
|
|
901 |
|
|
902 |
Gamut = cmsAllocLUT();
|
|
903 |
Gamut = cmsAlloc3DGrid(Gamut, nGridpoints, nChannels, 1);
|
|
904 |
|
|
905 |
// If no input, then this is a gamut tag operated by Lab,
|
|
906 |
// so include pertinent prelinearization
|
|
907 |
if (hInput == NULL) {
|
|
908 |
|
|
909 |
CreateLabPrelinearization(Trans);
|
|
910 |
cmsAllocLinearTable(Gamut, Trans, 1);
|
|
911 |
cmsFreeGammaTriple(Trans);
|
|
912 |
}
|
|
913 |
|
|
914 |
|
|
915 |
cmsSample3DGrid(Gamut, GamutSampler, (LPVOID) &Chain, Gamut ->wFlags);
|
|
916 |
}
|
|
917 |
else
|
|
918 |
Gamut = NULL; // Didn't work...
|
|
919 |
|
|
920 |
// Free all needed stuff.
|
|
921 |
if (Chain.hInput) cmsDeleteTransform(Chain.hInput);
|
|
922 |
if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
|
|
923 |
if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
|
|
924 |
|
|
925 |
cmsCloseProfile(hLab);
|
|
926 |
|
|
927 |
// And return computed hull
|
|
928 |
return Gamut;
|
|
929 |
}
|
|
930 |
|
|
931 |
|
|
932 |
// Wrapper
|
|
933 |
|
|
934 |
LPLUT _cmsComputeGamutLUT(cmsHPROFILE hProfile, int Intent)
|
|
935 |
{
|
|
936 |
return ComputeGamutWithInput(NULL, hProfile, Intent);
|
|
937 |
}
|
|
938 |
|
|
939 |
|
|
940 |
// This routine does compute the gamut check CLUT. This CLUT goes from whatever
|
|
941 |
// input space to the 0 or != 0 gamut check.
|
|
942 |
|
|
943 |
LPLUT _cmsPrecalculateGamutCheck(cmsHTRANSFORM h)
|
|
944 |
{
|
|
945 |
_LPcmsTRANSFORM p = (_LPcmsTRANSFORM) h;
|
|
946 |
|
|
947 |
return ComputeGamutWithInput(p->InputProfile, p ->PreviewProfile, p->Intent);
|
|
948 |
}
|
|
949 |
|
|
950 |
|
|
951 |
// SoftProofing. Convert from Lab to device, then back to Lab,
|
|
952 |
// any gamut remapping is applied
|
|
953 |
|
|
954 |
static
|
|
955 |
int SoftProofSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
956 |
{
|
|
957 |
LPGAMUTCHAIN t = (LPGAMUTCHAIN) Cargo;
|
|
958 |
WORD Colorant[MAXCHANNELS];
|
|
959 |
|
|
960 |
// From pcs to colorant
|
|
961 |
cmsDoTransform(t -> hForward, In, Colorant, 1);
|
|
962 |
|
|
963 |
// Now, do the inverse, from colorant to pcs.
|
|
964 |
cmsDoTransform(t -> hReverse, Colorant, Out, 1);
|
|
965 |
|
|
966 |
return TRUE;
|
|
967 |
}
|
|
968 |
|
|
969 |
// Does return Softproofing LUT on desired intent
|
|
970 |
|
|
971 |
LPLUT _cmsComputeSoftProofLUT(cmsHPROFILE hProfile, int nIntent)
|
|
972 |
{
|
|
973 |
cmsHPROFILE hLab;
|
|
974 |
LPLUT SoftProof;
|
|
975 |
DWORD dwFormat;
|
|
976 |
GAMUTCHAIN Chain;
|
|
977 |
int nErrState;
|
|
978 |
LPGAMMATABLE Trans[3];
|
|
979 |
|
|
980 |
|
|
981 |
// LUTs are never abs. colorimetric, is the transform who
|
|
982 |
// is responsible of generating white point displacement
|
|
983 |
if (nIntent == INTENT_ABSOLUTE_COLORIMETRIC)
|
|
984 |
nIntent = INTENT_RELATIVE_COLORIMETRIC;
|
|
985 |
|
|
986 |
ZeroMemory(&Chain, sizeof(GAMUTCHAIN));
|
|
987 |
|
|
988 |
hLab = cmsCreateLabProfile(NULL);
|
|
989 |
|
|
990 |
// ONLY 4 channels
|
|
991 |
dwFormat = (CHANNELS_SH(4)|BYTES_SH(2));
|
|
992 |
|
|
993 |
// Safeguard against early abortion
|
|
994 |
nErrState = cmsErrorAction(LCMS_ERROR_IGNORE);
|
|
995 |
|
|
996 |
// Does create the first step
|
|
997 |
Chain.hForward = cmsCreateTransform(hLab, TYPE_Lab_16,
|
|
998 |
hProfile, dwFormat,
|
|
999 |
nIntent,
|
|
1000 |
cmsFLAGS_NOTPRECALC);
|
|
1001 |
|
|
1002 |
// Does create the last step
|
|
1003 |
Chain.hReverse = cmsCreateTransform(hProfile, dwFormat,
|
|
1004 |
hLab, TYPE_Lab_16,
|
|
1005 |
INTENT_RELATIVE_COLORIMETRIC,
|
|
1006 |
cmsFLAGS_NOTPRECALC);
|
|
1007 |
|
|
1008 |
// Restores error handler previous state
|
|
1009 |
cmsErrorAction(nErrState);
|
|
1010 |
|
|
1011 |
// All ok?
|
|
1012 |
if (Chain.hForward && Chain.hReverse) {
|
|
1013 |
|
|
1014 |
// This is Lab -> Lab, so 33 point should hold anything
|
|
1015 |
SoftProof = cmsAllocLUT();
|
|
1016 |
SoftProof = cmsAlloc3DGrid(SoftProof, 33, 3, 3);
|
|
1017 |
|
|
1018 |
CreateLabPrelinearization(Trans);
|
|
1019 |
cmsAllocLinearTable(SoftProof, Trans, 1);
|
|
1020 |
cmsFreeGammaTriple(Trans);
|
|
1021 |
|
|
1022 |
cmsSample3DGrid(SoftProof, SoftProofSampler, (LPVOID) &Chain, SoftProof->wFlags);
|
|
1023 |
}
|
|
1024 |
else
|
|
1025 |
SoftProof = NULL; // Didn't work...
|
|
1026 |
|
|
1027 |
// Free all needed stuff.
|
|
1028 |
if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
|
|
1029 |
if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
|
|
1030 |
|
|
1031 |
cmsCloseProfile(hLab);
|
|
1032 |
|
|
1033 |
return SoftProof;
|
|
1034 |
}
|
|
1035 |
|
|
1036 |
|
|
1037 |
static
|
|
1038 |
int MostlyLinear(WORD Table[], int nEntries)
|
|
1039 |
{
|
|
1040 |
register int i;
|
|
1041 |
int diff;
|
|
1042 |
|
|
1043 |
for (i=5; i < nEntries; i++) {
|
|
1044 |
|
|
1045 |
diff = abs((int) Table[i] - (int) _cmsQuantizeVal(i, nEntries));
|
|
1046 |
if (diff > 0x0300)
|
|
1047 |
return 0;
|
|
1048 |
}
|
|
1049 |
|
|
1050 |
return 1;
|
|
1051 |
}
|
|
1052 |
|
|
1053 |
|
|
1054 |
static
|
|
1055 |
void SlopeLimiting(WORD Table[], int nEntries)
|
|
1056 |
{
|
|
1057 |
int At = (int) floor((double) nEntries * 0.02 + 0.5); // Cutoff at 2%
|
|
1058 |
double Val, Slope;
|
|
1059 |
int i;
|
|
1060 |
|
|
1061 |
Val = Table[At];
|
|
1062 |
Slope = Val / At;
|
|
1063 |
|
|
1064 |
for (i=0; i < At; i++)
|
|
1065 |
Table[i] = (WORD) floor(i * Slope + 0.5);
|
|
1066 |
|
|
1067 |
}
|
|
1068 |
|
|
1069 |
|
|
1070 |
// Check for monotonicity.
|
|
1071 |
|
|
1072 |
static
|
2394
|
1073 |
LCMSBOOL IsMonotonic(LPGAMMATABLE t)
|
2
|
1074 |
{
|
|
1075 |
int n = t -> nEntries;
|
|
1076 |
int i, last;
|
|
1077 |
|
|
1078 |
last = t ->GammaTable[n-1];
|
|
1079 |
|
|
1080 |
for (i = n-2; i >= 0; --i) {
|
|
1081 |
|
|
1082 |
if (t ->GammaTable[i] > last)
|
|
1083 |
|
|
1084 |
return FALSE;
|
|
1085 |
else
|
|
1086 |
last = t ->GammaTable[i];
|
|
1087 |
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
return TRUE;
|
|
1091 |
}
|
|
1092 |
|
|
1093 |
// Check for endpoints
|
|
1094 |
|
|
1095 |
static
|
2394
|
1096 |
LCMSBOOL HasProperEndpoints(LPGAMMATABLE t)
|
2
|
1097 |
{
|
|
1098 |
if (t ->GammaTable[0] != 0) return FALSE;
|
|
1099 |
if (t ->GammaTable[t ->nEntries-1] != 0xFFFF) return FALSE;
|
|
1100 |
|
|
1101 |
return TRUE;
|
|
1102 |
}
|
|
1103 |
|
|
1104 |
|
|
1105 |
|
|
1106 |
#define PRELINEARIZATION_POINTS 4096
|
|
1107 |
|
|
1108 |
// Fixes the gamma balancing of transform. Thanks to Mike Chaney
|
|
1109 |
// for pointing this subtle bug.
|
|
1110 |
|
|
1111 |
void _cmsComputePrelinearizationTablesFromXFORM(cmsHTRANSFORM h[], int nTransforms, LPLUT Grid)
|
|
1112 |
{
|
|
1113 |
LPGAMMATABLE Trans[MAXCHANNELS];
|
|
1114 |
unsigned int t, i, v;
|
|
1115 |
int j;
|
|
1116 |
WORD In[MAXCHANNELS], Out[MAXCHANNELS];
|
2394
|
1117 |
LCMSBOOL lIsSuitable;
|
2
|
1118 |
_LPcmsTRANSFORM InputXForm = (_LPcmsTRANSFORM) h[0];
|
|
1119 |
_LPcmsTRANSFORM OutputXForm = (_LPcmsTRANSFORM) h[nTransforms-1];
|
|
1120 |
|
|
1121 |
|
|
1122 |
// First space is *Lab, use our specialized curves for v2 Lab
|
|
1123 |
|
|
1124 |
if (InputXForm ->EntryColorSpace == icSigLabData &&
|
|
1125 |
OutputXForm->ExitColorSpace != icSigLabData) {
|
|
1126 |
|
|
1127 |
CreateLabPrelinearization(Trans);
|
|
1128 |
cmsAllocLinearTable(Grid, Trans, 1);
|
|
1129 |
cmsFreeGammaTriple(Trans);
|
|
1130 |
return;
|
|
1131 |
}
|
|
1132 |
|
|
1133 |
|
2394
|
1134 |
// Do nothing on all but Gray/RGB to Gray/RGB transforms
|
2
|
1135 |
|
2394
|
1136 |
if (((InputXForm ->EntryColorSpace != icSigRgbData) && (InputXForm ->EntryColorSpace != icSigGrayData)) ||
|
|
1137 |
((OutputXForm->ExitColorSpace != icSigRgbData) && (OutputXForm->ExitColorSpace != icSigGrayData))) return;
|
2
|
1138 |
|
|
1139 |
|
|
1140 |
for (t = 0; t < Grid -> InputChan; t++)
|
|
1141 |
Trans[t] = cmsAllocGamma(PRELINEARIZATION_POINTS);
|
|
1142 |
|
|
1143 |
for (i=0; i < PRELINEARIZATION_POINTS; i++) {
|
|
1144 |
|
|
1145 |
v = _cmsQuantizeVal(i, PRELINEARIZATION_POINTS);
|
|
1146 |
|
|
1147 |
for (t=0; t < Grid -> InputChan; t++)
|
|
1148 |
In[t] = (WORD) v;
|
|
1149 |
|
|
1150 |
cmsDoTransform(h[0], In, Out, 1);
|
|
1151 |
for (j=1; j < nTransforms; j++)
|
|
1152 |
cmsDoTransform(h[j], Out, Out, 1);
|
|
1153 |
|
|
1154 |
for (t=0; t < Grid -> InputChan; t++)
|
|
1155 |
Trans[t] ->GammaTable[i] = Out[t];
|
|
1156 |
|
|
1157 |
}
|
|
1158 |
|
|
1159 |
|
|
1160 |
// Check transfer curves
|
|
1161 |
lIsSuitable = TRUE;
|
|
1162 |
for (t=0; (lIsSuitable && (t < Grid->InputChan)); t++) {
|
|
1163 |
|
|
1164 |
|
|
1165 |
// Exclude if already linear
|
|
1166 |
if (MostlyLinear(Trans[t]->GammaTable, PRELINEARIZATION_POINTS))
|
|
1167 |
lIsSuitable = FALSE;
|
|
1168 |
|
|
1169 |
// Exclude if non-monotonic
|
|
1170 |
if (!IsMonotonic(Trans[t]))
|
|
1171 |
lIsSuitable = FALSE;
|
|
1172 |
|
|
1173 |
// Exclude if weird endpoints
|
|
1174 |
if (!HasProperEndpoints(Trans[t]))
|
|
1175 |
lIsSuitable = FALSE;
|
|
1176 |
|
2394
|
1177 |
/*
|
2
|
1178 |
// Exclude if transfer function is not smooth enough
|
|
1179 |
// to be modelled as a gamma function, or the gamma is reversed
|
2394
|
1180 |
|
2
|
1181 |
if (cmsEstimateGamma(Trans[t]) < 1.0)
|
|
1182 |
lIsSuitable = FALSE;
|
2394
|
1183 |
*/
|
2
|
1184 |
|
|
1185 |
}
|
|
1186 |
|
|
1187 |
if (lIsSuitable) {
|
|
1188 |
|
|
1189 |
for (t = 0; t < Grid ->InputChan; t++)
|
|
1190 |
SlopeLimiting(Trans[t]->GammaTable, Trans[t]->nEntries);
|
|
1191 |
}
|
|
1192 |
|
|
1193 |
if (lIsSuitable) cmsAllocLinearTable(Grid, Trans, 1);
|
|
1194 |
|
|
1195 |
|
|
1196 |
for (t = 0; t < Grid ->InputChan; t++)
|
|
1197 |
cmsFreeGamma(Trans[t]);
|
|
1198 |
|
|
1199 |
|
|
1200 |
}
|
|
1201 |
|
|
1202 |
|
|
1203 |
// Compute K -> L* relationship. Flags may include black point compensation. In this case,
|
|
1204 |
// the relationship is assumed from the profile with BPC to a black point zero.
|
|
1205 |
static
|
|
1206 |
LPGAMMATABLE ComputeKToLstar(cmsHPROFILE hProfile, int nPoints, int Intent, DWORD dwFlags)
|
|
1207 |
{
|
|
1208 |
LPGAMMATABLE out;
|
|
1209 |
int i;
|
|
1210 |
WORD cmyk[4], wLab[3];
|
|
1211 |
cmsHPROFILE hLab = cmsCreateLabProfile(NULL);
|
|
1212 |
cmsHTRANSFORM xform = cmsCreateTransform(hProfile, TYPE_CMYK_16,
|
|
1213 |
hLab, TYPE_Lab_16,
|
|
1214 |
Intent, (dwFlags|cmsFLAGS_NOTPRECALC));
|
|
1215 |
|
|
1216 |
|
|
1217 |
out = cmsAllocGamma(nPoints);
|
|
1218 |
for (i=0; i < nPoints; i++) {
|
|
1219 |
|
|
1220 |
cmyk[0] = 0;
|
|
1221 |
cmyk[1] = 0;
|
|
1222 |
cmyk[2] = 0;
|
|
1223 |
cmyk[3] = _cmsQuantizeVal(i, nPoints);
|
|
1224 |
|
|
1225 |
cmsDoTransform(xform, cmyk, wLab, 1);
|
|
1226 |
out->GammaTable[i] = (WORD) (0xFFFF - wLab[0]);
|
|
1227 |
}
|
|
1228 |
|
|
1229 |
cmsDeleteTransform(xform);
|
|
1230 |
cmsCloseProfile(hLab);
|
|
1231 |
|
|
1232 |
return out;
|
|
1233 |
}
|
|
1234 |
|
|
1235 |
|
|
1236 |
|
|
1237 |
// Compute Black tone curve on a CMYK -> CMYK transform. This is done by
|
|
1238 |
// using the proof direction on both profiles to find K->L* relationship
|
|
1239 |
// then joining both curves. dwFlags may include black point compensation.
|
|
1240 |
|
|
1241 |
LPGAMMATABLE _cmsBuildKToneCurve(cmsHTRANSFORM hCMYK2CMYK, int nPoints)
|
|
1242 |
{
|
|
1243 |
LPGAMMATABLE in, out;
|
|
1244 |
LPGAMMATABLE KTone;
|
|
1245 |
_LPcmsTRANSFORM p = (_LPcmsTRANSFORM) hCMYK2CMYK;
|
|
1246 |
|
|
1247 |
|
|
1248 |
// Make sure CMYK -> CMYK
|
|
1249 |
if (p -> EntryColorSpace != icSigCmykData ||
|
|
1250 |
p -> ExitColorSpace != icSigCmykData) return NULL;
|
|
1251 |
|
|
1252 |
// Create individual curves. BPC works also as each K to L* is
|
|
1253 |
// computed as a BPC to zero black point in case of L*
|
|
1254 |
in = ComputeKToLstar(p ->InputProfile, nPoints, p->Intent, p -> dwOriginalFlags);
|
|
1255 |
out = ComputeKToLstar(p ->OutputProfile, nPoints, p->Intent, p -> dwOriginalFlags);
|
|
1256 |
|
|
1257 |
// Build the relationship
|
|
1258 |
KTone = cmsJoinGamma(in, out);
|
|
1259 |
|
|
1260 |
cmsFreeGamma(in); cmsFreeGamma(out);
|
|
1261 |
|
|
1262 |
// Make sure it is monotonic
|
|
1263 |
|
|
1264 |
if (!IsMonotonic(KTone)) {
|
|
1265 |
|
|
1266 |
cmsFreeGamma(KTone);
|
|
1267 |
return NULL;
|
|
1268 |
}
|
|
1269 |
|
|
1270 |
|
|
1271 |
return KTone;
|
|
1272 |
}
|