// // 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_PIXEL_ITERATOR_ADAPTOR_HPP #define BOOST_GIL_PIXEL_ITERATOR_ADAPTOR_HPP #include #include #include #include namespace boost { namespace gil { /// \defgroup PixelIteratorModelDerefPtr dereference_iterator_adaptor /// \ingroup PixelIteratorModel /// \brief An iterator that invokes a provided function object upon dereference. Models: IteratorAdaptorConcept, PixelIteratorConcept /// \ingroup PixelIteratorModelDerefPtr PixelBasedModel /// \brief An adaptor over an existing iterator that provides for custom filter on dereferencing the object. Models: IteratorAdaptorConcept, PixelIteratorConcept template // Models Returns the result of dereferencing a given iterator of type Iterator class dereference_iterator_adaptor : public iterator_adaptor, Iterator, typename DFn::value_type, typename std::iterator_traits::iterator_category, typename DFn::reference, use_default> { DFn _deref_fn; public: using parent_t = iterator_adaptor, Iterator, typename DFn::value_type, typename std::iterator_traits::iterator_category, typename DFn::reference, use_default>; using reference = typename DFn::result_type; using difference_type = typename std::iterator_traits::difference_type; using dereference_fn = DFn; dereference_iterator_adaptor() {} template dereference_iterator_adaptor(const dereference_iterator_adaptor& dit) : parent_t(dit.base()), _deref_fn(dit._deref_fn) {} dereference_iterator_adaptor(Iterator it, DFn deref_fn=DFn()) : parent_t(it), _deref_fn(deref_fn) {} template dereference_iterator_adaptor(const dereference_iterator_adaptor& it) : parent_t(it.base()), _deref_fn(it._deref_fn) {} /// For some reason operator[] provided by iterator_facade returns a custom class that is convertible to reference /// We require our own reference because it is registered in iterator_traits reference operator[](difference_type d) const { return *(*this+d);} // although iterator_adaptor defines these, the default implementation computes distance and compares for zero. // it is often faster to just apply the relation operator to the base bool operator> (const dereference_iterator_adaptor& p) const { return this->base_reference()> p.base_reference(); } bool operator< (const dereference_iterator_adaptor& p) const { return this->base_reference()< p.base_reference(); } bool operator>=(const dereference_iterator_adaptor& p) const { return this->base_reference()>=p.base_reference(); } bool operator<=(const dereference_iterator_adaptor& p) const { return this->base_reference()<=p.base_reference(); } bool operator==(const dereference_iterator_adaptor& p) const { return this->base_reference()==p.base_reference(); } bool operator!=(const dereference_iterator_adaptor& p) const { return this->base_reference()!=p.base_reference(); } Iterator& base() { return this->base_reference(); } const Iterator& base() const { return this->base_reference(); } const DFn& deref_fn() const { return _deref_fn; } private: template friend class dereference_iterator_adaptor; friend class boost::iterator_core_access; reference dereference() const { return _deref_fn(*(this->base_reference())); } }; template struct const_iterator_type > { using type = dereference_iterator_adaptor::type,typename DFn::const_t>; }; template struct iterator_is_mutable> : std::integral_constant {}; template struct is_iterator_adaptor> : std::true_type {}; template struct iterator_adaptor_get_base> { using type = I; }; template struct iterator_adaptor_rebind,NewBaseIterator> { using type = dereference_iterator_adaptor; }; ///////////////////////////// // PixelBasedConcept ///////////////////////////// template struct color_space_type > : public color_space_type {}; template struct channel_mapping_type > : public channel_mapping_type {}; template struct is_planar > : public is_planar {}; template struct channel_type > : public channel_type {}; ///////////////////////////// // MemoryBasedIteratorConcept ///////////////////////////// template struct byte_to_memunit> : public byte_to_memunit {}; template inline auto memunit_step(dereference_iterator_adaptor const& p) -> typename std::iterator_traits::difference_type { return memunit_step(p.base()); } template inline auto memunit_distance(dereference_iterator_adaptor const& p1, dereference_iterator_adaptor const& p2) -> typename std::iterator_traits::difference_type { return memunit_distance(p1.base(),p2.base()); } template inline void memunit_advance(dereference_iterator_adaptor& p, typename std::iterator_traits::difference_type diff) { memunit_advance(p.base(), diff); } template inline auto memunit_advanced(dereference_iterator_adaptor const& p, typename std::iterator_traits::difference_type diff) -> dereference_iterator_adaptor { return dereference_iterator_adaptor(memunit_advanced(p.base(), diff), p.deref_fn()); } template inline auto memunit_advanced_ref(dereference_iterator_adaptor const& p, typename std::iterator_traits::difference_type diff) -> typename std::iterator_traits >::reference { return *memunit_advanced(p, diff); } ///////////////////////////// // HasDynamicXStepTypeConcept ///////////////////////////// template struct dynamic_x_step_type > { using type = dereference_iterator_adaptor::type,DFn>; }; /// \brief Returns the type (and creates an instance) of an iterator that invokes the given dereference adaptor upon dereferencing /// \ingroup PixelIteratorModelDerefPtr template struct iterator_add_deref { BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept) using type = dereference_iterator_adaptor; static type make(const Iterator& it, const Deref& d) { return type(it,d); } }; /// \ingroup PixelIteratorModelDerefPtr /// \brief For dereference iterator adaptors, compose the new function object after the old one template struct iterator_add_deref,Deref> { // BOOST_GIL_CLASS_REQUIRE(Deref, boost::gil, PixelDereferenceAdaptorConcept) using type = dereference_iterator_adaptor>; static type make(const dereference_iterator_adaptor& it, const Deref& d) { return type(it.base(),deref_compose(d,it.deref_fn())); } }; }} // namespace boost::gil #endif