CTexture.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "CTexture.h"
  2. #include <string>
  3. #include <cstring>
  4. #include <lz4.h>
  5. using namespace WallpaperEngine::Assets;
  6. CTexture::CTexture (void* fileData)
  7. {
  8. // ensure the header is parsed
  9. this->m_header = this->parseHeader (static_cast <char*> (fileData));
  10. GLint internalFormat;
  11. if (this->isAnimated () == true)
  12. {
  13. this->m_resolution = {
  14. this->m_header->textureWidth, this->m_header->textureHeight,
  15. this->m_header->gifWidth, this->m_header->gifHeight
  16. };
  17. }
  18. else
  19. {
  20. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  21. {
  22. // wpengine-texture format always has one mipmap
  23. // get first image size
  24. std::vector<TextureMipmap*>::const_iterator element = this->m_header->images.find (0)->second.begin ();
  25. // set the texture resolution
  26. this->m_resolution = {
  27. (*element)->width, (*element)->height,
  28. this->m_header->width, this->m_header->height
  29. };
  30. }
  31. else
  32. {
  33. // set the texture resolution
  34. this->m_resolution = {
  35. this->m_header->textureWidth, this->m_header->textureHeight,
  36. this->m_header->width, this->m_header->height
  37. };
  38. }
  39. }
  40. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  41. {
  42. internalFormat = GL_RGBA8;
  43. // set some extra information too as it's used for image sizing
  44. // this ensures that a_TexCoord uses the full image instead of just part of it
  45. // TODO: MAYBE IT'S BETTER TO CREATE A TEXTURE OF THE GIVEN SIZE AND COPY OVER WHAT WE READ FROM THE FILE?
  46. /*this->m_header->width = this->m_header->mipmaps [0]->width;
  47. this->m_header->height = this->m_header->mipmaps [0]->height;
  48. this->m_header->textureWidth = this->m_header->mipmaps [0]->width;
  49. this->m_header->textureHeight = this->m_header->mipmaps [0]->height;*/
  50. }
  51. else
  52. {
  53. // detect the image format and hand it to openGL to be used
  54. switch (this->m_header->format)
  55. {
  56. case TextureFormat::DXT5:
  57. internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
  58. break;
  59. case TextureFormat::DXT3:
  60. internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
  61. break;
  62. case TextureFormat::DXT1:
  63. internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
  64. break;
  65. case TextureFormat::ARGB8888:
  66. internalFormat = GL_RGBA8;
  67. break;
  68. case TextureFormat::R8:
  69. internalFormat = GL_R8;
  70. break;
  71. case TextureFormat::RG88:
  72. internalFormat = GL_RG8;
  73. break;
  74. default:
  75. delete this->m_header;
  76. throw std::runtime_error ("Cannot determine the texture format");
  77. }
  78. }
  79. // allocate texture ids list
  80. this->m_textureID = new GLuint [this->m_header->imageCount];
  81. // ask opengl for the correct amount of textures
  82. glGenTextures (this->m_header->imageCount, this->m_textureID);
  83. auto imgCur = this->m_header->images.begin ();
  84. auto imgEnd = this->m_header->images.end ();
  85. for (int index = 0; imgCur != imgEnd; imgCur ++, index ++)
  86. {
  87. // bind the texture to assign information to it
  88. glBindTexture (GL_TEXTURE_2D, this->m_textureID [index]);
  89. // set mipmap levels
  90. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
  91. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, this->m_header->mipmapCount - 1);
  92. // setup texture wrapping and filtering
  93. if (this->m_header->flags & TextureFlags::ClampUVs)
  94. {
  95. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  96. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  97. }
  98. else
  99. {
  100. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  101. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  102. }
  103. if (this->m_header->flags & TextureFlags::NoInterpolation)
  104. {
  105. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  106. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  107. }
  108. else
  109. {
  110. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  111. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  112. }
  113. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 8.0f);
  114. auto cur = (*imgCur).second.begin ();
  115. auto end = (*imgCur).second.end ();
  116. for (int32_t level = 0; cur != end; cur ++, level ++)
  117. {
  118. FIBITMAP* bitmap = nullptr;
  119. FIBITMAP* converted = nullptr;
  120. FIMEMORY* memory = nullptr;
  121. void* dataptr = (*cur)->uncompressedData;
  122. uint32_t width = (*cur)->width;
  123. uint32_t height = (*cur)->height;
  124. uint32_t bufferSize = (*cur)->uncompressedSize;
  125. GLenum textureFormat = GL_RGBA;
  126. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  127. {
  128. memory = FreeImage_OpenMemory (reinterpret_cast <BYTE *> ((*cur)->uncompressedData), (*cur)->uncompressedSize);
  129. // load the image and setup pointers so they can be used
  130. bitmap = FreeImage_LoadFromMemory (this->m_header->freeImageFormat, memory);
  131. // flip the image vertically
  132. FreeImage_FlipVertical (bitmap);
  133. // convert to a 32bits bytearray
  134. converted = FreeImage_ConvertTo32Bits (bitmap);
  135. dataptr = FreeImage_GetBits (converted);
  136. width = FreeImage_GetWidth (converted);
  137. height = FreeImage_GetHeight (converted);
  138. bufferSize = FreeImage_GetMemorySize (converted);
  139. textureFormat = GL_BGRA;
  140. }
  141. else
  142. {
  143. if (this->m_header->format == TextureFormat::RG88)
  144. textureFormat = GL_RG;
  145. else if (this->m_header->format == TextureFormat::R8)
  146. textureFormat = GL_RED;
  147. }
  148. switch (internalFormat)
  149. {
  150. case GL_RGBA8:
  151. case GL_RG8:
  152. case GL_R8:
  153. glTexImage2D (
  154. GL_TEXTURE_2D, level, internalFormat,
  155. width, height, 0,
  156. textureFormat, GL_UNSIGNED_BYTE,
  157. dataptr
  158. );
  159. break;
  160. case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  161. case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  162. case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  163. glCompressedTexImage2D (
  164. GL_TEXTURE_2D, level, internalFormat,
  165. width, height, 0,
  166. bufferSize, dataptr
  167. );
  168. break;
  169. default:
  170. throw std::runtime_error ("Cannot load texture, unknown format");
  171. }
  172. // freeimage buffer won't be used anymore, so free memory
  173. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  174. {
  175. FreeImage_Unload (bitmap);
  176. FreeImage_Unload (converted);
  177. FreeImage_CloseMemory (memory);
  178. }
  179. }
  180. }
  181. }
  182. CTexture::~CTexture ()
  183. {
  184. if (this->getHeader () == nullptr)
  185. return;
  186. // free the header if it was allocated
  187. delete this->getHeader ();
  188. }
  189. const GLuint CTexture::getTextureID (uint32_t imageIndex) const
  190. {
  191. // ensure we do not go out of bounds
  192. if (imageIndex > this->m_header->imageCount)
  193. return this->m_textureID [0];
  194. return this->m_textureID [imageIndex];
  195. }
  196. const uint32_t CTexture::getTextureWidth (uint32_t imageIndex) const
  197. {
  198. if (imageIndex > this->m_header->imageCount)
  199. return this->getHeader ()->textureWidth;
  200. return (*this->m_header->images [imageIndex].begin ())->width;
  201. }
  202. const uint32_t CTexture::getTextureHeight (uint32_t imageIndex) const
  203. {
  204. if (imageIndex > this->m_header->imageCount)
  205. return this->getHeader ()->textureHeight;
  206. return (*this->m_header->images [imageIndex].begin ())->height;
  207. }
  208. const uint32_t CTexture::getRealWidth () const
  209. {
  210. return this->isAnimated () == true ? this->getHeader ()->gifWidth : this->getHeader ()->width;
  211. }
  212. const uint32_t CTexture::getRealHeight () const
  213. {
  214. return this->isAnimated () == true ? this->getHeader ()->gifHeight : this->getHeader ()->height;
  215. }
  216. const ITexture::TextureFormat CTexture::getFormat () const
  217. {
  218. return this->getHeader ()->format;
  219. }
  220. const ITexture::TextureFlags CTexture::getFlags () const
  221. {
  222. return this->getHeader ()->flags;
  223. }
  224. const CTexture::TextureHeader* CTexture::getHeader () const
  225. {
  226. return this->m_header;
  227. }
  228. const glm::vec4* CTexture::getResolution () const
  229. {
  230. return &this->m_resolution;
  231. }
  232. const std::vector<ITexture::TextureFrame*>& CTexture::getFrames () const
  233. {
  234. return this->getHeader ()->frames;
  235. }
  236. const bool CTexture::isAnimated () const
  237. {
  238. return this->getHeader ()->isAnimated ();
  239. }
  240. CTexture::TextureMipmap::TextureMipmap ()
  241. {
  242. }
  243. CTexture::TextureMipmap::~TextureMipmap ()
  244. {
  245. if (this->compression == 1)
  246. delete this->compressedData;
  247. delete this->uncompressedData;
  248. }
  249. void CTexture::TextureMipmap::decompressData ()
  250. {
  251. if (this->compression == 1)
  252. {
  253. this->uncompressedData = new char [this->uncompressedSize];
  254. int result = LZ4_decompress_safe (
  255. this->compressedData, this->uncompressedData,
  256. this->compressedSize, this->uncompressedSize
  257. );
  258. if (!result)
  259. throw std::runtime_error ("Cannot decompress texture data");
  260. }
  261. }
  262. CTexture::TextureFrame::TextureFrame ()
  263. {
  264. }
  265. CTexture::TextureFrame::~TextureFrame ()
  266. {
  267. }
  268. CTexture::TextureHeader::TextureHeader ()
  269. {
  270. }
  271. CTexture::TextureHeader::~TextureHeader ()
  272. {
  273. auto imgCur = this->images.begin ();
  274. auto imgEnd = this->images.end ();
  275. for (; imgCur != imgEnd; imgCur ++)
  276. {
  277. auto cur = (*imgCur).second.begin ();
  278. auto end = (*imgCur).second.end ();
  279. for (; cur != end; cur ++)
  280. delete *cur;
  281. }
  282. }
  283. CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
  284. {
  285. // check the magic value on the header first
  286. if (memcmp (fileData, "TEXV0005", 9) != 0)
  287. throw std::runtime_error ("unexpected texture container type");
  288. // jump to the next value
  289. fileData += 9;
  290. // check the sub-magic value on the header
  291. if (memcmp (fileData, "TEXI0001", 9) != 0)
  292. throw std::runtime_error ("unexpected texture sub-container type");
  293. // jump through the string again
  294. fileData += 9;
  295. TextureHeader* header = new TextureHeader;
  296. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  297. header->format = static_cast <TextureFormat>(*pointer ++);
  298. header->flags = static_cast <TextureFlags> (*pointer ++);
  299. header->textureWidth = *pointer ++;
  300. header->textureHeight = *pointer ++;
  301. header->width = *pointer ++;
  302. header->height = *pointer ++;
  303. pointer ++; // ignore some more bytes
  304. // now we're going to parse some more data that is string
  305. // so get the current position back as string
  306. fileData = reinterpret_cast <char*> (pointer);
  307. // get the position of what comes after the texture data
  308. pointer = reinterpret_cast <uint32_t*> (fileData + 9);
  309. if (memcmp (fileData, "TEXB0003", 9) == 0)
  310. {
  311. header->containerVersion = ContainerVersion::TEXB0003;
  312. header->imageCount = *pointer ++;
  313. header->freeImageFormat = static_cast <FREE_IMAGE_FORMAT> (*pointer++);
  314. }
  315. else if(memcmp (fileData, "TEXB0002", 9) == 0)
  316. {
  317. header->containerVersion = ContainerVersion::TEXB0002;
  318. header->imageCount = *pointer ++;
  319. }
  320. else if (memcmp (fileData, "TEXB0001", 9) == 0)
  321. {
  322. header->containerVersion = ContainerVersion::TEXB0001;
  323. header->imageCount = *pointer ++;
  324. }
  325. else
  326. {
  327. delete header;
  328. throw std::runtime_error ("unknown texture format type");
  329. }
  330. for (uint32_t image = 0; image < header->imageCount; image ++)
  331. {
  332. // read the number of mipmaps available for this image
  333. header->mipmapCount = *pointer ++;
  334. std::vector <TextureMipmap*> mipmaps;
  335. fileData = reinterpret_cast <char*> (pointer);
  336. for (uint32_t i = 0; i < header->mipmapCount; i ++)
  337. mipmaps.emplace_back (parseMipmap (header, &fileData));
  338. // add the pixmaps back
  339. header->images.insert (std::pair <uint32_t, std::vector <TextureMipmap*>> (image, mipmaps));
  340. pointer = reinterpret_cast <uint32_t*> (fileData);
  341. }
  342. // gifs have extra information after the mipmaps
  343. if (header->isAnimated () == true)
  344. {
  345. if (memcmp (fileData, "TEXS0002", 9) == 0)
  346. {
  347. header->animatedVersion = AnimatedVersion::TEXS0002;
  348. }
  349. else if (memcmp (fileData, "TEXS0003", 9) == 0)
  350. {
  351. header->animatedVersion = AnimatedVersion::TEXS0003;
  352. }
  353. else
  354. {
  355. throw std::runtime_error ("found animation information of unknown type");
  356. }
  357. // get an integer pointer back to read the frame count
  358. pointer = reinterpret_cast <uint32_t*> (fileData + 9);
  359. uint32_t framecount = *pointer++;
  360. if (header->animatedVersion == AnimatedVersion::TEXS0003)
  361. {
  362. // ignore two extra integers as those are width and height of the git
  363. header->gifWidth = *pointer ++;
  364. header->gifHeight = *pointer ++;
  365. }
  366. // get back the pointer into filedata
  367. fileData = reinterpret_cast <char*> (pointer);
  368. while (framecount > 0)
  369. {
  370. // add the frame to the list
  371. header->frames.push_back (parseAnimation (header, &fileData));
  372. framecount --;
  373. }
  374. // ensure gif width and height is right for TEXS0002
  375. if (header->animatedVersion == AnimatedVersion::TEXS0002)
  376. {
  377. TextureFrame* first = *header->frames.begin ();
  378. header->gifWidth = first->width1;
  379. header->gifHeight = first->height1;
  380. }
  381. }
  382. return header;
  383. }
  384. CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, char** originalFileData)
  385. {
  386. char* fileData = *originalFileData;
  387. // get back the pointer into integer
  388. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  389. // start reading frame information
  390. TextureFrame* frame = new TextureFrame();
  391. frame->frameNumber = *pointer++;
  392. // reinterpret the pointer into float
  393. float* fPointer = reinterpret_cast <float*> (pointer);
  394. frame->frametime = *fPointer ++;
  395. frame->x = *fPointer ++;
  396. frame->y = *fPointer ++;
  397. frame->width1 = *fPointer ++;
  398. frame->width2 = *fPointer ++;
  399. frame->height2 = *fPointer ++;
  400. frame->height1 = *fPointer ++;
  401. // get back the pointer into fileData so it can be reused later
  402. *originalFileData = reinterpret_cast <char*> (fPointer);
  403. return frame;
  404. }
  405. CTexture::TextureMipmap* CTexture::parseMipmap (TextureHeader* header, char** originalFileData)
  406. {
  407. TextureMipmap* mipmap = new TextureMipmap ();
  408. // get the current position
  409. char* fileData = *originalFileData;
  410. // get an integer pointer
  411. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  412. mipmap->width = *pointer++;
  413. mipmap->height = *pointer++;
  414. if (header->containerVersion == ContainerVersion::TEXB0002 ||
  415. header->containerVersion == ContainerVersion::TEXB0003)
  416. {
  417. mipmap->compression = *pointer++;
  418. mipmap->uncompressedSize = *pointer++;
  419. }
  420. mipmap->compressedSize = *pointer++;
  421. // get back a normal char pointer
  422. fileData = reinterpret_cast <char*> (pointer);
  423. if (mipmap->compression == 0)
  424. {
  425. // this might be better named as mipmap_bytes_size instead of compressedSize
  426. // as in uncompressed files this variable actually holds the file length
  427. mipmap->uncompressedSize = mipmap->compressedSize;
  428. }
  429. mipmap->uncompressedData = new char [mipmap->uncompressedSize];
  430. if (mipmap->compression == 1)
  431. {
  432. mipmap->compressedData = new char [mipmap->compressedSize];
  433. memcpy (mipmap->compressedData, fileData, mipmap->compressedSize);
  434. mipmap->decompressData ();
  435. // advance to the end of the mipmap
  436. fileData += mipmap->compressedSize;
  437. }
  438. else
  439. {
  440. memcpy (mipmap->uncompressedData, fileData, mipmap->uncompressedSize);
  441. // advance to the end of the mipmap
  442. fileData += mipmap->uncompressedSize;
  443. }
  444. // ensure the pointer is updated with the latest position when reading the data
  445. *originalFileData = fileData;
  446. return mipmap;
  447. }
  448. const bool CTexture::TextureHeader::isAnimated () const
  449. {
  450. return this->flags & TextureFlags::IsGif;
  451. }