Boost GIL


image_view.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 #ifndef BOOST_GIL_IMAGE_VIEW_HPP
13 #define BOOST_GIL_IMAGE_VIEW_HPP
14 
23 
24 #include <boost/gil/config.hpp>
26 
27 #include <cstddef>
28 #include <iterator>
29 
30 //#ifdef _MSC_VER
31 //#pragma warning(push)
32 //#pragma warning(disable : 4244) // conversion from 'gil::image<V,Alloc>::coord_t' to 'int', possible loss of data (visual studio compiler doesn't realize that the two types are the same)
33 //#endif
34 
35 namespace boost { namespace gil {
36 
68 template <typename Loc> // Models 2D Pixel Locator
69 class image_view {
70 public:
71 
72 // typedefs required by ConstRandomAccessNDImageViewConcept
73  static const std::size_t num_dimensions=2;
74  typedef typename Loc::value_type value_type;
75  typedef typename Loc::reference reference; // result of dereferencing
76  typedef typename Loc::coord_t coord_t; // 1D difference type (same for all dimensions)
77  typedef coord_t difference_type; // result of operator-(1d_iterator,1d_iterator)
78  typedef typename Loc::point_t point_t;
79  typedef Loc locator;
80  typedef image_view<typename Loc::const_t> const_t; // same as this type, but over const values
81  template <std::size_t D> struct axis {
82  typedef typename Loc::template axis<D>::coord_t coord_t; // difference_type along each dimension
83  typedef typename Loc::template axis<D>::iterator iterator; // 1D iterator type along each dimension
84  };
85  typedef iterator_from_2d<Loc> iterator; // 1D iterator type for each pixel left-to-right inside top-to-bottom
86  typedef std::reverse_iterator<iterator> reverse_iterator;
87  typedef std::size_t size_type;
88 
89 // typedefs required by ConstRandomAccess2DImageViewConcept
90  typedef locator xy_locator;
91  typedef typename xy_locator::x_iterator x_iterator; // pixel iterator along a row
92  typedef typename xy_locator::y_iterator y_iterator; // pixel iterator along a column
93  typedef typename xy_locator::x_coord_t x_coord_t;
94  typedef typename xy_locator::y_coord_t y_coord_t;
95 
96  template <typename Deref> struct add_deref {
98  static type make(const image_view<Loc>& iv, const Deref& d) { return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d)); }
99  };
100 
101  image_view() : _dimensions(0,0) {}
102  template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
103 
104  template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {}
105  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) {}
106 
107  template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
108  image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
109 
110  template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
111  template <typename View> bool operator!=(const View& v) const { return !(*this==v); }
112 
113  template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
114 
115  const point_t& dimensions() const { return _dimensions; }
116  const locator& pixels() const { return _pixels; }
117  x_coord_t width() const { return dimensions().x; }
118  y_coord_t height() const { return dimensions().y; }
119  std::size_t num_channels() const { return gil::num_channels<value_type>::value; }
120  bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); }
121 
122  //\{@
124  size_type size() const { return width()*height(); }
125  iterator begin() const { return iterator(_pixels,_dimensions.x); }
126  iterator end() const { return begin()+(difference_type)size(); } // potential performance problem!
127  reverse_iterator rbegin() const { return reverse_iterator(end()); }
128  reverse_iterator rend() const { return reverse_iterator(begin()); }
129  reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem!
130  iterator at(difference_type i)const { return begin()+i; }
131  iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; }
132  iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
133 
134  //\}@
135 
136  //\{@
138  reference operator()(const point_t& p) const { return _pixels(p.x,p.y); }
139  reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
140  template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.axis_iterator<D>(p); }
141  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)); }
142  locator xy_at(const point_t& p) const { return _pixels+p; }
143  //\}@
144 
145  //\{@
147  x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); }
148  x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); }
149  x_iterator row_begin(y_coord_t y) const { return x_at(0,y); }
150  x_iterator row_end(y_coord_t y) const { return x_at(width(),y); }
151  //\}@
152 
153  //\{@
155  y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); }
156  y_iterator y_at(const point_t& p) const { return xy_at(p).y(); }
157  y_iterator col_begin(x_coord_t x) const { return y_at(x,0); }
158  y_iterator col_end(x_coord_t x) const { return y_at(x,height()); }
159  //\}@
160 
161 private:
162  template <typename L2> friend class image_view;
163 
164  point_t _dimensions;
165  xy_locator _pixels;
166 };
167 
168 template <typename L2>
169 inline void swap(image_view<L2>& x, image_view<L2>& y) {
170  using std::swap;
171  swap(x._dimensions,y._dimensions);
172  swap(x._pixels, y._pixels); // TODO: Extend further
173 }
174 
176 // PixelBasedConcept
178 
179 template <typename L>
180 struct channel_type<image_view<L> > : public channel_type<L> {};
181 
182 template <typename L>
183 struct color_space_type<image_view<L> > : public color_space_type<L> {};
184 
185 template <typename L>
186 struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
187 
188 template <typename L>
189 struct is_planar<image_view<L> > : public is_planar<L> {};
190 
192 // HasDynamicXStepTypeConcept
194 
195 template <typename L>
196 struct dynamic_x_step_type<image_view<L> > {
197  typedef image_view<typename dynamic_x_step_type<L>::type> type;
198 };
199 
201 // HasDynamicYStepTypeConcept
203 
204 template <typename L>
205 struct dynamic_y_step_type<image_view<L> > {
206  typedef image_view<typename dynamic_y_step_type<L>::type> type;
207 };
208 
210 // HasTransposedTypeConcept
212 
213 template <typename L>
214 struct transposed_type<image_view<L> > {
215  typedef image_view<typename transposed_type<L>::type> type;
216 };
217 
218 } } // namespace boost::gil
219 
220 //#ifdef _MSC_VER
221 //#pragma warning(pop)
222 //#endif
223 
224 #endif
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept.
Definition: image_view.hpp:69
GIL configuration file.
Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept, PixelBasedConcept, HasDynamicXStepTypeConcept.
Definition: iterator_from_2d.hpp:54
Returns an MPL integral type specifying the number of elements in a color base.
Definition: color_base_algorithm.hpp:60
Returns the number of channels of a pixel-based GIL construct.
Definition: concept.hpp:68
pixel step iterator, pixel image iterator and pixel dereference iterator