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;
00135           unsigned char *dst = m_image.data.image.raw_data.get_buffer();
00136           for (int i=0; i<image->height; i++){
00137               for (int j=0; j<image->width; j++){
00138                   src = image->imageData + image->widthStep*i + j*3;
00139                   dst[2] = src[0];
00140                   dst[1] = src[1];
00141                   dst[0] = src[2];
00142                   dst += 3;
00143               }
00144           }
00145           break;
00146       }
00147   case 1:
00148       m_image.data.image.format = Img::CF_GRAY;
00149       memcpy(m_image.data.image.raw_data.get_buffer(),
00150              image->imageData,
00151              m_image.data.image.raw_data.length());
00152       break;
00153   default:
00154       break;
00155   }
00156   
00157   cvReleaseImage (&image);
00158   
00159   m_imageOut.write();
00160   
00161   return RTC::RTC_OK;
00162 }
00163 
00164 /*
00165 RTC::ReturnCode_t CameraImageLoader::onAborting(RTC::UniqueId ec_id)
00166 {
00167   return RTC::RTC_OK;
00168 }
00169 */
00170 
00171 /*
00172 RTC::ReturnCode_t CameraImageLoader::onError(RTC::UniqueId ec_id)
00173 {
00174   return RTC::RTC_OK;
00175 }
00176 */
00177 
00178 /*
00179 RTC::ReturnCode_t CameraImageLoader::onReset(RTC::UniqueId ec_id)
00180 {
00181   return RTC::RTC_OK;
00182 }
00183 */
00184 
00185 /*
00186 RTC::ReturnCode_t CameraImageLoader::onStateUpdate(RTC::UniqueId ec_id)
00187 {
00188   return RTC::RTC_OK;
00189 }
00190 */
00191 
00192 /*
00193 RTC::ReturnCode_t CameraImageLoader::onRateChanged(RTC::UniqueId ec_id)
00194 {
00195   return RTC::RTC_OK;
00196 }
00197 */
00198 
00199 
00200 
00201 extern "C"
00202 {
00203 
00204   void CameraImageLoaderInit(RTC::Manager* manager)
00205   {
00206     RTC::Properties profile(spec);
00207     manager->registerFactory(profile,
00208                              RTC::Create<CameraImageLoader>,
00209                              RTC::Delete<CameraImageLoader>);
00210   }
00211 
00212 };
00213 
00214 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:17