Skip to content

Instantly share code, notes, and snippets.

@flamendless
Created July 31, 2019 14:38
Show Gist options
  • Save flamendless/6c3ba9b17f777c19728e7d97ebea67fc to your computer and use it in GitHub Desktop.
Save flamendless/6c3ba9b17f777c19728e7d97ebea67fc to your computer and use it in GitHub Desktop.
function test1()
local imgdata = love.image.newImageData("avatar.png")
local format = imgdata:getFormat()
local filename = "pack"
if love.filesystem.getInfo(filename) then
love.filesystem.remove(filename)
end
local file = love.filesystem.newFile(filename, "w")
file:write(love.data.pack("data", "<I4I4", imgdata:getDimensions()))
file:write(love.data.pack("data", "<s4", format))
file:write(imgdata)
local packdata = love.filesystem.newFileData(filename)
local w, h, index = love.data.unpack("<I4I4", packdata)
local str_format, index2 = love.data.unpack("<s4", packdata, 9)
local view_size = love.data.newDataView(packdata, index2 - 1, packdata:getSize() - index2 + 1)
print(view_size:getSize(), w * h * 4)
local imgdata2 = love.image.newImageData(w, h, str_format, view_size)
local img = love.graphics.newImage(imgdata)
local img2 = love.graphics.newImage(imgdata2)
end
local function pack(file, format, str)
assert(file:type() == "File", "arg1 must be a File")
assert(type(format) == "string", "arg2 must be a string")
assert(type(str) == "string", "arg3 must be a string")
local data = love.data.pack("data", format, str)
local res, err = file:write(data)
if not res then
error(err)
end
end
local function pack_table(file, t)
assert(file:type() == "File", "arg1 must be a File")
assert(type(t) == "table", "arg2 must be a table")
local n = 0
for i = 1, #t do
local format = t[i][1]
local str = t[i][2]
pack(file, format, str)
n = n + 1
end
return n
end
local function get_headers(packdata, count, format)
assert(packdata:type() == "FileData", "arg1 must be a FileData")
assert(type(count) == "number", "arg2 must be a number")
assert(type(format) == "string", "arg3 must be a string")
local t = {}
local prev
for i = 1, count do
local str, index = love.data.unpack("<s4", packdata, prev)
t[i] = str
prev = index
end
return t, prev
end
function test2()
local path = "avatar.png"
local data = love.filesystem.read("data", path)
local filename = "pack2"
if love.filesystem.getInfo(filename) then
love.filesystem.remove(filename)
end
local file = love.filesystem.newFile(filename, "w")
-- pack(file, "<s4", "brbl")
-- pack(file, "<s4", "png")
-- pack(file, "<s4", path)
local t = {
{ "<s4", "brbl" },
{ "<s4", "png" },
{ "<s4", path },
}
local n = pack_table(file, t)
file:write(data)
local packdata = love.filesystem.newFileData(filename)
local headers, last = get_headers(packdata, n or 3, "<s4")
print("Headers: " .. table.concat(headers, ", "))
print("Last Index: " .. last)
local view = love.data.newDataView(packdata, last - 1, packdata:getSize() - last + 1)
local imgdata = love.image.newImageData(view)
local w, h = imgdata:getDimensions()
local img = love.graphics.newImage(imgdata)
end
-- test1()
test2()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment