8 #ifndef BOOST_GIL_IMAGE_VIEW_HPP
9 #define BOOST_GIL_IMAGE_VIEW_HPP
11 #include <boost/gil/dynamic_step.hpp>
12 #include <boost/gil/iterator_from_2d.hpp>
14 #include <boost/assert.hpp>
19 namespace boost {
namespace gil {
52 template <
typename Loc>
57 static const std::size_t num_dimensions=2;
58 using value_type =
typename Loc::value_type;
59 using reference =
typename Loc::reference;
60 using coord_t =
typename Loc::coord_t;
61 using difference_type = coord_t;
62 using point_t =
typename Loc::point_t;
65 template <std::
size_t D>
struct axis
67 using coord_t =
typename Loc::template axis<D>::coord_t;
68 using iterator =
typename Loc::template axis<D>::iterator;
72 using const_reference =
typename const_t::reference;
73 using pointer =
typename std::iterator_traits<iterator>::pointer;
74 using reverse_iterator = std::reverse_iterator<iterator>;
75 using size_type = std::size_t;
78 using xy_locator = locator;
79 using x_iterator =
typename xy_locator::x_iterator;
80 using y_iterator =
typename xy_locator::y_iterator;
81 using x_coord_t =
typename xy_locator::x_coord_t;
82 using y_coord_t =
typename xy_locator::y_coord_t;
84 template <
typename Deref>
90 return type(
view.dimensions(), Loc::template add_deref<Deref>::make(
view.pixels(), d));
96 : _dimensions(img_view.dimensions()), _pixels(img_view.pixels())
99 template <
typename View>
102 template <
typename L2>
103 image_view(point_t
const& dims, L2
const& loc) : _dimensions(dims), _pixels(loc) {}
105 template <
typename L2>
106 image_view(coord_t width, coord_t height, L2
const& loc)
107 : _dimensions(x_coord_t(width), y_coord_t(height)), _pixels(loc)
110 template <
typename View>
113 _pixels =
view.pixels();
114 _dimensions =
view.dimensions();
121 _pixels =
view.pixels();
122 _dimensions =
view.dimensions();
126 template <
typename View>
127 bool operator==(View
const &
view)
const
129 return pixels() ==
view.pixels() && dimensions() ==
view.dimensions();
132 template <
typename View>
133 bool operator!=(View
const&
view)
const
135 return !(*
this ==
view);
138 template <
typename L2>
152 auto dimensions() const ->
point_t const&
157 auto pixels() const -> locator const&
162 auto width() const -> x_coord_t
164 return dimensions().x;
167 auto height() const -> y_coord_t
169 return dimensions().y;
172 auto num_channels() const -> std::
size_t
174 return gil::num_channels<value_type>::value;
177 bool is_1d_traversable()
const
179 return _pixels.is_1d_traversable(width());
188 return !(width() > 0 && height() > 0);
197 BOOST_ASSERT(!
empty());
207 BOOST_ASSERT(!
empty());
213 auto size() const -> size_type
215 return width() * height();
218 auto begin() const -> iterator
220 return iterator(_pixels, _dimensions.x);
223 auto end() const -> iterator
226 return begin() +
static_cast<difference_type
>(size());
229 auto rbegin() const -> reverse_iterator
231 return reverse_iterator(end());
234 auto rend() const -> reverse_iterator
236 return reverse_iterator(begin());
239 auto operator[](difference_type i)
const -> reference
241 BOOST_ASSERT(i <
static_cast<difference_type
>(size()));
245 auto at(difference_type i)
const -> iterator
248 BOOST_ASSERT(i <
static_cast<difference_type
>(size()));
252 auto at(point_t
const& p)
const -> iterator
255 BOOST_ASSERT(0 <= p.x && p.x < width());
256 BOOST_ASSERT(0 <= p.y && p.y < height());
257 return begin() + p.y * width() + p.x;
260 auto at(x_coord_t x, y_coord_t y)
const -> iterator
263 BOOST_ASSERT(0 <= x && x < width());
264 BOOST_ASSERT(0 <= y && y < height());
265 return begin() + y * width() + x;
271 auto operator()(point_t
const& p)
const -> reference
273 BOOST_ASSERT(0 <= p.x && p.x < width());
274 BOOST_ASSERT(0 <= p.y && p.y < height());
275 return _pixels(p.x, p.y);
278 auto operator()(x_coord_t x, y_coord_t y)
const -> reference
280 BOOST_ASSERT(0 <= x && x < width());
281 BOOST_ASSERT(0 <= y && y < height());
282 return _pixels(x, y);
285 template <std::
size_t D>
286 auto axis_iterator(point_t
const& p)
const ->
typename axis<D>::iterator
289 BOOST_ASSERT(0 <= p.x && p.x <= width());
290 BOOST_ASSERT(0 <= p.y && p.y <= height());
291 return _pixels.template axis_iterator<D>(p);
294 auto xy_at(x_coord_t x, y_coord_t y)
const -> xy_locator
297 BOOST_ASSERT(x < width());
298 BOOST_ASSERT(y <= height());
299 return _pixels + point_t(x, y);
302 auto xy_at(point_t
const& p)
const -> xy_locator
305 BOOST_ASSERT(p.x < width());
306 BOOST_ASSERT(p.y < height());
313 auto x_at(x_coord_t x, y_coord_t y)
const -> x_iterator
315 BOOST_ASSERT(0 <= x && x <= width());
316 BOOST_ASSERT(0 <= y && y < height());
317 return _pixels.x_at(x, y);
320 auto x_at(point_t
const& p)
const -> x_iterator
322 BOOST_ASSERT(0 <= p.x && p.x <= width());
323 BOOST_ASSERT(0 <= p.y && p.y < height());
324 return _pixels.x_at(p);
327 auto row_begin(y_coord_t y)
const -> x_iterator
329 BOOST_ASSERT(0 <= y && y < height());
333 auto row_end(y_coord_t y)
const -> x_iterator
335 BOOST_ASSERT(0 <= y && y < height());
336 return x_at(width(), y);
342 auto y_at(x_coord_t x, y_coord_t y)
const -> y_iterator
344 BOOST_ASSERT(0 <= x && x < width());
345 BOOST_ASSERT(0 <= y && y <= height());
346 return xy_at(x, y).y();
349 auto y_at(point_t
const& p)
const -> y_iterator
351 BOOST_ASSERT(0 <= p.x && p.x < width());
352 BOOST_ASSERT(0 <= p.y && p.y <= height());
356 auto col_begin(x_coord_t x)
const -> y_iterator
358 BOOST_ASSERT(0 <= x && x < width());
362 auto col_end(x_coord_t x)
const -> y_iterator
364 BOOST_ASSERT(0 <= x && x < width());
365 return y_at(x, height());
370 template <
typename L2>
371 friend class image_view;
377 template <
typename L2>
378 inline void swap(image_view<L2>& x, image_view<L2>& y) {
380 swap(x._dimensions,y._dimensions);
381 swap(x._pixels, y._pixels);
388 template <
typename L>
389 struct channel_type<image_view<L> > :
public channel_type<L> {};
391 template <
typename L>
392 struct color_space_type<image_view<L> > :
public color_space_type<L> {};
394 template <
typename L>
395 struct channel_mapping_type<image_view<L> > :
public channel_mapping_type<L> {};
397 template <
typename L>
398 struct is_planar<image_view<L> > :
public is_planar<L> {};
404 template <
typename L>
405 struct dynamic_x_step_type<image_view<L>>
407 using type = image_view<typename gil::dynamic_x_step_type<L>::type>;
414 template <
typename L>
415 struct dynamic_y_step_type<image_view<L>>
417 using type = image_view<typename gil::dynamic_y_step_type<L>::type>;
424 template <
typename L>
425 struct transposed_type<image_view<L>>
427 using type = image_view<typename transposed_type<L>::type>;
A lightweight object that interprets memory as a 2D array of pixels. Models ImageViewConcept,...
Definition: image_view.hpp:54
auto back() const -> reference
Returns a reference to the last element in raster order.
Definition: image_view.hpp:205
auto front() const -> reference
Returns a reference to the first element in raster order.
Definition: image_view.hpp:195
bool empty() const
Returns true if the view has no elements, false otherwise.
Definition: image_view.hpp:186
void swap(image_view< Loc > &other)
Exchanges the elements of the current view with those of other in constant time.
Definition: image_view.hpp:146
Provides 1D random-access navigation to the pixels of the image. Models: PixelIteratorConcept,...
Definition: iterator_from_2d.hpp:46
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
void swap(boost::gil::packed_channel_reference< BF, FB, NB, M > const x, R &y)
swap for packed_channel_reference
Definition: channel.hpp:583
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36
Returns an integral constant type specifying the number of elements in a color base.
Definition: color_base_algorithm.hpp:42