CameraImageSaver.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <cv.h>
11 #include <highgui.h>
12 #include "CameraImageSaver.h"
13 
14 // Module specification
15 // <rtc-template block="module_spec">
16 static const char* spec[] =
17  {
18  "implementation_id", "CameraImageSaver",
19  "type_name", "CameraImageSaver",
20  "description", "camera image saver",
21  "version", HRPSYS_PACKAGE_VERSION,
22  "vendor", "AIST",
23  "category", "example",
24  "activity_type", "DataFlowComponent",
25  "max_instance", "10",
26  "language", "C++",
27  "lang_type", "compile",
28  // Configuration variables
29  "conf.defalt.basename", "image",
30 
31  ""
32  };
33 // </rtc-template>
34 
36  : RTC::DataFlowComponentBase(manager),
37  // <rtc-template block="initializer">
38  m_imageIn("image", m_image),
39  // </rtc-template>
40  m_count(0),
41  dummy(0)
42 {
43 }
44 
46 {
47 }
48 
49 
50 
51 RTC::ReturnCode_t CameraImageSaver::onInitialize()
52 {
53  //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
54  // <rtc-template block="bind_config">
55  // Bind variables and configuration variable
56  bindParameter("basename", m_basename, "image");
57 
58  // </rtc-template>
59 
60  // Registration: InPort/OutPort/Service
61  // <rtc-template block="registration">
62  // Set InPort buffers
63  addInPort("image", m_imageIn);
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 CameraImageSaver::onFinalize()
84 {
85  return RTC::RTC_OK;
86 }
87 */
88 
89 /*
90 RTC::ReturnCode_t CameraImageSaver::onStartup(RTC::UniqueId ec_id)
91 {
92  return RTC::RTC_OK;
93 }
94 */
95 
96 /*
97 RTC::ReturnCode_t CameraImageSaver::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  return RTC::RTC_OK;
107 }
108 
110 {
111  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
112  return RTC::RTC_OK;
113 }
114 
116 {
117  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
118  if (m_imageIn.isNew()){
119  m_imageIn.read();
120 
121  IplImage* cvImage;
122  switch (m_image.data.image.format){
123  case Img::CF_RGB:
124  cvImage = cvCreateImage(cvSize(m_image.data.image.width,
125  m_image.data.image.height),
126  IPL_DEPTH_8U, 3);
127  break;
128  case Img::CF_GRAY:
129  cvImage = cvCreateImage(cvSize(m_image.data.image.width,
130  m_image.data.image.height),
131  IPL_DEPTH_8U, 1);
132  break;
133  default:
134  std::cerr << "unsupported color format("
135  << m_image.data.image.format << ")" << std::endl;
136  return RTC::RTC_ERROR;
137  }
138  switch(m_image.data.image.format){
139  case Img::CF_RGB:
140  {
141  // RGB -> BGR
142  char *dst = cvImage->imageData;
143  for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
144  dst[i ] = m_image.data.image.raw_data[i+2];
145  dst[i+1] = m_image.data.image.raw_data[i+1];
146  dst[i+2] = m_image.data.image.raw_data[i ];
147  }
148  break;
149  }
150  case Img::CF_GRAY:
151  memcpy(cvImage->imageData,
152  m_image.data.image.raw_data.get_buffer(),
153  m_image.data.image.raw_data.length());
154  break;
155  default:
156  break;
157  }
158 
159  char fname[256];
160  sprintf(fname, "%s%04d.png", m_basename.c_str(), m_count++);
161  cvSaveImage(fname, cvImage);
162 
163  cvReleaseImage(&cvImage);
164  }
165 
166  return RTC::RTC_OK;
167 }
168 
169 /*
170 RTC::ReturnCode_t CameraImageSaver::onAborting(RTC::UniqueId ec_id)
171 {
172  return RTC::RTC_OK;
173 }
174 */
175 
176 /*
177 RTC::ReturnCode_t CameraImageSaver::onError(RTC::UniqueId ec_id)
178 {
179  return RTC::RTC_OK;
180 }
181 */
182 
183 /*
184 RTC::ReturnCode_t CameraImageSaver::onReset(RTC::UniqueId ec_id)
185 {
186  return RTC::RTC_OK;
187 }
188 */
189 
190 /*
191 RTC::ReturnCode_t CameraImageSaver::onStateUpdate(RTC::UniqueId ec_id)
192 {
193  return RTC::RTC_OK;
194 }
195 */
196 
197 /*
198 RTC::ReturnCode_t CameraImageSaver::onRateChanged(RTC::UniqueId ec_id)
199 {
200  return RTC::RTC_OK;
201 }
202 */
203 
204 
205 
206 extern "C"
207 {
208 
210  {
212  manager->registerFactory(profile,
213  RTC::Create<CameraImageSaver>,
214  RTC::Delete<CameraImageSaver>);
215  }
216 
217 };
218 
219 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual ~CameraImageSaver()
Destructor.
Img::TimedCameraImage m_image
void CameraImageSaverInit(RTC::Manager *manager)
png_uint_32 i
coil::Properties & getProperties()
std::string m_basename
ExecutionContextHandle_t UniqueId
CameraImageSaver(RTC::Manager *manager)
Constructor.
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
static const char * spec[]
InPort< Img::TimedCameraImage > m_imageIn
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
prop
std::string sprintf(char const *__restrict fmt,...)
virtual bool isNew()
virtual RTC::ReturnCode_t onInitialize()
bool addInPort(const char *name, InPortBase &inport)
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
camera image saver
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)


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