Moved the ICC TRC patching code for RTv2 profiles from ImProcFunctions::lab2rgbOut to ICCStore

See #5026
This commit is contained in:
Alberto Griggio
2018-11-25 14:23:06 +01:00
parent 23aa0562aa
commit cfc947a865
2 changed files with 125 additions and 80 deletions

View File

@@ -303,82 +303,7 @@ Imagefloat* ImProcFunctions::lab2rgbOut(LabImage* lab, int cx, int cy, int cw, i
}
Imagefloat* image = new Imagefloat(cw, ch);
cmsHPROFILE oprof = nullptr;
oprof = ICCStore::getInstance()->getProfile(icm.outputProfile);
Glib::ustring outtest = icm.outputProfile;
std::string fileis_RTv2 = outtest.substr(0, 4);
//printf("IsRTv2=%s\n", fileis_RTv2.c_str());
if(fileis_RTv2 == "RTv2") {//Only fot ICC v2 : read tag from desc to retrieve gamma and slope save before in generate ICC v2
//due to bug in LCMS in CmsToneCurve
//printf("icmout=%s \n",icm.output.c_str());
GammaValues g_b; //gamma parameters
const double eps = 0.000000001; // not divide by zero
double gammatag = 2.4;
double slopetag = 12.92310;
cmsMLU *modelDescMLU = (cmsMLU*) (cmsReadTag(oprof, cmsSigDeviceModelDescTag));
if (modelDescMLU) {
cmsUInt32Number count = cmsMLUgetWide(modelDescMLU, "en", "US", nullptr, 0); // get buffer length first
if (count) {
wchar_t *buffer = new wchar_t[count];
count = cmsMLUgetWide(modelDescMLU, "en", "US", buffer, count); // now put the string in the buffer
Glib::ustring modelDesc;
#if __SIZEOF_WCHAR_T__ == 2
char* cModelDesc = g_utf16_to_utf8((unsigned short int*)buffer, -1, nullptr, nullptr, nullptr); // convert to utf-8 in a buffer allocated by glib
if (cModelDesc) {
modelDesc.assign(cModelDesc);
g_free(cModelDesc);
}
#else
modelDesc = utf32_to_utf8(buffer, count);
#endif
delete [] buffer;
if (!modelDesc.empty()) {
std::size_t pos = modelDesc.find("g");
std::size_t posmid = modelDesc.find("s");
std::size_t posend = modelDesc.find("!");
std::string strgamma = modelDesc.substr(pos + 1, (posmid - pos));
gammatag = std::stod(strgamma.c_str());
std::string strslope = modelDesc.substr(posmid + 1, (posend - posmid));
slopetag = std::stod(strslope.c_str());
// printf("gam=%f slo=%f\n", gammatag, slopetag);
}
} else {
printf("Error: lab2rgbOut / String length is null!\n");
}
} else {
printf("Error: lab2rgbOut / cmsReadTag/cmsSigDeviceModelDescTag failed!\n");
}
double pwr = 1.0 / gammatag;
double ts = slopetag;
double slope = slopetag == 0 ? eps : slopetag;
int mode = 0;
Color::calcGamma(pwr, ts, mode, g_b); // call to calcGamma with selected gamma and slope : return parameters for LCMS2
cmsFloat64Number gammaParams[7]; //gamma parameters
gammaParams[4] = g_b[3] * ts;
gammaParams[0] = gammatag;
gammaParams[1] = 1. / (1.0 + g_b[4]);
gammaParams[2] = g_b[4] / (1.0 + g_b[4]);
gammaParams[3] = 1. / slope;
gammaParams[5] = 0.0;
gammaParams[6] = 0.0;
cmsToneCurve* GammaTRC[3];
if(slopetag == 0.) {
//printf("gammatag=%f\n", gammatag);
GammaTRC[0] = GammaTRC[1] = GammaTRC[2] = cmsBuildGamma(NULL, gammatag);
}
else {
GammaTRC[0] = GammaTRC[1] = GammaTRC[2] = cmsBuildParametricToneCurve(nullptr, 5, gammaParams); //5 = smoother than 4
}
cmsWriteTag(oprof, cmsSigRedTRCTag, GammaTRC[0]);
cmsWriteTag(oprof, cmsSigGreenTRCTag, GammaTRC[1]);
cmsWriteTag(oprof, cmsSigBlueTRCTag, GammaTRC[2]);
cmsFreeToneCurve(GammaTRC[0]);
}
cmsHPROFILE oprof = ICCStore::getInstance()->getProfile(icm.outputProfile);
if (oprof) {
cmsUInt32Number flags = cmsFLAGS_NOOPTIMIZE | cmsFLAGS_NOCACHE;