Added auto texture size calculation

This commit is contained in:
KiritoDv
2024-01-31 12:05:40 -06:00
committed by Lywx
parent 6e5414f704
commit 7c93abed9a
4 changed files with 43 additions and 10 deletions
+7 -2
View File
@@ -64,12 +64,14 @@ void Companion::ExtractNode(YAML::Node& node, std::string& name, SWrapper* binar
auto type = GetSafeNode<std::string>(node, "type");
std::transform(type.begin(), type.end(), type.begin(), ::toupper);
spdlog::set_pattern(regular);
if(node["offset"]) {
auto offset = node["offset"].as<uint32_t>();
SPDLOG_INFO("[{}] Processing {} at 0x{:X}", type, name, offset);
SPDLOG_INFO("- [{}] Processing {} at 0x{:X}", type, name, offset);
} else {
SPDLOG_INFO("[{}] Processing {}", type, name);
SPDLOG_INFO("- [{}] Processing {}", type, name);
}
spdlog::set_pattern(line);
auto factory = this->GetFactory(type);
if(!factory.has_value()){
@@ -97,6 +99,9 @@ void Companion::ExtractNode(YAML::Node& node, std::string& name, SWrapper* binar
std::replace(doutput.begin(), doutput.end(), '\\', '/');
this->gAssetDependencies[this->gCurrentFile][fst].second = true;
this->ExtractNode(snd.first, doutput, binary);
spdlog::set_pattern(regular);
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);
}
switch (this->gConfig.exporterType) {
+32 -4
View File
@@ -17,6 +17,32 @@ static const std::unordered_map <std::string, TextureType> gTextureTypes = {
{ "IA16", TextureType::GrayscaleAlpha16bpp },
};
size_t CalculateTextureSize(TextureType type, uint32_t width, uint32_t height) {
switch (type) {
// 4 bytes per pixel
case TextureType::RGBA32bpp:
return width * height * 4;
// 2 bytes per pixel
case TextureType::RGBA16bpp:
case TextureType::GrayscaleAlpha16bpp:
return width * height * 2;
// 1 byte per pixel
case TextureType::Grayscale8bpp:
case TextureType::Palette8bpp:
case TextureType::GrayscaleAlpha8bpp:
// TODO: We need to validate this MegaMech
case TextureType::GrayscaleAlpha1bpp:
return width * height;
// 1/2 byte per pixel
case TextureType::Palette4bpp:
case TextureType::Grayscale4bpp:
case TextureType::GrayscaleAlpha4bpp:
return (width * height) / 2;
default:
return 0;
}
}
std::vector<uint8_t> alloc_ia8_text_from_i1(uint16_t *in, int16_t width, int16_t height) {
int32_t inPos;
uint16_t bitMask;
@@ -110,7 +136,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
auto format = GetSafeNode<std::string>(node, "format");
auto width = GetSafeNode<uint32_t>(node, "width");
auto height = GetSafeNode<uint32_t>(node, "height");
auto size = GetSafeNode<uint32_t>(node, "size");
auto size = GetSafeNode<uint32_t>(node, "size", CalculateTextureSize(gTextureTypes.at(format), width, height));
auto offset = Decompressor::TranslateAddr(GetSafeNode<uint32_t>(node, "offset"));
if (format.empty()) {
@@ -126,7 +152,7 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
TextureType type = gTextureTypes.at(format);
auto [_, segment] = Decompressor::AutoDecode(node, buffer);
auto [_, segment] = Decompressor::AutoDecode(node, buffer, size);
std::vector<uint8_t> result;
if(type == TextureType::GrayscaleAlpha1bpp){
@@ -135,10 +161,12 @@ std::optional<std::shared_ptr<IParsedData>> TextureFactory::parse(std::vector<ui
result = std::vector(segment.data, segment.data + segment.size);
}
SPDLOG_INFO("Texture: {} {}x{} {}", format, width, height, size);
SPDLOG_INFO("Texture: {}", format);
SPDLOG_INFO("Width: {}", width);
SPDLOG_INFO("Height: {}", height);
SPDLOG_INFO("Size: {}", size);
SPDLOG_INFO("Offset: 0x{:X}", offset);
SPDLOG_INFO("Is Compressed: {}", Decompressor::IsCompressed(node) ? "true" : "false");
SPDLOG_INFO("Size: {}", result.size());
if(result.size() == 0){
return std::nullopt;
+3 -3
View File
@@ -35,7 +35,7 @@ DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32
}
}
DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer) {
DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer, std::optional<size_t> manualSize) {
if(!node["offset"]){
throw std::runtime_error("Failed to find offset");
}
@@ -45,7 +45,7 @@ DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>
if(node["mio0"]){
const auto mio0 = node["mio0"].as<uint32_t>();
auto decoded = Decode(buffer, offset, CompressionType::MIO0);
auto size = node["size"] ? node["size"].as<size_t>() : decoded->size - mio0;
auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size - mio0);
return {
.root = decoded,
@@ -63,7 +63,7 @@ DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>
return {
.root = nullptr,
.segment = { buffer.data() + offset, node["size"] ? node["size"].as<size_t>() : buffer.size() - offset }
.segment = { buffer.data() + offset, node["size"] ? node["size"].as<size_t>() : manualSize.value_or(buffer.size() - offset) }
};
}
+1 -1
View File
@@ -26,7 +26,7 @@ struct DecompressedData {
class Decompressor {
public:
static DataChunk* Decode(const std::vector<uint8_t>& buffer, uint32_t offset, CompressionType type);
static DecompressedData AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer);
static DecompressedData AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer, std::optional<size_t> size = std::nullopt);
static uint32_t TranslateAddr(uint32_t addr);
static bool IsSegmented(uint32_t addr);
static bool IsCompressed(YAML::Node& node);