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 |
// ---------------------------------------------------------------------------------
|
|
57 |
|
|
58 |
static volatile int GlobalBlackPreservationStrategy = 0;
|
|
59 |
|
|
60 |
// Quantize a value 0 <= i < MaxSamples
|
|
61 |
|
|
62 |
WORD _cmsQuantizeVal(double i, int MaxSamples)
|
|
63 |
{
|
|
64 |
double x;
|
|
65 |
|
|
66 |
x = ((double) i * 65535.) / (double) (MaxSamples - 1);
|
|
67 |
|
|
68 |
return (WORD) floor(x + .5);
|
|
69 |
}
|
|
70 |
|
|
71 |
|
|
72 |
// Is a table linear?
|
|
73 |
|
|
74 |
int cmsIsLinear(WORD Table[], int nEntries)
|
|
75 |
{
|
|
76 |
register int i;
|
|
77 |
int diff;
|
|
78 |
|
|
79 |
for (i=0; i < nEntries; i++) {
|
|
80 |
|
|
81 |
diff = abs((int) Table[i] - (int) _cmsQuantizeVal(i, nEntries));
|
|
82 |
if (diff > 3)
|
|
83 |
return 0;
|
|
84 |
}
|
|
85 |
|
|
86 |
return 1;
|
|
87 |
}
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
// pow() restricted to integer
|
|
92 |
|
|
93 |
static
|
|
94 |
int ipow(int base, int exp)
|
|
95 |
{
|
|
96 |
int res = base;
|
|
97 |
|
|
98 |
while (--exp)
|
|
99 |
res *= base;
|
|
100 |
|
|
101 |
return res;
|
|
102 |
}
|
|
103 |
|
|
104 |
|
|
105 |
// Given n, 0<=n<=clut^dim, returns the colorant.
|
|
106 |
|
|
107 |
static
|
|
108 |
int ComponentOf(int n, int clut, int nColorant)
|
|
109 |
{
|
|
110 |
if (nColorant <= 0)
|
|
111 |
return (n % clut);
|
|
112 |
|
|
113 |
n /= ipow(clut, nColorant);
|
|
114 |
|
|
115 |
return (n % clut);
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
// This routine does a sweep on whole input space, and calls its callback
|
|
121 |
// function on knots. returns TRUE if all ok, FALSE otherwise.
|
|
122 |
|
2394
|
123 |
LCMSBOOL LCMSEXPORT cmsSample3DGrid(LPLUT Lut, _cmsSAMPLER Sampler, LPVOID Cargo, DWORD dwFlags)
|
2
|
124 |
{
|
|
125 |
int i, t, nTotalPoints, Colorant, index;
|
|
126 |
WORD In[MAXCHANNELS], Out[MAXCHANNELS];
|
|
127 |
|
|
128 |
nTotalPoints = ipow(Lut->cLutPoints, Lut -> InputChan);
|
|
129 |
|
|
130 |
index = 0;
|
|
131 |
for (i = 0; i < nTotalPoints; i++) {
|
|
132 |
|
|
133 |
for (t=0; t < (int) Lut -> InputChan; t++) {
|
|
134 |
|
|
135 |
Colorant = ComponentOf(i, Lut -> cLutPoints, (Lut -> InputChan - t - 1 ));
|
|
136 |
In[t] = _cmsQuantizeVal(Colorant, Lut -> cLutPoints);
|
|
137 |
}
|
|
138 |
|
|
139 |
|
|
140 |
if (dwFlags & SAMPLER_HASTL1) {
|
|
141 |
|
|
142 |
for (t=0; t < (int) Lut -> InputChan; t++)
|
|
143 |
In[t] = cmsReverseLinearInterpLUT16(In[t],
|
|
144 |
Lut -> L1[t],
|
|
145 |
&Lut -> In16params);
|
|
146 |
}
|
|
147 |
|
2394
|
148 |
for (t=0; t < (int) Lut -> OutputChan; t++)
|
|
149 |
Out[t] = Lut->T[index + t];
|
2
|
150 |
|
2394
|
151 |
if (dwFlags & SAMPLER_HASTL2) {
|
2
|
152 |
|
|
153 |
for (t=0; t < (int) Lut -> OutputChan; t++)
|
2394
|
154 |
Out[t] = cmsLinearInterpLUT16(Out[t],
|
|
155 |
Lut -> L2[t],
|
|
156 |
&Lut -> Out16params);
|
|
157 |
}
|
2
|
158 |
|
|
159 |
|
|
160 |
if (!Sampler(In, Out, Cargo))
|
|
161 |
return FALSE;
|
|
162 |
|
|
163 |
if (!(dwFlags & SAMPLER_INSPECT)) {
|
|
164 |
|
|
165 |
if (dwFlags & SAMPLER_HASTL2) {
|
|
166 |
|
|
167 |
for (t=0; t < (int) Lut -> OutputChan; t++)
|
|
168 |
Out[t] = cmsReverseLinearInterpLUT16(Out[t],
|
|
169 |
Lut -> L2[t],
|
|
170 |
&Lut -> Out16params);
|
|
171 |
}
|
|
172 |
|
|
173 |
|
|
174 |
for (t=0; t < (int) Lut -> OutputChan; t++)
|
|
175 |
Lut->T[index + t] = Out[t];
|
|
176 |
|
|
177 |
}
|
|
178 |
|
|
179 |
index += Lut -> OutputChan;
|
|
180 |
|
|
181 |
}
|
|
182 |
|
|
183 |
return TRUE;
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
// choose reasonable resolution
|
|
192 |
int _cmsReasonableGridpointsByColorspace(icColorSpaceSignature Colorspace, DWORD dwFlags)
|
|
193 |
{
|
|
194 |
int nChannels;
|
|
195 |
|
|
196 |
// Already specified?
|
|
197 |
if (dwFlags & 0x00FF0000) {
|
|
198 |
// Yes, grab'em
|
|
199 |
return (dwFlags >> 16) & 0xFF;
|
|
200 |
}
|
|
201 |
|
|
202 |
nChannels = _cmsChannelsOf(Colorspace);
|
|
203 |
|
|
204 |
// HighResPrecalc is maximum resolution
|
|
205 |
|
|
206 |
if (dwFlags & cmsFLAGS_HIGHRESPRECALC) {
|
|
207 |
|
|
208 |
if (nChannels > 4)
|
|
209 |
return 7; // 7 for Hifi
|
|
210 |
|
|
211 |
if (nChannels == 4) // 23 for CMYK
|
|
212 |
return 23;
|
|
213 |
|
|
214 |
return 49; // 49 for RGB and others
|
|
215 |
}
|
|
216 |
|
|
217 |
|
|
218 |
// LowResPrecal is stripped resolution
|
|
219 |
|
|
220 |
if (dwFlags & cmsFLAGS_LOWRESPRECALC) {
|
|
221 |
|
|
222 |
if (nChannels > 4)
|
|
223 |
return 6; // 6 for Hifi
|
|
224 |
|
|
225 |
if (nChannels == 1)
|
|
226 |
return 33; // For monochrome
|
|
227 |
|
|
228 |
return 17; // 17 for remaining
|
|
229 |
}
|
|
230 |
|
|
231 |
// Default values
|
|
232 |
|
|
233 |
if (nChannels > 4)
|
|
234 |
return 7; // 7 for Hifi
|
|
235 |
|
|
236 |
if (nChannels == 4)
|
|
237 |
return 17; // 17 for CMYK
|
|
238 |
|
|
239 |
return 33; // 33 for RGB
|
|
240 |
|
|
241 |
}
|
|
242 |
|
|
243 |
// Sampler implemented by another transform. This is a clean way to
|
|
244 |
// precalculate the devicelink 3D CLUT for almost any transform
|
|
245 |
|
|
246 |
static
|
|
247 |
int XFormSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
248 |
{
|
|
249 |
cmsDoTransform((cmsHTRANSFORM) Cargo, In, Out, 1);
|
|
250 |
return TRUE;
|
|
251 |
}
|
|
252 |
|
|
253 |
// This routine does compute the devicelink CLUT containing whole
|
|
254 |
// transform. Handles any channel number.
|
|
255 |
|
|
256 |
LPLUT _cmsPrecalculateDeviceLink(cmsHTRANSFORM h, DWORD dwFlags)
|
|
257 |
{
|
|
258 |
_LPcmsTRANSFORM p = (_LPcmsTRANSFORM) h;
|
|
259 |
LPLUT Grid;
|
|
260 |
int nGridPoints;
|
|
261 |
DWORD dwFormatIn, dwFormatOut;
|
2394
|
262 |
DWORD SaveFormatIn, SaveFormatOut;
|
2
|
263 |
int ChannelsIn, ChannelsOut;
|
|
264 |
LPLUT SaveGamutLUT;
|
|
265 |
|
2394
|
266 |
|
2
|
267 |
// Remove any gamut checking
|
|
268 |
SaveGamutLUT = p ->Gamut;
|
|
269 |
p ->Gamut = NULL;
|
|
270 |
|
|
271 |
ChannelsIn = _cmsChannelsOf(p -> EntryColorSpace);
|
|
272 |
ChannelsOut = _cmsChannelsOf(p -> ExitColorSpace);
|
|
273 |
|
|
274 |
nGridPoints = _cmsReasonableGridpointsByColorspace(p -> EntryColorSpace, dwFlags);
|
|
275 |
|
|
276 |
Grid = cmsAllocLUT();
|
|
277 |
if (!Grid) return NULL;
|
|
278 |
|
|
279 |
Grid = cmsAlloc3DGrid(Grid, nGridPoints, ChannelsIn, ChannelsOut);
|
|
280 |
|
|
281 |
// Compute device link on 16-bit basis
|
|
282 |
dwFormatIn = (CHANNELS_SH(ChannelsIn)|BYTES_SH(2));
|
|
283 |
dwFormatOut = (CHANNELS_SH(ChannelsOut)|BYTES_SH(2));
|
|
284 |
|
2394
|
285 |
SaveFormatIn = p ->InputFormat;
|
|
286 |
SaveFormatOut = p ->OutputFormat;
|
|
287 |
|
|
288 |
p -> InputFormat = dwFormatIn;
|
|
289 |
p -> OutputFormat = dwFormatOut;
|
|
290 |
p -> FromInput = _cmsIdentifyInputFormat(p, dwFormatIn);
|
|
291 |
p -> ToOutput = _cmsIdentifyOutputFormat(p, dwFormatOut);
|
2
|
292 |
|
|
293 |
// Fix gamut & gamma possible mismatches.
|
|
294 |
|
|
295 |
if (!(dwFlags & cmsFLAGS_NOPRELINEARIZATION)) {
|
|
296 |
|
|
297 |
cmsHTRANSFORM hOne[1];
|
|
298 |
hOne[0] = h;
|
|
299 |
|
|
300 |
_cmsComputePrelinearizationTablesFromXFORM(hOne, 1, Grid);
|
|
301 |
}
|
|
302 |
|
|
303 |
// Attention to this typecast! we can take the luxury to
|
|
304 |
// do this since cmsHTRANSFORM is only an alias to a pointer
|
|
305 |
// to the transform struct.
|
|
306 |
|
|
307 |
if (!cmsSample3DGrid(Grid, XFormSampler, (LPVOID) p, Grid -> wFlags)) {
|
|
308 |
|
|
309 |
cmsFreeLUT(Grid);
|
2394
|
310 |
Grid = NULL;
|
2
|
311 |
}
|
|
312 |
|
2394
|
313 |
p ->Gamut = SaveGamutLUT;
|
|
314 |
p ->InputFormat = SaveFormatIn;
|
|
315 |
p ->OutputFormat = SaveFormatOut;
|
2
|
316 |
|
|
317 |
return Grid;
|
|
318 |
}
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
// Sampler for Black-preserving CMYK->CMYK transforms
|
|
323 |
|
|
324 |
typedef struct {
|
|
325 |
cmsHTRANSFORM cmyk2cmyk;
|
|
326 |
cmsHTRANSFORM cmyk2Lab;
|
|
327 |
LPGAMMATABLE KTone;
|
|
328 |
L16PARAMS KToneParams;
|
|
329 |
LPLUT LabK2cmyk;
|
|
330 |
double MaxError;
|
|
331 |
|
|
332 |
cmsHTRANSFORM hRoundTrip;
|
|
333 |
int MaxTAC;
|
|
334 |
|
|
335 |
cmsHTRANSFORM hProofOutput;
|
|
336 |
|
|
337 |
} BPCARGO, *LPBPCARGO;
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
// Preserve black only if that is the only ink used
|
|
342 |
static
|
|
343 |
int BlackPreservingGrayOnlySampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
344 |
{
|
|
345 |
BPCARGO* bp = (LPBPCARGO) Cargo;
|
|
346 |
|
|
347 |
// If going across black only, keep black only
|
|
348 |
if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
|
|
349 |
|
|
350 |
// TAC does not apply because it is black ink!
|
|
351 |
Out[0] = Out[1] = Out[2] = 0;
|
|
352 |
Out[3] = cmsLinearInterpLUT16(In[3], bp->KTone ->GammaTable, &bp->KToneParams);
|
|
353 |
return 1;
|
|
354 |
}
|
|
355 |
|
|
356 |
// Keep normal transform for other colors
|
|
357 |
cmsDoTransform(bp ->cmyk2cmyk, In, Out, 1);
|
|
358 |
return 1;
|
|
359 |
}
|
|
360 |
|
|
361 |
|
|
362 |
|
2394
|
363 |
// Preserve all K plane.
|
2
|
364 |
static
|
|
365 |
int BlackPreservingSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
366 |
{
|
|
367 |
|
|
368 |
WORD LabK[4];
|
|
369 |
double SumCMY, SumCMYK, Error;
|
|
370 |
cmsCIELab ColorimetricLab, BlackPreservingLab;
|
|
371 |
BPCARGO* bp = (LPBPCARGO) Cargo;
|
|
372 |
|
|
373 |
// Get the K across Tone curve
|
|
374 |
LabK[3] = cmsLinearInterpLUT16(In[3], bp->KTone ->GammaTable, &bp->KToneParams);
|
|
375 |
|
|
376 |
// If going across black only, keep black only
|
|
377 |
if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
|
|
378 |
|
|
379 |
Out[0] = Out[1] = Out[2] = 0;
|
|
380 |
Out[3] = LabK[3];
|
|
381 |
return 1;
|
|
382 |
}
|
|
383 |
|
|
384 |
// Try the original transform, maybe K is already ok (valid on K=0)
|
|
385 |
cmsDoTransform(bp ->cmyk2cmyk, In, Out, 1);
|
|
386 |
if (Out[3] == LabK[3]) return 1;
|
|
387 |
|
|
388 |
|
|
389 |
// No, mesure and keep Lab measurement for further usage
|
|
390 |
cmsDoTransform(bp->hProofOutput, Out, &ColorimetricLab, 1);
|
|
391 |
|
|
392 |
// Is not black only and the transform doesn't keep black.
|
|
393 |
// Obtain the Lab of CMYK. After that we have Lab + K
|
|
394 |
cmsDoTransform(bp ->cmyk2Lab, In, LabK, 1);
|
|
395 |
|
|
396 |
// Obtain the corresponding CMY using reverse interpolation.
|
|
397 |
// As a seed, we use the colorimetric CMY
|
|
398 |
cmsEvalLUTreverse(bp ->LabK2cmyk, LabK, Out, Out);
|
|
399 |
|
|
400 |
// Estimate the error
|
|
401 |
cmsDoTransform(bp->hProofOutput, Out, &BlackPreservingLab, 1);
|
|
402 |
Error = cmsDeltaE(&ColorimetricLab, &BlackPreservingLab);
|
|
403 |
|
|
404 |
|
|
405 |
// Apply TAC if needed
|
|
406 |
|
|
407 |
SumCMY = Out[0] + Out[1] + Out[2];
|
|
408 |
SumCMYK = SumCMY + Out[3];
|
|
409 |
|
|
410 |
if (SumCMYK > bp ->MaxTAC) {
|
|
411 |
|
|
412 |
double Ratio = 1 - ((SumCMYK - bp->MaxTAC) / SumCMY);
|
|
413 |
if (Ratio < 0)
|
|
414 |
Ratio = 0;
|
|
415 |
|
|
416 |
Out[0] = (WORD) floor(Out[0] * Ratio + 0.5); // C
|
|
417 |
Out[1] = (WORD) floor(Out[1] * Ratio + 0.5); // M
|
|
418 |
Out[2] = (WORD) floor(Out[2] * Ratio + 0.5); // Y
|
|
419 |
}
|
|
420 |
|
|
421 |
return 1;
|
|
422 |
}
|
|
423 |
|
|
424 |
|
|
425 |
// Sample whole gamut to estimate maximum TAC
|
|
426 |
|
|
427 |
#ifdef _MSC_VER
|
|
428 |
#pragma warning(disable : 4100)
|
|
429 |
#endif
|
|
430 |
|
|
431 |
static
|
|
432 |
int EstimateTAC(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
433 |
{
|
|
434 |
BPCARGO* bp = (LPBPCARGO) Cargo;
|
|
435 |
WORD RoundTrip[4];
|
|
436 |
int Sum;
|
|
437 |
|
|
438 |
cmsDoTransform(bp->hRoundTrip, In, RoundTrip, 1);
|
|
439 |
|
|
440 |
Sum = RoundTrip[0] + RoundTrip[1] + RoundTrip[2] + RoundTrip[3];
|
|
441 |
|
|
442 |
if (Sum > bp ->MaxTAC)
|
|
443 |
bp ->MaxTAC = Sum;
|
|
444 |
|
|
445 |
return 1;
|
|
446 |
}
|
|
447 |
|
|
448 |
|
|
449 |
// Estimate the maximum error
|
|
450 |
static
|
|
451 |
int BlackPreservingEstimateErrorSampler(register WORD In[], register WORD Out[], register LPVOID Cargo)
|
|
452 |
{
|
|
453 |
BPCARGO* bp = (LPBPCARGO) Cargo;
|
|
454 |
WORD ColorimetricOut[4];
|
|
455 |
cmsCIELab ColorimetricLab, BlackPreservingLab;
|
|
456 |
double Error;
|
|
457 |
|
|
458 |
if (In[0] == 0 && In[1] == 0 && In[2] == 0) return 1;
|
|
459 |
|
|
460 |
cmsDoTransform(bp->cmyk2cmyk, In, ColorimetricOut, 1);
|
|
461 |
|
|
462 |
cmsDoTransform(bp->hProofOutput, ColorimetricOut, &ColorimetricLab, 1);
|
|
463 |
cmsDoTransform(bp->hProofOutput, Out, &BlackPreservingLab, 1);
|
|
464 |
|
|
465 |
Error = cmsDeltaE(&ColorimetricLab, &BlackPreservingLab);
|
|
466 |
|
|
467 |
if (Error > bp ->MaxError)
|
|
468 |
bp ->MaxError = Error;
|
|
469 |
|
|
470 |
return 1;
|
|
471 |
}
|
|
472 |
|
|
473 |
// Setup the K preservation strategy
|
|
474 |
int LCMSEXPORT cmsSetCMYKPreservationStrategy(int n)
|
|
475 |
{
|
|
476 |
int OldVal = GlobalBlackPreservationStrategy;
|
|
477 |
|
|
478 |
if (n >= 0)
|
|
479 |
GlobalBlackPreservationStrategy = n;
|
|
480 |
|
|
481 |
return OldVal;
|
|
482 |
}
|
|
483 |
|
2394
|
484 |
#pragma warning(disable: 4550)
|
2
|
485 |
|
|
486 |
// Get a pointer to callback on depending of strategy
|
|
487 |
static
|
|
488 |
_cmsSAMPLER _cmsGetBlackPreservationSampler(void)
|
|
489 |
{
|
|
490 |
switch (GlobalBlackPreservationStrategy) {
|
|
491 |
|
|
492 |
case 0: return BlackPreservingGrayOnlySampler;
|
|
493 |
default: return BlackPreservingSampler;
|
|
494 |
}
|
|
495 |
|
|
496 |
}
|
|
497 |
|
|
498 |
// This is the black-preserving devicelink generator
|
|
499 |
LPLUT _cmsPrecalculateBlackPreservingDeviceLink(cmsHTRANSFORM hCMYK2CMYK, DWORD dwFlags)
|
|
500 |
{
|
|
501 |
_LPcmsTRANSFORM p = (_LPcmsTRANSFORM) hCMYK2CMYK;
|
|
502 |
BPCARGO Cargo;
|
|
503 |
LPLUT Grid;
|
|
504 |
DWORD LocalFlags;
|
|
505 |
cmsHPROFILE hLab = cmsCreateLabProfile(NULL);
|
|
506 |
int nGridPoints;
|
|
507 |
icTagSignature Device2PCS[] = {icSigAToB0Tag, // Perceptual
|
|
508 |
icSigAToB1Tag, // Relative colorimetric
|
|
509 |
icSigAToB2Tag, // Saturation
|
|
510 |
icSigAToB1Tag }; // Absolute colorimetric
|
|
511 |
// (Relative/WhitePoint)
|
|
512 |
|
|
513 |
nGridPoints = _cmsReasonableGridpointsByColorspace(p -> EntryColorSpace, dwFlags);
|
|
514 |
|
|
515 |
// Get a copy of inteserting flags for this kind of xform
|
|
516 |
LocalFlags = cmsFLAGS_NOTPRECALC;
|
|
517 |
if (p -> dwOriginalFlags & cmsFLAGS_BLACKPOINTCOMPENSATION)
|
|
518 |
LocalFlags |= cmsFLAGS_BLACKPOINTCOMPENSATION;
|
|
519 |
|
|
520 |
// Fill in cargo struct
|
|
521 |
Cargo.cmyk2cmyk = hCMYK2CMYK;
|
|
522 |
|
2394
|
523 |
// Compute tone curve.
|
2
|
524 |
Cargo.KTone = _cmsBuildKToneCurve(hCMYK2CMYK, 256);
|
|
525 |
if (Cargo.KTone == NULL) return NULL;
|
|
526 |
cmsCalcL16Params(Cargo.KTone ->nEntries, &Cargo.KToneParams);
|
|
527 |
|
|
528 |
|
|
529 |
// Create a CMYK->Lab "normal" transform on input, without K-preservation
|
|
530 |
Cargo.cmyk2Lab = cmsCreateTransform(p ->InputProfile, TYPE_CMYK_16,
|
|
531 |
hLab, TYPE_Lab_16, p->Intent, LocalFlags);
|
|
532 |
|
|
533 |
// We are going to use the reverse of proof direction
|
|
534 |
Cargo.LabK2cmyk = cmsReadICCLut(p->OutputProfile, Device2PCS[p->Intent]);
|
|
535 |
|
|
536 |
// Is there any table available?
|
2394
|
537 |
if (Cargo.LabK2cmyk == NULL) {
|
2
|
538 |
|
2394
|
539 |
Grid = NULL;
|
2
|
540 |
goto Cleanup;
|
2394
|
541 |
}
|
2
|
542 |
|
|
543 |
// Setup a roundtrip on output profile for TAC estimation
|
|
544 |
Cargo.hRoundTrip = cmsCreateTransform(p ->OutputProfile, TYPE_CMYK_16,
|
|
545 |
p ->OutputProfile, TYPE_CMYK_16, p->Intent, cmsFLAGS_NOTPRECALC);
|
|
546 |
|
|
547 |
|
|
548 |
// Setup a proof CMYK->Lab on output
|
|
549 |
Cargo.hProofOutput = cmsCreateTransform(p ->OutputProfile, TYPE_CMYK_16,
|
|
550 |
hLab, TYPE_Lab_DBL, p->Intent, LocalFlags);
|
|
551 |
|
|
552 |
|
|
553 |
// Create an empty LUT for holding K-preserving xform
|
|
554 |
Grid = cmsAllocLUT();
|
|
555 |
if (!Grid) goto Cleanup;
|
|
556 |
|
|
557 |
Grid = cmsAlloc3DGrid(Grid, nGridPoints, 4, 4);
|
|
558 |
|
|
559 |
// Setup formatters
|
|
560 |
p -> FromInput = _cmsIdentifyInputFormat(p, TYPE_CMYK_16);
|
|
561 |
p -> ToOutput = _cmsIdentifyOutputFormat(p, TYPE_CMYK_16);
|
|
562 |
|
|
563 |
|
|
564 |
|
|
565 |
// Step #1, estimate TAC
|
|
566 |
Cargo.MaxTAC = 0;
|
|
567 |
if (!cmsSample3DGrid(Grid, EstimateTAC, (LPVOID) &Cargo, 0)) {
|
|
568 |
|
|
569 |
cmsFreeLUT(Grid);
|
|
570 |
Grid = NULL;
|
|
571 |
goto Cleanup;
|
|
572 |
}
|
|
573 |
|
|
574 |
|
|
575 |
// Step #2, compute approximation
|
|
576 |
if (!cmsSample3DGrid(Grid, _cmsGetBlackPreservationSampler(), (LPVOID) &Cargo, 0)) {
|
|
577 |
|
|
578 |
cmsFreeLUT(Grid);
|
|
579 |
Grid = NULL;
|
|
580 |
goto Cleanup;
|
|
581 |
}
|
|
582 |
|
|
583 |
// Step #3, estimate error
|
|
584 |
Cargo.MaxError = 0;
|
|
585 |
cmsSample3DGrid(Grid, BlackPreservingEstimateErrorSampler, (LPVOID) &Cargo, SAMPLER_INSPECT);
|
|
586 |
|
|
587 |
|
|
588 |
Cleanup:
|
|
589 |
|
|
590 |
if (Cargo.cmyk2Lab) cmsDeleteTransform(Cargo.cmyk2Lab);
|
|
591 |
if (Cargo.hRoundTrip) cmsDeleteTransform(Cargo.hRoundTrip);
|
|
592 |
if (Cargo.hProofOutput) cmsDeleteTransform(Cargo.hProofOutput);
|
|
593 |
|
|
594 |
if (hLab) cmsCloseProfile(hLab);
|
|
595 |
if (Cargo.KTone) cmsFreeGamma(Cargo.KTone);
|
|
596 |
if (Cargo.LabK2cmyk) cmsFreeLUT(Cargo.LabK2cmyk);
|
|
597 |
|
|
598 |
return Grid;
|
|
599 |
}
|
|
600 |
|
|
601 |
|
|
602 |
|
|
603 |
// Fix broken LUT. just to obtain other CMS compatibility
|
|
604 |
|
|
605 |
static
|
|
606 |
void PatchLUT(LPLUT Grid, WORD At[], WORD Value[],
|
|
607 |
int nChannelsOut, int nChannelsIn)
|
|
608 |
{
|
|
609 |
LPL16PARAMS p16 = &Grid -> CLut16params;
|
|
610 |
double px, py, pz, pw;
|
|
611 |
int x0, y0, z0, w0;
|
|
612 |
int i, index;
|
|
613 |
|
|
614 |
|
|
615 |
if (Grid ->wFlags & LUT_HASTL1) return; // There is a prelinearization
|
|
616 |
|
|
617 |
px = ((double) At[0] * (p16->Domain)) / 65535.0;
|
|
618 |
py = ((double) At[1] * (p16->Domain)) / 65535.0;
|
|
619 |
pz = ((double) At[2] * (p16->Domain)) / 65535.0;
|
|
620 |
pw = ((double) At[3] * (p16->Domain)) / 65535.0;
|
|
621 |
|
|
622 |
x0 = (int) floor(px);
|
|
623 |
y0 = (int) floor(py);
|
|
624 |
z0 = (int) floor(pz);
|
|
625 |
w0 = (int) floor(pw);
|
|
626 |
|
|
627 |
if (nChannelsIn == 4) {
|
|
628 |
|
|
629 |
if (((px - x0) != 0) ||
|
|
630 |
((py - y0) != 0) ||
|
|
631 |
((pz - z0) != 0) ||
|
|
632 |
((pw - w0) != 0)) return; // Not on exact node
|
|
633 |
|
|
634 |
index = p16 -> opta4 * x0 +
|
|
635 |
p16 -> opta3 * y0 +
|
|
636 |
p16 -> opta2 * z0 +
|
|
637 |
p16 -> opta1 * w0;
|
|
638 |
}
|
|
639 |
else
|
|
640 |
if (nChannelsIn == 3) {
|
|
641 |
|
|
642 |
if (((px - x0) != 0) ||
|
|
643 |
((py - y0) != 0) ||
|
|
644 |
((pz - z0) != 0)) return; // Not on exact node
|
|
645 |
|
|
646 |
index = p16 -> opta3 * x0 +
|
|
647 |
p16 -> opta2 * y0 +
|
|
648 |
p16 -> opta1 * z0;
|
|
649 |
}
|
|
650 |
else
|
|
651 |
if (nChannelsIn == 1) {
|
|
652 |
|
|
653 |
if (((px - x0) != 0)) return; // Not on exact node
|
|
654 |
|
|
655 |
index = p16 -> opta1 * x0;
|
|
656 |
}
|
|
657 |
else {
|
|
658 |
cmsSignalError(LCMS_ERRC_ABORTED, "(internal) %d Channels are not supported on PatchLUT", nChannelsIn);
|
|
659 |
return;
|
|
660 |
}
|
|
661 |
|
|
662 |
for (i=0; i < nChannelsOut; i++)
|
|
663 |
Grid -> T[index + i] = Value[i];
|
|
664 |
|
|
665 |
}
|
|
666 |
|
|
667 |
|
|
668 |
|
2394
|
669 |
LCMSBOOL _cmsFixWhiteMisalignment(_LPcmsTRANSFORM p)
|
2
|
670 |
{
|
|
671 |
|
|
672 |
WORD *WhitePointIn, *WhitePointOut, *BlackPointIn, *BlackPointOut;
|
|
673 |
int nOuts, nIns;
|
|
674 |
|
|
675 |
|
|
676 |
if (!p -> DeviceLink) return FALSE;
|
|
677 |
|
|
678 |
if (p ->Intent == INTENT_ABSOLUTE_COLORIMETRIC) return FALSE;
|
|
679 |
if ((p ->PreviewProfile != NULL) &&
|
|
680 |
(p ->ProofIntent == INTENT_ABSOLUTE_COLORIMETRIC)) return FALSE;
|
|
681 |
|
|
682 |
|
|
683 |
if (!_cmsEndPointsBySpace(p -> EntryColorSpace,
|
|
684 |
&WhitePointIn, &BlackPointIn, &nIns)) return FALSE;
|
|
685 |
|
|
686 |
|
|
687 |
if (!_cmsEndPointsBySpace(p -> ExitColorSpace,
|
|
688 |
&WhitePointOut, &BlackPointOut, &nOuts)) return FALSE;
|
|
689 |
|
|
690 |
// Fix white only
|
|
691 |
|
|
692 |
PatchLUT(p -> DeviceLink, WhitePointIn, WhitePointOut, nOuts, nIns);
|
|
693 |
// PatchLUT(p -> DeviceLink, BlackPointIn, BlackPointOut, nOuts, nIns);
|
|
694 |
|
|
695 |
return TRUE;
|
|
696 |
}
|
2394
|
697 |
|