Back to index

nux  3.0.0
ImageSurface.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef IMAGESURFACE_H
00024 #define IMAGESURFACE_H
00025 
00026 #include "BitmapFormats.h"
00027 #include "NuxCore/Math/MathUtility.h"
00028 
00029 #include <gdk-pixbuf/gdk-pixbuf.h>
00030 
00031 #define DEBUG_ENTER(a)
00032 #define DEBUG_WARNING(a)
00033 #define DEBUG_EXIT(a)
00034 
00035 namespace nux
00036 {
00037 
00038   class RawData
00039   {
00040   public:
00041     RawData() {};
00042     ~RawData() {};
00043 
00045     unsigned int GetElementCount()
00046     {
00047       return 0;
00048     }
00049 
00051     unsigned int GetElementSize()
00052     {
00053       return 0;
00054     }
00055 
00056     void GetSize() {};
00057 
00058   private:
00059     void *m_RawDataPtr;
00060   };
00061 
00062   struct DXTColBlock
00063   {
00064     unsigned short col0;
00065     unsigned short col1;
00066 
00067     unsigned char row[4];
00068   };
00069 
00070   struct DXT3AlphaBlock
00071   {
00072     unsigned short row[4];
00073   };
00074 
00075   struct DXT5AlphaBlock
00076   {
00077     unsigned char alpha0;
00078     unsigned char alpha1;
00079 
00080     unsigned char row[6];
00081   };
00082 
00084 
00087   class ImageSurface
00088   {
00089   public:
00090     ImageSurface();
00091     ~ImageSurface();
00092     ImageSurface(BitmapFormat format, unsigned int width, unsigned int height);
00093     ImageSurface(const ImageSurface &);
00094     ImageSurface &operator = (const ImageSurface &);
00095 
00096     bool IsNull() const;
00097     int GetWidth() const
00098     {
00099       return width_;
00100     }
00101     int GetHeight() const
00102     {
00103       return height_;
00104     }
00105     void Allocate(BitmapFormat format, int width, int height);
00106     void Write32b(int i, int j, unsigned int value);
00107     void Write24b(int i, int j, unsigned int value);
00108     void Write16b(int i, int j, unsigned short value);
00109     void Write8b(int i, int j, unsigned char value);
00110     void Write(int i, int j, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
00112 
00118     unsigned int Read(int i, int j);
00120     void Clear();
00122     void FlipHorizontal();
00124     void FlipVertical();
00126     int GetPitch() const;
00127     int GetBlockHeight() const;
00128     int GetAlignment() const;
00129     int GetSize() const;
00130     BitmapFormat GetFormat() const;
00131     const unsigned char *GetPtrRawData() const;
00132     unsigned char *GetPtrRawData();
00133 
00134     static int GetLevelPitch(BitmapFormat format, int width, int height, int miplevel);
00135     static int GetLevelPitchNoMemAlignment(BitmapFormat format, int width, int height, int miplevel);
00136     static int GetLevelSize(BitmapFormat format, int width, int height, int miplevel);
00137     static int GetLevelSize(BitmapFormat format, int width, int height, int depth, int miplevel);
00138     static int GetLevelWidth(BitmapFormat format, int width, int miplevel);
00139     static int GetLevelHeight(BitmapFormat format, int height, int miplevel);
00140     static int GetLevelDim(BitmapFormat format, int length, int miplevel);
00141     static int GetNumMipLevel(BitmapFormat format, int width, int height);
00142     static int GetMemAlignment(BitmapFormat format);
00143     static int GetLevelBlockWidth(BitmapFormat format, int width, int miplevel);
00144     static int GetLevelBlockHeight(BitmapFormat format, int height, int miplevel); // for DXT
00145 
00146     // Image Processing
00148 
00152     Color AverageColor();
00153 
00154   private:
00155     void FlipDXTVertical();
00156     void SwapBlocks(void *byte1, void *byte2, int size);
00157     void FlipBlocksDXT1(DXTColBlock *line, int numBlocks);
00158     void FlipBlocksDXT3(DXTColBlock *line, int numBlocks);
00159     void FlipBlocksDXT5(DXTColBlock *line, int numBlocks);
00160     void FlipDXT5Alpha(DXT5AlphaBlock *block);
00161 
00162 
00163     int width_;           
00164     int height_;          
00165     BitmapFormat format_;   
00166     int m_Pitch;          
00167     int bpe_;             
00168     int Alignment_;       
00169     unsigned char    *RawData_;
00170   };
00171 
00172 
00173   class NBitmapData
00174   {
00175   public:
00176     NBitmapData();
00177     virtual ~NBitmapData();
00178 
00179     virtual const ImageSurface &GetSurface(int MipLevel) const = 0;
00180     virtual ImageSurface &GetSurface(int MipLevel) = 0;
00181     virtual const ImageSurface &GetSurface(int face, int MipLevel) const = 0;
00182     virtual ImageSurface &GetSurface(int face, int MipLevel) = 0;
00183 
00184     virtual bool IsTextureData() const
00185     {
00186       return false;
00187     };
00188     virtual bool IsCubemapTextureData() const
00189     {
00190       return false;
00191     }
00192     virtual bool IsVolumeTextureData() const
00193     {
00194       return false;
00195     }
00196     virtual bool IsAnimatedTextureData() const
00197     {
00198       return false;
00199     }
00200 
00201     virtual int GetNumMipmap() const = 0;
00202     virtual int GetWidth() const = 0;
00203     virtual int GetHeight() const = 0;
00204     virtual int GetDepth() const
00205     {
00206       return 0;
00207     }
00208     virtual BitmapFormat GetFormat() const = 0;
00209     virtual bool IsNull() const = 0;
00210     virtual int GetMemorySize() const
00211     {
00212       return m_TotalMemorySize;
00213     }
00214 
00215   protected:
00216     unsigned int m_TotalMemorySize;
00217 //protected:
00218 //    unsigned int m_Width;
00219 //    unsigned int m_Height;
00220 //    BitmapFormat m_Format;
00221   };
00222 
00223   class NTextureData: public NBitmapData
00224   {
00225   public:
00226     NTextureData(BitmapFormat f = BITFMT_R8G8B8A8, int width = 16, int height = 16, int NumMipmap = 1);
00227     virtual ~NTextureData();
00229     NTextureData(const NTextureData &);
00231     NTextureData &operator = (const NTextureData &);
00232 
00233     virtual void Allocate(BitmapFormat f, int width, int height, int NumMipmap = 1);
00234     virtual void AllocateCheckBoardTexture(int width, int height, int NumMipmap, Color color0, Color color1, int TileWidth = 4, int TileHeight = 4);
00235     virtual void AllocateColorTexture(int width, int height, int NumMipmap, Color color0 = Color(0xFFFFFFF));
00236 
00237     virtual const ImageSurface &GetSurface(int MipLevel) const
00238     {
00239       return *m_MipSurfaceArray[MipLevel];
00240     };
00241     virtual ImageSurface &GetSurface(int MipLevel)
00242     {
00243       return const_cast<ImageSurface &> ((const_cast< const NTextureData * > (this))->GetSurface(MipLevel));
00244     }
00245     virtual const ImageSurface &GetSurface(int face, int MipLevel) const
00246     {
00247       //nuxAssertMsg(0, "[NTextureData::GetSurface] Use GetSurface(unsigned int MipLevel) for NTextureData.");
00248       return GetSurface(MipLevel);
00249     }
00250     virtual ImageSurface &GetSurface(int face, int MipLevel)
00251     {
00252       //nuxAssertMsg(0, "[NTextureData::GetSurface] Use GetSurface(unsigned int MipLevel) for NTextureData.");
00253       return GetSurface(MipLevel);
00254     }
00255 
00256     bool SetSurface(int MipLevel, const ImageSurface &object);
00257 
00258     virtual bool IsTextureData() const
00259     {
00260       return true;
00261     }
00262 
00263     virtual int GetNumMipmap() const;
00264     virtual int GetWidth() const
00265     {
00266       return m_MipSurfaceArray[0]->GetWidth();
00267     }
00268     virtual int GetHeight() const
00269     {
00270       return m_MipSurfaceArray[0]->GetHeight();
00271     }
00272     virtual BitmapFormat GetFormat() const
00273     {
00274       return m_MipSurfaceArray[0]->GetFormat();
00275     }
00276     virtual bool IsNull() const
00277     {
00278       return m_MipSurfaceArray[0]->IsNull();
00279     }
00280 
00281   private:
00282     int m_NumMipmap;
00283     std::vector<ImageSurface *> m_MipSurfaceArray;
00284     void ClearData();
00285 #if defined(NUX_OS_WINDOWS)
00286     friend NBitmapData *read_tga_file(const TCHAR *file_name);
00287 #endif
00288   };
00289 
00290   class NCubemapData: public NBitmapData
00291   {
00292   public:
00293     NCubemapData(BitmapFormat f = BITFMT_R8G8B8A8, int width = 16, int height = 16, int NumMipmap = 1);
00294     virtual ~NCubemapData();
00296     NCubemapData(const NCubemapData &);
00298     NCubemapData &operator = (const NCubemapData &);
00299 
00300     virtual void Allocate(BitmapFormat f, int width, int height, int NumMipmap = 1);
00301     virtual void AllocateCheckBoardTexture(int width, int height, int NumMipmap, Color color0, Color color1, int TileWidth = 4, int TileHeight = 4);
00302     virtual void AllocateColorTexture(int width, int height, int NumMipmap, Color color0 = Color(0xFFFFFFF));
00303 
00304     virtual const ImageSurface &GetSurface(int face, int MipLevel) const
00305     {
00306       return *m_MipSurfaceArray[face][MipLevel];
00307     };
00308     virtual ImageSurface &GetSurface(int face, int MipLevel)
00309     {
00310       return const_cast<ImageSurface &> ((const_cast< const NCubemapData * > (this))->GetSurface(face, MipLevel));
00311     }
00312     virtual const ImageSurface &GetSurface(int MipLevel) const
00313     {
00314       nuxAssertMsg(0, "[NCubemapData::GetSurface] Use GetSurface(unsigned int face, unsigned int MipLevel) for NCubemapData.");
00315       return GetSurface(0, MipLevel);
00316     }
00317     virtual ImageSurface &GetSurface(int MipLevel)
00318     {
00319       nuxAssertMsg(0, "[NCubemapData::GetSurface] Use GetSurface(unsigned int face, unsigned int MipLevel) for NCubemapData.");
00320       return GetSurface(0, MipLevel);
00321     }
00322     bool SetSurface(int face, int MipLevel, const ImageSurface &object);
00323 
00324     virtual bool IsCubemapTextureData() const
00325     {
00326       return true;
00327     }
00328 
00329     virtual int GetNumMipmap() const;
00330     virtual int GetWidth() const
00331     {
00332       return m_MipSurfaceArray[0][0]->GetWidth();
00333     }
00334     virtual int GetHeight() const
00335     {
00336       return m_MipSurfaceArray[0][0]->GetHeight();
00337     }
00338     virtual BitmapFormat GetFormat() const
00339     {
00340       return m_MipSurfaceArray[0][0]->GetFormat();
00341     }
00342     virtual bool IsNull() const
00343     {
00344       return m_MipSurfaceArray[0][0]->IsNull();
00345     }
00346 
00347   private:
00348     void ClearData();
00349     int m_NumMipmap;
00350     std::vector<ImageSurface *> m_MipSurfaceArray[6];
00351   };
00352 
00353   class NVolumeData: public NBitmapData
00354   {
00355   public:
00356     NVolumeData(BitmapFormat f = BITFMT_R8G8B8A8, int width = 16, int height = 16, int slice = 1, int NumMipmap = 1);
00357     virtual ~NVolumeData();
00359     NVolumeData(const NVolumeData &);
00361     NVolumeData &operator = (const NVolumeData &);
00362 
00363     virtual void Allocate(BitmapFormat f, int width, int height, int slice, int NumMipmap = 1);
00364     virtual void AllocateCheckBoardTexture(int width, int height, int slice, int NumMipmap, Color color0, Color color1, int TileWidth = 4, int TileHeight = 4);
00365     virtual void AllocateColorTexture(int width, int height, int slice, int NumMipmap, Color color0 = Color(0xFFFFFFF));
00366 
00367     virtual const ImageSurface &GetSurface(int MipLevel, int slice) const
00368     {
00369       return *m_MipSurfaceArray[MipLevel][slice];
00370     };
00371     virtual ImageSurface &GetSurface(int MipLevel, int slice)
00372     {
00373       return const_cast<ImageSurface &> ((const_cast< const NVolumeData * > (this))->GetSurface(MipLevel, slice));
00374     }
00375     virtual const ImageSurface &GetSurface(int MipLevel) const
00376     {
00377       nuxAssertMsg(0, "[NVolumeData::GetSurface] Use GetSurface(unsigned int MipLevel, unsigned int MipLevel) for NVolumeData.");
00378       return GetSurface(MipLevel, 0);
00379     }
00380     virtual ImageSurface &GetSurface(int MipLevel)
00381     {
00382       nuxAssertMsg(0, "[NVolumeData::GetSurface] Use GetSurface(unsigned int MipLevel, unsigned int MipLevel) for NVolumeData.");
00383       return GetSurface(MipLevel, 0);
00384     }
00385     bool SetSurface(int face, int MipLevel, const ImageSurface &object);
00386 
00387     virtual bool IsVolumeTextureData() const
00388     {
00389       return true;
00390     }
00391 
00392     int GetNumMipmap() const;
00393     virtual int GetWidth() const
00394     {
00395       return m_MipSurfaceArray[0][0]->GetWidth();
00396     }
00397     virtual int GetHeight() const
00398     {
00399       return m_MipSurfaceArray[0][0]->GetHeight();
00400     }
00401     virtual int GetDepth() const
00402     {
00403       return m_Depth;
00404     }
00405     virtual BitmapFormat GetFormat() const
00406     {
00407       return m_MipSurfaceArray[0][0]->GetFormat();
00408     }
00409     virtual bool IsNull() const
00410     {
00411       return m_MipSurfaceArray[0][0]->IsNull();
00412     }
00413 
00414   private:
00415     void ClearData();
00416     int m_NumMipmap;
00417     int m_Depth;
00418     std::vector<ImageSurface *> *m_MipSurfaceArray;
00419   };
00420 
00421   class NAnimatedTextureData: public NBitmapData
00422   {
00423   public:
00424     NAnimatedTextureData(BitmapFormat f = BITFMT_R8G8B8A8, int width = 16, int height = 16, int slice = 1 /*, unsigned int NumMipmap = 1*/);
00425     virtual ~NAnimatedTextureData();
00427     NAnimatedTextureData(const NAnimatedTextureData &);
00429     NAnimatedTextureData &operator = (const NAnimatedTextureData &);
00430 
00431     virtual void Allocate(BitmapFormat f, int width, int height, int slice, int NumMipmap = 1);
00432     virtual void AllocateCheckBoardTexture(int width, int height, int slice, int NumMipmap, Color color0, Color color1, int TileWidth = 4, int TileHeight = 4);
00433     virtual void AllocateColorTexture(int width, int height, int slice, int NumMipmap, Color color0 = Color(0xFFFFFFF));
00434 
00435     virtual const ImageSurface &GetSurface(int MipLevel, int slice) const
00436     {
00437       nuxAssertMsg(0, "[NAnimatedTextureData::GetSurface] Use GetSurface(unsigned int Frame) for NAnimatedTextureData.");
00438       return GetSurface(0);
00439     }
00440     virtual ImageSurface &GetSurface(int MipLevel, int slice)
00441     {
00442       nuxAssertMsg(0, "[NAnimatedTextureData::GetSurface] Use GetSurface(unsigned int Frame) for NAnimatedTextureData.");
00443       return GetSurface(0);
00444     }
00445     virtual const ImageSurface &GetSurface(int Frame) const
00446     {
00447       nuxAssert(Frame >= 0);
00448       nuxAssert(Frame < m_Depth);
00449       return *m_MipSurfaceArray[0][Frame];
00450     }
00451     virtual ImageSurface &GetSurface(int Frame)
00452     {
00453       nuxAssert(Frame >= 0);
00454       nuxAssert(Frame < m_Depth);
00455       return const_cast<ImageSurface &> ((const_cast< const NAnimatedTextureData * > (this))->GetSurface(Frame));
00456     }
00457 
00458     bool SetSurface(int face, int MipLevel, const ImageSurface &object);
00459 
00460     virtual bool IsAnimatedTextureData() const
00461     {
00462       return true;
00463     }
00464 
00465     int GetFrameTime(int Frame) const
00466     {
00467       nuxAssert(Frame >= 0);
00468       nuxAssert(Frame < m_Depth);
00469       return m_FrameTimeArray[Frame];
00470     }
00471 
00472     void AddFrameTime(unsigned int FrameTime)
00473     {
00474       m_FrameTimeArray.push_back(FrameTime);
00475     }
00476 
00477     int GetNumMipmap() const;
00478     virtual int GetWidth() const
00479     {
00480       return m_MipSurfaceArray[0][0]->GetWidth();
00481     }
00482     virtual int GetHeight() const
00483     {
00484       return m_MipSurfaceArray[0][0]->GetHeight();
00485     }
00486     virtual int GetDepth() const
00487     {
00488       return m_Depth;
00489     }
00490     virtual BitmapFormat GetFormat() const
00491     {
00492       return m_MipSurfaceArray[0][0]->GetFormat();
00493     }
00494     virtual bool IsNull() const
00495     {
00496       return m_MipSurfaceArray[0][0]->IsNull();
00497     }
00498 
00499   private:
00500     void ClearData();
00501     int m_NumMipmap;
00502     int m_Depth;
00503     std::vector<ImageSurface *> *m_MipSurfaceArray;
00504     std::vector<unsigned int> m_FrameTimeArray;
00505   };
00506 
00507 
00508   struct ImageInfo
00509   {
00510     bool         isDelegate;        // true if delegate knows this format
00511     int width ;            // Image size(if known)
00512     int height;
00513     int bytes_per_pixel;   // Bytes per pixel(if known)
00514     int planes;            // Number of planes(if known) 0=mono, 3=color
00515     std::string  format;            // Additional image format information
00516   };
00517 
00518   void MakeCheckBoardImage(ImageSurface& Image,
00519                            int width, int height,
00520                            Color const& dark, Color const& light,
00521                            int TileWidth = 4, int TileHeight = 4);
00522 
00523   bool HasOpenEXRSupport();
00524 
00530   NBitmapData* LoadGdkPixbuf(GdkPixbuf *pixbuf);
00531 
00537   NBitmapData* LoadImageFile(const TCHAR *Filename);
00538 
00539 }
00540 
00541 #endif // IMAGE_H