Gnash  0.8.10
GnashImage.h
Go to the documentation of this file.
00001 // GnashImage.h: Base class for reading image data in Gnash.
00002 // 
00003 //   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
00004 //   Free Software Foundation, Inc
00005 // 
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 
00021 // The GnashImage class and subclasses are partly based on the public domain
00022 // work of Thatcher Ulrich <tu@tulrich.com> 2002
00023 
00024 #ifndef GNASH_GNASHIMAGE_H
00025 #define GNASH_GNASHIMAGE_H
00026 
00027 #include <boost/shared_ptr.hpp>
00028 #include <boost/noncopyable.hpp>
00029 #include <boost/cstdint.hpp>
00030 #include <boost/scoped_array.hpp>
00031 #include <memory> 
00032 
00033 #include "GnashEnums.h"
00034 #include "log.h"
00035 #include "dsodefs.h"
00036 
00037 // Forward declarations
00038 namespace gnash {
00039     class IOChannel;
00040 }
00041 
00042 namespace gnash {
00043 
00045 namespace image {
00046 
00048 enum ImageType
00049 {
00050     GNASH_IMAGE_INVALID,
00051     TYPE_RGB,
00052     TYPE_RGBA
00053 };
00054 
00056 enum ImageLocation
00057 {
00058     GNASH_IMAGE_CPU = 1,
00059     GNASH_IMAGE_GPU
00060 };
00061 
00062 inline size_t
00063 numChannels(ImageType t)
00064 {
00065     switch (t) {
00066         case TYPE_RGBA:
00067             return 4;
00068         case TYPE_RGB:
00069             return 3;
00070         default:
00071             std::abort();
00072     }
00073 }
00074 
00076 //
00079 class DSOEXPORT GnashImage : boost::noncopyable
00080 {
00081 public:
00082 
00083     typedef boost::uint8_t value_type;
00084     typedef boost::scoped_array<value_type> container_type;
00085     typedef value_type* iterator;
00086     typedef const value_type* const_iterator;
00087 
00088     virtual ~GnashImage() {}
00089 
00091     //
00093     ImageType type() const {
00094         return _type;
00095     }
00096 
00098     //
00100     ImageLocation location() const {
00101         return _location;
00102     }
00103 
00105     //
00107     size_t size() const {
00108         return stride() * _height;
00109     }
00110 
00112     //
00114     virtual size_t stride() const {
00115         return _width * channels();
00116     }
00117 
00119     //
00121     size_t channels() const {
00122         return numChannels(_type);
00123     }
00124 
00126     //
00128     size_t width() const {
00129         return _width;
00130     }
00131 
00133     //
00135     size_t height() const {
00136         return _height;
00137     }
00138 
00140     //
00146     void update(const_iterator data);
00147 
00149     //
00153     void update(const GnashImage& from);
00154     
00156     virtual iterator begin() {
00157         return _data.get();
00158     }
00159 
00161     virtual const_iterator begin() const {
00162         return _data.get();
00163     }
00164 
00166     iterator end() {
00167         return begin() + size();
00168     }
00169 
00171     const_iterator end() const {
00172         return begin() + size();
00173     }
00174 
00175 protected:
00176 
00178     //
00184     GnashImage(iterator data, size_t width, size_t height, ImageType type,
00185             ImageLocation location = GNASH_IMAGE_CPU);
00186 
00188     //
00191     //
00195     GnashImage(size_t width, size_t height, ImageType type,
00196                ImageLocation location = GNASH_IMAGE_CPU);
00197 
00199     const ImageType _type;
00200 
00202     const ImageLocation _location;
00203 
00205     const size_t _width;
00206 
00208     const size_t _height;
00209 
00211     container_type _data;
00212 
00213 };
00214 
00216 //
00218 class DSOEXPORT ImageRGB : public GnashImage
00219 {
00220 public:
00221 
00223     ImageRGB(size_t width, size_t height);
00224 
00226     ImageRGB(iterator data, size_t width, size_t height)
00227         :
00228         GnashImage(data, width, height, TYPE_RGB)
00229     {}
00230 
00231     virtual ~ImageRGB();
00232 };
00233 
00235 //
00237 class DSOEXPORT ImageRGBA : public GnashImage
00238 {
00239 
00240 public:
00241 
00243     ImageRGBA(size_t width, size_t height);
00244 
00245     ImageRGBA(iterator data, size_t width, size_t height)
00246         :
00247         GnashImage(data, width, height, TYPE_RGBA)
00248     {}
00249     
00250     ~ImageRGBA();
00251 
00253     //
00256     void setPixel(size_t x, size_t y, value_type r, value_type g, value_type b,
00257             value_type a);
00258 };
00259 
00261 class Input : boost::noncopyable
00262 {
00263 public:
00264 
00266     //
00270     Input(boost::shared_ptr<IOChannel> in)
00271         :
00272         _inStream(in),
00273         _type(GNASH_IMAGE_INVALID)
00274     {}
00275 
00276     virtual ~Input() {}
00277 
00279     virtual void read() = 0;
00280 
00282     //
00284     virtual size_t getHeight() const = 0;
00285 
00287     //
00289     virtual size_t getWidth() const = 0;
00290 
00292     //
00294     virtual size_t getComponents() const = 0;
00295 
00297     //
00299     virtual void readScanline(unsigned char* rgbData) = 0;
00300 
00302     //
00306     ImageType imageType() { return _type; }
00307 
00311     DSOEXPORT static std::auto_ptr<ImageRGBA> readSWFJpeg3(
00312             boost::shared_ptr<gnash::IOChannel> in);
00313 
00315     //
00321     DSOEXPORT static std::auto_ptr<GnashImage> readImageData(
00322             boost::shared_ptr<gnash::IOChannel> in, FileType type);
00323 
00324 protected:
00325 
00326     boost::shared_ptr<IOChannel> _inStream;
00327 
00328     ImageType _type;
00329 
00330 };
00331 
00332 // Base class for writing image data.
00333 class Output : boost::noncopyable
00334 {
00335 
00336 public:
00337 
00339     //
00344     Output(boost::shared_ptr<IOChannel> out, size_t width, size_t height)
00345         :
00346         _width(width),
00347         _height(height),
00348         _outStream(out)
00349     {}
00350 
00351     virtual ~Output() {}
00352     
00354     //
00356     virtual void writeImageRGB(const unsigned char* rgbData) = 0;
00357     
00359     //
00361     virtual void writeImageRGBA(const unsigned char* /*rgbaData*/)
00362     {
00363         log_error(_("This image format does not support writing RGBA images"));
00364     }
00365 
00367     //
00375     DSOEXPORT static void writeImageData(FileType type,
00376             boost::shared_ptr<gnash::IOChannel> out, const GnashImage& image,
00377             int quality);
00378 
00379 protected:
00380 
00381     const size_t _width;
00382 
00383     const size_t _height;
00384     
00385     boost::shared_ptr<IOChannel> _outStream;
00386 
00387 };
00388 
00390 //
00393 inline GnashImage::iterator
00394 scanline(GnashImage& im, size_t row)
00395 {
00396     assert(row < im.height());
00397     return im.begin() + im.stride() * row;
00398 }
00399 
00401 //
00404 inline GnashImage::const_iterator
00405 scanline(const GnashImage& im, size_t row)
00406 {
00407     assert(row < im.height());
00408     return im.begin() + im.stride() * row;
00409 }
00410 
00411 DSOEXPORT void mergeAlpha(ImageRGBA& im, GnashImage::const_iterator alphaData,
00412         const size_t bufferLength);
00413 
00414 } // namespace image
00415 } // namespace gnash
00416 
00417 #endif