JpegEncoder.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <opencv2/highgui/highgui.hpp>
11 #include <opencv2/imgproc/imgproc.hpp>
12 #include <opencv2/core/core.hpp>
13 #include "JpegEncoder.h"
14 
15 // Module specification
16 // <rtc-template block="module_spec">
17 static const char* jpegdecoder_spec[] =
18  {
19  "implementation_id", "JpegEncoder",
20  "type_name", "JpegEncoder",
21  "description", "null component",
22  "version", HRPSYS_PACKAGE_VERSION,
23  "vendor", "AIST",
24  "category", "example",
25  "activity_type", "DataFlowComponent",
26  "max_instance", "10",
27  "language", "C++",
28  "lang_type", "compile",
29  // Configuration variables
30  "conf.default.quality", "95",
31 
32  ""
33  };
34 // </rtc-template>
35 
37  : RTC::DataFlowComponentBase(manager),
38  // <rtc-template block="initializer">
39  m_decodedIn("decoded", m_decoded),
40  m_encodedOut("encoded", m_encoded),
41  // </rtc-template>
42  m_quality(95),
43  dummy(0)
44 {
45 }
46 
48 {
49 }
50 
51 
52 
53 RTC::ReturnCode_t JpegEncoder::onInitialize()
54 {
55  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
56  // <rtc-template block="bind_config">
57  // Bind variables and configuration variable
58  bindParameter("quality", m_quality, "95");
59 
60  // </rtc-template>
61 
62  // Registration: InPort/OutPort/Service
63  // <rtc-template block="registration">
64  // Set InPort buffers
65  addInPort("decodedIn", m_decodedIn);
66 
67  // Set OutPort buffer
68  addOutPort("encodedOut", m_encodedOut);
69 
70  // Set service provider to Ports
71 
72  // Set service consumers to Ports
73 
74  // Set CORBA Service Ports
75 
76  // </rtc-template>
77 
78  //RTC::Properties& prop = getProperties();
79 
80  return RTC::RTC_OK;
81 }
82 
83 
84 
85 /*
86 RTC::ReturnCode_t JpegEncoder::onFinalize()
87 {
88  return RTC::RTC_OK;
89 }
90 */
91 
92 /*
93 RTC::ReturnCode_t JpegEncoder::onStartup(RTC::UniqueId ec_id)
94 {
95  return RTC::RTC_OK;
96 }
97 */
98 
99 /*
100 RTC::ReturnCode_t JpegEncoder::onShutdown(RTC::UniqueId ec_id)
101 {
102  return RTC::RTC_OK;
103 }
104 */
105 
106 RTC::ReturnCode_t JpegEncoder::onActivated(RTC::UniqueId ec_id)
107 {
108  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
109  return RTC::RTC_OK;
110 }
111 
113 {
114  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
115  return RTC::RTC_OK;
116 }
117 
118 RTC::ReturnCode_t JpegEncoder::onExecute(RTC::UniqueId ec_id)
119 {
120  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
121  if (m_decodedIn.isNew()){
122  m_decodedIn.read();
123 
124  Img::ImageData& idat = m_decoded.data.image;
125  std::vector<uchar>buf;
126  std::vector<int> param = std::vector<int>(2);
127  param[0] = CV_IMWRITE_JPEG_QUALITY;
128  param[1] = m_quality;
129 
130  switch(idat.format){
131  case Img::CF_RGB:
132  {
133  // RGB -> BGR
134  uchar r,g,b, *raw=idat.raw_data.get_buffer();
135  for (unsigned int i=0; i<idat.raw_data.length(); i+=3, raw+=3){
136  r = raw[0]; g = raw[1]; b = raw[2];
137  raw[0] = b; raw[1] = g; raw[2] = r;
138  }
139 
140  cv::Mat src(idat.height, idat.width, CV_8UC3,
141  idat.raw_data.get_buffer());
142  cv::imencode(".jpg", src, buf, param);
143  m_encoded.data.image.format = Img::CF_RGB_JPEG;
144  }
145  break;
146  case Img::CF_GRAY:
147  {
148  cv::Mat src(idat.height, idat.width, CV_8U,
149  idat.raw_data.get_buffer());
150  cv::imencode(".jpg", src, buf, param);
151  m_encoded.data.image.format = Img::CF_GRAY_JPEG;
152  }
153  break;
154  }
155  m_encoded.data.image.raw_data.length(buf.size());
156  unsigned char *dst = m_encoded.data.image.raw_data.get_buffer();
157  memcpy(dst, &buf[0], buf.size());
158 
159 #if 0
160  std::cout << "JpegEncoder:" << idat.raw_data.length() << "->"
161  << m_encoded.data.image.raw_data.length() << std::endl;
162 #endif
164  }
165  return RTC::RTC_OK;
166 }
167 
168 /*
169 RTC::ReturnCode_t JpegEncoder::onAborting(RTC::UniqueId ec_id)
170 {
171  return RTC::RTC_OK;
172 }
173 */
174 
175 /*
176 RTC::ReturnCode_t JpegEncoder::onError(RTC::UniqueId ec_id)
177 {
178  return RTC::RTC_OK;
179 }
180 */
181 
182 /*
183 RTC::ReturnCode_t JpegEncoder::onReset(RTC::UniqueId ec_id)
184 {
185  return RTC::RTC_OK;
186 }
187 */
188 
189 /*
190 RTC::ReturnCode_t JpegEncoder::onStateUpdate(RTC::UniqueId ec_id)
191 {
192  return RTC::RTC_OK;
193 }
194 */
195 
196 /*
197 RTC::ReturnCode_t JpegEncoder::onRateChanged(RTC::UniqueId ec_id)
198 {
199  return RTC::RTC_OK;
200 }
201 */
202 
203 
204 
205 extern "C"
206 {
207 
209  {
211  manager->registerFactory(profile,
212  RTC::Create<JpegEncoder>,
213  RTC::Delete<JpegEncoder>);
214  }
215 
216 };
217 
218 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
Img::TimedCameraImage m_decoded
Definition: JpegEncoder.h:106
virtual RTC::ReturnCode_t onInitialize()
Definition: JpegEncoder.cpp:53
jpeg encoder component
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
png_uint_32 i
InPort< Img::TimedCameraImage > m_decodedIn
Definition: JpegEncoder.h:110
bool addOutPort(const char *name, OutPortBase &outport)
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
OutPort< Img::TimedCameraImage > m_encodedOut
Definition: JpegEncoder.h:118
void JpegEncoderInit(RTC::Manager *manager)
Img::TimedCameraImage m_encoded
Definition: JpegEncoder.h:114
png_bytep buf
virtual bool isNew()
virtual bool write(DataType &value)
virtual ~JpegEncoder()
Destructor.
Definition: JpegEncoder.cpp:47
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
bool addInPort(const char *name, InPortBase &inport)
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
static const char * jpegdecoder_spec[]
Definition: JpegEncoder.cpp:17
JpegEncoder(RTC::Manager *manager)
Constructor.
Definition: JpegEncoder.cpp:36


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:50