Gnash  0.8.11dev
GnashImage.h
Go to the documentation of this file.
1 // GnashImage.h: Base class for reading image data in Gnash.
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 // The GnashImage class and subclasses are partly based on the public domain
22 // work of Thatcher Ulrich <tu@tulrich.com> 2002
23 
24 #ifndef GNASH_GNASHIMAGE_H
25 #define GNASH_GNASHIMAGE_H
26 
27 #include <boost/shared_ptr.hpp>
28 #include <boost/noncopyable.hpp>
29 #include <boost/cstdint.hpp>
30 #include <boost/scoped_array.hpp>
31 #include <memory>
32 
33 #include "GnashEnums.h"
34 #include "log.h"
35 #include "dsodefs.h"
36 
37 // Forward declarations
38 namespace gnash {
39  class IOChannel;
40 }
41 
42 namespace gnash {
43 
45 namespace image {
46 
49 {
53 };
54 
57 {
60 };
61 
62 inline size_t
64 {
65  switch (t) {
66  case TYPE_RGBA:
67  return 4;
68  case TYPE_RGB:
69  return 3;
70  default:
71  std::abort();
72  }
73 }
74 
76 //
79 class DSOEXPORT GnashImage : boost::noncopyable
80 {
81 public:
82 
83  typedef boost::uint8_t value_type;
84  typedef boost::scoped_array<value_type> container_type;
85  typedef value_type* iterator;
86  typedef const value_type* const_iterator;
87 
88  virtual ~GnashImage() {}
89 
91  //
93  ImageType type() const {
94  return _type;
95  }
96 
98  //
101  return _location;
102  }
103 
105  //
107  size_t size() const {
108  return stride() * _height;
109  }
110 
112  //
114  virtual size_t stride() const {
115  return _width * channels();
116  }
117 
119  //
121  size_t channels() const {
122  return numChannels(_type);
123  }
124 
126  //
128  size_t width() const {
129  return _width;
130  }
131 
133  //
135  size_t height() const {
136  return _height;
137  }
138 
140  //
146  void update(const_iterator data);
147 
149  //
153  void update(const GnashImage& from);
154 
156  virtual iterator begin() {
157  return _data.get();
158  }
159 
161  virtual const_iterator begin() const {
162  return _data.get();
163  }
164 
167  return begin() + size();
168  }
169 
171  const_iterator end() const {
172  return begin() + size();
173  }
174 
175 protected:
176 
178  //
184  GnashImage(iterator data, size_t width, size_t height, ImageType type,
185  ImageLocation location = GNASH_IMAGE_CPU);
186 
188  //
191  //
195  GnashImage(size_t width, size_t height, ImageType type,
196  ImageLocation location = GNASH_IMAGE_CPU);
197 
200 
203 
205  const size_t _width;
206 
208  const size_t _height;
209 
212 
213 };
214 
216 //
219 {
220 public:
221 
223  ImageRGB(size_t width, size_t height);
224 
226  ImageRGB(iterator data, size_t width, size_t height)
227  :
228  GnashImage(data, width, height, TYPE_RGB)
229  {}
230 
231  virtual ~ImageRGB();
232 };
233 
235 //
238 {
239 
240 public:
241 
243  ImageRGBA(size_t width, size_t height);
244 
245  ImageRGBA(iterator data, size_t width, size_t height)
246  :
247  GnashImage(data, width, height, TYPE_RGBA)
248  {}
249 
250  ~ImageRGBA();
251 
253  //
256  void setPixel(size_t x, size_t y, value_type r, value_type g, value_type b,
257  value_type a);
258 };
259 
261 class Input : boost::noncopyable
262 {
263 public:
264 
266  //
270  Input(boost::shared_ptr<IOChannel> in)
271  :
272  _inStream(in),
274  {}
275 
276  virtual ~Input() {}
277 
279  virtual void read() = 0;
280 
282  //
284  virtual size_t getHeight() const = 0;
285 
287  //
289  virtual size_t getWidth() const = 0;
290 
292  //
294  virtual size_t getComponents() const = 0;
295 
297  //
299  virtual void readScanline(unsigned char* rgbData) = 0;
300 
302  //
306  ImageType imageType() { return _type; }
307 
311  DSOEXPORT static std::auto_ptr<ImageRGBA> readSWFJpeg3(
312  boost::shared_ptr<gnash::IOChannel> in);
313 
315  //
321  DSOEXPORT static std::auto_ptr<GnashImage> readImageData(
322  boost::shared_ptr<gnash::IOChannel> in, FileType type);
323 
324 protected:
325 
326  boost::shared_ptr<IOChannel> _inStream;
327 
329 
330 };
331 
332 // Base class for writing image data.
333 class Output : boost::noncopyable
334 {
335 
336 public:
337 
339  //
344  Output(boost::shared_ptr<IOChannel> out, size_t width, size_t height)
345  :
346  _width(width),
347  _height(height),
348  _outStream(out)
349  {}
350 
351  virtual ~Output() {}
352 
354  //
356  virtual void writeImageRGB(const unsigned char* rgbData) = 0;
357 
359  //
361  virtual void writeImageRGBA(const unsigned char* /*rgbaData*/)
362  {
363  log_error(_("This image format does not support writing RGBA images"));
364  }
365 
367  //
376  boost::shared_ptr<gnash::IOChannel> out, const GnashImage& image,
377  int quality);
378 
379 protected:
380 
381  const size_t _width;
382 
383  const size_t _height;
384 
385  boost::shared_ptr<IOChannel> _outStream;
386 
387 };
388 
390 //
394 scanline(GnashImage& im, size_t row)
395 {
396  assert(row < im.height());
397  return im.begin() + im.stride() * row;
398 }
399 
401 //
405 scanline(const GnashImage& im, size_t row)
406 {
407  assert(row < im.height());
408  return im.begin() + im.stride() * row;
409 }
410 
411 DSOEXPORT void mergeAlpha(ImageRGBA& im, GnashImage::const_iterator alphaData,
412  const size_t bufferLength);
413 
414 } // namespace image
415 } // namespace gnash
416 
417 #endif