Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/lcms2/src/cmssamp.c @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 1 //--------------------------------------------------------------------------------- | |
| 2 // | |
| 3 // Little Color Management System | |
| 4 // Copyright (c) 1998-2023 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 #define cmsmin(a, b) (((a) < (b)) ? (a) : (b)) | |
| 31 #define cmsmax(a, b) (((a) > (b)) ? (a) : (b)) | |
| 32 | |
| 33 // This file contains routines for resampling and LUT optimization, black point detection | |
| 34 // and black preservation. | |
| 35 | |
| 36 // Black point detection ------------------------------------------------------------------------- | |
| 37 | |
| 38 | |
| 39 // PCS -> PCS round trip transform, always uses relative intent on the device -> pcs | |
| 40 static | |
| 41 cmsHTRANSFORM CreateRoundtripXForm(cmsContext ContextID, cmsHPROFILE hProfile, cmsUInt32Number nIntent) | |
| 42 { | |
| 43 cmsHPROFILE hLab = cmsCreateLab4Profile(ContextID, NULL); | |
| 44 cmsHTRANSFORM xform; | |
| 45 cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE }; | |
| 46 cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 }; | |
| 47 cmsHPROFILE hProfiles[4]; | |
| 48 cmsUInt32Number Intents[4]; | |
| 49 | |
| 50 hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab; | |
| 51 Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC; | |
| 52 | |
| 53 xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents, | |
| 54 States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE); | |
| 55 | |
| 56 cmsCloseProfile(ContextID, hLab); | |
| 57 return xform; | |
| 58 } | |
| 59 | |
| 60 // Use darker colorants to obtain black point. This works in the relative colorimetric intent and | |
| 61 // assumes more ink results in darker colors. No ink limit is assumed. | |
| 62 static | |
| 63 cmsBool BlackPointAsDarkerColorant(cmsContext ContextID, | |
| 64 cmsHPROFILE hInput, | |
| 65 cmsUInt32Number Intent, | |
| 66 cmsCIEXYZ* BlackPoint, | |
| 67 cmsUInt32Number dwFlags) | |
| 68 { | |
| 69 cmsUInt16Number *Black; | |
| 70 cmsHTRANSFORM xform; | |
| 71 cmsColorSpaceSignature Space; | |
| 72 cmsUInt32Number nChannels; | |
| 73 cmsUInt32Number dwFormat; | |
| 74 cmsHPROFILE hLab; | |
| 75 cmsCIELab Lab; | |
| 76 cmsCIEXYZ BlackXYZ; | |
| 77 | |
| 78 // If the profile does not support input direction, assume Black point 0 | |
| 79 if (!cmsIsIntentSupported(ContextID, hInput, Intent, LCMS_USED_AS_INPUT)) { | |
| 80 | |
| 81 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 82 return FALSE; | |
| 83 } | |
| 84 | |
| 85 // Create a formatter which has n channels and no floating point | |
| 86 dwFormat = cmsFormatterForColorspaceOfProfile(ContextID, hInput, 2, FALSE); | |
| 87 | |
| 88 // Try to get black by using black colorant | |
| 89 Space = cmsGetColorSpace(ContextID, hInput); | |
| 90 | |
| 91 // This function returns darker colorant in 16 bits for several spaces | |
| 92 if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) { | |
| 93 | |
| 94 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 95 return FALSE; | |
| 96 } | |
| 97 | |
| 98 if (nChannels != T_CHANNELS(dwFormat)) { | |
| 99 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 100 return FALSE; | |
| 101 } | |
| 102 | |
| 103 // Lab will be used as the output space, but lab2 will avoid recursion | |
| 104 hLab = cmsCreateLab2Profile(ContextID, NULL); | |
| 105 if (hLab == NULL) { | |
| 106 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 107 return FALSE; | |
| 108 } | |
| 109 | |
| 110 // Create the transform | |
| 111 xform = cmsCreateTransform(ContextID, hInput, dwFormat, | |
| 112 hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE); | |
| 113 cmsCloseProfile(ContextID, hLab); | |
| 114 | |
| 115 if (xform == NULL) { | |
| 116 | |
| 117 // Something went wrong. Get rid of open resources and return zero as black | |
| 118 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 119 return FALSE; | |
| 120 } | |
| 121 | |
| 122 // Convert black to Lab | |
| 123 cmsDoTransform(ContextID, xform, Black, &Lab, 1); | |
| 124 | |
| 125 // Force it to be neutral, check for inconsistencies | |
| 126 if (Lab.L > 95) | |
| 127 Lab.L = 0; // Synthetic negative profiles | |
| 128 else if (Lab.L > 50) | |
| 129 Lab.L = 50; | |
| 130 else if (Lab.L < 0) | |
| 131 Lab.L = 0; | |
| 132 Lab.a = Lab.b = 0; | |
| 133 | |
| 134 // Free the resources | |
| 135 cmsDeleteTransform(ContextID, xform); | |
| 136 | |
| 137 // Convert from Lab (which is now clipped) to XYZ. | |
| 138 cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &Lab); | |
| 139 | |
| 140 if (BlackPoint != NULL) | |
| 141 *BlackPoint = BlackXYZ; | |
| 142 | |
| 143 return TRUE; | |
| 144 | |
| 145 cmsUNUSED_PARAMETER(dwFlags); | |
| 146 } | |
| 147 | |
| 148 // Get a black point of output CMYK profile, discounting any ink-limiting embedded | |
| 149 // in the profile. For doing that, we use perceptual intent in input direction: | |
| 150 // Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab | |
| 151 static | |
| 152 cmsBool BlackPointUsingPerceptualBlack(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile) | |
| 153 { | |
| 154 cmsHTRANSFORM hRoundTrip; | |
| 155 cmsCIELab LabIn, LabOut; | |
| 156 cmsCIEXYZ BlackXYZ; | |
| 157 | |
| 158 // Is the intent supported by the profile? | |
| 159 if (!cmsIsIntentSupported(ContextID, hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) { | |
| 160 | |
| 161 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 162 return TRUE; | |
| 163 } | |
| 164 | |
| 165 hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, INTENT_PERCEPTUAL); | |
| 166 if (hRoundTrip == NULL) { | |
| 167 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 168 return FALSE; | |
| 169 } | |
| 170 | |
| 171 LabIn.L = LabIn.a = LabIn.b = 0; | |
| 172 cmsDoTransform(ContextID, hRoundTrip, &LabIn, &LabOut, 1); | |
| 173 | |
| 174 // Clip Lab to reasonable limits | |
| 175 if (LabOut.L > 50) LabOut.L = 50; | |
| 176 LabOut.a = LabOut.b = 0; | |
| 177 | |
| 178 cmsDeleteTransform(ContextID, hRoundTrip); | |
| 179 | |
| 180 // Convert it to XYZ | |
| 181 cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &LabOut); | |
| 182 | |
| 183 if (BlackPoint != NULL) | |
| 184 *BlackPoint = BlackXYZ; | |
| 185 | |
| 186 return TRUE; | |
| 187 } | |
| 188 | |
| 189 // This function shouldn't exist at all -- there is such quantity of broken | |
| 190 // profiles on black point tag, that we must somehow fix chromaticity to | |
| 191 // avoid huge tint when doing Black point compensation. This function does | |
| 192 // just that. There is a special flag for using black point tag, but turned | |
| 193 // off by default because it is bogus on most profiles. The detection algorithm | |
| 194 // involves to turn BP to neutral and to use only L component. | |
| 195 cmsBool CMSEXPORT cmsDetectBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags) | |
| 196 { | |
| 197 cmsProfileClassSignature devClass; | |
| 198 | |
| 199 // Make sure the device class is adequate | |
| 200 devClass = cmsGetDeviceClass(ContextID, hProfile); | |
| 201 if (devClass == cmsSigLinkClass || | |
| 202 devClass == cmsSigAbstractClass || | |
| 203 devClass == cmsSigNamedColorClass) { | |
| 204 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 205 return FALSE; | |
| 206 } | |
| 207 | |
| 208 // Make sure intent is adequate | |
| 209 if (Intent != INTENT_PERCEPTUAL && | |
| 210 Intent != INTENT_RELATIVE_COLORIMETRIC && | |
| 211 Intent != INTENT_SATURATION) { | |
| 212 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 213 return FALSE; | |
| 214 } | |
| 215 | |
| 216 // v4 + perceptual & saturation intents does have its own black point, and it is | |
| 217 // well specified enough to use it. Black point tag is deprecated in V4. | |
| 218 if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) && | |
| 219 (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { | |
| 220 | |
| 221 // Matrix shaper share MRC & perceptual intents | |
| 222 if (cmsIsMatrixShaper(ContextID, hProfile)) | |
| 223 return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0); | |
| 224 | |
| 225 // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents | |
| 226 BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; | |
| 227 BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; | |
| 228 BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; | |
| 229 | |
| 230 return TRUE; | |
| 231 } | |
| 232 | |
| 233 | |
| 234 #ifdef CMS_USE_PROFILE_BLACK_POINT_TAG | |
| 235 | |
| 236 // v2, v4 rel/abs colorimetric | |
| 237 if (cmsIsTag(ContextID, hProfile, cmsSigMediaBlackPointTag) && | |
| 238 Intent == INTENT_RELATIVE_COLORIMETRIC) { | |
| 239 | |
| 240 cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite; | |
| 241 cmsCIELab Lab; | |
| 242 | |
| 243 // If black point is specified, then use it, | |
| 244 | |
| 245 BlackPtr = cmsReadTag(ContextID, hProfile, cmsSigMediaBlackPointTag); | |
| 246 if (BlackPtr != NULL) { | |
| 247 | |
| 248 BlackXYZ = *BlackPtr; | |
| 249 _cmsReadMediaWhitePoint(ContextID, &MediaWhite, hProfile); | |
| 250 | |
| 251 // Black point is absolute XYZ, so adapt to D50 to get PCS value | |
| 252 cmsAdaptToIlluminant(ContextID, &UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(ContextID), &BlackXYZ); | |
| 253 | |
| 254 // Force a=b=0 to get rid of any chroma | |
| 255 cmsXYZ2Lab(ContextID, NULL, &Lab, &UntrustedBlackPoint); | |
| 256 Lab.a = Lab.b = 0; | |
| 257 if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50 | |
| 258 cmsLab2XYZ(ContextID, NULL, &TrustedBlackPoint, &Lab); | |
| 259 | |
| 260 if (BlackPoint != NULL) | |
| 261 *BlackPoint = TrustedBlackPoint; | |
| 262 | |
| 263 return TRUE; | |
| 264 } | |
| 265 } | |
| 266 #endif | |
| 267 | |
| 268 // That is about v2 profiles. | |
| 269 | |
| 270 // If output profile, discount ink-limiting and that's all | |
| 271 if (Intent == INTENT_RELATIVE_COLORIMETRIC && | |
| 272 (cmsGetDeviceClass(ContextID, hProfile) == cmsSigOutputClass) && | |
| 273 (cmsGetColorSpace(ContextID, hProfile) == cmsSigCmykData)) | |
| 274 return BlackPointUsingPerceptualBlack(ContextID, BlackPoint, hProfile); | |
| 275 | |
| 276 // Nope, compute BP using current intent. | |
| 277 return BlackPointAsDarkerColorant(ContextID, hProfile, Intent, BlackPoint, dwFlags); | |
| 278 } | |
| 279 | |
| 280 | |
| 281 | |
| 282 // --------------------------------------------------------------------------------------------------------- | |
| 283 | |
| 284 // Least Squares Fit of a Quadratic Curve to Data | |
| 285 // http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html | |
| 286 | |
| 287 static | |
| 288 cmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(cmsContext ContextID, int n, cmsFloat64Number x[], cmsFloat64Number y[]) | |
| 289 { | |
| 290 double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0; | |
| 291 double sum_y = 0, sum_yx = 0, sum_yx2 = 0; | |
| 292 double d, a, b, c; | |
| 293 int i; | |
| 294 cmsMAT3 m; | |
| 295 cmsVEC3 v, res; | |
| 296 | |
| 297 if (n < 4) return 0; | |
| 298 | |
| 299 for (i=0; i < n; i++) { | |
| 300 | |
| 301 double xn = x[i]; | |
| 302 double yn = y[i]; | |
| 303 | |
| 304 sum_x += xn; | |
| 305 sum_x2 += xn*xn; | |
| 306 sum_x3 += xn*xn*xn; | |
| 307 sum_x4 += xn*xn*xn*xn; | |
| 308 | |
| 309 sum_y += yn; | |
| 310 sum_yx += yn*xn; | |
| 311 sum_yx2 += yn*xn*xn; | |
| 312 } | |
| 313 | |
| 314 _cmsVEC3init(ContextID, &m.v[0], n, sum_x, sum_x2); | |
| 315 _cmsVEC3init(ContextID, &m.v[1], sum_x, sum_x2, sum_x3); | |
| 316 _cmsVEC3init(ContextID, &m.v[2], sum_x2, sum_x3, sum_x4); | |
| 317 | |
| 318 _cmsVEC3init(ContextID, &v, sum_y, sum_yx, sum_yx2); | |
| 319 | |
| 320 if (!_cmsMAT3solve(ContextID, &res, &m, &v)) return 0; | |
| 321 | |
| 322 | |
| 323 a = res.n[2]; | |
| 324 b = res.n[1]; | |
| 325 c = res.n[0]; | |
| 326 | |
| 327 if (fabs(a) < 1.0E-10) { | |
| 328 | |
| 329 if (fabs(b) < 1.0E-10) return 0; | |
| 330 return cmsmin(0, cmsmax(50, -c/b )); | |
| 331 } | |
| 332 else { | |
| 333 | |
| 334 d = b*b - 4.0 * a * c; | |
| 335 if (d <= 0) { | |
| 336 return 0; | |
| 337 } | |
| 338 else { | |
| 339 | |
| 340 double rt; | |
| 341 | |
| 342 if (fabs(a) < 1.0E-10) return 0; | |
| 343 | |
| 344 rt = (-b + sqrt(d)) / (2.0 * a); | |
| 345 | |
| 346 return cmsmax(0, cmsmin(50, rt)); | |
| 347 } | |
| 348 } | |
| 349 | |
| 350 } | |
| 351 | |
| 352 | |
| 353 | |
| 354 // Calculates the black point of a destination profile. | |
| 355 // This algorithm comes from the Adobe paper disclosing its black point compensation method. | |
| 356 cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsContext ContextID, cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags) | |
| 357 { | |
| 358 cmsColorSpaceSignature ColorSpace; | |
| 359 cmsHTRANSFORM hRoundTrip = NULL; | |
| 360 cmsCIELab InitialLab, destLab, Lab; | |
| 361 cmsFloat64Number inRamp[256], outRamp[256]; | |
| 362 cmsFloat64Number MinL, MaxL; | |
| 363 cmsBool NearlyStraightMidrange = TRUE; | |
| 364 cmsFloat64Number yRamp[256]; | |
| 365 cmsFloat64Number x[256], y[256]; | |
| 366 cmsFloat64Number lo, hi; | |
| 367 int n, l; | |
| 368 cmsProfileClassSignature devClass; | |
| 369 | |
| 370 // Make sure the device class is adequate | |
| 371 devClass = cmsGetDeviceClass(ContextID, hProfile); | |
| 372 if (devClass == cmsSigLinkClass || | |
| 373 devClass == cmsSigAbstractClass || | |
| 374 devClass == cmsSigNamedColorClass) { | |
| 375 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 376 return FALSE; | |
| 377 } | |
| 378 | |
| 379 // Make sure intent is adequate | |
| 380 if (Intent != INTENT_PERCEPTUAL && | |
| 381 Intent != INTENT_RELATIVE_COLORIMETRIC && | |
| 382 Intent != INTENT_SATURATION) { | |
| 383 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 384 return FALSE; | |
| 385 } | |
| 386 | |
| 387 | |
| 388 // v4 + perceptual & saturation intents does have its own black point, and it is | |
| 389 // well specified enough to use it. Black point tag is deprecated in V4. | |
| 390 if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) && | |
| 391 (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { | |
| 392 | |
| 393 // Matrix shaper share MRC & perceptual intents | |
| 394 if (cmsIsMatrixShaper(ContextID, hProfile)) | |
| 395 return BlackPointAsDarkerColorant(ContextID, hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0); | |
| 396 | |
| 397 // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents | |
| 398 BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; | |
| 399 BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; | |
| 400 BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; | |
| 401 return TRUE; | |
| 402 } | |
| 403 | |
| 404 | |
| 405 // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document) | |
| 406 ColorSpace = cmsGetColorSpace(ContextID, hProfile); | |
| 407 if (!cmsIsCLUT(ContextID, hProfile, Intent, LCMS_USED_AS_OUTPUT ) || | |
| 408 (ColorSpace != cmsSigGrayData && | |
| 409 ColorSpace != cmsSigRgbData && | |
| 410 ColorSpace != cmsSigCmykData)) { | |
| 411 | |
| 412 // In this case, handle as input case | |
| 413 return cmsDetectBlackPoint(ContextID, BlackPoint, hProfile, Intent, dwFlags); | |
| 414 } | |
| 415 | |
| 416 // It is one of the valid cases!, use Adobe algorithm | |
| 417 | |
| 418 | |
| 419 // Set a first guess, that should work on good profiles. | |
| 420 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { | |
| 421 | |
| 422 cmsCIEXYZ IniXYZ; | |
| 423 | |
| 424 // calculate initial Lab as source black point | |
| 425 if (!cmsDetectBlackPoint(ContextID, &IniXYZ, hProfile, Intent, dwFlags)) { | |
| 426 return FALSE; | |
| 427 } | |
| 428 | |
| 429 // convert the XYZ to lab | |
| 430 cmsXYZ2Lab(ContextID, NULL, &InitialLab, &IniXYZ); | |
| 431 | |
| 432 } else { | |
| 433 | |
| 434 // set the initial Lab to zero, that should be the black point for perceptual and saturation | |
| 435 InitialLab.L = 0; | |
| 436 InitialLab.a = 0; | |
| 437 InitialLab.b = 0; | |
| 438 } | |
| 439 | |
| 440 | |
| 441 // Step 2 | |
| 442 // ====== | |
| 443 | |
| 444 // Create a roundtrip. Define a Transform BT for all x in L*a*b* | |
| 445 hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, Intent); | |
| 446 if (hRoundTrip == NULL) return FALSE; | |
| 447 | |
| 448 // Compute ramps | |
| 449 | |
| 450 for (l=0; l < 256; l++) { | |
| 451 | |
| 452 Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0; | |
| 453 Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a)); | |
| 454 Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b)); | |
| 455 | |
| 456 cmsDoTransform(ContextID, hRoundTrip, &Lab, &destLab, 1); | |
| 457 | |
| 458 inRamp[l] = Lab.L; | |
| 459 outRamp[l] = destLab.L; | |
| 460 } | |
| 461 | |
| 462 // Make monotonic | |
| 463 for (l = 254; l > 0; --l) { | |
| 464 outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]); | |
| 465 } | |
| 466 | |
| 467 // Check | |
| 468 if (! (outRamp[0] < outRamp[255])) { | |
| 469 | |
| 470 cmsDeleteTransform(ContextID, hRoundTrip); | |
| 471 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 472 return FALSE; | |
| 473 } | |
| 474 | |
| 475 | |
| 476 // Test for mid range straight (only on relative colorimetric) | |
| 477 NearlyStraightMidrange = TRUE; | |
| 478 MinL = outRamp[0]; MaxL = outRamp[255]; | |
| 479 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { | |
| 480 | |
| 481 for (l=0; l < 256; l++) { | |
| 482 | |
| 483 if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) || | |
| 484 (fabs(inRamp[l] - outRamp[l]) < 4.0 ))) | |
| 485 NearlyStraightMidrange = FALSE; | |
| 486 } | |
| 487 | |
| 488 // If the mid range is straight (as determined above) then the | |
| 489 // DestinationBlackPoint shall be the same as initialLab. | |
| 490 // Otherwise, the DestinationBlackPoint shall be determined | |
| 491 // using curve fitting. | |
| 492 if (NearlyStraightMidrange) { | |
| 493 | |
| 494 cmsLab2XYZ(ContextID, NULL, BlackPoint, &InitialLab); | |
| 495 cmsDeleteTransform(ContextID, hRoundTrip); | |
| 496 return TRUE; | |
| 497 } | |
| 498 } | |
| 499 | |
| 500 | |
| 501 // curve fitting: The round-trip curve normally looks like a nearly constant section at the black point, | |
| 502 // with a corner and a nearly straight line to the white point. | |
| 503 for (l=0; l < 256; l++) { | |
| 504 | |
| 505 yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL); | |
| 506 } | |
| 507 | |
| 508 // find the black point using the least squares error quadratic curve fitting | |
| 509 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { | |
| 510 lo = 0.1; | |
| 511 hi = 0.5; | |
| 512 } | |
| 513 else { | |
| 514 | |
| 515 // Perceptual and saturation | |
| 516 lo = 0.03; | |
| 517 hi = 0.25; | |
| 518 } | |
| 519 | |
| 520 // Capture shadow points for the fitting. | |
| 521 n = 0; | |
| 522 for (l=0; l < 256; l++) { | |
| 523 | |
| 524 cmsFloat64Number ff = yRamp[l]; | |
| 525 | |
| 526 if (ff >= lo && ff < hi) { | |
| 527 x[n] = inRamp[l]; | |
| 528 y[n] = yRamp[l]; | |
| 529 n++; | |
| 530 } | |
| 531 } | |
| 532 | |
| 533 | |
| 534 // No suitable points | |
| 535 if (n < 3 ) { | |
| 536 cmsDeleteTransform(ContextID, hRoundTrip); | |
| 537 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; | |
| 538 return FALSE; | |
| 539 } | |
| 540 | |
| 541 | |
| 542 // fit and get the vertex of quadratic curve | |
| 543 Lab.L = RootOfLeastSquaresFitQuadraticCurve(ContextID, n, x, y); | |
| 544 | |
| 545 if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative | |
| 546 Lab.L = 0; | |
| 547 } | |
| 548 | |
| 549 Lab.a = InitialLab.a; | |
| 550 Lab.b = InitialLab.b; | |
| 551 | |
| 552 cmsLab2XYZ(ContextID, NULL, BlackPoint, &Lab); | |
| 553 | |
| 554 cmsDeleteTransform(ContextID, hRoundTrip); | |
| 555 return TRUE; | |
| 556 } |
