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 <stdexcept>
15 
16 namespace boost { namespace gil {
17 
18 // Constructs for static-to-dynamic integer convesion
19 
20 #define GIL_AT_C_VALUE(z, N, text) mpl::at_c<IntTypes,S+N>::type::value,
21 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
22 
23 #define GIL_AT_C_LOOKUP(z, NUM, text) \
24  template<std::size_t S> \
25  struct at_c_fn<S,NUM> { \
26  template <typename IntTypes, typename ValueType> inline \
27  static ValueType apply(std::size_t index) { \
28  static ValueType table[] = { \
29  BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
30  }; \
31  return table[index]; \
32  } \
33  };
34 
35 namespace detail {
36  namespace at_c {
37  template <std::size_t START, std::size_t NUM> struct at_c_fn;
38  BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
39 
40  template <std::size_t QUOT> struct at_c_impl;
41 
42  template <>
43  struct at_c_impl<0> {
44  template <typename IntTypes, typename ValueType> inline
45  static ValueType apply(std::size_t index) {
46  return at_c_fn<0,mpl::size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
47  }
48  };
49 
50  template <>
51  struct at_c_impl<1> {
52  template <typename IntTypes, typename ValueType> inline
53  static ValueType apply(std::size_t index) {
54  const std::size_t SIZE=mpl::size<IntTypes>::value;
55  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
56  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
57  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
58  case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
59  };
60  throw;
61  }
62  };
63 
64  template <>
65  struct at_c_impl<2> {
66  template <typename IntTypes, typename ValueType> inline
67  static ValueType apply(std::size_t index) {
68  const std::size_t SIZE=mpl::size<IntTypes>::value;
69  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
70  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
71  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
72  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);
73  case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
74  };
75  throw;
76  }
77  };
78 
79  template <>
80  struct at_c_impl<3> {
81  template <typename IntTypes, typename ValueType> inline
82  static ValueType apply(std::size_t index) {
83  const std::size_t SIZE=mpl::size<IntTypes>::value;
84  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
85  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
86  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
87  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);
88  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);
89  case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
90  };
91  throw;
92  }
93  };
94  }
95 }
96 
103 
104 template <typename IntTypes, typename ValueType> inline
105 ValueType at_c(std::size_t index) {
106  const std::size_t Size=mpl::size<IntTypes>::value;
107  return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
108 }
109 
110 #undef GIL_AT_C_VALUE
111 #undef GIL_DYNAMIC_AT_C_LIMIT
112 #undef GIL_AT_C_LOOKUP
113 
114 }} // namespace boost::gil
115 
116 #endif
Definition: algorithm.hpp:30
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:387