Not really sure how useful this is but I thought I’d share it anyway. Defold PNG is a Defold native extension that allows you to inspect, load and save PNG files. Usage:
-- load my.png from disk
local f = io.open("my.png", "rb")
local bytes = f:read("*a")
-- read the PNG header
local info = png.info(bytes)
print(info.width)
print(info.height)
print(info.colortype) -- png.RGB, png.RGBA, png.GREY, png.GREY_ALPHA or png.PALETTE
print(info.bitdepth)
-- decode the PNG to a Defold buffer in RGB(A) format
local buf, w, h = png.decode_rgb(bytes)
local buf, w, h = png.decode_rgba(bytes)
-- use buffer as a sprite texture
local resource_path = go.get("#sprite", "texture0")
local header = { width = w, height = h, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGBA, num_mip_maps = 1 }
resource.set_texture(resource_path, header, buf)
-- create 100x100 raw pixels of random colors
local w, h = 100, 100
local pixels = ""
for i=1,w*h do
pixels = pixels .. string.char(math.random(1,255), math.random(1,255), math.random(1,255), 255)
end
--- encode raw RGB(A) pixels into a PNG
local bytes = png.encode_rgb(pixels, w, h)
local bytes = png.encode_rgba(pixels, w, h)
-- write bytes to foo.png
local f = io.open("foo.png", "wb")
f:write(bytes)
f:flush()
f:close()