1 //---------------------------------------------------------------------------------
3 // Little Color Management System
4 // Copyright (c) 1998-2012 Marti Maria Saguer
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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.
24 //---------------------------------------------------------------------------------
27 #include "lcms2_internal.h"
29 // Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
32 static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual
33 cmsSigAToB1Tag, // Relative colorimetric
34 cmsSigAToB2Tag, // Saturation
35 cmsSigAToB1Tag }; // Absolute colorimetric
37 static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual
38 cmsSigDToB1Tag, // Relative colorimetric
39 cmsSigDToB2Tag, // Saturation
40 cmsSigDToB3Tag }; // Absolute colorimetric
42 static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual
43 cmsSigBToA1Tag, // Relative colorimetric
44 cmsSigBToA2Tag, // Saturation
45 cmsSigBToA1Tag }; // Absolute colorimetric
47 static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual
48 cmsSigBToD1Tag, // Relative colorimetric
49 cmsSigBToD2Tag, // Saturation
50 cmsSigBToD3Tag }; // Absolute colorimetric
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)
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 };
63 // Get a media white point fixing some issues found in certain old profiles
64 cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
68 _cmsAssert(Dest != NULL);
70 Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
74 *Dest = *cmsD50_XYZ();
78 // V2 display profiles should give D50
79 if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
81 if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
82 *Dest = *cmsD50_XYZ();
93 // Chromatic adaptation matrix. Fix some issues as well
94 cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
98 _cmsAssert(Dest != NULL);
100 Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
108 // No CHAD available, default it to identity
109 _cmsMAT3identity(Dest);
111 // V2 display profiles should give D50
112 if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
114 if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
116 cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
120 _cmsMAT3identity(Dest);
124 return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());
132 // Auxiliar, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
134 cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
136 cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
138 _cmsAssert(r != NULL);
140 PtrRed = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);
141 PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);
142 PtrBlue = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);
144 if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
147 _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);
148 _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);
149 _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);
155 // Gray input pipeline
157 cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)
159 cmsToneCurve *GrayTRC;
161 cmsContext ContextID = cmsGetProfileContextID(hProfile);
163 GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
164 if (GrayTRC == NULL) return NULL;
166 Lut = cmsPipelineAlloc(ContextID, 1, 3);
167 if (Lut == NULL) return NULL;
169 if (cmsGetPCS(hProfile) == cmsSigLabData) {
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];
176 EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);
178 if (EmptyTab == NULL) {
180 cmsPipelineFree(Lut);
184 LabCurves[0] = GrayTRC;
185 LabCurves[1] = EmptyTab;
186 LabCurves[2] = EmptyTab;
188 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, OneToThreeInputMatrix, NULL));
189 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves));
191 cmsFreeToneCurve(EmptyTab);
195 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC));
196 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, GrayInputMatrix, NULL));
204 cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
208 cmsToneCurve *Shapes[3];
209 cmsContext ContextID = cmsGetProfileContextID(hProfile);
212 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
214 // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
215 // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
216 // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
218 for (i=0; i < 3; i++)
219 for (j=0; j < 3; j++)
220 Mat.v[i].n[j] *= InpAdj;
223 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
224 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
225 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
227 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
230 Lut = cmsPipelineAlloc(ContextID, 3, 3);
233 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes));
234 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL));
236 // Note that it is certainly possible a single profile would have a LUT based
237 // tag for output working in lab and a matrix-shaper for the fallback cases.
238 // This is not allowed by the spec, but this code is tolerant to those cases
239 if (cmsGetPCS(hProfile) == cmsSigLabData) {
241 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID));
251 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
253 cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
255 cmsContext ContextID = cmsGetProfileContextID(hProfile);
256 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
257 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
259 if (Lut == NULL) return NULL;
261 // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
262 // and since the formatter has already accomodated to 0..1.0, we should undo this change
263 if ( spc == cmsSigLabData)
265 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID));
268 if (spc == cmsSigXYZData)
270 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID));
277 cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
279 cmsContext ContextID = cmsGetProfileContextID(hProfile);
280 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
281 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
282 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
284 if (Lut == NULL) return NULL;
286 // input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,
287 // these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)
288 if ( spc == cmsSigLabData)
290 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID));
292 else if (spc == cmsSigXYZData)
294 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID));
297 if ( PCS == cmsSigLabData)
299 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID));
301 else if( PCS == cmsSigXYZData)
303 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID));
310 // Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
311 // is adjusted here in order to create a LUT that takes care of all those details
312 cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
314 cmsTagTypeSignature OriginalType;
315 cmsTagSignature tag16 = Device2PCS16[Intent];
316 cmsTagSignature tagFloat = Device2PCSFloat[Intent];
317 cmsContext ContextID = cmsGetProfileContextID(hProfile);
319 // On named color, take the appropiate tag
320 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
323 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
325 if (nc == NULL) return NULL;
327 Lut = cmsPipelineAlloc(ContextID, 0, 0);
329 cmsFreeNamedColorList(nc);
333 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE));
334 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
338 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
340 // Floating point LUT are always V4, but the encoding range is no
341 // longer 0..1.0, so we need to add an stage depending on the color space
342 return _cmsReadFloatInputTag(hProfile, tagFloat);
345 // Revert to perceptual if no tag is found
346 if (!cmsIsTag(hProfile, tag16)) {
347 tag16 = Device2PCS16[0];
350 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
352 // Check profile version and LUT type. Do the necessary adjustments if needed
354 // First read the tag
355 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
356 if (Lut == NULL) return NULL;
358 // After reading it, we have now info about the original type
359 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
361 // The profile owns the Lut, so we need to copy it
362 Lut = cmsPipelineDup(Lut);
364 // We need to adjust data only for Lab16 on output
365 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
368 // If the input is Lab, add also a conversion at the begin
369 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
370 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID));
372 // Add a matrix for conversion V2 to V4 Lab PCS
373 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
377 // Lut was not found, try to create a matrix-shaper
379 // Check if this is a grayscale profile.
380 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
382 // if so, build appropiate conversion tables.
383 // The tables are the PCS iluminant, scaled across GrayTRC
384 return BuildGrayInputMatrixPipeline(hProfile);
387 // Not gray, create a normal matrix-shaper
388 return BuildRGBInputMatrixShaper(hProfile);
391 // ---------------------------------------------------------------------------------------------------------------
393 // Gray output pipeline.
394 // XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be
395 // given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.
396 // The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.
399 cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)
401 cmsToneCurve *GrayTRC, *RevGrayTRC;
403 cmsContext ContextID = cmsGetProfileContextID(hProfile);
405 GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);
406 if (GrayTRC == NULL) return NULL;
408 RevGrayTRC = cmsReverseToneCurve(GrayTRC);
409 if (RevGrayTRC == NULL) return NULL;
411 Lut = cmsPipelineAlloc(ContextID, 3, 1);
413 cmsFreeToneCurve(RevGrayTRC);
417 if (cmsGetPCS(hProfile) == cmsSigLabData) {
419 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL));
422 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL));
425 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC));
426 cmsFreeToneCurve(RevGrayTRC);
435 cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
438 cmsToneCurve *Shapes[3], *InvShapes[3];
441 cmsContext ContextID = cmsGetProfileContextID(hProfile);
443 if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
446 if (!_cmsMAT3inverse(&Mat, &Inv))
449 // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
450 // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
451 // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
453 for (i=0; i < 3; i++)
454 for (j=0; j < 3; j++)
455 Inv.v[i].n[j] *= OutpAdj;
457 Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);
458 Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);
459 Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);
461 if (!Shapes[0] || !Shapes[1] || !Shapes[2])
464 InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
465 InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
466 InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
468 if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
472 Lut = cmsPipelineAlloc(ContextID, 3, 3);
475 // Note that it is certainly possible a single profile would have a LUT based
476 // tag for output working in lab and a matrix-shaper for the fallback cases.
477 // This is not allowed by the spec, but this code is tolerant to those cases
478 if (cmsGetPCS(hProfile) == cmsSigLabData) {
480 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID));
483 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL));
484 cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes));
487 cmsFreeToneCurveTriple(InvShapes);
492 // Change CLUT interpolation to trilinear
494 void ChangeInterpolationToTrilinear(cmsPipeline* Lut)
498 for (Stage = cmsPipelineGetPtrToFirstStage(Lut);
500 Stage = cmsStageNext(Stage)) {
502 if (cmsStageType(Stage) == cmsSigCLutElemType) {
504 _cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;
506 CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;
507 _cmsSetInterpolationRoutine(CLUT ->Params);
513 // Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded
515 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
517 cmsContext ContextID = cmsGetProfileContextID(hProfile);
518 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
519 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
521 if (Lut == NULL) return NULL;
523 // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
524 // and since the formatter has already accomodated to 0..1.0, we should undo this change
525 if ( PCS == cmsSigLabData)
527 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID));
530 if (PCS == cmsSigXYZData)
532 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID));
539 cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
541 cmsContext ContextID = cmsGetProfileContextID(hProfile);
542 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
543 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
544 cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);
546 if (Lut == NULL) return NULL;
548 // If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,
549 // and since the formatter has already accomodated to 0..1.0, we should undo this change
550 if ( PCS == cmsSigLabData)
552 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID));
555 if (PCS == cmsSigXYZData)
557 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID));
560 // the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline
561 if ( dataSpace == cmsSigLabData)
563 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID));
565 else if ( dataSpace == cmsSigXYZData)
567 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID));
573 // Create an output MPE LUT from agiven profile. Version mismatches are handled here
574 cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
576 cmsTagTypeSignature OriginalType;
577 cmsTagSignature tag16 = PCS2Device16[Intent];
578 cmsTagSignature tagFloat = PCS2DeviceFloat[Intent];
579 cmsContext ContextID = cmsGetProfileContextID(hProfile);
581 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
583 // Floating point LUT are always V4
584 return _cmsReadFloatOutputTag(hProfile, tagFloat);
587 // Revert to perceptual if no tag is found
588 if (!cmsIsTag(hProfile, tag16)) {
589 tag16 = PCS2Device16[0];
592 if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
594 // Check profile version and LUT type. Do the necessary adjustments if needed
596 // First read the tag
597 cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
598 if (Lut == NULL) return NULL;
600 // After reading it, we have info about the original type
601 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
603 // The profile owns the Lut, so we need to copy it
604 Lut = cmsPipelineDup(Lut);
605 if (Lut == NULL) return NULL;
607 // Now it is time for a controversial stuff. I found that for 3D LUTS using
608 // Lab used as indexer space, trilinear interpolation should be used
609 if (cmsGetPCS(hProfile) == cmsSigLabData)
610 ChangeInterpolationToTrilinear(Lut);
612 // We need to adjust data only for Lab and Lut16 type
613 if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
616 // Add a matrix for conversion V4 to V2 Lab PCS
617 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID));
619 // If the output is Lab, add also a conversion at the end
620 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
621 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
626 // Lut not found, try to create a matrix-shaper
628 // Check if this is a grayscale profile.
629 if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
631 // if so, build appropiate conversion tables.
632 // The tables are the PCS iluminant, scaled across GrayTRC
633 return BuildGrayOutputPipeline(hProfile);
636 // Not gray, create a normal matrix-shaper, which only operates in XYZ space
637 return BuildRGBOutputMatrixShaper(hProfile);
640 // ---------------------------------------------------------------------------------------------------------------
642 // Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded
644 cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)
646 cmsContext ContextID = cmsGetProfileContextID(hProfile);
647 cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
648 cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);
649 cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);
651 if (Lut == NULL) return NULL;
653 if (spc == cmsSigLabData)
655 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID));
658 if (spc == cmsSigXYZData)
660 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID));
663 if (PCS == cmsSigLabData)
665 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID));
668 if (PCS == cmsSigXYZData)
670 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID));
676 // This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
677 // tag name here may default to AToB0
678 cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
681 cmsTagTypeSignature OriginalType;
682 cmsTagSignature tag16 = Device2PCS16[Intent];
683 cmsTagSignature tagFloat = Device2PCSFloat[Intent];
684 cmsContext ContextID = cmsGetProfileContextID(hProfile);
687 // On named color, take the appropiate tag
688 if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {
690 cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);
692 if (nc == NULL) return NULL;
694 Lut = cmsPipelineAlloc(ContextID, 0, 0);
696 cmsFreeNamedColorList(nc);
700 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE));
701 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
702 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
706 if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
708 // Floating point LUT are always V
709 return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);
712 tagFloat = Device2PCSFloat[0];
713 if (cmsIsTag(hProfile, tagFloat)) {
715 return cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));
718 if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
720 tag16 = Device2PCS16[0];
721 if (!cmsIsTag(hProfile, tag16)) return NULL;
724 // Check profile version and LUT type. Do the necessary adjustments if needed
727 Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);
728 if (Lut == NULL) return NULL;
730 // The profile owns the Lut, so we need to copy it
731 Lut = cmsPipelineDup(Lut);
732 if (Lut == NULL) return NULL;
734 // Now it is time for a controversial stuff. I found that for 3D LUTS using
735 // Lab used as indexer space, trilinear interpolation should be used
736 if (cmsGetColorSpace(hProfile) == cmsSigLabData)
737 ChangeInterpolationToTrilinear(Lut);
739 // After reading it, we have info about the original type
740 OriginalType = _cmsGetTagTrueType(hProfile, tag16);
742 // We need to adjust data for Lab16 on output
743 if (OriginalType != cmsSigLut16Type) return Lut;
745 // Here it is possible to get Lab on both sides
747 if (cmsGetPCS(hProfile) == cmsSigLabData) {
748 cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID));
751 if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
752 cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
760 // ---------------------------------------------------------------------------------------------------------------
762 // Returns TRUE if the profile is implemented as matrix-shaper
763 cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
765 switch (cmsGetColorSpace(hProfile)) {
769 return cmsIsTag(hProfile, cmsSigGrayTRCTag);
773 return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
774 cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
775 cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
776 cmsIsTag(hProfile, cmsSigRedTRCTag) &&
777 cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
778 cmsIsTag(hProfile, cmsSigBlueTRCTag));
786 // Returns TRUE if the intent is implemented as CLUT
787 cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
789 const cmsTagSignature* TagTable;
791 // For devicelinks, the supported intent is that one stated in the header
792 if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
793 return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
796 switch (UsedDirection) {
798 case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
799 case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
801 // For proofing, we need rel. colorimetric in output. Let's do some recursion
802 case LCMS_USED_AS_PROOF:
803 return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
804 cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
807 cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
811 return cmsIsTag(hProfile, TagTable[Intent]);
816 // Return info about supported intents
817 cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
818 cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
821 if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
823 // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
824 // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
825 // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
826 // the accuracy would be less than optimal in rel.col and v2 case.
828 return cmsIsMatrixShaper(hProfile);
832 // ---------------------------------------------------------------------------------------------------------------
834 // Read both, profile sequence description and profile sequence id if present. Then combine both to
835 // create qa unique structure holding both. Shame on ICC to store things in such complicated way.
836 cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
843 // Take profile sequence description first
844 ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
846 // Take profile sequence ID
847 ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
849 if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
851 if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
852 if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
854 // We have to mix both together. For that they must agree
855 if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
857 NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
859 // Ok, proceed to the mixing
860 if (NewSeq != NULL) {
861 for (i=0; i < ProfileSeq ->n; i++) {
863 memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
864 NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
870 // Dump the contents of profile sequence in both tags (if v4 available)
871 cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
873 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
875 if (cmsGetProfileVersion(hProfile) >= 4.0) {
877 if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
884 // Auxiliar, read and duplicate a MLU if found.
886 cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
888 cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);
889 if (mlu == NULL) return NULL;
891 return cmsMLUdup(mlu);
894 // Create a sequence description out of an array of profiles
895 cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
898 cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
900 if (seq == NULL) return NULL;
902 for (i=0; i < nProfiles; i++) {
904 cmsPSEQDESC* ps = &seq ->seq[i];
905 cmsHPROFILE h = hProfiles[i];
906 cmsTechnologySignature* techpt;
908 cmsGetHeaderAttributes(h, &ps ->attributes);
909 cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
910 ps ->deviceMfg = cmsGetHeaderManufacturer(h);
911 ps ->deviceModel = cmsGetHeaderModel(h);
913 techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);
915 ps ->technology = (cmsTechnologySignature) 0;
917 ps ->technology = *techpt;
919 ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
920 ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
921 ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
928 // -------------------------------------------------------------------------------------------------------------------
932 const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
938 case cmsInfoDescription:
939 sig = cmsSigProfileDescriptionTag;
942 case cmsInfoManufacturer:
943 sig = cmsSigDeviceMfgDescTag;
947 sig = cmsSigDeviceModelDescTag;
950 case cmsInfoCopyright:
951 sig = cmsSigCopyrightTag;
954 default: return NULL;
958 return (cmsMLU*) cmsReadTag(hProfile, sig);
963 cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
964 const char LanguageCode[3], const char CountryCode[3],
965 wchar_t* Buffer, cmsUInt32Number BufferSize)
967 const cmsMLU* mlu = GetInfo(hProfile, Info);
968 if (mlu == NULL) return 0;
970 return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
974 cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
975 const char LanguageCode[3], const char CountryCode[3],
976 char* Buffer, cmsUInt32Number BufferSize)
978 const cmsMLU* mlu = GetInfo(hProfile, Info);
979 if (mlu == NULL) return 0;
981 return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);