123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- #ifndef BOOST_GIL_IMAGE_VIEW_HPP
- #define BOOST_GIL_IMAGE_VIEW_HPP
- #include <boost/gil/dynamic_step.hpp>
- #include <boost/gil/iterator_from_2d.hpp>
- #include <boost/assert.hpp>
- #include <cstddef>
- #include <iterator>
- namespace boost { namespace gil {
- template <typename Loc>
- class image_view
- {
- public:
-
- static const std::size_t num_dimensions=2;
- using value_type = typename Loc::value_type;
- using reference = typename Loc::reference;
- using coord_t = typename Loc::coord_t;
- using difference_type = coord_t;
- using point_t = typename Loc::point_t;
- using locator = Loc;
- using const_t = image_view<typename Loc::const_t>;
- template <std::size_t D> struct axis
- {
- using coord_t = typename Loc::template axis<D>::coord_t;
- using iterator = typename Loc::template axis<D>::iterator;
- };
- using iterator = iterator_from_2d<Loc>;
- using const_iterator = typename const_t::iterator;
- using const_reference = typename const_t::reference;
- using pointer = typename std::iterator_traits<iterator>::pointer;
- using reverse_iterator = std::reverse_iterator<iterator>;
- using size_type = std::size_t;
-
- using xy_locator = locator;
- using x_iterator = typename xy_locator::x_iterator;
- using y_iterator = typename xy_locator::y_iterator;
- using x_coord_t = typename xy_locator::x_coord_t;
- using y_coord_t = typename xy_locator::y_coord_t;
- template <typename Deref>
- struct add_deref
- {
- using type = image_view<typename Loc::template add_deref<Deref>::type>;
- static type make(image_view<Loc> const& view, Deref const& d)
- {
- return type(view.dimensions(), Loc::template add_deref<Deref>::make(view.pixels(), d));
- }
- };
- image_view() : _dimensions(0,0) {}
- image_view(image_view const& img_view)
- : _dimensions(img_view.dimensions()), _pixels(img_view.pixels())
- {}
- template <typename View>
- image_view(View const& view) : _dimensions(view.dimensions()), _pixels(view.pixels()) {}
- template <typename L2>
- image_view(point_t const& dims, L2 const& loc) : _dimensions(dims), _pixels(loc) {}
- template <typename L2>
- image_view(coord_t width, coord_t height, L2 const& loc)
- : _dimensions(x_coord_t(width), y_coord_t(height)), _pixels(loc)
- {}
- template <typename View>
- image_view& operator=(View const& view)
- {
- _pixels = view.pixels();
- _dimensions = view.dimensions();
- return *this;
- }
- image_view& operator=(image_view const& view)
- {
-
- _pixels = view.pixels();
- _dimensions = view.dimensions();
- return *this;
- }
- template <typename View>
- bool operator==(View const &view) const
- {
- return pixels() == view.pixels() && dimensions() == view.dimensions();
- }
- template <typename View>
- bool operator!=(View const& view) const
- {
- return !(*this == view);
- }
- template <typename L2>
- friend void swap(image_view<L2> &lhs, image_view<L2> &rhs);
-
-
-
-
-
- void swap(image_view<Loc>& other)
- {
- using boost::gil::swap;
- swap(*this, other);
- }
- auto dimensions() const -> point_t const&
- {
- return _dimensions;
- }
- auto pixels() const -> locator const&
- {
- return _pixels;
- }
- auto width() const -> x_coord_t
- {
- return dimensions().x;
- }
- auto height() const -> y_coord_t
- {
- return dimensions().y;
- }
- auto num_channels() const -> std::size_t
- {
- return gil::num_channels<value_type>::value;
- }
- bool is_1d_traversable() const
- {
- return _pixels.is_1d_traversable(width());
- }
-
-
-
-
- bool empty() const
- {
- return !(width() > 0 && height() > 0);
- }
-
-
-
-
- auto front() const -> reference
- {
- BOOST_ASSERT(!empty());
- return *begin();
- }
-
-
-
-
- auto back() const -> reference
- {
- BOOST_ASSERT(!empty());
- return *rbegin();
- }
-
-
- auto size() const -> size_type
- {
- return width() * height();
- }
- auto begin() const -> iterator
- {
- return iterator(_pixels, _dimensions.x);
- }
- auto end() const -> iterator
- {
-
- return begin() + static_cast<difference_type>(size());
- }
- auto rbegin() const -> reverse_iterator
- {
- return reverse_iterator(end());
- }
- auto rend() const -> reverse_iterator
- {
- return reverse_iterator(begin());
- }
- auto operator[](difference_type i) const -> reference
- {
- BOOST_ASSERT(i < static_cast<difference_type>(size()));
- return begin()[i];
- }
- auto at(difference_type i) const -> iterator
- {
-
- BOOST_ASSERT(i < static_cast<difference_type>(size()));
- return begin() + i;
- }
- auto at(point_t const& p) const -> iterator
- {
-
- BOOST_ASSERT(0 <= p.x && p.x < width());
- BOOST_ASSERT(0 <= p.y && p.y < height());
- return begin() + p.y * width() + p.x;
- }
- auto at(x_coord_t x, y_coord_t y) const -> iterator
- {
-
- BOOST_ASSERT(0 <= x && x < width());
- BOOST_ASSERT(0 <= y && y < height());
- return begin() + y * width() + x;
- }
-
-
-
- auto operator()(point_t const& p) const -> reference
- {
- BOOST_ASSERT(0 <= p.x && p.x < width());
- BOOST_ASSERT(0 <= p.y && p.y < height());
- return _pixels(p.x, p.y);
- }
- auto operator()(x_coord_t x, y_coord_t y) const -> reference
- {
- BOOST_ASSERT(0 <= x && x < width());
- BOOST_ASSERT(0 <= y && y < height());
- return _pixels(x, y);
- }
- template <std::size_t D>
- auto axis_iterator(point_t const& p) const -> typename axis<D>::iterator
- {
-
- BOOST_ASSERT(0 <= p.x && p.x <= width());
- BOOST_ASSERT(0 <= p.y && p.y <= height());
- return _pixels.template axis_iterator<D>(p);
- }
- auto xy_at(x_coord_t x, y_coord_t y) const -> xy_locator
- {
-
- BOOST_ASSERT(x < width());
- BOOST_ASSERT(y <= height());
- return _pixels + point_t(x, y);
- }
- auto xy_at(point_t const& p) const -> xy_locator
- {
-
- BOOST_ASSERT(p.x < width());
- BOOST_ASSERT(p.y < height());
- return _pixels + p;
- }
-
-
-
- auto x_at(x_coord_t x, y_coord_t y) const -> x_iterator
- {
- BOOST_ASSERT(0 <= x && x <= width());
- BOOST_ASSERT(0 <= y && y < height());
- return _pixels.x_at(x, y);
- }
- auto x_at(point_t const& p) const -> x_iterator
- {
- BOOST_ASSERT(0 <= p.x && p.x <= width());
- BOOST_ASSERT(0 <= p.y && p.y < height());
- return _pixels.x_at(p);
- }
- auto row_begin(y_coord_t y) const -> x_iterator
- {
- BOOST_ASSERT(0 <= y && y < height());
- return x_at(0, y);
- }
- auto row_end(y_coord_t y) const -> x_iterator
- {
- BOOST_ASSERT(0 <= y && y < height());
- return x_at(width(), y);
- }
-
-
-
- auto y_at(x_coord_t x, y_coord_t y) const -> y_iterator
- {
- BOOST_ASSERT(0 <= x && x < width());
- BOOST_ASSERT(0 <= y && y <= height());
- return xy_at(x, y).y();
- }
- auto y_at(point_t const& p) const -> y_iterator
- {
- BOOST_ASSERT(0 <= p.x && p.x < width());
- BOOST_ASSERT(0 <= p.y && p.y <= height());
- return xy_at(p).y();
- }
- auto col_begin(x_coord_t x) const -> y_iterator
- {
- BOOST_ASSERT(0 <= x && x < width());
- return y_at(x, 0);
- }
- auto col_end(x_coord_t x) const -> y_iterator
- {
- BOOST_ASSERT(0 <= x && x < width());
- return y_at(x, height());
- }
-
- private:
- template <typename L2>
- friend class image_view;
- point_t _dimensions;
- xy_locator _pixels;
- };
- template <typename L2>
- inline void swap(image_view<L2>& x, image_view<L2>& y) {
- using std::swap;
- swap(x._dimensions,y._dimensions);
- swap(x._pixels, y._pixels);
- }
- template <typename L>
- struct channel_type<image_view<L> > : public channel_type<L> {};
- template <typename L>
- struct color_space_type<image_view<L> > : public color_space_type<L> {};
- template <typename L>
- struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {};
- template <typename L>
- struct is_planar<image_view<L> > : public is_planar<L> {};
- template <typename L>
- struct dynamic_x_step_type<image_view<L>>
- {
- using type = image_view<typename gil::dynamic_x_step_type<L>::type>;
- };
- template <typename L>
- struct dynamic_y_step_type<image_view<L>>
- {
- using type = image_view<typename gil::dynamic_y_step_type<L>::type>;
- };
- template <typename L>
- struct transposed_type<image_view<L>>
- {
- using type = image_view<typename transposed_type<L>::type>;
- };
- }}
- #endif
|