dcm4rs
dcm4rs is a pure-Rust library for working with DICOM files. It reads, modifies and writes DICOM data sets, and decodes and encodes Pixel Data, without unsafe code in the core and without a C toolchain unless you ask for one. Values are stored as the bytes the standard defines, so a file that is read and written back unmodified loses nothing.
Note that dcm4rs is a general-purpose framework concerned with reading and writing DICOM data.
It does not handle the specifics of individual SOP classes, and DICOM networking — associations
and the DIMSE services — belongs to a separate dcm4rs-net package, the way pydicom keeps
networking in pynetdicom. The DICOM JSON model,
media storage directories and waveform decoding are part of the library.
Work in progress. dcm4rs is under active development and is not ready for production use. APIs may change without notice, and several codecs and the network layer are still missing (see Status). Use it for experiments, prototypes and evaluation only.
Installation
[]
= "0.2"
Rust 1.75 or newer is required. The default pixel-jpeg feature decodes eight-bit and 12-bit
JPEG; the remaining codecs are opt-in:
| Feature | Default | Codec |
|---|---|---|
pixel-jpeg |
yes | 8-bit and 12-bit JPEG baseline, extended and progressive via zune-jpeg and oxiarc-jpeg |
pixel-jpegls |
no | JPEG-LS lossless and near-lossless via charls (needs CMake and a C++ toolchain) |
pixel-j2k |
no | JPEG 2000 / HTJ2K via jpeg2k with the pure-Rust openjp2 port |
pixel-jxl |
no | JPEG XL decoding via jxl-oxide, encoding via zune-jpegxl (needs Rust 1.80) |
JPEG Lossless (Process 14 and Selection Value 1) needs no feature: it is pure Rust in the core, next to RLE Lossless.
Documentation
The API reference is on docs.rs; every module documents the standard clauses it implements.
Data sets
use tags;
use ;
let mut ds = new;
ds.set_string;
ds.set_u16;
ds.set;
assert_eq!;
assert_eq!;
assert_eq!;
The VRs with internal structure are tedious to use as bytes, so the valuerep module parses them
on demand and, like pydicom, keeps the text each value was written as:
use tags;
let dataset = read_file?.dataset;
let name = dataset.person_name?; // PN: three groups of five components
let thickness = dataset.decimal_strings?; // DS: text + f64
let echo = dataset.integer_strings?; // IS: text + i32
let date = dataset.date?; // DA: year / month / day
let time = dataset.time?; // TM: hour / minute / second / fraction
let stamp = dataset.date_time?; // DT: partial values are allowed
Each accessor checks the VR and reports Error::WrongVr on a mismatch. DateTime keeps the UTC
offset (offset_minutes) out of Ord on purpose: a value with an offset names a different
instant from the same fields without one, so comparing them silently would hide that.
Reading and writing files
use tags;
use ;
let file = read_file.unwrap;
println!;
assert_eq!;
// Stored bytes are written back as they are, so the data set must match the syntax.
let bytes = file.write.unwrap;
Supported input: explicit VR little endian, explicit VR big endian (retired), implicit VR,
deflated, sequences with defined or undefined lengths and encapsulated pixel data. A malformed
encoding is an error by default; ReadOptions::lenient_undefined_length relaxes the one common
case, an undefined length on a VR that does not allow it, by scanning to the item delimiter the
way pydicom does. Repeated elements keep the first occurrence, where pydicom warns; the tags
that appeared more than once are listed by DataSet::duplicates. Writing is canonical: sequences
get defined lengths, group length elements are dropped and values are padded to even length, so
most sources round-trip semantically rather than byte for byte; the 128-byte preamble is kept as
read, and a file built with DicomFile::new starts with zeros. DataSet::read decodes a bare data
set without a file meta; DataSet::to_endian converts numeric values, including nested sequence
items, to another byte order.
Pixel Data
Compressed and uncompressed Pixel Data is available as the frames it is:
DicomFile::pixel_frames returns the encoded frames, PixelDescriptor describes them, and
decode_frame or decode_pixel_data produce samples.
use DicomFile;
let file = read_file.unwrap;
let descriptor = file.pixel_descriptor.unwrap;
let pixels = file.decode_frame.unwrap;
println!;
Decompressing
RLE Lossless, JPEG Lossless, 8-bit and 12-bit JPEG, JPEG-LS, JPEG 2000 / HTJ2K and JPEG XL are
decoded with 8, 12, 14, 15 or 16-bit samples. Frames follow the Basic Offset Table and may span
several fragments; an empty table falls back to Number of Frames and JPEG end-of-image
markers, as pydicom does. Codestream precision that differs from Bits Stored is accepted,
again matching pydicom.
MPEG video stays undecoded on purpose — as far as dcm4che goes, and no further. Frames come out
of encapsulated_frames as the compressed bitstreams they are, mpeg2_sequence_header reads the
MPEG-2 sequence header into Rows, Columns, Cine Rate, Frame Time and Pixel Aspect Ratio, and
asking for a pixel reports Error::UnsupportedPixelData.
Compressing
compress_rle, compress_jpeg(quality), compress_jpegls, compress_j2k and compress_jxl
replace the frames with encapsulated data and switch the transfer syntax. Baseline JPEG writes
8-bit samples and turns a colour frame into YBR_FULL_422; the others are lossless:
| Method | Transfer syntax |
|---|---|
compress_rle |
RLE Lossless |
compress_jpeg(q) |
JPEG Baseline (8-bit, YBR_FULL_422 for colour) |
compress_jpegls |
JPEG-LS Lossless |
compress_j2k |
JPEG 2000 Lossless |
compress_jxl |
JPEG XL Lossless |
Data dictionary
The PS3.6 dictionary is generated and committed, so building needs no network access: 5212
standard elements and 88 repeating groups. Regenerate it with the generator in cli:
See data/README.md for the source files.
Contributing
CI runs formatting and lint checks, runs the suites under cargo-llvm-cov and uploads the LCOV
report to Codecov (add a CODECOV_TOKEN repository secret; upload failures do not block CI).
The test modules mirror pydicom's, one file per module, so behaviour can be compared module by
module.
There is an optional conformance run over the
pylibjpeg-data corpus (about 70 MB, downloaded on
demand, never committed); lossless categories are compared byte-for-byte with the uncompressed
_matching_dataset. It skips when the corpus is absent:
The workspace:
Cargo.toml virtual workspace
dcm4rs/ library: Tag / Vr / Value / Element / DataSet, dictionary, file and pixel I/O
cli/ command-line tools; depends on dcm4rs, is not part of the library API
data/ PS3.6 DocBook source (not committed)
Status
Implemented: data model and dictionary, Part 10 read/write, character set conversion, DEFLATE,
private dictionary registration, private creator blocks, deferred reads, the DICOM JSON model
(PS3.18 F.2), modality and VOI LUTs, media storage directories (the DICOMDIR), waveform decoding,
typed views for the structured VRs (PN, DS, IS, DA, TM and DT), RLE Lossless, baseline
JPEG, JPEG-LS, JPEG 2000 and JPEG XL encoding, and RLE / JPEG Lossless / 8-bit and 12-bit JPEG /
JPEG-LS / JPEG 2000 / JPEG XL decoding.
Not implemented yet:
- Network services (association, C-FIND / C-MOVE / C-GET) as a separate
dcm4rs-netpackage.
Where dcm4rs deliberately differs from pydicom, the difference is pinned by a test:
| Area | dcm4rs |
|---|---|
| Part 10 preamble | kept as read and written back; a file built with DicomFile::new starts with 128 zero bytes |
| Group length elements | never written; (0002,0000) and (xxxx,0000) are dropped |
| Ambiguous VR | corrected on write from Pixel Representation, Bits Allocated and LUT context |
| Unknown escape sequence | Error::InvalidText instead of pydicom's warning and default encoding |
DataSet equality |
compares elements; byte order and inherited character set do not take part |
| Unknown/undefined-length non-SQ element | Error::InvalidData, except UN (read as SQ) and OB/OW (read as fragments); ReadOptions::lenient_undefined_length scans to the item delimiter instead |
JSON BulkDataURI |
Error::InvalidValue on read, because fetching it needs whatever hosts the payload |
| JSON encapsulated pixel data | Error::UnsupportedPixelData on write, because the fragments would come back as one opaque blob |
| Deferred reads | the tag stays out of the data set until load_element re-reads it, rather than holding a placeholder whose value fetches on access |
| DICOMDIR | the tree is rebuilt from the record flags, not from the byte offsets; writing recomputes the offsets from the record order |
License
MIT. See LICENSE.