CharLS WebAssembly

April 3, 2026 · View on GitHub

JPEG-LS Logo Wasm Logo

CharLS WebAssembly

npm version License REUSE status Build and test Deploy

CharLS WebAssembly is a WebAssembly build of CharLS. This project was originally forked from charls-js, created by Chris Hafey. It has been updated to use Wasm 3.0 and the latest version of CharLS. Supported environments are browsers and Node.js. See the Support Matrix for details.

Try It Out!

Try it in your browser here

Supported platforms

All environments that can run Wasm 3.0 modules are supported. Building charls-wasm itself is only supported on Linux platforms.

Support Matrix

DimensionSupported VersionScope
CharLS>= 3.0.0Development
CMake>= 3.22Development
Emscripten>= 5.0.4Development
Chrome/Edge>= 137Usage
Firefox>= 131Usage
Safari>= 26.2Usage
Node.js>= 18.0Usage

Install

Install this in your JavaScript project using npm:

npm install @team-charls/charls-wasm

Usage

Initialization

Before using this library, create the codec instances needed:

// For optimal performance, re-use the codec instances.
import { createJpegLSDecoder, createJpegLSEncoder } from '@team-charls/charls-wasm'
const decoder = await createJpegLSDecoder()
const encoder = await createJpegLSEncoder()

Decoding

To decode a JPEG-LS image, use a pre-allocated or create a local decoder instance. Use the single method decode or readHeader+decodeToBuffer. The readHeader method allows more control and provides more feedback to decide to decode or abort when reading untrusted images.

function decode (jpeglsEncodedBuffer) {
  // Decode it
  const decodedPixelBuffer = decoder.decode(jpeglsEncodedBuffer)

  // Get information about the decoded image
  const frameInfo = decoder.getFrameInfo()
  const interleaveMode = decoder.getInterleaveMode()
  const nearLossless = decoder.getNearLossless()

  // Do something with the decoded pixels here (e.g. display them)
  // The pixel arrangement for color images varies depending upon the
  // interleaveMode parameter, see documentation in JpegLSDecoder::getInterleaveMode()
}

// Example usage:
// const jpeglsEncodedBuffer = ... // read from file, load from URL, etc
// decode(jpeglsEncodedBuffer)

Encoding

To encode a file pass the buffer and the information about the image to the encoder.

function encode (pixelBuffer, frameInfo) {
  const jpeglsEncodedBuffer = encoder.encode(
    pixelBuffer,
    frameInfo.width,
    frameInfo.height,
    frameInfo.bitsPerSample,
    frameInfo.componentCount,
    0, // Interleave mode (0 = none, 1 = line, 2 = sample)
    0, // Encoding options bitmask (0=none, 1=evenSize, 2=includeVersion, 4=includePCParametersJAI)
    0 // NEAR parameter (0 = lossless, >0 = near-lossless)
  )

  // Do something with the encoded data: save it to a file, decode it, etc.
  return jpeglsEncodedBuffer
}

// Example usage:
// encode(pixelBuffer, frameInfo)

Disposing

For complex scenarios (long running applications) the method dispose can be called to release allocated WASM memory.

// To explicitly release memory call dispose on the codec objects.
// When the Node.js application ends or the browser page is closed this
// is normally done automatically.
decoder.dispose()
encoder.dispose()

See examples for browsers and nodejs. Also read the API documentation for JpegLSDecoder.js and JpegLSEncoder.js

Building

See information about building here

Design

Read about the design considerations that went into this library here

Performance

Read about the encode/decode performance of this library with NodeJS 14, Google Chrome and FireFox vs Native here