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 <qpainter.h>
26 #include <qimage.h>
27 
28 #include <IceUtil/Time.h>
29 
31 
32 
33 namespace visionx
34 {
36  : QWidget(parent)
37  {
38  inputWidth = -1;
39  inputHeight = -1;
40  numberImages = 0;
41 
42  buffer = 0;
43  }
44 
46  {
47  if (buffer)
48  {
49  delete [] buffer;
50  }
51  }
52 
53  void ImageViewerArea::paintEvent(QPaintEvent* e)
54  {
55  if (numberImages == 0)
56  {
57  return;
58  }
59 
60  QPainter painter(this);
61  std::scoped_lock lock(imageMutex);
62  painter.drawImage(0, 0, scaledImage);
63  }
64 
66  {
67  return numberImages;
68  }
69 
71  {
72  return displayDelay;
73  }
74 
76  {
77  return timeDisplayed;
78  }
79 
80  void ImageViewerArea::setImages(int numberImages, CByteImage** images, IceUtil::Time imageTimestamp, IceUtil::Time receiveTimestamp)
81  {
82  std::scoped_lock lock(bufferMutex);
83  this->imageTimestamp = imageTimestamp;
84  this->receiveTimestamp = receiveTimestamp;
85  if (inputWidth != images[0]->width || inputHeight != images[0]->height || this->numberImages != numberImages)
86  {
87  inputWidth = images[0]->width;
88  inputHeight = images[0]->height;
89  this->numberImages = numberImages;
90 
91  if (buffer)
92  {
93  delete [] buffer;
94  }
95 
96  buffer = new unsigned char[inputWidth * inputHeight * 4 * numberImages];
97  }
98 
99 
100  for (int i = 0 ; i < numberImages ; i++)
101  {
102  if (images[i]->type == CByteImage::eRGB24)
103  {
104  unsigned char* pixels = images[i]->pixels;
105  int* output = (int*) buffer;
106  int inputOffset = 0;
107 
108  for (int y = 0 ; y < inputHeight ; y++)
109  {
110  int baseOffset = y * inputWidth * numberImages + i * inputWidth;
111  for (int x = 0 ; x < inputWidth ; x++)
112  {
113  output[x + baseOffset] = 255 << 24 | pixels[inputOffset] << 16 | pixels[inputOffset + 1] << 8 | pixels[inputOffset + 2];
114  inputOffset += 3;
115  }
116  }
117 
118  }
119  }
120 
121  QImage image(buffer, inputWidth * this->numberImages, inputHeight, QImage::Format_RGB32);
122  //scaledImage = image.scaled (width(), height(), Qt::KeepAspectRatio, Qt::FastTransformation);
123  std::scoped_lock lock2(imageMutex);
124  scaledImage = image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
125 
126 
127  QMetaObject::invokeMethod(this, "updateImage", Q_ARG(long, receiveTimestamp.toMicroSeconds()));
128  }
129 
130  void ImageViewerArea::updateImage(long timeReceived)
131  {
132  update(0, 0, width(), height());
133  timeDisplayed = armarx::TimeUtil::GetTime();
134  displayDelay = timeDisplayed - IceUtil::Time::microSeconds(timeReceived);
135  }
136 
138  {
139  return {(float) scaledImage.width(), (float) scaledImage.height()};
140  }
141 
142 }
143 
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ImageViewerArea::updateImage
void updateImage(long timeReceived)
Definition: ImageViewerArea.cpp:130
visionx::ImageViewerArea::getTimeDisplayed
IceUtil::Time getTimeDisplayed() const
Definition: ImageViewerArea.cpp:75
GfxTL::Vec2d
VectorXD< 2, double > Vec2d
Definition: VectorXD.h:694
visionx::ImageViewerArea::setImages
void setImages(int numberImages, CByteImage **images, IceUtil::Time imageTimestamp, IceUtil::Time receiveTimestamp)
Definition: ImageViewerArea.cpp:80
visionx::ImageViewerArea::getDisplayDelay
IceUtil::Time getDisplayDelay() const
Definition: ImageViewerArea.cpp:70
visionx::ImageViewerArea::paintEvent
void paintEvent(QPaintEvent *pPaintEvent) override
Definition: ImageViewerArea.cpp:53
visionx::ImageViewerArea::~ImageViewerArea
~ImageViewerArea() override
Definition: ImageViewerArea.cpp:45
ImageViewerArea.h
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
visionx::ImageViewerArea::getScaledImageDimensions
Vec2d getScaledImageDimensions()
Definition: ImageViewerArea.cpp:137
TimeUtil.h
float
#define float
Definition: 16_Level.h:22
visionx::ImageViewerArea::getNumberImages
int getNumberImages()
Definition: ImageViewerArea.cpp:65
visionx::ImageViewerArea::ImageViewerArea
ImageViewerArea(QWidget *parent=0)
Definition: ImageViewerArea.cpp:35