1 #include "imageviewer.h"
4 #include <QDesktopWidget>
9 ImageViewer::ImageViewer(QWidget *parent)
14 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
16 connect(upperSpinBox, SIGNAL(valueChanged(double)),
18 connect(flipCheckBox, SIGNAL(stateChanged(int)),
20 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
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));
29 QPalette pal = scrollAreaWidgetContents->palette();
30 pal.setBrush(QPalette::Background,
32 pal.setBrush(QPalette::Base,
34 scrollAreaWidgetContents->setPalette(pal);
37 void ImageViewer::setImage(const QImage &image)
41 QPixmap px = QPixmap::fromImage(m_temp);
42 imageLabel->setPixmap(px);
46 static inline int clamp(int x)
57 void ImageViewer::slotUpdate()
59 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
61 double lowerValue = lowerSpinBox->value();
62 double upperValue = upperSpinBox->value();
64 bool opaque = opaqueCheckBox->isChecked();
66 if (lowerValue != 0.0 || upperValue != 1.0 || opaque) {
70 * XXX: This would be much more useful if done with the full precision
71 * of the original image
74 int offset = - lowerValue * 255;
75 int scale = 256 / (upperValue - lowerValue);
77 m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
81 << "offset = " << offset << "\n"
82 << "scale = " << scale << "\n";
85 int width = m_temp.width();
86 int height = m_temp.height();
88 int aMask = opaque ? 0xff : 0;
90 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];
95 int g = qGreen(pixel);
97 int a = qAlpha(pixel);
98 r = clamp(((r + offset) * scale) >> 8);
99 g = clamp(((g + offset) * scale) >> 8);
100 b = clamp(((b + offset) * scale) >> 8);
102 scanline[x] = qRgba(r, g, b, a);
107 QPixmap px = QPixmap::fromImage(m_temp);
108 imageLabel->setPixmap(px);
113 QSize ImageViewer::sizeHint() const
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);
124 QRect screenRect = QApplication::desktop()->availableGeometry();
125 const float maxPercentOfDesktopSpace = 0.8;
126 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
127 maxPercentOfDesktopSpace * screenRect.height());
129 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
130 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
133 #include "imageviewer.moc"