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++

Add basic gamut mapping filter to Krita

Wolthera van Hövell tot Westerflier06cec8448582
diff --git a/plugins/color/colorspaceextensions/CMakeLists.txt b/plugins/color/colorspaceextensions/CMakeLists.txt
index 4df756f614..5dfcbcc7c4 100644
--- a/plugins/color/colorspaceextensions/CMakeLists.txt
+++ b/plugins/color/colorspaceextensions/CMakeLists.txt
@@ -9,8 +9,14 @@ set( extensions_plugin_SOURCES
kis_burnshadows_adjustment.cpp
kis_color_balance_adjustment.cpp
kis_desaturate_adjustment.cpp
+ kis_gamutmap_clip_adjustment.cpp
)
kis_add_library(krita_colorspaces_extensions MODULE ${extensions_plugin_SOURCES})
+
+target_sources(krita_colorspaces_extensions
+ PRIVATE
+ kis_gamutmap_clip_adjustment.h kis_gamutmap_clip_adjustment.cpp
+)
target_link_libraries(krita_colorspaces_extensions kritapigment kritaglobal ${LINK_OPENEXR_LIB} KF${KF_MAJOR}::I18n KF${KF_MAJOR}::CoreAddons)
install( TARGETS krita_colorspaces_extensions DESTINATION ${KRITA_PLUGIN_INSTALL_DIR} )
diff --git a/plugins/color/colorspaceextensions/extensions_plugin.cc b/plugins/color/colorspaceextensions/extensions_plugin.cc
index 5f5469a423..2973057bf1 100644
--- a/plugins/color/colorspaceextensions/extensions_plugin.cc
+++ b/plugins/color/colorspaceextensions/extensions_plugin.cc
@@ -19,6 +19,7 @@
#include "kis_burnshadows_adjustment.h"
#include "kis_color_balance_adjustment.h"
#include "kis_desaturate_adjustment.h"
+#include "kis_gamutmap_clip_adjustment.h"
K_PLUGIN_FACTORY_WITH_JSON(ExtensionsPluginFactory, "krita_colorspaces_extensions_plugin.json", registerPlugin<ExtensionsPlugin>();)
@@ -39,6 +40,8 @@ ExtensionsPlugin::ExtensionsPlugin(QObject *parent, const QVariantList &)
KoColorTransformationFactoryRegistry::addColorTransformationFactory(new KisColorBalanceAdjustmentFactory);
KoColorTransformationFactoryRegistry::addColorTransformationFactory(new KisDesaturateAdjustmentFactory);
+
+ KoColorTransformationFactoryRegistry::addColorTransformationFactory(new KisGamutMapClipAdjustmentFactory);
}
ExtensionsPlugin::~ExtensionsPlugin()
diff --git a/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp
new file mode 100644
index 0000000000..834d07cc06
--- /dev/null
+++ b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp
@@ -0,0 +1,197 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-only
+ */
+
+#include "kis_gamutmap_clip_adjustment.h"
+
+#include <kis_debug.h>
+
+#include <KoColorSpace.h>
+#include <KoColorSpaceRegistry.h>
+#include <KoColorProfile.h>
+
+#include <KoColorConversions.h>
+#include <KoColorModelStandardIds.h>
+#include <KoColorSpaceTraits.h>
+#include <KoColorTransformation.h>
+#include <KoID.h>
+
+template<typename _channel_type_,typename traits>
+class KisGamutMapClipAdjustment : public KoColorTransformation
+{
+ typedef traits PixelTrait;
+ typedef typename PixelTrait::Pixel Pixel;
+public:
+ KisGamutMapClipAdjustment(const KoColorSpace *sourceSpace): m_sourceSpace(sourceSpace) {}
+public:
+
+ static qreal difference(const QVector<float> &a, const QVector<float> &b) {
+ //This is E76, but we should also have a E2000 implementation.
+ float dL = a[0] - b[0];
+ float dA = a[1] - b[1];
+ float dB = a[2] - b[2];
+ return qSqrt(dL * dL + dA * dA + dB * dB);
+ }
+
+ // Algorithm based of https://drafts.csswg.org/css-color-4/#pseudo-binsearch
+ void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
+ {
+ if (!m_sourceSpace) {
+ qWarning() << "no source space!";
+ return;
+ }
+ const KoColorProfile *targetProfile = KoColorSpaceRegistry::instance()->profileByName(m_targetColorProfileName);
+ if (!targetProfile) {
+ qWarning() << "no target profile found for..." << m_targetColorProfileName;
+ return;
+ }
+ const KoColorSpace *targetSpace = KoColorSpaceRegistry::instance()->colorSpace(targetProfile->colorModelID(), Integer16BitsColorDepthID.id(), targetProfile);
+ if (!targetSpace) {
+ qWarning() << "no target space!";
+ return;
+ }
+
+ while (nPixels > 0) {
+ // We could make good use of a LUT here.
+ QVector<float> originalChannels(m_sourceSpace->channelCount());
+ QVector<float> clippedChannels(m_sourceSpace->channelCount());
+ quint8* tmpOriginal = new quint8(m_sourceSpace->pixelSize());
+ quint8* tmpTarget = new quint8(targetSpace->pixelSize());
+ quint8* tmpClipped = new quint8(m_sourceSpace->pixelSize());
+
+ m_sourceSpace->normalisedChannelsValue(srcU8, originalChannels);
+ m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal, originalChannels);
+
+ m_sourceSpace->convertPixelsTo(tmpOriginal, tmpTarget, targetSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());
+ targetSpace->convertPixelsTo(tmpTarget, tmpClipped, m_sourceSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());
+
+ m_sourceSpace->normalisedChannelsValue(tmpClipped, clippedChannels);
+
+ QVector<qreal> originalLCH(3);
+ QVector<qreal> modifiedChannels(3);
+ QVector<qreal> clippedLCH(3);
+ if (difference(originalChannels, clippedChannels) > 0.02) {
+ LabToLCH(float(originalChannels[0]), float(originalChannels[1]), float(originalChannels[2]), &originalLCH[0], &originalLCH[1], &originalLCH[2]);
+ float min = 0;
+ float max = originalLCH[1];
+
+ while (max-min > 0.0001) {
+
+ LabToLCH(float(originalChannels[0]), float(originalChannels[1]), float(originalChannels[2]), &originalLCH[0], &originalLCH[1], &originalLCH[2]);
+ LabToLCH(float(clippedChannels[0]), float(clippedChannels[1]), float(clippedChannels[2]), &clippedLCH[0], &clippedLCH[1], &clippedLCH[2]);
+
+ qreal newChroma = (max + min) * 0.5;
+
+ LCHToLab(originalLCH[0], newChroma, originalLCH[2], &modifiedChannels[0], &modifiedChannels[1], &modifiedChannels[2]);
+ originalChannels[0] = modifiedChannels[0];
+ originalChannels[1] = modifiedChannels[1];
+ originalChannels[2] = modifiedChannels[2];
+
+ m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal, originalChannels);
+ m_sourceSpace->convertPixelsTo(tmpOriginal, tmpTarget, targetSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());
+ targetSpace->convertPixelsTo(tmpTarget, tmpClipped, m_sourceSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());
+ m_sourceSpace->normalisedChannelsValue(tmpClipped, clippedChannels);
+
+ if (difference(originalChannels, clippedChannels) < 0.02) {
+ min = newChroma;
+ } else {
+ max = newChroma;
+ }
+ }
+ }
+
+ memcpy(dstU8, tmpOriginal, m_sourceSpace->pixelSize());
+
+ --nPixels;
+ srcU8 += m_sourceSpace->pixelSize();
+ dstU8 += m_sourceSpace->pixelSize();
+ }
+ }
+
+ QList<QString> parameters() const override
+ {
+ QList<QString> list;
+ list << "targetColorProfileName";
+ return list;
+ }
+
+ int parameterId(const QString& name) const override
+ {
+ if (name == "targetColorProfileName") {
+ return 0;
+ }
+ return -1;
+ }
+
+ void setParameter(int id, const QVariant& parameter) override
+ {
+ switch(id)
+ {
+ case 0:
+ m_targetColorProfileName = parameter.toString();
+ break;
+ default:
+ ;
+ }
+ }
+
+private:
+
+ QString m_targetColorProfileName;
+ QString m_targetColorSpaceModel;
+ const KoColorSpace *m_sourceSpace;
+};
+
+KisGamutMapClipAdjustmentFactory::KisGamutMapClipAdjustmentFactory()
+ : KoColorTransformationFactory("gamutmap_clip_adjustment")
+{
+
+}
+
+QList<QPair<KoID, KoID> > KisGamutMapClipAdjustmentFactory::supportedModels() const
+{
+ QList< QPair< KoID, KoID > > l;
+ // While the other two work for gamut mapping, they clip HDR values.
+ l.append(QPair< KoID, KoID >(LABAColorModelID , Float32BitsColorDepthID));
+
+ // Note: Only ICtCp is really supported.
+ l.append(QPair< KoID, KoID >(YCbCrAColorModelID , Float32BitsColorDepthID));
+ return l;
+}
+
+KoColorTransformation *KisGamutMapClipAdjustmentFactory::createTransformation(const KoColorSpace *colorSpace, QHash<QString, QVariant> parameters) const
+{
+ KoColorTransformation * adj;
+
+ if (colorSpace->colorModelId() == LABAColorModelID) {
+ if (colorSpace->colorDepthId() == Integer8BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< quint8, KoLabTraits < quint8 > >(colorSpace);
+ } else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< quint16, KoLabTraits < quint16 > >(colorSpace);
+ } else if (colorSpace->colorDepthId() == Float32BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< float, KoLabTraits < float > >(colorSpace);
+ } else {
+ dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisGamutMapClipAdjustmentFactory::createTransformation";
+ return 0;
+ }
+ } else if (colorSpace->colorModelId() == YCbCrAColorModelID) {
+ if (colorSpace->colorDepthId() == Integer8BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< quint8, KoYCbCrTraits < quint8 > >(colorSpace);
+ } else if (colorSpace->colorDepthId() == Integer16BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< quint16, KoYCbCrTraits < quint16 > >(colorSpace);
+ } else if (colorSpace->colorDepthId() == Float32BitsColorDepthID) {
+ adj = new KisGamutMapClipAdjustment< float, KoYCbCrTraits < float > >(colorSpace);
+ } else {
+ dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisGamutMapClipAdjustmentFactory::createTransformation";
+ return 0;
+ }
+ } else {
+ dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisGamutMapClipAdjustmentFactory::createTransformation";
+ return 0;
+ }
+
+ adj->setParameters(parameters);
+ return adj;
+}
diff --git a/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.h b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.h
new file mode 100644
index 0000000000..fa4b16da9f
--- /dev/null
+++ b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.h
@@ -0,0 +1,37 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-only
+ */
+
+#ifndef KIS_GAMUTMAP_CLIP_ADJUSTMENT_H
+#define KIS_GAMUTMAP_CLIP_ADJUSTMENT_H
+
+#include "KoColorTransformationFactory.h"
+
+/**
+ * @brief The KisGamutMapClipAdjustment class
+ *
+ * Normally, in the ICC workflow, a profile is supossed to handle
+ * any kind of gamut mapping. This includes dealing with clipping
+ * values that are out of gamut.
+ *
+ * This KoColorTransformation takes colors, converts them to an
+ * opponent space, like Lab or ICtCp, and then desaturates the
+ * colors until they are inside the target color gamut.
+ *
+ * There's more complex gamut mapping algorithms available, but
+ * this particular transform focuses on simplicity and speed.
+ */
+
+class KisGamutMapClipAdjustmentFactory : public KoColorTransformationFactory
+{
+public:
+ KisGamutMapClipAdjustmentFactory();
+
+ QList< QPair< KoID, KoID > > supportedModels() const override;
+
+ KoColorTransformation* createTransformation(const KoColorSpace* colorSpace, QHash<QString, QVariant> parameters) const override;
+};
+
+#endif // KIS_GAMUTMAP_CLIP_ADJUSTMENT_H
diff --git a/plugins/filters/CMakeLists.txt b/plugins/filters/CMakeLists.txt
index 91625abf9a..dbbca6133d 100644
--- a/plugins/filters/CMakeLists.txt
+++ b/plugins/filters/CMakeLists.txt
@@ -32,3 +32,4 @@ add_subdirectory( asccdl )
add_subdirectory( palettize )
add_subdirectory( resettransparent )
add_subdirectory( propagatecolors )
+add_subdirectory( tonegamutmap )
diff --git a/plugins/filters/tonegamutmap/CMakeLists.txt b/plugins/filters/tonegamutmap/CMakeLists.txt
new file mode 100644
index 0000000000..f9f8b9c319
--- /dev/null
+++ b/plugins/filters/tonegamutmap/CMakeLists.txt
@@ -0,0 +1,7 @@
+set(kritatonegamutmapfilter_SOURCES KisToneGamutMapFilter.cpp )
+
+
+kis_add_library(kritatonegamutmapfilter MODULE ${kritatonegamutmapfilter_SOURCES})
+target_link_libraries(kritatonegamutmapfilter kritaui)
+install(TARGETS kritatonegamutmapfilter DESTINATION ${KRITA_PLUGIN_INSTALL_DIR})
+
diff --git a/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp
new file mode 100644
index 0000000000..c9331a6738
--- /dev/null
+++ b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp
@@ -0,0 +1,41 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-only
+ */
+#include "KisToneGamutMapFilter.h"
+#include <kis_filter_category_ids.h>
+#include <filter/kis_filter_registry.h>
+#include <KoColorModelStandardIds.h>
+
+#include <kpluginfactory.h>
+#include <klocalizedstring.h>
+
+K_PLUGIN_FACTORY_WITH_JSON(KisToneGamutMapFilterFactory, "kritatonegamutmapfilter.json", registerPlugin<KisToneGamutMapFilterPlugin>();)
+
+KisToneGamutMapFilterPlugin::KisToneGamutMapFilterPlugin(QObject *parent, const QVariantList &): QObject(parent)
+{
+ KisFilterRegistry::instance()->add(KisFilterSP(new KisToneGamutMapFilter()));
+}
+
+KisToneGamutMapFilterPlugin::~KisToneGamutMapFilterPlugin()
+{
+
+}
+
+KisToneGamutMapFilter::KisToneGamutMapFilter()
+ : KisColorTransformationFilter(id(), FiltersCategoryColorId, i18n("Tone/Gamut Map..."))
+{
+ setSupportsThreading(true);
+ setSupportsAdjustmentLayers(false);
+ setSupportsPainting(false);
+}
+
+KoColorTransformation *KisToneGamutMapFilter::createTransformation(const KoColorSpace *cs, const KisFilterConfigurationSP config) const
+{
+ QHash<QString, QVariant> params;
+ params["targetColorProfileName"] = "sRGB-elle-V2-srgbtrc.icc";
+ return cs->createColorTransformation("gamutmap_clip_adjustment", params);
+}
+
+#include "KisToneGamutMapFilter.moc"
diff --git a/plugins/filters/tonegamutmap/KisToneGamutMapFilter.h b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.h
new file mode 100644
index 0000000000..badf901ede
--- /dev/null
+++ b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.h
@@ -0,0 +1,32 @@
+/*
+ * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <[email protected]>
+ *
+ * SPDX-License-Identifier: LGPL-2.0-only
+ */
+#ifndef KISTONEGAMUTMAPFILTER_H
+#define KISTONEGAMUTMAPFILTER_H
+
+#include "filter/kis_color_transformation_filter.h"
+
+class KisToneGamutMapFilterPlugin : public QObject
+{
+ Q_OBJECT
+public:
+ KisToneGamutMapFilterPlugin(QObject *parent, const QVariantList &);
+ ~KisToneGamutMapFilterPlugin() override;
+};
+
+class KisToneGamutMapFilter: public KisColorTransformationFilter
+{
+public:
+ KisToneGamutMapFilter();
+
+ KoColorTransformation* createTransformation(const KoColorSpace* cs, const KisFilterConfigurationSP config) const override;
+
+ static inline KoID id() {
+ return KoID("tone_gamut_map", i18n("Tone/Gamut Map"));
+ }
+
+};
+
+#endif // KISTONEGAMUTMAPFILTER_H
diff --git a/plugins/filters/tonegamutmap/kritatonegamutmapfilter.json b/plugins/filters/tonegamutmap/kritatonegamutmapfilter.json
new file mode 100644
index 0000000000..dd7846012c
--- /dev/null
+++ b/plugins/filters/tonegamutmap/kritatonegamutmapfilter.json
@@ -0,0 +1,10 @@
+{
+ "Icon": "",
+ "Id": "Tone Gamut Map Filter",
+ "Type": "Service",
+ "X-KDE-Library": "kritatonegamutmapfilter",
+ "X-KDE-ServiceTypes": [
+ "Krita/Filter"
+ ],
+ "X-Krita-Version": "28"
+}