File indexing completed on 2024-05-12 04:33:22

0001 /*
0002  * SPDX-FileCopyrightText: 2007-2008 Kare Sars <kare.sars@iki .fi>
0003  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 // Local includes
0009 #include "gammadisp.h"
0010 
0011 // Qt includes
0012 #include <QPainter>
0013 
0014 #include <cmath>
0015 
0016 namespace KSaneIface
0017 {
0018 
0019 GammaDisp::GammaDisp(QWidget *parent, int *brightness, int *contrast, int *gamma, int maxValue)
0020 : QWidget(parent)
0021 , m_brightness(brightness)
0022 , m_contrast(contrast)
0023 , m_gamma(gamma)
0024 , m_gammaColor(QColor::fromRgb(0,0,0))
0025 , m_maxValue(maxValue)
0026 {}
0027 
0028 QSize GammaDisp::minimumSizeHint() const
0029 {
0030     return QSize(75, 75);
0031 }
0032 
0033 QSize GammaDisp::sizeHint() const
0034 {
0035     return QSize(75, 75);
0036 }
0037 
0038 void GammaDisp::setColor(const QColor &color)
0039 {
0040     m_gammaColor = color;
0041 }
0042 
0043 void GammaDisp::resizeEvent(QResizeEvent *)
0044 {
0045     repaint();
0046 }
0047 
0048 void GammaDisp::paintEvent(QPaintEvent *)
0049 {
0050     /*    QMemArray<QRect> rects = event->region().rects();
0051         for (int i = 0; i < (int)rects.size(); i++) {
0052             bitBlt(this, rects[i].topLeft(), &pixmap, rects[i]);
0053         }*/
0054     QPointF p1, p2;
0055     QPainter painter(this);
0056     painter.fillRect(rect(), QBrush(Qt::white));
0057 
0058     const int xResolution = 100;
0059     double max = static_cast<double>(m_maxValue);
0060     double xscale = static_cast<double>(size().width() - 1)  / static_cast<double>(xResolution);
0061     double yscale = static_cast<double>(size().height() - 1) / max;
0062 
0063     painter.setPen(m_gammaColor);
0064 
0065     double gamma    = 100.0 / *m_gamma;
0066     double contrast = (200.0 / (100.0 - *m_contrast)) - 1;
0067     double halfMax  = max / 2.0;
0068     double brightness  = (*m_brightness / halfMax) * max;
0069 
0070     double xPrevious = 0;
0071     double xNext = 0;
0072 
0073     // gamma is zero for first one, start with contrast
0074     xPrevious = (contrast * (xPrevious - halfMax)) + halfMax;
0075     // apply brightness + rounding
0076     xPrevious += brightness + 0.5;
0077     // ensure correct value
0078     if (xPrevious > max) {
0079         xPrevious = max;
0080     }
0081     if (xPrevious < 0) {
0082         xPrevious = 0;
0083     }
0084 
0085     for (int i = 0; i < xResolution - 1; i++) {
0086         xNext = std::pow(static_cast<double>(i+1) / xResolution, gamma) * max;
0087         // apply contrast
0088         xNext = (contrast * (xNext - halfMax)) + halfMax;
0089         // apply brightness + rounding
0090         xNext += brightness + 0.5;
0091 
0092         // ensure correct value
0093         if (xNext > max) {
0094             xNext = max;
0095         }
0096         if (xNext < 0) {
0097             xNext = 0;
0098         }
0099 
0100         p1.setX(i * xscale);
0101         p1.setY(size().height() - 1 - (xPrevious * yscale));
0102 
0103         p2.setX((i + 1)*xscale);
0104         p2.setY(size().height() - 1 - (xNext * yscale));
0105 
0106         painter.drawLine(p1, p2);
0107         xPrevious = xNext;
0108     }
0109 }
0110 
0111 }  // NameSpace KSaneIface
0112 
0113 #include "moc_gammadisp.cpp"