CTexture.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. // TODO: ADD SUPPORT FOR .tex-json FILES AS THEY ALSO HAVE FLAGS LIKE THESE ONES
  93. // setup texture wrapping and filtering
  94. if (this->m_header->flags & TextureFlags::ClampUVs)
  95. {
  96. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  97. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  98. }
  99. else
  100. {
  101. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  102. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  103. }
  104. if (this->m_header->flags & TextureFlags::NoInterpolation)
  105. {
  106. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  107. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  108. }
  109. else
  110. {
  111. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  112. glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  113. }
  114. glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY, 8.0f);
  115. // TODO: USE THIS ONE
  116. // uint32_t blockSize = (internalFormat == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) ? 8 : 16;
  117. auto cur = (*imgCur).second.begin ();
  118. auto end = (*imgCur).second.end ();
  119. for (int32_t level = 0; cur != end; cur ++, level ++)
  120. {
  121. FIBITMAP* bitmap = nullptr;
  122. FIBITMAP* converted = nullptr;
  123. FIMEMORY* memory = nullptr;
  124. void* dataptr = (*cur)->uncompressedData;
  125. uint32_t width = (*cur)->width;
  126. uint32_t height = (*cur)->height;
  127. GLenum textureFormat = GL_RGBA;
  128. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  129. {
  130. memory = FreeImage_OpenMemory (reinterpret_cast <BYTE *> ((*cur)->uncompressedData), (*cur)->uncompressedSize);
  131. // load the image and setup pointers so they can be used
  132. bitmap = FreeImage_LoadFromMemory (this->m_header->freeImageFormat, memory);
  133. // flip the image vertically
  134. FreeImage_FlipVertical (bitmap);
  135. // convert to a 32bits bytearray
  136. converted = FreeImage_ConvertTo32Bits (bitmap);
  137. dataptr = FreeImage_GetBits (converted);
  138. width = FreeImage_GetWidth (converted);
  139. height = FreeImage_GetHeight (converted);
  140. textureFormat = GL_BGRA;
  141. }
  142. else
  143. {
  144. if (this->m_header->format == TextureFormat::RG88)
  145. textureFormat = GL_RG;
  146. else if (this->m_header->format == TextureFormat::R8)
  147. textureFormat = GL_RED;
  148. }
  149. switch (internalFormat)
  150. {
  151. case GL_RGBA8:
  152. case GL_RG8:
  153. case GL_R8:
  154. glTexImage2D (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. (*cur)->width, (*cur)->height, 0,
  166. (*cur)->uncompressedSize, dataptr
  167. );
  168. break;
  169. }
  170. // freeimage buffer won't be used anymore, so free memory
  171. if (this->m_header->freeImageFormat != FREE_IMAGE_FORMAT::FIF_UNKNOWN)
  172. {
  173. FreeImage_Unload (bitmap);
  174. FreeImage_Unload (converted);
  175. FreeImage_CloseMemory (memory);
  176. }
  177. }
  178. }
  179. }
  180. CTexture::~CTexture ()
  181. {
  182. if (this->getHeader () == nullptr)
  183. return;
  184. // free the header if it was allocated
  185. delete this->getHeader ();
  186. }
  187. const GLuint CTexture::getTextureID (uint32_t imageIndex) const
  188. {
  189. // ensure we do not go out of bounds
  190. if (imageIndex > this->m_header->imageCount)
  191. return this->m_textureID [0];
  192. return this->m_textureID [imageIndex];
  193. }
  194. const uint32_t CTexture::getTextureWidth (uint32_t imageIndex) const
  195. {
  196. if (imageIndex > this->m_header->imageCount)
  197. return this->getHeader ()->textureWidth;
  198. return (*this->m_header->images [imageIndex].begin ())->width;
  199. }
  200. const uint32_t CTexture::getTextureHeight (uint32_t imageIndex) const
  201. {
  202. if (imageIndex > this->m_header->imageCount)
  203. return this->getHeader ()->textureHeight;
  204. return (*this->m_header->images [imageIndex].begin ())->height;
  205. }
  206. const uint32_t CTexture::getRealWidth () const
  207. {
  208. return this->isAnimated () == true ? this->getHeader ()->gifWidth : this->getHeader ()->width;
  209. }
  210. const uint32_t CTexture::getRealHeight () const
  211. {
  212. return this->isAnimated () == true ? this->getHeader ()->gifHeight : this->getHeader ()->height;
  213. }
  214. const ITexture::TextureFormat CTexture::getFormat () const
  215. {
  216. return this->getHeader ()->format;
  217. }
  218. const ITexture::TextureFlags CTexture::getFlags () const
  219. {
  220. return this->getHeader ()->flags;
  221. }
  222. const CTexture::TextureHeader* CTexture::getHeader () const
  223. {
  224. return this->m_header;
  225. }
  226. const glm::vec4* CTexture::getResolution () const
  227. {
  228. return &this->m_resolution;
  229. }
  230. const std::vector<ITexture::TextureFrame*>& CTexture::getFrames () const
  231. {
  232. return this->getHeader ()->frames;
  233. }
  234. const bool CTexture::isAnimated () const
  235. {
  236. return this->getHeader ()->isAnimated ();
  237. }
  238. CTexture::TextureMipmap::TextureMipmap ()
  239. {
  240. }
  241. CTexture::TextureMipmap::~TextureMipmap ()
  242. {
  243. if (this->compression == 1)
  244. delete this->compressedData;
  245. delete this->uncompressedData;
  246. }
  247. void CTexture::TextureMipmap::decompressData ()
  248. {
  249. if (this->compression == 1)
  250. {
  251. this->uncompressedData = new char [this->uncompressedSize];
  252. int result = LZ4_decompress_safe (
  253. this->compressedData, this->uncompressedData,
  254. this->compressedSize, this->uncompressedSize
  255. );
  256. if (!result)
  257. throw std::runtime_error ("Cannot decompress texture data");
  258. }
  259. }
  260. CTexture::TextureFrame::TextureFrame ()
  261. {
  262. }
  263. CTexture::TextureFrame::~TextureFrame ()
  264. {
  265. }
  266. CTexture::TextureHeader::TextureHeader ()
  267. {
  268. }
  269. CTexture::TextureHeader::~TextureHeader ()
  270. {
  271. auto imgCur = this->images.begin ();
  272. auto imgEnd = this->images.end ();
  273. for (; imgCur != imgEnd; imgCur ++)
  274. {
  275. auto cur = (*imgCur).second.begin ();
  276. auto end = (*imgCur).second.end ();
  277. for (; cur != end; cur ++)
  278. delete *cur;
  279. }
  280. }
  281. CTexture::TextureHeader* CTexture::parseHeader (char* fileData)
  282. {
  283. // check the magic value on the header first
  284. if (memcmp (fileData, "TEXV0005", 9) != 0)
  285. throw std::runtime_error ("unexpected texture container type");
  286. // jump to the next value
  287. fileData += 9;
  288. // check the sub-magic value on the header
  289. if (memcmp (fileData, "TEXI0001", 9) != 0)
  290. throw std::runtime_error ("unexpected texture sub-container type");
  291. // jump through the string again
  292. fileData += 9;
  293. TextureHeader* header = new TextureHeader;
  294. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  295. header->format = static_cast <TextureFormat>(*pointer ++);
  296. header->flags = static_cast <TextureFlags> (*pointer ++);
  297. header->textureWidth = *pointer ++;
  298. header->textureHeight = *pointer ++;
  299. header->width = *pointer ++;
  300. header->height = *pointer ++;
  301. pointer ++; // ignore some more bytes
  302. // now we're going to parse some more data that is string
  303. // so get the current position back as string
  304. fileData = reinterpret_cast <char*> (pointer);
  305. // get the position of what comes after the texture data
  306. pointer = reinterpret_cast <uint32_t*> (fileData + 9);
  307. if (memcmp (fileData, "TEXB0003", 9) == 0)
  308. {
  309. header->containerVersion = ContainerVersion::TEXB0003;
  310. header->imageCount = *pointer ++;
  311. header->freeImageFormat = static_cast <FREE_IMAGE_FORMAT> (*pointer++);
  312. }
  313. else if(memcmp (fileData, "TEXB0002", 9) == 0)
  314. {
  315. header->containerVersion = ContainerVersion::TEXB0002;
  316. header->imageCount = *pointer ++;
  317. }
  318. else if (memcmp (fileData, "TEXB0001", 9) == 0)
  319. {
  320. header->containerVersion = ContainerVersion::TEXB0001;
  321. header->imageCount = *pointer ++;
  322. }
  323. else
  324. {
  325. delete header;
  326. throw std::runtime_error ("unknown texture format type");
  327. }
  328. for (uint32_t image = 0; image < header->imageCount; image ++)
  329. {
  330. // read the number of mipmaps available for this image
  331. header->mipmapCount = *pointer ++;
  332. std::vector <TextureMipmap*> mipmaps;
  333. fileData = reinterpret_cast <char*> (pointer);
  334. for (uint32_t i = 0; i < header->mipmapCount; i ++)
  335. mipmaps.emplace_back (parseMipmap (header, &fileData));
  336. // add the pixmaps back
  337. header->images.insert (std::pair <uint32_t, std::vector <TextureMipmap*>> (image, mipmaps));
  338. pointer = reinterpret_cast <uint32_t*> (fileData);
  339. }
  340. // gifs have extra information after the mipmaps
  341. if (header->isAnimated () == true)
  342. {
  343. if (memcmp (fileData, "TEXS0002", 9) == 0)
  344. {
  345. header->animatedVersion = AnimatedVersion::TEXS0002;
  346. }
  347. else if (memcmp (fileData, "TEXS0003", 9) == 0)
  348. {
  349. header->animatedVersion = AnimatedVersion::TEXS0003;
  350. }
  351. else
  352. {
  353. throw std::runtime_error ("found animation information of unknown type");
  354. }
  355. // get an integer pointer back to read the frame count
  356. pointer = reinterpret_cast <uint32_t*> (fileData + 9);
  357. uint32_t framecount = *pointer++;
  358. if (header->animatedVersion == AnimatedVersion::TEXS0003)
  359. {
  360. // ignore two extra integers as those are width and height of the git
  361. header->gifWidth = *pointer ++;
  362. header->gifHeight = *pointer ++;
  363. }
  364. // get back the pointer into filedata
  365. fileData = reinterpret_cast <char*> (pointer);
  366. while (framecount > 0)
  367. {
  368. // add the frame to the list
  369. header->frames.push_back (parseAnimation (header, &fileData));
  370. framecount --;
  371. }
  372. // ensure gif width and height is right for TEXS0002
  373. if (header->animatedVersion == AnimatedVersion::TEXS0002)
  374. {
  375. TextureFrame* first = *header->frames.begin ();
  376. header->gifWidth = first->width1;
  377. header->gifHeight = first->height1;
  378. }
  379. }
  380. return header;
  381. }
  382. CTexture::TextureFrame* CTexture::parseAnimation (TextureHeader* header, char** originalFileData)
  383. {
  384. char* fileData = *originalFileData;
  385. // get back the pointer into integer
  386. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  387. // start reading frame information
  388. TextureFrame* frame = new TextureFrame();
  389. frame->frameNumber = *pointer++;
  390. // reinterpret the pointer into float
  391. float* fPointer = reinterpret_cast <float*> (pointer);
  392. frame->frametime = *fPointer ++;
  393. frame->x = *fPointer ++;
  394. frame->y = *fPointer ++;
  395. frame->width1 = *fPointer ++;
  396. frame->width2 = *fPointer ++;
  397. frame->height2 = *fPointer ++;
  398. frame->height1 = *fPointer ++;
  399. // get back the pointer into fileData so it can be reused later
  400. *originalFileData = reinterpret_cast <char*> (fPointer);
  401. return frame;
  402. }
  403. CTexture::TextureMipmap* CTexture::parseMipmap (TextureHeader* header, char** originalFileData)
  404. {
  405. TextureMipmap* mipmap = new TextureMipmap ();
  406. // get the current position
  407. char* fileData = *originalFileData;
  408. // get an integer pointer
  409. uint32_t* pointer = reinterpret_cast <uint32_t*> (fileData);
  410. mipmap->width = *pointer++;
  411. mipmap->height = *pointer++;
  412. if (header->containerVersion == ContainerVersion::TEXB0002 ||
  413. header->containerVersion == ContainerVersion::TEXB0003)
  414. {
  415. mipmap->compression = *pointer++;
  416. mipmap->uncompressedSize = *pointer++;
  417. }
  418. mipmap->compressedSize = *pointer++;
  419. // get back a normal char pointer
  420. fileData = reinterpret_cast <char*> (pointer);
  421. if (mipmap->compression == 0)
  422. {
  423. // this might be better named as mipmap_bytes_size instead of compressedSize
  424. // as in uncompressed files this variable actually holds the file length
  425. mipmap->uncompressedSize = mipmap->compressedSize;
  426. }
  427. mipmap->uncompressedData = new char [mipmap->uncompressedSize];
  428. if (mipmap->compression == 1)
  429. {
  430. mipmap->compressedData = new char [mipmap->compressedSize];
  431. memcpy (mipmap->compressedData, fileData, mipmap->compressedSize);
  432. mipmap->decompressData ();
  433. // advance to the end of the mipmap
  434. fileData += mipmap->compressedSize;
  435. }
  436. else
  437. {
  438. memcpy (mipmap->uncompressedData, fileData, mipmap->uncompressedSize);
  439. // advance to the end of the mipmap
  440. fileData += mipmap->uncompressedSize;
  441. }
  442. // ensure the pointer is updated with the latest position when reading the data
  443. *originalFileData = fileData;
  444. return mipmap;
  445. }
  446. const bool CTexture::TextureHeader::isAnimated () const
  447. {
  448. return this->flags & TextureFlags::IsGif;
  449. }