Merge pull request #4314 from MartijnKaijser/beta1
[vuplus_xbmc] / xbmc / guilib / Texture.h
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #pragma once
22
23 #include "system.h"
24 #include "gui3d.h"
25 #include "utils/StdString.h"
26 #include "XBTF.h"
27 #include "guilib/imagefactory.h"
28
29 #pragma pack(1)
30 struct COLOR {unsigned char b,g,r,x;};  // Windows GDI expects 4bytes per color
31 #pragma pack()
32
33 class CTexture;
34 class CGLTexture;
35 class CPiTexture;
36 class CDXTexture;
37
38 /*!
39 \ingroup textures
40 \brief Base texture class, subclasses of which depend on the render spec (DX, GL etc.)
41 */
42 class CBaseTexture
43 {
44
45 public:
46   CBaseTexture(unsigned int width = 0, unsigned int height = 0, unsigned int format = XB_FMT_A8R8G8B8);
47
48   virtual ~CBaseTexture();
49
50   /*! \brief Load a texture from a file
51    Loads a texture from a file, restricting in size if needed based on maxHeight and maxWidth.
52    Note that these are the ideal size to load at - the returned texture may be smaller or larger than these.
53    \param texturePath the path of the texture to load.
54    \param idealWidth the ideal width of the texture (defaults to 0, no ideal width).
55    \param idealHeight the ideal height of the texture (defaults to 0, no ideal height).
56    \param autoRotate whether the textures should be autorotated based on EXIF information (defaults to false).
57    \param strMimeType mimetype of the given texture if available (defaults to empty)
58    \return a CBaseTexture pointer to the created texture - NULL if the texture failed to load.
59    */
60   static CBaseTexture *LoadFromFile(const CStdString& texturePath, unsigned int idealWidth = 0, unsigned int idealHeight = 0,
61                                     bool autoRotate = false, bool requirePixels = false, const std::string& strMimeType = "");
62
63   /*! \brief Load a texture from a file in memory
64    Loads a texture from a file in memory, restricting in size if needed based on maxHeight and maxWidth.
65    Note that these are the ideal size to load at - the returned texture may be smaller or larger than these.
66    \param buffer the memory buffer holding the file.
67    \param bufferSize the size of buffer.
68    \param mimeType the mime type of the file in buffer.
69    \param idealWidth the ideal width of the texture (defaults to 0, no ideal width).
70    \param idealHeight the ideal height of the texture (defaults to 0, no ideal height).
71    \return a CBaseTexture pointer to the created texture - NULL if the texture failed to load.
72    */
73   static CBaseTexture *LoadFromFileInMemory(unsigned char* buffer, size_t bufferSize, const std::string& mimeType,
74                                             unsigned int idealWidth = 0, unsigned int idealHeight = 0);
75
76   bool LoadFromMemory(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, bool hasAlpha, unsigned char* pixels);
77   bool LoadPaletted(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, const COLOR *palette);
78
79   bool HasAlpha() const;
80
81   virtual void CreateTextureObject() = 0;
82   virtual void DestroyTextureObject() = 0;
83   virtual void LoadToGPU() = 0;
84   virtual void BindToUnit(unsigned int unit) = 0;
85
86   unsigned char* GetPixels() const { return m_pixels; }
87   unsigned int GetPitch() const { return GetPitch(m_textureWidth); }
88   unsigned int GetRows() const { return GetRows(m_textureHeight); }
89   unsigned int GetTextureWidth() const { return m_textureWidth; }
90   unsigned int GetTextureHeight() const { return m_textureHeight; }
91   unsigned int GetWidth() const { return m_imageWidth; }
92   unsigned int GetHeight() const { return m_imageHeight; }
93   /*! \brief return the original width of the image, before scaling/cropping */
94   unsigned int GetOriginalWidth() const { return m_originalWidth; }
95   /*! \brief return the original height of the image, before scaling/cropping */
96   unsigned int GetOriginalHeight() const { return m_originalHeight; }
97
98   int GetOrientation() const { return m_orientation; }
99   void SetOrientation(int orientation) { m_orientation = orientation; }
100
101   void Update(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, bool loadToGPU);
102   void Allocate(unsigned int width, unsigned int height, unsigned int format);
103   void ClampToEdge();
104
105   static unsigned int PadPow2(unsigned int x);
106   static bool SwapBlueRed(unsigned char *pixels, unsigned int height, unsigned int pitch, unsigned int elements = 4, unsigned int offset=0);
107
108 private:
109   // no copy constructor
110   CBaseTexture(const CBaseTexture &copy);
111
112 protected:
113   bool LoadFromFileInMem(unsigned char* buffer, size_t size, const std::string& mimeType,
114                          unsigned int maxWidth, unsigned int maxHeight);
115   bool LoadFromFileInternal(const CStdString& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool autoRotate, bool requirePixels, const std::string& strMimeType = "");
116   bool LoadIImage(IImage* pImage, unsigned char* buffer, unsigned int bufSize, unsigned int width, unsigned int height, bool autoRotate=false);
117   // helpers for computation of texture parameters for compressed textures
118   unsigned int GetPitch(unsigned int width) const;
119   unsigned int GetRows(unsigned int height) const;
120   unsigned int GetBlockSize() const;
121
122   unsigned int m_imageWidth;
123   unsigned int m_imageHeight;
124   unsigned int m_textureWidth;
125   unsigned int m_textureHeight;
126   unsigned int m_originalWidth;   ///< original image width before scaling or cropping
127   unsigned int m_originalHeight;  ///< original image height before scaling or cropping
128
129   unsigned char* m_pixels;
130   bool m_loadedToGPU;
131   unsigned int m_format;
132   int m_orientation;
133   bool m_hasAlpha;
134 };
135
136 #if defined(HAS_OMXPLAYER)
137 #include "TexturePi.h"
138 #define CTexture CPiTexture
139 #elif defined(HAS_GL) || defined(HAS_GLES)
140 #include "TextureGL.h"
141 #define CTexture CGLTexture
142 #elif defined(HAS_DX)
143 #include "TextureDX.h"
144 #define CTexture CDXTexture
145 #endif