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++
Add scaling factor into HLG algorithms, as per bt 2390.
Wolthera van Hövell tot Westerfliere0aa4e0b135e
diff --git a/libs/pigment/KoColorTransferFunctions.h b/libs/pigment/KoColorTransferFunctions.hindex 351caedbb5..9cb16a29af 100644--- a/libs/pigment/KoColorTransferFunctions.h+++ b/libs/pigment/KoColorTransferFunctions.h@@ -80,7 +80,7 @@ ALWAYS_INLINE void applyHLGOOTF(float *rgb,const float luma = (rgb[0] * static_cast<float>(lumaCoefficients[0]))+ (rgb[1] * static_cast<float>(lumaCoefficients[1]))+ (rgb[2] * static_cast<float>(lumaCoefficients[2]));- const float a = powf(luma, (1.0-gamma)/gamma);+ const float a = powf(luma, gamma);rgb[0] *= a;rgb[1] *= a;rgb[2] *= a;@@ -97,7 +97,7 @@ ALWAYS_INLINE void removeHLGOOTF(float *rgb,const float luma = (rgb[0] * static_cast<float>(lumaCoefficients[0]))+ (rgb[1] * static_cast<float>(lumaCoefficients[1]))+ (rgb[2] * static_cast<float>(lumaCoefficients[2]));- const float multiplier = powf(luma, gamma-1.f) ;+ const float multiplier = powf(luma, 1.0 / gamma) ;rgb[0] *= multiplier;rgb[1] *= multiplier;rgb[2] *= multiplier;@@ -109,11 +109,14 @@ ALWAYS_INLINE float applyHLGCurve(float x) noexceptconst float b = 0.28466892f;const float c = 0.55991073f;+ /// This scales everything so that diffuse white is at 75~%,+ /// as per BT.2408, pg 4-5, and BT. 2390 pg. 52-53.+ const float scale = (1.0 / 3.7743);+if (x > 1.0f / 12.0f) {- return (a * logf(12.0f * x - b) + c);+ return (a * logf(12.0f * (x*scale) - b) + c) ;} else {- // return (sqrt(3.0) * powf(x, 0.5));- return (sqrtf(3.0f * x));+ return (sqrtf(3.0f * (x * scale)));}}@@ -122,11 +125,14 @@ ALWAYS_INLINE float removeHLGCurve(float x) noexceptconst float a = 0.17883277f;const float b = 0.28466892f;const float c = 0.55991073f;++ const float scale = 3.7743;+if (x <= 0.5f) {// return (powf(x, 2.0) / 3.0);- return (x * x) / 3.0f;+ return ((x * x) / 3.0f) * scale;} else {- return (expf((x - c) * (1.f / a)) + b) * (1.f / 12.0f);+ return ((expf((x - c) * (1.f / a)) + b) * (1.f / 12.0f)) * scale;}}@@ -171,10 +177,12 @@ struct KoColorTransferFunctions {constexpr float b = 0.28466892f;constexpr float c = 0.55991073f;+ constexpr float scale = 3.7743;+const float_v x1 = x * x * (1.f / 3.0f);const float_v x2 =(xsimd::exp((x - c) * (1.f / a)) + b) * (1.f / 12.0f);- x = xsimd::select(x <= float_v(0.5f), x1, x2);+ x = xsimd::select(x <= float_v(0.5f), x1, x2) * scale;}static ALWAYS_INLINE void removeSMPTE_ST_428Curve(float_v &x) noexceptdiff --git a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cppindex 768d45a87d..f14e581e28 100644--- a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp+++ b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp@@ -105,9 +105,8 @@ struct perceptualDummyHelper {*/cmsInt32Number samplePQDummyClut(const cmsUInt16Number In[], cmsUInt16Number Out[], void *Cargo) {struct perceptualDummyHelper *helper = (struct perceptualDummyHelper *) Cargo;- const float scale = 3.7743; /// This scales everything so that diffuse white is at 75~%.const float pqScale = 125.0; /// Important: this normalizes the pq signal.- const float nominalPeak = 10000.0/1000.0;/// HLG goes up to 1000, so we're scaling the pq down to that.+ const float nominalPeak = 10000.0/392.0;/// See bt. 2390 pg. 52.double coeff[] = {0.2126, 0.7152, 0.0722};if (helper->luma) {coeff[0] = helper->luma[0];@@ -120,20 +119,13 @@ cmsInt32Number samplePQDummyClut(const cmsUInt16Number In[], cmsUInt16Number Outif (helper->toXYZ) {lin[i] = (removeSmpte2048Curve(val) / pqScale) * nominalPeak;} else {- lin[i] = removeHLGCurve(val) * scale;+ lin[i] = removeHLGCurve(val);}}- if (helper->toXYZ) {- // display to scene- applyHLGOOTF(lin, coeff);- } else {- // scene to display- removeHLGOOTF(lin, coeff);- }for (int i = 0; i < helper->channels; i++) {cmsUInt16Number finalResult = In[i];if (helper->toXYZ) {- finalResult = qBound(0, qRound( applyHLGCurve(lin[i] / scale) * 65535 ), 65535);+ finalResult = qBound(0, qRound( applyHLGCurve(lin[i]) * 65535 ), 65535);} else {finalResult = qBound(0, qRound( applySmpte2048Curve((lin[i] / nominalPeak) * pqScale) * 65535 ), 65535);}