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/noncopyable.hpp>
28 #include <cstdint>
29 #include <memory>
30 
31 #include "GnashEnums.h"
32 #include "log.h"
33 #include "dsodefs.h"
34 
35 // Forward declarations
36 namespace gnash {
37  class IOChannel;
38 }
39 
40 namespace gnash {
41 
43 namespace image {
44 
47 {
51 };
52 
55 {
58 };
59 
60 inline size_t
62 {
63  switch (t) {
64  case TYPE_RGBA:
65  return 4;
66  case TYPE_RGB:
67  return 3;
68  default:
69  std::abort();
70  }
71 }
72 
74 //
77 class DSOEXPORT GnashImage : boost::noncopyable
78 {
79 public:
80 
81  typedef std::uint8_t value_type;
82  typedef std::unique_ptr<value_type[]> container_type;
83  typedef value_type* iterator;
84  typedef const value_type* const_iterator;
85 
86  virtual ~GnashImage() {}
87 
89  //
91  ImageType type() const {
92  return _type;
93  }
94 
96  //
99  return _location;
100  }
101 
103  //
105  size_t size() const {
106  return stride() * _height;
107  }
108 
110  //
112  virtual size_t stride() const {
113  return _width * channels();
114  }
115 
117  //
119  size_t channels() const {
120  return numChannels(_type);
121  }
122 
124  //
126  size_t width() const {
127  return _width;
128  }
129 
131  //
133  size_t height() const {
134  return _height;
135  }
136 
138  //
144  void update(const_iterator data);
145 
147  //
151  void update(const GnashImage& from);
152 
154  virtual iterator begin() {
155  return _data.get();
156  }
157 
159  virtual const_iterator begin() const {
160  return _data.get();
161  }
162 
164  iterator end() {
165  return begin() + size();
166  }
167 
169  const_iterator end() const {
170  return begin() + size();
171  }
172 
173 protected:
174 
176  //
182  GnashImage(iterator data, size_t width, size_t height, ImageType type,
183  ImageLocation location = GNASH_IMAGE_CPU);
184 
186  //
189  //
193  GnashImage(size_t width, size_t height, ImageType type,
194  ImageLocation location = GNASH_IMAGE_CPU);
195 
198 
201 
203  const size_t _width;
204 
206  const size_t _height;
207 
209  container_type _data;
210 
211 };
212 
214 //
217 {
218 public:
219 
221  ImageRGB(size_t width, size_t height);
222 
224  ImageRGB(iterator data, size_t width, size_t height)
225  :
226  GnashImage(data, width, height, TYPE_RGB)
227  {}
228 
229  virtual ~ImageRGB();
230 };
231 
233 //
236 {
237 
238 public:
239 
241  ImageRGBA(size_t width, size_t height);
242 
243  ImageRGBA(iterator data, size_t width, size_t height)
244  :
245  GnashImage(data, width, height, TYPE_RGBA)
246  {}
247 
248  ~ImageRGBA();
249 
251  //
254  void setPixel(size_t x, size_t y, value_type r, value_type g, value_type b,
255  value_type a);
256 };
257 
259 class Input : boost::noncopyable
260 {
261 public:
262 
264  //
268  Input(std::shared_ptr<IOChannel> in)
269  :
270  _inStream(in),
271  _type(GNASH_IMAGE_INVALID)
272  {}
273 
274  virtual ~Input() {}
275 
277  virtual void read() = 0;
278 
280  //
282  virtual size_t getHeight() const = 0;
283 
285  //
287  virtual size_t getWidth() const = 0;
288 
290  //
292  virtual size_t getComponents() const = 0;
293 
295  //
297  virtual void readScanline(unsigned char* rgbData) = 0;
298 
300  //
304  ImageType imageType() { return _type; }
305 
309  DSOEXPORT static std::unique_ptr<ImageRGBA> readSWFJpeg3(
310  std::shared_ptr<gnash::IOChannel> in);
311 
313  //
319  DSOEXPORT static std::unique_ptr<GnashImage> readImageData(
320  std::shared_ptr<gnash::IOChannel> in, FileType type);
321 
322 protected:
323 
324  std::shared_ptr<IOChannel> _inStream;
325 
327 
328 };
329 
330 // Base class for writing image data.
331 class Output : boost::noncopyable
332 {
333 
334 public:
335 
337  //
342  Output(std::shared_ptr<IOChannel> out, size_t width, size_t height)
343  :
344  _width(width),
345  _height(height),
346  _outStream(out)
347  {}
348 
349  virtual ~Output() {}
350 
352  //
354  virtual void writeImageRGB(const unsigned char* rgbData) = 0;
355 
357  //
359  virtual void writeImageRGBA(const unsigned char* /*rgbaData*/)
360  {
361  log_error(_("This image format does not support writing RGBA images"));
362  }
363 
365  //
373  DSOEXPORT static void writeImageData(FileType type,
374  std::shared_ptr<gnash::IOChannel> out, const GnashImage& image,
375  int quality);
376 
377 protected:
378 
379  const size_t _width;
380 
381  const size_t _height;
382 
383  std::shared_ptr<IOChannel> _outStream;
384 
385 };
386 
388 //
392 scanline(GnashImage& im, size_t row)
393 {
394  assert(row < im.height());
395  return im.begin() + im.stride() * row;
396 }
397 
399 //
403 scanline(const GnashImage& im, size_t row)
404 {
405  assert(row < im.height());
406  return im.begin() + im.stride() * row;
407 }
408 
410  const size_t bufferLength);
411 
412 } // namespace image
413 } // namespace gnash
414 
415 #endif
Definition: GnashKey.h:147
ImageLocation location() const
Return the ImageLocation of the image.
Definition: GnashImage.h:98
32-bit RGBA bitmap
Definition: GnashImage.h:235
FileType
Definition: GnashEnums.h:25
const size_t _height
Definition: GnashImage.h:381
Input(std::shared_ptr< IOChannel > in)
Construct an Input object to read from an IOChannel.
Definition: GnashImage.h:268
Output(std::shared_ptr< IOChannel > out, size_t width, size_t height)
Construct an Output for writing to an IOChannel.
Definition: GnashImage.h:342
ImageRGBA(iterator data, size_t width, size_t height)
Definition: GnashImage.h:243
Definition: GnashImage.h:49
const ImageLocation _location
Image data location (CPU or GPU)
Definition: GnashImage.h:200
size_t size() const
Get the size of the image buffer.
Definition: GnashImage.h:105
ImageRGB(iterator data, size_t width, size_t height)
Create an ImageRGB taking ownership of the data.
Definition: GnashImage.h:224
pixel_iterator< T > begin(GnashImage &im)
Definition: ImageIterators.h:191
virtual ~GnashImage()
Definition: GnashImage.h:86
virtual iterator begin()
Access the raw data.
Definition: GnashImage.h:154
container_type _data
Data if held in this class.
Definition: GnashImage.h:209
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
type
Definition: GnashKey.h:329
const size_t _width
Definition: GnashImage.h:379
Definition: klash_part.cpp:329
24-bit RGB bitmap
Definition: GnashImage.h:216
virtual ~Input()
Definition: GnashImage.h:274
const size_t _width
Width of image, in pixels.
Definition: GnashImage.h:203
ImageType imageType()
Get the ImageType of the image.
Definition: GnashImage.h:304
size_t height() const
Get the image&#39;s width.
Definition: GnashImage.h:133
Definition: GnashImage.h:50
Definition: GnashKey.h:164
Definition: GnashKey.h:166
std::uint8_t value_type
Definition: GnashImage.h:81
Definition: klash_part.cpp:329
#define _(String)
Definition: log.h:44
std::unique_ptr< value_type[]> container_type
Definition: GnashImage.h:82
std::shared_ptr< IOChannel > _outStream
Definition: GnashImage.h:383
size_t width() const
Get the image&#39;s width.
Definition: GnashImage.h:126
void log_error(StringType msg, Args... args)
Definition: log.h:283
ImageType
The types of images handled in Gnash.
Definition: GnashImage.h:46
virtual void writeImageRGBA(const unsigned char *)
Write RGBA image data using the parameters supplied at construction.
Definition: GnashImage.h:359
GnashImage::iterator scanline(GnashImage &im, size_t row)
Get a pointer to a given row of any image.
Definition: GnashImage.h:392
const_iterator end() const
An iterator to the end of the data.
Definition: GnashImage.h:169
virtual size_t stride() const
Get the pitch of the image buffer.
Definition: GnashImage.h:112
std::int32_t x
Definition: BitmapData_as.cpp:434
size_t numChannels(ImageType t)
Definition: GnashImage.h:61
Definition: GnashKey.h:148
#define DSOEXPORT
Definition: dsodefs.h:55
ImageLocation
The locations of images handled in Gnash.
Definition: GnashImage.h:54
Definition: GnashImage.h:331
const size_t _height
Height of image, in pixels.
Definition: GnashImage.h:206
std::shared_ptr< IOChannel > _inStream
Definition: GnashImage.h:324
Definition: GnashImage.h:56
value_type * iterator
Definition: GnashImage.h:83
std::int32_t y
Definition: BitmapData_as.cpp:435
Base class for different types of bitmaps.
Definition: GnashImage.h:77
Definition: GnashKey.h:153
size_t channels() const
Get the number of channels.
Definition: GnashImage.h:119
Definition: GnashImage.h:57
const ImageType _type
The type of the image: RGBA or RGB.
Definition: GnashImage.h:197
void mergeAlpha(ImageRGBA &im, GnashImage::const_iterator alphaData, const size_t bufferLength)
Definition: GnashImage.cpp:146
iterator end()
An iterator to the end of the data.
Definition: GnashImage.h:164
ImageType _type
Definition: GnashImage.h:326
ImageType type() const
Return the ImageType of the image.
Definition: GnashImage.h:91
const value_type * const_iterator
Definition: GnashImage.h:84
virtual ~Output()
Definition: GnashImage.h:349
Definition: GnashImage.h:48
The base class for reading image data.
Definition: GnashImage.h:259
as_value getHeight(DisplayObject &o)
Definition: DisplayObject.cpp:356
virtual const_iterator begin() const
Access the raw data.
Definition: GnashImage.h:159