iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PixelBase.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_PIXELBASE_H
37 #define INCLUDE_NRT_CORE_IMAGE_PIXELBASE_H
38 
43 
44 #include <algorithm>
45 #include <array>
46 #include <sstream>
47 #include <string>
48 #include <typeinfo>
49 
50 namespace nrt
51 {
52  template <class T> struct PixRGB;
53 
54  template <class T, size_t numChans, template<typename> class PixType>
55  struct PixelBase : private trivial_type, public PixelBaseCore
56  {
57  static_assert(std::is_pod<T>::value, "Pixel types may only store plain-old-data");
58 
59  template<class Q> struct rebind { typedef PixelBase<Q, numChans, PixType> type; };
60 
61  //! Access beginning of underlying array
62  T* begin();
63 
64  //! Access the end of underlying array
65  T* end();
66 
67  //! Access the beginning of underlying array
68  T const * begin() const;
69 
70  //! Access the end of underlying array
71  T const * end() const;
72 
73  //! Convenience function to print out the values as a nicely formatted string
74  std::string toString();
75 
76  //! The number of channels contained in this Pixel
77  const static size_t numChannels = numChans;
78 
79  //! The type of data stored in the channels
80  typedef T PODType;
81 
82  //! The actual data held by this pixel type
83  std::array<T,numChans> channels;
84 
85  template<class Archive>
86  void serialize(Archive& ar)
87  {
88  ar( channels );
89  }
90 
91  protected:
92  //! Default Constructor
93  PixelBase();
94 
95  //! Uniform Constructor
96  template <typename T1, typename = typename std::enable_if<std::is_arithmetic<T1>::value>::type>
97  explicit PixelBase(T1 const & val);
98 
99  //! Copy Constructor
100  PixelBase(PixelBase<T, numChans, PixType> const & other) = default;
101 
102  //! Conversion Copy Constructor
103  /*! Converts all elements using clamped_convert */
104  template<class T2>
105  PixelBase(PixelBase<T2, numChans, PixType> const & other);
106 
107  //! construct from a single channel pixel
108  template <class T1, template<typename> class PixType1>
109  explicit PixelBase(PixelBase<T1,1,PixType1> const & other);
110 
111  //! Colorspace Conversion Copy Constructor
112  /*Don't allow implicit conversion because 1) avoid unintended
113  expensive copies and 2) we don't want an automatic promotion to other
114  pixel types in the operators */
115  template<template<typename ...> class PixType2, typename ...PixType2Args,
116  typename = typename std::enable_if<pixel_traits<PixType2<PixType2Args...>>::is_multi_channel>::type
117  >
118  explicit PixelBase(PixType2<PixType2Args...> const & other);
119  };
122 }
123 
124 #endif // INCLUDE_NRT_CORE_IMAGE_PIXELBASE_H
125