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
26
27#include <stdio.h>
28
29#include <filesystem>
30
33
34#include <Image/ImageProcessor.h>
35#include <zlib.h>
36
37
38using namespace armarx;
39
40void
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);
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
84void
86{
88 getProperty<std::string>("DebugObserverName").getValue());
90 getProperty<std::string>("DebugDrawerTopicName").getValue());
91
92 /*
93 StringVariantBaseMap debugValues;
94 debugValues["debug_value"] = new Variant(1.0);
95 debugObserver->setDebugChannel(getName(), debugValues);
96 */
97}
98
99void
103
104bool
105KLGImageProvider::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
190void
192{
193 fclose(fp);
194
195 for (size_t i = 0; i < 2; i++)
196 {
197 delete rgbImages[i];
198 }
199
200 delete[] rgbImages;
201}
202
209
210std::string
212{
213 return "KLGImageProvider";
214}
215
216std::string
218{
219 return GetDefaultName();
220}
221
std::string timestamp()
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
Brief description of class KLGImageProvider.
void onStartCapture(float frameRate) override
This is called when the image provider capturing has been started.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onStopCapture() override
This is called when the image provider capturing has been stopped.
void onExitCapturingImageProvider() override
This is called when the Component::onExitComponent() setup is called.
static std::string GetDefaultName()
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
std::string getDefaultName() const override
Retrieve default name of component.
void offeringTopic(const std::string &name)
Registers a topic for retrival after initialization.
TopicProxyType getTopic(const std::string &name)
Returns a proxy of the specified topic.
ArmarXManagerPtr getArmarXManager() const
Returns the ArmarX manager used to add and remove components.
void stopCapture(const Ice::Current &c=Ice::emptyCurrent) override
Stops image capturing.
armarx::IceSharedMemoryProvider< unsignedchar >::pointer_type sharedMemoryProvider
shared memory provider
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
void setNumberImages(int numberImages)
Sets the number of images on each capture.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_FATAL
The logging level for unexpected behaviour, that will lead to a seriously malfunctioning program and ...
Definition Logging.h:199
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.