KSaneCore

gammaoption.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
3 * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8#include "gammaoption.h"
9
10#include <QVarLengthArray>
11
12#include <ksanecore_debug.h>
13
14#include <cmath>
15
16namespace KSaneCore
17{
18
19GammaOption::GammaOption(const SANE_Handle handle, const int index)
20 : BaseOption(handle, index)
21{
22 m_optionType = Option::TypeGamma;
23}
24
25bool GammaOption::setValue(const QVariant &value)
26{
27 if (state() == Option::StateHidden) {
28 return false;
29 }
30 if (value.userType() == QMetaType::QString) {
31 const QString stringValue = value.toString();
32 QStringList gammaValues;
33 int brightness;
34 int contrast;
35 int gamma;
36 bool ok = true;
37
38 gammaValues = stringValue.split(QLatin1Char(':'));
39 if (gammaValues.size() != 3) {
40 return false;
41 }
42 brightness = gammaValues.at(0).toInt(&ok);
43 if (ok) {
44 contrast = gammaValues.at(1).toInt(&ok);
45 }
46 if (ok) {
47 gamma = gammaValues.at(2).toInt(&ok);
48 }
49
50 if (ok && (m_brightness != brightness || m_contrast != contrast || m_gamma != gamma) ) {
51 m_brightness = brightness;
52 m_contrast = contrast;
53 m_gamma = gamma;
54 calculateGTwriteData();
55 }
56 return true;
57 }
58 if (value.canConvert<QVariantList>()) { // It's a list
59 QVariantList copy = value.toList();
60 if (copy.size() != 3 || copy.at(0).userType() != QMetaType::Int || copy.at(1).userType() != QMetaType::Int || copy.at(2).userType() != QMetaType::Int) {
61 return false;
62 }
63 if (m_brightness != copy.at(0).toInt() || m_contrast != copy.at(1).toInt() || m_gamma != copy.at(2).toInt() ) {
64 m_brightness = copy.at(0).toInt();
65 m_contrast = copy.at(1).toInt();
66 m_gamma = copy.at(2).toInt();
67 calculateGTwriteData();
68 }
69 return true;
70 }
71 return false;
72}
73
74void GammaOption::readValue()
75{
76 if (state() == Option::StateHidden) {
77 return;
78 }
79
80 QVarLengthArray<unsigned char> data(m_optDesc->size);
81 SANE_Status status;
82 SANE_Int res;
83 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
84 if (status != SANE_STATUS_GOOD) {
85 return;
86 }
87
88 QVector<int> gammaTable;
89 gammaTable.reserve(data.size() / sizeof(int));
90 for (int i = 0; i < data.size(); i += sizeof(SANE_Word)) gammaTable.append(toSANE_Word(&data[i]));
91
92 if (gammaTable != m_gammaTable) {
93 m_gammaTable = gammaTable;
94
95 m_gammaTableMax = m_optDesc->constraint.range->max;
96
97 calculateBCGwriteData();
98 }
99}
100
101QVariant GammaOption::value() const
102{
103 if (state() == Option::StateHidden) {
104 return QVariant();
105 }
106 return QVariantList{ m_brightness, m_contrast, m_gamma };
107}
108
109int GammaOption::valueSize() const
110{
111 return 3;
112}
113
114QVariant GammaOption::maximumValue() const
115{
116 QVariant value;
117 if (m_optDesc) {
118 value = static_cast<float>(m_optDesc->constraint.range->max);
119 return value;
120 }
121 return value;
122}
123
124QString GammaOption::valueAsString() const
125{
126 if (state() == Option::StateHidden) {
127 return QString();
128 }
129
130 return QString::asprintf("%d:%d:%d", m_brightness, m_contrast, m_gamma);
131}
132
133void GammaOption::calculateGTwriteData()
134{
135 double maxValue = m_optDesc->constraint.range->max;
136 double gamma = 100.0 / m_gamma;
137 double contrast = (200.0 / (100.0 - m_contrast)) - 1;
138 double halfMax = maxValue / 2.0;
139 // NOTE: This used to add the value times 2, not scaled to maxValue
140 double brightness = m_brightness * maxValue / 100.0;
141 double x;
142
143 for (int i = 0; i < m_gammaTable.size(); i++) {
144 // apply gamma
145 x = std::pow(static_cast<double>(i) / m_gammaTable.size(), gamma) * maxValue;
146
147 // apply contrast
148 x = (contrast * (x - halfMax)) + halfMax;
149
150 // apply brightness + rounding
151 x += brightness + 0.5;
152
153 // ensure correct value
154 if (x > maxValue) {
155 x = maxValue;
156 }
157 if (x < 0) {
158 x = 0;
159 }
160
161 m_gammaTable[i] = static_cast<int>(x);
162 }
163
164 writeData(m_gammaTable.data());
165 QVariantList values = { m_brightness, m_contrast, m_gamma };
166 Q_EMIT valueChanged(values);
167}
168
169void GammaOption::calculateBCGwriteData() {
170 int beginIndex = 0;
171 int endIndex = m_gammaTable.size() - 1;
172 // Find the start and end of the curve, to skip the flat regions
173 while (beginIndex < endIndex && m_gammaTable[beginIndex] == m_gammaTable[0])
174 beginIndex++;
175 while (endIndex > beginIndex && m_gammaTable[endIndex] == m_gammaTable[m_gammaTable.size()-1])
176 endIndex--;
177
178 float gamma = 0, contrast = 0, brightness = 0;
179 const QVector<int> &gammaTable = m_gammaTable;
180 const int &gammaTableMax = m_gammaTableMax;
181
182 auto guessGamma = [&gammaTable, &gamma](int i1, int i2, int step) {
183 int diff1 = gammaTable[i1 + step] - gammaTable[i1 - step];
184 int diff2 = gammaTable[i2 + step] - gammaTable[i2 - step];
185 if (diff1 == 0 || diff2 == 0)
186 return;
187 float stepProportion = static_cast<float>(i2) / i1;
188 float diffProportion = static_cast<float>(diff2) / diff1;
189 float guessedGamma = log(stepProportion * diffProportion) / log(stepProportion);
190 gamma += guessedGamma;
191 };
192
193 auto guessContrast = [&gammaTable, &gammaTableMax, &gamma, &contrast](int i1, int i2, int) {
194 int prevVal = gammaTable[i1], nextVal = gammaTable[i2];
195 float scaledDiff = static_cast<float>(nextVal - prevVal) / gammaTableMax;
196 float scaledPrevIndex = static_cast<float>(i1) / gammaTable.size();
197 float scaledNextIndex = static_cast<float>(i2) / gammaTable.size();
198 float guessedContrast = scaledDiff / (pow(scaledNextIndex, gamma) - pow(scaledPrevIndex, gamma));
199 contrast += guessedContrast;
200 };
201
202 auto guessBrightness = [&gammaTable, &gammaTableMax, &gamma, &contrast, &brightness](int i, int, int) {
203 float scaledThisVal = static_cast<float>(gammaTable[i]) / gammaTableMax;
204 float scaledIndex = static_cast<float>(i) / gammaTable.size();
205 float guessedBrightness = scaledThisVal - ((pow(scaledIndex, gamma) - 0.5) * contrast + 0.5);
206 brightness += guessedBrightness;
207 };
208
209 const int numberOfApproximations = 16;
210
211 auto passValuePairsAndSteps = [&beginIndex, &endIndex](auto func) {
212 const int step = (endIndex - beginIndex) / 8;
213 for (int i = 0; i < numberOfApproximations;) {
214 // Calculate step, even if not passed to the function, to separate the samples
215 int i1 = rand() % (endIndex - beginIndex - 2 * step - 2) + beginIndex + step + 1;
216 int i2 = rand() % (endIndex - beginIndex - 2 * step - 2) + beginIndex + step + 1;
217 if (i2 - i1 >= 4 * step) {
218 func(i1, i2, step);
219 i++;
220 }
221 }
222 };
223
224 if (endIndex == beginIndex) {
225 qCDebug(KSANECORE_LOG()) << "Ignoring gamma table: horizontal line at" << m_gammaTable[0];
226 setValue(QVariantList{0, 0, 100}); // Ignore the table, it's wrong
227 return;
228 }
229
230 if (endIndex - beginIndex <= 32) { // Table too small, make single guesses
231 if (endIndex - beginIndex > 4) { // Measurements don't overlap by just one value
232 guessGamma(beginIndex + 2, endIndex - 2, 2);
233 } else {
234 gamma = 1.0; // Assume linear gamma
235 }
236 guessContrast(beginIndex, endIndex, 0);
237 guessBrightness((beginIndex + endIndex) / 2, 0, 0);
238 } else {
239 passValuePairsAndSteps(guessGamma);
240 gamma /= numberOfApproximations;
241
242 passValuePairsAndSteps(guessContrast);
243 contrast /= numberOfApproximations;
244
245 passValuePairsAndSteps(guessBrightness);
246 brightness /= numberOfApproximations;
247 }
248
249 int newGamma = 100.0 / gamma;
250 int newContrast = 100.0 - 200.0 / (contrast + 1.0);
251 int newBrightness = brightness * 100.0;
252
253 if (m_gamma != newGamma || m_contrast != newContrast || m_brightness != newBrightness) {
254 m_gamma = newGamma;
255 m_contrast = newContrast;
256 m_brightness = newBrightness;
257
258 QVariantList values = { m_brightness, m_contrast, m_gamma };
259 Q_EMIT valueChanged(values);
260 }
261}
262
263} // namespace KSaneCore
264
265#include "moc_gammaoption.cpp"
Q_SCRIPTABLE CaptureState status()
const QList< QKeySequence > & copy()
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
void reserve(qsizetype size)
qsizetype size() const const
QString asprintf(const char *cformat,...)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool canConvert() const const
QList< QVariant > toList() const const
QString toString() const const
int userType() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.