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