From 3d914ec2e52690fcf43b1def28a6e2c7eeb7a696 Mon Sep 17 00:00:00 2001 From: RDW Date: Sun, 13 Aug 2023 20:22:02 +0200 Subject: [PATCH] Tools: Add a script to dump SPR images as PNG No tests since it's a temporary measure. Ideally, it should generate proper spritesheets but I'd rather not reimplement that right now. --- .gitignore | 1 + Tools/RagnarokTools.lua | 27 +++++++++++++++++++++++++++ Tools/dump-spr-images.lua | 15 +++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 Tools/dump-spr-images.lua diff --git a/.gitignore b/.gitignore index 75dd84a6..73767d2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ *.log data.grf *.extracted.grf/* +Exports *.mp3 *.bin *.exe diff --git a/Tools/RagnarokTools.lua b/Tools/RagnarokTools.lua index 89b1b724..8f571c93 100644 --- a/Tools/RagnarokTools.lua +++ b/Tools/RagnarokTools.lua @@ -13,6 +13,7 @@ local table_insert = table.insert local RagnarokGND = require("Core.FileFormats.RagnarokGND") local RagnarokGRF = require("Core.FileFormats.RagnarokGRF") +local RagnarokSPR = require("Core.FileFormats.RagnarokSPR") function RagnarokTools:GenerateMapListFromGRF(grfFilePath) grfFilePath = grfFilePath or "data.grf" @@ -151,4 +152,30 @@ function RagnarokTools:ExportLightmapsFromGND(gndFileContents) stbi.bindings.stbi_flip_vertically_on_write(false) end +function RagnarokTools:ExportImagesFromSPR(sprFileContents, outputDirectory) + outputDirectory = outputDirectory or "Exports" + C_FileSystem.MakeDirectoryTree(outputDirectory) + + local spr = RagnarokSPR() + spr:DecodeFileContents(sprFileContents) + + local palette = spr:GetEmbeddedColorPalette(sprFileContents) + + for index, image in ipairs(spr.bmpImages) do + local indexedColorImageBytes = image.pixelBuffer + local rgbaImageBytes = RagnarokSPR:ApplyColorPalette(indexedColorImageBytes, palette) + local pngFileContents = C_ImageProcessing.EncodePNG(rgbaImageBytes, image.pixelWidth, image.pixelHeight) + + local pngFilePath = path.join(outputDirectory, format("bmp-frame-%d.png", index)) + C_FileSystem.WriteFile(pngFilePath, tostring(pngFileContents)) + end + + for index, image in ipairs(spr.tgaImages) do + local pngFileContents = C_ImageProcessing.EncodePNG(image.pixelBuffer, image.pixelWidth, image.pixelHeight) + + local pngFilePath = path.join(outputDirectory, format("tga-frame-%d.png", index)) + C_FileSystem.WriteFile(pngFilePath, tostring(pngFileContents)) + end +end + return RagnarokTools diff --git a/Tools/dump-spr-images.lua b/Tools/dump-spr-images.lua new file mode 100644 index 00000000..69e1208e --- /dev/null +++ b/Tools/dump-spr-images.lua @@ -0,0 +1,15 @@ +local RagnarokGRF = require("Core.FileFormats.RagnarokGRF") +local RagnarokTools = require("Tools.RagnarokTools") + +local sprFilePath = arg[1] or "data/sprite/몬스터/bakonawa.spr" +printf("Dumping SPR frames: %s", sprFilePath) + +local grfPath = "data.grf" +local grf = RagnarokGRF() +grf:Open(grfPath) + +local sprBytes = grf:ExtractFileInMemory(sprFilePath) + +grf:Close() + +RagnarokTools:ExportImagesFromSPR(sprBytes)