summaryrefslogtreecommitdiff
path: root/src/cmsio1.c
diff options
context:
space:
mode:
authorMarti Maria <info@littlecms.com>2010-02-08 17:20:25 +0100
committerMarti Maria <info@littlecms.com>2010-02-08 17:20:25 +0100
commit3cefc4ee12fe7144a65a205f6645c14ec5be68ce (patch)
tree37c160448e2accb5bbada6f6d4798a90741e4a12 /src/cmsio1.c
downloadlcms2-3cefc4ee12fe7144a65a205f6645c14ec5be68ce.tar.gz
initial commit
Diffstat (limited to 'src/cmsio1.c')
-rw-r--r--src/cmsio1.c746
1 files changed, 746 insertions, 0 deletions
diff --git a/src/cmsio1.c b/src/cmsio1.c
new file mode 100644
index 0000000..2c09549
--- /dev/null
+++ b/src/cmsio1.c
@@ -0,0 +1,746 @@
+//---------------------------------------------------------------------------------
+//
+// Little Color Management System
+// Copyright (c) 1998-2010 Marti Maria Saguer
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the Software
+// is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
+// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+//---------------------------------------------------------------------------------
+//
+
+#include "lcms2_internal.h"
+
+// Read tags using low-level functions, provides necessary glue code to adapt versions, etc.
+
+// LUT tags
+static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual
+ cmsSigAToB1Tag, // Relative colorimetric
+ cmsSigAToB2Tag, // Saturation
+ cmsSigAToB1Tag }; // Absolute colorimetric
+
+static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual
+ cmsSigDToB1Tag, // Relative colorimetric
+ cmsSigDToB2Tag, // Saturation
+ cmsSigDToB3Tag }; // Absolute colorimetric
+
+static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual
+ cmsSigBToA1Tag, // Relative colorimetric
+ cmsSigBToA2Tag, // Saturation
+ cmsSigBToA1Tag }; // Absolute colorimetric
+
+static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual
+ cmsSigBToD1Tag, // Relative colorimetric
+ cmsSigBToD2Tag, // Saturation
+ cmsSigBToD3Tag }; // Absolute colorimetric
+
+
+// Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa
+#define InpAdj (1.0/MAX_ENCODEABLE_XYZ) // (65536.0/(65535.0*2.0))
+#define OutpAdj (MAX_ENCODEABLE_XYZ) // ((2.0*65535.0)/65536.0)
+
+// Equal power for Gray. This is just to obtain XYZ of D50 scaled down. Note that gray spaces are implemented
+// as RGB spaces, and R=G=B is forced in input.
+static const cmsFloat64Number
+ GrayInputEqupower[] = { (InpAdj*cmsD50X)/3.0, (InpAdj*cmsD50X)/3.0, (InpAdj*cmsD50X)/3.0,
+ (InpAdj*cmsD50Y)/3.0, (InpAdj*cmsD50Y)/3.0, (InpAdj*cmsD50Y)/3.0,
+ (InpAdj*cmsD50Z)/3.0, (InpAdj*cmsD50Z)/3.0, (InpAdj*cmsD50Z)/3.0,
+ 0, 0, 0 };
+
+static const cmsFloat64Number
+ GrayOutputEqupower[] = {(OutpAdj*cmsD50X)/3.0, (OutpAdj*cmsD50X)/3.0, (OutpAdj*cmsD50X)/3.0,
+ (OutpAdj*cmsD50Y)/3.0, (OutpAdj*cmsD50Y)/3.0, (OutpAdj*cmsD50Y)/3.0,
+ (OutpAdj*cmsD50Z)/3.0, (OutpAdj*cmsD50Z)/3.0, (OutpAdj*cmsD50Z)/3.0,
+ 0, 0, 0 };
+
+// Get a media white point fixing some issues found in certain old profiles
+cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)
+{
+ cmsCIEXYZ* Tag;
+
+ Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
+
+ // If no wp, take D50
+ if (Tag == NULL) {
+ *Dest = *cmsD50_XYZ();
+ return TRUE;
+ }
+
+ // V2 display profiles should give D50
+ if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
+
+ if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
+ *Dest = *cmsD50_XYZ();
+ return TRUE;
+ }
+ }
+
+ // All seems ok
+ *Dest = *Tag;
+ return TRUE;
+}
+
+
+// Chromatic adaptation matrix. Fix some issues as well
+cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)
+{
+ cmsMAT3* Tag;
+
+ Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);
+
+ if (Tag == NULL) {
+ _cmsMAT3identity(Dest);
+ return TRUE;
+ }
+
+ // V2 display profiles should give D50
+ if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {
+
+ if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {
+
+ cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);
+
+ if (White == NULL) {
+
+ _cmsMAT3identity(Dest);
+ return TRUE;
+ }
+
+ return _cmsAdaptationMatrix(Dest, NULL, cmsD50_XYZ(), White);
+ }
+ }
+
+ *Dest = *Tag;
+ return TRUE;
+}
+
+
+// Auxiliar, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper
+static
+cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)
+{
+ cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;
+
+ PtrRed = cmsReadTag(hProfile, cmsSigRedColorantTag);
+ PtrGreen = cmsReadTag(hProfile, cmsSigGreenColorantTag);
+ PtrBlue = cmsReadTag(hProfile, cmsSigBlueColorantTag);
+
+ if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)
+ return FALSE;
+
+ _cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);
+ _cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);
+ _cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);
+
+ return TRUE;
+}
+
+// Some gray profiles are using kTCR as L*, this function converts the curve to XYZ PCS.
+static
+void FromLstarToXYZ(cmsToneCurve* g, cmsToneCurve* gxyz[3])
+{
+ int i;
+ const int nPoints = 4096;
+ cmsCIELab Lab;
+ cmsCIEXYZ XYZ;
+
+ // Allocate curves
+ gxyz[0] = cmsBuildTabulatedToneCurve16(g ->InterpParams->ContextID, nPoints, NULL);
+ gxyz[1] = cmsBuildTabulatedToneCurve16(g ->InterpParams->ContextID, nPoints, NULL);
+ gxyz[2] = cmsBuildTabulatedToneCurve16(g ->InterpParams->ContextID, nPoints, NULL);
+
+ // Resample curve, converting from Lab to XYZ
+ for (i=0; i < nPoints; i++) {
+
+ cmsUInt16Number val = _cmsQuantizeVal(i, nPoints);
+ cmsUInt16Number w;
+
+ w = cmsEvalToneCurve16(g, val);
+
+ Lab.L = ((cmsFloat64Number) 100.0 * w ) / 65535.0;
+ Lab.a = Lab.b = 0;
+
+ cmsLab2XYZ(NULL, &XYZ, &Lab);
+
+ // Store XYX data
+ gxyz[0] ->Table16[i] = _cmsQuickSaturateWord((65535.0 * XYZ.X) / cmsD50X);
+ gxyz[1] ->Table16[i] = _cmsQuickSaturateWord((65535.0 * XYZ.Y) / cmsD50Y);
+ gxyz[2] ->Table16[i] = _cmsQuickSaturateWord((65535.0 * XYZ.Z) / cmsD50Z);
+ }
+}
+
+
+// Gray matrix shaper
+static
+cmsPipeline* BuildGrayInputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsToneCurve *GrayTRC;
+ cmsToneCurve *Shapes[3];
+ cmsPipeline* Lut;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ GrayTRC = cmsReadTag(hProfile, cmsSigGrayTRCTag); // This is Y
+ if (GrayTRC == NULL) return NULL;
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ // Fixup for Lab monochrome
+ FromLstarToXYZ(GrayTRC, Shapes);
+ }
+ else {
+ Shapes[0] = cmsDupToneCurve(GrayTRC);
+ Shapes[1] = cmsDupToneCurve(GrayTRC);
+ Shapes[2] = cmsDupToneCurve(GrayTRC);
+ }
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2]) {
+ return NULL;
+ }
+
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes));
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, GrayInputEqupower, NULL));
+ }
+
+ cmsFreeToneCurveTriple(Shapes);
+ return Lut;
+}
+
+// RGB Matrix shaper
+static
+cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsPipeline* Lut;
+ cmsMAT3 Mat;
+ cmsToneCurve *Shapes[3];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+ int i, j;
+
+ if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;
+
+ // XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so
+ // we need to adjust the output by a factor of (0x10000/0xffff) to put data in
+ // a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ Mat.v[i].n[j] *= InpAdj;
+
+
+ Shapes[0] = cmsReadTag(hProfile, cmsSigRedTRCTag);
+ Shapes[1] = cmsReadTag(hProfile, cmsSigGreenTRCTag);
+ Shapes[2] = cmsReadTag(hProfile, cmsSigBlueTRCTag);
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2])
+ return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes));
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL));
+ }
+
+ return Lut;
+}
+
+// Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc
+// is adjusted here in order to create a LUT that takes care of all those details
+cmsPipeline* _cmsReadInputLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = Device2PCS16[Intent];
+ cmsTagSignature tagFloat = Device2PCSFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V4, so no adjustment is required
+ return cmsPipelineDup(cmsReadTag(hProfile, tagFloat));
+ }
+
+ if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // First read the tag
+ cmsPipeline* Lut = cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // After reading it, we have now info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+
+ // We need to adjust data only for Lab16 on output
+ if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
+ return Lut;
+
+ // Add a matrix for conversion V2 to V4 Lab PCS
+ cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
+ return Lut;
+ }
+
+ // Lut was not found, try to create a matrix-shaper
+
+ // Check if this is a grayscale profile.
+ if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
+
+ // if so, build appropiate conversion tables.
+ // The tables are the PCS iluminant, scaled across GrayTRC
+ return BuildGrayInputMatrixShaper(hProfile);
+ }
+
+ // Not gray, create a normal matrix-shaper
+ return BuildRGBInputMatrixShaper(hProfile);
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Gray matrix shaper
+static
+cmsPipeline* BuildGrayOutputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsToneCurve *GrayTRC, *Shapes[3], *RevShapes[3];
+ cmsPipeline* Lut;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ GrayTRC = cmsReadTag(hProfile, cmsSigGrayTRCTag); // Y
+ if (GrayTRC == NULL) return NULL;
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+
+ // Fixup for Lab monochrome
+ FromLstarToXYZ(GrayTRC, Shapes);
+ }
+ else {
+ Shapes[0] = cmsDupToneCurve(GrayTRC);
+ Shapes[1] = cmsDupToneCurve(GrayTRC);
+ Shapes[2] = cmsDupToneCurve(GrayTRC);
+ }
+
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2])
+ return NULL;
+
+ // This is output matrix-shaper, so we need to reverse curves
+ RevShapes[0] = cmsReverseToneCurve(Shapes[0]);
+ RevShapes[1] = cmsReverseToneCurve(Shapes[1]);
+ RevShapes[2] = cmsReverseToneCurve(Shapes[2]);
+
+ if (!RevShapes[0] || !RevShapes[1] || !RevShapes[2])
+ return NULL;
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, RevShapes));
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, GrayOutputEqupower, NULL));
+ }
+
+ cmsFreeToneCurveTriple(Shapes);
+ cmsFreeToneCurveTriple(RevShapes);
+ return Lut;
+}
+
+static
+cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)
+{
+ cmsPipeline* Lut;
+ cmsToneCurve *Shapes[3], *InvShapes[3];
+ cmsMAT3 Mat, Inv;
+ int i, j;
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))
+ return NULL;
+
+ if (!_cmsMAT3inverse(&Mat, &Inv))
+ return NULL;
+
+ // XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so
+ // we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of
+ // (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;
+
+ for (i=0; i < 3; i++)
+ for (j=0; j < 3; j++)
+ Inv.v[i].n[j] *= OutpAdj;
+
+ Shapes[0] = cmsReadTag(hProfile, cmsSigRedTRCTag);
+ Shapes[1] = cmsReadTag(hProfile, cmsSigGreenTRCTag);
+ Shapes[2] = cmsReadTag(hProfile, cmsSigBlueTRCTag);
+
+ if (!Shapes[0] || !Shapes[1] || !Shapes[2])
+ return NULL;
+
+ InvShapes[0] = cmsReverseToneCurve(Shapes[0]);
+ InvShapes[1] = cmsReverseToneCurve(Shapes[1]);
+ InvShapes[2] = cmsReverseToneCurve(Shapes[2]);
+
+ if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {
+ return NULL;
+ }
+
+ Lut = cmsPipelineAlloc(ContextID, 3, 3);
+ if (Lut != NULL) {
+
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL));
+ cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes));
+ }
+
+ cmsFreeToneCurveTriple(InvShapes);
+ return Lut;
+}
+
+// Create an output MPE LUT from agiven profile. Version mismatches are handled here
+cmsPipeline* _cmsReadOutputLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = PCS2Device16[Intent];
+ cmsTagSignature tagFloat = PCS2DeviceFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V4, so no adjustment is required
+ return cmsPipelineDup(cmsReadTag(hProfile, tagFloat));
+ }
+
+ if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // First read the tag
+ cmsPipeline* Lut = cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // After reading it, we have info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+
+ // We need to adjust data only for Lab and Lut16 type
+ if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)
+ return Lut;
+
+ // Add a matrix for conversion V4 to V2 Lab PCS
+ cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID));
+ return Lut;
+ }
+
+ // Lut not found, try to create a matrix-shaper
+
+ // Check if this is a grayscale profile.
+ if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {
+
+ // if so, build appropiate conversion tables.
+ // The tables are the PCS iluminant, scaled across GrayTRC
+ return BuildGrayOutputMatrixShaper(hProfile);
+ }
+
+ // Not gray, create a normal matrix-shaper
+ return BuildRGBOutputMatrixShaper(hProfile);
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The
+// tag name here may default to AToB0
+cmsPipeline* _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, int Intent)
+{
+ cmsPipeline* Lut;
+ cmsTagTypeSignature OriginalType;
+ cmsTagSignature tag16 = Device2PCS16[Intent];
+ cmsTagSignature tagFloat = Device2PCSFloat[Intent];
+ cmsContext ContextID = cmsGetProfileContextID(hProfile);
+
+ if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence
+
+ // Floating point LUT are always V4, no adjustment is required
+ return cmsPipelineDup(cmsReadTag(hProfile, tagFloat));
+ }
+
+ tagFloat = Device2PCSFloat[0];
+ if (cmsIsTag(hProfile, tagFloat)) {
+
+ return cmsPipelineDup(cmsReadTag(hProfile, tagFloat));
+ }
+
+ if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?
+
+ tag16 = Device2PCS16[0];
+ if (!cmsIsTag(hProfile, tag16)) return NULL;
+ }
+
+ // Check profile version and LUT type. Do the necessary adjustments if needed
+
+ // Read the tag
+ Lut = cmsReadTag(hProfile, tag16);
+ if (Lut == NULL) return NULL;
+
+ // The profile owns the Lut, so we need to copy it
+ Lut = cmsPipelineDup(Lut);
+
+ // After reading it, we have info about the original type
+ OriginalType = _cmsGetTagTrueType(hProfile, tag16);
+
+ // We need to adjust data for Lab16 on output
+ if (OriginalType != cmsSigLut16Type) return Lut;
+
+ // Here it is possible to get Lab on both sides
+
+ if (cmsGetPCS(hProfile) == cmsSigLabData) {
+ cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID));
+ }
+
+ if (cmsGetColorSpace(hProfile) == cmsSigLabData) {
+ cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID));
+ }
+
+ return Lut;
+
+
+}
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Returns TRUE if the profile is implemented as matrix-shaper
+cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)
+{
+ switch (cmsGetColorSpace(hProfile)) {
+
+ case cmsSigGrayData:
+
+ return cmsIsTag(hProfile, cmsSigGrayTRCTag);
+
+ case cmsSigRgbData:
+
+ return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&
+ cmsIsTag(hProfile, cmsSigGreenColorantTag) &&
+ cmsIsTag(hProfile, cmsSigBlueColorantTag) &&
+ cmsIsTag(hProfile, cmsSigRedTRCTag) &&
+ cmsIsTag(hProfile, cmsSigGreenTRCTag) &&
+ cmsIsTag(hProfile, cmsSigBlueTRCTag));
+
+ default:
+
+ return FALSE;
+ }
+}
+
+// Returns TRUE if the intent is implemented as CLUT
+cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, int UsedDirection)
+{
+ const cmsTagSignature* TagTable;
+
+ // For devicelinks, the supported intent is that one stated in the header
+ if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {
+ return (cmsGetHeaderRenderingIntent(hProfile) == Intent);
+ }
+
+ switch (UsedDirection) {
+
+ case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;
+ case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;
+
+ // For proofing, we need rel. colorimetric in output. Let's do some recursion
+ case LCMS_USED_AS_PROOF:
+ return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&
+ cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);
+
+ default:
+ cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);
+ return FALSE;
+ }
+
+ return cmsIsTag(hProfile, TagTable[Intent]);
+
+}
+
+
+// Return info about supported intents
+cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,
+ cmsUInt32Number Intent, int UsedDirection)
+{
+
+ if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;
+
+ // Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper
+ // does not fully support relative colorimetric because they cannot deal with non-zero black points, but
+ // many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter
+ // the accuracy would be less than optimal in rel.col and v2 case.
+
+ return cmsIsMatrixShaper(hProfile);
+}
+
+
+// ---------------------------------------------------------------------------------------------------------------
+
+// Read both, profile sequence description and profile sequence id if present. Then combine both to
+// create qa unique structure holding both. Shame on ICC to store things in such complicated way.
+
+cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)
+{
+ cmsSEQ* ProfileSeq;
+ cmsSEQ* ProfileId;
+ cmsSEQ* NewSeq;
+ cmsUInt32Number i;
+
+ // Take profile sequence description first
+ ProfileSeq = cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);
+
+ // Take profile sequence ID
+ ProfileId = cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);
+
+ if (ProfileSeq == NULL && ProfileId == NULL) return NULL;
+
+ if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);
+ if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);
+
+ // We have to mix both together. For that they must agree
+ if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);
+
+ NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);
+
+ // Ok, proceed to the mixing
+ for (i=0; i < ProfileSeq ->n; i++) {
+
+ memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));
+ NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);
+ }
+
+ return NewSeq;
+}
+
+// Dump the contents of profile sequence in both tags (if v4 available)
+cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)
+{
+ if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;
+
+ if (cmsGetProfileVersion(hProfile) >= 4.0) {
+
+ if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+// Auxiliar, read and duplicate a MLU if found.
+static
+cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)
+{
+ cmsMLU* mlu = cmsReadTag(h, sig);
+ if (mlu == NULL) return NULL;
+
+ return cmsMLUdup(mlu);
+}
+
+// Create a sequence description out of an array of profiles
+cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])
+{
+ cmsUInt32Number i;
+ cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);
+
+ if (seq == NULL) return NULL;
+
+ for (i=0; i < nProfiles; i++) {
+
+ cmsPSEQDESC* ps = &seq ->seq[i];
+ cmsHPROFILE h = hProfiles[i];
+ cmsTechnologySignature* techpt;
+
+ cmsGetHeaderAttributes(h, &ps ->attributes);
+ cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);
+ ps ->deviceMfg = cmsGetHeaderManufacturer(h);
+ ps ->deviceModel = cmsGetHeaderModel(h);
+
+ techpt = cmsReadTag(h, cmsSigTechnologyTag);
+ if (techpt == NULL)
+ ps ->technology = (cmsTechnologySignature) 0;
+ else
+ ps ->technology = *techpt;
+
+ ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);
+ ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);
+ ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);
+
+ }
+
+ return seq;
+}
+
+// -------------------------------------------------------------------------------------------------------------------
+
+
+static
+const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)
+{
+ cmsTagSignature sig;
+
+ switch (Info) {
+
+ case cmsInfoDescription:
+ sig = cmsSigProfileDescriptionTag;
+ break;
+
+ case cmsInfoManufacturer:
+ sig = cmsSigDeviceMfgDescTag;
+ break;
+
+ case cmsInfoModel:
+ sig = cmsSigDeviceModelDescTag;
+ break;
+
+ case cmsInfoCopyright:
+ sig = cmsSigCopyrightTag;
+ break;
+
+ default: return NULL;
+ }
+
+
+ return (cmsMLU*) cmsReadTag(hProfile, sig);
+}
+
+
+cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ wchar_t* Buffer, cmsUInt32Number BufferSize)
+{
+ const cmsMLU* mlu = GetInfo(hProfile, Info);
+ if (mlu == NULL) return 0;
+
+ return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
+}
+
+
+cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,
+ const char LanguageCode[3], const char CountryCode[3],
+ char* Buffer, cmsUInt32Number BufferSize)
+{
+ const cmsMLU* mlu = GetInfo(hProfile, Info);
+ if (mlu == NULL) return 0;
+
+ return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);
+}