diff options
| author | MegaMech <MegaMech@users.noreply.github.com> | 2024-04-06 15:36:03 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-06 15:36:03 -0600 |
| commit | 24633ebd7cc6866da3e3d06fddc6f0f49a741f1d (patch) | |
| tree | 357a563fae006b04ded941ee534b1d6522f3fc32 /src/factories/TextureFactory.cpp | |
| parent | ad667b705dad3476a1ae803f23588c55baab2ee4 (diff) | |
Introduce Metadata and ItemCurve Factories (#27)
* Introduce metadata
* Metadata works now
* Cleanup
* Fixes
* Cleanup 2
* yaml improvement
* Comment update
* Rename tables to metadata
* Update
* Fix warnings
* Implement ItemCurve
* Remove unused enum
* Replace static with const
* Fix bug
* Add const
* More fixes
* Fix quote bug in IncludeFactory
* Fix header in ItemCurve
* Fixes
* Fix texture factory
* updates
* Clearer debug info
* Fixes
* Working ci8 png
* Fix size output
* Fix
* Add gitignore for modding
* Fix precision for mtx
* Remove header exporter from metadata
* Remove hardcoded enum
* Remove tabs
* Remove tabs
* tlut temp fix
---------
Co-authored-by: = <=>
Diffstat (limited to 'src/factories/TextureFactory.cpp')
| -rw-r--r-- | src/factories/TextureFactory.cpp | 99 |
1 files changed, 67 insertions, 32 deletions
diff --git a/src/factories/TextureFactory.cpp b/src/factories/TextureFactory.cpp index a293bfc..5d7ebc0 100644 --- a/src/factories/TextureFactory.cpp +++ b/src/factories/TextureFactory.cpp @@ -20,9 +20,9 @@ static const std::unordered_map <std::string, TextureFormat> gTextureFormats = { { "I4", { TextureType::Grayscale4bpp, 4 } }, { "I8", { TextureType::Grayscale8bpp, 8 } }, { "IA1", { TextureType::GrayscaleAlpha1bpp, 1 } }, - { "IA4", { TextureType::GrayscaleAlpha4bpp, 4 } }, - { "IA8", { TextureType::GrayscaleAlpha8bpp, 8 } }, - { "IA16", { TextureType::GrayscaleAlpha16bpp, 16 } }, + { "IA4", { TextureType::GrayscaleAlpha4bpp, 4 } }, + { "IA8", { TextureType::GrayscaleAlpha8bpp, 8 } }, + { "IA16", { TextureType::GrayscaleAlpha16bpp, 16 } }, { "TLUT", { TextureType::TLUT, 16 } }, }; @@ -156,7 +156,7 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP } if(start == offset){ - write << GetSafeNode<std::string>(node, "ctype", "static unsigned char") << " " << name << "[][" << (texture->mBuffer.size() / byteSize) << "] = {\n"; + write << GetSafeNode<std::string>(node, "ctype", "u8") << " " << name << "[][" << (texture->mBuffer.size() / byteSize) << "] = {\n"; } write << tab << "{\n"; @@ -170,7 +170,7 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP } } } else { - write << GetSafeNode<std::string>(node, "ctype", "static const u8") << " " << symbol << "[] = {\n"; + write << GetSafeNode<std::string>(node, "ctype", "u8") << " " << symbol << "[] = {\n"; write << tab << "#include \"" << Companion::Instance->GetOutputPath() + "/" << *replacement << ".inc.c\"\n"; write << "};\n"; @@ -235,11 +235,21 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr< } case TextureType::Palette8bpp: case TextureType::Palette4bpp: { - ci* imgi = raw2ci_torch(texture->mBuffer.data(), texture->mWidth, texture->mHeight, format.depth); - if(ci2png(&raw, &size, imgi, texture->mWidth, texture->mHeight)) { - throw std::runtime_error("Failed to convert texture to PNG"); + // This check needed until sf64 has tluts fixed. + if (node["tlut_symbol"]) { + auto tlut = GetSafeNode<std::string>(node,"tlut_symbol"); + auto tlutTextureMap = Companion::Instance->GetTlutTextureMap(); + auto palettePtr = tlutTextureMap[tlut]; + + if (palettePtr) { + convert_raw_to_ci8(&raw, &size, texture->mBuffer.data(), (uint8_t *)palettePtr->mBuffer.data(), 0, texture->mWidth, texture->mHeight, palettePtr->mFormat.depth); + + } else { + auto symbol = GetSafeNode<std::string>(node, "symbol"); + throw std::runtime_error("Could not convert ci8 '"+symbol+"' the tlut symbol name is probably wrong for tlut_symbol node"); + } + break; } - break; } case TextureType::Grayscale8bpp: case TextureType::Grayscale4bpp: { @@ -262,6 +272,7 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr< std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) { auto offset = GetSafeNode<uint32_t>(node, "offset"); auto format = GetSafeNode<std::string>(node, "format"); + auto symbol = GetSafeNode<std::string>(node, "symbol"); uint32_t width; uint32_t height; uint32_t size; @@ -275,11 +286,11 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui return std::nullopt; } - if(!gTextureFormats.contains(format)) { - return std::nullopt; - } + if(!gTextureFormats.contains(format)) { + return std::nullopt; + } - TextureFormat fmt = gTextureFormats.at(format); + TextureFormat fmt = gTextureFormats.at(format); if(fmt.type == TextureType::TLUT){ width = GetSafeNode<uint32_t>(node, "colors"); @@ -292,13 +303,10 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui if((format == "CI4" || format == "CI8") && node["tlut"] && node["colors"]) { YAML::Node tlutNode; const auto tlutOffset = GetSafeNode<uint32_t>(node, "tlut"); - if(node["symbol"]) { - const auto symbol = GetSafeNode<std::string>(node, "symbol"); - const auto tlutSymbol = GetSafeNode(node, "tlut_symbol", symbol + "_tlut"); - std::ostringstream offsetSeg; - offsetSeg << std::uppercase << std::hex << tlutOffset; - tlutNode["symbol"] = std::regex_replace(tlutSymbol, std::regex(R"(OFFSET)"), offsetSeg.str()); - } + const auto tlutSymbol = GetSafeNode(node, "tlut_symbol", symbol + "_tlut"); + std::ostringstream offsetSeg; + offsetSeg << std::uppercase << std::hex << tlutOffset; + tlutNode["symbol"] = std::regex_replace(tlutSymbol, std::regex(R"(OFFSET)"), offsetSeg.str()); tlutNode["type"] = "TEXTURE"; tlutNode["format"] = "TLUT"; tlutNode["offset"] = tlutOffset; @@ -325,13 +333,23 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui SPDLOG_INFO("Width: {}", width); SPDLOG_INFO("Height: {}", height); } - SPDLOG_INFO("Size: {}", size); + SPDLOG_INFO("Size: {}", size); SPDLOG_INFO("Offset: 0x{:X}", offset); if(result.size() == 0){ return std::nullopt; } + if(result.size() == 0){ + return std::nullopt; + } + + if (fmt.type == TextureType::TLUT) { + auto textureData = std::make_shared<TextureData>(fmt, width, height, result); + Companion::Instance->AddTlutTextureMap(symbol, textureData); + return textureData; + } + return std::make_shared<TextureData>(fmt, width, height, result); } @@ -349,11 +367,11 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v return std::nullopt; } - if(!gTextureFormats.contains(format)) { - return std::nullopt; - } + if(!gTextureFormats.contains(format)) { + return std::nullopt; + } - TextureFormat fmt = gTextureFormats.at(format); + TextureFormat fmt = gTextureFormats.at(format); if(fmt.type == TextureType::TLUT){ width = GetSafeNode<uint32_t>(node, "colors"); height = 1; @@ -389,14 +407,31 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v } case TextureType::Palette8bpp: case TextureType::Palette4bpp: { - SPDLOG_WARN("Converting PNG to CI texture is kind of broken, use at your own risk!"); - const auto imgi = png2ci(buffer.data(), buffer.size(), &width, &height); - size = width * height * fmt.depth / 8; - raw = new uint8_t[size]; + // This implementation is not correct. + // The process should be: + // png2rgba --> imgpal2rawci - if(ci2raw_torch(raw, imgi, width, height, fmt.depth) <= 0){ - throw std::runtime_error("Failed to convert PNG to texture"); - } + // todo: Add wheel palette input + // Implement so that it works. + + // auto tlut = GetSafeNode<std::string>(node,"tlut_symbol"); + // auto tlutTextureMap = Companion::Instance->GetTlutTextureMap(); + // auto palettePtr = tlutTextureMap[tlut]; + + // if (palettePtr) { + + // auto imgi = png2rgba(buffer.data(), buffer.size(), &width, &height); + // auto pal = png2rgba(palettePtr->mBuffer.data(), (palettePtr->mWidth * palettePtr->mWidth * palettePtr->mFormat.depth * 2), &width, &height); + + // size = width * height * fmt.depth / 8; + // raw = new uint8_t[size]; + + // if(imgpal2rawci(raw, imgi, pal, 0, 0, width, height, fmt.depth) <= 0){ + // throw std::runtime_error("Failed to convert PNG to texture"); + // } + // } else { + + // } break; } case TextureType::Grayscale8bpp: |
