Defold native extension to load and save PNG images using lodepng.
You can use defold-png in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:
https://github.com/britzl/defold-png/archive/master.zip
Or point to the ZIP file of a specific release.
Decode a PNG into a Defold buffer containing the raw RGBA pixel data of the PNG. If premultiply_alpha
is true the alpha value of each pixel will be premultiplied into the RGB color values. This is needed when using the buffer to update a sprite texture or similar since the Defold engine uses premultiplied alpha internally. Once you have the buffer it's possible to get and manipulate the raw pixel data in Lua using buffer.get_stream()
or set it as a texture on a sprite:
-- load a png from disk
local f = io.open("my.png", "rb")
local bytes = f:read("*a")
-- decode the png to RGBA buffer
local buf, w, h = png.decode_rgba(bytes, true)
-- change the texture on a sprite to the loaded png
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)
Decode a PNG into a Defold buffer containing the raw RGB pixel data of the PNG. Once you have the buffer it's possible to get and manipulate the raw pixel data in Lua using buffer.get_stream()
or set it as a texture on a sprite:
-- load a png from disk
local f = io.open("my.png", "rb")
local bytes = f:read("*a")
-- decode the png to RGB buffer
local buf, w, h = png.decode_rgb(bytes)
-- change the texture on a sprite to the loaded png
local resource_path = go.get("#sprite", "texture0")
local header = { width = w, height = h, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGB, num_mip_maps = 1 }
resource.set_texture(resource_path, header, buf)
Read the PNG header and return information about the PNG.
-- load a png from disk
local f = io.open("my.png", "rb")
local bytes = f:read("*a")
local info = png.info(bytes)
print(info.width)
print(info.height)
print(info.colortype)
print(info.bitdepth)
Encode raw RGB pixels to a PNG with the same color type.
-- create 100x100 red pixels
local w = 100
local h = 100
local pixels = string.rep(string.char(255,0,0), w * h)
-- encode
local bytes = png.encode_rgb(pixels, w, h)
-- save png to file
local f = io.open("my.png", "wb")
f:write(bytes)
f:flush()
f:close()
Encode raw RGBA pixels to a PNG with the same color type.
-- create 100x100 red pixels with 50% transparency
local w = 100
local h = 100
local pixels = string.rep(string.char(255,0,0, 128), w * h)
-- encode
local bytes = png.encode_rgba(pixels, w, h)
-- save png to file
local f = io.open("my.png", "wb")
f:write(bytes)
f:flush()
f:close()
Red, green and blue: rgb/truecolor
Red, green, blue and alpha
Grayscale
Grayscale and alpha: level of opacity for each pixel
Indexed: channel containing indices into a palette of colors