JPEG Lossless Decoder for JavaScript

2.1.2 · active · verified Tue Apr 21

jpeg-lossless-decoder-js is a specialized JavaScript and TypeScript library designed to decode JPEG Lossless images, particularly those encountered within the DICOM standard. It specifically supports DICOM transfer syntaxes 1.2.840.10008.1.2.4.57 (JPEG Lossless, Nonhierarchical, Processes 14) and 1.2.840.10008.1.2.4.70 (JPEG Lossless, Nonhierarchical, Processes 14 Selection 1). The library is currently stable at version 2.1.2, with recent significant updates including modernization to TypeScript in version 2.1.0. While release cadence isn't strictly fixed, it receives updates for bug fixes and modernization. Its primary differentiation lies in its ability to handle these specific DICOM-related JPEG lossless formats, which are generally not supported by common JPEG decoders, making it a critical tool in medical imaging applications. It originated as a port from a Java implementation by Helmut Dersch.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and instantiate the `Decoder` class, and use its `decompress` method. It includes a minimal (but invalid) JPEG Lossless buffer to illustrate API usage, along with an example of using the optional `offset` and `length` parameters.

import { Decoder } from 'jpeg-lossless-decoder-js';

// A minimal, invalid JPEG Lossless header for demonstration purposes.
// In a real application, this buffer would come from a DICOM dataset.
// This is NOT a valid JPEG stream, but demonstrates API usage.
const dummyJpegLosslessBuffer = new Uint8Array([
  0xFF, 0xD8, // SOI (Start of Image)
  0xFF, 0xC3, // SOF_0 (Start Of Frame, Lossless, Huffman)
  0x00, 0x11, // Length
  0x08,       // Sample Precision (8 bits)
  0x00, 0x01, // Number of Lines (1)
  0x00, 0x01, // Samples per Line (1)
  0x01,       // Number of Components (1)
  0x01,       // Component ID (1)
  0x11,       // Horizontal/Vertical Sampling Factor (1x1)
  0x00,       // Quantization Table Selector (unused for lossless)
  0xFF, 0xDA, // SOS (Start Of Scan)
  0x00, 0x08, // Length
  0x01,       // Number of components in scan
  0x01,       // Component selector
  0x00,       // DC entropy coding table selector
  0x00, 0x00, // EOI (End of Image) - placeholder, not actual data
  0xFF, 0xD9  // EOI (End Of Image)
]).buffer;

const decoder = new Decoder();

try {
  // Attempt to decompress the (dummy) buffer.
  // In a real scenario, this would be a valid JPEG Lossless byte stream.
  const outputBuffer = decoder.decompress(dummyJpegLosslessBuffer);
  console.log('Successfully initialized Decoder and attempted decompression.');
  console.log('Output buffer size:', outputBuffer.byteLength, 'bytes');
} catch (error) {
  console.error('Error during decompression (expected for dummy buffer):', error.message);
  console.log('Ensure you provide a valid JPEG Lossless ArrayBuffer for actual decoding.');
}

// Example with offset and length (if only a part of a larger buffer is the JPEG data)
const largeBuffer = new ArrayBuffer(dummyJpegLosslessBuffer.byteLength + 100);
new Uint8Array(largeBuffer, 50, dummyJpegLosslessBuffer.byteLength).set(new Uint8Array(dummyJpegLosslessBuffer));

try {
  const outputWithOffset = decoder.decompress(largeBuffer, 50, dummyJpegLosslessBuffer.byteLength);
  console.log('Decompression with offset/length simulated.');
  console.log('Output with offset buffer size:', outputWithOffset.byteLength, 'bytes');
} catch (error) {
  console.error('Error during offset/length decompression (expected for dummy buffer):', error.message);
}

view raw JSON →