NDWasmImage

Namespace

NDWasmImage

Description:
  • Provides WebAssembly-powered functions for image processing. This module allows for efficient conversion between image binary data and NDArrays.

Source:

Methods

(static) convertUint8ArrrayToDataurl(uint8array, mimeTypeopt) → {string}

Description:
  • Converts a Uint8Array of binary data into a Base64 Data URL. This is a utility function that runs purely in JavaScript.

Source:
Example
const pngBytes = NDWasmImage.encodePng(myNdarray);
const dataUrl = NDWasmImage.convertUint8ArrrayToDataurl(pngBytes, 'image/png');
// <img src={dataUrl} />
Parameters:
Name Type Attributes Default Description
uint8array Uint8Array

The byte array to convert.

mimeType string <optional>
'image/png'

The MIME type for the Data URL (e.g., 'image/jpeg').

Returns:

The complete Data URL string.

Type
string

(static) decode(imageBytes) → {NDArray|null}

Description:
  • Decodes a binary image into an NDArray. Supports common formats like PNG, JPEG, GIF, and WebP. The resulting NDArray will have a shape of [height, width, 4] and a 'uint8c' dtype, representing RGBA channels. This provides a consistent starting point for image manipulation.

Source:
Example
const imageBlob = await fetch('./my-image.png').then(res => res.blob());
const imageBytes = new Uint8Array(await imageBlob.arrayBuffer());
const imageArray = NDWasmImage.decode(imageBytes);
// imageArray.shape is [height, width, 4]
Parameters:
Name Type Description
imageBytes Uint8Array

The raw binary data of the image file.

Returns:

A 3D NDArray representing the image, or null if decoding fails.

Type
NDArray | null

(static) encode(ndarray, optionsopt) → {Uint8Array|null}

Description:
  • Encodes an NDArray into a binary image format (PNG or JPEG).

Source:
See:
Example
// Encode a 3-channel float array to a high-quality JPEG using the main function
const floatArr = NDArray.random([100, 150, 3]);
const jpegBytes = NDWasmImage.encode(floatArr, { format: 'jpeg', quality: 95 });
Parameters:
Name Type Attributes Default Description
ndarray NDArray

The input array. Supported dtypes: 'uint8', 'uint8c', 'float32', 'float64'. Float values should be in the range [0, 1]. Supported shapes: [h, w] (grayscale), [h, w, 1] (grayscale), [h, w, 3] (RGB), or [h, w, 4] (RGBA).

options object <optional>
{}

Encoding options.

Properties
Name Type Attributes Default Description
format string <optional>
'png'

The target format: 'png' or 'jpeg'. It is recommended to use the encodePng or encodeJpeg helpers instead.

quality number <optional>
90

The quality for JPEG encoding (1-100). Ignored for PNG.

Returns:

A Uint8Array containing the binary data of the encoded image, or null on failure.

Type
Uint8Array | null

(static) encodeJpeg(ndarray, optionsopt) → {Uint8Array|null}

Description:
  • Encodes an NDArray into a JPEG image. This is a helper function that calls encode with format: 'jpeg'.

Source:
Example
const floatArr = NDArray.random([20, 20, 3]);
const jpegBytes = NDWasmImage.encodeJpeg(floatArr, { quality: 85 });
Parameters:
Name Type Attributes Default Description
ndarray NDArray

The input array. See encode for supported shapes and dtypes.

options object <optional>
{}

Encoding options.

Properties
Name Type Attributes Default Description
quality number <optional>
90

The quality for JPEG encoding (1-100).

Returns:

A Uint8Array containing the binary data of the JPEG image.

Type
Uint8Array | null

(static) encodePng(ndarray) → {Uint8Array|null}

Description:
  • Encodes an NDArray into a PNG image. This is a helper function that calls encode with format: 'png'.

Source:
Example
const grayArr = ndarray.zeros([16, 16]);
const pngBytes = NDWasmImage.encodePng(grayArr);
Parameters:
Name Type Description
ndarray NDArray

The input array. See encode for supported shapes and dtypes.

Returns:

A Uint8Array containing the binary data of the PNG image.

Type
Uint8Array | null