Boost GIL


dynamic_at_c.hpp
1 //
2 // Copyright 2005-2007 Adobe Systems Incorporated
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_DYNAMIC_AT_C_HPP
9 #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_DYNAMIC_AT_C_HPP
10 
11 #include <boost/mpl/at.hpp>
12 #include <boost/mpl/size.hpp>
13 
14 #include <cassert>
15 #include <stdexcept>
16 
17 namespace boost { namespace gil {
18 
19 // Constructs for static-to-dynamic integer convesion
20 
21 #define GIL_AT_C_VALUE(z, N, text) mpl::at_c<IntTypes,S+N>::type::value,
22 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
23 
24 #define GIL_AT_C_LOOKUP(z, NUM, text) \
25  template<std::size_t S> \
26  struct at_c_fn<S,NUM> { \
27  template <typename IntTypes, typename ValueType> inline \
28  static ValueType apply(std::size_t index) { \
29  static ValueType table[] = { \
30  BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
31  }; \
32  return table[index]; \
33  } \
34  };
35 
36 namespace detail {
37  namespace at_c {
38  template <std::size_t START, std::size_t NUM> struct at_c_fn;
39  BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
40 
41  template <std::size_t QUOT> struct at_c_impl;
42 
43  template <>
44  struct at_c_impl<0> {
45  template <typename IntTypes, typename ValueType> inline
46  static ValueType apply(std::size_t index) {
47  return at_c_fn<0,mpl::size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
48  }
49  };
50 
51  template <>
52  struct at_c_impl<1> {
53  template <typename IntTypes, typename ValueType> inline
54  static ValueType apply(std::size_t index) {
55  const std::size_t SIZE=mpl::size<IntTypes>::value;
56  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
57  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
58  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
59  case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
60  };
61  throw;
62  }
63  };
64 
65  template <>
66  struct at_c_impl<2> {
67  template <typename IntTypes, typename ValueType> inline
68  static ValueType apply(std::size_t index) {
69  const std::size_t SIZE=mpl::size<IntTypes>::value;
70  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
71  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
72  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
73  case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
74  case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
75  };
76  throw;
77  }
78  };
79 
80  template <>
81  struct at_c_impl<3> {
82  template <typename IntTypes, typename ValueType> inline
83  static ValueType apply(std::size_t index) {
84  const std::size_t SIZE=mpl::size<IntTypes>::value;
85  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
86  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
87  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
88  case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
89  case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
90  case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
91  };
92  throw;
93  }
94  };
95  }
96 }
97 
104 
105 template <typename IntTypes, typename ValueType> inline
106 ValueType at_c(std::size_t index) {
107  const std::size_t Size=mpl::size<IntTypes>::value;
108  return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
109 }
110 
111 #undef GIL_AT_C_VALUE
112 #undef GIL_DYNAMIC_AT_C_LIMIT
113 #undef GIL_AT_C_LOOKUP
114 
115 }} // namespace boost::gil
116 
117 #endif
add_reference< E >::type at_c(detail::homogeneous_color_base< E, L, N > &p)
Provides mutable access to the K-th element, in physical order.
Definition: color_base.hpp:381