Boost GIL


virtual_locator.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_VIRTUAL_LOCATOR_HPP
14 #define GIL_VIRTUAL_LOCATOR_HPP
15 
24 
25 #include <boost/iterator/iterator_facade.hpp>
26 #include "position_iterator.hpp"
27 
28 namespace boost { namespace gil {
29 
33 template <typename Deref, bool IsTransposed> // A function object that given a point returns a reference. Models PixelDereferenceAdaptorConcept
34 class virtual_2d_locator : public pixel_2d_locator_base<virtual_2d_locator<Deref,IsTransposed>, position_iterator<Deref,IsTransposed>, position_iterator<Deref,1-IsTransposed> > {
36 public:
39 
40  typedef Deref deref_fn_t;
41  typedef typename parent_t::point_t point_t;
42 
43  typedef typename parent_t::coord_t coord_t;
44  typedef typename parent_t::x_coord_t x_coord_t;
45  typedef typename parent_t::y_coord_t y_coord_t;
46  typedef typename parent_t::x_iterator x_iterator;
47  typedef typename parent_t::y_iterator y_iterator;
48 
49  template <typename NewDeref> struct add_deref {
50  typedef virtual_2d_locator<deref_compose<NewDeref,Deref>,IsTransposed > type;
51  static type make(const virtual_2d_locator<Deref,IsTransposed>& loc, const NewDeref& nderef) {
52  return type(loc.pos(), loc.step(), deref_compose<NewDeref,Deref>(nderef,loc.deref_fn()));
53  }
54  };
55 
56  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) {}
57  template <typename D, bool TR> virtual_2d_locator(const virtual_2d_locator<D,TR>& loc, coord_t y_step)
58  : _p(loc.pos(), point_t(loc.step().x,loc.step().y*y_step), loc.deref_fn()) {}
59  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)
60  : _p(loc.pos(), transpose ?
61  point_t(loc.step().x*y_step,loc.step().y*x_step) :
62  point_t(loc.step().x*x_step,loc.step().y*y_step), loc.deref_fn()) { assert(transpose==(IsTransposed!=TR));}
63 
64  template <typename D, bool TR> virtual_2d_locator(const virtual_2d_locator<D,TR>& pl) : _p(pl._p) {}
65  virtual_2d_locator(const virtual_2d_locator& pl) : _p(pl._p) {}
66 
67  bool operator==(const this_t& p) const { return _p==p._p; }
68 
69  x_iterator& x() { return *gil_reinterpret_cast<x_iterator*>(this); }
70  y_iterator& y() { return _p; }
71  x_iterator const& x() const { return *gil_reinterpret_cast_c<x_iterator const*>(this); }
72  y_iterator const& y() const { return _p; }
73 
74  // Returns the y distance between two x_iterators given the difference of their x positions
75  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]; }
76  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?
77 
78  // Methods specific for virtual 2D locator
79  const point_t& pos() const { return _p.pos(); }
80  const point_t& step() const { return _p.step(); }
81  const deref_fn_t& deref_fn() const { return _p.deref_fn(); }
82 private:
83  template <typename D, bool TR> friend class virtual_2d_locator;
84  y_iterator _p; // contains the current position, the step and the dereference object
85 };
86 
88 // PixelBasedConcept
90 
91 template <typename D, bool TR>
92 struct channel_type<virtual_2d_locator<D,TR> > : public channel_type<typename virtual_2d_locator<D,TR>::parent_t> {
93 };
94 
95 template <typename D, bool TR>
96 struct color_space_type<virtual_2d_locator<D,TR> > : public color_space_type<typename virtual_2d_locator<D,TR>::parent_t> {
97 };
98 
99 template <typename D, bool TR>
100 struct channel_mapping_type<virtual_2d_locator<D,TR> > : public channel_mapping_type<typename virtual_2d_locator<D,TR>::parent_t> {
101 };
102 
103 template <typename D, bool TR>
104 struct is_planar<virtual_2d_locator<D,TR> > : public is_planar<typename virtual_2d_locator<D,TR>::parent_t> {
105 };
106 
108 // HasDynamicXStepTypeConcept
110 
111 template <typename D, bool TR>
112 struct dynamic_x_step_type<virtual_2d_locator<D,TR> > {
113  typedef virtual_2d_locator<D,TR> type;
114 };
115 
117 // HasDynamicYStepTypeConcept
119 
120 template <typename D, bool TR>
121 struct dynamic_y_step_type<virtual_2d_locator<D,TR> > {
122  typedef virtual_2d_locator<D,TR> type;
123 };
124 
126 // HasTransposedTypeConcept
128 
129 template <typename D, bool IsTransposed>
130 struct transposed_type<virtual_2d_locator<D,IsTransposed> > {
131  typedef virtual_2d_locator<D,1-IsTransposed> type;
132 };
133 
134 } } // namespace boost::gil
135 
136 #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:118
An iterator that remembers its current X,Y position and invokes a function object with it upon derefe...
Definition: position_iterator.hpp:40
A 2D locator over a virtual image. Upon dereferencing, invokes a given function object passing it its...
Definition: virtual_locator.hpp:34
Locator for virtual image views.
Composes two dereference function objects. Similar to std::unary_compose but needs to pull some typed...
Definition: utilities.hpp:196
2D point both axes of which have the same dimension typeModels: Point2DConcept
Definition: gil_concept.hpp:52