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 void
33 {
34  numImages = getProperty<int>("numImages").getValue();
35  height = getProperty<int>("height").getValue();
36  width = getProperty<int>("width").getValue();
37 
38  colorMask = getProperty<Eigen::Vector3i>("colorMask").getValue();
39 
40  ARMARX_INFO << "color mask is r:" << colorMask(0) << " g:" << colorMask(1)
41  << " b:" << colorMask(2);
42 
43  setImageFormat(visionx::ImageDimension(width, height), visionx::eRgb);
44  setNumberImages(numImages);
45 
46  bytesPerPixel = getImageFormat().bytesPerPixel;
47 
48  pollImagesTask =
49  new PeriodicTask<ResultImageFuser>(this, &ResultImageFuser::pollImageProviders, 50);
50 }
51 
52 void
54 {
55 }
56 
57 void
59 {
60  pollImagesTask->start();
61 
62  if (imageSources.size() == 0)
63  {
64  std::vector<std::string> imageProviders =
65  getProperty<std::vector<std::string>>("imageProviders").getValue();
66  setResultImageProviders(imageProviders);
67  }
68 }
69 
70 void
72 {
73  pollImagesTask->stop();
74 }
75 
76 bool
77 ResultImageFuser::capture(void** ppImages)
78 {
79  usleep(10000);
80 
81  const size_t imageSize = width * height * bytesPerPixel;
82  CByteImage* resultImage = visionx::tools::createByteImage(getImageFormat(), visionx::eRgb);
83 
84  bool imageCaptured = false;
85 
86  for (auto& any : imageSources)
87  {
88 
89  std::string proxyName = any.first;
90 
91  if (!imageAvailable[proxyName])
92  {
93  continue;
94  }
95  else
96  {
97  imageAvailable[proxyName] = false;
98  }
99 
100  ImageProviderInterfacePrx providerPrx;
101 
102  try
103  {
104  providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
105  }
106  catch (...)
107  {
108  ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
109  continue;
110  }
111 
112 
113  CByteImage** providerImages = any.second;
114  for (int n = 0; n < numImages; n++)
115  {
116  CByteImage* image = providerImages[n];
117 
118  for (int j = 0; j < height; j++)
119  {
120  for (int i = 0; i < width; i++)
121  {
122 
123  int r = image->pixels[3 * (j * width + i) + 0];
124  int g = image->pixels[3 * (j * width + i) + 1];
125  int b = image->pixels[3 * (j * width + i) + 2];
126 
127  if (!(r == colorMask(0) && g == colorMask(1) && b == colorMask(2)))
128  {
129  resultImage->pixels[3 * (j * width + i) + 0] = r;
130  resultImage->pixels[3 * (j * width + i) + 1] = g;
131  resultImage->pixels[3 * (j * width + i) + 2] = b;
132  }
133  }
134  }
135  }
136 
137  imageCaptured = true;
138  }
139 
140  {
141  armarx::SharedMemoryScopedWriteLockPtr lock = getScopedWriteLock();
142 
143  for (int i = 0; i < numImages; i++)
144  {
145  memcpy(ppImages[i], resultImage->pixels, imageSize);
146  }
147  }
148 
149  return imageCaptured;
150 }
151 
152 void
153 ResultImageFuser::setResultImageProviders(std::vector<std::string> imageProviders)
154 {
155  imageSources.clear();
156  // removeProxyDependency()
157 
158  for (std::string& proxyName : imageProviders)
159  {
160 
161  ImageProviderInterfacePrx providerPrx;
162 
163  try
164  {
165  providerPrx = getProxy<ImageProviderInterfacePrx>(proxyName, false);
166  }
167  catch (...)
168  {
169  ARMARX_WARNING << "provider" << proxyName << " is no longer here.";
170  continue;
171  }
172 
173  const int providerNumImages = providerPrx->getNumberImages();
174  ImageFormatInfo imageFormat = providerPrx->getImageFormat();
175 
176  assert(imageFormat.dimension.width == width);
177  assert(imageFormat.dimension.height == height);
178  assert(imageFormat.bytesPerPixel == bytesPerPixel);
179  assert(numImages <= providerNumImages);
180 
181  CByteImage** images = new CByteImage*[providerNumImages];
182 
183  for (int n = 0; n < numImages; n++)
184  {
185  // todo visionx::eRgb might be wrong
186  images[n] = visionx::tools::createByteImage(imageFormat, visionx::eRgb);
187  }
188 
189  imageSources[proxyName] = images;
190  imageAvailable[proxyName] = false;
191  }
192 }
193 
194 void
195 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 
233 {
234  return PropertyDefinitionsPtr(new ResultImageFuserPropertyDefinitions(getConfigIdentifier()));
235 }
armarx::ResultImageFuser::onStartCapture
void onStartCapture(float frameRate) override
Definition: ResultImageFuser.cpp:58
armarx::ResultImageFuser::onExitCapturingImageProvider
void onExitCapturingImageProvider() override
Definition: ResultImageFuser.cpp:53
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
armarx::ResultImageFuser::onInitCapturingImageProvider
void onInitCapturingImageProvider() override
Definition: ResultImageFuser.cpp:32
armarx::ResultImageFuserPropertyDefinitions
Definition: ResultImageFuser.h:66
armarx::SharedMemoryScopedWriteLockPtr
std::shared_ptr< SharedMemoryScopedWriteLock > SharedMemoryScopedWriteLockPtr
Definition: SharedMemoryProvider.h:47
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:71
visionx::CapturingImageProvider::capture
virtual void capture()
Definition: CapturingImageProvider.cpp:109
ResultImageFuser.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::ResultImageFuser::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ResultImageFuser.cpp:232
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27