]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 GIL_IMAGE_VIEW_H | |
13 | #define GIL_IMAGE_VIEW_H | |
14 | ||
15 | //////////////////////////////////////////////////////////////////////////////////////// | |
16 | /// \file | |
17 | /// \brief image view class | |
18 | /// \author Lubomir Bourdev and Hailin Jin \n | |
19 | /// Adobe Systems Incorporated | |
20 | /// \date 2005-2007 \n Last updated on February 12, 2007 | |
21 | /// | |
22 | //////////////////////////////////////////////////////////////////////////////////////// | |
23 | ||
24 | #include <cstddef> | |
25 | #include <iterator> | |
26 | #include "gil_config.hpp" | |
27 | #include "iterator_from_2d.hpp" | |
28 | ||
29 | //#ifdef _MSC_VER | |
30 | //#pragma warning(push) | |
31 | //#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) | |
32 | //#endif | |
33 | ||
34 | namespace boost { namespace gil { | |
35 | ||
36 | //////////////////////////////////////////////////////////////////////////////////////// | |
37 | /// \class image_view | |
38 | /// \ingroup ImageViewModel PixelBasedModel | |
39 | /// \brief A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,PixelBasedConcept,HasDynamicXStepTypeConcept,HasDynamicYStepTypeConcept,HasTransposedTypeConcept | |
40 | /// | |
41 | /// Image view consists of a pixel 2D locator (defining the mechanism for navigating in 2D) | |
42 | /// and the image dimensions. | |
43 | /// | |
44 | /// Image views to images are what ranges are to STL containers. They are lightweight objects, | |
45 | /// that don't own the pixels. It is the user's responsibility that the underlying data remains | |
46 | /// valid for the lifetime of the image view. | |
47 | /// | |
48 | /// Similar to iterators and ranges, constness of views does not extend to constness of pixels. | |
49 | /// A const \p image_view does not allow changing its location in memory (resizing, moving) but does | |
50 | /// not prevent one from changing the pixels. The latter requires an image view whose value_type | |
51 | /// is const. | |
52 | /// | |
53 | /// Images have interfaces consistent with STL 1D random access containers, so they can be used | |
54 | /// directly in STL algorithms like: | |
55 | /// \code | |
56 | /// std::fill(img.begin(), img.end(), red_pixel); | |
57 | /// \endcode | |
58 | /// | |
59 | /// In addition, horizontal, vertical and 2D random access iterators are provided. | |
60 | /// | |
61 | /// Note also that \p image_view does not require that its element type be a pixel. It could be | |
62 | /// instantiated with a locator whose \p value_type models only \p Regular. In this case the image | |
63 | /// view models the weaker RandomAccess2DImageViewConcept, and does not model PixelBasedConcept. | |
64 | /// Many generic algorithms don't require the elements to be pixels. | |
65 | /// | |
66 | //////////////////////////////////////////////////////////////////////////////////////// | |
67 | template <typename Loc> // Models 2D Pixel Locator | |
68 | class image_view { | |
69 | public: | |
70 | ||
71 | // typedefs required by ConstRandomAccessNDImageViewConcept | |
72 | static const std::size_t num_dimensions=2; | |
73 | typedef typename Loc::value_type value_type; | |
74 | typedef typename Loc::reference reference; // result of dereferencing | |
75 | typedef typename Loc::coord_t coord_t; // 1D difference type (same for all dimensions) | |
76 | typedef coord_t difference_type; // result of operator-(1d_iterator,1d_iterator) | |
77 | typedef typename Loc::point_t point_t; | |
78 | typedef Loc locator; | |
79 | typedef image_view<typename Loc::const_t> const_t; // same as this type, but over const values | |
80 | template <std::size_t D> struct axis { | |
81 | typedef typename Loc::template axis<D>::coord_t coord_t; // difference_type along each dimension | |
82 | typedef typename Loc::template axis<D>::iterator iterator; // 1D iterator type along each dimension | |
83 | }; | |
84 | typedef iterator_from_2d<Loc> iterator; // 1D iterator type for each pixel left-to-right inside top-to-bottom | |
85 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
86 | typedef std::size_t size_type; | |
87 | ||
88 | // typedefs required by ConstRandomAccess2DImageViewConcept | |
89 | typedef locator xy_locator; | |
90 | typedef typename xy_locator::x_iterator x_iterator; // pixel iterator along a row | |
91 | typedef typename xy_locator::y_iterator y_iterator; // pixel iterator along a column | |
92 | typedef typename xy_locator::x_coord_t x_coord_t; | |
93 | typedef typename xy_locator::y_coord_t y_coord_t; | |
94 | ||
95 | template <typename Deref> struct add_deref { | |
96 | typedef image_view<typename Loc::template add_deref<Deref>::type> type; | |
97 | static type make(const image_view<Loc>& iv, const Deref& d) { return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d)); } | |
98 | }; | |
99 | ||
100 | image_view() : _dimensions(0,0) {} | |
101 | template <typename View> image_view(const View& iv) : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {} | |
102 | ||
103 | template <typename L2> image_view(const point_t& sz , const L2& loc) : _dimensions(sz), _pixels(loc) {} | |
104 | 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) {} | |
105 | ||
106 | template <typename View> image_view& operator=(const View& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; } | |
107 | image_view& operator=(const image_view& iv) { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; } | |
108 | ||
109 | template <typename View> bool operator==(const View& v) const { return pixels()==v.pixels() && dimensions()==v.dimensions(); } | |
110 | template <typename View> bool operator!=(const View& v) const { return !(*this==v); } | |
111 | ||
112 | template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y); | |
113 | ||
114 | const point_t& dimensions() const { return _dimensions; } | |
115 | const locator& pixels() const { return _pixels; } | |
116 | x_coord_t width() const { return dimensions().x; } | |
117 | y_coord_t height() const { return dimensions().y; } | |
118 | std::size_t num_channels() const { return gil::num_channels<value_type>::value; } | |
119 | bool is_1d_traversable() const { return _pixels.is_1d_traversable(width()); } | |
120 | ||
121 | //\{@ | |
122 | /// \name 1D navigation | |
123 | size_type size() const { return width()*height(); } | |
124 | iterator begin() const { return iterator(_pixels,_dimensions.x); } | |
125 | iterator end() const { return begin()+(difference_type)size(); } // potential performance problem! | |
126 | reverse_iterator rbegin() const { return reverse_iterator(end()); } | |
127 | reverse_iterator rend() const { return reverse_iterator(begin()); } | |
128 | reference operator[](difference_type i) const { return begin()[i]; } // potential performance problem! | |
129 | iterator at(difference_type i)const { return begin()+i; } | |
130 | iterator at(const point_t& p) const { return begin()+p.y*width()+p.x; } | |
131 | iterator at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; } | |
132 | ||
133 | //\}@ | |
134 | ||
135 | //\{@ | |
136 | /// \name 2-D navigation | |
137 | reference operator()(const point_t& p) const { return _pixels(p.x,p.y); } | |
138 | reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); } | |
139 | template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.axis_iterator<D>(p); } | |
140 | 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)); } | |
141 | locator xy_at(const point_t& p) const { return _pixels+p; } | |
142 | //\}@ | |
143 | ||
144 | //\{@ | |
145 | /// \name X navigation | |
146 | x_iterator x_at(x_coord_t x, y_coord_t y) const { return _pixels.x_at(x,y); } | |
147 | x_iterator x_at(const point_t& p) const { return _pixels.x_at(p); } | |
148 | x_iterator row_begin(y_coord_t y) const { return x_at(0,y); } | |
149 | x_iterator row_end(y_coord_t y) const { return x_at(width(),y); } | |
150 | //\}@ | |
151 | ||
152 | //\{@ | |
153 | /// \name Y navigation | |
154 | y_iterator y_at(x_coord_t x, y_coord_t y) const { return xy_at(x,y).y(); } | |
155 | y_iterator y_at(const point_t& p) const { return xy_at(p).y(); } | |
156 | y_iterator col_begin(x_coord_t x) const { return y_at(x,0); } | |
157 | y_iterator col_end(x_coord_t x) const { return y_at(x,height()); } | |
158 | //\}@ | |
159 | ||
160 | private: | |
161 | template <typename L2> friend class image_view; | |
162 | ||
163 | point_t _dimensions; | |
164 | xy_locator _pixels; | |
165 | }; | |
166 | ||
167 | template <typename L2> | |
168 | inline void swap(image_view<L2>& x, image_view<L2>& y) { | |
169 | using std::swap; | |
170 | swap(x._dimensions,y._dimensions); | |
171 | swap(x._pixels, y._pixels); // TODO: Extend further | |
172 | } | |
173 | ||
174 | ///////////////////////////// | |
175 | // PixelBasedConcept | |
176 | ///////////////////////////// | |
177 | ||
178 | template <typename L> | |
179 | struct channel_type<image_view<L> > : public channel_type<L> {}; | |
180 | ||
181 | template <typename L> | |
182 | struct color_space_type<image_view<L> > : public color_space_type<L> {}; | |
183 | ||
184 | template <typename L> | |
185 | struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {}; | |
186 | ||
187 | template <typename L> | |
188 | struct is_planar<image_view<L> > : public is_planar<L> {}; | |
189 | ||
190 | ///////////////////////////// | |
191 | // HasDynamicXStepTypeConcept | |
192 | ///////////////////////////// | |
193 | ||
194 | template <typename L> | |
195 | struct dynamic_x_step_type<image_view<L> > { | |
196 | typedef image_view<typename dynamic_x_step_type<L>::type> type; | |
197 | }; | |
198 | ||
199 | ///////////////////////////// | |
200 | // HasDynamicYStepTypeConcept | |
201 | ///////////////////////////// | |
202 | ||
203 | template <typename L> | |
204 | struct dynamic_y_step_type<image_view<L> > { | |
205 | typedef image_view<typename dynamic_y_step_type<L>::type> type; | |
206 | }; | |
207 | ||
208 | ///////////////////////////// | |
209 | // HasTransposedTypeConcept | |
210 | ///////////////////////////// | |
211 | ||
212 | template <typename L> | |
213 | struct transposed_type<image_view<L> > { | |
214 | typedef image_view<typename transposed_type<L>::type> type; | |
215 | }; | |
216 | ||
217 | } } // namespace boost::gil | |
218 | ||
219 | //#ifdef _MSC_VER | |
220 | //#pragma warning(pop) | |
221 | //#endif | |
222 | ||
223 | #endif |