23 #include "image_thread.h" 24 #include "utils/setup.h" 26 #include <core/threading/mutex_locker.h> 27 #include <fvutils/ipc/shm_image.h> 28 #include <fvutils/color/colorspaces.h> 29 #include <fvutils/color/bayer.h> 30 #include <fvutils/color/yuv.h> 31 #include <fvutils/color/yuvrgb.h> 32 #include <fvutils/color/rgbyuv.h> 49 :
Thread(
"OpenNiImageThread",
Thread::OPMODE_WAITFORWAKEUP),
66 __cfg_copy_mode = CONVERT_YUV;
68 __image_gen =
new xn::ImageGenerator();
69 #if __cplusplus >= 201103L 70 std::unique_ptr<xn::ImageGenerator> imagegen_uniqueptr(__image_gen);
72 std::auto_ptr<xn::ImageGenerator> imagegen_uniqueptr(__image_gen);
77 fawkes::openni::find_or_create_node(
openni, XN_NODE_TYPE_IMAGE, __image_gen);
79 fawkes::openni::setup_map_generator(*__image_gen,
config);
81 fawkes::openni::get_usb_info(*__image_gen, __usb_vendor, __usb_product);
83 if ( (__usb_vendor == 0x045e) && (__usb_product == 0x02ae) ) {
88 if (__image_gen->SetIntProperty(
"InputFormat", 6) != XN_STATUS_OK) {
89 throw Exception(
"Failed to set uncompressed bayer input format");
91 if (__image_gen->SetPixelFormat(XN_PIXEL_FORMAT_GRAYSCALE_8_BIT) != XN_STATUS_OK)
93 throw Exception(
"Failed to set pixel format");
102 __cfg_copy_mode = DEBAYER_BILINEAR;
104 std::string debayering =
config->
get_string(
"/plugins/openni-image/debayering");
105 if (debayering ==
"bilinear") {
106 __cfg_copy_mode = DEBAYER_BILINEAR;
107 }
else if (debayering ==
"nearest_neighbor") {
108 __cfg_copy_mode = DEBAYER_NEAREST_NEIGHBOR;
118 if (__image_gen->SetIntProperty(
"InputFormat", 5) != XN_STATUS_OK) {
119 throw Exception(
"Failed to set uncompressed bayer input format");
121 if (__image_gen->SetPixelFormat(XN_PIXEL_FORMAT_YUV422) != XN_STATUS_OK) {
122 throw Exception(
"Failed to set pixel format");
124 __cfg_copy_mode = CONVERT_YUV;
127 __image_md =
new xn::ImageMetaData();
129 __image_gen->GetMetaData(*__image_md);
131 __image_width = __image_md->XRes();
132 __image_height = __image_md->YRes();
155 __image_md->XRes(), __image_md->YRes());
159 __image_md->XRes(), __image_md->YRes());
162 __image_gen->StartGenerating();
167 __image_gen->WaitAndUpdateData();
170 *__capture_start -= (
long int)__image_gen->GetTimestamp();
172 imagegen_uniqueptr.release();
183 delete __image_buf_yuv;
184 delete __image_buf_rgb;
192 bool is_image_new = __image_gen->IsDataNew();
193 __image_gen->GetMetaData(*__image_md);
194 const XnUInt8 *
const image_data = __image_md->Data();
195 fawkes::Time ts = *__capture_start + (
long int)__image_gen->GetTimestamp();
198 if (is_image_new && (__image_buf_yuv->
num_attached() > 1)) {
200 if (__cfg_copy_mode == DEBAYER_BILINEAR) {
201 bayerGRBG_to_yuv422planar_bilinear(image_data, __image_buf_yuv->
buffer(),
202 __image_width, __image_height);
203 }
else if (__cfg_copy_mode == CONVERT_YUV) {
204 yuv422packed_to_yuv422planar(image_data, __image_buf_yuv->
buffer(),
205 __image_width, __image_height);
206 }
else if (__cfg_copy_mode == CONVERT_RGB) {
207 rgb_to_yuv422planar_plainc(image_data, __image_buf_yuv->
buffer(),
208 __image_width, __image_height);
209 }
else if (__cfg_copy_mode == DEBAYER_NEAREST_NEIGHBOR) {
210 bayerGRBG_to_yuv422planar_nearest_neighbour(image_data,
211 __image_buf_yuv->
buffer(),
212 __image_width, __image_height);
215 __image_buf_yuv->
unlock();
218 if (is_image_new && (__image_buf_rgb->
num_attached() > 1)) {
220 if (__cfg_copy_mode == DEBAYER_BILINEAR) {
221 bayerGRBG_to_rgb_bilinear(image_data, __image_buf_rgb->
buffer(),
222 __image_width, __image_height);
223 }
else if (__cfg_copy_mode == CONVERT_YUV) {
224 yuv422packed_to_rgb_plainc(image_data, __image_buf_rgb->
buffer(),
225 __image_width, __image_height);
226 }
else if (__cfg_copy_mode == CONVERT_RGB) {
227 memcpy(__image_buf_rgb->
buffer(), image_data,
228 colorspace_buffer_size(RGB, __image_width, __image_height));
229 }
else if (__cfg_copy_mode == DEBAYER_NEAREST_NEIGHBOR) {
230 bayerGRBG_to_rgb_nearest_neighbour(image_data, __image_buf_rgb->
buffer(),
231 __image_width, __image_height);
234 __image_buf_rgb->
unlock();
LockPtr< xn::Context > openni
Central OpenNI context.
Time & stamp_systime()
Set this time to the current system time.
void lock_for_write()
Lock shared memory segment for writing.
Fawkes library namespace.
A class for handling time.
virtual void loop()
Code to execute in the thread.
Thread class encapsulation of pthreads.
Logger * logger
This is the Logger member used to access the logger.
unsigned char * buffer() const
Get image buffer.
Clock * clock
By means of this member access to the clock is given.
virtual void init()
Initialize the thread.
Thread aspect to use blocked timing.
virtual void finalize()
Finalize the thread.
Base class for exceptions in Fawkes.
Mutex * objmutex_ptr() const
Get object mutex.
Shared memory image buffer.
unsigned int num_attached() const
Get number of attached processes.
const char * name() const
Get name of thread.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
OpenNiImageThread()
Constructor.
virtual ~OpenNiImageThread()
Destructor.
Configuration * config
This is the Configuration member used to access the configuration.
void set_capture_time(fawkes::Time *time)
Set the capture time.
void unlock()
Unlock memory.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.