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 // aliases required by ConstRandomAccessNDImageViewConcept
54  static const std::size_t num_dimensions=2;
55  using value_type = typename Loc::value_type;
56  using reference = typename Loc::reference; // result of dereferencing
57  using coord_t = typename Loc::coord_t; // 1D difference type (same for all dimensions)
58  using difference_type = coord_t; // result of operator-(1d_iterator,1d_iterator)
59  using point_t = typename Loc::point_t;
60  using locator = Loc;
61  using const_t = image_view<typename Loc::const_t>; // same as this type, but over const values
62  template <std::size_t D> struct axis
63  {
64  using coord_t = typename Loc::template axis<D>::coord_t; // difference_type along each dimension
65  using iterator = typename Loc::template axis<D>::iterator; // 1D iterator type along each dimension
66  };
67  using iterator = iterator_from_2d<Loc>; // 1D iterator type for each pixel left-to-right inside top-to-bottom
68  using const_iterator = typename const_t::iterator; // may be used to examine, but not to modify values
69  using const_reference = typename const_t::reference; // behaves as a const reference
70  using pointer = typename std::iterator_traits<iterator>::pointer; // behaves as a pointer to the value type
71  using reverse_iterator = std::reverse_iterator<iterator>;
72  using size_type = std::size_t;
73 
74 // aliases required by ConstRandomAccess2DImageViewConcept
75  using xy_locator = locator;
76  using x_iterator = typename xy_locator::x_iterator; // pixel iterator along a row
77  using y_iterator = typename xy_locator::y_iterator; // pixel iterator along a column
78  using x_coord_t = typename xy_locator::x_coord_t;
79  using y_coord_t = typename xy_locator::y_coord_t;
80 
81  template <typename Deref> struct add_deref
82  {
84  static type make(const image_view<Loc>& iv, const Deref& d)
85  {
86  return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d));
87  }
88  };
89 
90  image_view() : _dimensions(0,0) {}
91  template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
92 
93  template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {}
94  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) {}
95 
96  template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
97  image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
98 
99  template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
100  template <typename View> bool operator!=(const View& v) const { return !(*this==v); }
101 
102  template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
103 
109  void swap(image_view<Loc>& other)
110  {
111  using boost::gil::swap;
112  swap(*this, other);
113  }
114 
119  bool empty() const { return !(width() > 0 && height() > 0); }
120 
125  reference front() const { return *begin(); }
126 
131  reference back() const { return *rbegin(); }
132 
133  const point_t& dimensions() const { return _dimensions; }
134  const locator& pixels() const { return _pixels; }
135  x_coord_t width() const { return dimensions().x; }
136  y_coord_t height() const { return dimensions().y; }
137  std::size_t num_channels() const { return gil::num_channels<value_type>::value; }
138  bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); }
139 
140  //\{@
142  size_type size() const { return width()*height(); }
143  iterator begin() const { return iterator(_pixels,_dimensions.x); }
144  iterator end() const { return begin()+(difference_type)size(); } // potential performance problem!
145  reverse_iterator rbegin() const { return reverse_iterator(end()); }
146  reverse_iterator rend() const { return reverse_iterator(begin()); }
147  reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem!
148  iterator at(difference_type i)const { return begin()+i; }
149  iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; }
150  iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
151 
152  //\}@
153 
154  //\{@
156  reference operator()(const point_t& p) const { return _pixels(p.x,p.y); }
157  reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
158  template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.template axis_iterator<D>(p); }
159  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)); }
160  locator xy_at(const point_t& p) const { return _pixels+p; }
161  //\}@
162 
163  //\{@
165  x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); }
166  x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); }
167  x_iterator row_begin(y_coord_t y) const { return x_at(0,y); }
168  x_iterator row_end(y_coord_t y) const { return x_at(width(),y); }
169  //\}@
170 
171  //\{@
173  y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); }
174  y_iterator y_at(const point_t& p) const { return xy_at(p).y(); }
175  y_iterator col_begin(x_coord_t x) const { return y_at(x,0); }
176  y_iterator col_end(x_coord_t x) const { return y_at(x,height()); }
177  //\}@
178 
179 private:
180  template <typename L2> friend class image_view;
181 
182  point_t _dimensions;
183  xy_locator _pixels;
184 };
185 
186 template <typename L2>
187 inline void swap(image_view<L2>& x, image_view<L2>& y) {
188  using std::swap;
189  swap(x._dimensions,y._dimensions);
190  swap(x._pixels, y._pixels); // TODO: Extend further
191 }
192 
194 // PixelBasedConcept
196 
197 template <typename L>
198 struct channel_type<image_view<L> > : public channel_type<L> {};
199 
200 template <typename L>
201 struct color_space_type<image_view<L> > : public color_space_type<L> {};
202 
203 template <typename L>
204 struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
205 
206 template <typename L>
207 struct is_planar<image_view<L> > : public is_planar<L> {};
208 
210 // HasDynamicXStepTypeConcept
212 
213 template <typename L>
214 struct dynamic_x_step_type<image_view<L> > {
215  using type = image_view<typename dynamic_x_step_type<L>::type>;
216 };
217 
219 // HasDynamicYStepTypeConcept
221 
222 template <typename L>
223 struct dynamic_y_step_type<image_view<L> > {
224  using type = image_view<typename dynamic_y_step_type<L>::type>;
225 };
226 
228 // HasTransposedTypeConcept
230 
231 template <typename L>
232 struct transposed_type<image_view<L> > {
233  using type = image_view<typename transposed_type<L>::type>;
234 };
235 
236 }} // namespace boost::gil
237 
238 #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:131
void swap(const boost::gil::packed_channel_reference< BF, FB, NB, M > x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:481
bool empty() const
Returns true if the view has no elements, false otherwise.
Definition: image_view.hpp:119
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:109
reference front() const
Returns a reference to the first element in raster order.
Definition: image_view.hpp:125