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)),
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));
27 QPalette pal = scrollAreaWidgetContents->palette();
28 pal.setBrush(QPalette::Background,
30 pal.setBrush(QPalette::Base,
32 scrollAreaWidgetContents->setPalette(pal);
35 void ImageViewer::setImage(const QImage &image)
39 QPixmap px = QPixmap::fromImage(m_temp);
40 imageLabel->setPixmap(px);
44 static inline int clamp(int x)
55 void ImageViewer::slotUpdate()
57 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
59 double lowerValue = lowerSpinBox->value();
60 double upperValue = upperSpinBox->value();
62 if (lowerValue != 0.0 || upperValue != 1.0) {
66 * XXX: This would be much more useful if done with the full precision
67 * of the original image
70 int offset = - lowerValue * 255;
71 int scale = 256 / (upperValue - lowerValue);
73 m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
77 << "offset = " << offset << "\n"
78 << "scale = " << scale << "\n";
81 int width = m_temp.width();
82 int height = m_temp.height();
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];
89 int g = qGreen(pixel);
91 r = clamp(((r + offset) * scale) >> 8);
92 g = clamp(((g + offset) * scale) >> 8);
93 b = clamp(((b + offset) * scale) >> 8);
95 scanline[x] = qRgba(r, g, b, a);
100 QPixmap px = QPixmap::fromImage(m_temp);
101 imageLabel->setPixmap(px);
106 QSize ImageViewer::sizeHint() const
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);
117 QRect screenRect = QApplication::desktop()->availableGeometry();
118 const float maxPercentOfDesktopSpace = 0.8;
119 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
120 maxPercentOfDesktopSpace * screenRect.height());
122 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
123 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
126 #include "imageviewer.moc"