1 /* 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. Oracle designates this 7 * particular file as subject to the "Classpath" exception as provided 8 * by Oracle in the LICENSE file that accompanied this code. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 // This file is available under and governed by the GNU General Public 26 // License version 2 only, as published by the Free Software Foundation. 27 // However, the following notice accompanied the original version of this 28 // file: 29 // 30 //--------------------------------------------------------------------------------- 31 // 32 // Little Color Management System 33 // Copyright (c) 1998-2014 Marti Maria Saguer 34 // 35 // Permission is hereby granted, free of charge, to any person obtaining 36 // a copy of this software and associated documentation files (the "Software"), 37 // to deal in the Software without restriction, including without limitation 38 // the rights to use, copy, modify, merge, publish, distribute, sublicense, 39 // and/or sell copies of the Software, and to permit persons to whom the Software 40 // is furnished to do so, subject to the following conditions: 41 // 42 // The above copyright notice and this permission notice shall be included in 43 // all copies or substantial portions of the Software. 44 // 45 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 46 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 47 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 48 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 49 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 50 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 51 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 52 // 53 //--------------------------------------------------------------------------------- 54 // 55 56 #include "lcms2_internal.h" 57 58 59 #define cmsmin(a, b) (((a) < (b)) ? (a) : (b)) 60 #define cmsmax(a, b) (((a) > (b)) ? (a) : (b)) 61 62 // This file contains routines for resampling and LUT optimization, black point detection 63 // and black preservation. 64 65 // Black point detection ------------------------------------------------------------------------- 66 67 68 // PCS -> PCS round trip transform, always uses relative intent on the device -> pcs 69 static 70 cmsHTRANSFORM CreateRoundtripXForm(cmsHPROFILE hProfile, cmsUInt32Number nIntent) 71 { 72 cmsContext ContextID = cmsGetProfileContextID(hProfile); 73 cmsHPROFILE hLab = cmsCreateLab4ProfileTHR(ContextID, NULL); 74 cmsHTRANSFORM xform; 75 cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE }; 76 cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 }; 77 cmsHPROFILE hProfiles[4]; 78 cmsUInt32Number Intents[4]; 79 80 hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab; 81 Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC; 82 83 xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents, 84 States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE); 85 86 cmsCloseProfile(hLab); 87 return xform; 88 } 89 90 // Use darker colorants to obtain black point. This works in the relative colorimetric intent and 91 // assumes more ink results in darker colors. No ink limit is assumed. 92 static 93 cmsBool BlackPointAsDarkerColorant(cmsHPROFILE hInput, 94 cmsUInt32Number Intent, 95 cmsCIEXYZ* BlackPoint, 96 cmsUInt32Number dwFlags) 97 { 98 cmsUInt16Number *Black; 99 cmsHTRANSFORM xform; 100 cmsColorSpaceSignature Space; 101 cmsUInt32Number nChannels; 102 cmsUInt32Number dwFormat; 103 cmsHPROFILE hLab; 104 cmsCIELab Lab; 105 cmsCIEXYZ BlackXYZ; 106 cmsContext ContextID = cmsGetProfileContextID(hInput); 107 108 // If the profile does not support input direction, assume Black point 0 109 if (!cmsIsIntentSupported(hInput, Intent, LCMS_USED_AS_INPUT)) { 110 111 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 112 return FALSE; 113 } 114 115 // Create a formatter which has n channels and floating point 116 dwFormat = cmsFormatterForColorspaceOfProfile(hInput, 2, FALSE); 117 118 // Try to get black by using black colorant 119 Space = cmsGetColorSpace(hInput); 120 121 // This function returns darker colorant in 16 bits for several spaces 122 if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) { 123 124 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 125 return FALSE; 126 } 127 128 if (nChannels != T_CHANNELS(dwFormat)) { 129 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 130 return FALSE; 131 } 132 133 // Lab will be used as the output space, but lab2 will avoid recursion 134 hLab = cmsCreateLab2ProfileTHR(ContextID, NULL); 135 if (hLab == NULL) { 136 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 137 return FALSE; 138 } 139 140 // Create the transform 141 xform = cmsCreateTransformTHR(ContextID, hInput, dwFormat, 142 hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE); 143 cmsCloseProfile(hLab); 144 145 if (xform == NULL) { 146 147 // Something went wrong. Get rid of open resources and return zero as black 148 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 149 return FALSE; 150 } 151 152 // Convert black to Lab 153 cmsDoTransform(xform, Black, &Lab, 1); 154 155 // Force it to be neutral, clip to max. L* of 50 156 Lab.a = Lab.b = 0; 157 if (Lab.L > 50) Lab.L = 50; 158 159 // Free the resources 160 cmsDeleteTransform(xform); 161 162 // Convert from Lab (which is now clipped) to XYZ. 163 cmsLab2XYZ(NULL, &BlackXYZ, &Lab); 164 165 if (BlackPoint != NULL) 166 *BlackPoint = BlackXYZ; 167 168 cmsUNUSED_PARAMETER(dwFlags); 169 170 return TRUE; 171 172 } 173 174 // Get a black point of output CMYK profile, discounting any ink-limiting embedded 175 // in the profile. For doing that, we use perceptual intent in input direction: 176 // Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab 177 static 178 cmsBool BlackPointUsingPerceptualBlack(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile) 179 { 180 cmsHTRANSFORM hRoundTrip; 181 cmsCIELab LabIn, LabOut; 182 cmsCIEXYZ BlackXYZ; 183 184 // Is the intent supported by the profile? 185 if (!cmsIsIntentSupported(hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) { 186 187 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 188 return TRUE; 189 } 190 191 hRoundTrip = CreateRoundtripXForm(hProfile, INTENT_PERCEPTUAL); 192 if (hRoundTrip == NULL) { 193 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 194 return FALSE; 195 } 196 197 LabIn.L = LabIn.a = LabIn.b = 0; 198 cmsDoTransform(hRoundTrip, &LabIn, &LabOut, 1); 199 200 // Clip Lab to reasonable limits 201 if (LabOut.L > 50) LabOut.L = 50; 202 LabOut.a = LabOut.b = 0; 203 204 cmsDeleteTransform(hRoundTrip); 205 206 // Convert it to XYZ 207 cmsLab2XYZ(NULL, &BlackXYZ, &LabOut); 208 209 if (BlackPoint != NULL) 210 *BlackPoint = BlackXYZ; 211 212 return TRUE; 213 } 214 215 // This function shouldn't exist at all -- there is such quantity of broken 216 // profiles on black point tag, that we must somehow fix chromaticity to 217 // avoid huge tint when doing Black point compensation. This function does 218 // just that. There is a special flag for using black point tag, but turned 219 // off by default because it is bogus on most profiles. The detection algorithm 220 // involves to turn BP to neutral and to use only L component. 221 cmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags) 222 { 223 cmsProfileClassSignature devClass; 224 225 // Make sure the device class is adequate 226 devClass = cmsGetDeviceClass(hProfile); 227 if (devClass == cmsSigLinkClass || 228 devClass == cmsSigAbstractClass || 229 devClass == cmsSigNamedColorClass) { 230 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 231 return FALSE; 232 } 233 234 // Make sure intent is adequate 235 if (Intent != INTENT_PERCEPTUAL && 236 Intent != INTENT_RELATIVE_COLORIMETRIC && 237 Intent != INTENT_SATURATION) { 238 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 239 return FALSE; 240 } 241 242 // v4 + perceptual & saturation intents does have its own black point, and it is 243 // well specified enough to use it. Black point tag is deprecated in V4. 244 if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) && 245 (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { 246 247 // Matrix shaper share MRC & perceptual intents 248 if (cmsIsMatrixShaper(hProfile)) 249 return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0); 250 251 // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents 252 BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; 253 BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; 254 BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; 255 256 return TRUE; 257 } 258 259 260 #ifdef CMS_USE_PROFILE_BLACK_POINT_TAG 261 262 // v2, v4 rel/abs colorimetric 263 if (cmsIsTag(hProfile, cmsSigMediaBlackPointTag) && 264 Intent == INTENT_RELATIVE_COLORIMETRIC) { 265 266 cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite; 267 cmsCIELab Lab; 268 269 // If black point is specified, then use it, 270 271 BlackPtr = cmsReadTag(hProfile, cmsSigMediaBlackPointTag); 272 if (BlackPtr != NULL) { 273 274 BlackXYZ = *BlackPtr; 275 _cmsReadMediaWhitePoint(&MediaWhite, hProfile); 276 277 // Black point is absolute XYZ, so adapt to D50 to get PCS value 278 cmsAdaptToIlluminant(&UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(), &BlackXYZ); 279 280 // Force a=b=0 to get rid of any chroma 281 cmsXYZ2Lab(NULL, &Lab, &UntrustedBlackPoint); 282 Lab.a = Lab.b = 0; 283 if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50 284 cmsLab2XYZ(NULL, &TrustedBlackPoint, &Lab); 285 286 if (BlackPoint != NULL) 287 *BlackPoint = TrustedBlackPoint; 288 289 return TRUE; 290 } 291 } 292 #endif 293 294 // That is about v2 profiles. 295 296 // If output profile, discount ink-limiting and that's all 297 if (Intent == INTENT_RELATIVE_COLORIMETRIC && 298 (cmsGetDeviceClass(hProfile) == cmsSigOutputClass) && 299 (cmsGetColorSpace(hProfile) == cmsSigCmykData)) 300 return BlackPointUsingPerceptualBlack(BlackPoint, hProfile); 301 302 // Nope, compute BP using current intent. 303 return BlackPointAsDarkerColorant(hProfile, Intent, BlackPoint, dwFlags); 304 } 305 306 307 308 // --------------------------------------------------------------------------------------------------------- 309 310 // Least Squares Fit of a Quadratic Curve to Data 311 // http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html 312 313 static 314 cmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(int n, cmsFloat64Number x[], cmsFloat64Number y[]) 315 { 316 double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0; 317 double sum_y = 0, sum_yx = 0, sum_yx2 = 0; 318 double d, a, b, c; 319 int i; 320 cmsMAT3 m; 321 cmsVEC3 v, res; 322 323 if (n < 4) return 0; 324 325 for (i=0; i < n; i++) { 326 327 double xn = x[i]; 328 double yn = y[i]; 329 330 sum_x += xn; 331 sum_x2 += xn*xn; 332 sum_x3 += xn*xn*xn; 333 sum_x4 += xn*xn*xn*xn; 334 335 sum_y += yn; 336 sum_yx += yn*xn; 337 sum_yx2 += yn*xn*xn; 338 } 339 340 _cmsVEC3init(&m.v[0], n, sum_x, sum_x2); 341 _cmsVEC3init(&m.v[1], sum_x, sum_x2, sum_x3); 342 _cmsVEC3init(&m.v[2], sum_x2, sum_x3, sum_x4); 343 344 _cmsVEC3init(&v, sum_y, sum_yx, sum_yx2); 345 346 if (!_cmsMAT3solve(&res, &m, &v)) return 0; 347 348 349 a = res.n[2]; 350 b = res.n[1]; 351 c = res.n[0]; 352 353 if (fabs(a) < 1.0E-10) { 354 355 return cmsmin(0, cmsmax(50, -c/b )); 356 } 357 else { 358 359 d = b*b - 4.0 * a * c; 360 if (d <= 0) { 361 return 0; 362 } 363 else { 364 365 double rt = (-b + sqrt(d)) / (2.0 * a); 366 367 return cmsmax(0, cmsmin(50, rt)); 368 } 369 } 370 371 } 372 373 374 375 // Calculates the black point of a destination profile. 376 // This algorithm comes from the Adobe paper disclosing its black point compensation method. 377 cmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags) 378 { 379 cmsColorSpaceSignature ColorSpace; 380 cmsHTRANSFORM hRoundTrip = NULL; 381 cmsCIELab InitialLab, destLab, Lab; 382 cmsFloat64Number inRamp[256], outRamp[256]; 383 cmsFloat64Number MinL, MaxL; 384 cmsBool NearlyStraightMidrange = TRUE; 385 cmsFloat64Number yRamp[256]; 386 cmsFloat64Number x[256], y[256]; 387 cmsFloat64Number lo, hi; 388 int n, l; 389 cmsProfileClassSignature devClass; 390 391 // Make sure the device class is adequate 392 devClass = cmsGetDeviceClass(hProfile); 393 if (devClass == cmsSigLinkClass || 394 devClass == cmsSigAbstractClass || 395 devClass == cmsSigNamedColorClass) { 396 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 397 return FALSE; 398 } 399 400 // Make sure intent is adequate 401 if (Intent != INTENT_PERCEPTUAL && 402 Intent != INTENT_RELATIVE_COLORIMETRIC && 403 Intent != INTENT_SATURATION) { 404 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 405 return FALSE; 406 } 407 408 409 // v4 + perceptual & saturation intents does have its own black point, and it is 410 // well specified enough to use it. Black point tag is deprecated in V4. 411 if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) && 412 (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { 413 414 // Matrix shaper share MRC & perceptual intents 415 if (cmsIsMatrixShaper(hProfile)) 416 return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0); 417 418 // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents 419 BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; 420 BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; 421 BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; 422 return TRUE; 423 } 424 425 426 // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document) 427 ColorSpace = cmsGetColorSpace(hProfile); 428 if (!cmsIsCLUT(hProfile, Intent, LCMS_USED_AS_OUTPUT ) || 429 (ColorSpace != cmsSigGrayData && 430 ColorSpace != cmsSigRgbData && 431 ColorSpace != cmsSigCmykData)) { 432 433 // In this case, handle as input case 434 return cmsDetectBlackPoint(BlackPoint, hProfile, Intent, dwFlags); 435 } 436 437 // It is one of the valid cases!, use Adobe algorithm 438 439 440 // Set a first guess, that should work on good profiles. 441 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { 442 443 cmsCIEXYZ IniXYZ; 444 445 // calculate initial Lab as source black point 446 if (!cmsDetectBlackPoint(&IniXYZ, hProfile, Intent, dwFlags)) { 447 return FALSE; 448 } 449 450 // convert the XYZ to lab 451 cmsXYZ2Lab(NULL, &InitialLab, &IniXYZ); 452 453 } else { 454 455 // set the initial Lab to zero, that should be the black point for perceptual and saturation 456 InitialLab.L = 0; 457 InitialLab.a = 0; 458 InitialLab.b = 0; 459 } 460 461 462 // Step 2 463 // ====== 464 465 // Create a roundtrip. Define a Transform BT for all x in L*a*b* 466 hRoundTrip = CreateRoundtripXForm(hProfile, Intent); 467 if (hRoundTrip == NULL) return FALSE; 468 469 // Compute ramps 470 471 for (l=0; l < 256; l++) { 472 473 Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0; 474 Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a)); 475 Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b)); 476 477 cmsDoTransform(hRoundTrip, &Lab, &destLab, 1); 478 479 inRamp[l] = Lab.L; 480 outRamp[l] = destLab.L; 481 } 482 483 // Make monotonic 484 for (l = 254; l > 0; --l) { 485 outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]); 486 } 487 488 // Check 489 if (! (outRamp[0] < outRamp[255])) { 490 491 cmsDeleteTransform(hRoundTrip); 492 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 493 return FALSE; 494 } 495 496 497 // Test for mid range straight (only on relative colorimetric) 498 NearlyStraightMidrange = TRUE; 499 MinL = outRamp[0]; MaxL = outRamp[255]; 500 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { 501 502 for (l=0; l < 256; l++) { 503 504 if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) || 505 (fabs(inRamp[l] - outRamp[l]) < 4.0 ))) 506 NearlyStraightMidrange = FALSE; 507 } 508 509 // If the mid range is straight (as determined above) then the 510 // DestinationBlackPoint shall be the same as initialLab. 511 // Otherwise, the DestinationBlackPoint shall be determined 512 // using curve fitting. 513 if (NearlyStraightMidrange) { 514 515 cmsLab2XYZ(NULL, BlackPoint, &InitialLab); 516 cmsDeleteTransform(hRoundTrip); 517 return TRUE; 518 } 519 } 520 521 522 // curve fitting: The round-trip curve normally looks like a nearly constant section at the black point, 523 // with a corner and a nearly straight line to the white point. 524 for (l=0; l < 256; l++) { 525 526 yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL); 527 } 528 529 // find the black point using the least squares error quadratic curve fitting 530 if (Intent == INTENT_RELATIVE_COLORIMETRIC) { 531 lo = 0.1; 532 hi = 0.5; 533 } 534 else { 535 536 // Perceptual and saturation 537 lo = 0.03; 538 hi = 0.25; 539 } 540 541 // Capture shadow points for the fitting. 542 n = 0; 543 for (l=0; l < 256; l++) { 544 545 cmsFloat64Number ff = yRamp[l]; 546 547 if (ff >= lo && ff < hi) { 548 x[n] = inRamp[l]; 549 y[n] = yRamp[l]; 550 n++; 551 } 552 } 553 554 555 // No suitable points 556 if (n < 3 ) { 557 cmsDeleteTransform(hRoundTrip); 558 BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; 559 return FALSE; 560 } 561 562 563 // fit and get the vertex of quadratic curve 564 Lab.L = RootOfLeastSquaresFitQuadraticCurve(n, x, y); 565 566 if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative 567 Lab.L = 0; 568 } 569 570 Lab.a = InitialLab.a; 571 Lab.b = InitialLab.b; 572 573 cmsLab2XYZ(NULL, BlackPoint, &Lab); 574 575 cmsDeleteTransform(hRoundTrip); 576 return TRUE; 577 }