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/dynamic_step.hpp>
12 #include <boost/gil/iterator_from_2d.hpp>
13 
14 #include <cstddef>
15 #include <iterator>
16 
17 namespace boost { namespace gil {
18 
50 template <typename Loc> // Models 2D Pixel Locator
51 class image_view {
52 public:
53 
54 // aliases required by ConstRandomAccessNDImageViewConcept
55  static const std::size_t num_dimensions=2;
56  using value_type = typename Loc::value_type;
57  using reference = typename Loc::reference; // result of dereferencing
58  using coord_t = typename Loc::coord_t; // 1D difference type (same for all dimensions)
59  using difference_type = coord_t; // result of operator-(1d_iterator,1d_iterator)
60  using point_t = typename Loc::point_t;
61  using locator = Loc;
62  using const_t = image_view<typename Loc::const_t>; // same as this type, but over const values
63  template <std::size_t D> struct axis
64  {
65  using coord_t = typename Loc::template axis<D>::coord_t; // difference_type along each dimension
66  using iterator = typename Loc::template axis<D>::iterator; // 1D iterator type along each dimension
67  };
68  using iterator = iterator_from_2d<Loc>; // 1D iterator type for each pixel left-to-right inside top-to-bottom
69  using const_iterator = typename const_t::iterator; // may be used to examine, but not to modify values
70  using const_reference = typename const_t::reference; // behaves as a const reference
71  using pointer = typename std::iterator_traits<iterator>::pointer; // behaves as a pointer to the value type
72  using reverse_iterator = std::reverse_iterator<iterator>;
73  using size_type = std::size_t;
74 
75 // aliases required by ConstRandomAccess2DImageViewConcept
76  using xy_locator = locator;
77  using x_iterator = typename xy_locator::x_iterator; // pixel iterator along a row
78  using y_iterator = typename xy_locator::y_iterator; // pixel iterator along a column
79  using x_coord_t = typename xy_locator::x_coord_t;
80  using y_coord_t = typename xy_locator::y_coord_t;
81 
82  template <typename Deref> struct add_deref
83  {
85  static type make(const image_view<Loc>& iv, const Deref& d)
86  {
87  return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d));
88  }
89  };
90 
91  image_view() : _dimensions(0,0) {}
92  image_view(image_view const& img_view) : _dimensions(img_view.dimensions()), _pixels(img_view.pixels()) {}
93  template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
94 
95  template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {}
96  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) {}
97 
98  template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
99  image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
100 
101  template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
102  template <typename View> bool operator!=(const View& v) const { return !(*this==v); }
103 
104  template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
105 
111  void swap(image_view<Loc>& other)
112  {
113  using boost::gil::swap;
114  swap(*this, other);
115  }
116 
121  bool empty() const { return !(width() > 0 && height() > 0); }
122 
127  reference front() const { return *begin(); }
128 
133  reference back() const { return *rbegin(); }
134 
135  const point_t& dimensions() const { return _dimensions; }
136  const locator& pixels() const { return _pixels; }
137  x_coord_t width() const { return dimensions().x; }
138  y_coord_t height() const { return dimensions().y; }
139  std::size_t num_channels() const { return gil::num_channels<value_type>::value; }
140  bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); }
141 
142  //\{@
144  size_type size() const { return width()*height(); }
145  iterator begin() const { return iterator(_pixels,_dimensions.x); }
146  iterator end() const { return begin()+(difference_type)size(); } // potential performance problem!
147  reverse_iterator rbegin() const { return reverse_iterator(end()); }
148  reverse_iterator rend() const { return reverse_iterator(begin()); }
149  reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem!
150  iterator at(difference_type i)const { return begin()+i; }
151  iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; }
152  iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
153 
154  //\}@
155 
156  //\{@
158  reference operator()(const point_t& p) const { return _pixels(p.x,p.y); }
159  reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
160  template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.template axis_iterator<D>(p); }
161  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)); }
162  locator xy_at(const point_t& p) const { return _pixels+p; }
163  //\}@
164 
165  //\{@
167  x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); }
168  x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); }
169  x_iterator row_begin(y_coord_t y) const { return x_at(0,y); }
170  x_iterator row_end(y_coord_t y) const { return x_at(width(),y); }
171  //\}@
172 
173  //\{@
175  y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); }
176  y_iterator y_at(const point_t& p) const { return xy_at(p).y(); }
177  y_iterator col_begin(x_coord_t x) const { return y_at(x,0); }
178  y_iterator col_end(x_coord_t x) const { return y_at(x,height()); }
179  //\}@
180 
181 private:
182  template <typename L2> friend class image_view;
183 
184  point_t _dimensions;
185  xy_locator _pixels;
186 };
187 
188 template <typename L2>
189 inline void swap(image_view<L2>& x, image_view<L2>& y) {
190  using std::swap;
191  swap(x._dimensions,y._dimensions);
192  swap(x._pixels, y._pixels); // TODO: Extend further
193 }
194 
196 // PixelBasedConcept
198 
199 template <typename L>
200 struct channel_type<image_view<L> > : public channel_type<L> {};
201 
202 template <typename L>
203 struct color_space_type<image_view<L> > : public color_space_type<L> {};
204 
205 template <typename L>
206 struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
207 
208 template <typename L>
209 struct is_planar<image_view<L> > : public is_planar<L> {};
210 
212 // HasDynamicXStepTypeConcept
214 
215 template <typename L>
216 struct dynamic_x_step_type<image_view<L>>
217 {
219 };
220 
222 // HasDynamicYStepTypeConcept
224 
225 template <typename L>
226 struct dynamic_y_step_type<image_view<L>>
227 {
229 };
230 
232 // HasTransposedTypeConcept
234 
235 template <typename L>
236 struct transposed_type<image_view<L>>
237 {
239 };
240 
241 }} // namespace boost::gil
242 
243 #endif
Definition: algorithm.hpp:30
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:51
reference back() const
Returns a reference to the last element in raster order.
Definition: image_view.hpp:133
bool empty() const
Returns true if the view has no elements, false otherwise.
Definition: image_view.hpp:121
Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept.
Definition: iterator_from_2d.hpp:42
void swap(image_view< Loc > &other)
Exchanges the elements of the current view with those of other in constant time.
Definition: image_view.hpp:111
Definition: image_view_factory.hpp:40
Base template for types that model HasDynamicYStepTypeConcept.
Definition: dynamic_step.hpp:21
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:524
reference front() const
Returns a reference to the first element in raster order.
Definition: image_view.hpp:127
Definition: color_convert.hpp:31
Returns an integral constant type specifying the number of elements in a color base.
Definition: color_base_algorithm.hpp:42
Returns the number of channels of a pixel-based GIL construct.
Definition: locator.hpp:38
Base template for types that model HasDynamicXStepTypeConcept.
Definition: dynamic_step.hpp:17