Krita Source Code Documentation
Loading...
Searching...
No Matches
KisToneGamutMapFilter.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 */
7
12
15#include <KoColorProfile.h>
17
20
21#include <kpluginfactory.h>
22#include <klocalizedstring.h>
23#include <KoGamutMappingUtils.h>
24
25
26#include "ui_kistonegamutmapwidget.h"
27
28const QString MAPPING_MODEL = "mapping_model";
29const QString TONE_MAP_ENABLED = "toneMap/Enabled";
30const QString TONE_MAP_MAX_WHITE = "toneMap/reinhard/MaxWhite";
31const QString GAMUT_MAP_ENABLED = "gamutMap/Enabled";
32const QString GAMUT_MAP_TARGET_PROFILE = "gamutMap/TargetProfile";
33const QString GAMUT_MAP_METHOD = "gamutMap/method";
34
35const QString TONE_MAP_TYPE = "toneMap/CurveType";
36
37const QString TONE_PIECE_WISE_TOE_STRENGTH = "toneMap/pieceWiseFilmic/toeStrength";
38const QString TONE_PIECE_WISE_TOE_LENGTH = "toneMap/pieceWiseFilmic/toeLength";
39const QString TONE_PIECE_WISE_SHOULDER_STRENGTH = "toneMap/pieceWiseFilmic/shoulderStrength";
40const QString TONE_PIECE_WISE_SHOULDER_LENGTH = "toneMap/pieceWiseFilmic/shoulderLength";
41const QString TONE_PIECE_WISE_SHOULDER_ANGLE = "toneMap/pieceWiseFilmic/shoulderAngle";
42
43K_PLUGIN_FACTORY_WITH_JSON(KisToneGamutMapFilterFactory, "kritatonegamutmapfilter.json", registerPlugin<KisToneGamutMapFilterPlugin>();)
44
45KisToneGamutMapFilterPlugin::KisToneGamutMapFilterPlugin(QObject *parent, const QVariantList &): QObject(parent)
46{
48}
49
54
70
72{
74 if (config->getProperty(MAPPING_MODEL).toInt() != KoGamutMappingUtils::LAB) {
76 }
78
79 if (config->getProperty(TONE_MAP_ENABLED).toBool()) {
80 QHash<QString, QVariant> params;
81 params["CurveType"] = config->getProperty(TONE_MAP_TYPE);
82
83 params["maxWhite"] = config->getProperty(TONE_MAP_MAX_WHITE);
84
85 params["toeStrength"] = config->getProperty(TONE_PIECE_WISE_TOE_STRENGTH);
86 params["toeLength"] = config->getProperty(TONE_PIECE_WISE_TOE_LENGTH);
87 params["shoulderStrength"] = config->getProperty(TONE_PIECE_WISE_SHOULDER_STRENGTH);
88 params["shoulderLength"] = config->getProperty(TONE_PIECE_WISE_SHOULDER_LENGTH);
89 params["shoulderAngle"] = config->getProperty(TONE_PIECE_WISE_SHOULDER_ANGLE);
90 KoColorTransformation *toneMap = mappingCs->createColorTransformation("tonemap_adjustment", params);
91 allTransforms << toneMap;
92 }
93
94 if (config->getProperty(GAMUT_MAP_ENABLED).toBool()) {
95 QHash<QString, QVariant> params;
96 params["targetColorProfileName"] = config->getProperty(GAMUT_MAP_TARGET_PROFILE);
97
98 const KoColorProfile *targetProfile = KoColorSpaceRegistry::instance()->profileByName(config->getProperty(GAMUT_MAP_TARGET_PROFILE).toString());
99 if (targetProfile && config->getProperty(GAMUT_MAP_METHOD).toString() == "CUSP_MAP") {
100 QVariantMap cuspDescription;
101 QMap<float, QPointF> tempCuspDescription = KoGamutMappingUtils::getGamutCuspDescription(targetProfile, KoGamutMappingUtils::MappingModel(config->getProperty(MAPPING_MODEL).toInt()));
102 if (!tempCuspDescription.isEmpty()) {
103 Q_FOREACH(const float key, tempCuspDescription.keys()) {
104 cuspDescription.insert(QString::number(key), tempCuspDescription.value(key));
105 }
106 params["cuspDescription"] = cuspDescription;
107 }
108 }
109
110 params["mappingModel"] = config->getProperty(MAPPING_MODEL);
111 KoColorTransformation *gamutMap = mappingCs->createColorTransformation("gamutmap_clip_adjustment", params);
112 allTransforms << gamutMap;
113 }
114
115 if (allTransforms.isEmpty()) return nullptr;
116
118}
119
121{
122 KisFilterConfigurationSP config = factoryConfiguration(resourcesInterface);
123 config->setProperty(TONE_MAP_ENABLED, true);
124 config->setProperty(TONE_MAP_MAX_WHITE, 1.0);
125
126 config->setProperty(TONE_PIECE_WISE_TOE_STRENGTH, 0.5);
127 config->setProperty(TONE_PIECE_WISE_TOE_LENGTH, 0.5);
128 config->setProperty(TONE_PIECE_WISE_SHOULDER_STRENGTH, 0.5);
129 config->setProperty(TONE_PIECE_WISE_SHOULDER_LENGTH, 2.0);
130 config->setProperty(TONE_PIECE_WISE_SHOULDER_ANGLE, 1.0);
131
132 config->setProperty(GAMUT_MAP_ENABLED, true);
133 config->setProperty(GAMUT_MAP_METHOD, "CUSP_MAP");
134 config->setProperty(GAMUT_MAP_TARGET_PROFILE, "sRGB-elle-V2-srgbtrc.icc");
135 return config;
136}
137
139{
140 Q_UNUSED(useForMasks)
141 return new KisToneGamutMapWidget(parent, dev);
142}
143
144
146 : KisConfigWidget(parent)
147 , ui(new Ui::KisToneGamutMapWidget)
148{
149 Q_ASSERT(dev);
150 ui->setupUi(this);
151
152 ui->targetGamut->showDepth(false);
153 ui->targetGamut->showColorBrowserButton(false);
154
155 ui->cmbMappingModel->addItem(i18n("LAB"), KoGamutMappingUtils::LAB);
156 ui->cmbMappingModel->addItem(i18n("XYZ (ICtCp)"), KoGamutMappingUtils::ICtCp);
157 ui->cmbMappingModel->addItem(i18n("XYZ (OkLAB)"), KoGamutMappingUtils::OkLAB);
158
159 ui->cmbToneCurveType->addItem(i18n("Reinhard"), KoGamutMappingUtils::Reinhard);
160 ui->cmbToneCurveType->addItem(i18n("Piecewise Filmic"), KoGamutMappingUtils::PiecewiseFilmic);
161
162 ui->cmbGamutMapMethod->addItem(i18n("Use Chroma-Lightness Cusp Map"), "CUSP_MAP");
163 ui->cmbGamutMapMethod->addItem(i18n("Minimum Delta E"), "MINDE");
164
165 ui->cmbGamutMapMethod->setItemData(0, i18nc("@info:tooltip", "Use a chroma-lightness cusp map for a faster,"
166 " if more inaccurate gamut mapping. Only works for RGB and CMYK profiles,"
167 " will fall back on Minimum Delta E."), Qt::ToolTipRole);
168 ui->cmbGamutMapMethod->setItemData(1, i18nc("@info:tooltip", "Will use the Minimum Delta E method,"
169 " where colors are brought into gamut until they minimally different from the transformed color."
170 " Slow but accurate."), Qt::ToolTipRole);
171
172 connect(ui->cmbMappingModel, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));
173 connect(ui->cmbToneCurveType, SIGNAL(activated(int)), ui->stkToneMap, SLOT(setCurrentIndex(int)));
174 connect(ui->cmbToneCurveType, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));
175
176 connect(ui->chkToneMap, SIGNAL(toggled(bool)), SIGNAL(sigConfigurationItemChanged()));
177
178 connect(ui->spnMaxWhite, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
179
180 connect(ui->spnToeStrength, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
181 connect(ui->spnToeLength, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
182 connect(ui->spnShoulderStrength, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
183 connect(ui->spnShoulderLength, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
184 connect(ui->spnShoulderAngle, SIGNAL(valueChanged(double)), SIGNAL(sigConfigurationItemChanged()));
185
186 connect(ui->chkGamutMap, SIGNAL(toggled(bool)), SIGNAL(sigConfigurationItemChanged()));
187 connect(ui->targetGamut, SIGNAL(colorSpaceChanged(const KoColorSpace*)), SIGNAL(sigConfigurationItemChanged()));
188 connect(ui->cmbGamutMapMethod, SIGNAL(activated(int)), SIGNAL(sigConfigurationItemChanged()));
189
190 QStringList yAxisValues;
191 for (int i = 0; i < 10; i++) {
192 yAxisValues << QString::number(float(i)/10.0, 'f', 1);
193 }
194 ui->toneCurvePreview->setYAxisValues(yAxisValues, 64);
195
196}
197
202
204{
205 QVariant value;
206
207 if (config->getProperty(MAPPING_MODEL, value)) {
208 ui->cmbMappingModel->setCurrentIndex(ui->cmbMappingModel->findData(value));
209 }
210 if (config->getProperty(TONE_MAP_TYPE, value)) {
211 ui->cmbToneCurveType->setCurrentIndex(ui->cmbToneCurveType->findData(value));
212 ui->stkToneMap->setCurrentIndex(ui->cmbToneCurveType->currentIndex());
213 }
214
215 if (config->getProperty(TONE_MAP_ENABLED, value)) {
216 ui->chkToneMap->setChecked(value.toBool());
217 }
218 if (config->getProperty(TONE_MAP_MAX_WHITE, value)) {
219 ui->spnMaxWhite->setValue(value.toDouble());
220 }
221 if (config->getProperty(TONE_PIECE_WISE_TOE_STRENGTH, value)) {
222 ui->spnToeStrength->setValue(value.toDouble());
223 }
224 if (config->getProperty(TONE_PIECE_WISE_TOE_LENGTH, value)) {
225 ui->spnToeLength->setValue(value.toDouble());
226 }
227 if (config->getProperty(TONE_PIECE_WISE_SHOULDER_STRENGTH, value)) {
228 ui->spnShoulderStrength->setValue(value.toDouble());
229 }
230 if (config->getProperty(TONE_PIECE_WISE_SHOULDER_LENGTH, value)) {
231 ui->spnShoulderLength->setValue(value.toDouble());
232 }
233 if (config->getProperty(TONE_PIECE_WISE_SHOULDER_ANGLE, value)) {
234 ui->spnShoulderAngle->setValue(value.toDouble());
235 }
236
237
238 if (config->getProperty(GAMUT_MAP_ENABLED, value)) {
239 ui->chkGamutMap->setChecked(value.toBool());
240 }
241 if (config->getProperty(GAMUT_MAP_TARGET_PROFILE, value)) {
243 if (profile) {
244 ui->targetGamut->setCurrentColorSpace(
245 KoColorSpaceRegistry::instance()->colorSpace(profile->colorModelID(),
247 profile));
248 }
249 }
250 int idx = ui->cmbGamutMapMethod->findData(config->getProperty(GAMUT_MAP_METHOD));
251 ui->cmbGamutMapMethod->setCurrentIndex(idx);
253}
254
256{
258 config->setProperty(MAPPING_MODEL, ui->cmbMappingModel->currentData());
259
260 config->setProperty(TONE_MAP_ENABLED, ui->chkToneMap->isChecked());
261 config->setProperty(TONE_MAP_MAX_WHITE, ui->spnMaxWhite->value());
262
263 config->setProperty(TONE_MAP_TYPE, ui->cmbToneCurveType->currentData());
264
265 config->setProperty(TONE_PIECE_WISE_TOE_STRENGTH, ui->spnToeStrength->value());
266 config->setProperty(TONE_PIECE_WISE_TOE_LENGTH, ui->spnToeLength->value());
267 config->setProperty(TONE_PIECE_WISE_SHOULDER_STRENGTH, ui->spnShoulderStrength->value());
268 config->setProperty(TONE_PIECE_WISE_SHOULDER_LENGTH, ui->spnShoulderLength->value());
269 config->setProperty(TONE_PIECE_WISE_SHOULDER_ANGLE, ui->spnShoulderAngle->value());
270
271
272 if (ui->cmbToneCurveType->currentData() == KoGamutMappingUtils::PiecewiseFilmic) {
274 = KoGamutMappingUtils::PiecewiseFilmicCurve::fromUserParams(ui->spnToeStrength->value(), ui->spnToeLength->value(), ui->spnShoulderStrength->value(), ui->spnShoulderLength->value(), ui->spnShoulderAngle->value());
275 QPolygonF poly;
276 const float white = 1.0/curve.inverseMaxWhite;
277 QStringList xAxisValues;
278 for (int x = 0; x < 20; x++) {
279 const float val = (float(x)/19.0)*white;
280 const float y = curve.evalCurve(val);
281 if (x%2 == 0) {
282 xAxisValues << QString::number(val, 'f', 1);
283
284 }
285 poly.append(QPointF(val/white, y));
286 }
287 ui->toneCurvePreview->setGreyscaleCurve(poly);
288 ui->toneCurvePreview->setProfileDataAvailable(true);
289 ui->toneCurvePreview->setToolTip(i18n("Current Maximum White: %1", QString::number(white, 'f', 2)));
290 ui->toneCurvePreview->setXAxisValues(xAxisValues);
291 ui->toneCurvePreview->update();
292 } else {
293 QPolygonF poly;
294 const float white = ui->spnMaxWhite->value();
295 QStringList xAxisValues;
296 for (int x = 0; x < 20; x++) {
297 const float val = (float(x)/19.0)*white;
298 const float y = KoGamutMappingUtils::reinhardToneMapping(val, white);
299 if (x%2 == 0) {
300 xAxisValues << QString::number(val, 'f', 1);
301 }
302 poly.append(QPointF(val/white, y));
303 }
304 ui->toneCurvePreview->setGreyscaleCurve(poly);
305 ui->toneCurvePreview->setProfileDataAvailable(true);
306 ui->toneCurvePreview->setToolTip(i18n("Current Maximum White: %1", QString::number(white, 'f', 2)));
307 ui->toneCurvePreview->setXAxisValues(xAxisValues);
308 ui->toneCurvePreview->update();
309 }
310
311 config->setProperty(GAMUT_MAP_ENABLED, ui->chkGamutMap->isChecked());
312 if (ui->targetGamut->currentColorSpace()) {
313 config->setProperty(GAMUT_MAP_TARGET_PROFILE, ui->targetGamut->currentColorSpace()->profile()->name());
314 }
315 config->setProperty(GAMUT_MAP_METHOD, ui->cmbGamutMapMethod->currentData());
316 return config;
317}
318
319
320#include "KisToneGamutMapFilter.moc"
float value(const T *src, size_t ch)
const QString GAMUT_MAP_METHOD
const QString GAMUT_MAP_ENABLED
bool, whether to do gamt mapping.
const QString TONE_PIECE_WISE_SHOULDER_ANGLE
const QString MAPPING_MODEL
Enum, model in which the gamut or tone mapping is done.
const QString TONE_MAP_MAX_WHITE
double, the maximum white that needs to be tonemapped.
const QString TONE_PIECE_WISE_SHOULDER_STRENGTH
const QString GAMUT_MAP_TARGET_PROFILE
QString, name of the color profile that is the target gamut.
const QString TONE_PIECE_WISE_TOE_LENGTH
const QString TONE_MAP_ENABLED
bool, whether to do tone mapping.
const QString TONE_PIECE_WISE_SHOULDER_LENGTH
const QString TONE_MAP_TYPE
String, indicates whether to use the cusp map or the MINDE method.
const QString TONE_PIECE_WISE_TOE_STRENGTH
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
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"))
KisFilterConfigurationSP factoryConfiguration(KisResourcesInterfaceSP resourcesInterface) const override
void sigConfigurationItemChanged()
void add(KisFilterSP item)
static KisFilterRegistry * instance()
static KisResourcesInterfaceSP instance()
KisToneGamutMapFilterPlugin(QObject *parent, const QVariantList &)
KoColorTransformation * createTransformation(const KoColorSpace *cs, const KisFilterConfigurationSP config) const override
KisConfigWidget * createConfigurationWidget(QWidget *parent, const KisPaintDeviceSP dev, bool useForMasks) const override
KisFilterConfigurationSP defaultConfiguration(KisResourcesInterfaceSP resourcesInterface) const override
KisPropertiesConfigurationSP configuration() const
void setConfiguration(const KisPropertiesConfigurationSP config)
KisToneGamutMapWidget(QWidget *parent, KisPaintDeviceSP dev)
Ui::KisToneGamutMapWidget * ui
KoColorTransformation * createColorTransformation(const QString &id, const QHash< QString, QVariant > &parameters) const
QString id() const
Definition KoID.cpp:63
K_PLUGIN_FACTORY_WITH_JSON(KritaASCCDLFactory, "kritaasccdl.json", registerPlugin< KritaASCCDL >();) KritaASCCDL
const KoID FiltersCategoryColorId("color_filters", ki18nc("The category of color transfer filters, like color to alpha. Noun.", "Colors"))
KisSharedPtr< KisFilter > KisFilterSP
Definition kis_types.h:167
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.
MappingModel
The MappingModel enum This represents the preferred model to do gamut and/or tone mapping in.
float KRITAPIGMENT_EXPORT reinhardToneMapping(const float linearY, const float maxY)
reinhardToneMapping Implements 'extended' reinhard tonemapping.
void setSupportsThreading(bool v)
void setSupportsAdjustmentLayers(bool v)
void setSupportsPainting(bool v)
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()
static KoColorTransformation * createOptimizedCompositeTransform(const QVector< KoColorTransformation * > transforms)
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)