iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GenericImageMacros.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
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_GENERICIMAGEMACROS_H
37 #define INCLUDE_NRT_CORE_IMAGE_GENERICIMAGEMACROS_H
38 
39 #include <nrt/Core/Typing/Macros.H>
40 
41 /*! \defgroup genericimagemacro GenericImage Macros
42 
43  Macros designed to facilitate the use of GenericImage and the design of generic code.
44 
45  \ingroup genericimage
46  \see genericimage */
47 
48 /*! @{ */ // **********************************************************************
49 
50 /*! \def NRT_GENERICIMAGE_PIXTYPE_WARN(genimage, PixType)
51  \hideinitializer
52 
53  Issue a warning if an nrt::GenericImage does not contain the specified nrt::Image<PixType>,
54  i.e., an expensive pixel-by-pixel conversion will be necessary to convert the GenericImage to Image<PixType> */
55 #define NRT_GENERICIMAGE_PIXTYPE_WARN(genimage, PixType) \
56  if (genimage.hasPixels<PixType>() == false) \
57  NRT_WARNING("Pixel-by-pixel conversion necessary from " << genimage.typeStr() << " to " << \
58  NRT_MACRO_TOSTRING(PixType) << " at " << NRT_MACRO_WHERE);
59 
60 /*! \def NRT_DECLARE_GENERICIMAGE_UNARY_FUNCTION(func)
61  \hideinitializer
62 
63  Declare the GenericImage version of a function that takes one Image and returns an Image. Standard promotion syntax
64  applies, so if your original function looks like:
65 
66  @code
67  template <typename promo, class PixType, typename DestType = typename promote<PixType, promo>::type>
68  Image<DestType> func(Image<PixType> const & src);
69  @endcode
70 
71  and you then write:
72 
73  @code
74  NRT_DECLARE_GENERICIMAGE_UNARY_FUNCTION(func);
75  @endcode
76 
77  you will essentially have written:
78 
79  @code
80  template <typename promo>
81  nrt::GenericImage func(GenericImage const & src);
82  @endcode
83 
84  You can then use #NRT_DEFINE_GENERICIMAGE_UNARY_FUNCTION(func) to actually define your function as a GenericImage
85  unary visitor. */
86 #define NRT_DECLARE_GENERICIMAGE_UNARY_FUNCTION(func) \
87  template <typename promo> \
88  nrt::GenericImage func(nrt::GenericImage const & src)
89 
90 /*! \def NRT_DEFINE_GENERICIMAGE_UNARY_FUNCTION(func)
91  \hideinitializer
92  Define the GenericImage version of a function that takes one Image and returns an Image. Use this in the same
93  namespace as you used #NRT_DECLARE_GENERICIMAGE_UNARY_FUNCTION(func). This defines a visitor and defines the body of
94  a function previously declared with #NRT_DECLARE_GENERICIMAGE_UNARY_FUNCTION(func). The visitor is applied to the
95  source GenericImage using the apply_visitor() function of GenericImage. Inside the visitor, the template operator()
96  function gets called during visitation, with as PixType the pixel type of the Image actually contained in the
97  GenericImage, and in there we call your original function on the passed Image<PixType>. Your original function then
98  returns an Image<DestType>, which we stuff into a GenericImage and return. For this to work, of course both PixType
99  and DestType should be listed as valid types in the boost::variant definition inside GenericImage; so if you are
100  writing a function that you will typically use with "exotic" pixel types, either don't attempt to write a
101  GenericImage version of it at all, or negotiate with the NRT developer team to have your exotic pixel types added to
102  the internals of GenericImage. */
103 #define NRT_DEFINE_GENERICIMAGE_UNARY_FUNCTION(func) \
104  namespace \
105  { \
106  template <typename promo> \
107  struct func_genericimage_unary_visitor : public boost::static_visitor<nrt::GenericImage> \
108  { \
109  template <class PixType> inline \
110  nrt::GenericImage operator()(nrt::Image<PixType> const & im) const \
111  { return nrt::GenericImage(func<promo>(im)); } \
112  }; \
113  } \
114  \
115  template <typename promo> inline \
116  nrt::GenericImage func(nrt::GenericImage const & src) \
117  { return src.apply_visitor(func_genericimage_unary_visitor<promo>()); }
118 
119 /*! @} */ // **********************************************************************
120 
121 
122 #endif // INCLUDE_NRT_CORE_IMAGE_GENERICIMAGEMACROS_H
123