Home | History | Annotate | Download | only in src
      1 //---------------------------------------------------------------------------------
      2 //
      3 //  Little Color Management System
      4 //  Copyright (c) 1998-2016 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 // Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
     30 
     31 // LUT tags
     32 static const cmsTagSignature Device2PCS16[]   =  {cmsSigAToB0Tag,     // Perceptual
     33                                                   cmsSigAToB1Tag,     // Relative colorimetric
     34                                                   cmsSigAToB2Tag,     // Saturation
     35                                                   cmsSigAToB1Tag };   // Absolute colorimetric
     36 
     37 static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag,     // Perceptual
     38                                                   cmsSigDToB1Tag,     // Relative colorimetric
     39                                                   cmsSigDToB2Tag,     // Saturation
     40                                                   cmsSigDToB3Tag };   // Absolute colorimetric
     41 
     42 static const cmsTagSignature PCS2Device16[]    = {cmsSigBToA0Tag,     // Perceptual
     43                                                   cmsSigBToA1Tag,     // Relative colorimetric
     44                                                   cmsSigBToA2Tag,     // Saturation
     45                                                   cmsSigBToA1Tag };   // Absolute colorimetric
     46 
     47 static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag,     // Perceptual
     48                                                   cmsSigBToD1Tag,     // Relative colorimetric
     49                                                   cmsSigBToD2Tag,     // Saturation
     50                                                   cmsSigBToD3Tag };   // Absolute colorimetric
     51 
     52 
     53 // Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa
     54 #define InpAdj   (1.0/MAX_ENCODEABLE_XYZ)     // (65536.0/(65535.0*2.0))
     55 #define OutpAdj  (MAX_ENCODEABLE_XYZ)         // ((2.0*65535.0)/65536.0)
     56 
     57 // Several resources for gray conversions.
     58 static const cmsFloat64Number GrayInputMatrix[] = { (InpAdj*cmsD50X),  (InpAdj*cmsD50Y),  (InpAdj*cmsD50Z) };
     59 static const cmsFloat64Number OneToThreeInputMatrix[] = { 1, 1, 1 };
     60 static const cmsFloat64Number PickYMatrix[] = { 0, (OutpAdj*cmsD50Y), 0 };
     61 static const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };
     62 
     63 // Get a media white point fixing some issues found in certain old profiles
     64 cmsBool  _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
     65 {
     66     cmsCIEXYZ* Tag;
     67 
     68     _cmsAssert(Dest != NULL);
     69 
     70     Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
     71 
     72     // If no wp, take D50
     73     if (Tag == NULL) {
     74         *Dest = *cmsD50_XYZ();
     75         return TRUE;
     76     }
     77 
     78     // V2 display profiles should give D50
     79     if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
     80 
     81         if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
     82             *Dest = *cmsD50_XYZ();
     83             return TRUE;
     84         }
     85     }
     86 
     87     // All seems ok
     88     *Dest = *Tag;
     89     return TRUE;
     90 }
     91 
     92 
     93 // Chromatic adaptation matrix. Fix some issues as well
     94 cmsBool  _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
     95 {
     96     cmsMAT3* Tag;
     97 
     98     _cmsAssert(Dest != NULL);
     99 
    100     Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
    101 
    102     if (Tag != NULL) {
    103         *Dest = *Tag;
    104         return TRUE;
    105     }
    106 
    107     // No CHAD available, default it to identity
    108     _cmsMAT3identity(Dest);
    109 
    110     // V2 display profiles should give D50
    111     if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
    112 
    113         if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
    114 
    115             cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
    116 
    117             if (White == NULL) {
    118 
    119                 _cmsMAT3identity(Dest);
    120                 return TRUE;
    121             }
    122 
    123             return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());
    124         }
    125     }
    126 
    127     return TRUE;
    128 }
    129 
    130 
    131 // Auxiliary, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
    132 static
    133 cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
    134 {
    135     cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
    136 
    137     _cmsAssert(r != NULL);
    138 
    139     PtrRed   = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);
    140     PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);
    141     PtrBlue  = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);
    142 
    143     if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
    144         return FALSE;
    145 
    146     _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X,  PtrBlue -> X);
    147     _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y,  PtrBlue -> Y);
    148     _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z,  PtrBlue -> Z);
    149 
    150     return TRUE;
    151 }
    152 
    153 
    154 // Gray input pipeline
    155 static
    156 cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)
    157 {
    158     cmsToneCurve *GrayTRC;
    159     cmsPipeline* Lut;
    160     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    161 
    162     GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
    163     if (GrayTRC == NULL) return NULL;
    164 
    165     Lut = cmsPipelineAlloc(ContextID, 1, 3);
    166     if (Lut == NULL)
    167         goto Error;
    168 
    169     if (cmsGetPCS(hProfile) == cmsSigLabData) {
    170 
    171         // In this case we implement the profile as an  identity matrix plus 3 tone curves
    172         cmsUInt16Number Zero[2] = { 0x8080, 0x8080 };
    173         cmsToneCurve* EmptyTab;
    174         cmsToneCurve* LabCurves[3];
    175 
    176         EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
    177 
    178         if (EmptyTab == NULL)
    179             goto Error;
    180 
    181         LabCurves[0] = GrayTRC;
    182         LabCurves[1] = EmptyTab;
    183         LabCurves[2] = EmptyTab;
    184 
    185         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3,  1, OneToThreeInputMatrix, NULL)) ||
    186             !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves))) {
    187                 cmsFreeToneCurve(EmptyTab);
    188                 goto Error;
    189         }
    190 
    191         cmsFreeToneCurve(EmptyTab);
    192 
    193     }
    194     else  {
    195 
    196         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC)) ||
    197             !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3,  1, GrayInputMatrix, NULL)))
    198             goto Error;
    199     }
    200 
    201     return Lut;
    202 
    203 Error:
    204     // memory pointed by GrayTRC is not a new malloc memory, so don't free it here,
    205     // memory pointed by GrayTRC will be freed when hProfile is closed.
    206     // test file :0047776_Pocket Medicine_ The Massachusetts General Hospital Handbook of Internal Medicine-2.pdf
    207     // Xiaochuan Liu, 20140421
    208     //cmsFreeToneCurve(GrayTRC);
    209     cmsPipelineFree(Lut);
    210     return NULL;
    211 }
    212 
    213 // RGB Matrix shaper
    214 static
    215 cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
    216 {
    217     cmsPipeline* Lut;
    218     cmsMAT3 Mat;
    219     cmsToneCurve *Shapes[3];
    220     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    221     int i, j;
    222 
    223     if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
    224 
    225     // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
    226     // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
    227     // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
    228 
    229     for (i=0; i < 3; i++)
    230         for (j=0; j < 3; j++)
    231             Mat.v[i].n[j] *= InpAdj;
    232 
    233 
    234     Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
    235     Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
    236     Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
    237 
    238     if (!Shapes[0] || !Shapes[1] || !Shapes[2])
    239         return NULL;
    240 
    241     Lut = cmsPipelineAlloc(ContextID, 3, 3);
    242     if (Lut != NULL) {
    243 
    244         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||
    245             !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))
    246             goto Error;
    247 
    248         // Note that it is certainly possible a single profile would have a LUT based
    249         // tag for output working in lab and a matrix-shaper for the fallback cases.
    250         // This is not allowed by the spec, but this code is tolerant to those cases
    251         if (cmsGetPCS(hProfile) == cmsSigLabData) {
    252 
    253             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))
    254                 goto Error;
    255         }
    256 
    257     }
    258 
    259     return Lut;
    260 
    261 Error:
    262     cmsPipelineFree(Lut);
    263     return NULL;
    264 }
    265 
    266 
    267 
    268 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
    269 static
    270 cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
    271 {
    272     cmsContext ContextID       = cmsGetProfileContextID(hProfile);
    273     cmsPipeline* Lut           = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    274     cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
    275     cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
    276 
    277     if (Lut == NULL) return NULL;
    278 
    279     // input and output of transform are in lcms 0..1 encoding.  If XYZ or Lab spaces are used,
    280     //  these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
    281     if ( spc == cmsSigLabData)
    282     {
    283         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
    284             goto Error;
    285     }
    286     else if (spc == cmsSigXYZData)
    287     {
    288         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
    289             goto Error;
    290     }
    291 
    292     if ( PCS == cmsSigLabData)
    293     {
    294         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
    295             goto Error;
    296     }
    297     else if( PCS == cmsSigXYZData)
    298     {
    299         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
    300             goto Error;
    301     }
    302 
    303     return Lut;
    304 
    305 Error:
    306     cmsPipelineFree(Lut);
    307     return NULL;
    308 }
    309 
    310 
    311 // Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
    312 // is adjusted here in order to create a LUT that takes care of all those details.
    313 // We add intent = -1 as a way to read matrix shaper always, no matter of other LUT
    314 cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
    315 {
    316     cmsTagTypeSignature OriginalType;
    317     cmsTagSignature tag16;
    318     cmsTagSignature tagFloat;
    319     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    320 
    321     // On named color, take the appropriate tag
    322     if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
    323 
    324         cmsPipeline* Lut;
    325         cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
    326 
    327         if (nc == NULL) return NULL;
    328 
    329         Lut = cmsPipelineAlloc(ContextID, 0, 0);
    330         if (Lut == NULL) {
    331             cmsFreeNamedColorList(nc);
    332             return NULL;
    333         }
    334 
    335         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||
    336             !cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {
    337             cmsPipelineFree(Lut);
    338             return NULL;
    339         }
    340         return Lut;
    341     }
    342 
    343     // This is an attempt to reuse this function to retrieve the matrix-shaper as pipeline no
    344     // matter other LUT are present and have precedence. Intent = -1 means just this.
    345     if (Intent >= INTENT_PERCEPTUAL && Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
    346 
    347         tag16 = Device2PCS16[Intent];
    348         tagFloat = Device2PCSFloat[Intent];
    349 
    350         if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    351 
    352             // Floating point LUT are always V4, but the encoding range is no
    353             // longer 0..1.0, so we need to add an stage depending on the color space
    354             return _cmsReadFloatInputTag(hProfile, tagFloat);
    355         }
    356 
    357         // Revert to perceptual if no tag is found
    358         if (!cmsIsTag(hProfile, tag16)) {
    359             tag16 = Device2PCS16[0];
    360         }
    361 
    362         if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
    363 
    364             // Check profile version and LUT type. Do the necessary adjustments if needed
    365 
    366             // First read the tag
    367             cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
    368             if (Lut == NULL) return NULL;
    369 
    370             // After reading it, we have now info about the original type
    371             OriginalType =  _cmsGetTagTrueType(hProfile, tag16);
    372 
    373             // The profile owns the Lut, so we need to copy it
    374             Lut = cmsPipelineDup(Lut);
    375 
    376             // We need to adjust data only for Lab16 on output
    377             if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
    378                 return Lut;
    379 
    380             // If the input is Lab, add also a conversion at the begin
    381             if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
    382                 !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    383                 goto Error;
    384 
    385             // Add a matrix for conversion V2 to V4 Lab PCS
    386             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    387                 goto Error;
    388 
    389             return Lut;
    390 Error:
    391             cmsPipelineFree(Lut);
    392             return NULL;
    393         }
    394     }
    395 
    396     // Lut was not found, try to create a matrix-shaper
    397 
    398     // Check if this is a grayscale profile.
    399     if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
    400 
    401         // if so, build appropriate conversion tables.
    402         // The tables are the PCS iluminant, scaled across GrayTRC
    403         return BuildGrayInputMatrixPipeline(hProfile);
    404     }
    405 
    406     // Not gray, create a normal matrix-shaper
    407     return BuildRGBInputMatrixShaper(hProfile);
    408 }
    409 
    410 // ---------------------------------------------------------------------------------------------------------------
    411 
    412 // Gray output pipeline.
    413 // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
    414 // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
    415 // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
    416 
    417 static
    418 cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
    419 {
    420     cmsToneCurve *GrayTRC, *RevGrayTRC;
    421     cmsPipeline* Lut;
    422     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    423 
    424     GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
    425     if (GrayTRC == NULL) return NULL;
    426 
    427     RevGrayTRC = cmsReverseToneCurve(GrayTRC);
    428     if (RevGrayTRC == NULL) return NULL;
    429 
    430     Lut = cmsPipelineAlloc(ContextID, 3, 1);
    431     if (Lut == NULL) {
    432         cmsFreeToneCurve(RevGrayTRC);
    433         return NULL;
    434     }
    435 
    436     if (cmsGetPCS(hProfile) == cmsSigLabData) {
    437 
    438         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1,  3, PickLstarMatrix, NULL)))
    439             goto Error;
    440     }
    441     else  {
    442         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1,  3, PickYMatrix, NULL)))
    443             goto Error;
    444     }
    445 
    446     if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
    447         goto Error;
    448 
    449     cmsFreeToneCurve(RevGrayTRC);
    450     return Lut;
    451 
    452 Error:
    453     cmsFreeToneCurve(RevGrayTRC);
    454     cmsPipelineFree(Lut);
    455     return NULL;
    456 }
    457 
    458 
    459 static
    460 cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
    461 {
    462     cmsPipeline* Lut;
    463     cmsToneCurve *Shapes[3], *InvShapes[3];
    464     cmsMAT3 Mat, Inv;
    465     int i, j;
    466     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    467 
    468     if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
    469         return NULL;
    470 
    471     if (!_cmsMAT3inverse(&Mat, &Inv))
    472         return NULL;
    473 
    474     // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
    475     // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
    476     // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
    477 
    478     for (i=0; i < 3; i++)
    479         for (j=0; j < 3; j++)
    480             Inv.v[i].n[j] *= OutpAdj;
    481 
    482     Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
    483     Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
    484     Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
    485 
    486     if (!Shapes[0] || !Shapes[1] || !Shapes[2])
    487         return NULL;
    488 
    489     InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
    490     InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
    491     InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
    492 
    493     if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
    494         return NULL;
    495     }
    496 
    497     Lut = cmsPipelineAlloc(ContextID, 3, 3);
    498     if (Lut != NULL) {
    499 
    500         // Note that it is certainly possible a single profile would have a LUT based
    501         // tag for output working in lab and a matrix-shaper for the fallback cases.
    502         // This is not allowed by the spec, but this code is tolerant to those cases
    503         if (cmsGetPCS(hProfile) == cmsSigLabData) {
    504 
    505             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
    506                 goto Error;
    507         }
    508 
    509         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
    510             !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
    511             goto Error;
    512     }
    513 
    514     cmsFreeToneCurveTriple(InvShapes);
    515     return Lut;
    516 Error:
    517     cmsFreeToneCurveTriple(InvShapes);
    518     cmsPipelineFree(Lut);
    519     return NULL;
    520 }
    521 
    522 
    523 // Change CLUT interpolation to trilinear
    524 static
    525 void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
    526 {
    527     cmsStage* Stage;
    528 
    529     for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
    530         Stage != NULL;
    531         Stage = cmsStageNext(Stage)) {
    532 
    533             if (cmsStageType(Stage) == cmsSigCLutElemType) {
    534 
    535                 _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
    536 
    537                 CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
    538                 _cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);
    539             }
    540     }
    541 }
    542 
    543 
    544 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
    545 static
    546 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
    547 {
    548     cmsContext ContextID       = cmsGetProfileContextID(hProfile);
    549     cmsPipeline* Lut           = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    550     cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
    551     cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
    552 
    553     if (Lut == NULL) return NULL;
    554 
    555     // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
    556     // and since the formatter has already accommodated to 0..1.0, we should undo this change
    557     if ( PCS == cmsSigLabData)
    558     {
    559         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
    560             goto Error;
    561     }
    562     else
    563         if (PCS == cmsSigXYZData)
    564         {
    565             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
    566                 goto Error;
    567         }
    568 
    569     // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
    570     if ( dataSpace == cmsSigLabData)
    571     {
    572         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
    573             goto Error;
    574     }
    575     else if (dataSpace == cmsSigXYZData)
    576     {
    577         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
    578             goto Error;
    579     }
    580 
    581     return Lut;
    582 
    583 Error:
    584     cmsPipelineFree(Lut);
    585     return NULL;
    586 }
    587 
    588 // Create an output MPE LUT from agiven profile. Version mismatches are handled here
    589 cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
    590 {
    591     cmsTagTypeSignature OriginalType;
    592     cmsTagSignature tag16;
    593     cmsTagSignature tagFloat;
    594     cmsContext ContextID  = cmsGetProfileContextID(hProfile);
    595 
    596 
    597     if (Intent >= INTENT_PERCEPTUAL && Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {
    598 
    599         tag16 = PCS2Device16[Intent];
    600         tagFloat = PCS2DeviceFloat[Intent];
    601 
    602         if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    603 
    604             // Floating point LUT are always V4
    605             return _cmsReadFloatOutputTag(hProfile, tagFloat);
    606         }
    607 
    608         // Revert to perceptual if no tag is found
    609         if (!cmsIsTag(hProfile, tag16)) {
    610             tag16 = PCS2Device16[0];
    611         }
    612 
    613         if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
    614 
    615             // Check profile version and LUT type. Do the necessary adjustments if needed
    616 
    617             // First read the tag
    618             cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
    619             if (Lut == NULL) return NULL;
    620 
    621             // After reading it, we have info about the original type
    622             OriginalType =  _cmsGetTagTrueType(hProfile, tag16);
    623 
    624             // The profile owns the Lut, so we need to copy it
    625             Lut = cmsPipelineDup(Lut);
    626             if (Lut == NULL) return NULL;
    627 
    628             // Now it is time for a controversial stuff. I found that for 3D LUTS using
    629             // Lab used as indexer space,  trilinear interpolation should be used
    630             if (cmsGetPCS(hProfile) == cmsSigLabData)
    631                 ChangeInterpolationToTrilinear(Lut);
    632 
    633             // We need to adjust data only for Lab and Lut16 type
    634             if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
    635                 return Lut;
    636 
    637             // Add a matrix for conversion V4 to V2 Lab PCS
    638             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    639                 goto Error;
    640 
    641             // If the output is Lab, add also a conversion at the end
    642             if (cmsGetColorSpace(hProfile) == cmsSigLabData)
    643                 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    644                     goto Error;
    645 
    646             return Lut;
    647 Error:
    648             cmsPipelineFree(Lut);
    649             return NULL;
    650         }
    651     }
    652 
    653     // Lut not found, try to create a matrix-shaper
    654 
    655     // Check if this is a grayscale profile.
    656     if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
    657 
    658         // if so, build appropriate conversion tables.
    659         // The tables are the PCS iluminant, scaled across GrayTRC
    660         return BuildGrayOutputPipeline(hProfile);
    661     }
    662 
    663     // Not gray, create a normal matrix-shaper, which only operates in XYZ space
    664     return BuildRGBOutputMatrixShaper(hProfile);
    665 }
    666 
    667 // ---------------------------------------------------------------------------------------------------------------
    668 
    669 // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
    670 static
    671 cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
    672 {
    673     cmsContext ContextID       = cmsGetProfileContextID(hProfile);
    674     cmsPipeline* Lut           = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    675     cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
    676     cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
    677 
    678     if (Lut == NULL) return NULL;
    679 
    680     if (spc == cmsSigLabData)
    681     {
    682         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
    683             goto Error;
    684     }
    685     else
    686         if (spc == cmsSigXYZData)
    687         {
    688             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
    689                 goto Error;
    690         }
    691 
    692         if (PCS == cmsSigLabData)
    693         {
    694             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
    695                 goto Error;
    696         }
    697         else
    698             if (PCS == cmsSigXYZData)
    699             {
    700                 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
    701                     goto Error;
    702             }
    703 
    704     return Lut;
    705 Error:
    706     cmsPipelineFree(Lut);
    707     return NULL;
    708 }
    709 
    710 // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
    711 // tag name here may default to AToB0
    712 cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
    713 {
    714     cmsPipeline* Lut;
    715     cmsTagTypeSignature OriginalType;
    716     cmsTagSignature tag16;
    717     cmsTagSignature tagFloat;
    718     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    719 
    720 
    721     if (Intent < INTENT_PERCEPTUAL || Intent > INTENT_ABSOLUTE_COLORIMETRIC)
    722         return NULL;
    723 
    724     tag16 = Device2PCS16[Intent];
    725     tagFloat = Device2PCSFloat[Intent];
    726 
    727     // On named color, take the appropriate tag
    728     if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
    729 
    730         cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*)cmsReadTag(hProfile, cmsSigNamedColor2Tag);
    731 
    732         if (nc == NULL) return NULL;
    733 
    734         Lut = cmsPipelineAlloc(ContextID, 0, 0);
    735         if (Lut == NULL)
    736             goto Error;
    737 
    738         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
    739             goto Error;
    740 
    741         if (cmsGetColorSpace(hProfile) == cmsSigLabData)
    742             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    743                 goto Error;
    744 
    745         return Lut;
    746     Error:
    747         cmsPipelineFree(Lut);
    748         cmsFreeNamedColorList(nc);
    749         return NULL;
    750     }
    751 
    752 
    753     if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    754 
    755         // Floating point LUT are always V
    756         return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
    757     }
    758 
    759     tagFloat = Device2PCSFloat[0];
    760     if (cmsIsTag(hProfile, tagFloat)) {
    761 
    762         return cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));
    763     }
    764 
    765     if (!cmsIsTag(hProfile, tag16)) {  // Is there any LUT-Based table?
    766 
    767         tag16 = Device2PCS16[0];
    768         if (!cmsIsTag(hProfile, tag16)) return NULL;
    769     }
    770 
    771     // Check profile version and LUT type. Do the necessary adjustments if needed
    772 
    773     // Read the tag
    774     Lut = (cmsPipeline*)cmsReadTag(hProfile, tag16);
    775     if (Lut == NULL) return NULL;
    776 
    777     // The profile owns the Lut, so we need to copy it
    778     Lut = cmsPipelineDup(Lut);
    779     if (Lut == NULL) return NULL;
    780 
    781     // Now it is time for a controversial stuff. I found that for 3D LUTS using
    782     // Lab used as indexer space,  trilinear interpolation should be used
    783     if (cmsGetPCS(hProfile) == cmsSigLabData)
    784         ChangeInterpolationToTrilinear(Lut);
    785 
    786     // After reading it, we have info about the original type
    787     OriginalType = _cmsGetTagTrueType(hProfile, tag16);
    788 
    789     // We need to adjust data for Lab16 on output
    790     if (OriginalType != cmsSigLut16Type) return Lut;
    791 
    792     // Here it is possible to get Lab on both sides
    793 
    794     if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
    795         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    796             goto Error2;
    797     }
    798 
    799     if (cmsGetPCS(hProfile) == cmsSigLabData) {
    800         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    801             goto Error2;
    802     }
    803 
    804     return Lut;
    805 
    806 Error2:
    807     cmsPipelineFree(Lut);
    808     return NULL;
    809 }
    810 
    811 // ---------------------------------------------------------------------------------------------------------------
    812 
    813 // Returns TRUE if the profile is implemented as matrix-shaper
    814 cmsBool  CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
    815 {
    816     switch (cmsGetColorSpace(hProfile)) {
    817 
    818     case cmsSigGrayData:
    819 
    820         return cmsIsTag(hProfile, cmsSigGrayTRCTag);
    821 
    822     case cmsSigRgbData:
    823 
    824         return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
    825                 cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
    826                 cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
    827                 cmsIsTag(hProfile, cmsSigRedTRCTag) &&
    828                 cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
    829                 cmsIsTag(hProfile, cmsSigBlueTRCTag));
    830 
    831     default:
    832 
    833         return FALSE;
    834     }
    835 }
    836 
    837 // Returns TRUE if the intent is implemented as CLUT
    838 cmsBool  CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
    839 {
    840     const cmsTagSignature* TagTable;
    841 
    842     // For devicelinks, the supported intent is that one stated in the header
    843     if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
    844             return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
    845     }
    846 
    847     switch (UsedDirection) {
    848 
    849        case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
    850        case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
    851 
    852        // For proofing, we need rel. colorimetric in output. Let's do some recursion
    853        case LCMS_USED_AS_PROOF:
    854            return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
    855                   cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
    856 
    857        default:
    858            cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
    859            return FALSE;
    860     }
    861 
    862     return cmsIsTag(hProfile, TagTable[Intent]);
    863 
    864 }
    865 
    866 
    867 // Return info about supported intents
    868 cmsBool  CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
    869                                         cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
    870 {
    871 
    872     if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
    873 
    874     // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
    875     // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
    876     // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
    877     // the accuracy would be less than optimal in rel.col and v2 case.
    878 
    879     return cmsIsMatrixShaper(hProfile);
    880 }
    881 
    882 
    883 // ---------------------------------------------------------------------------------------------------------------
    884 
    885 // Read both, profile sequence description and profile sequence id if present. Then combine both to
    886 // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
    887 cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
    888 {
    889     cmsSEQ* ProfileSeq;
    890     cmsSEQ* ProfileId;
    891     cmsSEQ* NewSeq;
    892     cmsUInt32Number i;
    893 
    894     // Take profile sequence description first
    895     ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
    896 
    897     // Take profile sequence ID
    898     ProfileId  = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
    899 
    900     if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
    901 
    902     if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
    903     if (ProfileId  == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
    904 
    905     // We have to mix both together. For that they must agree
    906     if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
    907 
    908     NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
    909 
    910     // Ok, proceed to the mixing
    911     if (NewSeq != NULL) {
    912         for (i=0; i < ProfileSeq ->n; i++) {
    913 
    914             memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
    915             NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
    916         }
    917     }
    918     return NewSeq;
    919 }
    920 
    921 // Dump the contents of profile sequence in both tags (if v4 available)
    922 cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
    923 {
    924     if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
    925 
    926     if (cmsGetEncodedICCversion(hProfile) >= 0x4000000) {
    927 
    928             if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
    929     }
    930 
    931     return TRUE;
    932 }
    933 
    934 
    935 // Auxiliary, read and duplicate a MLU if found.
    936 static
    937 cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
    938 {
    939     cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
    940     if (mlu == NULL) return NULL;
    941 
    942     return cmsMLUdup(mlu);
    943 }
    944 
    945 // Create a sequence description out of an array of profiles
    946 cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
    947 {
    948     cmsUInt32Number i;
    949     cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
    950 
    951     if (seq == NULL) return NULL;
    952 
    953     for (i=0; i < nProfiles; i++) {
    954 
    955         cmsPSEQDESC* ps = &seq ->seq[i];
    956         cmsHPROFILE h = hProfiles[i];
    957         cmsTechnologySignature* techpt;
    958 
    959         cmsGetHeaderAttributes(h, &ps ->attributes);
    960         cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
    961         ps ->deviceMfg   = cmsGetHeaderManufacturer(h);
    962         ps ->deviceModel = cmsGetHeaderModel(h);
    963 
    964         techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
    965         if (techpt == NULL)
    966             ps ->technology   =  (cmsTechnologySignature) 0;
    967         else
    968             ps ->technology   = *techpt;
    969 
    970         ps ->Manufacturer = GetMLUFromProfile(h,  cmsSigDeviceMfgDescTag);
    971         ps ->Model        = GetMLUFromProfile(h,  cmsSigDeviceModelDescTag);
    972         ps ->Description  = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
    973 
    974     }
    975 
    976     return seq;
    977 }
    978 
    979 // -------------------------------------------------------------------------------------------------------------------
    980 
    981 
    982 static
    983 const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
    984 {
    985     cmsTagSignature sig;
    986 
    987     switch (Info) {
    988 
    989     case cmsInfoDescription:
    990         sig = cmsSigProfileDescriptionTag;
    991         break;
    992 
    993     case cmsInfoManufacturer:
    994         sig = cmsSigDeviceMfgDescTag;
    995         break;
    996 
    997     case cmsInfoModel:
    998         sig = cmsSigDeviceModelDescTag;
    999          break;
   1000 
   1001     case cmsInfoCopyright:
   1002         sig = cmsSigCopyrightTag;
   1003         break;
   1004 
   1005     default: return NULL;
   1006     }
   1007 
   1008 
   1009     return (cmsMLU*) cmsReadTag(hProfile, sig);
   1010 }
   1011 
   1012 
   1013 
   1014 cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
   1015                                             const char LanguageCode[3], const char CountryCode[3],
   1016                                             wchar_t* Buffer, cmsUInt32Number BufferSize)
   1017 {
   1018     const cmsMLU* mlu = GetInfo(hProfile, Info);
   1019     if (mlu == NULL) return 0;
   1020 
   1021     return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
   1022 }
   1023 
   1024 
   1025 cmsUInt32Number  CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
   1026                                                           const char LanguageCode[3], const char CountryCode[3],
   1027                                                           char* Buffer, cmsUInt32Number BufferSize)
   1028 {
   1029     const cmsMLU* mlu = GetInfo(hProfile, Info);
   1030     if (mlu == NULL) return 0;
   1031 
   1032     return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
   1033 }
   1034