nux-1.14.0
ImageSurface.h
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     t_u32 GetElementCount()
00046     {
00047       return 0;
00048     }
00049 
00051     t_u32 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, t_u32 width, t_u32 height);
00093     ImageSurface (const ImageSurface &);
00094     ImageSurface &operator = (const ImageSurface &);
00095 
00096     bool IsNull() const;
00097     t_s32 GetWidth() const
00098     {
00099       return width_;
00100     }
00101     t_s32 GetHeight() const
00102     {
00103       return height_;
00104     }
00105     void Allocate (BitmapFormat format, t_s32 width, t_s32 height);
00106     void Write32b (t_s32 i, t_s32 j, t_u32 value);
00107     void Write24b (t_s32 i, t_s32 j, t_u32 value);
00108     void Write16b (t_s32 i, t_s32 j, t_u16 value);
00109     void Write8b (t_s32 i, t_s32 j, t_u8 value);
00110     void Write (t_s32 i, t_s32 j, t_u8 r, t_u8 g, t_u8 b, t_u8 a);
00112 
00118     t_u32 Read (t_s32 i, t_s32 j);
00120     void Clear();
00122     void FlipHorizontal();
00124     void FlipVertical();
00126     t_s32 GetPitch() const;
00127     t_s32 GetBlockHeight() const;
00128     t_s32 GetAlignment() const;
00129     t_s32 GetSize() const;
00130     BitmapFormat GetFormat() const;
00131     const t_u8 *GetPtrRawData() const;
00132     t_u8 *GetPtrRawData();
00133 
00134     static t_s32 GetLevelPitch (BitmapFormat format, t_s32 width, t_s32 height, t_s32 miplevel);
00135     static t_s32 GetLevelPitchNoMemAlignment (BitmapFormat format, t_s32 width, t_s32 height, t_s32 miplevel);
00136     static t_s32 GetLevelSize (BitmapFormat format, t_s32 width, t_s32 height, t_s32 miplevel);
00137     static t_s32 GetLevelSize (BitmapFormat format, t_s32 width, t_s32 height, t_s32 depth, t_s32 miplevel);
00138     static t_s32 GetLevelWidth (BitmapFormat format, t_s32 width, t_s32 miplevel);
00139     static t_s32 GetLevelHeight (BitmapFormat format, t_s32 height, t_s32 miplevel);
00140     static t_s32 GetLevelDim (BitmapFormat format, t_s32 length, t_s32 miplevel);
00141     static t_s32 GetNumMipLevel (BitmapFormat format, t_s32 width, t_s32 height);
00142     static t_s32 GetMemAlignment (BitmapFormat format);
00143     static t_s32 GetLevelBlockWidth (BitmapFormat format, t_s32 width, t_s32 miplevel);
00144     static t_s32 GetLevelBlockHeight (BitmapFormat format, t_s32 height, t_s32 miplevel); // for DXT
00145 
00146     // Image Processing
00148 
00152     struct Color AverageColor();
00153 
00154   private:
00155     void FlipDXTVertical();
00156     void SwapBlocks (void *byte1, void *byte2, t_s32 size);
00157     void FlipBlocksDXT1 (DXTColBlock *line, t_s32 numBlocks);
00158     void FlipBlocksDXT3 (DXTColBlock *line, t_s32 numBlocks);
00159     void FlipBlocksDXT5 (DXTColBlock *line, t_s32 numBlocks);
00160     void FlipDXT5Alpha (DXT5AlphaBlock *block);
00161 
00162 
00163     t_s32 width_;           
00164     t_s32 height_;          
00165     BitmapFormat format_;   
00166     t_s32 m_Pitch;          
00167     t_s32 bpe_;             
00168     t_s32 Alignment_;       
00169     t_u8    *RawData_;
00170   };
00171 
00172 
00173   class NBitmapData
00174   {
00175   public:
00176     NBitmapData();
00177     virtual ~NBitmapData();
00178 
00179     virtual const ImageSurface &GetSurface (t_s32 MipLevel) const = 0;
00180     virtual ImageSurface &GetSurface (t_s32 MipLevel) = 0;
00181     virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const = 0;
00182     virtual ImageSurface &GetSurface (t_s32 face, t_s32 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 t_s32 GetNumMipmap() const = 0;
00202     virtual t_s32 GetWidth() const = 0;
00203     virtual t_s32 GetHeight() const = 0;
00204     virtual t_s32 GetDepth() const
00205     {
00206       return 0;
00207     }
00208     virtual BitmapFormat GetFormat() const = 0;
00209     virtual bool IsNull() const = 0;
00210     virtual t_s32 GetMemorySize() const
00211     {
00212       return m_TotalMemorySize;
00213     }
00214 
00215   protected:
00216     t_u32 m_TotalMemorySize;
00217 //protected:
00218 //    t_u32 m_Width;
00219 //    t_u32 m_Height;
00220 //    BitmapFormat m_Format;
00221   };
00222 
00223   class NTextureData: public NBitmapData
00224   {
00225   public:
00226     NTextureData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 NumMipmap = 1);
00227     virtual ~NTextureData();
00229     NTextureData (const NTextureData &);
00231     NTextureData &operator = (const NTextureData &);
00232 
00233     virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 NumMipmap = 1);
00234     virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4);
00235     virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) );
00236 
00237     virtual const ImageSurface &GetSurface (t_s32 MipLevel) const
00238     {
00239       return *m_MipSurfaceArray[MipLevel];
00240     };
00241     virtual ImageSurface &GetSurface (t_s32 MipLevel)
00242     {
00243       return const_cast<ImageSurface &> ( (const_cast< const NTextureData * > (this) )->GetSurface (MipLevel) );
00244     }
00245     virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const
00246     {
00247       //nuxAssertMsg(0, TEXT("[NTextureData::GetSurface] Use GetSurface(t_u32 MipLevel) for NTextureData."));
00248       return GetSurface (MipLevel);
00249     }
00250     virtual ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel)
00251     {
00252       //nuxAssertMsg(0, TEXT("[NTextureData::GetSurface] Use GetSurface(t_u32 MipLevel) for NTextureData."));
00253       return GetSurface (MipLevel);
00254     }
00255 
00256     bool SetSurface (t_s32 MipLevel, const ImageSurface &object);
00257 
00258     virtual bool IsTextureData() const
00259     {
00260       return true;
00261     }
00262 
00263     virtual t_s32 GetNumMipmap() const;
00264     virtual t_s32 GetWidth() const
00265     {
00266       return m_MipSurfaceArray[0]->GetWidth();
00267     }
00268     virtual t_s32 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     t_s32 m_NumMipmap;
00283     std::vector<ImageSurface *> m_MipSurfaceArray;
00284     void ClearData();
00285 
00286     friend NBitmapData *read_tga_file (const TCHAR *file_name);
00287     friend NBitmapData *read_bmp_file (const TCHAR *file_name);
00288     friend NBitmapData *read_png_rgb (const TCHAR *filename);
00289     friend NBitmapData *read_png_rgba (const TCHAR *filename);
00290 
00291   };
00292 
00293   class NCubemapData: public NBitmapData
00294   {
00295   public:
00296     NCubemapData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 NumMipmap = 1);
00297     virtual ~NCubemapData();
00299     NCubemapData (const NCubemapData &);
00301     NCubemapData &operator = (const NCubemapData &);
00302 
00303     virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 NumMipmap = 1);
00304     virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4);
00305     virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) );
00306 
00307     virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const
00308     {
00309       return *m_MipSurfaceArray[face][MipLevel];
00310     };
00311     virtual ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel)
00312     {
00313       return const_cast<ImageSurface &> ( (const_cast< const NCubemapData * > (this) )->GetSurface (face, MipLevel) );
00314     }
00315     virtual const ImageSurface &GetSurface (t_s32 MipLevel) const
00316     {
00317       nuxAssertMsg (0, TEXT ("[NCubemapData::GetSurface] Use GetSurface(t_u32 face, t_u32 MipLevel) for NCubemapData.") );
00318       return GetSurface (0, MipLevel);
00319     }
00320     virtual ImageSurface &GetSurface (t_s32 MipLevel)
00321     {
00322       nuxAssertMsg (0, TEXT ("[NCubemapData::GetSurface] Use GetSurface(t_u32 face, t_u32 MipLevel) for NCubemapData.") );
00323       return GetSurface (0, MipLevel);
00324     }
00325     bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object);
00326 
00327     virtual bool IsCubemapTextureData() const
00328     {
00329       return true;
00330     }
00331 
00332     virtual t_s32 GetNumMipmap() const;
00333     virtual t_s32 GetWidth() const
00334     {
00335       return m_MipSurfaceArray[0][0]->GetWidth();
00336     }
00337     virtual t_s32 GetHeight() const
00338     {
00339       return m_MipSurfaceArray[0][0]->GetHeight();
00340     }
00341     virtual BitmapFormat GetFormat() const
00342     {
00343       return m_MipSurfaceArray[0][0]->GetFormat();
00344     }
00345     virtual bool IsNull() const
00346     {
00347       return m_MipSurfaceArray[0][0]->IsNull();
00348     }
00349 
00350   private:
00351     void ClearData();
00352     t_s32 m_NumMipmap;
00353     std::vector<ImageSurface *> m_MipSurfaceArray[6];
00354   };
00355 
00356   class NVolumeData: public NBitmapData
00357   {
00358   public:
00359     NVolumeData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 slice = 1, t_s32 NumMipmap = 1);
00360     virtual ~NVolumeData();
00362     NVolumeData (const NVolumeData &);
00364     NVolumeData &operator = (const NVolumeData &);
00365 
00366     virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap = 1);
00367     virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4);
00368     virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) );
00369 
00370     virtual const ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) const
00371     {
00372       return *m_MipSurfaceArray[MipLevel][slice];
00373     };
00374     virtual ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice)
00375     {
00376       return const_cast<ImageSurface &> ( (const_cast< const NVolumeData * > (this) )->GetSurface (MipLevel, slice) );
00377     }
00378     virtual const ImageSurface &GetSurface (t_s32 MipLevel) const
00379     {
00380       nuxAssertMsg (0, TEXT ("[NVolumeData::GetSurface] Use GetSurface(t_u32 MipLevel, t_u32 MipLevel) for NVolumeData.") );
00381       return GetSurface (MipLevel, 0);
00382     }
00383     virtual ImageSurface &GetSurface (t_s32 MipLevel)
00384     {
00385       nuxAssertMsg (0, TEXT ("[NVolumeData::GetSurface] Use GetSurface(t_u32 MipLevel, t_u32 MipLevel) for NVolumeData.") );
00386       return GetSurface (MipLevel, 0);
00387     }
00388     bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object);
00389 
00390     virtual bool IsVolumeTextureData() const
00391     {
00392       return true;
00393     }
00394 
00395     t_s32 GetNumMipmap() const;
00396     virtual t_s32 GetWidth() const
00397     {
00398       return m_MipSurfaceArray[0][0]->GetWidth();
00399     }
00400     virtual t_s32 GetHeight() const
00401     {
00402       return m_MipSurfaceArray[0][0]->GetHeight();
00403     }
00404     virtual t_s32 GetDepth() const
00405     {
00406       return m_Depth;
00407     }
00408     virtual BitmapFormat GetFormat() const
00409     {
00410       return m_MipSurfaceArray[0][0]->GetFormat();
00411     }
00412     virtual bool IsNull() const
00413     {
00414       return m_MipSurfaceArray[0][0]->IsNull();
00415     }
00416 
00417   private:
00418     void ClearData();
00419     t_s32 m_NumMipmap;
00420     t_s32 m_Depth;
00421     std::vector<ImageSurface *> *m_MipSurfaceArray;
00422   };
00423 
00424   class NAnimatedTextureData: public NBitmapData
00425   {
00426   public:
00427     NAnimatedTextureData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 slice = 1 /*, t_u32 NumMipmap = 1*/);
00428     virtual ~NAnimatedTextureData();
00430     NAnimatedTextureData (const NAnimatedTextureData &);
00432     NAnimatedTextureData &operator = (const NAnimatedTextureData &);
00433 
00434     virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap = 1);
00435     virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4);
00436     virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) );
00437 
00438     virtual const ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) const
00439     {
00440       nuxAssertMsg (0, TEXT ("[NAnimatedTextureData::GetSurface] Use GetSurface(t_u32 Frame) for NAnimatedTextureData.") );
00441       return GetSurface (0);
00442     }
00443     virtual ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice)
00444     {
00445       nuxAssertMsg (0, TEXT ("[NAnimatedTextureData::GetSurface] Use GetSurface(t_u32 Frame) for NAnimatedTextureData.") );
00446       return GetSurface (0);
00447     }
00448     virtual const ImageSurface &GetSurface (t_s32 Frame) const
00449     {
00450       nuxAssert (Frame >= 0);
00451       nuxAssert (Frame < m_Depth);
00452       return *m_MipSurfaceArray[0][Frame];
00453     }
00454     virtual ImageSurface &GetSurface (t_s32 Frame)
00455     {
00456       nuxAssert (Frame >= 0);
00457       nuxAssert (Frame < m_Depth);
00458       return const_cast<ImageSurface &> ( (const_cast< const NAnimatedTextureData * > (this) )->GetSurface (Frame) );
00459     }
00460 
00461     bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object);
00462 
00463     virtual bool IsAnimatedTextureData() const
00464     {
00465       return true;
00466     }
00467 
00468     t_s32 GetFrameTime (t_s32 Frame) const
00469     {
00470       nuxAssert (Frame >= 0);
00471       nuxAssert (Frame < m_Depth);
00472       return m_FrameTimeArray[Frame];
00473     }
00474 
00475     void AddFrameTime (t_u32 FrameTime)
00476     {
00477       m_FrameTimeArray.push_back (FrameTime);
00478     }
00479 
00480     t_s32 GetNumMipmap() const;
00481     virtual t_s32 GetWidth() const
00482     {
00483       return m_MipSurfaceArray[0][0]->GetWidth();
00484     }
00485     virtual t_s32 GetHeight() const
00486     {
00487       return m_MipSurfaceArray[0][0]->GetHeight();
00488     }
00489     virtual t_s32 GetDepth() const
00490     {
00491       return m_Depth;
00492     }
00493     virtual BitmapFormat GetFormat() const
00494     {
00495       return m_MipSurfaceArray[0][0]->GetFormat();
00496     }
00497     virtual bool IsNull() const
00498     {
00499       return m_MipSurfaceArray[0][0]->IsNull();
00500     }
00501 
00502   private:
00503     void ClearData();
00504     t_s32 m_NumMipmap;
00505     t_s32 m_Depth;
00506     std::vector<ImageSurface *> *m_MipSurfaceArray;
00507     std::vector<t_u32> m_FrameTimeArray;
00508   };
00509 
00510 
00511   struct ImageInfo
00512   {
00513     bool         isDelegate;        // true if delegate knows this format
00514     t_s32 width ;            // Image size (if known)
00515     t_s32 height;
00516     t_s32 bytes_per_pixel;   // Bytes per pixel (if known)
00517     t_s32 planes;            // Number of planes (if known) 0=mono, 3=color
00518     std::string  format;            // Additional image format information
00519   };
00520 
00521   void MakeCheckBoardImage(ImageSurface& Image,
00522                            int width, int height,
00523                            Color const& dark, Color const& light,
00524                            int TileWidth = 4, int TileHeight = 4);
00525 
00526   bool HasOpenEXRSupport();
00527 
00533   NBitmapData* LoadGdkPixbuf(GdkPixbuf *pixbuf);
00534 
00540   NBitmapData* LoadImageFile(const TCHAR *Filename);
00541 
00542 }
00543 
00544 #endif // IMAGE_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends