SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
rgb_components.h
Go to the documentation of this file.
1 /*
2  This file is part of the CVD Library.
3 
4  Copyright (C) 2005 The Authors
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc.,
19  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21 #ifndef CVD_RGB_TRAITS_H
22 #define CVD_RGB_TRAITS_H
23 
24 #include "rgb.h"
25 #include "rgba.h"
26 #include "rgb8.h"
27 #include "la.h"
28 #include "builtin_components.h"
29 #include "pixel_traits.h"
30 
31 namespace CVD
32 {
33  namespace Pixel
34  {
35 
36  template<class P> struct Component<Rgb<P> >
37  {
38  typedef P type;
39  static const size_t count = 3;
40 
41 
42  //This version is much faster, with -funroll-loops
43  static const P& get(const Rgb<P>& pixel, size_t i)
44  {
45  return *(reinterpret_cast<const P*>(&pixel)+i);
46  //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue);
47  }
48 
49  static P& get(Rgb<P>& pixel, size_t i)
50  {
51  return *(reinterpret_cast<P*>(&pixel)+i);
52  // return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue);
53  }
54  };
55 
56 
57 
58  template<> struct Component<Rgb8>
59  {
60  typedef unsigned char type;
61  static const size_t count = 3;
62 
63  static const type& get(const Rgb8& pixel, size_t i)
64  {
65  return *(reinterpret_cast<const unsigned char*>(&pixel)+i);
66  //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue);
67  }
68 
69  static type& get(Rgb8& pixel, size_t i)
70  {
71  return *(reinterpret_cast<unsigned char*>(&pixel)+i);
72  //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue);
73  }
74  };
75 
76  template<class P> struct Component<Rgba<P> >
77  {
78  typedef P type;
79  static const size_t count = 4;
80 
81  static const P& get(const Rgba<P>& pixel, size_t i)
82  {
83  return *(reinterpret_cast<const P*>(&pixel)+i);
84  //return i == 0 ? pixel.red : (i==1 ? pixel.green : (i==2 ?pixel.blue: pixel.alpha));
85  }
86 
87  static P& get(Rgba<P>& pixel, size_t i)
88  {
89  return *(reinterpret_cast<P*>(&pixel)+i);
90  //return i == 0 ? pixel.red : (i==1 ? pixel.green : (i==2 ?pixel.blue: pixel.alpha));
91  }
92  };
93 
94  template<class P> struct Component<La<P> >
95  {
96  typedef P type;
97  static const size_t count = 2;
98 
99  static const P& get(const La<P>& pixel, size_t i)
100  {
101  return *(reinterpret_cast<const P*>(&pixel)+i);
102  }
103 
104  static P& get(La<P>& pixel, size_t i)
105  {
106  return *(reinterpret_cast<P*>(&pixel)+i);
107  }
108  };
109 
110  template <class T> struct is_Rgb { enum { value = 0 }; };
111  template <class T> struct is_Rgb<Rgb<T> > { enum { value = 1 }; };
112  template <> struct is_Rgb<Rgb8> { enum { value = 1 }; };
113  template <class T> struct is_Rgb<Rgba<T> > { enum { value = 1 }; };
114 
115 
116  template<class T, int LIFT> struct traits<Rgb<T>, LIFT>
117  {
120  };
121 
122  template<class T, int LIFT> struct traits<Rgba<T>, LIFT>
123  {
126  };
127 
128  template<class T, int LIFT> struct traits<La<T>, LIFT>
129  {
132  };
133 
134  template<int LIFT> struct traits<Rgb8, LIFT>
135  {
138  };
139 
140  }
141  template <class T> struct Rgb_ops {
142  template <class S> static inline T sum(const T& a, const S& b) { return T(a.red+b.red, a.green+b.green, a.blue+b.blue); }
143  template <class S> static inline void add(T& a, const S& b) { a.red+=b.red; a.green+=b.green; a.blue+=b.blue; }
144  template <class S> static inline T diff(const T& a, const S& b) { return T(a.red-b.red, a.green-b.green, a.blue-b.blue); }
145  template <class S> static inline void sub(T& a, const S& b) { a.red-=b.red; a.green-=b.green; a.blue-=b.blue; }
146  template <class S> static inline T prod(const T& a, const S& b) { return T(a.red*b, a.green*b, a.blue*b); }
147  template <class S> static inline void mul(T& a, const S& b) { a.red*=b; a.green*=b; a.blue*=b; }
148  template <class S> static inline T quot(const T& a, const S& b) { return T(a.red/b, a.green/b, a.blue/b); }
149  template <class S> static inline void div(T& a, const S& b) { a.red/=b; a.green/=b; a.blue/=b; }
150  template <class S> static inline void assign(T& a, const S& b) { a.red=b.red; a.green=b.green; a.blue=b.blue; }
151  };
152 
153  template <class T, class S> inline Rgb<T> operator+(const Rgb<T>& a, const Rgb<S>& b) { return Rgb_ops<Rgb<T> >::sum(a,b); }
154  template <class T, class S> inline Rgb<T>& operator+=(Rgb<T>& a, const Rgb<S>& b) { Rgb_ops<Rgb<T> >::add(a,b); return a; }
155  template <class T, class S> inline Rgb<T> operator-(const Rgb<T>& a, const Rgb<S>& b) { return Rgb_ops<Rgb<T> >::diff(a,b); }
156  template <class T, class S> inline Rgb<T>& operator-=(Rgb<T>& a, const Rgb<S>& b) { Rgb_ops<Rgb<T> >::sub(a,b); return a; }
157  template <class T, class S> inline Rgb<T> operator*(const Rgb<T>& a, const S& b) { return Rgb_ops<Rgb<T> >::prod(a,b); }
158  template <class T, class S> inline Rgb<T> operator*(const S& b, const Rgb<T>& a) { return Rgb_ops<Rgb<T> >::prod(a,b); }
159  template <class T, class S> inline Rgb<T>& operator*=(Rgb<T>& a, const S& b) { Rgb_ops<Rgb<T> >::mul(a,b); return a; }
160  template <class T, class S> inline Rgb<T> operator/(const Rgb<T>& a, const S& b) { return Rgb_ops<Rgb<T> >::quot(a,b); }
161  template <class T, class S> inline Rgb<T> operator/(const S& b, const Rgb<T>& a) { return Rgb_ops<Rgb<T> >::quot(a,b); }
162  template <class T, class S> inline Rgb<T>& operator/=(Rgb<T>& a, const S& b) { Rgb_ops<Rgb<T> >::div(a,b); return a; }
163 
164 
165  template <class T> struct Rgba_ops {
166  template <class S> static inline T sum(const T& a, const S& b) { return T(a.red+b.red, a.green+b.green, a.blue+b.blue, a.alpha+b.alpha); }
167  template <class S> static inline void add(T& a, const S& b) { a.red+=b.red; a.green+=b.green; a.blue+=b.blue; a.alpha+=b.alpha;}
168  template <class S> static inline T diff(const T& a, const S& b) { return T(a.red-b.red, a.green-b.green, a.blue-b.blue, a.alpha-b.alpha); }
169  template <class S> static inline void sub(T& a, const S& b) { a.red-=b.red; a.green-=b.green; a.blue-=b.blue; a.alpha-=b.alpha; }
170  template <class S> static inline T prod(const T& a, const S& b) { return T(a.red*b, a.green*b, a.blue*b, a.alpha*b); }
171  template <class S> static inline void mul(T& a, const S& b) { a.red*=b; a.green*=b; a.blue*=b; a.alpha*=b; }
172  template <class S> static inline T quot(const T& a, const S& b) { return T(a.red/b, a.green/b, a.blue/b, a.alpha/b); }
173  template <class S> static inline void div(T& a, const S& b) { a.red/=b; a.green/=b; a.blue/=b; a.alpha/=b;}
174  template <class S> static inline void assign(T& a, const S& b) { a.red=b.red; a.green=b.green; a.blue=b.blue; a.alpha=b.alpha; }
175  };
176 
177  template <class T, class S> inline Rgba<T> operator+(const Rgba<T>& a, const Rgba<S>& b) { return Rgba_ops<Rgba<T> >::sum(a,b); }
178  template <class T, class S> inline Rgba<T>& operator+=(Rgba<T>& a, const Rgba<S>& b) { Rgba_ops<Rgba<T> >::add(a,b); return a; }
179  template <class T, class S> inline Rgba<T> operator-(const Rgba<T>& a, const Rgba<S>& b) { return Rgba_ops<Rgba<T> >::diff(a,b); }
180  template <class T, class S> inline Rgba<T>& operator-=(Rgba<T>& a, const Rgba<S>& b) { Rgba_ops<Rgba<T> >::sub(a,b); return a; }
181  template <class T, class S> inline Rgba<T> operator*(const Rgba<T>& a, const S& b) { return Rgba_ops<Rgba<T> >::prod(a,b); }
182  template <class T, class S> inline Rgba<T> operator*(const S& b, const Rgba<T>& a) { return Rgba_ops<Rgba<T> >::prod(a,b); }
183  template <class T, class S> inline Rgba<T>& operator*=(Rgba<T>& a, const S& b) { Rgba_ops<Rgba<T> >::mul(a,b); return a; }
184  template <class T, class S> inline Rgba<T> operator/(const Rgba<T>& a, const S& b) { return Rgba_ops<Rgba<T> >::quot(a,b); }
185  template <class T, class S> inline Rgba<T> operator/(const S& b, const Rgba<T>& a) { return Rgba_ops<Rgba<T> >::quot(a,b); }
186  template <class T, class S> inline Rgba<T>& operator/=(Rgba<T>& a, const S& b) { Rgba_ops<Rgba<T> >::div(a,b); return a; }
187 
188  template <class T> struct La_ops {
189  template <class S> static inline T sum(const T& a, const S& b) { return T(a.luminance+b.luminance, a.alpha+b.alpha); }
190  template <class S> static inline void add(T& a, const S& b) { a.luminance+=b.luminance; a.alpha+=b.alpha;}
191  template <class S> static inline T diff(const T& a, const S& b) { return T(a.luminance-b.luminance, a.alpha-b.alpha); }
192  template <class S> static inline void sub(T& a, const S& b) { a.luminance-=b.luminance; a.alpha-=b.alpha; }
193  template <class S> static inline T prod(const T& a, const S& b) { return T(a.luminance*b, a.alpha*b); }
194  template <class S> static inline void mul(T& a, const S& b) { a.luminance*=b; a.alpha*=b; }
195  template <class S> static inline T quot(const T& a, const S& b) { return T(a.luminance/b, a.alpha/b); }
196  template <class S> static inline void div(T& a, const S& b) { a.luminance/=b; a.alpha/=b;}
197  template <class S> static inline void assign(T& a, const S& b) { a.luminance=b.luminance; a.alpha=b.alpha; }
198  };
199 
200  template <class T, class S> inline La<T> operator+(const La<T>& a, const La<S>& b) { return La_ops<La<T> >::sum(a,b); }
201  template <class T, class S> inline La<T>& operator+=(La<T>& a, const La<S>& b) { La_ops<La<T> >::add(a,b); return a; }
202  template <class T, class S> inline La<T> operator-(const La<T>& a, const La<S>& b) { return La_ops<La<T> >::diff(a,b); }
203  template <class T, class S> inline La<T>& operator-=(La<T>& a, const La<S>& b) { La_ops<La<T> >::sub(a,b); return a; }
204  template <class T, class S> inline La<T> operator*(const La<T>& a, const S& b) { return La_ops<La<T> >::prod(a,b); }
205  template <class T, class S> inline La<T> operator*(const S& b, const La<T>& a) { return La_ops<La<T> >::prod(a,b); }
206  template <class T, class S> inline La<T>& operator*=(La<T>& a, const S& b) { La_ops<La<T> >::mul(a,b); return a; }
207  template <class T, class S> inline La<T> operator/(const La<T>& a, const S& b) { return La_ops<La<T> >::quot(a,b); }
208  template <class T, class S> inline La<T> operator/(const S& b, const La<T>& a) { return La_ops<La<T> >::quot(a,b); }
209  template <class T, class S> inline La<T>& operator/=(La<T>& a, const S& b) { La_ops<La<T> >::div(a,b); return a; }
210 
211 
212 
213 }
214 #endif
static T diff(const T &a, const S &b)
La< typename Pixel::traits< T >::wider_type > wider_type
static void div(T &a, const S &b)
Rgba< typename Pixel::traits< T >::wider_type > wider_type
static const size_t count
Definition: rgb.h:45
Definition: rgba.h:38
static T quot(const T &a, const S &b)
static void add(T &a, const S &b)
Rgba< typename Pixel::traits< T >::float_type > float_type
static T diff(const T &a, const S &b)
static T prod(const T &a, const S &b)
static void add(T &a, const S &b)
static void mul(T &a, const S &b)
static T sum(const T &a, const S &b)
Rgb< T > operator/(const Rgb< T > &a, const S &b)
Definition: abs.h:24
static void div(T &a, const S &b)
static T quot(const T &a, const S &b)
static void assign(T &a, const S &b)
Rgb< typename Pixel::traits< T >::wider_type > wider_type
Rgb< T > operator+(const Rgb< T > &a, const Rgb< S > &b)
static T prod(const T &a, const S &b)
Rgb< T > operator-(const Rgb< T > &a, const Rgb< S > &b)
static void sub(T &a, const S &b)
static T diff(const T &a, const S &b)
static T quot(const T &a, const S &b)
static void mul(T &a, const S &b)
ImageRef operator*(const int scale, const ImageRef &ref)
Definition: image_ref.h:367
Rgb< T > & operator-=(Rgb< T > &a, const Rgb< S > &b)
static T prod(const T &a, const S &b)
Rgb< T > & operator/=(Rgb< T > &a, const S &b)
static void add(T &a, const S &b)
Rgb< typename Pixel::traits< T >::float_type > float_type
static T sum(const T &a, const S &b)
Rgb< T > & operator+=(Rgb< T > &a, const Rgb< S > &b)
static void mul(T &a, const S &b)
static void sub(T &a, const S &b)
La< typename Pixel::traits< T >::float_type > float_type
Definition: rgb8.h:33
Definition: la.h:38
static T sum(const T &a, const S &b)
static void div(T &a, const S &b)
static void assign(T &a, const S &b)
static void sub(T &a, const S &b)
Rgb< T > & operator*=(Rgb< T > &a, const S &b)
static void assign(T &a, const S &b)