CameraImageViewer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include "CameraImageViewer.h"
00011 
00012 // Module specification
00013 // <rtc-template block="module_spec">
00014 static const char* cameraimageviewercomponent_spec[] =
00015 {
00016     "implementation_id", "CameraImageViewer",
00017     "type_name",         "CameraImageViewer",
00018     "description",       "camera image viewer component",
00019     "version",           HRPSYS_PACKAGE_VERSION,
00020     "vendor",            "AIST",
00021     "category",          "example",
00022     "activity_type",     "DataFlowComponent",
00023     "max_instance",      "10",
00024     "language",          "C++",
00025     "lang_type",         "compile",
00026     // Configuration variables
00027     "conf.default.depthBits", "11",
00028 
00029     ""
00030 };
00031 // </rtc-template>
00032 
00033 CameraImageViewer::CameraImageViewer(RTC::Manager* manager)
00034     : RTC::DataFlowComponentBase(manager),
00035       // <rtc-template block="initializer">
00036       m_imageIn("imageIn", m_image),
00037       m_imageOldIn("imageOldIn", m_imageOld),
00038       // </rtc-template>
00039       m_cvImage(NULL),
00040       dummy(0)
00041 {
00042 }
00043 
00044 CameraImageViewer::~CameraImageViewer()
00045 {
00046 }
00047 
00048 
00049 
00050 RTC::ReturnCode_t CameraImageViewer::onInitialize()
00051 {
00052     std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00053     // <rtc-template block="bind_config">
00054     // Bind variables and configuration variable
00055     bindParameter("depthBits", m_depthBits, "11");
00056   
00057     // </rtc-template>
00058 
00059     // Registration: InPort/OutPort/Service
00060     // <rtc-template block="registration">
00061     // Set InPort buffers
00062     addInPort("imageIn", m_imageIn);
00063     addInPort("imageOldIn", m_imageOldIn);
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 CameraImageViewer::onFinalize()
00084   {
00085   return RTC::RTC_OK;
00086   }
00087 */
00088 
00089 /*
00090   RTC::ReturnCode_t CameraImageViewer::onStartup(RTC::UniqueId ec_id)
00091   {
00092   return RTC::RTC_OK;
00093   }
00094 */
00095 
00096 /*
00097   RTC::ReturnCode_t CameraImageViewer::onShutdown(RTC::UniqueId ec_id)
00098   {
00099   return RTC::RTC_OK;
00100   }
00101 */
00102 
00103 RTC::ReturnCode_t CameraImageViewer::onActivated(RTC::UniqueId ec_id)
00104 {
00105     std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00106     cvNamedWindow("Image",CV_WINDOW_AUTOSIZE);
00107     return RTC::RTC_OK;
00108 }
00109 
00110 RTC::ReturnCode_t CameraImageViewer::onDeactivated(RTC::UniqueId ec_id)
00111 {
00112     std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00113     if (m_cvImage) {
00114         cvReleaseImage(&m_cvImage);
00115         m_cvImage = NULL;
00116     }
00117     cvDestroyWindow("Image");
00118     return RTC::RTC_OK;
00119 }
00120 
00121 RTC::ReturnCode_t CameraImageViewer::onExecute(RTC::UniqueId ec_id)
00122 {
00123     //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")"  << std::endl;
00124 
00125     if (m_imageIn.isNew()){
00126         do {
00127             m_imageIn.read();
00128         }while(m_imageIn.isNew());
00129         if (m_cvImage && (m_image.data.image.width != m_cvImage->width 
00130                           || m_image.data.image.height != m_cvImage->height)){
00131             cvReleaseImage(&m_cvImage);
00132             m_cvImage = NULL;
00133         }
00134         if (!m_cvImage){
00135             switch (m_image.data.image.format){
00136             case Img::CF_RGB:
00137                 m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00138                                                  m_image.data.image.height),
00139                                           IPL_DEPTH_8U, 3);
00140                 break;
00141             case Img::CF_GRAY:
00142             case Img::CF_DEPTH:
00143                 m_cvImage = cvCreateImage(cvSize(m_image.data.image.width,
00144                                                  m_image.data.image.height),
00145                                           IPL_DEPTH_8U, 1);
00146                 break;
00147             default:
00148                 std::cerr << "unsupported color format(" 
00149                           << m_image.data.image.format << ")" << std::endl;
00150                 return RTC::RTC_ERROR;
00151             }
00152         }
00153         switch(m_image.data.image.format){
00154         case Img::CF_RGB:
00155         {
00156             // RGB -> BGR
00157             unsigned char *src = m_image.data.image.raw_data.get_buffer();
00158             char *dst;
00159             for (int i=0; i<m_cvImage->height; i++){
00160               for (int j=0; j<m_cvImage->width; j++){
00161                   dst = m_cvImage->imageData + m_cvImage->widthStep*i + j*3;
00162                   dst[0] = src[2];
00163                   dst[1] = src[1];
00164                   dst[2] = src[0];
00165                   src += 3;
00166               }
00167             }
00168             break;
00169         }
00170         case Img::CF_GRAY:
00171             memcpy(m_cvImage->imageData, 
00172                    m_image.data.image.raw_data.get_buffer(),
00173                    m_image.data.image.raw_data.length());
00174             break;
00175         case Img::CF_DEPTH:
00176             {
00177                 // depth -> gray scale
00178                 char *dst = m_cvImage->imageData;
00179                 Img::ImageData &id = m_image.data.image;
00180                 unsigned short *src = (unsigned short *)id.raw_data.get_buffer();
00181                 int shift = m_depthBits - 8;
00182                 for (unsigned int i=0; i<id.width*id.height; i++){
00183                     dst[i] = 0xff - src[i]>>shift;
00184                 }
00185             }
00186             break;
00187         default:
00188             break;
00189         }
00190     }
00191 
00192     if (m_imageOldIn.isNew()){
00193         do {
00194             m_imageOldIn.read();
00195         }while(m_imageOldIn.isNew());
00196         if (m_cvImage && (m_imageOld.width != m_cvImage->width 
00197                           || m_imageOld.height != m_cvImage->height)){
00198             cvReleaseImage(&m_cvImage);
00199             m_cvImage = NULL;
00200         }
00201         int bytes = m_imageOld.bpp/8;
00202         if (!bytes){
00203             bytes = m_imageOld.pixels.length()/(m_imageOld.width*m_imageOld.height);
00204         }
00205         if (!m_cvImage){
00206             m_cvImage = cvCreateImage(cvSize(m_imageOld.width,
00207                                              m_imageOld.height),
00208                                       IPL_DEPTH_8U, bytes);
00209         }
00210         switch(bytes){
00211         case 1:
00212             memcpy(m_cvImage->imageData, 
00213                    m_imageOld.pixels.get_buffer(),
00214                    m_imageOld.pixels.length());
00215             break;
00216         case 3:
00217             // RGB -> BGR
00218             char *dst = m_cvImage->imageData;
00219             for (unsigned int i=0; i<m_imageOld.pixels.length(); i+=3){
00220                 dst[i  ] = m_imageOld.pixels[i+2]; 
00221                 dst[i+1] = m_imageOld.pixels[i+1]; 
00222                 dst[i+2] = m_imageOld.pixels[i  ]; 
00223             }
00224             break;
00225         }
00226     }
00227     if (m_cvImage){
00228         cvShowImage("Image",m_cvImage);
00229         cvWaitKey(10);
00230     }
00231 
00232     return RTC::RTC_OK;
00233 }
00234 
00235 /*
00236   RTC::ReturnCode_t CameraImageViewer::onAborting(RTC::UniqueId ec_id)
00237   {
00238   return RTC::RTC_OK;
00239   }
00240 */
00241 
00242 /*
00243   RTC::ReturnCode_t CameraImageViewer::onError(RTC::UniqueId ec_id)
00244   {
00245   return RTC::RTC_OK;
00246   }
00247 */
00248 
00249 /*
00250   RTC::ReturnCode_t CameraImageViewer::onReset(RTC::UniqueId ec_id)
00251   {
00252   return RTC::RTC_OK;
00253   }
00254 */
00255 
00256 /*
00257   RTC::ReturnCode_t CameraImageViewer::onStateUpdate(RTC::UniqueId ec_id)
00258   {
00259   return RTC::RTC_OK;
00260   }
00261 */
00262 
00263 /*
00264   RTC::ReturnCode_t CameraImageViewer::onRateChanged(RTC::UniqueId ec_id)
00265   {
00266   return RTC::RTC_OK;
00267   }
00268 */
00269 
00270 
00271 
00272 extern "C"
00273 {
00274 
00275     void CameraImageViewerInit(RTC::Manager* manager)
00276     {
00277         RTC::Properties profile(cameraimageviewercomponent_spec);
00278         manager->registerFactory(profile,
00279                                  RTC::Create<CameraImageViewer>,
00280                                  RTC::Delete<CameraImageViewer>);
00281     }
00282 
00283 };
00284 
00285 


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