1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
#include "imageviewer.h"
#include "pixelwidget.h"
#include "apisurface.h"
#include "image/image.hpp"
#include <QDebug>
#include <QDesktopWidget>
#include <QPainter>
#include <QPixmap>
#include <QScrollBar>
ImageViewer::ImageViewer(QWidget *parent)
: QDialog(parent),
m_image(0)
{
setupUi(this);
connect(lowerSpinBox, SIGNAL(valueChanged(double)),
SLOT(slotUpdate()));
connect(upperSpinBox, SIGNAL(valueChanged(double)),
SLOT(slotUpdate()));
connect(flipCheckBox, SIGNAL(stateChanged(int)),
SLOT(slotUpdate()));
connect(opaqueCheckBox, SIGNAL(stateChanged(int)),
SLOT(slotUpdate()));
connect(alphaCheckBox, SIGNAL(stateChanged(int)),
SLOT(slotUpdate()));
QPixmap px(32, 32);
QPainter p(&px);
p.fillRect(0, 0, 32, 32, Qt::white);
p.fillRect(0, 0, 16, 16, QColor(193, 193, 193));
p.fillRect(16, 16, 16, 16, QColor(193, 193, 193));
p.end();
QPalette pal = scrollAreaWidgetContents->palette();
pal.setBrush(QPalette::Background,
QBrush(px));
pal.setBrush(QPalette::Base,
QBrush(px));
scrollAreaWidgetContents->setPalette(pal);
m_pixelWidget = new PixelWidget(scrollAreaWidgetContents);
verticalLayout_2->addWidget(m_pixelWidget);
rectLabel->hide();
pixelLabel->hide();
connect(m_pixelWidget, SIGNAL(zoomChanged(int)),
zoomSpinBox, SLOT(setValue(int)));
connect(zoomSpinBox, SIGNAL(valueChanged(int)),
m_pixelWidget, SLOT(setZoom(int)));
connect(m_pixelWidget, SIGNAL(mousePosition(int, int)),
this, SLOT(showPixel(int, int)));
connect(m_pixelWidget, SIGNAL(gridGeometry(const QRect &)),
this, SLOT(showGrid(const QRect &)));
}
ImageViewer::~ImageViewer()
{
delete m_image;
}
void ImageViewer::setBase64Data(const QByteArray &base64)
{
delete m_image;
m_image = ApiSurface::imageFromBase64(base64);
m_convertedImage = ApiSurface::qimageFromRawImage(m_image);
m_pixelWidget->setSurface(m_convertedImage);
updateGeometry();
}
void ImageViewer::slotUpdate()
{
m_convertedImage =
m_convertedImage.mirrored(false, flipCheckBox->isChecked());
double lowerValue = lowerSpinBox->value();
double upperValue = upperSpinBox->value();
bool opaque = opaqueCheckBox->isChecked();
bool alpha = alphaCheckBox->isChecked();
m_convertedImage = ApiSurface::qimageFromRawImage(m_image,
lowerValue, upperValue,
opaque, alpha);
if (flipCheckBox->isChecked()) {
m_convertedImage = m_convertedImage.mirrored(false, true);
}
m_pixelWidget->setSurface(m_convertedImage);
updateGeometry();
}
QSize ImageViewer::sizeHint() const
{
QSize size;
int vScrollWidth = scrollArea->verticalScrollBar() ?
scrollArea->verticalScrollBar()->width() : 0;
int hScrollHeight = scrollArea->horizontalScrollBar() ?
scrollArea->horizontalScrollBar()->height() : 0;
QSize optimalWindowSize(m_convertedImage.width() + vScrollWidth,
m_convertedImage.height() + hScrollHeight);
QRect screenRect = QApplication::desktop()->availableGeometry();
const float maxPercentOfDesktopSpace = 0.8f;
QSize maxAvailableSize(maxPercentOfDesktopSpace * screenRect.width(),
maxPercentOfDesktopSpace * screenRect.height());
return QSize(qMin(optimalWindowSize.width(), maxAvailableSize.width()),
qMin(optimalWindowSize.height(), maxAvailableSize.height()));
}
void ImageViewer::resizeEvent(QResizeEvent *e)
{
QWidget::resizeEvent(e);
}
template <class T>
QString createPixelLabel(image::Image *img, int x, int y)
{
QString pixelLabel;
unsigned char *pixelLocation = 0;
T *pixel;
if (x < 0 || y < 0 || x >= img->width || y >= img->height) {
return QString::fromLatin1("(Out of bounds)");
}
pixelLocation = img->pixels + img->stride() * y;
pixelLocation += x * img->bytesPerPixel;
pixel = ((T*)pixelLocation);
pixelLabel += QLatin1String("[");
pixelLabel += QString::fromLatin1("%1").arg((double)pixel[0], 0, 'g', 9);
for (int channel = 1; channel < img->channels; ++channel) {
pixelLabel += QString::fromLatin1(", %1").arg((double)pixel[channel], 0, 'g', 9);
}
pixelLabel += QLatin1String("]");
return pixelLabel;
}
void ImageViewer::showPixel(int x, int y)
{
xSpinBox->setValue(x);
ySpinBox->setValue(y);
if (!m_image)
return;
QString label = tr("Pixel: ");
/* If the image is flipped, substitute y to match */
if (flipCheckBox->isChecked()) {
y = m_convertedImage.height() - y - 1;
}
if (m_image->channelType == image::TYPE_UNORM8) {
label += createPixelLabel<unsigned char>(m_image, x, y);
} else {
label += createPixelLabel<float>(m_image, x, y);
}
pixelLabel->setText(label);
pixelLabel->show();
}
void ImageViewer::showGrid(const QRect &rect)
{
if (rect.isEmpty()) {
rectLabel->hide();
return;
}
rectLabel->setText(tr("Grid: [%1, %2, %3, %4]")
.arg(rect.x())
.arg(rect.y())
.arg(rect.width())
.arg(rect.height()));
rectLabel->show();
}
#include "imageviewer.moc"
|