1 #include "imageviewer.h"
2 #include "pixelwidget.h"
3 #include "apisurface.h"
5 #include "image/image.hpp"
8 #include <QDesktopWidget>
13 ImageViewer::ImageViewer(QWidget *parent)
19 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
21 connect(upperSpinBox, SIGNAL(valueChanged(double)),
23 connect(flipCheckBox, SIGNAL(stateChanged(int)),
25 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
27 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
32 p.fillRect(0, 0, 32, 32, Qt::white);
33 p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
34 p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
36 QPalette pal = scrollAreaWidgetContents->palette();
37 pal.setBrush(QPalette::Background,
39 pal.setBrush(QPalette::Base,
41 scrollAreaWidgetContents->setPalette(pal);
43 m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
44 verticalLayout_2->addWidget(m_pixelWidget);
49 connect(m_pixelWidget, SIGNAL(zoomChanged(int)),
50 zoomSpinBox, SLOT(setValue(int)));
51 connect(zoomSpinBox, SIGNAL(valueChanged(int)),
52 m_pixelWidget, SLOT(setZoom(int)));
53 connect(m_pixelWidget, SIGNAL(mousePosition(int, int)),
54 this, SLOT(showPixel(int, int)));
55 connect(m_pixelWidget, SIGNAL(gridGeometry(const QRect &)),
56 this, SLOT(showGrid(const QRect &)));
59 ImageViewer::~ImageViewer()
64 void ImageViewer::setBase64Data(const QByteArray &base64)
67 m_image = ApiSurface::imageFromBase64(base64);
68 m_convertedImage = ApiSurface::qimageFromRawImage(m_image);
69 m_pixelWidget->setSurface(m_convertedImage);
73 static inline int clamp(int x)
84 void ImageViewer::slotUpdate()
87 m_convertedImage.mirrored(false, flipCheckBox->isChecked());
89 double lowerValue = lowerSpinBox->value();
90 double upperValue = upperSpinBox->value();
92 bool opaque = opaqueCheckBox->isChecked();
93 bool alpha = alphaCheckBox->isChecked();
95 if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
99 * XXX: This would be much more useful if done with the full precision
100 * of the original image
103 int offset = - lowerValue * 255;
104 int scale = 256 / (upperValue - lowerValue);
107 m_convertedImage.convertToFormat(QImage::Format_ARGB32);
111 << "offset = " << offset << "\n"
112 << "scale = " << scale << "\n";
115 int width = m_convertedImage.width();
116 int height = m_convertedImage.height();
118 int aMask = opaque ? 0xff : 0;
120 for (int y = 0; y < height; ++y) {
121 QRgb *scanline = (QRgb *)m_convertedImage.scanLine(y);
122 for (int x = 0; x < width; ++x) {
123 QRgb pixel = scanline[x];
125 int g = qGreen(pixel);
126 int b = qBlue(pixel);
127 int a = qAlpha(pixel);
129 a = clamp(((a + offset) * scale) >> 8);
130 scanline[x] = qRgba(a, a, a, 0xff);
132 r = clamp(((r + offset) * scale) >> 8);
133 g = clamp(((g + offset) * scale) >> 8);
134 b = clamp(((b + offset) * scale) >> 8);
136 scanline[x] = qRgba(r, g, b, a);
142 m_pixelWidget->setSurface(m_convertedImage);
147 QSize ImageViewer::sizeHint() const
151 int vScrollWidth = scrollArea->verticalScrollBar() ?
152 scrollArea->verticalScrollBar()->width() : 0;
153 int hScrollHeight = scrollArea->horizontalScrollBar() ?
154 scrollArea->horizontalScrollBar()->height() : 0;
155 QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth,
156 m_convertedImage.height() + hScrollHeight);
158 QRect screenRect = QApplication::desktop()->availableGeometry();
159 const float maxPercentOfDesktopSpace = 0.8f;
160 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
161 maxPercentOfDesktopSpace * screenRect.height());
163 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
164 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
167 void ImageViewer::resizeEvent(QResizeEvent *e)
169 QWidget::resizeEvent(e);
173 QString createPixelLabel(image::Image *img, int x, int y)
176 unsigned char *pixelLocation = 0;
179 pixelLocation = img->pixels + img->stride() * y;
180 pixelLocation += x * img->bytesPerPixel;
181 pixel = ((T*)pixelLocation);
183 pixelLabel += QLatin1String("[");
184 pixelLabel += QString::fromLatin1("%1").arg(pixel[0]);
186 for (int channel = 1; channel < img->channels; ++channel) {
187 pixelLabel += QString::fromLatin1(", %1").arg(pixel[channel]);
189 pixelLabel += QLatin1String("]");
194 void ImageViewer::showPixel(int x, int y)
196 xSpinBox->setValue(x);
197 ySpinBox->setValue(y);
202 QString label = tr("Pixel: ");
204 if (m_image->channelType == image::TYPE_UNORM8) {
205 label += createPixelLabel<unsigned char>(m_image, x, y);
207 label += createPixelLabel<float>(m_image, x, y);
210 pixelLabel->setText(label);
214 void ImageViewer::showGrid(const QRect &rect)
216 if (rect.isEmpty()) {
220 rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
224 .arg(rect.height()));
228 #include "imageviewer.moc"