8#include "gammaoption.h"
10#include <QVarLengthArray>
12#include <ksanecore_debug.h>
19GammaOption::GammaOption(
const SANE_Handle handle,
const int index)
20 : BaseOption(handle, index)
22 m_optionType = Option::TypeGamma;
25bool GammaOption::setValue(
const QVariant &value)
27 if (state() == Option::StateHidden) {
30#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
43 if (gammaValues.
size() != 3) {
46 brightness = gammaValues.
at(0).toInt(&ok);
48 contrast = gammaValues.
at(1).toInt(&ok);
51 gamma = gammaValues.
at(2).toInt(&ok);
54 if (ok && (m_brightness != brightness || m_contrast != contrast || m_gamma != gamma) ) {
55 m_brightness = brightness;
56 m_contrast = contrast;
58 calculateGTwriteData();
64#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
72 if (m_brightness !=
copy.at(0).toInt() || m_contrast !=
copy.at(1).toInt() || m_gamma !=
copy.at(2).toInt() ) {
73 m_brightness =
copy.at(0).toInt();
74 m_contrast =
copy.at(1).toInt();
75 m_gamma =
copy.at(2).toInt();
76 calculateGTwriteData();
83void GammaOption::readValue()
85 if (state() == Option::StateHidden) {
92 status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
93 if (
status != SANE_STATUS_GOOD) {
98 gammaTable.
reserve(data.size() /
sizeof(
int));
99 for (
int i = 0; i < data.size(); i +=
sizeof(SANE_Word)) gammaTable.
append(toSANE_Word(&data[i]));
101 if (gammaTable != m_gammaTable) {
102 m_gammaTable = gammaTable;
104 m_gammaTableMax = m_optDesc->constraint.range->max;
106 calculateBCGwriteData();
112 if (state() == Option::StateHidden) {
115 return QVariantList{ m_brightness, m_contrast, m_gamma };
118int GammaOption::valueSize()
const
123QVariant GammaOption::maximumValue()
const
127 value =
static_cast<float>(m_optDesc->constraint.range->max);
133QString GammaOption::valueAsString()
const
135 if (state() == Option::StateHidden) {
142void GammaOption::calculateGTwriteData()
144 double maxValue = m_optDesc->constraint.range->max;
145 double gamma = 100.0 / m_gamma;
146 double contrast = (200.0 / (100.0 - m_contrast)) - 1;
147 double halfMax = maxValue / 2.0;
149 double brightness = m_brightness * maxValue / 100.0;
152 for (
int i = 0; i < m_gammaTable.size(); i++) {
154 x = std::pow(
static_cast<double>(i) / m_gammaTable.size(), gamma) * maxValue;
157 x = (contrast * (x - halfMax)) + halfMax;
160 x += brightness + 0.5;
170 m_gammaTable[i] =
static_cast<int>(x);
173 writeData(m_gammaTable.data());
174 QVariantList values = { m_brightness, m_contrast, m_gamma };
175 Q_EMIT valueChanged(values);
178void GammaOption::calculateBCGwriteData() {
180 int endIndex = m_gammaTable.size() - 1;
182 while (beginIndex < endIndex && m_gammaTable[beginIndex] == m_gammaTable[0])
184 while (endIndex > beginIndex && m_gammaTable[endIndex] == m_gammaTable[m_gammaTable.size()-1])
187 float gamma = 0, contrast = 0, brightness = 0;
189 const int &gammaTableMax = m_gammaTableMax;
191 auto guessGamma = [&gammaTable, &gamma](
int i1,
int i2,
int step) {
192 int diff1 = gammaTable[i1 + step] - gammaTable[i1 - step];
193 int diff2 = gammaTable[i2 + step] - gammaTable[i2 - step];
194 if (diff1 == 0 || diff2 == 0)
196 float stepProportion =
static_cast<float>(i2) / i1;
197 float diffProportion =
static_cast<float>(diff2) / diff1;
198 float guessedGamma = log(stepProportion * diffProportion) / log(stepProportion);
199 gamma += guessedGamma;
202 auto guessContrast = [&gammaTable, &gammaTableMax, &gamma, &contrast](
int i1,
int i2, int) {
203 int prevVal = gammaTable[i1], nextVal = gammaTable[i2];
204 float scaledDiff =
static_cast<float>(nextVal - prevVal) / gammaTableMax;
205 float scaledPrevIndex =
static_cast<float>(i1) / gammaTable.size();
206 float scaledNextIndex =
static_cast<float>(i2) / gammaTable.size();
207 float guessedContrast = scaledDiff / (pow(scaledNextIndex, gamma) - pow(scaledPrevIndex, gamma));
208 contrast += guessedContrast;
211 auto guessBrightness = [&gammaTable, &gammaTableMax, &gamma, &contrast, &brightness](
int i, int, int) {
212 float scaledThisVal =
static_cast<float>(gammaTable[i]) / gammaTableMax;
213 float scaledIndex =
static_cast<float>(i) / gammaTable.size();
214 float guessedBrightness = scaledThisVal - ((pow(scaledIndex, gamma) - 0.5) * contrast + 0.5);
215 brightness += guessedBrightness;
218 const int numberOfApproximations = 16;
220 auto passValuePairsAndSteps = [&beginIndex, &endIndex](
auto func) {
221 const int step = (endIndex - beginIndex) / 8;
222 for (
int i = 0; i < numberOfApproximations;) {
224 int i1 = rand() % (endIndex - beginIndex - 2 * step - 2) + beginIndex + step + 1;
225 int i2 = rand() % (endIndex - beginIndex - 2 * step - 2) + beginIndex + step + 1;
226 if (i2 - i1 >= 4 * step) {
233 if (endIndex == beginIndex) {
234 qCDebug(KSANECORE_LOG()) <<
"Ignoring gamma table: horizontal line at" << m_gammaTable[0];
235 setValue(QVariantList{0, 0, 100});
239 if (endIndex - beginIndex <= 32) {
240 if (endIndex - beginIndex > 4) {
241 guessGamma(beginIndex + 2, endIndex - 2, 2);
245 guessContrast(beginIndex, endIndex, 0);
246 guessBrightness((beginIndex + endIndex) / 2, 0, 0);
248 passValuePairsAndSteps(guessGamma);
249 gamma /= numberOfApproximations;
251 passValuePairsAndSteps(guessContrast);
252 contrast /= numberOfApproximations;
254 passValuePairsAndSteps(guessBrightness);
255 brightness /= numberOfApproximations;
258 int newGamma = 100.0 / gamma;
259 int newContrast = 100.0 - 200.0 / (contrast + 1.0);
260 int newBrightness = brightness * 100.0;
262 if (m_gamma != newGamma || m_contrast != newContrast || m_brightness != newBrightness) {
264 m_contrast = newContrast;
265 m_brightness = newBrightness;
267 QVariantList values = { m_brightness, m_contrast, m_gamma };
268 Q_EMIT valueChanged(values);
274#include "moc_gammaoption.cpp"
Q_SCRIPTABLE CaptureState status()
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
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