CTexture.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #pragma once
  2. #include "ITexture.h"
  3. #include <GL/glew.h>
  4. #include <glm/vec4.hpp>
  5. #include <map>
  6. #include <stdexcept>
  7. #include <string>
  8. #include <vector>
  9. namespace WallpaperEngine::Assets {
  10. /**
  11. * A normal texture file in WallpaperEngine's format
  12. */
  13. class CTexture final : public ITexture {
  14. /**
  15. * Different texture container versions supported
  16. */
  17. enum ContainerVersion : int {
  18. UNKNOWN = -1,
  19. TEXB0003 = 3,
  20. TEXB0002 = 2,
  21. TEXB0001 = 1
  22. };
  23. /**
  24. * Different texture animation versions supported
  25. */
  26. enum AnimatedVersion : int {
  27. TEXSUNKN = -1,
  28. TEXS0002 = 0,
  29. TEXS0003 = 1,
  30. };
  31. enum FreeImageFormat : int {
  32. FIF_UNKNOWN = -1,
  33. FIF_BMP = 0,
  34. FIF_ICO = 1,
  35. FIF_JPEG = 2,
  36. FIF_JNG = 3,
  37. FIF_KOALA = 4,
  38. FIF_LBM = 5,
  39. FIF_IFF = FIF_LBM,
  40. FIF_MNG = 6,
  41. FIF_PBM = 7,
  42. FIF_PBMRAW = 8,
  43. FIF_PCD = 9,
  44. FIF_PCX = 10,
  45. FIF_PGM = 11,
  46. FIF_PGMRAW = 12,
  47. FIF_PNG = 13,
  48. FIF_PPM = 14,
  49. FIF_PPMRAW = 15,
  50. FIF_RAS = 16,
  51. FIF_TARGA = 17,
  52. FIF_TIFF = 18,
  53. FIF_WBMP = 19,
  54. FIF_PSD = 20,
  55. FIF_CUT = 21,
  56. FIF_XBM = 22,
  57. FIF_XPM = 23,
  58. FIF_DDS = 24,
  59. FIF_GIF = 25,
  60. FIF_HDR = 26,
  61. FIF_FAXG3 = 27,
  62. FIF_SGI = 28,
  63. FIF_EXR = 29,
  64. FIF_J2K = 30,
  65. FIF_JP2 = 31,
  66. FIF_PFM = 32,
  67. FIF_PICT = 33,
  68. FIF_RAW = 34,
  69. FIF_WEBP = 35,
  70. FIF_JXR = 36
  71. };
  72. /**
  73. * Texture mipmap data
  74. */
  75. class TextureMipmap {
  76. public:
  77. TextureMipmap ();
  78. ~TextureMipmap ();
  79. /** Width of the mipmap */
  80. uint32_t width = 0;
  81. /** Height of the mipmap */
  82. uint32_t height = 0;
  83. /** If the mipmap data is compressed */
  84. uint32_t compression = 0;
  85. /** Uncompressed size of the mipmap */
  86. uint32_t uncompressedSize = 0;
  87. /** Compress size of the mipmap */
  88. uint32_t compressedSize = 0;
  89. /** Pointer to the compressed data */
  90. char* compressedData = nullptr;
  91. /** Pointer to the uncompressed data */
  92. char* uncompressedData = nullptr;
  93. /**
  94. * Performs actual decompression of the compressed data
  95. */
  96. void decompressData ();
  97. };
  98. /**
  99. * Texture header data
  100. */
  101. class TextureHeader {
  102. public:
  103. TextureHeader ();
  104. ~TextureHeader ();
  105. [[nodiscard]] bool isAnimated () const;
  106. /** The version of the texture container */
  107. ContainerVersion containerVersion = ContainerVersion::UNKNOWN;
  108. /** The version of the animated data */
  109. AnimatedVersion animatedVersion = AnimatedVersion::TEXSUNKN;
  110. /** Flags with extra texture information */
  111. TextureFlags flags;
  112. /** Real width of the texture */
  113. uint32_t width;
  114. /** Real height of the texture */
  115. uint32_t height;
  116. /** Texture width in memory (power of 2) */
  117. uint32_t textureWidth;
  118. /** Texture height in memory (power of 2) */
  119. uint32_t textureHeight;
  120. /** Gif width */
  121. uint32_t gifWidth;
  122. /** Gif height */
  123. uint32_t gifHeight;
  124. /** Texture data format */
  125. TextureFormat format;
  126. /** Free Image format */
  127. FreeImageFormat freeImageFormat = FreeImageFormat::FIF_UNKNOWN;
  128. /** The amount of images in the texture file */
  129. uint32_t imageCount;
  130. /** Number of mipmap levels on the texture */
  131. uint32_t mipmapCount;
  132. /** List of mipmaps */
  133. std::map<uint32_t, std::vector<TextureMipmap*>> images;
  134. /** List of animation frames */
  135. std::vector<TextureFrame*> frames;
  136. };
  137. public:
  138. explicit CTexture (const void* fileData);
  139. ~CTexture () override;
  140. /** @inheritdoc */
  141. [[nodiscard]] const GLuint getTextureID (uint32_t imageIndex = 0) const override;
  142. /** @inheritdoc */
  143. [[nodiscard]] const uint32_t getTextureWidth (uint32_t imageIndex = 0) const override;
  144. /** @inheritdoc */
  145. [[nodiscard]] const uint32_t getTextureHeight (uint32_t imageIndex = 0) const override;
  146. /** @inheritdoc */
  147. [[nodiscard]] const uint32_t getRealWidth () const override;
  148. /** @inheritdoc */
  149. [[nodiscard]] const uint32_t getRealHeight () const override;
  150. /** @inheritdoc */
  151. [[nodiscard]] const TextureFormat getFormat () const override;
  152. /** @inheritdoc */
  153. [[nodiscard]] const TextureFlags getFlags () const override;
  154. /** @inheritdoc */
  155. [[nodiscard]] const glm::vec4* getResolution () const override;
  156. /** @inheritdoc */
  157. [[nodiscard]] const std::vector<TextureFrame*>& getFrames () const override;
  158. /** @inheritdoc */
  159. [[nodiscard]] const bool isAnimated () const override;
  160. private:
  161. /**
  162. * @return The texture header
  163. */
  164. [[nodiscard]] const TextureHeader* getHeader () const;
  165. /**
  166. * Tries to parse a header off the given data pointer
  167. *
  168. * @param fileData The point at which to start reading data off from
  169. * @return
  170. */
  171. static TextureHeader* parseHeader (const char* fileData);
  172. /**
  173. * Tries to parse an animation frame off the given data pointer
  174. *
  175. * @param originalFileData The point at which to start reading data off from
  176. * @return
  177. */
  178. static TextureFrame* parseAnimation (const char** originalFileData);
  179. /**
  180. * Tries to parse mipmap information off the given data pointer
  181. *
  182. * @param header The file header
  183. * @param fileData The point at which to start reading data off from
  184. * @return
  185. */
  186. static TextureMipmap* parseMipmap (const TextureHeader* header, const char** fileData);
  187. /** The texture header */
  188. TextureHeader* m_header;
  189. /** OpenGL's texture ID */
  190. GLuint* m_textureID;
  191. /** Resolution vector of the texture */
  192. glm::vec4 m_resolution;
  193. };
  194. } // namespace WallpaperEngine::Assets