Point Cloud Library (PCL)  1.9.1-dev
NCVColorConversion.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (C) 2009-2010, NVIDIA Corporation, all rights reserved.
6  * Third party copyrights are property of their respective owners.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: $
38  * Ported to PCL by Koen Buys : Attention Work in progress!
39  */
40 
41 #ifndef _ncv_color_conversion_hpp_
42 #define _ncv_color_conversion_hpp_
43 
44 #include "NCVPixelOperations.hpp"
45 
46 enum NCVColorSpace
47 {
48  NCVColorSpaceGray,
49  NCVColorSpaceRGBA,
50 };
51 
52 template<NCVColorSpace CSin, NCVColorSpace CSout, typename Tin, typename Tout> struct __pixColorConv {
53 static void _pixColorConv(const Tin &pixIn, Tout &pixOut);
54 };
55 
56 template<typename Tin, typename Tout> struct __pixColorConv<NCVColorSpaceRGBA, NCVColorSpaceGray, Tin, Tout> {
57 static void _pixColorConv(const Tin &pixIn, Tout &pixOut)
58 {
59  Ncv32f luma = 0.299f * pixIn.x + 0.587f * pixIn.y + 0.114f * pixIn.z;
60  _TDemoteClampNN(luma, pixOut.x);
61 }};
62 
63 template<typename Tin, typename Tout> struct __pixColorConv<NCVColorSpaceGray, NCVColorSpaceRGBA, Tin, Tout> {
64 static void _pixColorConv(const Tin &pixIn, Tout &pixOut)
65 {
66  _TDemoteClampNN(pixIn.x, pixOut.x);
67  _TDemoteClampNN(pixIn.x, pixOut.y);
68  _TDemoteClampNN(pixIn.x, pixOut.z);
69  pixOut.w = 0;
70 }};
71 
72 template<NCVColorSpace CSin, NCVColorSpace CSout, typename Tin, typename Tout>
73 static
74 NCVStatus _ncvColorConv_host(const NCVMatrix<Tin> &h_imgIn,
75  const NCVMatrix<Tout> &h_imgOut)
76 {
77  ncvAssertReturn(h_imgIn.size() == h_imgOut.size(), NCV_DIMENSIONS_INVALID);
78  ncvAssertReturn(h_imgIn.memType() == h_imgOut.memType() &&
79  (h_imgIn.memType() == NCVMemoryTypeHostPinned || h_imgIn.memType() == NCVMemoryTypeNone), NCV_MEM_RESIDENCE_ERROR);
80  NCV_SET_SKIP_COND(h_imgIn.memType() == NCVMemoryTypeNone);
81  NCV_SKIP_COND_BEGIN
82 
83  for (Ncv32u i=0; i<h_imgIn.height(); i++)
84  {
85  for (Ncv32u j=0; j<h_imgIn.width(); j++)
86  {
87  __pixColorConv<CSin, CSout, Tin, Tout>::_pixColorConv(h_imgIn.at(j,i), h_imgOut.at(j,i));
88  }
89  }
90 
91  NCV_SKIP_COND_END
92  return NCV_SUCCESS;
93 }
94 
95 #endif //_ncv_color_conversion_hpp_
NCVMemoryType memType() const
Definition: NCV.hpp:769
NcvSize32u size() const
Definition: NCV.hpp:767
T & at(Ncv32u x, Ncv32u y) const
Definition: NCV.hpp:753
Ncv32u height() const
Definition: NCV.hpp:766
static void _pixColorConv(const Tin &pixIn, Tout &pixOut)
NCVMatrix (2D)
Definition: NCV.hpp:679
Ncv32u width() const
Definition: NCV.hpp:765