Gnash  0.8.11dev
ImageIterators.h
Go to the documentation of this file.
1 // ImageIterators.h: Specialized iterators for image data.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
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.
10 //
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.
15 //
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
19 //
20 
21 
22 #ifndef GNASH_IMAGE_ITERATORS_H
23 #define GNASH_IMAGE_ITERATORS_H
24 
25 #include <boost/iterator/iterator_facade.hpp>
26 #include <iterator>
27 #include <algorithm>
28 
29 #include "GnashImage.h"
30 
31 namespace gnash {
32 namespace image {
33 
35 class ARGB
36 {
37 public:
38 
40 
42  ARGB(iterator& i, ImageType t)
43  :
44  _it(i),
45  _t(t)
46  {}
47 
49  //
51  ARGB& operator=(const ARGB& other) {
52  switch (_t) {
53  case TYPE_RGBA:
54  // RGBA to RGBA
55  if (other._t == TYPE_RGBA) {
56  std::copy(other._it, other._it + 4, _it);
57  break;
58  }
59 
60  // RGB to RGBA
61  std::copy(other._it, other._it + 3, _it);
62  *(_it + 3) = 0xff;
63  break;
64 
65  case TYPE_RGB:
66  // It doesn't matter what the other image is.
67  std::copy(other._it, other._it + 3, _it);
68 
69  default:
70  break;
71  }
72  return *this;
73  }
74 
76  //
78  const ARGB& operator=(std::uint32_t pixel) const {
79  switch (_t) {
80  case TYPE_RGBA:
81  // alpha
82  *(_it + 3) = (pixel & 0xff000000) >> 24;
83  case TYPE_RGB:
84  *_it = (pixel & 0x00ff0000) >> 16;
85  *(_it + 1) = (pixel & 0x0000ff00) >> 8;
86  *(_it + 2) = (pixel & 0x000000ff);
87  default:
88  break;
89  }
90  return *this;
91  }
92 
94  operator std::uint32_t() const {
95  std::uint32_t ret = 0xff000000;
96  switch (_t) {
97  case TYPE_RGBA:
98  // alpha
99  ret = *(_it + 3) << 24;
100  case TYPE_RGB:
101  ret |= (*_it << 16 | *(_it + 1) << 8 | *(_it + 2));
102  default:
103  break;
104  }
105  return ret;
106  }
107 
108 private:
109  iterator& _it;
110  const ImageType _t;
111 };
112 
113 
115 //
118 //
121 template<typename Pixel>
122 struct pixel_iterator : public boost::iterator_facade<
123  pixel_iterator<Pixel>,
124  const Pixel,
125  std::random_access_iterator_tag>
126 {
127 
128  typedef std::ptrdiff_t difference_type;
129  typedef typename Pixel::iterator iterator;
130 
132  pixel_iterator(iterator it, ImageType t)
133  :
134  _it(it),
135  _t(t),
136  _p(_it, _t)
137  {}
138 
141  :
142  _it(other._it),
143  _t(other._t),
144  _p(_it, _t)
145  {}
146 
149  {
150  _it = other._it;
151  _t = other._t;
152  _p = Pixel(_it, _t);
153  return *this;
154  }
155 
156 private:
157 
158  friend class boost::iterator_core_access;
159 
160  const Pixel& dereference() const {
161  return _p;
162  }
163 
164  void increment() {
165  _it += numChannels(_t);
166  }
167 
168  void decrement() {
169  _it -= numChannels(_t);
170  }
171 
172  bool equal(const pixel_iterator& o) const {
173  return o._it == _it;
174  }
175 
176  difference_type distance_to(const pixel_iterator& o) const {
177  return (o._it - _it) / static_cast<int>(numChannels(_t));
178  }
179 
180  void advance(difference_type n) {
181  _it += n * numChannels(_t);
182  }
183 
184  iterator _it;
185  ImageType _t;
186  Pixel _p;
187 };
188 
189 template<typename T>
192 {
193  return pixel_iterator<T>(im.begin(), im.type());
194 }
195 
196 template<typename T>
199 {
200  return pixel_iterator<T>(im.end(), im.type());
201 }
202 
203 } // namespace image
204 } // namespace gnash
205 
206 #endif
Definition: GnashImage.h:49
pixel_iterator< T > begin(GnashImage &im)
Definition: ImageIterators.h:191
virtual iterator begin()
Access the raw data.
Definition: GnashImage.h:154
ARGB & operator=(const ARGB &other)
Standard assignment just copies bytes.
Definition: ImageIterators.h:51
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
Definition: GnashKey.h:161
The pixel_iterator class is a pixel-level adaptor for a GnashImage.
Definition: ImageIterators.h:122
ARGB(iterator &i, ImageType t)
Construct an ARGB pixel helper.
Definition: ImageIterators.h:42
const ARGB & operator=(std::uint32_t pixel) const
Writes a 32-bit unsigned value in ARGB byte order to the image.
Definition: ImageIterators.h:78
Definition: GnashImage.h:50
Definition: GnashKey.h:160
Definition: GnashKey.h:166
Adapt a pixel_iterator to use 32-bit values in ARGB byte order.
Definition: ImageIterators.h:35
GnashImage::iterator iterator
Definition: ImageIterators.h:39
bool equal(string_table &st, string_table::key a, string_table::key b, bool caseless)
Check whether two keys are equivalent.
Definition: string_table.cpp:174
ImageType
The types of images handled in Gnash.
Definition: GnashImage.h:46
size_t numChannels(ImageType t)
Definition: GnashImage.h:61
std::ptrdiff_t difference_type
Definition: ImageIterators.h:128
value_type * iterator
Definition: GnashImage.h:83
Base class for different types of bitmaps.
Definition: GnashImage.h:77
Definition: GnashKey.h:155
pixel_iterator(const pixel_iterator &other)
Copy a pixel_iterator.
Definition: ImageIterators.h:140
pixel_iterator< T > end(GnashImage &im)
Definition: ImageIterators.h:198
Pixel::iterator iterator
Definition: ImageIterators.h:129
pixel_iterator & operator=(const pixel_iterator &other)
Assign to a pixel_iterator.
Definition: ImageIterators.h:148
iterator end()
An iterator to the end of the data.
Definition: GnashImage.h:164
ImageType type() const
Return the ImageType of the image.
Definition: GnashImage.h:91
pixel_iterator(iterator it, ImageType t)
Construct a pixel_iterator.
Definition: ImageIterators.h:132