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
|
|
32 |
// Copyright (C) 1998-2006 Marti Maria
|
|
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 |
#include "lcms.h"
|
|
53 |
|
|
54 |
// Pipeline of LUT. Enclosed by {} are new revision 4.0 of ICC spec.
|
|
55 |
//
|
|
56 |
// [Mat] -> [L1] -> { [Mat3] -> [Ofs3] -> [L3] ->} [CLUT] { -> [L4] -> [Mat4] -> [Ofs4] } -> [L2]
|
|
57 |
//
|
|
58 |
// Some of these stages would be missing. This implements the totality of
|
|
59 |
// combinations of old and new LUT types as follows:
|
|
60 |
//
|
|
61 |
// Lut8 & Lut16
|
|
62 |
// ============
|
|
63 |
// [Mat] -> [L1] -> [CLUT] -> [L2]
|
|
64 |
//
|
|
65 |
// Mat2, Ofs2, L3, L3, Mat3, Ofs3 are missing
|
|
66 |
//
|
|
67 |
// LutAToB
|
|
68 |
// ========
|
|
69 |
//
|
|
70 |
// [L1] -> [CLUT] -> [L4] -> [Mat4] -> [Ofs4] -> [L2]
|
|
71 |
//
|
|
72 |
// Mat, Mat3, Ofs3, L3 are missing
|
|
73 |
// L1 = A curves
|
|
74 |
// L4 = M curves
|
|
75 |
// L2 = B curves
|
|
76 |
//
|
|
77 |
// LutBToA
|
|
78 |
// =======
|
|
79 |
//
|
|
80 |
// [L1] -> [Mat3] -> [Ofs3] -> [L3] -> [CLUT] -> [L2]
|
|
81 |
//
|
|
82 |
// Mat, L4, Mat4, Ofs4 are missing
|
|
83 |
// L1 = B Curves
|
|
84 |
// L3 = M Curves
|
|
85 |
// L2 = A curves
|
|
86 |
//
|
|
87 |
//
|
|
88 |
// V2&3 emulation
|
|
89 |
// ===============
|
|
90 |
//
|
|
91 |
// For output, Mat is multiplied by
|
|
92 |
//
|
|
93 |
//
|
|
94 |
// | 0xff00 / 0xffff 0 0 |
|
|
95 |
// | 0 0xff00 / 0xffff 0 |
|
|
96 |
// | 0 0 0xff00 / 0xffff |
|
|
97 |
//
|
|
98 |
//
|
|
99 |
// For input, an additional matrix is needed at the very last end of the chain
|
|
100 |
//
|
|
101 |
//
|
|
102 |
// | 0xffff / 0xff00 0 0 |
|
|
103 |
// | 0 0xffff / 0xff00 0 |
|
|
104 |
// | 0 0 0xffff / 0xff00 |
|
|
105 |
//
|
|
106 |
//
|
|
107 |
// Which reduces to (val * 257) >> 8
|
|
108 |
|
|
109 |
// A couple of macros to convert between revisions
|
|
110 |
|
|
111 |
#define FROM_V2_TO_V4(x) (((((x)<<8)+(x))+0x80)>>8) // BY 65535 DIV 65280 ROUND
|
|
112 |
#define FROM_V4_TO_V2(x) ((((x)<<8)+0x80)/257) // BY 65280 DIV 65535 ROUND
|
|
113 |
|
|
114 |
|
|
115 |
// Lut Creation & Destruction
|
|
116 |
|
|
117 |
LPLUT LCMSEXPORT cmsAllocLUT(void)
|
|
118 |
{
|
|
119 |
LPLUT NewLUT;
|
|
120 |
|
|
121 |
NewLUT = (LPLUT) malloc(sizeof(LUT));
|
|
122 |
if (NewLUT)
|
|
123 |
ZeroMemory(NewLUT, sizeof(LUT));
|
|
124 |
|
|
125 |
return NewLUT;
|
|
126 |
}
|
|
127 |
|
|
128 |
void LCMSEXPORT cmsFreeLUT(LPLUT Lut)
|
|
129 |
{
|
|
130 |
unsigned int i;
|
|
131 |
|
|
132 |
if (!Lut) return;
|
|
133 |
|
|
134 |
if (Lut -> T) free(Lut -> T);
|
|
135 |
|
|
136 |
for (i=0; i < Lut -> OutputChan; i++)
|
|
137 |
{
|
|
138 |
if (Lut -> L2[i]) free(Lut -> L2[i]);
|
|
139 |
}
|
|
140 |
|
|
141 |
for (i=0; i < Lut -> InputChan; i++)
|
|
142 |
{
|
|
143 |
|
|
144 |
if (Lut -> L1[i]) free(Lut -> L1[i]);
|
|
145 |
}
|
|
146 |
|
|
147 |
|
|
148 |
if (Lut ->wFlags & LUT_HASTL3) {
|
|
149 |
|
|
150 |
for (i=0; i < Lut -> InputChan; i++) {
|
|
151 |
|
|
152 |
if (Lut -> L3[i]) free(Lut -> L3[i]);
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
if (Lut ->wFlags & LUT_HASTL4) {
|
|
157 |
|
|
158 |
for (i=0; i < Lut -> OutputChan; i++) {
|
|
159 |
|
|
160 |
if (Lut -> L4[i]) free(Lut -> L4[i]);
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
if (Lut ->CLut16params.p8)
|
|
165 |
free(Lut ->CLut16params.p8);
|
|
166 |
|
|
167 |
free(Lut);
|
|
168 |
}
|
|
169 |
|
|
170 |
|
|
171 |
static
|
|
172 |
LPVOID DupBlockTab(LPVOID Org, size_t size)
|
|
173 |
{
|
|
174 |
LPVOID mem = malloc(size);
|
|
175 |
|
|
176 |
CopyMemory(mem, Org, size);
|
|
177 |
return mem;
|
|
178 |
}
|
|
179 |
|
|
180 |
|
|
181 |
LPLUT LCMSEXPORT cmsDupLUT(LPLUT Orig)
|
|
182 |
{
|
|
183 |
LPLUT NewLUT = cmsAllocLUT();
|
|
184 |
unsigned int i;
|
|
185 |
|
|
186 |
CopyMemory(NewLUT, Orig, sizeof(LUT));
|
|
187 |
|
|
188 |
for (i=0; i < Orig ->InputChan; i++)
|
|
189 |
NewLUT -> L1[i] = (LPWORD) DupBlockTab((LPVOID) Orig ->L1[i],
|
|
190 |
sizeof(WORD) * Orig ->In16params.nSamples);
|
|
191 |
|
|
192 |
for (i=0; i < Orig ->OutputChan; i++)
|
|
193 |
NewLUT -> L2[i] = (LPWORD) DupBlockTab((LPVOID) Orig ->L2[i],
|
|
194 |
sizeof(WORD) * Orig ->Out16params.nSamples);
|
|
195 |
|
|
196 |
NewLUT -> T = (LPWORD) DupBlockTab((LPVOID) Orig ->T, Orig -> Tsize);
|
|
197 |
|
|
198 |
return NewLUT;
|
|
199 |
}
|
|
200 |
|
|
201 |
|
|
202 |
static
|
|
203 |
unsigned int UIpow(unsigned int a, unsigned int b)
|
|
204 |
{
|
|
205 |
unsigned int rv = 1;
|
|
206 |
|
|
207 |
for (; b > 0; b--)
|
|
208 |
rv *= a;
|
|
209 |
|
|
210 |
return rv;
|
|
211 |
}
|
|
212 |
|
|
213 |
|
|
214 |
LPLUT LCMSEXPORT cmsAlloc3DGrid(LPLUT NewLUT, int clutPoints, int inputChan, int outputChan)
|
|
215 |
{
|
|
216 |
DWORD nTabSize;
|
|
217 |
|
|
218 |
NewLUT -> wFlags |= LUT_HAS3DGRID;
|
|
219 |
NewLUT -> cLutPoints = clutPoints;
|
|
220 |
NewLUT -> InputChan = inputChan;
|
|
221 |
NewLUT -> OutputChan = outputChan;
|
|
222 |
|
|
223 |
|
|
224 |
nTabSize = (NewLUT -> OutputChan * UIpow(NewLUT->cLutPoints,
|
|
225 |
NewLUT->InputChan)
|
|
226 |
* sizeof(WORD));
|
|
227 |
|
|
228 |
NewLUT -> T = (LPWORD) malloc(nTabSize);
|
|
229 |
ZeroMemory(NewLUT -> T, nTabSize);
|
|
230 |
NewLUT ->Tsize = nTabSize;
|
|
231 |
|
|
232 |
|
|
233 |
cmsCalcCLUT16Params(NewLUT -> cLutPoints, NewLUT -> InputChan,
|
|
234 |
NewLUT -> OutputChan,
|
|
235 |
&NewLUT -> CLut16params);
|
|
236 |
|
|
237 |
return NewLUT;
|
|
238 |
}
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
LPLUT LCMSEXPORT cmsAllocLinearTable(LPLUT NewLUT, LPGAMMATABLE Tables[], int nTable)
|
|
244 |
{
|
|
245 |
unsigned int i;
|
|
246 |
LPWORD PtrW;
|
|
247 |
|
|
248 |
switch (nTable) {
|
|
249 |
|
|
250 |
|
|
251 |
case 1: NewLUT -> wFlags |= LUT_HASTL1;
|
|
252 |
cmsCalcL16Params(Tables[0] -> nEntries, &NewLUT -> In16params);
|
|
253 |
NewLUT -> InputEntries = Tables[0] -> nEntries;
|
|
254 |
|
|
255 |
for (i=0; i < NewLUT -> InputChan; i++) {
|
|
256 |
|
|
257 |
PtrW = (LPWORD) malloc(sizeof(WORD) * NewLUT -> InputEntries);
|
|
258 |
NewLUT -> L1[i] = PtrW;
|
|
259 |
CopyMemory(PtrW, Tables[i]->GammaTable, sizeof(WORD) * NewLUT -> InputEntries);
|
|
260 |
CopyMemory(&NewLUT -> LCurvesSeed[0][i], &Tables[i] -> Seed, sizeof(LCMSGAMMAPARAMS));
|
|
261 |
}
|
|
262 |
|
|
263 |
|
|
264 |
break;
|
|
265 |
|
|
266 |
case 2: NewLUT -> wFlags |= LUT_HASTL2;
|
|
267 |
cmsCalcL16Params(Tables[0] -> nEntries, &NewLUT -> Out16params);
|
|
268 |
NewLUT -> OutputEntries = Tables[0] -> nEntries;
|
|
269 |
for (i=0; i < NewLUT -> OutputChan; i++) {
|
|
270 |
|
|
271 |
PtrW = (LPWORD) malloc(sizeof(WORD) * NewLUT -> OutputEntries);
|
|
272 |
NewLUT -> L2[i] = PtrW;
|
|
273 |
CopyMemory(PtrW, Tables[i]->GammaTable, sizeof(WORD) * NewLUT -> OutputEntries);
|
|
274 |
CopyMemory(&NewLUT -> LCurvesSeed[1][i], &Tables[i] -> Seed, sizeof(LCMSGAMMAPARAMS));
|
|
275 |
}
|
|
276 |
break;
|
|
277 |
|
|
278 |
|
|
279 |
// 3 & 4 according ICC 4.0 spec
|
|
280 |
|
|
281 |
case 3:
|
|
282 |
NewLUT -> wFlags |= LUT_HASTL3;
|
|
283 |
cmsCalcL16Params(Tables[0] -> nEntries, &NewLUT -> L3params);
|
|
284 |
NewLUT -> L3Entries = Tables[0] -> nEntries;
|
|
285 |
|
|
286 |
for (i=0; i < NewLUT -> InputChan; i++) {
|
|
287 |
|
|
288 |
PtrW = (LPWORD) malloc(sizeof(WORD) * NewLUT -> L3Entries);
|
|
289 |
NewLUT -> L3[i] = PtrW;
|
|
290 |
CopyMemory(PtrW, Tables[i]->GammaTable, sizeof(WORD) * NewLUT -> L3Entries);
|
|
291 |
CopyMemory(&NewLUT -> LCurvesSeed[2][i], &Tables[i] -> Seed, sizeof(LCMSGAMMAPARAMS));
|
|
292 |
}
|
|
293 |
break;
|
|
294 |
|
|
295 |
case 4:
|
|
296 |
NewLUT -> wFlags |= LUT_HASTL4;
|
|
297 |
cmsCalcL16Params(Tables[0] -> nEntries, &NewLUT -> L4params);
|
|
298 |
NewLUT -> L4Entries = Tables[0] -> nEntries;
|
|
299 |
for (i=0; i < NewLUT -> OutputChan; i++) {
|
|
300 |
|
|
301 |
PtrW = (LPWORD) malloc(sizeof(WORD) * NewLUT -> L4Entries);
|
|
302 |
NewLUT -> L4[i] = PtrW;
|
|
303 |
CopyMemory(PtrW, Tables[i]->GammaTable, sizeof(WORD) * NewLUT -> L4Entries);
|
|
304 |
CopyMemory(&NewLUT -> LCurvesSeed[3][i], &Tables[i] -> Seed, sizeof(LCMSGAMMAPARAMS));
|
|
305 |
}
|
|
306 |
break;
|
|
307 |
|
|
308 |
|
|
309 |
default:;
|
|
310 |
}
|
|
311 |
|
|
312 |
return NewLUT;
|
|
313 |
}
|
|
314 |
|
|
315 |
|
|
316 |
// Set the LUT matrix
|
|
317 |
|
|
318 |
LPLUT LCMSEXPORT cmsSetMatrixLUT(LPLUT Lut, LPMAT3 M)
|
|
319 |
{
|
|
320 |
MAT3toFix(&Lut ->Matrix, M);
|
|
321 |
|
|
322 |
if (!MAT3isIdentity(&Lut->Matrix, 0.0001))
|
|
323 |
Lut ->wFlags |= LUT_HASMATRIX;
|
|
324 |
|
|
325 |
return Lut;
|
|
326 |
}
|
|
327 |
|
|
328 |
|
|
329 |
// Set matrix & offset, v4 compatible
|
|
330 |
|
|
331 |
LPLUT LCMSEXPORT cmsSetMatrixLUT4(LPLUT Lut, LPMAT3 M, LPVEC3 off, DWORD dwFlags)
|
|
332 |
{
|
|
333 |
WMAT3 WMat;
|
|
334 |
WVEC3 Woff;
|
|
335 |
VEC3 Zero = {{0, 0, 0}};
|
|
336 |
|
|
337 |
MAT3toFix(&WMat, M);
|
|
338 |
|
|
339 |
if (off == NULL)
|
|
340 |
off = &Zero;
|
|
341 |
|
|
342 |
VEC3toFix(&Woff, off);
|
|
343 |
|
|
344 |
// Nop if identity
|
|
345 |
if (MAT3isIdentity(&WMat, 0.0001) &&
|
|
346 |
(Woff.n[VX] == 0 && Woff.n[VY] == 0 && Woff.n[VZ] == 0))
|
|
347 |
return Lut;
|
|
348 |
|
|
349 |
switch (dwFlags) {
|
|
350 |
|
|
351 |
case LUT_HASMATRIX:
|
|
352 |
Lut ->Matrix = WMat;
|
|
353 |
Lut ->wFlags |= LUT_HASMATRIX;
|
|
354 |
break;
|
|
355 |
|
|
356 |
case LUT_HASMATRIX3:
|
|
357 |
Lut ->Mat3 = WMat;
|
|
358 |
Lut ->Ofs3 = Woff;
|
|
359 |
Lut ->wFlags |= LUT_HASMATRIX3;
|
|
360 |
break;
|
|
361 |
|
|
362 |
case LUT_HASMATRIX4:
|
|
363 |
Lut ->Mat4 = WMat;
|
|
364 |
Lut ->Ofs4 = Woff;
|
|
365 |
Lut ->wFlags |= LUT_HASMATRIX4;
|
|
366 |
break;
|
|
367 |
|
|
368 |
|
|
369 |
default:;
|
|
370 |
}
|
|
371 |
|
|
372 |
return Lut;
|
|
373 |
}
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
// The full evaluator
|
|
378 |
|
|
379 |
void LCMSEXPORT cmsEvalLUT(LPLUT Lut, WORD In[], WORD Out[])
|
|
380 |
{
|
|
381 |
register unsigned int i;
|
|
382 |
WORD StageABC[MAXCHANNELS], StageLMN[MAXCHANNELS];
|
|
383 |
|
|
384 |
|
|
385 |
// Try to speedup things on plain devicelinks
|
|
386 |
if (Lut ->wFlags == LUT_HAS3DGRID) {
|
|
387 |
|
|
388 |
Lut ->CLut16params.Interp3D(In, Out, Lut -> T, &Lut -> CLut16params);
|
|
389 |
return;
|
|
390 |
}
|
|
391 |
|
|
392 |
|
|
393 |
// Nope, evaluate whole LUT
|
|
394 |
|
|
395 |
for (i=0; i < Lut -> InputChan; i++)
|
|
396 |
StageABC[i] = In[i];
|
|
397 |
|
|
398 |
|
|
399 |
if (Lut ->wFlags & LUT_V4_OUTPUT_EMULATE_V2) {
|
|
400 |
|
|
401 |
// Clamp Lab to avoid overflow
|
|
402 |
if (StageABC[0] > 0xFF00)
|
|
403 |
StageABC[0] = 0xFF00;
|
|
404 |
|
|
405 |
StageABC[0] = (WORD) FROM_V2_TO_V4(StageABC[0]);
|
|
406 |
StageABC[1] = (WORD) FROM_V2_TO_V4(StageABC[1]);
|
|
407 |
StageABC[2] = (WORD) FROM_V2_TO_V4(StageABC[2]);
|
|
408 |
|
|
409 |
}
|
|
410 |
|
|
411 |
if (Lut ->wFlags & LUT_V2_OUTPUT_EMULATE_V4) {
|
|
412 |
|
|
413 |
StageABC[0] = (WORD) FROM_V4_TO_V2(StageABC[0]);
|
|
414 |
StageABC[1] = (WORD) FROM_V4_TO_V2(StageABC[1]);
|
|
415 |
StageABC[2] = (WORD) FROM_V4_TO_V2(StageABC[2]);
|
|
416 |
}
|
|
417 |
|
|
418 |
|
|
419 |
// Matrix handling.
|
|
420 |
|
|
421 |
if (Lut -> wFlags & LUT_HASMATRIX) {
|
|
422 |
|
|
423 |
WVEC3 InVect, OutVect;
|
|
424 |
|
|
425 |
// In LUT8 here comes the special gray axis fixup
|
|
426 |
|
|
427 |
if (Lut ->FixGrayAxes) {
|
|
428 |
|
|
429 |
StageABC[1] = _cmsClampWord(StageABC[1] - 128);
|
|
430 |
StageABC[2] = _cmsClampWord(StageABC[2] - 128);
|
|
431 |
}
|
|
432 |
|
|
433 |
// Matrix
|
|
434 |
|
|
435 |
InVect.n[VX] = ToFixedDomain(StageABC[0]);
|
|
436 |
InVect.n[VY] = ToFixedDomain(StageABC[1]);
|
|
437 |
InVect.n[VZ] = ToFixedDomain(StageABC[2]);
|
|
438 |
|
|
439 |
|
|
440 |
MAT3evalW(&OutVect, &Lut -> Matrix, &InVect);
|
|
441 |
|
|
442 |
// PCS in 1Fixed15 format, adjusting
|
|
443 |
|
|
444 |
StageABC[0] = _cmsClampWord(FromFixedDomain(OutVect.n[VX]));
|
|
445 |
StageABC[1] = _cmsClampWord(FromFixedDomain(OutVect.n[VY]));
|
|
446 |
StageABC[2] = _cmsClampWord(FromFixedDomain(OutVect.n[VZ]));
|
|
447 |
}
|
|
448 |
|
|
449 |
|
|
450 |
// First linearization
|
|
451 |
|
|
452 |
if (Lut -> wFlags & LUT_HASTL1)
|
|
453 |
{
|
|
454 |
for (i=0; i < Lut -> InputChan; i++)
|
|
455 |
StageABC[i] = cmsLinearInterpLUT16(StageABC[i],
|
|
456 |
Lut -> L1[i],
|
|
457 |
&Lut -> In16params);
|
|
458 |
}
|
|
459 |
|
|
460 |
|
|
461 |
// Mat3, Ofs3, L3 processing
|
|
462 |
|
|
463 |
if (Lut ->wFlags & LUT_HASMATRIX3) {
|
|
464 |
|
|
465 |
WVEC3 InVect, OutVect;
|
|
466 |
|
|
467 |
InVect.n[VX] = ToFixedDomain(StageABC[0]);
|
|
468 |
InVect.n[VY] = ToFixedDomain(StageABC[1]);
|
|
469 |
InVect.n[VZ] = ToFixedDomain(StageABC[2]);
|
|
470 |
|
|
471 |
MAT3evalW(&OutVect, &Lut -> Mat3, &InVect);
|
|
472 |
|
|
473 |
OutVect.n[VX] += Lut ->Ofs3.n[VX];
|
|
474 |
OutVect.n[VY] += Lut ->Ofs3.n[VY];
|
|
475 |
OutVect.n[VZ] += Lut ->Ofs3.n[VZ];
|
|
476 |
|
|
477 |
StageABC[0] = _cmsClampWord(FromFixedDomain(OutVect.n[VX]));
|
|
478 |
StageABC[1] = _cmsClampWord(FromFixedDomain(OutVect.n[VY]));
|
|
479 |
StageABC[2] = _cmsClampWord(FromFixedDomain(OutVect.n[VZ]));
|
|
480 |
|
|
481 |
}
|
|
482 |
|
|
483 |
if (Lut ->wFlags & LUT_HASTL3) {
|
|
484 |
|
|
485 |
for (i=0; i < Lut -> InputChan; i++)
|
|
486 |
StageABC[i] = cmsLinearInterpLUT16(StageABC[i],
|
|
487 |
Lut -> L3[i],
|
|
488 |
&Lut -> L3params);
|
|
489 |
|
|
490 |
}
|
|
491 |
|
|
492 |
|
|
493 |
|
|
494 |
if (Lut -> wFlags & LUT_HAS3DGRID) {
|
|
495 |
|
|
496 |
Lut ->CLut16params.Interp3D(StageABC, StageLMN, Lut -> T, &Lut -> CLut16params);
|
|
497 |
|
|
498 |
}
|
|
499 |
else
|
|
500 |
{
|
|
501 |
|
|
502 |
for (i=0; i < Lut -> InputChan; i++)
|
|
503 |
StageLMN[i] = StageABC[i];
|
|
504 |
|
|
505 |
}
|
|
506 |
|
|
507 |
|
|
508 |
// Mat4, Ofs4, L4 processing
|
|
509 |
|
|
510 |
if (Lut ->wFlags & LUT_HASTL4) {
|
|
511 |
|
|
512 |
for (i=0; i < Lut -> OutputChan; i++)
|
|
513 |
StageLMN[i] = cmsLinearInterpLUT16(StageLMN[i],
|
|
514 |
Lut -> L4[i],
|
|
515 |
&Lut -> L4params);
|
|
516 |
}
|
|
517 |
|
|
518 |
if (Lut ->wFlags & LUT_HASMATRIX4) {
|
|
519 |
|
|
520 |
WVEC3 InVect, OutVect;
|
|
521 |
|
|
522 |
InVect.n[VX] = ToFixedDomain(StageLMN[0]);
|
|
523 |
InVect.n[VY] = ToFixedDomain(StageLMN[1]);
|
|
524 |
InVect.n[VZ] = ToFixedDomain(StageLMN[2]);
|
|
525 |
|
|
526 |
MAT3evalW(&OutVect, &Lut -> Mat4, &InVect);
|
|
527 |
|
|
528 |
OutVect.n[VX] += Lut ->Ofs4.n[VX];
|
|
529 |
OutVect.n[VY] += Lut ->Ofs4.n[VY];
|
|
530 |
OutVect.n[VZ] += Lut ->Ofs4.n[VZ];
|
|
531 |
|
|
532 |
StageLMN[0] = _cmsClampWord(FromFixedDomain(OutVect.n[VX]));
|
|
533 |
StageLMN[1] = _cmsClampWord(FromFixedDomain(OutVect.n[VY]));
|
|
534 |
StageLMN[2] = _cmsClampWord(FromFixedDomain(OutVect.n[VZ]));
|
|
535 |
|
|
536 |
}
|
|
537 |
|
|
538 |
// Last linearitzation
|
|
539 |
|
|
540 |
if (Lut -> wFlags & LUT_HASTL2)
|
|
541 |
{
|
|
542 |
for (i=0; i < Lut -> OutputChan; i++)
|
|
543 |
Out[i] = cmsLinearInterpLUT16(StageLMN[i],
|
|
544 |
Lut -> L2[i],
|
|
545 |
&Lut -> Out16params);
|
|
546 |
}
|
|
547 |
else
|
|
548 |
{
|
|
549 |
for (i=0; i < Lut -> OutputChan; i++)
|
|
550 |
Out[i] = StageLMN[i];
|
|
551 |
}
|
|
552 |
|
|
553 |
|
|
554 |
|
|
555 |
if (Lut ->wFlags & LUT_V4_INPUT_EMULATE_V2) {
|
|
556 |
|
|
557 |
Out[0] = (WORD) FROM_V4_TO_V2(Out[0]);
|
|
558 |
Out[1] = (WORD) FROM_V4_TO_V2(Out[1]);
|
|
559 |
Out[2] = (WORD) FROM_V4_TO_V2(Out[2]);
|
|
560 |
|
|
561 |
}
|
|
562 |
|
|
563 |
if (Lut ->wFlags & LUT_V2_INPUT_EMULATE_V4) {
|
|
564 |
|
|
565 |
Out[0] = (WORD) FROM_V2_TO_V4(Out[0]);
|
|
566 |
Out[1] = (WORD) FROM_V2_TO_V4(Out[1]);
|
|
567 |
Out[2] = (WORD) FROM_V2_TO_V4(Out[2]);
|
|
568 |
}
|
|
569 |
}
|
|
570 |
|
|
571 |
|
|
572 |
// Precomputes tables for 8-bit on input devicelink.
|
|
573 |
//
|
|
574 |
LPLUT _cmsBlessLUT8(LPLUT Lut)
|
|
575 |
{
|
|
576 |
int i, j;
|
|
577 |
WORD StageABC[3];
|
|
578 |
Fixed32 v1, v2, v3;
|
|
579 |
LPL8PARAMS p8;
|
|
580 |
LPL16PARAMS p = &Lut ->CLut16params;
|
|
581 |
|
|
582 |
|
|
583 |
p8 = (LPL8PARAMS) malloc(sizeof(L8PARAMS));
|
|
584 |
if (p8 == NULL) return NULL;
|
|
585 |
|
|
586 |
// values comes * 257, so we can safely take first byte (x << 8 + x)
|
|
587 |
// if there are prelinearization, is already smelted in tables
|
|
588 |
|
|
589 |
for (i=0; i < 256; i++) {
|
|
590 |
|
|
591 |
StageABC[0] = StageABC[1] = StageABC[2] = RGB_8_TO_16(i);
|
|
592 |
|
|
593 |
if (Lut ->wFlags & LUT_HASTL1) {
|
|
594 |
|
|
595 |
for (j=0; j < 3; j++)
|
|
596 |
StageABC[i] = cmsLinearInterpLUT16(StageABC[i],
|
|
597 |
Lut -> L1[i],
|
|
598 |
&Lut -> In16params);
|
|
599 |
Lut ->wFlags &= ~LUT_HASTL1;
|
|
600 |
}
|
|
601 |
|
|
602 |
|
|
603 |
v1 = ToFixedDomain(StageABC[0] * p -> Domain);
|
|
604 |
v2 = ToFixedDomain(StageABC[1] * p -> Domain);
|
|
605 |
v3 = ToFixedDomain(StageABC[2] * p -> Domain);
|
|
606 |
|
|
607 |
p8 ->X0[i] = p->opta3 * FIXED_TO_INT(v1);
|
|
608 |
p8 ->Y0[i] = p->opta2 * FIXED_TO_INT(v2);
|
|
609 |
p8 ->Z0[i] = p->opta1 * FIXED_TO_INT(v3);
|
|
610 |
|
|
611 |
p8 ->rx[i] = (WORD) FIXED_REST_TO_INT(v1);
|
|
612 |
p8 ->ry[i] = (WORD) FIXED_REST_TO_INT(v2);
|
|
613 |
p8 ->rz[i] = (WORD) FIXED_REST_TO_INT(v3);
|
|
614 |
|
|
615 |
}
|
|
616 |
|
|
617 |
Lut -> CLut16params.p8 = p8;
|
|
618 |
Lut -> CLut16params.Interp3D = cmsTetrahedralInterp8;
|
|
619 |
|
|
620 |
return Lut;
|
|
621 |
|
|
622 |
}
|
|
623 |
|
|
624 |
|
|
625 |
|
|
626 |
|
|
627 |
// ----------------------------------------------------------- Reverse interpolation
|
|
628 |
|
|
629 |
|
|
630 |
// Here's how it goes. The derivative Df(x) of the function f is the linear
|
|
631 |
// transformation that best approximates f near the point x. It can be represented
|
|
632 |
// by a matrix A whose entries are the partial derivatives of the components of f
|
|
633 |
// with respect to all the coordinates. This is know as the Jacobian
|
|
634 |
//
|
|
635 |
// The best linear approximation to f is given by the matrix equation:
|
|
636 |
//
|
|
637 |
// y-y0 = A (x-x0)
|
|
638 |
//
|
|
639 |
// So, if x0 is a good "guess" for the zero of f, then solving for the zero of this
|
|
640 |
// linear approximation will give a "better guess" for the zero of f. Thus let y=0,
|
|
641 |
// and since y0=f(x0) one can solve the above equation for x. This leads to the
|
|
642 |
// Newton's method formula:
|
|
643 |
//
|
|
644 |
// xn+1 = xn - A-1 f(xn)
|
|
645 |
//
|
|
646 |
// where xn+1 denotes the (n+1)-st guess, obtained from the n-th guess xn in the
|
|
647 |
// fashion described above. Iterating this will give better and better approximations
|
|
648 |
// if you have a "good enough" initial guess.
|
|
649 |
|
|
650 |
|
|
651 |
#define JACOBIAN_EPSILON 0.001
|
|
652 |
#define INVERSION_MAX_ITERATIONS 30
|
|
653 |
|
|
654 |
|
|
655 |
|
|
656 |
// Increment with reflexion on boundary
|
|
657 |
|
|
658 |
static
|
|
659 |
void IncDelta(double *Val)
|
|
660 |
{
|
|
661 |
if (*Val < (1.0 - JACOBIAN_EPSILON))
|
|
662 |
|
|
663 |
*Val += JACOBIAN_EPSILON;
|
|
664 |
|
|
665 |
else
|
|
666 |
*Val -= JACOBIAN_EPSILON;
|
|
667 |
|
|
668 |
}
|
|
669 |
|
|
670 |
|
|
671 |
|
|
672 |
static
|
|
673 |
void ToEncoded(WORD Encoded[3], LPVEC3 Float)
|
|
674 |
{
|
|
675 |
Encoded[0] = (WORD) floor(Float->n[0] * 65535.0 + 0.5);
|
|
676 |
Encoded[1] = (WORD) floor(Float->n[1] * 65535.0 + 0.5);
|
|
677 |
Encoded[2] = (WORD) floor(Float->n[2] * 65535.0 + 0.5);
|
|
678 |
}
|
|
679 |
|
|
680 |
static
|
|
681 |
void FromEncoded(LPVEC3 Float, WORD Encoded[3])
|
|
682 |
{
|
|
683 |
Float->n[0] = Encoded[0] / 65535.0;
|
|
684 |
Float->n[1] = Encoded[1] / 65535.0;
|
|
685 |
Float->n[2] = Encoded[2] / 65535.0;
|
|
686 |
}
|
|
687 |
|
|
688 |
// Evaluates the CLUT part of a LUT (4 -> 3 only)
|
|
689 |
static
|
|
690 |
void EvalLUTdoubleKLab(LPLUT Lut, const VEC3* In, WORD FixedK, LPcmsCIELab Out)
|
|
691 |
{
|
|
692 |
WORD wIn[4], wOut[3];
|
|
693 |
|
|
694 |
wIn[0] = (WORD) floor(In ->n[0] * 65535.0 + 0.5);
|
|
695 |
wIn[1] = (WORD) floor(In ->n[1] * 65535.0 + 0.5);
|
|
696 |
wIn[2] = (WORD) floor(In ->n[2] * 65535.0 + 0.5);
|
|
697 |
wIn[3] = FixedK;
|
|
698 |
|
|
699 |
cmsEvalLUT(Lut, wIn, wOut);
|
|
700 |
cmsLabEncoded2Float(Out, wOut);
|
|
701 |
}
|
|
702 |
|
|
703 |
// Builds a Jacobian CMY->Lab
|
|
704 |
|
|
705 |
static
|
|
706 |
void ComputeJacobianLab(LPLUT Lut, LPMAT3 Jacobian, const VEC3* Colorant, WORD K)
|
|
707 |
{
|
|
708 |
VEC3 ColorantD;
|
|
709 |
cmsCIELab Lab, LabD;
|
|
710 |
int j;
|
|
711 |
|
|
712 |
EvalLUTdoubleKLab(Lut, Colorant, K, &Lab);
|
|
713 |
|
|
714 |
|
|
715 |
for (j = 0; j < 3; j++) {
|
|
716 |
|
|
717 |
ColorantD.n[0] = Colorant ->n[0];
|
|
718 |
ColorantD.n[1] = Colorant ->n[1];
|
|
719 |
ColorantD.n[2] = Colorant ->n[2];
|
|
720 |
|
|
721 |
IncDelta(&ColorantD.n[j]);
|
|
722 |
|
|
723 |
EvalLUTdoubleKLab(Lut, &ColorantD, K, &LabD);
|
|
724 |
|
|
725 |
Jacobian->v[0].n[j] = ((LabD.L - Lab.L) / JACOBIAN_EPSILON);
|
|
726 |
Jacobian->v[1].n[j] = ((LabD.a - Lab.a) / JACOBIAN_EPSILON);
|
|
727 |
Jacobian->v[2].n[j] = ((LabD.b - Lab.b) / JACOBIAN_EPSILON);
|
|
728 |
|
|
729 |
}
|
|
730 |
}
|
|
731 |
|
|
732 |
|
|
733 |
// Evaluate a LUT in reverse direction. It only searches on 3->3 LUT, but It
|
|
734 |
// can be used on CMYK -> Lab LUT to obtain black preservation.
|
|
735 |
// Target holds LabK in this case
|
|
736 |
|
|
737 |
// x1 <- x - [J(x)]^-1 * f(x)
|
|
738 |
|
|
739 |
|
|
740 |
LCMSAPI double LCMSEXPORT cmsEvalLUTreverse(LPLUT Lut, WORD Target[], WORD Result[], LPWORD Hint)
|
|
741 |
{
|
|
742 |
int i;
|
|
743 |
double error, LastError = 1E20;
|
|
744 |
cmsCIELab fx, Goal;
|
|
745 |
VEC3 tmp, tmp2, x;
|
|
746 |
MAT3 Jacobian;
|
|
747 |
WORD FixedK;
|
|
748 |
WORD LastResult[4];
|
|
749 |
|
|
750 |
|
|
751 |
// This is our Lab goal
|
|
752 |
cmsLabEncoded2Float(&Goal, Target);
|
|
753 |
|
|
754 |
// Special case for CMYK->Lab
|
|
755 |
|
|
756 |
if (Lut ->InputChan == 4)
|
|
757 |
FixedK = Target[3];
|
|
758 |
else
|
|
759 |
FixedK = 0;
|
|
760 |
|
|
761 |
|
|
762 |
// Take the hint as starting point if specified
|
|
763 |
|
|
764 |
if (Hint == NULL) {
|
|
765 |
|
|
766 |
// Begin at any point, we choose 1/3 of neutral CMY gray
|
|
767 |
|
|
768 |
x.n[0] = x.n[1] = x.n[2] = 0.3;
|
|
769 |
|
|
770 |
}
|
|
771 |
else {
|
|
772 |
|
|
773 |
FromEncoded(&x, Hint);
|
|
774 |
}
|
|
775 |
|
|
776 |
|
|
777 |
// Iterate
|
|
778 |
|
|
779 |
for (i = 0; i < INVERSION_MAX_ITERATIONS; i++) {
|
|
780 |
|
|
781 |
// Get beginning fx
|
|
782 |
EvalLUTdoubleKLab(Lut, &x, FixedK, &fx);
|
|
783 |
|
|
784 |
// Compute error
|
|
785 |
error = cmsDeltaE(&fx, &Goal);
|
|
786 |
|
|
787 |
// If not convergent, return last safe value
|
|
788 |
if (error >= LastError)
|
|
789 |
break;
|
|
790 |
|
|
791 |
// Keep latest values
|
|
792 |
LastError = error;
|
|
793 |
|
|
794 |
ToEncoded(LastResult, &x);
|
|
795 |
LastResult[3] = FixedK;
|
|
796 |
|
|
797 |
// Obtain slope
|
|
798 |
ComputeJacobianLab(Lut, &Jacobian, &x, FixedK);
|
|
799 |
|
|
800 |
// Solve system
|
|
801 |
tmp2.n[0] = fx.L - Goal.L;
|
|
802 |
tmp2.n[1] = fx.a - Goal.a;
|
|
803 |
tmp2.n[2] = fx.b - Goal.b;
|
|
804 |
|
|
805 |
if (!MAT3solve(&tmp, &Jacobian, &tmp2))
|
|
806 |
break;
|
|
807 |
|
|
808 |
// Move our guess
|
|
809 |
x.n[0] -= tmp.n[0];
|
|
810 |
x.n[1] -= tmp.n[1];
|
|
811 |
x.n[2] -= tmp.n[2];
|
|
812 |
|
|
813 |
// Some clipping....
|
|
814 |
VEC3saturate(&x);
|
|
815 |
}
|
|
816 |
|
|
817 |
Result[0] = LastResult[0];
|
|
818 |
Result[1] = LastResult[1];
|
|
819 |
Result[2] = LastResult[2];
|
|
820 |
Result[3] = LastResult[3];
|
|
821 |
|
|
822 |
return LastError;
|
|
823 |
|
|
824 |
}
|