Skip to content

Commit 9b5016f

Browse files
authored
Merge pull request #421 from anastasiia-kornilova/2d/show_image
Add draw function for pixel array
2 parents 55bbd67 + 11e5bfd commit 9b5016f

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

plugins/robots/interpreters/trikKitInterpreterCommon/include/trikKitInterpreterCommon/robotModel/twoD/parts/twoDDisplay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public slots: // needed for invokeMethod
7272
void paint(QPainter *painter, const QRect &outputRect) override;
7373
void reset() override;
7474
void redraw() override;
75+
void show(const QVector<int32_t> &array, int width, int height, const QString &format);
7576

7677
signals:
7778
/// Emitted when bacground color has changed.

plugins/robots/interpreters/trikKitInterpreterCommon/include/trikKitInterpreterCommon/trikEmulation/trikdisplayemu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TrikDisplayEmu : public trikControl::DisplayInterface
4141

4242
public slots:
4343
virtual void showImage(const QString &fileName) override;
44+
virtual void show(const QVector<int32_t> &array, int width, int height, const QString &format) override;
4445
virtual void addLabel(const QString &text, int x, int y) override;
4546
virtual void removeLabels() override {}
4647
virtual void setPainterColor(const QString &color) override;

plugins/robots/interpreters/trikKitInterpreterCommon/src/robotModel/twoD/parts/twoDDisplay.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "trikKitInterpreterCommon/robotModel/twoD/parts/twoDDisplay.h"
1616

1717
#include <QtCore/QJsonArray>
18+
#include <thirdparty/qslog/QsLog.h>
1819

1920
#include <utils/canvas/textObject.h>
2021

@@ -48,6 +49,8 @@ Display::Display(const DeviceInfo &info
4849
emit propertyChanged("objects", toJson());
4950
}
5051
});
52+
53+
qRegisterMetaType<QVector<int32_t>>("QVector<int32_t>");
5154
}
5255

5356
QString Display::background() const
@@ -188,3 +191,43 @@ void Display::redraw()
188191
mEngine.display()->repaintDisplay();
189192
}
190193
}
194+
195+
void Display::show(const QVector<int32_t> &array, int width, int height, const QString &format)
196+
{
197+
auto fmt = QImage::Format_RGB32;
198+
auto *rawData = static_cast<const uchar *>(static_cast<const void *>(array.data()));
199+
200+
// Reserve maximum possible size to avoid reallocation
201+
QVector<uchar> formattedData( (width + 3) * (height + 3) * 3 + 3);
202+
203+
// QImage requires 32-bit aligned scan lines
204+
// Helper function to convert data
205+
auto copyAligned = [&](int perLine){
206+
auto scanLineSize = static_cast<int>((static_cast<unsigned>(perLine + 3)) & 0xFFFFFFFC);
207+
formattedData.resize(scanLineSize * height);
208+
auto dst = formattedData.begin();
209+
for (auto src = array.begin(); src < array.end(); src += perLine) {
210+
dst = std::copy(src, src + perLine, dst);
211+
dst += scanLineSize - perLine;
212+
}
213+
rawData = formattedData.constData();
214+
};
215+
216+
if (!format.compare("rgb32", Qt::CaseInsensitive)) {
217+
/* do nothing */
218+
} else if (!format.compare("rgb888", Qt::CaseInsensitive)) {
219+
fmt = QImage::Format_RGB888;
220+
copyAligned(3 * width);
221+
} else if (format == "grayscale8") {
222+
fmt = QImage::Format_Grayscale8;
223+
copyAligned(width);
224+
} else {
225+
QLOG_ERROR() << "Unsupported format" << format;
226+
return;
227+
}
228+
229+
// QImage doesn't create copy of a buffer and requires clean up function
230+
// Simple workaround -- create deep-copy using "copy" function
231+
mCurrentImage = QImage(rawData, width, height, fmt).copy();
232+
mEngine.display()->repaintDisplay();
233+
}

plugins/robots/interpreters/trikKitInterpreterCommon/src/trikEmulation/trikdisplayemu.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ void TrikDisplayEmu::showImage(const QString &fileName)
4444
Q_ARG(bool, smile));
4545
}
4646

47+
void TrikDisplayEmu::show(const QVector<int32_t> &array, int width, int height, const QString &format)
48+
{
49+
QMetaObject::invokeMethod(mDisplay, "show", Q_ARG(QVector<int32_t>, array)
50+
, Q_ARG(int, width), Q_ARG(int, height), Q_ARG(QString, format));
51+
}
52+
4753
void TrikDisplayEmu::addLabel(const QString &text, int x, int y)
4854
{
4955
QMetaObject::invokeMethod(mDisplay,

0 commit comments

Comments
 (0)