On this page

image

Functions for creating image objects.

Functions

image.load(buffer: string, options?: Record<string | number, unknown>): Record<string | number, unknown> | nil

Load image (PNG or JPEG) from buffer.

// 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 = image.load(response.response);
  const tx = gui.new_texture("image_node", img.width, img.height, img.type, img.buffer);
});

Parameters

  • buffer: string — image data buffer
  • options?: Record<string | number, unknown> — 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: Record<string | number, unknown> | nil — 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

image.load_buffer(buffer: string, options?: Record<string | number, unknown>): Record<string | number, unknown> | nil

Load image (PNG or JPEG) from a string 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 = image.load_buffer(response.response, { flip_vertically: true });
  const tparams = {
    width: img.width,
    height: img.height,
    type: graphics.TEXTURE_TYPE_2D,
    format: graphics.TEXTURE_FORMAT_RGBA,
  };

  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);
});

Parameters

  • buffer: string — image data buffer
  • options?: Record<string | number, unknown> — 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: Record<string | number, unknown> | nil — 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.

image.get_astc_header(buffer: string): Record<string | number, unknown> | nil

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");
const header = image.get_astc_header(s);
pprint(s);

Parameters

  • buffer: string — .astc file data buffer

Returns

  • table: Record<string | number, unknown> | nil — header or nil if buffer is not a valid .astc. The header has these fields:

  • number width: image width

  • number height: image height

  • number depth: image depth

  • number block_size_x: block size x

  • number block_size_y: block size y

  • number block_size_z: block size z

Constants

image.TYPE_RGB

RGB image type

image.TYPE_RGBA

RGBA image type

image.TYPE_LUMINANCE

luminance image type

image.TYPE_LUMINANCE_ALPHA

luminance image type