@cornerstonejs/dicom-codec

中文文档
1.2.0发布于 2026/10/14ISCCommonJS构建来源已验证近 30 天下载 3930

DICOM codecs for JavaScript, with browser and Node.js support.

原始使用文档
文档目录10 节
  1. Next releases planning
  2. Future releases
  3. decode
  4. encode
  5. transcode
  6. getPixelData
  7. setConfig
  8. TransferSyntax Specific Encoding Options
  9. JPEG XL
  10. Testing

dicom-codec

DICOM codecs for JavaScript, with browser and Node.js support.

Features (v1.0.1)

codec nametransferSyntaxUID(s)decodeencodeexternal codecjs/wasm based
LittleEndian1.2.840.10008.1.2 1.2.840.10008.1.2.1 1.2.840.10008.1.2.1.99----
BigEndian1.2.840.10008.1.2.2----
LibjpegTurbo8Bit1.2.840.10008.1.2.4.50XXXX
LibjpegTurbo12Bit1.2.840.10008.1.2.4.51----
JpegLossless1.2.840.10008.1.2.4.57 1.2.840.10008.1.2.4.70X-X-
Jpegls1.2.840.10008.1.2.4.80 1.2.840.10008.1.2.4.81XXXX
Jpeg20001.2.840.10008.1.2.4.90 1.2.840.10008.1.2.4.91XXXX
JpegXL1.2.840.10008.1.2.4.110 1.2.840.10008.1.2.4.111 1.2.840.10008.1.2.4.112XX except .111XX
RleLossless1.2.840.10008.1.2.5X---
HTJ2K1.2.840.10008.1.2.202 (201,203 as well)XXXX

Next releases planning

v0.0.11: support for LibjpegTurbo12Bit v.0.0.12 support for encoding options. v1.0.0: support for browser (dynamic loading included) and node.

Future releases

  • RleLossless to be js/wasm based.
  • Support for the rest of operations for existing codecs.
  • Support for the rest of the DICOM transfer syntaxes.

Building

# Restore packages
pnpm install

# Build
pnpm run build

How to use

Commonjs

const dicomCodec = require('@cornerstonejs/dicom-codec');
....
const imageFrame = ....// see API
const imageInfo = {} // add here image information (see API)
const result = await dicomCodec.transcode(imageFrame, imageInfo, sourceTransferSyntaxUID, targetTransferSyntaxUID);
const pixelData = dicomCodec.getPixelData(result.imageFrame, result.imageInfo);

ES6

import dicomCodec from '@cornerstonejs/dicom-codec';
...
const imageFrame = ....// see API
const imageInfo = {} // add here image information (see API)
const result = await dicomCodec.transcode(imageFrame, imageInfo, sourceTransferSyntaxUID, targetTransferSyntaxUID);
const pixelData = dicomCodec.getPixelData(result.imageFrame, result.imageInfo);

API

decode

async function that decodes an image

Parameters (It does not mutate any param):

  • compressedImageFrame - TypedArray with the compressed image frame bytes.
  • imageInfo - Object
    • rows - Number with the image rows/height.
    • columns - Number with the image columns/width.
    • bitsAllocated - Number with bits per pixel sample.
    • samplesPerPixel - Number with number of components per pixel.
    • signed - Boolean true if pixel data is signed, false if unsigned.
  • sourceTransferSyntaxUID - String with the transfer syntax uid of the compressed image frame

Returns:

  • Object
    • imageFrame - TypedArray with the uncompressed image frame bytes (Mostly codecs returns Uint8Array, but Uint16Array, Int16Array can be seen)
    • imageInfo - Object
      • rows - Number with the image rows/height.
      • columns - Number with the image columns/width.
      • bitsAllocated - Number with bits per pixel sample.
      • samplesPerPixel - Number with number of components per pixel.
      • signed - Boolean true if pixel data is signed, false if unsigned.
      • there are also some other codec properties.

Decode does not occur if there is no codec for sourceTransferSyntaxUID or related codec's transferSyntaxUID refers to uncompressed.

encode

async function that encodes an image

Parameters (It does not mutate any param):

  • imageFrame - TypedArray with the uncompressed image frame bytes
  • imageInfo - Object
    • rows - Number with the image rows/height.
    • columns - Number with the image columns/width.
    • bitsAllocated - Number with bits per pixel sample.
    • samplesPerPixel - Number with number of components per pixel.
    • signed - Boolean true if pixel data is signed, false if unsigned.
  • targetTransferSyntaxUID - String with the transfer syntax uid to encode the image frame as
  • encodeOptions - Object - contents specific to each codec (see below) (v>=0.0.12)

Returns:

  • Object
    • imageFrame - TypedArray with the image frame bytes (Mostly codecs returns Uint8Array, but Uint16Array, Int16Array can be seen)
    • imageInfo - Object
      • rows - Number with the image rows/height.
      • columns - Number with the image columns/width.
      • bitsAllocated - Number with bits per pixel sample.
      • samplesPerPixel - Number with number of components per pixel.
      • signed - Boolean true if pixel data is signed, false if unsigned.
      • there are also some other codec properties.
    • processInfo - Object
      • duration - Number with process'duration in ms.

Encode does not occur if there is no codec for targetTransferSyntaxUID or related codec's transferSyntaxUID refers to uncompressed.

transcode

async function that transcodes an image (decodes and then encodes)

Parameters (It does not mutate any param):

  • imageFrame - TypedArray with the image frame bytes
  • imageInfo - Object
    • rows - Number with the image rows/height.
    • columns - Number with the image columns/width.
    • bitsAllocated - Number with bits per pixel sample.
    • samplesPerPixel - Number with number of components per pixel.
    • signed - Boolean true if pixel data is signed, false if unsigned.
  • sourceTransferSyntaxUID - String with the transfer syntax uid of the compressed.
  • targetTransferSyntaxUID - String with the transfer syntax uid to encode the image frame as.
  • encodeOptions - Object - contents specific to each codec (see below) (v>=0.0.12)

Returns:

  • Object
    • imageFrame - TypedArray with the uncompressed image frame bytes (Mostly codecs returns Uint8Array, but Uint16Array, Int16Array can be seen)
    • imageInfo - Object
      • rows - Number with the image rows/height.
      • columns - Number with the image columns/width.
      • bitsAllocated - Number with bits per pixel sample.
      • samplesPerPixel - Number with number of components per pixel.
      • signed - Boolean true if pixel data is signed, false if unsigned.
      • there are also some other codec properties.
    • processInfo - Object
      • duration - Number with process'duration in ms.

Transcode might not occur depending on transferSyntaxUID's params (see above).

getPixelData

function that returns formatted imageFrame based on imageInfo Some of codecs might have specific rules for pixelData based on imageInfo.

Parameters (It does not mutate any param):

  • imageFrame - TypedArray with the uncompressed image frame bytes
  • imageInfo - Object
    • rows - Number with the image rows/height.
    • columns - Number with the image columns/width.
    • bitsAllocated - Number with bits per pixel sample.
    • samplesPerPixel - Number with number of components per pixel.
    • signed - Boolean true if pixel data is signed, false if unsigned.

Returns:

  • Object
    • imageFrame - TypedArray with the image frame bytes (Mostly codecs returns Uint8Array, but Uint16Array, Int16Array can be seen). Each codec might treat this differently based on imageInfo.

setConfig

function that set up dicom-codec configuration properties Parameters (It does not mutate any param ):

  • options - Object
    • verbose - Boolean true if dicom-codec should be verbose, false otherwise.

TransferSyntax Specific Encoding Options

JPEG XL

Transfer syntax 1.2.840.10008.1.2.4.110 always uses lossless encoding. Transfer syntax 1.2.840.10008.1.2.4.111 supports decoding only because the pixel encoder does not create JPEG reconstruction data.

Transfer syntax 1.2.840.10008.1.2.4.112 accepts these encoding options:

  • lossless: Boolean, defaults to true.
  • distance: Butteraugli distance from 0 to 25 for lossy encoding. Defaults to 1.0 — cjxl's default, "visually lossless" — when lossless is false and no distance is given. Do not rely on libjxl's own default of 0: it reaches distance 0 through VarDCT rather than the modular path, which is larger than a real lossless encode and still not lossless.
  • effort: Integer from 1 to 9.
  • decodingSpeed: Integer from 0 to 4.

Signed pixel data

JPEG XL has no signed sample type — JxlDataType is unsigned integer or float — so Pixel Representation cannot be carried in the codestream. PS3.5 8.2.15 allows Pixel Representation to be 1 for monochrome JPEG XL images but defines no mechanism for it, so the mapping is a convention. This library uses a different one per transfer syntax, and applies it automatically:

Transfer syntaxSigned samples
.110 LosslessPassed through as two's complement. Byte-exact round trip; the bare codestream reads as unsigned and any reader that applies Pixel Representation itself agrees.
.112Level-shifted up by half the sample range before encoding and back after decoding.

.112 needs the shift because as unsigned, a CT frame's [-2048, 1704] becomes [0, 1704] plus [63488, 65535], and lossy coding smears across the gap: 2211 HU of maximum error at distance 1.0 without it. The shift is applied to every signed .112 frame, lossless option or not, so that decoding can invert it knowing only the transfer syntax.

Two consequences worth knowing:

  • A signed frame produces different bytes under .110 and .112. Both decode back to the same pixels here, but a .112 stream read by a decoder that assumes two's complement is off by 2^(BitsStored-1). Nothing in the standard distinguishes the conventions.
  • Butteraugli distance is relative to the full BitsAllocated range. CT occupies roughly 6% of a 16-bit range, so distance 1.0 still permits hundreds of HU of error. Use a small distance, or .110, when HU fidelity matters.

The same limitation applies to JPEG-LS, which also has no sign flag; this library only ever encodes JPEG-LS losslessly for that reason.

Testing

pnpm run test   # run vitest dispatch + integration tests

The integration tests require every underlying wasm codec's dist/ to be built locally; otherwise they skip cleanly. CI builds everything first.

版本与发布记录

@cornerstonejs/dicom-codec 共发布 31 个版本,首次发布于 2021/02/15;过去 12 个月发布了 9 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.2.0latest2026/10/1427 天前
1.1.62026/09/102 个月前
1.1.52026/09/092 个月前
1.1.42026/09/092 个月前
1.1.32026/09/092 个月前
1.1.22026/09/042 个月前

关于 @cornerstonejs/dicom-codec 的常见问题

@cornerstonejs/dicom-codec 是什么?

DICOM codecs for JavaScript, with browser and Node.js support.

如何安装 @cornerstonejs/dicom-codec?

在项目目录运行 npm install @cornerstonejs/dicom-codec,或使用 pnpm add / yarn add / bun add。

@cornerstonejs/dicom-codec 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cornerstonejs__dicom-codec 是否存在。

@cornerstonejs/dicom-codec 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@cornerstonejs/dicom-codec 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cornerstonejs/dicom-codec 可以商用吗?

@cornerstonejs/dicom-codec 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cornerstonejs/dicom-codec 的下载量有多少?

根据 npm 官方下载统计,@cornerstonejs/dicom-codec 在 2026/10/11 – 2026/11/09 这 30 天内被下载 3,930 次,日均约 131 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cornerstonejs/dicom-codec 还在维护吗?

最新版本 1.2.0 发布于 2026/10/14(27 天前);至今共发布 31 个版本,其中过去 12 个月发布了 9 个;首次发布于 2021/02/15。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。