1 #include "imageviewer.h"
2 #include "pixelwidget.h"
5 #include <QDesktopWidget>
10 ImageViewer::ImageViewer(QWidget *parent)
15 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
17 connect(upperSpinBox, SIGNAL(valueChanged(double)),
19 connect(flipCheckBox, SIGNAL(stateChanged(int)),
21 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
23 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
28 p.fillRect(0, 0, 32, 32, Qt::white);
29 p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
30 p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
32 QPalette pal = scrollAreaWidgetContents->palette();
33 pal.setBrush(QPalette::Background,
35 pal.setBrush(QPalette::Base,
37 scrollAreaWidgetContents->setPalette(pal);
39 m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
40 verticalLayout_2->addWidget(m_pixelWidget);
45 connect(m_pixelWidget, SIGNAL(zoomChanged(int)),
46 zoomSpinBox, SLOT(setValue(int)));
47 connect(zoomSpinBox, SIGNAL(valueChanged(int)),
48 m_pixelWidget, SLOT(setZoom(int)));
49 connect(m_pixelWidget, SIGNAL(mousePosition(int, int)),
50 this, SLOT(showPixel(int, int)));
51 connect(m_pixelWidget, SIGNAL(gridGeometry(const QRect &)),
52 this, SLOT(showGrid(const QRect &)));
55 void ImageViewer::setImage(const QImage &image)
59 m_pixelWidget->setSurface(m_image);
63 static inline int clamp(int x)
74 void ImageViewer::slotUpdate()
76 m_temp = m_image.mirrored(false, flipCheckBox->isChecked());
78 double lowerValue = lowerSpinBox->value();
79 double upperValue = upperSpinBox->value();
81 bool opaque = opaqueCheckBox->isChecked();
82 bool alpha = alphaCheckBox->isChecked();
84 if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
88 * XXX: This would be much more useful if done with the full precision
89 * of the original image
92 int offset = - lowerValue * 255;
93 int scale = 256 / (upperValue - lowerValue);
95 m_temp = m_temp.convertToFormat(QImage::Format_ARGB32);
99 << "offset = " << offset << "\n"
100 << "scale = " << scale << "\n";
103 int width = m_temp.width();
104 int height = m_temp.height();
106 int aMask = opaque ? 0xff : 0;
108 for (int y = 0; y < height; ++y) {
109 QRgb *scanline = (QRgb *)m_temp.scanLine(y);
110 for (int x = 0; x < width; ++x) {
111 QRgb pixel = scanline[x];
113 int g = qGreen(pixel);
114 int b = qBlue(pixel);
115 int a = qAlpha(pixel);
117 a = clamp(((a + offset) * scale) >> 8);
118 scanline[x] = qRgba(a, a, a, 0xff);
120 r = clamp(((r + offset) * scale) >> 8);
121 g = clamp(((g + offset) * scale) >> 8);
122 b = clamp(((b + offset) * scale) >> 8);
124 scanline[x] = qRgba(r, g, b, a);
130 m_pixelWidget->setSurface(m_temp);
135 QSize ImageViewer::sizeHint() const
139 int vScrollWidth = scrollArea->verticalScrollBar() ?
140 scrollArea->verticalScrollBar()->width() : 0;
141 int hScrollHeight = scrollArea->horizontalScrollBar() ?
142 scrollArea->horizontalScrollBar()->height() : 0;
143 QSize optimalWindowSize(m_image.width() + vScrollWidth,
144 m_image.height() + hScrollHeight);
146 QRect screenRect = QApplication::desktop()->availableGeometry();
147 const float maxPercentOfDesktopSpace = 0.8f;
148 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
149 maxPercentOfDesktopSpace * screenRect.height());
151 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
152 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
155 void ImageViewer::resizeEvent(QResizeEvent *e)
157 QWidget::resizeEvent(e);
160 void ImageViewer::showPixel(int x, int y)
162 xSpinBox->setValue(x);
163 ySpinBox->setValue(y);
164 QColor clr = m_pixelWidget->colorAtCurrentPosition();
165 pixelLabel->setText(tr("RGBA: (%1, %2, %3, %4)")
173 void ImageViewer::showGrid(const QRect &rect)
175 if (rect.isEmpty()) {
179 rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
183 .arg(rect.height()));
187 #include "imageviewer.moc"