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)),
22 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
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));
31 QPalette pal = scrollAreaWidgetContents->palette();
32 pal.setBrush(QPalette::Background,
34 pal.setBrush(QPalette::Base,
36 scrollAreaWidgetContents->setPalette(pal);
39 void ImageViewer::setImage(const QImage &image)
43 QPixmap px = QPixmap::fromImage(m_temp);
44 imageLabel->setPixmap(px);
48 static inline int clamp(int x)
59 void ImageViewer::slotUpdate()
61 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
63 double lowerValue = lowerSpinBox->value();
64 double upperValue = upperSpinBox->value();
66 bool opaque = opaqueCheckBox->isChecked();
67 bool alpha = alphaCheckBox->isChecked();
69 if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
73 * XXX: This would be much more useful if done with the full precision
74 * of the original image
77 int offset = - lowerValue * 255;
78 int scale = 256 / (upperValue - lowerValue);
80 m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
84 << "offset = " << offset << "\n"
85 << "scale = " << scale << "\n";
88 int width = m_temp.width();
89 int height = m_temp.height();
91 int aMask = opaque ? 0xff : 0;
93 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];
98 int g = qGreen(pixel);
100 int a = qAlpha(pixel);
102 a = clamp(((a + offset) * scale) >> 8);
103 scanline[x] = qRgba(a, a, a, 0xff);
105 r = clamp(((r + offset) * scale) >> 8);
106 g = clamp(((g + offset) * scale) >> 8);
107 b = clamp(((b + offset) * scale) >> 8);
109 scanline[x] = qRgba(r, g, b, a);
115 QPixmap px = QPixmap::fromImage(m_temp);
116 imageLabel->setPixmap(px);
121 QSize ImageViewer::sizeHint() const
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);
132 QRect screenRect = QApplication::desktop()->availableGeometry();
133 const float maxPercentOfDesktopSpace = 0.8f;
134 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
135 maxPercentOfDesktopSpace * screenRect.height());
137 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
138 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
141 #include "imageviewer.moc"