]> git.cworth.org Git - apitrace/blobdiff - gui/imageviewer.cpp
Update downloads link.
[apitrace] / gui / imageviewer.cpp
index 4a11da5308d4c786d347cfc8472edc88e84115a7..1a1fe5aeec63656421a7ab3027a182f2a5fa787f 100644 (file)
@@ -1,5 +1,6 @@
 #include "imageviewer.h"
 
+#include <QDebug>
 #include <QDesktopWidget>
 #include <QPainter>
 #include <QPixmap>
@@ -10,6 +11,17 @@ ImageViewer::ImageViewer(QWidget *parent)
 {
     setupUi(this);
 
+    connect(lowerSpinBox, SIGNAL(valueChanged(double)),
+            SLOT(slotUpdate()));
+    connect(upperSpinBox, SIGNAL(valueChanged(double)),
+            SLOT(slotUpdate()));
+    connect(flipCheckBox, SIGNAL(stateChanged(int)),
+            SLOT(slotUpdate()));
+    connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
+            SLOT(slotUpdate()));
+    connect(alphaCheckBox, SIGNAL(stateChanged(int)),
+            SLOT(slotUpdate()));
+
     QPixmap px(32, 32);
     QPainter p(&px);
     p.fillRect(0, 0, 32, 32, Qt::white);
@@ -27,8 +39,82 @@ ImageViewer::ImageViewer(QWidget *parent)
 void ImageViewer::setImage(const QImage &image)
 {
     m_image = image;
-    QPixmap px = QPixmap::fromImage(image);
+    m_temp = m_image;
+    QPixmap px = QPixmap::fromImage(m_temp);
+    imageLabel->setPixmap(px);
+    updateGeometry();
+}
+
+static inline int clamp(int x)
+{
+    if (x <= 0) {
+        return 0;
+    }
+    if (x > 255) {
+        return 255;
+    }
+    return x;
+}
+
+void ImageViewer::slotUpdate()
+{
+    m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
+
+    double lowerValue = lowerSpinBox->value();
+    double upperValue = upperSpinBox->value();
+
+    bool opaque = opaqueCheckBox->isChecked();
+    bool alpha  = alphaCheckBox->isChecked();
+
+    if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
+        /*
+         * Rescale the image.
+         *
+         * XXX: This would be much more useful if done with the full precision
+         * of the original image
+         */
+
+        int offset = - lowerValue * 255;
+        int scale = 256 / (upperValue - lowerValue);
+
+        m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
+
+        if (0) {
+            qDebug()
+                << "offset = " << offset << "\n"
+                << "scale = " << scale << "\n";
+        }
+
+        int width = m_temp.width();
+        int height = m_temp.height();
+
+        int aMask = opaque ? 0xff : 0;
+
+        for (int y = 0; y < height; ++y) {
+            QRgb *scanline = (QRgb *)m_temp.scanLine(y);
+            for (int x = 0; x < width; ++x) {
+                QRgb pixel = scanline[x];
+                int r = qRed(pixel);
+                int g = qGreen(pixel);
+                int b = qBlue(pixel);
+                int a = qAlpha(pixel);
+                if (alpha) {
+                    a = clamp(((a + offset) * scale) >> 8);
+                    scanline[x] = qRgba(a, a, a, 0xff);
+                } else {
+                    r = clamp(((r + offset) * scale) >> 8);
+                    g = clamp(((g + offset) * scale) >> 8);
+                    b = clamp(((b + offset) * scale) >> 8);
+                    a |= aMask;
+                    scanline[x] = qRgba(r, g, b, a);
+                }
+            }
+        }
+    }
+
+    QPixmap px = QPixmap::fromImage(m_temp);
     imageLabel->setPixmap(px);
+
     updateGeometry();
 }
 
@@ -44,7 +130,7 @@ QSize ImageViewer::sizeHint() const
                             m_image.height() + hScrollHeight);
 
     QRect screenRect = QApplication::desktop()->availableGeometry();
-    const float maxPercentOfDesktopSpace = 0.8;
+    const float maxPercentOfDesktopSpace = 0.8f;
     QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
                            maxPercentOfDesktopSpace * screenRect.height());