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#include <stdio.h>
26
27#include <filesystem>
28
31
32#include <Image/ImageProcessor.h>
33#include <zlib.h>
34
35
36using namespace armarx;
37
38void
40{
41 offeringTopic(getProperty<std::string>("DebugObserverName").getValue());
42 offeringTopic(getProperty<std::string>("DebugDrawerTopicName").getValue());
43
44 doRewind = getProperty<bool>("Rewind").getValue();
45
46 Eigen::Vector2i dimensions = getProperty<Eigen::Vector2i>("Dimensions").getValue();
47 setImageFormat(visionx::ImageDimension(dimensions(0), dimensions(1)), visionx::eRgb);
49
50 rgbImages = new CByteImage*[2];
51 rgbImages[0] = new CByteImage(dimensions(0), dimensions(1), CByteImage::eRGB24);
52 rgbImages[1] = new CByteImage(dimensions(0), dimensions(1), CByteImage::eRGB24);
53 depthBuffer = cv::Mat(dimensions(1), dimensions(0), CV_16UC1, 0.0);
54
55 std::string fileName = getProperty<std::string>("FileName").getValue();
56
57 if (!ArmarXDataPath::getAbsolutePath(fileName, fileName))
58 {
59 ARMARX_ERROR << "Could not find point cloud file in ArmarXDataPath: " << fileName;
60 }
61
62
63 if (!std::filesystem::exists(fileName.c_str()))
64 {
65 ARMARX_FATAL << " files does not exists " << fileName;
66 return;
67 }
68
69 fp = fopen(fileName.c_str(), "rb");
70
71 currentFrame = 0;
72
73 if (fread(&totalFrames, sizeof(int32_t), 1, fp) != sizeof(int32_t))
74 {
75 ARMARX_FATAL << "invalid file";
76 return;
77 }
78
79 ARMARX_INFO << "total number of frames " << totalFrames;
80}
81
82void
84{
86 getProperty<std::string>("DebugObserverName").getValue());
88 getProperty<std::string>("DebugDrawerTopicName").getValue());
89
90 /*
91 StringVariantBaseMap debugValues;
92 debugValues["debug_value"] = new Variant(1.0);
93 debugObserver->setDebugChannel(getName(), debugValues);
94 */
95}
96
97void
101
102bool
103KLGImageProvider::capture(void** ppImageBuffers)
104{
105
106 if (currentFrame > totalFrames)
107 {
108 if (doRewind)
109 {
110 currentFrame = 0;
111 rewind(fp);
112
113 [[maybe_unused]] auto r = fread(&totalFrames, sizeof(int32_t), 1, fp);
114 }
115 else
116 {
117 getArmarXManager()->asyncShutdown();
118 stopCapture();
119 return false;
120 }
121 }
122
123 ::ImageProcessor::Zero(rgbImages[0]);
124 ::ImageProcessor::Zero(rgbImages[1]);
125
126 int64_t timestamp;
127 int depthSize;
128 int32_t imageSize;
129
130 [[maybe_unused]] auto r = fread(&timestamp, sizeof(int64_t), 1, fp);
131 r = fread(&depthSize, sizeof(int32_t), 1, fp);
132 r = fread(&imageSize, sizeof(int32_t), 1, fp);
133
134 if (depthSize == rgbImages[1]->height * rgbImages[1]->width * 2)
135 {
136 r = fread(depthBuffer.data, depthSize, 1, fp);
137 //fread(rgbImages[1]->pixels, imageSize, 1, fp);
138 }
139 else if (depthSize > 0)
140 {
141 uint8_t* tmpImage = new uint8_t[depthSize];
142
143 unsigned long decompLength = rgbImages[1]->height * rgbImages[1]->width * 2;
144
145 r = fread(tmpImage, depthSize, 1, fp);
146 uncompress(depthBuffer.data, &decompLength, tmpImage, depthSize);
147 }
148
149 if (imageSize == rgbImages[0]->height * rgbImages[0]->width * 3)
150 {
151 r = fread(rgbImages[0]->pixels, imageSize, 1, fp);
152 }
153 else if (imageSize > 0)
154 {
155 std::vector<uint8_t> tmpImage(imageSize, 0);
156
157 r = fread(&tmpImage[0], imageSize, 1, fp);
158 cv::Mat decImg = cv::imdecode(tmpImage, 1);
159 imageSize = decImg.step * decImg.rows;
160 memcpy(rgbImages[0]->pixels, decImg.data, decImg.step * decImg.rows);
161 }
162
163 currentFrame++;
164
165 for (int i = 0; i < rgbImages[1]->height; i++)
166 {
167 for (int j = 0; j < rgbImages[1]->width; j++)
168 {
169 unsigned short value = depthBuffer.at<unsigned short>(i, j);
170
171 rgbImages[1]->pixels[3 * (i * rgbImages[1]->width + j) + 0] = value & 0xFF;
172 rgbImages[1]->pixels[3 * (i * rgbImages[1]->width + j) + 1] = (value >> 8) & 0xFF;
173 }
174 }
175
176 // TODO set the timestamp
177
179
180 for (size_t i = 0; i < 2; i++)
181 {
182 memcpy(ppImageBuffers[i], rgbImages[i]->pixels, imageSize);
183 }
184
185 return true;
186}
187
188void
190{
191 fclose(fp);
192
193 for (size_t i = 0; i < 2; i++)
194 {
195 delete rgbImages[i];
196 }
197
198 delete[] rgbImages;
199}
200
std::string timestamp()
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)
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.
void onInitCapturingImageProvider() override
This is called when the Component::onInitComponent() is called.
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.