Boost GIL


image_view.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_IMAGE_VIEW_HPP
9 #define BOOST_GIL_IMAGE_VIEW_HPP
10 
11 #include <boost/gil/iterator_from_2d.hpp>
12 
13 #include <cstddef>
14 #include <iterator>
15 
16 namespace boost { namespace gil {
17 
49 template <typename Loc> // Models 2D Pixel Locator
50 class image_view {
51 public:
52 
53 // typedefs required by ConstRandomAccessNDImageViewConcept
54  static const std::size_t num_dimensions=2;
55  typedef typename Loc::value_type value_type;
56  typedef typename Loc::reference reference; // result of dereferencing
57  typedef typename Loc::coord_t coord_t; // 1D difference type (same for all dimensions)
58  typedef coord_t difference_type; // result of operator-(1d_iterator,1d_iterator)
59  typedef typename Loc::point_t point_t;
60  typedef Loc locator;
61  typedef image_view<typename Loc::const_t> const_t; // same as this type, but over const values
62  template <std::size_t D> struct axis {
63  typedef typename Loc::template axis<D>::coord_t coord_t; // difference_type along each dimension
64  typedef typename Loc::template axis<D>::iterator iterator; // 1D iterator type along each dimension
65  };
66  typedef iterator_from_2d<Loc> iterator; // 1D iterator type for each pixel left-to-right inside top-to-bottom
67  typedef typename const_t::iterator const_iterator; // may be used to examine, but not to modify values
68  typedef typename const_t::reference const_reference; // behaves as a const reference
69  typedef typename std::iterator_traits<iterator>::pointer pointer; // behaves as a pointer to the value type
70  typedef std::reverse_iterator<iterator> reverse_iterator;
71  typedef std::size_t size_type;
72 
73 // typedefs required by ConstRandomAccess2DImageViewConcept
74  typedef locator xy_locator;
75  typedef typename xy_locator::x_iterator x_iterator; // pixel iterator along a row
76  typedef typename xy_locator::y_iterator y_iterator; // pixel iterator along a column
77  typedef typename xy_locator::x_coord_t x_coord_t;
78  typedef typename xy_locator::y_coord_t y_coord_t;
79 
80  template <typename Deref> struct add_deref {
82  static type make(const image_view<Loc>& iv, const Deref& d) { return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d)); }
83  };
84 
85  image_view() : _dimensions(0,0) {}
86  template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
87 
88  template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {}
89  template <typename L2> image_view(coord_t width, coord_t height, const L2& loc) : _dimensions(x_coord_t(width),y_coord_t(height)), _pixels(loc) {}
90 
91  template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
92  image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
93 
94  template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
95  template <typename View> bool operator!=(const View& v) const { return !(*this==v); }
96 
97  template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
98 
104  void swap(image_view<Loc>& other)
105  {
106  using boost::gil::swap;
107  swap(*this, other);
108  }
109 
114  bool empty() const { return !(width() > 0 && height() > 0); }
115 
120  reference front() const { return *begin(); }
121 
126  reference back() const { return *rbegin(); }
127 
128  const point_t& dimensions() const { return _dimensions; }
129  const locator& pixels() const { return _pixels; }
130  x_coord_t width() const { return dimensions().x; }
131  y_coord_t height() const { return dimensions().y; }
132  std::size_t num_channels() const { return gil::num_channels<value_type>::value; }
133  bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); }
134 
135  //\{@
137  size_type size() const { return width()*height(); }
138  iterator begin() const { return iterator(_pixels,_dimensions.x); }
139  iterator end() const { return begin()+(difference_type)size(); } // potential performance problem!
140  reverse_iterator rbegin() const { return reverse_iterator(end()); }
141  reverse_iterator rend() const { return reverse_iterator(begin()); }
142  reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem!
143  iterator at(difference_type i)const { return begin()+i; }
144  iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; }
145  iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
146 
147  //\}@
148 
149  //\{@
151  reference operator()(const point_t& p) const { return _pixels(p.x,p.y); }
152  reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
153  template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.template axis_iterator<D>(p); }
154  xy_locator xy_at(x_coord_t x, y_coord_t y) const { return _pixels+point_t(x_coord_t(x),y_coord_t(y)); }
155  locator xy_at(const point_t& p) const { return _pixels+p; }
156  //\}@
157 
158  //\{@
160  x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); }
161  x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); }
162  x_iterator row_begin(y_coord_t y) const { return x_at(0,y); }
163  x_iterator row_end(y_coord_t y) const { return x_at(width(),y); }
164  //\}@
165 
166  //\{@
168  y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); }
169  y_iterator y_at(const point_t& p) const { return xy_at(p).y(); }
170  y_iterator col_begin(x_coord_t x) const { return y_at(x,0); }
171  y_iterator col_end(x_coord_t x) const { return y_at(x,height()); }
172  //\}@
173 
174 private:
175  template <typename L2> friend class image_view;
176 
177  point_t _dimensions;
178  xy_locator _pixels;
179 };
180 
181 template <typename L2>
182 inline void swap(image_view<L2>& x, image_view<L2>& y) {
183  using std::swap;
184  swap(x._dimensions,y._dimensions);
185  swap(x._pixels, y._pixels); // TODO: Extend further
186 }
187 
189 // PixelBasedConcept
191 
192 template <typename L>
193 struct channel_type<image_view<L> > : public channel_type<L> {};
194 
195 template <typename L>
196 struct color_space_type<image_view<L> > : public color_space_type<L> {};
197 
198 template <typename L>
199 struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
200 
201 template <typename L>
202 struct is_planar<image_view<L> > : public is_planar<L> {};
203 
205 // HasDynamicXStepTypeConcept
207 
208 template <typename L>
209 struct dynamic_x_step_type<image_view<L> > {
210  typedef image_view<typename dynamic_x_step_type<L>::type> type;
211 };
212 
214 // HasDynamicYStepTypeConcept
216 
217 template <typename L>
218 struct dynamic_y_step_type<image_view<L> > {
219  typedef image_view<typename dynamic_y_step_type<L>::type> type;
220 };
221 
223 // HasTransposedTypeConcept
225 
226 template <typename L>
227 struct transposed_type<image_view<L> > {
228  typedef image_view<typename transposed_type<L>::type> type;
229 };
230 
231 }} // namespace boost::gil
232 
233 #endif
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:50
reference back() const
Returns a reference to the last element in raster order.
Definition: image_view.hpp:126
void swap(const boost::gil::packed_channel_reference< BF, FB, NB, M > x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:480
bool empty() const
Returns true if the view has no elements, false otherwise.
Definition: image_view.hpp:114
Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept.
Definition: iterator_from_2d.hpp:43
void swap(image_view< Loc > &other)
Exchanges the elements of the current view with those of other in constant time.
Definition: image_view.hpp:104
reference front() const
Returns a reference to the first element in raster order.
Definition: image_view.hpp:120