/src/mupdf/thirdparty/lcms2/src/cmssamp.c
Line | Count | Source |
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 | 4.09k | #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(cmsContext ContextID, cmsHPROFILE hProfile, cmsUInt32Number nIntent) |
42 | 45 | { |
43 | 45 | cmsHPROFILE hLab = cmsCreateLab4Profile(ContextID, NULL); |
44 | 45 | cmsHTRANSFORM xform; |
45 | 45 | cmsBool BPC[4] = { FALSE, FALSE, FALSE, FALSE }; |
46 | 45 | cmsFloat64Number States[4] = { 1.0, 1.0, 1.0, 1.0 }; |
47 | 45 | cmsHPROFILE hProfiles[4]; |
48 | 45 | cmsUInt32Number Intents[4]; |
49 | | |
50 | 45 | hProfiles[0] = hLab; hProfiles[1] = hProfile; hProfiles[2] = hProfile; hProfiles[3] = hLab; |
51 | 45 | Intents[0] = INTENT_RELATIVE_COLORIMETRIC; Intents[1] = nIntent; Intents[2] = INTENT_RELATIVE_COLORIMETRIC; Intents[3] = INTENT_RELATIVE_COLORIMETRIC; |
52 | | |
53 | 45 | xform = cmsCreateExtendedTransform(ContextID, 4, hProfiles, BPC, Intents, |
54 | 45 | States, NULL, 0, TYPE_Lab_DBL, TYPE_Lab_DBL, cmsFLAGS_NOCACHE|cmsFLAGS_NOOPTIMIZE); |
55 | | |
56 | 45 | cmsCloseProfile(ContextID, hLab); |
57 | 45 | return xform; |
58 | 45 | } |
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 | 365 | { |
69 | 365 | cmsUInt16Number *Black; |
70 | 365 | cmsHTRANSFORM xform; |
71 | 365 | cmsColorSpaceSignature Space; |
72 | 365 | cmsUInt32Number nChannels; |
73 | 365 | cmsUInt32Number dwFormat; |
74 | 365 | cmsHPROFILE hLab; |
75 | 365 | cmsCIELab Lab; |
76 | 365 | cmsCIEXYZ BlackXYZ; |
77 | | |
78 | | // If the profile does not support input direction, assume Black point 0 |
79 | 365 | if (!cmsIsIntentSupported(ContextID, hInput, Intent, LCMS_USED_AS_INPUT)) { |
80 | | |
81 | 5 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
82 | 5 | return FALSE; |
83 | 5 | } |
84 | | |
85 | | // Create a formatter which has n channels and no floating point |
86 | 360 | dwFormat = cmsFormatterForColorspaceOfProfile(ContextID, hInput, 2, FALSE); |
87 | | |
88 | | // Try to get black by using black colorant |
89 | 360 | Space = cmsGetColorSpace(ContextID, hInput); |
90 | | |
91 | | // This function returns darker colorant in 16 bits for several spaces |
92 | 360 | 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 | 360 | 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 | 360 | hLab = cmsCreateLab2Profile(ContextID, NULL); |
105 | 360 | 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 | 360 | xform = cmsCreateTransform(ContextID, hInput, dwFormat, |
112 | 360 | hLab, TYPE_Lab_DBL, Intent, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE); |
113 | 360 | cmsCloseProfile(ContextID, hLab); |
114 | | |
115 | 360 | 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 | 360 | cmsDoTransform(ContextID, xform, Black, &Lab, 1); |
124 | | |
125 | | // Force it to be neutral, check for inconsistencies |
126 | 360 | if (Lab.L > 95) |
127 | 1 | Lab.L = 0; // Synthetic negative profiles |
128 | 359 | else if (Lab.L > 50) |
129 | 0 | Lab.L = 50; |
130 | 359 | else if (Lab.L < 0) |
131 | 0 | Lab.L = 0; |
132 | 360 | Lab.a = Lab.b = 0; |
133 | | |
134 | | // Free the resources |
135 | 360 | cmsDeleteTransform(ContextID, xform); |
136 | | |
137 | | // Convert from Lab (which is now clipped) to XYZ. |
138 | 360 | cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &Lab); |
139 | | |
140 | 360 | if (BlackPoint != NULL) |
141 | 360 | *BlackPoint = BlackXYZ; |
142 | | |
143 | 360 | return TRUE; |
144 | | |
145 | 0 | cmsUNUSED_PARAMETER(dwFlags); |
146 | 0 | } |
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 | 41 | { |
154 | 41 | cmsHTRANSFORM hRoundTrip; |
155 | 41 | cmsCIELab LabIn, LabOut; |
156 | 41 | cmsCIEXYZ BlackXYZ; |
157 | | |
158 | | // Is the intent supported by the profile? |
159 | 41 | if (!cmsIsIntentSupported(ContextID, hProfile, INTENT_PERCEPTUAL, LCMS_USED_AS_INPUT)) { |
160 | |
|
161 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
162 | 0 | return TRUE; |
163 | 0 | } |
164 | | |
165 | 41 | hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, INTENT_PERCEPTUAL); |
166 | 41 | if (hRoundTrip == NULL) { |
167 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
168 | 0 | return FALSE; |
169 | 0 | } |
170 | | |
171 | 41 | LabIn.L = LabIn.a = LabIn.b = 0; |
172 | 41 | cmsDoTransform(ContextID, hRoundTrip, &LabIn, &LabOut, 1); |
173 | | |
174 | | // Clip Lab to reasonable limits |
175 | 41 | if (LabOut.L > 50) LabOut.L = 50; |
176 | 41 | LabOut.a = LabOut.b = 0; |
177 | | |
178 | 41 | cmsDeleteTransform(ContextID, hRoundTrip); |
179 | | |
180 | | // Convert it to XYZ |
181 | 41 | cmsLab2XYZ(ContextID, NULL, &BlackXYZ, &LabOut); |
182 | | |
183 | 41 | if (BlackPoint != NULL) |
184 | 41 | *BlackPoint = BlackXYZ; |
185 | | |
186 | 41 | return TRUE; |
187 | 41 | } |
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 | 420 | { |
197 | 420 | cmsProfileClassSignature devClass; |
198 | | |
199 | | // Make sure the device class is adequate |
200 | 420 | devClass = cmsGetDeviceClass(ContextID, hProfile); |
201 | 420 | if (devClass == cmsSigLinkClass || |
202 | 420 | devClass == cmsSigAbstractClass || |
203 | 406 | devClass == cmsSigNamedColorClass) { |
204 | 14 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
205 | 14 | return FALSE; |
206 | 14 | } |
207 | | |
208 | | // Make sure intent is adequate |
209 | 406 | if (Intent != INTENT_PERCEPTUAL && |
210 | 254 | Intent != INTENT_RELATIVE_COLORIMETRIC && |
211 | 0 | Intent != INTENT_SATURATION) { |
212 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
213 | 0 | return FALSE; |
214 | 0 | } |
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 | 406 | if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) && |
219 | 40 | (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { |
220 | | |
221 | | // Matrix shaper share MRC & perceptual intents |
222 | 12 | if (cmsIsMatrixShaper(ContextID, hProfile)) |
223 | 0 | 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 | 12 | BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; |
227 | 12 | BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; |
228 | 12 | BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; |
229 | | |
230 | 12 | return TRUE; |
231 | 12 | } |
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 | 394 | if (Intent == INTENT_RELATIVE_COLORIMETRIC && |
272 | 254 | (cmsGetDeviceClass(ContextID, hProfile) == cmsSigOutputClass) && |
273 | 41 | (cmsGetColorSpace(ContextID, hProfile) == cmsSigCmykData)) |
274 | 41 | return BlackPointUsingPerceptualBlack(ContextID, BlackPoint, hProfile); |
275 | | |
276 | | // Nope, compute BP using current intent. |
277 | 353 | return BlackPointAsDarkerColorant(ContextID, hProfile, Intent, BlackPoint, dwFlags); |
278 | 394 | } |
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 | 0 | { |
290 | 0 | double sum_x = 0, sum_x2 = 0, sum_x3 = 0, sum_x4 = 0; |
291 | 0 | double sum_y = 0, sum_yx = 0, sum_yx2 = 0; |
292 | 0 | double d, a, b, c; |
293 | 0 | int i; |
294 | 0 | cmsMAT3 m; |
295 | 0 | cmsVEC3 v, res; |
296 | |
|
297 | 0 | if (n < 4) return 0; |
298 | | |
299 | 0 | for (i=0; i < n; i++) { |
300 | |
|
301 | 0 | double xn = x[i]; |
302 | 0 | double yn = y[i]; |
303 | |
|
304 | 0 | sum_x += xn; |
305 | 0 | sum_x2 += xn*xn; |
306 | 0 | sum_x3 += xn*xn*xn; |
307 | 0 | sum_x4 += xn*xn*xn*xn; |
308 | |
|
309 | 0 | sum_y += yn; |
310 | 0 | sum_yx += yn*xn; |
311 | 0 | sum_yx2 += yn*xn*xn; |
312 | 0 | } |
313 | |
|
314 | 0 | _cmsVEC3init(ContextID, &m.v[0], n, sum_x, sum_x2); |
315 | 0 | _cmsVEC3init(ContextID, &m.v[1], sum_x, sum_x2, sum_x3); |
316 | 0 | _cmsVEC3init(ContextID, &m.v[2], sum_x2, sum_x3, sum_x4); |
317 | |
|
318 | 0 | _cmsVEC3init(ContextID, &v, sum_y, sum_yx, sum_yx2); |
319 | |
|
320 | 0 | if (!_cmsMAT3solve(ContextID, &res, &m, &v)) return 0; |
321 | | |
322 | | |
323 | 0 | a = res.n[2]; |
324 | 0 | b = res.n[1]; |
325 | 0 | c = res.n[0]; |
326 | |
|
327 | 0 | if (fabs(a) < 1.0E-10) { |
328 | |
|
329 | 0 | if (fabs(b) < 1.0E-10) return 0; |
330 | 0 | return cmsmin(0, cmsmax(50, -c/b )); |
331 | 0 | } |
332 | 0 | else { |
333 | |
|
334 | 0 | d = b*b - 4.0 * a * c; |
335 | 0 | if (d <= 0) { |
336 | 0 | return 0; |
337 | 0 | } |
338 | 0 | else { |
339 | |
|
340 | 0 | double rt; |
341 | |
|
342 | 0 | if (fabs(a) < 1.0E-10) return 0; |
343 | | |
344 | 0 | rt = (-b + sqrt(d)) / (2.0 * a); |
345 | |
|
346 | 0 | return cmsmax(0, cmsmin(50, rt)); |
347 | 0 | } |
348 | 0 | } |
349 | |
|
350 | 0 | } |
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 | 223 | { |
358 | 223 | cmsColorSpaceSignature ColorSpace; |
359 | 223 | cmsHTRANSFORM hRoundTrip = NULL; |
360 | 223 | cmsCIELab InitialLab, destLab, Lab; |
361 | 223 | cmsFloat64Number inRamp[256], outRamp[256]; |
362 | 223 | cmsFloat64Number MinL, MaxL; |
363 | 223 | cmsBool NearlyStraightMidrange = TRUE; |
364 | 223 | cmsFloat64Number yRamp[256]; |
365 | 223 | cmsFloat64Number x[256], y[256]; |
366 | 223 | cmsFloat64Number lo, hi; |
367 | 223 | int n, l; |
368 | 223 | cmsProfileClassSignature devClass; |
369 | | |
370 | | // Make sure the device class is adequate |
371 | 223 | devClass = cmsGetDeviceClass(ContextID, hProfile); |
372 | 223 | if (devClass == cmsSigLinkClass || |
373 | 223 | devClass == cmsSigAbstractClass || |
374 | 223 | devClass == cmsSigNamedColorClass) { |
375 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
376 | 0 | return FALSE; |
377 | 0 | } |
378 | | |
379 | | // Make sure intent is adequate |
380 | 223 | if (Intent != INTENT_PERCEPTUAL && |
381 | 127 | Intent != INTENT_RELATIVE_COLORIMETRIC && |
382 | 0 | Intent != INTENT_SATURATION) { |
383 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
384 | 0 | return FALSE; |
385 | 0 | } |
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 | 223 | if ((cmsGetEncodedICCversion(ContextID, hProfile) >= 0x4000000) && |
391 | 40 | (Intent == INTENT_PERCEPTUAL || Intent == INTENT_SATURATION)) { |
392 | | |
393 | | // Matrix shaper share MRC & perceptual intents |
394 | 26 | if (cmsIsMatrixShaper(ContextID, hProfile)) |
395 | 12 | 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 | 14 | BlackPoint -> X = cmsPERCEPTUAL_BLACK_X; |
399 | 14 | BlackPoint -> Y = cmsPERCEPTUAL_BLACK_Y; |
400 | 14 | BlackPoint -> Z = cmsPERCEPTUAL_BLACK_Z; |
401 | 14 | return TRUE; |
402 | 26 | } |
403 | | |
404 | | |
405 | | // Check if the profile is lut based and gray, rgb or cmyk (7.2 in Adobe's document) |
406 | 197 | ColorSpace = cmsGetColorSpace(ContextID, hProfile); |
407 | 197 | if (!cmsIsCLUT(ContextID, hProfile, Intent, LCMS_USED_AS_OUTPUT ) || |
408 | 4 | (ColorSpace != cmsSigGrayData && |
409 | 4 | ColorSpace != cmsSigRgbData && |
410 | 193 | ColorSpace != cmsSigCmykData)) { |
411 | | |
412 | | // In this case, handle as input case |
413 | 193 | return cmsDetectBlackPoint(ContextID, BlackPoint, hProfile, Intent, dwFlags); |
414 | 193 | } |
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 | 4 | if (Intent == INTENT_RELATIVE_COLORIMETRIC) { |
421 | | |
422 | 4 | cmsCIEXYZ IniXYZ; |
423 | | |
424 | | // calculate initial Lab as source black point |
425 | 4 | if (!cmsDetectBlackPoint(ContextID, &IniXYZ, hProfile, Intent, dwFlags)) { |
426 | 0 | return FALSE; |
427 | 0 | } |
428 | | |
429 | | // convert the XYZ to lab |
430 | 4 | cmsXYZ2Lab(ContextID, NULL, &InitialLab, &IniXYZ); |
431 | | |
432 | 4 | } else { |
433 | | |
434 | | // set the initial Lab to zero, that should be the black point for perceptual and saturation |
435 | 0 | InitialLab.L = 0; |
436 | 0 | InitialLab.a = 0; |
437 | 0 | InitialLab.b = 0; |
438 | 0 | } |
439 | | |
440 | | |
441 | | // Step 2 |
442 | | // ====== |
443 | | |
444 | | // Create a roundtrip. Define a Transform BT for all x in L*a*b* |
445 | 4 | hRoundTrip = CreateRoundtripXForm(ContextID, hProfile, Intent); |
446 | 4 | if (hRoundTrip == NULL) return FALSE; |
447 | | |
448 | | // Compute ramps |
449 | | |
450 | 1.02k | for (l=0; l < 256; l++) { |
451 | | |
452 | 1.02k | Lab.L = (cmsFloat64Number) (l * 100.0) / 255.0; |
453 | 1.02k | Lab.a = cmsmin(50, cmsmax(-50, InitialLab.a)); |
454 | 1.02k | Lab.b = cmsmin(50, cmsmax(-50, InitialLab.b)); |
455 | | |
456 | 1.02k | cmsDoTransform(ContextID, hRoundTrip, &Lab, &destLab, 1); |
457 | | |
458 | 1.02k | inRamp[l] = Lab.L; |
459 | 1.02k | outRamp[l] = destLab.L; |
460 | 1.02k | } |
461 | | |
462 | | // Make monotonic |
463 | 1.02k | for (l = 254; l > 0; --l) { |
464 | 1.01k | outRamp[l] = cmsmin(outRamp[l], outRamp[l+1]); |
465 | 1.01k | } |
466 | | |
467 | | // Check |
468 | 4 | if (! (outRamp[0] < outRamp[255])) { |
469 | |
|
470 | 0 | cmsDeleteTransform(ContextID, hRoundTrip); |
471 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
472 | 0 | return FALSE; |
473 | 0 | } |
474 | | |
475 | | |
476 | | // Test for mid range straight (only on relative colorimetric) |
477 | 4 | NearlyStraightMidrange = TRUE; |
478 | 4 | MinL = outRamp[0]; MaxL = outRamp[255]; |
479 | 4 | if (Intent == INTENT_RELATIVE_COLORIMETRIC) { |
480 | | |
481 | 1.02k | for (l=0; l < 256; l++) { |
482 | | |
483 | 1.02k | if (! ((inRamp[l] <= MinL + 0.2 * (MaxL - MinL) ) || |
484 | 684 | (fabs(inRamp[l] - outRamp[l]) < 4.0 ))) |
485 | 0 | NearlyStraightMidrange = FALSE; |
486 | 1.02k | } |
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 | 4 | if (NearlyStraightMidrange) { |
493 | | |
494 | 4 | cmsLab2XYZ(ContextID, NULL, BlackPoint, &InitialLab); |
495 | 4 | cmsDeleteTransform(ContextID, hRoundTrip); |
496 | 4 | return TRUE; |
497 | 4 | } |
498 | 4 | } |
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 | 0 | for (l=0; l < 256; l++) { |
504 | |
|
505 | 0 | yRamp[l] = (outRamp[l] - MinL) / (MaxL - MinL); |
506 | 0 | } |
507 | | |
508 | | // find the black point using the least squares error quadratic curve fitting |
509 | 0 | if (Intent == INTENT_RELATIVE_COLORIMETRIC) { |
510 | 0 | lo = 0.1; |
511 | 0 | hi = 0.5; |
512 | 0 | } |
513 | 0 | else { |
514 | | |
515 | | // Perceptual and saturation |
516 | 0 | lo = 0.03; |
517 | 0 | hi = 0.25; |
518 | 0 | } |
519 | | |
520 | | // Capture shadow points for the fitting. |
521 | 0 | n = 0; |
522 | 0 | for (l=0; l < 256; l++) { |
523 | |
|
524 | 0 | cmsFloat64Number ff = yRamp[l]; |
525 | |
|
526 | 0 | if (ff >= lo && ff < hi) { |
527 | 0 | x[n] = inRamp[l]; |
528 | 0 | y[n] = yRamp[l]; |
529 | 0 | n++; |
530 | 0 | } |
531 | 0 | } |
532 | | |
533 | | |
534 | | // No suitable points |
535 | 0 | if (n < 3 ) { |
536 | 0 | cmsDeleteTransform(ContextID, hRoundTrip); |
537 | 0 | BlackPoint -> X = BlackPoint ->Y = BlackPoint -> Z = 0.0; |
538 | 0 | return FALSE; |
539 | 0 | } |
540 | | |
541 | | |
542 | | // fit and get the vertex of quadratic curve |
543 | 0 | Lab.L = RootOfLeastSquaresFitQuadraticCurve(ContextID, n, x, y); |
544 | |
|
545 | 0 | if (Lab.L < 0.0) { // clip to zero L* if the vertex is negative |
546 | 0 | Lab.L = 0; |
547 | 0 | } |
548 | |
|
549 | 0 | Lab.a = InitialLab.a; |
550 | 0 | Lab.b = InitialLab.b; |
551 | |
|
552 | 0 | cmsLab2XYZ(ContextID, NULL, BlackPoint, &Lab); |
553 | |
|
554 | 0 | cmsDeleteTransform(ContextID, hRoundTrip); |
555 | 0 | return TRUE; |
556 | 0 | } |