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 kritasurfacecolormanagementapi library
Dmitry Kazakovb225c0f7c5d3
diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txtindex aa7af642ea..9bd2684829 100644--- a/libs/CMakeLists.txt+++ b/libs/CMakeLists.txt@@ -23,3 +23,7 @@ add_subdirectory( metadata )add_subdirectory( resourcewidgets )add_subdirectory( psd )add_subdirectory( multiarch )++if (NOT ANDROID AND HAVE_WAYLAND)+ add_subdirectory( surfacecolormanagementapi )+endif()diff --git a/libs/surfacecolormanagementapi/CMakeLists.txt b/libs/surfacecolormanagementapi/CMakeLists.txtnew file mode 100644index 0000000000..052f40e888--- /dev/null+++ b/libs/surfacecolormanagementapi/CMakeLists.txt@@ -0,0 +1,21 @@+set(kritasurfacecolormanagementapi_LIB_SRCS+ KisColorimetryUtils.cpp+ surfacecolormanagement/KisOutputColorInfoInterface.cpp+ surfacecolormanagement/KisOutputColorInfoInterface.h+ surfacecolormanagement/KisSurfaceColorimetry.cpp+ surfacecolormanagement/KisSurfaceColorimetry.h+ surfacecolormanagement/KisSurfaceColorManagerInterface.cpp+ surfacecolormanagement/KisSurfaceColorManagerInterface.h+)++kis_add_library(kritasurfacecolormanagementapi SHARED ${kritasurfacecolormanagementapi_LIB_SRCS})+generate_export_header(kritasurfacecolormanagementapi BASE_NAME kritasurfacecolormanagementapi)++target_link_libraries(kritasurfacecolormanagementapi kritaglobal ${QT_QTCORE_LIBRARY})++set_target_properties(kritasurfacecolormanagementapi PROPERTIES+ VERSION ${GENERIC_KRITA_LIB_VERSION} SOVERSION ${GENERIC_KRITA_LIB_SOVERSION}+)++install(TARGETS kritasurfacecolormanagementapi ${INSTALL_TARGETS_DEFAULT_ARGS})+diff --git a/libs/surfacecolormanagementapi/KisColorimetryUtils.cpp b/libs/surfacecolormanagementapi/KisColorimetryUtils.cppnew file mode 100644index 0000000000..86b648b261--- /dev/null+++ b/libs/surfacecolormanagementapi/KisColorimetryUtils.cpp@@ -0,0 +1,455 @@+/*+ * SPDX-FileCopyrightText: 2023 Xaver Hugl <[email protected]>+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisColorimetryUtils.h"++#include <QDebug>++namespace KisColorimetryUtils+{++QMatrix4x4 matrixFromColumns(const QVector3D &first, const QVector3D &second, const QVector3D &third)+{+ QMatrix4x4 ret;+ ret(0, 0) = first.x();+ ret(1, 0) = first.y();+ ret(2, 0) = first.z();+ ret(0, 1) = second.x();+ ret(1, 1) = second.y();+ ret(2, 1) = second.z();+ ret(0, 2) = third.x();+ ret(1, 2) = third.y();+ ret(2, 2) = third.z();+ return ret;+}++XYZ xy::toXYZ() const+{+ if (qFuzzyIsNull(y)) {+ return XYZ{0, 0, 0};+ }+ return XYZ{+ .X = x / y,+ .Y = 1.0,+ .Z = (1 - x - y) / y,+ };+}++QVector2D xy::asVector() const+{+ return QVector2D(x, y);+}++bool xy::operator==(const xy &other) const+{+ return qFuzzyCompare(x, other.x) && qFuzzyCompare(y, other.y);+}++XYZ xyY::toXYZ() const+{+ if (qFuzzyIsNull(y)) {+ return XYZ{0, 0, 0};+ }+ return XYZ{+ .X = Y * x / y,+ .Y = Y,+ .Z = Y * (1 - x - y) / y,+ };+}++bool xyY::operator==(const xyY &other) const+{+ return qFuzzyCompare(x, other.x) && qFuzzyCompare(y, other.y) && qFuzzyCompare(Y, other.Y);+}++xyY XYZ::toxyY() const+{+ const double sum = X + Y + Z;+ if (qFuzzyIsNull(sum)) {+ // this is nonsense, but at least won't crash+ return xyY{+ .x = 0,+ .y = 0,+ .Y = 1,+ };+ }+ return xyY{+ .x = X / sum,+ .y = Y / sum,+ .Y = Y,+ };+}++xy XYZ::toxy() const+{+ const double sum = X + Y + Z;+ if (qFuzzyIsNull(sum)) {+ // this is nonsense, but at least won't crash+ return xy{+ .x = 0,+ .y = 0,+ };+ }+ return xy{+ .x = X / sum,+ .y = Y / sum,+ };+}++XYZ XYZ::operator*(double factor) const+{+ return XYZ{+ .X = X * factor,+ .Y = Y * factor,+ .Z = Z * factor,+ };+}++XYZ XYZ::operator/(double divisor) const+{+ return XYZ{+ .X = X / divisor,+ .Y = Y / divisor,+ .Z = Z / divisor,+ };+}++XYZ XYZ::operator+(const XYZ &other) const+{+ return XYZ{+ .X = X + other.X,+ .Y = Y + other.Y,+ .Z = Z + other.Z,+ };+}++QVector3D XYZ::asVector() const+{+ return QVector3D(X, Y, Z);+}++XYZ XYZ::fromVector(const QVector3D &vector)+{+ return XYZ{+ .X = vector.x(),+ .Y = vector.y(),+ .Z = vector.z(),+ };+}++bool XYZ::operator==(const XYZ &other) const+{+ return qFuzzyCompare(X, other.X) && qFuzzyCompare(Y, other.Y) && qFuzzyCompare(Z, other.Z);+}++QMatrix4x4 Colorimetry::chromaticAdaptationMatrix(XYZ sourceWhitepoint, XYZ destinationWhitepoint)+{+ static const QMatrix4x4 bradford = []() {+ QMatrix4x4 ret;+ ret(0, 0) = 0.8951;+ ret(0, 1) = 0.2664;+ ret(0, 2) = -0.1614;+ ret(1, 0) = -0.7502;+ ret(1, 1) = 1.7135;+ ret(1, 2) = 0.0367;+ ret(2, 0) = 0.0389;+ ret(2, 1) = -0.0685;+ ret(2, 2) = 1.0296;+ return ret;+ }();+ static const QMatrix4x4 inverseBradford = []() {+ QMatrix4x4 ret;+ ret(0, 0) = 0.9869929;+ ret(0, 1) = -0.1470543;+ ret(0, 2) = 0.1599627;+ ret(1, 0) = 0.4323053;+ ret(1, 1) = 0.5183603;+ ret(1, 2) = 0.0492912;+ ret(2, 0) = -0.0085287;+ ret(2, 1) = 0.0400428;+ ret(2, 2) = 0.9684867;+ return ret;+ }();+ if (sourceWhitepoint == destinationWhitepoint) {+ return QMatrix4x4{};+ }+ const QVector3D factors = (bradford.map(destinationWhitepoint.asVector())) / (bradford.map(sourceWhitepoint.asVector()));+ QMatrix4x4 adaptation{};+ adaptation(0, 0) = factors.x();+ adaptation(1, 1) = factors.y();+ adaptation(2, 2) = factors.z();+ return inverseBradford * adaptation * bradford;+}++QMatrix4x4 Colorimetry::calculateToXYZMatrix(XYZ red, XYZ green, XYZ blue, XYZ white)+{+ const QVector3D r = red.asVector();+ const QVector3D g = green.asVector();+ const QVector3D b = blue.asVector();+ const auto component_scale = (matrixFromColumns(r, g, b)).inverted().map(white.asVector());+ return matrixFromColumns(r * component_scale.x(), g * component_scale.y(), b * component_scale.z());+}++Colorimetry Colorimetry::interpolateGamutTo(const Colorimetry &one, double factor) const+{+ return Colorimetry{+ m_red * (1 - factor) + one.red() * factor,+ m_green * (1 - factor) + one.green() * factor,+ m_blue * (1 - factor) + one.blue() * factor,+ m_white, // whitepoint should stay the same+ };+}++static double triangleArea(QVector2D p1, QVector2D p2, QVector2D p3)+{+ return std::abs(0.5 * (p1.x() * (p2.y() - p3.y()) + p2.x() * (p3.y() - p1.y()) + p3.x() * (p1.y() - p2.y())));+}++bool Colorimetry::isValid(xy red, xy green, xy blue, xy white)+{+ // this is more of a heuristic than a hard rule+ // but if the gamut is too small, it's not really usable+ const double gamutArea = triangleArea(red.asVector(), green.asVector(), blue.asVector());+ if (gamutArea < 0.02) {+ return false;+ }+ // if the white point is inside the gamut triangle,+ // the three triangles made up between the primaries and the whitepoint+ // must have the same area as the gamut triangle+ const double area1 = triangleArea(white.asVector(), green.asVector(), blue.asVector());+ const double area2 = triangleArea(red.asVector(), white.asVector(), blue.asVector());+ const double area3 = triangleArea(red.asVector(), green.asVector(), white.asVector());+ if (std::abs(area1 + area2 + area3 - gamutArea) > 0.001) {+ // this would cause terrible glitches+ return false;+ }+ return true;+}++bool Colorimetry::isReal(xy red, xy green, xy blue, xy white)+{+ if (!isValid(red, green, blue, white)) {+ return false;+ }+ // outside of XYZ definitely can't be shown on a display+ // TODO maybe calculate if all values are within the human-visible gamut too?+ if (red.x < 0 || red.x > 1 || red.y < 0 || red.y > 1 || green.x < 0 || green.x > 1 || green.y < 0 || green.y > 1 || blue.x < 0 || blue.x > 1 || blue.y < 0+ || blue.y > 1 || white.x < 0 || white.x > 1 || white.y < 0 || white.y > 1) {+ return false;+ }+ return true;+}++Colorimetry::Colorimetry(XYZ red, XYZ green, XYZ blue, XYZ white)+ : m_red(red)+ , m_green(green)+ , m_blue(blue)+ , m_white(white)+ , m_toXYZ(calculateToXYZMatrix(red, green, blue, white))+ , m_fromXYZ(m_toXYZ.inverted())+{+}++Colorimetry::Colorimetry(xyY red, xyY green, xyY blue, xyY white)+ : Colorimetry(red.toXYZ(), green.toXYZ(), blue.toXYZ(), white.toXYZ())+{+}++Colorimetry::Colorimetry(xy red, xy green, xy blue, xy white)+ : m_white(xyY{white.x, white.y, 1.0}.toXYZ())+{+ const auto brightness = (matrixFromColumns(xyY{red.x, red.y, 1.0}.toXYZ().asVector(),+ xyY{green.x, green.y, 1.0}.toXYZ().asVector(),+ xyY{blue.x, blue.y, 1.0}.toXYZ().asVector()))+ .inverted().map(+ xyY{white.x, white.y, 1.0}.toXYZ().asVector());+ m_red = xyY{red.x, red.y, brightness.x()}.toXYZ();+ m_green = xyY{green.x, green.y, brightness.y()}.toXYZ();+ m_blue = xyY{blue.x, blue.y, brightness.z()}.toXYZ();+ m_toXYZ = calculateToXYZMatrix(m_red, m_green, m_blue, m_white);+ m_fromXYZ = m_toXYZ.inverted();+}++const QMatrix4x4 &Colorimetry::toXYZ() const+{+ return m_toXYZ;+}++const QMatrix4x4 &Colorimetry::fromXYZ() const+{+ return m_fromXYZ;+}++// converts from XYZ to LMS suitable for ICtCp+static const QMatrix4x4 s_xyzToDolbyLMS = []() {+ QMatrix4x4 ret;+ ret(0, 0) = 0.3593;+ ret(0, 1) = 0.6976;+ ret(0, 2) = -0.0359;+ ret(1, 0) = -0.1921;+ ret(1, 1) = 1.1005;+ ret(1, 2) = 0.0754;+ ret(2, 0) = 0.0071;+ ret(2, 1) = 0.0748;+ ret(2, 2) = 0.8433;+ return ret;+}();+static const QMatrix4x4 s_inverseDolbyLMS = s_xyzToDolbyLMS.inverted();++QMatrix4x4 Colorimetry::toLMS() const+{+ return s_xyzToDolbyLMS * m_toXYZ;+}++QMatrix4x4 Colorimetry::fromLMS() const+{+ return m_fromXYZ * s_inverseDolbyLMS;+}++Colorimetry Colorimetry::adaptedTo(xyY newWhitepoint) const+{+ const auto mat = chromaticAdaptationMatrix(this->white(), newWhitepoint.toXYZ());+ return Colorimetry{+ XYZ::fromVector(mat.map(red().asVector())),+ XYZ::fromVector(mat.map(green().asVector())),+ XYZ::fromVector(mat.map(blue().asVector())),+ newWhitepoint.toXYZ(),+ };+}++Colorimetry Colorimetry::withWhitepoint(xyY newWhitePoint) const+{+ newWhitePoint.Y = 1;+ return Colorimetry{+ m_red,+ m_green,+ m_blue,+ newWhitePoint.toXYZ(),+ };+}++QMatrix4x4 Colorimetry::relativeColorimetricTo(const Colorimetry &other) const+{+ return other.fromXYZ() * chromaticAdaptationMatrix(white(), other.white()) * toXYZ();+}++QMatrix4x4 Colorimetry::absoluteColorimetricTo(const Colorimetry &other) const+{+ return other.fromXYZ() * toXYZ();+}++bool Colorimetry::operator==(const Colorimetry &other) const+{+ return red() == other.red() && green() == other.green() && blue() == other.blue() && white() == other.white();+}++const XYZ &Colorimetry::red() const+{+ return m_red;+}++const XYZ &Colorimetry::green() const+{+ return m_green;+}++const XYZ &Colorimetry::blue() const+{+ return m_blue;+}++const XYZ &Colorimetry::white() const+{+ return m_white;+}++const Colorimetry Colorimetry::BT709 = Colorimetry{+ xy{0.64, 0.33},+ xy{0.30, 0.60},+ xy{0.15, 0.06},+ xy{0.3127, 0.3290},+};+const Colorimetry Colorimetry::PAL_M = Colorimetry{+ xy{0.67, 0.33},+ xy{0.21, 0.71},+ xy{0.14, 0.08},+ xy{0.310, 0.316},+};+const Colorimetry Colorimetry::PAL = Colorimetry{+ xy{0.640, 0.330},+ xy{0.290, 0.600},+ xy{0.150, 0.060},+ xy{0.3127, 0.3290},+};+const Colorimetry Colorimetry::NTSC = Colorimetry{+ xy{0.630, 0.340},+ xy{0.310, 0.595},+ xy{0.155, 0.070},+ xy{0.3127, 0.3290},+};+const Colorimetry Colorimetry::GenericFilm = Colorimetry{+ xy{0.681, 0.319},+ xy{0.243, 0.692},+ xy{0.145, 0.049},+ xy{0.310, 0.316},+};+const Colorimetry Colorimetry::BT2020 = Colorimetry{+ xy{0.708, 0.292},+ xy{0.170, 0.797},+ xy{0.131, 0.046},+ xy{0.3127, 0.3290},+};+const Colorimetry Colorimetry::CIEXYZ = Colorimetry{+ XYZ{1.0, 0.0, 0.0},+ XYZ{0.0, 1.0, 0.0},+ XYZ{0.0, 0.0, 1.0},+ xy{1.0 / 3.0, 1.0 / 3.0}.toXYZ(),+};+const Colorimetry Colorimetry::DCIP3 = Colorimetry{+ xy{0.680, 0.320},+ xy{0.265, 0.690},+ xy{0.150, 0.060},+ xy{0.314, 0.351},+};+const Colorimetry Colorimetry::DisplayP3 = Colorimetry{+ xy{0.680, 0.320},+ xy{0.265, 0.690},+ xy{0.150, 0.060},+ xy{0.3127, 0.3290},+};+const Colorimetry Colorimetry::AdobeRGB = Colorimetry{+ xy{0.6400, 0.3300},+ xy{0.2100, 0.7100},+ xy{0.1500, 0.0600},+ xy{0.3127, 0.3290},+};++QDebug operator<<(QDebug debug, const xy &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "xy(x: " << value.x << ", y: " << value.y << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const xyY &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "xyY(x: " << value.x << ", y: " << value.y << ", Y: " << value.Y << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const XYZ &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "XYZ(X: " << value.X << ", Y: " << value.Y << ", Z: " << value.Z << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const Colorimetry &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "Colorimetry(Red: " << value.red().toxy() << ", Green: " << value.green().toxy() << ", Blue: " << value.blue().toxy() << ", White: " << value.white().toxy() << ")";+ return debug;+}++} // namespace KisColorimetryUtils\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/KisColorimetryUtils.h b/libs/surfacecolormanagementapi/KisColorimetryUtils.hnew file mode 100644index 0000000000..465dd86d68--- /dev/null+++ b/libs/surfacecolormanagementapi/KisColorimetryUtils.h@@ -0,0 +1,152 @@+/*+ * SPDX-FileCopyrightText: 2023 Xaver Hugl <[email protected]>+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KISCOLORIMETRYUTILS_H+#define KISCOLORIMETRYUTILS_H++#include <QVector2D>+#include <QVector3D>+#include <QMatrix4x4>+#include <boost/operators.hpp>+#include <QDebug>++namespace KisColorimetryUtils+{++// these functions were taken from KWin's colorimetry code+// https://invent.kde.org/plasma/kwin/-/blob/master/src/core/colorspace.h++QMatrix4x4 matrixFromColumns(const QVector3D &first, const QVector3D &second, const QVector3D &third);++struct XYZ;++/**+ * xyY, with Y unspecified+ */+struct xy {+ double x;+ double y;++ XYZ toXYZ() const;+ QVector2D asVector() const;+ bool operator==(const xy &other) const;+};++struct xyY {+ double x;+ double y;+ double Y;++ XYZ toXYZ() const;+ bool operator==(const xyY &other) const;+};++struct XYZ {+ double X;+ double Y;+ double Z;++ xyY toxyY() const;+ xy toxy() const;+ QVector3D asVector() const;+ XYZ operator*(double factor) const;+ XYZ operator/(double factor) const;+ XYZ operator+(const XYZ &other) const;+ bool operator==(const XYZ &other) const;++ static XYZ fromVector(const QVector3D &vector);+};++/**+ * Describes the definition of colors in a color space.+ * Red, green and blue define the chromaticities ("absolute colors") of the red, green and blue LEDs on a display in xy coordinates+ * White defines the the chromaticity of the reference white in xy coordinates+ */+class Colorimetry+{+public:+ static const Colorimetry BT709;+ static const Colorimetry PAL_M;+ static const Colorimetry PAL;+ static const Colorimetry NTSC;+ static const Colorimetry GenericFilm;+ static const Colorimetry BT2020;+ static const Colorimetry CIEXYZ;+ static const Colorimetry DCIP3;+ static const Colorimetry DisplayP3;+ static const Colorimetry AdobeRGB;++ /**+ * @returns a matrix adapting XYZ values from the source whitepoint to the destination whitepoint with the Bradford transform+ */+ static QMatrix4x4 chromaticAdaptationMatrix(XYZ sourceWhitepoint, XYZ destinationWhitepoint);++ static QMatrix4x4 calculateToXYZMatrix(XYZ red, XYZ green, XYZ blue, XYZ white);++ /**+ * checks if the colorimetry is sane and won't cause crashes or glitches+ */+ static bool isValid(xy red, xy green, xy blue, xy white);+ /**+ * checks if the colorimetry could be from a real display+ */+ static bool isReal(xy red, xy green, xy blue, xy white);++ explicit Colorimetry(XYZ red, XYZ green, XYZ blue, XYZ white);+ explicit Colorimetry(xyY red, xyY green, xyY blue, xyY white);+ explicit Colorimetry(xy red, xy green, xy blue, xy white);++ /**+ * @returns a matrix that transforms from the linear RGB representation of colors in this colorimetry to the XYZ representation+ */+ const QMatrix4x4 &toXYZ() const;+ /**+ * @returns a matrix that transforms from the XYZ representation to the linear RGB representation of colors in this colorimetry+ */+ const QMatrix4x4 &fromXYZ() const;+ QMatrix4x4 toLMS() const;+ QMatrix4x4 fromLMS() const;++ bool operator==(const Colorimetry &other) const;+ /**+ * @returns this colorimetry, adapted to the new whitepoint using the Bradford transform+ */+ Colorimetry adaptedTo(xyY newWhitepoint) const;+ /**+ * replaces the current whitepoint with the new one+ * this does not do whitepoint adaptation!+ */+ Colorimetry withWhitepoint(xyY newWhitePoint) const;+ /**+ * interpolates the primaries depending on the passed factor. The whitepoint stays unchanged+ */+ Colorimetry interpolateGamutTo(const Colorimetry &one, double factor) const;++ QMatrix4x4 relativeColorimetricTo(const Colorimetry &other) const;+ QMatrix4x4 absoluteColorimetricTo(const Colorimetry &other) const;++ const XYZ &red() const;+ const XYZ &green() const;+ const XYZ &blue() const;+ const XYZ &white() const;++private:+ XYZ m_red;+ XYZ m_green;+ XYZ m_blue;+ XYZ m_white;+ QMatrix4x4 m_toXYZ;+ QMatrix4x4 m_fromXYZ;+};++QDebug operator<<(QDebug debug, const xy &value);+QDebug operator<<(QDebug debug, const xyY &value);+QDebug operator<<(QDebug debug, const XYZ &value);+QDebug operator<<(QDebug debug, const Colorimetry &value);++} // namespace KisColorimetryUtils++#endif /* KISCOLORIMETRYUTILS_H */\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.cpp b/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.cppnew file mode 100644index 0000000000..d51cc8e0a2--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.cpp@@ -0,0 +1,13 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisOutputColorInfoInterface.h"++KisOutputColorInfoInterface::~KisOutputColorInfoInterface()+{+}++#include <moc_KisOutputColorInfoInterface.cpp>\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.h b/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.hnew file mode 100644index 0000000000..8e898c60b2--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisOutputColorInfoInterface.h@@ -0,0 +1,89 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KISOUTPUTCOLORINFOINTERFACE_H+#define KISOUTPUTCOLORINFOINTERFACE_H++#include "KisSurfaceColorimetry.h"++#include <QObject>+#include <QFuture>++class QWindow;+class QScreen;++/**+ * A basic interface for fetching colorimetry information from a QScreen+ * object.+ *+ * Due to complications of some protocols (i.e. Wayland), the construction+ * and initialization of the object may be not straight forward and may+ * include several steps:+ *+ * 1) Initially, when KisOutputColorInfoInterface is created, it is created+ * in a "not ready" state. \ref isReady() will return false and+ * \ref outputDescription() for any screen may (or may not) return+ * std::nullopt.+ *+ * 2) Then the screens will asynchronously initialize. The interface will+ * emit \ref sigOutputDescriptionChanged() for each known interfrace.+ *+ * 3) When all known screens are initialized (and all+ * \ref sigOutputDescriptionChanged() signals are emitted), the interface+ * emits \ref sigReadyChanged() with argument set to true. At this point+ * \ref outputDescription() will return a correct description for every+ * known screen.+ *+ * When the connection to the compositor is lost, the interface first+ * emits \ref sigReadyChanged() with argument set to false, and restarts+ * the initialization process.+ */+class KisOutputColorInfoInterface : public QObject+{+ Q_OBJECT+public:+ virtual ~KisOutputColorInfoInterface();++ /**+ * \return true when the interface is considered as "fully initialized",+ * i.e. when all screen descriptions are ready.+ */+ virtual bool isReady() const = 0;++ /**+ * \return a color space description for the specified screen+ *+ * When \ref isReady() is set to false may or may not return std::nullopt,+ * wait for \ref sigReadyChanged() to make sure all screens are initialized.+ */+ virtual std::optional<KisSurfaceColorimetry::SurfaceDescription> outputDescription(const QScreen *screen) const = 0;++Q_SIGNALS:+ /**+ * Emits when the interface transitions between ready and non-ready states.+ *+ * The interface becomes ready when the connection to the compositor is+ * established and information about all screens is fetched.+ *+ * The interface may become non-ready at any moment of time when the+ * connection to the compositor is lost. You should wait for+ * sigReadyChanged(true) in such a case.+ */+ void sigReadyChanged(bool isReady);++ /**+ * Emits when a screen has changed its color description, e.g. when+ * the user assigned a new color profile to this screen in the compositor+ *+ * Please note that during the initialization process this signal is emitted+ * for every screen **before** sigReadyChanged(true).+ */+ void sigOutputDescriptionChanged(QScreen *screen, const KisSurfaceColorimetry::SurfaceDescription &desc);++protected:+ QWindow *m_window;+};++#endif /* KISOUTPUTCOLORINFOINTERFACE_H */\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.cpp b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.cppnew file mode 100644index 0000000000..6824514f63--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.cpp@@ -0,0 +1,18 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisSurfaceColorManagerInterface.h"++KisSurfaceColorManagerInterface::KisSurfaceColorManagerInterface(QWindow *window)+ : m_window(window)+{+}++KisSurfaceColorManagerInterface::~KisSurfaceColorManagerInterface()+{+}++#include <moc_KisSurfaceColorManagerInterface.cpp>\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.h b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.hnew file mode 100644index 0000000000..4464e105b9--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorManagerInterface.h@@ -0,0 +1,121 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KISSURFACECOLORMANAGERINTERFACE_H+#define KISSURFACECOLORMANAGERINTERFACE_H++#include "KisSurfaceColorimetry.h"++#include <QObject>+#include <QFuture>++class QWindow;+class QScreen;+++/**+ * An interface for fetching and setting the colorimetry information+ * of a surface underlying a QWindow+ *+ * Due to complications of some protocols (i.e. Wayland), the construction+ * and initialization of the object may be not straight forward and may+ * include several steps:+ *+ * 1) Initially, When KisSurfaceColorManagerInterface is created, it is+ * created in a "not ready" state:+ *+ * * \ref isReady() will return false+ * * \ref surfaceDescription() will return std::nullopt+ * * \ref preferredSurfaceDescription() will return std::nullopt+ * * the calls to \ref setSurfaceDescription() and \ref unsetSurfaceDescription()+ * do nothing+ *+ * 2) When initialization is completed, the interface emits \ref sigReadyChanged()+ * with argument set to true. At this moment all the functions of the interface+ * are supposed to work and do what expected.+ *+ * WARNING: Please take it into account that \ref sigPreferredSurfaceDescriptionChanged()+ * is not emitted during the initialization process. You should handle both+ * the signals.+ *+ * 3) When the preferred surface color space is changed, the interface emits+ * \ref sigPreferredSurfaceDescriptionChanged(). This may happen when the+ * QWindow is moved to a different screen or when the user changes the settings+ * of the current screen.+ *+ * When the connection to the compositor is lost, the interface first+ * emits \ref sigReadyChanged() with argument set to false, and restarts+ * the initialization process.+ */+class KisSurfaceColorManagerInterface : public QObject+{+ Q_OBJECT+public:+ KisSurfaceColorManagerInterface(QWindow *window);+ virtual ~KisSurfaceColorManagerInterface();++ /**+ * \return true when the interface is considered as "fully initialized",+ * i.e. all the methods of the interface are supposed to work+ * as expected+ */+ virtual bool isReady() const = 0;++ /**+ * Test if the surface description is supported by the compositor+ */+ virtual bool supportsSurfaceDescription(const KisSurfaceColorimetry::SurfaceDescription &desc) = 0;++ /**+ * Test if the rendering intent is supported by the compositor+ */+ virtual bool supportsRenderIntent(const KisSurfaceColorimetry::RenderIntent &intent) = 0;++ /**+ * Sets the surface description of the linked QWindow+ *+ * The operation may be asynchronous on some platforms, so you should+ * handle a QFuture<bool> object to get the actual result.+ *+ * \return a future telling if the description has already been set or not.+ *+ * WARNING: Please do NOT call future.waitForFinished() from the GUI thread.+ * On some platforms (e.g. wayland) a signal from the even loop should be processed+ * to actually set the surface description, so you'll get a deadlock if you try to+ * wait for it in the event loop's thread.+ */+ virtual QFuture<bool> setSurfaceDescription(const KisSurfaceColorimetry::SurfaceDescription &desc, KisSurfaceColorimetry::RenderIntent intent) = 0;++ /**+ * Unset the surface description of the underlying surface. In most of the compositors+ * it means that the surface will be treated as sRGB.+ */+ virtual void unsetSurfaceDescription() = 0;++ /**+ * \return the current description of the underlying surface+ *+ * It can return std::nullopt if the surface discription is unset or+ * if the connection to the compositor has been lost.+ */+ virtual std::optional<KisSurfaceColorimetry::SurfaceDescription> surfaceDescription() const = 0;++ /**+ * \return the preferred description for the underlying surface+ * from the viewpoint of the compositor+ *+ * It can return std::nullopt only when the interface is not ready+ */+ virtual std::optional<KisSurfaceColorimetry::SurfaceDescription> preferredSurfaceDescription() const = 0;++Q_SIGNALS:+ void sigReadyChanged(bool value);+ void sigPreferredSurfaceDescriptionChanged(const KisSurfaceColorimetry::SurfaceDescription &desc);++protected:+ QWindow *m_window;+};++#endif /* KISSURFACECOLORMANAGERINTERFACE_H */\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.cpp b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.cppnew file mode 100644index 0000000000..f1ed4913aa--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.cpp@@ -0,0 +1,156 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisSurfaceColorimetry.h"++#include <QDebug>++namespace KisSurfaceColorimetry {++QDebug operator<<(QDebug debug, const NamedPrimaries &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "NamedPrimaries(";+ switch (value) {+ case NamedPrimaries::primaries_unknown:+ debug.nospace() << "primaries_unknown";+ break;+ case NamedPrimaries::primaries_srgb:+ debug.nospace() << "primaries_srgb";+ break;+ case NamedPrimaries::primaries_bt2020:+ debug.nospace() << "primaries_bt2020";+ break;+ case NamedPrimaries::primaries_dci_p3:+ debug.nospace() << "primaries_dci_p3";+ break;+ case NamedPrimaries::primaries_display_p3:+ debug.nospace() << "primaries_display_p3";+ break;+ case NamedPrimaries::primaries_adobe_rgb:+ debug.nospace() << "primaries_adobe_rgb";+ break;+ }+ debug.nospace() << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const NamedTransferFunction &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "NamedTransferFunction(";+ switch (value) {+ case NamedTransferFunction::transfer_function_unknown:+ debug.nospace() << "transfer_function_unknown";+ break;+ case NamedTransferFunction::transfer_function_bt1886:+ debug.nospace() << "transfer_function_bt1886";+ break;+ case NamedTransferFunction::transfer_function_gamma22:+ debug.nospace() << "transfer_function_gamma22";+ break;+ case NamedTransferFunction::transfer_function_gamma28:+ debug.nospace() << "transfer_function_gamma28";+ break;+ case NamedTransferFunction::transfer_function_ext_linear:+ debug.nospace() << "transfer_function_ext_linear";+ break;+ case NamedTransferFunction::transfer_function_srgb:+ debug.nospace() << "transfer_function_srgb";+ break;+ case NamedTransferFunction::transfer_function_ext_srgb:+ debug.nospace() << "transfer_function_ext_srgb";+ break;+ case NamedTransferFunction::transfer_function_st2084_pq:+ debug.nospace() << "transfer_function_st2084_pq";+ break;+ case NamedTransferFunction::transfer_function_st428:+ debug.nospace() << "transfer_function_st428";+ break;+ }+ debug.nospace() << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const Luminance &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "Luminance(minLuminance: " << value.minLuminance+ << ", maxLuminance: " << value.maxLuminance+ << ", referenceLuminance: " << value.referenceLuminance << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const MasteringLuminance &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "MasteringLuminance(minLuminance: " << value.minLuminance+ << ", maxLuminance: " << value.maxLuminance << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const ColorSpace &value) {+ QDebugStateSaver saver(debug);++ debug.nospace() << "ColorSpace(";+ std::visit([&] (auto &&v) {debug.nospace() << "primaries: " << v; }, value.primaries);+ std::visit([&] (auto &&v) {debug.nospace() << ", transferFunction: " << v; }, value.transferFunction);+ if (value.luminance) {+ debug.nospace() << ", luminance: " << value.luminance;+ } else {+ debug.nospace() << ", luminance: " << "<none>";+ }+ debug.nospace() << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const MasteringInfo &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "MasteringInfo(primaries: " << value.primaries+ << ", luminance: " << value.luminance;+ if (value.maxCll) {+ debug.nospace() << ", maxCll: " << *value.maxCll;+ }+ if (value.maxFall) {+ debug.nospace() << ", maxFall: " << *value.maxFall;+ }+ debug.nospace() << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const SurfaceDescription &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "SurfaceDescription(colorSpace: " << value.colorSpace;+ if (value.masteringInfo) {+ debug.nospace() << ", masteringInfo: " << *value.masteringInfo;+ }+ debug.nospace() << ")";+ return debug;+}++QDebug operator<<(QDebug debug, const KisSurfaceColorimetry::RenderIntent &value) {+ QDebugStateSaver saver(debug);+ debug.nospace() << "RenderIntent(";+ switch (value) {+ case KisSurfaceColorimetry::RenderIntent::render_intent_perceptual:+ debug.nospace() << "render_intent_perceptual";+ break;+ case KisSurfaceColorimetry::RenderIntent::render_intent_relative:+ debug.nospace() << "render_intent_relative";+ break;+ case KisSurfaceColorimetry::RenderIntent::render_intent_saturation:+ debug.nospace() << "render_intent_saturation";+ break;+ case KisSurfaceColorimetry::RenderIntent::render_intent_absolute:+ debug.nospace() << "render_intent_absolute";+ break;+ case KisSurfaceColorimetry::RenderIntent::render_intent_relative_bpc:+ debug.nospace() << "render_intent_relative_bpc";+ break;+ }+ debug.nospace() << ")";+ return debug;+}++} // namespace KisSurfaceColorimetry++/* KISSURFACECOLORIMETRY_H */\ No newline at end of filediff --git a/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.h b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.hnew file mode 100644index 0000000000..2f73b90f12--- /dev/null+++ b/libs/surfacecolormanagementapi/surfacecolormanagement/KisSurfaceColorimetry.h@@ -0,0 +1,133 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */+#ifndef KISSURFACECOLORIMETRY_H+#define KISSURFACECOLORIMETRY_H++#include <boost/operators.hpp>+#include <KisColorimetryUtils.h>+++namespace KisSurfaceColorimetry+{+ using namespace KisColorimetryUtils;++ enum class NamedPrimaries {+ primaries_unknown = 0,+ primaries_srgb = 1,+ primaries_bt2020 = 6,+ primaries_dci_p3 = 8,+ primaries_display_p3 = 9,+ primaries_adobe_rgb = 10,+ };++ enum class NamedTransferFunction {+ transfer_function_unknown = 0,+ transfer_function_bt1886 = 1,+ transfer_function_gamma22 = 2, // supported by KDE+ transfer_function_gamma28 = 3,+ transfer_function_ext_linear = 5, // supported by KDE+ transfer_function_srgb = 9, // supported by KDE+ transfer_function_ext_srgb = 10,+ transfer_function_st2084_pq = 11, // supported by KDE+ transfer_function_st428 = 12,+ };++ enum class RenderIntent {+ render_intent_perceptual = 0, // perceptual+ render_intent_relative = 1, // media-relative colorimetric+ render_intent_saturation = 2, // saturation+ render_intent_absolute = 3, // ICC-absolute colorimetric+ render_intent_relative_bpc = 4, // media-relative colorimetric + black point compensation+ };++ struct Luminance : boost::equality_comparable<Luminance> {+ Luminance() = default;+ Luminance(uint32_t minLuminanceArg, uint32_t maxLuminanceArg, uint32_t referenceLuminanceArg)+ : minLuminance(minLuminanceArg)+ , maxLuminance(maxLuminanceArg)+ , referenceLuminance(referenceLuminanceArg)+ {+ }++ uint32_t minLuminance = 2000; // (cd/m^2) * 10000+ uint32_t maxLuminance = 80; // (cd/m^2)+ uint32_t referenceLuminance = 80; // (cd/m^2)++ bool operator==(const Luminance &other) const {+ return minLuminance == other.minLuminance &&+ maxLuminance == other.maxLuminance &&+ referenceLuminance == other.referenceLuminance;+ }+ };++ struct MasteringLuminance : boost::equality_comparable<MasteringLuminance> {+ MasteringLuminance() = default;+ MasteringLuminance(uint32_t minLuminanceArg, uint32_t maxLuminanceArg)+ : minLuminance(minLuminanceArg)+ , maxLuminance(maxLuminanceArg)+ {}++ uint32_t minLuminance = 2000; // (cd/m^2) * 10000+ uint32_t maxLuminance = 80; // (cd/m^2)++ static MasteringLuminance fromLuminance(const Luminance &rhs) {+ return {rhs.minLuminance, rhs.maxLuminance};+ }++ bool operator==(const MasteringLuminance &other) const {+ return minLuminance == other.minLuminance &&+ maxLuminance == other.maxLuminance;+ }+ };++ struct ColorSpace : boost::equality_comparable<ColorSpace> {+ std::variant<NamedPrimaries, Colorimetry> primaries;+ // named transfer function or <exponent * 10000>+ std::variant<NamedTransferFunction, uint32_t> transferFunction;+ std::optional<Luminance> luminance;++ bool operator==(const ColorSpace &other) const {+ return primaries == other.primaries &&+ transferFunction == other.transferFunction &&+ luminance == other.luminance;+ }+ };++ struct MasteringInfo : boost::equality_comparable<MasteringInfo> {+ Colorimetry primaries = Colorimetry::BT709;+ MasteringLuminance luminance;+ std::optional<uint32_t> maxCll;+ std::optional<uint32_t> maxFall;++ bool operator==(const MasteringInfo &other) const {+ return primaries == other.primaries &&+ luminance == other.luminance &&+ maxCll == other.maxCll &&+ maxFall == other.maxFall;+ }+ };++ struct SurfaceDescription : boost::equality_comparable<SurfaceDescription> {+ ColorSpace colorSpace;+ std::optional<MasteringInfo> masteringInfo;++ bool operator==(const SurfaceDescription &other) const {+ return colorSpace == other.colorSpace &&+ masteringInfo == other.masteringInfo;+ }+ };++ QDebug operator<<(QDebug debug, const NamedPrimaries &value);+ QDebug operator<<(QDebug debug, const NamedTransferFunction &value);+ QDebug operator<<(QDebug debug, const Luminance &value);+ QDebug operator<<(QDebug debug, const MasteringLuminance &value);+ QDebug operator<<(QDebug debug, const ColorSpace &value);+ QDebug operator<<(QDebug debug, const MasteringInfo &value);+ QDebug operator<<(QDebug debug, const SurfaceDescription &value);+ QDebug operator<<(QDebug debug, const RenderIntent &value);+}++#endif /* KISSURFACECOLORIMETRY_H */\ No newline at end of file