blob: f4ba76640277f0edace423cc4707507fb475c31e [file] [log] [blame]
Zack Rusin5cb6b872011-04-10 02:19:59 -04001#include "imageviewer.h"
2
José Fonsecad562b8e2011-11-25 15:51:09 +00003#include <QDebug>
Zack Rusin2ffe9f82011-09-23 20:25:47 -04004#include <QDesktopWidget>
Zack Rusin5cb6b872011-04-10 02:19:59 -04005#include <QPainter>
6#include <QPixmap>
Zack Rusin2ffe9f82011-09-23 20:25:47 -04007#include <QScrollBar>
Zack Rusin5cb6b872011-04-10 02:19:59 -04008
9ImageViewer::ImageViewer(QWidget *parent)
10 : QDialog(parent)
11{
12 setupUi(this);
13
José Fonsecad562b8e2011-11-25 15:51:09 +000014 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
Zack Rusin5cb6b872011-04-10 02:19:59 -040021 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
35void ImageViewer::setImage(const QImage &image)
36{
Zack Rusin2ffe9f82011-09-23 20:25:47 -040037 m_image = image;
José Fonsecad562b8e2011-11-25 15:51:09 +000038 m_temp = m_image;
39 QPixmap px = QPixmap::fromImage(m_temp);
Zack Rusin5cb6b872011-04-10 02:19:59 -040040 imageLabel->setPixmap(px);
Zack Rusin2ffe9f82011-09-23 20:25:47 -040041 updateGeometry();
42}
43
José Fonsecad562b8e2011-11-25 15:51:09 +000044static 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
55void 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
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400106QSize 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()));
Zack Rusin5cb6b872011-04-10 02:19:59 -0400124}
125
126#include "imageviewer.moc"