1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2010 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"
31 // This file contains routines for resampling and LUT optimization, black point detection
32 // and black preservation.
34 // Black point detection -------------------------------------------------------------------------
37 // PCS -> PCS round trip transform, always uses relative intent on the device -> pcs
39 cmsHTRANSFORM CreateRoundtripXForm(cmsHPROFILE hProfile, cmsUInt32Number nIntent)
41 cmsHPROFILE hLab = cmsCreateLab4Profile(NULL);
43 cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
44 cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
45 cmsHPROFILE hProfiles[4];
46 cmsUInt32Number Intents[4];
47 cmsContext ContextID = cmsGetProfileContextID(hProfile);
49 hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
50 Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
52 xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents,
53 States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
55 cmsCloseProfile(hLab);
59 // Use darker colorants to obtain black point. This works in the relative colorimetric intent and
60 // assumes more ink results in darker colors. No ink limit is assumed.
62 cmsBool BlackPointAsDarkerColorant(cmsHPROFILE hInput,
63 cmsUInt32Number Intent,
64 cmsCIEXYZ* BlackPoint,
65 cmsUInt32Number dwFlags)
67 cmsUInt16Number *Black;
69 cmsColorSpaceSignature Space;
70 cmsUInt32Number nChannels;
71 cmsUInt32Number dwFormat;
75 cmsContext ContextID = cmsGetProfileContextID(hInput);
77 // If the profile does not support input direction, assume Black point 0
78 if (!cmsIsIntentSupported(hInput, Intent, LCMS_USED_AS_INPUT)) {
80 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
84 // Create a formatter which has n channels and floating point
85 dwFormat = cmsFormatterForColorspaceOfProfile(hInput, 2, FALSE);
87 // Try to get black by using black colorant
88 Space = cmsGetColorSpace(hInput);
90 // This function returns darker colorant in 16 bits for several spaces
91 if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) {
93 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
97 if (nChannels != T_CHANNELS(dwFormat)) {
98 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
102 // Lab will be used as the output space, but lab2 will avoid recursion
103 hLab = cmsCreateLab2ProfileTHR(ContextID, NULL);
105 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
109 // Create the transform
110 xform = cmsCreateTransformTHR(ContextID, hInput, dwFormat,
111 hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
112 cmsCloseProfile(hLab);
115 // Something went wrong. Get rid of open resources and return zero as black
117 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
121 // Convert black to Lab
122 cmsDoTransform(xform, Black, &Lab, 1);
124 // Force it to be neutral, clip to max. L* of 50
126 if (Lab.L > 50) Lab.L = 50;
128 // Free the resources
129 cmsDeleteTransform(xform);
131 // Convert from Lab (which is now clipped) to XYZ.
132 cmsLab2XYZ(NULL, &BlackXYZ, &Lab);
134 if (BlackPoint != NULL)
135 *BlackPoint = BlackXYZ;
139 cmsUNUSED_PARAMETER(dwFlags);
142 // Get a black point of output CMYK profile, discounting any ink-limiting embedded
143 // in the profile. For doing that, we use perceptual intent in input direction:
144 // Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
146 cmsBool BlackPointUsingPerceptualBlack(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
149 cmsHTRANSFORM hRoundTrip;
150 cmsCIELab LabIn, LabOut;
153 // Is the intent supported by the profile?
154 if (!cmsIsIntentSupported(hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
156 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
160 hRoundTrip = CreateRoundtripXForm(hProfile, INTENT_PERCEPTUAL);
161 if (hRoundTrip == NULL) {
162 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
166 LabIn.L = LabIn.a = LabIn.b = 0;
167 cmsDoTransform(hRoundTrip, &LabIn, &LabOut, 1);
169 // Clip Lab to reasonable limits
170 if (LabOut.L > 50) LabOut.L = 50;
171 LabOut.a = LabOut.b = 0;
173 cmsDeleteTransform(hRoundTrip);
176 cmsLab2XYZ(NULL, &BlackXYZ, &LabOut);
178 if (BlackPoint != NULL)
179 *BlackPoint = BlackXYZ;
184 // This function shouldn't exist at all -- there is such quantity of broken
185 // profiles on black point tag, that we must somehow fix chromaticity to
186 // avoid huge tint when doing Black point compensation. This function does
187 // just that. There is a special flag for using black point tag, but turned
188 // off by default because it is bogus on most profiles. The detection algorithm
189 // involves to turn BP to neutral and to use only L component.
191 cmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
194 // Zero for black point
195 if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
197 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
201 // v4 + perceptual & saturation intents does have its own black point, and it is
202 // well specified enough to use it. Black point tag is deprecated in V4.
204 if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&
205 (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
207 // Matrix shaper share MRC & perceptual intents
208 if (cmsIsMatrixShaper(hProfile))
209 return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
211 // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
212 BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
213 BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
214 BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
220 #ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
222 // v2, v4 rel/abs colorimetric
223 if (cmsIsTag(hProfile, cmsSigMediaBlackPointTag) &&
224 Intent == INTENT_RELATIVE_COLORIMETRIC) {
226 cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
229 // If black point is specified, then use it,
231 BlackPtr = cmsReadTag(hProfile, cmsSigMediaBlackPointTag);
232 if (BlackPtr != NULL) {
234 BlackXYZ = *BlackPtr;
235 _cmsReadMediaWhitePoint(&MediaWhite, hProfile);
237 // Black point is absolute XYZ, so adapt to D50 to get PCS value
238 cmsAdaptToIlluminant(&UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(), &BlackXYZ);
240 // Force a=b=0 to get rid of any chroma
241 cmsXYZ2Lab(NULL, &Lab, &UntrustedBlackPoint);
243 if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
244 cmsLab2XYZ(NULL, &TrustedBlackPoint, &Lab);
246 if (BlackPoint != NULL)
247 *BlackPoint = TrustedBlackPoint;
254 // That is about v2 profiles.
256 // If output profile, discount ink-limiting and that's all
257 if (Intent == INTENT_RELATIVE_COLORIMETRIC &&
258 (cmsGetDeviceClass(hProfile) == cmsSigOutputClass) &&
259 (cmsGetColorSpace(hProfile) == cmsSigCmykData))
260 return BlackPointUsingPerceptualBlack(BlackPoint, hProfile);
262 // Nope, compute BP using current intent.
263 return BlackPointAsDarkerColorant(hProfile, Intent, BlackPoint, dwFlags);