Home | History | Annotate | Download | only in src
      1 //---------------------------------------------------------------------------------
      2 //
      3 //  Little Color Management System
      4 //  Copyright (c) 1998-2012 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 // Auxiliar, 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    = Device2PCS16[Intent];
    318     cmsTagSignature tagFloat = Device2PCSFloat[Intent];
    319     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    320 
    321     // On named color, take the appropiate 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 funtion 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 != -1) {
    346 
    347         if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    348 
    349             // Floating point LUT are always V4, but the encoding range is no
    350             // longer 0..1.0, so we need to add an stage depending on the color space
    351             return _cmsReadFloatInputTag(hProfile, tagFloat);
    352         }
    353 
    354         // Revert to perceptual if no tag is found
    355         if (!cmsIsTag(hProfile, tag16)) {
    356             tag16 = Device2PCS16[0];
    357         }
    358 
    359         if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
    360 
    361             // Check profile version and LUT type. Do the necessary adjustments if needed
    362 
    363             // First read the tag
    364             cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
    365             if (Lut == NULL) return NULL;
    366 
    367             // After reading it, we have now info about the original type
    368             OriginalType =  _cmsGetTagTrueType(hProfile, tag16);
    369 
    370             // The profile owns the Lut, so we need to copy it
    371             Lut = cmsPipelineDup(Lut);
    372 
    373             // We need to adjust data only for Lab16 on output
    374             if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
    375                 return Lut;
    376 
    377             // If the input is Lab, add also a conversion at the begin
    378             if (cmsGetColorSpace(hProfile) == cmsSigLabData &&
    379                 !cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    380                 goto Error;
    381 
    382             // Add a matrix for conversion V2 to V4 Lab PCS
    383             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    384                 goto Error;
    385 
    386             return Lut;
    387 Error:
    388             cmsPipelineFree(Lut);
    389             return NULL;
    390         }
    391     }
    392 
    393     // Lut was not found, try to create a matrix-shaper
    394 
    395     // Check if this is a grayscale profile.
    396     if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
    397 
    398         // if so, build appropiate conversion tables.
    399         // The tables are the PCS iluminant, scaled across GrayTRC
    400         return BuildGrayInputMatrixPipeline(hProfile);
    401     }
    402 
    403     // Not gray, create a normal matrix-shaper
    404     return BuildRGBInputMatrixShaper(hProfile);
    405 }
    406 
    407 // ---------------------------------------------------------------------------------------------------------------
    408 
    409 // Gray output pipeline.
    410 // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
    411 // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
    412 // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
    413 
    414 static
    415 cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
    416 {
    417     cmsToneCurve *GrayTRC, *RevGrayTRC;
    418     cmsPipeline* Lut;
    419     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    420 
    421     GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
    422     if (GrayTRC == NULL) return NULL;
    423 
    424     RevGrayTRC = cmsReverseToneCurve(GrayTRC);
    425     if (RevGrayTRC == NULL) return NULL;
    426 
    427     Lut = cmsPipelineAlloc(ContextID, 3, 1);
    428     if (Lut == NULL) {
    429         cmsFreeToneCurve(RevGrayTRC);
    430         return NULL;
    431     }
    432 
    433     if (cmsGetPCS(hProfile) == cmsSigLabData) {
    434 
    435         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1,  3, PickLstarMatrix, NULL)))
    436             goto Error;
    437     }
    438     else  {
    439         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1,  3, PickYMatrix, NULL)))
    440             goto Error;
    441     }
    442 
    443     if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))
    444         goto Error;
    445 
    446     cmsFreeToneCurve(RevGrayTRC);
    447     return Lut;
    448 
    449 Error:
    450     cmsFreeToneCurve(RevGrayTRC);
    451     cmsPipelineFree(Lut);
    452     return NULL;
    453 }
    454 
    455 
    456 static
    457 cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
    458 {
    459     cmsPipeline* Lut;
    460     cmsToneCurve *Shapes[3], *InvShapes[3];
    461     cmsMAT3 Mat, Inv;
    462     int i, j;
    463     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    464 
    465     if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
    466         return NULL;
    467 
    468     if (!_cmsMAT3inverse(&Mat, &Inv))
    469         return NULL;
    470 
    471     // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
    472     // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
    473     // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
    474 
    475     for (i=0; i < 3; i++)
    476         for (j=0; j < 3; j++)
    477             Inv.v[i].n[j] *= OutpAdj;
    478 
    479     Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
    480     Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
    481     Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
    482 
    483     if (!Shapes[0] || !Shapes[1] || !Shapes[2])
    484         return NULL;
    485 
    486     InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
    487     InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
    488     InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
    489 
    490     if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
    491         return NULL;
    492     }
    493 
    494     Lut = cmsPipelineAlloc(ContextID, 3, 3);
    495     if (Lut != NULL) {
    496 
    497         // Note that it is certainly possible a single profile would have a LUT based
    498         // tag for output working in lab and a matrix-shaper for the fallback cases.
    499         // This is not allowed by the spec, but this code is tolerant to those cases
    500         if (cmsGetPCS(hProfile) == cmsSigLabData) {
    501 
    502             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))
    503                 goto Error;
    504         }
    505 
    506         if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||
    507             !cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))
    508             goto Error;
    509     }
    510 
    511     cmsFreeToneCurveTriple(InvShapes);
    512     return Lut;
    513 Error:
    514     cmsFreeToneCurveTriple(InvShapes);
    515     cmsPipelineFree(Lut);
    516     return NULL;
    517 }
    518 
    519 
    520 // Change CLUT interpolation to trilinear
    521 static
    522 void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
    523 {
    524     cmsStage* Stage;
    525 
    526     for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
    527         Stage != NULL;
    528         Stage = cmsStageNext(Stage)) {
    529 
    530             if (cmsStageType(Stage) == cmsSigCLutElemType) {
    531 
    532                 _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
    533 
    534                 CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
    535                 _cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);
    536             }
    537     }
    538 }
    539 
    540 
    541 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
    542 static
    543 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
    544 {
    545     cmsContext ContextID       = cmsGetProfileContextID(hProfile);
    546     cmsPipeline* Lut           = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    547     cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
    548     cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
    549 
    550     if (Lut == NULL) return NULL;
    551 
    552     // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
    553     // and since the formatter has already accomodated to 0..1.0, we should undo this change
    554     if ( PCS == cmsSigLabData)
    555     {
    556         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
    557             goto Error;
    558     }
    559     else
    560         if (PCS == cmsSigXYZData)
    561         {
    562             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
    563                 goto Error;
    564         }
    565 
    566     // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
    567     if ( dataSpace == cmsSigLabData)
    568     {
    569         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
    570             goto Error;
    571     }
    572     else if (dataSpace == cmsSigXYZData)
    573     {
    574         if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
    575             goto Error;
    576     }
    577 
    578     return Lut;
    579 
    580 Error:
    581     cmsPipelineFree(Lut);
    582     return NULL;
    583 }
    584 
    585 // Create an output MPE LUT from agiven profile. Version mismatches are handled here
    586 cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
    587 {
    588     cmsTagTypeSignature OriginalType;
    589     cmsTagSignature tag16    = PCS2Device16[Intent];
    590     cmsTagSignature tagFloat = PCS2DeviceFloat[Intent];
    591     cmsContext ContextID     = cmsGetProfileContextID(hProfile);
    592 
    593 
    594     if (Intent != -1) {
    595 
    596         if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    597 
    598             // Floating point LUT are always V4
    599             return _cmsReadFloatOutputTag(hProfile, tagFloat);
    600         }
    601 
    602         // Revert to perceptual if no tag is found
    603         if (!cmsIsTag(hProfile, tag16)) {
    604             tag16 = PCS2Device16[0];
    605         }
    606 
    607         if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
    608 
    609             // Check profile version and LUT type. Do the necessary adjustments if needed
    610 
    611             // First read the tag
    612             cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
    613             if (Lut == NULL) return NULL;
    614 
    615             // After reading it, we have info about the original type
    616             OriginalType =  _cmsGetTagTrueType(hProfile, tag16);
    617 
    618             // The profile owns the Lut, so we need to copy it
    619             Lut = cmsPipelineDup(Lut);
    620             if (Lut == NULL) return NULL;
    621 
    622             // Now it is time for a controversial stuff. I found that for 3D LUTS using
    623             // Lab used as indexer space,  trilinear interpolation should be used
    624             if (cmsGetPCS(hProfile) == cmsSigLabData)
    625                 ChangeInterpolationToTrilinear(Lut);
    626 
    627             // We need to adjust data only for Lab and Lut16 type
    628             if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
    629                 return Lut;
    630 
    631             // Add a matrix for conversion V4 to V2 Lab PCS
    632             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    633                 goto Error;
    634 
    635             // If the output is Lab, add also a conversion at the end
    636             if (cmsGetColorSpace(hProfile) == cmsSigLabData)
    637                 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    638                     goto Error;
    639 
    640             return Lut;
    641 Error:
    642             cmsPipelineFree(Lut);
    643             return NULL;
    644         }
    645     }
    646 
    647     // Lut not found, try to create a matrix-shaper
    648 
    649     // Check if this is a grayscale profile.
    650     if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
    651 
    652         // if so, build appropiate conversion tables.
    653         // The tables are the PCS iluminant, scaled across GrayTRC
    654         return BuildGrayOutputPipeline(hProfile);
    655     }
    656 
    657     // Not gray, create a normal matrix-shaper, which only operates in XYZ space
    658     return BuildRGBOutputMatrixShaper(hProfile);
    659 }
    660 
    661 // ---------------------------------------------------------------------------------------------------------------
    662 
    663 // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
    664 static
    665 cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
    666 {
    667     cmsContext ContextID       = cmsGetProfileContextID(hProfile);
    668     cmsPipeline* Lut           = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    669     cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
    670     cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
    671 
    672     if (Lut == NULL) return NULL;
    673 
    674     if (spc == cmsSigLabData)
    675     {
    676         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))
    677             goto Error;
    678     }
    679     else
    680         if (spc == cmsSigXYZData)
    681         {
    682             if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))
    683                 goto Error;
    684         }
    685 
    686         if (PCS == cmsSigLabData)
    687         {
    688             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))
    689                 goto Error;
    690         }
    691         else
    692             if (PCS == cmsSigXYZData)
    693             {
    694                 if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))
    695                     goto Error;
    696             }
    697 
    698     return Lut;
    699 Error:
    700     cmsPipelineFree(Lut);
    701     return NULL;
    702 }
    703 
    704 // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
    705 // tag name here may default to AToB0
    706 cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
    707 {
    708     cmsPipeline* Lut;
    709     cmsTagTypeSignature OriginalType;
    710     cmsTagSignature tag16    = Device2PCS16[Intent];
    711     cmsTagSignature tagFloat = Device2PCSFloat[Intent];
    712     cmsContext ContextID = cmsGetProfileContextID(hProfile);
    713 
    714 
    715     // On named color, take the appropiate tag
    716     if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
    717 
    718         cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
    719 
    720         if (nc == NULL) return NULL;
    721 
    722         Lut = cmsPipelineAlloc(ContextID, 0, 0);
    723         if (Lut == NULL)
    724             goto Error;
    725 
    726         if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))
    727             goto Error;
    728 
    729         if (cmsGetColorSpace(hProfile) == cmsSigLabData)
    730             if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    731                 goto Error;
    732 
    733         return Lut;
    734 Error:
    735         cmsPipelineFree(Lut);
    736         cmsFreeNamedColorList(nc);
    737         return NULL;
    738     }
    739 
    740     if (cmsIsTag(hProfile, tagFloat)) {  // Float tag takes precedence
    741 
    742         // Floating point LUT are always V
    743         return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
    744     }
    745 
    746     tagFloat = Device2PCSFloat[0];
    747     if (cmsIsTag(hProfile, tagFloat)) {
    748 
    749         return cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
    750     }
    751 
    752     if (!cmsIsTag(hProfile, tag16)) {  // Is there any LUT-Based table?
    753 
    754         tag16    = Device2PCS16[0];
    755         if (!cmsIsTag(hProfile, tag16)) return NULL;
    756     }
    757 
    758     // Check profile version and LUT type. Do the necessary adjustments if needed
    759 
    760     // Read the tag
    761     Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
    762     if (Lut == NULL) return NULL;
    763 
    764     // The profile owns the Lut, so we need to copy it
    765     Lut = cmsPipelineDup(Lut);
    766     if (Lut == NULL) return NULL;
    767 
    768     // Now it is time for a controversial stuff. I found that for 3D LUTS using
    769     // Lab used as indexer space,  trilinear interpolation should be used
    770     if (cmsGetPCS(hProfile) == cmsSigLabData)
    771         ChangeInterpolationToTrilinear(Lut);
    772 
    773     // After reading it, we have info about the original type
    774     OriginalType =  _cmsGetTagTrueType(hProfile, tag16);
    775 
    776     // We need to adjust data for Lab16 on output
    777     if (OriginalType != cmsSigLut16Type) return Lut;
    778 
    779     // Here it is possible to get Lab on both sides
    780 
    781     if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
    782         if(!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))
    783             goto Error2;
    784     }
    785 
    786     if (cmsGetPCS(hProfile) == cmsSigLabData) {
    787         if(!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))
    788             goto Error2;
    789     }
    790 
    791     return Lut;
    792 
    793 Error2:
    794     cmsPipelineFree(Lut);
    795     return NULL;
    796 }
    797 
    798 // ---------------------------------------------------------------------------------------------------------------
    799 
    800 // Returns TRUE if the profile is implemented as matrix-shaper
    801 cmsBool  CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
    802 {
    803     switch (cmsGetColorSpace(hProfile)) {
    804 
    805     case cmsSigGrayData:
    806 
    807         return cmsIsTag(hProfile, cmsSigGrayTRCTag);
    808 
    809     case cmsSigRgbData:
    810 
    811         return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
    812                 cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
    813                 cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
    814                 cmsIsTag(hProfile, cmsSigRedTRCTag) &&
    815                 cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
    816                 cmsIsTag(hProfile, cmsSigBlueTRCTag));
    817 
    818     default:
    819 
    820         return FALSE;
    821     }
    822 }
    823 
    824 // Returns TRUE if the intent is implemented as CLUT
    825 cmsBool  CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
    826 {
    827     const cmsTagSignature* TagTable;
    828 
    829     // For devicelinks, the supported intent is that one stated in the header
    830     if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
    831             return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
    832     }
    833 
    834     switch (UsedDirection) {
    835 
    836        case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
    837        case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
    838 
    839        // For proofing, we need rel. colorimetric in output. Let's do some recursion
    840        case LCMS_USED_AS_PROOF:
    841            return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
    842                   cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
    843 
    844        default:
    845            cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
    846            return FALSE;
    847     }
    848 
    849     return cmsIsTag(hProfile, TagTable[Intent]);
    850 
    851 }
    852 
    853 
    854 // Return info about supported intents
    855 cmsBool  CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
    856                                         cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
    857 {
    858 
    859     if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
    860 
    861     // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
    862     // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
    863     // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
    864     // the accuracy would be less than optimal in rel.col and v2 case.
    865 
    866     return cmsIsMatrixShaper(hProfile);
    867 }
    868 
    869 
    870 // ---------------------------------------------------------------------------------------------------------------
    871 
    872 // Read both, profile sequence description and profile sequence id if present. Then combine both to
    873 // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
    874 cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
    875 {
    876     cmsSEQ* ProfileSeq;
    877     cmsSEQ* ProfileId;
    878     cmsSEQ* NewSeq;
    879     cmsUInt32Number i;
    880 
    881     // Take profile sequence description first
    882     ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
    883 
    884     // Take profile sequence ID
    885     ProfileId  = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
    886 
    887     if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
    888 
    889     if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
    890     if (ProfileId  == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
    891 
    892     // We have to mix both together. For that they must agree
    893     if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
    894 
    895     NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
    896 
    897     // Ok, proceed to the mixing
    898     if (NewSeq != NULL) {
    899         for (i=0; i < ProfileSeq ->n; i++) {
    900 
    901             memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
    902             NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
    903         }
    904     }
    905     return NewSeq;
    906 }
    907 
    908 // Dump the contents of profile sequence in both tags (if v4 available)
    909 cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
    910 {
    911     if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
    912 
    913     if (cmsGetProfileVersion(hProfile) >= 4.0) {
    914 
    915             if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
    916     }
    917 
    918     return TRUE;
    919 }
    920 
    921 
    922 // Auxiliar, read and duplicate a MLU if found.
    923 static
    924 cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
    925 {
    926     cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
    927     if (mlu == NULL) return NULL;
    928 
    929     return cmsMLUdup(mlu);
    930 }
    931 
    932 // Create a sequence description out of an array of profiles
    933 cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
    934 {
    935     cmsUInt32Number i;
    936     cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
    937 
    938     if (seq == NULL) return NULL;
    939 
    940     for (i=0; i < nProfiles; i++) {
    941 
    942         cmsPSEQDESC* ps = &seq ->seq[i];
    943         cmsHPROFILE h = hProfiles[i];
    944         cmsTechnologySignature* techpt;
    945 
    946         cmsGetHeaderAttributes(h, &ps ->attributes);
    947         cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
    948         ps ->deviceMfg   = cmsGetHeaderManufacturer(h);
    949         ps ->deviceModel = cmsGetHeaderModel(h);
    950 
    951         techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
    952         if (techpt == NULL)
    953             ps ->technology   =  (cmsTechnologySignature) 0;
    954         else
    955             ps ->technology   = *techpt;
    956 
    957         ps ->Manufacturer = GetMLUFromProfile(h,  cmsSigDeviceMfgDescTag);
    958         ps ->Model        = GetMLUFromProfile(h,  cmsSigDeviceModelDescTag);
    959         ps ->Description  = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
    960 
    961     }
    962 
    963     return seq;
    964 }
    965 
    966 // -------------------------------------------------------------------------------------------------------------------
    967 
    968 
    969 static
    970 const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
    971 {
    972     cmsTagSignature sig;
    973 
    974     switch (Info) {
    975 
    976     case cmsInfoDescription:
    977         sig = cmsSigProfileDescriptionTag;
    978         break;
    979 
    980     case cmsInfoManufacturer:
    981         sig = cmsSigDeviceMfgDescTag;
    982         break;
    983 
    984     case cmsInfoModel:
    985         sig = cmsSigDeviceModelDescTag;
    986          break;
    987 
    988     case cmsInfoCopyright:
    989         sig = cmsSigCopyrightTag;
    990         break;
    991 
    992     default: return NULL;
    993     }
    994 
    995 
    996     return (cmsMLU*) cmsReadTag(hProfile, sig);
    997 }
    998 
    999 
   1000 
   1001 cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
   1002                                             const char LanguageCode[3], const char CountryCode[3],
   1003                                             wchar_t* Buffer, cmsUInt32Number BufferSize)
   1004 {
   1005     const cmsMLU* mlu = GetInfo(hProfile, Info);
   1006     if (mlu == NULL) return 0;
   1007 
   1008     return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
   1009 }
   1010 
   1011 
   1012 cmsUInt32Number  CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
   1013                                                           const char LanguageCode[3], const char CountryCode[3],
   1014                                                           char* Buffer, cmsUInt32Number BufferSize)
   1015 {
   1016     const cmsMLU* mlu = GetInfo(hProfile, Info);
   1017     if (mlu == NULL) return 0;
   1018 
   1019     return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
   1020 }
   1021