23 #include "image_thread.h" 25 #include "utils/setup.h" 27 #include <core/threading/mutex_locker.h> 28 #include <fvutils/color/bayer.h> 29 #include <fvutils/color/colorspaces.h> 30 #include <fvutils/color/rgbyuv.h> 31 #include <fvutils/color/yuv.h> 32 #include <fvutils/color/yuvrgb.h> 33 #include <fvutils/ipc/shm_image.h> 38 using namespace firevision;
50 :
Thread(
"OpenNiImageThread",
Thread::OPMODE_WAITFORWAKEUP),
65 cfg_copy_mode_ = CONVERT_YUV;
67 image_gen_ =
new xn::ImageGenerator();
68 #if __cplusplus >= 201103L 69 std::unique_ptr<xn::ImageGenerator> imagegen_uniqueptr(image_gen_);
71 std::auto_ptr<xn::ImageGenerator> imagegen_uniqueptr(image_gen_);
76 fawkes::openni::find_or_create_node(
openni, XN_NODE_TYPE_IMAGE, image_gen_);
78 fawkes::openni::setup_map_generator(*image_gen_,
config);
80 fawkes::openni::get_usb_info(*image_gen_, usb_vendor_, usb_product_);
82 if ((usb_vendor_ == 0x045e) && (usb_product_ == 0x02ae)) {
87 if (image_gen_->SetIntProperty(
"InputFormat", 6) != XN_STATUS_OK) {
88 throw Exception(
"Failed to set uncompressed bayer input format");
90 if (image_gen_->SetPixelFormat(XN_PIXEL_FORMAT_GRAYSCALE_8_BIT) != XN_STATUS_OK) {
91 throw Exception(
"Failed to set pixel format");
100 cfg_copy_mode_ = DEBAYER_BILINEAR;
102 std::string debayering =
config->
get_string(
"/plugins/openni-image/debayering");
103 if (debayering ==
"bilinear") {
104 cfg_copy_mode_ = DEBAYER_BILINEAR;
105 }
else if (debayering ==
"nearest_neighbor") {
106 cfg_copy_mode_ = DEBAYER_NEAREST_NEIGHBOR;
109 "Unknown de-bayering mode '%s', using bilinear instead.",
117 if (image_gen_->SetIntProperty(
"InputFormat", 5) != XN_STATUS_OK) {
118 throw Exception(
"Failed to set uncompressed bayer input format");
120 if (image_gen_->SetPixelFormat(XN_PIXEL_FORMAT_YUV422) != XN_STATUS_OK) {
121 throw Exception(
"Failed to set pixel format");
123 cfg_copy_mode_ = CONVERT_YUV;
126 image_md_ =
new xn::ImageMetaData();
128 image_gen_->GetMetaData(*image_md_);
130 image_width_ = image_md_->XRes();
131 image_height_ = image_md_->YRes();
160 image_gen_->StartGenerating();
165 image_gen_->WaitAndUpdateData();
168 *capture_start_ -= (
long int)image_gen_->GetTimestamp();
170 imagegen_uniqueptr.release();
180 delete image_buf_yuv_;
181 delete image_buf_rgb_;
182 delete capture_start_;
189 bool is_image_new = image_gen_->IsDataNew();
190 image_gen_->GetMetaData(*image_md_);
191 const XnUInt8 *
const image_data = image_md_->Data();
192 fawkes::Time ts = *capture_start_ + (
long int)image_gen_->GetTimestamp();
195 if (is_image_new && (image_buf_yuv_->
num_attached() > 1)) {
197 if (cfg_copy_mode_ == DEBAYER_BILINEAR) {
198 bayerGRBG_to_yuv422planar_bilinear(image_data,
202 }
else if (cfg_copy_mode_ == CONVERT_YUV) {
203 yuv422packed_to_yuv422planar(image_data,
207 }
else if (cfg_copy_mode_ == CONVERT_RGB) {
208 rgb_to_yuv422planar_plainc(image_data, image_buf_yuv_->
buffer(), image_width_, image_height_);
209 }
else if (cfg_copy_mode_ == DEBAYER_NEAREST_NEIGHBOR) {
210 bayerGRBG_to_yuv422planar_nearest_neighbour(image_data,
219 if (is_image_new && (image_buf_rgb_->
num_attached() > 1)) {
221 if (cfg_copy_mode_ == DEBAYER_BILINEAR) {
222 bayerGRBG_to_rgb_bilinear(image_data, image_buf_rgb_->
buffer(), image_width_, image_height_);
223 }
else if (cfg_copy_mode_ == CONVERT_YUV) {
224 yuv422packed_to_rgb_plainc(image_data, image_buf_rgb_->
buffer(), image_width_, image_height_);
225 }
else if (cfg_copy_mode_ == CONVERT_RGB) {
226 memcpy(image_buf_rgb_->
buffer(),
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,
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.