Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_gamutmap_clip_adjustment.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 Wolthera van Hövell tot Westerflier <griffinvalley@gmail.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-only
5 */
6
8#include "KoAlwaysInline.h"
9
10#include <kis_debug.h>
11
12#include <KoColorSpace.h>
14#include <KoColorProfile.h>
15
16#include <KoColorConversions.h>
18#include <KoColorSpaceTraits.h>
20#include <KoID.h>
21
22#include <KoGamutMappingUtils.h>
24
25namespace
26{
27
28
29struct xyzToOkLabModel {
30 static ALWAYS_INLINE std::array<qreal, 3> toLCH (const float* xyzChannels) {
31 const QVector4D OkLab = KoGamutMappingUtils::XYZToOkLab(QVector4D(xyzChannels[0], xyzChannels[1], xyzChannels[2], 0.0));
32 std::array<qreal, 3> lch {0.0, 0.0, 0.0};
33 LabToLCH(OkLab.x(), OkLab.y()+0.5, OkLab.z()+0.5, &lch[0], &lch[1], &lch[2]);
34 return lch;
35 }
36
37 static ALWAYS_INLINE void fromLCH (const std::array<qreal, 3> &lch, float *xyzChannels) {
38 std::array<qreal, 3> output {0.0, 0.0, 0.0};
39 LCHToLab(lch[0], lch[1], lch[2], &output[0], &output[1], &output[2]);
40
41 const QVector4D xyz = KoGamutMappingUtils::OkLabToXYZ(QVector4D(output[0], output[1]-0.5, output[2]-0.5, 0.0));
42 xyzChannels[0] = xyz.x();
43 xyzChannels[1] = xyz.y();
44 xyzChannels[2] = xyz.z();
45
46 //qDebug() << "lch" << lch << "col" << color << "lmsprime" << lmsPrime << "lms" << lms << "xyz" << channels;
47 }
48
49 static ALWAYS_INLINE float cuberootLightness(float lightness) {
50 return lightness;
51 }
52};
53
54struct xyzToICtCpModel {
55 static ALWAYS_INLINE std::array<qreal, 3> toLCH (const float *xyzChannels) {
56 QVector4D color (xyzChannels[0], xyzChannels[1], xyzChannels[2], 1.0);
57 const QVector4D ICtCp = KoGamutMappingUtils::XYZToICtCp(color);
58 std::array<qreal, 3> lch {0.0, 0.0, 0.0};
59 LabToLCH(ICtCp.x(), (ICtCp.y()+0.5), (ICtCp.z()+0.5), &lch[0], &lch[1], &lch[2]);
60
61 return lch;
62 }
63
64 static ALWAYS_INLINE void fromLCH (const std::array<qreal, 3> &lch, float *xyzChannels) {
65 std::array<qreal, 3> output {0.0, 0.0, 0.0};
66 LCHToLab(lch[0], lch[1], lch[2], &output[0], &output[1], &output[2]);
67
68
69 QVector4D color (output[0], output[1]-0.5, output[2]-0.5, 0.0);
70 const QVector4D xyz = KoGamutMappingUtils::ICtCpToXYZ(color);
71 xyzChannels[0] = xyz.x();
72 xyzChannels[1] = xyz.y();
73 xyzChannels[2] = xyz.z();
74
75 }
76
77 static ALWAYS_INLINE float cuberootLightness(float lightness) {
78 return cbrtf(removeSmpte2048Curve(lightness));
79 }
80};
81
82struct labModel {
83 static ALWAYS_INLINE std::array<qreal, 3> toLCH (const float *labChannels) {
84 std::array<qreal, 3> lch {0.0, 0.0, 0.0};
85 LabToLCH(labChannels[0], labChannels[1], labChannels[2], &lch[0], &lch[1], &lch[2]);
86 return lch;
87 }
88
89 static ALWAYS_INLINE void fromLCH (const std::array<qreal, 3> &lch, float *labChannels) {
90 std::array<qreal, 3> output {0.0, 0.0, 0.0};
91 LCHToLab(lch[0], lch[1], lch[2], &output[0], &output[1], &output[2]);
92 labChannels[0] = output[0];
93 labChannels[1] = output[1];
94 labChannels[2] = output[2];
95 }
96
97 static ALWAYS_INLINE float cuberootLightness(float lightness) {
98 return lightness;
99 }
100};
101
102struct dE76 {
103 static ALWAYS_INLINE float difference(const QVector<float> &a, const QVector<float> &b) {
105 }
106};
107}
108
109template<class ModelType, class DifferenceType>
111{
112public:
114 : m_sourceSpace(sourceSpace) {}
115public:
116
117 // Algorithm based of https://drafts.csswg.org/css-color-4/#pseudo-binsearch
118 void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
119 {
120
121 if (!m_sourceSpace) {
122 qWarning() << "no source space!";
123 return;
124 }
126 if (!targetProfile) {
127 qWarning() << "no target profile found for..." << m_targetColorProfileName;
128 return;
129 }
130 const bool greyscale = targetProfile->colorModelID() == GrayAColorModelID.id();
131 const KoColorSpace *targetSpace = KoColorSpaceRegistry::instance()->colorSpace(targetProfile->colorModelID(), Integer16BitsColorDepthID.id(), targetProfile);
132 if (!targetSpace) {
133 qWarning() << "no target space!";
134 return;
135 }
136
137 QVector<float> originalChannels(m_sourceSpace->channelCount());
138 QVector<float> clippedChannels(m_sourceSpace->channelCount());
139 std::unique_ptr<quint8[]> tmpOriginal(new quint8[m_sourceSpace->pixelSize()]);
140 std::unique_ptr<quint8[]> tmpTarget (new quint8[targetSpace->pixelSize()]);
141 std::unique_ptr<quint8[]> tmpClipped (new quint8[m_sourceSpace->pixelSize()]);
142 std::array<qreal, 3> LCH {0.0, 0.0, 0.0};
143
144 while (nPixels > 0) {
145 // We could make good use of a LUT here.
146 m_sourceSpace->normalisedChannelsValue(srcU8, originalChannels);
147 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
148
149 if (greyscale) {
150 LCH = ModelType::toLCH(originalChannels.data());
151 LCH[1] = 0;
152 ModelType::fromLCH(LCH, originalChannels.data());
153 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
154 } else if (!m_cuspDescription.isEmpty()) {
155 LCH = ModelType::toLCH(originalChannels.data());
157
164 const float L = ModelType::cuberootLightness(LCH[0]);
165 const float cuspL = ModelType::cuberootLightness(cusp.y());
166 if (LCH[0] > cusp.y()) {
167 const float t = (L - cuspL) / (1.0 - cuspL);
168 const float c = qMax(0.0, cusp.x()* (1.0-t));
169
170 LCH[1] = qMin(LCH[1], qreal(c));
171
172 } else {
173 const float t = L / (cuspL);
174 const float c = qMax(0.0, cusp.x()*t);
175 LCH[1] = qMin(LCH[1], qreal(c));
176 }
177 ModelType::fromLCH(LCH, originalChannels.data());
178 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
179 } else {
182
183 m_sourceSpace->normalisedChannelsValue(tmpClipped.get(), clippedChannels);
184
185 if (DifferenceType::difference(originalChannels, clippedChannels) > 0.02) {
186 LCH = ModelType::toLCH(originalChannels.data());
187
188 float min = 0;
189 float max = LCH[1];
190
191 while (max-min > 0.0001) {
192
193 LCH[1] = (max + min) * 0.5;
194 ModelType::fromLCH(LCH, originalChannels.data());
195
196
197 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
200 m_sourceSpace->normalisedChannelsValue(tmpClipped.get(), clippedChannels);
201
202 if (DifferenceType::difference(originalChannels, clippedChannels) < 0.02) {
203 min = LCH[1];
204 } else {
205 max = LCH[1];
206 }
207 }
208 }
209 }
210
211 memcpy(dstU8, tmpOriginal.get(), m_sourceSpace->pixelSize());
212
213 --nPixels;
214 srcU8 += m_sourceSpace->pixelSize();
215 dstU8 += m_sourceSpace->pixelSize();
216 }
217 }
218
219 QList<QString> parameters() const override
220 {
221 QList<QString> list;
222 list << "targetColorProfileName";
223 list << "cuspDescription";
224 return list;
225 }
226
227 int parameterId(const QString& name) const override
228 {
229 if (name == "targetColorProfileName") {
230 return 0;
231 } else if (name == "cuspDescription") {
232 return 1;
233 }
234 return -1;
235 }
236
237 void setParameter(int id, const QVariant& parameter) override
238 {
239 switch(id)
240 {
241 case 0:
242 m_targetColorProfileName = parameter.toString();
243 break;
244 case 1:
245
246 m_cuspDescription = QMap<float, QPointF>();
247 {
248 const QVariantMap map = parameter.toMap();
249 Q_FOREACH (QString key, map.keys()){
250 m_cuspDescription.insert(key.toFloat(), map.value(key).toPointF());
251 }
252 }
253 break;
254 default:
255 ;
256 }
257 }
258
259private:
260
262 QMap<float, QPointF> m_cuspDescription;
264};
265
271
273{
275 l.append(QPair< KoID, KoID >(XYZAColorModelID , Float32BitsColorDepthID));
276 // While the other two work for gamut mapping, they clip HDR values.
277 l.append(QPair< KoID, KoID >(LABAColorModelID , Float32BitsColorDepthID));
278
279 // Note: Only ICtCp is really supported.
280 l.append(QPair< KoID, KoID >(YCbCrAColorModelID , Float32BitsColorDepthID));
281
282
283 return l;
284}
285
286KoColorTransformation *KisGamutMapClipAdjustmentFactory::createTransformation(const KoColorSpace *colorSpace, QHash<QString, QVariant> parameters) const
287{
289
290 if (colorSpace->colorModelId() == LABAColorModelID) {
292 } else if (colorSpace->colorModelId() == YCbCrAColorModelID) {
294 } else if (colorSpace->colorModelId() == XYZAColorModelID) {
295 if (parameters["mappingModel"].toInt() == KoGamutMappingUtils::OkLAB) {
297 } else {
299 }
300 } else {
301 dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisGamutMapClipAdjustmentFactory::createTransformation";
302 return 0;
303 }
304
305 adj->setParameters(parameters);
306 return adj;
307}
#define ALWAYS_INLINE
void LabToLCH(const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
void LCHToLab(const qreal L, const qreal C, const qreal H, qreal *l, qreal *a, qreal *b)
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID YCbCrAColorModelID("YCbCrA", ki18n("YCbCr/Alpha"))
const KoID GrayAColorModelID("GRAYA", ki18n("Grayscale/Alpha"))
const KoID XYZAColorModelID("XYZA", ki18n("XYZ/Alpha"))
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
const KoID LABAColorModelID("LABA", ki18n("L*a*b*/Alpha"))
ALWAYS_INLINE float removeSmpte2048Curve(float x, float refWhite=203.0) noexcept
KoColorTransformation * createTransformation(const KoColorSpace *colorSpace, QHash< QString, QVariant > parameters) const override
QList< QPair< KoID, KoID > > supportedModels() const override
void setParameter(int id, const QVariant &parameter) override
int parameterId(const QString &name) const override
KisGamutMapClipAdjustment(const KoColorSpace *sourceSpace)
void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
QList< QString > parameters() const override
virtual quint32 pixelSize() const =0
virtual KoID colorModelId() const =0
virtual quint32 channelCount() const =0
virtual void normalisedChannelsValue(const quint8 *pixel, QVector< float > &channels) const =0
virtual void fromNormalisedChannelsValue(quint8 *pixel, const QVector< float > &values) const =0
virtual bool convertPixelsTo(const quint8 *src, quint8 *dst, const KoColorSpace *dstColorSpace, quint32 numPixels, KoColorConversionTransformation::Intent renderingIntent, KoColorConversionTransformation::ConversionFlags conversionFlags) const
void setParameters(const QHash< QString, QVariant > &parameters)
QString id() const
Definition KoID.cpp:63
#define dbgKrita
Definition kis_debug.h:49
QVector4D KRITAPIGMENT_EXPORT ICtCpToXYZ(const QVector4D &iCtCp)
float KRITAPIGMENT_EXPORT deltaE76(const QVector< float > &a, const QVector< float > &b)
deltaE76 calculates the difference between two colors, represented as normalized channels.
QVector4D KRITAPIGMENT_EXPORT OkLabToXYZ(const QVector4D &okLab)
QVector4D KRITAPIGMENT_EXPORT XYZToICtCp(const QVector4D &xyz)
XYZToICtCp Ad-hoc implementation of ICtCp for use in filters. Unlike the official YCbCr-compatible st...
QVector4D KRITAPIGMENT_EXPORT XYZToOkLab(const QVector4D &xyz)
XYZToOkLab Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB mode...
QPointF KRITAPIGMENT_EXPORT cuspForHue(const QMap< float, QPointF > &gamutCuspDescription, const float hue)
constexpr std::enable_if< sizeof...(values)==0, size_t >::type max()
virtual QString colorModelID() const
const KoColorProfile * profileByName(const QString &name) const
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()