| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- #include <cmath>
- #include <cstring>
- #include <lz4.h>
- #include <nlohmann/json.hpp>
- #include "TextureParser.h"
- #include "WallpaperEngine/Data/Assets/Texture.h"
- #include "WallpaperEngine/Logging/Log.h"
- using namespace WallpaperEngine::Data::Assets;
- using namespace WallpaperEngine::Data::Parsers;
- TextureUniquePtr TextureParser::parse (const BinaryReader& file) {
- auto result = std::make_unique<Texture> ();
- // wallpaper64.exe sub_14015E580: TEXV0004 is a version 0 TEXI header followed by a version 0 TEXB body (one
- // image, mips like TEXB0001) with no section tags and no TEXS
- const bool legacy = parseTextureHeader (*result, file);
- if (legacy) {
- result->containerVersion = ContainerVersion_TEXB0001;
- result->imageCount = 1;
- } else {
- parseContainer (*result, file);
- }
- for (uint32_t image = 0; image < result->imageCount; image++) {
- const uint32_t mipmapCount = file.nextUInt32 ();
- MipmapList mipmaps;
- for (uint32_t mipmap = 0; mipmap < mipmapCount; mipmap++) {
- mipmaps.emplace_back (parseMipmap (file, *result));
- if (!result->conditions.empty ()) {
- parseMipmapPatches (*mipmaps.back (), file);
- }
- }
- result->images.emplace (image, mipmaps);
- }
- if (legacy || !result->isAnimated ()) {
- return result;
- }
- parseAnimations (*result, file);
- return result;
- }
- MipmapSharedPtr TextureParser::parseMipmap (const BinaryReader& file, const Texture& header) {
- auto result = std::make_shared<Mipmap> ();
- result->width = file.nextUInt32 ();
- result->height = file.nextUInt32 ();
- // wallpaper64.exe sub_14015C8D0, volume textures (image filter LUTs)
- if (header.flags & TextureFlags_Volume) {
- result->depth = file.nextUInt32 ();
- }
- if (header.containerVersion == ContainerVersion_TEXB0003 || header.containerVersion == ContainerVersion_TEXB0002) {
- result->compression = file.nextUInt32 ();
- result->uncompressedSize = file.nextInt ();
- }
- result->compressedSize = file.nextInt ();
- if (result->compression == 0) {
- // misnamed: in uncompressed files compressedSize actually holds the file length
- result->uncompressedSize = result->compressedSize;
- }
- result->uncompressedData = std::unique_ptr<char[]> (new char[result->uncompressedSize]);
- if (result->compression == 1) {
- result->compressedData = std::unique_ptr<char[]> (new char[result->compressedSize]);
- file.next (result->compressedData.get (), result->compressedSize);
- int bytes = LZ4_decompress_safe (
- result->compressedData.get (), result->uncompressedData.get (), result->compressedSize,
- result->uncompressedSize
- );
- if (bytes < 0) {
- sLog.exception ("Cannot decompress texture data, LZ4_decompress_safe returned an error");
- }
- } else {
- file.next (result->uncompressedData.get (), result->uncompressedSize);
- }
- return result;
- }
- void TextureParser::parseMipmapPatches (Mipmap& mipmap, const BinaryReader& file) {
- const uint32_t groupCount = file.nextUInt32 ();
- for (uint32_t group = 0; group < groupCount; group++) {
- const uint32_t patchCount = file.nextUInt32 ();
- for (uint32_t index = 0; index < patchCount; index++) {
- MipmapPatch patch;
- std::ignore = file.nextUInt32 ();
- patch.key = file.nextUInt32 ();
- patch.x = file.nextUInt32 ();
- patch.y = file.nextUInt32 ();
- patch.width = file.nextUInt32 ();
- patch.height = file.nextUInt32 ();
- patch.format = file.nextInt ();
- patch.data.resize (file.nextUInt32 ());
- file.next (patch.data.data (), patch.data.size ());
- mipmap.patches.push_back (std::move (patch));
- }
- }
- }
- FrameSharedPtr TextureParser::parseFrameV1 (const BinaryReader& file) {
- auto result = std::make_shared<Frame> ();
- result->frameNumber = file.nextUInt32 ();
- result->frametime = file.nextFloat ();
- result->x = static_cast<float> (file.nextUInt32 ());
- result->y = static_cast<float> (file.nextUInt32 ());
- result->width1 = static_cast<float> (file.nextUInt32 ());
- result->width2 = static_cast<float> (file.nextUInt32 ());
- result->height2 = static_cast<float> (file.nextUInt32 ());
- result->height1 = static_cast<float> (file.nextUInt32 ());
- return result;
- }
- FrameSharedPtr TextureParser::parseFrame (const BinaryReader& file) {
- auto result = std::make_shared<Frame> ();
- result->frameNumber = file.nextUInt32 ();
- result->frametime = file.nextFloat ();
- result->x = file.nextFloat ();
- result->y = file.nextFloat ();
- result->width1 = file.nextFloat ();
- result->width2 = file.nextFloat ();
- result->height2 = file.nextFloat ();
- result->height1 = file.nextFloat ();
- return result;
- }
- TextureMap TextureParser::parseTextureMap (const JSON& it) {
- if (!it.is_array ()) {
- return {};
- }
- TextureMap result = {};
- int textureIndex = -1;
- for (const auto& cur : it) {
- textureIndex++;
- if (cur.is_null ()) {
- continue;
- }
- if (cur.is_object ()) {
- const auto nameIt = cur.find ("name");
- if (nameIt != cur.end () && nameIt->is_string ()) {
- result.emplace (textureIndex, nameIt->get<std::string> ());
- }
- } else if (cur.is_string ()) {
- std::string texName = cur;
- if (!texName.empty ()) {
- result.emplace (textureIndex, texName);
- }
- }
- }
- return result;
- }
- TextureFormat TextureParser::parseTextureFormat (uint32_t value) {
- switch (value) {
- case TextureFormat_UNKNOWN:
- case TextureFormat_ARGB8888:
- case TextureFormat_RGB888:
- case TextureFormat_RGB565:
- case TextureFormat_DXT5:
- case TextureFormat_DXT3:
- case TextureFormat_DXT1:
- case TextureFormat_RG88:
- case TextureFormat_R8:
- case TextureFormat_RG1616f:
- case TextureFormat_R16f:
- case TextureFormat_BC7:
- case TextureFormat_RGBa1010102:
- case TextureFormat_RGBA16161616f:
- case TextureFormat_RGB161616f:
- return static_cast<TextureFormat> (value);
- default:
- sLog.exception ("unknown texture format: ", value);
- }
- }
- bool TextureParser::parseTextureHeader (Texture& header, const BinaryReader& file) {
- char magic[9] = { 0 };
- file.next (magic, 9);
- const bool legacy = strncmp (magic, "TEXV0004", 9) == 0;
- if (!legacy && strncmp (magic, "TEXV0005", 9) != 0) {
- sLog.exception ("unexpected texture container type: ", std::string_view (magic, 9));
- }
- if (!legacy) {
- file.next (magic, 9);
- if (strncmp (magic, "TEXI0001", 9) != 0) {
- sLog.exception ("unexpected texture sub-container type: ", std::string_view (magic, 9));
- }
- }
- header.format = parseTextureFormat (file.nextUInt32 ());
- header.flags = parseTextureFlags (file.nextUInt32 ());
- header.textureWidth = file.nextUInt32 ();
- header.textureHeight = file.nextUInt32 ();
- header.width = file.nextUInt32 ();
- header.height = file.nextUInt32 ();
- // wallpaper64.exe sub_14015C760
- if (header.flags & TextureFlags_Volume) {
- header.depth = file.nextUInt32 ();
- }
- // only TEXI version 1 and up has this field
- if (!legacy) {
- std::ignore = file.nextUInt32 ();
- }
- return legacy;
- }
- void TextureParser::parseContainer (Texture& header, const BinaryReader& file) {
- char magic[9] = { 0 };
- file.next (magic, 9);
- header.imageCount = file.nextUInt32 ();
- if (strncmp (magic, "TEXB0004", 9) == 0) {
- // sub_14015C8D0 (2.8.42): FIF, property conditions, then TEXB0003 mipmaps. Videos are detected by the texture
- // flags, not this header
- header.containerVersion = ContainerVersion_TEXB0003;
- header.freeImageFormat = parseFIF (file.nextUInt32 ());
- const uint32_t conditionCount = file.nextUInt32 ();
- for (uint32_t index = 0; index < conditionCount; index++) {
- TextureCondition condition;
- condition.group = file.nextUInt32 ();
- condition.key = file.nextUInt32 ();
- condition.flags = file.nextUInt32 ();
- condition.json = file.nextNullTerminatedString ();
- header.conditions.push_back (std::move (condition));
- }
- } else if (strncmp (magic, "TEXB0003", 9) == 0) {
- header.containerVersion = ContainerVersion_TEXB0003;
- header.freeImageFormat = parseFIF (file.nextUInt32 ());
- } else if (strncmp (magic, "TEXB0002", 9) == 0) {
- header.containerVersion = ContainerVersion_TEXB0002;
- } else if (strncmp (magic, "TEXB0001", 9) == 0) {
- header.containerVersion = ContainerVersion_TEXB0001;
- } else {
- sLog.exception ("unknown texture format type: ", std::string_view (magic, 9));
- }
- }
- void TextureParser::parseAnimations (Texture& header, const BinaryReader& file) {
- char magic[9] = { 0 };
- file.next (magic, 9);
- if (strncmp (magic, "TEXS0001", 9) == 0) {
- header.animatedVersion = AnimatedVersion_TEXS0001;
- } else if (strncmp (magic, "TEXS0002", 9) == 0) {
- header.animatedVersion = AnimatedVersion_TEXS0002;
- } else if (strncmp (magic, "TEXS0003", 9) == 0) {
- header.animatedVersion = AnimatedVersion_TEXS0003;
- } else {
- sLog.exception ("found animation information of unknown type: ", std::string_view (magic, 9));
- }
- uint32_t frameCount = file.nextUInt32 ();
- if (header.animatedVersion == AnimatedVersion_TEXS0003) {
- header.gifWidth = file.nextUInt32 ();
- header.gifHeight = file.nextUInt32 ();
- }
- while (frameCount-- > 0) {
- if (header.animatedVersion == AnimatedVersion_TEXS0001) {
- header.frames.push_back (parseFrameV1 (file));
- } else {
- header.frames.push_back (parseFrame (file));
- }
- }
- // wallpaper64.exe 2.8.42 sub_14015E1D0: before TEXS0003 the frame size is the TEXI image size
- if (header.animatedVersion == AnimatedVersion_TEXS0001 || header.animatedVersion == AnimatedVersion_TEXS0002) {
- header.gifWidth = header.width;
- header.gifHeight = header.height;
- }
- }
- uint32_t TextureParser::parseTextureFlags (uint32_t value) { return value & TextureFlags_All; }
- FIF TextureParser::parseFIF (uint32_t value) {
- switch (value) {
- case FIF_UNKNOWN:
- case FIF_BMP:
- case FIF_ICO:
- case FIF_JPEG:
- case FIF_JNG:
- case FIF_KOALA:
- case FIF_LBM:
- case FIF_MNG:
- case FIF_PBM:
- case FIF_PBMRAW:
- case FIF_PCD:
- case FIF_PCX:
- case FIF_PGM:
- case FIF_PGMRAW:
- case FIF_PNG:
- case FIF_PPM:
- case FIF_PPMRAW:
- case FIF_RAS:
- case FIF_TARGA:
- case FIF_TIFF:
- case FIF_WBMP:
- case FIF_PSD:
- case FIF_CUT:
- case FIF_XBM:
- case FIF_XPM:
- case FIF_DDS:
- case FIF_GIF:
- case FIF_HDR:
- case FIF_FAXG3:
- case FIF_SGI:
- case FIF_EXR:
- case FIF_J2K:
- case FIF_JP2:
- case FIF_PFM:
- case FIF_PICT:
- case FIF_RAW:
- case FIF_WEBP:
- case FIF_JXR:
- return static_cast<FIF> (value);
- default:
- sLog.exception ("unknown free image format: ", value);
- }
- }
|