blob: 0e0657a3d60e265cd5d20c85815e69f881933e21 [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()));
José Fonsecafe13f772012-03-21 08:49:40 +000020 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
21 SLOT(slotUpdate()));
gregory8f0aa382012-07-01 17:18:04 +020022 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
23 SLOT(slotUpdate()));
José Fonsecad562b8e2011-11-25 15:51:09 +000024
Zack Rusin5cb6b872011-04-10 02:19:59 -040025 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
39void ImageViewer::setImage(const QImage &image)
40{
Zack Rusin2ffe9f82011-09-23 20:25:47 -040041 m_image = image;
José Fonsecad562b8e2011-11-25 15:51:09 +000042 m_temp = m_image;
43 QPixmap px = QPixmap::fromImage(m_temp);
Zack Rusin5cb6b872011-04-10 02:19:59 -040044 imageLabel->setPixmap(px);
Zack Rusin2ffe9f82011-09-23 20:25:47 -040045 updateGeometry();
46}
47
José Fonsecad562b8e2011-11-25 15:51:09 +000048static 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
59void ImageViewer::slotUpdate()
60{
61 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
62
63 double lowerValue = lowerSpinBox->value();
64 double upperValue = upperSpinBox->value();
65
José Fonsecafe13f772012-03-21 08:49:40 +000066 bool opaque = opaqueCheckBox->isChecked();
gregory8f0aa382012-07-01 17:18:04 +020067 bool alpha = alphaCheckBox->isChecked();
José Fonsecafe13f772012-03-21 08:49:40 +000068
gregory8f0aa382012-07-01 17:18:04 +020069 if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
José Fonsecad562b8e2011-11-25 15:51:09 +000070 /*
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
José Fonsecafe13f772012-03-21 08:49:40 +000091 int aMask = opaque ? 0xff : 0;
92
José Fonsecad562b8e2011-11-25 15:51:09 +000093 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);
José Fonsecafe13f772012-03-21 08:49:40 +0000100 int a = qAlpha(pixel);
gregory8f0aa382012-07-01 17:18:04 +0200101 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 }
José Fonsecad562b8e2011-11-25 15:51:09 +0000111 }
112 }
113 }
114
115 QPixmap px = QPixmap::fromImage(m_temp);
116 imageLabel->setPixmap(px);
117
118 updateGeometry();
119}
120
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400121QSize 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.8;
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()));
Zack Rusin5cb6b872011-04-10 02:19:59 -0400139}
140
141#include "imageviewer.moc"