iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OpenNIImageSourceImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Shane Grant
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 namespace
37 {
38  // Color conversion functions adapted from NiViewer OpenNI sample
39  //! Converts one YUV444 pixel to one RGB pixel (bytes)
40  inline void YUV444ToRGB( XnUInt8 cY, XnUInt8 cU, XnUInt8 cV,
42  {
43  XnInt32 nC = cY - 16;
44  XnInt16 nD = cU - 128;
45  XnInt16 nE = cV - 128;
46 
47  nC = nC * 298 + 128;
48 
49  rgb.setR( XN_MIN(XN_MAX((nC + 409 * nE) >> 8, 0), 255) );
50  rgb.setG( XN_MIN(XN_MAX((nC - 100 * nD - 208 * nE) >> 8, 0), 255) );
51  rgb.setB( XN_MIN(XN_MAX((nC + 516 * nD ) >> 8, 0), 255) );
52  }
53 
54  //! Converts one YUV422 pixel to two PixRGB<nrt::byte>
55  inline void YUV422ToRGB( XnYUV422DoublePixel const & yuv, nrt::Image<nrt::PixRGB<nrt::byte>>::iterator const & iter )
56  {
57  YUV444ToRGB( yuv.nY1, yuv.nU, yuv.nV, *iter );
58  YUV444ToRGB( yuv.nY2, yuv.nU, yuv.nV, *(iter+1) );
59  }
60 }
61 
63 nrt::OpenNIImageSource::getYUV422Image( xn::ImageMetaData & imageMD )
64 {
65  nrt::Image<nrt::PixRGB<nrt::byte> > img(imageMD.XRes(), imageMD.YRes(), nrt::ImageInitPolicy::None);
66  const XnYUV422DoublePixel * yuvData = imageMD.YUV422Data();
67 
68  for( auto i = img.begin(); i != img.end(); i += 2 ) YUV422ToRGB( *yuvData++, i );
69 
70  return img;
71 }
72 
74 nrt::OpenNIImageSource::getRGB24Image( xn::ImageMetaData & imageMD )
75 {
76  nrt::Image<nrt::PixRGB<nrt::byte> > img(imageMD.XRes(), imageMD.YRes(), nrt::ImageInitPolicy::None);
77 
78  memcpy(img.pod_begin(), itsImageGen.GetRGB24ImageMap(), img.size() * 3);
79 
80  return img;
81 }
82 
84 nrt::OpenNIImageSource::getGray8Image( xn::ImageMetaData & imageMD)
85 {
86  nrt::Image<nrt::PixGray<nrt::byte> > img(imageMD.XRes(), imageMD.YRes(), nrt::ImageInitPolicy::None);
87 
88  memcpy(img.pod_begin(), imageMD.Grayscale8Data(), img.size());
89 
90  return img;
91 }
92 
94 nrt::OpenNIImageSource::getGray16Image( xn::ImageMetaData & imageMD)
95 {
96  nrt::Image<nrt::PixGray<uint16_t> > img(imageMD.XRes(), imageMD.YRes(), nrt::ImageInitPolicy::None);
97 
98  memcpy(img.pod_begin(), imageMD.Grayscale16Data(), img.size() * sizeof(uint16_t));
99 
100  return img;
101 }
102 
104 nrt::OpenNIImageSource::getDepthImage(xn::DepthMetaData & depthMD)
105 {
106  nrt::Image<nrt::PixGray<uint16_t> > ret(depthMD.XRes(), depthMD.YRes(), nrt::ImageInitPolicy::None);
107 
108  memcpy(ret.pod_begin(), depthMD.Data(), ret.size() * sizeof(uint16_t));
109 
110  return ret;
111 }
112 
114 nrt::OpenNIImageSource::getIRImage( xn::IRMetaData & irMD)
115 {
116  nrt::Image<nrt::PixGray<uint16_t> > ret(irMD.XRes(), irMD.YRes(), nrt::ImageInitPolicy::None);
117 
118  memcpy(ret.pod_begin(), itsIRGen.GetIRMap(), ret.size() * sizeof(uint16_t));
119 
120  return ret;
121 }