1 // GnashImage.h: Base class for reading image data in Gnash.
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
21 // The GnashImage class and subclasses are partly based on the public domain
22 // work of Thatcher Ulrich <tu@tulrich.com> 2002
24 #ifndef GNASH_GNASHIMAGE_H
25 #define GNASH_GNASHIMAGE_H
27 #include <boost/shared_ptr.hpp>
28 #include <boost/noncopyable.hpp>
29 #include <boost/cstdint.hpp>
30 #include <boost/scoped_array.hpp>
33 #include "GnashEnums.h"
37 // Forward declarations
44 /// Image handling functions and classes.
47 /// The types of images handled in Gnash.
55 /// The locations of images handled in Gnash.
63 numChannels(ImageType t
)
75 /// Base class for different types of bitmaps
77 /// 1. Bytes are packed in RGB(A) order.
78 /// 2. Rowstride is equal to channels * width
79 class DSOEXPORT GnashImage
: boost::noncopyable
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
;
88 virtual ~GnashImage() {}
90 /// Return the ImageType of the image.
92 /// This saves guessing when dynamic_cast is used.
93 ImageType
type() const {
97 /// Return the ImageLocation of the image.
99 /// This saves guessing when dynamic_cast is used.
100 ImageLocation
location() const {
104 /// Get the size of the image buffer
106 /// @return The size of the buffer in bytes
107 size_t size() const {
108 return stride() * _height
;
111 /// Get the pitch of the image buffer
113 /// @return The rowstride of the buffer in bytes
114 virtual size_t stride() const {
115 return _width
* channels();
118 /// Get the number of channels
120 /// @return The number of channels
121 size_t channels() const {
122 return numChannels(_type
);
125 /// Get the image's width
127 /// @return The image's width in pixels.
128 size_t width() const {
132 /// Get the image's width
134 /// @return The image's height in pixels.
135 size_t height() const {
139 /// Copy image data from a buffer.
141 /// Note that this buffer MUST have the same rowstride and type, or
142 /// unexpected things will happen. In general, it is only safe to copy
143 /// from another GnashImage or unexpected things will happen.
145 /// @param data buffer to copy data from.
146 void update(const_iterator data
);
148 /// Copy image data from another image data
150 /// Note that this buffer must have the same rowstride and type
152 /// @param from image to copy data from.
153 void update(const GnashImage
& from
);
155 /// Access the raw data.
156 virtual iterator
begin() {
160 /// Access the raw data
161 virtual const_iterator
begin() const {
165 /// An iterator to the end of the data.
167 return begin() + size();
170 /// An iterator to the end of the data.
171 const_iterator
end() const {
172 return begin() + size();
177 /// Construct a GnashImage from a data buffer, taking ownership of the data.
179 /// @param data The raw image data. This class takes ownership.
180 /// @param width The width of the image in pixels.
181 /// @param height The height of the image in pixels.
182 /// @param pitch The pitch (rowstride) of the image in bytes.
183 /// @param type The ImageType of the image.
184 GnashImage(iterator data
, size_t width
, size_t height
, ImageType type
,
185 ImageLocation location
= GNASH_IMAGE_CPU
);
187 /// Construct an empty GnashImage
189 /// Note: there is an arbitrary limit of boost::int32_t::max bytes for the
190 /// total size of the bitmap constructed with this constructor.
192 /// @param width The width of the image in pixels.
193 /// @param height The height of the image in pixels.
194 /// @param type The ImageType of the image.
195 GnashImage(size_t width
, size_t height
, ImageType type
,
196 ImageLocation location
= GNASH_IMAGE_CPU
);
198 /// The type of the image: RGBA or RGB.
199 const ImageType _type
;
201 /// Image data location (CPU or GPU)
202 const ImageLocation _location
;
204 /// Width of image, in pixels
207 /// Height of image, in pixels
208 const size_t _height
;
210 /// Data if held in this class
211 container_type _data
;
215 /// 24-bit RGB bitmap
217 /// Channels are in RGB order.
218 class DSOEXPORT ImageRGB
: public GnashImage
222 /// Create an empty RGB image with uninitialized data.
223 ImageRGB(size_t width
, size_t height
);
225 /// Create an ImageRGB taking ownership of the data.
226 ImageRGB(iterator data
, size_t width
, size_t height
)
228 GnashImage(data
, width
, height
, TYPE_RGB
)
234 /// 32-bit RGBA bitmap
236 /// Channels are in RGBA order.
237 class DSOEXPORT ImageRGBA
: public GnashImage
242 /// Create an empty RGB image with uninitialized data.
243 ImageRGBA(size_t width
, size_t height
);
245 ImageRGBA(iterator data
, size_t width
, size_t height
)
247 GnashImage(data
, width
, height
, TYPE_RGBA
)
254 /// TODO: move in base class ?
256 void setPixel(size_t x
, size_t y
, value_type r
, value_type g
, value_type b
,
260 /// The base class for reading image data.
261 class Input
: boost::noncopyable
265 /// Construct an Input object to read from an IOChannel.
267 /// @param in The stream to read data from. Ownership is shared
268 /// between caller and Input, so it is freed
269 /// automatically when the last owner is destroyed.
270 Input(boost::shared_ptr
<IOChannel
> in
)
273 _type(GNASH_IMAGE_INVALID
)
278 /// Begin processing the image data.
279 virtual void read() = 0;
281 /// Get the image's height in pixels.
283 /// @return The height of the image in pixels.
284 virtual size_t getHeight() const = 0;
286 /// Get the image's width in pixels.
288 /// @return The width of the image in pixels.
289 virtual size_t getWidth() const = 0;
291 /// Get number of components (channels)
293 /// @return The number of components, e.g. 3 for RGB
294 virtual size_t getComponents() const = 0;
296 /// Read a scanline's worth of image data into the given buffer.
298 /// @param rgbData The buffer for writing raw RGB data to.
299 virtual void readScanline(unsigned char* rgbData
) = 0;
301 /// Get the ImageType of the image.
303 /// @return The type of the image. This is GNASH_IMAGE_INVALID
304 /// at least until read() has been called and reads some
306 ImageType
imageType() { return _type
; }
309 /// For reading SWF JPEG3-style image data, like ordinary JPEG,
310 /// but stores the data in ImageRGBA format.
311 DSOEXPORT
static std::auto_ptr
<ImageRGBA
> readSWFJpeg3(
312 boost::shared_ptr
<gnash::IOChannel
> in
);
314 /// Read image data from an IOChannel into an GnashImage.
316 /// @param in The IOChannel to read the image from.
317 /// @param type The type of image to read.
318 /// @return An GnashImage with the read image data. If type
319 /// is an unsupported FileType or image reading fails,
320 /// a NULL auto_ptr is returned.
321 DSOEXPORT
static std::auto_ptr
<GnashImage
> readImageData(
322 boost::shared_ptr
<gnash::IOChannel
> in
, FileType type
);
326 boost::shared_ptr
<IOChannel
> _inStream
;
332 // Base class for writing image data.
333 class Output
: boost::noncopyable
338 /// Construct an Output for writing to an IOChannel
340 /// @param out The gnash::IOChannel to write the image to. Ownership
342 /// @param width The width of the resulting image
343 /// @param height The height of the resulting image.
344 Output(boost::shared_ptr
<IOChannel
> out
, size_t width
, size_t height
)
353 /// Write RGB image data using the parameters supplied at construction.
355 /// @param rgbData The raw RGB image data to write as an image.
356 virtual void writeImageRGB(const unsigned char* rgbData
) = 0;
358 /// Write RGBA image data using the parameters supplied at construction.
360 /// @param rgbaData The raw RGBA image data to write as an image.
361 virtual void writeImageRGBA(const unsigned char* /*rgbaData*/)
363 log_error(_("This image format does not support writing RGBA images"));
366 /// Write the given image to the given IOChannel in a specified format.
368 /// @param type The image format to write in (see GnashEnums.h)
369 /// @param out The IOChannel to write to.
370 /// @param image The image to write.
371 /// @param quality The quality of the image output, from 0..100. Values
372 /// outside this range will be clamped to the minimum or
373 /// maxium value. The quality is not used for all
375 DSOEXPORT
static void writeImageData(FileType type
,
376 boost::shared_ptr
<gnash::IOChannel
> out
, const GnashImage
& image
,
383 const size_t _height
;
385 boost::shared_ptr
<IOChannel
> _outStream
;
389 /// Get a pointer to a given row of any image.
391 /// @param row The index of the required row.
392 /// @return A pointer to the first byte of the specified row.
393 inline GnashImage::iterator
394 scanline(GnashImage
& im
, size_t row
)
396 assert(row
< im
.height());
397 return im
.begin() + im
.stride() * row
;
400 /// Get a read-only pointer to a given row of any image.
402 /// @param y The index of the required row.
403 /// @return A read-only pointer to the first byte of the specified row.
404 inline GnashImage::const_iterator
405 scanline(const GnashImage
& im
, size_t row
)
407 assert(row
< im
.height());
408 return im
.begin() + im
.stride() * row
;
411 DSOEXPORT
void mergeAlpha(ImageRGBA
& im
, GnashImage::const_iterator alphaData
,
412 const size_t bufferLength
);