Krita Source Code Documentation
Loading...
Searching...
No Matches
kis_tonemap_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
9
10#include <KoColorSpace.h>
13#include <KoID.h>
14
15#include <KoGamutMappingUtils.h>
16
17#include <kis_debug.h>
18
19namespace {
20
21struct XYZLuminanceModel {
22 static ALWAYS_INLINE float luminance (const float *xyzChannels) {
23 return xyzChannels[1];
24 }
25
26 static ALWAYS_INLINE void setLuminance(float *xyzChannels, const float luminance) {
27 const float sum = xyzChannels[0] + xyzChannels[1] + xyzChannels[2];
28 const float x = xyzChannels[0] / sum;
29 const float y = xyzChannels[1] / sum;
30 const float Y = luminance;
31 xyzChannels[0] = (x*Y)/y;
32 xyzChannels[1] = Y;
33 xyzChannels[2] = ((1.0-x-y)*Y)/y;
34 }
35};
36
37struct LABLuminanceModel {
38 static ALWAYS_INLINE float luminance (const float *labChannels) {
39 return powf(labChannels[0], 3.0);
40 }
41
42 static ALWAYS_INLINE void setLuminance(float *labChannels, const float luminance) {
43 labChannels[0] = cbrtf(luminance);
44 }
45};
46
47}
48
49template <class LuminanceModel>
51{
52public:
53 KisReinhardToneMapAdjustment(const KoColorSpace *sourceSpace): m_sourceSpace(sourceSpace) {}
54
55 void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
56 {
57 if (!m_sourceSpace) {
58 qWarning() << "no source space!";
59 return;
60 }
61
62 QVector<float> originalChannels(m_sourceSpace->channelCount());
63 std::unique_ptr<quint8[]> tmpOriginal(new quint8[m_sourceSpace->pixelSize()]);
64 while (nPixels > 0) {
65
66 m_sourceSpace->normalisedChannelsValue(srcU8, originalChannels);
67
68 float Y = LuminanceModel::luminance(originalChannels.data());
70 LuminanceModel::setLuminance(originalChannels.data(), Y);
71
72 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
73 memcpy(dstU8, tmpOriginal.get(), m_sourceSpace->pixelSize());
74
75 --nPixels;
76 srcU8 += m_sourceSpace->pixelSize();
77 dstU8 += m_sourceSpace->pixelSize();
78 }
79 }
80
81 QList<QString> parameters() const override
82 {
83 QList<QString> list;
84 list << "maxWhite";
85 return list;
86 }
87
88 int parameterId(const QString& name) const override
89 {
90 if (name == "maxWhite") {
91 return 0;
92 }
93 return -1;
94 }
95
96 void setParameter(int id, const QVariant& parameter) override
97 {
98 switch(id)
99 {
100 case 0:
101 m_maxWhite = parameter.toFloat();
102 break;
103 default:
104 ;
105 }
106 }
107
108private:
110 float m_maxWhite{1.0};
111
112};
113
114template <class LuminanceModel>
116{
117public:
119
120 void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
121 {
122 if (!m_sourceSpace) {
123 qWarning() << "no source space!";
124 return;
125 }
126
129
130 QVector<float> originalChannels(m_sourceSpace->channelCount());
131 std::unique_ptr<quint8[]> tmpOriginal(new quint8[m_sourceSpace->pixelSize()]);
132 while (nPixels > 0) {
133 m_sourceSpace->normalisedChannelsValue(srcU8, originalChannels);
134
135 float Y = LuminanceModel::luminance(originalChannels.data());
136 Y = curve.evalCurve(Y);
137 LuminanceModel::setLuminance(originalChannels.data(), Y);
138
139 m_sourceSpace->fromNormalisedChannelsValue(tmpOriginal.get(), originalChannels);
140 memcpy(dstU8, tmpOriginal.get(), m_sourceSpace->pixelSize());
141
142 --nPixels;
143 srcU8 += m_sourceSpace->pixelSize();
144 dstU8 += m_sourceSpace->pixelSize();
145 }
146 }
147
148 QList<QString> parameters() const override
149 {
150 QList<QString> list;
151 list << "toeStrength";
152 list << "toeLength";
153 list << "shoulderStrength";
154 list << "shoulderLength";
155 list << "shoulderAngle";
156 return list;
157 }
158
159 int parameterId(const QString& name) const override
160 {
161 if (name == "toeStrength") {
162 return 0;
163 } else if (name == "toeLength") {
164 return 1;
165 } else if (name == "shoulderStrength") {
166 return 2;
167 } else if (name == "shoulderLength") {
168 return 3;
169 } else if (name == "shoulderAngle") {
170 return 4;
171 }
172 return -1;
173 }
174
175 void setParameter(int id, const QVariant& parameter) override
176 {
177 switch(id)
178 {
179 case 0:
180 m_toeStrength = parameter.toFloat();
181 break;
182 case 1:
183 m_toeLength = parameter.toFloat();
184 break;
185 case 2:
186 m_shoulderStrength = parameter.toFloat();
187 break;
188 case 3:
189 m_shoulderLength = parameter.toFloat();
190 break;
191 case 4:
192 m_shoulderAngle = parameter.toFloat();
193 break;
194 default:
195 ;
196 }
197 }
198
199private:
201 float m_toeStrength{0.5};
202 float m_toeLength{0.5};
205 float m_shoulderAngle{1.0};
206
207};
208
214
216{
218 // TODO: consider 16bit float.
219 l.append(QPair< KoID, KoID >(XYZAColorModelID , Float32BitsColorDepthID));
220 l.append(QPair< KoID, KoID >(LABAColorModelID , Float32BitsColorDepthID));
221 return l;
222}
223
224KoColorTransformation *KisToneMapAdjustmentFactory::createTransformation(const KoColorSpace *colorSpace, QHash<QString, QVariant> parameters) const
225{
227
228 if (colorSpace->colorModelId() == XYZAColorModelID) {
229 if (parameters["CurveType"].toInt() == KoGamutMappingUtils::Reinhard) {
231 } else {
233 }
234 } else if (colorSpace->colorModelId() == LABAColorModelID) {
235 if (parameters["CurveType"].toInt() == KoGamutMappingUtils::Reinhard) {
237 } else {
239 }
240 } else {
241 dbgKrita << "Unsupported color space " << colorSpace->id() << " in KisGamutMapClipAdjustmentFactory::createTransformation";
242 return nullptr;
243 }
244
245 adj->setParameters(parameters);
246 return adj;
247}
#define ALWAYS_INLINE
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID XYZAColorModelID("XYZA", ki18n("XYZ/Alpha"))
const KoID LABAColorModelID("LABA", ki18n("L*a*b*/Alpha"))
int parameterId(const QString &name) const override
KisPiecewiseFilmicToneMapAdjustment(const KoColorSpace *sourceSpace)
QList< QString > parameters() const override
void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
void setParameter(int id, const QVariant &parameter) override
void setParameter(int id, const QVariant &parameter) override
void transform(const quint8 *srcU8, quint8 *dstU8, qint32 nPixels) const override
KisReinhardToneMapAdjustment(const KoColorSpace *sourceSpace)
int parameterId(const QString &name) const override
QList< QString > parameters() const override
QList< QPair< KoID, KoID > > supportedModels() const override
KoColorTransformation * createTransformation(const KoColorSpace *colorSpace, QHash< QString, QVariant > 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
void setParameters(const QHash< QString, QVariant > &parameters)
#define dbgKrita
Definition kis_debug.h:49
const QString Y
float KRITAPIGMENT_EXPORT reinhardToneMapping(const float linearY, const float maxY)
reinhardToneMapping Implements 'extended' reinhard tonemapping.
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)