CameraImageLoader.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <cv.h>
00011 #include <highgui.h>
00012 #include "CameraImageLoader.h"
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* spec[] =
00017   {
00018     "implementation_id", "CameraImageLoader",
00019     "type_name",         "CameraImageLoader",
00020     "description",       "camera image loader",
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 
00030     ""
00031   };
00032 // </rtc-template>
00033 
00034 CameraImageLoader::CameraImageLoader(RTC::Manager* manager)
00035   : RTC::DataFlowComponentBase(manager),
00036     // <rtc-template block="initializer">
00037     m_imageOut("image", m_image),
00038     // </rtc-template>
00039     dummy(0)
00040 {
00041 }
00042 
00043 CameraImageLoader::~CameraImageLoader()
00044 {
00045 }
00046 
00047 
00048 
00049 RTC::ReturnCode_t CameraImageLoader::onInitialize()
00050 {
00051   //std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00052   // <rtc-template block="bind_config">
00053   // Bind variables and configuration variable
00054   
00055   // </rtc-template>
00056 
00057   // Registration: InPort/OutPort/Service
00058   // <rtc-template block="registration">
00059   // Set InPort buffers
00060 
00061   // Set OutPort buffer
00062     addOutPort("image", m_imageOut);
00063   
00064   // Set service provider to Ports
00065   
00066   // Set service consumers to Ports
00067   
00068   // Set CORBA Service Ports
00069   
00070   // </rtc-template>
00071 
00072   RTC::Properties& prop = getProperties();
00073 
00074   return RTC::RTC_OK;
00075 }
00076 
00077 
00078 
00079 /*
00080 RTC::ReturnCode_t CameraImageLoader::onFinalize()
00081 {
00082   return RTC::RTC_OK;
00083 }
00084 */
00085 
00086 /*
00087 RTC::ReturnCode_t CameraImageLoader::onStartup(RTC::UniqueId ec_id)
00088 {
00089   return RTC::RTC_OK;
00090 }
00091 */
00092 
00093 /*
00094 RTC::ReturnCode_t CameraImageLoader::onShutdown(RTC::UniqueId ec_id)
00095 {
00096   return RTC::RTC_OK;
00097 }
00098 */
00099 
00100 RTC::ReturnCode_t CameraImageLoader::onActivated(RTC::UniqueId ec_id)
00101 {
00102   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00103   return RTC::RTC_OK;
00104 }
00105 
00106 RTC::ReturnCode_t CameraImageLoader::onDeactivated(RTC::UniqueId ec_id)
00107 {
00108   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00109   return RTC::RTC_OK;
00110 }
00111 
00112 RTC::ReturnCode_t CameraImageLoader::onExecute(RTC::UniqueId ec_id)
00113 {
00114   //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00115   std::cerr << "image filename: " << std::flush;
00116   std::string filename;
00117   std::cin >> filename;
00118 
00119   IplImage *image = cvLoadImage(filename.c_str(), CV_LOAD_IMAGE_COLOR);
00120   if (!image) {
00121       std::cerr << m_profile.instance_name << ": failed to load("
00122                 << filename << ")" << std::endl;
00123       return RTC::RTC_OK;
00124   }
00125 
00126   m_image.data.image.width = image->width;
00127   m_image.data.image.height = image->height;
00128   m_image.data.image.raw_data.length(image->imageSize);
00129   switch(image->nChannels){
00130   case 3:
00131       m_image.data.image.format = Img::CF_RGB;
00132       {
00133           // BGR -> RGB
00134           char *src = image->imageData;
00135           for (unsigned int i=0; i<m_image.data.image.raw_data.length(); i+=3){
00136               m_image.data.image.raw_data[i+2] = src[i  ]; 
00137               m_image.data.image.raw_data[i+1] = src[i+1]; 
00138               m_image.data.image.raw_data[i  ] = src[i+2]; 
00139           }
00140           break;
00141       }
00142   case 1:
00143       m_image.data.image.format = Img::CF_GRAY;
00144       memcpy(m_image.data.image.raw_data.get_buffer(),
00145              image->imageData,
00146              m_image.data.image.raw_data.length());
00147       break;
00148   default:
00149       break;
00150   }
00151   
00152   cvReleaseImage (&image);
00153   
00154   m_imageOut.write();
00155   
00156   return RTC::RTC_OK;
00157 }
00158 
00159 /*
00160 RTC::ReturnCode_t CameraImageLoader::onAborting(RTC::UniqueId ec_id)
00161 {
00162   return RTC::RTC_OK;
00163 }
00164 */
00165 
00166 /*
00167 RTC::ReturnCode_t CameraImageLoader::onError(RTC::UniqueId ec_id)
00168 {
00169   return RTC::RTC_OK;
00170 }
00171 */
00172 
00173 /*
00174 RTC::ReturnCode_t CameraImageLoader::onReset(RTC::UniqueId ec_id)
00175 {
00176   return RTC::RTC_OK;
00177 }
00178 */
00179 
00180 /*
00181 RTC::ReturnCode_t CameraImageLoader::onStateUpdate(RTC::UniqueId ec_id)
00182 {
00183   return RTC::RTC_OK;
00184 }
00185 */
00186 
00187 /*
00188 RTC::ReturnCode_t CameraImageLoader::onRateChanged(RTC::UniqueId ec_id)
00189 {
00190   return RTC::RTC_OK;
00191 }
00192 */
00193 
00194 
00195 
00196 extern "C"
00197 {
00198 
00199   void CameraImageLoaderInit(RTC::Manager* manager)
00200   {
00201     RTC::Properties profile(spec);
00202     manager->registerFactory(profile,
00203                              RTC::Create<CameraImageLoader>,
00204                              RTC::Delete<CameraImageLoader>);
00205   }
00206 
00207 };
00208 
00209 


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