SDK JS
    Preparing search index...

    Browser/Node JPEG XL decoder over the vendored pure-Rust turbojxl wasm (no emscripten). Decodes a JPEG XL byte stream to RGBA8 pixels.

    Measured against the ISO/IEC 18181-3 conformance corpus, turbojxl decodes VarDCT and modular streams, greyscale, 8- to 32-bit samples, alpha (premultiplied and straight), patches, splines, noise, progressive, upsampling, JPEG-recompressed streams, and the first frame of an animation. It is not complete: streams built from multi-layer / blend-mode frame compositing, or carrying non-colour extra channels (CMYK Black, spot colour), reject (see decode). The half of that matrix that fits in the repo is asserted in jxl-conformance.test.ts. By default decode is single-threaded and runs everywhere (browsers + Node/Bun); pass JxlDecoderOptions.threads to opt into multithreaded decode on a cross-origin-isolated page. The wasm is initialized lazily on the first decode call and reused across decoders that share the same mode.

    import { JxlDecoder } from '@bitruvius/turbo-jxl';

    const bytes = new Uint8Array(await (await fetch('/image.jxl')).arrayBuffer());
    const img = await new JxlDecoder().decode(bytes);
    // img: { width, height, hasAlpha, premultipliedAlpha, rgba: Uint8Array }
    // Cross-origin / CDN: serve the .wasm from a base URL once, globally.
    import { configure } from '@bitruvius/foundation';
    import { JxlDecoder } from '@bitruvius/turbo-jxl';

    configure({ wasmBaseUrl: 'https://cdn.example.com/bitruvius/wasm/' });
    const img = await new JxlDecoder().decode(bytes);
    Index

    Constructors

    Methods

    Constructors

    Methods

    • Decode one complete JPEG XL byte stream to RGBA8 pixels.

      Parameters

      • bytes: Uint8Array

        The whole JPEG XL file as raw bytes (a bare codestream or a .jxl container). Element count equals byte count.

      Returns Promise<JxlImageData>

      The decoded JxlImageData: RGBA8 pixels (width * height * 4 bytes) plus width/height in pixels and the alpha metadata.

      If bytes is not a valid JPEG XL stream (corrupt or wrong format).

      If the stream uses a feature the vendored decoder does not support yet — multi-layer / blend-mode frame compositing, or non-colour extra channels (CMYK Black, spot colour). These surface as a wasm RuntimeError rather than a typed error; the decoder stays usable afterwards.

      If the decoder wasm fails to initialize — e.g. invalid wasmInit.wasmBytes/wasmUrl, or a non-OK fetch of the sibling .wasm.

      const img = await new JxlDecoder().decode(bytes);
      const data = new ImageData(new Uint8ClampedArray(img.rgba), img.width, img.height);
      ctx.putImageData(data, 0, 0);