Claw  1.7.3
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:
54 
59  class scanline:
60  private std::vector<pixel_type>
61  {
62  friend class image;
63 
64  public:
66  typedef std::vector<pixel_type> super;
67 
69  typedef super::value_type value_type;
70 
72  typedef super::reference reference;
73 
75  typedef super::const_reference const_reference;
76 
78  typedef super::iterator iterator;
79 
81  typedef super::const_iterator const_iterator;
82 
84  typedef super::size_type size_type;
85 
86  public:
87  iterator begin();
88  iterator end();
89 
90  const_iterator begin() const;
91  const_iterator end() const;
92 
93  inline reference operator[](unsigned int i);
94  inline const_reference operator[](unsigned int i) const;
95 
96  size_type size() const;
97 
98  }; // class scanline
99 
100  public:
105  template<typename Image, typename Pixel>
107  public std::iterator<std::random_access_iterator_tag, Pixel>
108  {
109  private:
111  typedef Image image_type;
112 
114  typedef Pixel pixel_type;
115 
118 
119  public:
121  typedef pixel_type value_type;
122 
125  typedef pixel_type& reference;
126 
129  typedef pixel_type* pointer;
130 
132  typedef ptrdiff_t difference_type;
133 
135  typedef std::random_access_iterator_tag iterator_category;
136 
137  public:
138  inline base_iterator();
139  inline base_iterator( image_type& owner, unsigned int x=0,
140  unsigned int y = 0 );
141 
142  inline bool operator==( const self_type& that ) const;
143  inline bool operator!=( const self_type& that ) const;
144  inline bool operator<( const self_type& that ) const;
145  inline bool operator>( const self_type& that ) const;
146  inline bool operator<=( const self_type& that ) const;
147  inline bool operator>=( const self_type& that ) const;
148 
149  inline self_type& operator+=( int n );
150  inline self_type& operator-=( int n );
151 
152  inline self_type operator+( int n ) const;
153  inline self_type operator-( int n ) const;
154 
161  template<typename ImageT, typename PixelT>
162  friend inline self_type operator+( int n, const self_type& self );
163 
164  inline difference_type operator-( const self_type& that ) const;
165 
166  inline self_type& operator++();
167  inline self_type operator++(int);
168  inline self_type& operator--();
169  inline self_type operator--(int);
170 
171  inline reference operator*() const;
172  inline pointer operator->() const;
173 
174  inline reference operator[]( int n ) const;
175 
176  private:
177  bool is_final() const;
178 
179  private:
181  image_type* m_owner;
182 
185 
186  }; // class base_iterator
187 
188  public:
196 
204 
205  public:
206  image();
207  image( unsigned int w, unsigned int h );
208  image( std::istream& f );
209 
210  void swap( image& that );
211 
212  unsigned int width() const;
213  unsigned int height() const;
214 
215  inline scanline& operator[](unsigned int i);
216  inline const scanline& operator[](unsigned int i) const;
217 
218  iterator begin();
219  iterator end();
220  const_iterator begin() const;
221  const_iterator end() const;
222 
223  void merge( const image& that );
224  void merge
225  ( const image& that, const math::coordinate_2d<int>& pos );
226 
227  void partial_copy
228  ( const image& that, const math::coordinate_2d<int>& pos );
229 
230  void flip();
231  void fill( const math::rectangle<int> r, const pixel_type& c );
232 
233  void set_size( unsigned int w, unsigned int h );
234 
235  void load( std::istream& f );
236 
237  private:
239  std::vector<scanline> m_data;
240 
241  }; // class image
242 
243  } // namespace graphic
244 } // namespace claw
245 
246 namespace std
247 {
248  void swap( claw::graphic::image& a, claw::graphic::image& b );
249 } // namespace std
250 
251 // Inline methods
252 #include <claw/impl/image.ipp>
253 
254 #endif // __CLAW_IMAGE_HPP__
One line in the image.
Definition: image.hpp:59
super::const_reference const_reference
Const reference to a pixel.
Definition: image.hpp:75
std::random_access_iterator_tag iterator_category
The type of this category.
Definition: image.hpp:135
void partial_copy(const image &that, const math::coordinate_2d< int > &pos)
Copy an image on the current image.
Definition: image.cpp:272
reference operator[](int n) const
Get a pixel, using the iterator like an array.
Definition: image.ipp:398
bool operator!=(const self_type &that) const
Tell if two iterator points to different addresses.
Definition: image.ipp:109
bool operator>(const self_type &that) const
Tell if the current iterator is after an other.
Definition: image.ipp:138
ptrdiff_t difference_type
The type of the distance between two iterators.
Definition: image.hpp:132
super::const_iterator const_iterator
Const iterator in the line.
Definition: image.hpp:81
rgba_pixel pixel_type
The type representing the colors of the pixels in the image.
Definition: image.hpp:53
reference operator[](unsigned int i)
Get a pixel from the line.
Definition: image.ipp:38
self_type operator-(int n) const
Get an iterator at a specific distance of the current iterator.
Definition: image.ipp:245
super::value_type value_type
The type of the pixels.
Definition: image.hpp:69
self_type & operator+=(int n)
Move the iterator.
Definition: image.ipp:178
iterator end()
Get an iterator pointing just past the last pixel.
Definition: image.cpp:177
Definition: gif.hpp:461
size_type size() const
Get the length of the line.
Definition: image.cpp:93
unsigned int height() const
Gets image&#39;s height.
Definition: image.cpp:159
base_iterator< image, pixel_type > iterator
The type of the iterator on the pixels of the image.
Definition: image.hpp:195
super::size_type size_type
An unsigned integral type.
Definition: image.hpp:84
bool operator==(const self_type &that) const
Tell if two iterator point to the same address.
Definition: image.ipp:91
void flip()
Set the image upside down.
Definition: image.cpp:301
void swap(image &that)
Swap the content of two images.
Definition: image.cpp:138
void fill(const math::rectangle< int > r, const pixel_type &c)
Fill an area of the image with a given color.
Definition: image.cpp:314
Base class for iterators on an image.
Definition: image.hpp:106
iterator end()
Get en iterator past the last pixel.
Definition: image.cpp:63
void load(std::istream &f)
Read the image from a stream.
Definition: image.cpp:381
Some mathematical structures and functions.
A class representing a rectangle by his x,y coordinates, width and height.
Definition: box_2d.hpp:39
pixel_type * pointer
The type of the pointers to the values accesssed by the iterator.
Definition: image.hpp:129
pointer operator->() const
Get a pointer on the pointed pixel.
Definition: image.ipp:384
image()
Constructor. Creates an image without datas.
Definition: image.cpp:106
bool operator>=(const self_type &that) const
Tell if the current iterator is after an other, or on the same address.
Definition: image.ipp:166
super::iterator iterator
Iterator in the line.
Definition: image.hpp:78
unsigned int width() const
Gets image&#39;s width.
Definition: image.cpp:147
pixel_type & reference
The type of the references to the values accesssed by the iterator.
Definition: image.hpp:125
std::vector< pixel_type > super
The type of the parent class.
Definition: image.hpp:66
scanline & operator[](unsigned int i)
Gets a line of the image.
Definition: image.ipp:429
reference operator*() const
Get a reference on the pointed pixel.
Definition: image.ipp:371
void merge(const image &that)
Merge an image on the current image.
Definition: image.cpp:205
pixel_type value_type
The type of the values accessed by the iterator.
Definition: image.hpp:121
iterator begin()
Get an iterator on the first pixel.
Definition: image.cpp:54
base_iterator< const image, const pixel_type > const_iterator
The type of the iterator to access constant pixels.
Definition: image.hpp:203
iterator begin()
Get an iterator pointing on the first pixel.
Definition: image.cpp:168
super::reference reference
Reference to a pixel..
Definition: image.hpp:72
Representation of a pixel in image processing.
bool operator<(const self_type &that) const
Tell if the current iterator is before an other.
Definition: image.ipp:122
A class to deal with images.
Definition: image.hpp:49
self_type & operator++()
Preincrement.
Definition: image.ipp:302
bool operator<=(const self_type &that) const
Tell if the current iterator is before an other, or on the same address.
Definition: image.ipp:152
self_type operator+(int n) const
Get an iterator at a specific distance of the current iterator.
Definition: image.ipp:231
self_type & operator--()
Predecrement.
Definition: image.ipp:336
This is the main namespace.
Definition: algorithm.hpp:33
self_type & operator-=(int n)
Move the iterator.
Definition: image.ipp:203
void set_size(unsigned int w, unsigned int h)
Set a new size to the image.
Definition: image.cpp:363
Inline methods for the claw::graphic::image class.