blob: 7b1e425f3e64ef2438d7d9c8e43ea5fbb6189e75 [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()));
José Fonsecad562b8e2011-11-25 15:51:09 +000022
Zack Rusin5cb6b872011-04-10 02:19:59 -040023 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
37void ImageViewer::setImage(const QImage &image)
38{
Zack Rusin2ffe9f82011-09-23 20:25:47 -040039 m_image = image;
José Fonsecad562b8e2011-11-25 15:51:09 +000040 m_temp = m_image;
41 QPixmap px = QPixmap::fromImage(m_temp);
Zack Rusin5cb6b872011-04-10 02:19:59 -040042 imageLabel->setPixmap(px);
Zack Rusin2ffe9f82011-09-23 20:25:47 -040043 updateGeometry();
44}
45
José Fonsecad562b8e2011-11-25 15:51:09 +000046static 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
57void ImageViewer::slotUpdate()
58{
59 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
60
61 double lowerValue = lowerSpinBox->value();
62 double upperValue = upperSpinBox->value();
63
José Fonsecafe13f772012-03-21 08:49:40 +000064 bool opaque = opaqueCheckBox->isChecked();
65
66 if (lowerValue != 0.0 || upperValue != 1.0 || opaque) {
José Fonsecad562b8e2011-11-25 15:51:09 +000067 /*
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
José Fonsecafe13f772012-03-21 08:49:40 +000088 int aMask = opaque ? 0xff : 0;
89
José Fonsecad562b8e2011-11-25 15:51:09 +000090 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);
José Fonsecafe13f772012-03-21 08:49:40 +000097 int a = qAlpha(pixel);
José Fonsecad562b8e2011-11-25 15:51:09 +000098 r = clamp(((r + offset) * scale) >> 8);
99 g = clamp(((g + offset) * scale) >> 8);
100 b = clamp(((b + offset) * scale) >> 8);
José Fonsecafe13f772012-03-21 08:49:40 +0000101 a |= aMask;
José Fonsecad562b8e2011-11-25 15:51:09 +0000102 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
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400113QSize 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()));
Zack Rusin5cb6b872011-04-10 02:19:59 -0400131}
132
133#include "imageviewer.moc"