0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A// This file is available under and governed by the GNU General Public
0N/A// License version 2 only, as published by the Free Software Foundation.
0N/A// However, the following notice accompanied the original version of this
0N/A// file:
0N/A//
2693N/A//---------------------------------------------------------------------------------
0N/A//
2693N/A// Little Color Management System
2693N/A// Copyright (c) 1998-2010 Marti Maria Saguer
0N/A//
0N/A// Permission is hereby granted, free of charge, to any person obtaining
0N/A// a copy of this software and associated documentation files (the "Software"),
0N/A// to deal in the Software without restriction, including without limitation
0N/A// the rights to use, copy, modify, merge, publish, distribute, sublicense,
0N/A// and/or sell copies of the Software, and to permit persons to whom the Software
0N/A// is furnished to do so, subject to the following conditions:
0N/A//
0N/A// The above copyright notice and this permission notice shall be included in
0N/A// all copies or substantial portions of the Software.
0N/A//
0N/A// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0N/A// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
0N/A// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0N/A// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0N/A// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0N/A// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0N/A// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2693N/A//
2693N/A//---------------------------------------------------------------------------------
2693N/A//
0N/A
2693N/A#include "lcms2_internal.h"
0N/A
0N/A
0N/A
2693N/A// This file contains routines for resampling and LUT optimization, black point detection
2693N/A// and black preservation.
0N/A
2693N/A// Black point detection -------------------------------------------------------------------------
0N/A
0N/A
2693N/A// PCS -> PCS round trip transform, always uses relative intent on the device -> pcs
2693N/Astatic
2693N/AcmsHTRANSFORM CreateRoundtripXForm(cmsHPROFILE hProfile, cmsUInt32Number nIntent)
0N/A{
2693N/A cmsHPROFILE hLab = cmsCreateLab4Profile(NULL);
2693N/A cmsHTRANSFORM xform;
2693N/A cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE };
2693N/A cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 };
2693N/A cmsHPROFILE hProfiles[4];
2693N/A cmsUInt32Number Intents[4];
2693N/A cmsContext ContextID = cmsGetProfileContextID(hProfile);
0N/A
2693N/A hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab;
2693N/A Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC;
0N/A
2693N/A xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents,
2693N/A States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE);
2693N/A
2693N/A cmsCloseProfile(hLab);
2693N/A return xform;
2693N/A}
0N/A
2693N/A// Use darker colorants to obtain black point. This works in the relative colorimetric intent and
2693N/A// assumes more ink results in darker colors. No ink limit is assumed.
2693N/Astatic
2693N/AcmsBool BlackPointAsDarkerColorant(cmsHPROFILE hInput,
2693N/A cmsUInt32Number Intent,
2693N/A cmsCIEXYZ* BlackPoint,
2693N/A cmsUInt32Number dwFlags)
2693N/A{
2693N/A cmsUInt16Number *Black;
2693N/A cmsHTRANSFORM xform;
2693N/A cmsColorSpaceSignature Space;
2693N/A cmsUInt32Number nChannels;
2693N/A cmsUInt32Number dwFormat;
2693N/A cmsHPROFILE hLab;
2693N/A cmsCIELab Lab;
2693N/A cmsCIEXYZ BlackXYZ;
2693N/A cmsContext ContextID = cmsGetProfileContextID(hInput);
0N/A
2693N/A // If the profile does not support input direction, assume Black point 0
2693N/A if (!cmsIsIntentSupported(hInput, Intent, LCMS_USED_AS_INPUT)) {
0N/A
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
0N/A
2693N/A // Create a formatter which has n channels and floating point
2693N/A dwFormat = cmsFormatterForColorspaceOfProfile(hInput, 2, FALSE);
2693N/A
2693N/A // Try to get black by using black colorant
2693N/A Space = cmsGetColorSpace(hInput);
2693N/A
2693N/A // This function returns darker colorant in 16 bits for several spaces
2693N/A if (!_cmsEndPointsBySpace(Space, NULL, &Black, &nChannels)) {
0N/A
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
2693N/A
2693N/A if (nChannels != T_CHANNELS(dwFormat)) {
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
2693N/A
2693N/A // Lab will be used as the output space, but lab2 will avoid recursion
2693N/A hLab = cmsCreateLab2ProfileTHR(ContextID, NULL);
2693N/A if (hLab == NULL) {
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
0N/A
2693N/A // Create the transform
2693N/A xform = cmsCreateTransformTHR(ContextID, hInput, dwFormat,
2693N/A hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
2693N/A cmsCloseProfile(hLab);
0N/A
2693N/A if (xform == NULL) {
2693N/A // Something went wrong. Get rid of open resources and return zero as black
2693N/A
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
2693N/A
2693N/A // Convert black to Lab
2693N/A cmsDoTransform(xform, Black, &Lab, 1);
0N/A
2693N/A // Force it to be neutral, clip to max. L* of 50
2693N/A Lab.a = Lab.b = 0;
2693N/A if (Lab.L > 50) Lab.L = 50;
0N/A
2693N/A // Free the resources
2693N/A cmsDeleteTransform(xform);
0N/A
2693N/A // Convert from Lab (which is now clipped) to XYZ.
2693N/A cmsLab2XYZ(NULL, &BlackXYZ, &Lab);
0N/A
2693N/A if (BlackPoint != NULL)
2693N/A *BlackPoint = BlackXYZ;
2693N/A
2693N/A return TRUE;
2693N/A
2693N/A cmsUNUSED_PARAMETER(dwFlags);
2693N/A}
0N/A
2693N/A// Get a black point of output CMYK profile, discounting any ink-limiting embedded
2693N/A// in the profile. For doing that, we use perceptual intent in input direction:
2693N/A// Lab (0, 0, 0) -> [Perceptual] Profile -> CMYK -> [Rel. colorimetric] Profile -> Lab
2693N/Astatic
2693N/AcmsBool BlackPointUsingPerceptualBlack(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile)
0N/A
2693N/A{
2693N/A cmsHTRANSFORM hRoundTrip;
2693N/A cmsCIELab LabIn, LabOut;
2693N/A cmsCIEXYZ BlackXYZ;
2693N/A
2693N/A // Is the intent supported by the profile?
2693N/A if (!cmsIsIntentSupported(hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) {
2693N/A
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return TRUE;
2693N/A }
0N/A
2693N/A hRoundTrip = CreateRoundtripXForm(hProfile, INTENT_PERCEPTUAL);
2693N/A if (hRoundTrip == NULL) {
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
2693N/A }
2693N/A
2693N/A LabIn.L = LabIn.a = LabIn.b = 0;
2693N/A cmsDoTransform(hRoundTrip, &LabIn, &LabOut, 1);
0N/A
2693N/A // Clip Lab to reasonable limits
2693N/A if (LabOut.L > 50) LabOut.L = 50;
2693N/A LabOut.a = LabOut.b = 0;
2693N/A
2693N/A cmsDeleteTransform(hRoundTrip);
0N/A
2693N/A // Convert it to XYZ
2693N/A cmsLab2XYZ(NULL, &BlackXYZ, &LabOut);
0N/A
2693N/A if (BlackPoint != NULL)
2693N/A *BlackPoint = BlackXYZ;
0N/A
0N/A return TRUE;
0N/A}
0N/A
2693N/A// This function shouldn't exist at all -- there is such quantity of broken
2693N/A// profiles on black point tag, that we must somehow fix chromaticity to
2693N/A// avoid huge tint when doing Black point compensation. This function does
2693N/A// just that. There is a special flag for using black point tag, but turned
2693N/A// off by default because it is bogus on most profiles. The detection algorithm
2693N/A// involves to turn BP to neutral and to use only L component.
2693N/AcmsBool CMSEXPORT cmsDetectBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
0N/A{
0N/A
2693N/A // Zero for black point
2693N/A if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
2693N/A
2693N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
2693N/A return FALSE;
0N/A }
0N/A
2693N/A // v4 + perceptual & saturation intents does have its own black point, and it is
2693N/A // well specified enough to use it. Black point tag is deprecated in V4.
0N/A
2693N/A if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&
2693N/A (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
0N/A
2693N/A // Matrix shaper share MRC & perceptual intents
2693N/A if (cmsIsMatrixShaper(hProfile))
2693N/A return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
0N/A
2693N/A // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
2693N/A BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
2693N/A BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
2693N/A BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
0N/A
2693N/A return TRUE;
0N/A }
0N/A
0N/A
2693N/A#ifdef CMS_USE_PROFILE_BLACK_POINT_TAG
0N/A
2693N/A // v2, v4 rel/abs colorimetric
2693N/A if (cmsIsTag(hProfile, cmsSigMediaBlackPointTag) &&
2693N/A Intent == INTENT_RELATIVE_COLORIMETRIC) {
0N/A
2693N/A cmsCIEXYZ *BlackPtr, BlackXYZ, UntrustedBlackPoint, TrustedBlackPoint, MediaWhite;
2693N/A cmsCIELab Lab;
0N/A
2693N/A // If black point is specified, then use it,
0N/A
2693N/A BlackPtr = cmsReadTag(hProfile, cmsSigMediaBlackPointTag);
2693N/A if (BlackPtr != NULL) {
0N/A
2693N/A BlackXYZ = *BlackPtr;
2693N/A _cmsReadMediaWhitePoint(&MediaWhite, hProfile);
0N/A
2693N/A // Black point is absolute XYZ, so adapt to D50 to get PCS value
2693N/A cmsAdaptToIlluminant(&UntrustedBlackPoint, &MediaWhite, cmsD50_XYZ(), &BlackXYZ);
0N/A
2693N/A // Force a=b=0 to get rid of any chroma
2693N/A cmsXYZ2Lab(NULL, &Lab, &UntrustedBlackPoint);
2693N/A Lab.a = Lab.b = 0;
2693N/A if (Lab.L > 50) Lab.L = 50; // Clip to L* <= 50
2693N/A cmsLab2XYZ(NULL, &TrustedBlackPoint, &Lab);
0N/A
2693N/A if (BlackPoint != NULL)
2693N/A *BlackPoint = TrustedBlackPoint;
0N/A
2693N/A return TRUE;
2693N/A }
0N/A }
0N/A#endif
0N/A
2693N/A // That is about v2 profiles.
0N/A
2693N/A // If output profile, discount ink-limiting and that's all
2693N/A if (Intent == INTENT_RELATIVE_COLORIMETRIC &&
2693N/A (cmsGetDeviceClass(hProfile) == cmsSigOutputClass) &&
2693N/A (cmsGetColorSpace(hProfile) == cmsSigCmykData))
2693N/A return BlackPointUsingPerceptualBlack(BlackPoint, hProfile);
0N/A
2693N/A // Nope, compute BP using current intent.
2693N/A return BlackPointAsDarkerColorant(hProfile, Intent, BlackPoint, dwFlags);
0N/A}
0N/A
6271N/A
6271N/A
6271N/A// ---------------------------------------------------------------------------------------------------------
6271N/A
6271N/A// Least Squares Fit of a Quadratic Curve to Data
6271N/A// http://www.personal.psu.edu/jhm/f90/lectures/lsq2.html
6271N/A
6271N/Astatic
6271N/AcmsFloat64Number RootOfLeastSquaresFitQuadraticCurve(int n, cmsFloat64Number x[], cmsFloat64Number y[])
6271N/A{
6271N/A double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0;
6271N/A double sum_y = 0, sum_yx = 0, sum_yx2 = 0;
6271N/A double disc;
6271N/A int i;
6271N/A cmsMAT3 m;
6271N/A cmsVEC3 v, res;
6271N/A
6271N/A if (n < 4) return 0;
6271N/A
6271N/A for (i=0; i < n; i++) {
6271N/A
6271N/A double xn = x[i];
6271N/A double yn = y[i];
6271N/A
6271N/A sum_x += xn;
6271N/A sum_x2 += xn*xn;
6271N/A sum_x3 += xn*xn*xn;
6271N/A sum_x4 += xn*xn*xn*xn;
6271N/A
6271N/A sum_y += yn;
6271N/A sum_yx += yn*xn;
6271N/A sum_yx2 += yn*xn*xn;
6271N/A }
6271N/A
6271N/A _cmsVEC3init(&m.v[0], n, sum_x, sum_x2);
6271N/A _cmsVEC3init(&m.v[1], sum_x, sum_x2, sum_x3);
6271N/A _cmsVEC3init(&m.v[2], sum_x2, sum_x3, sum_x4);
6271N/A
6271N/A _cmsVEC3init(&v, sum_y, sum_yx, sum_yx2);
6271N/A
6271N/A if (!_cmsMAT3solve(&res, &m, &v)) return 0;
6271N/A
6271N/A // y = t x2 + u x + c
6271N/A // x = ( - u + Sqrt( u^2 - 4 t c ) ) / ( 2 t )
6271N/A disc = res.n[1]*res.n[1] - 4.0 * res.n[0] * res.n[2];
6271N/A if (disc < 0) return -1;
6271N/A
6271N/A return ( -1.0 * res.n[1] + sqrt( disc )) / (2.0 * res.n[0]);
6271N/A}
6271N/A
6271N/Astatic
6271N/AcmsBool IsMonotonic(int n, const cmsFloat64Number Table[])
6271N/A{
6271N/A int i;
6271N/A cmsFloat64Number last;
6271N/A
6271N/A last = Table[n-1];
6271N/A
6271N/A for (i = n-2; i >= 0; --i) {
6271N/A
6271N/A if (Table[i] > last)
6271N/A
6271N/A return FALSE;
6271N/A else
6271N/A last = Table[i];
6271N/A
6271N/A }
6271N/A
6271N/A return TRUE;
6271N/A}
6271N/A
6271N/A// Calculates the black point of a destination profile.
6271N/A// This algorithm comes from the Adobe paper disclosing its black point compensation method.
6271N/AcmsBool CMSEXPORT cmsDetectDestinationBlackPoint(cmsCIEXYZ* BlackPoint, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number dwFlags)
6271N/A{
6271N/A cmsColorSpaceSignature ColorSpace;
6271N/A cmsHTRANSFORM hRoundTrip = NULL;
6271N/A cmsCIELab InitialLab, destLab, Lab;
6271N/A
6271N/A cmsFloat64Number MinL, MaxL;
6271N/A cmsBool NearlyStraightMidRange = FALSE;
6271N/A cmsFloat64Number L;
6271N/A cmsFloat64Number x[101], y[101];
6271N/A cmsFloat64Number lo, hi, NonMonoMin;
6271N/A int n, l, i, NonMonoIndx;
6271N/A
6271N/A
6271N/A // Make sure intent is adequate
6271N/A if (Intent != INTENT_PERCEPTUAL &&
6271N/A Intent != INTENT_RELATIVE_COLORIMETRIC &&
6271N/A Intent != INTENT_SATURATION) {
6271N/A BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0;
6271N/A return FALSE;
6271N/A }
6271N/A
6271N/A
6271N/A // v4 + perceptual & saturation intents does have its own black point, and it is
6271N/A // well specified enough to use it. Black point tag is deprecated in V4.
6271N/A if ((cmsGetEncodedICCversion(hProfile) >= 0x4000000) &&
6271N/A (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) {
6271N/A
6271N/A // Matrix shaper share MRC & perceptual intents
6271N/A if (cmsIsMatrixShaper(hProfile))
6271N/A return BlackPointAsDarkerColorant(hProfile, INTENT_RELATIVE_COLORIMETRIC, BlackPoint, 0);
6271N/A
6271N/A // Get Perceptual black out of v4 profiles. That is fixed for perceptual & saturation intents
6271N/A BlackPoint -> X = cmsPERCEPTUAL_BLACK_X;
6271N/A BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y;
6271N/A BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z;
6271N/A return TRUE;
6271N/A }
6271N/A
6271N/A
6271N/A // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document)
6271N/A ColorSpace = cmsGetColorSpace(hProfile);
6271N/A if (!cmsIsCLUT(hProfile, Intent, LCMS_USED_AS_OUTPUT ) ||
6271N/A (ColorSpace != cmsSigGrayData &&
6271N/A ColorSpace != cmsSigRgbData &&
6271N/A ColorSpace != cmsSigCmykData)) {
6271N/A
6271N/A // In this case, handle as input case
6271N/A return cmsDetectBlackPoint(BlackPoint, hProfile, Intent, dwFlags);
6271N/A }
6271N/A
6271N/A // It is one of the valid cases!, presto chargo hocus pocus, go for the Adobe magic
6271N/A
6271N/A // Step 1
6271N/A // ======
6271N/A
6271N/A // Set a first guess, that should work on good profiles.
6271N/A if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
6271N/A
6271N/A cmsCIEXYZ IniXYZ;
6271N/A
6271N/A // calculate initial Lab as source black point
6271N/A if (!cmsDetectBlackPoint(&IniXYZ, hProfile, Intent, dwFlags)) {
6271N/A return FALSE;
6271N/A }
6271N/A
6271N/A // convert the XYZ to lab
6271N/A cmsXYZ2Lab(NULL, &InitialLab, &IniXYZ);
6271N/A
6271N/A } else {
6271N/A
6271N/A // set the initial Lab to zero, that should be the black point for perceptual and saturation
6271N/A InitialLab.L = 0;
6271N/A InitialLab.a = 0;
6271N/A InitialLab.b = 0;
6271N/A }
6271N/A
6271N/A
6271N/A // Step 2
6271N/A // ======
6271N/A
6271N/A // Create a roundtrip. Define a Transform BT for all x in L*a*b*
6271N/A hRoundTrip = CreateRoundtripXForm(hProfile, Intent);
6271N/A if (hRoundTrip == NULL) return FALSE;
6271N/A
6271N/A // Calculate Min L*
6271N/A Lab = InitialLab;
6271N/A Lab.L = 0;
6271N/A cmsDoTransform(hRoundTrip, &Lab, &destLab, 1);
6271N/A MinL = destLab.L;
6271N/A
6271N/A // Calculate Max L*
6271N/A Lab = InitialLab;
6271N/A Lab.L = 100;
6271N/A cmsDoTransform(hRoundTrip, &Lab, &destLab, 1);
6271N/A MaxL = destLab.L;
6271N/A
6271N/A // Step 3
6271N/A // ======
6271N/A
6271N/A // check if quadratic estimation needs to be done.
6271N/A if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
6271N/A
6271N/A // Conceptually, this code tests how close the source l and converted L are to one another in the mid-range
6271N/A // of the values. If the converted ramp of L values is close enough to a straight line y=x, then InitialLab
6271N/A // is good enough to be the DestinationBlackPoint,
6271N/A NearlyStraightMidRange = TRUE;
6271N/A
6271N/A for (l=0; l <= 100; l++) {
6271N/A
6271N/A Lab.L = l;
6271N/A Lab.a = InitialLab.a;
6271N/A Lab.b = InitialLab.b;
6271N/A
6271N/A cmsDoTransform(hRoundTrip, &Lab, &destLab, 1);
6271N/A
6271N/A L = destLab.L;
6271N/A
6271N/A // Check the mid range in 20% after MinL
6271N/A if (L > (MinL + 0.2 * (MaxL - MinL))) {
6271N/A
6271N/A // Is close enough?
6271N/A if (fabs(L - l) > 4.0) {
6271N/A
6271N/A // Too far away, profile is buggy!
6271N/A NearlyStraightMidRange = FALSE;
6271N/A break;
6271N/A }
6271N/A }
6271N/A }
6271N/A }
6271N/A else {
6271N/A // Check is always performed for perceptual and saturation intents
6271N/A NearlyStraightMidRange = FALSE;
6271N/A }
6271N/A
6271N/A
6271N/A // If no furter checking is needed, we are done
6271N/A if (NearlyStraightMidRange) {
6271N/A
6271N/A cmsLab2XYZ(NULL, BlackPoint, &InitialLab);
6271N/A cmsDeleteTransform(hRoundTrip);
6271N/A return TRUE;
6271N/A }
6271N/A
6271N/A // The round-trip curve normally looks like a nearly constant section at the black point,
6271N/A // with a corner and a nearly straight line to the white point.
6271N/A
6271N/A // STEP 4
6271N/A // =======
6271N/A
6271N/A // find the black point using the least squares error quadratic curve fitting
6271N/A
6271N/A if (Intent == INTENT_RELATIVE_COLORIMETRIC) {
6271N/A lo = 0.1;
6271N/A hi = 0.5;
6271N/A }
6271N/A else {
6271N/A
6271N/A // Perceptual and saturation
6271N/A lo = 0.03;
6271N/A hi = 0.25;
6271N/A }
6271N/A
6271N/A // Capture points for the fitting.
6271N/A n = 0;
6271N/A for (l=0; l <= 100; l++) {
6271N/A
6271N/A cmsFloat64Number ff;
6271N/A
6271N/A Lab.L = (cmsFloat64Number) l;
6271N/A Lab.a = InitialLab.a;
6271N/A Lab.b = InitialLab.b;
6271N/A
6271N/A cmsDoTransform(hRoundTrip, &Lab, &destLab, 1);
6271N/A
6271N/A ff = (destLab.L - MinL)/(MaxL - MinL);
6271N/A
6271N/A if (ff >= lo && ff < hi) {
6271N/A
6271N/A x[n] = Lab.L;
6271N/A y[n] = ff;
6271N/A n++;
6271N/A }
6271N/A
6271N/A }
6271N/A
6271N/A // This part is not on the Adobe paper, but I found is necessary for getting any result.
6271N/A
6271N/A if (IsMonotonic(n, y)) {
6271N/A
6271N/A // Monotonic means lower point is stil valid
6271N/A cmsLab2XYZ(NULL, BlackPoint, &InitialLab);
6271N/A cmsDeleteTransform(hRoundTrip);
6271N/A return TRUE;
6271N/A }
6271N/A
6271N/A // No suitable points, regret and use safer algorithm
6271N/A if (n == 0) {
6271N/A cmsDeleteTransform(hRoundTrip);
6271N/A return cmsDetectBlackPoint(BlackPoint, hProfile, Intent, dwFlags);
6271N/A }
6271N/A
6271N/A
6271N/A NonMonoMin = 100;
6271N/A NonMonoIndx = 0;
6271N/A for (i=0; i < n; i++) {
6271N/A
6271N/A if (y[i] < NonMonoMin) {
6271N/A NonMonoIndx = i;
6271N/A NonMonoMin = y[i];
6271N/A }
6271N/A }
6271N/A
6271N/A Lab.L = x[NonMonoIndx];
6271N/A
6271N/A // fit and get the vertex of quadratic curve
6271N/A Lab.L = RootOfLeastSquaresFitQuadraticCurve(n, x, y);
6271N/A
6271N/A if (Lab.L < 0.0 || Lab.L > 50.0) { // clip to zero L* if the vertex is negative
6271N/A Lab.L = 0;
6271N/A }
6271N/A
6271N/A Lab.a = InitialLab.a;
6271N/A Lab.b = InitialLab.b;
6271N/A
6271N/A cmsLab2XYZ(NULL, BlackPoint, &Lab);
6271N/A
6271N/A cmsDeleteTransform(hRoundTrip);
6271N/A return TRUE;
6271N/A}