Krita Source Code Documentation
Loading...
Searching...
No Matches
KoGamutMappingUtils.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.1-or-later
5 */
7#include <kis_global.h>
8
10#include <KoColorSpace.h>
11#include <KoColor.h>
12#include <KoColorConversions.h>
14
15#include <QtMath>
16
18 //This is E76, E2000 implementation is too complicated and buggy.
19 float dL = a[0] - b[0];
20 float dA = a[1] - b[1];
21 float dB = a[2] - b[2];
22 return qSqrt(dL * dL + dA * dA + dB * dB);
23}
24
25float KoGamutMappingUtils::reinhardToneMapping(const float linearY, const float maxY)
26{
27 const float num = linearY * (1.0f + (linearY/(maxY*maxY)));
28 return num / (linearY + 1.0f);
29}
30
31//---------- Piecewise Filmic Curve -----------//
32
33static void SolveAB(float & lnA, float & B, float x0, float y0, float m)
34{
35 B = (m*x0)/y0;
36 lnA = logf(y0) - B*logf(x0);
37}
38
39static void AsSlopeIntercept(float & m, float & b, float x0, float x1, float y0, float y1)
40{
41 float dy = (y1-y0);
42 float dx = (x1-x0);
43 if (dx == 0)
44 m = 1.0f;
45 else
46 m = dy/dx;
47
48 b = y0 - x0*m;
49}
50
51static float EvalDerivativeLinearGamma(float m, float b, float g, float x)
52{
53 float ret = g*m*powf(m*x+b,g-1.0f);
54 return ret;
55}
56
57KoGamutMappingUtils::PiecewiseFilmicCurve::PiecewiseFilmicCurve(float maxWhite, QPointF point0, QPointF point1, QPointF overshootPoint)
58{
60
61 p0 = point0;
62 p1 = point1;
63 QPointF overshoot = overshootPoint;
64
65 inverseMaxWhite = 1.0/maxWhite;
66 // normalize
67
68
69
70 p0.setX(p0.x()/maxWhite);
71 p1.setX(p1.x()/maxWhite);
72
73 overshoot.setX(overshootPoint.x()/maxWhite);
74
75 Segment linear;
76 float m, b;
77 AsSlopeIntercept(m, b, p0.x(), p1.x(), p0.y(), p1.y());
78 const float g = 1.0;
79
80 linear.offsetX = -(b / m);
81 linear.lnA = g * logf(m);
82 linear.b = g;
83
84 segments[1] = linear;
85
86 const float toeM = EvalDerivativeLinearGamma(m, b, g, p0.x());
87 const float shoulderM = EvalDerivativeLinearGamma(m, b, g, p1.x());
88
89 Segment toe;
90 SolveAB(toe.lnA, toe.b, p0.x(), p0.y(), toeM);
91 segments[0] = toe;
92
93 Segment shoulder;
94 const float x0 = (1.0f + overshoot.x()) - p1.x();
95 const float y0 = (1.0f + overshoot.y()) - p1.y();
96 SolveAB(shoulder.lnA, shoulder.b, x0, y0, shoulderM);
97
98 shoulder.offsetX = (1.0f + overshoot.x());
99 shoulder.offsetY = (1.0f + overshoot.y());
100 shoulder.scaleX = -1.0;
101 shoulder.scaleY = -1.0;
102
103 segments[2] = shoulder;
104
105 {
106 const float scale = shoulder.evalSegment(1.0f);
107 const float invScale = 1.0f / scale;
108
109 segments[0].offsetY *= invScale;
110 segments[0].scaleY *= invScale;
111
112 segments[1].offsetY *= invScale;
113 segments[1].scaleY *= invScale;
114
115 segments[2].offsetY *= invScale;
116 segments[2].scaleY *= invScale;
117 }
118}
119
121{
122 int index = (x < p0.x()) ? 0 : ((x < p1.x()) ? 1 : 2);
123 Segment s = segments.at(index);
124 return s.evalSegment(x*inverseMaxWhite);
125}
126
128{
129 float x0 = (x - offsetX) * scaleX;
130 float y0 = x0 > 0? expf(lnA + b * logf(x0)): 0.0;
131 return y0 * scaleY + offsetY;
132}
133
134KoGamutMappingUtils::PiecewiseFilmicCurve KoGamutMappingUtils::PiecewiseFilmicCurve::fromUserParams(float toeStrength, float toeLength, float shoulderStrength, float shoulderLength, float shoulderAngle)
135{
136 const float uiGamma = 2.2f;
137
138 const float normalizedToeLength = powf(qBound(0.0f, toeLength, 1.0f), uiGamma);
139 const float normalizedToeStrength = qBound(0.0f, toeStrength, 1.0f);
140 const float normalizedShoulderAngle = qBound(0.0f, shoulderAngle, 1.0f);
141
142 const float x0 = normalizedToeLength * .5f;
143 const float y0 = (1.0f - normalizedToeStrength) * x0;
144
145 const float remainingY = 1.0f - y0;
146 const float initialW = x0 + remainingY;
147
148 const float y1_offset = (1.0f - shoulderStrength) * remainingY;
149 const float x1 = x0 + y1_offset;
150 const float y1 = y0 + y1_offset;
151
152 const float extraW = exp2f(shoulderLength)-1.0f;
153 const float W = initialW + extraW;
154
155 QPointF point0(powf(x0, uiGamma), powf(y0, uiGamma));
156 QPointF point1(powf(x1, uiGamma), powf(y1, uiGamma));
157 float maxWhite = W;
158
159 QPointF overshoot;
160 overshoot.setX((W * 2.0f) * normalizedShoulderAngle * shoulderStrength);
161 overshoot.setY(0.5f * normalizedShoulderAngle * shoulderStrength);
162
163 return PiecewiseFilmicCurve(maxWhite, point0, point1, overshoot);
164}
165
166const QMatrix4x4 xyz50To65 {QMatrix4x4(
167 0.955473421488075, -0.02309845494876471, 0.06325924320057072, 0.0,
168 -0.0283697093338637, 1.0099953980813041, 0.021041441191917323, 0.0,
169 0.012314014864481998, -0.020507649298898964, 1.330365926242124, 0.0,
170 0.0, 0.0, 0.0, 1.0
171 )};
172
173const QMatrix4x4 xyz50To65_inv = xyz50To65.inverted();
174
175const QMatrix4x4 XYZtoOkLABLMS {QMatrix4x4(
176 0.8190224379967030, 0.3619062600528904, -0.1288737815209879, 0.0,
177 0.0329836539323885, 0.9292868615863434, 0.0361446663506424, 0.0,
178 0.0481771893596242, 0.2642395317527308, 0.6335478284694309, 0.0,
179 0.0, 0.0, 0.0, 1.0
180 )};
181
182const QMatrix4x4 XYZtoOkLABLMS_inv = XYZtoOkLABLMS.inverted();
183
184const QMatrix4x4 lmsToOkLab {QMatrix4x4(
185 0.2104542683093140, 0.7936177747023054, -0.0040720430116193, 0.0,
186 1.9779985324311684, -2.4285922420485799, 0.4505937096174110, 0.0,
187 0.0259040424655478, 0.7827717124575296, -0.8086757549230774, 0.0,
188 0.0, 0.0, 0.0, 1.0
189 )};
190
191const QMatrix4x4 lmsToOkLab_inv = lmsToOkLab.inverted();
192
193const QMatrix4x4 XYZd65toDolbyLMS {QMatrix4x4(
194 0.3592832590121217, 0.6976051147779502, -0.0358915932320290, 0.0,
195 -0.1920808463704993, 1.1004767970374321, 0.0753748658519118, 0.0,
196 0.0070797844607479, 0.0748396662186362, 0.8433265453898765, 0.0,
197 0.0, 0.0, 0.0, 1.0
198 )};
199
200const QMatrix4x4 XYZd65toDolbyLMS_inv = XYZd65toDolbyLMS.inverted();
201
202const QMatrix4x4 lmsToICtCp {QMatrix4x4(
203 2048.0 / 4096.0, 2048.0 / 4096.0, 0.0 , 0.0,
204 6610.0 / 4096.0, -13613.0 / 4096.0, 7003.0 / 4096.0 , 0.0,
205 17933.0 / 4096.0, -17390.0 / 4096.0, -543.0 / 4096.0, 0.0,
206 0.0, 0.0, 0.0, 1.0
207 )};
208
209const QMatrix4x4 lmsToICtCp_inv = lmsToICtCp.inverted();
210
211QVector4D KoGamutMappingUtils::XYZToOkLab(const QVector4D &xyz) {
212 const QVector4D lms = XYZtoOkLABLMS * (xyz50To65 * xyz);
213
214 const QVector4D lmsPrime = QVector4D(cbrtf(lms.x()),
215 cbrtf(lms.y()),
216 cbrtf(lms.z()), 0.0);
217
218 return lmsToOkLab * lmsPrime;
219}
220
221QVector4D KoGamutMappingUtils::OkLabToXYZ(const QVector4D &okLab) {
222 const QVector4D lmsPrime = lmsToOkLab_inv * okLab;
223 const QVector4D lms = QVector4D(powf(lmsPrime.x(), 3.0),
224 powf(lmsPrime.y(), 3.0),
225 powf(lmsPrime.z(), 3.0), 0.0);
226 return xyz50To65_inv * (XYZtoOkLABLMS_inv * lms);
227}
228
229QVector4D KoGamutMappingUtils::XYZToICtCp(const QVector4D &xyz)
230{
231 const QVector4D lms = XYZd65toDolbyLMS * (xyz50To65 * xyz);
232
233 const QVector4D lmsPrime = QVector4D(applySmpte2048Curve(lms.x()),
234 applySmpte2048Curve(lms.y()),
235 applySmpte2048Curve(lms.z()), 0.0);
236
237 return lmsToICtCp * lmsPrime;
238}
239
240QVector4D KoGamutMappingUtils::ICtCpToXYZ(const QVector4D &iCtCp)
241{
242 const QVector4D lmsPrime = lmsToICtCp_inv * iCtCp;
243 const QVector4D lms = QVector4D(removeSmpte2048Curve(lmsPrime.x()),
244 removeSmpte2048Curve(lmsPrime.y()),
245 removeSmpte2048Curve(lmsPrime.z()), 0.0);
246 return xyz50To65_inv * (XYZd65toDolbyLMS_inv * lms);
247}
248
249QMap<float, QPointF> KoGamutMappingUtils::getGamutCuspDescription(const KoColorProfile *profile, KoGamutMappingUtils::MappingModel model, const int samplesPerChannel)
250{
251 QMap<float, QPointF> bounds;
255 QVector<float> xyzChannels(xyzCs->channelCount());
256 QVector<float> labChannels(xyzCs->channelCount());
257
258 QVector<QVector<float>> colorList;
259 if (profile->colorModelID() == RGBAColorModelID.id()) {
260 for (int i = 0; i < samplesPerChannel; i++) {
261 const float ch = float(i)/float(samplesPerChannel-1);
262 QVector<float> c = {ch, 1.0, 0.0, 1.0};
263 colorList << c;
264 QVector<float> c2 = {ch, 0.0, 1.0, 1.0};
265 colorList << c2;
266 }
267 for (int i = 0; i < samplesPerChannel; i++) {
268 const float ch = float(i)/float(samplesPerChannel-1);
269 QVector<float> c = {1.0, ch, 0.0, 1.0};
270 colorList << c;
271 QVector<float> c2 = {0.0, ch, 1.0, 1.0};
272 colorList << c2;
273 }
274 for (int i = 0; i < samplesPerChannel; i++) {
275 const float ch = float(i)/float(samplesPerChannel-1);
276 QVector<float> c = {1.0, 0.0, ch, 1.0};
277 colorList << c;
278 QVector<float> c2 = {0.0, 1.0, ch, 1.0};
279 colorList << c2;
280 }
281 } else if (profile->colorModelID() == CMYKAColorModelID.id()) {
282 for (int i = 0; i < samplesPerChannel; i++) {
283 const float ch = float(i)/float(samplesPerChannel-1);
284 QVector<float> c = {ch, 1.0, 0.0, 0.0, 1.0};
285 colorList << c;
286 QVector<float> c2 = {ch, 0.0, 1.0, 0.0, 1.0};
287 colorList << c2;
288 }
289 for (int i = 0; i < samplesPerChannel; i++) {
290 const float ch = float(i)/float(samplesPerChannel-1);
291 QVector<float> c = {1.0, ch, 0.0, 0.0, 1.0};
292 colorList << c;
293 QVector<float> c2 = {0.0, ch, 1.0, 0.0, 1.0};
294 colorList << c2;
295 }
296 for (int i = 0; i < samplesPerChannel; i++) {
297 const float ch = float(i)/float(samplesPerChannel-1);
298 QVector<float> c = {1.0, 0.0, ch, 0.0, 1.0};
299 colorList << c;
300 QVector<float> c2 = {0.0, 1.0, ch, 0.0, 1.0};
301 colorList << c2;
302 }
303 }
304 KoColor c(cs);
305
306 Q_FOREACH( const QVector<float> channels, colorList) {
307 cs->fromNormalisedChannelsValue(c.data(), channels);
308 std::array<qreal, 3> lch;
309 if (model == LAB) {
310 KoColor labC;
311 cs->convertPixelsTo(c.data(), labC.data(), labCs, 1,
314 labCs->normalisedChannelsValue(labC.data(), labChannels);
315 LabToLCH(labChannels[0], labChannels[1], labChannels[2], &lch[0], &lch[1], &lch[2]);
316 } else {
317
318 KoColor xyzC;
319 cs->convertPixelsTo(c.data(), xyzC.data(), xyzCs, 1,
322 xyzCs->normalisedChannelsValue(xyzC.data(), xyzChannels);
323
324 if (model == ICtCp) {
325 QVector4D ictcp = KoGamutMappingUtils::XYZToICtCp(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));
326 LabToLCH(ictcp.x(), ictcp.y()+0.5, ictcp.z()+0.5, &lch[0], &lch[1], &lch[2]);
327 } else {
328 QVector4D okLab = KoGamutMappingUtils::XYZToOkLab(QVector4D (xyzChannels[0], xyzChannels[1], xyzChannels[2], 0));
329 LabToLCH(okLab.x(), okLab.y()+0.5, okLab.z()+0.5, &lch[0], &lch[1], &lch[2]);
330 }
331 }
332
333 bounds.insert(lch[2], QPointF(lch[1], lch[0]));
334 }
335
336 return bounds;
337}
338
339QPointF KoGamutMappingUtils::cuspForHue(const QMap<float, QPointF> &gamutCuspDescription, const float hue)
340{
341 if (gamutCuspDescription.isEmpty()) return QPointF();
342
343 float firstAngle = 0.0;
344 float secondAngle = 0.0;
345
346 auto it = gamutCuspDescription.upperBound(hue);
347 if (it == gamutCuspDescription.begin()) {
348 firstAngle = gamutCuspDescription.lastKey();
349 secondAngle = gamutCuspDescription.firstKey();
350 } else if (it == gamutCuspDescription.end()) {
351 firstAngle = gamutCuspDescription.lastKey();
352 secondAngle = gamutCuspDescription.firstKey();
353 } else {
354 firstAngle = std::prev(it).key();
355 secondAngle = it.key();
356 }
357
358 const float angle = firstAngle;
359 const float nextAngle = secondAngle;
360
361 QPointF lch1 = gamutCuspDescription.value(angle);
362 QPointF lch2 = gamutCuspDescription.value(nextAngle);
363 const float t = hue < angle
364 ? (hue - angle + 1.0) / (nextAngle + 1.0 - angle)
365 : (hue - angle) / (nextAngle - angle);
366 return QLineF(lch1, lch2).pointAt(t);
367}
368
369
QPointF p0
QPointF p1
void LabToLCH(const qreal l, const qreal a, const qreal b, qreal *L, qreal *C, qreal *H)
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID XYZAColorModelID("XYZA", ki18n("XYZ/Alpha"))
const KoID Integer8BitsColorDepthID("U8", ki18n("8-bit integer/channel"))
const KoID CMYKAColorModelID("CMYKA", ki18n("CMYK/Alpha"))
const KoID LABAColorModelID("LABA", ki18n("L*a*b*/Alpha"))
const KoID RGBAColorModelID("RGBA", ki18n("RGB/Alpha"))
ALWAYS_INLINE float applySmpte2048Curve(float x, float refWhite=203.0) noexcept
ALWAYS_INLINE float removeSmpte2048Curve(float x, float refWhite=203.0) noexcept
const QMatrix4x4 XYZtoOkLABLMS
const QMatrix4x4 xyz50To65_inv
static void AsSlopeIntercept(float &m, float &b, float x0, float x1, float y0, float y1)
static float EvalDerivativeLinearGamma(float m, float b, float g, float x)
const QMatrix4x4 XYZd65toDolbyLMS
const QMatrix4x4 xyz50To65
const QMatrix4x4 XYZtoOkLABLMS_inv
static void SolveAB(float &lnA, float &B, float x0, float y0, float m)
const QMatrix4x4 XYZd65toDolbyLMS_inv
const QMatrix4x4 lmsToOkLab_inv
const QMatrix4x4 lmsToICtCp
const QMatrix4x4 lmsToOkLab
const QMatrix4x4 lmsToICtCp_inv
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
quint8 * data()
Definition KoColor.h:144
QString id() const
Definition KoID.cpp:63
#define bounds(x, a, b)
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)
QMap< float, QPointF > KRITAPIGMENT_EXPORT getGamutCuspDescription(const KoColorProfile *profile, KoGamutMappingUtils::MappingModel model, const int samplesPerChannel=5)
getGamutCuspDescription Generates a hexagonal gamut cusp description from a RGB or CMYK profile.
QVector4D KRITAPIGMENT_EXPORT XYZToICtCp(const QVector4D &xyz)
XYZToICtCp Ad-hoc implementation of ICtCp for use in filters. Unlike the official YCbCr-compatible st...
MappingModel
The MappingModel enum This represents the preferred model to do gamut and/or tone mapping in.
QVector4D KRITAPIGMENT_EXPORT XYZToOkLab(const QVector4D &xyz)
XYZToOkLab Ad-hoc implementation of OkLAB. This takes a QVector4D and transforms it to the OkLAB mode...
float KRITAPIGMENT_EXPORT reinhardToneMapping(const float linearY, const float maxY)
reinhardToneMapping Implements 'extended' reinhard tonemapping.
QPointF KRITAPIGMENT_EXPORT cuspForHue(const QMap< float, QPointF > &gamutCuspDescription, const float hue)
virtual QString colorModelID() const
const KoColorSpace * colorSpace(const QString &colorModelId, const QString &colorDepthId, const KoColorProfile *profile)
static KoColorSpaceRegistry * instance()
The piecewiseFilmicCurve class Implement's Hable's piecewise filmic curve. https://filmicworlds....
static PiecewiseFilmicCurve fromUserParams(float toeStrength=0.5, float toeLength=0.5, float shoulderStrength=2.0, float shoulderLength=0.5, float shoulderAngle=1.0)
PiecewiseFilmicCurve(float maxWhite, QPointF point0, QPointF point1, QPointF overshootPoint)