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 AToB and BToA tags for the generated pq profile.
Wolthera van Hövell tot Westerflier1906c3999e0b
diff --git a/plugins/color/lcms2engine/CMakeLists.txt b/plugins/color/lcms2engine/CMakeLists.txtindex a38638d5ee..9c3b5b255f 100644--- a/plugins/color/lcms2engine/CMakeLists.txt+++ b/plugins/color/lcms2engine/CMakeLists.txt@@ -63,6 +63,7 @@ set ( lcmsengine_SRCScolorprofiles/LcmsColorProfileContainer.cppcolorprofiles/IccColorProfile.cpp+ colorprofiles/LcmsPredefinedPipelineFunctions.cppIccColorSpaceEngine.cppLcmsColorSpace.cppLcmsEnginePlugin.cppdiff --git a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cppindex 1d5f2fd3d8..ad50d90a52 100644--- a/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp+++ b/plugins/color/lcms2engine/colorprofiles/IccColorProfile.cpp@@ -21,6 +21,7 @@#include <kis_assert.h>#include "LcmsColorProfileContainer.h"+#include "LcmsPredefinedPipelineFunctions.h"#include <KisLazyStorage.h>#include <KisLazyValueWrapper.h>@@ -171,6 +172,19 @@ IccColorProfile::IccColorProfile(const QVector<double> &colorants,cmsCIEXYZ media_blackpoint = {0.0, 0.0, 0.0};cmsWriteTag (iccProfile, cmsSigMediaBlackPointTag, &media_blackpoint);+ if (transferFunction == TRC_ITU_R_BT_2100_0_PQ) {+ LcmsPredefinedPipelineFunctions::setPerceptualQuantizerAToBDummyPipeline(iccProfile, cmsSigAToB0Tag, colorPrimariesType);+ LcmsPredefinedPipelineFunctions::setPerceptualQuantizerBToADummyPipeline(iccProfile, cmsSigBToA0Tag, colorPrimariesType);+ // Otherwise this is recognised as a matrix shaper...+ cmsWriteTag(iccProfile, cmsSigRedColorantTag, nullptr);+ cmsWriteTag(iccProfile, cmsSigGreenColorantTag, nullptr);+ cmsWriteTag(iccProfile, cmsSigBlueColorantTag, nullptr);+ cmsWriteTag(iccProfile, cmsSigRedTRCTag, nullptr);+ cmsWriteTag(iccProfile, cmsSigGreenTRCTag, nullptr);+ cmsWriteTag(iccProfile, cmsSigBlueTRCTag, nullptr);++ }+if (colorPrimariesType < 256 && transferFunction < 256) {cmsVideoSignalType cicpValues;cicpValues.ColourPrimaries = quint8(colorPrimariesType);diff --git a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cppnew file mode 100644index 0000000000..78577231ac--- /dev/null+++ b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.cpp@@ -0,0 +1,170 @@+/*+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: LGPL-2.1-or-later+ */+#include "LcmsPredefinedPipelineFunctions.h"+#include <KoColorTransferFunctions.h>+#include <KoColorProfile.h>+#include <QTransform>+#include <QGenericMatrix>++/**+ * @brief bradfordMatrix+ * From http://brucelindbloom.com/index.html?Eqn_ChromAdapt.html+ * @param src -- source white point XYZ+ * @param dst -- destionation white point XYZ+ * @return chromatic adaptation matrix.+ */+static QTransform bradfordMatrix(double *src, double *dst) {+ double bradford[] = {+ 0.8951000, 0.2664000, -0.1614000,+ -0.7502000, 1.7135000, 0.0367000,+ 0.0389000, -0.0685000, 1.0296000+ };++ QGenericMatrix<1, 3, double> srcLMS = QGenericMatrix<3, 3, double>(bradford) * QGenericMatrix<1, 3, double>(src);+ QGenericMatrix<1, 3, double> dstLMS = QGenericMatrix<3, 3, double>(bradford) * QGenericMatrix<1, 3, double>(dst);++ QTransform adaptation (+ dstLMS(0, 0)/srcLMS(0, 0), 0, 0,+ 0, dstLMS(1, 0)/srcLMS(1, 0), 0,+ 0, 0, dstLMS(2, 0)/srcLMS(2, 0)+ );++ QTransform bradfordInv (+ 0.9869929, -0.1470543, 0.1599627,+ 0.4323053, 0.5183603, 0.0492912,+ -0.0085287, 0.0400428, 0.9684867+ );++ return bradfordInv * adaptation * bradfordInv.inverted();+}+/**+ * @brief rgbMatrix+ * Generates a proper rgb matrix from a primaries type.+ * @param primaries -- default primaries to use.+ * @param factor -- scaling factor.+ * @return transfrom representing a XYZ to RGB conversion.+ */+static QTransform rgbMatrix(ColorPrimaries primaries, double factor = 1.0) {+ const double mul = 1.0/factor;+ // Calculation: http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html+ // Calculates XYZ with Y = 1.0+ QVector<double> col;+ KoColorProfile::colorantsForType(primaries, col);+ QTransform initialXYZMatrix(+ col[2]/col[3], col[4]/col[5], col[6]/col[7],+ 1.0, 1.0, 1.0,+ (1.0-col[2]-col[3])/col[3], (1.0-col[4]-col[5])/col[5], (1.0-col[6]-col[7])/col[7]+ );+ QTransform invTf = initialXYZMatrix.inverted();++ // Calculate scaling factor with white point.+ double wp [] = {+ col[0]/col[1], 1.0, (1.0-col[0]-col[1])/col[1]+ };+ double inv[] = {+ invTf.m11(), invTf.m12(), invTf.m13(),+ invTf.m21(), invTf.m22(), invTf.m23(),+ invTf.m31(), invTf.m32(), invTf.m33()+ };++ QGenericMatrix<1, 3, double> S = QGenericMatrix<3, 3, double>(inv) * QGenericMatrix<1, 3, double>(wp);++ // Apply scaling factor to normalize the Y and get the matrix.+ QTransform finalXYZMatrix(+ initialXYZMatrix.m11() * S(0, 0), initialXYZMatrix.m12() * S(1, 0), initialXYZMatrix.m13() * S(2, 0),+ initialXYZMatrix.m21() * S(0, 0), initialXYZMatrix.m22() * S(1, 0), initialXYZMatrix.m23() * S(2, 0),+ initialXYZMatrix.m31() * S(0, 0), initialXYZMatrix.m32() * S(1, 0), initialXYZMatrix.m33() * S(2, 0)+ );+ // Apply bradford white point adaptation.+ double wpD50 [] = {+ 0.9642, 1.0, 0.8249+ };+ finalXYZMatrix = bradfordMatrix(wp, wpD50) * finalXYZMatrix;++ return QTransform(+ finalXYZMatrix.m11() * mul, finalXYZMatrix.m12() * mul, finalXYZMatrix.m13() * mul,+ finalXYZMatrix.m21() * mul, finalXYZMatrix.m22() * mul, finalXYZMatrix.m23() * mul,+ finalXYZMatrix.m31() * mul, finalXYZMatrix.m32() * mul, finalXYZMatrix.m33() * mul+ );+}+++bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerAToBDummyPipeline(cmsHPROFILE iccProfile, cmsTagSignature tag, ColorPrimaries primaries)+{+ // From profile space to XYZ.+ const double factor = 2.0;++++ // linear curves, obligatory for A2B with both matrix and clut: linear-clut-curves-matrix-linear.+ cmsPipeline *a2B = cmsPipelineAlloc(nullptr, 3, 3);++ // close to "PQ" curve+ cmsToneCurve *curve[3];+ cmsFloat64Number para[] = {5.0, factor, 0};+ curve[0] = curve[1] = curve[2] = cmsBuildParametricToneCurve(nullptr, 2, para);+ cmsStage *curveStage = cmsStageAllocToneCurves(nullptr, 3, curve);+ cmsPipelineInsertStage(a2B, cmsAT_END, curveStage);++ // matrix++ QTransform tf = rgbMatrix(primaries, factor);+ double m[] = {+ tf.m11(), tf.m12(), tf.m13(),+ tf.m21(), tf.m22(), tf.m23(),+ tf.m31(), tf.m32(), tf.m33()+ };++ cmsStage* matrixStage = cmsStageAllocMatrix(nullptr, 3, 3, m, nullptr);+ 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);++ bool success = cmsWriteTag(iccProfile, tag, a2B);+ cmsFreeToneCurve(curve[0]);+ cmsFreeToneCurve(linearCurves[0]);+ cmsPipelineFree(a2B);+ return success;+}++bool LcmsPredefinedPipelineFunctions::setPerceptualQuantizerBToADummyPipeline(cmsHPROFILE iccProfile, cmsTagSignature tag, ColorPrimaries primaries)+{+ // From XYZ to profile space.+ cmsPipeline *b2A = cmsPipelineAlloc(nullptr, 3, 3);++ // curves, linear+ cmsToneCurve *linearCurves[3];+ linearCurves[0] = linearCurves[1] = linearCurves[2] = cmsBuildGamma(nullptr, 1.0);+ cmsStage *lCurveStage = cmsStageAllocToneCurves(nullptr, 3, linearCurves);+ cmsPipelineInsertStage(b2A, cmsAT_END, lCurveStage);++ // matrix+ QTransform tf = rgbMatrix(primaries, 2.0).inverted();+ double m[] = {+ tf.m11(), tf.m12(), tf.m13(),+ tf.m21(), tf.m22(), tf.m23(),+ tf.m31(), tf.m32(), tf.m33()+ };++ cmsStage* matrixStage = cmsStageAllocMatrix(nullptr, 3, 3, m, nullptr);+ 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);+ cmsStage *curveStage = cmsStageAllocToneCurves(nullptr, 3, curve);+ cmsPipelineInsertStage(b2A, cmsAT_END, curveStage);++ bool success = cmsWriteTag(iccProfile, tag, b2A);+ cmsFreeToneCurve(curve[0]);+ cmsFreeToneCurve(linearCurves[0]);+ cmsPipelineFree(b2A);+ return success;+}diff --git a/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.h b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.hnew file mode 100644index 0000000000..1c07d290cb--- /dev/null+++ b/plugins/color/lcms2engine/colorprofiles/LcmsPredefinedPipelineFunctions.h@@ -0,0 +1,35 @@+/*+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>+ *+ * SPDX-License-Identifier: LGPL-2.1-or-later+ */+#ifndef LCMSPREDFINEDPIPELINEFUNCTIONS_H+#define LCMSPREDFINEDPIPELINEFUNCTIONS_H++#include <lcms2.h>+#include <KoColorProfileConstants.h>++/**+ * @brief The LcmsPredfinedPipelineFunctions class+ * This class defines static functions to help create+ * ICC profiles with LCMS.+ *+ *+ */+class LcmsPredefinedPipelineFunctions+{+public:++/******************+ * Rec2100 PQ dummy functions.+ * PQ cannot be represented correctly in ICC due scaling issues. Therefore, we+ * create a pipeline that's good enough to represent the data in a pinch,+ * but it is combined with cicp values in the profile.+ *****************/++ static bool setPerceptualQuantizerAToBDummyPipeline(cmsHPROFILE iccProfile, cmsTagSignature tag, ColorPrimaries primaries);+ static bool setPerceptualQuantizerBToADummyPipeline(cmsHPROFILE iccProfile, cmsTagSignature tag, ColorPrimaries primaries);++};++#endif // LCMSPREDEFINEDPIPELINEFUNCTIONS_H