ImageProvider.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package VisionX::Core
19 * @author Jan Issac (jan dot issac at gmx dot net)
20 * @author Kai Welke (kai dot welke at kit dot edu)
21 * @date 2011
22 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23 * GNU General Public License
24 */
25
26
27#pragma once
28
29// STD/STL
30#include <deque>
31#include <mutex>
32#include <unordered_map>
33
34// OpenCV
35#include <opencv2/core/mat.hpp>
36
37// IVT
38#include <Image/ByteImage.h>
39#include <Image/FloatImage.h>
40
41// ArmarX
46
47#include <RobotAPI/interface/units/UnitInterface.h>
48
49#include <VisionX/interface/core/CompressedImageProviderInterface.h>
50#include <VisionX/interface/core/DataTypes.h>
51#include <VisionX/interface/core/ImageProcessorInterface.h>
53
54namespace visionx
55{
56 using CByteImageUPtr = std::unique_ptr<CByteImage>;
57 using CByteImageUPtrVec = std::vector<std::unique_ptr<CByteImage>>;
58
59 // ====================================================================== //
60 // == class ImageProvider declaration =============================== //
61 // ====================================================================== //
62 /**
63 * ImageProvider abstract class defines a component which provide images
64 * via ice or shared memory. *
65 */
67 virtual public armarx::Component,
68 virtual public CompressedImageProviderInterface
69 {
70 public:
71 // ================================================================== //
72 // == ImageProvider ice interface =================================== //
73 // ================================================================== //
74 /**
75 * Retrieve images via Ice. Bypasses the IcesharedMemoryProvider
76 */
77 armarx::Blob getImages(const Ice::Current& c = Ice::emptyCurrent) override;
78
79 armarx::Blob getImagesAndMetaInfo(armarx::MetaInfoSizeBasePtr&,
80 const Ice::Current&) override;
81
82
83 /**
84 * Returns the entire image format info struct via Ice
85 */
86 ImageFormatInfo getImageFormat(const Ice::Current& c = Ice::emptyCurrent) override;
87
88 /**
89 * Retrieve number of images handled by this provider
90 */
91 int getNumberImages(const Ice::Current& c = Ice::emptyCurrent) override;
92
93 bool
94 hasSharedMemorySupport(const Ice::Current& c = Ice::emptyCurrent) override
95 {
96 return true;
97 }
98
99 protected:
100 // ================================================================== //
101 // == Interface of ImageProvider ================================= //
102 // ================================================================== //
103 /**
104 * This is called when the Component::onInitComponent() is called.
105 *
106 * Implement this method in the ImageProvider in order to setup its
107 * parameters. Use this to set the image format.
108 */
109 virtual void onInitImageProvider() = 0;
110
111 /**
112 * This is called when the Component::onConnectComponent() setup is called
113 */
114 virtual void
118
119 virtual void
123
124 /**
125 * This is called when the Component::onExitComponent() setup is called
126 *
127 * Implement this method in the ImageProvider in order clean up right
128 * before terminating.
129 */
130 virtual void onExitImageProvider() = 0;
131
132 // ================================================================== //
133 // == Utility methods for ImageProviders ============================ //
134 // ================================================================== //
135 /**
136 * Sets the number of images on each capture
137 *
138 * @param numberImages number of images on each capture cycle
139 */
140 void setNumberImages(int numberImages);
141
142 /**
143 * Sets the image basic format data.
144 *
145 * @param imageDimension size of image
146 *
147 * @param imageType Image type
148 *
149 * @param bayerPatternType Bayer Pattern type if using BayerPattern as
150 * image type
151 */
152 void setImageFormat(ImageDimension imageDimension,
153 ImageType imageType,
154 BayerPatternType bayerPatternType = visionx::eBayerPatternRg);
155
156 /**
157 * send images raw. Take care to fit imageFormat. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
158 */
159 void provideImages(void** inputBuffers,
160 const IceUtil::Time& imageTimestamp = IceUtil::Time());
161
162 /**
163 * send ByteImages. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
164 */
165 void provideImages(CByteImage** images,
166 const IceUtil::Time& imageTimestamp = IceUtil::Time());
167 void provideImages(const std::vector<CByteImageUPtr>& images,
168 const IceUtil::Time& imageTimestamp = IceUtil::Time());
169
170 /**
171 * send FloatImages. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
172 */
173 void provideImages(CFloatImage** images,
174 const IceUtil::Time& imageTimestamp = IceUtil::Time());
175
176 bool startImageRecording(const imrec::Config& cfg, const Ice::Current&) override;
177 imrec::Status getImageRecordingStatus(const Ice::Current&) override;
178 bool stopImageRecording(const Ice::Current&) override;
179 std::vector<imrec::ChannelPreferences>
180 getImageRecordingChannelPreferences(const Ice::Current&) override;
181
182 // ================================================================== //
183 // == Component implementation =============================== //
184 // ================================================================== //
185 /**
186 * @see Component::onInitComponent()
187 */
188 void onInitComponent() override;
189
190 /**
191 * @see Component::onConnectComponent()
192 */
193 void onConnectComponent() override;
194 void onDisconnectComponent() override;
195
196 /**
197 * @see Component::onExitComponent()
198 */
199 void onExitComponent() override;
200
201 void
202 request(const Ice::Current& c) override
203 {
204 }
205
206 void
207 release(const Ice::Current& c) override
208 {
209 }
210
211 void
212 init(const Ice::Current& c) override
213 {
214 }
215
216 void
217 start(const Ice::Current& c) override
218 {
219 }
220
221 void
222 stop(const Ice::Current& c) override
223 {
224 }
225
226 armarx::UnitExecutionState
227 getExecutionState(const Ice::Current& c) override
228 {
229 return armarx::eUndefinedUnitExecutionState;
230 }
231
232 protected:
233 /**
234 * Retrieve scoped lock for writing to the memory.
235 *
236 * @return the scoped lock
237 */
240 {
241 auto prov = sharedMemoryProvider;
242 if (prov)
243 {
244 return sharedMemoryProvider->getScopedWriteLock();
245 }
246 else
247 {
249 }
250 }
251
252 /**
253 * @brief Updates the timestamp of the currently captured image.
254 * @param timestamp in Milleseconds since epoch
255 */
256 void updateTimestamp(Ice::Long timestamp, bool threadSafe = true);
257 void updateTimestamp(IceUtil::Time timestamp, bool threadSafe = true);
258 void updateTimestamp(const armarx::DateTime& timestamp, bool threadSafe = true);
259
260 void recordImages(const IceUtil::Time& image_timestamp);
261
262 /**
263 * Retrieve whether provider is exiting
264 *
265 * @return exiting status
266 */
267 bool
269 {
270 return exiting;
271 }
272
273 /**
274 * Ice proxy of the image processor interface
275 */
276 ImageProcessorInterfacePrx imageProcessorProxy;
277
278 /**
279 * Image buffer memory
280 */
282
283 /**
284 * shared memory provider
285 */
287
289
290 private:
291 /**
292 * Image information
293 */
294 ImageFormatInfo imageFormat;
295
296 /**
297 * number of images
298 */
299 int numberImages;
300
301 /**
302 * Indicates that exit is in process and thread needs to be stopped
303 */
304 bool exiting;
305
306 struct CompressionData
307 {
308 armarx::Blob compressedImage;
309 IceUtil::Time imageTimestamp;
310 };
311
312 using CompressionDataMap = std::map<std::pair<CompressionType, int>, CompressionData>;
313 CompressionDataMap compressionDataMap;
314 std::mutex compressionDataMapMutex;
315
316 struct RecordingProperties
317 {
318 std::mutex callMutex;
319 imrec::Config config;
321 std::unordered_map<int, visionx::imrec::Recording> channelRecordings;
322 std::mutex statusMutex;
323 imrec::Status status;
324 std::condition_variable cv;
325 std::mutex bufferMutex;
326 std::deque<std::tuple<std::chrono::microseconds, std::unordered_map<int, CByteImage*>>>
327 buffer;
328 };
329
330 RecordingProperties rec;
331
332 // CompressedImageProviderInterface interface
333 public:
334 armarx::Blob getCompressedImagesAndMetaInfo(CompressionType,
335 Ice::Int compressionQuality,
336 armarx::MetaInfoSizeBasePtr& info,
337 const Ice::Current&) override;
338 };
339} // namespace visionx
std::string timestamp()
constexpr T c
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
IceUtil::Handle< IceSharedMemoryProvider< MemoryObject, MemoryObjectMetaInfo > > pointer_type
pointer type for convenience.
IceUtil::Handle< RunningTask< T > > pointer_type
Shared pointer type for convenience.
Represents a point in time.
Definition DateTime.h:25
ImageProvider abstract class defines a component which provide images via ice or shared memory.
virtual void onExitImageProvider()=0
This is called when the Component::onExitComponent() setup is called.
bool hasSharedMemorySupport(const Ice::Current &c=Ice::emptyCurrent) override
void onInitComponent() override
armarx::Blob getImagesAndMetaInfo(armarx::MetaInfoSizeBasePtr &, const Ice::Current &) override
virtual void onDisconnectImageProvider()
void onDisconnectComponent() override
Hook for subclass.
void stop(const Ice::Current &c) override
void request(const Ice::Current &c) override
void recordImages(const IceUtil::Time &image_timestamp)
bool isExiting()
Retrieve whether provider is exiting.
armarx::IceSharedMemoryProvider< unsignedchar >::pointer_type sharedMemoryProvider
shared memory provider
virtual void onInitImageProvider()=0
This is called when the Component::onInitComponent() is called.
void updateTimestamp(Ice::Long timestamp, bool threadSafe=true)
Updates the timestamp of the currently captured image.
void release(const Ice::Current &c) override
armarx::UnitExecutionState getExecutionState(const Ice::Current &c) override
void start(const Ice::Current &c) override
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
void setImageFormat(ImageDimension imageDimension, ImageType imageType, BayerPatternType bayerPatternType=visionx::eBayerPatternRg)
Sets the image basic format data.
bool startImageRecording(const imrec::Config &cfg, const Ice::Current &) override
bool stopImageRecording(const Ice::Current &) override
int getNumberImages(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve number of images handled by this provider.
virtual void onConnectImageProvider()
This is called when the Component::onConnectComponent() setup is called.
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
void onConnectComponent() override
void setNumberImages(int numberImages)
Sets the number of images on each capture.
void ** imageBuffers
Image buffer memory.
armarx::SharedMemoryScopedWriteLockPtr getScopedWriteLock()
Retrieve scoped lock for writing to the memory.
std::vector< imrec::ChannelPreferences > getImageRecordingChannelPreferences(const Ice::Current &) override
ImageProcessorInterfacePrx imageProcessorProxy
Ice proxy of the image processor interface.
void init(const Ice::Current &c) override
armarx::Blob getImages(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve images via Ice.
void onExitComponent() override
imrec::Status getImageRecordingStatus(const Ice::Current &) override
armarx::Blob getCompressedImagesAndMetaInfo(CompressionType, Ice::Int compressionQuality, armarx::MetaInfoSizeBasePtr &info, const Ice::Current &) override
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition helper.h:35
ArmarX headers.
std::vector< std::unique_ptr< CByteImage > > CByteImageUPtrVec
std::unique_ptr< CByteImage > CByteImageUPtr