Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a script to dump SPR images as PNG #111

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.log
data.grf
*.extracted.grf/*
Exports
*.mp3
*.bin
*.exe
Expand Down
27 changes: 27 additions & 0 deletions Tools/RagnarokTools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions Tools/dump-spr-images.lua
Original file line number Diff line number Diff line change
@@ -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)
Loading