nux-0.9.46
|
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 Color AverageColor(); 00153 00154 void GaussianBlur (int radius); 00155 00156 private: 00157 void FlipDXTVertical(); 00158 void SwapBlocks (void *byte1, void *byte2, t_s32 size); 00159 void FlipBlocksDXT1 (DXTColBlock *line, t_s32 numBlocks); 00160 void FlipBlocksDXT3 (DXTColBlock *line, t_s32 numBlocks); 00161 void FlipBlocksDXT5 (DXTColBlock *line, t_s32 numBlocks); 00162 void FlipDXT5Alpha (DXT5AlphaBlock *block); 00163 00164 00165 t_s32 width_; 00166 t_s32 height_; 00167 BitmapFormat format_; 00168 t_s32 m_Pitch; 00169 t_s32 bpe_; 00170 t_s32 Alignment_; 00171 t_u8 *RawData_; 00172 }; 00173 00174 00175 class NBitmapData 00176 { 00177 public: 00178 NBitmapData(); 00179 virtual ~NBitmapData(); 00180 00181 virtual const ImageSurface &GetSurface (t_s32 MipLevel) const = 0; 00182 virtual ImageSurface &GetSurface (t_s32 MipLevel) = 0; 00183 virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const = 0; 00184 virtual ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) = 0; 00185 00186 virtual bool IsTextureData() const 00187 { 00188 return false; 00189 }; 00190 virtual bool IsCubemapTextureData() const 00191 { 00192 return false; 00193 } 00194 virtual bool IsVolumeTextureData() const 00195 { 00196 return false; 00197 } 00198 virtual bool IsAnimatedTextureData() const 00199 { 00200 return false; 00201 } 00202 00203 virtual t_s32 GetNumMipmap() const = 0; 00204 virtual t_s32 GetWidth() const = 0; 00205 virtual t_s32 GetHeight() const = 0; 00206 virtual t_s32 GetDepth() const 00207 { 00208 return 0; 00209 } 00210 virtual BitmapFormat GetFormat() const = 0; 00211 virtual bool IsNull() const = 0; 00212 virtual t_s32 GetMemorySize() const 00213 { 00214 return m_TotalMemorySize; 00215 } 00216 00217 protected: 00218 t_u32 m_TotalMemorySize; 00219 //protected: 00220 // t_u32 m_Width; 00221 // t_u32 m_Height; 00222 // BitmapFormat m_Format; 00223 }; 00224 00225 class NTextureData: public NBitmapData 00226 { 00227 public: 00228 NTextureData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 NumMipmap = 1); 00229 virtual ~NTextureData(); 00231 NTextureData (const NTextureData &); 00233 NTextureData &operator = (const NTextureData &); 00234 00235 virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 NumMipmap = 1); 00236 virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4); 00237 virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) ); 00238 00239 virtual const ImageSurface &GetSurface (t_s32 MipLevel) const 00240 { 00241 return *m_MipSurfaceArray[MipLevel]; 00242 }; 00243 virtual ImageSurface &GetSurface (t_s32 MipLevel) 00244 { 00245 return const_cast<ImageSurface &> ( (const_cast< const NTextureData * > (this) )->GetSurface (MipLevel) ); 00246 } 00247 virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const 00248 { 00249 //nuxAssertMsg(0, TEXT("[NTextureData::GetSurface] Use GetSurface(t_u32 MipLevel) for NTextureData.")); 00250 return GetSurface (MipLevel); 00251 } 00252 virtual ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) 00253 { 00254 //nuxAssertMsg(0, TEXT("[NTextureData::GetSurface] Use GetSurface(t_u32 MipLevel) for NTextureData.")); 00255 return GetSurface (MipLevel); 00256 } 00257 00258 bool SetSurface (t_s32 MipLevel, const ImageSurface &object); 00259 00260 virtual bool IsTextureData() const 00261 { 00262 return true; 00263 } 00264 00265 virtual t_s32 GetNumMipmap() const; 00266 virtual t_s32 GetWidth() const 00267 { 00268 return m_MipSurfaceArray[0]->GetWidth(); 00269 } 00270 virtual t_s32 GetHeight() const 00271 { 00272 return m_MipSurfaceArray[0]->GetHeight(); 00273 } 00274 virtual BitmapFormat GetFormat() const 00275 { 00276 return m_MipSurfaceArray[0]->GetFormat(); 00277 } 00278 virtual bool IsNull() const 00279 { 00280 return m_MipSurfaceArray[0]->IsNull(); 00281 } 00282 00283 private: 00284 t_s32 m_NumMipmap; 00285 std::vector<ImageSurface *> m_MipSurfaceArray; 00286 void ClearData(); 00287 00288 friend NBitmapData *read_tga_file (const TCHAR *file_name); 00289 friend NBitmapData *read_bmp_file (const TCHAR *file_name); 00290 friend NBitmapData *read_png_rgb (const TCHAR *filename); 00291 friend NBitmapData *read_png_rgba (const TCHAR *filename); 00292 00293 }; 00294 00295 class NCubemapData: public NBitmapData 00296 { 00297 public: 00298 NCubemapData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 NumMipmap = 1); 00299 virtual ~NCubemapData(); 00301 NCubemapData (const NCubemapData &); 00303 NCubemapData &operator = (const NCubemapData &); 00304 00305 virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 NumMipmap = 1); 00306 virtual void AllocateCheckBoardTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4); 00307 virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) ); 00308 00309 virtual const ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) const 00310 { 00311 return *m_MipSurfaceArray[face][MipLevel]; 00312 }; 00313 virtual ImageSurface &GetSurface (t_s32 face, t_s32 MipLevel) 00314 { 00315 return const_cast<ImageSurface &> ( (const_cast< const NCubemapData * > (this) )->GetSurface (face, MipLevel) ); 00316 } 00317 virtual const ImageSurface &GetSurface (t_s32 MipLevel) const 00318 { 00319 nuxAssertMsg (0, TEXT ("[NCubemapData::GetSurface] Use GetSurface(t_u32 face, t_u32 MipLevel) for NCubemapData.") ); 00320 return GetSurface (0, MipLevel); 00321 } 00322 virtual ImageSurface &GetSurface (t_s32 MipLevel) 00323 { 00324 nuxAssertMsg (0, TEXT ("[NCubemapData::GetSurface] Use GetSurface(t_u32 face, t_u32 MipLevel) for NCubemapData.") ); 00325 return GetSurface (0, MipLevel); 00326 } 00327 bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object); 00328 00329 virtual bool IsCubemapTextureData() const 00330 { 00331 return true; 00332 } 00333 00334 virtual t_s32 GetNumMipmap() const; 00335 virtual t_s32 GetWidth() const 00336 { 00337 return m_MipSurfaceArray[0][0]->GetWidth(); 00338 } 00339 virtual t_s32 GetHeight() const 00340 { 00341 return m_MipSurfaceArray[0][0]->GetHeight(); 00342 } 00343 virtual BitmapFormat GetFormat() const 00344 { 00345 return m_MipSurfaceArray[0][0]->GetFormat(); 00346 } 00347 virtual bool IsNull() const 00348 { 00349 return m_MipSurfaceArray[0][0]->IsNull(); 00350 } 00351 00352 private: 00353 void ClearData(); 00354 t_s32 m_NumMipmap; 00355 std::vector<ImageSurface *> m_MipSurfaceArray[6]; 00356 }; 00357 00358 class NVolumeData: public NBitmapData 00359 { 00360 public: 00361 NVolumeData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 slice = 1, t_s32 NumMipmap = 1); 00362 virtual ~NVolumeData(); 00364 NVolumeData (const NVolumeData &); 00366 NVolumeData &operator = (const NVolumeData &); 00367 00368 virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap = 1); 00369 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); 00370 virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) ); 00371 00372 virtual const ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) const 00373 { 00374 return *m_MipSurfaceArray[MipLevel][slice]; 00375 }; 00376 virtual ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) 00377 { 00378 return const_cast<ImageSurface &> ( (const_cast< const NVolumeData * > (this) )->GetSurface (MipLevel, slice) ); 00379 } 00380 virtual const ImageSurface &GetSurface (t_s32 MipLevel) const 00381 { 00382 nuxAssertMsg (0, TEXT ("[NVolumeData::GetSurface] Use GetSurface(t_u32 MipLevel, t_u32 MipLevel) for NVolumeData.") ); 00383 return GetSurface (MipLevel, 0); 00384 } 00385 virtual ImageSurface &GetSurface (t_s32 MipLevel) 00386 { 00387 nuxAssertMsg (0, TEXT ("[NVolumeData::GetSurface] Use GetSurface(t_u32 MipLevel, t_u32 MipLevel) for NVolumeData.") ); 00388 return GetSurface (MipLevel, 0); 00389 } 00390 bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object); 00391 00392 virtual bool IsVolumeTextureData() const 00393 { 00394 return true; 00395 } 00396 00397 t_s32 GetNumMipmap() const; 00398 virtual t_s32 GetWidth() const 00399 { 00400 return m_MipSurfaceArray[0][0]->GetWidth(); 00401 } 00402 virtual t_s32 GetHeight() const 00403 { 00404 return m_MipSurfaceArray[0][0]->GetHeight(); 00405 } 00406 virtual t_s32 GetDepth() const 00407 { 00408 return m_Depth; 00409 } 00410 virtual BitmapFormat GetFormat() const 00411 { 00412 return m_MipSurfaceArray[0][0]->GetFormat(); 00413 } 00414 virtual bool IsNull() const 00415 { 00416 return m_MipSurfaceArray[0][0]->IsNull(); 00417 } 00418 00419 private: 00420 void ClearData(); 00421 t_s32 m_NumMipmap; 00422 t_s32 m_Depth; 00423 std::vector<ImageSurface *> *m_MipSurfaceArray; 00424 }; 00425 00426 class NAnimatedTextureData: public NBitmapData 00427 { 00428 public: 00429 NAnimatedTextureData (BitmapFormat f = BITFMT_R8G8B8A8, t_s32 width = 16, t_s32 height = 16, t_s32 slice = 1 /*, t_u32 NumMipmap = 1*/); 00430 virtual ~NAnimatedTextureData(); 00432 NAnimatedTextureData (const NAnimatedTextureData &); 00434 NAnimatedTextureData &operator = (const NAnimatedTextureData &); 00435 00436 virtual void Allocate (BitmapFormat f, t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap = 1); 00437 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); 00438 virtual void AllocateColorTexture (t_s32 width, t_s32 height, t_s32 slice, t_s32 NumMipmap, Color color0 = Color (0xFFFFFFF) ); 00439 00440 virtual const ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) const 00441 { 00442 nuxAssertMsg (0, TEXT ("[NAnimatedTextureData::GetSurface] Use GetSurface(t_u32 Frame) for NAnimatedTextureData.") ); 00443 return GetSurface (0); 00444 } 00445 virtual ImageSurface &GetSurface (t_s32 MipLevel, t_s32 slice) 00446 { 00447 nuxAssertMsg (0, TEXT ("[NAnimatedTextureData::GetSurface] Use GetSurface(t_u32 Frame) for NAnimatedTextureData.") ); 00448 return GetSurface (0); 00449 } 00450 virtual const ImageSurface &GetSurface (t_s32 Frame) const 00451 { 00452 nuxAssert (Frame >= 0); 00453 nuxAssert (Frame < m_Depth); 00454 return *m_MipSurfaceArray[0][Frame]; 00455 } 00456 virtual ImageSurface &GetSurface (t_s32 Frame) 00457 { 00458 nuxAssert (Frame >= 0); 00459 nuxAssert (Frame < m_Depth); 00460 return const_cast<ImageSurface &> ( (const_cast< const NAnimatedTextureData * > (this) )->GetSurface (Frame) ); 00461 } 00462 00463 bool SetSurface (t_s32 face, t_s32 MipLevel, const ImageSurface &object); 00464 00465 virtual bool IsAnimatedTextureData() const 00466 { 00467 return true; 00468 } 00469 00470 t_s32 GetFrameTime (t_s32 Frame) const 00471 { 00472 nuxAssert (Frame >= 0); 00473 nuxAssert (Frame < m_Depth); 00474 return m_FrameTimeArray[Frame]; 00475 } 00476 00477 void AddFrameTime (t_u32 FrameTime) 00478 { 00479 m_FrameTimeArray.push_back (FrameTime); 00480 } 00481 00482 t_s32 GetNumMipmap() const; 00483 virtual t_s32 GetWidth() const 00484 { 00485 return m_MipSurfaceArray[0][0]->GetWidth(); 00486 } 00487 virtual t_s32 GetHeight() const 00488 { 00489 return m_MipSurfaceArray[0][0]->GetHeight(); 00490 } 00491 virtual t_s32 GetDepth() const 00492 { 00493 return m_Depth; 00494 } 00495 virtual BitmapFormat GetFormat() const 00496 { 00497 return m_MipSurfaceArray[0][0]->GetFormat(); 00498 } 00499 virtual bool IsNull() const 00500 { 00501 return m_MipSurfaceArray[0][0]->IsNull(); 00502 } 00503 00504 private: 00505 void ClearData(); 00506 t_s32 m_NumMipmap; 00507 t_s32 m_Depth; 00508 std::vector<ImageSurface *> *m_MipSurfaceArray; 00509 std::vector<t_u32> m_FrameTimeArray; 00510 }; 00511 00512 00513 struct ImageInfo 00514 { 00515 bool isDelegate; // true if delegate knows this format 00516 t_s32 width ; // Image size (if known) 00517 t_s32 height; 00518 t_s32 bytes_per_pixel; // Bytes per pixel (if known) 00519 t_s32 planes; // Number of planes (if known) 0=mono, 3=color 00520 std::string format; // Additional image format information 00521 }; 00522 00523 void MakeCheckBoardImage (ImageSurface &Image, t_s32 w, t_s32 h, Color color0, Color color1, t_s32 TileWidth = 4, t_s32 TileHeight = 4); 00524 00525 bool HasOpenEXRSupport(); 00526 00527 NBitmapData *LoadGdkPixbuf (GdkPixbuf *pixbuf); 00528 NBitmapData *LoadImageFile (const TCHAR *Filename); 00529 00530 } 00531 00532 #endif // IMAGE_H