-/*
- Copyright 2005-2007 Adobe Systems Incorporated
-
- Use, modification and distribution are subject to the Boost Software License,
- Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- http://www.boost.org/LICENSE_1_0.txt).
-
- See http://opensource.adobe.com/gil for most recent version including documentation.
-*/
-
-/*************************************************************************************************/
-
-#ifndef GIL_LOCATOR_H
-#define GIL_LOCATOR_H
-
-
-////////////////////////////////////////////////////////////////////////////////////////
-/// \file
-/// \brief pixel 2D locator
-/// \author Lubomir Bourdev and Hailin Jin \n
-/// Adobe Systems Incorporated
-/// \date 2005-2007 \n September 20, 2006
-///
-////////////////////////////////////////////////////////////////////////////////////////
+//
+// Copyright 2005-2007 Adobe Systems Incorporated
+//
+// Distributed under the Boost Software License, Version 1.0
+// See accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt
+//
+#ifndef BOOST_GIL_LOCATOR_HPP
+#define BOOST_GIL_LOCATOR_HPP
+
+#include <boost/gil/dynamic_step.hpp>
+#include <boost/gil/pixel_iterator.hpp>
+#include <boost/gil/point.hpp>
+
+#include <boost/assert.hpp>
#include <cstddef>
-#include <cassert>
-#include "pixel_iterator.hpp"
-
-////////////////////////////////////////////////////////////////////////////////////////
-/// Pixel 2D LOCATOR
-////////////////////////////////////////////////////////////////////////////////////////
-
namespace boost { namespace gil {
+/// Pixel 2D locator
+
//forward declarations
-template <typename P> ptrdiff_t memunit_step(const P*);
-template <typename P> P* memunit_advanced(const P* p, ptrdiff_t diff);
-template <typename P> P& memunit_advanced_ref(P* p, ptrdiff_t diff);
+template <typename P> std::ptrdiff_t memunit_step(const P*);
+template <typename P> P* memunit_advanced(const P* p, std::ptrdiff_t diff);
+template <typename P> P& memunit_advanced_ref(P* p, std::ptrdiff_t diff);
template <typename Iterator, typename D> struct iterator_add_deref;
-template <typename T> class point2;
+template <typename T> class point;
namespace detail {
// helper class specialized for each axis of pixel_2d_locator
template <std::size_t D, typename Loc> class locator_axis;
}
-template <typename T> struct dynamic_x_step_type;
-template <typename T> struct dynamic_y_step_type;
template <typename T> struct channel_type;
template <typename T> struct color_space_type;
template <typename T> struct is_planar;
template <typename T> struct num_channels;
-// The type of a locator or a view that has X and Y swapped. By default it is the same
-template <typename T> struct transposed_type {
- typedef T type;
+/// Base template for types that model HasTransposedTypeConcept.
+/// The type of a locator or a view that has X and Y swapped.
+/// By default it is the same.
+template <typename LocatorOrView>
+struct transposed_type
+{
+ using type = LocatorOrView;
};
/// \class pixel_2d_locator_base
/// \brief base class for models of PixelLocatorConcept
/// \ingroup PixelLocatorModel PixelBasedModel
///
-/// Pixel locator is similar to a pixel iterator, but allows for 2D navigation of pixels within an image view.
+/// Pixel locator is similar to a pixel iterator, but allows for 2D navigation of pixels within an image view.
/// It has a 2D difference_type and supports random access operations like:
/// \code
/// difference_type offset2(2,3);
/// It is called a locator because it doesn't implement the complete interface of a random access iterator.
/// For example, increment and decrement operations don't make sense (no way to specify dimension).
/// Also 2D difference between two locators cannot be computed without knowledge of the X position within the image.
-///
-/// This base class provides most of the methods and typedefs needed to create a model of a locator. GIL provides two
+///
+/// This base class provides most of the methods and type aliases needed to create a model of a locator. GIL provides two
/// locator models as subclasses of \p pixel_2d_locator_base. A memory-based locator, \p memory_based_2d_locator and a virtual
/// locator, \p virtual_2d_locator.
/// The minimum functionality a subclass must provide is this:
/// \code
/// class my_locator : public pixel_2d_locator_base<my_locator, ..., ...> { // supply the types for x-iterator and y-iterator
-/// typedef ... const_t; // read-only locator
+/// using const_t = ...; // read-only locator
///
/// template <typename Deref> struct add_deref {
-/// typedef ... type; // locator that invokes the Deref dereference object upon pixel access
+/// using type = ...; // locator that invokes the Deref dereference object upon pixel access
/// static type make(const my_locator& loc, const Deref& d);
/// };
///
/// // return the vertical distance to another locator. Some models need the horizontal distance to compute it
/// y_coord_t y_distance_to(const my_locator& loc2, x_coord_t xDiff) const;
///
-/// // return true iff incrementing an x-iterator located at the last column will position it at the first
+/// // return true iff incrementing an x-iterator located at the last column will position it at the first
/// // column of the next row. Some models need the image width to determine that.
/// bool is_1d_traversable(x_coord_t width) const;
/// };
///
template <typename Loc, typename XIterator, typename YIterator> // The concrete subclass, the X-iterator and the Y-iterator
-class pixel_2d_locator_base {
+class pixel_2d_locator_base
+{
public:
- typedef XIterator x_iterator;
- typedef YIterator y_iterator;
+ using x_iterator = XIterator;
+ using y_iterator = YIterator;
- // typedefs required by ConstRandomAccessNDLocatorConcept
+ // aliasesrequired by ConstRandomAccessNDLocatorConcept
static const std::size_t num_dimensions=2;
- typedef typename std::iterator_traits<x_iterator>::value_type value_type;
- typedef typename std::iterator_traits<x_iterator>::reference reference; // result of dereferencing
- typedef typename std::iterator_traits<x_iterator>::difference_type coord_t; // 1D difference type (same for all dimensions)
- typedef point2<coord_t> difference_type; // result of operator-(locator,locator)
- typedef difference_type point_t;
- template <std::size_t D> struct axis {
- typedef typename detail::locator_axis<D,Loc>::coord_t coord_t;
- typedef typename detail::locator_axis<D,Loc>::iterator iterator;
+ using value_type = typename std::iterator_traits<x_iterator>::value_type;
+ using reference = typename std::iterator_traits<x_iterator>::reference; // result of dereferencing
+ using coord_t = typename std::iterator_traits<x_iterator>::difference_type; // 1D difference type (same for all dimensions)
+ using difference_type = point<coord_t>; // result of operator-(locator,locator)
+ using point_t = difference_type;
+ template <std::size_t D> struct axis
+ {
+ using coord_t = typename detail::locator_axis<D,Loc>::coord_t;
+ using iterator = typename detail::locator_axis<D,Loc>::iterator;
};
-// typedefs required by ConstRandomAccess2DLocatorConcept
- typedef typename point_t::template axis<0>::coord_t x_coord_t;
- typedef typename point_t::template axis<1>::coord_t y_coord_t;
+// aliases required by ConstRandomAccess2DLocatorConcept
+ using x_coord_t = typename point_t::template axis<0>::coord_t;
+ using y_coord_t = typename point_t::template axis<1>::coord_t;
bool operator!=(const Loc& p) const { return !(concrete()==p); }
Loc& operator+=(const difference_type& d) { concrete().x()+=d.x; concrete().y()+=d.y; return concrete(); }
Loc& operator-=(const difference_type& d) { concrete().x()-=d.x; concrete().y()-=d.y; return concrete(); }
-
+
Loc operator+(const difference_type& d) const { return xy_at(d); }
Loc operator-(const difference_type& d) const { return xy_at(-d); }
// Some locators can cache 2D coordinates for faster subsequent access. By default there is no caching
- typedef difference_type cached_location_t;
+ using cached_location_t = difference_type;
cached_location_t cache_location(const difference_type& d) const { return d; }
cached_location_t cache_location(x_coord_t dx, y_coord_t dy)const { return difference_type(dx,dy); }
// helper classes for each axis of pixel_2d_locator_base
namespace detail {
- template <typename Loc>
+ template <typename Loc>
class locator_axis<0,Loc> {
- typedef typename Loc::point_t point_t;
+ using point_t = typename Loc::point_t;
public:
- typedef typename point_t::template axis<0>::coord_t coord_t;
- typedef typename Loc::x_iterator iterator;
+ using coord_t = typename point_t::template axis<0>::coord_t;
+ using iterator = typename Loc::x_iterator;
inline iterator& operator()( Loc& loc) const { return loc.x(); }
inline iterator const& operator()(const Loc& loc) const { return loc.x(); }
inline iterator operator()(const Loc& loc, const point_t& d) const { return loc.x_at(d); }
};
- template <typename Loc>
+ template <typename Loc>
class locator_axis<1,Loc> {
- typedef typename Loc::point_t point_t;
+ using point_t = typename Loc::point_t;
public:
- typedef typename point_t::template axis<1>::coord_t coord_t;
- typedef typename Loc::y_iterator iterator;
+ using coord_t = typename point_t::template axis<1>::coord_t;
+ using iterator = typename Loc::y_iterator;
inline iterator& operator()( Loc& loc) const { return loc.y(); }
inline iterator const& operator()(const Loc& loc) const { return loc.y(); }
/// while its base iterator provides horizontal navigation.
///
/// Each instantiation is optimal in terms of size and efficiency.
-/// For example, xy locator over interleaved rgb image results in a step iterator consisting of
-/// one std::ptrdiff_t for the row size and one native pointer (8 bytes total). ++locator.x() resolves to pointer
-/// increment. At the other extreme, a 2D navigation of the even pixels of a planar CMYK image results in a step
-/// iterator consisting of one std::ptrdiff_t for the doubled row size, and one step iterator consisting of
+/// For example, xy locator over interleaved rgb image results in a step iterator consisting of
+/// one std::ptrdiff_t for the row size and one native pointer (8 bytes total). ++locator.x() resolves to pointer
+/// increment. At the other extreme, a 2D navigation of the even pixels of a planar CMYK image results in a step
+/// iterator consisting of one std::ptrdiff_t for the doubled row size, and one step iterator consisting of
/// one std::ptrdiff_t for the horizontal step of two and a CMYK planar_pixel_iterator consisting of 4 pointers (24 bytes).
/// In this case ++locator.x() results in four native pointer additions.
///
template <typename StepIterator>
class memory_based_2d_locator : public pixel_2d_locator_base<memory_based_2d_locator<StepIterator>, typename iterator_adaptor_get_base<StepIterator>::type, StepIterator> {
- typedef memory_based_2d_locator<StepIterator> this_t;
+ using this_t = memory_based_2d_locator<StepIterator>;
GIL_CLASS_REQUIRE(StepIterator, boost::gil, StepIteratorConcept)
public:
- typedef pixel_2d_locator_base<memory_based_2d_locator<StepIterator>, typename iterator_adaptor_get_base<StepIterator>::type, StepIterator> parent_t;
- typedef memory_based_2d_locator<typename const_iterator_type<StepIterator>::type> const_t; // same as this type, but over const values
-
- typedef typename parent_t::coord_t coord_t;
- typedef typename parent_t::x_coord_t x_coord_t;
- typedef typename parent_t::y_coord_t y_coord_t;
- typedef typename parent_t::x_iterator x_iterator;
- typedef typename parent_t::y_iterator y_iterator;
- typedef typename parent_t::difference_type difference_type;
- typedef typename parent_t::reference reference;
-
- template <typename Deref> struct add_deref {
- typedef memory_based_2d_locator<typename iterator_add_deref<StepIterator,Deref>::type> type;
- static type make(const memory_based_2d_locator<StepIterator>& loc, const Deref& nderef) {
- return type(iterator_add_deref<StepIterator,Deref>::make(loc.y(),nderef));
+ using parent_t = pixel_2d_locator_base<memory_based_2d_locator<StepIterator>, typename iterator_adaptor_get_base<StepIterator>::type, StepIterator>;
+ using const_t = memory_based_2d_locator<typename const_iterator_type<StepIterator>::type>; // same as this type, but over const values
+
+ using coord_t = typename parent_t::coord_t;
+ using x_coord_t = typename parent_t::x_coord_t;
+ using y_coord_t = typename parent_t::y_coord_t;
+ using x_iterator = typename parent_t::x_iterator;
+ using y_iterator = typename parent_t::y_iterator;
+ using difference_type = typename parent_t::difference_type;
+ using reference = typename parent_t::reference;
+
+ template <typename Deref> struct add_deref
+ {
+ using type = memory_based_2d_locator<typename iterator_add_deref<StepIterator,Deref>::type>;
+ static type make(const memory_based_2d_locator<StepIterator>& loc, const Deref& nderef) {
+ return type(iterator_add_deref<StepIterator,Deref>::make(loc.y(),nderef));
}
};
memory_based_2d_locator(x_iterator xit, std::ptrdiff_t row_bytes) : _p(xit,row_bytes) {}
template <typename X> memory_based_2d_locator(const memory_based_2d_locator<X>& pl) : _p(pl._p) {}
memory_based_2d_locator(const memory_based_2d_locator& pl) : _p(pl._p) {}
+ memory_based_2d_locator& operator=(memory_based_2d_locator const& other) = default;
bool operator==(const this_t& p) const { return _p==p._p; }
x_iterator& x() { return _p.base(); }
y_iterator& y() { return _p; }
- // These are faster versions of functions already provided in the superclass
- x_iterator x_at (x_coord_t dx, y_coord_t dy) const { return memunit_advanced(x(), offset(dx,dy)); }
+ // These are faster versions of functions already provided in the superclass
+ x_iterator x_at (x_coord_t dx, y_coord_t dy) const { return memunit_advanced(x(), offset(dx,dy)); }
x_iterator x_at (const difference_type& d) const { return memunit_advanced(x(), offset(d.x,d.y)); }
this_t xy_at (x_coord_t dx, y_coord_t dy) const { return this_t(x_at( dx , dy ), row_size()); }
this_t xy_at (const difference_type& d) const { return this_t(x_at( d.x, d.y), row_size()); }
this_t& operator-=(const difference_type& d) { memunit_advance(x(),offset(-d.x,-d.y)); return *this; }
// Memory-based locators can have 1D caching of 2D relative coordinates
- typedef std::ptrdiff_t cached_location_t; // type used to store relative location (to allow for more efficient repeated access)
+ using cached_location_t = std::ptrdiff_t; // type used to store relative location (to allow for more efficient repeated access)
cached_location_t cache_location(const difference_type& d) const { return offset(d.x,d.y); }
cached_location_t cache_location(x_coord_t dx, y_coord_t dy)const { return offset(dx,dy); }
reference operator[](const cached_location_t& loc) const { return memunit_advanced_ref(x(),loc); }
bool is_1d_traversable(x_coord_t width) const { return row_size()-pixel_size()*width==0; } // is there no gap at the end of each row?
// Returns the vertical distance (it2.y-it1.y) between two x_iterators given the difference of their x positions
- std::ptrdiff_t y_distance_to(const this_t& p2, x_coord_t xDiff) const {
- std::ptrdiff_t rowDiff=memunit_distance(x(),p2.x())-pixel_size()*xDiff;
- assert(( rowDiff % row_size())==0);
+ std::ptrdiff_t y_distance_to(this_t const& p2, x_coord_t xDiff) const
+ {
+ std::ptrdiff_t rowDiff = memunit_distance(x(), p2.x()) - pixel_size() * xDiff;
+ BOOST_ASSERT((rowDiff % row_size()) == 0);
return rowDiff / row_size();
}
template <typename SI>
struct dynamic_x_step_type<memory_based_2d_locator<SI> > {
private:
- typedef typename iterator_adaptor_get_base<SI>::type base_iterator_t;
- typedef typename dynamic_x_step_type<base_iterator_t>::type base_iterator_step_t;
- typedef typename iterator_adaptor_rebind<SI, base_iterator_step_t>::type dynamic_step_base_t;
+ using base_iterator_t = typename iterator_adaptor_get_base<SI>::type;
+ using base_iterator_step_t = typename dynamic_x_step_type<base_iterator_t>::type;
+ using dynamic_step_base_t = typename iterator_adaptor_rebind<SI, base_iterator_step_t>::type;
public:
- typedef memory_based_2d_locator<dynamic_step_base_t> type;
+ using type = memory_based_2d_locator<dynamic_step_base_t>;
};
/////////////////////////////
template <typename SI>
struct dynamic_y_step_type<memory_based_2d_locator<SI> > {
- typedef memory_based_2d_locator<SI> type;
+ using type = memory_based_2d_locator<SI>;
};
-
} } // namespace boost::gil
#endif