Skip to Content
defold-typescript
Get startedGuidesAPILibraries
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 }

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, or nil if 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.

  1. (buffer: string)

  2. (buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean })

    Parameters

    • buffer: string — image data buffer
    • options: { premultiply_alpha?: boolean; flip_vertically?: boolean } — An optional table containing parameters for loading the image. Supported entries: premultiply_alpha boolean True if alpha should be premultiplied into the color components. Defaults to false. flip_vertically boolean True if the image contents should be flipped vertically. Defaults to false.

    Returns

    • image: { width: number; height: number; type: Opaque<"constant">; buffer: string } | undefined — object or nil if loading fails. The object is a table with the following fields:
      • number width: image width
      • number height: image height
      • constant type: image type
        • image.TYPE_RGB
        • image.TYPE_RGBA
        • image.TYPE_LUMINANCE
        • image.TYPE_LUMINANCE_ALPHA
      • string buffer: the raw image data
// 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.

  1. (buffer: string)

  2. (buffer: string, options: { premultiply_alpha?: boolean; flip_vertically?: boolean })

    Parameters

    • buffer: string — image data buffer
    • options: { premultiply_alpha?: boolean; flip_vertically?: boolean } — An optional table containing parameters for loading the image. Supported entries: premultiply_alpha boolean True if alpha should be premultiplied into the color components. Defaults to false. flip_vertically boolean True if the image contents should be flipped vertically. Defaults to false.

    Returns

    • image: { width: number; height: number; type: Opaque<"constant">; buffer: Opaque<"buffer"> & { length: LuaLengthMethod<number> } } | undefined — object or nil if loading fails. The object is a table with the following fields:
      • number width: image width
      • number height: image height
      • constant type: image type
        • image.TYPE_RGB
        • image.TYPE_RGBA
        • image.TYPE_LUMINANCE
        • image.TYPE_LUMINANCE_ALPHA
      • buffer buffer: the script buffer that holds the decompressed image data. See buffer.create how to use the buffer.
// 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.

On this page