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


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Sat Dec 17 2022 03:52:20