Registry / serialization / dicom-parser

dicom-parser

JSON →
library1.2.3jsnpmunverified

dicom-parser is a JavaScript library designed for parsing DICOM Part 10 byte streams, as well as raw (non-Part 10) DICOM data. It is compatible with modern web browsers (IE10+), Node.js environments, and Meteor applications. The current stable version is 1.8.21, with releases primarily focusing on bug fixes and minor improvements, maintaining an active development cadence. A key differentiator is its lightweight nature and minimal external dependencies, making it efficient for integration into various projects. While it generally has no required external dependencies, users needing to support the Deflated Explicit VR Little Endian transfer syntax must explicitly install `pako`. It forms a foundational component within the wider CornerstoneJS ecosystem, often used by libraries like `cornerstoneWADOImageLoader` for extracting pixel data.

npm install dicom-parser
INSTALL
IMPORT
SIG · DICOM-PARSER
D
dicom-parser
serializationjavascriptv1.2.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

dicomParser
import dicomParser from 'dicom-parser'
import { parseDicom } from 'dicom-parser'
The library primarily exports a default object containing all parsing functionalities, including `parseDicom`.
dicomParser (CommonJS)
const dicomParser = require('dicom-parser')
const { parseDicom } = require('dicom-parser')
Standard CommonJS import for Node.js environments. The `parseDicom` function is a method of the `dicomParser` object.
DataSet (Type)
import type { DataSet } from 'dicom-parser'
Used for type-checking when working with TypeScript. The `DataSet` type represents the object returned by `parseDicom`.

Demonstrates how to initialize a byte array (mocking DICOM data) and use `dicomParser.parseDicom` to extract elements like Study Instance UID and Pixel Data, including options for raw DICOM parsing.

// In a real application, `byteArray` would come from a DICOM P10 byte stream // (e.g., XMLHttpRequest, file read, or network stream). // This is a minimal example to demonstrate parsing structure. const bufferSize = 256; // Dummy size for example const arrayBuffer = new ArrayBuffer(bufferSize); const byteArray = new Uint8Array(arrayBuffer); // For a valid DICOM file, you'd populate byteArray with the actual data. // For demonstration, let's pretend it has some minimal structure. // NOTE: This dummy byteArray will likely *not* produce a valid DICOM dataset. // It serves to show the API usage. try { // Allows parsing raw DICOM (not Part 10 encapsulated) by specifying TransferSyntaxUID. // For disk files, '1.2.840.10008.1.2' (Little Endian Implicit) is a common default. const options = { TransferSyntaxUID: '1.2.840.10008.1.2' }; // Parse the byte array to get a DataSet object containing the parsed contents. const dataSet = dicomParser.parseDicom(byteArray, options); // Access a string element (e.g., Study Instance UID) // In a real DICOM, 'x0020000d' would be the tag for StudyInstanceUID. // This will likely be undefined in our dummy example. const studyInstanceUid = dataSet.string('x0020000d'); console.log('Study Instance UID:', studyInstanceUid); // Get the pixel data element (contains the offset and length of the data). // Tag 'x7fe00010' is typically for Pixel Data. const pixelDataElement = dataSet.elements.x7fe00010; console.log('Pixel Data Element:', pixelDataElement); // If pixelDataElement exists and has data, create a typed array. // This example assumes 16-bit unsigned data, but actual type varies by DICOM. if (pixelDataElement && pixelDataElement.length > 0) { const pixelData = new Uint16Array( dataSet.byteArray.buffer, pixelDataElement.dataOffset, pixelDataElement.length / 2 ); console.log('First few pixel data values:', pixelData.slice(0, 10)); } else { console.log('No pixel data element found or it is empty.'); } } catch (ex) { console.error('Error parsing byte stream:', ex.message); }
Debug
Known issues
gotchaTo parse DICOM files compressed with the Deflated Explicit VR Little Endian transfer syntax, the 'pako' library must be manually installed as a separate dependency. It is not bundled or automatically installed by dicom-parser.
fix
Install pako: `npm install pako` or `yarn add pako`.
affects: >=1.0.0
gotchaWhen parsing raw DICOM byte streams (i.e., not DICOM Part 10 encapsulated files), you must provide the 'TransferSyntaxUID' option to 'parseDicom'. Failure to do so may result in incorrect parsing or errors.
fix
Pass an options object to dicomParser.parseDicom with the appropriate TransferSyntaxUID, e.g., `{ TransferSyntaxUID: '1.2.840.10008.1.2' }` for Little Endian Implicit.
affects: >=1.8.12
gotchaDICOM elements are accessed using their hexadecimal tag representations (e.g., 'x0020000d' for Study Instance UID) as strings, not their friendly names. Ensure correct hexadecimal formatting, prefixed with 'x'.
fix
Refer to the DICOM standard or a data dictionary to find the correct hexadecimal tag for the desired element. Always use the 'x' prefix, e.g., `dataSet.string('x0020000d')`.
affects: >=1.0.0
Errors
Common errors & fixes
Error parsing byte stream: Buffer too small to parse element tag
Attempting to parse an incomplete or malformed DICOM byte stream.
fix
Ensure the input `byteArray` contains a complete and valid DICOM Part 10 file or raw DICOM data. Verify the integrity of the byte stream from its source.
Cannot read properties of undefined (reading 'string')
Attempting to access a DICOM element (e.g., `dataSet.string('x0020000d')`) that does not exist or was not successfully parsed in the `DataSet` object.
fix
Before accessing, check if the DICOM tag exists in `dataSet.elements` or if `dataSet.string()` or `dataSet.uint16()` (etc.) returns a defined value. Ensure the DICOM file contains the expected element.
Upgrade
Version history
1.2.3latest on npm
Audit
Dependencies
pakooptionalRequired for supporting the Deflated Explicit VR Little Endian transfer syntax for DICOM compression.
Agent activity
8 hits · last 30 days
node
8
Resources
dicom-parser — npm install dicom-parser · libregistry