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-2021 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 // Auxiliary: append a Lab identity after the given sequence of profiles
 60 // and return the transform. Lab profile is closed, rest of profiles are kept open.
 61 cmsHTRANSFORM _cmsChain2Lab(cmsContext            ContextID,
 62                             cmsUInt32Number        nProfiles,
 63                             cmsUInt32Number        InputFormat,
 64                             cmsUInt32Number        OutputFormat,
 65                             const cmsUInt32Number  Intents[],
 66                             const cmsHPROFILE      hProfiles[],
 67                             const cmsBool          BPC[],
 68                             const cmsFloat64Number AdaptationStates[],
 69                             cmsUInt32Number        dwFlags)
 70 {
 71     cmsHTRANSFORM xform;
 72     cmsHPROFILE   hLab;
 73     cmsHPROFILE   ProfileList[256];
 74     cmsBool       BPCList[256];
 75     cmsFloat64Number AdaptationList[256];
 76     cmsUInt32Number IntentList[256];
 77     cmsUInt32Number i;
 78 
 79     // This is a rather big number and there is no need of dynamic memory
 80     // since we are adding a profile, 254 + 1 = 255 and this is the limit
 81     if (nProfiles > 254) return NULL;
 82 
 83     // The output space
 84     hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
 85     if (hLab == NULL) return NULL;
 86 
 87     // Create a copy of parameters
 88     for (i=0; i < nProfiles; i++) {
 89 
 90         ProfileList[i]    = hProfiles[i];
 91         BPCList[i]        = BPC[i];
 92         AdaptationList[i] = AdaptationStates[i];
 93         IntentList[i]     = Intents[i];
 94     }
 95 
 96     // Place Lab identity at chain's end.
 97     ProfileList[nProfiles]    = hLab;
 98     BPCList[nProfiles]        = 0;
 99     AdaptationList[nProfiles] = 1.0;
100     IntentList[nProfiles]     = INTENT_RELATIVE_COLORIMETRIC;
101 
102     // Create the transform
103     xform = cmsCreateExtendedTransform(ContextID, nProfiles + 1, ProfileList,
104                                        BPCList,
105                                        IntentList,
106                                        AdaptationList,
107                                        NULL, 0,
108                                        InputFormat,
109                                        OutputFormat,
110                                        dwFlags);
111 
112     cmsCloseProfile(hLab);
113 
114     return xform;
115 }
116 
117 
118 // Compute K -> L* relationship. Flags may include black point compensation. In this case,
119 // the relationship is assumed from the profile with BPC to a black point zero.
120 static
121 cmsToneCurve* ComputeKToLstar(cmsContext            ContextID,
122                                cmsUInt32Number       nPoints,
123                                cmsUInt32Number       nProfiles,
124                                const cmsUInt32Number Intents[],
125                                const cmsHPROFILE     hProfiles[],
126                                const cmsBool         BPC[],
127                                const cmsFloat64Number AdaptationStates[],
128                                cmsUInt32Number dwFlags)
129 {
130     cmsToneCurve* out = NULL;
131     cmsUInt32Number i;
132     cmsHTRANSFORM xform;
133     cmsCIELab Lab;
134     cmsFloat32Number cmyk[4];
135     cmsFloat32Number* SampledPoints;
136 
137     xform = _cmsChain2Lab(ContextID, nProfiles, TYPE_CMYK_FLT, TYPE_Lab_DBL, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
138     if (xform == NULL) return NULL;
139 
140     SampledPoints = (cmsFloat32Number*) _cmsCalloc(ContextID, nPoints, sizeof(cmsFloat32Number));
141     if (SampledPoints  == NULL) goto Error;
142 
143     for (i=0; i < nPoints; i++) {
144 
145         cmyk[0] = 0;
146         cmyk[1] = 0;
147         cmyk[2] = 0;
148         cmyk[3] = (cmsFloat32Number) ((i * 100.0) / (nPoints-1));
149 
150         cmsDoTransform(xform, cmyk, &Lab, 1);
151         SampledPoints[i]= (cmsFloat32Number) (1.0 - Lab.L / 100.0); // Negate K for easier operation
152     }
153 
154     out = cmsBuildTabulatedToneCurveFloat(ContextID, nPoints, SampledPoints);
155 
156 Error:
157 
158     cmsDeleteTransform(xform);
159     if (SampledPoints) _cmsFree(ContextID, SampledPoints);
160 
161     return out;
162 }
163 
164 
165 // Compute Black tone curve on a CMYK -> CMYK transform. This is done by
166 // using the proof direction on both profiles to find K->L* relationship
167 // then joining both curves. dwFlags may include black point compensation.
168 cmsToneCurve* _cmsBuildKToneCurve(cmsContext        ContextID,
169                                    cmsUInt32Number   nPoints,
170                                    cmsUInt32Number   nProfiles,
171                                    const cmsUInt32Number Intents[],
172                                    const cmsHPROFILE hProfiles[],
173                                    const cmsBool     BPC[],
174                                    const cmsFloat64Number AdaptationStates[],
175                                    cmsUInt32Number   dwFlags)
176 {
177     cmsToneCurve *in, *out, *KTone;
178 
179     // Make sure CMYK -> CMYK
180     if (cmsGetColorSpace(hProfiles[0]) != cmsSigCmykData ||
181         cmsGetColorSpace(hProfiles[nProfiles-1])!= cmsSigCmykData) return NULL;
182 
183 
184     // Make sure last is an output profile
185     if (cmsGetDeviceClass(hProfiles[nProfiles - 1]) != cmsSigOutputClass) return NULL;
186 
187     // Create individual curves. BPC works also as each K to L* is
188     // computed as a BPC to zero black point in case of L*
189     in  = ComputeKToLstar(ContextID, nPoints, nProfiles - 1, Intents, hProfiles, BPC, AdaptationStates, dwFlags);
190     if (in == NULL) return NULL;
191 
192     out = ComputeKToLstar(ContextID, nPoints, 1,
193                             Intents + (nProfiles - 1),
194                             &hProfiles [nProfiles - 1],
195                             BPC + (nProfiles - 1),
196                             AdaptationStates + (nProfiles - 1),
197                             dwFlags);
198     if (out == NULL) {
199         cmsFreeToneCurve(in);
200         return NULL;
201     }
202 
203     // Build the relationship. This effectively limits the maximum accuracy to 16 bits, but
204     // since this is used on black-preserving LUTs, we are not losing  accuracy in any case
205     KTone = cmsJoinToneCurve(ContextID, in, out, nPoints);
206 
207     // Get rid of components
208     cmsFreeToneCurve(in); cmsFreeToneCurve(out);
209 
210     // Something went wrong...
211     if (KTone == NULL) return NULL;
212 
213     // Make sure it is monotonic
214     if (!cmsIsToneCurveMonotonic(KTone)) {
215         cmsFreeToneCurve(KTone);
216         return NULL;
217     }
218 
219     return KTone;
220 }
221 
222 
223 // Gamut LUT Creation -----------------------------------------------------------------------------------------
224 
225 // Used by gamut & softproofing
226 
227 typedef struct {
228 
229     cmsHTRANSFORM hInput;               // From whatever input color space. 16 bits to DBL
230     cmsHTRANSFORM hForward, hReverse;   // Transforms going from Lab to colorant and back
231     cmsFloat64Number Threshold;         // The threshold after which is considered out of gamut
232 
233     } GAMUTCHAIN;
234 
235 // This sampler does compute gamut boundaries by comparing original
236 // values with a transform going back and forth. Values above ERR_THRESHOLD
237 // of maximum are considered out of gamut.
238 
239 #define ERR_THRESHOLD      5
240 
241 
242 static
243 int GamutSampler(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void* Cargo)
244 {
245     GAMUTCHAIN*  t = (GAMUTCHAIN* ) Cargo;
246     cmsCIELab LabIn1, LabOut1;
247     cmsCIELab LabIn2, LabOut2;
248     cmsUInt16Number Proof[cmsMAXCHANNELS], Proof2[cmsMAXCHANNELS];
249     cmsFloat64Number dE1, dE2, ErrorRatio;
250 
251     // Assume in-gamut by default. NEVER READ, USED FOR DEBUG PURPOSES.
252     ErrorRatio = 1.0;
253 
254     // Convert input to Lab
255     cmsDoTransform(t -> hInput, In, &LabIn1, 1);
256 
257     // converts from PCS to colorant. This always
258     // does return in-gamut values,
259     cmsDoTransform(t -> hForward, &LabIn1, Proof, 1);
260 
261     // Now, do the inverse, from colorant to PCS.
262     cmsDoTransform(t -> hReverse, Proof, &LabOut1, 1);
263 
264     memmove(&LabIn2, &LabOut1, sizeof(cmsCIELab));
265 
266     // Try again, but this time taking Check as input
267     cmsDoTransform(t -> hForward, &LabOut1, Proof2, 1);
268     cmsDoTransform(t -> hReverse, Proof2, &LabOut2, 1);
269 
270     // Take difference of direct value
271     dE1 = cmsDeltaE(&LabIn1, &LabOut1);
272 
273     // Take difference of converted value
274     dE2 = cmsDeltaE(&LabIn2, &LabOut2);
275 
276 
277     // if dE1 is small and dE2 is small, value is likely to be in gamut
278     if (dE1 < t->Threshold && dE2 < t->Threshold)
279         Out[0] = 0;
280     else {
281 
282         // if dE1 is small and dE2 is big, undefined. Assume in gamut
283         if (dE1 < t->Threshold && dE2 > t->Threshold)
284             Out[0] = 0;
285         else
286             // dE1 is big and dE2 is small, clearly out of gamut
287             if (dE1 > t->Threshold && dE2 < t->Threshold)
288                 Out[0] = (cmsUInt16Number) _cmsQuickFloor((dE1 - t->Threshold) + .5);
289             else  {
290 
291                 // dE1 is big and dE2 is also big, could be due to perceptual mapping
292                 // so take error ratio
293                 if (dE2 == 0.0)
294                     ErrorRatio = dE1;
295                 else
296                     ErrorRatio = dE1 / dE2;
297 
298                 if (ErrorRatio > t->Threshold)
299                     Out[0] = (cmsUInt16Number)  _cmsQuickFloor((ErrorRatio - t->Threshold) + .5);
300                 else
301                     Out[0] = 0;
302             }
303     }
304 
305 
306     return TRUE;
307 }
308 
309 // Does compute a gamut LUT going back and forth across pcs -> relativ. colorimetric intent -> pcs
310 // the dE obtained is then annotated on the LUT. Values truly out of gamut are clipped to dE = 0xFFFE
311 // and values changed are supposed to be handled by any gamut remapping, so, are out of gamut as well.
312 //
313 // **WARNING: This algorithm does assume that gamut remapping algorithms does NOT move in-gamut colors,
314 // of course, many perceptual and saturation intents does not work in such way, but relativ. ones should.
315 
316 cmsPipeline* _cmsCreateGamutCheckPipeline(cmsContext ContextID,
317                                           cmsHPROFILE hProfiles[],
318                                           cmsBool  BPC[],
319                                           cmsUInt32Number Intents[],
320                                           cmsFloat64Number AdaptationStates[],
321                                           cmsUInt32Number nGamutPCSposition,
322                                           cmsHPROFILE hGamut)
323 {
324     cmsHPROFILE hLab;
325     cmsPipeline* Gamut;
326     cmsStage* CLUT;
327     cmsUInt32Number dwFormat;
328     GAMUTCHAIN Chain;
329     cmsUInt32Number nGridpoints;
330     cmsInt32Number nChannels;
331     cmsColorSpaceSignature ColorSpace;
332     cmsUInt32Number i;
333     cmsHPROFILE ProfileList[256];
334     cmsBool     BPCList[256];
335     cmsFloat64Number AdaptationList[256];
336     cmsUInt32Number IntentList[256];
337 
338     memset(&Chain, 0, sizeof(GAMUTCHAIN));
339 
340 
341     if (nGamutPCSposition <= 0 || nGamutPCSposition > 255) {
342         cmsSignalError(ContextID, cmsERROR_RANGE, "Wrong position of PCS. 1..255 expected, %d found.", nGamutPCSposition);
343         return NULL;
344     }
345 
346     hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
347     if (hLab == NULL) return NULL;
348 
349 
350     // The figure of merit. On matrix-shaper profiles, should be almost zero as
351     // the conversion is pretty exact. On LUT based profiles, different resolutions
352     // of input and output CLUT may result in differences.
353 
354     if (cmsIsMatrixShaper(hGamut)) {
355 
356         Chain.Threshold = 1.0;
357     }
358     else {
359         Chain.Threshold = ERR_THRESHOLD;
360     }
361 
362 
363     // Create a copy of parameters
364     for (i=0; i < nGamutPCSposition; i++) {
365         ProfileList[i]    = hProfiles[i];
366         BPCList[i]        = BPC[i];
367         AdaptationList[i] = AdaptationStates[i];
368         IntentList[i]     = Intents[i];
369     }
370 
371     // Fill Lab identity
372     ProfileList[nGamutPCSposition] = hLab;
373     BPCList[nGamutPCSposition] = 0;
374     AdaptationList[nGamutPCSposition] = 1.0;
375     IntentList[nGamutPCSposition] = INTENT_RELATIVE_COLORIMETRIC;
376 
377 
378     ColorSpace  = cmsGetColorSpace(hGamut);
379     nChannels   = cmsChannelsOfColorSpace(ColorSpace);
380     nGridpoints = _cmsReasonableGridpointsByColorspace(ColorSpace, cmsFLAGS_HIGHRESPRECALC);
381     dwFormat    = (CHANNELS_SH(nChannels)|BYTES_SH(2));
382 
383     // 16 bits to Lab double
384     Chain.hInput = cmsCreateExtendedTransform(ContextID,
385         nGamutPCSposition + 1,
386         ProfileList,
387         BPCList,
388         IntentList,
389         AdaptationList,
390         NULL, 0,
391         dwFormat, TYPE_Lab_DBL,
392         cmsFLAGS_NOCACHE);
393 
394 
395     // Does create the forward step. Lab double to device
396     dwFormat    = (CHANNELS_SH(nChannels)|BYTES_SH(2));
397     Chain.hForward = cmsCreateTransformTHR(ContextID,
398         hLab, TYPE_Lab_DBL,
399         hGamut, dwFormat,
400         INTENT_RELATIVE_COLORIMETRIC,
401         cmsFLAGS_NOCACHE);
402 
403     // Does create the backwards step
404     Chain.hReverse = cmsCreateTransformTHR(ContextID, hGamut, dwFormat,
405         hLab, TYPE_Lab_DBL,
406         INTENT_RELATIVE_COLORIMETRIC,
407         cmsFLAGS_NOCACHE);
408 
409 
410     // All ok?
411     if (Chain.hInput && Chain.hForward && Chain.hReverse) {
412 
413         // Go on, try to compute gamut LUT from PCS. This consist on a single channel containing
414         // dE when doing a transform back and forth on the colorimetric intent.
415 
416         Gamut = cmsPipelineAlloc(ContextID, 3, 1);
417         if (Gamut != NULL) {
418 
419             CLUT = cmsStageAllocCLut16bit(ContextID, nGridpoints, nChannels, 1, NULL);
420             if (!cmsPipelineInsertStage(Gamut, cmsAT_BEGIN, CLUT)) {
421                 cmsPipelineFree(Gamut);
422                 Gamut = NULL;
423             }
424             else {
425                 cmsStageSampleCLut16bit(CLUT, GamutSampler, (void*) &Chain, 0);
426             }
427         }
428     }
429     else
430         Gamut = NULL;   // Didn't work...
431 
432     // Free all needed stuff.
433     if (Chain.hInput)   cmsDeleteTransform(Chain.hInput);
434     if (Chain.hForward) cmsDeleteTransform(Chain.hForward);
435     if (Chain.hReverse) cmsDeleteTransform(Chain.hReverse);
436     if (hLab) cmsCloseProfile(hLab);
437 
438     // And return computed hull
439     return Gamut;
440 }
441 
442 // Total Area Coverage estimation ----------------------------------------------------------------
443 
444 typedef struct {
445     cmsUInt32Number  nOutputChans;
446     cmsHTRANSFORM    hRoundTrip;
447     cmsFloat32Number MaxTAC;
448     cmsFloat32Number MaxInput[cmsMAXCHANNELS];
449 
450 } cmsTACestimator;
451 
452 
453 // This callback just accounts the maximum ink dropped in the given node. It does not populate any
454 // memory, as the destination table is NULL. Its only purpose it to know the global maximum.
455 static
456 int EstimateTAC(CMSREGISTER const cmsUInt16Number In[], CMSREGISTER cmsUInt16Number Out[], CMSREGISTER void * Cargo)
457 {
458     cmsTACestimator* bp = (cmsTACestimator*) Cargo;
459     cmsFloat32Number RoundTrip[cmsMAXCHANNELS];
460     cmsUInt32Number i;
461     cmsFloat32Number Sum;
462 
463 
464     // Evaluate the xform
465     cmsDoTransform(bp->hRoundTrip, In, RoundTrip, 1);
466 
467     // All all amounts of ink
468     for (Sum=0, i=0; i < bp ->nOutputChans; i++)
469             Sum += RoundTrip[i];
470 
471     // If above maximum, keep track of input values
472     if (Sum > bp ->MaxTAC) {
473 
474             bp ->MaxTAC = Sum;
475 
476             for (i=0; i < bp ->nOutputChans; i++) {
477                 bp ->MaxInput[i] = In[i];
478             }
479     }
480 
481     return TRUE;
482 
483     cmsUNUSED_PARAMETER(Out);
484 }
485 
486 
487 // Detect Total area coverage of the profile
488 cmsFloat64Number CMSEXPORT cmsDetectTAC(cmsHPROFILE hProfile)
489 {
490     cmsTACestimator bp;
491     cmsUInt32Number dwFormatter;
492     cmsUInt32Number GridPoints[MAX_INPUT_DIMENSIONS];
493     cmsHPROFILE hLab;
494     cmsContext ContextID = cmsGetProfileContextID(hProfile);
495 
496     // TAC only works on output profiles
497     if (cmsGetDeviceClass(hProfile) != cmsSigOutputClass) {
498         return 0;
499     }
500 
501     // Create a fake formatter for result
502     dwFormatter = cmsFormatterForColorspaceOfProfile(hProfile, 4, TRUE);
503 
504     // Unsupported color space?
505     if (dwFormatter == 0) return 0;
506 
507     bp.nOutputChans = T_CHANNELS(dwFormatter);
508     bp.MaxTAC = 0;    // Initial TAC is 0
509 
510     //  for safety
511     if (bp.nOutputChans >= cmsMAXCHANNELS) return 0;
512 
513     hLab = cmsCreateLab4ProfileTHR(ContextID, NULL);
514     if (hLab == NULL) return 0;
515     // Setup a roundtrip on perceptual intent in output profile for TAC estimation
516     bp.hRoundTrip = cmsCreateTransformTHR(ContextID, hLab, TYPE_Lab_16,
517                                           hProfile, dwFormatter, INTENT_PERCEPTUAL, cmsFLAGS_NOOPTIMIZE|cmsFLAGS_NOCACHE);
518 
519     cmsCloseProfile(hLab);
520     if (bp.hRoundTrip == NULL) return 0;
521 
522     // For L* we only need black and white. For C* we need many points
523     GridPoints[0] = 6;
524     GridPoints[1] = 74;
525     GridPoints[2] = 74;
526 
527 
528     if (!cmsSliceSpace16(3, GridPoints, EstimateTAC, &bp)) {
529         bp.MaxTAC = 0;
530     }
531 
532     cmsDeleteTransform(bp.hRoundTrip);
533 
534     // Results in %
535     return bp.MaxTAC;
536 }
537 
538 
539 // Carefully,  clamp on CIELab space.
540 
541 cmsBool CMSEXPORT cmsDesaturateLab(cmsCIELab* Lab,
542                                    double amax, double amin,
543                                    double bmax, double bmin)
544 {
545 
546     // Whole Luma surface to zero
547 
548     if (Lab -> L < 0) {
549 
550         Lab-> L = Lab->a = Lab-> b = 0.0;
551         return FALSE;
552     }
553 
554     // Clamp white, DISCARD HIGHLIGHTS. This is done
555     // in such way because icc spec doesn't allow the
556     // use of L>100 as a highlight means.
557 
558     if (Lab->L > 100)
559         Lab -> L = 100;
560 
561     // Check out gamut prism, on a, b faces
562 
563     if (Lab -> a < amin || Lab->a > amax||
564         Lab -> b < bmin || Lab->b > bmax) {
565 
566             cmsCIELCh LCh;
567             double h, slope;
568 
569             // Falls outside a, b limits. Transports to LCh space,
570             // and then do the clipping
571 
572 
573             if (Lab -> a == 0.0) { // Is hue exactly 90?
574 
575                 // atan will not work, so clamp here
576                 Lab -> b = Lab->b < 0 ? bmin : bmax;
577                 return TRUE;
578             }
579 
580             cmsLab2LCh(&LCh, Lab);
581 
582             slope = Lab -> b / Lab -> a;
583             h = LCh.h;
584 
585             // There are 4 zones
586 
587             if ((h >= 0. && h < 45.) ||
588                 (h >= 315 && h <= 360.)) {
589 
590                     // clip by amax
591                     Lab -> a = amax;
592                     Lab -> b = amax * slope;
593             }
594             else
595                 if (h >= 45. && h < 135.)
596                 {
597                     // clip by bmax
598                     Lab -> b = bmax;
599                     Lab -> a = bmax / slope;
600                 }
601                 else
602                     if (h >= 135. && h < 225.) {
603                         // clip by amin
604                         Lab -> a = amin;
605                         Lab -> b = amin * slope;
606 
607                     }
608                     else
609                         if (h >= 225. && h < 315.) {
610                             // clip by bmin
611                             Lab -> b = bmin;
612                             Lab -> a = bmin / slope;
613                         }
614                         else  {
615                             cmsSignalError(0, cmsERROR_RANGE, "Invalid angle");
616                             return FALSE;
617                         }
618 
619     }
620 
621     return TRUE;
622 }
623 
624 // Detect whatever a given ICC profile works in linear (gamma 1.0) space
625 // Actually, doing that "well" is quite hard, since every component may behave completely different.
626 // Since the true point of this function is to detect suitable optimizations, I am imposing some requirements
627 // that simplifies things: only RGB, and only profiles that can got in both directions.
628 // The algorithm obtains Y from a synthetical gray R=G=B. Then least squares fitting is used to estimate gamma.
629 // For gamma close to 1.0, RGB is linear. On profiles not supported, -1 is returned.
630 
631 cmsFloat64Number CMSEXPORT cmsDetectRGBProfileGamma(cmsHPROFILE hProfile, cmsFloat64Number threshold)
632 {
633     cmsContext ContextID;
634     cmsHPROFILE hXYZ;
635     cmsHTRANSFORM xform;
636     cmsToneCurve* Y_curve;
637     cmsUInt16Number rgb[256][3];
638     cmsCIEXYZ XYZ[256];
639     cmsFloat32Number Y_normalized[256];
640     cmsFloat64Number gamma;
641     cmsProfileClassSignature cl;
642     int i;
643 
644     if (cmsGetColorSpace(hProfile) != cmsSigRgbData)
645         return -1;
646 
647     cl = cmsGetDeviceClass(hProfile);
648     if (cl != cmsSigInputClass && cl != cmsSigDisplayClass &&
649         cl != cmsSigOutputClass && cl != cmsSigColorSpaceClass)
650         return -1;
651 
652     ContextID = cmsGetProfileContextID(hProfile);
653     hXYZ = cmsCreateXYZProfileTHR(ContextID);
654     if (hXYZ == NULL)
655         return -1;
656     xform = cmsCreateTransformTHR(ContextID, hProfile, TYPE_RGB_16, hXYZ, TYPE_XYZ_DBL,
657                                     INTENT_RELATIVE_COLORIMETRIC, cmsFLAGS_NOOPTIMIZE);
658 
659     if (xform == NULL) { // If not RGB or forward direction is not supported, regret with the previous error
660 
661         cmsCloseProfile(hXYZ);
662         return -1;
663     }
664 
665     for (i = 0; i < 256; i++) {
666         rgb[i][0] = rgb[i][1] = rgb[i][2] = FROM_8_TO_16(i);
667     }
668 
669     cmsDoTransform(xform, rgb, XYZ, 256);
670 
671     cmsDeleteTransform(xform);
672     cmsCloseProfile(hXYZ);
673 
674     for (i = 0; i < 256; i++) {
675         Y_normalized[i] = (cmsFloat32Number) XYZ[i].Y;
676     }
677 
678     Y_curve = cmsBuildTabulatedToneCurveFloat(ContextID, 256, Y_normalized);
679     if (Y_curve == NULL)
680         return -1;
681 
682     gamma = cmsEstimateGamma(Y_curve, threshold);
683 
684     cmsFreeToneCurve(Y_curve);
685 
686     return gamma;
687 }
688