/src/ghostpdl/lcms2mt/src/cmsgmt.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 | | // Auxiliary: append a Lab identity after the given sequence of profiles |
31 | | // and return the transform. Lab profile is closed, rest of profiles are kept open. |
32 | | cmsHTRANSFORM _cmsChain2Lab(cmsContext ContextID, |
33 | | cmsUInt32Number nProfiles, |
34 | | cmsUInt32Number InputFormat, |
35 | | cmsUInt32Number OutputFormat, |
36 | | const cmsUInt32Number Intents[], |
37 | | const cmsHPROFILE hProfiles[], |
38 | | const cmsBool BPC[], |
39 | | const cmsFloat64Number AdaptationStates[], |
40 | | cmsUInt32Number dwFlags) |
41 | 0 | { |
42 | 0 | cmsHTRANSFORM xform; |
43 | 0 | cmsHPROFILE hLab; |
44 | 0 | cmsHPROFILE ProfileList[256]; |
45 | 0 | cmsBool BPCList[256]; |
46 | 0 | cmsFloat64Number AdaptationList[256]; |
47 | 0 | cmsUInt32Number IntentList[256]; |
48 | 0 | cmsUInt32Number i; |
49 | | |
50 | | // This is a rather big number and there is no need of dynamic memory |
51 | | // since we are adding a profile, 254 + 1 = 255 and this is the limit |
52 | 0 | if (nProfiles > 254) return NULL; |
53 | | |
54 | | // The output space |
55 | 0 | hLab = cmsCreateLab4Profile(ContextID, NULL); |
56 | 0 | if (hLab == NULL) return NULL; |
57 | | |
58 | | // Create a copy of parameters |
59 | 0 | for (i=0; i < nProfiles; i++) { |
60 | |
|
61 | 0 | ProfileList[i] = hProfiles[i]; |
62 | 0 | BPCList[i] = BPC[i]; |
63 | 0 | AdaptationList[i] = AdaptationStates[i]; |
64 | 0 | IntentList[i] = Intents[i]; |
65 | 0 | } |
66 | | |
67 | | // Place Lab identity at chain's end. |
68 | 0 | ProfileList[nProfiles] = hLab; |
69 | 0 | BPCList[nProfiles] = 0; |
70 | 0 | AdaptationList[nProfiles] = 1.0; |
71 | 0 | IntentList[nProfiles] = INTENT_RELATIVE_COLORIMETRIC; |
72 | | |
73 | | // Create the transform |
74 | 0 | xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList, |
75 | 0 | BPCList, |
76 | 0 | IntentList, |
77 | 0 | AdaptationList, |
78 | 0 | NULL, 0, |
79 | 0 | InputFormat, |
80 | 0 | OutputFormat, |
81 | 0 | dwFlags); |
82 | |
|
83 | 0 | cmsCloseProfile(ContextID, hLab); |
84 | |
|
85 | 0 | return xform; |
86 | 0 | } |
87 | | |
88 | | |
89 | | // Compute K -> L* relationship. Flags may include black point compensation. In this case, |
90 | | // the relationship is assumed from the profile with BPC to a black point zero. |
91 | | static |
92 | | cmsToneCurve* ComputeKToLstar(cmsContext ContextID, |
93 | | cmsUInt32Number nPoints, |
94 | | cmsUInt32Number nProfiles, |
95 | | const cmsUInt32Number Intents[], |
96 | | const cmsHPROFILE hProfiles[], |
97 | | const cmsBool BPC[], |
98 | | const cmsFloat64Number AdaptationStates[], |
99 | | cmsUInt32Number dwFlags) |
100 | 0 | { |
101 | 0 | cmsToneCurve* out = NULL; |
102 | 0 | cmsUInt32Number i; |
103 | 0 | cmsHTRANSFORM xform; |
104 | 0 | cmsCIELab Lab; |
105 | 0 | cmsFloat32Number cmyk[4]; |
106 | 0 | cmsFloat32Number* SampledPoints; |
107 | |
|
108 | 0 | xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags); |
109 | 0 | if (xform == NULL) return NULL; |
110 | | |
111 | 0 | SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number)); |
112 | 0 | if (SampledPoints == NULL) goto Error; |
113 | | |
114 | 0 | for (i=0; i < nPoints; i++) { |
115 | |
|
116 | 0 | cmyk[0] = 0; |
117 | 0 | cmyk[1] = 0; |
118 | 0 | cmyk[2] = 0; |
119 | 0 | cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1)); |
120 | |
|
121 | 0 | cmsDoTransform(ContextID, xform, cmyk, &Lab, 1); |
122 | 0 | SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation |
123 | 0 | } |
124 | |
|
125 | 0 | out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints); |
126 | |
|
127 | 0 | Error: |
128 | |
|
129 | 0 | cmsDeleteTransform(ContextID, xform); |
130 | 0 | if (SampledPoints) _cmsFree(ContextID, SampledPoints); |
131 | |
|
132 | 0 | return out; |
133 | 0 | } |
134 | | |
135 | | |
136 | | // Compute Black tone curve on a CMYK -> CMYK transform. This is done by |
137 | | // using the proof direction on both profiles to find K->L* relationship |
138 | | // then joining both curves. dwFlags may include black point compensation. |
139 | | cmsToneCurve* _cmsBuildKToneCurve(cmsContext ContextID, |
140 | | cmsUInt32Number nPoints, |
141 | | cmsUInt32Number nProfiles, |
142 | | const cmsUInt32Number Intents[], |
143 | | const cmsHPROFILE hProfiles[], |
144 | | const cmsBool BPC[], |
145 | | const cmsFloat64Number AdaptationStates[], |
146 | | cmsUInt32Number dwFlags) |
147 | 0 | { |
148 | 0 | cmsToneCurve *in, *out, *KTone; |
149 | | |
150 | | // Make sure CMYK -> CMYK |
151 | 0 | if (cmsGetColorSpace(ContextID, hProfiles[0]) != cmsSigCmykData || |
152 | 0 | cmsGetColorSpace(ContextID, hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL; |
153 | | |
154 | | |
155 | | // Make sure last is an output profile |
156 | 0 | if (cmsGetDeviceClass(ContextID, hProfiles[nProfiles - 1]) != cmsSigOutputClass) return NULL; |
157 | | |
158 | | // Create individual curves. BPC works also as each K to L* is |
159 | | // computed as a BPC to zero black point in case of L* |
160 | 0 | in = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags); |
161 | 0 | if (in == NULL) return NULL; |
162 | | |
163 | 0 | out = ComputeKToLstar(ContextID, nPoints, 1, |
164 | 0 | Intents + (nProfiles - 1), |
165 | 0 | &hProfiles [nProfiles - 1], |
166 | 0 | BPC + (nProfiles - 1), |
167 | 0 | AdaptationStates + (nProfiles - 1), |
168 | 0 | dwFlags); |
169 | 0 | if (out == NULL) { |
170 | 0 | cmsFreeToneCurve(ContextID, in); |
171 | 0 | return NULL; |
172 | 0 | } |
173 | | |
174 | | // Build the relationship. This effectively limits the maximum accuracy to 16 bits, but |
175 | | // since this is used on black-preserving LUTs, we are not losing accuracy in any case |
176 | 0 | KTone = cmsJoinToneCurve(ContextID, in, out, nPoints); |
177 | | |
178 | | // Get rid of components |
179 | 0 | cmsFreeToneCurve(ContextID, in); cmsFreeToneCurve(ContextID, out); |
180 | | |
181 | | // Something went wrong... |
182 | 0 | if (KTone == NULL) return NULL; |
183 | | |
184 | | // Make sure it is monotonic |
185 | 0 | if (!cmsIsToneCurveMonotonic(ContextID, KTone)) { |
186 | 0 | cmsFreeToneCurve(ContextID, KTone); |
187 | 0 | return NULL; |
188 | 0 | } |
189 | | |
190 | 0 | return KTone; |
191 | 0 | } |
192 | | |
193 | | |
194 | | // Gamut LUT Creation ----------------------------------------------------------------------------------------- |
195 | | |
196 | | // Used by gamut & softproofing |
197 | | |
198 | | typedef struct { |
199 | | |
200 | | cmsHTRANSFORM hInput; // From whatever input color space. 16 bits to DBL |
201 | | cmsHTRANSFORM hForward, hReverse; // Transforms going from Lab to colorant and back |
202 | | cmsFloat64Number Thereshold; // The thereshold after which is considered out of gamut |
203 | | |
204 | | } GAMUTCHAIN; |
205 | | |
206 | | // This sampler does compute gamut boundaries by comparing original |
207 | | // values with a transform going back and forth. Values above ERR_THERESHOLD |
208 | | // of maximum are considered out of gamut. |
209 | | |
210 | 0 | #define ERR_THERESHOLD 5 |
211 | | |
212 | | |
213 | | static |
214 | | int GamutSampler(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo) |
215 | 0 | { |
216 | 0 | GAMUTCHAIN* t = (GAMUTCHAIN* ) Cargo; |
217 | 0 | cmsCIELab LabIn1, LabOut1; |
218 | 0 | cmsCIELab LabIn2, LabOut2; |
219 | 0 | cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS]; |
220 | 0 | cmsFloat64Number dE1, dE2, ErrorRatio; |
221 | | |
222 | | // Assume in-gamut by default. |
223 | 0 | ErrorRatio = 1.0; |
224 | | |
225 | | // Convert input to Lab |
226 | 0 | cmsDoTransform(ContextID, t -> hInput, In, &LabIn1, 1); |
227 | | |
228 | | // converts from PCS to colorant. This always |
229 | | // does return in-gamut values, |
230 | 0 | cmsDoTransform(ContextID, t -> hForward, &LabIn1, Proof, 1); |
231 | | |
232 | | // Now, do the inverse, from colorant to PCS. |
233 | 0 | cmsDoTransform(ContextID, t -> hReverse, Proof, &LabOut1, 1); |
234 | |
|
235 | 0 | memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab)); |
236 | | |
237 | | // Try again, but this time taking Check as input |
238 | 0 | cmsDoTransform(ContextID, t -> hForward, &LabOut1, Proof2, 1); |
239 | 0 | cmsDoTransform(ContextID, t -> hReverse, Proof2, &LabOut2, 1); |
240 | | |
241 | | // Take difference of direct value |
242 | 0 | dE1 = cmsDeltaE(ContextID, &LabIn1, &LabOut1); |
243 | | |
244 | | // Take difference of converted value |
245 | 0 | dE2 = cmsDeltaE(ContextID, &LabIn2, &LabOut2); |
246 | | |
247 | | |
248 | | // if dE1 is small and dE2 is small, value is likely to be in gamut |
249 | 0 | if (dE1 < t->Thereshold && dE2 < t->Thereshold) |
250 | 0 | Out[0] = 0; |
251 | 0 | else { |
252 | | |
253 | | // if dE1 is small and dE2 is big, undefined. Assume in gamut |
254 | 0 | if (dE1 < t->Thereshold && dE2 > t->Thereshold) |
255 | 0 | Out[0] = 0; |
256 | 0 | else |
257 | | // dE1 is big and dE2 is small, clearly out of gamut |
258 | 0 | if (dE1 > t->Thereshold && dE2 < t->Thereshold) |
259 | 0 | Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Thereshold) + .5); |
260 | 0 | else { |
261 | | |
262 | | // dE1 is big and dE2 is also big, could be due to perceptual mapping |
263 | | // so take error ratio |
264 | 0 | if (dE2 == 0.0) |
265 | 0 | ErrorRatio = dE1; |
266 | 0 | else |
267 | 0 | ErrorRatio = dE1 / dE2; |
268 | |
|
269 | 0 | if (ErrorRatio > t->Thereshold) |
270 | 0 | Out[0] = (cmsUInt16Number) _cmsQuickFloor((ErrorRatio - t->Thereshold) + .5); |
271 | 0 | else |
272 | 0 | Out[0] = 0; |
273 | 0 | } |
274 | 0 | } |
275 | | |
276 | |
|
277 | 0 | return TRUE; |
278 | 0 | } |
279 | | |
280 | | // Does compute a gamut LUT going back and forth across pcs -> relativ. colorimetric intent -> pcs |
281 | | // the dE obtained is then annotated on the LUT. Values truly out of gamut are clipped to dE = 0xFFFE |
282 | | // and values changed are supposed to be handled by any gamut remapping, so, are out of gamut as well. |
283 | | // |
284 | | // **WARNING: This algorithm does assume that gamut remapping algorithms does NOT move in-gamut colors, |
285 | | // of course, many perceptual and saturation intents does not work in such way, but relativ. ones should. |
286 | | |
287 | | cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID, |
288 | | cmsHPROFILE hProfiles[], |
289 | | cmsBool BPC[], |
290 | | cmsUInt32Number Intents[], |
291 | | cmsFloat64Number AdaptationStates[], |
292 | | cmsUInt32Number nGamutPCSposition, |
293 | | cmsHPROFILE hGamut) |
294 | 0 | { |
295 | 0 | cmsHPROFILE hLab; |
296 | 0 | cmsPipeline* Gamut; |
297 | 0 | cmsStage* CLUT; |
298 | 0 | cmsUInt32Number dwFormat; |
299 | 0 | GAMUTCHAIN Chain; |
300 | 0 | cmsUInt32Number nChannels, nGridpoints; |
301 | 0 | cmsColorSpaceSignature ColorSpace; |
302 | 0 | cmsUInt32Number i; |
303 | 0 | cmsHPROFILE ProfileList[256]; |
304 | 0 | cmsBool BPCList[256]; |
305 | 0 | cmsFloat64Number AdaptationList[256]; |
306 | 0 | cmsUInt32Number IntentList[256]; |
307 | |
|
308 | 0 | memset(&Chain, 0, sizeof(GAMUTCHAIN)); |
309 | | |
310 | |
|
311 | 0 | if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) { |
312 | 0 | cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition); |
313 | 0 | return NULL; |
314 | 0 | } |
315 | | |
316 | 0 | hLab = cmsCreateLab4Profile(ContextID, NULL); |
317 | 0 | if (hLab == NULL) return NULL; |
318 | | |
319 | | |
320 | | // The figure of merit. On matrix-shaper profiles, should be almost zero as |
321 | | // the conversion is pretty exact. On LUT based profiles, different resolutions |
322 | | // of input and output CLUT may result in differences. |
323 | | |
324 | 0 | if (cmsIsMatrixShaper(ContextID, hGamut)) { |
325 | |
|
326 | 0 | Chain.Thereshold = 1.0; |
327 | 0 | } |
328 | 0 | else { |
329 | 0 | Chain.Thereshold = ERR_THERESHOLD; |
330 | 0 | } |
331 | | |
332 | | |
333 | | // Create a copy of parameters |
334 | 0 | for (i=0; i < nGamutPCSposition; i++) { |
335 | 0 | ProfileList[i] = hProfiles[i]; |
336 | 0 | BPCList[i] = BPC[i]; |
337 | 0 | AdaptationList[i] = AdaptationStates[i]; |
338 | 0 | IntentList[i] = Intents[i]; |
339 | 0 | } |
340 | | |
341 | | // Fill Lab identity |
342 | 0 | ProfileList[nGamutPCSposition] = hLab; |
343 | 0 | BPCList[nGamutPCSposition] = 0; |
344 | 0 | AdaptationList[nGamutPCSposition] = 1.0; |
345 | 0 | IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC; |
346 | | |
347 | |
|
348 | 0 | ColorSpace = cmsGetColorSpace(ContextID, hGamut); |
349 | |
|
350 | 0 | nChannels = cmsChannelsOf(ContextID, ColorSpace); |
351 | 0 | nGridpoints = _cmsReasonableGridpointsByColorspace(ContextID, ColorSpace, cmsFLAGS_HIGHRESPRECALC); |
352 | 0 | dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2)); |
353 | | |
354 | | // 16 bits to Lab double |
355 | 0 | Chain.hInput = cmsCreateExtendedTransform(ContextID, |
356 | 0 | nGamutPCSposition + 1, |
357 | 0 | ProfileList, |
358 | 0 | BPCList, |
359 | 0 | IntentList, |
360 | 0 | AdaptationList, |
361 | 0 | NULL, 0, |
362 | 0 | dwFormat, TYPE_Lab_DBL, |
363 | 0 | cmsFLAGS_NOCACHE); |
364 | | |
365 | | |
366 | | // Does create the forward step. Lab double to device |
367 | 0 | dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2)); |
368 | 0 | Chain.hForward = cmsCreateTransform(ContextID, |
369 | 0 | hLab, TYPE_Lab_DBL, |
370 | 0 | hGamut, dwFormat, |
371 | 0 | INTENT_RELATIVE_COLORIMETRIC, |
372 | 0 | cmsFLAGS_NOCACHE); |
373 | | |
374 | | // Does create the backwards step |
375 | 0 | Chain.hReverse = cmsCreateTransform(ContextID, hGamut, dwFormat, |
376 | 0 | hLab, TYPE_Lab_DBL, |
377 | 0 | INTENT_RELATIVE_COLORIMETRIC, |
378 | 0 | cmsFLAGS_NOCACHE); |
379 | | |
380 | | |
381 | | // All ok? |
382 | 0 | if (Chain.hInput && Chain.hForward && Chain.hReverse) { |
383 | | |
384 | | // Go on, try to compute gamut LUT from PCS. This consist on a single channel containing |
385 | | // dE when doing a transform back and forth on the colorimetric intent. |
386 | |
|
387 | 0 | Gamut = cmsPipelineAlloc(ContextID, 3, 1); |
388 | 0 | if (Gamut != NULL) { |
389 | |
|
390 | 0 | CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL); |
391 | 0 | if (!cmsPipelineInsertStage(ContextID, Gamut, cmsAT_BEGIN, CLUT)) { |
392 | 0 | cmsPipelineFree(ContextID, Gamut); |
393 | 0 | Gamut = NULL; |
394 | 0 | } |
395 | 0 | else { |
396 | 0 | cmsStageSampleCLut16bit(ContextID, CLUT, GamutSampler, (void*) &Chain, 0); |
397 | 0 | } |
398 | 0 | } |
399 | 0 | } |
400 | 0 | else |
401 | 0 | Gamut = NULL; // Didn't work... |
402 | | |
403 | | // Free all needed stuff. |
404 | 0 | if (Chain.hInput) cmsDeleteTransform(ContextID, Chain.hInput); |
405 | 0 | if (Chain.hForward) cmsDeleteTransform(ContextID, Chain.hForward); |
406 | 0 | if (Chain.hReverse) cmsDeleteTransform(ContextID, Chain.hReverse); |
407 | 0 | if (hLab) cmsCloseProfile(ContextID, hLab); |
408 | | |
409 | | // And return computed hull |
410 | 0 | return Gamut; |
411 | 0 | } |
412 | | |
413 | | // Total Area Coverage estimation ---------------------------------------------------------------- |
414 | | |
415 | | typedef struct { |
416 | | cmsUInt32Number nOutputChans; |
417 | | cmsHTRANSFORM hRoundTrip; |
418 | | cmsFloat32Number MaxTAC; |
419 | | cmsFloat32Number MaxInput[cmsMAXCHANNELS]; |
420 | | |
421 | | } cmsTACestimator; |
422 | | |
423 | | |
424 | | // This callback just accounts the maximum ink dropped in the given node. It does not populate any |
425 | | // memory, as the destination table is NULL. Its only purpose it to know the global maximum. |
426 | | static |
427 | | int EstimateTAC(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo) |
428 | 0 | { |
429 | 0 | cmsTACestimator* bp = (cmsTACestimator*) Cargo; |
430 | 0 | cmsFloat32Number RoundTrip[cmsMAXCHANNELS]; |
431 | 0 | cmsUInt32Number i; |
432 | 0 | cmsFloat32Number Sum; |
433 | | |
434 | | |
435 | | // Evaluate the xform |
436 | 0 | cmsDoTransform(ContextID, bp->hRoundTrip, In, RoundTrip, 1); |
437 | | |
438 | | // All all amounts of ink |
439 | 0 | for (Sum=0, i=0; i < bp ->nOutputChans; i++) |
440 | 0 | Sum += RoundTrip[i]; |
441 | | |
442 | | // If above maximum, keep track of input values |
443 | 0 | if (Sum > bp ->MaxTAC) { |
444 | |
|
445 | 0 | bp ->MaxTAC = Sum; |
446 | |
|
447 | 0 | for (i=0; i < bp ->nOutputChans; i++) { |
448 | 0 | bp ->MaxInput[i] = In[i]; |
449 | 0 | } |
450 | 0 | } |
451 | |
|
452 | 0 | return TRUE; |
453 | | |
454 | 0 | cmsUNUSED_PARAMETER(Out); |
455 | 0 | } |
456 | | |
457 | | |
458 | | // Detect Total area coverage of the profile |
459 | | cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsContext ContextID, cmsHPROFILE hProfile) |
460 | 0 | { |
461 | 0 | cmsTACestimator bp; |
462 | 0 | cmsUInt32Number dwFormatter; |
463 | 0 | cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS]; |
464 | 0 | cmsHPROFILE hLab; |
465 | | |
466 | | // TAC only works on output profiles |
467 | 0 | if (cmsGetDeviceClass(ContextID, hProfile) != cmsSigOutputClass) { |
468 | 0 | return 0; |
469 | 0 | } |
470 | | |
471 | | // Create a fake formatter for result |
472 | 0 | dwFormatter = cmsFormatterForColorspaceOfProfile(ContextID, hProfile, 4, TRUE); |
473 | |
|
474 | 0 | bp.nOutputChans = T_CHANNELS(dwFormatter); |
475 | 0 | bp.MaxTAC = 0; // Initial TAC is 0 |
476 | | |
477 | | // for safety |
478 | 0 | if (bp.nOutputChans >= cmsMAXCHANNELS) return 0; |
479 | | |
480 | 0 | hLab = cmsCreateLab4Profile(ContextID, NULL); |
481 | 0 | if (hLab == NULL) return 0; |
482 | | // Setup a roundtrip on perceptual intent in output profile for TAC estimation |
483 | 0 | bp.hRoundTrip = cmsCreateTransform(ContextID, hLab, TYPE_Lab_16, |
484 | 0 | hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE); |
485 | |
|
486 | 0 | cmsCloseProfile(ContextID, hLab); |
487 | 0 | if (bp.hRoundTrip == NULL) return 0; |
488 | | |
489 | | // For L* we only need black and white. For C* we need many points |
490 | 0 | GridPoints[0] = 6; |
491 | 0 | GridPoints[1] = 74; |
492 | 0 | GridPoints[2] = 74; |
493 | | |
494 | |
|
495 | 0 | if (!cmsSliceSpace16(ContextID, 3, GridPoints, EstimateTAC, &bp)) { |
496 | 0 | bp.MaxTAC = 0; |
497 | 0 | } |
498 | |
|
499 | 0 | cmsDeleteTransform(ContextID, bp.hRoundTrip); |
500 | | |
501 | | // Results in % |
502 | 0 | return bp.MaxTAC; |
503 | 0 | } |
504 | | |
505 | | |
506 | | // Carefully, clamp on CIELab space. |
507 | | |
508 | | cmsBool CMSEXPORT cmsDesaturateLab(cmsContext ContextID, cmsCIELab* Lab, |
509 | | double amax, double amin, |
510 | | double bmax, double bmin) |
511 | 0 | { |
512 | | |
513 | | // Whole Luma surface to zero |
514 | |
|
515 | 0 | if (Lab -> L < 0) { |
516 | |
|
517 | 0 | Lab-> L = Lab->a = Lab-> b = 0.0; |
518 | 0 | return FALSE; |
519 | 0 | } |
520 | | |
521 | | // Clamp white, DISCARD HIGHLIGHTS. This is done |
522 | | // in such way because icc spec doesn't allow the |
523 | | // use of L>100 as a highlight means. |
524 | | |
525 | 0 | if (Lab->L > 100) |
526 | 0 | Lab -> L = 100; |
527 | | |
528 | | // Check out gamut prism, on a, b faces |
529 | |
|
530 | 0 | if (Lab -> a < amin || Lab->a > amax|| |
531 | 0 | Lab -> b < bmin || Lab->b > bmax) { |
532 | |
|
533 | 0 | cmsCIELCh LCh; |
534 | 0 | double h, slope; |
535 | | |
536 | | // Falls outside a, b limits. Transports to LCh space, |
537 | | // and then do the clipping |
538 | | |
539 | |
|
540 | 0 | if (Lab -> a == 0.0) { // Is hue exactly 90? |
541 | | |
542 | | // atan will not work, so clamp here |
543 | 0 | Lab -> b = Lab->b < 0 ? bmin : bmax; |
544 | 0 | return TRUE; |
545 | 0 | } |
546 | | |
547 | 0 | cmsLab2LCh(ContextID, &LCh, Lab); |
548 | |
|
549 | 0 | slope = Lab -> b / Lab -> a; |
550 | 0 | h = LCh.h; |
551 | | |
552 | | // There are 4 zones |
553 | |
|
554 | 0 | if ((h >= 0. && h < 45.) || |
555 | 0 | (h >= 315 && h <= 360.)) { |
556 | | |
557 | | // clip by amax |
558 | 0 | Lab -> a = amax; |
559 | 0 | Lab -> b = amax * slope; |
560 | 0 | } |
561 | 0 | else |
562 | 0 | if (h >= 45. && h < 135.) |
563 | 0 | { |
564 | | // clip by bmax |
565 | 0 | Lab -> b = bmax; |
566 | 0 | Lab -> a = bmax / slope; |
567 | 0 | } |
568 | 0 | else |
569 | 0 | if (h >= 135. && h < 225.) { |
570 | | // clip by amin |
571 | 0 | Lab -> a = amin; |
572 | 0 | Lab -> b = amin * slope; |
573 | |
|
574 | 0 | } |
575 | 0 | else |
576 | 0 | if (h >= 225. && h < 315.) { |
577 | | // clip by bmin |
578 | 0 | Lab -> b = bmin; |
579 | 0 | Lab -> a = bmin / slope; |
580 | 0 | } |
581 | 0 | else { |
582 | 0 | cmsSignalError(0, cmsERROR_RANGE, "Invalid angle"); |
583 | 0 | return FALSE; |
584 | 0 | } |
585 | |
|
586 | 0 | } |
587 | | |
588 | 0 | return TRUE; |
589 | 0 | } |
590 | | |
591 | | // Detect whatever a given ICC profile works in linear (gamma 1.0) space |
592 | | // Actually, doing that "well" is quite hard, since every component may behave completely different. |
593 | | // Since the true point of this function is to detect suitable optimizations, I am imposing some requirements |
594 | | // that simplifies things: only RGB, and only profiles that can got in both directions. |
595 | | // The algorith obtains Y from a syntetical gray R=G=B. Then least squares fitting is used to estimate gamma. |
596 | | // For gamma close to 1.0, RGB is linear. On profiles not supported, -1 is returned. |
597 | | |
598 | | cmsFloat64Number CMSEXPORT cmsDetectRGBProfileGamma(cmsContext ContextID, cmsHPROFILE hProfile, cmsFloat64Number thereshold) |
599 | 355k | { |
600 | 355k | cmsHPROFILE hXYZ; |
601 | 355k | cmsHTRANSFORM xform; |
602 | 355k | cmsToneCurve* Y_curve; |
603 | 355k | cmsUInt16Number rgb[256][3]; |
604 | 355k | cmsCIEXYZ XYZ[256]; |
605 | 355k | cmsFloat32Number Y_normalized[256]; |
606 | 355k | cmsFloat64Number gamma; |
607 | 355k | cmsProfileClassSignature cl; |
608 | 355k | int i; |
609 | | |
610 | 355k | if (cmsGetColorSpace(ContextID, hProfile) != cmsSigRgbData) |
611 | 0 | return -1; |
612 | | |
613 | 355k | cl = cmsGetDeviceClass(ContextID, hProfile); |
614 | 355k | if (cl != cmsSigInputClass && cl != cmsSigDisplayClass && |
615 | 355k | cl != cmsSigOutputClass && cl != cmsSigColorSpaceClass) |
616 | 340 | return -1; |
617 | | |
618 | 355k | hXYZ = cmsCreateXYZProfile(ContextID); |
619 | 355k | xform = cmsCreateTransform(ContextID, hProfile, TYPE_RGB_16, hXYZ, TYPE_XYZ_DBL, |
620 | 355k | INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE); |
621 | | |
622 | 355k | if (xform == NULL) { // If not RGB or forward direction is not supported, regret with the previous error |
623 | | |
624 | 329k | cmsCloseProfile(ContextID, hXYZ); |
625 | 329k | return -1; |
626 | 329k | } |
627 | | |
628 | 6.48M | for (i = 0; i < 256; i++) { |
629 | 6.45M | rgb[i][0] = rgb[i][1] = rgb[i][2] = FROM_8_TO_16(i); |
630 | 6.45M | } |
631 | | |
632 | 25.2k | cmsDoTransform(ContextID, xform, rgb, XYZ, 256); |
633 | | |
634 | 25.2k | cmsDeleteTransform(ContextID, xform); |
635 | 25.2k | cmsCloseProfile(ContextID, hXYZ); |
636 | | |
637 | 6.48M | for (i = 0; i < 256; i++) { |
638 | 6.45M | Y_normalized[i] = (cmsFloat32Number) XYZ[i].Y; |
639 | 6.45M | } |
640 | | |
641 | 25.2k | Y_curve = cmsBuildTabulatedToneCurveFloat(ContextID, 256, Y_normalized); |
642 | 25.2k | if (Y_curve == NULL) |
643 | 0 | return -1; |
644 | | |
645 | 25.2k | gamma = cmsEstimateGamma(ContextID, Y_curve, thereshold); |
646 | | |
647 | 25.2k | cmsFreeToneCurve(ContextID, Y_curve); |
648 | | |
649 | 25.2k | return gamma; |
650 | 25.2k | } |