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