Claw  1.7.0
image.hpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@gamned.org
24 */
30 #ifndef __CLAW_IMAGE_HPP__
31 #define __CLAW_IMAGE_HPP__
32 
33 #include <claw/pixel.hpp>
34 #include <claw/math.hpp>
35 
36 #include <vector>
37 #include <iterator>
38 #include <iostream>
39 #include <cstddef>
40 
41 namespace claw
42 {
43  namespace graphic
44  {
49  class image
50  {
51  public:
52  typedef rgba_pixel pixel_type;
53 
58  class scanline:
59  private std::vector<pixel_type>
60  {
61  friend class image;
62 
63  public:
65  typedef std::vector<pixel_type> super;
66 
68  typedef super::value_type value_type;
69 
71  typedef super::reference reference;
72 
74  typedef super::const_reference const_reference;
75 
77  typedef super::iterator iterator;
78 
80  typedef super::const_iterator const_iterator;
81 
83  typedef super::size_type size_type;
84 
85  public:
86  iterator begin();
87  iterator end();
88 
89  const_iterator begin() const;
90  const_iterator end() const;
91 
92  inline reference operator[](unsigned int i);
93  inline const_reference operator[](unsigned int i) const;
94 
95  size_type size() const;
96 
97  }; // class scanline
98 
99  public:
104  template<typename Image, typename Pixel>
106  public std::iterator<std::random_access_iterator_tag, Pixel>
107  {
108  private:
110  typedef Image image_type;
111 
113  typedef Pixel pixel_type;
114 
117 
118  public:
119  typedef pixel_type value_type;
120  typedef pixel_type& reference;
121  typedef pixel_type* pointer;
122  typedef ptrdiff_t difference_type;
123 
124  typedef std::random_access_iterator_tag iterator_category;
125 
126  public:
127  inline base_iterator();
128  inline base_iterator( image_type& owner, unsigned int x=0,
129  unsigned int y = 0 );
130 
131  inline bool operator==( const self_type& that ) const;
132  inline bool operator!=( const self_type& that ) const;
133  inline bool operator<( const self_type& that ) const;
134  inline bool operator>( const self_type& that ) const;
135  inline bool operator<=( const self_type& that ) const;
136  inline bool operator>=( const self_type& that ) const;
137 
138  inline self_type& operator+=( int n );
139  inline self_type& operator-=( int n );
140 
141  inline self_type operator+( int n ) const;
142  inline self_type operator-( int n ) const;
143 
144  template<typename ImageT, typename PixelT>
145  friend inline self_type operator+( int n, const self_type& self );
146 
147  inline difference_type operator-( const self_type& that ) const;
148 
149  inline self_type& operator++();
150  inline self_type operator++(int);
151  inline self_type& operator--();
152  inline self_type operator--(int);
153 
154  inline reference operator*() const;
155  inline pointer operator->() const;
156 
157  inline reference operator[]( int n ) const;
158 
159  private:
160  bool is_final() const;
161 
162  private:
164  image_type* m_owner;
165 
168 
169  }; // class base_iterator
170 
171  public:
174 
175  public:
176  image();
177  image( unsigned int w, unsigned int h );
178  image( std::istream& f );
179 
180  void swap( image& that );
181 
182  unsigned int width() const;
183  unsigned int height() const;
184 
185  inline scanline& operator[](unsigned int i);
186  inline const scanline& operator[](unsigned int i) const;
187 
188  iterator begin();
189  iterator end();
190  const_iterator begin() const;
191  const_iterator end() const;
192 
193  void merge( const image& that );
194  void merge
195  ( const image& that, const math::coordinate_2d<int>& pos );
196 
197  void partial_copy
198  ( const image& that, const math::coordinate_2d<int>& pos );
199 
200  void flip();
201  void fill( const math::rectangle<int> r, const pixel_type& c );
202 
203  void set_size( unsigned int w, unsigned int h );
204 
205  void load( std::istream& f );
206 
207  private:
209  std::vector<scanline> m_data;
210 
211  }; // class image
212 
213  } // namespace graphic
214 } // namespace claw
215 
216 namespace std
217 {
218  void swap( claw::graphic::image& a, claw::graphic::image& b );
219 } // namespace std
220 
221 // Inline methods
222 #include <claw/impl/image.ipp>
223 
224 #endif // __CLAW_IMAGE_HPP__