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 a cusp based gamut mapping method.
Wolthera van Hövell tot Westerfliera5d41cdf5a20
diff --git a/libs/pigment/KoGamutMappingUtils.cpp b/libs/pigment/KoGamutMappingUtils.cppindex 249ffb04a1..94a99111c1 100644--- a/libs/pigment/KoGamutMappingUtils.cpp+++ b/libs/pigment/KoGamutMappingUtils.cpp@@ -6,6 +6,12 @@#include "KoGamutMappingUtils.h"#include <kis_global.h>+#include <KoColorModelStandardIds.h>+#include <KoColorSpace.h>+#include <KoColor.h>+#include <KoColorConversions.h>+#include <KoColorTransferFunctions.h>+#include <QtMath>float KoGamutMappingUtils::deltaE76(const QVector<float> &a, const QVector<float> &b) {@@ -250,3 +256,196 @@ KoGamutMappingUtils::PiecewiseFilmicCurve KoGamutMappingUtils::PiecewiseFilmicCureturn PiecewiseFilmicCurve(maxWhite, point0, point1, overshoot);}++const QMatrix4x4 xyz50To65 {QMatrix4x4(+ 0.955473421488075, -0.02309845494876471, 0.06325924320057072, 0.0,+ -0.0283697093338637, 1.0099953980813041, 0.021041441191917323, 0.0,+ 0.012314014864481998, -0.020507649298898964, 1.330365926242124, 0.0,+ 0.0, 0.0, 0.0, 1.0+ )};++const QMatrix4x4 xyz50To65_inv = xyz50To65.inverted();++const QMatrix4x4 XYZtoOkLABLMS {QMatrix4x4(+ 0.8190224379967030, 0.3619062600528904, -0.1288737815209879, 0.0,+ 0.0329836539323885, 0.9292868615863434, 0.0361446663506424, 0.0,+ 0.0481771893596242, 0.2642395317527308, 0.6335478284694309, 0.0,+ 0.0, 0.0, 0.0, 1.0+ )};++const QMatrix4x4 XYZtoOkLABLMS_inv = XYZtoOkLABLMS.inverted();++const QMatrix4x4 lmsToOkLab {QMatrix4x4(+ 0.2104542683093140, 0.7936177747023054, -0.0040720430116193, 0.0,+ 1.9779985324311684, -2.4285922420485799, 0.4505937096174110, 0.0,+ 0.0259040424655478, 0.7827717124575296, -0.8086757549230774, 0.0,+ 0.0, 0.0, 0.0, 1.0+ )};++const QMatrix4x4 lmsToOkLab_inv = lmsToOkLab.inverted();++const QMatrix4x4 XYZd65toDolbyLMS {QMatrix4x4(+ 0.3592832590121217, 0.6976051147779502, -0.0358915932320290, 0.0,+ -0.1920808463704993, 1.1004767970374321, 0.0753748658519118, 0.0,+ 0.0070797844607479, 0.0748396662186362, 0.8433265453898765, 0.0,+ 0.0, 0.0, 0.0, 1.0+ )};++const QMatrix4x4 XYZd65toDolbyLMS_inv = XYZd65toDolbyLMS.inverted();++const QMatrix4x4 lmsToICtCp {QMatrix4x4(+ 2048.0 / 4096.0, 2048.0 / 4096.0, 0.0 , 0.0,+ 6610.0 / 4096.0, -13613.0 / 4096.0, 7003.0 / 4096.0 , 0.0,+ 17933.0 / 4096.0, -17390.0 / 4096.0, -543.0 / 4096.0, 0.0,+ 0.0, 0.0, 0.0, 1.0+ )};++const QMatrix4x4 lmsToICtCp_inv = lmsToICtCp.inverted();++QVector4D KoGamutMappingUtils::XYZToOkLab(const QVector4D &xyz) {+ const QVector4D lms = XYZtoOkLABLMS * (xyz50To65 * xyz);++ const QVector4D lmsPrime = QVector4D(cbrtf(lms.x()),+ cbrtf(lms.y()),+ cbrtf(lms.z()), 0.0);++ return lmsToOkLab * lmsPrime;+}++QVector4D KoGamutMappingUtils::OkLabToXYZ(const QVector4D &okLab) {+ const QVector4D lmsPrime = lmsToOkLab_inv * okLab;+ const QVector4D lms = QVector4D(powf(lmsPrime.x(), 3.0),+ powf(lmsPrime.y(), 3.0),+ powf(lmsPrime.z(), 3.0), 0.0);+ return xyz50To65_inv * (XYZtoOkLABLMS_inv * lms);+}++QVector4D KoGamutMappingUtils::XYZToICtCp(const QVector4D &xyz)+{+ const QVector4D lms = XYZd65toDolbyLMS * (xyz50To65 * xyz);++ const QVector4D lmsPrime = QVector4D(applySmpte2048Curve(lms.x()),+ applySmpte2048Curve(lms.y()),+ applySmpte2048Curve(lms.z()), 0.0);++ return lmsToICtCp * lmsPrime;+}++QVector4D KoGamutMappingUtils::ICtCpToXYZ(const QVector4D &iCtCp)+{+ const QVector4D lmsPrime = lmsToICtCp_inv * iCtCp;+ const QVector4D lms = QVector4D(removeSmpte2048Curve(lmsPrime.x()),+ removeSmpte2048Curve(lmsPrime.y()),+ removeSmpte2048Curve(lmsPrime.z()), 0.0);+ return xyz50To65_inv * (XYZd65toDolbyLMS_inv * lms);+}++QMap<float, QPointF> KoGamutMappingUtils::getGamutCuspDescription(const KoColorProfile *profile, KoGamutMappingUtils::MappingModel model, const int samplesPerChannel)+{+ QMap<float, QPointF> bounds;+ const KoColorSpace *cs = KoColorSpaceRegistry::instance()->colorSpace(profile->colorModelID(), Integer8BitsColorDepthID.id(), profile);+ const KoColorSpace *xyzCs = KoColorSpaceRegistry::instance()->colorSpace(XYZAColorModelID.id(), Float32BitsColorDepthID.id(), "");+ const KoColorSpace *labCs = KoColorSpaceRegistry::instance()->colorSpace(LABAColorModelID.id(), Float32BitsColorDepthID.id(), "");+ QVector<float> xyzChannels(xyzCs->channelCount());+ QVector<float> labChannels(xyzCs->channelCount());++ QVector<QVector<float>> colorList;+ if (profile->colorModelID() == RGBAColorModelID.id()) {+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {ch, 1.0, 0.0, 1.0};+ colorList << c;+ QVector<float> c2 = {ch, 0.0, 1.0, 1.0};+ colorList << c2;+ }+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {1.0, ch, 0.0, 1.0};+ colorList << c;+ QVector<float> c2 = {0.0, ch, 1.0, 1.0};+ colorList << c2;+ }+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {1.0, 0.0, ch, 1.0};+ colorList << c;+ QVector<float> c2 = {0.0, 1.0, ch, 1.0};+ colorList << c2;+ }+ } else if (profile->colorModelID() == CMYKAColorModelID.id()) {+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {ch, 1.0, 0.0, 0.0, 1.0};+ colorList << c;+ QVector<float> c2 = {ch, 0.0, 1.0, 0.0, 1.0};+ colorList << c2;+ }+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {1.0, ch, 0.0, 0.0, 1.0};+ colorList << c;+ QVector<float> c2 = {0.0, ch, 1.0, 0.0, 1.0};+ colorList << c2;+ }+ for (int i = 0; i < samplesPerChannel; i++) {+ const float ch = float(i)/float(samplesPerChannel);+ QVector<float> c = {1.0, 0.0, ch, 0.0, 1.0};+ colorList << c;+ QVector<float> c2 = {0.0, 1.0, ch, 0.0, 1.0};+ colorList << c2;+ }+ }+ KoColor c(cs);++ Q_FOREACH( const QVector<float> channels, colorList) {+ cs->fromNormalisedChannelsValue(c.data(), channels);+ std::array<qreal, 3> lch;+ if (model == LAB) {+ KoColor xyc = c.convertedTo(labCs);+ labCs->normalisedChannelsValue(xyc.data(), labChannels);+ LabToLCH(labChannels[0], labChannels[1], labChannels[2], &lch[0], &lch[1], &lch[2]);+ } else {+ KoColor xyc = c.convertedTo(xyzCs);+ xyzCs->normalisedChannelsValue(xyc.data(), xyzChannels);++ if (model == ICtCp) {+ QVector4D ictcp = KoGamutMappingUtils::XYZToICtCp(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));+ LabToLCH(ictcp.x(), ictcp.y()+0.5, ictcp.z()+0.5, &lch[0], &lch[1], &lch[2]);+ } else {+ QVector4D okLab = KoGamutMappingUtils::XYZToOkLab(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));+ LabToLCH(okLab.x(), okLab.y()+0.5, okLab.z()+0.5, &lch[0], &lch[1], &lch[2]);+ }+ }++ bounds.insert(lch[2], QPointF(lch[1], lch[0]));+ }++ return bounds;+}++QPointF KoGamutMappingUtils::cuspForHue(const QMap<float, QPointF> &gamutCuspDescription, const float hue)+{+ QPointF cusp;++ for (int i = 0; i < gamutCuspDescription.size(); i++) {+ const float angle = gamutCuspDescription.keys().at(i);+ float nextAngle = gamutCuspDescription.keys().value(i+1, gamutCuspDescription.keys().first());+ if (i +1 == gamutCuspDescription.size()) {+ nextAngle += 1.0;+ }+ if (qFuzzyCompare(hue, angle)) {+ cusp = gamutCuspDescription.value(angle);+ break;+ } else if (hue > angle && hue < nextAngle) {+ QPointF lch1 = gamutCuspDescription.value(angle);+ QPointF lch2 = gamutCuspDescription.value(nextAngle);+ const float t = (hue - angle) / (nextAngle - angle);+ cusp = QLineF(lch1, lch2).pointAt(t);+ break;+ }+ }++ return cusp;+}++diff --git a/libs/pigment/KoGamutMappingUtils.h b/libs/pigment/KoGamutMappingUtils.hindex d00f483a32..d50dd41c22 100644--- a/libs/pigment/KoGamutMappingUtils.h+++ b/libs/pigment/KoGamutMappingUtils.h@@ -9,6 +9,9 @@#include <QPointF>#include <QVector>+#include <KoColorimetryUtils.h>+#include <KoColorProfile.h>+#include "kritapigment_export.h"namespace KoGamutMappingUtils {@@ -27,6 +30,25 @@ float KRITAPIGMENT_EXPORT deltaE76(const QVector<float> &a, const QVector<float>*/float KRITAPIGMENT_EXPORT deltaE2000(const QVector<float> &a, const QVector<float> &b);+/**+ * @brief XYZToOkLab+ * Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB model.+ * @param xyz+ * @return Oklab color.+ */+QVector4D KRITAPIGMENT_EXPORT XYZToOkLab(const QVector4D &xyz);+QVector4D KRITAPIGMENT_EXPORT OkLabToXYZ(const QVector4D &okLab);++/**+ * @brief XYZToICtCp+ * Ad-hoc implementation of ICtCp for use in filters.+ * Unlike the official YCbCr-compatible standard, this one doesn't have a 0.5 offset in the Ct and Cp channels.+ * @param xyz+ * @return+ */+QVector4D KRITAPIGMENT_EXPORT XYZToICtCp(const QVector4D &xyz);+QVector4D KRITAPIGMENT_EXPORT ICtCpToXYZ(const QVector4D &iCtCp);+/*** @brief The MappingModel enum* This represents the preferred model to do gamut and/or tone mapping in.@@ -39,6 +61,16 @@ enum MappingModel {OkLAB};+/**+ * @brief getGamutCuspDescription+ * Generates a hexagonal gamut cusp description from a RGB or CMYK profile.+ * @param profile cmyk or RGB profile to use.+ * @return gamut cusp description, consisting of hue and the XYZ of the maximum chroma.+ */+QMap<float, QPointF> KRITAPIGMENT_EXPORT getGamutCuspDescription(const KoColorProfile *profile, KoGamutMappingUtils::MappingModel model, const int samplesPerChannel = 5);+QPointF KRITAPIGMENT_EXPORT cuspForHue(const QMap<float, QPointF> &gamutCuspDescription, const float hue);++enum CurveType {Reinhard,PiecewiseFilmicdiff --git a/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cppindex b49b448366..cb12ffed3d 100644--- a/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp+++ b/plugins/color/colorspaceextensions/kis_gamutmap_clip_adjustment.cpp@@ -25,44 +25,10 @@namespace{-const QMatrix4x4 xyz50To65 {QMatrix4x4(- 0.955473421488075, -0.02309845494876471, 0.06325924320057072, 0.0,- -0.0283697093338637, 1.0099953980813041, 0.021041441191917323, 0.0,- 0.012314014864481998, -0.020507649298898964, 1.330365926242124, 0.0,- 0.0, 0.0, 0.0, 1.0- )};--const QMatrix4x4 xyz50To65_inv = xyz50To65.inverted();--const QMatrix4x4 XYZtoOkLABLMS {QMatrix4x4(- 0.8190224379967030, 0.3619062600528904, -0.1288737815209879, 0.0,- 0.0329836539323885, 0.9292868615863434, 0.0361446663506424, 0.0,- 0.0481771893596242, 0.2642395317527308, 0.6335478284694309, 0.0,- 0.0, 0.0, 0.0, 1.0- )};--const QMatrix4x4 XYZtoOkLABLMS_inv = XYZtoOkLABLMS.inverted();--const QMatrix4x4 lmsToOkLab {QMatrix4x4(- 0.2104542683093140, 0.7936177747023054, -0.0040720430116193, 0.0,- 1.9779985324311684, -2.4285922420485799, 0.4505937096174110, 0.0,- 0.0259040424655478, 0.7827717124575296, -0.8086757549230774, 0.0,- 0.0, 0.0, 0.0, 1.0- )};--const QMatrix4x4 lmsToOkLab_inv = lmsToOkLab.inverted();struct xyzToOkLabModel {static ALWAYS_INLINE std::array<qreal, 3> toLCH (const QVector<float> &channels) {- QVector4D color (channels[0], channels[1], channels[2], 0.0);-- const QVector4D lms = XYZtoOkLABLMS * (xyz50To65 * color);-- const QVector4D lmsPrime = QVector4D(cbrtf(lms.x()),- cbrtf(lms.y()),- cbrtf(lms.z()), 0.0);-- const QVector4D OkLab = lmsToOkLab * lmsPrime;+ const QVector4D OkLab = KoGamutMappingUtils::XYZToOkLab(QVector4D(channels[0], channels[1], channels[2], 0.0));std::array<qreal, 3> lch {0.0, 0.0, 0.0};LabToLCH(OkLab.x(), OkLab.y()+0.5, OkLab.z()+0.5, &lch[0], &lch[1], &lch[2]);return lch;@@ -72,14 +38,7 @@ struct xyzToOkLabModel {std::array<qreal, 3> output {0.0, 0.0, 0.0};LCHToLab(lch[0], lch[1], lch[2], &output[0], &output[1], &output[2]);-- QVector4D color (output[0], output[1]-0.5, output[2]-0.5, 0.0);-- const QVector4D lmsPrime = lmsToOkLab_inv * color;- const QVector4D lms = QVector4D(powf(lmsPrime.x(), 3.0),- powf(lmsPrime.y(), 3.0),- powf(lmsPrime.z(), 3.0), 0.0);- const QVector4D xyz = xyz50To65_inv * (XYZtoOkLABLMS_inv * lms);+ const QVector4D xyz = KoGamutMappingUtils::OkLabToXYZ(QVector4D(output[0], output[1]-0.5, output[2]-0.5, 0.0));channels[0] = xyz.x();channels[1] = xyz.y();channels[2] = xyz.z();@@ -87,37 +46,16 @@ struct xyzToOkLabModel {//qDebug() << "lch" << lch << "col" << color << "lmsprime" << lmsPrime << "lms" << lms << "xyz" << channels;return channels;}-};--const QMatrix4x4 XYZd65toDolbyLMS {QMatrix4x4(- 0.3592832590121217, 0.6976051147779502, -0.0358915932320290, 0.0,- -0.1920808463704993, 1.1004767970374321, 0.0753748658519118, 0.0,- 0.0070797844607479, 0.0748396662186362, 0.8433265453898765, 0.0,- 0.0, 0.0, 0.0, 1.0- )};--const QMatrix4x4 XYZd65toDolbyLMS_inv = XYZd65toDolbyLMS.inverted();--const QMatrix4x4 lmsToICtCp {QMatrix4x4(- 2048.0 / 4096.0, 2048.0 / 4096.0, 0.0 , 0.0,- 6610.0 / 4096.0, -13613.0 / 4096.0, 7003.0 / 4096.0 , 0.0,- 17933.0 / 4096.0, -17390.0 / 4096.0, -543.0 / 4096.0, 0.0,- 0.0, 0.0, 0.0, 1.0- )};-const QMatrix4x4 lmsToICtCp_inv = lmsToICtCp.inverted();+ static ALWAYS_INLINE float linearizeLightness(float lightness) {+ return powf(lightness, 3.0);+ }+};struct xyzToICtCpModel {static ALWAYS_INLINE std::array<qreal, 3> toLCH (const QVector<float> &channels) {QVector4D color (channels[0], channels[1], channels[2], 0.0);-- const QVector4D lms = XYZd65toDolbyLMS * (xyz50To65 * color);-- const QVector4D lmsPrime = QVector4D(applySmpte2048Curve(lms.x()),- applySmpte2048Curve(lms.y()),- applySmpte2048Curve(lms.z()), 0.0);-- const QVector4D ICtCp = lmsToICtCp * lmsPrime;+ const QVector4D ICtCp = KoGamutMappingUtils::XYZToICtCp(color);std::array<qreal, 3> lch {0.0, 0.0, 0.0};LabToLCH(ICtCp.x(), (ICtCp.y()+0.5), (ICtCp.z()+0.5), &lch[0], &lch[1], &lch[2]);@@ -130,18 +68,17 @@ struct xyzToICtCpModel {QVector4D color (output[0], output[1]-0.5, output[2]-0.5, 0.0);-- const QVector4D lmsPrime = lmsToICtCp_inv * color;- const QVector4D lms = QVector4D(removeSmpte2048Curve(lmsPrime.x()),- removeSmpte2048Curve(lmsPrime.y()),- removeSmpte2048Curve(lmsPrime.z()), 0.0);- const QVector4D xyz = xyz50To65_inv * (XYZd65toDolbyLMS_inv * lms);+ const QVector4D xyz = KoGamutMappingUtils::ICtCpToXYZ(color);channels[0] = xyz.x();channels[1] = xyz.y();channels[2] = xyz.z();return channels;}++ static ALWAYS_INLINE float linearizeLightness(float lightness) {+ return removeSmpte2048Curve(lightness);+ }};struct labModel {@@ -159,6 +96,10 @@ struct labModel {channels[2] = output[2];return channels;}++ static ALWAYS_INLINE float linearizeLightness(float lightness) {+ return powf(lightness, 3.0);+ }};struct dE76 {@@ -178,12 +119,14 @@ template<class ModelType, class DifferenceType>class KisGamutMapClipAdjustment : public KoColorTransformation{public:- KisGamutMapClipAdjustment(const KoColorSpace *sourceSpace): m_sourceSpace(sourceSpace) {}+ KisGamutMapClipAdjustment(const KoColorSpace *sourceSpace)+ : m_sourceSpace(sourceSpace) {}public:// Algorithm based of https://drafts.csswg.org/css-color-4/#pseudo-binsearchvoid transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override{+if (!m_sourceSpace) {qWarning() << "no source space!";return;@@ -193,6 +136,7 @@ public:qWarning() << "no target profile found for..." << m_targetColorProfileName;return;}+ const bool greyscale = targetProfile->colorModelID() == GrayAColorModelID.id();const KoColorSpace *targetSpace = KoColorSpaceRegistry::instance()->colorSpace(targetProfile->colorModelID(), Integer16BitsColorDepthID.id(), targetProfile);if (!targetSpace) {qWarning() << "no target space!";@@ -217,29 +161,58 @@ public:m_sourceSpace->normalisedChannelsValue(tmpClipped.get(), clippedChannels);- if (DifferenceType::difference(originalChannels, clippedChannels) > 0.02) {+ if (greyscale) {+ LCH = ModelType::toLCH(originalChannels);+ LCH[1] = 0;+ originalChannels = ModelType::fromLCH(LCH, originalChannels);+ m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);+ } else if (DifferenceType::difference(originalChannels, clippedChannels) > 0.02) {LCH = ModelType::toLCH(originalChannels);- float min = 0;- float max = LCH[1];-- while (max-min > 0.0001) {+ if (!m_cuspDescription.isEmpty()) {+ QPointF cusp = KoGamutMappingUtils::cuspForHue(m_cuspDescription, LCH[2]);++ /// We are treating the cusp triangle as linear inside linear space.+ /// This is good enough for RGB profiles, CMYK profiles might get better+ /// results with the MINDE method. Overall though, we just want to move+ /// the colors into gamut enough so that we don't get hue shifts when converting,+ /// and this method delivers on that nicely.+ const float L = ModelType::linearizeLightness(LCH[0]);+ const float cuspL = ModelType::linearizeLightness(cusp.y());+ if (LCH[0] > cusp.y()) {+ const float t = (L - cuspL) / (1.0 - cuspL);+ const float c = qMax(0.0, cusp.x()* (1.0-t));+ LCH[1] = qMin(LCH[1], c);- LCH[1] = (max + min) * 0.5;+ } else {+ const float t = L / (cuspL);+ const float c = qMax(0.0, cusp.x()*t);+ LCH[1] = qMin(LCH[1], c);+ }originalChannels = ModelType::fromLCH(LCH, originalChannels);+ m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);+ } else {+ float min = 0;+ float max = LCH[1];+ while (max-min > 0.0001) {- m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);- m_sourceSpace->convertPixelsTo(tmpOriginal.get(), tmpTarget.get(), targetSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());- targetSpace->convertPixelsTo(tmpTarget.get(), tmpClipped.get(), m_sourceSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());- m_sourceSpace->normalisedChannelsValue(tmpClipped.get(), clippedChannels);+ LCH[1] = (max + min) * 0.5;+ originalChannels = ModelType::fromLCH(LCH, originalChannels);- if (DifferenceType::difference(originalChannels, clippedChannels) < 0.02) {- min = LCH[1];- } else {- max = LCH[1];- }- }++ m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);+ m_sourceSpace->convertPixelsTo(tmpOriginal.get(), tmpTarget.get(), targetSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());+ targetSpace->convertPixelsTo(tmpTarget.get(), tmpClipped.get(), m_sourceSpace, 1, KoColorConversionTransformation::IntentRelativeColorimetric, KoColorConversionTransformation::adjustmentConversionFlags());+ m_sourceSpace->normalisedChannelsValue(tmpClipped.get(), clippedChannels);++ if (DifferenceType::difference(originalChannels, clippedChannels) < 0.02) {+ min = LCH[1];+ } else {+ max = LCH[1];+ }+ }+ }}memcpy(dstU8, tmpOriginal.get(), m_sourceSpace->pixelSize());@@ -254,6 +227,7 @@ public:{QList<QString> list;list << "targetColorProfileName";+ list << "cuspDescription";return list;}@@ -261,6 +235,8 @@ public:{if (name == "targetColorProfileName") {return 0;+ } else if (name == "cuspDescription") {+ return 1;}return -1;}@@ -272,6 +248,16 @@ public:case 0:m_targetColorProfileName = parameter.toString();break;+ case 1:++ m_cuspDescription = QMap<float, QPointF>();+ {+ const QVariantMap map = parameter.toMap();+ Q_FOREACH (QString key, map.keys()){+ m_cuspDescription.insert(key.toFloat(), map.value(key).toPointF());+ }+ }+ break;default:;}@@ -280,7 +266,7 @@ public:private:QString m_targetColorProfileName;- QString m_targetColorSpaceModel;+ QMap<float, QPointF> m_cuspDescription;const KoColorSpace *m_sourceSpace;};diff --git a/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cppindex 23827035bb..ff1a45e123 100644--- a/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp+++ b/plugins/filters/tonegamutmap/KisToneGamutMapFilter.cpp@@ -30,6 +30,7 @@ const QString TONE_MAP_ENABLED = "toneMap/Enabled"; ///< bool, whether to do tonconst QString TONE_MAP_MAX_WHITE = "toneMap/reinhard/MaxWhite"; ///< double, the maximum white that needs to be tonemapped.const QString GAMUT_MAP_ENABLED = "gamutMap/Enabled"; ///< bool, whether to do gamt mapping.const QString GAMUT_MAP_TARGET_PROFILE = "gamutMap/TargetProfile"; ///< QString, name of the color profile that is the target gamut.+const QString GAMUT_MAP_METHOD = "gamutMap/method"; ///String, indicates whether to use the cusp map or the MINDE method.const QString TONE_MAP_TYPE = "toneMap/CurveType";@@ -93,6 +94,19 @@ KoColorTransformation *KisToneGamutMapFilter::createTransformation(const KoColorif (config->getProperty(GAMUT_MAP_ENABLED).toBool()) {QHash<QString, QVariant> params;params["targetColorProfileName"] = config->getProperty(GAMUT_MAP_TARGET_PROFILE);++ const KoColorProfile *targetProfile = KoColorSpaceRegistry::instance()->profileByName(config->getProperty(GAMUT_MAP_TARGET_PROFILE).toString());+ if (targetProfile && config->getProperty(GAMUT_MAP_METHOD).toString() == "CUSP_MAP") {+ QVariantMap cuspDescription;+ QMap<float, QPointF> tempCuspDescription = KoGamutMappingUtils::getGamutCuspDescription(targetProfile, KoGamutMappingUtils::MappingModel(config->getProperty(MAPPING_MODEL).toInt()));+ if (!tempCuspDescription.isEmpty()) {+ Q_FOREACH(const float key, tempCuspDescription.keys()) {+ cuspDescription.insert(QString::number(key), tempCuspDescription.value(key));+ }+ params["cuspDescription"] = cuspDescription;+ }+ }+params["mappingModel"] = config->getProperty(MAPPING_MODEL);KoColorTransformation *gamutMap = mappingCs->createColorTransformation("gamutmap_clip_adjustment", params);allTransforms << gamutMap;@@ -116,6 +130,7 @@ KisFilterConfigurationSP KisToneGamutMapFilter::defaultConfiguration(KisResourceconfig->setProperty(TONE_PIECE_WISE_SHOULDER_ANGLE, 1.0);config->setProperty(GAMUT_MAP_ENABLED, true);+ config->setProperty(GAMUT_MAP_METHOD, "CUSP_MAP");config->setProperty(GAMUT_MAP_TARGET_PROFILE, "sRGB-elle-V2-srgbtrc.icc");return config;}@@ -144,6 +159,16 @@ KisToneGamutMapWidget::KisToneGamutMapWidget(QWidget *parent, KisPaintDeviceSP dui->cmbToneCurveType->addItem(i18n("Reinhard"), KoGamutMappingUtils::Reinhard);ui->cmbToneCurveType->addItem(i18n("Piecewise Filmic"), KoGamutMappingUtils::PiecewiseFilmic);+ ui->cmbGamutMapMethod->addItem(i18n("Use Chroma-Lightness Cusp Map"), "CUSP_MAP");+ ui->cmbGamutMapMethod->addItem(i18n("Minimum Delta E"), "MINDE");++ ui->cmbGamutMapMethod->setItemData(0, i18nc("@info:tooltip", "Use a chroma-lightness cusp map for a faster,"+ " if more inaccurate gamut mapping. Only works for RGB and CMYK profiles,"+ " will fall back on Minimum Delta E."), Qt::ToolTipRole);+ ui->cmbGamutMapMethod->setItemData(1, i18nc("@info:tooltip", "Will use the Minimum Delta E method,"+ " where colors are brought into gamut until they minimally different from the transformed color."+ " Slow but accurate."), Qt::ToolTipRole);+connect(ui->cmbMappingModel, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));connect(ui->cmbToneCurveType, SIGNAL(activated(int)), ui->stkToneMap, SLOT(setCurrentIndex(int)));connect(ui->cmbToneCurveType, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));@@ -160,6 +185,8 @@ KisToneGamutMapWidget::KisToneGamutMapWidget(QWidget *parent, KisPaintDeviceSP dconnect(ui->chkGamutMap, SIGNAL(toggled(bool)), SIGNAL(sigConfigurationItemChanged()));connect(ui->targetGamut, SIGNAL(colorSpaceChanged(const KoColorSpace*)), SIGNAL(sigConfigurationItemChanged()));+ connect(ui->cmbGamutMapMethod, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));+}KisToneGamutMapWidget::~KisToneGamutMapWidget()@@ -214,6 +241,8 @@ void KisToneGamutMapWidget::setConfiguration(const KisPropertiesConfigurationSPprofile));}}+ int idx = ui->cmbGamutMapMethod->findData(config->getProperty(GAMUT_MAP_METHOD));+ ui->cmbGamutMapMethod->setCurrentIndex(idx);Q_EMIT sigConfigurationItemChanged();}@@ -265,6 +294,7 @@ KisPropertiesConfigurationSP KisToneGamutMapWidget::configuration() constif (ui->targetGamut->currentColorSpace()) {config->setProperty(GAMUT_MAP_TARGET_PROFILE, ui->targetGamut->currentColorSpace()->profile()->name());}+ config->setProperty(GAMUT_MAP_METHOD, ui->cmbGamutMapMethod->currentData());return config;}diff --git a/plugins/filters/tonegamutmap/kistonegamutmapwidget.ui b/plugins/filters/tonegamutmap/kistonegamutmapwidget.uiindex 1bf623ec2d..f99e5734bb 100644--- a/plugins/filters/tonegamutmap/kistonegamutmapwidget.ui+++ b/plugins/filters/tonegamutmap/kistonegamutmapwidget.ui@@ -223,6 +223,9 @@<item><widget class="KisColorSpaceSelector" name="targetGamut" native="true"/></item>+ <item>+ <widget class="QComboBox" name="cmbGamutMapMethod"/>+ </item></layout></widget></item>