summaryrefslogtreecommitdiff
path: root/src/factories/TextureFactory.cpp
diff options
context:
space:
mode:
authorKiritoDv <kiritodev01@gmail.com>2026-03-23 21:23:59 -0600
committerKiritoDv <kiritodev01@gmail.com>2026-03-23 21:23:59 -0600
commit75f64161cb4addcbc6e29732df8d9b519a62c43f (patch)
tree92e2b17bd8c41af42bdf24a6eb4b7b08b90254f6 /src/factories/TextureFactory.cpp
parent654b451ddc6cf25db201b2c858948f430784cc5f (diff)
Implemented clang-format
Diffstat (limited to 'src/factories/TextureFactory.cpp')
-rw-r--r--src/factories/TextureFactory.cpp156
1 files changed, 87 insertions, 69 deletions
diff --git a/src/factories/TextureFactory.cpp b/src/factories/TextureFactory.cpp
index b7d555b..c436ca6 100644
--- a/src/factories/TextureFactory.cpp
+++ b/src/factories/TextureFactory.cpp
@@ -14,53 +14,55 @@ extern "C" {
static bool isTable = false;
static std::vector<std::string> tableEntries;
-static const std::unordered_map <std::string, TextureFormat> sTextureFormats = {
+static const std::unordered_map<std::string, TextureFormat> sTextureFormats = {
{ "RGBA16", { TextureType::RGBA16bpp, 16 } },
{ "RGBA32", { TextureType::RGBA32bpp, 32 } },
- { "CI4", { TextureType::Palette4bpp, 4 } },
- { "CI8", { TextureType::Palette8bpp, 8 } },
- { "I4", { TextureType::Grayscale4bpp, 4 } },
- { "I8", { TextureType::Grayscale8bpp, 8 } },
- { "IA1", { TextureType::GrayscaleAlpha1bpp, 1 } },
- { "IA4", { TextureType::GrayscaleAlpha4bpp, 4 } },
- { "IA8", { TextureType::GrayscaleAlpha8bpp, 8 } },
- { "IA16", { TextureType::GrayscaleAlpha16bpp, 16 } },
- { "TLUT", { TextureType::TLUT, 16 } },
+ { "CI4", { TextureType::Palette4bpp, 4 } },
+ { "CI8", { TextureType::Palette8bpp, 8 } },
+ { "I4", { TextureType::Grayscale4bpp, 4 } },
+ { "I8", { TextureType::Grayscale8bpp, 8 } },
+ { "IA1", { TextureType::GrayscaleAlpha1bpp, 1 } },
+ { "IA4", { TextureType::GrayscaleAlpha4bpp, 4 } },
+ { "IA8", { TextureType::GrayscaleAlpha8bpp, 8 } },
+ { "IA16", { TextureType::GrayscaleAlpha16bpp, 16 } },
+ { "TLUT", { TextureType::TLUT, 16 } },
};
-ExportResult TextureHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
+ExportResult TextureHeaderExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
+ std::string& entryName, YAML::Node& node, std::string* replacement) {
const auto symbol = GetSafeNode(node, "symbol", entryName);
const auto offset = GetSafeNode<uint32_t>(node, "offset");
auto format = GetSafeNode<std::string>(node, "format");
auto texture = std::static_pointer_cast<TextureData>(raw);
auto data = texture->mBuffer;
auto isOTR = Companion::Instance->IsOTRMode();
- size_t byteSize = std::max(1, (int) (texture->mFormat.depth / 8));
+ size_t byteSize = std::max(1, (int)(texture->mFormat.depth / 8));
const auto searchTable = Companion::Instance->SearchTable(offset);
- if(searchTable.has_value()){
+ if (searchTable.has_value()) {
const auto [name, start, end, mode, index_size] = searchTable.value();
unsigned int isize = index_size > -1 ? index_size : data.size() / byteSize;
- if(isOTR){
+ if (isOTR) {
write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
tableEntries.push_back(symbol);
- if(end == offset){
+ if (end == offset) {
write << "static const char* " << name << "[] = {\n";
- for(auto& entry : tableEntries){
+ for (auto& entry : tableEntries) {
write << tab_t << entry << ",\n";
}
write << "};\n\n";
tableEntries.clear();
}
} else {
- write << "extern " << GetSafeNode<std::string>(node, "ctype", "u8") << " " << name << "[][" << isize << "];\n";
+ write << "extern " << GetSafeNode<std::string>(node, "ctype", "u8") << " " << name << "[][" << isize
+ << "];\n";
}
} else {
- if(isOTR){
+ if (isOTR) {
write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
if (Companion::Instance->AddTextureDefines()) {
write << "#define _" << symbol << "_WIDTH 0x" << std::hex << texture->mWidth << std::dec << "\n";
@@ -78,7 +80,8 @@ ExportResult TextureHeaderExporter::Export(std::ostream &write, std::shared_ptr<
return std::nullopt;
}
-ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
+ExportResult TextureCodeExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw, std::string& entryName,
+ YAML::Node& node, std::string* replacement) {
auto texture = std::static_pointer_cast<TextureData>(raw);
auto data = texture->mBuffer;
auto offset = GetSafeNode<uint32_t>(node, "offset");
@@ -89,16 +92,16 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP
(*replacement) += "." + format;
std::string dpath = Companion::Instance->GetOutputPath() + "/" + (*replacement);
- if(!exists(fs::path(dpath).parent_path())){
+ if (!exists(fs::path(dpath).parent_path())) {
create_directories(fs::path(dpath).parent_path());
}
std::ostringstream imgstream;
- size_t byteSize = std::max(1, (int) (texture->mFormat.depth / 8));
+ size_t byteSize = std::max(1, (int)(texture->mFormat.depth / 8));
size_t isize = texture->mBuffer.size() / byteSize;
- for (int i = 0; i < data.size(); i+=byteSize) {
+ for (int i = 0; i < data.size(); i += byteSize) {
if (i % 16 == 0 && i != 0) {
imgstream << std::endl;
}
@@ -113,7 +116,7 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP
}
imgstream << std::endl;
- if (!Companion::Instance->IsUsingIndividualIncludes()){
+ if (!Companion::Instance->IsUsingIndividualIncludes()) {
std::ofstream file(dpath + ".inc.c", std::ios::binary);
file << imgstream.str();
file.close();
@@ -121,10 +124,10 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP
const auto searchTable = Companion::Instance->SearchTable(offset);
- if(searchTable.has_value()){
+ if (searchTable.has_value()) {
const auto [name, start, end, mode, index_size] = searchTable.value();
- if(mode != TableMode::Append){
+ if (mode != TableMode::Append) {
throw std::runtime_error("Reference mode is not supported for now");
}
@@ -132,29 +135,32 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP
isize = index_size;
}
- if(start == offset){
+ if (start == offset) {
write << GetSafeNode<std::string>(node, "ctype", "u8") << " " << name << "[][" << isize << "] = {\n";
}
write << tab_t << "{\n";
- if (!Companion::Instance->IsUsingIndividualIncludes()){
- write << tab_t << tab_t << "#include \"" << Companion::Instance->GetDestRelativeOutputPath() + "/" << *replacement << ".inc.c\"\n";
+ if (!Companion::Instance->IsUsingIndividualIncludes()) {
+ write << tab_t << tab_t << "#include \"" << Companion::Instance->GetDestRelativeOutputPath() + "/"
+ << *replacement << ".inc.c\"\n";
} else {
write << imgstream.str();
}
write << tab_t << "},\n";
- if(end == offset){
+ if (end == offset) {
write << "};\n";
if (Companion::Instance->IsDebug()) {
- write << "// size: 0x" << std::hex << std::uppercase << ASSET_PTR((end - start) + isize * byteSize) << "\n";
+ write << "// size: 0x" << std::hex << std::uppercase << ASSET_PTR((end - start) + isize * byteSize)
+ << "\n";
}
}
} else {
- write << GetSafeNode<std::string>(node, "ctype", "u8") << " " << symbol << "[] = {\n";
+ write << GetSafeNode<std::string>(node, "ctype", "u8") << " " << symbol << "[] = {\n";
- if (!Companion::Instance->IsUsingIndividualIncludes()){
- write << tab_t << "#include \"" << Companion::Instance->GetDestRelativeOutputPath() + "/" << *replacement << ".inc.c\"\n";
+ if (!Companion::Instance->IsUsingIndividualIncludes()) {
+ write << tab_t << "#include \"" << Companion::Instance->GetDestRelativeOutputPath() + "/" << *replacement
+ << ".inc.c\"\n";
} else {
write << imgstream.str();
}
@@ -170,28 +176,30 @@ ExportResult TextureCodeExporter::Export(std::ostream &write, std::shared_ptr<IP
return offset + isize * byteSize;
}
-ExportResult TextureBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
+ExportResult TextureBinaryExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
+ std::string& entryName, YAML::Node& node, std::string* replacement) {
auto writer = LUS::BinaryWriter();
auto texture = std::static_pointer_cast<TextureData>(raw);
auto data = texture->mBuffer;
WriteHeader(writer, Torch::ResourceType::Texture, 0);
- if(texture->mFormat.type == TextureType::TLUT) {
+ if (texture->mFormat.type == TextureType::TLUT) {
texture->mFormat.type = TextureType::RGBA16bpp;
}
- writer.Write((uint32_t) texture->mFormat.type);
+ writer.Write((uint32_t)texture->mFormat.type);
writer.Write(texture->mWidth);
writer.Write(texture->mHeight);
- writer.Write((uint32_t) data.size());
- writer.Write((char*) data.data(), data.size());
+ writer.Write((uint32_t)data.size());
+ writer.Write((char*)data.data(), data.size());
writer.Finish(write);
return std::nullopt;
}
-ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<IParsedData> data, std::string&entryName, YAML::Node&node, std::string* replacement) {
+ExportResult TextureModdingExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> data,
+ std::string& entryName, YAML::Node& node, std::string* replacement) {
auto texture = std::static_pointer_cast<TextureData>(data);
auto format = texture->mFormat;
uint8_t* raw = new uint8_t[TextureUtils::CalculateTextureSize(format.type, texture->mWidth, texture->mHeight) * 2];
@@ -207,7 +215,7 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<
case TextureType::RGBA16bpp:
case TextureType::RGBA32bpp: {
rgba* imgr = raw2rgba(texture->mBuffer.data(), texture->mWidth, texture->mHeight, format.depth);
- if(rgba2png(&raw, &size, imgr, texture->mWidth, texture->mHeight)) {
+ if (rgba2png(&raw, &size, imgr, texture->mWidth, texture->mHeight)) {
throw std::runtime_error("Failed to convert texture to PNG");
}
break;
@@ -217,7 +225,7 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<
case TextureType::GrayscaleAlpha4bpp:
case TextureType::GrayscaleAlpha1bpp: {
ia* imgia = raw2ia(texture->mBuffer.data(), texture->mWidth, texture->mHeight, format.depth);
- if(ia2png(&raw, &size, imgia, texture->mWidth, texture->mHeight)) {
+ if (ia2png(&raw, &size, imgia, texture->mWidth, texture->mHeight)) {
throw std::runtime_error("Failed to convert texture to PNG");
}
break;
@@ -225,29 +233,35 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<
case TextureType::Palette8bpp:
case TextureType::Palette4bpp: {
if (node["tlut_symbol"]) {
- auto tlut = GetSafeNode<std::string>(node,"tlut_symbol");
+ auto tlut = GetSafeNode<std::string>(node, "tlut_symbol");
auto palette = Companion::Instance->GetParseDataBySymbol(tlut);
if (palette.has_value()) {
auto palTexture = std::static_pointer_cast<TextureData>(palette.value().data.value());
- convert_raw_to_ci8(&raw, &size, texture->mBuffer.data(), (uint8_t *)palTexture->mBuffer.data(), 0, texture->mWidth, texture->mHeight, texture->mFormat.depth, palTexture->mFormat.depth);
+ convert_raw_to_ci8(&raw, &size, texture->mBuffer.data(), (uint8_t*)palTexture->mBuffer.data(), 0,
+ texture->mWidth, texture->mHeight, texture->mFormat.depth,
+ palTexture->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");
+ throw std::runtime_error("Could not convert ci8 '" + symbol +
+ "' the tlut symbol name is probably wrong for tlut_symbol node");
}
break;
}
if (node["tlut"]) {
- auto tlut = GetSafeNode<uint32_t>(node,"tlut");
+ auto tlut = GetSafeNode<uint32_t>(node, "tlut");
auto palette = Companion::Instance->GetParseDataByAddr(tlut);
if (palette.has_value()) {
auto palTexture = std::static_pointer_cast<TextureData>(palette.value().data.value());
- convert_raw_to_ci8(&raw, &size, texture->mBuffer.data(), (uint8_t *)palTexture->mBuffer.data(), 0, texture->mWidth, texture->mHeight, texture->mFormat.depth, palTexture->mFormat.depth);
+ convert_raw_to_ci8(&raw, &size, texture->mBuffer.data(), (uint8_t*)palTexture->mBuffer.data(), 0,
+ texture->mWidth, texture->mHeight, texture->mFormat.depth,
+ palTexture->mFormat.depth);
} else {
auto symbol = GetSafeNode<std::string>(node, "symbol");
- throw std::runtime_error("Could not convert ci8 '"+symbol+"' the address is probably wrong for tlut address node");
+ throw std::runtime_error("Could not convert ci8 '" + symbol +
+ "' the address is probably wrong for tlut address node");
}
break;
}
@@ -255,7 +269,7 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<
case TextureType::Grayscale8bpp:
case TextureType::Grayscale4bpp: {
ia* imgi = raw2i(texture->mBuffer.data(), texture->mWidth, texture->mHeight, format.depth);
- if(ia2png(&raw, &size, imgi, texture->mWidth, texture->mHeight)) {
+ if (ia2png(&raw, &size, imgi, texture->mWidth, texture->mHeight)) {
throw std::runtime_error("Failed to convert texture to PNG");
}
break;
@@ -269,7 +283,6 @@ ExportResult TextureModdingExporter::Export(std::ostream&write, std::shared_ptr<
return std::nullopt;
}
-
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");
@@ -283,17 +296,18 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
if (format.empty()) {
SPDLOG_ERROR("Texture entry at {:X} in yaml missing format node\n\
Please add one of the following formats\n\
- rgba16, rgba32, ia16, ia8, ia4, i8, i4, ci8, ci4, 1bpp, tlut", offset);
+ rgba16, rgba32, ia16, ia8, ia4, i8, i4, ci8, ci4, 1bpp, tlut",
+ offset);
return std::nullopt;
}
- if(!Torch::contains(sTextureFormats, format)) {
+ if (!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
TextureFormat fmt = sTextureFormats.at(format);
- if(fmt.type == TextureType::TLUT){
+ if (fmt.type == TextureType::TLUT) {
width = GetSafeNode<uint32_t>(node, "colors");
height = 1;
} else {
@@ -301,7 +315,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
height = GetSafeNode<uint32_t>(node, "height");
}
- if((format == "CI4" || format == "CI8") && node["tlut"] && node["colors"]) {
+ if ((format == "CI4" || format == "CI8") && node["tlut"] && node["colors"]) {
YAML::Node tlutNode;
const auto tlutOffset = GetSafeNode<uint32_t>(node, "tlut");
const auto tlutSymbol = GetSafeNode(node, "tlut_symbol", symbol + "_tlut");
@@ -313,23 +327,24 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
tlutNode["offset"] = tlutOffset;
tlutNode["colors"] = GetSafeNode<uint32_t>(node, "colors");
node["tlut"] = tlutOffset;
- if(node["tlut_ctype"]) {
+ if (node["tlut_ctype"]) {
tlutNode["ctype"] = GetSafeNode<std::string>(node, "tlut_ctype");
}
Companion::Instance->AddAsset(tlutNode);
}
- size = GetSafeNode<uint32_t>(node, "size", TextureUtils::CalculateTextureSize(sTextureFormats.at(format).type, width, height));
+ size = GetSafeNode<uint32_t>(node, "size",
+ TextureUtils::CalculateTextureSize(sTextureFormats.at(format).type, width, height));
auto [_, segment] = Decompressor::AutoDecode(node, buffer, size);
std::vector<uint8_t> result;
- if(fmt.type == TextureType::GrayscaleAlpha1bpp){
+ if (fmt.type == TextureType::GrayscaleAlpha1bpp) {
result = TextureUtils::alloc_ia8_text_from_i1(reinterpret_cast<uint16_t*>(segment.data), 8, 16);
} else {
result = std::vector(segment.data, segment.data + segment.size);
}
SPDLOG_INFO("Texture: {}", format);
- if(fmt.type == TextureType::TLUT){
+ if (fmt.type == TextureType::TLUT) {
SPDLOG_INFO("Colors: {}", width);
} else {
SPDLOG_INFO("Width: {}", width);
@@ -338,18 +353,19 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
SPDLOG_INFO("Size: {}", size);
SPDLOG_INFO("Offset: 0x{:X}", offset);
- if(result.size() == 0){
+ if (result.size() == 0) {
return std::nullopt;
}
- if(result.size() == 0){
+ if (result.size() == 0) {
return std::nullopt;
}
return std::make_shared<TextureData>(fmt, width, height, result);
}
-std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::vector<uint8_t>& buffer, YAML::Node& node) {
+std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::vector<uint8_t>& buffer,
+ YAML::Node& node) {
auto format = GetSafeNode<std::string>(node, "format");
int width;
int height;
@@ -359,16 +375,17 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
if (format.empty()) {
SPDLOG_ERROR("Texture entry at {:X} in yaml missing format node\n\
Please add one of the following formats\n\
- rgba16, rgba32, ia16, ia8, ia4, i8, i4, ci8, ci4, 1bpp, tlut", offset);
+ rgba16, rgba32, ia16, ia8, ia4, i8, i4, ci8, ci4, 1bpp, tlut",
+ offset);
return std::nullopt;
}
- if(!Torch::contains(sTextureFormats, format)) {
+ if (!Torch::contains(sTextureFormats, format)) {
return std::nullopt;
}
TextureFormat fmt = sTextureFormats.at(format);
- if(fmt.type == TextureType::TLUT){
+ if (fmt.type == TextureType::TLUT) {
width = GetSafeNode<uint32_t>(node, "colors");
height = 1;
} else {
@@ -384,7 +401,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
const auto imgr = png2rgba(buffer.data(), buffer.size(), &width, &height);
size = width * height * fmt.depth / 8;
raw = new uint8_t[size];
- if(rgba2raw(raw, imgr, width, height, fmt.depth) <= 0){
+ if (rgba2raw(raw, imgr, width, height, fmt.depth) <= 0) {
throw std::runtime_error("Failed to convert PNG to texture");
}
break;
@@ -396,7 +413,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
const auto imgia = png2ia(buffer.data(), buffer.size(), &width, &height);
size = width * height * fmt.depth / 8;
raw = new uint8_t[size];
- if(ia2raw(raw, imgia, width, height, fmt.depth) <= 0){
+ if (ia2raw(raw, imgia, width, height, fmt.depth) <= 0) {
throw std::runtime_error("Failed to convert PNG to texture");
}
break;
@@ -417,7 +434,8 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
// 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);
+ // 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];
@@ -436,7 +454,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
const auto imgi = png2ia(buffer.data(), buffer.size(), &width, &height);
size = width * height * fmt.depth / 8;
raw = new uint8_t[size];
- if(i2raw(raw, imgi, width, height, fmt.depth) <= 0){
+ if (i2raw(raw, imgi, width, height, fmt.depth) <= 0) {
throw std::runtime_error("Failed to convert PNG to texture");
}
break;
@@ -450,7 +468,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
auto result = std::vector(raw, raw + size);
SPDLOG_INFO("Texture: {}", format);
- if(fmt.type == TextureType::TLUT){
+ if (fmt.type == TextureType::TLUT) {
SPDLOG_INFO("Colors: {}", width);
} else {
SPDLOG_INFO("Width: {}", width);
@@ -459,7 +477,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse_modding(std::v
SPDLOG_INFO("Size: {}", size);
SPDLOG_INFO("Offset: 0x{:X}", offset);
- if(result.size() == 0){
+ if (result.size() == 0) {
return std::nullopt;
}