iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PixelBaseHelpers.H
Go to the documentation of this file.
1 /*! @file
2  @author Randolph Voorhies
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 #ifndef INCLUDE_NRT_CORE_IMAGE_DETAILS_PIXELBASEHELPERS_H
37 #define INCLUDE_NRT_CORE_IMAGE_DETAILS_PIXELBASEHELPERS_H
38 
40 
41 namespace nrt{
42  class PixelBaseCore {};
43 
44  template<class T>
45  struct PixelWithAlpha
46  {
47  public:
48 
49  typedef T ExtraPODType;
50 
51  PixelWithAlpha() : alpha(0) {}
52 
53  template <typename T1, typename = typename std::enable_if<std::is_arithmetic<T1>::value>::type>
54  PixelWithAlpha(T1 const & val) { alpha = clamped_convert<ExtraPODType>(val); }
55 
56  template <typename T1>
57  PixelWithAlpha(PixelWithAlpha<T1> const & other) { alpha = clamped_convert<ExtraPODType>(other.alpha); }
58 
59  T a() const { return alpha; }
60 
61  void setA(T a) { alpha = a; }
62 
63  template<class T2>
64  void setA(T2 a) { alpha = clamped_convert<T>(a); }
65 
66  template <class Archive>
67  void serialize(Archive& ar)
68  { ar( alpha ); }
69 
70  T alpha;
71  };
72 
73  template<class T1, typename T2> inline
74  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithAlpha<T1>>::type &
75  operator+=(PixelWithAlpha<T1> & lhs, T2 const rhs)
76  {
77  lhs.alpha += rhs;
78  return lhs;
79  }
80 
81  template<class T1, typename T2> inline
82  PixelWithAlpha<typename promote<T1, T2>::type>
83  operator+(PixelWithAlpha<T1> const & lhs, PixelWithAlpha<T2> const & rhs)
84  {
85  PixelWithAlpha<typename promote<T1, T2>::type> ret;
86  ret.alpha = lhs.alpha + rhs.alpha;
87  return ret;
88  }
89 
90  template<class T1, typename T2> inline
91  typename std::enable_if<std::is_arithmetic<T2>::value,
92  PixelWithAlpha<typename promote<T1, T2>::type>>::type
93  operator+(PixelWithAlpha<T1> const & lhs, T2 const & rhs)
94  {
95  PixelWithAlpha<typename promote<T1, T2>::type> ret;
96  ret.alpha = lhs.alpha + rhs;
97  return ret;
98  }
99 
100  template<class T1, typename T2> inline
101  typename std::enable_if<std::is_arithmetic<T1>::value,
102  PixelWithAlpha<typename promote<T1, T2>::type>>::type
103  operator+(T1 const & lhs, PixelWithAlpha<T2> const & rhs)
104  {
105  PixelWithAlpha<typename promote<T1, T2>::type> ret;
106  ret.alpha = lhs + rhs.alpha;
107  return ret;
108  }
109 
110  template<class T1, typename T2> inline
111  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithAlpha<T1>>::type &
112  operator-=(PixelWithAlpha<T1> & lhs, T2 const rhs)
113  {
114  lhs.alpha -= rhs;
115  return lhs;
116  }
117 
118  template<class T1, typename T2> inline
119  PixelWithAlpha<typename promote<T1, T2>::type>
120  operator-(PixelWithAlpha<T1> const & lhs, PixelWithAlpha<T2> const & rhs)
121  {
122  PixelWithAlpha<typename promote<T1, T2>::type> ret;
123  ret.alpha = lhs.alpha - rhs.alpha;
124  return ret;
125  }
126 
127  template<class T1, typename T2> inline
128  typename std::enable_if<std::is_arithmetic<T2>::value,
129  PixelWithAlpha<typename promote<T1, T2>::type>>::type
130  operator-(PixelWithAlpha<T1> const & lhs, T2 const & rhs)
131  {
132  PixelWithAlpha<typename promote<T1, T2>::type> ret;
133  ret.alpha = lhs.alpha - rhs;
134  return ret;
135  }
136 
137  template<class T1, typename T2> inline
138  typename std::enable_if<std::is_arithmetic<T1>::value,
139  PixelWithAlpha<typename promote<T1, T2>::type>>::type
140  operator-(T1 const & lhs, PixelWithAlpha<T2> const & rhs)
141  {
142  PixelWithAlpha<typename promote<T1, T2>::type> ret;
143  ret.alpha = lhs - rhs.alpha;
144  return ret;
145  }
146 
147  template<class T1, typename T2> inline
148  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithAlpha<T1>>::type &
149  operator*=(PixelWithAlpha<T1> & lhs, T2 const rhs)
150  {
151  lhs.alpha *= rhs;
152  return lhs;
153  }
154 
155  template<class T1, typename T2> inline
156  PixelWithAlpha<typename promote<T1, T2>::type>
157  operator*(PixelWithAlpha<T1> const & lhs, PixelWithAlpha<T2> const & rhs)
158  {
159  PixelWithAlpha<typename promote<T1, T2>::type> ret;
160  ret.alpha = lhs.alpha * rhs.alpha;
161  return ret;
162  }
163 
164  template<class T1, typename T2> inline
165  typename std::enable_if<std::is_arithmetic<T2>::value,
166  PixelWithAlpha<typename promote<T1, T2>::type>>::type
167  operator*(PixelWithAlpha<T1> const & lhs, T2 const & rhs)
168  {
169  PixelWithAlpha<typename promote<T1, T2>::type> ret;
170  ret.alpha = lhs.alpha * rhs;
171  return ret;
172  }
173 
174  template<class T1, typename T2> inline
175  typename std::enable_if<std::is_arithmetic<T1>::value,
176  PixelWithAlpha<typename promote<T1, T2>::type>>::type
177  operator*(T1 const & lhs, PixelWithAlpha<T2> const & rhs)
178  {
179  PixelWithAlpha<typename promote<T1, T2>::type> ret;
180  ret.alpha = lhs * rhs.alpha;
181  return ret;
182  }
183 
184  template<class T1, typename T2> inline
185  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithAlpha<T1>>::type &
186  operator/=(PixelWithAlpha<T1> & lhs, T2 const rhs)
187  {
188  lhs.alpha /= rhs;
189  return lhs;
190  }
191 
192  template<class T1, typename T2> inline
193  PixelWithAlpha<typename promote<T1, T2>::type>
194  operator/(PixelWithAlpha<T1> const & lhs, PixelWithAlpha<T2> const & rhs)
195  {
196  PixelWithAlpha<typename promote<T1, T2>::type> ret;
197  ret.alpha = lhs.alpha / rhs.alpha;
198  return ret;
199  }
200 
201  template<class T1, typename T2> inline
202  typename std::enable_if<std::is_arithmetic<T2>::value,
203  PixelWithAlpha<typename promote<T1, T2>::type>>::type
204  operator/(PixelWithAlpha<T1> const & lhs, T2 const & rhs)
205  {
206  PixelWithAlpha<typename promote<T1, T2>::type> ret;
207  ret.alpha = lhs.alpha / rhs;
208  return ret;
209  }
210 
211  template<class T1, typename T2> inline
212  typename std::enable_if<std::is_arithmetic<T1>::value,
213  PixelWithAlpha<typename promote<T1, T2>::type>>::type
214  operator/(T1 const & lhs, PixelWithAlpha<T2> const & rhs)
215  {
216  PixelWithAlpha<typename promote<T1, T2>::type> ret;
217  ret.alpha = lhs / rhs.alpha;
218  return ret;
219  }
220 
221 
222  template<class T>
223  class PixelWithDepth
224  {
225  public:
226 
227  typedef T ExtraPODType;
228 
229  PixelWithDepth() : depth(0) {}
230 
231  template <typename T1, typename = typename std::enable_if<std::is_arithmetic<T1>::value>::type>
232  PixelWithDepth(T1 const & val) { depth = clamped_convert<ExtraPODType>(val); }
233 
234  template <typename T1>
235  PixelWithDepth(PixelWithDepth<T1> const & other) { depth = clamped_convert<ExtraPODType>(other.depth); }
236 
237  T d() const { return depth; }
238 
239  void setD(T d) { depth = d; }
240 
241  template<class T2>
242  void setD(T2 d) { depth = clamped_convert<T>(d); }
243 
244  template <class Archive>
245  void serialize(Archive& ar)
246  { ar( depth ); }
247 
248  T depth;
249  };
250 
251  template<class T1, typename T2> inline
252  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithDepth<T1>>::type &
253  operator+=(PixelWithDepth<T1> & lhs, T2 const rhs)
254  {
255  lhs.depth += rhs;
256  return lhs;
257  }
258 
259  template<class T1, typename T2> inline
260  PixelWithDepth<typename promote<T1, T2>::type>
261  operator+(PixelWithDepth<T1> const & lhs, PixelWithDepth<T2> const & rhs)
262  {
263  PixelWithDepth<typename promote<T1, T2>::type> ret;
264  ret.depth = lhs.depth + rhs.depth;
265  return ret;
266  }
267 
268  template<class T1, typename T2> inline
269  typename std::enable_if<std::is_arithmetic<T2>::value,
270  PixelWithDepth<typename promote<T1, T2>::type>>::type
271  operator+(PixelWithDepth<T1> const & lhs, T2 const & rhs)
272  {
273  PixelWithDepth<typename promote<T1, T2>::type> ret;
274  ret.depth = lhs.depth + rhs;
275  return ret;
276  }
277 
278  template<class T1, typename T2> inline
279  typename std::enable_if<std::is_arithmetic<T1>::value,
280  PixelWithDepth<typename promote<T1, T2>::type>>::type
281  operator+(T1 const & lhs, PixelWithDepth<T2> const & rhs)
282  {
283  PixelWithDepth<typename promote<T1, T2>::type> ret;
284  ret.depth = lhs + rhs.depth;
285  return ret;
286  }
287 
288  template<class T1, typename T2> inline
289  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithDepth<T1>>::type &
290  operator-=(PixelWithDepth<T1> & lhs, T2 const rhs)
291  {
292  lhs.depth -= rhs;
293  return lhs;
294  }
295 
296  template<class T1, typename T2> inline
297  PixelWithDepth<typename promote<T1, T2>::type>
298  operator-(PixelWithDepth<T1> const & lhs, PixelWithDepth<T2> const & rhs)
299  {
300  PixelWithDepth<typename promote<T1, T2>::type> ret;
301  ret.depth = lhs.depth - rhs.depth;
302  return ret;
303  }
304 
305  template<class T1, typename T2> inline
306  typename std::enable_if<std::is_arithmetic<T2>::value,
307  PixelWithDepth<typename promote<T1, T2>::type>>::type
308  operator-(PixelWithDepth<T1> const & lhs, T2 const & rhs)
309  {
310  PixelWithDepth<typename promote<T1, T2>::type> ret;
311  ret.depth = lhs.depth - rhs;
312  return ret;
313  }
314 
315  template<class T1, typename T2> inline
316  typename std::enable_if<std::is_arithmetic<T1>::value,
317  PixelWithDepth<typename promote<T1, T2>::type>>::type
318  operator-(T1 const & lhs, PixelWithDepth<T2> const & rhs)
319  {
320  PixelWithDepth<typename promote<T1, T2>::type> ret;
321  ret.depth = lhs - rhs.depth;
322  return ret;
323  }
324 
325  template<class T1, typename T2> inline
326  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithDepth<T1>>::type &
327  operator*=(PixelWithDepth<T1> & lhs, T2 const rhs)
328  {
329  lhs.depth *= rhs;
330  return lhs;
331  }
332 
333  template<class T1, typename T2> inline
334  PixelWithDepth<typename promote<T1, T2>::type>
335  operator*(PixelWithDepth<T1> const & lhs, PixelWithDepth<T2> const & rhs)
336  {
337  PixelWithDepth<typename promote<T1, T2>::type> ret;
338  ret.depth = lhs.depth * rhs.depth;
339  return ret;
340  }
341 
342  template<class T1, typename T2> inline
343  typename std::enable_if<std::is_arithmetic<T2>::value,
344  PixelWithDepth<typename promote<T1, T2>::type>>::type
345  operator*(PixelWithDepth<T1> const & lhs, T2 const & rhs)
346  {
347  PixelWithDepth<typename promote<T1, T2>::type> ret;
348  ret.depth = lhs.depth * rhs;
349  return ret;
350  }
351 
352  template<class T1, typename T2> inline
353  typename std::enable_if<std::is_arithmetic<T1>::value,
354  PixelWithDepth<typename promote<T1, T2>::type>>::type
355  operator*(T1 const & lhs, PixelWithDepth<T2> const & rhs)
356  {
357  PixelWithDepth<typename promote<T1, T2>::type> ret;
358  ret.depth = lhs * rhs.depth;
359  return ret;
360  }
361 
362  template<class T1, typename T2> inline
363  typename std::enable_if<std::is_arithmetic<T2>::value, PixelWithDepth<T1>>::type &
364  operator/=(PixelWithDepth<T1> & lhs, T2 const rhs)
365  {
366  lhs.depth /= rhs;
367  return lhs;
368  }
369 
370  template<class T1, typename T2> inline
371  PixelWithDepth<typename promote<T1, T2>::type>
372  operator/(PixelWithDepth<T1> const & lhs, PixelWithDepth<T2> const & rhs)
373  {
374  PixelWithDepth<typename promote<T1, T2>::type> ret;
375  ret.depth = lhs.depth / rhs.depth;
376  return ret;
377  }
378 
379  template<class T1, typename T2> inline
380  typename std::enable_if<std::is_arithmetic<T2>::value,
381  PixelWithDepth<typename promote<T1, T2>::type>>::type
382  operator/(PixelWithDepth<T1> const & lhs, T2 const & rhs)
383  {
384  PixelWithDepth<typename promote<T1, T2>::type> ret;
385  ret.depth = lhs.depth / rhs;
386  return ret;
387  }
388 
389  template<class T1, typename T2> inline
390  typename std::enable_if<std::is_arithmetic<T1>::value,
391  PixelWithDepth<typename promote<T1, T2>::type>>::type
392  operator/(T1 const & lhs, PixelWithDepth<T2> const & rhs)
393  {
394  PixelWithDepth<typename promote<T1, T2>::type> ret;
395  ret.depth = lhs / rhs.depth;
396  return ret;
397  }
398 
399 
400 }
401 
402 #endif // INCLUDE_NRT_CORE_IMAGE_DETAILS_PIXELBASEHELPERS_H