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


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