Defold
image
Functions for creating image objects.
Functions
image.get_astc_header(buffer: string): { width: number; height: number; depth: number; block_size_x: number; block_size_y: number; block_size_z: number }image.load(2 overloads): { width: number; height: number; type:Opaque<"constant">; buffer: string } | undefinedimage.load_buffer(2 overloads): { width: number; height: number; type:Opaque<"constant">; buffer:Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined
image.get_astc_header(buffer: string): { width: number; height: number; depth: number; block_size_x: number; block_size_y: number; block_size_z: number }
get the header of an .astc buffer
// How to get the block size and dimensions from a .astc file
const [s] = sys.load_resource("/assets/cat.astc");
if (s !== undefined) {
const header = image.get_astc_header(s);
pprint(s);
}local s = sys.load_resource("/assets/cat.astc")
local header = image.get_astc_header(s)
pprint(s)Parameters
buffer:string— .astc file data buffer
Returns
header:image.astc_header | nil— header, ornilif the buffer is not a valid ASTC image
image.load(2 overloads): { width: number; height: number; type: Opaque<"constant">; buffer: string } | undefined
- Signature changed in Defold 1.13.2
Load image (PNG or JPEG) from buffer.
(buffer: string)(buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean })Parameters
buffer:string— image data bufferoptions:{ premultiply_alpha?: boolean; flip_vertically?: boolean }— An optional table containing parameters for loading the image. Supported entries:premultiply_alphaboolean True if alpha should be premultiplied into the color components. Defaults tofalse.flip_verticallyboolean True if the image contents should be flipped vertically. Defaults tofalse.
Returns
image:{ width: number; height: number; type: Opaque<"constant">; buffer: string } | undefined— object ornilif loading fails. The object is a table with the following fields:- number
width: image width - number
height: image height - constant
type: image typeimage.TYPE_RGBimage.TYPE_RGBAimage.TYPE_LUMINANCEimage.TYPE_LUMINANCE_ALPHA
- string
buffer: the raw image data
- number
// How to load an image from an URL and create a GUI texture from it:
const imgurl = "http://www.site.com/image.png";
http.request(imgurl, "GET", (self, id, response) => {
const img = response.response !== undefined ? image.load(response.response) : undefined;
if (img !== undefined) {
const tx = gui.new_texture("image_node", img.width, img.height, img.type, img.buffer);
}
});local imgurl = "http://www.site.com/image.png"
http.request(imgurl, "GET", function(self, id, response)
local img = image.load(response.response)
local tx = gui.new_texture("image_node", img.width, img.height, img.type, img.buffer)
end)image.load_buffer(2 overloads): { width: number; height: number; type: Opaque<"constant">; buffer: Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined
- Signature changed in Defold 1.13.2
Load image (PNG or JPEG) from a string buffer.
(buffer: string)(buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean })Parameters
buffer:string— image data bufferoptions:{ premultiply_alpha?: boolean; flip_vertically?: boolean }— An optional table containing parameters for loading the image. Supported entries:premultiply_alphaboolean True if alpha should be premultiplied into the color components. Defaults tofalse.flip_verticallyboolean True if the image contents should be flipped vertically. Defaults tofalse.
Returns
image:{ width: number; height: number; type: Opaque<"constant">; buffer: Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined— object ornilif loading fails. The object is a table with the following fields:- number
width: image width - number
height: image height - constant
type: image typeimage.TYPE_RGBimage.TYPE_RGBAimage.TYPE_LUMINANCEimage.TYPE_LUMINANCE_ALPHA
- buffer
buffer: the script buffer that holds the decompressed image data. See buffer.create how to use the buffer.
- number
// Load an image from an URL as a buffer and create a texture resource from it:
const imgurl = "http://www.site.com/image.png";
http.request(imgurl, "GET", (self, id, response) => {
const img =
response.response !== undefined
? image.load_buffer(response.response, { flip_vertically: true })
: undefined;
// the engine registers a texture format only when the driver supports it
const format = graphics.TEXTURE_FORMAT_RGBA;
if (img !== undefined && format !== undefined) {
const tparams = {
width: img.width,
height: img.height,
type: graphics.TEXTURE_TYPE_2D,
format,
};
const my_texture_id = resource.create_texture("/my_custom_texture.texturec", tparams, img.buffer);
// Apply the texture to a model
go.set("/go1#model", "texture0", my_texture_id);
}
});local imgurl = "http://www.site.com/image.png"
http.request(imgurl, "GET", function(self, id, response)
local img = image.load_buffer(response.response, { flip_vertically = true })
local tparams = {
width = img.width,
height = img.height,
type = graphics.TEXTURE_TYPE_2D,
format = graphics.TEXTURE_FORMAT_RGBA }
local my_texture_id = resource.create_texture("/my_custom_texture.texturec", tparams, img.buffer)
-- Apply the texture to a model
go.set("/go1#model", "texture0", my_texture_id)
end)Constants
image.TYPE_LUMINANCE: string & { readonly __brand: "image.TYPE_LUMINANCE" }
Luminance image type.
image.TYPE_LUMINANCE_ALPHA: string & { readonly __brand: "image.TYPE_LUMINANCE_ALPHA" }
Luminance-alpha image type.
image.TYPE_RGB: string & { readonly __brand: "image.TYPE_RGB" }
RGB image type.
image.TYPE_RGBA: string & { readonly __brand: "image.TYPE_RGBA" }
RGBA image type.