1 // ImageIterators.h: Specialized iterators for image data.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifndef GNASH_IMAGE_ITERATORS_H
23 #define GNASH_IMAGE_ITERATORS_H
25 #include <boost/iterator/iterator_facade.hpp>
29 #include "GnashImage.h"
34 /// Adapt a pixel_iterator to use 32-bit values in ARGB byte order.
39 typedef GnashImage::iterator iterator
;
41 /// Construct an ARGB pixel helper
42 ARGB(iterator
& i
, ImageType t
)
48 /// Standard assignment just copies bytes.
50 /// Underlying bytes are really in RGBA order, so we use that.
51 const ARGB
& operator=(const ARGB
& other
) const {
55 if (other
._t
== TYPE_RGBA
) {
56 std::copy(other
._it
, other
._it
+ 4, _it
);
61 std::copy(other
._it
, other
._it
+ 3, _it
);
66 // It doesn't matter what the other image is.
67 std::copy(other
._it
, other
._it
+ 3, _it
);
75 /// Writes a 32-bit unsigned value in ARGB byte order to the image
77 /// Take note of the different byte order!
78 const ARGB
& operator=(boost::uint32_t pixel
) const {
82 *(_it
+ 3) = (pixel
& 0xff000000) >> 24;
84 *_it
= (pixel
& 0x00ff0000) >> 16;
85 *(_it
+ 1) = (pixel
& 0x0000ff00) >> 8;
86 *(_it
+ 2) = (pixel
& 0x000000ff);
93 /// Convert to uint32_t in ARGB order
94 operator boost::uint32_t() const {
95 boost::uint32_t ret
= 0xff000000;
99 ret
= *(_it
+ 3) << 24;
101 ret
|= (*_it
<< 16 | *(_it
+ 1) << 8 | *(_it
+ 2));
114 /// The pixel_iterator class is a pixel-level adaptor for a GnashImage
116 /// Instead of iterating byte-by-byte, this iterator provides access at a
117 /// whole-pixel level. This makes it possible to assign custom colour values.
119 /// @tparam Pixel A class that determines the byte order of the colour
121 template<typename Pixel
>
122 struct pixel_iterator
: public boost::iterator_facade
<
123 pixel_iterator
<Pixel
>,
125 std::random_access_iterator_tag
>
128 typedef std::ptrdiff_t difference_type
;
129 typedef typename
Pixel::iterator iterator
;
131 /// Construct a pixel_iterator
132 pixel_iterator(iterator it
, ImageType t
)
139 /// Copy a pixel_iterator
140 pixel_iterator(const pixel_iterator
& other
)
147 /// Assign to a pixel_iterator
148 pixel_iterator
& operator=(const pixel_iterator
& other
)
158 friend class boost::iterator_core_access
;
160 const Pixel
& dereference() const {
165 _it
+= numChannels(_t
);
169 _it
-= numChannels(_t
);
172 bool equal(const pixel_iterator
& o
) const {
176 difference_type
distance_to(const pixel_iterator
& o
) const {
177 return (o
._it
- _it
) / static_cast<int>(numChannels(_t
));
180 void advance(difference_type n
) {
181 _it
+= n
* numChannels(_t
);
191 begin(GnashImage
& im
)
193 return pixel_iterator
<T
>(im
.begin(), im
.type());
200 return pixel_iterator
<T
>(im
.end(), im
.type());