ResultImageFuser.cpp
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::ArmarXObjects::ResultImageFuser
19  * @author Markus Grotz ( markus dot grotz at kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "ResultImageFuser.h"
26 
27 
28 using namespace armarx;
29 using namespace visionx;
30 
31 
32 
34 {
35  numImages = getProperty<int>("numImages").getValue();
36  height = getProperty<int>("height").getValue();
37  width = getProperty<int>("width").getValue();
38 
39  colorMask = getProperty<Eigen::Vector3i>("colorMask").getValue();
40 
41  ARMARX_INFO << "color mask is r:" << colorMask(0) << " g:" << colorMask(1) << " b:" << colorMask(2);
42 
43  setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
44  setNumberImages(numImages);
45 
46  bytesPerPixel = getImageFormat().bytesPerPixel;
47 
48  pollImagesTask = new PeriodicTask<ResultImageFuser>(this, &ResultImageFuser::pollImageProviders, 50);
49 }
50 
52 {
53 
54 }
55 
56 
57 
58 void ResultImageFuser::onStartCapture(float frameRate)
59 {
60  pollImagesTask->start();
61 
62  if (imageSources.size() == 0)
63  {
64  std::vector<std::string> imageProviders = getProperty<std::vector<std::string>>("imageProviders").getValue();
65  setResultImageProviders(imageProviders);
66  }
67 }
68 
70 {
71  pollImagesTask->stop();
72 }
73 
74 bool ResultImageFuser::capture(void** ppImages)
75 {
76  usleep(10000);
77 
78  const size_t imageSize = width * height * bytesPerPixel;
79  CByteImage* resultImage = visionx::tools::createByteImage(getImageFormat(), visionx::eRgb);
80 
81  bool imageCaptured = false;
82 
83  for (auto& any : imageSources)
84  {
85 
86  std::string proxyName = any.first;
87 
88  if (!imageAvailable[proxyName])
89  {
90  continue;
91  }
92  else
93  {
94  imageAvailable[proxyName] = false;
95  }
96 
97  ImageProviderInterfacePrx providerPrx;
98 
99  try
100  {
101  providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
102  }
103  catch (...)
104  {
105  ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
106  continue;
107  }
108 
109 
110 
111  CByteImage** providerImages = any.second;
112  for (int n = 0; n < numImages; n++)
113  {
114  CByteImage* image = providerImages[n];
115 
116  for (int j = 0; j < height; j++)
117  {
118  for (int i = 0; i < width; i++)
119  {
120 
121  int r = image->pixels[3 * (j * width + i) + 0];
122  int g = image->pixels[3 * (j * width + i) + 1];
123  int b = image->pixels[3 * (j * width + i) + 2];
124 
125  if (!(r == colorMask(0) && g == colorMask(1) && b == colorMask(2)))
126  {
127  resultImage->pixels[3 * (j * width + i) + 0] = r;
128  resultImage->pixels[3 * (j * width + i) + 1] = g;
129  resultImage->pixels[3 * (j * width + i) + 2] = b;
130  }
131  }
132  }
133  }
134 
135  imageCaptured = true;
136  }
137 
138  {
139  armarx::SharedMemoryScopedWriteLockPtr lock = getScopedWriteLock();
140 
141  for (int i = 0; i < numImages; i++)
142  {
143  memcpy(ppImages[i], resultImage->pixels, imageSize);
144  }
145  }
146 
147  return imageCaptured;
148 }
149 
150 
151 
152 void ResultImageFuser::setResultImageProviders(std::vector<std::string> imageProviders)
153 {
154  imageSources.clear();
155  // removeProxyDependency()
156 
157  for (std::string& proxyName : imageProviders)
158  {
159 
160  ImageProviderInterfacePrx providerPrx;
161 
162  try
163  {
164  providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
165  }
166  catch (...)
167  {
168  ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
169  continue;
170  }
171 
172  const int providerNumImages = providerPrx->getNumberImages();
173  ImageFormatInfo imageFormat = providerPrx->getImageFormat();
174 
175  assert(imageFormat.dimension.width == width);
176  assert(imageFormat.dimension.height == height);
177  assert(imageFormat.bytesPerPixel == bytesPerPixel);
178  assert(numImages <= providerNumImages);
179 
180  CByteImage** images = new CByteImage*[providerNumImages];
181 
182  for (int n = 0; n < numImages; n++)
183  {
184  // todo visionx::eRgb might be wrong
185  images[n] = visionx::tools::createByteImage(imageFormat, visionx::eRgb);
186  }
187 
188  imageSources[proxyName] = images;
189  imageAvailable[proxyName] = false;
190  }
191 }
192 
193 
194 
195 void ResultImageFuser::pollImageProviders()
196 {
197  std::unique_lock lock(imageMutex);
198 
199  for (auto& any : imageSources)
200  {
201  std::string proxyName = any.first;
202 
203  ImageProviderInterfacePrx providerPrx;
204 
205  try
206  {
207  providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
208  }
209  catch (...)
210  {
211  ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
212  continue;
213  }
214 
215  CByteImage** bufferImages = any.second;
216  ImageFormatInfo imageFormat = providerPrx->getImageFormat();
217 
218  armarx::Blob images = providerPrx->getImages();
219 
220  size_t imageSize = width * height * imageFormat.bytesPerPixel;
221 
222  for (int i = 0; i < numImages; i++)
223  {
224  memcpy(bufferImages[i]->pixels, &images[imageSize * i], imageSize);
225  }
226 
227  imageAvailable[proxyName] = true;
228  }
229 }
230 
232 {
234  getConfigIdentifier()));
235 }
236 
armarx::ResultImageFuser::onStartCapture
void onStartCapture(float frameRate) override
Definition: ResultImageFuser.cpp:58
armarx::ResultImageFuser::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
Definition: ResultImageFuser.cpp:51
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::ResultImageFuser::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
Definition: ResultImageFuser.cpp:33
armarx::ResultImageFuserPropertyDefinitions
Definition: ResultImageFuser.h:67
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:46
visionx::tools::createByteImage
CByteImage * createByteImage(const ImageFormatInfo &imageFormat, const ImageType imageType)
Creates a ByteImage for the destination type specified in the given imageProviderInfo.
armarx::ResultImageFuser::onStopCapture
void onStopCapture() override
Definition: ResultImageFuser.cpp:69
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:106
ResultImageFuser.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ResultImageFuser::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ResultImageFuser.cpp:231
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28