Boost GIL


virtual_locator.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_VIRTUAL_LOCATOR_HPP
9 #define BOOST_GIL_VIRTUAL_LOCATOR_HPP
10 
11 #include <boost/gil/position_iterator.hpp>
12 
13 #include <boost/iterator/iterator_facade.hpp>
14 
15 namespace boost { namespace gil {
16 
20 template <typename Deref, bool IsTransposed> // A function object that given a point returns a reference. Models PixelDereferenceAdaptorConcept
21 class virtual_2d_locator : public pixel_2d_locator_base<virtual_2d_locator<Deref,IsTransposed>, position_iterator<Deref,IsTransposed>, position_iterator<Deref,1-IsTransposed> > {
23 public:
26 
27  typedef Deref deref_fn_t;
28  typedef typename parent_t::point_t point_t;
29 
30  typedef typename parent_t::coord_t coord_t;
31  typedef typename parent_t::x_coord_t x_coord_t;
32  typedef typename parent_t::y_coord_t y_coord_t;
33  typedef typename parent_t::x_iterator x_iterator;
34  typedef typename parent_t::y_iterator y_iterator;
35 
36  template <typename NewDeref> struct add_deref {
37  typedef virtual_2d_locator<deref_compose<NewDeref,Deref>,IsTransposed > type;
38  static type make(const virtual_2d_locator<Deref,IsTransposed>& loc, const NewDeref& nderef) {
39  return type(loc.pos(), loc.step(), deref_compose<NewDeref,Deref>(nderef,loc.deref_fn()));
40  }
41  };
42 
43  virtual_2d_locator(const point_t& p=point_t(0,0), const point_t& step=point_t(1,1), const deref_fn_t& d=deref_fn_t()) : _p(p,step,d) {}
44  template <typename D, bool TR> virtual_2d_locator(const virtual_2d_locator<D,TR>& loc, coord_t y_step)
45  : _p(loc.pos(), point_t(loc.step().x,loc.step().y*y_step), loc.deref_fn()) {}
46  template <typename D, bool TR> virtual_2d_locator(const virtual_2d_locator<D,TR>& loc, coord_t x_step, coord_t y_step, bool transpose=false)
47  : _p(loc.pos(), transpose ?
48  point_t(loc.step().x*y_step,loc.step().y*x_step) :
49  point_t(loc.step().x*x_step,loc.step().y*y_step), loc.deref_fn()) { assert(transpose==(IsTransposed!=TR));}
50 
51  template <typename D, bool TR> virtual_2d_locator(const virtual_2d_locator<D,TR>& pl) : _p(pl._p) {}
52  virtual_2d_locator(const virtual_2d_locator& pl) : _p(pl._p) {}
53 
54  bool operator==(const this_t& p) const { return _p==p._p; }
55 
56  x_iterator& x() { return *gil_reinterpret_cast<x_iterator*>(this); }
57  y_iterator& y() { return _p; }
58  x_iterator const& x() const { return *gil_reinterpret_cast_c<x_iterator const*>(this); }
59  y_iterator const& y() const { return _p; }
60 
61  // Returns the y distance between two x_iterators given the difference of their x positions
62  y_coord_t y_distance_to(const this_t& it2, x_coord_t) const { return (it2.pos()[1-IsTransposed] - pos()[1-IsTransposed])/step()[1-IsTransposed]; }
63  bool is_1d_traversable(x_coord_t) const { return false; } // is there no gap at the end of each row? I.e. can we use x_iterator to visit every pixel instead of nested loops?
64 
65  // Methods specific for virtual 2D locator
66  const point_t& pos() const { return _p.pos(); }
67  const point_t& step() const { return _p.step(); }
68  const deref_fn_t& deref_fn() const { return _p.deref_fn(); }
69 private:
70  template <typename D, bool TR> friend class virtual_2d_locator;
71  y_iterator _p; // contains the current position, the step and the dereference object
72 };
73 
75 // PixelBasedConcept
77 
78 template <typename D, bool TR>
79 struct channel_type<virtual_2d_locator<D,TR> > : public channel_type<typename virtual_2d_locator<D,TR>::parent_t> {
80 };
81 
82 template <typename D, bool TR>
83 struct color_space_type<virtual_2d_locator<D,TR> > : public color_space_type<typename virtual_2d_locator<D,TR>::parent_t> {
84 };
85 
86 template <typename D, bool TR>
87 struct channel_mapping_type<virtual_2d_locator<D,TR> > : public channel_mapping_type<typename virtual_2d_locator<D,TR>::parent_t> {
88 };
89 
90 template <typename D, bool TR>
91 struct is_planar<virtual_2d_locator<D,TR> > : public is_planar<typename virtual_2d_locator<D,TR>::parent_t> {
92 };
93 
95 // HasDynamicXStepTypeConcept
97 
98 template <typename D, bool TR>
99 struct dynamic_x_step_type<virtual_2d_locator<D,TR> > {
100  typedef virtual_2d_locator<D,TR> type;
101 };
102 
104 // HasDynamicYStepTypeConcept
106 
107 template <typename D, bool TR>
108 struct dynamic_y_step_type<virtual_2d_locator<D,TR> > {
109  typedef virtual_2d_locator<D,TR> type;
110 };
111 
113 // HasTransposedTypeConcept
115 
116 template <typename D, bool IsTransposed>
117 struct transposed_type<virtual_2d_locator<D,IsTransposed> > {
118  typedef virtual_2d_locator<D,1-IsTransposed> type;
119 };
120 
121 }} // namespace boost::gil
122 
123 #endif
base class for models of PixelLocatorConceptPixel locator is similar to a pixel iterator, but allows for 2D navigation of pixels within an image view. It has a 2D difference_type and supports random access operations like:
Definition: locator.hpp:105
An iterator that remembers its current X,Y position and invokes a function object with it upon derefe...
Definition: position_iterator.hpp:27
A 2D locator over a virtual image. Upon dereferencing, invokes a given function object passing it its...
Definition: virtual_locator.hpp:21
Composes two dereference function objects. Similar to std::unary_compose but needs to pull some typed...
Definition: utilities.hpp:105
Definition: color_convert.hpp:30
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: concepts.hpp:42