openni_image_rgb24.cpp
Go to the documentation of this file.
00001 #include <pcl/pcl_config.h>
00002 #ifdef HAVE_OPENNI
00003 
00004 #include <pcl/io/openni_camera/openni_image_rgb24.h>
00005 
00006 namespace openni_wrapper
00007 {
00008 ImageRGB24::ImageRGB24 (boost::shared_ptr<xn::ImageMetaData> image_meta_data) throw ()
00009 : Image (image_meta_data)
00010 {
00011 }
00012 
00013 ImageRGB24::~ImageRGB24 () throw ()
00014 {
00015 }
00016 
00017 void ImageRGB24::fillGrayscale (unsigned width, unsigned height, unsigned char* gray_buffer, unsigned gray_line_step) const
00018 {
00019   if (width > image_md_->XRes () || height > image_md_->YRes ())
00020     THROW_OPENNI_EXCEPTION ("Up-sampling not supported. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00021 
00022   if (image_md_->XRes () % width == 0 && image_md_->YRes () % height == 0)
00023   {
00024     unsigned src_step = image_md_->XRes () / width;
00025     unsigned src_skip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
00026 
00027     if (gray_line_step == 0)
00028       gray_line_step = width;
00029 
00030     unsigned dst_skip = gray_line_step - width; // skip of padding values in bytes
00031 
00032     unsigned char* dst_line = gray_buffer;
00033     const XnRGB24Pixel* src_line = image_md_->RGB24Data();
00034 
00035     for (unsigned yIdx = 0; yIdx < height; ++yIdx, src_line += src_skip, dst_line += dst_skip)
00036     {
00037       for (unsigned xIdx = 0; xIdx < width; ++xIdx, src_line += src_step, dst_line ++)
00038       {
00039         *dst_line = static_cast<unsigned char>((static_cast<int> (src_line->nRed)   * 299 + 
00040                                                 static_cast<int> (src_line->nGreen) * 587 +
00041                                                 static_cast<int> (src_line->nBlue)  * 114) * 0.001);
00042       }
00043     }
00044   }
00045   else
00046   {
00047     THROW_OPENNI_EXCEPTION ("Down-sampling only possible for integer scale. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00048   }
00049 }
00050 
00051 void ImageRGB24::fillRGB (unsigned width, unsigned height, unsigned char* rgb_buffer, unsigned rgb_line_step) const
00052 {
00053   if (width > image_md_->XRes () || height > image_md_->YRes ())
00054     THROW_OPENNI_EXCEPTION ("Up-sampling not supported. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00055 
00056   if (width == image_md_->XRes () && height == image_md_->YRes ())
00057   {
00058     unsigned line_size = width * 3;
00059     if (rgb_line_step == 0 || rgb_line_step == line_size)
00060     {
00061       memcpy (rgb_buffer, image_md_->Data(), image_md_->DataSize());
00062     }
00063     else // line by line
00064     {
00065       unsigned char* rgb_line = rgb_buffer;
00066       const unsigned char* src_line = static_cast<const unsigned char*> (image_md_->Data());
00067       for (unsigned yIdx = 0; yIdx < height; ++yIdx, rgb_line += rgb_line_step, src_line += line_size)
00068       {
00069         memcpy (rgb_line, src_line, line_size);
00070       }
00071     }
00072   }
00073   else if (image_md_->XRes () % width == 0 && image_md_->YRes () % height == 0) // downsamplig
00074   {
00075     unsigned src_step = image_md_->XRes () / width;
00076     unsigned src_skip = (image_md_->YRes () / height - 1) * image_md_->XRes ();
00077 
00078     if (rgb_line_step == 0)
00079       rgb_line_step = width * 3;
00080 
00081     unsigned dst_skip = rgb_line_step - width * 3; // skip of padding values in bytes
00082 
00083     XnRGB24Pixel* dst_line = reinterpret_cast<XnRGB24Pixel*> (rgb_buffer);
00084     const XnRGB24Pixel* src_line = image_md_->RGB24Data();
00085 
00086     for (unsigned yIdx = 0; yIdx < height; ++yIdx, src_line += src_skip)
00087     {
00088       for (unsigned xIdx = 0; xIdx < width; ++xIdx, src_line += src_step, dst_line ++)
00089       {
00090         *dst_line = *src_line;
00091       }
00092 
00093       if (dst_skip != 0)
00094       {
00095         // use bytes to skip rather than XnRGB24Pixel's, since line_step does not need to be multiple of 3
00096         unsigned char* temp = reinterpret_cast <unsigned char*> (dst_line);
00097         dst_line = reinterpret_cast <XnRGB24Pixel*> (temp + dst_skip);
00098       }
00099     }
00100   }
00101   else
00102   {
00103     THROW_OPENNI_EXCEPTION ("Down-sampling only possible for integer scale. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00104   }
00105 }
00106 
00107 bool ImageRGB24::isResizingSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const
00108 {
00109   return ImageRGB24::resizingSupported (input_width, input_height, output_width, output_height);
00110 }
00111 }
00112 
00113 #endif //HAVE_OPENNI
00114 


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:16:02