13 #ifndef GIL_UTILITIES_H
14 #define GIL_UTILITIES_H
18 #include <boost/config/no_tr1/cmath.hpp>
23 #include <boost/static_assert.hpp>
24 #include <boost/type_traits.hpp>
25 #include <boost/mpl/size.hpp>
26 #include <boost/mpl/distance.hpp>
27 #include <boost/mpl/begin.hpp>
28 #include <boost/mpl/find.hpp>
29 #include <boost/mpl/range_c.hpp>
30 #include <boost/iterator/iterator_adaptor.hpp>
31 #include <boost/iterator/iterator_facade.hpp>
43 namespace boost {
namespace gil {
70 template <std::
size_t D>
struct axis {
typedef value_type coord_t; };
71 static const std::size_t num_dimensions=2;
73 point2() : x(0), y(0) {}
74 point2(T newX, T newY) : x(newX), y(newY) {}
75 point2(
const point2& p) : x(p.x), y(p.y) {}
78 point2& operator=(
const point2& p) { x=p.x; y=p.y;
return *
this; }
80 point2 operator<<(std::ptrdiff_t shift)
const {
return point2(x<<shift,y<<shift); }
81 point2 operator>>(std::ptrdiff_t shift)
const {
return point2(x>>shift,y>>shift); }
82 point2& operator+=(
const point2& p) { x+=p.x; y+=p.y;
return *
this; }
83 point2& operator-=(
const point2& p) { x-=p.x; y-=p.y;
return *
this; }
84 point2& operator/=(
double t) {
if (t<0 || 0<t) { x/=t; y/=t; }
return *
this; }
86 const T& operator[](std::size_t i)
const {
return this->*mem_array[i]; }
87 T& operator[](std::size_t i) {
return this->*mem_array[i]; }
92 static T point2<T>::*
const mem_array[num_dimensions];
96 T point2<T>::*
const point2<T>::mem_array[point2<T>::num_dimensions] = { &point2<T>::x, &point2<T>::y };
99 template <
typename T> BOOST_FORCEINLINE
100 bool operator==(
const point2<T>& p1,
const point2<T>& p2) {
return (p1.x==p2.x && p1.y==p2.y); }
102 template <
typename T> BOOST_FORCEINLINE
103 bool operator!=(
const point2<T>& p1,
const point2<T>& p2) {
return p1.x!=p2.x || p1.y!=p2.y; }
105 template <
typename T> BOOST_FORCEINLINE
106 point2<T> operator+(
const point2<T>& p1,
const point2<T>& p2) {
return point2<T>(p1.x+p2.x,p1.y+p2.y); }
108 template <
typename T> BOOST_FORCEINLINE
109 point2<T> operator-(
const point2<T>& p) {
return point2<T>(-p.x,-p.y); }
111 template <
typename T> BOOST_FORCEINLINE
112 point2<T> operator-(
const point2<T>& p1,
const point2<T>& p2) {
return point2<T>(p1.x-p2.x,p1.y-p2.y); }
114 template <
typename T> BOOST_FORCEINLINE
115 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); }
117 template <
typename T> BOOST_FORCEINLINE
118 point2<T> operator*(
const point2<T>& p, std::ptrdiff_t t) {
return point2<T>(p.x*t,p.y*t); }
120 template <
typename T> BOOST_FORCEINLINE
121 point2<T> operator*(std::ptrdiff_t t,
const point2<T>& p) {
return point2<T>(p.x*t,p.y*t); }
124 template <std::
size_t K,
typename T> BOOST_FORCEINLINE
125 const T& axis_value(
const point2<T>& p) {
return p[K]; }
128 template <std::
size_t K,
typename T> BOOST_FORCEINLINE
129 T& axis_value( point2<T>& p) {
return p[K]; }
137 inline std::ptrdiff_t iround(
float x ) {
return static_cast<std::ptrdiff_t
>(x + (x < 0.0f ? -0.5f : 0.5f)); }
138 inline std::ptrdiff_t iround(
double x) {
return static_cast<std::ptrdiff_t
>(x + (x < 0.0 ? -0.5 : 0.5)); }
139 inline std::ptrdiff_t ifloor(
float x ) {
return static_cast<std::ptrdiff_t
>(std::floor(x)); }
140 inline std::ptrdiff_t ifloor(
double x) {
return static_cast<std::ptrdiff_t
>(std::floor(x)); }
141 inline std::ptrdiff_t iceil(
float x ) {
return static_cast<std::ptrdiff_t
>(std::ceil(x)); }
142 inline std::ptrdiff_t iceil(
double x) {
return static_cast<std::ptrdiff_t
>(std::ceil(x)); }
153 inline point2<std::ptrdiff_t> iround(
const point2<float >& p) {
return point2<std::ptrdiff_t>(iround(p.x),iround(p.y)); }
156 inline point2<std::ptrdiff_t> iround(
const point2<double>& p) {
return point2<std::ptrdiff_t>(iround(p.x),iround(p.y)); }
158 inline point2<std::ptrdiff_t> ifloor(
const point2<float >& p) {
return point2<std::ptrdiff_t>(ifloor(p.x),ifloor(p.y)); }
160 inline point2<std::ptrdiff_t> ifloor(
const point2<double>& p) {
return point2<std::ptrdiff_t>(ifloor(p.x),ifloor(p.y)); }
162 inline point2<std::ptrdiff_t> iceil (
const point2<float >& p) {
return point2<std::ptrdiff_t>(iceil(p.x), iceil(p.y)); }
164 inline point2<std::ptrdiff_t> iceil (
const point2<double>& p) {
return point2<std::ptrdiff_t>(iceil(p.x), iceil(p.y)); }
172 template <
typename T>
173 inline T align(T val, std::size_t alignment) {
174 return val+(alignment - val%alignment)%alignment;
180 template <
typename ConstT,
typename Value,
typename Reference,
typename ConstReference,
181 typename ArgType,
typename ResultType,
bool IsMutable>
183 typedef ArgType argument_type;
184 typedef ResultType result_type;
185 typedef ConstT const_t;
186 typedef Value value_type;
187 typedef Reference reference;
188 typedef ConstReference const_reference;
189 BOOST_STATIC_CONSTANT(
bool, is_mutable = IsMutable);
195 template <
typename D1,
typename D2>
197 deref_compose<typename D1::const_t, typename D2::const_t>,
198 typename D1::value_type, typename D1::reference, typename D1::const_reference,
199 typename D2::argument_type, typename D1::result_type, D1::is_mutable && D2::is_mutable>
205 typedef typename D2::argument_type argument_type;
206 typedef typename D1::result_type result_type;
209 deref_compose(
const D1& x,
const D2& y) : _fn1(x), _fn2(y) {}
213 result_type operator()(argument_type x)
const {
return _fn1(_fn2(x)); }
214 result_type operator()(argument_type x) {
return _fn1(_fn2(x)); }
218 template <
typename OutPtr,
typename In> BOOST_FORCEINLINE
219 OutPtr gil_reinterpret_cast( In* p) {
return static_cast<OutPtr
>(
static_cast<void*
>(p)); }
221 template <
typename OutPtr,
typename In> BOOST_FORCEINLINE
222 const OutPtr gil_reinterpret_cast_c(
const In* p) {
return static_cast<const OutPtr
>(
static_cast<const void*
>(p)); }
232 template <
class InputIter,
class Size,
class OutputIter>
233 std::pair<InputIter, OutputIter> _copy_n(InputIter first, Size count,
235 std::input_iterator_tag) {
236 for ( ; count > 0; --count) {
241 return std::pair<InputIter, OutputIter>(first, result);
244 template <
class RAIter,
class Size,
class OutputIter>
245 inline std::pair<RAIter, OutputIter>
246 _copy_n(RAIter first, Size count, OutputIter result, std::random_access_iterator_tag) {
247 RAIter last = first + count;
248 return std::pair<RAIter, OutputIter>(last,
std::copy(first, last, result));
251 template <
class InputIter,
class Size,
class OutputIter>
252 inline std::pair<InputIter, OutputIter>
253 _copy_n(InputIter first, Size count, OutputIter result) {
254 return _copy_n(first, count, result,
typename std::iterator_traits<InputIter>::iterator_category());
257 template <
class InputIter,
class Size,
class OutputIter>
258 inline std::pair<InputIter, OutputIter>
259 copy_n(InputIter first, Size count, OutputIter result) {
260 return detail::_copy_n(first, count, result);
264 template <
typename T>
266 typedef T argument_type;
267 typedef T result_type;
268 const T& operator()(
const T& val)
const {
return val; }
274 template <
typename T1,
typename T2>
276 typedef T1 first_argument_type;
277 typedef T2 second_argument_type;
278 typedef T1 result_type;
279 T1 operator()(T1 f1, T2 f2)
const {
287 template <
typename T>
289 typedef T argument_type;
290 typedef T result_type;
291 T operator()(T x)
const {
return ++x; }
297 template <
typename T>
299 typedef T argument_type;
300 typedef T result_type;
301 T operator()(T x)
const {
return --x; }
306 template <
typename Types,
typename T>
308 :
public mpl::distance<typename mpl::begin<Types>::type,
309 typename mpl::find<Types,T>::type>::type {};
316 template <typename ColorSpace, typename ChannelMapping = mpl::range_c<int,0,mpl::size<ColorSpace>::value> >
318 typedef ColorSpace color_space_t;
319 typedef ChannelMapping channel_mapping_t;
323 template <
typename Value,
typename T1,
typename T2>
324 void swap_proxy(T1& left, T2& right) {
331 inline bool little_endian() {
332 short tester = 0x0001;
333 return *(
char*)&tester!=0;
336 inline bool big_endian() {
337 return !little_endian();
plus function object whose arguments may be of different type.
Definition: utilities.hpp:275
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:151
Helper base class for pixel dereference adaptors.
Definition: utilities.hpp:182
operator– wrapped in a function object
Definition: utilities.hpp:298
identity taken from SGI STL.
Definition: utilities.hpp:265
operator++ wrapped in a function object
Definition: utilities.hpp:288
Represents a color space and ordering of channels in memory.
Definition: utilities.hpp:317
Composes two dereference function objects. Similar to std::unary_compose but needs to pull some typed...
Definition: utilities.hpp:196
Returns the index corresponding to the first occurrance of a given given type in. ...
Definition: utilities.hpp:307