KSane

gammadisp.cpp
1/*
2 * SPDX-FileCopyrightText: 2007-2008 Kare Sars <kare.sars@iki .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// Local includes
9#include "gammadisp.h"
10
11// Qt includes
12#include <QPainter>
13
14#include <cmath>
15
16namespace KSaneIface
17{
18
19GammaDisp::GammaDisp(QWidget *parent, int *brightness, int *contrast, int *gamma, int maxValue)
20: QWidget(parent)
21, m_brightness(brightness)
22, m_contrast(contrast)
23, m_gamma(gamma)
24, m_gammaColor(QColor::fromRgb(0,0,0))
25, m_maxValue(maxValue)
26{}
27
28QSize GammaDisp::minimumSizeHint() const
29{
30 return QSize(75, 75);
31}
32
33QSize GammaDisp::sizeHint() const
34{
35 return QSize(75, 75);
36}
37
38void GammaDisp::setColor(const QColor &color)
39{
40 m_gammaColor = color;
41}
42
43void GammaDisp::resizeEvent(QResizeEvent *)
44{
45 repaint();
46}
47
48void GammaDisp::paintEvent(QPaintEvent *)
49{
50 /* QMemArray<QRect> rects = event->region().rects();
51 for (int i = 0; i < (int)rects.size(); i++) {
52 bitBlt(this, rects[i].topLeft(), &pixmap, rects[i]);
53 }*/
54 QPointF p1, p2;
55 QPainter painter(this);
56 painter.fillRect(rect(), QBrush(Qt::white));
57
58 const int xResolution = 100;
59 double max = static_cast<double>(m_maxValue);
60 double xscale = static_cast<double>(size().width() - 1) / static_cast<double>(xResolution);
61 double yscale = static_cast<double>(size().height() - 1) / max;
62
63 painter.setPen(m_gammaColor);
64
65 double gamma = 100.0 / *m_gamma;
66 double contrast = (200.0 / (100.0 - *m_contrast)) - 1;
67 double halfMax = max / 2.0;
68 double brightness = (*m_brightness / halfMax) * max;
69
70 double xPrevious = 0;
71 double xNext = 0;
72
73 // gamma is zero for first one, start with contrast
74 xPrevious = (contrast * (xPrevious - halfMax)) + halfMax;
75 // apply brightness + rounding
76 xPrevious += brightness + 0.5;
77 // ensure correct value
78 if (xPrevious > max) {
79 xPrevious = max;
80 }
81 if (xPrevious < 0) {
82 xPrevious = 0;
83 }
84
85 for (int i = 0; i < xResolution - 1; i++) {
86 xNext = std::pow(static_cast<double>(i+1) / xResolution, gamma) * max;
87 // apply contrast
88 xNext = (contrast * (xNext - halfMax)) + halfMax;
89 // apply brightness + rounding
90 xNext += brightness + 0.5;
91
92 // ensure correct value
93 if (xNext > max) {
94 xNext = max;
95 }
96 if (xNext < 0) {
97 xNext = 0;
98 }
99
100 p1.setX(i * xscale);
101 p1.setY(size().height() - 1 - (xPrevious * yscale));
102
103 p2.setX((i + 1)*xscale);
104 p2.setY(size().height() - 1 - (xNext * yscale));
105
106 painter.drawLine(p1, p2);
107 xPrevious = xNext;
108 }
109}
110
111} // NameSpace KSaneIface
112
113#include "moc_gammadisp.cpp"
void setX(qreal x)
void setY(qreal y)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:18:32 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.