]> git.cworth.org Git - apitrace/blob - gui/imageviewer.cpp
Cleanup how pointers are handled.
[apitrace] / gui / imageviewer.cpp
1 #include "imageviewer.h"
2
3 #include <QDebug>
4 #include <QDesktopWidget>
5 #include <QPainter>
6 #include <QPixmap>
7 #include <QScrollBar>
8
9 ImageViewer::ImageViewer(QWidget *parent)
10     : QDialog(parent)
11 {
12     setupUi(this);
13
14     connect(lowerSpinBox, SIGNAL(valueChanged(double)),
15             SLOT(slotUpdate()));
16     connect(upperSpinBox, SIGNAL(valueChanged(double)),
17             SLOT(slotUpdate()));
18     connect(flipCheckBox, SIGNAL(stateChanged(int)),
19             SLOT(slotUpdate()));
20     connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
21             SLOT(slotUpdate()));
22
23     QPixmap px(32, 32);
24     QPainter p(&px);
25     p.fillRect(0, 0, 32, 32, Qt::white);
26     p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
27     p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
28     p.end();
29     QPalette pal = scrollAreaWidgetContents->palette();
30     pal.setBrush(QPalette::Background,
31                  QBrush(px));
32     pal.setBrush(QPalette::Base,
33                  QBrush(px));
34     scrollAreaWidgetContents->setPalette(pal);
35 }
36
37 void ImageViewer::setImage(const QImage &image)
38 {
39     m_image = image;
40     m_temp = m_image;
41     QPixmap px = QPixmap::fromImage(m_temp);
42     imageLabel->setPixmap(px);
43     updateGeometry();
44 }
45
46 static inline int clamp(int x)
47 {
48     if (x <= 0) {
49         return 0;
50     }
51     if (x > 255) {
52         return 255;
53     }
54     return x;
55 }
56
57 void ImageViewer::slotUpdate()
58 {
59     m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
60
61     double lowerValue = lowerSpinBox->value();
62     double upperValue = upperSpinBox->value();
63
64     bool opaque = opaqueCheckBox->isChecked();
65
66     if (lowerValue != 0.0 || upperValue != 1.0 || opaque) {
67         /*
68          * Rescale the image.
69          *
70          * XXX: This would be much more useful if done with the full precision
71          * of the original image
72          */
73
74         int offset = - lowerValue * 255;
75         int scale = 256 / (upperValue - lowerValue);
76
77         m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
78
79         if (0) {
80             qDebug()
81                 << "offset = " << offset << "\n"
82                 << "scale = " << scale << "\n";
83         }
84
85         int width = m_temp.width();
86         int height = m_temp.height();
87
88         int aMask = opaque ? 0xff : 0;
89
90         for (int y = 0; y < height; ++y) {
91             QRgb *scanline = (QRgb *)m_temp.scanLine(y);
92             for (int x = 0; x < width; ++x) {
93                 QRgb pixel = scanline[x];
94                 int r = qRed(pixel);
95                 int g = qGreen(pixel);
96                 int b = qBlue(pixel);
97                 int a = qAlpha(pixel);
98                 r = clamp(((r + offset) * scale) >> 8);
99                 g = clamp(((g + offset) * scale) >> 8);
100                 b = clamp(((b + offset) * scale) >> 8);
101                 a |= aMask;
102                 scanline[x] = qRgba(r, g, b, a);
103             }
104         }
105     }
106
107     QPixmap px = QPixmap::fromImage(m_temp);
108     imageLabel->setPixmap(px);
109
110     updateGeometry();
111 }
112
113 QSize ImageViewer::sizeHint() const
114 {
115     QSize size;
116
117     int vScrollWidth = scrollArea->verticalScrollBar() ?
118                 scrollArea->verticalScrollBar()->width() : 0;
119     int hScrollHeight = scrollArea->horizontalScrollBar() ?
120                 scrollArea->horizontalScrollBar()->height() : 0;
121     QSize optimalWindowSize(m_image.width() + vScrollWidth,
122                             m_image.height() + hScrollHeight);
123
124     QRect screenRect = QApplication::desktop()->availableGeometry();
125     const float maxPercentOfDesktopSpace = 0.8;
126     QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
127                            maxPercentOfDesktopSpace * screenRect.height());
128
129     return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
130                  qMin(optimalWindowSize.height(), maxAvailableSize.height()));
131 }
132
133 #include "imageviewer.moc"