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