Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/lcms2/src/cmsgmt.c @ 3:2c135c81b16c
MERGE: upstream PyMuPDF 1.26.4 with MuPDF 1.26.7
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:44:09 +0200 |
| parents | b50eed0cc0ef |
| children |
comparison
equal
deleted
inserted
replaced
| 0:6015a75abc2d | 3:2c135c81b16c |
|---|---|
| 1 //--------------------------------------------------------------------------------- | |
| 2 // | |
| 3 // Little Color Management System | |
| 4 // Copyright (c) 1998-2021 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 { | |
| 42 cmsHTRANSFORM xform; | |
| 43 cmsHPROFILE hLab; | |
| 44 cmsHPROFILE ProfileList[256]; | |
| 45 cmsBool BPCList[256]; | |
| 46 cmsFloat64Number AdaptationList[256]; | |
| 47 cmsUInt32Number IntentList[256]; | |
| 48 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 if (nProfiles > 254) return NULL; | |
| 53 | |
| 54 // The output space | |
| 55 hLab = cmsCreateLab4Profile(ContextID, NULL); | |
| 56 if (hLab == NULL) return NULL; | |
| 57 | |
| 58 // Create a copy of parameters | |
| 59 for (i=0; i < nProfiles; i++) { | |
| 60 | |
| 61 ProfileList[i] = hProfiles[i]; | |
| 62 BPCList[i] = BPC[i]; | |
| 63 AdaptationList[i] = AdaptationStates[i]; | |
| 64 IntentList[i] = Intents[i]; | |
| 65 } | |
| 66 | |
| 67 // Place Lab identity at chain's end. | |
| 68 ProfileList[nProfiles] = hLab; | |
| 69 BPCList[nProfiles] = 0; | |
| 70 AdaptationList[nProfiles] = 1.0; | |
| 71 IntentList[nProfiles] = INTENT_RELATIVE_COLORIMETRIC; | |
| 72 | |
| 73 // Create the transform | |
| 74 xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList, | |
| 75 BPCList, | |
| 76 IntentList, | |
| 77 AdaptationList, | |
| 78 NULL, 0, | |
| 79 InputFormat, | |
| 80 OutputFormat, | |
| 81 dwFlags); | |
| 82 | |
| 83 cmsCloseProfile(ContextID, hLab); | |
| 84 | |
| 85 return xform; | |
| 86 } | |
| 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 { | |
| 101 cmsToneCurve* out = NULL; | |
| 102 cmsUInt32Number i; | |
| 103 cmsHTRANSFORM xform; | |
| 104 cmsCIELab Lab; | |
| 105 cmsFloat32Number cmyk[4]; | |
| 106 cmsFloat32Number* SampledPoints; | |
| 107 | |
| 108 xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags); | |
| 109 if (xform == NULL) return NULL; | |
| 110 | |
| 111 SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number)); | |
| 112 if (SampledPoints == NULL) goto Error; | |
| 113 | |
| 114 for (i=0; i < nPoints; i++) { | |
| 115 | |
| 116 cmyk[0] = 0; | |
| 117 cmyk[1] = 0; | |
| 118 cmyk[2] = 0; | |
| 119 cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1)); | |
| 120 | |
| 121 cmsDoTransform(ContextID, xform, cmyk, &Lab, 1); | |
| 122 SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation | |
| 123 } | |
| 124 | |
| 125 out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints); | |
| 126 | |
| 127 Error: | |
| 128 | |
| 129 cmsDeleteTransform(ContextID, xform); | |
| 130 if (SampledPoints) _cmsFree(ContextID, SampledPoints); | |
| 131 | |
| 132 return out; | |
| 133 } | |
| 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 { | |
| 148 cmsToneCurve *in, *out, *KTone; | |
| 149 | |
| 150 // Make sure CMYK -> CMYK | |
| 151 if (cmsGetColorSpace(ContextID, hProfiles[0]) != cmsSigCmykData || | |
| 152 cmsGetColorSpace(ContextID, hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL; | |
| 153 | |
| 154 | |
| 155 // Make sure last is an output profile | |
| 156 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 in = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags); | |
| 161 if (in == NULL) return NULL; | |
| 162 | |
| 163 out = ComputeKToLstar(ContextID, nPoints, 1, | |
| 164 Intents + (nProfiles - 1), | |
| 165 &hProfiles [nProfiles - 1], | |
| 166 BPC + (nProfiles - 1), | |
| 167 AdaptationStates + (nProfiles - 1), | |
| 168 dwFlags); | |
| 169 if (out == NULL) { | |
| 170 cmsFreeToneCurve(ContextID, in); | |
| 171 return NULL; | |
| 172 } | |
| 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 KTone = cmsJoinToneCurve(ContextID, in, out, nPoints); | |
| 177 | |
| 178 // Get rid of components | |
| 179 cmsFreeToneCurve(ContextID, in); cmsFreeToneCurve(ContextID, out); | |
| 180 | |
| 181 // Something went wrong... | |
| 182 if (KTone == NULL) return NULL; | |
| 183 | |
| 184 // Make sure it is monotonic | |
| 185 if (!cmsIsToneCurveMonotonic(ContextID, KTone)) { | |
| 186 cmsFreeToneCurve(ContextID, KTone); | |
| 187 return NULL; | |
| 188 } | |
| 189 | |
| 190 return KTone; | |
| 191 } | |
| 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 Threshold; // The threshold 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_THRESHOLD | |
| 208 // of maximum are considered out of gamut. | |
| 209 | |
| 210 #define ERR_THRESHOLD 5 | |
| 211 | |
| 212 | |
| 213 static | |
| 214 int GamutSampler(cmsContext ContextID, CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo) | |
| 215 { | |
| 216 GAMUTCHAIN* t = (GAMUTCHAIN* ) Cargo; | |
| 217 cmsCIELab LabIn1, LabOut1; | |
| 218 cmsCIELab LabIn2, LabOut2; | |
| 219 cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS]; | |
| 220 cmsFloat64Number dE1, dE2, ErrorRatio; | |
| 221 | |
| 222 // Assume in-gamut by default. NEVER READ, USED FOR DEBUG PURPOSES. | |
| 223 ErrorRatio = 1.0; | |
| 224 | |
| 225 // Convert input to Lab | |
| 226 cmsDoTransform(ContextID, t -> hInput, In, &LabIn1, 1); | |
| 227 | |
| 228 // converts from PCS to colorant. This always | |
| 229 // does return in-gamut values, | |
| 230 cmsDoTransform(ContextID, t -> hForward, &LabIn1, Proof, 1); | |
| 231 | |
| 232 // Now, do the inverse, from colorant to PCS. | |
| 233 cmsDoTransform(ContextID, t -> hReverse, Proof, &LabOut1, 1); | |
| 234 | |
| 235 memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab)); | |
| 236 | |
| 237 // Try again, but this time taking Check as input | |
| 238 cmsDoTransform(ContextID, t -> hForward, &LabOut1, Proof2, 1); | |
| 239 cmsDoTransform(ContextID, t -> hReverse, Proof2, &LabOut2, 1); | |
| 240 | |
| 241 // Take difference of direct value | |
| 242 dE1 = cmsDeltaE(ContextID, &LabIn1, &LabOut1); | |
| 243 | |
| 244 // Take difference of converted value | |
| 245 dE2 = cmsDeltaE(ContextID, &LabIn2, &LabOut2); | |
| 246 | |
| 247 | |
| 248 // if dE1 is small and dE2 is small, value is likely to be in gamut | |
| 249 if (dE1 < t->Threshold && dE2 < t->Threshold) | |
| 250 Out[0] = 0; | |
| 251 else { | |
| 252 | |
| 253 // if dE1 is small and dE2 is big, undefined. Assume in gamut | |
| 254 if (dE1 < t->Threshold && dE2 > t->Threshold) | |
| 255 Out[0] = 0; | |
| 256 else | |
| 257 // dE1 is big and dE2 is small, clearly out of gamut | |
| 258 if (dE1 > t->Threshold && dE2 < t->Threshold) | |
| 259 Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Threshold) + .5); | |
| 260 else { | |
| 261 | |
| 262 // dE1 is big and dE2 is also big, could be due to perceptual mapping | |
| 263 // so take error ratio | |
| 264 if (dE2 == 0.0) | |
| 265 ErrorRatio = dE1; | |
| 266 else | |
| 267 ErrorRatio = dE1 / dE2; | |
| 268 | |
| 269 if (ErrorRatio > t->Threshold) | |
| 270 Out[0] = (cmsUInt16Number) _cmsQuickFloor((ErrorRatio - t->Threshold) + .5); | |
| 271 else | |
| 272 Out[0] = 0; | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 | |
| 277 return TRUE; | |
| 278 } | |
| 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 { | |
| 295 cmsHPROFILE hLab; | |
| 296 cmsPipeline* Gamut; | |
| 297 cmsStage* CLUT; | |
| 298 cmsUInt32Number dwFormat; | |
| 299 GAMUTCHAIN Chain; | |
| 300 cmsUInt32Number nGridpoints; | |
| 301 cmsInt32Number nChannels; | |
| 302 cmsColorSpaceSignature ColorSpace; | |
| 303 cmsUInt32Number i; | |
| 304 cmsHPROFILE ProfileList[256]; | |
| 305 cmsBool BPCList[256]; | |
| 306 cmsFloat64Number AdaptationList[256]; | |
| 307 cmsUInt32Number IntentList[256]; | |
| 308 | |
| 309 memset(&Chain, 0, sizeof(GAMUTCHAIN)); | |
| 310 | |
| 311 | |
| 312 if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) { | |
| 313 cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition); | |
| 314 return NULL; | |
| 315 } | |
| 316 | |
| 317 hLab = cmsCreateLab4Profile(ContextID, NULL); | |
| 318 if (hLab == NULL) return NULL; | |
| 319 | |
| 320 | |
| 321 // The figure of merit. On matrix-shaper profiles, should be almost zero as | |
| 322 // the conversion is pretty exact. On LUT based profiles, different resolutions | |
| 323 // of input and output CLUT may result in differences. | |
| 324 | |
| 325 if (cmsIsMatrixShaper(ContextID, hGamut)) { | |
| 326 | |
| 327 Chain.Threshold = 1.0; | |
| 328 } | |
| 329 else { | |
| 330 Chain.Threshold = ERR_THRESHOLD; | |
| 331 } | |
| 332 | |
| 333 | |
| 334 // Create a copy of parameters | |
| 335 for (i=0; i < nGamutPCSposition; i++) { | |
| 336 ProfileList[i] = hProfiles[i]; | |
| 337 BPCList[i] = BPC[i]; | |
| 338 AdaptationList[i] = AdaptationStates[i]; | |
| 339 IntentList[i] = Intents[i]; | |
| 340 } | |
| 341 | |
| 342 // Fill Lab identity | |
| 343 ProfileList[nGamutPCSposition] = hLab; | |
| 344 BPCList[nGamutPCSposition] = 0; | |
| 345 AdaptationList[nGamutPCSposition] = 1.0; | |
| 346 IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC; | |
| 347 | |
| 348 | |
| 349 ColorSpace = cmsGetColorSpace(ContextID, hGamut); | |
| 350 nChannels = cmsChannelsOfColorSpace(ContextID, ColorSpace); | |
| 351 nGridpoints = _cmsReasonableGridpointsByColorspace(ContextID, ColorSpace, cmsFLAGS_HIGHRESPRECALC); | |
| 352 dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2)); | |
| 353 | |
| 354 // 16 bits to Lab double | |
| 355 Chain.hInput = cmsCreateExtendedTransform(ContextID, | |
| 356 nGamutPCSposition + 1, | |
| 357 ProfileList, | |
| 358 BPCList, | |
| 359 IntentList, | |
| 360 AdaptationList, | |
| 361 NULL, 0, | |
| 362 dwFormat, TYPE_Lab_DBL, | |
| 363 cmsFLAGS_NOCACHE); | |
| 364 | |
| 365 | |
| 366 // Does create the forward step. Lab double to device | |
| 367 dwFormat = (CHANNELS_SH(nChannels)|BYTES_SH(2)); | |
| 368 Chain.hForward = cmsCreateTransform(ContextID, | |
| 369 hLab, TYPE_Lab_DBL, | |
| 370 hGamut, dwFormat, | |
| 371 INTENT_RELATIVE_COLORIMETRIC, | |
| 372 cmsFLAGS_NOCACHE); | |
| 373 | |
| 374 // Does create the backwards step | |
| 375 Chain.hReverse = cmsCreateTransform(ContextID, hGamut, dwFormat, | |
| 376 hLab, TYPE_Lab_DBL, | |
| 377 INTENT_RELATIVE_COLORIMETRIC, | |
| 378 cmsFLAGS_NOCACHE); | |
| 379 | |
| 380 | |
| 381 // All ok? | |
| 382 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 Gamut = cmsPipelineAlloc(ContextID, 3, 1); | |
| 388 if (Gamut != NULL) { | |
| 389 | |
| 390 CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL); | |
| 391 if (!cmsPipelineInsertStage(ContextID, Gamut, cmsAT_BEGIN, CLUT)) { | |
| 392 cmsPipelineFree(ContextID, Gamut); | |
| 393 Gamut = NULL; | |
| 394 } | |
| 395 else { | |
| 396 cmsStageSampleCLut16bit(ContextID, CLUT, GamutSampler, (void*) &Chain, 0); | |
| 397 } | |
| 398 } | |
| 399 } | |
| 400 else | |
| 401 Gamut = NULL; // Didn't work... | |
| 402 | |
| 403 // Free all needed stuff. | |
| 404 if (Chain.hInput) cmsDeleteTransform(ContextID, Chain.hInput); | |
| 405 if (Chain.hForward) cmsDeleteTransform(ContextID, Chain.hForward); | |
| 406 if (Chain.hReverse) cmsDeleteTransform(ContextID, Chain.hReverse); | |
| 407 if (hLab) cmsCloseProfile(ContextID, hLab); | |
| 408 | |
| 409 // And return computed hull | |
| 410 return Gamut; | |
| 411 } | |
| 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 { | |
| 429 cmsTACestimator* bp = (cmsTACestimator*) Cargo; | |
| 430 cmsFloat32Number RoundTrip[cmsMAXCHANNELS]; | |
| 431 cmsUInt32Number i; | |
| 432 cmsFloat32Number Sum; | |
| 433 | |
| 434 | |
| 435 // Evaluate the xform | |
| 436 cmsDoTransform(ContextID, bp->hRoundTrip, In, RoundTrip, 1); | |
| 437 | |
| 438 // All all amounts of ink | |
| 439 for (Sum=0, i=0; i < bp ->nOutputChans; i++) | |
| 440 Sum += RoundTrip[i]; | |
| 441 | |
| 442 // If above maximum, keep track of input values | |
| 443 if (Sum > bp ->MaxTAC) { | |
| 444 | |
| 445 bp ->MaxTAC = Sum; | |
| 446 | |
| 447 for (i=0; i < bp ->nOutputChans; i++) { | |
| 448 bp ->MaxInput[i] = In[i]; | |
| 449 } | |
| 450 } | |
| 451 | |
| 452 return TRUE; | |
| 453 | |
| 454 cmsUNUSED_PARAMETER(Out); | |
| 455 } | |
| 456 | |
| 457 | |
| 458 // Detect Total area coverage of the profile | |
| 459 cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsContext ContextID, cmsHPROFILE hProfile) | |
| 460 { | |
| 461 cmsTACestimator bp; | |
| 462 cmsUInt32Number dwFormatter; | |
| 463 cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS]; | |
| 464 cmsHPROFILE hLab; | |
| 465 | |
| 466 // TAC only works on output profiles | |
| 467 if (cmsGetDeviceClass(ContextID, hProfile) != cmsSigOutputClass) { | |
| 468 return 0; | |
| 469 } | |
| 470 | |
| 471 // Create a fake formatter for result | |
| 472 dwFormatter = cmsFormatterForColorspaceOfProfile(ContextID, hProfile, 4, TRUE); | |
| 473 | |
| 474 // Unsupported color space? | |
| 475 if (dwFormatter == 0) return 0; | |
| 476 | |
| 477 bp.nOutputChans = T_CHANNELS(dwFormatter); | |
| 478 bp.MaxTAC = 0; // Initial TAC is 0 | |
| 479 | |
| 480 // for safety | |
| 481 if (bp.nOutputChans >= cmsMAXCHANNELS) return 0; | |
| 482 | |
| 483 hLab = cmsCreateLab4Profile(ContextID, NULL); | |
| 484 if (hLab == NULL) return 0; | |
| 485 // Setup a roundtrip on perceptual intent in output profile for TAC estimation | |
| 486 bp.hRoundTrip = cmsCreateTransform(ContextID, hLab, TYPE_Lab_16, | |
| 487 hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE); | |
| 488 | |
| 489 cmsCloseProfile(ContextID, hLab); | |
| 490 if (bp.hRoundTrip == NULL) return 0; | |
| 491 | |
| 492 // For L* we only need black and white. For C* we need many points | |
| 493 GridPoints[0] = 6; | |
| 494 GridPoints[1] = 74; | |
| 495 GridPoints[2] = 74; | |
| 496 | |
| 497 | |
| 498 if (!cmsSliceSpace16(ContextID, 3, GridPoints, EstimateTAC, &bp)) { | |
| 499 bp.MaxTAC = 0; | |
| 500 } | |
| 501 | |
| 502 cmsDeleteTransform(ContextID, bp.hRoundTrip); | |
| 503 | |
| 504 // Results in % | |
| 505 return bp.MaxTAC; | |
| 506 } | |
| 507 | |
| 508 | |
| 509 // Carefully, clamp on CIELab space. | |
| 510 | |
| 511 cmsBool CMSEXPORT cmsDesaturateLab(cmsContext ContextID, cmsCIELab* Lab, | |
| 512 double amax, double amin, | |
| 513 double bmax, double bmin) | |
| 514 { | |
| 515 | |
| 516 // Whole Luma surface to zero | |
| 517 | |
| 518 if (Lab -> L < 0) { | |
| 519 | |
| 520 Lab-> L = Lab->a = Lab-> b = 0.0; | |
| 521 return FALSE; | |
| 522 } | |
| 523 | |
| 524 // Clamp white, DISCARD HIGHLIGHTS. This is done | |
| 525 // in such way because icc spec doesn't allow the | |
| 526 // use of L>100 as a highlight means. | |
| 527 | |
| 528 if (Lab->L > 100) | |
| 529 Lab -> L = 100; | |
| 530 | |
| 531 // Check out gamut prism, on a, b faces | |
| 532 | |
| 533 if (Lab -> a < amin || Lab->a > amax|| | |
| 534 Lab -> b < bmin || Lab->b > bmax) { | |
| 535 | |
| 536 cmsCIELCh LCh; | |
| 537 double h, slope; | |
| 538 | |
| 539 // Falls outside a, b limits. Transports to LCh space, | |
| 540 // and then do the clipping | |
| 541 | |
| 542 | |
| 543 if (Lab -> a == 0.0) { // Is hue exactly 90? | |
| 544 | |
| 545 // atan will not work, so clamp here | |
| 546 Lab -> b = Lab->b < 0 ? bmin : bmax; | |
| 547 return TRUE; | |
| 548 } | |
| 549 | |
| 550 cmsLab2LCh(ContextID, &LCh, Lab); | |
| 551 | |
| 552 slope = Lab -> b / Lab -> a; | |
| 553 h = LCh.h; | |
| 554 | |
| 555 // There are 4 zones | |
| 556 | |
| 557 if ((h >= 0. && h < 45.) || | |
| 558 (h >= 315 && h <= 360.)) { | |
| 559 | |
| 560 // clip by amax | |
| 561 Lab -> a = amax; | |
| 562 Lab -> b = amax * slope; | |
| 563 } | |
| 564 else | |
| 565 if (h >= 45. && h < 135.) | |
| 566 { | |
| 567 // clip by bmax | |
| 568 Lab -> b = bmax; | |
| 569 Lab -> a = bmax / slope; | |
| 570 } | |
| 571 else | |
| 572 if (h >= 135. && h < 225.) { | |
| 573 // clip by amin | |
| 574 Lab -> a = amin; | |
| 575 Lab -> b = amin * slope; | |
| 576 | |
| 577 } | |
| 578 else | |
| 579 if (h >= 225. && h < 315.) { | |
| 580 // clip by bmin | |
| 581 Lab -> b = bmin; | |
| 582 Lab -> a = bmin / slope; | |
| 583 } | |
| 584 else { | |
| 585 cmsSignalError(0, cmsERROR_RANGE, "Invalid angle"); | |
| 586 return FALSE; | |
| 587 } | |
| 588 | |
| 589 } | |
| 590 | |
| 591 return TRUE; | |
| 592 } | |
| 593 | |
| 594 // Detect whatever a given ICC profile works in linear (gamma 1.0) space | |
| 595 // Actually, doing that "well" is quite hard, since every component may behave completely different. | |
| 596 // Since the true point of this function is to detect suitable optimizations, I am imposing some requirements | |
| 597 // that simplifies things: only RGB, and only profiles that can got in both directions. | |
| 598 // The algorithm obtains Y from a synthetical gray R=G=B. Then least squares fitting is used to estimate gamma. | |
| 599 // For gamma close to 1.0, RGB is linear. On profiles not supported, -1 is returned. | |
| 600 | |
| 601 cmsFloat64Number CMSEXPORT cmsDetectRGBProfileGamma(cmsContext ContextID, cmsHPROFILE hProfile, cmsFloat64Number threshold) | |
| 602 { | |
| 603 cmsHPROFILE hXYZ; | |
| 604 cmsHTRANSFORM xform; | |
| 605 cmsToneCurve* Y_curve; | |
| 606 cmsUInt16Number rgb[256][3]; | |
| 607 cmsCIEXYZ XYZ[256]; | |
| 608 cmsFloat32Number Y_normalized[256]; | |
| 609 cmsFloat64Number gamma; | |
| 610 cmsProfileClassSignature cl; | |
| 611 int i; | |
| 612 | |
| 613 if (cmsGetColorSpace(ContextID, hProfile) != cmsSigRgbData) | |
| 614 return -1; | |
| 615 | |
| 616 cl = cmsGetDeviceClass(ContextID, hProfile); | |
| 617 if (cl != cmsSigInputClass && cl != cmsSigDisplayClass && | |
| 618 cl != cmsSigOutputClass && cl != cmsSigColorSpaceClass) | |
| 619 return -1; | |
| 620 | |
| 621 hXYZ = cmsCreateXYZProfile(ContextID); | |
| 622 if (hXYZ == NULL) | |
| 623 return -1; | |
| 624 xform = cmsCreateTransform(ContextID, hProfile, TYPE_RGB_16, hXYZ, TYPE_XYZ_DBL, | |
| 625 INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE); | |
| 626 | |
| 627 if (xform == NULL) { // If not RGB or forward direction is not supported, regret with the previous error | |
| 628 | |
| 629 cmsCloseProfile(ContextID, hXYZ); | |
| 630 return -1; | |
| 631 } | |
| 632 | |
| 633 for (i = 0; i < 256; i++) { | |
| 634 rgb[i][0] = rgb[i][1] = rgb[i][2] = FROM_8_TO_16(i); | |
| 635 } | |
| 636 | |
| 637 cmsDoTransform(ContextID, xform, rgb, XYZ, 256); | |
| 638 | |
| 639 cmsDeleteTransform(ContextID, xform); | |
| 640 cmsCloseProfile(ContextID, hXYZ); | |
| 641 | |
| 642 for (i = 0; i < 256; i++) { | |
| 643 Y_normalized[i] = (cmsFloat32Number) XYZ[i].Y; | |
| 644 } | |
| 645 | |
| 646 Y_curve = cmsBuildTabulatedToneCurveFloat(ContextID, 256, Y_normalized); | |
| 647 if (Y_curve == NULL) | |
| 648 return -1; | |
| 649 | |
| 650 gamma = cmsEstimateGamma(ContextID, Y_curve, threshold); | |
| 651 | |
| 652 cmsFreeToneCurve(ContextID, Y_curve); | |
| 653 | |
| 654 return gamma; | |
| 655 } |
