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


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