From d7d733131d62777481c9a72fee9005dd58cf0c74 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 22 Oct 2013 02:33:11 +0200 Subject: Optimize widget redraw Signed-off-by: Stanislaw Halik --- FTNoIR_Tracker_PT/pt_video_widget.cpp | 35 ++++++++++++++++++++------------ FTNoIR_Tracker_PT/pt_video_widget.h | 4 +++- ftnoir_tracker_aruco/ar_video_widget.cpp | 26 ++++++++++++++++++------ ftnoir_tracker_aruco/ar_video_widget.h | 10 ++++++++- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/FTNoIR_Tracker_PT/pt_video_widget.cpp b/FTNoIR_Tracker_PT/pt_video_widget.cpp index 03c42fc7..35a2c42b 100644 --- a/FTNoIR_Tracker_PT/pt_video_widget.cpp +++ b/FTNoIR_Tracker_PT/pt_video_widget.cpp @@ -18,19 +18,7 @@ using namespace std; void PTVideoWidget::update_image(const cv::Mat& frame) { QMutexLocker foo(&mtx); - QImage qframe = QImage(frame.cols, frame.rows, QImage::Format_RGB888); - uchar* data = qframe.bits(); - const int pitch = qframe.bytesPerLine(); - for (int y = 0; y < frame.rows; y++) - for (int x = 0; x < frame.cols; x++) - { - const int pos = 3 * (y*frame.cols + x); - data[y * pitch + x * 3 + 0] = frame.data[pos + 2]; - data[y * pitch + x * 3 + 1] = frame.data[pos + 1]; - data[y * pitch + x * 3 + 2] = frame.data[pos + 0]; - } - qframe = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); - pixmap = QPixmap::fromImage(qframe); + _frame = frame; } // ---------------------------------------------------------------------------- @@ -50,3 +38,24 @@ VideoWidgetDialog::VideoWidgetDialog(QWidget *parent, FrameProvider* provider) setLayout(layout); resize(VIDEO_FRAME_WIDTH, VIDEO_FRAME_HEIGHT); } + +void PTVideoWidget::update_and_repaint() +{ + QMutexLocker foo(&mtx); + if (_frame.empty()) + return; + QImage qframe = QImage(_frame.cols, _frame.rows, QImage::Format_RGB888); + uchar* data = qframe.bits(); + const int pitch = qframe.bytesPerLine(); + for (int y = 0; y < _frame.rows; y++) + for (int x = 0; x < _frame.cols; x++) + { + const int pos = 3 * (y*_frame.cols + x); + data[y * pitch + x * 3 + 0] = _frame.data[pos + 2]; + data[y * pitch + x * 3 + 1] = _frame.data[pos + 1]; + data[y * pitch + x * 3 + 2] = _frame.data[pos + 0]; + } + qframe = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); + pixmap = QPixmap::fromImage(qframe); + update(); +} diff --git a/FTNoIR_Tracker_PT/pt_video_widget.h b/FTNoIR_Tracker_PT/pt_video_widget.h index e67e6d57..f5663e47 100644 --- a/FTNoIR_Tracker_PT/pt_video_widget.h +++ b/FTNoIR_Tracker_PT/pt_video_widget.h @@ -31,7 +31,7 @@ class PTVideoWidget : public QWidget, public FrameObserver public: PTVideoWidget(QWidget *parent, FrameProvider* provider) : QWidget(parent), /* to avoid linker errors */ FrameObserver(provider) { - connect(&timer, SIGNAL(timeout()), this, SLOT(update())); + connect(&timer, SIGNAL(timeout()), this, SLOT(update_and_repaint())); timer.start(45); } void update_image(const cv::Mat &frame); @@ -42,10 +42,12 @@ protected slots: QPainter painter(this); painter.drawPixmap(e->rect(), pixmap, e->rect()); } + void update_and_repaint(); private: QMutex mtx; QPixmap pixmap; QTimer timer; + cv::Mat _frame; }; // ---------------------------------------------------------------------------- diff --git a/ftnoir_tracker_aruco/ar_video_widget.cpp b/ftnoir_tracker_aruco/ar_video_widget.cpp index 6a4572a0..b727679b 100644 --- a/ftnoir_tracker_aruco/ar_video_widget.cpp +++ b/ftnoir_tracker_aruco/ar_video_widget.cpp @@ -14,17 +14,31 @@ using namespace std; void ArucoVideoWidget::update_image(unsigned char *frame, int width, int height) { QMutexLocker foo(&mtx); + memcpy(fb, frame, width * height * 3); + this->width = width; + this->height = height; +} + +void ArucoVideoWidget::update_and_repaint() +{ + QMutexLocker foo(&mtx); + if (width*height <= 0) + return; QImage qframe = QImage(width, height, QImage::Format_RGB888); uchar* data = qframe.bits(); const int pitch = qframe.bytesPerLine(); for (int y = 0; y < height; y++) + { + const int part = y*width; for (int x = 0; x < width; x++) { - const int pos = 3 * (y*width + x); - data[y * pitch + x * 3 + 0] = frame[pos + 2]; - data[y * pitch + x * 3 + 1] = frame[pos + 1]; - data[y * pitch + x * 3 + 2] = frame[pos + 0]; + const int pos = 3 * (part + x); + data[y * pitch + x * 3 + 0] = fb[pos + 2]; + data[y * pitch + x * 3 + 1] = fb[pos + 1]; + data[y * pitch + x * 3 + 2] = fb[pos + 0]; } - qframe = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); - pixmap = QPixmap::fromImage(qframe); + } + auto qframe2 = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); + pixmap = QPixmap::fromImage(qframe2); + update(); } diff --git a/ftnoir_tracker_aruco/ar_video_widget.h b/ftnoir_tracker_aruco/ar_video_widget.h index dd0c16ac..3a1574cd 100644 --- a/ftnoir_tracker_aruco/ar_video_widget.h +++ b/ftnoir_tracker_aruco/ar_video_widget.h @@ -15,6 +15,7 @@ #include #include #include +#include // ---------------------------------------------------------------------------- class ArucoVideoWidget : public QWidget @@ -22,7 +23,9 @@ class ArucoVideoWidget : public QWidget Q_OBJECT public: - ArucoVideoWidget(QWidget *parent) : QWidget(parent), mtx() { + ArucoVideoWidget(QWidget *parent) : QWidget(parent), width(0), height(0), fb{0} { + connect(&timer, SIGNAL(timeout()), this, SLOT(update_and_repaint())); + timer.start(60); } void update_image(unsigned char* frame, int width, int height); protected slots: @@ -31,9 +34,14 @@ protected slots: QPainter painter(this); painter.drawPixmap(e->rect(), pixmap, e->rect()); } + void update_and_repaint(); + private: QMutex mtx; QPixmap pixmap; + QTimer timer; + char fb[2048*2048*3]; + int width,height; }; #endif // VIDEOWIDGET_H -- cgit v1.2.3