CameraImageViewer.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "CameraImageViewer.h"
11 
12 // Module specification
13 // <rtc-template block="module_spec">
14 static const char* cameraimageviewercomponent_spec[] =
15 {
16  "implementation_id", "CameraImageViewer",
17  "type_name", "CameraImageViewer",
18  "description", "camera image viewer component",
19  "version", HRPSYS_PACKAGE_VERSION,
20  "vendor", "AIST",
21  "category", "example",
22  "activity_type", "DataFlowComponent",
23  "max_instance", "10",
24  "language", "C++",
25  "lang_type", "compile",
26  // Configuration variables
27  "conf.default.depthBits", "11",
28 
29  ""
30 };
31 // </rtc-template>
32 
34  : RTC::DataFlowComponentBase(manager),
35  // <rtc-template block="initializer">
36  m_imageIn("imageIn", m_image),
37  m_imageOldIn("imageOldIn", m_imageOld),
38  // </rtc-template>
39  m_cvImage(NULL),
40  dummy(0)
41 {
42 }
43 
45 {
46 }
47 
48 
49 
51 {
52  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
53  // <rtc-template block="bind_config">
54  // Bind variables and configuration variable
55  bindParameter("depthBits", m_depthBits, "11");
56 
57  // </rtc-template>
58 
59  // Registration: InPort/OutPort/Service
60  // <rtc-template block="registration">
61  // Set InPort buffers
62  addInPort("imageIn", m_imageIn);
63  addInPort("imageOldIn", m_imageOldIn);
64 
65  // Set OutPort buffer
66 
67  // Set service provider to Ports
68 
69  // Set service consumers to Ports
70 
71  // Set CORBA Service Ports
72 
73  // </rtc-template>
74 
76 
77  return RTC::RTC_OK;
78 }
79 
80 
81 
82 /*
83  RTC::ReturnCode_t CameraImageViewer::onFinalize()
84  {
85  return RTC::RTC_OK;
86  }
87 */
88 
89 /*
90  RTC::ReturnCode_t CameraImageViewer::onStartup(RTC::UniqueId ec_id)
91  {
92  return RTC::RTC_OK;
93  }
94 */
95 
96 /*
97  RTC::ReturnCode_t CameraImageViewer::onShutdown(RTC::UniqueId ec_id)
98  {
99  return RTC::RTC_OK;
100  }
101 */
102 
104 {
105  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
106  cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
107  return RTC::RTC_OK;
108 }
109 
111 {
112  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
113  if (m_cvImage) {
114  cvReleaseImage(&m_cvImage);
115  m_cvImage = NULL;
116  }
117  cvDestroyWindow("Image");
118  return RTC::RTC_OK;
119 }
120 
122 {
123  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
124 
125  if (m_imageIn.isNew()){
126  do {
127  m_imageIn.read();
128  }while(m_imageIn.isNew());
129  if (m_cvImage && (m_image.data.image.width != m_cvImage->width
130  || m_image.data.image.height != m_cvImage->height)){
131  cvReleaseImage(&m_cvImage);
132  m_cvImage = NULL;
133  }
134  if (!m_cvImage){
135  switch (m_image.data.image.format){
136  case Img::CF_RGB:
137  m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
138  m_image.data.image.height),
139  IPL_DEPTH_8U, 3);
140  break;
141  case Img::CF_GRAY:
142  case Img::CF_DEPTH:
143  m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
144  m_image.data.image.height),
145  IPL_DEPTH_8U, 1);
146  break;
147  default:
148  std::cerr << "unsupported color format("
149  << m_image.data.image.format << ")" << std::endl;
150  return RTC::RTC_ERROR;
151  }
152  }
153  switch(m_image.data.image.format){
154  case Img::CF_RGB:
155  {
156  // RGB -> BGR
157  unsigned char *src = m_image.data.image.raw_data.get_buffer();
158  char *dst;
159  for (int i=0; i<m_cvImage->height; i++){
160  for (int j=0; j<m_cvImage->width; j++){
161  dst = m_cvImage->imageData + m_cvImage->widthStep*i + j*3;
162  dst[0] = src[2];
163  dst[1] = src[1];
164  dst[2] = src[0];
165  src += 3;
166  }
167  }
168  break;
169  }
170  case Img::CF_GRAY:
171  memcpy(m_cvImage->imageData,
172  m_image.data.image.raw_data.get_buffer(),
173  m_image.data.image.raw_data.length());
174  break;
175  case Img::CF_DEPTH:
176  {
177  // depth -> gray scale
178  char *dst = m_cvImage->imageData;
179  Img::ImageData &id = m_image.data.image;
180  unsigned short *src = (unsigned short *)id.raw_data.get_buffer();
181  int shift = m_depthBits - 8;
182  for (unsigned int i=0; i<id.width*id.height; i++){
183  dst[i] = 0xff - src[i]>>shift;
184  }
185  }
186  break;
187  default:
188  break;
189  }
190  }
191 
192  if (m_imageOldIn.isNew()){
193  do {
194  m_imageOldIn.read();
195  }while(m_imageOldIn.isNew());
196  if (m_cvImage && (m_imageOld.width != m_cvImage->width
197  || m_imageOld.height != m_cvImage->height)){
198  cvReleaseImage(&m_cvImage);
199  m_cvImage = NULL;
200  }
201  int bytes = m_imageOld.bpp/8;
202  if (!bytes){
203  bytes = m_imageOld.pixels.length()/(m_imageOld.width*m_imageOld.height);
204  }
205  if (!m_cvImage){
206  m_cvImage = cvCreateImage(cvSize(m_imageOld.width,
207  m_imageOld.height),
208  IPL_DEPTH_8U, bytes);
209  }
210  switch(bytes){
211  case 1:
212  memcpy(m_cvImage->imageData,
213  m_imageOld.pixels.get_buffer(),
214  m_imageOld.pixels.length());
215  break;
216  case 3:
217  // RGB -> BGR
218  char *dst = m_cvImage->imageData;
219  for (unsigned int i=0; i<m_imageOld.pixels.length(); i+=3){
220  dst[i ] = m_imageOld.pixels[i+2];
221  dst[i+1] = m_imageOld.pixels[i+1];
222  dst[i+2] = m_imageOld.pixels[i ];
223  }
224  break;
225  }
226  }
227  if (m_cvImage){
228  cvShowImage("Image",m_cvImage);
229  cvWaitKey(10);
230  }
231 
232  return RTC::RTC_OK;
233 }
234 
235 /*
236  RTC::ReturnCode_t CameraImageViewer::onAborting(RTC::UniqueId ec_id)
237  {
238  return RTC::RTC_OK;
239  }
240 */
241 
242 /*
243  RTC::ReturnCode_t CameraImageViewer::onError(RTC::UniqueId ec_id)
244  {
245  return RTC::RTC_OK;
246  }
247 */
248 
249 /*
250  RTC::ReturnCode_t CameraImageViewer::onReset(RTC::UniqueId ec_id)
251  {
252  return RTC::RTC_OK;
253  }
254 */
255 
256 /*
257  RTC::ReturnCode_t CameraImageViewer::onStateUpdate(RTC::UniqueId ec_id)
258  {
259  return RTC::RTC_OK;
260  }
261 */
262 
263 /*
264  RTC::ReturnCode_t CameraImageViewer::onRateChanged(RTC::UniqueId ec_id)
265  {
266  return RTC::RTC_OK;
267  }
268 */
269 
270 
271 
272 extern "C"
273 {
274 
276  {
278  manager->registerFactory(profile,
279  RTC::Create<CameraImageViewer>,
280  RTC::Delete<CameraImageViewer>);
281  }
282 
283 };
284 
285 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
InPort< Img::TimedCameraImage > m_imageIn
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
virtual ~CameraImageViewer()
Destructor.
void CameraImageViewerInit(RTC::Manager *manager)
static const char * cameraimageviewercomponent_spec[]
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
png_uint_32 i
coil::Properties & getProperties()
Img::TimedCameraImage m_image
ExecutionContextHandle_t UniqueId
InPort< CameraImage > m_imageOldIn
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
def j(str, encoding="cp932")
CameraImageViewer(RTC::Manager *manager)
Constructor.
prop
null component
virtual RTC::ReturnCode_t onInitialize()
virtual bool isNew()
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:49