Claw 1.7.0
image.hpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00030 #ifndef __CLAW_IMAGE_HPP__
00031 #define __CLAW_IMAGE_HPP__
00032 
00033 #include <claw/pixel.hpp>
00034 #include <claw/math.hpp>
00035 
00036 #include <vector>
00037 #include <iterator>
00038 #include <iostream>
00039 #include <cstddef>
00040 
00041 namespace claw
00042 {
00043   namespace graphic
00044   {
00049     class image
00050     {
00051     public:
00052       typedef rgba_pixel pixel_type;
00053 
00058       class scanline:
00059         private std::vector<pixel_type>
00060       {
00061         friend class image;
00062 
00063       public:
00065         typedef std::vector<pixel_type> super;
00066 
00068         typedef super::value_type value_type;
00069 
00071         typedef super::reference reference;
00072 
00074         typedef super::const_reference const_reference;
00075 
00077         typedef super::iterator iterator;
00078 
00080         typedef super::const_iterator const_iterator;
00081 
00083         typedef super::size_type size_type;
00084 
00085       public:
00086         iterator begin();
00087         iterator end();
00088 
00089         const_iterator begin() const;
00090         const_iterator end() const;
00091 
00092         inline reference operator[](unsigned int i);
00093         inline const_reference operator[](unsigned int i) const;
00094 
00095         size_type size() const;
00096 
00097       }; // class scanline
00098 
00099     public:
00104       template<typename Image, typename Pixel>
00105       class base_iterator:
00106         public std::iterator<std::random_access_iterator_tag, Pixel>
00107       {
00108       private:
00110         typedef Image image_type;
00111 
00113         typedef Pixel pixel_type;
00114 
00116         typedef base_iterator<image_type, pixel_type> self_type;
00117 
00118       public:
00119         typedef pixel_type value_type;
00120         typedef pixel_type& reference;
00121         typedef pixel_type* pointer;
00122         typedef ptrdiff_t difference_type;
00123 
00124         typedef std::random_access_iterator_tag iterator_category;
00125 
00126       public:
00127         inline base_iterator();
00128         inline base_iterator( image_type& owner, unsigned int x=0,
00129                               unsigned int y = 0 );
00130 
00131         inline bool operator==( const self_type& that ) const;
00132         inline bool operator!=( const self_type& that ) const;
00133         inline bool operator<( const self_type& that ) const;
00134         inline bool operator>( const self_type& that ) const;
00135         inline bool operator<=( const self_type& that ) const;
00136         inline bool operator>=( const self_type& that ) const;
00137 
00138         inline self_type& operator+=( int n );
00139         inline self_type& operator-=( int n );
00140 
00141         inline self_type operator+( int n ) const;
00142         inline self_type operator-( int n ) const;
00143 
00144         template<typename ImageT, typename PixelT>
00145         friend inline self_type operator+( int n, const self_type& self );
00146 
00147         inline difference_type operator-( const self_type& that ) const;
00148 
00149         inline self_type& operator++();
00150         inline self_type  operator++(int);
00151         inline self_type& operator--();
00152         inline self_type  operator--(int);
00153 
00154         inline reference operator*() const;
00155         inline pointer   operator->() const;
00156 
00157         inline reference operator[]( int n ) const;
00158 
00159       private:
00160         bool is_final() const;
00161 
00162       private:
00164         image_type* m_owner;
00165 
00167         math::coordinate_2d<unsigned int> m_pos;
00168 
00169       }; // class base_iterator
00170 
00171     public:
00172       typedef base_iterator<image, pixel_type> iterator;
00173       typedef base_iterator<const image, const pixel_type> const_iterator;
00174 
00175     public:
00176       image();
00177       image( unsigned int w, unsigned int h );
00178       image( std::istream& f );
00179 
00180       void swap( image& that );
00181 
00182       unsigned int width() const;
00183       unsigned int height() const;
00184 
00185       inline scanline& operator[](unsigned int i);
00186       inline const scanline& operator[](unsigned int i) const;
00187 
00188       iterator begin();
00189       iterator end();
00190       const_iterator begin() const;
00191       const_iterator end() const;
00192 
00193       void merge( const image& that );
00194       void merge
00195       ( const image& that, const math::coordinate_2d<int>& pos );
00196 
00197       void partial_copy
00198       ( const image& that, const math::coordinate_2d<int>& pos );
00199 
00200       void flip();
00201       void fill( const math::rectangle<int> r, const pixel_type& c );
00202 
00203       void set_size( unsigned int w, unsigned int h );
00204 
00205       void load( std::istream& f );
00206 
00207     private:
00209       std::vector<scanline> m_data;
00210         
00211     }; // class image
00212 
00213   } // namespace graphic
00214 } // namespace claw
00215 
00216 namespace std
00217 {
00218   void swap( claw::graphic::image& a, claw::graphic::image& b );
00219 } // namespace std
00220 
00221 // Inline methods
00222 #include <claw/impl/image.ipp>
00223 
00224 #endif // __CLAW_IMAGE_HPP__