00001
00010 #include <opencv2/highgui/highgui.hpp>
00011 #include <opencv2/imgproc/imgproc.hpp>
00012 #include <opencv2/core/core.hpp>
00013 #include "JpegEncoder.h"
00014
00015
00016
00017 static const char* jpegdecoder_spec[] =
00018 {
00019 "implementation_id", "JpegEncoder",
00020 "type_name", "JpegEncoder",
00021 "description", "null component",
00022 "version", HRPSYS_PACKAGE_VERSION,
00023 "vendor", "AIST",
00024 "category", "example",
00025 "activity_type", "DataFlowComponent",
00026 "max_instance", "10",
00027 "language", "C++",
00028 "lang_type", "compile",
00029
00030 "conf.default.quality", "95",
00031
00032 ""
00033 };
00034
00035
00036 JpegEncoder::JpegEncoder(RTC::Manager* manager)
00037 : RTC::DataFlowComponentBase(manager),
00038
00039 m_decodedIn("decoded", m_decoded),
00040 m_encodedOut("encoded", m_encoded),
00041
00042 m_quality(95),
00043 dummy(0)
00044 {
00045 }
00046
00047 JpegEncoder::~JpegEncoder()
00048 {
00049 }
00050
00051
00052
00053 RTC::ReturnCode_t JpegEncoder::onInitialize()
00054 {
00055 std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00056
00057
00058 bindParameter("quality", m_quality, "95");
00059
00060
00061
00062
00063
00064
00065 addInPort("decodedIn", m_decodedIn);
00066
00067
00068 addOutPort("encodedOut", m_encodedOut);
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 return RTC::RTC_OK;
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 RTC::ReturnCode_t JpegEncoder::onActivated(RTC::UniqueId ec_id)
00107 {
00108 std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00109 return RTC::RTC_OK;
00110 }
00111
00112 RTC::ReturnCode_t JpegEncoder::onDeactivated(RTC::UniqueId ec_id)
00113 {
00114 std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00115 return RTC::RTC_OK;
00116 }
00117
00118 RTC::ReturnCode_t JpegEncoder::onExecute(RTC::UniqueId ec_id)
00119 {
00120
00121 if (m_decodedIn.isNew()){
00122 m_decodedIn.read();
00123
00124 Img::ImageData& idat = m_decoded.data.image;
00125 std::vector<uchar>buf;
00126 std::vector<int> param = std::vector<int>(2);
00127 param[0] = CV_IMWRITE_JPEG_QUALITY;
00128 param[1] = m_quality;
00129
00130 switch(idat.format){
00131 case Img::CF_RGB:
00132 {
00133
00134 uchar r,g,b, *raw=idat.raw_data.get_buffer();
00135 for (unsigned int i=0; i<idat.raw_data.length(); i+=3, raw+=3){
00136 r = raw[0]; g = raw[1]; b = raw[2];
00137 raw[0] = b; raw[1] = g; raw[2] = r;
00138 }
00139
00140 cv::Mat src(idat.height, idat.width, CV_8UC3,
00141 idat.raw_data.get_buffer());
00142 cv::imencode(".jpg", src, buf, param);
00143 m_encoded.data.image.format = Img::CF_RGB_JPEG;
00144 }
00145 break;
00146 case Img::CF_GRAY:
00147 {
00148 cv::Mat src(idat.height, idat.width, CV_8U,
00149 idat.raw_data.get_buffer());
00150 cv::imencode(".jpg", src, buf, param);
00151 m_encoded.data.image.format = Img::CF_GRAY_JPEG;
00152 }
00153 break;
00154 }
00155 m_encoded.data.image.raw_data.length(buf.size());
00156 unsigned char *dst = m_encoded.data.image.raw_data.get_buffer();
00157 memcpy(dst, &buf[0], buf.size());
00158
00159 #if 0
00160 std::cout << "JpegEncoder:" << idat.raw_data.length() << "->"
00161 << m_encoded.data.image.raw_data.length() << std::endl;
00162 #endif
00163 m_encodedOut.write();
00164 }
00165 return RTC::RTC_OK;
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
00202
00203
00204
00205 extern "C"
00206 {
00207
00208 void JpegEncoderInit(RTC::Manager* manager)
00209 {
00210 RTC::Properties profile(jpegdecoder_spec);
00211 manager->registerFactory(profile,
00212 RTC::Create<JpegEncoder>,
00213 RTC::Delete<JpegEncoder>);
00214 }
00215
00216 };
00217
00218