image_view.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 #ifndef GIL_IMAGE_VIEW_H
00013 #define GIL_IMAGE_VIEW_H
00014 
00023 
00024 #include <cstddef>
00025 #include <iterator>
00026 #include "gil_config.hpp"
00027 #include "iterator_from_2d.hpp"
00028 
00029 //#ifdef _MSC_VER
00030 //#pragma warning(push)
00031 //#pragma warning(disable : 4244)     // conversion from 'gil::image<V,Alloc>::coord_t' to 'int', possible loss of data (visual studio compiler doesn't realize that the two types are the same)
00032 //#endif
00033 
00034 namespace boost { namespace gil {
00035 
00067 template <typename Loc>     // Models 2D Pixel Locator
00068 class image_view {
00069 public:
00070 
00071 // typedefs required by ConstRandomAccessNDImageViewConcept
00072     static const std::size_t num_dimensions=2;
00073     typedef typename Loc::value_type                 value_type;
00074     typedef typename Loc::reference                  reference;       // result of dereferencing
00075     typedef typename Loc::coord_t                    coord_t;      // 1D difference type (same for all dimensions)
00076     typedef coord_t                                  difference_type; // result of operator-(1d_iterator,1d_iterator)
00077     typedef typename Loc::point_t                    point_t;
00078     typedef Loc                                      locator;
00079     typedef image_view<typename Loc::const_t>        const_t;      // same as this type, but over const values
00080     template <std::size_t D> struct axis {
00081         typedef typename Loc::template axis<D>::coord_t  coord_t;     // difference_type along each dimension
00082         typedef typename Loc::template axis<D>::iterator iterator;       // 1D iterator type along each dimension
00083     };
00084     typedef iterator_from_2d<Loc>                    iterator;       // 1D iterator type for each pixel left-to-right inside top-to-bottom
00085     typedef std::reverse_iterator<iterator>          reverse_iterator;
00086     typedef std::size_t                              size_type;
00087 
00088 // typedefs required by ConstRandomAccess2DImageViewConcept
00089     typedef locator                                  xy_locator;
00090     typedef typename xy_locator::x_iterator          x_iterator;     // pixel iterator along a row
00091     typedef typename xy_locator::y_iterator          y_iterator;     // pixel iterator along a column
00092     typedef typename xy_locator::x_coord_t           x_coord_t;
00093     typedef typename xy_locator::y_coord_t           y_coord_t;
00094 
00095     template <typename Deref> struct add_deref {
00096         typedef image_view<typename Loc::template add_deref<Deref>::type> type;
00097         static type make(const image_view<Loc>& iv, const Deref& d) { return type(iv.dimensions(), Loc::template add_deref<Deref>::make(iv.pixels(),d)); }
00098     };
00099 
00100     image_view() : _dimensions(0,0) {}
00101     template <typename View> image_view(const View& iv)                                    : _dimensions(iv.dimensions()), _pixels(iv.pixels()) {}
00102 
00103     template <typename L2> image_view(const point_t& sz            , const L2& loc)        : _dimensions(sz),          _pixels(loc) {}
00104     template <typename L2> image_view(coord_t width, coord_t height, const L2& loc)        : _dimensions(x_coord_t(width),y_coord_t(height)), _pixels(loc) {}
00105 
00106     template <typename View> image_view& operator=(const View& iv)  { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
00107     image_view& operator=(const image_view& iv)                     { _pixels=iv.pixels(); _dimensions=iv.dimensions(); return *this; }
00108 
00109     template <typename View> bool operator==(const View& v) const   { return pixels()==v.pixels() && dimensions()==v.dimensions(); }
00110     template <typename View> bool operator!=(const View& v) const   { return !(*this==v); }
00111 
00112     template <typename L2> friend void swap(image_view<L2>& x, image_view<L2>& y);
00113 
00114     const point_t&   dimensions()            const { return _dimensions; }
00115     const locator&   pixels()                const { return _pixels; }
00116     x_coord_t        width()                 const { return dimensions().x; }
00117     y_coord_t        height()                const { return dimensions().y; }
00118     std::size_t      num_channels()          const { return gil::num_channels<value_type>::value; }
00119     bool             is_1d_traversable()     const { return _pixels.is_1d_traversable(width()); }
00120 
00121     //\{@
00123     size_type           size()               const { return width()*height(); }  
00124     iterator            begin()              const { return iterator(_pixels,_dimensions.x); }
00125     iterator            end()                const { return begin()+(difference_type)size(); }    // potential performance problem!
00126     reverse_iterator    rbegin()             const { return reverse_iterator(end()); }
00127     reverse_iterator    rend()               const { return reverse_iterator(begin()); }
00128     reference operator[](difference_type i)  const { return begin()[i]; }        // potential performance problem!
00129     iterator            at(difference_type i)const { return begin()+i; }
00130     iterator            at(const point_t& p) const { return begin()+p.y*width()+p.x; }
00131     iterator            at(x_coord_t x, y_coord_t y)const { return begin()+y*width()+x; }
00132 
00133     //\}@
00134 
00135     //\{@
00137     reference operator()(const point_t& p)        const { return _pixels(p.x,p.y); }
00138     reference operator()(x_coord_t x, y_coord_t y)const { return _pixels(x,y); }
00139     template <std::size_t D> typename axis<D>::iterator axis_iterator(const point_t& p) const { return _pixels.axis_iterator<D>(p); }
00140     xy_locator xy_at(x_coord_t x, y_coord_t y)    const { return _pixels+point_t(x_coord_t(x),y_coord_t(y)); }
00141     locator    xy_at(const point_t& p)            const { return _pixels+p; }
00142     //\}@
00143 
00144     //\{@
00146     x_iterator x_at(x_coord_t x, y_coord_t y)     const { return _pixels.x_at(x,y); }
00147     x_iterator x_at(const point_t& p)             const { return _pixels.x_at(p); }
00148     x_iterator row_begin(y_coord_t y)             const { return x_at(0,y); }
00149     x_iterator row_end(y_coord_t y)               const { return x_at(width(),y); }
00150     //\}@
00151 
00152     //\{@
00154     y_iterator y_at(x_coord_t x, y_coord_t y)     const { return xy_at(x,y).y(); }
00155     y_iterator y_at(const point_t& p)             const { return xy_at(p).y(); }
00156     y_iterator col_begin(x_coord_t x)             const { return y_at(x,0); }
00157     y_iterator col_end(x_coord_t x)               const { return y_at(x,height()); }
00158     //\}@
00159 
00160 private:
00161     template <typename L2> friend class image_view;
00162 
00163     point_t    _dimensions;
00164     xy_locator _pixels;
00165 };
00166 
00167 template <typename L2> 
00168 inline void swap(image_view<L2>& x, image_view<L2>& y) { 
00169     using std::swap;
00170     swap(x._dimensions,y._dimensions); 
00171     swap(x._pixels, y._pixels);            // TODO: Extend further
00172 }
00173 
00175 //  PixelBasedConcept
00177 
00178 template <typename L>
00179 struct channel_type<image_view<L> > : public channel_type<L> {}; 
00180 
00181 template <typename L>
00182 struct color_space_type<image_view<L> > : public color_space_type<L> {}; 
00183 
00184 template <typename L>
00185 struct channel_mapping_type<image_view<L> > : public channel_mapping_type<L> {}; 
00186 
00187 template <typename L>
00188 struct is_planar<image_view<L> > : public is_planar<L> {}; 
00189 
00191 //  HasDynamicXStepTypeConcept
00193 
00194 template <typename L>
00195 struct dynamic_x_step_type<image_view<L> > {
00196     typedef image_view<typename dynamic_x_step_type<L>::type> type;
00197 };
00198 
00200 //  HasDynamicYStepTypeConcept
00202 
00203 template <typename L>
00204 struct dynamic_y_step_type<image_view<L> > {
00205     typedef image_view<typename dynamic_y_step_type<L>::type> type;
00206 };
00207 
00209 //  HasTransposedTypeConcept
00211 
00212 template <typename L>
00213 struct transposed_type<image_view<L> > {
00214     typedef image_view<typename transposed_type<L>::type> type;
00215 };
00216 
00217 } }  // namespace boost::gil
00218 
00219 //#ifdef _MSC_VER
00220 //#pragma warning(pop)
00221 //#endif
00222 
00223 #endif

Generated on Sat May 2 13:50:14 2009 for Generic Image Library by  doxygen 1.5.6