/src/ghostpdl/lcms2mt/src/cmscnvrt.c
Line | Count | Source (jump to first uncovered line) |
1 | | //--------------------------------------------------------------------------------- |
2 | | // |
3 | | // Little Color Management System |
4 | | // Copyright (c) 1998-2020 Marti Maria Saguer |
5 | | // |
6 | | // Permission is hereby granted, free of charge, to any person obtaining |
7 | | // a copy of this software and associated documentation files (the "Software"), |
8 | | // to deal in the Software without restriction, including without limitation |
9 | | // the rights to use, copy, modify, merge, publish, distribute, sublicense, |
10 | | // and/or sell copies of the Software, and to permit persons to whom the Software |
11 | | // is furnished to do so, subject to the following conditions: |
12 | | // |
13 | | // The above copyright notice and this permission notice shall be included in |
14 | | // all copies or substantial portions of the Software. |
15 | | // |
16 | | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
17 | | // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO |
18 | | // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
19 | | // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
20 | | // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
21 | | // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
22 | | // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
23 | | // |
24 | | //--------------------------------------------------------------------------------- |
25 | | // |
26 | | |
27 | | #include "lcms2_internal.h" |
28 | | |
29 | | |
30 | | // This is the default routine for ICC-style intents. A user may decide to override it by using a plugin. |
31 | | // Supported intents are perceptual, relative colorimetric, saturation and ICC-absolute colorimetric |
32 | | static |
33 | | cmsPipeline* DefaultICCintents(cmsContext ContextID, |
34 | | cmsUInt32Number nProfiles, |
35 | | cmsUInt32Number Intents[], |
36 | | cmsHPROFILE hProfiles[], |
37 | | cmsBool BPC[], |
38 | | cmsFloat64Number AdaptationStates[], |
39 | | cmsUInt32Number dwFlags); |
40 | | |
41 | | //--------------------------------------------------------------------------------- |
42 | | |
43 | | // This is the entry for black-preserving K-only intents, which are non-ICC. Last profile have to be a output profile |
44 | | // to do the trick (no devicelinks allowed at that position) |
45 | | static |
46 | | cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID, |
47 | | cmsUInt32Number nProfiles, |
48 | | cmsUInt32Number Intents[], |
49 | | cmsHPROFILE hProfiles[], |
50 | | cmsBool BPC[], |
51 | | cmsFloat64Number AdaptationStates[], |
52 | | cmsUInt32Number dwFlags); |
53 | | |
54 | | //--------------------------------------------------------------------------------- |
55 | | |
56 | | // This is the entry for black-plane preserving, which are non-ICC. Again, Last profile have to be a output profile |
57 | | // to do the trick (no devicelinks allowed at that position) |
58 | | static |
59 | | cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID, |
60 | | cmsUInt32Number nProfiles, |
61 | | cmsUInt32Number Intents[], |
62 | | cmsHPROFILE hProfiles[], |
63 | | cmsBool BPC[], |
64 | | cmsFloat64Number AdaptationStates[], |
65 | | cmsUInt32Number dwFlags); |
66 | | |
67 | | //--------------------------------------------------------------------------------- |
68 | | |
69 | | |
70 | | // This is a structure holding implementations for all supported intents. |
71 | | typedef struct _cms_intents_list { |
72 | | |
73 | | cmsUInt32Number Intent; |
74 | | char Description[256]; |
75 | | cmsIntentFn Link; |
76 | | struct _cms_intents_list* Next; |
77 | | |
78 | | } cmsIntentsList; |
79 | | |
80 | | |
81 | | // Built-in intents |
82 | | static cmsIntentsList DefaultIntents[] = { |
83 | | |
84 | | { INTENT_PERCEPTUAL, "Perceptual", DefaultICCintents, &DefaultIntents[1] }, |
85 | | { INTENT_RELATIVE_COLORIMETRIC, "Relative colorimetric", DefaultICCintents, &DefaultIntents[2] }, |
86 | | { INTENT_SATURATION, "Saturation", DefaultICCintents, &DefaultIntents[3] }, |
87 | | { INTENT_ABSOLUTE_COLORIMETRIC, "Absolute colorimetric", DefaultICCintents, &DefaultIntents[4] }, |
88 | | { INTENT_PRESERVE_K_ONLY_PERCEPTUAL, "Perceptual preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[5] }, |
89 | | { INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC, "Relative colorimetric preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[6] }, |
90 | | { INTENT_PRESERVE_K_ONLY_SATURATION, "Saturation preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[7] }, |
91 | | { INTENT_PRESERVE_K_PLANE_PERCEPTUAL, "Perceptual preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[8] }, |
92 | | { INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC,"Relative colorimetric preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[9] }, |
93 | | { INTENT_PRESERVE_K_PLANE_SATURATION, "Saturation preserving black plane", BlackPreservingKPlaneIntents, NULL } |
94 | | }; |
95 | | |
96 | | |
97 | | // A pointer to the beginning of the list |
98 | | _cmsIntentsPluginChunkType _cmsIntentsPluginChunk = { NULL }; |
99 | | |
100 | | // Duplicates the zone of memory used by the plug-in in the new context |
101 | | static |
102 | | void DupPluginIntentsList(struct _cmsContext_struct* ctx, |
103 | | const struct _cmsContext_struct* src) |
104 | 0 | { |
105 | 0 | _cmsIntentsPluginChunkType newHead = { NULL }; |
106 | 0 | cmsIntentsList* entry; |
107 | 0 | cmsIntentsList* Anterior = NULL; |
108 | 0 | _cmsIntentsPluginChunkType* head = (_cmsIntentsPluginChunkType*) src->chunks[IntentPlugin]; |
109 | | |
110 | | // Walk the list copying all nodes |
111 | 0 | for (entry = head->Intents; |
112 | 0 | entry != NULL; |
113 | 0 | entry = entry ->Next) { |
114 | |
|
115 | 0 | cmsIntentsList *newEntry = ( cmsIntentsList *) _cmsSubAllocDup(ctx ->MemPool, entry, sizeof(cmsIntentsList)); |
116 | |
|
117 | 0 | if (newEntry == NULL) |
118 | 0 | return; |
119 | | |
120 | | // We want to keep the linked list order, so this is a little bit tricky |
121 | 0 | newEntry -> Next = NULL; |
122 | 0 | if (Anterior) |
123 | 0 | Anterior -> Next = newEntry; |
124 | |
|
125 | 0 | Anterior = newEntry; |
126 | |
|
127 | 0 | if (newHead.Intents == NULL) |
128 | 0 | newHead.Intents = newEntry; |
129 | 0 | } |
130 | | |
131 | 0 | ctx ->chunks[IntentPlugin] = _cmsSubAllocDup(ctx->MemPool, &newHead, sizeof(_cmsIntentsPluginChunkType)); |
132 | 0 | } |
133 | | |
134 | | void _cmsAllocIntentsPluginChunk(struct _cmsContext_struct* ctx, |
135 | | const struct _cmsContext_struct* src) |
136 | 162k | { |
137 | 162k | if (src != NULL) { |
138 | | |
139 | | // Copy all linked list |
140 | 0 | DupPluginIntentsList(ctx, src); |
141 | 0 | } |
142 | 162k | else { |
143 | 162k | static _cmsIntentsPluginChunkType IntentsPluginChunkType = { NULL }; |
144 | 162k | ctx ->chunks[IntentPlugin] = _cmsSubAllocDup(ctx ->MemPool, &IntentsPluginChunkType, sizeof(_cmsIntentsPluginChunkType)); |
145 | 162k | } |
146 | 162k | } |
147 | | |
148 | | |
149 | | // Search the list for a suitable intent. Returns NULL if not found |
150 | | static |
151 | | cmsIntentsList* SearchIntent(cmsContext ContextID, cmsUInt32Number Intent) |
152 | 2.35M | { |
153 | 2.35M | _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(ContextID, IntentPlugin); |
154 | 2.35M | cmsIntentsList* pt; |
155 | | |
156 | 2.35M | for (pt = ctx -> Intents; pt != NULL; pt = pt -> Next) |
157 | 0 | if (pt ->Intent == Intent) return pt; |
158 | | |
159 | 4.95M | for (pt = DefaultIntents; pt != NULL; pt = pt -> Next) |
160 | 4.95M | if (pt ->Intent == Intent) return pt; |
161 | | |
162 | 0 | return NULL; |
163 | 2.35M | } |
164 | | |
165 | | // Black point compensation. Implemented as a linear scaling in XYZ. Black points |
166 | | // should come relative to the white point. Fills an matrix/offset element m |
167 | | // which is organized as a 4x4 matrix. |
168 | | static |
169 | | void ComputeBlackPointCompensation(cmsContext ContextID, const cmsCIEXYZ* BlackPointIn, |
170 | | const cmsCIEXYZ* BlackPointOut, |
171 | | cmsMAT3* m, cmsVEC3* off) |
172 | 12.6k | { |
173 | 12.6k | cmsFloat64Number ax, ay, az, bx, by, bz, tx, ty, tz; |
174 | | |
175 | | // Now we need to compute a matrix plus an offset m and of such of |
176 | | // [m]*bpin + off = bpout |
177 | | // [m]*D50 + off = D50 |
178 | | // |
179 | | // This is a linear scaling in the form ax+b, where |
180 | | // a = (bpout - D50) / (bpin - D50) |
181 | | // b = - D50* (bpout - bpin) / (bpin - D50) |
182 | | |
183 | 12.6k | tx = BlackPointIn->X - cmsD50_XYZ(ContextID)->X; |
184 | 12.6k | ty = BlackPointIn->Y - cmsD50_XYZ(ContextID)->Y; |
185 | 12.6k | tz = BlackPointIn->Z - cmsD50_XYZ(ContextID)->Z; |
186 | | |
187 | 12.6k | ax = (BlackPointOut->X - cmsD50_XYZ(ContextID)->X) / tx; |
188 | 12.6k | ay = (BlackPointOut->Y - cmsD50_XYZ(ContextID)->Y) / ty; |
189 | 12.6k | az = (BlackPointOut->Z - cmsD50_XYZ(ContextID)->Z) / tz; |
190 | | |
191 | 12.6k | bx = - cmsD50_XYZ(ContextID)-> X * (BlackPointOut->X - BlackPointIn->X) / tx; |
192 | 12.6k | by = - cmsD50_XYZ(ContextID)-> Y * (BlackPointOut->Y - BlackPointIn->Y) / ty; |
193 | 12.6k | bz = - cmsD50_XYZ(ContextID)-> Z * (BlackPointOut->Z - BlackPointIn->Z) / tz; |
194 | | |
195 | 12.6k | _cmsVEC3init(ContextID, &m ->v[0], ax, 0, 0); |
196 | 12.6k | _cmsVEC3init(ContextID, &m ->v[1], 0, ay, 0); |
197 | 12.6k | _cmsVEC3init(ContextID, &m ->v[2], 0, 0, az); |
198 | 12.6k | _cmsVEC3init(ContextID, off, bx, by, bz); |
199 | | |
200 | 12.6k | } |
201 | | |
202 | | |
203 | | // Approximate a blackbody illuminant based on CHAD information |
204 | | static |
205 | | cmsFloat64Number CHAD2Temp(cmsContext ContextID, const cmsMAT3* Chad) |
206 | 0 | { |
207 | | // Convert D50 across inverse CHAD to get the absolute white point |
208 | 0 | cmsVEC3 d, s; |
209 | 0 | cmsCIEXYZ Dest; |
210 | 0 | cmsCIExyY DestChromaticity; |
211 | 0 | cmsFloat64Number TempK; |
212 | 0 | cmsMAT3 m1, m2; |
213 | |
|
214 | 0 | m1 = *Chad; |
215 | 0 | if (!_cmsMAT3inverse(ContextID, &m1, &m2)) return FALSE; |
216 | | |
217 | 0 | s.n[VX] = cmsD50_XYZ(ContextID) -> X; |
218 | 0 | s.n[VY] = cmsD50_XYZ(ContextID) -> Y; |
219 | 0 | s.n[VZ] = cmsD50_XYZ(ContextID) -> Z; |
220 | |
|
221 | 0 | _cmsMAT3eval(ContextID, &d, &m2, &s); |
222 | |
|
223 | 0 | Dest.X = d.n[VX]; |
224 | 0 | Dest.Y = d.n[VY]; |
225 | 0 | Dest.Z = d.n[VZ]; |
226 | |
|
227 | 0 | cmsXYZ2xyY(ContextID, &DestChromaticity, &Dest); |
228 | |
|
229 | 0 | if (!cmsTempFromWhitePoint(ContextID, &TempK, &DestChromaticity)) |
230 | 0 | return -1.0; |
231 | | |
232 | 0 | return TempK; |
233 | 0 | } |
234 | | |
235 | | // Compute a CHAD based on a given temperature |
236 | | static |
237 | | void Temp2CHAD(cmsContext ContextID, cmsMAT3* Chad, cmsFloat64Number Temp) |
238 | 0 | { |
239 | 0 | cmsCIEXYZ White; |
240 | 0 | cmsCIExyY ChromaticityOfWhite; |
241 | |
|
242 | 0 | cmsWhitePointFromTemp(ContextID, &ChromaticityOfWhite, Temp); |
243 | 0 | cmsxyY2XYZ(ContextID,&White, &ChromaticityOfWhite); |
244 | 0 | _cmsAdaptationMatrix(ContextID, Chad, NULL, &White, cmsD50_XYZ(ContextID)); |
245 | 0 | } |
246 | | |
247 | | // Join scalings to obtain relative input to absolute and then to relative output. |
248 | | // Result is stored in a 3x3 matrix |
249 | | static |
250 | | cmsBool ComputeAbsoluteIntent(cmsContext ContextID, cmsFloat64Number AdaptationState, |
251 | | const cmsCIEXYZ* WhitePointIn, |
252 | | const cmsMAT3* ChromaticAdaptationMatrixIn, |
253 | | const cmsCIEXYZ* WhitePointOut, |
254 | | const cmsMAT3* ChromaticAdaptationMatrixOut, |
255 | | cmsMAT3* m) |
256 | 0 | { |
257 | 0 | cmsMAT3 Scale, m1, m2, m3, m4; |
258 | | |
259 | | // TODO: Follow Marc Mahy's recommendation to check if CHAD is same by using M1*M2 == M2*M1. If so, do nothing. |
260 | | // TODO: Add support for ArgyllArts tag |
261 | | |
262 | | // Adaptation state |
263 | 0 | if (AdaptationState == 1.0) { |
264 | | |
265 | | // Observer is fully adapted. Keep chromatic adaptation. |
266 | | // That is the standard V4 behaviour |
267 | 0 | _cmsVEC3init(ContextID, &m->v[0], WhitePointIn->X / WhitePointOut->X, 0, 0); |
268 | 0 | _cmsVEC3init(ContextID, &m->v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0); |
269 | 0 | _cmsVEC3init(ContextID, &m->v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z); |
270 | |
|
271 | 0 | } |
272 | 0 | else { |
273 | | |
274 | | // Incomplete adaptation. This is an advanced feature. |
275 | 0 | _cmsVEC3init(ContextID, &Scale.v[0], WhitePointIn->X / WhitePointOut->X, 0, 0); |
276 | 0 | _cmsVEC3init(ContextID, &Scale.v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0); |
277 | 0 | _cmsVEC3init(ContextID, &Scale.v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z); |
278 | | |
279 | |
|
280 | 0 | if (AdaptationState == 0.0) { |
281 | |
|
282 | 0 | m1 = *ChromaticAdaptationMatrixOut; |
283 | 0 | _cmsMAT3per(ContextID, &m2, &m1, &Scale); |
284 | | // m2 holds CHAD from output white to D50 times abs. col. scaling |
285 | | |
286 | | // Observer is not adapted, undo the chromatic adaptation |
287 | 0 | _cmsMAT3per(ContextID, m, &m2, ChromaticAdaptationMatrixOut); |
288 | |
|
289 | 0 | m3 = *ChromaticAdaptationMatrixIn; |
290 | 0 | if (!_cmsMAT3inverse(ContextID, &m3, &m4)) return FALSE; |
291 | 0 | _cmsMAT3per(ContextID, m, &m2, &m4); |
292 | |
|
293 | 0 | } else { |
294 | |
|
295 | 0 | cmsMAT3 MixedCHAD; |
296 | 0 | cmsFloat64Number TempSrc, TempDest, Temp; |
297 | |
|
298 | 0 | m1 = *ChromaticAdaptationMatrixIn; |
299 | 0 | if (!_cmsMAT3inverse(ContextID, &m1, &m2)) return FALSE; |
300 | 0 | _cmsMAT3per(ContextID, &m3, &m2, &Scale); |
301 | | // m3 holds CHAD from input white to D50 times abs. col. scaling |
302 | |
|
303 | 0 | TempSrc = CHAD2Temp(ContextID, ChromaticAdaptationMatrixIn); |
304 | 0 | TempDest = CHAD2Temp(ContextID, ChromaticAdaptationMatrixOut); |
305 | |
|
306 | 0 | if (TempSrc < 0.0 || TempDest < 0.0) return FALSE; // Something went wrong |
307 | | |
308 | 0 | if (_cmsMAT3isIdentity(ContextID, &Scale) && fabs(TempSrc - TempDest) < 0.01) { |
309 | |
|
310 | 0 | _cmsMAT3identity(ContextID, m); |
311 | 0 | return TRUE; |
312 | 0 | } |
313 | | |
314 | 0 | Temp = (1.0 - AdaptationState) * TempDest + AdaptationState * TempSrc; |
315 | | |
316 | | // Get a CHAD from whatever output temperature to D50. This replaces output CHAD |
317 | 0 | Temp2CHAD(ContextID, &MixedCHAD, Temp); |
318 | |
|
319 | 0 | _cmsMAT3per(ContextID, m, &m3, &MixedCHAD); |
320 | 0 | } |
321 | |
|
322 | 0 | } |
323 | 0 | return TRUE; |
324 | |
|
325 | 0 | } |
326 | | |
327 | | // Just to see if m matrix should be applied |
328 | | static |
329 | | cmsBool IsEmptyLayer(cmsContext ContextID, cmsMAT3* m, cmsVEC3* off) |
330 | 1.10M | { |
331 | 1.10M | cmsFloat64Number diff = 0; |
332 | 1.10M | cmsMAT3 Ident; |
333 | 1.10M | int i; |
334 | | |
335 | 1.10M | if (m == NULL && off == NULL) return TRUE; // NULL is allowed as an empty layer |
336 | 1.10M | if (m == NULL && off != NULL) return FALSE; // This is an internal error |
337 | | |
338 | 1.10M | _cmsMAT3identity(ContextID, &Ident); |
339 | | |
340 | 11.0M | for (i=0; i < 3*3; i++) |
341 | 9.91M | diff += fabs(((cmsFloat64Number*)m)[i] - ((cmsFloat64Number*)&Ident)[i]); |
342 | | |
343 | 4.40M | for (i=0; i < 3; i++) |
344 | 3.30M | diff += fabs(((cmsFloat64Number*)off)[i]); |
345 | | |
346 | | |
347 | 1.10M | return (diff < 0.002); |
348 | 1.10M | } |
349 | | |
350 | | |
351 | | // Compute the conversion layer |
352 | | static |
353 | | cmsBool ComputeConversion(cmsContext ContextID, |
354 | | cmsUInt32Number i, |
355 | | cmsHPROFILE hProfiles[], |
356 | | cmsUInt32Number Intent, |
357 | | cmsBool BPC, |
358 | | cmsFloat64Number AdaptationState, |
359 | | cmsMAT3* m, cmsVEC3* off) |
360 | 1.07M | { |
361 | | |
362 | 1.07M | int k; |
363 | | |
364 | | // m and off are set to identity and this is detected latter on |
365 | 1.07M | _cmsMAT3identity(ContextID, m); |
366 | 1.07M | _cmsVEC3init(ContextID, off, 0, 0, 0); |
367 | | |
368 | | // If intent is abs. colorimetric, |
369 | 1.07M | if (Intent == INTENT_ABSOLUTE_COLORIMETRIC) { |
370 | |
|
371 | 0 | cmsCIEXYZ WhitePointIn, WhitePointOut; |
372 | 0 | cmsMAT3 ChromaticAdaptationMatrixIn, ChromaticAdaptationMatrixOut; |
373 | |
|
374 | 0 | _cmsReadMediaWhitePoint(ContextID, &WhitePointIn, hProfiles[i-1]); |
375 | 0 | _cmsReadCHAD(ContextID, &ChromaticAdaptationMatrixIn, hProfiles[i-1]); |
376 | |
|
377 | 0 | _cmsReadMediaWhitePoint(ContextID, &WhitePointOut, hProfiles[i]); |
378 | 0 | _cmsReadCHAD(ContextID, &ChromaticAdaptationMatrixOut, hProfiles[i]); |
379 | |
|
380 | 0 | if (!ComputeAbsoluteIntent(ContextID, AdaptationState, |
381 | 0 | &WhitePointIn, &ChromaticAdaptationMatrixIn, |
382 | 0 | &WhitePointOut, &ChromaticAdaptationMatrixOut, m)) return FALSE; |
383 | |
|
384 | 0 | } |
385 | 1.07M | else { |
386 | | // Rest of intents may apply BPC. |
387 | | |
388 | 1.07M | if (BPC) { |
389 | | |
390 | 323k | cmsCIEXYZ BlackPointIn, BlackPointOut; |
391 | | |
392 | 323k | cmsDetectBlackPoint(ContextID, &BlackPointIn, hProfiles[i-1], Intent, 0); |
393 | 323k | cmsDetectDestinationBlackPoint(ContextID, &BlackPointOut, hProfiles[i], Intent, 0); |
394 | | |
395 | | // If black points are equal, then do nothing |
396 | 323k | if (BlackPointIn.X != BlackPointOut.X || |
397 | 323k | BlackPointIn.Y != BlackPointOut.Y || |
398 | 323k | BlackPointIn.Z != BlackPointOut.Z) |
399 | 12.6k | ComputeBlackPointCompensation(ContextID, &BlackPointIn, &BlackPointOut, m, off); |
400 | 323k | } |
401 | 1.07M | } |
402 | | |
403 | | // Offset should be adjusted because the encoding. We encode XYZ normalized to 0..1.0, |
404 | | // to do that, we divide by MAX_ENCODEABLE_XZY. The conversion stage goes XYZ -> XYZ so |
405 | | // we have first to convert from encoded to XYZ and then convert back to encoded. |
406 | | // y = Mx + Off |
407 | | // x = x'c |
408 | | // y = M x'c + Off |
409 | | // y = y'c; y' = y / c |
410 | | // y' = (Mx'c + Off) /c = Mx' + (Off / c) |
411 | | |
412 | 4.31M | for (k=0; k < 3; k++) { |
413 | 3.23M | off ->n[k] /= MAX_ENCODEABLE_XYZ; |
414 | 3.23M | } |
415 | | |
416 | 1.07M | return TRUE; |
417 | 1.07M | } |
418 | | |
419 | | |
420 | | // Add a conversion stage if needed. If a matrix/offset m is given, it applies to XYZ space |
421 | | static |
422 | | cmsBool AddConversion(cmsContext ContextID, cmsPipeline* Result, cmsColorSpaceSignature InPCS, cmsColorSpaceSignature OutPCS, cmsMAT3* m, cmsVEC3* off) |
423 | 1.10M | { |
424 | 1.10M | cmsFloat64Number* m_as_dbl = (cmsFloat64Number*) m; |
425 | 1.10M | cmsFloat64Number* off_as_dbl = (cmsFloat64Number*) off; |
426 | | |
427 | | // Handle PCS mismatches. A specialized stage is added to the LUT in such case |
428 | 1.10M | switch (InPCS) { |
429 | | |
430 | 1.02M | case cmsSigXYZData: // Input profile operates in XYZ |
431 | | |
432 | 1.02M | switch (OutPCS) { |
433 | | |
434 | 387k | case cmsSigXYZData: // XYZ -> XYZ |
435 | 387k | if (!IsEmptyLayer(ContextID, m, off) && |
436 | 387k | !cmsPipelineInsertStage(ContextID, Result, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, m_as_dbl, off_as_dbl))) |
437 | 0 | return FALSE; |
438 | 387k | break; |
439 | | |
440 | 635k | case cmsSigLabData: // XYZ -> Lab |
441 | 635k | if (!IsEmptyLayer(ContextID, m, off) && |
442 | 635k | !cmsPipelineInsertStage(ContextID, Result, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, m_as_dbl, off_as_dbl))) |
443 | 0 | return FALSE; |
444 | 635k | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID))) |
445 | 0 | return FALSE; |
446 | 635k | break; |
447 | | |
448 | 635k | default: |
449 | 0 | return FALSE; // Colorspace mismatch |
450 | 1.02M | } |
451 | 1.02M | break; |
452 | | |
453 | 1.02M | case cmsSigLabData: // Input profile operates in Lab |
454 | | |
455 | 78.5k | switch (OutPCS) { |
456 | | |
457 | 7.46k | case cmsSigXYZData: // Lab -> XYZ |
458 | | |
459 | 7.46k | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID))) |
460 | 0 | return FALSE; |
461 | 7.46k | if (!IsEmptyLayer(ContextID, m, off) && |
462 | 7.46k | !cmsPipelineInsertStage(ContextID, Result, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, m_as_dbl, off_as_dbl))) |
463 | 0 | return FALSE; |
464 | 7.46k | break; |
465 | | |
466 | 71.0k | case cmsSigLabData: // Lab -> Lab |
467 | | |
468 | 71.0k | if (!IsEmptyLayer(ContextID, m, off)) { |
469 | 100 | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)) || |
470 | 100 | !cmsPipelineInsertStage(ContextID, Result, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, m_as_dbl, off_as_dbl)) || |
471 | 100 | !cmsPipelineInsertStage(ContextID, Result, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID))) |
472 | 0 | return FALSE; |
473 | 100 | } |
474 | 71.0k | break; |
475 | | |
476 | 71.0k | default: |
477 | 0 | return FALSE; // Mismatch |
478 | 78.5k | } |
479 | 78.5k | break; |
480 | | |
481 | | // On colorspaces other than PCS, check for same space |
482 | 78.5k | default: |
483 | 0 | if (InPCS != OutPCS) return FALSE; |
484 | 0 | break; |
485 | 1.10M | } |
486 | | |
487 | 1.10M | return TRUE; |
488 | 1.10M | } |
489 | | |
490 | | |
491 | | // Is a given space compatible with another? |
492 | | static |
493 | | cmsBool ColorSpaceIsCompatible(cmsColorSpaceSignature a, cmsColorSpaceSignature b) |
494 | 3.46M | { |
495 | | // If they are same, they are compatible. |
496 | 3.46M | if (a == b) return TRUE; |
497 | | |
498 | | // Check for MCH4 substitution of CMYK |
499 | 643k | if ((a == cmsSig4colorData) && (b == cmsSigCmykData)) return TRUE; |
500 | 643k | if ((a == cmsSigCmykData) && (b == cmsSig4colorData)) return TRUE; |
501 | | |
502 | | // Check for XYZ/Lab. Those spaces are interchangeable as they can be computed one from other. |
503 | 643k | if ((a == cmsSigXYZData) && (b == cmsSigLabData)) return TRUE; |
504 | 635k | if ((a == cmsSigLabData) && (b == cmsSigXYZData)) return TRUE; |
505 | | |
506 | 60 | return FALSE; |
507 | 635k | } |
508 | | |
509 | | |
510 | | // Default handler for ICC-style intents |
511 | | static |
512 | | cmsPipeline* DefaultICCintents(cmsContext ContextID, |
513 | | cmsUInt32Number nProfiles, |
514 | | cmsUInt32Number TheIntents[], |
515 | | cmsHPROFILE hProfiles[], |
516 | | cmsBool BPC[], |
517 | | cmsFloat64Number AdaptationStates[], |
518 | | cmsUInt32Number dwFlags) |
519 | 2.35M | { |
520 | 2.35M | cmsPipeline* Lut = NULL; |
521 | 2.35M | cmsPipeline* Result; |
522 | 2.35M | cmsHPROFILE hProfile; |
523 | 2.35M | cmsMAT3 m; |
524 | 2.35M | cmsVEC3 off; |
525 | 2.35M | cmsColorSpaceSignature ColorSpaceIn, ColorSpaceOut = cmsSigLabData, CurrentColorSpace; |
526 | 2.35M | cmsProfileClassSignature ClassSig; |
527 | 2.35M | cmsUInt32Number i, Intent; |
528 | | |
529 | | // For safety |
530 | 2.35M | if (nProfiles == 0) return NULL; |
531 | | |
532 | | // Allocate an empty LUT for holding the result. 0 as channel count means 'undefined' |
533 | 2.35M | Result = cmsPipelineAlloc(ContextID, 0, 0); |
534 | 2.35M | if (Result == NULL) return NULL; |
535 | | |
536 | 2.35M | CurrentColorSpace = cmsGetColorSpace(ContextID, hProfiles[0]); |
537 | | |
538 | 4.51M | for (i=0; i < nProfiles; i++) { |
539 | | |
540 | 3.46M | cmsBool lIsDeviceLink, lIsInput; |
541 | | |
542 | 3.46M | hProfile = hProfiles[i]; |
543 | 3.46M | ClassSig = cmsGetDeviceClass(ContextID, hProfile); |
544 | 3.46M | lIsDeviceLink = (ClassSig == cmsSigLinkClass || ClassSig == cmsSigAbstractClass ); |
545 | | |
546 | | // First profile is used as input unless devicelink or abstract |
547 | 3.46M | if ((i == 0) && !lIsDeviceLink) { |
548 | 2.33M | lIsInput = TRUE; |
549 | 2.33M | } |
550 | 1.12M | else { |
551 | | // Else use profile in the input direction if current space is not PCS |
552 | 1.12M | lIsInput = (CurrentColorSpace != cmsSigXYZData) && |
553 | 1.12M | (CurrentColorSpace != cmsSigLabData); |
554 | 1.12M | } |
555 | | |
556 | 3.46M | Intent = TheIntents[i]; |
557 | | |
558 | 3.46M | if (lIsInput || lIsDeviceLink) { |
559 | | |
560 | 3.08M | ColorSpaceIn = cmsGetColorSpace(ContextID, hProfile); |
561 | 3.08M | ColorSpaceOut = cmsGetPCS(ContextID, hProfile); |
562 | 3.08M | } |
563 | 373k | else { |
564 | | |
565 | 373k | ColorSpaceIn = cmsGetPCS(ContextID, hProfile); |
566 | 373k | ColorSpaceOut = cmsGetColorSpace(ContextID, hProfile); |
567 | 373k | } |
568 | | |
569 | 3.46M | if (!ColorSpaceIsCompatible(ColorSpaceIn, CurrentColorSpace)) { |
570 | | |
571 | 60 | cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "ColorSpace mismatch"); |
572 | 60 | goto Error; |
573 | 60 | } |
574 | | |
575 | | // If devicelink is found, then no custom intent is allowed and we can |
576 | | // read the LUT to be applied. Settings don't apply here. |
577 | 3.46M | if (lIsDeviceLink || ((ClassSig == cmsSigNamedColorClass) && (nProfiles == 1))) { |
578 | | |
579 | | // Get the involved LUT from the profile |
580 | 733k | Lut = _cmsReadDevicelinkLUT(ContextID, hProfile, Intent); |
581 | 733k | if (Lut == NULL) goto Error; |
582 | | |
583 | | // What about abstract profiles? |
584 | 733k | if (ClassSig == cmsSigAbstractClass && i > 0) { |
585 | 709k | if (!ComputeConversion(ContextID, i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error; |
586 | 709k | } |
587 | 23.0k | else { |
588 | 23.0k | _cmsMAT3identity(ContextID, &m); |
589 | 23.0k | _cmsVEC3init(ContextID, &off, 0, 0, 0); |
590 | 23.0k | } |
591 | | |
592 | | |
593 | 733k | if (!AddConversion(ContextID, Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error; |
594 | | |
595 | 733k | } |
596 | 2.73M | else { |
597 | | |
598 | 2.73M | if (lIsInput) { |
599 | | // Input direction means non-pcs connection, so proceed like devicelinks |
600 | 2.35M | Lut = _cmsReadInputLUT(ContextID, hProfile, Intent); |
601 | 2.35M | if (Lut == NULL) goto Error; |
602 | 2.35M | } |
603 | 373k | else { |
604 | | |
605 | | // Output direction means PCS connection. Intent may apply here |
606 | 373k | Lut = _cmsReadOutputLUT(ContextID, hProfile, Intent); |
607 | 373k | if (Lut == NULL) goto Error; |
608 | | |
609 | | |
610 | 368k | if (!ComputeConversion(ContextID, i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error; |
611 | 368k | if (!AddConversion(ContextID, Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error; |
612 | | |
613 | 368k | } |
614 | 2.73M | } |
615 | | |
616 | | // Concatenate to the output LUT |
617 | 2.16M | if (!cmsPipelineCat(ContextID, Result, Lut)) |
618 | 0 | goto Error; |
619 | | |
620 | 2.16M | cmsPipelineFree(ContextID, Lut); |
621 | 2.16M | Lut = NULL; |
622 | | |
623 | | // Update current space |
624 | 2.16M | CurrentColorSpace = ColorSpaceOut; |
625 | 2.16M | } |
626 | | |
627 | | // Check for non-negatives clip |
628 | 1.05M | if (dwFlags & cmsFLAGS_NONEGATIVES) { |
629 | |
|
630 | 0 | if (ColorSpaceOut == cmsSigGrayData || |
631 | 0 | ColorSpaceOut == cmsSigRgbData || |
632 | 0 | ColorSpaceOut == cmsSigCmykData) { |
633 | |
|
634 | 0 | cmsStage* clip = _cmsStageClipNegatives(ContextID, cmsChannelsOf(ContextID, ColorSpaceOut)); |
635 | 0 | if (clip == NULL) goto Error; |
636 | | |
637 | 0 | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_END, clip)) |
638 | 0 | goto Error; |
639 | 0 | } |
640 | |
|
641 | 0 | } |
642 | | |
643 | 1.05M | return Result; |
644 | | |
645 | 1.30M | Error: |
646 | | |
647 | 1.30M | if (Lut != NULL) cmsPipelineFree(ContextID, Lut); |
648 | 1.30M | if (Result != NULL) cmsPipelineFree(ContextID, Result); |
649 | 1.30M | return NULL; |
650 | | |
651 | 0 | cmsUNUSED_PARAMETER(dwFlags); |
652 | 0 | } |
653 | | |
654 | | |
655 | | // Wrapper for DLL calling convention |
656 | | cmsPipeline* CMSEXPORT _cmsDefaultICCintents(cmsContext ContextID, |
657 | | cmsUInt32Number nProfiles, |
658 | | cmsUInt32Number TheIntents[], |
659 | | cmsHPROFILE hProfiles[], |
660 | | cmsBool BPC[], |
661 | | cmsFloat64Number AdaptationStates[], |
662 | | cmsUInt32Number dwFlags) |
663 | 0 | { |
664 | 0 | return DefaultICCintents(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags); |
665 | 0 | } |
666 | | |
667 | | // Black preserving intents --------------------------------------------------------------------------------------------- |
668 | | |
669 | | // Translate black-preserving intents to ICC ones |
670 | | static |
671 | | cmsUInt32Number TranslateNonICCIntents(cmsUInt32Number Intent) |
672 | 0 | { |
673 | 0 | switch (Intent) { |
674 | 0 | case INTENT_PRESERVE_K_ONLY_PERCEPTUAL: |
675 | 0 | case INTENT_PRESERVE_K_PLANE_PERCEPTUAL: |
676 | 0 | return INTENT_PERCEPTUAL; |
677 | | |
678 | 0 | case INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC: |
679 | 0 | case INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC: |
680 | 0 | return INTENT_RELATIVE_COLORIMETRIC; |
681 | | |
682 | 0 | case INTENT_PRESERVE_K_ONLY_SATURATION: |
683 | 0 | case INTENT_PRESERVE_K_PLANE_SATURATION: |
684 | 0 | return INTENT_SATURATION; |
685 | | |
686 | 0 | default: return Intent; |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | | // Sampler for Black-only preserving CMYK->CMYK transforms |
691 | | |
692 | | typedef struct { |
693 | | cmsPipeline* cmyk2cmyk; // The original transform |
694 | | cmsToneCurve* KTone; // Black-to-black tone curve |
695 | | |
696 | | } GrayOnlyParams; |
697 | | |
698 | | |
699 | | // Preserve black only if that is the only ink used |
700 | | static |
701 | | int BlackPreservingGrayOnlySampler(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo) |
702 | 0 | { |
703 | 0 | GrayOnlyParams* bp = (GrayOnlyParams*) Cargo; |
704 | | |
705 | | // If going across black only, keep black only |
706 | 0 | if (In[0] == 0 && In[1] == 0 && In[2] == 0) { |
707 | | |
708 | | // TAC does not apply because it is black ink! |
709 | 0 | Out[0] = Out[1] = Out[2] = 0; |
710 | 0 | Out[3] = cmsEvalToneCurve16(ContextID, bp->KTone, In[3]); |
711 | 0 | return TRUE; |
712 | 0 | } |
713 | | |
714 | | // Keep normal transform for other colors |
715 | 0 | bp ->cmyk2cmyk ->Eval16Fn(ContextID, In, Out, bp ->cmyk2cmyk->Data); |
716 | 0 | return TRUE; |
717 | 0 | } |
718 | | |
719 | | // This is the entry for black-preserving K-only intents, which are non-ICC |
720 | | static |
721 | | cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID, |
722 | | cmsUInt32Number nProfiles, |
723 | | cmsUInt32Number TheIntents[], |
724 | | cmsHPROFILE hProfiles[], |
725 | | cmsBool BPC[], |
726 | | cmsFloat64Number AdaptationStates[], |
727 | | cmsUInt32Number dwFlags) |
728 | 0 | { |
729 | 0 | GrayOnlyParams bp; |
730 | 0 | cmsPipeline* Result; |
731 | 0 | cmsUInt32Number ICCIntents[256]; |
732 | 0 | cmsStage* CLUT; |
733 | 0 | cmsUInt32Number i, nGridPoints; |
734 | 0 | cmsUInt32Number lastProfilePos; |
735 | 0 | cmsUInt32Number preservationProfilesCount; |
736 | 0 | cmsHPROFILE hLastProfile; |
737 | | |
738 | | |
739 | | // Sanity check |
740 | 0 | if (nProfiles < 1 || nProfiles > 255) return NULL; |
741 | | |
742 | | // Translate black-preserving intents to ICC ones |
743 | 0 | for (i=0; i < nProfiles; i++) |
744 | 0 | ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]); |
745 | | |
746 | | |
747 | | // Trim all CMYK devicelinks at the end |
748 | 0 | lastProfilePos = nProfiles - 1; |
749 | 0 | hLastProfile = hProfiles[lastProfilePos]; |
750 | |
|
751 | 0 | while (lastProfilePos > 1) |
752 | 0 | { |
753 | 0 | hLastProfile = hProfiles[--lastProfilePos]; |
754 | 0 | if (cmsGetColorSpace(ContextID, hLastProfile) != cmsSigCmykData || |
755 | 0 | cmsGetDeviceClass(ContextID, hLastProfile) != cmsSigLinkClass) |
756 | 0 | break; |
757 | 0 | } |
758 | |
|
759 | 0 | preservationProfilesCount = lastProfilePos + 1; |
760 | | |
761 | | // Check for non-cmyk profiles |
762 | 0 | if (cmsGetColorSpace(ContextID, hProfiles[0]) != cmsSigCmykData || |
763 | 0 | !(cmsGetColorSpace(ContextID, hLastProfile) == cmsSigCmykData || |
764 | 0 | cmsGetDeviceClass(ContextID, hLastProfile) == cmsSigOutputClass)) |
765 | 0 | return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags); |
766 | | |
767 | | // Allocate an empty LUT for holding the result |
768 | 0 | Result = cmsPipelineAlloc(ContextID, 4, 4); |
769 | 0 | if (Result == NULL) return NULL; |
770 | | |
771 | 0 | memset(&bp, 0, sizeof(bp)); |
772 | | |
773 | | // Create a LUT holding normal ICC transform |
774 | 0 | bp.cmyk2cmyk = DefaultICCintents(ContextID, |
775 | 0 | preservationProfilesCount, |
776 | 0 | ICCIntents, |
777 | 0 | hProfiles, |
778 | 0 | BPC, |
779 | 0 | AdaptationStates, |
780 | 0 | dwFlags); |
781 | |
|
782 | 0 | if (bp.cmyk2cmyk == NULL) goto Error; |
783 | | |
784 | | // Now, compute the tone curve |
785 | 0 | bp.KTone = _cmsBuildKToneCurve(ContextID, |
786 | 0 | 4096, |
787 | 0 | preservationProfilesCount, |
788 | 0 | ICCIntents, |
789 | 0 | hProfiles, |
790 | 0 | BPC, |
791 | 0 | AdaptationStates, |
792 | 0 | dwFlags); |
793 | |
|
794 | 0 | if (bp.KTone == NULL) goto Error; |
795 | | |
796 | | |
797 | | // How many gridpoints are we going to use? |
798 | 0 | nGridPoints = _cmsReasonableGridpointsByColorspace(ContextID, cmsSigCmykData, dwFlags); |
799 | | |
800 | | // Create the CLUT. 16 bits |
801 | 0 | CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL); |
802 | 0 | if (CLUT == NULL) goto Error; |
803 | | |
804 | | // This is the one and only MPE in this LUT |
805 | 0 | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_BEGIN, CLUT)) |
806 | 0 | goto Error; |
807 | | |
808 | | // Sample it. We cannot afford pre/post linearization this time. |
809 | 0 | if (!cmsStageSampleCLut16bit(ContextID, CLUT, BlackPreservingGrayOnlySampler, (void*) &bp, 0)) |
810 | 0 | goto Error; |
811 | | |
812 | | |
813 | | // Insert possible devicelinks at the end |
814 | 0 | for (i = lastProfilePos + 1; i < nProfiles; i++) |
815 | 0 | { |
816 | 0 | cmsPipeline* devlink = _cmsReadDevicelinkLUT(ContextID, hProfiles[i], ICCIntents[i]); |
817 | 0 | if (devlink == NULL) |
818 | 0 | goto Error; |
819 | | |
820 | 0 | if (!cmsPipelineCat(ContextID, Result, devlink)) |
821 | 0 | goto Error; |
822 | 0 | } |
823 | | |
824 | | |
825 | | // Get rid of xform and tone curve |
826 | 0 | cmsPipelineFree(ContextID, bp.cmyk2cmyk); |
827 | 0 | cmsFreeToneCurve(ContextID, bp.KTone); |
828 | |
|
829 | 0 | return Result; |
830 | | |
831 | 0 | Error: |
832 | |
|
833 | 0 | if (bp.cmyk2cmyk != NULL) cmsPipelineFree(ContextID, bp.cmyk2cmyk); |
834 | 0 | if (bp.KTone != NULL) cmsFreeToneCurve(ContextID, bp.KTone); |
835 | 0 | if (Result != NULL) cmsPipelineFree(ContextID, Result); |
836 | 0 | return NULL; |
837 | |
|
838 | 0 | } |
839 | | |
840 | | // K Plane-preserving CMYK to CMYK ------------------------------------------------------------------------------------ |
841 | | |
842 | | typedef struct { |
843 | | |
844 | | cmsPipeline* cmyk2cmyk; // The original transform |
845 | | cmsHTRANSFORM hProofOutput; // Output CMYK to Lab (last profile) |
846 | | cmsHTRANSFORM cmyk2Lab; // The input chain |
847 | | cmsToneCurve* KTone; // Black-to-black tone curve |
848 | | cmsPipeline* LabK2cmyk; // The output profile |
849 | | cmsFloat64Number MaxError; |
850 | | |
851 | | cmsHTRANSFORM hRoundTrip; |
852 | | cmsFloat64Number MaxTAC; |
853 | | |
854 | | |
855 | | } PreserveKPlaneParams; |
856 | | |
857 | | |
858 | | // The CLUT will be stored at 16 bits, but calculations are performed at cmsFloat32Number precision |
859 | | static |
860 | | int BlackPreservingSampler(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo) |
861 | 0 | { |
862 | 0 | int i; |
863 | 0 | cmsFloat32Number Inf[4], Outf[4]; |
864 | 0 | cmsFloat32Number LabK[4]; |
865 | 0 | cmsFloat64Number SumCMY, SumCMYK, Error, Ratio; |
866 | 0 | cmsCIELab ColorimetricLab, BlackPreservingLab; |
867 | 0 | PreserveKPlaneParams* bp = (PreserveKPlaneParams*) Cargo; |
868 | | |
869 | | // Convert from 16 bits to floating point |
870 | 0 | for (i=0; i < 4; i++) |
871 | 0 | Inf[i] = (cmsFloat32Number) (In[i] / 65535.0); |
872 | | |
873 | | // Get the K across Tone curve |
874 | 0 | LabK[3] = cmsEvalToneCurveFloat(ContextID, bp ->KTone, Inf[3]); |
875 | | |
876 | | // If going across black only, keep black only |
877 | 0 | if (In[0] == 0 && In[1] == 0 && In[2] == 0) { |
878 | |
|
879 | 0 | Out[0] = Out[1] = Out[2] = 0; |
880 | 0 | Out[3] = _cmsQuickSaturateWord(LabK[3] * 65535.0); |
881 | 0 | return TRUE; |
882 | 0 | } |
883 | | |
884 | | // Try the original transform, |
885 | 0 | cmsPipelineEvalFloat(ContextID, Inf, Outf, bp ->cmyk2cmyk); |
886 | | |
887 | | // Store a copy of the floating point result into 16-bit |
888 | 0 | for (i=0; i < 4; i++) |
889 | 0 | Out[i] = _cmsQuickSaturateWord(Outf[i] * 65535.0); |
890 | | |
891 | | // Maybe K is already ok (mostly on K=0) |
892 | 0 | if (fabsf(Outf[3] - LabK[3]) < (3.0 / 65535.0)) { |
893 | 0 | return TRUE; |
894 | 0 | } |
895 | | |
896 | | // K differ, measure and keep Lab measurement for further usage |
897 | | // this is done in relative colorimetric intent |
898 | 0 | cmsDoTransform(ContextID, bp->hProofOutput, Out, &ColorimetricLab, 1); |
899 | | |
900 | | // Is not black only and the transform doesn't keep black. |
901 | | // Obtain the Lab of output CMYK. After that we have Lab + K |
902 | 0 | cmsDoTransform(ContextID, bp ->cmyk2Lab, Outf, LabK, 1); |
903 | | |
904 | | // Obtain the corresponding CMY using reverse interpolation |
905 | | // (K is fixed in LabK[3]) |
906 | 0 | if (!cmsPipelineEvalReverseFloat(ContextID, LabK, Outf, Outf, bp ->LabK2cmyk)) { |
907 | | |
908 | | // Cannot find a suitable value, so use colorimetric xform |
909 | | // which is already stored in Out[] |
910 | 0 | return TRUE; |
911 | 0 | } |
912 | | |
913 | | // Make sure to pass through K (which now is fixed) |
914 | 0 | Outf[3] = LabK[3]; |
915 | | |
916 | | // Apply TAC if needed |
917 | 0 | SumCMY = (cmsFloat64Number) Outf[0] + Outf[1] + Outf[2]; |
918 | 0 | SumCMYK = SumCMY + Outf[3]; |
919 | |
|
920 | 0 | if (SumCMYK > bp ->MaxTAC) { |
921 | |
|
922 | 0 | Ratio = 1 - ((SumCMYK - bp->MaxTAC) / SumCMY); |
923 | 0 | if (Ratio < 0) |
924 | 0 | Ratio = 0; |
925 | 0 | } |
926 | 0 | else |
927 | 0 | Ratio = 1.0; |
928 | |
|
929 | 0 | Out[0] = _cmsQuickSaturateWord(Outf[0] * Ratio * 65535.0); // C |
930 | 0 | Out[1] = _cmsQuickSaturateWord(Outf[1] * Ratio * 65535.0); // M |
931 | 0 | Out[2] = _cmsQuickSaturateWord(Outf[2] * Ratio * 65535.0); // Y |
932 | 0 | Out[3] = _cmsQuickSaturateWord(Outf[3] * 65535.0); |
933 | | |
934 | | // Estimate the error (this goes 16 bits to Lab DBL) |
935 | 0 | cmsDoTransform(ContextID, bp->hProofOutput, Out, &BlackPreservingLab, 1); |
936 | 0 | Error = cmsDeltaE(ContextID, &ColorimetricLab, &BlackPreservingLab); |
937 | 0 | if (Error > bp -> MaxError) |
938 | 0 | bp->MaxError = Error; |
939 | |
|
940 | 0 | return TRUE; |
941 | 0 | } |
942 | | |
943 | | |
944 | | |
945 | | // This is the entry for black-plane preserving, which are non-ICC |
946 | | static |
947 | | cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID, |
948 | | cmsUInt32Number nProfiles, |
949 | | cmsUInt32Number TheIntents[], |
950 | | cmsHPROFILE hProfiles[], |
951 | | cmsBool BPC[], |
952 | | cmsFloat64Number AdaptationStates[], |
953 | | cmsUInt32Number dwFlags) |
954 | 0 | { |
955 | 0 | PreserveKPlaneParams bp; |
956 | |
|
957 | 0 | cmsPipeline* Result = NULL; |
958 | 0 | cmsUInt32Number ICCIntents[256]; |
959 | 0 | cmsStage* CLUT; |
960 | 0 | cmsUInt32Number i, nGridPoints; |
961 | 0 | cmsUInt32Number lastProfilePos; |
962 | 0 | cmsUInt32Number preservationProfilesCount; |
963 | 0 | cmsHPROFILE hLastProfile; |
964 | 0 | cmsHPROFILE hLab; |
965 | | |
966 | | // Sanity check |
967 | 0 | if (nProfiles < 1 || nProfiles > 255) return NULL; |
968 | | |
969 | | // Translate black-preserving intents to ICC ones |
970 | 0 | for (i=0; i < nProfiles; i++) |
971 | 0 | ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]); |
972 | | |
973 | | // Trim all CMYK devicelinks at the end |
974 | 0 | lastProfilePos = nProfiles - 1; |
975 | 0 | hLastProfile = hProfiles[lastProfilePos]; |
976 | |
|
977 | 0 | while (lastProfilePos > 1) |
978 | 0 | { |
979 | 0 | hLastProfile = hProfiles[--lastProfilePos]; |
980 | 0 | if (cmsGetColorSpace(ContextID, hLastProfile) != cmsSigCmykData || |
981 | 0 | cmsGetDeviceClass(ContextID, hLastProfile) != cmsSigLinkClass) |
982 | 0 | break; |
983 | 0 | } |
984 | |
|
985 | 0 | preservationProfilesCount = lastProfilePos + 1; |
986 | | |
987 | | // Check for non-cmyk profiles |
988 | 0 | if (cmsGetColorSpace(ContextID, hProfiles[0]) != cmsSigCmykData || |
989 | 0 | !(cmsGetColorSpace(ContextID, hLastProfile) == cmsSigCmykData || |
990 | 0 | cmsGetDeviceClass(ContextID, hLastProfile) == cmsSigOutputClass)) |
991 | 0 | return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags); |
992 | | |
993 | | // Allocate an empty LUT for holding the result |
994 | 0 | Result = cmsPipelineAlloc(ContextID, 4, 4); |
995 | 0 | if (Result == NULL) return NULL; |
996 | | |
997 | 0 | memset(&bp, 0, sizeof(bp)); |
998 | | |
999 | | // We need the input LUT of the last profile, assuming this one is responsible of |
1000 | | // black generation. This LUT will be searched in inverse order. |
1001 | 0 | bp.LabK2cmyk = _cmsReadInputLUT(ContextID, hLastProfile, INTENT_RELATIVE_COLORIMETRIC); |
1002 | 0 | if (bp.LabK2cmyk == NULL) goto Cleanup; |
1003 | | |
1004 | | // Get total area coverage (in 0..1 domain) |
1005 | 0 | bp.MaxTAC = cmsDetectTAC(ContextID, hLastProfile) / 100.0; |
1006 | 0 | if (bp.MaxTAC <= 0) goto Cleanup; |
1007 | | |
1008 | | |
1009 | | // Create a LUT holding normal ICC transform |
1010 | 0 | bp.cmyk2cmyk = DefaultICCintents(ContextID, |
1011 | 0 | preservationProfilesCount, |
1012 | 0 | ICCIntents, |
1013 | 0 | hProfiles, |
1014 | 0 | BPC, |
1015 | 0 | AdaptationStates, |
1016 | 0 | dwFlags); |
1017 | 0 | if (bp.cmyk2cmyk == NULL) goto Cleanup; |
1018 | | |
1019 | | // Now the tone curve |
1020 | 0 | bp.KTone = _cmsBuildKToneCurve(ContextID, 4096, preservationProfilesCount, |
1021 | 0 | ICCIntents, |
1022 | 0 | hProfiles, |
1023 | 0 | BPC, |
1024 | 0 | AdaptationStates, |
1025 | 0 | dwFlags); |
1026 | 0 | if (bp.KTone == NULL) goto Cleanup; |
1027 | | |
1028 | | // To measure the output, Last profile to Lab |
1029 | 0 | hLab = cmsCreateLab4Profile(ContextID, NULL); |
1030 | 0 | bp.hProofOutput = cmsCreateTransform(ContextID, hLastProfile, |
1031 | 0 | CHANNELS_SH(4)|BYTES_SH(2), hLab, TYPE_Lab_DBL, |
1032 | 0 | INTENT_RELATIVE_COLORIMETRIC, |
1033 | 0 | cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE); |
1034 | 0 | if ( bp.hProofOutput == NULL) goto Cleanup; |
1035 | | |
1036 | | // Same as anterior, but lab in the 0..1 range |
1037 | 0 | bp.cmyk2Lab = cmsCreateTransform(ContextID, hLastProfile, |
1038 | 0 | FLOAT_SH(1)|CHANNELS_SH(4)|BYTES_SH(4), hLab, |
1039 | 0 | FLOAT_SH(1)|CHANNELS_SH(3)|BYTES_SH(4), |
1040 | 0 | INTENT_RELATIVE_COLORIMETRIC, |
1041 | 0 | cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE); |
1042 | 0 | if (bp.cmyk2Lab == NULL) goto Cleanup; |
1043 | 0 | cmsCloseProfile(ContextID, hLab); |
1044 | | |
1045 | | // Error estimation (for debug only) |
1046 | 0 | bp.MaxError = 0; |
1047 | | |
1048 | | // How many gridpoints are we going to use? |
1049 | 0 | nGridPoints = _cmsReasonableGridpointsByColorspace(ContextID, cmsSigCmykData, dwFlags); |
1050 | | |
1051 | |
|
1052 | 0 | CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL); |
1053 | 0 | if (CLUT == NULL) goto Cleanup; |
1054 | | |
1055 | 0 | if (!cmsPipelineInsertStage(ContextID, Result, cmsAT_BEGIN, CLUT)) |
1056 | 0 | goto Cleanup; |
1057 | | |
1058 | 0 | cmsStageSampleCLut16bit(ContextID, CLUT, BlackPreservingSampler, (void*) &bp, 0); |
1059 | | |
1060 | | // Insert possible devicelinks at the end |
1061 | 0 | for (i = lastProfilePos + 1; i < nProfiles; i++) |
1062 | 0 | { |
1063 | 0 | cmsPipeline* devlink = _cmsReadDevicelinkLUT(ContextID, hProfiles[i], ICCIntents[i]); |
1064 | 0 | if (devlink == NULL) |
1065 | 0 | goto Cleanup; |
1066 | | |
1067 | 0 | if (!cmsPipelineCat(ContextID, Result, devlink)) |
1068 | 0 | goto Cleanup; |
1069 | 0 | } |
1070 | | |
1071 | | |
1072 | 0 | Cleanup: |
1073 | |
|
1074 | 0 | if (bp.cmyk2cmyk) cmsPipelineFree(ContextID, bp.cmyk2cmyk); |
1075 | 0 | if (bp.cmyk2Lab) cmsDeleteTransform(ContextID, bp.cmyk2Lab); |
1076 | 0 | if (bp.hProofOutput) cmsDeleteTransform(ContextID, bp.hProofOutput); |
1077 | |
|
1078 | 0 | if (bp.KTone) cmsFreeToneCurve(ContextID, bp.KTone); |
1079 | 0 | if (bp.LabK2cmyk) cmsPipelineFree(ContextID, bp.LabK2cmyk); |
1080 | |
|
1081 | 0 | return Result; |
1082 | 0 | } |
1083 | | |
1084 | | |
1085 | | |
1086 | | // Link routines ------------------------------------------------------------------------------------------------------ |
1087 | | |
1088 | | // Chain several profiles into a single LUT. It just checks the parameters and then calls the handler |
1089 | | // for the first intent in chain. The handler may be user-defined. Is up to the handler to deal with the |
1090 | | // rest of intents in chain. A maximum of 255 profiles at time are supported, which is pretty reasonable. |
1091 | | cmsPipeline* _cmsLinkProfiles(cmsContext ContextID, |
1092 | | cmsUInt32Number nProfiles, |
1093 | | cmsUInt32Number TheIntents[], |
1094 | | cmsHPROFILE hProfiles[], |
1095 | | cmsBool BPC[], |
1096 | | cmsFloat64Number AdaptationStates[], |
1097 | | cmsUInt32Number dwFlags) |
1098 | 2.35M | { |
1099 | 2.35M | cmsUInt32Number i; |
1100 | 2.35M | cmsIntentsList* Intent; |
1101 | | |
1102 | | // Make sure a reasonable number of profiles is provided |
1103 | 2.35M | if (nProfiles <= 0 || nProfiles > 255) { |
1104 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't link '%d' profiles", nProfiles); |
1105 | 0 | return NULL; |
1106 | 0 | } |
1107 | | |
1108 | 7.11M | for (i=0; i < nProfiles; i++) { |
1109 | | |
1110 | | // Check if black point is really needed or allowed. Note that |
1111 | | // following Adobe's document: |
1112 | | // BPC does not apply to devicelink profiles, nor to abs colorimetric, |
1113 | | // and applies always on V4 perceptual and saturation. |
1114 | | |
1115 | 4.75M | if (TheIntents[i] == INTENT_ABSOLUTE_COLORIMETRIC) |
1116 | 288k | BPC[i] = FALSE; |
1117 | | |
1118 | 4.75M | if (TheIntents[i] == INTENT_PERCEPTUAL || TheIntents[i] == INTENT_SATURATION) { |
1119 | | |
1120 | | // Force BPC for V4 profiles in perceptual and saturation |
1121 | 681k | if (cmsGetEncodedICCversion(ContextID, hProfiles[i]) >= 0x4000000) |
1122 | 6.81k | BPC[i] = TRUE; |
1123 | 681k | } |
1124 | 4.75M | } |
1125 | | |
1126 | | // Search for a handler. The first intent in the chain defines the handler. That would |
1127 | | // prevent using multiple custom intents in a multiintent chain, but the behaviour of |
1128 | | // this case would present some issues if the custom intent tries to do things like |
1129 | | // preserve primaries. This solution is not perfect, but works well on most cases. |
1130 | | |
1131 | 2.35M | Intent = SearchIntent(ContextID, TheIntents[0]); |
1132 | 2.35M | if (Intent == NULL) { |
1133 | 0 | cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported intent '%d'", TheIntents[0]); |
1134 | 0 | return NULL; |
1135 | 0 | } |
1136 | | |
1137 | | // Call the handler |
1138 | 2.35M | return Intent ->Link(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags); |
1139 | 2.35M | } |
1140 | | |
1141 | | // ------------------------------------------------------------------------------------------------- |
1142 | | |
1143 | | // Get information about available intents. nMax is the maximum space for the supplied "Codes" |
1144 | | // and "Descriptions" the function returns the total number of intents, which may be greater |
1145 | | // than nMax, although the matrices are not populated beyond this level. |
1146 | | cmsUInt32Number CMSEXPORT cmsGetSupportedIntents(cmsContext ContextID, cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions) |
1147 | 0 | { |
1148 | 0 | _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(ContextID, IntentPlugin); |
1149 | 0 | cmsIntentsList* pt; |
1150 | 0 | cmsUInt32Number nIntents; |
1151 | | |
1152 | |
|
1153 | 0 | for (nIntents=0, pt = ctx->Intents; pt != NULL; pt = pt -> Next) |
1154 | 0 | { |
1155 | 0 | if (nIntents < nMax) { |
1156 | 0 | if (Codes != NULL) |
1157 | 0 | Codes[nIntents] = pt ->Intent; |
1158 | |
|
1159 | 0 | if (Descriptions != NULL) |
1160 | 0 | Descriptions[nIntents] = pt ->Description; |
1161 | 0 | } |
1162 | |
|
1163 | 0 | nIntents++; |
1164 | 0 | } |
1165 | |
|
1166 | 0 | for (nIntents=0, pt = DefaultIntents; pt != NULL; pt = pt -> Next) |
1167 | 0 | { |
1168 | 0 | if (nIntents < nMax) { |
1169 | 0 | if (Codes != NULL) |
1170 | 0 | Codes[nIntents] = pt ->Intent; |
1171 | |
|
1172 | 0 | if (Descriptions != NULL) |
1173 | 0 | Descriptions[nIntents] = pt ->Description; |
1174 | 0 | } |
1175 | |
|
1176 | 0 | nIntents++; |
1177 | 0 | } |
1178 | 0 | return nIntents; |
1179 | 0 | } |
1180 | | |
1181 | | // The plug-in registration. User can add new intents or override default routines |
1182 | | cmsBool _cmsRegisterRenderingIntentPlugin(cmsContext id, cmsPluginBase* Data) |
1183 | 162k | { |
1184 | 162k | _cmsIntentsPluginChunkType* ctx = ( _cmsIntentsPluginChunkType*) _cmsContextGetClientChunk(id, IntentPlugin); |
1185 | 162k | cmsPluginRenderingIntent* Plugin = (cmsPluginRenderingIntent*) Data; |
1186 | 162k | cmsIntentsList* fl; |
1187 | | |
1188 | | // Do we have to reset the custom intents? |
1189 | 162k | if (Data == NULL) { |
1190 | | |
1191 | 162k | ctx->Intents = NULL; |
1192 | 162k | return TRUE; |
1193 | 162k | } |
1194 | | |
1195 | 0 | fl = (cmsIntentsList*) _cmsPluginMalloc(id, sizeof(cmsIntentsList)); |
1196 | 0 | if (fl == NULL) return FALSE; |
1197 | | |
1198 | | |
1199 | 0 | fl ->Intent = Plugin ->Intent; |
1200 | 0 | strncpy(fl ->Description, Plugin ->Description, sizeof(fl ->Description)-1); |
1201 | 0 | fl ->Description[sizeof(fl ->Description)-1] = 0; |
1202 | |
|
1203 | 0 | fl ->Link = Plugin ->Link; |
1204 | |
|
1205 | 0 | fl ->Next = ctx ->Intents; |
1206 | 0 | ctx ->Intents = fl; |
1207 | |
|
1208 | 0 | return TRUE; |
1209 | 0 | } |