planar_pixel_iterator.hpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2005-2007 Adobe Systems Incorporated
00003    
00004     Use, modification and distribution are subject to the Boost Software License,
00005     Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
00006     http://www.boost.org/LICENSE_1_0.txt).
00007 
00008     See http://opensource.adobe.com/gil for most recent version including documentation.
00009 */
00010 
00011 /*************************************************************************************************/
00012 
00013 #ifndef GIL_PLANAR_PTR_H
00014 #define GIL_PLANAR_PTR_H
00015 
00024 
00025 #include <cassert>
00026 #include <iterator>
00027 #include <boost/iterator/iterator_facade.hpp>
00028 #include "gil_config.hpp"
00029 #include "pixel.hpp"
00030 #include "step_iterator.hpp"
00031 
00032 namespace boost { namespace gil {
00033 
00034 //forward declaration (as this file is included in planar_pixel_reference.hpp)
00035 template <typename ChannelReference, typename ColorSpace> 
00036 struct planar_pixel_reference;
00037 
00042 
00046 
00054 template <typename ChannelPtr, typename ColorSpace>
00055 struct planar_pixel_iterator : public iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
00056                                                       pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
00057                                                       random_access_traversal_tag,
00058                                                       const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> >,
00059                                public detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value > {
00060 private:
00061     typedef iterator_facade<planar_pixel_iterator<ChannelPtr,ColorSpace>,
00062                             pixel<typename std::iterator_traits<ChannelPtr>::value_type,layout<ColorSpace> >,
00063                             random_access_traversal_tag,
00064                             const planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> > parent_t;
00065     typedef detail::homogeneous_color_base<ChannelPtr,layout<ColorSpace>,mpl::size<ColorSpace>::value> color_base_parent_t;
00066     typedef typename std::iterator_traits<ChannelPtr>::value_type channel_t;
00067 public:
00068     typedef typename parent_t::value_type                 value_type;
00069     typedef typename parent_t::reference                  reference;
00070     typedef typename parent_t::difference_type            difference_type;
00071 
00072     planar_pixel_iterator() : color_base_parent_t(0) {} 
00073     planar_pixel_iterator(bool) {}        // constructor that does not fill with zero (for performance)
00074 
00075     planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1) : color_base_parent_t(v0,v1) {}
00076     planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2) : color_base_parent_t(v0,v1,v2) {}
00077     planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3) : color_base_parent_t(v0,v1,v2,v3) {}
00078     planar_pixel_iterator(const ChannelPtr& v0, const ChannelPtr& v1, const ChannelPtr& v2, const ChannelPtr& v3, const ChannelPtr& v4) : color_base_parent_t(v0,v1,v2,v3,v4) {}
00079 
00080     template <typename IC1,typename C1> 
00081     planar_pixel_iterator(const planar_pixel_iterator<IC1,C1>& ptr) : color_base_parent_t(ptr) {}
00082 
00083 
00087     template <typename P> 
00088     planar_pixel_iterator(P* pix) : color_base_parent_t(pix, true) {
00089         function_requires<PixelsCompatibleConcept<P,value_type> >();
00090     }
00091 
00092     struct address_of { template <typename T> T* operator()(T& t) { return &t; } };
00093     template <typename P> 
00094     planar_pixel_iterator& operator=(P* pix) {
00095         function_requires<PixelsCompatibleConcept<P,value_type> >();
00096         static_transform(*pix,*this, address_of());
00097 
00098         // PERFORMANCE_CHECK: Compare to this:
00099         //this->template semantic_at_c<0>()=&pix->template semantic_at_c<0>();
00100         //this->template semantic_at_c<1>()=&pix->template semantic_at_c<1>();
00101         //this->template semantic_at_c<2>()=&pix->template semantic_at_c<2>();
00102         return *this;
00103     }
00104 
00107     reference operator[](difference_type d)       const { return memunit_advanced_ref(*this,d*sizeof(channel_t));}
00108 
00109     reference operator->()                        const { return **this; }
00110 
00111     // PERFORMANCE_CHECK: Remove?
00112     bool operator< (const planar_pixel_iterator& ptr)   const { return at_c<0>(*this)< at_c<0>(ptr); }
00113     bool operator!=(const planar_pixel_iterator& ptr)   const { return at_c<0>(*this)!=at_c<0>(ptr); }
00114 private:
00115     friend class boost::iterator_core_access;
00116 
00117     void increment()            { static_transform(*this,*this,detail::inc<ChannelPtr>()); }
00118     void decrement()            { static_transform(*this,*this,detail::dec<ChannelPtr>()); }
00119     void advance(ptrdiff_t d)   { static_transform(*this,*this,std::bind2nd(detail::plus_asymmetric<ChannelPtr,ptrdiff_t>(),d)); }
00120     reference dereference() const { return this->template deref<reference>(); }
00121 
00122     ptrdiff_t distance_to(const planar_pixel_iterator& it) const { return at_c<0>(it)-at_c<0>(*this); }
00123     bool equal(const planar_pixel_iterator& it) const { return at_c<0>(*this)==at_c<0>(it); }
00124 };
00125 
00126 namespace detail {
00127     template <typename IC> struct channel_iterator_is_mutable : public mpl::true_ {};
00128     template <typename T>  struct channel_iterator_is_mutable<const T*> : public mpl::false_ {};
00129 }
00130 
00131 template <typename IC, typename C> 
00132 struct const_iterator_type<planar_pixel_iterator<IC,C> > { 
00133 private:
00134     typedef typename std::iterator_traits<IC>::value_type channel_t;
00135 public:
00136     typedef planar_pixel_iterator<typename channel_traits<channel_t>::const_pointer,C> type; 
00137 };
00138 
00139 // The default implementation when the iterator is a C pointer is to use the standard constness semantics
00140 template <typename IC, typename C> 
00141 struct iterator_is_mutable<planar_pixel_iterator<IC,C> > : public detail::channel_iterator_is_mutable<IC> {};
00142 
00144 //  ColorBasedConcept
00146 
00147 template <typename IC, typename C, int K>  
00148 struct kth_element_type<planar_pixel_iterator<IC,C>, K> {
00149     typedef IC type;
00150 };
00151 
00152 template <typename IC, typename C, int K>  
00153 struct kth_element_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<IC> {};
00154 
00155 template <typename IC, typename C, int K>  
00156 struct kth_element_const_reference_type<planar_pixel_iterator<IC,C>, K> : public add_reference<typename add_const<IC>::type> {};
00157 
00159 //  HomogeneousPixelBasedConcept
00161 
00162 template <typename IC, typename C>
00163 struct color_space_type<planar_pixel_iterator<IC,C> > {
00164     typedef C type;
00165 };
00166 
00167 template <typename IC, typename C>
00168 struct channel_mapping_type<planar_pixel_iterator<IC,C> > : public channel_mapping_type<typename planar_pixel_iterator<IC,C>::value_type> {};
00169 
00170 template <typename IC, typename C>
00171 struct is_planar<planar_pixel_iterator<IC,C> > : public mpl::true_ {};
00172 
00173 template <typename IC, typename C>
00174 struct channel_type<planar_pixel_iterator<IC,C> > {
00175     typedef typename std::iterator_traits<IC>::value_type type;
00176 };
00177 
00179 //  MemoryBasedIteratorConcept
00181 
00182 template <typename IC, typename C>
00183 inline std::ptrdiff_t memunit_step(const planar_pixel_iterator<IC,C>&) { return sizeof(typename std::iterator_traits<IC>::value_type); }
00184 
00185 template <typename IC, typename C>
00186 inline std::ptrdiff_t memunit_distance(const planar_pixel_iterator<IC,C>& p1, const planar_pixel_iterator<IC,C>& p2) { 
00187     return memunit_distance(at_c<0>(p1),at_c<0>(p2)); 
00188 }
00189 
00190 template <typename IC>
00191 struct memunit_advance_fn {
00192     memunit_advance_fn(std::ptrdiff_t diff) : _diff(diff) {}
00193     IC operator()(const IC& p) const { return memunit_advanced(p,_diff); }
00194 
00195     std::ptrdiff_t _diff;
00196 };
00197 
00198 template <typename IC, typename C>
00199 inline void memunit_advance(planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) { 
00200     static_transform(p, p, memunit_advance_fn<IC>(diff));
00201 }
00202 
00203 template <typename IC, typename C>
00204 inline planar_pixel_iterator<IC,C> memunit_advanced(const planar_pixel_iterator<IC,C>& p, std::ptrdiff_t diff) {
00205     planar_pixel_iterator<IC,C> ret=p;
00206     memunit_advance(ret, diff);
00207     return ret;
00208 }
00209 
00210 template <typename ChannelPtr, typename ColorSpace>
00211 inline planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace> 
00212     memunit_advanced_ref(const planar_pixel_iterator<ChannelPtr,ColorSpace>& ptr, std::ptrdiff_t diff) {
00213     return planar_pixel_reference<typename std::iterator_traits<ChannelPtr>::reference,ColorSpace>(ptr, diff);
00214 }
00215 
00217 //  HasDynamicXStepTypeConcept
00219 
00220 template <typename IC, typename C>
00221 struct dynamic_x_step_type<planar_pixel_iterator<IC,C> > {
00222     typedef memory_based_step_iterator<planar_pixel_iterator<IC,C> > type;
00223 };
00224 
00225 } }  // namespace boost::gil
00226 
00227 #endif

Copyright © 2006-2007 Adobe Systems Incorporated.

Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy.

Search powered by Google