blob: a795993fb6c7b538128e477e3b625cbff9f0a4fc [file] [log] [blame]
Zack Rusin5cb6b872011-04-10 02:19:59 -04001#include "imageviewer.h"
Zack Rusin66ce10a2013-09-10 20:30:59 -04002#include "pixelwidget.h"
Zack Rusina69f0de2013-09-12 17:21:51 -04003#include "apisurface.h"
4
5#include "image/image.hpp"
Zack Rusin5cb6b872011-04-10 02:19:59 -04006
José Fonsecad562b8e2011-11-25 15:51:09 +00007#include <QDebug>
Zack Rusin2ffe9f82011-09-23 20:25:47 -04008#include <QDesktopWidget>
Zack Rusin5cb6b872011-04-10 02:19:59 -04009#include <QPainter>
10#include <QPixmap>
Zack Rusin2ffe9f82011-09-23 20:25:47 -040011#include <QScrollBar>
Zack Rusin5cb6b872011-04-10 02:19:59 -040012
13ImageViewer::ImageViewer(QWidget *parent)
Zack Rusina69f0de2013-09-12 17:21:51 -040014 : QDialog(parent),
15 m_image(0)
Zack Rusin5cb6b872011-04-10 02:19:59 -040016{
17 setupUi(this);
18
José Fonsecad562b8e2011-11-25 15:51:09 +000019 connect(lowerSpinBox, SIGNAL(valueChanged(double)),
20 SLOT(slotUpdate()));
21 connect(upperSpinBox, SIGNAL(valueChanged(double)),
22 SLOT(slotUpdate()));
23 connect(flipCheckBox, SIGNAL(stateChanged(int)),
24 SLOT(slotUpdate()));
José Fonsecafe13f772012-03-21 08:49:40 +000025 connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
26 SLOT(slotUpdate()));
gregory8f0aa382012-07-01 17:18:04 +020027 connect(alphaCheckBox, SIGNAL(stateChanged(int)),
28 SLOT(slotUpdate()));
José Fonsecad562b8e2011-11-25 15:51:09 +000029
Zack Rusin5cb6b872011-04-10 02:19:59 -040030 QPixmap px(32, 32);
31 QPainter p(&px);
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));
35 p.end();
36 QPalette pal = scrollAreaWidgetContents->palette();
37 pal.setBrush(QPalette::Background,
38 QBrush(px));
39 pal.setBrush(QPalette::Base,
40 QBrush(px));
41 scrollAreaWidgetContents->setPalette(pal);
Zack Rusin66ce10a2013-09-10 20:30:59 -040042
43 m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
44 verticalLayout_2->addWidget(m_pixelWidget);
45
46 rectLabel->hide();
47 pixelLabel->hide();
48
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 &)));
Zack Rusin5cb6b872011-04-10 02:19:59 -040057}
58
Zack Rusina69f0de2013-09-12 17:21:51 -040059ImageViewer::~ImageViewer()
Zack Rusin5cb6b872011-04-10 02:19:59 -040060{
Zack Rusina69f0de2013-09-12 17:21:51 -040061 delete m_image;
62}
63
64void ImageViewer::setBase64Data(const QByteArray &base64)
65{
66 delete m_image;
67 m_image = ApiSurface::imageFromBase64(base64);
68 m_convertedImage = ApiSurface::qimageFromRawImage(m_image);
69 m_pixelWidget->setSurface(m_convertedImage);
Zack Rusin2ffe9f82011-09-23 20:25:47 -040070 updateGeometry();
71}
72
José Fonsecad562b8e2011-11-25 15:51:09 +000073static inline int clamp(int x)
74{
75 if (x <= 0) {
76 return 0;
77 }
78 if (x > 255) {
79 return 255;
80 }
81 return x;
82}
83
84void ImageViewer::slotUpdate()
85{
Zack Rusina69f0de2013-09-12 17:21:51 -040086 m_convertedImage =
87 m_convertedImage.mirrored(false, flipCheckBox->isChecked());
José Fonsecad562b8e2011-11-25 15:51:09 +000088
89 double lowerValue = lowerSpinBox->value();
90 double upperValue = upperSpinBox->value();
91
José Fonsecafe13f772012-03-21 08:49:40 +000092 bool opaque = opaqueCheckBox->isChecked();
gregory8f0aa382012-07-01 17:18:04 +020093 bool alpha = alphaCheckBox->isChecked();
José Fonsecafe13f772012-03-21 08:49:40 +000094
gregory8f0aa382012-07-01 17:18:04 +020095 if (lowerValue != 0.0 || upperValue != 1.0 || opaque || alpha) {
José Fonsecad562b8e2011-11-25 15:51:09 +000096 /*
97 * Rescale the image.
98 *
99 * XXX: This would be much more useful if done with the full precision
100 * of the original image
101 */
102
103 int offset = - lowerValue * 255;
104 int scale = 256 / (upperValue - lowerValue);
105
Zack Rusina69f0de2013-09-12 17:21:51 -0400106 m_convertedImage =
107 m_convertedImage.convertToFormat(QImage::Format_ARGB32);
José Fonsecad562b8e2011-11-25 15:51:09 +0000108
109 if (0) {
110 qDebug()
111 << "offset = " << offset << "\n"
112 << "scale = " << scale << "\n";
113 }
114
Zack Rusina69f0de2013-09-12 17:21:51 -0400115 int width = m_convertedImage.width();
116 int height = m_convertedImage.height();
José Fonsecad562b8e2011-11-25 15:51:09 +0000117
José Fonsecafe13f772012-03-21 08:49:40 +0000118 int aMask = opaque ? 0xff : 0;
119
José Fonsecad562b8e2011-11-25 15:51:09 +0000120 for (int y = 0; y < height; ++y) {
Zack Rusina69f0de2013-09-12 17:21:51 -0400121 QRgb *scanline = (QRgb *)m_convertedImage.scanLine(y);
José Fonsecad562b8e2011-11-25 15:51:09 +0000122 for (int x = 0; x < width; ++x) {
123 QRgb pixel = scanline[x];
124 int r = qRed(pixel);
125 int g = qGreen(pixel);
126 int b = qBlue(pixel);
José Fonsecafe13f772012-03-21 08:49:40 +0000127 int a = qAlpha(pixel);
gregory8f0aa382012-07-01 17:18:04 +0200128 if (alpha) {
129 a = clamp(((a + offset) * scale) >> 8);
130 scanline[x] = qRgba(a, a, a, 0xff);
131 } else {
132 r = clamp(((r + offset) * scale) >> 8);
133 g = clamp(((g + offset) * scale) >> 8);
134 b = clamp(((b + offset) * scale) >> 8);
135 a |= aMask;
136 scanline[x] = qRgba(r, g, b, a);
137 }
José Fonsecad562b8e2011-11-25 15:51:09 +0000138 }
139 }
140 }
141
Zack Rusina69f0de2013-09-12 17:21:51 -0400142 m_pixelWidget->setSurface(m_convertedImage);
José Fonsecad562b8e2011-11-25 15:51:09 +0000143
144 updateGeometry();
145}
146
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400147QSize ImageViewer::sizeHint() const
148{
149 QSize size;
150
151 int vScrollWidth = scrollArea->verticalScrollBar() ?
152 scrollArea->verticalScrollBar()->width() : 0;
153 int hScrollHeight = scrollArea->horizontalScrollBar() ?
154 scrollArea->horizontalScrollBar()->height() : 0;
Zack Rusina69f0de2013-09-12 17:21:51 -0400155 QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth,
156 m_convertedImage.height() + hScrollHeight);
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400157
158 QRect screenRect = QApplication::desktop()->availableGeometry();
José Fonseca5d7fc052012-11-13 19:53:47 +0000159 const float maxPercentOfDesktopSpace = 0.8f;
Zack Rusin2ffe9f82011-09-23 20:25:47 -0400160 QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
161 maxPercentOfDesktopSpace * screenRect.height());
162
163 return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
164 qMin(optimalWindowSize.height(), maxAvailableSize.height()));
Zack Rusin5cb6b872011-04-10 02:19:59 -0400165}
166
Zack Rusin66ce10a2013-09-10 20:30:59 -0400167void ImageViewer::resizeEvent(QResizeEvent *e)
168{
169 QWidget::resizeEvent(e);
170}
171
Zack Rusina69f0de2013-09-12 17:21:51 -0400172template <class T>
173QString createPixelLabel(image::Image *img, int x, int y)
174{
175 QString pixelLabel;
176 unsigned char *pixelLocation = 0;
177 T *pixel;
178
179 pixelLocation = img->pixels + img->stride() * y;
180 pixelLocation += x * img->bytesPerPixel;
181 pixel = ((T*)pixelLocation);
182
183 pixelLabel += QLatin1String("[");
184 pixelLabel += QString::fromLatin1("%1").arg(pixel[0]);
185
186 for (int channel = 1; channel < img->channels; ++channel) {
187 pixelLabel += QString::fromLatin1(", %1").arg(pixel[channel]);
188 }
189 pixelLabel += QLatin1String("]");
190
191 return pixelLabel;
192}
193
Zack Rusin66ce10a2013-09-10 20:30:59 -0400194void ImageViewer::showPixel(int x, int y)
195{
196 xSpinBox->setValue(x);
197 ySpinBox->setValue(y);
Zack Rusina69f0de2013-09-12 17:21:51 -0400198
199 if (!m_image)
200 return;
201
202 QString label = tr("Pixel: ");
203
204 if (m_image->channelType == image::TYPE_UNORM8) {
205 label += createPixelLabel<unsigned char>(m_image, x, y);
206 } else {
207 label += createPixelLabel<float>(m_image, x, y);
208 }
209
210 pixelLabel->setText(label);
Zack Rusin66ce10a2013-09-10 20:30:59 -0400211 pixelLabel->show();
212}
213
214void ImageViewer::showGrid(const QRect &rect)
215{
216 if (rect.isEmpty()) {
217 rectLabel->hide();
218 return;
219 }
220 rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
221 .arg(rect.x())
222 .arg(rect.y())
223 .arg(rect.width())
224 .arg(rect.height()));
225 rectLabel->show();
226}
227
Zack Rusin5cb6b872011-04-10 02:19:59 -0400228#include "imageviewer.moc"