Skip to Content
defold-typescript libraries
britzl

britzl/defold-png/png

Encode raw pixels into PNG bytes and decode PNG bytes into a Defold buffer, powered by LodePNG.

  • GitHub: britzl/defold-png — pinned to 1.2.3
    1. Pick a release from britzl/defold-png releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as png from "png"

Functions

encode_rgb(pixels: string, width: number, height: number): string

Encodes raw RGB pixels (3 bytes per pixel) into PNG bytes.

Parameters

  • pixels: string — The raw pixel data, row by row.
  • width: number — Width of the image in pixels.
  • height: number — Height of the image in pixels.

Returns

  • string — The PNG bytes.

encode_rgba(pixels: string, width: number, height: number): string

Encodes raw RGBA pixels (4 bytes per pixel) into PNG bytes.

Parameters

  • pixels: string — The raw pixel data, row by row.
  • width: number — Width of the image in pixels.
  • height: number — Height of the image in pixels.

Returns

  • string — The PNG bytes.

decode_rgb(png: string, premultiply_alpha?: boolean): LuaMultiReturn<[ Opaque<"buffer"> & { length: LuaLengthMethod<number>; }, number, number ] | [ undefined, undefined, undefined ]>

Decodes a valid PNG into an RGB buffer with one uint8 pixels stream of three components per pixel. The decoded rows are flipped vertically to match Defold's buffer orientation. The extension ignores LodePNG's decode error, so a corrupt PNG may crash the engine instead of returning nils. Three nils mean the buffer could not be created or failed validation, so check the buffer before reading the dimensions.

Parameters

  • png: string — The PNG bytes, which must be a valid PNG.
  • premultiply_alpha?: boolean — Accepted for symmetry with {@link png.decode_rgba}; it only has effect in the RGBA path.

Returns

  • LuaMultiReturn<[ Opaque<"buffer"> & { length: LuaLengthMethod<number>; }, number, number ] | [ undefined, undefined, undefined ]> — The pixel buffer, its width and its height, or three nils when the buffer cannot be created or fails validation.

decode_rgba(png: string, premultiply_alpha?: boolean): LuaMultiReturn<[ Opaque<"buffer"> & { length: LuaLengthMethod<number>; }, number, number ] | [ undefined, undefined, undefined ]>

Decodes a valid PNG into an RGBA buffer with one uint8 pixels stream of four components per pixel. The decoded rows are flipped vertically to match Defold's buffer orientation. The extension ignores LodePNG's decode error, so a corrupt PNG may crash the engine instead of returning nils. Three nils mean the buffer could not be created or failed validation, so check the buffer before reading the dimensions.

Parameters

  • png: string — The PNG bytes, which must be a valid PNG.
  • premultiply_alpha?: boolean — Multiply each pixel's color components by its alpha while decoding.

Returns

  • LuaMultiReturn<[ Opaque<"buffer"> & { length: LuaLengthMethod<number>; }, number, number ] | [ undefined, undefined, undefined ]> — The pixel buffer, its width and its height, or three nils when the buffer cannot be created or fails validation.

info(png: string): { width: number; height: number; colortype: number; bitdepth: number; }

Reads the header of PNG bytes without decoding the pixels.

Parameters

  • png: string — The PNG bytes.

Returns

  • { width: number; height: number; colortype: number; bitdepth: number; } — The image's dimensions, its LodePNG color type ({@link png.RGBA}, {@link png.RGB}, {@link png.GREY}, {@link png.GREY_ALPHA} or {@link png.PALETTE}) and its bit depth.
    • width: number
    • height: number
    • colortype: number
    • bitdepth: number

Variables

RGBA: number

LodePNG color type: red, green, blue and alpha, as {@link png.info} reports it.

RGB: number

LodePNG color type: red, green and blue, as {@link png.info} reports it.

GREY: number

LodePNG color type: greyscale, as {@link png.info} reports it.

GREY_ALPHA: number

LodePNG color type: greyscale and alpha, as {@link png.info} reports it.

PALETTE: number

LodePNG color type: palette, as {@link png.info} reports it.

On this page