CameraImageSaver.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <cv.h>
00011 #include <highgui.h>
00012 #include "CameraImageSaver.h"
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* spec[] =
00017   {
00018     "implementation_id", "CameraImageSaver",
00019     "type_name",         "CameraImageSaver",
00020     "description",       "camera image saver",
00021     "version",           HRPSYS_PACKAGE_VERSION,
00022     "vendor",            "AIST",
00023     "category",          "example",
00024     "activity_type",     "DataFlowComponent",
00025     "max_instance",      "10",
00026     "language",          "C++",
00027     "lang_type",         "compile",
00028     // Configuration variables
00029     "conf.defalt.basename", "image",
00030 
00031     ""
00032   };
00033 // </rtc-template>
00034 
00035 CameraImageSaver::CameraImageSaver(RTC::Manager* manager)
00036   : RTC::DataFlowComponentBase(manager),
00037     // <rtc-template block="initializer">
00038     m_imageIn("image", m_image),
00039     // </rtc-template>
00040     m_count(0),
00041     dummy(0)
00042 {
00043 }
00044 
00045 CameraImageSaver::~CameraImageSaver()
00046 {
00047 }
00048 
00049 
00050 
00051 RTC::ReturnCode_t CameraImageSaver::onInitialize()
00052 {
00053   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00054   // <rtc-template block="bind_config">
00055   // Bind variables and configuration variable
00056   bindParameter("basename", m_basename, "image");
00057   
00058   // </rtc-template>
00059 
00060   // Registration: InPort/OutPort/Service
00061   // <rtc-template block="registration">
00062   // Set InPort buffers
00063   addInPort("image", m_imageIn);
00064 
00065   // Set OutPort buffer
00066   
00067   // Set service provider to Ports
00068   
00069   // Set service consumers to Ports
00070   
00071   // Set CORBA Service Ports
00072   
00073   // </rtc-template>
00074 
00075   RTC::Properties& prop = getProperties();
00076 
00077   return RTC::RTC_OK;
00078 }
00079 
00080 
00081 
00082 /*
00083 RTC::ReturnCode_t CameraImageSaver::onFinalize()
00084 {
00085   return RTC::RTC_OK;
00086 }
00087 */
00088 
00089 /*
00090 RTC::ReturnCode_t CameraImageSaver::onStartup(RTC::UniqueId ec_id)
00091 {
00092   return RTC::RTC_OK;
00093 }
00094 */
00095 
00096 /*
00097 RTC::ReturnCode_t CameraImageSaver::onShutdown(RTC::UniqueId ec_id)
00098 {
00099   return RTC::RTC_OK;
00100 }
00101 */
00102 
00103 RTC::ReturnCode_t CameraImageSaver::onActivated(RTC::UniqueId ec_id)
00104 {
00105   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00106   return RTC::RTC_OK;
00107 }
00108 
00109 RTC::ReturnCode_t CameraImageSaver::onDeactivated(RTC::UniqueId ec_id)
00110 {
00111   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00112   return RTC::RTC_OK;
00113 }
00114 
00115 RTC::ReturnCode_t CameraImageSaver::onExecute(RTC::UniqueId ec_id)
00116 {
00117   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00118   if (m_imageIn.isNew()){
00119     m_imageIn.read();
00120 
00121     IplImage* cvImage;
00122     switch (m_image.data.image.format){
00123     case Img::CF_RGB:
00124       cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00125                                      m_image.data.image.height),
00126                               IPL_DEPTH_8U, 3);
00127       break;
00128     case Img::CF_GRAY:
00129       cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00130                                      m_image.data.image.height),
00131                               IPL_DEPTH_8U, 1);
00132       break;
00133     default:
00134       std::cerr << "unsupported color format(" 
00135                 << m_image.data.image.format << ")" << std::endl;
00136       return RTC::RTC_ERROR;
00137     }
00138     switch(m_image.data.image.format){
00139     case Img::CF_RGB:
00140       {
00141         // RGB -> BGR
00142         char *dst = cvImage->imageData;
00143         for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
00144           dst[i  ] = m_image.data.image.raw_data[i+2]; 
00145           dst[i+1] = m_image.data.image.raw_data[i+1]; 
00146           dst[i+2] = m_image.data.image.raw_data[i  ]; 
00147         }
00148         break;
00149       }
00150     case Img::CF_GRAY:
00151       memcpy(cvImage->imageData, 
00152              m_image.data.image.raw_data.get_buffer(),
00153              m_image.data.image.raw_data.length());
00154       break;
00155     default:
00156       break;
00157     }
00158 
00159     char fname[256];
00160     sprintf(fname, "%s%04d.png", m_basename.c_str(), m_count++);
00161     cvSaveImage(fname, cvImage);
00162     
00163     cvReleaseImage(&cvImage);
00164   }
00165   
00166   return RTC::RTC_OK;
00167 }
00168 
00169 /*
00170 RTC::ReturnCode_t CameraImageSaver::onAborting(RTC::UniqueId ec_id)
00171 {
00172   return RTC::RTC_OK;
00173 }
00174 */
00175 
00176 /*
00177 RTC::ReturnCode_t CameraImageSaver::onError(RTC::UniqueId ec_id)
00178 {
00179   return RTC::RTC_OK;
00180 }
00181 */
00182 
00183 /*
00184 RTC::ReturnCode_t CameraImageSaver::onReset(RTC::UniqueId ec_id)
00185 {
00186   return RTC::RTC_OK;
00187 }
00188 */
00189 
00190 /*
00191 RTC::ReturnCode_t CameraImageSaver::onStateUpdate(RTC::UniqueId ec_id)
00192 {
00193   return RTC::RTC_OK;
00194 }
00195 */
00196 
00197 /*
00198 RTC::ReturnCode_t CameraImageSaver::onRateChanged(RTC::UniqueId ec_id)
00199 {
00200   return RTC::RTC_OK;
00201 }
00202 */
00203 
00204 
00205 
00206 extern "C"
00207 {
00208 
00209   void CameraImageSaverInit(RTC::Manager* manager)
00210   {
00211     RTC::Properties profile(spec);
00212     manager->registerFactory(profile,
00213                              RTC::Create<CameraImageSaver>,
00214                              RTC::Delete<CameraImageSaver>);
00215   }
00216 
00217 };
00218 
00219 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed Sep 6 2017 02:35:54