Boost GIL


utilities.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
4  Use, modification and distribution are subject to the Boost Software License,
5  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6  http://www.boost.org/LICENSE_1_0.txt).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_UTILITIES_H
14 #define GIL_UTILITIES_H
15 
16 #include <functional>
17 #include <boost/config/no_tr1/cmath.hpp>
18 #include <cstddef>
19 #include <algorithm>
20 #include <utility>
21 #include <iterator>
22 #include <boost/static_assert.hpp>
23 #include <boost/type_traits.hpp>
24 #include <boost/mpl/size.hpp>
25 #include <boost/mpl/distance.hpp>
26 #include <boost/mpl/begin.hpp>
27 #include <boost/mpl/find.hpp>
28 #include <boost/mpl/range_c.hpp>
29 #include <boost/iterator/iterator_adaptor.hpp>
30 #include <boost/iterator/iterator_facade.hpp>
31 
41 
42 namespace boost { namespace gil {
43 
56 // CLASS point2
64 
65 template <typename T>
66 class point2 {
67 public:
68  typedef T value_type;
69  template <std::size_t D> struct axis { typedef value_type coord_t; };
70  static const std::size_t num_dimensions=2;
71 
72  point2() : x(0), y(0) {}
73  point2(T newX, T newY) : x(newX), y(newY) {}
74  point2(const point2& p) : x(p.x), y(p.y) {}
75  ~point2() {}
76 
77  point2& operator=(const point2& p) { x=p.x; y=p.y; return *this; }
78 
79  point2 operator<<(std::ptrdiff_t shift) const { return point2(x<<shift,y<<shift); }
80  point2 operator>>(std::ptrdiff_t shift) const { return point2(x>>shift,y>>shift); }
81  point2& operator+=(const point2& p) { x+=p.x; y+=p.y; return *this; }
82  point2& operator-=(const point2& p) { x-=p.x; y-=p.y; return *this; }
83  point2& operator/=(double t) { if (t<0 || 0<t) { x/=t; y/=t; } return *this; }
84 
85  const T& operator[](std::size_t i) const { return this->*mem_array[i]; }
86  T& operator[](std::size_t i) { return this->*mem_array[i]; }
87 
88  T x,y;
89 private:
90  // this static array of pointers to member variables makes operator[] safe and doesn't seem to exhibit any performance penalty
91  static T point2<T>::* const mem_array[num_dimensions];
92 };
93 
94 template <typename T>
95 T point2<T>::* const point2<T>::mem_array[point2<T>::num_dimensions] = { &point2<T>::x, &point2<T>::y };
96 
98 template <typename T> BOOST_FORCEINLINE
99 bool operator==(const point2<T>& p1, const point2<T>& p2) { return (p1.x==p2.x && p1.y==p2.y); }
101 template <typename T> BOOST_FORCEINLINE
102 bool operator!=(const point2<T>& p1, const point2<T>& p2) { return p1.x!=p2.x || p1.y!=p2.y; }
104 template <typename T> BOOST_FORCEINLINE
105 point2<T> operator+(const point2<T>& p1, const point2<T>& p2) { return point2<T>(p1.x+p2.x,p1.y+p2.y); }
107 template <typename T> BOOST_FORCEINLINE
108 point2<T> operator-(const point2<T>& p) { return point2<T>(-p.x,-p.y); }
110 template <typename T> BOOST_FORCEINLINE
111 point2<T> operator-(const point2<T>& p1, const point2<T>& p2) { return point2<T>(p1.x-p2.x,p1.y-p2.y); }
113 template <typename T> BOOST_FORCEINLINE
114 point2<double> operator/(const point2<T>& p, double t) { return (t<0 || 0<t) ? point2<double>(p.x/t,p.y/t) : point2<double>(0,0); }
116 template <typename T> BOOST_FORCEINLINE
117 point2<T> operator*(const point2<T>& p, std::ptrdiff_t t) { return point2<T>(p.x*t,p.y*t); }
119 template <typename T> BOOST_FORCEINLINE
120 point2<T> operator*(std::ptrdiff_t t, const point2<T>& p) { return point2<T>(p.x*t,p.y*t); }
121 
123 template <std::size_t K, typename T> BOOST_FORCEINLINE
124 const T& axis_value(const point2<T>& p) { return p[K]; }
125 
127 template <std::size_t K, typename T> BOOST_FORCEINLINE
128  T& axis_value( point2<T>& p) { return p[K]; }
129 
135 
136 inline std::ptrdiff_t iround(float x ) { return static_cast<std::ptrdiff_t>(x + (x < 0.0f ? -0.5f : 0.5f)); }
137 inline std::ptrdiff_t iround(double x) { return static_cast<std::ptrdiff_t>(x + (x < 0.0 ? -0.5 : 0.5)); }
138 inline std::ptrdiff_t ifloor(float x ) { return static_cast<std::ptrdiff_t>(std::floor(x)); }
139 inline std::ptrdiff_t ifloor(double x) { return static_cast<std::ptrdiff_t>(std::floor(x)); }
140 inline std::ptrdiff_t iceil(float x ) { return static_cast<std::ptrdiff_t>(std::ceil(x)); }
141 inline std::ptrdiff_t iceil(double x) { return static_cast<std::ptrdiff_t>(std::ceil(x)); }
142 
152 inline point2<std::ptrdiff_t> iround(const point2<float >& p) { return point2<std::ptrdiff_t>(iround(p.x),iround(p.y)); }
155 inline point2<std::ptrdiff_t> iround(const point2<double>& p) { return point2<std::ptrdiff_t>(iround(p.x),iround(p.y)); }
157 inline point2<std::ptrdiff_t> ifloor(const point2<float >& p) { return point2<std::ptrdiff_t>(ifloor(p.x),ifloor(p.y)); }
159 inline point2<std::ptrdiff_t> ifloor(const point2<double>& p) { return point2<std::ptrdiff_t>(ifloor(p.x),ifloor(p.y)); }
161 inline point2<std::ptrdiff_t> iceil (const point2<float >& p) { return point2<std::ptrdiff_t>(iceil(p.x), iceil(p.y)); }
163 inline point2<std::ptrdiff_t> iceil (const point2<double>& p) { return point2<std::ptrdiff_t>(iceil(p.x), iceil(p.y)); }
164 
170 
171 template <typename T>
172 inline T align(T val, std::size_t alignment) {
173  return val+(alignment - val%alignment)%alignment;
174 }
175 
179 template <typename ConstT, typename Value, typename Reference, typename ConstReference,
180  typename ArgType, typename ResultType, bool IsMutable>
181 struct deref_base {
182  typedef ArgType argument_type;
183  typedef ResultType result_type;
184  typedef ConstT const_t;
185  typedef Value value_type;
186  typedef Reference reference;
187  typedef ConstReference const_reference;
188  BOOST_STATIC_CONSTANT(bool, is_mutable = IsMutable);
189 };
190 
194 template <typename D1, typename D2>
195 class deref_compose : public deref_base<
196  deref_compose<typename D1::const_t, typename D2::const_t>,
197  typename D1::value_type, typename D1::reference, typename D1::const_reference,
198  typename D2::argument_type, typename D1::result_type, D1::is_mutable && D2::is_mutable>
199 {
200 public:
201  D1 _fn1;
202  D2 _fn2;
203 
204  typedef typename D2::argument_type argument_type;
205  typedef typename D1::result_type result_type;
206 
207  deref_compose() {}
208  deref_compose(const D1& x, const D2& y) : _fn1(x), _fn2(y) {}
209  deref_compose(const deref_compose& dc) : _fn1(dc._fn1), _fn2(dc._fn2) {}
210  template <typename _D1, typename _D2> deref_compose(const deref_compose<_D1,_D2>& dc) : _fn1(dc._fn1), _fn2(dc._fn2) {}
211 
212  result_type operator()(argument_type x) const { return _fn1(_fn2(x)); }
213  result_type operator()(argument_type x) { return _fn1(_fn2(x)); }
214 };
215 
216 // reinterpret_cast is implementation-defined. Static cast is not.
217 template <typename OutPtr, typename In> BOOST_FORCEINLINE
218  OutPtr gil_reinterpret_cast( In* p) { return static_cast<OutPtr>(static_cast<void*>(p)); }
219 
220 template <typename OutPtr, typename In> BOOST_FORCEINLINE
221 const OutPtr gil_reinterpret_cast_c(const In* p) { return static_cast<const OutPtr>(static_cast<const void*>(p)); }
222 
223 namespace detail {
224 
230 
231 template <class InputIter, class Size, class OutputIter>
232 std::pair<InputIter, OutputIter> _copy_n(InputIter first, Size count,
233  OutputIter result,
234  std::input_iterator_tag) {
235  for ( ; count > 0; --count) {
236  *result = *first;
237  ++first;
238  ++result;
239  }
240  return std::pair<InputIter, OutputIter>(first, result);
241 }
242 
243 template <class RAIter, class Size, class OutputIter>
244 inline std::pair<RAIter, OutputIter>
245 _copy_n(RAIter first, Size count, OutputIter result, std::random_access_iterator_tag) {
246  RAIter last = first + count;
247  return std::pair<RAIter, OutputIter>(last, std::copy(first, last, result));
248 }
249 
250 template <class InputIter, class Size, class OutputIter>
251 inline std::pair<InputIter, OutputIter>
252 _copy_n(InputIter first, Size count, OutputIter result) {
253  return _copy_n(first, count, result, typename std::iterator_traits<InputIter>::iterator_category());
254 }
255 
256 template <class InputIter, class Size, class OutputIter>
257 inline std::pair<InputIter, OutputIter>
258 copy_n(InputIter first, Size count, OutputIter result) {
259  return detail::_copy_n(first, count, result);
260 }
261 
263 template <typename T>
264 struct identity {
265  typedef T argument_type;
266  typedef T result_type;
267  const T& operator()(const T& val) const { return val; }
268 };
269 
270 /*************************************************************************************************/
271 
273 template <typename T1, typename T2>
275  typedef T1 first_argument_type;
276  typedef T2 second_argument_type;
277  typedef T1 result_type;
278  T1 operator()(T1 f1, T2 f2) const {
279  return f1+f2;
280  }
281 };
282 
283 /*************************************************************************************************/
284 
286 template <typename T>
287 struct inc {
288  typedef T argument_type;
289  typedef T result_type;
290  T operator()(T x) const { return ++x; }
291 };
292 
293 /*************************************************************************************************/
294 
296 template <typename T>
297 struct dec {
298  typedef T argument_type;
299  typedef T result_type;
300  T operator()(T x) const { return --x; }
301 };
302 
304 // a given MPL RandomAccessSequence (or size if the type is not present)
305 template <typename Types, typename T>
307  : public mpl::distance<typename mpl::begin<Types>::type,
308  typename mpl::find<Types,T>::type>::type {};
309 } // namespace detail
310 
311 
312 
315 template <typename ColorSpace, typename ChannelMapping = mpl::range_c<int,0,mpl::size<ColorSpace>::value> >
316 struct layout {
317  typedef ColorSpace color_space_t;
318  typedef ChannelMapping channel_mapping_t;
319 };
320 
322 template <typename Value, typename T1, typename T2> // where value_type<T1> == value_type<T2> == Value
323 void swap_proxy(T1& left, T2& right) {
324  Value tmp = left;
325  left = right;
326  right = tmp;
327 }
328 
330 inline bool little_endian() {
331  short tester = 0x0001;
332  return *(char*)&tester!=0;
333 }
335 inline bool big_endian() {
336  return !little_endian();
337 }
338 
339 } } // namespace boost::gil
340 
341 #endif
plus function object whose arguments may be of different type.
Definition: utilities.hpp:274
BOOST_FORCEINLINE boost::gil::pixel< T, Cs > * copy(boost::gil::pixel< T, Cs > *first, boost::gil::pixel< T, Cs > *last, boost::gil::pixel< T, Cs > *dst)
Copy when both src and dst are interleaved and of the same type can be just memmove.
Definition: algorithm.hpp:150
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:181
operator– wrapped in a function object
Definition: utilities.hpp:297
identity taken from SGI STL.
Definition: utilities.hpp:264
operator++ wrapped in a function object
Definition: utilities.hpp:287
Represents a color space and ordering of channels in memory.
Definition: utilities.hpp:316
Composes two dereference function objects. Similar to std::unary_compose but needs to pull some typed...
Definition: utilities.hpp:195
Returns the index corresponding to the first occurrance of a given given type in. ...
Definition: utilities.hpp:306