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, unsigned int 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  unsigned int 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, unsigned int height, unsigned int pitch, unsigned int format, bool hasAlpha, const 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  void SetMipmapping();
82  bool IsMipmapped() const;
83  void SetScalingMethod(TEXTURE_SCALING scalingMethod) { m_scalingMethod = scalingMethod; }
84  TEXTURE_SCALING GetScalingMethod() const { return m_scalingMethod; }
85  void SetCacheMemory(bool bCacheMemory) { m_bCacheMemory = bCacheMemory; }
86  bool GetCacheMemory() const { return m_bCacheMemory; }
87 
88  virtual void CreateTextureObject() = 0;
89  virtual void DestroyTextureObject() = 0;
90  virtual void LoadToGPU() = 0;
91  virtual void BindToUnit(unsigned int unit) = 0;
92 
93  unsigned char* GetPixels() const { return m_pixels; }
94  unsigned int GetPitch() const { return GetPitch(m_textureWidth); }
95  unsigned int GetRows() const { return GetRows(m_textureHeight); }
96  unsigned int GetTextureWidth() const { return m_textureWidth; }
97  unsigned int GetTextureHeight() const { return m_textureHeight; }
98  unsigned int GetWidth() const { return m_imageWidth; }
99  unsigned int GetHeight() const { return m_imageHeight; }
101  unsigned int GetOriginalWidth() const { return m_originalWidth; }
103  unsigned int GetOriginalHeight() const { return m_originalHeight; }
104 
105  int GetOrientation() const { return m_orientation; }
106  void SetOrientation(int orientation) { m_orientation = orientation; }
107 
108  void Update(unsigned int width, unsigned int height, unsigned int pitch, unsigned int format, const unsigned char *pixels, bool loadToGPU);
109  void Allocate(unsigned int width, unsigned int height, unsigned int format);
110  void ClampToEdge();
111 
112  static unsigned int PadPow2(unsigned int x);
113  static bool SwapBlueRed(unsigned char *pixels, unsigned int height, unsigned int pitch, unsigned int elements = 4, unsigned int offset=0);
114 
115 private:
116  // no copy constructor
117  CTexture(const CTexture& copy) = delete;
118 
119 protected:
120  bool LoadFromFileInMem(unsigned char* buffer, size_t size, const std::string& mimeType,
121  unsigned int maxWidth, unsigned int maxHeight);
122  bool LoadFromFileInternal(const std::string& texturePath, unsigned int maxWidth, unsigned int maxHeight, bool requirePixels, const std::string& strMimeType = "");
123  bool LoadIImage(IImage* pImage, unsigned char* buffer, unsigned int bufSize, unsigned int width, unsigned int height);
124  // helpers for computation of texture parameters for compressed textures
125  unsigned int GetPitch(unsigned int width) const;
126  unsigned int GetRows(unsigned int height) const;
127  unsigned int GetBlockSize() const;
128 
129  unsigned int m_imageWidth;
130  unsigned int m_imageHeight;
131  unsigned int m_textureWidth;
132  unsigned int m_textureHeight;
133  unsigned int m_originalWidth;
134  unsigned int m_originalHeight;
135 
136  unsigned char* m_pixels;
137  bool m_loadedToGPU;
138  unsigned int m_format;
139  int m_orientation;
140  bool m_hasAlpha = true ;
141  bool m_mipmapping = false ;
142  TEXTURE_SCALING m_scalingMethod = TEXTURE_SCALING::LINEAR;
143  bool m_bCacheMemory = false;
144 };
unsigned int m_originalHeight
original image height before scaling or cropping
Definition: Texture.h:134
Definition: Texture.h:21
unsigned int GetOriginalWidth() const
return the original width of the image, before scaling/cropping
Definition: Texture.h:101
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:133
unsigned int GetOriginalHeight() const
return the original height of the image, before scaling/cropping
Definition: Texture.h:103