Krita Source Code Documentation
Loading...
Searching...
No Matches
KisLevelsCurve.cpp
Go to the documentation of this file.
1/*
2 * KDE. Krita Project.
3 *
4 * SPDX-FileCopyrightText: 2021 Deif Lou <ginoba@gmail.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include <cmath>
10
11#include <kis_dom_utils.h>
12
13#include "KisLevelsCurve.h"
14
16 : KisLevelsCurve(defaultInputBlackPoint(), defaultInputWhitePoint(), defaultInputGamma(),
17 defaultOutputBlackPoint(), defaultOutputWhitePoint())
18{}
19
20KisLevelsCurve::KisLevelsCurve(qreal inputBlackPoint, qreal inputWhitePoint, qreal inputGamma,
21 qreal outputBlackPoint, qreal outputWhitePoint)
22 : m_inputBlackPoint(inputBlackPoint)
23 , m_inputWhitePoint(inputWhitePoint)
24 , m_inputGamma(inputGamma)
25 , m_outputBlackPoint(outputBlackPoint)
26 , m_outputWhitePoint(outputWhitePoint)
27 , m_inputLevelsDelta(inputWhitePoint - inputBlackPoint)
28 , m_inverseInputGamma(1.0 / m_inputGamma)
29 , m_outputLevelsDelta(outputWhitePoint - outputBlackPoint)
30 , m_mustRecomputeU16Transfer(true)
31 , m_mustRecomputeFTransfer(true)
32{}
33
36{
37 fromString(text);
38}
39
41{
42 return
43 &rhs == this ||
44 (
50 );
51}
52
53qreal KisLevelsCurve::value(qreal x) const
54{
55 if (x <= m_inputBlackPoint)
56 return m_outputBlackPoint;
57 else if (x < m_inputWhitePoint) {
59 } else {
60 return m_outputWhitePoint;
61 }
62}
63
65{
66 return m_inputBlackPoint;
67}
68
70{
71 return m_inputWhitePoint;
72}
73
75{
76 return m_inputGamma;
77}
78
83
88
89void KisLevelsCurve::setInputBlackPoint(qreal newInputBlackPoint)
90{
91 m_inputBlackPoint = newInputBlackPoint;
93 invalidate();
94}
95
96void KisLevelsCurve::setInputWhitePoint(qreal newInputWhitePoint)
97{
98 m_inputWhitePoint = newInputWhitePoint;
100 invalidate();
101}
102
103void KisLevelsCurve::setInputGamma(qreal newInputGamma)
104{
105 m_inputGamma = newInputGamma;
107 invalidate();
108}
109
110void KisLevelsCurve::setOutputBlackPoint(qreal newOutputBlackPoint)
111{
112 m_outputBlackPoint = newOutputBlackPoint;
114 invalidate();
115}
116
117void KisLevelsCurve::setOutputWhitePoint(qreal newOutputWhitePoint)
118{
119 m_outputWhitePoint = newOutputWhitePoint;
121 invalidate();
122}
123
136
146
154
156{
157 return
158 m_inputBlackPoint == 0.0 &&
159 m_inputWhitePoint == 1.0 &&
160 m_inputGamma == 1.0 &&
161 m_outputBlackPoint == 0.0 &&
162 m_outputWhitePoint == 1.0;
163}
164
165const QString& KisLevelsCurve::name() const
166{
167 return m_name;
168}
169
170void KisLevelsCurve::setName(const QString &newName)
171{
172 m_name = newName;
173}
174
176{
177
178 if (!m_mustRecomputeU16Transfer && size == m_u16Transfer.size()) {
179 return m_u16Transfer;
180 }
181
182 m_u16Transfer.resize(size);
183
184 for (int i = 0; i < size; ++i) {
185 const qreal x = static_cast<qreal>(i) / static_cast<qreal>(size - 1);
186 m_u16Transfer[i] = static_cast<quint16>(qRound(value(x) * static_cast<qreal>(0xFFFF)));
187 }
188
190 return m_u16Transfer;
191}
192
194{
195 if (!m_mustRecomputeFTransfer && size == m_fTransfer.size()) {
196 return m_fTransfer;
197 }
198
199 m_fTransfer.resize(size);
200
201 for (int i = 0; i < size; ++i) {
202 m_fTransfer[i] = value(static_cast<qreal>(i) / static_cast<qreal>(size - 1));
203 }
204
206 return m_fTransfer;
207}
208
218
219void KisLevelsCurve::fromString(const QString &text, bool *ok)
220{
221 KIS_SAFE_ASSERT_RECOVER(!text.isEmpty()) {
222 resetAll();
223 if (ok) {
224 *ok = false;
225 }
226 return;
227 }
228
229 const QStringList data = text.split(';');
230 KIS_SAFE_ASSERT_RECOVER(data.size() == 5) {
231 resetAll();
232 if (ok) {
233 *ok = false;
234 }
235 return;
236 }
237
238 qreal values[5];
239 bool ok_;
240 for (int i = 0; i < 5; ++i) {
241 ok_ = false;
242 values[i] = KisDomUtils::toDouble(data.at(i), &ok_);
244 resetAll();
245 if (ok) {
246 *ok = false;
247 }
248 return;
249 }
250 }
251
252 m_inputBlackPoint = values[0];
253 m_inputWhitePoint = values[1];
254 m_inputGamma = values[2];
255 m_outputBlackPoint = values[3];
256 m_outputWhitePoint = values[4];
260 invalidate();
261
262 if (ok) {
263 *ok = true;
264 }
265}
266
This class holds the parameters for a levels adjustment. It is modeled after KisCubicCurve and has si...
qreal outputBlackPoint() const
Get the output black point.
bool operator==(const KisLevelsCurve &rhs) const
void setOutputBlackPoint(qreal newOutputBlackPoint)
Set the output black point.
bool isIdentity() const
Check whether the level info maps all values to themselves.
const QVector< qreal > & floatTransfer(int size=256) const
Returns a vector of size.
qreal inputBlackPoint() const
Get the input black point.
void setOutputWhitePoint(qreal newOutputWhitePoint)
Set the output white point.
static constexpr qreal defaultInputGamma()
Default value for the input gamma.
qreal value(qreal x) const
Evaluates the function formed by the levels parameters for a given x. The input and output values are...
static constexpr qreal defaultOutputBlackPoint()
Default value for the output black point.
QVector< qreal > m_fTransfer
qreal inputGamma() const
Get the gamma value.
void setName(const QString &newName)
Set the name associated with this levels info object. This allows us to carry around a display name f...
void resetOutputLevels()
Resets the output levels only.
const QVector< quint16 > & uint16Transfer(int size=256) const
Returns a vector of size.
qreal inputWhitePoint() const
Get the input white point.
static constexpr qreal defaultOutputWhitePoint()
Default value for the output white point.
bool m_mustRecomputeFTransfer
void setInputGamma(qreal newInputGamma)
Set the gamma value.
const QString & name() const
Get the name associated with this levels info object.
void setInputWhitePoint(qreal newInputWhitePoint)
Set the input white point.
QVector< quint16 > m_u16Transfer
QString toString() const
Get a text representation of the parameters. The format is: "input_black_point;input_white_point;inpu...
void resetInputLevels()
Resets the input levels only (and gamma)
void fromString(const QString &text, bool *ok=nullptr)
Parses the parameters from a given text.
qreal outputWhitePoint() const
Get the output white point.
void setInputBlackPoint(qreal newInputBlackPoint)
Set the input black point.
void resetAll()
Resets the input and output levels (and gamma)
bool m_mustRecomputeU16Transfer
static constexpr qreal defaultInputWhitePoint()
Default value for the input white point.
static constexpr qreal defaultInputBlackPoint()
Default value for the input black point.
#define KIS_SAFE_ASSERT_RECOVER(cond)
Definition kis_assert.h:126
double toDouble(const QString &str, bool *ok=nullptr)
QString toString(const QString &value)