dcm4rs 0.1.0

DICOM 数据模型与数据字典
Documentation
  • Coverage
  • 100%
    5301 out of 5301 items documented1 out of 12 items with examples
  • Size
  • Source code size: 1.5 MB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 29.0 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • xbmlz

dcm4rs

A foundational DICOM library for Rust. Phase one is complete: data model + data dictionary.

Layout

Cargo.toml  virtual workspace
dcm4rs/     library: data model (Tag / Vr / Value / Element / DataSet) + data dictionary
cli/        command-line package: depends on dcm4rs only, not part of the library's public API
data/       PS3.6 DocBook source (not committed)

With just two packages there is no crates/ layer: uv, ruff and rust-analyzer use crates/ because they have dozens of members; two packages read more directly in the flat layout used by clap / tokio / serde, and cargo run -p cli is just as short.

Dependencies flow one way: cli ──▶ dcm4rs; the library never depends on the command-line tool. Third parties depend only on the dcm4rs library, not on cli. New commands (validate, dump, transcode, ...) go into cli/src/bin/; the library itself ships no binary.

The naming follows the shape of pydicom / fo-dicom: one library package, with the dictionary as a module inside it (dcm4rs::dictionary), rather than splitting it into core / dict packages the way dcm4che does — Java can call a package dcm4che-core, but in Rust the name core clashes with the sysroot crate, so copying that split would only bring trouble. If the dictionary ever needs to be compiled separately, dcm4rs-dictionary can be split out then.

Design decisions

  1. Values are stored as raw bytes. Value::Bytes keeps the bytes that appear in the standard; interpreting numbers and text happens at read time. Data under any transfer syntax (explicit or implicit VR, big endian) can be written back byte for byte, and the parser never has to guess.
  2. Explicit endianness. Decoders such as Element::u16s(endian) require an Endian, and DataSet carries one set from the transfer syntax for convenience accessors such as ds.u16(tag). There is no hidden global state.
  3. Strict rather than lenient. A VR that does not match the decoding, a length that is not a multiple of the unit, or text that is not valid UTF-8 all produce explicit errors (Error::WrongVr / WrongLength / InvalidUtf8) instead of silently yielding garbage.
  4. Tag order is standard order. DataSet stores elements in a BTreeMap, so iteration is naturally ascending by (group, element), matching the order required for writing.
  5. Zero dependencies. Easy to embed in long-lived products such as a PACS, and avoids supply chain risk.

Usage

use dcm4rs::dictionary::tags;
use dcm4rs::{DataSet, Endian, Tag, Value, Vr};

let mut ds = DataSet::new();
ds.set_string(tags::PATIENT_NAME, Vr::Pn, "Doe^John");
ds.set_u16(tags::COLUMNS, 512);
ds.set(tags::PIXEL_DATA, Vr::Ow, Value::from_u16s(&[0, 1]));

assert_eq!(ds.string(tags::PATIENT_NAME).unwrap(), "Doe^John");
assert_eq!(ds.u16(tags::COLUMNS).unwrap(), 512);
assert_eq!(ds.get(tags::PATIENT_NAME).unwrap().tag, Tag::new(0x0010, 0x0010));

// Big-endian transfer syntax: bytes are stored big endian and declared when decoding
let mut big = DataSet::with_endian(Endian::Big);
big.set(tags::ROWS, Vr::Us, Value::from_bytes(vec![0x02, 0x00]));
assert_eq!(big.u16(tags::ROWS).unwrap(), 512);

// Dictionary lookup
let entry = dcm4rs::dictionary::lookup(tags::PIXEL_DATA).unwrap();
assert!(entry.vr.is_ambiguous()); // OB or OW, decided by context

Tests and checks

cargo test --workspace
cargo clippy --workspace --all-targets

Data dictionary

Generated from the official PS3.6 (currently 2026d): 5212 standard elements (retired ones included), 88 repeating groups, 5212 tag constants. The generator lives in cli/src/bin/dictgen.rs; it depends on dcm4rs to validate VRs and keeps no second VR list. The generated file is committed, so building needs no network access; see data/README.md to regenerate:

cargo run -p cli --bin dictgen -- data/part06.xml dcm4rs/src/dictionary/generated.rs

Not implemented yet (next phase)

  • Charset decoding: SpecificCharacterSet → encoding_rs encodings (including ISO 2022 escape sequences). Today string() only decodes UTF-8, so data in non-UTF-8 charsets returns InvalidUtf8. That step brings in encoding_rs (unless we maintain our own tables), turning "zero dependencies" into "only encoding_rs".
  • File and stream parsing/writing (encode): explicit/implicit VR, deflate, undefined lengths, encapsulated pixel data.
  • Transfer syntax registry and compression/decompression.
  • Pixel data decoding (RLE, the JPEG family).
  • UL/DIMSE network layer: a separate package dcm4rs-net (following pynetdicom and dcm4che-net) that depends on dcm4rs. Networking needs an async/thread runtime, so it stays out of the core library; dcm4rs remains lean (association, C-FIND/C-MOVE/C-GET, ...).

Reference implementations

Semantics follow pydicom / pynetdicom, conformance and edge-case encoding are checked against DCMTK, the async network API shape follows fo-dicom, and the enterprise feature list follows dcm4che.

License

MIT OR Apache-2.0.