Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Implement a CLUT for the PQ dummy profile that lets it be interpreted as HLG.

Wolthera van Hövell tot Westerflier7dbb719cbb64
diff --git a/libs/pigment/KoColorTransferFunctions.h b/libs/pigment/KoColorTransferFunctions.h
index b64a7a967d..0390a0c3f8 100644
--- a/libs/pigment/KoColorTransferFunctions.h
+++ b/libs/pigment/KoColorTransferFunctions.h
@@ -113,7 +113,7 @@ ALWAYS_INLINE float applyHLGCurve(float x) noexcept
return (a * logf(12.0f * x - b) + c);
} else {
// return (sqrt(3.0) * powf(x, 0.5));
- return (sqrtf(3.0f) * sqrtf(x));
+ return (sqrtf(3.0f * x));
}
}
@@ -124,7 +124,7 @@ ALWAYS_INLINE float removeHLGCurve(float x) noexcept
const float c = 0.55991073f;
if (x <= 0.5f) {
// return (powf(x, 2.0) / 3.0);
- return x * x * (1.f / 3.0f);
+ return (x * x) / 3.0f;
} else {
return (expf((x - c) * (1.f / a)) + b) * (1.f / 12.0f);
}
diff --git a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp
index 78577231ac..e1bee5cd75 100644
--- a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp
+++ b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp
@@ -91,6 +91,34 @@ static QTransform rgbMatrix(ColorPrimaries primaries, double factor = 1.0) {
);
}
+struct perceptualDummyHelper {
+ bool toXYZ{false};
+ int channels {3};
+ };
+
+/**
+ * @brief samplePQDummyClut
+ * In this sampler, we convert the PQ data to linear and then to HLG. The benefit of HLG
+ * is that it sort of allows for tonemapping.
+ */
+cmsInt32Number samplePQDummyClut(const cmsUInt16Number In[], cmsUInt16Number Out[], void *Cargo) {
+ struct perceptualDummyHelper *helper = (struct perceptualDummyHelper *) Cargo;
+ const float scale = 3.7743;
+ for (int i = 0; i < helper->channels; i++) {
+ const float val = float(In[i])/65535.0;
+
+ cmsUInt16Number finalResult = In[i];
+ if (!helper->toXYZ) {
+ const float lin = removeSmpte2048Curve(val);
+ finalResult = qBound(0, qRound( applyHLGCurve(lin / scale) * 65535 ), 65535);
+ } else {
+ const float lin = removeHLGCurve(val) * scale;
+ finalResult = qBound(0, qRound( applySmpte2048Curve(lin) * 65535 ), 65535);
+ }
+ Out[i] = finalResult;
+ }
+ return true;
+};
bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerAToBDummyPipeline(cmsHPROFILE iccProfile, cmsTagSignature tag, ColorPrimaries primaries)
{
@@ -102,10 +130,19 @@ bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerAToBDummyPipeline(cm
// linear curves, obligatory for A2B with both matrix and clut: linear-clut-curves-matrix-linear.
cmsPipeline *a2B = cmsPipelineAlloc(nullptr, 3, 3);
+ cmsToneCurve *linearCurves[3];
+ linearCurves[0] = linearCurves[1] = linearCurves[2] = cmsBuildGamma(nullptr, 1.0);
+ cmsStage *lCurveStage = cmsStageAllocToneCurves(nullptr, 3, linearCurves);
+ cmsPipelineInsertStage(a2B, cmsAT_END, lCurveStage);
+
+ cmsStage *clutStage = cmsStageAllocCLut16bit(nullptr, 8, 3, 3, nullptr);
+ perceptualDummyHelper helper;
+ cmsStageSampleCLut16bit(clutStage, &samplePQDummyClut, &helper, 0);
+ cmsPipelineInsertStage(a2B, cmsAT_END, clutStage);
+
// close to "PQ" curve
cmsToneCurve *curve[3];
- cmsFloat64Number para[] = {5.0, factor, 0};
- curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(nullptr, 2, para);
+ curve[0] = curve[1] = curve[2] = cmsBuildGamma(nullptr, 2.2);
cmsStage *curveStage = cmsStageAllocToneCurves(nullptr, 3, curve);
cmsPipelineInsertStage(a2B, cmsAT_END, curveStage);
@@ -122,10 +159,7 @@ bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerAToBDummyPipeline(cm
cmsPipelineInsertStage(a2B, cmsAT_END, matrixStage);
// curves, linear
- cmsToneCurve *linearCurves[3];
- linearCurves[0] = linearCurves[1] = linearCurves[2] = cmsBuildGamma(nullptr, 1.0);
- cmsStage *lCurveStage = cmsStageAllocToneCurves(nullptr, 3, linearCurves);
- cmsPipelineInsertStage(a2B, cmsAT_END, lCurveStage);
+ cmsPipelineInsertStage(a2B, cmsAT_END, cmsStageDup(lCurveStage));
bool success = cmsWriteTag(iccProfile, tag, a2B);
cmsFreeToneCurve(curve[0]);
@@ -157,11 +191,18 @@ bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerBToADummyPipeline(cm
cmsPipelineInsertStage(b2A, cmsAT_END, matrixStage);
// pq curve
cmsToneCurve *curve[3];
- cmsFloat64Number para[] = {1/5.0, 0.5, 0};
- curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(nullptr, 2, para);
+ curve[0] = curve[1] = curve[2] = cmsBuildGamma(nullptr, 1/2.2);
cmsStage *curveStage = cmsStageAllocToneCurves(nullptr, 3, curve);
cmsPipelineInsertStage(b2A, cmsAT_END, curveStage);
+ cmsStage *clutStage = cmsStageAllocCLut16bit(nullptr, 8, 3, 3, nullptr);
+ perceptualDummyHelper helper;
+ helper.toXYZ = false;
+ cmsStageSampleCLut16bit(clutStage, &samplePQDummyClut, &helper, 0);
+ cmsPipelineInsertStage(b2A, cmsAT_END, clutStage);
+
+ cmsPipelineInsertStage(b2A, cmsAT_END, cmsStageDup(lCurveStage));
+
bool success = cmsWriteTag(iccProfile, tag, b2A);
cmsFreeToneCurve(curve[0]);
cmsFreeToneCurve(linearCurves[0]);