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 {
50  TYPE_RGBA
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 
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 
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  :
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  :
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),
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  //
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 
409 DSOEXPORT void mergeAlpha(ImageRGBA& im, GnashImage::const_iterator alphaData,
410  const size_t bufferLength);
411 
412 } // namespace image
413 } // namespace gnash
414 
415 #endif
gnash::image::GnashImage::value_type
std::uint8_t value_type
Definition: GnashImage.h:81
gnash::image::begin
pixel_iterator< T > begin(GnashImage &im)
Definition: ImageIterators.h:191
height
@ height
Definition: klash_part.cpp:329
gnash::image::GnashImage::container_type
std::unique_ptr< value_type[]> container_type
Definition: GnashImage.h:82
gnash::image::Output::writeImageRGB
virtual void writeImageRGB(const unsigned char *rgbData)=0
Write RGB image data using the parameters supplied at construction.
gnash::image::GnashImage::width
size_t width() const
Get the image's width.
Definition: GnashImage.h:126
gnash::image::ImageRGB
24-bit RGB bitmap
Definition: GnashImage.h:217
gnash::image::Output::_outStream
std::shared_ptr< IOChannel > _outStream
Definition: GnashImage.h:383
gnash::image::GnashImage::_height
const size_t _height
Height of image, in pixels.
Definition: GnashImage.h:206
gnash::image::GnashImage::stride
virtual size_t stride() const
Get the pitch of the image buffer.
Definition: GnashImage.h:112
dsodefs.h
gnash::image::createGifInput
std::unique_ptr< Input > createGifInput(std::shared_ptr< IOChannel > in)
Create a GifInput and transfer ownership to the caller.
Definition: GnashImageGif.cpp:311
gnash::image::GnashImage::end
const_iterator end() const
An iterator to the end of the data.
Definition: GnashImage.h:169
y
std::int32_t y
Definition: BitmapData_as.cpp:435
gnash::key::i
@ i
Definition: GnashKey.h:155
gnash::image::Input::Input
Input(std::shared_ptr< IOChannel > in)
Construct an Input object to read from an IOChannel.
Definition: GnashImage.h:268
gnash::image::Input::getHeight
virtual size_t getHeight() const =0
Get the image's height in pixels.
gnash::image::Output
Definition: GnashImage.h:332
gnash::image::GnashImage::iterator
value_type * iterator
Definition: GnashImage.h:83
gnash::image::ImageRGBA::ImageRGBA
ImageRGBA(iterator data, size_t width, size_t height)
Definition: GnashImage.h:243
_
#define _(String)
Definition: log.h:44
gnash
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:41
gnash::key::g
@ g
Definition: GnashKey.h:153
gnash::image::GnashImage
Base class for different types of bitmaps.
Definition: GnashImage.h:78
gnash::FileType
FileType
Definition: GnashEnums.h:25
gnash::image::Input::imageType
ImageType imageType()
Get the ImageType of the image.
Definition: GnashImage.h:304
gnash::GNASH_FILETYPE_JPEG
@ GNASH_FILETYPE_JPEG
Definition: GnashEnums.h:26
gnash::image::GnashImage::channels
size_t channels() const
Get the number of channels.
Definition: GnashImage.h:119
gnash::image::GnashImage::_type
const ImageType _type
The type of the image: RGBA or RGB.
Definition: GnashImage.h:197
GnashImage.h
gnash::image::Output::writeImageRGBA
virtual void writeImageRGBA(const unsigned char *)
Write RGBA image data using the parameters supplied at construction.
Definition: GnashImage.h:359
gnash::image::mergeAlpha
void mergeAlpha(ImageRGBA &im, GnashImage::const_iterator alphaData, const size_t bufferLength)
Definition: GnashImage.cpp:146
GnashEnums.h
gnash::log_error
void log_error(StringType msg, Args... args)
Definition: log.h:283
gnash::image::GnashImage::type
ImageType type() const
Return the ImageType of the image.
Definition: GnashImage.h:91
gnash::image::GnashImage::end
iterator end()
An iterator to the end of the data.
Definition: GnashImage.h:164
gnash::image::JpegInput::createSWFJpeg2HeaderOnly
static std::unique_ptr< JpegInput > createSWFJpeg2HeaderOnly(std::shared_ptr< IOChannel > in, unsigned int maxHeaderBytes)
Create a JPEG 'loader' object by reading a JPEG header.
Definition: GnashImageJpeg.h:145
gnash::key::t
@ t
Definition: GnashKey.h:166
gnash::key::r
@ r
Definition: GnashKey.h:164
GnashImagePng.h
gnash::image::Input::~Input
virtual ~Input()
Definition: GnashImage.h:274
gnash::key::a
@ a
Definition: GnashKey.h:147
gnash::image::GnashImage::const_iterator
const value_type * const_iterator
Definition: GnashImage.h:84
gnash::image::GnashImage::begin
virtual const_iterator begin() const
Access the raw data.
Definition: GnashImage.h:159
gnash::image::createPngInput
std::unique_ptr< Input > createPngInput(std::shared_ptr< IOChannel > in)
Create a PngInput and transfer ownership to the caller.
Definition: GnashImagePng.cpp:403
gnash::key::type
type
Definition: GnashKey.h:330
gnash::image::GnashImage::location
ImageLocation location() const
Return the ImageLocation of the image.
Definition: GnashImage.h:98
gnash::image::JpegInput::create
static std::unique_ptr< Input > create(std::shared_ptr< IOChannel > in)
Create a JpegInput and transfer ownership to the caller.
Definition: GnashImageJpeg.h:121
gnash::key::p
@ p
Definition: GnashKey.h:162
GnashNumeric.h
gnash::image::scanline
GnashImage::iterator scanline(GnashImage &im, size_t row)
Get a pointer to a given row of any image.
Definition: GnashImage.h:392
gnash::image::ImageLocation
ImageLocation
The locations of images handled in Gnash.
Definition: GnashImage.h:55
gnash::image::GNASH_IMAGE_INVALID
@ GNASH_IMAGE_INVALID
Definition: GnashImage.h:48
gnash::image::ImageRGB::ImageRGB
ImageRGB(size_t width, size_t height)
Create an empty RGB image with uninitialized data.
Definition: GnashImage.cpp:109
gnash::image::Input
The base class for reading image data.
Definition: GnashImage.h:260
gnash::image::Input::getComponents
virtual size_t getComponents() const =0
Get number of components (channels)
gnash::image::GnashImage::_location
const ImageLocation _location
Image data location (CPU or GPU)
Definition: GnashImage.h:200
gnash::image::Input::readScanline
virtual void readScanline(unsigned char *rgbData)=0
Read a scanline's worth of image data into the given buffer.
GnashImageJpeg.h
gnash::image::GNASH_IMAGE_CPU
@ GNASH_IMAGE_CPU
Definition: GnashImage.h:56
gnash::GNASH_FILETYPE_PNG
@ GNASH_FILETYPE_PNG
Definition: GnashEnums.h:27
log.h
gnash::image::ImageRGB::~ImageRGB
virtual ~ImageRGB()
Definition: GnashImage.cpp:115
gnash::image::Input::_inStream
std::shared_ptr< IOChannel > _inStream
Definition: GnashImage.h:324
gnash::image::GnashImage::size
size_t size() const
Get the size of the image buffer.
Definition: GnashImage.h:105
gnash::image::JpegOutput::create
static std::unique_ptr< Output > create(std::shared_ptr< IOChannel > out, size_t width, size_t height, int quality)
Create a JpegOutput, transferring ownership to the caller.
Definition: GnashImageJpeg.cpp:624
gnash::image::Output::_width
const size_t _width
Definition: GnashImage.h:379
gnash::image::TYPE_RGB
@ TYPE_RGB
Definition: GnashImage.h:49
gnash::image::Input::readSWFJpeg3
static DSOEXPORT std::unique_ptr< ImageRGBA > readSWFJpeg3(std::shared_ptr< gnash::IOChannel > in)
For reading SWF JPEG3-style image data, like ordinary JPEG, but stores the data in ImageRGBA format.
Definition: GnashImage.cpp:283
gnash::image::Input::getWidth
virtual size_t getWidth() const =0
Get the image's width in pixels.
gnash::image::Output::Output
Output(std::shared_ptr< IOChannel > out, size_t width, size_t height)
Construct an Output for writing to an IOChannel.
Definition: GnashImage.h:342
gnash::GNASH_FILETYPE_GIF
@ GNASH_FILETYPE_GIF
Definition: GnashEnums.h:28
gnash::image::GnashImage::begin
virtual iterator begin()
Access the raw data.
Definition: GnashImage.h:154
gnash::image::GnashImage::~GnashImage
virtual ~GnashImage()
Definition: GnashImage.h:86
gnash::image::ImageRGBA::setPixel
void setPixel(size_t x, size_t y, value_type r, value_type g, value_type b, value_type a)
Set pixel value.
Definition: GnashImage.cpp:130
gnash::image::Output::_height
const size_t _height
Definition: GnashImage.h:381
DSOEXPORT
#define DSOEXPORT
Definition: dsodefs.h:55
gnash::image::GnashImage::_data
container_type _data
Data if held in this class.
Definition: GnashImage.h:209
gnash::image::ImageType
ImageType
The types of images handled in Gnash.
Definition: GnashImage.h:47
gnash::image::Output::~Output
virtual ~Output()
Definition: GnashImage.h:349
gnash::image::Input::read
virtual void read()=0
Begin processing the image data.
gnash::image::Input::_type
ImageType _type
Definition: GnashImage.h:326
gnash::image::GnashImage::_width
const size_t _width
Width of image, in pixels.
Definition: GnashImage.h:203
gnash::image::Input::readImageData
static DSOEXPORT std::unique_ptr< GnashImage > readImageData(std::shared_ptr< gnash::IOChannel > in, FileType type)
Read image data from an IOChannel into an GnashImage.
Definition: GnashImage.cpp:215
gnash::image::GNASH_IMAGE_GPU
@ GNASH_IMAGE_GPU
Definition: GnashImage.h:57
gnash::image::ImageRGBA::~ImageRGBA
~ImageRGBA()
Definition: GnashImage.cpp:125
width
@ width
Definition: klash_part.cpp:329
x
std::int32_t x
Definition: BitmapData_as.cpp:434
gnash::image::ImageRGBA::ImageRGBA
ImageRGBA(size_t width, size_t height)
Create an empty RGB image with uninitialized data.
Definition: GnashImage.cpp:119
gnash::key::b
@ b
Definition: GnashKey.h:148
data
SimpleBuffer data
Definition: LocalConnection_as.cpp:151
IOChannel.h
gnash::image::Output::writeImageData
static DSOEXPORT void writeImageData(FileType type, std::shared_ptr< gnash::IOChannel > out, const GnashImage &image, int quality)
Write the given image to the given IOChannel in a specified format.
Definition: GnashImage.cpp:175
gnash::key::e
@ e
Definition: GnashKey.h:151
gnash::image::GnashImage::update
void update(const_iterator data)
Copy image data from a buffer.
Definition: GnashImage.cpp:94
gnash::image::TYPE_RGBA
@ TYPE_RGBA
Definition: GnashImage.h:50
gnash::image::GnashImage::GnashImage
GnashImage(iterator data, size_t width, size_t height, ImageType type, ImageLocation location=GNASH_IMAGE_CPU)
Construct a GnashImage from a data buffer, taking ownership of the data.
Definition: GnashImage.cpp:63
gnash::image::createPngOutput
std::unique_ptr< Output > createPngOutput(std::shared_ptr< IOChannel > o, size_t width, size_t height, int quality)
Definition: GnashImagePng.cpp:411
GnashImageGif.h
gnash::image::GnashImage::height
size_t height() const
Get the image's width.
Definition: GnashImage.h:133
gnash::image::ImageRGBA
32-bit RGBA bitmap
Definition: GnashImage.h:236
gnash::image::ImageRGB::ImageRGB
ImageRGB(iterator data, size_t width, size_t height)
Create an ImageRGB taking ownership of the data.
Definition: GnashImage.h:224
gnash::image::numChannels
size_t numChannels(ImageType t)
Definition: GnashImage.h:61