{"id":15653,"library":"jpeg-lossless-decoder-js","title":"JPEG Lossless Decoder for JavaScript","description":"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.","status":"active","version":"2.1.2","language":"javascript","source_language":"en","source_url":"https://github.com/rii-mango/JPEGLosslessDecoderJS","tags":["javascript","JavaScript","JPEG","Lossless","Decoder","DICOM","1.2.840.10008.1.2.4.57","1.2.840.10008.1.2.4.70","typescript"],"install":[{"cmd":"npm install jpeg-lossless-decoder-js","lang":"bash","label":"npm"},{"cmd":"yarn add jpeg-lossless-decoder-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add jpeg-lossless-decoder-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary class, Decoder, is a named export. Default import is incorrect.","wrong":"import Decoder from 'jpeg-lossless-decoder-js';","symbol":"Decoder","correct":"import { Decoder } from 'jpeg-lossless-decoder-js';"},{"note":"For CommonJS, use destructuring as Decoder is a named export. Direct require() without destructuring would yield the module object.","wrong":"const Decoder = require('jpeg-lossless-decoder-js');","symbol":"Decoder (CommonJS)","correct":"const { Decoder } = require('jpeg-lossless-decoder-js');"},{"note":"When included directly via a script tag (e.g., lossless.js from the /release folder), the library exposes a global `jpeg` object. This pattern is primarily for browser environments without module bundlers.","symbol":"Global 'jpeg' object","correct":"var decoder = new jpeg.lossless.Decoder();"}],"quickstart":{"code":"import { Decoder } from 'jpeg-lossless-decoder-js';\n\n// A minimal, invalid JPEG Lossless header for demonstration purposes.\n// In a real application, this buffer would come from a DICOM dataset.\n// This is NOT a valid JPEG stream, but demonstrates API usage.\nconst dummyJpegLosslessBuffer = new Uint8Array([\n  0xFF, 0xD8, // SOI (Start of Image)\n  0xFF, 0xC3, // SOF_0 (Start Of Frame, Lossless, Huffman)\n  0x00, 0x11, // Length\n  0x08,       // Sample Precision (8 bits)\n  0x00, 0x01, // Number of Lines (1)\n  0x00, 0x01, // Samples per Line (1)\n  0x01,       // Number of Components (1)\n  0x01,       // Component ID (1)\n  0x11,       // Horizontal/Vertical Sampling Factor (1x1)\n  0x00,       // Quantization Table Selector (unused for lossless)\n  0xFF, 0xDA, // SOS (Start Of Scan)\n  0x00, 0x08, // Length\n  0x01,       // Number of components in scan\n  0x01,       // Component selector\n  0x00,       // DC entropy coding table selector\n  0x00, 0x00, // EOI (End of Image) - placeholder, not actual data\n  0xFF, 0xD9  // EOI (End Of Image)\n]).buffer;\n\nconst decoder = new Decoder();\n\ntry {\n  // Attempt to decompress the (dummy) buffer.\n  // In a real scenario, this would be a valid JPEG Lossless byte stream.\n  const outputBuffer = decoder.decompress(dummyJpegLosslessBuffer);\n  console.log('Successfully initialized Decoder and attempted decompression.');\n  console.log('Output buffer size:', outputBuffer.byteLength, 'bytes');\n} catch (error) {\n  console.error('Error during decompression (expected for dummy buffer):', error.message);\n  console.log('Ensure you provide a valid JPEG Lossless ArrayBuffer for actual decoding.');\n}\n\n// Example with offset and length (if only a part of a larger buffer is the JPEG data)\nconst largeBuffer = new ArrayBuffer(dummyJpegLosslessBuffer.byteLength + 100);\nnew Uint8Array(largeBuffer, 50, dummyJpegLosslessBuffer.byteLength).set(new Uint8Array(dummyJpegLosslessBuffer));\n\ntry {\n  const outputWithOffset = decoder.decompress(largeBuffer, 50, dummyJpegLosslessBuffer.byteLength);\n  console.log('Decompression with offset/length simulated.');\n  console.log('Output with offset buffer size:', outputWithOffset.byteLength, 'bytes');\n} catch (error) {\n  console.error('Error during offset/length decompression (expected for dummy buffer):', error.message);\n}","lang":"typescript","description":"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."},"warnings":[{"fix":"Ensure you are using v2.0.6 or later if you rely on the global `jpeg.lossless.Decoder` object in browser environments without module bundlers. For modern development, use ESM/CommonJS imports.","message":"Version 2.0.5 temporarily removed the global `jpeg` object, which was re-added in v2.0.6. Users relying on the global variable for browser usage directly via script tags might have experienced issues if they updated to v2.0.5 and then reverted or updated to v2.0.6.","severity":"breaking","affected_versions":"2.0.5"},{"fix":"Verify that your import statements correctly use named imports (`import { Decoder } from 'jpeg-lossless-decoder-js';`) and that your TypeScript compilation is set up to handle ESM imports correctly.","message":"The library was modernized to TypeScript in v2.1.0. While this provides type safety, users migrating from older JavaScript versions might need to adjust their import statements or build configurations. The API surface itself remained largely consistent.","severity":"gotcha","affected_versions":">=2.1.0"},{"fix":"Always ensure the input `buffer` passed to `decompress` contains a valid JPEG Lossless byte stream conforming to the supported DICOM transfer syntaxes.","message":"The `decompress` method expects a `Uint8Array` or `ArrayBuffer` containing the JPEG Lossless byte stream. It's crucial that this data is a valid JPEG Lossless stream (specifically DICOM compliant) for successful decoding. Passing in generic or incorrect JPEG data will lead to errors.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For browser script tag usage, ensure `lossless.js` (or `lossless-min.js`) is included before your script. For module-based environments, use `import { Decoder } from 'jpeg-lossless-decoder-js';` or `const { Decoder } = require('jpeg-lossless-decoder-js');`.","cause":"Attempting to use `jpeg.lossless.Decoder` in a browser environment without including the `lossless.js` script or in a Node/bundled environment without proper ESM/CommonJS import.","error":"ReferenceError: jpeg is not defined"},{"fix":"In CommonJS, use `const { Decoder } = require('jpeg-lossless-decoder-js');`. In ESM, use `import { Decoder } from 'jpeg-lossless-decoder-js';`.","cause":"This typically occurs in CommonJS environments when attempting to `require('jpeg-lossless-decoder-js')` and then directly calling it as a constructor, or when mixing default/named import syntax incorrectly.","error":"TypeError: (0 , jpeg_lossless_decoder_js_1.Decoder) is not a constructor"},{"fix":"Verify that the input `ArrayBuffer` or `Uint8Array` contains a JPEG Lossless byte stream, specifically one conforming to DICOM transfer syntaxes 1.2.840.10008.1.2.4.57 or 1.2.840.10008.1.2.4.70.","cause":"The input buffer provided to `decompress` is a standard JPEG (e.g., Baseline DCT), not a JPEG Lossless stream (which uses SOF marker 0xC3 for Huffman lossless, or 0xC7 for progressive lossless, not 0xC0).","error":"Error: JPEG not lossless, SOF: 0xc0"}],"ecosystem":"npm"}