KLGImageProvider.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 VisionX::ArmarXObjects::KLGImageProvider
17  * @author Markus Grotz ( markus dot grotz at kit dot edu )
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "KLGImageProvider.h"
24 
25 
26 #include <stdio.h>
27 #include <filesystem>
28 
29 #include <Image/ImageProcessor.h>
30 
31 #include <zlib.h>
32 
33 
36 
37 
38 
39 using namespace armarx;
40 
42 {
43  offeringTopic(getProperty<std::string>("DebugObserverName").getValue());
44  offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
45 
46  doRewind = getProperty<bool>("Rewind").getValue();
47 
48  Eigen::Vector2i dimensions = getProperty<Eigen::Vector2i>("Dimensions").getValue();
49  setImageFormat(visionx::ImageDimension(dimensions(0), dimensions(1)), visionx::eRgb);
50  setNumberImages(2);
51 
52  rgbImages = new CByteImage*[2];
53  rgbImages[0] = new CByteImage(dimensions(0), dimensions(1), CByteImage::eRGB24);
54  rgbImages[1] = new CByteImage(dimensions(0), dimensions(1), CByteImage::eRGB24);
55  depthBuffer = cv::Mat(dimensions(1), dimensions(0), CV_16UC1, 0.0);
56 
57  std::string fileName = getProperty<std::string>("FileName").getValue();
58 
59  if (!ArmarXDataPath::getAbsolutePath(fileName, fileName))
60  {
61  ARMARX_ERROR << "Could not find point cloud file in ArmarXDataPath: " << fileName;
62  }
63 
64 
65  if (!std::filesystem::exists(fileName.c_str()))
66  {
67  ARMARX_FATAL << " files does not exists " << fileName;
68  return;
69  }
70 
71  fp = fopen(fileName.c_str(), "rb");
72 
73  currentFrame = 0;
74 
75  if (fread(&totalFrames, sizeof(int32_t), 1, fp) != sizeof(int32_t))
76  {
77  ARMARX_FATAL << "invalid file";
78  return;
79  }
80 
81  ARMARX_INFO << "total number of frames " << totalFrames;
82 
83 }
84 
85 
86 void KLGImageProvider::onStartCapture(float framesPerSecond)
87 {
88  debugObserver = getTopic<DebugObserverInterfacePrx>(getProperty<std::string>("DebugObserverName").getValue());
89  debugDrawer = getTopic<DebugDrawerInterfacePrx>(getProperty<std::string>("DebugDrawerTopicName").getValue());
90 
91  /*
92  StringVariantBaseMap debugValues;
93  debugValues["debug_value"] = new Variant(1.0);
94  debugObserver->setDebugChannel(getName(), debugValues);
95  */
96 }
97 
98 
100 {
101 
102 }
103 
104 
105 bool KLGImageProvider::capture(void** ppImageBuffers)
106 {
107 
108  if (currentFrame > totalFrames)
109  {
110  if (doRewind)
111  {
112  currentFrame = 0;
113  rewind(fp);
114 
115  [[maybe_unused]] auto r = fread(&totalFrames, sizeof(int32_t), 1, fp);
116  }
117  else
118  {
119  getArmarXManager()->asyncShutdown();
120  stopCapture();
121  return false;
122  }
123  }
124 
125  ::ImageProcessor::Zero(rgbImages[0]);
126  ::ImageProcessor::Zero(rgbImages[1]);
127 
128  int64_t timestamp;
129  int depthSize;
130  int32_t imageSize;
131 
132  [[maybe_unused]] auto r = fread(&timestamp, sizeof(int64_t), 1, fp);
133  r = fread(&depthSize, sizeof(int32_t), 1, fp);
134  r = fread(&imageSize, sizeof(int32_t), 1, fp);
135 
136  if (depthSize == rgbImages[1]->height * rgbImages[1]->width * 2)
137  {
138  r = fread(depthBuffer.data, depthSize, 1, fp);
139  //fread(rgbImages[1]->pixels, imageSize, 1, fp);
140  }
141  else if (depthSize > 0)
142  {
143  uint8_t* tmpImage = new uint8_t[depthSize];
144 
145  unsigned long decompLength = rgbImages[1]->height * rgbImages[1]->width * 2;
146 
147  r = fread(tmpImage, depthSize, 1, fp);
148  uncompress(depthBuffer.data, &decompLength, tmpImage, depthSize);
149  }
150 
151  if (imageSize == rgbImages[0]->height * rgbImages[0]->width * 3)
152  {
153  r = fread(rgbImages[0]->pixels, imageSize, 1, fp);
154  }
155  else if (imageSize > 0)
156  {
157  std::vector<uint8_t> tmpImage(imageSize, 0);
158 
159  r = fread(&tmpImage[0], imageSize, 1, fp);
160  cv::Mat decImg = cv::imdecode(tmpImage, 1);
161  imageSize = decImg.step * decImg.rows;
162  memcpy(rgbImages[0]->pixels, decImg.data, decImg.step * decImg.rows);
163  }
164 
165  currentFrame++;
166 
167  for (int i = 0; i < rgbImages[1]->height; i++)
168  {
169  for (int j = 0; j < rgbImages[1]->width; j++)
170  {
171  unsigned short value = depthBuffer.at<unsigned short>(i, j);
172 
173  rgbImages[1]->pixels[3 * (i * rgbImages[1]->width + j) + 0] = value & 0xFF;
174  rgbImages[1]->pixels[3 * (i * rgbImages[1]->width + j) + 1] = (value >> 8) & 0xFF;
175  }
176  }
177 
178  // TODO set the timestamp
179 
181 
182  for (size_t i = 0; i < 2; i++)
183  {
184  memcpy(ppImageBuffers[i], rgbImages[i]->pixels, imageSize);
185  }
186 
187  return true;
188 }
189 
190 
191 
193 {
194  fclose(fp);
195 
196  for (size_t i = 0; i < 2; i++)
197  {
198  delete rgbImages[i];
199  }
200 
201  delete [] rgbImages;
202 }
203 
204 
206 {
209 }
210 
armarx::IceSharedMemoryProvider::getScopedWriteLock
SharedMemoryScopedWriteLockPtr getScopedWriteLock() const
Retrieve scoped lock for writing to the memory.
Definition: IceSharedMemoryProvider.h:156
ArmarXManager.h
armarx::KLGImageProvider::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
Definition: KLGImageProvider.cpp:41
armarx::ManagedIceObject::getArmarXManager
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
Definition: ManagedIceObject.cpp:348
armarx::KLGImageProvider::onStartCapture
void onStartCapture(float frameRate) override
This is called when the image provider capturing has been started.
Definition: KLGImageProvider.cpp:86
visionx::CapturingImageProvider::stopCapture
void stopCapture(const Ice::Current &c=Ice::emptyCurrent) override
Stops image capturing.
Definition: CapturingImageProvider.cpp:53
armarx::KLGImageProvider::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
Definition: KLGImageProvider.cpp:192
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:46
visionx::ImageProvider::setImageFormat
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
Definition: ImageProvider.cpp:275
visionx::ImageProvider::sharedMemoryProvider
armarx::IceSharedMemoryProvider< unsigned char >::pointer_type sharedMemoryProvider
shared memory provider
Definition: ImageProvider.h:256
KLGImageProvider.h
ARMARX_FATAL
#define ARMARX_FATAL
Definition: Logging.h:192
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::KLGImageProvider::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: KLGImageProvider.cpp:205
armarx::KLGImageProviderPropertyDefinitions
Definition: KLGImageProvider.h:48
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:106
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
visionx::ImageProvider::setNumberImages
void setNumberImages(int numberImages)
Sets the number of images on each capture.
Definition: ImageProvider.cpp:313
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::KLGImageProvider::onStopCapture
void onStopCapture() override
This is called when the image provider capturing has been stopped.
Definition: KLGImageProvider.cpp:99
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ManagedIceObject::offeringTopic
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
Definition: ManagedIceObject.cpp:290
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:111
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28