TextureParser.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <cmath>
  2. #include <cstring>
  3. #include <lz4.h>
  4. #include <nlohmann/json.hpp>
  5. #include "TextureParser.h"
  6. #include "WallpaperEngine/Data/Assets/Texture.h"
  7. #include "WallpaperEngine/Logging/Log.h"
  8. using namespace WallpaperEngine::Data::Assets;
  9. using namespace WallpaperEngine::Data::Parsers;
  10. TextureUniquePtr TextureParser::parse (const BinaryReader& file) {
  11. auto result = std::make_unique<Texture> ();
  12. // wallpaper64.exe sub_14015E580: TEXV0004 is a version 0 TEXI header followed by a version 0 TEXB body (one
  13. // image, mips like TEXB0001) with no section tags and no TEXS
  14. const bool legacy = parseTextureHeader (*result, file);
  15. if (legacy) {
  16. result->containerVersion = ContainerVersion_TEXB0001;
  17. result->imageCount = 1;
  18. } else {
  19. parseContainer (*result, file);
  20. }
  21. for (uint32_t image = 0; image < result->imageCount; image++) {
  22. const uint32_t mipmapCount = file.nextUInt32 ();
  23. MipmapList mipmaps;
  24. for (uint32_t mipmap = 0; mipmap < mipmapCount; mipmap++) {
  25. mipmaps.emplace_back (parseMipmap (file, *result));
  26. if (!result->conditions.empty ()) {
  27. parseMipmapPatches (*mipmaps.back (), file);
  28. }
  29. }
  30. result->images.emplace (image, mipmaps);
  31. }
  32. if (legacy || !result->isAnimated ()) {
  33. return result;
  34. }
  35. parseAnimations (*result, file);
  36. return result;
  37. }
  38. MipmapSharedPtr TextureParser::parseMipmap (const BinaryReader& file, const Texture& header) {
  39. auto result = std::make_shared<Mipmap> ();
  40. result->width = file.nextUInt32 ();
  41. result->height = file.nextUInt32 ();
  42. // wallpaper64.exe sub_14015C8D0, volume textures (image filter LUTs)
  43. if (header.flags & TextureFlags_Volume) {
  44. result->depth = file.nextUInt32 ();
  45. }
  46. if (header.containerVersion == ContainerVersion_TEXB0003 || header.containerVersion == ContainerVersion_TEXB0002) {
  47. result->compression = file.nextUInt32 ();
  48. result->uncompressedSize = file.nextInt ();
  49. }
  50. result->compressedSize = file.nextInt ();
  51. if (result->compression == 0) {
  52. // misnamed: in uncompressed files compressedSize actually holds the file length
  53. result->uncompressedSize = result->compressedSize;
  54. }
  55. result->uncompressedData = std::unique_ptr<char[]> (new char[result->uncompressedSize]);
  56. if (result->compression == 1) {
  57. result->compressedData = std::unique_ptr<char[]> (new char[result->compressedSize]);
  58. file.next (result->compressedData.get (), result->compressedSize);
  59. int bytes = LZ4_decompress_safe (
  60. result->compressedData.get (), result->uncompressedData.get (), result->compressedSize,
  61. result->uncompressedSize
  62. );
  63. if (bytes < 0) {
  64. sLog.exception ("Cannot decompress texture data, LZ4_decompress_safe returned an error");
  65. }
  66. } else {
  67. file.next (result->uncompressedData.get (), result->uncompressedSize);
  68. }
  69. return result;
  70. }
  71. void TextureParser::parseMipmapPatches (Mipmap& mipmap, const BinaryReader& file) {
  72. const uint32_t groupCount = file.nextUInt32 ();
  73. for (uint32_t group = 0; group < groupCount; group++) {
  74. const uint32_t patchCount = file.nextUInt32 ();
  75. for (uint32_t index = 0; index < patchCount; index++) {
  76. MipmapPatch patch;
  77. std::ignore = file.nextUInt32 ();
  78. patch.key = file.nextUInt32 ();
  79. patch.x = file.nextUInt32 ();
  80. patch.y = file.nextUInt32 ();
  81. patch.width = file.nextUInt32 ();
  82. patch.height = file.nextUInt32 ();
  83. patch.format = file.nextInt ();
  84. patch.data.resize (file.nextUInt32 ());
  85. file.next (patch.data.data (), patch.data.size ());
  86. mipmap.patches.push_back (std::move (patch));
  87. }
  88. }
  89. }
  90. FrameSharedPtr TextureParser::parseFrameV1 (const BinaryReader& file) {
  91. auto result = std::make_shared<Frame> ();
  92. result->frameNumber = file.nextUInt32 ();
  93. result->frametime = file.nextFloat ();
  94. result->x = static_cast<float> (file.nextUInt32 ());
  95. result->y = static_cast<float> (file.nextUInt32 ());
  96. result->width1 = static_cast<float> (file.nextUInt32 ());
  97. result->width2 = static_cast<float> (file.nextUInt32 ());
  98. result->height2 = static_cast<float> (file.nextUInt32 ());
  99. result->height1 = static_cast<float> (file.nextUInt32 ());
  100. return result;
  101. }
  102. FrameSharedPtr TextureParser::parseFrame (const BinaryReader& file) {
  103. auto result = std::make_shared<Frame> ();
  104. result->frameNumber = file.nextUInt32 ();
  105. result->frametime = file.nextFloat ();
  106. result->x = file.nextFloat ();
  107. result->y = file.nextFloat ();
  108. result->width1 = file.nextFloat ();
  109. result->width2 = file.nextFloat ();
  110. result->height2 = file.nextFloat ();
  111. result->height1 = file.nextFloat ();
  112. return result;
  113. }
  114. TextureMap TextureParser::parseTextureMap (const JSON& it) {
  115. if (!it.is_array ()) {
  116. return {};
  117. }
  118. TextureMap result = {};
  119. int textureIndex = -1;
  120. for (const auto& cur : it) {
  121. textureIndex++;
  122. if (cur.is_null ()) {
  123. continue;
  124. }
  125. if (cur.is_object ()) {
  126. const auto nameIt = cur.find ("name");
  127. if (nameIt != cur.end () && nameIt->is_string ()) {
  128. result.emplace (textureIndex, nameIt->get<std::string> ());
  129. }
  130. } else if (cur.is_string ()) {
  131. std::string texName = cur;
  132. if (!texName.empty ()) {
  133. result.emplace (textureIndex, texName);
  134. }
  135. }
  136. }
  137. return result;
  138. }
  139. TextureFormat TextureParser::parseTextureFormat (uint32_t value) {
  140. switch (value) {
  141. case TextureFormat_UNKNOWN:
  142. case TextureFormat_ARGB8888:
  143. case TextureFormat_RGB888:
  144. case TextureFormat_RGB565:
  145. case TextureFormat_DXT5:
  146. case TextureFormat_DXT3:
  147. case TextureFormat_DXT1:
  148. case TextureFormat_RG88:
  149. case TextureFormat_R8:
  150. case TextureFormat_RG1616f:
  151. case TextureFormat_R16f:
  152. case TextureFormat_BC7:
  153. case TextureFormat_RGBa1010102:
  154. case TextureFormat_RGBA16161616f:
  155. case TextureFormat_RGB161616f:
  156. return static_cast<TextureFormat> (value);
  157. default:
  158. sLog.exception ("unknown texture format: ", value);
  159. }
  160. }
  161. bool TextureParser::parseTextureHeader (Texture& header, const BinaryReader& file) {
  162. char magic[9] = { 0 };
  163. file.next (magic, 9);
  164. const bool legacy = strncmp (magic, "TEXV0004", 9) == 0;
  165. if (!legacy && strncmp (magic, "TEXV0005", 9) != 0) {
  166. sLog.exception ("unexpected texture container type: ", std::string_view (magic, 9));
  167. }
  168. if (!legacy) {
  169. file.next (magic, 9);
  170. if (strncmp (magic, "TEXI0001", 9) != 0) {
  171. sLog.exception ("unexpected texture sub-container type: ", std::string_view (magic, 9));
  172. }
  173. }
  174. header.format = parseTextureFormat (file.nextUInt32 ());
  175. header.flags = parseTextureFlags (file.nextUInt32 ());
  176. header.textureWidth = file.nextUInt32 ();
  177. header.textureHeight = file.nextUInt32 ();
  178. header.width = file.nextUInt32 ();
  179. header.height = file.nextUInt32 ();
  180. // wallpaper64.exe sub_14015C760
  181. if (header.flags & TextureFlags_Volume) {
  182. header.depth = file.nextUInt32 ();
  183. }
  184. // only TEXI version 1 and up has this field
  185. if (!legacy) {
  186. std::ignore = file.nextUInt32 ();
  187. }
  188. return legacy;
  189. }
  190. void TextureParser::parseContainer (Texture& header, const BinaryReader& file) {
  191. char magic[9] = { 0 };
  192. file.next (magic, 9);
  193. header.imageCount = file.nextUInt32 ();
  194. if (strncmp (magic, "TEXB0004", 9) == 0) {
  195. // sub_14015C8D0 (2.8.42): FIF, property conditions, then TEXB0003 mipmaps. Videos are detected by the texture
  196. // flags, not this header
  197. header.containerVersion = ContainerVersion_TEXB0003;
  198. header.freeImageFormat = parseFIF (file.nextUInt32 ());
  199. const uint32_t conditionCount = file.nextUInt32 ();
  200. for (uint32_t index = 0; index < conditionCount; index++) {
  201. TextureCondition condition;
  202. condition.group = file.nextUInt32 ();
  203. condition.key = file.nextUInt32 ();
  204. condition.flags = file.nextUInt32 ();
  205. condition.json = file.nextNullTerminatedString ();
  206. header.conditions.push_back (std::move (condition));
  207. }
  208. } else if (strncmp (magic, "TEXB0003", 9) == 0) {
  209. header.containerVersion = ContainerVersion_TEXB0003;
  210. header.freeImageFormat = parseFIF (file.nextUInt32 ());
  211. } else if (strncmp (magic, "TEXB0002", 9) == 0) {
  212. header.containerVersion = ContainerVersion_TEXB0002;
  213. } else if (strncmp (magic, "TEXB0001", 9) == 0) {
  214. header.containerVersion = ContainerVersion_TEXB0001;
  215. } else {
  216. sLog.exception ("unknown texture format type: ", std::string_view (magic, 9));
  217. }
  218. }
  219. void TextureParser::parseAnimations (Texture& header, const BinaryReader& file) {
  220. char magic[9] = { 0 };
  221. file.next (magic, 9);
  222. if (strncmp (magic, "TEXS0001", 9) == 0) {
  223. header.animatedVersion = AnimatedVersion_TEXS0001;
  224. } else if (strncmp (magic, "TEXS0002", 9) == 0) {
  225. header.animatedVersion = AnimatedVersion_TEXS0002;
  226. } else if (strncmp (magic, "TEXS0003", 9) == 0) {
  227. header.animatedVersion = AnimatedVersion_TEXS0003;
  228. } else {
  229. sLog.exception ("found animation information of unknown type: ", std::string_view (magic, 9));
  230. }
  231. uint32_t frameCount = file.nextUInt32 ();
  232. if (header.animatedVersion == AnimatedVersion_TEXS0003) {
  233. header.gifWidth = file.nextUInt32 ();
  234. header.gifHeight = file.nextUInt32 ();
  235. }
  236. while (frameCount-- > 0) {
  237. if (header.animatedVersion == AnimatedVersion_TEXS0001) {
  238. header.frames.push_back (parseFrameV1 (file));
  239. } else {
  240. header.frames.push_back (parseFrame (file));
  241. }
  242. }
  243. // wallpaper64.exe 2.8.42 sub_14015E1D0: before TEXS0003 the frame size is the TEXI image size
  244. if (header.animatedVersion == AnimatedVersion_TEXS0001 || header.animatedVersion == AnimatedVersion_TEXS0002) {
  245. header.gifWidth = header.width;
  246. header.gifHeight = header.height;
  247. }
  248. }
  249. uint32_t TextureParser::parseTextureFlags (uint32_t value) { return value & TextureFlags_All; }
  250. FIF TextureParser::parseFIF (uint32_t value) {
  251. switch (value) {
  252. case FIF_UNKNOWN:
  253. case FIF_BMP:
  254. case FIF_ICO:
  255. case FIF_JPEG:
  256. case FIF_JNG:
  257. case FIF_KOALA:
  258. case FIF_LBM:
  259. case FIF_MNG:
  260. case FIF_PBM:
  261. case FIF_PBMRAW:
  262. case FIF_PCD:
  263. case FIF_PCX:
  264. case FIF_PGM:
  265. case FIF_PGMRAW:
  266. case FIF_PNG:
  267. case FIF_PPM:
  268. case FIF_PPMRAW:
  269. case FIF_RAS:
  270. case FIF_TARGA:
  271. case FIF_TIFF:
  272. case FIF_WBMP:
  273. case FIF_PSD:
  274. case FIF_CUT:
  275. case FIF_XBM:
  276. case FIF_XPM:
  277. case FIF_DDS:
  278. case FIF_GIF:
  279. case FIF_HDR:
  280. case FIF_FAXG3:
  281. case FIF_SGI:
  282. case FIF_EXR:
  283. case FIF_J2K:
  284. case FIF_JP2:
  285. case FIF_PFM:
  286. case FIF_PICT:
  287. case FIF_RAW:
  288. case FIF_WEBP:
  289. case FIF_JXR:
  290. return static_cast<FIF> (value);
  291. default:
  292. sLog.exception ("unknown free image format: ", value);
  293. }
  294. }