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