Skip to content

API Reference

Core Functions

encodeToKTX2

The main function for converting images to KTX2 format.

typescript
async function encodeToKTX2(
  imageBuffer: Uint8Array, 
  options?: Partial<IEncodeOptions>
): Promise<Uint8Array>

Parameters

  • imageBuffer: Raw image data as Uint8Array
  • options: Optional configuration object (see IEncodeOptions below)

Example

typescript
import { encodeToKTX2 } from 'ktx2-encoder';

const ktx2Data = await encodeToKTX2(imageBuffer, {
  isUASTC: true,
  generateMipmap: true,
  isNormalMap: false
});

encodeKTX2Cube

Function for converting cubemap images to KTX2 format.

typescript
async function encodeKTX2Cube(
  imageBuffers: Uint8Array[], 
  options?: Partial<IEncodeOptions>
): Promise<Uint8Array>

Parameters

  • imageBuffers: Array of 6 image buffers for each cubemap face
  • options: Same options as encodeToKTX2

Example

typescript
const ktx2Data = await encodeKTX2Cube([
  posx, negx, posy, negy, posz, negz
], {
  isUASTC: true,
  generateMipmap: true
});

Configuration Options

IEncodeOptions Interface

Complete configuration options for the encoder:

OptionTypeDefaultDescription
isUASTCbooleantrueUse UASTC texture format instead of ETC1S
enableDebugbooleanfalseEnable debug output
isYFlipbooleanfalseIf true, the source images will be Y flipped before compression
qualityLevelnumber-1(unused)Sets the ETC1S encoder's quality level (1-255). Controls file size vs. quality tradeoff
compressionLevelnumber2Controls encoder performance vs. file size tradeoff for ETC1S files (0-6)
needSupercompressionbooleanfalseUse UASTC Zstandard supercompression
isNormalMapbooleanfalseOptimize compression parameters for normal maps
isSetKTX2SRGBTransferFuncbooleanfalseInput source is sRGB. Should match the "perceptual" setting
generateMipmapbooleanfalseGenerate mipmaps from source images
isKTX2FilebooleanfalseCreate .KTX2 files instead of .basis files
kvDataRecord<string, string | Uint8Array>{}Custom key-value metadata for the KTX2 file
typeSourceType-Type of input source (RAW = 0, PNG = 1)
imageDecoderFunctionundefinedFunction to decode compressed image buffer to RGBA (Required for Node.js)
jsUrlstringundefinedURL to the JavaScript module
wasmUrlstringundefinedURL to the WebAssembly module

Image Decoder Function Type

For Node.js usage, the imageDecoder function should match this signature:

typescript
type ImageDecoder = (
  buffer: Uint8Array
) => Promise<{
  width: number;
  height: number;
  data: Uint8Array;
}>;

Source Type Enum

typescript
enum SourceType {
  RAW = 0,
  PNG = 1
}

Examples

Basic Usage

typescript
const ktx2Data = await encodeToKTX2(imageBuffer, {
  isUASTC: true,
  generateMipmap: true,
  isNormalMap: false
});

Normal Map Compression

typescript
const normalMapData = await encodeToKTX2(normalMapBuffer, {
  isUASTC: true,  // Recommended for normal maps
  isNormalMap: true,
  isSetKTX2SRGBTransferFunc: false  // Important for normal maps
});

ETC1S with Quality Settings

typescript
const etc1sData = await encodeToKTX2(imageBuffer, {
  isUASTC: false,
  qualityLevel: 128,
  compressionLevel: 2
});

With Custom Metadata

typescript
const ktx2Data = await encodeToKTX2(imageBuffer, {
  kvData: {
    'myKey': 'myValue',
    'customData': new Uint8Array([1, 2, 3])
  }
});