image
Functions for creating image objects.
Functions
image.get_astc_header(buffer: string): image.AstcHeader
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.AstcHeader— header, ornilif the buffer is not a valid ASTC image
image.load(2 overloads): image.LoadResult | undefinedC
Load image (PNG or JPEG) from buffer.
(buffer: string)Signature changed in Defold 1.13.2 — before:
(buffer: string): { width: number; height: number; type: Opaque<"constant">; buffer: string } | undefined
(buffer: string, options: boolean | image.LoadOptions)Parameters
buffer:string— image data bufferoptions:boolean | image.LoadOptions— Optional loading parameters. A boolean is accepted for backwards compatibility and controlspremultiply_alpha.
Returns
image:image.LoadResult | undefined— loaded image, ornilif loading fails
Signature changed in Defold 1.13.2 — before:
(buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean }): { width: number; height: number; type: Opaque<"constant">; buffer: string } | undefined
// 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): image.LoadBufferResult | undefinedC
Load image (PNG or JPEG) from a string buffer.
(buffer: string)Signature changed in Defold 1.13.2 — before:
(buffer: string): { width: number; height: number; type: Opaque<"constant">; buffer: Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined
(buffer: string, options: boolean | image.LoadOptions)Parameters
buffer:string— image data bufferoptions:boolean | image.LoadOptions— Optional loading parameters. A boolean is accepted for backwards compatibility and controlspremultiply_alpha.
Returns
image:image.LoadBufferResult | undefined— loaded image, ornilif loading fails
Signature changed in Defold 1.13.2 — before:
(buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean }): { width: number; height: number; type: Opaque<"constant">; buffer: Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined
// 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.