1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2012 Marti Maria Saguer
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
24 //---------------------------------------------------------------------------------
27 #include "lcms2_internal.h"
30 // Link several profiles to obtain a single LUT modelling the whole color transform. Intents, Black point
31 // compensation and Adaptation parameters may vary across profiles. BPC and Adaptation refers to the PCS
32 // after the profile. I.e, BPC[0] refers to connexion between profile(0) and profile(1)
33 cmsPipeline* _cmsLinkProfiles(cmsContext ContextID,
34 cmsUInt32Number nProfiles,
35 cmsUInt32Number Intents[],
36 cmsHPROFILE hProfiles[],
38 cmsFloat64Number AdaptationStates[],
39 cmsUInt32Number dwFlags);
41 //---------------------------------------------------------------------------------
43 // This is the default routine for ICC-style intents. A user may decide to override it by using a plugin.
44 // Supported intents are perceptual, relative colorimetric, saturation and ICC-absolute colorimetric
46 cmsPipeline* DefaultICCintents(cmsContext ContextID,
47 cmsUInt32Number nProfiles,
48 cmsUInt32Number Intents[],
49 cmsHPROFILE hProfiles[],
51 cmsFloat64Number AdaptationStates[],
52 cmsUInt32Number dwFlags);
54 //---------------------------------------------------------------------------------
56 // This is the entry for black-preserving K-only intents, which are non-ICC. Last profile have to be a output profile
57 // to do the trick (no devicelinks allowed at that position)
59 cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID,
60 cmsUInt32Number nProfiles,
61 cmsUInt32Number Intents[],
62 cmsHPROFILE hProfiles[],
64 cmsFloat64Number AdaptationStates[],
65 cmsUInt32Number dwFlags);
67 //---------------------------------------------------------------------------------
69 // This is the entry for black-plane preserving, which are non-ICC. Again, Last profile have to be a output profile
70 // to do the trick (no devicelinks allowed at that position)
72 cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID,
73 cmsUInt32Number nProfiles,
74 cmsUInt32Number Intents[],
75 cmsHPROFILE hProfiles[],
77 cmsFloat64Number AdaptationStates[],
78 cmsUInt32Number dwFlags);
80 //---------------------------------------------------------------------------------
83 // This is a structure holding implementations for all supported intents.
84 typedef struct _cms_intents_list {
86 cmsUInt32Number Intent;
87 char Description[256];
89 struct _cms_intents_list* Next;
95 static cmsIntentsList DefaultIntents[] = {
97 { INTENT_PERCEPTUAL, "Perceptual", DefaultICCintents, &DefaultIntents[1] },
98 { INTENT_RELATIVE_COLORIMETRIC, "Relative colorimetric", DefaultICCintents, &DefaultIntents[2] },
99 { INTENT_SATURATION, "Saturation", DefaultICCintents, &DefaultIntents[3] },
100 { INTENT_ABSOLUTE_COLORIMETRIC, "Absolute colorimetric", DefaultICCintents, &DefaultIntents[4] },
101 { INTENT_PRESERVE_K_ONLY_PERCEPTUAL, "Perceptual preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[5] },
102 { INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC, "Relative colorimetric preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[6] },
103 { INTENT_PRESERVE_K_ONLY_SATURATION, "Saturation preserving black ink", BlackPreservingKOnlyIntents, &DefaultIntents[7] },
104 { INTENT_PRESERVE_K_PLANE_PERCEPTUAL, "Perceptual preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[8] },
105 { INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC,"Relative colorimetric preserving black plane", BlackPreservingKPlaneIntents, &DefaultIntents[9] },
106 { INTENT_PRESERVE_K_PLANE_SATURATION, "Saturation preserving black plane", BlackPreservingKPlaneIntents, NULL }
110 // A pointer to the begining of the list
111 static cmsIntentsList *Intents = DefaultIntents;
113 // Search the list for a suitable intent. Returns NULL if not found
115 cmsIntentsList* SearchIntent(cmsUInt32Number Intent)
119 for (pt = Intents; pt != NULL; pt = pt -> Next)
120 if (pt ->Intent == Intent) return pt;
125 // Black point compensation. Implemented as a linear scaling in XYZ. Black points
126 // should come relative to the white point. Fills an matrix/offset element m
127 // which is organized as a 4x4 matrix.
129 void ComputeBlackPointCompensation(const cmsCIEXYZ* BlackPointIn,
130 const cmsCIEXYZ* BlackPointOut,
131 cmsMAT3* m, cmsVEC3* off)
133 cmsFloat64Number ax, ay, az, bx, by, bz, tx, ty, tz;
135 // Now we need to compute a matrix plus an offset m and of such of
136 // [m]*bpin + off = bpout
137 // [m]*D50 + off = D50
139 // This is a linear scaling in the form ax+b, where
140 // a = (bpout - D50) / (bpin - D50)
141 // b = - D50* (bpout - bpin) / (bpin - D50)
143 tx = BlackPointIn->X - cmsD50_XYZ()->X;
144 ty = BlackPointIn->Y - cmsD50_XYZ()->Y;
145 tz = BlackPointIn->Z - cmsD50_XYZ()->Z;
147 ax = (BlackPointOut->X - cmsD50_XYZ()->X) / tx;
148 ay = (BlackPointOut->Y - cmsD50_XYZ()->Y) / ty;
149 az = (BlackPointOut->Z - cmsD50_XYZ()->Z) / tz;
151 bx = - cmsD50_XYZ()-> X * (BlackPointOut->X - BlackPointIn->X) / tx;
152 by = - cmsD50_XYZ()-> Y * (BlackPointOut->Y - BlackPointIn->Y) / ty;
153 bz = - cmsD50_XYZ()-> Z * (BlackPointOut->Z - BlackPointIn->Z) / tz;
155 _cmsVEC3init(&m ->v[0], ax, 0, 0);
156 _cmsVEC3init(&m ->v[1], 0, ay, 0);
157 _cmsVEC3init(&m ->v[2], 0, 0, az);
158 _cmsVEC3init(off, bx, by, bz);
163 // Approximate a blackbody illuminant based on CHAD information
165 cmsFloat64Number CHAD2Temp(const cmsMAT3* Chad)
167 // Convert D50 across inverse CHAD to get the absolute white point
170 cmsCIExyY DestChromaticity;
171 cmsFloat64Number TempK;
175 if (!_cmsMAT3inverse(&m1, &m2)) return FALSE;
177 s.n[VX] = cmsD50_XYZ() -> X;
178 s.n[VY] = cmsD50_XYZ() -> Y;
179 s.n[VZ] = cmsD50_XYZ() -> Z;
181 _cmsMAT3eval(&d, &m2, &s);
187 cmsXYZ2xyY(&DestChromaticity, &Dest);
189 if (!cmsTempFromWhitePoint(&TempK, &DestChromaticity))
195 // Compute a CHAD based on a given temperature
197 void Temp2CHAD(cmsMAT3* Chad, cmsFloat64Number Temp)
200 cmsCIExyY ChromaticityOfWhite;
202 cmsWhitePointFromTemp(&ChromaticityOfWhite, Temp);
203 cmsxyY2XYZ(&White, &ChromaticityOfWhite);
204 _cmsAdaptationMatrix(Chad, NULL, &White, cmsD50_XYZ());
207 // Join scalings to obtain relative input to absolute and then to relative output.
208 // Result is stored in a 3x3 matrix
210 cmsBool ComputeAbsoluteIntent(cmsFloat64Number AdaptationState,
211 const cmsCIEXYZ* WhitePointIn,
212 const cmsMAT3* ChromaticAdaptationMatrixIn,
213 const cmsCIEXYZ* WhitePointOut,
214 const cmsMAT3* ChromaticAdaptationMatrixOut,
217 cmsMAT3 Scale, m1, m2, m3, m4;
220 if (AdaptationState == 1.0) {
222 // Observer is fully adapted. Keep chromatic adaptation.
223 // That is the standard V4 behaviour
224 _cmsVEC3init(&m->v[0], WhitePointIn->X / WhitePointOut->X, 0, 0);
225 _cmsVEC3init(&m->v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0);
226 _cmsVEC3init(&m->v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z);
231 // Incomplete adaptation. This is an advanced feature.
232 _cmsVEC3init(&Scale.v[0], WhitePointIn->X / WhitePointOut->X, 0, 0);
233 _cmsVEC3init(&Scale.v[1], 0, WhitePointIn->Y / WhitePointOut->Y, 0);
234 _cmsVEC3init(&Scale.v[2], 0, 0, WhitePointIn->Z / WhitePointOut->Z);
237 if (AdaptationState == 0.0) {
239 m1 = *ChromaticAdaptationMatrixOut;
240 _cmsMAT3per(&m2, &m1, &Scale);
241 // m2 holds CHAD from output white to D50 times abs. col. scaling
243 // Observer is not adapted, undo the chromatic adaptation
244 _cmsMAT3per(m, &m3, ChromaticAdaptationMatrixOut);
246 m3 = *ChromaticAdaptationMatrixIn;
247 if (!_cmsMAT3inverse(&m3, &m4)) return FALSE;
248 _cmsMAT3per(m, &m2, &m4);
253 cmsFloat64Number TempSrc, TempDest, Temp;
255 m1 = *ChromaticAdaptationMatrixIn;
256 if (!_cmsMAT3inverse(&m1, &m2)) return FALSE;
257 _cmsMAT3per(&m3, &m2, &Scale);
258 // m3 holds CHAD from input white to D50 times abs. col. scaling
260 TempSrc = CHAD2Temp(ChromaticAdaptationMatrixIn);
261 TempDest = CHAD2Temp(ChromaticAdaptationMatrixOut);
263 if (TempSrc < 0.0 || TempDest < 0.0) return FALSE; // Something went wrong
265 if (_cmsMAT3isIdentity(&Scale) && fabs(TempSrc - TempDest) < 0.01) {
271 Temp = (1.0 - AdaptationState) * TempDest + AdaptationState * TempSrc;
273 // Get a CHAD from whatever output temperature to D50. This replaces output CHAD
274 Temp2CHAD(&MixedCHAD, Temp);
276 _cmsMAT3per(m, &m3, &MixedCHAD);
284 // Just to see if m matrix should be applied
286 cmsBool IsEmptyLayer(cmsMAT3* m, cmsVEC3* off)
288 cmsFloat64Number diff = 0;
292 if (m == NULL && off == NULL) return TRUE; // NULL is allowed as an empty layer
293 if (m == NULL && off != NULL) return FALSE; // This is an internal error
295 _cmsMAT3identity(&Ident);
297 for (i=0; i < 3*3; i++)
298 diff += fabs(((cmsFloat64Number*)m)[i] - ((cmsFloat64Number*)&Ident)[i]);
300 for (i=0; i < 3; i++)
301 diff += fabs(((cmsFloat64Number*)off)[i]);
304 return (diff < 0.002);
308 // Compute the conversion layer
310 cmsBool ComputeConversion(int i, cmsHPROFILE hProfiles[],
311 cmsUInt32Number Intent,
313 cmsFloat64Number AdaptationState,
314 cmsMAT3* m, cmsVEC3* off)
319 // m and off are set to identity and this is detected latter on
321 _cmsVEC3init(off, 0, 0, 0);
323 // If intent is abs. colorimetric,
324 if (Intent == INTENT_ABSOLUTE_COLORIMETRIC) {
326 cmsCIEXYZ WhitePointIn, WhitePointOut;
327 cmsMAT3 ChromaticAdaptationMatrixIn, ChromaticAdaptationMatrixOut;
329 _cmsReadMediaWhitePoint(&WhitePointIn, hProfiles[i-1]);
330 _cmsReadCHAD(&ChromaticAdaptationMatrixIn, hProfiles[i-1]);
332 _cmsReadMediaWhitePoint(&WhitePointOut, hProfiles[i]);
333 _cmsReadCHAD(&ChromaticAdaptationMatrixOut, hProfiles[i]);
335 if (!ComputeAbsoluteIntent(AdaptationState,
336 &WhitePointIn, &ChromaticAdaptationMatrixIn,
337 &WhitePointOut, &ChromaticAdaptationMatrixOut, m)) return FALSE;
341 // Rest of intents may apply BPC.
345 cmsCIEXYZ BlackPointIn, BlackPointOut;
347 cmsDetectBlackPoint(&BlackPointIn, hProfiles[i-1], Intent, 0);
348 cmsDetectDestinationBlackPoint(&BlackPointOut, hProfiles[i], Intent, 0);
350 // If black points are equal, then do nothing
351 if (BlackPointIn.X != BlackPointOut.X ||
352 BlackPointIn.Y != BlackPointOut.Y ||
353 BlackPointIn.Z != BlackPointOut.Z)
354 ComputeBlackPointCompensation(&BlackPointIn, &BlackPointOut, m, off);
358 // Offset should be adjusted because the encoding. We encode XYZ normalized to 0..1.0,
359 // to do that, we divide by MAX_ENCODEABLE_XZY. The conversion stage goes XYZ -> XYZ so
360 // we have first to convert from encoded to XYZ and then convert back to encoded.
364 // y = y'c; y' = y / c
365 // y' = (Mx'c + Off) /c = Mx' + (Off / c)
367 for (k=0; k < 3; k++) {
368 off ->n[k] /= MAX_ENCODEABLE_XYZ;
375 // Add a conversion stage if needed. If a matrix/offset m is given, it applies to XYZ space
377 cmsBool AddConversion(cmsPipeline* Result, cmsColorSpaceSignature InPCS, cmsColorSpaceSignature OutPCS, cmsMAT3* m, cmsVEC3* off)
379 cmsFloat64Number* m_as_dbl = (cmsFloat64Number*) m;
380 cmsFloat64Number* off_as_dbl = (cmsFloat64Number*) off;
382 // Handle PCS mismatches. A specialized stage is added to the LUT in such case
385 case cmsSigXYZData: // Input profile operates in XYZ
389 case cmsSigXYZData: // XYZ -> XYZ
390 if (!IsEmptyLayer(m, off))
391 cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl));
394 case cmsSigLabData: // XYZ -> Lab
395 if (!IsEmptyLayer(m, off))
396 cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl));
397 cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocXYZ2Lab(Result ->ContextID));
401 return FALSE; // Colorspace mismatch
406 case cmsSigLabData: // Input profile operates in Lab
410 case cmsSigXYZData: // Lab -> XYZ
412 cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocLab2XYZ(Result ->ContextID));
413 if (!IsEmptyLayer(m, off))
414 cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl));
417 case cmsSigLabData: // Lab -> Lab
419 if (!IsEmptyLayer(m, off)) {
420 cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocLab2XYZ(Result ->ContextID));
421 cmsPipelineInsertStage(Result, cmsAT_END, cmsStageAllocMatrix(Result ->ContextID, 3, 3, m_as_dbl, off_as_dbl));
422 cmsPipelineInsertStage(Result, cmsAT_END, _cmsStageAllocXYZ2Lab(Result ->ContextID));
427 return FALSE; // Mismatch
432 // On colorspaces other than PCS, check for same space
434 if (InPCS != OutPCS) return FALSE;
442 // Is a given space compatible with another?
444 cmsBool ColorSpaceIsCompatible(cmsColorSpaceSignature a, cmsColorSpaceSignature b)
446 // If they are same, they are compatible.
447 if (a == b) return TRUE;
449 // Check for MCH4 substitution of CMYK
450 if ((a == cmsSig4colorData) && (b == cmsSigCmykData)) return TRUE;
451 if ((a == cmsSigCmykData) && (b == cmsSig4colorData)) return TRUE;
453 // Check for XYZ/Lab. Those spaces are interchangeable as they can be computed one from other.
454 if ((a == cmsSigXYZData) && (b == cmsSigLabData)) return TRUE;
455 if ((a == cmsSigLabData) && (b == cmsSigXYZData)) return TRUE;
461 // Default handler for ICC-style intents
463 cmsPipeline* DefaultICCintents(cmsContext ContextID,
464 cmsUInt32Number nProfiles,
465 cmsUInt32Number TheIntents[],
466 cmsHPROFILE hProfiles[],
468 cmsFloat64Number AdaptationStates[],
469 cmsUInt32Number dwFlags)
471 cmsPipeline* Lut, *Result;
472 cmsHPROFILE hProfile;
475 cmsColorSpaceSignature ColorSpaceIn, ColorSpaceOut, CurrentColorSpace;
476 cmsProfileClassSignature ClassSig;
477 cmsUInt32Number i, Intent;
480 if (nProfiles == 0) return NULL;
482 // Allocate an empty LUT for holding the result. 0 as channel count means 'undefined'
483 Result = cmsPipelineAlloc(ContextID, 0, 0);
484 if (Result == NULL) return NULL;
486 CurrentColorSpace = cmsGetColorSpace(hProfiles[0]);
488 for (i=0; i < nProfiles; i++) {
490 cmsBool lIsDeviceLink, lIsInput;
492 hProfile = hProfiles[i];
493 ClassSig = cmsGetDeviceClass(hProfile);
494 lIsDeviceLink = (ClassSig == cmsSigLinkClass || ClassSig == cmsSigAbstractClass );
496 // First profile is used as input unless devicelink or abstract
497 if ((i == 0) && !lIsDeviceLink) {
501 // Else use profile in the input direction if current space is not PCS
502 lIsInput = (CurrentColorSpace != cmsSigXYZData) &&
503 (CurrentColorSpace != cmsSigLabData);
506 Intent = TheIntents[i];
508 if (lIsInput || lIsDeviceLink) {
510 ColorSpaceIn = cmsGetColorSpace(hProfile);
511 ColorSpaceOut = cmsGetPCS(hProfile);
515 ColorSpaceIn = cmsGetPCS(hProfile);
516 ColorSpaceOut = cmsGetColorSpace(hProfile);
519 if (!ColorSpaceIsCompatible(ColorSpaceIn, CurrentColorSpace)) {
521 cmsSignalError(ContextID, cmsERROR_COLORSPACE_CHECK, "ColorSpace mismatch");
525 // If devicelink is found, then no custom intent is allowed and we can
526 // read the LUT to be applied. Settings don't apply here.
527 if (lIsDeviceLink || ((ClassSig == cmsSigNamedColorClass) && (nProfiles == 1))) {
529 // Get the involved LUT from the profile
530 Lut = _cmsReadDevicelinkLUT(hProfile, Intent);
531 if (Lut == NULL) goto Error;
533 // What about abstract profiles?
534 if (ClassSig == cmsSigAbstractClass && i > 0) {
535 if (!ComputeConversion(i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error;
538 _cmsMAT3identity(&m);
539 _cmsVEC3init(&off, 0, 0, 0);
543 if (!AddConversion(Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error;
549 // Input direction means non-pcs connection, so proceed like devicelinks
550 Lut = _cmsReadInputLUT(hProfile, Intent);
551 if (Lut == NULL) goto Error;
555 // Output direction means PCS connection. Intent may apply here
556 Lut = _cmsReadOutputLUT(hProfile, Intent);
557 if (Lut == NULL) goto Error;
560 if (!ComputeConversion(i, hProfiles, Intent, BPC[i], AdaptationStates[i], &m, &off)) goto Error;
561 if (!AddConversion(Result, CurrentColorSpace, ColorSpaceIn, &m, &off)) goto Error;
566 // Concatenate to the output LUT
567 cmsPipelineCat(Result, Lut);
568 cmsPipelineFree(Lut);
570 // Update current space
571 CurrentColorSpace = ColorSpaceOut;
578 if (Result != NULL) cmsPipelineFree(Result);
581 cmsUNUSED_PARAMETER(dwFlags);
585 // Wrapper for DLL calling convention
586 cmsPipeline* CMSEXPORT _cmsDefaultICCintents(cmsContext ContextID,
587 cmsUInt32Number nProfiles,
588 cmsUInt32Number TheIntents[],
589 cmsHPROFILE hProfiles[],
591 cmsFloat64Number AdaptationStates[],
592 cmsUInt32Number dwFlags)
594 return DefaultICCintents(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags);
597 // Black preserving intents ---------------------------------------------------------------------------------------------
599 // Translate black-preserving intents to ICC ones
601 int TranslateNonICCIntents(int Intent)
604 case INTENT_PRESERVE_K_ONLY_PERCEPTUAL:
605 case INTENT_PRESERVE_K_PLANE_PERCEPTUAL:
606 return INTENT_PERCEPTUAL;
608 case INTENT_PRESERVE_K_ONLY_RELATIVE_COLORIMETRIC:
609 case INTENT_PRESERVE_K_PLANE_RELATIVE_COLORIMETRIC:
610 return INTENT_RELATIVE_COLORIMETRIC;
612 case INTENT_PRESERVE_K_ONLY_SATURATION:
613 case INTENT_PRESERVE_K_PLANE_SATURATION:
614 return INTENT_SATURATION;
616 default: return Intent;
620 // Sampler for Black-only preserving CMYK->CMYK transforms
623 cmsPipeline* cmyk2cmyk; // The original transform
624 cmsToneCurve* KTone; // Black-to-black tone curve
629 // Preserve black only if that is the only ink used
631 int BlackPreservingGrayOnlySampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
633 GrayOnlyParams* bp = (GrayOnlyParams*) Cargo;
635 // If going across black only, keep black only
636 if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
638 // TAC does not apply because it is black ink!
639 Out[0] = Out[1] = Out[2] = 0;
640 Out[3] = cmsEvalToneCurve16(bp->KTone, In[3]);
644 // Keep normal transform for other colors
645 bp ->cmyk2cmyk ->Eval16Fn(In, Out, bp ->cmyk2cmyk->Data);
649 // This is the entry for black-preserving K-only intents, which are non-ICC
651 cmsPipeline* BlackPreservingKOnlyIntents(cmsContext ContextID,
652 cmsUInt32Number nProfiles,
653 cmsUInt32Number TheIntents[],
654 cmsHPROFILE hProfiles[],
656 cmsFloat64Number AdaptationStates[],
657 cmsUInt32Number dwFlags)
661 cmsUInt32Number ICCIntents[256];
663 cmsUInt32Number i, nGridPoints;
667 if (nProfiles < 1 || nProfiles > 255) return NULL;
669 // Translate black-preserving intents to ICC ones
670 for (i=0; i < nProfiles; i++)
671 ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]);
673 // Check for non-cmyk profiles
674 if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
675 cmsGetColorSpace(hProfiles[nProfiles-1]) != cmsSigCmykData)
676 return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags);
678 memset(&bp, 0, sizeof(bp));
680 // Allocate an empty LUT for holding the result
681 Result = cmsPipelineAlloc(ContextID, 4, 4);
682 if (Result == NULL) return NULL;
684 // Create a LUT holding normal ICC transform
685 bp.cmyk2cmyk = DefaultICCintents(ContextID,
693 if (bp.cmyk2cmyk == NULL) goto Error;
695 // Now, compute the tone curve
696 bp.KTone = _cmsBuildKToneCurve(ContextID,
705 if (bp.KTone == NULL) goto Error;
708 // How many gridpoints are we going to use?
709 nGridPoints = _cmsReasonableGridpointsByColorspace(cmsSigCmykData, dwFlags);
711 // Create the CLUT. 16 bits
712 CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL);
713 if (CLUT == NULL) goto Error;
715 // This is the one and only MPE in this LUT
716 cmsPipelineInsertStage(Result, cmsAT_BEGIN, CLUT);
718 // Sample it. We cannot afford pre/post linearization this time.
719 if (!cmsStageSampleCLut16bit(CLUT, BlackPreservingGrayOnlySampler, (void*) &bp, 0))
722 // Get rid of xform and tone curve
723 cmsPipelineFree(bp.cmyk2cmyk);
724 cmsFreeToneCurve(bp.KTone);
730 if (bp.cmyk2cmyk != NULL) cmsPipelineFree(bp.cmyk2cmyk);
731 if (bp.KTone != NULL) cmsFreeToneCurve(bp.KTone);
732 if (Result != NULL) cmsPipelineFree(Result);
737 // K Plane-preserving CMYK to CMYK ------------------------------------------------------------------------------------
741 cmsPipeline* cmyk2cmyk; // The original transform
742 cmsHTRANSFORM hProofOutput; // Output CMYK to Lab (last profile)
743 cmsHTRANSFORM cmyk2Lab; // The input chain
744 cmsToneCurve* KTone; // Black-to-black tone curve
745 cmsPipeline* LabK2cmyk; // The output profile
746 cmsFloat64Number MaxError;
748 cmsHTRANSFORM hRoundTrip;
749 cmsFloat64Number MaxTAC;
752 } PreserveKPlaneParams;
755 // The CLUT will be stored at 16 bits, but calculations are performed at cmsFloat32Number precision
757 int BlackPreservingSampler(register const cmsUInt16Number In[], register cmsUInt16Number Out[], register void* Cargo)
760 cmsFloat32Number Inf[4], Outf[4];
761 cmsFloat32Number LabK[4];
762 cmsFloat64Number SumCMY, SumCMYK, Error, Ratio;
763 cmsCIELab ColorimetricLab, BlackPreservingLab;
764 PreserveKPlaneParams* bp = (PreserveKPlaneParams*) Cargo;
766 // Convert from 16 bits to floating point
767 for (i=0; i < 4; i++)
768 Inf[i] = (cmsFloat32Number) (In[i] / 65535.0);
770 // Get the K across Tone curve
771 LabK[3] = cmsEvalToneCurveFloat(bp ->KTone, Inf[3]);
773 // If going across black only, keep black only
774 if (In[0] == 0 && In[1] == 0 && In[2] == 0) {
776 Out[0] = Out[1] = Out[2] = 0;
777 Out[3] = _cmsQuickSaturateWord(LabK[3] * 65535.0);
781 // Try the original transform,
782 cmsPipelineEvalFloat( Inf, Outf, bp ->cmyk2cmyk);
784 // Store a copy of the floating point result into 16-bit
785 for (i=0; i < 4; i++)
786 Out[i] = _cmsQuickSaturateWord(Outf[i] * 65535.0);
788 // Maybe K is already ok (mostly on K=0)
789 if ( fabs(Outf[3] - LabK[3]) < (3.0 / 65535.0) ) {
793 // K differ, mesure and keep Lab measurement for further usage
794 // this is done in relative colorimetric intent
795 cmsDoTransform(bp->hProofOutput, Out, &ColorimetricLab, 1);
797 // Is not black only and the transform doesn't keep black.
798 // Obtain the Lab of output CMYK. After that we have Lab + K
799 cmsDoTransform(bp ->cmyk2Lab, Outf, LabK, 1);
801 // Obtain the corresponding CMY using reverse interpolation
802 // (K is fixed in LabK[3])
803 if (!cmsPipelineEvalReverseFloat(LabK, Outf, Outf, bp ->LabK2cmyk)) {
805 // Cannot find a suitable value, so use colorimetric xform
806 // which is already stored in Out[]
810 // Make sure to pass thru K (which now is fixed)
813 // Apply TAC if needed
814 SumCMY = Outf[0] + Outf[1] + Outf[2];
815 SumCMYK = SumCMY + Outf[3];
817 if (SumCMYK > bp ->MaxTAC) {
819 Ratio = 1 - ((SumCMYK - bp->MaxTAC) / SumCMY);
826 Out[0] = _cmsQuickSaturateWord(Outf[0] * Ratio * 65535.0); // C
827 Out[1] = _cmsQuickSaturateWord(Outf[1] * Ratio * 65535.0); // M
828 Out[2] = _cmsQuickSaturateWord(Outf[2] * Ratio * 65535.0); // Y
829 Out[3] = _cmsQuickSaturateWord(Outf[3] * 65535.0);
831 // Estimate the error (this goes 16 bits to Lab DBL)
832 cmsDoTransform(bp->hProofOutput, Out, &BlackPreservingLab, 1);
833 Error = cmsDeltaE(&ColorimetricLab, &BlackPreservingLab);
834 if (Error > bp -> MaxError)
835 bp->MaxError = Error;
840 // This is the entry for black-plane preserving, which are non-ICC
842 cmsPipeline* BlackPreservingKPlaneIntents(cmsContext ContextID,
843 cmsUInt32Number nProfiles,
844 cmsUInt32Number TheIntents[],
845 cmsHPROFILE hProfiles[],
847 cmsFloat64Number AdaptationStates[],
848 cmsUInt32Number dwFlags)
850 PreserveKPlaneParams bp;
851 cmsPipeline* Result = NULL;
852 cmsUInt32Number ICCIntents[256];
854 cmsUInt32Number i, nGridPoints;
858 if (nProfiles < 1 || nProfiles > 255) return NULL;
860 // Translate black-preserving intents to ICC ones
861 for (i=0; i < nProfiles; i++)
862 ICCIntents[i] = TranslateNonICCIntents(TheIntents[i]);
864 // Check for non-cmyk profiles
865 if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
866 !(cmsGetColorSpace(hProfiles[nProfiles-1]) == cmsSigCmykData ||
867 cmsGetDeviceClass(hProfiles[nProfiles-1]) == cmsSigOutputClass))
868 return DefaultICCintents(ContextID, nProfiles, ICCIntents, hProfiles, BPC, AdaptationStates, dwFlags);
870 // Allocate an empty LUT for holding the result
871 Result = cmsPipelineAlloc(ContextID, 4, 4);
872 if (Result == NULL) return NULL;
875 memset(&bp, 0, sizeof(bp));
877 // We need the input LUT of the last profile, assuming this one is responsible of
878 // black generation. This LUT will be seached in inverse order.
879 bp.LabK2cmyk = _cmsReadInputLUT(hProfiles[nProfiles-1], INTENT_RELATIVE_COLORIMETRIC);
880 if (bp.LabK2cmyk == NULL) goto Cleanup;
882 // Get total area coverage (in 0..1 domain)
883 bp.MaxTAC = cmsDetectTAC(hProfiles[nProfiles-1]) / 100.0;
884 if (bp.MaxTAC <= 0) goto Cleanup;
887 // Create a LUT holding normal ICC transform
888 bp.cmyk2cmyk = DefaultICCintents(ContextID,
895 if (bp.cmyk2cmyk == NULL) goto Cleanup;
897 // Now the tone curve
898 bp.KTone = _cmsBuildKToneCurve(ContextID, 4096, nProfiles,
904 if (bp.KTone == NULL) goto Cleanup;
906 // To measure the output, Last profile to Lab
907 hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
908 bp.hProofOutput = cmsCreateTransformTHR(ContextID, hProfiles[nProfiles-1],
909 CHANNELS_SH(4)|BYTES_SH(2), hLab, TYPE_Lab_DBL,
910 INTENT_RELATIVE_COLORIMETRIC,
911 cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
912 if ( bp.hProofOutput == NULL) goto Cleanup;
914 // Same as anterior, but lab in the 0..1 range
915 bp.cmyk2Lab = cmsCreateTransformTHR(ContextID, hProfiles[nProfiles-1],
916 FLOAT_SH(1)|CHANNELS_SH(4)|BYTES_SH(4), hLab,
917 FLOAT_SH(1)|CHANNELS_SH(3)|BYTES_SH(4),
918 INTENT_RELATIVE_COLORIMETRIC,
919 cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
920 if (bp.cmyk2Lab == NULL) goto Cleanup;
921 cmsCloseProfile(hLab);
923 // Error estimation (for debug only)
926 // How many gridpoints are we going to use?
927 nGridPoints = _cmsReasonableGridpointsByColorspace(cmsSigCmykData, dwFlags);
930 CLUT = cmsStageAllocCLut16bit(ContextID, nGridPoints, 4, 4, NULL);
931 if (CLUT == NULL) goto Cleanup;
933 cmsPipelineInsertStage(Result, cmsAT_BEGIN, CLUT);
935 cmsStageSampleCLut16bit(CLUT, BlackPreservingSampler, (void*) &bp, 0);
939 if (bp.cmyk2cmyk) cmsPipelineFree(bp.cmyk2cmyk);
940 if (bp.cmyk2Lab) cmsDeleteTransform(bp.cmyk2Lab);
941 if (bp.hProofOutput) cmsDeleteTransform(bp.hProofOutput);
943 if (bp.KTone) cmsFreeToneCurve(bp.KTone);
944 if (bp.LabK2cmyk) cmsPipelineFree(bp.LabK2cmyk);
949 // Link routines ------------------------------------------------------------------------------------------------------
951 // Chain several profiles into a single LUT. It just checks the parameters and then calls the handler
952 // for the first intent in chain. The handler may be user-defined. Is up to the handler to deal with the
953 // rest of intents in chain. A maximum of 255 profiles at time are supported, which is pretty reasonable.
954 cmsPipeline* _cmsLinkProfiles(cmsContext ContextID,
955 cmsUInt32Number nProfiles,
956 cmsUInt32Number TheIntents[],
957 cmsHPROFILE hProfiles[],
959 cmsFloat64Number AdaptationStates[],
960 cmsUInt32Number dwFlags)
963 cmsIntentsList* Intent;
965 // Make sure a reasonable number of profiles is provided
966 if (nProfiles <= 0 || nProfiles > 255) {
967 cmsSignalError(ContextID, cmsERROR_RANGE, "Couldn't link '%d' profiles", nProfiles);
971 for (i=0; i < nProfiles; i++) {
973 // Check if black point is really needed or allowed. Note that
974 // following Adobe's document:
975 // BPC does not apply to devicelink profiles, nor to abs colorimetric,
976 // and applies always on V4 perceptual and saturation.
978 if (TheIntents[i] == INTENT_ABSOLUTE_COLORIMETRIC)
981 if (TheIntents[i] == INTENT_PERCEPTUAL || TheIntents[i] == INTENT_SATURATION) {
983 // Force BPC for V4 profiles in perceptual and saturation
984 if (cmsGetProfileVersion(hProfiles[i]) >= 4.0)
989 // Search for a handler. The first intent in the chain defines the handler. That would
990 // prevent using multiple custom intents in a multiintent chain, but the behaviour of
991 // this case would present some issues if the custom intent tries to do things like
992 // preserve primaries. This solution is not perfect, but works well on most cases.
994 Intent = SearchIntent(TheIntents[0]);
995 if (Intent == NULL) {
996 cmsSignalError(ContextID, cmsERROR_UNKNOWN_EXTENSION, "Unsupported intent '%d'", TheIntents[0]);
1001 return Intent ->Link(ContextID, nProfiles, TheIntents, hProfiles, BPC, AdaptationStates, dwFlags);
1004 // -------------------------------------------------------------------------------------------------
1006 // Get information about available intents. nMax is the maximum space for the supplied "Codes"
1007 // and "Descriptions" the function returns the total number of intents, which may be greater
1008 // than nMax, although the matrices are not populated beyond this level.
1009 cmsUInt32Number CMSEXPORT cmsGetSupportedIntents(cmsUInt32Number nMax, cmsUInt32Number* Codes, char** Descriptions)
1012 cmsUInt32Number nIntents;
1014 for (nIntents=0, pt = Intents; pt != NULL; pt = pt -> Next)
1016 if (nIntents < nMax) {
1018 Codes[nIntents] = pt ->Intent;
1020 if (Descriptions != NULL)
1021 Descriptions[nIntents] = pt ->Description;
1030 // The plug-in registration. User can add new intents or override default routines
1031 cmsBool _cmsRegisterRenderingIntentPlugin(cmsPluginBase* Data)
1033 cmsPluginRenderingIntent* Plugin = (cmsPluginRenderingIntent*) Data;
1036 // Do we have to reset the intents?
1039 Intents = DefaultIntents;
1043 fl = SearchIntent(Plugin ->Intent);
1046 fl = (cmsIntentsList*) _cmsPluginMalloc(sizeof(cmsIntentsList));
1047 if (fl == NULL) return FALSE;
1050 fl ->Intent = Plugin ->Intent;
1051 strncpy(fl ->Description, Plugin ->Description, 255);
1052 fl ->Description[255] = 0;
1054 fl ->Link = Plugin ->Link;
1056 fl ->Next = Intents;