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


openni_camera
Author(s): Patrick Mihelich, Suat Gedikli, Radu Bogdan Rusu
autogenerated on Tue Apr 18 2017 02:14:10