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
45 #include <ArmarXCore/util/tasks.h>
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 
54 
55 namespace visionx
56 {
57  using CByteImageUPtr = std::unique_ptr<CByteImage>;
58  using CByteImageUPtrVec = std::vector<std::unique_ptr<CByteImage>>;
59  // ====================================================================== //
60  // == class ImageProvider declaration =============================== //
61  // ====================================================================== //
62  /**
63  * ImageProvider abstract class defines a component which provide images
64  * via ice or shared memory. *
65  */
66  class ImageProvider :
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&, const Ice::Current&) override;
80 
81 
82  /**
83  * Returns the entire image format info struct via Ice
84  */
85  ImageFormatInfo getImageFormat(const Ice::Current& c = Ice::emptyCurrent) override;
86 
87  /**
88  * Retrieve number of images handled by this provider
89  */
90  int getNumberImages(const Ice::Current& c = Ice::emptyCurrent) override;
91 
92 
93  bool hasSharedMemorySupport(const Ice::Current& c = Ice::emptyCurrent) override
94  {
95  return true;
96  }
97 
98  protected:
99  // ================================================================== //
100  // == Interface of ImageProvider ================================= //
101  // ================================================================== //
102  /**
103  * This is called when the Component::onInitComponent() is called.
104  *
105  * Implement this method in the ImageProvider in order to setup its
106  * parameters. Use this to set the image format.
107  */
108  virtual void onInitImageProvider() = 0;
109 
110  /**
111  * This is called when the Component::onConnectComponent() setup is called
112  */
113  virtual void onConnectImageProvider() { }
114  virtual void onDisconnectImageProvider() { }
115 
116  /**
117  * This is called when the Component::onExitComponent() setup is called
118  *
119  * Implement this method in the ImageProvider in order clean up right
120  * before terminating.
121  */
122  virtual void onExitImageProvider() = 0;
123 
124  // ================================================================== //
125  // == Utility methods for ImageProviders ============================ //
126  // ================================================================== //
127  /**
128  * Sets the number of images on each capture
129  *
130  * @param numberImages number of images on each capture cycle
131  */
132  void setNumberImages(int numberImages);
133 
134  /**
135  * Sets the image basic format data.
136  *
137  * @param imageDimension size of image
138  *
139  * @param imageType Image type
140  *
141  * @param bayerPatternType Bayer Pattern type if using BayerPattern as
142  * image type
143  */
144  void setImageFormat(
145  ImageDimension imageDimension,
146  ImageType imageType,
147  BayerPatternType bayerPatternType = visionx::eBayerPatternRg);
148 
149  /**
150  * send images raw. Take care to fit imageFormat. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
151  */
152  void provideImages(void** inputBuffers, const IceUtil::Time& imageTimestamp = IceUtil::Time());
153 
154  /**
155  * send ByteImages. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
156  */
157  void provideImages(CByteImage** images, const IceUtil::Time& imageTimestamp = IceUtil::Time());
158  void provideImages(const std::vector<CByteImageUPtr >& images, const IceUtil::Time& imageTimestamp = IceUtil::Time());
159 
160  /**
161  * send FloatImages. Timestamp is used to call updateTimestamp(), when the mutex is locked (only done when timestamp > 0)
162  */
163  void provideImages(CFloatImage** images, const IceUtil::Time& imageTimestamp = IceUtil::Time());
164 
165  bool startImageRecording(const imrec::Config& cfg, const Ice::Current&) override;
166  imrec::Status getImageRecordingStatus(const Ice::Current&) override;
167  bool stopImageRecording(const Ice::Current&) override;
168  std::vector<imrec::ChannelPreferences> getImageRecordingChannelPreferences(const Ice::Current&) override;
169 
170  // ================================================================== //
171  // == Component implementation =============================== //
172  // ================================================================== //
173  /**
174  * @see Component::onInitComponent()
175  */
176  void onInitComponent() override;
177 
178  /**
179  * @see Component::onConnectComponent()
180  */
181  void onConnectComponent() override;
182  void onDisconnectComponent() override;
183 
184  /**
185  * @see Component::onExitComponent()
186  */
187  void onExitComponent() override;
188 
189  void request(const Ice::Current& c) override { }
190 
191  void release(const Ice::Current& c) override { }
192 
193  void init(const Ice::Current& c) override { }
194 
195  void start(const Ice::Current& c) override { }
196 
197  void stop(const Ice::Current& c) override { }
198 
199  armarx::UnitExecutionState getExecutionState(const Ice::Current& c) override
200  {
201  return armarx::eUndefinedUnitExecutionState;
202  }
203 
204  protected:
205  /**
206  * Retrieve scoped lock for writing to the memory.
207  *
208  * @return the scoped lock
209  */
211  {
212  auto prov = sharedMemoryProvider;
213  if (prov)
214  {
216  }
217  else
218  {
220  }
221  }
222 
223  /**
224  * @brief Updates the timestamp of the currently captured image.
225  * @param timestamp in Milleseconds since epoch
226  */
227  void updateTimestamp(Ice::Long timestamp, bool threadSafe = true);
228  void updateTimestamp(IceUtil::Time timestamp, bool threadSafe = true);
229  void updateTimestamp(const armarx::DateTime& timestamp, bool threadSafe = true);
230 
231  void recordImages(const IceUtil::Time& image_timestamp);
232 
233  /**
234  * Retrieve whether provider is exiting
235  *
236  * @return exiting status
237  */
238  bool isExiting()
239  {
240  return exiting;
241  }
242 
243  /**
244  * Ice proxy of the image processor interface
245  */
246  ImageProcessorInterfacePrx imageProcessorProxy;
247 
248  /**
249  * Image buffer memory
250  */
251  void** imageBuffers;
252 
253  /**
254  * shared memory provider
255  */
257 
259 
260  private:
261  /**
262  * Image information
263  */
264  ImageFormatInfo imageFormat;
265 
266  /**
267  * number of images
268  */
269  int numberImages;
270 
271  /**
272  * Indicates that exit is in process and thread needs to be stopped
273  */
274  bool exiting;
275 
276  struct CompressionData
277  {
278  armarx::Blob compressedImage;
279  IceUtil::Time imageTimestamp;
280  };
281  using CompressionDataMap = std::map<std::pair<CompressionType, int>, CompressionData>;
282  CompressionDataMap compressionDataMap;
283  std::mutex compressionDataMapMutex;
284 
285  struct RecordingProperties
286  {
287  std::mutex callMutex;
288  imrec::Config config;
290  std::unordered_map<int, visionx::imrec::Recording> channelRecordings;
291  std::mutex statusMutex;
292  imrec::Status status;
293  std::condition_variable cv;
294  std::mutex bufferMutex;
295  std::deque<std::tuple<std::chrono::microseconds, std::unordered_map<int, CByteImage*>>> buffer;
296  };
297 
298  RecordingProperties rec;
299 
300  // CompressedImageProviderInterface interface
301  public:
302  armarx::Blob getCompressedImagesAndMetaInfo(CompressionType, Ice::Int compressionQuality, armarx::MetaInfoSizeBasePtr& info, const Ice::Current&) override;
303  };
304 }
305 
visionx::ImageProvider::onExitImageProvider
virtual void onExitImageProvider()=0
This is called when the Component::onExitComponent() setup is called.
armarx::IceSharedMemoryProvider::getScopedWriteLock
SharedMemoryScopedWriteLockPtr getScopedWriteLock() const
Retrieve scoped lock for writing to the memory.
Definition: IceSharedMemoryProvider.h:156
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::ImageProvider::stopImageRecording
bool stopImageRecording(const Ice::Current &) override
Definition: ImageProvider.cpp:531
visionx::ImageProvider::startImageRecording
bool startImageRecording(const imrec::Config &cfg, const Ice::Current &) override
Definition: ImageProvider.cpp:473
visionx::ImageProvider::onExitComponent
void onExitComponent() override
Definition: ImageProvider.cpp:152
visionx::CByteImageUPtrVec
std::vector< std::unique_ptr< CByteImage > > CByteImageUPtrVec
Definition: ImageProvider.h:58
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::ImageProvider::getNumberImages
int getNumberImages(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve number of images handled by this provider.
Definition: ImageProvider.cpp:80
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::getImagesAndMetaInfo
armarx::Blob getImagesAndMetaInfo(armarx::MetaInfoSizeBasePtr &, const Ice::Current &) override
Definition: ImageProvider.cpp:65
visionx::ImageProvider::getCompressedImagesAndMetaInfo
armarx::Blob getCompressedImagesAndMetaInfo(CompressionType, Ice::Int compressionQuality, armarx::MetaInfoSizeBasePtr &info, const Ice::Current &) override
Definition: ImageProvider.cpp:202
visionx::ImageProvider::getImageFormat
ImageFormatInfo getImageFormat(const Ice::Current &c=Ice::emptyCurrent) override
Returns the entire image format info struct via Ice.
Definition: ImageProvider.cpp:75
visionx::ImageProvider::start
void start(const Ice::Current &c) override
Definition: ImageProvider.h:195
visionx::ImageProvider::updateTimestamp
void updateTimestamp(Ice::Long timestamp, bool threadSafe=true)
Updates the timestamp of the currently captured image.
Definition: ImageProvider.cpp:165
visionx::ImageProvider::sharedMemoryProvider
armarx::IceSharedMemoryProvider< unsigned char >::pointer_type sharedMemoryProvider
shared memory provider
Definition: ImageProvider.h:256
IceSharedMemoryProvider.h
visionx::ImageProvider::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ImageProvider.cpp:141
visionx::ImageProvider::imageBuffers
void ** imageBuffers
Image buffer memory.
Definition: ImageProvider.h:251
visionx::ImageProvider::getImageRecordingChannelPreferences
std::vector< imrec::ChannelPreferences > getImageRecordingChannelPreferences(const Ice::Current &) override
Definition: ImageProvider.cpp:705
armarx::status
status
Definition: FiniteStateMachine.h:259
visionx::CByteImageUPtr
std::unique_ptr< CByteImage > CByteImageUPtr
Definition: ImageProvider.h:57
visionx::ImageProvider::stop
void stop(const Ice::Current &c) override
Definition: ImageProvider.h:197
imrec.h
visionx::ImageProvider::imageRecordingRunningTask
void imageRecordingRunningTask()
Definition: ImageProvider.cpp:560
visionx::ImageProvider::request
void request(const Ice::Current &c) override
Definition: ImageProvider.h:189
visionx::ImageProvider::onInitImageProvider
virtual void onInitImageProvider()=0
This is called when the Component::onInitComponent() is called.
visionx::ImageProvider::recordImages
void recordImages(const IceUtil::Time &image_timestamp)
Definition: ImageProvider.cpp:425
visionx::ImageProvider::getExecutionState
armarx::UnitExecutionState getExecutionState(const Ice::Current &c) override
Definition: ImageProvider.h:199
visionx::ImageProvider::onConnectImageProvider
virtual void onConnectImageProvider()
This is called when the Component::onConnectComponent() setup is called.
Definition: ImageProvider.h:113
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
visionx::ImageProvider::release
void release(const Ice::Current &c) override
Definition: ImageProvider.h:191
visionx::ImageProvider::getImageRecordingStatus
imrec::Status getImageRecordingStatus(const Ice::Current &) override
Definition: ImageProvider.cpp:522
tasks.h
visionx::ImageProvider::provideImages
void provideImages(void **inputBuffers, const IceUtil::Time &imageTimestamp=IceUtil::Time())
send images raw.
Definition: ImageProvider.cpp:319
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
forward_declarations.h
Component.h
visionx::ImageProvider::imageProcessorProxy
ImageProcessorInterfacePrx imageProcessorProxy
Ice proxy of the image processor interface.
Definition: ImageProvider.h:246
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
visionx::ImageProvider::hasSharedMemorySupport
bool hasSharedMemorySupport(const Ice::Current &c=Ice::emptyCurrent) override
Definition: ImageProvider.h:93
visionx::ImageProvider::setNumberImages
void setNumberImages(int numberImages)
Sets the number of images on each capture.
Definition: ImageProvider.cpp:313
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
visionx::ImageProvider::getImages
armarx::Blob getImages(const Ice::Current &c=Ice::emptyCurrent) override
Retrieve images via Ice.
Definition: ImageProvider.cpp:54
visionx::ImageProvider
ImageProvider abstract class defines a component which provide images via ice or shared memory.
Definition: ImageProvider.h:66
visionx::ImageProvider::onConnectComponent
void onConnectComponent() override
Definition: ImageProvider.cpp:104
IceUtil::Handle
Definition: forward_declarations.h:29
visionx::ImageProvider::onInitComponent
void onInitComponent() override
Definition: ImageProvider.cpp:88
visionx::ImageProvider::getScopedWriteLock
armarx::SharedMemoryScopedWriteLockPtr getScopedWriteLock()
Retrieve scoped lock for writing to the memory.
Definition: ImageProvider.h:210
visionx::ImageProvider::init
void init(const Ice::Current &c) override
Definition: ImageProvider.h:193
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
cv
Definition: helper.h:35
visionx::ImageProvider::isExiting
bool isExiting()
Retrieve whether provider is exiting.
Definition: ImageProvider.h:238
visionx::ImageProvider::onDisconnectImageProvider
virtual void onDisconnectImageProvider()
Definition: ImageProvider.h:114