xbmc
Texture.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "guilib/TextureFormats.h"
12 
13 #include <cstddef>
14 #include <memory>
15 #include <string>
16 
17 class IImage;
18 
19 
20 #pragma pack(1)
21 struct COLOR {unsigned char b,g,r,x;}; // Windows GDI expects 4bytes per color
22 #pragma pack()
23 
24 enum class TEXTURE_SCALING
25 {
26  LINEAR,
27  NEAREST,
28 };
29 
34 class CTexture
35 {
36 
37 public:
38  CTexture(unsigned int width = 0, unsigned int height = 0, XB_FMT format = XB_FMT_A8R8G8B8);
39  virtual ~CTexture();
40 
41  static std::unique_ptr<CTexture> CreateTexture(unsigned int width = 0,
42  unsigned int height = 0,
43  XB_FMT format = XB_FMT_A8R8G8B8);
44 
54  static std::unique_ptr<CTexture> LoadFromFile(const std::string& texturePath,
55  unsigned int idealWidth = 0,
56  unsigned int idealHeight = 0,
57  bool requirePixels = false,
58  const std::string& strMimeType = "");
59 
70  static std::unique_ptr<CTexture> LoadFromFileInMemory(unsigned char* buffer,
71  size_t bufferSize,
72  const std::string& mimeType,
73  unsigned int idealWidth = 0,
74  unsigned int idealHeight = 0);
75 
76  bool LoadFromMemory(unsigned int width,
77  unsigned int height,
78  unsigned int pitch,
79  XB_FMT format,
80  bool hasAlpha,
81  const unsigned char* pixels);
82  bool LoadPaletted(unsigned int width,
83  unsigned int height,
84  unsigned int pitch,
85  XB_FMT format,
86  const unsigned char* pixels,
87  const COLOR* palette);
88 
89  bool HasAlpha() const;
90  void SetAlpha(bool hasAlpha) { m_hasAlpha = hasAlpha; }
91 
92  void SetMipmapping();
93  bool IsMipmapped() const;
94  void SetScalingMethod(TEXTURE_SCALING scalingMethod) { m_scalingMethod = scalingMethod; }
95  TEXTURE_SCALING GetScalingMethod() const { return m_scalingMethod; }
96  void SetCacheMemory(bool bCacheMemory) { m_bCacheMemory = bCacheMemory; }
97  bool GetCacheMemory() const { return m_bCacheMemory; }
98 
99  virtual void CreateTextureObject() = 0;
100  virtual void DestroyTextureObject() = 0;
101  virtual void LoadToGPU() = 0;
102  virtual void BindToUnit(unsigned int unit) = 0;
103 
104  unsigned char* GetPixels() const { return m_pixels; }
105  unsigned int GetPitch() const { return GetPitch(m_textureWidth); }
106  unsigned int GetRows() const { return GetRows(m_textureHeight); }
107  unsigned int GetTextureWidth() const { return m_textureWidth; }
108  unsigned int GetTextureHeight() const { return m_textureHeight; }
109  unsigned int GetWidth() const { return m_imageWidth; }
110  unsigned int GetHeight() const { return m_imageHeight; }
112  unsigned int GetOriginalWidth() const { return m_originalWidth; }
114  unsigned int GetOriginalHeight() const { return m_originalHeight; }
115 
116  int GetOrientation() const { return m_orientation; }
117  void SetOrientation(int orientation) { m_orientation = orientation; }
118 
119  void Update(unsigned int width,
120  unsigned int height,
121  unsigned int pitch,
122  XB_FMT format,
123  const unsigned char* pixels,
124  bool loadToGPU);
125  void Allocate(unsigned int width, unsigned int height, XB_FMT format);
126  void ClampToEdge();
127 
128  static unsigned int PadPow2(unsigned int x);
129  static bool SwapBlueRed(unsigned char *pixels, unsigned int height, unsigned int pitch, unsigned int elements = 4, unsigned int offset=0);
130 
131 private:
132  // no copy constructor
133  CTexture(const CTexture& copy) = delete;
134 
135 protected:
136  bool LoadFromFileInMem(unsigned char* buffer, size_t size, const std::string& mimeType,
137  unsigned int maxWidth, unsigned int maxHeight);
138  bool LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool requirePixels, const std::string& strMimeType = "");
139  bool LoadIImage(IImage* pImage, unsigned char* buffer, unsigned int bufSize, unsigned int width, unsigned int height);
140  // helpers for computation of texture parameters for compressed textures
141  unsigned int GetPitch(unsigned int width) const;
142  unsigned int GetRows(unsigned int height) const;
143  unsigned int GetBlockSize() const;
144 
145  unsigned int m_imageWidth;
146  unsigned int m_imageHeight;
147  unsigned int m_textureWidth;
148  unsigned int m_textureHeight;
149  unsigned int m_originalWidth;
150  unsigned int m_originalHeight;
151 
152  unsigned char* m_pixels;
153  bool m_loadedToGPU;
154  XB_FMT m_format;
155  int m_orientation;
156  bool m_hasAlpha = true ;
157  bool m_mipmapping = false ;
158  TEXTURE_SCALING m_scalingMethod = TEXTURE_SCALING::LINEAR;
159  bool m_bCacheMemory = false;
160 };
unsigned int m_originalHeight
original image height before scaling or cropping
Definition: Texture.h:150
Definition: Texture.h:21
unsigned int GetOriginalWidth() const
return the original width of the image, before scaling/cropping
Definition: Texture.h:112
Definition: iimage.h:13
Base texture class, subclasses of which depend on the render spec (DX, GL etc.)
Definition: Texture.h:34
unsigned int m_originalWidth
original image width before scaling or cropping
Definition: Texture.h:149
unsigned int GetOriginalHeight() const
return the original height of the image, before scaling/cropping
Definition: Texture.h:114