ImageViewerArea.cpp
Go to the documentation of this file.
1/*
2* This file is part of ArmarX.
3*
4* ArmarX is free software; you can redistribute it and/or modify
5* it under the terms of the GNU General Public License version 2 as
6* published by the Free Software Foundation.
7*
8* ArmarX is distributed in the hope that it will be useful, but
9* WITHOUT ANY WARRANTY; without even the implied warranty of
10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11* GNU General Public License for more details.
12*
13* You should have received a copy of the GNU General Public License
14* along with this program. If not, see <http://www.gnu.org/licenses/>.
15*
16* @package ArmarX::
17* @author Kai Welke ( welke at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
23#include "ImageViewerArea.h"
24
25#include <qimage.h>
26#include <qpainter.h>
27
28#include <IceUtil/Time.h>
29
31
32namespace visionx
33{
34 ImageViewerArea::ImageViewerArea(QWidget* parent) : QWidget(parent)
35 {
36 inputWidth = -1;
37 inputHeight = -1;
38 numberImages = 0;
39
40 buffer = 0;
41 }
42
44 {
45 if (buffer)
46 {
47 delete[] buffer;
48 }
49 }
50
51 void
53 {
54 if (numberImages == 0)
55 {
56 return;
57 }
58
59 QPainter painter(this);
60 std::scoped_lock lock(imageMutex);
61 painter.drawImage(0, 0, scaledImage);
62 }
63
64 int
66 {
67 return numberImages;
68 }
69
70 IceUtil::Time
72 {
73 return displayDelay;
74 }
75
76 IceUtil::Time
78 {
79 return timeDisplayed;
80 }
81
82 void
84 CByteImage** images,
85 IceUtil::Time imageTimestamp,
86 IceUtil::Time receiveTimestamp)
87 {
88 std::scoped_lock lock(bufferMutex);
89 this->imageTimestamp = imageTimestamp;
90 this->receiveTimestamp = receiveTimestamp;
91 if (inputWidth != images[0]->width || inputHeight != images[0]->height ||
92 this->numberImages != numberImages)
93 {
94 inputWidth = images[0]->width;
95 inputHeight = images[0]->height;
96 this->numberImages = numberImages;
97
98 if (buffer)
99 {
100 delete[] buffer;
101 }
102
103 buffer = new unsigned char[inputWidth * inputHeight * 4 * numberImages];
104 }
105
106
107 for (int i = 0; i < numberImages; i++)
108 {
109 if (images[i]->type == CByteImage::eRGB24)
110 {
111 unsigned char* pixels = images[i]->pixels;
112 int* output = (int*)buffer;
113 int inputOffset = 0;
114
115 for (int y = 0; y < inputHeight; y++)
116 {
117 int baseOffset = y * inputWidth * numberImages + i * inputWidth;
118 for (int x = 0; x < inputWidth; x++)
119 {
120 output[x + baseOffset] = 255 << 24 | pixels[inputOffset] << 16 |
121 pixels[inputOffset + 1] << 8 |
122 pixels[inputOffset + 2];
123 inputOffset += 3;
124 }
125 }
126 }
127 }
128
129 QImage image(buffer, inputWidth * this->numberImages, inputHeight, QImage::Format_RGB32);
130 //scaledImage = image.scaled (width(), height(), Qt::KeepAspectRatio, Qt::FastTransformation);
131 std::scoped_lock lock2(imageMutex);
132 scaledImage =
133 image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
134
135
136 QMetaObject::invokeMethod(
137 this, "updateImage", Q_ARG(long, receiveTimestamp.toMicroSeconds()));
138 }
139
140 void
142 {
143 update(0, 0, width(), height());
144 timeDisplayed = armarx::TimeUtil::GetTime();
145 displayDelay = timeDisplayed - IceUtil::Time::microSeconds(timeReceived);
146 }
147
148 Vec2d
150 {
151 return {(float)scaledImage.width(), (float)scaledImage.height()};
152 }
153
154} // namespace visionx
#define float
Definition 16_Level.h:22
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition TimeUtil.cpp:42
void updateImage(long timeReceived)
IceUtil::Time getDisplayDelay() const
IceUtil::Time getTimeDisplayed() const
void setImages(int numberImages, CByteImage **images, IceUtil::Time imageTimestamp, IceUtil::Time receiveTimestamp)
void paintEvent(QPaintEvent *pPaintEvent) override
ImageViewerArea(QWidget *parent=0)
This file offers overloads of toIce() and fromIce() functions for STL container types.
ArmarX headers.