00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <openni_camera/openni_image_yuv_422.h>
00038 #include <sstream>
00039 #include <iostream>
00040
00041 using namespace std;
00042 namespace openni_wrapper
00043 {
00044
00045 ImageYUV422::ImageYUV422 (boost::shared_ptr<xn::ImageMetaData> image_meta_data) throw ()
00046 : Image (image_meta_data)
00047 {
00048 }
00049
00050 ImageYUV422::~ImageYUV422 () throw ()
00051 {
00052 }
00053
00054 bool ImageYUV422::isResizingSupported (unsigned input_width, unsigned input_height, unsigned output_width, unsigned output_height) const
00055 {
00056 return ImageYUV422::resizingSupported (input_width, input_height, output_width, output_height);
00057 }
00058
00059 void ImageYUV422::fillRGB (unsigned width, unsigned height, unsigned char* rgb_buffer, unsigned rgb_line_step) const throw (OpenNIException)
00060 {
00061
00062 if (width > image_md_->XRes () || height > image_md_->YRes ())
00063 THROW_OPENNI_EXCEPTION ("Upsampling not supported. Request was: %d x %d -> %d x %d", image_md_->XRes (), image_md_->YRes (), width, height);
00064
00065 if (image_md_->XRes () % width != 0 || image_md_->YRes () % height != 0)
00066 THROW_OPENNI_EXCEPTION ("Downsampling only possible for integer scales in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00067
00068 register const XnUInt8* yuv_buffer = image_md_->Data();
00069
00070 unsigned rgb_line_skip = 0;
00071 if (rgb_line_step != 0)
00072 rgb_line_skip = rgb_line_step - width * 3;
00073
00074 if (image_md_->XRes() == width && image_md_->YRes() == height)
00075 {
00076 for( register unsigned yIdx = 0; yIdx < height; ++yIdx, rgb_buffer += rgb_line_skip )
00077 {
00078 for( register unsigned xIdx = 0; xIdx < width; xIdx += 2, rgb_buffer += 6, yuv_buffer += 4 )
00079 {
00080 rgb_buffer[0] = max (0, min (255, (int)yuv_buffer[1] + (int)(1.13983 * (float)(yuv_buffer[2]-128) ) ) );
00081 rgb_buffer[1] = max (0, min (255, (int)yuv_buffer[1] + (int)(124.832 -0.39465 * (float)yuv_buffer[0] - 0.58060 * (float)yuv_buffer[2] )));
00082 rgb_buffer[2] = max (0, min (255, (int)yuv_buffer[1] + (int)(2.03211 * (float)(yuv_buffer[0]-128)) ) );
00083
00084 rgb_buffer[3] = max (0, min (255, (int)yuv_buffer[3] + (int)(1.13983 * (float)(yuv_buffer[2]-128) ) ) );
00085 rgb_buffer[4] = max (0, min (255, (int)yuv_buffer[3] + (int)(124.832 -0.39465 * (float)yuv_buffer[0] - 0.58060 * (float)yuv_buffer[2] )));
00086 rgb_buffer[5] = max (0, min (255, (int)yuv_buffer[3] + (int)(2.03211 * (float)(yuv_buffer[0]-128)) ) );
00087 }
00088 }
00089 }
00090 else
00091 {
00092 register unsigned yuv_step = image_md_->XRes() / width;
00093 register unsigned yuv_x_step = yuv_step << 1;
00094 register unsigned yuv_skip = (image_md_->YRes() / height - 1) * ( image_md_->XRes() << 1 );
00095 for( register unsigned yIdx = 0; yIdx < image_md_->YRes(); yIdx += yuv_step, yuv_buffer += yuv_skip, rgb_buffer += rgb_line_skip )
00096 {
00097 for( register unsigned xIdx = 0; xIdx < image_md_->XRes(); xIdx += yuv_step, rgb_buffer += 3, yuv_buffer += yuv_x_step )
00098 {
00099 rgb_buffer[0] = max (0, min (255, (int)yuv_buffer[1] + (int)(1.13983 * (float)(yuv_buffer[2]-128) ) ) );
00100 rgb_buffer[1] = max (0, min (255, (int)yuv_buffer[1] + (int)(124.832 -0.39465 * (float)yuv_buffer[0] - 0.58060 * (float)yuv_buffer[2] )));
00101 rgb_buffer[2] = max (0, min (255, (int)yuv_buffer[1] + (int)(2.03211 * (float)(yuv_buffer[0]-128)) ) );
00102 }
00103 }
00104 }
00105 }
00106
00107 void ImageYUV422::fillGrayscale (unsigned width, unsigned height, unsigned char* gray_buffer, unsigned gray_line_step) const throw (OpenNIException)
00108 {
00109
00110 if (width > image_md_->XRes () || height > image_md_->YRes ())
00111 THROW_OPENNI_EXCEPTION ("Upsampling not supported. Request was: %d x %d -> %d x %d", image_md_->XRes (), image_md_->YRes (), width, height);
00112
00113 if (image_md_->XRes () % width != 0 || image_md_->YRes () % height != 0)
00114 THROW_OPENNI_EXCEPTION ("Downsampling only possible for integer scales in both dimensions. Request was %d x %d -> %d x %d.", image_md_->XRes (), image_md_->YRes (), width, height);
00115
00116 unsigned gray_line_skip = 0;
00117 if (gray_line_step != 0)
00118 gray_line_skip = gray_line_step - width;
00119
00120 register unsigned yuv_step = image_md_->XRes() / width;
00121 register unsigned yuv_x_step = yuv_step << 1;
00122 register unsigned yuv_skip = (image_md_->YRes() / height - 1) * ( image_md_->XRes() << 1 );
00123 register const XnUInt8* yuv_buffer = (image_md_->Data() + 1);
00124
00125 for( register unsigned yIdx = 0; yIdx < image_md_->YRes(); yIdx += yuv_step, yuv_buffer += yuv_skip, gray_buffer += gray_line_skip )
00126 {
00127 for( register unsigned xIdx = 0; xIdx < image_md_->XRes(); xIdx += yuv_step, ++gray_buffer, yuv_buffer += yuv_x_step )
00128 {
00129 *gray_buffer = *yuv_buffer;
00130 }
00131 }
00132 }
00133 }