Go to the documentation of this file.00001
00010 #include <opencv2/highgui/highgui.hpp>
00011 #include <opencv2/core/core.hpp>
00012 #include "JpegDecoder.h"
00013
00014
00015
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
00029
00030 ""
00031 };
00032
00033
00034 JpegDecoder::JpegDecoder(RTC::Manager* manager)
00035 : RTC::DataFlowComponentBase(manager),
00036
00037 m_encodedIn("encoded", m_encoded),
00038 m_decodedOut("decoded", m_decoded),
00039
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
00054
00055
00056
00057
00058
00059
00060
00061 addInPort("encodedIn", m_encodedIn);
00062
00063
00064 addOutPort("decodedOut", m_decodedOut);
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 return RTC::RTC_OK;
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
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
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
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
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
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