• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List
  • File Members

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 Free Software
00004 //   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 "FileTypes.h"
00034 #include "log.h"
00035 #include "dsodefs.h"
00036 
00037 // Forward declarations
00038 namespace gnash {
00039     class IOChannel;
00040     class JpegInput;
00041 }
00042 
00043 namespace gnash {
00044 
00046 namespace image {
00047 
00049 enum ImageType
00050 {
00051     GNASH_IMAGE_INVALID,
00052     TYPE_RGB,
00053     TYPE_RGBA
00054 };
00055 
00057 enum ImageLocation
00058 {
00059     GNASH_IMAGE_CPU = 1,
00060     GNASH_IMAGE_GPU
00061 };
00062 
00063 inline size_t
00064 numChannels(ImageType t)
00065 {
00066     switch (t) {
00067         case TYPE_RGBA:
00068             return 4;
00069         case TYPE_RGB:
00070             return 3;
00071         default:
00072             std::abort();
00073     }
00074 }
00075 
00077 //
00080 class DSOEXPORT GnashImage : boost::noncopyable
00081 {
00082 public:
00083 
00084     typedef boost::uint8_t value_type;
00085     typedef boost::scoped_array<value_type> container_type;
00086     typedef value_type* iterator;
00087     typedef const value_type* const_iterator;
00088 
00089     virtual ~GnashImage() {}
00090 
00092     //
00094     ImageType type() const {
00095         return _type;
00096     }
00097 
00099     //
00101     ImageLocation location() const {
00102         return _location;
00103     }
00104 
00106     //
00108     size_t size() const {
00109         return stride() * _height;
00110     }
00111 
00113     //
00115     virtual size_t stride() const {
00116         return _width * channels();
00117     }
00118 
00120     //
00122     size_t channels() const {
00123         return numChannels(_type);
00124     }
00125 
00127     //
00129     size_t width() const {
00130         return _width;
00131     }
00132 
00134     //
00136     size_t height() const {
00137         return _height;
00138     }
00139 
00141     //
00147     void update(const_iterator data);
00148 
00150     //
00154     void update(const GnashImage& from);
00155     
00157     virtual iterator begin() {
00158         return _data.get();
00159     }
00160 
00162     virtual const_iterator begin() const {
00163         return _data.get();
00164     }
00165 
00167     iterator end() {
00168         return begin() + size();
00169     }
00170 
00172     const_iterator end() const {
00173         return begin() + size();
00174     }
00175 
00176 protected:
00177 
00179     //
00185     GnashImage(iterator data, size_t width, size_t height, ImageType type,
00186             ImageLocation location = GNASH_IMAGE_CPU);
00187 
00189     //
00192     //
00196     GnashImage(size_t width, size_t height, ImageType type,
00197                ImageLocation location = GNASH_IMAGE_CPU);
00198 
00200     const ImageType _type;
00201 
00203     const ImageLocation _location;
00204 
00206     const size_t _width;
00207 
00209     const size_t _height;
00210 
00212     container_type _data;
00213 
00214 };
00215 
00217 //
00219 class DSOEXPORT ImageRGB : public GnashImage
00220 {
00221 public:
00222 
00224     ImageRGB(size_t width, size_t height);
00225 
00227     ImageRGB(iterator data, size_t width, size_t height)
00228         :
00229         GnashImage(data, width, height, TYPE_RGB)
00230     {}
00231 
00232     virtual ~ImageRGB();
00233 };
00234 
00236 //
00238 class DSOEXPORT ImageRGBA : public GnashImage
00239 {
00240 
00241 public:
00242 
00244     ImageRGBA(size_t width, size_t height);
00245 
00246     ImageRGBA(iterator data, size_t width, size_t height)
00247         :
00248         GnashImage(data, width, height, TYPE_RGBA)
00249     {}
00250     
00251     ~ImageRGBA();
00252 
00254     //
00257     void setPixel(size_t x, size_t y, value_type r, value_type g, value_type b,
00258             value_type a);
00259 };
00260 
00262 class Input : boost::noncopyable
00263 {
00264 public:
00265 
00267     //
00271     Input(boost::shared_ptr<IOChannel> in)
00272         :
00273         _inStream(in),
00274         _type(GNASH_IMAGE_INVALID)
00275     {}
00276 
00277     virtual ~Input() {}
00278 
00280     virtual void read() = 0;
00281 
00283     //
00285     virtual size_t getHeight() const = 0;
00286 
00288     //
00290     virtual size_t getWidth() const = 0;
00291 
00293     //
00295     virtual size_t getComponents() const = 0;
00296 
00298     //
00300     virtual void readScanline(unsigned char* rgbData) = 0;
00301 
00303     //
00307     ImageType imageType() { return _type; }
00308 
00312     DSOEXPORT static std::auto_ptr<ImageRGBA> readSWFJpeg3(
00313             boost::shared_ptr<gnash::IOChannel> in);
00314 
00316     //
00322     DSOEXPORT static std::auto_ptr<GnashImage> readImageData(
00323             boost::shared_ptr<gnash::IOChannel> in, FileType type);
00324 
00325 protected:
00326 
00327     boost::shared_ptr<IOChannel> _inStream;
00328 
00329     ImageType _type;
00330 
00331 };
00332 
00333 // Base class for writing image data.
00334 class Output : boost::noncopyable
00335 {
00336 
00337 public:
00338 
00340     //
00345     Output(boost::shared_ptr<IOChannel> out, size_t width, size_t height)
00346         :
00347         _width(width),
00348         _height(height),
00349         _outStream(out)
00350     {}
00351 
00352     virtual ~Output() {}
00353     
00355     //
00357     virtual void writeImageRGB(const unsigned char* rgbData) = 0;
00358     
00360     //
00362     virtual void writeImageRGBA(const unsigned char* /*rgbaData*/)
00363     {
00364         log_error(_("This image format does not support writing RGBA images"));
00365     }
00366 
00368     //
00374     DSOEXPORT static void writeImageData(FileType type,
00375             boost::shared_ptr<gnash::IOChannel> out, const GnashImage& image,
00376             int quality);
00377 
00378 protected:
00379 
00380     const size_t _width;
00381 
00382     const size_t _height;
00383     
00384     boost::shared_ptr<IOChannel> _outStream;
00385 
00386 };
00387 
00389 //
00392 inline GnashImage::iterator
00393 scanline(GnashImage& im, size_t row)
00394 {
00395     assert(row < im.height());
00396     return im.begin() + im.stride() * row;
00397 }
00398 
00400 //
00403 inline GnashImage::const_iterator
00404 scanline(const GnashImage& im, size_t row)
00405 {
00406     assert(row < im.height());
00407     return im.begin() + im.stride() * row;
00408 }
00409 
00410 void mergeAlpha(ImageRGBA& im, GnashImage::const_iterator alphaData,
00411         const size_t bufferLength);
00412 
00413 } // namespace image
00414 } // namespace gnash
00415 
00416 #endif

Generated on Thu Sep 2 2010 for Gnash by  doxygen 1.7.1