JpegDecoder.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <opencv2/highgui/highgui.hpp>
00011 #include <opencv2/core/core.hpp>
00012 #include "JpegDecoder.h"
00013 
00014 // Module specification
00015 // <rtc-template block="module_spec">
00016 static const char* jpegdecoder_spec[] =
00017   {
00018     "implementation_id", "JpegDecoder",
00019     "type_name",         "JpegDecoder",
00020     "description",       "null component",
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 JpegDecoder::JpegDecoder(RTC::Manager* manager)
00035   : RTC::DataFlowComponentBase(manager),
00036     // <rtc-template block="initializer">
00037     m_encodedIn("encoded",  m_encoded),
00038     m_decodedOut("decoded", m_decoded),
00039     // </rtc-template>
00040     dummy(0)
00041 {
00042 }
00043 
00044 JpegDecoder::~JpegDecoder()
00045 {
00046 }
00047 
00048 
00049 
00050 RTC::ReturnCode_t JpegDecoder::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   
00056   // </rtc-template>
00057 
00058   // Registration: InPort/OutPort/Service
00059   // <rtc-template block="registration">
00060   // Set InPort buffers
00061   addInPort("encodedIn", m_encodedIn);
00062 
00063   // Set OutPort buffer
00064   addOutPort("decodedOut", m_decodedOut);
00065   
00066   // Set service provider to Ports
00067   
00068   // Set service consumers to Ports
00069   
00070   // Set CORBA Service Ports
00071   
00072   // </rtc-template>
00073 
00074   //RTC::Properties& prop = getProperties();
00075 
00076   return RTC::RTC_OK;
00077 }
00078 
00079 
00080 
00081 /*
00082 RTC::ReturnCode_t JpegDecoder::onFinalize()
00083 {
00084   return RTC::RTC_OK;
00085 }
00086 */
00087 
00088 /*
00089 RTC::ReturnCode_t JpegDecoder::onStartup(RTC::UniqueId ec_id)
00090 {
00091   return RTC::RTC_OK;
00092 }
00093 */
00094 
00095 /*
00096 RTC::ReturnCode_t JpegDecoder::onShutdown(RTC::UniqueId ec_id)
00097 {
00098   return RTC::RTC_OK;
00099 }
00100 */
00101 
00102 RTC::ReturnCode_t JpegDecoder::onActivated(RTC::UniqueId ec_id)
00103 {
00104   std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00105   return RTC::RTC_OK;
00106 }
00107 
00108 RTC::ReturnCode_t JpegDecoder::onDeactivated(RTC::UniqueId ec_id)
00109 {
00110   std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00111   return RTC::RTC_OK;
00112 }
00113 
00114 RTC::ReturnCode_t JpegDecoder::onExecute(RTC::UniqueId ec_id)
00115 {
00116     //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00117   if (m_encodedIn.isNew()){
00118       m_encodedIn.read();
00119 
00120       if (m_encoded.error_code != 0) {
00121           return RTC::RTC_OK;
00122       }
00123 
00124       Img::ImageData& idat = m_encoded.data.image;
00125 
00126       std::vector<uchar>buf;
00127       int len = idat.raw_data.length();
00128       buf.resize(idat.raw_data.length());
00129       memcpy(&buf[0], (void *)&(idat.raw_data[0]), len);
00130 
00131       int flags = (idat.format == Img::CF_GRAY_JPEG) ? CV_LOAD_IMAGE_GRAYSCALE : CV_LOAD_IMAGE_COLOR;
00132       cv::Mat image = cv::imdecode(cv::Mat(buf), flags);
00133 
00134       if (idat.format == Img::CF_GRAY_JPEG){
00135         m_decoded.data.image.format = Img::CF_GRAY;
00136         m_decoded.data.image.raw_data.length(image.cols*image.rows);
00137       }else{
00138         m_decoded.data.image.format = Img::CF_RGB;
00139         m_decoded.data.image.raw_data.length(image.cols*image.rows*3);
00140       }
00141       m_decoded.data.image.width = image.cols;
00142       m_decoded.data.image.height = image.rows;
00143       unsigned char *src = image.data;
00144       unsigned char *dst = m_decoded.data.image.raw_data.get_buffer();
00145       if (idat.format == Img::CF_GRAY_JPEG){
00146         memcpy(dst, src, m_decoded.data.image.raw_data.length());
00147       }else{
00148         for (int i=0; i<image.rows; i++){
00149           for (int j=0; j<image.cols; j++){
00150             // BGR -> RGB
00151             *dst++ = src[2];
00152             *dst++ = src[1];
00153             *dst++ = src[0];
00154             src+=3;
00155           }
00156         }
00157       }
00158 
00159       m_decodedOut.write();
00160   }
00161   return RTC::RTC_OK;
00162 }
00163 
00164 /*
00165 RTC::ReturnCode_t JpegDecoder::onAborting(RTC::UniqueId ec_id)
00166 {
00167   return RTC::RTC_OK;
00168 }
00169 */
00170 
00171 /*
00172 RTC::ReturnCode_t JpegDecoder::onError(RTC::UniqueId ec_id)
00173 {
00174   return RTC::RTC_OK;
00175 }
00176 */
00177 
00178 /*
00179 RTC::ReturnCode_t JpegDecoder::onReset(RTC::UniqueId ec_id)
00180 {
00181   return RTC::RTC_OK;
00182 }
00183 */
00184 
00185 /*
00186 RTC::ReturnCode_t JpegDecoder::onStateUpdate(RTC::UniqueId ec_id)
00187 {
00188   return RTC::RTC_OK;
00189 }
00190 */
00191 
00192 /*
00193 RTC::ReturnCode_t JpegDecoder::onRateChanged(RTC::UniqueId ec_id)
00194 {
00195   return RTC::RTC_OK;
00196 }
00197 */
00198 
00199 
00200 
00201 extern "C"
00202 {
00203 
00204   void JpegDecoderInit(RTC::Manager* manager)
00205   {
00206     RTC::Properties profile(jpegdecoder_spec);
00207     manager->registerFactory(profile,
00208                              RTC::Create<JpegDecoder>,
00209                              RTC::Delete<JpegDecoder>);
00210   }
00211 
00212 };
00213 
00214 


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