Path: blob/master/src/java.desktop/share/native/liblcms/cmsio1.c
41149 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324// This file is available under and governed by the GNU General Public25// License version 2 only, as published by the Free Software Foundation.26// However, the following notice accompanied the original version of this27// file:28//29//---------------------------------------------------------------------------------30//31// Little Color Management System32// Copyright (c) 1998-2020 Marti Maria Saguer33//34// Permission is hereby granted, free of charge, to any person obtaining35// a copy of this software and associated documentation files (the "Software"),36// to deal in the Software without restriction, including without limitation37// the rights to use, copy, modify, merge, publish, distribute, sublicense,38// and/or sell copies of the Software, and to permit persons to whom the Software39// is furnished to do so, subject to the following conditions:40//41// The above copyright notice and this permission notice shall be included in42// all copies or substantial portions of the Software.43//44// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,45// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO46// THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND47// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE48// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION49// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION50// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.51//52//---------------------------------------------------------------------------------53//5455#include "lcms2_internal.h"5657// Read tags using low-level functions, provides necessary glue code to adapt versions, etc.5859// LUT tags60static const cmsTagSignature Device2PCS16[] = {cmsSigAToB0Tag, // Perceptual61cmsSigAToB1Tag, // Relative colorimetric62cmsSigAToB2Tag, // Saturation63cmsSigAToB1Tag }; // Absolute colorimetric6465static const cmsTagSignature Device2PCSFloat[] = {cmsSigDToB0Tag, // Perceptual66cmsSigDToB1Tag, // Relative colorimetric67cmsSigDToB2Tag, // Saturation68cmsSigDToB3Tag }; // Absolute colorimetric6970static const cmsTagSignature PCS2Device16[] = {cmsSigBToA0Tag, // Perceptual71cmsSigBToA1Tag, // Relative colorimetric72cmsSigBToA2Tag, // Saturation73cmsSigBToA1Tag }; // Absolute colorimetric7475static const cmsTagSignature PCS2DeviceFloat[] = {cmsSigBToD0Tag, // Perceptual76cmsSigBToD1Tag, // Relative colorimetric77cmsSigBToD2Tag, // Saturation78cmsSigBToD3Tag }; // Absolute colorimetric798081// Factors to convert from 1.15 fixed point to 0..1.0 range and vice-versa82#define InpAdj (1.0/MAX_ENCODEABLE_XYZ) // (65536.0/(65535.0*2.0))83#define OutpAdj (MAX_ENCODEABLE_XYZ) // ((2.0*65535.0)/65536.0)8485// Several resources for gray conversions.86static const cmsFloat64Number GrayInputMatrix[] = { (InpAdj*cmsD50X), (InpAdj*cmsD50Y), (InpAdj*cmsD50Z) };87static const cmsFloat64Number OneToThreeInputMatrix[] = { 1, 1, 1 };88static const cmsFloat64Number PickYMatrix[] = { 0, (OutpAdj*cmsD50Y), 0 };89static const cmsFloat64Number PickLstarMatrix[] = { 1, 0, 0 };9091// Get a media white point fixing some issues found in certain old profiles92cmsBool _cmsReadMediaWhitePoint(cmsCIEXYZ* Dest, cmsHPROFILE hProfile)93{94cmsCIEXYZ* Tag;9596_cmsAssert(Dest != NULL);9798Tag = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);99100// If no wp, take D50101if (Tag == NULL) {102*Dest = *cmsD50_XYZ();103return TRUE;104}105106// V2 display profiles should give D50107if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {108109if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {110*Dest = *cmsD50_XYZ();111return TRUE;112}113}114115// All seems ok116*Dest = *Tag;117return TRUE;118}119120121// Chromatic adaptation matrix. Fix some issues as well122cmsBool _cmsReadCHAD(cmsMAT3* Dest, cmsHPROFILE hProfile)123{124cmsMAT3* Tag;125126_cmsAssert(Dest != NULL);127128Tag = (cmsMAT3*) cmsReadTag(hProfile, cmsSigChromaticAdaptationTag);129130if (Tag != NULL) {131*Dest = *Tag;132return TRUE;133}134135// No CHAD available, default it to identity136_cmsMAT3identity(Dest);137138// V2 display profiles should give D50139if (cmsGetEncodedICCversion(hProfile) < 0x4000000) {140141if (cmsGetDeviceClass(hProfile) == cmsSigDisplayClass) {142143cmsCIEXYZ* White = (cmsCIEXYZ*) cmsReadTag(hProfile, cmsSigMediaWhitePointTag);144145if (White == NULL) {146147_cmsMAT3identity(Dest);148return TRUE;149}150151return _cmsAdaptationMatrix(Dest, NULL, White, cmsD50_XYZ());152}153}154155return TRUE;156}157158159// Auxiliary, read colorants as a MAT3 structure. Used by any function that needs a matrix-shaper160static161cmsBool ReadICCMatrixRGB2XYZ(cmsMAT3* r, cmsHPROFILE hProfile)162{163cmsCIEXYZ *PtrRed, *PtrGreen, *PtrBlue;164165_cmsAssert(r != NULL);166167PtrRed = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigRedColorantTag);168PtrGreen = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigGreenColorantTag);169PtrBlue = (cmsCIEXYZ *) cmsReadTag(hProfile, cmsSigBlueColorantTag);170171if (PtrRed == NULL || PtrGreen == NULL || PtrBlue == NULL)172return FALSE;173174_cmsVEC3init(&r -> v[0], PtrRed -> X, PtrGreen -> X, PtrBlue -> X);175_cmsVEC3init(&r -> v[1], PtrRed -> Y, PtrGreen -> Y, PtrBlue -> Y);176_cmsVEC3init(&r -> v[2], PtrRed -> Z, PtrGreen -> Z, PtrBlue -> Z);177178return TRUE;179}180181182// Gray input pipeline183static184cmsPipeline* BuildGrayInputMatrixPipeline(cmsHPROFILE hProfile)185{186cmsToneCurve *GrayTRC;187cmsPipeline* Lut;188cmsContext ContextID = cmsGetProfileContextID(hProfile);189190GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);191if (GrayTRC == NULL) return NULL;192193Lut = cmsPipelineAlloc(ContextID, 1, 3);194if (Lut == NULL)195goto Error;196197if (cmsGetPCS(hProfile) == cmsSigLabData) {198199// In this case we implement the profile as an identity matrix plus 3 tone curves200cmsUInt16Number Zero[2] = { 0x8080, 0x8080 };201cmsToneCurve* EmptyTab;202cmsToneCurve* LabCurves[3];203204EmptyTab = cmsBuildTabulatedToneCurve16(ContextID, 2, Zero);205206if (EmptyTab == NULL)207goto Error;208209LabCurves[0] = GrayTRC;210LabCurves[1] = EmptyTab;211LabCurves[2] = EmptyTab;212213if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, OneToThreeInputMatrix, NULL)) ||214!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, LabCurves))) {215cmsFreeToneCurve(EmptyTab);216goto Error;217}218219cmsFreeToneCurve(EmptyTab);220221}222else {223224if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &GrayTRC)) ||225!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 1, GrayInputMatrix, NULL)))226goto Error;227}228229return Lut;230231Error:232cmsPipelineFree(Lut);233return NULL;234}235236// RGB Matrix shaper237static238cmsPipeline* BuildRGBInputMatrixShaper(cmsHPROFILE hProfile)239{240cmsPipeline* Lut;241cmsMAT3 Mat;242cmsToneCurve *Shapes[3];243cmsContext ContextID = cmsGetProfileContextID(hProfile);244int i, j;245246if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile)) return NULL;247248// XYZ PCS in encoded in 1.15 format, and the matrix output comes in 0..0xffff range, so249// we need to adjust the output by a factor of (0x10000/0xffff) to put data in250// a 1.16 range, and then a >> 1 to obtain 1.15. The total factor is (65536.0)/(65535.0*2)251252for (i=0; i < 3; i++)253for (j=0; j < 3; j++)254Mat.v[i].n[j] *= InpAdj;255256257Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);258Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);259Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);260261if (!Shapes[0] || !Shapes[1] || !Shapes[2])262return NULL;263264Lut = cmsPipelineAlloc(ContextID, 3, 3);265if (Lut != NULL) {266267if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, Shapes)) ||268!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Mat, NULL)))269goto Error;270271// Note that it is certainly possible a single profile would have a LUT based272// tag for output working in lab and a matrix-shaper for the fallback cases.273// This is not allowed by the spec, but this code is tolerant to those cases274if (cmsGetPCS(hProfile) == cmsSigLabData) {275276if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocXYZ2Lab(ContextID)))277goto Error;278}279280}281282return Lut;283284Error:285cmsPipelineFree(Lut);286return NULL;287}288289290291// Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded292static293cmsPipeline* _cmsReadFloatInputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)294{295cmsContext ContextID = cmsGetProfileContextID(hProfile);296cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));297cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);298cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);299300if (Lut == NULL) return NULL;301302// input and output of transform are in lcms 0..1 encoding. If XYZ or Lab spaces are used,303// these need to be normalized into the appropriate ranges (Lab = 100,0,0, XYZ=1.0,1.0,1.0)304if ( spc == cmsSigLabData)305{306if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))307goto Error;308}309else if (spc == cmsSigXYZData)310{311if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))312goto Error;313}314315if ( PCS == cmsSigLabData)316{317if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))318goto Error;319}320else if( PCS == cmsSigXYZData)321{322if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))323goto Error;324}325326return Lut;327328Error:329cmsPipelineFree(Lut);330return NULL;331}332333334// Read and create a BRAND NEW MPE LUT from a given profile. All stuff dependent of version, etc335// is adjusted here in order to create a LUT that takes care of all those details.336// We add intent = 0xffffffff as a way to read matrix shaper always, no matter of other LUT337cmsPipeline* CMSEXPORT _cmsReadInputLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)338{339cmsTagTypeSignature OriginalType;340cmsTagSignature tag16;341cmsTagSignature tagFloat;342cmsContext ContextID = cmsGetProfileContextID(hProfile);343344// On named color, take the appropriate tag345if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {346347cmsPipeline* Lut;348cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*) cmsReadTag(hProfile, cmsSigNamedColor2Tag);349350if (nc == NULL) return NULL;351352Lut = cmsPipelineAlloc(ContextID, 0, 0);353if (Lut == NULL) {354cmsFreeNamedColorList(nc);355return NULL;356}357358if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, TRUE)) ||359!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID))) {360cmsPipelineFree(Lut);361return NULL;362}363return Lut;364}365366// This is an attempt to reuse this function to retrieve the matrix-shaper as pipeline no367// matter other LUT are present and have precedence. Intent = 0xffffffff can be used for that.368if (Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {369370tag16 = Device2PCS16[Intent];371tagFloat = Device2PCSFloat[Intent];372373if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence374375// Floating point LUT are always V4, but the encoding range is no376// longer 0..1.0, so we need to add an stage depending on the color space377return _cmsReadFloatInputTag(hProfile, tagFloat);378}379380// Revert to perceptual if no tag is found381if (!cmsIsTag(hProfile, tag16)) {382tag16 = Device2PCS16[0];383}384385if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?386387// Check profile version and LUT type. Do the necessary adjustments if needed388389// First read the tag390cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);391if (Lut == NULL) return NULL;392393// After reading it, we have now info about the original type394OriginalType = _cmsGetTagTrueType(hProfile, tag16);395396// The profile owns the Lut, so we need to copy it397Lut = cmsPipelineDup(Lut);398399// We need to adjust data only for Lab16 on output400if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)401return Lut;402403// If the input is Lab, add also a conversion at the begin404if (cmsGetColorSpace(hProfile) == cmsSigLabData &&405!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))406goto Error;407408// Add a matrix for conversion V2 to V4 Lab PCS409if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))410goto Error;411412return Lut;413Error:414cmsPipelineFree(Lut);415return NULL;416}417}418419// Lut was not found, try to create a matrix-shaper420421// Check if this is a grayscale profile.422if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {423424// if so, build appropriate conversion tables.425// The tables are the PCS iluminant, scaled across GrayTRC426return BuildGrayInputMatrixPipeline(hProfile);427}428429// Not gray, create a normal matrix-shaper430return BuildRGBInputMatrixShaper(hProfile);431}432433// ---------------------------------------------------------------------------------------------------------------434435// Gray output pipeline.436// XYZ -> Gray or Lab -> Gray. Since we only know the GrayTRC, we need to do some assumptions. Gray component will be437// given by Y on XYZ PCS and by L* on Lab PCS, Both across inverse TRC curve.438// The complete pipeline on XYZ is Matrix[3:1] -> Tone curve and in Lab Matrix[3:1] -> Tone Curve as well.439440static441cmsPipeline* BuildGrayOutputPipeline(cmsHPROFILE hProfile)442{443cmsToneCurve *GrayTRC, *RevGrayTRC;444cmsPipeline* Lut;445cmsContext ContextID = cmsGetProfileContextID(hProfile);446447GrayTRC = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGrayTRCTag);448if (GrayTRC == NULL) return NULL;449450RevGrayTRC = cmsReverseToneCurve(GrayTRC);451if (RevGrayTRC == NULL) return NULL;452453Lut = cmsPipelineAlloc(ContextID, 3, 1);454if (Lut == NULL) {455cmsFreeToneCurve(RevGrayTRC);456return NULL;457}458459if (cmsGetPCS(hProfile) == cmsSigLabData) {460461if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickLstarMatrix, NULL)))462goto Error;463}464else {465if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 1, 3, PickYMatrix, NULL)))466goto Error;467}468469if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 1, &RevGrayTRC)))470goto Error;471472cmsFreeToneCurve(RevGrayTRC);473return Lut;474475Error:476cmsFreeToneCurve(RevGrayTRC);477cmsPipelineFree(Lut);478return NULL;479}480481482static483cmsPipeline* BuildRGBOutputMatrixShaper(cmsHPROFILE hProfile)484{485cmsPipeline* Lut;486cmsToneCurve *Shapes[3], *InvShapes[3];487cmsMAT3 Mat, Inv;488int i, j;489cmsContext ContextID = cmsGetProfileContextID(hProfile);490491if (!ReadICCMatrixRGB2XYZ(&Mat, hProfile))492return NULL;493494if (!_cmsMAT3inverse(&Mat, &Inv))495return NULL;496497// XYZ PCS in encoded in 1.15 format, and the matrix input should come in 0..0xffff range, so498// we need to adjust the input by a << 1 to obtain a 1.16 fixed and then by a factor of499// (0xffff/0x10000) to put data in 0..0xffff range. Total factor is (2.0*65535.0)/65536.0;500501for (i=0; i < 3; i++)502for (j=0; j < 3; j++)503Inv.v[i].n[j] *= OutpAdj;504505Shapes[0] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigRedTRCTag);506Shapes[1] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigGreenTRCTag);507Shapes[2] = (cmsToneCurve *) cmsReadTag(hProfile, cmsSigBlueTRCTag);508509if (!Shapes[0] || !Shapes[1] || !Shapes[2])510return NULL;511512InvShapes[0] = cmsReverseToneCurve(Shapes[0]);513InvShapes[1] = cmsReverseToneCurve(Shapes[1]);514InvShapes[2] = cmsReverseToneCurve(Shapes[2]);515516if (!InvShapes[0] || !InvShapes[1] || !InvShapes[2]) {517return NULL;518}519520Lut = cmsPipelineAlloc(ContextID, 3, 3);521if (Lut != NULL) {522523// Note that it is certainly possible a single profile would have a LUT based524// tag for output working in lab and a matrix-shaper for the fallback cases.525// This is not allowed by the spec, but this code is tolerant to those cases526if (cmsGetPCS(hProfile) == cmsSigLabData) {527528if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLab2XYZ(ContextID)))529goto Error;530}531532if (!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocMatrix(ContextID, 3, 3, (cmsFloat64Number*) &Inv, NULL)) ||533!cmsPipelineInsertStage(Lut, cmsAT_END, cmsStageAllocToneCurves(ContextID, 3, InvShapes)))534goto Error;535}536537cmsFreeToneCurveTriple(InvShapes);538return Lut;539Error:540cmsFreeToneCurveTriple(InvShapes);541cmsPipelineFree(Lut);542return NULL;543}544545546// Change CLUT interpolation to trilinear547static548void ChangeInterpolationToTrilinear(cmsPipeline* Lut)549{550cmsStage* Stage;551552for (Stage = cmsPipelineGetPtrToFirstStage(Lut);553Stage != NULL;554Stage = cmsStageNext(Stage)) {555556if (cmsStageType(Stage) == cmsSigCLutElemType) {557558_cmsStageCLutData* CLUT = (_cmsStageCLutData*) Stage ->Data;559560CLUT ->Params->dwFlags |= CMS_LERP_FLAGS_TRILINEAR;561_cmsSetInterpolationRoutine(Lut->ContextID, CLUT ->Params);562}563}564}565566567// Read the DToAX tag, adjusting the encoding of Lab or XYZ if neded568static569cmsPipeline* _cmsReadFloatOutputTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)570{571cmsContext ContextID = cmsGetProfileContextID(hProfile);572cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*) cmsReadTag(hProfile, tagFloat));573cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);574cmsColorSpaceSignature dataSpace = cmsGetColorSpace(hProfile);575576if (Lut == NULL) return NULL;577578// If PCS is Lab or XYZ, the floating point tag is accepting data in the space encoding,579// and since the formatter has already accommodated to 0..1.0, we should undo this change580if ( PCS == cmsSigLabData)581{582if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))583goto Error;584}585else586if (PCS == cmsSigXYZData)587{588if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))589goto Error;590}591592// the output can be Lab or XYZ, in which case normalisation is needed on the end of the pipeline593if ( dataSpace == cmsSigLabData)594{595if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))596goto Error;597}598else if (dataSpace == cmsSigXYZData)599{600if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))601goto Error;602}603604return Lut;605606Error:607cmsPipelineFree(Lut);608return NULL;609}610611// Create an output MPE LUT from agiven profile. Version mismatches are handled here612cmsPipeline* CMSEXPORT _cmsReadOutputLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)613{614cmsTagTypeSignature OriginalType;615cmsTagSignature tag16;616cmsTagSignature tagFloat;617cmsContext ContextID = cmsGetProfileContextID(hProfile);618619620if (Intent <= INTENT_ABSOLUTE_COLORIMETRIC) {621622tag16 = PCS2Device16[Intent];623tagFloat = PCS2DeviceFloat[Intent];624625if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence626627// Floating point LUT are always V4628return _cmsReadFloatOutputTag(hProfile, tagFloat);629}630631// Revert to perceptual if no tag is found632if (!cmsIsTag(hProfile, tag16)) {633tag16 = PCS2Device16[0];634}635636if (cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?637638// Check profile version and LUT type. Do the necessary adjustments if needed639640// First read the tag641cmsPipeline* Lut = (cmsPipeline*) cmsReadTag(hProfile, tag16);642if (Lut == NULL) return NULL;643644// After reading it, we have info about the original type645OriginalType = _cmsGetTagTrueType(hProfile, tag16);646647// The profile owns the Lut, so we need to copy it648Lut = cmsPipelineDup(Lut);649if (Lut == NULL) return NULL;650651// Now it is time for a controversial stuff. I found that for 3D LUTS using652// Lab used as indexer space, trilinear interpolation should be used653if (cmsGetPCS(hProfile) == cmsSigLabData)654ChangeInterpolationToTrilinear(Lut);655656// We need to adjust data only for Lab and Lut16 type657if (OriginalType != cmsSigLut16Type || cmsGetPCS(hProfile) != cmsSigLabData)658return Lut;659660// Add a matrix for conversion V4 to V2 Lab PCS661if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))662goto Error;663664// If the output is Lab, add also a conversion at the end665if (cmsGetColorSpace(hProfile) == cmsSigLabData)666if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))667goto Error;668669return Lut;670Error:671cmsPipelineFree(Lut);672return NULL;673}674}675676// Lut not found, try to create a matrix-shaper677678// Check if this is a grayscale profile.679if (cmsGetColorSpace(hProfile) == cmsSigGrayData) {680681// if so, build appropriate conversion tables.682// The tables are the PCS iluminant, scaled across GrayTRC683return BuildGrayOutputPipeline(hProfile);684}685686// Not gray, create a normal matrix-shaper, which only operates in XYZ space687return BuildRGBOutputMatrixShaper(hProfile);688}689690// ---------------------------------------------------------------------------------------------------------------691692// Read the AToD0 tag, adjusting the encoding of Lab or XYZ if neded693static694cmsPipeline* _cmsReadFloatDevicelinkTag(cmsHPROFILE hProfile, cmsTagSignature tagFloat)695{696cmsContext ContextID = cmsGetProfileContextID(hProfile);697cmsPipeline* Lut = cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));698cmsColorSpaceSignature PCS = cmsGetPCS(hProfile);699cmsColorSpaceSignature spc = cmsGetColorSpace(hProfile);700701if (Lut == NULL) return NULL;702703if (spc == cmsSigLabData)704{705if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToLabFloat(ContextID)))706goto Error;707}708else709if (spc == cmsSigXYZData)710{711if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageNormalizeToXyzFloat(ContextID)))712goto Error;713}714715if (PCS == cmsSigLabData)716{717if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromLabFloat(ContextID)))718goto Error;719}720else721if (PCS == cmsSigXYZData)722{723if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageNormalizeFromXyzFloat(ContextID)))724goto Error;725}726727return Lut;728Error:729cmsPipelineFree(Lut);730return NULL;731}732733// This one includes abstract profiles as well. Matrix-shaper cannot be obtained on that device class. The734// tag name here may default to AToB0735cmsPipeline* CMSEXPORT _cmsReadDevicelinkLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent)736{737cmsPipeline* Lut;738cmsTagTypeSignature OriginalType;739cmsTagSignature tag16;740cmsTagSignature tagFloat;741cmsContext ContextID = cmsGetProfileContextID(hProfile);742743744if (Intent > INTENT_ABSOLUTE_COLORIMETRIC)745return NULL;746747tag16 = Device2PCS16[Intent];748tagFloat = Device2PCSFloat[Intent];749750// On named color, take the appropriate tag751if (cmsGetDeviceClass(hProfile) == cmsSigNamedColorClass) {752753cmsNAMEDCOLORLIST* nc = (cmsNAMEDCOLORLIST*)cmsReadTag(hProfile, cmsSigNamedColor2Tag);754755if (nc == NULL) return NULL;756757Lut = cmsPipelineAlloc(ContextID, 0, 0);758if (Lut == NULL)759goto Error;760761if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocNamedColor(nc, FALSE)))762goto Error;763764if (cmsGetColorSpace(hProfile) == cmsSigLabData)765if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))766goto Error;767768return Lut;769Error:770cmsPipelineFree(Lut);771cmsFreeNamedColorList(nc);772return NULL;773}774775776if (cmsIsTag(hProfile, tagFloat)) { // Float tag takes precedence777778// Floating point LUT are always V779return _cmsReadFloatDevicelinkTag(hProfile, tagFloat);780}781782tagFloat = Device2PCSFloat[0];783if (cmsIsTag(hProfile, tagFloat)) {784785return cmsPipelineDup((cmsPipeline*)cmsReadTag(hProfile, tagFloat));786}787788if (!cmsIsTag(hProfile, tag16)) { // Is there any LUT-Based table?789790tag16 = Device2PCS16[0];791if (!cmsIsTag(hProfile, tag16)) return NULL;792}793794// Check profile version and LUT type. Do the necessary adjustments if needed795796// Read the tag797Lut = (cmsPipeline*)cmsReadTag(hProfile, tag16);798if (Lut == NULL) return NULL;799800// The profile owns the Lut, so we need to copy it801Lut = cmsPipelineDup(Lut);802if (Lut == NULL) return NULL;803804// Now it is time for a controversial stuff. I found that for 3D LUTS using805// Lab used as indexer space, trilinear interpolation should be used806if (cmsGetPCS(hProfile) == cmsSigLabData)807ChangeInterpolationToTrilinear(Lut);808809// After reading it, we have info about the original type810OriginalType = _cmsGetTagTrueType(hProfile, tag16);811812// We need to adjust data for Lab16 on output813if (OriginalType != cmsSigLut16Type) return Lut;814815// Here it is possible to get Lab on both sides816817if (cmsGetColorSpace(hProfile) == cmsSigLabData) {818if (!cmsPipelineInsertStage(Lut, cmsAT_BEGIN, _cmsStageAllocLabV4ToV2(ContextID)))819goto Error2;820}821822if (cmsGetPCS(hProfile) == cmsSigLabData) {823if (!cmsPipelineInsertStage(Lut, cmsAT_END, _cmsStageAllocLabV2ToV4(ContextID)))824goto Error2;825}826827return Lut;828829Error2:830cmsPipelineFree(Lut);831return NULL;832}833834// ---------------------------------------------------------------------------------------------------------------835836// Returns TRUE if the profile is implemented as matrix-shaper837cmsBool CMSEXPORT cmsIsMatrixShaper(cmsHPROFILE hProfile)838{839switch (cmsGetColorSpace(hProfile)) {840841case cmsSigGrayData:842843return cmsIsTag(hProfile, cmsSigGrayTRCTag);844845case cmsSigRgbData:846847return (cmsIsTag(hProfile, cmsSigRedColorantTag) &&848cmsIsTag(hProfile, cmsSigGreenColorantTag) &&849cmsIsTag(hProfile, cmsSigBlueColorantTag) &&850cmsIsTag(hProfile, cmsSigRedTRCTag) &&851cmsIsTag(hProfile, cmsSigGreenTRCTag) &&852cmsIsTag(hProfile, cmsSigBlueTRCTag));853854default:855856return FALSE;857}858}859860// Returns TRUE if the intent is implemented as CLUT861cmsBool CMSEXPORT cmsIsCLUT(cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)862{863const cmsTagSignature* TagTable;864865// For devicelinks, the supported intent is that one stated in the header866if (cmsGetDeviceClass(hProfile) == cmsSigLinkClass) {867return (cmsGetHeaderRenderingIntent(hProfile) == Intent);868}869870switch (UsedDirection) {871872case LCMS_USED_AS_INPUT: TagTable = Device2PCS16; break;873case LCMS_USED_AS_OUTPUT:TagTable = PCS2Device16; break;874875// For proofing, we need rel. colorimetric in output. Let's do some recursion876case LCMS_USED_AS_PROOF:877return cmsIsIntentSupported(hProfile, Intent, LCMS_USED_AS_INPUT) &&878cmsIsIntentSupported(hProfile, INTENT_RELATIVE_COLORIMETRIC, LCMS_USED_AS_OUTPUT);879880default:881cmsSignalError(cmsGetProfileContextID(hProfile), cmsERROR_RANGE, "Unexpected direction (%d)", UsedDirection);882return FALSE;883}884885return cmsIsTag(hProfile, TagTable[Intent]);886887}888889890// Return info about supported intents891cmsBool CMSEXPORT cmsIsIntentSupported(cmsHPROFILE hProfile,892cmsUInt32Number Intent, cmsUInt32Number UsedDirection)893{894895if (cmsIsCLUT(hProfile, Intent, UsedDirection)) return TRUE;896897// Is there any matrix-shaper? If so, the intent is supported. This is a bit odd, since V2 matrix shaper898// does not fully support relative colorimetric because they cannot deal with non-zero black points, but899// many profiles claims that, and this is certainly not true for V4 profiles. Lets answer "yes" no matter900// the accuracy would be less than optimal in rel.col and v2 case.901902return cmsIsMatrixShaper(hProfile);903}904905906// ---------------------------------------------------------------------------------------------------------------907908// Read both, profile sequence description and profile sequence id if present. Then combine both to909// create qa unique structure holding both. Shame on ICC to store things in such complicated way.910cmsSEQ* _cmsReadProfileSequence(cmsHPROFILE hProfile)911{912cmsSEQ* ProfileSeq;913cmsSEQ* ProfileId;914cmsSEQ* NewSeq;915cmsUInt32Number i;916917// Take profile sequence description first918ProfileSeq = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceDescTag);919920// Take profile sequence ID921ProfileId = (cmsSEQ*) cmsReadTag(hProfile, cmsSigProfileSequenceIdTag);922923if (ProfileSeq == NULL && ProfileId == NULL) return NULL;924925if (ProfileSeq == NULL) return cmsDupProfileSequenceDescription(ProfileId);926if (ProfileId == NULL) return cmsDupProfileSequenceDescription(ProfileSeq);927928// We have to mix both together. For that they must agree929if (ProfileSeq ->n != ProfileId ->n) return cmsDupProfileSequenceDescription(ProfileSeq);930931NewSeq = cmsDupProfileSequenceDescription(ProfileSeq);932933// Ok, proceed to the mixing934if (NewSeq != NULL) {935for (i=0; i < ProfileSeq ->n; i++) {936937memmove(&NewSeq ->seq[i].ProfileID, &ProfileId ->seq[i].ProfileID, sizeof(cmsProfileID));938NewSeq ->seq[i].Description = cmsMLUdup(ProfileId ->seq[i].Description);939}940}941return NewSeq;942}943944// Dump the contents of profile sequence in both tags (if v4 available)945cmsBool _cmsWriteProfileSequence(cmsHPROFILE hProfile, const cmsSEQ* seq)946{947if (!cmsWriteTag(hProfile, cmsSigProfileSequenceDescTag, seq)) return FALSE;948949if (cmsGetEncodedICCversion(hProfile) >= 0x4000000) {950951if (!cmsWriteTag(hProfile, cmsSigProfileSequenceIdTag, seq)) return FALSE;952}953954return TRUE;955}956957958// Auxiliary, read and duplicate a MLU if found.959static960cmsMLU* GetMLUFromProfile(cmsHPROFILE h, cmsTagSignature sig)961{962cmsMLU* mlu = (cmsMLU*) cmsReadTag(h, sig);963if (mlu == NULL) return NULL;964965return cmsMLUdup(mlu);966}967968// Create a sequence description out of an array of profiles969cmsSEQ* _cmsCompileProfileSequence(cmsContext ContextID, cmsUInt32Number nProfiles, cmsHPROFILE hProfiles[])970{971cmsUInt32Number i;972cmsSEQ* seq = cmsAllocProfileSequenceDescription(ContextID, nProfiles);973974if (seq == NULL) return NULL;975976for (i=0; i < nProfiles; i++) {977978cmsPSEQDESC* ps = &seq ->seq[i];979cmsHPROFILE h = hProfiles[i];980cmsTechnologySignature* techpt;981982cmsGetHeaderAttributes(h, &ps ->attributes);983cmsGetHeaderProfileID(h, ps ->ProfileID.ID8);984ps ->deviceMfg = cmsGetHeaderManufacturer(h);985ps ->deviceModel = cmsGetHeaderModel(h);986987techpt = (cmsTechnologySignature*) cmsReadTag(h, cmsSigTechnologyTag);988if (techpt == NULL)989ps ->technology = (cmsTechnologySignature) 0;990else991ps ->technology = *techpt;992993ps ->Manufacturer = GetMLUFromProfile(h, cmsSigDeviceMfgDescTag);994ps ->Model = GetMLUFromProfile(h, cmsSigDeviceModelDescTag);995ps ->Description = GetMLUFromProfile(h, cmsSigProfileDescriptionTag);996997}998999return seq;1000}10011002// -------------------------------------------------------------------------------------------------------------------100310041005static1006const cmsMLU* GetInfo(cmsHPROFILE hProfile, cmsInfoType Info)1007{1008cmsTagSignature sig;10091010switch (Info) {10111012case cmsInfoDescription:1013sig = cmsSigProfileDescriptionTag;1014break;10151016case cmsInfoManufacturer:1017sig = cmsSigDeviceMfgDescTag;1018break;10191020case cmsInfoModel:1021sig = cmsSigDeviceModelDescTag;1022break;10231024case cmsInfoCopyright:1025sig = cmsSigCopyrightTag;1026break;10271028default: return NULL;1029}103010311032return (cmsMLU*) cmsReadTag(hProfile, sig);1033}1034103510361037cmsUInt32Number CMSEXPORT cmsGetProfileInfo(cmsHPROFILE hProfile, cmsInfoType Info,1038const char LanguageCode[3], const char CountryCode[3],1039wchar_t* Buffer, cmsUInt32Number BufferSize)1040{1041const cmsMLU* mlu = GetInfo(hProfile, Info);1042if (mlu == NULL) return 0;10431044return cmsMLUgetWide(mlu, LanguageCode, CountryCode, Buffer, BufferSize);1045}104610471048cmsUInt32Number CMSEXPORT cmsGetProfileInfoASCII(cmsHPROFILE hProfile, cmsInfoType Info,1049const char LanguageCode[3], const char CountryCode[3],1050char* Buffer, cmsUInt32Number BufferSize)1051{1052const cmsMLU* mlu = GetInfo(hProfile, Info);1053if (mlu == NULL) return 0;10541055return cmsMLUgetASCII(mlu, LanguageCode, CountryCode, Buffer, BufferSize);1056}105710581059